]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkModulePkg/Universal/DebugSupport/Dxe/x64/plDebugSupport.c
Add X64 support for DebugSupport driver.
[mirror_edk2.git] / EdkModulePkg / Universal / DebugSupport / Dxe / x64 / plDebugSupport.c
diff --git a/EdkModulePkg/Universal/DebugSupport/Dxe/x64/plDebugSupport.c b/EdkModulePkg/Universal/DebugSupport/Dxe/x64/plDebugSupport.c
new file mode 100644 (file)
index 0000000..b3580a4
--- /dev/null
@@ -0,0 +1,437 @@
+/**@file\r
+  X64 specific debug support functions\r
+\r
+Copyright (c) 2006 Intel Corporation                                                         \r
+All rights reserved. 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
+//\r
+// private header files\r
+//\r
+#include "plDebugSupport.h"\r
+\r
+//\r
+// This the global main table to keep track of the interrupts\r
+//\r
+IDT_ENTRY   *IdtEntryTable  = NULL;\r
+DESCRIPTOR  NullDesc        = {0, 0};\r
+\r
+STATIC\r
+EFI_STATUS\r
+CreateEntryStub (\r
+  IN EFI_EXCEPTION_TYPE     ExceptionType,\r
+  OUT VOID                  **Stub\r
+  )\r
+/*++\r
+\r
+Routine Description: Allocate pool for a new IDT entry stub.  Copy the generic\r
+    stub into the new buffer and fixup the vector number and jump target address.\r
+\r
+Arguments:\r
+    ExceptionType - This is the exception type that the new stub will be created\r
+                    for.\r
+    Stub - On successful exit, *Stub contains the newly allocated entry stub.\r
+Returns:\r
+  Typically EFI_SUCCESS\r
+  other possibilities are passed through from AllocatePool\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       *StubCopy;\r
+\r
+  //\r
+  // First, allocate a new buffer and copy the stub code into it\r
+  //\r
+  Status = gBS->AllocatePool (EfiBootServicesData, StubSize, Stub);\r
+  if (Status == EFI_SUCCESS) {\r
+    StubCopy = *Stub;\r
+    gBS->CopyMem (StubCopy, InterruptEntryStub, StubSize);\r
+\r
+    //\r
+    // Next fixup the stub code for this vector\r
+    //\r
+\r
+    // The stub code looks like this:\r
+    //\r
+    //    00000000  6A 00               push    0                       ; push vector number - will be modified before installed\r
+    //    00000002  E9                  db      0e9h                    ; jump rel32\r
+    //    00000003  00000000            dd      0                       ; fixed up to relative address of CommonIdtEntry\r
+    //\r
+\r
+    //\r
+    // poke in the exception type so the second push pushes the exception type\r
+    //\r
+    StubCopy[0x1] = (UINT8) ExceptionType;\r
+\r
+    //\r
+    // fixup the jump target to point to the common entry\r
+    //\r
+    *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+HookEntry (\r
+  IN EFI_EXCEPTION_TYPE            ExceptionType,\r
+  IN VOID                         (*NewCallback) ()\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Creates a nes entry stub.  Then saves the current IDT entry and replaces it\r
+  with an interrupt gate for the new entry point.  The IdtEntryTable is updated\r
+  with the new registered function.\r
+\r
+  This code executes in boot services context.  The stub entry executes in interrupt\r
+  context.\r
+\r
+Arguments:\r
+  ExceptionType - specifies which vector to hook.\r
+  NewCallback - a pointer to the new function to be registered.\r
+\r
+Returns:\r
+  EFI_SUCCESS\r
+  Other possibilities are passed through by CreateEntryStub\r
+\r
+--*/\r
+{\r
+  BOOLEAN     OldIntFlagState;\r
+  EFI_STATUS  Status;\r
+\r
+  Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);\r
+  if (Status == EFI_SUCCESS) {\r
+    OldIntFlagState = WriteInterruptFlag (0);\r
+    ReadIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
+\r
+    ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0]  = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[0];\r
+    ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1]  = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[3];\r
+    ((UINT32 *) &IdtEntryTable[ExceptionType].OrigVector)[1]  = ((UINT32 *) &IdtEntryTable[ExceptionType].OrigDesc.High)[0];\r
+\r
+    Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r
+    IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r
+    WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r
+    WriteInterruptFlag (OldIntFlagState);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+UnhookEntry (\r
+  IN EFI_EXCEPTION_TYPE           ExceptionType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Undoes HookEntry. This code executes in boot services context.\r
+\r
+Arguments:\r
+  ExceptionType - specifies which entry to unhook\r
+\r
+Returns:\r
+  EFI_SUCCESS\r
+  Other values are passed through from FreePool\r
+\r
+--*/\r
+{\r
+  BOOLEAN     OldIntFlagState;\r
+  EFI_STATUS  Status;\r
+\r
+  OldIntFlagState = WriteInterruptFlag (0);\r
+  WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
+  Status = gBS->FreePool ((VOID *) (UINTN) IdtEntryTable[ExceptionType].StubEntry);\r
+  ZeroMem (&IdtEntryTable[ExceptionType], sizeof (IDT_ENTRY));\r
+  WriteInterruptFlag (OldIntFlagState);\r
+\r
+  return (Status);\r
+}\r
+\r
+EFI_STATUS\r
+ManageIdtEntryTable (\r
+  VOID (*NewCallback)(),\r
+  EFI_EXCEPTION_TYPE ExceptionType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  This is the main worker function that manages the state of the interrupt\r
+  handlers.  It both installs and uninstalls interrupt handlers based on the\r
+  value of NewCallback.  If NewCallback is NULL, then uninstall is indicated.\r
+  If NewCallback is non-NULL, then install is indicated.\r
+\r
+Arguments:\r
+  NewCallback - If non-NULL, NewCallback specifies the new handler to register.\r
+                If NULL, specifies that the previously registered handler should\r
+                    be uninstalled.\r
+  ExceptionType - Indicates which entry to manage\r
+\r
+Returns:\r
+  EFI_SUCCESS\r
+  EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
+                          no handler registered for it\r
+  EFI_ALREADY_STARTED   - requested install to a vector that already has a handler registered.\r
+\r
+  Other possible return values are passed through from UnHookEntry and HookEntry.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  if (CompareDescriptor (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {\r
+    //\r
+    // we've already installed to this vector\r
+    //\r
+    if (NewCallback != NULL) {\r
+      //\r
+      // if the input handler is non-null, error\r
+      //\r
+      Status = EFI_ALREADY_STARTED;\r
+    } else {\r
+      Status = UnhookEntry (ExceptionType);\r
+    }\r
+  } else {\r
+    //\r
+    // no user handler installed on this vector\r
+    //\r
+    if (NewCallback == NULL) {\r
+      //\r
+      // if the input handler is null, error\r
+      //\r
+      Status = EFI_INVALID_PARAMETER;\r
+    } else {\r
+      Status = HookEntry (ExceptionType, NewCallback);\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+GetMaximumProcessorIndex (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL       *This,\r
+  OUT UINTN                           *MaxProcessorIndex\r
+  )\r
+/*++\r
+\r
+Routine Description: This is a DebugSupport protocol member function.\r
+\r
+Arguments:\r
+  This              - The DebugSupport instance\r
+  MaxProcessorIndex - The maximuim supported processor index\r
+\r
+Returns:\r
+  Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0\r
+\r
+--*/\r
+{\r
+  *MaxProcessorIndex = 0;\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+RegisterPeriodicCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
+  IN UINTN                      ProcessorIndex,\r
+  IN EFI_PERIODIC_CALLBACK      PeriodicCallback\r
+  )\r
+/*++\r
+\r
+Routine Description: This is a DebugSupport protocol member function.\r
+\r
+Arguments:\r
+  This             - The DebugSupport instance\r
+  ProcessorIndex   - Which processor the callback applies to.\r
+  PeriodicCallback - Callback function\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS\r
+  EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
+                          no handler registered for it\r
+  EFI_ALREADY_STARTED   - requested install to a vector that already has a handler registered.\r
+\r
+  Other possible return values are passed through from UnHookEntry and HookEntry.\r
+\r
+--*/\r
+{\r
+  return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+RegisterExceptionCallback (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r
+  IN UINTN                      ProcessorIndex,\r
+  IN EFI_EXCEPTION_CALLBACK     NewCallback,\r
+  IN EFI_EXCEPTION_TYPE         ExceptionType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  This is a DebugSupport protocol member function.\r
+\r
+  This code executes in boot services context.\r
+\r
+Arguments:\r
+  This             - The DebugSupport instance\r
+  ProcessorIndex   - Which processor the callback applies to.\r
+  NewCallback      - Callback function\r
+  ExceptionType    - Which exception to hook\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS\r
+  EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has\r
+                          no handler registered for it\r
+  EFI_ALREADY_STARTED   - requested install to a vector that already has a handler registered.\r
+\r
+  Other possible return values are passed through from UnHookEntry and HookEntry.\r
+\r
+--*/\r
+{\r
+  return ManageIdtEntryTable (NewCallback, ExceptionType);\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InvalidateInstructionCache (\r
+  IN EFI_DEBUG_SUPPORT_PROTOCOL       *This,\r
+  IN UINTN                            ProcessorIndex,\r
+  IN VOID                             *Start,\r
+  IN UINT64                           Length\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  This is a DebugSupport protocol member function.\r
+  Calls assembly routine to flush cache.\r
+\r
+Arguments:\r
+  This             - The DebugSupport instance\r
+  ProcessorIndex   - Which processor the callback applies to.\r
+  Start            - Physical base of the memory range to be invalidated\r
+  Length           - mininum number of bytes in instruction cache to invalidate\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS - always return success\r
+\r
+--*/\r
+{\r
+  AsmWbinvd ();\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+plInitializeDebugSupportDriver (\r
+  VOID\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Initializes driver's handler registration database.\r
+\r
+  This code executes in boot services context.\r
+\r
+Arguments:\r
+  None\r
+\r
+Returns:\r
+  EFI_SUCCESS\r
+  EFI_UNSUPPORTED - if X64 processor does not support FXSTOR/FXRSTOR instructions,\r
+                    the context save will fail, so these processor's are not supported.\r
+  EFI_OUT_OF_RESOURCES - not resource to finish initialization\r
+\r
+--*/\r
+{\r
+  if (!FxStorSupport ()) {\r
+    return EFI_UNSUPPORTED;\r
+  } else {\r
+    IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);\r
+    if (IdtEntryTable != NULL) {\r
+      return EFI_SUCCESS;\r
+    } else {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+  }\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+plUnloadDebugSupportDriver (\r
+  IN EFI_HANDLE ImageHandle\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  This is the callback that is written to the LoadedImage protocol instance\r
+  on the image handle. It uninstalls all registered handlers and frees all entry\r
+  stub memory.\r
+\r
+  This code executes in boot services context.\r
+\r
+Arguments:\r
+  ImageHandle - The image handle of the unload handler\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS - always return success\r
+\r
+--*/\r
+{\r
+  EFI_EXCEPTION_TYPE  ExceptionType;\r
+\r
+  for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
+    ManageIdtEntryTable (NULL, ExceptionType);\r
+  }\r
+\r
+  gBS->FreePool (IdtEntryTable);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+InterruptDistrubutionHub (\r
+  EFI_EXCEPTION_TYPE      ExceptionType,\r
+  EFI_SYSTEM_CONTEXT_IA32 *ContextRecord\r
+  )\r
+/*++\r
+\r
+Routine Description: Common piece of code that invokes the registered handlers.\r
+\r
+  This code executes in exception context so no efi calls are allowed.\r
+\r
+Arguments:\r
+  ExceptionType - exception type\r
+  ContextRecord - system context\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r
+    if (ExceptionType != SYSTEM_TIMER_VECTOR) {\r
+      IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);\r
+    } else {\r
+      OrigVector = IdtEntryTable[ExceptionType].OrigVector;\r
+      IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);\r
+    }\r
+  }\r
+}\r