]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
index e9d1d5b612a6d94abd40a8b181805084b15b882c..d9c65a8045e2906ceaf6ae6d6e33b3fe51fbaf4a 100644 (file)
@@ -3,7 +3,7 @@
   The GCD services are used to manage the memory and I/O regions that\r
   are accessible to the CPU that is executing the DXE core.\r
 \r
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2018, 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
@@ -16,6 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "DxeMain.h"\r
 #include "Gcd.h"\r
+#include "Mem/HeapGuard.h"\r
 \r
 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 \r
@@ -47,8 +48,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \\r
                                         EFI_MEMORY_RO)\r
 \r
-#define INVALID_CPU_ARCH_ATTRIBUTES   0xffffffff\r
-\r
 //\r
 // Module Variables\r
 //\r
@@ -115,7 +114,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdMemoryTypeNames[] = {
   "Reserved ",  // EfiGcdMemoryTypeReserved\r
   "SystemMem",  // EfiGcdMemoryTypeSystemMemory\r
   "MMIO     ",  // EfiGcdMemoryTypeMemoryMappedIo\r
-  "PersisMem",  // EfiGcdMemoryTypePersistentMemory\r
+  "PersisMem",  // EfiGcdMemoryTypePersistent\r
   "MoreRelia",  // EfiGcdMemoryTypeMoreReliable\r
   "Unknown  "   // EfiGcdMemoryTypeMaximum\r
 };\r
@@ -127,7 +126,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdIoTypeNames[] = {
   "NonExist",  // EfiGcdIoTypeNonExistent\r
   "Reserved",  // EfiGcdIoTypeReserved\r
   "I/O     ",  // EfiGcdIoTypeIo\r
-  "Unknown "   // EfiGcdIoTypeMaximum \r
+  "Unknown "   // EfiGcdIoTypeMaximum\r
 };\r
 \r
 ///\r
@@ -147,7 +146,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdAllocationTypeNames[] = {
   PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.\r
 \r
   @param  InitialMap  TRUE if the initial GCD Memory Map is being dumped.  Otherwise, FALSE.\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -160,7 +159,7 @@ CoreDumpGcdMemorySpaceMap (
     UINTN                            NumberOfDescriptors;\r
     EFI_GCD_MEMORY_SPACE_DESCRIPTOR  *MemorySpaceMap;\r
     UINTN                            Index;\r
-   \r
+\r
     Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
     ASSERT (Status == EFI_SUCCESS && MemorySpaceMap != NULL);\r
 \r
@@ -170,11 +169,11 @@ CoreDumpGcdMemorySpaceMap (
     DEBUG ((DEBUG_GCD, "GCDMemType Range                             Capabilities     Attributes      \n"));\r
     DEBUG ((DEBUG_GCD, "========== ================================= ================ ================\n"));\r
     for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
-      DEBUG ((DEBUG_GCD, "%a  %016lx-%016lx %016lx %016lx%c\n", \r
+      DEBUG ((DEBUG_GCD, "%a  %016lx-%016lx %016lx %016lx%c\n",\r
         mGcdMemoryTypeNames[MIN (MemorySpaceMap[Index].GcdMemoryType, EfiGcdMemoryTypeMaximum)],\r
-        MemorySpaceMap[Index].BaseAddress, \r
+        MemorySpaceMap[Index].BaseAddress,\r
         MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - 1,\r
-        MemorySpaceMap[Index].Capabilities, \r
+        MemorySpaceMap[Index].Capabilities,\r
         MemorySpaceMap[Index].Attributes,\r
         MemorySpaceMap[Index].ImageHandle == NULL ? ' ' : '*'\r
         ));\r
@@ -185,11 +184,11 @@ CoreDumpGcdMemorySpaceMap (
 }\r
 \r
 /**\r
-  Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when \r
+  Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when\r
   PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.\r
 \r
   @param  InitialMap  TRUE if the initial GCD I/O Map is being dumped.  Otherwise, FALSE.\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -202,20 +201,20 @@ CoreDumpGcdIoSpaceMap (
     UINTN                        NumberOfDescriptors;\r
     EFI_GCD_IO_SPACE_DESCRIPTOR  *IoSpaceMap;\r
     UINTN                        Index;\r
-    \r
+\r
     Status = CoreGetIoSpaceMap (&NumberOfDescriptors, &IoSpaceMap);\r
     ASSERT (Status == EFI_SUCCESS && IoSpaceMap != NULL);\r
-    \r
+\r
     if (InitialMap) {\r
       DEBUG ((DEBUG_GCD, "GCD:Initial GCD I/O Space Map\n"));\r
-    }  \r
-    \r
+    }\r
+\r
     DEBUG ((DEBUG_GCD, "GCDIoType  Range                            \n"));\r
     DEBUG ((DEBUG_GCD, "========== =================================\n"));\r
     for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
-      DEBUG ((DEBUG_GCD, "%a   %016lx-%016lx%c\n", \r
+      DEBUG ((DEBUG_GCD, "%a   %016lx-%016lx%c\n",\r
         mGcdIoTypeNames[MIN (IoSpaceMap[Index].GcdIoType, EfiGcdIoTypeMaximum)],\r
-        IoSpaceMap[Index].BaseAddress, \r
+        IoSpaceMap[Index].BaseAddress,\r
         IoSpaceMap[Index].BaseAddress + IoSpaceMap[Index].Length - 1,\r
         IoSpaceMap[Index].ImageHandle == NULL ? ' ' : '*'\r
         ));\r
@@ -224,11 +223,11 @@ CoreDumpGcdIoSpaceMap (
     FreePool (IoSpaceMap);\r
   );\r
 }\r
-  \r
+\r
 /**\r
   Validate resource descriptor HOB's attributes.\r
 \r
-  If Attributes includes some memory resource's settings, it should include \r
+  If Attributes includes some memory resource's settings, it should include\r
   the corresponding capabilites also.\r
 \r
   @param  Attributes  Resource descriptor HOB attributes.\r
@@ -391,12 +390,21 @@ CoreAllocateGcdMapEntry (
   IN OUT EFI_GCD_MAP_ENTRY  **BottomEntry\r
   )\r
 {\r
+  //\r
+  // Set to mOnGuarding to TRUE before memory allocation. This will make sure\r
+  // that the entry memory is not "guarded" by HeapGuard. Otherwise it might\r
+  // cause problem when it's freed (if HeapGuard is enabled).\r
+  //\r
+  mOnGuarding = TRUE;\r
   *TopEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+  mOnGuarding = FALSE;\r
   if (*TopEntry == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
+  mOnGuarding = TRUE;\r
   *BottomEntry = AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+  mOnGuarding = FALSE;\r
   if (*BottomEntry == NULL) {\r
     CoreFreePool (*TopEntry);\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -663,11 +671,6 @@ ConverToCpuArchAttributes (
 {\r
   UINT64      CpuArchAttributes;\r
 \r
-  if ((Attributes & ~(EXCLUSIVE_MEMORY_ATTRIBUTES |\r
-                      NONEXCLUSIVE_MEMORY_ATTRIBUTES)) != 0) {\r
-    return INVALID_CPU_ARCH_ATTRIBUTES;\r
-  }\r
-\r
   CpuArchAttributes = Attributes & NONEXCLUSIVE_MEMORY_ATTRIBUTES;\r
 \r
   if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
@@ -863,12 +866,30 @@ CoreConvertSpace (
   }\r
   ASSERT (TopEntry != NULL && BottomEntry != NULL);\r
 \r
+  //\r
+  // Initialize CpuArchAttributes to suppress incorrect compiler/analyzer warnings.\r
+  //\r
+  CpuArchAttributes = 0;\r
   if (Operation == GCD_SET_ATTRIBUTES_MEMORY_OPERATION) {\r
     //\r
     // Call CPU Arch Protocol to attempt to set attributes on the range\r
     //\r
     CpuArchAttributes = ConverToCpuArchAttributes (Attributes);\r
-    if (CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES) {\r
+    //\r
+    // CPU arch attributes include page attributes and cache attributes.\r
+    // Only page attributes supports to be cleared, but not cache attributes.\r
+    // Caller is expected to use GetMemorySpaceDescriptor() to get the current\r
+    // attributes, AND/OR attributes, and then calls SetMemorySpaceAttributes()\r
+    // to set the new attributes.\r
+    // So 0 CPU arch attributes should not happen as memory should always have\r
+    // a cache attribute (no matter UC or WB, etc).\r
+    //\r
+    // Here, 0 CPU arch attributes will be filtered to be compatible with the\r
+    // case that caller just calls SetMemorySpaceAttributes() with none CPU\r
+    // arch attributes (for example, RUNTIME) as the purpose of the case is not\r
+    // to clear CPU arch attributes.\r
+    //\r
+    if (CpuArchAttributes != 0) {\r
       if (gCpu == NULL) {\r
         Status = EFI_NOT_AVAILABLE_YET;\r
       } else {\r
@@ -931,6 +952,13 @@ CoreConvertSpace (
     // Set attributes operation\r
     //\r
     case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\r
+      if (CpuArchAttributes == 0) {\r
+        //\r
+        // Keep original CPU arch attributes when caller just calls\r
+        // SetMemorySpaceAttributes() with none CPU arch attributes (for example, RUNTIME).\r
+        //\r
+        Attributes |= (Entry->Attributes & (EXCLUSIVE_MEMORY_ATTRIBUTES | NONEXCLUSIVE_MEMORY_ATTRIBUTES));\r
+      }\r
       Entry->Attributes = Attributes;\r
       break;\r
     //\r
@@ -1265,7 +1293,7 @@ Done:
     DEBUG ((DEBUG_GCD, "  (BaseAddress = %016lx)", *BaseAddress));\r
   }\r
   DEBUG ((DEBUG_GCD, "\n"));\r
-  \r
+\r
   if ((Operation & GCD_MEMORY_SPACE_OPERATION) != 0) {\r
     CoreReleaseGcdMemoryLock ();\r
     CoreDumpGcdMemorySpaceMap (FALSE);\r
@@ -1356,7 +1384,7 @@ CoreAllocateMemorySpace (
   DEBUG ((DEBUG_GCD, "  Alignment       = %016lx\n", LShiftU64 (1, Alignment)));\r
   DEBUG ((DEBUG_GCD, "  ImageHandle     = %p\n", ImageHandle));\r
   DEBUG ((DEBUG_GCD, "  DeviceHandle    = %p\n", DeviceHandle));\r
-  \r
+\r
   return CoreAllocateSpace (\r
            GCD_ALLOCATE_MEMORY_OPERATION,\r
            GcdAllocateType,\r
@@ -1488,7 +1516,7 @@ CoreRemoveMemorySpace (
   )\r
 {\r
   DEBUG ((DEBUG_GCD, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
-  \r
+\r
   return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -1577,7 +1605,7 @@ CoreGetMemorySpaceDescriptor (
   @param  Attributes             Specified attributes\r
 \r
   @retval EFI_SUCCESS           The attributes were set for the memory region.\r
-  @retval EFI_INVALID_PARAMETER Length is zero. \r
+  @retval EFI_INVALID_PARAMETER Length is zero.\r
   @retval EFI_UNSUPPORTED       The processor does not support one or more bytes of the memory\r
                                 resource range specified by BaseAddress and Length.\r
   @retval EFI_UNSUPPORTED       The bit mask of attributes is not support for the memory resource\r
@@ -1734,7 +1762,7 @@ CoreAddIoSpace (
 {\r
   DEBUG ((DEBUG_GCD, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
   DEBUG ((DEBUG_GCD, "  GcdIoType    = %a\n", mGcdIoTypeNames[MIN (GcdIoType, EfiGcdIoTypeMaximum)]));\r
-  \r
+\r
   //\r
   // Make sure parameters are valid\r
   //\r
@@ -1784,7 +1812,7 @@ CoreAllocateIoSpace (
   DEBUG ((DEBUG_GCD, "  Alignment       = %016lx\n", LShiftU64 (1, Alignment)));\r
   DEBUG ((DEBUG_GCD, "  ImageHandle     = %p\n", ImageHandle));\r
   DEBUG ((DEBUG_GCD, "  DeviceHandle    = %p\n", DeviceHandle));\r
-  \r
+\r
   return CoreAllocateSpace (\r
            GCD_ALLOCATE_IO_OPERATION,\r
            GcdAllocateType,\r
@@ -1840,7 +1868,7 @@ CoreRemoveIoSpace (
   )\r
 {\r
   DEBUG ((DEBUG_GCD, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress, Length));\r
-  \r
+\r
   return CoreConvertSpace (GCD_REMOVE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
 }\r
 \r
@@ -2110,15 +2138,15 @@ CoreInitializeMemoryServices (
   // Cache the PHIT HOB for later use\r
   //\r
   PhitHob = Hob.HandoffInformationTable;\r
-  \r
+\r
   if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
-       ReservedCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
-       ReservedCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
-   \r
-       //\r
-       // cache the Top address for loading modules at Fixed Address \r
-       //\r
-    gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress = PhitHob->EfiMemoryTop \r
+    ReservedCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
+    ReservedCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
+\r
+    //\r
+    // cache the Top address for loading modules at Fixed Address\r
+    //\r
+    gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress = PhitHob->EfiMemoryTop\r
                                                                    + EFI_PAGES_TO_SIZE(ReservedCodePageNumber);\r
   }\r
   //\r
@@ -2185,14 +2213,14 @@ CoreInitializeMemoryServices (
     Length      = PageAlignLength  (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
     if (Length < MinimalMemorySizeNeeded) {\r
       //\r
-      // If that range is not large enough to intialize the DXE Core, then \r
+      // If that range is not large enough to intialize the DXE Core, then\r
       // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop\r
       //\r
       BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
       Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
       if (Length < MinimalMemorySizeNeeded) {\r
         //\r
-        // If that range is not large enough to intialize the DXE Core, then \r
+        // If that range is not large enough to intialize the DXE Core, then\r
         // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List\r
         //\r
         BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
@@ -2269,7 +2297,7 @@ CoreInitializeMemoryServices (
       //\r
       BaseAddress = TestedMemoryBaseAddress;\r
       Length      = TestedMemoryLength;\r
-      Attributes  = ResourceHob->ResourceAttribute; \r
+      Attributes  = ResourceHob->ResourceAttribute;\r
       HighAddress = ResourceHob->PhysicalStart;\r
     }\r
   }\r
@@ -2375,7 +2403,7 @@ CoreInitializeGcdServices (
   InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);\r
 \r
   CoreDumpGcdMemorySpaceMap (TRUE);\r
-  \r
+\r
   //\r
   // Initialize the GCD I/O Space Map\r
   //\r
@@ -2387,7 +2415,7 @@ CoreInitializeGcdServices (
   InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);\r
 \r
   CoreDumpGcdIoSpaceMap (TRUE);\r
-  \r
+\r
   //\r
   // Walk the HOB list and add all resource descriptors to the GCD\r
   //\r
@@ -2416,7 +2444,7 @@ CoreInitializeGcdServices (
           GcdMemoryType = EfiGcdMemoryTypeReserved;\r
         }\r
         if ((ResourceHob->ResourceAttribute & EFI_RESOURCE_ATTRIBUTE_PERSISTENT) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT) {\r
-          GcdMemoryType = EfiGcdMemoryTypePersistentMemory;\r
+          GcdMemoryType = EfiGcdMemoryTypePersistent;\r
         }\r
         break;\r
       case EFI_RESOURCE_MEMORY_MAPPED_IO:\r