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 423147803CD for ; Mon, 13 May 2024 02:13:20 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=HX5SDypMXx7RlYeeT1z1PJ2U4+Rzj/akmMzTq4000hU=; c=relaxed/simple; d=groups.io; h=From:To:CC:Subject:Thread-Topic:Thread-Index:Date:Message-ID:References:In-Reply-To:Accept-Language:msip_labels: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-Language:Content-Type; s=20240206; t=1715566398; v=1; b=Z8TcmwCKE5gL+3W+tDWo4Q1CFkBPh/rRoRRwPuoqAfVlQBlFJQH8BklOTlodMhQJB7ct5m0b Ng3TMVG/LxQG0nj/LryZlQLxE7XbKt9fzUs5daKcXv9m2AorbzLVKxc3n+VDyjuM8pVTDHJPKMW uemb48kBm77HkgM1wx1NSJerrGS03buhLXRR9yj36EwDI3cn56YDT+TI+uhfqHBpgspkas9ut83 kSMY4zhp7ivBy+pQEObCW8Yxp54mgl946Cgzq8da6EfCrfvYPrpt1ZdzcZHjykIFVnFZVSaBxMH EcidRUQUDcthfwslRQ7GrdImyVmZYP+Q+V50XZO5gX10A== X-Received: by 127.0.0.2 with SMTP id y7XyYY7687511xUaRQpmUu7W; Sun, 12 May 2024 19:13:18 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) by mx.groups.io with SMTP id smtpd.web11.51133.1715566397619843218 for ; Sun, 12 May 2024 19:13:17 -0700 X-CSE-ConnectionGUID: VgBdkuHaTqW9Eua74TC/kw== X-CSE-MsgGUID: B2Ol9GHsQlOGGuPPRADQpg== X-IronPort-AV: E=McAfee;i="6600,9927,11071"; a="11318866" X-IronPort-AV: E=Sophos;i="6.08,157,1712646000"; d="scan'208,217";a="11318866" X-Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 May 2024 19:13:18 -0700 X-CSE-ConnectionGUID: AOnKsnMMTGegHBUvvjD3DQ== X-CSE-MsgGUID: dPLSV2xUQTyj85Gji5umqw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,157,1712646000"; d="scan'208,217";a="35055397" X-Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by orviesa004.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 12 May 2024 19:13:17 -0700 X-Received: from fmsmsx610.amr.corp.intel.com (10.18.126.90) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Sun, 12 May 2024 19:13:16 -0700 X-Received: from FMSEDG603.ED.cps.intel.com (10.1.192.133) by fmsmsx610.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35 via Frontend Transport; Sun, 12 May 2024 19:13:16 -0700 X-Received: from NAM02-BN1-obe.outbound.protection.outlook.com (104.47.51.41) by edgegateway.intel.com (192.55.55.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 12 May 2024 19:13:16 -0700 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by MN2PR11MB4584.namprd11.prod.outlook.com (2603:10b6:208:264::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.55; Mon, 13 May 2024 02:13:14 +0000 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::41a4:c775:32e6:76a8]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::41a4:c775:32e6:76a8%4]) with mapi id 15.20.7544.052; Mon, 13 May 2024 02:13:14 +0000 From: "Ni, Ray" To: "Tan, Dun" , "devel@edk2.groups.io" CC: Laszlo Ersek , "Kumar, Rahul R" , Gerd Hoffmann , "Wu, Jiaxin" Subject: Re: [edk2-devel] [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to function Thread-Topic: [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to function Thread-Index: AQHaosIePgx5UvMJk0yeyeoheAbtNLGUcJ1T Date: Mon, 13 May 2024 02:13:14 +0000 Message-ID: References: <20240510100827.1903-1-dun.tan@intel.com> <20240510100827.1903-9-dun.tan@intel.com> In-Reply-To: <20240510100827.1903-9-dun.tan@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|MN2PR11MB4584:EE_ x-ms-office365-filtering-correlation-id: 8bcddbd6-fbbb-41b4-64f7-08dc72f23ed7 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: =?us-ascii?Q?GEPGJPV/lRhQFXGGJNM80TLeMW2W/98yTgii2Rl+/GnBeqitqT/oTUmkq3oV?= =?us-ascii?Q?+OpiUcYlvWJ7GrnOtGOP3hoFNaAeBMQq+TdYMb7qYm/445DjhqjrtcV+UOCC?= =?us-ascii?Q?zSkO/XLH+vLP75uf23MhYqxJtIk2dpZIWrthmSRurL7K4Ulw+kigHM6kCMPm?= =?us-ascii?Q?9ehuy/oeHcoCX4o5wqGThbD10PCN4wNNDuyF4RBLmTJ6igb5QmdSXQUSvhme?= =?us-ascii?Q?/Iw6nO2DBYB1qoNDS63YYORhYP3yCKeIvNXuZr3emj9zCiiVPGywTeZFidL4?= =?us-ascii?Q?xcr+7iv0R2doeOp5wdX4Rp6vb7GepLNiwRDecTBl/T0elDNXUL8NTq0k7lNC?= =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XREium/64qCSEB4gMpzokcZElqkNGM?= =?us-ascii?Q?7CnsxWiSNfd0/xl7MBJFgkJFqTvluOXYflskNTnSaRTcZUM3h67ekPwSko1c?= =?us-ascii?Q?q+EoZCIMa4++tCiZqBBw0g5ogc5YwF490aY2wplYO95UvQ=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?k8v9JmbVglnWU7fkLW/7F1SwBPL0ZBZqYF2W5j5/TUbAwRcHziRAh9GU78s2?= =?us-ascii?Q?Yw2UqiMoD5sD0GYvorPUgFZaWKG1Xm+ttaxWWE7pB2ckybWJQLMuPlbNpfVX?= =?us-ascii?Q?Zs2yKpL8A0lkVM+XbYfYOWuzLh3AnV0jfHZo80mZdtroaOvBTxF53JuT14Ys?= =?us-ascii?Q?NIzstKBsvywXN/j/wiAQVbBp3Zluo+txPjMb6fobaFNS6g29Yu+OS9Ns5b96?= =?us-ascii?Q?bOKyquArWdCeV7MI/tnyhvYzESMpaeoPtvJ4+8dY6EC7G+ei+1pWeCrdIgmG?= =?us-ascii?Q?1CDdcdGfpBz+DozSYsd7KDzStcjI5FqVvs64J531RYaIp5aUGxP0P7jGCBy1?= =?us-ascii?Q?/J/RJJMiuWL4ZFQZUoLD++SMhrWenXxDLoOqNxcMv6QZVj3qIIu885Hc6NVC?= =?us-ascii?Q?EdQUKFy/cB65MKad6DaU5RhXRTe5B14XctmfXAlqHPhWv8yPo8KrXnvWRuNq?= =?us-ascii?Q?mSb1SZwV4hlATVtuZ0H/7GEQDvujbSNvyCa7TMxyvLIZcY1BnhrEm3VIh2mx?= =?us-ascii?Q?5jVFvt4YNyJc+wq4bWjOf+raUnucDIRnIqmAah3JIRmHnzy85r1KrTuAxewf?= =?us-ascii?Q?r8B6fTw7D4pQ3HiMbaZPwbz/ysYYCwJkti0iTji1jO4vpikuJkq37BH0WPvJ?= =?us-ascii?Q?/zx/RFZzqcXP74nJ0BOjOPT7xTcRci+K9LKxKl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ag+1b8dA=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN6PR11MB8244.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8bcddbd6-fbbb-41b4-64f7-08dc72f23ed7 X-MS-Exchange-CrossTenant-originalarrivaltime: 13 May 2024 02:13:14.1710 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: vEDcTbXZkFneTKPEuJotxv8jjC1vQ5+jwGpLcIB9IihqmNdAkvxJVm0Usk7zy8QHWqA9z9RAEsgddPRJXLTrqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR11MB4584 X-OriginatorOrg: intel.com 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: Sun, 12 May 2024 19:13:17 -0700 Resent-From: ray.ni@intel.com Reply-To: devel@edk2.groups.io,ray.ni@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: E65a02OImCB3X8bEtMUygmVzx7686176AA= Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MN6PR11MB82443B5A0EC23E1F5B6DCA7A8CE22MN6PR11MB8244namp_" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=Z8TcmwCK; 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 --_000_MN6PR11MB82443B5A0EC23E1F5B6DCA7A8CE22MN6PR11MB8244namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ray Ni Thanks, Ray ________________________________ From: Tan, Dun Sent: Friday, May 10, 2024 18:08 To: devel@edk2.groups.io Cc: Ni, Ray ; Laszlo Ersek ; Kumar, Ra= hul R ; Gerd Hoffmann ; Wu, Jia= xin Subject: [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to function 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/M= pInitLib/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 =3D 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 G= CD + // service. + // + Status =3D 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 =3D &CpuMpData->AddressMap; + if (CpuMpData->UseSevEsAPMethod) { + // + // 64-bit AMD processors with SEV-ES + // + Address =3D BASE_4GB - 1; + ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddressAmdSev; + ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSizeAmdSev; + } else { + // + // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-b= it AMD processors without SEV-ES + // + Address =3D MAX_ADDRESS; + ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddressGeneric; + ApLoopFuncSize =3D 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 =3D EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SI= ZE); + FuncPages =3D EFI_SIZE_TO_PAGES (ApLoopFuncSize); + + AllocateApLoopCodeBuffer (StackPages + FuncPages, &Address); + ASSERT (Address !=3D 0); + + Cr0.UintN =3D AsmReadCr0 (); + if (Cr0.Bits.PG !=3D 0) { + // + // Make sure that the buffer memory is executable if NX protection is = enabled + // for EfiReservedMemoryType. + // + RemoveNxprotection (Address, EFI_PAGES_TO_SIZE (FuncPages)); + } + + mReservedTopOfApStack =3D (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages= +FuncPages); + ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) =3D= =3D 0); + mReservedApLoop.Data =3D (VOID *)(UINTN)Address; + ASSERT (mReservedApLoop.Data !=3D NULL); + CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize); + if (!CpuMpData->UseSevEsAPMethod) { + // + // processors without SEV-ES and paging is enabled + // + mApPageTable =3D 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 =3D &CpuMpData->AddressMap; - if (CpuMpData->UseSevEsAPMethod) { - // - // 64-bit AMD processors with SEV-ES - // - Address =3D BASE_4GB - 1; - ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddressAmdSev; - ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSizeAmdSev; - } else { - // - // Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-b= it AMD processors without SEV-ES - // - Address =3D MAX_ADDRESS; - ApLoopFunc =3D AddressMap->RelocateApLoopFuncAddressGeneric; - ApLoopFuncSize =3D 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 =3D EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SI= ZE); - FuncPages =3D EFI_SIZE_TO_PAGES (ApLoopFuncSize); - - Status =3D gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - StackPages + FuncPages, - &Address - ); - ASSERT_EFI_ERROR (Status); - - // - // Make sure that the buffer memory is executable if NX protection is en= abled - // for EfiReservedMemoryType. - // - // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE G= CD - // service. - // - Status =3D gDS->GetMemorySpaceDescriptor (Address, &MemDesc); - if (!EFI_ERROR (Status)) { - gDS->SetMemorySpaceAttributes ( - Address, - EFI_PAGES_TO_SIZE (FuncPages), - MemDesc.Attributes & (~EFI_MEMORY_XP) - ); - } - - mReservedTopOfApStack =3D (UINTN)Address + EFI_PAGES_TO_SIZE (StackPages= +FuncPages); - ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) =3D= =3D 0); - mReservedApLoop.Data =3D (VOID *)(UINTN)Address; - ASSERT (mReservedApLoop.Data !=3D NULL); - CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize); - if (!CpuMpData->UseSevEsAPMethod) { - // - // processors without SEV-ES - // - mApPageTable =3D CreatePageTable ( - (UINTN)Address, - EFI_PAGES_TO_SIZE (StackPages+FuncPages) - ); - } + PrepareApLoopCode (CpuMpData); Status =3D gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, -- 2.31.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 (#118839): https://edk2.groups.io/g/devel/message/118839 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] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --_000_MN6PR11MB82443B5A0EC23E1F5B6DCA7A8CE22MN6PR11MB8244namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Reviewed-by: Ray Ni <ray.ni@intel.com>

Thanks,
Ray

From: Tan, Dun <dun.tan@= intel.com>
Sent: Friday, May 10, 2024 18:08
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Ni, Ray <ray.ni@intel.com>; Laszlo Ersek <lersek@redhat= .com>; Kumar, Rahul R <rahul.r.kumar@intel.com>; Gerd Hoffmann <= ;kraxel@redhat.com>; Wu, Jiaxin <jiaxin.wu@intel.com>
Subject: [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to fun= ction
 
Abstract some DxeMpLib code to function in this co= mmit.
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 <dun.tan@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
---
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 220 +++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++----------------------------------= -----------------------------------------------
 1 file changed, 139 insertions(+), 81 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/M= pInitLib/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    Nu= mber 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 =3D gBS->AllocatePages (
+            &n= bsp;     AllocateMaxAddress,
+            &n= bsp;     EfiReservedMemoryType,
+            &n= bsp;     Pages,
+            &n= bsp;     Address
+            &n= bsp;     );
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  Remove Nx protection for the range specific by BaseAddress and Leng= th.
+
+  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         &nb= sp;            = 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 =3D gDS->GetMemorySpaceDescriptor (BaseAddress, &MemD= esc);
+  if (!EFI_ERROR (Status)) {
+    gDS->SetMemorySpaceAttributes (
+           BaseAddress,<= br> +           Length,
+           MemDesc.Attri= butes & (~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          &n= bsp;         *ApLoopFunc;
+  UINTN          &n= bsp;         ApLoopFuncSize;
+  UINTN          &n= bsp;         StackPages;
+  UINTN          &n= bsp;         FuncPages;
+  IA32_CR0          = ;       Cr0;
+
+  AddressMap =3D &CpuMpData->AddressMap;
+  if (CpuMpData->UseSevEsAPMethod) {
+    //
+    // 64-bit AMD processors with SEV-ES
+    //
+    Address        =3D B= ASE_4GB - 1;
+    ApLoopFunc     =3D AddressMap->R= elocateApLoopFuncAddressAmdSev;
+    ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSiz= eAmdSev;
+  } else {
+    //
+    // Intel processors (32-bit or 64-bit), 32-bit AMD proc= essors, or 64-bit AMD processors without SEV-ES
+    //
+    Address        =3D M= AX_ADDRESS;
+    ApLoopFunc     =3D AddressMap->R= elocateApLoopFuncAddressGeneric;
+    ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSiz= eGeneric;
+  }
+
+  //
+  // Avoid APs access invalid buffer data which allocated by BootServ= ices,
+  // 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 =3D EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_= STACK_SIZE);
+  FuncPages  =3D EFI_SIZE_TO_PAGES (ApLoopFuncSize);
+
+  AllocateApLoopCodeBuffer (StackPages + FuncPages, &Address); +  ASSERT (Address !=3D 0);
+
+  Cr0.UintN =3D AsmReadCr0 ();
+  if (Cr0.Bits.PG !=3D 0) {
+    //
+    // Make sure that the buffer memory is executable if NX= protection is enabled
+    // for EfiReservedMemoryType.
+    //
+    RemoveNxprotection (Address, EFI_PAGES_TO_SIZE (FuncPag= es));
+  }
+
+  mReservedTopOfApStack =3D (UINTN)Address + EFI_PAGES_TO_SIZE (Stack= Pages+FuncPages);
+  ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - = 1)) =3D=3D 0);
+  mReservedApLoop.Data =3D (VOID *)(UINTN)Address;
+  ASSERT (mReservedApLoop.Data !=3D NULL);
+  CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize);
+  if (!CpuMpData->UseSevEsAPMethod) {
+    //
+    // processors without SEV-ES and paging is enabled
+    //
+    mApPageTable =3D CreatePageTable (
+            &n= bsp;        (UINTN)Address,
+            &n= bsp;        EFI_PAGES_TO_SIZE (StackPage= s+FuncPages)
+            &n= bsp;        );
+  }
+}
+
 /**
   Callback function for ExitBootServices.
 
@@ -477,16 +615,10 @@ InitMpGlobalData (
   )
 {
   EFI_STATUS        &nbs= p;            &= nbsp; Status;
-  EFI_PHYSICAL_ADDRESS        = ;     Address;
   UINTN         &nb= sp;            =       Index;
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR  MemDesc;
   UINTN         &nb= sp;            =       StackBase;
   CPU_INFO_IN_HOB        = ;          *CpuInfoInHob;
-  MP_ASSEMBLY_ADDRESS_MAP       &n= bsp;  *AddressMap;
-  UINT8          &n= bsp;            = ;     *ApLoopFunc;
-  UINTN          &n= bsp;            = ;     ApLoopFuncSize;
-  UINTN          &n= bsp;            = ;     StackPages;
-  UINTN          &n= bsp;            = ;     FuncPages;
 
   SaveCpuMpData (CpuMpData);
 
@@ -541,81 +673,7 @@ InitMpGlobalData (
     }
   }
 
-  AddressMap =3D &CpuMpData->AddressMap;
-  if (CpuMpData->UseSevEsAPMethod) {
-    //
-    // 64-bit AMD processors with SEV-ES
-    //
-    Address        =3D B= ASE_4GB - 1;
-    ApLoopFunc     =3D AddressMap->R= elocateApLoopFuncAddressAmdSev;
-    ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSiz= eAmdSev;
-  } else {
-    //
-    // Intel processors (32-bit or 64-bit), 32-bit AMD proc= essors, or 64-bit AMD processors without SEV-ES
-    //
-    Address        =3D M= AX_ADDRESS;
-    ApLoopFunc     =3D AddressMap->R= elocateApLoopFuncAddressGeneric;
-    ApLoopFuncSize =3D AddressMap->RelocateApLoopFuncSiz= eGeneric;
-  }
-
-  //
-  // Avoid APs access invalid buffer data which allocated by BootServ= ices,
-  // 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 =3D EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_= STACK_SIZE);
-  FuncPages  =3D EFI_SIZE_TO_PAGES (ApLoopFuncSize);
-
-  Status =3D gBS->AllocatePages (
-            &n= bsp;     AllocateMaxAddress,
-            &n= bsp;     EfiReservedMemoryType,
-            &n= bsp;     StackPages + FuncPages,
-            &n= bsp;     &Address
-            &n= bsp;     );
-  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 =3D gDS->GetMemorySpaceDescriptor (Address, &MemDesc)= ;
-  if (!EFI_ERROR (Status)) {
-    gDS->SetMemorySpaceAttributes (
-           Address,
-           EFI_PAGES_TO_= SIZE (FuncPages),
-           MemDesc.Attri= butes & (~EFI_MEMORY_XP)
-           );
-  }
-
-  mReservedTopOfApStack =3D (UINTN)Address + EFI_PAGES_TO_SIZE (Stack= Pages+FuncPages);
-  ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - = 1)) =3D=3D 0);
-  mReservedApLoop.Data =3D (VOID *)(UINTN)Address;
-  ASSERT (mReservedApLoop.Data !=3D NULL);
-  CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize);
-  if (!CpuMpData->UseSevEsAPMethod) {
-    //
-    // processors without SEV-ES
-    //
-    mApPageTable =3D CreatePageTable (
-            &n= bsp;        (UINTN)Address,
-            &n= bsp;        EFI_PAGES_TO_SIZE (StackPage= s+FuncPages)
-            &n= bsp;        );
-  }
+  PrepareApLoopCode (CpuMpData);
 
   Status =3D gBS->CreateEvent (
            &nb= sp;      EVT_TIMER | EVT_NOTIFY_SIGNAL,
--
2.31.1.windows.1

_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#118839) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--_000_MN6PR11MB82443B5A0EC23E1F5B6DCA7A8CE22MN6PR11MB8244namp_--