Android Q linux kernel init 代码走读
Android Q 系统启动后首先进入uboot进行引导,根据bootmode 进行相应的启动模式,进入boot或者recovery mode。
在内核启动时,首先进入kernel/kernel4.14/arch/arm64/kernel/head.S 进行初始化,然后跳转到kernel4.14/init/main.c 下的start_kernel 函数,该函数是内核启动的C语言入口,主要包括内存、io、中断、进程、任务调度等初始化,最后调用rest_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"))