+++ /dev/null
-;/** @file\r
-; Low leve IA32 specific debug support functions.\r
-;\r
-; Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
-; This program and the accompanying materials\r
-; are licensed and made available under the terms and conditions of the BSD License\r
-; which accompanies this distribution. The full text of the license may be found at\r
-; http://opensource.org/licenses/bsd-license.php\r
-;\r
-; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-;\r
-;**/\r
-\r
-.586p\r
-.MODEL FLAT, C\r
-\r
-EXCPT32_DIVIDE_ERROR EQU 0\r
-EXCPT32_DEBUG EQU 1\r
-EXCPT32_NMI EQU 2\r
-EXCPT32_BREAKPOINT EQU 3\r
-EXCPT32_OVERFLOW EQU 4\r
-EXCPT32_BOUND EQU 5\r
-EXCPT32_INVALID_OPCODE EQU 6\r
-EXCPT32_DOUBLE_FAULT EQU 8\r
-EXCPT32_INVALID_TSS EQU 10\r
-EXCPT32_SEG_NOT_PRESENT EQU 11\r
-EXCPT32_STACK_FAULT EQU 12\r
-EXCPT32_GP_FAULT EQU 13\r
-EXCPT32_PAGE_FAULT EQU 14\r
-EXCPT32_FP_ERROR EQU 16\r
-EXCPT32_ALIGNMENT_CHECK EQU 17\r
-EXCPT32_MACHINE_CHECK EQU 18\r
-EXCPT32_SIMD EQU 19\r
-\r
-FXSTOR_FLAG EQU 01000000h ; bit cpuid 24 of feature flags\r
-\r
-;; The FXSTOR and FXRSTOR commands are used for saving and restoring the x87,\r
-;; MMX, SSE, SSE2, etc registers. The initialization of the debugsupport driver\r
-;; MUST check the CPUID feature flags to see that these instructions are available\r
-;; and fail to init if they are not.\r
-\r
-;; fxstor [edi]\r
-FXSTOR_EDI MACRO\r
- db 0fh, 0aeh, 00000111y ; mod = 00, reg/op = 000, r/m = 111 = [edi]\r
-ENDM\r
-\r
-;; fxrstor [esi]\r
-FXRSTOR_ESI MACRO\r
- db 0fh, 0aeh, 00001110y ; mod = 00, reg/op = 001, r/m = 110 = [esi]\r
-ENDM\r
-.DATA\r
-\r
-public OrigVector, InterruptEntryStub, StubSize, CommonIdtEntry, FxStorSupport\r
-\r
-StubSize dd InterruptEntryStubEnd - InterruptEntryStub\r
-AppEsp dd 11111111h ; ?\r
-DebugEsp dd 22222222h ; ?\r
-ExtraPush dd 33333333h ; ?\r
-ExceptData dd 44444444h ; ?\r
-Eflags dd 55555555h ; ?\r
-OrigVector dd 66666666h ; ?\r
-\r
-;; The declarations below define the memory region that will be used for the debug stack.\r
-;; The context record will be built by pushing register values onto this stack.\r
-;; It is imparitive that alignment be carefully managed, since the FXSTOR and\r
-;; FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.\r
-;;\r
-;; The stub will switch stacks from the application stack to the debuger stack\r
-;; and pushes the exception number.\r
-;;\r
-;; Then we building the context record on the stack. Since the stack grows down,\r
-;; we push the fields of the context record from the back to the front. There\r
-;; are 132 bytes of stack used prior allocating the 512 bytes of stack to be\r
-;; used as the memory buffer for the fxstor instruction. Therefore address of\r
-;; the buffer used for the FXSTOR instruction is &Eax - 132 - 512, which\r
-;; must be 16 byte aligned.\r
-;;\r
-;; We carefully locate the stack to make this happen.\r
-;;\r
-;; For reference, the context structure looks like this:\r
-;; struct {\r
-;; UINT32 ExceptionData;\r
-;; FX_SAVE_STATE_IA32 FxSaveState; // 512 bytes, must be 16 byte aligned\r
-;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
-;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
-;; UINT32 EFlags;\r
-;; UINT32 Ldtr, Tr;\r
-;; UINT32 Gdtr[2], Idtr[2];\r
-;; UINT32 Eip;\r
-;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;\r
-;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
-;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record\r
-\r
-\r
-align 16\r
-DebugStackEnd db "DbgStkEnd >>>>>>" ;; 16 byte long string - must be 16 bytes to preserve alignment\r
- dd 1ffdh dup (000000000h) ;; 32K should be enough stack\r
- ;; This allocation is coocked to insure\r
- ;; that the the buffer for the FXSTORE instruction\r
- ;; will be 16 byte aligned also.\r
- ;;\r
-ExceptionNumber dd ? ;; first entry will be the vector number pushed by the stub\r
-\r
-DebugStackBegin db "<<<< DbgStkBegin" ;; initial debug ESP == DebugStackBegin, set in stub\r
-\r
-.CODE\r
-\r
-externdef InterruptDistrubutionHub:near\r
-\r
-;------------------------------------------------------------------------------\r
-; BOOLEAN\r
-; FxStorSupport (\r
-; void\r
-; )\r
-;\r
-; Abstract: Returns TRUE if FxStor instructions are supported\r
-;\r
-FxStorSupport PROC C PUBLIC\r
-\r
-;\r
-; cpuid corrupts ebx which must be preserved per the C calling convention\r
-;\r
- push ebx\r
- mov eax, 1\r
- cpuid\r
- mov eax, edx\r
- and eax, FXSTOR_FLAG\r
- shr eax, 24\r
- pop ebx\r
- ret\r
-FxStorSupport ENDP\r
-\r
-\r
-\r
-;------------------------------------------------------------------------------\r
-; void\r
-; Vect2Desc (\r
-; DESCRIPTOR * DestDesc,\r
-; void (*Vector) (void)\r
-; )\r
-;\r
-; Abstract: Encodes an IDT descriptor with the given physical address\r
-;\r
-Vect2Desc PROC C PUBLIC DestPtr:DWORD, Vector:DWORD\r
-\r
- mov eax, Vector\r
- mov ecx, DestPtr\r
- mov word ptr [ecx], ax ; write bits 15..0 of offset\r
- mov dx, cs\r
- mov word ptr [ecx+2], dx ; SYS_CODE_SEL from GDT\r
- mov word ptr [ecx+4], 0e00h OR 8000h ; type = 386 interrupt gate, present\r
- shr eax, 16\r
- mov word ptr [ecx+6], ax ; write bits 31..16 of offset\r
-\r
- ret\r
-\r
-Vect2Desc ENDP\r
-\r
-\r
-\r
-;------------------------------------------------------------------------------\r
-; InterruptEntryStub\r
-;\r
-; Abstract: This code is not a function, but is a small piece of code that is\r
-; copied and fixed up once for each IDT entry that is hooked.\r
-;\r
-InterruptEntryStub::\r
- mov AppEsp, esp ; save stack top\r
- mov esp, offset DebugStackBegin ; switch to debugger stack\r
- push 0 ; push vector number - will be modified before installed\r
- db 0e9h ; jump rel32\r
- dd 0 ; fixed up to relative address of CommonIdtEntry\r
-InterruptEntryStubEnd:\r
-\r
-\r
-\r
-;------------------------------------------------------------------------------\r
-; CommonIdtEntry\r
-;\r
-; Abstract: This code is not a function, but is the common part for all IDT\r
-; vectors.\r
-;\r
-CommonIdtEntry::\r
-;;\r
-;; At this point, the stub has saved the current application stack esp into AppEsp\r
-;; and switched stacks to the debug stack, where it pushed the vector number\r
-;;\r
-;; The application stack looks like this:\r
-;;\r
-;; ...\r
-;; (last application stack entry)\r
-;; eflags from interrupted task\r
-;; CS from interrupted task\r
-;; EIP from interrupted task\r
-;; Error code <-------------------- Only present for some exeption types\r
-;;\r
-;;\r
-\r
-\r
-;; The stub switched us to the debug stack and pushed the interrupt number.\r
-;;\r
-;; Next, construct the context record. It will be build on the debug stack by\r
-;; pushing the registers in the correct order so as to create the context structure\r
-;; on the debug stack. The context record must be built from the end back to the\r
-;; beginning because the stack grows down...\r
-;\r
-;; For reference, the context record looks like this:\r
-;;\r
-;; typedef\r
-;; struct {\r
-;; UINT32 ExceptionData;\r
-;; FX_SAVE_STATE_IA32 FxSaveState;\r
-;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
-;; UINT32 Cr0, Cr2, Cr3, Cr4;\r
-;; UINT32 EFlags;\r
-;; UINT32 Ldtr, Tr;\r
-;; UINT32 Gdtr[2], Idtr[2];\r
-;; UINT32 Eip;\r
-;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;\r
-;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
-;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record\r
-\r
-;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
- pushad\r
-\r
-;; Save interrupt state eflags register...\r
- pushfd\r
- pop eax\r
- mov dword ptr Eflags, eax\r
-\r
-;; We need to determine if any extra data was pushed by the exception, and if so, save it\r
-;; To do this, we check the exception number pushed by the stub, and cache the\r
-;; result in a variable since we'll need this again.\r
- .IF ExceptionNumber == EXCPT32_DOUBLE_FAULT\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_INVALID_TSS\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_SEG_NOT_PRESENT\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_STACK_FAULT\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_GP_FAULT\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_PAGE_FAULT\r
- mov ExtraPush, 1\r
- .ELSEIF ExceptionNumber == EXCPT32_ALIGNMENT_CHECK\r
- mov ExtraPush, 1\r
- .ELSE\r
- mov ExtraPush, 0\r
- .ENDIF\r
-\r
-;; If there's some extra data, save it also, and modify the saved AppEsp to effectively\r
-;; pop this value off the application's stack.\r
- .IF ExtraPush == 1\r
- mov eax, AppEsp\r
- mov ebx, [eax]\r
- mov ExceptData, ebx\r
- add eax, 4\r
- mov AppEsp, eax\r
- .ELSE\r
- mov ExceptData, 0\r
- .ENDIF\r
-\r
-;; The "pushad" above pushed the debug stack esp. Since what we're actually doing\r
-;; is building the context record on the debug stack, we need to save the pushed\r
-;; debug ESP, and replace it with the application's last stack entry...\r
- mov eax, [esp + 12]\r
- mov DebugEsp, eax\r
- mov eax, AppEsp\r
- add eax, 12\r
- ; application stack has eflags, cs, & eip, so\r
- ; last actual application stack entry is\r
- ; 12 bytes into the application stack.\r
- mov [esp + 12], eax\r
-\r
-;; continue building context record\r
-;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero\r
- mov eax, ss\r
- push eax\r
-\r
- ; CS from application is one entry back in application stack\r
- mov eax, AppEsp\r
- movzx eax, word ptr [eax + 4]\r
- push eax\r
-\r
- mov eax, ds\r
- push eax\r
- mov eax, es\r
- push eax\r
- mov eax, fs\r
- push eax\r
- mov eax, gs\r
- push eax\r
-\r
-;; UINT32 Eip;\r
- ; Eip from application is on top of application stack\r
- mov eax, AppEsp\r
- push dword ptr [eax]\r
-\r
-;; UINT32 Gdtr[2], Idtr[2];\r
- push 0\r
- push 0\r
- sidt fword ptr [esp]\r
- push 0\r
- push 0\r
- sgdt fword ptr [esp]\r
-\r
-;; UINT32 Ldtr, Tr;\r
- xor eax, eax\r
- str ax\r
- push eax\r
- sldt ax\r
- push eax\r
-\r
-;; UINT32 EFlags;\r
-;; Eflags from application is two entries back in application stack\r
- mov eax, AppEsp\r
- push dword ptr [eax + 8]\r
-\r
-;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
-;; insure FXSAVE/FXRSTOR is enabled in CR4...\r
-;; ... while we're at it, make sure DE is also enabled...\r
- mov eax, cr4\r
- or eax, 208h\r
- mov cr4, eax\r
- push eax\r
- mov eax, cr3\r
- push eax\r
- mov eax, cr2\r
- push eax\r
- push 0\r
- mov eax, cr0\r
- push eax\r
-\r
-;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
- mov eax, dr7\r
- push eax\r
-;; clear Dr7 while executing debugger itself\r
- xor eax, eax\r
- mov dr7, eax\r
-\r
- mov eax, dr6\r
- push eax\r
-;; insure all status bits in dr6 are clear...\r
- xor eax, eax\r
- mov dr6, eax\r
-\r
- mov eax, dr3\r
- push eax\r
- mov eax, dr2\r
- push eax\r
- mov eax, dr1\r
- push eax\r
- mov eax, dr0\r
- push eax\r
-\r
-;; FX_SAVE_STATE_IA32 FxSaveState;\r
- sub esp, 512\r
- mov edi, esp\r
- ; IMPORTANT!! The debug stack has been carefully constructed to\r
- ; insure that esp and edi are 16 byte aligned when we get here.\r
- ; They MUST be. If they are not, a GP fault will occur.\r
- FXSTOR_EDI\r
-\r
-;; UEFI calling convention for IA32 requires that Direction flag in EFLAGs is clear\r
- cld\r
-\r
-;; UINT32 ExceptionData;\r
- mov eax, ExceptData\r
- push eax\r
-\r
-; call to C code which will in turn call registered handler\r
-; pass in the vector number\r
- mov eax, esp\r
- push eax\r
- mov eax, ExceptionNumber\r
- push eax\r
- call InterruptDistrubutionHub\r
- add esp, 8\r
-\r
-; restore context...\r
-;; UINT32 ExceptionData;\r
- add esp, 4\r
-\r
-;; FX_SAVE_STATE_IA32 FxSaveState;\r
- mov esi, esp\r
- FXRSTOR_ESI\r
- add esp, 512\r
-\r
-;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
- pop eax\r
- mov dr0, eax\r
- pop eax\r
- mov dr1, eax\r
- pop eax\r
- mov dr2, eax\r
- pop eax\r
- mov dr3, eax\r
-;; skip restore of dr6. We cleared dr6 during the context save.\r
- add esp, 4\r
- pop eax\r
- mov dr7, eax\r
-\r
-;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;\r
- pop eax\r
- mov cr0, eax\r
- add esp, 4\r
- pop eax\r
- mov cr2, eax\r
- pop eax\r
- mov cr3, eax\r
- pop eax\r
- mov cr4, eax\r
-\r
-;; UINT32 EFlags;\r
- mov eax, AppEsp\r
- pop dword ptr [eax + 8]\r
-\r
-;; UINT32 Ldtr, Tr;\r
-;; UINT32 Gdtr[2], Idtr[2];\r
-;; Best not let anyone mess with these particular registers...\r
- add esp, 24\r
-\r
-;; UINT32 Eip;\r
- pop dword ptr [eax]\r
-\r
-;; UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;\r
-;; NOTE - modified segment registers could hang the debugger... We\r
-;; could attempt to insulate ourselves against this possibility,\r
-;; but that poses risks as well.\r
-;;\r
-\r
- pop gs\r
- pop fs\r
- pop es\r
- pop ds\r
- pop [eax + 4]\r
- pop ss\r
-\r
-;; The next stuff to restore is the general purpose registers that were pushed\r
-;; using the "pushad" instruction.\r
-;;\r
-;; The value of ESP as stored in the context record is the application ESP\r
-;; including the 3 entries on the application stack caused by the exception\r
-;; itself. It may have been modified by the debug agent, so we need to\r
-;; determine if we need to relocate the application stack.\r
-\r
- mov ebx, [esp + 12] ; move the potentially modified AppEsp into ebx\r
- mov eax, AppEsp\r
- add eax, 12\r
- cmp ebx, eax\r
- je NoAppStackMove\r
-\r
- mov eax, AppEsp\r
- mov ecx, [eax] ; EIP\r
- mov [ebx], ecx\r
-\r
- mov ecx, [eax + 4] ; CS\r
- mov [ebx + 4], ecx\r
-\r
- mov ecx, [eax + 8] ; EFLAGS\r
- mov [ebx + 8], ecx\r
-\r
- mov eax, ebx ; modify the saved AppEsp to the new AppEsp\r
- mov AppEsp, eax\r
-NoAppStackMove:\r
- mov eax, DebugEsp ; restore the DebugEsp on the debug stack\r
- ; so our "popad" will not cause a stack switch\r
- mov [esp + 12], eax\r
-\r
- cmp ExceptionNumber, 068h\r
- jne NoChain\r
-\r
-Chain:\r
-\r
-;; Restore eflags so when we chain, the flags will be exactly as if we were never here.\r
-;; We gin up the stack to do an iretd so we can get ALL the flags.\r
- mov eax, AppEsp\r
- mov ebx, [eax + 8]\r
- and ebx, NOT 300h ; special handling for IF and TF\r
- push ebx\r
- push cs\r
- push PhonyIretd\r
- iretd\r
-PhonyIretd:\r
-\r
-;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
- popad\r
-\r
-;; Switch back to application stack\r
- mov esp, AppEsp\r
-\r
-;; Jump to original handler\r
- jmp OrigVector\r
-\r
-NoChain:\r
-;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;\r
- popad\r
-\r
-;; Switch back to application stack\r
- mov esp, AppEsp\r
-\r
-;; We're outa here...\r
- iretd\r
-END\r
-\r
-\r
-\r