UefiCpuPkg/CpuDxe: Fix out-of-sync issue in page attributes
authorJian J Wang <jian.j.wang@intel.com>
Sat, 16 Sep 2017 13:26:28 +0000 (21:26 +0800)
committerStar Zeng <star.zeng@intel.com>
Thu, 21 Sep 2017 01:38:15 +0000 (09:38 +0800)
From CpuDxe driver perspective, it doesn't update GCD memory attributes from
current page table setup during its initialization. So the memory attributes in
GCD might not reflect all memory attributes in real world.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Suggested-by: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
UefiCpuPkg/CpuDxe/CpuDxe.c
UefiCpuPkg/CpuDxe/CpuDxe.h
UefiCpuPkg/CpuDxe/CpuPageTable.c

index b386f3b677c69f713e2d88e01d50f9aaf185e5fe..4e8fa100e0e225435d635fd6e4952416f815e4df 100644 (file)
@@ -863,6 +863,11 @@ RefreshGcdMemoryAttributes (
     FreePool (MemorySpaceMap);\r
   }\r
 \r
+  //\r
+  // Update page attributes\r
+  //\r
+  RefreshGcdMemoryAttributesFromPaging();\r
+\r
   mIsFlushingGCD = FALSE;\r
 }\r
 \r
index 4861abee76252902a940a8bc2126a1b9d6ccfa42..a25b35c6ebf01d6f844a2dd6582b1778a3d997dd 100644 (file)
                                        EFI_MEMORY_UCE   \\r
                                        )\r
 \r
+#define EFI_MEMORY_PAGETYPE_MASK      (EFI_MEMORY_RP  | \\r
+                                       EFI_MEMORY_XP  | \\r
+                                       EFI_MEMORY_RO    \\r
+                                       )\r
 \r
 /**\r
   Flush CPU data cache. If the instruction cache is fully coherent\r
@@ -261,5 +265,10 @@ SetDataSelectors (
   UINT16 Selector\r
   );\r
 \r
+VOID\r
+RefreshGcdMemoryAttributesFromPaging (\r
+  VOID\r
+  );\r
+\r
 #endif\r
 \r
index 2c61e7503e234eae95b2ec646ae926d297fc5ba8..ae93f3f5534f446ae2f066922c50c83e61acb20f 100644 (file)
@@ -23,6 +23,8 @@
 #include <Library/DebugLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Protocol/MpService.h>\r
+\r
+#include "CpuDxe.h"\r
 #include "CpuPageTable.h"\r
 \r
 ///\r
@@ -767,6 +769,103 @@ AssignMemoryPageAttributes (
   return Status;\r
 }\r
 \r
+/**\r
+  Update GCD memory space attributes according to current page table setup.\r
+**/\r
+VOID\r
+RefreshGcdMemoryAttributesFromPaging (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  UINTN                               NumberOfDescriptors;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR     *MemorySpaceMap;\r
+  PAGE_TABLE_LIB_PAGING_CONTEXT       PagingContext;\r
+  PAGE_ATTRIBUTE                      PageAttribute;\r
+  UINT64                              *PageEntry;\r
+  UINT64                              PageLength;\r
+  UINT64                              MemorySpaceLength;\r
+  UINT64                              Length;\r
+  UINT64                              BaseAddress;\r
+  UINT64                              PageStartAddress;\r
+  UINT64                              Attributes;\r
+  UINT64                              Capabilities;\r
+  BOOLEAN                             DoUpdate;\r
+  UINTN                               Index;\r
+\r
+  //\r
+  // Assuming that memory space map returned is sorted already; otherwise sort\r
+  // them in the order of lowest address to highest address.\r
+  //\r
+  Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  GetCurrentPagingContext (&PagingContext);\r
+\r
+  BaseAddress = 0;\r
+  PageLength  = 0;\r
+  for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
+    if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
+      continue;\r
+    }\r
+\r
+    if (MemorySpaceMap[Index].BaseAddress >= (BaseAddress + PageLength)) {\r
+      //\r
+      // Current memory space starts at a new page. Resetting PageLength will\r
+      // trigger a retrieval of page attributes at new address.\r
+      //\r
+      PageLength = 0;\r
+    } else {\r
+      //\r
+      // In case current memory space is not adjacent to last one\r
+      //\r
+      PageLength -= (MemorySpaceMap[Index].BaseAddress - BaseAddress);\r
+    }\r
+\r
+    // Sync real page attributes to GCD\r
+    BaseAddress       = MemorySpaceMap[Index].BaseAddress;\r
+    MemorySpaceLength = MemorySpaceMap[Index].Length;\r
+    while (MemorySpaceLength > 0) {\r
+      if (PageLength == 0) {\r
+        PageEntry = GetPageTableEntry (&PagingContext, BaseAddress, &PageAttribute);\r
+        if (PageEntry == NULL) {\r
+          break;\r
+        }\r
+\r
+        //\r
+        // Note current memory space might start in the middle of a page\r
+        //\r
+        PageStartAddress  = (*PageEntry) & (UINT64)PageAttributeToMask(PageAttribute);\r
+        PageLength        = PageAttributeToLength (PageAttribute) - (BaseAddress - PageStartAddress);\r
+        Attributes        = GetAttributesFromPageEntry (PageEntry);\r
+\r
+        if (Attributes != (MemorySpaceMap[Index].Attributes & EFI_MEMORY_PAGETYPE_MASK)) {\r
+          DoUpdate = TRUE;\r
+          Attributes |= (MemorySpaceMap[Index].Attributes & ~EFI_MEMORY_PAGETYPE_MASK);\r
+          Capabilities = Attributes | MemorySpaceMap[Index].Capabilities;\r
+        } else {\r
+          DoUpdate = FALSE;\r
+        }\r
+      }\r
+\r
+      Length = MIN (PageLength, MemorySpaceLength);\r
+      if (DoUpdate) {\r
+        gDS->SetMemorySpaceCapabilities (BaseAddress, Length, Capabilities);\r
+        gDS->SetMemorySpaceAttributes (BaseAddress, Length, Attributes);\r
+        DEBUG ((DEBUG_INFO, "Update memory space attribute: [%02d] %016lx - %016lx (%08lx -> %08lx)\r\n",\r
+                             Index, BaseAddress, BaseAddress + Length - 1,\r
+                             MemorySpaceMap[Index].Attributes, Attributes));\r
+      }\r
+\r
+      PageLength        -= Length;\r
+      MemorySpaceLength -= Length;\r
+      BaseAddress       += Length;\r
+    }\r
+  }\r
+\r
+  FreePool (MemorySpaceMap);\r
+}\r
+\r
 /**\r
   Initialize the Page Table lib.\r
 **/\r