From nobody Mon Sep 16 19:17:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101120+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101120+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1678727898; cv=none; d=zohomail.com; s=zohoarc; b=i9hpZSIawme+N2YqElz7H6nh98B47CdWKGamJmqX6b2HygYW5gpHkVUrb0yPakDHwpCLdyu9qtWja2Tlp/hNfuDILzGwsPBpYo1JtgnV0wnNxnCsq7brjqzzgZiZak+FSF2cltc6+DHGY9esk3LKZfWB1SZx4PswA5yNNslsaQQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678727898; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=EGm0+M8XX0LeMrcwfl1uHoYtbtts4QDe6lP1OBmo9nc=; b=VPwiMyjkedN/fKLnf7H1f94z4LL3PyzLE9fXLhZrOi1LIgIlBb+NsXguzd0dkYB8XoEkEOj98tp/eqaHuD/JRHVtWhp9yyCbgn3pI7rnXK2/YMZHAwTbkQHfmaGCIwXMsqy9nIF9d/7Vi7JYTxjSd1Bn5rSJyGsPhiGWR3+mjaI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101120+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1678727898174569.5944549593522; Mon, 13 Mar 2023 10:18:18 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id hwWeYY1788612x19C3S8DPou; Mon, 13 Mar 2023 10:18:17 -0700 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.26492.1678727896978083534 for ; Mon, 13 Mar 2023 10:18:17 -0700 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7DEF161381; Mon, 13 Mar 2023 17:18:16 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2F2BC433A0; Mon, 13 Mar 2023 17:18:13 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe Subject: [edk2-devel] [PATCH v5 16/38] MdePkg/BasePeCoffLib: Add API to keep track of relocation range Date: Mon, 13 Mar 2023 18:16:52 +0100 Message-Id: <20230313171714.3866151-17-ardb@kernel.org> In-Reply-To: <20230313171714.3866151-1-ardb@kernel.org> References: <20230313171714.3866151-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,ardb@kernel.org X-Gm-Message-State: tjBm5nZrxZ0968o0wB7JLSlIx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678727897; bh=WxyGHM2rYIa10NywHnJCOVvPkO8VCMear2y0VzP8gUs=; h=Cc:Date:From:Reply-To:Subject:To; b=hbotGIwvBURJh2exuQQhdkdxj5Bn3AtWofvMJyeM7UHL8I4HKIbm+7ZD7At7jOmUaK/ aeBP2yzKD5r+i4XQm13QZ2tDzgnQM+1hy91qzdK/wgOt9jC/kJRz4u3mtUjFdjfHGx7kl k5pgwwNwGcQBRSW3GvKcVUZ4waoMb6cl4LM= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678727900090100001 Content-Type: text/plain; charset="utf-8" Add a library call to obtain the start and end of the region covered by relocation fixups. This will be used in a future patch to limit the range of memory that needs to be remapped with read-write-execute permissions at ExitBootServices() time. Signed-off-by: Ard Biesheuvel --- MdePkg/Include/Library/PeCoffLib.h | 23 ++++++ MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 82 +++++++++++++++++++- 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/Pe= CoffLib.h index b45879453785..df2f7f5e5961 100644 --- a/MdePkg/Include/Library/PeCoffLib.h +++ b/MdePkg/Include/Library/PeCoffLib.h @@ -382,4 +382,27 @@ PeCoffLoaderUnloadImage ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ); =20 +/** + Retrieve the range subject to relocation fixups from the recorded fixup = data + of a runtime image + + @param ImageBase The base address of a PE/COFF image tha= t has been loaded + and relocated into system memory. + @param ImageSize The size, in bytes, of the PE/COFF imag= e. + @param RelocationData A pointer to the relocation data that w= as collected when the + PE/COFF image was relocated using PeCof= fLoaderRelocateImage(). + @param[out] RelocationRangeMin The start of the relocated range. + @param[out] RelocationRangeMax The end of the relocated range. + +**/ +VOID +EFIAPI +PeCoffLoaderGetRelocationRange ( + IN PHYSICAL_ADDRESS ImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData, + OUT PHYSICAL_ADDRESS *RelocationRangeMin, + OUT PHYSICAL_ADDRESS *RelocationRangeMax + ); + #endif diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/Bas= ePeCoffLib/BasePeCoff.c index 97a8aaf8c73d..31e1f2035963 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c @@ -936,6 +936,8 @@ PeCoffLoaderRelocateImage ( PHYSICAL_ADDRESS BaseAddress; UINT32 NumberOfRvaAndSizes; UINT32 TeStrippedOffset; + PHYSICAL_ADDRESS *RelocRangeStart; + PHYSICAL_ADDRESS *RelocRangeEnd; =20 ASSERT (ImageContext !=3D NULL); =20 @@ -1043,6 +1045,21 @@ PeCoffLoaderRelocateImage ( // Run the relocation information and apply the fixups // FixupData =3D ImageContext->FixupData; + if (FixupData !=3D NULL) { + FixupData =3D ALIGN_POINTER (FixupData, sizeof (PHYSICAL_ADDRESS)); + + // + // Use the first two UINT64s in the fixup data to keep track of the = start + // and end of the region that is subject to relocation fixups. + // + RelocRangeStart =3D (PHYSICAL_ADDRESS *)FixupData; + RelocRangeEnd =3D RelocRangeStart + 1; + FixupData +=3D 2 * sizeof (PHYSICAL_ADDRESS); + + *RelocRangeStart =3D MAX_UINT64; + *RelocRangeEnd =3D 0; + } + while ((UINTN)RelocBase < (UINTN)RelocBaseEnd) { Reloc =3D (UINT16 *)((CHAR8 *)RelocBase + sizeof (EFI_IMAGE_BASE_REL= OCATION)); // @@ -1070,6 +1087,14 @@ PeCoffLoaderRelocateImage ( return RETURN_LOAD_ERROR; } =20 + // + // Capture this page in the recorded relocation range + // + if (FixupData !=3D NULL) { + *RelocRangeStart =3D MIN (*RelocRangeStart, (UINTN)FixupBase); + *RelocRangeEnd =3D MAX (*RelocRangeEnd, (UINTN)FixupBase + SIZE_= 4KB); + } + // // Run this relocation record // @@ -1470,6 +1495,9 @@ PeCoffLoaderLoadImage ( // ImageContext->FixupData =3D NULL; =20 + // Add two UINT64s at the start to carry the min/max of the relocated ra= nge + ImageContext->FixupDataSize +=3D 2 * sizeof (PHYSICAL_ADDRESS); + // // Load the Codeview information if present // @@ -1824,7 +1852,8 @@ PeCoffLoaderRelocateImageForRuntime ( // by code will not be fixed up, since that would set them back to // defaults. // - FixupData =3D RelocationData; + FixupData =3D ALIGN_POINTER (RelocationData, sizeof (PHYSICAL_ADDR= ESS)); + FixupData +=3D 2 * sizeof (PHYSICAL_ADDRESS); RelocBaseOrig =3D RelocBase; while ((UINTN)RelocBase < (UINTN)RelocBaseEnd) { // @@ -1994,3 +2023,54 @@ PeCoffLoaderUnloadImage ( PeCoffLoaderUnloadImageExtraAction (ImageContext); return RETURN_SUCCESS; } + +/** + Retrieve the range subject to relocation fixups from the recorded fixup = data + of a runtime image + + @param ImageBase The base address of a PE/COFF image tha= t has been loaded + and relocated into system memory. + @param ImageSize The size, in bytes, of the PE/COFF imag= e. + @param RelocationData A pointer to the relocation data that w= as collected when the + PE/COFF image was relocated using PeCof= fLoaderRelocateImage(). + @param[out] RelocationRangeMin The start of the relocated range. + @param[out] RelocationRangeMax The end of the relocated range. + +**/ +VOID +EFIAPI +PeCoffLoaderGetRelocationRange ( + IN PHYSICAL_ADDRESS ImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData, + OUT PHYSICAL_ADDRESS *RelocationRangeMin, + OUT PHYSICAL_ADDRESS *RelocationRangeMax + ) +{ + PHYSICAL_ADDRESS *FixupData; + + if ((RelocationData =3D=3D NULL) || (ImageBase =3D=3D 0x0)) { + return; + } + + FixupData =3D ALIGN_POINTER (RelocationData, sizeof (PHYSICAL_ADDRESS)); + + if ((FixupData[0] =3D=3D MAX_UINT64) && (FixupData[1] =3D=3D 0)) { + // No fixups recorded + *RelocationRangeMin =3D ImageBase; + *RelocationRangeMax =3D ImageBase; + return; + } + + if ((FixupData[0] < ImageBase) || + (FixupData[1] > (ImageBase + ImageSize))) + { + ASSERT (FALSE); + *RelocationRangeMin =3D ImageBase; + *RelocationRangeMax =3D ImageBase + ImageSize; + return; + } + + *RelocationRangeMin =3D FixupData[0]; + *RelocationRangeMax =3D FixupData[1]; +} --=20 2.39.2 -=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 (#101120): https://edk2.groups.io/g/devel/message/101120 Mute This Topic: https://groups.io/mt/97586017/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-