Skip to content
BIASED

Virtual memory

A private address space per process: VPN/PPN and offset, Sv32/Sv39, PTE bits, the TLB, page faults and demand paging, huge pages.

Why virtual memory

Caches used physical addresses. RARS maps every program’s data at 0x10010000 — if those were physical, two programs could not coexist, any pointer could smash another process or the OS, size was capped by DRAM, and free memory fragmented into holes too small for a contiguous request. Virtual memory puts a translation between the addresses a program uses (VA) and those memory sees (PA).

Notation. Pages of 2p2^p bytes (p=12p=12 → 4 KiB). VA = VPN + offset; PA = PPN + same offset. A PTE holds a PPN and permission bits (V,R,W,X,U,A,DV,R,W,X,U,A,D).

Key idea. Divide both spaces into equal pages; keep per-process a table mapping each VPN to a frame or marking it absent. Hardware translates every access through the current table. Three wins: isolation (unmapped frames do not exist for the process; R/W/X/UR/W/X/U gate the rest), a large private layout from 0 (physical frames need not be contiguous; unused pages can live on disk), and sharing (same frame in several tables — libraries, COW after fork, mmap). Atlas (Kilburn et al., 1962) already presented DRAM and drum as one paged store.

Multi-level tables: Sv32

A flat table for 20-bit VPNs is 4 MiB per process (220×42^{20}\times 4 B); a flat table for 39-bit Sv39 would be 1 GiB. Multi-level tables store only used pieces.

Sv32: VPN[1] (10) | VPN[0] (10) | offset (12). Each table is 1024 × 4 B = one page. satp holds the root PPN (and an ASID). Walk: if R=W=X=0R=W=X=0 and V=1V=1, follow the pointer; else a leaf — check rights, form PA = PPN·4096+offset. Root entries can themselves be leaves mapping 4 MiB megapages.

Worked. Load 0x10010008 with satp.PPN=0x80000: VPN[1]=64, VPN[0]=16, offset=8. Root PTE at 0x80000100 is a pointer to leaf table 0x80001; leaf at 0x80001040 yields PPN 0x8F0E2 with V,R,W,U,A,DV,R,W,U,A,D → PA 0x8F0E2008. A small RARS program needs the root plus one leaf for data and one for stack — 12 KiB of tables instead of 4 MiB.

Sv39: three 9-bit indices, 512 × 8 B PTEs, 512 GiB VA; leaves higher up are 2 MiB / 1 GiB pages.

TLB and page faults

A walk every access would kill performance (two extra reads before the data in Sv32; three in Sv39). The TLB caches VPN→PPN (+ rights); hits finish in-cycle. Misses walk and refill. ASIDs avoid flushing on every context switch; sfence.vma drops stale entries after the OS changes a table.

Page fault: exception → OS. Absent-but-valid (demand paging): allocate/read frame, set VV, retry the instruction — mepc/sepc must still name it. Bad access (no mapping, wrong rights) → SIGSEGV. Disk faults cost ~ms; thrashing when the working set spills RAM and the machine swaps continuously. The accessed (AA) and dirty (DD) bits help the OS pick victims and know which frames must be written back.

TLB reach and huge pages

TLB reach = entries × page size: 64 × 4 KiB = 256 KiB; 2 MiB pages → 128 MiB; 1 GiB → 64 GiB. Huge pages cut misses and shorten walks; they need contiguous aligned frames and can waste space on sparse mappings. Linux transparent huge pages and explicit mmap/hugetlbfs both exist; databases and scientific codes often pin 2 MiB pages for big heaps.

L1 is often VIPT: index bits sit in the page offset (≥12\ge 12 for 4 KiB pages), so set select overlaps the TLB lookup. Synonym problems appear when the same physical line is mapped at VAs that disagree on those index bits — OS and hardware cooperate to avoid or flush them.

The offset never changes under translation: only the page number does. That invariant is why file-backed mmap can share the page cache’s frames directly with user mappings, and why COW after fork only allocates a new frame on the first write that finds DD clear and a shared mapping. Kernel addresses on many systems live in the high half of VA with the U bit clear, so user loads of those pointers fault instead of silently reading kernel memory.

Measuring translation cost: a microbenchmark that chases a pointer through more pages than the TLB holds will show a CPI cliff once walks dominate; huge pages move that cliff. When the OS reclaims a frame it must also invalidate remote TLBs (IPI / shootdown) — a scalability tax on machines with many cores that map the same file. Soft page faults (page already in the cache but not in this process’s table) are far cheaper than disk faults; watching /proc fault counters separates the two.

Permission bits also encode execute-only and read-only code pages used by W^X policies: a JIT that writes instructions must flip W off and X on (and fence the I-cache) before running them. Without that discipline, a buffer overflow that writes data can also inject code. RISC-V’s sfence.vma and fence.i are the architectural hooks; the OS topic on processes will show mprotect as the syscall wrapper.

Next: The datapath and pipelining — open the processor that issues those loads.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.