]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/CpuExceptionHandlerLib: Avoid calling PEI services from AP
authorRuiyu Ni <ruiyu.ni@intel.com>
Fri, 31 Aug 2018 08:29:20 +0000 (16:29 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 3 Sep 2018 06:02:26 +0000 (14:02 +0800)
When an exception happens in AP, system hangs at
GetPeiServicesTablePointer(), complaining the PeiServices retrieved
from memory before IDT is NULL.

Due to the following commit:
c563077a380437c114aba4c95be65eb963ebc1f3
* UefiCpuPkg/MpInitLib: Avoid calling PEI services from AP
the IDT used by AP no longer preserve PeiServices pointer in the
very beginning.
But the implementation of PeiExceptionHandlerLib still assumes
the PeiServices pointer is there, so the assertion happens.

The patch fixes the exception handler library to not call
PEI services from AP.

The patch duplicates the #0 exception stub header in an allocated
pool but with extra 4-byte/8-byte to store the exception handler
data which was originally stored in HOB.
When AP exception happens, the code gets the exception handler data
from the exception handler for #0.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Fan Jeff <vanjeff_919@hotmail.com>
UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h
UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c

index e10d9379d59644bee34eaea684ff4d78f1f7cd1f..459f06ac8452fbf60a00c7e6db732f87cddba4ea 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Common header file for CPU Exception Handler Library.\r
 \r
-  Copyright (c) 2012 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2018, 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
 \r
 #include "ArchInterruptDefs.h"\r
 \r
-#define CPU_EXCEPTION_HANDLER_LIB_HOB_GUID \\r
-  { \\r
-    0xb21d9148, 0x9211, 0x4d8f, { 0xad, 0xd3, 0x66, 0xb1, 0x89, 0xc9, 0x2c, 0x83 } \\r
-  }\r
-\r
 #define CPU_STACK_SWITCH_EXCEPTION_NUMBER \\r
   FixedPcdGetSize (PcdCpuStackSwitchExceptionList)\r
 \r
index 6f271983f2d27d4842368674b57e8c9dfe6b6690..5dd8423d2f5e8c5b0cd6c2fa986f60c676fd67d5 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   CPU exception handler library implementation for PEIM module.\r
 \r
-Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2016 - 2018, 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
@@ -20,10 +20,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 CONST UINTN    mDoFarReturnFlag  = 0;\r
 \r
-EFI_GUID mCpuExceptrionHandlerLibHobGuid = CPU_EXCEPTION_HANDLER_LIB_HOB_GUID;\r
+typedef struct {\r
+  UINT8                   ExceptionStubHeader[HOOKAFTER_STUB_SIZE];\r
+  EXCEPTION_HANDLER_DATA  *ExceptionHandlerData;\r
+} EXCEPTION0_STUB_HEADER;\r
 \r
 /**\r
-  Get exception handler data pointer from GUIDed HOb.\r
+  Get exception handler data pointer from IDT[0].\r
+\r
+  The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r
+  exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r
+  The code assumes that all processors uses the same exception handler for #0 exception.\r
 \r
   @return  pointer to exception handler data.\r
 **/\r
@@ -32,18 +39,50 @@ GetExceptionHandlerData (
   VOID\r
   )\r
 {\r
-  EFI_HOB_GUID_TYPE       *GuidHob;\r
-  VOID                    *DataInHob;\r
-  EXCEPTION_HANDLER_DATA  *ExceptionHandlerData;\r
+  IA32_DESCRIPTOR                  IdtDescriptor;\r
+  IA32_IDT_GATE_DESCRIPTOR         *IdtTable;\r
+  EXCEPTION0_STUB_HEADER           *Exception0StubHeader;\r
+\r
+  AsmReadIdtr (&IdtDescriptor);\r
+  IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r
+  \r
+  Exception0StubHeader = (EXCEPTION0_STUB_HEADER *)ArchGetIdtHandler (&IdtTable[0]);\r
+  return Exception0StubHeader->ExceptionHandlerData;\r
+}\r
 \r
-  ExceptionHandlerData = NULL;\r
-  GuidHob = GetFirstGuidHob (&mCpuExceptrionHandlerLibHobGuid);\r
-  if (GuidHob != NULL) {\r
-    DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
-    ExceptionHandlerData = (EXCEPTION_HANDLER_DATA *)(*(UINTN *)DataInHob);\r
-  }\r
-  ASSERT (ExceptionHandlerData != NULL);\r
-  return ExceptionHandlerData;\r
+/**\r
+  Set exception handler data pointer to IDT[0].\r
+\r
+  The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r
+  exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r
+  The code assumes that all processors uses the same exception handler for #0 exception.\r
+\r
+  @param  pointer to exception handler data.\r
+**/\r
+VOID\r
+SetExceptionHandlerData (\r
+  IN EXCEPTION_HANDLER_DATA        *ExceptionHandlerData\r
+  )\r
+{\r
+  EXCEPTION0_STUB_HEADER           *Exception0StubHeader;\r
+  IA32_DESCRIPTOR                  IdtDescriptor;\r
+  IA32_IDT_GATE_DESCRIPTOR         *IdtTable;\r
+  //\r
+  // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.\r
+  // So AP can get the ExceptionHandlerData by reading the IDT[0].\r
+  //\r
+  AsmReadIdtr (&IdtDescriptor);\r
+  IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r
+  \r
+  Exception0StubHeader = AllocatePool (sizeof (*Exception0StubHeader));\r
+  ASSERT (Exception0StubHeader != NULL);\r
+  CopyMem (\r
+    Exception0StubHeader->ExceptionStubHeader,\r
+    (VOID *)ArchGetIdtHandler (&IdtTable[0]),\r
+    sizeof (Exception0StubHeader->ExceptionStubHeader)\r
+    );\r
+  Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;\r
+  ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);\r
 }\r
 \r
 /**\r
@@ -109,15 +148,7 @@ InitializeCpuExceptionHandlers (
     return Status;\r
   }\r
 \r
-  //\r
-  // Build location of CPU MP DATA buffer in HOB\r
-  //\r
-  BuildGuidDataHob (\r
-    &mCpuExceptrionHandlerLibHobGuid,\r
-    (VOID *)&ExceptionHandlerData,\r
-    sizeof(UINT64)\r
-    );\r
-\r
+  SetExceptionHandlerData (ExceptionHandlerData);\r
   return EFI_SUCCESS;\r
 }\r
 \r