Repository navigation
3.1 E820 Memory Map
Relevant source files
- src/kernel/E820.zig
- src/kernel/E820Store.zig
- src/kernel/bitmap.zig
- src/kernel/debug.zig
- src/kernel/e820_test.zig
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), read-only indexed queries (src/kernel/E820.zig1-63), and bring-up diagnostics (src/kernel/e820_test.zig1-102 and src/kernel/debug.zig1-98).
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 immediately copies up to MAX_E820_ENTRIES (64) into a static kernel-owned buffer during early initialization (src/kernel/E820Store.zig21-27).
-
init()reads the boot information viabi.get()to retrievee820_addrande820_count(src/kernel/E820Store.zig55-57). - The entry count is clamped against
MAX_E820_ENTRIES(src/kernel/E820Store.zig59-63). - Entries are copied iteratively from the physical source address into the static
tablearray (src/kernel/E820Store.zig67-73). -
getTableAddr()converts the virtual address of the copied table back to a physical address usingmem.virtToPhys()(src/kernel/E820Store.zig34-37), allowing the kernel to register it with higher-level components.getTableCount()exposes the active count (src/kernel/E820Store.zig39-42).
Sources: src/kernel/E820Store.zig1-74
Once src/kernel/E820Store.zig populates the static buffer, 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).
-
E820Entry: Anextern structmatching the standard 24-byte layout: -
base: Physical start address (u64) src/kernel/E820.zig17. -
length: Size of the region in bytes (u64) src/kernel/E820.zig18. -
entry_type: Region classification (e.g.,1for usable RAM) (u32) src/kernel/E820.zig19. -
acpi: Extended ACPI attributes (u32) src/kernel/E820.zig20. -
setTable(addr: usize, count: usize): Registers the safe table address and entry count (src/kernel/E820.zig41-44). -
getCount(): Returns the number of valid entries (src/kernel/E820.zig47-49). -
getEntry(index: usize) ?E820Entry: Performs bounds-checking againsttable_countand computes the exact offset to return anE820Entrycopy (src/kernel/E820.zig57-63).
Sources: src/kernel/E820.zig1-63
CadenzaOS provides diagnostic tools to verify memory map integrity during bring-up.
-
src/kernel/e820_test.zig: Contains helper functions (
test1(),test2(),test3(), andtestIteratorState()) that exercise the iteration and formatting of memory regions via VGA output (src/kernel/e820_test.zig43-102). It usesprintEntry()to format base addresses, lengths, and types as hexadecimal values (src/kernel/e820_test.zig24-39). -
src/kernel/debug.zig: Bypasses
E820Storeto inspect the raw legacy bootloader physical addressPHYS_E820 = 0x0009_0000directly (src/kernel/debug.zig28-84). It providesdumpFirstEntries()for low-level validation andpause()to halt the CPU withhltinstructions during faults (src/kernel/debug.zig67-98).
Sources: [src/kernel/debug.zig:1-99], src/kernel/e820_test.zig1-102
The following diagram bridges natural language subsystem roles to concrete code entities involved in capturing and exposing the E820 map.

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
This diagram illustrates the function call hierarchy when a kernel component queries a specific E820 memory entry.
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
- Overview
- Getting Started and Build System
- Kernel Configuration and Global State
- Project Governance and Contributing
- Boot and Early Kernel Initialization
- Bootloader Stages and Linking
- kmain Boot Sequence
- Splash Screen and Branding
- Interrupts, Scheduling, and Syscalls
- IDT, PIC, APIC and IRQ Stubs
- Scheduler and Task Lifecycle
- System Call Interface
- Demonstration Tasks
- CodaFS Filesystem and Storage
- On-Disk Format and Operations
- Space Manager and Block Devices
- Binary Format and Program Loading
- Shell, Terminal, and Display
- Shell Command Pipeline
- Terminal, Input Devices and Prompts
- Display: VGA, Framebuffer, Console and Serial