Lab: system calls
From: https://pdos.csail.mit.edu/6.S081/2020/labs/syscall.html
【NOTE】Add a new system call
To add a new system call, say, named xxx
:
-
add a prototype for the system call to
user/user.h
int xxx(int)
-
add a stub to
user/usys.pl
entry("xxx");
-
add a syscall number to
kernel/syscall.h
#define SYS_xxx 22
-
add the new syscall into
kernel/syscall.c
extern uint64 sys_xxx(void); // 1 static uint64 (*syscalls[])(void) = { ... [SYS_xxx] sys_xxx, // 2 };
-
add
sys_xxx
(a function takes void as argument and returns uint64) inkernel/sysproc.c
. This function do fetch arguments about the system call and return values.uint64 sys_xxx(void) { // about arguments: the [xv6 book](https://pdos.csail.mit.edu/6.S081/2020/xv6/book-riscv-rev1.pdf) Sections 4.3 and 4.4 of Chapter 4 }
-
implement the syscall somewhere in the kernel. (e.g. implement a
xxx
function , defines inkernel/defs.h
, to do hard work)
System call tracing
In this assignment you will add a system call tracing feature that may help you when debugging later labs. You’ll create a new trace
system call that will control tracing. It should take one argument, an integer “mask”, whose bits specify which system calls to trace. For example, to trace the fork system call, a program calls trace(1 << SYS_fork)
, where SYS_fork
is a syscall number from kernel/syscall.h
. You have to modify the xv6 kernel to print out a line when each system call is about to return, if the system call’s number is set in the mask. The line should contain the process id, the name of the system call and the return value; you don’t need to print the system call arguments. The trace
system call should enable tracing for the process that calls it and any children that it subsequently forks, but should not affect other processes.
Main implement
kernel/sysproc.c
:
...
// *
uint64
sys_trace(void)
{
if (argint(0, &myproc()->tracemask) < 0)
return -1;
return 0;
}
kernel/proc.h
:
struct proc {
struct spinlock lock;
...
int pid; // Process I