Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Appendix A · RISC-V Registers and CSRs

An appendix in this book is a reference, not a chapter: no argument, no narrative, and everything in it either cites a primary source or comes from a stamped result under bench/results/.

Everything on this page is from the RISC-V unprivileged RISC-V International (n.d.) and privileged RISC-V International (n.d.) specifications and the psABI RISC-V International (n.d.), restated for the subset this book uses. It is not a substitute for reading them; it is what to have open while you do.

The integer registers

Thirty-two, each sixty-four bits wide on RV64. x0 is hardwired to zero — writes to it are discarded, which is how the instruction set gets a no-op, a move and a compare-against-zero without spending encodings on them.

The names on the left are the only ones you will see in this book’s listings. The hardware has no opinion about them: the roles below are a convention RISC-V International (n.d.), agreed between compilers. ch14 shows what the convention buys a compiler, and ch16 shows what it is worth to a trap — which is nothing, because a trap is not a party to it.

Registerx numberRolePreserved across a call?
zerox0Hardwired zero—
rax1Return addressNo
spx2Stack pointerYes
gpx3Global pointer— (set once)
tpx4Thread pointer— (set once)
t0–t2x5–x7TemporariesNo
s0/fpx8Saved, or frame pointerYes
s1x9SavedYes
a0–a1x10–x11Arguments, and return valuesNo
a2–a7x12–x17ArgumentsNo
s2–s11x18–x27SavedYes
t3–t6x28–x31TemporariesNo

Two consequences of that table:

Eight argument registers. A call passing eight or fewer word-sized arguments touches no memory to do it, which is most of why ch14’s -O2 listings have no stack frame at all.

“Preserved” means the callee promised. A caller-saved register is not saved by anybody unless the caller needs it afterwards; a callee-saved one is saved by whoever wants to use it. Neither promise binds the hardware, which is why ch16’s trap path saves all thirty-one.

The control and status registers this book touches

CSRs are a separate address space, read and written with their own instructions (csrr, csrw, csrrw and the immediate forms). They are not the integer registers and they are not memory.

The privileged specification RISC-V International (n.d.) defines several dozen. These are the ones this book’s code reads or writes, each with the chapter that meets it.

They come in two sets, because this book runs at two privilege levels. Part II has no kernel under it and runs in machine mode, so it uses the m registers directly. xv6 runs in supervisor mode and uses the s ones, which do the same jobs one level down.

Machine-mode CSRWhat it isWhere
mtvecThe address the hardware jumps to on a machine trapch06
mepcThe address of the instruction that trapped; mret returns herech06, ch07
mcauseWhy the trap happened, with the interrupt bit at the topch06, ch07
mstatusMachine status: previous privilege in MPP, previous interrupt-enable in MPIEch07

mepc points at the instruction, not past it. ch06’s first problem turns on that, and so does the rule that a handler for ecall has to advance it and a handler for an interrupt must not.

Supervisor-mode CSRWhat it isWhere
stvecThe address the hardware jumps to on a supervisor trapch16
sepcThe PC at the moment of the trap; sret returns herech16
scauseWhy the trap happened, as a code with the interrupt bit at the topch16, ch19
sstatusSupervisor status: previous privilege, previous interrupt-enablech16, ch19
sscratchOne word the trap path may use before it has a usable registerch16
stvalThe faulting address, for a fault that has onech18
satpThe root page table and the translation modech17
sie / sipWhich interrupts are enabled, and which are pendingch19
timeThe real-time counter — an unprivileged CSR, read here by the supervisor-mode driverch19

scause’s top bit is the one to read first. It separates an interrupt — something outside the program asking for attention — from an exception, which is this instruction refusing to complete. The rest of the field means different things in the two cases, and ch19 is about why the distinction is structural rather than a numbering convenience.

sscratch exists because of a bootstrapping problem. A trap handler needs a register to work with and every register currently belongs to the interrupted program. sscratch is the one place to put something that survives the swap, and ch16 traces exactly what xv6 keeps there.

Sv39, in one place

The paging scheme this book uses. ch17 derives it and measures the walk; this is the field layout to keep beside that chapter.

A virtual address is thirty-nine bits of meaning: three nine-bit indices and a twelve-bit offset. A page-table entry is sixty-four bits: flags at the bottom, a physical page number above them.

FieldBitsWhat it decides
V0Whether the entry means anything at all
R W X1–3Readable, writable, executable. All three clear means “this points at another table”
U4Reachable from user mode
G5Global — the same in every address space
A D6–7Accessed, dirty
RSW8–9Two bits the hardware ignores and software may use
PPN10–53The physical page number

The R/W/X bits decide whether the entry is a leaf. An entry with none of them set is a pointer to the next level; an entry with any of them set is a leaf. That is how one format serves both, and how a leaf can appear at a level above the last to map a larger page.

Where to go next

The unprivileged specification RISC-V International (n.d.) for the instruction encodings and the memory model, the privileged specification RISC-V International (n.d.) for everything on this page below the register table, and the psABI RISC-V International (n.d.) for the convention. The SBI specification RISC-V International (n.d.) describes the firmware layer beneath the kernel, which ch01 meets once — a RISC-V machine reaches its performance counters through it — and this book does not otherwise use.

References
  1. RISC-V International. (n.d.). The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture. https://riscv.org/technical/specifications/
  2. RISC-V International. (n.d.). The RISC-V Instruction Set Manual, Volume II: Privileged Architecture. https://riscv.org/technical/specifications/
  3. RISC-V International. (n.d.). RISC-V ELF psABI Specification. https://github.com/riscv-non-isa/riscv-elf-psabi-doc
  4. RISC-V International. (n.d.). RISC-V Supervisor Binary Interface Specification. https://github.com/riscv-non-isa/riscv-sbi-doc