Systems Concept

Virtual and Physical Pages

The MMU translates each virtual page number through a per-process page table to a physical page frame and adds the page offset; the kernel fills in those mappings lazily as page faults occur.

The short answer

A virtual address is split into a virtual page number and an offset within the page. The memory management unit (MMU) walks a per-process page table to translate the page number into a physical page frame, then adds the offset unchanged. The page table is built by the kernel, often lazily: an address range can be valid while its pages are not yet backed by physical memory. On x86-64 and ARM64 the walk goes through several levels, and a translation cache called the TLB avoids repeating it.

A minimal example

import mmap, os

region = mmap.mmap(-1, 1 << 30)   # reserve 1 GiB of address space
region[0] = 1                     # first touch faults in one page

with open(f"/proc/{os.getpid()}/status") as status:
    for line in status:
        if line.startswith(("VmSize", "VmRSS")):
            print(line.strip())

Run this on Linux: VmSize includes the full gigabyte immediately, while VmRSS grows by only a few pages, typically 4-16 KiB. Reserving virtual address space costs page-table entries, not physical data frames; frames are allocated on first use. macOS has no /proc, so the same check needs vmmap.

Why this happens

Each process needs the illusion of a private, contiguous address space, while physical memory is shared, fragmented, and limited. Paging provides that illusion: the same physical frame can be mapped into several processes, copy-on-write can share frames after fork(), and file-backed pages can come from the page cache. The cost is one translation per access, which hardware tries to hide with the TLB.

For x86-64 with four-level paging, a 48-bit virtual address is divided as follows:

Bits Field Role
63-48 Sign extension Must repeat bit 47 for a canonical address
47-39 PML4 index Selects the top-level entry
38-30 PDPT index Selects the next level
29-21 Page-directory index Selects the next level
20-12 Page-table index Selects the final entry
11-0 Offset Byte within the 4 KiB page

Each entry supplies the physical address of the next table, or of the final page frame, plus permission and status bits. ARM64 uses a similar hierarchical scheme with a configurable number of levels and base page sizes of 4, 16, or 64 KiB.

What changes the answer

  • Huge pages map a range with one entry: 2 MiB and 1 GiB on x86-64, with equivalent block mappings on ARM64. They reduce TLB pressure and table-walk depth, not the number of bytes read.
  • A present bit of zero causes a page fault. The page may be swapped out, waiting for file I/O, not yet allocated, or copy-on-write; the kernel's fault handler decides.
  • Permission bits are checked at every level, so a valid address can still fault on a write to a read-only mapping or on execution from a no-execute page.
  • Five-level paging extends x86-64 to 57-bit addresses when the CPU, kernel, and application all agree; ARM64 similarly supports 52-bit addresses in some configurations.
  • MAP_POPULATE, mlock(), and madvise() change when or whether frames are allocated.

Edge cases

A page fault is normal, not an error: the first write to a fresh malloc() block always faults. /proc/<pid>/pagemap can reveal physical frame numbers, but since Linux 4.0 the kernel zeroes them unless the reader has CAP_SYS_ADMIN. Unmapping memory requires TLB shootdowns on other CPUs, which is why frequent munmap() calls in multithreaded programs are expensive. Shared pages, transparent huge pages, and kernel same-page merging mean two virtual pages can legitimately map to one frame.

Practical checks

getconf PAGESIZE reports the base page size. /proc/<pid>/smaps shows the resident size, shared size, and kernel page size for each mapping. perf stat -e page-faults counts faults, and mincore() reports which pages of a mapping are resident. Translation overhead is only part of locality: once a page is resident, cost is dominated by cache lines, as covered in CPU cache lines and sequential reads.

Sources

Revision history

Published
Updated
— Initial publication.