]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugSupportDxe/X64/AsmFuncs.S
MdeModulePkg/DebugSupportDxe: Fix XCODE5 build failure
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / X64 / AsmFuncs.S
CommitLineData
4b738c76
HT
1///**@file\r
2// Low leve x64 specific debug support functions.\r
3//\r
0fff7d67 4// Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
4b738c76
HT
5// Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
6// This program and the accompanying materials\r
7// are licensed and made available under the terms and conditions of the BSD License\r
8// which accompanies this distribution. The full text of the license may be found at\r
9// http://opensource.org/licenses/bsd-license.php\r
10//\r
11// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13//\r
14//**/\r
15\r
16ASM_GLOBAL ASM_PFX(OrigVector)\r
17ASM_GLOBAL ASM_PFX(InterruptEntryStub)\r
18ASM_GLOBAL ASM_PFX(StubSize)\r
19ASM_GLOBAL ASM_PFX(CommonIdtEntry)\r
20ASM_GLOBAL ASM_PFX(FxStorSupport)\r
21\r
22.data\r
23\r
24ASM_PFX(StubSize): .long ASM_PFX(InterruptEntryStubEnd) - ASM_PFX(InterruptEntryStub)\r
25ASM_PFX(AppRsp): .long 0x11111111 # ?\r
26 .long 0x11111111 # ?\r
27ASM_PFX(DebugRsp): .long 0x22222222 # ?\r
28 .long 0x22222222 # ?\r
29ASM_PFX(ExtraPush): .long 0x33333333 # ?\r
30 .long 0x33333333 # ?\r
31ASM_PFX(ExceptData): .long 0x44444444 # ?\r
32 .long 0x44444444 # ?\r
33ASM_PFX(Rflags): .long 0x55555555 # ?\r
34 .long 0x55555555 # ?\r
35ASM_PFX(OrigVector): .long 0x66666666 # ?\r
36 .long 0x66666666 # ?\r
37\r
38// The declarations below define the memory region that will be used for the debug stack.\r
39// The context record will be built by pushing register values onto this stack.\r
40// It is imparitive that alignment be carefully managed, since the FXSTOR and\r
41// FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.\r
42//\r
43// The stub will switch stacks from the application stack to the debuger stack\r
44// and pushes the exception number.\r
45//\r
46// Then we building the context record on the stack. Since the stack grows down,\r
47// we push the fields of the context record from the back to the front. There\r
48// are 336 bytes of stack used prior allocating the 512 bytes of stack to be\r
49// used as the memory buffer for the fxstor instruction. Therefore address of\r
50// the buffer used for the FXSTOR instruction is &Eax - 336 - 512, which\r
51// must be 16 byte aligned.\r
52//\r
53// We carefully locate the stack to make this happen.\r
54//\r
55// For reference, the context structure looks like this:\r
56// struct {\r
57// UINT64 ExceptionData;\r
58// FX_SAVE_STATE_X64 FxSaveState; // 512 bytes, must be 16 byte aligned\r
59// UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
60// UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;\r
61// UINT64 RFlags;\r
62// UINT64 Ldtr, Tr;\r
63// UINT64 Gdtr[2], Idtr[2];\r
64// UINT64 Rip;\r
65// UINT64 Gs, Fs, Es, Ds, Cs, Ss;\r
66// UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;\r
67// UINT64 R8, R9, R10, R11, R12, R13, R14, R15;\r
68// } SYSTEM_CONTEXT_X64; // 64 bit system context record\r
69\r
70.p2align 4\r
71DebugStackEnd : .ascii "DbgStkEnd >>>>>>" # 16 byte long string - must be 16 bytes to preserve alignment\r
72 .fill 0x1ffc, 4, 0x00000000\r
73 # 32K should be enough stack\r
74 # This allocation is coocked to insure\r
75 # that the the buffer for the FXSTORE instruction\r
76 # will be 16 byte aligned also.\r
77 #\r
78ASM_PFX(ExceptionNumber): .long 0x77777777 # first entry will be the vector number pushed by the stub\r
79 .long 0x77777777 # ?\r
80\r
81DebugStackBegin : .ascii "<<<< DbgStkBegin" # initial debug ESP == DebugStackBegin, set in stub\r
82\r
83\r
84.text\r
85\r
86//------------------------------------------------------------------------------\r
87// BOOLEAN\r
88// FxStorSupport (\r
89// void\r
90// )\r
91//\r
92// Abstract: Returns TRUE if FxStor instructions are supported\r
93//\r
94ASM_GLOBAL ASM_PFX(FxStorSupport)\r
95ASM_PFX(FxStorSupport):\r
96//\r
97// cpuid corrupts rbx which must be preserved per the C calling convention\r
98//\r
99 pushq %rbx\r
100 movq $1, %rax\r
101 cpuid\r
102 movl %edx, %eax\r
103 andq $0x01000000, %rax\r
104 shrq $24, %rax\r
105 popq %rbx\r
106 ret\r
107//------------------------------------------------------------------------------\r
108// void\r
109// Vect2Desc (\r
110// IA32_IDT_GATE_DESCRIPTOR * DestDesc, // rcx\r
111// void (*Vector) (void) // rdx\r
112// )\r
113//\r
114// Abstract: Encodes an IDT descriptor with the given physical address\r
115//\r
116ASM_GLOBAL ASM_PFX(Vect2Desc)\r
117ASM_PFX(Vect2Desc):\r
118 movq %rdx, %rax\r
119 movw %ax, (%rcx) # write bits 15..0 of offset\r
120 movw %cs, %dx\r
121 movw %dx, 2(%rcx) # SYS_CODE_SEL from GDT\r
122 movw $(0x0e00 | 0x8000), 4(%rcx) # type = 386 interrupt gate, present\r
123 shrq $16, %rax\r
124 movw %ax, 6(%rcx) # write bits 31..16 of offset\r
125 shrq $16, %rax\r
126 movl %eax, 8(%rcx) # write bits 63..32 of offset\r
127\r
128 ret\r
129\r
130//------------------------------------------------------------------------------\r
131// InterruptEntryStub\r
132//\r
133// Abstract: This code is not a function, but is a small piece of code that is\r
134// copied and fixed up once for each IDT entry that is hooked.\r
135//\r
136ASM_GLOBAL ASM_PFX(InterruptEntryStub)\r
137ASM_PFX(InterruptEntryStub):\r
138\r
139 pushq $0 # push vector number - will be modified before installed\r
140 jmp ASM_PFX(CommonIdtEntry)\r
141\r
142ASM_GLOBAL ASM_PFX(InterruptEntryStubEnd)\r
143ASM_PFX(InterruptEntryStubEnd):\r
144\r
145//------------------------------------------------------------------------------\r
146// CommonIdtEntry\r
147//\r
148// Abstract: This code is not a function, but is the common part for all IDT\r
149// vectors.\r
150//\r
151ASM_GLOBAL ASM_PFX(CommonIdtEntry)\r
152//\r
153// At this point, the stub has saved the current application stack esp into AppRsp\r
154// and switched stacks to the debug stack, where it pushed the vector number\r
155//\r
156// The application stack looks like this:\r
157//\r
158// ...\r
159// (last application stack entry)\r
160// [16 bytes alignment, do not care it]\r
161// SS from interrupted task\r
162// RSP from interrupted task\r
163// rflags from interrupted task\r
164// CS from interrupted task\r
165// RIP from interrupted task\r
166// Error code <-------------------- Only present for some exeption types\r
167//\r
168// Vector Number <----------------- pushed in our IDT Entry\r
169//\r
170\r
171\r
172// The stub switched us to the debug stack and pushed the interrupt number.\r
173//\r
174// Next, construct the context record. It will be build on the debug stack by\r
175// pushing the registers in the correct order so as to create the context structure\r
176// on the debug stack. The context record must be built from the end back to the\r
177// beginning because the stack grows down...\r
178//\r
179// For reference, the context record looks like this:\r
180//\r
181// typedef\r
182// struct {\r
183// UINT64 ExceptionData;\r
184// FX_SAVE_STATE_X64 FxSaveState;\r
185// UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
186// UINT64 Cr0, Cr2, Cr3, Cr4, Cr8;\r
187// UINT64 RFlags;\r
188// UINT64 Ldtr, Tr;\r
189// UINT64 Gdtr[2], Idtr[2];\r
190// UINT64 Rip;\r
191// UINT64 Gs, Fs, Es, Ds, Cs, Ss;\r
192// UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;\r
193// UINT64 R8, R9, R10, R11, R12, R13, R14, R15;\r
194// } SYSTEM_CONTEXT_X64; // 64\r
195ASM_PFX(CommonIdtEntry):\r
196// NOTE: we save rsp here to prevent compiler put rip reference cause error AppRsp\r
197 pushq %rax\r
198 movq (8)(%rsp), %rax # save vector number\r
199 movq %rax, ASM_PFX(ExceptionNumber)(%rip) # save vector number\r
200 popq %rax\r
201 addq $8, %rsp # pop vector number\r
202 movq %rsp, ASM_PFX(AppRsp)(%rip) # save stack top\r
203 movq DebugStackBegin(%rip), %rsp # switch to debugger stack\r
204 subq $8, %rsp # leave space for vector number\r
205// UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;\r
206// UINT64 R8, R9, R10, R11, R12, R13, R14, R15;\r
207 pushq %r15\r
208 pushq %r14\r
209 pushq %r13\r
210 pushq %r12\r
211 pushq %r11\r
212 pushq %r10\r
213 pushq %r9\r
214 pushq %r8\r
215 pushq %rax\r
216 pushq %rcx\r
217 pushq %rdx\r
218 pushq %rbx\r
219 pushq %rsp\r
220 pushq %rbp\r
221 pushq %rsi\r
222 pushq %rdi\r
223// Save interrupt state rflags register...\r
224 pushfq\r
225 popq %rax\r
226 movq %rax, ASM_PFX(Rflags)(%rip)\r
227// We need to determine if any extra data was pushed by the exception, and if so, save it\r
228// To do this, we check the exception number pushed by the stub, and cache the\r
229// result in a variable since we'll need this again.\r
230 cmpl $0, ASM_PFX(ExceptionNumber)(%rip)\r
231 jz ExtraPushOne\r
232 cmpl $10, ASM_PFX(ExceptionNumber)(%rip)\r
233 jz ExtraPushOne\r
234 cmpl $11, ASM_PFX(ExceptionNumber)(%rip)\r
235 jz ExtraPushOne\r
236 cmpl $12, ASM_PFX(ExceptionNumber)(%rip)\r
237 jz ExtraPushOne\r
238 cmpl $13, ASM_PFX(ExceptionNumber)(%rip)\r
239 jz ExtraPushOne\r
240 cmpl $14, ASM_PFX(ExceptionNumber)(%rip)\r
241 jz ExtraPushOne\r
242 cmpl $17, ASM_PFX(ExceptionNumber)(%rip)\r
243 jz ExtraPushOne\r
244 movl $0, ASM_PFX(ExtraPush)(%rip)\r
245 movl $0, ASM_PFX(ExceptData)(%rip)\r
246 jmp ExtraPushDone\r
247ExtraPushOne:\r
248 movl $1, ASM_PFX(ExtraPush)(%rip)\r
249\r
250// If there's some extra data, save it also, and modify the saved AppRsp to effectively\r
251// pop this value off the application's stack.\r
252 movq ASM_PFX(AppRsp)(%rip), %rax\r
253 movq (%rax), %rbx\r
254 movq %rbx, ASM_PFX(ExceptData)(%rip)\r
255 addq $8, %rax\r
256 movq %rax, ASM_PFX(AppRsp)(%rip)\r
257\r
258ExtraPushDone:\r
259\r
260// The "push" above pushed the debug stack rsp. Since what we're actually doing\r
261// is building the context record on the debug stack, we need to save the pushed\r
262// debug RSP, and replace it with the application's last stack entry...\r
263 movq 24(%rsp), %rax\r
264 movq %rax, ASM_PFX(DebugRsp)(%rip)\r
265 movq ASM_PFX(AppRsp)(%rip), %rax\r
266 movq 24(%rax), %rax\r
267 # application stack has ss, rsp, rflags, cs, & rip, so\r
268 # last actual application stack entry is saved at offset\r
269 # 24 bytes from stack top.\r
270 movq %rax, 24(%rsp)\r
271\r
272// continue building context record\r
273// UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero\r
274 mov %ss, %rax\r
275 pushq %rax\r
276 # CS from application is one entry back in application stack\r
277 movq ASM_PFX(AppRsp)(%rip), %rax\r
0fff7d67 278 movzwq 8(%rax), %rax\r
4b738c76
HT
279 pushq %rax\r
280\r
281 mov %ds, %rax\r
282 pushq %rax\r
a1252397 283 mov %es, %rax\r
4b738c76
HT
284 pushq %rax\r
285 mov %fs, %rax\r
286 pushq %rax\r
287 mov %gs, %rax\r
288 pushq %rax\r
289// UINT64 Rip;\r
290 # Rip from application is on top of application stack\r
291 movq ASM_PFX(AppRsp)(%rip), %rax\r
292 pushq (%rax)\r
293// UINT64 Gdtr[2], Idtr[2];\r
294 push $0\r
295 push $0\r
296 sidtq (%rsp)\r
297 push $0\r
298 push $0\r
299 sgdtq (%rsp)\r
300\r
301// UINT64 Ldtr, Tr;\r
302 xorq %rax, %rax\r
303 str %ax\r
304 pushq %rax\r
305 sldt %ax\r
306 pushq %rax\r
307\r
308// UINT64 RFlags;\r
309// Rflags from application is two entries back in application stack\r
310 movq ASM_PFX(AppRsp)(%rip), %rax\r
311 pushq 16(%rax)\r
312// UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;\r
313// insure FXSAVE/FXRSTOR is enabled in CR4...\r
314// ... while we're at it, make sure DE is also enabled...\r
315 movq %cr8, %rax\r
316 pushq %rax\r
317 movq %cr4, %rax\r
318 orq $0x208, %rax\r
319 movq %rax, %cr4\r
320 pushq %rax\r
321 movq %cr3, %rax\r
322 pushq %rax\r
323 movq %cr2, %rax\r
324 pushq %rax\r
325 push $0\r
326 movq %cr0, %rax\r
327 pushq %rax\r
328// UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
329 movq %dr7, %rax\r
330 pushq %rax\r
331// clear Dr7 while executing debugger itself\r
332 xorq %rax, %rax\r
333 movq %rax, %dr7\r
334\r
335 movq %dr6, %rax\r
336 pushq %rax\r
337// insure all status bits in dr6 are clear...\r
338 xorq %rax, %rax\r
339 movq %rax, %dr6\r
340\r
341 movq %dr3, %rax\r
342 pushq %rax\r
343 movq %dr2, %rax\r
344 pushq %rax\r
345 movq %dr1, %rax\r
346 pushq %rax\r
347 movq %dr0, %rax\r
348 pushq %rax\r
349\r
350// FX_SAVE_STATE_X64 FxSaveState;\r
351 subq $512, %rsp\r
352 movq %rsp, %rdi\r
353 # IMPORTANT!! The debug stack has been carefully constructed to\r
354 # insure that rsp and rdi are 16 byte aligned when we get here.\r
355 # They MUST be. If they are not, a GP fault will occur.\r
356\r
357 # FXSTOR_RDI\r
358 fxsave (%rdi)\r
359\r
360// UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear\r
361 cld\r
362 \r
363// UINT64 ExceptionData;\r
364 movq ASM_PFX(ExceptData)(%rip), %rax\r
365 pushq %rax\r
366\r
367// call to C code which will in turn call registered handler\r
368// pass in the vector number\r
369 movq %rsp, %rdx\r
370 movq ASM_PFX(ExceptionNumber)(%rip), %rcx\r
371 subq $40, %rsp\r
372 call ASM_PFX(InterruptDistrubutionHub)\r
373 addq $40, %rsp\r
374// restore context...\r
375// UINT64 ExceptionData;\r
376 addq $8, %rsp\r
377\r
378// FX_SAVE_STATE_X64 FxSaveState;\r
379 movq %rsp, %rsi\r
380\r
381 # FXRSTOR_RSI\r
382 fxrstor (%rsi)\r
383\r
384 addq $512, %rsp\r
385\r
386// UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
387 popq %rax\r
388 movq %rax, %dr0\r
389 popq %rax\r
390 movq %rax, %dr1\r
391 popq %rax\r
392 movq %rax, %dr2\r
393 popq %rax\r
394 movq %rax, %dr3\r
395\r
396// skip restore of dr6. We cleared dr6 during the context save.\r
397 addq $8, %rsp\r
398 popq %rax\r
399 movq %rax, %dr7\r
400\r
401// UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;\r
402 popq %rax\r
403 movq %rax, %cr0\r
404 addq $8, %rsp\r
405 popq %rax\r
406 movq %rax, %cr2\r
407 popq %rax\r
408 movq %rax, %cr3\r
409 popq %rax\r
410 movq %rax, %cr4\r
411 popq %rax\r
412 movq %rax, %cr8\r
413// UINT64 RFlags;\r
414 movq ASM_PFX(AppRsp)(%rip), %rax\r
415 popq 16(%rax)\r
416// UINT64 Ldtr, Tr;\r
417// UINT64 Gdtr[2], Idtr[2];\r
418// Best not let anyone mess with these particular registers...\r
419 addq $48, %rsp\r
420// UINT64 Rip;\r
421 popq (%rax)\r
422\r
423// UINT64 Gs, Fs, Es, Ds, Cs, Ss;\r
424// NOTE - modified segment registers could hang the debugger... We\r
425// could attempt to insulate ourselves against this possibility,\r
426// but that poses risks as well.\r
427//\r
428\r
429 popq %rax\r
430 # mov %rax, %gs\r
431 popq %rax\r
432 # mov %rax, %fs\r
433 popq %rax\r
434 mov %rax, %es\r
435 popq %rax\r
436 mov %rax, %ds\r
437 movq ASM_PFX(AppRsp)(%rip), %rax\r
438 popq 8(%rax)\r
439 popq %rax\r
440 mov %rax, %ss\r
441## The next stuff to restore is the general purpose registers that were pushed\r
442## using the "push" instruction.\r
443##\r
444## The value of RSP as stored in the context record is the application RSP\r
445## including the 5 entries on the application stack caused by the exception\r
446## itself. It may have been modified by the debug agent, so we need to\r
447## determine if we need to relocate the application stack.\r
448\r
449 movq 24(%rsp), %rbx # move the potentially modified AppRsp into rbx\r
450 movq ASM_PFX(AppRsp)(%rip), %rax\r
451 movq 24(%rax), %rax\r
452 cmpq %rax, %rbx\r
453 je NoAppStackMove\r
454\r
455 movq ASM_PFX(AppRsp)(%rip), %rax\r
456 movq (%rax), %rcx # RIP\r
457 movq %rcx, (%rbx)\r
458\r
459 movq 8(%rax), %rcx # CS\r
460 movq %rcx, 8(%rbx)\r
461\r
462 movq 16(%rax), %rcx # RFLAGS\r
463 movq %rcx, 16(%rbx)\r
464\r
465 movq 24(%rax), %rcx # RSP\r
466 movq %rcx, 24(%rbx)\r
467\r
468 movq 32(%rax), %rcx # SS\r
469 movq %rcx, 32(%rbx)\r
470\r
471 movq %rbx, %rax # modify the saved AppRsp to the new AppRsp\r
472 movq %rax, ASM_PFX(AppRsp)(%rip)\r
473NoAppStackMove:\r
474 movq ASM_PFX(DebugRsp)(%rip), %rax # restore the DebugRsp on the debug stack\r
475 # so our "pop" will not cause a stack switch\r
476 movq %rax, 24(%rsp)\r
477\r
478 cmpl $0x068, ASM_PFX(ExceptionNumber)(%rip)\r
479 jne NoChain\r
480\r
481Chain:\r
482\r
483// Restore rflags so when we chain, the flags will be exactly as if we were never here.\r
484// We gin up the stack to do an iretq so we can get ALL the flags.\r
485 movq ASM_PFX(AppRsp)(%rip), %rax\r
486 movq 40(%rax), %rbx\r
487 pushq %rbx\r
488 mov %ss, %rax\r
489 pushq %rax\r
490 movq %rsp, %rax\r
491 addq $16, %rax\r
492 pushq %rax\r
493 movq ASM_PFX(AppRsp)(%rip), %rax\r
494 movq 16(%rax), %rbx\r
495 andq $0xfffffffffffffcff, %rbx # special handling for IF and TF\r
496 pushq %rbx\r
497 mov %cs, %rax\r
498 pushq %rax\r
499 movq PhonyIretq(%rip), %rax\r
500 pushq %rax\r
501 iretq\r
502PhonyIretq:\r
503\r
504// UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;\r
505// UINT64 R8, R9, R10, R11, R12, R13, R14, R15;\r
506 popq %rdi\r
507 popq %rsi\r
508 popq %rbp\r
509 popq %rsp\r
510 popq %rbx\r
511 popq %rdx\r
512 popq %rcx\r
513 popq %rax\r
514 popq %r8\r
515 popq %r9\r
516 popq %r10\r
517 popq %r11\r
518 popq %r12\r
519 popq %r13\r
520 popq %r14\r
521 popq %r15\r
522\r
523// Switch back to application stack\r
524 movq ASM_PFX(AppRsp)(%rip), %rsp\r
525// Jump to original handler\r
526 jmp ASM_PFX(OrigVector)\r
527NoChain:\r
528// UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;\r
529// UINT64 R8, R9, R10, R11, R12, R13, R14, R15;\r
530 popq %rdi\r
531 popq %rsi\r
532 popq %rbp\r
533 popq %rsp\r
534 popq %rbx\r
535 popq %rdx\r
536 popq %rcx\r
537 popq %rax\r
538 popq %r8\r
539 popq %r9\r
540 popq %r10\r
541 popq %r11\r
542 popq %r12\r
543 popq %r13\r
544 popq %r14\r
545 popq %r15\r
546\r
547// Switch back to application stack\r
548 movq ASM_PFX(AppRsp)(%rip), %rsp\r
549\r
550// We're outa here...\r
551 iret\r