A TLB miss occurs when the Translation Lookaside Buffer cannot find the virtual-to-physical address mapping needed for a memory access. Let me break down what this means and the subsequent actions.
What is a TLB?
The Translation Lookaside Buffer (TLB) is a small, fast cache that stores recent virtual-to-physical address translations. When a program accesses memory using a virtual address, the TLB is checked first to see if the translation is already available. This avoids the much slower process of walking through the page tables in main memory.
When Does a TLB Miss Occur?
A TLB miss happens when:
- The CPU needs to translate a virtual address to a physical address
- The required translation is not present in the TLB
- This forces the system to consult the page tables in main memory
TLB misses are common because the TLB is small (typically 64-1024 entries) compared to the total number of possible page translations a program might need.
Actions Following a TLB Miss
The response to a TLB miss depends on whether the missing page is valid and present in memory:
Case 1: Soft TLB Miss (Page is in Memory)
When the page is valid and present in physical memory, but just not cached in the TLB:
-
Hardware/Software Page Table Walk:
- The system performs a page table walk to find the translation
- Modern processors often do this in hardware for speed
- Some architectures rely on software (OS) to handle the walk
-
Retrieve the Page Table Entry (PTE):
- Navigate through the page table hierarchy (e.g., in x86-64: PML4 → PDPT → PD → PT)
- Each level requires a memory access, making this expensive (4 memory accesses for x86-64)
-
Check Page Permissions:
- Verify the access is allowed (read/write/execute permissions)
- Check if the page is marked as present
-
Update the TLB:
- Install the new translation in the TLB
- May require evicting an existing entry if the TLB is full
- Update any associated metadata (access bits, dirty bits)
-
Restart the Memory Access:
- The original memory instruction is retried
- Now the TLB contains the translation, so it should hit
Case 2: Hard TLB Miss (Page Fault)
When the page is not present in physical memory:
-
Generate a Page Fault Exception:
- Hardware detects that the page is not present (Present bit = 0 in PTE)
- CPU switches to kernel mode and invokes the page fault handler
-
Operating System Page Fault Handler:
- Check if the access is valid: Is this virtual address within a valid memory region (VMA) of the process?
- If invalid: Terminate the process (segmentation fault)
- If valid: Proceed with page loading
-
Find a Physical Page Frame:
- Allocate a free physical page frame
- If no free frames available, run the page replacement algorithm (LRU, FIFO, etc.)
- If replacement is needed, write the victim page to storage if it's dirty
-
Load the Page from Storage:
- Read the required page from disk/swap into the allocated physical frame
- This involves I/O operations and is very slow (milliseconds vs. nanoseconds)
-
Update Page Tables:
- Modify the page table entry to point to the new physical frame
- Set the Present bit and appropriate permission bits
- Update the TLB with the new translation
-
Resume Process Execution:
- Return control to the user process
- Restart the faulting instruction
- The memory access should now succeed
Performance Impact
The performance cost varies dramatically:
- TLB Hit: ~1 CPU cycle
- Soft TLB Miss: 10-100 CPU cycles (depending on page table walk complexity)
- Hard TLB Miss (Page Fault): Millions of CPU cycles due to disk I/O
Optimization Strategies
Systems use several techniques to minimize TLB miss penalties:
- Larger Page Sizes: Using 2MB or 1GB pages instead of 4KB reduces TLB pressure
- Multi-level TLBs: Separate TLBs for instructions and data, with L1 and L2 TLB hierarchy
- Hardware Page Table Walkers: Dedicated hardware to accelerate page table traversal
- TLB Prefetching: Speculatively loading likely-needed translations
- Process-Specific Address Space IDs (ASIDs): Avoid TLB flushes on context switches
The TLB miss handling mechanism is crucial for virtual memory performance, as it directly impacts how efficiently programs can access memory in modern computer systems.