From Virtual Address to Physical Memory: A Full Walkthrough
System DesignModel answerLast updated
Trace every step a CPU takes to resolve a virtual address — TLB, page-table walk, MMU checks, page faults, and context-switch invalidation.
Virtual Address Resolution
Modern processes never see physical RAM directly. Instead, each process operates in its own private virtual address space, and the hardware translates every memory reference to a real location at runtime.
Suppose a process executes a load instruction targeting some virtual address. Walk through everything that happens between that instruction and the moment data arrives in a register.
What your answer should cover
- How a virtual address is decomposed into page number and offset
- The role of the Translation Lookaside Buffer (TLB) and what happens on a hit vs. a miss
- How the Memory Management Unit (MMU) walks page tables when the TLB cannot satisfy a request
- Permission and validity bits, and how the hardware uses them
- The difference between a TLB miss (translation absent from the cache) and a page fault (page not resident or access is illegal)
- What the operating system does when it handles a page fault
- How address spaces are managed across context switches, including TLB invalidation
- At least one performance-oriented topic: huge pages, Address Space Identifiers (ASIDs), or multi-core TLB shootdowns
Constraints and assumptions
- Treat address translation and physical memory/cache access as two distinct stages — explain where the boundary is
- Acknowledge that page-table depth, cache-access ordering (VIPT vs. PIPT), and shootdown protocols differ by architecture; you do not need to commit to one, but note that variation exists
- A complete answer distinguishes a TLB miss from a page fault — they are not the same event
Clarifying questions worth raising
Before diving in, a strong candidate might ask:
- Should we assume a specific architecture (x86-64 4-level paging, ARMv8, RISC-V Sv48) or keep it architecture-agnostic?
- Is the target page resident in physical memory, currently swapped out, or entirely unmapped?
- Should the answer include multi-core scenarios such as TLB shootdowns on page-table updates?
Hints
Hint 1
Start from first principles: a virtual address has two logical parts. What are they, and what does each one tell the hardware?
Hint 2
A TLB miss and a page fault are handled by different actors — one is handled by hardware (or a hardware-triggered microcode walk), the other traps into the OS kernel. Be precise about which is which and when each occurs.
Hint 3
Think about what has to happen when the scheduler switches from process A to process B. The page tables change — what does that mean for the TLB, and how do ASIDs or PCIDs reduce the cost of that transition?
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
1. Decomposing the virtual address
A virtual address is split into two fields: a virtual page number (VPN) and a page offset. With a 4 KB page the bottom 12 bits are the offset (identical in the physical address); the upper bits identify which page. On x86-64 with 4-level paging those upper bits are further divided into four 9-bit indices — one per level of the page table hierarchy — plus a 9-bit index into the final page-table page.
2. TLB lookup (the fast path)
The MMU first consults the Translation Lookaside Buffer, a small, fully-associative or set-associative cache of recent VPN → physical frame number (PFN) mappings. A typical L1 TLB holds 32–128 entries; a unified L2 TLB may hold 1,024+.
TLB hit: The MMU reads the cached PFN, appends the page offset, and has a physical address. It simultaneously checks the entry's permission bits (read, write, execute, user/supervisor). A violation raises a protection fault. On a clean hit the whole path takes ~1 cycle.
TLB miss: The translation is not cached. This is not a page fault — it means only that the TLB does not have a copy of an otherwise valid mapping. The MMU (or OS on architectures with software-managed TLBs like MIPS) now walks the page tables.
3. Page-table walk on a TLB miss
On x86-64 the CPU's hardware page-table walker reads CR3, which holds the physical base address of the level-4 page-table (PML4). It uses each 9-bit VPN slice as an index into successive tables, following physical pointers at each level:
CR3 → PML4[VPN[3]] → PDPT[VPN[2]] → PD[VPN[1]] → PT[VPN[0]] → PFN
Each entry is 8 bytes and contains the next-level physical address plus flags. The hardware sets the Accessed bit on every entry it traverses and the Dirty bit on the final PTE when the access is a write. After a successful walk the MMU loads the new VPN→PFN mapping into the TLB and retries.
The walk touches up to four additional memory references, which is why a populated TLB is so valuable — and why page-table pages themselves are usually kept in the CPU's L1/L2 caches.
4. Page fault: when the PTE is not present
If the Present bit in the final PTE is clear, the hardware raises a page fault exception, saving the faulting address in a control register (CR2 on x86). The OS kernel's page-fault handler then decides:
- Valid but not resident (demand paging / swap): Allocate a physical frame, read the page from disk or the swap partition, update the PTE, set Present, flush or reload the TLB entry, and return to retry the instruction.
- Copy-on-write: Allocate a new frame, copy the page, update the PTE to point to the new frame with write permission, and return.
- Genuinely unmapped or a protection violation: Deliver
SIGSEGVto the process.
The kernel distinguishes these cases using its own virtual memory area (VMA) descriptor for the process, not just the PTE itself.
5. Physical memory and cache access
Once a physical address is known, the CPU issues a read to its cache hierarchy (L1 → L2 → L3 → DRAM). Whether the cache is indexed before or after translation (VIPT vs. PIPT) is an architectural detail, but the key point is that translation and caching are sequential stages — the physical address must be known before a PIPT cache can be checked, whereas a VIPT cache can begin the lookup in parallel with the TLB using the offset bits (which are the same in both virtual and physical addresses when page size ≥ cache-set size).
6. Context switches and TLB invalidation
When the scheduler replaces process A with process B, CR3 is reloaded with B's page-table root. Because the TLB entries hold A's mappings, they must be invalidated — otherwise B could read A's memory.
The naive approach flushes the entire TLB on every switch, which is expensive on a workload with many short time-slices. The common optimization is Process-Context Identifiers (PCIDs) on x86-64 or ASIDs on ARM: each TLB entry is tagged with a process identifier, so the hardware can keep entries from multiple processes simultaneously and avoid a full flush on every switch.
7. Multi-core shootdowns
When one core modifies a PTE (e.g., the kernel unmaps a page), every other core that might have cached that mapping in its TLB must be told to invalidate it. This is done via an inter-processor interrupt (IPI) — the modifying core sends a shootdown IPI, other cores execute INVLPG (or equivalent) for the affected virtual address, and acknowledge. Only then can the modifying core safely reclaim or remap the physical frame.
Shootdowns are a meaningful bottleneck in large multi-socket systems and in hypervisors that manage many guest address spaces simultaneously.
8. Huge pages
Standard 4 KB pages mean a 1 GB working set requires 262,144 TLB entries. Huge pages (2 MB on x86-64; 1 GB is also supported) reduce that to 512 entries, dramatically improving TLB reach at the cost of internal fragmentation and the added complexity of keeping large contiguous physical frames available. Databases and JVM heaps often opt in explicitly.
Common mistakes to avoid
- Conflating a TLB miss with a page fault — they have different handlers and very different costs.
- Forgetting that the page-table walk itself hits the cache hierarchy, not bare DRAM.
- Assuming TLB flushes are free across context switches — they are not, and ASIDs/PCIDs exist precisely to reduce this cost.
- Treating permission checks as an OS-level operation — the MMU enforces them in hardware before a fault is ever raised.