]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Correct description for return status code of PEI service AllocatePages(), according...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
index 9c570834da9efa4df953d1e430cc62245f45b3ad..7e1bb55242cc9f1448c9b65ff6fa86f01077c065 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   EFI PEI Core memory services\r
   \r
-Copyright (c) 2006, Intel Corporation                                                         \r
+Copyright (c) 2006 - 2010, Intel Corporation                                                         \r
 All rights reserved. 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
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <PeiMain.h>\r
+#include "PeiMain.h"\r
 \r
 /**\r
 \r
@@ -52,9 +52,9 @@ InitializeMemoryServices (
       );\r
 \r
     //\r
-    // Set PS to point to ServiceTableShadow in Cache\r
+    // Set Ps to point to ServiceTableShadow in Cache\r
     //\r
-    PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
+    PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r
   }\r
   \r
   return;\r
@@ -89,6 +89,17 @@ PeiInstallPeiMemory (
   DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
 \r
+  //\r
+  // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r
+  // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r
+  // simply return EFI_SUCESS in release tip to ignore it.\r
+  // \r
+  if (PrivateData->PeiMemoryInstalled) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r
+    ASSERT (PrivateData->PeiMemoryInstalled);\r
+    return EFI_SUCCESS;\r
+  }\r
+  \r
   PrivateData->PhysicalMemoryBegin   = MemoryBegin;\r
   PrivateData->PhysicalMemoryLength  = MemoryLength;\r
   PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
@@ -128,6 +139,7 @@ PeiAllocatePages (
   EFI_PEI_HOB_POINTERS                    Hob;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;\r
+  UINTN                                   RemainingPages;\r
 \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   Hob.Raw     = PrivateData->HobList.Raw;\r
@@ -137,8 +149,8 @@ PeiAllocatePages (
   //\r
   if (!PrivateData->PeiMemoryInstalled) {\r
     //\r
-    // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
-    // the AllocatePage will dependent on the field of PEI_CORE_INSTANCE structure.\r
+    // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r
+    // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
     //\r
     if (!PrivateData->SwitchStackSignal) {\r
       return EFI_NOT_AVAILABLE_YET;\r
@@ -159,11 +171,14 @@ PeiAllocatePages (
   //\r
   // Verify that there is sufficient memory to satisfy the allocation\r
   //\r
-  if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
-      *(FreeMemoryBottom)) {\r
-    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r
-    DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r
-    EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r
+  RemainingPages = EFI_SIZE_TO_PAGES ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom));\r
+  //\r
+  // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page.\r
+  // So the number of remaining pages needs to be greater than that of the request pages.\r
+  //\r
+  if (RemainingPages <= Pages) {\r
+    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));\r
+    DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));\r
     return  EFI_OUT_OF_RESOURCES;\r
   } else {\r
     //\r
@@ -192,7 +207,7 @@ PeiAllocatePages (
 /**\r
 \r
   Pool allocation service. Before permenent memory is discoveried, the pool will \r
-  be allocated the heap in the CAR. Genenrally, the size of heap in temporary \r
+  be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r
   memory does not exceed to 64K, so the biggest pool size could be allocated is \r
   64K.\r
 \r
@@ -225,7 +240,10 @@ PeiAllocatePool (
   // Generally, the size of heap in temporary memory does not exceed to 64K,\r
   // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r
   //\r
-  ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
+  if (Size >= (0x10000 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
   Status = PeiServicesCreateHob (\r
              EFI_HOB_TYPE_MEMORY_POOL,\r
              (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r