The Two Bits That Spare the Kernel Thirty-Two Saves
matters at ISA because the verified behavior is the privileged specification's own contract, section 3.1.6.7: FS, the field in mstatus bits 14:13, reports the floating-point unit's save state, SD at bit 63 is the OR of the dirty states, and Off makes floating-point instructions trap, so this state machine binds every machine-mode supervisor.
not at Microarch because the verified contract is an architecturally specified state machine: the Initial-to-Dirty move on a single fmv.d.x and the trap on Off are specified by the privilege manual, so they hold on every compliant hart and vary with no implementation choice.
matters at Firmware because machine-mode firmware owns mstatus at boot and sets FS before any supervisor runs: the verified sequence opens with the boot word 0xa00000000, FS at Off, and that starting value is firmware's decision, so the field is load-bearing for whoever writes the boot code.
matters at Kernel because the kernel's context switch is the consumer of the verified bits: the save discipline writes the floating-point state out only when FS reads Dirty and marks it Clean, the restore discipline loads from memory only when Clean, and Initial is restored from the constant instead of memory, so the two bits delete thirty-two register spills on the switches that never needed them.
not at Algorithm because the verified claim is a hardware state transition read from mstatus, not a computation: the Initial-to-Dirty move on a real fmv.d.x exists only where a floating-point unit sits behind the FS field, so no portable argument can carry it.
The module starts at boot and reads mstatus: 0xa00000000, with FS, the floating-point status field in bits 14:13, sitting at 0, Off. One csrs sets FS to 1, Initial. One fmv.d.x f1, x0, an instruction that moves a 64-bit value into a floating-point register, which counts as touching the unit, moves FS to 3, Dirty, and bit 63, SD, the summary dirty bit, lights with it. A second fmv.d.x leaves FS at 3: sticky. A csrc clears it back to the exact boot word. The trap counter never moved, across three byte-identical runs on QEMU 8.2.2.
Photo: Raysonho (Wikimedia Commons user Raysonho) (CC0)
The honest version of that sequence was not the first plan. The plan was to write f1 with FS at Off and watch it go Dirty. The hardware refused: at Off, a floating-point instruction does not execute and get recorded, it raises an illegal-instruction exception instead. Off is not a starting point: floating-point instructions trap there. The unit is disabled until software enables it at Initial, and only then does a write dirty it. The plan needed two more corrections of the same kind, and all three shipped in the module's PROOF.md: the gloss said to clear FS through fcsr, but FS lives in mstatus bits 14:13; it named sstatus, but the module runs in M-mode and reads mstatus directly, sstatus being only the supervisor's view of the same bits. A module that publishes its own corrections is one worth reading.
Why do the two bits exist at all? A process's floating-point state is thirty-two registers plus fcsr, 256 bytes of doubles the kernel must preserve across a context switch. Without FS, every switch saves and restores all of it, even for the integer-only process that never touched floating point. With FS, the trap handler reads one field and skips the spill when the answer is not Dirty. The privileged manual states the whole contract in section 3.1.6.7: the design anticipates that most context switches will not need to save or restore the floating-point state, so it provides a fast check via the SD bit. The four states are Off, where floating-point instructions trap; Initial, where the state holds the constant initial value; Clean, where the state matches the last context save; and Dirty, where the state may have changed since.
The discipline the manual demands fits in three lines:
if (fs == DIRTY) { save_fpu(); fs = CLEAN; }
if (fs == CLEAN) { restore_fpu(); }
if (fs == INITIAL) { zero_fpu(); } /* constant, never memory */
That last line is a security boundary wearing a performance costume. Restoring Initial from memory would load another process's registers into this one, so the manual demands the constant instead. The module measured exactly the flip the kernel trusts: Initial to Dirty on a single instruction, sticky on the next, cleared back to the baseline word, with no trap fired.
One sentence to carry away: the CPU keeps a two-bit receipt of whether the floating-point state changed since the last save, so the kernel can skip thirty-two register stores on the switches that never needed them.
Code: src/sstatus-fs-dirty