linux内核学习(12)启动全过程概述之三

进入了vmlinux内核映像部分,就进入了arch/x86/compressed/head_32.S。这个文件主要工作就是解压了,将真正的内核给释放出来,不过还得把这个文件分析完,内核才能出世。

来自arch/x86/compressed/head_32.S:

........省略了头文件......

__HEAD
ENTRY(startup_32)
    cld           /*di,si++*/
    /*
     * Test KEEP_SEGMENTS flag to see if the bootloader is asking
     * us to not reload segments
     */
    testb    $(1<<6), BP_loadflags(%esi)   /*esi = boot_params*/
    jnz    1f
# esi是之前就设置好了的,boot_params结构体的首地址,而loadflags的位6如果置位,则表示需要将段寄存器重新载入
    cli        /*close interrupt*/
    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.
 */
    leal    (BP_scratch+4)(%esi), %esp  /*boot_params->scratch*/
    call    1f
1:    popl    %ebp
    subl    $1b, %ebp
# 上面这段程序就是找到vmlinux加载的首地址,将它存入ebp中。
# boot_params->scratch只是一个中间变量而已。
# 这里牵涉到eip代码指针,当我们call时,其实发生了一些事情,
# 第一步:eip = eip + (1f-当前指针偏移)
# 第二步:pushl eip
# 第三步:跳到cs:eip执行指令
# 估计现在你应该懂了吧。
/*
 * %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     
    movl    BP_kernel_alignment(%esi), %eax
    decl    %eax
    addl    %eax, %ebx
    notl    %eax
    andl    %eax, %ebx         /*the kernel image align*/
# 代码啊,看着简单,其实要知道作用还得多分析分析。
# 作用:将ebp地址按照kernel_alignment字节对齐,传递给ebx,
# 这样ebx就对齐了。
# 希望你能看懂。
#else
    movl    $LOAD_PHYSICAL_ADDR, %ebx
#endif

    /* Target address to relocate to for decompression */
    addl    $z_extract_offset, %ebx       /*add to offset*/
# 这样一加,就确定了 解压内核的临时内存地址

    /* Set up the stack */
    leal    boot_stack_end(%ebx), %esp
# 设置一下栈,注意现在这个栈离我们蛮远的

    /* Zero EFLAGS */  # 清标志寄存器eflags
    pushl    $0
    popfl

/*
 * Copy the compressed kernel to the end of our buffer
 * where decompression in place becomes safe.
 */
# 这段代码可以说是这个文件中的精华了,作用很简单将vmlinux拷贝到安全地带,
# 也就是上面确定的ebx。
# 要读懂,首先要了解什么是一个程序的bss段?
# bss段就是未被初始化的全局静态变量存放的地方,比如
# static int x;没有被初始化,会被放入bss段。
# 一般它的位置在.text、.data段的后面。
# 注意这里的bss段要和代码里最下面那个.bss区分开来。

    pushl    %esi           # 保存好
    leal    (_bss-4)(%ebp), %esi          # 源内核末尾地址
    leal    (_bss-4)(%ebx), %edi          # 目的内核末尾地址
# 这里为什么要-4,其实也好理解,因为一开始就执行拷贝了,
# 因此首先源地址和目的地址处就得有内容。
    movl    $(_bss - startup_32), %ecx     # 计算出需要拷贝的字节数目,很明显拷贝很完善
    shrl    $2, %ecx     /*right move 4 bits*/   # 一次拷贝4个字节
    std         /*di,si --*/
    rep    movsl
    cld          /*di,si ++*/
    popl    %esi     # 取出来

/*
 * Jump to the relocated address.
 */
# 这里开始得注意了,一跳转后,就在临时内核里执行了
# 不过执行的内容还是一样而已。
    leal    relocated(%ebx), %eax
    jmp    *%eax    # AT@T汇编跳转得在目标前加上*号
ENDPROC(startup_32)

    .text
relocated:
# 进入了临时内核了
/*
 * Clear BSS (stack is currently empty)
 */
# 给bss段清0,那个_ebss故名意思就是end bss。
    xorl    %eax, %eax
    leal    _bss(%ebx), %edi
    leal    _ebss(%ebx), %ecx
    subl    %edi, %ecx
    shrl    $2, %ecx
    rep    stosl`

/*
 * Adjust our own GOT
 */
# 不要管
    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..
 */
# 这段看似有些复杂,其实很简单,调用解压函数而已
# 而且这里终于符合C调用约定了
    leal    z_extract_offset_negative(%ebx), %ebp
                /* push arguments for decompress_kernel: */
    pushl    %ebp        /* output address */   # 解压后输出地址,就是最开始的内核加载地址
    pushl    $z_input_len    /* input_len */     # 长度
    leal    input_data(%ebx), %eax               # 压缩内核地址
    pushl    %eax        /* input_data */            
    leal    boot_heap(%ebx), %eax              # 如果需要用到堆内存
    pushl    %eax        /* heap area */
    pushl    %esi        /* real mode pointer */  /*boot_params struct*/      # 这个简单,就是实模式下的启动参数结构体
    call    decompress_kernel     # 调用一下,轻松搞定,估计里面不会轻松,有兴趣可以研究一下,这里就飘过~~
    addl    $20, %esp    # 恢复栈,注意入栈,esp向低内存延伸。

#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. */
/*
 * Process relocations.
 */

1:    subl    $4, %edi
    movl    (%edi), %ecx
    testl    %ecx, %ecx      /*if ecx==0 jump*/
    jz    2f
    addl    %ebx, -__PAGE_OFFSET(%ebx, %ecx)
    jmp    1b
2:
#endif

/*
 * Jump to the decompressed kernel.
 */
# 正式跳入kernel,哈哈!终于即将看到真实内核的身影啦!
    xorl    %ebx, %ebx
    jmp    *%ebp

/*
 * 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:

冬天走了,春天来了,呵呵!是我们的春天,我们一步一步在逼近自己的目标,我们在实现!不晓得现在的你是否和我一样感受到巨大的力量在自己的胸口徘徊呢,坚持就是万劫不复的源泉。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值