Linux Kernel Analysis No.3

Source: Internet
Author: User

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.

    1. CD linuxkernel/
    2. 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

  1. # download Kernel source code compile kernel
  2. CD ~/linuxkernel/
  3. wget https://www. kernel.org/pub/linux/kernel/v3. x/linux-3.18.6 . Tar. XZ
  4. Xz-d linux-3.18.6. tar. XZ
  5. Tar -xvf linux-3.18.6. Tar
  6. CD linux-3.18.6
  7. Make i386_defconfig
  8. make # usually compiles for a long time, less 20 minutes more hours
  9. # Make Root file system
  10. CD ~/linuxkernel/
  11. mkdir Rootfs
  12. git clone https://github. com/mengning/menu. Git # If you are on a wall, you can use attachments menu.zip
  13. CD Menu
  14. Gcc-o init linktable.c menu.c test. C-m32-static–lpthread
  15. CD .. /rootfs
  16. CP .. /menu/init ./
  17. Find . | cpio-o-HNEWC | Gzip -9 >. /rootfs. img
  18. # Start the MENUOS system
  19. CD ~/linuxkernel/
  20. Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage -initrd rootfs.img

    • Reconfigure Linux to carry debug information

    1. On the basis of the original configuration, make Menuconfig Select the following option to reconfigure Linux to carry debug information

      1. Kernel hacking->
      2. [*] Compile the kernel with debug info
    2. 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)

    • Debug kernel with GDB trace

    1. Qemu-kernel linux-3.18.6/arch/x86/boot/bzimage -initrd rootfs.img-s -S # Description of the-s and-s options:
    2. #-S Freeze CPU at startup (with ' C ' to start execution)
    3. #-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

  1. GDB
  2. (GDB) file  linux-3.18 .6 /vmlinux #  loading the symbol table before targe remote in the GDB interface
  3. (GDB) Target remote:1234 #  establish a connection between GDB and Gdbserver, press c  to let the Linux on Qemu continue to run
  4. (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

Related Article

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.