
Recall first from What an operating system does
- An operating system does three jobs: it multiplexes hardware, isolates programs, and lets them interact through processes, memory, and files.
- The kernel runs in a privileged CPU mode; user programs trap into it with a system call such as ecall.
- System calls are a small, stable interface, and the shell is just an ordinary user program built on fork, exec and wait.
A tour of the xv6 source tree
2 min readNow that you know what an operating system does and how system calls cross the privilege boundary, this lesson answers how you tour, build, boot and debug xv6 for the first time.
xv6 lives in a single repository with three main areas. The kernel is in kernel/, the user programs and their tiny C library are in user/, and mkfs/ builds the file-system image the kernel boots with. The whole system is roughly twelve thousand lines: the kernel is about 6,700 and the user side about 5,700. For contrast, individual subsystems of Linux dwarf all of xv6, which is exactly why this module starts here rather than in the Linux tree.
The kernel files map almost one-to-one onto the jobs from earlier:
- proc.c (about 700 lines) implements processes, the scheduler and wait/exit; main.c controls boot-time initialisation.
- syscall.c dispatches system calls to their handlers; sysproc.c and sysfile.c contain those handlers, split by process and file concerns.
- fs.c (the largest file, about 740 lines) implements the file system, with bio.c as its disk-block cache and log.c as its crash-recovery log.
- vm.c (about 490 lines) manages page tables and address spaces; riscv.h holds the RISC-V constants and register macros.
- virtio_disk.c is the disk device driver, uart.c the serial console driver, and plic.c the RISC-V interrupt controller.
- swtch.S is the thread switch, and kernel.ld tells the linker where to place everything in memory.
On the user side, sh.c is the shell, ls.c and echo.c are utilities, usys.pl generates the assembly stubs that turn each system call into an ecall, and ulib.c is a minimal C library. A small but important file is entry.S, under thirty lines, where the CPU first enters the kernel.
You do not need to understand every file on day one. The skill to build now is finding your way: grep -rn "fork" kernel/ user/ to see every definition and use, and grep -n "panic" kernel/*.c to find where failures are reported. Interfaces between files are declared in kernel/defs.h, so that header is your map of who calls whom.
How xv6 boots
4 min readBooting is the least magical and most instructive part of a kernel, because it is a straight line of code with no concurrency yet. When QEMU starts the RISC-V machine, the CPU begins in machine mode with paging off, and the boot loader, the small program that loads the kernel into memory and hands control to it, has already placed the kernel at physical address 0x80000000 and jumped there. The first instruction is in entry.S.
entry.S: set a stack pointer for the current hart from the mhartid register, then jump to start().
start.c (machine mode): write mstatus, mepc and satp, delegate traps to supervisor mode, program the timer, then execute mret.
main.c: initialise memory, page tables, processes, traps, the PLIC, the buffer cache, the file system and the virtio disk, then call userinit and the scheduler.
userinit: create the first process; the first time the scheduler runs it, forkret() in proc.c initialises the file system and calls kexec("/init") from inside the kernel.
init: open the console as file descriptors 0, 1 and 2, then launch the shell.
scheduler: the scheduler loop now hands the CPU between runnable processes forever.
Step through the hand-offs below:
The scheduler, the kernel code that decides which runnable process gets each CPU next, is what turns this one-time chain into a running system. main.c ends by calling it, and from then on the kernel is an engine that switches between processes, each switch triggered by a timer interrupt or a system call.
The hard-coded addresses in this path are not arbitrary; they are the memory map of QEMU's virt board, the ground truth every xv6 driver relies on.
Going deeperThe QEMU virt memory map
The virt machine lays out devices at fixed addresses below the RAM, which is why the kernel starts at 0x80000000 rather than 0: everything under that line is I/O. From the xv6 constants in kernel/memlayout.h: the CLINT (core-local timer) sits at 0x02000000, the PLIC (platform interrupt controller) at 0x0c000000, the UART at 0x10000000, and the virtio disk at 0x10001000. RAM occupies 0x80000000 to 0x88000000, 128 MiB, matching QEMU's -m 128M flag. A real board encodes this same information in a device tree instead of a header, but the principle, a fixed map of memory and memory-mapped I/O, is identical.
Enter an address to see which region it falls in:
Building, booting and extending xv6
3 min readEverything from here is hands-on. Clone the repository and, with the toolchain installed, type make qemu. The Makefile invokes a cross-compiler, a compiler that runs on your PC but produces code for another machine (here RISC-V); the Makefile probes for one, and on Ubuntu 24.04 it finds riscv64-linux-gnu-gcc, to build the kernel and each user program. It then runs mkfs to bake those programs into a disk image, and finally starts QEMU with options that amount to: emulate the virt RISC-V machine, load kernel/kernel at 0x80000000, give it 128 MiB of RAM and three CPUs, and attach a text console instead of a window.
Typical boot output looks like this (yours will differ in detail):
xv6 kernel is booting
hart 2 starting
hart 1 starting
init: starting sh
$
You are now at the xv6 shell. It is a real shell but a deliberately minimal one: you can run ls to list the file system, cat to print a file, and echo hello to print text. There is no graphical environment and no package manager; everything on the disk image is a program the Makefile put there. That minimalism is the point: a system this small is one you can read and rebuild end to end.
Adding your own program is a two-file change and does not touch the kernel. Create user/hello.c:
// user/hello.c
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int
main(void)
{
printf("hello from xv6\n");
exit(0);
}
Then add $U/_hello\ to the UPROGS list in the Makefile and run make qemu again. mkfs now includes the hello binary, and typing hello at the prompt runs it. exit(0) is the system call that returns the process's resources to the kernel. If main simply returns instead, the start() wrapper in user/ulib.c, the real entry point of every xv6 user program, calls exit with main's return value.
Running xv6 under QEMU is the same shape as running Linux on a board: a boot stage loads a kernel to a fixed address, the kernel initialises drivers for devices at fixed addresses, and an init program starts a shell or a service. On a physical SoC the fixed addresses come from a device tree and the cross-compiler targets your board's architecture; the init program is often BusyBox. Later modules cover both, so notice the shape now.
Debugging xv6 with GDB
2 min readA kernel is debugged the same way as any program, except that the debugger attaches to an emulated machine over a network socket. Run make qemu-gdb in one terminal; this starts QEMU with two extra flags: -S, which tells it not to execute a single instruction until told, and a request to expose a GDB server on a TCP port the Makefile computes from your user id (uid % 5000 + 25000, so 26000 for uid 1000; make print-gdbport prints it). In a second terminal, run gdb-multiarch in the same directory, and it reads the project's .gdbinit once you allow that file to auto-load.
Going deeperWhat .gdbinit does
The xv6 .gdbinit performs four setup steps automatically: it turns off confirmation prompts, sets the target architecture to riscv:rv64, connects to the waiting QEMU with target remote 127.0.0.1 and the port (the Makefile generates .gdbinit from .gdbinit.tmpl-riscv, replacing the template's 1234 with your port), and loads the kernel symbols from kernel/kernel. It also enables disassembly of the next line and compressed breakpoints. Without it you would type those commands by hand each session.
With the connection made, set a breakpoint at the first C function and run:
(gdb) b main
(gdb) c
Execution stops at main() before any device is initialised. Now inspect the machine: info reg prints all 32 integer registers and the program counter, and x/6i $pc disassembles the next six instructions. si steps a single instruction at a time, which is how you watch the boot chain from the previous section actually run. To trace a system call, set a second breakpoint. The kernel's side of fork is named kfork (in proc.c, called by sys_fork); b fork fails with Function "fork" not defined. because fork is only the user-space stub:
(gdb) b kfork
(gdb) c
The breakpoint fires when any process calls fork, first init as it starts the shell, and bt then shows kfork called from sys_fork and syscall. From there you can step through the kernel's copy of the process and watch the new process come into being.
If your distribution has no riscv64 GDB, the package gdb-multiarch works; QEMU itself comes from the qemu-system-misc package on Debian and Ubuntu. The GDB server listens on the port make print-gdbport prints, so nothing else on the machine may already be using it.
Common first-day mistakes and reading a panic
2 min readThe fastest way to learn a kernel is to break it, so expect these failures and know how to read them. The central diagnostic is panic(). When xv6 detects a violated invariant, it calls panic() with a message, prints panic: followed by that message, freezes console output from the other CPUs, and then spins in an infinite loop. A panic is a deliberate, permanent halt, and its value is the text it printed: the message names the invariant, and the source of the call is usually one line away.
The common failure mode is to stare at the register dump or the frozen cursor. The fix is to read the line after panic: first. It tells you whether a page table was inconsistent, a lock was released twice, or a system call received a bad argument, which narrows the search to one file.
Three mistakes come up in the first week, each with a symptom, a cause, a diagnosis and a fix:
- Symptom: make stops with
*** Error: Couldn't find a riscv64 version of GCC/binutils.Cause: the cross-toolchain is not installed or not on the path. Diagnose withwhich riscv64-linux-gnu-gcc. Fix: install gcc-riscv64-linux-gnu and binutils-riscv64-linux-gnu and re-run make. - Symptom: your new program does not appear in the shell, or the build now fails. Cause: you edited a kernel file or the system call table instead of adding the program to UPROGS. Diagnose by checking whether your file is listed in the Makefile's UPROGS. Fix: put the program in user/ and add it to UPROGS; mkfs does the rest.
- Symptom: gdb-multiarch prints
auto-loading has been declined by your `auto-load safe-path'and thenThe program has no registers now.Cause: GDB refuses to run the project's .gdbinit, so it never connects to QEMU. Diagnose by reading the warning, which names the file. Fix: add the lineadd-auto-load-safe-pathfollowed by the full path of your .gdbinit to ~/.config/gdb/gdbinit, then start gdb-multiarch again.
Try it
1 min readThis lesson's lab is the hands-on half: clone the xv6-riscv repository, build it, and boot it under QEMU, then attach GDB, break at main() and at kfork(), the kernel side of fork, and single-step through one system call. Finish by drawing an annotated map of the repository tree that labels each file with the subsystem it implements. Work through the practice pack's setup guide and the first exercises, and keep the panic-reading checklist beside you, because a panic is not a failure, it is the kernel telling you exactly where to look.
Key takeaways
- 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.
- The QEMU virt board's fixed memory map is the same ground truth real SoCs encode in their device trees.
Reflect
- Explain to a colleague, in two sentences: navigate the xv6 source tree and map each major file to the subsystem it implements.
- 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.