2020.03.25-Android Q linux kernel initコードウォークスルー

8448 ワード

Android Q linux kernel initコードウォークスルー
Android Qシステムが起動するとまずubootに入りブートし、bootmodeに従って相応の起動モードを行い、bootまたはrecovery modeに入ります.カーネルが起動すると、まずkernel/kernel 4に入ります.14/arch/arm64/kernel/head.Sは初期化を行い、kernel 4にジャンプする.14/init/main.c下のstart_カーネルが起動するC言語のエントリで、主にメモリ、io、割り込み、プロセス、タスクスケジューリングなどの初期化を含み、最後にrest_を呼び出すkernel関数Init()関数は、selinux、initを含む他の初期化を完了する.rc、serviceなどの初期化.
395  static noinline void __ref rest_init(void)
396  {
397  	struct task_struct *tsk;
398  	int pid;
399  
400  	rcu_scheduler_starting();
401  	/*
402  	 * We need to spawn init first so that it obtains pid 1, however
403  	 * the init task will end up wanting to create kthreads, which, if
404  	 * we schedule it before we create kthreadd, will OOPS.
405  	 */
406  	pid = kernel_thread(kernel_init, NULL, CLONE_FS);
  		  kernel synchronize init mem system_state
  		//    rcu ??
407  	/*
408  	 * Pin init on the boot CPU. Task migration is not properly working
409  	 * until sched_init_smp() has been run. It will set the allowed
410  	 * CPUs for init to the non isolated CPUs.
411  	 */
412  	rcu_read_lock();
413  	tsk = find_task_by_pid_ns(pid, &init_pid_ns);
414  	set_cpus_allowed_ptr(tsk, cpumask_of(smp_processor_id()));
415  	rcu_read_unlock();
416     //  memory    
417  	numa_default_policy();
418  	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
419  	rcu_read_lock();
420  	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
421  	rcu_read_unlock();
422  
423  	/*
424  	 * Enable might_sleep() and smp_processor_id() checks.
425  	 * They cannot be enabled earlier because with CONFIG_PRREMPT=y
426  	 * kernel_thread() would trigger might_sleep() splats. With
427  	 * CONFIG_PREEMPT_VOLUNTARY=y the init task might have scheduled
428  	 * already, but it's stuck on the kthreadd_done completion.
429  	 */
430  	system_state = SYSTEM_SCHEDULING;
431  
432  	complete(&kthreadd_done);
433  
434  	/*
435  	 * The boot idle thread must execute schedule()
436  	 * at least once to get things moving:
437  	 */
438  	schedule_preempt_disabled();
439  	/* Call into cpu_idle with preempt disabled */
440  	cpu_startup_entry(CPUHP_ONLINE);
441  }

pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);Initプログラムの実行
1044  	if (!try_to_run_init_process("/sbin/init") ||
1045  	    !try_to_run_init_process("/etc/init") ||
1046  	    !try_to_run_init_process("/bin/init") ||
1047  	    !try_to_run_init_process("/bin/sh"))