Introduction to the overall architecture of the kernel (Linux)

1. Note: This article and other "Linux Kernel Analysis" articles are based on the following conventions:  
a) The kernel version is Linux 3.10.29 (this version is a long term version and will be maintained by the Linux community for at least 2 years), which can be Get it from the following link: https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.29.tar.xz 
b) Since most embedded systems use ARM processors, it involves The content of the architecture part is based on ARM as the analysis object

2. Core functions of the Linux kernel

As shown in the figure below, the Linux kernel is only a part of the Linux operating system. Right, it manages all hardware devices of the system; right, it provides interfaces to Library Routine (such as C library) or other applications through system calls.

image

Therefore, its core function is to manage hardware devices for application use. The standard components of a modern computer (whether it is a PC or an embedded system) 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.

3. The overall architecture of the Linux kernel

3.1 Overall Architecture and Subsystem Division

image

The above diagram 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, process scheduling. Responsible for managing CPU resources so that each process can access the CPU in the fairest possible way.

2. Memory Manager, memory management. Responsible for managing Memory resources so that 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 can use. The unused memory will be stored in the external non-volatile memory through the file system, and when it needs to be used, it will be retrieved. in memory.

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

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), inter-process communication. IPC does not manage any hardware, it is mainly responsible for the communication between processes in the Linux system.

3.2 Process Scheduler

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

The process scheduling subsystem includes 4 sub-modules (see the figure below), and their functions are as follows:

scheduler

1. Scheduling Policy, a strategy for implementing process scheduling, which determines which (or which) processes will have the CPU.

2. Architecture-specific Schedulers, architecture-related parts, are used to abstract the control of different CPUs 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 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, the system call interface. The process scheduling subsystem opens up the interface that needs to be provided to the user space through the system call interface, and at the same time shields the details that do not need to be concerned by the user space program.

3.3 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 will establish a mapping relationship between the hardware physical memory and the memory used by the process (called virtual memory). This mapping is in units of processes, so different processes can use the same virtual memory, and these same The virtual memory can be mapped to different physical memory.

The memory management subsystem includes 3 sub-modules (see the figure below), and their functions are as follows:

mman

1. Architecture Specific Managers, architecture-related parts. Provides a virtual interface for accessing hardware memory.

2. Architecture Independent Manager, the architecture independent part. Provides all memory management mechanisms, including: process-based memory mapping; virtual memory Swapping.

3. System Call Interface, the system call interface. Through this interface, functions such as memory allocation, release, and file map are provided to user space programs and applications.

3.4 Virtual Filesystem (VFS)

A file system in the traditional sense is a method of storing and organizing computer data. It abstracts cold data blocks on computer disks, hard disks and other devices in an easy-to-understand, human-friendly way (file and directory structure), making them easy to find and access. Therefore, the essence of the file system is "the 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".

As computer technology advances, so do the methods of storing and organizing data, resulting in various types of file systems, such as FAT, FAT32, NTFS, EXT2, EXT3, and more. 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 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 drives, 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 and so on. Our access control to hardware devices can also be summarized as reading or writing data, so it can be accessed with a unified file operation interface. The Linux kernel does just that, abstracting away device filesystems, in-memory filesystems, and more, in addition to traditional disk filesystems. These logics are implemented by the VFS subsystem.

The VFS subsystem includes 6 sub-modules (see the figure below), and their functions are as follows:

vfs

1. Device Drivers, device drivers, are used to control all external devices and controllers. Since there are a large number of hardware devices (especially embedded products) that are not compatible with each other, there are also a lot of device drivers. Therefore, nearly half of the source codes in the Linux kernel are device drivers, and most of the Linux bottom-level engineers (especially domestic enterprises) are writing or maintaining device drivers, and have no time to estimate other content (they are precisely the essence of the Linux kernel). where).

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, the interface can be provided upward with a consistent situation.

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 the user space with a unified interface for accessing the file system and hardware devices.

3.5 Network Subsystem (Net)

The network subsystem in the Linux kernel is mainly responsible for managing various 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, it includes 5 sub-modules (see the figure below), and their functions are as follows:

net

1. Network Device Drivers, the drivers of network devices, are the same as the device drivers in the VFS subsystem.

2. Device Independent Interface, which is the same as that in the VFS subsystem.

3. Network Protocols, which implements various network transmission protocols, such as IP, TCP, UDP and so on.

4. Protocol Independent Interface, which shields different hardware devices and network protocols and provides interfaces (sockets) in the same format.

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

 

As for the IPC subsystem, since the function is relatively simple, it will not be described here.

4. Directory structure of Linux kernel source code

The Linux kernel source code consists of three main parts:

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

2. Other non-core codes, such as library files (because the Linux kernel is a self-contained kernel, that is, the kernel does not depend on any other software and can be compiled by itself), firmware collections, KVM (virtual machine technology), etc.

3. Compilation scripts, configuration files, help documents, copyright instructions and other auxiliary files

The following figure shows the top-level directory structure of the kernel source code seen by the ls command, which is described below.

kernel_src_tree

include/ ---- Kernel header files, which need to be provided to external modules (such as user-space code) for use.

kernel/ ---- The core code of the Linux kernel, including the process scheduling subsystem described in Section 3.2, and the modules related to process scheduling.

mm/ ---- memory management subsystem (section 3.3).

fs/ ---- VFS subsystem (section 3.4).

net/ ---- Does not include the network subsystem for network device drivers (section 3.5).

ipc/ ---- IPC (Inter-Process Communication) subsystem.

arch// ---- Architecture-related code, such as arm, x86, etc. 
    arch//mach- ---- specific machine/board related code. 
    arch//include/asm ---- Architecture-specific header files. 
    arch//boot/dts ---- Device Tree file.

init/ ---- Linux system startup initialization related code. 
block/ ---- Provides a hierarchy of block devices. 
sound/ ---- Audio related drivers and subsystems, which can be regarded as "audio subsystem". 
drivers/ ---- device drivers (in Linux kernel 3.10, device drivers account for 49.4 of the code).

lib/ ---- Implement library functions that need to be used in the kernel, such as CRC, FIFO, list, MD5, etc. 
crypto/ ----- Encryption and decryption related library functions. 
security/ ---- Provides security features (SELinux). 
virt/ ---- Provides support for virtual machine technology (KVM, etc.). 
usr/ ---- code for generating initramfs. 
firmware/ ---- holds the firmware used to drive third-party devices.

samples/ ---- some sample code. 
tools/ ---- some common tools, such as performance analysis, self-testing, etc.

Kconfig, Kbuild, Makefile, scripts/ ---- Configuration files, scripts, etc. for kernel compilation.

COPYING ---- Copyright notice. 
MAINTAINERS ---- List of maintainers. 
CREDITS - List of major Linux contributors. 
REPORTING-BUGS ---- Guidelines for bug reporting.

Documentation, README ---- Help, documentation.

 

Original article, please indicate the source when forwarding. Wowo Technology, www.wowotech.net .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324358395&siteId=291194637