Chris Low-level systems developer
← Back to all posts

The Thirty-Five Registers of a Rescue Plan

ISA

The rescue plan runs on privileged ISA primitives: sscratch parks a0 when no register is free, sfence.vma fences both satp switches, and sret returns through sepc with SPP and SPIE. The article quotes uservec's csrw sscratch, a0 and the fence pairs from kernel/trampoline.S and names the privileged spec as the contract behind each.

MICROARCH

Nothing in the article measures or reasons about any processor's internals: the trapframe layout, the sscratch swap, and the fence pairs are supervisor-mode code identical on any hart running xv6, so the mechanism is judged at the ISA and kernel layers.

FIRMWARE

Machine mode sets up the stage with stvec and the page tables, but the article never goes there: the catch, the save, and the return are supervisor-mode trap machinery built from the return ticket and thirty-one register slots, none of it firmware behavior.

KERNEL

The entire mechanism is xv6 kernel code: struct trapframe in kernel/proc.h, uservec and userret in kernel/trampoline.S, usertrap and prepare_return in kernel/trap.c, all read firsthand from the xv6-getscount checkout. The return ticket exists so the kernel can suspend a process and set it back down.

ALGORITHM

No algorithm is presented: the article walks a data structure and two assembly routines with fixed offsets and a fixed instruction order, not a procedure with an invariant to prove.

A user process is running. A timer interrupt fires, or the program calls a syscall. The hardware does almost nothing for it: it records the cause, stashes the faulting address, and jumps to a vector. Every register the process was holding is still sitting in the physical registers, and the first real instruction of the trap handler is about to overwrite them. Before the kernel can do a single thing, it needs somewhere to put the process down.

xv6's answer is struct trapframe in kernel/proc.h. Its first five fields are not registers at all: kernel_satp, kernel_sp, kernel_trap, epc, kernel_hartid. They are the return ticket. Only then comes the process itself, all thirty-one integer registers in ABI order, ra at byte 40 through t6 at byte 280 [1]. Every process maps its trapframe at the same virtual address, TRAPFRAME, so the catch code can write li a0, TRAPFRAME and mean it for any process.

Two trapeze artists mid-flight: the catcher holds the flyer upside down under a star-painted circus tent
Double trapeze act, Continental Circus Berlin, Aviemore, 2009: the catcher takes the flyer mid-flight and sets her back down. The trapframe is where xv6 catches a running process mid-stride, thirty-five registers holding everything it was, so sret can set it back down exactly where it was.
Photo: Aaron Sneddon, CC BY, via Wikimedia Commons

The catch is uservec in kernel/trampoline.S, and its first instruction is the whole philosophy: csrw sscratch, a0. On trap entry you have exactly zero free registers, so a0 is parked in sscratch, a swap that needs no scratch space, and only then does the code reach for the trapframe. Thirty-one stores. Then the kernel stack pointer into sp, the hartid into tp, the address of usertrap into t0, the kernel page table into t1. The satp switch is fenced by sfence.vma on both sides, because a TLB entry cached under one page table is a lie under the other [2]. Then jalr t0, into usertrap.

usertrap does the C work. sepc is stashed into the trapframe's epc first, and on the syscall path epc is bumped by four before interrupts are enabled, because the ecall is finished and the return must point past it, and an interrupt would move sepc out from under the bump [1]. prepare_return() rebuilds the trip back: stvec points to uservec again, the four kernel ticket fields are refreshed, SPP is cleared so sret drops to user mode, SPIE is set, sepc is reloaded. userret flushes the instruction cache, switches back to the user page table under the same fence pair, reloads all thirty-one registers, and sret.

The trapframe earns its thirty-five registers by being honest about the constraint. On trap entry the hardware hands you nothing but a0's old value and one constant address. The ticket, the save area, the fences: every part of the rescue plan is built out of that.

Sources

  1. 01
    dillingerstaffing/xv6-getscount kernelChris Dillinger

    struct trapframe layout (kernel/proc.h), uservec and userret (kernel/trampoline.S), usertrap and prepare_return (kernel/trap.c): the five-field return ticket, the thirty-one register slots in ABI order, the sscratch swap, the sfence.vma pairs around both satp switches, the epc bump before intr_on. Read firsthand.

  2. 02
    RISC-V ISA Manual, privileged architectureRISC-V International

    sscratch as the supervisor scratch register for the trap-entry sequence, sfence.vma ordering requirements around satp writes, sret privilege transition and interrupt behavior keyed off sstatus SPP and SPIE, sepc as the saved trap program counter.

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