「这是我参与11月更文挑战的第23天,活动详情查看:2021最后一次更文挑战」
Lab: system calls
【总结】编写系统调用的一般步骤
先给出在 Xv6 中编写系统调用的方法。
如果我们要在 Xv6 中添加一个系统,调用,比如叫做 xxx
:
- add a prototype for the system call to
user/user.h
1 | c复制代码int xxx(int) |
- add a stub to
user/usys.pl
1 | perl复制代码entry("xxx"); |
- add a syscall number to
kernel/syscall.h
1 | c复制代码#define SYS_xxx 22 |
- add the new syscall into
kernel/syscall.c
1 | c复制代码extern uint64 sys_xxx(void); // 1 |
- 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.
1 | c复制代码uint64 |
- implement the syscall somewhere in the kernel. (e.g. implement a
xxx
function , defines inkernel/defs.h
, to do hard work)
下面再做这个 Lab 的题目:
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
:
1 | c复制代码... |
kernel/proc.h
:
1 | c复制代码struct proc { |
kernel/syscall.c
:
1 | c复制代码... |
Detailed diff
1 | diff复制代码diff --git a/Makefile b/Makefile |
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 a struct sysinfo
(see kernel/sysinfo.h
). The kernel should fill out the fields of this struct: the freemem
field should be set to the number of bytes of free memory, and the nproc
field should be set to the number of processes whose state
is not UNUSED
. We provide a test program sysinfotest
; you pass this assignment if it prints “sysinfotest: OK”.
也是照着题目写即可。
Main implement
kernel/sysproc.c
:
1 | c复制代码uint64 |
kernel/sysinfo.c
:
1 | c复制代码#include "types.h" |
kernel/proc.c
:
1 | c复制代码// Get the number of processes whose state is not UNUSED. |
kernel/kalloc.c
:
1 | c复制代码// Get the number of bytes of free memory |
Detailed diff
1 | diff复制代码diff --git a/Makefile b/Makefile |
EOF
By CDFMLR
顶部图片来自于网络,系随机选取的图片,仅用于检测屏幕显示的机械、光电性能,与文章的任何内容及观点无关,也并不代表本人局部或全部同意、支持或者反对其中的任何内容及观点。如有侵权,联系删除。
本文转载自: 掘金