无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站广告联系 微信:wuyouceo QQ:184822951
查看: 2105|回复: 1
打印 上一主题 下一主题

FreeDOS 代码学习

[复制链接]
跳转到指定楼层
1#
发表于 2014-12-13 10:02:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
以下是 kernel.asm 中的片段。代码显示出有关中断向量的重要信息。

  1.         ; dummy interrupt return handlers

  2.                 global _int22_handler
  3.                 global _int28_handler
  4.                 global _int2a_handler
  5.                 global _empty_handler
  6. _int22_handler:         
  7. _int28_handler:
  8. _int2a_handler:
  9. _empty_handler:
  10.                 iret
  11.    

  12. global _initforceEnableA20
  13. initforceEnableA20:
  14.                 call near forceEnableA20
  15.                 retf   

  16.     global __HMARelocationTableStart
  17. __HMARelocationTableStart:   

  18.                 global  _int2f_handler
  19.                 extern  reloc_call_int2f_handler
  20. _int2f_handler: jmp 0:reloc_call_int2f_handler
  21.                 call near forceEnableA20

  22.                 global  _int20_handler
  23.                 extern  reloc_call_int20_handler
  24. _int20_handler: jmp 0:reloc_call_int20_handler
  25.                 call near forceEnableA20

  26.                 global  _int21_handler
  27.                 extern  reloc_call_int21_handler
  28. _int21_handler: jmp 0:reloc_call_int21_handler
  29.                 call near forceEnableA20


  30.                 global  _low_int25_handler
  31.                 extern  reloc_call_low_int25_handler
  32. _low_int25_handler: jmp 0:reloc_call_low_int25_handler
  33.                 call near forceEnableA20

  34.                 global  _low_int26_handler
  35.                 extern  reloc_call_low_int26_handler
  36. _low_int26_handler: jmp 0:reloc_call_low_int26_handler
  37.                 call near forceEnableA20

  38.                 global  _int27_handler
  39.                 extern  reloc_call_int27_handler
  40. _int27_handler: jmp 0:reloc_call_int27_handler
  41.                 call near forceEnableA20

  42.                 global  _int0_handler
  43.                 extern  reloc_call_int0_handler
  44. _int0_handler:  jmp 0:reloc_call_int0_handler
  45.                 call near forceEnableA20

  46.                 global  _int6_handler
  47.                 extern  reloc_call_int6_handler
  48. _int6_handler:  jmp 0:reloc_call_int6_handler
  49.                 call near forceEnableA20

  50.                 global  _int19_handler
  51.                 extern  reloc_call_int19_handler
  52. _int19_handler: jmp 0:reloc_call_int19_handler
  53.                 call near forceEnableA20

  54.                 global  _cpm_entry
  55.                 extern  reloc_call_cpm_entry
  56. _cpm_entry:     jmp 0:reloc_call_cpm_entry
  57.                 call near forceEnableA20

  58.                 global  _reloc_call_blk_driver
  59.                 extern  _blk_driver
  60. _reloc_call_blk_driver:
  61.                 jmp 0:_blk_driver
  62.                 call near forceEnableA20

  63.                 global  _reloc_call_clk_driver
  64.                 extern  _clk_driver
  65. _reloc_call_clk_driver:
  66.                 jmp 0:_clk_driver
  67.                 call near forceEnableA20

  68.                 global  _CharMapSrvc ; in _DATA (see AARD)
  69.                 extern  _reloc_call_CharMapSrvc
  70. _CharMapSrvc:   jmp 0:_reloc_call_CharMapSrvc
  71.                 call near forceEnableA20

  72.                 global _init_call_p_0
  73.                 extern reloc_call_p_0
  74. _init_call_p_0: jmp  0:reloc_call_p_0
  75.                 call near forceEnableA20
复制代码
2#
 楼主| 发表于 2014-12-18 11:42:10 | 只看该作者
以下是 grub4dos 有关实模式与保护模式环境切换的关键接口函数。

  1. /*
  2. * long realmode_run(long regs_ptr)
  3. *
  4. */

  5. ENTRY(realmode_run)

  6.         .code32

  7.         pushl        %ebp
  8.         movl        %esp, %ebp

  9.         pushl        %ebx
  10.         movl        8(%ebp), %ebx                /* EBX=regs_ptr */

  11.         /* check sanity */

  12.         xorl        %eax, %eax                /* EAX=0 for failure */

  13.         movl        0x20(%ebx), %ecx        /* GS */
  14.         incl        %ecx
  15.         jz        1f
  16.         decl        %ecx
  17.         shrl        $16, %ecx
  18.         jnz        2f                        /* failure */
  19. 1:

  20.         movl        0x24(%ebx), %ecx        /* FS */
  21.         incl        %ecx
  22.         jz        1f
  23.         decl        %ecx
  24.         shrl        $16, %ecx
  25.         jnz        2f                        /* failure */
  26. 1:

  27.         movl        0x28(%ebx), %ecx        /* ES */
  28.         incl        %ecx
  29.         jz        1f
  30.         decl        %ecx
  31.         shrl        $16, %ecx
  32.         jnz        2f                        /* failure */
  33. 1:

  34.         movl        0x2C(%ebx), %ecx        /* DS */
  35.         incl        %ecx
  36.         jz        1f
  37.         decl        %ecx
  38.         shrl        $16, %ecx
  39.         jnz        2f                        /* failure */
  40. 1:

  41.         movl        0x30(%ebx), %ecx        /* SS */
  42.         movl        0x0C(%ebx), %edx        /* ESP */
  43.         incl        %ecx
  44.         jz        1f
  45.         decl        %ecx
  46.         orl        %edx, %ecx
  47.         shrl        $16, %ecx
  48.         jnz        2f                        /* failure */
  49.         jmp        3f
  50. 1:
  51.         incl        %edx
  52.         jnz        2f                        /* failure */
  53. 3:

  54.         movl        0x38(%ebx), %ecx        /* CS */
  55.         movl        0x34(%ebx), %edx        /* EIP */
  56.         incl        %ecx
  57.         jz        1f
  58.         decl        %ecx
  59.         orl        %edx, %ecx
  60.         shrl        $16, %ecx
  61.         jnz        2f                        /* failure */
  62.         jmp        3f
  63. 1:
  64.         cmpb        $0xCD, %dl
  65.         jnz        2f                        /* failure */
  66.         shrl        $16, %edx
  67.         incw        %dx
  68.         jnz        2f                        /* failure */
  69. 3:
  70.         /* sanity check ok, run it! */

  71.         pushal
  72.         pushal
  73.         pushal
  74.         pushal
  75.         movl        %esp, ABS(chain_stage1_esp)

  76.         /* mov the struct to stack */
  77.         subl        $64, %esp
  78.         movl        %esp, %edi
  79.         movl        %ebx, %esi
  80.         movl        $16, %ecx
  81.         cld
  82.         repz movsl

  83.         movl        %esp, %ebp        /* EBP point to struct on stack */

  84.         call        EXT_C(prot_to_real)

  85.         .code16

  86.         sti

  87.         movl        %ebp, %esp

  88.         /* check CS:EIP */

  89.         movl        0x38(%bp), %ecx                /* CS */
  90.         movl        0x34(%bp), %edx                /* EIP */
  91.         incl        %ecx                        /* CS = -1? */
  92.         jz        3f                        /* yes, jump to INTxx code */

  93.         /* run user defined code */
  94.         decl        %ecx                        /* CS */
  95.         /* 0xEA might have been changed to 0xCD previously, so we must restore
  96.          * it back to 0xEA.
  97.          */
  98.         movb        $0xEA, ABS(1f-1)        /* opcode for "far jump" */
  99.         movw        %dx, ABS(1f)                /* IP in the far jump instruction */
  100.         movw        %cx, ABS(1f + 2)        /* CS in the far jump instruction */

  101.         /* set return address for user code to return */
  102.         movl        $ABS(4f), %ecx
  103.         movl        %ecx, 0x8201
  104.         jmp        6f
  105. 3:
  106.         /* run int xx */
  107.         movw        %dx, ABS(1f - 1)        /* int XX */
  108.         movb        $0xE9, ABS(1f + 1)        /* jmp */
  109.         movw        $(4f - 1f - 4), ABS(1f + 2)
  110. 6:
  111.         movw        %sp, ABS(8f)                /* initial SP */
  112.         movw        %ss, ABS(7f)                /* initial SS */

  113.         /* set DS, ES, FS, GS */
  114.         movl        0x20(%bp), %ecx                /* GS */
  115.         incl        %ecx
  116.         jz        5f
  117.         decl        %ecx
  118.         movw        %cx, %gs
  119. 5:

  120.         movl        0x24(%bp), %ecx                /* FS */
  121.         incl        %ecx
  122.         jz        5f
  123.         decl        %ecx
  124.         movw        %cx, %fs
  125. 5:

  126.         movl        0x28(%bp), %ecx                /* ES */
  127.         incl        %ecx
  128.         jz        5f
  129.         decl        %ecx
  130.         movw        %cx, %es
  131. 5:

  132.         movl        0x2C(%bp), %ecx                /* DS */
  133.         incl        %ecx
  134.         jz        5f
  135.         decl        %ecx
  136.         movw        %cx, %ds
  137. 5:

  138.         movl        0x30(%bp), %ecx                /* SS */
  139.         incl        %ecx
  140.         jz        5f
  141.         decl        %ecx
  142.         movw        %cx, ABS(7f)
  143.         movw        0x0C(%bp), %dx                /* SP */
  144.         movw        %dx, ABS(8f)
  145. 5:
  146.         movl        0x3C(%bp), %ecx                /* EFLAGS */
  147.         incl        %ecx
  148.         jz        5f
  149.         decl        %ecx
  150.         pushl        %ecx
  151.         popfl
  152. 5:
  153.         popal                                /* setup general registers */

  154.         /* move SS and SP */
  155.         .byte        0xBC                        /* MOV SP */
  156. 7:
  157.         .word        0                        /* SS */
  158.         movw        %sp, %ss
  159.         .byte        0xBC                        /* MOV SP */
  160. 8:
  161.         .word        0                        /* SP */

  162.         /* far jump to user code */
  163.         .byte        0xEA                        /* ljmp */
  164. 1:
  165.         .word        0                        /* IP */
  166.         .word        0                        /* CS */

  167. 4:        /* return from user code */

  168.         /* CS:IP need not care */

  169.         /* use the stack already built by the user */

  170.         pushal
  171.         pushfl
  172.         pushl        %cs
  173.         pushl        $0
  174.         pushl        %ss
  175.         pushl        %ds
  176.         pushl        %es
  177.         pushl        %fs
  178.         pushl        %gs

  179.         movl        %ss, %ebp
  180.         shll        $4, %ebp
  181.         xorl        %eax, %eax
  182.         movw        %ax, %ds
  183.         movw        %ax, %es
  184.         movw        %sp, %ax
  185.         addl        %eax, %ebp                /* physical address of stack */

  186.         call        EXT_C(real_to_prot)

  187.         .code32

  188.         sti

  189.         cld
  190.         movl        %ebp, %esi

  191.         movl        ABS(chain_stage1_esp), %esp
  192.         movl        16(%esp), %ebx                /* EBX=regs_ptr */

  193.         leal        32(%ebx), %edi
  194.         movl        $8, %ecx
  195.         repz movsl

  196.         leal        32(%ebp), %esi
  197.         movl        %ebx, %edi
  198.         movl        $8, %ecx
  199.         repz movsl

  200.         /* the stack should not change! */

  201.         /* fix the pushed ESPs before checking */
  202.         movl        $32, %ecx
  203.         subl        %ecx, (32 + 12)(%esp)
  204.         movl        $64, %ecx
  205.         subl        %ecx, (64 + 12)(%esp)
  206.         movl        $96, %ecx
  207.         subl        %ecx, (96 + 12)(%esp)

  208.         movl        %esp, %esi
  209.         leal        32(%esp), %edi
  210.         movl        $8, %ecx
  211.         repz cmpsl
  212.         jnz        1f
  213.         movl        %esp, %esi
  214.         leal        64(%esp), %edi
  215.         movl        $16, %ecx
  216.         repz cmpsl
  217.         jnz        1f
  218.         popal
  219.         popal
  220.         popal
  221.         popal
  222.         movl        $1, %eax                /* success */
  223.         popl        %ebx
  224.         popl        %ebp
  225.         ret
  226. 1:
  227.         popal
  228.         popal
  229.         popal
  230.         popal
  231.         xorl        %eax, %eax                /* failure */
  232. 2:
  233.         popl        %ebx
  234.         popl        %ebp
  235.         ret
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2024-9-22 07:07

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表