A 1 GiB process calls fork(), and the child immediately replaces its image with a small program. Eagerly copying 1 GiB delays the child and consumes memory for bytes it never reads. Pre-materializing every valid page avoids its initial not-present fault but pays work before demand exists. Deferring all allocation without recording region intent lets an invalid pointer masquerade as legitimate demand.
A page fault resolves that tension. It is a synchronous exception raised by the current instruction when address translation or access checking cannot complete. Some faults are policy requests—materialize a valid lazy page or make a private copy. Others are violations—unmapped address, forbidden operation, or access beyond the backing object. The same hardware event can represent either class; region metadata and access intent decide which.
Fault-resolution invariant. Retrying the faulting instruction is safe only after the kernel has installed a mapping that (1) belongs to the faulting region, (2) represents the correct backing object and byte offset, (3) grants no more authority than the region allows, and (4) is visible after required translation invalidation.
A fault is evidence, not a diagnosis
The assessed machine uses precise, restartable RISC-V page faults. sepc identifies the faulting instruction; scause identifies the access class; stval supplies the faulting virtual address when the implementation reports it. Relevant synchronous cause codes are:
scause code | Access that failed | Typical policy distinction |
|---|---|---|
| 12 | instruction page fault | executable mapping, code loading, or execute denial |
| 13 | load page fault | readable mapping, demand-zero, or file read |
| 15 | store/AMO page fault | writable mapping, dirtying, or copy-on-write |
Hardware does not know whether an absent PTE means “grow a heap,” “load a file page,” “guard page,” “mapping removed,” or “process bug.” The kernel must look up the virtual-memory region that contains the address and apply its policy.
A useful classification key is the tuple
| Region/PTE condition | Load | Store | Instruction fetch |
|---|---|---|---|
| anonymous, authorized, absent | allocate zeroed frame | allocate zeroed frame | reject unless region executable |
| private file, authorized, absent | load/cache file page | load then preserve private-write semantics | load if executable |
| present COW mapping | read shared frame | create or promote private writable frame | follow execute permission |
| present genuine read-only mapping | read | reject | follow execute permission |
| no containing region | reject | reject | reject |
| guard region | reject | reject | reject |
Retrieval — state the missing evidence. A store page fault arrives at an address whose PTE has W=0. Why is “make it writable” an invalid generic handler?
Reveal the diagnostic answerExpandCollapse
W=0 could encode a genuine read-only mapping, a COW mapping, a guard, or an invalid state. The handler needs region permission and a COW-specific marker or equivalent metadata. Promoting every read-only PTE grants write authority to code, constants, and shared read-only objects.
Page fault classification and copy-on-write invariants
Evaluate each statement regarding page faults, copy-on-write fork semantics, and memory-mapped files:
Classify each option as correct or incorrect, or leave uncertain options unanswered (“I don't know”) and verify at any time.
Derive a restartable fault protocol
Fault resolution crosses several ownership domains: process metadata, page tables, physical allocator, file/page cache, and scheduler. A robust protocol separates validation, preparation, commit, and retry.
The prepare-before-commit shape matters under memory pressure. If allocation or file I/O fails, the old mapping remains valid and no partial authority is exposed. Revalidation matters under concurrency: another thread may resolve, unmap, protect, or replace the region while the handler sleeps for memory or storage.
The handler also needs a bounded lock discipline. Holding a spinlock across file I/O or a sleeping allocator deadlocks or stalls unrelated CPUs. Dropping all protection without recording a generation permits committing a page into a region that has been unmapped and reused. A production kernel solves this with layered locks, reference counts, region versions, or retry protocols; the assessed invariant is that commit rechecks the state it relied on.
Worked trace: anonymous demand-zero allocation
Region [0x400000, 0x404000) is anonymous, user read/write, non-executable. No pages are resident. A process stores 7 at 0x4012A0.
scause=15identifies a store page fault;stval=0x4012A0identifies the address.- Region lookup succeeds and authorizes a store.
- The absent PTE is an expected lazy state, not a COW or file-backed state.
- The kernel allocates a 4 KiB frame and zeroes all bytes before user exposure. Zeroing prevents disclosure of a prior owner’s data.
- Under the mapping lock, the kernel rechecks that the region still covers the address and the PTE is still absent.
- It installs a user read/write, non-executable leaf for page
0x401000, performs architecture-required translation synchronization, and retries the store. - The retried store changes only offset
0x2A0; every other byte remains zero.
If frame allocation fails at step 4, the PTE stays absent. Mapping an uninitialized frame and planning to zero it later violates confidentiality as soon as user mode can load from it.
Worked trace: a competing unmap
Thread T0 faults at 0x4012A0 and drops the region lock to allocate. T1 concurrently unmaps [0x400000, 0x404000) and maps a read-only file at the same virtual interval. T0 resumes with a zeroed frame.
Committing from the stale region snapshot would overwrite the new file mapping with a writable anonymous page. Revalidation detects that the region generation or backing object changed; T0 releases its frame and restarts classification. The restarted store now fails against the read-only file region.
Application — order the operations. Place these actions into a safe demand-zero sequence: publish writable PTE, zero frame, allocate frame, validate region, revalidate region/PTE, retry instruction, invalidate stale translation if needed.
Reveal the ordering and failure pointsExpandCollapse
Validate region → allocate frame → zero frame → revalidate region/PTE → publish writable PTE → invalidate any stale translation required by the architecture → retry. Allocation and zeroing may occur before the commit lock; publication cannot. Failure before publication releases the candidate frame. Failure after publication must use a defined rollback that first removes authority and completes invalidation; avoiding that complex path is why commit should contain no fallible work.
Copy-on-write preserves fork semantics lazily
POSIX requires a child’s private mappings to behave as private after fork(): writes performed by either process after the fork are not visible through the other’s private mapping. It does not require an eager physical copy. Copy-on-write (COW) implements the semantic contract with temporary physical sharing plus write faults.
For each writable private page at fork time:
- map the same PFN into parent and child;
- clear writable permission in both PTEs;
- mark both mappings as COW using software metadata distinguishable from genuine read-only state;
- increment the physical frame’s reference count for the additional mapping; and
- invalidate any parent translation that may still cache write authority before either process resumes.
The COW invariant is:
A COW frame may have multiple readers, but a process receives writable authority only to a frame that is private to the mapping or is being atomically converted to private ownership.
Worked trace: parent and child diverge
Before fork, parent VPN 6 maps PFN 40 RW; frame 40 contains bytes [A, B, C, …] and has reference count 1.
After fork:
| Mapping | PTE | Physical frame | Refcount |
|---|---|---|---|
| parent VPN 6 | R--, COW | 40 | 2 |
| child VPN 6 | R--, COW | 40 | 2 |
The child stores Z at offset 1:
- Hardware raises a store page fault because the child PTE is not writable.
- Region and software metadata prove this was originally writable private memory and is now COW.
- Refcount 2 means another mapping still needs frame 40. Before relying on its bytes outside the mapping lock, the handler acquires a temporary reference/pin to frame 40. It allocates frame 91 and copies all 4 KiB from the pinned source.
- Under the relevant locks, it revalidates the child PTE, establishes frame 91’s mapping reference, and maps child VPN 6 to frame 91 as
RWwithout COW. It retains both the child’s old mapping reference to frame 40 and the temporary pin while any stale child translation can still name frame 40. - It completes the required invalidation on every hart that could use the child’s old translation. Only after those acknowledgements may it drop the child’s old mapping reference and then the temporary pin. Frame 40 now has one mapping reference, held by the parent.
- The store retries and changes frame 91 offset 1 to
Z. Parent frame 40 remains[A, B, C, …].
The parent later writes the same page. Since frame 40 now has refcount 1 and the parent’s PTE is still COW, the kernel may remove the COW marker and restore write permission in place. No copy is needed, but translation invalidation still is.
The original permission must survive
Suppose a text page was read/execute before fork. Marking every shared page COW would let a store fault convert executable text into writable memory. The design needs to distinguish:
- private writable, temporarily read-only because of COW; and
- intrinsically read-only or execute-only.
Software-reserved PTE bits can carry a COW marker on an architecture that permits it, or region/page metadata can. The encoding choice is subordinate to the semantic proof: only a mapping whose policy includes write may resolve a COW store.
Concurrent COW faults
Two threads in one process can fault on the same COW page concurrently. Both may allocate candidate frames before either commits. At commit, one wins and installs its private frame. The other must detect that the PTE no longer matches the old COW state, release its unused candidate, and retry the store against the new writable mapping. Installing both candidates in sequence can leak one frame or lose a completed write.
A failure-atomic skeleton is:
lock mapping and old-frame references
snapshot := require current COW PTE
acquire a temporary pin while snapshot's mapping reference is valid
unlock
candidate := allocate and copy old frame
lock mapping and old-frame references
if current PTE != snapshot:
unlock; free candidate; drop temporary pin; retry classification
establish candidate's mapping reference
install candidate as private writable
retain old mapping reference and temporary pin
unlock
invalidate every target translation and wait for completion
drop old mapping reference, then drop temporary pin
retry instruction
The two retained old-frame references have different meanings: one accounts for the PTE that was replaced, and one pins the snapshot while the handler works. Neither may be released in a way that makes the old frame reusable before stale translations are gone. A kernel may instead invalidate the old PTE and complete shootdown before publishing the candidate, but it must serialize competing faults through that transient state. The real implementation must define lock ordering, shootdown waiting, and the exact reference-count operations. The skeleton intentionally omits a complete kernel patch.
Transfer — allocation failure. A child faults on a shared COW page with refcount 2, but no frame is available. Which three states must remain unchanged, and why is “temporarily grant write to the shared frame” invalid?
Reveal the failure contractExpandCollapse
The child’s PTE must remain read-only COW, the parent’s mapping must remain unchanged, and the shared frame’s reference count/content must remain valid. Granting the child write authority would make its modification visible to the parent, violating private mapping semantics. The kernel may report an out-of-memory failure or apply a documented reclamation policy; it cannot weaken isolation to make the instruction complete.
mmap binds regions to memory objects
mmap() establishes a region whose bytes correspond to an anonymous object or to a file object’s byte range. prot describes permitted accesses. MAP_PRIVATE and MAP_SHARED describe the disposition of writes, not whether initial physical pages are shared.
| Mapping | Read fault | First authorized write | Persistence relation |
|---|---|---|---|
| anonymous private | supply zero page/frame | private writable frame | no file object |
file MAP_PRIVATE | obtain file-backed page | COW to process-private frame | modification does not change underlying object |
file MAP_SHARED | obtain shared file page | dirty shared page under VM/filesystem rules | modification targets underlying object |
A private mapping can initially reference the same page-cache frame as other mappings. “Private” constrains the visibility of modifications; it does not demand a unique frame for every read.
Worked trace: one file, two mapping policies
A file contains 6,000 bytes. Page size is 4,096 bytes. Process P maps 8,192 bytes MAP_PRIVATE | PROT_READ | PROT_WRITE; process Q maps the same range MAP_SHARED | PROT_READ | PROT_WRITE.
- P and Q read offset 100. Both may map the same page-cache frame; no private copy is required.
- P writes offset 100. A COW fault gives P a private frame for the first file page. Q and the file object do not observe P’s byte.
- Q writes offset 5,000. Q modifies the shared second file page and marks it dirty according to the OS/filesystem contract.
- Bytes from file offset 6,000 to the end of the last partial page are zero-filled under POSIX mapping semantics, and modifications there are not written to the file.
- If a longer mapping included a whole page beyond the object and the process referenced that page, POSIX requires
SIGBUSfor the out-of-object whole-page access.
Closing the file descriptor after a successful mapping does not remove the mapping: POSIX specifies that the mapping holds an additional reference to the file until the mappings are gone.
Truncation is a concurrent object change
If another process changes the mapped file’s size after mmap, POSIX leaves the effect on added or removed portions unspecified. A robust application cannot treat a concurrent truncation as an ordinary stable-memory event. Kernel internals still need coherent invalidation and fault behavior, but portable application reasoning must stay within the interface contract.
Application — classify without the slogan “fault equals disk.” For each event, state whether disk I/O is required under the information given:
- first write to a COW page whose source frame is resident;
- first read of an anonymous demand-zero page;
- read fault on a file page already in the page cache;
- read fault on a nonresident file page;
- store to a genuine read-only mapping.
Reveal classificationsExpandCollapse
- No file read is required; memory allocation and a RAM copy may be required.
- No file read is required; a zeroed frame or protected shared-zero-page mechanism is sufficient.
- No storage read is required; install a translation to the resident cached page subject to permissions.
- Storage I/O is required unless another cache/device layer already supplies the data.
- No recovery I/O is justified; the access is rejected.
The diagnostic gap is conflating a translation exception with one particular backing-store action.
Fault-handler boundaries that prevent security defects
Zero before exposure
A newly allocated user frame may contain bytes from a previous process or kernel object. Zeroing after PTE publication creates a disclosure interval. The zeroing operation must complete before any user-readable mapping exists.
Validate arithmetic and object offsets
For file-backed mapping start fault address and file offset the backing byte begins at
Every subtraction, alignment, addition, and length check must be overflow-safe. An overflow that wraps into the beginning of a file can map an authorized virtual address to the wrong object bytes.
A timeout or kill does not revoke DMA or I/O
If a process is killed while a fault waits on storage, the in-flight I/O may still own a kernel buffer. The process mapping can be abandoned only through a path that preserves buffer and request lifetimes. Process cancellation is not device cancellation.
Kernel accesses to user memory need a policy
Copying syscall data through copyin or copyout may encounter lazy pages without arriving through the ordinary user-trap path. The kernel must either invoke the same validated fault mechanism or reject the copy according to its contract. Bypassing region checks because the CPU is privileged can turn a user pointer into kernel-chosen memory corruption.
Deep Practice
Solve these problems to test and consolidate your understanding. Complete worked solutions and diagnostic hints are available for each problem.
Exercise 4.3.1: Demand-Zero Anonymous Page Fault State Transitions
A user process invokes sbrk(4096) to expand its heap by one 4 KiB page. The kernel records the virtual allocation in its region descriptor list but leaves the hardware PTE invalid
- State the hardware register values (
scause,sepc,stval) when the user code first reads from the new virtual page address0x00405000. - Trace the kernel page-fault handler actions to resolve this fault cleanly.
- Why must the newly allocated physical frame be zeroed before the PTE is set to valid?
HintExpandCollapse
scause 13 indicates a load page fault; stval holds the faulting address; sepc holds the faulting instruction PC.
Worked solutionExpandCollapse
- Hardware trap state:
scause: 13 (Load page fault exception).stval:0x00405000(the faulting virtual memory address).sepc: points to the specific load instruction that triggered the fault.
- Kernel resolution steps:
- Trap entry saves user registers and queries
stvalandscause. - The handler searches the process virtual memory region tree and verifies that
0x00405000falls within an authorized, readable anonymous heap region. - The kernel allocates a free physical frame from the physical memory pool.
- The kernel zeroes all 4096 bytes of frame
- The kernel installs a leaf PTE mapping with
- The kernel executes
SFENCE.VMA(or issues appropriate TLB invalidation if required). - Trap return restores registers and executes
sret, re-executing the faulting instruction atsepc, which now succeeds.
- Trap entry saves user registers and queries
- Information disclosure defense: If the frame is mapped into user space before being zeroed, a concurrent thread or immediate load instruction can read residual bytes left behind by prior processes or the kernel itself, creating a critical information leak vulnerability.
Exercise 4.3.2: Tracing Copy-on-Write Reference Counts and Private In-Place Promotion
A parent process owns a private writable page mapped to physical frame The parent forks child which later forks child No process writes until after all forks complete.
- State the PTE permissions and physical frame reference count for immediately after the second fork.
- Child executes a store to the page. Describe the fault handling, frame allocation and updated reference counts.
- Parent terminates. Child then executes a store to the page. Describe how the kernel handles this second write without copying.
HintExpandCollapse
On the second write, check if the remaining reference count is equal to one.
Worked solutionExpandCollapse
- State after two forks:
- Each process (Parent, maps with permissions (read-only COW).
- The kernel reference count for frame is
- First store by
- triggers a store page fault (
scause15). - Handler verifies has write authority in its region descriptor, but
- Handler allocates a fresh physical frame copies 4096 bytes from to and sets
- Handler updates ‘s PTE to map to with
- Handler decrements from 3 to 2, flushes ‘s local TLB, and returns to user space.
- triggers a store page fault (
- Termination and second store by
- When Parent terminates, its page table is unmapped, decrementing from 2 to 1.
- When writes, it triggers a store page fault on
- Handler checks and discovers it equals 1.
- Because is the sole remaining owner of no memory copy is needed. The kernel directly promotes ‘s PTE in place by setting
- After invalidating ‘s TLB, the store completes on without allocating memory.
Exercise 4.3.4: Handling Memory Exhaustion during Copy-on-Write Allocation
Suppose two processes share a COW page frame Process executes a store instruction, triggering a COW fault, but the physical frame allocator returns NULL because physical memory and swap are completely exhausted.
- Why must the kernel NOT set directly on frame to let the store proceed?
- Why can the kernel NOT ignore the fault and resume user execution at
sepc + 4? - What is the correct, safe kernel failure behavior?
HintExpandCollapse
Setting W=1 destroys process isolation; skipping the instruction silently skips user computation.
Worked solutionExpandCollapse
- Violation of memory isolation: If the kernel set on the shared frame without allocating a private frame, Process ‘s store would overwrite memory visibly belonging to Process This would destroy process memory isolation, leading to arbitrary data corruption or privilege escalation.
- Violation of execution correctness:
If the kernel skipped the store by resuming at
sepc + 4, the user program would proceed under the false assumption that its memory update completed, causing silent mathematical and data structure corruption. - Correct failure recovery:
The kernel must treat allocation failure as an unrecoverable resource exhaustion condition for the faulting thread or process. It either:
- Blocks the thread until memory can be reclaimed by eviction or page out (if asynchronous reclamation is configured), or
- Delivers a
SIGBUSorSIGSEGVsignal to Process or terminates Process via the Out-Of-Memory (OOM) killer, leaving Process ‘s state and frame completely intact.
Exercise 4.3.5: Proving Safe Kernel Copyin Execution over Lazy Virtual Memory
When a user program passes a pointer buf to the read(fd, buf, count) system call, the target buffer may reside on a valid but unmaterialized lazy anonymous page. Prove that the kernel must validate user region bounds and permissions rather than relying on raw supervisor loads and stores.
HintExpandCollapse
In supervisor mode, page fault protections may differ; passing an arbitrary kernel address must be rejected.
Worked solutionExpandCollapse
Assume for contradiction that the kernel implements copyout by directly dereferencing the user pointer in supervisor mode without explicit user-address validation:
- In supervisor mode, the MMU may allow stores to supervisor pages unless restricted by hardware features like SMAP (Supervisor Mode Access Prevention).
- If an adversarial user process passes a kernel data structure address (e.g.,
buf = 0xFFFFFFFF80000000), a naive supervisor dereference will read or write directly into kernel memory, allowing an unprivileged process to overwrite page tables or kernel text. - Furthermore, if
bufpoints to an unmapped user address or a read-only code region (PROT_READ), a blind kernel write would either trigger an unexpected kernel-mode trap or overwrite read-only binaries.
Therefore, the kernel must execute copyin/copyout using specialized routines that:
- Explicitly verify that
[buf, buf + count)lies strictly within the user virtual address range. - Validate that the containing virtual memory region grants the requested access for
copyout, forcopyin). - Install a trap recovery fixup table so that any unexpected page fault inside the copy routine returns
-EFAULTcleanly to the system call caller rather than panicking the kernel.