Design and Implementation of Linux kernel Reading Notes (5)-system calls

Source: Internet
Author: User


Reading Notes on Linux kernel design and implementation (5)-Main content of system calling: What is the implementation principle of system calling on Linux? A simple implementation of system calling 1. in simple terms, system calls are a bridge between user programs and hardware devices. The user program calls the hardware device as needed. The existence of system calls has the following important meanings:
1) The user program uses hardware through system calls without having to care about specific hardware devices. This greatly simplifies the development of user programs. For example, a user program can write data to a file by calling the write () system without worrying about whether the file is on a disk or a floppy disk or other storage. 2) system calls make user programs more portable. As long as the system call interfaces provided by the operating system are the same, the user program can migrate from one system to another without modification.
3) system calls allow inner nuclear energy to better manage user programs and enhance system stability. Because system calls are implemented by the kernel, the kernel uses system calls to control what functions and permissions are provided to the user program. In this way, the user program can avoid improper use of hardware devices, thus destroying other programs. 4) system calls effectively isolate the development of user programs and kernels. A user program only needs to focus on calling APIS by the system to develop their own applications. The kernel only needs to care about the implementation of the system call API, without having to worry about how they are called. The call relationships between user programs, system calls, kernels, and hardware devices are as follows:

2. the implementation principle of system calls on Linux must be implemented in the following aspects: notify the kernel to call a system call user program to pass system call parameters to the kernel user program to obtain the system call return values returned by the kernel. The following shows how Linux implements the above three functions. 2.1 notify the kernel to call a system. Each system call has a system call number. When a system call occurs, the kernel knows which system call is called Based on the incoming system call number. In x86 architecture, the user space places the system call number in eax, and the system call handler obtains the system call number through eax. The system call number is defined in the kernel code: arch/alpha/include/asm/unistd. h. We can see that there are not many linux system calls. 2.2 The user program transmits the system call parameters to the kernel system call parameters through registers to the kernel. on x86 systems, the first five parameters called by the system are placed in ebx, ecx, in edx, esi, and edi, if there are many parameters, you also need to use a separate register to store pointers to all parameters in the user space address. Generally, system calls are accessed through the C library (the most commonly used glibc library). the Linux Kernel provides a method to directly access system calls from user programs. Refer to the kernel code: arch/cris/include/arch-v10/arch/unistd. h, which defines 6 macros, the number of parameters can be called respectively 0 ~ 6. The system calls www.2cto.com _ syscall0 (type, name) _ syscall1 (type, name, type1, arg1) _ syscall2 (type, name, type1, arg1, type2, arg2) _ syscall3 (type, name, type1, arg1, type2, arg2, type3, arg3) _ syscall4 (type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4) _ syscall5 (type, name, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) _ syscall6 (type, name, type1, arg1, systems with more than 6 parameters: type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) Calls are rare, so only six are defined here. 2.3 The user program obtains the system call return value returned by the kernel. The system call return value is also obtained through registers. on x86 systems, the return value is placed in eax. 3. A simple implementation of system calling understands the principle of system calling on Linux. You can implement a simple system call by yourself. 3.1 prepare the environment www.2cto.com. In order not to damage the existing system, I used virtual machines to experiment. HOST: fedora16 x86_64 system + kvm (a virtual technology like virtualbox, vmware, etc.) Virtual Machine: it is also installed with fedora16 x86_64 System (it is easy to install a system through virt-manager) download the kernel source code: www.kernel.org download the latest on the line 3.2 modify the corresponding file in the kernel source code mainly modify the following files: arch/x86/ia32/ia32entry. sarch/x86/include/asm/unistd_32.harch/x86/include/asm/unistd_64.harch/x86/kernel/syscall_table_32.Sinclude/asm-generic/unistd. hinclude/linux/syscils. hkernel/sys. c I am in sys. c adds two functions: sys_foo and sys_bar. For system calling, you only need to modify arch/x86/include/asm/unistd_64.h, such as sys_bar. For more information, see the following diff file: diff-r new/arch/x86/ia32/ia32entry. S old/arch/x86/ia32/ia32entry. s855d854 <. quad sys_foodiff-r new/arch/x86/include/asm/unistd_32.h old/arch/x86/include/asm/templates <# define _ NR_foo 349361c360 <# define nr_syscils 350 --- www.2cto.com> # define nr_syscall349 diff-r new/arch/x86/include/asm/unistd_64.h old/arch/x86/include/asm/unistd_64.h689, 692d688 <# define _ NR_foo 312 <_ SYSCALL (_ NR_foo, sys_foo) <# define _ NR_bar 313 <_ SYSCALL (_ NR_bar, sys_bar) diff-r new/arch/x86/kernel/syscall_table_32.S old/arch/x86/kernel/syscall_table_32.S351d350 <. long sys_foodiff-r new/include/asm-generic/unistd. h old/include/asm-generic/unistd. h694, 695d693 <# define _ NR_foo 272 <_ SYSCALL (_ NR_foo, sys_foo) 698c696 <# define _ nr_syscils 273 ---> # define _ nr_syscils 272 diff-r new/kernel/sys. c old/kernel/sys. c1920, 1928d1919 <asmlinkage long sys_foo (void) <{<return 1112223334444555; <} <asmlinkage long sys_bar (void) <{<return 1234567890; <} www.2cto.com 3.3 compile the kernel # cd linux-3.2.28 # make menuconfig (select to compile parameters, if not familiar with the kernel compilation, use the default option) # make all (this step takes a long time ......) # make modules_install # make install (this step will add the new kernel to the startup Item) # reboot (restart the system to enter the new kernel) 3.4 compile the called System Call Code # include <unistd. h> # include <sys/syscall. h> # include <string. h> # include <stdio. h> # include <errno. h> # define _ NR_foo 312 # define _ NR_bar 313 www.2cto.com int main () {printf ("result foo is % ld \ n", syscall (_ NR_foo )); printf ("% s \ n", strerror (errno); printf ("result bar is % ld \ n", syscall (_ NR_bar )); printf ("% s \ n", strerror (errno); return 0;} compile and run the above Code: # gcc test. c-o test #. /test running result: result foo is 1112223334444555 Successresult bar is 1234567890 Success
 

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.