Repository navigation
2.2 kmain Boot Sequence
Relevant source files
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
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 fromBOOT_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_bufferdefined in.bsssrc/kernel/kernel.zig75 is bound tofba(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

Sources: src/kernel/kernel.zig46-50src/kernel/kernel.zig71-82src/kernel/boot_info.zig23-72
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.bsssection with 16-byte alignment. Additional shell allocation is reserved viashell_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 sizeRAMDISK_SIZE(4 * 1024 * 1024bytes) src/kernel/kernel.zig91, backed byfs_ramdisk_bufsrc/kernel/kernel.zig97 and global CodaFS instancefs_globalsrc/kernel/kernel.zig100.
Title: Memory and Stack Migration Pipeline

Sources: src/kernel/kernel.zig17-45src/kernel/kernel.zig52-110
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 asisr80_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
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 globalfs_globalinstance src/kernel/kernel.zig100 is initialized here. -
Task Registration and Shell Spawn: Initial user programs are loaded via
binary_loader.zigand registered with the scheduler using structures defined inscheduler.zigandtask.zig. The shell task is specifically spawned usingshell.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
- 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