]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Page.c
Add the beginning of a GDB based Debug Agent. IA-32 and X64 don't have low level...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
index 1f9e659753af2ceec5ff7ecf0bfc6321154338a9..b0cf1ece1e6545c50fcda28b67baead29c4164c7 100644 (file)
@@ -89,7 +89,12 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
   { EfiPalCode,                 0 },\r
   { EfiMaxMemoryType,           0 }\r
 };\r
-\r
+//\r
+// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
+// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a \r
+//  address assigned by DXE core.\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED   BOOLEAN       gLoadFixedAddressCodeMemoryReady = FALSE;\r
 \r
 /**\r
   Enter critical section by gaining lock on gMemoryLock.\r
@@ -419,7 +424,70 @@ PromoteMemoryResource (
 \r
   return;\r
 }\r
+/**\r
+  This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
+  PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
+  size of boot time and runtime code.\r
 \r
+**/\r
+VOID\r
+CoreLoadingFixedAddressHook (\r
+  VOID\r
+  )\r
+{\r
+   UINT32                     RuntimeCodePageNumber;\r
+   UINT32                     BootTimeCodePageNumber;\r
+   EFI_PHYSICAL_ADDRESS       RuntimeCodeBase;\r
+   EFI_PHYSICAL_ADDRESS       BootTimeCodeBase;\r
+   EFI_STATUS                 Status;\r
+\r
+   //\r
+   // Make sure these 2 areas are not initialzied.\r
+   //\r
+   if (!gLoadFixedAddressCodeMemoryReady) {   \r
+     RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
+     BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
+     RuntimeCodeBase       = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
+     BootTimeCodeBase      = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
+     //\r
+     // Try to allocate runtime memory.\r
+     //\r
+     Status = CoreAllocatePages (\r
+                       AllocateAddress,\r
+                       EfiRuntimeServicesCode,\r
+                       RuntimeCodePageNumber,\r
+                       &RuntimeCodeBase\r
+                       );\r
+     if (EFI_ERROR(Status)) {\r
+       //\r
+       // Runtime memory allocation failed \r
+       //\r
+       return;\r
+     }\r
+     //\r
+     // Try to allocate boot memory.\r
+     //\r
+     Status = CoreAllocatePages (\r
+                       AllocateAddress,\r
+                       EfiBootServicesCode,\r
+                       BootTimeCodePageNumber,\r
+                       &BootTimeCodeBase\r
+                       );\r
+     if (EFI_ERROR(Status)) {\r
+       //\r
+        // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
+        // new memory range is installed.\r
+        //\r
+        CoreFreePages (\r
+              RuntimeCodeBase,\r
+              RuntimeCodePageNumber\r
+              );\r
+       return;\r
+     }\r
+     gLoadFixedAddressCodeMemoryReady = TRUE;\r
+   } \r
+   return;\r
+}  \r
 \r
 /**\r
   Called to initialize the memory map and add descriptors to\r
@@ -448,7 +516,7 @@ CoreAddMemoryDescriptor (
   EFI_STATUS                  Status;\r
   UINTN                       Index;\r
   UINTN                       FreeIndex;\r
-\r
+  \r
   if ((Start & EFI_PAGE_MASK) != 0) {\r
     return;\r
   }\r
@@ -456,13 +524,19 @@ CoreAddMemoryDescriptor (
   if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
     return;\r
   }\r
-\r
   CoreAcquireMemoryLock ();\r
   End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
   CoreAddRange (Type, Start, End, Attribute);\r
   CoreFreeMemoryMapStack ();\r
   CoreReleaseMemoryLock ();\r
 \r
+  //\r
+  // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
+  //\r
+  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+    CoreLoadingFixedAddressHook();\r
+  }\r
+  \r
   //\r
   // Check to see if the statistics for the different memory types have already been established\r
   //\r
@@ -470,6 +544,7 @@ CoreAddMemoryDescriptor (
     return;\r
   }\r
 \r
+  \r
   //\r
   // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
   //\r
@@ -481,7 +556,6 @@ CoreAddMemoryDescriptor (
     if (Type < 0 || Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
-\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
       //\r
       // Allocate pages for the current memory type from the top of available memory\r
@@ -549,7 +623,6 @@ CoreAddMemoryDescriptor (
     if (Type < 0 || Type > EfiMaxMemoryType) {\r
       continue;\r
     }\r
-\r
     if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
       CoreFreePages (\r
         mMemoryTypeStatistics[Type].BaseAddress,\r
@@ -651,6 +724,8 @@ CoreConvertPages (
     // if that's all we've got\r
     //\r
     RangeEnd = End;\r
+\r
+    ASSERT (Entry != NULL);\r
     if (Entry->End < End) {\r
       RangeEnd = Entry->End;\r
     }\r
@@ -1093,12 +1168,13 @@ CoreFreePages (
     }\r
   }\r
   if (Link == &gMemoryMap) {\r
-    CoreReleaseMemoryLock ();\r
-    return EFI_NOT_FOUND;\r
+    Status = EFI_NOT_FOUND;\r
+    goto Done;\r
   }\r
 \r
   Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
 \r
+  ASSERT (Entry != NULL);\r
   if  (Entry->Type == EfiACPIReclaimMemory   ||\r
        Entry->Type == EfiACPIMemoryNVS       ||\r
        Entry->Type == EfiRuntimeServicesCode ||\r
@@ -1109,8 +1185,8 @@ CoreFreePages (
   }\r
 \r
   if ((Memory & (Alignment - 1)) != 0) {\r
-    CoreReleaseMemoryLock ();\r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto Done;\r
   }\r
 \r
   NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
@@ -1118,10 +1194,8 @@ CoreFreePages (
 \r
   Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
 \r
-  CoreReleaseMemoryLock ();\r
-\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -1131,6 +1205,8 @@ CoreFreePages (
     DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);\r
   }\r
 \r
+Done:\r
+  CoreReleaseMemoryLock ();\r
   return Status;\r
 }\r
 \r