From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web10.38106.1681289773082554342 for ; Wed, 12 Apr 2023 01:56:13 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=HqFoELzw; spf=pass (domain: intel.com, ip: 192.55.52.43, 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=1681289773; x=1712825773; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7cBzmlwvZE8uGwPy0hcXgU080Rz+lhm4pk9d8ZhZzNM=; b=HqFoELzwoYV8rBUUnM5uN29DQeqjz4WIHaYVBff65IkdEK4qSQ9qmFZw WcWAUqiv1INBBSPf/whik6QlP4soEGkSr2OR/ZpZgOQtvQRn76Zz0Ifw1 i/IQpABcxAYz5L8P+fqMtZKv6aHD2eUxyHXCoIqfwfTMVpYoP4mbZecFd /37FIxNOoqf24qqv2fVH6DfxEhM2bpyH0qgeNoUWq2DEGVg9yRgfuFbT2 AblXf/+EoaGfj2q1sq1v8/9KeIKcaZQLnGHYfJtxnVb54R8h4fumXVPsL OC5PlSp63ZzIe1W0Y/m5h1sQnczQfHkjKUUWLt3ZUxpBitxHVi3kQLUSn w==; X-IronPort-AV: E=McAfee;i="6600,9927,10677"; a="430126787" X-IronPort-AV: E=Sophos;i="5.98,338,1673942400"; d="scan'208";a="430126787" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 01:56:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10677"; a="666262093" X-IronPort-AV: E=Sophos;i="5.98,338,1673942400"; d="scan'208";a="666262093" Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 01:56:02 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [Patch V2 4/8] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid setting non-present range to RO/NX Date: Wed, 12 Apr 2023 16:53:39 +0800 Message-Id: <20230412085343.1077-5-dun.tan@intel.com> X-Mailer: git-send-email 2.31.1.windows.1 In-Reply-To: <20230412085343.1077-1-dun.tan@intel.com> References: <20230412085343.1077-1-dun.tan@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index deb5895d83..89040d386e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -858,6 +858,89 @@ PatchGdtIdtMap ( ); } +/** + This function remove the non-present range in [MemMapStart, MemMapLimit] and + set [MemMapStart, NonPresentRangeStart] as MemoryAttribute in page table. + + @param MemMapStart Pointer to the start address of range. + @param MemMapLimit Limit address of range. + @param NonPresentRangeStart Start address of non-present range. + @param NonPresentRangeLimit Limit address of non-present range. + @param MemoryAttribute The bit mask of attributes to modify for the memory region. + +**/ +VOID +RemoveNonPresentRange ( + UINT64 *MemMapStart, + UINT64 MemMapLimit, + UINT64 NonPresentRangeStart, + UINT64 NonPresentRangeLimit, + UINT64 MemoryAttribute + ) +{ + if (*MemMapStart < NonPresentRangeStart) { + SmmSetMemoryAttributes ( + *MemMapStart, + NonPresentRangeStart - *MemMapStart, + MemoryAttribute + ); + } + + *MemMapStart = NonPresentRangeLimit; +} + +/** + This function set [MemMapStart, MemMapLimit] to the input MemoryAttribute. + + @param MemMapStart Start address of range. + @param MemMapLimit Limit address of range. + @param Map Pointer to the array of Cr3 IA32_MAP_ENTRY. + @param Count Count of IA32_MAP_ENTRY in Map. + @param MemoryAttribute The bit mask of attributes to modify for the memory region. + +**/ +VOID +SetMemMapWithNonPresentRange ( + UINT64 MemMapStart, + UINT64 MemMapLimit, + IA32_MAP_ENTRY *Map, + UINTN Count, + UINT64 MemoryAttribute + ) +{ + UINTN Index; + UINT64 NonPresentRangeStart; + + NonPresentRangeStart = 0; + + for (Index = 0; Index < Count; Index++) { + if ((Map[Index].LinearAddress > NonPresentRangeStart) && + (MemMapStart < Map[Index].LinearAddress) && (MemMapLimit > NonPresentRangeStart)) + { + // + // [NonPresentRangeStart, Map[Index].LinearAddress] is non-present. + // + RemoveNonPresentRange (&MemMapStart, MemMapLimit, NonPresentRangeStart, Map[Index].LinearAddress, MemoryAttribute); + } + + NonPresentRangeStart = Map[Index].LinearAddress + Map[Index].Length; + if (NonPresentRangeStart >= MemMapLimit) { + break; + } + } + + // + // There is no non-present in current [MemMapStart, MemMapLimit] anymore. + // + if (MemMapStart < MemMapLimit) { + SmmSetMemoryAttributes ( + MemMapStart, + MemMapLimit - MemMapStart, + MemoryAttribute + ); + } +} + /** This function sets memory attribute according to MemoryAttributesTable. **/ @@ -872,6 +955,21 @@ 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; + + Count = 0; + Map = NULL; + PageTable = AsmReadCr3 (); + Status = PageTableParse (PageTable, mPagingMode, NULL, &Count); + ASSERT (Status == RETURN_BUFFER_TOO_SMALL); + Map = AllocatePool (Count * sizeof (IA32_MAP_ENTRY)); + ASSERT (Map != NULL); + Status = PageTableParse (PageTable, mPagingMode, Map, &Count); + ASSERT_RETURN_ERROR (Status); SmmGetSystemConfigurationTable (&gEdkiiPiSmmMemoryAttributesTableGuid, (VOID **)&MemoryAttributesTable); if (MemoryAttributesTable == NULL) { @@ -901,33 +999,32 @@ SetMemMapAttributes ( MemoryMap = MemoryMapStart; for (Index = 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 == EfiRuntimeServicesCode) { + MemoryAttribute = EFI_MEMORY_RO; + } else { + // + // Set other type memory as NX. + // + MemoryAttribute = EFI_MEMORY_XP; } + // + // 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), + Map, + Count, + MemoryAttribute + ); + MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } + FreePool (Map); + PatchSmmSaveStateMap (); PatchGdtIdtMap (); -- 2.39.1.windows.1