System Call (1), System Call (

Source: Internet
Author: User

System Call (1), System Call (

(1): communication with the kernel

The system call adds an intermediate layer between the user space and the hardware device. This layer has three main functions:

1. It provides a hardware abstract interface for the user space. 2. system calls ensure system stability and security. 3: every process runs in a virtual system, and this public interface is provided in the User space and the rest of the system.

In Linux, system calling is the only means for user space to access the kernel.

(2): API, POSIX, and C library

In general, applications are programmed through the application programming interface (API) implemented in the user space rather than through system calls. An API defines the programming interfaces used by a group of applications.

Next, let's take a look at the relationship between POSIX, API, and C library and system calls.

(3): System Call

First, let's take a look at the implementation of a relatively simple system call:

SYSCALL_DEFINE0(getpid){    return task_tgrid_vnr(current);}

Note that the definition does not specify how to implement it.

SYSCALL_DFINE0 is just a macro, which defines a system call without parameters. The code after the extension is:

asmlinkage long sys_getpid(void)

Let's take a look at how to define system calls:
First, pay attention to the asmlinkage qualifier in the function declaration. This is a Compilation instruction that notifies the compiler to extract only the parameters of this function from the stack. This qualifier is required for all system calls.
Second, the function returns long. To ensure compatibility between 32-bit and 64-bit systems, system calls have different return value types in the user space and kernel space. The return value type is int in the user space and long in the kernel space.
Finally, note that the system calls get_pid () and is defined as sys_getpid () in the kernel (). This is a naming rule.

1: system call number

In Linux, each system call is assigned a system call number. In this way, each system call is associated with a system call.

The system call number is very important. Once assigned, no changes can be made. Otherwise, the compiled application will crash. In addition, if a system call is deleted, the system call number occupied by it cannot be recycled. Otherwise, the previously compiled code will call this system call, but in fact it calls another system call. In Linux, there is an "unimplemented" System Call sys_ni_syscall (), which does nothing except to return-ENOSYS. This system call is specially designed for invalid system calls. If a system call is deleted or becomes unavailable, the system call is responsible for "filling in the blanks".

In sys_call_table, It is a list of all registered system calls recorded by the kernel. In the x84-64, defined in the file arch/i386/kernel/syscall_64.c. This table specifies a unique system call number for no valid system call.

Now let's take a look:

Const sys_call_ptr_t sys_call_table [_ NR_syscall_max + 1] = {/** Smells like a compiler bug -- it doesn't work * when the & below is removed. ** it looks like a compiler bug-after the current and removed, it will not work */[0... _ NR_syscall_max] = & sys_ni_syscall, # include <asm/unistd_64.h> };

2: System Call Performance

Linux calls are faster than other operating systems. Linux has a very short context switching time, which is an important reason. The introduction of kernel and kernel optimization is efficient. At the same time, the system call handler function and each system call are also very simple.

(4) system call processing functions
Because the user space program cannot execute the kernel program, all need a mechanism to notify the kernel to execute a system call.

The mechanism for notifying the kernel is implemented through soft interruptions: The system switches to the kernel state to execute the exception handling program by initiating an exception. At this time, a handler is a system call handler. You will learn more about interruptions later.

1: specify an appropriate system call

Because all system calls fall into the kernel in the same way, you need to pass the system call number to the kernel together. On x86, the system call number is passed to the kernel through the eax register. Before getting into the kernel, the user space passes in the number corresponding to the system call to eax.

The system_call () function checks its validity by comparing the given system call number with nr_syscils. If the value is greater than or equal to nr_syscils, the function returns-ENOSYS. Otherwise, execute the corresponding system call:

call *sys_call_table(,%rax,8)

Because the table items in the system call table are stored in the 63-bit type, the kernel needs to multiply the given system call number by 4 and then query the location in the table with the obtained results.

2: parameter transfer

Like the system call number, parameters can also be passed to the kernel through registers when passing parameters. In a x86-32 system, ebx, ecx, edx, esi, edi stores the first five parameters in order. In addition, A separate register should be used to store pointers pointing to all these parameters in the user space address.

Let's take a look at the system call process:

The returned values to the user space are also transmitted through registers. On the x86 system, it is stored in the eax register.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.