"Linux kernel design and implementation" Reading notes the fifth chapter-system call

Source: Internet
Author: User
Tags posix

5th Chapter System Call

The operating system provides the interface mainly to ensure that the system is stable and reliable, to avoid arbitrary application.

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:

  • Provides a hardware abstraction interface for user space.
  • System call to ensure the stability and security of the system
  • Each process runs in a virtual system, and the user space and the rest of the system provide such a layer of public interfaces.
5.2 API, POSIX, and C libraries

1, the API can be implemented in a variety of operating systems, to provide the exact same interface to the application, and they themselves on these systems may be different implementations.

2, in the Unix world, the most popular application programming interface is based on the POSIX standard.

3. C Library includes:

  • Standard C library functions
  • System Call Interface

4. provide a mechanism rather than a strategy

5.3 System Calls

1. System call--often called Syscall in Linux

2, the system call again error when the C library will write the error code errno global variables. By calling the Perror () library function, you can translate the variable into an error string that the user can understand.

3. SYSCALL_DEFINE0 is just a macro that defines a system call without parameters.

3. Asmlinkage: This is a compilation instruction that tells the compiler to extract only the parameters of the function from the stack.

  • This qualifier is required for all system calls.
  • The next function returns a long.
    System calls have different return value types in user space and kernel space, where the user space is int in kernel space is long.
5.3.1 System call number

1, the system call number once assigned can no longer have any changes.

  • 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 called 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.

  • In x86-64, it is defined in the arch/i386/kernel/syscall_64.c file.
5.3.2 Performance of system calls

The reason why the Linux system executes fast:

  • Very short context switching time.
  • System call handlers and each system call itself are also very concise.
5.4 System call handlers

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

  • The exception handler is actually the system call handler by throwing an exception that causes the system to switch to the kernel state to execute the exception handler.
  • It is closely related to the hardware architecture.
5.4.1 specifying the appropriate system call

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

2. The System_call () function checks its validity by comparing the given system call number to Nr--syscall.

3. Because the table entries in the system call table are stored in a 64-bit type, the kernel needs to multiply the given system call number by 4 and then query its location in the table with the resulting results.

5.4.2 parameter passing

The simplest approach is to put these parameters in the register as well as passing the system call number.

5.5 Implementation of system call 5.5.1 implement system call

(1) determine its purpose.

  • Many system calls provide flag parameters to ensure forward compatibility. Flags are not used to allow a single system call to have multiple different row behaviors, in order to add new features and options, do not break backwards compatibility, or add new system calls.

(2) Is this system call portable?

  • Provides a mechanism rather than a policy.
5.5.2 Parameter Validation
    • System calls related to file I/O must check that the file descriptor is valid.
    • Process-related functions must check that the provided PID is valid.

One of the most important checks is to check whether a user-supplied pointer is valid.

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, and the process must not bypass the memory access limit.

The last check is for legitimate permissions.

5.6 System Call Context

1. 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.

2, in the context of the process, the kernel can hibernate and can be preempted.

3, when the system call returns, the control is still in System_call (), it will eventually be responsible for switching to user space, and let the user process continue to execute.

5.6.1 The last step of binding a system call

1. 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. The system call number is the part of the architecture ABI (Application Binary interface)

5.6.2 access system calls from user space

1. The user program can use the system call by including the standard header file and the C library link.

2, for each macro, there are 2+2*n parameters.
-the first parameter corresponds to the return value type of the system call.
-the second parameter is the name of the system call
-the type and name of each parameter that is later arranged in the order of the system invocation parameters.

5.6.3 why not implement by system call

The benefits of building 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.
5.7 Summary

1, what is the system call in the end.
2. How they relate to library functions and application interfaces.
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" Reading notes the fifth chapter-system call

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.