Week Five Linux Learning notes

Source: Internet
Author: User

Fifth system call 5.1 communication with the kernel

The system call adds a middle tier between the user space process and the hardware device. The main role of this layer is three.

    • It provides a hardware abstraction interface for user space.
    • System calls guarantee the system's felt and safety.
    • As mentioned in the 3rd chapter, each process runs in a virtual system, and it is for this reason that the user space and the rest of the system provide such a layer of public interface.

In Linux, system calls are the only means of user-space access to the kernel: they are the only legitimate entry for the kernel except for exceptions and falls.

5.2 API, POSIX and C libraries

Applications are programmed through application programming interfaces (APIs) implemented in user space rather than directly through system tuning.

POSIX is an international standard for application programming interfaces, and C libraries provide most of the POSIX APIs.

Diagrams between POSIX, API, c libraries, and system calls

5.3 System Calls

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

5.3.1 System call number
    1. In Linux, each system call is given a system call number. This makes it possible to associate system calls with this unique number. When a user-space process executes a system call, the system call number is used to indicate which system call is being executed: the process does not mention the name of the system call.
    2. The system call number is quite important, and once the allocation is no longer changed, the compiled application crashes.
    3. The kernel records the list of all registered system calls in the system call table, stored in the Sys_call_ table.
    4. This table specifies a unique system call number for each system call that has been placed.

5.3.2 Performance of system calls

Linux system calls are performed faster than many other operating systems. Linux is a very short transition time of the Earth context is an important reason, the core is optimized to be simple and efficient. Another reason is that the system call handlers and each system call itself are also very concise.

5.4 System call handlers

User-space programs do not directly execute kernel code, so notify the system in some way, telling the kernel to call the system itself and let the system switch to the kernel state.

1. The mechanism for notifying the kernel is implemented by soft interrupts:

By throwing an exception that causes the system to switch to the kernel state to execute the exception handler, the exception handler is actually the system call handler, and the predefined soft interrupt on the x86 system is the interrupt number 128. This interrupt is triggered by the int¥0x80 instruction, which triggers an exception that causes the system to switch to the kernel state and execute the 128th exception handler, which is the system call handler, which is aptly named System_call (). It is closely related to the hardware architecture.

2. Specify the appropriate system call

(1) All system calls fall into the same kernel mode and must pass the system call number to the kernel.

(2) On x86, the system call number is passed to the kernel via the EAX register.

(3) The System_call () function checks for validity by comparing the given system call number to the Nr_syscall.

(4) Perform the appropriate system calls: Call *sys_call_table (,%rax,8)

3. Parameter passing

(1) x86-32 system, Ebx,ecx,edx,esi,edi in order to store the first five parameters.

(2) When 6 or more parameters are required, a separate register is applied to hold a pointer to all of these parameters in the user-space address.

(3) The return value to the user space is also passed through the register. On the x86 system, it is stored in the EAX register.

5.5 Implementation of system calls 1. Implementing system Calls

The first step is to decide the use, not advocating the use of multi-purpose system calls;

注意:  1.系统调用的接口应该力求简洁,参数尽可能少。  2.很多系统调用提供了标志参数以确保向前兼容。标志并不是用来让单个系统调用具有多个不同的行别如前所述,这是不允许的,而是为了即使增加新的功能和选项,也不破坏向后兼容或不需要增加新的系统调用。  3.系统调用设计得越通用越好。不要假设这个系统调用现在怎么用将来也一定就是这么用。系统调用的目的可能不变,但它的用法却可能改变。  4.可移植性和健壮性要好,要确保不对系统调用做错误的假设,否则将来这个调用就可能会崩溃。
2. Parameter Validation

Check that all parameters are valid and correct. A process should not allow the kernel to access resources that it does not have access to.

One of the most important checks is to check whether a user-supplied pointer is valid. Before receiving a pointer to a user space, the kernel must ensure that:

(1) The memory area pointed to by the pointer is a user space, and the process must not spoof the kernel to read the data in the kernel space.

(2) The memory area pointed to by the pointer is in the address space of the process, and the process must not spoof the kernel to read data from other processes.

(3) If it is read, the memory should be marked as readable; if it is write, the memory should be marked as writable; if it is executable, the memory is marked as executable. The process must not bypass the memory access permissions.

Two ways to complete a check and a back-and-forth copy of data between kernel space and user space:

Week Five Linux Learning 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.