Linux Kernel Learning Summary
Liu Hao + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Foreword: The conclusion is divided into three parts, the first part summarizes the knowledge points of the course, the second part is the learning experience, the third part is the appendix, the blog link for all the work, easy to inquire and study.
First, the Linux Summary of kernel analysis
(a) How the computer works
1. Stored program Computer working model
2. X86CPU Register: General Register, segment register, flag register, etc.
3. Assembly Instructions for Computers
(1) MOVL directive:
- Register addressing, register mode, register identifier beginning with%. Do not deal with memory, eax assignment to edx;
- Immediate addressing, placing the immediate number directly in the register, the immediate number is the value beginning with $;
- Direct addressing, direct access to the data of a specified memory address;
- Indirect addressing: Accesses memory as a memory address for the value of the Register;
- Variable addressing: Changes the value of the register at the time of the indirect addressing.
Note: the/T assembler format differs slightly from the Intel assembler format, which is used by the Linux kernel in the/T assembly format.
(2) Other directives
The stack is growing downward, with a base address ebp pointing to the bottom of the stack stack
- PUSHL press Stack, esp minus 4, put EAX in ESP memory location
- POPL out the stack, from the stack top take 32 bits into the register eax inside, there are two actions: the first indirect addressing, put the stack top value into the eax inside, and then add 4 to the top of the stack.
- Call function calls, the current EIP pressure stack, to the EIP to assign a new value;
Note: * Refers to these instructions are pseudo-instructions, the programmer can not directly modify these, that is, the EIP register can not be directly modified, can only be indirectly modified by special instructions.
4. Compiling C code into assembly code
(1) The function call stack is superimposed on a logically multiple stack
(2) The return value of the function is returned by default to the upper-level function using the EAX register store
(3) Compile the assembly code using the command: Gcc–s–o main.s main.c-m32
(ii) How the operating system works
1. Stack--the Stack C language program must run with a record call path and parameter space. Includes: function call frame; pass parameter; save return address (e.g. EAX); provide local variable space
2. Stack register: ESP stack pointer and EBP base address pointer (represents the current function call base address in C language)
3. Stack operation: Push stack top pointer reduces 4 bytes (32 bits) and pop stack top pointer add 4 bytes
4. Parameter passing and local variables
(1) Establishing the framework (equivalent to the call command)
Push%EBP
MOVL%ESP,%EBP
(2) Frame removal (equivalent to RET instruction)
MOVL%ebp,%esp
Pop%EBP
When the function returns, the frame must be removed, and the build and dismantle are one by one corresponding.
(3) Transfer parameters
Before the framework of a child function is established, the value of the local variable is saved in the caller's stack frame, so the value of the variable can be put into the stack before the child function framework is established.
! The return value of the function is passed through the EAX register
(c) to construct a simple Linux system Menuos
1. Computer Three Magic Weapon: Stored program computer, function call stack, interrupt
2. Operating system Two swords: Interrupt context switching (save site and recovery site) and process context switching
3. Summary: Rest_init is a process of No. No. 0 and has been in existence. Process # No. 0 created process Kernel_init 1th and created additional service threads. namely Daosh one (Start_kernel....cpu_idle), Life Two (Kernel_init and Kthreadd), two born three (i.e. front 0, 1 and 23 processes), Sansheng everything (1th process is the ancestor of all user-state processes, Process number 2nd is the ancestor of all kernel threads.
In the absence of a process concept, Linux will always execute from the initialization section of the code to Start_kernel, and then to its last function call Rest_init.
Starting with Rest_init, Linux began to produce a process, because Init_task is statically manufactured, Pid=0, which attempts to incorporate execution from the earliest assembly code until start_kernel into the context of the init_task process. In the Rest_init function, the kernel will generate the first real process (pid=1) with the following code. Then Init_task becomes an idle task,init_idle function The first parameter of current is &init_task, in Init_idle will add init_task to the CPU's running queue, In this way, when there is no other ready process in the run queue, Init_task (that is, the idle task) is called, and its core is a while (1) loop, in which it invokes the schedule function to switch to the new process when a new process joins in the run queue.
(iv) The three-layer skin of the system call is clawed
1. User state and Kernel state
- User state: In the corresponding low execution state, the control of the code is limited, only within the scope of the corresponding level allowed to be active
- Kernel state: At high execution level, code can execute privileged instructions and access any physical address.
Intel x86 CPUs have four different levels of execution 0-3,linux only use the 0 and 3 stages of the interim to represent the kernel state and the user state respectively.
2. Understand the complete process of interrupt processing: interrupt signal (int instruction) Complete: Save the CS:EIP value, the current stack segment stack top and the current flag, while loading the current interrupt signal or the system call associated interrupt service entry into the Cs:eip inside, the current battle segment and ESP loaded into the CPU.
Save all after the completion of the dispatch, then execute Restore_all, if a process scheduling, the current state will be temporarily saved in the system, the next time the process is scheduled to switch to the current process and then the execution is complete.
3. Three levels of system calls
The three levels of system calls are: xyz function (API), system_ call (interrupt vector), and Sys_ XYZ (Interrupt service program).
4. Summary:
In a Linux system, the system call is triggered by activating the 0x80 interrupt, and the calling system call number implementation is assigned to the EAX memory, if there is an incoming parameter assignable to the EBX register, if more than 1 are assigned to EBX, ECX, edx, ESI, EDI, EBP, If there are more than 6, the pointer variable points to the other stack area, and if no parameter is passed in, the assignment is 0.
Although the Intel X86 CPU has 4 execution level 0~3, only 0 and 3 levels are used in the Linux system, respectively, to represent the kernel state and the user state. Some of the underlying, hardware, core operations must be in the kernel state to allow execution, for operating system programs and drivers exclusive, ordinary programs can only be executed in the user state. If the normal program needs to involve the operation of the kernel state, it needs to be implemented by the system call. The advantage of this is that the shielding platform related operation reduces the software development difficulty, enhances the system security, makes the program has the better portability (the Linux system and other UNIX systems follow the unified standard, the system calls basically the same).
(v) Process volume management and process creation
Operating system kernel Three main functions: Process Management (CORE), memory management and file system.
1.Linux creates a new process by copying the parent process, which is implemented by calling Do_fork.
2.Linux dynamically assigns a task_struct structure to each newly created process.
3. In order to organize all the processes in the kernel, Linux provides several organizational ways, in which the hash table and the bidirectional loop list method are for all processes in the system (including kernel threads), while the run queue and wait queue are organized by the process in the same state.
The 4.fork () function is called once, but returns two times.
(vi) Loading of executable programs
1. Executable procedure: Pre-processing the. cpp, which is translated into assembly code. S to the target code. O, and then link it to executables, loaded into memory for execution.
2. The executable file is loaded into memory to begin execution of the first line of code, 0x8048x00 the actual entry.
3. Dynamic linking is divided into executable program loading dynamic link and runtime dynamic link.
4. Do_ Execve calls Do_ execve_ common,do_ execve_ Common mainly relies on exec_ BINPRM, which is an important function: Search_binary_handler (BPRM).
(vii) Process switching and general implementation of the system
1. Process scheduling algorithm--each process is not the same as the CPU, I/O resource requirements.
- First Category: I/O intensive (i/o-bound) and CPU intensive (cpu-bound)
- Second classification: batch process; real-time process; interactive process
2. Timing of process scheduling (schedule () function implementation):
- Interrupt processing (including clock interrupts, I/O interrupts, system calls, and exceptions), call schedule () directly, or call schedule () based on the need_resched tag when returning to the user state;
- Kernel thread (only kernel state without user state) can directly call schedule () for process switching, can also be scheduled during interrupt processing, that is, kernel threads as a class of special processes can be active scheduling, can also be passive scheduling;
- The user-state process cannot implement the active scheduling, but can only be dispatched by a point in time after the kernel state, that is, scheduling during interrupt processing.
Note: The user-state process can only be dispatched passively, and the kernel thread is a special process that has no user state in the kernel state.
3. The operating system (any computer system that contains a basic set of programs) has two purposes:
- Interact with hardware, manage all hardware resources;
- Provides a good execution environment for user programs (applications).
4. This week, we mainly understand the process scheduling and process switching processes in Linux. Process scheduling is a strategy to dynamically assign a processor to a process in the ready queue for execution. The process switch is to retract the processor from the running process and then make the pending process consume the processor. Essentially, the intermediate data stored in the processor's registers is saved, freeing up the processor registers for other processes to use.
Second, learning feelings and experience:
As a junior student, from this semester has not started to learn cloud classroom "Linux kernel Analysis", followed by Meng teacher step by step to understand the Linux kernel, to now half a semester has passed, and the textbook "Linux kernel Design and implementation" of some chapters, with the video to consolidate the study. Time feels so fast. But at the same time also learned a lot of knowledge.
First, the last semester began to contact with Linux, this time through the course of learning, deepened the understanding of operating system theory, know how the Linux system is working, how to code reading, debugging to follow the verification of Linux system operating mechanism. Second, Linux as an extremely successful operating system, its core complex, profound, I personally learn is very difficult. Although the completion of the net class, read the textbook, Meng teacher you also speak very humorous, but I still feel that I have just started to learn, and need to dig deep in a lot of things.
Through half a semester of study, I think it is important not to learn how much kernel code (in fact, but also important), but more important is the learning method, that is, where to start learning the Linux kernel, such as: How to debug the kernel, how to understand the kernel of the assembly code, how to analyze system calls and so on. This is also my study after the biggest harvest. In short, although the net class is over but the study is not over, continue to refuel ~
Appendix--Blog Job list:
The first week of study summary-How the computer works: http://www.cnblogs.com/lhc-java/p/5217595.html
Second week study summary-how the operating system works: http://www.cnblogs.com/lhc-java/p/5246779.html
Third week study summary--construct a simple Linux system menuos:http://www.cnblogs.com/lhc-java/p/5271085.html
The four-week study summary--the three layers of the flap system call (top): http://www.cnblogs.com/lhc-java/p/5282826.html
Week five study summary--the three layers of the flap system call (bottom): http://www.cnblogs.com/lhc-java/p/5326027.html
Summary of the six weeks of learning--process volume management and process creation: http://www.cnblogs.com/lhc-java/p/5340414.html
Seventh Week study summary-executable program loading: http://www.cnblogs.com/lhc-java/p/5361824.html
Eighth Week study summary-process switching and system general execution process: http://www.cnblogs.com/lhc-java/p/5389954.html
Linux Kernel Learning Summary