[edk2-devel] [edk2-platforms][PATCH 18/34] Silicon/Ampere: Fixup runtime memory attribute

Nhi Pham via groups.io nhi=os.amperecomputing.com at groups.io
Wed Dec 9 09:25:15 UTC 2020


This adds new FixupMemoryMap module to scan the runtime memory and apply
the XP memory attribute as OS does not complain about missing RO and XP
attributes.

Signed-off-by: Nhi Pham <nhi at os.amperecomputing.com>
---
 Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc            |  5 ++
 Platform/Ampere/JadePkg/Jade.fdf                         |  5 ++
 Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf | 41 +++++++++
 Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c   | 93 ++++++++++++++++++++
 4 files changed, 144 insertions(+)

diff --git a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
index cb03f7fc076f..746db5052c16 100755
--- a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
+++ b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc
@@ -574,6 +574,11 @@ [Components.common]
   EmbeddedPkg/MetronomeDxe/MetronomeDxe.inf
   Silicon/Ampere/AmpereAltraPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
 
+  #
+  # Fix Memory Map
+  #
+  Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf
+
   #
   # Environment Variables Protocol
   #
diff --git a/Platform/Ampere/JadePkg/Jade.fdf b/Platform/Ampere/JadePkg/Jade.fdf
index f826b14d67d0..100d5bf6f80c 100755
--- a/Platform/Ampere/JadePkg/Jade.fdf
+++ b/Platform/Ampere/JadePkg/Jade.fdf
@@ -234,6 +234,11 @@ [FV.FvMain]
   INF MdeModulePkg/Universal/HiiDatabaseDxe/HiiDatabaseDxe.inf
   INF Silicon/Ampere/AmpereAltraPkg/Drivers/MmCommunicationDxe/MmCommunication.inf
 
+  #
+  # Fixup memory map
+  #
+  INF Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf
+
   #
   # Environment Variables Protocol
   #
diff --git a/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf
new file mode 100644
index 000000000000..773e0dcd64ec
--- /dev/null
+++ b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf
@@ -0,0 +1,41 @@
+## @file
+#
+# Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = FixupMemoryMap
+  FILE_GUID                      = 183CEBEC-1229-4F59-B4F9-3BA1EC27B168
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = FixupMemoryMapInitialize
+
+[Sources.common]
+  FixupMemoryMap.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  ArmPkg/ArmPkg.dec
+  ArmPlatformPkg/ArmPlatformPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  UefiLib
+  UefiDriverEntryPoint
+  UefiBootServicesTableLib
+  DxeServicesTableLib
+
+[Guids]
+  gEfiMemoryAttributesTableGuid  ## CONSUMES ## SystemTable
+
+[Protocols]
+  gEfiCpuArchProtocolGuid
+
+[Depex]
+  TRUE
diff --git a/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c
new file mode 100644
index 000000000000..9fb7d025c64c
--- /dev/null
+++ b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c
@@ -0,0 +1,93 @@
+/** @file
+
+  Copyright (c) 2020, Ampere Computing LLC. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Uefi/UefiSpec.h>
+#include <Guid/EventGroup.h>
+#include <Guid/MemoryAttributesTable.h>
+#include <Protocol/Cpu.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+EFI_CPU_ARCH_PROTOCOL             *mCpu;
+
+/**
+  Notify function for event group EVT_SIGNAL_EXIT_BOOT_SERVICES.
+
+  @param[in]  Event   The Event that is being processed.
+  @param[in]  Context The Event Context.
+
+**/
+VOID
+EFIAPI
+OnExitBootServices(
+  IN EFI_EVENT        Event,
+  IN VOID             *Context
+  )
+{
+  EFI_STATUS                    Status;
+  EFI_MEMORY_ATTRIBUTES_TABLE   *MemoryAttributesTable;
+  EFI_MEMORY_DESCRIPTOR         *Desc;
+  UINTN                         Index;
+
+  DEBUG ((DEBUG_INFO, "%a:%d +\n", __FUNCTION__, __LINE__));
+
+  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__));
+    return;
+  }
+
+  Status = EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID **) &MemoryAttributesTable);
+  if (EFI_ERROR (Status) || MemoryAttributesTable == NULL) {
+    DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__));
+    return;
+  }
+
+  Desc = (EFI_MEMORY_DESCRIPTOR *) ((UINT64) MemoryAttributesTable + sizeof (EFI_MEMORY_ATTRIBUTES_TABLE));
+  for (Index = 0; Index < MemoryAttributesTable->NumberOfEntries; Index++) {
+    if (Desc->Type != EfiRuntimeServicesCode && Desc->Type != EfiRuntimeServicesData) {
+      Desc = (EFI_MEMORY_DESCRIPTOR *)((UINT64) Desc + MemoryAttributesTable->DescriptorSize);
+      continue;
+    }
+
+    if (!(Desc->Attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))) {
+      Desc->Attribute |= EFI_MEMORY_XP;
+      mCpu->SetMemoryAttributes (mCpu, Desc->PhysicalStart, EFI_PAGES_TO_SIZE (Desc->NumberOfPages), Desc->Attribute);
+      DEBUG ((DEBUG_INFO, "%a: Set memory attribute, Desc->PhysicalStart=0x%X, size=%d, Attributes=0x%X\n",
+                          __FUNCTION__, Desc->PhysicalStart, EFI_PAGES_TO_SIZE (Desc->NumberOfPages), Desc->Attribute));
+    }
+    Desc = (EFI_MEMORY_DESCRIPTOR *)((UINT64) Desc + MemoryAttributesTable->DescriptorSize);
+  }
+
+  DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__));
+}
+
+EFI_STATUS
+EFIAPI
+FixupMemoryMapInitialize (
+  IN EFI_HANDLE         ImageHandle,
+  IN EFI_SYSTEM_TABLE   *SystemTable
+  )
+{
+  EFI_EVENT             ExitBootServicesEvent;
+  EFI_STATUS            Status;
+
+  Status = gBS->CreateEvent (
+                  EVT_SIGNAL_EXIT_BOOT_SERVICES,
+                  TPL_NOTIFY,
+                  OnExitBootServices,
+                  NULL,
+                  &ExitBootServicesEvent);
+  ASSERT_EFI_ERROR (Status);
+
+  return EFI_SUCCESS;
+}
-- 
2.17.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#68518): https://edk2.groups.io/g/devel/message/68518
Mute This Topic: https://groups.io/mt/78825502/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-





More information about the edk2-devel-archive mailing list