Part 3 of 8 · 3 min read
A system call step by step
Think of ordering at a restaurant counter. You do not walk into the kitchen. You tell the cashier what you want, the order goes through one window, and the food comes back through it. The analogy stops in one place: a cashier may refuse an order by choice, and the kernel refuses by fixed rules.
In plain words: A system call is the one guarded way into the kernel. Through it, a program asks the kernel to do something for it. Three facts hold for every system call:
- It changes the processor from user mode to kernel mode, so that the kernel can reach protected memory.
- The set of system calls is fixed. Each has a unique number, though programs normally use names.
- Each may have arguments that pass information from user memory (the program's own address space) to kernel memory and back.
To a C programmer, a system call looks like a normal function call. Behind the scenes many steps occur. TLPI lists them for 32-bit x86 processors. Newer processors use a faster entry instruction, but the order is the same:
| Step | What happens |
|---|---|
| 1 | The program calls a wrapper function in the C library, such as getpid() or write(). |
| 2 | The wrapper puts the arguments in the registers the kernel expects. |
| 3 | The wrapper puts the system call number into a fixed register. |
| 4 | The wrapper executes a trap instruction. The processor switches to kernel mode and jumps to a fixed place named in the trap vector. |
| 5 | The kernel's entry code saves the registers, checks the system call number, and looks up the number in a table of service routines. |
| 6 | The service routine checks its arguments (for example, that an address really points into the user's memory), does the work, and returns a result. |
| 7 | The entry code restores the registers and returns to the wrapper, which puts the processor back in user mode. |
| 8 | If the result signals an error, the wrapper stores the error code in errno, and returns -1 to the caller. |
Step 6 matters for safety. The kernel never trusts what a program hands it. A program that passes a bad pointer gets an error, not a crash of the OS.
The key lines.
A small program shows the number idea. On the test machine (64-bit Linux), the call getpid has number 39. The program asks for the process id twice, through the library and through the raw syscall() function:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
int main(void) {
long a = getpid();
long b = syscall(SYS_getpid);
printf("libc getpid=%ld, raw syscall=%ld, syscall number=%d\n", a, b, (int)SYS_getpid);
return 0;
}
Output (the process id differs on each run):
libc getpid=364, raw syscall=364, syscall number=39
Both routes give the same answer, because the library wrapper does exactly steps 2 to 4 and 7 to 8. The number differs by architecture: 39 is the 64-bit x86 number, not a universal constant.
System call
A system call is the controlled entry point through which a user-mode program asks the kernel to perform a service for it, such as reading a file or creating a process. The call executes a special instruction that switches the processor to kernel mode and jumps to a fixed kernel address, and the processor returns to user mode when the service is done.
Example: a program calls write(1, "hi\n", 3) to put three bytes on the screen, and the kernel does the actual output.
Check yourself: 1. Why does the wrapper place a number in a register? 2. What does the kernel do with a pointer argument before using it? 3. Why can the same system call have a different number on a different processor?