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