# E820 Memory Map Relevant source files - [src/kernel/E820.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig) - [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/debug.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/debug.zig) - [src/kernel/e820_test.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig) ## Purpose and Scope This page details the implementation of the E820 physical memory map subsystem in CadenzaOS. During early boot, the x86-64 BIOS provides a memory layout describing usable RAM, reserved regions, ACPI tables, and bad memory ranges. Because the bootloader's scratch memory area does not remain valid after initialization, CadenzaOS duplicates this table into kernel-managed storage. This subsystem encompasses raw map duplication ([src/kernel/E820Store.zig1-74](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L74)), read-only indexed queries ([src/kernel/E820.zig1-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L1-L63)), and bring-up diagnostics ([src/kernel/e820_test.zig1-102](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig#L1-L102) and [src/kernel/debug.zig1-98](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/debug.zig#L1-L98)). --- ## 1. Bootloader Memory Map Acquisition (`E820Store.zig`) The bootloader stage passes the physical address and entry count of the raw BIOS E820 array via boot information structures. Because this memory can be overwritten, [src/kernel/E820Store.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig) immediately copies up to `MAX_E820_ENTRIES` (64) into a static kernel-owned buffer during early initialization ([src/kernel/E820Store.zig21-27](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L21-L27)). ### Initialization Flow 1. `init()` reads the boot information via `bi.get()` to retrieve `e820_addr` and `e820_count` ([src/kernel/E820Store.zig55-57](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L55-L57)). 2. The entry count is clamped against `MAX_E820_ENTRIES` ([src/kernel/E820Store.zig59-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L59-L63)). 3. Entries are copied iteratively from the physical source address into the static `table` array ([src/kernel/E820Store.zig67-73](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L67-L73)). 4. `getTableAddr()` converts the virtual address of the copied table back to a physical address using `mem.virtToPhys()` ([src/kernel/E820Store.zig34-37](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L34-L37)), allowing the kernel to register it with higher-level components. `getTableCount()` exposes the active count ([src/kernel/E820Store.zig39-42](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L39-L42)). Sources: [src/kernel/E820Store.zig1-74](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L1-L74) --- ## 2. Kernel-Owned Table Representation (`E820.zig`) Once [src/kernel/E820Store.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig) populates the static buffer, [src/kernel/E820.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig) acts as the read-only interface for the rest of the kernel. It avoids pointer-aliasing issues by returning entries by value ([src/kernel/E820.zig57-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L57-L63)). ### Data Structures and Functions - `E820Entry`: An `extern struct` matching the standard 24-byte layout: - `base`: Physical start address (`u64`) [src/kernel/E820.zig17](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L17-L17). - `length`: Size of the region in bytes (`u64`) [src/kernel/E820.zig18](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L18-L18). - `entry_type`: Region classification (e.g., `1` for usable RAM) (`u32`) [src/kernel/E820.zig19](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L19-L19). - `acpi`: Extended ACPI attributes (`u32`) [src/kernel/E820.zig20](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L20-L20). - `setTable(addr: usize, count: usize)`: Registers the safe table address and entry count ([src/kernel/E820.zig41-44](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L41-L44)). - `getCount()`: Returns the number of valid entries ([src/kernel/E820.zig47-49](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L47-L49)). - `getEntry(index: usize) ?E820Entry`: Performs bounds-checking against `table_count` and computes the exact offset to return an `E820Entry` copy ([src/kernel/E820.zig57-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L57-L63)). Sources: [src/kernel/E820.zig1-63](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820.zig#L1-L63) --- ## 3. Diagnostics and Testing (`e820_test.zig` & `debug.zig`) CadenzaOS provides diagnostic tools to verify memory map integrity during bring-up. ### Diagnostic Modules - [src/kernel/e820_test.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig): Contains helper functions (`test1()`, `test2()`, `test3()`, and `testIteratorState()`) that exercise the iteration and formatting of memory regions via VGA output ([src/kernel/e820_test.zig43-102](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig#L43-L102)). It uses `printEntry()` to format base addresses, lengths, and types as hexadecimal values ([src/kernel/e820_test.zig24-39](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig#L24-L39)). - [src/kernel/debug.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/debug.zig): Bypasses `E820Store` to inspect the raw legacy bootloader physical address `PHYS_E820 = 0x0009_0000` directly ([src/kernel/debug.zig28-84](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/debug.zig#L28-L84)). It provides `dumpFirstEntries()` for low-level validation and `pause()` to halt the CPU with `hlt` instructions during faults ([src/kernel/debug.zig67-98](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/debug.zig#L67-L98)). Sources: `[src/kernel/debug.zig:1-99]`, [src/kernel/e820_test.zig1-102](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig#L1-L102) --- ## 4. Architecture and Data Flow ### Diagram: Memory Map Migration & Storage The following diagram bridges natural language subsystem roles to concrete code entities involved in capturing and exposing the E820 map. ![diagram](mermaids/3.1-e820-memory-map-diagram-1.png) Sources: `[src/kernel/boot_info.zig:1-1]`, `[src/kernel/E820Store.zig:48-75]`, `[src/kernel/E820.zig:41-64]`, `[src/kernel/debug.zig:28-84]`, [src/kernel/e820_test.zig43-102](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/e820_test.zig#L43-L102) ### Diagram: Indexed Query Data Flow This diagram illustrates the function call hierarchy when a kernel component queries a specific E820 memory entry. ```mermaid sequenceDiagram participant KernelComponent as "KernelComponent" participant E820Module as "E820.zig" participant Storage as "E820Store.zig table[]" KernelComponent->>E820Module: getEntry(index) E820Module->>E820Module: Check index < table_count E820Module-->>KernelComponent: return null E820Module->>Storage: Compute address (table_addr + index * 24) Storage-->>E820Module: Read raw extern struct bytes E820Module-->>KernelComponent: Return E820Entry by value ``` Sources: `[src/kernel/E820.zig:47-64]`, [src/kernel/E820Store.zig21-27](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/E820Store.zig#L21-L27)