# Memory Management Relevant source files - [src/kernel/E820Store.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig) - [src/kernel/bitmap.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig) - [src/kernel/memory.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig) - [src/kernel/page_allocator.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/page_allocator.zig) - [src/kernel/panic.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/panic.zig) - [src/kernel/pic.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/pic.zig) ## 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-21](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L1-L21)[src/kernel/frame_allocator.zig1-16](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L1-L16). Because this is a parent page, low-level implementation details are summarized briefly here and delegated to specialized child pages: - For E820 bootloader map parsing and region discovery, see [E820 Memory Map](/GlenMeehan/CadenzaOS/3.1-e820-memory-map). - For frame tracking, bitmap management, and contiguous allocation, see [Frame Allocator and Bitmap](/GlenMeehan/CadenzaOS/3.2-frame-allocator-and-bitmap). - For virtual address translation, `mapPage`, and page-level allocation, see [Paging and Page Allocation](/GlenMeehan/CadenzaOS/3.3-paging-and-page-allocation). Sources: [src/kernel/memory.zig1-21](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L1-L21)[src/kernel/frame_allocator.zig1-16](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L1-L16) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L20-L35). ![diagram](mermaids/3-memory-management-diagram-1.png) Sources: [src/kernel/memory.zig1-35](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L1-L35)[src/kernel/frame_allocator.zig32-136](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L32-L136) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L57). For detailed information on parsing routines, `E820Store.zig`, and diagnostic tools such as `e820_test`, refer to [E820 Memory Map](/GlenMeehan/CadenzaOS/3.1-e820-memory-map). Sources: [src/kernel/E820Store.zig1-57](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L57) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L1-L30). 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L75-L107). For detailed information on frame tracking, contiguous block allocation, and bitmap manipulation, refer to [Frame Allocator and Bitmap](/GlenMeehan/CadenzaOS/3.2-frame-allocator-and-bitmap). Sources: [src/kernel/bitmap.zig1-107](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/bitmap.zig#L1-L107)[src/kernel/frame_allocator.zig32-136](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/frame_allocator.zig#L32-L136) --- ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L129-L136). 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L27-L160). ![diagram](mermaids/3-memory-management-diagram-2.png) Sources: [src/kernel/memory.zig95-202](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L95-L202) For detailed discussions on `mapPage`, page allocators, and kernel fault panic handling, refer to [Paging and Page Allocation](/GlenMeehan/CadenzaOS/3.3-paging-and-page-allocation). Sources: [src/kernel/memory.zig95-202](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/memory.zig#L95-L202)[src/kernel/page_allocator.zig1-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/page_allocator.zig#L1-L50)