Chris Low-level systems developer
← Back to all posts

The Store That Fails Quietly

ISA

The claim is about the sc.w instruction's contract: a store-conditional with no live reservation for its target address reports failure in rd as a nonzero value, writes nothing to memory, and raises no trap. The run exercises all three shapes, a missing reservation, a mismatched reservation, and the live-reservation control, checking rd and memory readbacks each time.

MICROARCH

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 failure contract is complete from the instruction semantics and the run.

FIRMWARE

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 reservation argument never enters firmware.

KERNEL

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.

ALGORITHM

The claim is one fixed instruction semantic applied as one fixed idiom, not a computation proved against an independent oracle. The retry loop is shown as the consequence of the contract, but nothing here proves a concurrent algorithm correct.

A lock-free counter on RISC-V is built on two instructions. lr.w loads the word and arms a reservation on its address. sc.w stores the new value, but only if that reservation is still live. The condition sits in the hardware, not in your code. [2]

What sc.w promises is narrower than it looks. With no live reservation for the target address, the instruction does three things: it writes a nonzero value into rd, it writes nothing to memory, and it raises no trap. That is the entire report, and it arrives in silence. The reservation is dead when you never armed one, or when you armed one on a different address. Either way the answer is identical: rd = 1, the word untouched, no exception. [1] [2]

The idiom this forces:

retry:
  lr.w t0, 0(a0)      # load the word, arm the reservation
  addi t0, t0, 1      # do the actual work
  sc.w t1, t0, 0(a0)  # attempt the store
  bnez t1, retry      # nonzero means it never happened: try again
Blueprint-style diagram of three store-conditional cases: sc.w with no lr.w and lr.w on one cell with sc.w on another both report rd = 1 with memory unchanged; lr.w and sc.w on the same cell reports rd = 0 with the value stored
The whole report arrives in rd. No reservation, or the wrong reservation: rd = 1, nothing written, no trap. Live reservation: rd = 0, stored.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal program on QEMU 8.2.2 ran all three shapes against two aligned cells. sc.w with no lr.w at all: rd = 1, the cell still 0xaaaaaaaa. lr.w on cell A followed by sc.w on cell B: rd = 1, both cells unchanged. The control, lr.w then sc.w on the same cell: rd = 0, and 0x12345678 read back. Eight checks, zero failures, and no trap fired in any run. One honest boundary: the executor is QEMU's machine model, faithful, but still a model, not silicon. [1]

The habit this buys: read rd after every sc.w, and treat nonzero as routine, not as an error. Contention, a stray store between the pair, or a missing lr.w all report the same way. The dangerous case is the one the hardware cannot flag: code that skips the check and assumes the store landed, because a failed sc.w leaves no trace except the value in rd. [2]

Sources

  1. 01
    src/sc-fail: module README and PROOF.mdChris Dillinger

    The firsthand QEMU run behind the post: sc.w with no reservation and with a mismatched reservation both reporting rd = 1 with memory unchanged, the live-reservation control reporting rd = 0 with the stored value read back, eight checks, zero traps.

  2. 02
    RISC-V ISA Manual, unprivileged architecture (A extension)RISC-V International

    Defines the load-reserved/store-conditional contract: a store-conditional whose reservation is not live for its target address writes a nonzero value to rd and does not write memory.

Related work: lock-free primitives and bare-metal concurrency bring-up, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app