Linux 内核初始化 - 何时解析 devicetree blob 并加载树节点?

Linux kernel initialization - When are devicetree blobs parsed and tree nodes are loaded?

我想为 Linux 初始化建立一个里程碑路线图,以便于我理解。 (对于嵌入式系统)这是我得到的:

  1. 引导加载程序将内核加载到 RAM 并启动它
  2. Linux 内核进入head.o,启动start_kernel()
  3. CPU架构找到,MMU启动。
  4. setup_arch() 被调用,设置 CPU。
  5. 内核子系统已加载。
  6. 调用
  7. do_initcalls() 并启动具有 *_initcall() 和 module_init() 函数的模块。
  8. 那么 /sbin/init(或类似的)是 运行。

这里不知道devicetree具体是什么时候处理的。是在处理 do_initcall() 函数时还是在处理之前? 一般什么时候解析devicetree,什么时候处理树节点? 非常感谢您。

非常感谢对我的想法的任何纠正。

这是个好问题。

首先,我想你已经知道内核会使用DT中的数据来识别特定的机器,如果跨平台或硬件通用,我们需要在早期启动时建立它,以便它有机会 运行 机器特定的修正。

这里是我从linux kernel documents.

中摘录的一些信息

In the majority of cases, the machine identity is irrelevant, and the kernel will instead select setup code based on the machine’s core CPU or SoC. On ARM for example, setup_arch() in arch/arm/kernel/setup.c will call setup_machine_fdt() in arch/arm/kernel/devtree.c which searches through the machine_desc table and selects the machine_desc which best matches the device tree data. It determines the best match by looking at the ‘compatible’ property in the root device tree node, and comparing it with the dt_compat list in struct machine_desc (which is defined in arch/arm/include/asm/mach/arch.h if you’re curious).

至于 Linux 初始化,我认为我们可以在列表中添加一些内容。

  1. 放上START按钮,复位信号触发

  2. CS:IP 修复为 BIOS 0XFFFF0 地址

  3. 跳转到BIOS的开始

  4. 自检,键盘等硬件设备启动,实模式IDT & GDT

  5. 加载 grub2 或 syslinux 等 Bootloader。

  6. 引导加载程序将内核加载到 RAM 并启动它 (boot.img->core.img)。

  7. A20打开,调用setup.s,切换到保护模式

  8. Linux内核进入head.o,IDT&GDT刷新,decompress_kernel(),启动start_kernel()

  9. INIT_TASK(init_task) 创建

  10. trap_init()

  11. CPU 找到架构,启动 MMU (mmu_init())。

  12. setup_arch() 被调用,设置 CPU。

  13. 内核子系统已加载。

  14. 调用
  15. do_initcalls() 并启动具有 *_initcall() 和 module_init() 函数的模块。

  16. rest_init() 将创建进程 1 和 2,换句话说,/sbin/init(或类似的)并且 kthreadd 是 运行.