The translation lookaside buffer (TLB) is a small hardware cache of page table entries, which map virtual page numbers to physical page numbers. Without a TLB, every memory access under paging first reads the page table, one access per level (four on x86-64). With a TLB, most accesses are translated without reading the page table.
Associativity
The TLB admits the same associativity options as other caches:
- Direct mapped: each virtual page number can occupy exactly one TLB slot. This organization is the simplest and has the fastest lookup, and it has the most conflict misses.
- Fully associative: a translation can occupy any slot, and the hardware compares every slot in parallel on each access. This organization is the most flexible and, because each slot needs its own comparator, the most difficult to build with many entries.
- N-way set associative: an intermediate design. The TLB is divided into sets, and a translation can occupy any slot within its set.
TLBs hold fewer entries than data caches, and each miss requires a page table walk, so TLBs are commonly built fully associative or with high set associativity.
The reach of a TLB, the memory it translates without a miss, is its entry count times the page size. The 96-entry first-level data TLB of an Intel Golden Cove core reaches 384 KiB of 4 KiB pages; a 2 MiB page needs one entry in place of 512.
Managing the TLB
The TLB satisfies almost all address translations, and the page table is consulted only on misses. On a miss, the translation is read from the page table and inserted into the TLB, usually replacing an existing entry.
A TLB miss is handled either in hardware or in software:
- Hardware, via the MMU, as on x86 and ARM. A hardware register (CR3 on x86) holds the location of the page table in memory, and the MMU walks the table itself. The OS maintains the page table, and it must keep it in the hardware-defined format.
- Software, by the OS, with a software-loaded TLB, as on MIPS. A TLB miss traps to the OS like a page fault. The OS finds the page table entry, loads it into the TLB, and restarts the instruction that missed. The handler runs on every miss and must therefore have low latency, so the ISA includes instructions for writing TLB entries (
tlbwron MIPS).
flowchart TD A[Memory access with virtual address] --> T{TLB hit?} T -->|hit| PA[Translation ready, access proceeds] T -->|miss| W[Walk page table, hardware MMU or OS trap] W --> V{PTE valid?} V -->|valid| FILL[Insert translation into TLB, evicting an entry] FILL --> RETRY[Retry the access, now a hit] V -->|invalid| PF[Page fault: OS must load the page first] style PA fill:#e8f5e9,stroke:#2e7d32 style RETRY fill:#e8f5e9,stroke:#2e7d32 style PF fill:#f9d0d0,stroke:#c00
Measured cost
On x86-64 a full page walk reads one entry per level, four with 48-bit addresses, but caches in the page walker hold upper-level entries, so most walks read fewer. On an Emerald Rapids virtual machine the TLB benchmarks measured 2.4 to 2.9 ns added by a second-level TLB hit, about 14 ns added by a page walk while the page tables fit in the data caches, and 50 to 300 ns at 1 to 8 GiB, where they do not.
Invalidation and shootdowns
The hardware does not keep the TLB consistent with the page table. When the OS removes a mapping or reduces its permissions, it invalidates the cached translation (invlpg on x86). Other CPUs running the address space may hold it too, and one CPU cannot invalidate another’s TLB, so the OS interrupts each and waits until it has, a TLB shootdown (Black et al. 1989).
Context switching
On x86 without process identifiers, loading CR3 to switch page tables flushes every TLB entry not marked global, a flag the kernel sets on its own mappings (Intel SDM 4.10.4). The misses that follow add to context switch cost. Tagging each entry with an address-space identifier (ASID on MIPS and ARM, PCID on x86) avoids the flush, since entries of different processes can then reside in the TLB at the same time.
Functionality built on page tables
- Memory protection: each PTE carries protection bits specifying access rights for the page, and the TLB caches them with the translation, so violations are detected in hardware, at much lower overhead than detection in software.
- Shared memory: multiple processes can map the same physical page. Shared libraries and shared memory use this, and copy-on-write builds on it.
Loading shared libraries
The OS loads a shared library once and maps the same physical pages into every process that uses it. The library need not occupy the same virtual address in every process, but its code pages can be shared only if no process patches them. Linux libraries are position-independent code, which reaches process-specific addresses through a per-process global offset table (GCC manual). A Windows DLL loaded away from its preferred address is relocated; Windows relocates it once and reuses that address in other processes, so the patched pages are shared (Chen 2016).
Memory-mapped files
A process accesses a memory-mapped file with loads and stores in place of read and write calls. In a shared mapping (MAP_SHARED), a store modifies the file’s page in the page cache, visible to other processes mapping it, and the OS writes it to disk later or on msync (mmap(2)). In a private mapping (MAP_PRIVATE), a store makes a copy-on-write copy and the file does not change. Mapping avoids a system call and a copy per access, at the cost of a page fault on first touch and a TLB shootdown on unmap.
Soft page faults
A soft (minor) fault is a fault on a page that is in memory but not mapped valid in the faulting process’s page table, so resolving it involves no disk I/O. Marking a resident page invalid notifies the OS of its next reference; Berkeley UNIX simulated the reference bit the VAX lacks this way (Babaoglu and Joy 1981), and Windows uses soft faults in its page replacement strategy; see Windows memory management.