Address translation
On this page
How a virtual address becomes a physical one: base and bounds, segmentation, paging, multi-level tables and the TLB – plus the process memory layout and the tricks translation makes possible, such as copy-on-write.
Virtual memory
In general, virtual memory is the address space a process can “see”. Its size depends on the instruction set: a 64-bit CPU has 64-bit addresses (in practice 48 or 40 bits are used), a 32-bit CPU has 32-bit addresses.
Every load and store uses a virtual address, which the memory management unit (MMU) converts to a physical address. The kernel sets up the translation tables for each process; the hardware does the translation.
Goals of address translation
- Memory protection
- Memory sharing: shared libraries, interprocess communication
- Sparse addresses: many regions of dynamic allocation (heaps and stacks)
- Efficiency: memory placement, runtime lookup, compact tables
- Portability
Process memory layout
From low to high addresses, a process's user address space typically holds:
- Text (code)
- The executable instructions. Usually read-only and shareable, so one copy can serve every process running the same program.
- Data
- Initialised global and static variables, e.g.
int debug = 1;outside any function. - BSS
- Uninitialised (or zero-initialised) globals and statics, e.g.
static int i;. The kernel zeroes it before the program starts. - Heap
- Dynamic memory from
malloc/free. Starts after BSS and grows upwards. - Stack
- A LIFO structure of stack frames: local variables, function arguments and the return address. On x86 it starts at high addresses and grows downwards.
On 32-bit Linux, the top 1 GB of the 4 GB virtual address space is reserved for the kernel. When you run the same program twice, the OS gives each copy its own data, heap and stack, but reuses the code.
void func() { int a; } // a is on the stack
void *mem = malloc(256); // 256 bytes on the heap
The exercise program proc_maps.c prints the address of main, a global and a local, then sleeps so you can read /proc/<pid>/maps. The addresses change between runs because of address space layout randomisation (ASLR), which randomises the location of the heap, stack and text segment.
Base and bounds
Each process gets one contiguous region of physical memory. With virtually addressed base and bounds, the hardware checks each virtual address against the bound and adds the base. On a context switch, only the base and bound registers are saved and restored.
| Pros | Cons |
|---|---|
| Simple; fast (2 registers, an adder, a comparator); safe; the process can be relocated in physical memory without changing it. | Can't keep a program from overwriting its own code; can't share code or data with other processes; can't grow the stack or heap as needed. |
Base and bounds does provide isolation between processes; that is not one of its limitations.
Segmentation
A segment is a contiguous region of virtual memory. Each process has a segment table in hardware – effectively an array of base and bound pairs, one per segment. Each entry has a start, a length and access permissions, and a segment can be anywhere in physical memory. Processes can share segments (same start and length, same or different permissions). The code segment should not be writable.
On a context switch, the segment table is saved and restored.
| Pros | Cons |
|---|---|
| Can share code and data segments; can protect the code segment; can grow stack and heap transparently; can detect when copy-on-write is needed. | Complex memory management: finding a free chunk of the right size, sometimes rearranging memory. External fragmentation: wasted space between chunks. |
Copy-on-write
UNIX fork makes a complete copy of a process. With segments (or pages) it can be done more efficiently:
- Copy the segment (or page) table into the child.
- Mark the parent's and child's segments read-only.
- Start the child; return to the parent.
- If either writes to a segment (for example the stack or heap), trap into the kernel, make a copy of that segment, and resume.
This avoids copying memory that is never written, which matters most for fork followed by exec, where the copy would be thrown away immediately. The cow.c exercise allocates 64 MB, forks, and reads /proc/self/smaps: right after the fork the memory is shared; once the child writes, it becomes private.
Zero-on-reference
How much physical memory does a stack or heap need? Only what is currently in use. When the program uses memory beyond the end of the stack, it faults into the kernel, which allocates some memory, zeroes it (to avoid leaking another process's data), updates the table and resumes the process.
Paging
Memory is managed in fixed-size units: pages in virtual memory and frames in physical memory. Finding a free frame is easy: a bitmap with one bit per frame. Each process has its own page table in physical memory (RAM), and hardware registers point to its start and length.
A virtual address is split into a virtual page number (VPN) and an offset. The page table maps the VPN to a page frame number (PFN). The offset passes through unchanged.
virtual address: | VPN | offset |
|
page table
v
physical address: | PFN | offset |
On a context switch, only the pointer to the page table and its size are saved; the table itself stays in memory. Very small pages mean huge page tables. Very large pages mean internal fragmentation: unused space inside a fixed-size chunk.
Sharing works by pointing entries in two page tables at the same frames. A core map of physical frames tracks which processes point to which frame (a reference count).
Translating by hand
Work out how many bits the offset needs (page size 2n → n bits), take the remaining high bits as the VPN, look it up, and glue the PFN to the unchanged offset. If the entry is not present, it is a page fault.
Example (exercises): 16-bit addresses, 4 KB pages, so a 12-bit (three hex digits) offset and a 4-bit VPN. The table maps VPN 3 → frame 5. Then 0x3500 → VPN 3, offset 0x500 → 0x5500.
Example (2025 exam): 256-byte pages and 20-bit addresses. Offset = 8 bits, VPN = 12 bits, so the table has 212 = 4096 entries and the address space is 220 = 1 MB. 0000 0000 1110 | 1010 1011 has VPN 14; if VPN 14 maps to PFN 0001 0110 0000, the physical address is 0001 0110 0000 1010 1011.
Example with decimal addresses (Norwegian exercises): with 4096-byte pages, virtual address 8300 is in page 2 (8300 div 4096) at offset 8300 − 2 × 4096 = 108. If page 2 is in frame 6, the physical address is 6 × 4096 + 108 = 24684.
Counting bits: 256 KB of virtual memory needs 18-bit addresses (218 = 256K); 64 KB of physical memory needs 16. The number of page table entries is the virtual memory size divided by the page size: 4 MB of virtual memory with 4 KB pages gives 1024 entries.
Fill on demand
Can a program start before its code is in memory? Yes: mark all page table entries invalid; when a page is referenced for the first time, the kernel traps, brings the page in from disk and resumes. The remaining pages can be loaded in the background. This leads to demand paging.
Multi-level translation
Address spaces are sparse: per-processor heaps, per-thread stacks, memory-mapped files, dynamically linked libraries. A flat page table for a large address space would be huge: 32-bit addresses with 4 KB pages need about 1 million entries (the slides say 500K), and 64-bit addresses about 4 quadrillion.
The solution is a tree of tables: paged segmentation, multi-level page tables, or multi-level paged segmentation. The lowest level is always a fixed-size page, which gives:
- efficient memory allocation (compared to segments),
- efficiency for sparse addresses (compared to flat paging),
- efficient disk transfers (fixed-size units),
- easier TLBs and efficient reverse lookup (physical → virtual),
- variable granularity for protection and sharing.
Paged segmentation: each segment table entry points to a page table and holds its length and permissions; each page table entry holds a frame and permissions.
Two-level example (2024 exam)
Format: 8-bit top-level index | 8-bit second-level index | 16-bit offset. To translate 0x0003AB05: top index 0x00 → page table A; second index 0x03 → frame 0xCACA; offset 0xAB05 → physical address 0xCACAAB05. If any level is invalid, the address is invalid.
How many entries per table? 16 KB pages and a 36-bit address space give a 14-bit offset, leaving 22 bits for the page number. Split over two levels, each table has 211 = 2048 entries.
Inverted page table
Many operating systems keep their own portable translation structures. One approach is an inverted page table: a hash from virtual page to physical page, using space proportional to the number of physical pages rather than virtual ones.
The TLB
Walking a multi-level page table on every memory access would be far too slow. The translation lookaside buffer (TLB) is a cache of recent virtual page → physical page translations. On a hit the translation is used directly; on a miss the hardware walks the page table.
Cost of translation = cost of TLB lookup + P(TLB miss) × cost of page table lookup
The hardware design principle: the bigger the memory, the slower the memory. So the TLB is small and fast. (The i7 example has an 8 MB shared L3 cache and a per-core L2.)
On a context switch, the TLB entries of the old process would be wrong. Either discard the TLB, or use a tagged TLB: each entry carries a process ID and only hits if it matches the current process.
What translation is used for
- Process isolation: keep processes out of each other's memory and the kernel's.
- Efficient IPC: shared regions of memory between processes.
- Shared code segments: e.g. common libraries.
- Program initialisation: start running before the program is in memory.
- Dynamic memory allocation: allocate and initialise stack and heap pages on demand.
- Program debugging: data breakpoints when an address is accessed.
- Zero-copy I/O: directly between the device and user memory.
- Memory-mapped files: access file data with load and store instructions.
- Demand-paged virtual memory: the illusion of near-infinite memory, backed by disk.
On earlier exams
- 2023, question 10 – Stack and heap
- 2024, question 3 – The heap
- 2024, question 4 – The stack
- 2024, question 5 – Running the same program twice
- 2024, question 6 – Base and bound
- 2024, question 7 – ASLR
- 2024, question 18 – Segmentation
- 2024, question 19 – Write permission
- 2024, question 24 – Two-level address translation
- 2025, question 6 – TLB
- 2025, question 24 – Paging
- 2025, question 27 – Zero-on-reference and copy-on-write
Practise this topic in the quiz (17 questions)