]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / DxeException.c
index b4b844cde928db4d819842a95d3fbf5a6c4a33db..fd59f09ecd082e21bd563b59cc9aee8bfe39fcc9 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   CPU exception handler library implemenation for DXE modules.\r
 \r
-  Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
-  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) 2013 - 2017, 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
 #include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
 \r
 CONST UINTN    mDoFarReturnFlag  = 0;\r
 \r
-extern SPIN_LOCK                   mDisplayMessageSpinLock;\r
-extern EFI_CPU_INTERRUPT_HANDLER   *mExternalInterruptHandler;\r
+RESERVED_VECTORS_DATA       mReservedVectorsData[CPU_EXCEPTION_NUM];\r
+EFI_CPU_INTERRUPT_HANDLER   mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];\r
+UINTN                       mEnabledInterruptNum = 0;\r
+\r
+EXCEPTION_HANDLER_DATA      mExceptionHandlerData;\r
+\r
+UINT8                       mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *\r
+                                      CPU_KNOWN_GOOD_STACK_SIZE];\r
+UINT8                       mNewGdt[CPU_TSS_GDT_SIZE];\r
+\r
+/**\r
+  Common exception handler.\r
+\r
+  @param ExceptionType  Exception type.\r
+  @param SystemContext  Pointer to EFI_SYSTEM_CONTEXT.\r
+**/\r
+VOID\r
+EFIAPI\r
+CommonExceptionHandler (\r
+  IN EFI_EXCEPTION_TYPE          ExceptionType,\r
+  IN EFI_SYSTEM_CONTEXT          SystemContext\r
+  )\r
+{\r
+  CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);\r
+}\r
 \r
 /**\r
   Initializes all CPU exceptions entries and provides the default exception handlers.\r
-  \r
+\r
   Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
   persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
-  If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL. \r
+  If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
   If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
 \r
   @param[in]  VectorInfo    Pointer to reserved vector list.\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
@@ -44,20 +62,23 @@ InitializeCpuExceptionHandlers (
   IN EFI_VECTOR_HANDOFF_INFO       *VectorInfo OPTIONAL\r
   )\r
 {\r
-  return InitializeCpuExceptionHandlersWorker (VectorInfo);\r
+  mExceptionHandlerData.ReservedVectors          = mReservedVectorsData;\r
+  mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;\r
+  InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
+  return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);\r
 }\r
 \r
 /**\r
   Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r
-  \r
+\r
   Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
   persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
-  If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL. \r
+  If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
   If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
 \r
   @param[in]  VectorInfo    Pointer to reserved vector list.\r
-  \r
-  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been successfully initialized \r
+\r
+  @retval EFI_SUCCESS           All CPU interrupt/exception entries have been successfully initialized\r
                                 with default interrupt/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
@@ -77,20 +98,26 @@ InitializeCpuInterruptHandlers (
   UINTN                              Index;\r
   UINTN                              InterruptEntry;\r
   UINT8                              *InterruptEntryCode;\r
+  RESERVED_VECTORS_DATA              *ReservedVectors;\r
+  EFI_CPU_INTERRUPT_HANDLER          *ExternalInterruptHandler;\r
 \r
-  mReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM);\r
-  ASSERT (mReservedVectors != NULL);\r
-  SetMem ((VOID *) mReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesCode,\r
+                  sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM,\r
+                  (VOID **)&ReservedVectors\r
+                  );\r
+  ASSERT (!EFI_ERROR (Status) && ReservedVectors != NULL);\r
+  SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r
   if (VectorInfo != NULL) {\r
-    Status = ReadAndVerifyVectorInfo (VectorInfo, mReservedVectors, CPU_INTERRUPT_NUM);\r
+    Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);\r
     if (EFI_ERROR (Status)) {\r
-      FreePool (mReservedVectors);\r
+      FreePool (ReservedVectors);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
-  InitializeSpinLock (&mDisplayMessageSpinLock);\r
-  mExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r
-  ASSERT (mExternalInterruptHandler != NULL);\r
+\r
+  ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r
+  ASSERT (ExternalInterruptHandler != NULL);\r
 \r
   //\r
   // Read IDT descriptor and calculate IDT size\r
@@ -109,9 +136,14 @@ InitializeCpuInterruptHandlers (
 \r
   AsmGetTemplateAddressMap (&TemplateMap);\r
   ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);\r
-  InterruptEntryCode = AllocatePool (TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM);\r
-  ASSERT (InterruptEntryCode != NULL);\r
-  \r
+\r
+  Status = gBS->AllocatePool (\r
+                  EfiBootServicesCode,\r
+                  TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM,\r
+                  (VOID **)&InterruptEntryCode\r
+                  );\r
+  ASSERT (!EFI_ERROR (Status) && InterruptEntryCode != NULL);\r
+\r
   InterruptEntry = (UINTN) InterruptEntryCode;\r
   for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {\r
     CopyMem (\r
@@ -119,12 +151,17 @@ InitializeCpuInterruptHandlers (
       (VOID *) TemplateMap.ExceptionStart,\r
       TemplateMap.ExceptionStubHeaderSize\r
       );\r
-    AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index);\r
+    AsmVectorNumFixup ((VOID *) InterruptEntry,  (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);\r
     InterruptEntry += TemplateMap.ExceptionStubHeaderSize;\r
   }\r
 \r
   TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;\r
-  UpdateIdtTable (IdtTable, &TemplateMap, CPU_INTERRUPT_NUM);\r
+  mExceptionHandlerData.IdtEntryCount            = CPU_INTERRUPT_NUM;\r
+  mExceptionHandlerData.ReservedVectors          = ReservedVectors;\r
+  mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;\r
+  InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
+\r
+  UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);\r
 \r
   //\r
   // Load Interrupt Descriptor Table\r
@@ -139,9 +176,9 @@ InitializeCpuInterruptHandlers (
 /**\r
   Registers a function to be called from the processor interrupt handler.\r
 \r
-  This function registers and enables the handler specified by InterruptHandler for a processor \r
-  interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
-  handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
+  This function registers and enables the handler specified by InterruptHandler for a processor\r
+  interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
+  handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
   The installed handler is called once for each processor interrupt or exception.\r
   NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
   InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
@@ -158,7 +195,7 @@ InitializeCpuInterruptHandlers (
                                 previously installed.\r
   @retval EFI_UNSUPPORTED       The interrupt specified by InterruptType is not supported,\r
                                 or this function is not supported.\r
-*/\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 RegisterCpuInterruptHandler (\r
@@ -166,5 +203,85 @@ RegisterCpuInterruptHandler (
   IN EFI_CPU_INTERRUPT_HANDLER     InterruptHandler\r
   )\r
 {\r
-  return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler);\r
+  return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);\r
+}\r
+\r
+/**\r
+  Initializes CPU exceptions entries and setup stack switch for given exceptions.\r
+\r
+  This method will call InitializeCpuExceptionHandlers() to setup default\r
+  exception handlers unless indicated not to do it explicitly.\r
+\r
+  If InitData is passed with NULL, this method will use the resource reserved\r
+  by global variables to initialize it; Otherwise it will use data in InitData\r
+  to setup stack switch. This is for the different use cases in DxeCore and\r
+  Cpu MP exception initialization.\r
+\r
+  @param[in]  VectorInfo    Pointer to reserved vector list.\r
+  @param[in]  InitData      Pointer to data required to setup stack switch for\r
+                            given exceptions.\r
+\r
+  @retval EFI_SUCCESS             The exceptions have been successfully\r
+                                  initialized.\r
+  @retval EFI_INVALID_PARAMETER   VectorInfo or InitData contains invalid\r
+                                  content.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeCpuExceptionHandlersEx (\r
+  IN EFI_VECTOR_HANDOFF_INFO            *VectorInfo OPTIONAL,\r
+  IN CPU_EXCEPTION_INIT_DATA            *InitData OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  CPU_EXCEPTION_INIT_DATA           EssData;\r
+  IA32_DESCRIPTOR                   Idtr;\r
+  IA32_DESCRIPTOR                   Gdtr;\r
+\r
+  //\r
+  // To avoid repeat initialization of default handlers, the caller should pass\r
+  // an extended init data with InitDefaultHandlers set to FALSE. There's no\r
+  // need to call this method to just initialize default handlers. Call non-ex\r
+  // version instead; or this method must be implemented as a simple wrapper of\r
+  // non-ex version of it, if this version has to be called.\r
+  //\r
+  if (InitData == NULL || InitData->X64.InitDefaultHandlers) {\r
+    Status = InitializeCpuExceptionHandlers (VectorInfo);\r
+  } else {\r
+    Status = EFI_SUCCESS;\r
+  }\r
+\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Initializing stack switch is only necessary for Stack Guard functionality.\r
+    //\r
+    if (PcdGetBool (PcdCpuStackGuard)) {\r
+      if (InitData == NULL) {\r
+        SetMem (mNewGdt, sizeof (mNewGdt), 0);\r
+\r
+        AsmReadIdtr (&Idtr);\r
+        AsmReadGdtr (&Gdtr);\r
+\r
+        EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
+        EssData.X64.KnownGoodStackTop = (UINTN)mNewStack + sizeof (mNewStack);\r
+        EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;\r
+        EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;\r
+        EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;\r
+        EssData.X64.IdtTable = (VOID *)Idtr.Base;\r
+        EssData.X64.IdtTableSize = Idtr.Limit + 1;\r
+        EssData.X64.GdtTable = mNewGdt;\r
+        EssData.X64.GdtTableSize = sizeof (mNewGdt);\r
+        EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;\r
+        EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;\r
+        EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;\r
+        EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;\r
+\r
+        InitData = &EssData;\r
+      }\r
+      Status = ArchSetupExceptionStack (InitData);\r
+    }\r
+  }\r
+\r
+  return  Status;\r
 }\r