From nobody Tue Feb 10 03:38:31 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+100461+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+100461+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1677175583; cv=none; d=zohomail.com; s=zohoarc; b=NJhXzRMJfld6h6zsSFOLWWUvjfkIG0gRikYRoll+jKx89Ft6EO4m8fOK49YTNehPbtDAJhnVEOqtpxC1hqalRYTbmDRA/3/tpVZbr9qucqjeZLUZRXL5J/YlJziO5voGG7rFNNC5ixrEHGpG8o3mllTczHb8naMKIcoxTmEwDbI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677175583; 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=Dk0ZhWk4qsOqS1FmQR1rGOUqpys38rlXN0+z7D6JkjY=; b=lTovv8WScbDJoK0abB/Pw+jK5S+aqyYG2YQ8SRSzSp8h6G6eAMh+0KkYs7PzgdJhHlO6DdidBSW5ZDpypqraj+sMKDJhHi0hmr9yxjiUXcY5QhNPgRzQKBUq37jf4PUmM0SoDOfr/LhRqq0vLPVBVXifgxidrmUwcX7KiqeeXkQ= 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+100461+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 1677175583781936.4819418779128; Thu, 23 Feb 2023 10:06:23 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id d687YY1788612x2s9PHzvxMk; Thu, 23 Feb 2023 10:06:23 -0800 X-Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web11.18315.1677175578044889092 for ; Thu, 23 Feb 2023 10:06:22 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10630"; a="333288092" X-IronPort-AV: E=Sophos;i="5.97,322,1669104000"; d="scan'208";a="333288092" X-Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Feb 2023 10:06:02 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10630"; a="918106899" X-IronPort-AV: E=Sophos;i="5.97,322,1669104000"; d="scan'208";a="918106899" X-Received: from shwdeopenlab705.ccr.corp.intel.com ([10.239.182.166]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Feb 2023 10:06:01 -0800 From: "Yuanhao Xie" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Sean Rhodes , James Lu , Gua Guo Subject: [edk2-devel] [Patch V3 3/6] UefiCpuPkg: Contiguous memory allocation and code clean-up. Date: Fri, 24 Feb 2023 02:05:32 +0800 Message-Id: <20230223180535.10383-4-yuanhao.xie@intel.com> In-Reply-To: <20230223180535.10383-1-yuanhao.xie@intel.com> References: <20230223180535.10383-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: p3oBpNz7uHtsolt3NRYlOHbbx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1677175583; bh=SUNwiM8WsQPpbOZTIzvxIVqvcT+7l+4uYxVACEETeBo=; h=Cc:Date:From:Reply-To:Subject:To; b=rT5ssfPX/r3Fj1msI6r0EQp1AKRjiSA6aAa7Cuh+0MYlm+uEnujkgXrl73/ZUgyNVfy MzrdSKO9Paccx1QwDO2i5yJGPs47zSh2ena6Oo0kMVsYeA1LJQ12H50yLaQwRE8AUuK7z buYlut+DHRRDJmaErWyVawpBwE2pV28NZ2c= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1677175585372100013 Content-Type: text/plain; charset="utf-8" This patch includes the code refactoring to eliminate the duplication, non-descriptive variable, etc. The memory is calculated taking into account the size difference of RelocateApLoopFunc under different cases. Allocate the memory for stacks and APs loop at contiguous address. Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo Signed-off-by: Yuanhao Xie Test-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..c095ee9f13 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 (ApLoopFuncSize); =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 5011533302..f0daa2c5af 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -400,6 +400,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 (#100461): https://edk2.groups.io/g/devel/message/100461 Mute This Topic: https://groups.io/mt/97188908/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-