linux kernel启动流程

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前面是正点原子部分内容,后面结合自己的一些代码追踪,主要是搞清楚module_init,xxx_init这些是如何在Linux kernel启动时被拉起来的。

一、start_kernel 函数

该函数在init/main.c

asmlinkage __visible void __init start_kernel(void)
{
 char *command_line;
 char *after_dashes;
 lockdep_init(); /* lockdep 是死锁检测模块,此函数会初始化两个 hash 表。此函数要求尽可能早的执行! */
set_task_stack_end_magic(&init_task);/* 设置任务栈结束魔术数,
*用于栈溢出检测
*/
 smp_setup_processor_id(); /* 跟 SMP 有关(多核处理器),设置处理器 ID。
 * 有很多资料说 ARM 架构下此函数为空函数,那是因
 * 为他们用的老版本 Linux,而那时候 ARM 还没有多
 * 核处理器。
*/
 debug_objects_early_init(); /* 做一些和 debug 有关的初始化 */
 boot_init_stack_canary(); /* 栈溢出检测初始化 */
 cgroup_init_early(); /* cgroup 初始化,cgroup 用于控制 Linux 系统资源*/
 local_irq_disable(); /* 关闭当前 CPU 中断 */
 early_boot_irqs_disabled = true;
 /*
 * 中断关闭期间做一些重要的操作,然后打开中断
 */
 boot_cpu_init(); /* 跟 CPU 有关的初始化 */
 page_address_init(); /* 页地址相关的初始化 */
 pr_notice("%s", linux_banner);/* 打印 Linux 版本号、编译时间等信息 */
 setup_arch(&command_line); /* 架构相关的初始化,此函数会解析传递进来的
 * ATAGS 或者设备树(DTB)文件。会根据设备树里面
 * 的 model 和 compatible 这两个属性值来查找
 * Linux 是否支持这个单板。此函数也会获取设备树
 * 中 chosen 节点下的 bootargs 属性值来得到命令
 * 行参数,也就是 uboot 中的 bootargs 环境变量的
* 值,获取到的命令行参数会保存到
*command_line 中。
 */
 mm_init_cpumask(&init_mm); /* 看名字,应该是和内存有关的初始化 */
 setup_command_line(command_line); /* 好像是存储命令行参数 */
 setup_nr_cpu_ids(); /* 如果只是 SMP(多核 CPU)的话,此函数用于获取
 * CPU 核心数量,CPU 数量保存在变量
 * nr_cpu_ids 中。
*/
 setup_per_cpu_areas(); /* 在 SMP 系统中有用,设置每个 CPU 的 per-cpu 数据 */
 smp_prepare_boot_cpu(); 
 build_all_zonelists(NULL, NULL); /* 建立系统内存页区(zone)链表 */
 page_alloc_init(); /* 处理用于热插拔 CPU 的页 */
/* 打印命令行信息 */ 
pr_notice("Kernel command line: %s\n", boot_command_line);
 parse_early_param(); /* 解析命令行中的 console 参数 */
 after_dashes = parse_args("Booting kernel",
 static_command_line, __start___param,
 __stop___param - __start___param,
 -1, -1, &unknown_bootoption);
 if (!IS_ERR_OR_NULL(after_dashes))
 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
 set_init_arg);
 jump_label_init();
 setup_log_buf(0); /* 设置 log 使用的缓冲区*/
 pidhash_init(); /* 构建 PID 哈希表,Linux 中每个进程都有一个 ID,
 * 这个 ID 叫做 PID。通过构建哈希表可以快速搜索进程
 * 信息结构体。
 */
vfs_caches_init_early(); /* 预先初始化 vfs(虚拟文件系统)的目录项和
* 索引节点缓存
*/
 sort_main_extable(); /* 定义内核异常列表 */
 trap_init(); /* 完成对系统保留中断向量的初始化 */
 mm_init(); /* 内存管理初始化 */
 sched_init(); /* 初始化调度器,主要是初始化一些结构体 */
 preempt_disable(); /* 关闭优先级抢占 */
 if (WARN(!irqs_disabled(), /* 检查中断是否关闭,如果没有的话就关闭中断 */
 "Interrupts were enabled *very* early, fixing it\n"))
 local_irq_disable();
 idr_init_cache(); /* IDR 初始化,IDR 是 Linux 内核的整数管理机
 * 制,也就是将一个整数 ID 与一个指针关联起来。
 */
 rcu_init(); /* 初始化 RCU,RCU 全称为 Read Copy Update(读-拷贝修改) */
 trace_init(); /* 跟踪调试相关初始化 */
 context_tracking_init(); 
 radix_tree_init(); /* 基数树相关数据结构初始化 */
 early_irq_init(); /* 初始中断相关初始化,主要是注册 irq_desc 结构体变
 * 量,因为 Linux 内核使用 irq_desc 来描述一个中断。
 */
 init_IRQ(); /* 中断初始化 */
 tick_init(); /* tick 初始化 */
 rcu_init_nohz(); 
 init_timers(); /* 初始化定时器 */
 hrtimers_init(); /* 初始化高精度定时器 */
 softirq_init(); /* 软中断初始化 */
 timekeeping_init(); 
 time_init(); /* 初始化系统时间 */
 sched_clock_postinit(); 
 perf_event_init();
 profile_init();
 call_function_init();
 WARN(!irqs_disabled(), "Interrupts were enabled early\n");
 early_boot_irqs_disabled = false;
 local_irq_enable(); /* 使能中断 */
 kmem_cache_init_late(); /* slab 初始化,slab 是 Linux 内存分配器 */
 console_init(); /* 初始化控制台,之前 printk 打印的信息都存放
 * 缓冲区中,并没有打印出来。只有调用此函数
 * 初始化控制台以后才能在控制台上打印信息。
 */
 if (panic_later) 
 panic("Too many boot %s vars at `%s'", panic_later,
 panic_param);
 lockdep_info();/* 如果定义了宏 CONFIG_LOCKDEP,那么此函数打印一些信息。*/
 locking_selftest() /* 锁自测 */ 
 ......
 page_ext_init(); 
 debug_objects_mem_init();
 kmemleak_init(); /* kmemleak 初始化,kmemleak 用于检查内存泄漏 */
 setup_per_cpu_pageset(); 
 numa_policy_init();
 if (late_time_init)
 late_time_init();
 sched_clock_init(); 
 calibrate_delay(); /* 测定 BogoMIPS 值,可以通过 BogoMIPS 来判断 CPU 的性能
 * BogoMIPS 设置越大,说明 CPU 性能越好。
 */
 pidmap_init(); /* PID 位图初始化 */
 anon_vma_init(); /* 生成 anon_vma slab 缓存 */ 
 acpi_early_init();
 ......
 thread_info_cache_init();

cred_init(); /* 为对象的每个用于赋予资格(凭证) */
 fork_init(); /* 初始化一些结构体以使用 fork 函数 */
 proc_caches_init(); /* 给各种资源管理结构分配缓存 */
 buffer_init(); /* 初始化缓冲缓存 */
 key_init(); /* 初始化密钥 */
 security_init(); /* 安全相关初始化 */
 dbg_late_init();
 vfs_caches_init(totalram_pages); /* 为 VFS 创建缓存 */
 signals_init(); /* 初始化信号 */
 page_writeback_init(); /* 页回写初始化 */
 proc_root_init(); /* 注册并挂载 proc 文件系统 */
 nsfs_init(); 
 cpuset_init(); /* 初始化 cpuset,cpuset 是将 CPU 和内存资源以逻辑性
 * 和层次性集成的一种机制,是 cgroup 使用的子系统之一
 */
 cgroup_init(); /* 初始化 cgroup */
 taskstats_init_early(); /* 进程状态初始化 */
 delayacct_init();
 check_bugs(); /* 检查写缓冲一致性 */
 acpi_subsystem_init(); 
 sfi_init_late();
 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
 efi_late_init();
 efi_free_boot_services();
 }
 ftrace_init();
 rest_init(); /* rest_init 函数 */
}

二、rest_init 函数

rest_init 函数定义在文件 init/main.c 中

static noinline void __init_refok rest_init(void) 
{
	int pid;

	rcu_scheduler_starting();
	smpboot_thread_init();
	/*
	* We need to spawn init first so that it obtains pid 1, however
	* the init task will end up wanting to create kthreads, which, 
	* if we schedule it before we create kthreadd, will OOPS.
	*/
	//主要关注kernel_init和kthreadd
	kernel_thread(kernel_init, NULL, CLONE_FS);
	numa_default_policy();
	pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES);
	rcu_read_lock();
	kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);
	rcu_read_unlock();
	complete(&kthreadd_done);
	 /*
	* The boot idle thread must execute schedule()
	* at least once to get things moving:
	*/
	init_idle_bootup_task(current);
	schedule_preempt_disabled();
	/* Call into cpu_idle with preempt disabled */
	cpu_startup_entry(CPUHP_ONLINE);
}

查看kernel_init

static int __ref kernel_init(void *unused)
{
	int ret;
	
	kernel_init_freeable(); /* init 进程的一些其他初始化工作 */
	/* need to finish all async __init code before freeing the memory */
	async_synchronize_full(); /* 等待所有的异步调用执行完成 */
	free_initmem(); /* 释放 init 段内存 */
	mark_rodata_ro();
	system_state = SYSTEM_RUNNING; /* 标记系统正在运行 */
	numa_default_policy();
	
	flush_delayed_fput();
	
	if (ramdisk_execute_command) {
	ret = run_init_process(ramdisk_execute_command);
	if (!ret)
	return 0;
	pr_err("Failed to execute %s (error %d)\n",
	ramdisk_execute_command, ret);
	}
	
	/*
	* We try each of these until one succeeds.
	*
	* The Bourne shell can be used instead of init if we are
	* trying to recover a really broken machine.
	*/
	if (execute_command) {
	ret = run_init_process(execute_command);
	if (!ret)
	return 0;
	panic("Requested init %s failed (error %d).",
	execute_command, ret);
	}
	if (!try_to_run_init_process("/sbin/init") ||
	!try_to_run_init_process("/etc/init") ||
	!try_to_run_init_process("/bin/init") ||
	!try_to_run_init_process("/bin/sh"))
	return 0;
	
	panic("No working init found. Try passing init= option to kernel. "
	"See Linux Documentation/init.txt for guidance.");
}

kernel_init_freeable

static noinline void __init kernel_init_freeable(void)
{
	/*
	* Wait until kthreadd is all set-up.
	*/
	wait_for_completion(&kthreadd_done);/* 等待 kthreadd 进程准备就绪 */
	...
	
	smp_init(); /* SMP 初始化 */
	sched_init_smp(); /* 多核(SMP)调度初始化 */
	
	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) <
	
	pr_err("Warning: unable to open an initial console.\n");
	
	(void) sys_dup(0);
	(void) sys_dup(0);
	/*
	* check if there is an early userspace init. If yes, let it do 
	* all the work
	*/
	
	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..
	*
	* rootfs is available now, try loading the public keys
	* and default modules
	*/
	
	integrity_load_keys();
	load_default_modules();
}

do_basic_setup

/*
 * Ok, the machine is now initialized. None of the devices
 * have been touched yet, but the CPU subsystem is up and
 * running, and memory and process management works.
 *
 * Now we can finally start doing some real work..
 */
static void __init do_basic_setup(void)
{
	cpuset_init_smp();
	driver_init();
	init_irq_proc();
	do_ctors();
	do_initcalls();		//这个单独列一个部分来讲解
}

driver_init做一些初始化,后续很多驱动所需要的总线、设备、class、platform_bus
等初始化

/**
 * driver_init - initialize driver model.
 *
 * Call the driver model init functions to initialize their
 * subsystems. Called early from init/main.c.
 */
void __init driver_init(void)
{
	/* These are the core pieces */
	bdi_init(&noop_backing_dev_info);
	devtmpfs_init();
	devices_init();
	buses_init();
	classes_init();
	firmware_init();
	hypervisor_init();

	/* These are also core pieces, but must come after the
	 * core core pieces.
	 */
	of_core_init();
	platform_bus_init();
	auxiliary_bus_init();
	cpu_dev_init();
	memory_dev_init();
	container_dev_init();
}

三、do_initcalls函数

这个就是按启动级别来启动那些函数,具体的启动级别见如下
在这里插入图片描述

static void __init do_initcalls(void)
{
	int level;
	size_t len = strlen(saved_command_line) + 1;
	char *command_line;

	command_line = kzalloc(len, GFP_KERNEL);
	if (!command_line)
		panic("%s: Failed to allocate %zu bytes\n", __func__, len);

	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
		/* Parser modifies command_line, restore it each time */
		strcpy(command_line, saved_command_line);
		do_initcall_level(level, command_line);
	}

	kfree(command_line);
}

do_initcall_level

static void __init do_initcall_level(int level, char *command_line)
{
	initcall_entry_t *fn;

	parse_args(initcall_level_names[level],
		   command_line, __start___param,
		   __stop___param - __start___param,
		   level, level,
		   NULL, ignore_unknown_bootoption);

	trace_initcall_level(initcall_level_names[level]);
	for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
		do_one_initcall(initcall_from_entry(fn));
}

do_one_initcall

int __init_or_module do_one_initcall(initcall_t fn)
{
	int count = preempt_count();
	char msgbuf[64];
	int ret;

	if (initcall_blacklisted(fn))
		return -EPERM;

	do_trace_initcall_start(fn);
	ret = fn();						//这个fn就是各种各样的xxx_init函数
	do_trace_initcall_finish(fn, ret);

	msgbuf[0] = 0;

	if (preempt_count() != count) {
		sprintf(msgbuf, "preemption imbalance ");
		preempt_count_set(count);
	}
	if (irqs_disabled()) {
		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
		local_irq_enable();
	}
	WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);

	add_latent_entropy();
	return ret;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值