home *** CD-ROM | disk | FTP | other *** search
/ Internet MPEG Audio Archive / IMAA.mdf / UTIL / DOS / L3V100N / RSX / SOURCE / EXCEP32.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-01-20  |  7.8 KB  |  358 lines

  1. ;
  2. ; EXCEP32.ASM (c) Rainer Schnitker 92,93
  3. ;
  4.  
  5. ; contains:
  6. ;
  7. ; Exception handler entry
  8. ; Ints that came from real-mode
  9. ; Other utils
  10. ;
  11.  
  12.     INCLUDE REGS386.INC
  13.  
  14.         .386P
  15.  
  16.  
  17. DGROUP  group _DATA
  18.  
  19.     extrn _myexcep13:near
  20.     extrn _back_from_syscall:near
  21.  
  22. _DATA   segment word public 'DATA' use16
  23.  
  24.     extrn _code16sel:word        ;  16 bit selectors
  25.     extrn _data16sel:word        ;
  26.         extrn _stack16sel:word          ;
  27.     extrn _stackp16:dword        ;  16 bit stackpointer
  28.  
  29.     extrn _npz:PROCESS        ;  current process
  30.     extrn _regf:REG386        ;  regs for exceptions
  31.  
  32.     public _time_tic
  33.         public _cbrkcall
  34.     _time_tic        dd    0
  35.         _cbrkcall               dw      0
  36.  
  37.         dpmiss                  dw      ?
  38.         dpmiesp                 dd      ?
  39.  
  40. _DATA   ends
  41.  
  42. _TEXT segment byte public 'CODE' use16
  43.         assume  cs:_TEXT,ds:DGROUP
  44.  
  45.  
  46.         public _load_ds
  47. _load_ds proc near        ; load our extender ds
  48.         push    ax
  49.     mov    ax,0000h ; value will be written after entering prot mode
  50.     mov    ds, ax
  51.         pop     ax
  52.         ret
  53. _load_ds        endp
  54.  
  55. ;     public _jmp_to_user
  56. ;_jmp_to_user proc near      ; jmp back to user program (debugger)
  57. ;     mov     si, word ptr DGROUP:_npz     ; load process ptr
  58. ;     mov     eax, dword ptr [si].R_EAX
  59. ;     mov     ebx, dword ptr [si].R_EBX
  60. ;     mov     ecx, dword ptr [si].R_ECX
  61. ;     mov     edx, dword ptr [si].R_EDX
  62. ;     mov     edi, dword ptr [si].R_EDI
  63. ;     mov     ebp, dword ptr [si].R_EBP
  64. ;     mov     es, word ptr [si].R_ES
  65. ;     mov     fs, word ptr [si].R_FS
  66. ;     mov     gs, word ptr [si].R_GS
  67. ;     mov     ss, word ptr [si].R_SS
  68. ;     mov     esp, dword ptr [si].R_ESP
  69. ;
  70. ;     push     dword ptr [si].R_EFLAGS
  71. ;     push     dword ptr [si].R_CS
  72. ;     push     dword ptr [si].R_EIP
  73. ;
  74. ;     mov     esi, dword ptr [si].R_ESI
  75. ;     push     es
  76. ;     pop     ds
  77. ;
  78. ;     iretd
  79. ;_jmp_to_user endp
  80. ;
  81.  
  82.     public _clearregs
  83. _clearregs proc near                ; clear high bits
  84.     and    eax, dword ptr 0ffffH        ; for 16 bit code
  85.     and    ebx, dword ptr 0ffffH
  86.     and    ecx, dword ptr 0ffffH
  87.     and    edx, dword ptr 0ffffH
  88.     and    esi, dword ptr 0ffffH
  89.     and    edi, dword ptr 0ffffH
  90.     and    ebp, dword ptr 0ffffH
  91.         ret
  92. _clearregs endp
  93.  
  94. ; this proc is called after exception handler return from DPMI-server
  95. ; cs:eip and ss:esp set
  96. exception_after_return proc near    ; called after FAR-RET from exception
  97.     call    _load_ds        ; get extender ds
  98.     push    ds
  99.     pop    es            ; make ds=es
  100.  
  101.     call    _clearregs
  102.     call    _myexcep13
  103.  
  104.     jmp    _back_from_syscall
  105. exception_after_return endp
  106.  
  107. ; EXCEPTIONS
  108. ;
  109. ;  DPMI-rules:
  110. ; - return with far return , org SS:ESP,CS:EIP,EFLAGS(i-flag!) will restored
  111. ;   (note: iret doesn't restore i-flag, because IOPL < DPL )
  112. ; - all fault have error code (only valid for 08,0A-0E)
  113. ; - handler must preserve and restore all registers
  114. ; - handler will be called on a locked stack with interrupts disabled
  115. ; - handler must return or jump to the next handler
  116. ; - handler can modify exception stack, but not return cs,eip
  117. ;   it must return to the orginal handler
  118. ; - called only for protected mode exceptions
  119. ;
  120. ;        exception STACK
  121. ;          0 : ebp  (our,not default)
  122. ;          4 : return eip (to host)
  123. ;          8 : return cs  (to host)
  124. ;         12 : error
  125. ;         16 : EIP (orginal)
  126. ;         20 : CS (orginal)
  127. ;         24 : Eflags
  128. ;         28 : ESP
  129. ;         32 : SS
  130.  
  131.  
  132.         public _excep13_386
  133. _excep13_386 proc far
  134.     push    word ptr 13
  135.     jmp    short exceptionhandler
  136.  
  137. public _excep14_386
  138. _excep14_386:
  139.     push    word ptr 14
  140.     jmp    short exceptionhandler
  141.  
  142. public _excep0_386
  143. _excep0_386:
  144.     push    word ptr 0
  145.     jmp    short exceptionhandler
  146.  
  147. public _excep1_386
  148. _excep1_386:
  149.     push    word ptr 1
  150.     jmp    short exceptionhandler
  151.  
  152. public _excep2_386
  153. _excep2_386:
  154.     push    word ptr 2
  155.     jmp    short exceptionhandler
  156.  
  157. public _excep3_386
  158. _excep3_386:
  159.     push    word ptr 3
  160.     jmp    short exceptionhandler
  161.  
  162. public _excep4_386
  163. _excep4_386:
  164.     push    word ptr 4
  165.     jmp    short exceptionhandler
  166.  
  167. public _excep5_386
  168. _excep5_386:
  169.     push    word ptr 5
  170.     jmp    short exceptionhandler
  171.  
  172. public _excep6_386
  173. _excep6_386:
  174.     push    word ptr 6
  175.     jmp    short exceptionhandler
  176.  
  177. public _excep7_386
  178. _excep7_386:
  179.     push    word ptr 7
  180.     jmp    short exceptionhandler
  181.  
  182. public _excep8_386
  183. _excep8_386:
  184.     push    word ptr 8
  185.     jmp    short exceptionhandler
  186.  
  187. public _excep9_386
  188. _excep9_386:
  189.     push    word ptr 9
  190.     jmp    short exceptionhandler
  191.  
  192. public _excep10_386
  193. _excep10_386:
  194.     push    word ptr 10
  195.     jmp    short exceptionhandler
  196.  
  197. public _excep11_386
  198. _excep11_386:
  199.     push    word ptr 11
  200.     jmp    short exceptionhandler
  201.  
  202. public _excep12_386
  203. _excep12_386:
  204.     push    word ptr 12
  205.     jmp    short exceptionhandler
  206.  
  207. public _excep15_386
  208. _excep15_386:
  209.     push    word ptr 15
  210.     jmp    short exceptionhandler
  211.  
  212. public _excep16_386
  213. _excep16_386:
  214.     push    word ptr 16
  215.     jmp    short exceptionhandler
  216.  
  217. public _excep17_386
  218. _excep17_386:
  219.     push    word ptr 17
  220.     jmp    short exceptionhandler
  221.  
  222. exceptionhandler:
  223.     push    ds                ; save user ds
  224.     call    _load_ds            ; we need our extender ds
  225.  
  226.     mov    dword ptr DGROUP:_regf.REG_EAX, eax
  227.     mov    dword ptr DGROUP:_regf.REG_EBX, ebx
  228.     mov    dword ptr DGROUP:_regf.REG_ECX, ecx
  229.     mov    dword ptr DGROUP:_regf.REG_EDX, edx
  230.     mov    dword ptr DGROUP:_regf.REG_EBP, ebp
  231.     mov    dword ptr DGROUP:_regf.REG_EDI, edi
  232.     mov    dword ptr DGROUP:_regf.REG_ESI, esi
  233.     mov    word ptr DGROUP:_regf.REG_ES , es
  234.     mov    word ptr DGROUP:_regf.REG_FS , fs
  235.     mov    word ptr DGROUP:_regf.REG_GS , gs
  236.  
  237.     pop    ax
  238.     mov    word ptr DGROUP:_regf.REG_DS, ax
  239.     pop    ax
  240.     mov    word ptr DGROUP:_regf.REG_FAULTNO, ax
  241.  
  242.     ; the rest regs are on stack
  243.     push    ebp
  244.     mov    ebp, esp
  245.  
  246.         ; get error code from stack
  247.     mov    eax,dword ptr [ebp+12]
  248.     mov    dword ptr DGROUP:_regf.REG_ERR, eax
  249.  
  250.         ; get eip from stack
  251.     mov    eax,dword ptr [ebp+16]
  252.     mov    dword ptr DGROUP:_regf.REG_EIP, eax
  253.  
  254.     ; get cs from stack
  255.     mov    eax,dword ptr [ebp+20]
  256.     mov    dword ptr DGROUP:_regf.REG_CS, eax
  257.  
  258.     ; get eflags from stack
  259.     mov    eax, dword ptr [ebp+24]
  260.     and    ax,0FEFFh                ; clear trace flag
  261.         or      ax, 0200h                               ; set iret flag
  262.     mov    dword ptr DGROUP:_regf.REG_EFLAGS, eax    ; else trap after fret
  263.     mov    dword ptr [ebp+24],eax            ; back on dpmi-stack
  264.  
  265.         ; get esp from stack
  266.     mov    eax, dword ptr [ebp+28]
  267.     mov    dword ptr DGROUP:_regf.REG_ESP, eax
  268.     mov    dword ptr DGROUP:_regf.REG_ESPORG, eax
  269.  
  270.         ; get ss from stack
  271.     mov    ax,word ptr [ebp+32]
  272.     mov    word ptr DGROUP:_regf.REG_SS, ax
  273.  
  274.  
  275.     ; set new return address cs:eip
  276.     ; to exception_after_return
  277.  
  278.     xor    eax,eax
  279.     mov    ax,offset exception_after_return
  280.     mov    dword ptr [ebp+16], eax         ; set new eip
  281.  
  282.     mov    ax,cs                    ; bzw _code16sel
  283.     mov    word ptr [ebp+20],ax            ; set cs
  284.  
  285.  
  286.     ; set new ss:esp
  287.     ; to our C gp_fault handler
  288.  
  289.     mov    eax,dword ptr DGROUP:_stackp16
  290.     mov    dword ptr [ebp+28],eax        ; set esp
  291.  
  292.     mov    ax,ds                ; bzw _stack16sel
  293.     mov    word ptr [ebp+32],ax        ; set ss
  294.  
  295.     pop ebp
  296.  
  297.         ; restore changed regs
  298.     mov    eax,dword ptr DGROUP:_regf.REG_EAX
  299.     push    word ptr _regf.REG_DS
  300.     pop    ds
  301.  
  302.     db 066H                 ; we need 16:32 bit return
  303.     retf
  304. _excep13_386 endp
  305.  
  306.  
  307. ;
  308. ; INTs that came form real mode (timer 0x1C,^C 0x23,crit error 0x24)
  309. ;
  310. ;  DPMI-rules:
  311. ; - handler must return ( don't terminate )
  312. ; - interrupts are disabled (?)
  313. ; - stack: locked protected mode stack from host (4 KB)
  314. ;
  315.  
  316. ; Control-C handler
  317.         public _prot_cbrk
  318. _prot_cbrk   proc    far
  319.         push    ds
  320.     push    si
  321.         call    _load_ds
  322.         mov     word ptr DGROUP:_cbrkcall,1
  323.     mov    si,word ptr DGROUP:_npz     ; load process ptr
  324.     bts    [si].sig_raised,2
  325.     pop    si
  326.         pop     ds
  327.  
  328.     ; iret doesn't restore I-flag
  329.     push    ax
  330.     mov    ax,0901h
  331.     int    031h
  332.     pop    ax
  333.  
  334.         iretd
  335.     ; retfar caused error by qemm/qdpmi
  336. _prot_cbrk   endp
  337.  
  338.  
  339. ; Timer handler
  340.         public _timer_handler
  341. _timer_handler proc far
  342.         push    ds
  343.         call    _load_ds
  344.     add    dword ptr _time_tic ,1
  345.         pop     ds
  346.  
  347.     ; iret doesn't restore I-flag
  348.     push    ax
  349.     mov    ax,0901h
  350.     int    031h
  351.     pop    ax
  352.  
  353.     iretd
  354. _timer_handler endp
  355.  
  356. _TEXT    ends
  357.         end
  358.