"Linux kernel design and implementation" Chapter 5 reading notes

Source: Internet
Author: User
Tags posix

"Linux kernel design and implementation" Chapter 5 reading notes

In modern operating systems, the kernel provides a set of interfaces that user processes interact with the kernel, which are:

    • Restrict application access to hardware devices
    • Provides a mechanism for creating new processes to communicate with existing processes
    • Provides the ability to request additional resources for the operating system
First, communication with the kernel 1, the role of system calls

The system call adds a middle layer between the user space process and the hardware device, with the following functions:

    1. Provides a hardware abstraction interface for user space;
    2. System calls ensure the stability and security of the system, that is, the application can avoid improper use of hardware devices, stealing other processes of resources;
    3. Each process runs in a virtual system, and the user space and the rest of the system provide such a layer of public interfaces.
Ii. API, POSIX, and C libraries

In general, applications are programmed by application programming interfaces (APIs) implemented in user space.

APIs can be implemented in a variety of operating systems, giving applications exactly the same interface, and their own implementations on these systems can be quite different.

In the Unix world, the most popular application programming interface is based on the POSIX standard. The POSIX standard is modeled after the interface of an earlier UNIX system.

The C library implements the main API for UNIX systems. In addition, the C library also provides most of the POSIX APIs.

The C library includes:

    1. Standard C library functions
    2. System Call Interface
Third, System call

To access system calls, it is usually done through function calls defined in the C library.

Typically, a negative return value indicates an error. A return value of 0 indicates success.

System call when an error occurs, the C library writes the error code to the errno global variable. By calling the Perror () library function, you can translate the variable into an error string that the user can understand.

SYSCALL_DEFINE0 is just a macro that defines a parameterless system call with the following code:

   $  long sys_getpid (void)

Asmlinkage is a compilation instruction

function returns long to ensure compatibility with 32-bit 64-bit systems

1. System call number

In Linux, each system call is given a system call number

1. Once the system call number is assigned, no further changes can be made.

    • If a system call is deleted, the system call number it occupies is also not allowed to be recycled.
    • Linux has an "not implemented" system call Sysnisyscall (), which does not do any other work except to return ―enosys, which is specifically designed for invalid system calls.

2. The kernel records the list of all registered system calls in the system call table, which is stored in theSys called.

2. Performance of system calls

Linux system calls are much faster than other operating systems:

    • Linux Context switch time is short
    • System call handlers and each system call itself is concise
Four, system call processing program

User-space programs cannot directly invoke functions in kernel space, it notifies the kernel in the form of soft interrupts.

The interrupt number of a predefined soft interrupt on the x86 system is 128, triggered by an int $0x80 instruction

1, specify the appropriate system call
    1. On x86, the system call number is passed to the kernel through the EAX register.
    2. The System_call () function checks for validity by comparing a given system call number to Nr_syscall.
2. Parameter passing
    1. When a sink occurs, the parameters should be passed from user space to the kernel.
    2. On the x86-32 system, EBX, ECX, edx, ESI, EDI, put the first five parameters in sequence
V. Implementation of system call 1, implement system call

System calls provide a mechanism rather than a policy

2. Parameter verification

1. The most important check: whether the user-supplied pointer is valid.

2. Before accepting a pointer between users, the kernel must ensure that:

    • The memory area that the pointer points to belongs to user space.
    • The memory area pointed to by the pointer is in the address space of the process
    • The process must not bypass memory access restrictions.

3. Two methods to check the back-and-forth copy of data between two spaces

    • Writes data to user space--copy_to_user ();
    • reads Data--copy_from_user () from user space. Success: return 0; failure: Return to standard -efault

Note: The above two approaches may cause blocking when用户数据的页被换出到硬盘上而不在物理内存上,进程会休眠,直至被换回物理内存。

VI. System Call Context
    • The kernel is in the process context when executing system calls;
    • The current pointer points to the present task, which is the process that raised the system call;
    • In the context of the process, the kernel can hibernate and can be preempted;
    • When the system call returns, System_call () is responsible for switching to the user space and allowing the user to continue the execution.
1. The last step of binding a system call

When a system call is written, registering it as a formal system call is trivial work:

    • First, add a table entry at the end of the system call table.
    • For each of the supported architectures, the system call number must be defined in <asm/unistd.h>.
    • System calls must be compiled into the kernel image (cannot be compiled into a module). For example, SYS.C, it contains a variety of system calls.
2. Accessing system calls from user space
    • Linux provides a set of macros: _syscalln () (The range of N: 0 to 6, which represents the number of arguments passed to the system call)
3. Why not implement the system call method

The benefits of establishing a new system call:

    • System calls are easy to create and easier to use.
    • The high performance of Linux system calls is obvious.

The problem is:

    • You need a system call number, which requires a kernel to be assigned to you by the official when it is in the development version.
    • System call is added to the stable kernel, it is cured, its interface is not allowed to make changes.
    • System calls need to be registered separately in each architecture that needs to be supported.
    • It is not easy to invoke a system call in a script, nor can it directly access system calls from the file system.
    • Because you need the system call number, it is difficult to maintain and use system calls outside of the main kernel tree.

Alternative methods:

Implement a device node and implement read () and write () on this. Use specific information for retrieval.

    • Some interfaces, such as semaphores, can be represented by file descriptors, so they can be manipulated in the same way.
    • Put the added information as a file in the right place of the SYSFS.
Vii. Summary 1, what is the system call in the end.
2. How the Linux kernel implements system calls
3, the execution of system call Chain reaction:
    • Trapped in the kernel
    • Passing system call numbers and parameters
    • Perform the correct system call function
    • and bring the return value back to the user space.

"Linux kernel design and implementation" Chapter 5 reading notes

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.