]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugSupportDxe/Ia32/AsmFuncs.asm
1. add public functions header.
[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
4; Copyright (c) 2006, Intel Corporation. <BR>\r
5; All rights reserved. This program and the accompanying materials\r
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
135;------------------------------------------------------------------------------\r
136; DESCRIPTOR *\r
137; GetIdtr (\r
138; void\r
139; )\r
140;\r
141; Abstract: Returns physical address of IDTR\r
142;\r
143GetIdtr PROC C PUBLIC\r
144 LOCAL IdtrBuf:FWORD\r
145\r
146 sidt IdtrBuf\r
147 mov eax, DWORD PTR IdtrBuf + 2\r
148 ret\r
149GetIdtr ENDP\r
150\r
151\r
152;------------------------------------------------------------------------------\r
153; BOOLEAN\r
154; WriteInterruptFlag (\r
155; BOOLEAN NewState\r
156; )\r
157;\r
158; Abstract: Programs interrupt flag to the requested state and returns previous\r
159; state.\r
160;\r
161WriteInterruptFlag PROC C PUBLIC State:DWORD\r
162\r
163 pushfd\r
164 pop eax\r
165 and eax, 200h\r
166 shr eax, 9\r
167 mov ecx, State\r
168 .IF cl == 0\r
169 cli\r
170 .ELSE\r
171 sti\r
172 .ENDIF\r
173 ret\r
174\r
175WriteInterruptFlag ENDP\r
176\r
177\r
178\r
179;------------------------------------------------------------------------------\r
180; void\r
181; Vect2Desc (\r
182; DESCRIPTOR * DestDesc,\r
183; void (*Vector) (void)\r
184; )\r
185;\r
186; Abstract: Encodes an IDT descriptor with the given physical address\r
187;\r
188Vect2Desc PROC C PUBLIC DestPtr:DWORD, Vector:DWORD\r
189\r
190 mov eax, Vector\r
191 mov ecx, DestPtr\r
192 mov word ptr [ecx], ax ; write bits 15..0 of offset\r
193 mov dx, cs\r
194 mov word ptr [ecx+2], dx ; SYS_CODE_SEL from GDT\r
195 mov word ptr [ecx+4], 0e00h OR 8000h ; type = 386 interrupt gate, present\r
196 shr eax, 16\r
197 mov word ptr [ecx+6], ax ; write bits 31..16 of offset\r
198\r
199 ret\r
200\r
201Vect2Desc ENDP\r
202\r
203\r
204\r
205;------------------------------------------------------------------------------\r
206; InterruptEntryStub\r
207;\r
208; Abstract: This code is not a function, but is a small piece of code that is\r
209; copied and fixed up once for each IDT entry that is hooked.\r
210;\r
211InterruptEntryStub::\r
212 mov AppEsp, esp ; save stack top\r
213 mov esp, offset DebugStackBegin ; switch to debugger stack\r
214 push 0 ; push vector number - will be modified before installed\r
215 db 0e9h ; jump rel32\r
216 dd 0 ; fixed up to relative address of CommonIdtEntry\r
217InterruptEntryStubEnd:\r
218\r
219\r
220\r
221;------------------------------------------------------------------------------\r
222; CommonIdtEntry\r
223;\r
224; Abstract: This code is not a function, but is the common part for all IDT\r
225; vectors.\r
226;\r
227CommonIdtEntry::\r
228;;\r
229;; At this point, the stub has saved the current application stack esp into AppEsp\r
230;; and switched stacks to the debug stack, where it pushed the vector number\r
231;;\r
232;; The application stack looks like this:\r
233;;\r
234;; ...\r
235;; (last application stack entry)\r
236;; eflags from interrupted task\r
237;; CS from interrupted task\r
238;; EIP from interrupted task\r
239;; Error code <-------------------- Only present for some exeption types\r
240;;\r
241;;\r
242\r
243\r
244;; The stub switched us to the debug stack and pushed the interrupt number.\r
245;;\r
246;; Next, construct the context record. It will be build on the debug stack by\r
247;; pushing the registers in the correct order so as to create the context structure\r
248;; on the debug stack. The context record must be built from the end back to the\r
249;; beginning because the stack grows down...\r
250;\r
251;; For reference, the context record looks like this:\r
252;;\r
253;; typedef\r
254;; struct {\r
255;; UINT32 ExceptionData;\r
256;; FX_SAVE_STATE_IA32 FxSaveState;\r
257;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
258;; UINT32 Cr0, Cr2, Cr3, Cr4;\r
259;; UINT32 EFlags;\r
260;; UINT32 Ldtr, Tr;\r
261;; UINT32 Gdtr[2], Idtr[2];\r
262;; UINT32 Eip;\r
263;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;\r
264;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
265;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record\r
266\r
267;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
268 pushad\r
269\r
270;; Save interrupt state eflags register...\r
271 pushfd\r
272 pop eax\r
273 mov dword ptr Eflags, eax\r
274\r
275;; We need to determine if any extra data was pushed by the exception, and if so, save it\r
276;; To do this, we check the exception number pushed by the stub, and cache the\r
277;; result in a variable since we'll need this again.\r
278 .IF ExceptionNumber == EXCPT32_DOUBLE_FAULT\r
279 mov ExtraPush, 1\r
280 .ELSEIF ExceptionNumber == EXCPT32_INVALID_TSS\r
281 mov ExtraPush, 1\r
282 .ELSEIF ExceptionNumber == EXCPT32_SEG_NOT_PRESENT\r
283 mov ExtraPush, 1\r
284 .ELSEIF ExceptionNumber == EXCPT32_STACK_FAULT\r
285 mov ExtraPush, 1\r
286 .ELSEIF ExceptionNumber == EXCPT32_GP_FAULT\r
287 mov ExtraPush, 1\r
288 .ELSEIF ExceptionNumber == EXCPT32_PAGE_FAULT\r
289 mov ExtraPush, 1\r
290 .ELSEIF ExceptionNumber == EXCPT32_ALIGNMENT_CHECK\r
291 mov ExtraPush, 1\r
292 .ELSE\r
293 mov ExtraPush, 0\r
294 .ENDIF\r
295\r
296;; If there's some extra data, save it also, and modify the saved AppEsp to effectively\r
297;; pop this value off the application's stack.\r
298 .IF ExtraPush == 1\r
299 mov eax, AppEsp\r
300 mov ebx, [eax]\r
301 mov ExceptData, ebx\r
302 add eax, 4\r
303 mov AppEsp, eax\r
304 .ELSE\r
305 mov ExceptData, 0\r
306 .ENDIF\r
307\r
308;; The "pushad" above pushed the debug stack esp. Since what we're actually doing\r
309;; is building the context record on the debug stack, we need to save the pushed\r
310;; debug ESP, and replace it with the application's last stack entry...\r
311 mov eax, [esp + 12]\r
312 mov DebugEsp, eax\r
313 mov eax, AppEsp\r
314 add eax, 12\r
315 ; application stack has eflags, cs, & eip, so\r
316 ; last actual application stack entry is\r
317 ; 12 bytes into the application stack.\r
318 mov [esp + 12], eax\r
319\r
320;; continue building context record\r
321;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero\r
322 mov eax, ss\r
323 push eax\r
324\r
325 ; CS from application is one entry back in application stack\r
326 mov eax, AppEsp\r
327 movzx eax, word ptr [eax + 4]\r
328 push eax\r
329\r
330 mov eax, ds\r
331 push eax\r
332 mov eax, es\r
333 push eax\r
334 mov eax, fs\r
335 push eax\r
336 mov eax, gs\r
337 push eax\r
338\r
339;; UINT32 Eip;\r
340 ; Eip from application is on top of application stack\r
341 mov eax, AppEsp\r
342 push dword ptr [eax]\r
343\r
344;; UINT32 Gdtr[2], Idtr[2];\r
345 push 0\r
346 push 0\r
347 sidt fword ptr [esp]\r
348 push 0\r
349 push 0\r
350 sgdt fword ptr [esp]\r
351\r
352;; UINT32 Ldtr, Tr;\r
353 xor eax, eax\r
354 str ax\r
355 push eax\r
356 sldt ax\r
357 push eax\r
358\r
359;; UINT32 EFlags;\r
360;; Eflags from application is two entries back in application stack\r
361 mov eax, AppEsp\r
362 push dword ptr [eax + 8]\r
363\r
364;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
365;; insure FXSAVE/FXRSTOR is enabled in CR4...\r
366;; ... while we're at it, make sure DE is also enabled...\r
367 mov eax, cr4\r
368 or eax, 208h\r
369 mov cr4, eax\r
370 push eax\r
371 mov eax, cr3\r
372 push eax\r
373 mov eax, cr2\r
374 push eax\r
375 push 0\r
376 mov eax, cr0\r
377 push eax\r
378\r
379;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
380 mov eax, dr7\r
381 push eax\r
382;; clear Dr7 while executing debugger itself\r
383 xor eax, eax\r
384 mov dr7, eax\r
385\r
386 mov eax, dr6\r
387 push eax\r
388;; insure all status bits in dr6 are clear...\r
389 xor eax, eax\r
390 mov dr6, eax\r
391\r
392 mov eax, dr3\r
393 push eax\r
394 mov eax, dr2\r
395 push eax\r
396 mov eax, dr1\r
397 push eax\r
398 mov eax, dr0\r
399 push eax\r
400\r
401;; FX_SAVE_STATE_IA32 FxSaveState;\r
402 sub esp, 512\r
403 mov edi, esp\r
404 ; IMPORTANT!! The debug stack has been carefully constructed to\r
405 ; insure that esp and edi are 16 byte aligned when we get here.\r
406 ; They MUST be. If they are not, a GP fault will occur.\r
407 FXSTOR_EDI\r
408\r
409;; UINT32 ExceptionData;\r
410 mov eax, ExceptData\r
411 push eax\r
412\r
413; call to C code which will in turn call registered handler\r
414; pass in the vector number\r
415 mov eax, esp\r
416 push eax\r
417 mov eax, ExceptionNumber\r
418 push eax\r
419 call InterruptDistrubutionHub\r
420 add esp, 8\r
421\r
422; restore context...\r
423;; UINT32 ExceptionData;\r
424 add esp, 4\r
425\r
426;; FX_SAVE_STATE_IA32 FxSaveState;\r
427 mov esi, esp\r
428 FXRSTOR_ESI\r
429 add esp, 512\r
430\r
431;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
432 pop eax\r
433 mov dr0, eax\r
434 pop eax\r
435 mov dr1, eax\r
436 pop eax\r
437 mov dr2, eax\r
438 pop eax\r
439 mov dr3, eax\r
440;; skip restore of dr6. We cleared dr6 during the context save.\r
441 add esp, 4\r
442 pop eax\r
443 mov dr7, eax\r
444\r
445;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
446 pop eax\r
447 mov cr0, eax\r
448 add esp, 4\r
449 pop eax\r
450 mov cr2, eax\r
451 pop eax\r
452 mov cr3, eax\r
453 pop eax\r
454 mov cr4, eax\r
455\r
456;; UINT32 EFlags;\r
457 mov eax, AppEsp\r
458 pop dword ptr [eax + 8]\r
459\r
460;; UINT32 Ldtr, Tr;\r
461;; UINT32 Gdtr[2], Idtr[2];\r
462;; Best not let anyone mess with these particular registers...\r
463 add esp, 24\r
464\r
465;; UINT32 Eip;\r
466 pop dword ptr [eax]\r
467\r
468;; UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;\r
469;; NOTE - modified segment registers could hang the debugger... We\r
470;; could attempt to insulate ourselves against this possibility,\r
471;; but that poses risks as well.\r
472;;\r
473\r
474 pop gs\r
475 pop fs\r
476 pop es\r
477 pop ds\r
478 pop [eax + 4]\r
479 pop ss\r
480\r
481;; The next stuff to restore is the general purpose registers that were pushed\r
482;; using the "pushad" instruction.\r
483;;\r
484;; The value of ESP as stored in the context record is the application ESP\r
485;; including the 3 entries on the application stack caused by the exception\r
486;; itself. It may have been modified by the debug agent, so we need to\r
487;; determine if we need to relocate the application stack.\r
488\r
489 mov ebx, [esp + 12] ; move the potentially modified AppEsp into ebx\r
490 mov eax, AppEsp\r
491 add eax, 12\r
492 cmp ebx, eax\r
493 je NoAppStackMove\r
494\r
495 mov eax, AppEsp\r
496 mov ecx, [eax] ; EIP\r
497 mov [ebx], ecx\r
498\r
499 mov ecx, [eax + 4] ; CS\r
500 mov [ebx + 4], ecx\r
501\r
502 mov ecx, [eax + 8] ; EFLAGS\r
503 mov [ebx + 8], ecx\r
504\r
505 mov eax, ebx ; modify the saved AppEsp to the new AppEsp\r
506 mov AppEsp, eax\r
507NoAppStackMove:\r
508 mov eax, DebugEsp ; restore the DebugEsp on the debug stack\r
509 ; so our "popad" will not cause a stack switch\r
510 mov [esp + 12], eax\r
511\r
512 cmp ExceptionNumber, 068h\r
513 jne NoChain\r
514\r
515Chain:\r
516\r
517;; Restore eflags so when we chain, the flags will be exactly as if we were never here.\r
518;; We gin up the stack to do an iretd so we can get ALL the flags.\r
519 mov eax, AppEsp\r
520 mov ebx, [eax + 8]\r
521 and ebx, NOT 300h ; special handling for IF and TF\r
522 push ebx\r
523 push cs\r
524 push PhonyIretd\r
525 iretd\r
526PhonyIretd:\r
527\r
528;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
529 popad\r
530\r
531;; Switch back to application stack\r
532 mov esp, AppEsp\r
533\r
534;; Jump to original handler\r
535 jmp OrigVector\r
536\r
537NoChain:\r
538;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
539 popad\r
540\r
541;; Switch back to application stack\r
542 mov esp, AppEsp\r
543\r
544;; We're outa here...\r
545 iretd\r
546END\r
547\r
548\r
549\r