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