]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DebugSupportDxe/ipf/plDebugSupport.c
Add DebugPort & DebugSupport drivers
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / ipf / plDebugSupport.c
diff --git a/MdeModulePkg/Universal/DebugSupportDxe/ipf/plDebugSupport.c b/MdeModulePkg/Universal/DebugSupportDxe/ipf/plDebugSupport.c
new file mode 100644 (file)
index 0000000..33233d6
--- /dev/null
@@ -0,0 +1,586 @@
+/**@file\r
+  IPF 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
+typedef struct {\r
+  UINT64  low;\r
+  UINT64  high;\r
+} BUNDLE;\r
+\r
+//\r
+// number of bundles to swap in ivt\r
+//\r
+#define NUM_BUNDLES_IN_STUB 5\r
+#define NUM_IVT_ENTRIES     64\r
+\r
+typedef struct {\r
+  BUNDLE  OrigBundles[NUM_BUNDLES_IN_STUB];\r
+  VOID (*RegisteredCallback) ();\r
+} IVT_ENTRY;\r
+\r
+STATIC\r
+EFI_STATUS\r
+ManageIvtEntryTable (\r
+  IN  EFI_EXCEPTION_TYPE                ExceptionType,\r
+  IN  BUNDLE                NewBundles[4],\r
+  IN  VOID                  (*NewCallback) ()\r
+  );\r
+\r
+STATIC\r
+VOID\r
+HookEntry (\r
+  IN  EFI_EXCEPTION_TYPE                ExceptionType,\r
+  IN  BUNDLE                NewBundles[4],\r
+  IN  VOID                  (*NewCallback) ()\r
+  );\r
+\r
+STATIC\r
+VOID\r
+UnhookEntry (\r
+  IN  EFI_EXCEPTION_TYPE    ExceptionType\r
+  );\r
+\r
+STATIC\r
+VOID\r
+ChainExternalInterrupt (\r
+  IN  VOID                  (*NewCallback) ()\r
+  );\r
+\r
+STATIC\r
+VOID\r
+UnchainExternalInterrupt (\r
+  VOID\r
+  );\r
+\r
+STATIC\r
+VOID\r
+GetHandlerEntryPoint (\r
+  UINTN                     HandlerIndex,\r
+  VOID                      **EntryPoint\r
+  );\r
+\r
+IVT_ENTRY IvtEntryTable[NUM_IVT_ENTRIES];\r
+\r
+//\r
+// IPF context record is overallocated by 512 bytes to guarantee a 512 byte alignment exists\r
+// within the buffer and still have a large enough buffer to hold a whole IPF context record.\r
+//\r
+UINT8     IpfContextBuf[sizeof (EFI_SYSTEM_CONTEXT_IPF) + 512];\r
+\r
+//\r
+// The PatchSaveBuffer is used to store the original bundles from the IVT where it is patched\r
+// with the common handler.\r
+//\r
+UINT8     PatchSaveBuffer[0x400];\r
+UINTN     ExternalInterruptCount;\r
+\r
+EFI_STATUS\r
+plInitializeDebugSupportDriver (\r
+  VOID\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  IPF specific DebugSupport driver initialization.  Must be public because it's\r
+  referenced from DebugSupport.c\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  SetMem (IvtEntryTable, sizeof (IvtEntryTable), 0);\r
+  ExternalInterruptCount = 0;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+plUnloadDebugSupportDriver (\r
+  IN EFI_HANDLE       ImageHandle\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Unload handler that is called during UnloadImage() - deallocates pool memory\r
+  used by the driver.  Must be public because it's referenced from DebugSuport.c\r
+\r
+Arguments:\r
+  ImageHandle - Image handle\r
+\r
+Returns:\r
+\r
+  EFI_STATUS - anything other than EFI_SUCCESS indicates the callback was not registered.\r
+\r
+--*/\r
+{\r
+  EFI_EXCEPTION_TYPE  ExceptionType;\r
+\r
+  for (ExceptionType = 0; ExceptionType < NUM_IVT_ENTRIES; ExceptionType++) {\r
+    ManageIvtEntryTable (ExceptionType, NULL, NULL);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+VOID\r
+CommonHandler (\r
+  IN EFI_EXCEPTION_TYPE ExceptionType,\r
+  IN EFI_SYSTEM_CONTEXT Context\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  C routine that is called for all registered exceptions.  This is the main\r
+  exception dispatcher.  Must be public because it's referenced from AsmFuncs.s.\r
+\r
+Arguments:\r
+  ExceptionType - Exception Type\r
+  Context       - System Context\r
+\r
+Returns:\r
+\r
+  Nothing\r
+  \r
+--*/\r
+{\r
+  static BOOLEAN  InHandler = FALSE;\r
+\r
+  DEBUG_CODE_BEGIN ();\r
+    if (InHandler) {\r
+      DEBUG ((EFI_D_INFO, "ERROR: Re-entered debugger!\n"\r
+                                    "       ExceptionType == %X\n"\r
+                                    "       Context       == %X\n"\r
+                                    "       Context.SystemContextIpf->CrIip  == %X\n"\r
+                                    "       Context.SystemContextIpf->CrIpsr == %X\n"\r
+                                    "       InHandler     == %X\n",\r
+                                    ExceptionType, \r
+                                    Context, \r
+                                    Context.SystemContextIpf->CrIip,\r
+                                    Context.SystemContextIpf->CrIpsr,\r
+                                    InHandler));\r
+    }\r
+  DEBUG_CODE_END ();\r
+\r
+  ASSERT (!InHandler);\r
+  InHandler = TRUE;\r
+  if (IvtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r
+    if (ExceptionType != EXCEPT_IPF_EXTERNAL_INTERRUPT) {\r
+      IvtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, Context.SystemContextIpf);\r
+    } else {\r
+      IvtEntryTable[ExceptionType].RegisteredCallback (Context.SystemContextIpf);\r
+    }\r
+  } else {\r
+    ASSERT (0);\r
+  }\r
+\r
+  InHandler = FALSE;\r
+}\r
+\r
+STATIC\r
+VOID\r
+GetHandlerEntryPoint (\r
+  UINTN   HandlerIndex,\r
+  VOID    **EntryPoint\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Given an integer number, return the physical address of the entry point in the IFT\r
+  \r
+Arguments:\r
+  HandlerIndex - Index of the Handler \r
+  EntryPoint   - IFT Entrypoint\r
+\r
+Returns:\r
+\r
+  Nothing\r
+  \r
+--*/\r
+{\r
+  UINT8 *TempPtr;\r
+\r
+  //\r
+  // get base address of IVT\r
+  //\r
+  TempPtr = GetIva ();\r
+\r
+  if (HandlerIndex < 20) {\r
+    //\r
+    // first 20 provide 64 bundles per vector\r
+    //\r
+    TempPtr += 0x400 * HandlerIndex;\r
+  } else {\r
+    //\r
+    // the rest provide 16 bundles per vector\r
+    //\r
+    TempPtr += 0x5000 + 0x100 * (HandlerIndex - 20);\r
+  }\r
+\r
+  *EntryPoint = (VOID *) TempPtr;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+ManageIvtEntryTable (\r
+  IN  EFI_EXCEPTION_TYPE                                         ExceptionType,\r
+  IN  BUNDLE                       NewBundles[NUM_BUNDLES_IN_STUB],\r
+  IN  VOID                         (*NewCallback) ()\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  This is the worker function that installs and removes all handlers\r
+  \r
+Arguments:\r
+  ExceptionType  - Exception Type\r
+  NewBundles     - New Boundles\r
+  NewCallback    - New Callback\r
+\r
+Returns:\r
+\r
+  EFI_STATUS - any return other than EFI_SUCCESS indicates the request was not\r
+  satisfied.\r
+  EFI_ALEADY_STARTED - Ivt already hooked.\r
+  \r
+--*/\r
+{\r
+  BUNDLE  *B0Ptr;\r
+  UINT64  InterruptFlags;\r
+  EFI_TPL OldTpl;\r
+\r
+  //\r
+  // Get address of bundle 0\r
+  //\r
+  GetHandlerEntryPoint (ExceptionType, (VOID **) &B0Ptr);\r
+\r
+  if (IvtEntryTable[ExceptionType].RegisteredCallback != NULL) {\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
+      return EFI_ALREADY_STARTED;\r
+    } else {\r
+      //\r
+      // else remove the previously installed handler\r
+      //\r
+      OldTpl          = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
+      InterruptFlags  = ProgramInterruptFlags (DISABLE_INTERRUPTS);\r
+      if (ExceptionType == EXCEPT_IPF_EXTERNAL_INTERRUPT) {\r
+        UnchainExternalInterrupt ();\r
+      } else {\r
+        UnhookEntry (ExceptionType);\r
+      }\r
+\r
+      ProgramInterruptFlags (InterruptFlags);\r
+      gBS->RestoreTPL (OldTpl);\r
+      //\r
+      // re-init IvtEntryTable\r
+      //\r
+      SetMem (&IvtEntryTable[ExceptionType], sizeof (IVT_ENTRY), 0);\r
+    }\r
+  } else {\r
+    //\r
+    // no user handler installed on this vector\r
+    //\r
+    if (NewCallback != NULL) {\r
+      OldTpl          = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
+      InterruptFlags  = ProgramInterruptFlags (DISABLE_INTERRUPTS);\r
+      if (ExceptionType == EXCEPT_IPF_EXTERNAL_INTERRUPT) {\r
+        ChainExternalInterrupt (NewCallback);\r
+      } else {\r
+        HookEntry (ExceptionType, NewBundles, NewCallback);\r
+      }\r
+\r
+      ProgramInterruptFlags (InterruptFlags);\r
+      gBS->RestoreTPL (OldTpl);\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+VOID\r
+HookEntry (\r
+  IN  EFI_EXCEPTION_TYPE  ExceptionType,\r
+  IN  BUNDLE              NewBundles[4],\r
+  IN  VOID                (*NewCallback) ()\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Saves original IVT contents and inserts a few new bundles which are fixed up\r
+  to store the ExceptionType and then call the common handler.\r
+  \r
+Arguments:\r
+  ExceptionType  - Exception Type\r
+  NewBundles     - New Boundles\r
+  NewCallback    - New Callback\r
+\r
+Returns:\r
+\r
+  Nothing\r
+    \r
+--*/\r
+{\r
+  BUNDLE  *FixupBundle;\r
+  BUNDLE  *B0Ptr;\r
+\r
+  //\r
+  // Get address of bundle 0\r
+  //\r
+  GetHandlerEntryPoint (ExceptionType, (VOID **) &B0Ptr);\r
+\r
+  //\r
+  // copy original bundles from IVT to IvtEntryTable so we can restore them later\r
+  //\r
+  CopyMem (\r
+    IvtEntryTable[ExceptionType].OrigBundles,\r
+    B0Ptr,\r
+    sizeof (BUNDLE) * NUM_BUNDLES_IN_STUB\r
+    );\r
+  //\r
+  // insert new B0\r
+  //\r
+  CopyMem (B0Ptr, NewBundles, sizeof (BUNDLE) * NUM_BUNDLES_IN_STUB);\r
+\r
+  //\r
+  // fixup IVT entry so it stores its index and whether or not to chain...\r
+  //\r
+  FixupBundle = B0Ptr + 2;\r
+  FixupBundle->high |= ExceptionType << 36;\r
+\r
+  InstructionCacheFlush (B0Ptr, 5);\r
+  IvtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r
+}\r
+\r
+STATIC\r
+VOID\r
+UnhookEntry (\r
+  IN  EFI_EXCEPTION_TYPE  ExceptionType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Restores original IVT contents when unregistering a callback function\r
+  \r
+Arguments:\r
+  ExceptionType  - Exception Type\r
+  NewBundles     - New Boundles\r
+  NewCallback    - New Callback\r
+\r
+Returns:\r
+\r
+  Nothing\r
+    \r
+--*/\r
+{\r
+  BUNDLE  *B0Ptr;\r
+\r
+  //\r
+  // Get address of bundle 0\r
+  //\r
+  GetHandlerEntryPoint (ExceptionType, (VOID **) &B0Ptr);\r
+  //\r
+  // restore original bundles in IVT\r
+  //\r
+  CopyMem (\r
+    B0Ptr,\r
+    IvtEntryTable[ExceptionType].OrigBundles,\r
+    sizeof (BUNDLE) * NUM_BUNDLES_IN_STUB\r
+    );\r
+  InstructionCacheFlush (B0Ptr, 5);\r
+}\r
+\r
+STATIC\r
+VOID\r
+ChainExternalInterrupt (\r
+  IN  VOID  (*NewCallback) ()\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Sets up cache flush and calls assembly function to chain external interrupt.\r
+  Records new callback in IvtEntryTable.\r
+  \r
+Arguments:\r
+  NewCallback    - New Callback\r
+\r
+Returns:\r
+\r
+  Nothing\r
+    \r
+--*/\r
+{\r
+  VOID  *Start;\r
+\r
+  Start = (VOID *) ((UINT8 *) GetIva () + 0x400 * EXCEPT_IPF_EXTERNAL_INTERRUPT + 0x400);\r
+  IvtEntryTable[EXCEPT_IPF_EXTERNAL_INTERRUPT].RegisteredCallback = NewCallback;\r
+  ChainHandler ();\r
+  InstructionCacheFlush (Start, 0x400);\r
+}\r
+\r
+STATIC\r
+VOID\r
+UnchainExternalInterrupt (\r
+  VOID\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  Sets up cache flush and calls assembly function to restore external interrupt.\r
+  Removes registered callback from IvtEntryTable.\r
+  \r
+Arguments:\r
+  Nothing\r
+  \r
+Returns:\r
+\r
+  Nothing\r
+    \r
+--*/\r
+{\r
+  VOID  *Start;\r
+\r
+  Start = (VOID *) ((UINT8 *) GetIva () + 0x400 * EXCEPT_IPF_EXTERNAL_INTERRUPT + 0x400);\r
+  UnchainHandler ();\r
+  InstructionCacheFlush (Start, 0x400);\r
+  IvtEntryTable[EXCEPT_IPF_EXTERNAL_INTERRUPT].RegisteredCallback = NULL;\r
+}\r
+\r
+//\r
+// The rest of the functions in this file are all member functions for the\r
+// DebugSupport protocol\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.  Hard\r
+  coded to support only 1 processor for now.\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          NewPeriodicCallback\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  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_STATUS - anything other than EFI_SUCCESS indicates the callback was not registered.\r
+\r
+--*/\r
+{\r
+  return ManageIvtEntryTable (EXCEPT_IPF_EXTERNAL_INTERRUPT, NULL, NewPeriodicCallback);\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
+  DebugSupport protocol member function\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_STATUS - anything other than EFI_SUCCESS indicates the callback was not registered.\r
+\r
+--*/\r
+{\r
+  return ManageIvtEntryTable (\r
+          ExceptionType,\r
+          (BUNDLE *) ((EFI_PLABEL *) HookStub)->EntryPoint,\r
+          NewCallback\r
+          );\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 UINTN                         Length\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+  DebugSupport protocol member function.  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
+  EFI_SUCCESS\r
+\r
+--*/\r
+{\r
+  InstructionCacheFlush (Start, Length);\r
+  return (EFI_SUCCESS);\r
+}\r