]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg DxeCore: Fix for missing Memory Attributes Table (MAT) update
authorMike Turner <miketur@microsoft.com>
Fri, 16 Aug 2019 15:39:35 +0000 (23:39 +0800)
committerLiming Gao <liming.gao@intel.com>
Tue, 20 Aug 2019 11:53:31 +0000 (19:53 +0800)
The Fpdt driver (FirmwarePerformanceDxe) saves a memory address across
reboots, and then does an AllocatePage for that memory address.
If, on this boot, that memory comes from a Runtime memory bucket,
the MAT table is not updated. This causes Windows to boot into Recovery.

This patch blocks the memory manager from changing the page
from a special bucket to a different memory type.  Once the buckets are
allocated, we freeze the memory ranges for the OS, and fragmenting
the special buckets will cause errors resuming from hibernate (S4).

The references to S4 here are the use case that fails.  This
failure is root caused to an inconsistent behavior of the
core memory services themselves when type AllocateAddress is used.

The main issue is apparently with the UEFI memory map -- the UEFI memory
map reflects the pre-allocated bins, but the actual allocations at fixed
addresses may go out of sync with that. Everything else, such as:
- EFI_MEMORY_ATTRIBUTES_TABLE (page protections) being out of sync,
- S4 failing
are just symptoms / consequences.

This patch is cherry pick from Project Mu:
https://github.com/microsoft/mu_basecore/commit/a9be767d9be96af94016ebd391ea6f340920735a
With the minor change,
1. Update commit message format to keep the message in 80 characters one line.
2. Remove // MU_CHANGE comments in source code.
3. Update comments style to follow edk2 style.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Liming Gao <liming.gao@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Dandan Bi <dandan.bi@intel.com>
Acked-by: Hao A Wu <hao.a.wu@intel.com>
MdeModulePkg/Core/Dxe/Mem/Page.c

index bd9e116aa59cb620045c8fbc2a9278d130da990e..1f0e3d94b93d3cc1c1c55e54be4912250e417add 100644 (file)
@@ -1265,12 +1265,13 @@ CoreInternalAllocatePages (
   IN BOOLEAN                NeedGuard\r
   )\r
 {\r
-  EFI_STATUS      Status;\r
-  UINT64          Start;\r
-  UINT64          NumberOfBytes;\r
-  UINT64          End;\r
-  UINT64          MaxAddress;\r
-  UINTN           Alignment;\r
+  EFI_STATUS       Status;\r
+  UINT64           Start;\r
+  UINT64           NumberOfBytes;\r
+  UINT64           End;\r
+  UINT64           MaxAddress;\r
+  UINTN            Alignment;\r
+  EFI_MEMORY_TYPE  CheckType;\r
 \r
   if ((UINT32)Type >= MaxAllocateType) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1321,6 +1322,7 @@ CoreInternalAllocatePages (
   // if (Start + NumberOfBytes) rolls over 0 or\r
   // if Start is above MAX_ALLOC_ADDRESS or\r
   // if End is above MAX_ALLOC_ADDRESS,\r
+  // if Start..End overlaps any tracked MemoryTypeStatistics range\r
   // return EFI_NOT_FOUND.\r
   //\r
   if (Type == AllocateAddress) {\r
@@ -1336,6 +1338,33 @@ CoreInternalAllocatePages (
         (End > MaxAddress)) {\r
       return EFI_NOT_FOUND;\r
     }\r
+\r
+    //\r
+    // A driver is allowed to call AllocatePages using an AllocateAddress type.  This type of\r
+    // AllocatePage request the exact physical address if it is not used.  The existing code\r
+    // will allow this request even in 'special' pages.  The problem with this is that the\r
+    // reason to have 'special' pages for OS hibernate/resume is defeated as memory is\r
+    // fragmented.\r
+    //\r
+\r
+    for (CheckType = (EFI_MEMORY_TYPE) 0; CheckType < EfiMaxMemoryType; CheckType++) {\r
+      if (MemoryType != CheckType &&\r
+          mMemoryTypeStatistics[CheckType].Special &&\r
+          mMemoryTypeStatistics[CheckType].NumberOfPages > 0) {\r
+        if (Start >= mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            Start <= mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+        if (End >= mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            End <= mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+        if (Start < mMemoryTypeStatistics[CheckType].BaseAddress &&\r
+            End   > mMemoryTypeStatistics[CheckType].MaximumAddress) {\r
+          return EFI_NOT_FOUND;\r
+        }\r
+      }\r
+    }\r
   }\r
 \r
   if (Type == AllocateMaxAddress) {\r