手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux0.11内核--启动引导代码分析setup.s

[日期:2012-04-26] 来源:Linux社区  作者:闫明 [字体:大 中 小]

下面开启A20地址线

操作系统从实模式转换成保护模式时需要开启A20地址线,那什么是A20地址线呢?

在8086/8088CPU中,地址线只有20根,而寄存器都是16位的,为了能够使用20位地址线,它采用段地址:偏移地址的形式,寻址范围就可以达到1M,但是到了80286地址线达到24根,80386达到32根寻址能力达到4GB。为了向后兼容,IBM采用了一个控制方法:用键盘

控制器上的一个空闲的控制线来控制。即A20控制线。当A20打开时可以使用20-31的地址线,如果A20没有打开,后面的地址线就不可以使用,全部为0。

所以,激活A20地址线的流程为:

    1.关闭中断;

    2.等待8042 Input buffer为空;

    3.发送禁止键盘操作命令;

    4.等待8042 Input buffer为空;

    5.发送读取8042 Output Port命令;

    6.等待8042 Output buffer有数据;

    7.读取8042 Output buffer,并保存得到的字节;

    8.等待8042 Input buffer为空;

    9.发送Write 8042 Output Port命令到8042 Input buffer;

    10.等待8042 Input buffer为空;

    11.将从8042 Output Port得到的字节的第2位置1(或清0),然后写入8042 Input buffer;

    12.等待,直到8042 Input buffer为空为止;

    13.发送允许键盘操作命令到8042 Input buffer;

    14. 打开中断。

下面是as86的汇编代码(linux0.11内核中)

  1. call    empty_8042  
  2. mov al,#0xD1        ! command write  
  3. out #0x64,al  
  4. call    empty_8042  
  5. mov al,#0xDF        ! A20 on  
  6. out #0x60,al  
  7. call    empty_8042  
  8.   
  9. mpty_8042:  
  10. .word   0x00eb,0x00eb  
  11. in  al,#0x64    ! 8042 status port  
  12. test    al,#2       ! is input buffer full?  
  13. jnz empty_8042  ! yes - loop  
  14. ret  

然后对中断进行编程

实际上就是设置8259中断控制器的ICW字

  1. mov $0x11, %al      # initialization sequence(ICW1)  
  2.                 # ICW4 needed(1),CASCADE mode,Level-triggered   
  3. out %al, $0x20      # send it to 8259A-1  
  4. .word   0x00eb,0x00eb       # jmp $+2, jmp $+2  
  5. out %al, $0xA0      # and to 8259A-2  
  6. .word   0x00eb,0x00eb  
  7. mov $0x20, %al      # start of hardware int's (0x20)(ICW2)  
  8. out %al, $0x21      # from 0x20-0x27  
  9. .word   0x00eb,0x00eb  
  10. mov $0x28, %al      # start of hardware int's 2 (0x28)  
  11. out %al, $0xA1      # from 0x28-0x2F  
  12. .word   0x00eb,0x00eb       #               IR 7654 3210  
  13. mov $0x04, %al      # 8259-1 is master(0000 0100) --/  
  14. out %al, $0x21      #               |  
  15. .word   0x00eb,0x00eb       #            INT    /  
  16. mov $0x02, %al      # 8259-2 is slave(       010 --> 2)  
  17. out %al, $0xA1  
  18. .word   0x00eb,0x00eb  
  19. mov $0x01, %al      # 8086 mode for both  
  20. out %al, $0x21  
  21. .word   0x00eb,0x00eb  
  22. out %al, $0xA1  
  23. .word   0x00eb,0x00eb  
  24. mov $0xFF, %al      # mask off all interrupts for now  
  25. out %al, $0x21  
  26. .word   0x00eb,0x00eb  
  27. out %al, $0xA1  

其中.word 0x00eb是直接进跳转指令的操作码,带一个字节的相对移位值,移动范围是-127--+127CPU通过将这个移位值加到EIP寄

存器,形成新的地址。实际上就是起到延迟的作用。

最后,进入32位的保护模式运行,首先加载机器状态字,CR0,其比特位0置1表示CPU工作在保护模式下。

下面分别是AS86格式和AT&T汇编格式

  1. mov ax,#0x0001  ! protected mode (PE) bit  
  2. lmsw    ax      ! This is it!  
  3. jmpi    0,8     ! jmp offset 0 of segment 8 (cs)  

 

  1. mov %cr0, %eax  # get machine status(cr0|MSW)     
  2. bts $0, %eax    # turn on the PE-bit   
  3. mov %eax, %cr0  # protection enabled  
  4.               
  5.             # segment-descriptor        (INDEX:TI:RPL)   
  6. .equ    sel_cs0, 0x0008 # select for code segment 0 (  001:0 :00)   
  7. ljmp    $sel_cs0, $0    # jmp offset 0 of code segment 0 in gdt  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款