Skip to content

Commit

Permalink
Made changes to use ArmSetMemoryAttributes over the individual permis…
Browse files Browse the repository at this point in the history
…sion helpers
  • Loading branch information
kenlautner committed Dec 19, 2023
1 parent 35d97fd commit b03e156
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ArmPkg/ArmPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -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).
#
Expand Down
6 changes: 4 additions & 2 deletions ArmPkg/Drivers/CpuDxe/CpuDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
71 changes: 71 additions & 0 deletions ArmPkg/Include/Library/ArmStandaloneMmMmuLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/** @file
Copyright (c) 2015 - 2016, Linaro Ltd. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef ARM_STANDALONE_MM_MMU_LIB_H_
#define ARM_STANDALONE_MM_MMU_LIB_H_
#include <Uefi/UefiBaseType.h>

#include <Library/ArmLib.h>

/**
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_
24 changes: 20 additions & 4 deletions ArmPkg/Library/MmuLib/MmuLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/SerialPortLib.h>
#include <Library/StandaloneMmMmuLib.h>
#include <Library/ArmStandaloneMmMmuLib.h>
#include <Library/PcdLib.h>

#include <IndustryStandard/ArmStdSmc.h>
Expand Down
4 changes: 3 additions & 1 deletion ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <Library/ArmLib.h>
#include <Library/ArmMmuLib.h>
#include <Library/ArmStandaloneMmMmuLib.h>
#include <Library/ArmSvcLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
Expand Down Expand Up @@ -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__));
Expand Down

0 comments on commit b03e156

Please sign in to comment.