Main content:
- What is a system call
- Principle of system call implementation on Linux
- Implementation of a simple system call
1. What is a system call
Simply put, a system call is a bridge between a user program and a hardware device.
The user program is used by the system to use the hardware device when needed.
The existence of system calls has the following important meanings:
1) User program through the system to use the hardware, rather than the specific hardware equipment, which greatly simplifies the development of user programs.
For example, a user program can write data to a file through a write () system call, without having to worry about whether the file is on disk or floppy, or on other storage.
2) system calls make the user program more portable.
As long as the system invocation interface provided by the operating system is the same, the user program can migrate from one system to another without modification.
3) system calls enable the kernel to better manage user programs and enhance the stability of the system.
Because system calls are implemented by the kernel, the kernel uses system calls to control what functions are open and what permissions are given to the user program.
This avoids the improper use of the hardware device by the user program, which destroys other programs.
4) The system call effectively separates the development of the user program and the kernel.
The user program only needs to care about the system call API, through these APIs to develop their own applications, do not care about the specific implementation of the API.
The kernel simply cares about the implementation of the system invoke API, without having to worry about how they are called.
2. Principle of system call implementation on Linux
To implement system calls, the main implementation of the following aspects:
- Notifies the kernel which system calls a call
- The user program passes the parameters of the system call to the kernel
- The user program gets the system call return value returned by the kernel
Let's see how Linux implements the above 3 features.
2.1 Notifies the kernel which system call is called
Each system call has a system call number, and when a system call occurs, the kernel knows which system is called based on the incoming system call number.
In the x86 schema, user space places the system call number in EAX, and the system call handler obtains the system call number through EAX.
The system call number is defined in the kernel code: ARCH/ALPHA/INCLUDE/ASM/UNISTD.H, you can see that Linux system calls are not many.
2.2 The user program passes the parameters of the system call to the kernel
The parameters of the system call are also passed to the kernel via registers, on the x86 system, the first 5 parameters of the system call are placed in Ebx,ecx,edx,esi and EDI, and if there are many parameters, a separate register is required to store pointers to all parameters in the user space address.
General system calls are accessed through the C library (most commonly the GLIBC library), and the Linux kernel provides a way to access the system calls directly from the user program.
Linux Reading notes Fifth Chapter