]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Gcd/Gcd.c
MdeModulePkg/Gcd: Suppress incorrect compiler/analyzer warnings
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Gcd / Gcd.c
index a06f8bb77cb01666a48100dd3790c7aa5e0ec621..e17e98230b79beb063c5a00cbe38c251e86e9fc3 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
@@ -40,7 +41,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #define PRESENT_MEMORY_ATTRIBUTES     (EFI_RESOURCE_ATTRIBUTE_PRESENT)\r
 \r
-#define INVALID_CPU_ARCH_ATTRIBUTES   0xffffffff\r
+#define EXCLUSIVE_MEMORY_ATTRIBUTES   (EFI_MEMORY_UC | EFI_MEMORY_WC | \\r
+                                       EFI_MEMORY_WT | EFI_MEMORY_WB | \\r
+                                       EFI_MEMORY_WP | EFI_MEMORY_UCE)\r
+\r
+#define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \\r
+                                        EFI_MEMORY_RO)\r
 \r
 //\r
 // Module Variables\r
@@ -108,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
@@ -384,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
@@ -654,28 +669,25 @@ ConverToCpuArchAttributes (
   UINT64 Attributes\r
   )\r
 {\r
-  if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
-    return EFI_MEMORY_UC;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {\r
-    return EFI_MEMORY_WC;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {\r
-    return EFI_MEMORY_WT;\r
-  }\r
-\r
-  if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {\r
-    return EFI_MEMORY_WB;\r
-  }\r
+  UINT64      CpuArchAttributes;\r
 \r
-  if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {\r
-    return EFI_MEMORY_WP;\r
-  }\r
-\r
-  return INVALID_CPU_ARCH_ATTRIBUTES;\r
+  CpuArchAttributes = Attributes & NONEXCLUSIVE_MEMORY_ATTRIBUTES;\r
 \r
+  if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
+    CpuArchAttributes |= EFI_MEMORY_UC;\r
+  } else if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {\r
+    CpuArchAttributes |= EFI_MEMORY_WC;\r
+  } else if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {\r
+    CpuArchAttributes |= EFI_MEMORY_WT;\r
+  } else if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {\r
+    CpuArchAttributes |= EFI_MEMORY_WB;\r
+  } else if ( (Attributes & EFI_MEMORY_UCE) == EFI_MEMORY_UCE) {\r
+    CpuArchAttributes |= EFI_MEMORY_UCE;\r
+  } else if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {\r
+    CpuArchAttributes |= EFI_MEMORY_WP;\r
+  }\r
+\r
+  return CpuArchAttributes;\r
 }\r
 \r
 \r
@@ -854,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
@@ -922,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
@@ -2407,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