Repository navigation
2.1 Bootloader Stages and Linking
Relevant source files
This page provides full technical documentation for the CadenzaOS boot sequence and memory layout configuration. It details the transition from the 512-byte Master Boot Record (src/boot/boot.asm) through Stage 2 long-mode initialization and E820 memory map collection (src/stage2/stage2.asm), higher-half kernel linking (linker.ld), and build-time metadata injection (kernel_info.inc).
Sources: src/boot/boot.asm:1-111, src/stage2/stage2.asm:1-180, linker.ld:1-50
The Stage 1 bootloader resides in sector 0 of the storage medium and is loaded by the BIOS into physical address 0x7C00src/boot/boot.asm6-11 Its execution flow initializes a real-mode stack, validates extended BIOS disk services, loads the Stage 2 binary from disk, and transfers control.
Upon entry, segment registers (ds, es, ss) are cleared to 0x0000, and the stack pointer sp is set to 0x7C00, growing downward away from the MBR code src/boot/boot.asm18-24 The boot drive identifier passed by the BIOS in dl is stored in [boot_drive]src/boot/boot.asm28-29
Stage 1 checks for LBA support using INT 13h AH=41h with magic value 0x55AA in bxsrc/boot/boot.asm39-46 It then reads 20 sectors (10 KiB) of Stage 2 from starting LBA 1 into physical address 0x7E00 via a Disk Address Packet (dap) structure using INT 13h AH=42hsrc/boot/boot.asm56-65 Finally, execution jumps to 0x7E00src/boot/boot.asm70
Title: Stage 1 Boot Execution Flow
Sources: src/boot/boot.asm:11-111
The Stage 2 bootloader executes between memory addresses 0x7E00 and 0xA5FFsrc/stage2/stage2.asm6 Its primary responsibilities are:
- Initializing serial logging (
serial_init16) src/stage2/stage2.asm81-104 - Transitioning through Unreal Mode to read memory above 1 MiB while in 16-bit mode src/stage2/stage2.asm153-174
- Constructing early page tables for identity and higher-half mapping src/stage2/stage2.asm37-43
- Collecting physical memory layout via BIOS E820 routines into
E820_BUF(0x9000) src/stage2/stage2.asm9 andMMAP_COUNT(0x8FF8) src/stage2/stage2.asm27 - Loading the kernel binary from disk into physical address
0x00100000(1 MiB) src/stage2/stage2.asm30 - Activating Long Mode and jumping to the higher-half kernel entry point.
To load the kernel image without fully entering protected mode immediately, Stage 2 temporarily enables Protected Mode (PE=1 in cr0), loads flat 4 GiB descriptors into data segment registers (ds, es, fs, gs), and returns to Real Mode (PE=0) src/stage2/stage2.asm153-174 This permits 32-bit addressing bounds while retaining 16-bit instruction execution.
Stage 2 allocates early page tables in physical memory between 0x1000 and 0x5000src/stage2/stage2.asm7:
-
PML4_ADDR(0x1000): Page Map Level 4 Table Base src/stage2/stage2.asm38 -
PDPT_ADDR(0x2000): Page Directory Pointer Table for Identity Mapping src/stage2/stage2.asm39 -
PD_ADDR(0x3000): Page Directory Table using 2 MiB Huge Pages src/stage2/stage2.asm40 -
PDPT_KERNEL_ADDR(0x4000): Higher-Half Kernel Page Directory Pointer Table src/stage2/stage2.asm41 -
PT_KERNEL_ADDR(0x5000): Fine-grained 4 KiB Kernel Page Table src/stage2/stage2.asm42
Title: Stage 2 Page Table Hierarchy
Sources: src/stage2/stage2.asm:6-180
The kernel linker script (linker.ld) enforces a higher-half virtual memory layout. All kernel code and data are linked to execute at virtual addresses starting at 0xFFFFFF8000000000 (KERNEL_OFFSET), while being physically loaded at 0x00100000 (KERNEL_PHYS, 1 MiB) linker.ld3-6
The linker script defines two loadable program headers (PHDRS) linker.ld12-18:
-
:text(PT_LOAD, flags read/write/execute7) linker.ld15: Contains.text,.rodata, and.datasections linker.ld26-37 -
:bss(PT_LOAD, flags read/write6) linker.ld17: Contains uninitialized data (.bssandCOMMON), mapped separately to avoid inflating the on-disk binary size linker.ld39-43
Title: Linker Layout and Higher-Half Virtual Mapping
Sources: linker.ld:1-50
During the build pipeline, metadata is extracted from the compiled ELF kernel and written to build/kernel_info.inc. This file is directly included by stage2.asm to synchronize build parameters with assembly-level constants src/stage2/stage2.asm31
Generated variables include:
-
KERNEL_SECTORS: Total sector count of the kernel binary. -
KERNEL_ENTRY: Resolved virtual entry point address (kernel_entry). -
BSS_PHYS: Physical address of the.bsssection. -
BSS_SIZE_DWORDS: Size of the.bsssection in 32-bit dwords for zero-initialization.
Title: Build Pipeline Metadata Generation Flow
Sources: src/stage2/stage2.asm:24-36
- 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