Final Summary of the Linux kernel analysis
20135109 Gao Yi Tong
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
First, blog address summary
The first week of study notes how the computer works
Second week study notes in-depth understanding of computers
The third week of learning notes constructs a simple Linux kernel menuos
The four-week learning note three layers of skin (top) of the system call
Week Five Learning notes 30% skins (bottom) of the system call
The description of the learning Notes process and the creation of the process in six weeks
Seventh Week Learning Notes executable loading of programs
The eighth week study notes process switching and the general execution process of the system
Ii. Summary of Knowledge points
(a) How the computer works
- Von Neumann architecture-core: Stored program computers
- x86 compilation Basics: CPU Registers (Universal registers, segment registers, flag registers), common assembly instructions, stacks
(ii) in-depth understanding of computers
- Stored program computers: Logical framework for all computer fundamentals
- Stack: A starting point for a high-level language, a function call requires a stack mechanism
- Interrupt mechanism: The basis of multi-channel system is the key to improve the efficiency of computer
- In the My_schedule function, complete the switchover of the process. There are two types of processes: 1. The next process has not been passed; 2. The next process is passed through and the state of the next process can be known
(iii) constructing a simple Linux kernel menuos
- Introduction to Linux kernel source code:
The code in the Arch/x86 directory is our most important concern.
fs/File System
init/Kernel boot-related code
The Start_kernel function is equivalent to the C language's main function
Core code of the Kernel/linux kernel
mm/Memory Management Code
- Simple analysis of Start_kernel:
Init_task the pcb,0 process, which is created manually, is the final idle process
Trap_init initialization interrupt, set interrupt gate, System trap Gate
Init_process the first user-state process of the Linux system, the INIT program under the root directory (as process number 1th) was created by Kernel_init
Rest_init No. No. 0 process, a process that has been in existence, creating process number 1th
The QEMU command is to impersonate the kernel to boot the virtual machine, starting the Linux kernel requires three parameters (kernel, initrd, root partition and directory), and the first file executed is init.
- Startup process: Boot kernel--Start init-> boot process
(iv) The three-layer skin (top) of the system call is clawed
- System call three layer skins: API xyz, Interrupt vector system_call, interrupt service program SYS_XYZ
The process of middle processing: first save the value of the CS:EIP, save the current stack segment, the top of the stack, the flag register, load the system call or the system call in the middle of the service History portal. After executing the kernel code, completing the interrupt service, the process scheduling occurs
- System call Number: Pass with EAX register; system call parameter pass: Each parameter length does not exceed the register length, the parameter length is not more than 6, more than 6 sets a register as the pointer to pass as memory
Triggering the same system call using the Library function API and the embed assembler code in C code
(1) Using the Library function API to get the current time
of the system
(2) method of embedding assembly code in C code
(3) Use C code to embed assembly code to trigger system call to get system current time
(v) 30% Peel of the system call (bottom)
- The process of system invocation simplifies the analysis of important statements of code:
(1) Save_all Preservation site
(2) sys_call_table call system call corresponding handler function
(3) syscall_exit_work determine whether the current task is to be handled
(4) work_pending need to process signal (WORK_NOTIFYSIG)
(5) work_resches needs to be re-dispatched
(6) Interrupt context switching and process context switching may occur during scheduling
(7) Kernel: A collection of many kinds of interrupt processing contexts
- Simply browse the important statement analysis between System_call to Iret:
(1) Save_all Preservation site
(2) sys_call_table call system call corresponding handler function
(3) work_pending need to process signal (WORK_NOTIFYSIG)
(4) Call schedule (the code that determines the process scheduling is in schedule)
(5) Restore all recovery site
(6) Interrmpt return system call End
- Using GDB tracing:
(1) Make Rootfs: Auto compile, Generate root file system, auto start
(2) (GDB) List View Code
(3) (GDB) s single step debugging into function body (4) (GDB) n Single Step debugging does not enter function body
(vi) Description of the process and creation of the process
- The three main management functions of the operating system: process management, memory management, file system
- Fork () The user state to create the subprocess, and the fork system call returns once in the parent and child processes
- Where the child process starts: ret_from_fork
- PCB task_struct: Process status, Process-open files, process priority information
- PID Unique Identity process
- To create a new process in the kernel execution process
(1) using the system call clone, fork, Vfork can create a new process, but all by calling Do_fork to implement the process of creating
(2) Copy the parent process pcb--task_struct to create a new process to assign a new kernel stack to the new process
(3) Modify the copied process data, such as PID, process chain list and so on to perform copy_process and Copy_thread.
(4) P->thread.sp = (unsigned long) childregs; Top of the kernel stack when dispatched to a child process
(5) P->thread.ip = (unsigned long) ret_from_fork; The address of the first instruction when dispatched to a child process
- Start_ kernel created cpu_ idle, which is process No. 0. And the No. 0 process created two threads, one is Kernel_ init, that is, process 1th, the process eventually started the user state, the other is Kthreadd. Process number No. 0 is a fixed code, process 1th is made by copying the No. 0 process PCB after the modification obtained
- Iret corresponds to the int 0x80 directive, one is the pop-up register value, and one is the value of the press-in register.
- If the system call is analogous to fork (), then it is equivalent to a system call that creates a child process, and then the child process returns and then runs in the kernel state, while returning to the parent process and still running in the user state
(vii) Loading of executable programs
- How the executable program is derived: C code is compiled by the compiler, compiled into assembly code, compiled by the compiler into the target code, linked to an executable file
- Linux kernel mounts and launches an executable program
(1) Create a new process
(2) The new process calls the EXECVE () system call to execute the specified elf file
(3) Call the kernel's entry function Sys_execve (), SYS_EXECVE () service routines Modify the execution context of the current process
There are 3 main executables in the elf format: relocatable files. o, executable file, share destination file
The elf executable is mapped to the 0x8048000 address by default.
How command-line arguments and environment variables are entered into the new program's stack
The shell program-->execve-->sys_execve and then copies it when the new program stack is initialized.
Function call parameter pass, then system call parameter Pass
The current program executes into the kernel state when the EXECVE system is called, loads the executable file in the kernel with EXECVE, overwrites the executable file of the current process, and execve the system call back to the starting point of the new executable program
The loading process of a dynamic link library is a graph traversal process, in which the. Interp and. Dynamic in the ELF format need to rely on the dynamic linker to parse, and return to the program entry of the dynamic linker when entry returns to the user state instead of returning to the starting point specified by the executable program
(eight) process switching and general execution of the system
(2) The user state process can only be passively dispatched, that is, in the interrupt processing process scheduling
(3) kernel thread can directly call schedule (), can also be scheduled in the interrupt processing process, can be actively scheduled or can be passively dispatched
- Process Context Switch: Hangs the process that is running on the CPU, is different from the Save field at the time of interruption, is in the same context before and after the interruption, but only by the user state to the kernel state, but the process switch is two process switching
(1) Information to be switched: User address space, control information, hardware context (interrupt also to save context), etc.
(2) Schedule () in the context_switch of the switch_to Toggle register and the state of the stack, the position of the EIP
(3) Thread.sp of the core stack; Thread.ip EIP for the current process
(4) The first instruction of next process: NEXT_IP is generally $1f, for newly created child processes is ret_from_fork
- Switch_to does a critical process context switch
- Restore_all Recovery Site
- 0-3G user state, 3gG or above kernel state access
Several special cases in the process of Linux system execution
(1) kernel thread does not change the CS segment through interrupt processing (switch between user-state process and kernel thread and kernel process and kernel process).
(2) kernel thread active dispatch schedule (), only the process context switch, no interrupt context switch
(3) Create a system call to the child process, when the child process returns the execution starting point is ret_form_fork (next_ip=ret_form_fork)
Third, experience
Learning from the eight-week Linux kernel Analysis course, I learned a lot about how the Linux kernel works. Every week on time to take part in the exercise of homework to write a blog has become a habit I gradually develop. At first I was very confused about some of the professional words I spoke to my teacher, and I can now explain those terms in my own words. From the dynamic presentation stack to the present I can skillfully form a change diagram in the brain, from the unfamiliar to the code can be skilled to make the experimental part, including cloning, tracking, set breakpoints and other basic operations , from ignorant to some of the more abstract concepts to listen to the teacher through the metaphor to become more clear, know their relationship and differences.
Learning the analysis of the Linux kernel, I learned more about the principle of computer work, the process of switching, system calls have a very deep understanding. Through the teacher set up the test + mutual evaluation + self-evaluation of the learning model, I better learn the strengths of others, but also in this eight weeks to constantly improve themselves.
Finally thanked Monensin Teacher's careful explanation, let me to the Linux kernel work principle to have the thorough study.
"Linux kernel Analysis" interim summary