vmlinux 属于 ELF 文件,要想了解如何启动 vmlinux,首先需要知道 ELF 的格式。 ![]() 精灵
代码段,通常是指用来存放程序执行代码的一块内存区域。这部分区域的大小在程序运行前就已经确定。
数据段,通常是指用来存放程序中已初始化的全局变量的一块内存区域。数据段属于静态内存分配。
通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域。BSS段属于静态内存分配。
linux定义的一种初始化过程中才会用到的段,一旦初始化完成,那么这些段所占用的内存会被释放掉,后续会继续说明。 vmlinux 入口:第一行运行的代码Linux启动,会启动内核编译后的文件 vmlinux,vmlinux 是一个 ELF 文件,按照 $ readelf -h vmlinuxELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: AArch64 Version: 0x1 Entry point address: 0xffff800010000000 Start of program headers: 64 (bytes into file) Start of section headers: 494679672 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 5 Size of section headers: 64 (bytes) Number of section headers: 38 Section header string table index: 37$ readelf -l vmlinuxElf file type is DYN (Shared object file)Entry point 0xffff800010000000There are 5 program headers, starting at offset 64Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000010000 0xffff800010000000 0xffff800010000000 0x0000000001beacdc 0x0000000001beacdc RWE 10000 LOAD 0x0000000001c00000 0xffff800011c00000 0xffff800011c00000 0x00000000000c899c 0x00000000000c899c R E 10000 LOAD 0x0000000001cd0000 0xffff800011cd0000 0xffff800011cd0000 0x0000000000876200 0x0000000000905794 RW 10000 NOTE 0x0000000001bfaca0 0xffff800011beaca0 0xffff800011beaca0 0x000000000000003c 0x000000000000003c R 4 GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 RW 10 Section to Segment mapping: Segment Sections... 00 .head.text .text .got.plt .rodata .pci_fixup __ksymtab __ksymtab_gpl __ksymtab_strings __param __modver __ex_table .notes 01 .init.text .exit.text .altinstructions 02 .init.data .data..percpu .hyp.data..percpu .rela.dyn .data __bug_table .mmuoff.data.write .mmuoff.data.read .pecoff_edata_padding .bss 03 .notes 04 通过上面的查询可知,此 vmlinux 为一个 aarch64 架构平台的 ELF 可执行文件,其程序入口的地址为 0xffff800010000000,此段对应的 section 为.head.text .text .got.plt......,所以 vmlinux 的入口在 .head.text 文本段。 更多linux内核视频教程文档资料免费领取后台私信【内核】自行获取. ![]() ![]() Linux内核源码/内存调优/文件系统/进程管理/设备驱动/网络协议栈-学习视频教程-腾讯课堂 .head.text 文本段通过 vmlinux.lds.S 找到 vmlinux 的入口函数。具体分析如下:
根据链接脚本语法,可以知道 OUTPUT_ARCH 关键字指定了链接之后的输出文件的体系结构是 aarch64。ENTRY 关键字指定了输出文件 vmlinux 的入口 地址是 _text, 因此只需找到 _text 的定义就可以知道 vmlinux 的入口函数。接下来的代码是: ![]()
/* include/asm-generic/vmlinux.lds.h文件 */#define HEAD_TEXT KEEP(*(.head.text))/* include/linux/init.h 文件*/#define __HEAD .section '.head.text','ax' 故转向 arch/arm64/kernel/head.S 中继续执行。
primary_entry进入正式的初始化流程。 SYM_CODE_START(primary_entry) bl preserve_boot_args bl el2_setup // Drop to EL1, w0=cpu_boot_mode adrp x23, __PHYS_OFFSET and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to 0 bl set_cpu_boot_mode_flag bl __create_page_tables /* * The following calls CPU setup code, see arch/arm64/mm/proc.S for * details. * On return, the CPU will be ready for the MMU to be turned on and * the TCR will have been set. */ bl __cpu_setup // initialise processor b __primary_switchSYM_CODE_END(primary_entry) preserve_boot_args 是用来保存从 bootloader 传递的参数,使 dcache 失效。 el2_setup 设定 core 启动状态。 set_cpu_boot_mode_flag 设置 core 启动的 EL。 __create_page_tables 创建页表我们知道 idmap_pg_dir 是 identity mapping 用到的页表,init_pg_dir 是 kernel_image_mapping 用到的页表。这里通过 __create_page_tables 来填充这两个页表。
![]() __cpu_setup 初始话 CPU为开启 MMU 做一些 CPU 的初始化工作。 SYM_FUNC_START(__cpu_setup) tlbi vmalle1 // Invalidate local TLB dsb nsh mov x1, #3 << 20 msr cpacr_el1, x1 // Enable FP/ASIMD mov x1, #1 << 12 // Reset mdscr_el1 and disable msr mdscr_el1, x1 // access to the DCC from EL0 isb // Unmask debug exceptions now, enable_dbg // since this is per-cpu reset_pmuserenr_el0 x1 // Disable PMU access from EL0 reset_amuserenr_el0 x1 // Disable AMU access from EL0 /* * Memory region attributes */ mov_q x5, MAIR_EL1_SET 前面做 TLB/FP/ASIMD/DCC/PMU/AMU 的初始化,后面做 Memory region attributes。 __primary_switch 开启切换到虚拟地址,并调用 __primary_switched。 __primary_switched__primary_switched主要完成了如下的工作:
用一张图概括: ![]() |
|