From b03e156491365522bfe98d2266eff2dcdb9d5b75 Mon Sep 17 00:00:00 2001 From: Ken Lautner Date: Wed, 25 Oct 2023 18:21:03 -0700 Subject: [PATCH] Made changes to use ArmSetMemoryAttributes over the individual permission helpers --- ArmPkg/ArmPkg.dec | 4 ++ ArmPkg/Drivers/CpuDxe/CpuDxe.c | 6 +- .../Include/Library/ArmStandaloneMmMmuLib.h | 71 +++++++++++++++++++ ArmPkg/Library/MmuLib/MmuLib.c | 24 +++++-- .../Arm/StandaloneMmCoreEntryPoint.c | 2 +- .../ArmMmuStandaloneMmLib.c | 4 +- 6 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 ArmPkg/Include/Library/ArmStandaloneMmMmuLib.h diff --git a/ArmPkg/ArmPkg.dec b/ArmPkg/ArmPkg.dec index e4b5812cc2..206f024593 100644 --- a/ArmPkg/ArmPkg.dec +++ b/ArmPkg/ArmPkg.dec @@ -58,6 +58,10 @@ # ArmMmuLib|Include/Library/ArmMmuLib.h + ## @libraryclass Provides a Standalone MM Mmu interface. + # + ArmStandaloneMmMmuLib|Include/Library/ArmStandaloneMmMmuLib.h + ## @libraryclass Provides a Mailbox Transport Layer (MTL) interface # for the System Control and Management Interface (SCMI). # diff --git a/ArmPkg/Drivers/CpuDxe/CpuDxe.c b/ArmPkg/Drivers/CpuDxe/CpuDxe.c index 20b3050ae6..b2d244d916 100644 --- a/ArmPkg/Drivers/CpuDxe/CpuDxe.c +++ b/ArmPkg/Drivers/CpuDxe/CpuDxe.c @@ -292,9 +292,11 @@ RemapUnusedMemoryNx ( MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + MemoryMapSize); while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) { if (MemoryMapEntry->Type == EfiConventionalMemory) { - ArmSetMemoryRegionNoExec ( + ArmSetMemoryAttributes ( MemoryMapEntry->PhysicalStart, - EFI_PAGES_TO_SIZE (MemoryMapEntry->NumberOfPages) + EFI_PAGES_TO_SIZE (MemoryMapEntry->NumberOfPages), + EFI_MEMORY_XP, + EFI_MEMORY_XP ); } diff --git a/ArmPkg/Include/Library/ArmStandaloneMmMmuLib.h b/ArmPkg/Include/Library/ArmStandaloneMmMmuLib.h new file mode 100644 index 0000000000..c9e6076627 --- /dev/null +++ b/ArmPkg/Include/Library/ArmStandaloneMmMmuLib.h @@ -0,0 +1,71 @@ +/** @file + + Copyright (c) 2015 - 2016, Linaro Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef ARM_STANDALONE_MM_MMU_LIB_H_ +#define ARM_STANDALONE_MM_MMU_LIB_H_ +#include + +#include + +/** + Convert a region of memory to read-protected, by clearing the access flag. + @param BaseAddress The start of the region. + @param Length The size of the region. + @retval EFI_SUCCESS The attributes were set successfully. + @retval EFI_OUT_OF_RESOURCES The operation failed due to insufficient memory. +**/ +EFI_STATUS +EFIAPI +ArmSetMemoryRegionNoAccess ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +/** + Convert a region of memory to read-enabled, by setting the access flag. + @param BaseAddress The start of the region. + @param Length The size of the region. + @retval EFI_SUCCESS The attributes were set successfully. + @retval EFI_OUT_OF_RESOURCES The operation failed due to insufficient memory. +**/ +EFI_STATUS +EFIAPI +ArmClearMemoryRegionNoAccess ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +EFI_STATUS +EFIAPI +ArmSetMemoryRegionNoExec ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +EFI_STATUS +EFIAPI +ArmClearMemoryRegionNoExec ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +EFI_STATUS +EFIAPI +ArmSetMemoryRegionReadOnly ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +EFI_STATUS +EFIAPI +ArmClearMemoryRegionReadOnly ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ); + +#endif // ARM_STANDALONE_MM_MMU_LIB_H_ diff --git a/ArmPkg/Library/MmuLib/MmuLib.c b/ArmPkg/Library/MmuLib/MmuLib.c index 56833b169b..f7d56e3d57 100644 --- a/ArmPkg/Library/MmuLib/MmuLib.c +++ b/ArmPkg/Library/MmuLib/MmuLib.c @@ -42,7 +42,7 @@ MmuSetAttributes ( Status = EFI_UNSUPPORTED; if (Attributes & EFI_MEMORY_XP) { - Status = ArmSetMemoryRegionNoExec (BaseAddress, Length); + Status = ArmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_XP, EFI_MEMORY_XP); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "%a - Failed to set NX. Status = %r\n", __FUNCTION__, Status)); } @@ -51,7 +51,7 @@ MmuSetAttributes ( } if (Attributes & EFI_MEMORY_RO) { - Status = ArmSetMemoryRegionReadOnly (BaseAddress, Length); + Status = ArmSetMemoryAttributes (BaseAddress, Length, EFI_MEMORY_RO, EFI_MEMORY_RO); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "%a - Failed to set RO. Status = %r\n", __FUNCTION__, Status)); } @@ -89,7 +89,15 @@ MmuClearAttributes ( Status = EFI_UNSUPPORTED; if (Attributes & EFI_MEMORY_XP) { - Status = ArmClearMemoryRegionNoExec (BaseAddress, Length); + // MU_CHANGE - START + // Use ArmSetMemoryAttributes because the individually called attribute updates have been removed + Status = ArmSetMemoryAttributes ( + BaseAddress, + Length, + 0, + Attributes + ); + // MU_CHANGE - END if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "%a - Failed to clear NX. Status = %r\n", __FUNCTION__, Status)); } @@ -98,7 +106,15 @@ MmuClearAttributes ( } if (Attributes & EFI_MEMORY_RO) { - Status = ArmClearMemoryRegionReadOnly (BaseAddress, Length); + // MU_CHANGE - START + // Use ArmSetMemoryAttributes because the individually called attribute updates have been removed + Status = ArmSetMemoryAttributes ( + BaseAddress, + Length, + 0, + Attributes + ); + // MU_CHANGE - END if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "%a - Failed to clear RO. Status = %r\n", __FUNCTION__, Status)); } diff --git a/ArmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c b/ArmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c index 96de10405a..93e50a98c6 100644 --- a/ArmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c +++ b/ArmPkg/Library/StandaloneMmCoreEntryPoint/Arm/StandaloneMmCoreEntryPoint.c @@ -21,7 +21,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include -#include +#include #include #include diff --git a/ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.c b/ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.c index 29b7f84b0c..a295962469 100644 --- a/ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.c +++ b/ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -363,7 +364,8 @@ EFI_STATUS ArmSetMemoryAttributes ( IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, - IN UINT64 Attributes + IN UINT64 Attributes, + IN UINT64 AttributeMask // MU_CHANGE - Added missing input variable ) { DEBUG ((DEBUG_ERROR, "%a() interface not implemented!\n", __FUNCTION__));