</>EmbeddedInterview

What is paging and what is a page fault?

MediumOperating System Concepts

Short answer: Paging splits virtual and physical memory into fixed-size blocks (pages and frames, usually 4 KB). The page table maps each virtual page to a physical frame with permission bits. A page fault is the exception the MMU raises when an access finds no valid mapping or not enough permission. The kernel either fixes it (allocate a page, read it from a file or swap, copy-on-write) and retries the instruction, or sends the process SIGSEGV if the access was invalid.

Address split (32-bit, 4 KB pages): virtual address = [ 20-bit page number | 12-bit offset ]. The page number indexes the page table; the offset (0–4095) is copied directly into the physical address.

Kinds of page fault

Kind Cause Kernel action
Minor (soft) page is in RAM but not mapped yet: first touch of anonymous memory, shared library page already in the page cache map a zero page or existing page; no disk I/O
Major (hard) page must be read from disk: swapped out, or a file-backed page not in cache start I/O, block the process, map when done
Copy-on-write write to a page shared after fork() copy the page, map the copy writable
Invalid address not in any mapping (NULL, freed memory), or permission violation (write to read-only, execute non-executable) send SIGSEGV (or SIGBUS)

What the hardware provides: the faulting address (FAR_EL1 on ARM64, CR2 on x86) and the reason (ESR_EL1 on ARM64: translation fault, permission fault, read or write, at which level).

Page table entries contain: physical frame number, valid bit, read/write/execute permissions, user/kernel bit, accessed and dirty bits (used for page replacement and write-back), and memory type (normal cacheable vs device memory on ARM).

Measuring: /usr/bin/time -v ./prog (major and minor faults), perf stat -e page-faults, /proc/<pid>/stat fields minflt/majflt.

Real-time relevance: a major fault can block for milliseconds. Real-time Linux applications call mlockall(MCL_CURRENT | MCL_FUTURE) and touch their stack and heap at start-up ("prefaulting") so no faults happen in the time-critical loop.