Lesson 5 of 15

Virtual Address Layout

Virtual Addresses

Every process in Linux sees its own virtual address space — a flat 64-bit range from 0 to 2^64. The kernel maps these virtual addresses to physical RAM using the MMU (Memory Management Unit).

A virtual address is split into two fields:

63        12 11       0
┌──────────┬──────────┐
│   VPN    │  Offset  │
│ (52 bits)│ (12 bits)│
└──────────┴──────────┘
  • Offset — position within the page (low page_bits bits)
  • VPN (Virtual Page Number) — which page (high bits)

With 4KB pages (page_bits = 12), each page holds 4096 bytes. The offset selects a byte within that page; the VPN selects which page.

Extracting the Fields

unsigned long offset = vaddr & ((1UL << page_bits) - 1);
unsigned long vpn    = vaddr >> page_bits;

For vaddr = 0x5ABC with page_bits = 12:

  • mask = (1 << 12) - 1 = 0xFFF
  • offset = 0x5ABC & 0xFFF = 0xABC
  • vpn = 0x5ABC >> 12 = 0x5

Your Task

Implement void parse_vaddr(unsigned long vaddr, int page_bits) that prints the VPN and offset in hexadecimal.

TCC compiler loading...
Loading...
Click "Run" to execute your code.