The Trap You Plant on Purpose
The claim is the breakpoint trap contract from the unprivileged specification: executing ebreak raises a precise trap with mcause 3 and mepc holding the breakpoint's address, in both the 4-byte and the compressed 2-byte encodings. The run fires one of each and reads back the identical trap shape, with the resume landing 4 bytes past the ebreak and 2 bytes past the c.ebreak.
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 breakpoint contract is complete from the unprivileged specification and the run.
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.
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 two fixed trap semantics exercised as two planted tripwires with register readback, not a computation proved against an independent oracle. The plant-it-on-the-can't-happen-path habit is shown as the consequence the contract forces, not as a verified algorithm.
Every bare-metal program has a path that should never execute: the default case the hardware manual says is impossible, the error return that cannot happen. The usual treatment is an empty infinite loop, and when the impossible happens you get silence. The hart spins, and nothing tells you which loop it died in. There is a one-instruction alternative: plant a breakpoint on the path and let the hardware report the exact address.
The instruction is ebreak. Section 2.8 of the unprivileged manual says it plainly: EBREAK is used to return control to a debugging environment, and it was designed for a debugger to make execution stop and fall back into the debugger. When the hart executes it, it traps with mcause 3, the breakpoint code, and mepc holds the address of the ebreak itself. A run on QEMU 8.2.2 executed one ebreak at 0x800002fe: the handler recorded mcause 0x3 and mepc 0x800002fe, the word at that address read back 0x00100073, the ebreak encoding, and execution resumed 4 bytes later at 0x80000302. [1] [2]
The trap record, reduced to its witness:
tripwire: ebreak at 0x800002fe # encoding 0x00100073
trap: mcause=0x3 mepc=0x800002fe # the hardware names the PC
resume: 0x80000302 # 4 bytes past the tripwire
Diagram: hand-built by Chris Dillinger for this post
There are two spellings, and the length is the part that bites. The compressed form, c.ebreak, is two bytes, encoding 0x9002. The same run fired it at 0x80000320: mcause 0x3 again, mepc 0x80000320, the halfword at that address 0x9002, resume 2 bytes later at 0x80000322. A handler that steps over the tripwire must skip the right length for the form planted, 4 for ebreak, 2 for c.ebreak, or it resumes in the middle of the next instruction. [1]
The C spelling is the one you will actually plant. The manual notes that EBREAK is used by the standard GCC compiler to mark code paths that should not be executed, and the current spelling of that is __builtin_trap(): compiled at -O2 it emits the compressed c.ebreak, a single 0x9002 sitting in .text.unlikely. So the can't-happen path in C is one builtin call, and the trap still arrives with mcause 3 and mepc naming it. Thirteen checks across the run, zero mismatches. [1] [2]
The bench habit. On a can't-happen path, plant __builtin_trap() instead of an empty loop. When the impossible happens, your trap handler reads mepc and you know the exact address that died, instead of hunting every parked loop in the binary. One stated limit: this names where the hart stopped, not why. The why is still your bug to find. [2]
One sentence to carry away: a planted breakpoint turns a silent hang into a trap that names its own address.
Sources
- 01src/ebreak-tripwire: module PROOF.md and the firsthand runChris Dillinger
The firsthand QEMU run behind the post: one 4-byte ebreak at 0x800002fe and one 2-byte c.ebreak at 0x80000320, each trapped with mcause 0x3 and mepc equal to the tripwire address; the word at the first mepc read 0x00100073 and the halfword at the second 0x9002; resumes landed 4 and 2 bytes past. Thirteen checks, zero mismatches, QEMU 8.2.2, three identical runs. The PROOF also records that __builtin_trap() compiled at -O2 emits the single 0x9002 c.ebreak in .text.unlikely.
- 02RISC-V ISA Manual, unprivileged architecture (EBREAK, section 2.8; C.EBREAK, C-extension chapter)RISC-V International
The breakpoint contract, read from the pinned release, section 2.8: 'The EBREAK instruction is used to return control to a debugging environment.' And the planting license: 'EBREAK was primarily designed to be used by a debugger to cause execution to stop and fall back into the debugger. EBREAK is also used by the standard GCC compiler to mark code paths that should not be executed.' The C-extension chapter: 'Debuggers can use the C.EBREAK instruction, which expands to ebreak, to cause control to be transferred back to the debugging environment.'