The Region Described by Trailing Ones
The claim is the PMP NAPOT encoding and matching rule as this machine shows it: one pmpaddr value names a base and a size through its trailing ones, and the matching rule refuses the inside edges while passing one word outside on either side. The run shows each side directly: pmpaddr0 = 0x200009ff read back exactly, loads at 0x80002000 and 0x80002ffc trapping with mcause 0x5, and loads at 0x80001ffc and 0x80003000 passing.
No claim depends on how any core implements PMP matching. 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 PMP CSRs own encoding and matching 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 S-mode view of the probes comes from MPRV set by the M-mode program itself, not from a supervisor running.
The claim is one fixed encoding and a fixed set of boundary probes with trap and value comparisons, not a computation proved against an independent oracle. The results are shown as the consequence the NAPOT rule forces, not as a verified algorithm.
Fencing a scratch buffer usually means checking every access by hand, or trusting a range you never tested. RISC-V physical memory protection gives a shorter way: one PMP entry can fence a whole region, and the fence is described by a single register value whose tail of trailing ones names the size.
Measured on QEMU 8.2.2, bare metal in M-mode. The program wrote pmpaddr0 = 0x200009ff and pmpcfg0 = 0x1f18, fencing 4 KiB at 0x80002000. Loads at the first word inside, 0x80002000, and the last word inside, 0x80002ffc, each trapped with mcause 0x5, and a store at 0x80002000 trapped with mcause 0x7. Loads one word below, at 0x80001ffc, and one word above, at 0x80003000, passed with their canary values, and stores at both outside addresses round-tripped. RESULT: PASS. [1]
The encoding, and how to read it back:
csrw pmpaddr0, t0 # t0 = 0x200009ff: 4 KiB at 0x80002000
csrw pmpaddr1, t1 # t1 = 0x200007ff: 16 KiB allow entry
csrw pmpcfg0, t2 # t2 = 0x1f18: entry 0 deny, entry 1 allow
# 9 trailing ones in 0x200009ff means 2^(9+3) = 4096 bytes
Why there are two entries, not one. An unlocked PMP entry does not restrict M-mode at all, so the probes ran as S-mode accesses, with mstatus.MPRV set and MPP = S. And an S-mode access that matches no entry is denied whenever any entry is programmed, so entry 1, a 16 KiB allow entry at 0x80000000, covers the outside addresses. Inside the deny region, entry 0 wins because the lowest matching entry decides. Without entry 1, the outside probes would have trapped for the wrong reason, and the test would have proved nothing about the edge.
The readback alone would not prove the fence. Reading pmpaddr0 back as 0x200009ff proves the register took the value; only the boundary probes prove what the value matches. The first and last words inside trap, one word outside on either side passes, and mtval names the faulting address in each trap, 0x80002000 for the low edge. That is the whole test: program the value, then touch both edges and one word past each.
The bench habit: when you fence a buffer with PMP, program the deny entry and an allow entry for everything else, then probe the first word, the last word, and one word past each edge. If an outside probe traps, check whether it matched the fence or merely matched nothing; the two failures look identical from the trap alone.
One sentence to carry away: count the trailing ones in a NAPOT pmpaddr value and you know the size of the region it fences, and two probes on either side of each edge tell you whether the fence sits where you think it does.
Diagram: hand-built by Chris Dillinger for this post
Sources
- 01src/pmp-napot-match: module PROOF.md and the QEMU runChris Dillinger
The QEMU 8.2.2 run behind the post, re-run firsthand for this post: pmpaddr0 = 0x200009ff, pmpaddr1 = 0x200007ff, pmpcfg0 = 0x1f18; loads at 0x80002000 and 0x80002ffc trapped with mcause 0x5, a store at 0x80002000 trapped with mcause 0x7; loads at 0x80001ffc and 0x80003000 passed, stores below and above round-tripped; quiet window showed no new traps; RESULT: PASS.