From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web12.29753.1647866653427366424 for ; Mon, 21 Mar 2022 05:44:14 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=bNswIPAW; spf=pass (domain: intel.com, ip: 192.55.52.136, mailfrom: ted.kuo@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647866653; x=1679402653; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=bZgy4mBVLh2qwYsWiexOGX8M0Yx/KW6uNCTYlSSuI54=; b=bNswIPAWf8ZK95HNl0mTzRKBXW4gCEsWOHaTsW1Jy74S5qmGEB6RQqA7 T0mzHUcE6Kl4OuT1oteHD3nCZPlwUCfBgJKjJxyrcx0kII6RU/jYXEERQ 8pjCJHIvlPvwh55T1gWuHXLPnIFoawpY+5tsRqeAakAk28NxiUJ5Pujx7 tNGYooFRRvGELasf8n9d5M/y+P6a38VKkthqvOVjq6B0syDbX8L/hzbFT CWL1SNALw1ClWq56OQzztge2shZxXaGE7ZSK4vO+LdL/vO0pjmF8Mioih QohKUUSeuqVMSfPhu0rD4fqzc2dqhqcBdUys3MGfPa3hvcu3jgvFvyXPn w==; X-IronPort-AV: E=McAfee;i="6200,9189,10292"; a="237483232" X-IronPort-AV: E=Sophos;i="5.90,198,1643702400"; d="scan'208";a="237483232" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 05:44:12 -0700 X-IronPort-AV: E=Sophos;i="5.90,198,1643702400"; d="scan'208";a="692159690" Received: from tedkuo1-win10.gar.corp.intel.com ([10.5.215.13]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 05:44:10 -0700 From: "Kuo, Ted" To: devel@edk2.groups.io Cc: Michael D Kinney , Dandan Bi , Liming Gao , Debkumar De , Harry Han , Catharine West , Jian J Wang , =?UTF-8?q?Marvin=20H=C3=A4user?= Subject: [edk2-devel][PATCH 1/2] MdeModulePkg: StackOffset must be aligned to a 16-byte boundary in X64 Date: Mon, 21 Mar 2022 20:43:27 +0800 Message-Id: <6301e56ae7ec1852c8bf499c2df69e0a04420443.1647864782.git.ted.kuo@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Cc: Dandan Bi Cc: Liming Gao Cc: Debkumar De Cc: Harry Han Cc: Catharine West Cc: Jian J Wang Cc: Marvin Häuser Signed-off-by: Ted Kuo --- 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