Fifth Chapter system call
5.1 communicating with the kernel
1. The system call adds a middle layer between the user space process and the hardware device, which has a primary role of three:
(1) Provides a hardware abstraction interface for user space
(2) system call to ensure the stability and security of the system
(3) Each process runs in a virtual system, while the user space and the rest of the system provide such a layer of public interface.
2, in Linux, system calls are the only means of user space access to the kernel: In addition to exceptions and fall, they are the kernel's only legitimate entry.
5.2 API , POSIX, and C libraries
1, in general, the application through the user space implementation of the application Programming interface (API) rather than directly through the system to programming.
2. An API defines a set of programming interfaces used by an application.
3. The relationship between API, POSIX, c libraries, and system calls:
4, in the Unix world, the most popular application programming interface is based on the POSIX standard.
5. Linux system calls are provided as part of the C library.
6. C Library implements the main API of UNIX system, including standard C library function and system call interface.
5.3 system Calls
First, the system calls
1, to access the system call (Syscall), usually through the C library defined in the function call to do.
2, the system call through a long type of return value to indicate success or error. Usually, but not absolutely, a negative return value is used to indicate an error. Returning a value of 0 usually indicates success. System call in the event of an error C library will write the error code to the errno global variable, by calling the Perror () library function, you can translate the variable into the user can understand the error string.
3, system calls in the user space and kernel space has a different return value type, in user space is int, in the kernel space is long.
Second, the system call number
1. When the process of user space executes a system call, it uses the system call number to indicate which system call to execute. The process does not mention the name of the system call.
2, the system call number is unique, once the allocation can no longer have any changes. Otherwise, the compiled application crashes.
3, Linux has an "not implemented" system call Sys_ni_syscall (), which in addition to return-enosys do not do any other work, this error number is specifically for the invalid system call set.
4. The kernel records the list of all registered system calls in the system call table, stored in sys_call_table.
Third, the performance of system calls
1. Linux system calls are performed faster than many other operating systems.
2. Reason:
(1) The context switch time is short.
(2) system call handlers and each system call itself are also very concise.
5.4 system call Handlers
First, the system calls the handler
1, the mechanism to notify the kernel is soft interrupt implementation: by throwing an exception to promote the system to switch to the kernel state to point to the exception handler, at this time the exception handler is the system call handler.
2, the predefined soft interrupt on the x86 system is the interrupt number 128, which is triggered by the int $0x80 instruction.
Second, specify 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_syscalls. Greater than or equal to return-enosys, otherwise execute the appropriate system call: called *sys_call_table (,%rax,8)
Third, parameter transfer
1. On the x86-32 system, Ebx,ecx,edx,esi and EDI store the first five parameters sequentially. Requires 6 or more parameters, applying a separate register to hold pointers to these parameters in the user-space address.
2, 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
First, implement system call
1, the first step to implement a new system call is to determine its purpose. Multi-purpose system calls are not advocated in Linux.
2, the system calls the interface should strive to be concise.
3, the system calls the more general design, the better.
4. Provide a mechanism rather than a strategy.
5. Be aware of portability and robustness at all times.
Second, parameter verification
1, the most important check: check whether the user-supplied pointer is valid.
2. Before accepting a pointer to a user space, the kernel must ensure that:
(1) The memory area pointed to by the pointer must belong to the user space.
(2) The memory area pointed to by the pointer is in the address space of the process.
(3) The memory access limit must not be bypassed.
3. Two methods check the back-and-forth copy of data between the kernel space and the user space:
(1) Write the data to the user space, the kernel provides the copy_to_user ();
(2) from the user space to read the data, the kernel provides copy_from_user ().
If successful: returns 0;
If failure: return to standard-efault
4, Copy_to_user () and Copy_from_user () are likely to cause blockages. When a page containing user data is swapped out on a hard disk rather than in physical memory, it can cause blocking, and the process will hibernate until the page-fault handler re-swapped the pages from the hard disk back to physical memory.
5, check whether there is legal rights--capable (): Return 0 does not have permission to operate, return non 0 permission to operate.
5.6 System Call Context
One, 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.
Second, the last step of binding a system call
1. Add a table entry at the end of the system call table.
2. For the various architectures supported, the system call number must be defined in <asm/unistd.h>.
3, the system call must be compiled into the kernel image (cannot be compiled into a module). Put in a related file under kernel/.
Third, from the user space Access system calls
1, System call by C library support, the user program by including the standard header file and C library link, you can use the system call.
2, Linux itself provides a set of macros for direct access to system calls. It sets the register and calls into the command. These macros are _syscalln (), where n ranges from 0 to 6, representing the number of arguments that need to be passed to the system call.
Four, why not through the system call the way to implement
1. The benefits of establishing a new system call:
(1) System call creation is easy and convenient to use.
(2) The high performance of Linux system calls is obvious.
2, the problem is:
(1) 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.
(2) system call is added to the stable kernel is cured, in order to avoid the application of the crash, its interface is not allowed to do motion.
(3) system calls need to be registered separately in each architecture that needs to be supported.
(4) It is not easy to invoke a system call in a script, nor can it directly access system calls from the file system.
(5) If only simple information exchange, the system call is overqualified.
3. Alternative methods:
Implement a device node and implement read () and write () on this. Use the IOCTL () to manipulate specific settings or to retrieve specific information.
(1) Some interfaces, such as semaphores, can be represented by file descriptors, so they can be manipulated in the way described above.
(2) Put the added information as a file in the appropriate location of the SYSFS.
4, the Linux system to avoid each emergence of a new abstraction simply add a new system call.
Summarize
System call is a set of interfaces that the operating system provides to interact with hardware devices (such as CPUs, disks, printers, and so on) that are running in the user state. When a system call is required for a user process, the CPU switches to the kernel state through a soft interrupt to begin executing the kernel system call function.
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.
Resources
The 3rd edition of the original book "Linux Kernel Design and implementation"
Linux kernel Analysis--fifth chapter system call