| vmlinux 屬于 ELF 文件,要想了解如何啟動 vmlinux,首先需要知道 ELF 的格式。  精靈
 代碼段,通常是指用來存放程序執(zhí)行代碼的一塊內(nèi)存區(qū)域。這部分區(qū)域的大小在程序運行前就已經(jīng)確定。 
 數(shù)據(jù)段,通常是指用來存放程序中已初始化的全局變量的一塊內(nèi)存區(qū)域。數(shù)據(jù)段屬于靜態(tài)內(nèi)存分配。 
 通常是指用來存放程序中未初始化的全局變量和靜態(tài)變量的一塊內(nèi)存區(qū)域。BSS段屬于靜態(tài)內(nèi)存分配。 
 linux定義的一種初始化過程中才會用到的段,一旦初始化完成,那么這些段所占用的內(nèi)存會被釋放掉,后續(xù)會繼續(xù)說明。 vmlinux 入口:第一行運行的代碼Linux啟動,會啟動內(nèi)核編譯后的文件 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 架構(gòu)平臺的 ELF 可執(zhí)行文件,其程序入口的地址為 0xffff800010000000,此段對應(yīng)的 section 為.head.text .text .got.plt......,所以 vmlinux 的入口在 .head.text 文本段。 更多l(xiāng)inux內(nèi)核視頻教程文檔資料免費領(lǐng)取后臺私信【內(nèi)核】自行獲取.   .head.text 文本段通過 vmlinux.lds.S 找到 vmlinux 的入口函數(shù)。具體分析如下: 根據(jù)鏈接腳本語法,可以知道 OUTPUT_ARCH 關(guān)鍵字指定了鏈接之后的輸出文件的體系結(jié)構(gòu)是 aarch64。ENTRY 關(guān)鍵字指定了輸出文件 vmlinux 的入口 地址是 _text, 因此只需找到 _text 的定義就可以知道 vmlinux 的入口函數(shù)。接下來的代碼是:  
 /* include/asm-generic/vmlinux.lds.h文件 */#define HEAD_TEXT KEEP(*(.head.text))/* include/linux/init.h 文件*/#define __HEAD .section '.head.text','ax'故轉(zhuǎn)向 arch/arm64/kernel/head.S 中繼續(xù)執(zhí)行。 primary_entry進(jìn)入正式的初始化流程。 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 傳遞的參數(shù),使 dcache 失效。 el2_setup 設(shè)定 core 啟動狀態(tài)。 set_cpu_boot_mode_flag 設(shè)置 core 啟動的 EL。 __create_page_tables 創(chuàng)建頁表我們知道 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 開啟切換到虛擬地址,并調(diào)用 __primary_switched。 __primary_switched__primary_switched主要完成了如下的工作: 
 用一張圖概括:  ? | 
|  |