Lab guidance

The labs in general require not many lines of code (tens to a few hundred lines), but the code is conceptually complicated and often details matter a lot. So, make sure you do the assigned reading for the labs, read the relevant files through, consult the documentation (the RISC-V manuals etc. are on the readings page) before you write any code. Only when you have a firm grasp of the assignment and solution, then start coding. When you start coding, implement your solution in small steps (the assignments often suggest how to break the problem down in smaller steps) and test whether each steps works before proceeding to the next one.

Although most C programs never need to cast between pointers and integers, operating systems frequently do. Whenever you see an addition involving a memory address, ask yourself whether it is an integer addition or pointer addition and make sure the value being added is appropriately multiplied or not. A few pointer common idioms are in particular worth remembering:

If you have an exercise partially working, checkpoint your progress by committing your code. If you break something later, you can then roll back to your checkpoint and go forward in smaller steps. To learn more about Git, take a look at the Git user’s manual, or, you may find this CS-oriented overview of Git useful.

If you fail a test, make sure you understand why your code fails the test. Insert print statements until you understand what is going on.

You may find that your print statements may produce much output that you would like to search through; one way to do that is to run make qemu inside of script (run man script on your machine), which logs all console output to a file, which you can then search. Don’t forget to exit script.

In many cases, print statements will be sufficient, but sometimes being able to single step through some assembly code or inspecting the variables on the stack is helpful. To use gdb with xv6, run make qemu-gdb in one window, run make gdb in another window, set a break point, followed by followed by c (continue), and xv6 will run until it hits the breakpoint. (See Using the GNU Debugger for helpful GDB tips.)

Before running GDB, you will need to configure/edit a few files. The first step you’ll want to do is create a ~/.gdbinit file in your home directory, or edit it if it already exists, and add the following line:

add-auto-load-safe-path /full/path/to/xv6/code/.gdbinit

Note that /full/path/to/xv6/code should be the actual path to wherever you have your xv6 repo cloned. If you don’t do this step, when running GDB, you will most likely be told that “the program is not being run”. This should address that issue.

Additionally, you’ll need to edit the GCC optimization level in the Makefile. As you want to have as much information as you can when debugging, you’ll need to tweak the optimization level to allow for the easiest debugging experience (i.e., no values are optimized out). Change the following line in the Makefile (around line 93):

CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb

To this:

CFLAGS = -Wall -Werror -O0 -fno-omit-frame-pointer -ggdb

Now, after running make clean, GCC will not introduce any optimizations the next time you compile the xv6 source code, allowing you to debug with GDB. Make sure to revert this before commiting changes/submitting your code.

If you want to see what the assembly is that the compiler generates for the kernel or to find out what the instruction is at a particular kernel address, see the file kernel.asm, which the Makefile produces when it compiles the kernel. (The Makefile also produces .asm for all user programs.)

If the kernel panics, it will print an error message listing the value of the program counter when it crashed; you can search kernel.asm to find out in which function the program counter was when it crashed, or you can run addr2line -e kernel/kernel pc-value (run man addr2line for details). If you want to get backtrace, restart using gdb: run make qemu-gdb in one window, run make gdb in another window, set breakpoint in panic (b panic), followed by followed by c (continue). When the kernel hits the break point, type ‘bt’ to get a backtrace.

If your kernel hangs (e.g., due to a deadlock) or cannot execute further (e.g., due to a page fault when executing a kernel instruction), you can use gdb to find out where it is hanging. Run make qemu-gdb in one window, make gdb in another window, followed by followed by c (continue). When the kernel appears to hang hit Ctrl-C in the qemu-gdb window and type bt to get a backtrace.

QEMU has a “monitor” that lets you query the state of the emulated machine. You can get at it by typing Ctrl-a c (the “c” is for console). A particularly useful monitor command is info mem to print the page table. You may need to use the cpu command to select which core info mem looks at, or you could start qemu with make CPUS=1 qemu to cause there to be just one core.