From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 107C2740038 for ; Fri, 10 May 2024 10:09:10 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=UjJNZv7ROUNQQmca6GS9Gpl+cyHVKp+/W/uYYsmYxno=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1715335749; v=1; b=RE1G16sSUmuWxnPO8Tb31fWG8e1CEuydv5x6aQJtGqU4F1Gu2wGweb2m1/4B753JxenGBqPO unAWjJUs+6S7MG6ZZyQ4wm/JW9WBrn9Br9kBdK2V5t+LOJy4uRV5q3uM6C/sl/XbV9RKL9Jqd0L V/3sGW61PGCTP+iG3cdIq5OLrsVOwrPiOyyV7oDkmx9883j32y0VdpvG7vl0XLk28tQ9DxRq5ZJ lpGBcP8EBqPGSnqRASEwuaQsuh7c7jxL59XhV+4ZLQzCfAo1Oj+lGeqYuK119t1VkFRa8e/QucQ RydKYJV1cO1FmZudY/uqvToXPb7LSBU/PrW318OnMqjTA== X-Received: by 127.0.0.2 with SMTP id TFMzYY7687511xTwUjfzQeZz; Fri, 10 May 2024 03:09:09 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by mx.groups.io with SMTP id smtpd.web10.9485.1715335727366928454 for ; Fri, 10 May 2024 03:09:09 -0700 X-CSE-ConnectionGUID: wijaLL1QQSWy5P4Y7h/Iew== X-CSE-MsgGUID: PmVsr2EMTkqsma50DVSa/Q== X-IronPort-AV: E=McAfee;i="6600,9927,11068"; a="28819171" X-IronPort-AV: E=Sophos;i="6.08,150,1712646000"; d="scan'208";a="28819171" X-Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2024 03:09:09 -0700 X-CSE-ConnectionGUID: Uc9Of9zSQby8FA1+Ze0EnA== X-CSE-MsgGUID: lChyqeFSSx+xfRr3BpkVQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,150,1712646000"; d="scan'208";a="60424278" X-Received: from unknown (HELO shwdeopenlab702.ccr.corp.intel.com) ([10.239.55.43]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 May 2024 03:09:08 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek , Rahul Kumar , Gerd Hoffmann , Jiaxin Wu Subject: [edk2-devel] [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to function Date: Fri, 10 May 2024 18:08:17 +0800 Message-Id: <20240510100827.1903-9-dun.tan@intel.com> In-Reply-To: <20240510100827.1903-1-dun.tan@intel.com> References: <20240510100827.1903-1-dun.tan@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 Resent-Date: Fri, 10 May 2024 03:09:09 -0700 Resent-From: dun.tan@intel.com Reply-To: devel@edk2.groups.io,dun.tan@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: usHS33uq9ZDFCb5Qpk7j3Egfx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=RE1G16sS; 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 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io Abstract some DxeMpLib code to function in this commit. Some of these internal functions will be moved to common MpLib.c in following commits. Then PeiMpLib can reuse the code. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Gerd Hoffmann Cc: Jiaxin Wu --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------- 1 file changed, 139 insertions(+), 81 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c index 57ddb86600..d13cebbee6 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -422,6 +422,144 @@ RelocateApLoop ( ASSERT (FALSE); } +/** + Allocate buffer for ApLoopCode. + + @param[in] Pages Number of pages to allocate. + @param[in, out] Address Pointer to the allocated buffer. +**/ +VOID +AllocateApLoopCodeBuffer ( + IN UINTN Pages, + IN OUT EFI_PHYSICAL_ADDRESS *Address + ) +{ + EFI_STATUS Status; + + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + Pages, + Address + ); + ASSERT_EFI_ERROR (Status); +} + +/** + Remove Nx protection for the range specific by BaseAddress and Length. + + The PEI implementation uses CpuPageTableLib to change the attribute. + The DXE implementation uses gDS to change the attribute. + + @param[in] BaseAddress BaseAddress of the range. + @param[in] Length Length of the range. +**/ +VOID +RemoveNxprotection ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINTN Length + ) +{ + EFI_STATUS Status; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc; + + // + // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD + // service. + // + Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &MemDesc); + if (!EFI_ERROR (Status)) { + gDS->SetMemorySpaceAttributes ( + BaseAddress, + Length, + MemDesc.Attributes & (~EFI_MEMORY_XP) + ); + } +} + +/** + Prepare ApLoopCode. + + @param[in] CpuMpData Pointer to CpuMpData. +**/ +VOID +PrepareApLoopCode ( + IN CPU_MP_DATA *CpuMpData + ) +{ + EFI_PHYSICAL_ADDRESS Address; + MP_ASSEMBLY_ADDRESS_MAP *AddressMap; + UINT8 *ApLoopFunc; + UINTN ApLoopFuncSize; + UINTN StackPages; + UINTN FuncPages; + IA32_CR0 Cr0; + + AddressMap = &CpuMpData->AddressMap; + if (CpuMpData->UseSevEsAPMethod) { + // + // 64-bit AMD processors with SEV-ES + // + Address = BASE_4GB - 1; + ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev; + ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev; + } else { + // + // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit AMD processors without SEV-ES + // + Address = MAX_ADDRESS; + ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric; + ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric; + } + + // + // Avoid APs access invalid buffer data which allocated by BootServices, + // so we will allocate reserved data for AP loop code. We also need to + // allocate this buffer below 4GB due to APs may be transferred to 32bit + // protected mode on long mode DXE. + // Allocating it in advance since memory services are not available in + // Exit Boot Services callback function. + // + // +------------+ (TopOfApStack) + // | Stack * N | + // +------------+ (stack base, 4k aligned) + // | Padding | + // +------------+ + // | Ap Loop | + // +------------+ ((low address, 4k-aligned) + // + + StackPages = EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); + FuncPages = EFI_SIZE_TO_PAGES (ApLoopFuncSize); + + AllocateApLoopCodeBuffer (StackPages + FuncPages, &Address); + ASSERT (Address != 0); + + Cr0.UintN = AsmReadCr0 (); + if (Cr0.Bits.PG != 0) { + // + // Make sure that the buffer memory is executable if NX protection is enabled + // for EfiReservedMemoryType. + // + RemoveNxprotection (Address, EFI_PAGES_TO_SIZE (FuncPages)); + } + + mReservedTopOfApStack = (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages+FuncPages); + ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); + mReservedApLoop.Data = (VOID *)(UINTN)Address; + ASSERT (mReservedApLoop.Data != NULL); + CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize); + if (!CpuMpData->UseSevEsAPMethod) { + // + // processors without SEV-ES and paging is enabled + // + mApPageTable = CreatePageTable ( + (UINTN)Address, + EFI_PAGES_TO_SIZE (StackPages+FuncPages) + ); + } +} + /** Callback function for ExitBootServices. @@ -477,16 +615,10 @@ InitMpGlobalData ( ) { EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Address; UINTN Index; EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc; UINTN StackBase; CPU_INFO_IN_HOB *CpuInfoInHob; - MP_ASSEMBLY_ADDRESS_MAP *AddressMap; - UINT8 *ApLoopFunc; - UINTN ApLoopFuncSize; - UINTN StackPages; - UINTN FuncPages; SaveCpuMpData (CpuMpData); @@ -541,81 +673,7 @@ InitMpGlobalData ( } } - AddressMap = &CpuMpData->AddressMap; - if (CpuMpData->UseSevEsAPMethod) { - // - // 64-bit AMD processors with SEV-ES - // - Address = BASE_4GB - 1; - ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev; - ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev; - } else { - // - // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit AMD processors without SEV-ES - // - Address = MAX_ADDRESS; - ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric; - ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric; - } - - // - // Avoid APs access invalid buffer data which allocated by BootServices, - // so we will allocate reserved data for AP loop code. We also need to - // allocate this buffer below 4GB due to APs may be transferred to 32bit - // protected mode on long mode DXE. - // Allocating it in advance since memory services are not available in - // Exit Boot Services callback function. - // - // +------------+ (TopOfApStack) - // | Stack * N | - // +------------+ (stack base, 4k aligned) - // | Padding | - // +------------+ - // | Ap Loop | - // +------------+ ((low address, 4k-aligned) - // - - StackPages = EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); - FuncPages = EFI_SIZE_TO_PAGES (ApLoopFuncSize); - - Status = gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - StackPages + FuncPages, - &Address - ); - ASSERT_EFI_ERROR (Status); - - // - // Make sure that the buffer memory is executable if NX protection is enabled - // for EfiReservedMemoryType. - // - // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD - // service. - // - Status = gDS->GetMemorySpaceDescriptor (Address, &MemDesc); - if (!EFI_ERROR (Status)) { - gDS->SetMemorySpaceAttributes ( - Address, - EFI_PAGES_TO_SIZE (FuncPages), - MemDesc.Attributes & (~EFI_MEMORY_XP) - ); - } - - mReservedTopOfApStack = (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages+FuncPages); - ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); - mReservedApLoop.Data = (VOID *)(UINTN)Address; - ASSERT (mReservedApLoop.Data != NULL); - CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize); - if (!CpuMpData->UseSevEsAPMethod) { - // - // processors without SEV-ES - // - mApPageTable = CreatePageTable ( - (UINTN)Address, - EFI_PAGES_TO_SIZE (StackPages+FuncPages) - ); - } + PrepareApLoopCode (CpuMpData); Status = gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, -- 2.31.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118800): https://edk2.groups.io/g/devel/message/118800 Mute This Topic: https://groups.io/mt/106018132/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-