From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 87C4974003B for ; Tue, 31 Oct 2023 08:22:56 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=lo/M6iEzq3re7z/Wacv+CSsEUz3YxSvjbX7osp4a2WI=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1698740575; v=1; b=vrPh9IvvsYe/mX49yiplfhTWWRT6Lz10m08WiTrRSU8p8ZhtZ2bxORuimNX1BMUWN1iGtDsa KOFKfyNjdcdyBZc+bmjWS6JHagVRCEdlPgawxDXPhBDcaXrylIY4MnVbgaEWHrDkcY0CDhDTPkC XgD2b0ZXxXh3nJtoC2BE/Seg= X-Received: by 127.0.0.2 with SMTP id oyVFYY7687511xeKmVCIpGlf; Tue, 31 Oct 2023 01:22:55 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web10.181437.1698740574380256778 for ; Tue, 31 Oct 2023 01:22:54 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="454705955" X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="454705955" X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Oct 2023 01:22:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10879"; a="877436583" X-IronPort-AV: E=Sophos;i="6.03,265,1694761200"; d="scan'208";a="877436583" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.158.139]) by fmsmga002.fm.intel.com with ESMTP; 31 Oct 2023 01:22:52 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Chasel Chiu Subject: [edk2-devel] [PATCH] IntelFsp2Pkg/SwitchStack: Reserve 32B when calling C function in 64bit Date: Tue, 31 Oct 2023 16:22:16 +0800 Message-Id: <20231031082216.2038-1-ray.ni@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ray.ni@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: f3VzieDxwGRWjEdjg0ZlLbZJx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=vrPh9Ivv; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io When FSP runs in API mode, it saves the IDTR in its own stack then switches to bootloader's stack before it returns from FspMemoryInit. Next time when the bootloader calls TempRamExit, FSP switches to its own stack and restores IDTR from its stack saved earlier. However, due to a bug in BaseFspSwitchStackLib, the IDTR saved on FSP's stack might be corrupted that results the following TempRamExit call fails inside FSP due to PeiServices pointer cannot be retrieved from IDT.base - 8. The bug is the assembly code doesn't reserve 32 bytes before calling the C routine in 64bit. According to the x86-64 calling convention, caller is responsible for allocating 32 bytes of "shadow space" on the stack right before calling the function (regardless of the actual number of parameters used). When FSP is built in optimization-off mode, the C routine makes use of the 32-byte "shadow space" which is not reserved by the assembly caller. That causes the IDTR saved on the stack is corrupted by the C routine. The patch fixes so by reserving the 32 bytes before calling C routine. Signed-off-by: Ray Ni Cc: Chasel Chiu M: Nate DeSimone M: Duggapu Chinni B M: Ray Han Lim Ng R: Star Zeng R: Ted Kuo R: Ashraf Ali S R: Susovan Mohapatra --- IntelFsp2Pkg/Library/BaseFspSwitchStackLib/X64/Stack.nasm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/IntelFsp2Pkg/Library/BaseFspSwitchStackLib/X64/Stack.nasm b/In= telFsp2Pkg/Library/BaseFspSwitchStackLib/X64/Stack.nasm index 1ea1220608..e3a7cf002f 100644 --- a/IntelFsp2Pkg/Library/BaseFspSwitchStackLib/X64/Stack.nasm +++ b/IntelFsp2Pkg/Library/BaseFspSwitchStackLib/X64/Stack.nasm @@ -1,6 +1,6 @@ ;-------------------------------------------------------------------------= -----=0D ;=0D -; Copyright (c) 2022, Intel Corporation. All rights reserved.
=0D +; Copyright (c) 2022 - 2023, Intel Corporation. All rights reserved.
=0D ; SPDX-License-Identifier: BSD-2-Clause-Patent=0D ;=0D ; Abstract:=0D @@ -60,7 +60,9 @@ ASM_PFX(FspSwitchStack): =0D ; Load new stack=0D mov rcx, rsp=0D + sub rsp, 0x20=0D call ASM_PFX(SwapStack)=0D + add rsp, 0x20=0D mov rsp, rax=0D =0D ; Restore previous contexts=0D --=20 2.39.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110384): https://edk2.groups.io/g/devel/message/110384 Mute This Topic: https://groups.io/mt/102293342/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/xyzzy [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-