]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
Update PEI/DXE/SMM dispatchers to include DEBUG ((DEBUG_DISPATCH, )) macros to log...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
index 455c79b1bb49298c33f8782b328d0d8fc694dc48..3dd7549df88265c7dbabed3ead761001352d9768 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   EFI PEI Core dispatch services\r
   \r
-Copyright (c) 2006 - 2010, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2010, 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
@@ -183,63 +183,57 @@ DiscoverPeimsAndOrderWithApriori (
 \r
 }\r
 \r
+//\r
+// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
+// This part of memory still need reserved on the very top of memory so that the DXE Core could  \r
+// use these memory for data initialization. This macro should be sync with the same marco\r
+// defined in DXE Core.\r
+//\r
+#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 /**\r
-  Shadow PeiCore module from flash to installed memory.\r
+  This function is to test if the memory range described in resource HOB is available or not. \r
   \r
-  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
-  @param PrivateInMem    PeiCore's private data structure\r
+  This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the \r
+  memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is\r
+  available or not.\r
 \r
-  @return PeiCore function address after shadowing.\r
+  @param PrivateData         Pointer to the private data passed in from caller\r
+  @param ResourceHob         Pointer to a resource HOB which described the memory range described by the input resource HOB\r
 **/\r
-VOID*\r
-ShadowPeiCore(\r
-  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
-  IN       PEI_CORE_INSTANCE    *PrivateInMem\r
+BOOLEAN\r
+PeiLoadFixAddressIsMemoryRangeAvailable (\r
+  IN PEI_CORE_INSTANCE                  *PrivateData,\r
+  IN EFI_HOB_RESOURCE_DESCRIPTOR        *ResourceHob\r
   )\r
 {\r
-  EFI_PEI_FILE_HANDLE  PeiCoreFileHandle;\r
-  EFI_PHYSICAL_ADDRESS EntryPoint;\r
-  EFI_STATUS           Status;\r
-  UINT32               AuthenticationState;\r
-\r
-  PeiCoreFileHandle = NULL;\r
-\r
-  //\r
-  // Find the PEI Core in the BFV\r
-  //\r
-  Status = PrivateInMem->Fv[0].FvPpi->FindFileByType (\r
-                                        PrivateInMem->Fv[0].FvPpi,\r
-                                        EFI_FV_FILETYPE_PEI_CORE,\r
-                                        PrivateInMem->Fv[0].FvHandle,\r
-                                        &PeiCoreFileHandle\r
-                                        );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Shadow PEI Core into memory so it will run faster\r
+       EFI_HOB_MEMORY_ALLOCATION          *MemoryHob;\r
+       BOOLEAN                             IsAvailable;\r
+       EFI_PEI_HOB_POINTERS                Hob;\r
+       \r
+  IsAvailable = TRUE;\r
+       if (PrivateData == NULL || ResourceHob == NULL) {\r
+         return FALSE;\r
+       }\r
+       //\r
+  // test if the memory range describe in the HOB is already allocated.\r
   //\r
-  Status = PeiLoadImage (\r
-              PeiServices,\r
-              *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),\r
-              PEIM_STATE_REGISITER_FOR_SHADOW,\r
-              &EntryPoint,\r
-              &AuthenticationState\r
-              );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  //\r
-  // Compute the PeiCore's function address after shaowed PeiCore.\r
-  // _ModuleEntryPoint is PeiCore main function entry\r
-  //\r
-  return (VOID*) ((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);\r
+  for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+    //                                                              \r
+    // See if this is a memory allocation HOB                     \r
+    //\r
+    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) { \r
+      MemoryHob = Hob.MemoryAllocation;\r
+      if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart && \r
+         MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {\r
+         IsAvailable = FALSE;\r
+         break;  \r
+       }\r
+     }\r
+  }\r
+  \r
+  return IsAvailable;\r
+       \r
 }\r
-//\r
-// This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
-// This part of memory still need reserved on the very top of memory so that the DXE Core could  \r
-// use these memory for data initialization. This macro should be sync with the same marco\r
-// defined in DXE Core.\r
-//\r
-#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 /**\r
   Hook function for Loading Module at Fixed Address feature\r
   \r
@@ -269,6 +263,7 @@ PeiLoadFixAddressHook(
   EFI_PEI_HOB_POINTERS               NextHob;\r
   EFI_PHYSICAL_ADDRESS               MaxMemoryBaseAddress;\r
   UINT64                             MaxMemoryLength;\r
+  EFI_HOB_MEMORY_ALLOCATION          *MemoryHob;\r
   //\r
   // Initialize Local Variables\r
   //\r
@@ -361,6 +356,61 @@ PeiLoadFixAddressHook(
       }\r
     } \r
   }\r
+  //\r
+  // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe\r
+  //  the allocated memory range\r
+  //\r
+  for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+    //                                                              \r
+    // See if this is a memory allocation HOB                     \r
+    //\r
+    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
+      MemoryHob = Hob.MemoryAllocation;\r
+      for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
+        //\r
+        // See if this is a resource descriptor HOB\r
+        //\r
+        if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+               NextResourceHob = NextHob.ResourceDescriptor;\r
+          //\r
+          // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
+          //\r
+          if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) {\r
+            continue;\r
+          }\r
+          //\r
+          // If the range describe in memory allocation HOB  belongs to the memroy range described by the resource hob\r
+          //          \r
+          if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart && \r
+              MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
+             //\r
+             // Build seperate resource hob for this allocated range\r
+             //                     \r
+             if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {\r
+               BuildResourceDescriptorHob (\r
+                 EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+                 NextResourceHob->ResourceAttribute,\r
+                 NextResourceHob->PhysicalStart,                             \r
+                 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)      \r
+               );\r
+             }\r
+             if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
+               BuildResourceDescriptorHob (\r
+                 EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+                 NextResourceHob->ResourceAttribute,\r
+                 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,                            \r
+                 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))    \r
+               );\r
+             }\r
+             NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+             NextResourceHob->ResourceLength = MemoryHob->AllocDescriptor.MemoryLength;\r
+             break;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   //\r
   // Try to find and validate the TOP address.\r
   //  \r
@@ -396,11 +446,12 @@ PeiLoadFixAddressHook(
             // See if Top address specified by user is valid.\r
             //\r
             if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress && \r
-                (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress) {\r
+                (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress && \r
+                PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
               CurrentResourceHob = ResourceHob; \r
               CurrentHob = Hob;\r
               break;\r
-            }\r
+           }\r
         }\r
       }  \r
     }  \r
@@ -428,7 +479,7 @@ PeiLoadFixAddressHook(
               //\r
               // See if Top address specified by user is valid.\r
               //\r
-              if (ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
+              if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
                  DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",  \r
                           (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE), \r
                           (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE) \r
@@ -440,7 +491,8 @@ PeiLoadFixAddressHook(
       //\r
       // Assert here \r
       //\r
-      ASSERT (FALSE);      \r
+      ASSERT (FALSE);   \r
+      return;   \r
     }     \r
   } else {\r
     //\r
@@ -462,7 +514,7 @@ PeiLoadFixAddressHook(
         //\r
         if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && \r
             ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
-            ResourceHob->ResourceLength > TotalReservedMemorySize) {\r
+            ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
           //\r
           // See if this is the highest largest system memory region below MaxAddress\r
           //\r
@@ -479,7 +531,8 @@ PeiLoadFixAddressHook(
       //\r
       // Assert here \r
       //\r
-      ASSERT (FALSE);      \r
+      ASSERT (FALSE);\r
+      return;  \r
     } else {\r
       TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ; \r
     }         \r
@@ -487,10 +540,10 @@ PeiLoadFixAddressHook(
   \r
   if (CurrentResourceHob != NULL) {\r
     //\r
-    // rebuild hob for PEI memmory and reserved memory\r
+    // rebuild resource HOB for PEI memmory and reserved memory\r
     //\r
     BuildResourceDescriptorHob (\r
-      EFI_RESOURCE_SYSTEM_MEMORY,                       // MemoryType,\r
+      EFI_RESOURCE_SYSTEM_MEMORY,                       \r
       (\r
       EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
       EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
@@ -500,15 +553,15 @@ PeiLoadFixAddressHook(
       EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
       EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
       ),\r
-      (TopLoadingAddress - TotalReservedMemorySize),                             // MemoryBegin\r
-      TotalReservedMemorySize      // MemoryLength\r
+      (TopLoadingAddress - TotalReservedMemorySize),                             \r
+      TotalReservedMemorySize     \r
     );\r
     //\r
-    // rebuild hob for the remain memory if necessary\r
+    // rebuild resource for the remain memory if necessary\r
     //\r
     if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
       BuildResourceDescriptorHob (\r
-        EFI_RESOURCE_SYSTEM_MEMORY,                       // MemoryType,\r
+        EFI_RESOURCE_SYSTEM_MEMORY,                       \r
         (\r
          EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
          EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
@@ -517,8 +570,8 @@ PeiLoadFixAddressHook(
          EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
          EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
          ),\r
-         CurrentResourceHob->PhysicalStart,                             // MemoryBegin\r
-         (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)      // MemoryLength\r
+         CurrentResourceHob->PhysicalStart,                             \r
+         (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)      \r
        );\r
     }\r
     if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength  > TopLoadingAddress ) {\r
@@ -582,19 +635,16 @@ PeiDispatcher (
   UINTN                               SaveCurrentFvCount;\r
   EFI_PEI_FILE_HANDLE                 SaveCurrentFileHandle;\r
   PEIM_FILE_HANDLE_EXTENDED_DATA      ExtendedData;\r
-  EFI_PHYSICAL_ADDRESS                NewPermenentMemoryBase;\r
   TEMPORARY_RAM_SUPPORT_PPI           *TemporaryRamSupportPpi;\r
-  EFI_HOB_HANDOFF_INFO_TABLE          *OldHandOffTable;\r
-  EFI_HOB_HANDOFF_INFO_TABLE          *NewHandOffTable;\r
-  INTN                                StackOffset;\r
-  INTN                                HeapOffset;\r
-  PEI_CORE_INSTANCE                   *PrivateInMem;\r
-  UINT64                              NewPeiStackSize;\r
-  UINT64                              OldPeiStackSize;\r
-  UINT64                              StackGap;\r
+  UINT64                              NewStackSize;\r
+  EFI_PHYSICAL_ADDRESS                TopOfNewStack;\r
+  EFI_PHYSICAL_ADDRESS                TopOfOldStack;\r
+  EFI_PHYSICAL_ADDRESS                TemporaryRamBase;\r
+  UINTN                               TemporaryRamSize;\r
+  EFI_PHYSICAL_ADDRESS                TemporaryStackSize;\r
+  UINTN                               StackOffset;\r
+  BOOLEAN                             StackOffsetPositive;\r
   EFI_FV_FILE_INFO                    FvFileInfo;\r
-  UINTN                               OldCheckingTop;\r
-  UINTN                               OldCheckingBottom;\r
   PEI_CORE_FV_HANDLE                  *CoreFvHandle;\r
   VOID                                *LoadFixPeiCodeBegin;\r
 \r
@@ -786,13 +836,14 @@ PeiDispatcher (
                      && (*StackPointer == INIT_CAR_VALUE);\r
                      StackPointer ++);\r
                      \r
+                DEBUG ((EFI_D_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
+                DEBUG ((EFI_D_INFO, "Temp Heap  : BaseAddress=0x%p Length=0x%X\n", Private->HobList.Raw, (UINT32)((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN) Private->HobList.Raw)));\r
                 DEBUG ((EFI_D_INFO, "Total temporary memory:    %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
                 DEBUG ((EFI_D_INFO, "  temporary memory stack ever used: %d bytes.\n",\r
-                       (SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
+                       (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
                       ));\r
                 DEBUG ((EFI_D_INFO, "  temporary memory heap used:       %d bytes.\n",\r
-                       ((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom -\r
-                       (UINTN) Private->HobList.Raw)\r
+                       (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
                       ));\r
               DEBUG_CODE_END ();\r
               \r
@@ -800,10 +851,10 @@ PeiDispatcher (
                 //\r
                 // Loading Module at Fixed Address is enabled\r
                 //\r
-                PeiLoadFixAddressHook(Private);\r
+                PeiLoadFixAddressHook (Private);\r
+\r
                 //\r
-                // if Loading Module at Fixed Address is enabled, This is the first invoke to page\r
-                // allocation for Pei Code range. This memory range should be reserved for loading PEIMs\r
+                // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.\r
                 //\r
                 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
                 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PeiCodeBegin = 0x%lX, PeiCodeTop= 0x%lX\n", (UINT64)(UINTN)LoadFixPeiCodeBegin, (UINT64)((UINTN)LoadFixPeiCodeBegin + PcdGet32(PcdLoadFixAddressPeiCodePageNumber) * EFI_PAGE_SIZE)));\r
@@ -812,170 +863,104 @@ PeiDispatcher (
               //\r
               // Reserve the size of new stack at bottom of physical memory\r
               //\r
-              OldPeiStackSize = (UINT64) SecCoreData->StackSize;\r
-              NewPeiStackSize = (RShiftU64 (Private->PhysicalMemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
-              if (PcdGet32(PcdPeiCoreMaxPeiStackSize) > (UINT32) NewPeiStackSize) {\r
-                Private->StackSize = NewPeiStackSize;\r
-              } else {\r
-                Private->StackSize = PcdGet32(PcdPeiCoreMaxPeiStackSize);\r
-              }\r
-\r
-              //\r
-              // In theory, the size of new stack in permenent memory should large than\r
-              // size of old stack in temporary memory.\r
+              // The size of new stack in permenent memory must be the same size \r
+              // or larger than the size of old stack in temporary memory.\r
               // But if new stack is smaller than the size of old stack, we also reserve\r
               // the size of old stack at bottom of permenent memory.\r
               //\r
-              DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (INT32) OldPeiStackSize, (INT32) Private->StackSize));\r
-              ASSERT (Private->StackSize >= OldPeiStackSize);\r
-              StackGap = Private->StackSize - OldPeiStackSize;\r
+              NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1);\r
+              NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE);\r
+              NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize);\r
+              DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize));\r
+              ASSERT (NewStackSize >= SecCoreData->StackSize);\r
 \r
               //\r
-              // Update HandOffHob for new installed permenent memory\r
+              // Caculate stack offset and heap offset between temporary memory and new permement \r
+              // memory seperately.\r
               //\r
-              OldHandOffTable   = Private->HobList.HandoffInformationTable;\r
-              OldCheckingBottom = (UINTN)(SecCoreData->TemporaryRamBase);\r
-              OldCheckingTop    = (UINTN)(OldCheckingBottom + SecCoreData->TemporaryRamSize);\r
+              TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;\r
+              TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize;\r
+              if (TopOfNewStack >= (UINTN)SecCoreData->PeiTemporaryRamBase) {\r
+                Private->HeapOffsetPositive = TRUE;\r
+                Private->HeapOffset = (UINTN)(TopOfNewStack - (UINTN)SecCoreData->PeiTemporaryRamBase);\r
+              } else {\r
+                Private->HeapOffsetPositive = FALSE;\r
+                Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - TopOfNewStack);\r
+              }\r
+              if (TopOfNewStack >= TopOfOldStack) {\r
+                StackOffsetPositive = TRUE;\r
+                StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack);\r
+              } else {\r
+                StackOffsetPositive = FALSE;\r
+                StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack);\r
+              }\r
+\r
+              DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64)Private->HeapOffset, (UINT64)(StackOffset)));\r
 \r
               //\r
-              // The whole temporary memory will be migrated to physical memory.\r
-              // CAUTION: The new base is computed accounding to gap of new stack.\r
+              // Build Stack HOB that describes the permanent memory stack\r
               //\r
-              NewPermenentMemoryBase = Private->PhysicalMemoryBegin + StackGap;\r
-              \r
+              DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize));\r
+              BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize);\r
+\r
               //\r
-              // Caculate stack offset and heap offset between temporary memory and new permement \r
-              // memory seperately.\r
+              // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address\r
               //\r
-              StackOffset            = (UINTN) NewPermenentMemoryBase - (UINTN) SecCoreData->StackBase;\r
-              HeapOffset             = (INTN) ((UINTN) Private->PhysicalMemoryBegin + Private->StackSize - \\r
-                                               (UINTN) SecCoreData->PeiTemporaryRamBase);\r
-              DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (INT64)HeapOffset, (INT64)StackOffset));\r
-              \r
+              TemporaryRamBase   = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase;\r
+              TemporaryRamSize   = SecCoreData->TemporaryRamSize;\r
+              TemporaryStackSize = SecCoreData->StackSize;\r
+\r
               //\r
-              // Caculate new HandOffTable and PrivateData address in permenet memory's stack\r
+              // Caculate new HandOffTable and PrivateData address in permanent memory's stack\r
               //\r
-              NewHandOffTable        = (EFI_HOB_HANDOFF_INFO_TABLE *)((UINTN)OldHandOffTable + HeapOffset);\r
-              PrivateInMem           = (PEI_CORE_INSTANCE *)((UINTN) (VOID*) Private + StackOffset);\r
+              if (StackOffsetPositive) {\r
+                SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset);\r
+                Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset);\r
+              } else {\r
+                SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset);\r
+                Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset);\r
+              }\r
 \r
               //\r
               // TemporaryRamSupportPpi is produced by platform's SEC\r
               //\r
-              Status = PeiLocatePpi (\r
-                         (CONST EFI_PEI_SERVICES **) PeiServices,\r
+              Status = PeiServicesLocatePpi (\r
                          &gEfiTemporaryRamSupportPpiGuid,\r
                          0,\r
                          NULL,\r
                          (VOID**)&TemporaryRamSupportPpi\r
                          );\r
-\r
-\r
               if (!EFI_ERROR (Status)) {\r
                 //\r
-                // Temporary Ram support Ppi is provided by platform, it will copy \r
+                // Temporary Ram Support PPI is provided by platform, it will copy \r
                 // temporary memory to permenent memory and do stack switching.\r
-                // After invoken temporary Ram support, following code's stack is in \r
-                // memory but not in temporary memory.\r
+                // After invoking Temporary Ram Support PPI, the following code's \r
+                // stack is in permanent memory.\r
                 //\r
                 TemporaryRamSupportPpi->TemporaryRamMigration (\r
-                                          (CONST EFI_PEI_SERVICES **) PeiServices,\r
-                                          (EFI_PHYSICAL_ADDRESS)(UINTN) SecCoreData->TemporaryRamBase,\r
-                                          (EFI_PHYSICAL_ADDRESS)(UINTN) NewPermenentMemoryBase,\r
-                                          SecCoreData->TemporaryRamSize\r
+                                          PeiServices,\r
+                                          TemporaryRamBase,\r
+                                          (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize),\r
+                                          TemporaryRamSize\r
                                           );\r
 \r
               } else {\r
                 //\r
                 // In IA32/x64/Itanium architecture, we need platform provide\r
-                // TEMPORAY_RAM_MIGRATION_PPI.\r
+                // TEMPORARY_RAM_MIGRATION_PPI.\r
                 //\r
                 ASSERT (FALSE);\r
               }\r
 \r
-\r
-              //\r
-              //\r
-              // Fixup the PeiCore's private data\r
-              //\r
-              PrivateInMem->Ps          = &PrivateInMem->ServiceTableShadow;\r
-              PrivateInMem->CpuIo       = &PrivateInMem->ServiceTableShadow.CpuIo;\r
-              PrivateInMem->HobList.Raw = (VOID*) ((UINTN) PrivateInMem->HobList.Raw + HeapOffset);\r
-              PrivateInMem->StackBase   = (EFI_PHYSICAL_ADDRESS)(((UINTN)PrivateInMem->PhysicalMemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK);\r
-\r
-              PeiServices = (CONST EFI_PEI_SERVICES **) &PrivateInMem->Ps;\r
-\r
-              //\r
-              // Fixup for PeiService's address\r
-              //\r
-              SetPeiServicesTablePointer(PeiServices);\r
-\r
-              //\r
-              // Update HandOffHob for new installed permenent memory\r
-              //\r
-              NewHandOffTable->EfiEndOfHobList =\r
-                (EFI_PHYSICAL_ADDRESS)((UINTN) NewHandOffTable->EfiEndOfHobList + HeapOffset);\r
-              NewHandOffTable->EfiMemoryTop        = PrivateInMem->PhysicalMemoryBegin +\r
-                                                     PrivateInMem->PhysicalMemoryLength;\r
-              NewHandOffTable->EfiMemoryBottom     = PrivateInMem->PhysicalMemoryBegin;\r
-              NewHandOffTable->EfiFreeMemoryTop    = PrivateInMem->FreePhysicalMemoryTop;\r
-              NewHandOffTable->EfiFreeMemoryBottom = NewHandOffTable->EfiEndOfHobList +\r
-                                                     sizeof (EFI_HOB_GENERIC_HEADER);\r
-\r
-              //\r
-              // We need convert the PPI desciptor's pointer\r
-              //\r
-              ConvertPpiPointers (PrivateInMem, \r
-                                  OldCheckingBottom, \r
-                                  OldCheckingTop, \r
-                                  HeapOffset\r
-                                  );\r
-\r
-              DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n",\r
-                                  PrivateInMem->StackBase,\r
-                                  PrivateInMem->StackSize));\r
-              BuildStackHob (PrivateInMem->StackBase, PrivateInMem->StackSize);\r
-\r
-              //\r
-              // After the whole temporary memory is migrated, then we can allocate page in\r
-              // permenent memory.\r
-              //\r
-              PrivateInMem->PeiMemoryInstalled     = TRUE;\r
-\r
-              //\r
-              // Indicate that PeiCore reenter\r
-              //\r
-              PrivateInMem->PeimDispatcherReenter  = TRUE;\r
-              \r
-              if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
-                //\r
-                // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.\r
-                // Every bit in the array indicate the status of the corresponding memory page available or not\r
-                //\r
-                PrivateInMem->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));\r
-              }\r
-              //\r
-              // Shadow PEI Core. When permanent memory is avaiable, shadow\r
-              // PEI Core and PEIMs to get high performance.\r
-              //\r
-              PrivateInMem->ShadowedPeiCore = ShadowPeiCore (\r
-                                                PeiServices,\r
-                                                PrivateInMem\r
-                                                );\r
-              //\r
-              // Process the Notify list and dispatch any notifies for\r
-              // newly installed PPIs.\r
-              //\r
-              ProcessNotifyList (PrivateInMem);\r
-\r
               //\r
               // Entry PEI Phase 2\r
               //\r
-              PeiCore (SecCoreData, NULL, PrivateInMem);\r
+              PeiCore (SecCoreData, NULL, Private);\r
 \r
               //\r
               // Code should not come here\r
               //\r
-              ASSERT_EFI_ERROR(FALSE);\r
+              ASSERT (FALSE);\r
             }\r
 \r
             //\r
@@ -1093,11 +1078,20 @@ DepexSatisfied (
 {\r
   EFI_STATUS           Status;\r
   VOID                 *DepexData;\r
+  EFI_FV_FILE_INFO     FileInfo;\r
 \r
+  Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n"));\r
+  } else {\r
+    DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));\r
+  }\r
+  \r
   if (PeimCount < Private->AprioriCount) {\r
     //\r
     // If its in the A priori file then we set Depex to TRUE\r
     //\r
+    DEBUG ((DEBUG_DISPATCH, "  RESULT = TRUE (Apriori)\n"));\r
     return TRUE;\r
   }\r
 \r
@@ -1114,6 +1108,7 @@ DepexSatisfied (
     //\r
     // If there is no DEPEX, assume the module can be executed\r
     //\r
+    DEBUG ((DEBUG_DISPATCH, "  RESULT = TRUE (No DEPEX)\n"));\r
     return TRUE;\r
   }\r
 \r