]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg DxeCore: Enhance MemoryAttributesTable installation
authorStar Zeng <star.zeng@intel.com>
Wed, 20 Apr 2016 09:27:40 +0000 (17:27 +0800)
committerStar Zeng <star.zeng@intel.com>
Fri, 22 Apr 2016 05:39:56 +0000 (13:39 +0800)
Current MemoryAttributesTable will be installed on ReadyToBoot event
at TPL_NOTIFY level, it maybe incorrect when PcdHiiOsRuntimeSupport
= TRUE as HiiDatabaseDxe will have runtime memory allocation for HII
OS runtime support on and after ReadyToBoot. The issue was exposed at
http://article.gmane.org/gmane.comp.bios.edk2.devel/10125.

To make sure the correctness of MemoryAttributesTable, this patch is
to enhance MemoryAttributesTable installation to install
MemoryAttributesTable on ReadyToBoot event at TPL_CALLBACK - 1 level
to make sure it is at the last of ReadyToBoot event, and also hook
runtime memory allocation after ReadyToBoot.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Feng Tian <feng.tian@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
MdeModulePkg/Core/Dxe/DxeMain.h
MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c
MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c

index edb7ff5b9fa53c91d0bae017fca73426e8d59d6a..82a3a9b4f2b776219cfa8fb1c3f8348f81490a9f 100644 (file)
@@ -276,6 +276,8 @@ extern EFI_RUNTIME_SERVICES                     *gDxeCoreRT;
 extern EFI_DXE_SERVICES                         *gDxeCoreDS;\r
 extern EFI_HANDLE                               gDxeCoreImageHandle;\r
 \r
+extern BOOLEAN                                  gMemoryMapTerminated;\r
+\r
 extern EFI_DECOMPRESS_PROTOCOL                  gEfiDecompress;\r
 \r
 extern EFI_RUNTIME_ARCH_PROTOCOL                *gRuntime;\r
@@ -2858,6 +2860,16 @@ CoreInitializeMemoryAttributesTable (
   VOID\r
   );\r
 \r
+/**\r
+  Install MemoryAttributesTable on memory allocation.\r
+\r
+  @param[in] MemoryType EFI memory type.\r
+**/\r
+VOID\r
+InstallMemoryAttributesTableOnMemoryAllocation (\r
+  IN EFI_MEMORY_TYPE    MemoryType\r
+  );\r
+\r
 /**\r
   Insert image record.\r
 \r
index a458d6b64fee8415c33799d3d545e3e58e73b8c4..70a283bc2ba23904c6fdea69ee2209f23f15efb4 100644 (file)
@@ -207,6 +207,7 @@ EFI_SYSTEM_TABLE      *gDxeCoreST = NULL;
 EFI_RUNTIME_SERVICES  *gDxeCoreRT = &mEfiRuntimeServicesTableTemplate;\r
 EFI_HANDLE            gDxeCoreImageHandle = NULL;\r
 \r
+BOOLEAN               gMemoryMapTerminated = FALSE;\r
 \r
 //\r
 // EFI Decompress Protocol\r
@@ -751,6 +752,8 @@ CoreExitBootServices (
     return Status;\r
   }\r
 \r
+  gMemoryMapTerminated = TRUE;\r
+\r
   //\r
   // Notify other drivers that we are exiting boot services.\r
   //\r
index fa71bd87dfdc6313dcb73f828460456d99dd5d61..dbdc71b59ca5bd1da4468d27424125c743dd41b8 100644 (file)
@@ -1336,6 +1336,7 @@ CoreAllocatePages (
   Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePages, MemoryType, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) *Memory);\r
+    InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
   }\r
   return Status;\r
 }\r
@@ -1444,6 +1445,7 @@ CoreFreePages (
   Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePages, (EFI_MEMORY_TYPE) 0, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) Memory);\r
+    InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
   }\r
   return Status;\r
 }\r
index 5496a09cfa72ab9d004efe8e2c665231e7846f8f..5eced88efb7572f620862783d23df74863806cfb 100644 (file)
@@ -277,6 +277,7 @@ CoreAllocatePool (
   Status = CoreInternalAllocatePool (PoolType, Size, Buffer);\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
@@ -505,6 +506,7 @@ CoreFreePool (
   Status = CoreInternalFreePool (Buffer, &PoolType);\r
   if (!EFI_ERROR (Status)) {\r
     CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer);\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
index 0362212c8b0b2c45aa9b6ef69b8c8addea9772be..3706b232472a9b60f8820177214b65837f7486ab 100644 (file)
@@ -71,18 +71,16 @@ CoreGetMemoryMapPropertiesTable (
   );\r
 \r
 extern EFI_PROPERTIES_TABLE  mPropertiesTable;\r
+EFI_MEMORY_ATTRIBUTES_TABLE  *mMemoryAttributesTable = NULL;\r
+BOOLEAN                      mMemoryAttributesTableReadyToBoot = FALSE;\r
 \r
 /**\r
   Install MemoryAttributesTable.\r
 \r
-  @param[in]  Event     The Event this notify function registered to.\r
-  @param[in]  Context   Pointer to the context data registered to the Event.\r
 **/\r
 VOID\r
-EFIAPI\r
 InstallMemoryAttributesTable (\r
-  EFI_EVENT                               Event,\r
-  VOID                                    *Context\r
+  VOID\r
   )\r
 {\r
   UINTN                          MemoryMapSize;\r
@@ -97,12 +95,29 @@ InstallMemoryAttributesTable (
   EFI_MEMORY_ATTRIBUTES_TABLE    *MemoryAttributesTable;\r
   EFI_MEMORY_DESCRIPTOR          *MemoryAttributesEntry;\r
 \r
+  if (gMemoryMapTerminated) {\r
+    //\r
+    // Directly return after MemoryMap terminated.\r
+    //\r
+    return;\r
+  }\r
+\r
   if ((mPropertiesTable.MemoryProtectionAttribute & EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) == 0) {\r
     DEBUG ((EFI_D_VERBOSE, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "));\r
     DEBUG ((EFI_D_VERBOSE, "because Runtime Driver Section Alignment is not %dK.\n", EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT >> 10));\r
     return ;\r
   }\r
 \r
+  if (mMemoryAttributesTable == NULL) {\r
+    //\r
+    // InstallConfigurationTable here to occupy one entry for MemoryAttributesTable\r
+    // before GetMemoryMap below, as InstallConfigurationTable may allocate runtime\r
+    // memory for the new entry.\r
+    //\r
+    Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID *) (UINTN) MAX_ADDRESS);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
   MemoryMapSize = 0;\r
   MemoryMap = NULL;\r
   Status = CoreGetMemoryMapPropertiesTable (\r
@@ -177,8 +192,52 @@ InstallMemoryAttributesTable (
   MemoryMap = MemoryMapStart;\r
   FreePool (MemoryMap);\r
 \r
+  //\r
+  // Update configuratoin table for MemoryAttributesTable.\r
+  //\r
   Status = gBS->InstallConfigurationTable (&gEfiMemoryAttributesTableGuid, MemoryAttributesTable);\r
   ASSERT_EFI_ERROR (Status);\r
+\r
+  if (mMemoryAttributesTable != NULL) {\r
+    FreePool (mMemoryAttributesTable);\r
+  }\r
+  mMemoryAttributesTable = MemoryAttributesTable; \r
+}\r
+\r
+/**\r
+  Install MemoryAttributesTable on memory allocation.\r
+\r
+  @param[in] MemoryType EFI memory type.\r
+**/\r
+VOID\r
+InstallMemoryAttributesTableOnMemoryAllocation (\r
+  IN EFI_MEMORY_TYPE    MemoryType\r
+  )\r
+{\r
+  //\r
+  // Install MemoryAttributesTable after ReadyToBoot on runtime memory allocation.\r
+  //\r
+  if (mMemoryAttributesTableReadyToBoot &&\r
+      ((MemoryType == EfiRuntimeServicesCode) || (MemoryType == EfiRuntimeServicesData))) {\r
+    InstallMemoryAttributesTable ();\r
+  }\r
+}\r
+\r
+/**\r
+  Install MemoryAttributesTable on ReadyToBoot.\r
+\r
+  @param[in] Event      The Event this notify function registered to.\r
+  @param[in] Context    Pointer to the context data registered to the Event.\r
+**/\r
+VOID\r
+EFIAPI\r
+InstallMemoryAttributesTableOnReadyToBoot (\r
+  IN EFI_EVENT          Event,\r
+  IN VOID               *Context\r
+  )\r
+{\r
+  InstallMemoryAttributesTable ();\r
+  mMemoryAttributesTableReadyToBoot = TRUE; \r
 }\r
 \r
 /**\r
@@ -194,17 +253,16 @@ CoreInitializeMemoryAttributesTable (
   EFI_EVENT   ReadyToBootEvent;\r
 \r
   //\r
-  // Construct the table at ReadyToBoot, because this should be\r
-  // last point to allocate RuntimeCode/RuntimeData.\r
+  // Construct the table at ReadyToBoot.\r
   //\r
-  Status = gBS->CreateEventEx (\r
-                  EVT_NOTIFY_SIGNAL,\r
-                  TPL_NOTIFY,\r
-                  InstallMemoryAttributesTable,\r
-                  NULL,\r
-                  &gEfiEventReadyToBootGuid,\r
-                  &ReadyToBootEvent\r
-                  );\r
+  Status = CoreCreateEventInternal (\r
+             EVT_NOTIFY_SIGNAL,\r
+             TPL_CALLBACK - 1,\r
+             InstallMemoryAttributesTableOnReadyToBoot,\r
+             NULL,\r
+             &gEfiEventReadyToBootGuid,\r
+             &ReadyToBootEvent\r
+             );\r
   ASSERT_EFI_ERROR (Status);\r
   return ;\r
 }\r