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.

Advanced 4 min readLesson 12 of 15

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 fork map 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.

100101101110virtual page number (36 bits)offset (12 bits)
48-bit virtual address with 4 KB pages (bits not to scale)

Each page table entry (PTE) holds the frame number plus bits:

BitMeaning
Present / validThe page is in RAM (otherwise → page fault)
Read / write / executePermissions (the NX bit prevents executing data pages)
User / supervisorWhether user mode may access it
AccessedSet by hardware on access (used by replacement algorithms)
DirtySet 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.

CR3root pointerPML4bits 47–39PDPTbits 38–30PDbits 29–21PTbits 20–12frame+ offset
A page walk: four dependent memory reads before the real access.

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 sizeHit time
L1 dTLB64–96 entries~1 cycle (in parallel with the L1 cache lookup)
L2 STLB1,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:

SituationOS actionKind
Page is valid but not yet loaded (first touch, or a lazily mapped file)Allocate or read the page, update the PTE, retryMinor / major fault
Page was swapped out to diskRead it back (milliseconds)Major fault
Write to a copy-on-write pageCopy the page, make it writableMinor fault
Invalid address or permission violationSend SIGSEGV → "Segmentation fault"Error

Putting it together: one load instruction

  1. The CPU issues virtual address V.
  2. TLB lookup (in parallel with the L1 cache index lookup, using the page-offset bits).
  3. TLB hit → physical address; compare the cache tag → data. Usually ~4 cycles total.
  4. TLB miss → hardware page walk (which may itself hit in the caches) → fill the TLB → retry.
  5. PTE not present → page fault → OS handler → possibly disk I/O → retry the instruction.
Key takeaways
  • 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.