Skip to content

3 Memory Management

Glen edited this page Nov 14, 2026 · 5 revisions

Memory Management

Relevant source files

Purpose and Scope

This section provides a high-level overview of the memory management subsystem in CadenzaOS. The memory architecture bridges early-boot physical memory discovery with higher-half virtual memory mapping, bitmap-based frame allocation, and dynamic 4-level page table management src/kernel/memory.zig1-21src/kernel/frame_allocator.zig1-16.

Because this is a parent page, low-level implementation details are summarized briefly here and delegated to specialized child pages:

Sources: src/kernel/memory.zig1-21src/kernel/frame_allocator.zig1-16


Subsystem Architecture

CadenzaOS organizes memory around a higher-half direct map. The kernel code and data structures are mapped into virtual addresses starting at 0xFFFFFF8000000000 (KERNEL_OFFSET), allowing straightforward translation between physical and virtual addresses src/kernel/memory.zig20-35.

diagram

Sources: src/kernel/memory.zig1-35src/kernel/frame_allocator.zig32-136


3.1 E820 Memory Map

The foundational layer of memory management relies on the BIOS/UEFI E820 memory map collected during early boot stages. The kernel reads this table via E820Store.zig to discover available RAM, reserved memory ranges, and unusable hardware regions src/kernel/E820Store.zig1-57.

For detailed information on parsing routines, E820Store.zig, and diagnostic tools such as e820_test, refer to E820 Memory Map.

Sources: src/kernel/E820Store.zig1-57


3.2 Frame Allocator and Bitmap

Once usable physical memory regions are identified, the frame allocator tracks individual 4 KiB physical frames src/kernel/bitmap.zig1-30. The subsystem utilizes a bitmap structure (bitmap.zig) to record allocated and free frames, initialising all memory as reserved and subsequently carving out usable regions src/kernel/bitmap.zig75-107.

For detailed information on frame tracking, contiguous block allocation, and bitmap manipulation, refer to Frame Allocator and Bitmap.

Sources: src/kernel/bitmap.zig1-107src/kernel/frame_allocator.zig32-136


3.3 Paging and Page Allocation

Virtual memory in CadenzaOS is managed via x86_64 4-level paging (PML4 -> PDPT -> PD -> PT -> Page) src/kernel/memory.zig129-136. The core memory module (memory.zig) provides address translation helpers (physToVirt, virtToPhys) and the dynamic page mapping function mapPage, which allocates missing intermediate table structures on demand using the bitmap frame allocator src/kernel/memory.zig27-160.

diagram

Sources: src/kernel/memory.zig95-202

For detailed discussions on mapPage, page allocators, and kernel fault panic handling, refer to Paging and Page Allocation.

Sources: src/kernel/memory.zig95-202src/kernel/page_allocator.zig1-50

Clone this wiki locally