Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the Kernel Source Code and the overall Kernel Architecture Design of the five modules of Linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you the Linux five modules kernel source code and the overall kernel architecture design example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. Preface

This article is a major in the series of "Linux kernel source code analysis". It will describe the overall architecture of the Linux kernel and the main software subsystems under the architecture based on the core functions of the kernel. After that, the directory structure of the Linux kernel source files is introduced, which corresponds to each software subsystem.

Note: this article and other Linux Kernel Analysis articles are based on the following conventions:

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) in view of the fact that most embedded systems use ARM processors, the content related to the architecture takes ARM as the analysis object.

Second, the core functions of the Linux kernel

As shown in the following figure, the Linux kernel is only a unified part of the Linux operating system. On the other hand, it manages all the hardware devices of the system; on the other hand, it provides interfaces to Library Routine (such as C libraries) or other applications through system calls.

Therefore, its core function is to manage hardware devices for use by applications. The standard components of modern computers (whether PC 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 gets backend private message for free [code])

III. The overall architecture of the Linux kernel

3.1 overall architecture and subsystems

The figure above illustrates the overall architecture of the Linux kernel. According to the core functions of the kernel, the Linux kernel proposes five 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 processes can access CPU as fairly as possible.

2. Memory Manager, memory management. Responsible for managing Memory (memory) resources so that processes can safely share the machine's memory resources. In addition, memory management provides a mechanism for virtual memory, which allows processes to use more memory than the Memory available to the system, and the unused memory is stored in external non-volatile memory through the file system and fetched back to memory when needed.

3. VFS (Virtual File System), virtual file system. The Linux kernel abstracts external devices with different functions, such as Disk devices (hard disk, disk, NAND Flash, Nor Flash, etc.), input and output devices, display devices, and so on, to be accessible through a unified file manipulation interface (open, close, read, write, etc.). This is the embodiment of Linux system "everything is a file" (in fact, what Linux does is not complete, because CPU, memory, network, etc., are not files yet. If you really need everything to be a file, you have to see the "Plan 9" that Bell Labs is developing).

4. Network, network subsystem. Responsible for managing the network equipment of the system and implementing a variety of network standards.

5. IPC (Inter-Process Communication), interprocess communication. IPC does not manage any hardware, it is mainly responsible for the communication between processes in the Linux system.

3.2 process scheduling (Process Scheduler)

Process scheduling is the most important subsystem in the Linux kernel, which mainly provides access control to CPU. Because CPU resources are limited in computers, and many applications use CPU resources, "process scheduling subsystem" is needed to schedule and manage CPU.

The process scheduling subsystem consists of four sub-modules (see figure below), which have the following functions:

1. Scheduling Policy, the policy that implements process scheduling, which determines which process (or processes) will have CPU.

2. Architecture-specific Schedulers, an architecture-related part that abstracts the control of different CPU into a unified interface. These controls are mainly used in suspend and resume processes, involving CPU register access, assembly instruction operations, and so on.

3. Architecture-independent Scheduler, the architecture-independent part. It communicates with the "Scheduling Policy module" to decide which process to execute next, and then through the process specified by the "Architecture-specific Schedulers module" resume.

4. System Call Interface, the system call API. Through the system call interface, the process scheduling subsystem opens out the interface that needs to be provided to the user space, and shields out the details that do not need to be concerned about by the user space program.

3.3memory management (Memory Manager, MM)

Memory management is also the most important subsystem in the Linux kernel, which mainly provides access control to memory resources. The Linux system establishes a mapping between the hardware physical memory and the memory used by the process (called virtual memory). This mapping is on a process-by-process basis, so that different processes can use the same virtual memory, while the same virtual memory can be mapped to different physical memory.

The memory management subsystem consists of three sub-modules (see figure below), which have the following functions:

1. Architecture Specific Managers, related to the architecture. Provides a virtual interface for accessing hardware Memory.

2. Architecture Independent Manager, architecture independent part. Provides all memory management mechanisms, including: Swapping of memory mapping; virtual memory in process units.

3. System Call Interface, the system call API. Through this interface, the user space application program is provided with the functions of memory allocation, release, file map and so on.

3.4 Virtual file system (Virtual Filesystem, VFS)

In the traditional sense, the file system is a method of storing and organizing computer data. It uses easy-to-understand and user-friendly methods (file and directory structure) to abstract cold data blocks on computer disks, hard disks and other devices, thus making it easy to find and access them. Therefore, the essence of the file system is "the way to store and organize data". The form of the file system is to "read data from and write data to a device".

With the progress of computer technology, the methods of storing and organizing data are also improving, resulting in many types of file systems, such as FAT, FAT32, NTFS, EXT2, EXT3 and so on. In order to be compatible, the operating system or kernel should support multiple types of file systems in the same form, which extends the concept of virtual file system (VFS).

The function of VFS is to manage a variety of file systems, shield their differences, and provide user programs with an interface to access files in a unified way.

We can read or write data from disks, hard drives, NAND Flash and other devices, so the original file system is built on these devices. This concept can also be extended to other hardware devices, such as memory, display (LCD), keyboard, serial port and so on.

Our access control to hardware devices can also be summarized as reading or writing data, so it can be accessed by a unified file operation interface. This is what the Linux kernel does. In addition to traditional disk file systems, it abstracts device file systems, memory file systems, and so on. These logics are implemented by the VFS subsystem.

The VFS subsystem consists of six sub-modules (see figure below), which have the following functions:

1. Device Drivers, device driver, used to control all external devices and controllers. Due to the existence of a large number of incompatible hardware devices (especially embedded products), there are also a lot of device drivers. Therefore, nearly half of the Source Code in the Linux kernel are device drivers, and most of the underlying Linux engineers (especially domestic enterprises) are writing or maintaining device drivers, and have no time to estimate anything else (they are precisely the essence of the Linux kernel).

2. Device Independent Interface, this module defines a unified way to describe hardware devices (unified device model). All device drivers follow this definition, which can reduce the difficulty of development. At the same time, the interface can be provided upward in a consistent situation.

3. Logical Systems, each file system, will correspond to a Logical System (logical file system), which will implement the 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 devices and character devices), so that the upper 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)

In the Linux kernel, the network subsystem is mainly responsible for managing all kinds of network devices, implementing various network protocol stacks, and finally realizing the function of connecting other systems through the network. In the Linux kernel, the network subsystem is almost self-contained and consists of five sub-modules (see 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 is the same as in the VFS subsystem.

3. Network Protocols, implement various network transport protocols, such as IP, TCP, UDP and so on.

4. Protocol Independent Interface, shielding different hardware devices and network protocols, and providing interfaces (socket) in the same format.

5. System Call interface, the system call interface, provides user space with a unified interface to access network devices.

As for the IPC subsystem, because the function is relatively simple, it is no longer described here.

IV. Directory structure of Linux kernel source code

The Linux kernel source code consists of three main parts:

1. Kernel core code, including the subsystems and submodules described in Chapter 3, as well as other supporting subsystems, such as power management, Linux initialization, etc.

two。 Other non-core code, such as library files (because the Linux kernel is a self-contained kernel, that is, the kernel can compile itself without relying on any other software), firmware collections, KVM (virtual machine technology), etc.

3. Auxiliary files such as compilation scripts, configuration files, help documents, copyright instructions, etc.

Figure r below shows the top-level directory structure of the kernel source code seen using the ls command, as described below.

Include/-kernel header files, which need to be provided to external modules (such as user space code). Kernel/-the core code of the Linux kernel contains the process scheduling subsystem described in section 3.2, as well as modules related to process scheduling. Mm/-memory management subsystem (Section 3.3). Fs/-VFS subsystem (Section 3.4). Net/-excluding network subsystems driven by network devices (Section 3. 5). Ipc/-IPC (interprocess communication) subsystem. Arch//-architecture-related code, such as arm, x86, etc. Arch//mach--specific machine/board related code. Arch//include/asm-architecture-related header files. Arch//boot/dts-device tree (Device Tree) file. Init/-Linux system startup initialization related code. Block/-provides the hierarchy of block devices. Sound/-audio-related drivers and subsystems can be regarded as "audio subsystems". Drivers/-device driver (in Linux kernel 3.10, device driver accounts for 49.4% of the code). Lib/-implement library functions that need to be used in the kernel, such as CRC, FIFO, list, MD5, and so on. Crypto/-Library functions related to encryption and decryption. Security/-provides security features (SELinux). Virt/-provides support for virtual machine technology (KVM, etc.). Usr/-the code used to generate initramfs. Firmware/-saves the firmware used to drive third-party devices. Samples/-some sample code. Tools/-some common tools, such as performance profiling, self-testing, etc. Kconfig, Kbuild, Makefile, scripts/-configuration files, scripts, etc., for kernel compilation. COPYING-copyright notice. MAINTAINERS-list of maintainers. CREDITS-the list of major contributors to Linux. REPORTING-BUGS-Bug reported guidelines. Documentation, README-help, documentation. The above is all the contents of the article "sample analysis of the kernel source code of the five modules of Linux and the overall architecture design of the kernel". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report