kernel/syscall.h
About this file
The list of system call numbers: the agreement between user programs and the kernel about which number means which system call.
The header is included on both sides of that agreement. In the kernel,
kernel/syscall.c uses the numbers as indices into its dispatch table
syscalls. On the user side, the stub generator user/usys.pl writes
#include "kernel/syscall.h" at the top of user/usys.S, and each stub loads its number
into register a7 before ecall, for example li a7, SYS_fork. Since the header is
shared, the two sides cannot disagree.
Read next: kernel/syscall.c.
One number per system call
Each line gives one system call a small positive integer. The numbers are arbitrary
but must be unique, and none is 0 (syscall rejects 0). They are plain #defines
with no C syntax beyond that, which is what allows user/usys.S, an assembly file
run through the C preprocessor, to include this header too.
The order is historical. To add a system call you add a number here, a handler
sys_name and a table entry in kernel/syscall.c, and an entry("name") line in
user/usys.pl plus a prototype in user/user.h.
A few of the names differ from the kernel functions that do the work: SYS_exit
leads to sys_exit, which calls kexit; SYS_fork to kfork; SYS_exec to
kexec. SYS_pause is the call older xv6 versions named sleep (it waits for a
number of clock ticks; see sys_pause), and SYS_sbrk takes a second argument
choosing eager or lazy allocation (see sys_sbrk).
fork: create a copy of the calling process. Handled by sys_fork.
read: read from a file descriptor; handled by sys_read in
kernel/sysfile.c.
sbrk: grow or shrink the process’s memory. The user library calls it as
sys_sbrk(n, SBRK_EAGER) from sbrk and sys_sbrk(n, SBRK_LAZY) from sbrklazy
(user/ulib.c:153).
pause: sleep for a number of clock ticks (sys_pause).
sync: wait until the file system log has committed outstanding operations
(sys_sync in kernel/log.c).