TLB Miss: The "Address Not Found" Scenario
Let's break down what a TLB miss is and the steps the system takes to recover from it.
1. Recap: What is a TLB?
The Translation Lookaside Buffer (TLB) is a tiny, super-fast cache used by the Memory Management Unit (MMU) to speed up virtual-to-physical address translation.
Here's the background:
- Virtual Memory: Programs use virtual addresses (e.g.,
0x12345678). These aren't direct memory locations.
- Physical Memory: RAM is organized by physical addresses (e.g.,
0x87654321). The OS must map virtual → physical.
- Page Tables: The OS stores these mappings in multi-level page tables (a hierarchical tree-like structure). Walking this tree is slow (multiple memory accesses).
- TLB's Job: Cache the most recently used virtual→physical mappings. Think of it as a shortcut.
TLB Entry Example:
| Virtual Page Number (VPN) | Physical Frame Number (PFN) | Flags (Valid, Dirty, etc.) |
|---|
0x1234 | 0x5678 | Valid = 1, Read-Only |
0xABCD | 0xEF01 | Valid = 1, Read-Write |
When a program accesses memory:
- The CPU generates a virtual address.
- The MMU splits it into:
- Virtual Page Number (VPN): The "index" of the page.
- Page Offset: The byte within that page.
- The MMU checks: "Is the VPN → PFN mapping already in the TLB?"
- TLB Hit: Yay! Mapping found. Combine PFN + Offset → Physical Address. Access memory. Done.
- TLB Miss: Uh-oh. Mapping not found in TLB. Now we're in trouble.
2. What is a TLB Miss?
A TLB miss occurs when:
- The requested Virtual Page Number (VPN) isn't present in the TLB cache.
- This means the MMU can't quickly translate the virtual address to a physical address.
Causes of a TLB Miss:
- Cold Miss: The program just started, and the TLB is empty (like opening a new browser for the first time).
- Capacity Miss: The TLB is full, and the needed entry was evicted (LRU policy kicked it out).
- Conflict Miss: (Less common in fully associative TLBs) Multiple VPNs map to the same TLB slot.
Example Scenario:
Program tries to access virtual address 0x4000C000. The MMU:
- Extracts
VPN = 0x4000 (top bits).
- Checks TLB: "Do I have
0x4000 → PFN cached?"
- Nope! TLB miss. Now what?
3. Handling a TLB Miss: The Recovery Process
The system follows a well-defined sequence to resolve the miss. Here's what happens in hardware and OS cooperation:
Step 1: Hardware Traps to the OS (TLB Miss Exception)
- The MMU raises a TLB Miss Exception (or Page Fault in some architectures).
- The CPU stops executing the current instruction stream.
- Context switch: Save the current state (registers, PC) and switch to kernel mode.
- Jump to a fixed address in the OS kernel: the TLB Miss Handler (part of the OS's memory management code).
Step 2: OS Walks the Page Tables (Slow Memory Accesses)
The TLB Miss Handler now runs. Its job: "Find the missing VPN → PFN mapping in the page tables."
Page Table Walk (Multi-Level):
- The OS knows the Page Table Base Register (PTBR) → points to the root of the page table tree.
- It reads:
- Level 1 Page Table Entry (PTE) at
PTBR + (VPN >> 18) ← First memory access.
- If valid: follow the pointer to Level 2 PTE at
L1_PTE + (VPN >> 9) ← Second memory access.
- If valid: final Level 3 PTE at
L2_PTE + (VPN & 0x1FF) ← Third memory access.
- Finally: Read the PTE (Page Table Entry) containing:
- PFN (Physical Frame Number): The RAM location.
- Flags: Is it valid? Read-only? Swapped to disk?
Example (x86-64, 4KB pages, 4-level tables):
- Virtual Address:
0x4000C000
- Split:
VPN = 0x4000C, Offset = 0x000
- Walk:
- Read PML4 (top level) at
CR3 + 0x400.
- Follow pointer to PDP (next level) at
PML4[0x400].
- Read PD (next level) at
PDP[0x0C].
- Final PT (page table) at
PD[0x000].
- Extract PFN from
PT[0x0C00]. Say it's 0x8F200.
Cost: 3–4 memory accesses. Ouch! That's ~100–200 clock cycles (vs. ~1 cycle for TLB hit).
Step 3: Update the TLB
Now the OS has the mapping: VPN 0x4000C → PFN 0x8F200.
- Insert into TLB: Update the TLB cache with the new entry.
- If TLB is full, evict the Least Recently Used (LRU) entry.
- Write:
VPN = 0x4000C | PFN = 0x8F200 | Flags (Valid=1, User=1)
- TLB Shootdown (Optional): If this is a shared TLB (multiple cores), send an inter-processor interrupt (IPI) to invalidate stale TLB entries on other CPUs.
Step 4: Retry the Faulting Instruction
- Context switch back to user mode.
- Restore registers and the exact PC (program counter) where the fault occurred.
- Re-execute the load/store instruction.
Now it's a TLB Hit!
- MMU re-checks the TLB: "Oh,
0x4000C → 0x8F200 is here!"
- Combines
PFN 0x8F200 + Offset 0x000 → Physical Address 0x8F2C000.
- Accesses RAM at
0x8F2C000. Success!
4. Variations: Soft vs. Hard TLB Miss
Not all TLB misses are equal:
Soft TLB Miss:
- The page is in physical memory, but not in the TLB.
- OS just walks page tables, updates TLB, and retries.
- Cost: ~100–200 cycles (page table walk).
Hard TLB Miss (Page Fault):
- The page isn't even in RAM; it's swapped to disk (e.g.,
/swapfile).
- Major page fault.
- OS:
- Allocates a free physical frame.
- Reads page from disk (10–20 million cycles latency!).
- Updates page tables.
- Inserts into TLB.
- Retries instruction.
Example: Firefox needs a library page not loaded yet. Disk I/O ensues.
5. Optimizations to Reduce TLB Misses
Since TLB misses are costly, hardware and OS designers employ tricks:
- Larger TLBs: More entries = fewer misses (but more power-hungry).
- Multi-Level TLBs (L1/L2 TLB): Small L1 TLB + larger L2 TLB.
- Huge Pages (e.g., 2MB, 1GB): Fewer TLB entries needed for large allocations.
- Prefetching: Predictively load nearby page table entries into TLB.
- Page Table Caching (PTE caches): Store intermediate page walks in hardware.
- Lazy TLB Shootdowns: Batch TLB invalidations during context switches.
Summary: TLB Miss Flowchart
- CPU generates virtual address.
- MMU checks TLB:
- Hit: Translate & access memory.
- Miss: Raise exception.
- Hardware traps to OS (TLB Miss Handler).
- OS walks page tables (multiple memory reads).
- OS inserts VPN → PFN into TLB.
- Return to user mode, retry instruction.
- Now: TLB Hit, memory access succeeds.
TLB Hit: ~1 cycle.
Soft TLB Miss: ~100–200 cycles.
Hard TLB Miss (Page Fault): ~10–20 million cycles (disk I/O).
The TLB is your CPU's best friend for speed. But when that friendship fails (TLB miss), the OS steps in to save the day—at a cost.