2019-2020-1 20199324 "Linux kernel principle and Analysis" in the fourth week of work

Chapter III MenuOs construction

A. Knowledge summary

  • Computer three magic weapons:
    • Stored program computer
    • Function call stack
    • Interrupt
  • The two swords operating system:
    • Interrupt context switching (save the site and site restoration)
    • Process context switch

They both assembly language and is inextricably linked

  • Linux kernel analysis of the more important are:
    • X86 source files in the directory under the arch directory
    • main.c in the init directory ( which is the starting point of start_kernel function to initialize the Linux kernel boot )
    • Under the kernel directory and process scheduling related code

II. Analysis of the Linux kernel boot process tracking

1. Experimental procedure

By the following command to the Linux system and a simple file system up and running

cd LinuxKernel/
qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img


Using gdb trace debugging kernel, add -S (before he began to freeze up CPU) and -s (to create a gdb-server on port 1234, you can then open another window with gdb kernel image with the symbol table is loaded in, and then set a breakpoint connected gdb srever tracking kernel) two parameters. The following diagram, you can see the kernel is frozen up.

Then open a window, start gdb, the kernel loads come in and establish a connection.
Enter the following command in gdb

file linux-3.18.6/vmlinux 
target remote:1234
break start_kernel 
c //按c让qemu上的Linux继续运行

We can see the results as

an input to the code can view the list of up and down start_kernel

Then set a breakpoint rest_init continue

I can see rest_init is called at the end of start_kernel.

2. The problem encountered

When I would open a window of time to start gdb, when loading the symbol table before gdb interface targe remote there is a problem found with the help of the students is when the last step of the kernel boot QEMU windows closed leads.

3. Analysis of the implementation process start_kernel function

asmlinkage __visible void __init start_kernel(void)
{
    char *command_line;
    char *after_dashes;
    
    /*
     * Need to run as early as possible, to initialize the
     * lockdep hash:
     */
    lockdep_init();
    set_task_stack_end_magic(&init_task);// init_task即手工创建的PCB,0号进程即最终的idle进程
    smp_setup_processor_id();
    debug_objects_early_init();
    // ...
    trap_init();                          // 中断向量的初始化
    mm_init();                            // 内存模块的初始化
    sched_init();                         // 调度模块的初始化
    // ...
    rest_init();                          // rest_init是0号进程(是使用宏初始化的),它创建1号进程init和其他的一些服务进程
}

4.Linux system startup process

The main kernel initialization module is called in start_kernel function.

idle process is how come : init_task () (PID = 0 ) after creating the init process, calling cpu_idle () evolved into the idle process, after which a scheduler, init process runs.

No. 1 process is how come : Part 1 kernel thread initialization responsible for the implementation of the kernel and system configuration, the last call do_execve load the init process, evolved into the init process (user mode 1 process), init process is the first kernel boot a user-mode process.

kthreadd (PID = 2) process created by the process number 0, always running in kernel space, is responsible for scheduling and managing all kernel threads.

After the completion of the kernel starts, there is a call_cpu_idle, when the system does not need to perform the process is called idle process, that is, "0 process." After the system idle process from the start has been in existence, it creates No. 1 init process and some other service process, so that the system starts up.

Guess you like

Origin www.cnblogs.com/yangdd/p/11627191.html