Virtual Memory Hardware (MMU & TLB)
How the MMU translates virtual to physical addresses with multi-level page tables, why the TLB is essential, page faults, protection bits and huge pages.
Every process sees its own private virtual address space, starting near 0 and spanning terabytes. The hardware memory management unit (MMU) translates each virtual address into a physical one on every single memory access. This lesson covers the hardware side; the OS side is in Paging & virtual memory.
Why virtual memory?
- Isolation — a process can't read or write another's memory.
- Simplicity — every program gets the same clean layout, regardless of what else is running.
- Overcommitment — the total virtual memory can exceed physical RAM; unused pages can live on disk.
- Sharing — shared libraries and copy-on-write
forkmap the same physical pages into many processes.
Pages and translation
Memory is divided into fixed-size pages (usually 4 KB). A virtual address splits into a virtual page number (VPN) and an offset; translation replaces the VPN with a physical frame number (PFN) and keeps the offset.
Each page table entry (PTE) holds the frame number plus bits:
| Bit | Meaning |
|---|---|
| Present / valid | The page is in RAM (otherwise → page fault) |
| Read / write / execute | Permissions (the NX bit prevents executing data pages) |
| User / supervisor | Whether user mode may access it |
| Accessed | Set by hardware on access (used by replacement algorithms) |
| Dirty | Set on write (the page must be written back before eviction) |
Multi-level page tables
A flat table for 2³⁶ pages × 8 bytes would be 512 GB per process. Instead x86-64 uses a 4-level tree (5-level on newer CPUs): each level is a 4 KB table of 512 entries, indexed by 9 bits of the VPN. Only the parts of the tree that are actually used exist.
The TLB: making translation fast
Walking 4 levels on every access would make each memory access 5 accesses. The translation lookaside buffer (TLB) is a small, highly associative cache of recent VPN → PFN translations.
| Typical size | Hit time | |
|---|---|---|
| L1 dTLB | 64–96 entries | ~1 cycle (in parallel with the L1 cache lookup) |
| L2 STLB | 1,500–3,000 entries | ~7 cycles |
| Page walk on a miss | — | 20–100+ cycles (page-table entries may themselves be cached) |
TLB reach = entries × page size. 1,536 entries × 4 KB covers only 6 MB, so programs with big, random working sets (databases, graph analytics, JVM heaps) suffer TLB misses.
Context switches and the TLB
Each process has its own page tables (its own CR3 value). Switching processes used to require flushing the TLB. Modern CPUs tag entries with an address-space ID (PCID on x86, ASID on ARM) so entries from several processes can coexist. Threads of the same process share page tables, so switching between them is cheaper.
Page faults
When the PTE isn't present, or the access violates permissions, the MMU raises a page fault and the OS handler decides what to do:
| Situation | OS action | Kind |
|---|---|---|
| Page is valid but not yet loaded (first touch, or a lazily mapped file) | Allocate or read the page, update the PTE, retry | Minor / major fault |
| Page was swapped out to disk | Read it back (milliseconds) | Major fault |
| Write to a copy-on-write page | Copy the page, make it writable | Minor fault |
| Invalid address or permission violation | Send SIGSEGV → "Segmentation fault" | Error |
Putting it together: one load instruction
- The CPU issues virtual address V.
- TLB lookup (in parallel with the L1 cache index lookup, using the page-offset bits).
- TLB hit → physical address; compare the cache tag → data. Usually ~4 cycles total.
- TLB miss → hardware page walk (which may itself hit in the caches) → fill the TLB → retry.
- PTE not present → page fault → OS handler → possibly disk I/O → retry the instruction.
- The MMU translates every virtual address: VPN → PFN, the offset unchanged.
- Multi-level page tables save space; x86-64 walks 4 (or 5) levels.
- The TLB caches translations; its reach is small, so huge pages help large working sets.
- PCID/ASID tags avoid flushing the TLB on every context switch.
- Page faults let the OS load pages lazily, implement copy-on-write, swap, and catch bad accesses.