MIT6.S081 XV6 lab2 syscall
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
tracesystem 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 callstrace(1 << SYS_fork), whereSYS_forkis a syscall number fromkernel/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. Thetracesystem call should enable tracing for the process that calls it and any children that it subsequently forks, but should not affect other processes.
- Add
$U/_traceto UPROGS in Makefile- Run make qemu and you will see that the compiler cannot compile
user/trace.c, because the user-space stubs for the system call don’t exist yet: add a prototype for the system call touser/user.h, a stub touser/usys.pl, and a syscall number tokernel/syscall.h. The Makefile invokes the perl scriptuser/usys.pl, which producesuser/usys.S, the actual system call stubs, which use the RISC-Vecallinstruction to transition to the kernel. Once you fix the compilation issues, run trace 32 grep hello README; it will fail because you haven’t implemented the system call in the kernel yet.- Add a
sys_trace()function inkernel/sysproc.cthat implements the new system call by remembering its argument in a new variable in theprocstructure (seekernel/proc.h). The functions to retrieve system call arguments from user space are inkernel/syscall.c, and you can see examples of their use inkernel/sysproc.c.- Modify
fork()(seekernel/proc.c) to copy the trace mask from the parent to the child process.- Modify the
syscall()function inkernel/syscall.cto print the trace output. You will need to add an array of syscall names to index into.
我们需要实现一个trace的调用,当用户态调用trace()函数时,传入mask,确定需要trace的系统调用(如fork,read等)。
大体思路时在进程描述中添加mask字段,用于判断该进程是否需要进程trace,然后在执行系统调用时,根据该mask打印调用信息。
实现
- 在用户态的头文件声明调用(
user/user.h),这样编译user/trace.c才不会报错。
1 | // sys strace |
- 在
user/usys.pl添加调用入口
1 | entry("trace"); |
- 在
kernel/syscall.h中添加SYS_trace调用号
1 |
- 在进程描述的结构体中添加
trace_mask字段代表该进程的trace状态,该文件位于kernel/proc.h。
1 | // Per-process state |
- 在
kernel/sysproc.c中添加sys_trace函数。(该文件主要用于实现系统调用)
1 | uint64 sys_trace(void) |
- 在
kernel/syscall.c中注册trace系统调用函数。并且在系统调用中根据mask判断打印的信息。
1 | ... |
Sysinfo
In this assignment you will add a system call,
sysinfo, that collects information about the running system. The system call takes one argument: a pointer to astruct sysinfo(seekernel/sysinfo.h). The kernel should fill out the fields of this struct: thefreememfield should be set to the number of bytes of free memory, and thenprocfield should be set to the number of processes whosestateis notUNUSED. We provide a test programsysinfotest; you pass this assignment if it prints “sysinfotest: OK”.
Add
$U/_sysinfotestto UPROGS in MakefileRun make qemu;
user/sysinfotest.cwill fail to compile. Add the system call sysinfo, following the same steps as in the previous assignment. To declare the prototype for sysinfo()in user/user.hyou need predeclare the existence ofstruct sysinfo:
1
2 struct sysinfo;
int sysinfo(struct sysinfo *);Once you fix the compilation issues, run sysinfotest; it will fail because you haven’t implemented the system call in the kernel yet.
sysinfo needs to copy a
struct sysinfoback to user space; seesys_fstat()(kernel/sysfile.c) andfilestat()(kernel/file.c) for examples of how to do that usingcopyout().To collect the amount of free memory, add a function to
kernel/kalloc.cTo collect the number of processes, add a function to
kernel/proc.c
获取系统信息,将查询的信息从内核态拷贝到用户态。
添加系统调用与上面类似,获取系统信息实现:
sys_sysinfo函数实现(kernel/sysproc.c),copyout函数实现了从内核态拷贝到用户态,p->pagetable为该进程用户态的页表,addr为写入的地址:
1 | uint64 sys_sysinfo(void) |
- 遍历所有进程,统计状态不为UNUSED的进程数量。注意操作进程时,获取和释放锁。
1 | uint64 get_nproc(void) |
- 遍历空闲页表,统计空闲页表数量,然后乘页大小即可
1 | uint64 get_freemem(void) |




