[edk2-devel] [PATCH v3 09/14] ArmPkg: Add Basic MMU Lib for Arm silicon

Bret Barkelew posted 14 patches 4 years, 3 months ago
[edk2-devel] [PATCH v3 09/14] ArmPkg: Add Basic MMU Lib for Arm silicon
Posted by Bret Barkelew 4 years, 3 months ago
From: Sean Brogan <sean.brogan@microsoft.com>

After adding the abstract MmuLib to MdePkg, add an implementation
backed by the existing ArmMmuLib to ArmPkg.

This implementation is currently just a shim to the
old library, while enabling higher-level code to be more
common.

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3651

Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Signed-off-by: Bret Barkelew <bret.barkelew@microsoft.com>
---
 ArmPkg/Library/MmuLib/MmuLib.c       | 120 ++++++++++++++++++++
 ArmPkg/ArmPkg.dsc                    |   1 +
 ArmPkg/Library/MmuLib/BaseMmuLib.inf |  30 +++++
 3 files changed, 151 insertions(+)

diff --git a/ArmPkg/Library/MmuLib/MmuLib.c b/ArmPkg/Library/MmuLib/MmuLib.c
new file mode 100644
index 000000000000..6f3b664971da
--- /dev/null
+++ b/ArmPkg/Library/MmuLib/MmuLib.c
@@ -0,0 +1,120 @@
+/** @file
+This library instance implements a very limited MMU Lib instance
+for the ARM/AARCH64 architectures.  This library shims a common library
+interface to the ArmPkg defined ArmMmuLib.ib.
+
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/ArmMmuLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MmuLib.h>
+
+/**
+  Bitwise sets the memory attributes on a range of memory based on an attributes mask.
+
+  @param  BaseAddress           The start of the range for which to set attributes.
+  @param  Length                The length of the range.
+  @param  Attributes            A bitmask of the attributes to set. See "Physical memory
+                                protection attributes" in UefiSpec.h
+
+  @return EFI_SUCCESS
+  @return Others
+
+**/
+EFI_STATUS
+EFIAPI
+MmuSetAttributes (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length,
+  IN  UINT64                    Attributes
+  )
+{
+    EFI_STATUS Status;
+
+    Status = EFI_UNSUPPORTED;
+
+    if ((Attributes & EFI_MEMORY_XP) == EFI_MEMORY_XP) {
+      Status = ArmSetMemoryRegionNoExec (BaseAddress, Length);
+      if (EFI_ERROR(Status)) {
+        DEBUG((DEBUG_ERROR, "%a - Failed to set NX.  Status = %r\n", __FUNCTION__, Status));
+      }
+    }
+
+    ASSERT_EFI_ERROR(Status);
+    return Status;
+}
+
+
+/**
+  Bitwise clears the memory attributes on a range of memory based on an attributes mask.
+
+  @param  BaseAddress           The start of the range for which to clear attributes.
+  @param  Length                The length of the range.
+  @param  Attributes            A bitmask of the attributes to clear. See "Physical memory
+                                protection attributes" in UefiSpec.h
+
+  @return EFI_SUCCESS
+  @return Others
+
+**/
+EFI_STATUS
+EFIAPI
+MmuClearAttributes (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  IN  UINT64                    Length,
+  IN  UINT64                    Attributes
+  )
+{
+    EFI_STATUS Status;
+
+    Status = EFI_UNSUPPORTED;
+
+    if ((Attributes & EFI_MEMORY_XP) == EFI_MEMORY_XP) {
+      Status = ArmClearMemoryRegionNoExec (BaseAddress, Length);
+      if (EFI_ERROR(Status)) {
+        DEBUG((DEBUG_ERROR, "%a - Failed to clear NX.  Status = %r\n", __FUNCTION__, Status));
+      }
+    }
+
+    if ((Attributes & EFI_MEMORY_RO) == EFI_MEMORY_RO) {
+      Status = ArmClearMemoryRegionReadOnly(BaseAddress, Length);
+      if (EFI_ERROR(Status)) {
+        DEBUG((DEBUG_ERROR, "%a - Failed to clear RO.  Status = %r\n", __FUNCTION__, Status));
+      }
+    }
+
+    ASSERT_EFI_ERROR(Status);
+    return Status;
+}
+
+
+/**
+  Returns the memory attributes on a range of memory.
+
+  @param  BaseAddress           The start of the range for which to set attributes.
+  @param  Attributes            A return pointer for the attributes.
+
+  @return EFI_SUCCESS
+  @return EFI_INVALID_PARAMETER   A return pointer is NULL.
+  @return Others
+
+**/
+EFI_STATUS
+EFIAPI
+MmuGetAttributes (
+  IN  EFI_PHYSICAL_ADDRESS      BaseAddress,
+  OUT UINT64                    *Attributes
+  )
+{
+    EFI_STATUS Status;
+
+    Status = EFI_UNSUPPORTED;
+
+    DEBUG ((DEBUG_ERROR, "%a() API not implemented\n", __FUNCTION__));
+
+    ASSERT_EFI_ERROR(Status);
+    return Status;
+}
diff --git a/ArmPkg/ArmPkg.dsc b/ArmPkg/ArmPkg.dsc
index f9268de8347e..197519a64718 100644
--- a/ArmPkg/ArmPkg.dsc
+++ b/ArmPkg/ArmPkg.dsc
@@ -165,3 +165,4 @@ [Components.AARCH64]
 
 [Components.AARCH64, Components.ARM]
   ArmPkg/Library/StandaloneMmMmuLib/ArmMmuStandaloneMmLib.inf
+  ArmPkg/Library/MmuLib/BaseMmuLib.inf
diff --git a/ArmPkg/Library/MmuLib/BaseMmuLib.inf b/ArmPkg/Library/MmuLib/BaseMmuLib.inf
new file mode 100644
index 000000000000..15095abee9c3
--- /dev/null
+++ b/ArmPkg/Library/MmuLib/BaseMmuLib.inf
@@ -0,0 +1,30 @@
+## @file
+# This library instance implements a very limited MMU Lib instance
+# for the ARM/AARCH64 architectures.  This library shims a common library
+# interface to the ArmPkg defined ArmMmuLib.
+#
+# Copyright (c) Microsoft Corporation.
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = MmuLib
+  FILE_GUID                      = 6f2ee9a4-79b3-4b77-9a47-e2bd4b917b75
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = MmuLib
+
+[Sources]
+  MmuLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  ArmPkg/ArmPkg.dec
+
+[LibraryClasses]
+  DebugLib
+  ArmMmuLib
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#83621): https://edk2.groups.io/g/devel/message/83621
Mute This Topic: https://groups.io/mt/86971568/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-