Chris Low-level systems developer
← Back to all posts

The Registers MRET Will Not Restore

ISA

The claim is about the xRET instruction's contract: mret restores the pc from mepc and pops the privilege stack (MPIE into MIE, MPP into the privilege mode), and it restores no general registers. The run executes one ecall and shows all eight argument registers, a0 through a7, coming back with the handler's values after mret, proving the no-restore half of the contract by readback.

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 restore contract is complete from the instruction semantics and the run.

FIRMWARE

No firmware service takes part in the claim. The M-mode trap handler is the experiment subject, written for the test; the behavior under test is the mret instruction's own contract, and no boot service or supervisor interface appears in the argument.

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 instruction semantic exercised as one fixed before-and-after readback, not a computation proved against an independent oracle. The save/restore idiom is shown as the consequence of the contract, but nothing here proves any handler discipline correct.

A trap is not a function call. When the ecall landed, the hardware set aside exactly three things: mcause (0xb, an M-mode environment call), mepc (0x80000556, the address of the ecall), and the privilege stack in mstatus, the old privilege in MPP and the old interrupt enable in MPIE. It set aside none of the general registers. The eight values in a0 through a7 still belonged to the interrupted program, and they were still sitting in the registers when the handler started. [1]

The handler wrote eight new sentinels into a0..a7 and executed mret. The specification's contract for xRET fits in two clauses: it manipulates the privilege stack (MPIE back into MIE, MPP back into the privilege mode) and sets the pc to the value in mepc. Register save and restore are not in either clause, because they are not the hardware's job. After mret, every one of the eight registers read back the handler's value: a0 went from 0x1111111111111111 to 0x9999999999999999, a1 from 0x2222222222222222 to 0xaaaaaaaaaaaaaaaa, down to a7 from 0x8888888888888888 to 0x123456789abcdef0. Nothing was restored, because nothing was saved. [1] [2]

The handler's end, the part that never puts anything back:

    li a0, POST_A0        # the handler's own sentinel set
    ...
    li a7, POST_A7
    mret                  # pc and privilege return; a0..a7 stay as written
Blueprint-style diagram of the mret no-restore behavior: on trap entry the hardware saves mepc, mcause, and the privilege stack but no general registers; after the handler writes a0..a7 and executes mret, the pc and privilege return while all eight registers still carry the handler's sentinels
mret returns the pc and the privilege stack. a0..a7 keep whatever the handler last wrote; nothing is restored.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal M-mode program on QEMU 8.2.2 loaded eight sentinels into a0..a7, recorded them, issued one ecall, and read the registers back after mret. mie read 0x0 at boot and mstatus.MIE was cleared, so the ecall was the only trap the run could take. One trap, eight readbacks, each matching the handler's sentinel set and none matching the pre-trap value. One honest boundary: the executor is QEMU's machine model, faithful, but still a model, not silicon. [1]

The habit this buys: a handler parks every register it touches and puts each one back before mret, or the interrupted code resumes with your values in its registers. The save and restore are a convention the handler owns; the hardware signs only for the pc and the privilege stack. [2]

Sources

  1. 01
    src/mret-no-restore: module README and PROOF.mdChris Dillinger

    The firsthand QEMU run behind the post: one M-mode ecall (mcause 0xb, mepc 0x80000556), the handler writes eight sentinels into a0..a7 and returns with mret and no restore; all eight registers read back the handler's values, none the pre-trap values. Ninety-five checks, zero mismatches, verified again on this run.

  2. 02
    RISC-V ISA Manual, privileged architecture (trap return)RISC-V International

    The xRET contract: manipulates the privilege stack (xPIE into xIE, xPP into the privilege mode) and sets the pc to the value in xepc. General register save/restore is not part of the instruction's defined behavior.

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