Chris Low-level systems developer
← Back to all posts

The Page Table Edit That Changed Nothing

ISA

The claim is the privileged specification's SFENCE.VMA contract: the fence orders prior page-table stores before the hart's subsequent implicit translation reads, and invalidates the address-translation cache. The run shows both faces on one hart: the PTE rewrite with no fence reads stale, the same load after sfence.vma reads fresh.

MICROARCH

No claim depends on how any core implements its translation cache. QEMU serves as a functional executor only; no cache geometry, pipeline, or timing behavior is asserted or measured, and the stale phase is the architecture-permitted behavior the fence exists to order.

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 fence is executed directly by the test's own M-mode code.

KERNEL

No kernel takes part at any step. The binary runs on bare QEMU with no operating system; the S-mode phases are a test payload, and no system call or kernel data structure appears in the argument.

ALGORITHM

The claim is one fixed ordering semantic exercised as three fixed phases with readback, not a computation proved against an independent oracle. The stale-then-fresh contrast is shown as the consequence the fence contract forces, not as a verified algorithm.

One virtual address, 0x40000000. A hand-built Sv39 page table maps it to a physical page holding the canary 0xaaaaaaaaaaaaaaaa. A supervisor-mode program loads the word and gets 0xaaaaaaaaaaaaaaaa back. That walk does something invisible too: the hart caches the translation, 0x40000000 goes to the canary-A page, in its address-translation cache. [1]

Now machine mode rewrites the leaf page-table entry in memory, repointing the address at a second page holding 0xbbbbbbbbbbbbbbbb. The PTE readback proves the write landed: 0x200010c7 before, 0x20000cc7 after. No fence is issued. The supervisor program loads the address again and gets 0xaaaaaaaaaaaaaaaa. The edit changed nothing the hart could see. The cache still holds the old walk, and the architecture permits the hart to keep using it. [1]

The three phases, reduced to their witness:

pte:    0x200010c7 -> 0x20000cc7   # the rewrite landed in memory
phaseB: readback 0xaaaaaaaaaaaaaaaa # stale: no fence issued
phaseC: readback 0xbbbbbbbbbbbbbbbb # fresh: after sfence.vma
Blueprint-style diagram of the sfence.vma remap experiment in three phases: phase A loads 0xaaaaaaaaaaaaaaaa from virtual address 0x40000000 and the hart caches the translation; M-mode rewrites the leaf PTE from 0x200010c7 to 0x20000cc7 with no fence and the next load still returns 0xaaaaaaaaaaaaaaaa, stale; after one sfence.vma the same load returns 0xbbbbbbbbbbbbbbbb, fresh
The PTE rewrite landed in memory (the readback proved it) but the hart kept its cached translation until one sfence.vma forced a fresh walk.
Diagram: hand-built by Chris Dillinger for this post

Then machine mode executes one sfence.vma. The same load now returns 0xbbbbbbbbbbbbbbbb. Note what the fence did not do: it did not move the write. The write was already in memory before the fence ran. The fence invalidated the hart's cached translation, so the next load walked the table again and found the new leaf. The privileged manual, section 12.2.1, states the contract: the fence guarantees that previous stores already visible to the hart are ordered before its subsequent implicit reads of the memory-management data structures, and it invalidates entries in the address-translation cache. [1] [2]

The bench habit: after every page-table edit, a map, an unmap, a permission change, issue sfence.vma before the address is used again. One boundary from the manual: the fence orders only the local hart, so every hart that may hold the entry needs its own fence, which is why operating systems do TLB shootdowns with interprocessor interrupts. [2] One boundary measured: the manual explicitly permits over-fencing, an implementation may ignore the address in rs1 and flush everything. The sibling run on QEMU 8.2.2 confirmed it: a scoped sfence.vma naming one address freshened both test addresses. Scoping is a performance hint, not a correctness tool you can verify on every implementation. [3]

One sentence to carry away: the PTE rewrite lands in memory; only sfence.vma lands it in the hart.

Sources

  1. 01
    src/sfence-vma-remap: module PROOF.md and the firsthand runChris Dillinger

    The firsthand QEMU 8.2.2 run behind the post: TEST_VA 0x40000000 mapped to the canary-A page; M-mode rewrote the leaf PTE, readback 0x200010c7 to 0x20000cc7, with no fence; the S-mode load returned stale 0xaaaaaaaaaaaaaaaa; after one sfence.vma the same load returned fresh 0xbbbbbbbbbbbbbbbb. 39 checks, 0 mismatches, byte-identical across runs; the post's own rebuild and rerun on the build VM reproduced the PASS and the checksum.

  2. 02
    RISC-V ISA Manual, privileged architecture (SFENCE.VMA, section 12.2.1)RISC-V International

    The fence contract, read from the pinned release, section 12.2.1: executing SFENCE.VMA guarantees that previous stores already visible to the hart are ordered before subsequent implicit references to the memory-management data structures, and the instruction invalidates entries in the address-translation cache. The same section states the fence orders only the local hart and that over-fencing is always legal.

  3. 03
    src/sfence-vma-rs1-scoped: module PROOF.mdChris Dillinger

    The sibling run behind the post's stated limit: sfence.vma with rs1 naming one address freshened both test addresses on QEMU 8.2.2, so the scoped form behaved as a full invalidation; 60 checks, 0 mismatches. The manual's over-fencing note predicts exactly this latitude.

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