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:

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:

  1. 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., OS at the bottom, Program 2 in the middle, Program 1 higher up).
  2. 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.
  3. 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

  1. 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.

  2. 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:

  1. 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.
  2. 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:

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:

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?

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:

  1. External Fragmentation: Imagine a 128\text{K} physical memory.
    • Initially, Job 1 (32\text{K}) and Job 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 copying Job 3 up to close the 24\text{K} gap—an expensive operation.
  2. 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 inside Job 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

  1. Suppose a Base Register holds 0x4000 and a Bound Register holds 0x1000. What happens when a program executes a load at logical address 0x1000? What about 0x0500? Answer: At logical address 0x1000, the check \text{Logical Address} \ge \text{Bound Register} (0x1000 \ge 0x1000) is true, so it triggers a Bounds Violation. At 0x0500, the check passes and the physical address is 0x4000 + 0x0500 = 0x4500.

  2. 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}\ ]

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:

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:

To understand the fundamental problem with simple linear (flat) page tables, compare the representation of data with and without a page table:

Let’s quantify the cost of an uncompressed linear page table with two concrete calculations:

  1. 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.
  2. 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

  1. 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}.

  2. 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:

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:

  1. p1 indexes 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).
  2. p2 indexes the selected Level 2 Page Table. Again, entries can be nonexistent, in secondary memory, or in primary memory.
  3. A valid L2 entry points to the physical Data Page, and offset selects 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:

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):

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?

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:

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):

Integrating this into our 5-stage pipeline gives the Page-Based Virtual-Memory Machine:

✎ Check Your Understanding

  1. 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.

  2. 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).