乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      一文搞懂內(nèi)核的啟動

       好漢勃士 2022-06-17 發(fā)布于廣東

      vmlinux 屬于 ELF 文件,要想了解如何啟動 vmlinux,首先需要知道 ELF 的格式。

      文章圖片1

      精靈

      • 文本段

      代碼段,通常是指用來存放程序執(zhí)行代碼的一塊內(nèi)存區(qū)域。這部分區(qū)域的大小在程序運(yùn)行前就已經(jīng)確定。

      • data段

      數(shù)據(jù)段,通常是指用來存放程序中已初始化的全局變量的一塊內(nèi)存區(qū)域。數(shù)據(jù)段屬于靜態(tài)內(nèi)存分配。

      • bss段

      通常是指用來存放程序中未初始化的全局變量和靜態(tài)變量的一塊內(nèi)存區(qū)域。BSS段屬于靜態(tài)內(nèi)存分配。

      • 初始化段

      linux定義的一種初始化過程中才會用到的段,一旦初始化完成,那么這些段所占用的內(nèi)存會被釋放掉,后續(xù)會繼續(xù)說明。

      vmlinux 入口:第一行運(yùn)行的代碼

      Linux啟動,會啟動內(nèi)核編譯后的文件 vmlinux,vmlinux 是一個 ELF 文件,按照
      ./arch/arm64/kernel/vmlinux.lds 設(shè)定的規(guī)則進(jìn)行鏈接,vmlinux.lds 是 vmlinux.lds.S 編譯之后生成的。所以為了確定 vmlinux 內(nèi)核的起始地址, 首先通過 vmlinux.lds.S 鏈接腳本進(jìn)行分析。如下所示:

      $ 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)核視頻教程文檔資料免費(fèi)領(lǐng)取后臺私信【內(nèi)核】自行獲取.

      文章圖片2
      文章圖片3

      Linux內(nèi)核源碼/內(nèi)存調(diào)優(yōu)/文件系統(tǒng)/進(jìn)程管理/設(shè)備驅(qū)動/網(wǎng)絡(luò)協(xié)議棧-學(xué)習(xí)視頻教程-騰訊課堂

      .head.text 文本段

      通過 vmlinux.lds.S 找到 vmlinux 的入口函數(shù)。具體分析如下:

      /* SPDX-License-Identifier: GPL-2.0 *//* * ld script to make ARM Linux kernel * taken from the i386 version by Russell King * Written by Martin Mares <mj@atrey.karlin.mff.cuni.cz> */#define RO_EXCEPTION_TABLE_ALIGN        8#define RUNTIME_DISCARD_EXIT#include <asm-generic/vmlinux.lds.h>#include <asm/cache.h>#include <asm/hyp_image.h>#include <asm/kernel-pgtable.h>#include <asm/memory.h>#include <asm/page.h>#include 'image.h'OUTPUT_ARCH(aarch64)ENTRY(_text)

      根據(jù)鏈接腳本語法,可以知道 OUTPUT_ARCH 關(guān)鍵字指定了鏈接之后的輸出文件的體系結(jié)構(gòu)是 aarch64。ENTRY 關(guān)鍵字指定了輸出文件 vmlinux 的入口 地址是 _text, 因此只需找到 _text 的定義就可以知道 vmlinux 的入口函數(shù)。接下來的代碼是:

      文章圖片4
      • 上圖中的宏 HEAD_TEXT 定義在文件 include/asm-generic/vmlinux.lds.S 中,其定義為 .head.text 文本段。
      • 上圖中的 idmap_pg_dir,init_pg_dir 是頁表映射,idmap_pg_dir 是 identity mapping 用到的頁表,init_pg_dir 是 kernel_image_mapping 用到的頁表。
      /* 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í)行。

              __HEAD_head:        /*         * DO NOT MODIFY. Image header expected by Linux boot-loaders.         */#ifdef CONFIG_EFI        /*         * This add instruction has no meaningful effect except that         * its opcode forms the magic 'MZ' signature required by UEFI.         */        add     x13, x18, #0x16        b       primary_entry#else        b       primary_entry                   // branch to kernel start, magic        .long   0                               // reserved#endif

      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 來填充這兩個頁表。

      SYM_FUNC_START_LOCAL(__create_page_tables) mov x28, lr  ...... /*  * Create the identity mapping.  */ adrp x0, idmap_pg_dir adrp x3, __idmap_text_start  // __pa(__idmap_text_start)  ......  adrp x5, __idmap_text_end  ...... /*  * Map the kernel image (starting with PHYS_OFFSET).  */ adrp x0, init_pg_dir mov_q x5, KIMAGE_VADDR  // compile time __va(_text) add x5, x5, x23   // add KASLR displacement mov x4, PTRS_PER_PGD adrp x6, _end   // runtime __pa(_end) adrp x3, _text   // runtime __pa(_text) sub x6, x6, x3   // _end - _text add x6, x6, x5   // runtime __va(_end)  ......SYM_FUNC_END(__create_page_tables)
      文章圖片5

      ?

      __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主要完成了如下的工作:

      1. 為init進(jìn)程設(shè)置好堆棧地址和大小,保存當(dāng)前進(jìn)程描述符地址到sp_el0;
      2. 設(shè)置異常向量表基址寄存器;
      3. 保存FDT地址到__fdt_pointer變量;
      4. 將kimage的虛擬地址和物理地址的偏移保存到kimage_voffset
      5. 清除 bss
      6. 跳轉(zhuǎn)到start_kernel

      用一張圖概括:

      文章圖片6

      ?

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多