linux 6.6.10
kernel 启动调用注解 init/main.c
asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protector
void start_kernel(void)
{
char *command_line;
char *after_dashes;
set_task_stack_end_magic(&init_task); /* 设置任务栈结束魔术数 用于栈溢出检测 */
smp_setup_processor_id(); /* 和SMP有关(多核处理器),设置处理器ID */
debug_objects_early_init(); /* 做一些和debug 有关的初始化 */
init_vmlinux_build_id(); /* 初始化内核版本信息和编译时间 */
cgroup_init_early(); /* cgroup 初始化,cgroup 用于控制 Linux 系统资源 */
local_irq_disable(); /* 关闭当前cpu 中断 */
early_boot_irqs_disabled = true; /* 标记早期引导中断已禁用, include/linux/kernel.h */
/*
* Interrupts are still disabled. Do necessary setups, then
* enable them.
*/
boot_cpu_init(); /* 跟CPU有关的初始化, 初始化引导 CPU */
page_address_init(); /* 页地址相关的初始化 */
pr_notice("%s", linux_banner); /* 打印 Linux 版本号、编译时间等信息 */
early_security_init(); /* 初始化安全相关配置 */
setup_arch(&command_line);/* 架构相关的初始化,此函数会解析传递进来的
* ATAGS 或者设备树(DTB)文件。会根据设备树里面
* 的 model 和 compatible 这两个属性值来查找
* Linux 是否支持这个单板。此函数也会获取设备树
* 中 chosen 节点下的 bootargs 属性值来得到命令
* 行参数,也就是 uboot 中的 bootargs 环境变量的
* 值,获取到的命令行参数会保存到 command_line 中。
*/
setup_boot_config(); /* 设置引导配置 */
setup_command_line(command_line); /* 解析命令行参数 */
setup_nr_cpu_ids(); /* 如果只是 SMP(多核 CPU)的话,此函数用于获取
* CPU 核心数量,CPU 数量保存在变量 nr_cpu_ids 中。
*/
setup_per_cpu_areas(); /* 在 SMP 系统中有用,设置每个 CPU 的内存区域 */
smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks 准备 boot CPU 的架构特定设置 */
boot_cpu_hotplug_init();/* 初始化引导 CPU 热插拔支持 */
pr_notice("Kernel command line: %s\n", saved_command_line);
/* parameters may set static keys */
jump_label_init(); /* 初始化跳跃标签 */
parse_early_param();/* 解析命令行中的 console 参数 */
after_dashes = parse_args("Booting kernel",
static_command_line, __start___param,
__stop___param - __start___param,
-1, -1, NULL, &unknown_bootoption);
print_unknown_bootoptions();/* 打印未识别的引导选项 */
if (!IS_ERR_OR_NULL(after_dashes)) /* 如果解析成功 */
parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
NULL, set_init_arg); /* 设置初始化参数 */
if (extra_init_args) /* 如果有额外的初始化参数 */
parse_args("Setting extra init args", extra_init_args,
NULL, 0, -1, -1, NULL, set_init_arg); /* 设置额外初始化参数 */
/* Architectural and non-timekeeping rng init, before allocator init */
random_init_early(command_line); /* 进行早期随机数生成器初始化 */
/*
* These use large bootmem allocations and must precede
* initalization of page allocator
*/
setup_log_buf(0);/* 设置 log 使用的缓冲区*/
vfs_caches_init_early();/* 预先初始化 vfs(虚拟文件系统)的目录项和索引节点缓存 */
sort_main_extable();/* 排序主异常表 */
trap_init();/* 完成对系统保留中断向量的初始化, 初始化异常处理 */
mm_core_init(); /* 初始化内存管理核心 */
poking_init(); /* 初始化 poking 机制 */
ftrace_init(); /* 初始化跟踪功能 */
/* trace_printk can be enabled here */
early_trace_init(); /* 进行早期跟踪初始化 */
/*
* Set up the scheduler prior starting any interrupts (such as the
* timer interrupt). Full topology setup happens at smp_init()
* time - but meanwhile we still have a functioning scheduler.
*/
sched_init();/* 初始化调度器,主要是初始化一些结构体 */
if (WARN(!irqs_disabled(), /* 检查中断是否意外启用 */
"Interrupts were enabled *very* early, fixing it\n"))
local_irq_disable(); /* 如果启用,则禁用本地中断 */
radix_tree_init();/* 基数树相关数据结构初始化 */
maple_tree_init(); /* 初始化maple tree,这是Linux新引入的数据结构 */
/*
* Set up housekeeping before setting up workqueues to allow the unbound
* workqueue to take non-housekeeping into account.
*/
housekeeping_init();/* 初始化系统管理相关内容 */
/*
* Allow workqueue creation and work item queueing/cancelling
* early. Work item execution depends on kthreads and starts after
* workqueue_init().
*/
workqueue_init_early(); /* 初始化工作队列 */
rcu_init();/* 初始化 RCU,RCU 全称为 Read Copy Update(读-拷贝修改) */
/* Trace events are available after this */
trace_init();/* 跟踪调试相关初始化 */
if (initcall_debug) /* 如果启用初始化调用调试 */
initcall_debug_enable(); /* 启用调试 */
context_tracking_init(); /* 初始化上下文跟踪 */
/* init some links before init_ISA_irqs() */
early_irq_init();/* 初始中断相关初始化,主要是注册 irq_desc 结构体变
* 量,因为 Linux 内核使用 irq_desc 来描述一个中断。
*/
init_IRQ();/* 中断初始化 */
tick_init();/* tick(定时器) 初始化 */
rcu_init_nohz();/* 初始化无时钟 RCU */
init_timers();/* 初始化定时器 */
srcu_init();
hrtimers_init();/* 初始化高精度定时器 */
softirq_init();/* 软中断初始化 */
timekeeping_init();
time_init();/* 初始化系统时间 */
/* This must be after timekeeping is initialized */
random_init();
/* These make use of the fully initialized rng */
kfence_init(); /* 初始化内存保护机制 */
boot_init_stack_canary();/* 栈溢出检测初始化 */
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();/* 延迟初始化内存缓存 */
/*
* HACK ALERT! This is early. We're enabling the console before
* we've done PCI setups etc, and console_init() must be aware of
* this. But we do want output early, in case something goes wrong.
*/
console_init();/* 初始化控制台,之前 printk 打印的信息都存放
* 缓冲区中,并没有打印出来。只有调用此函数
* 初始化控制台以后才能在控制台上打印信息。
*/
if (panic_later) /* 如果有紧急情况 */
panic("Too many boot %s vars at `%s'", panic_later,
panic_param); /* 触发内核恐慌 */
lockdep_init(); /* 死锁检测模块, 此函数会初始化两个hash表。此函数要求尽可能早的执行 */
/*
* Need to run this when irqs are enabled, because it wants
* to self-test [hard/soft]-irqs on/off lock inversion bugs
* too:
*/
locking_selftest();/* 锁自测 */
#ifdef CONFIG_BLK_DEV_INITRD
if (initrd_start && !initrd_below_start_ok && /* 检查 initrd */
page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
page_to_pfn(virt_to_page((void *)initrd_start)),
min_low_pfn);
initrd_start = 0; /* 如果被覆盖则禁用 initrd */
}
#endif
setup_per_cpu_pageset(); /* 设置每个 CPU 的页面集合 */
numa_policy_init(); /* 初始化 NUMA 策略 */
acpi_early_init(); /* 早期初始化 ACPI */
if (late_time_init) /* 如果有延迟时间初始化 */
late_time_init();/* 执行延迟时间初始化 */
sched_clock_init(); /* 初始化调度时钟 */
calibrate_delay();/* 校准延迟 */
arch_cpu_finalize_init();/* 完成架构相关的 CPU 初始化 */
pid_idr_init();/* 初始化 PID IDR */
anon_vma_init();/* 初始化匿名虚拟内存 */
#ifdef CONFIG_X86
if (efi_enabled(EFI_RUNTIME_SERVICES)) /* 检查 EFI 运行时服务是否启用 */
efi_enter_virtual_mode(); /* 进入 EFI 虚拟模式 */
#endif
thread_stack_cache_init(); /* 初始化线程栈缓存 */
cred_init(); /* 初始化凭证管理 */
fork_init(); /* 初始化一些结构体以使用 fork 函数 */
proc_caches_init(); /* 给各种资源管理结构分配缓存,初始化进程缓存 */
uts_ns_init(); /* 初始化 UTS 命名空间 */
key_init(); /* 初始化密钥管理 */
security_init(); /* 初始化安全管理 */
dbg_late_init(); /* 延迟初始化调试相关内容 */
net_ns_init(); /* 初始化网络命名空间 */
vfs_caches_init(); /* 为 VFS 创建缓存, 初始化虚拟文件系统缓存 */
pagecache_init(); /* 初始化页面缓存 */
signals_init(); /* 初始化信号 */
seq_file_init(); /* 初始化序列文件支持 */
proc_root_init();/* 注册并挂载 proc 文件系统 */
nsfs_init();
cpuset_init(); /* 初始化 cpuset,cpuset 是将 CPU 和内存资源以逻辑性
* 和层次性集成的一种机制,是 cgroup 使用的子系统之一
*/
cgroup_init(); /* 初始化 cgroup(控制组) */
taskstats_init_early(); /* 进程状态初始化 */
delayacct_init(); /* 初始化延迟账户 */
acpi_subsystem_init(); /* 初始化 ACPI 子系统 */
arch_post_acpi_subsys_init(); /* 执行架构特定的 ACPI 后初始化 */
kcsan_init(); /* 初始化 KCSAN(内存竞争检测) */
/* Do the rest non-__init'ed, we're now alive */
arch_call_rest_init();//调用 rest_init(); 执行后续初始化,开始内核主循环
/*
* Avoid stack canaries in callers of boot_init_stack_canary for gcc-10
* and older.
*/
#if !__has_attribute(__no_stack_protector__)
prevent_tail_call_optimization();
#endif
}