Linux Syscall


by: burt rosenberg
at: university of miami
Revised:

9 september 2020, new formatting and copyright
11 September 2014
adapted from csc521 semester 101

System services

The syscall and the syscall table are the fundamental mechanism for getting system services. The kernel of the operating system exposes an API. These are the operations that can be called on the kernel. Because a program is not linked to the kernel, the call to a system service is done through pre-assigned addresses and operation codes. Also, a call to a kernel function changes the mode of the CPU, and the thread moves from user mode to kernel mode at the instant of the call.

System calls on the Intel architecture involve a special trap instruction. A trap instruction works similar to a call instruction, in that it pushes a return address on the stack and branches to a new instruction address. However, a syscall trap also:

Because the trap instruction is the same for all unix implementations, and the operation codes are the same across all Linux platforms, an application program can be compiled separately from the kernel and the syscall will work properly. The trap will enter the kernel at the syscall handing code, and the operation code will be used in a table of function pointers to execute the proper syscall functionality.

Starting in linux kernel 3.3, the syscall operation code is vectored through a table at

(Prior to that, it was at linux/arch/x86/kernel/syscall_table_32.S, and was written in assembler.) Each entry names a function. As an example the sys_setgid function is in The SYSCALL_DEFINE1 symbol is a macro that sets up the signature properly, including changing the setgid argument into sys_setgid as the name of the function.

In the Intel x86 architecture, the code immediately run in response to the trap is determined by a complicated thing called the GDT, the Global Descriptor Table. A pointer to this table is installed in the CPU during linux boot, and it has entries for various sorts of traps. Each entry is called a call gate which directs the change of privilege and gives the address of the function that will respond to he trap. All of the CPU context can change through a call gate. In particular, the stack is changed from the user stack to the kernel stack, and new data segments are installed that allow for full permission over the entire 4G virtual memory space.

More on traps

The syscall depends on a trap. A trap is one of several call-like instructions that perform special operations as part of the call. What they all have in common is that an event causes a call-like instruction branch, with the return address saved on the stack, with additional changes in processor mode.

The names and taxonomy of this special sorts of calls will vary from processor to processor. Intel breaks them down in interrupts and exceptions:

Exceptions are classified as faults, traps or aborts.