Page fault (source code)

= Page fault
{wiki}

A <page fault> transfers control to the <operating system> when an access cannot proceed under the current <paging> translation or protection rules. The term includes both unavailable translations and forbidden accesses on <x86>. Intel's manual distinguishes these causes; AMD's error code likewise distinguishes ordinary not-present faults from protection violations. Other architectural causes, including malformed <page table entries>, also exist. https://cdrdv2-public.intel.com/671200/325462-sdm-vol-1-2abcd-3abcd.pdf[Intel SDM, volume 3A, section 4.7: Page-Fault Exceptions]{ref}https://docs.amd.com/api/khub/documents/sD1_QL~h4Afq2_tvzxqqSQ/content[AMD64 Architecture Programmer's Manual, volume 2, revision 3.44, section 8.4.2: Page-Fault Error Code]{ref}

The illustrative <page table> in <paging> separates three situations:
* A read at <virtual address> `0x1434` targets a permitted but nonresident <memory page>. The <operating system> may supply it and retry the access.
* A write at <virtual address> `0x1234` targets a resident, read-only <memory page>. The <page fault> is caused by missing write permission, not by missing contents.
* A read at <virtual address> `0x1534` targets an unmapped part of the <address space>. The <operating system> must reject it unless its policy authorizes creating a mapping.

A <page fault> is not necessarily a program error or a storage-device read. <Demand paging> can allocate a zero-filled <page frame> without reading secondary storage. A <copy-on-write> write deliberately encounters read-only permissions; the <operating system> supplies a private copy and enables the write. A truly forbidden access may instead terminate the <operating-system process>. The handler interprets the fault using both architectural fault information and its own mapping policy. https://pages.cs.wisc.edu/~remzi/OSTEP/vm-complete.pdf[Arpaci-Dusseau and Arpaci-Dusseau, OSTEP chapter 23: demand zeroing and copy-on-write]{ref}

For a nonresident <memory page> whose contents must be loaded, the handler obtains a <page frame>, possibly invokes <page replacement>, supplies the contents, updates the <page table>, arranges any necessary <translation lookaside buffer> invalidation, and resumes the operation. If no mapping or permission can legally be supplied, it reports failure. This mechanism lets ordinary accesses request work only when it becomes necessary. <Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others/Beyond Physical Memory: Mechanisms>[Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others; Arpaci-Dusseau and Arpaci-Dusseau, OSTEP chapter 21: Beyond Physical Memory, Mechanisms, sections 21.3–21.5]{ref}

// Target: computer-science.bigb