新手学linux之三————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.
 */
 
//预备知识
//在看本程序之前  我们需要搞懂逻辑地址 虚拟地址 物理地址 以及他们之间的关系
//参考:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=2083672

//NOTE:这个程序比较好懂  只需熟悉x86的保护模式相关硬件 熟悉相关的AT&T语法

//建议:我有一本比较好的书 <<80386保护模式下编程>> 需要的在评论里留下邮箱 我发给你

/*
该程序功能总结:
1. 设置内核堆栈;
2. 设置中断描述符表idt;
3. 设置全局描述符表gdt;
4. 设置页目录表和页表;
5. 将/init/main.c程序的入口地址预先压入堆栈中,并在随后利用返回指令弹出该地址,去执行main()程序。
*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir:
startup_32:
//注意:应在保护模式下(通过GDT)思考内存的问题
    movl $0x10,%eax
    mov %ax,%ds        ;把代码中的段选择子设置为特权级为零,指向GDT,GDT的第二项
    mov %ax,%es        ;注意参考setup.S中设置的相关GDTR和GDT的值
    mov %ax,%fs        
    mov %ax,%gs        
    lss _stack_start,%esp    ;lss指令表示把stack_start->ss:esp ss为0x10 esp为0x1e25c
    call setup_idt
    call setup_gdt
    movl $0x10,%eax        # reload all the segment registers
    mov %ax,%ds            # after changing gdt. CS was already
    mov %ax,%es            # reloaded in 'setup_gdt'
    mov %ax,%fs            //因为重新加载了GDT IDT所以需要重新加载段选择子
    mov %ax,%gs        
    lss _stack_start,%esp    
    xorl %eax,%eax
//下面开始验证A20是否开启
//具体方法是让
//如果没有开启A20那么[0x0]=[0x100000](1M)=eax 那么就陷入死循环
1:    incl %eax        # check that A20 really IS enabled
    movl %eax,0x000000    # loop forever if it isn't
    cmpl %eax,0x100000    ;这里的0x0 和 0x100000就是内存中那个地址
    je 1b                //b表示backward 向后跳转1 标号可以有0-10 f表示forward
/*
 * 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
    movl %eax,%cr0
    call check_x87
    jmp after_page_tables
/head.S完

/*
 * We depend on ET to be correct. This checks for 287/387.
 */
check_x87:
    fninit            //init协处理器
    fstsw %ax        //将协处理器的状态字发送到ax
    cmpb $0,%al
    je 1f            /* no coprocessor: have to set bits */
    movl %cr0,%eax
    xorl $6,%eax        /* reset MP, set EM */
    movl %eax,%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:
    lea ignore_int,%edx        //lea/lds/lfs/lss/lgs等指令
    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:
    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
 * more than 16MB will have to expand this.
 */
.org 0x1000
pg0:

.org 0x2000
pg1:

.org 0x3000
pg2:

.org 0x4000
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 :-)
    pushl $0
    pushl $0
    pushl $L6        # return address for main, if it decides to.
    pushl $_main
    jmp setup_paging
L6:
    jmp L6            # main should never return here, but
                # just in case, we know what happens.

/* This is the default interrupt "handler" :-) */
int_msg:
    .asciz "Unknown interrupt\n\r"
.align 2
//调用内核函数printk显示int_msg
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    ;该函数在/kernel/printk.c中
    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
/*设置页面后 其内存地址空间如下
        ...
        ...
        ...
        main.c
        gdt(2K)
        idt(2K)
        head.s部分代码    
0x5000 软盘缓冲区  即上面的(tmp_floppy_erea)
0x4000 页表3    (以下代码都是覆盖head.s本身来创建的 需要常驻内存)
...
...
0x1000 页表0
0x0000 页目录(4kB)
*/
setup_paging:
    movl $1024*5,%ecx        /* 5 pages - pg_dir+4 page tables */    //1K+4K
    xorl %eax,%eax
    xorl %edi,%edi            /* pg_dir is at 0x000 */
    cld;rep;stosl            //清方向位 循环
    //这里是设置页目录 并且设置每个页表都是 bit/user r/w的
    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        /*  --------- " " --------- */
    
    //这里设置的是4个页表项
    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    //stosl指令将eax的值保存到es:edi指向的空间 stosl会自动调整edi
    jge 1b
    //cr3寄存器中包含页的基址 设为0x0
    xorl %eax,%eax        /* pg_dir is at 0x0000 */
    movl %eax,%cr3        /* cr3 - page directory start */
    //设置cr0 开启分页机制
    movl %cr0,%eax
    orl $0x80000000,%eax
    movl %eax,%cr0        /* set paging (PG) bit */
    ret            /* this also flushes prefetch-queue */    //跳到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

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

转载于:https://my.oschina.net/lngligelang/blog/223200

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值