System Calls from user Mode

System calls give up control from user mode code to kernel code in order to run privileged instructions.

In xv6, there are 21 syscalls, as well as some library functions for user code, in ulib.c.

System Calls

System CallPurpose
forkCreates a new process (child process).
exitTerminates the current process.
waitWaits for a child process to finish.
pipeCreates a unidirectional pipe for interprocess communication.
readReads data from a file descriptor.
writeWrites data to a file descriptor.
closeCloses an open file descriptor.
killSends a signal to terminate a process.
execReplaces the current process with a new program.
openOpens a file and returns a file descriptor.
mknodCreates a special file (device file).
unlinkDeletes a file.
fstatRetrieves file metadata from a file descriptor.
linkCreates a new directory entry (hard link) to an existing file.
mkdirCreates a new directory.
chdirChanges the current working directory.
dupDuplicates an existing file descriptor.
getpidReturns the process ID of the calling process.
sbrkIncreases or decreases the size of the process’s heap.
sleepPuts the calling process to sleep for a specified time.
uptimeReturns the system uptime (ticks since boot).

ulib.c functions

FunctionPurpose
startCalls main() and ensures exit(0) is called if main() returns.
strcpyCopies a string from source to destination.
strcmpCompares two strings.
strlenReturns the length of a string.
memsetFills a memory area with a constant value.
strchrFinds the first occurrence of a character in a string.
getsReads a line of input from standard input.
statRetrieves file metadata by opening and calling fstat.
atoiConverts a string to an integer.
memmoveCopies memory, handling overlapping regions safely.
memcmpCompares two memory areas.
memcpyCopies memory from source to destination (calls memmove).

Initcode

There’s code that starts up a user process, by setting up init, and argv, in initcode.S.

#include "syscall.h"
 
# exec(init, argv)
.globl start
start:
        la a0, init
        la a1, argv
        li a7, SYS_exec
        ecall
 
# for(;;) exit();
exit:
        li a7, SYS_exit
        ecall
        jal exit
 
# char init[] = "/init\0";
init:
  .string "/init\0"
 
# char *argv[] = { init, 0 };
.p2align 2
argv:
  .quad init
  .quad 0