How the MMU Works: A Complete Walkthrough of Address Translation
This article explains how the Memory Management Unit (MMU) translates virtual addresses into physical addresses and enforces memory protection. It walks through a full memory access cycle, covering virtual address bit-field splitting, TLB lookups, multi-level page table walks, permission and attribute checks, page fault handling by the operating system, and address space isolation during context switches. A conceptual pseudocode example and practical notes on performance tuning and troubleshooting are included to help readers understand the hardware foundation behind process isolation, virtual memory, and copy-on-write.
1. Introduction: Why the MMU Exists
In modern computer architecture, the operating system and applications see a continuous, independent, and mutually non-interfering address space, while the actual physical memory is shared among multiple processes. The key hardware unit responsible for this “space isolation and mapping” is the Memory Management Unit (MMU).
Your understanding of the MMU’s core function is correct: its essential task is to translate the virtual addresses issued by the CPU into physical addresses. But the real flow is far more than “receive an address, output an address.” In between, it involves permission checks, cache acceleration, multi-level table lookups, and exception triggering. The following sections break this down along a complete memory access cycle.
2. Overall Flow Overview
A typical address translation can be abstracted into the following chain:
CPU core issues a virtual address↓MMU receives and parses the address bit fields↓Look up the TLB (address translation cache)├── Hit → directly obtain the physical frame number└── Miss ↓Walk the multi-level page tables (Page Walk)├── Entry valid → refill the TLB, obtain the physical address└── Entry invalid → trigger a page fault exception, handled by the OS↓Permission and attribute checks (read/write/execute, user/kernel mode)↓Concatenate the page offset, output the physical address↓Access the cache hierarchy and main memory
3. Step-by-Step Breakdown
1. Receiving the Virtual Address and Splitting Bit Fields
The MMU does not treat the virtual address as a single whole. Taking the common 4KB page and 48-bit virtual address as an example, the address is split into three parts:
- The highest several bits: the first-level page table index;
- The middle several bits: the second-, third-, and fourth-level indices in sequence;
- The lowest 12 bits: the page offset, which does not participate in translation.
Illustration:
| 9bit L0 | 9bit L1 | 9bit L2 | 9bit L3 | 12bit offset |
The page offset does not need translation because data within a page is “shifted as a whole” between virtual and physical space; only the page start address needs to be mapped.
2. TLB Lookup: Ask the Cache First
If every memory access had to walk the four-level page table, a single memory access would degrade into five accesses, which is unacceptable for performance. Therefore, the MMU contains an address translation cache (TLB, Translation Lookaside Buffer) that stores recently used “virtual page number → physical frame number” mappings.
- Hit: the physical frame number is retrieved directly, and the flow is almost as fast as when there is no MMU;
- Miss: enter the page table walk flow, and after the walk completes, refill the result into the TLB.
The TLB is usually a small, high-speed fully associative or set associative store, with the number of entries ranging from tens to over a thousand. It also carries global entry markers, used to retain kernel mappings when switching address spaces.
3. Multi-Level Page Table Walk (Page Walk)
On a TLB miss, the MMU uses each segment index in the virtual address as a subscript and reads page table entries (PTEs) level by level:
- Use the L0 index to find the top-level page table base and read the L1 table frame number;
- Use the L1 index to locate the L2 table and read the L2 table frame number;
- Continue in the same way down to the last level;
- The last-level PTE provides the target physical frame number.
Each level’s PTE usually contains: a present bit, the physical frame number, an accessed bit, a dirty bit, and permission bits (readable/writable/executable, accessible in user mode or not). If a level’s present bit is 0, the mapping does not exist, the hardware stops the walk, and an exception is thrown.
To reduce walk overhead, architectures generally support huge pages (such as 2MB and 1GB). In this case, an intermediate-level PTE can directly provide the physical frame number, skipping the subsequent levels.
4. Permission and Attribute Checks
A successful translation does not mean the access is allowed. While producing the physical address, the MMU also compares the permission bits in the PTE against the current CPU privilege level:
- A user-mode program writing to a read-only page → triggers a protection fault;
- A user-mode program accessing kernel space → triggers a protection fault;
- A data segment attempting to execute → if the no-execute bit protection is enabled, it is likewise blocked.
This step is the hardware foundation for process isolation, code segment protection, and copy-on-write mechanisms.
5. Page Fault Exception and Operating System Involvement
When a PTE’s present bit is 0, or the permission check fails, the MMU pushes an error code (access type, whether it is user mode, whether it is an instruction fetch, etc.) onto the stack and triggers a page fault exception, handing control to the operating system’s page fault handler. The system’s typical subsequent actions include:
- Allocate a page from free physical memory, zero it, and establish a mapping;
- Read the swapped-out page back in from persistent storage or the swap area;
- Perform a copy for a copy-on-write page and establish a new mapping;
- If the address is illegal (such as dereferencing a null pointer), terminate the process and report an error.
After handling completes, control returns to the original instruction, and the MMU runs the translation flow again. This time it usually hits in the TLB.
6. Context Switching and Address Space Isolation
Each process has an independent page table root. When switching processes, the operating system changes the page table base register and invalidates the process-related entries in the TLB (or relies on TLB entries tagged with a process identifier to distinguish them), ensuring the new process does not mistakenly use the old process’s mappings. This is also one of the main sources of the fixed overhead of context switching.
4. Illustrative Code: From Concept to Implementation
The following pseudocode is only used to express the semantics of the flow and does not correspond to any specific platform interface:
// Conceptual illustration: high-level logic of one address translationphys_addr_t translate(virt_addr_t va, access_type_t type) {if (tlb_lookup(va, &pa)) // 1. Look up the TLB firstreturn check_perm(pa, type) ? pa : raise_fault();pte_t *pte = walk_page_tables(va); // 2. Multi-level walkif (!pte->present)page_fault(va, type); // 3. Hand over to the systemif (!perm_ok(pte, type))protection_fault(va, type);tlb_fill(va, pte->frame); // 4. Refill the cachereturn build_pa(pte->frame, offset(va));}
5. Focus Points in Engineering Practice
- Performance tuning: The TLB hit rate and page table walk depth directly determine memory access overhead. In practice, locality can be improved by using huge pages, reducing random access to large blocks of memory, and controlling the scale of resident memory.
- Troubleshooting: Abnormal process exits, out-of-bounds access, and performance jitter can often be located through metrics such as page fault counts and TLB miss rates.
- Cloud extension: In general cloud capability scenarios such as container platforms, log services, and monitoring alerts, memory- and address-translation-related metrics are often incorporated into the observability system for capacity planning and fault diagnosis; Baidu AI Cloud’s related products also follow this general engineering paradigm.
6. Summary
The MMU’s workflow can be summarized as a single main line: receive the virtual address and split the bit fields → look up the TLB first → on a miss, walk the multi-level page tables → check permissions and attributes → if successful, output the physical address; if it fails, trigger an exception for the operating system to fix → when switching address spaces, refresh the cache to ensure isolation. Understanding this main line means grasping the common foundation of process isolation, virtual memory, copy-on-write, and performance optimization.
