From nobody Sun May 19 10:01:21 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+99466+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+99466+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1675337277; cv=none; d=zohomail.com; s=zohoarc; b=BmH5M3e+SmMOvwjqkiq1oeEUYw96AZLXtMr3YSZ/nXGaQgQdhpfQgIPSZejL1SIb79DnRVLW83ds7mjUxKMHa80IgC4KpOQEzsmco8/kGfpnjnFawwEi0z2XCc60LFb68dF78A4cbdu6NlmMCJGZQgN4wTA2bryo9Ma25ENve24= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675337277; 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=iQI92XKfsBJzCmdvP41HODMUfrB9Q6o+n8rXmXnnC4E=; b=Vx3WW/Z67vB6i9c0ZhvY+YKFGCmMNbhGVr3LcV6fVuHsmC6na9UgpfRGzLOCet6v4n6nC/WO4aHoQ/iQxWez21H+fXT7/O6rejDSGbv/81ev1/JL+IqNGCRY29+dOQquj7B5R9zWOPh1QYYU+xvoazdrnoiYcnE68bgNF44Twlo= 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+99466+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 1675337277984365.2120597594136; Thu, 2 Feb 2023 03:27:57 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id fcthYY1788612xaA8MjNY26K; Thu, 02 Feb 2023 03:27:57 -0800 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.12396.1675337276999803720 for ; Thu, 02 Feb 2023 03:27:57 -0800 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 82F1F61AED; Thu, 2 Feb 2023 11:27:56 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5F2FC4339B; Thu, 2 Feb 2023 11:27:53 +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 v2 1/3] MdePkg: Add Memory Attribute Protocol definition Date: Thu, 2 Feb 2023 12:27:20 +0100 Message-Id: <20230202112722.2200755-2-ardb@kernel.org> In-Reply-To: <20230202112722.2200755-1-ardb@kernel.org> References: <20230202112722.2200755-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: hVeOlHjCFrsP8SsUbnHwVXmRx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675337277; bh=94iZX5sbyAy+WI96FyE9SmzC2xJt9z+GikxIK/9YMz8=; h=Cc:Date:From:Reply-To:Subject:To; b=xRR3QRSQU48K5skpULJGy3I4uGg+jUiq8LFdtrGtWXBksKnUhIhOoBwbl/fT3tKGPqa 2vkJiV3Ri73kdPcKyVQyfBmRxWcCYpa/GoPF2ve0vthaJ63wUqgNw9f/JlOFsrH/7EGuI bSgDXPoFw9Q0BQjakoM6QWl4BAblj0RpReo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675337279425100001 Content-Type: text/plain; charset="utf-8" Add the Memory Attribute Protocol definition, which was adopted and included in version 2.10 of the UEFI specification. Link: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3519 Signed-off-by: Ard Biesheuvel Reviewed-by: Liming Gao --- MdePkg/Include/Protocol/MemoryAttribute.h | 142 ++++++++++++++++++++ MdePkg/MdePkg.dec | 3 + 2 files changed, 145 insertions(+) diff --git a/MdePkg/Include/Protocol/MemoryAttribute.h b/MdePkg/Include/Pro= tocol/MemoryAttribute.h new file mode 100644 index 000000000000..ff930fb21aa6 --- /dev/null +++ b/MdePkg/Include/Protocol/MemoryAttribute.h @@ -0,0 +1,142 @@ +/** @file + + EFI Memory Attribute Protocol provides retrieval and update service + for memory attributes in EFI environment. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ Copyright (c) 2023, Google LLC. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __EFI_MEMORY_ATTRIBUTE_H__ +#define __EFI_MEMORY_ATTRIBUTE_H__ + +#define EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID \ + { \ + 0xf4560cf6, 0x40ec, 0x4b4a, { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1= , 0x89 } \ + } + +typedef struct _EFI_MEMORY_ATTRIBUTE_PROTOCOL EFI_MEMORY_ATTRIBUTE_PROTOCO= L; + +/** + This function set given attributes of the memory region specified by + BaseAddress and Length. + + The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes The bit mask of attributes to set for the memo= ry + region. + + @retval EFI_SUCCESS The attributes were set for the memory reg= ion. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combinatio= n of + attributes that cannot be set together. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + The bit mask of attributes is not supporte= d for + the memory resource range specified by + BaseAddress and Length. + @retval EFI_OUT_OF_RESOURCES Requested attributes cannot be applied due= to + lack of system resources. + @retval EFI_ACCESS_DENIED Attributes for the requested memory region= are + controlled by system firmware and cannot be + updated via the protocol. + +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_SET_MEMORY_ATTRIBUTES)( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ); + +/** + This function clears given attributes of the memory region specified by + BaseAddress and Length. + + The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes The bit mask of attributes to clear for the me= mory + region. + + @retval EFI_SUCCESS The attributes were cleared for the memory= region. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combinatio= n of + attributes that cannot be cleared together. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + The bit mask of attributes is not supporte= d for + the memory resource range specified by + BaseAddress and Length. + @retval EFI_OUT_OF_RESOURCES Requested attributes cannot be applied due= to + lack of system resources. + @retval EFI_ACCESS_DENIED Attributes for the requested memory region= are + controlled by system firmware and cannot be + updated via the protocol. + +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_CLEAR_MEMORY_ATTRIBUTES)( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ); + +/** + This function retrieves the attributes of the memory region specified by + BaseAddress and Length. If different attributes are got from different p= art + of the memory region, EFI_NO_MAPPING will be returned. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes Pointer to attributes returned. + + @retval EFI_SUCCESS The attributes got for the memory region. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes is NULL. + @retval EFI_NO_MAPPING Attributes are not consistent cross the me= mory + region. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + +**/ +typedef +EFI_STATUS +(EFIAPI *EFI_GET_MEMORY_ATTRIBUTES)( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + OUT UINT64 *Attributes + ); + +/// +/// EFI Memory Attribute Protocol provides services to retrieve or update +/// attribute of memory in the EFI environment. +/// +struct _EFI_MEMORY_ATTRIBUTE_PROTOCOL { + EFI_GET_MEMORY_ATTRIBUTES GetMemoryAttributes; + EFI_SET_MEMORY_ATTRIBUTES SetMemoryAttributes; + EFI_CLEAR_MEMORY_ATTRIBUTES ClearMemoryAttributes; +}; + +extern EFI_GUID gEfiMemoryAttributeProtocolGuid; + +#endif diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 3d08f20d15b0..a8658403c8fd 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -1915,6 +1915,9 @@ [Protocols] ## Include/Protocol/RedfishDiscover.h gEfiRedfishDiscoverProtocolGuid =3D { 0x5db12509, 0x4550, 0x4347, {= 0x96, 0xb3, 0x73, 0xc0, 0xff, 0x6e, 0x86, 0x9f }} =20 + ## Include/Protocol/MemoryAttribute.h + gEfiMemoryAttributeProtocolGuid =3D { 0xf4560cf6, 0x40ec, 0x4b4a, { 0xa1= , 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 }} + # # Protocols defined in Shell2.0 # --=20 2.39.0 -=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 (#99466): https://edk2.groups.io/g/devel/message/99466 Mute This Topic: https://groups.io/mt/96697936/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- From nobody Sun May 19 10:01:21 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+99467+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+99467+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1675337280; cv=none; d=zohomail.com; s=zohoarc; b=bomTyiB6VXlHoC3s9rttFekdxWXaO0Go7HgEazlYQt6qtUKQWsTSkk9nb5RKUMVI+EWu3WhSoZAO6G+qrD5cfjBU30satwbPd3GqtSaCoFGywYuxoPJiGyT73kCKVl5zX653LWh/2Gu3q+gOtB06cyGM/S5+uJ39VCMTkb6IK+A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675337280; 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=6abPjrGyRIdLzze5/GqYXymVb4KO8au6vP0d30W5Eps=; b=Qf0XBJz7lBxrOMB5eHudwqvIiZOWDoP6RxLZuFQzEu3hyZxs2RgdOE+ZsXYNffXSd6ltz8QQAStP/RS5y3qtpUmno6L0QzN8l6V5SCS5yDvISYQ54HCD8J9JQFh0dQUlurBEnK2fOSkgAULL48pg7wxZ7AHG1POhe94nFf+tc6o= 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+99467+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 1675337280124863.079297596455; Thu, 2 Feb 2023 03:28:00 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id Se8pYY1788612xjdUTePZ0Uo; Thu, 02 Feb 2023 03:27:59 -0800 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.12352.1675337279205681673 for ; Thu, 02 Feb 2023 03:27:59 -0800 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 A516761AD7; Thu, 2 Feb 2023 11:27:58 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F5F3C433A0; Thu, 2 Feb 2023 11:27:56 +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 v2 2/3] ArmPkg/CpuDxe: Unify PageAttributeToGcdAttribute helper Date: Thu, 2 Feb 2023 12:27:21 +0100 Message-Id: <20230202112722.2200755-3-ardb@kernel.org> In-Reply-To: <20230202112722.2200755-1-ardb@kernel.org> References: <20230202112722.2200755-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: 4Ph87som4bTNfRZ4f2KdSWjEx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675337279; bh=p7CfyHzDV2okvRMKi8eJzFbLyzefRjRHnoLsoyZ1fog=; h=Cc:Date:From:Reply-To:Subject:To; b=TzTeKcZHlOj4bhe7SpL9RAUQGbFCG5PieZGpqaNCfeo9tm/s5FeXAYT37F2BrW0yeCU KK1/2LyssqzGqX6PCNS7h5VQuYIO989BSj+4hjYIA1Oka1PQedBhG9IWo4veUDSk1tqV1 ViuibupSQH8YR43U0OYuNDr8B25KlQ4BaUc= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675337281420100006 Content-Type: text/plain; charset="utf-8" In preparation for introducing an implementation of the EFI memory attributes protocol that is shared between ARM and AArch64, unify the existing code that converts a page table descriptor into a EFI_MEMORY_xxx bitfield, so it can be called from the generic code. Signed-off-by: Ard Biesheuvel --- ArmPkg/Drivers/CpuDxe/AArch64/Mmu.c | 5 +-- ArmPkg/Drivers/CpuDxe/Arm/Mmu.c | 46 +++++++++++--------- ArmPkg/Drivers/CpuDxe/CpuDxe.h | 5 +++ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/ArmPkg/Drivers/CpuDxe/AArch64/Mmu.c b/ArmPkg/Drivers/CpuDxe/AA= rch64/Mmu.c index 8bb33046e707..4ec9fc0a582c 100644 --- a/ArmPkg/Drivers/CpuDxe/AArch64/Mmu.c +++ b/ArmPkg/Drivers/CpuDxe/AArch64/Mmu.c @@ -30,13 +30,12 @@ GetRootTranslationTableInfo ( *RootTableEntryCount =3D TT_ENTRY_COUNT >> (T0SZ - MIN_T0SZ) % BITS_PER_= LEVEL; } =20 -STATIC UINT64 PageAttributeToGcdAttribute ( - IN UINT64 PageAttributes + IN UINTN PageAttributes ) { - UINT64 GcdAttributes; + UINTN GcdAttributes; =20 switch (PageAttributes & TT_ATTR_INDX_MASK) { case TT_ATTR_INDX_DEVICE_MEMORY: diff --git a/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c b/ArmPkg/Drivers/CpuDxe/Arm/Mm= u.c index 2daf47ba6fe5..9545a1c1d2d3 100644 --- a/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c +++ b/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c @@ -77,39 +77,46 @@ SectionToGcdAttributes ( return EFI_SUCCESS; } =20 -EFI_STATUS -PageToGcdAttributes ( - IN UINT32 PageAttributes, - OUT UINT64 *GcdAttributes +UINT64 +PageAttributeToGcdAttribute ( + IN UINTN PageAttributes ) { - *GcdAttributes =3D 0; + UINT64 GcdAttributes; =20 // determine cacheability attributes switch (PageAttributes & TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK) { case TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED: - *GcdAttributes |=3D EFI_MEMORY_UC; + GcdAttributes =3D EFI_MEMORY_UC; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_SHAREABLE_DEVICE: - *GcdAttributes |=3D EFI_MEMORY_UC; + GcdAttributes =3D EFI_MEMORY_UC; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC: - *GcdAttributes |=3D EFI_MEMORY_WT; + GcdAttributes =3D EFI_MEMORY_WT; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_NO_ALLOC: - *GcdAttributes |=3D EFI_MEMORY_WB; + GcdAttributes =3D EFI_MEMORY_WB; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE: - *GcdAttributes |=3D EFI_MEMORY_WC; + GcdAttributes =3D EFI_MEMORY_WC; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC: - *GcdAttributes |=3D EFI_MEMORY_WB; + GcdAttributes =3D EFI_MEMORY_WB; break; case TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_SHAREABLE_DEVICE: - *GcdAttributes |=3D EFI_MEMORY_UC; + GcdAttributes =3D EFI_MEMORY_UC; break; default: - return EFI_UNSUPPORTED; + DEBUG (( + DEBUG_ERROR, + "PageAttributeToGcdAttribute: PageAttributes:0x%X not supported.\n= ", + PageAttributes + )); + ASSERT (0); + // The Global Coherency Domain (GCD) value is defined as a bit set. + // Returning 0 means no attribute has been set. + GcdAttributes =3D 0; } =20 // determine protection attributes @@ -126,7 +133,7 @@ PageToGcdAttributes ( // read only cases map to write-protect case TT_DESCRIPTOR_PAGE_AP_RO_NO: case TT_DESCRIPTOR_PAGE_AP_RO_RO: - *GcdAttributes |=3D EFI_MEMORY_RO; + GcdAttributes |=3D EFI_MEMORY_RO; break; =20 default: @@ -135,10 +142,10 @@ PageToGcdAttributes ( =20 // now process eXectue Never attribute if ((PageAttributes & TT_DESCRIPTOR_PAGE_XN_MASK) !=3D 0 ) { - *GcdAttributes |=3D EFI_MEMORY_XP; + GcdAttributes |=3D EFI_MEMORY_XP; } =20 - return EFI_SUCCESS; + return GcdAttributes; } =20 EFI_STATUS @@ -152,7 +159,6 @@ SyncCacheConfigPage ( IN OUT UINT32 *NextSectionAttributes ) { - EFI_STATUS Status; UINT32 i; volatile ARM_PAGE_TABLE_ENTRY *SecondLevelTable; UINT32 NextPageAttributes; @@ -183,8 +189,7 @@ SyncCacheConfigPage ( NextPageAttributes =3D PageAttributes; } else if (PageAttributes !=3D NextPageAttributes) { // Convert Section Attributes into GCD Attributes - Status =3D PageToGcdAttributes (NextPageAttributes, &GcdAttributes= ); - ASSERT_EFI_ERROR (Status); + GcdAttributes =3D PageAttributeToGcdAttribute (NextPageAttributes); =20 // update GCD with these changes (this will recurse into our own C= puSetMemoryAttributes below which is OK) SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, = *NextRegionBase, *NextRegionLength, GcdAttributes); @@ -196,8 +201,7 @@ SyncCacheConfigPage ( } } else if (NextPageAttributes !=3D 0) { // Convert Page Attributes into GCD Attributes - Status =3D PageToGcdAttributes (NextPageAttributes, &GcdAttributes); - ASSERT_EFI_ERROR (Status); + GcdAttributes =3D PageAttributeToGcdAttribute (NextPageAttributes); =20 // update GCD with these changes (this will recurse into our own Cpu= SetMemoryAttributes below which is OK) SetGcdMemorySpaceAttributes (MemorySpaceMap, NumberOfDescriptors, *N= extRegionBase, *NextRegionLength, GcdAttributes); diff --git a/ArmPkg/Drivers/CpuDxe/CpuDxe.h b/ArmPkg/Drivers/CpuDxe/CpuDxe.h index ff672390ce51..8933fa90c4ed 100644 --- a/ArmPkg/Drivers/CpuDxe/CpuDxe.h +++ b/ArmPkg/Drivers/CpuDxe/CpuDxe.h @@ -126,4 +126,9 @@ SetGcdMemorySpaceAttributes ( IN UINT64 Attributes ); =20 +UINT64 +PageAttributeToGcdAttribute ( + IN UINTN PageAttributes + ); + #endif // CPU_DXE_H_ --=20 2.39.0 -=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 (#99467): https://edk2.groups.io/g/devel/message/99467 Mute This Topic: https://groups.io/mt/96697937/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- From nobody Sun May 19 10:01:21 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+99468+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+99468+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1675337283; cv=none; d=zohomail.com; s=zohoarc; b=Net072uJCE7GpQZz2/BdSKAaoGqt08kiEuT2MpCjggTT0YWEpBefkQYV3O6+tTb1Y/toLTKY2pquM0QvC4Qw5TMw376LGliCmy90Girvq4yKAe9VL/Ow5xN0bH61HkgkKho6HlHztDDe0dlhPtaA/zmtaNXBzWGKj9dcL8oH8Xw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675337283; 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=sxu8lsEBpH0HcTnv+LBFp3F2FPLYCr9EpqaMvuCDNIM=; b=Fet1APCuSv0sFjoEYW5ImTd8R+Yckc3L/SNh56TtUyb3KqOTlc8ck/QTvinnT/A6duZBBuYkhVcDf1u9mif1jDlN2yjcwLMzsAqWXM0iCX1cvjHIkfd4hWbdF0z5L6i3K1BI45xhs9Ln4ETiTbGL0rkkVW6HorbPUcLZ8F5J50w= 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+99468+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 1675337283057102.78426758732326; Thu, 2 Feb 2023 03:28:03 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id nKBDYY1788612xhSz47NlK8m; Thu, 02 Feb 2023 03:28:02 -0800 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.12398.1675337282157162624 for ; Thu, 02 Feb 2023 03:28:02 -0800 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 8BE9361A88; Thu, 2 Feb 2023 11:28:01 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDCDFC433A0; Thu, 2 Feb 2023 11:27:58 +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 v2 3/3] ArmPkg/CpuDxe: Implement EFI memory attributes protocol Date: Thu, 2 Feb 2023 12:27:22 +0100 Message-Id: <20230202112722.2200755-4-ardb@kernel.org> In-Reply-To: <20230202112722.2200755-1-ardb@kernel.org> References: <20230202112722.2200755-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: rEg1gq9Z8dGgSPSchYqCFY2vx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1675337282; bh=R+MKWhfIIs24Vqvztvzt5uh94RhCgat+J1MmrqyQ6g4=; h=Cc:Date:From:Reply-To:Subject:To; b=no2+SWRqwRV7xA5KYAP1arxUmXoV0t+Im+3urNykuhy4zCwn9I7G9dKoe1YYro5dDUb QPbWn5eGiEhSApGDvYCd6+cXbgaTmLepC4bnuknLt7tRDZBOS1Dp3MlwjDN9RDYOUSzHo bVF7kyFb0D4kxoXOyCHYQSYX27c0yt3skjY= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1675337283452100009 Content-Type: text/plain; charset="utf-8" Expose the protocol introduced in v2.10 that permits the caller to manage mapping permissions in the page tables. Signed-off-by: Ard Biesheuvel --- ArmPkg/Drivers/CpuDxe/CpuDxe.c | 2 + ArmPkg/Drivers/CpuDxe/CpuDxe.h | 3 + ArmPkg/Drivers/CpuDxe/CpuDxe.inf | 2 + ArmPkg/Drivers/CpuDxe/MemoryAttribute.c | 252 ++++++++++++++++++++ 4 files changed, 259 insertions(+) diff --git a/ArmPkg/Drivers/CpuDxe/CpuDxe.c b/ArmPkg/Drivers/CpuDxe/CpuDxe.c index e6742f0a25fc..d04958e79e52 100644 --- a/ArmPkg/Drivers/CpuDxe/CpuDxe.c +++ b/ArmPkg/Drivers/CpuDxe/CpuDxe.c @@ -244,6 +244,8 @@ CpuDxeInitialize ( &mCpuHandle, &gEfiCpuArchProtocolGuid, &mCpu, + &gEfiMemoryAttributeProtocolGuid, + &mMemoryAttribute, NULL ); =20 diff --git a/ArmPkg/Drivers/CpuDxe/CpuDxe.h b/ArmPkg/Drivers/CpuDxe/CpuDxe.h index 8933fa90c4ed..f45d2bc101a3 100644 --- a/ArmPkg/Drivers/CpuDxe/CpuDxe.h +++ b/ArmPkg/Drivers/CpuDxe/CpuDxe.h @@ -30,9 +30,12 @@ #include #include #include +#include =20 extern BOOLEAN mIsFlushingGCD; =20 +extern EFI_MEMORY_ATTRIBUTE_PROTOCOL mMemoryAttribute; + /** This function registers and enables the handler specified by InterruptHa= ndler for a processor interrupt or exception type specified by InterruptType. If InterruptHand= ler is NULL, then the diff --git a/ArmPkg/Drivers/CpuDxe/CpuDxe.inf b/ArmPkg/Drivers/CpuDxe/CpuDx= e.inf index 10792b393fc8..e732e21cb94a 100644 --- a/ArmPkg/Drivers/CpuDxe/CpuDxe.inf +++ b/ArmPkg/Drivers/CpuDxe/CpuDxe.inf @@ -23,6 +23,7 @@ [Sources.Common] CpuDxe.h CpuMmuCommon.c Exception.c + MemoryAttribute.c =20 [Sources.ARM] Arm/Mmu.c @@ -53,6 +54,7 @@ [LibraryClasses] =20 [Protocols] gEfiCpuArchProtocolGuid + gEfiMemoryAttributeProtocolGuid =20 [Guids] gEfiDebugImageInfoTableGuid diff --git a/ArmPkg/Drivers/CpuDxe/MemoryAttribute.c b/ArmPkg/Drivers/CpuDx= e/MemoryAttribute.c new file mode 100644 index 000000000000..827deb94dd99 --- /dev/null +++ b/ArmPkg/Drivers/CpuDxe/MemoryAttribute.c @@ -0,0 +1,252 @@ +/** @file + + Copyright (c) 2023, Google LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "CpuDxe.h" + +/** + This function retrieves the attributes of the memory region specified by + BaseAddress and Length. If different attributes are got from different p= art + of the memory region, EFI_NO_MAPPING will be returned. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes Pointer to attributes returned. + + @retval EFI_SUCCESS The attributes got for the memory region. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes is NULL. + @retval EFI_NO_MAPPING Attributes are not consistent cross the me= mory + region. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + +**/ +STATIC +EFI_STATUS +GetMemoryAttributes ( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + OUT UINT64 *Attributes + ) +{ + UINTN RegionAddress; + UINTN RegionLength; + UINTN RegionAttributes; + UINTN Union; + UINTN Intersection; + EFI_STATUS Status; + + if ((Length =3D=3D 0) || (Attributes =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + DEBUG ((DEBUG_INFO, + "%a: BaseAddress =3D=3D 0x%lx, Length =3D=3D 0x%lx\n", + __FUNCTION__, + (UINTN)BaseAddress, + (UINTN)Length + )); + + Union =3D 0; + Intersection =3D MAX_UINTN; + + for (RegionAddress =3D (UINTN)BaseAddress; + RegionAddress < (UINTN)(BaseAddress + Length); + RegionAddress +=3D RegionLength) { + + Status =3D GetMemoryRegion (&RegionAddress, + &RegionLength, + &RegionAttributes + ); + + DEBUG ((DEBUG_INFO, + "%a: RegionAddress =3D=3D 0x%lx, RegionLength =3D=3D 0x%lx, Re= gionAttributes =3D=3D 0x%lx\n", + __FUNCTION__, + RegionAddress, + RegionLength, + RegionAttributes + )); + + if (EFI_ERROR (Status)) { + return EFI_NO_MAPPING; + } + + Union |=3D RegionAttributes; + Intersection &=3D RegionAttributes; + } + + DEBUG ((DEBUG_INFO, + "%a: Union =3D=3D %lx, Intersection =3D=3D %lx\n", + __FUNCTION__, + Union, + Intersection + )); + + if (Union !=3D Intersection) { + return EFI_NO_MAPPING; + } + + *Attributes =3D PageAttributeToGcdAttribute (Union) & (EFI_MEMORY_RO | E= FI_MEMORY_XP); + return EFI_SUCCESS; +} + +/** + This function set given attributes of the memory region specified by + BaseAddress and Length. + + The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes The bit mask of attributes to set for the memo= ry + region. + + @retval EFI_SUCCESS The attributes were set for the memory reg= ion. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combinatio= n of + attributes that cannot be set together. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + The bit mask of attributes is not supporte= d for + the memory resource range specified by + BaseAddress and Length. + @retval EFI_OUT_OF_RESOURCES Requested attributes cannot be applied due= to + lack of system resources. + @retval EFI_ACCESS_DENIED Attributes for the requested memory region= are + controlled by system firmware and cannot be + updated via the protocol. + +**/ +STATIC +EFI_STATUS +SetMemoryAttributes ( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ) +{ + EFI_STATUS Status; + + DEBUG ((DEBUG_INFO, + "%a: BaseAddress =3D=3D 0x%lx, Length =3D=3D 0x%lx, Attributes = =3D=3D 0x%lx\n", + __FUNCTION__, + (UINTN)BaseAddress, + (UINTN)Length, + (UINTN)Attributes + )); + + if ((Length =3D=3D 0) || + ((Attributes & ~(EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP)) != =3D 0)) { + return EFI_INVALID_PARAMETER; + } + + if ((Attributes & EFI_MEMORY_RP) !=3D 0) { + return EFI_UNSUPPORTED; + } + + if ((Attributes & EFI_MEMORY_RO) !=3D 0) { + Status =3D ArmSetMemoryRegionReadOnly (BaseAddress, Length); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + if ((Attributes & EFI_MEMORY_XP) !=3D 0) { + Status =3D ArmSetMemoryRegionNoExec (BaseAddress, Length); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + return EFI_SUCCESS; +} + +/** + This function clears given attributes of the memory region specified by + BaseAddress and Length. + + The valid Attributes is EFI_MEMORY_RP, EFI_MEMORY_XP, and EFI_MEMORY_RO. + + @param This The EFI_MEMORY_ATTRIBUTE_PROTOCOL instance. + @param BaseAddress The physical address that is the start address= of + a memory region. + @param Length The size in bytes of the memory region. + @param Attributes The bit mask of attributes to clear for the me= mory + region. + + @retval EFI_SUCCESS The attributes were cleared for the memory= region. + @retval EFI_INVALID_PARAMETER Length is zero. + Attributes specified an illegal combinatio= n of + attributes that cannot be cleared together. + @retval EFI_UNSUPPORTED The processor does not support one or more + bytes of the memory resource range specifi= ed + by BaseAddress and Length. + The bit mask of attributes is not supporte= d for + the memory resource range specified by + BaseAddress and Length. + @retval EFI_OUT_OF_RESOURCES Requested attributes cannot be applied due= to + lack of system resources. + @retval EFI_ACCESS_DENIED Attributes for the requested memory region= are + controlled by system firmware and cannot be + updated via the protocol. + +**/ +STATIC +EFI_STATUS +ClearMemoryAttributes ( + IN EFI_MEMORY_ATTRIBUTE_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length, + IN UINT64 Attributes + ) +{ + EFI_STATUS Status; + + DEBUG ((DEBUG_INFO, + "%a: BaseAddress =3D=3D 0x%lx, Length =3D=3D 0x%lx, Attributes = =3D=3D 0x%lx\n", + __FUNCTION__, + (UINTN)BaseAddress, + (UINTN)Length, + (UINTN)Attributes + )); + + if ((Length =3D=3D 0) || + ((Attributes & ~(EFI_MEMORY_RO | EFI_MEMORY_RP | EFI_MEMORY_XP)) != =3D 0)) { + return EFI_INVALID_PARAMETER; + } + + if ((Attributes & EFI_MEMORY_RO) !=3D 0) { + Status =3D ArmClearMemoryRegionReadOnly (BaseAddress, Length); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + if ((Attributes & EFI_MEMORY_XP) !=3D 0) { + Status =3D ArmClearMemoryRegionNoExec (BaseAddress, Length); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + } + + return EFI_SUCCESS; +} + +EFI_MEMORY_ATTRIBUTE_PROTOCOL mMemoryAttribute =3D { + GetMemoryAttributes, + SetMemoryAttributes, + ClearMemoryAttributes +}; --=20 2.39.0 -=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 (#99468): https://edk2.groups.io/g/devel/message/99468 Mute This Topic: https://groups.io/mt/96697939/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-