Friday 3 January 2014

Linux System Call (syscall) in brief



Syscall is a very important idea in Linux. It provides a method for the user program to interact with the OS kernel. This blog will cover the basic idea what is the syscall and how it works.

Why we need a syscall, Let’s take x86 cpu as example, it has 4 rings which presents different privilege of CPU commands. As below.


So when the linux is implemented, there are two types of program running status, one is kernel model and another is user mode. Most of time, program is running in user model to do basic business related logic, when it needs OS resource, such as write to a device, open a socket. It switch to kernel mode. There are a few switching method and syscall is one of the most important.

Most of time, user program does not call syscall directory, it will use the os library (clib) to interactive with syscall. Let’s take ‘write a string to file’ as example.

Fputs (string, fd) ---> clib write () ---> syscall write ()
User program          clib                          syscall

Every syscall has a unique syscall ID in the OS. The syscall should be allocated by the kernel developer, it is not encouraged to design the syscall unless it is definitely needed.
Internally, syscall is implemented by the soft interrupt. (int $0x80).

When syscall is invoked, syscall ID and other paramters will be passed to kernel in the registers.
This is the diagram how a syscall is invoked.



No comments:

Post a Comment