Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

471 lines
12 KiB

  1. page ,132
  2. subttl emerror.asm - Emulator error handler
  3. ;***
  4. ;emerror.asm - Emulator error handler
  5. ;
  6. ; Microsoft Confidential
  7. ;
  8. ; Copyright (c) Microsoft Corporation 1987, 1991
  9. ;
  10. ; All Rights Reserved
  11. ;
  12. ;Purpose:
  13. ; Emulator error handler
  14. ;
  15. ;Revision History: (also see emulator.hst)
  16. ;
  17. ; 10/30/89 WAJ Added this header.
  18. ; 11/15/89 WAJ Major changes for Dos32RaiseExcpetion().
  19. ; 12/01/89 WAJ Now set cbExceptionInfo correctly.
  20. ; 02/08/90 WAJ Fixed GP fault in 32 bit exception handler.
  21. ; 09/03/91 JWM Modified entry/exit sequence for DOSX32.
  22. ; 02/15/92 JWM Adapted for NT.
  23. ;
  24. ;*******************************************************************************
  25. ifdef _DOS32EXT
  26. include except32.inc
  27. endif
  28. ;*** error_return - return to user code (regardless of error)
  29. ;
  30. ; This macro returns to user code. It goes to some lengths
  31. ; to restore the flags on the instruction immediately before
  32. ; the return so that any pending trace trap will be
  33. ; acknowledged immediately after the retfd (and before the
  34. ; next user instruction) instead of after the instruction
  35. ; following the return as would be the case if we returned
  36. ; using iretd.
  37. ;
  38. ; ENTRY ((SS:ESP)) = user's EAX
  39. ; ((SS:ESP)+4) = return EIP
  40. ; ((SS:ESP)+8) = return CS
  41. ; ((SS:ESP)+12) = user's EFLAGS
  42. ; EXIT return to user program, above arguments
  43. ; popped off stack, user's EAX and EFLAGS
  44. ; restored.
  45. error_return macro noerror
  46. ifdef _DOS32EXT
  47. sti ; JWM, 9/3/91
  48. push dword ptr [esp+8] ; JWM, 9/6/91
  49. popfd ; JWM, 9/6/91
  50. endif ; DOS32EXT
  51. ifdef NT386
  52. if DBG
  53. push dword ptr [esp+8] ; On checked build, allow
  54. popfd ; single step to work
  55. endif
  56. endif
  57. iretd
  58. endm
  59. TESTif macro nam
  60. mov bl,err&nam ; default error number
  61. if (nam ge 100h)
  62. test ah,nam/256
  63. else ;not (nam ge 100h)
  64. test al,nam
  65. endif ;(nam ge 100h)
  66. JSNZ signalerror
  67. endm
  68. EM_ENTRY eCommonExceptions
  69. CommonExceptions:
  70. mov ebx,[esp].[OldLongStatus]
  71. and ebx,LongSavedFlags ;preserve condition codes, error flags
  72. or EMSEG:[LongStatusWord],ebx ;merge saved status word, condition codes
  73. pop eax
  74. pop ecx
  75. pop edx
  76. pop ebx
  77. add esp,4 ; toss esp value
  78. pop ebp
  79. pop esi
  80. pop edi
  81. add esp,8 ;toss old PrevCodeOff and StatusWord
  82. pop ds
  83. call Emexcept
  84. error_return noerror
  85. ifdef _DOS32EXT
  86. EmExcept PROC C, OldEIP:DWORD, OldCS:DWORD, OldFlags:DWORD
  87. LOCAL SSAR:DWORD
  88. LOCAL ec:_DX32_CONTEXT
  89. ;*
  90. ;* Set up SS access rights.
  91. ;*
  92. push ds
  93. mov [ec.R_Eax], eax
  94. GetEmData ds,ax
  95. mov eax, ss
  96. lar eax, eax
  97. mov [SSAR], eax
  98. ;*
  99. ;* Fill in ExceptionContext structure.
  100. ;*
  101. mov [ec.NPXContextFlags], NPX_CONTEXT_FULL
  102. mov [ec.R_Edi], edi
  103. mov [ec.R_Esi], esi
  104. mov eax, [ebp]
  105. mov [ec.R_Ebp], eax
  106. lea eax, [OldFlags+4]
  107. mov [ec.R_Esp], eax
  108. mov [ec.R_Ebx], ebx
  109. mov [ec.R_Edx], edx
  110. mov [ec.R_Ecx], ecx
  111. mov eax, EMSEG:[PrevCodeOff]
  112. mov [ec.R_Eip], eax
  113. mov eax, [OldFlags]
  114. mov [ec.EFlags], eax
  115. mov eax, [OldCS]
  116. movzx eax,ax
  117. mov [ec.SegCs], eax
  118. mov ax,ss
  119. movzx eax,ax
  120. mov [ec.SegSs], eax
  121. pop eax
  122. movzx eax,ax
  123. mov [ec.SegDs], eax ; ds was pushed on entry.
  124. mov ax,es
  125. movzx eax,ax
  126. mov [ec.SegEs], eax
  127. mov ax,fs
  128. movzx eax,ax
  129. mov [ec.SegFs], eax
  130. mov ax,gs
  131. movzx eax,ax
  132. mov [ec.SegGs], eax
  133. lea esi, [ec]
  134. add esi, 4
  135. push ebp
  136. call SaveState
  137. pop ebp
  138. lea eax, [ec]
  139. push ds
  140. push es
  141. mov bx, seg FLAT:CURstk
  142. mov ds, ebx
  143. mov es, ebx
  144. push eax
  145. call DOS32RAISEEXCEPTION
  146. add esp, 4
  147. pop es
  148. pop ds
  149. RaiseExceptRet:
  150. or eax, eax
  151. JZ ExceptNotHandled
  152. ;*
  153. ;* Copy new flags, cs, eip to new stack.
  154. ;*
  155. mov ds, [ec.SegSs]
  156. mov esi, [ec.R_Esp] ; ds:esi == new ss:esp
  157. mov eax, [ec.Eflags] ; set up iretd frame
  158. mov [esi-4], eax
  159. mov eax, [ec.SegCs]
  160. mov [esi-8], eax
  161. mov eax, [ec.R_Eip]
  162. mov [esi-12], eax
  163. ;*
  164. ;* Put new stack pointer on stack.
  165. ;*
  166. push ds
  167. sub esi, 12
  168. push esi
  169. ;*
  170. ;* Reset other registers.
  171. ;*
  172. mov edi, [ec.R_Edi]
  173. mov esi, [ec.R_Esi]
  174. mov ebx, [ec.R_Ebx]
  175. mov edx, [ec.R_Edx]
  176. mov ecx, [ec.R_Ecx]
  177. mov eax, [ec.R_Eax]
  178. mov ds, [ec.SegDs]
  179. mov es, [ec.SegEs]
  180. mov fs, [ec.SegFs]
  181. mov gs, [ec.SegGs]
  182. mov ebp, [ec.R_Ebp] ; must do this last.
  183. lss esp, fword ptr [esp] ; reset ss:esp
  184. sti ; JWM, 9/3/91
  185. push [esp+8] ; JWM, 9/6/91
  186. popfd ; JWM, 9/6/91
  187. iretd ; reset flags, cs, eip
  188. ExceptNotHandled:
  189. EmExcept ENDP
  190. endif ; ifdef _DOS32EXT
  191. ifdef NT386
  192. ISIZE equ 4
  193. ISizeEC equ (ContextFrameLength + ISIZE - 1) and (not (ISIZE - 1))
  194. ISizeExceptStruct equ (ExceptionRecordLength + ISIZE - 1) and (not (ISIZE - 1))
  195. ec_off EQU 4+ISizeEc
  196. estruct_off EQU ec_off+ISizeExceptStruct
  197. SSAR EQU <[ebp][-4]>
  198. ec EQU <[ebp][-ec_off]>
  199. eStruct EQU <[ebp][-estruct_off]>
  200. OldEIP EQU <ebp+8>
  201. OldCS EQU <ebp+12>
  202. OldFlags EQU <ebp+16>
  203. EmExcept PROC NEAR
  204. push ebp
  205. mov ebp,esp
  206. sub esp,estruct_off
  207. ;*
  208. ;* Set up SS access rights.
  209. ;*
  210. push ds
  211. mov [ec.ctx_RegEax], eax
  212. GetEmData ds,ax
  213. mov eax, ss
  214. lar eax, eax
  215. mov [SSAR], eax
  216. ;*
  217. ;* Fill in ExceptionContext structure.
  218. ;*
  219. mov dword ptr [ec.ContextFlags], NPX_CONTEXT_FULL
  220. mov dword ptr [ec.ctx_Cr0NpxState], CR0_EM
  221. mov [ec.ctx_RegEdi], edi
  222. mov [ec.ctx_RegEsi], esi
  223. mov eax, [ebp]
  224. mov [ec.ctx_RegEbp], eax
  225. lea eax, [OldFlags+4]
  226. mov [ec.ctx_RegEsp], eax
  227. mov [ec.ctx_RegEbx], ebx
  228. mov [ec.ctx_RegEdx], edx
  229. mov [ec.ctx_RegEcx], ecx
  230. mov eax, [OldEIP]
  231. mov [ec.ctx_RegEip], eax
  232. mov eax, [OldFlags]
  233. mov [ec.ctx_EFlags], eax
  234. mov eax, [OldCS]
  235. movzx eax,ax
  236. mov [ec.ctx_SegCs], eax
  237. mov ax,ss
  238. movzx eax,ax
  239. mov [ec.ctx_SegSs], eax
  240. pop eax
  241. movzx eax,ax
  242. mov [ec.ctx_SegDs], eax ; ds was pushed on entry.
  243. mov ax,es
  244. movzx eax,ax
  245. mov [ec.ctx_SegEs], eax
  246. mov ax,fs
  247. movzx eax,ax
  248. mov [ec.ctx_SegFs], eax
  249. mov ax,gs
  250. movzx eax,ax
  251. mov [ec.ctx_SegGs], eax
  252. lea esi, [ec]
  253. add esi, ctx_env
  254. or EMSEG:[StatusWord], 8000H ; set 'busy' bit
  255. or EMSEG:[SWerr], Summary ; set Summary bit
  256. or EMSEG:[CURerr], Summary
  257. mov cl, EMSEG:[ErrMask]
  258. push ecx
  259. push ebp
  260. call SaveState
  261. pop ebp
  262. pop ecx
  263. call GetEMSEGStatusWord ; EAX = status word
  264. test al, cl ; test status word against mask
  265. jne short Err00
  266. ifdef TRACENPX
  267. mov edx, 0C1020304h ; Raise bogus exception code, to trace with
  268. jmp short Err50
  269. endif
  270. mov al, Invalid
  271. ;
  272. ; According to the floating error priority, we test what is the cause of
  273. ; the NPX error and raise an appropriate exception.
  274. ;
  275. Err00:
  276. test al, Invalid ; Invalid Op?
  277. jz short Err10 ; No, go check next
  278. mov edx, XCPT_FLOAT_INVALID_OPERATION
  279. test al, StackFlag ; Stack fault?
  280. jz short Err50 ; No, go raise invalid op
  281. mov edx, XCPT_FLOAT_STACK_CHECK
  282. jmp short Err50 ; Go raise stack fault
  283. Err10: mov edx, XCPT_FLOAT_DIVIDE_BY_ZERO
  284. test al, ZeroDivide
  285. jnz short Err50
  286. mov edx, XCPT_FLOAT_DENORMAL_OPERAND
  287. test al, Denormal
  288. jnz short Err50
  289. mov edx, XCPT_FLOAT_OVERFLOW
  290. test al, Overflow
  291. jnz short Err50
  292. mov edx, XCPT_FLOAT_UNDERFLOW
  293. test al, Underflow
  294. jnz short Err50
  295. mov edx, XCPT_FLOAT_INEXACT_RESULT
  296. Err50: mov [eStruct.ExceptionNum], edx
  297. xor eax,eax
  298. mov [eStruct.fHandlerFlags], eax
  299. mov [eStruct.NestedExceptionReportRecord], eax
  300. mov dword ptr [eStruct.CParameters], 1 ; GeorgioP convention
  301. mov [eStruct.ErExceptionInformation], eax ; GeorgioP convention
  302. mov eax, EMSEG:[PrevCodeOff]
  303. mov [eStruct.ExceptionAddress], eax
  304. lea edx, [eStruct]
  305. lea eax, [ec]
  306. push ds
  307. push es
  308. ;TRUE, this is a first-chance exception
  309. stdCall _NtRaiseException,<edx, eax, 1>
  310. stdCall _RtlRaiseStatus, <eax>
  311. pop es
  312. pop ds
  313. RaiseExceptRet:
  314. or eax, eax
  315. JZ ExceptNotHandled
  316. ;*
  317. ;* Copy new flags, cs, eip to new stack.
  318. ;*
  319. mov ds, [ec.ctx_SegSs]
  320. mov esi, [ec.ctx_RegEsp] ; ds:esi == new ss:esp
  321. mov eax, [ec.ctx_Eflags] ; set up iretd frame
  322. mov [esi-4], eax
  323. mov eax, [ec.ctx_SegCs]
  324. mov [esi-8], eax
  325. mov eax, [ec.ctx_RegEip]
  326. mov [esi-12], eax
  327. ;*
  328. ;* Put new stack pointer on stack.
  329. ;*
  330. push ds
  331. sub esi, 12
  332. push esi
  333. ;*
  334. ;* Reset other registers.
  335. ;*
  336. mov edi, [ec.ctx_RegEdi]
  337. mov esi, [ec.ctx_RegEsi]
  338. mov ebx, [ec.ctx_RegEbx]
  339. mov edx, [ec.ctx_RegEdx]
  340. mov ecx, [ec.ctx_RegEcx]
  341. mov eax, [ec.ctx_RegEax]
  342. mov ds, [ec.ctx_SegDs]
  343. mov es, [ec.ctx_SegEs]
  344. mov fs, [ec.ctx_SegFs]
  345. mov gs, [ec.ctx_SegGs]
  346. mov ebp, [ec.ctx_RegEbp] ; must do this last.
  347. lss esp, fword ptr [esp] ; reset ss:esp
  348. sti ; JWM, 9/3/91
  349. push [esp+8] ; JWM, 9/6/91
  350. popfd ; JWM, 9/6/91
  351. iretd ; reset flags, cs, eip
  352. ExceptNotHandled:
  353. EmExcept ENDP
  354. endif ; ifdef NT386
  355. int 3 ; Added For BBT, a return here is needed or BBT
  356. ret ; has a flow problem.
  357. ifdef DEBUG
  358. lab PageFault
  359. mov al, byte ptr cs:[iax]
  360. ret
  361. endif