Linux内核启动流程之start_kernel问题

Linux内核启动流程之start_kernel问题

 更新时间:2024年01月11日 10:22:11   作者:嵌入式Linux系统开发  
这篇文章主要介绍了Linux内核启动流程之start_kernel问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

目录
  • Linux内核启动流程start_kernel
    • 参考注释
  • 总结

    Linux内核启动流程start_kernel

    参考注释

    目录:/init/main.c

    asmlinkage __visible void __init __no_sanitize_address 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;
    
    	/*
    	 * Interrupts are still disabled. Do necessary setups, then
    	 * enable them.
    	 * 中断关闭期间做一些重要的操作,然后打开中断
    	 */
    	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-specific boot-cpu hooks */
    	boot_cpu_hotplug_init();
    
    	build_all_zonelists(NULL);/* 建立系统内存页区(zone)链表 */
    	page_alloc_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
    	 * kmem_cache_init()
    	 */
    	setup_log_buf(0);/* 设置 log 使用的缓冲区*/
    	vfs_caches_init_early(); /* 预先初始化 vfs(虚拟文件系统)的目录项和索引节点缓存*/
    	sort_main_extable();/* 定义内核异常列表 */
    	trap_init();/* 完成对系统保留中断向量的初始化 */
    	mm_init();/* 内存管理初始化 */
    
    	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();
    
    	/*
    	 * 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() */
    
    	/* 初始中断相关初始化,主要是注册 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();/* 初始化系统时间 */
    
    	/* 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();/* slab 初始化,slab 是 Linux 内存分配器 */
    
    	/*
    	 * 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.
    	 */
    	/* 初始化控制台,之前 printk 打印的信息都存放
    	 * 缓冲区中,并没有打印出来。只有调用此函数
    	 * 初始化控制台以后才能在控制台上打印信息。
    	 */
    	console_init();
    	if (panic_later)
    		panic("Too many boot %s vars at `%s'", panic_later,
    		      panic_param);
    
    	lockdep_init();
    
    	/*
    	 * 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();/* 锁自测 */ 
    
    	/*
    	 * This needs to be called before any devices perform DMA
    	 * operations that might use the SWIOTLB bounce buffers. It will
    	 * mark the bounce buffers as decrypted so that their usage will
    	 * not cause "plain-text" data to be decrypted when accessed.
    	 */
    	mem_encrypt_init();
    
    #ifdef CONFIG_BLK_DEV_INITRD
    	if (initrd_start && !initrd_below_start_ok &&
    	    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;
    	}
    #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_vma slab 缓存 */ 
    #ifdef CONFIG_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();
    
    	/* Do the rest non-__init'ed, we're now alive */
    	/* 调用 rest_init 函数 */
    	/* 创建 init、kthread、idle 线程 */
    	arch_call_rest_init();
    
    	prevent_tail_call_optimization();
    }
    

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:

    • linux中的内核死锁调试
    • 深入理解Linux网络之内核是如何发送网络包的
    • 一文学会使用Linux内核模块&proc实例统计所有进程信息
    • Linux内核页表及页表缓存原理
    • Linux内核中的设计模式之全面理解与示例代码


    • Linux
    • 内核启动
    • start kernel

    相关文章

    • 虚拟机使用PuTTY、SSH Secure Shell Client前的配置

      这篇文章主要介绍了虚拟机使用PuTTY、SSH Secure Shell Client前的配置的相关资料,需要的朋友可以参考下 2017-01-01

    • Linux centos下php安装cphalcon扩展的方法

      这篇文章主要介绍了Linux下php安装cphalcon扩展的方法,需要的朋友可以参考下 2017-02-02

    • Ubuntu 18.04安装 pyenv、pyenv-virtualenv、virtualenv、Numpy、SciPy

      virtualenv 是一个创建隔绝的Python环境的工具。这篇文章主要介绍了Ubuntu 18.04安装 pyenv、pyenv-virtualenv、virtualenv、Numpy、SciPy、Pillow、Matplotlib的教程,需要的朋友可以参考下 2019-11-11

    • Linux 监控文件被什么进程修改(详解)

      下面小编就为大家带来一篇Linux 监控文件被什么进程修改(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 2016-12-12

    • Linux如何创建用户组和用户

      这篇文章主要介绍了Linux如何创建用户组和用户问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教 2023-08-08

    • Linux环境下部署Consul集群

      这篇文章介绍了Linux环境下部署Consul集群的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 2022-04-04

    • winxp apache用php建本地虚拟主机的方法

      windows xp用php建本地虚拟主机的方法(注:以下目录是笔者系统目录) 2009-07-07

    • 关于Linux安装mysql默认配置文件位置详解

      本篇文章主要介绍了关于Linux安装mysql默认配置文件位置详解,具有一定的参考价值,有兴趣的可以了解一下。 2017-06-06

    • PHP程序员玩转Linux系列 CentOS安装使用教程

      这篇文章主要为大家详细介绍了PHP程序员玩转Linux系列文章,CentOS安装使用教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 2017-04-04

    • Centos7下用户登录失败N次后锁定用户禁止登陆的方法

      这篇文章主要给大家介绍了关于在Centos7系统下用户登录失败N次后锁定用户禁止登陆的相关资料,文中先对PAM的配置文件进行了简单的介绍,然后通过示例代码将实现的方法介绍的非常详细,对大家的学习或者工作具有一定的参考价值,需要的朋友们下面随着小编来一起看看吧。 2018-01-01

    最新评论

    版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
    文章名称:《Linux内核启动流程之start_kernel问题》
    文章链接:https://zhuji.vsping.com/196632.html
    本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。