Chris Low-level systems developer
← Back to all posts

The Alarm That Will Not Leave Its Room

Relevance by layertest log
ISA

matters at ISA because the enable/pending split and the WARL delegation behavior are the privileged architecture's own contracts: interrupt i is taken only when mstatus.MIE, mie[i], and mip[i] are all set, and mideleg is write-any-read-legal, so the machine may legally refuse the bit-7 write; the modules verified both with real CSR reads and writes: mip 0x80 stayed silent under mie 0x0, mcause read 0x8000000000000007 under mie 0x80, and every mideleg bit-7 write read back 0x1444 with the bit dropped.

MICROARCH

not at Microarch because both modules' verdicts are contract rules about register state, identical on every conforming implementation: no timing, pipeline, or cache behavior was measured, and none could change whether the trap fires or whether the delegation bit takes.

FIRMWARE

matters at Firmware because mie.MTIE and mideleg are writable only in M-mode, so every action under test belongs to firmware: the 64-bit arming write, the handler's all-ones disarm, the mideleg write probes, and the drop-to-S-mode observation are all M-mode operations, and on this machine the decision to hear the alarm, and the discovery that it cannot be delegated, sits in firmware's hands.

KERNEL

not at Kernel because the machine timer interrupt can never reach S-mode on this machine: mideleg bit 7 is read-only zero, so the armed timer trapped in M-mode with zero S-mode arrivals; a supervisor kernel's behavior is invariant under this bit, and its own timer arrives through the supervisor interrupt path, which the modules never programmed.

ALGORITHM

not at Algorithm because the enable gate and the WARL refusal are the architecture's privilege mechanism, not a portable technique: with no M-mode enable register and no delegable-bit contract the claim has nothing to mean, and nothing in the verified behavior travels to another machine.

Light a fuse. On a bare-metal RISC-V machine running M-mode firmware, one 64-bit MMIO write of mtime + 5000 ticks lands in the CLINT's mtimecmp register for hart 0 at 0x02004000. The board's mtime runs at 10 MHz, so 5000 ticks is exactly 500 microseconds: a half-millisecond fuse, lit in software. Set mstatus.MIE, the master interrupt switch, but write zero to mie, the register with one enable bit per interrupt source. Poll mip, the pending register. Bit 7, MTIP, reads set: the CLINT, the core-local interruptor, lifted it when the running clock crossed the compare. Two million reads of the cycle counter roll by, a window in which an enabled interrupt would have fired within a handful of instructions, and the trap count stays zero. The fuse burned, the alarm rang, and the hart did not move. Then set bit 7 of mie, the MTIE enable. Exactly one trap fires. mcause reads 0x8000000000000007: the top bit says interrupt, the low bits say code 7, machine timer interrupt. The handler's first duty is to write mtimecmp to all ones, lifting the compare above the running clock so the alarm cannot refire. The second quiet window confirms it: the trap count stays exactly one. Three runs, byte-identical transcripts.

A vintage twin-bell alarm clock
The alarm rings, but the room stays shut

Why does the enable bit exist at all? The obvious machine needs only one: the pending bit says the alarm wants attention, so take it. The break is who writes what. The CLINT sets MTIP when the clock crosses the compare; no code writes that bit, the hardware raises it and only a disarm clears it. Firmware writes MTIE: that is the running code deciding, per source, whether it may be interrupted right now. Without the split, arming the timer would be taking it: the write that sets the deadline would guarantee a trap at the deadline, with no way to leave the alarm pending while the work in flight finishes. And the split is what makes the handler's discipline load-bearing rather than polite: clear the source first, then leave. With the gate still open and the source uncleared, the trap would refire the instant the handler returns. Phase C, the trap count staying exactly one after the all-ones write, is the test that the disarm actually disarmed.

The second module asked the next question. If MTIP is just bit 7 of the pending register, can the alarm be handed down to S-mode, where a kernel would receive it? The backlog item assumed yes: set bit 7 of mideleg, the machine interrupt delegation register, and the timer interrupt should arrive in supervisor mode. The measurement said no. mideleg is a WARL register, write any value and read back a legal one, and on this machine bit 7 is read-only zero: writes of 0x0, 0x80, and 0x14c4 all read back 0x1444, the bit dropped every time, matching the earlier mideleg-warl module's published delegable mask. The module dropped the hart to S-mode anyway and armed the timer 500 ticks out. Exactly one trap, in M-mode, mcause 0x8000000000000007, zero S-mode arrivals. On QEMU 8.2.2's virt board, the machine timer interrupt has no other room to go to. The honest scope: this is the emulator's CSR model, not silicon; real silicon publishes its own delegable set.

The checks that carry the argument, verbatim from the two modules:

check(trap_count == 0, "trap fired while mie.MTIE was clear");
check(mie == MIE_MTIE, "mie readback != 0x80 after csrs mie, 0x80");
check((rd_bit7 & MIDELEG_MTIP_BIT) == 0, "mideleg bit 7 took on readback");
check(s_regs[0] == 0, "an S-mode trap fired: the interrupt was delegated");

Twenty checks per module, zero failures. Three runs of the gate module on QEMU 8.2.2 produced byte-identical transcripts, checksummed identically. That is what a tight test looks like: the expected outputs are small integers, a trap count of 0 or 1, a cause code, a register readback, so there is nowhere for nondeterminism to hide.

The module's notes contain a debugging lesson worth noting. The first routing build armed the timer 500 ticks out, 50 microseconds at the board's 10 MHz mtime, then printed its arm line before reading the armed value back. One UART line at 115200 baud takes about seven milliseconds, 139 timer expiries, so the readback measured history, not the arm. The fix reads the armed value immediately after the arm write, before any slow output. On bare metal, ordering a measurement around slow I/O is the experiment's design, not its plumbing. One sentence to carry away: a pending interrupt is a fact, an enabled interrupt is a permission, and some interrupts cannot be delegated to a lower privilege mode at all; check the delegation mask before assuming the interrupt will arrive where you want it.

Related work: crash triage is $250/symptom, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app