Add a new system call in xv6

Viduni Wickramarachchi
3 min readApr 18, 2018

--

If you did follow my previous blog post, you would already have an insight into the xv6 operating system. However, just to brief the new readers, xv6 is a teaching operating system which was developed by MIT. This blog is the second phase of a series of hacks which I’m going to write about using the xv6 OS.

What is a system call?

As you all know, an operating system supports two modes; the kernel mode and the user mode.

When a program in user mode requires access to RAM or a hardware resource, it must ask the kernel to provide access to that particular resource. This is done via a system call. When a program makes a system call, the mode is switched from user mode to kernel mode.

There are many system calls in an operating system which executes different types of tasks when they are called.

Adding a custom system call in xv6

In order to define your own system call in xv6, you need to make changes to 5 files. Namely, these files are as follows.

1. syscall.h
2. syscall.c
3. sysproc.c
4. usys.S
5. user.h

Let’s write a system call to return the year Unix version 6 was released.

We would start the procedure by editing syscall.h in which a number is given to every system call. This file already contains 21 system calls. In order to add the custom system call, the following line needs to be added to this file.

#define SYS_getyear 22

Next, we need to add a pointer to the system call in the syscall.c file. This file contains an array of function pointers which uses the above-defined numbers (indexes) as pointers to system calls which are defined in a different location. In order to add our custom system call, add the following line to this file.

[SYS_getyear] sys_getyear,

What exactly happens in here?

The underlying meaning of the above two changes is as follows.

  • When the system call with number 22 is called by a user program, the function pointer sys_getyear which has the index SYS_getyear or 22 will call the system call function

Therefore, we need to implement the system call function. However, we do not implement the system call function in the syscall.c file. Instead, we only add the function prototype in here and we define the function implementation in a different file. The function prototype which needs to be added to the syscall.c file is as follows.

extern int sys_getyear(void);

Next, we will implement the system call function. In order to do this, open the sysproc.c file where system call functions are defined.

// return the year of which the Unix version 6 was releasedint
sys_getyear(void)
{
return 1975;
}

The basic implementation of the system call is now complete. However, there are 2 more minor steps remaining.

Add the interface for the system call

In order for a user program to call the system call, an interface needs to be added. Therefore, we need to edit the usys.S file where we should add the following line.

SYSCALL(getyear)

Next, the user.h file needs to be edited.

int getyear(void);

This would be the function which the user program calls. This function will be mapped to the system call with the number 22 which is defined as SYS_getyear preprocessor directive.

If you have completed all of the above, you have successfully added a new system call to xv6. However, in order to test the functionality of this, you would need to add a user program which calls this system call.

The user program could be as follows.

#include “types.h”
#include “stat.h”
#include “user.h”

int
main(void)
{
printf(1, “Unix V6 was released in the year %d\n”, getyear());
exit();
}

In order to add this user program to xv6, you need to follow few steps. If you are not familiar with adding a user program to xv6 please refer this blog.

The final step would be to run the user program in the qemu window which can be obtained by running the command make qemu on the terminal.

The year 1975 should be displayed on the terminal if your system call has been added to xv6 correctly.

Please feel free to leave a comment if you have any queries.

Thank You!

--

--

Viduni Wickramarachchi
Viduni Wickramarachchi

Responses (3)