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 30CD7AC1346 for ; Sat, 16 Mar 2024 12:21:38 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=BpgctWS3soB6sO+kyIJz2udChWALWJ7OIiJMc8n8zjg=; c=relaxed/simple; d=groups.io; h=Subject:To:From:User-Agent:MIME-Version:Date:References:In-Reply-To:Message-ID:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type; s=20240206; t=1710591697; v=1; b=yvebCkLEgaO9pw2Sjz3S93AmMJBCU0Cn33MGZ9LgHS4Hr1sFRMvEMuszRDK+Zr/W0wbLF8RO V6IZ4UcMI+rJKJVwFFuHIEa2VtGEw3v51yYahKXZaRLdl6NlQ5CSSPpmFmcJJO7kDVif0ik7pUh DXQ1oUUS1UbtgcXMZ4LGKMMdGp9Tc91UNEfSjnJsSHb5hYBGMNgQuarNKK3/WjfHQabgT0rpoDH gwm/B3LFoSevMN1VH4Bh7aOkNA3VKIVDmUlajdjnVaLmGHJ/V6fmxUgTLqlv4eR1OZPrqSnwef4 1jnppI07gBM7vvCt0WCuCyOLQCSHJ5JL/L1g3w9DsBXwg== X-Received: by 127.0.0.2 with SMTP id cp0lYY7687511xpHEySf4VhR; Sat, 16 Mar 2024 05:21:37 -0700 Subject: Re: [edk2-devel] [PATCH v2 1/3] MdeModulePkg: ImagePropertiesRecordLib: Use SectionAlignment for CodeSize To: "Oliver Smith-Denny" , devel@edk2.groups.io From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= X-Originating-Location: Berlin, Land Berlin, DE (104.28.62.43) X-Originating-Platform: Mac Safari 17.4 User-Agent: GROUPS.IO Web Poster MIME-Version: 1.0 Date: Sat, 16 Mar 2024 05:21:37 -0700 References: <20240311212924.11633-2-osde@linux.microsoft.com> In-Reply-To: <20240311212924.11633-2-osde@linux.microsoft.com> Message-ID: <20853.1710591697076151808@groups.io> 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 Reply-To: devel@edk2.groups.io,mhaeuser@posteo.de List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: lNGJ7568sDSjxaC8OSKbGzrtx7686176AA= Content-Type: multipart/alternative; boundary="Hlx14S0dtZapyHkdVP1Q" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=yvebCkLE; dmarc=fail reason="SPF not aligned (strict), DKIM not aligned (strict)" header.from=posteo.de (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 --Hlx14S0dtZapyHkdVP1Q Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Reviewed-by: Marvin H=C3=A4user On Mon, Mar 11, 2024 at 02:29 PM, Oliver Smith-Denny wrote: >=20 > 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. >=20 > 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. >=20 > 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. >=20 > Cc: Liming Gao > Cc: Leif Lindholm > Cc: Ard Biesheuvel > Cc: Sami Mujawar > Cc: Taylor Beebe >=20 > Signed-off-by: Oliver Smith-Denny > --- > MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c= =3D > | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) >=20 > diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= =3D >=20 > sRecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropert= =3D >=20 > iesRecordLib.c > index e53ce086c54c..763a8d65d565 100644 > --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecord= =3D >=20 > Lib.c > +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecord= =3D >=20 > Lib.c > @@ -1090,7 +1090,9 @@ CreateImagePropertiesRecord ( > ImageRecordCodeSection->Signature =3D3D IMAGE_PROPERTIES_RECORD_CODE=3D > _SECTION_SIGNATURE; > =3D20 > ImageRecordCodeSection->CodeSegmentBase =3D3D (UINTN)ImageBase + Sec=3D > tion[Index].VirtualAddress; > - ImageRecordCodeSection->CodeSegmentSize =3D3D Section[Index].SizeOfR=3D > awData; > + // We still need to align the VirtualSize to the SectionAlignment =3D > because MSVC does not do > + // this when creating a PE image. It expects the loader to do this=3D > . > + ImageRecordCodeSection->CodeSegmentSize =3D3D ALIGN_VALUE (Section[I=3D > ndex].Misc.VirtualSize, SectionAlignment); > =3D20 > InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSec=3D > tion->Link); > ImageRecord->CodeSegmentCount++; > --=3D20 > 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 (#116822): https://edk2.groups.io/g/devel/message/116822 Mute This Topic: https://groups.io/mt/104873193/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- --Hlx14S0dtZapyHkdVP1Q Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Reviewed-by: Marvin Häuser <mhaeuser@posteo.de>

On Mo= n, Mar 11, 2024 at 02:29 PM, Oliver Smith-Denny wrote:
When an ImageRecord is stored by ImagePropertiesRecordLib, it r= eports the
CodeSegmentSize as the SizeOfRawData from the image. Howeve= r, the image
as loaded into memory is aligned to the SectionAlignment,= so
SizeOfRawData is under the actual size in memory. This is importan= t,
because the memory attributes table uses these image records to cre= ate
its entries and it will report that the alignment of an image isincorrect, even though the actual image is correct.

This was= discovered on ARM64, which has a 64k runtime page granularity
alignme= nt, which is backed by a 64k section alignment for
DXE_RUNTIME_DRIVERs= . The runtime code and data was correctly being
loaded into memory, ho= wever 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 align= s the CodeSegmentSize to the SectionAlignment
and the corresponding me= mory attribute table entries are now correctly
aligned and pointing to= the right places in memory.

Cc: Liming Gao <gaoliming@byosof= t.com.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
= Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar = <sami.mujawar@arm.com>
Cc: Taylor Beebe <taylor.d.beebe@gmail= .com>

Signed-off-by: Oliver Smith-Denny <osde@linux.micros= oft.com>
---
MdeModulePkg/Library/ImagePropertiesRecordLib/Ima= gePropertiesRecordLib.c=3D
| 4 +++-
1 file changed, 3 insertions(= +), 1 deletion(-)

diff --git a/MdeModulePkg/Library/ImagePropert= iesRecordLib/ImagePropertie=3D
sRecordLib.c b/MdeModulePkg/Library/Ima= gePropertiesRecordLib/ImagePropert=3D
iesRecordLib.c
index e53ce0= 86c54c..763a8d65d565 100644
--- a/MdeModulePkg/Library/ImageProperties= RecordLib/ImagePropertiesRecord=3D
Lib.c
+++ b/MdeModulePkg/Libra= ry/ImagePropertiesRecordLib/ImagePropertiesRecord=3D
Lib.c
@@ -10= 90,7 +1090,9 @@ CreateImagePropertiesRecord (
ImageRecordCodeSection-&= gt;Signature =3D3D IMAGE_PROPERTIES_RECORD_CODE=3D
_SECTION_SIGNATURE;=
=3D20
ImageRecordCodeSection->CodeSegmentBase =3D3D (UINTN)Im= ageBase + Sec=3D
tion[Index].VirtualAddress;
- ImageRecordCodeSec= tion->CodeSegmentSize =3D3D Section[Index].SizeOfR=3D
awData;
= + // We still need to align the VirtualSize to the SectionAlignment =3D
because MSVC does not do
+ // this when creating a PE image. It expe= cts the loader to do this=3D
.
+ ImageRecordCodeSection->CodeS= egmentSize =3D3D ALIGN_VALUE (Section[I=3D
ndex].Misc.VirtualSize, Sec= tionAlignment);
=3D20
InsertTailList (&ImageRecord->CodeSe= gmentList, &ImageRecordCodeSec=3D
tion->Link);
ImageRecord= ->CodeSegmentCount++;
--=3D20
2.40.1
_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#116822) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--Hlx14S0dtZapyHkdVP1Q--