Virtual Memory: Translation, TLBs, Faults, and COW
A process uses virtual addresses. Hardware and the OS translate them into physical addresses.
Core promise:
- isolation
- private address-space illusion
- efficient sharing
1 / 15
A process uses virtual addresses. Hardware and the OS translate them into physical addresses.
Core promise:
Without virtual memory:
Virtual memory makes every process feel private while still letting the OS share physical pages.
For page size 4096:
VPN = virtualAddress / 4096
offset = virtualAddress % 4096
If:
VPN 2 -> PFN 10
then:
physicalAddress = 10 * 4096 + offset
The offset is preserved.
A PTE is not only a frame number.
Typical simplified fields:
Translation and protection are coupled.
No usable mapping:
page fault
Mapping exists but operation violates permission:
protection fault
Do not mix them.
The TLB is a translation cache.
(address-space id, VPN) -> PFN + permissions
The page table is the source of truth.
A TLB miss is not necessarily a page fault.
Capacity 2:
trace: 1 2 1 3 1 2
Result:
hits=2
misses=4
On hit: refresh recency. On miss: evict least recently used if full.
Same VPN, different process:
PID 1: VPN 7 -> PFN 100
PID 2: VPN 7 -> PFN 200
A TLB key cannot be just VPN.
Use:
(PID or ASID, VPN)
or flush on context switch.
If the page table changes:
VPN 5 -> PFN 10
VPN 5 -> PFN 20
old TLB entries must be invalidated or updated.
Stale translations can corrupt memory.
Working set = pages used recently.
If the active set fits:
If it does not fit:
Before fork:
parent -> A, B
ref(A)=1, ref(B)=1
After fork:
parent -> A, B
child -> A, B
ref(A)=2, ref(B)=2
Shared mappings become read-only/COW.
Child writes page A.
Wrong:
mutate A in place
Correct:
copy A -> C
child -> C
parent stays -> A
ref(A)--
ref(C)=1
Invariant: shared pages are not mutated in place.
For every physical page:
refcount == number of live mappings to that page
Fork increments. COW copy decrements old and creates new. Exit decrements and frees when zero.
Correct VM: