---
title: Virtual memory
heading: Pages, page tables and the TLB
eyebrow: C8 · Lesson 47 · Virtual memory
xp: 40
---

::: hook
On a laptop, every program believes it has the whole address space to itself, starting at the same addresses, and none of them can read another's memory. That illusion is **virtual memory**: each address a program uses is translated, page by page, to a physical address in RAM, using a table the operating system keeps for each process. It gives isolation and protection, lets programs use more memory than is installed, and lets the OS share and move memory freely. Translation sits on every single load, store and instruction fetch, so it has to be fast, and that is the job of a small, specialized cache called the TLB.
:::

::: play
- Every memory access needs a translation. A TLB hit gives it in a cycle; a miss means walking the page table, one memory access per level, before the real access can start.
- The curve is the average time per access against the TLB hit rate. Below about 99% the walks add up quickly.
- Change the number of page-table levels: 64-bit processors use 4 or 5 levels, so their misses cost even more.
:::
:::

```sim plot
view: graph
x: h = 0.9..1
y: t = 0..200 cycles
slider: n = 1..5 step 1 = 2
slider: M = 50..200 step 10 = 100
plot: h*(1 + M) + (1 - h)*(1 + n*M + M) | label: average access time
readout: at a 99% hit rate, cycles = 0.99*(1 + M) + 0.01*(1 + n*M + M) | digits: 4
readout: with no TLB, cycles = n*M + M | digits: 4
```

::: predict
## A 32-bit machine uses 4 KiB pages and a single-level page table with 4-byte entries. How big is each process's page table?

- [ ] 4 KiB
- [ ] 1 MiB
- [x] 4 MiB
- [ ] 4 GiB

::: reveal
4 MiB: 32-bit addresses with a 12-bit offset leave 20 bits of page number, $2^{20}$ pages, each with a 4-byte entry. For a hundred processes that's 400 MiB of tables, mostly describing memory the processes never use. Multi-level tables only build the parts in use.
:::
:::

::: nameit
## Pages and translation

Memory is divided into **pages**, typically 4 KiB. A virtual address splits into a **virtual page number** (VPN) and a **page offset**; with 4 KiB pages the offset is the low 12 bits. The **page table** maps each VPN to a **physical page number** (PPN), and

$$\text{physical address} = \text{PPN} \times \text{page size} + \text{offset}$$

so the offset passes through unchanged. Each entry also holds a **valid** bit and permissions (read, write, execute, user).

## Page faults

An access to a page that isn't valid, or that breaks its permissions, traps to the operating system: a **page fault**. The OS might load the page from disk (millions of cycles), allocate a fresh zeroed page, copy a shared page on write, or end the program for a bad access.

## Multi-level tables

RISC-V's Sv32 splits the 20-bit VPN into two 10-bit indices. The first indexes a 1024-entry root table, whose entry points to a second-level table of 1024 entries. A process using a little memory needs the root and a few second-level tables, a few pages instead of 4 MiB. The cost is a **page-table walk** of one memory access per level.

## The TLB

The **translation lookaside buffer** caches recent translations, typically 32 to a few thousand entries, often fully associative. Its **reach** is entries × page size. With hit rate h, n levels and a memory access time M, the average time per access is about

$$t = h(t_\text{TLB} + M) + (1 - h)(t_\text{TLB} + nM + M)$$

**Microcontrollers** usually skip all of this: one physical address space and, at most, a **memory protection unit** that sets permissions on a few regions.
:::

::: example
## Translate the virtual address 0x00403A2C with 4 KiB pages, where the page table maps VPN 0x00403 to PPN 0x001F2.

::: solution
1. **Split the address.** The offset is the low 12 bits, 0xA2C; the VPN is 0x00403.
2. **Look up the page.** The page table maps VPN 0x00403 to PPN 0x001F2.
3. **Form the physical address.**
   $$\begin{aligned} &\text{0x1F2} \times 4096 + \text{0xA2C} \\ &= \text{0x001F2A2C} \end{aligned}$$

**Answer:** the physical address is 0x001F2A2C.
:::
:::

::: example
## A TLB hits 99% of the time in 1 cycle; memory takes 100 cycles and the page table has 2 levels. What is the average access time, and what would it be with no TLB?

::: solution
1. **Average over hits and misses.**
   $$\begin{aligned} t &= 0.99(1 + 100) \\ &\quad + 0.01(1 + 200 + 100) \\ &= 99.99 + 3.01 \\ &= 103\ \text{cycles} \end{aligned}$$
2. **With no TLB.** Every access walks the table:
   $$2 \times 100 + 100 = 300\ \text{cycles}$$

**Answer:** 103 cycles, against 300 with no TLB.
:::
:::

::: example
## How big is a flat page table for 32-bit addresses, 4 KiB pages and 4-byte entries? How much does a two-level table (10, 10 and 12 bits) need for a process with 4 MiB of code at the bottom and one page of stack at the top?

::: solution
1. **A flat table.**
   $$2^{20} \times 4\ \text{bytes} = 4\ \text{MiB}$$
   per process, mostly describing unused space.
2. **The two-level root.** The root table is 4 KiB.
3. **Second-level tables.** Each covers 1024 pages, 4 MiB, so the code needs one and the stack one more.
4. **The total.** That's 3 tables, 12 KiB in all.
5. **The trade.** The levels cost an extra memory access per walk and save memory in proportion to how sparse the address space is.

**Answer:** 4 MiB for the flat table, and 12 KiB for the two-level one.
:::
:::

::: check
kind: parts
given: v = 4198000 | 20000000 | 305419896

## A program uses virtual address {v:hex8}, with 4 KiB pages.

::: part
answer: floor(v/4096)
base: 16
width: 5

### What is its virtual page number?

::: misses
- [floor(v/1024)] A 4 KiB page has a 12-bit offset: shift by 12.
- [any] Not yet. The address over 4096.
:::
:::

::: part
answer: v - 4096*floor(v/4096)
base: 16
width: 3

### What is its page offset?

::: misses
- [any] The low 12 bits.
:::
:::
:::

::: check
given: p = 498 | 4097 | 77; o = 2604 | 100 | 4000
answer: p*4096 + o
base: 16
width: 8

## A virtual page maps to physical page {p:hex5}, and the page offset is {o:hex3}. What physical address results (4 KiB pages)?

::: right
Right. PPN × 4096 + offset.
:::

::: misses
- [p + o] The page number goes above the 12 offset bits.
- [any] Not yet. PPN × 4096 + offset.
:::
:::

::: check
kind: parts
given: n = 32 | 36 | 40; g = 12 | 14 | 16; e = 4 | 8

## A machine has {n}-bit virtual addresses, pages of $2^{\given{g}}$ bytes and {e}-byte page-table entries.

::: part
answer: 2^(n - g)
tolerance: 0.001%

### How many pages does a single-level table have to describe?

::: misses
- [2^n] Divide by the page size.
- [any] Not yet. $2^{n - g}$.
:::
:::

::: part
answer: 2^(n - g)*e/2^20
unit: MiB
tolerance: 0.5%

### How big is that table, in MiB?

::: misses
- [any] Pages × entry size.
:::
:::
:::

::: check
kind: parts
given: h = 0.95 | 0.99 | 0.998; M = 60 | 120; n = 2 | 4

## A TLB answers in 1 cycle and hits {h} of the time. Memory takes {M} cycles and the page table has {n} levels.

::: part
answer: h*(1 + M) + (1 - h)*(1 + n*M + M)
unit: cycles
tolerance: 0.5%

### What is the average time per access?

::: misses
- [any] Not yet. Hits: TLB + memory; misses: TLB + n walks + memory.
:::
:::

::: part
answer: (n*M + M)/(h*(1 + M) + (1 - h)*(1 + n*M + M))
tolerance: 0.5%

### How many times faster is that than walking the table on every access?

::: misses
- [any] (n + 1)M over the average.
:::
:::
:::

::: check
kind: parts
given: E = 32 | 64 | 1536; P = 4 | 2048

## A TLB has {E} entries, and pages are {P} KiB.

::: part
answer: E*P
unit: KiB
tolerance: 0.5%

### What is the TLB's reach?

::: misses
- [any] Entries × page size.
:::
:::

::: part
answer: E
tolerance: 0

### How many different pages can a loop touch over and over without a TLB miss, at most?

::: misses
- [any] One entry per page.
:::
:::
:::

::: check
given: f = 1 | 10 | 100; c = 5 | 20; t = 100
answer: t + f*1e-6*c*1e6
unit: ns
tolerance: 0.5%

## An access normally takes {t} ns, but {f} in a million accesses page-fault, costing {c} ms each. What is the average access time?

::: right
Right. {t} ns + {f} × 10⁻⁶ × {c} ms = {answer} ns.
:::

::: misses
- [t] Rare faults are so expensive that they dominate.
- [any] Not yet. Normal time + fault rate × fault cost, in the same units.
:::
:::

::: check
kind: choice

## A program stores to an address on a page whose entry is valid but marked read-only. What happens?

- [ ] The store is silently ignored
- [ ] The value is written to the physical page anyway
- [x] A page fault traps to the operating system, which decides what to do
- [ ] The TLB marks the page writable

::: misses
- [1] The hardware has to tell someone: the program may have a bug, or the OS may be sharing the page.
- [2] That would defeat the protection.
- [4] Only the operating system changes permissions.
:::
:::

::: check
kind: parts
given: v = 32 | 36; p = 4 | 16

## Virtual addresses have {v} bits, pages are {p} KiB and entries 4 bytes.

::: part
answer: 2^(v - log(p*1024)/log(2))*4/1024
unit: KiB
tolerance: 0.5%

### How big is a flat page table?

::: misses
- [any] Pages × 4 bytes.
:::
:::

::: part
answer: p*1024/4
tolerance: 0

### How many entries fit in one page?

::: misses
- [any] Page size ÷ 4.
:::
:::
:::

::: check
kind: parts
given: h = 95 | 99 | 99.9; M = 60 | 100; L = 2 | 3 | 4

## A TLB hits {h}% of the time in 1 cycle; memory takes {M} cycles and the page table has {L} levels.

::: part
answer: 1 + M + (1 - h/100)*L*M
tolerance: 0.5%

### What's the average access time?

::: misses
- [any] Hits: 1 + M. Misses add a walk of L accesses.
:::
:::

::: part
answer: (L + 1)*M
tolerance: 0.5%

### And with no TLB?

::: misses
- [any] L table reads, then the data.
:::
:::
:::

::: explain
## Why doesn't the page offset need to be translated?

::: model
Pages in virtual memory and in physical memory are the same size and both start at multiples of that size, so a virtual page maps onto a physical page as a whole block. A byte's position inside its page is therefore the same in both, and only the page number changes. That's why the hardware can send the offset bits straight through, and why, with small enough caches, it can start indexing the cache with the offset while the TLB is still translating the page number.
:::
:::

::: practice

::: check
given: v = 76476 | 4202288 | 8192000 | 123456789 | 268501056 | 2147479536 | 3237998080
answer: floor(v/4096)
base: 16
width: 5

## What is the virtual page number of {v:hex8} with 4 KiB pages?

::: misses
- [any] Shift right by 12.
:::
:::

::: check
given: p = 300 | 1027; o = 64 | 3000
answer: p*4096 + o
base: 16
width: 8

## Physical page {p:hex5}, offset {o:hex3}: what physical address?

::: misses
- [any] PPN × 4096 + offset.
:::
:::

::: check
given: E = 64 | 512; P = 4 | 64
answer: E*P
unit: KiB
tolerance: 0.5%

## What is the reach of a {E}-entry TLB with {P} KiB pages?

::: misses
- [any] Entries × page size.
:::
:::
:::
