REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3865
For X64, StackOffset must be aligned to a 16-byte boundary as well as
old stack and new stack. Otherwise, it'll get wrong data from Private
pointer after switching from old stack to new stack.

Cc: Michael D Kinney <michael.d.kin...@intel.com>
Cc: Dandan Bi <dandan...@intel.com>
Cc: Liming Gao <gaolim...@byosoft.com.cn>
Cc: Debkumar De <debkumar...@intel.com>
Cc: Harry Han <harry....@intel.com>
Cc: Catharine West <catharine.w...@intel.com>
Cc: Jian J Wang <jian.j.w...@intel.com>
Cc: Marvin Häuser <mhaeu...@posteo.de>
Signed-off-by: Ted Kuo <ted....@intel.com>
---
 MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c 
b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
index 3552feda8f..8a2c1ec779 100644
--- a/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
+++ b/MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
@@ -823,6 +823,19 @@ PeiCheckAndSwitchStack (
                (VOID **)&TemporaryRamSupportPpi
                );
     if (!EFI_ERROR (Status)) {
+      //
+      // For X64, StackOffset must be aligned to a 16-byte boundary. 
Otherwise, it'll get wrong data
+      // from Private pointer after switching to new stack.
+      //
+      if ((sizeof (UINTN) == sizeof (UINT64)) && ((StackOffset & 0x0F) == 8)) {
+        if (StackOffsetPositive == TRUE) {
+          StackOffset -= 8;
+        } else {
+          StackOffset += 8;
+        }
+        Private->StackOffset = StackOffset;
+      }
+
       //
       // Heap Offset
       //
@@ -852,7 +865,10 @@ PeiCheckAndSwitchStack (
       // Temporary Ram Support PPI is provided by platform, it will copy
       // temporary memory to permanent memory and do stack switching.
       // After invoking Temporary Ram Support PPI, the following code's
-      // stack is in permanent memory.
+      // stack is in permanent memory. For X64, the bit3:0 of the new stack
+      // produced by TemporaryRamMigration must be aligned with the bit3:0 of
+      // the old stack. Otherwise, it'll break the original stack alignment
+      // after switching to new stack.
       //
       TemporaryRamSupportPpi->TemporaryRamMigration (
                                 PeiServices,
-- 
2.16.2.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#87792): https://edk2.groups.io/g/devel/message/87792
Mute This Topic: https://groups.io/mt/89926603/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to