Linux kernel analysis-booting from Start_kernel to init process using GDB trace debug kernel

Source: Internet
Author: User

Name: Jiang

id:fuchen1994

Date of experiment: 2016.3.13

Experimental guidance

    • 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 is completed into the menu program ("Software Engineering C Coding Practice" course project), support three commands help, version and quit, you can also add more commands, for elective "Software Engineering C Coding practice" of children's shoes should be a piece of cake.

    • 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

Open another Shell window

    1. Gdb
    2. (gdb)file linux-3.18.6/vmlinux # Load symbol table before Targe remote in GDB interface
    3. (GDB) Target remote:1234 # Establish a connection between GDB and Gdbserver, press C to keep the Linux on qemu running
    4. (GDB) break Start_kernel # breakpoints can be set before target remote or after

Experimental requirements:

    • Booting from Start_kernel to init process using GDB trace debug kernel

    • Detailed analysis of the process initiated from Start_kernel to Init process and the combination of experiments to write a signed blog, and in the blog post "real name (and the name of the final application certificate must be consistent) + Original works reproduced please specify the source +" Linux kernel Analysis "MOOC Course/http mooc.study.163.com/course/ustc-1000029000 ", the specific requirements of the blog content are as follows:

      • Topic self-proposed, the content revolves around the Linux kernel boot process, namely starts from Start_kernel to the INIT process;

      • You need to use an experiment in your blog

      • The execution of the Start_kernel function needs to be carefully analyzed in the blog content

      • The summary section needs to clarify its understanding of the "Linux system startup process", especially how the idle process, process 1th is coming.

    • 3) Please submit the blog post URL to the NetEase cloud classroom MOOC Platform Linux kernel Analysis Mooc course, edited into a link can be directly clicked open.

Linux Kernel directory:

Arch The directory includes all the core code related to the architecture. Each subdirectory below it represents a Linux -supported architecture, such as i386, a subdirectory of the Intel CPU and its compatible architecture. PC machines are generally based on this directory .

COPYING Under the catalogue is the GPL copyright statement. Any program created by the modification of the source code with the GPL's copyright, or a program generated using the GPL, has the obligation to use the GPL, such as public source.

CREDITS Under the catalogue is the Hall of Fame. Information about some of the people who have made a big contribution to Linux.

Documentation The directory is a few documents, no kernel code, unfortunately are Chinese, is the role of each directory specific instructions.

Drivers All device drivers in the system are in the directory . It is further divided into several types of device drivers, each with a corresponding sub- directory , such as the driver of the sound card corresponding to the Drivers/sound; Block device drivers, such as the IDE (IDE.C). If you want to see how all the devices that might contain the file system are initialized, you can look at the Device_setup () in DRIVERS/BLOCK/GENHD.C. It not only initializes the hard disk, but also initializes it because the NFS file system needs to be installed on the network other: for example, lib to place the core library code; NET, core and network-related code; IPC, this directory contains the code for the core interprocess communication; Fs, all file system code and various types of file operation code, each of its subdirectories supports a file system, such as fat and ext2.

FS The directory holds Linux -supported file system code and various types of file manipulation codes. Each subdirectory supports a file system, such as the ext3 file system that corresponds to the ext3 subdirectory .

include The directory includes most of the header files needed to compile the core, such as platform-independent header files under the include/Linux subdirectory, and Intel CPU-related header files in include/ Asm-i386 subdirectory, and the INCLUDE/SCSI directory is the header file directory for the SCSI device.

Init The directory contains the core initialization code (not the system's boot code), with MAIN.C and version.c two files. This is a good starting point for studying how the core works.

IPC The directory contains the communication code between the core processes.

Kernel Kernel Management Core code, the file in this directory implements the kernel functions of most Linux systems, the most important of which is SCHED.C, and the processor structure-related code is placed in the archlib/ directory .

maintainers The catalog holds a list of maintenance personnel who are responsible for each part of the current version of the kernel.

Makefile The first makefile file of the directory . Used to organize the kernel of the various modules, recorded a module of the relationship between each other and rely on relationships, compile-time use; it is helpful to read the makefile files in each sub-directory to understand the relationship and reliance of each document.

mm The directory contains all the memory management code that is independent of the CPU architecture, such as the allocation and deallocation of page storage management memory. The memory management code associated with the specific hardware architecture is located in the arch/*/mm directory , such as arch/i386/mm/fault.c.

Modules The directory contains the built-in, dynamically loaded module file directory, which is an empty directory for storing the module target files generated at compile time.

Net The directory is the core of the network part of the code , each of its subdirectories corresponds to one aspect of the network.

ReadMe The directory is the core and its compilation configuration method simple Introduction

reporting-bugs There are some things about reporting bugs in the catalog .

Rules.make In the catalogue are some common rules used by various Makefilemake

Scripts The directory contains script files for configuring the core, and so on.

In general, there is a. depend file and a makefile file in each directory . These two files are the secondary files that are used at compile time. A careful reading of these two documents is helpful in figuring out the links and dependencies of each document. The other directory also has a readme file, which is a description of the file in the directory , the same is conducive to the understanding of the kernel source code .

Command Description:

Qemu-kernel linux-3.18. 6/arch/x86/boot/bzimage-initrd rootfs.img-s-S # about-Sand--s Freeze CPU at startup (use ' C ' to S Tart execution)  for-gdb tcp::1234 If you do not want to use port 1234, you can use-GDB tcp:xxxx to replace the-s option  // A GDP service was created on this port of TCP1234
gdb (gdb) file Linux-3.18. 6/vmlinux # Targe remote before loading symbol table (GDB) in the GDB interface target remote:1234  # Establishes a connection between GDB and Gdbserver, Press C to allow the Linux on QEMU to continue running (GDB) break Start_kernel # Breakpoint settings can be before target remote, or after

Experimental process:

First step: Start and freeze

Now we start to start the GDB service

Load Symbol table

Link Gdbserver

Set a breakpoint

Press the C system to start execution to pause at the breakpoint

Using the list command, you can see the context of the Start_kernel function

Set breakpoints at Rest_init

Start_kernel () is the assembly code of the kernel and the junction of C code, before this, all the assembly code to complete the initialization of various environments, including the kernel code loaded into memory, set the C run environment and so on.

When running to Start_kernel (), we can roughly analyze the following:

1. Manually create process # No. 0 init_task (), he eventually becomes the idle process

Set_task_stack_end_magic (&init_task);

The Init_idle () function will add init_task to the CPU's running queue, and Init_task will always run when no other process joins the CPU queue, and Init_task will be set to idle when other processes join in. and use the dispatch function to switch to the newly added process.

2. Initialize each module

The module is as follows: Memory Management module Interrupt Dispatch module, etc.

3. Run to Rest_init (), Initialize the process

where Rest_init () is the last step in kernel initialization, and it is also the ancestor of all processes.

Here we analyze the function

Note This code:

Kernel_thread (init, NULL, CLONE_FS |  Clone_sighand); Initializes the first user-state process, which is process number 1th

OK, let's continue with the analysis.

Static voidNoinline Rest_init (void)417__releases (Kernel_lock)418  {419Kernel_thread (init, NULL, CLONE_FS |Clone_sighand);420Numa_default_policy ();421Unlock_kernel ();423  /*424 * The boot idle thread must execute schedule () 425 * At least one to get things moving:426*/427preempt_enable_no_resched ();428schedule ();429preempt_disable ();430 431  /*Call to Cpu_idle with preempt disabled*/432Cpu_idle ();433}

In this piece of code, we just need to analyze

Kernel_thread (init, NULL, CLONE_FS |  Clone_sighand);
Cpu_idle ();  CPU Queue process switch, set number NO. 0 process to idle

These two lines, the others can be temporarily ignored

The function init passed in Kernel_thread needs to be analyzed, and we intercept some of the code as follows:

Run_init_process ("/sbin/init"); Run_init_process () is actually building a similar user-state code through an embedded assembly
Run_init_process ("/etc/init"); The same sys_execve () call, whose argument is the executable file name to execute, and
Run_init_process ("/bin/init"); Is here the init process file on disk.
Run_init_process ("/bin/sh");

Because the lab building's rotten environment is always stuck, so we don't analyze it.

At Run_init_procrss () breakpoint debugging, Run_init_process is running the init program through EXECVE ().

The task of Idle_task () is completed here, and the scheduled function will be set to the idle process.

Summary: Linux is the assembler code before Start_kernel executes, and after he executes, the various environments are initialized to execute C code. Process No. 0 is created manually by the author, and its task is to execute it in the absence of a process in the CPU queue, switch to a new process when there is a process, and then be set to an idle state. Start_kernel the last part of the pid=1 is the formal generation of the first user-state process, which is rest_init (), which is the system's 1th process, at which time the process No. 0 will be set to the idle process. 1th Process execution, all the processes required to build the system, is actually called the run_init_process () function load file, build process

Linux kernel analysis-booting from Start_kernel to init process using GDB trace debug kernel

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.