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

Construct a simple Linux system MenuOS

Using gdb debugging kernel tracing

  • Open a shell

The code analysis:
QEMU: Start already installed in the system corresponds to a virtual machine program qemu, this program provides a start for the kernel context.
-kernel file name + path: the launch kernel, the kernel is formed after compiling a file named init, and before it has to copy the file directory rootfs, and by way of cpio packaged into a file named rootfs under roofs. img image file.
-initrd rootfs.img: rootfs designated as drive hardware at startup.

  • Using gdb debugging kernel tracing

  • Still further a shell open window, Ctrl + Shift + O achieve segmentation level, start GDB, the kernel is loaded, establishes the connection.

  • Set breakpoints start_kerenl and rest_init

*/  
static __initdata DECLARE_COMPLETION(kthreadd_done);
    
static noinline void __init_refok rest_init(void)
{
int pid;

rcu_scheduler_starting();
/*

Simple analysis start_kernel

  • the start_kernel () function code portion
 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);
        smp_setup_processor_id();
        debug_objects_early_init();
                .............

        /* Do the rest non-__init'ed, we're now alive */
        rest_init();
 }
  • analysis

start_kernel () function to complete the initialization of the Linux kernel. Each core member is initialized with this function.

1. Call sched_init () function to initialize the scheduler

2. Call build_all_zonelists () function to initialize both memory management

3. Call page_alloc_init () function to initialize the buddy system allocator

4. Invoke trap_init () function and init_IRQ () function is to initialize the IDT

5. Call softing_init () function initializes TASKLET_SOFTIRQ and HI_SOFTIRQ (soft interrupt)

6. Call time_init () to initialize the system date and time

7. Call kmem_cache_init () function initializes the slab allocator (ordinary and cache)

8. call the calibrate_delay () function to determine the CPU clock (delay function)

9. calls kernel_thread () function to create a process within one thread, the kernel thread will create additional kernel threads and executes the / sbin / init program

10. start_kernel () will be displayed after the start of the implementation of linux version, in addition, the final phase will be the implementation of the init program and kernel threads show a lot of other information. Finally, there will be a familiar login prompt on the console, the Linux kernel has been launched to inform the user is running.

to sum up

  • Linux kernel boot process is:

    • A process that is performed initially 0 process init_task, it is a kernel thread at system initialization phase by the start_kernel () function created from scratch by hand, a process created after the No. 1 0 kernel thread kernel_init, call cpu_idle () become idle process, and when the system is idle process when the process does not need to be performed to schedule used.

    • No. 1 kernel processes responsible for implementing parts of the kernel initialization and system configuration, and then use kernel_thread (kernel_init, NULL, CLONE_FS) function (ie fork mode) established pid = 1, process 1, also called the init process (user mode No. 1 process), the system became the ancestors of all other processes when the scheduler to select the init process, the init process continue with the rest of the initialization. Then call kernel_thread execution kthreadd, create PID kernel threads 2, this process is always running in kernel space, it is responsible for scheduling and managing all kernel threads.

Guess you like

Origin www.cnblogs.com/Huyiming/p/11619862.html