On Mon, Mar 13, 2023 at 18:16:57 +0100, Ard Biesheuvel wrote:
> 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 <ardb@kernel.org>
> ---
> 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
>
> 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 <PiDxe.h>
> +
> +#include <Library/ArmMmuLib.h>
> +#include <Library/DebugLib.h>
> +#include <Protocol/Cpu.h>
> +
> +extern EFI_CPU_SET_MEMORY_ATTRIBUTES gCpuSetMemoryAttributes;
Could this declaration live in a global include?
/
Leif
> +
> +STATIC UINTN mRecursionLevel;
> +
> +/**
> + Clone of CPU_ARCH_PROTOCOL::SetMemoryAttributes() which is made available 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 function.
> + ASSERT (mRecursionLevel < 2);
> +
> + mRecursionLevel++;
> +
> + Status = ArmSetMemoryAttributes (
> + BaseAddress,
> + Length,
> + Attributes
> + );
> +
> + mRecursionLevel--;
> + return Status;
> +}
> +
> +/**
> + Library constructor.
> +
> + @retval RETURN_SUCCESS Operation successful.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +ArmSetMemoryOverrideLibConstructor (
> + VOID
> + )
> +{
> + gCpuSetMemoryAttributes = 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 = 1.29
> + BASE_NAME = ArmSetMemoryOverrideLib
> + FILE_GUID = 849a43c0-6ad9-428e-8a5a-e090f7853bd3
> + MODULE_TYPE = BASE
> + VERSION_STRING = 1.0
> + LIBRARY_CLASS = NULL|DXE_CORE
> + CONSTRUCTOR = ArmSetMemoryOverrideLibConstructor
> +
> +[Sources.common]
> + ArmSetMemoryOverrideLib.c
> +
> +[Packages]
> + ArmPkg/ArmPkg.dec
> + MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> + ArmMmuLib
> + DebugLib
> --
> 2.39.2
>
>
>
>
>
>
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#101269): https://edk2.groups.io/g/devel/message/101269
Mute This Topic: https://groups.io/mt/97586026/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3901457/1787277/102458076/xyzzy [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-