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