]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / PlDebugSupport.c
index a148842b4050c11b4eb4f7c78aa9633d821724f6..40fe74d8c236a0e9d7074d596983602cb73d42a5 100644 (file)
@@ -1,86 +1,63 @@
-/**@file\r
-  IA32 specific debug support functions\r
+/** @file\r
+  IA32/x64 generic functions to support Debug Support protocol.\r
 \r
-Copyright (c) 2006 - 2007, 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
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-//\r
-// private header files\r
-//\r
-#include "plDebugSupport.h"\r
+#include "DebugSupport.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;\r
+IDT_ENTRY  *IdtEntryTable = NULL;\r
 \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
+  Read IDT Gate Descriptor from IDT Table.\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
+  @param  Vector            Specifies vector number.\r
+  @param  IdtGateDescriptor Pointer to IDT Gate Descriptor read from IDT Table.\r
 \r
---*/\r
+**/\r
+VOID\r
+ReadIdtGateDescriptor (\r
+  IN  EFI_EXCEPTION_TYPE        Vector,\r
+  OUT IA32_IDT_GATE_DESCRIPTOR  *IdtGateDescriptor\r
+  )\r
 {\r
-  UINT8       *StubCopy;\r
+  IA32_DESCRIPTOR           IdtrValue;\r
+  IA32_IDT_GATE_DESCRIPTOR  *IdtTable;\r
 \r
-  StubCopy = *Stub;\r
+  AsmReadIdtr (&IdtrValue);\r
+  IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;\r
 \r
-  //\r
-  // Fixup the stub code for this vector\r
-  //\r
+  CopyMem ((VOID *)IdtGateDescriptor, (VOID *)&(IdtTable)[Vector], sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
+}\r
 \r
-  // The stub code looks like this:\r
-  //\r
-  //    00000000  89 25 00000004 R  mov     AppEsp, esp             ; save stack top\r
-  //    00000006  BC 00008014 R     mov     esp, offset DbgStkBot   ; switch to debugger stack\r
-  //    0000000B  6A 00             push    0                       ; push vector number - will be modified before installed\r
-  //    0000000D  E9                db      0e9h                    ; jump rel32\r
-  //    0000000E  00000000          dd      0                       ; fixed up to relative address of CommonIdtEntry\r
-  //\r
+/**\r
+  Write IDT Gate Descriptor into IDT Table.\r
 \r
-  //\r
-  // poke in the exception type so the second push pushes the exception type\r
-  //\r
-  StubCopy[0x0c] = (UINT8) ExceptionType;\r
+  @param  Vector            Specifies vector number.\r
+  @param  IdtGateDescriptor Pointer to IDT Gate Descriptor written into IDT Table.\r
 \r
-  //\r
-  // fixup the jump target to point to the common entry\r
-  //\r
-  *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];\r
+**/\r
+VOID\r
+WriteIdtGateDescriptor (\r
+  EFI_EXCEPTION_TYPE        Vector,\r
+  IA32_IDT_GATE_DESCRIPTOR  *IdtGateDescriptor\r
+  )\r
+{\r
+  IA32_DESCRIPTOR           IdtrValue;\r
+  IA32_IDT_GATE_DESCRIPTOR  *IdtTable;\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+  AsmReadIdtr (&IdtrValue);\r
+  IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;\r
 \r
-EFI_STATUS\r
-HookEntry (\r
-  IN EFI_EXCEPTION_TYPE            ExceptionType,\r
-  IN VOID                         (*NewCallback) ()\r
-  )\r
-/*++\r
+  CopyMem ((VOID *)&(IdtTable)[Vector], (VOID *)IdtGateDescriptor, sizeof (IA32_IDT_GATE_DESCRIPTOR));\r
+}\r
 \r
-Routine Description:\r
+/**\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
@@ -88,268 +65,272 @@ Routine Description:
   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
+  @param  ExceptionType      Specifies which vector to hook.\r
+  @param  NewCallback        A pointer to the new function to be registered.\r
 \r
---*/\r
+**/\r
+VOID\r
+HookEntry (\r
+  IN EFI_EXCEPTION_TYPE  ExceptionType,\r
+  IN CALLBACK_FUNC       NewCallback\r
+  )\r
 {\r
-  BOOLEAN     OldIntFlagState;\r
-  EFI_STATUS  Status;\r
+  BOOLEAN  OldIntFlagState;\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
+  CreateEntryStub (ExceptionType, (VOID **)&IdtEntryTable[ExceptionType].StubEntry);\r
 \r
-    ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0]  = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[0];\r
-    ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1]  = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];\r
+  //\r
+  // Disables CPU interrupts and returns the previous interrupt state\r
+  //\r
+  OldIntFlagState = SaveAndDisableInterrupts ();\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
+  // gets IDT Gate descriptor by index\r
+  //\r
+  ReadIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
+  //\r
+  // stores orignal interrupt handle\r
+  //\r
+  IdtEntryTable[ExceptionType].OrigVector = (DEBUG_PROC)GetInterruptHandleFromIdt (&(IdtEntryTable[ExceptionType].OrigDesc));\r
 \r
-  return Status;\r
-}\r
+  //\r
+  // encodes new IDT Gate descriptor by stub entry\r
+  //\r
+  Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r
+  //\r
+  // stores NewCallback\r
+  //\r
+  IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r
 \r
-EFI_STATUS\r
-UnhookEntry (\r
-  IN EFI_EXCEPTION_TYPE           ExceptionType\r
-  )\r
-/*++\r
+  //\r
+  // writes back new IDT Gate descriptor\r
+  //\r
+  WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r
 \r
-Routine Description:\r
-  Undoes HookEntry. This code executes in boot services context.\r
+  //\r
+  // restore interrupt state\r
+  //\r
+  SetInterruptState (OldIntFlagState);\r
 \r
-Arguments:\r
-  ExceptionType - specifies which entry to unhook\r
+  return;\r
+}\r
 \r
-Returns:\r
-  EFI_SUCCESS\r
+/**\r
+  Undoes HookEntry. This code executes in boot services context.\r
 \r
---*/\r
-{\r
-  BOOLEAN     OldIntFlagState;\r
+  @param  ExceptionType   Specifies which entry to unhook\r
 \r
-  OldIntFlagState = WriteInterruptFlag (0);\r
-  WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r
-  WriteInterruptFlag (OldIntFlagState);\r
+**/\r
+VOID\r
+UnhookEntry (\r
+  IN EFI_EXCEPTION_TYPE  ExceptionType\r
+  )\r
+{\r
+  BOOLEAN  OldIntFlagState;\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+  //\r
+  // Disables CPU interrupts and returns the previous interrupt state\r
+  //\r
+  OldIntFlagState = SaveAndDisableInterrupts ();\r
 \r
-EFI_STATUS\r
-ManageIdtEntryTable (\r
-  VOID (*NewCallback)(),\r
-  EFI_EXCEPTION_TYPE ExceptionType\r
-  )\r
-/*++\r
+  //\r
+  // restore the default IDT Date Descriptor\r
+  //\r
+  WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\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
+  // restore interrupt state\r
+  //\r
+  SetInterruptState (OldIntFlagState);\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
+  return;\r
+}\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
+  Returns the maximum value that may be used for the ProcessorIndex parameter in\r
+  RegisterPeriodicCallback() and RegisterExceptionCallback().\r
 \r
-  Other possible return values are passed through from UnHookEntry and HookEntry.\r
+  Hard coded to support only 1 processor for now.\r
 \r
---*/\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  Status = EFI_SUCCESS;\r
-\r
-  if (!FeaturePcdGet (PcdNtEmulatorEnable)) {\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
+  @param  This                  A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r
+  @param  MaxProcessorIndex     Pointer to a caller-allocated UINTN in which the maximum supported\r
+                                processor index is returned. Always 0 returned.\r
 \r
-  return Status;\r
-}\r
+  @retval EFI_SUCCESS           Always returned with **MaxProcessorIndex set to 0.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 GetMaximumProcessorIndex (\r
-  IN EFI_DEBUG_SUPPORT_PROTOCOL       *This,\r
-  OUT UINTN                           *MaxProcessorIndex\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
+  return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Registers a function to be called back periodically in interrupt context.\r
+\r
+  @param  This                  A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r
+  @param  ProcessorIndex        Specifies which processor the callback function applies to.\r
+  @param  PeriodicCallback      A pointer to a function of type PERIODIC_CALLBACK that is the main\r
+                                periodic entry point of the debug agent.\r
+\r
+  @retval EFI_SUCCESS           The function completed successfully.\r
+  @retval EFI_ALREADY_STARTED   Non-NULL PeriodicCallback parameter when a callback\r
+                                function was previously registered.\r
+  @retval EFI_OUT_OF_RESOURCES  System has insufficient memory resources to register new callback\r
+                                function.\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
+  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
+/**\r
+  Registers a function to be called when a given processor exception occurs.\r
+\r
+  This code executes in boot services context.\r
+\r
+  @param  This                  A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r
+  @param  ProcessorIndex        Specifies which processor the callback function applies to.\r
+  @param  ExceptionCallback     A pointer to a function of type EXCEPTION_CALLBACK that is called\r
+                                when the processor exception specified by ExceptionType occurs.\r
+  @param  ExceptionType         Specifies which processor exception to hook.\r
+\r
+  @retval EFI_SUCCESS           The function completed successfully.\r
+  @retval EFI_ALREADY_STARTED   Non-NULL PeriodicCallback parameter when a callback\r
+                                function was previously registered.\r
+  @retval EFI_OUT_OF_RESOURCES  System has insufficient memory resources to register new callback\r
+                                function.\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
+  IN EFI_DEBUG_SUPPORT_PROTOCOL  *This,\r
+  IN UINTN                       ProcessorIndex,\r
+  IN EFI_EXCEPTION_CALLBACK      ExceptionCallback,\r
+  IN EFI_EXCEPTION_TYPE          ExceptionType\r
   )\r
-/*++\r
+{\r
+  return ManageIdtEntryTable (ExceptionCallback, ExceptionType);\r
+}\r
 \r
-Routine Description:\r
-  This is a DebugSupport protocol member function.\r
+/**\r
+  Invalidates processor instruction cache for a memory range. Subsequent execution in this range\r
+  causes a fresh memory fetch to retrieve code to be executed.\r
 \r
-  This code executes in boot services context.\r
+  @param  This                  A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r
+  @param  ProcessorIndex        Specifies which processor's instruction cache is to be invalidated.\r
+  @param  Start                 Specifies the physical base of the memory range to be invalidated.\r
+  @param  Length                Specifies the minimum number of bytes in the processor's instruction\r
+                                cache to invalidate.\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
+  @retval EFI_SUCCESS           Always returned.\r
 \r
-Returns:\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
+  AsmWbinvd ();\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Common piece of code that invokes the registered handlers.\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
+  This code executes in exception context so no efi calls are allowed.\r
+  This code is called from assembly file.\r
 \r
-  Other possible return values are passed through from UnHookEntry and HookEntry.\r
+  @param  ExceptionType     Exception type\r
+  @param  ContextRecord     System context\r
 \r
---*/\r
+**/\r
+VOID\r
+InterruptDistrubutionHub (\r
+  EFI_EXCEPTION_TYPE       ExceptionType,\r
+  EFI_SYSTEM_CONTEXT_IA32  *ContextRecord\r
+  )\r
 {\r
-  return ManageIdtEntryTable (NewCallback, ExceptionType);\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
 \r
+/**\r
+  This is the callback that is written to the Loaded Image protocol instance\r
+  on the image handle. It uninstalls all registered handlers and frees all entry\r
+  stub memory.\r
+\r
+  @param  ImageHandle    The firmware allocated handle for the EFI image.\r
+\r
+  @retval EFI_SUCCESS    Always.\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
+PlUnloadDebugSupportDriver (\r
+  IN EFI_HANDLE  ImageHandle\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
+  EFI_EXCEPTION_TYPE  ExceptionType;\r
 \r
-Returns:\r
+  for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
+    ManageIdtEntryTable (NULL, ExceptionType);\r
+    //\r
+    // Free space for each Interrupt Stub precedure.\r
+    //\r
+    if (IdtEntryTable[ExceptionType].StubEntry != NULL) {\r
+      FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r
+    }\r
+  }\r
 \r
-  EFI_SUCCESS - always return success\r
+  FreePool (IdtEntryTable);\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
+/**\r
   Initializes driver's handler registration database.\r
 \r
   This code executes in boot services context.\r
+  Must be public because it's referenced from DebugSupport.c\r
 \r
-Arguments:\r
-  None\r
-\r
-Returns:\r
-  EFI_SUCCESS\r
-  EFI_UNSUPPORTED - if IA32 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
+  @retval  EFI_UNSUPPORTED      If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,\r
+                                the context save will fail, so these processors are not supported.\r
+  @retval  EFI_OUT_OF_RESOURCES Fails to allocate memory.\r
+  @retval  EFI_SUCCESS          Initializes successfully.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+PlInitializeDebugSupportDriver (\r
+  VOID\r
+  )\r
 {\r
   EFI_EXCEPTION_TYPE  ExceptionType;\r
 \r
+  //\r
+  // Check whether FxStor instructions are supported.\r
+  //\r
   if (!FxStorSupport ()) {\r
     return EFI_UNSUPPORTED;\r
   }\r
@@ -360,13 +341,17 @@ Returns:
   }\r
 \r
   for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r
-    IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);\r
+    IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC)(UINTN)AllocatePool (StubSize);\r
     if (IdtEntryTable[ExceptionType].StubEntry == NULL) {\r
       goto ErrorCleanup;\r
     }\r
 \r
+    //\r
+    // Copy Interrupt stub code.\r
+    //\r
     CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);\r
   }\r
+\r
   return EFI_SUCCESS;\r
 \r
 ErrorCleanup:\r
@@ -376,71 +361,8 @@ ErrorCleanup:
       FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r
     }\r
   }\r
-  FreePool (IdtEntryTable);\r
-\r
-  return EFI_OUT_OF_RESOURCES;\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
   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
+  return EFI_OUT_OF_RESOURCES;\r
 }\r