For many Linux enthusiasts who are interested in the kernel, this article aims to introduce an entry method for interpreting the linux kernel source code, rather than explaining the complex Linux kernel mechanism.
For many Linux enthusiasts who are very interested in the kernel, this article aims to introduce an entry method for interpreting the linux kernel source code, rather than explaining the complicated Linux kernel mechanism;
1. file organization of the core source program:
1. the Linux core source code is usually installed in/usr/src/linux, and it has a very simple numbering convention: any even core (such as 2.0.30) is the core of a stable release, and any odd core (such as 2.1.42) is the core of development.
This article is based on the stable 2.2.5 source code. The second part of the implementation platform is Redhat Linux 6.0.
2. the core source program files are organized in a tree structure. at the top of the source program tree, you will see the following directories:
◆ Arch: The arch sub-directory contains all core code related to the architecture. Each of its subdirectories represents a supported architecture. for example, i386 is about intel
The cpu and its sub-directories that are compatible with the architecture. Generally, PCs are based on this directory;
◆ Include: the include sub-directory includes most of the header files required for compiling the core. Platform-independent header files are in the include/linux subdirectory and are not related to intel
Cpu-related header files are in the include/asm-i386 subdirectory, while the include/scsi directory is the header file directory of the scsi device;
◆ Init: This directory contains the core initialization code (note: it is not the system boot code) and contains two main files. c and Version. c. This is a very good starting point for studying how the core works.
◆ Mm: This directory includes all resources independent from the cpu.
The memory management code of the architecture, such as the distribution and release of the page-based storage management memory. the memory management code related to the architecture is located in arch/*/mm /, for example, arch/i386/mm/Fault. c
◆ Kernel: the main core code. the files in this directory implement the Kernel functions of most linux systems. The most important file is sched. c; similarly, the architecture-related code is in arch/*/kernel;
◆ Drivers: Place all the device Drivers in the system. each driver occupies a sub-directory, for example,/block.
For example, ide (ide. c ). If you want to check how all devices that may contain a file system are initialized, you can check device_setup () in drivers/block/genhd. c (). It not only initializes the hard disk, but also the network, because the network is required when the nfs file system is installed:
For example, Lib stores the core library code; Net, core and network-related code; Ipc, which contains the code for inter-process communication between the core; Fs, all file system code and various types of file operation code. each subdirectory of the code supports a file system, such as fat and ext2; Scripts. this directory contains the script files used to configure the core.
Generally, there is. the depend file and a Makefile file are both auxiliary files used for compilation. reading these two files carefully is helpful for figuring out the relationship and dependency between each file. Moreover, there are also Readme files in some directories, which are descriptions of the files in this directory, which is also conducive to our understanding of the kernel source code;
2. interpreting practice: add a system call for your kernel
Although the Linux kernel source code is organized reasonably and scientifically in a tree structure, all files associated with functions are placed under the same subdirectory, which makes the program more readable. However, the Linux kernel source code is too large and complex. even if a reasonable file organization method is adopted, there are still many associations between files under different directories, some code at the analysis core usually needs to view several other related files, and these files may not be in the same subdirectory.
The complexity of the system and the complexity of the association between documents may be the main reason why many people are afraid of it. Of course, the return of this daunting Labor is also fascinating: you can not only learn a lot of underlying computer knowledge (such as the system guidance described below ), I realized that the entire operating system architecture is exquisite and the algorithm is clever when solving a specific problem in detail. More importantly, in the source code analysis process, you will be professionally specialized at 1.1 points. Even after analyzing the code in 10 million points, you will deeply understand what kind of code is written by a professional programmer, what kind of code is written by a hobbyist.
In order to allow readers to better understand this feature, The following describes a specific kernel analysis instance, this gives readers some specific understanding of the Linux kernel organization, from which they can also learn some kernel analysis methods.
The following is an analysis instance:
A. Operating Platform:
Hardware: cpu intel Pentium II;
Software: Redhat Linux 6.0; kernel version 2.2.5
B. kernel source code analysis:
1. system Boot and initialization: There are several Boot methods for Linux: Lilo, Loadin, and Linux bootstrap (bootsect-loader ), the source program corresponding to the latter is arch/i386/boot/bootsect. s, which is an assembly program in the real mode. the length is not analyzed here. no matter which boot mode is used, the system will jump to arch/i386/Kernel/setup. s, setup. S is the initialization in the progressive mode to prepare the system for entering the protection mode. after that, the system executes
Arch/i386/kernel/head. S (perform arch/i386/boot/compressed/head. S for the compressed kernel );
Head. setup_idt, an assembly program defined in S, is responsible for creating an idt table (Interrupt DescriptorTable) with 256 items. this table stores all the inactive and inactive endpoints; this includes the entry address of system_call, the general control program of the system call. of course, in addition, head. S also needs to do some initialization work;
2. the first kernel program asmlinkage void _ init start_kernel (void) that runs after system initialization is defined in
In/usr/src/linux/init/main. c, it calls a function in usr/src/linux/arch/i386/kernel/traps. c.
Void _ init trap_init (void) sets the entry address of each sink and interrupt service program to idt
In the table, the system calls the general control program system_cal, which is one of the interrupted service programs. The void _ init trap_init (void) function calls a macro
Set_system_gate (SYSCALL_VECTOR, & system_call); hangs the entry of the system call control program on the interrupt 0x80. the SYSCALL_VECTOR is defined in/usr/src/linux/arch/i386/kernel/irq. a constant of 0x80 in h, and system_call is the entry address for interrupting the Master Control Program; the interrupt control program is defined in/usr/src/linux/arch/i386/kernel/entry. s;
3. the interrupt control program is mainly responsible for storing the status before the processor executes the system call, checking whether the current call is legal, and according to the system call vector, redirect the processor to the entry of the corresponding system service routine saved in the sys_call_table table; restore the processor status and return it to the user program after the system service routine returns; the system call vector is defined in/usr/src/linux/include/asm-386/unistd. the sys_call_table table is defined in/usr/src/linux/arch/i386/kernel/entry. at the same time in/usr/src/linux/include/asm-386/unistd. h also defines the user programming interface called by the system;
4. it can be seen that the linux system call is also like the dos system's int 21 h interrupt service. it uses 0x80 interrupt as the total entry, and then forwards it to
The entry addresses of various interrupt service routines in the sys_call_table table form different types of interrupt services. from the above source code analysis, we can see that to add a system call, we must add one in the sys_call_table table, save the entry address of your system service routine and re-compile the kernel. of course, the system service routine is essential.
It can be seen that in the Linux kernel source program of this version, the source program files related to system calls include the following:
- Arch/i386/boot/bootsect. S
- Arch/i386/Kernel/setup. S
- Arch/i386/boot/compressed/head. S
- Arch/i386/kernel/head. S
- Init/main. c
- Arch/i386/kernel/traps. c
- Arch/i386/kernel/entry. S
- Arch/i386/kernel/irq. h
- Include/asm-386/unistd. h
Of course, this only involves several major files, and in fact, adding a system call really wants to modify files only include/asm-386/unistd. h and arch/i386/kernel/entry. S.
C. modify the kernel source code:
1. add the system service routine in kernel/sys. c as follows:
- Asmlinkage int sys_addtotal (int numdata)
- {
- Int I = 0, enddata = 0;
- While (I <= numdata)
- Enddata + = I ++;
- Return enddata;
- }
This function has an int-type entry parameter numdata and returns the accumulated value from 0 to numdata. of course, you can also place the system service routine in a custom file or other file, only necessary instructions should be made in the corresponding file;
2. add the entry address of asmlinkage int sys_addtotal (int) to the sys_call_table table:
The last few lines of source code in arch/i386/kernel/entry. S are changed:
- ......
- . Long SYMBOL_NAME (sys_sendfile)
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams1 */
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams2 */
- . Long SYMBOL_NAME (sys_vfork)/* 190 */
- . Rept NR_syscalls-190
- . Long SYMBOL_NAME (sys_ni_syscall)
- . Endr
Modified :......
- . Long SYMBOL_NAME (sys_sendfile)
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams1 */
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams2 */
- . Long SYMBOL_NAME (sys_vfork)/* 190 */
- /* Add by I */
- . Long SYMBOL_NAME (sys_addtotal)
- . Rept NR_syscalls-191
- . Long SYMBOL_NAME (sys_ni_syscall)
- . Endr
3. put the vector corresponding to the added sys_call_table table entry in include/asm-386/unistd. h for user processes and other system processes to query or call: Added part/usr/src/linux/include/asm-386/unistd. the h file is as follows:
- ......
- # Define _ NR_sendfile 187
- # Define _ NR_getpmsg 188
- # Define _ NR_putpmsg 189
- # Define _ NR_vfork 190
- /* Add by I */
- # Define _ NR_addtotal 191
4. the test program (test. c) is as follows:
- # Include
- # Include
- _ Syscall1 (int, addtotal, int, num)
- Main ()
- {
- Int I, j;
- Do
- Printf ("Please input a numbern ");
- While (scanf ("% d", & I) = EOF );
- If (j = addtotal (I) =-1)
- Printf ("Error occurred in syscall-addtotal (); n ");
- Printf ("Total from 0 to % d is % d n", I, j );
- }
Compile the new kernel after modification and guide it as a new operating system. after running several programs, you can see that everything is normal; compile the test program in the new system (* Note: Because the original kernel does not provide this system call, only in the new kernel after compilation, this test program can be compiled and passed). The running conditions are as follows:
$ Gcc-o test. c
$./Test
Please input a number
36
Total from 0 to 36 is 666
It can be seen that the modification is successful. further analysis of the relevant source code shows that in the kernel of this version, from/usr/src/linux/arch/i386/kernel/entry. S
The settings of the sys_call_table table in the file show that the service routines called by several systems are defined in the same function in/usr/src/linux/kernel/sys. c:
- Asmlinkage int sys_ni_syscall (void)
- {
- Return-ENOSYS;
- }
For example, this is true for items 188th and 189th:
- ......
- . Long SYMBOL_NAME (sys_sendfile)
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams1 */
- . Long SYMBOL_NAME (sys_ni_syscall)/* streams2 */
- . Long SYMBOL_NAME (sys_vfork)/* 190 */
- ......
The two items are declared in the file/usr/src/linux/include/asm-386/unistd. h as follows:
- ......
- # Define _ NR_sendfile 187
- # Define _ NR_getpmsg 188/* some people actually want streams */
- # Define _ NR_putpmsg 189/* some people actually want streams */
- # Define _ NR_vfork 190
It can be seen that in the kernel source code of this version, the asmlinkage int sys_ni_syscall (void) function does not perform any operation, so several system calls, including getpmsg and putpmsg, do not perform any operation, that is, the air conditioners to be expanded are used, but they still occupy the sys_call_table table items. it is estimated that they are arranged by the designers to facilitate the expansion of system calls;
Therefore, you only need to add the corresponding service routines (for example, add the service routines getmsg or putpmsg) to increase system calls.