]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Fix Memory Attributes table type issue
authorJiewen Yao <jiewen.yao@intel.com>
Tue, 23 Feb 2016 04:43:57 +0000 (12:43 +0800)
committerHao Wu <hao.a.wu@intel.com>
Thu, 25 Feb 2016 03:23:43 +0000 (11:23 +0800)
According to the spec, each entry in the Memory
Attributes table shall have the same type as
the region it was carved out of in the UEFI memory map.
The current attribute uses RTData for PE Data, but
it should be RTCode.

This patch fixed the issue. It is validated with or
without PropertiesTable.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: "Yao, Jiewen" <jiewen.yao@intel.com>
Tested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c
MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c

index c84146a8e8edec4f1296829823e6bd47f26c2f41..416ed3dca846b481014e802e62c7424b4dd251d4 100644 (file)
@@ -72,8 +72,6 @@ CoreGetMemoryMapPropertiesTable (
 \r
 extern EFI_PROPERTIES_TABLE  mPropertiesTable;\r
 \r
-BOOLEAN         mIsConstructingMemoryAttributesTable = FALSE;\r
-\r
 /**\r
   Install MemoryAttributesTable.\r
 \r
@@ -105,8 +103,6 @@ InstallMemoryAttributesTable (
     return ;\r
   }\r
 \r
-  mIsConstructingMemoryAttributesTable = TRUE;\r
-\r
   MemoryMapSize = 0;\r
   MemoryMap = NULL;\r
   Status = CoreGetMemoryMapPropertiesTable (\r
@@ -181,8 +177,6 @@ InstallMemoryAttributesTable (
 \r
   Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);\r
   ASSERT_EFI_ERROR (Status);\r
-\r
-  mIsConstructingMemoryAttributesTable = FALSE;\r
 }\r
 \r
 /**\r
index 6e5ad033198886c071b3bea4bde23f621d2be63f..ebe70966db78fd9f32bdc84074c565b34072008c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI PropertiesTable support\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -80,14 +80,7 @@ EFI_PROPERTIES_TABLE  mPropertiesTable = {
 \r
 EFI_LOCK           mPropertiesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
 \r
-//\r
-// Temporary save for original memory map.\r
-// This is for MemoryAttributesTable only.\r
-//\r
-extern BOOLEAN         mIsConstructingMemoryAttributesTable;\r
-EFI_MEMORY_DESCRIPTOR  *mMemoryMapOrg;\r
-UINTN                  mMemoryMapOrgSize;\r
-UINTN                  mDescriptorSize;\r
+BOOLEAN            mPropertiesTableEnable;\r
 \r
 //\r
 // Below functions are for MemoryMap\r
@@ -198,42 +191,6 @@ SortMemoryMap (
   return ;\r
 }\r
 \r
-/**\r
-  Check if this memory entry spans across original memory map boundary.\r
-\r
-  @param PhysicalStart   The PhysicalStart of memory\r
-  @param NumberOfPages   The NumberOfPages of memory\r
-\r
-  @retval TRUE  This memory entry spans across original memory map boundary.\r
-  @retval FALSE This memory entry does not span cross original memory map boundary.\r
-**/\r
-STATIC\r
-BOOLEAN\r
-DoesEntrySpanAcrossBoundary (\r
-  IN UINT64                      PhysicalStart,\r
-  IN UINT64                      NumberOfPages\r
-  )\r
-{\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEntry;\r
-  EFI_MEMORY_DESCRIPTOR       *MemoryMapEnd;\r
-  UINT64                      MemoryBlockLength;\r
-\r
-  MemoryMapEntry = mMemoryMapOrg;\r
-  MemoryMapEnd   = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) mMemoryMapOrg + mMemoryMapOrgSize);\r
-  while (MemoryMapEntry < MemoryMapEnd) {\r
-    MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
-\r
-    if ((MemoryMapEntry->PhysicalStart <= PhysicalStart) &&\r
-        (MemoryMapEntry->PhysicalStart + MemoryBlockLength > PhysicalStart) &&\r
-        (MemoryMapEntry->PhysicalStart + MemoryBlockLength < PhysicalStart + EfiPagesToSize (NumberOfPages))) {\r
-      return TRUE;\r
-    }\r
-\r
-    MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, mDescriptorSize);\r
-  }\r
-  return FALSE;\r
-}\r
-\r
 /**\r
   Merge continous memory map entries whose have same attributes.\r
 \r
@@ -271,8 +228,7 @@ MergeMemoryMap (
       if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
           (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&\r
           (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&\r
-          ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart) &&\r
-          (!DoesEntrySpanAcrossBoundary (MemoryMapEntry->PhysicalStart, MemoryMapEntry->NumberOfPages + NextMemoryMapEntry->NumberOfPages))) {\r
+          ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
         MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
         if (NewMemoryMapEntry != MemoryMapEntry) {\r
           NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
@@ -430,7 +386,11 @@ SetNewRecord (
       //\r
       // DATA\r
       //\r
-      NewRecord->Type = EfiRuntimeServicesData;\r
+      if (!mPropertiesTableEnable) {\r
+        NewRecord->Type = TempRecord.Type;\r
+      } else {\r
+        NewRecord->Type = EfiRuntimeServicesData;\r
+      }\r
       NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
       NewRecord->VirtualStart  = 0;\r
       NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentBase - NewRecord->PhysicalStart);\r
@@ -443,7 +403,11 @@ SetNewRecord (
       //\r
       // CODE\r
       //\r
-      NewRecord->Type = EfiRuntimeServicesCode;\r
+      if (!mPropertiesTableEnable) {\r
+        NewRecord->Type = TempRecord.Type;\r
+      } else {\r
+        NewRecord->Type = EfiRuntimeServicesCode;\r
+      }\r
       NewRecord->PhysicalStart = ImageRecordCodeSection->CodeSegmentBase;\r
       NewRecord->VirtualStart  = 0;\r
       NewRecord->NumberOfPages = EfiSizeToPages(ImageRecordCodeSection->CodeSegmentSize);\r
@@ -467,7 +431,11 @@ SetNewRecord (
   // Final DATA\r
   //\r
   if (TempRecord.PhysicalStart < ImageEnd) {\r
-    NewRecord->Type = EfiRuntimeServicesData;\r
+    if (!mPropertiesTableEnable) {\r
+      NewRecord->Type = TempRecord.Type;\r
+    } else {\r
+      NewRecord->Type = EfiRuntimeServicesData;\r
+    }\r
     NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
     NewRecord->VirtualStart  = 0;\r
     NewRecord->NumberOfPages = EfiSizeToPages (ImageEnd - TempRecord.PhysicalStart);\r
@@ -549,6 +517,7 @@ SplitRecord (
   UINT64                  PhysicalEnd;\r
   UINTN                   NewRecordCount;\r
   UINTN                   TotalNewRecordCount;\r
+  BOOLEAN                 IsLastRecordData;\r
 \r
   if (MaxSplitRecordCount == 0) {\r
     CopyMem (NewRecord, OldRecord, DescriptorSize);\r
@@ -576,7 +545,17 @@ SplitRecord (
         // If this is still address in this record, need record.\r
         //\r
         NewRecord = PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);\r
-        if (NewRecord->Type == EfiRuntimeServicesData) {\r
+        IsLastRecordData = FALSE;\r
+        if (!mPropertiesTableEnable) {\r
+          if ((NewRecord->Attribute & EFI_MEMORY_XP) != 0) {\r
+            IsLastRecordData = TRUE;\r
+          }\r
+        } else {\r
+          if (NewRecord->Type == EfiRuntimeServicesData) {\r
+            IsLastRecordData = TRUE;\r
+          }\r
+        }\r
+        if (IsLastRecordData) {\r
           //\r
           // Last record is DATA, just merge it.\r
           //\r
@@ -586,7 +565,11 @@ SplitRecord (
           // Last record is CODE, create a new DATA entry.\r
           //\r
           NewRecord = NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize);\r
-          NewRecord->Type = EfiRuntimeServicesData;\r
+          if (!mPropertiesTableEnable) {\r
+            NewRecord->Type = TempRecord.Type;\r
+          } else {\r
+            NewRecord->Type = EfiRuntimeServicesData;\r
+          }\r
           NewRecord->PhysicalStart = TempRecord.PhysicalStart;\r
           NewRecord->VirtualStart  = 0;\r
           NewRecord->NumberOfPages = TempRecord.NumberOfPages;\r
@@ -814,38 +797,13 @@ CoreGetMemoryMapPropertiesTable (
       //\r
       Status = EFI_BUFFER_TOO_SMALL;\r
     } else {\r
-      if (mIsConstructingMemoryAttributesTable) {\r
-        //\r
-        // If the memory map is constructed for memory attributes table,\r
-        // save original memory map, because they will be checked later\r
-        // to make sure the memory attributes table entry does not cross\r
-        // the original memory map entry boundary.\r
-        // This work must NOT be done in normal GetMemoryMap() because\r
-        // allocating memory is not allowed due to MapKey update.\r
-        //\r
-        mDescriptorSize = *DescriptorSize;\r
-        mMemoryMapOrgSize = *MemoryMapSize;\r
-        mMemoryMapOrg = AllocateCopyPool (*MemoryMapSize, MemoryMap);\r
-        if (mMemoryMapOrg == NULL) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          goto Exit;\r
-        }\r
-      }\r
-\r
       //\r
       // Split PE code/data\r
       //\r
       SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize);\r
-\r
-      if (mIsConstructingMemoryAttributesTable) {\r
-        FreePool (mMemoryMapOrg);\r
-        mMemoryMapOrg = NULL;\r
-        mMemoryMapOrgSize = 0;\r
-      }\r
     }\r
   }\r
 \r
-Exit:\r
   CoreReleasePropertiesTableLock ();\r
   return Status;\r
 }\r
@@ -1412,6 +1370,8 @@ InstallPropertiesTable (
     DEBUG ((EFI_D_VERBOSE, "Total Image Count - 0x%x\n", mImagePropertiesPrivateData.ImageRecordCount));\r
     DEBUG ((EFI_D_VERBOSE, "Dump ImageRecord:\n"));\r
     DumpImageRecord ();\r
+\r
+    mPropertiesTableEnable = TRUE;\r
   }\r
 }\r
 \r