Aprelius logo
uptime: 00:00:00
Computer Science

MMU

The Memory Management Unit (MMU) is the CPU hardware that translates virtual addresses into physical addresses and checks page permissions on every access. The OS defines the mapping by building the page tables — but it isn't involved in each load/store.

Every instruction the CPU executes carries a virtual address. The MMU resolves it on the fly using either a cached translation from the TLB or a hardware page-table walk, raising a page fault when the access isn't allowed.

TLB

Without a cache, every memory access would trigger a multi-level walk through RAM — the translation would cost more than the access itself. The TLB (Translation Lookaside Buffer) is the MMU's small, fast cache of recent virtual -> physical mappings.

  • TLB hit -> translation done in a cycle.
  • TLB miss -> hardware page walk, then the mapping is cached in the TLB.
  • Huge pages (2MB and 1GB pages on x86-64) map a big range with a single entry, so fewer TLB entries cover more memory. That's why databases and JVMs often enable them — real programs working over large heaps can spend a visible share of time on TLB misses otherwise.

Context switching

Each process gets its own virtual address space, which means its own page table hierarchy and its own root register (CR3 on x86-64). Swapping CR3 swaps the entire memory view.

Stale translations are the risk: an old process's TLB entries could let a new process see memory it shouldn't. Two solutions:

  • Flush the TLB on every switch — simple, but cold TLB = slow starts.
  • Tag entries — ARM uses ASIDs, x86 uses PCIDs, so the TLB can hold translations for several processes at once and match them by tag instead of flushing.

Why this design pays off

Two OS features lean directly on the MMU:

  • Copy-on-writefork() maps the child's pages to the same physical pages as the parent, but read-only. When either side writes, the protection fault fires and the OS copies the page then. Forking a process becomes nearly free, and only pages that actually diverge get copied.
  • Shared memory / mmap — multiple virtual addresses (possibly in different processes) can point at the same physical page, so two programs can share data without copying it.

References

  • MMU on OSDev wiki