From nobody Tue Feb 10 01:16:01 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+100355+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100355+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1676870453; cv=none; d=zohomail.com; s=zohoarc; b=QRfMGpnhMF8FT2NjdPp43tnuj5mpA1ju/PbMSej/+ICf6QbQYvtezHjoOwz/PtyHs2JtkMjz298iKiuRnb4PGxsdwSls+jBkQDMB8uU23xRuU87raA1x/x847z2nAUeeaUxL3pAn0mZLHn0wegh2LO41JLevRuAHFDLW+Aj5/jM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676870453; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=LYf0zW3DvZtyJSc8j97WWDfaebKPaJgS7utiyZmec5c=; b=BBsp6xA8iV+rH/YpLinXcBAXU0HXdGiZbs3PXeVHkcqxB/UqgxHRkZxtQiJpeycpYknWWsEQxoXCnAxkUP83o9otQWVMugcqqtzxVJDvRo5Awf5DRCowM/vODN+MF2psajP5jZ62dX/h1AYVKZUjNnBbKlFv7BIf5ALk9+HSkos= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+100355+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1676870453255536.6914798409749; Sun, 19 Feb 2023 21:20:53 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id neGnYY1788612x0DB4IE0sZL; Sun, 19 Feb 2023 21:20:52 -0800 X-Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web11.6790.1676870445058179079 for ; Sun, 19 Feb 2023 21:20:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10626"; a="311957536" X-IronPort-AV: E=Sophos;i="5.97,311,1669104000"; d="scan'208";a="311957536" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Feb 2023 21:20:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10626"; a="701556254" X-IronPort-AV: E=Sophos;i="5.97,311,1669104000"; d="scan'208";a="701556254" X-Received: from shwdeopenlab705.ccr.corp.intel.com ([10.239.182.166]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Feb 2023 21:20:50 -0800 From: "Yuanhao Xie" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Sean Rhodes , James Lu , Gua Guo Subject: [edk2-devel] [Patch V2 2/5] UefiCpuPkg: Contiguous memory allocation and code clean-up. Date: Mon, 20 Feb 2023 13:20:19 +0800 Message-Id: <20230220052022.9290-3-yuanhao.xie@intel.com> In-Reply-To: <20230220052022.9290-1-yuanhao.xie@intel.com> References: <20230220052022.9290-1-yuanhao.xie@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,yuanhao.xie@intel.com X-Gm-Message-State: mhm2cqAwLpcTvnaHmTbYbBBTx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1676870452; bh=QA012OfcWqVspTuvTG4oRG9benz/ERK01prRNh2o8mM=; h=Cc:Date:From:Reply-To:Subject:To; b=ut6LRPR2SLRNLjrPQSDKcrtNn9YM44NgV0T8L+nSoH7GCuZos0BhU6YBLOdO25xZJtm zzM8LorDuHJ8YKPgzLiMKjj15fG8K5pkf55H4aYMupruDCF0ZMidzKpI+UN+NJ0GED4eT 5UnPpxwkri2xLzy/SZaEdqxZvcjCjUrGCsk= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1676870455351100001 Content-Type: text/plain; charset="utf-8" Allocate the memory for stacks and APs loop at contiguous address. The memory is calculated taking into account the size difference of RelocateApLoopFunc under different cases. This patch also includes the code refactoring to eliminate the duplication, non-descriptive variable, etc. Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo Signed-off-by: Yuanhao Xie --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 157 ++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++------------------------------= ------------------------------------------------------ UefiCpuPkg/Library/MpInitLib/MpLib.h | 6 ++++++ 2 files changed, 79 insertions(+), 84 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/M= pInitLib/DxeMpLib.c index dd935a79d3..75743faf5f 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -20,14 +20,15 @@ =20 #define AP_SAFE_STACK_SIZE 128 =20 -CPU_MP_DATA *mCpuMpData =3D NULL; -EFI_EVENT mCheckAllApsEvent =3D NULL; -EFI_EVENT mMpInitExitBootServicesEvent =3D NULL; -EFI_EVENT mLegacyBootEvent =3D NULL; -volatile BOOLEAN mStopCheckAllApsStatus =3D TRUE; -VOID *mReservedApLoopFunc =3D NULL; -UINTN mReservedTopOfApStack; -volatile UINT32 mNumberToFinish =3D 0; +CPU_MP_DATA *mCpuMpData =3D NULL; +EFI_EVENT mCheckAllApsEvent =3D NULL; +EFI_EVENT mMpInitExitBootServicesEvent =3D NULL; +EFI_EVENT mLegacyBootEvent =3D NULL; +volatile BOOLEAN mStopCheckAllApsStatus =3D TRUE; +UINTN mReservedTopOfApStack; +volatile UINT32 mNumberToFinish =3D 0; +RELOCATE_AP_LOOP_ENTRY mReservedApLoop; + =20 // // Begin wakeup buffer allocation below 0x88000 @@ -380,8 +381,6 @@ RelocateApLoop ( { CPU_MP_DATA *CpuMpData; BOOLEAN MwaitSupport; - ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc; - ASM_RELOCATE_AP_LOOP_AMDSEV AsmRelocateApLoopFuncAmdSev; UINTN ProcessorNumber; UINTN StackStart; =20 @@ -389,31 +388,29 @@ RelocateApLoop ( CpuMpData =3D GetCpuMpData (); MwaitSupport =3D IsMwaitSupport (); if (CpuMpData->UseSevEsAPMethod) { - StackStart =3D CpuMpData->SevEsAPResetStackStart; - AsmRelocateApLoopFuncAmdSev =3D (ASM_RELOCATE_AP_LOOP)(UINTN)mReserved= ApLoopFunc; - AsmRelocateApLoopFuncAmdSev ( - MwaitSupport, - CpuMpData->ApTargetCState, - CpuMpData->PmCodeSegment, - StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, - (UINTN)&mNumberToFinish, - CpuMpData->Pm16CodeSegment, - CpuMpData->SevEsAPBuffer, - CpuMpData->WakeupBuffer - ); + StackStart =3D CpuMpData->SevEsAPResetStackStart; + mReservedApLoop.AmdSevEntry ( + MwaitSupport, + CpuMpData->ApTargetCState, + CpuMpData->PmCodeSegment, + StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, + (UINTN)&mNumberToFinish, + CpuMpData->Pm16CodeSegment, + CpuMpData->SevEsAPBuffer, + CpuMpData->WakeupBuffer + ); } else { - StackStart =3D mReservedTopOfApStack; - AsmRelocateApLoopFunc =3D (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoop= Func; - AsmRelocateApLoopFunc ( - MwaitSupport, - CpuMpData->ApTargetCState, - CpuMpData->PmCodeSegment, - StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, - (UINTN)&mNumberToFinish, - CpuMpData->Pm16CodeSegment, - CpuMpData->SevEsAPBuffer, - CpuMpData->WakeupBuffer - ); + StackStart =3D mReservedTopOfApStack; + mReservedApLoop.GenericEntry ( + MwaitSupport, + CpuMpData->ApTargetCState, + CpuMpData->PmCodeSegment, + StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, + (UINTN)&mNumberToFinish, + CpuMpData->Pm16CodeSegment, + CpuMpData->SevEsAPBuffer, + CpuMpData->WakeupBuffer + ); } =20 // @@ -477,12 +474,16 @@ InitMpGlobalData ( ) { EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Address; - UINTN ApSafeBufferSize; + MP_ASSEMBLY_ADDRESS_MAP *AddressMap; + UINTN StackPages; + UINTN FuncPages; UINTN Index; EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc; UINTN StackBase; CPU_INFO_IN_HOB *CpuInfoInHob; + EFI_PHYSICAL_ADDRESS Address; + UINT8 *ApLoopFunc; + UINTN ApLoopFuncSize; =20 SaveCpuMpData (CpuMpData); =20 @@ -537,6 +538,15 @@ InitMpGlobalData ( } } =20 + AddressMap =3D &CpuMpData->AddressMap; + if (CpuMpData->UseSevEsAPMethod) { + ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddressAmdSev; + ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSizeAmdSev; + } else { + ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddress; + ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSize; + } + // // Avoid APs access invalid buffer data which allocated by BootServices, // so we will allocate reserved data for AP loop code. We also need to @@ -545,26 +555,31 @@ InitMpGlobalData ( // Allocating it in advance since memory services are not available in // Exit Boot Services callback function. // - ApSafeBufferSize =3D EFI_PAGES_TO_SIZE ( - EFI_SIZE_TO_PAGES ( - CpuMpData->AddressMap.RelocateApLoopFuncSize - ) - ); - Address =3D BASE_4GB - 1; - Status =3D gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - EFI_SIZE_TO_PAGES (ApSafeBufferSize), - &Address - ); - ASSERT_EFI_ERROR (Status); + // +------------+ (TopOfApStack) + // | Stack * N | + // +------------+ + // | Padding | + // +------------+ + // | Ap Loop | + // +------------+ (low address ) + // =20 - mReservedApLoopFunc =3D (VOID *)(UINTN)Address; - ASSERT (mReservedApLoopFunc !=3D NULL); + Address =3D BASE_4GB - 1; + StackPages =3D EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SI= ZE); + FuncPages =3D EFI_SIZE_TO_PAGES (ALIGN_VALUE (ApLoopFuncSize, EFI_PAGE_= SIZE)); =20 - // - // Make sure that the buffer memory is executable if NX protection is en= abled - // for EfiReservedMemoryType. + Status =3D gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + StackPages+FuncPages, + &Address + ); + ASSERT_EFI_ERROR (Status); + // If a memory range has the EFI_MEMORY_XP attribute, OS loader + // may set the IA32_EFER.NXE (No-eXecution Enable) bit in IA32_EFER MSR, + // then set the XD (eXecution Disable) bit in the CPU PAE page table. + // Here is to make sure that the 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 G= CD // service. @@ -573,41 +588,15 @@ InitMpGlobalData ( if (!EFI_ERROR (Status)) { gDS->SetMemorySpaceAttributes ( Address, - ApSafeBufferSize, + ALIGN_VALUE (ApLoopFuncSize, EFI_PAGE_SIZE), MemDesc.Attributes & (~EFI_MEMORY_XP) ); } =20 - ApSafeBufferSize =3D EFI_PAGES_TO_SIZE ( - EFI_SIZE_TO_PAGES ( - CpuMpData->CpuCount * AP_SAFE_STACK_SIZE - ) - ); - Address =3D BASE_4GB - 1; - Status =3D gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - EFI_SIZE_TO_PAGES (ApSafeBufferSize), - &Address - ); - ASSERT_EFI_ERROR (Status); - - mReservedTopOfApStack =3D (UINTN)Address + ApSafeBufferSize; + mReservedTopOfApStack =3D (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages= +FuncPages); ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) =3D= =3D 0); - if (CpuMpData->UseSevEsAPMethod) { - CopyMem ( - mReservedApLoopFunc, - CpuMpData->AddressMap.RelocateApLoopFuncAddressAmdSev, - CpuMpData->AddressMap.RelocateApLoopFuncSizeAmdSev - ); - } else { - CopyMem ( - mReservedApLoopFunc, - CpuMpData->AddressMap.RelocateApLoopFuncAddress, - CpuMpData->AddressMap.RelocateApLoopFuncSize - ); - } - + mReservedApLoop.Data =3D (VOID *)(UINTN)Address; + CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize); Status =3D gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY, diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index fe60084333..772a828045 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -401,6 +401,12 @@ typedef IN UINTN WakeupBuffer ); =20 +typedef union { + VOID *Data; + ASM_RELOCATE_AP_LOOP_AMDSEV AmdSevEntry; // 64-bit AMD Sev processors + ASM_RELOCATE_AP_LOOP GenericEntry; // Intel processors (32-bit= or 64-bit), 32-bit AMD processors, or AMD non-Sev processors +} RELOCATE_AP_LOOP_ENTRY; + /** Assembly code to get starting address and size of the rendezvous entry f= or APs. Information for fixing a jump instruction in the code is also returned. --=20 2.36.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#100355): https://edk2.groups.io/g/devel/message/100355 Mute This Topic: https://groups.io/mt/97081030/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-