From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.5468.1686219390604968631 for ; Thu, 08 Jun 2023 03:16:31 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=OsWROwZj; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: ray.ni@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686219390; x=1717755390; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=foA/vQ7VtPdVTG0lnMI9GZoWlpIRiSXQf15yTUxh5FU=; b=OsWROwZjccdu+nozS6ACv2qXJ1dawEjhdw9S6Uobx7gUFiO9+//DzIw4 WoD3bKcf/FQG6h/BZemPj+zk2rBdhTUf9pnMvVpYiMcUFeRv/sN1JshWt XhPEEuF9bn34E2LhTVVFcvy4Iu5OCAwKM0yRS5ptFWvdinczfEDYNfZDv gcKpOdrUuF6YEV3pqJFtT2KFBkjuwPd7QrQfbibDLbl86dd1Wur8KUBJX ciwPVVZw2DPq0qTuTd88KuFH6zgdNRMraydN/02a2+Cw6XhEFQcflGsBv AS45nLsY3+I89Rz94698y/RZh/3+7NdY2abkCXPVoJdFLBTuYAqHG4TrI A==; X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="420832887" X-IronPort-AV: E=Sophos;i="6.00,226,1681196400"; d="scan'208";a="420832887" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jun 2023 03:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="660327326" X-IronPort-AV: E=Sophos;i="6.00,226,1681196400"; d="scan'208";a="660327326" Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by orsmga003.jf.intel.com with ESMTP; 08 Jun 2023 03:16:29 -0700 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.23; Thu, 8 Jun 2023 03:16:29 -0700 Received: from fmsedg602.ED.cps.intel.com (10.1.192.136) 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.23 via Frontend Transport; Thu, 8 Jun 2023 03:16:29 -0700 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.168) by edgegateway.intel.com (192.55.55.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.23; Thu, 8 Jun 2023 03:16:28 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QvVpucm48do3X5KvOv5bc79bbBk+KoUJQSf3Gfx5Iu0w+CPkSJhMQzZevMj60udXEt+lvcC7D+xrX6UCujaXaxJ5+h27mw9z5ypdLk6IlD8LzoxfMJqaQCEKjRShXR1kk29TuCdMr2T5Evh86IY96+aMSgmAbrsryr1qYhJcQAfK94VVVsXojOTbdhLAheWEmoAPXZxdWeyY55d7Akq1h6825PbNhWN0CSO4zab6hHUKE2Ox+WCgY2xH0IQwC6XOX5aGb3HRU1ZcQ1Hra9bpjrWi6CaqL3C2FAwEbV/RJ5My7pS+79J0liIHe+wP2oLYYBCkC0bNmLNl7BYA3n096A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=6pi/p0aXI4YBP6wqkFsmVugXKruOtzHkYfNT0A1Msfw=; b=ULQ/jhQtGy50ke2R103EtoDRHvcat75hyYv9nCY0iENETPB2QI9MhiUXypepLlUKyMbzrJTRKRx9sArI7YthoUwquhBLtSNjuSSHmeqv/Iyt+Xqkm98q93sTxfn4WHZGXPnhtVBsDM4PGeyuayQ0619u6qBLayqcVdO67raUYOz7KZWKP81LjyTrNOG11QmXk/PTjnD63yrkAkdT/EWaecb8r8KmtJbLrMJ+uqQDdKM2wRrqgrAB6GoTPt+hYwhVBZrsV0gvlrGL5nB4zSrcOo8DGR12FpwLHsGhR9o913t1tSfMkSoyBZObrHgT5UEV0zGzs0Q6f9YRDfTLiBX+pw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by PH0PR11MB4934.namprd11.prod.outlook.com (2603:10b6:510:30::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.33; Thu, 8 Jun 2023 10:16:22 +0000 Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::892b:b8e6:bab7:635d]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::892b:b8e6:bab7:635d%2]) with mapi id 15.20.6455.034; Thu, 8 Jun 2023 10:16:21 +0000 From: "Ni, Ray" To: "Tan, Dun" , "devel@edk2.groups.io" CC: "Dong, Eric" , "Kumar, Rahul R" , Gerd Hoffmann Subject: Re: [Patch V5 09/14] UefiCpuPkg: Add GenSmmPageTable() to create smm page table Thread-Topic: [Patch V5 09/14] UefiCpuPkg: Add GenSmmPageTable() to create smm page table Thread-Index: AQHZmbEFDkDcsfuza0mu79aPmket26+AsRTg Date: Thu, 8 Jun 2023 10:16:21 +0000 Message-ID: References: <20230608022742.1292-1-dun.tan@intel.com> <20230608022742.1292-10-dun.tan@intel.com> In-Reply-To: <20230608022742.1292-10-dun.tan@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|PH0PR11MB4934:EE_ x-ms-office365-filtering-correlation-id: 83e5ad1c-1441-4da5-941c-08db68096827 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: eQFxE7TSCLpk+Pd4ACizO3tKotbXDHP5GpS3vfQc+rfQ6zXbWQlYTwJvEwEmCYtTHgcj3mGj/iqYMFHSQ/qjGXZTJwK2dX72CXLpP5KqUbrzYYjNI8wIdf/OPgNiEac8kH2VLf2XyvHkYR8EPgBAYDUbM15wkmhI0Ex1yfIw2EqLUrIAoBKsHV7iAvVt3hqcpeRtntChgFhTk3h8pRH2wzMzw7hNCLBjJ+uCBxig1zXa3p5UOcBWTd45JIrhJI6qygu9PL5RrDNSsxLJu21B8mzp+FwqYvPe3G0Byuv4wUp9jobx9nnuxX0x7kTVc/9Yo8KHX1JO0DlR6jk8TTFalRJoswg98yBRj9GHB4J414//cTQKayCnpkoag0eZjsop0L0KOIb0L2stIrEWXLjWp09WTgwaakx3Ne16OT50lR6SXRWmVAxnka6mwh5680fwt5WXvc7mgEDh9ND1Tm0ZG7bfUkYlBUFzdCR/vqbJI4P3Xse3ZkrsvsaKWS27TuTToCmyC4HzZmWVFDKmyq37OqowHJNJKkNFMlaPcgENcatl41DHRP6BGVMVgZ8xl/2JUQxmV2wIHaGwhDxIe7OXm6GNs+oyXSWlcI8eBaQVBF7OPydcTzLwSXqsT6lNPNlx x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN6PR11MB8244.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(376002)(346002)(39860400002)(396003)(366004)(136003)(451199021)(41300700001)(8676002)(4326008)(66446008)(76116006)(66556008)(66476007)(64756008)(8936002)(66946007)(110136005)(5660300002)(316002)(19627235002)(52536014)(54906003)(55016003)(2906002)(30864003)(478600001)(38100700002)(82960400001)(122000001)(9686003)(6506007)(53546011)(86362001)(26005)(186003)(71200400001)(7696005)(83380400001)(38070700005)(33656002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?tnEA1dpUgRN4A+hoHllLKsNGlaFuh3kPooN/XbGC8yL5A4LqSOjOis0ABHT1?= =?us-ascii?Q?h1kVQt+CjsTUjwrJ1Ctybe9Twc65cYVVV9zACEMwsAFOzx7Pvh8UlzMXcwWs?= =?us-ascii?Q?rMLbd1fJpWOGKknomemZOlL6ZjdY7U7mfLH57uD7AOOiSWoM+CZQ15iC9P4n?= =?us-ascii?Q?ciJi+R7UrUYq/0kXxyn7IQbuHlsGKVr4ZI2m2AdSHvaXaIY6rig1jkAEJLoi?= =?us-ascii?Q?V34RvSKsStT+QwGZBulB4z2rdJQiziR59gLYHbM/G4dz/f3ZBqqOAIb1OWQe?= =?us-ascii?Q?0vmu6jzI9lrhYOEua5aj12PO+fgxxEkXRrdAwhHEaLRjNT/00TGN4jnz/OV9?= =?us-ascii?Q?oqYgRsLcFtzdB3/aIaC9SUf9E0P9+k40dkQClqPCQnSK/MAQArvuttZEWS6E?= =?us-ascii?Q?urJNCnGTb0JrxRNIPdpbudtFy5JC8rGTVzaI9CaPfsR9j9/KvvkTLquCR7hr?= =?us-ascii?Q?ObkMKpd21DiQPTT1014VOlOGifsR6GvFTYT76lj4rnwlr5jXvetd1nXm1EFL?= =?us-ascii?Q?xdskIScLX1MB9jU3SZWuVGvWzJOt4USxLai0Lzh8FdKKCufmhjkirU2r/F73?= =?us-ascii?Q?jLKakzO0RGd0SH9p3Edof68wonx52UAVLONfPNiHyqrJKbD+JEqKiSIkDaao?= =?us-ascii?Q?Tuq41KnzxUsOLxdR+62HTKJ7bM8fHS/lAOieW2n9kYp6K+/izGLhaHLbnnqu?= =?us-ascii?Q?NlKU+BNvnd1A3ULxfYRgF8dV2oRMWNDIrQ5Bv5edP24yujB/BoiRHcU5vxst?= =?us-ascii?Q?LmvmVS4VBJQ9C4nG3AKT/YAwKhocf5lPw7ZfEJGVHEVSevCsD0VpKDoxqv0c?= =?us-ascii?Q?6eSVJgqwzu5nkfEj2oCrMMNV/YjWjdhm/3Kqhm+7RSoTEooNZhME+CoWqEYM?= =?us-ascii?Q?MPXMmLoKY1J/Vhzjs228b+AKYliqCNG+Qp0ql3GBN4qEo+6b/UASN6LGXKy2?= =?us-ascii?Q?+C80WGIvc8G3bo6shDXay0DqcBO6gZBPAR6kSUYcgnwPf4SKxn+/4HG2posq?= =?us-ascii?Q?bDhwEJb7Q+JXNLV7y23xCINdVqgUz9oOpQ0z0JNCEnbJR4ReKjI0mANksfH/?= =?us-ascii?Q?JuSIttK5NQVgZjub+vxl7ZVjKr3MPdXgIsP5Q1mavMpV1kyiH7dZ0vd8GrWc?= =?us-ascii?Q?taj6hYyPT1xOBZC5c+kNY9kskkzfiM6kGODoMj+MeblSb3nqMCZWWrWo6naE?= =?us-ascii?Q?EhBzX7QWMp0X0BXUL6rWTvAf41HGDEGSAT2Vl54ZLPexPjnZ/AaELpmsPCZU?= =?us-ascii?Q?W1Fy1Ka+J6PTYo0p0Jk/O84QwBnCK38fgfFZC21gXaVECkKKDE2zyQzAxQYc?= =?us-ascii?Q?y2OVrYS2z9UJUTUOgLjoYcmhN+FdM6SSEiemkZ9EJ1qmKEHuRsQhmp31AJfM?= =?us-ascii?Q?3NUvYYcT22M3AV7Shq+fiK2m3AI1WA4aVQ7MompTZzBMLLls5IYBsWcJfnZZ?= =?us-ascii?Q?24LemqzsY5gRFWp/812tFjqu15V/RahnenQ9ow2Ftc/7bdMHzWUV/pa4KKM1?= =?us-ascii?Q?g7zJ98L2LRZGvf9AcPog2A6Kc4sH3U5XJhpHNlLJA4JoFxlTVE1ky71RsHku?= =?us-ascii?Q?7yBv1vU2czbjV6ZUfjMeXrZykWiVIaRik8ygdGm/?= 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: 83e5ad1c-1441-4da5-941c-08db68096827 X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Jun 2023 10:16:21.4385 (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: rvji/VECrQgDZ3VV78esogAZXUJK9aUDdDh9fLzx4gUpXQ6ez2Rj0TCgW4Fb2cxuJl3YZU7zCnKmV2AyFjdaZQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR11MB4934 Return-Path: ray.ni@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ray Ni > -----Original Message----- > From: Tan, Dun > Sent: Thursday, June 8, 2023 10:28 AM > To: devel@edk2.groups.io > Cc: Dong, Eric ; Ni, Ray ; Kumar, > Rahul R ; Gerd Hoffmann > Subject: [Patch V5 09/14] UefiCpuPkg: Add GenSmmPageTable() to create > smm page table >=20 > This commit is code refinement to current smm pagetable generation > code. Add a new GenSmmPageTable() API to create smm page table > based on the PageTableMap() API in CpuPageTableLib. Caller only > needs to specify the paging mode and the PhysicalAddressBits to map. > This function can be used to create both IA32 pae paging and X64 > 5level, 4level paging. >=20 > Signed-off-by: Dun Tan > Cc: Eric Dong > Cc: Ray Ni > Cc: Rahul Kumar > Cc: Gerd Hoffmann > --- > UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 2 +- > UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 15 > +++++++++++++++ > UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 65 > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > +++++++ > UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 220 > ++++++++++++++++++++++++++-----------------------------------------------= ----- > -------------------------------------------------------------------------= --------------------- > ------------------------------------------------ > 4 files changed, 107 insertions(+), 195 deletions(-) >=20 > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c > index 9c8107080a..b11264ce4a 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c > @@ -63,7 +63,7 @@ SmmInitPageTable ( > InitializeIDTSmmStackGuard (); > } >=20 > - return Gen4GPageTable (TRUE); > + return GenSmmPageTable (PagingPae, mPhysicalAddressBits); > } >=20 > /** > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > index a7da9673a5..5399659bc0 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h > @@ -553,6 +553,21 @@ Gen4GPageTable ( > IN BOOLEAN Is32BitPageTable > ); >=20 > +/** > + Create page table based on input PagingMode and PhysicalAddressBits in > smm. > + > + @param[in] PagingMode The paging mode. > + @param[in] PhysicalAddressBits The bits of physical address to m= ap. > + > + @retval PageTable Address > + > +**/ > +UINTN > +GenSmmPageTable ( > + IN PAGING_MODE PagingMode, > + IN UINT8 PhysicalAddressBits > + ); > + > /** > Initialize global data for MP synchronization. >=20 > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > index 4ee99d06d7..4e93d26eb4 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > @@ -1640,6 +1640,71 @@ EdkiiSmmClearMemoryAttributes ( > return SmmClearMemoryAttributes (BaseAddress, Length, Attributes); > } >=20 > +/** > + Create page table based on input PagingMode and PhysicalAddressBits in > smm. > + > + @param[in] PagingMode The paging mode. > + @param[in] PhysicalAddressBits The bits of physical address to m= ap. > + > + @retval PageTable Address > + > +**/ > +UINTN > +GenSmmPageTable ( > + IN PAGING_MODE PagingMode, > + IN UINT8 PhysicalAddressBits > + ) > +{ > + UINTN PageTableBufferSize; > + UINTN PageTable; > + VOID *PageTableBuffer; > + IA32_MAP_ATTRIBUTE MapAttribute; > + IA32_MAP_ATTRIBUTE MapMask; > + RETURN_STATUS Status; > + UINTN GuardPage; > + UINTN Index; > + UINT64 Length; > + > + Length =3D LShiftU64 (1, PhysicalAddressBits= ); > + PageTable =3D 0; > + PageTableBufferSize =3D 0; > + MapMask.Uint64 =3D MAX_UINT64; > + MapAttribute.Uint64 =3D mAddressEncMask; > + MapAttribute.Bits.Present =3D 1; > + MapAttribute.Bits.ReadWrite =3D 1; > + MapAttribute.Bits.UserSupervisor =3D 1; > + MapAttribute.Bits.Accessed =3D 1; > + MapAttribute.Bits.Dirty =3D 1; > + > + Status =3D PageTableMap (&PageTable, PagingMode, NULL, > &PageTableBufferSize, 0, Length, &MapAttribute, &MapMask, NULL); > + ASSERT (Status =3D=3D RETURN_BUFFER_TOO_SMALL); > + DEBUG ((DEBUG_INFO, "GenSMMPageTable: 0x%x bytes needed for initial > SMM page table\n", PageTableBufferSize)); > + PageTableBuffer =3D AllocatePageTableMemory (EFI_SIZE_TO_PAGES > (PageTableBufferSize)); > + ASSERT (PageTableBuffer !=3D NULL); > + Status =3D PageTableMap (&PageTable, PagingMode, PageTableBuffer, > &PageTableBufferSize, 0, Length, &MapAttribute, &MapMask, NULL); > + ASSERT (Status =3D=3D RETURN_SUCCESS); > + ASSERT (PageTableBufferSize =3D=3D 0); > + > + if (FeaturePcdGet (PcdCpuSmmStackGuard)) { > + // > + // Mark the 4KB guard page between known good stack and smm stack as > non-present > + // > + for (Index =3D 0; Index < gSmmCpuPrivate- > >SmmCoreEntryContext.NumberOfCpus; Index++) { > + GuardPage =3D mSmmStackArrayBase + EFI_PAGE_SIZE + Index * > (mSmmStackSize + mSmmShadowStackSize); > + Status =3D ConvertMemoryPageAttributes (PageTable, PagingMode, > GuardPage, SIZE_4KB, EFI_MEMORY_RP, TRUE, NULL); > + } > + } > + > + if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT1) !=3D 0) { > + // > + // Mark [0, 4k] as non-present > + // > + Status =3D ConvertMemoryPageAttributes (PageTable, PagingMode, 0, > SIZE_4KB, EFI_MEMORY_RP, TRUE, NULL); > + } > + > + return (UINTN)PageTable; > +} > + > /** > This function retrieves the attributes of the memory region specified = by > BaseAddress and Length. If different attributes are got from different= part > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > index 46d8ff5d4e..ddd9be66b5 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > @@ -167,160 +167,6 @@ CalculateMaximumSupportAddress ( > return PhysicalAddressBits; > } >=20 > -/** > - Set static page table. > - > - @param[in] PageTable Address of page table. > - @param[in] PhysicalAddressBits The maximum physical address bits > supported. > -**/ > -VOID > -SetStaticPageTable ( > - IN UINTN PageTable, > - IN UINT8 PhysicalAddressBits > - ) > -{ > - UINT64 PageAddress; > - UINTN NumberOfPml5EntriesNeeded; > - UINTN NumberOfPml4EntriesNeeded; > - UINTN NumberOfPdpEntriesNeeded; > - UINTN IndexOfPml5Entries; > - UINTN IndexOfPml4Entries; > - UINTN IndexOfPdpEntries; > - UINTN IndexOfPageDirectoryEntries; > - UINT64 *PageMapLevel5Entry; > - UINT64 *PageMapLevel4Entry; > - UINT64 *PageMap; > - UINT64 *PageDirectoryPointerEntry; > - UINT64 *PageDirectory1GEntry; > - UINT64 *PageDirectoryEntry; > - > - // > - // IA-32e paging translates 48-bit linear addresses to 52-bit physical > addresses > - // when 5-Level Paging is disabled. > - // > - ASSERT (PhysicalAddressBits <=3D 52); > - if (!m5LevelPagingNeeded && (PhysicalAddressBits > 48)) { > - PhysicalAddressBits =3D 48; > - } > - > - NumberOfPml5EntriesNeeded =3D 1; > - if (PhysicalAddressBits > 48) { > - NumberOfPml5EntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBi= ts > - 48); > - PhysicalAddressBits =3D 48; > - } > - > - NumberOfPml4EntriesNeeded =3D 1; > - if (PhysicalAddressBits > 39) { > - NumberOfPml4EntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBi= ts > - 39); > - PhysicalAddressBits =3D 39; > - } > - > - NumberOfPdpEntriesNeeded =3D 1; > - ASSERT (PhysicalAddressBits > 30); > - NumberOfPdpEntriesNeeded =3D (UINTN)LShiftU64 (1, PhysicalAddressBits = - > 30); > - > - // > - // By architecture only one PageMapLevel4 exists - so lets allocate st= orage for > it. > - // > - PageMap =3D (VOID *)PageTable; > - > - PageMapLevel4Entry =3D PageMap; > - PageMapLevel5Entry =3D NULL; > - if (m5LevelPagingNeeded) { > - // > - // By architecture only one PageMapLevel5 exists - so lets allocate = storage > for it. > - // > - PageMapLevel5Entry =3D PageMap; > - } > - > - PageAddress =3D 0; > - > - for ( IndexOfPml5Entries =3D 0 > - ; IndexOfPml5Entries < NumberOfPml5EntriesNeeded > - ; IndexOfPml5Entries++, PageMapLevel5Entry++) > - { > - // > - // Each PML5 entry points to a page of PML4 entires. > - // So lets allocate space for them and fill them in in the IndexOfPm= l4Entries > loop. > - // When 5-Level Paging is disabled, below allocation happens only on= ce. > - // > - if (m5LevelPagingNeeded) { > - PageMapLevel4Entry =3D (UINT64 *)((*PageMapLevel5Entry) & > ~mAddressEncMask & gPhyMask); > - if (PageMapLevel4Entry =3D=3D NULL) { > - PageMapLevel4Entry =3D AllocatePageTableMemory (1); > - ASSERT (PageMapLevel4Entry !=3D NULL); > - ZeroMem (PageMapLevel4Entry, EFI_PAGES_TO_SIZE (1)); > - > - *PageMapLevel5Entry =3D (UINT64)(UINTN)PageMapLevel4Entry | > mAddressEncMask | PAGE_ATTRIBUTE_BITS; > - } > - } > - > - for (IndexOfPml4Entries =3D 0; IndexOfPml4Entries < > (NumberOfPml5EntriesNeeded =3D=3D 1 ? NumberOfPml4EntriesNeeded : 512); > IndexOfPml4Entries++, PageMapLevel4Entry++) { > - // > - // Each PML4 entry points to a page of Page Directory Pointer entr= ies. > - // > - PageDirectoryPointerEntry =3D (UINT64 *)((*PageMapLevel4Entry) & > ~mAddressEncMask & gPhyMask); > - if (PageDirectoryPointerEntry =3D=3D NULL) { > - PageDirectoryPointerEntry =3D AllocatePageTableMemory (1); > - ASSERT (PageDirectoryPointerEntry !=3D NULL); > - ZeroMem (PageDirectoryPointerEntry, EFI_PAGES_TO_SIZE (1)); > - > - *PageMapLevel4Entry =3D (UINT64)(UINTN)PageDirectoryPointerEntry= | > mAddressEncMask | PAGE_ATTRIBUTE_BITS; > - } > - > - if (m1GPageTableSupport) { > - PageDirectory1GEntry =3D PageDirectoryPointerEntry; > - for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntr= ies < 512; > IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress +=3D > SIZE_1GB) { > - if ((IndexOfPml4Entries =3D=3D 0) && (IndexOfPageDirectoryEntr= ies < 4)) { > - // > - // Skip the < 4G entries > - // > - continue; > - } > - > - // > - // Fill in the Page Directory entries > - // > - *PageDirectory1GEntry =3D PageAddress | mAddressEncMask | > IA32_PG_PS | PAGE_ATTRIBUTE_BITS; > - } > - } else { > - PageAddress =3D BASE_4GB; > - for (IndexOfPdpEntries =3D 0; IndexOfPdpEntries < > (NumberOfPml4EntriesNeeded =3D=3D 1 ? NumberOfPdpEntriesNeeded : 512); > IndexOfPdpEntries++, PageDirectoryPointerEntry++) { > - if ((IndexOfPml4Entries =3D=3D 0) && (IndexOfPdpEntries < 4)) = { > - // > - // Skip the < 4G entries > - // > - continue; > - } > - > - // > - // Each Directory Pointer entries points to a page of Page Dir= ectory > entires. > - // So allocate space for them and fill them in in the > IndexOfPageDirectoryEntries loop. > - // > - PageDirectoryEntry =3D (UINT64 *)((*PageDirectoryPointerEntry)= & > ~mAddressEncMask & gPhyMask); > - if (PageDirectoryEntry =3D=3D NULL) { > - PageDirectoryEntry =3D AllocatePageTableMemory (1); > - ASSERT (PageDirectoryEntry !=3D NULL); > - ZeroMem (PageDirectoryEntry, EFI_PAGES_TO_SIZE (1)); > - > - // > - // Fill in a Page Directory Pointer Entries > - // > - *PageDirectoryPointerEntry =3D (UINT64)(UINTN)PageDirectoryE= ntry | > mAddressEncMask | PAGE_ATTRIBUTE_BITS; > - } > - > - for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEn= tries < > 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += =3D > SIZE_2MB) { > - // > - // Fill in the Page Directory entries > - // > - *PageDirectoryEntry =3D PageAddress | mAddressEncMask | IA32= _PG_PS > | PAGE_ATTRIBUTE_BITS; > - } > - } > - } > - } > - } > -} > - > /** > Create PageTable for SMM use. >=20 > @@ -332,15 +178,16 @@ SmmInitPageTable ( > VOID > ) > { > - EFI_PHYSICAL_ADDRESS Pages; > - UINT64 *PTEntry; > + UINTN PageTable; > LIST_ENTRY *FreePage; > UINTN Index; > UINTN PageFaultHandlerHookAddress; > IA32_IDT_GATE_DESCRIPTOR *IdtEntry; > EFI_STATUS Status; > + UINT64 *PdptEntry; > UINT64 *Pml4Entry; > UINT64 *Pml5Entry; > + UINT8 PhysicalAddressBits; >=20 > // > // Initialize spin lock > @@ -357,59 +204,44 @@ SmmInitPageTable ( > } else { > mPagingMode =3D m1GPageTableSupport ? Paging4Level1GB : Paging4Level= ; > } > + > DEBUG ((DEBUG_INFO, "5LevelPaging Needed - %d\n", > m5LevelPagingNeeded)); > DEBUG ((DEBUG_INFO, "1GPageTable Support - %d\n", > m1GPageTableSupport)); > DEBUG ((DEBUG_INFO, "PcdCpuSmmRestrictedMemoryAccess - %d\n", > mCpuSmmRestrictedMemoryAccess)); > DEBUG ((DEBUG_INFO, "PhysicalAddressBits - %d\n", > mPhysicalAddressBits)); > - // > - // Generate PAE page table for the first 4GB memory space > - // > - Pages =3D Gen4GPageTable (FALSE); >=20 > // > - // Set IA32_PG_PMNT bit to mask this entry > + // Generate initial SMM page table. > + // Only map [0, 4G] when PcdCpuSmmRestrictedMemoryAccess is FALSE. > // > - PTEntry =3D (UINT64 *)(UINTN)Pages; > - for (Index =3D 0; Index < 4; Index++) { > - PTEntry[Index] |=3D IA32_PG_PMNT; > - } > - > - // > - // Fill Page-Table-Level4 (PML4) entry > - // > - Pml4Entry =3D (UINT64 *)AllocatePageTableMemory (1); > - ASSERT (Pml4Entry !=3D NULL); > - *Pml4Entry =3D Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS; > - ZeroMem (Pml4Entry + 1, EFI_PAGE_SIZE - sizeof (*Pml4Entry)); > - > - // > - // Set sub-entries number > - // > - SetSubEntriesNum (Pml4Entry, 3); > - PTEntry =3D Pml4Entry; > + PhysicalAddressBits =3D mCpuSmmRestrictedMemoryAccess ? > mPhysicalAddressBits : 32; > + PageTable =3D GenSmmPageTable (mPagingMode, PhysicalAddressB= its); >=20 > if (m5LevelPagingNeeded) { > + Pml5Entry =3D (UINT64 *)PageTable; > // > - // Fill PML5 entry > - // > - Pml5Entry =3D (UINT64 *)AllocatePageTableMemory (1); > - ASSERT (Pml5Entry !=3D NULL); > - *Pml5Entry =3D (UINTN)Pml4Entry | mAddressEncMask | > PAGE_ATTRIBUTE_BITS; > - ZeroMem (Pml5Entry + 1, EFI_PAGE_SIZE - sizeof (*Pml5Entry)); > - // > - // Set sub-entries number > + // Set Pml5Entry sub-entries number for smm PF handler usage. > // > SetSubEntriesNum (Pml5Entry, 1); > - PTEntry =3D Pml5Entry; > + Pml4Entry =3D (UINT64 *)((*Pml5Entry) & ~mAddressEncMask & > gPhyMask); > + } else { > + Pml4Entry =3D (UINT64 *)PageTable; > + } > + > + // > + // Set IA32_PG_PMNT bit to mask first 4 PdptEntry. > + // > + PdptEntry =3D (UINT64 *)((*Pml4Entry) & ~mAddressEncMask & gPhyMask); > + for (Index =3D 0; Index < 4; Index++) { > + PdptEntry[Index] |=3D IA32_PG_PMNT; > } >=20 > - if (mCpuSmmRestrictedMemoryAccess) { > + if (!mCpuSmmRestrictedMemoryAccess) { > // > - // When access to non-SMRAM memory is restricted, create page table > - // that covers all memory space. > + // Set Pml4Entry sub-entries number for smm PF handler usage. > // > - SetStaticPageTable ((UINTN)PTEntry, mPhysicalAddressBits); > - } else { > + SetSubEntriesNum (Pml4Entry, 3); > + > // > // Add pages to page pool > // > @@ -466,7 +298,7 @@ SmmInitPageTable ( > // > // Return the address of PML4/PML5 (to set CR3) > // > - return (UINT32)(UINTN)PTEntry; > + return (UINT32)PageTable; > } >=20 > /** > -- > 2.31.1.windows.1