The composition of the Linux kernel
First, the Linux kernel source code directory structure
- Arch: Contains code related to hardware architecture, each of which occupies a corresponding directory, such as i386, ARM, PowerPC, MIPS, etc.
- Block: Blocks device driver I/O scheduling.
- Crypto: Commonly used encryption and hashing algorithms (such as AES, SHA, etc.), as well as some compression and CRC check algorithms.
- Documentation: Common explanations and annotations for each part of the kernel.
- Drivers: Device drivers, each of which consumes a subdirectory, such as char, block, net, MTD, I²c, and so on.
- FS: Various file systems supported, such as EXT, F at, NTFS, JFFS2, and so on.
- Include: header files, System-related header files are placed under the Include/linux subdirectory.
- INIT: Kernel initialization code.
- IPC: Code for interprocess communication. Kernel: The core part of the kernel, including process scheduling, timers, and so on, and a portion of the Peace station code is placed in the Arch/*/kernel directory.
- LIB: library file code.
- MM: Memory management code, and part of the platform-related code is placed in the ARCH/*/MM directory.
- NET: Network-related code, the realization of a variety of common network protocols.
- Scripts: Contains the script file used to configure the kernel.
- Security: mainly includes SELinux module.
- Drive core code and common device drivers for Sound:alsa, OSS Audio Devices.
- USR: cpio for packaging and compression are implemented.
Components of the 1.1Linux kernel
The Linux kernel consists of 5 subsystems, such as process scheduling (SCHED), memory Management (MMU), virtual file system (VFS), network Interface (NET), and interprocess communication (IPC).
1.1.1 Process Scheduling
Multiple processes in the Precision scheduling control system access the CPU so that multiple processes can be micro-serial in the CPU, macro-parallel execution. Process scheduling is at the center of the system, and other subsystems in the kernel rely on it because each subsystem needs to suspend or resume the process.
The Linux process switches between several states. In device-driven programming, when the requested resource is not met, the driver typically dispatches other processes to execute and causes the driver to go to sleep until the resource it requests is freed before it is awakened to a ready state. Sleep is divided into interrupted sleep and non-interrupted sleep, and the difference is that interrupted sleep wakes up when you receive a signal.
In a device driver, if you need several concurrently executed tasks, you can start the kernel thread, and the function that starts the kernel thread is:
int kernel_thread (int (*FN) (void *), void * arg, unsigned long flags);
1.1.2 Memory Management
The primary role of memory management is to control multiple processes to safely share the main memory area. When the CPU provides a memory management unit (MMU), Linux memory management completes the conversion of virtual memory to physical memory for each process.
In general, each process of Linux has 4GB of memory space, 0~3GB belongs to user space, 3~4GB belongs to kernel space, kernel space has different processing methods for conventional memory, I/O device memory and high-end memory.
1.1.3 Virtual File system
The Linux virtual file system (VFS) hides a variety of hardware specifics, providing a unified interface for all devices. Moreover, it is independent of the individual file system, is an abstraction of the various file systems, it uses super block super blocks to store file system information, using index node inode to store the physical information of the file, using the directory entry dentry to store the logical information of the file.
1.1.4 Network Interface
Network interfaces provide standard access to various networks and support for a variety of network hardware. Network interface can be divided into network protocols and network drivers, the network protocol is responsible for the implementation of each possible network transport protocol, the network device driver is responsible for communication with the hardware devices, each of the possible hardware devices have a corresponding device driver.
1.1.5 Process Communication
Linux supports a variety of communication mechanisms between processes, including semaphores, shared memory, pipelines, and other mechanisms that can assist in multiple processes, mutually exclusive access to multiple resources, synchronization between processes, and message delivery.
The dependencies between the 5 components of the Linux kernel are as follows:
- Relationship between process scheduling and memory management: These two subsystems depend on each other. In a multi-channel program environment, the process must be created for the program, and the first thing to create a process is to load the program and data into memory.
- Relationship between interprocess communication and memory management: The interprocess communication subsystem relies on memory management to support the shared memory communication mechanism, which allows two processes to access a common area of memory in addition to their own private space.
- The relationship between a virtual file system and a network interface: the virtual file system uses a network interface to support network file systems (NFS) and also uses memory management to support RAMDISK devices.
- The relationship between memory management and virtual file systems: Memory management leverages the virtual file system to support switching, and the switching process (SWAPD) is periodically dispatched by the scheduler, which is the only reason for memory management to depend on process scheduling. When a process accesses a memory map that is swapped out, memory management makes a request to the file system while suspending the currently running process.
In addition to these dependencies, all subsystems in the kernel depend on some common resources. These resources include routines used by all subsystems, such as functions to allocate and free memory space, functions to print warnings or error messages, and system-provided debug routines.
Second, Linux kernel space and user space
Different operating modes (levels) are often implemented within the CPU
For example, ARM processors have the following 7 modes of operation:
- User mode (USR): Most applications run in user mode, and some protected system resources cannot be accessed when the processor is running in user mode.
- Fast Interrupt Mode (FIQ): For high-speed data transmission or justification.
- External interrupt mode (IRQ): Used for general interrupt reason.
- Management Mode (SVC): The protected mode used by the operating system.
- Data access termination mode (ABT): Enters this mode when data or instruction prefetching is terminated, and can be used for virtual storage and storage protection.
- System mode (SYS): Runs a privileged operating system task.
- No instruction Abort mode defined (und): Enter this mode when undefined instruction is executed, which can be used to support software emulation of hardware coprocessor.
For example: The X86 processor contains 4 different privileged levels, called Ring0~ring3. Under RING0, you can execute privileged-level directives, have access to any I/O devices, and Ring3 have many operational limitations.
The Linux system takes full advantage of this hardware feature of the CPU, but it uses only level two. In a Linux system, the kernel can do anything, while the application is prohibited from direct access to the hardware and unauthorized access to memory. For example, if you use the X86 processor, the user code runs at privileged level 3, and the system kernel code runs at privileged level 0.
The two nouns of kernel space and user space are used to differentiate between these two different states of program execution, which use different address spaces. Linux systems can only complete control transfers from user space to kernel space through system calls and hardware interrupts
The composition of the Linux kernel, Wang Ming learning Learn