Milo targets the QEMU virt AArch64 platform (cortex-a53 / cortex-a57) and separates architecture-agnostic HAL interfaces (kernel/hal/) from target-specific platform code (kernel/hal/targets/qemu_virt/), AArch64 exception/trap handling (arch/aarch64-linux-gnu/), and device drivers (kernel/drivers/, kernel/input/).
arch/aarch64-linux-gnu/boot.s, arch/aarch64-linux-gnu/trap_aarch64.cpp, kernel/hal/trap.hpp)vector_table in arch/aarch64-linux-gnu/boot.s): Placed in .text.vectors with 2048-byte alignment (0x800) and installed into vbar_el1 during _start. Each of the 16 exception entries occupies 128 bytes (0x80):
0..3): Branch to trap_invalid_common with x9 set to 0..3.4..7): Synchronous (4) branches to trap_sync_common; IRQ (5) branches to trap_irq_common; FIQ (6) and SError (7) branch to trap_invalid_common (x9 = 6, 7).8..11): Synchronous (8) branches to trap_sync_common; IRQ (9) branches to trap_irq_common; FIQ (10) and SError (11) branch to trap_invalid_common (x9 = 10, 11).12..15): Branch to trap_invalid_common with x9 set to 12..15.Context Save & Restore (trap_sync_common, trap_irq_common, trap_invalid_common, user_trap_exit in boot.s): Allocates 288 bytes on the stack (sizeof(AArch64CpuContext) == sizeof(HAL::CpuContext)), saves x0–x30 plus sp_el0, elr_el1, spsr_el1, and tpidr_el0, invokes the C++ handler, and on user_trap_exit restores those system/general registers before eret.
aarch64_handle_sync_exception in trap_aarch64.cpp):
EC == 0x15 (SVC in AArch64): Dispatches HAL::TrapType::Syscall through HAL::get_trap_dispatcher().handle_trap(frame) (SyscallDispatcher in kernel/syscalls/SyscallDispatcher.cpp), then calls Kernel::Process::deliver_pending_signals(*ctx).EC 0x20, 0x21, 0x24, 0x25 (Instruction/Data Aborts): For translation faults (FSC 0x04..0x07 with FnV == 0), resolves demand-zero PTEs/VMAs (PTE_DEMAND_ZERO or anonymous Vma) or file-backed VMAs (Kernel::MM::AddressSpace::resolve_file_backed_page via PageCache). For data abort write permission faults (EC == 0x24, FSC 0x0C..0x0F, WnR == 1) on DESC_COW pages, copies shared frames via Kernel::MM::alloc_frame() + Kernel::MM::page_put() or upgrades exclusive frames in place, then invalidates TLB (tlbi vaae1is) and I-cache if executable. Unresolved faults terminate an EL0 process ((spsr_el1 & 0xF) == 0) via exit_current_process(-1) / return_to_kernel(-1), or in EL1 either restore ELR_EL1 = x30 for EC == 0x21 or advance ELR_EL1 += 4 (for test fault recovery).EC values log [PANIC] Unhandled Synchronous Exception! and either terminate an EL0 process or halt in a HAL::get_cpu().halt() loop; aarch64_handle_invalid_exception logs [PANIC] Invalid/Async Trap Captured: and similarly terminates EL0 or halts EL1.kernel/drivers/irq/gicv2.{hpp,cpp}, kernel/hal/targets/qemu_virt/gicv2.hpp, arch/aarch64-linux-gnu/trap_aarch64.cpp)GICv2 Configuration (GicV2::init & HAL::QemuVirt::init_gicv2): Uses Distributor base 0x08000000 (GICD_BASE), CPU Interface base 0x08010000 (GICC_BASE), and MAX_IRQ_LINES = 1020U (MAX_SGI_PPI_IRQ = 31U, MIN_SPI_IRQ = 32U, MAX_SPI_IRQ = 1019U, SPURIOUS_IRQ_THRESHOLD = 1020U). init() disables GICC and GICD during setup, sizes lines from GICD_TYPER ((ITLinesNumber + 1) * 32, clamped to [256, 1020]), assigns Group 0, disables/clears pending and active bits, sets default priority 0x80, routes SPIs (32..max_lines) to CPU 0 (0x01), sets GICC_PMR = 0xF0, and enables GICD and GICC.
IRQ Dispatch & User-Space Driver Access: aarch64_handle_irq_exception calls GicV2::acknowledge_irq() (GICC_IAR & 0x3FF), ignores spurious IDs >= 1020 without writing EOIR, handles TIMER_PPI_IRQ (30U) by reloading the timer, sending EOIR, and calling Kernel::Scheduler::on_timer_tick(from_user), or dispatches registered PPI/SPI handlers via GicV2::dispatch_irq(irq), delivers pending signals when from_user is true, and writes GICC_EOIR. Hardware drivers run inside EL1 and expose user-space access through devfs character nodes (/dev/fb0 and /dev/input/event<N> with poll(), read(), ioctl(), and mmap()) rather than exposing raw MMIO or IRQs directly to EL0.
flowchart LR
subgraph DEV["Hardware & VirtIO Devices"]
TMR["ARM Generic Timer<br/>PPI IRQ 30"]
VIO_IRQ["VirtIO-MMIO Slots<br/>0x0A000000+ (SPIs 48..79)"]
UART_HW["PL011 UART0<br/>0x09000000"]
GPU_HW["VirtIO-GPU / ramfb<br/>& fw_cfg 0x09020000"]
INP_HW["VirtIO-Input<br/>Keyboard / Tablet"]
BLK_HW["VirtIO-Blk<br/>disk.img (ext2)"]
end
subgraph GIC_TRAP["Interrupt & Trap Path"]
GIC["GICv2 (0x08000000 / 0x08010000)<br/>acknowledge_irq / end_of_interrupt"]
VEC["vector_table -> trap_irq_common<br/>(arch/aarch64-linux-gnu/boot.s)"]
H_IRQ["aarch64_handle_irq_exception<br/>(arch/aarch64-linux-gnu/trap_aarch64.cpp)"]
end
subgraph DRV_VFS["EL1 Drivers & VFS Nodes"]
SCHED_T["Scheduler::on_timer_tick"]
D_BLK["VirtioBlkDevice + WaitQueue"]
D_NET["VirtioNetDevice"]
D_GPU["VirtioGpuDevice / RamfbDevice<br/>-> /dev/fb0"]
D_INP["VirtioInputDevice + InputQueue<br/>-> /dev/input/eventN"]
D_UART["Pl011Uart -> /dev/console, /dev/tty"]
end
TMR --> GIC
VIO_IRQ --> GIC
GIC --> VEC --> H_IRQ
H_IRQ -->|"IRQ 30"| SCHED_T
H_IRQ -->|"SPI 48..79"| D_BLK
H_IRQ -->|"SPI 48..79"| D_NET
H_IRQ -->|"SPI 48..79"| D_GPU
H_IRQ -->|"SPI 48..79"| D_INP
UART_HW <--> D_UART
GPU_HW <--> D_GPU
INP_HW <--> D_INP
BLK_HW <--> D_BLK
kernel/hal/targets/qemu_virt/timer.{hpp,cpp}, kernel/hal/platform.hpp)HAL::QemuVirt::GenericTimer controls the EL1 physical timer (TIMER_PPI_IRQ = 30U):
init(freq_hz) reads cntfrq_el0, computes g_reload_val = g_cntfrq / freq_hz (defaulting freq_hz = 100U when 0, with a minimum reload of 1), calls reload(), and writes 1 to cntp_ctl_el0 (ENABLE=1, IMASK=0) with isb.disable() writes 2 to cntp_ctl_el0 (ENABLE=0, IMASK=1) with isb.reload() writes g_reload_val to cntp_tval_el0 with isb.frequency_hz() and counter_ticks() (reading cntpct_el0) back HAL::Platform::timer_frequency_hz() and HAL::Platform::timer_counter_ticks().kernel/drivers/uart/pl011.{hpp,cpp}, arch/aarch64-linux-gnu/uart_console.cpp, kernel/hal/targets/qemu_virt/pl001.cpp)Pl011Uart (kernel/drivers/uart/pl011.{hpp,cpp}) implements HAL::Console (init, putc, getc, has_input, write) over ARM PrimeCell PL011 registers (UARTDR, UARTRSR, UARTFR, UARTLCR_H, UARTCR, UARTIMSC, UARTICR).init() clears UARTCR, masks UARTIMSC, clears UARTICR (0x7FF) and UARTRSR, configures 8N1 with FIFOs enabled (UARTLCR_H_WLEN_8 | UARTLCR_H_FEN), and enables UARTCR_UARTEN | UARTCR_TXE | UARTCR_RXE.arch/aarch64-linux-gnu/uart_console.cpp defines global_uart_driver at 0x09000000ULL (constinit), exposing HAL::get_console(), HAL::Platform::primary_uart(), and HAL::console_on_mmu_enabled() (which calls global_uart_driver.on_mmu_enabled() to switch m_mmio_base to Kernel::MM::phys_to_virt(m_phys_base)). kernel/hal/targets/qemu_virt/pl001.cpp is currently an empty stub ([WIP]).fw_cfg, Framebuffer, ramfb & VirtIO-MMIO Drivers (kernel/drivers/, kernel/input/, kernel/hal/targets/qemu_virt/platform_*.cpp)FwCfgDevice (kernel/drivers/firmware/fw_cfg.{hpp,cpp}): Probes QEMU fw_cfg MMIO (from FDT qemu,fw-cfg-mmio or default 0x09020000, size 0x18), verifies the "QEMU" signature on selector 0x0000, reads FW_CFG_ID (0x0001), allocates a DMA control frame, supports MMIO reads (read_mmio), DMA reads/writes (dma_read_phys, dma_write_phys), and directory lookup/read (find_file, read_file, e.g. "etc/ramfb").Framebuffer & RamfbDevice (kernel/drivers/video/{framebuffer,ramfb}.{hpp,cpp}): Framebuffer allocates contiguous PFA frames for XRGB8888 / ARGB8888 buffers (init, destroy, clear, put_pixel, fill_rect, flush, flush_rect, and an optional flush callback). RamfbDevice locates "etc/ramfb" (28-byte big-endian RamfbConfigRaw), allocates backing pages, and commits the configuration via fw_cfg DMA. devfs_register_fb0() registers /dev/fb0 (FB0_RDEV = 0x1D00) when HAL::Platform::primary_framebuffer() is active.kernel/drivers/virtio/, kernel/input/, kernel/hal/targets/qemu_virt/platform_{block,gpu,input,net}.cpp):
SplitVirtqueue allocates contiguous descriptor, available, and used ring pages via PFA and handles descriptor chains (submit_chain, pop_used, has_used, should_notify, suppress_interrupts).VirtioMmioTransport probes modern VirtIO-MMIO (magic == 0x74726976, version == 2, non-zero device ID), negotiates features including VIRTIO_F_VERSION_1, configures queues, reads/writes config space at 0x100, and manages interrupts/status.VirtioMmioBus / VirtioMmioSlot discovers slots from FDT virtio,mmio nodes or the 32 QEMU virt fallback slots (0x0A000000, stride 0x200, SPIs 48..79) and binds drivers by device ID.VirtioBlkDevice (virtio_blk.{hpp,cpp}): Binds VIRTIO_DEV_ID_BLK (2), submits VIRTIO_BLK_T_IN, VIRTIO_BLK_T_OUT, and VIRTIO_BLK_T_FLUSH descriptor chains from a 4 KiB DMA page (up to 7 sectors / 3584 bytes per chunk), sleeps on WaitQueues when a process context is active (or polls/handles IRQs in early/shell context), and wakes waiters on IRQ completion.VirtioGpuDevice (virtio_gpu.{hpp,cpp}): Binds VIRTIO_DEV_ID_GPU (16), sets up controlq (0) and optional cursorq (1), queries display mode (GET_DISPLAY_INFO, falling back to 1024x768), creates a 2D resource (B8G8R8X8_UNORM), attaches backing pages (RESOURCE_ATTACH_BACKING), configures scanout (SET_SCANOUT), hooks Framebuffer flushes to TRANSFER_TO_HOST_2D + RESOURCE_FLUSH, and tears down scanout/backing/resource on shutdown().VirtioInputDevice & InputQueue (virtio_input.{hpp,cpp}, kernel/input/InputQueue.{hpp,cpp}, InputEvent.hpp): Binds VIRTIO_DEV_ID_INPUT (18), queries device name, IDs, supported event types, and ABS_X/ABS_Y info from config space, pre-populates eventq (0) with VirtioInputEvent buffers, translates events with Duration::from_ticks(ticks, freq).nanoseconds() into InputQueue (256-event ring with WaitQueue and oldest-drop overflow policy), and registers /dev/input/event<N> via devfs_register_input_event.VirtioNetDevice (virtio_net.{hpp,cpp}, kernel/net/NetworkDevice.hpp): Binds VIRTIO_DEV_ID_NET (1), negotiates VIRTIO_NET_F_MAC (with fallback MAC 02:56:49:52:54:4F) and VIRTIO_NET_F_STATUS, initializes rxq (0) and txq (1) with 12-byte VirtioNetHdr, and supports send_frame, receive_frame, poll, stats, and IRQ handling.