"Linux kernel design and implementation" Reading notes chapter fifth system calls

Source: Internet
Author: User

Fifth Chapter system Calls

A system call is an interface that a user process interacts with the kernel. In order to protect the system stable and reliable, avoid the application wanton carried away.

5 . 1 communication with the kernel

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

Function: Provides a hardware abstraction interface for user space, ensures the stability and security of the system, avoids improper use of hardware by the application, steals resources from other processes, or makes behavior that endangers the system, in order to achieve multitasking and virtual memory.

Linux provides much less system calls than most operating systems.

5.2 API , POSIX , and C Library

An API defines the programming interfaces used by a set of applications. (API and system calls are not one by one) APIs can be implemented on a variety of operating systems, providing the exact same interface to the application, and the implementation of the API itself may differ in different systems.

The UNIX API is based on the Posix,linux effort to be compatible with POSIX and SUSv3

Linux system calls are provided as part of the C library.

5.3 system Calls

Access system calls are typically made through function calls defined in the C library, and typically require parameters to be defined, and may have side effects that can cause the system state to change. The system call also returns a long type of value indicating success or error.

Define system call: Asmlinkage long sys_***

Qualifier return type naming rules

System call Number: Each system call is given a system call number , which indicates the system call with the system call number when the user space executes the system call. Once the assignment is no longer changed, the system call is deleted and its system call number is not allowed to be recycled. The kernel records the list of all registered system calls in the system call table, stored in sys_call_table

Performance of system calls: Linux system calls are faster than other operating systems because the context switch time is short; system invoker and system calls are simple.

5.4 system call Handlers

The kernel resides on the protected address space, so the application notifies the kernel that it needs to use system calls. Notifies the kernel that it is implemented by a soft interrupt and the system switches to the kernel state when an exception is thrown. Execute 128th Exception Handler (System call handler) by triggering interrupt with int $0x80

Specify the appropriate system call: The system call number on the X86 is passed to the kernel via the EAX register

Parameter passing: In addition to the system call number, it is necessary to pass external parameters, which are stored sequentially in Ebx,ecx,edx,esi and EDI five registers, and rarely require six parameters.

5.5 implementation of system calls

Implement system call: 1, decide the use (do not promote the use of multi-purpose); 2, the parameters of the call, the return value and the error code should be what (excuses to be concise, the parameters as little as possible, to stabilize); 3, design interface as far as possible for the future do consider (whether there are unnecessary restrictions, whether portable)

Provide mechanisms ( Mechaniam ) does not provide a policy ( Policy )

Parameter validation: You must carefully check that the parameters are valid, because the system calls execute in kernel space, and if there is an illegal input, it threatens the security and stability of the system. The most important check is to check that the user-supplied pointer is valid. Before receiving a pointer to a user space, the kernel must guarantee that 1, the memory area that the pointer points to belongs to the user space. 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. The process must not spoof the kernel to read data from other processes, 3. The process must not bypass the memory access limit.

The kernel provides copy_to_user (), Copy_from_user () Two methods to complete the checking of copy data

5.6 System Call Context

The kernel is in the process context when it executes system calls, and in the context of the process, the kernel can hibernate and can be preempted. Hibernation indicates that system calls can use most of the functionality provided by the kernel. You can preempt that the new process can use the same system call.

The last step of binding a system call: 1, add a table entry at the end of the system call table, 2, the system call number must be defined in <asm/unistd.h>;3, the system call must be compiled into the kernel image.

Access system calls from User space: Write only system call GILC I'm afraid I don't support it. Linux itself provides a set of macro _syscalln (), the range of n from 0 to 6, which represents the number of arguments that need to be passed to the system call.

The benefits of creating a new system call: 1, the system call is easy to create and easy to use, 2, system calls are very efficient.

Cons: 1, requires the system call number, which requires a kernel in the development version of the time, the official assigned to you, 2, the system calls the interface does not allow changes, 3, the system calls need to be registered in each of the required supporting architecture; 4. It is not easy to invoke system calls in scripts. Can not directly access the system calls from the file system, 5, for the simple information exchange, system calls overqualified;

"Linux kernel design and implementation" Reading notes chapter fifth system calls

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.