because the operating system kernel is close to the hardware, you need to understand the computer architecture, such as the CPU operating principle, interface features, and bios calls. Don't be discouraged. Here we will introduce some essential skills for reading the Linux kernel source code.
obtain the kernel
in Linux systems,/usr/src/Linux *. *. *(*. *. * Indicates the kernel version, such 2.4.23 ) directory is the kernel Source Code (if there is no similar directory, because the kernel Code is not installed yet ). You can also download it from the Internet for free. Note: Do not download images at http://www.kernel.org. Please go. There are two types of code packages, namely linux-2.4.23.tar.gzand linux-2.4.23.tar.bz2. The content of the two code packages is the same, but the compressed Program is different .. Gzis compressed by gzip.zip bz2 is compressed by Bzip2. Bzip2 has better compression capability than gzip.
Code directory structure
Before reading the source code, you should also know the overall distribution of the Linux kernel source code. Modern operating systems are generally composed of process management, memory management, file systems, drivers, and networks. The Linux kernel source code directories correspond roughly to this, and their composition is as follows (assuming relative to the Linux-2.4.23 directory ):
◆ Arch The directory contains all the core Code related to the architecture. Each subdirectory under it represents a Linux-supported architecture. For example, i386 is the subdirectory of the Intel cpu and its compatible architecture. Generally, PCs are based on this directory.
◆ Include The Directory includes most of the header files required for compiling the core. For example, the platform-independent header files are in the include/Linux subdirectory.
◆ Init The directory contains the core initialization Code (not the System Boot Code), including the main. C and version. c files. This is a good starting point for studying how to work.
◆ Mm The directory contains all the memory management code. The Memory Management Code related to the specific hardware architecture is located in the arch/*/mm directory.
◆ Drivers The directory contains all the device drivers in the system. It is further divided into several types of device drivers, each with corresponding sub-directories, such as the driver of the Sound Card corresponding to drivers/sound.
◆ IPC The directory contains the communication code between core processes.
◆ Modules The directory stores the created modules that can be dynamically loaded.
◆ FS Directory stores the file system code supported by Linux. Different file systems have different subdirectories. For example, ext3 file systems correspond to ext3 sub-directories.
Kernel The core code of kernel management is put here. At the same time, the Code related to the processor structure is stored in the arch/*/kernel directory.
◆ Net The directory is part of the core network code. Each subdirectory corresponds to one aspect of the network.
◆ Lib The directory contains the core library code, but the library Code related to the processor structure is stored in the arch/*/lib/directory.
◆ Scripts The directory contains the script file used to configure the core.
◆ Documentation The directory contains some documents, which are specific descriptions of the role of each directory.
Generally, each directory contains a. Depend file and a MAKEFILE file. Both files are auxiliary files used for compilation. Reading these two documents carefully is helpful for figuring out the relationship and dependency between each file. In addition, there are readme files in some directories, which are descriptions of the files in the directory, which is also conducive to understanding the kernel source code.
reading started
There are vertical and horizontal reading methods or sequences. The vertical direction is to follow the execution sequence of the program. The horizontal direction is to follow the module. They are often combined. The Linux Startup code can be read step by step according to the Linux Startup sequence. For the memory management part, you can read and analyze it separately. In fact, this is a repetitive process, and it is impossible to read it once.
lxr (http://lxr.linux.no) is a good tool to assist reading, it can create an index database for the specified source code file and dynamically generate a Web page containing the source code using the Perl script. On this web page, all variables, constants, and functions are provided in the form of hyper-connections, which is very convenient for reference. In addition, lxr provides identifier searches and file searches, combined with the glimpse (http://glimpse.cs.arizona.edu) program can also perform full-text searches for all source files, even including comments. For the installation method, refer to the Help file in the code. There is also a tool for Linux kernel reading under Windows called source insight (which can be downloaded from the http://www.sourcedyn.com ).