Kernel啓動流程源碼解析 2 head.S


  1. __cpu_setup。定義kernel\arch\arm64\mm\proc.S中。
#define MAIR(attr, mt)    ((attr) << ((mt) * 8))

/*
 *    __cpu_setup
 *
 *    Initialise the processor for turning the MMU on.  Return in x0 the
 *    value of the SCTLR_EL1 register.
 */
ENTRY(__cpu_setup)
    ic    iallu                // I+BTB cache invalidate // 使instruction和BTB cache中的所有的cache line是無效,直到PoU // BTB(Branch Target Buffer)
    tlbi    vmalle1is            // invalidate I + D TLBs // 使TLB內容無效vm-all-e1-is,vmall表示invalidate all TLB entry,e1表示EL1,is表示inner sharebility
    dsb    ish // dsb 數據同步屏蔽指令,確保前面的指令執行完成,ish表示inner shareable

    mov    x0, #3 << 20
    msr    cpacr_el1, x0            // Enable FP/ASIMD // 使能SIMD and floating-point
    msr    mdscr_el1, xzr            // Reset mdscr_el1 // Monitor Debug System Control Register
    /*
     * Memory region attributes for LPAE:
     *
     *   n = AttrIndx[2:0] // 最重要的memory attributes指向MAIR_ELx中具體的memory attribute
     *            n    MAIR
     *   DEVICE_nGnRnE    000    00000000 // G:Gathering 表示對多個memory的訪問是否可以合併
     *   DEVICE_nGnRE    001    00000100 // R: Re-ordering 表示是否允許處理器對內存訪問指令進行重排
     *   DEVICE_GRE        010    00001100 // E: Early Write Acknowledgement
     *   NORMAL_NC        011    01000100 // 前面加n表示不允許
     *   NORMAL        100    11111111 // C: 表示是否cacheable
     */
    ldr    x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \
             MAIR(0x04, MT_DEVICE_nGnRE) | \
             MAIR(0x0c, MT_DEVICE_GRE) | \
             MAIR(0x44, MT_NORMAL_NC) | \
             MAIR(0xff, MT_NORMAL)
    msr    mair_el1, x5 // MAIR_EL1(Memory Attribute Indirection Register (EL1))
    /*
     * Prepare SCTLR
     */
    adr    x5, crval // crval的定義在底下
    ldp    w5, w6, [x5] // w5 = 0x000802c2 // w6 = 0x0405d03d
    mrs    x0, sctlr_el1 // System Control Register (EL1) // x0將作爲傳遞給__enable_mmu時的參數
    bic    x0, x0, x5            // clear bits // 清0,0x000802c2中爲1的位
    orr    x0, x0, x6            // set bits // 置1,0x0405d03d中爲1的位
    /*
     * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for
     * both user and kernel.
     */
    ldr    x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \
            TCR_TG_FLAGS | TCR_ASID16 | TCR_TBI0 // ldr x10, 0xffffffc000d0ca38
    /*
     * Read the PARange bits from ID_AA64MMFR0_EL1 and set the IPS bits in
     * TCR_EL1.
     */
    mrs    x9, ID_AA64MMFR0_EL1
    bfi    x10, x9, #32, #3 // 將x9的[34:32]bit寫入到x10的[34:32]bit
    msr    tcr_el1, x10
    ret                    // return to head.S // 由於lr保存的是__enable_mmu,因此返回到__enable_mmu函數繼續執行
ENDPROC(__cpu_setup)

    /*
     *                 n n            T
     *       U E      WT T UD     US IHBS
     *       CE0      XWHW CZ     ME TEEA S
     * .... .IEE .... NEAI TE.I ..AD DEN0 ACAM
     * 0011 0... 1101 ..0. ..0. 10.. .... .... < hardware reserved
     * .... .1.. .... 01.1 11.1 ..01 0001 1101 < software settings
     */
    .type    crval, #object
crval:
    .word    0x000802c2    // clear // SCTLR_EL1寄存器中需要清0的bit
    .word    0x0405d03d    // set // SCTLR_EL1寄存器中需要置1的bit


  1. __enable_mmu。
/*
 * Setup common bits before finally enabling the MMU. Essentially this is just
 * loading the page table pointer and vector base registers.
 *
 * On entry to this code, x0 must contain the SCTLR_EL1 value for turning on
 * the MMU.
 */
// 要求x0中保存SCTLR_EL1的值
__enable_mmu:
    ldr    x5, =vectors // vectors:EL1狀態的異常向量表 ,定義在kernel\arch\arm64\kernel\entry.S中
    msr    vbar_el1, x5 // VBAR_EL1 Vector Base Address Register (EL1),保存EL1狀態的異常向量表。
    msr    ttbr0_el1, x25            // load TTBR0 // idmap_pg_dir 用於用戶空間的進程,在進程切換的時候,其地址空間的切換實際就是修改TTBR0的值
    msr    ttbr1_el1, x26            // load TTBR1 // swapper_pg_dir 用於kernel space,所有的內核線程都是共享一個空間
    isb // 指令同步屏障
    b    __turn_mmu_on
ENDPROC(__enable_mmu)

    .align    4
__turn_mmu_on:
    msr    sctlr_el1, x0 // 配置sctlr_el1,系統控制寄存器
    isb  // 指令同步屏障
    br    x27 // 跳轉到__mmap_switched執行
ENDPROC(__turn_mmu_on)

  1. __mmap_switched。
/*
 * The following fragment of code is executed with the MMU on in MMU mode, and
 * uses absolute addresses; this is not position independent.
 */
__mmap_switched:
    adr    x3, __switch_data + 8 // 跳過__mmap_switched

    ldp    x6, x7, [x3], #16 // x6 = __bss_start // x7 = __bss_stop
1:    cmp    x6, x7
    b.hs    2f
    str    xzr, [x6], #8            // Clear BSS // 循環清除bss段,BSS段用來存放程序中未初始化的全局變量和靜態變量
    b    1b
2:
    ldp    x4, x5, [x3], #16 // x4 = processor_id // x5 = __fdt_pointer
    ldr    x6, [x3], #8 // x6 = memstart_addr
    ldr    x16, [x3] // init_thread_union + THREAD_START_SP // sp // THREAD_START_SP = THREAD_SIZE - 16
    mov    sp, x16 // 跳轉到c語言前,必須設置好棧
    str    x22, [x4]            // Save processor ID // 將x22保存的當前cpu id保存到processor_id(kernel/arch/arm64 /kernel/setup.c中定義的 )中
    str    x21, [x5]            // Save FDT pointer // 將x21暫存的device tree的地址保存到__fdt_pointer(在kernel/arch/arm64 /kernel/setup.c中定義的)中
    str    x24, [x6]            // Save PHYS_OFFSET // 將x24保存的kernel起始的地址保存到memstart_addr(在kernel/arch/arm64/mm/init.c中定義的)
    mov    x29, #0 // 將x29清0,還不清楚爲什麼要特別的做這一步

    b    start_kernel // 長出一口氣,終於到C語言了。
ENDPROC(__mmap_switched)


    .align    3
    .type    __switch_data, %object
__switch_data:
    .quad    __mmap_switched
    .quad    __bss_start            // x6
    .quad    __bss_stop            // x7
    .quad    processor_id            // x4
    .quad    __fdt_pointer            // x5
    .quad    memstart_addr            // x6 // phys_addr_t memstart_addr __read_mostly = 0;
    .quad    init_thread_union + THREAD_START_SP // sp // init_thread_uniond定義在kernel/init/init_task.c中


#define THREAD_SIZE        16384
#define THREAD_START_SP        (THREAD_SIZE - 16)

// 進入start_kernel之前瞥一眼init_thread_union這個聯合體和init_task這個結構體,這裏我們只需知道init_task 是0號swapper進程的task_struct,esp指針指向這個init_thread_union以上(16384 - 16)作爲0號swapper進程棧頂
union thread_union init_thread_union __init_task_data =
    { INIT_THREAD_INFO(init_task) };

#define __init_task_data __attribute__((__section__(".data..init_task")))



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章