一路走來真是內(nèi)牛滿面,現(xiàn)在終于來到了kernel的32位入口了。這就是startup_32。這個入口在arch/x86/boot/compressed/header_32.S里面。至此,請記住,bootloader對于kernel來說意義只有boot_params結(jié)構(gòu)了,其他的一切的一切已經(jīng)都是浮云了。而boot_params當(dāng)前地址在esi中。
startup_32缺省在內(nèi)存的絕對地址0x10000。讓我們慢慢解析startup_32。
- #include <linux/init.h>
- #include <linux/linkage.h>
- #include <asm/segment.h>
- #include <asm/page_types.h>
- #include <asm/boot.h>
- #include <asm/asm-offsets.h>
- __HEAD
- ENTRY(startup_32)
- cld
- /*
- * Test KEEP_SEGMENTS flag to see if the bootloader is asking
- * us to not reload segments. BP_loadflags(%esi)即指向boot_params.loadflags.這一位應(yīng)該是在設(shè)置了code32_start Hook的時(shí)候使用,因?yàn)樵趐rotected_mode_jump的最后已經(jīng)將所有的段都置為_BOOT_DS了。如果bootloader hook了code32_start,返回kernel的時(shí)候顯然kernel需要去恢復(fù)所有的段。
- */
- testb $(1<<6), BP_loadflags(%esi)
- jnz 1f
- cli
- movl $__BOOT_DS, %eax
- movl %eax, %ds
- movl %eax, %es
- movl %eax, %fs
- movl %eax, %gs
- movl %eax, %ss
- 1:
- /*
- * Calculate the delta between where we were compiled to run
- * at and where we were actually loaded at. This can only be done
- * with a short local call on x86. Nothing else will tell us what
- * address we are running at. The reserved chunk of the real-mode
- * data at 0x1e4 (defined as a scratch field) are used as the stack
- * for this calculation. Only 4 bytes are needed.
- */
- //仔細(xì)閱讀了以上的這段英文,不能不說代碼構(gòu)思的巧妙。由于不知道代碼是否被加載到0x100000的地址,通過以下的代碼就能計(jì)算出實(shí)際加載的地址和預(yù)期地址的差異,也就是說是實(shí)際的startup_32的位置。
- leal (BP_scratch+4)(%esi), %esp //boot_params.scratch的地址設(shè)置成為堆棧頂。
- call 1f //boot_params.scratch里面就是1:的實(shí)際地址
- 1: popl %ebp //ebp就是1:的實(shí)際地址
- subl $1b, %ebp //ebp-1:就是實(shí)際與預(yù)期的差異, 也就是說是實(shí)際的startup_32的位置。
- /*
- * %ebp contains the address we are loaded at by the boot loader and %ebx
- * contains the address where we should move the kernel image temporarily
- * for safe in-place decompression.
- */
- #ifdef CONFIG_RELOCATABLE
- movl %ebp, %ebx
-
- //kernel_alignment里面是kernel地址對齊所需要移動的位移量,這是有bootloader填入的,因?yàn)閎ootloader可能將startup_32裝載在非對齊的地址。那么就需要增加移動的位移量來保證對齊而達(dá)到更好的性能。下面的代碼就是要調(diào)整地址的位移而保證對齊。
- movl BP_kernel_alignment(%esi), %eax
- decl %eax
- addl %eax, %ebx
- notl %eax
- andl %eax, %ebx
- #else
- movl $LOAD_PHYSICAL_ADDR, %ebx //LOAD_PHYSICAL_ADDR在 arch/x86/include/asm/boot.h里.實(shí)際上應(yīng)該是0x100000
- #endif
- /* Target address to relocate to for decompression */
- addl $z_extract_offset, %ebx //z_extract_offset由MKpiggy.c 在編譯時(shí)產(chǎn)生的piggy.S里面定義。在我編譯kernel時(shí),z_extract_offset是0x4a0000,現(xiàn)在ebx的值在不考慮reloc的情況下是0x5a0000
- /* Set up the stack */
- leal boot_stack_end(%ebx), %esp //在0x5a0000+boot_stack_end的位置建立棧。
- /* Zero EFLAGS */
- pushl $0
- popfl
- /*
- * Copy the compressed kernel to the end of our buffer
- * where decompression in place becomes safe.
- */
- pushl %esi
- leal (_bss-4)(%ebp), %esi //esi指向源,即ebp+_bss-4的地址,是當(dāng)前bootloader加載32位kernel的地址空間
- leal (_bss-4)(%ebx), %edi //edi指向目的地址,即ebx+_bss-4的地址,如果kernel不要reloc,就是0x5a0000+_bss-4
- movl $(_bss - startup_32), %ecx //從startup_32d到_bss有多少個字節(jié)?
- shrl $2, %ecx //實(shí)際我們移動每次4個字節(jié),所以ecx需要除4.
- std
- rep movsl //走咯,我們把自己移動上去
- cld
- popl %esi
- /*
- * Jump to the relocated address.
- */
- leal relocated(%ebx), %eax
- jmp *%eax //跳轉(zhuǎn)到relocated上去即ebx+relocated,即0x5a0000+relocated.
- ENDPROC(startup_32)
- .text
- relocated:
- /*
- * Clear BSS (stack is currently empty)
- */
- xorl %eax, %eax
- leal _bss(%ebx), %edi
- leal _ebss(%ebx), %ecx
- subl %edi, %ecx
- shrl $2, %ecx
- rep stosl
- /*
- * Adjust our own GOT GOT是什么?難道是Global Object Table?為什么GOT里面的每一個項(xiàng)都加上了ebx(0x5a0000)?難道里面是一堆指針需要調(diào)整所以加上ebx?
- */
- leal _got(%ebx), %edx
- leal _egot(%ebx), %ecx
- 1:
- cmpl %ecx, %edx
- jae 2f
- addl %ebx, (%edx)
- addl $4, %edx
- jmp 1b
- 2:
- /*
- * Do the decompression, and jump to the new kernel..
- */
- leal z_extract_offset_negative(%ebx), %ebp //ebp=ebx-0x4a0000=0x100000
- /* push arguments for decompress_kernel: */
- pushl %ebp /* output address */ //將Kernel解壓縮到0x100000
- pushl $z_input_len /* input_len */ //壓縮過的kernel大小
- leal input_data(%ebx), %eax //壓縮kernel開始地址
- pushl %eax /* input_data */
- leal boot_heap(%ebx), %eax //工作的堆
- pushl %eax /* heap area */
- pushl %esi /* real mode pointer */ //esi是boot_params
- call decompress_kernel //我不準(zhǔn)備去看怎么解壓,只要知道它解壓了好了
- addl $20, %esp //看來不需要恢復(fù)寄存器
- #if CONFIG_RELOCATABLE
- /*
- * Find the address of the relocations.
- */
- leal z_output_len(%ebp), %edi
- /*
- * Calculate the delta between where vmlinux was compiled to run
- * and where it was actually loaded.
- */
- movl %ebp, %ebx
- subl $LOAD_PHYSICAL_ADDR, %ebx
- jz 2f /* Nothing to be done if loaded at compiled addr. */ //如果ebx=0x100000,則不許要reloc
- /*
- * Process relocations. //這段沒懂,但應(yīng)該不影響理解
- */
- 1: subl $4, %edi
- movl (%edi), %ecx
- testl %ecx, %ecx
- jz 2f
- addl %ebx, -__PAGE_OFFSET(%ebx, %ecx)
- jmp 1b
- 2:
- #endif
- /*
- * Jump to the decompressed kernel.
- */
- xorl %ebx, %ebx
- jmp *%ebp //重新跳到0x100000開始。
- /*
- * Stack and heap for uncompression
- */
- .bss
- .balign 4
- boot_heap:
- .fill BOOT_HEAP_SIZE, 1, 0
- boot_stack:
- .fill BOOT_STACK_SIZE, 1, 0
- boot_stack_end:
把以上的代碼總結(jié)一下其實(shí)很簡單,startup_32將壓縮過的kernel和本身移動到0x100000(或bootloader裝載startup_32的地址)+0x4a0000的位置,然后解壓縮kernel回到0x100000(或bootloader裝載startup_32的地址),然后將控制權(quán)交回到0x100000((或bootloader裝載startup_32的地址)).
啟動代碼終于結(jié)束了,明天就要進(jìn)入真正的kernel了。寫得有些亂,就是對代碼進(jìn)行注釋,暫時(shí)找不到更好的方法來對代碼進(jìn)行解釋,可能使大家看起來有點(diǎn)累。希望對大家有幫助
|