Introduction to the overall architecture of linux

1 Introduction

This article is the "Linux kernel analysis" series, the first article, will be a core function of the kernel as a starting point, describing the overall architecture of the Linux kernel, as well as under the framework of major software subsystems. After that, it will introduce the directory structure of the Linux kernel source files, and software subsystems and various correspondence.

Note: This article and other "Linux kernel analysis" article are based on the following agreement:  
A) kernel version is Linux 3.10.29 (This version is a version of the long term, it will be ongoing maintenance of the Linux community for at least 2 years), from below the link for: https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.29.tar.xz 
b) in view of the embedded systems mostly use ARM processors, so it comes to architecture content section, are to be analyzed for the ARM

2. Linux kernel is the core function

As shown below, Linux kernel only part of the Linux operating system. Next, it manages all system hardware; pair, it calls through the system, provides an interface to Library Routine (e.g. C library), or other applications.

Therefore, its core function is to: manage hardware devices, for use by the program. The modern computer (either PC or embedded system) of standard components, that is, CPU, Memory (RAM and external memory), input and output devices, network devices and other peripherals. So in order to manage these devices, Linux kernel proposed the following framework.

3. Linux kernel overall architecture

3.1 overall architecture and subsystems division

The figure illustrates the overall architecture of the Linux kernel. According to the core functions of the kernel, Linux kernel proposed five sub-systems, 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 to try to be fair manner.

2. Memory Manager, memory management. Responsible for the management Memory (RAM) resources, so that each process can be safely shared memory resources of the machine. In addition, memory management will provide virtual memory mechanism, which allows the process to use more than the available system memory Memory, the memory will not save time in an external non-volatile memory to use by the file system, and then take back memory.

3. VFS (Virtual File System), the virtual file system. Linux kernel external devices with different functions, such as Disk device (hard disk, NAND Flash, Nor Flash, etc.), input and output devices, display devices, etc., that can be abstracted through a unified user interface file (open, close, read, write, etc.) to access. This is the Linux system, "Everything is a file" of the expression (Linux actually do not complete, because the CPU, memory, network, etc. is not the file, if all that is really required documents, will have to see being developed by Bell Labs " Plan 9 " is).

4. Network, network subsystem. Responsible for network device management system, and to achieve a variety of network standards.

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

3.2 Process Scheduler (Process Scheduler)

Linux kernel process scheduler is the most important subsystem that provides access control of the main CPU. Because in the computer, CPU resources are limited, and many applications have to use CPU resources, it is necessary to "process scheduling subsystem" to CPU scheduling management.

4 comprises a process of scheduling sub-module subsystem (see below), which functions as follows:

1. Scheduling Policy, realize process scheduling strategy, which determines which (or which of) the process will have the CPU.

2. Architecture-specific Schedulers, the relevant portion of the architecture for controlling the CPU will be different, abstract uniform interface. The main control process when the suspend and resume use, involving the CPU register access, assembly instruction operation.

3. Architecture-independent Scheduler,体系结构无关的部分。它会和“Scheduling Policy模块”沟通,决定接下来要执行哪个进程,然后通过“Architecture-specific Schedulers模块”resume指定的进程。

4. System Call Interface,系统调用接口。进程调度子系统通过系统调用接口,将需要提供给用户空间的接口开放出去,同时屏蔽掉不需要用户空间程序关心的细节。

3.3 内存管理(Memory Manager, MM)

内存管理同样是Linux内核中最重要的子系统,它主要提供对内存资源的访问控制。Linux系统会在硬件物理内存和进程所使用的内存(称作虚拟内存)之间建立一种映射关系,这种映射是以进程为单位,因而不同的进程可以使用相同的虚拟内存,而这些相同的虚拟内存,可以映射到不同的物理内存上。

内存管理子系统包括3个子模块(见下图),它们的功能如下:

1. Architecture Specific Managers,体系结构相关部分。提供用于访问硬件Memory的虚拟接口。

2. Architecture Independent Manager,体系结构无关部分。提供所有的内存管理机制,包括:以进程为单位的memory mapping;虚拟内存的Swapping。

3. System Call Interface,系统调用接口。通过该接口,向用户空间程序应用程序提供内存的分配、释放,文件的map等功能。

3.4 虚拟文件系统(Virtual Filesystem, VFS)

传统意义上的文件系统,是一种存储和组织计算机数据的方法。它用易懂、人性化的方法(文件和目录结构),抽象计算机磁盘、硬盘等设备上冰冷的数据块,从而使对它们的查找和访问变得容易。因而文件系统的实质,就是“存储和组织数据的方法”,文件系统的表现形式,就是“从某个设备中读取数据和向某个设备写入数据”。

随着计算机技术的进步,存储和组织数据的方法也是在不断进步的,从而导致有多种类型的文件系统,例如FAT、FAT32、NTFS、EXT2、EXT3等等。而为了兼容,操作系统或者内核,要以相同的表现形式,同时支持多种类型的文件系统,这就延伸出了虚拟文件系统(VFS)的概念。VFS的功能就是管理各种各样的文件系统,屏蔽它们的差异,以统一的方式,为用户程序提供访问文件的接口。

我们可以从磁盘、硬盘、NAND Flash等设备中读取或写入数据,因而最初的文件系统都是构建在这些设备之上的。这个概念也可以推广到其它的硬件设备,例如内存、显示器(LCD)、键盘、串口等等。我们对硬件设备的访问控制,也可以归纳为读取或者写入数据,因而可以用统一的文件操作接口访问。Linux内核就是这样做的,除了传统的磁盘文件系统之外,它还抽象出了设备文件系统、内存文件系统等等。这些逻辑,都是由VFS子系统实现。

VFS子系统包括6个子模块(见下图),它们的功能如下:

1. Device Drivers,设备驱动,用于控制所有的外部设备及控制器。由于存在大量不能相互兼容的硬件设备(特别是嵌入式产品),所以也有非常多的设备驱动。因此,Linux内核中将近一半的Source Code都是设备驱动,大多数的Linux底层工程师(特别是国内的企业)都是在编写或者维护设备驱动,而无暇估计其它内容(它们恰恰是Linux内核的精髓所在)。

2. Device Independent Interface, 该模块定义了描述硬件设备的统一方式(统一设备模型),所有的设备驱动都遵守这个定义,可以降低开发的难度。同时可以用一致的形势向上提供接口。

3. Logical Systems,每一种文件系统,都会对应一个Logical System(逻辑文件系统),它会实现具体的文件系统逻辑。

4. System Independent Interface,该模块负责以统一的接口(快设备和字符设备)表示硬件设备和逻辑文件系统,这样上层软件就不再关心具体的硬件形态了。

5. System Call Interface,系统调用接口,向用户空间提供访问文件系统和硬件设备的统一的接口。

3.5 网络子系统(Net)

网络子系统在Linux内核中主要负责管理各种网络设备,并实现各种网络协议栈,最终实现通过网络连接其它系统的功能。在Linux内核中,网络子系统几乎是自成体系,它包括5个子模块(见下图),它们的功能如下:

1. Network Device Drivers,网络设备的驱动,和VFS子系统中的设备驱动是一样的。

2. Device Independent Interface,和VFS子系统中的是一样的。

3. Network Protocols,实现各种网络传输协议,例如IP, TCP, UDP等等。

4. Protocol Independent Interface,屏蔽不同的硬件设备和网络协议,以相同的格式提供接口(socket)。

5. System Call interface,系统调用接口,向用户空间提供访问网络设备的统一的接口。

 

至于IPC子系统,由于功能比较单纯,这里就不再描述了。

4. Linux内核源代码的目录结构

Linux内核源代码包括三个主要部分:

1. 内核核心代码,包括第3章所描述的各个子系统和子模块,以及其它的支撑子系统,例如电源管理、Linux初始化等

2. 其它非核心代码,例如库文件(因为Linux内核是一个自包含的内核,即内核不依赖其它的任何软件,自己就可以编译通过)、固件集合、KVM(虚拟机技术)等

3. 编译脚本、配置文件、帮助文档、版权说明等辅助性文件

下图示使用ls命令看到的内核源代码的顶层目录结构,具体描述如下。

 

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 ---- 帮助、说明文档。

Guess you like

Origin www.cnblogs.com/still-smile/p/12410420.html