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 A41B9D80229 for ; Mon, 13 May 2024 02:23:14 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=TbTMNonjWNApRkFcLtFniSbIaDaMWOJQ1xo+4OE7SVk=; 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=1715566993; v=1; b=wga25X1jG3tKAkHafpZmbUTehn8WnuF+tHgYX2Mc3ZxpgmuEHUjMLX28cesii2nd7a03Ihlv iCOSPh4mnUpw8lC0yllM7G0EVuBBwxW1fGrNzvmTLJbalzZkXqvVLSY9FvS+pFIg/ifPJPZ/qCd Eqt6aOGlH6Th3DzJ6prskWd0YwmB7S4KXT4Dq3kO6CW1VUyn+QEg6MuCSbrGv9Y9S5uZODXxOdU YmGOf5FXC9sxC6h/VWqzA5UZHoMW1fdviHb4ifh6Q8OAWAxneAKRY3rWTeBTw0zjb3uE2jzYAp+ uCM5ez0WasbMTCgIBS5H4YByAV7me9kdQxkRpCQ4WDtXQ== X-Received: by 127.0.0.2 with SMTP id CUAnYY7687511xN3A3qOCuSn; Sun, 12 May 2024 19:23:13 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by mx.groups.io with SMTP id smtpd.web10.51060.1715566991733567618 for ; Sun, 12 May 2024 19:23:11 -0700 X-CSE-ConnectionGUID: 9qurEtDvTZq0As8/qVMqZg== X-CSE-MsgGUID: MEeRoZ5TRUC1Yypjy5gdhg== X-IronPort-AV: E=McAfee;i="6600,9927,11071"; a="22155165" X-IronPort-AV: E=Sophos;i="6.08,157,1712646000"; d="scan'208,217";a="22155165" X-Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 May 2024 19:23:12 -0700 X-CSE-ConnectionGUID: sBSE/4BJTaeABNlxM7etmg== X-CSE-MsgGUID: lx1chmNJQ8q8Wtc3Kryu1w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,157,1712646000"; d="scan'208,217";a="30270266" X-Received: from orsmsx603.amr.corp.intel.com ([10.22.229.16]) by fmviesa010.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 12 May 2024 19:23:10 -0700 X-Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX603.amr.corp.intel.com (10.22.229.16) 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:23:09 -0700 X-Received: from orsedg603.ED.cps.intel.com (10.7.248.4) by orsmsx610.amr.corp.intel.com (10.22.229.23) 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:23:09 -0700 X-Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.171) by edgegateway.intel.com (134.134.137.100) 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:23:09 -0700 X-Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by DS0PR11MB7927.namprd11.prod.outlook.com (2603:10b6:8:fd::14) 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:23:06 +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:23:06 +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 10/18] UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib Thread-Topic: [PATCH 10/18] UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib Thread-Index: AQHaosIk7d4/UW13JkuEGzuUOawdRbGUc1Ms Date: Mon, 13 May 2024 02:23:06 +0000 Message-ID: References: <20240510100827.1903-1-dun.tan@intel.com> <20240510100827.1903-11-dun.tan@intel.com> In-Reply-To: <20240510100827.1903-11-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_|DS0PR11MB7927:EE_ x-ms-office365-filtering-correlation-id: 1922a6dd-e5c3-4671-4f76-08dc72f3a021 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: =?us-ascii?Q?Op1jA/eY+EgwhZYzwXOmP/4/q5XIkuJIwddx6NbtMZoZO7gxF2VqEy/m+I+6?= =?us-ascii?Q?h4KUzmhuqLfbmq4yguAqr2bXjaMeONFqyIZPS6UtEBdNuC4AoUMlGrzoBGEK?= =?us-ascii?Q?5pjreLYTxAiaSJcgY0tZ4rMPSFcx31d4B/u9jGdZfPA7SsWOOMznnej8r+j+?= =?us-ascii?Q?5lHQfCE8P91orzW0uh3c5VnHx1aPb9NOtBkGfOVvV32XlA6zrbQYv+2auJPS?= =?us-ascii?Q?DklptDKxSNP0aZc2A3xU+iV3Tsna3nB2qQdlsNnU8vih7efz/iPshJQbyhU8?= =?us-ascii?Q?XGmzfmSJ23x49hn3S0SYAGYHshsGEKx4tpqqS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jG3sLV+4sEecaBYgXRktUVRjSBBuMoOHDisJoIO8Fkcy3NgvlA?= =?us-ascii?Q?1Clew62ZeaVNVwASl6vej3SDX+aue7fmv3NzoGpNu6Gu0CCsvhE8XouVSO1x?= =?us-ascii?Q?CyehpTOptuPM6sOx4Xo6C1eWoFm8wRcccmSDeK8m6n4f+g=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?so4T90n9yAJto6wSwCPe4DmKvirI0e8IoxyWMTT5qIBUYeov46mX61QaUmhk?= =?us-ascii?Q?EGynBN+DWKlrjvMsKeONJicwr2mCK9PJITgnz2MpJvccParP0hxoFShJpTAY?= =?us-ascii?Q?xTE/mlZLn//gIEYjXKXgYmYRSyif87xFnTyZRjJgWXkWROPF2yCxr6j310JF?= =?us-ascii?Q?fhOHKQxwxvRAXQKMrFnJW4qPZBMtqTQ1g1SLCe039c6DX3juVNcBgup+v5o9?= =?us-ascii?Q?lr14yrRGdr9lvj7X2fsIOLDeoXLKfPrFMyN1ENqrM7nVdIKorgwSot3SPn1D?= =?us-ascii?Q?0eCdKpwAh++K5UQ7/i1hLUQ6BU/njMann9EDOHb4PwrZchvGiXGY19Eb3hid?= =?us-ascii?Q?SZxTyVwrMJL/p5lqDVSpjM+L1ycI3VVWUSwIuppZwaH1X3excRTGte28F2Yi?= =?us-ascii?Q?MonLov+8nOKkvoizh/aCfL3Yit0y5OUjWlHHFyfBs5FkOngRea0CRTTn1dqf?= =?us-ascii?Q?UVrXzsV8yTwkSz3LsAOqgLRw7fnCKvrFCRNF9Ky7Jx2DU420QHCq+fRIJTCV?= =?us-ascii?Q?Z0SxFebx5Ox+c1ZYsjPBkJI+KrTNnBzoMyZ/72L0abjus98NMzVW7my2858P?= =?us-ascii?Q?ynukFNhT5uw7TGs694uyYck5W+QNT5Ijd6GhPLgUTzu7RZxyUkEL6xlPJkVV?= =?us-ascii?Q?PS03St4tdA3idkqRLWkaG0gl33Ic3ft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YOEFEkBwtMUJ23VXg=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: 1922a6dd-e5c3-4671-4f76-08dc72f3a021 X-MS-Exchange-CrossTenant-originalarrivaltime: 13 May 2024 02:23:06.8327 (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: 5pwYrLLyneJ8w2Psnl2g8B2EFqUyzLZZWsJqmUpnBtpnsT/qVoxajqPuW3JyWzZU09kRrPava8InareBdEPcEQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR11MB7927 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:23:11 -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: iGCwVxqe70jvwfixJ0OIZNAbx7686176AA= Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MN6PR11MB82440BC30FBB6ADCF7B27C198CE22MN6PR11MB8244namp_" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=wga25X1j; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none) --_000_MN6PR11MB82440BC30FBB6ADCF7B27C198CE22MN6PR11MB8244namp_ 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 10/18] UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLi= b In this commit, change PeiMpLib to install callback of gEdkiiEndOfS3ResumeGuid to relocate AP to new safe buffer. The gEdkiiEndOfS3ResumeGuid is installed in S3Resume.c before jmping to OS waking vector. Previously, code in CpuS3.c of PiSmmCpuDxe driver will prepare the new safe buffer for AP and place AP in hlt loop state. With this code change, we can remove the Machine Instructions of mApHltLoopCode in PiSmmCpuDxe. Also we can reuse the related code in DxeMpLib for PeiMpLib. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Gerd Hoffmann Cc: Jiaxin Wu --- UefiCpuPkg/Library/MpInitLib/MpLib.h | 3 +++ UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf | 4 ++++ UefiCpuPkg/Library/MpInitLib/PeiMpLib.c | 152 ++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index 11e0d2661f..3efd913395 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -68,6 +69,8 @@ // #define DEFAULT_MAX_MICROCODE_PATCH_NUM 8 +#define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull + // // Data structure for microcode patch information // diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf b/UefiCpuPkg/Lib= rary/MpInitLib/PeiMpInitLib.inf index e31e34b6f9..8736690348 100644 --- a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf +++ b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf @@ -25,10 +25,12 @@ [Sources.IA32] Ia32/AmdSev.c Ia32/MpFuncs.nasm + Ia32/CreatePageTable.c [Sources.X64] X64/AmdSev.c X64/MpFuncs.nasm + X64/CreatePageTable.c [Sources.IA32, Sources.X64] AmdSev.c @@ -64,6 +66,7 @@ LocalApicLib MicrocodeLib MtrrLib + CpuPageTableLib [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase ## CONS= UMES @@ -87,6 +90,7 @@ gEdkiiS3SmmInitDoneGuid gEdkiiMicrocodePatchHobGuid gGhcbApicIdsGuid ## SOMETIMES_CONSUMES + gEdkiiEndOfS3ResumeGuid [Guids.LoongArch64] gProcessorResourceHobGuid ## SOMETIMES_CONSUMES ## HOB diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c b/UefiCpuPkg/Library/M= pInitLib/PeiMpLib.c index 4d3acb491f..deb5fc3aac 100644 --- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c @@ -9,6 +9,7 @@ #include "MpLib.h" #include #include +#include #include STATIC UINT64 mSevEsPeiWakeupBuffer =3D BASE_1MB; @@ -449,6 +450,47 @@ BuildMicrocodeCacheHob ( return; } +/** + S3 SMM Init Done notification function. + + @param PeiServices Indirect reference to the PEI Services Table. + @param NotifyDesc Address of the notification descriptor data str= ucture. + @param InvokePpi Address of the PPI that was invoked. + + @retval EFI_SUCCESS The function completes successfully. + +**/ +EFI_STATUS +EFIAPI +NotifyOnEndOfS3Resume ( + IN EFI_PEI_SERVICES **PeiServices, + IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc, + IN VOID *InvokePpi + ) +{ + CPU_MP_DATA *CpuMpData; + + CpuMpData =3D GetCpuMpData (); + mNumberToFinish =3D CpuMpData->CpuCount - 1; + WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE); + while (mNumberToFinish > 0) { + CpuPause (); + } + + DEBUG ((DEBUG_INFO, "%a() done!\n", __func__)); + + return EFI_SUCCESS; +} + +// +// Global function +// +EFI_PEI_NOTIFY_DESCRIPTOR mEndOfS3ResumeNotifyDesc =3D { + EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINAT= E_LIST, + &gEdkiiEndOfS3ResumeGuid, + NotifyOnEndOfS3Resume +}; + /** Initialize global data for MP support. @@ -463,12 +505,16 @@ InitMpGlobalData ( BuildMicrocodeCacheHob (CpuMpData); SaveCpuMpData (CpuMpData); + PrepareApLoopCode (CpuMpData); /// /// Install Notify /// Status =3D PeiServicesNotifyPpi (&mS3SmmInitDoneNotifyDesc); ASSERT_EFI_ERROR (Status); + + Status =3D PeiServicesNotifyPpi (&mEndOfS3ResumeNotifyDesc); + ASSERT_EFI_ERROR (Status); } /** @@ -815,3 +861,109 @@ PlatformShadowMicrocode ( return EFI_SUCCESS; } + +/** + 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 PeiServicesAllocatePages (EfiACPIMemoryNVS, Pages, Address); + if (EFI_ERROR (Status)) { + *Address =3D 0; + } +} + +/** + 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; + UINTN PageTable; + EFI_PHYSICAL_ADDRESS Buffer; + UINTN BufferSize; + IA32_MAP_ATTRIBUTE MapAttribute; + IA32_MAP_ATTRIBUTE MapMask; + PAGING_MODE PagingMode; + IA32_CR4 Cr4; + BOOLEAN Page5LevelSupport; + UINT32 RegEax; + BOOLEAN Page1GSupport; + CPUID_EXTENDED_CPU_SIG_EDX RegEdx; + + if (sizeof (UINTN) =3D=3D sizeof (UINT64)) { + // + // Check Page5Level Support or not. + // + Cr4.UintN =3D AsmReadCr4 (); + Page5LevelSupport =3D (Cr4.Bits.LA57 ? TRUE : FALSE); + + // + // Check Page1G Support or not. + // + Page1GSupport =3D FALSE; + AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL); + if (RegEax >=3D CPUID_EXTENDED_CPU_SIG) { + AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &RegEdx.Uint32); + if (RegEdx.Bits.Page1GB !=3D 0) { + Page1GSupport =3D TRUE; + } + } + + // + // Decide Paging Mode according Page5LevelSupport & Page1GSupport. + // + if (Page5LevelSupport) { + PagingMode =3D Page1GSupport ? Paging5Level1GB : Paging5Level; + } else { + PagingMode =3D Page1GSupport ? Paging4Level1GB : Paging4Level; + } + } else { + PagingMode =3D PagingPae; + } + + MapAttribute.Uint64 =3D 0; + MapMask.Uint64 =3D 0; + MapMask.Bits.Nx =3D 1; + PageTable =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; + BufferSize =3D 0; + + // + // Get required buffer size for changing the pagetable. + // + Status =3D PageTableMap (&PageTable, PagingMode, 0, &BufferSize, BaseAdd= ress, Length, &MapAttribute, &MapMask, NULL); + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + // + // Allocate required Buffer. + // + Status =3D PeiServicesAllocatePages ( + EfiBootServicesData, + EFI_SIZE_TO_PAGES (BufferSize), + &Buffer + ); + ASSERT_EFI_ERROR (Status); + Status =3D PageTableMap (&PageTable, PagingMode, (VOID *)(UINTN)Buffer= , &BufferSize, BaseAddress, Length, &MapAttribute, &MapMask, NULL); + } + + ASSERT_EFI_ERROR (Status); + AsmWriteCr3 (PageTable); +} -- 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 (#118841): https://edk2.groups.io/g/devel/message/118841 Mute This Topic: https://groups.io/mt/106018135/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_MN6PR11MB82440BC30FBB6ADCF7B27C198CE22MN6PR11MB8244namp_ 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 10/18] UefiCpuPkg:Relocate AP to new safe buffer in = PeiMpLib
 
In this commit, change PeiMpLib to install callbac= k
of gEdkiiEndOfS3ResumeGuid to relocate AP to new safe
buffer. The gEdkiiEndOfS3ResumeGuid is installed in
S3Resume.c before jmping to OS waking vector.

Previously, code in CpuS3.c of PiSmmCpuDxe driver will
prepare the new safe buffer for AP and place AP in hlt
loop state. With this code change, we can remove the
Machine Instructions of mApHltLoopCode in PiSmmCpuDxe.
Also we can reuse the related code in DxeMpLib for
PeiMpLib.

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/MpLib.h     &nb= sp;    |   3 +++
 UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf |   4 ++++  UefiCpuPkg/Library/MpInitLib/PeiMpLib.c     =   | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++
 3 files changed, 159 insertions(+)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h
index 11e0d2661f..3efd913395 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -33,6 +33,7 @@
 #include <Library/HobLib.h>
 #include <Library/PcdLib.h>
 #include <Library/MicrocodeLib.h>
+#include <Library/CpuPageTableLib.h>
 #include <ConfidentialComputingGuestAttr.h>
 
 #include <Register/Amd/Fam17Msr.h>
@@ -68,6 +69,8 @@
 //
 #define DEFAULT_MAX_MICROCODE_PATCH_NUM  8
 
+#define PAGING_4K_ADDRESS_MASK_64  0x000FFFFFFFFFF000ull
+
 //
 // Data structure for microcode patch information
 //
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf b/UefiCpuPkg/Lib= rary/MpInitLib/PeiMpInitLib.inf
index e31e34b6f9..8736690348 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
@@ -25,10 +25,12 @@
 [Sources.IA32]
   Ia32/AmdSev.c
   Ia32/MpFuncs.nasm
+  Ia32/CreatePageTable.c
 
 [Sources.X64]
   X64/AmdSev.c
   X64/MpFuncs.nasm
+  X64/CreatePageTable.c
 
 [Sources.IA32, Sources.X64]
   AmdSev.c
@@ -64,6 +66,7 @@
   LocalApicLib
   MicrocodeLib
   MtrrLib
+  CpuPageTableLib
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase   &n= bsp;            = ;       ## CONSUMES
@@ -87,6 +90,7 @@
   gEdkiiS3SmmInitDoneGuid
   gEdkiiMicrocodePatchHobGuid
   gGhcbApicIdsGuid       &nbs= p;            &= nbsp;  ## SOMETIMES_CONSUMES
+  gEdkiiEndOfS3ResumeGuid
 
 [Guids.LoongArch64]
   gProcessorResourceHobGuid      &= nbsp;       ## SOMETIMES_CONSUMES  ## HO= B
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c b/UefiCpuPkg/Library/M= pInitLib/PeiMpLib.c
index 4d3acb491f..deb5fc3aac 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
@@ -9,6 +9,7 @@
 #include "MpLib.h"
 #include <Library/PeiServicesLib.h>
 #include <Guid/S3SmmInitDone.h>
+#include <Guid/EndOfS3Resume.h>
 #include <Ppi/ShadowMicrocode.h>
 
 STATIC UINT64  mSevEsPeiWakeupBuffer =3D BASE_1MB;
@@ -449,6 +450,47 @@ BuildMicrocodeCacheHob (
   return;
 }
 
+/**
+  S3 SMM Init Done notification function.
+
+  @param  PeiServices      Indirect ref= erence to the PEI Services Table.
+  @param  NotifyDesc       Address= of the notification descriptor data structure.
+  @param  InvokePpi        Ad= dress of the PPI that was invoked.
+
+  @retval EFI_SUCCESS      The function comp= letes successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+NotifyOnEndOfS3Resume (
+  IN  EFI_PEI_SERVICES       =     **PeiServices,
+  IN  EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDesc,
+  IN  VOID         =             &nb= sp; *InvokePpi
+  )
+{
+  CPU_MP_DATA  *CpuMpData;
+
+  CpuMpData       =3D GetCpuMpData ();<= br> +  mNumberToFinish =3D CpuMpData->CpuCount - 1;
+  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
+  while (mNumberToFinish > 0) {
+    CpuPause ();
+  }
+
+  DEBUG ((DEBUG_INFO, "%a() done!\n", __func__));
+
+  return EFI_SUCCESS;
+}
+
+//
+// Global function
+//
+EFI_PEI_NOTIFY_DESCRIPTOR  mEndOfS3ResumeNotifyDesc =3D {
+  EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TER= MINATE_LIST,
+  &gEdkiiEndOfS3ResumeGuid,
+  NotifyOnEndOfS3Resume
+};
+
 /**
   Initialize global data for MP support.
 
@@ -463,12 +505,16 @@ InitMpGlobalData (
 
   BuildMicrocodeCacheHob (CpuMpData);
   SaveCpuMpData (CpuMpData);
+  PrepareApLoopCode (CpuMpData);
 
   ///
   /// Install Notify
   ///
   Status =3D PeiServicesNotifyPpi (&mS3SmmInitDoneNotifyDesc= );
   ASSERT_EFI_ERROR (Status);
+
+  Status =3D PeiServicesNotifyPpi (&mEndOfS3ResumeNotifyDesc); +  ASSERT_EFI_ERROR (Status);
 }
 
 /**
@@ -815,3 +861,109 @@ PlatformShadowMicrocode (
 
   return EFI_SUCCESS;
 }
+
+/**
+  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 PeiServicesAllocatePages (EfiACPIMemoryNVS, Pages, Addre= ss);
+  if (EFI_ERROR (Status)) {
+    *Address =3D 0;
+  }
+}
+
+/**
+  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;
+  UINTN          &n= bsp;            Page= Table;
+  EFI_PHYSICAL_ADDRESS        Buff= er;
+  UINTN          &n= bsp;            Buff= erSize;
+  IA32_MAP_ATTRIBUTE        &= nbsp; MapAttribute;
+  IA32_MAP_ATTRIBUTE        &= nbsp; MapMask;
+  PAGING_MODE         &n= bsp;       PagingMode;
+  IA32_CR4          = ;          Cr4;
+  BOOLEAN          =            Page5LevelSupp= ort;
+  UINT32          &= nbsp;           RegEax; +  BOOLEAN          =            Page1GSupport;=
+  CPUID_EXTENDED_CPU_SIG_EDX  RegEdx;
+
+  if (sizeof (UINTN) =3D=3D sizeof (UINT64)) {
+    //
+    // Check Page5Level Support or not.
+    //
+    Cr4.UintN       &nbs= p; =3D AsmReadCr4 ();
+    Page5LevelSupport =3D (Cr4.Bits.LA57 ? TRUE : FALSE); +
+    //
+    // Check Page1G Support or not.
+    //
+    Page1GSupport =3D FALSE;
+    AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, N= ULL, NULL);
+    if (RegEax >=3D CPUID_EXTENDED_CPU_SIG) {
+      AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NUL= L, NULL, &RegEdx.Uint32);
+      if (RegEdx.Bits.Page1GB !=3D 0) {
+        Page1GSupport =3D TRUE;
+      }
+    }
+
+    //
+    // Decide Paging Mode according Page5LevelSupport &= Page1GSupport.
+    //
+    if (Page5LevelSupport) {
+      PagingMode =3D Page1GSupport ? Paging5Level= 1GB : Paging5Level;
+    } else {
+      PagingMode =3D Page1GSupport ? Paging4Level= 1GB : Paging4Level;
+    }
+  } else {
+    PagingMode =3D PagingPae;
+  }
+
+  MapAttribute.Uint64 =3D 0;
+  MapMask.Uint64      =3D 0;
+  MapMask.Bits.Nx     =3D 1;
+  PageTable         &nbs= p; =3D AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64;
+  BufferSize          = =3D 0;
+
+  //
+  // Get required buffer size for changing the pagetable.
+  //
+  Status =3D PageTableMap (&PageTable, PagingMode, 0, &Buffer= Size, BaseAddress, Length, &MapAttribute, &MapMask, NULL);
+  if (Status =3D=3D EFI_BUFFER_TOO_SMALL) {
+    //
+    // Allocate required Buffer.
+    //
+    Status =3D PeiServicesAllocatePages (
+            &n= bsp;  EfiBootServicesData,
+            &n= bsp;  EFI_SIZE_TO_PAGES (BufferSize),
+            &n= bsp;  &Buffer
+            &n= bsp;  );
+    ASSERT_EFI_ERROR (Status);
+    Status =3D PageTableMap (&PageTable, PagingMode, (V= OID *)(UINTN)Buffer, &BufferSize, BaseAddress, Length, &MapAttribut= e, &MapMask, NULL);
+  }
+
+  ASSERT_EFI_ERROR (Status);
+  AsmWriteCr3 (PageTable);
+}
--
2.31.1.windows.1

_._,_._,_

Groups.io Links:

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

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

_._,_._,_
--_000_MN6PR11MB82440BC30FBB6ADCF7B27C198CE22MN6PR11MB8244namp_--