milo

02 — Boot and Initialization

This guide traces how Milo boots on AArch64 (qemu-system-aarch64 -M virt), initializes hardware and freestanding kernel subsystems in kmain, mounts the initial filesystems, and launches user-space (EL0) shells and binaries.

Boot Flow

1. Linker Image Layout (arch/aarch64-linux-gnu/linker.ld)

The linker script places the kernel entry point at physical/virtual load address 0x40080000 (RAM_BASE 0x40000000 + 512 KiB, leaving room for QEMU bootloader/DTB placement) and defines three PT_LOAD segments:

Note ([WIP] / Linker vs. Loader): .bss is reserved in linker.ld between __bss_start and __bss_end, while boot.s does not run an explicit zero-loop over __bss_start..__bss_end prior to kmain (relying on the loader/image state).

2. Early Assembly Entry (arch/aarch64-linux-gnu/boot.s)

When QEMU enters _start at 0x40080000 with the DTB physical address in x0:

  1. Enable FP/SIMD: Reads cpacr_el1, sets FPEN = 0b11 (#(3 << 20)), and executes isb so floating-point/SIMD instructions do not trap in EL1 or EL0.
  2. Install Vector Table: Loads vector_table into vbar_el1 and executes isb.
  3. Set Temporary Boot Stack: Sets sp to boot_stack_top (top of the 16384-byte .bss stack boot_stack_bottom..boot_stack_top).
  4. Call kmain: Invokes bl kmain with x0 (dtb_paddr) intact. If kmain ever returns, _start loops on wfe.

3. kmain Bring-Up Sequence (kernel/main.cpp)

kmain(uintptr_t dtb_paddr) records g_dtb_paddr = dtb_paddr and brings up the kernel in eight stages:

  1. Early Console & Static Constructors: Calls HAL::get_console().init() (initializing the Pl011Uart instance at 0x09000000 with FIFOs enabled and interrupts masked) and calls run_init_array() across __init_array_start..__init_array_end.
  2. Pre-MMU FDT Discovery (kernel/boot/fdt.cpp): Validates the DTB header via Kernel::Boot::validate_header(dtb_paddr, &dtb_size), collects usable RAM regions via find_memory_regions(dtb_paddr), reads /memreserve/ spans via find_memory_reservations(dtb_paddr), and locates the initrd via find_initrd(dtb_paddr) (stored in g_initrd_loc). If no valid DTB is present, find_memory_regions falls back to HAL::get_platform_info().ram_base with 256 MiB (0x10000000).
  3. Platform Memory Update: Computes discovered_ram_span = mem_map.max_paddr - mem_map.min_paddr and calls HAL::set_platform_memory(mem_map.min_paddr, discovered_ram_span).
  4. Pre-MMU Page Frame Allocator Init (kernel/mm/pfa.cpp): Calls Kernel::MM::pfa_init(...) with [ _text_start, __bss_end ), DTB span, initrd span, and reserve_map, carving out the Page metadata array in usable RAM and marking non-bank gaps and reserved spans as Reserved.
  5. MMU Activation & Higher-Half Rebasing (arch/aarch64-linux-gnu/mmu_aarch64.cpp): Calls HAL::get_mmu().activate(), which maps GICv2 (0x08000000, 0x08010000) and UART0 (0x09000000) as Device memory, maps .text (RX), .rodata (R), and .data/.bss (RW) at 4 KiB granularity in both TTBR0_EL1 and TTBR1_EL1 (+ 0xFFFF800000000000), maps remaining physical RAM outside the kernel using 2 MiB blocks / 4 KiB pages, and enables the MMU/caches. kmain then calls HAL::console_on_mmu_enabled() and Kernel::MM::pfa_on_mmu_enabled() to rebase MMIO and Page array pointers into the higher half.
  6. Heap & Core VFS Mounts: Calls Kernel::MM::heap_init(), then mounts rootfs at / via Kernel::FS::rootfs_mount_from_initrd(g_initrd_loc), devfs at /dev via Kernel::FS::devfs_init_and_mount(), and tmpfs at /tmp via Kernel::FS::tmpfs_init_and_mount().
  7. GICv2, Timer, Scheduler & Device Bring-Up:
    • Calls HAL::Platform::init_irq_controller() (GicV2::init(0x08000000, 0x08010000)), HAL::Platform::init_periodic_timer(100U), HAL::Platform::enable_timer_irq(), and Kernel::Scheduler::init().
    • Enumerates VirtIO-MMIO via VirtioMmioBus::instance().discover(dtb_paddr) (using FDT virtio,mmio nodes or falling back to 32 slots at 0x0A000000, stride 0x200, SPIs 48..79).
    • Initializes network (HAL::Platform::init_network_devices(dtb_paddr)), input (HAL::Platform::init_input_devices(dtb_paddr), registering /dev/input/event<N>), and GPU (HAL::Platform::init_gpu_devices(dtb_paddr)), clearing/drawing a banner and flushing scanout if a GPU is bound.
    • Probes fw_cfg (find_fw_cfg_device(dtb_paddr) or default 0x09020000, size 0x18) and, if no primary framebuffer exists yet and etc/ramfb is present with DMA support, initializes RamfbDevice and sets it as primary framebuffer.
    • Calls Kernel::FS::devfs_register_fb0() to expose /dev/fb0 when a primary framebuffer is initialized.
    • Calls HAL::Platform::init_block_devices(dtb_paddr), attempts Kernel::FS::ext2_mount(blk, "/mnt/disk", nullptr) if a primary block device is present, and runs verify_primary_block_device() (writing and reading back a 512-byte pattern on LBA 0).
  8. Shell Handoff: Disables interrupts (HAL::get_cpu().disable_interrupts()) and enters Kernel::Shell::run().

First User-Space Process (init / Shells & EL0 Binaries)

Milo does not hardcode a single /sbin/init daemon at boot; instead, after Kernel::Shell::run() starts:

Boot Sequence Diagram

sequenceDiagram
    autonumber
    participant Boot as QEMU / Bootloader
    participant Asm as boot.s (_start)
    participant KMain as kmain (kernel/main.cpp)
    participant FDT as fdt.cpp & PFA (pfa.cpp)
    participant MMU as mmu_aarch64.cpp & Heap
    participant VFS as rootfs / devfs / tmpfs / ext2
    participant Subsys as GICv2 / Timer / Sched / VirtIO
    participant Shell as Shell (shell.cpp) & EL0

    Boot->>Asm: Jump to 0x40080000 (x0 = dtb_paddr)
    Asm->>Asm: Enable CPACR_EL1 FP/SIMD, set VBAR_EL1, set sp = boot_stack_top
    Asm->>KMain: bl kmain(x0)
    KMain->>KMain: init PL011 console & run_init_array()
    KMain->>FDT: validate_header, find_memory_regions, find_memory_reservations, find_initrd
    KMain->>FDT: pfa_init(mem_map, kernel, dtb, initrd, reserve_map)
    KMain->>MMU: HAL::get_mmu().activate() (TTBR0/TTBR1, W^X, 2MiB/4KiB RAM)
    KMain->>FDT: console_on_mmu_enabled() & pfa_on_mmu_enabled()
    KMain->>MMU: heap_init()
    KMain->>VFS: rootfs_mount_from_initrd(/), devfs_init_and_mount(/dev), tmpfs_init_and_mount(/tmp)
    KMain->>Subsys: init_irq_controller(), init_periodic_timer(100Hz), enable_timer_irq(), Scheduler::init()
    KMain->>Subsys: VirtioMmioBus::discover(), net, input, gpu, fw_cfg/ramfb, devfs_register_fb0(), blk/ext2
    KMain->>Shell: disable_interrupts() & Kernel::Shell::run()
    Shell->>Shell: run_lazy_memory_acceptance_suite() & print milo> prompt
    Shell-->>Boot: Optional EL0 /bin/sh or /bin/bash via load_elf + Process::execute()