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