Chris Low-level systems developer
← Back to all posts

The Register That Names the Bad Address

ISA

The claim is the privileged specification's mtval contract: on an access fault, a nonzero mtval holds the faulting virtual address. The run traps once on a load and once on a store at the same unmapped address and reads back the identical mtval both times, proving the witness behaves the same for both access types.

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 mtval contract is complete from the privileged specification and the run.

FIRMWARE

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 trap hardware's own contract.

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 trap semantic exercised as two fixed faulting accesses with readback, not a computation proved against an independent oracle. The read-mtval-first habit is shown as the consequence the contract forces, not as a verified algorithm.

Two traps, one address. A bare-metal program loads a word from the unmapped address 0xffffffffc0000000 with lw a0, 0(a1), and the hart traps with mcause 0x5, a load access fault. It then stores to the same address with sw a0, 0(a1): mcause 0x7, a store access fault. The handler reads mtval on each trap, and the UART printed 0xffffffffc0000000 both times. Two different traps, two different cause codes, one identical witness. [1]

The mechanism is one register the hardware fills on the way into the trap. The privileged manual, section 3.1.16, says that when a trap is taken into M-mode, mtval is either set to zero or written with exception-specific information to assist software in handling the trap. Then the sentence that matters: if mtval is written with a nonzero value when an access-fault exception occurs on a load or store, then mtval will contain the faulting virtual address. The platform decides whether it reports or writes zero; the contract is about what a nonzero value means, not a promise that every hart reports. [1] [2]

The two trap records, reduced to their witness:

load trap:  mcause=0x5  mtval=0xffffffffc0000000   # lw a0,0(a1) at 0x80000322
store trap: mcause=0x7  mtval=0xffffffffc0000000   # sw a0,0(a1) at 0x8000036a
Blueprint-style diagram of mtval on access faults: a LOAD TRAP box with mcause 0x5 and a STORE TRAP box with mcause 0x7, both showing mtval = 0xffffffffc0000000; two arrows converge on the one address both traps named, labeled unmapped on the QEMU virt board
Two traps, two mcause codes, one identical witness. Both the load fault and the store fault reported the same mtval.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal M-mode program on QEMU 8.2.2 issued one load and one store at the unmapped address and recorded the mcause/mepc/mtval triple for each trap. Both mtval values came back 0xffffffffc0000000, equal to the address in the register and equal to each other. The disassembly shows the real instructions: the lw encoding 0x0005a503 at 0x80000322, the sw encoding 0x00a5a023 at 0x8000036a, each mepc matching its faulting address exactly. Nineteen checks, zero mismatches, and the machine exited clean. [1]

The bench habit. In your access-fault handler, read mtval first, before you decode anything. A nonzero value names the exact address that faulted; mcause tells you whether it was a load (5) or a store (7); mepc points at the instruction. Without it you would disassemble the faulting instruction at mepc to recover the base register and the offset, which is work the hardware already did for you. One stated limit: a zero mtval means the platform chose not to report, so treat zero as "not told", never as "the fault was at address zero". [2]

One sentence to carry away: when a load or store faults, check mtval before anything else. If it is nonzero, the hardware already named the bad address.

Sources

  1. 01
    src/mtval-fault-address: module PROOF.md and the firsthand runChris Dillinger

    The firsthand QEMU run behind the post: one lw and one sw at the unmapped address 0xffffffffc0000000; the load trap reported mcause 0x5 and the store trap mcause 0x7, and both traps reported mtval 0xffffffffc0000000, equal to the faulting address and to each other. Nineteen checks, zero mismatches, QEMU 8.2.2.

  2. 02
    RISC-V ISA Manual, privileged architecture (mtval, section 3.1.16)RISC-V International

    The mtval contract, read from the pinned release: 'When a trap is taken into M-mode, mtval is either set to zero or written with exception-specific information to assist software in handling the trap.' And the reporting rule: 'If mtval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, page-fault, or hardware-error exception occurs on an instruction fetch, load, or store, then mtval will contain the faulting virtual address.'

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app