AsmFuncs.nasm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. ;------------------------------------------------------------------------------
  2. ;
  3. ; Copyright (c) 2010 - 2022, Intel Corporation. All rights reserved.<BR>
  4. ; SPDX-License-Identifier: BSD-2-Clause-Patent
  5. ;
  6. ; Module Name:
  7. ;
  8. ; AsmFuncs.nasm
  9. ;
  10. ; Abstract:
  11. ;
  12. ; Debug interrupt handle functions.
  13. ;
  14. ;------------------------------------------------------------------------------
  15. #include "DebugException.h"
  16. ;
  17. ; InterruptProcess()
  18. ;
  19. extern ASM_PFX(InterruptProcess)
  20. global ASM_PFX(Exception0Handle)
  21. global ASM_PFX(TimerInterruptHandle)
  22. global ASM_PFX(ExceptionStubHeaderSize)
  23. %macro AGENT_HANDLER_SIGNATURE 0
  24. db 0x41, 0x47, 0x54, 0x48 ; SIGNATURE_32('A','G','T','H')
  25. %endmacro
  26. SECTION .data
  27. ASM_PFX(ExceptionStubHeaderSize): DD Exception1Handle - ASM_PFX(Exception0Handle)
  28. CommonEntryAddr: DD CommonEntry
  29. SECTION .text
  30. AGENT_HANDLER_SIGNATURE
  31. ASM_PFX(Exception0Handle):
  32. cli
  33. push eax
  34. mov eax, 0
  35. jmp dword [CommonEntryAddr]
  36. AGENT_HANDLER_SIGNATURE
  37. Exception1Handle:
  38. cli
  39. push eax
  40. mov eax, 1
  41. jmp dword [CommonEntryAddr]
  42. AGENT_HANDLER_SIGNATURE
  43. Exception2Handle:
  44. cli
  45. push eax
  46. mov eax, 2
  47. jmp dword [CommonEntryAddr]
  48. AGENT_HANDLER_SIGNATURE
  49. Exception3Handle:
  50. cli
  51. push eax
  52. mov eax, 3
  53. jmp dword [CommonEntryAddr]
  54. AGENT_HANDLER_SIGNATURE
  55. Exception4Handle:
  56. cli
  57. push eax
  58. mov eax, 4
  59. jmp dword [CommonEntryAddr]
  60. AGENT_HANDLER_SIGNATURE
  61. Exception5Handle:
  62. cli
  63. push eax
  64. mov eax, 5
  65. jmp dword [CommonEntryAddr]
  66. AGENT_HANDLER_SIGNATURE
  67. Exception6Handle:
  68. cli
  69. push eax
  70. mov eax, 6
  71. jmp dword [CommonEntryAddr]
  72. AGENT_HANDLER_SIGNATURE
  73. Exception7Handle:
  74. cli
  75. push eax
  76. mov eax, 7
  77. jmp dword [CommonEntryAddr]
  78. AGENT_HANDLER_SIGNATURE
  79. Exception8Handle:
  80. cli
  81. push eax
  82. mov eax, 8
  83. jmp dword [CommonEntryAddr]
  84. AGENT_HANDLER_SIGNATURE
  85. Exception9Handle:
  86. cli
  87. push eax
  88. mov eax, 9
  89. jmp dword [CommonEntryAddr]
  90. AGENT_HANDLER_SIGNATURE
  91. Exception10Handle:
  92. cli
  93. push eax
  94. mov eax, 10
  95. jmp dword [CommonEntryAddr]
  96. AGENT_HANDLER_SIGNATURE
  97. Exception11Handle:
  98. cli
  99. push eax
  100. mov eax, 11
  101. jmp dword [CommonEntryAddr]
  102. AGENT_HANDLER_SIGNATURE
  103. Exception12Handle:
  104. cli
  105. push eax
  106. mov eax, 12
  107. jmp dword [CommonEntryAddr]
  108. AGENT_HANDLER_SIGNATURE
  109. Exception13Handle:
  110. cli
  111. push eax
  112. mov eax, 13
  113. jmp dword [CommonEntryAddr]
  114. AGENT_HANDLER_SIGNATURE
  115. Exception14Handle:
  116. cli
  117. push eax
  118. mov eax, 14
  119. jmp dword [CommonEntryAddr]
  120. AGENT_HANDLER_SIGNATURE
  121. Exception15Handle:
  122. cli
  123. push eax
  124. mov eax, 15
  125. jmp dword [CommonEntryAddr]
  126. AGENT_HANDLER_SIGNATURE
  127. Exception16Handle:
  128. cli
  129. push eax
  130. mov eax, 16
  131. jmp dword [CommonEntryAddr]
  132. AGENT_HANDLER_SIGNATURE
  133. Exception17Handle:
  134. cli
  135. push eax
  136. mov eax, 17
  137. jmp dword [CommonEntryAddr]
  138. AGENT_HANDLER_SIGNATURE
  139. Exception18Handle:
  140. cli
  141. push eax
  142. mov eax, 18
  143. jmp dword [CommonEntryAddr]
  144. AGENT_HANDLER_SIGNATURE
  145. Exception19Handle:
  146. cli
  147. push eax
  148. mov eax, 19
  149. jmp dword [CommonEntryAddr]
  150. AGENT_HANDLER_SIGNATURE
  151. ASM_PFX(TimerInterruptHandle):
  152. cli
  153. push eax
  154. mov eax, 32
  155. jmp dword [CommonEntryAddr]
  156. CommonEntry:
  157. ;
  158. ; +---------------------+
  159. ; + EFlags +
  160. ; +---------------------+
  161. ; + CS +
  162. ; +---------------------+
  163. ; + EIP +
  164. ; +---------------------+
  165. ; + Error Code +
  166. ; +---------------------+
  167. ; + EAX / Vector Number +
  168. ; +---------------------+
  169. ; + EBP +
  170. ; +---------------------+ <-- EBP
  171. ;
  172. cmp eax, DEBUG_EXCEPT_DOUBLE_FAULT
  173. je NoExtrPush
  174. cmp eax, DEBUG_EXCEPT_INVALID_TSS
  175. je NoExtrPush
  176. cmp eax, DEBUG_EXCEPT_SEG_NOT_PRESENT
  177. je NoExtrPush
  178. cmp eax, DEBUG_EXCEPT_STACK_FAULT
  179. je NoExtrPush
  180. cmp eax, DEBUG_EXCEPT_GP_FAULT
  181. je NoExtrPush
  182. cmp eax, DEBUG_EXCEPT_PAGE_FAULT
  183. je NoExtrPush
  184. cmp eax, DEBUG_EXCEPT_ALIGNMENT_CHECK
  185. je NoExtrPush
  186. push dword [esp]
  187. mov dword [esp + 4], 0
  188. NoExtrPush:
  189. push ebp
  190. mov ebp, esp ; save esp in ebp
  191. ;
  192. ; Make stack 16-byte alignment to make sure save fxrstor later
  193. ;
  194. and esp, 0xfffffff0
  195. sub esp, 12
  196. ; store UINT32 Edi, Esi, Ebp, Ebx, Edx, Ecx, Eax;
  197. push dword [ebp + 4] ; original eax
  198. push ebx
  199. push ecx
  200. push edx
  201. mov ebx, eax ; save vector in ebx
  202. mov eax, ebp
  203. add eax, 4 * 6
  204. push eax ; original ESP
  205. push dword [ebp] ; EBP
  206. push esi
  207. push edi
  208. ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
  209. ;; insure FXSAVE/FXRSTOR is enabled in CR4...
  210. ;; ... while we're at it, make sure DE is also enabled...
  211. mov eax, 1
  212. push ebx ; temporarily save value of ebx on stack
  213. cpuid ; use CPUID to determine if FXSAVE/FXRESTOR and
  214. ; DE are supported
  215. pop ebx ; restore value of ebx that was overwritten by CPUID
  216. mov eax, cr4
  217. push eax ; push cr4 firstly
  218. test edx, BIT24 ; Test for FXSAVE/FXRESTOR support
  219. jz .0
  220. or eax, BIT9 ; Set CR4.OSFXSR
  221. .0:
  222. test edx, BIT2 ; Test for Debugging Extensions support
  223. jz .1
  224. or eax, BIT3 ; Set CR4.DE
  225. .1:
  226. mov cr4, eax
  227. mov eax, cr3
  228. push eax
  229. mov eax, cr2
  230. push eax
  231. push 0 ; cr0 will not saved???
  232. mov eax, cr0
  233. push eax
  234. xor ecx, ecx
  235. mov ecx, Ss
  236. push ecx
  237. mov ecx, Cs
  238. push ecx
  239. mov ecx, Ds
  240. push ecx
  241. mov ecx, Es
  242. push ecx
  243. mov ecx, Fs
  244. push ecx
  245. mov ecx, Gs
  246. push ecx
  247. ;; EIP
  248. mov ecx, [ebp + 4 * 3] ; EIP
  249. push ecx
  250. ;; UINT32 Gdtr[2], Idtr[2];
  251. sub esp, 8
  252. sidt [esp]
  253. sub esp, 8
  254. sgdt [esp]
  255. ;; UINT32 Ldtr, Tr;
  256. xor eax, eax
  257. str ax
  258. push eax
  259. sldt ax
  260. push eax
  261. ;; EFlags
  262. mov ecx, [ebp + 4 * 5]
  263. push ecx
  264. ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
  265. mov eax, dr7
  266. push eax
  267. ;; clear Dr7 while executing debugger itself
  268. xor eax, eax
  269. mov dr7, eax
  270. ;; Dr6
  271. mov eax, dr6
  272. push eax
  273. ;; insure all status bits in dr6 are clear...
  274. xor eax, eax
  275. mov dr6, eax
  276. mov eax, dr3
  277. push eax
  278. mov eax, dr2
  279. push eax
  280. mov eax, dr1
  281. push eax
  282. mov eax, dr0
  283. push eax
  284. ;; Clear Direction Flag
  285. cld
  286. ;; FX_SAVE_STATE_IA32 FxSaveState;
  287. sub esp, 512
  288. mov edi, esp
  289. ;; Clear the buffer
  290. xor eax, eax
  291. mov ecx, 128 ;= 512 / 4
  292. rep stosd
  293. mov edi, esp
  294. test edx, BIT24 ; Test for FXSAVE/FXRESTOR support.
  295. ; edx still contains result from CPUID above
  296. jz .2
  297. fxsave [edi]
  298. .2:
  299. ;; save the exception data
  300. push dword [ebp + 8]
  301. ; call the C interrupt process function
  302. push esp ; Structure
  303. push ebx ; vector
  304. call ASM_PFX(InterruptProcess)
  305. add esp, 8
  306. ; skip the exception data
  307. add esp, 4
  308. ;; FX_SAVE_STATE_IA32 FxSaveState;
  309. mov esi, esp
  310. mov eax, 1
  311. cpuid ; use CPUID to determine if FXSAVE/FXRESTOR are supported
  312. test edx, BIT24 ; Test for FXSAVE/FXRESTOR support
  313. jz .3
  314. fxrstor [esi]
  315. .3:
  316. add esp, 512
  317. ;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
  318. pop eax
  319. mov dr0, eax
  320. pop eax
  321. mov dr1, eax
  322. pop eax
  323. mov dr2, eax
  324. pop eax
  325. mov dr3, eax
  326. ;; skip restore of dr6. We cleared dr6 during the context save.
  327. add esp, 4
  328. pop eax
  329. mov dr7, eax
  330. ;; set EFlags
  331. pop dword [ebp + 4 * 5] ; set EFLAGS in stack
  332. ;; UINT32 Ldtr, Tr;
  333. ;; UINT32 Gdtr[2], Idtr[2];
  334. ;; Best not let anyone mess with these particular registers...
  335. add esp, 24
  336. ;; UINT32 Eip;
  337. pop dword [ebp + 4 * 3] ; set EIP in stack
  338. ;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;
  339. ;; NOTE - modified segment registers could hang the debugger... We
  340. ;; could attempt to insulate ourselves against this possibility,
  341. ;; but that poses risks as well.
  342. ;;
  343. pop gs
  344. pop fs
  345. pop es
  346. pop ds
  347. pop dword [ebp + 4 * 4] ; set CS in stack
  348. pop ss
  349. ;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
  350. pop eax
  351. mov cr0, eax
  352. add esp, 4 ; skip for Cr1
  353. pop eax
  354. mov cr2, eax
  355. pop eax
  356. mov cr3, eax
  357. pop eax
  358. mov cr4, eax
  359. ;; restore general register
  360. pop edi
  361. pop esi
  362. pop dword [ebp] ; save updated ebp
  363. pop dword [ebp + 4] ; save updated esp
  364. pop edx
  365. pop ecx
  366. pop ebx
  367. pop eax
  368. mov esp, ebp
  369. pop ebp ; restore ebp maybe updated
  370. pop esp ; restore esp maybe updated
  371. sub esp, 4 * 3 ; restore interrupt pushced stack
  372. iretd