Adding New System Calls to xv6

A direct approach

Matteus Silva
3 min readApr 19, 2018

Hi! what’s up?

It’s my second text about xv6 or how to add some stuff to xv6. To understand why I’m doing this, read the first post.

In my first contact with xv6, after compiling and running, I missed a command to shut down the virtual machine. So the first program I tried to write to xv6 was the shutdown.

Function written on a paper.

I thought about it for a while. My program, following the pattern of other programs in xv6, should be something like that function written on a paper.

Searching about ways to shut down in QEMU, I found this site and I found that is necessary to send a special signal to QEMU.

The problem is that an user program can not access the hardware directly. I needed something that offered me a interface to talk the hardware I want to shut down.

According to Wikipedia, a system call is the programmatic way in which a computer program requests a service from the kernel of the operating system it is executing on. I needed the kernel. I needed a system call.

The syscalls that existed at that moment in my fork of xv6.

Well, I looked for a system call in xv6 to help me to shut down the machine and I found nothing.

It was time to write my first system call.

I figured out what things to add in xv6 and in what files to add these things to write my syscall and make it available for user programs.

Here are the steps to add a shutdown in xv6 (running into QEMU emulator version 2.5.0).

Create shutdown.c

shutdown.c

The program shutdown will call the system call halt.

Create the system call halt

The system call halt must be implemented in sysproc.c and uses the documentation provided in https://wiki.osdev.org/Shutdown to send a special signal to QEMU.

Code of the system call halt.

Add SYSCALL(halt) to the end of the file usys.S.

Add a new number for the syscall in syscall.h by adding #define SYS_halt 22 to the end of the file.

Add the following to syscall.c.

At least, expose the new system call to user programs:

For the shutdown work, find the definition of QEMUOPTS in the Makefile and add -device isa-debug-exit,iobase=0xf4,iosize=0x04 to the line.

That’s it! The system call halt is now available for our shutdown program. To know how to add the user program with xv6, see this post.

--

--