Implementation Overview
This section documents the actual paging implementation in memory/paging.rs. Everything here reflects the current working code.
File: memory/paging.rs
This single file contains the complete paging subsystem:
- Frame allocator (bump allocator)
- Page directory and table management
map_page,unmap_page,get_physaddrinit_paging— the entry point called fromkernel_main
Initialization Sequence
init_paging(mem_size) runs once at boot, after the IDT, PIC, interrupts, and GDT are set up:
init_paging(16MB)
│
├─ Get _kernel_end from linker script
├─ Align up to 4KB boundary
├─ init_frame_allocator(start, mem_size)
├─ allocate_frame() → Page Directory
├─ Zero all 1024 PD entries
├─ allocate_frame() → Page Table 0
├─ Identity map 1024 pages (0x000000 – 0x3FFFFF)
├─ PD[0] = Page Table 0
├─ PD[1023] = Page Directory itself (recursive)
└─ enable_paging(pd_physical_address)
├─ mov cr3, pd_phys
└─ set bit 31 of cr0
After enable_paging returns, all memory accesses go through the page tables.
Subchapters
- Frame Allocator — How physical frames are managed
- Page Directory — Allocation and zeroing of the PD
- Identity Mapping — First 4MB implementation
- Recursive Mapping — The PD[1023] trick
- Enabling Paging — CR3 and CR0 manipulation