notes

Porting nife to RISC-V (the second architecture)

The point of a second port is not to reach RISC-V. It is to prove rule #1: that all architecture-specific code lives under kernel/src/arch/, an assumption maintained on faith since milestone 1. A genuinely different ISA is the first real test of it. RISC-V (rv64, qemu-system-riscv64 -machine virt) is chosen over x86_64 because it is clean-different rather than legacy-different. It exercises the HAL abstraction (a different trap, paging, interrupt, and firmware model) without the real-mode / GDT / IDT / APIC / UEFI tax that would make most of an x86 port about x86 plumbing, not about the abstraction. See the reasoning in the roadmap (milestone 20). x86_64 is the third port.

The detail behind this note is in riscv-port/.

The arch boundary RISC-V must satisfy

arch/mod.rs dispatches by #[cfg(target_arch)] and re-exports the arch module flat as crate::arch. A RISC-V arch adds #[cfg(target_arch = "riscv64")] mod riscv64; pub use riscv64::*;. The contract the rest of the kernel calls through crate::arch:

The two HAL leaks the port exposes (and must fix)

The port is worth doing precisely because it finds where the abstraction leaked. It found five, and every one is closed. The full account of each is in riscv-port/hal-leaks.md.

  1. thread::Context was aarch64-register-shaped in portable thread.rs. It now lives in arch/*/context.rs behind Context::for_kernel_thread and Context::for_user_thread, and thread.rs names no register.
  2. The page_tables crate encoded the aarch64 descriptor format. A PageFormat trait now carries the seam, the Mapper walk is written once, and Aarch64 and Sv39 each implement it with their own Kani proofs (DECISIONS §17 (second architecture)).
  3. user.rs embedded the whole userspace-entry mechanism: an aarch64 TrapFrame, cache maintenance asm!, and hand-written aarch64 programs. Arch seams (TrapFrame::for_user_entry, arch::sync_icache, arch::current_sp, arch::enter_user) replaced them. enter_user must be #[inline(always)], because the user-entry frame sits on the caller's own kernel stack.
  4. The system call ABI read frame.x[8]; it now goes through TrapFrame::{system_call_nr, arg, set_arg}.
  5. Portable code named drivers::gic; it now names arch::interrupt_controller, with the GIC and the PLIC behind it.

Where the user-entry frame goes was wrong on RISC-V for a year (milestone 71 (thread-start fault), 2026-08-03). The port placed it below the live sp, where a trap frame could overwrite it, and the thread then returned to a garbage PC and hung silently. The fix is a reservation: user_entry_trampoline drops sp by a frame before any Rust frame exists, and enter_frame asserts the slot sits at or above the live sp. thread_trampoline does not reserve. Only the TCB entry path can be shallow; the exec path reaches enter_frame through run and the ELF loader, far below the stack top, and a reservation there would spend a frame on every kernel thread. The lost-wakeup hang once blamed on it was a test bug, not a RISC-V one (milestone 72 (lost wakeup)).

Finding these is the point; each gets pushed under arch/.

RISC-V specifics (the clean-different)

The per-hart tp hazard: a preempted kernel thread must not carry its origin hart's pointer

tp being a general register (line above), not a system register, is the one place the "clean different" bites. TPIDR_EL1 is banked per core and no thread ever saves or restores it; a thread that migrates from one aarch64 core to another reads whatever TPIDR_EL1 the destination core set at boot. RISC-V has no such register. The kernel per-CPU pointer lives in tp, and the trap entry saves the full GPR set, tp included (x[4]), because a trap from U-mode arrives with the user's tp and the frame has to preserve it.

That save is correct. The naive restore is not. Reloading x[4] unconditionally in trap_return is right for a return to U-mode (the user owns tp), and harmless for a kernel thread only while a kernel thread always resumes on the hart it was preempted on. DECISIONS §28 (SMP placement) breaks that assumption: idle harts steal runnable threads, so a kernel thread preempted on hart A (its frame holding tp = &percpu[A]) can resume on hart B. An unconditional restore then loads &percpu[A] while running on B, and every cpu::current() after that reads hart A's per-CPU block: its current tid, its idle, its run queue, its held_rank.

The failure is spectacular and non-deterministic. A migrated worker, thinking it is on A, calls exit() and marks hart A's current thread (often A's idle) Finished; A's next schedule() then panics "the last thread exited". Other interleavings trip the rank-assert! in sync.rs (a bogus held_rank read from the wrong hart) or lose a thread and hang. Adding tracing moves it, the classic weak-ordering-adjacent tell, though the root cause is not a missing barrier: it is reading a hart-local register restored from another hart's frame.

Fix (in trap.s, the return path): restore x[4] only for a return to U-mode. For an S-mode (kernel) return, keep the live tp. It is already this hart's: trap_entry reloaded it from the per-hart stash (sscratch), and switch_to never touches tp, so it survives a migration intact. This is the exact mirror of trap_entry's ld tp, 8(t0). The regression test (smp::tests::a_migrated_kernel_thread_keeps_its_hart_pointer) drives the steal-a-preempted-thread interleaving and checks the invariant directly: sscratch names this hart's TrapStash and never migrates, so (sscratch → hart) == cpu::id() must hold on every spin; the aarch64 twin of the check is a constant true. The lesson generalizes: anything the kernel keeps in a general register is part of the thread context on RISC-V, and the trap path must decide per privilege level whether it is the thread's or the hart's. tp is the hart's.

Build setup

The incremental plan (each a provable, committable piece)

Each step was a provable, committable piece, and all six are done. The boot line that proved each one, and the bugs each surfaced, are in riscv-port/bring-up-log.md.

  1. Compiles for riscv64, with the full arch contract stubbed and no change above arch/.
  2. Boots and prints: OpenSBI hands off in S-mode and the NS16550 console prints a banner.
  3. Traps: stvec, the TrapFrame and trap.s, proven by a boot self-test that catches an ebreak.
  4. MMU: Sv39 kernel tables, W^X, high half, and the kernel mapping surface.
  5. Timer and interrupts: SBI TIME at 100 Hz, then the PLIC for device interrupts.
  6. The capability core: U-mode programs, capability invocation, preemption, a compiled ELF, an init that builds its children from its own budget, and a userspace UART driver on an Interrupt capability.

The port is complete. Boot, MMU, traps, timer, scheduler, preemption, U-mode user programs, capability invocation, userspace-built processes (init loads and builds children), and device interrupts serviced by a userspace driver all run on RISC-V, the same portable core as aarch64.

The reward beyond the proof: when a real RISC-V board (a ~$70 StarFive VisionFive 2 / Milk-V Mars, or a rented Graviton later) is on hand, the QEMU-virt work transfers. Real boards use the same OpenSBI + S-mode + device-tree model. And "one verified capability microkernel, aarch64 and RISC-V" is a stronger demonstrator line than any single board.