Liu Forest Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course
Blog Directory
The first week: how Computers work
Second week: Complete a simple time slice rotation multi-channel program kernel code
Third week: Linux kernel boot analysis process
Week Four: Use the same system call using the Library function API and the embed assembly code in C code two ways
Week Five: Analyzing the System_call interrupt processing process
Week Six: Analysis of the system call processing process for the fork function
Week Seventh: Analysis of the system call processing process for the exec* function
Eighth week: Linux process scheduling--schedule () function analysis
Course Summary 1, how the computer works
This paper mainly introduces some basic knowledge, including the basic knowledge of assembly language, the stack change during C function call and the process of parameter passing.
- Storage program Computer working model--Von Neumann architecture.
- Programs: tells the computer how to do the steps, the data entered, and how to store the processed results.
- Assembly Language: assembly language is a translation of machine languages.
2, complete a simple time slice rotation multi-channel program kernel code
Simple simulation of kernel code. It mainly includes function call stack, function stack frame, kernel initialization, interrupt, process context switching process and multi-channel program simulation based on time slice rotation.
- Operating system Three Magic weapon: the storage program computer, function call stack, interrupt mechanism.
- Operating system Two Swords: interrupt context, the process context switch.
- Interrupts: Multi-channel program design, can be run simultaneously by multiple programs, when an interrupt occurs, by the CPU and the kernel code together to achieve the save site and recovery site.
- Operating system core functions: Process scheduling and interrupt mechanism, through the cooperation with the hardware to achieve multi-tasking, coupled with the upper application software support + hold, and eventually become a computer system that can make it easy for users to operate.
- Process switching: When a running process waits for additional system resources, the Linux kernel gains control of the CPU and allocates the CPU to other waiting processes. The process switching mechanism includes switching of ESP, switching of stacks.
3, the Linux kernel boot analysis process
During this week, we learned to read the kernel source code and construct the Linux system Menuos, mainly including the source code main structure, kernel boot and debugging method and Start_kernel interpretation. We conclude as: Daosh one (Start_kernel–>cpu_idle), Life Two (Kernel_init and Kthreadd), second born three (i.e. the preceding 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)
- Kernel boot completed into menu program, support three command quit, version, help
- IPC and process communication-related directories
4, using the Library function API and C code embedded in the assembly code two ways to use the same system call
Knowledge about system calls. Mainly includes the system call and the user state, the kernel state understanding, by writing the assembly code to understand the system call mechanism.
- Intel x86 CPUs have four different execution levels 0-3,linux use only 0 levels and 3 levels to represent the kernel State and user state respectively.
- The logical address space above 0xc0000000 can only be accessed in the kernel state, and the logical address space of 0X00000000-0XBFFFFFFF is accessible in both states.
- Interrupt processing is the primary way to enter the kernel state from the user state, and the system call is only a special interrupt.
The complete process of interrupt processing:
- The relationship between the API (Application programming Interface) provided by the operating system and the system call: not one by one correspondence
- Three-layer skins for system calls: XYZ, System_call, and SYS_XYZ
- Performing system calls through INT$0X80 in Linux-generating a programming exception with a vector of 128
- The process needs to indicate which system call (via EAX register) needs to be passed by passing a parameter called the system call number--the system call number Associates XYZ with SYS_XYZ.
- System_call is the entry point for all system calls in Linux, with at least one parameter per system call, which is the system call number passed by eax
5, Analysis System_call Interrupt processing process
This week's experiment required us to add a system call we wrote to deepen our understanding of the process of system invocation in kernel code.
- The first step, Save_all save the scene.
- The second step is to determine the interrupt information, pass the system call number through EAX, sys_call_table query to the calling system call, and then jump to the appropriate program for processing.
- The third step is to handle interrupts.
- Fourth step, Restore_all restore the field when the system is called, Iret returns the user state
6. Analyze the system call processing process corresponding to the fork function
This week we mainly learned about process description and creation. Mainly includes the organization form of PCB, process descriptor of data structure process, the key execution process of fork system call.
- Operating system three main functions: Process Management (CORE), memory management, file system
- PCB task_struct includes: Process status, process open files, process priority information
- Process Control block pcb--Process Descriptor Task_stuck provides the process information that the kernel needs to understand
- Process creation: Start_kernel Cpu_idle–>kernel_init and Kthreadd–>0, 1, 2nd process (where 1th process is the ancestor of all user threads, process 2nd is the ancestor of all kernel threads)
- Fork () system call: Create a child process in the user state, each return in the parent and child processes, the return value of the PID in the child process is 0, the return value in the parent process is the PID of the child process
- P->thread.ip = (unsigned long) ret_from_fork; The first instruction when scheduling to a child process
7, analysis of the exec* function corresponding system call processing process
This week we learned about the program loading process and the elf file format. It mainly includes getting an executable procedure, structure and static link of elf file format, static loading process of executable program and dynamic loading process.
- Two tasks for the linker: symbolic parsing, relocation
- Three forms of the target file: relocatable target file (compiler and assembler can be generated), executable target file (linker can be generated), shared target file (compiler and assembler can be generated)
- New executable starting point-typically the address space is 0x8048000 or 0x8048300
- Execve the value of an EIP saved in the kernel stack as a starting point for a new process when performing a static linker
- There are two forms of dynamic connections: Dynamic connection and runtime dynamic linking when the executable program is loaded
- Run-time dynamic load link requires at least dlopen, dlsym functions
8, Linux process scheduling--schedule () function analysis
This week we mainly learned about process switching and the overall execution process. Mainly includes the Linux process scheduling algorithm introduction, Process switch code Analysis, switch_to understanding, process-related data structure analysis.
- Linux process scheduling is based on time-sharing and priority
- In Linux, kernel threads are special processes that only have a kernel state without a user state
- The kernel can be seen as a collection of various interrupt processing and kernel threads
- User-State process cannot be scheduled actively, only through system calls or other interrupts into the kernel time to dispatch
- In Linux, kernel threads can be actively dispatched without interrupting context switching when actively scheduling
- The Linux kernel calls the schedule () function to dispatch and invokes Context_switch for context switching, which calls switch_to for critical context switching.
- Timing of process scheduling--schedule () function implementation scheduling
Feeling Harvest
- Have a certain understanding of the basic knowledge of Linux, some things are always wanted to learn but there is no chance
- Learn the use of system calls and GDB debugging tools, for future learning lay the foundation
- A lot of doubts about the operating system, such as process creation switching, have been understood in this course.
Regret
- There are still a lot of people do not understand, there is no time to learn, a lot of difficulties in the value of the course after the end of slowly studying
Linux Kernel Learning Summary