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