Maps to kfs3 of the kfs subject series from the 42 advanced curriculum.
A kernel that:
- Enables 32-bit paging.
- Creates page directory and page table structures for
i386. - Loads
cr3with the active page directory. - Enables paging through
cr0. - Keeps the kernel alive across the paging switch.
- Defines kernel-space and user-space virtual address ranges.
- Represents page rights, at minimum:
- present
- writable/read-only
- supervisor/user
- Implements functions to create, get, map, and unmap memory pages.
- Implements physical-memory allocation, free, and size retrieval for variable-sized allocations.
- Implements virtual-memory allocation, free, and size retrieval for variable-sized allocations.
- Provides the subject-required physical memory helpers:
kmallockfreeksizekbrk
- Provides the subject-required virtual memory helpers:
vmallocvfreevsizevbrk
- Handles kernel panics with print-and-stop behavior.
- Distinguishes fatal and non-fatal panic/error situations.
The subject requires allocation, free, and size retrieval for a variable in both physical and virtual memory. That means the public allocator behavior must be variable-sized, even if the backing implementation reserves whole frames or whole pages internally.
For each successful allocation, the kernel must record allocation metadata so that:
- the allocation can be freed by its returned pointer/address;
- the allocation's size can be retrieved later;
- freeing an unknown or already freed allocation is detected as an error;
- the allocator can distinguish the requested variable size from any internal rounded backing size.
For this project, ksize and vsize should return the requested variable size,
not the rounded frame/page span. The rounded backing size may be exposed through
separate debugging output later if it becomes useful.
Stage 3 uses classic non-PAE 32-bit paging:
- one page directory contains 1024 entries;
- one page table contains 1024 entries;
- a normal page is 4 KiB;
- a page table maps 4 MiB;
- a page directory can describe 4 GiB of virtual address space.
The initial mapping should be conservative:
- identity-map the low memory needed to survive the paging switch;
- map the kernel image and statically reserved kernel data;
- reserve a kernel virtual allocation area;
- reserve user-space ranges without executing userspace yet.
These are useful if they remain small and directly support the mandatory memory work:
- Typed addresses - use newtypes for physical addresses, virtual addresses, frames, pages, and allocation sizes.
- Boot diagnostics - print a compact memory self-check after paging and allocation are online.
- Allocator invariants - validate alignment, double-free detection, and page-right expectations in debug output.
- Small-object heap sophistication - slabs, bins, coalescing heaps, and sub-page reuse can come later. Stage 3 may back variable-sized allocations with whole pages or frames as long as it records and reports variable sizes correctly.
- Swapping/disk-backed memory - mentioned as motivation in the subject text, but not required for this stage.
- Real process isolation - define user-space ranges and page rights now; actual userspace execution belongs to later stages.
- Copy-on-write and demand paging - not required for the first paging and allocation implementation.
Interrupt-driven page fault handling, multitasking, ELF loading, syscalls, userspace process startup, filesystem-backed memory, and advanced heap allocation policies.
make runboots the kernel in QEMU.- Paging is enabled and the kernel continues executing.
- Kernel and reserved user virtual ranges are defined.
- Pages can be created, retrieved, mapped, and unmapped with explicit rights.
- A physical allocation can be created for an arbitrary requested byte size, freed, and queried for its requested size.
- A virtual allocation can be created for an arbitrary requested byte size, accessed, freed, and queried for its requested size.
- Unknown or double frees are reported as non-fatal errors where possible.
- Fatal memory failures panic with useful output and halt.
- Boot-time diagnostics demonstrate both physical and virtual variable allocation paths.