1. 程式人生 > >Linux核心初始化步驟(四)

Linux核心初始化步驟(四)

kernel_init函式將完成裝置驅動的初始化,並呼叫init_post函式啟動使用者空間的init程序


static int __init kernel_init(void * unused)
{
	/*
	 * Wait until kthreadd is all set-up.
	 */
	wait_for_completion(&kthreadd_done);
	/*
	 * init can allocate pages on any node
	 */
	set_mems_allowed(node_states[N_HIGH_MEMORY]);
	/*
	 * init can run on any cpu.
	 */
	set_cpus_allowed_ptr(current, cpu_all_mask);

	cad_pid = task_pid(current);

	smp_prepare_cpus(setup_max_cpus);

	do_pre_smp_initcalls();
	lockup_detector_init();

	smp_init();
	sched_init_smp();
	//以上程式碼是在SMP系統做準備,啟用所有CPU,並開始SMP系統的排程

	/*此時與體系結構相關的部分已經初始化完成,CPU已經執行起來了,記憶體和程序管理也
	開始工作了,從現在開始呼叫do_basic_setup函式初始化裝置,完成外設及其驅動程式
	(直接編譯進核心的模組)的載入和初始化*/
	do_basic_setup();

	/* Open the /dev/console on the rootfs, this should never fail */
	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
		printk(KERN_WARNING "Warning: unable to open an initial console.\n");
	
	/*複製兩次標準輸入(0)的檔案描述符(它是上面開啟的/dev/console,也就是系統控制檯);
	一個作為標準輸出(1)
	一個作為標準出錯(2)
	*/
	(void) sys_dup(0);
	(void) sys_dup(0);
	/*
	 * check if there is an early userspace init.  If yes, let it do all
	 * the work
	 */
	/*檢查是否有早期使用者空間的init程式,如果有,讓其執行*/
	if (!ramdisk_execute_command)
		ramdisk_execute_command = "/init";

	if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) {
		ramdisk_execute_command = NULL;
		prepare_namespace();
	}

	/*
	 * Ok, we have completed the initial bootup, and
	 * we're essentially up and running. Get rid of the
	 * initmem segments and start the user-mode stuff..
	 */

	init_post();
	return 0;
}