Align the top of the stack correctly when the PEI Core calls SwitchStack()
[mirror_edk2.git] / EdkModulePkg / Core / Pei / Dispatcher / Dispatcher.c
index b0de859073ad9e464504aeeba76bda688a3e8a1a..ccd840d3761e42ed8b039bec7f50db74cc7e19b0 100644 (file)
@@ -63,6 +63,7 @@ Returns:
   BOOLEAN                           NextFvFound;\r
   EFI_FIRMWARE_VOLUME_HEADER        *NextFvAddress;\r
   EFI_FIRMWARE_VOLUME_HEADER        *DefaultFvAddress;\r
+  VOID                              *TopOfStack;\r
   //\r
   // Debug data for uninstalled Peim list\r
   //\r
@@ -204,15 +205,16 @@ Returns:
                 PrivateDataInMem = (UINTN) TransferOldDataToNewDataRange (PrivateData);\r
                 ASSERT (PrivateDataInMem != 0);\r
                 //\r
-                //Subtract 0x10 from the 4th parameter indicating the new stack base,\r
-                //in order to provide buffer protection against possible illegal stack\r
-                //access that might corrupt the stack.\r
+                // Adjust the top of stack to be aligned at CPU_STACK_ALIGNMENT\r
                 //\r
+                TopOfStack = (VOID *)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize - CPU_STACK_ALIGNMENT);\r
+                TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
+\r
                 PeiSwitchStacks (\r
                   (SWITCH_STACK_ENTRY_POINT)(UINTN)TempPtr.Raw,\r
                   PeiStartupDescriptor,\r
                   (VOID*)PrivateDataInMem,\r
-                  (VOID*)((UINTN)PrivateData->StackBase + (UINTN)PrivateData->StackSize),\r
+                  TopOfStack,\r
                   (VOID*)(UINTN)PrivateData->StackBase\r
                   );\r
               }\r