Computer Architecture Cheatsheet

Virtual Memory

Use this Computer Architecture reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Purpose of Virtual Memory

Virtual memory provides each process with its own private, contiguous address space, independent of physical DRAM layout. It enables:

  • Protection — processes cannot read/write each other's memory
  • Isolation — a crash in one process doesn't corrupt others
  • Abstraction — programs can be larger than physical RAM (demand paging)
  • Sharing — pages can be mapped read-only into multiple processes (shared libraries)
  • Simplified linking — programs can assume fixed load addresses

Address Translation

Every memory access uses a virtual address (VA). The MMU translates it to a physical address (PA):

Virtual Address          Page Table           Physical Address
[ VPN | page offset ] ──────────────► [ PPN | page offset ]
                          (MMU)
  • VPN = Virtual Page Number
  • PPN (or PFN) = Physical Page Number / Frame Number
  • Page offset = unchanged; same in VA and PA
  • Page size = 2^(offset bits). Common: 4 KB (12-bit offset), 2 MB, 1 GB (huge pages)

Address Space Example (x86-64, 4 KB pages)

64-bit VA: [ 16 sign-ext | 9 PML4 | 9 PDP | 9 PD | 9 PT | 12 offset ]
  • 4-level page table on x86-64 (PML4 → PDPT → PD → PT → page) → 48-bit VA
  • Bits 63:48 must equal bit 47 (canonical addresses — sign-extended); non-canonical access faults (#GP)
  • 5-level paging (LA57): adds a PML5 level → 57-bit VA (128 PiB); shipping on Ice Lake+ servers, enabled per boot
  • Each table has 2⁹ = 512 entries × 8 bytes = 4 KB per table
  • ARM uses a similar structure (PGD → PUD → PMD → PTE)

Page Table Entry (PTE) Fields (x86-64)

Bit(s)FieldMeaning
0Present (P)1 = page in physical memory
1R/W0 = read-only, 1 = read/write
2U/S0 = supervisor only, 1 = user accessible
3Write-throughCache write policy
4Cache disableBypass cache (MMIO)
5Accessed (A)Set by hardware on any access
6Dirty (D)Set by hardware on write
7PAT / Page sizeIf set at PD level → 2 MB huge page
11:8AvailableOS use
51:12PPNPhysical page frame number
62:59Protection key4-bit key (PKU/PKS), permission checked against PKRU
63NX / XDNo-execute (prevents code execution)

Page Fault Types

TypeCauseOS action
Demand page faultPage not loaded yet (P=0)Load page from disk, update PTE
Protection faultWrite to read-only page, or user access to kernelSIGSEGV / segmentation fault
Copy-on-Write (CoW)Write to shared read-only copy after fork()Duplicate page, mark both writable
Minor faultPage not mapped, but in page cacheMap the existing physical page
Major faultPage must be read from diskI/O required; high latency

TLB (Translation Lookaside Buffer)

The page table walk is expensive (3–5 memory accesses for a 4-level table). The TLB is a small cache of recent VA→PA translations — L1 TLBs are fully or highly associative; larger L2 TLBs are set-associative (4–8-way) on modern cores.

TLB hit: ~1 cycle extra latency (parallel with cache lookup) TLB miss: ~20–50+ cycles (hardware page-table walk, PTW)

Typical TLB Parameters

LevelEntriesHit latencyNotes
L1 ITLB64–1280–1 cyclesInstruction-only
L1 DTLB32–640–1 cyclesData-only
L2 Unified TLB512–20485–10 cyclesCombined
Page-Walk CacheCaches intermediate page-table entries

TLB Shootdown

When the OS modifies a PTE (e.g., unmaps a page), it must invalidate any TLB entry holding that VA across all cores using that address space:

  1. OS modifies PTE
  2. OS sends IPI (Inter-Processor Interrupt) to other cores
  3. Each core executes INVLPG <va> (x86) or TLBI (ARM)
  4. Originating core waits for acknowledgements
  5. Flushing continues

TLB shootdowns are expensive — minimize mappings changes (mmap/munmap) in hot paths.

Page Replacement Policies

When a page fault occurs and physical memory is full, the OS must evict a page:

AlgorithmDescriptionOptimal?Notes
Optimal (OPT)Evict the page not needed for longest future timeYesTheoretical; not implementable
LRUEvict least recently usedNear-optimalExpensive to implement exactly
Clock (second chance)Circular list; evict first page with A=0Classic UNIX/Mach; PostgreSQL's buffer pool (clock-sweep)
LRU-KUse Kth most recent accessMore scan-resistant; LRU-2 used in SQL Server's buffer manager
NRU (Not Recently Used)Combine A and D bits into 4 classesCheap; approximate LRU

Linux does not use classic clock — it approximates LRU with two lists (active/inactive) per memory cgroup, promoting/demoting pages between them (multi-gen LRU since 6.1).

NRU Classes

ClassAccessedDirtyPreference to evict
000First (best)
101Second
210Third
311Last (worst)

Huge Pages

Sizex86-64ARMBenefit
4 KBStandardStandardFine-grained control
2 MBLarge page (PD entry)2 MB blockFewer TLB entries for same range
1 GBHuge page (PDP entry)1 GB blockMinimal TLB pressure

Linux: Transparent Huge Pages (THP) automatically promotes 4 KB pages to 2 MB when contiguous. hugetlbfs for explicit huge-page allocation.

Memory Protection

MechanismDescription
Read/Write/Execute bitsPer-page permissions in PTE
Supervisor bitKernel vs user access control
NX bit (no-execute)Prevents executing data pages (defeats shellcode)
SMEP / SMAPSupervisor cannot execute/access user pages (x86)
ASLRRandomize base addresses of stack, heap, libraries
PIE (Position Independent Executable)Text relocatable; ASLR can randomize code

Segmentation (legacy)

x86 originally used segments (base + limit per logical address region) before paging. Modern x86-64 uses flat segmentation (all segments base=0, limit=2⁶⁴) — paging is the real protection mechanism. Segments still exist for fs and gs as thread-local storage pointers.

Copy-on-Write (CoW) in fork()

Parent calls fork():
  - Child gets a copy of parent's page table
  - All pages marked read-only in both parent and child
  - Physical pages are shared

First write by either process:
  - Protection fault
  - OS allocates new page, copies content
  - Updates faulting process's PTE to new page, marks R/W
  - Other process still references original (now also R/W)

CoW makes fork() + exec() extremely cheap — only modified pages are actually copied.