Home>Article>Operation and Maintenance> Linux kernel source code of five major modules and overall kernel architecture design (detailed graphic and text explanation)
This article brings you relevant knowledge about the kernel source code of the five major modules ofLinux, including issues related to the overall architecture design of the kernel. I hope it will be helpful to everyone.
Related recommendations: "Linux Video Tutorial"
This article is " The "Linux Kernel Source Code Analysis" series of majors will take the core functions of the kernel as the starting point to describe the overall architecture of the Linux kernel and the main software subsystems under the architecture. After that, the directory structure of the Linux kernel source files will be introduced and correspond to each software subsystem.
Note: This article and other "Linux kernel analysis" articles are based on the following convention:
a) The kernel version is Linux 5.6.18, which can be obtained from the following link:
https:// mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.6.18.tar.xz
b) Since most embedded systems use ARM processors, the architecture part is involved , all with ARM as the object of analysis
As shown in the figure below, the Linux kernel is only a part of the Linux operating system. On the lower side, it manages all hardware devices of the system; on the other side, it provides interfaces to Library Routine (such as C library) or other applications through system calls.
Therefore, its core function is: managing hardware devices for use by applications. The standard components of modern computers (whether PCs or embedded systems) are CPU, Memory (memory and external memory), input and output devices, network devices and other peripheral devices. So in order to manage these devices, the Linux kernel proposes the following architecture.
(code to get backend private message for free [code])
3.1 Overall architecture and subsystem division
The above figure illustrates the overall architecture of the Linux kernel. According to the core functions of the kernel, the Linux kernel proposes 5 subsystems, which are responsible for the following functions:
1. Process Scheduler, also known as process management and process scheduling. Responsible for managing CPU resources so that each process can access the CPU in as fair a manner as possible.
2. Memory Manager, memory management. Responsible for managing Memory resources so that various processes can safely share the machine's memory resources. In addition, memory management will provide a virtual memory mechanism, which allows the process to use more memory than the system's available memory. The unused memory will be saved in external non-volatile memory through the file system, and will be retrieved when needed. in memory.
3. VFS (Virtual File System), virtual file system. The Linux kernel abstracts external devices with different functions, such as Disk devices (hard disk, magnetic disk, NAND Flash, Nor Flash, etc.), input and output devices, display devices, etc., into a unified file operation interface (open, close, read, write, etc.) to access. This is the embodiment of "everything is a file" in the Linux system (in fact, Linux is not thorough, because the CPU, memory, network, etc. are not files yet. If you really need everything to be a file, you have to look at what Bell Labs is developing "Plan 9").
4. Network, network subsystem. Responsible for managing the system's network devices and implementing a variety of network standards.
5. IPC (Inter-Process Communication), inter-process communication. IPC does not manage any hardware, it is mainly responsible for communication between processes in the Linux system.
3.2 Process Scheduler
Process scheduling is the most important subsystem in the Linux kernel. It mainly provides access control to the CPU. Because CPU resources are limited in computers, and many applications use CPU resources, a "process scheduling subsystem" is needed to schedule and manage the CPU.
The process scheduling subsystem includes 4 sub-modules (see the figure below), their functions are as follows:
1. Scheduling Policy, the strategy for implementing process scheduling , which determines which process (or processes) will own the CPU.
2. Architecture-specific Schedulers, the architecture-related parts, are used to abstract the control of different CPUs into a unified interface. These controls are mainly used during the suspend and resume processes, involving CPU register access, assembly instruction operations, etc.
3. Architecture-independent Scheduler, the architecture-independent part. It will communicate with the "Scheduling Policy module" to decide which process to execute next, and then resume the specified process through the "Architecture-specific Schedulers module".
4. System Call Interface, system call interface. The process scheduling subsystem opens the interfaces that need to be provided to user space through the system call interface, while shielding details that do not require user space programs to care.
3.3 Memory Manager (MM)
Memory management is also the most important subsystem in the Linux kernel. It mainly provides access control to memory resources. The Linux system will establish a mapping relationship between the hardware physical memory and the memory used by the process (called virtual memory). This mapping is per process, so different processes can use the same virtual memory, and these are the same Virtual memory can be mapped to different physical memories.
The memory management subsystem includes 3 sub-modules (see the figure below), their functions are as follows:
1. Architecture Specific Managers, architecture-related parts . Provides a virtual interface for accessing hardware Memory.
2. Architecture Independent Manager, architecture-independent part. Provides all memory management mechanisms, including: process-based memory mapping; virtual memory swapping.
3. System Call Interface, system call interface. Through this interface, functions such as memory allocation and release, and file mapping are provided to user space program applications.
3.4 Virtual Filesystem (VFS)
A file system in the traditional sense is a method of storing and organizing computer data. It abstracts the cold data blocks on computer disks, hard drives and other devices in an easy-to-understand, user-friendly way (file and directory structure), thereby making it easy to find and access them. Therefore, the essence of the file system is "a method of storing and organizing data", and the manifestation of the file system is "reading data from a certain device and writing data to a certain device".
With the advancement of computer technology, the methods of storing and organizing data are also constantly improving, resulting in various types of file systems, such as FAT, FAT32, NTFS, EXT2, EXT3, etc. For compatibility, the operating system or kernel must support multiple types of file systems in the same form of expression, which extends the concept of virtual file system (VFS).
The function of VFS is to manage various file systems, shield their differences, and provide user programs with an interface to access files in a unified manner.
We can read or write data from disks, hard disks, NAND Flash and other devices, so the original file systems were built on these devices. This concept can also be extended to other hardware devices, such as memory, display (LCD), keyboard, serial port, etc.
Our access control to hardware devices can also be summarized as reading or writing data, so it can be accessed using a unified file operation interface. This is what the Linux kernel does. In addition to the traditional disk file system, it also abstracts the device file system, memory file system, etc. These logics are all implemented by the VFS subsystem.
The VFS subsystem includes 6 sub-modules (see the figure below), their functions are as follows:
1. Device Drivers, device drivers, used for control All external devices and controllers. Since there are a large number of hardware devices (especially embedded products) that are incompatible with each other, there are also a lot of device drivers. Therefore, nearly half of the Source Code in the Linux kernel are device drivers. Most of the underlying Linux engineers (especially domestic companies) are writing or maintaining device drivers and have no time to estimate other contents (which are precisely the essence of the Linux kernel). location).
2. Device Independent Interface, this module defines a unified way to describe hardware devices (unified device model). All device drivers comply with this definition, which can reduce the difficulty of development. At the same time, interfaces can be provided upwards in a consistent manner.
3. Logical Systems, each file system will correspond to a Logical System (logical file system), which will implement specific file system logic.
4. System Independent Interface, this module is responsible for representing hardware devices and logical file systems with a unified interface (fast device and character device), so that the upper-layer software no longer cares about the specific hardware form.
5. System Call Interface, the system call interface, provides user space with a unified interface for accessing file systems and hardware devices.
3.5 Network Subsystem (Net)
The network subsystem is mainly responsible for managing various network devices in the Linux kernel, and implementing various network protocol stacks, and ultimately connecting to other systems through the network. Function. In the Linux kernel, the network subsystem is almost self-contained. It includes 5 sub-modules (see the figure below). Their functions are as follows:
1. Network Device Drivers , the network device driver is the same as the device driver in the VFS subsystem.
2. Device Independent Interface, which is the same as that in the VFS subsystem.
3. Network Protocols, implementing various network transmission protocols, such as IP, TCP, UDP, etc.
4. Protocol Independent Interface, shields different hardware devices and network protocols, and provides interfaces (sockets) in the same format.
5. System Call interface, the system call interface provides user space with a unified interface for accessing network devices.
As for the IPC subsystem, since its function is relatively simple, it will not be described here.
Linux kernel source code includes three main parts:
1. Kernel core code, including what is described in Chapter 3 Various subsystems and submodules, as well as other supporting subsystems, such as power management, Linux initialization, etc.
2. Other non-core code, such as library files (because the Linux kernel is a self-contained kernel, that is, the kernel does not Rely on any other software, you can compile it yourself), firmware collection, KVM (virtual machine technology), etc.
3. Compile scripts, configuration files, help documents, copyright statements and other auxiliary files
Figure r below shows the top-level directory structure of the kernel source code seen using the ls command. The specific description is as follows.
include/ ---- 内核头文件,需要提供给外部模块(例如用户空间代码)使用。 kernel/ ---- Linux内核的核心代码,包含了3.2小节所描述的进程调度子系统,以及和进程调度相关的模块。 mm/ ---- 内存管理子系统(3.3小节)。 fs/ ---- VFS子系统(3.4小节)。 net/ ---- 不包括网络设备驱动的网络子系统(3.5小节)。 ipc/ ---- IPC(进程间通信)子系统。 arch// ---- 体系结构相关的代码,例如arm, x86等等。 arch//mach- ---- 具体的machine/board相关的代码。 arch//include/asm ---- 体系结构相关的头文件。 arch//boot/dts ---- 设备树(Device Tree)文件。 init/ ---- Linux系统启动初始化相关的代码。 block/ ---- 提供块设备的层次。 sound/ ---- 音频相关的驱动及子系统,可以看作“音频子系统”。 drivers/ ---- 设备驱动(在Linux kernel 3.10中,设备驱动占了49.4的代码量)。 lib/ ---- 实现需要在内核中使用的库函数,例如CRC、FIFO、list、MD5等。 crypto/ ----- 加密、解密相关的库函数。 security/ ---- 提供安全特性(SELinux)。 virt/ ---- 提供虚拟机技术(KVM等)的支持。 usr/ ---- 用于生成initramfs的代码。 firmware/ ---- 保存用于驱动第三方设备的固件。 samples/ ---- 一些示例代码。 tools/ ---- 一些常用工具,如性能剖析、自测试等。 Kconfig, Kbuild, Makefile, scripts/ ---- 用于内核编译的配置文件、脚本等。 COPYING ---- 版权声明。 MAINTAINERS ----维护者名单。 CREDITS ---- Linux主要的贡献者名单。 REPORTING-BUGS ---- Bug上报的指南。 Documentation, README ---- 帮助、说明文档。
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of Linux kernel source code of five major modules and overall kernel architecture design (detailed graphic and text explanation). For more information, please follow other related articles on the PHP Chinese website!