页面加载中,请稍候
来源:嵌入式Linux系统开发发布时间:2023-01-021698浏览
询问 AI上次我们写过了 Linux 启动详细流程,这次单独解析 start_kernel 函数。
原来的文章如下:
如下请参考注释:
Linux kernel-6.1/init/main.c
asmlinkage__visiblevoid__init__no_sanitize_addressstart_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;
/*
*Interruptsarestilldisabled.Donecessarysetups,then
*enablethem.
*中断关闭期间做一些重要的操作,然后打开中断
*/
boot_cpu_init();/*跟CPU有关的初始化*/
page_address_init();/*页地址相关的初始化*/
pr_notice("%s",linux_banner);/*打印Linux版本号、编译时间等信息*/
early_security_init();
/*系统架构相关的初始化,此函数会解析传递进来的
* ATAGS 或者设备树(DTB)文件。会根据设备树里面
*的model和compatible这两个属性值来查找
* Linux 是否支持这个单板。此函数也会获取设备树
*中chosen节点下的bootargs属性值来得到命令
*行参数,也就是uboot中的bootargs环境变量的
*值,获取到的命令行参数会保存到command_line中
*/
setup_arch(command_line);
setup_boot_config();
setup_command_line(command_line);/*存储命令行参数*/
/*如果只是SMP(多核CPU)的话,此函数用于获取
* CPU 核心数量,CPU 数量保存在变量 nr_cpu_ids 中。
*/
setup_nr_cpu_ids();
setup_per_cpu_areas();/*在SMP系统中有用,设置每个CPU的per-cpu数据*/
smp_prepare_boot_cpu();/*arch-specificboot-cpuhooks*/
boot_cpu_hotplug_init();
build_all_zonelists(NULL);/*建立系统内存页区(zone)链表*/
page_alloc_init();/*处理用于热插拔CPU的页*/
/*打印命令行信息*/
pr_notice("Kernelcommandline:%s\n",saved_command_line);
/*parametersmaysetstatickeys*/
jump_label_init();
parse_early_param();/*解析命令行中的console参数*/
after_dashes=parse_args("Bootingkernel",
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("Settinginitargs",after_dashes,NULL,0,-1,-1,
NULL,set_init_arg);
if(extra_init_args)
parse_args("Settingextrainitargs",extra_init_args,
NULL,0,-1,-1,NULL,set_init_arg);
/*Architecturalandnon-timekeepingrnginit,beforeallocatorinit*/
random_init_early(command_line);
/*
*Theseuselargebootmemallocationsandmustprecede
*kmem_cache_init()
*/
setup_log_buf(0);/*设置log使用的缓冲区*/
vfs_caches_init_early();/*预先初始化vfs(虚拟文件系统)的目录项和索引节点缓存*/
sort_main_extable();/*定义内核异常列表*/
trap_init();/*完成对系统保留中断向量的初始化*/
mm_init();/*内存管理初始化*/
ftrace_init();
/*trace_printkcanbeenabledhere*/
early_trace_init();
/*
*Setuptheschedulerpriorstartinganyinterrupts(suchasthe
*timerinterrupt).Fulltopologysetuphappensatsmp_init()
*time-butmeanwhilewestillhaveafunctioningscheduler.
*/
sched_init();/*初始化调度器,主要是初始化一些结构体*/
if(WARN(!irqs_disabled(),
"Interruptswereenabled*very*early,fixingit\n"))
local_irq_disable();/*检查中断是否关闭,如果没有的话就关闭中断*/
radix_tree_init();/*基数树相关数据结构初始化*/
maple_tree_init();
/*
*Setuphousekeepingbeforesettingupworkqueuestoallowtheunbound
*workqueuetotakenon-housekeepingintoaccount.
*/
housekeeping_init();
/*
*Allowworkqueuecreationandworkitemqueueing/cancelling
*early.Workitemexecutiondependsonkthreadsandstartsafter
*workqueue_init().
*/
workqueue_init_early();
rcu_init();/*初始化RCU,RCU全称为ReadCopyUpdate(读-拷贝修改)*/
/*Traceeventsareavailableafterthis*/
trace_init();/*跟踪调试相关初始化*/
if(initcall_debug)
initcall_debug_enable();
context_tracking_init();
/*initsomelinksbeforeinit_ISA_irqs()*/
/*初始中断相关初始化,主要是注册irq_desc结构体变
*量,因为 Linux 内核使用 irq_desc 来描述一个中断。
*/
early_irq_init();
init_IRQ();/*中断初始化*/
tick_init();/*tick初始化*/
rcu_init_nohz();
init_timers();/*初始化定时器*/
srcu_init();
hrtimers_init();/*初始化高精度定时器*/
softirq_init();/*软中断初始化*/
timekeeping_init();
time_init();/*初始化系统时间*/
/*Thismustbeaftertimekeepingisinitialized*/
random_init();
/*Thesemakeuseofthefullyinitializedrng*/
kfence_init();
boot_init_stack_canary();
perf_event_init();
profile_init();
call_function_init();
WARN(!irqs_disabled(),"Interruptswereenabledearly\n");
early_boot_irqs_disabled=false;
local_irq_enable();/*使能中断*/
kmem_cache_init_late();/*slab初始化,slab是Linux内存分配器*/
/*
*HACKALERT!Thisisearly.We'reenablingtheconsolebefore
*we'vedonePCIsetupsetc,andconsole_init()mustbeawareof
*this.Butwedowantoutputearly,incasesomethinggoeswrong.
*/
/*初始化控制台,之前printk打印的信息都存放
*缓冲区中,并没有打印出来。只有调用此函数
*初始化控制台以后才能在控制台上打印信息。
*/
console_init();
if(panic_later)
panic("Toomanyboot%svarsat`%s'",panic_later,
panic_param);
lockdep_init();
/*
*Needtorunthiswhenirqsareenabled,becauseitwants
*toself-test[hard/soft]-irqson/offlockinversionbugs
*too:
*/
locking_selftest();/*锁自测*/
/*
*ThisneedstobecalledbeforeanydevicesperformDMA
*operationsthatmightusetheSWIOTLBbouncebuffers.Itwill
*markthebouncebuffersasdecryptedsothattheirusagewill
*notcause"plain-text"datatobedecryptedwhenaccessed.
*/
mem_encrypt_init();
#ifdefCONFIG_BLK_DEV_INITRD
if(initrd_start!initrd_below_start_ok
page_to_pfn(virt_to_page((void*)initrd_start))<min_low_pfn){
pr_crit("initrdoverwritten(0x%08lx<0x%08lx)-disablingit.\n",
page_to_pfn(virt_to_page((void*)initrd_start)),
min_low_pfn);
initrd_start=0;
}
#endif
setup_per_cpu_pageset();
numa_policy_init();
acpi_early_init();
if(late_time_init)
late_time_init();
sched_clock_init();
/*测定BogoMIPS值,可以通过BogoMIPS来判断CPU的性能
* BogoMIPS 设置越大,说明 CPU 性能越好。
*/
calibrate_delay();
pid_idr_init();
anon_vma_init();/*生成anon_vmaslab缓存*/
#ifdefCONFIG_X86
if(efi_enabled(EFI_RUNTIME_SERVICES))
efi_enter_virtual_mode();
#endif
thread_stack_cache_init();
cred_init();/*为对象的每个用于赋予资格(凭证)*/
fork_init();/*初始化一些结构体以使用fork函数*/
proc_caches_init();/*给各种资源管理结构分配缓存*/
uts_ns_init();
key_init();/*初始化密钥*/
security_init();/*安全相关初始化*/
dbg_late_init();
net_ns_init();
vfs_caches_init();/*虚拟文件系统缓存初始化*/
pagecache_init();
signals_init();/*初始化信号*/
seq_file_init();
proc_root_init();/*注册并挂载proc文件系统*/
nsfs_init();
/*初始化cpuset,cpuset是将CPU和内存资源以逻辑性
*和层次性集成的一种机制,是cgroup使用的子系统之一
*/
cpuset_init();
cgroup_init();/*初始化cgroup*/
taskstats_init_early();/*进程状态初始化*/
delayacct_init();
poking_init();
check_bugs();/*检查写缓冲一致性*/
acpi_subsystem_init();
arch_post_acpi_subsys_init();
kcsan_init();
/*Dotherestnon-__init'ed,we'renowalive*/
/*调用rest_init函数*/
/*创建init、kthread、idle线程*/
arch_call_rest_init();
prevent_tail_call_optimization();
}
新闻来源:嵌入式Linux系统开发,文中所述为作者独立观点,不代表icspec立场。更多精彩资讯请下载icspec App。如对本稿件有异议,请联系微信客服specltkj。
暂无评论哦,快来评论一下吧!
2026-06-17

2026-06-26

2026-07-16