roadmap BUILT

447. A thread's vector registers are its own

Built 2026-09-20. This is Route 2 of milestone 164 (x86_64 userspace can't build aes: no SSE, no scalar fallback), opened by calef; that milestone's block prices the route.

This block is complete on its own. Four appendices hold the working (index):

What was true before this, and it was true on purpose

The kernel saved no floating-point or vector state anywhere, on any of the three architectures. Not a gap: a decision, recorded three times and enforced by construction.

So the interesting thing here is not that a kernel can save registers. It is that a kernel built this far on the assumption that it never had to now does, without slowing the switch for threads that do not use it. And the three ISAs' mechanisms are different enough to be worth writing down.

The rule, which is one sentence

The FP/SIMD register file holds the running thread's data, or it holds the initial state. It never holds a thread's data while a different thread runs.

That is kernel/src/fp.rs's header, and everything else is that sentence made true at the one instant it could stop being true. crate::fp::hand_over is called by the outgoing thread, on the outgoing thread's stack, with IPC_TABLES already released and interrupts masked, immediately before arch::switch_to. Four cases:

outgoing incoming what happens
not live not live nothing. Two loads and a branch. Every switch in this tree today.
live live save, restore
not live live enable, restore
live not live save, scrub to the initial state, disable

The switch itself is untouched. context.s still saves what a function call may destroy, on all three architectures, and this runs beside it rather than inside it. That was a deliberate choice over growing Context and letting switch_to do the whole job. The register file is not a calling convention's callee-saved set; it is 512 bytes that cross a thread boundary. Keeping it in Rust keeps it typed, testable, and out of three files of delicate hand-computed offsets.

And it lives in the free space of the thread's own TCB page, not in the Thread struct and not on the kernel stack. That was the second shape rather than the first, and script/stack-frame-check is the reason (see the cost summary below).

Why eager, and why "lazy" is a warning word here

The cheap-looking scheme is to leave the outgoing thread's registers in the hardware, trap when the incoming thread touches them, and only then swap. Several kernels did that until 2018, when LazyFP, CVE-2018-3665, showed that on x86 the trap is not a boundary. Speculative execution past the #NM reads the registers the trap was supposed to protect, and one thread recovers another's AES round keys. The mechanism was CR0.TS, which is the very bit this tree's x86 half now uses.

So the three control registers here answer "has this thread ever wanted FP", which is a performance question. They never answer "whose data is in the registers", which is a confidentiality one. The second question is answered by the registers always being right, which is what the fourth row of the table above is for. When a thread that used the unit hands off to one that has not, the file is scrubbed to the architectural initial state before the trap is re-armed. There is nothing behind the trap to speculate at.

Three architectures, three mechanisms, one rule

gate first-use trap what moves bytes
aarch64 CPACR_EL1.FPEN, 0b00 (traps EL0 and EL1) against 0b11 its own exception class, ESR_EL1.EC == 0x07 q0-q31, FPCR, FPSR 512 + 16
riscv64 sstatus.FS, Off against anything else an ordinary illegal instruction; nothing distinguishes it f0-f31, fcsr 256 + 8
x86_64 CR0.TS, with CR4.OSFXSR/OSXMMEXCPT set and CR0.EM clear #NM, vector 7 the FXSAVE area: x87, MXCSR, xmm0-xmm15 512

The RISC-V row cost this milestone real time, twice (riscv.md has both). RISC-V does not say why an instruction was illegal, so the handler retries rather than decodes: a genuinely illegal instruction traps again with FS no longer Off and takes its fault. And sstatus travels in RISC-V's trap frame, so the first-use handler sets the frame's copy too.

What it found: one architecture's boot path does not run arch::init

arch::fp::init shuts the unit for a core before any thread exists. It went first in arch::init, which was green on aarch64 and x86_64 and silently wrong on RISC-V. main's RISC-V tour calls arch::exceptions::init() directly and never passes through arch::init. OpenSBI hands over a hart with sstatus.FS already set, so no thread took the first-use trap and two threads shared a register file with nothing saying so.

The fix is not a third call site. The invariant is about threads, so it lives where threads begin to exist: sched::init and sched::adopt_secondary_idle, both portable. The test that caught it, the_first_floating_point_instruction_takes_a_trap, was written to catch exactly this. The full account is in riscv.md.

What it costs, measured

The tables and working are in costs.md.

The proof, and it fails against a kernel without this

Five #[test_case]s in kernel/src/fp.rs, running on all three architectures; proof.md says what each one catches. two_threads_doing_vector_work_do_not_see_each_others_registers is the milestone. Two threads on one core fill the whole register file with distinct patterns, yield two hundred times each, and check their own values after every turn. Falsified on purpose: with save removed from hand_over, it fails with "a thread found another thread's values in its own vector registers". The others check the scrub (the fourth row, and CVE-2018-3665's case), every register by name, the first-use trap, and that threads which never used the unit leave it shut.

The tests are kernel threads, because every target in targets/ is soft-float and no userspace binary can execute an FP instruction. So all three first-use handlers serve the kernel's own exception level as well as userspace, and §31's C seam is untouched.

What this does NOT do: the target flip is not taken

No target JSON is changed and no force-soft flag is removed. Turning +soft-float off changes the calling convention for every userspace binary and every std crate built against it. That is an ABI two programs agree on, which AGENTS.md's move fast on what can be undone tenet puts in the irreversible category. §22 (Rust std on the native ABI, the Hermit way) chose it deliberately, specifying nife's target JSON as "softfloat, and singlethread = true".

This milestone makes the flip possible, and it is an architect's to make, on evidence. target-flip.md prices it: four edits and a rebuild, buying less than the framing suggests (one force-soft flag, aes_force_soft), and costing a save on most switches once most threads are live. Milestone 534 (the soft-float targets could now be flipped) carries it now.

BUGS

Follow-on

Index row

Milestone 164's refused Route 2, reopened by calef. The kernel saved no FP or vector state anywhere, which is why every target in targets/ is soft-float as a correctness requirement rather than a preference. It now saves the whole register file across a context switch on all three architectures, under one rule: the file holds the running thread's data or the initial state, never a stranger's. Eager and not lazy, because the trap this uses on x86 is CR0.TS and deferring the restore behind it is CVE-2018-3665. The expensive half is #[cold] and off the IPC fastpath, so a soft-float thread pays two loads and a branch. And the register file costs no memory: it lives in the 2,944 bytes of a thread's TCB page that were already allocated and idle. That is where script/stack-frame-check sent it after the inline shape put Thread::spawn 1,056 bytes over the guard page. script/fastpath-footprint within bound on every ISA, a context switch 2-3% more instructions, coremark and script/icount unmoved. Proved by two threads doing vector work on one core, falsified against a kernel with the save removed. The target flip is not taken: it is an ABI and calef's, and the block ends with what it would take, buy and cost.