]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / PeiDxeSmmCpuException.c
index 4a3aad896d2464f833f1a8a492759e17bb501223..72c2aeca4c130f8d4e9341fff231b235229a5fac 100644 (file)
@@ -1,88 +1,76 @@
 /** @file\r
   CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.\r
 \r
-Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials are licensed and made available under\r
-the terms and conditions of the BSD License that accompanies this distribution.\r
-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) 2012 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "CpuExceptionCommon.h"\r
 #include <Library/DebugLib.h>\r
 \r
-//\r
-// Spinlock for CPU information display\r
-//\r
-SPIN_LOCK        mDisplayMessageSpinLock;\r
-\r
-//\r
-// Image align size for DXE/SMM\r
-//\r
-CONST UINTN      mImageAlignSize = SIZE_4KB;\r
-\r
-RESERVED_VECTORS_DATA       mReservedVectorsData[CPU_EXCEPTION_NUM];\r
-EFI_CPU_INTERRUPT_HANDLER   mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];\r
-EFI_CPU_INTERRUPT_HANDLER   *mExternalInterruptHandler = NULL;\r
-UINTN                       mEnabledInterruptNum = 0;\r
-\r
 /**\r
-  Common exception handler.\r
+  Internal worker function for common exception handler.\r
 \r
-  @param ExceptionType  Exception type.\r
-  @param SystemContext  Pointer to EFI_SYSTEM_CONTEXT.\r
+  @param ExceptionType         Exception type.\r
+  @param SystemContext         Pointer to EFI_SYSTEM_CONTEXT.\r
+  @param ExceptionHandlerData  Pointer to exception handler data.\r
 **/\r
 VOID\r
-EFIAPI\r
-CommonExceptionHandler (\r
-  IN EFI_EXCEPTION_TYPE          ExceptionType, \r
-  IN EFI_SYSTEM_CONTEXT          SystemContext\r
+CommonExceptionHandlerWorker (\r
+  IN EFI_EXCEPTION_TYPE          ExceptionType,\r
+  IN EFI_SYSTEM_CONTEXT          SystemContext,\r
+  IN EXCEPTION_HANDLER_DATA      *ExceptionHandlerData\r
   )\r
 {\r
   EXCEPTION_HANDLER_CONTEXT      *ExceptionHandlerContext;\r
+  RESERVED_VECTORS_DATA          *ReservedVectors;\r
+  EFI_CPU_INTERRUPT_HANDLER      *ExternalInterruptHandler;\r
 \r
-  ExceptionHandlerContext = (EXCEPTION_HANDLER_CONTEXT *) (UINTN) (SystemContext.SystemContextIa32);\r
+  ExceptionHandlerContext  = (EXCEPTION_HANDLER_CONTEXT *) (UINTN) (SystemContext.SystemContextIa32);\r
+  ReservedVectors          = ExceptionHandlerData->ReservedVectors;\r
+  ExternalInterruptHandler = ExceptionHandlerData->ExternalInterruptHandler;\r
 \r
-  switch (mReservedVectors[ExceptionType].Attribute) {\r
+  switch (ReservedVectors[ExceptionType].Attribute) {\r
   case EFI_VECTOR_HANDOFF_HOOK_BEFORE:\r
     //\r
-    // Need to jmp to old IDT handler after this exception handler\r
+    // The new exception handler registered by RegisterCpuInterruptHandler() is executed BEFORE original handler.\r
+    // Save the original handler to stack so the assembly code can jump to it instead of returning from handler.\r
     //\r
     ExceptionHandlerContext->ExceptionDataFlag = (mErrorCodeFlag & (1 << ExceptionType)) ? TRUE : FALSE;\r
-    ExceptionHandlerContext->OldIdtHandler     = mReservedVectors[ExceptionType].ExceptonHandler;\r
+    ExceptionHandlerContext->OldIdtHandler     = ReservedVectors[ExceptionType].ExceptonHandler;\r
     break;\r
   case EFI_VECTOR_HANDOFF_HOOK_AFTER:\r
     while (TRUE) {\r
       //\r
-      // If if anyone has gotten SPIN_LOCK for owner running hook after\r
+      // If spin-lock can be acquired, it's the first time entering here.\r
       //\r
-      if (AcquireSpinLockOrFail (&mReservedVectors[ExceptionType].SpinLock)) {\r
+      if (AcquireSpinLockOrFail (&ReservedVectors[ExceptionType].SpinLock)) {\r
         //\r
-        // Need to execute old IDT handler before running this exception handler\r
+        // The new exception handler registered by RegisterCpuInterruptHandler() is executed AFTER original handler.\r
+        // Save the original handler to stack but skip running the new handler so the original handler is executed\r
+        // firstly.\r
         //\r
-        mReservedVectors[ExceptionType].ApicId = GetApicId ();\r
-        ArchSaveExceptionContext (ExceptionType, SystemContext);\r
+        ReservedVectors[ExceptionType].ApicId = GetApicId ();\r
+        ArchSaveExceptionContext (ExceptionType, SystemContext, ExceptionHandlerData);\r
         ExceptionHandlerContext->ExceptionDataFlag = (mErrorCodeFlag & (1 << ExceptionType)) ? TRUE : FALSE;\r
-        ExceptionHandlerContext->OldIdtHandler     = mReservedVectors[ExceptionType].ExceptonHandler;\r
+        ExceptionHandlerContext->OldIdtHandler     = ReservedVectors[ExceptionType].ExceptonHandler;\r
         return;\r
       }\r
       //\r
-      // If failed to acquire SPIN_LOCK, check if it was locked by processor itself\r
+      // If spin-lock cannot be acquired, it's the second time entering here.\r
+      // 'break' instead of 'return' is used so the new exception handler can be executed.\r
       //\r
-      if (mReservedVectors[ExceptionType].ApicId == GetApicId ()) {\r
+      if (ReservedVectors[ExceptionType].ApicId == GetApicId ()) {\r
         //\r
-        // Old IDT handler has been executed, then retore CPU exception content to\r
+        // Old IDT handler has been executed, then restore CPU exception content to\r
         // run new exception handler.\r
         //\r
-        ArchRestoreExceptionContext (ExceptionType, SystemContext);\r
+        ArchRestoreExceptionContext (ExceptionType, SystemContext, ExceptionHandlerData);\r
         //\r
         // Rlease spin lock for ApicId\r
         //\r
-        ReleaseSpinLock (&mReservedVectors[ExceptionType].SpinLock);\r
+        ReleaseSpinLock (&ReservedVectors[ExceptionType].SpinLock);\r
         break;\r
       }\r
       CpuPause ();\r
@@ -97,28 +85,33 @@ CommonExceptionHandler (
     CpuDeadLoop ();\r
     break;\r
   }\r
-  \r
-  if (mExternalInterruptHandler[ExceptionType] != NULL) {\r
-    (mExternalInterruptHandler[ExceptionType]) (ExceptionType, SystemContext);\r
+\r
+  if (ExternalInterruptHandler != NULL &&\r
+      ExternalInterruptHandler[ExceptionType] != NULL) {\r
+    (ExternalInterruptHandler[ExceptionType]) (ExceptionType, SystemContext);\r
   } else if (ExceptionType < CPU_EXCEPTION_NUM) {\r
     //\r
     // Get Spinlock to display CPU information\r
     //\r
-    while (!AcquireSpinLockOrFail (&mDisplayMessageSpinLock)) {\r
+    while (!AcquireSpinLockOrFail (&ExceptionHandlerData->DisplayMessageSpinLock)) {\r
       CpuPause ();\r
     }\r
     //\r
+    // Initialize the serial port before dumping.\r
+    //\r
+    SerialPortInitialize ();\r
+    //\r
     // Display ExceptionType, CPU information and Image information\r
-    //  \r
-    DumpCpuContent (ExceptionType, SystemContext);\r
+    //\r
+    DumpImageAndCpuContent (ExceptionType, SystemContext);\r
     //\r
     // Release Spinlock of output message\r
     //\r
-    ReleaseSpinLock (&mDisplayMessageSpinLock);\r
+    ReleaseSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);\r
     //\r
     // Enter a dead loop if needn't to execute old IDT handler further\r
     //\r
-    if (mReservedVectors[ExceptionType].Attribute != EFI_VECTOR_HANDOFF_HOOK_BEFORE) {\r
+    if (ReservedVectors[ExceptionType].Attribute != EFI_VECTOR_HANDOFF_HOOK_BEFORE) {\r
       CpuDeadLoop ();\r
     }\r
   }\r
@@ -194,11 +187,6 @@ UpdateIdtTable (
       break;\r
     }\r
   }\r
\r
-  //\r
-  // Save Interrupt number to global variable used for RegisterCpuInterruptHandler ()\r
-  //\r
-  mEnabledInterruptNum = ExceptionHandlerData->IdtEntryCount;\r
 }\r
 \r
 /**\r
@@ -206,8 +194,8 @@ UpdateIdtTable (
 \r
   @param[in]      VectorInfo            Pointer to reserved vector list.\r
   @param[in, out] ExceptionHandlerData  Pointer to exception handler data.\r
-  \r
-  @retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized \r
+\r
+  @retval EFI_SUCCESS           CPU Exception Entries have been successfully initialized\r
                                 with default exception handlers.\r
   @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
   @retval EFI_UNSUPPORTED       This function is not supported.\r
@@ -234,9 +222,7 @@ InitializeCpuExceptionHandlersWorker (
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
-  InitializeSpinLock (&mDisplayMessageSpinLock);\r
 \r
-  mExternalInterruptHandler = mExternalInterruptHandlerTable;\r
   //\r
   // Read IDT descriptor and calculate IDT size\r
   //\r
@@ -255,17 +241,18 @@ InitializeCpuExceptionHandlersWorker (
 \r
   ExceptionHandlerData->IdtEntryCount = IdtEntryCount;\r
   UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);\r
-  mEnabledInterruptNum = IdtEntryCount;\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Registers a function to be called from the processor interrupt handler.\r
 \r
-  @param[in]  InterruptType     Defines which interrupt or exception to hook.\r
-  @param[in]  InterruptHandler  A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
-                                when a processor interrupt occurs. If this parameter is NULL, then the handler\r
-                                will be uninstalled.\r
+  @param[in]  InterruptType        Defines which interrupt or exception to hook.\r
+  @param[in]  InterruptHandler     A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
+                                   when a processor interrupt occurs. If this parameter is NULL, then the handler\r
+                                   will be uninstalled\r
+  @param[in] ExceptionHandlerData  Pointer to exception handler data.\r
 \r
   @retval EFI_SUCCESS           The handler for the processor interrupt was successfully installed or uninstalled.\r
   @retval EFI_ALREADY_STARTED   InterruptHandler is not NULL, and a handler for InterruptType was\r
@@ -278,23 +265,32 @@ InitializeCpuExceptionHandlersWorker (
 EFI_STATUS\r
 RegisterCpuInterruptHandlerWorker (\r
   IN EFI_EXCEPTION_TYPE            InterruptType,\r
-  IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler\r
+  IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler,\r
+  IN EXCEPTION_HANDLER_DATA        *ExceptionHandlerData\r
   )\r
 {\r
-  if (InterruptType < 0 || InterruptType >= (EFI_EXCEPTION_TYPE)mEnabledInterruptNum ||\r
-      mReservedVectors[InterruptType].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {\r
+  UINTN                          EnabledInterruptNum;\r
+  RESERVED_VECTORS_DATA          *ReservedVectors;\r
+  EFI_CPU_INTERRUPT_HANDLER      *ExternalInterruptHandler;\r
+\r
+  EnabledInterruptNum      = ExceptionHandlerData->IdtEntryCount;\r
+  ReservedVectors          = ExceptionHandlerData->ReservedVectors;\r
+  ExternalInterruptHandler = ExceptionHandlerData->ExternalInterruptHandler;\r
+\r
+  if (InterruptType < 0 || InterruptType >= (EFI_EXCEPTION_TYPE)EnabledInterruptNum ||\r
+      ReservedVectors[InterruptType].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  if (InterruptHandler == NULL && mExternalInterruptHandler[InterruptType] == NULL) {\r
+  if (InterruptHandler == NULL && ExternalInterruptHandler[InterruptType] == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (InterruptHandler != NULL && mExternalInterruptHandler[InterruptType] != NULL) {\r
+  if (InterruptHandler != NULL && ExternalInterruptHandler[InterruptType] != NULL) {\r
     return EFI_ALREADY_STARTED;\r
   }\r
 \r
-  mExternalInterruptHandler[InterruptType] = InterruptHandler;\r
+  ExternalInterruptHandler[InterruptType] = InterruptHandler;\r
   return EFI_SUCCESS;\r
 }\r
 \r