Linux Kernel Source Analysis--Kernel boot (5) Image kernel boot (rest_init function) (Linux-3.0 ARMv7) "Go"

Source: Internet
Author: User

Original address: Linux kernel source Code Analysis--Kernel boot (5) Image kernel boot (rest_init function) (Linux-3.0 ARMv7) Tekkamanninja

Transferred from: http://blog.chinaunix.net/uid-25909619-id-4938395.html

The previous rough analysis of the Start_kernel function, which basically is the memory management and the data structure initialization of each subsystem. When the kernel initialization function Start_kernel executes to the end, it is called rest_init function, the main mission of this function is to create and start the kernel thread init。 This function, although the meaning of the rest of the initialization, but this "left" is a lot of content, the following detailed analysis is as follows:
  1. /*
  2. * We must determine in a non-__init function or
  3. * The race between the other root thread (root thread) and the initialization thread (init threads).
  4. * (this race may cause Start_kernel to be free_initmem "harvested" before the root thread runs to Cpu_idle.) )
  5. *
  6. *
  7. * gcc-3.4 occasionally uses this function as an inline function, so noinline is used.
  8. */
  9. Static __initdata declare_completion (Kthreadd_done);

  10. Click (here) to collapse or open

    1. Define a complete variable to tell the init thread that the kthreads thread has been created.
    2. It used to be a big kernel lock instead of a complete lock.
  11. static noinline void __init_refok rest_init (void)
  12. {
  13. int pid;
  14. Rcu_scheduler_starting ();

    Click (here) to collapse or open

    1. The kernel RCU lock mechanism is scheduled to start, because the following will be used
  15. /*
  16. * We must first create the Init kernel thread so that it can get a PID of 1.
  17. * Although so the init thread will hang up and wait for the kthreads thread to be created.
  18. * If we dispatch it before creating the Kthreadd thread, the oops will appear.
  19. */
  20. Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand);

    Click (here) to collapse or open

    1. Create Kernel_init kernel thread, kernel of process # 1th!!!!!
  21. Numa_default_policy ();

    Click (here) to collapse or open

    1. Set the memory access policy for the NUMA system as the default
  22. PID = Kernel_thread (Kthreadd, NULL, CLONE_FS | Clone_files);
  23. Click (here) to collapse or open

    1. Creates a Kthreadd kernel thread that manages and dispatches other kernel threads.
    2. It loops through a function called Kthreadd, which is the function of running the kernel thread maintained in the Kthread_create_list global list.
    3. Call Kthread_create to create a kthread that will be added to the kthread_create_list linked list;
    4. The executed kthread will be removed from the kthread_create_list list;
    5. And Kthreadd will constantly call the scheduler function to make up the CPU. This thread cannot be closed.
    the above two threads are what we usually see in the Linux system with the PS command:

    Click (here) to collapse or open

    1. [Email protected]:~/development/analyze/linux-3.0$ ps-a
    2. PID TTY Time CMD
    3. 1? 00:00:00 Init
    4. 2? 00:00:00 Kthreadd
    5. ......
    Rcu_read_lock ();
  24. Kthreadd_task = Find_task_by_pid_ns (PID, &init_pid_ns);
  25. Rcu_read_unlock ();
  26. Complete (&kthreadd_done);

  27. Click (here) to collapse or open

    1. Gets the thread information for the Kthreadd and gets the completion instructions Kthreadd has been created successfully. and a complete variable (kthreadd_done) is passed to notify the kernel_init thread.
  28. /*
  29. * To get the system up and running,
  30. * Boot idle thread must execute at least once schedule ():
  31. */
  32. Init_idle_bootup_task (current);

    Click (here) to collapse or open

    1. Sets the current process to the idle (idle) Process class.
  33. Preempt_enable_no_resched ();

    Click (here) to collapse or open

    1. Enable preemption, but not rescheduling
  34. Schedule ();

    Click (here) to collapse or open

    1. Execute schedule, switch process.
  35. Preempt_disable ();

    Click (here) to collapse or open

    1. Process scheduling is complete, back here, to disable preemption.
  36. /* Call Cpu_idle when preemption is disabled */
  37. Cpu_idle ();

    Click (here) to collapse or open

    1. At this point the kernel body enters the idle state and consumes the idle CPU time slice in a loop, which never returns. This function is preempted when there are other processes that need to work! This function differs depending on the architecture.
  38. }
In the above function, the kernel creates two kernel threads, one is the kernel thread's manager, the other is the kernel initialization thread init, which is the focus of our analysis of the kernel boot, which continues to do the initialization of the system (which includes the device driver system):

Click (here) to collapse or open

    1. The following function is a function run by the kernel init thread that will complete the initialization of the device driver and invoke the Init_post function to start the init process of user space.
  1. static int __init kernel_init (void * unused)
  2. {
  3. /*
  4. * Wait for the Kthreadd to start to complete.
  5. */
  6. Wait_for_completion (&kthreadd_done);
  7. /*
  8. * Init can be assigned to a memory page at any node
  9. */
  10. Set_mems_allowed (Node_states[n_high_memory]);
  11. /*
  12. * Init can be run on any CPU.
  13. */
  14. Set_cpus_allowed_ptr (current, cpu_all_mask);

    Click (here) to collapse or open

    1. Increase the CPU affinity of the current process so that all CPUs (if they are SMP) can run this thread.
    2. A thread can be migrated to run on a masked CPU, but if the CPU bit is removed from the in-place mask, the thread will not run on that CPU.
  15. Cad_pid = Task_pid (current);

  16. Click (here) to collapse or open

    1. Cad_pid is the process ID of the int signal that receives the Ctrl-alt-del operation, which is obviously the PID set for Init
  17. Smp_prepare_cpus (Setup_max_cpus);
  18. Do_pre_smp_initcalls ();
  19. Lockup_detector_init ();
  20. Smp_init ();
  21. SCHED_INIT_SMP ();

    Click (here) to collapse or open

    1. The above code is prepared in the SMP system, activates all CPUs, and begins the scheduling of the SMP system.
  22. Do_basic_setup ();

    Click (here) to collapse or open

    1. In this connection, the architecture-related section has been initialized, and theDo_basic_setup function is primarily initialized with the initialization of the device driver, which completes the other drivers (modules that are compiled directly into the kernel). Most of the boot data output in the kernel (which is the driver module output of each device) is generated here.
    2. This function is more important and will be analyzed in detail later!
  23. /* Open the/dev/console in the root file system, no failure here */
  24. if (sys_open (const char __user *) "/dev/console", O_rdwr, 0) < 0)
  25. PRINTK (kern_warning "warning:unable to open an initial console.\n");

    Click (here) to collapse or open

    1. This is the first file opened by the Kernel_init (later init process) and it becomes the standard input.
    2. This needs to open the/dev/console, if there is no node, the system is wrong. This error message is also frequently encountered. The possible causes are:
    3. 1. When making the file system, forget to create the/dev/console node
    4. 2, file system mount problem, mounted on the file system is not nothing, is hanging the wrong node.
  26. (void) sys_dup (0);
  27. (void) sys_dup (0);

    Click (here) to collapse or open

    1. Copy the file descriptor of the standard input (0) two times (it is the/dev/console open above, which is the system console):
    2. One as standard output (1)
    3. One as standard error (2)
    4. Now the standard input, standard output, standard error is/dev/console.
    5. This console can be configured as a serial port (TTYSN, Ttyon, and so on) in the kernel boot parameters, or it can be a virtual Console (TTY0). So we see the system login prompt on the serial or monitor.
  28. /*
  29. * Check if there is an INIT program for early user space. If there is, let it execute
  30. *
  31. */
  32. if (!ramdisk_execute_command)
  33. Ramdisk_execute_command = "/init";
  34. if (sys_access (const char __user *) Ramdisk_execute_command, 0)! = 0) {
  35. Ramdisk_execute_command = NULL;
  36. Prepare_namespace ();
  37. }
  38. /*
  39. * Ok, we have completed the startup initialization, and
  40. * And we're essentially already running. Frees the initialized memory (INITMEM) segment
  41. * and start the user-space program.
  42. */
  43. Init_post ();
  44. return 0;
  45. }
At the end of the kernel init thread, the Init_post function was executed, and in this function the user space process Init was actually started, as explained below:
  1. /* This is a non-__init function. Force it to be a non-inline function in case gcc
  2. * Let it inline into init () and become part of the Init.text segment.
  3. */

    Click (here) to collapse or open

    1. From this function name, this function is run in the user space before the INIT program
  4. static noinline int init_post (void)
  5. {
  6. /* All asynchronous __init code must be completed before releasing memory */
  7. Async_synchronize_full ();
  8. Free_initmem ();

    Click (here) to collapse or open

    1. Frees all memory in the init.* segment.
  9. Mark_rodata_ro ();

    Click (here) to collapse or open

    1. By modifying the page table, the read-only data field is guaranteed to be read-only. Most architectures are empty functions.
  10. System_state = system_running;

    Click (here) to collapse or open

    1. Set the system state to a running state
  11. Numa_default_policy ();

    Click (here) to collapse or open

    1. Set the memory access policy for the NUMA system as the default
  12. Current->signal->flags |= signal_unkillable;

    Click (here) to collapse or open

    1. Set the current process (init) to not kill the process (ignoring the deadly signal)
  13. if (Ramdisk_execute_command) {
  14. Run_init_process (Ramdisk_execute_command);
  15. PRINTK (kern_warning "Failed to execute%s\n",
  16. Ramdisk_execute_command);
  17. }

    Click (here) to collapse or open

    1. If Ramdisk_execute_command has the specified init program, execute it.
  18. /*
  19. * We try each of the following functions until the function executes successfully.
  20. *
  21. * If we try to fix a really problematic device,
  22. * The Bourne Shell can replace the init process.
  23. */
  24. if (Execute_command) {
  25. Run_init_process (Execute_command);
  26. PRINTK (kern_warning "Failed to execute%s. Attempting"
  27. "defaults...\n", Execute_command);
  28. }

    Click (here) to collapse or open

    1. If Execute_command has the specified init program, execute it.
  29. Run_init_process ("/sbin/init");
  30. Run_init_process ("/etc/init");
  31. Run_init_process ("/bin/init");
  32. Run_init_process ("/bin/sh");
  33. Panic ("No init found. Try passing init= option to kernel. "
  34. "See Linux Documentation/init.txt for guidance.");

    Click (here) to collapse or open

    1. In the case where Ramdisk_execute_command and Execute_command are empty, the following initializations are executed sequentially: If none are found, the error message is printed. This is the error message that we often encounter when we do a system transplant, and this information is likely to be:
    2. 1, your startup parameter configuration problem, by specifying the INIT program, but not found, and the default of the four programs are not in the file system.
    3. 2, file system mount problem, file does not exist
    4. 3. Init program does not have permission to execute

    Click (here) to collapse or open

    1. At this point, the initialization of the kernel ended, formally entered the user space initialization process!!
  35. }

Linux Kernel Source Analysis--Kernel boot (5) Image kernel boot (rest_init function) (Linux-3.0 ARMv7) "Go"

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.