Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

MACHINE_START 與 MACHINE_END 宏到底展開成什麼

在 linux-2.6.38 的 arch/arm/mach-w90x900/mach-nuc950evb.c 檔案末尾,會看到一段看似神祕的宏定義:

MACHINE_START(W90P950EVB, "W90P950EVB")
    .phys_io     = W90X900_PA_UART,
    .io_pg_offst = (((u32)W90X900_VA_UART) >> 18) & 0xfffc,
    .boot_params = 0x100,
    .map_io     = nuc950evb_map_io,
    .init_irq   = nuc900_init_irq,
    .init_machine = nuc950evb_init,
    .timer      = &nuc900_timer,
MACHINE_END

宏的定義位置

這兩個宏定義在 arch/arm/include/asm/mach/arch.h,而它們所組装的 machine_desc 結構如下:

struct machine_desc {
    /*
     * Note! The first four elements are used
     * by assembler code in head.S, head-common.S
     */
    unsigned int   nr;             /* architecture number */
    unsigned int   phys_io;        /* start of physical io */
    unsigned int   io_pg_offst;    /* byte offset for io page table entry */
    const char    *name;           /* architecture name */
    unsigned long  boot_params;    /* tagged list */
    unsigned int   video_start;    /* start of video RAM */
    unsigned int   video_end;      /* end of video RAM */
    unsigned int   reserve_lp0 :1;
    unsigned int   reserve_lp1 :1;
    unsigned int   reserve_lp2 :1;
    unsigned int   soft_reboot :1;
    void  (*fixup)(struct machine_desc *, struct tag *, char **, struct meminfo *);
    void  (*map_io)(void);
    void  (*init_irq)(void);
    struct sys_timer *timer;
    void  (*init_machine)(void);
};

#define MACHINE_START(_type,_name)     \
static const struct machine_desc __mach_desc_##_type \
__used __attribute__((__section__(".arch.info.init"))) = { \
    .nr   = MACH_TYPE_##_type,      \
    .name = _name,

#define MACHINE_END                    \
};

展開後的樣子

static const struct machine_desc __mach_desc_W90P950EVB
__used __attribute__((__section__(".arch.info.init"))) = {
    .nr = MACH_TYPE_W90P950EVB,
    .name = "W90P950EVB",
    .phys_io = W90X900_PA_UART,
    .io_pg_offst = (((u32)W90X900_VA_UART) >> 18) & 0xfffc,
    .boot_params = 0x100,
    .map_io = nuc950evb_map_io,
    .init_irq = nuc900_init_irq,
    .init_machine = nuc950evb_init,
    .timer = &nuc900_timer,
}

關鍵在於 __attribute__((__section__(".arch.info.init"))):所有 machine_desc 都被放進同一個 section,由連結器收集成一張表,內核啟動時再逐筆比對。

機型號碼的來源

MACH_TYPE_W90P950EVB 定義在 arch/arm/include/asm/mach-types.h,值為 1860(十六進位 0x744):

#define MACH_TYPE_W90P950EVB    1860

這個號碼並非手寫,而是由 arch/arm/tool/mach-types 中的清單在編譯時生成:

/* arch/arm/tool/mach-types */
w90x900    MACH_W90X900    W90X900    1860

啟動時的比對流程

在 arch/arm/kernel/head-common.S 的 __lookup_machine_type 中,會拿 bootloader 傳來的機型號去 table 裡逐筆比對:

__lookup_machine_type:
#ifdef CONFIG_ARCH_W90X900        // 就是前面的 1860,十六進位 0x744
        mov   r1, #0x700
        add   r1, r1, #0x40
        add   r1, r1, #0x04
#endif
    adr  r3, 4b
    ldmia r3, {r4, r5, r6}
    sub  r3, r3, r4      @ get offset between virt&phys
    add  r5, r5, r3      @ convert virt addresses to
    add  r6, r6, r3      @ physical address space
1:  ldr  r3, [r5, #MACHINFO_TYPE]   @ get machine type
    teq  r3, r1                      @ matches loader number?
    beq  2f                          @ found
    add  r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
    cmp  r5, r6
    blo  1b
    mov  r5, #0                      @ unknown machine
2:  mov  pc, lr
ENDPROC(__lookup_machine_type)

這裡有個實務上的細節值得注意:由於此時 MMU 尚未開啟,不能用絕對位址去存取 __arch_info 清單,必須先算出虛擬位址與實體位址的偏移量再加回去。比對成功後 r5 會指向該機型的 machine_desc(實體位址),比對失敗則回傳 0 代表未知機型。

Copyright © Linux教程網 All Rights Reserved