8. Virtual Memory and Virtual Machines
Description
Virtual memory gives each process the illusion of its own large, contiguous address space, backed by physical RAM and disk. Benefits: isolation and protection between processes, simpler programming, efficient sharing, and the ability to run programs larger than RAM.
Paging: memory is divided into fixed-size pages (commonly 4 KB) mapped to physical frames. Only the pages in use need to be resident (demand paging). A page fault occurs when a needed page is not in memory; the OS loads it from storage.
Page table: per-process structure mapping virtual page number (VPN) to physical frame number, plus bits for valid, read/write/execute, user/supervisor, dirty, accessed. Large address spaces use multi-level page tables so unused regions need no entries.
TLB (Translation Lookaside Buffer): a small cache of recent translations. Without it every memory access would need extra memory accesses for the page walk.
Segmentation: divides memory into variable-sized logical segments (code, data, stack). It maps well to program structure but suffers from external fragmentation; modern systems rely mainly on paging (early x86 used segmentation heavily).
Protection: hardware checks permissions on every access; violations raise exceptions (e.g. a segmentation fault). User/kernel modes protect the OS.
Virtual machines (VMs): a hypervisor (virtual machine monitor) lets several operating systems share one machine securely.
| Type 1 (bare-metal) | Type 2 (hosted) | |
|---|---|---|
| Runs on | Directly on hardware | On top of a host OS |
| Examples | Xen, VMware ESXi (KVM is a kernel-integrated hypervisor often grouped here) | VirtualBox, VMware Workstation |
| Typical use | Data centers, cloud | Desktops, testing |
Nested (two-dimensional) page tables (Intel EPT, AMD NPT): hardware translates guest-virtual → guest-physical → host-physical, so the hypervisor does not have to trap on every guest page-table update.
Key Formulas
Virtual address = | Virtual page number (VPN) | Page offset |
offset bits = log2(page size); VPN bits = address bits − offset bits
Single-level page table size = 2^(VPN bits) × PTE size
EAT (with TLB, 1 memory access per walk level n):
EAT = h · (t_TLB + t_mem) + (1 − h) · (t_TLB + n · t_mem + t_mem)
(n = page-walk levels; single-level: n = 1)
Demand paging: EAT = (1 − p) · t_mem + p · t_fault (p = page-fault rate)
Nested paging worst case (g guest levels, h host levels): (g + 1)(h + 1) − 1 memory references
Steps
Address translation: (1) split VA into VPN and offset, (2) look up VPN in the TLB, (3) on a miss walk the page table, (4) if the valid bit is 0 → page fault, OS loads the page and updates the table, (5) check permissions, (6) physical address = frame number concatenated with offset.
Page fault handling: (1) trap to OS, (2) find a free frame or evict a victim (write back if dirty), (3) read the page from disk, (4) update the page table and TLB, (5) restart the instruction.
Worked Example
A. Translation. 4 KB pages (offset 12 bits). VA = 0x12345 → VPN = 0x12, offset = 0x345. If the page table maps VPN 0x12 to frame 0x80, PA = 0x80345.
B. Page table size. 32-bit VA, 4 KB pages: VPN = 20 bits → 2²⁰ entries × 4 B = 4 MB per process, which is why multi-level tables exist. Two-level split: 10 + 10 + 12. x86-64 uses four levels: 9 + 9 + 9 + 9 + 12 = 48-bit virtual addresses.
C. EAT with TLB. t_TLB = 1 ns, t_mem = 100 ns, single-level table, hit ratio 98%. Hit: 1 + 100 = 101 ns. Miss: 1 + 100 + 100 = 201 ns. EAT = 0.98 × 101 + 0.02 × 201 = 98.98 + 4.02 = 103 ns (only 3% overhead because of the TLB).
D. Page faults are expensive. t_mem = 100 ns, fault service 8 ms = 8,000,000 ns. EAT = (1 − p) × 100 + p × 8,000,000. To keep slowdown under 10% (EAT ≤ 110 ns): 100 + 7,999,900 p ≤ 110 → p ≤ about 1.25 × 10⁻⁶ (fewer than 1 fault per 800,000 accesses).
E. Nested paging. Guest and host both 4-level: worst case (4 + 1)(4 + 1) − 1 = 24 memory references for one translation. Hence large TLBs and walk caches matter even more for virtualized workloads.
F. Protection. A process writes to a read-only page (e.g. code): the MMU raises a protection fault, the OS delivers a segmentation fault to the process.
Python / Assembly
PAGE_BITS = 12
page_table = {0x12: 0x80, 0x13: 0x81} # VPN -> frame
tlb = {} # VPN -> frame
def translate(va):
vpn, off = va >> PAGE_BITS, va & ((1 << PAGE_BITS) - 1)
if vpn in tlb:
frame = tlb[vpn]
elif vpn in page_table:
frame = page_table[vpn]; tlb[vpn] = frame # TLB fill
else:
raise RuntimeError("page fault at VPN 0x%x" % vpn)
return (frame << PAGE_BITS) | off
print(hex(translate(0x12345))) # 0x80345
def eat_tlb(h, t_tlb, t_mem, levels=1):
return h*(t_tlb + t_mem) + (1-h)*(t_tlb + levels*t_mem + t_mem)
print(eat_tlb(0.98, 1, 100)) # 103.0
Enabling paging in RISC-V supervisor mode (Sv39) and flushing stale translations:
la t0, root_page_table # physical address of top-level table
srli t0, t0, 12 # PPN field
li t1, 8 << 60 # MODE = 8 (Sv39) in satp[63:60]
or t0, t0, t1
csrw satp, t0 # turn translation on
sfence.vma # flush TLB / ordering fence for address translation
Notes
- Page size trade-off: larger pages shrink page tables and raise TLB reach but waste memory (internal fragmentation) and cost more per fault.
- TLB misses are far cheaper than page faults; the TLB miss rate must be tiny to keep EAT near memory speed.
- Virtual memory is the foundation of multitasking OSes and process isolation; VMs extend isolation to whole operating systems and underpin the cloud.
- Overhead: VMs run slightly slower than native hardware; hardware assists (nested paging, virtualization extensions) shrink the gap.
- Quick facts: virtual memory extends physical memory using disk; the page table maps virtual to physical addresses; a page fault means the page must be fetched from storage; demand paging loads pages only when needed.
References: [HP6] Ch. 2 (2.4 virtual memory and virtual machines), Appendix B (B.4); [PH-RV] Ch. 5 (5.7); [RV-Spec] Vol. II (supervisor-level memory management);
All rights remain with the original author (Algo-holics). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me