Part 3 of 7 · 3 min read
The page fault
In plain words: Suppose the program reads address 0x7f3a2000, and the page table entry for that page has the present bit clear. The hardware cannot finish the access. It raises an exception, called a page fault, and control passes to the kernel. A program does not see any of this. After the fault is handled, the program continues as if nothing happened, only later.
The steps, in order (the numbers of frames and pages are small to keep the trace short):
- The hardware finds the present bit clear and traps into the kernel. It records the faulting address (on x86-64, in the CR2 register) and the kind of access (read, write or instruction fetch).
- The kernel checks whether the address is legal. It looks in the list of regions of the process (page 1 showed these regions). If the address lies in no region, or the access is not allowed, such as a write to a read-only region, the process receives a signal. On Linux, this is SIGSEGV, the familiar segmentation fault. The process usually ends here.
- If the access is legal, the kernel finds a free frame. If there is none, it must first free one (page 8 covers that choice).
- The kernel fills the frame. For a page of zeros, it clears the frame. For a page of a file, it reads the block from disk. For a page that was swapped out, it reads the page back from swap space.
- The kernel updates the page table entry: it writes the frame number and sets the present bit.
- The process becomes ready again, and the faulting instruction runs again from its start. This time the translation works.
Step 4 is the long one when it needs a disk read. While the disk works, the kernel runs another process, in the same way as for any other wait (Level 3). So a page fault that needs the disk behaves much like a blocking system call.
Step 6 needs the hardware to make instructions restartable: when a fault stops an instruction half way, the machine state must allow the instruction to run again from the start with no wrong result. Most processors guarantee this. An instruction that copies a block of bytes can touch several pages, and each of them may fault in turn, so the instruction can be restarted several times.
Page fault
A page fault is an exception raised by the hardware when an access uses a page whose page table entry does not allow it, usually because the present bit is clear, and the kernel's handler either makes the page available so that the instruction can run again or ends the process with a signal.
Example: the first write to a freshly allocated array page raises a page fault, and the kernel gives the process a frame full of zeros.
Common mistake
A page fault is not always an error. Most page faults are a normal part of running: the kernel handles them and the program never notices. Only a fault on an address that the process may not use leads to a segmentation fault.
Check yourself: 1. What does the kernel check first when a page fault arrives? 2. Why must an instruction be restartable? 3. What happens to the process when the disk read is under way?