The Bit That Reroutes a Trap
matters at ISA because the bit itself is the machine contract: the privileged architecture defines medeleg, assigns cause 9 to a supervisor environment call, and says a set bit 9 routes that trap to S-mode. The module checked the contract's exact shape: identical ecall, two destinations, one bit.
not at Microarch because the destination flip is a contract property, identical on every conforming implementation: no pipeline, cache, or branch-predictor behavior was measured or asserted, and none could change which handler the trap lands in.
matters at Firmware because firmware is the layer that holds the pen: medeleg is only writable in M-mode, so the decision to delegate is firmware policy, and the module's phase-two setup (write 0x200, read back bit 9 set) is the firmware action under test.
matters at Kernel because the delegated trap is the kernel's syscall path: with the bit set, a supervisor kernel fields its own environment calls directly instead of receiving them forwarded down from M-mode, which is the hot path the delegation exists to shorten.
not at Algorithm because the mechanism is the architecture's privilege rule, not a portable technique: a trap destination switch has no meaning on a machine without M-mode, S-mode, and a delegation register, and nothing in the verified behavior travels.
Run the same instruction twice. Boot a bare-metal RISC-V machine, drop from machine mode into supervisor mode, and execute ecall, the instruction that asks the layer above for a service. With the medeleg register at zero, the trap lands in the machine-mode handler: its cause register reads mcause = 9, the code for an environment call from supervisor mode. Write 0x200 to medeleg, which is bit 9 set, and execute the identical ecall again. This time the machine handler never fires. The supervisor-mode handler catches the trap instead, with scause = 9. Same instruction, two destinations, one bit of difference.
The bit exists because of who pays for every trap. The kernel runs in supervisor mode, and a system call is the most frequent request a machine fields: every file read, every byte on the terminal, every new process. Without delegation, each of those traps would rise to machine mode first, usually firmware, which would then have to forward it back down to the kernel that knows what to do with it. The obvious fix is a forwarder in firmware, but it sits on the hottest path in the system and would have to understand the kernel's business. Delegation removes the middleman: bit 9 of medeleg says supervisor environment calls belong to the supervisor, and the trap goes straight there. Trust, expressed as one bit.
The mechanism, step by step. RISC-V runs code in privilege modes: M-mode, machine mode, the most privileged, where firmware boots; and S-mode, supervisor mode, where an operating system kernel runs. A trap is the hardware interrupting whatever is running and jumping to a handler; each handler reads its mode's cause register, mcause or scause, to learn what happened. medeleg is an M-mode register with one bit per trap cause: when a bit is set, traps of that cause are delegated to S-mode instead of landing in M-mode. The test checks the contract from both sides. Phase one writes 0 to medeleg, reads back 0, and demands exactly one trap in the M-mode handler with mcause = 9 and the resume address mepc pointing at the captured ecall address. Phase two writes 0x200, reads back bit 9 set, and demands exactly one trap in the S-mode handler with scause = 9 and the supervisor's own resume address sepc at the same ecall.
The check that carries the argument is a list of small exact checks:
check((p2_medeleg_rb & (1UL << 9)) != 0,
"phase-2 medeleg readback missing bit 9");
check(s_regs[0] == 1, "phase-2 S-mode trap did not fire exactly once");
check(s_regs[2] == SCAUSE_S_ECALL,
"phase-2 scause != environment call from S-mode (9)");
Fourteen checks in all: the delegation readbacks, the exact trap counts per mode per phase, the cause codes, the resume addresses matching the captured ecall, the privilege records in mstatus and sstatus confirming arrival from supervisor mode, a cross-phase proof that no trap leaked into the wrong handler, and a quiet window where the counts were required not to move. Three runs on QEMU 8.2.2 produced byte-identical FNV-1a checksums over the verdict values. That is what a tight test looks like: the expected outputs are small integers, a trap count of 1 or 0, so there is nowhere for nondeterminism to hide.
Two honest boundaries. The module ran on QEMU's model of a hart, not on silicon, so it proves the contract as the emulator implements it: the specification's text, rendered in software. And the backlog item that ordered this test said the phase-one cause would be 0xb; the test asserts 0x9 instead, because the specification assigns 0xb to an environment call from M-mode and 9 to one from S-mode, and this ecall runs in S-mode. One sentence to carry away: the destination of a supervisor's system call is one bit in a register, and setting it is how the firmware gets out of the way.