Linux kernel learning (1)

xiaoxiao2021-03-06  43

My learning is mainly based on "

Linux kernel full annotation "This book begins.

Chapter 1 Overview

This chapter makes me feel deeply the process of Linus to prepare Linux. In 1990, it was a 20-year-old Linus or a second-year student of a university in Finland. I bought a 386-compatible computer to learn computer knowledge, and from The United States ordered the Minix system software, waiting for the system software, and he carefully studied 80386 hardware knowledge, and then connects to the school host by modem dialing, using assembly language and uses 80386 CPU multitasking characteristics to prepare a terminal. Simulation program. At the same time, in order to copy the software of the old computer to the new computer, the driver has been prepared for some hardware devices such as the floppy drive, keyboard. During the process of learning the Minix system, he realized the system's limitations. In April 1991, he started writing its own operating system, the main algorithm of the operating system and the name of many important functions by modifying terminal emulation programs and hardware drivers. Both reference and from MJBACH "UNIX operating system design". Especially when he answered a minix problem, the first sentence is "Please read the source code". This shows that the learning system software does not only need to understand the basic working principle of the system, but also need to combine the source code, learning system implementation, It's not a simple thing, but this learning process can learn a lot of things, 80386 architecture, assembly language, operating system (single, contain considerable knowledge, available for future design development), c Language programming, maybe you can also find more flash points that make you Mao Ton.

Whether you are looking for a Linux kernel, please note that the operating system kernel program is an abstract and access schedule for hardware resources, so you need to review the hardware and programming knowledge about Intel80 * 86 architecture before learning. In particular, it works in protecting the working principle in virtual address mode, or constantly reviewing the relevant references during the learning kernel, and also requires the "UNIX operating system design" to establish an operating system concept to understand the relevant algorithm. To this end, I found BBBARY "Intel Microprocessor Structure, Programming and Interface" from the library, and UNIX did not find it. Find a "UNIX structure analysis (the structure and algorithm of core code)" as a library. reference. Another important premise is that you are already familiar with the Linux operating system, which has certain understanding for its system management, that is, you will use Linux, in fact, I also say that I have linux, based on Linux system The embedded design has been in half a year. It is deeply attracted by its openness. I have always thought about it. I have a good life in the winter holiday! If you are interested, please follow it, a piece of discussion!

Chapter 2 Linux core architecture

2.1 Linux kernel mode and architecture

The structure of the operating system:

* User application: word processing, browser ETC

* Operating system service: X window system, shell command interpretation system ETC

* Operating system kernel: Our research object, it is an abstract and access schedule for hardware resources

* Hardware system

The kernel structure mode of the operating system:

* Overall single-core mode (mode used in version 0.11): The main program layer of calling service, executing the service layer of the system call, support the underlying function called by the system

* Hierarchical micronary mode

5 modules of the Linux kernel:

* Process Scheduling Module: Control Process Use * Core Management Module: Make sure all processes can safely share machine main memory, support virtual memory management mode (version 0.95 implementation), set the switching area in the secondary storage unit Exchange mechanism, etc.

* File system module: Provide drivers and storage of each device, and operate each device as a unified file form

* Inter-process communication module: used to support information exchange between multiple processes

* Network Interface Module: Provide access to network communication standards and support multiple network hardware (version 0.96)

2.2 Linux interrupt mechanism

Two-piece 8259A programmable interrupt controller is used in 80 * 86CPU, each management of 8 interrupts, 2-piece-class joint, division slave film, can manage 15 interrupt sources (due to INT from the film to the main film On IRQ2). The CPU initializes the 8259A chip with the IN or OUT instructions. The post chip enters the operating state, responding to the response interrupt proposed by the external device at any time. The interrupt response must be promptly selected, after selecting an interrupt service object, after the CPU's INT notification CPU external interrupt request, after returning the response, the chip feeds the current service object from the data line D7 to D0, the CPU is interrupted according to the interrupt The corresponding interrupt service is performed.

There are two categories: hardware interrupts and software interrupts (digital identifiers between 0 to 255), where interrupt INT0 ~ INT31 is Intel fixed setting or reserved, the software interrupt, and they perform instructions in the CPU Trigger when the abnormal condition is detected, so Intel is called an abnormality, which can be divided into two types of faults and traps. INT32 ~ INT255 User Auto Setup, the Linux system will request IRQ0 ~ IRQ15 as the 8259A hardware interrupt request IRQ0 ~ IRQ15, and the system call issued by program programming is interrupted as INT128. The kernel source HEAD.S program is default the 256 descriptors of the interrupt descriptor table (IDT).

2.3 Linux system timing

In the 0.11 version of Linux, the PC issues a clock interrupt (IRQ0) through the programmable timing chip Intel8253, which is a system tick. Each tick is called a clock interrupt handler (Timer_Interrupt), which first adds Jiffies (the total tap of the system) plus 1. Then call the current privilege level CPL in the interrupt program as the parameter to call the DO_TIMER () function. It performs a cumulative run time of the current process based on the CPL, thereby implementing a multi-process timing mechanism, using the time slice concept, makes multiple processes simultaneously. At the same time, according to the privilege level, the current process works in the user state (CPL> 0) or the kernel state, if working in the user state, the time sheet is running with the DO_TIMER () call scheduling program schedule () calls other processes, if work When it is interrupted, do_timer () exits immediately. That is to say, the kernel state can not be preemptive, and the user may.

2.4Linux kernel process control

The process control of the kernel has been mentioned above has made all the processes seem to be run at the same time, and the time tablets running each process are 15 system ticks, that is, 150ms. The system starts the first process "manual" creation, and other processes are the system calling the fork function and the process is a parent process, and the data structure is copied. Each process consists of executable instruction code, data, and stack zone, each process uses the process identification number (PROCESS ID, PID) ID. 2.4.1 Task Data Structure

The kernel program of the Linux system is managed by a process table (defined in the task_struct task structure pointer in Sched.h). The table contains all information about the control and management process, such as the current state of the process, the process number, the parent process number, the accumulated runtime, the file being used, and so on. The process first needs to save the context of the current process before switching, saved in the process table in the process table in the process table in order to perform the process again.

2.4.2 Process Operation Status

Due to the presence of the minute mechanism, the process may have the following states in the survival period: run (executed by the CPU), interrupt sleep (uninterrupted in the system or the resource required for the process), not interrupt sleep (need The Wake_UP () function is clearly awakened), pause (received SigStop, SigtStp, SigTTI, etc., received Sigcout signal recovery execution), zombie (the process is over, but not inquired by the parent process). In order to prevent the internal core data incorrect caused by the process switch, the execution of the critical area code is prohibited from interruption.

2.4.3 Process initialization

The boot program in the boot / directory loads the kernel to memory, then performs initializer init / main, determine how to use the system's physical memory and call the initialization program (memory management, interrupt processing, block device, and character equipment, process) Hardware devices such as management and hardware drives are initialized. At this time, the initialization of the task 0 has been initialized (setting the field of the data structure in SCHED_INIT ()), and its data structure is added to the task status section (TSS) descriptor and local description of the task 0 in the global descriptor. The segment descriptor of the table (LDT) is then moved from the macro MOVE_TO_USER_MODE (include / asm / system.h) to the process 0.

2.4.4 Creating a new process

All new processes are created by replication process 0, all of which are sub-processs of process 0.

The creation process of the sub-process: Find out an unused empty item in the task array (if there is no null, the error is returned, set the value of 64 maximum processes in 0.11) -> Apply one page for the new process Store task data structural information, and copy the current process task data structure as a new process template -> Settings new process to uninterrupted sleep status -> Set the current process for the parent process of the new process -> Clear signal bitmap and reset New Process Value -> Set the initial run time slice value of 15 system ticks -> Set the value of the registers in the task status segment (TSS) in the current process -> Set the code and data segment base address of the new task , Limited to the page table table table table table table list of the current process memory paging management - Set the new task in the GDT to set the new task to run state and return to the new process number

2.4.5 process schedule

The scheduling policy based on priority queuing is used in Linux0.11.

1. Scheduling program schedule ()

Scanning Tasks Array (Comparing Running Time Dipping Tasks in Running Tasks) -> Select the larger use task switch macro function to switch to this process -> All process time filters are used, recalculate counter (Counter = Counter / 2 Priority) -> Repeat the above selection process, call Switch_to () to perform the actual process switch -> No process can be used when selecting Process 0 Run 2. Process Switch Switch_to ()

Check if the process that must be switched is the current process. If you exit it directly -> Current to the task -> Jump to the address of the new task TSS -> CPU Save all registers to the current task register TR The current task data structure pointed to the TSS in the TSS structure -> New Task data pointer to the new task data structure in the new task data structure is restored to the CPU -> new task begins to execute

2.4.6 Termination Process

User Program When eXit (), the system executes the kernel function DO_EXIT () -> Release Process code segment and data segment occupied memory page -> Close Process Open All files -> If the process is a child process, let init As the parent process of the sub-process, if the process is a session head and a control terminal, the terminal is released, and transmits SIGHUP -> to set the process in the process in all processes belonging to the terminal -> Send Sigchld to the parent process Signal -> The parent process integrates sub-process runtime to your own procedure -> Release the memory page occupied by the child process data structure and takes a finale term that is occupied in the task array.

转载请注明原文地址:https://www.9cbs.com/read-52327.html

New Post(0)