]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugSupportDxe/Ia32/AsmFuncs.asm
Clear the direction flag in interrupt/exception handlers' assembly entry code before...
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / AsmFuncs.asm
CommitLineData
7b414b4e 1;/** @file\r
2; Low leve IA32 specific debug support functions.\r
3;\r
ad8cbf71 4; Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5; This program and the accompanying materials\r
7b414b4e 6; are licensed and made available under the terms and conditions of the BSD License\r
7; which accompanies this distribution. The full text of the license may be found at\r
8; http://opensource.org/licenses/bsd-license.php\r
9;\r
10; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12;\r
13;**/\r
c1f23d63 14\r
15.586p\r
16.MODEL FLAT, C\r
17\r
18EXCPT32_DIVIDE_ERROR EQU 0\r
19EXCPT32_DEBUG EQU 1\r
20EXCPT32_NMI EQU 2\r
21EXCPT32_BREAKPOINT EQU 3\r
22EXCPT32_OVERFLOW EQU 4\r
23EXCPT32_BOUND EQU 5\r
24EXCPT32_INVALID_OPCODE EQU 6\r
25EXCPT32_DOUBLE_FAULT EQU 8\r
26EXCPT32_INVALID_TSS EQU 10\r
27EXCPT32_SEG_NOT_PRESENT EQU 11\r
28EXCPT32_STACK_FAULT EQU 12\r
29EXCPT32_GP_FAULT EQU 13\r
30EXCPT32_PAGE_FAULT EQU 14\r
31EXCPT32_FP_ERROR EQU 16\r
32EXCPT32_ALIGNMENT_CHECK EQU 17\r
33EXCPT32_MACHINE_CHECK EQU 18\r
34EXCPT32_SIMD EQU 19\r
35\r
36FXSTOR_FLAG EQU 01000000h ; bit cpuid 24 of feature flags\r
37\r
38;; The FXSTOR and FXRSTOR commands are used for saving and restoring the x87,\r
39;; MMX, SSE, SSE2, etc registers. The initialization of the debugsupport driver\r
40;; MUST check the CPUID feature flags to see that these instructions are available\r
41;; and fail to init if they are not.\r
42\r
43;; fxstor [edi]\r
44FXSTOR_EDI MACRO\r
45 db 0fh, 0aeh, 00000111y ; mod = 00, reg/op = 000, r/m = 111 = [edi]\r
46ENDM\r
47\r
48;; fxrstor [esi]\r
49FXRSTOR_ESI MACRO\r
50 db 0fh, 0aeh, 00001110y ; mod = 00, reg/op = 001, r/m = 110 = [esi]\r
51ENDM\r
52.DATA\r
53\r
54public OrigVector, InterruptEntryStub, StubSize, CommonIdtEntry, FxStorSupport\r
55\r
56StubSize dd InterruptEntryStubEnd - InterruptEntryStub\r
57AppEsp dd 11111111h ; ?\r
58DebugEsp dd 22222222h ; ?\r
59ExtraPush dd 33333333h ; ?\r
60ExceptData dd 44444444h ; ?\r
61Eflags dd 55555555h ; ?\r
62OrigVector dd 66666666h ; ?\r
63\r
64;; The declarations below define the memory region that will be used for the debug stack.\r
65;; The context record will be built by pushing register values onto this stack.\r
66;; It is imparitive that alignment be carefully managed, since the FXSTOR and\r
67;; FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.\r
68;;\r
69;; The stub will switch stacks from the application stack to the debuger stack\r
70;; and pushes the exception number.\r
71;;\r
72;; Then we building the context record on the stack. Since the stack grows down,\r
73;; we push the fields of the context record from the back to the front. There\r
74;; are 132 bytes of stack used prior allocating the 512 bytes of stack to be\r
75;; used as the memory buffer for the fxstor instruction. Therefore address of\r
76;; the buffer used for the FXSTOR instruction is &Eax - 132 - 512, which\r
77;; must be 16 byte aligned.\r
78;;\r
79;; We carefully locate the stack to make this happen.\r
80;;\r
81;; For reference, the context structure looks like this:\r
82;; struct {\r
83;; UINT32 ExceptionData;\r
84;; FX_SAVE_STATE_IA32 FxSaveState; // 512 bytes, must be 16 byte aligned\r
85;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
86;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
87;; UINT32 EFlags;\r
88;; UINT32 Ldtr, Tr;\r
89;; UINT32 Gdtr[2], Idtr[2];\r
90;; UINT32 Eip;\r
91;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;\r
92;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
93;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record\r
94\r
95\r
96align 16\r
97DebugStackEnd db "DbgStkEnd >>>>>>" ;; 16 byte long string - must be 16 bytes to preserve alignment\r
98 dd 1ffdh dup (000000000h) ;; 32K should be enough stack\r
7b414b4e 99 ;; This allocation is coocked to insure\r
c1f23d63 100 ;; that the the buffer for the FXSTORE instruction\r
101 ;; will be 16 byte aligned also.\r
102 ;;\r
103ExceptionNumber dd ? ;; first entry will be the vector number pushed by the stub\r
104\r
105DebugStackBegin db "<<<< DbgStkBegin" ;; initial debug ESP == DebugStackBegin, set in stub\r
106\r
107.CODE\r
108\r
109externdef InterruptDistrubutionHub:near\r
110\r
111;------------------------------------------------------------------------------\r
112; BOOLEAN\r
113; FxStorSupport (\r
114; void\r
115; )\r
116;\r
117; Abstract: Returns TRUE if FxStor instructions are supported\r
118;\r
119FxStorSupport PROC C PUBLIC\r
120\r
121;\r
122; cpuid corrupts ebx which must be preserved per the C calling convention\r
123;\r
124 push ebx\r
125 mov eax, 1\r
126 cpuid\r
127 mov eax, edx\r
128 and eax, FXSTOR_FLAG\r
129 shr eax, 24\r
130 pop ebx\r
131 ret\r
132FxStorSupport ENDP\r
133\r
134\r
c1f23d63 135\r
c1f23d63 136;------------------------------------------------------------------------------\r
137; void\r
138; Vect2Desc (\r
139; DESCRIPTOR * DestDesc,\r
140; void (*Vector) (void)\r
141; )\r
142;\r
143; Abstract: Encodes an IDT descriptor with the given physical address\r
144;\r
145Vect2Desc PROC C PUBLIC DestPtr:DWORD, Vector:DWORD\r
146\r
147 mov eax, Vector\r
148 mov ecx, DestPtr\r
149 mov word ptr [ecx], ax ; write bits 15..0 of offset\r
150 mov dx, cs\r
151 mov word ptr [ecx+2], dx ; SYS_CODE_SEL from GDT\r
152 mov word ptr [ecx+4], 0e00h OR 8000h ; type = 386 interrupt gate, present\r
153 shr eax, 16\r
154 mov word ptr [ecx+6], ax ; write bits 31..16 of offset\r
155\r
156 ret\r
157\r
158Vect2Desc ENDP\r
159\r
160\r
161\r
162;------------------------------------------------------------------------------\r
163; InterruptEntryStub\r
164;\r
165; Abstract: This code is not a function, but is a small piece of code that is\r
166; copied and fixed up once for each IDT entry that is hooked.\r
167;\r
168InterruptEntryStub::\r
169 mov AppEsp, esp ; save stack top\r
170 mov esp, offset DebugStackBegin ; switch to debugger stack\r
171 push 0 ; push vector number - will be modified before installed\r
172 db 0e9h ; jump rel32\r
173 dd 0 ; fixed up to relative address of CommonIdtEntry\r
174InterruptEntryStubEnd:\r
175\r
176\r
177\r
178;------------------------------------------------------------------------------\r
179; CommonIdtEntry\r
180;\r
181; Abstract: This code is not a function, but is the common part for all IDT\r
182; vectors.\r
183;\r
184CommonIdtEntry::\r
185;;\r
186;; At this point, the stub has saved the current application stack esp into AppEsp\r
187;; and switched stacks to the debug stack, where it pushed the vector number\r
188;;\r
189;; The application stack looks like this:\r
190;;\r
191;; ...\r
192;; (last application stack entry)\r
193;; eflags from interrupted task\r
194;; CS from interrupted task\r
195;; EIP from interrupted task\r
196;; Error code <-------------------- Only present for some exeption types\r
197;;\r
198;;\r
199\r
200\r
201;; The stub switched us to the debug stack and pushed the interrupt number.\r
202;;\r
203;; Next, construct the context record. It will be build on the debug stack by\r
204;; pushing the registers in the correct order so as to create the context structure\r
205;; on the debug stack. The context record must be built from the end back to the\r
206;; beginning because the stack grows down...\r
207;\r
208;; For reference, the context record looks like this:\r
209;;\r
210;; typedef\r
211;; struct {\r
212;; UINT32 ExceptionData;\r
213;; FX_SAVE_STATE_IA32 FxSaveState;\r
214;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
215;; UINT32 Cr0, Cr2, Cr3, Cr4;\r
216;; UINT32 EFlags;\r
217;; UINT32 Ldtr, Tr;\r
218;; UINT32 Gdtr[2], Idtr[2];\r
219;; UINT32 Eip;\r
220;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;\r
221;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
222;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record\r
223\r
224;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
225 pushad\r
226\r
227;; Save interrupt state eflags register...\r
228 pushfd\r
229 pop eax\r
230 mov dword ptr Eflags, eax\r
231\r
232;; We need to determine if any extra data was pushed by the exception, and if so, save it\r
233;; To do this, we check the exception number pushed by the stub, and cache the\r
234;; result in a variable since we'll need this again.\r
235 .IF ExceptionNumber == EXCPT32_DOUBLE_FAULT\r
236 mov ExtraPush, 1\r
237 .ELSEIF ExceptionNumber == EXCPT32_INVALID_TSS\r
238 mov ExtraPush, 1\r
239 .ELSEIF ExceptionNumber == EXCPT32_SEG_NOT_PRESENT\r
240 mov ExtraPush, 1\r
241 .ELSEIF ExceptionNumber == EXCPT32_STACK_FAULT\r
242 mov ExtraPush, 1\r
243 .ELSEIF ExceptionNumber == EXCPT32_GP_FAULT\r
244 mov ExtraPush, 1\r
245 .ELSEIF ExceptionNumber == EXCPT32_PAGE_FAULT\r
246 mov ExtraPush, 1\r
247 .ELSEIF ExceptionNumber == EXCPT32_ALIGNMENT_CHECK\r
248 mov ExtraPush, 1\r
249 .ELSE\r
250 mov ExtraPush, 0\r
251 .ENDIF\r
252\r
253;; If there's some extra data, save it also, and modify the saved AppEsp to effectively\r
254;; pop this value off the application's stack.\r
255 .IF ExtraPush == 1\r
256 mov eax, AppEsp\r
257 mov ebx, [eax]\r
258 mov ExceptData, ebx\r
259 add eax, 4\r
260 mov AppEsp, eax\r
261 .ELSE\r
262 mov ExceptData, 0\r
263 .ENDIF\r
264\r
265;; The "pushad" above pushed the debug stack esp. Since what we're actually doing\r
266;; is building the context record on the debug stack, we need to save the pushed\r
267;; debug ESP, and replace it with the application's last stack entry...\r
268 mov eax, [esp + 12]\r
269 mov DebugEsp, eax\r
270 mov eax, AppEsp\r
271 add eax, 12\r
272 ; application stack has eflags, cs, & eip, so\r
273 ; last actual application stack entry is\r
274 ; 12 bytes into the application stack.\r
275 mov [esp + 12], eax\r
276\r
277;; continue building context record\r
278;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero\r
279 mov eax, ss\r
280 push eax\r
281\r
282 ; CS from application is one entry back in application stack\r
283 mov eax, AppEsp\r
284 movzx eax, word ptr [eax + 4]\r
285 push eax\r
286\r
287 mov eax, ds\r
288 push eax\r
289 mov eax, es\r
290 push eax\r
291 mov eax, fs\r
292 push eax\r
293 mov eax, gs\r
294 push eax\r
295\r
296;; UINT32 Eip;\r
297 ; Eip from application is on top of application stack\r
298 mov eax, AppEsp\r
299 push dword ptr [eax]\r
300\r
301;; UINT32 Gdtr[2], Idtr[2];\r
302 push 0\r
303 push 0\r
304 sidt fword ptr [esp]\r
305 push 0\r
306 push 0\r
307 sgdt fword ptr [esp]\r
308\r
309;; UINT32 Ldtr, Tr;\r
310 xor eax, eax\r
311 str ax\r
312 push eax\r
313 sldt ax\r
314 push eax\r
315\r
316;; UINT32 EFlags;\r
317;; Eflags from application is two entries back in application stack\r
318 mov eax, AppEsp\r
319 push dword ptr [eax + 8]\r
320\r
321;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
322;; insure FXSAVE/FXRSTOR is enabled in CR4...\r
323;; ... while we're at it, make sure DE is also enabled...\r
324 mov eax, cr4\r
325 or eax, 208h\r
326 mov cr4, eax\r
327 push eax\r
328 mov eax, cr3\r
329 push eax\r
330 mov eax, cr2\r
331 push eax\r
332 push 0\r
333 mov eax, cr0\r
334 push eax\r
335\r
336;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
337 mov eax, dr7\r
338 push eax\r
339;; clear Dr7 while executing debugger itself\r
340 xor eax, eax\r
341 mov dr7, eax\r
342\r
343 mov eax, dr6\r
344 push eax\r
345;; insure all status bits in dr6 are clear...\r
346 xor eax, eax\r
347 mov dr6, eax\r
348\r
349 mov eax, dr3\r
350 push eax\r
351 mov eax, dr2\r
352 push eax\r
353 mov eax, dr1\r
354 push eax\r
355 mov eax, dr0\r
356 push eax\r
357\r
358;; FX_SAVE_STATE_IA32 FxSaveState;\r
359 sub esp, 512\r
360 mov edi, esp\r
361 ; IMPORTANT!! The debug stack has been carefully constructed to\r
362 ; insure that esp and edi are 16 byte aligned when we get here.\r
363 ; They MUST be. If they are not, a GP fault will occur.\r
364 FXSTOR_EDI\r
365\r
ad8cbf71 366;; UEFI calling convention for IA32 requires that Direction flag in EFLAGs is clear\r
367 cld\r
368\r
c1f23d63 369;; UINT32 ExceptionData;\r
370 mov eax, ExceptData\r
371 push eax\r
372\r
373; call to C code which will in turn call registered handler\r
374; pass in the vector number\r
375 mov eax, esp\r
376 push eax\r
377 mov eax, ExceptionNumber\r
378 push eax\r
379 call InterruptDistrubutionHub\r
380 add esp, 8\r
381\r
382; restore context...\r
383;; UINT32 ExceptionData;\r
384 add esp, 4\r
385\r
386;; FX_SAVE_STATE_IA32 FxSaveState;\r
387 mov esi, esp\r
388 FXRSTOR_ESI\r
389 add esp, 512\r
390\r
391;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
392 pop eax\r
393 mov dr0, eax\r
394 pop eax\r
395 mov dr1, eax\r
396 pop eax\r
397 mov dr2, eax\r
398 pop eax\r
399 mov dr3, eax\r
400;; skip restore of dr6. We cleared dr6 during the context save.\r
401 add esp, 4\r
402 pop eax\r
403 mov dr7, eax\r
404\r
405;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
406 pop eax\r
407 mov cr0, eax\r
408 add esp, 4\r
409 pop eax\r
410 mov cr2, eax\r
411 pop eax\r
412 mov cr3, eax\r
413 pop eax\r
414 mov cr4, eax\r
415\r
416;; UINT32 EFlags;\r
417 mov eax, AppEsp\r
418 pop dword ptr [eax + 8]\r
419\r
420;; UINT32 Ldtr, Tr;\r
421;; UINT32 Gdtr[2], Idtr[2];\r
422;; Best not let anyone mess with these particular registers...\r
423 add esp, 24\r
424\r
425;; UINT32 Eip;\r
426 pop dword ptr [eax]\r
427\r
428;; UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;\r
429;; NOTE - modified segment registers could hang the debugger... We\r
430;; could attempt to insulate ourselves against this possibility,\r
431;; but that poses risks as well.\r
432;;\r
433\r
434 pop gs\r
435 pop fs\r
436 pop es\r
437 pop ds\r
438 pop [eax + 4]\r
439 pop ss\r
440\r
441;; The next stuff to restore is the general purpose registers that were pushed\r
442;; using the "pushad" instruction.\r
443;;\r
444;; The value of ESP as stored in the context record is the application ESP\r
445;; including the 3 entries on the application stack caused by the exception\r
446;; itself. It may have been modified by the debug agent, so we need to\r
447;; determine if we need to relocate the application stack.\r
448\r
449 mov ebx, [esp + 12] ; move the potentially modified AppEsp into ebx\r
450 mov eax, AppEsp\r
451 add eax, 12\r
452 cmp ebx, eax\r
453 je NoAppStackMove\r
454\r
455 mov eax, AppEsp\r
456 mov ecx, [eax] ; EIP\r
457 mov [ebx], ecx\r
458\r
459 mov ecx, [eax + 4] ; CS\r
460 mov [ebx + 4], ecx\r
461\r
462 mov ecx, [eax + 8] ; EFLAGS\r
463 mov [ebx + 8], ecx\r
464\r
465 mov eax, ebx ; modify the saved AppEsp to the new AppEsp\r
466 mov AppEsp, eax\r
467NoAppStackMove:\r
468 mov eax, DebugEsp ; restore the DebugEsp on the debug stack\r
469 ; so our "popad" will not cause a stack switch\r
470 mov [esp + 12], eax\r
471\r
472 cmp ExceptionNumber, 068h\r
473 jne NoChain\r
474\r
475Chain:\r
476\r
477;; Restore eflags so when we chain, the flags will be exactly as if we were never here.\r
478;; We gin up the stack to do an iretd so we can get ALL the flags.\r
479 mov eax, AppEsp\r
480 mov ebx, [eax + 8]\r
481 and ebx, NOT 300h ; special handling for IF and TF\r
482 push ebx\r
483 push cs\r
484 push PhonyIretd\r
485 iretd\r
486PhonyIretd:\r
487\r
488;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
489 popad\r
490\r
491;; Switch back to application stack\r
492 mov esp, AppEsp\r
493\r
494;; Jump to original handler\r
495 jmp OrigVector\r
496\r
497NoChain:\r
498;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
499 popad\r
500\r
501;; Switch back to application stack\r
502 mov esp, AppEsp\r
503\r
504;; We're outa here...\r
505 iretd\r
506END\r
507\r
508\r
509\r