Linux0.11版源代码分析——boot/head.s

Linux0.11版源代码分析第三篇,head.s源码注释分析。

/*
 *  linux/boot/head.s
 *
 *  (C) 1991  Linus Torvalds
 */

/*
 *  head.s contains the 32-bit startup code.
 *
 * NOTE!!! Startup happens at absolute address 0x00000000, which is also where
 * the page directory will exist. The startup code will be overwritten by
 * the page directory.
 */
 
 /*
 *  head.s 包含32位启动代码.
 *
 * 注意!!! 启动代码位于绝对地址0x00000000处,将来也是页目录所在
 * 启动代码将被页目录所覆盖.
 */
 
 !定义全局标识符
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir:                                                   !页目录所在处
startup_32:                                                !启动(32位模式)
    movl $0x10,%eax                                          !段选择符
    mov %ax,%ds                                              !设置段寄存器
    mov %ax,%es
    mov %ax,%fs
    mov %ax,%gs
    lss _stack_start,%esp                                    !加载系统堆栈
    call setup_idt                                           !加载中断描述符表
    call setup_gdt                                           !加载全局描述符表
    movl $0x10,%eax        # reload all the segment registers     !重新设置段寄存器
    mov %ax,%ds        # after changing gdt. CS was already       !在改变gdt后,cs已经
    mov %ax,%es        # reloaded in 'setup_gdt'                  !重新加载在setup_gdt
    mov %ax,%fs                                              !
    mov %ax,%gs                                              !
    lss _stack_start,%esp                                    !加载系统堆栈
    xorl %eax,%eax                                           !清空eax
1:    incl %eax        # check that A20 really IS enabled         !检测A20地址线
    movl %eax,0x000000    # loop forever if it isn't
    cmpl %eax,0x100000
    je 1b
/*
 * NOTE! 486 should set bit 16, to check for write-protect in supervisor
 * mode. Then it would be unnecessary with the "verify_area()"-calls.
 * 486 users probably want to set the NE (#5) bit also, so as to use
 * int 16 for math errors.
 */
    movl %cr0,%eax        # check math chip                      !检测数学芯片
    andl $0x80000011,%eax    # Save PG,PE,ET
/* "orl $0x10020,%eax" here for 486 might be good */
    orl $2,%eax        # set MP                                   !设置mp
    movl %eax,%cr0                                           !
    call check_x87                                           !调用check_x87
    jmp after_page_tables                                    !跳转到after_page_tables,设置目录页表

/*
 * We depend on ET to be correct. This checks for 287/387. !通过ET检测数学协处理器
 */
check_x87:                                                 !检测x87芯片
    fninit                                                   !初始化协处理器
    fstsw %ax                                                !将该处理器状态放置到ax中
    cmpb $0,%al                                              !比较,如果是0,则不存在
    je 1f            /* no coprocessor: have to set bits */         !不存在,则跳转到标号1处
    movl %cr0,%eax                                           !将cr0寄存器内容复制到eax
    xorl $6,%eax        /* reset MP, set EM */                   !和6进行异或
    movl %eax,%cr0                                           !复制到cr0
    ret
.align 2
1:    .byte 0xDB,0xE4        /* fsetpm for 287, ignored by 387 */
    ret

/*
 *  setup_idt
 *
 *  sets up a idt with 256 entries pointing to
 *  ignore_int, interrupt gates. It then loads
 *  idt. Everything that wants to install itself
 *  in the idt-table may do so themselves. Interrupts
 *  are enabled elsewhere, when we can be relatively
 *  sure everything is ok. This routine will be over-
 *  written by the page tables.
 */
setup_idt:                                                          !设置idt
    lea ignore_int,%edx
    movl $0x00080000,%eax
    movw %dx,%ax        /* selector = 0x0008 = cs */
    movw $0x8E00,%dx    /* interrupt gate - dpl=0, present */

    lea _idt,%edi
    mov $256,%ecx
rp_sidt:                                                            !
    movl %eax,(%edi)
    movl %edx,4(%edi)
    addl $8,%edi
    dec %ecx
    jne rp_sidt
    lidt idt_descr
    ret

/*
 *  setup_gdt
 *
 *  This routines sets up a new gdt and loads it.
 *  Only two entries are currently built, the same
 *  ones that were built in init.s. The routine
 *  is VERY complicated at two whole lines, so this
 *  rather long comment is certainly needed :-).
 *  This routine will beoverwritten by the page tables.
 */
setup_gdt:                                                            !设置gdt
    lgdt gdt_descr
    ret

/*
 * I put the kernel page tables right after the page directory,             !我放置内核页表在页目录之后
 * using 4 of them to span 16 Mb of physical memory. People with            !使用16M物理内存中的4M。如果
 * more than 16MB will have to expand this.                                 !你有超过16M内存可以自己扩展
 */
.org 0x1000                                                                 !偏移为0x1000为表1
pg0:

.org 0x2000                                                                 !偏移为0x2000出放置表2
pg1:
 
.org 0x3000                                                                 !放置表3
pg2:

.org 0x4000                                                                 !放置表4
pg3:

.org 0x5000
/*
 * tmp_floppy_area is used by the floppy-driver when DMA cannot
 * reach to a buffer-block. It needs to be aligned, so that it isn't
 * on a 64kB border.
 */
_tmp_floppy_area:
    .fill 1024,1,0

after_page_tables:                                                    !设置页目录表
    pushl $0        # These are the parameters to main :-)                  !main函数的参数,但是实际上main函数不要参数,所以设置为0
    pushl $0
    pushl $0
    pushl $L6        # return address for main, if it decides to.            !main函数返回地址
    pushl $_main                                                        !setup_paging返回地址,即setup_paging返回后执行main函数
    jmp setup_paging                                                    !跳转到setup_paging
L6:
    jmp L6            # main should never return here, but                    !main函数将永远不会返回到这里,但是一旦进入到这里,我们
                # just in case, we know what happens.                         !将知道发生什么了

/* This is the default interrupt "handler" :-) */                     !这是默认的中断处理
int_msg:                                                              !中断消息
    .asciz "Unknown interrupt\n\r"
.align 2
ignore_int:
    pushl %eax
    pushl %ecx
    pushl %edx
    push %ds
    push %es
    push %fs
    movl $0x10,%eax
    mov %ax,%ds
    mov %ax,%es
    mov %ax,%fs
    pushl $int_msg
    call _printk
    popl %eax
    pop %fs
    pop %es
    pop %ds
    popl %edx
    popl %ecx
    popl %eax
    iret                                          //中断返回


/*
 * Setup_paging
 *
 * This routine sets up paging by setting the page bit
 * in cr0. The page tables are set up, identity-mapping
 * the first 16MB. The pager assumes that no illegal
 * addresses are produced (ie >4Mb on a 4Mb machine).
 *
 * NOTE! Although all physical memory should be identity
 * mapped by this routine, only the kernel page functions
 * use the >1Mb addresses directly. All "normal" functions
 * use just the lower 1Mb, or the local data space, which
 * will be mapped to some other place - mm keeps track of
 * that.
 *
 * For those with more memory than 16 Mb - tough luck. I've
 * not got it, why should you :-) The source is here. Change
 * it. (Seriously - it shouldn't be too difficult. Mostly
 * change some constants etc. I left it at 16Mb, as my machine
 * even cannot be extended past that (ok, but it was cheap :-)
 * I've tried to show which constants to change by having
 * some kind of marker at them (search for "16Mb"), but I
 * won't guarantee that's all :-( )
 */
.align 2
setup_paging:                                                           !设置页目录表
    movl $1024*5,%ecx        /* 5 pages - pg_dir+4 page tables */        !五页,一个页目录表+四个页表,实际上作为计数器为下面循环计数
    xorl %eax,%eax                                                      !清空eax
    xorl %edi,%edi            /* pg_dir is at 0x000 */                    !清空edi
    cld;rep;stosl                                                       !清楚方向位标识,以四字节为单位循环将eax中的数据拷贝到es:edi中,即将前五页内存清零
    movl $pg0+7,_pg_dir        /* set present bit/user r/w */              !设置页目录表
    movl $pg1+7,_pg_dir+4        /*  --------- " " --------- */
    movl $pg2+7,_pg_dir+8        /*  --------- " " --------- */
    movl $pg3+7,_pg_dir+12        /*  --------- " " --------- */
    movl $pg3+4092,%edi                                                 !也表中最后一项
    movl $0xfff007,%eax        /*  16Mb - 4096 + 7 (r/w user,p) */         !页表填充
    std
1:    stosl            /* fill pages backwards - more efficient :-) */
    subl $0x1000,%eax
    jge 1b
    xorl %eax,%eax        /* pg_dir is at 0x0000 */                       !设置页目录表起始地址
    movl %eax,%cr3        /* cr3 - page directory start */
    movl %cr0,%eax                                                      !启动分页处理机制
    orl $0x80000000,%eax
    movl %eax,%cr0        /* set paging (PG) bit */
    ret            /* this also flushes prefetch-queue */                  !将main地址弹出栈,从而进行main函数执行

.align 2
.word 0
idt_descr:                                                              !中断描述符表
    .word 256*8-1        # idt contains 256 entries
    .long _idt
.align 2
.word 0
gdt_descr:                                                              !全局段描述符表
    .word 256*8-1        # so does gdt (not that that's any              !
    .long _gdt        # magic number, but it works for me :^)

    .align 3
_idt:    .fill 256,8,0        # idt is uninitialized                      !中断描述符256个未初始化

_gdt:    .quad 0x0000000000000000    /* NULL descriptor */               !空选择符
    .quad 0x00c09a0000000fff    /* 16Mb */                              !代码段
    .quad 0x00c0920000000fff    /* 16Mb */                              !数据段
    .quad 0x0000000000000000    /* TEMPORARY - don't use */             !临时的,不用
    .fill 252,8,0            /* space for LDT's and TSS's etc */         !填充252个8字节的段选择符为0

 至此,终于将boot文件夹中的三个文件注视,完毕,基本理解了Linux操作系统的boot机制。下一步,分析init文件夹中的代码。

posted @ 2012-09-01 15:24  微笑风  阅读(437)  评论(0编辑  收藏  举报