linux专题:嵌入式linux系统启动流程基础分析

简介: linux专题:嵌入式linux系统启动流程基础分析

e6cbe3d2b9d824e7eaf29a9bbd7cd90a.png


第一:linux内核源码基本简介


下载 Linux 内核网址:

https://www.kernel.org/

最新 Linux 内核是 5.15 版本。现在常用 Linux 内核源码为4.14、4.19、4.9 等版本,其中 4.14 版本源码压缩包大概 90+M,解压后 700+M,合计 61350 个文件。如此众多的文件,用 source insight 或者 VSCode 查看都会比较卡,所以可以采用在线查看的方式。

在线查看 Linux 内核源码网址:

https://elixir.bootlin.com/linux/latest/source

在线查看 Android 源码:

http://androidxref.com/

Android系统是基于Linux 内核的,最底层为Linux内核,源码量翻很多倍。所以用软件看安卓源码更卡,可以使用在线网址看源码。


第二:uboot启动分析

我们知道,Linux 系统的启动,前面有一个启动引导程序 bootloader,比如常用的 uboot,本文不分析 uboot 的启动,只放一张流程图:

1d7e9f3b8fd1c359a08c1a2600f45824.png

本文主要讲解当从 bootloader 跳转到 Linux 系统的启动函数 start_kernel 后,此函数对系统初始化的流程。


第三:内核源码分析


在 linux4.14/arch/arm/kernel/head.S 文件中,是最后汇编阶段的初始化,而后会跳转到 main.c 文件的 start_kernel 函数,在此做 Linux 启动初始化,在这个函数中会调用将近100个函数去完成 Linux 系统的初始化,调用函数如下(不同内核版本,顺序和细节有变化):

linux4.14/init/main.c,start_kernel 函数。

asmlinkage __visible void __init start_kernel(void)
{
 char *command_line;
 char *after_dashes;
 set_task_stack_end_magic(&init_task);
 smp_setup_processor_id();
 debug_objects_early_init();
 cgroup_init_early();
 local_irq_disable();
 early_boot_irqs_disabled = true;
 /*
  * Interrupts are still disabled. Do necessary setups, then
  * enable them.
  */
 boot_cpu_init();
 page_address_init();
 pr_notice("%s", linux_banner);
 setup_arch(&command_line);
 /*
  * Set up the the initial canary and entropy after arch
  * and after adding latent and command line entropy.
  */
 add_latent_entropy();
 add_device_randomness(command_line, strlen(command_line));
 boot_init_stack_canary();
 mm_init_cpumask(&init_mm);
 setup_command_line(command_line);
 setup_nr_cpu_ids();
 setup_per_cpu_areas();
 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
 boot_cpu_hotplug_init();
 build_all_zonelists(NULL);
 page_alloc_init();
 pr_notice("Kernel command line: %s\n", boot_command_line);
 /* parameters may set static keys */
 jump_label_init();
 parse_early_param();
 after_dashes = parse_args("Booting kernel",
      static_command_line, __start___param,
      __stop___param - __start___param,
      -1, -1, NULL, &unknown_bootoption);
 if (!IS_ERR_OR_NULL(after_dashes))
  parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
      NULL, set_init_arg);
 /*
  * These use large bootmem allocations and must precede
  * kmem_cache_init()
  */
 setup_log_buf(0);
 pidhash_init();
 vfs_caches_init_early();
 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();
 /*
  * Disable preemption - early bootup scheduling is extremely
  * fragile until we cpu_idle() for the first time.
  */
 preempt_disable();
 if (WARN(!irqs_disabled(),
   "Interrupts were enabled *very* early, fixing it\n"))
  local_irq_disable();
 radix_tree_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();
 /* Trace events are available after this */
 trace_init();
 context_tracking_init();
 /* init some links before init_ISA_irqs() */
 early_irq_init();
 init_IRQ();
 tick_init();
 rcu_init_nohz();
 init_timers();
 hrtimers_init();
 softirq_init();
 timekeeping_init();
 time_init();
 sched_clock_postinit();
 printk_safe_init();
 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();
 if (panic_later)
  panic("Too many boot %s vars at `%s'", panic_later,
        panic_param);
 lockdep_info();
 /*
  * 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
 kmemleak_init();
 debug_objects_mem_init();
 setup_per_cpu_pageset();
 numa_policy_init();
 if (late_time_init)
  late_time_init();
 calibrate_delay();
 pidmap_init();
 anon_vma_init();
 acpi_early_init();
#ifdef CONFIG_X86
 if (efi_enabled(EFI_RUNTIME_SERVICES))
  efi_enter_virtual_mode();
#endif
 thread_stack_cache_init();
 cred_init();
 fork_init();
 proc_caches_init();
 buffer_init();
 key_init();
 security_init();
 dbg_late_init();
 vfs_caches_init();
 pagecache_init();
 signals_init();
 proc_root_init();
 nsfs_init();
 cpuset_init();
 cgroup_init();
 taskstats_init_early();
 delayacct_init();
 check_bugs();
 acpi_subsystem_init();
 arch_post_acpi_subsys_init();
 sfi_init_late();
 if (efi_enabled(EFI_RUNTIME_SERVICES)) {
  efi_free_boot_services();
 }
 /* Do the rest non-__init'ed, we're now alive */
 rest_init();
 prevent_tail_call_optimization();
}


其中有七个函数较为重要,分别为:

setup_arch(&command_line);
mm_init();
sched_init();
init_IRQ();
console_init();
vfs_caches_init();
rest_init();

1、setup_arch(&command_line)

此函数是系统架构初始化函数,处理 uboot 传递进来的参数,不同的架构进行不同的初始化,也就是说每个架构都会有一个 setup_arch 函数。

linux4.14/arch/arm/kernel/setup.c

7d0bfb1cb53c2784804e3f72099b3a95.png

2、mm_init

内存初始化函数

linux4.14/init/main.c

7e86fbfce413f38e5f45f7019ac89a5c.png

3、sched_init

核心进程调度器初始化。Linux 内核实现了四种调度方式,一般是采用 CFS 调度方式。作为一个普适性的操作系统,必须考虑各种需求,我们不能只按照中断优先级或者时间轮转片来规定进程运行的时间。作为一个多用户操作系统,必须考虑到每个用户的公平性。不能因为一个用户没有高级权限,就限制他的进程的运行时间,要考虑每个用户拥有公平的时间。

linux4.14/kernel/sched/core.c

f74d6c596f7d346b74b8a8eed85def0b.png

4、init_IRQ

中断初始化函数,这个很好理解,大家都用过中断。

linux4.14/arch/arm/kernel/irq.c

a8f24c75ecc6aabd344b01022cff3a4f.png

5、console_init

在这个函数初始化之前,你所有写的内核打印函数 printk 都打印不出东西。在这个函数初始化之前,所有打印都会存在 buf 里,此函数初始化以后,会将 buf里面的数据打印出来,你才能在终端看到 printk 打印的东西。

tty 是 Linux 中的终端, _con_initcall_start 和_con_initcall_end 这两句的意思是执行所有两者之间的 initcall 函数。

linux4.14/kernel/printk/printk.c

0f53dc17fde09233d07085cc6cda4410.png

6、vfs_caches_init

虚拟文件系统初始化,比如 sysfs,根文件系统等,就是在这一步进行挂载,proc 是内核虚拟的,用来输出内核数据结构信息,不算在这里。

vfs虚拟文件系统,屏蔽了底层硬件的不同,提供了统一的接口,方便系统的移植和使用。使用户在不用更改应用代码的情况下直接移植代码到其他平台。

linux4.14/fs/dcache.c

35c7e958c7692bf19a9f03184093704a.png

这里的挂载主要在mnt_init()函数中:

linux4.14/fs/namespace.c

58a126b7f8834f73b793b1be005d20b0.png

7、rest_init

这个函数可以算是 start_kernel函数调用的最后一个函数,在这里产生了最重要的两个内核进程 kernel_init 和 kthreadd,kernel_init后面会从内核空间跳转到用户空间,变成用户空间的 init 进程,PID=1,而 kthreadd ,PID=2,是内核进程,专门用来监听创建内核进程的请求,它维护了一个链表,如果有创建内核进程的需求,就会在链表上创建。

至此,用户空间最重要的 init 进程已经出来,后面用户空间的进程都由 init进程来 fork。如果是安卓系统,init 进程会 fork 出一个 zygote 进程,他是所有安卓系统进程的父进程。

linux4.14/init.main.c

340700f51d72e340ba240978ece690a3.png

5db55ea98042fa8ee83613312b73e837.png

上图,400 行创建了 kernel_init 进程,412 行创建了 kthreadd 进程,这两个都是内核进程。426 行通知 kernel_init 进程 kthreadd 已经创建完毕。也就是说,实际上是 kthreadd 先运行,kernel_init 再运行。


其余的函数大家可以参照下面的文章去理解:

https://www.cnblogs.com/andyfly/p/9410441.html

https://www.cnblogs.com/lifexy/p/7366782.html

https://www.cnblogs.com/yanzs/p/13910344.html#radix_tree:init

目录
相关文章
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
1316 3
Linux系统禁用swap
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1906 3
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
1008 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
944 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
1389 3
Linux系统初始化脚本
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
903 18
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
2912 10
|
存储 Linux
Linux环境下删除大文件后磁盘空间未释放问题诊断流程。
以上诊断流程涉及Linux底层机制与高级管理技能结合之处,并需要管理员根据实际环境灵活调整诊断策略与解决方案。
1040 8
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
1475 1
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1764 1