how do i add a system call / utility in xv6

2019-01-13 19:12发布

问题:

Can any one tell me/ point me any references to how to add a system call / utility in XV6

exhaustive search on google was futile and hacking the hard way also was not productive so far .

the reference book also did not have any hello world example to start with any help greatly appreciated

回答1:

Read this: http://zoo.cs.yale.edu/classes/cs422/2010/xv6-book/trap.pdf
It explains it quite well



回答2:

To add a system call that can be called in xv6's shell, you should so something with the five files

  • sysproc.c add the real implementation of your method here
  • syscall.h define the position of the system call vector that connect to your implementation
  • user.h define the function that can be called through the shell
  • syscall.c external define the function that connect the shell and the kernel, use the position defined in syscall.h to add the function to the system call vector
  • usys.S use the macro to define connect the call of user to the system call function

  • defs.h add a forward declaration for your new system call

  • sysfunc.h add the new system call handler into this file too like "int sys_newsystemcall(void)"


回答3:

I will explain a simple example on adding a system call in xv6.

Assume that you have to add a system call to get the address space size of the currently running user program. You can name your system call as getmysize() and call this system call from within a user program.

You can write your user program as follows. Let the program name be myprog.c I assume that you know how to add a user program to xv6. So I will skip mentioning those steps here because it will be off the topic.

#include "types.h"
#include "stat.h"
#include "user.h"

int main(void){
  printf(1,"The size of my address space is %d bytes\n", getmysize());
  exit();
}

This program is written for the purpose of demonstrating the working of the system call only.

Now we will add our system call to xv6.

First add the following line at the end of the file syscall.h.

#define SYS_getmysize 23

Note that the 23 here might change depending on the number given before the line you are going to add in the file. That is if the system call number is 22 in the line before the line that you are going to add, your line should have the number 23. Simple as that.

Now add the following lines to the syscall.c file.

extern int sys_getmysize(void);

and in the array of syscalls, append the following line in.

[SYS_getmysize] sys_getmysize,

In the file sysproc.c, that is where the implementation of your system call goes if it is a system call related to process handling or memory management, put the implementation of our system call as follows.

int 
sys_getmysize(void)
{
   return proc->sz;
}

Now in the usys.S file, add the line,

SYSCALL(getmysize)

Then in the user.h file, add the following. This is how your user program sees your system call.

int getmysize(void);

Ok. Now you are ready to run your user program and see how your system call runs.

When you run myprog.c which calls your system call, the size of the address space of this same program will be returned according to what we have implemented here.