diff options
author | 2023-10-10 14:33:42 +0000 | |
---|---|---|
committer | 2023-10-10 14:33:42 +0000 | |
commit | af1a266670d040d2f4083ff309d732d648afba2a (patch) | |
tree | 2fc46203448ddcc6f81546d379abfaeb323575e9 /roms/edk2/MdeModulePkg/Universal/DebugSupportDxe | |
parent | e02cda008591317b1625707ff8e115a4841aa889 (diff) |
Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
Diffstat (limited to 'roms/edk2/MdeModulePkg/Universal/DebugSupportDxe')
12 files changed, 2276 insertions, 0 deletions
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.c b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.c new file mode 100644 index 000000000..1a03ba465 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupport.c @@ -0,0 +1,127 @@ +/** @file
+ Top level C file for debug support driver. Contains initialization function.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "PlDebugSupport.h"
+
+EFI_DEBUG_SUPPORT_PROTOCOL mDebugSupportProtocolInterface = {
+ EFI_ISA,
+ GetMaximumProcessorIndex,
+ RegisterPeriodicCallback,
+ RegisterExceptionCallback,
+ InvalidateInstructionCache
+};
+
+
+/**
+ Debug Support Driver entry point.
+
+ Checks to see if there's not already a Debug Support protocol installed for
+ the selected processor before installing it.
+
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.
+ @param[in] SystemTable A pointer to the EFI System Table.
+
+ @retval EFI_SUCCESS The entry point is executed successfully.
+ @retval EFI_ALREADY_STARTED Debug Support protocol is installed already.
+ @retval other Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+InitializeDebugSupportDriver (
+ IN EFI_HANDLE ImageHandle,
+ IN EFI_SYSTEM_TABLE *SystemTable
+ )
+{
+ EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProtocolPtr;
+ EFI_STATUS Status;
+ EFI_HANDLE Handle;
+ EFI_HANDLE *HandlePtr;
+ UINTN NumHandles;
+ EFI_DEBUG_SUPPORT_PROTOCOL *DebugSupportProtocolPtr;
+
+ //
+ // First check to see that the debug support protocol for this processor
+ // type is not already installed
+ //
+ Status = gBS->LocateHandleBuffer (
+ ByProtocol,
+ &gEfiDebugSupportProtocolGuid,
+ NULL,
+ &NumHandles,
+ &HandlePtr
+ );
+
+ if (Status != EFI_NOT_FOUND) {
+ do {
+ NumHandles--;
+ Status = gBS->OpenProtocol (
+ HandlePtr[NumHandles],
+ &gEfiDebugSupportProtocolGuid,
+ (VOID **) &DebugSupportProtocolPtr,
+ ImageHandle,
+ NULL,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+ if ((Status == EFI_SUCCESS) && (DebugSupportProtocolPtr->Isa == EFI_ISA)) {
+ //
+ // a Debug Support protocol has been installed for this processor
+ //
+ FreePool (HandlePtr);
+ Status = EFI_ALREADY_STARTED;
+ goto ErrExit;
+ }
+ } while (NumHandles > 0);
+ FreePool (HandlePtr);
+ }
+
+ //
+ // Get our image information and install platform specific unload handler
+ //
+ Status = gBS->OpenProtocol (
+ ImageHandle,
+ &gEfiLoadedImageProtocolGuid,
+ (VOID **) &LoadedImageProtocolPtr,
+ ImageHandle,
+ NULL,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL
+ );
+ ASSERT (!EFI_ERROR (Status));
+ if (Status != EFI_SUCCESS) {
+ goto ErrExit;
+ }
+
+ LoadedImageProtocolPtr->Unload = PlUnloadDebugSupportDriver;
+
+ //
+ // Call hook for processor specific initialization
+ //
+ Status = PlInitializeDebugSupportDriver ();
+ ASSERT (!EFI_ERROR (Status));
+ if (Status != EFI_SUCCESS) {
+ goto ErrExit;
+ }
+
+ //
+ // Install Debug Support protocol to new handle
+ //
+ Handle = NULL;
+ Status = gBS->InstallProtocolInterface (
+ &Handle,
+ &gEfiDebugSupportProtocolGuid,
+ EFI_NATIVE_INTERFACE,
+ &mDebugSupportProtocolInterface
+ );
+ ASSERT (!EFI_ERROR (Status));
+ if (Status != EFI_SUCCESS) {
+ goto ErrExit;
+ }
+
+ErrExit:
+ return Status;
+}
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf new file mode 100644 index 000000000..1bb8635bf --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.inf @@ -0,0 +1,72 @@ +## @file
+# This driver installs Debug Support protocol for the selected processor.
+#
+# This driver provides the capabilities for debug-agent to gain control of the machine
+# when certain types of events occur, i.e. breakpoint, processor execptions, etc. It also
+# provides debug-agent to periodically gain control during operation of the machine to
+# check for asynchronous commands form the host.
+#
+# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+ INF_VERSION = 0x00010005
+ BASE_NAME = DebugSupportDxe
+ MODULE_UNI_FILE = DebugSupportDxe.uni
+ FILE_GUID = 911D584C-35F7-4955-BEF9-B452769DDC3A
+ MODULE_TYPE = DXE_DRIVER
+ VERSION_STRING = 1.0
+ ENTRY_POINT = InitializeDebugSupportDriver
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Sources]
+ DebugSupport.c
+
+[Sources.Ia32]
+ Ia32/DebugSupport.h
+ Ia32/PlDebugSupport.c
+ Ia32/PlDebugSupport.h
+ Ia32/PlDebugSupportIa32.c
+ Ia32/AsmFuncs.nasm
+
+[Sources.X64]
+ Ia32/DebugSupport.h
+ Ia32/PlDebugSupport.c
+ X64/PlDebugSupport.h
+ X64/PlDebugSupportX64.c
+ X64/AsmFuncs.nasm
+
+[Packages]
+ MdePkg/MdePkg.dec
+ MdeModulePkg/MdeModulePkg.dec
+
+
+[LibraryClasses]
+ UefiBootServicesTableLib
+ MemoryAllocationLib
+ BaseMemoryLib
+ UefiDriverEntryPoint
+ DebugLib
+
+[LibraryClasses.IA32, LibraryClasses.X64]
+ BaseLib
+
+[Protocols]
+ gEfiLoadedImageProtocolGuid ## CONSUMES
+ gEfiDebugSupportProtocolGuid ## PRODUCES
+
+
+[Depex]
+ TRUE
+
+[UserExtensions.TianoCore."ExtraFiles"]
+ DebugSupportDxeExtra.uni
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.uni b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.uni new file mode 100644 index 000000000..1403d41ef --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxe.uni @@ -0,0 +1,19 @@ +// /** @file
+// This driver installs Debug Support protocol for the selected processor.
+//
+// This driver provides the capabilities for debug-agent to gain control of the machine
+// when certain types of events occur, i.e. breakpoint, processor execptions, etc. It also
+// provides debug-agent to periodically gain control during operation of the machine to
+// check for asynchronous commands form the host.
+//
+// Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT #language en-US "Installs Debug Support protocol for the selected processor"
+
+#string STR_MODULE_DESCRIPTION #language en-US "This driver provides the capabilities for debug-agent to gain control of the machine when certain types of events occur, i.e. breakpoint, processor exceptions, etc. It also provides debug-agent to periodically gain control during operation of the machine to check for asynchronous commands from the host."
+
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxeExtra.uni b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxeExtra.uni new file mode 100644 index 000000000..7ceb95d3f --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/DebugSupportDxeExtra.uni @@ -0,0 +1,14 @@ +// /** @file
+// DebugSupportDxe Localized Strings and Content
+//
+// Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"Debug Support DXE Driver"
+
+
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/AsmFuncs.nasm b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/AsmFuncs.nasm new file mode 100644 index 000000000..cfb418748 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/AsmFuncs.nasm @@ -0,0 +1,493 @@ +;/** @file
+; Low leve IA32 specific debug support functions.
+;
+; Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
+; SPDX-License-Identifier: BSD-2-Clause-Patent
+;
+;**/
+
+%define EXCPT32_DIVIDE_ERROR 0
+%define EXCPT32_DEBUG 1
+%define EXCPT32_NMI 2
+%define EXCPT32_BREAKPOINT 3
+%define EXCPT32_OVERFLOW 4
+%define EXCPT32_BOUND 5
+%define EXCPT32_INVALID_OPCODE 6
+%define EXCPT32_DOUBLE_FAULT 8
+%define EXCPT32_INVALID_TSS 10
+%define EXCPT32_SEG_NOT_PRESENT 11
+%define EXCPT32_STACK_FAULT 12
+%define EXCPT32_GP_FAULT 13
+%define EXCPT32_PAGE_FAULT 14
+%define EXCPT32_FP_ERROR 16
+%define EXCPT32_ALIGNMENT_CHECK 17
+%define EXCPT32_MACHINE_CHECK 18
+%define EXCPT32_SIMD 19
+
+%define FXSTOR_FLAG 0x1000000 ; bit cpuid 24 of feature flags
+
+;; The FXSTOR and FXRSTOR commands are used for saving and restoring the x87,
+;; MMX, SSE, SSE2, etc registers. The initialization of the debugsupport driver
+;; MUST check the CPUID feature flags to see that these instructions are available
+;; and fail to init if they are not.
+
+;; fxstor [edi]
+%macro FXSTOR_EDI 0
+ db 0xf, 0xae, 00000111y ; mod = 00, reg/op = 000, r/m = 111 = [edi]
+%endmacro
+
+;; fxrstor [esi]
+%macro FXRSTOR_ESI 0
+ db 0xf, 0xae, 00001110y ; mod = 00, reg/op = 001, r/m = 110 = [esi]
+%endmacro
+SECTION .data
+
+global ASM_PFX(OrigVector)
+global ASM_PFX(InterruptEntryStub)
+global ASM_PFX(StubSize)
+global ASM_PFX(CommonIdtEntry)
+global ASM_PFX(FxStorSupport)
+extern ASM_PFX(InterruptDistrubutionHub)
+
+ASM_PFX(StubSize): dd InterruptEntryStubEnd - ASM_PFX(InterruptEntryStub)
+AppEsp: dd 0x11111111 ; ?
+DebugEsp: dd 0x22222222 ; ?
+ExtraPush: dd 0x33333333 ; ?
+ExceptData: dd 0x44444444 ; ?
+Eflags: dd 0x55555555 ; ?
+ASM_PFX(OrigVector): dd 0x66666666 ; ?
+
+;; The declarations below define the memory region that will be used for the debug stack.
+;; The context record will be built by pushing register values onto this stack.
+;; It is imparitive that alignment be carefully managed, since the FXSTOR and
+;; FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.
+;;
+;; The stub will switch stacks from the application stack to the debuger stack
+;; and pushes the exception number.
+;;
+;; Then we building the context record on the stack. Since the stack grows down,
+;; we push the fields of the context record from the back to the front. There
+;; are 132 bytes of stack used prior allocating the 512 bytes of stack to be
+;; used as the memory buffer for the fxstor instruction. Therefore address of
+;; the buffer used for the FXSTOR instruction is &Eax - 132 - 512, which
+;; must be 16 byte aligned.
+;;
+;; We carefully locate the stack to make this happen.
+;;
+;; For reference, the context structure looks like this:
+;; struct {
+;; UINT32 ExceptionData;
+;; FX_SAVE_STATE_IA32 FxSaveState; // 512 bytes, must be 16 byte aligned
+;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
+;; UINT32 EFlags;
+;; UINT32 Ldtr, Tr;
+;; UINT32 Gdtr[2], Idtr[2];
+;; UINT32 Eip;
+;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;
+;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
+;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record
+
+align 16
+DebugStackEnd: db "DbgStkEnd >>>>>>" ;; 16 byte long string - must be 16 bytes to preserve alignment
+ times 0x1ffc dd 0x0 ;; 32K should be enough stack
+ ;; This allocation is coocked to insure
+ ;; that the the buffer for the FXSTORE instruction
+ ;; will be 16 byte aligned also.
+ ;;
+ExceptionNumber: dd 0 ;; first entry will be the vector number pushed by the stub
+
+DebugStackBegin: db "<<<< DbgStkBegin" ;; initial debug ESP == DebugStackBegin, set in stub
+
+SECTION .text
+
+;------------------------------------------------------------------------------
+; BOOLEAN
+; FxStorSupport (
+; void
+; )
+;
+; Abstract: Returns TRUE if FxStor instructions are supported
+;
+global ASM_PFX(FxStorSupport)
+ASM_PFX(FxStorSupport):
+
+;
+; cpuid corrupts ebx which must be preserved per the C calling convention
+;
+ push ebx
+ mov eax, 1
+ cpuid
+ mov eax, edx
+ and eax, FXSTOR_FLAG
+ shr eax, 24
+ pop ebx
+ ret
+
+;------------------------------------------------------------------------------
+; void
+; Vect2Desc (
+; DESCRIPTOR * DestDesc,
+; void (*Vector) (void)
+; )
+;
+; Abstract: Encodes an IDT descriptor with the given physical address
+;
+global ASM_PFX(Vect2Desc)
+ASM_PFX(Vect2Desc):
+ push ebp
+ mov ebp, esp
+ mov eax, [ebp + 0xC]
+ mov ecx, [ebp + 0x8]
+ mov word [ecx], ax ; write bits 15..0 of offset
+ mov dx, cs
+ mov word [ecx+2], dx ; SYS_CODE_SEL from GDT
+ mov word [ecx+4], 0xe00 | 0x8000 ; type = 386 interrupt gate, present
+ shr eax, 16
+ mov word [ecx+6], ax ; write bits 31..16 of offset
+ leave
+ ret
+
+;------------------------------------------------------------------------------
+; InterruptEntryStub
+;
+; Abstract: This code is not a function, but is a small piece of code that is
+; copied and fixed up once for each IDT entry that is hooked.
+;
+ASM_PFX(InterruptEntryStub):
+ mov [AppEsp], esp ; save stack top
+ mov esp, DebugStackBegin ; switch to debugger stack
+ push 0 ; push vector number - will be modified before installed
+ db 0xe9 ; jump rel32
+ dd 0 ; fixed up to relative address of CommonIdtEntry
+InterruptEntryStubEnd:
+
+;------------------------------------------------------------------------------
+; CommonIdtEntry
+;
+; Abstract: This code is not a function, but is the common part for all IDT
+; vectors.
+;
+ASM_PFX(CommonIdtEntry):
+;;
+;; At this point, the stub has saved the current application stack esp into AppEsp
+;; and switched stacks to the debug stack, where it pushed the vector number
+;;
+;; The application stack looks like this:
+;;
+;; ...
+;; (last application stack entry)
+;; eflags from interrupted task
+;; CS from interrupted task
+;; EIP from interrupted task
+;; Error code <-------------------- Only present for some exeption types
+;;
+;;
+
+;; The stub switched us to the debug stack and pushed the interrupt number.
+;;
+;; Next, construct the context record. It will be build on the debug stack by
+;; pushing the registers in the correct order so as to create the context structure
+;; on the debug stack. The context record must be built from the end back to the
+;; beginning because the stack grows down...
+;
+;; For reference, the context record looks like this:
+;;
+;; typedef
+;; struct {
+;; UINT32 ExceptionData;
+;; FX_SAVE_STATE_IA32 FxSaveState;
+;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+;; UINT32 Cr0, Cr2, Cr3, Cr4;
+;; UINT32 EFlags;
+;; UINT32 Ldtr, Tr;
+;; UINT32 Gdtr[2], Idtr[2];
+;; UINT32 Eip;
+;; UINT32 Gs, Fs, Es, Ds, Cs, Ss;
+;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
+;; } SYSTEM_CONTEXT_IA32; // 32 bit system context record
+
+;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
+ pushad
+
+;; Save interrupt state eflags register...
+ pushfd
+ pop eax
+ mov [Eflags], eax
+
+;; We need to determine if any extra data was pushed by the exception, and if so, save it
+;; To do this, we check the exception number pushed by the stub, and cache the
+;; result in a variable since we'll need this again.
+ cmp dword [ExceptionNumber], EXCPT32_DOUBLE_FAULT
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_INVALID_TSS
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_SEG_NOT_PRESENT
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_STACK_FAULT
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_GP_FAULT
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_PAGE_FAULT
+ jz ExtraPushOne
+ cmp dword [ExceptionNumber], EXCPT32_ALIGNMENT_CHECK
+ jz ExtraPushOne
+ mov dword [ExtraPush], 0
+ mov dword [ExceptData], 0
+ jmp ExtraPushDone
+
+ExtraPushOne:
+ mov dword [ExtraPush], 1
+
+;; If there's some extra data, save it also, and modify the saved AppEsp to effectively
+;; pop this value off the application's stack.
+ mov eax, [AppEsp]
+ mov ebx, [eax]
+ mov [ExceptData], ebx
+ add eax, 4
+ mov [AppEsp], eax
+
+ExtraPushDone:
+
+;; The "pushad" above pushed the debug stack esp. Since what we're actually doing
+;; is building the context record on the debug stack, we need to save the pushed
+;; debug ESP, and replace it with the application's last stack entry...
+ mov eax, [esp + 12]
+ mov [DebugEsp], eax
+ mov eax, [AppEsp]
+ add eax, 12
+ ; application stack has eflags, cs, & eip, so
+ ; last actual application stack entry is
+ ; 12 bytes into the application stack.
+ mov [esp + 12], eax
+
+;; continue building context record
+;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
+ mov eax, ss
+ push eax
+
+ ; CS from application is one entry back in application stack
+ mov eax, [AppEsp]
+ movzx eax, word [eax + 4]
+ push eax
+
+ mov eax, ds
+ push eax
+ mov eax, es
+ push eax
+ mov eax, fs
+ push eax
+ mov eax, gs
+ push eax
+
+;; UINT32 Eip;
+ ; Eip from application is on top of application stack
+ mov eax, [AppEsp]
+ push dword [eax]
+
+;; UINT32 Gdtr[2], Idtr[2];
+ push 0
+ push 0
+ sidt [esp]
+ push 0
+ push 0
+ sgdt [esp]
+
+;; UINT32 Ldtr, Tr;
+ xor eax, eax
+ str ax
+ push eax
+ sldt ax
+ push eax
+
+;; UINT32 EFlags;
+;; Eflags from application is two entries back in application stack
+ mov eax, [AppEsp]
+ push dword [eax + 8]
+
+;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
+;; insure FXSAVE/FXRSTOR is enabled in CR4...
+;; ... while we're at it, make sure DE is also enabled...
+ mov eax, cr4
+ or eax, 0x208
+ mov cr4, eax
+ push eax
+ mov eax, cr3
+ push eax
+ mov eax, cr2
+ push eax
+ push 0
+ mov eax, cr0
+ push eax
+
+;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+ mov eax, dr7
+ push eax
+;; clear Dr7 while executing debugger itself
+ xor eax, eax
+ mov dr7, eax
+
+ mov eax, dr6
+ push eax
+;; insure all status bits in dr6 are clear...
+ xor eax, eax
+ mov dr6, eax
+
+ mov eax, dr3
+ push eax
+ mov eax, dr2
+ push eax
+ mov eax, dr1
+ push eax
+ mov eax, dr0
+ push eax
+
+;; FX_SAVE_STATE_IA32 FxSaveState;
+ sub esp, 512
+ mov edi, esp
+ ; IMPORTANT!! The debug stack has been carefully constructed to
+ ; insure that esp and edi are 16 byte aligned when we get here.
+ ; They MUST be. If they are not, a GP fault will occur.
+ FXSTOR_EDI
+
+;; UEFI calling convention for IA32 requires that Direction flag in EFLAGs is clear
+ cld
+
+;; UINT32 ExceptionData;
+ mov eax, [ExceptData]
+ push eax
+
+; call to C code which will in turn call registered handler
+; pass in the vector number
+ mov eax, esp
+ push eax
+ mov eax, [ExceptionNumber]
+ push eax
+ call ASM_PFX(InterruptDistrubutionHub)
+ add esp, 8
+
+; restore context...
+;; UINT32 ExceptionData;
+ add esp, 4
+
+;; FX_SAVE_STATE_IA32 FxSaveState;
+ mov esi, esp
+ FXRSTOR_ESI
+ add esp, 512
+
+;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+ pop eax
+ mov dr0, eax
+ pop eax
+ mov dr1, eax
+ pop eax
+ mov dr2, eax
+ pop eax
+ mov dr3, eax
+;; skip restore of dr6. We cleared dr6 during the context save.
+ add esp, 4
+ pop eax
+ mov dr7, eax
+
+;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4;
+ pop eax
+ mov cr0, eax
+ add esp, 4
+ pop eax
+ mov cr2, eax
+ pop eax
+ mov cr3, eax
+ pop eax
+ mov cr4, eax
+
+;; UINT32 EFlags;
+ mov eax, [AppEsp]
+ pop dword [eax + 8]
+
+;; UINT32 Ldtr, Tr;
+;; UINT32 Gdtr[2], Idtr[2];
+;; Best not let anyone mess with these particular registers...
+ add esp, 24
+
+;; UINT32 Eip;
+ pop dword [eax]
+
+;; UINT32 SegGs, SegFs, SegEs, SegDs, SegCs, SegSs;
+;; NOTE - modified segment registers could hang the debugger... We
+;; could attempt to insulate ourselves against this possibility,
+;; but that poses risks as well.
+;;
+
+ pop gs
+ pop fs
+ pop es
+ pop ds
+ pop dword [eax + 4]
+ pop ss
+
+;; The next stuff to restore is the general purpose registers that were pushed
+;; using the "pushad" instruction.
+;;
+;; The value of ESP as stored in the context record is the application ESP
+;; including the 3 entries on the application stack caused by the exception
+;; itself. It may have been modified by the debug agent, so we need to
+;; determine if we need to relocate the application stack.
+
+ mov ebx, [esp + 12] ; move the potentially modified AppEsp into ebx
+ mov eax, [AppEsp]
+ add eax, 12
+ cmp ebx, eax
+ je NoAppStackMove
+
+ mov eax, [AppEsp]
+ mov ecx, [eax] ; EIP
+ mov [ebx], ecx
+
+ mov ecx, [eax + 4] ; CS
+ mov [ebx + 4], ecx
+
+ mov ecx, [eax + 8] ; EFLAGS
+ mov [ebx + 8], ecx
+
+ mov eax, ebx ; modify the saved AppEsp to the new AppEsp
+ mov [AppEsp], eax
+NoAppStackMove:
+ mov eax, [DebugEsp] ; restore the DebugEsp on the debug stack
+ ; so our "popad" will not cause a stack switch
+ mov [esp + 12], eax
+
+ cmp dword [ExceptionNumber], 0x68
+ jne NoChain
+
+Chain:
+
+;; Restore eflags so when we chain, the flags will be exactly as if we were never here.
+;; We gin up the stack to do an iretd so we can get ALL the flags.
+ mov eax, [AppEsp]
+ mov ebx, [eax + 8]
+ and ebx, ~ 0x300 ; special handling for IF and TF
+ push ebx
+ push cs
+ push PhonyIretd
+ iretd
+PhonyIretd:
+
+;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
+ popad
+
+;; Switch back to application stack
+ mov esp, [AppEsp]
+
+;; Jump to original handler
+ jmp [ASM_PFX(OrigVector)]
+
+NoChain:
+;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax;
+ popad
+
+;; Switch back to application stack
+ mov esp, [AppEsp]
+
+;; We're outa here...
+ iretd
+
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/DebugSupport.h b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/DebugSupport.h new file mode 100644 index 000000000..9f88fab71 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/DebugSupport.h @@ -0,0 +1,292 @@ +/** @file
+ Generic debug support macros, typedefs and prototypes for IA32/x64.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _DEBUG_SUPPORT_H_
+#define _DEBUG_SUPPORT_H_
+
+#include <Uefi.h>
+
+#include <Protocol/DebugSupport.h>
+#include <Protocol/LoadedImage.h>
+
+#include <Library/DebugLib.h>
+#include <Library/UefiDriverEntryPoint.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/BaseLib.h>
+
+#define NUM_IDT_ENTRIES 0x78
+#define SYSTEM_TIMER_VECTOR 0x68
+
+typedef
+VOID
+(*DEBUG_PROC) (
+ VOID
+ );
+
+typedef
+VOID
+(EFIAPI *CALLBACK_FUNC) (
+ );
+
+typedef struct {
+ IA32_IDT_GATE_DESCRIPTOR OrigDesc;
+ DEBUG_PROC OrigVector;
+ IA32_IDT_GATE_DESCRIPTOR NewDesc;
+ DEBUG_PROC StubEntry;
+ CALLBACK_FUNC RegisteredCallback;
+} IDT_ENTRY;
+
+extern UINT8 InterruptEntryStub[];
+extern UINT32 StubSize;
+extern VOID (*OrigVector) (VOID);
+extern IDT_ENTRY *IdtEntryTable;
+extern IA32_IDT_GATE_DESCRIPTOR NullDesc;
+
+/**
+ Generic IDT entry.
+
+**/
+VOID
+CommonIdtEntry (
+ VOID
+ );
+
+/**
+ Check whether FXSTOR is supported
+
+ @retval TRUE FXSTOR is supported.
+ @retval FALSE FXSTOR is not supported.
+
+**/
+BOOLEAN
+FxStorSupport (
+ VOID
+ );
+
+/**
+ Encodes an IDT descriptor with the given physical address.
+
+ @param DestDesc The IDT descriptor address.
+ @param Vecotr The interrupt vector entry.
+
+**/
+VOID
+Vect2Desc (
+ IA32_IDT_GATE_DESCRIPTOR * DestDesc,
+ VOID (*Vector) (VOID)
+ );
+
+/**
+ Initializes driver's handler registration database.
+
+ This code executes in boot services context
+ Must be public because it's referenced from DebugSupport.c
+
+ @retval EFI_UNSUPPORTED If IA32 processor does not support FXSTOR/FXRSTOR instructions,
+ the context save will fail, so these processor's are not supported.
+ @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
+ @retval EFI_SUCCESS Initializes successfully.
+
+**/
+EFI_STATUS
+PlInitializeDebugSupportDriver (
+ VOID
+ );
+
+/**
+ This is the callback that is written to the LoadedImage protocol instance
+ on the image handle. It uninstalls all registered handlers and frees all entry
+ stub memory.
+
+ @param ImageHandle The firmware allocated handle for the EFI image.
+
+ @retval EFI_SUCCESS Always.
+
+**/
+EFI_STATUS
+EFIAPI
+PlUnloadDebugSupportDriver (
+ IN EFI_HANDLE ImageHandle
+ );
+
+/**
+ Returns the maximum value that may be used for the ProcessorIndex parameter in
+ RegisterPeriodicCallback() and RegisterExceptionCallback().
+
+ Hard coded to support only 1 processor for now.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param MaxProcessorIndex Pointer to a caller-allocated UINTN in which the maximum supported
+ processor index is returned. Always 0 returned.
+
+ @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
+
+**/
+EFI_STATUS
+EFIAPI
+GetMaximumProcessorIndex (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ OUT UINTN *MaxProcessorIndex
+ );
+
+/**
+ Registers a function to be called back periodically in interrupt context.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor the callback function applies to.
+ @param PeriodicCallback A pointer to a function of type PERIODIC_CALLBACK that is the main
+ periodic entry point of the debug agent.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
+ function was previously registered.
+ @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
+ function.
+**/
+EFI_STATUS
+EFIAPI
+RegisterPeriodicCallback (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN EFI_PERIODIC_CALLBACK PeriodicCallback
+ );
+
+/**
+ Registers a function to be called when a given processor exception occurs.
+
+ This code executes in boot services context.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor the callback function applies to.
+ @param ExceptionCallback A pointer to a function of type EXCEPTION_CALLBACK that is called
+ when the processor exception specified by ExceptionType occurs.
+ @param ExceptionType Specifies which processor exception to hook.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
+ function was previously registered.
+ @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
+ function.
+**/
+EFI_STATUS
+EFIAPI
+RegisterExceptionCallback (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN EFI_EXCEPTION_CALLBACK ExceptionCallback,
+ IN EFI_EXCEPTION_TYPE ExceptionType
+ );
+
+/**
+ Invalidates processor instruction cache for a memory range. Subsequent execution in this range
+ causes a fresh memory fetch to retrieve code to be executed.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.
+ @param Start Specifies the physical base of the memory range to be invalidated.
+ @param Length Specifies the minimum number of bytes in the processor's instruction
+ cache to invalidate.
+
+ @retval EFI_SUCCESS Always returned.
+
+**/
+EFI_STATUS
+EFIAPI
+InvalidateInstructionCache (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN VOID *Start,
+ IN UINT64 Length
+ );
+
+/**
+ Allocate pool for a new IDT entry stub.
+
+ Copy the generic stub into the new buffer and fixup the vector number
+ and jump target address.
+
+ @param ExceptionType This is the exception type that the new stub will be created
+ for.
+ @param Stub On successful exit, *Stub contains the newly allocated entry stub.
+
+**/
+VOID
+CreateEntryStub (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ OUT VOID **Stub
+ );
+
+/**
+ Get Interrupt Handle from IDT Gate Descriptor.
+
+ @param IdtGateDecriptor IDT Gate Descriptor.
+
+ @return Interrupt Handle stored in IDT Gate Descriptor.
+
+**/
+UINTN
+GetInterruptHandleFromIdt (
+ IN IA32_IDT_GATE_DESCRIPTOR *IdtGateDecriptor
+ );
+
+/**
+ This is the main worker function that manages the state of the interrupt
+ handlers. It both installs and uninstalls interrupt handlers based on the
+ value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
+ If NewCallback is non-NULL, then install is indicated.
+
+ @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
+ If NULL, specifies that the previously registered handler should
+ be uninstalled.
+ @param ExceptionType Indicates which entry to manage.
+
+ @retval EFI_SUCCESS Process is ok.
+ @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
+ no handler registered for it
+ @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
+ @retval others Possible return values are passed through from UnHookEntry and HookEntry.
+
+**/
+EFI_STATUS
+ManageIdtEntryTable (
+ CALLBACK_FUNC NewCallback,
+ EFI_EXCEPTION_TYPE ExceptionType
+ );
+
+/**
+ Creates a nes entry stub. Then saves the current IDT entry and replaces it
+ with an interrupt gate for the new entry point. The IdtEntryTable is updated
+ with the new registered function.
+
+ This code executes in boot services context. The stub entry executes in interrupt
+ context.
+
+ @param ExceptionType Specifies which vector to hook.
+ @param NewCallback A pointer to the new function to be registered.
+
+**/
+VOID
+HookEntry (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ IN CALLBACK_FUNC NewCallback
+ );
+
+/**
+ Undoes HookEntry. This code executes in boot services context.
+
+ @param ExceptionType Specifies which entry to unhook
+
+**/
+VOID
+UnhookEntry (
+ IN EFI_EXCEPTION_TYPE ExceptionType
+ );
+
+#endif
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c new file mode 100644 index 000000000..afea3a218 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c @@ -0,0 +1,367 @@ +/** @file
+ IA32/x64 generic functions to support Debug Support protocol.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "DebugSupport.h"
+
+//
+// This the global main table to keep track of the interrupts
+//
+IDT_ENTRY *IdtEntryTable = NULL;
+
+/**
+ Read IDT Gate Descriptor from IDT Table.
+
+ @param Vector Specifies vector number.
+ @param IdtGateDescriptor Pointer to IDT Gate Descriptor read from IDT Table.
+
+**/
+VOID
+ReadIdtGateDescriptor (
+ IN EFI_EXCEPTION_TYPE Vector,
+ OUT IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
+ )
+{
+ IA32_DESCRIPTOR IdtrValue;
+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;
+
+ AsmReadIdtr (&IdtrValue);
+ IdtTable = (IA32_IDT_GATE_DESCRIPTOR *) IdtrValue.Base;
+
+ CopyMem ((VOID *) IdtGateDescriptor, (VOID *) &(IdtTable)[Vector], sizeof (IA32_IDT_GATE_DESCRIPTOR));
+}
+
+/**
+ Write IDT Gate Descriptor into IDT Table.
+
+ @param Vector Specifies vector number.
+ @param IdtGateDescriptor Pointer to IDT Gate Descriptor written into IDT Table.
+
+**/
+VOID
+WriteIdtGateDescriptor (
+ EFI_EXCEPTION_TYPE Vector,
+ IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
+ )
+{
+ IA32_DESCRIPTOR IdtrValue;
+ IA32_IDT_GATE_DESCRIPTOR *IdtTable;
+
+ AsmReadIdtr (&IdtrValue);
+ IdtTable = (IA32_IDT_GATE_DESCRIPTOR *) IdtrValue.Base;
+
+ CopyMem ((VOID *) &(IdtTable)[Vector], (VOID *) IdtGateDescriptor, sizeof (IA32_IDT_GATE_DESCRIPTOR));
+}
+
+/**
+ Creates a nes entry stub. Then saves the current IDT entry and replaces it
+ with an interrupt gate for the new entry point. The IdtEntryTable is updated
+ with the new registered function.
+
+ This code executes in boot services context. The stub entry executes in interrupt
+ context.
+
+ @param ExceptionType Specifies which vector to hook.
+ @param NewCallback A pointer to the new function to be registered.
+
+**/
+VOID
+HookEntry (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ IN CALLBACK_FUNC NewCallback
+ )
+{
+ BOOLEAN OldIntFlagState;
+
+ CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);
+
+ //
+ // Disables CPU interrupts and returns the previous interrupt state
+ //
+ OldIntFlagState = SaveAndDisableInterrupts ();
+
+ //
+ // gets IDT Gate descriptor by index
+ //
+ ReadIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
+ //
+ // stores orignal interrupt handle
+ //
+ IdtEntryTable[ExceptionType].OrigVector = (DEBUG_PROC) GetInterruptHandleFromIdt (&(IdtEntryTable[ExceptionType].OrigDesc));
+
+ //
+ // encodes new IDT Gate descriptor by stub entry
+ //
+ Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
+ //
+ // stores NewCallback
+ //
+ IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
+
+ //
+ // writes back new IDT Gate descriptor
+ //
+ WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
+
+ //
+ // restore interrupt state
+ //
+ SetInterruptState (OldIntFlagState);
+
+ return ;
+}
+
+/**
+ Undoes HookEntry. This code executes in boot services context.
+
+ @param ExceptionType Specifies which entry to unhook
+
+**/
+VOID
+UnhookEntry (
+ IN EFI_EXCEPTION_TYPE ExceptionType
+ )
+{
+ BOOLEAN OldIntFlagState;
+
+ //
+ // Disables CPU interrupts and returns the previous interrupt state
+ //
+ OldIntFlagState = SaveAndDisableInterrupts ();
+
+ //
+ // restore the default IDT Date Descriptor
+ //
+ WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
+
+ //
+ // restore interrupt state
+ //
+ SetInterruptState (OldIntFlagState);
+
+ return ;
+}
+
+/**
+ Returns the maximum value that may be used for the ProcessorIndex parameter in
+ RegisterPeriodicCallback() and RegisterExceptionCallback().
+
+ Hard coded to support only 1 processor for now.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param MaxProcessorIndex Pointer to a caller-allocated UINTN in which the maximum supported
+ processor index is returned. Always 0 returned.
+
+ @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
+
+**/
+EFI_STATUS
+EFIAPI
+GetMaximumProcessorIndex (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ OUT UINTN *MaxProcessorIndex
+ )
+{
+ *MaxProcessorIndex = 0;
+ return EFI_SUCCESS;
+}
+
+/**
+ Registers a function to be called back periodically in interrupt context.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor the callback function applies to.
+ @param PeriodicCallback A pointer to a function of type PERIODIC_CALLBACK that is the main
+ periodic entry point of the debug agent.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
+ function was previously registered.
+ @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
+ function.
+**/
+EFI_STATUS
+EFIAPI
+RegisterPeriodicCallback (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN EFI_PERIODIC_CALLBACK PeriodicCallback
+ )
+{
+ return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
+}
+
+/**
+ Registers a function to be called when a given processor exception occurs.
+
+ This code executes in boot services context.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor the callback function applies to.
+ @param ExceptionCallback A pointer to a function of type EXCEPTION_CALLBACK that is called
+ when the processor exception specified by ExceptionType occurs.
+ @param ExceptionType Specifies which processor exception to hook.
+
+ @retval EFI_SUCCESS The function completed successfully.
+ @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
+ function was previously registered.
+ @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
+ function.
+**/
+EFI_STATUS
+EFIAPI
+RegisterExceptionCallback (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN EFI_EXCEPTION_CALLBACK ExceptionCallback,
+ IN EFI_EXCEPTION_TYPE ExceptionType
+ )
+{
+ return ManageIdtEntryTable (ExceptionCallback, ExceptionType);
+}
+
+
+/**
+ Invalidates processor instruction cache for a memory range. Subsequent execution in this range
+ causes a fresh memory fetch to retrieve code to be executed.
+
+ @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
+ @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.
+ @param Start Specifies the physical base of the memory range to be invalidated.
+ @param Length Specifies the minimum number of bytes in the processor's instruction
+ cache to invalidate.
+
+ @retval EFI_SUCCESS Always returned.
+
+**/
+EFI_STATUS
+EFIAPI
+InvalidateInstructionCache (
+ IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
+ IN UINTN ProcessorIndex,
+ IN VOID *Start,
+ IN UINT64 Length
+ )
+{
+ AsmWbinvd ();
+ return EFI_SUCCESS;
+}
+
+/**
+ Common piece of code that invokes the registered handlers.
+
+ This code executes in exception context so no efi calls are allowed.
+ This code is called from assembly file.
+
+ @param ExceptionType Exception type
+ @param ContextRecord System context
+
+**/
+VOID
+InterruptDistrubutionHub (
+ EFI_EXCEPTION_TYPE ExceptionType,
+ EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
+ )
+{
+ if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {
+ if (ExceptionType != SYSTEM_TIMER_VECTOR) {
+ IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);
+ } else {
+ OrigVector = IdtEntryTable[ExceptionType].OrigVector;
+ IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);
+ }
+ }
+}
+
+/**
+ This is the callback that is written to the Loaded Image protocol instance
+ on the image handle. It uninstalls all registered handlers and frees all entry
+ stub memory.
+
+ @param ImageHandle The firmware allocated handle for the EFI image.
+
+ @retval EFI_SUCCESS Always.
+
+**/
+EFI_STATUS
+EFIAPI
+PlUnloadDebugSupportDriver (
+ IN EFI_HANDLE ImageHandle
+ )
+{
+ EFI_EXCEPTION_TYPE ExceptionType;
+
+ for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
+ ManageIdtEntryTable (NULL, ExceptionType);
+ //
+ // Free space for each Interrupt Stub precedure.
+ //
+ if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
+ FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
+ }
+ }
+
+ FreePool (IdtEntryTable);
+
+ return EFI_SUCCESS;
+}
+
+/**
+ Initializes driver's handler registration database.
+
+ This code executes in boot services context.
+ Must be public because it's referenced from DebugSupport.c
+
+ @retval EFI_UNSUPPORTED If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,
+ the context save will fail, so these processors are not supported.
+ @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
+ @retval EFI_SUCCESS Initializes successfully.
+
+**/
+EFI_STATUS
+PlInitializeDebugSupportDriver (
+ VOID
+ )
+{
+ EFI_EXCEPTION_TYPE ExceptionType;
+
+ //
+ // Check whether FxStor instructions are supported.
+ //
+ if (!FxStorSupport ()) {
+ return EFI_UNSUPPORTED;
+ }
+
+ IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
+ if (IdtEntryTable == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
+
+ for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType ++) {
+ IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);
+ if (IdtEntryTable[ExceptionType].StubEntry == NULL) {
+ goto ErrorCleanup;
+ }
+
+ //
+ // Copy Interrupt stub code.
+ //
+ CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);
+ }
+ return EFI_SUCCESS;
+
+ErrorCleanup:
+
+ for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
+ if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
+ FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
+ }
+ }
+ FreePool (IdtEntryTable);
+
+ return EFI_OUT_OF_RESOURCES;
+}
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.h b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.h new file mode 100644 index 000000000..ed7d98ce2 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.h @@ -0,0 +1,16 @@ +/** @file
+ IA32 specific debug support macros, typedefs and prototypes.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _PLDEBUG_SUPPORT_H_
+#define _PLDEBUG_SUPPORT_H_
+
+#include "Ia32/DebugSupport.h"
+
+#define EFI_ISA IsaIa32
+
+#endif
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupportIa32.c b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupportIa32.c new file mode 100644 index 000000000..37c330626 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupportIa32.c @@ -0,0 +1,139 @@ +/** @file
+ IA32 specific functions to support Debug Support protocol.
+
+Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "PlDebugSupport.h"
+
+IA32_IDT_GATE_DESCRIPTOR NullDesc = {{0}};
+
+/**
+ Get Interrupt Handle from IDT Gate Descriptor.
+
+ @param IdtGateDescriptor IDT Gate Descriptor.
+
+ @return Interrupt Handle stored in IDT Gate Descriptor.
+
+**/
+UINTN
+GetInterruptHandleFromIdt (
+ IN IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
+ )
+{
+ UINTN InterruptHandle;
+
+ //
+ // InterruptHandle 0-15 : OffsetLow
+ // InterruptHandle 16-31 : OffsetHigh
+ //
+ ((UINT16 *) &InterruptHandle)[0] = (UINT16) IdtGateDescriptor->Bits.OffsetLow;
+ ((UINT16 *) &InterruptHandle)[1] = (UINT16) IdtGateDescriptor->Bits.OffsetHigh;
+
+ return InterruptHandle;
+}
+
+/**
+ Allocate pool for a new IDT entry stub.
+
+ Copy the generic stub into the new buffer and fixup the vector number
+ and jump target address.
+
+ @param ExceptionType This is the exception type that the new stub will be created
+ for.
+ @param Stub On successful exit, *Stub contains the newly allocated entry stub.
+
+**/
+VOID
+CreateEntryStub (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ OUT VOID **Stub
+ )
+{
+ UINT8 *StubCopy;
+
+ StubCopy = *Stub;
+
+ //
+ // Fixup the stub code for this vector
+ //
+
+ // The stub code looks like this:
+ //
+ // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top
+ // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
+ // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
+ // 0000000D E9 db 0e9h ; jump rel32
+ // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
+ //
+
+ //
+ // poke in the exception type so the second push pushes the exception type
+ //
+ StubCopy[0x0c] = (UINT8) ExceptionType;
+
+ //
+ // fixup the jump target to point to the common entry
+ //
+ *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];
+
+ return ;
+}
+
+/**
+ This is the main worker function that manages the state of the interrupt
+ handlers. It both installs and uninstalls interrupt handlers based on the
+ value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
+ If NewCallback is non-NULL, then install is indicated.
+
+ @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
+ If NULL, specifies that the previously registered handler should
+ be uninstalled.
+ @param ExceptionType Indicates which entry to manage.
+
+ @retval EFI_SUCCESS Installing or Uninstalling operation is ok.
+ @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
+ no handler registered for it
+ @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
+
+**/
+EFI_STATUS
+ManageIdtEntryTable (
+ CALLBACK_FUNC NewCallback,
+ EFI_EXCEPTION_TYPE ExceptionType
+ )
+{
+ EFI_STATUS Status;
+
+ Status = EFI_SUCCESS;
+
+ if (CompareMem (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc, sizeof (IA32_IDT_GATE_DESCRIPTOR)) != 0) {
+ //
+ // we've already installed to this vector
+ //
+ if (NewCallback != NULL) {
+ //
+ // if the input handler is non-null, error
+ //
+ Status = EFI_ALREADY_STARTED;
+ } else {
+ UnhookEntry (ExceptionType);
+ }
+ } else {
+ //
+ // no user handler installed on this vector
+ //
+ if (NewCallback == NULL) {
+ //
+ // if the input handler is null, error
+ //
+ Status = EFI_INVALID_PARAMETER;
+ } else {
+ HookEntry (ExceptionType, NewCallback);
+ }
+ }
+
+ return Status;
+}
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/AsmFuncs.nasm b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/AsmFuncs.nasm new file mode 100644 index 000000000..9cc38a312 --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/AsmFuncs.nasm @@ -0,0 +1,581 @@ +;/** @file
+; Low level x64 routines used by the debug support driver.
+;
+; Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+; SPDX-License-Identifier: BSD-2-Clause-Patent
+;
+;**/
+
+%define EXCPT64_DIVIDE_ERROR 0
+%define EXCPT64_DEBUG 1
+%define EXCPT64_NMI 2
+%define EXCPT64_BREAKPOINT 3
+%define EXCPT64_OVERFLOW 4
+%define EXCPT64_BOUND 5
+%define EXCPT64_INVALID_OPCODE 6
+%define EXCPT64_DOUBLE_FAULT 8
+%define EXCPT64_INVALID_TSS 10
+%define EXCPT64_SEG_NOT_PRESENT 11
+%define EXCPT64_STACK_FAULT 12
+%define EXCPT64_GP_FAULT 13
+%define EXCPT64_PAGE_FAULT 14
+%define EXCPT64_FP_ERROR 16
+%define EXCPT64_ALIGNMENT_CHECK 17
+%define EXCPT64_MACHINE_CHECK 18
+%define EXCPT64_SIMD 19
+
+%define FXSTOR_FLAG 0x1000000 ; bit cpuid 24 of feature flags
+
+;; The FXSTOR and FXRSTOR commands are used for saving and restoring the x87,
+;; MMX, SSE, SSE2, etc registers. The initialization of the debugsupport driver
+;; MUST check the CPUID feature flags to see that these instructions are available
+;; and fail to init if they are not.
+
+;; fxstor [rdi]
+%macro FXSTOR_RDI 0
+ db 0xf, 0xae, 00000111y ; mod = 00, reg/op = 000, r/m = 111 = [rdi]
+%endmacro
+
+;; fxrstor [rsi]
+%macro FXRSTOR_RSI 0
+ db 0xf, 0xae, 00001110y ; mod = 00, reg/op = 001, r/m = 110 = [rsi]
+%endmacro
+
+SECTION .data
+
+global ASM_PFX(OrigVector)
+global ASM_PFX(InterruptEntryStub)
+global ASM_PFX(StubSize)
+global ASM_PFX(CommonIdtEntry)
+global ASM_PFX(FxStorSupport)
+extern ASM_PFX(InterruptDistrubutionHub)
+
+ASM_PFX(StubSize): dd InterruptEntryStubEnd - ASM_PFX(InterruptEntryStub)
+AppRsp: dq 0x1111111111111111 ; ?
+DebugRsp: dq 0x2222222222222222 ; ?
+ExtraPush: dq 0x3333333333333333 ; ?
+ExceptData: dq 0x4444444444444444 ; ?
+Rflags: dq 0x5555555555555555 ; ?
+ASM_PFX(OrigVector): dq 0x6666666666666666 ; ?
+
+;; The declarations below define the memory region that will be used for the debug stack.
+;; The context record will be built by pushing register values onto this stack.
+;; It is imparitive that alignment be carefully managed, since the FXSTOR and
+;; FXRSTOR instructions will GP fault if their memory operand is not 16 byte aligned.
+;;
+;; The stub will switch stacks from the application stack to the debuger stack
+;; and pushes the exception number.
+;;
+;; Then we building the context record on the stack. Since the stack grows down,
+;; we push the fields of the context record from the back to the front. There
+;; are 336 bytes of stack used prior allocating the 512 bytes of stack to be
+;; used as the memory buffer for the fxstor instruction. Therefore address of
+;; the buffer used for the FXSTOR instruction is &Eax - 336 - 512, which
+;; must be 16 byte aligned.
+;;
+;; We carefully locate the stack to make this happen.
+;;
+;; For reference, the context structure looks like this:
+;; struct {
+;; UINT64 ExceptionData;
+;; FX_SAVE_STATE_X64 FxSaveState; // 512 bytes, must be 16 byte aligned
+;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
+;; UINT64 RFlags;
+;; UINT64 Ldtr, Tr;
+;; UINT64 Gdtr[2], Idtr[2];
+;; UINT64 Rip;
+;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
+;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
+;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
+;; } SYSTEM_CONTEXT_X64; // 64 bit system context record
+
+align 16
+DebugStackEnd: db "DbgStkEnd >>>>>>" ;; 16 byte long string - must be 16 bytes to preserve alignment
+ times 0x1ffc dd 0x0 ;; 32K should be enough stack
+ ;; This allocation is coocked to insure
+ ;; that the the buffer for the FXSTORE instruction
+ ;; will be 16 byte aligned also.
+ ;;
+ExceptionNumber: dq 0 ;; first entry will be the vector number pushed by the stub
+
+DebugStackBegin: db "<<<< DbgStkBegin" ;; initial debug ESP == DebugStackBegin, set in stub
+
+DEFAULT REL
+SECTION .text
+
+;------------------------------------------------------------------------------
+; BOOLEAN
+; FxStorSupport (
+; void
+; )
+;
+; Abstract: Returns TRUE if FxStor instructions are supported
+;
+global ASM_PFX(FxStorSupport)
+ASM_PFX(FxStorSupport):
+
+;
+; cpuid corrupts rbx which must be preserved per the C calling convention
+;
+ push rbx
+ mov rax, dword 1
+ cpuid
+ mov eax, edx
+ and rax, FXSTOR_FLAG
+ shr rax, 24
+ pop rbx
+ ret
+
+;------------------------------------------------------------------------------
+; void
+; Vect2Desc (
+; IA32_IDT_GATE_DESCRIPTOR * DestDesc, // rcx
+; void (*Vector) (void) // rdx
+; )
+;
+; Abstract: Encodes an IDT descriptor with the given physical address
+;
+global ASM_PFX(Vect2Desc)
+ASM_PFX(Vect2Desc):
+
+ mov rax, rdx
+ mov word [rcx], ax ; write bits 15..0 of offset
+ mov dx, cs
+ mov word [rcx+2], dx ; SYS_CODE_SEL from GDT
+ mov word [rcx+4], 0xe00 | 0x8000 ; type = 386 interrupt gate, present
+ shr rax, 16
+ mov word [rcx+6], ax ; write bits 31..16 of offset
+ shr rax, 16
+ mov dword [rcx+8], eax ; write bits 63..32 of offset
+
+ ret
+
+;------------------------------------------------------------------------------
+; InterruptEntryStub
+;
+; Abstract: This code is not a function, but is a small piece of code that is
+; copied and fixed up once for each IDT entry that is hooked.
+;
+ASM_PFX(InterruptEntryStub):
+ push 0 ; push vector number - will be modified before installed
+ db 0xe9 ; jump rel32
+ dd 0 ; fixed up to relative address of CommonIdtEntry
+InterruptEntryStubEnd:
+
+;------------------------------------------------------------------------------
+; CommonIdtEntry
+;
+; Abstract: This code is not a function, but is the common part for all IDT
+; vectors.
+;
+ASM_PFX(CommonIdtEntry):
+;;
+;; At this point, the stub has saved the current application stack esp into AppRsp
+;; and switched stacks to the debug stack, where it pushed the vector number
+;;
+;; The application stack looks like this:
+;;
+;; ...
+;; (last application stack entry)
+;; [16 bytes alignment, do not care it]
+;; SS from interrupted task
+;; RSP from interrupted task
+;; rflags from interrupted task
+;; CS from interrupted task
+;; RIP from interrupted task
+;; Error code <-------------------- Only present for some exeption types
+;;
+;; Vector Number <----------------- pushed in our IDT Entry
+;;
+
+;; The stub switched us to the debug stack and pushed the interrupt number.
+;;
+;; Next, construct the context record. It will be build on the debug stack by
+;; pushing the registers in the correct order so as to create the context structure
+;; on the debug stack. The context record must be built from the end back to the
+;; beginning because the stack grows down...
+;
+;; For reference, the context record looks like this:
+;;
+;; typedef
+;; struct {
+;; UINT64 ExceptionData;
+;; FX_SAVE_STATE_X64 FxSaveState;
+;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+;; UINT64 Cr0, Cr2, Cr3, Cr4, Cr8;
+;; UINT64 RFlags;
+;; UINT64 Ldtr, Tr;
+;; UINT64 Gdtr[2], Idtr[2];
+;; UINT64 Rip;
+;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
+;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
+;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
+;; } SYSTEM_CONTEXT_X64; // 64 bit system context record
+
+;; NOTE: we save rsp here to prevent compiler put rip reference cause error AppRsp
+ push rax
+ mov rax, qword [rsp+8] ; save vector number
+ mov [ExceptionNumber], rax ; save vector number
+ pop rax
+ add rsp, 8 ; pop vector number
+ mov [AppRsp], rsp ; save stack top
+ lea rsp, [DebugStackBegin] ; switch to debugger stack
+ sub rsp, 8 ; leave space for vector number
+
+;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
+;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
+ push r15
+ push r14
+ push r13
+ push r12
+ push r11
+ push r10
+ push r9
+ push r8
+ push rax
+ push rcx
+ push rdx
+ push rbx
+ push rsp
+ push rbp
+ push rsi
+ push rdi
+
+;; Save interrupt state rflags register...
+ pushfq
+ pop rax
+ mov [Rflags], rax
+
+;; We need to determine if any extra data was pushed by the exception, and if so, save it
+;; To do this, we check the exception number pushed by the stub, and cache the
+;; result in a variable since we'll need this again.
+ cmp qword [ExceptionNumber], EXCPT64_DOUBLE_FAULT
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_INVALID_TSS
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_SEG_NOT_PRESENT
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_STACK_FAULT
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_GP_FAULT
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_PAGE_FAULT
+ jz ExtraPushOne
+ cmp qword [ExceptionNumber], EXCPT64_ALIGNMENT_CHECK
+ jz ExtraPushOne
+ mov qword [ExtraPush], 0
+ mov qword [ExceptData], 0
+ jmp ExtraPushDone
+ExtraPushOne:
+ mov qword [ExtraPush], 1
+
+;; If there's some extra data, save it also, and modify the saved AppRsp to effectively
+;; pop this value off the application's stack.
+ mov rax, [AppRsp]
+ mov rbx, [rax]
+ mov qword [ExceptData], rbx
+ add rax, 8
+ mov [AppRsp], rax
+
+ExtraPushDone:
+
+;; The "push" above pushed the debug stack rsp. Since what we're actually doing
+;; is building the context record on the debug stack, we need to save the pushed
+;; debug RSP, and replace it with the application's last stack entry...
+ mov rax, [rsp + 24]
+ mov [DebugRsp], rax
+ mov rax, [AppRsp]
+ mov rax, QWORD [rax + 24]
+ ; application stack has ss, rsp, rflags, cs, & rip, so
+ ; last actual application stack entry is saved at offset
+ ; 24 bytes from stack top.
+ mov [rsp + 24], rax
+
+;; continue building context record
+;; UINT64 Gs, Fs, Es, Ds, Cs, Ss; insure high 16 bits of each is zero
+ mov rax, ss
+ push rax
+
+ ; CS from application is one entry back in application stack
+ mov rax, [AppRsp]
+ movzx rax, word [rax + 8]
+ push rax
+
+ mov rax, ds
+ push rax
+ mov rax, es
+ push rax
+ mov rax, fs
+ push rax
+ mov rax, gs
+ push rax
+
+;; UINT64 Rip;
+ ; Rip from application is on top of application stack
+ mov rax, [AppRsp]
+ push qword [rax]
+
+;; UINT64 Gdtr[2], Idtr[2];
+ push 0
+ push 0
+ sidt [rsp]
+ push 0
+ push 0
+ sgdt [rsp]
+
+;; UINT64 Ldtr, Tr;
+ xor rax, rax
+ str ax
+ push rax
+ sldt ax
+ push rax
+
+;; UINT64 RFlags;
+;; Rflags from application is two entries back in application stack
+ mov rax, [AppRsp]
+ push qword [rax + 16]
+
+;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
+;; insure FXSAVE/FXRSTOR is enabled in CR4...
+;; ... while we're at it, make sure DE is also enabled...
+ mov rax, cr8
+ push rax
+ mov rax, cr4
+ or rax, 0x208
+ mov cr4, rax
+ push rax
+ mov rax, cr3
+ push rax
+ mov rax, cr2
+ push rax
+ push 0
+ mov rax, cr0
+ push rax
+
+;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+ mov rax, dr7
+ push rax
+;; clear Dr7 while executing debugger itself
+ xor rax, rax
+ mov dr7, rax
+
+ mov rax, dr6
+ push rax
+;; insure all status bits in dr6 are clear...
+ xor rax, rax
+ mov dr6, rax
+
+ mov rax, dr3
+ push rax
+ mov rax, dr2
+ push rax
+ mov rax, dr1
+ push rax
+ mov rax, dr0
+ push rax
+
+;; FX_SAVE_STATE_X64 FxSaveState;
+ sub rsp, 512
+ mov rdi, rsp
+ ; IMPORTANT!! The debug stack has been carefully constructed to
+ ; insure that rsp and rdi are 16 byte aligned when we get here.
+ ; They MUST be. If they are not, a GP fault will occur.
+ FXSTOR_RDI
+
+;; UEFI calling convention for x64 requires that Direction flag in EFLAGs is clear
+ cld
+
+;; UINT64 ExceptionData;
+ mov rax, [ExceptData]
+ push rax
+
+; call to C code which will in turn call registered handler
+; pass in the vector number
+ mov rdx, rsp
+ mov rcx, [ExceptionNumber]
+ sub rsp, 40
+ call ASM_PFX(InterruptDistrubutionHub)
+ add rsp, 40
+
+; restore context...
+;; UINT64 ExceptionData;
+ add rsp, 8
+
+;; FX_SAVE_STATE_X64 FxSaveState;
+ mov rsi, rsp
+ FXRSTOR_RSI
+ add rsp, 512
+
+;; UINT64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;
+ pop rax
+ mov dr0, rax
+ pop rax
+ mov dr1, rax
+ pop rax
+ mov dr2, rax
+ pop rax
+ mov dr3, rax
+;; skip restore of dr6. We cleared dr6 during the context save.
+ add rsp, 8
+ pop rax
+ mov dr7, rax
+
+;; UINT64 Cr0, Cr1, Cr2, Cr3, Cr4, Cr8;
+ pop rax
+ mov cr0, rax
+ add rsp, 8
+ pop rax
+ mov cr2, rax
+ pop rax
+ mov cr3, rax
+ pop rax
+ mov cr4, rax
+ pop rax
+ mov cr8, rax
+
+;; UINT64 RFlags;
+ mov rax, [AppRsp]
+ pop qword [rax + 16]
+
+;; UINT64 Ldtr, Tr;
+;; UINT64 Gdtr[2], Idtr[2];
+;; Best not let anyone mess with these particular registers...
+ add rsp, 48
+
+;; UINT64 Rip;
+ pop qword [rax]
+
+;; UINT64 Gs, Fs, Es, Ds, Cs, Ss;
+;; NOTE - modified segment registers could hang the debugger... We
+;; could attempt to insulate ourselves against this possibility,
+;; but that poses risks as well.
+;;
+
+ pop rax
+ ; mov gs, rax
+ pop rax
+ ; mov fs, rax
+ pop rax
+ mov es, rax
+ pop rax
+ mov ds, rax
+ mov rax, [AppRsp]
+ pop qword [rax + 8]
+ pop rax
+ mov ss, rax
+
+;; The next stuff to restore is the general purpose registers that were pushed
+;; using the "push" instruction.
+;;
+;; The value of RSP as stored in the context record is the application RSP
+;; including the 5 entries on the application stack caused by the exception
+;; itself. It may have been modified by the debug agent, so we need to
+;; determine if we need to relocate the application stack.
+
+ mov rbx, [rsp + 24] ; move the potentially modified AppRsp into rbx
+ mov rax, [AppRsp]
+ mov rax, QWORD [rax + 24]
+ cmp rbx, rax
+ je NoAppStackMove
+
+ mov rax, [AppRsp]
+ mov rcx, [rax] ; RIP
+ mov [rbx], rcx
+
+ mov rcx, [rax + 8] ; CS
+ mov [rbx + 8], rcx
+
+ mov rcx, [rax + 16] ; RFLAGS
+ mov [rbx + 16], rcx
+
+ mov rcx, [rax + 24] ; RSP
+ mov [rbx + 24], rcx
+
+ mov rcx, [rax + 32] ; SS
+ mov [rbx + 32], rcx
+
+ mov rax, rbx ; modify the saved AppRsp to the new AppRsp
+ mov [AppRsp], rax
+NoAppStackMove:
+ mov rax, [DebugRsp] ; restore the DebugRsp on the debug stack
+ ; so our "pop" will not cause a stack switch
+ mov [rsp + 24], rax
+
+ cmp qword [ExceptionNumber], 0x68
+ jne NoChain
+
+Chain:
+
+;; Restore rflags so when we chain, the flags will be exactly as if we were never here.
+;; We gin up the stack to do an iretq so we can get ALL the flags.
+ mov rax, [AppRsp]
+ mov rbx, [rax + 40]
+ push rbx
+ mov rax, ss
+ push rax
+ mov rax, rsp
+ add rax, 16
+ push rax
+ mov rax, [AppRsp]
+ mov rbx, [rax + 16]
+ and rbx, ~ 0x300 ; special handling for IF and TF
+ push rbx
+ mov rax, cs
+ push rax
+ lea rax, [PhonyIretq]
+ push rax
+ iretq
+PhonyIretq:
+
+;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
+;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
+ pop rdi
+ pop rsi
+ pop rbp
+ pop rsp
+ pop rbx
+ pop rdx
+ pop rcx
+ pop rax
+ pop r8
+ pop r9
+ pop r10
+ pop r11
+ pop r12
+ pop r13
+ pop r14
+ pop r15
+
+;; Switch back to application stack
+ mov rsp, [AppRsp]
+
+;; Jump to original handler
+ jmp [ASM_PFX(OrigVector)]
+
+NoChain:
+;; UINT64 Rdi, Rsi, Rbp, Rsp, Rbx, Rdx, Rcx, Rax;
+;; UINT64 R8, R9, R10, R11, R12, R13, R14, R15;
+ pop rdi
+ pop rsi
+ pop rbp
+ pop rsp
+ pop rbx
+ pop rdx
+ pop rcx
+ pop rax
+ pop r8
+ pop r9
+ pop r10
+ pop r11
+ pop r12
+ pop r13
+ pop r14
+ pop r15
+
+;; Switch back to application stack
+ mov rsp, [AppRsp]
+
+;; We're outa here...
+ iretq
+
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupport.h b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupport.h new file mode 100644 index 000000000..b3743fcdb --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupport.h @@ -0,0 +1,16 @@ +/** @file
+ X64 specific debug support macros.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _PLDEBUG_SUPPORT_H_
+#define _PLDEBUG_SUPPORT_H_
+
+#include "Ia32/DebugSupport.h"
+
+#define EFI_ISA IsaX64
+
+#endif
diff --git a/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupportX64.c b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupportX64.c new file mode 100644 index 000000000..1e40873bc --- /dev/null +++ b/roms/edk2/MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupportX64.c @@ -0,0 +1,140 @@ +/** @file
+ X64 specific functions to support Debug Support protocol.
+
+Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "PlDebugSupport.h"
+
+IA32_IDT_GATE_DESCRIPTOR NullDesc = {{0,0}};
+
+/**
+ Get Interrupt Handle from IDT Gate Descriptor.
+
+ @param IdtGateDecriptor IDT Gate Descriptor.
+
+ @return Interrupt Handle stored in IDT Gate Descriptor.
+
+**/
+UINTN
+GetInterruptHandleFromIdt (
+ IN IA32_IDT_GATE_DESCRIPTOR *IdtGateDecriptor
+ )
+{
+ UINTN InterruptHandle;
+
+ //
+ // InterruptHandle 0-15 : OffsetLow
+ // InterruptHandle 16-31 : OffsetHigh
+ // InterruptHandle 32-63 : OffsetUpper
+ //
+ InterruptHandle = ((UINTN) IdtGateDecriptor->Bits.OffsetLow) |
+ (((UINTN) IdtGateDecriptor->Bits.OffsetHigh) << 16) |
+ (((UINTN) IdtGateDecriptor->Bits.OffsetUpper) << 32) ;
+
+ return InterruptHandle;
+}
+
+/**
+ Allocate pool for a new IDT entry stub.
+
+ Copy the generic stub into the new buffer and fixup the vector number
+ and jump target address.
+
+ @param ExceptionType This is the exception type that the new stub will be created
+ for.
+ @param Stub On successful exit, *Stub contains the newly allocated entry stub.
+
+**/
+VOID
+CreateEntryStub (
+ IN EFI_EXCEPTION_TYPE ExceptionType,
+ OUT VOID **Stub
+ )
+{
+ UINT8 *StubCopy;
+
+ StubCopy = *Stub;
+
+ //
+ // Fixup the stub code for this vector
+ //
+
+ // The stub code looks like this:
+ //
+ // 00000000 6A 00 push 0 ; push vector number - will be modified before installed
+ // 00000002 E9 db 0e9h ; jump rel32
+ // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
+ //
+
+ //
+ // poke in the exception type so the second push pushes the exception type
+ //
+ StubCopy[0x1] = (UINT8) ExceptionType;
+
+ //
+ // fixup the jump target to point to the common entry
+ //
+ *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);
+
+ return;
+}
+
+/**
+ This is the main worker function that manages the state of the interrupt
+ handlers. It both installs and uninstalls interrupt handlers based on the
+ value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
+ If NewCallback is non-NULL, then install is indicated.
+
+ @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
+ If NULL, specifies that the previously registered handler should
+ be uninstalled.
+ @param ExceptionType Indicates which entry to manage.
+
+ @retval EFI_SUCCESS Process is ok.
+ @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
+ no handler registered for it
+ @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
+ @retval others Possible return values are passed through from UnHookEntry and HookEntry.
+
+**/
+EFI_STATUS
+ManageIdtEntryTable (
+ CALLBACK_FUNC NewCallback,
+ EFI_EXCEPTION_TYPE ExceptionType
+ )
+{
+ EFI_STATUS Status;
+
+ Status = EFI_SUCCESS;
+
+ if (CompareMem (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc, sizeof (IA32_IDT_GATE_DESCRIPTOR)) != 0) {
+ //
+ // we've already installed to this vector
+ //
+ if (NewCallback != NULL) {
+ //
+ // if the input handler is non-null, error
+ //
+ Status = EFI_ALREADY_STARTED;
+ } else {
+ UnhookEntry (ExceptionType);
+ }
+ } else {
+ //
+ // no user handler installed on this vector
+ //
+ if (NewCallback == NULL) {
+ //
+ // if the input handler is null, error
+ //
+ Status = EFI_INVALID_PARAMETER;
+ } else {
+ HookEntry (ExceptionType, NewCallback);
+ }
+ }
+
+ return Status;
+}
|