Chris Low-level systems developer
← Back to all posts

The Bit That Sorts the Trap

ISA

The claim is about mcause bit 63 semantics: the top bit marks asynchronous interrupts and stays clear for synchronous exceptions, with the low bits carrying the cause code. The run reads mcause after a real M-mode ecall and a real machine timer interrupt, and checks the bit and the full register value on both.

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 bit rule is complete from the architecture definition 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 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 CSR semantic applied as one fixed idiom, not a computation proved against an independent oracle. The bit rule is shown working across one exception and one interrupt, but nothing here establishes a general algorithm.

Two traps hit the same handler on a bare-metal hart. First an M-mode ecall: the handler read mcause and found 0xb, an environment call from M-mode, with the register's top bit clear. Then a machine timer interrupt, armed by writing mtimecmp 50000 ticks ahead: the handler read mcause and found 0x8000000000000007, a machine timer interrupt with the top bit set. Same register, same handler, one bit of difference between the two visits. [1]

On RV64, bit 63 of mcause is the sorting office. The hardware sets it when the trap is an asynchronous interrupt and leaves it clear when the trap is a synchronous exception. The remaining bits carry the code: 11 for the ecall above, 7 for the timer. The handler's first decision is one test of one bit, and the two answers demand opposite handling. An interrupt wants its source acknowledged and cleared. An exception wants the faulting instruction decoded, with mepc pointing at it. Mix them up and you treat a timer tick like a broken instruction, or you try to step past an interrupt the way you step past a fault. [2]

The whole dispatch, in six lines:

unsigned long cause;
__asm__ volatile("csrr %0, mcause" : "=r"(cause));
if (cause & (1UL << 63))
    interrupt(cause & ~(1UL << 63));  /* async: the low bits are the code */
else
    exception(cause);                 /* sync: mepc holds the faulting PC */
Blueprint-style diagram of the 64-bit mcause register drawn twice: after an M-mode ecall with bit 63 clear (exception, code 11) and after a machine timer interrupt with bit 63 set (interrupt, code 7)
One bit, two stories. mcause reads 0xb after an M-mode ecall, bit 63 clear; it reads 0x8000000000000007 after a machine timer interrupt, bit 63 set. The handler's first branch is that one bit.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal M-mode program installed one direct-mode handler, fired an ecall, then armed the machine timer and waited. Sixteen in-program checks, zero mismatches: the ecall printed mcause=0xb bit63=0, the timer printed mcause=0x8000000000000007 bit63=1, the disarm read back all ones, and no unexpected trap fired in either quiet window. One honest boundary: the executor is QEMU 8.2.2's M-mode model, faithful, but still a model, not silicon. [1]

The habit this buys: branch on the bit before touching anything else. The interrupt half of your handler clears its source first, because a source left armed re-fires the moment you return. The exception half reads mepc first, because the faulting address is the whole story. One bit decides which of those two jobs you are doing, and the register tells you for free. [2]

Sources

  1. 01
    src/mcause-interrupt-bit: module README and PROOF.mdChris Dillinger

    The firsthand QEMU run behind the post: the 16-check sequence, the measured mcause values 0xb and 0x8000000000000007, and the PASS verdict.

  2. 02
    The RISC-V Instruction Set Manual, Volume II: Privileged Architecture (mcause register definition and cause table)RISC-V International

    Defines the mcause Interrupt bit and lists the machine cause codes: 7 for machine timer interrupt, 11 for environment call from M-mode.

Related work: trap handler bring-up and crash triage, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app