From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web11.379.1686191316401101708 for ; Wed, 07 Jun 2023 19:28:36 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=Hx/YcO5P; spf=pass (domain: intel.com, ip: 192.55.52.115, 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=1686191316; x=1717727316; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=x2KMhmH7mgzWVKqqRu9RGR+mmdDui5FoaQXOoJ7LGoc=; b=Hx/YcO5Pb3mx11mgoRgHniESHs0e53hPzSjCDgdfCm1wJjOzqFCP7QF1 ChMBfMmOix6jQmSK1AjAgnFBEOgFrK6XrgvegSlY2gCa5qFyfh6tEiYMK xShS46MBmVZEIJ9HtMmlFZRmZjFgdLL9VQTh+Qy8Z+iNzHomfG4GQHs2w 3SfVtwpl8Jvau5k7Z/lRfztNF7MAHwDUs4GMYFPo3bzLo+BNbROm5uYiZ iGcUtZ/sTx9ecLDu5ASOtUSXlBpvs/rXfry12QQ5mePE8P27riJDmy1Cj EAMovAQuOrs9ldJjX/QZH08cWpY6a4+Zm3n9QMprKJLfFxS6ZSq3PQhL3 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="357184226" X-IronPort-AV: E=Sophos;i="6.00,225,1681196400"; d="scan'208";a="357184226" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jun 2023 19:28:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10734"; a="774877744" X-IronPort-AV: E=Sophos;i="6.00,225,1681196400"; d="scan'208";a="774877744" Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.158]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jun 2023 19:28:34 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [Patch V5 04/14] UefiCpuPkg: Add DEBUG_CODE for special case when clear RP Date: Thu, 8 Jun 2023 10:27:32 +0800 Message-Id: <20230608022742.1292-5-dun.tan@intel.com> X-Mailer: git-send-email 2.31.1.windows.1 In-Reply-To: <20230608022742.1292-1-dun.tan@intel.com> References: <20230608022742.1292-1-dun.tan@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In ConvertMemoryPageAttributes() function, when clear RP for a specific range [BaseAddress, BaseAddress + Length], it means to set the present bit to 1 and assign default value for other attributes in page table. The default attributes for the input specific range are NX disabled and ReadOnly. If there is existing present range in [BaseAddress, BaseAddress + Length] and the attributes are not NX disabled or not ReadOnly, then output the DEBUG message to indicate that the NX and ReadOnly attributes of the existing present range are modified in the function. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 12723e5750..862b3e9720 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -349,6 +349,8 @@ ConvertMemoryPageAttributes ( IA32_MAP_ENTRY *Map; UINTN Count; UINTN Index; + UINT64 OverlappedRangeBase; + UINT64 OverlappedRangeLimit; ASSERT (Attributes != 0); ASSERT ((Attributes & ~EFI_MEMORY_ATTRIBUTE_MASK) == 0); @@ -430,6 +432,52 @@ ConvertMemoryPageAttributes ( // By default memory is Ring 3 accessble. // PagingAttribute.Bits.UserSupervisor = 1; + + DEBUG_CODE_BEGIN (); + if (((Attributes & EFI_MEMORY_RO) == 0) || (((Attributes & EFI_MEMORY_XP) == 0) && (mXdSupported))) { + // + // When mapping a range to present and EFI_MEMORY_RO or EFI_MEMORY_XP is not specificed, + // check if [BaseAddress, BaseAddress + Length] contains present range. + // Existing Present range in [BaseAddress, BaseAddress + Length] is set to NX disable or ReadOnly. + // + Count = 0; + Map = NULL; + Status = PageTableParse (PageTableBase, mPagingMode, NULL, &Count); + + while (Status == RETURN_BUFFER_TOO_SMALL) { + if (Map != NULL) { + FreePool (Map); + } + + Map = AllocatePool (Count * sizeof (IA32_MAP_ENTRY)); + ASSERT (Map != NULL); + Status = PageTableParse (PageTableBase, mPagingMode, Map, &Count); + } + + ASSERT_RETURN_ERROR (Status); + for (Index = 0; Index < Count; Index++) { + if (Map[Index].LinearAddress >= BaseAddress + Length) { + break; + } + + if ((BaseAddress < Map[Index].LinearAddress + Map[Index].Length) && (BaseAddress + Length > Map[Index].LinearAddress)) { + OverlappedRangeBase = MAX (BaseAddress, Map[Index].LinearAddress); + OverlappedRangeLimit = MIN (BaseAddress + Length, Map[Index].LinearAddress + Map[Index].Length); + + if (((Attributes & EFI_MEMORY_RO) == 0) && (Map[Index].Attribute.Bits.ReadWrite == 1)) { + DEBUG ((DEBUG_ERROR, "SMM ConvertMemoryPageAttributes: [0x%lx, 0x%lx] is set from ReadWrite to ReadOnly\n", OverlappedRangeBase, OverlappedRangeLimit)); + } + + if (((Attributes & EFI_MEMORY_XP) == 0) && (mXdSupported) && (Map[Index].Attribute.Bits.Nx == 1)) { + DEBUG ((DEBUG_ERROR, "SMM ConvertMemoryPageAttributes: [0x%lx, 0x%lx] is set from NX enabled to NX disabled\n", OverlappedRangeBase, OverlappedRangeLimit)); + } + } + } + + FreePool (Map); + } + + DEBUG_CODE_END (); } } -- 2.31.1.windows.1