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.

501 lines
13 KiB

  1. title "Trap Processing"
  2. ;++
  3. ;
  4. ; Copyright (c) 1996 Microsoft Corporation
  5. ;
  6. ; Module Name:
  7. ;
  8. ; trap.asm
  9. ;
  10. ; Abstract:
  11. ;
  12. ; This module implements the code necessary to field and process i386
  13. ; trap conditions.
  14. ;
  15. ; Author:
  16. ;
  17. ; David N. Cutler (davec) 1-Dec-96
  18. ;
  19. ; Environment:
  20. ;
  21. ; Kernel mode only.
  22. ;
  23. ; Revision History:
  24. ;
  25. ;--
  26. .386p
  27. .xlist
  28. KERNELONLY equ 1
  29. include ks386.inc
  30. include callconv.inc
  31. include i386\kimacro.inc
  32. include mac386.inc
  33. .list
  34. extrn _BdDebugRoutine:DWORD
  35. page ,132
  36. subttl "Equated Values"
  37. ;
  38. ; Debug register 6 (dr6) BS (single step) bit mask
  39. ;
  40. DR6_BS_MASK EQU 4000H
  41. ;
  42. ; EFLAGS single step bit
  43. ;
  44. EFLAGS_TF_BIT EQU 100h
  45. EFLAGS_OF_BIT EQU 4000H
  46. _TEXT$00 SEGMENT PUBLIC 'CODE'
  47. ASSUME DS:NOTHING, ES:NOTHING, SS:FLAT, FS:NOTHING, GS:NOTHING
  48. page ,132
  49. subttl "Macros"
  50. ;++
  51. ;
  52. ; GENERATE_TRAP_FRAME
  53. ;
  54. ; Macro Dexcription:
  55. ;
  56. ; This macro generates a trap frame and saves the current register state.
  57. ;
  58. ; Arguments:
  59. ;
  60. ; None.
  61. ;
  62. ;--
  63. GENERATE_TRAP_FRAME macro
  64. ;
  65. ; Build trap frame minus the V86 and privilege level transition arguments.
  66. ;
  67. ; N.B. It is assumed that the error code has already been pushed on the stack.
  68. ;
  69. push ebp ; save nonvolatile registers
  70. push ebx ;
  71. push esi ;
  72. push edi ;
  73. push fs ; save FS segment register
  74. push -1 ; push dummy exception list
  75. push -1 ; dummy previous mode
  76. push eax ; save the volatile registers
  77. push ecx ;
  78. push edx ;
  79. push ds ; save segment registers
  80. push es ;
  81. push gs ;
  82. sub esp, TsSegGs ; allocate remainder of trap frame
  83. mov ebp, esp ; set ebp to base of trap frame
  84. cld ; clear direction bit
  85. endm
  86. page ,132
  87. subttl "Debug Exception"
  88. ;++
  89. ;
  90. ; Routine Description:
  91. ;
  92. ; Handle debug exceptions.
  93. ;
  94. ; This exception is generated for the following reasons:
  95. ;
  96. ; Instruction breakpoint fault.
  97. ; Data address breakpoint trap.
  98. ; General detect fault.
  99. ; Single-step trap.
  100. ; Task-switch breadkpoint trap.
  101. ;
  102. ; Arguments:
  103. ;
  104. ; On entry the stack contains:
  105. ;
  106. ; eflags
  107. ; cs
  108. ; eip
  109. ;
  110. ; N.B. There are no privilege transitions in the boot debugger. Therefore,
  111. ; the none of the previous ss, esp, or V86 registers are saved.
  112. ;
  113. ; Return value:
  114. ;
  115. ; None
  116. ;--
  117. ASSUME DS:NOTHING, SS:NOTHING, ES:NOTHING
  118. align 16
  119. public _BdTrap01@0
  120. _BdTrap01@0 proc
  121. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  122. push 0 ; push dummy error code
  123. GENERATE_TRAP_FRAME ; generate trap frame
  124. ;
  125. ; Set exception parameters.
  126. ;
  127. and dword ptr [ebp] + TsEflags, not EFLAGS_TF_BIT ; clear TF flag
  128. mov eax, STATUS_SINGLE_STEP ; set exception code
  129. mov ebx, [ebp] + TsEip ; set address of faulting instruction
  130. xor ecx, ecx ; set number of parameters
  131. call _BdDispatch ; dispatch exception
  132. jmp _BdExit ; dummy
  133. _BdTrap01@0 endp
  134. page ,132
  135. subttl "Int 3 Breakpoint"
  136. ;++
  137. ;
  138. ; Routine Description:
  139. ;
  140. ; Handle int 3 (breakpoint).
  141. ;
  142. ; This trap is caused by the int 3 instruction.
  143. ;
  144. ; Arguments:
  145. ;
  146. ; On entry the stack contains:
  147. ;
  148. ; eflags
  149. ; cs
  150. ; eip
  151. ;
  152. ; N.B. There are no privilege transitions in the boot debugger. Therefore,
  153. ; the none of the previous ss, esp, or V86 registers are saved.
  154. ;
  155. ; Return value:
  156. ;
  157. ; None
  158. ;
  159. ;--
  160. ASSUME DS:NOTHING, SS:NOTHING, ES:NOTHING
  161. align 16
  162. public _BdTrap03@0
  163. _BdTrap03@0 proc
  164. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  165. push 0 ; push dummy error code
  166. GENERATE_TRAP_FRAME ; generate trap frame
  167. ;
  168. ; Set exception parameters.
  169. ;
  170. dec dword ptr [ebp] + TsEip ; back up to int 3 instruction
  171. mov eax, STATUS_BREAKPOINT ; set exception code
  172. mov ebx, [ebp] + TsEip ; set address of faulting instruction
  173. mov ecx, 1 ; set number of parameters
  174. mov edx, BREAKPOINT_BREAK ; set service name
  175. call _BdDispatch ; dispatch exception
  176. jmp _BdExit ; dummy
  177. _BdTrap03@0 endp
  178. page ,132
  179. subttl "General Protect"
  180. ;++
  181. ;
  182. ; Routine Description:
  183. ;
  184. ; General protect violation.
  185. ;
  186. ; Arguments:
  187. ;
  188. ; On entry the stack contains:
  189. ;
  190. ; eflags
  191. ; cs
  192. ; eip
  193. ; error code
  194. ;
  195. ; N.B. There are no privilege transitions in the boot debugger. Therefore,
  196. ; the none of the previous ss, esp, or V86 registers are saved.
  197. ;
  198. ; Return value:
  199. ;
  200. ; N.B. There is no return from this fault.
  201. ;
  202. ;--
  203. ASSUME DS:NOTHING, SS:NOTHING, ES:NOTHING
  204. align 16
  205. public _BdTrap0d@0
  206. _BdTrap0d@0 proc
  207. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  208. GENERATE_TRAP_FRAME ; generate trap frame
  209. ;
  210. ; Set exception parameters.
  211. ;
  212. _BdTrap0d10: ;
  213. mov eax, STATUS_ACCESS_VIOLATION ; set exception code
  214. mov ebx, [ebp] + TsEip ; set address of faulting instruction
  215. mov ecx, 1 ; set number of parameters
  216. mov edx, [ebp] + TsErrCode ; set error code
  217. and edx, 0FFFFH ;
  218. call _BdDispatch ; dispatch exception
  219. jmp _BdTrap0d10 ; repeat
  220. _BdTrap0d@0 endp
  221. page ,132
  222. subttl "Page Fault"
  223. ;++
  224. ;
  225. ; Routine Description:
  226. ;
  227. ; Page fault.
  228. ;
  229. ; Arguments:
  230. ;
  231. ; On entry the stack contains:
  232. ;
  233. ; eflags
  234. ; cs
  235. ; eip
  236. ; error code
  237. ;
  238. ; N.B. There are no privilege transitions in the boot debugger. Therefore,
  239. ; the none of the previous ss, esp, or V86 registers are saved.
  240. ;
  241. ; Return value:
  242. ;
  243. ; N.B. There is no return from this fault.
  244. ;
  245. ;--
  246. ASSUME DS:NOTHING, SS:NOTHING, ES:NOTHING
  247. align 16
  248. public _BdTrap0e@0
  249. _BdTrap0e@0 proc
  250. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  251. GENERATE_TRAP_FRAME ; generate trap frame
  252. ;
  253. ; Set exception parameters.
  254. ;
  255. _BdTrap0e10: ;
  256. mov eax, STATUS_ACCESS_VIOLATION ; set exception code
  257. mov ebx, [ebp] + TsEip ; set address of faulting instruction
  258. mov ecx, 3 ; set number of parameters
  259. mov edx, [ebp] + TsErrCode ; set read/write code
  260. and edx, 2 ;
  261. mov edi, cr2 ; set fault address
  262. xor esi, esi ; set previous mode
  263. call _BdDispatch ; dispatch exception
  264. jmp _BdTrap0e10 ; repeat
  265. _BdTrap0e@0 endp
  266. page ,132
  267. subttl "Debug Service"
  268. ;++
  269. ;
  270. ; Routine Description:
  271. ;
  272. ; Handle int 2d (debug service).
  273. ;
  274. ; The trap is caused by an int 2d instruction. This instruction is used
  275. ; instead of an int 3 instruction so parameters can be passed to the
  276. ; requested debug service.
  277. ;
  278. ; N.B. An int 3 instruction must immediately follow the int 2d instruction.
  279. ;
  280. ; Arguments:
  281. ;
  282. ; On entry the stack contains:
  283. ;
  284. ; eflags
  285. ; cs
  286. ; eip
  287. ;
  288. ; N.B. There are no privilege transitions in the boot debugger. Therefore,
  289. ; the none of the previous ss, esp, or V86 registers are saved.
  290. ;
  291. ; Service (eax) - Supplies the service to perform.
  292. ; Argument1 (ecx) - Supplies the first argument.
  293. ; Argument2 (edx) - Supplies the second argument.
  294. ;
  295. ;--
  296. ASSUME DS:NOTHING, SS:NOTHING, ES:NOTHING
  297. align 16
  298. public _BdTrap2d@0
  299. _BdTrap2d@0 proc
  300. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  301. ;
  302. ; Build trap frame minus the V86 and privilege level transition arguments.
  303. ;
  304. push 0 ; push dummy error code
  305. GENERATE_TRAP_FRAME ; generate trap frame
  306. ;
  307. ; Set exception parameters.
  308. ;
  309. mov eax, STATUS_BREAKPOINT ; set exception code
  310. mov ebx, [ebp] + TsEip ; set address of faulting instruction
  311. mov ecx, 3 ; set number of parameters
  312. mov edx, [ebp] + TsEax ; set service name
  313. mov edi, [ebp] + TsEcx ; set first argument value
  314. mov esi, [ebp] + TsEdx ; set second argument value
  315. call _BdDispatch ; dispatch exception
  316. jmp _BdExit ; dummy
  317. _BdTrap2d@0 endp
  318. page , 132
  319. subttl "Exception Dispatch"
  320. ;++
  321. ;
  322. ; Dispatch
  323. ;
  324. ; Routine Description:
  325. ;
  326. ; This functions allocates an exception record, initializes the exception
  327. ; record, and calls the general exception dispatch routine.
  328. ;
  329. ; Arguments:
  330. ;
  331. ; Code (eax) - Suppplies the exception code.
  332. ; Address (ebx) = Supplies the address of the exception.
  333. ; Number (ecx) = Supplies the number of parameters.
  334. ; Parameter1 (edx) - Supplies exception parameter 1;
  335. ; Parameter2 (edi) - Supplies exception parameter 2;
  336. ; Parameter3 (esi) - Supplies exception parameter 3.
  337. ;
  338. ; Return Value:
  339. ;
  340. ; None.
  341. ;
  342. ;--
  343. align 16
  344. public _BdDispatch
  345. _BdDispatch proc
  346. .FPO (ExceptionRecordLength / 4, 0, 0, 0, 0, FPO_TRAPFRAME)
  347. ;
  348. ; Allocate and initialize exception record.
  349. ;
  350. sub esp, ExceptionRecordLength ; allocate exception record
  351. mov [esp] + ErExceptionCode, eax ; set exception code
  352. xor eax, eax ; zero register
  353. mov [esp] + ErExceptionFlags, eax ; zero exception flags
  354. mov [esp] + ErExceptionRecord, eax ; zero associated exception record
  355. mov [esp] + ErExceptionAddress, ebx ; set exception address
  356. mov [esp] + ErNumberParameters, ecx ; set number of parameters
  357. mov [esp] + ErExceptionInformation + 0, edx ; set parameter 1
  358. mov [esp] + ErExceptionInformation + 4, edi ; set parameter 2
  359. mov [esp] + ErExceptionInformation + 8, esi ; set parameter 3
  360. ;
  361. ; Save debug registers in trap frame.
  362. ;
  363. mov eax, dr0 ; save dr0
  364. mov [ebp] + TsDr0, eax ;
  365. mov eax, dr1 ; save dr1
  366. mov [ebp] + TsDr1, eax ;
  367. mov eax, dr2 ; save dr2
  368. mov [ebp] + TsDr2, eax ;
  369. mov eax, dr3 ; save dr3
  370. mov [ebp] + TsDr3, eax ;
  371. mov eax, dr6 ; save dr6
  372. mov [ebp] + TsDr6, eax ;
  373. mov eax, dr7 ; save dr7
  374. mov [ebp] + TsDr7, eax ;
  375. ;
  376. ; Save previous stack address and segment selector.
  377. ;
  378. mov eax, ss ; save stack segment register
  379. mov [ebp] + TsTempSegCs, eax ;
  380. mov [ebp] + TsTempEsp, ebp ; compute previous stack address
  381. add [ebp] + TsTempEsp, TsEFlags + 4 ;
  382. ;
  383. ; Call the general exception dispatcher.
  384. ;
  385. mov ecx, esp ; set address of exception record
  386. push ebp ; push address of trap frame
  387. push 0 ; push address of exception frame
  388. push ecx ; push address of exception record
  389. call [_BdDebugRoutine] ; call dispatch routine
  390. add esp, ExceptionRecordLength ; deallocate exception record
  391. ret ;
  392. _BdDispatch endp
  393. page ,132
  394. subttl "Common Trap Exit"
  395. ;++
  396. ;
  397. ; Exit
  398. ;
  399. ; Routine Description:
  400. ;
  401. ; This code is transfered to at the end of the processing for an exception.
  402. ; Its function is to restore machine state and continue execution.
  403. ;
  404. ; Arguments:
  405. ;
  406. ; ebp - Supplies the address of the trap frame.
  407. ;
  408. ; Return Value:
  409. ;
  410. ; None.
  411. ;
  412. ;--
  413. align 16
  414. public _BdExit
  415. _BdExit proc
  416. .FPO (0, 0, 0, 0, 0, FPO_TRAPFRAME)
  417. lea esp, [ebp] + TsSegGs ; get address of save area
  418. pop gs ; restore segment registers
  419. pop es ;
  420. pop ds ;
  421. pop edx ; restore volatile registers
  422. pop ecx ;
  423. pop eax ;
  424. add esp, 8 ; remove mode and exception list
  425. pop fs ; restore FS segment register
  426. pop edi ; restore nonvolatile registers
  427. pop esi ;
  428. pop ebx ;
  429. pop ebp ;
  430. add esp, 4 ; remove error code
  431. iretd ; return
  432. _BdExit endp
  433. _TEXT$00 ends
  434. end