From mboxrd@z Thu Jan  1 00:00:00 1970
Received: from mga09.intel.com (mga09.intel.com [134.134.136.24])
 by mx.groups.io with SMTP id smtpd.web10.5705.1686220368877940973
 for <devel@edk2.groups.io>;
 Thu, 08 Jun 2023 03:32:49 -0700
Authentication-Results: mx.groups.io;
 dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=TZUNuRQN;
 spf=pass (domain: intel.com, ip: 134.134.136.24, 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=1686220368; x=1717756368;
  h=from:to:cc:subject:date:message-id:references:
   in-reply-to:content-transfer-encoding:mime-version;
  bh=YPGeCvbLU7+zsrqUcg6nt193T0XWM6DRUq0kNgcesxw=;
  b=TZUNuRQNkPQnYxkY7IS+062eAzEy0540wjDue80Y2fHY3CMAAku7FXjf
   CbpckT2DNeepHFnKeQsLss8GRt2o6wj5lLOhDVsScRuK3Yli2zrzZiRej
   UdxTmeFbbyzZHmB/42/4HEmugGivwVO7IuYXzBMLFgcuSe05mjT4lCKha
   M8eowWhYCkQobXUM5Kw1vvK903TiCYBEotmOLAc3aa7gED0hV0YSZpmXF
   2NSyi7hLFgu+m8APInVXuRi6m2Rjd/aAEGnjNmtQHIWUboYLf4WfPt1D/
   ftqXAZcdClVMms5A9jyDHKSPLueo2bwGeeFIBp/AtflIfgAkt4hR0VZsI
   w==;
X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="359739617"
X-IronPort-AV: E=Sophos;i="6.00,226,1681196400"; 
   d="scan'208";a="359739617"
Received: from fmsmga002.fm.intel.com ([10.253.24.26])
  by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jun 2023 03:32:36 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="822566811"
X-IronPort-AV: E=Sophos;i="6.00,226,1681196400"; 
   d="scan'208";a="822566811"
Received: from orsmsx603.amr.corp.intel.com ([10.22.229.16])
  by fmsmga002.fm.intel.com with ESMTP; 08 Jun 2023 03:32:36 -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, 8 Jun 2023 03:32:35 -0700
Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) 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; Thu, 8 Jun 2023 03:32:35 -0700
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.23 via Frontend Transport; Thu, 8 Jun 2023 03:32:35 -0700
Received: from NAM04-MW2-obe.outbound.protection.outlook.com (104.47.73.177)
 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.23; Thu, 8 Jun 2023 03:32:34 -0700
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=hE3SPO0J8fgO/3fzaCwjY/eK90RqO3qSOtnAoQ3xiN03QovIoHSVda7eifKJQ5e0sfGC3vbWE6fuLe1Qb20h6y2CMrq0ToNJS//f8LcBlJSyFO5tVZVoWUxAEZeeYztOQF0EfYEHjuY4KInDKHlSYUi0ZzpkEUIhVVcXF5DHeOUI491jkyXBXWe52cqttuNA6LKLxas94MeFlCmaVFx8gPav3e3TENhG48kVbeVFhSbK4eSxgrWeljSBWULEruNVD1yX10FDPZa+dYBReRfSCCCtcmlUjYGmg/zf8Uhi3xSKKGEjipoogLInz9viWLUKRAauiCeMRuykVWpJmT12SQ==
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=NB+2dF1aR7V4KXLXTNpEiAetH1d9UapsYxONHe0D0Oo=;
 b=YdoEij6nRj0XuHrDu/TDDOSzK0E7LMH13w0oRBQtoGWO/HTjvs5O5PZ8l2sQKcxd1Z+uwawpkIpgv+C0/58uPhteASq174tlTL5ImIePTfs+eG+BMmiLPEJ1D0BViDa7glglOIzKozWJ8v0AWYFI7pWizdMDXYp2e432jlW67TNylj/nzeAKAI7+65FV7o/WNpouc1qF0KvRr50fB3HqRFDoSQvUI/FlxYfuvNUObTpC+1UDS3suy+H9MhCJeVlqveP/ROatf1wanTPScfbYAzevwAz+HlDrUkPp7sFQcY5IplMFT9keeco9aIMUPHkqnJVrZOoCfUVmXPIGVvg+ZA==
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 SJ1PR11MB6250.namprd11.prod.outlook.com (2603:10b6:a03:459::17) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.39; Thu, 8 Jun
 2023 10:32:31 +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:32:33 +0000
From: "Ni, Ray" <ray.ni@intel.com>
To: "devel@edk2.groups.io" <devel@edk2.groups.io>, "Tan, Dun"
	<dun.tan@intel.com>
CC: "Dong, Eric" <eric.dong@intel.com>, "Kumar, Rahul R"
	<rahul.r.kumar@intel.com>, Gerd Hoffmann <kraxel@redhat.com>
Subject: Re: [edk2-devel] [Patch V5 05/14] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid setting non-present range to RO/NX
Thread-Topic: [edk2-devel] [Patch V5 05/14] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid
 setting non-present range to RO/NX
Thread-Index: AQHZmbD2n9w4v+f9tUuOWU2gEY/hn6+AtaHg
Date: Thu, 8 Jun 2023 10:32:33 +0000
Message-ID: <MN6PR11MB824466A32776BCDE38C28C508C50A@MN6PR11MB8244.namprd11.prod.outlook.com>
References: <20230608022742.1292-1-dun.tan@intel.com>
 <20230608022742.1292-6-dun.tan@intel.com>
In-Reply-To: <20230608022742.1292-6-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_|SJ1PR11MB6250:EE_
x-ms-office365-filtering-correlation-id: eaf57590-e451-43da-ea75-08db680babad
x-ms-exchange-senderadcheck: 1
x-ms-exchange-antispam-relay: 0
x-microsoft-antispam: BCL:0;
x-microsoft-antispam-message-info: 9MjrA5lIOETef4pGxmf+8nUzwVq2WJ0mZRdfFOWc9uPQ1/Z0vnBDcsoCeGBeueFhUzlAJV8AKxHAbPR2idxnfi9MZcRdgc0WQouZYS6i86HcCQDriG0CvcKXSk30kj56AU9DrEUGshTYSaodVNaj8qLQqp21k9PJ0u4jjDxBO7Lw3RUkoELITsKj7lSCFcOiX2Cy8W3SAVGeZpo6O0LMQ13D+IjdavcDQMy26P99StN9saQjD7gUGT9VpEez8bMCtbIS91NUPJjqbfkCd9dJGQMgDIq6vBMTOZoz9nvDCRbfyVeUS5LyAOelfqpkj5HcuvNiXoaylV3YkoLc+KOMoBDXh3ByEevJ58iYJuwpcaAtllHnJz+3KZxfddH39DnVXiRBzDoBQ7zoPU4l/zvS7117meYfPsfzfDljOWEYAovurQpDJVCgO/K9zr9j0fAR8ntAhSIYTZnRqX4sFkXhk7E6wFoMNbrpDRmPWShVX3qgLFyvyJ8X0wbHPT0MzxG7sbSXGdw+G9ymoVdmhgdCLDt5mvKUuRKzBLKdwRnN8S0/QvQb1HaHcFBzwCVkW+s4S5I83oGeYaUask4mEwkBAXRtVwaAaJv9At5ndCtwUiSt6HqnSYMVX32Yd7B4U+v+9402+viAHtXoDEabhOzRUA==
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)(6029001)(39860400002)(366004)(136003)(376002)(396003)(346002)(451199021)(66946007)(66556008)(66446008)(64756008)(66476007)(76116006)(478600001)(54906003)(110136005)(8676002)(52536014)(8936002)(5660300002)(71200400001)(66899021)(6636002)(4326008)(316002)(41300700001)(966005)(7696005)(38100700002)(82960400001)(122000001)(55016003)(9686003)(53546011)(38070700005)(26005)(186003)(6506007)(83380400001)(86362001)(33656002)(2906002);DIR:OUT;SFP:1102;
x-ms-exchange-antispam-messagedata-chunkcount: 1
x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?nZ+Bp8OSdwo7sGBa50oPYWkBDzWaBS1vawxtYQUrKxdua7pd9R8J1rzTsnEB?=
 =?us-ascii?Q?oBjrqcpyNELvLaB0P9X+x3n6wp1QgdrP/Aixs2EBgnnXW6Oo4tDiB/0OJ/Pz?=
 =?us-ascii?Q?QRFQjHRYrmmHKlv88tG1R2ctnETD05t/s4LQIndA+vkr78vSIHUZs4bcziUg?=
 =?us-ascii?Q?9aQ+ussSMZfd8Lp3/2VmeGGrnqUiKu+igEtPVIi8g0+DxOsSM3/ipwNP4tAH?=
 =?us-ascii?Q?zbyzybGqRmyJOm6KCUCFPv2y8vRsxVbLH9Pt0v8QT07nfV1i59i9+BtRSRbE?=
 =?us-ascii?Q?fsFgid0/uzaNPEETp8FG8HOoPxQxPy/W75Wu0PNoPTrIes3xdCCs5IVp5DdW?=
 =?us-ascii?Q?XlgqX9GpaHTa4lC6vlkG0+oXTY4QpV/y7Go9rtlXYKnc5acPCWsFuL/qibzK?=
 =?us-ascii?Q?eJnksdeLtaGOtf2xpNE/BTm6vE6kca4+0WhxeZa5opSnL0CvKATJaBftE/tY?=
 =?us-ascii?Q?eJaNoUCSnuHJeCLt4AkMwyB7s4xz9jeGVJQUIHUx06LNi8TRerSTqkK9nBaa?=
 =?us-ascii?Q?AA1AHdzigBfgvGsZFIH49bynlsha0paQGLfQqCfo45UEnx+M8bt2bLfYwlMS?=
 =?us-ascii?Q?q8RmY1332bOWiGBHXLEdMh19CjDsmSbw5Ep/hT6PDiX51oK+oLL6q7GApLNY?=
 =?us-ascii?Q?sdR1St9r3HNuGJ08I0QtRygxRN7m602jS6pxwlPUZkeuHVZv7VkdlNe7Zjzg?=
 =?us-ascii?Q?gP1k+DlcbpmaaytBUEwOYLKL8INbA2IQFo8ednxRyjOY9r5jVWBlZT08Jlyd?=
 =?us-ascii?Q?KnYBb3yknvKGOuDcBezUK9LCaUSRD44sH3/4CwpLH26wzMzfGet8I5/w57vb?=
 =?us-ascii?Q?Z2zwSYrSHqShvNZK1Ma+cK3exKcycB9ruOcyRs4ZhHbkZ61ARs9o53iRvi17?=
 =?us-ascii?Q?YR9T8cfqmLRTY5tYYLOGeN4O7NR1Hj8gamhaskruQbVV8PDJsxqlAP47lC/i?=
 =?us-ascii?Q?ryzWFTKeneB2hpmKwXtxWS80/xZMZzZ26xB1wr7JrTeAdYQaFlWNRafZfIIl?=
 =?us-ascii?Q?0tMjMPwXzShSOCgCke+YjWAyoT4FrdnQa9jX9N0qmBrar4KQr1w25OQRoC0l?=
 =?us-ascii?Q?yWe6oF37u8JCYdemFocPc1kw1V9/PljzDpBKfVOXqQgQf+P/0mPIugQeVxX8?=
 =?us-ascii?Q?iHq2KK0tKzoSdaAs3ZLwpQGUQnC8CwRyqeJpNT5faPAOACkG9/pAAIE9J8Hf?=
 =?us-ascii?Q?BsddNnM+0odWNT3hrsryq5puH25Zc2kDtdmVxiA9nILQfJmXvrUrXXzOKSrr?=
 =?us-ascii?Q?mh39UTjewrpo84GRxDTZNWlw+KiEl08dpXl4QHXaofwSYRhjPQR0alMMKB0z?=
 =?us-ascii?Q?PodNTy/6ghmo0md8oZfyqSsPwrN0guIloqTxErETALV6VHPj+ijgS3Gl4Kka?=
 =?us-ascii?Q?CVF+hzDd1jtRLE3LDlir7HKVhVnK4Al/Ww1/rG0qa6KW258QdrB+tfpurGQG?=
 =?us-ascii?Q?bgMgazLY7Ez9N1LXR78DF/hvebP611SSAVCGSHXEza9URPQPj2tu3NHJxUqr?=
 =?us-ascii?Q?oT8gopKFtjA+FlV5uvfjMQX9+3m5+J7+Vzc/P6NJQBmNwz8YgcbQLFKCyA9Q?=
 =?us-ascii?Q?Zp32M/NcoIxm+5seinmXIla1mQ3Pn/MTfQtYxyXM?=
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: eaf57590-e451-43da-ea75-08db680babad
X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Jun 2023 10:32:33.7200
 (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: 9PZ+PxF/LMwO/Vms1t5MPSPQcB3i+mUeg+hwxgRxLIX+sdg27DTCgni+3Yhk/UhsNrxoid2ofUVwwJ4VkirJCA==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR11MB6250
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 <ray.ni@intel.com>

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of duntan
> Sent: Thursday, June 8, 2023 10:28 AM
> To: devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Kumar,
> Rahul R <rahul.r.kumar@intel.com>; Gerd Hoffmann <kraxel@redhat.com>
> Subject: [edk2-devel] [Patch V5 05/14] UefiCpuPkg/PiSmmCpuDxeSmm:
> Avoid setting non-present range to RO/NX
>=20
> In PiSmmCpuDxeSmm code, SetMemMapAttributes() marks memory ranges
> in SmmMemoryAttributesTable to RO/NX. There may exist non-present
> range in these memory ranges. Set other attributes for a non-present
> range is not permitted in CpuPageTableMapLib. So add code to handle
> this case. Only map the present ranges in SmmMemoryAttributesTable
> to RO or NX.
>=20
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> Cc: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 129
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++++++++++++++++++++++++++++++++++++++++++++---------------
> -------
>  1 file changed, 107 insertions(+), 22 deletions(-)
>=20
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> index 862b3e9720..3c79927c7b 100644
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
> @@ -918,6 +918,70 @@ PatchGdtIdtMap (
>      );
>  }
>=20
> +/**
> +  This function set [Base, Limit] to the input MemoryAttribute.
> +
> +  @param  Base        Start address of range.
> +  @param  Limit       Limit address of range.
> +  @param  Attribute   The bit mask of attributes to modify for the memor=
y
> region.
> +  @param  Map         Pointer to the array of Cr3 IA32_MAP_ENTRY.
> +  @param  Count       Count of IA32_MAP_ENTRY in Map.
> +**/
> +VOID
> +SetMemMapWithNonPresentRange (
> +  UINT64          Base,
> +  UINT64          Limit,
> +  UINT64          Attribute,
> +  IA32_MAP_ENTRY  *Map,
> +  UINTN           Count
> +  )
> +{
> +  UINTN   Index;
> +  UINT64  NonPresentRangeStart;
> +
> +  NonPresentRangeStart =3D 0;
> +  for (Index =3D 0; Index < Count; Index++) {
> +    if ((Map[Index].LinearAddress > NonPresentRangeStart) &&
> +        (Base < Map[Index].LinearAddress) && (Limit > NonPresentRangeSta=
rt))
> +    {
> +      //
> +      // We should NOT set attributes for non-present ragne.
> +      //
> +      //
> +      // There is a non-present ( [NonPresentStart,
> Map[Index].LinearAddress] ) range before current Map[Index]
> +      // and it is overlapped with [Base, Limit].
> +      //
> +      if (Base < NonPresentRangeStart) {
> +        SmmSetMemoryAttributes (
> +          Base,
> +          NonPresentRangeStart - Base,
> +          Attribute
> +          );
> +      }
> +
> +      Base =3D Map[Index].LinearAddress;
> +    }
> +
> +    NonPresentRangeStart =3D Map[Index].LinearAddress + Map[Index].Lengt=
h;
> +    if (NonPresentRangeStart >=3D Limit) {
> +      break;
> +    }
> +  }
> +
> +  Limit =3D MIN (NonPresentRangeStart, Limit);
> +
> +  if (Base < Limit) {
> +    //
> +    // There is no non-present range in current [Base, Limit] anymore.
> +    //
> +    SmmSetMemoryAttributes (
> +      Base,
> +      Limit - Base,
> +      Attribute
> +      );
> +  }
> +}
> +
>  /**
>    This function sets memory attribute according to MemoryAttributesTable=
.
>  **/
> @@ -932,6 +996,11 @@ SetMemMapAttributes (
>    UINTN                                 DescriptorSize;
>    UINTN                                 Index;
>    EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE  *MemoryAttributesTable;
> +  UINTN                                 PageTable;
> +  EFI_STATUS                            Status;
> +  IA32_MAP_ENTRY                        *Map;
> +  UINTN                                 Count;
> +  UINT64                                MemoryAttribute;
>=20
>    SmmGetSystemConfigurationTable
> (&gEdkiiPiSmmMemoryAttributesTableGuid, (VOID
> **)&MemoryAttributesTable);
>    if (MemoryAttributesTable =3D=3D NULL) {
> @@ -958,36 +1027,52 @@ SetMemMapAttributes (
>      MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap,
> DescriptorSize);
>    }
>=20
> +  Count     =3D 0;
> +  Map       =3D NULL;
> +  PageTable =3D AsmReadCr3 ();
> +  Status    =3D PageTableParse (PageTable, mPagingMode, NULL, &Count);
> +  while (Status =3D=3D RETURN_BUFFER_TOO_SMALL) {
> +    if (Map !=3D NULL) {
> +      FreePool (Map);
> +    }
> +
> +    Map =3D AllocatePool (Count * sizeof (IA32_MAP_ENTRY));
> +    ASSERT (Map !=3D NULL);
> +    Status =3D PageTableParse (PageTable, mPagingMode, Map, &Count);
> +  }
> +
> +  ASSERT_RETURN_ERROR (Status);
> +
>    MemoryMap =3D MemoryMapStart;
>    for (Index =3D 0; Index < MemoryMapEntryCount; Index++) {
>      DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n",
> MemoryMap->PhysicalStart, MemoryMap->NumberOfPages));
> -    switch (MemoryMap->Type) {
> -      case EfiRuntimeServicesCode:
> -        SmmSetMemoryAttributes (
> -          MemoryMap->PhysicalStart,
> -          EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages),
> -          EFI_MEMORY_RO
> -          );
> -        break;
> -      case EfiRuntimeServicesData:
> -        SmmSetMemoryAttributes (
> -          MemoryMap->PhysicalStart,
> -          EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages),
> -          EFI_MEMORY_XP
> -          );
> -        break;
> -      default:
> -        SmmSetMemoryAttributes (
> -          MemoryMap->PhysicalStart,
> -          EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages),
> -          EFI_MEMORY_XP
> -          );
> -        break;
> +    if (MemoryMap->Type =3D=3D EfiRuntimeServicesCode) {
> +      MemoryAttribute =3D EFI_MEMORY_RO;
> +    } else {
> +      ASSERT ((MemoryMap->Type =3D=3D EfiRuntimeServicesData) ||
> (MemoryMap->Type =3D=3D EfiConventionalMemory));
> +      //
> +      // Set other type memory as NX.
> +      //
> +      MemoryAttribute =3D EFI_MEMORY_XP;
>      }
>=20
> +    //
> +    // There may exist non-present range overlaps with the MemoryMap
> range.
> +    // Do not change other attributes of non-present range while still
> remaining it as non-present
> +    //
> +    SetMemMapWithNonPresentRange (
> +      MemoryMap->PhysicalStart,
> +      MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE
> ((UINTN)MemoryMap->NumberOfPages),
> +      MemoryAttribute,
> +      Map,
> +      Count
> +      );
> +
>      MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap,
> DescriptorSize);
>    }
>=20
> +  FreePool (Map);
> +
>    PatchSmmSaveStateMap ();
>    PatchGdtIdtMap ();
>=20
> --
> 2.31.1.windows.1
>=20
>=20
>=20
>=20
>=20