]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg: Update CPU MP drivers to support single CPU configuration
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index 3e30d7417d4f0ea88739911544cfb2984e30ba41..da3686e2788e8c50f0652bd743a3a010c42e5a2e 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   CPU DXE Module.\r
 \r
-  Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2015, 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
@@ -20,11 +20,14 @@ UINTN gApStackSize;
 UINTN gPollInterval = 100; // 100 microseconds\r
 \r
 MP_SYSTEM_DATA mMpSystemData;\r
+EFI_HANDLE     mMpServiceHandle       = NULL;\r
+EFI_EVENT      mExitBootServicesEvent = (EFI_EVENT)NULL;\r
 \r
 VOID *mCommonStack = 0;\r
 VOID *mTopOfApCommonStack = 0;\r
 VOID *mApStackStart = 0;\r
 \r
+volatile BOOLEAN mAPsAlreadyInitFinished = FALSE;\r
 volatile BOOLEAN mStopCheckAllAPsStatus = TRUE;\r
 \r
 EFI_MP_SERVICES_PROTOCOL  mMpServicesTemplate = {\r
@@ -51,6 +54,7 @@ GetMpSpinLock (
   while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
     CpuPause ();\r
   }\r
+  CpuData->LockSelf = GetApicId ();\r
 }\r
 \r
 /**\r
@@ -174,7 +178,7 @@ TestCpuStatusFlag (
   Ret = CpuData->Info.StatusFlag & Flags;\r
   ReleaseMpSpinLock (CpuData);\r
 \r
-  return !!(Ret);\r
+  return (BOOLEAN) (Ret != 0);\r
 }\r
 \r
 /**\r
@@ -294,9 +298,15 @@ CheckAndUpdateAllAPsToIdleState (
           SetApProcedure (&mMpSystemData.CpuDatas[NextNumber],\r
                           mMpSystemData.Procedure,\r
                           mMpSystemData.ProcedureArgument);\r
+          //\r
+          // If this AP previous state is blocked, we should\r
+          // wake up this AP by sent a SIPI. and avoid\r
+          // re-involve the sleeping state. we must call\r
+          // SetApProcedure() first.\r
+          //\r
+          ResetProcessorToIdleState (&mMpSystemData.CpuDatas[NextNumber]);\r
         }\r
       }\r
-\r
       SetApState (CpuData, CpuStateIdle);\r
     }\r
   }\r
@@ -339,7 +349,8 @@ ResetAllFailedAPs (
     }\r
 \r
     CpuState = GetApState (CpuData);\r
-    if (CpuState != CpuStateIdle) {\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
       if (mMpSystemData.FailedList != NULL) {\r
         (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number;\r
       }\r
@@ -611,6 +622,7 @@ StartupAllAPs (
   CPU_DATA_BLOCK        *CpuData;\r
   UINTN                 Number;\r
   CPU_STATE             APInitialState;\r
+  CPU_STATE             CpuState;\r
 \r
   CpuData = NULL;\r
 \r
@@ -630,6 +642,11 @@ StartupAllAPs (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
     CpuData = &mMpSystemData.CpuDatas[Number];\r
     if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
@@ -646,21 +663,18 @@ StartupAllAPs (
       continue;\r
     }\r
 \r
-    if (GetApState (CpuData) != CpuStateIdle) {\r
+    CpuState = GetApState (CpuData);\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
       return EFI_NOT_READY;\r
     }\r
   }\r
 \r
-  //\r
-  // temporarily stop checkAllAPsStatus for initialize parameters.\r
-  //\r
-  mStopCheckAllAPsStatus = TRUE;\r
-\r
   mMpSystemData.Procedure         = Procedure;\r
   mMpSystemData.ProcedureArgument = ProcedureArgument;\r
   mMpSystemData.WaitEvent         = WaitEvent;\r
   mMpSystemData.Timeout           = TimeoutInMicroseconds;\r
-  mMpSystemData.TimeoutActive     = !!(TimeoutInMicroseconds);\r
+  mMpSystemData.TimeoutActive     = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
   mMpSystemData.FinishCount       = 0;\r
   mMpSystemData.StartCount        = 0;\r
   mMpSystemData.SingleThread      = SingleThread;\r
@@ -690,13 +704,24 @@ StartupAllAPs (
     // state 1 by 1, until the previous 1 finished its task\r
     // if not "SingleThread", all APs are put to ready state from the beginning\r
     //\r
-    if (GetApState (CpuData) == CpuStateIdle) {\r
+    CpuState = GetApState (CpuData);\r
+    if (CpuState == CpuStateIdle ||\r
+        CpuState == CpuStateSleeping) {\r
       mMpSystemData.StartCount++;\r
 \r
       SetApState (CpuData, APInitialState);\r
 \r
       if (APInitialState == CpuStateReady) {\r
         SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+        //\r
+        // If this AP previous state is Sleeping, we should\r
+        // wake up this AP by sent a SIPI. and avoid\r
+        // re-involve the sleeping state. we must call\r
+        // SetApProcedure() first.\r
+        //\r
+        if (CpuState == CpuStateSleeping) {\r
+          ResetProcessorToIdleState (CpuData);\r
+        }\r
       }\r
 \r
       if (SingleThread) {\r
@@ -714,6 +739,11 @@ StartupAllAPs (
     return EFI_SUCCESS;\r
   }\r
 \r
+  //\r
+  // Blocking temporarily stop CheckAllAPsStatus()\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   while (TRUE) {\r
     CheckAndUpdateAllAPsToIdleState ();\r
     if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {\r
@@ -838,6 +868,7 @@ StartupThisAP (
   )\r
 {\r
   CPU_DATA_BLOCK        *CpuData;\r
+  CPU_STATE             CpuState;\r
 \r
   CpuData = NULL;\r
 \r
@@ -857,28 +888,39 @@ StartupThisAP (
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
   if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
       !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (GetApState (CpuData) != CpuStateIdle) {\r
+  CpuState = GetApState (CpuData);\r
+  if (CpuState != CpuStateIdle &&\r
+      CpuState != CpuStateSleeping) {\r
     return EFI_NOT_READY;\r
   }\r
 \r
-  //\r
-  // temporarily stop checkAllAPsStatus for initialize parameters.\r
-  //\r
-  mStopCheckAllAPsStatus = TRUE;\r
-\r
   SetApState (CpuData, CpuStateReady);\r
 \r
   SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+  //\r
+  // If this AP previous state is Sleeping, we should\r
+  // wake up this AP by sent a SIPI. and avoid\r
+  // re-involve the sleeping state. we must call\r
+  // SetApProcedure() first.\r
+  //\r
+  if (CpuState == CpuStateSleeping) {\r
+    ResetProcessorToIdleState (CpuData);\r
+  }\r
 \r
   CpuData->Timeout = TimeoutInMicroseconds;\r
   CpuData->WaitEvent = WaitEvent;\r
-  CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
+  CpuData->TimeoutActive = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
   CpuData->Finished = Finished;\r
 \r
   mStopCheckAllAPsStatus = FALSE;\r
@@ -1011,6 +1053,11 @@ EnableDisableAP (
   )\r
 {\r
   CPU_DATA_BLOCK *CpuData;\r
+  BOOLEAN        TempStopCheckState;\r
+  CPU_STATE      CpuState;\r
+\r
+  CpuData = NULL;\r
+  TempStopCheckState = FALSE;\r
 \r
   if (!IsBSP ()) {\r
     return EFI_DEVICE_ERROR;\r
@@ -1020,12 +1067,22 @@ EnableDisableAP (
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for initialize parameters.\r
+  //\r
+  if (!mStopCheckAllAPsStatus) {\r
+    mStopCheckAllAPsStatus = TRUE;\r
+    TempStopCheckState = TRUE;\r
+  }\r
+\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
   if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (GetApState (CpuData) != CpuStateIdle) {\r
+  CpuState = GetApState (CpuData);\r
+  if (CpuState != CpuStateIdle &&\r
+      CpuState != CpuStateSleeping) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1046,6 +1103,10 @@ EnableDisableAP (
     CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
   }\r
 \r
+  if (TempStopCheckState) {\r
+    mStopCheckAllAPsStatus = FALSE;\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -1112,6 +1173,7 @@ ResetProcessorToIdleState (
   IN CPU_DATA_BLOCK  *CpuData\r
   )\r
 {\r
+  ResetApStackless ((UINT32)CpuData->Info.ProcessorId);\r
 }\r
 \r
 /**\r
@@ -1131,12 +1193,34 @@ ProcessorToIdleState (
   UINTN                 ProcessorNumber;\r
   CPU_DATA_BLOCK        *CpuData;\r
   EFI_AP_PROCEDURE      Procedure;\r
-  VOID                  *ProcedureArgument;\r
+  volatile VOID         *ProcedureArgument;\r
+\r
+  AsmApDoneWithCommonStack ();\r
+\r
+  while (!mAPsAlreadyInitFinished) {\r
+    CpuPause ();\r
+  }\r
 \r
   WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
 \r
-  AsmApDoneWithCommonStack ();\r
+  //\r
+  // Avoid forcibly reset AP caused the AP got lock not release.\r
+  //\r
+  if (CpuData->LockSelf == (INTN) GetApicId ()) {\r
+    ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  }\r
+\r
+  //\r
+  // Avoid forcibly reset AP caused the timeout AP State is not\r
+  // updated.\r
+  //\r
+  GetMpSpinLock (CpuData);\r
+  if (CpuData->State == CpuStateBusy) {\r
+    CpuData->Procedure = NULL;\r
+  }\r
+  CpuData->State = CpuStateIdle;\r
+  ReleaseMpSpinLock (CpuData);\r
 \r
   while (TRUE) {\r
     GetMpSpinLock (CpuData);\r
@@ -1145,12 +1229,28 @@ ProcessorToIdleState (
     ReleaseMpSpinLock (CpuData);\r
 \r
     if (Procedure != NULL) {\r
-      Procedure (ProcedureArgument);\r
+      SetApState (CpuData, CpuStateBusy);\r
+\r
+      Procedure ((VOID*) ProcedureArgument);\r
 \r
       GetMpSpinLock (CpuData);\r
       CpuData->Procedure = NULL;\r
       CpuData->State = CpuStateFinished;\r
       ReleaseMpSpinLock (CpuData);\r
+    } else {\r
+      //\r
+      // if no procedure to execution, we simply put AP\r
+      // into sleeping state, and waiting BSP sent SIPI.\r
+      //\r
+      GetMpSpinLock (CpuData);\r
+      if (CpuData->State == CpuStateIdle) {\r
+          CpuData->State = CpuStateSleeping;\r
+      }\r
+      ReleaseMpSpinLock (CpuData);\r
+    }\r
+\r
+    if (GetApState (CpuData) == CpuStateSleeping) {\r
+      CpuSleep ();\r
     }\r
 \r
     CpuPause ();\r
@@ -1281,20 +1381,6 @@ CheckAllAPsStatus (
   //\r
   for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
     CpuData = &mMpSystemData.CpuDatas[Number];\r
-    if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
-      //\r
-      // Skip BSP\r
-      //\r
-      continue;\r
-    }\r
-\r
-    if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
-      //\r
-      // Skip Disabled processors\r
-      //\r
-      continue;\r
-    }\r
-\r
     if (CpuData->WaitEvent) {\r
       CheckThisAPStatus (NULL, (VOID *)CpuData);\r
     }\r
@@ -1319,13 +1405,27 @@ ApEntryPointInC (
   VOID\r
   )\r
 {\r
-  VOID* TopOfApStack;\r
+  VOID*           TopOfApStack;\r
+  UINTN           ProcessorNumber;\r
 \r
-  FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
-  TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
-  mApStackStart = TopOfApStack;\r
+  if (!mAPsAlreadyInitFinished) {\r
+    FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
+    TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
+    mApStackStart = TopOfApStack;\r
 \r
-  mMpSystemData.NumberOfProcessors++;\r
+    //\r
+    // Store the Stack address, when reset the AP, We can found the original address.\r
+    //\r
+    mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack;\r
+    mMpSystemData.NumberOfProcessors++;\r
+    mMpSystemData.NumberOfEnabledProcessors++;\r
+  } else {\r
+    WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
+    //\r
+    // Get the original stack address.\r
+    //\r
+    TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack;\r
+  }\r
 \r
   SwitchStack (\r
     (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
@@ -1362,11 +1462,12 @@ FillInProcessorInformation (
   CpuData->Info.Location.Package = ProcessorId;\r
   CpuData->Info.Location.Core    = 0;\r
   CpuData->Info.Location.Thread  = 0;\r
-  CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
+  CpuData->State = Bsp ? CpuStateBusy : CpuStateIdle;\r
 \r
   CpuData->Procedure        = NULL;\r
   CpuData->Parameter        = NULL;\r
   InitializeSpinLock (&CpuData->CpuDataLock);\r
+  CpuData->LockSelf         = -1;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1419,6 +1520,111 @@ InitMpSystemData (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Collects BIST data from HOB.\r
+\r
+  This function collects BIST data from HOB built from Sec Platform Information\r
+  PPI or SEC Platform Information2 PPI.\r
+\r
+**/\r
+VOID\r
+CollectBistDataFromHob (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE                     *GuidHob;\r
+  EFI_SEC_PLATFORM_INFORMATION_RECORD2  *SecPlatformInformation2;\r
+  EFI_SEC_PLATFORM_INFORMATION_RECORD   *SecPlatformInformation;\r
+  UINTN                                 NumberOfData;\r
+  EFI_SEC_PLATFORM_INFORMATION_CPU      *CpuInstance;\r
+  EFI_SEC_PLATFORM_INFORMATION_CPU      BspCpuInstance;\r
+  UINTN                                 ProcessorNumber;\r
+  UINT32                                InitialLocalApicId;\r
+  CPU_DATA_BLOCK                        *CpuData;\r
+\r
+  SecPlatformInformation2 = NULL;\r
+  SecPlatformInformation  = NULL;\r
+\r
+  //\r
+  // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly\r
+  //\r
+  GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);\r
+  if (GuidHob != NULL) {\r
+    //\r
+    // Sec Platform Information2 PPI includes BSP/APs' BIST information\r
+    //\r
+    SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);\r
+    NumberOfData = SecPlatformInformation2->NumberOfCpus;\r
+    CpuInstance  = SecPlatformInformation2->CpuInstance;\r
+  } else {\r
+    //\r
+    // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB\r
+    //\r
+    GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);\r
+    if (GuidHob != NULL) {\r
+      SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);\r
+      NumberOfData = 1;\r
+      //\r
+      // SEC Platform Information only includes BSP's BIST information\r
+      // does not have BSP's APIC ID\r
+      //\r
+      BspCpuInstance.CpuLocation = GetApicId ();\r
+      BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32  = SecPlatformInformation->IA32HealthFlags.Uint32;\r
+      CpuInstance = &BspCpuInstance;\r
+    } else {\r
+      DEBUG ((EFI_D_INFO, "Does not find any HOB stored CPU BIST information!\n"));\r
+      //\r
+      // Does not find any HOB stored BIST information\r
+      //\r
+      return;\r
+    }\r
+  }\r
+\r
+  while ((NumberOfData--) > 0) {\r
+    for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) {\r
+      CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+      InitialLocalApicId = (UINT32) CpuData->Info.ProcessorId;\r
+      if (InitialLocalApicId == CpuInstance[NumberOfData].CpuLocation) {\r
+        //\r
+        // Update CPU health status for MP Services Protocol according to BIST data.\r
+        //\r
+        if (CpuInstance[NumberOfData].InfoRecord.IA32HealthFlags.Uint32 != 0) {\r
+          CpuData->Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;\r
+          //\r
+          // Report Status Code that self test is failed\r
+          //\r
+          REPORT_STATUS_CODE (\r
+            EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
+            (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)\r
+            );\r
+        }\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Callback function for ExitBootServices.\r
+\r
+  @param  Event                 Event whose notification function is being invoked.\r
+  @param  Context               The pointer to the notification function's context,\r
+                                which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ExitBootServicesCallback (\r
+  IN EFI_EVENT                Event,\r
+  IN VOID                     *Context\r
+  )\r
+{\r
+  //\r
+  // Avoid APs access invalid buff datas which allocated by BootServices,\r
+  // so we send INIT IPI to APs to let them wait for SIPI state.\r
+  //\r
+  SendInitIpiAllExcludingSelf ();\r
+}\r
+\r
 /**\r
   Initialize Multi-processor support.\r
 \r
@@ -1428,40 +1634,83 @@ InitializeMpSupport (
   VOID\r
   )\r
 {\r
+  EFI_STATUS Status;\r
+\r
   gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
   if (gMaxLogicalProcessorNumber < 1) {\r
     DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
     return;\r
   }\r
 \r
-  if (gMaxLogicalProcessorNumber == 1) {\r
-    return;\r
-  }\r
 \r
-  gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
-  ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
 \r
-  mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
-  ASSERT (mApStackStart != NULL);\r
+  InitMpSystemData ();\r
 \r
   //\r
-  // the first buffer of stack size used for common stack, when the amount of AP\r
-  // more than 1, we should never free the common stack which maybe used for AP reset.\r
+  // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1\r
   //\r
-  mCommonStack = mApStackStart;\r
-  mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
-  mApStackStart = mTopOfApCommonStack;\r
+  if (gMaxLogicalProcessorNumber > 1) {\r
 \r
-  InitMpSystemData ();\r
+    gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
+    ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
+\r
+    mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    ASSERT (mApStackStart != NULL);\r
+\r
+    //\r
+    // the first buffer of stack size used for common stack, when the amount of AP\r
+    // more than 1, we should never free the common stack which maybe used for AP reset.\r
+    //\r
+    mCommonStack = mApStackStart;\r
+    mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
+    mApStackStart = mTopOfApCommonStack;\r
+\r
+    PrepareAPStartupCode ();\r
 \r
+    StartApsStackless ();\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mMpSystemData.NumberOfProcessors));\r
   if (mMpSystemData.NumberOfProcessors == 1) {\r
-    FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
-    return;\r
+    FreeApStartupCode ();\r
+    if (mCommonStack != NULL) {\r
+      FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    }\r
   }\r
 \r
-  if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
-    FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
-                                (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
-                                gApStackSize));\r
+  mMpSystemData.CpuDatas = ReallocatePool (\r
+                             sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber,\r
+                             sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,\r
+                             mMpSystemData.CpuDatas);\r
+\r
+  mAPsAlreadyInitFinished = TRUE;\r
+\r
+  //\r
+  // Update CPU healthy information from Guided HOB\r
+  //\r
+  CollectBistDataFromHob ();\r
+\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mMpServiceHandle,\r
+                  &gEfiMpServiceProtocolGuid,  &mMpServicesTemplate,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (mMpSystemData.NumberOfProcessors > 1 && mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
+    if (mApStackStart != NULL) {\r
+      FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
+                                  (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
+                                  gApStackSize));\r
+    }\r
   }\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
+                  TPL_CALLBACK,\r
+                  ExitBootServicesCallback,\r
+                  NULL,\r
+                  &mExitBootServicesEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r