Linux kernel Analysis (iii)--tracing and analyzing the start-up process of Linux kernel __linux

Source: Internet
Author: User
Tags bit set

Author: Sandy Original works reproduced please indicate the source
"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000 "
Experimental environment: C+linux64 bit (32-bit system may result in different)
In accordance with the academic integrity of the terms, I guarantee that the answer for my original, all the references to the external materials have been marked by provenance.

Experimental environment: ubuntu14.04 operating system, x86 architecture
Experiment Request: Use GDB to trace debug kernel from Start_kernel to init process launch

The first step, the Linux kernel code structure

This course provides a Linux kernel source, which is structured as follows:

Core Address: http://codelab.shiyanlou.com/xref/linux-3.18.6/

Different folders represent the different modules of the kernel, the meaning is: arch/is the architecture-related code, in which the contents of the/x86 folder is x86 architecture related code, is the core analysis of the important analysis goals. init/is the kernel boot-related code, which is the focus of this article. The/init/main.c file is the starting point for the kernel startup and is the primary analysis object for analyzing the kernel initiation process. The fs/filesystem (file system?) kernel/kernel-related code, and some of the key objects used in the kernel are defined in this area. mm/related code for memory management (Memory managment?)

The second step, Linux kernel startup process analysis

The Start_kernel () function in the/init/main.c file is the starting point for all the initialization of the system before the function is called, so the startup analysis of the kernel generally starts with this function; there is no main function in the MAIN.C, Start_ Kernel () This function is equivalent to the main function in the C program. The following is an analysis of the kernel start process from this function.

Because there is no time to keep the habit of writing blog, coupled with the reason of cheap hands, so this is a new blog post, because the reason for the time the experiment screenshot is not put on, write a little bit of their own attention to it

The prototype of the Start_kernel () function is:

This function initializes and defines some very important elements in the kernel during execution, and its execution involves almost all the modules of the kernel; first, it's init_task.

The definition of init_task in/LINUX-3.18.6/INIT/INIT_TASK.C

struct Task_struct init_task = Init_task (Init_task);

It is actually a task_struct, like the task_struct of the user process, Task_struct holds all the basic information of a process, such as process state, stack start address, process number PID, etc. init_task is special in its pid=0, Also known as the No. 0 process (which will eventually evolve into a idle process, which will be analyzed below), the significance of the NO. 0 process will continue to be analyzed here as long as you remember that it was created by the Start_kernel () function.

After the No. 0 process has been created, the Start_kernel () function continues to invoke various system modules for various initialization tasks such as:

Trap_init () is the relevant setting of the interrupt Vector mm_init () is the setting of the Memory management Sched_init () is the initialization of the dispatch module

As for the number of modules to call and its related functions are not carefully written here (in fact, because lost the original blog too late to rewrite AH), because I saw the same study of the course of the Lu Ying students of the blog on this issue analysis of the very detailed, so reprint it:

Above this picture reprint from Lu Ying schoolmate's blog: http://blog.csdn.net/myfather103/article/details/44337461

After performing the above work, this is the last line of code for the Start_kernel () function:

This invokes another very important function rest_init (), where it is located in/linux-3.18.6/init/main.c, with the following code:

393static noinline void __init_refok rest_init (void)
394{
395 int pid;
396
397 rcu_scheduler_starting ();
398/*
399  * We need to spawn init the IT obtains PID 1, however
* The  init task would end up Wanting to create kthreads, which, if
401  * We schedule it before we create Kthreadd, 'll OOPS.
402  * *
403 kernel_thread (Kernel_init, NULL, clone_fs);
404 Numa_default_policy ();
405 PID = Kernel_thread (Kthreadd, NULL, CLONE_FS | Clone_files);
406 Rcu_read_lock ();
407 Kthreadd_task = Find_task_by_pid_ns (PID, &init_pid_ns);
408 Rcu_read_unlock ();
409 complete (&kthreadd_done);
410
411  /* 412 * The boot idle thread must execute schedule ()
413  * At least once to get things M Oving:
414  * *
415 Init_idle_bootup_task (current);
416 schedule_preempt_disabled ();
417/* Call to Cpu_idle with preempt disabled *
/418 cpu_startup_entry (cpuhp_online);
419}

This line of code in the Rest_init () function:

Kernel_thread (Kernel_init, NULL, CLONE_FS);

Kernel_thread The source code for this function (/LINUX-3.18.6/KERNEL/FORK.C) is:

As you can see from the annotation section, the function of this function is to create a kernel thread (it should be a process). Otherwise where there will be PID ah hello. )
The first parameter in the Kernel_thread function is a function pointer, which means that the kernel now fork a new process to perform the Kernel_init function (this function in the lower kernel is named Init, which is changed to distinguish the init process so that it is kernel_ init); The INIT process was officially launched in the Kernel_init function (/LINUX-3.18.6/INIT/MAIN.C):

The Rest_init () function now launches another well-known Init process, the 1th process, about its importance as well as its functionality, followed by the following line of code for the Rest_init () function:

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

From the above analysis can know that this line of code folk a new process to execute the function Kthreadd, the source of this function here/LINUX-3.18.6/KERNEL/KTHREAD.C, the above line of code to achieve the function is to create a pid=2 kernel process, To manage some of the kernel's resources.

Rest_init () After we created the 1th, 2nd process, we ignored the rest (actually because we couldn't read it). ) to parse the last line of code directly:

Cpu_startup_entry (Cpuhp_online);

Called the Cpu_startup_entry (/linux-3.18.6/kernel/sched/idle.c) function, its source code:

256void Cpu_startup_entry (enum cpuhp_state State)
257{
258/  * 259 * This #ifdef needs to die, but it ' s too  Late in the "cycle to" * Make this  generic (arm and SH have never invoked the Canary
261  * init for the Non boot cpus!). would be fixed in 3.11
262 * *
263#ifdef config_x86
264/  * 265 * If we ' re the Non-boot CPU, not Hing set the stack canary up
266  * for us. The boot CPU already has it initialized but no harm
267  * In doing it again. This is a good place for updating it, as
268  * We wont ever return to this function (so invalid
22/>* Canaries already on the stack wont ever trigger).
270  * *
271 boot_init_stack_canary ();
272#endif
273 Arch_cpu_idle_prepare ();
274 Cpu_idle_loop ();
275}
276

And the Cpu_idle_loop is actually entering an infinite loop:

189static void Cpu_idle_loop (void) 190{191 while (1) {* * * 193 * If The arch has a polling bit, we maintain An invariant:194 * 195 * Our polling bit are clear if we ' re not scheduled (i.e. if 196 * Rq->curr!=  >idle). This means that, if Rq->idle has 197 * The polling bit set, then setting need_resched are 198 * Guaranteed to
Cause the CPU to reschedule.
199 * * 201 __current_set_polling ();
Tick_nohz_idle_enter (); 203 204 while (!need_resched ()) {205 Check_pgt_cache (); 206 RMB (); 207 The IF (cpu_is_offline
(smp_processor_id ()))
209 Arch_cpu_idle_dead ();
210 211 local_irq_disable ();
212 Arch_cpu_idle_enter ();
213 214/* 215 * In poll mode we reenable interrupts and spin. 216 * 217 * Also If we detected in the wakeup from idle 218 * path that tick broadcast CE expired 219 * For us, we don ' t want to go deEP Idle as we * know that the IPI are going to arrive right 221 * away 222 */223 if
(Cpu_idle_force_poll | | tick_check_broadcast_expired ()) 224 cpu_idle_poll ();
The Cpuidle_idle_call ();
228 arch_cpu_idle_exit (); 229} 230 231/* 232 * Since We fell out of the loop above, we know 233 * tif_need_resched must to be set,
Propagate it into 234 * preempt_need_resched. 235 * 236 * This are required because for polling idle loops we'll 237 * not have had a IPI to fold the s
Tate for us.
238 * * 239 preempt_set_need_resched ();
Tick_nohz_idle_exit ();
241 __current_clr_polling (); 242 243/* 244 * We promise to call sched_ttwu_pending and reschedule 245 * If need_resched are set while Pol  Ling is set.
That 246 * means the clearing polling needs to be visible 247 * before doing. 248 * * 249 Smp_mb__after_atomIC ();
251 sched_ttwu_pending ();
252 schedule_preempt_disabled (); 253} 254} 255

In other words, the No. 0 process was fold the 1th process and did the rest of the startup work, and finally "evolution" became the idle process.

At this point, the kernel started by the Start_kernel () function is up and running, and the system can now work on the "normal" task of accepting tasks.
Diagram of the above startup process:

Because it is just a simple experiment, for the complex kernel boot process can not only be summed up with a picture above, the reference in this article provides a large number of kernel boot-related processes, if you are interested to see

third, process No. 0 and number 1th

Process No. 0: "Ancestors" of all processes, a kernel process created "manually" by the Start_kernel () function during kernel startup, always in the kernel state. After the kernel started, the No. 0 process was completely "evolved" into a idle process, and the system began to loop infinitely
Process number 1th:

Add: The init executable file is available in the/sbin/init,/etc/init,/bin/init,/bin/sh

The above two picture screenshots from the course-provided documentation:
http://blog.csdn.net/hardy_2009/article/details/7383815
Http://teamtrac.ustcsz.edu.cn/raw-attachment/wiki/Linux2012/Linux-init-process-analyse.pdf Reference:

For a complex kernel startup process, it is not possible to use an article to explain it, although in the experiment to do some tracking analysis, but in order to complete this article or refer to a lot of excellent literature, many articles on the core of the start analysis than this article to be more thorough, in this to the author expressed thanks.

http://blog.csdn.net/titer1/article/details/44423031
Http://www.linuxidc.com/Linux/2014-10/108033p5.htm
http://itdreamerchen.com/tracks the Linux-kernel startup process from the source/
http://blog.csdn.net/hlchou/article/details/6663994
http://blog.csdn.net/myfather103/article/details/44337461

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.