From nobody Mon Sep 16 19:14:37 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+101125+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+101125+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1678727912; cv=none; d=zohomail.com; s=zohoarc; b=NWZUcQarBWV1cac3lCkXbH1TbSaVE9sUbbQxvXsBji3+T1TeUUW6o7cz0+qK9e6AbYmeHmYrnmdYJCrNPlyU/qQ7aZkVOUrMp3w+2yO3SyE1j/OYtdy8T7st28lIn4yX3D9tnoiiLEeDcsy0DlT7YrACjGZAw1pGXIX3w2Q3I6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678727912; 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=6BTDZn4xYYQjJPb7A2Z0CV2wadgJjUEAZtN7iwYxYtQ=; b=Mf0HPBiP2jGw4z6txJzt+lMGxRyvoaFskUsoOXc/kkiuvOvcF6A+Pc+PnucpLf8qdIKuGHAvNRAwVS8PhR0Q8LmCAUyDaDH+6qmr/VjSpIVzQ5hJwUHgzXpJSiA9rDIF3g+7XzhfMVDEt520Ht/bdbxHeZPHGz9fCpExAgorZBY= 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+101125+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 1678727912166160.03124748887774; Mon, 13 Mar 2023 10:18:32 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id HoJ9YY1788612xjGjLtokqwG; Mon, 13 Mar 2023 10:18:31 -0700 X-Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web10.25813.1678727910867828690 for ; Mon, 13 Mar 2023 10:18:31 -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 ams.source.kernel.org (Postfix) with ESMTPS id 6BB1DB811B1; Mon, 13 Mar 2023 17:18:29 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id E379BC433D2; Mon, 13 Mar 2023 17:18:26 +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 21/38] ArmPkg: Implement ArmSetMemoryOverrideLib Date: Mon, 13 Mar 2023 18:16:57 +0100 Message-Id: <20230313171714.3866151-22-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: 4f4R2iZtFkwW1zGH6Sz0Ics3x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678727911; bh=9+m2BNgPDueiLZ9OOrh3gnK/iIRnxzhBITXlboQHQdE=; h=Cc:Date:From:Reply-To:Subject:To; b=XD0YJQzBl4SCwvQtpVg2WKFyySvGU8ndhJaKMppdHbo8xduwwKo2IEov5pwuFuiv2R4 GB34NEBVRLufpHGvDcpHsdfAy6ljTSTdx77Gabm/uxcAUmsF+dvndvbyvPVjiwhG4w/VM +/CtTr+c2XofrnCeb4C8ne/oBiC/hCTc7y4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678727914181100004 Content-Type: text/plain; charset="utf-8" Implement the ARM version of a NULL class library that can be overlaid on top of the DXE core to equip it right from its launch with an implementation of the CPU arch protocol member that sets type and permission attributes on memory regions. This bridges the gap between dispatch of DXE core and dispatch of the DXE driver that implements the CPU arch protocol, removing the need to rely on memory mappings that are writable and executable at the same time. Signed-off-by: Ard Biesheuvel --- ArmPkg/ArmPkg.dsc | 1 + ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.c | 78 ++= ++++++++++++++++++ ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.inf | 28 ++= +++++ 3 files changed, 107 insertions(+) diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc index 3fb95d1951a9..43eb0f4f463e 100644 --- a/ArmPkg/ArmPkg.dsc +++ b/ArmPkg/ArmPkg.dsc @@ -119,6 +119,7 @@ [Components.common] ArmPkg/Library/ArmPsciResetSystemLib/ArmPsciResetSystemLib.inf ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.inf ArmPkg/Library/ArmExceptionLib/ArmRelocateExceptionLib.inf + ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.inf =20 ArmPkg/Drivers/CpuDxe/CpuDxe.inf ArmPkg/Drivers/CpuPei/CpuPei.inf diff --git a/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib= .c b/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.c new file mode 100644 index 000000000000..866dbbdaa7d5 --- /dev/null +++ b/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.c @@ -0,0 +1,78 @@ +/** @file + Overlay implementation of DXE core gCpuSetMemoryAttributes for ARM. + + Copyright (c) 2023, Google LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include + +#include +#include +#include + +extern EFI_CPU_SET_MEMORY_ATTRIBUTES gCpuSetMemoryAttributes; + +STATIC UINTN mRecursionLevel; + +/** + Clone of CPU_ARCH_PROTOCOL::SetMemoryAttributes() which is made availabl= e to + the DXE core by NULL library class resolution, so that it can manage page + permissions right from the start. + + @param This CPU arch protocol pointer, should be NULL. + @param BaseAddress Start address of the region. + @param Length Size of the region, in bytes. + @param Attributes Attributes to set on the region. + + @retval EFI_SUCCESS Operation completed successfully. + @retval EFI_OUT_OF_RESOURCES Operation failed due to lack of memory. + +**/ +STATIC +EFI_STATUS +EFIAPI +EarlyArmSetMemoryAttributes ( + IN EFI_CPU_ARCH_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ) +{ + EFI_STATUS Status; + + // There are cases where the use of strict memory permissions may trigger + // unbounded recursion in the page table code. This happens when setting + // memory permissions results in a page table split and therefore a page + // allocation, which could trigger a recursive invocation of this functi= on. + ASSERT (mRecursionLevel < 2); + + mRecursionLevel++; + + Status =3D ArmSetMemoryAttributes ( + BaseAddress, + Length, + Attributes + ); + + mRecursionLevel--; + return Status; +} + +/** + Library constructor. + + @retval RETURN_SUCCESS Operation successful. + +**/ +RETURN_STATUS +EFIAPI +ArmSetMemoryOverrideLibConstructor ( + VOID + ) +{ + gCpuSetMemoryAttributes =3D EarlyArmSetMemoryAttributes; + + return RETURN_SUCCESS; +} diff --git a/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib= .inf b/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.inf new file mode 100644 index 000000000000..2dc9d42d09bd --- /dev/null +++ b/ArmPkg/Library/ArmSetMemoryOverrideLib/ArmSetMemoryOverrideLib.inf @@ -0,0 +1,28 @@ +## @file +# Overlay implementation of DXE core gCpuSetMemoryAttributes for ARM. +# +# Copyright (c) 2023, Google LLC. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.29 + BASE_NAME =3D ArmSetMemoryOverrideLib + FILE_GUID =3D 849a43c0-6ad9-428e-8a5a-e090f7853bd3 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D NULL|DXE_CORE + CONSTRUCTOR =3D ArmSetMemoryOverrideLibConstructor + +[Sources.common] + ArmSetMemoryOverrideLib.c + +[Packages] + ArmPkg/ArmPkg.dec + MdePkg/MdePkg.dec + +[LibraryClasses] + ArmMmuLib + DebugLib --=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 (#101125): https://edk2.groups.io/g/devel/message/101125 Mute This Topic: https://groups.io/mt/97586026/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-