Construct a simple Linux system menuos
Note: Xuan Wenjun, original works reproduced please indicate the source, "Linux kernel analysis"MOOC course http://mooc.study.163.com/course/ USTC-1000029000
First, the Linux kernel source code introduction
1. Root directory
The code in the Arch/x86 directory is our focus, and arch includes source code that supports different CPUs.
The init directory contains kernel boot-related code, such as MAIN.C (the Start_kernel function is equivalent to the main function of the normal C program, which is the starting point for the Linux kernel initialization).
IPC: Inter-process communication
Core code of the Kernel:linux kernel
Follow the Readme file
Second, construct a simple Linux system menuos
< step guidance >
1, in the Experimental building environment:
CD linuxkernel/
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img
You can start the kernel, then go to the menu program, support three commands help, version and quit.
2, the use of their own Linux system environment to build MENUOS process
# 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 (Unzip)
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 wall, you can use attachments Menu.zip
CD Menu
Gcc-o init linktable.c menu.c test.c-m32-static–lpthread (init is the first user-state process, is process 1th, in the form of static compilation)
Cd.. /rootfs
Cp.. /menu/init./
Find. | Cpio-o-HNEWC |gzip-9 >. /rootfs.img (img image file)
# Start the MENUOS system
CD ~/linuxkernel/
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img
3. Reconfigure and compile Linux to carry the debug information
(1) 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
(2) Make recompile (time longer)
4. 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
Open another Shell window
Gdb
(gdb) file Linux-3.18.6/vmlinux # Load symbol table before target remote in GDB interface file Home/shiyanlou/linuxkernel/vmlinux
(GDB) Target remote:1234 # Establish a connection between GDB and Gdbserver, press C to keep the Linux on qemu running
(GDB) Break Start_kernel # breakpoints can be set before target remote or after
Third, follow-up debugging Linux kernel boot process
1, before the kernel boot is frozen, the breakpoint is set to complete, hit C Enter can resume startup.
Command: (GDB) list to view the code before and after a breakpoint
Set breakpoints before Rest_init (): Break Rest_init (), and press C to run, list to view the code.
2, simple analysis of Start_kernel
In the MAIN.C under the init directory
The global variable init_task, which is the pcb,0 process created by hand, is the final idle process.
Regardless of which part of the analysis kernel will be involved in Start_kernel.
Trap_init (); Initialize interrupt vector
Example: Set_system_trap_gate (SYSCALL vector,&system_call) system Trap Gate (System call)
Mm_init (); Memory Management module
Sched_init (); System scheduling module
The last sentence in Start_kernel: Rest_init (); The Start_kernel has been in existence since the start of the kernel, which is number No. 0.
Process number No. 0 created process Kernel_init 1th.
Dispatched to the idle process when no process is required for the system to execute.
Daosh One, life two, two three, Sansheng everything.
Lab Report:
1, CD Home/yl/menu/rootfs, start the Linux kernel: QEMU-SYSTEM-X86_64-KERNEL/BOOT/VMLINUZ-4.3.0-KALI1-AMD64-INITRD. /rootfs.img
Note: Find. | Cpio-o-HNEWC | Gzip-9 >. /rootfs.img to package all files in the current directory to generate an IMG image file
2, QEMU-SYSTEM-X86_64-KERNEL/BOOT/VMLINUZ-4.3.0-KALI1-AMD64-INITRD. /rootfs.img-s-S, start the Linux kernel, stop at the beginning and set breakpoints for debugging.
3. Reopen a terminal window, enter GDB, and use the Help command to view the commands you can select.
4. Load symbol table before target remote in GDB interface: Switch to usr/src/linux-source-4.4 Path First, then enter command file Vmlinux. Then establish the connection between GDB and Gdbserver: Target remote:1234.
5. Set breakpoints before Start_kernel: Break Start_kernel, press C to keep the Linux on qemu running
Summarize:
The experiment is to construct a simple Linux system menuos, using the teacher has configured the virtual machine, it is more convenient to do, but because of the Linux kernel version and the reason for the configuration, so in the use of the command and net lesson introduced some differences, mainly the path of the difference, After understanding the meaning of the parameters in each command, the command is easier to use and the experiment can be carried out smoothly.
In the course of the consolidation, I learned that the Linux kernel boot three elements are: KERNEL,INITRD and root partition. For kernel boot, the most important command is: QEMU-SYSTEM-X86_64-KERNEL/BOOT/VMLINUZ-4.3.0-KALI1-AMD64-INITRD. /rootfs.img. In the following GDB debugging process, pay attention to the path!
Job 3: Construct a simple Linux system menuos 20135115 Xuan Wenjun