The Bit That Lets Machine Mode Read as User Mode
The claim is the mstatus.MPRV access rule as this machine shows it: with MPRV set and MPP=U, an M-mode load from a deny-all PMP page traps as a load access fault with mepc at the probe site, and with MPRV clear, or with MPP=M, the same load returns the canary. The run shows each side directly: phase A returned 0xc0ffee1234567890 with zero traps, phase B trapped once with mcause 0x5 at 0x800005ee and mtval 0x80002000, and phase C returned the canary again.
No claim depends on how any core implements the MPRV check. QEMU serves as a functional executor only; no pipeline or execution-unit behavior is asserted or measured, and the honest limit states the result is emulator-observed, not silicon-proved.
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 mstatus MPRV bit's own access rule.
No kernel code runs in the claim. The binary is a bare-metal M-mode program with no supervisor, no scheduler, and no system call path; the U-mode check is a privilege field in mstatus, not a guest operating system.
The claim is one fixed switch and one fixed probe load with trap comparisons, not a computation proved against an independent oracle. The results are shown as the consequence the MPRV rule forces, not as a verified algorithm.
A machine-mode monitor that wants to check a guest's pointer has a problem: its own loads bypass the protections the guest lives under. M-mode ignores unlocked PMP entries, so a load from a page the guest cannot touch still succeeds for the monitor. One bit in mstatus closes that gap. MPRV, bit 17, makes M-mode loads and stores take the privilege level in MPP instead of M-mode, so the monitor's load is checked exactly as the guest's would be.
Measured on QEMU 8.2.2, bare metal in M-mode. The program fenced a 4 KiB page at 0x80002000 with one unlocked PMP entry, R=W=X=0, then ran the same load three ways. MPRV clear, MPP=U: the load returned the canary 0xc0ffee1234567890, zero traps. MPRV set, MPP=U: the same load trapped once, mcause 0x5, mepc 0x800005ee, the probe site itself, mtval 0x80002000, and the destination kept its poison value 0xbadc0de0badc0de0. MPRV set, MPP=M: the load returned the canary again, zero new traps. 20 checks, 0 mismatches. RESULT: PASS. [1]
The probe, three ways:
csrc mstatus, t1 # t1 = MPP mask: MPP=U, the guest's mode
csrs mstatus, t0 # t0 = 1<<17: MPRV set
ld s3, 0(s2) # s2 = 0x80002000: traps, mcause 0x5
csrc mstatus, t0 # MPRV clear: machine-mode checks again
Why the handler clears the bit first. While MPRV is set with MPP=U, the trap handler's own stack saves are checked as U-mode accesses too, so a handler that saves registers before clearing the bit faults on its own first store. This module's entry stashes t0 in mscratch, then clears MPRV with csrc before the first sd. CSR accesses touch no data memory, so that sequence is safe while the bit is still set. The bit changes the rules for every explicit load and store, including the handler's own. [1]
The bench habit: when a monitor reads memory on a guest's behalf, set MPRV and MPP to the guest's mode first, and let the hardware refuse what the guest could not touch. A load that traps under MPRV is a pointer the guest could not have used either, and a load that returns is one the guest could have made itself. Never read guest memory with machine-mode privilege and assume the result means the guest could have read it.
One sentence to carry away: one bit lets machine mode borrow the guest's permissions for a single load, and a trap under that bit means the guest could not have made the load either.
Diagram: hand-built by Chris Dillinger for this post
Sources
- 01src/mstatus-mprv-load: module PROOF.md and the QEMU runChris Dillinger
The QEMU 8.2.2 run behind the post, re-run firsthand for this post: boot mstatus 0xa00000000, pmpcfg0 0x08 over 4 KiB at 0x80002000; phase A (MPRV=0, MPP=U) value 0xc0ffee1234567890, traps 0; phase B (MPRV=1, MPP=U) new traps 1, cause 0x5, mepc 0x800005ee, mtval 0x80002000, destination kept 0xbadc0de0badc0de0; phase C (MPRV=1, MPP=M) value 0xc0ffee1234567890, new traps 0; phase D value 0xc0ffee1234567890; 20 checks, 0 mismatches; checksum 0xcaa47691de34383c; RESULT: PASS.