# kmain Boot Sequence Relevant source files - [src/kernel/kernel.zig](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig) ## 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-192](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L1-L192)[src/kernel/boot_info.zig1-72](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/boot_info.zig#L1-L72) ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/boot_info.zig#L23-L72) - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L46-L50). - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L47-L47). - **Bootstrap Heap**: A static memory buffer `heap_buffer` defined in `.bss`[src/kernel/kernel.zig75](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L75-L75) is bound to `fba` (`std.heap.FixedBufferAllocator`) [src/kernel/kernel.zig78](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L78-L78) to supply dynamic allocations prior to full physical memory and paging management. Title: Kernel Entry and Initialization Flow ![diagram](mermaids/2.2-kmain-boot-sequence-diagram-1.png) Sources: [src/kernel/kernel.zig46-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L46-L50)[src/kernel/kernel.zig71-82](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L71-L82)[src/kernel/boot_info.zig23-72](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/boot_info.zig#L23-L72) ## 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_stack`[src/kernel/kernel.zig109](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L109-L109), a 16 KiB dedicated stack allocated in the `.bss` section with 16-byte alignment. Additional shell allocation is reserved via `shell_stack_buf`[src/kernel/kernel.zig115](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L115-L115). - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L88-L88) maps the initial storage block of size `RAMDISK_SIZE` (`4 * 1024 * 1024` bytes) [src/kernel/kernel.zig91](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L91-L91), backed by `fs_ramdisk_buf`[src/kernel/kernel.zig97](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L97-L97) and global CodaFS instance `fs_global`[src/kernel/kernel.zig100](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L100-L100). Title: Memory and Stack Migration Pipeline ![diagram](mermaids/2.2-kmain-boot-sequence-diagram-2.png) Sources: [src/kernel/kernel.zig17-45](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L17-L45)[src/kernel/kernel.zig52-110](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L52-L110) ## 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_stub`[src/kernel/kernel.zig118](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L118-L118) for system calls and hardware IRQ stubs (`irq0_stub`, `irq1_stub`, `irq12_stub`) [src/kernel/kernel.zig58-60](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L58-L60). - **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-30](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L29-L30)[src/kernel/kernel.zig48](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L48-L48). - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L50-L50). Sources: [src/kernel/kernel.zig27-50](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L27-L50)[src/kernel/kernel.zig58-60](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L58-L60)[src/kernel/kernel.zig118](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L118-L118) ## 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L100-L100) 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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L33-L33). - **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](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L41-L41). Sources: [src/kernel/kernel.zig33-43](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L33-L43)[src/kernel/kernel.zig99-100](https://github.com/GlenMeehan/CadenzaOS/blob/8f182160/src/kernel/kernel.zig#L99-L100)