The RISC-V port's incremental plan, step by step
Name: provisional. After the brief's own example of a name for content.
An appendix to notes/riscv-port.md, moved there on 2026-10-11 (UTC) under §212 (a prose budget). Each step was a provable, committable piece, and each is recorded here with the boot line that proved it and the bugs it surfaced.
-
Compiles for
riscv64: done. The full arch contract stubbed (pure primitives real, every deferred piece a loudunimplemented!()), all four leaks resolved, the build wiring in place.cargo build --target riscv64imac-unknown-none-elflinks a RISC-V ELF, and bothcargo clippypasses (aarch64 and riscv64) are clean under-D warnings. aarch64 stays fully green: 116 kernel tests pass. This proves the boundary is complete: a second architecture compiles against the entire kernel with no change abovearch/(the four leaks were the exceptions, now closed). What is not yet done is running: everyunimplemented!()inarch/riscv64is real work for the steps below. -
Boots and prints: done. OpenSBI hands off in S-mode;
_startsetsspand zeroes.bss.kernel_mainsets thetpper-CPU register, brings up the NS16550 console (a plain-volatile byte UART,drivers/ns16550.rs, selected by a compile-time alias inconsole.rs), and prints a banner including the real device-tree pointer OpenSBI passed ina1. That one line exercises the whole chain: S-mode entry, stack,.bss,tp,interrupts::disable(the console lock uses it), the UART, and the DTB handoff. Then a cleanwfihalt. The milestone-1 moment on a second ISA. A fifth leak surfaced and was deferred, not fixed. Portable code (sched,smp,system_call,user) nameddrivers::gicdirectly, an interrupt-controller coupling that the traps step resolves with a PLIC abstraction. Until then the GIC driver stays un-gated (it compiles on riscv and is dead). -
Traps: done.
stvec+ theTrapFrame+ trap.s (save 36 registers, dispatch onscause, restore,sret). The system-call-ABI leak is resolved:system_call.rsreads the number and args throughTrapFrame::{system_call_nr, arg, set_arg}, soecall's a7/a0..a5 map without portable code naming a register. Proven by a boot self-test: anebreakis caught,sepcstepped past it, andsretreturns. First cut is S-mode traps on the current stack; thesscratchstack switch for U-mode traps arrives with the user path. -
MMU (Sv39): done on the kernel side. It took the paging-format work (leak
#2) and the higher-half boot transition (calef chose high-half, DECISIONS §17 (the second architecture); proven by the banner's live code address0xffffffc0_8020_xxxx). It also took the fine-grained W^X kernel tables (mmu::initviaMapper<_, _, Sv39>, replacing the coarse RWX boot table,satpswitched live with the console surviving), and the kernel mapping surface (map_page/unmap_page/translate/flush_tlb, proven by a map/write/read/unmap self-test). Remaining: the user-mapping surface and per-processsatp(the user path). The RISC-V single-satpmodel means every process root shares the kernel's high-half top-level entries;reserved_root/switch_user_rootalready implement the kernel-thread side of that. -
Timer and interrupts: the timer is done. SBI TIME
set_timer+sie.STIE, the dispatcher routesscause= timer totimer::tick; proven by ~17 ticks in 0.2 s at 100 Hz. Remaining: the PLIC (external/device interrupts), which resolves thedrivers::gicleak; it is exercised by the userspace-driver path, so it lands with that. -
The capability core runs: done, and a user program runs at U-mode. The scheduler and context switch run on RISC-V ("2 of 2 kernel threads ran"). The user-mapping surface and the single-
satpmodel work (a processsatpis installed, the kernel survives viashare_kernel_half, a user page maps and translates); and a hand-written RISC-V program runs at U-mode and makes system_calls: "a program ran at U-mode and made 3 system calls (yield/yield/exit via ecall)". That exercises the whole path:enter_user'ssret, thesscratchU-mode trap entry,ecalldispatch through the ABI accessors, and the return to U-mode, twice, thenexit.The last-mile bug and its root cause, a genuine HAL lesson:
tpis a general register on RISC-V, not a system register. aarch64 keeps the per-CPU pointer inTPIDR_EL1, a system register that survives an EL0 round trip untouched. RISC-V'stp(x4) is an ordinary GPR, so the user trap frame left it 0, thesretgave U-modetp = 0, and the ecall trap handler ran withtp = 0and null-dereferencedcpu::current(). That panicked. The panic handler then re-panicked while formatting the message (acore::fmtpath), recursing into itself and blowing the kernel stack. That is what presented as the mystery "store fault at the frame page" (the nested-fault cascade of the overflow, unrelated to the frame's mapping, which was fine all along). QEMU's-d intexception log pinned it: the recursion was inrust_begin_unwind, and a raw-UART dump of the panic location (bypassingcore::fmt) namedcurrentinkernel/src/cpu.rs. Fix:for_user_entrycarries the kerneltpin the frame, so it survives the round trip. Follow-up (noted, not yet done): that leaks the kernel per-CPU address into U-mode'stp; the leak-free fix restorestpintrap.sfrom a per-hart source (the standard sscratch-trapframe approach), needed for SMP.IPC and capabilities work too. A second program is built from parts (a TCB, a code and stack mapping, an endpoint capability granted in slot 0) and started. It
SYS_INVOKEs that cap to SEND a word home, which the kernel receives: "a U-mode process invoked a cap and SENT 0xc4". That is the whole capability boundary (cap lookup, rights check, IPC) from U-mode on RISC-V. One RISC-V-specific bug surfaced there. The TCB entry path is shallow (trampoline →user_thread_entry→enter_frame). A trap frame placed at the top of the kernel stack (fine for aarch64, whose entry paths are deep) overlapped and corruptedenter_frame's own frame asframe.writeran, sending thesretto a garbagesepc. Fixed by placing the RISC-V trap frame below the livesp(sscratchtracks it, so re-entries stay consistent).Preemption works too, and it is the property that separates a kernel from a runtime. The S-mode timer already fired every 10ms; the missing half was hanging a reschedule off it. Now
riscv64_trap_dispatchrecords the tick (sched::on_tick) and defers the switch to the trap tail, the same four lines as aarch64'shandle_irq:if take_need_resched() && is_running() { count_preemption(); schedule(); }. Two threads whose entire body is a tight loop (no yield, no system call, not even a call) both make progress: ~680k and ~666k iterations in 0.2s, 18 preemptions. Under any cooperative scheduler the first would own the CPU forever. This is the RISC-V half of DECISIONS §5 (preemptive threads with real stacks). It works for a U-mode thread and an S-mode kernel thread alike, becausetrap.ssaved a full frame for whichever the timer interrupted, andschedule()returns throughtrap_returnto exactly the instruction it left.A real compiled ELF runs at U-mode too. Every user program above is a hand-written machine-code blob. The
workerprogram is a Rust binary compiled to a riscv64 ELF, delivered as the initrd (QEMU-initrd, read from/chosen/linux,initrd-startthe same way Linux gets its initramfs). It runs through the kernel's real ELF loader:user::loadparses the file, builds an address space with eachPT_LOADsegment mapped W^X at the VA it names, and maps a stack. The worker is granted WRITE on one endpoint (slot 0), started with an input ina1, squares it, and SENDs the answer home: "loaded a 13568-byte riscv ELF, ran worker(7) at U-mode, it sent 49". Three small pieces made this work, each closing an aarch64 assumption.user_mode_runtime(the userspace system call runtime) grew a RISC-V ABI (ecall+a7besidesvc+x8). Theelfcrate accepts the running kernel's machine (EXPECTED_MACHINE, cfg-selected, symmetric: each kernel refuses the other's ELF). Andworker.rsarch-gated its one trap instruction (ebreakvsbrk). The loader itself (load,map_segments) was already arch-neutral and needed no change. This is the first RISC-V userspace program the kernel did not hand-write.And userspace init builds the system, from a richer initrd. The single-ELF initrd became a nifefs archive holding two programs:
init(the portablebuilder) andworker. The kernel (riscv_initrd_demo, a minimalspawn_progenitorwith no GIC/PL011/IRQ baggage) loads onlyinitand maps the whole archive read-only into it.spawn_elf_child_builderis that function's descendant. It grantsinittwo capabilities: an untyped budget and a report endpoint. From those,initreadsworkerout of the archive by name. It builds it as a child entirely from its own budget through the capability verbs: retype an address space, copy each segment into retyped frames and map them, retype a TCB, endow it, configure, start. It hands the child a WRITE view of the report endpoint, and starts it with an input. The child squares it and SENDs81straight home; the kernel never parsed or mapped the worker. This exercised the full userspace capability system call surface on RISC-V for the first time (untyped RETYPE/RETYPE_OBJ, aspace MAP_INTO, frame MAP, tcb CAPABILITY_INSERT/CONFIGURE/START), all across theecallABI.builderis fully portable; it builds for aarch64, though aarch64's init is the PL011-wired role ofhello. Build the archive withcargo xtask initrd-riscv.And a real device interrupt flows through the PLIC. A keystroke becomes a message. The NS16550 raises its receive line into the PLIC, and the PLIC delivers a supervisor external interrupt (
scause= 9).riscv64_trap_dispatchclaims it (acknowledging and masking that source), looks up the endpoint it is routed to, and notifies it. A kernel driver thread blocked there wakes, reads the byte, and re-arms the source. Boot shows "a keystroke reached a kernel driver through the PLIC: 0x41 ('A')". The shape is exactly aarch64'shandle_irq(mask, notify, complete) and it reuses the arch-neutral routing (bind_irq/irq_route/irq_notify) unchanged. The PLIC's base comes from the device tree, which exposed one parser bug:node_register_regionsonly matched nodes at depth 2 (the aarch64 GIC is a direct child of the root), but the riscv PLIC is at/soc/plic@c000000, depth 3.node_register_regionsnow matches at any depth and decodesregwith the parent's cell counts. Two test caveats: pipe the byte after boot (the console clears the RX FIFO during init), and the demo driver runs in the kernel for now.The driver moved to userspace, and the last leak closed. First the demo driver became an unprivileged U-mode process (
components/src/serial_driver.rs). The kernel maps the NS16550 into it device-typed and grants it anIrqcapability and a report endpoint. It runs the seL4 IRQHandler loop,WAITon the cap (block for the interrupt as a message), read the byte through its own device mapping,SENDit,ACK. "an unprivileged userspace driver serviced the UART via its Irq cap and sent 0x5a". The kernel is never in the data path. That exercised theIrqcapabilityWAIT/ACKfrom RISC-V U-mode for the first time, and theACKre-arm is what forced the interrupt-controller seam into being.Then
arch::irqwas extracted with two working consumers behind it (the GIC and the PLIC), anddrivers::gicwas gated to aarch64. Every portable caller (sched::place_on,smp,user::spawn_elf_child_builder,main::interrupts_init, theIrqACK) now namesarch::irq, not a controller. The only code still namingdrivers::gicis aarch64 arch code (rule#1lets arch name its own driver) and thecfg(test)aarch64 IRQ tests. That was the last HAL leak. A new ISA is a newarch/directory, not a diff across the kernel, with zero exceptions in portable non-test code.The port is complete, as the main page states.