From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web10.4706.1685677599561832919 for ; Thu, 01 Jun 2023 20:46:40 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=byikl3SO; spf=pass (domain: intel.com, ip: 192.55.52.136, 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=1685677599; x=1717213599; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=8p6Tb0c3vk6/qmk/WYqYUtm3FmlOFWb9BJbfaGkz5SA=; b=byikl3SOBH632o8CjUfv+W5QatblrsbAfs+CYPTawVZAWTo42VCHj9NN hN0VruM29pcs9/G6i9NuPHE6Abxv7dzpnQJvpBZVkz546AILOt3XFHbbw aUnx8EK0U2pmntgLz9bpVMl0B5VGDctTnWXKN9zwip/xGGu76QvFt6EJg IAiXx+CA9Z92ghwTSjfRwp1D6GN4Bkanamm998ouDH31sxvR+EA9aR4L2 1i6zXqLERbjCe3znsD3iZxZ4AyeN3eQHagpaXqvXSXDtjgeRQB0c2n0EB cOlLNPluGN0J2G7FiYJzFfRg5GWrl5T35RXbnbzFZvl2CNgY9LbEgsFIO A==; X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="335374263" X-IronPort-AV: E=Sophos;i="6.00,211,1681196400"; d="scan'208";a="335374263" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2023 20:46:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="797442721" X-IronPort-AV: E=Sophos;i="6.00,211,1681196400"; d="scan'208";a="797442721" Received: from orsmsx603.amr.corp.intel.com ([10.22.229.16]) by FMSMGA003.fm.intel.com with ESMTP; 01 Jun 2023 20:46:34 -0700 Received: from orsmsx611.amr.corp.intel.com (10.22.229.24) 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:46:34 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX611.amr.corp.intel.com (10.22.229.24) 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:46:34 -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:46:34 -0700 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (104.47.66.45) 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:46:33 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bYS8uSC0ADhis0wwdgUP/nvjq1EiqwYQEiBLm2KZGA1hfxGcohEmIVFvDTFf56Lrn4fd4Akg6csVIxL0OmgzJS6inb6xckewUseAwMz7o2CmjiuFn4/bNjw+SNEImEtHxjQBRR/vS322NzPh9JBRjiLQR4HScAorsNOtTkAgW/ya7t4gu8ws3lokw1KQgomlgjz7tqElHGbolxkHvj4wrwSCPss0Utk8PSZNBRj4W6sBqNE2llf3+7Y5Ipr5xIRQIrVIUVFUlNmi4Sgl/eGjAgdGcPo69BT2uJNqa9wnRCulDiX3FyF9HEms5lhrvSvWN2mklELiBMmhy3uvbvBAeg== 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=K6x/xlYzaPxFcV9/v1ITGEq02jqVpXhKDCMzjQUg8tc=; b=YPOaxwm86w4qnYf+N82ylt7uf5RkB8NfWYADtZ2lR753GrpiI8JdHOVUxROz+q6x2L/uHKJXtxbr8UJa1oF1PW6OREj6UE57MvO7Ls/uhqO+BVb6x0i0jgd+Uzfay2w+dg02O/q4raSXi8YXP+tRboJ9u2I77a6VoagdyExj4Rbr8JaNmpidQxmIEtQY4gtzi6RB+q0zQNkNOw77S8Kr2aAeQ6lPZIZ9P973M+hs/m1Aw7xa3YmzgbSX4IrCFMOLABHGfarW9zT1Iaxg6iVciEyoH7VGxxK0FFSLCRQMW5X/nkqFlYt69cqTuHymB/yvxywHCNgfRoq5/FXGeoIK3Q== 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 DM4PR11MB6118.namprd11.prod.outlook.com (2603:10b6:8:b2::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.26; Fri, 2 Jun 2023 03:46:32 +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:46:32 +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: AQHZlQGXoP12ZGXSlESw3n09GrnBb6922ZOQgAAFHMA= Date: Fri, 2 Jun 2023 03:46:31 +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_|DM4PR11MB6118:EE_ x-ms-office365-filtering-correlation-id: 8cb3e464-e2b1-4d47-bdd9-08db631bf477 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: IBvrfjgXqqhwuQpR0wIhFC3SQHo3EIUJ5qoLDsFdDiB+IyTv5KDaF2nMCjnN98GrBuPiG7XOXtCn11V6qyvnYxEkfowtR0BSpyd4lNy3iR8WoqNI3JKonytqieYp+25tgZNZnjyncuLsVAmNW2Uak3QbOBeaYHdpamJhXRFfZeG6ZCAzSBMdd0CvJYVKPShtTDNwGRnVkxBvJwgdCQdgm8kxQCRMTygRPYfNYIGy6aoo24NnyW8OoGVfcvjJafXeop0jHVnnZHUgx7Rw0mi0y7oTsZJPoOdEUE2JkpOoHIoayYVmseGsSjgSYqh9PAc+JGNqYq/BOVaX8ng+UxS65MG7g9MSXqOSgK8UjFE22jF5uY95okbjDjoWF+d6fAyQ1vfRKl9L2EXymkkD0JEOtc8ieQUq0tsNpjdRz1r6D9gRWPhFJllKoX6FUHUqFGdyIJJI/Ci7RF17mS6oHsIjxTafw0sB+bn21/XdvCBxFhGyzTYWky5E+FaQdECSuVTTADtFO5Ih/vZyQ6LPobjgO55PFIIoTUPAVoJnIF8KEa5Y+o9A7zQCd8JK9Lf1aAG/V17ZSuuo1L+lewyHXjtmM8MXNyN6XiUchD3vKAU6q/iTdMFwNPN6/Ji1PzNn9Fd+a6so0aghhRPFCpNx7INN4g== 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)(366004)(346002)(396003)(136003)(376002)(451199021)(6506007)(186003)(9686003)(53546011)(2940100002)(26005)(966005)(7696005)(71200400001)(83380400001)(41300700001)(2906002)(30864003)(316002)(52536014)(5660300002)(8936002)(478600001)(66556008)(64756008)(66446008)(66946007)(76116006)(19627235002)(66476007)(110136005)(54906003)(86362001)(55016003)(38070700005)(33656002)(4326008)(8676002)(122000001)(38100700002)(82960400001)(66899021);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?T110EJFprGpPGyI14Zj+1slgMwywJguvDkLU7r2mxavud/bHevlIwMECMvmt?= =?us-ascii?Q?VeRDtGQJiYJ8tGF2a3+EVKZyXKQLw596fd3JCpO09C37HR0QRcOuGfZ6MO6L?= =?us-ascii?Q?SPLqJ4vzpzKfW2HqI35ZsEOnyOqNZLCqij2dr26RWBF6ktVWbc32Qa6hX2SP?= =?us-ascii?Q?9IbSdaI/BTnbEPiOIKQd22pEHBnmkI7GCLhdTORzbnl3Ys0A5oWE4TPlfjIT?= =?us-ascii?Q?Z5oP36AdM8TWZUCKB/Sc99eB2l/vuhPV1s5+MF0UOy0v4638qp/oF/w8144/?= =?us-ascii?Q?VdqWsN6ZhhZRtrRDQDypHmJ8+uQRzi+XdNv2lVxveqhu2LyVGTqQYTfNGMr1?= =?us-ascii?Q?fOM/xtilNUYtCfiQUy2hk/iQlF7LJOdeyvJjkqiFjBMjaBT6mDnBMF5HVtcj?= =?us-ascii?Q?ipoz362lCHp5cF/IHg8Ohy4B4d+s3XTez1LCEeU2xdTlXyfHgiwfBK1Zff+T?= =?us-ascii?Q?amTkkjH+4AzqrKu4brK+GfhS9DZyj12yMNDsa03mEOZ2Bcu3dS/TbYREt7Fc?= =?us-ascii?Q?36KqYo2q1nBUuTzYSNJ58RRs5VA8Oo47NeWVLpW7necStkfTEfcjqOxSGKrr?= =?us-ascii?Q?PlM9+09vLpbZXaaG5IcyqprOPxpIKd4kTy6NBzJqGf0yZL8PC+nfTSxlX3/8?= =?us-ascii?Q?sb+DsfQcUmiEqbgpGACEhQ3lx8raVguLvQ4Y1NIh/KWkixwL0fJIO2Vb4HH5?= =?us-ascii?Q?UARI3GddR1Fabn+GVk0eb5h1W7HxIrk0D9dJhoAhSNODJzg2TD+FYOc/Nq8Q?= =?us-ascii?Q?DFw/YSi0Dr7YjF+WmKuoUmMZAA5YdGVzgQqQyXd9LTflKMh0wrR48l0jC2wk?= =?us-ascii?Q?DlI2xfrq/ExVOOBVSP/mPIZnCvU0lm6FRxSmuFxdV/XTNtXDOOcpY0Pgl4uV?= =?us-ascii?Q?lNH8HdoZp4EXgbptPEH+VeZTbWakRt1FCYpQ4EZNfbzJwrShc2z1exGIVEXW?= =?us-ascii?Q?4v1ZbzfX7hcbbkYXfL6Dq8hJqDXKiSl3wPXfTHANtXm07Odod7kM2QPhWson?= =?us-ascii?Q?fYtBDm2v4II04p8EOl/S3q6Yyuth2GljZh2G2s6bCMoAIaoID9KTnIFEzQhr?= =?us-ascii?Q?4WLzynYcb50w2jgcMe0euOp7E0vxOpT5vLhhhNth7wzgDsj4orBSvW2nHqR4?= =?us-ascii?Q?Y/yZwO4bF89IPjpppLvkeifscnWGa6hdeXnT8uFBJWVdB8BMA9jTwtbt5WVO?= =?us-ascii?Q?/iHMz75VMOHXVBr4YKbZ1oYJy5cSrEFhUKYNQRmjbR7NYs0LVD4APikPHWin?= =?us-ascii?Q?YHVfjqfiF/Rg7xoT5Pe1Tjkusub9iS5mP9Sbxnqj8grrX3/eXUyVK8LJya0E?= =?us-ascii?Q?Lo7nuXHvvY/rg3GUqR3f+2h8v4ndyQhRhJ5IVv9gHHl5EC9E6omn2dRM7Mq1?= =?us-ascii?Q?v+V13xblMg3YqA1VGkNlBW5WTYGhk21oUSUknZTWhULjaYGgD6/GDLYkmz5j?= =?us-ascii?Q?vzrfZmxZX+z4CRMno3pQbtOdYNia3tdZBA3q+fwmjjP3gAQf0kQiDU1OgMDI?= =?us-ascii?Q?tAM3xEF55GsqtYYmsGpyhoIrK4t3Yr1XCFYLzGIVJkpa+hwq5N9UxdYQKUF+?= =?us-ascii?Q?qG46MwfXOO1oq60ncPA=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: 8cb3e464-e2b1-4d47-bdd9-08db631bf477 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Jun 2023 03:46:31.9326 (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: Q2r+3k75w8ISt9WgBSwHlPIp/8KNTTRV7cijGhO9OIwVBqDfWUWqiBuBjtDsFOpXJp+N8YN1rQVhe6I2S0xMIA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR11MB6118 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 Edited the reply to make it clearer.=20 -----Original Message----- From: Tan, Dun=20 Sent: Friday, June 2, 2023 11:36 AM 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 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 the code logic, the "Guard page" in "mSmmShadowStackSize range" is marke= d as not-present after InitializeMpServiceData() creates the initial smm pa= ge table. This process is only done for smm runtime page table. Thanks, Dun -----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