The Bit That Turns a Wait Into a Trap
The claim is about the mstatus.TW bit's contract: with TW set, a WFI executed in S-mode raises an illegal-instruction exception instead of waiting. The run verifies the TW write sticks (bit 21 readback set), delegates the trap to S-mode, and shows one S-mode wfi taking exactly one trap with scause = 2, sepc exactly at the wfi site, and stval holding the wfi instruction word.
No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured; QEMU serves as a functional executor only. The specification permits the trap after a bounded wait, and the immediate trap observed is this implementation's behavior, stated as such.
No firmware service takes part in the claim. The M-mode setup (the TW write, the medeleg delegation, the mret drop to S-mode) is the experiment harness; the behavior under test is the S-mode wfi and the delegated trap that follows it.
No kernel is involved at any step. The test binary runs on bare QEMU with no operating system, and no system interface appears in the argument.
The claim is one fixed control bit applied as one fixed interception idiom, not a computation proved against an independent oracle. The world-switch use is the consequence the specification itself names, but nothing here proves any scheduling policy correct.
Supervisor code with nothing to do runs wfi and sleeps until the next interrupt arrives. It is the polite thing for an idle loop to do. But the machine-mode monitor may not want the guest deciding when the hart sleeps. One bit in mstatus, TW at bit 21, set from M-mode, changes what wfi does everywhere below it. [2]
With TW set, an S-mode wfi does not wait. It raises an illegal-instruction exception instead. scause reads 2, sepc points exactly at the wfi, and stval carries the faulting instruction word, 0x10500073, the encoding of wfi itself. The handler learns everything from three CSRs: which instruction, and where it sat. One honest boundary: the specification permits the trap to arrive after an implementation-specific bounded wait rather than at once; on QEMU it fires immediately, and that immediacy is the implementation's choice, not the contract's promise. [1] [2]
Setting the hook, in M-mode, then the one instruction that never sleeps:
li t0, 1<<21
csrs mstatus, t0 # TW = 1: S-mode wfi now traps
...
wfi # in S-mode: scause = 2, sepc at this line
Diagram: hand-built by Chris Dillinger for this post
The run behind it. A bare-metal program on QEMU 8.2.2 set TW from M-mode and read back 0xa00200000, bit 21 set, then delegated the illegal-instruction trap to S-mode and dropped there. One wfi at a labeled site took exactly one trap: scause = 0x2, stval = 0x10500073, sepc exactly the wfi site, sepc+4 exactly the resume label. Six checks, zero failures. One honest boundary: the executor is QEMU's machine model, faithful, but still a model, not silicon. [1]
The habit this buys: set TW once, at boot, in your machine-mode monitor. Every idle attempt the guest makes after that arrives as a trap you own. The specification names the use outright: trapping wfi can trigger a world switch to another guest OS, rather than wastefully idling in the current guest. Count the idle, switch the guest, or emulate the wait and resume past the wfi. The guest asked to sleep; you get to decide what that means. [2]
Sources
- 01src/mstatus-tw-trap: module README and PROOF.mdChris Dillinger
The firsthand QEMU run behind the post: M-mode sets mstatus.TW (bit 21, readback 0xa00200000), delegates the illegal-instruction trap to S-mode, and one S-mode wfi takes exactly one trap with scause = 2, stval = 0x10500073, and sepc exactly at the wfi site. Six checks, zero failures, verified again on this run.
- 02RISC-V ISA Manual, privileged architecture (mstatus.TW)RISC-V International
Defines the TW (Timeout Wait) bit: when TW=1, WFI executed in a less-privileged mode causes an illegal-instruction exception, possibly after an implementation-specific bounded time; notes the use as a hook to switch to another guest OS instead of idling.