Source code of Windows XP (NT5)
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.

387 lines
9.1 KiB

  1. ;++
  2. ;
  3. ; Module Name:
  4. ;
  5. ; trap.asm
  6. ;
  7. ; Author:
  8. ;
  9. ; Thomas Parslow [tomp]
  10. ;
  11. ; Created:
  12. ;
  13. ; 15-Jan-91
  14. ;
  15. ;
  16. ; Description:
  17. ;
  18. ; x86 exception code
  19. ;
  20. ;
  21. include su.inc
  22. ;
  23. ; Exception Routing Table:
  24. ; ~~~~~~~~~~~~~~~~~~~~~~~~
  25. ; When an exception occurs in the SU module or before the OS loader
  26. ; is able to setup its own IDT, control is vectored to one of the
  27. ; Trap0 though TrapF labels. We push a number on the stack identifying
  28. ; the exception number and then jump to code that pushes the register set
  29. ; onto the stack. We then call a general purpose C routine that will dump
  30. ; the register contents, the trap number, and error code information
  31. ; onto the display.
  32. ;
  33. _TEXT segment para use16 public 'CODE'
  34. ASSUME CS: _TEXT, DS: DGROUP, SS: DGROUP
  35. .386p
  36. extrn _TrapHandler:near
  37. extrn _putx:near
  38. extrn _GDTregister:fword
  39. extrn _IDTregister:fword
  40. extrn _InDebugger:word
  41. extrn SaveSP:word
  42. public Trap0,Trap1,Trap2,Trap3,Trap4,Trap5,Trap6,Trap7
  43. public Trap8,Trap9,TrapA,TrapB,TrapC,TrapD,TrapE,TrapF
  44. Trap0: TRAP_NUMBER 0,MakeTrapFrame
  45. Trap1: TRAP_NUMBER 1,MakeTrapFrame
  46. Trap2: TRAP_NUMBER 2,MakeTrapFrame
  47. Trap3: TRAP_NUMBER 3,MakeTrapFrame
  48. Trap4: TRAP_NUMBER 4,MakeTrapFrame
  49. Trap5: TRAP_NUMBER 5,MakeTrapFrame
  50. Trap6: TRAP_NUMBER 6,MakeTrapFrame
  51. Trap7: TRAP_NUMBER 7,MakeTrapFrame
  52. Trap8: TRAP_NUMBER 8,MakeTrapFrame
  53. Trap9: TRAP_NUMBER 9,MakeTrapFrame
  54. TrapA: TRAP_NUMBER 0Ah,MakeTrapFrame
  55. TrapB: TRAP_NUMBER 0Bh,MakeTrapFrame
  56. TrapC: TRAP_NUMBER 0Ch,MakeTrapFrame
  57. TrapD: TRAP_NUMBER 0Dh,MakeTrapFrame
  58. TrapE: TRAP_NUMBER 0Eh,MakeTrapFrame
  59. TrapF: TRAP_NUMBER 0Fh,MakeTrapFrame
  60. ;
  61. ; We save the user's register contents here on the stack and call
  62. ; a C routine to display those contents.
  63. ; Uses 42 bytes of stack. (40 for frame)
  64. ;
  65. ; Note that we build a stack frame that's independent of the code and
  66. ; stack segments' "size" during execution. That way whether we enter
  67. ; with a 16bit or 32bit stack, the arguments frame is exacely the same.
  68. ;
  69. MakeTrapFrame:
  70. mov eax,esp
  71. push ecx
  72. push edx
  73. push ebx
  74. push eax ; (eax)=(esp)
  75. push ebp
  76. push esi
  77. push edi
  78. mov ax,ds
  79. push ax
  80. mov ax,es
  81. push ax
  82. mov ax,fs
  83. push ax
  84. mov ax,gs
  85. push ax
  86. mov ax,ss
  87. push ax
  88. mov eax,cr3
  89. push eax
  90. mov eax,cr2
  91. push eax
  92. mov eax,cr0
  93. push eax
  94. mov eax,dr6
  95. push eax
  96. str ax
  97. push ax
  98. ;
  99. ; Clear out debug register signals
  100. ;
  101. xor eax,eax
  102. mov dr6,eax
  103. ;
  104. ; Get a known good data segment
  105. ;
  106. mov ax,SuDataSelector
  107. mov ds,ax
  108. ;
  109. ; Save system registers
  110. ;
  111. mov bx,offset DGROUP:_GDTregister
  112. sgdt fword ptr [bx]
  113. mov bx,offset DGROUP:_IDTregister
  114. sidt fword ptr [bx]
  115. ;
  116. ; Is the exception frame on a 16bit or 32bit stack?
  117. ;
  118. mov ax,ss
  119. cmp ax,KeDataSelector
  120. je Trap32
  121. cmp ax,DbDataSelector
  122. jne mtf8
  123. ;
  124. ; Most likely we took a trap while initializing the 386 kernel debugger
  125. ; So we've got a 16bit stack that isn't ours. We need to move
  126. ; the stack frame onto the SU module's stack.
  127. jmp Trap16
  128. mtf8:
  129. ;
  130. ; Frame on a our 16bit stack so just call the trap dump routine
  131. ;
  132. mov bx,offset DGROUP:SaveSP
  133. mov [bx],sp
  134. call _TrapHandler
  135. ;
  136. ; Get rid of the junk we saved just to display
  137. ;
  138. pop eax ; get rid of ebp pushed for other returns
  139. add sp,ExceptionFrame.Fgs
  140. ;
  141. ; Reload the user's context and return to 16bit code
  142. ;
  143. pop gs
  144. pop fs
  145. pop es
  146. pop ds
  147. pop edi
  148. pop esi
  149. pop ebp
  150. pop eax
  151. pop ebx
  152. pop edx
  153. pop ecx
  154. pop eax ; get rid of trap #
  155. pop eax ; get original eax
  156. add esp,4 ; get rid of error code
  157. ;
  158. ; Pop IRET frame and return to where the trap occured
  159. ;
  160. OPSIZE
  161. iret
  162. Trap32:
  163. ;
  164. ; The exception frame is on a 32bit stack so we must setup a 16bit
  165. ; stack and then move the exception frame on to it before calling
  166. ; the trap dump routine.
  167. ;
  168. mov ebx,esp
  169. ;
  170. ; Setup a known good stack
  171. ;
  172. mov ax,SuDataSelector
  173. mov ss,ax
  174. mov sp,EXPORT_STACK
  175. ;
  176. ; Copy the exception frame to the new stack
  177. ;
  178. mov ecx, (size ExceptionFrame)/2 ; # of words in frame
  179. mov esi,ebx ; (esi) = offset of argument frame
  180. push KeDataSelector ; (ax) = Flat 32bit segment selector
  181. pop ds ; (ds:esi) points to argument frame
  182. push ss ;
  183. pop es ; (es) = 16bit stack selector
  184. sub sp, size ExceptionFrame ; make room for the arguments
  185. xor edi,edi ; clear out upper 16bits of edi
  186. mov di,sp ; (es:edi) points to top of stack
  187. ;
  188. ; Loop and copy a word at a time.
  189. ;
  190. msf1:
  191. mov ax,[esi]
  192. mov es:[edi],ax
  193. add esi,2
  194. add edi,2
  195. loop msf1
  196. push es ;
  197. pop ds ; put 16bit selector back into ds
  198. ;
  199. ; Now call the general purpose exception handler
  200. ;
  201. push ebx ; save esp for return
  202. ;
  203. ; Save SP in order to restore the stack in case we
  204. ; take a fault in the debugger
  205. ;
  206. mov bx,offset DGROUP:SaveSP
  207. mov [bx],sp
  208. call _TrapHandler
  209. IFDEF DEBUG0
  210. public DebugReturn
  211. DebugReturn:
  212. ENDIF ;DEBUG
  213. pop ebx
  214. ;
  215. ; We may have changed the flags while in the debugger. Copy the
  216. ; new eflag to the iret frame. After we restore the original stack
  217. ; pointers.
  218. ;
  219. mov bp,sp
  220. mov ecx,[bp].Feflags
  221. mov ax,KeDataSelector
  222. mov ss,ax
  223. mov esp,ebx
  224. mov [esp].Feflags,ecx
  225. ;
  226. ; Get rid of the junk we saved just to display
  227. ;
  228. add esp,ExceptionFrame.Fgs
  229. ;
  230. ; Reload the user's context
  231. ;
  232. pop gs
  233. pop fs
  234. pop es
  235. pop ds
  236. pop edi
  237. pop esi
  238. pop ebp
  239. pop eax
  240. pop ebx
  241. pop edx
  242. pop ecx
  243. pop eax ; get rid of trap #
  244. pop eax ; get original eax
  245. add esp,4 ; get rid of error code
  246. ;
  247. ; Pop IRET frame and return to where the trap occured
  248. ;
  249. OPSIZE
  250. iret
  251. Trap16:
  252. ; The exception frame is on a 16bit stack that isn't ours. So we must
  253. ; move the exception frame on to the SU module's stack before calling
  254. ; the trap dump routine.
  255. ;
  256. mov ebx,esp
  257. ;
  258. ; Setup a known good stack
  259. ;
  260. mov ax,SuDataSelector
  261. mov ss,ax
  262. mov sp,EXPORT_STACK
  263. ;
  264. ; Copy the exception frame to the new stack
  265. ;
  266. mov ecx, (size ExceptionFrame)/2
  267. mov si,bx ; (esi) = offset of argument frame
  268. push DbDataSelector ;
  269. pop ds ; (ds:esi) points to argument frame
  270. push ss ;
  271. pop es ; (es) = 16bit stack selector
  272. sub sp, size ExceptionFrame ; make room for the arguments
  273. mov di,sp ; (es:edi) points to top of stack
  274. ;
  275. ; Loop and copy a word at a time.
  276. ;
  277. Trap16_10:
  278. mov ax,[si]
  279. mov es:[di],ax
  280. add si,2
  281. add di,2
  282. loop Trap16_10
  283. push es ;
  284. pop ds ; put 16bit selector back into ds
  285. ;
  286. ; Now call the general purpose exception handler
  287. ;
  288. push ebx ; save (original esp) for return
  289. ;
  290. ; Save SP in order to restore the stack in case we
  291. ; take a fault in the debugger
  292. ;
  293. mov bx,offset DGROUP:SaveSP
  294. mov [bx],sp
  295. call _TrapHandler
  296. IFDEF DEBUG0
  297. public Debug16Return
  298. Debug16Return:
  299. ENDIF ;DEBUG
  300. pop ebx
  301. ;
  302. ; We may have changed the flags while in the debugger. Copy the
  303. ; new eflag to the iret frame. After we restore the original stack
  304. ; pointers.
  305. ;
  306. mov bp,sp
  307. mov ecx,dword ptr [bp].Feflags
  308. mov ax,DbDataSelector
  309. mov ss,ax
  310. mov esp,ebx
  311. mov dword ptr ss:[bx].Feflags,ecx
  312. ;
  313. ; Get rid of the junk we saved just to display
  314. ;
  315. add sp,ExceptionFrame.Fgs
  316. ;
  317. ; Reload the user's context
  318. ;
  319. pop gs
  320. pop fs
  321. pop es
  322. pop ds
  323. pop edi
  324. pop esi
  325. pop ebp
  326. pop eax
  327. pop ebx
  328. pop edx
  329. pop ecx
  330. pop eax ; get rid of trap #
  331. pop eax ; get original eax
  332. add esp,4 ; get rid of error code
  333. ;
  334. ; Pop IRET frame and return to where the trap occured
  335. ;
  336. OPSIZE
  337. iret
  338. _TEXT ends
  339. end
  340.