Skip to content

Latest commit

 

History

History
188 lines (139 loc) · 12.5 KB

reference.md

File metadata and controls

188 lines (139 loc) · 12.5 KB

Milo Features, Repository Layout & Build Reference

This document preserves the full subsystem feature overview, directory layout, toolchain/host prerequisites, build/test commands, interactive shell commands, and roadmap details for milo.

Features

  • AArch64 Reference Target & Portable HAL (arch/, kernel/hal/, kernel/core/):

    • Exception entry (_start), VBAR_EL1 vector table dispatch, FP/SIMD enablement, and kernel/user context switching (cpu_switch_context, enter_user_mode, enter_user_context, return_to_kernel, kernel_thread_trampoline).
    • 4-level AArch64 MMU page tables (TTBR0_EL1 / TTBR1_EL1) with hybrid 2 MiB block / 4 KiB page RAM mapping, W^X and PXN/UXN enforcement, lazy demand-zero paging, and Copy-on-Write (CoW) page fault resolution.
    • FDT/DTB memory region, /memreserve/, linux,initrd-*, and virtio,mmio discovery, GICv2 interrupt controller (gicv2), ARM Generic Timer (100 Hz preemptive tick), PL011 MMIO UART console driver, SplitVirtqueue, VirtioMmioTransport, and virtio_blk block driver.
    • Freestanding trait helpers (Kernel::Traits::is_bool / is_bool_v).
  • Freestanding C++20 & Synchronization (kernel/runtime/, kernel/sync/):

    • Compiled with -std=c++20 -ffreestanding -fno-exceptions -fno-rtti and strict warning/static-analysis gates (clang-format, clang-tidy).
    • Kernel::Sync::IrqGuard: zero-overhead RAII interrupt-masking guard.
  • Memory Management (kernel/mm/):

    • MM::Page & MM::PageFrameAllocator: FDT-driven physical memory discovery (with /memreserve/ carveouts and discontiguous bank gaps) and atomic reference counting (page_get / page_put) for CoW sharing.
    • MM::KernelHeap: 8-class slab allocator (16 B - 2048 B) paired with a compound multi-page allocator backing kmalloc/kzalloc/krealloc/kfree and operator new/delete.
    • MM::AddressSpace: isolated user page tables, clone(), map_demand_zero(), and validated copy_from_user/copy_to_user transfers.
  • Processes, Scheduling & Signals (kernel/process/):

    • Encapsulated Process class with preemptive round-robin Scheduler and intrusive WaitQueues.
    • FdTable & FileDescriptor: RAII file descriptor table with geometric growth bounded by RLIMIT_NOFILE, RLIMIT_NPROC dynamic scaling, and O_CLOEXEC support.
    • SignalTable: per-process signal masks, SigAction handlers, unmaskable SIGKILL/SIGSTOP invariant enforcement, and EL0 rt_sigframe delivery/unwinding (rt_sigreturn).
    • UserStackBuilder: System V AArch64 ABI initial stack layout (argc, argv, envp, auxv including AT_RANDOM).
  • Linux AArch64 ABI & Syscall Layer (kernel/uapi/, kernel/syscalls/, kernel/time/):

    • Dedicated C-compatible kernel/uapi/ headers verified at compile time with sizeof, offsetof, std::is_trivial_v, and std::is_standard_layout_v static_asserts.
    • Time::Duration domain wrapper isolating internal timer/nanosecond logic from linux_timespec/linux_timeval.
    • Modular syscall handlers covering process lifecycle (clone, execve with #! shebangs, wait4, exit/exit_group, job control, prlimit64), memory (brk, mmap, munmap, mprotect), VFS/IO (openat, read, write, writev, pipe2, dup/dup3, fcntl, ioctl, getdents64, ppoll, pselect6, ftruncate/truncate, utimensat, fsync/fdatasync, fchmod/fchmodat, fchown/fchownat, newfstatat/fstat, faccessat/faccessat2, readlinkat, mkdirat, unlinkat, renameat/renameat2, getcwd, chdir, lseek, close), sockets (socket, socketpair, bind, listen, accept/accept4, connect, getsockname, getpeername, sendto, recvfrom, setsockopt, getsockopt, shutdown, sendmsg, recvmsg), signals (rt_sigaction, rt_sigprocmask, rt_sigreturn, kill), and time/system info (clock_gettime, nanosleep, times, getrusage, sysinfo, getrandom, uname).
  • VFS, IPC, ELF Loader & Drivers (kernel/fs/, kernel/ipc/, kernel/loader/, kernel/drivers/):

    • Virtual File System with rootfs (CPIO newc archive loading), tmpfs (page-backed writable filesystem), ext2 (block-backed filesystem with direct, singly indirect, and doubly indirect blocks, directory entry creation/coalescing, and fsync), and devfs (/dev/console, /dev/tty, /dev/null with termios/winsize ioctls, canonical/non-canonical line discipline, and foreground PGID job control, plus /dev/fb0 and /dev/input/event<N>).
    • Encapsulated Kernel::IPC::Pipe ring buffer with blocking/non-blocking I/O, SIGPIPE generation, and poll() support.
    • Static/PIE ELF64 binary parser and loader (Elf64View, load_elf, load_elf_interpreter) with OOM rollback and W^X segment mapping.
    • VirtIO-MMIO modern transport (VirtioMmioTransport), SplitVirtqueue, VirtioMmioBus, and drivers (virtio_blk, virtio_gpu, virtio_input, virtio_net).
  • User-Space Payloads & Dual-Target Testing (user/, tests/host/, kernel/shell/):

    • Sample user programs (user/hello.s, user/hello_musl.c, user/hello_dyn.c, user/fb_test.c, user/evdev_smoke.c, user/unix_echo_server.c, user/unix_echo_client.c, user/unix_sock_smoke.c) and optional musl, BusyBox, and GNU Bash initrd images (initrds/busybox.cpio, initrds/bash.cpio).
    • Host-native snitch unit tests (HOST_TEST) with ASan/UBSan and gcovr coverage.
    • Interactive in-kernel shell (milo>) with hardware diagnostics and EL0/syscall/scheduler/lazy-memory verification suites.

Repository Structure

.
├── arch/
│   └── aarch64-linux-gnu/       # AArch64 boot, vectors, MMU/CoW, trap handling, PL011 UART, linker script, EL0 tests
├── docs/                        # Architecture, onboarding, ADRs, and full reference docs
├── kernel/
│   ├── boot/                    # Flattened Device Tree (FDT) parser & memory/initrd/virtio discovery
│   ├── core/                    # Freestanding type traits & logging (traits.hpp, log.hpp)
│   ├── drivers/                 # In-tree hardware drivers (GICv2, PL011, fw_cfg, framebuffer/ramfb, VirtIO)
│   ├── fs/                      # VFS core, cpio, PageCache, rootfs, tmpfs, devfs (TTY/FB/evdev), and ext2
│   ├── hal/                     # Architecture-agnostic HAL traits & target backends (qemu_virt)
│   ├── input/                   # InputEvent & InputQueue
│   ├── ipc/                     # Encapsulated Pipe implementation (Pipe.hpp / Pipe.cpp)
│   ├── loader/                  # ELF64 parser & executable/interpreter loader (elf64, elf_loader)
│   ├── mm/                      # Page, PageFrameAllocator, KernelHeap, AddressSpace
│   ├── net/                     # NetworkDevice & AF_UNIX socket implementation
│   ├── process/                 # Process, FdTable, FileDescriptor, SignalTable, Scheduler, WaitQueue, StackBuilder, resource
│   ├── runtime/                 # Freestanding C++ ABI support (cxx_abi.cpp)
│   ├── shell/                   # Interactive kernel shell (milo>) & runtime test suite
│   ├── sync/                    # RAII synchronization primitives (IrqGuard.hpp)
│   ├── syscalls/                # Modular Linux AArch64 syscall handlers & dispatcher
│   ├── time/                    # Time::Duration domain wrapper
│   ├── uapi/                    # Compile-time verified Linux AArch64 UAPI structs & constants
│   ├── kernel.mk                # Kernel build rules
│   └── main.cpp                 # Kernel entry point (kmain)
├── scripts/                     # Build, quality report, license check, LOC, and dump helpers
├── tests/
│   └── host/                    # Host-compiled snitch unit tests, HAL mocks & .clang-tidy
├── user/                        # Sample EL0 user programs
├── LICENSE                      # GNU General Public License v3.0
└── Makefile                     # Top-level build, QEMU runner, initrd packaging, host tests, and quality gates

Prerequisites & Toolchain

To build, test, and run Milo locally:

  • Cross Compiler: aarch64-linux-gnu-g++, aarch64-linux-gnu-as, aarch64-linux-gnu-ld, aarch64-linux-gnu-objcopy
  • Host Compiler (for HOST_TEST): g++ (C++20) with snitch and gcovr
  • Emulator: qemu-system-aarch64
  • Quality Tools (Optional): clang-format, clang-tidy
  • Debugger (Optional): gdb-multiarch

The aarch64-linux-gnu triplet is used to match the Linux AArch64 user-space ABI/syscall convention while compiling bare-metal freestanding code.

Devcontainer & Linux Host Prerequisites

The .devcontainer/devcontainer.json configuration provisions the cross-toolchain, QEMU, snitch, gcovr, and lint tools automatically.

To allow namespace creation inside the container without --privileged or CAP_SYS_ADMIN, runArgs configures:

  • --security-opt=seccomp=unconfined — permits clone/unshare namespace syscalls blocked by Docker's default seccomp profile.
  • --security-opt=apparmor=unconfined — permits mount-namespace setup blocked by Docker's docker-default AppArmor profile (and required on Ubuntu 24.04+ / Pop!_OS 24.04 hosts with AppArmor userns restrictions).

Linux host prerequisites:

  • Ensure the host kernel allows unprivileged user namespaces (sysctl user.max_user_namespaces > 0, and sysctl kernel.unprivileged_userns_clone=1 on distributions that expose it).
  • On Ubuntu 24.04+ hosts with kernel.apparmor_restrict_unprivileged_userns=1, keep --security-opt=apparmor=unconfined enabled in runArgs (and rebuild/reopen the devcontainer after changing devcontainer.json).

Building, Testing & Running

Build All Targets (Kernel + Initrd)

make all

Launch in QEMU

Boots build/kernel.bin (with initrds/busybox.cpio and disk.img) on QEMU virt:

make run

To exit QEMU, press Ctrl+A then X.

Host Unit Tests & Coverage

make test-host

Optional Initrd Images (musl, BusyBox, Bash)

make setup-musl
make setup-busybox
make setup-bash

Code Formatting, License & Static Analysis

make format        # Format codebase with clang-format
make license-check # Verify SPDX and copyright headers
make quality       # Verify formatting, license headers, clang-tidy, and host tests

Debugging with GDB

  1. Launch QEMU listening for GDB on port 1234:
make debug
  1. Attach GDB in another terminal:
make gdb

Clean Build Artifacts

make clean

Interactive Kernel Shell (milo>)

Once booted, the milo> shell provides built-in diagnostics and verification commands, including:

  • help / info / clear / halt — System info (EL, MMU/cache status, PFA frames), terminal control, and CPU halt (wfi).
  • ls initrd / cat initrd <path> / elf initrd <path> — Inspect CPIO initrd entries, file contents, and ELF64 headers/segments.
  • sh / bash — Launch an interactive /bin/sh or /bin/bash session from the initrd.
  • Subsystem & EL0 verification commands (test cpu, test mmu, test el0, test cpp, test svc, test abort, test pfa, test ro, test nx, test sys, test cpio, test elf, test elf_interp, test dyn_exec, test vmm, test vma_mmap, test file_vma, test page_cache, test loader, test user, test user_echo, test uxn_fault, test syscall, test tty_echo, test user_mm, test proc_exit, test fork_exec, test musl, test sched, test heap, test dynamic_io, test refcount, test cow, test demand_paging, test lazy_mm, test virtio, test gpu, test fb, test input, test net, test af_unix, test unix_sock, test blk, test stack_abi).
  • test all — Run the full integrated in-kernel verification suite.

Roadmap

  • Decoupled Driver Development Kit (DDK): Introduce public include/ddk/ interfaces (Driver, PlatformServices, and RAII MmioRegion/DmaBuffer/IrqHandle) and a top-level drivers/ tree isolated from private kernel/ headers, with out-of-tree build support.
  • Additional Drivers & Storage: Migrate virtio_blk to the DDK, expand persistent filesystem support (ext2), and add additional peripheral/networking drivers.
  • Multi-Architecture Expansion: Implement HAL backends for additional targets (e.g., x86_64, riscv64) under arch/.

License

Distributed under the GNU General Public License v3.0 (GPL-3.0). See LICENSE for details.