]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/PropertiesTable.c
MdeModulePkg/DxeCore: switch to MdePkg allocation granularity macros
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / PropertiesTable.c
index 01ecca64ab0a4a698fa919882c19d482d96b820a..e7c4a9571250dd264e808d8b86ad1d38cabb1cb4 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 - 2017, 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
@@ -36,26 +36,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r
   ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))\r
 \r
-#define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I','P','R','C')\r
-\r
-typedef struct {\r
-  UINT32                 Signature;\r
-  LIST_ENTRY             Link;\r
-  EFI_PHYSICAL_ADDRESS   CodeSegmentBase;\r
-  UINT64                 CodeSegmentSize;\r
-} IMAGE_PROPERTIES_RECORD_CODE_SECTION;\r
-\r
-#define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D')\r
-\r
-typedef struct {\r
-  UINT32                 Signature;\r
-  LIST_ENTRY             Link;\r
-  EFI_PHYSICAL_ADDRESS   ImageBase;\r
-  UINT64                 ImageSize;\r
-  UINTN                  CodeSegmentCount;\r
-  LIST_ENTRY             CodeSegmentList;\r
-} IMAGE_PROPERTIES_RECORD;\r
-\r
 #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P','D')\r
 \r
 typedef struct {\r
@@ -80,6 +60,8 @@ EFI_PROPERTIES_TABLE  mPropertiesTable = {
 \r
 EFI_LOCK           mPropertiesTableLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
 \r
+BOOLEAN            mPropertiesTableEnable;\r
+\r
 //\r
 // Below functions are for MemoryMap\r
 //\r
@@ -221,14 +203,24 @@ MergeMemoryMap (
     CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
     NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
 \r
-    MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
-    if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
-        (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&\r
-        (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&\r
-        ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
-      NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
-      MemoryMapEntry = NextMemoryMapEntry;\r
-    }\r
+    do {\r
+      MemoryBlockLength = (UINT64) (EfiPagesToSize (MemoryMapEntry->NumberOfPages));\r
+      if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
+          (MemoryMapEntry->Type == NextMemoryMapEntry->Type) &&\r
+          (MemoryMapEntry->Attribute == NextMemoryMapEntry->Attribute) &&\r
+          ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
+        MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
+        if (NewMemoryMapEntry != MemoryMapEntry) {\r
+          NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
+        }\r
+\r
+        NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
+        continue;\r
+      } else {\r
+        MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
+        break;\r
+      }\r
+    } while (TRUE);\r
 \r
     MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
     NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);\r
@@ -374,7 +366,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
@@ -387,7 +383,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
@@ -411,7 +411,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
@@ -493,6 +497,7 @@ SplitRecord (
   UINT64                  PhysicalEnd;\r
   UINTN                   NewRecordCount;\r
   UINTN                   TotalNewRecordCount;\r
+  BOOLEAN                 IsLastRecordData;\r
 \r
   if (MaxSplitRecordCount == 0) {\r
     CopyMem (NewRecord, OldRecord, DescriptorSize);\r
@@ -520,7 +525,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
@@ -530,7 +545,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
@@ -686,7 +705,7 @@ SplitTable (
 }\r
 \r
 /**\r
-  This function for GetMemoryMap() with properties table.\r
+  This function for GetMemoryMap() with properties table capability.\r
 \r
   It calls original GetMemoryMap() to get the original memory map information. Then\r
   plus the additional memory map entries for PE Code/Data seperation.\r
@@ -717,10 +736,9 @@ SplitTable (
   @retval EFI_INVALID_PARAMETER  One of the parameters has an invalid value.\r
 \r
 **/\r
-STATIC\r
 EFI_STATUS\r
 EFIAPI\r
-CoreGetMemoryMapPropertiesTable (\r
+CoreGetMemoryMapWithSeparatedImageSection (\r
   IN OUT UINTN                  *MemoryMapSize,\r
   IN OUT EFI_MEMORY_DESCRIPTOR  *MemoryMap,\r
   OUT UINTN                     *MapKey,\r
@@ -752,6 +770,7 @@ CoreGetMemoryMapPropertiesTable (
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;\r
   } else if (Status == EFI_SUCCESS) {\r
+    ASSERT (MemoryMap != NULL);\r
     if (OldMemoryMapSize - *MemoryMapSize < (*DescriptorSize) * AdditionalRecordCount) {\r
       *MemoryMapSize = *MemoryMapSize + (*DescriptorSize) * AdditionalRecordCount;\r
       //\r
@@ -775,7 +794,7 @@ CoreGetMemoryMapPropertiesTable (
 //\r
 \r
 /**\r
-  Set PropertiesTable accroding to PE/COFF image section alignment.\r
+  Set PropertiesTable according to PE/COFF image section alignment.\r
 \r
   @param  SectionAlignment    PE/COFF section alignment\r
 **/\r
@@ -785,10 +804,10 @@ SetPropertiesTableSectionAlignment (
   IN UINT32  SectionAlignment\r
   )\r
 {\r
-  if (((SectionAlignment & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) &&\r
+  if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) &&\r
       ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) != 0)) {\r
     DEBUG ((EFI_D_VERBOSE, "SetPropertiesTableSectionAlignment - Clear\n"));\r
-    mPropertiesTable.MemoryProtectionAttribute &= ~EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA;\r
+    mPropertiesTable.MemoryProtectionAttribute &= ~((UINT64)EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);\r
     gBS->GetMemoryMap = CoreGetMemoryMap;\r
     gBS->Hdr.CRC32 = 0;\r
     gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);\r
@@ -825,7 +844,6 @@ SwapImageRecordCodeSection (
 \r
   @param  ImageRecord    image record to be sorted\r
 **/\r
-STATIC\r
 VOID\r
 SortImageRecordCodeSection (\r
   IN IMAGE_PROPERTIES_RECORD              *ImageRecord\r
@@ -876,7 +894,6 @@ SortImageRecordCodeSection (
   @retval TRUE  image record is valid\r
   @retval FALSE image record is invalid\r
 **/\r
-STATIC\r
 BOOLEAN\r
 IsImageRecordCodeSectionValid (\r
   IN IMAGE_PROPERTIES_RECORD              *ImageRecord\r
@@ -1119,12 +1136,12 @@ InsertImageRecord (
   }\r
 \r
   SetPropertiesTableSectionAlignment (SectionAlignment);\r
-  if ((SectionAlignment & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
-    DEBUG ((EFI_D_ERROR, "!!!!!!!!  InsertImageRecord - Section Alignment(0x%x) is not %dK  !!!!!!!!\n",\r
-      SectionAlignment, EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT >> 10));\r
+  if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
+    DEBUG ((EFI_D_WARN, "!!!!!!!!  InsertImageRecord - Section Alignment(0x%x) is not %dK  !!!!!!!!\n",\r
+      SectionAlignment, RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));\r
     PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageAddress);\r
     if (PdbPointer != NULL) {\r
-      DEBUG ((EFI_D_ERROR, "!!!!!!!!  Image - %a  !!!!!!!!\n", PdbPointer));\r
+      DEBUG ((EFI_D_WARN, "!!!!!!!!  Image - %a  !!!!!!!!\n", PdbPointer));\r
     }\r
     goto Finish;\r
   }\r
@@ -1219,7 +1236,7 @@ Finish:
 }\r
 \r
 /**\r
-  Find image record accroding to image base and size.\r
+  Find image record according to image base and size.\r
 \r
   @param  ImageBase    Base of PE image\r
   @param  ImageSize    Size of PE image\r
@@ -1320,16 +1337,20 @@ InstallPropertiesTable (
 \r
     DEBUG ((EFI_D_INFO, "MemoryProtectionAttribute - 0x%016lx\n", mPropertiesTable.MemoryProtectionAttribute));\r
     if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
+      DEBUG ((EFI_D_ERROR, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));\r
+      DEBUG ((EFI_D_ERROR, "because Runtime Driver Section Alignment is not %dK.\n", RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10));\r
       return ;\r
     }\r
 \r
-    gBS->GetMemoryMap = CoreGetMemoryMapPropertiesTable;\r
+    gBS->GetMemoryMap = CoreGetMemoryMapWithSeparatedImageSection;\r
     gBS->Hdr.CRC32 = 0;\r
     gBS->CalculateCrc32 ((UINT8 *)gBS, gBS->Hdr.HeaderSize, &gBS->Hdr.CRC32);\r
 \r
     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