Chris Low-level systems developer
← Back to all posts

The Bit That Lets the Supervisor Read User Memory

ISA

SUM is bit 18 of sstatus, defined by the privileged architecture, and the article verifies the exact fault rule the manual states for U=1 pages.

MICROARCH

No microarchitectural claim is made: the behavior was measured on QEMU's functional Sv39 page-walk model, not on silicon.

FIRMWARE

The M-mode boot code is a test harness that sets up tables and delegates the fault; the article's mechanism is the S-mode SUM gate, not firmware behavior.

KERNEL

The article's final paragraph is the kernel's real alternative: xv6 copyin walks the user page table with walkaddr and memmoves from the resolved physical address, never setting SUM.

ALGORITHM

No algorithm is presented; the post measures a single architectural permission bit through fault and load probes.

I put a canary word on a U=1 user page at virtual address 0x40000000, dropped a bare-metal RISC-V machine into S-mode with SUM, bit 18 of sstatus, clear, and ran one ld from that page. Exactly one trap: scause 0xd, a load page fault, with stval 0x40000000 naming the faulting address. The destination register had been poisoned to zero before the load and stayed zero, so the load never completed [2].

Then I set bit 18 with csrs sstatus and ran the identical load again. The canary came back, 0xc0decafe5ca1ab1e, with no new trap. The full bench is 31 checks, 0 mismatches, on QEMU 8.2.2: the SUM=0 phase faulted exactly once, and the SUM=1 phase read the page cleanly [2].

A brass key inserted in a door lock, the rest of the door softly out of focus
One bit, one door: SUM opens supervisor data access to user pages, and nothing else.
Photo: Shixart1985 (CC BY 2.0), via Wikimedia Commons

The boundary is fetch. With SUM still set, I jumped into the same user page, and the machine took one instruction page fault: scause 12, with sepc and stval both 0x40000000. The privileged manual states it as an absolute: "S-mode can never execute instructions from user pages, regardless of the state of SUM." [1] The bit rewrites the data rule for loads and stores, and never the fetch rule.

This is the kernel's everyday question: how does the supervisor touch user memory without becoming it? xv6 answers by never asking. Its copyin walks the user page table with walkaddr, resolves the user virtual address to a physical address, and memmoves from the physical address in the kernel's own mapping, so the kernel never performs a supervisor-mode load from a user virtual address at all [3]. SUM exists for kernels that do ask: the manual pairs the bit with the absolute fetch ban as the two mechanisms that keep supervisor data access controlled and supervisor execution of user pages impossible at all times [1]. The bit is a data door, never an execution door.

Sources

  1. 01
    The RISC-V Instruction Set Manual, Volume II: Privileged ArchitectureRISC-V International

    Defines sstatus.SUM (bit 18): when SUM=0, S-mode accesses to U=1 pages fault; when SUM=1 they are permitted; and states that S-mode can never execute instructions from user pages, regardless of the state of SUM.

  2. 02
    src/sstatus-sum: sstatus.SUM gate on U-page loadsChris Dillinger, riscv-baremetal-demo

    The firsthand experiment behind this post: SUM=0 faults exactly once (scause 0xd, stval 0x40000000, load never completes), SUM=1 returns the canary 0xc0decafe5ca1ab1e with no new trap; 31 checks, 0 mismatches on QEMU 8.2.2.

  3. 03
    xv6-riscv kernel/vm.c: copyinMIT PDOS

    xv6's copyin walks the user page table with walkaddr, resolves the user virtual address to a physical address, and memmoves from the physical address, so the kernel never performs a supervisor-mode load from a user virtual address.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app