From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 94C90AC19EA for ; Fri, 29 Mar 2024 20:21:27 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=DW9tF5o4PhuFWdjDaBBHsafn54rrdo2X5BpOHznjF0w=; c=relaxed/simple; d=groups.io; h=DKIM-Filter:From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type:Content-Transfer-Encoding; s=20240206; t=1711743686; v=1; b=LeUIG7NLpd5WFpNgAj//i7WJ7o4vzO2Yo2NZjTt4Qa8XNkSb93/HBl06LP3Y89m/mb3WaNPg Jh1ikt4NPXi56Cgjjk1gpcE1UdaP86uzEgW8Xv9u5GDtYRPf8Zmz33j071ApzxOtdktZ34NINJz q82c6qbKcmH2NI0VLXmQJwDAmjnU+7cnmjXX2F+Iadntih7MXIWPSNo/SJfZ4GX3L475I72RYs3 bcTVNBofCduIecFb6/r+yj+0Ih3yV1UkW9AotPgsS0RwRvYpRTYvv+M9Xm3L4hbZ5CQ+uYqW9g8 xYgeLnnBgh9PLoJgiI7TCwXy/osAuA9yh1/PFIAZtfr2g== X-Received: by 127.0.0.2 with SMTP id JJo4YY7687511xA0Wc3k47Og; Fri, 29 Mar 2024 13:21:26 -0700 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web11.1032.1711743685501315215 for ; Fri, 29 Mar 2024 13:21:25 -0700 X-Received: from OSD-Desktop.redmond.corp.microsoft.com (unknown [131.107.159.43]) by linux.microsoft.com (Postfix) with ESMTPSA id D9C4A20E6F57; Fri, 29 Mar 2024 13:21:24 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D9C4A20E6F57 From: "Oliver Smith-Denny" To: devel@edk2.groups.io Cc: Liming Gao , Leif Lindholm , Ard Biesheuvel , Sami Mujawar , Taylor Beebe , Michael D Kinney , Ard Biesheuvel , =?UTF-8?q?Marvin=20H=C3=A4user?= Subject: [edk2-devel] [PATCH v3 1/3] MdeModulePkg: ImagePropertiesRecordLib: Use SectionAlignment for CodeSize Date: Fri, 29 Mar 2024 13:21:27 -0700 Message-Id: <20240329202129.12988-2-osde@linux.microsoft.com> In-Reply-To: <20240329202129.12988-1-osde@linux.microsoft.com> References: <20240329202129.12988-1-osde@linux.microsoft.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 29 Mar 2024 13:21:25 -0700 Reply-To: devel@edk2.groups.io,osde@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 8DLJOcdzAGtRuHaWOHcNQCTdx7686176AA= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=LeUIG7NL; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io When an ImageRecord is stored by ImagePropertiesRecordLib, it reports the CodeSegmentSize as the SizeOfRawData from the image. However, the image as loaded into memory is aligned to the SectionAlignment, so SizeOfRawData is under the actual size in memory. This is important, because the memory attributes table uses these image records to create its entries and it will report that the alignment of an image is incorrect, even though the actual image is correct. This was discovered on ARM64, which has a 64k runtime page granularity alignment, which is backed by a 64k section alignment for DXE_RUNTIME_DRIVERs. The runtime code and data was correctly being loaded into memory, however the memory attribute table was incorrectly reporting misaligned ranges to the OS, causing attributes to be ignored for these sections for OSes using greater than 4k pages. This patch correctly aligns the CodeSegmentSize to the SectionAlignment and the corresponding memory attribute table entries are now correctly aligned and pointing to the right places in memory. Cc: Liming Gao Cc: Leif Lindholm Cc: Ard Biesheuvel Cc: Sami Mujawar Cc: Taylor Beebe Acked-by: Michael D Kinney Reviewed-by: Ard Biesheuvel Reviewed-by: Marvin H=C3=A4user Signed-off-by: Oliver Smith-Denny --- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c |= 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index e53ce086c54c..763a8d65d565 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -1090,7 +1090,9 @@ CreateImagePropertiesRecord ( ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; =20 ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageBase + Secti= on[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; + // We still need to align the VirtualSize to the SectionAlignment be= cause MSVC does not do + // this when creating a PE image. It expects the loader to do this. + ImageRecordCodeSection->CodeSegmentSize =3D ALIGN_VALUE (Section[Ind= ex].Misc.VirtualSize, SectionAlignment); =20 InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); ImageRecord->CodeSegmentCount++; --=20 2.40.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117240): https://edk2.groups.io/g/devel/message/117240 Mute This Topic: https://groups.io/mt/105223003/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-