]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmVirtPkg/HighMemDxe/HighMemDxe.c
ArmVirtPkg/HighMemDxe: use CPU arch protocol to apply memprotect policy
[mirror_edk2.git] / ArmVirtPkg / HighMemDxe / HighMemDxe.c
index f70978f6414fc80c0325feb7afa42339ae4699f6..4e41120deff3a198b83739d9f1b869ff8ed72cf6 100644 (file)
@@ -20,6 +20,7 @@
 #include <Library/PcdLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 \r
+#include <Protocol/Cpu.h>\r
 #include <Protocol/FdtClient.h>\r
 \r
 EFI_STATUS\r
@@ -30,6 +31,7 @@ InitializeHighMemDxe (
   )\r
 {\r
   FDT_CLIENT_PROTOCOL   *FdtClient;\r
+  EFI_CPU_ARCH_PROTOCOL *Cpu;\r
   EFI_STATUS            Status, FindNodeStatus;\r
   INT32                 Node;\r
   CONST UINT32          *Reg;\r
@@ -43,6 +45,10 @@ InitializeHighMemDxe (
                   (VOID **)&FdtClient);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL,\r
+                  (VOID **)&Cpu);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   //\r
   // Check for memory node and add the memory spaces except the lowest one\r
   //\r
@@ -78,13 +84,24 @@ InitializeHighMemDxe (
           continue;\r
         }\r
 \r
+        Status = gDS->SetMemorySpaceAttributes (CurBase, CurSize,\r
+                        EFI_MEMORY_WB);\r
+        if (EFI_ERROR (Status)) {\r
+          DEBUG ((DEBUG_WARN,\r
+            "%a: gDS->SetMemorySpaceAttributes() failed on region 0x%lx - 0x%lx (%r)\n",\r
+            __FUNCTION__, CurBase, CurBase + CurSize - 1, Status));\r
+        }\r
+\r
+        //\r
+        // Due to the ambiguous nature of the RO/XP GCD memory space attributes,\r
+        // it is impossible to add a memory space with the XP attribute in a way\r
+        // that does not result in the XP attribute being set on *all* UEFI\r
+        // memory map entries that are carved from it, including code regions\r
+        // that require executable permissions.\r
         //\r
-        // Take care not to strip any permission attributes that will have been\r
-        // set by DxeCore on the region we just added if a strict permission\r
-        // policy is in effect for EfiConventionalMemory regions.\r
-        // Unfortunately, we cannot interrogate the GCD memory space map for\r
-        // those permissions, since they are not recorded there (for historical\r
-        // reasons), so check the policy directly.\r
+        // So instead, we never set the RO/XP attributes in the GCD memory space\r
+        // capabilities or attribute fields, and apply any protections directly\r
+        // on the page table mappings by going through the cpu arch protocol.\r
         //\r
         Attributes = EFI_MEMORY_WB;\r
         if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy) &\r
@@ -92,7 +109,7 @@ InitializeHighMemDxe (
           Attributes |= EFI_MEMORY_XP;\r
         }\r
 \r
-        Status = gDS->SetMemorySpaceAttributes (CurBase, CurSize, Attributes);\r
+        Status = Cpu->SetMemoryAttributes (Cpu, CurBase, CurSize, Attributes);\r
 \r
         if (EFI_ERROR (Status)) {\r
           DEBUG ((EFI_D_ERROR,\r