Lecture 10: Address Translation
Section 1: Bare Machines and the Need for Dynamic Address Translation
Consider the simplest possible processor running a single program directly on hardware—a bare machine. In a bare machine, there is only one kind of address: a physical address, which corresponds directly to the address lines of the actual hardware memory (DRAM). Look at how a basic 5-stage pipeline operates in this world:
- The Program Counter (
PC) outputs a physical address directly to the Instruction Cache (Inst. Cache). - After
Decodeand execution (E, where an adder computes effective memory addresses), the Memory stage (M) sends a physical address directly to the Data Cache (Data Cache). - On a cache miss, both caches send physical addresses to the Memory Controller, which drives those exact addresses onto Main Memory (
DRAM).
Early computers, as well as the first minicomputers and microcomputers, worked exactly this way. They ran only one program at a time, and that program had unrestricted access to all of physical memory and all I/O devices. As subroutine libraries became popular, programmers wrote routines in location-independent form so different programs could combine different routines. To run a program on a bare machine, a linker or loader program had to statically relocate all library modules to actual fixed locations in physical memory before execution began.
Why did this model break down? Three pressures forced hardware designers to invent dynamic address translation:
- Slow I/O and Multiprogramming: In early machines, I/O devices were vastly slower than the CPU, and every transfer involved the CPU directly (programmed I/O), leaving the processor idle while waiting for disks or printers. Higher throughput becomes possible if the CPU runs one program while another waits on I/O, overlapping CPU and I/O across two or more programs. Achieving this multiprogramming (using DMA I/O devices and interrupts) requires keeping multiple programs—alongside a resident OS—simultaneously in physical memory (e.g.,
OSat the bottom,Program 2in the middle,Program 1higher up). - Location-Independent Programs: If Program 1 and Program 2 are compiled statically for the same physical addresses, they cannot sit in different regions of DRAM simultaneously unless a loader rewrites their addresses every time they move. For programming and storage management ease, hardware needs a base register so a program can run from any starting physical location without relinking.
- Protection: When independent programs coexist in DRAM alongside the OS, a bug in Program 1 must not corrupt Program 2 or the OS. Hardware needs a bound register to confine each program to its allocated region.
Together, multiprogramming requires resident supervisor software (the OS) to manage context switches between programs and configure these hardware translation and protection mechanisms.
[Editor Note: removed course title slide, instructor information, and website links.]
✎ Check Your Understanding
In a bare machine, what two software tools were responsible for placing location-independent library modules at specific physical memory addresses before a program ran? Answer: The linker or loader program statically relocated library modules to actual locations in physical memory.
What hardware problem motivated keeping multiple programs in memory at once, and what two registers were introduced to make that safe and flexible? Answer: Slow I/O left the CPU idle during transfers, motivating multiprogramming to overlap one program’s CPU work with another’s I/O. A base register provides location independence, and a bound register provides protection between programs.
Section 2: Base-and-Bound Translation and the Limits of Segmentation
To implement dynamic address translation without rewriting program binaries, we introduce Simple Base and Bound Translation. Instead of generating physical addresses directly, the running program operates inside its own Program Address Space starting at address 0 and generates logical addresses.
Consider what happens when an instruction executes Load X:
- The logical address X is compared against the Bound Register (which holds the Segment Length). If \text{Logical Address} \ge \text{Bound Register}, the hardware immediately flags a Bounds Violation exception.
- In parallel, the logical address X is added to the Base Register (which holds the Base Physical Address of the Current Segment): \text{Physical Address} = \text{Base Register} + \text{Logical Address} Let’s unpack this equation:
- \text{Logical Address} is the offset generated by the program relative to the start of its own address space (0 up to \text{Bound} - 1).
- \text{Base Register} is the starting physical address in DRAM assigned to this program’s contiguous block (Current Segment).
- Adding them translates the program’s local view into the actual hardware address in physical memory.
Crucially, base and bounds registers are visible and accessible only when the processor is running in supervisor mode. If a user program could modify its own base or bound register, it could read or overwrite any region of physical memory, completely defeating protection.
Separating Program and Data Segments
Instead of a single base/bound pair for the entire program, we can split a program’s memory into separate areas for program and data (a scheme used on all Cray vector supercomputers prior to the X1 in 2002). In this design:
- Instruction fetches use the Program Counter, checked against the Program Bound Register (\ge triggers a Bounds Violation) and added to the Program Base Register to access the Program Segment in physical memory.
- Data accesses (like
Load X) use the Data Bound Register and Data Base Register to access the Data Segment.
What is the advantage of this separation? First, instructions (code) are typically read-only; isolating the program segment prevents stray data writes from corrupting instructions, and allows multiple users running the same program (such as an editor or compiler) to share a single physical Program Segment while keeping their Data Segments private. Second, the data segment can grow or be relocated independently of the code segment. Extending this idea to more base/bound pairs (for example, separate segments for code, stack, and heap) yields general segmentation.
Building Base and Bound into the Pipeline
How do we integrate separate program and data base/bound checks into a 5-stage pipeline?
- In the Fetch stage, the
PC(a logical address) is checked against theProgram Bound Register(\ge?) and added to theProgram Base Registerbefore indexing theInst. Cachewith a physical address. - In the Memory stage, the computed logical data address (from
Estage’sregister + immediatecalculation) is checked against theData Bound Register(\ge?) and added to theData Base Registerbefore indexing theData Cachewith a physical address.
You might worry that adding the Data Base Register in the Memory stage adds an entire extra addition delay onto the critical path before accessing the Data Cache. We avoid that slowdown by folding the addition of the base register into the (register + immediate) address calculation using a carry-save adder. Recall that a carry-save adder takes three binary numbers (register + immediate + Data Base) and, in just one layer of full adders (a few gate delays), reduces them to two numbers (a sum vector and a carry vector), which are then added by one standard carry-propagate adder. Thus, summing three numbers takes only a few gate delays more than adding two numbers.
Why Segmentation Fails: Fragmentation
Even with multiple segments, contiguous allocation breaks down due to two forms of fragmentation:
- External Fragmentation: Imagine a 128\text{K} physical memory.
- Initially,
Job 1(32\text{K}) andJob 2(24\text{K}) are loaded contiguously at the top, leaving a single 72\text{K} contiguous free region. Job 3(64\text{K}) starts and fits into the 72\text{K} region, leaving 8\text{K} free at the bottom.- Now
Job 2(24\text{K}) finishes. Total free memory is 24\text{K} + 8\text{K} = 32\text{K}. - Next,
Job 4(32\text{K}) arrives. Even though 32\text{K} of physical memory is free, we cannot run Job 4 because there is not enough contiguous space (the largest hole is 24\text{K}). To run Job 4, the OS must pause everything and compact memory by copyingJob 3up to close the 24\text{K} gap—an expensive operation.
- Initially,
- Internal Fragmentation: Suppose
Job 1(32\text{K}) uses a low address (say, code/static data near the bottom) and a high address (say, a stack at the top), but doesn’t use all addresses in between. Because a segment must be allocated contiguously from the low address to the high address, all of that unused space insideJob 1’s 32\text{K} segment is locked up and wasted.
To eliminate external fragmentation and compaction entirely, we must stop requiring a program’s memory to be contiguous in physical DRAM.
✎ Check Your Understanding
Suppose a Base Register holds
0x4000and a Bound Register holds0x1000. What happens when a program executes a load at logical address0x1000? What about0x0500? Answer: At logical address0x1000, the check \text{Logical Address} \ge \text{Bound Register} (0x1000 \ge 0x1000) is true, so it triggers a Bounds Violation. At0x0500, the check passes and the physical address is0x4000 + 0x0500 = 0x4500.Explain the difference between external fragmentation and internal fragmentation in a segmented memory system. Answer: External fragmentation occurs when total free memory is sufficient for a new job, but it is split into non-contiguous holes too small to fit the segment. Internal fragmentation occurs inside an already-allocated segment when the job leaves unused addresses between its low and high used addresses.
Section 3: Paged Memory Systems and Linear Page Tables
To solve external fragmentation and eliminate memory compaction, we replace variable-sized contiguous segments with Paged Memory Systems. Instead of mapping an entire program contiguously, we divide both the virtual address space and physical memory into fixed-size chunks called pages.
A program-generated (virtual or logical) address is split into two bitfields: \text{Virtual Address} = [\ \text{Page Number}\ |\ \text{Offset}\ ]
- The Offset specifies the byte location within a single page and passes straight through translation unchanged.
- The Page Number indexes a hardware-walked or OS-managed Page Table, which stores the starting physical address of each fixed-size virtual page.
Consider Job 1 with four virtual address space pages (0, 1, 2, 3). Its Page Table maps virtual page 0 to physical frame 1 (actually the second slot in the diagram, labeled 0), virtual page 1 to physical frame 0 (top slot), virtual page 2 to a non-contiguous frame further down, and virtual page 3 to a frame in between. Paging makes it possible to store a large contiguous virtual memory space using non-contiguous physical memory pages.
Private Address Spaces and Allocation Benefits
When multiple jobs share the machine (Job 1, Job 2, Job 3), each job sees its own private virtual address space (0, 1, 2, 3) and has its own Page Table. Every job can use virtual address 0, while their respective Page Tables map those virtual pages to completely disjoint physical memory pages alongside the Operating System Pages, isolating users from one another.
Paging dramatically simplifies OS memory allocation:
- Fixed-size pages can be kept on a simple OS free list and allocated as needed to any process—no searching for best-fit holes or compacting memory.
- A process’s memory usage can easily grow and shrink dynamically simply by adding or removing pages from its page table.
- Paging does still suffer from internal fragmentation when a process does not use all bytes on an allocated page (for example, a program using only 100 bytes of a 4\text{ KB} page). However, for common page sizes (4\text{--}8\text{ KB}), internal fragmentation is a much smaller issue than external fragmentation or compaction—though it is one major reason system designers oppose moving to much larger page sizes.
Where Do Page Tables Live, and How Big Are They?
Because each process can have thousands or millions of pages, Page Tables live in main memory (for instance, Page Table for Job 1 and Page Table for Job 2 occupy physical memory frames alongside the data pages they point to). Modern operating systems even place page tables in the kernel’s virtual memory so that unused page tables can be swapped to secondary storage (disk).
Even with paging, many computational problems are too large to fit into primary memory all at once, forcing data to be copied to and from secondary storage (drum or disk). Early machines used two approaches before demand-paged virtual memory:
- Manual overlays, where the programmer explicitly copied code and data in and out of primary memory—tedious and error-prone (what happens if you jump to non-resident code?).
- Software interpretive coding (Brooker, 1960), where a dynamic interpreter detected variables swapped out to drum and brought them back in—simple for the programmer, but inefficient. (Note that explicitly managed local stores are not just ancient history: processors like the IBM Cell used in the PlayStation-3 and many modern “deep learning” accelerators use explicitly managed scratchpad memories with a similar structure.)
To understand the fundamental problem with simple linear (flat) page tables, compare the representation of data with and without a page table:
- Without a page table: Data memory is stored uncompressed—every address in the program’s space must be physically present in main memory.
- With a page table: Data memory is stored compressed—virtual pages that are not needed do not need to be present in DRAM. However, a simple linear page table itself is stored uncompressed—meaning the entire page table must be present in contiguous virtual/physical lookup space, with one Page Table Entry (PTE) for every possible virtual page!
Let’s quantify the cost of an uncompressed linear page table with two concrete calculations:
- 32-bit virtual addresses, 4\text{ KB} pages, and 4\text{-byte} PTEs:
- A 4\text{ KB} page is 2^{12}\text{ bytes}, so the page offset requires 12 bits.
- That leaves 32 - 12 = 20 bits for the Virtual Page Number, giving 2^{20} possible virtual pages—and therefore 2^{20} PTEs.
- Multiplying by 4\text{ bytes} per PTE: \text{Page Table Size} = 2^{20} \times 4\text{ bytes} = 4\text{ MB per user}
- In addition, 4\text{ GB} of swap space would be needed to back up a full 32-bit virtual address space. Having a 4\text{ MB} linear table per process is already heavy if hundreds of processes run simultaneously, most of which only use a few kilobytes or megabytes of memory.
- 64-bit virtual address space:
- Suppose we try to shrink the page table by using huge 1\text{ MB} (2^{20}\text{ byte}) pages and 8\text{-byte} PTEs.
- The offset uses 20 bits, leaving 64 - 20 = 44 bits for the page number—which requires 2^{44} PTEs per process!
- At 8 bytes per PTE, that is 2^{44} \times 8 = 2^{47}\text{ bytes} = 128\text{ TiB} (roughly 141\text{ TB}; even under smaller variants cited in slides as 35\text{ TB}, i.e., 2^{45}\text{ bytes}), per process! Obviously, no computer has tens or hundreds of terabytes of DRAM just to store one process’s empty page table.
You might ask: Why not just make pages even larger? Making pages larger causes two severe problems: internal fragmentation skyrockets (most of each multi-megabyte page goes unused), and the page fault penalty increases dramatically (reading a giant page from disk takes much longer). We need a way to compress the page table itself so we only allocate page table entries for regions of the virtual address space that a program actually uses.
✎ Check Your Understanding
Suppose a machine has 32-bit virtual addresses, 8\text{ KB} (2^{13}-byte) pages, and 4-byte PTEs. How large is a linear page table for one process? Answer: The offset is 13 bits, leaving 32 - 13 = 19 bits for the page number, so there are 2^{19} PTEs. At 4 bytes each, the linear page table is 2^{19} \times 4 = 2^{21}\text{ bytes} = 2\text{ MB}.
Why can’t we solve the 64-bit linear page table size explosion simply by increasing the page size to many megabytes? Answer: Larger pages cause severe internal fragmentation (wasting unused bytes inside allocated pages) and a larger page fault penalty (taking much longer to read a huge page from disk).
Section 4: Hierarchical Page Tables and Translation-Lookaside Buffers (TLBs)
Because processes use their virtual address spaces sparsely (for example, code at the bottom, heap growing upward, stack at the top, and vast empty space in between), we can compress the page table using the exact same trick we used to compress data memory: recursion. If the data is too big for physical memory, we build a Page Table (PT) for it. If PT is too big for physical memory, we build a Page Table for PT (PT2), and if PT2 is too big, we build a Page Table for PT2 (PT3), and so on. This gives us a Hierarchical (Multi-Level) Page Table.
Two-Level Walk: RISC-V Sv32
In the RISC-V Sv32 virtual memory scheme, a 32-bit virtual address from the CPU is split into three fields:
- Bits
31..22(10-bit L1 index,p1): indexes the Level 1 Page Table. - Bits
21..12(10-bit L2 index,p2): indexes a Level 2 Page Table. - Bits
11..0(12-bit offset,offset): byte offset within the 4\text{ KB} data page.
A special processor register (satp in RISC-V) holds the physical address of the Root of the Current Page Table (the single 4\text{ KB} Level 1 Page Table, containing 2^{10} = 1024 4-byte PTEs). Let’s trace how the translation works:
p1indexes the Level 1 Page Table. Notice what can sit in an L1 entry:- If the program does not use any addresses in that 4\text{ MB} region, the L1 entry is marked nonexistent—no Level 2 Page Table needs to be allocated in memory at all!
- If the region is valid, the L1 entry points to a Level 2 Page Table (which may be in primary memory, or swapped out to secondary memory).
p2indexes the selected Level 2 Page Table. Again, entries can be nonexistent, in secondary memory, or in primary memory.- A valid L2 entry points to the physical Data Page, and
offsetselects the desired byte inside that page.
When two users (User 1 and User 2) both access the exact same virtual address VA1, User 1’s satp points to Level 1 PT User 1, which leads through its L2 table to User1/VA1 in physical memory, while User 2’s satp points to Level 1 PT User 2, leading through Level 2 PT User 2 to User2/VA1.
Three-Level Walk: SPARC v8
For systems needing finer subdivision or context tables, the same principle extends to three levels, as in SPARC v8:
- A Context Table Register points to a Context Table, indexed by a Context Register (identifying the active process) to obtain the
root ptrto that process’s L1 Table. - The 32-bit Virtual Address is split into
Index 1(bits31..24, 8 bits),Index 2(bits23..18, 6 bits),Index 3(bits17..12, 6 bits), andOffset(bits11..0, 12 bits). Index 1selects a Page Table Pointer (PTP) in theL1 Table;Index 2selects aPTPin theL2 Table;Index 3selects the Page Table Entry (PTE) in theL3 Table.- The
PTEsupplies the Physical Page Number (PPN), which is concatenated with the untranslatedOffsetto form the 32-bit Physical Address.
The Performance Crisis and the TLB
Every single instruction fetch and every single data load or store needs both address translation (converting Virtual Page No. (VPN) to Physical Page No. (PPN)) and a protection check (verifying Supervisor/User Mode and Read/Write permissions against the page’s attributes, raising an exception if violated).
Look at what hierarchical page tables just did to memory latency: in a two-level page table, each memory reference now becomes three memory accesses (read L1 PTE, read L2 PTE, read actual data)—and in a 4-level 64-bit page table, five memory accesses! Address translation is far too slow if we walk the page table on every access. A good virtual memory design must be fast (~one cycle) and space-efficient.
The solution is to exploit locality of reference and cache recent translations in a Translation-Lookaside Buffer (TLB):
- On a TLB hit, translation and protection checks complete in a single cycle.
- On a TLB miss, the machine performs a page-table walk to refill the TLB.
Each TLB entry contains status and protection bits—V (Valid), R (Read permission), W (Write permission), D (Dirty bit, indicating the page has been written)—along with the virtual tag (matching the incoming VPN) and the translated PPN. When a virtual address arrives, its VPN is compared against the TLB tags; on a hit, the PPN is concatenated with the unchanged offset to produce the physical address.
TLB Designs and TLB Reach
How are TLBs organized in hardware?
- Small L1 TLBs typically have 32–128 entries and are usually fully associative. Why fully associative? Unlike cache lines (64 bytes), each TLB entry maps an entire 4\text{ KB} page, so there is much less spatial locality across consecutive pages, making it far more likely that two active entries would conflict in a direct-mapped or low-associativity structure.
- Larger TLBs (256–512 entries) are sometimes 4–8 way set-associative, and larger systems often use multi-level (L1 and L2) TLBs.
- Replacement policy is typically Random or FIFO.
- Example (AMD Zen 5): chasing one cache line per 4 KB page, latency stays at the 4-cycle L1 hit time up to 96 pages and jumps to about 11 cycles at 128 pages, so the L1 DTLB holds between 96 and 127 translations and an L2 TLB hit costs roughly 7 extra cycles.
An important metric for TLB performance is TLB Reach, defined as the size of the largest virtual address space that can be simultaneously mapped by the TLB without missing: \text{TLB Reach} = (\text{Number of TLB Entries}) \times (\text{Page Size}) Let’s work through a concrete example:
- Suppose a processor has 64 TLB entries, 4\text{ KB} pages, and maps one page per entry.
- \text{TLB Reach} = 64\text{ entries} \times 4\text{ KB} = 256\text{ KB (if contiguous)} Notice the limitation: even if your L2 cache is several megabytes, a 256\text{ KB} TLB reach means a program touching a larger working set will suffer frequent TLB misses.
Handling a TLB Miss: Hardware vs. Software
When a TLB miss occurs, who performs the page-table walk? There are two strategies (and note that a given ISA can actually use either strategy):
- Software-Managed TLB (e.g., MIPS, Alpha): A TLB miss triggers an exception, and the operating system walks the page tables and reloads the TLB using a privileged “untranslated” addressing mode (so the walk itself doesn’t cause recursive TLB misses). However, software TLB misses can be very expensive on out-of-order superscalar processors because taking an exception requires flushing the pipeline to jump to the trap handler.
- Hardware-Managed TLB (e.g., SPARC v8, x86, PowerPC, RISC-V): A hardware Memory Management Unit (MMU)—a Hardware Page Table Walker—reads the Page-Table Base Register (
satp), walks the page tables in untranslated physical memory, and reloads the TLB without flushing the pipeline. Only if a missing data or page-table page is encountered during the walk does the MMU give up and signal a Page Fault exception for the original instruction.
Integrating this into our 5-stage pipeline gives the Page-Based Virtual-Memory Machine:
- An Inst. TLB sits in front of the
Inst. Cachein Fetch, and a Data TLB sits in front of theData Cachein the Memory stage. - Both TLBs check for Page Faults and Protection violations, and both connect on a
Miss?to the Hardware Page Table Walker, which reads thePage-Table Base Registerand accesses memory (or theData Cachevia physical addresses) to refill the TLB.
✎ Check Your Understanding
In a 32-bit two-level page table (10-bit L1 index, 10-bit L2 index, 12-bit offset), why does a small program using only 8\text{ KB} of code and 8\text{ KB} of stack not need 4\text{ MB} of memory for its page table? Answer: Because the L1 page table has entries marked nonexistent for all the unused 4\text{ MB} regions in between; Level 2 page tables only need to be allocated for the active regions actually used by the code and stack.
Calculate the TLB Reach for a processor with a 128-entry TLB and 8\text{ KB} pages. Answer: \text{TLB Reach} = 128 \times 8\text{ KB} = 1024\text{ KB} = 1\text{ MB} (if contiguous).