The Swap With No Middle
The claim is about the csrrw instruction's semantics: one instruction atomically exchanges a CSR with a register, rd receiving the old CSR value while rs1 is written into the CSR. The run performs the swap twice on mscratch and checks both sides of each exchange, the read side in rd and the write side by readback.
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, and the swap contract is complete from the instruction semantics and the run.
No boot code, machine-mode service, or privilege handoff takes part. The binary installs its own M-mode trap handler on the bare virt machine with no firmware beyond the reset state, and the swap argument never enters firmware.
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 instruction semantic applied as one fixed idiom, not a computation proved against an independent oracle. The swap is shown working across one sentinel round-trip, but nothing here establishes a general algorithm.
A trap fires and the handler needs a stack. The stack pointer in sp belongs to the interrupted code, and every general register holds something that code was using. You cannot store a single byte until you have a stack, and you cannot get a stack without moving sp somewhere first. [1]
One instruction moves it. csrrw sp, mscratch, sp writes the old sp into mscratch and loads the old mscratch value into sp, as one atomic step. Point mscratch at your handler stack once at boot, and trap entry becomes this single instruction: the victim's stack pointer is parked in mscratch, the handler stack lands in sp. Because it is one instruction, no interrupt can be taken between the read half and the write half. There is no between. No temporary register, no memory traffic. [1] [2]
The exchange, in both directions:
csrrw sp, mscratch, sp # entry: victim sp parked, handler stack live
# ... save registers, handle the trap ...
csrrw sp, mscratch, sp # exit: the same instruction swaps them back
Diagram: hand-built by Chris Dillinger for this post
The run behind it. A bare-metal M-mode program on QEMU 8.2.2 swapped a sentinel through mscratch twice. The boot read found 0x0. The first csrrw returned the old value 0x0 in rd while mscratch read back 0xa5a55a5a5a5a5a5; the restore swap returned the sentinel in rd while mscratch read back 0x0. Five checks, zero failures, zero traps. One honest boundary: the executor is QEMU's M-mode model, faithful, but still a model, not silicon. [1]
The habit this buys: set mscratch once, at boot, to the top of the handler stack. Every trap entry then starts with one instruction instead of a register-shuffling prologue, and trap exit runs the mirror image. The swap is its own undo: run it twice and everything is exactly where it was. [2]
Sources
- 01src/mscratch-csrrw: module README and PROOF.mdChris Dillinger
The firsthand QEMU run behind the post: the two csrrw swaps on mscratch, the measured values 0x0 and 0xa5a55a5a5a5a5a5 on both the read side in rd and the write side by readback, and the PASS verdict.
- 02RISC-V ISA Manual, unprivileged architecture (Zicsr)RISC-V International
Defines the CSR read-write instructions: csrrw atomically reads the CSR into rd and writes rs1 into the CSR as one operation.