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