Project 1b: xv6 Intro

We'll be doing kernel hacking projects in xv6, a port of a classic version of unix to a modern processor, Intel's x86. It is a clean and beautiful little kernel, and thus a perfect object for our study and usage.

This first project is just a warmup, and thus relatively light on work. The goal of the project is simple: to add a system call to xv6. Your system call, int addnum(int x), simply adds x to a counter (which starts at 0), and returns the new value of the counter.

From a userspace program, the call might be used as follows:

printf(1, "counter = %d\n", addnum(3)); // prints "counter = 3"
printf(1, "counter = %d\n", addnum(2)); // prints "counter = 5"
printf(1, "counter = %d\n", addnum(9)); // prints "counter = 14"

Tips

Find some other system call, like getpid(). Basically, copy it in all the ways you think are needed. Then modify it to do what you need.

Note how other system calls retrieve arguments with argint() and write similar code.

You may want to think about what happens when multiple processes call addnum simultaneously. We won't test this when grading, but feel free to use the acquire and release if you want to handle this correctly.

Most of the time will be spent on understanding the code. There shouldn't be a whole lot of code added.

Using gdb (the debugger) may be helpful in understanding code, doing code traces, and is helpful for later projects too. Get familiar with this fine tool!

The Code

The source code for xv6 (and associated README) can be found in ~cs537-2/public/xv6.tar.gz. Everything you need to build and run and even debug the kernel is in there; start by reading the README.

You may also find the following book about xv6 useful, written by the same team that ported xv6 to x86: book. Particularly useful for this project: Chapters 0 and 3 (and maybe 4). Note that our version of xv6 is slightly older than the book's, so you may encounter a difference here and there.