Chris Low-level systems developer
← Back to all posts

The Bit That Traps the Page Table

ISA

The claim is the mstatus.TVM access rule as this machine shows it: with TVM set, an S-mode satp write, satp read, and sfence.vma each trap as illegal instruction with mepc at the exact probe site, and with TVM clear the identical pad takes no trap but the closing ecall. The run shows each side directly: mstatus read back 0xa00100000 with the bit set, three mcause 0x2 traps at 0x800002cc, 0x800002d0, and 0x800002e0, and one ecall trap in each phase.

MICROARCH

No claim depends on how any core implements the TVM trap. 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.

FIRMWARE

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 TVM bit's own gating rule.

KERNEL

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 hypervisor use is named as the reason for the switch, not as a behavior this run measures.

ALGORITHM

The claim is one fixed switch and one fixed probe pad with trap comparisons, not a computation proved against an independent oracle. The results are shown as the consequence the TVM rule forces, not as a verified algorithm.

One bit in mstatus decides whether supervisor mode may touch virtual-memory control. TVM, bit 20, is the trap switch: set it, and three ordinary S-mode operations trap to M-mode instead of running. Clear it, and the same three operations run normally. The bit lives in M-mode only. S-mode cannot read it or change it; sstatus carries no TVM bit. It can only feel the effect.

Measured on QEMU 8.2.2, bare metal in M-mode. The program set TVM with one csrs and read mstatus back as 0xa00100000, bit 20 set and nothing else disturbed. An S-mode pad then tried three operations: write satp, read satp, execute sfence.vma. All three trapped as illegal instruction, mcause 0x2, with mepc at the exact probe site each time: 0x800002cc, 0x800002d0, 0x800002e0. A closing ecall made the fourth trap, mcause 0x9. The trapped satp write had no effect: satp still read 0 from M-mode. With TVM clear, the identical pad took one trap only, the closing ecall, and the satp write stuck. 19 checks, 0 mismatches. RESULT: PASS. [1]

The three traps, side by side:

csrs mstatus, t0  # t0 = 1 << 20: TVM set, bit 20
# S-mode csrw satp, zero: trap, mcause 0x2, mepc 0x800002cc
# S-mode csrr t1, satp: trap, mcause 0x2, mepc 0x800002d0
# S-mode sfence.vma: trap, mcause 0x2, mepc 0x800002e0

Why the read matters. The first guess was that only the write traps, and the first run proved that guess wrong: the S-mode satp read trapped too, at 0x800002d0, with the same illegal-instruction cause. The specification covers attempts to read or write satp, and this hart follows the letter of it. A hypervisor that sets TVM to trap a guest's page-table changes must therefore handle the read as well as the write, or the guest's innocent read of satp dies in a trap it never expected. [1]

The bench habit: when you install a trap-and-emulate switch, probe every operation the switch gates, not just the one you named it for. Here the switch is called a write trap in shorthand, and the read was the operation that actually surprised. A pad that tries the read, the write, and the fence, with the switch set and clear, turns a one-line gloss into a measured rule.

One sentence to carry away: one bit makes the page-table register untouchable from S-mode, and the read traps just like the write.

Blueprint-style diagram of the mstatus.TVM trap switch in three panels: THE SWITCH shows mstatus 0xa00000000 at boot and 0xa00100000 after csrs with bit 20 set; SET: THREE TRAPS shows csrw satp trapping with mcause 0x2 at 0x800002cc, csrr satp trapping at 0x800002d0, and sfence.vma trapping at 0x800002e0; CLEAR: NO TRAPS shows the same pad taking one trap only, the closing ecall, with 19 checks and 0 mismatches
TVM set: the satp write, the satp read, and sfence.vma all trap. TVM clear: the same pad takes no trap but the closing ecall.
Diagram: hand-built by Chris Dillinger for this post

Sources

  1. 01
    src/mstatus-tvm-trap: module PROOF.md and the QEMU runChris Dillinger

    The QEMU 8.2.2 run behind the post, re-run firsthand for this post: mstatus at boot 0xa00000000, after TVM set 0xa00100000; phase 1 traps 4: mcause 0x2 at 0x800002cc (satp write), 0x2 at 0x800002d0 (satp read), 0x2 at 0x800002e0 (sfence.vma), 0x9 at 0x800002e4 (ecall); satp after phase 1 0x0; phase 2 new traps 1, ecall only, satp readback 0x0; 19 checks, 0 mismatches; RESULT: PASS.

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app