Malmqvist + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
Introduction of Linux Kernel source code
1, computer three magic weapon
Stored program computers
function Call stack
Interrupt mechanism
2, the operating system two swords
Switching of interrupt contexts
Switching of the process context
3. Function Directory
Linux-3.18.6/arch/x86
Kernel boot-related code basically exists in the init directory.
The Start_kernel function is equivalent to the main function of a normal C program.
The core code of the Linux kernel is in the kernel directory.
Second, construct a simple Linux system menuos
Experimental process:
Enter the virtual machine of the lab building and open the shell.
CD linuxkernel/
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img
After the kernel boot is completed, enter the menu program, Support Command Help, version and quit.
Iii. ways to use GDB to track and debug the Linux kernel
1. Using GDB to trace the debug kernel
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
The current state is frozen.
2, GDB set breakpoints
(1) Also open a shell window
Gdb
(gdb) file Linux-3.18.6/vmlinux load symbol table before #在gdb界面中targe remote
(GDB) Target remote:1234 #建立gdb和gdbserver之间的连接, press C to keep the Linux on qemu running
(GDB) Break Start_kernel #断点的设置可以在target remote before it can be
(2) After setting the breakpoint, enter the C command to continue execution, and the function will stop at the breakpoint.
(3) Enter the list directive to see the code at the breakpoint.
Four, simple analysis Start_kernel
1. Global variable Init_tast: The pcb,0 process, which is created manually, is the final idle process.
2, Trap_init: Hardware interrupt, initialization of some interrupt vectors, system calls.
Set_intr_gate: Set the interrupt gate.
Set_system_trap_gate: System Trap Gate Syscall VECTOR.
3. Mm_init: Initialization of memory management module.
4, Sched_init: Process scheduling initialization function, the function has done a very critical one-step initialization-on the No. 0 process, that is, the idle process initialization.
5, Rest_init: Other initialization functions, within the function will create a process 1th, that is, the init process.
6, Init_process: Is the Linux system 1th process, is the first user-state process, the default root directory of the INIT program.
7, Kthreadd: Kernel thread, used to manage system resources.
V. Summary
1, Daosh One, life two, two born three, Sansheng everything.
2, the Start_kernel function is equivalent to the normal C program's main function. The kernel boot process includes Start_kernel before and after, all of which are pre-initialized assembly instructions, followed by the OS initialization of the C code, and finally the first user-state process init.
3. In general, the Linux kernel boot process of the x86 architecture is divided into 6 major steps, namely:
(1) The entry function of the real mode _start (): In the header. S, this goes into the well-known main function, which copies bootloader parameters, executes basic hardware settings, and parses command-line arguments.
(2) Ingress function for Protected mode startup_32 (): In Compressed/header_32.s, the Bzimage kernel image is decompressed and the Vmlinux kernel file is loaded.
(3) Kernel entry function startup_32 (): In Kernel/header_32.s, this is called process 0, which goes into the architecture-independent start_kernel () function, known as the Linux kernel startup function. Start_kernel () will do a lot of kernel initialization, parse the kernel-initiated command-line arguments, and start a kernel thread to complete the kernel module initialization process, then enter the idle loop.
(4) Kernel module initialization entry function Kernel_init (): In Init/main.c, this will start the kernel module, create a memory-based ROOTFS, load Initramfs file or CPIO-INITRD, and start a kernel thread to run it. Init script to complete the mount of the real root file system.
(5) Root file system Mount script/init: This will mount the root file system, run/sbin/init, and start the well-known process 1.
(6) Init process of the system initialization process: Execute the relevant script to complete the system initialization, such as setting the keyboard, font, loading module, set up the network, etc., and finally run the login program, the login screen appears.
Linux kernel Analysis--construct a simple Linux system menuos