System call basics
Easy
xv6 book Sect 2.1 Abstracting physical resources
xv6 book Sect 2.2 User mode, supervisor mode, and system calls
System calls vs. function calls
System calls allow processes to request services from the operating system. When a system call is executed, a process temporarily gives control of the processor to the OS.
At first glance, a system call looks very similar to a function call. In the previous session, we wrote the userspace function puts(char* str) that writes a string to stdout.
A process that wants to write a string to stdout can use the function call puts, as follows:
const char* str = "Hello, world!";
puts(str);
Alternatively, it can use the system call write, like this:
const char* str = "Hello, world!\n";
write(1, str, strlen(str));
Note that C provides a wrapper function, so that it is possible to invoke a system call as a function. The real syscall is therefore in the implementation of the write wrapper function. However, these wrappers must be implemented at the assembly level. So it is time to dive back into assembly.
![]()
putsand other functions such asprintfalso use the system callwriteinternally. It is not possible to write to the console without a system call. That does not change the fact that calls toputsandprintfare normal function calls.
Invoke a system call in RISC-V
All system calls in RISC-V are executed using the ecall instruction. This instruction, when executed from user mode, will cause the processor to go into supervisor mode and then jump to a trap handler. The trap handler will determine the cause of the trap. If the trap was caused by an ecall, we know that the user was trying to invoke a system call. So it jumps to the system call handler, specifically the syscall(void) function.
-
Look at the function
syscallin the xv6 code. What register is used here to determine which system call to invoke? -
Now add a file
user/hi_asm_write.S(note the capitalSin the extension). Use theecallinstruction to execute the system callwrite. You will need to use the register from the question above to request the correct system call. To run your code, you need to add it to the Makefile as a userspace program, just as you would with a .c program. You can start from the code below:
#include "kernel/syscall.h"
.text
.global main
main:
#TODO implement
#HINT-1: you can use the symbols defined in the header file kernel/syscall.h
# e.g. li t0, SYS_fork loads the value 1 into t0
#HINT-2: don't compute the length manually. You can hardcode it or use strlen!
.section .rodata
hello_str:
.string "Hello, world!\n"
In addition to selecting the system call, it will also be necessary to pass parameters to
write. This is done according to the same conventions as with a normal function calls. The signature ofwriteisint write(int fd, const void *buf, int nbytes). So you will need to pass 3 arguments using registersa0,a1, anda2. Remember that the return value is stored in a0. You can have a look at the RISC-V calling conventions for more details.
Syscall implementation
We now know how a system call is called from user space, how the trap handler and system call handler ensure that the correct system call is executed, and we understand how a C wrapper function can make this very easy for a programmer.
Time to look at the implementation of such a system call.
- Look at the implementation of the simple system call
getpid. It can be that simple. - Look at the other system calls that are implemented in the same file. It is not necessary to understand all of them in detail yet.