]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Allocate a separate SEV-ES AP reset stack area
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
index 46a48a4166e151036636697b6085eefa2da61933..93fc63bf93e3070d5e3ddffae14158d72aacc74c 100644 (file)
 /** @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
 #include "MpLib.h"\r
 \r
+#include <Library/UefiLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugAgentLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/VmgExitLib.h>\r
+#include <Register/Amd/Fam17Msr.h>\r
+#include <Register/Amd/Ghcb.h>\r
+\r
+#include <Protocol/Timer.h>\r
+\r
+#define  AP_SAFE_STACK_SIZE    128\r
+\r
+CPU_MP_DATA      *mCpuMpData = NULL;\r
+EFI_EVENT        mCheckAllApsEvent = NULL;\r
+EFI_EVENT        mMpInitExitBootServicesEvent = NULL;\r
+EFI_EVENT        mLegacyBootEvent = NULL;\r
+volatile BOOLEAN mStopCheckAllApsStatus = TRUE;\r
+VOID             *mReservedApLoopFunc = NULL;\r
+UINTN            mReservedTopOfApStack;\r
+volatile UINT32  mNumberToFinish = 0;\r
+\r
+//\r
+// Begin wakeup buffer allocation below 0x88000\r
+//\r
+STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;\r
+\r
+/**\r
+  Enable Debug Agent to support source debugging on AP function.\r
+\r
+**/\r
+VOID\r
+EnableDebugAgent (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Initialize Debug Agent to support source level debug in DXE phase\r
+  //\r
+  InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP, NULL, NULL);\r
+}\r
+\r
+/**\r
+  Get the pointer to CPU MP Data structure.\r
+\r
+  @return  The pointer to CPU MP Data structure.\r
+**/\r
+CPU_MP_DATA *\r
+GetCpuMpData (\r
+  VOID\r
+  )\r
+{\r
+  ASSERT (mCpuMpData != NULL);\r
+  return mCpuMpData;\r
+}\r
+\r
+/**\r
+  Save the pointer to CPU MP Data structure.\r
+\r
+  @param[in] CpuMpData  The pointer to CPU MP Data structure will be saved.\r
+**/\r
+VOID\r
+SaveCpuMpData (\r
+  IN CPU_MP_DATA   *CpuMpData\r
+  )\r
+{\r
+  mCpuMpData = CpuMpData;\r
+}\r
+\r
+/**\r
+  Get available system memory below 0x88000 by specified size.\r
+\r
+  @param[in] WakeupBufferSize   Wakeup buffer size required\r
+\r
+  @retval other   Return wakeup buffer address below 1MB.\r
+  @retval -1      Cannot find free memory below 1MB.\r
+**/\r
+UINTN\r
+GetWakeupBuffer (\r
+  IN UINTN                WakeupBufferSize\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  EFI_PHYSICAL_ADDRESS    StartAddress;\r
+  EFI_MEMORY_TYPE         MemoryType;\r
+\r
+  if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    MemoryType = EfiReservedMemoryType;\r
+  } else {\r
+    MemoryType = EfiBootServicesData;\r
+  }\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
+  if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    //\r
+    // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one\r
+    //\r
+    StartAddress = mSevEsDxeWakeupBuffer;\r
+  } else {\r
+    StartAddress = 0x88000;\r
+  }\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  MemoryType,\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
+  } else if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    //\r
+    // Next SEV-ES wakeup buffer allocation must be below this allocation\r
+    //\r
+    mSevEsDxeWakeupBuffer = StartAddress;\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
+                      (UINTN) StartAddress, WakeupBufferSize));\r
+\r
+  return (UINTN) StartAddress;\r
+}\r
+\r
+/**\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
+  @retval other   Return wakeup transition buffer address below 4GB.\r
+  @retval 0       Cannot find free memory below 4GB.\r
+**/\r
+UINTN\r
+GetModeTransitionBuffer (\r
+  IN UINTN                BufferSize\r
+  )\r
+{\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
+  Return the address of the SEV-ES AP jump table.\r
+\r
+  This buffer is required in order for an SEV-ES guest to transition from\r
+  UEFI into an OS.\r
+\r
+  @return         Return SEV-ES AP jump table buffer\r
+**/\r
+UINTN\r
+GetSevEsAPMemory (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_PHYSICAL_ADDRESS      StartAddress;\r
+  MSR_SEV_ES_GHCB_REGISTER  Msr;\r
+  GHCB                      *Ghcb;\r
+  BOOLEAN                   InterruptState;\r
+\r
+  //\r
+  // Allocate 1 page for AP jump table page\r
+  //\r
+  StartAddress = BASE_4GB - 1;\r
+  Status = gBS->AllocatePages (\r
+                  AllocateMaxAddress,\r
+                  EfiReservedMemoryType,\r
+                  1,\r
+                  &StartAddress\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN) StartAddress));\r
+\r
+  //\r
+  // Save the SevEsAPMemory as the AP jump table.\r
+  //\r
+  Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
+  Ghcb = Msr.Ghcb;\r
+\r
+  VmgInit (Ghcb, &InterruptState);\r
+  VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64) (UINTN) StartAddress);\r
+  VmgDone (Ghcb, InterruptState);\r
+\r
+  return (UINTN) StartAddress;\r
+}\r
+\r
+/**\r
+  Checks APs status and updates APs status if needed.\r
+\r
+**/\r
+VOID\r
+CheckAndUpdateApsStatus (\r
+  VOID\r
+  )\r
+{\r
+  UINTN                   ProcessorNumber;\r
+  EFI_STATUS              Status;\r
+  CPU_MP_DATA             *CpuMpData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  //\r
+  // First, check whether pending StartupAllAPs() exists.\r
+  //\r
+  if (CpuMpData->WaitEvent != NULL) {\r
+\r
+    Status = CheckAllAPs ();\r
+    //\r
+    // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.\r
+    //\r
+    if (Status != EFI_NOT_READY) {\r
+      Status = gBS->SignalEvent (CpuMpData->WaitEvent);\r
+      CpuMpData->WaitEvent = NULL;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Second, check whether pending StartupThisAPs() callings exist.\r
+  //\r
+  for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+\r
+    if (CpuMpData->CpuData[ProcessorNumber].WaitEvent == NULL) {\r
+      continue;\r
+    }\r
+\r
+    Status = CheckThisAP (ProcessorNumber);\r
+\r
+    if (Status != EFI_NOT_READY) {\r
+      gBS->SignalEvent (CpuMpData->CpuData[ProcessorNumber].WaitEvent);\r
+     CpuMpData->CpuData[ProcessorNumber].WaitEvent = NULL;\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Checks APs' status periodically.\r
+\r
+  This function is triggered by timer periodically to check the\r
+  state of APs for StartupAllAPs() and StartupThisAP() executed\r
+  in non-blocking mode.\r
+\r
+  @param[in]  Event    Event triggered.\r
+  @param[in]  Context  Parameter passed with the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+CheckApsStatus (\r
+  IN  EFI_EVENT                           Event,\r
+  IN  VOID                                *Context\r
+  )\r
+{\r
+  //\r
+  // If CheckApsStatus() is not stopped, otherwise return immediately.\r
+  //\r
+  if (!mStopCheckAllApsStatus) {\r
+    CheckAndUpdateApsStatus ();\r
+  }\r
+}\r
+\r
+/**\r
+  Get Protected mode code segment with 16-bit default addressing\r
+  from current GDT table.\r
+\r
+  @return  Protected mode 16-bit code segment value.\r
+**/\r
+UINT16\r
+GetProtectedMode16CS (\r
+  VOID\r
+  )\r
+{\r
+  IA32_DESCRIPTOR          GdtrDesc;\r
+  IA32_SEGMENT_DESCRIPTOR  *GdtEntry;\r
+  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
+  for (Index = 0; Index < GdtEntryCount; Index++) {\r
+    if (GdtEntry->Bits.L == 0) {\r
+      if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.DB == 0) {\r
+        break;\r
+      }\r
+    }\r
+    GdtEntry++;\r
+  }\r
+  ASSERT (Index != GdtEntryCount);\r
+  return Index * 8;\r
+}\r
+\r
+/**\r
+  Get Protected mode code segment from current GDT table.\r
+\r
+  @return  Protected mode code segment value.\r
+**/\r
+UINT16\r
+GetProtectedModeCS (\r
+  VOID\r
+  )\r
+{\r
+  IA32_DESCRIPTOR          GdtrDesc;\r
+  IA32_SEGMENT_DESCRIPTOR  *GdtEntry;\r
+  UINTN                    GdtEntryCount;\r
+  UINT16                   Index;\r
+\r
+  AsmReadGdtr (&GdtrDesc);\r
+  GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
+  GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
+  for (Index = 0; Index < GdtEntryCount; Index++) {\r
+    if (GdtEntry->Bits.L == 0) {\r
+      if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.DB == 1) {\r
+        break;\r
+      }\r
+    }\r
+    GdtEntry++;\r
+  }\r
+  ASSERT (Index != GdtEntryCount);\r
+  return Index * 8;\r
+}\r
+\r
+/**\r
+  Do sync on APs.\r
+\r
+  @param[in, out] Buffer  Pointer to private data buffer.\r
+**/\r
+VOID\r
+EFIAPI\r
+RelocateApLoop (\r
+  IN OUT VOID  *Buffer\r
+  )\r
+{\r
+  CPU_MP_DATA            *CpuMpData;\r
+  BOOLEAN                MwaitSupport;\r
+  ASM_RELOCATE_AP_LOOP   AsmRelocateApLoopFunc;\r
+  UINTN                  ProcessorNumber;\r
+  UINTN                  StackStart;\r
+\r
+  MpInitLibWhoAmI (&ProcessorNumber);\r
+  CpuMpData    = GetCpuMpData ();\r
+  MwaitSupport = IsMwaitSupport ();\r
+  if (CpuMpData->SevEsIsEnabled) {\r
+    StackStart = CpuMpData->SevEsAPResetStackStart;\r
+  } else {\r
+    StackStart = mReservedTopOfApStack;\r
+  }\r
+  AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;\r
+  AsmRelocateApLoopFunc (\r
+    MwaitSupport,\r
+    CpuMpData->ApTargetCState,\r
+    CpuMpData->PmCodeSegment,\r
+    StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
+    (UINTN) &mNumberToFinish,\r
+    CpuMpData->Pm16CodeSegment,\r
+    CpuMpData->SevEsAPBuffer,\r
+    CpuMpData->WakeupBuffer\r
+    );\r
+  //\r
+  // It should never reach here\r
+  //\r
+  ASSERT (FALSE);\r
+}\r
+\r
+/**\r
+  Callback function for ExitBootServices.\r
+\r
+  @param[in]  Event             Event whose notification function is being invoked.\r
+  @param[in]  Context           The pointer to the notification function's context,\r
+                                which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MpInitChangeApLoopCallback (\r
+  IN EFI_EVENT                Event,\r
+  IN VOID                     *Context\r
+  )\r
+{\r
+  CPU_MP_DATA               *CpuMpData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+  CpuMpData->PmCodeSegment = GetProtectedModeCS ();\r
+  CpuMpData->Pm16CodeSegment = GetProtectedMode16CS ();\r
+  CpuMpData->ApLoopMode = PcdGet8 (PcdCpuApLoopMode);\r
+  mNumberToFinish = CpuMpData->CpuCount - 1;\r
+  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);\r
+  while (mNumberToFinish > 0) {\r
+    CpuPause ();\r
+  }\r
+\r
+  if (CpuMpData->SevEsIsEnabled && (CpuMpData->WakeupBuffer != (UINTN) -1)) {\r
+    //\r
+    // There are APs present. Re-use reserved memory area below 1MB from\r
+    // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
+    // in support of booting of the AP by an OS.\r
+    //\r
+    CopyMem (\r
+      (VOID *) CpuMpData->WakeupBuffer,\r
+      (VOID *) (CpuMpData->AddressMap.RendezvousFunnelAddress +\r
+                  CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
+      CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
+      );\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
+}\r
+\r
+/**\r
+  Initialize global data for MP support.\r
+\r
+  @param[in] CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+InitMpGlobalData (\r
+  IN CPU_MP_DATA               *CpuMpData\r
+  )\r
+{\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
+  if (CpuMpData->CpuCount == 1) {\r
+    //\r
+    // If only BSP exists, return\r
+    //\r
+    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
+  // allocate this buffer below 4GB due to APs may be transferred to 32bit\r
+  // protected mode on long mode DXE.\r
+  // Allocating it in advance since memory services are not available in\r
+  // Exit Boot Services callback function.\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
+                   EfiReservedMemoryType,\r
+                   EFI_SIZE_TO_PAGES (ApSafeBufferSize),\r
+                   &Address\r
+                   );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  mReservedApLoopFunc = (VOID *) (UINTN) Address;\r
+  ASSERT (mReservedApLoopFunc != NULL);\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
+    CpuMpData->AddressMap.RelocateApLoopFuncAddress,\r
+    CpuMpData->AddressMap.RelocateApLoopFuncSize\r
+    );\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  CheckApsStatus,\r
+                  NULL,\r
+                  &mCheckAllApsEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Set timer to check all APs status.\r
+  //\r
+  Status = gBS->SetTimer (\r
+                  mCheckAllApsEvent,\r
+                  TimerPeriodic,\r
+                  EFI_TIMER_PERIOD_MICROSECONDS (\r
+                    PcdGet32 (PcdCpuApStatusCheckIntervalInMicroSeconds)\r
+                    )\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
+                  TPL_CALLBACK,\r
+                  MpInitChangeApLoopCallback,\r
+                  NULL,\r
+                  &mMpInitExitBootServicesEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  MpInitChangeApLoopCallback,\r
+                  NULL,\r
+                  &gEfiEventLegacyBootGuid,\r
+                  &mLegacyBootEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
 /**\r
   This service executes a caller provided function on all enabled APs.\r
 \r
                                       EFI_EVENT is defined in CreateEvent() in\r
                                       the Unified Extensible Firmware Interface\r
                                       Specification.\r
-  @param[in]  TimeoutInMicrosecsond   Indicates the time limit in microseconds for\r
+  @param[in]  TimeoutInMicroseconds   Indicates the time limit in microseconds for\r
                                       APs to return from Procedure, either for\r
                                       blocking or non-blocking mode. Zero means\r
                                       infinity.  If the timeout expires before\r
@@ -100,7 +692,29 @@ MpInitLibStartupAllAPs (
   OUT UINTN                     **FailedCpuList         OPTIONAL\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  EFI_STATUS              Status;\r
+\r
+  //\r
+  // Temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllApsStatus = TRUE;\r
+\r
+  Status = StartupAllCPUsWorker (\r
+             Procedure,\r
+             SingleThread,\r
+             TRUE,\r
+             WaitEvent,\r
+             TimeoutInMicroseconds,\r
+             ProcedureArgument,\r
+             FailedCpuList\r
+             );\r
+\r
+  //\r
+  // Start checkAllApsStatus\r
+  //\r
+  mStopCheckAllApsStatus = FALSE;\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -130,7 +744,7 @@ MpInitLibStartupAllAPs (
                                       EFI_EVENT is defined in CreateEvent() in\r
                                       the Unified Extensible Firmware Interface\r
                                       Specification.\r
-  @param[in]  TimeoutInMicrosecsond   Indicates the time limit in microseconds for\r
+  @param[in]  TimeoutInMicroseconds   Indicates the time limit in microseconds for\r
                                       this AP to finish this Procedure, either for\r
                                       blocking or non-blocking mode. Zero means\r
                                       infinity.  If the timeout expires before\r
@@ -185,7 +799,25 @@ MpInitLibStartupThisAP (
   OUT BOOLEAN                   *Finished               OPTIONAL\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  EFI_STATUS              Status;\r
+\r
+  //\r
+  // temporarily stop checkAllApsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllApsStatus = TRUE;\r
+\r
+  Status = StartupThisAPWorker (\r
+             Procedure,\r
+             ProcessorNumber,\r
+             WaitEvent,\r
+             TimeoutInMicroseconds,\r
+             ProcedureArgument,\r
+             Finished\r
+             );\r
+\r
+  mStopCheckAllApsStatus = FALSE;\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -221,7 +853,40 @@ MpInitLibSwitchBSP (
   IN BOOLEAN                   EnableOldBSP\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  EFI_STATUS                   Status;\r
+  EFI_TIMER_ARCH_PROTOCOL      *Timer;\r
+  UINT64                       TimerPeriod;\r
+\r
+  TimerPeriod = 0;\r
+  //\r
+  // Locate Timer Arch Protocol\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **) &Timer);\r
+  if (EFI_ERROR (Status)) {\r
+    Timer = NULL;\r
+  }\r
+\r
+  if (Timer != NULL) {\r
+    //\r
+    // Save current rate of DXE Timer\r
+    //\r
+    Timer->GetTimerPeriod (Timer, &TimerPeriod);\r
+    //\r
+    // Disable DXE Timer and drain pending interrupts\r
+    //\r
+    Timer->SetTimerPeriod (Timer, 0);\r
+  }\r
+\r
+  Status = SwitchBSPWorker (ProcessorNumber, EnableOldBSP);\r
+\r
+  if (Timer != NULL) {\r
+    //\r
+    // Enable and restore rate of DXE Timer\r
+    //\r
+    Timer->SetTimerPeriod (Timer, TimerPeriod);\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -262,5 +927,47 @@ MpInitLibEnableDisableAP (
   IN  UINT32                    *HealthFlag OPTIONAL\r
   )\r
 {\r
+  EFI_STATUS     Status;\r
+  BOOLEAN        TempStopCheckState;\r
+\r
+  TempStopCheckState = FALSE;\r
+  //\r
+  // temporarily stop checkAllAPsStatus for initialize parameters.\r
+  //\r
+  if (!mStopCheckAllApsStatus) {\r
+    mStopCheckAllApsStatus = TRUE;\r
+    TempStopCheckState     = TRUE;\r
+  }\r
+\r
+  Status = EnableDisableApWorker (ProcessorNumber, EnableAP, HealthFlag);\r
+\r
+  if (TempStopCheckState) {\r
+    mStopCheckAllApsStatus = FALSE;\r
+  }\r
+\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, out] 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