milo

07 — Contributing and Debugging

This guide covers debugging Milo in QEMU and GDB, interpreting faults and panics, following repository commit and licensing rules, and adding new syscalls, drivers, or host unit tests.

Debugging Workflow

Attaching GDB to QEMU (Makefile, arch/aarch64-linux-gnu/boot.s, arch/aarch64-linux-gnu/trap_aarch64.cpp)

  1. Start QEMU paused at the entry point with GDB stub listening on TCP port 1234:

    make debug
    

    This invokes $(QEMU) $(QEMUFLAGS) -s -S (qemu-system-aarch64 -M virt -cpu cortex-a57 -m 256M -nographic -kernel build/kernel.bin ... -s -S). Note that debug does not list $(DISK_IMG) as a prerequisite, so run make disk beforehand if you need disk.img mounted.

  2. In a second terminal, launch gdb-multiarch against the symbol-rich ELF binary:

    make gdb
    

    This runs $(GDB) build/kernel.elf -ex "target remote localhost:1234" -ex "layout split".

  3. Useful breakpoints and register checks during bring-up or fault triage:

    • Break at _start (0x40080000) or kmain to inspect pre-MMU state (x0 = DTB physical address, vbar_el1, cpacr_el1).
    • Inspect translation tables after MMU activation by reading ttbr0_el1 / ttbr1_el1 (p/x $TTBR0_EL1, p/x $TTBR1_EL1) or dumping g_ttbr0_l0 / g_ttbr1_l0 (x/8gx &g_ttbr0_l0, x/8gx &g_ttbr1_l0) and walking 64-bit descriptors ((desc & 0x000FFFFFFFFFF000)).
    • Break at aarch64_handle_sync_exception or aarch64_handle_irq_exception (arch/aarch64-linux-gnu/trap_aarch64.cpp) to inspect esr_el1, far_el1, elr_el1, spsr_el1, and the saved AArch64CpuContext frame.
    • Break at Kernel::Scheduler::schedule (kernel/process/scheduler.cpp) or Kernel::Syscalls::SyscallDispatcher::handle_trap (kernel/syscalls/SyscallDispatcher.cpp) to trace task switches and svc #0 calls.

Panics, Faults & Verbose Logging (kernel/core/log.{hpp,cpp}, arch/aarch64-linux-gnu/trap_aarch64.cpp, kernel/mm/heap.cpp)

Commits, Pre-Commit Hook & License Headers (.githooks/pre-commit, scripts/check_license_headers.py, Makefile)

Adding a New Syscall, Driver, or Host Test

Adding a Linux AArch64 Syscall

  1. Define UAPI constants/structs if needed (kernel/uapi/*.hpp): Use extern "C" standard-layout, trivial structs with static_asserts on sizeof and offsetof (and #undef colliding macros under #ifdef HOST_TEST).
  2. Implement the syscall handler (kernel/syscalls/SyscallInternal.hpp and FileSyscalls.cpp / MemorySyscalls.cpp / ProcessSyscalls.cpp / SignalSyscalls.cpp / SocketSyscalls.cpp / TimeSyscalls.cpp): Validate user pointers/ranges (copy_from_user, copy_to_user, validate_user_read_range, validate_user_write_range, copy_user_cstring) and return negative ERR_* codes on failure.
  3. Register the syscall number in kernel/syscalls/SyscallDispatcher.cpp: Add constexpr uint64_t SYS_<NAME> = ...; and its case arm in SyscallDispatcher::handle_trap.
  4. Add tests & refresh compile_commands.json: Add a snitch test in tests/host/ (and/or an EL0 test in arch/aarch64-linux-gnu/user_test.s + kernel/shell/tests/), run make compile_commands.json, and run make quality.

Adding a Hardware / VirtIO Driver

  1. Implement the driver (kernel/drivers/... or kernel/input/): Follow freestanding C++20 rules (no exceptions/RTTI, IrqGuard around shared/IRQ state, phys_to_virt / virt_to_phys for MMIO and PFA DMA frames, flush_dcache_range around DMA buffers, and GicV2::register_spi_handler / unregister_spi_handler for SPIs).
  2. Wire into platform & boot (kernel/hal/targets/qemu_virt/platform_*.cpp, kernel/fs/devfs/devfs.cpp, kernel/main.cpp): Bind via VirtioMmioBus::instance().bind_device / bind_all (or FDT helpers in kernel/boot/fdt.cpp), expose VFS/devfs interfaces where appropriate, and call initialization during kmain bring-up.
  3. Register build sources: Add .cpp files to KERNEL_CORE_SRCS in kernel/kernel.mk if the driver runs in HOST_TEST (automatically mirrored into HOST_KERNEL_SRCS), or to KERNEL_TARGET_SRCS if it is bare-metal/target-only.

Adding a Host Unit Test (docs/adr/0001-unit-testing-framework.md, Makefile)

  1. Create tests/host/<feature>_test.cpp: Include <snitch/snitch.hpp> (linked against the pre-installed -L/usr/local/lib -lsnitch library with -DSNITCH_WITH_EXCEPTIONS=0), use HostTest::reset_host_hal() to reset simulated RAM/roots/mappings/console, and assert that pfa_free_pages_count() and heap stats return to baseline after teardown.
  2. Verify auto-discovery: Makefile automatically discovers tests/host/*.cpp (and tests/host/mocks/*.cpp) via HOST_TEST_CASE_SRCS / HOST_ONLY_TEST_SRCS, wiring new tests into $(HOST_TEST_BIN), $(HOST_COV_BIN), TEST_FORMAT_SRCS, and compile_commands.json without manual edits.
  3. Format, lint & run make quality: Run make compile_commands.json, make format, and make -j"$(nproc)" quality.