"Linux kernel Design and implementation" chapter fifth

Source: Internet
Author: User

Fifth Chapter system call

First, communication with the kernel

The system call adds a middle tier between the user space process and the hardware device. function :

    • Provides a hardware abstraction interface for user space.
    • System calls guarantee the stability and security of the system.
    • Each process runs in a virtual system, and it is for this reason that a common interface is provided in the user space and the rest of the system.

In Linux, system calls are the only means of accessing the kernel in a user's space, and they are the only legitimate entry for the kernel except exceptions and falls.

Ii. API, POSIX, and C libraries

In general, applications are programmed through the application Programming Interface (API) implemented in user space rather than directly through system tuning.

An API defines the programming interfaces used by a set of applications. Because APIs do not actually need to correspond to system calls (APIs can be implemented on a variety of operating systems), an API can be implemented as a system call, or it can be implemented by invoking multiple system calls, or not at all.

POSIX, API, C library, and system call relationships are as follows:

The most popular application programming interface in the UNIX world is given to the POSIX standard .

  The C library implements most of the POSIX standard APIs.

Third, System call

To access system calls (often referred to as Syscall in Linux), this is usually done through function calls defined in the C library.

  A negative return value indicates an error, and a value of0 usually indicates success. System call when an error occurs, the C library writes the error code to the errno global variable, and by invoking the perror () library function, the variable can be translated into a user-understandable error string.

For example : Get the process ID number of the system call Getpid ()

Asmlinkage long sys_getpid (void) {

Return current->tgid;

}

    • The asmlinkage qualifier is a compiler directive that informs the compiler to extract only the parameters of a function from the stack (all system calls require this qualifier);
    • The kernel returns a long, and the user space returns intto ensure compatibility with 32-bit/64-bit systems;
    • Get_pid is defined in the kernel as Sys_getpid (), and all system calls in Linux are defined so that bar () is defined as Sys_bar ().

1. System call number

In Linux, each system call number is given a unique system call number, and the process does not refer to the system call name, but instead uses the system call number to correlate the specific system call. The system call number can no longer be changed once it is allocated, and if a system call is deleted, the system call number it occupies is also forbid

Be recycled. Linux has an "not implemented" system call Sys_ni_syscall () to fill in the number of deleted calls.

2. Performance of system calls

Linux system calls perform faster than many other operating systems: Linux has a very short context switching time, and the system invocation handlers and each system call itself are very concise.

Four, system call processing program

User-space programs cannot execute kernel code directly, and a soft interrupt mechanism informs the kernel that a system call needs to be performed. An exception is thrown by a soft interrupt, which causes the system to switch to the kernel state and execute the exception handler code; The exception handler is the system call handler System_call (). in the

The soft interrupt on the x86 system is the interrupt number 128, which triggers the interrupt System_call () via the int $0x80 instruction.

    • Locate the specified system call

On x86, the system call number is passed to the kernel through the EAX register.

    • Parameter passing

On the x86-32 system, Ebx,ecx,edx,esi and EDI store the first five parameters in turn, and if more than six parameters are required, a single register is used to point to the pointer to these parameters in the user space address. Store the return value by EAX.

V. Implementation of System call

1. Implement system call

    • Decide the use, each system call function should be single clear, do not advocate multi-purpose system call;
    • System call parameters, return values and error codes should be clear;
    • Design the interface as much as possible for the future, do not make assumptions about the machine's byte length and byte sequence.

2. Parameter verification

    • Check that all parameters are valid and the most important one is to check that the user-supplied pointers are valid.
    • Before receiving a pointer to a user space, the kernel must ensure that:

1) Pointer to user space memory, the kernel cannot be accessed directly;

2) Pointer to the memory in the user process space, the kernel can not read other process space;

3) Memory cannot bypass access restrictions: The readable memory is marked as readable, writable and writeable, and executable marked as executable.

    • The kernel provides two methods to complete the necessary checks and a back-and-forth copy of the data between the kernel space and the user space:

1) Copy_to_user () is provided to write the data kernel to the user space;

2) Copy_from_user () is provided to read the data core from the user space.

3) The second parameter is assigned the location data to the first parameter specified position, the length is determined by the third parameter. If the execution fails, both returns the number of bytes of the data that was not completed and returns 0 successfully.

4) both Copy_to_user () and copy_from_user () can cause blocking to cause process hibernation.

    • The last check is for legitimate permissions.

VI. System Call Context

The kernel is in the process context when it executes the system call, and the urrent pointer points to the process that raised the system call.

In the context of the process, the kernel can hibernate and can be preempted. So the system call must be reentrant.

1. The last step of binding a system call

    • Add table entries to the system call table;
    • The system call number is defined in <asm/unistd.h>;
    • Compile into the kernel image and put the relevant files under kernel/.

2. Call from user space Access system:

    • System calls are supported by the C library.
    • Linux itself provides a set of macros for direct access to system calls, which are _syscalln () and the range of n is 0-6, which represents the number of arguments that need to be passed to the system call.

3, using system call as the implementation method

The advantages are:

    • System call creation easy, and easy to use;
    • The high performance of Linux system calls is obvious.

The disadvantages are:

    • Requires a system call number, which needs to be officially assigned;
    • System call is added to the stable kernel after curing, the interface can not be changed;
    • System calls need to be assigned to various architectures separately;
    • It is not easy to invoke the system call in the script, nor can it directly access the system call from the file system;
    • Difficult to maintain outside the main kernel tree;
    • System calls are overqualified if only simple information is exchanged.

Alternative methods:

    • Implements a device node and operates on this implementation of read () and write (), IOCTL ();
    • Some interfaces, such as semaphores, can be represented by a file descriptor;
    • Put the added information as a file in the right place of the SYSFS.

Vii. Summary

This chapter describes the system call knowledge, Linux as far as possible to make system calls concise, in fact, Linux is a relatively stable and the function of the more complete operating system. The content of this chapter complements the knowledge on the video, helps us understand the system invocation, and we understand what the system calls are and how they relate to the API and library functions. There is also the process of implementing the system call, and parameter verification, and finally summarizes the advantages and disadvantages of using system call as the implementation method. Helps us to understand and master.

"Linux kernel Design and implementation" chapter fifth

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.