Chris Low-level systems developer
← Back to all posts

LATEST

The Fence That Names Only Its Top

ISA

The claim is the PMP TOR addressing rule as this machine shows it: in TOR mode each entry's region runs from the previous pmpaddr to its own, so pmpaddr1 = 0x20000c00 ends the denied region at 0x80003000 and pmpaddr0 = 0x20000800 ends the allowed region at 0x80002000. The run shows each side directly: the load at 0x80001fff completed with no trap and returned the 0x5a sentinel, and the load at 0x80002000 trapped once with mcause 0x5, mepc 0x80000272, and mtval 0x80002000.

MICROARCH

No claim depends on how any core implements the TOR match. 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 PMP TOR addressing rule itself.

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 fence is a hardware PMP entry, not an operating system service.

ALGORITHM

The claim is one fixed addressing rule with boundary probes, not a computation proved against an independent oracle. The results are shown as the consequence the TOR rule forces, not as a verified algorithm.

A fence usually names both of its edges: the bottom where protection starts and the top where it ends. A PMP entry in TOR mode names only the top. The bottom comes from the entry below it. pmpaddr1 holds the top of its region; the region starts where pmpaddr0 ends. Entry 0 has no entry below it, so its region starts at address 0.

Measured on QEMU 8.2.2, bare metal in M-mode. The program wrote pmpaddr0 = 0x20000800 and pmpaddr1 = 0x20000c00, then pmpcfg0 = 0x880f: entry 0 TOR with R=W=X=1, entry 1 locked TOR with R=W=X=0. Entry 0 covers [0x0, 0x80002000) and entry 1 covers [0x80002000, 0x80003000), a 4 KiB scratch buffer. A load of the last allowed byte at 0x80001fff completed with no trap and returned the 0x5a sentinel written before programming. A load of the first denied byte at 0x80002000 trapped once, mcause 0x5, mepc 0x80000272, the faulting load itself, mtval 0x80002000. The same boundary, probed three times, gave the same verdict every time. RESULT: PASS. [1]

The programming, two addresses and one config word:

csrw pmpaddr0, s1  # s1 = 0x20000800: entry 0 ends at 0x80002000
csrw pmpaddr1, s2  # s2 = 0x20000c00: entry 1 ends at 0x80003000
csrw pmpcfg0, s0   # s0 = 0x880f: entry 0 TOR allow, entry 1 locked TOR deny

Why entry 1 is locked. An unlocked PMP entry does not restrict M-mode at all, and this program runs in M-mode, so an unlocked deny entry would have let both probes through and proved nothing. The lock bit is what makes the fence bind the most privileged mode. The program verified the lock instead of assuming it: after programming, a write of zero to pmpcfg0 left entry 1 at 0x88 while entry 0 cleared to 0x00, and the boundary probes ran after that clear, so the denial came from the locked entry alone. [1]

The bench habit: when you fence a buffer with TOR, program the bottom into the entry below and the top into the fencing entry, then probe the last byte below the boundary and the first byte inside it. If the inside probe passes, check the lock bit before doubting the fence: an unlocked entry never binds M-mode, and a fence that only binds lower modes is a fence with a hole at the top.

One sentence to carry away: in TOR mode a PMP entry names only the top of its fence, the entry below names the bottom, and the lock bit is what makes the fence bind machine mode.

Blueprint-style diagram of the PMP TOR fence in three panels: THE RULE shows pmpaddr names only the top, the region runs from the previous pmpaddr to its own, and entry 0 starts at address 0; BELOW THE LINE: READS shows a load at 0x80001fff completing with no trap and returning the 0x5a sentinel; ON THE LINE: TRAPS shows a load at 0x80002000 trapping with mcause 0x5, mepc 0x80000272, and mtval 0x80002000
Below the boundary the byte reads. On the boundary the load traps. The entry below names the bottom.
Diagram: hand-built by Chris Dillinger for this post

Sources

  1. 01
    src/pmp-tor: module PROOF.md and the QEMU runChris Dillinger

    The QEMU 8.2.2 run behind the post, run firsthand for this post from the module's prebuilt ELF: pmpaddr0 0x20000800, pmpaddr1 0x20000c00, pmpcfg0 0x880f (entry 0 TOR allow, entry 1 locked TOR deny); pmpcfg0 readback 0x880f, after clear attempt 0x8800 (entry 1 byte 0x88 locked, entry 0 byte cleared); lbu at 0x80001fff seen 0, byte 0x5a; lbu at 0x80002000 seen 1, mcause 0x5, mepc 0x80000272, mtval 0x80002000; three runs identical apart from the timeout PID; 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