Skip to content

3.1 E820 Memory Map

Glen edited this page Nov 14, 2026 · 2 revisions

E820 Memory Map

Relevant source files

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), 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).


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 immediately copies up to MAX_E820_ENTRIES (64) into a static kernel-owned buffer during early initialization (src/kernel/E820Store.zig21-27).

Initialization Flow

  1. init() reads the boot information via bi.get() to retrieve e820_addr and e820_count (src/kernel/E820Store.zig55-57).
  2. The entry count is clamped against MAX_E820_ENTRIES (src/kernel/E820Store.zig59-63).
  3. Entries are copied iteratively from the physical source address into the static table array (src/kernel/E820Store.zig67-73).
  4. getTableAddr() converts the virtual address of the copied table back to a physical address using mem.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


2. Kernel-Owned Table Representation (E820.zig)

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).

Data Structures and Functions

Sources: src/kernel/E820.zig1-63


3. Diagnostics and Testing (e820_test.zig & debug.zig)

CadenzaOS provides diagnostic tools to verify memory map integrity during bring-up.

Diagnostic Modules

Sources: [src/kernel/debug.zig:1-99], src/kernel/e820_test.zig1-102


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

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

Diagram: Indexed Query Data Flow

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
Loading

Sources: [src/kernel/E820.zig:47-64], src/kernel/E820Store.zig21-27

Clone this wiki locally