The Swap That Needs No Spare Register
The claim is the Zicsr csrrw rule: one instruction writes rd with the old CSR value and writes rs1 into the CSR, so the two values exchange with no third register spent. The run shows both sides directly: the swap returned the boot value 0x0 and the readback showed the sentinel 0xa5a55a5a5a5a5a5, then the restore swap returned the sentinel and the final readback was 0x0 again.
No claim depends on how any core implements a CSR swap. QEMU serves as a functional executor only; no pipeline or execution-unit behavior is asserted or measured, and the honest limit states the result is emulator-observed, not silicon-proved.
No firmware service takes part in the claim. The binary runs in M-mode on bare QEMU with no SBI or firmware calls; the behavior under test is the csrrw instruction's own exchange semantic.
The exchange is the trap-entry idiom kernels are written against: at trap entry no general-purpose register is free, so xv6's uservec opens with csrrw a0, sscratch, a0, putting the trapframe address in a0 and the user's a0 in sscratch. The module verifies the same exchange on mscratch in M-mode, which is the mechanism the idiom relies on.
The claim is one fixed instruction semantic exercised as a single sentinel round trip with register and CSR readback, not a computation proved against an independent oracle. The results are shown as the consequence the csrrw rule forces, not as a verified algorithm.
A trap lands and every register belongs to the interrupted code. The handler must save all 32 registers before it can use any of them, but storing to memory needs an address, and an address needs a register. There is no register to spare, and no place to put one down first. That is the trap-entry problem, and one instruction solves it.
Measured on QEMU 8.2.2, bare metal in M-mode. The program swapped a sentinel into mscratch, then swapped the boot value back. The first swap returned the boot value, 0x0, and the readback showed the sentinel, 0xa5a55a5a5a5a5a5. The restoring swap returned the sentinel, and the final readback was 0x0 again. Five checks, zero failures, no traps, and the run printed the same checksum the module records, 0xd2dc5b584cb8669. [1]
The witness, the one instruction and what it returned:
csrrw a0, sscratch, a0 # exchange, not read then write
# before: a0 = user value, sscratch = trapframe
# after: a0 = trapframe, sscratch = user value
Why one instruction matters. A read followed by a write is two steps with a gap between them: the read puts the old value in a register, and until the write lands, the exchange is half done and a register is spent holding the copy. csrrw has no gap and spends no register. The destination register receives the old CSR value at the same moment the CSR receives the register's value, so neither value is ever homeless, and nothing needs a third place to sit. The swap is the whole operation, not two operations stapled together.
That is exactly the door xv6 walks through. Before a process runs in user space, the kernel writes the address of its trapframe into sscratch. When a trap lands, uservec opens with csrrw a0, sscratch, a0: a0 now holds the trapframe address, sscratch now holds the user's a0, and the handler stores every register into the trapframe through a0, including the user's a0, read back from sscratch. [2] The module behind this post proves the same exchange on mscratch in M-mode, because that is the CSR a bare-metal M-mode program can touch; the instruction is the same, only the scratch register changes. [1]
The bench habit: whenever a trap entry or a context switch needs a register before it has anywhere to save one, park the pointer in a scratch CSR ahead of time and open with the swap. One instruction buys the first free register, and the value it displaced is already safe in the CSR, waiting to be stored with the rest.
One sentence to carry away: csrrw does not read a CSR and then write it, it exchanges with it, and the exchange is what frees the first register.
Diagram: hand-built by Chris Dillinger for this post
Sources
- 01src/mscratch-csrrw: module PROOF.md and the QEMU runChris Dillinger
The QEMU 8.2.2 run behind the post: csrrw on mscratch in M-mode, a sentinel swapped in and the boot value swapped back, the swap returning the boot value 0x0, the readback showing the sentinel 0xa5a55a5a5a5a5a5, the restore returning the sentinel, and the final readback 0x0. 5 checks, 0 failures, 0 traps, checksum 0xd2dc5b584cb8669, re-run firsthand for this post with the same verdict.
- 02L Internal: xv6 trap internals lecture notesMIT 6.S081
uservec starts with csrrw a0, sscratch, a0: the swap saves user a0 in sscratch and gives a0 the trapframe address, so the handler can save the remaining registers. The kernel set sscratch to the trapframe address before entering user space.