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