Linux Kernel Design Third week learning summary trace Analysis The boot process of the Linux kernel

Source: Internet
Author: User

Chen Chaojan Original works reproduced please specify the source

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Experimental steps

Landing Lab Building Virtual machine http://www.shiyanlou.com/courses/195

Open the shell terminal and execute the following command:

CD linuxkernel/

Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD rootfs.img


After execution, the Qemu window pops up and the Linux kernel boot information is output.


The interface displays the Menuos command prompt after a successful start


The system supports three commands: Help, version, quit
Debug kernel with GDB trace

Open the shell terminal and execute the following command:

CD linuxkernel/

Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S

Notes on the-s and-s options:

-S freeze CPU at startup (using ' C ' to start execution) freezes the CPU at system startup and continues with the C key

-S shorthand for-gdb tcp::1234 open remote debugging port, TCP protocol 1234 is used by default, and if you do not want to use port 1234, you can use-gdb tcp:xxxx instead of-s option


Open another shell terminal and execute the following command

Gdb

(GDB) Filelinux-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 the Linux on qemu running
(GDB) Breakstart_kernel # breakpoints can be set before target remote or after
Press the C key to continue to the Start_kernel () function
Enter the list command to view the Start_kernel () function code
Then set a breakpoint Rest_init
View Rest_init () function code
Using break to set breakpoints, C to continue, List view function code makes it easy to debug any functions used in the Linux boot process. Experimental analysis

This article takes Linux3.19.1 source code as an example analysis.

Analysis./init/main.c source file Start_kernel () function

The Linux kernel boot code is roughly divided into 2 parts:

Part of the hardware platform-related , stored in the./arch/directory, the platform to distinguish between different directories, such as the x86 platform in the./arch/x86/directory, written by the assembly language .

The other part is hardware platform-independent , written by C language .

The Start_kernel () function in the./INIT/MAIN.C is the first function executed by the Linux kernel boot process from platform-dependent to platform-independent code, in which the Linux kernel begins to actually enter the initialization phase.

Here is a brief introduction to several of these functions. (italics are source code, bold words for interpretation)

/*

* Need to run as early-possible, to initialize the

* LOCKDEP Hash:

*/

Lockdep_init ();

LOCKDEP is a kernel debug module that checks for potential deadlock problems with kernel mutex mechanisms, especially spin locks.

Set_task_stack_end_magic (&init_task);

The pcb,0 process, which is created manually, is the final idle process.

/*

* Set up the The initial Canary ASAP:

*/

Boot_init_stack_canary ();

The Canary value is the protection word for the stack that is used to prevent stack overflow attacks.

Trap_init ();

Initializes the kernel trap exception.

Mm_init ();

Initializes the kernel memory allocator.

/*

* Set up the scheduler prior starting any interrupts (such as the

* Timer interrupt). Full topology setup happens at Smp_init ()

* Time-but Meanwhile we still have a functioning scheduler.

*/

Sched_init ();

Initializes the scheduler data structure and creates a run queue. (The answer to a job title: what function is called in the Linux source Start_kernel function to initialize the process? )

/* Do the rest non-__init ' Ed, we ' re now alive */

Rest_init ();

The last function called in the Start_kernel () function.

Analysis./init/main.c source file Rest_init () function

The main function of the Rest_init () function is to create and start the kernel process init, the first user-state process.

int pid;

Define PID variable hold process number

Rcu_scheduler_starting ();

RCU (read-copy Update) lock mechanism started.

Reference: New lock mechanism in the Linux 2.6 kernel--rcu

/*

* We need to spawn Init first so it obtains PID 1, however

* The INIT task would end up wanting to create kthreads, which, if

* We schedule it before we create Kthreadd, would OOPS.

*/

Kernel_thread (Kernel_init, NULL, CLONE_FS);

The init process was created at this time, but it cannot be dispatched now.

Numa_default_policy ();

Set the memory access policy for the NUMA (Non-uniform Memory Access Architecture) system as the default.

Reference: NUMA technology for Linux

PID = Kernel_thread (Kthreadd, NULL, CLONE_FS | Clone_files);

Creates a Kthreadd kernel thread that manages and dispatches other kernel threads.

Rcu_read_lock ();

Kthreadd_task = Find_task_by_pid_ns (PID, &init_pid_ns);

Gets the thread information for the Kthreadd and gets the completion instructions Kthreadd has been created successfully.

Rcu_read_unlock ();

Complete (&kthreadd_done);

Notifies the kernel_init thread through a complete variable (kthreadd_done).

Experiment Summary:

When the computer system is power on the PC, the BIOS code is called to execute, and then starts to call the execution of the Linux kernel initialization code, after the platform-related assembly code is executed, it jumps to the Start_kernel () function and begins the real kernel initialization, where init_ The task creates the No. 0 process, which is the final idle process, and the Rest_init () function then creates the init process, the 1th process, and the Kthreadd process, the 2nd process, and the system starts to work properly.

Linux Kernel Design Third week learning summary trace Analysis The boot process of the Linux 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.