Linux kernel analysis The third week constructs a simple Linux system menuos

Source: Internet
Author: User

First, the computer's three magic weapon

Stored program computer, function call stack, interrupt
Second, the operating system of the two swords:
1. Interrupt context switch, save site and restore site
2. Switching of the process context.

Third, the Linux kernel source code analysis:

arch/directory saves source code that supports a variety of CPU types, with a focus on x86
init directory: contains MAIN.C, kernel boot-related code is basically in the init directory
IPC Directory: Inter-process communication
Kernel directory: The core code of the Linux kernel.
Iv. constructing a simple Linux system

Open the shell with a virtual machine in the lab building

1.CD linuxkernel/
2.qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img

After the kernel boot, enter the menu program, support three commands help, version and quit.

V. Using GDB to trace the debug kernel

Debug kernel with GDB trace

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 is frozen before startup (use ' C ' to start execution) CPU is initialized
-S shorthand for-gdb tcp::1234 establishes a GDB server on port 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 Targe remote in GDB interface

(GDB) Target remote:1234 # Establish a connection between GDB and Gdbserver, press C to keep Linux on qemu running

(GDB)break Start_kernel # breakpoints can be set before target remote or after

(GDB) C # System started, boot to Start_kernel

(gdb)List # can see the code start_kernel up and down

(GDB) Breakrest_init

(GDB) C # Current system execution to Rest_init

(GDB) Span class= "hljs-built_in" >list # you can see that rest_init is called at the tail of Start_kernel.

break kernel_thread

(GDB) c # Current system execution to kernel_thread

(GDB) list # This step you can see Kernel_thread (KERNEL_INIT,NULL,CLONE_FS)

break kernel_init

(GDB) c # current system execution to kernel _init

list #

(GDB) break run_init_process

(GDB) c

(GDB) d Span class= "Hljs-number" >3 # Kernel_thread is numbered 3

(GDB) c # stop at run_init_process

(gdb)list

Six, the Linux kernel boot process

Three parameters to start the Linux kernel: kernel initrd root partition, directory

The most important line of code:

qemu -kernel (文件名) -initrd (rootfs.img)

QEMU is equivalent to opening a virtual machine

Kernel starts a kernel, and the location is specified by the file name after it. If in the current directory, you can enter the file name directly, if not, you need to enter the full path of the kernel.

INITRD instruction is to hang a ramdisk virtual hard disk, is an important complement to the kernel, rootfs.img is the virtual hard disk, there is a partition inside, and then started is actually the init file, this file is compiled by the previous menuos, Gcc-o named Init.

So it's going to start a kernel, hang a hard drive, and then run an init process of number 1th.
In other words, there is a start_kernel function in MAIN.C in init
A rest_init is called at the tail of the Start_kernel function
There is a global variable init_task, the pcb,0 process that is created manually, the final idle process. Process number No. 0 is always present and the system does not have a process that needs to be dispatched to process No. 0.

Process No. No. 0 created process 1th and other
Rest_init () Kernel_thread (KERNEL_INIT,NULL,CLONE_FS)
There are run_init_process in Kernel_init,
Run_init_process creates a process, a program under the default path.

VII. Experimental Process

To construct a simple Linux system menuos:

Construct a simple Linux system menuos

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.

Eight, 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

Linux kernel analysis The third week constructs a simple Linux system menuos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.