Understanding Page Fault Handling and Virtual Memory Management in uCore OS
This article explains the principles of virtual memory, the data structures and functions used in uCore to manage page faults, and how the operating system handles demand paging, page swapping, and invalid accesses through detailed code examples and workflow descriptions.
When a program accesses a virtual memory page that is not present in physical memory, a page fault exception is triggered; the operating system must handle this exception by loading the required page from disk into memory.
The core of virtual memory management is the page fault handling process, primarily implemented in the do_pgfault function. The OS uses this mechanism to perform demand paging and page swapping, allowing the program to continue execution after the fault is resolved.
Basic concepts : Virtual memory is the address space seen by the programmer or CPU, which may not have a direct physical counterpart. Memory address virtualization allows the OS to set page table entries to restrict access, providing protection and enabling on‑demand allocation of physical pages.
Key data structures in uCore include struct vma_struct and struct mm_struct :
struct vma_struct {
struct mm_struct *vm_mm;
uintptr_t vm_start; // start address of VMA
uintptr_t vm_end; // end address of VMA
uint32_t vm_flags; // VMA flags
list_entry_t list_link; // linked list sorted by start address
}; #define VM_READ 0x00000001 // read‑only
#define VM_WRITE 0x00000002 // read‑write
#define VM_EXEC 0x00000004 // executable struct mm_struct {
list_entry_t mmap_list; // list of all VMAs
struct vma_struct *mmap_cache; // fast‑path cache
pde_t *pgdir; // page directory
int map_count; // number of VMAs
void *sm_priv; // swap manager private data
};Functions for managing VMAs include vma_create , insert_vma_struct , and find_vma ; for MM structures, mm_create and mm_destroy handle allocation and cleanup.
The page‑fault handling flow in uCore is:
trap → trap_dispatch → pgfault_handler → do_pgfaultWhen a fault occurs, the CPU saves the faulting address in CR2 and an error code on the stack. The kernel examines the address against the VMA list to determine whether the access is legal, whether the page is present, and whether permissions are correct. If the VMA is valid but the page is missing, a free physical page is allocated, the page table is updated, the TLB is flushed, and execution resumes at the faulting instruction.
Page faults are classified as hard (major) faults requiring disk I/O, soft (minor) faults where the page already resides in memory, and invalid faults caused by illegal addresses such as null‑pointer dereferences.
The article concludes with a step‑by‑step summary of how the OS detects a missing page, invokes the fault handler, allocates or loads the page, updates the page tables, and finally restarts the interrupted instruction.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.