Skip to content

2.2 kmain Boot Sequence

Glen edited this page Nov 14, 2026 · 2 revisions

kmain Boot Sequence

Relevant source files

Purpose and Scope

This page details the phase-by-phase execution flow of the CadenzaOS kernel from long-mode handoff up to scheduler handoff. It covers bootstrap logging over serial, VESA framebuffer configuration, static heap initialization (fba), stack migration to kmain_stack, memory reservation, APIC/IDT/TSS setup, CodaFS recovery, and task spawning.

Sources: src/kernel/kernel.zig1-192src/kernel/boot_info.zig1-72

Kernel Entry and Early Diagnostics

When the bootloader transitions the system to 64-bit long mode, control reaches the kernel entry stub, which initializes foundational hardware communication channels before executing high-level Zig logic in kmain.

  • BootInfo Ingestion: The kernel reads early configuration structures from boot_info.get(), which maps layout fields from BOOT_INFO_ADDR (0x7000) describing physical memory boundaries, stack locations, and framebuffer properties src/kernel/boot_info.zig23-72
  • Serial Porting: Early logging is directed to the COM1 serial interface via serial.init() driver routines to ensure debug output is captured in headless emulation src/kernel/kernel.zig46-50.
  • Framebuffer Initialization: Video output transitions from legacy VGA text mode to the VESA frame buffer configured during stage 2 boot via framebuffer.init()src/kernel/kernel.zig47.
  • Bootstrap Heap: A static memory buffer heap_buffer defined in .bsssrc/kernel/kernel.zig75 is bound to fba (std.heap.FixedBufferAllocator) src/kernel/kernel.zig78 to supply dynamic allocations prior to full physical memory and paging management.

Title: Kernel Entry and Initialization Flow

diagram

Sources: src/kernel/kernel.zig46-50src/kernel/kernel.zig71-82src/kernel/boot_info.zig23-72

Stack Migration and Memory Configuration

To ensure isolation from bootstrap bootloader structures, the kernel migrates execution to a dedicated permanent stack and establishes memory management subsystems.

  • Stack Switch: The execution context switches from the temporary boot stack to kmain_stacksrc/kernel/kernel.zig109, a 16 KiB dedicated stack allocated in the .bss section with 16-byte alignment. Additional shell allocation is reserved via shell_stack_bufsrc/kernel/kernel.zig115.
  • Memory Maps & Allocators: The kernel parses E820 memory map structures (E820.zig, E820Store.zig), initializes the physical frame allocator (frame_allocator.zig), and configures higher-half page tables (memory.zig, page_allocator.zig).
  • RAM Disk and Filesystem Buffers: The fixed virtual address RAMDISK_VIRT_ADDR (0xFFFFFF8002000000) src/kernel/kernel.zig88 maps the initial storage block of size RAMDISK_SIZE (4 * 1024 * 1024 bytes) src/kernel/kernel.zig91, backed by fs_ramdisk_bufsrc/kernel/kernel.zig97 and global CodaFS instance fs_globalsrc/kernel/kernel.zig100.

Title: Memory and Stack Migration Pipeline

diagram

Sources: src/kernel/kernel.zig17-45src/kernel/kernel.zig52-110

Interrupts, APIC, IDT, and TSS Setup

CadenzaOS configures x86-64 interrupt handling infrastructure to transition from synchronous execution to preemptive multi-tasking.

  • IDT Configuration: idt.init() populates Interrupt Descriptor Table gates, registering exception handlers and interrupt service routines such as isr80_stubsrc/kernel/kernel.zig118 for system calls and hardware IRQ stubs (irq0_stub, irq1_stub, irq12_stub) src/kernel/kernel.zig58-60.
  • PIC & APIC: The legacy Programmable Interrupt Controller is remapped or disabled in favor of the Local APIC and IOAPIC configuration (apic.init(), pic.init()), configuring timer interrupts for preemption src/kernel/kernel.zig29-30src/kernel/kernel.zig48.
  • Task State Segment: tss.init() establishes the Task State Segment (TSS) to handle privilege-level stack switches securely during interrupts and exceptions src/kernel/kernel.zig50.

Sources: src/kernel/kernel.zig27-50src/kernel/kernel.zig58-60src/kernel/kernel.zig118

Filesystem Recovery and Scheduler Handoff

The final phases of kmain restore persistent storage state, load embedded user applications, and start the scheduler.

  • Filesystem Mount: The kernel mounts CodaFS over block devices (such as ATA drivers or RAM disks), resolving metadata blocks and recovering journaling or extent allocations via coda_fs. The global fs_global instance src/kernel/kernel.zig100 is initialized here.
  • Task Registration and Shell Spawn: Initial user programs are loaded via binary_loader.zig and registered with the scheduler using structures defined in scheduler.zig and task.zig. The shell task is specifically spawned using shell.spawn()src/kernel/kernel.zig33.
  • Scheduler Handoff: scheduler.start() enables interrupts and yields execution to the first active task, transferring control away from the bootstrap boot sequence into normal multitasking operation src/kernel/kernel.zig41.

Sources: src/kernel/kernel.zig33-43src/kernel/kernel.zig99-100

Clone this wiki locally