First, we will study the top-level directory of the Linux source code tree, which is usually but not always located in/usr/src/linux- . We will not study it too detailed, because the Linux source code often changes, but we will try to make the given information sufficient to find the location of a specific driver or function.
Makefile: This file is the top-level makefile of the entire source code tree. It defines many practical variables and rules, such as the default gcc compilation tag.
Documentation/: This directory contains a lot of practical information about Kernel configuration, ramdisk running, and other tasks, but it is usually out of date ). However, the help entries corresponding to different configuration options are not here-they are inKconfigFile.
arch/: All the Code related to the architecture is in this directory andinclude/asm-Directory. In this directory, each architecture has its own directory. For example, the code for a PowerPC-based computer is located inarch/ppcDirectory. In these directories, you can find the underlying memory management, interrupt processing, early initialization, assembly routines, and so on.
crypto/: This is the encryption API used by the kernel itself.
drivers/: By convention, the code for running the peripheral device can be found in the subdirectory of this directory. Includes video drivers, Nic drivers, underlying SCSI drivers, and other similar drivers. For exampledrivers/netYou can find most of the NIC drivers. Some higher-level code that combines a type of drivers may or may not) be included in the same directory as the underlying driver itself.
fs/: The common File System code is called VFS, that is, Virtual File System) and the Code of different File systems can be found in this directory. Ext2 file system is one of the most widely used file systems in Linux.fs/ext2You can find the code to read the ext2 format. Not all file systems compile or run. For some people looking for Kernel projects, a more uncommon file system will always be an ideal candidate.
include/: In.cMost header files contained at the beginning of the file can be found in this directory.asm-The directory contains include files related to the architecture. Some kernel build processes are created fromasmSpecifyasm-. In this way, you do not need to encode it.cFile#include You can obtain the correct document for that architecture. Other directories contain non-architecture-related header files. If more than one.cIf a struct, constant, or variable is used in the file, it may be placed in one of the header files.
init/: The files in this directory includemain.c, CreateEarly user space early userspace)And other initialization code. It can be consideredmain.cIs the kernel "glue )". In the next section, we will discuss it in depth.main.c. Early user space provided the functions required for Linux kernel boot, which do not need to run on the kernel itself.
ipc/:IPCWhich meansInterprocess communication). It contains the shared memory, semaphores, and other formats of IPC code.
kernel/: Generic kernel-level code that is not suitable for any other location is located here. There are high-level System Call code andprintk()Code, scheduler, and signal processing code. The file name contains a lot of information, so you can usels kernel/Is not always able to accurately guess the function of each file.
lib/: This is a practical routine for all kernel code. Common string operations, debugging routines, and command line parsing code are all located here.
mm/: This directory contains high-level kernel Management Code. The combined use of these routines and underlying architecture-related routines are usually locatedarch//mm/Directory) to implement Virtual memory, VM ). Early memory management will be completed here before the memory subsystem is fully established ), and File Memory ing, page Cache Management, memory allocation, and page clearing in RAM ).
net/: High-level network code. The underlying network driver exchanges data packets with this hierarchical code. This hierarchical code can pass data to the user-layer application based on the data packet, or discard the data, or use it in the kernel.net/coreContains code that can be used by most different network protocols, and somenet/The files in the directory itself are the same. Specific network protocols arenet/. For examplenet/ipv4Directory, you can find the IP version 4) code.
scripts/: The scripts contained in this directory can be used for Kernel construction, but no code is added to the kernel itself. For example, various configuration tools can put their files here.
security/: You can find code for different Linux Security models, such as NSA Security-Enhanced Linux and socket and network Security hook functions hooks, and other Security options.
sound/: The audio card driver and other audio-related code are placed here.
usr/: The code in this directory is used to build a cpio-format archive file that contains the root file system image for early user space.
Where are all these collected?
init/main.cFile is the central link of the entire Linux kernel. Each architecture executes some underlying setup functions, and then runsstart_kernelFunctions ininit/main.c).
The code execution sequence is roughly as follows:
Architecture-specific set-up code (in arch//*) | vThe function start_kernel() (in init/main.c) | vThe function init() (in init/main.c) | vThe user level "init" program
|
More details about the execution sequence
In more detail, what happens is:
- Execute the configuration Code related to the architecture:
- Extract and move the kernel code as needed
- Initialize hardware
- This may include the underlying memory management settings.
- Forward control to function
start_kernel()
start_kernel()Perform the following and other tasks ):
- Print the kernel version and command line
- Start console output
- Enable interrupt
- Calibration delay cycle
- Call
rest_init(), This function will:
- Start a kernel thread to run
init()Function
- Enter idle cycle
init():
- Start other processors on SMP machines)
- Start the device Subsystem
- Mount the root file system
- Release unused kernel memory
- Run
/sbin/initOr/etc/init, Or ...)
User-levelinitThe program is running. It starts the network device and runs on the console.gettyLogon Program) and other tasks.
Add your ownprintkAnd observe the sub-system'sprintkCompared with your ownprintkWhen it appears, you can point out that the subsystem is instart_kernel()Ininit(). For example, if you want to know when the ALSA sound system is initializedprintkAddstart_kernel()Andinit()And then find "Advanced Linux Sound Architecture [...]"printkWhere to print it out.