Trace analysis of the boot process of the Linux kernel
Yu Jiacen Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
In the previous lesson, we learned about the three magic weapons of the operating system (the stored program computer, the function call stack, the interrupt mechanism), and two swords (interrupt context switch and process context switch), and this lesson is focused on the interrupt mechanism
Prior to this we first learned about the Linux kernel source code, which has several important code:
The code in the Arch directory is huge, and the code in the Arch/x86 directory is very important.
INIT/MAIN.C is the kernel boot-related code, the Linux kernel starting point is start-kernel, equivalent to the normal C program in the main function
Kernel/is the process dispatch related code.
There are many other directories and codes that are not detailed here.
Then we construct a menuos, which is the content of the experiment.
- CD linuxkernel/
- Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage -initrd rootfs.img
In the lab building, just type in these two lines of code.
The following is the process of building menuos using your own Linux system environment
- # download Kernel source code compile kernel
- CD ~/linuxkernel/
- wget https://www. kernel.org/pub/linux/kernel/v3. x/linux-3.18.6 . Tar. XZ
- Xz-d linux-3.18.6. tar. XZ
- Tar -xvf linux-3.18.6. Tar
- CD linux-3.18.6
- Make i386_defconfig
- make # usually compiles for a long time, less 20 minutes more hours
- # Make Root file system
- CD ~/linuxkernel/
- mkdir Rootfs
- git clone https://github. com/mengning/menu. Git # If you are on a wall, you can use attachments menu.zip
- CD Menu
- Gcc-o init linktable.c menu.c test. C-m32-static–lpthread
- CD .. /rootfs
- CP .. /menu/init ./
- Find . | cpio-o-HNEWC | Gzip -9 >. /rootfs. img
- # Start the MENUOS system
- CD ~/linuxkernel/
- Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage -initrd rootfs.img
On the basis of the original configuration, make Menuconfig Select the following option to reconfigure Linux to carry debug information
-
- Kernel hacking->
- [*] Compile the kernel with debug info
Make recompile (longer)
Menuos Build Success
Open Directory
Where linux-3.18.6 is the kernel source code, ROOTFS store the executable file (stored in it with the menu compiled executable file)
- Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage -initrd rootfs.img-s -S # Description of the-s and-s options:
- #-S Freeze CPU at startup (with ' C ' to start execution)
- #-S Shorthand for-gdb tcp::1234 If you do not want to use port 1234, you can use-GDB tcp:xxxx to replace the-s option
Where-s means that the CPU is frozen before execution, and-s refers to creating a GDP server on port 1234
Open another Shell window
- GDB
- (GDB) file linux-3.18 .6 /vmlinux # loading the symbol table before targe remote in the GDB interface
- (GDB) Target remote:1234 # establish a connection between GDB and Gdbserver, press c to let the Linux on Qemu continue to run
- (GDB) break Start_kernel # breakpoints can be set before target remote or after
To set breakpoints before Start-kernel
The program is only executed to Start-kernel
Enter list, you can see the code of Start-kernel
Similarly, set a Rest-init breakpoint, the program executes to Rest-init, enter list, you can see Rest-init code
Through this experiment we found that no matter what part of the analysis kernel would involve start-kernel.
Now let's analyze Start-kernel.
There is a init_task, which is a hand-created PCB
Trap_init initialization Interrupt--"set-system-trap-gate (Syscall_vector,&system_call) sets the system trap Gate, where Syscall_vector is a system call and the system call is also an interrupt , just using instructions to trigger
There's a lot of modules in Start-kernel.
Mm_init () memory Management module
Sched_init () Dispatch module
Rest_init () Other-->kernel-init-->run-init-process is a process of the system, the default root directory of the Init
Kthreadd using a kernel process to manage resources
-->call into Cpu-idle
-->cpu-idle-loop
Idle is the system's No. No. 0 process, and the above instruction is the cycle # NO. 0 Process
Dispatch process No. 0 when no process is required for the system to execute
Rest-init is present at the start of the program, creating process 1th and kernel processes
Summary: The content of this course is a bit difficult and some places are not understood. Idle is process No. 0, about process No. 0 and process 1th, the so-called 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 all Things (process 1th is the ancestor of all user-state processes, and process number 2nd is the ancestor of all kernel threads).
Linux Kernel Analysis No.3