]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg: Remove FIT based microcode shadow logic from MpInitLib.
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
index b393244e0582a369f884fc89176037024509b2e4..a987c3210957c042b3616d34659c2f06aaeed066 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   MP initialize support functions for DXE phase.\r
 \r
-  Copyright (c) 2016, 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) 2016 - 2020, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -17,6 +11,7 @@
 #include <Library/UefiLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/DebugAgentLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
 \r
 #include <Protocol/Timer.h>\r
 \r
@@ -75,72 +70,79 @@ SaveCpuMpData (
 }\r
 \r
 /**\r
-  Allocate reset vector buffer.\r
+  Get available system memory below 0x88000 by specified size.\r
+\r
+  @param[in] WakeupBufferSize   Wakeup buffer size required\r
 \r
-  @param[in, out]  CpuMpData  The pointer to CPU MP Data structure.\r
+  @retval other   Return wakeup buffer address below 1MB.\r
+  @retval -1      Cannot find free memory below 1MB.\r
 **/\r
-VOID\r
-AllocateResetVector (\r
-  IN OUT CPU_MP_DATA          *CpuMpData\r
+UINTN\r
+GetWakeupBuffer (\r
+  IN UINTN                WakeupBufferSize\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  UINTN                 ApResetVectorSize;\r
-  EFI_PHYSICAL_ADDRESS  StartAddress;\r
-\r
-  if (CpuMpData->SaveRestoreFlag) {\r
-    BackupAndPrepareWakeupBuffer (CpuMpData);\r
-  } else {\r
-    ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +\r
-                        sizeof (MP_CPU_EXCHANGE_INFO);\r
-\r
-    StartAddress = BASE_1MB;\r
-    Status = gBS->AllocatePages (\r
-                    AllocateMaxAddress,\r
-                    EfiACPIMemoryNVS,\r
-                    EFI_SIZE_TO_PAGES (ApResetVectorSize),\r
-                    &StartAddress\r
-                    );\r
-    ASSERT_EFI_ERROR (Status);\r
-\r
-    CpuMpData->WakeupBuffer      = (UINTN) StartAddress;\r
-    CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)\r
-                  (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);\r
-    //\r
-    // copy AP reset code in it\r
-    //\r
-    CopyMem (\r
-      (VOID *) CpuMpData->WakeupBuffer,\r
-      (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
-      CpuMpData->AddressMap.RendezvousFunnelSize\r
-      );\r
+  EFI_STATUS              Status;\r
+  EFI_PHYSICAL_ADDRESS    StartAddress;\r
+\r
+  //\r
+  // Try to allocate buffer below 1M for waking vector.\r
+  // LegacyBios driver only reports warning when page allocation in range\r
+  // [0x60000, 0x88000) fails.\r
+  // This library is consumed by CpuDxe driver to produce CPU Arch protocol.\r
+  // LagacyBios driver depends on CPU Arch protocol which guarantees below\r
+  // allocation runs earlier than LegacyBios driver.\r
+  //\r
+  StartAddress = 0x88000;\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  EfiBootServicesData,\r
+                  EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
+                  &StartAddress\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    StartAddress = (EFI_PHYSICAL_ADDRESS) -1;\r
   }\r
+\r
+  DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
+                      (UINTN) StartAddress, WakeupBufferSize));\r
+\r
+  return (UINTN) StartAddress;\r
 }\r
 \r
 /**\r
-  Free AP reset vector buffer.\r
+  Get available EfiBootServicesCode memory below 4GB by specified size.\r
+\r
+  This buffer is required to safely transfer AP from real address mode to\r
+  protected mode or long mode, due to the fact that the buffer returned by\r
+  GetWakeupBuffer() may be marked as non-executable.\r
+\r
+  @param[in] BufferSize   Wakeup transition buffer size.\r
 \r
-  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+  @retval other   Return wakeup transition buffer address below 4GB.\r
+  @retval 0       Cannot find free memory below 4GB.\r
 **/\r
-VOID\r
-FreeResetVector (\r
-  IN CPU_MP_DATA              *CpuMpData\r
+UINTN\r
+GetModeTransitionBuffer (\r
+  IN UINTN                BufferSize\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  UINTN                 ApResetVectorSize;\r
-\r
-  if (CpuMpData->SaveRestoreFlag) {\r
-    RestoreWakeupBuffer (CpuMpData);\r
-  } else {\r
-    ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +\r
-                        sizeof (MP_CPU_EXCHANGE_INFO);\r
-    Status = gBS->FreePages(\r
-               (EFI_PHYSICAL_ADDRESS)CpuMpData->WakeupBuffer,\r
-               EFI_SIZE_TO_PAGES (ApResetVectorSize)\r
-               );\r
-    ASSERT_EFI_ERROR (Status);\r
+  EFI_STATUS              Status;\r
+  EFI_PHYSICAL_ADDRESS    StartAddress;\r
+\r
+  StartAddress = BASE_4GB - 1;\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  EfiBootServicesCode,\r
+                  EFI_SIZE_TO_PAGES (BufferSize),\r
+                  &StartAddress\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    StartAddress = 0;\r
   }\r
+\r
+  return (UINTN)StartAddress;\r
 }\r
 \r
 /**\r
@@ -232,7 +234,6 @@ GetProtectedModeCS (
   UINTN                    GdtEntryCount;\r
   UINT16                   Index;\r
 \r
-  Index = (UINT16) -1;\r
   AsmReadGdtr (&GdtrDesc);\r
   GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
   GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
@@ -244,7 +245,7 @@ GetProtectedModeCS (
     }\r
     GdtEntry++;\r
   }\r
-  ASSERT (Index != -1);\r
+  ASSERT (Index != GdtEntryCount);\r
   return Index * 8;\r
 }\r
 \r
@@ -264,7 +265,7 @@ RelocateApLoop (
   ASM_RELOCATE_AP_LOOP   AsmRelocateApLoopFunc;\r
   UINTN                  ProcessorNumber;\r
 \r
-  MpInitLibWhoAmI (&ProcessorNumber); \r
+  MpInitLibWhoAmI (&ProcessorNumber);\r
   CpuMpData    = GetCpuMpData ();\r
   MwaitSupport = IsMwaitSupport ();\r
   AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;\r
@@ -299,11 +300,10 @@ MpInitChangeApLoopCallback (
   CPU_MP_DATA               *CpuMpData;\r
 \r
   CpuMpData = GetCpuMpData ();\r
-  CpuMpData->SaveRestoreFlag = TRUE;\r
   CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
   CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
   mNumberToFinish = CpuMpData->CpuCount - 1;\r
-  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL);\r
+  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
   while (mNumberToFinish > 0) {\r
     CpuPause ();\r
   }\r
@@ -320,9 +320,13 @@ InitMpGlobalData (
   IN CPU_MP_DATA               *CpuMpData\r
   )\r
 {\r
-  EFI_STATUS                 Status;\r
-  EFI_PHYSICAL_ADDRESS       Address;\r
-  UINTN                      ApSafeBufferSize;\r
+  EFI_STATUS                          Status;\r
+  EFI_PHYSICAL_ADDRESS                Address;\r
+  UINTN                               ApSafeBufferSize;\r
+  UINTN                               Index;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR     MemDesc;\r
+  UINTN                               StackBase;\r
+  CPU_INFO_IN_HOB                     *CpuInfoInHob;\r
 \r
   SaveCpuMpData (CpuMpData);\r
 \r
@@ -333,6 +337,46 @@ InitMpGlobalData (
     return;\r
   }\r
 \r
+  if (PcdGetBool (PcdCpuStackGuard)) {\r
+    //\r
+    // One extra page at the bottom of the stack is needed for Guard page.\r
+    //\r
+    if (CpuMpData->CpuApStackSize <= EFI_PAGE_SIZE) {\r
+      DEBUG ((DEBUG_ERROR, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));\r
+      ASSERT (FALSE);\r
+    }\r
+\r
+    //\r
+    // DXE will reuse stack allocated for APs at PEI phase if it's available.\r
+    // Let's check it here.\r
+    //\r
+    // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of\r
+    // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be\r
+    // set here.\r
+    //\r
+    CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;\r
+    for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {\r
+      if (CpuInfoInHob != NULL && CpuInfoInHob[Index].ApTopOfStack != 0) {\r
+        StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;\r
+      } else {\r
+        StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;\r
+      }\r
+\r
+      Status = gDS->GetMemorySpaceDescriptor (StackBase, &MemDesc);\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      Status = gDS->SetMemorySpaceAttributes (\r
+                      StackBase,\r
+                      EFI_PAGES_TO_SIZE (1),\r
+                      MemDesc.Attributes | EFI_MEMORY_RP\r
+                      );\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      DEBUG ((DEBUG_INFO, "Stack Guard set at %lx [cpu%lu]!\n",\r
+              (UINT64)StackBase, (UINT64)Index));\r
+    }\r
+  }\r
+\r
   //\r
   // Avoid APs access invalid buffer data which allocated by BootServices,\r
   // so we will allocate reserved data for AP loop code. We also need to\r
@@ -341,9 +385,9 @@ InitMpGlobalData (
   // Allocating it in advance since memory services are not available in\r
   // Exit Boot Services callback function.\r
   //\r
-  ApSafeBufferSize  = CpuMpData->AddressMap.RelocateApLoopFuncSize;\r
-  ApSafeBufferSize += CpuMpData->CpuCount * AP_SAFE_STACK_SIZE;\r
-\r
+  ApSafeBufferSize  = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
+                        CpuMpData->AddressMap.RelocateApLoopFuncSize\r
+                        ));\r
   Address = BASE_4GB - 1;\r
   Status  = gBS->AllocatePages (\r
                    AllocateMaxAddress,\r
@@ -352,9 +396,39 @@ InitMpGlobalData (
                    &Address\r
                    );\r
   ASSERT_EFI_ERROR (Status);\r
+\r
   mReservedApLoopFunc = (VOID *) (UINTN) Address;\r
   ASSERT (mReservedApLoopFunc != NULL);\r
-  mReservedTopOfApStack = (UINTN) Address + EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize));\r
+\r
+  //\r
+  // Make sure that the buffer memory is executable if NX protection is enabled\r
+  // for EfiReservedMemoryType.\r
+  //\r
+  // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD\r
+  //       service.\r
+  //\r
+  Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc);\r
+  if (!EFI_ERROR (Status)) {\r
+    gDS->SetMemorySpaceAttributes (\r
+           Address,\r
+           ApSafeBufferSize,\r
+           MemDesc.Attributes & (~EFI_MEMORY_XP)\r
+           );\r
+  }\r
+\r
+  ApSafeBufferSize = EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (\r
+                       CpuMpData->CpuCount * AP_SAFE_STACK_SIZE\r
+                       ));\r
+  Address = BASE_4GB - 1;\r
+  Status  = gBS->AllocatePages (\r
+                   AllocateMaxAddress,\r
+                   EfiReservedMemoryType,\r
+                   EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
+                   &Address\r
+                   );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  mReservedTopOfApStack = (UINTN) Address + ApSafeBufferSize;\r
   ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);\r
   CopyMem (\r
     mReservedApLoopFunc,\r
@@ -494,9 +568,10 @@ MpInitLibStartupAllAPs (
   //\r
   mStopCheckAllApsStatus = TRUE;\r
 \r
-  Status = StartupAllAPsWorker (\r
+  Status = StartupAllCPUsWorker (\r
              Procedure,\r
              SingleThread,\r
+             TRUE,\r
              WaitEvent,\r
              TimeoutInMicroseconds,\r
              ProcedureArgument,\r
@@ -741,3 +816,27 @@ MpInitLibEnableDisableAP (
 \r
   return Status;\r
 }\r
+\r
+/**\r
+  This funtion will try to invoke platform specific microcode shadow logic to\r
+  relocate microcode update patches into memory.\r
+\r
+  @param[in] CpuMpData  The pointer to CPU MP Data structure.\r
+\r
+  @retval EFI_SUCCESS              Shadow microcode success.\r
+  @retval EFI_OUT_OF_RESOURCES     No enough resource to complete the operation.\r
+  @retval EFI_UNSUPPORTED          Can't find platform specific microcode shadow\r
+                                   PPI/Protocol.\r
+**/\r
+EFI_STATUS\r
+PlatformShadowMicrocode (\r
+  IN OUT CPU_MP_DATA             *CpuMpData\r
+  )\r
+{\r
+  //\r
+  // There is no DXE version of platform shadow microcode protocol so far.\r
+  // A platform which only uses DxeMpInitLib instance could only supports\r
+  // the PCD based microcode shadowing.\r
+  //\r
+  return EFI_UNSUPPORTED;\r
+}\r