
Recall first from A tour of xv6: building, booting and debugging
- xv6 is a Unix-like teaching kernel of roughly twelve thousand lines, small enough to read end to end, whose source tree maps cleanly to subsystems: proc.c for processes, fs.c for files, syscall.c for dispatch, virtio_disk.c for the disk.
- make qemu builds and boots xv6; make qemu-gdb plus a second gdb terminal gives a breakpoint-based view of boot.
- A kernel panic is a deliberate halt with a message; reading that message is the first kernel debugging skill.
Why xv6 runs on RISC-V, and why you must read it
3 min readThe previous lesson gave you a map of xv6 and a booted system. This lesson adds the vocabulary to read its lowest layer, and answers why xv6 runs on RISC-V and which registers and instructions you must be able to read.
The previous lesson gave you a map of xv6 and a booted system. This lesson gives you the vocabulary to read its lowest layer. Roughly three hundred lines of the xv6 kernel are not C at all (279 lines in its four .S files): the first instructions that run after reset, the code that switches from one process to another, and the code that crosses between user mode and the kernel. A kernel programr cannot skip them, because every crash, every race and every performance question eventually lands in those lines.
RISC-V is an open instruction set architecture (ISA), the contract between software and a processor: which instructions exist, what the registers are, and how privilege works. It began as a research project at the University of California, Berkeley, around 2010. The base integer instruction set and the privileged architecture were later frozen as ratified specifications (the main volumes in 2019), and since then a non-profit, RISC-V International, maintains them. Nobody pays a license to implement it, which is why companies large and small build RISC-V cores.
MIT chose RISC-V for the xv6 teaching kernel around 2019 for a practical reason: the ISA is small and regular. An older x86 version of xv6 spent pages of explanation on segmentation and legacy modes. The RISC-V version spends them on ideas. The same property helps embedded engineers. Microcontrollers such as the Espressif ESP32-C3 use 32-bit RISC-V cores, and boards such as the StarFive VisionFive 2 run full Linux on 64-bit RISC-V. Linux itself gained RISC-V support in version 4.15, released in early 2018.
A hart is a hardware thread: one independent stream of instruction fetch and execute, with its own registers; the specification uses this word instead of core or CPU. xv6 runs on QEMU's virt machine with several harts, and every hart has its own copy of the registers you will meet below.
xv6 uses all three privilege modes that this lesson covers, which makes it unusual. A typical Linux system on RISC-V runs the kernel in supervisor mode and puts a small firmware layer in machine mode below it.
Registers and the RV64GC instruction set
3 min readA register is a small, very fast storage slot inside the processor. RISC-V has 32 general-purpose integer registers named x0 to x31, and on a 64-bit machine each holds 64 bits. The name RV64GC says what you get: RV64 means a 64-bit base integer ISA (named I), and G is shorthand for a bundle of standard extensions: M for integer multiply and divide, A for atomic instructions, F and D for single and double precision floating point, plus two small extensions for control registers (Zicsr) and instruction-cache synchronisation (Zifencei). C adds compressed instructions, 16-bit encodings of the most common operations that make code smaller.
The kernel and the compiler agree on names for the registers. An ABI (application binary interface) is the agreement on register use, argument passing and data layout that lets separately compiled code call each other. The RISC-V ABI gives each register a second name that tells you its job.
| Register | ABI name | Role |
|---|---|---|
| x0 | zero | Always reads zero; writes are discarded |
| x1 | ra | Return address |
| x2 | sp | Stack pointer |
| x3 | gp | Global pointer (unused by xv6 kernel code) |
| x4 | tp | Thread pointer (xv6 stores the hart number here) |
| x5-x7 | t0-t2 | Temporaries, not preserved across calls |
| x8 | s0 / fp | Saved register, also the frame pointer |
| x9 | s1 | Saved register |
| x10-x11 | a0-a1 | Arguments and return values |
| x12-x17 | a2-a7 | More arguments (a7 also carries the system call number) |
| x18-x27 | s2-s11 | Saved registers |
| x28-x31 | t3-t6 | Temporaries |
The program counter, pc, is separate: you cannot name it in an ordinary instruction, and only jumps, branches and traps change it. Register x0 is a clever trick. Because it always reads zero, addi a0, zero, 5 loads the constant 5, and a jump that discards its return address simply writes to x0. This is why the instruction set can stay small.
On RV64 the C type int is 32 bits and long and pointers are 64 bits (the LP64 data model). Instructions with a w suffix, such as addw, operate on the low 32 bits and sign-extend the result to 64 bits, which is how 32-bit int arithmetic is kept correct inside 64-bit registers.
Floating-point registers (f0 to f31) exist too, but the xv6 kernel does not use floating point, so you can ignore them while reading its source. A Linux kernel on RISC-V tracks a floating-point state field in sstatus so that it only saves those registers for processes that used them.
Instruction formats and the working set
6 min readEvery standard RISC-V instruction is 32 bits wide, and the low seven bits are the opcode. The remaining bits are laid out in one of six instruction formats: R, I, S, B, U and J. The designers kept the source register fields, rs1 and rs2, and the destination register field, rd, in the same bit positions in every format that has them. The processor can start reading the register file before it has finished decoding.
| Format | Used for | Fields, high bit to low bit |
|---|---|---|
| R | register-register arithmetic (add, sub) | funct7, rs2, rs1, funct3, rd, opcode |
| I | immediates, loads, jalr, CSR access | imm[11:0], rs1, funct3, rd, opcode |
| S | stores | imm[11:5], rs2, rs1, funct3, imm[4:0], opcode |
| B | conditional branches | imm[12|10:5], rs2, rs1, funct3, imm[4:1|11], opcode |
| U | lui, auipc | imm[31:12], rd, opcode |
| J | jal | imm[20|10:1|11|19:12], rd, opcode |
The funct3 and funct7 fields select the exact operation inside an opcode. Immediates are signed constants stored in the instruction itself. An I-type immediate is 12 bits, so it covers -2048 to 2047.
The working set you will meet in xv6
You do not need all of the instruction set. These instructions cover almost everything in xv6:
addi rd, rs1, immadds a signed 12-bit constant. It is also howmv(copy),li(small constants) andnopare built.add,sub,and,or,xor,sll,srl,sratake two registers.ld rd, off(rs1)andsd rs2, off(rs1)load and store 64 bits.lw,lwu,sw,lb,lbu,sb,lh,lhuandshhandle 32, 16 and 8 bits.beq,bne,blt,bge,bltuandbgeucompare two registers and branch if the test is true. There is no flags register and no separate compare instruction.jal rd, targetjumps and writes the return address to rd.jalr rd, off(rs1)jumps to a register value plus an offset.lui rd, immputs a 20-bit value into bits 31 to 12 of rd.auipc rd, immadds that shifted value to the current pc.csrr,csrw,ecall,mret,sretandsfence.vmafor the privileged work.
Many names you see in assembly are pseudoinstructions: A pseudoinstruction is an assembler shorthand that expands to one or more real instructions, such as ret expanding to jalr zero, 0(ra). Table of the common ones:
| You write | Real instruction |
|---|---|
mv a0, a1 |
addi a0, a1, 0 |
li a0, 5 |
addi a0, zero, 5 |
j label |
jal zero, label |
ret |
jalr zero, 0(ra) |
call func |
auipc ra, hi then jalr ra, lo(ra) |
beqz a0, label |
beq a0, zero, label |
la a0, sym |
auipc a0, hi then addi a0, a0, lo (position independent) |
Memory access uses exactly one addressing mode: a base register plus a signed 12-bit byte offset, as in ld a0, 16(sp). There is no indirect addressing through a memory location, no auto-increment and no scaled index. To walk an array, code adds to a pointer register with addi or add. Only loads and stores touch memory; arithmetic works on registers only. This is the load/store architecture that gives RISC its name.
Try decoding one by hand. The widget below holds addi sp, sp, -16, whose encoding is 0xff010113. Toggle bits and watch the fields change:
Reading the result: opcode 0x13 is the arithmetic-immediate group, rd and rs1 are both 2 (sp), funct3 0 selects add, and the 12-bit immediate 0xff0 is -16 in two's complement. So the word means "sp = sp - 16". The same method decodes sd ra, 8(sp) as 0x00113423 (S-type: rs2 is ra, rs1 is sp) and ld ra, 8(sp) as 0x00813083 (I-type).
Going deeperWhy the B and J immediates look scrambled, and how li builds a 32-bit constant
Branch and jump targets are offsets from the pc and are always even, so bit 0 is never stored. The B and J formats scatter the remaining immediate bits so that most bits stay in the same instruction positions as in the S and I formats. This keeps the sign bit always at instruction bit 31, which lets hardware start sign-extending early. The price is that humans decode them with a table, and assemblers do it for you. Branches reach plus or minus 4 KiB, and jal reaches plus or minus 1 MiB; farther targets need an auipc and jalr pair.
To load a 32-bit constant, the assembler emits lui for the upper 20 bits and addi for the lower 12. Because addi sign-extends its immediate, a constant whose bit 11 is set needs the lui value rounded up by one. And on RV64, lui itself sign-extends bit 31 into the upper 32 bits, so lui a0, 0x80000 produces 0xffffffff80000000, not 0x80000000. A 64-bit address like the kernel base 0x80000000 therefore cannot be a plain lui: the assembler uses a longer sequence, and xv6's entry code uses la, which builds the address relative to pc.
Key takeaways
- RV64GC is a 64-bit base integer ISA plus multiply, atomics, single and double float, and 16-bit compressed instructions.
- Registers have ABI roles: ra, sp, a0-a7 for arguments, t0-t6 as temporaries, s0-s11 as callee-saved; x0 always reads zero.
- Six instruction formats (R, I, S, B, U, J) keep register fields in fixed bit positions, so hardware decodes them cheaply.
- RISC-V is an open, small and regular ISA, which is why MIT chose it for xv6 and why it appears across embedded and Linux systems.
Reflect
- Explain to a colleague, in two sentences: name the RV64GC extensions and the ABI role of each general register, and state which registers a callee must preserve.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
Official documentation first. Links checked when the lesson was published.