From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web10.4596.1685676988604046510 for ; Thu, 01 Jun 2023 20:36:28 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=WJbURP/M; spf=pass (domain: intel.com, ip: 192.55.52.151, mailfrom: dun.tan@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685676988; x=1717212988; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=+91Fs46w5NoXMcBNgoKPyH7nzuBNmOZ4FST9RiYq+lw=; b=WJbURP/MY410abMtJdbQ2rLj1aQMyvQIoR1pJe5zKPm/l56Haw2krizp 1wb+dZNhpJDam4gB1O021U3fwJ1ZuN4suOa+IkR/ZG2Yuh7sSZucF08rv Fb1/ezeOdEV1fKaBKtnvKKncVh5xwd33MKwG3gYztMIyKAJCvvub2HE1y mdHAMxRfasHj7MUwYGRaLU4Fp5EBFItwZzzsmJCtdIeU+MGCiambnNCU5 DMs/B3H/ZLSzYiSg+Okv+s3tAD8Xw4V28FXGqzcLbtlAJXqZCe46Xmvgy 1bZIVQbTXrS+Nz9pQxdO4DzddvBh86u55kCigsUsvk6Qi9LCO5m6thCXw g==; X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="336121725" X-IronPort-AV: E=Sophos;i="6.00,211,1681196400"; d="scan'208";a="336121725" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2023 20:36:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="1037749287" X-IronPort-AV: E=Sophos;i="6.00,211,1681196400"; d="scan'208";a="1037749287" Received: from orsmsx603.amr.corp.intel.com ([10.22.229.16]) by fmsmga005.fm.intel.com with ESMTP; 01 Jun 2023 20:36:16 -0700 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.23; Thu, 1 Jun 2023 20:36:15 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) 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.23 via Frontend Transport; Thu, 1 Jun 2023 20:36:15 -0700 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.168) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.23; Thu, 1 Jun 2023 20:36:15 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TFmr4InzsJfF9dbh+QpsaoHauNQfXe5aE11crXosXegRobd+/+qHPZNeGcUHv72JBM+t6eQ+90HKzWz916oF2Z6WsE1Kngei6PiOAfakZKUv0jzNTkvxWZbfKfTstwv3mA7hNVFIRtQjhsPaVxoIeX6SrG72l0bL9W3rv5aNXUN4w/Yl+796P+XeH2m8iMUC34z4l2i2RFVpovruZJxEgL/3d/fcaYso3pDPNXyXSiX65G9jTf9IaSM2EzY34wrjr2xtYl+9puG/djjbCFb6yI3y0sGNaE5QnE6AJTCaWQJx2EY42Q6aTrs+ki5js7MlrjKrv/CtGC1Sn2Uuad/Vrg== 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=qZqaLZbrxo0ICX7F2XRJFFJ5vLyT+6xzG2b63RYvGog=; b=b4eTrEel7+JdxuNmEzZsOkbgcYVXAs1+5pRwQXzotEUooR/cNDXA95J1sr6XI9Y6AHaiKuYaZVK/O/4i75ESnWwmMV4Y28ef05MOg+H0xEb85ofHWjeQc2oXMfndvST2egFQ5GQNw/pECyNTl7RPNrcsL6phSx6KbPtNnk83lDSEU7/AUQn3PjgsLn3oIj8F8WAsjfAZ4grIVsoXJ4n23kXq2iV93Zaxc6Jkx7qDRQOB983667QmTB007eKxzaiUoxezDO6Md+p0q8FEHH0BwubQ2aLP3qQoMH4tQmjVn7i6a+1gdfOUK5LyKtfo85Cx86c3PPyZxtRwZ36aEZa1cA== 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 BN9PR11MB5483.namprd11.prod.outlook.com (2603:10b6:408:104::10) by IA1PR11MB7679.namprd11.prod.outlook.com (2603:10b6:208:3f1::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.24; Fri, 2 Jun 2023 03:36:13 +0000 Received: from BN9PR11MB5483.namprd11.prod.outlook.com ([fe80::3023:34d3:bd4e:7901]) by BN9PR11MB5483.namprd11.prod.outlook.com ([fe80::3023:34d3:bd4e:7901%3]) with mapi id 15.20.6455.024; Fri, 2 Jun 2023 03:36:13 +0000 From: "duntan" To: "Ni, Ray" , "devel@edk2.groups.io" CC: "Dong, Eric" , "Kumar, Rahul R" , Gerd Hoffmann Subject: Re: [edk2-devel] [Patch V4 10/15] UefiCpuPkg: Add GenSmmPageTable() to create smm page table Thread-Topic: [edk2-devel] [Patch V4 10/15] UefiCpuPkg: Add GenSmmPageTable() to create smm page table Thread-Index: AQHZlQGXoP12ZGXSlESw3n09GrnBb6922ZOQ Date: Fri, 2 Jun 2023 03:36:12 +0000 Message-ID: References: <20230516095932.1525-1-dun.tan@intel.com> <20230516095932.1525-11-dun.tan@intel.com> In-Reply-To: Accept-Language: zh-CN, 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: BN9PR11MB5483:EE_|IA1PR11MB7679:EE_ x-ms-office365-filtering-correlation-id: 149d4ab5-22ec-4b8a-f71b-08db631a82fe x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: FS4c//Pk2D44QU73Hd61qhhlAWCnMOYeAi+hSKg2fJTBwq0EnvJy8d4A+og2uZeyWEVFqwPk1IaRh4pwZWuHoToAx2yzEJIM+SV+q/PWM0QQqnZNV454konVBpWEr7j45y1jfGW3Kv6YQ3n6G+WUZYZAy4Cw576TH+pGS7x+7PB3N8zvmT6iruu6GM2gABWS6M1ySnpkdCLdG7SSBLhebnWZPmwXeoipNt+sRyYCSSq3iEo1kcle6t6rUV2/PoAll5ZWWs92NH/XtY2uc3YqSk+BKS3ZIweKRxc5SaxMsAsX5fkplUKpTNoBlIVBou2MQdNfq3IMWOEXPFetuZ8hTkRlwCf/B8j+QTlqygHC9Fz+y+qN2BdSE+CUYb4/pqxq/cvkq/gRBmOy0k2F0TuQtNppCkFjx1hQdKfhR+etKF76sB9anxo/ODPZZ0ABbWKNwiXPZL8+1TIdWZwAKdNLZGcQChyz80TuSqEMvTtUWjLfq0p7LnLVyz+fAEDXSjzPWr3iSHfApCaHxeVcGr5qMeLHenK7NbteZW1d+ov/ndq4pyZ3ASCPH83z2J3nKAOqOf5LWkDoCKsGvmJcfsT9I/U1tAOQnyXtHCOsticp4f9/se8YsAHOcb5g30rRtpg+ulXCRCxnb28r6FQY6YWv1A== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN9PR11MB5483.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(39860400002)(136003)(346002)(366004)(396003)(376002)(451199021)(86362001)(33656002)(82960400001)(122000001)(38100700002)(38070700005)(478600001)(71200400001)(19627235002)(110136005)(54906003)(66476007)(7696005)(8936002)(5660300002)(966005)(64756008)(66446008)(30864003)(2906002)(52536014)(66946007)(66556008)(316002)(76116006)(41300700001)(4326008)(8676002)(66899021)(6506007)(53546011)(55016003)(186003)(83380400001)(9686003)(26005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?XuBfNjRoLt9/atLGtVbZ8hzg/FHHezbOYrnIK38q/iVoD8QCAs3ZHu4Hg409?= =?us-ascii?Q?yRY+XaYDOICDJF2bygaOU9W+HrlXabhMQHiQxNzgwcG9wcX2aY8XGmVMel6T?= =?us-ascii?Q?Y2ZKBk5xiEo/e5VXO1CW7Zqn8e4/BC5HOySDj4/0TM0thO5rJLm0jTYJT/WC?= =?us-ascii?Q?Zj8nMT5EHk6l89wGL3wGnGuyJPTng64b8t+QlLUupnOqKzuqw6XH+ksBkKiO?= =?us-ascii?Q?N5Ay2nZaL/Dv2fssGO3+PGG8ORnOf2bm3p0Z+35Vo+UQKCjGmKCejBfNS8Dk?= =?us-ascii?Q?7v21emzchSORb0hHz+7E+gut4smkGEriQcctk3KV7N9vHQqJZ4eCkiIWWDRf?= =?us-ascii?Q?4boU0tRlza4NCRkiWYOsztGjNxvJk8TFGyLJmsYTHFvoU2PaUl12HvZQxjdx?= =?us-ascii?Q?lVnbLf2iFclKO1Vs5UK2ZMz2E/NDIejs1dDx4OLBtvTk/QbtVSf70ncPYu+k?= =?us-ascii?Q?vFflciTT5DvUKuVs1GfPaj7thn0yIBlOhfCl5TnodMsyikOAq9jtgyJVfz0z?= =?us-ascii?Q?ILoJ0outd92n/Ol9ZYLJqpMk6pvHlSjU+8CqC20R0AqyF+oNrMqR9RmBmDEI?= =?us-ascii?Q?Bd4FfXpCLNYCBUwVHYF8TL5m25wnYEWW8eMJZ2+twGsjuWS2Vs5IdhxBDbzR?= =?us-ascii?Q?KnfQU51xlKfse+FQyT7bWL6xLo5ySiK+VOczzxYbHpvvLwgHjL8OOS68ErF4?= =?us-ascii?Q?2KucnKr7WU5QfwZZ4iXfkxmE2Wg3lZllVLxlwANI0JRPqCYxt1IoI5Nvbqko?= =?us-ascii?Q?oEjXMcekD/I1+9w76+U/ryp8Zzqa3cRVdW8lxD96l7XnujoqsVlW0acRXz5y?= =?us-ascii?Q?lIWGtWozbQYClick+jV4NT5SMxB3lUxrQA44B6aIM0AFnWJZ/RSydQDxDWbp?= =?us-ascii?Q?1jjHuFa1UZGX72lNM+VspCZIqrnd04gYYY85qpSowoW6CuYnjFEN3rIDpFhq?= =?us-ascii?Q?vhT6xgdEoBLx3PvBExJ1ah/Ho54rYQQgYh8d1L2A48r0ucj0k306u0/CbEqG?= =?us-ascii?Q?EyQS/4hKW0Hn4JWAhRvqO7EneubzL2DIYLKSnTSUmFRXjb/C1oOntpciuFcp?= =?us-ascii?Q?d7Lwe5C3eNNvRNk4+YqJggGyZNxDrXP4ZxB0F7MHD4PGpAgInLnwMup+W8+h?= =?us-ascii?Q?vzRyWnXMabUBB/2vM/a7XxhyApwLMyK1iv95ruoR/y5z+KHKjoIGtTRwI1zv?= =?us-ascii?Q?Aq6BZZqQ+Z/fxzkGD8Dgl4fgLsgMkaikheQHbI+kcHfBgGRPTFQA4NTctsQY?= =?us-ascii?Q?RvIFCO/WffLAUuE0y245KOHAyWG+QQvLom2c1YLTgySb8cxrGlI2maCTIh/A?= =?us-ascii?Q?MmqdaooaUGrtxzRzy3ELBpvkRotXvmvXW6/bv0KyFKnKRbNCSpgAWfTij0r1?= =?us-ascii?Q?wl8cs2t8iFS0fWEvHNakBbk61AC3VuNTFcIbcvruQMb8gnjBkCyVXggYoNAp?= =?us-ascii?Q?pvalq+xaBiPnAEswyT1GDKtGmWHGzM8iwFbTyH+UQSVZ/dXdXwF8ktk4GFz+?= =?us-ascii?Q?5gykcE3KnyO622A4OzTy3fzmJZynQArq+dL0ww0UkyV4FwFr4hS9j4b276fB?= =?us-ascii?Q?WR2f69EVjHO9fkpOAzA=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BN9PR11MB5483.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 149d4ab5-22ec-4b8a-f71b-08db631a82fe X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jun 2023 03:36:12.0661 (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: cJ/0+TtVA+nsEurq87j393PE5h5TAXCfdrCQg9w+QAGo3mmbS8wwo53IUUnPUASi7DCFiizha4ijKWLI92DbVw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR11MB7679 Return-Path: dun.tan@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable GenSmmPageTable() doesn't mark the "Guard page" in "mSmmShadowStackSize ran= ge" is to align with old behavior. GenSmmPageTable() is also used to create SmmS3Cr3 and the "Guard page" in "= mSmmShadowStackSize range" is not marked as non-present in SmmS3Cr3. In old code logic, the "Guard page" in "mSmmShadowStackSize range" is marke= d as not-present after InitializeMpServiceData() creates the initial smm pa= ge table. -----Original Message----- From: Ni, Ray =20 Sent: Friday, June 2, 2023 11:23 AM To: devel@edk2.groups.io; Tan, Dun Cc: Dong, Eric ; Kumar, Rahul R ; Gerd Hoffmann Subject: RE: [edk2-devel] [Patch V4 10/15] UefiCpuPkg: Add GenSmmPageTable(= ) to create smm page table // // SMM Stack Guard Enabled // Append Shadow Stack after normal stack // 2 more pages is allocated for each processor, one is guard page = and the other is known good shadow stack. // // |=3D Stacks // +--------------------------------------------------+--------------= -------------------------------------------------+ // | Known Good Stack | Guard Page | SMM Stack | Known Good Sh= adow Stack | Guard Page | SMM Shadow Stack | // +--------------------------------------------------+--------------= -------------------------------------------------+ // | 4K | 4K |PcdCpuSmmStackSize| 4K= | 4K |PcdCpuSmmShadowStackSize| // |<---------------- mSmmStackSize ----------------->|<-------------= -------- mSmmShadowStackSize ------------------->| // | = | // |<-------------------------------------------- Processor N -------= ------------------------------------------------>| // GenSmmPageTable() only sets the "Guard page" in "mSmmStackSize range" as no= t-present. But the "Guard page" in "mSmmShadowStackSize range" is not marked as not-pr= esent. Why? Thanks, Ray > -----Original Message----- > From: devel@edk2.groups.io On Behalf Of duntan > Sent: Tuesday, May 16, 2023 5:59 PM > To: devel@edk2.groups.io > Cc: Dong, Eric ; Ni, Ray ;=20 > Kumar, Rahul R ; Gerd Hoffmann=20 > > Subject: [edk2-devel] [Patch V4 10/15] UefiCpuPkg: Add=20 > GenSmmPageTable() to create smm page table >=20 > This commit is code refinement to current smm pagetable generation=20 > code. Add a new GenSmmPageTable() API to create smm page table based=20 > on the PageTableMap() API in CpuPageTableLib. Caller only needs to=20 > specify the paging mode and the PhysicalAddressBits to map. > This function can be used to create both IA32 pae paging and X64=20 > 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 ef0ba9a355..138ff43c9d 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c > @@ -1642,6 +1642,71 @@ EdkiiSmmClearMemoryAttributes ( > return SmmClearMemoryAttributes (BaseAddress, Length, Attributes); =20 > } >=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,= =20 > + "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=20 > + 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,=20 > + 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=20 > different part diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c > index 25ced50955..060e6dc147 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=20 > 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= =20 > - 30); > - > - // > - // By architecture only one PageMapLevel4 exists - so lets allocate=20 > storage 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",=20 > 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 &=20 > + gPhyMask); } else { > + Pml4Entry =3D (UINT64 *)PageTable; > + } > + > + // > + // Set IA32_PG_PMNT bit to mask first 4 PdptEntry. > + // > + PdptEntry =3D (UINT64 *)((*Pml4Entry) & ~mAddressEncMask & gPhyMask); = =20 > + 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 >=20 >=20 >=20 >=20 >=20