Lecture 11: Virtual-Physical Caches

Section 1: Demand Paging, Page Faults, and Pipeline Exception Handling

Virtual memory provides two pillars for modern systems—the illusion of a large, private, uniform store:

  1. Protection & Privacy: Multiple users or processes each have their own private address space (plus one or more shared address spaces). Each page table defines a private name space (page table \equiv name space).
  2. Demand Paging: Uses DRAM as a software-managed “cache” for secondary storage (disk), allowing programs larger than physical primary memory to run and hiding differences in hardware memory configurations across machines.

Let’s trace the complete end-to-end flow of address translation, seeing exactly which steps are handled in hardware versus software:

  1. A Virtual Address arrives at the TLB Lookup (hardware).
  2. If TLB Hit \to Protection Check (hardware):
    • If permitted, the translated Physical Address goes to the cache.
    • If denied, hardware raises a Protection Fault (software OS handles it, typically delivering a SEGFAULT).
  3. If TLB Miss \to Page Table Walk (hardware MMU or software handler):
    • If the page is in memory \to Update TLB (hardware or software) and proceed. (Notice a key detail: where does updating the TLB resume? Usually the protection check / instruction re-execution).
    • If the page is not in memory (\notin memory) \to Page Fault (software OS loads the page from disk).

Inside the Page-Fault Handler

When the referenced page is not in DRAM, the OS Page-Fault Handler takes over:

  1. The missing page is located on disk (or newly created if it is a fresh zero-filled page).
  2. If no free physical frames remain in DRAM, the OS selects a victim page to swap out to disk (often using a pseudo-LRU replacement policy implemented in software; the OS can also maintain a pool of free pages as a background activity).
  3. The requested page is read from disk into DRAM, and the page table is updated. Because reading from disk takes milliseconds (millions of CPU cycles), page faults are always handled completely in software by the OS, using an untranslated addressing mode so the kernel can safely read and modify page tables. While the faulting job waits for the disk transfer, the OS saves its architectural context and lets another user job run on the CPU.

Handling VM Exceptions in the Pipeline

Because both Inst. TLB (Fetch stage) and Data TLB (Memory stage) can trigger a TLB miss, Page Fault, or Protection violation, the processor must support restartable exceptions. After the software OS handler brings the missing page in from disk, it must be able to resume the interrupted program cleanly. While it is possible to build imprecise restartable exceptions, doing so greatly complicates OS software; precise exceptions make restarting straightforward. (Protection violations may abort the process, though they are often routed through the same exception mechanism as page faults.)

Coping with TLB Latency

Look closely at the pipeline again: placing the Inst. TLB before the Inst. Cache and the Data TLB before the Data Cache puts the TLB in series with the cache lookup. We must now wait for TLB translation before we can even index the cache! We have four options to cope with this extra latency:

  1. Slow down the clock (unacceptable for high performance).
  2. Pipeline the TLB and cache access (increases branch misprediction and load-use latency).
  3. Virtual-address caches (index and tag the cache with virtual addresses, eliminating the TLB from the hit path).
  4. Parallel TLB/cache access (overlap TLB translation with cache indexing).

[Editor Note: removed redundant summary slide of previous section and two duplicate recap diagrams.]

✎ Check Your Understanding

  1. Why are TLB misses sometimes handled in hardware, whereas page faults are always handled in software by the OS? Answer: A TLB miss only requires walking page tables in DRAM (tens of nanoseconds), which hardware can do rapidly without flushing an out-of-order pipeline. A page fault requires reading from disk (milliseconds), which takes millions of cycles—so software OS handling has negligible overhead and allows the OS to context-switch to another job.

  2. Why must a page fault be a restartable exception? Answer: Because the faulting instruction has not yet accessed its memory data; once the OS brings the missing page from disk into DRAM and updates the page table, the processor must re-execute the faulting instruction from the beginning.


Section 2: Virtual Memory and Cache Interaction (VIVT, VIPT, and Anti-Aliasing)

Let’s examine the two high-speed alternatives for hiding TLB latency: Virtual-Address Caches and Concurrent TLB/Cache Access.

Option 1: Virtual-Address Caches (Virtually Indexed, Virtually Tagged — VIVT)

Instead of placing the TLB before the cache (CPU -> VA -> TLB -> PA -> Physical Cache -> PA -> Primary Memory), we place the cache before the TLB: \text{CPU} \xrightarrow{\text{VA}} \text{Virtual Cache} \xrightarrow{\text{VA}} \text{TLB} \xrightarrow{\text{PA}} \text{Primary Memory} (Used, for example, in the StrongARM processor.)

In this pipeline, the CPU sends the Virtual Address straight into the Inst. Cache and Data Cache. Translation happens only on a cache miss: on a Miss?, the virtual address goes to the Inst. TLB or Data TLB to get the physical address for the Memory Controller.

The general solution to aliasing in a virtual cache is to prevent aliases from coexisting in the cache using one of two strategies:

  1. Design cache Fill/Evict hardware to enforce block mutual exclusive residency (evict any existing alias before filling the new one).
  2. Search and deduplicate using physical tags (which brings us to Option 2 and L2 anti-aliasing).

Option 2: Concurrent Access to TLB and Cache (Virtually Indexed, Physically Tagged — VIPT)

Can we get the one-step speed of a virtual cache and the clean physical identity of a physical cache? Look at the bits of a Virtual Address (VA) and Physical Address (PA): \text{VA} = [\ \text{VPN}\ |\ \text{Page Offset (}k\text{ bits)}\ ] \text{PA} = [\ \text{PPN}\ |\ \text{Page Offset (}k\text{ bits)}\ ] Remember that translation never changes the k-bit Page Offset! Now consider a direct-mapped cache with 2^L blocks (sets), each of size 2^b bytes:

Suppose L + b \le k. Then the entire L-bit cache index lies completely inside the untranslated k-bit Page Offset! Because Index L is available without consulting the TLB, cache indexing and TLB lookup can begin simultaneously! While the TLB translates VPN into PPN, the cache reads out the Physical Tag and Data at index L. When both finish, a single comparator (=) checks whether the cache’s Physical Tag matches the TLB’s PPN to determine hit?. Because the tags are physical, aliases cannot mismatch on tag comparison—and if L + b \le k, two aliases map to the exact same cache index too!

Scaling VIPT Caches Beyond One Page Size

Wait—what happens if we want a cache larger than one page (2^k bytes)? If we keep a direct-mapped cache and increase L so that L + b > k, then the cache size 2^{L+b} exceeds the page size 2^k. Notice what happens to the Virtual Index:

How do real processors build large L1 caches without aliasing? Increase associativity instead of index bits! Fix the index length to L = k - b so that the Virtual Index stays strictly inside the k-bit Page Offset. To scale the cache up by a factor of 2^a, replicate the direct-mapped array 2^a times (making a 2^a-way set-associative cache). All 2^a ways are indexed in parallel using the untranslated L bits; once the TLB outputs PPN, 2^a physical tags are compared in parallel to select the matching Data way.

Worked Example: AMD Zen 5 L1D Cache

An AMD Zen 5 core (EPYC 9575F) has:

How many cache lines are in the cache, and what must its associativity be so that it can be virtually indexed without aliasing?

  1. Total number of cache lines: \frac{48 \times 1024\text{ bytes}}{64\text{ bytes/line}} = 768\text{ cache lines}
  2. Maximum alias-free index bits (L = k - b): L = 12 - 6 = 6\text{ bits} \implies 2^6 = 64\text{ sets}
  3. Required associativity: \text{Associativity} = \frac{\text{Total cache lines}}{\text{Number of sets}} = \frac{768}{64} = \frac{\text{cache size}}{\text{page size}} = \frac{48\text{ KB}}{4\text{ KB}} = \mathbf{12\text{-way set-associative}} Each of the 12 ways holds 64 \text{ sets} \times 64 \text{ B} = 4\text{ KB} (exactly one page), so all 48\text{ KB} is accessed concurrently with the TLB with zero aliasing. Notice that 12 is not a power of two: the alias-free rule fixes the size of one way at one page, not the number of ways.

You can see the 12 ways directly with a pointer chase over lines spaced exactly 4\text{ KB} apart, which all map to the same set: up to 12 such lines keep hitting in L1 at 4 cycles, while 14 no longer fit and every access falls to L2 at about 14 cycles.

Anti-Aliasing via an Inclusive L2 Cache (When L1 > Page Size or VIVT)

What if a designer does want a large direct-mapped (or low-associativity) L1 cache where L + b > k, or even a fully virtually indexed/tagged L1 cache? We can prevent aliasing using a Unified L2 Cache that is inclusive of both the L1 Instruction and L1 Data caches (meaning L2 is guaranteed to hold a copy of every line currently in either L1).

Look at how the MIPS R10000 (1996) solved this for a VIPT L1 cache with L + b > k:

The exact same principle works for a Virtually Indexed, Virtually Tagged L1 (L1 VA Cache) backed by a Physically Indexed/Tagged L2 (L2 PA Cache) that “contains” L1: L2 stores the "Virtual Tag" (VA1) alongside PA. On any L1 miss that hits in L2 under a different virtual address \text{VA}_2, L2 uses the stored VA1 to invalidate the old alias in L1 before loading \text{VA}_2.

[Editor Note: removed two blank transition slides.]

✎ Check Your Understanding

  1. For a VIPT cache with 4\text{ KB} pages (k=12) and 32\text{ B} cache lines (b=5), what is the largest direct-mapped cache size that guarantees no aliasing? If you build a 64\text{ KB} VIPT cache instead, what minimum associativity avoids aliasing? Answer: The largest alias-free direct-mapped cache equals the page size: 4\text{ KB} (L = 12 - 5 = 7 \implies 2^7 = 128 sets of 32\text{ B}). For 64\text{ KB}, you need 64\text{ KB} / 4\text{ KB} = \mathbf{16\text{-way}} associativity.

  2. In the MIPS R10000 anti-aliasing scheme, why must the physically indexed/tagged L2 cache store the extra a1 bits from the virtual address inside its L2 tag? Answer: Because when a second alias \text{VA}_2 misses in L1 and hits PA in L2, L2 needs a1 to detect the virtual-index mismatch and locate the exact L1 set where \text{VA}_1 resides so it can purge \text{VA}_1 from L1.


Section 3: Historical Evolution and Modern Deployment of Virtual Memory

Virtual memory features directly track the evolution of computing workloads:

Today, virtual memory deployment splits cleanly by domain:

  1. Servers, desktops, laptops, and smartphones: Use full demand-paged virtual memory for portability across memory sizes, isolation between users/tasks, sharing DRAM among active tasks, and simplifying OS features.
  2. Vector supercomputers and modern GPUs: Provide translation and protection, but rarely complete demand paging (older Crays used base & bound; Japanese supercomputers and Cray X1/X2 used pages). Why avoid demand paging? You cannot afford to waste hugely expensive compute hardware thrashing to disk (batch jobs are sized to fit in memory), and implementing restartable exceptions on wide vector instructions is extremely difficult.
  3. Most embedded processors and DSPs: Provide physical addressing only. They cannot afford the area, speed, or power budget for TLBs and page-table walkers, often have no secondary storage to swap to, run custom software written for a fixed hardware memory map, and have exposed pipeline architectures where precise restartable exceptions are hard to implement.

[Editor Note: removed acknowledgements slide.]

✎ Check Your Understanding

  1. Why do modern GPUs and vector supercomputers typically implement address translation and protection without full demand paging to disk? Answer: They run jobs sized to fit in memory to avoid wasting massive compute throughput thrashing to disk, and implementing precise, restartable exceptions for wide vector instructions is very difficult.