]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Temporarily rollback the change from 5625 though 5628 for more investigation.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
index 3be63fb105fda195d4be5264dea3d8e0ab29f92a..d24897d7767b505022f074df42f25342b67881b3 100644 (file)
@@ -1,5 +1,6 @@
-/*++\r
-\r
+/** @file\r
+  EFI PEI Core memory services\r
+  \r
 Copyright (c) 2006, 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
@@ -9,55 +10,46 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
-Module Name:\r
+**/\r
 \r
-  MemoryServices.c\r
+#include <PeiMain.h>\r
 \r
-Abstract:\r
+/**\r
 \r
-  EFI PEI Core memory services\r
+  Initialize the memory services.\r
 \r
---*/\r
 \r
-#include <PeiMain.h>\r
+  @param PrivateData     Add parameter description\r
+  @param SecCoreData     Points to a data structure containing information about the PEI core's operating\r
+                         environment, such as the size and location of temporary RAM, the stack location and\r
+                         the BFV location.\r
+  @param OldCoreData     Pointer to the PEI Core data.\r
+                         NULL if being run in non-permament memory mode.\r
 \r
+**/\r
 VOID\r
 InitializeMemoryServices (\r
   IN PEI_CORE_INSTANCE           *PrivateData,\r
   IN CONST EFI_SEC_PEI_HAND_OFF  *SecCoreData,\r
   IN PEI_CORE_INSTANCE           *OldCoreData\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Initialize the memory services.\r
-\r
-Arguments:\r
-\r
-  PeiServices          - The PEI core services table.\r
-  SecCoreData          - Points to a data structure containing information about the PEI core's operating\r
-                         environment, such as the size and location of temporary RAM, the stack location and\r
-                         the BFV location.\r
-\r
-  OldCoreData          - Pointer to the PEI Core data.\r
-                         NULL if being run in non-permament memory mode.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
-  PrivateData->SwitchStackSignal = FALSE;\r
+  \r
+  PrivateData->SwitchStackSignal      = FALSE;\r
 \r
+  //\r
+  // First entering PeiCore, following code will initialized some field\r
+  // in PeiCore's private data according to hand off data from sec core.\r
+  //\r
   if (OldCoreData == NULL) {\r
 \r
     PrivateData->PeiMemoryInstalled = FALSE;\r
 \r
-    PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase; \r
-    PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
\r
+    PrivateData->BottomOfCarHeap        = SecCoreData->PeiTemporaryRamBase; \r
+    PrivateData->TopOfCarHeap           = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
+    PrivateData->SizeOfTemporaryMemory  = SecCoreData->TemporaryRamSize;\r
+    PrivateData->StackSize              = (UINT64) SecCoreData->StackSize;\r
+    \r
     DEBUG_CODE_BEGIN ();\r
       PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r
       PrivateData->MaxTopOfCarHeap  = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r
@@ -82,103 +74,58 @@ Returns:
   return;\r
 }\r
 \r
-EFI_STATUS\r
-EFIAPI\r
-PeiInstallPeiMemory (\r
-  IN CONST EFI_PEI_SERVICES      **PeiServices,\r
-  IN EFI_PHYSICAL_ADDRESS  MemoryBegin,\r
-  IN UINT64                MemoryLength\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
 \r
   Install the permanent memory is now available.\r
   Creates HOB (PHIT and Stack).\r
 \r
-Arguments:\r
-\r
-  PeiServices   - The PEI core services table.\r
-  MemoryBegin   - Start of memory address.\r
-  MemoryLength  - Length of memory.\r
+  @param PeiServices        An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param MemoryBegin        Start of memory address.\r
+  @param MemoryLength       Length of memory.\r
 \r
-Returns:\r
+  @return EFI_SUCCESS Always success.\r
 \r
-  Status  - EFI_SUCCESS\r
-            \r
---*/\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiInstallPeiMemory (\r
+  IN CONST EFI_PEI_SERVICES  **PeiServices,\r
+  IN EFI_PHYSICAL_ADDRESS    MemoryBegin,\r
+  IN UINT64                  MemoryLength\r
+  )\r
 {\r
   PEI_CORE_INSTANCE                     *PrivateData;\r
-  EFI_HOB_HANDOFF_INFO_TABLE            *OldHandOffHob;\r
-  EFI_HOB_HANDOFF_INFO_TABLE            *NewHandOffHob;\r
-  UINT64                                PeiStackSize;\r
-  UINT64                                EfiFreeMemorySize;\r
-  EFI_PHYSICAL_ADDRESS                  PhysicalAddressOfOldHob;\r
-\r
-  if (MemoryLength > (MAX_ADDRESS - MemoryBegin + 1))\r
-    return EFI_INVALID_PARAMETER;\r
-    \r
-   \r
+\r
   DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
-  \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
 \r
-  PrivateData->SwitchStackSignal = TRUE;\r
-  PrivateData->PeiMemoryInstalled = TRUE;\r
-  \r
-  //\r
-  // Ensure the stack base is in page alignment \r
-  //\r
-  PrivateData->StackBase = ((UINTN)MemoryBegin + (EFI_PAGE_SIZE - 1)) & EFI_PAGE_SIZE;\r
-  \r
-  PeiStackSize = (RShiftU64 (MemoryLength, 1) + (EFI_PAGE_SIZE - 1)) & EFI_PAGE_SIZE;\r
-  if (PEI_STACK_SIZE > PeiStackSize) {\r
-    PrivateData->StackSize = PeiStackSize;\r
-  } else {\r
-    PrivateData->StackSize = PEI_STACK_SIZE;\r
-  }\r
+  PrivateData->PhysicalMemoryBegin   = MemoryBegin;\r
+  PrivateData->PhysicalMemoryLength  = MemoryLength;\r
+  PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
+   \r
+  PrivateData->SwitchStackSignal      = TRUE;\r
 \r
-  OldHandOffHob = PrivateData->HobList.HandoffInformationTable;\r
+  return EFI_SUCCESS;   \r
+}\r
 \r
-  PrivateData->HobList.Raw = (VOID *)((UINTN)(PrivateData->StackBase + PrivateData->StackSize));\r
-  NewHandOffHob = PrivateData->HobList.HandoffInformationTable;\r
-  PhysicalAddressOfOldHob = (EFI_PHYSICAL_ADDRESS) (UINTN) OldHandOffHob;\r
+/**\r
 \r
-  EfiFreeMemorySize = OldHandOffHob->EfiFreeMemoryBottom - PhysicalAddressOfOldHob;\r
-  \r
-  DEBUG ((EFI_D_INFO, "HOBLIST address before memory init = 0x%p\n",  OldHandOffHob));\r
-  DEBUG ((EFI_D_INFO, "HOBLIST address after memory init = 0x%p\n", NewHandOffHob));\r
-\r
-  CopyMem (\r
-    NewHandOffHob,\r
-    OldHandOffHob,\r
-    (UINTN)EfiFreeMemorySize\r
-    );\r
-\r
-  NewHandOffHob->EfiMemoryTop     = MemoryBegin + MemoryLength;\r
-  NewHandOffHob->EfiFreeMemoryTop = NewHandOffHob->EfiMemoryTop;\r
-  NewHandOffHob->EfiMemoryBottom  = MemoryBegin;\r
-  \r
-  NewHandOffHob->EfiFreeMemoryBottom = (UINTN)NewHandOffHob + EfiFreeMemorySize;                                     \r
-                                       \r
-  NewHandOffHob->EfiEndOfHobList     = (UINTN)NewHandOffHob +\r
-                                       (OldHandOffHob->EfiEndOfHobList -\r
-                                        PhysicalAddressOfOldHob);\r
-  \r
-  //\r
-  // For IPF in CAR mode the real memory access is uncached,in InstallPeiMemory()\r
-  //  the 63-bit of address is set to 1.\r
-  //\r
-  SWITCH_TO_CACHE_MODE (PrivateData);\r
-  \r
-  ConvertPpiPointers (PeiServices, OldHandOffHob, NewHandOffHob);\r
+  Memory allocation service on permanent memory,\r
+  not usable prior to the memory installation.\r
 \r
-  BuildStackHob (PrivateData->StackBase, PrivateData->StackSize);\r
-  \r
 \r
-  return EFI_SUCCESS;   \r
-}\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param MemoryType      Type of memory to allocate.\r
+  @param Pages           Number of pages to allocate.\r
+  @param Memory          Pointer of memory allocated.\r
+\r
+  @retval EFI_SUCCESS              The allocation was successful\r
+  @retval EFI_INVALID_PARAMETER    Only AllocateAnyAddress is supported.\r
+  @retval EFI_NOT_AVAILABLE_YET    Called with permanent memory not available\r
+  @retval EFI_OUT_OF_RESOURCES     There is not enough HOB heap to satisfy the requirement\r
+                                   to allocate the number of pages.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiAllocatePages (\r
@@ -187,83 +134,72 @@ PeiAllocatePages (
   IN UINTN                      Pages,\r
   OUT EFI_PHYSICAL_ADDRESS      *Memory\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Memory allocation service on permanent memory, \r
-  not usable prior to the memory installation.\r
-\r
-Arguments:\r
-\r
-  PeiServices - The PEI core services table.\r
-  MemoryType  - Type of memory to allocate.\r
-  Pages       - Number of pages to allocate.\r
-  Memory      - Pointer of memory allocated.\r
-\r
-Returns:\r
-\r
-  Status - EFI_SUCCESS              The allocation was successful\r
-           EFI_INVALID_PARAMETER    Only AllocateAnyAddress is supported.\r
-           EFI_NOT_AVAILABLE_YET    Called with permanent memory not available\r
-           EFI_OUT_OF_RESOURCES     There is not enough HOB heap to satisfy the requirement\r
-                                    to allocate the number of pages.\r
-\r
---*/\r
 {\r
   PEI_CORE_INSTANCE                       *PrivateData;\r
   EFI_PEI_HOB_POINTERS                    Hob;\r
   EFI_PHYSICAL_ADDRESS                    Offset;\r
+  EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;\r
+  EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;\r
 \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
-\r
+  Hob.Raw     = PrivateData->HobList.Raw;\r
+  \r
   //\r
   // Check if Hob already available\r
   //\r
   if (!PrivateData->PeiMemoryInstalled) {\r
-    return EFI_NOT_AVAILABLE_YET;\r
+    //\r
+    // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
+    // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.\r
+    //\r
+    if (!PrivateData->SwitchStackSignal) {\r
+      return EFI_NOT_AVAILABLE_YET;\r
+    } else {\r
+      FreeMemoryTop     = &(PrivateData->FreePhysicalMemoryTop);\r
+      FreeMemoryBottom  = &(PrivateData->PhysicalMemoryBegin);\r
+    }\r
+  } else {\r
+    FreeMemoryTop     = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
+    FreeMemoryBottom  = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
   }\r
 \r
-  Hob.Raw = PrivateData->HobList.Raw;\r
-\r
   //\r
   // Check to see if on 4k boundary\r
   //\r
-  Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
-\r
+  Offset = *(FreeMemoryTop) & 0xFFF;\r
+  \r
   //\r
   // If not aligned, make the allocation aligned.\r
   //\r
   if (Offset != 0) {\r
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
+    *(FreeMemoryTop) -= Offset;\r
   }\r
-\r
-  ASSERT (Hob.HandoffInformationTable->EfiFreeMemoryTop >= Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
+  \r
   //\r
   // Verify that there is sufficient memory to satisfy the allocation\r
   //\r
-  if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
-      Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\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) (Hob.HandoffInformationTable->EfiFreeMemoryTop - Hob.HandoffInformationTable->EfiFreeMemoryBottom))));\r
+    EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r
     return  EFI_OUT_OF_RESOURCES;\r
   } else {\r
     //\r
     // Update the PHIT to reflect the memory usage\r
     //\r
-    Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
+    *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r
 \r
     //\r
     // Update the value for the caller\r
     //\r
-    *Memory = Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
+    *Memory = *(FreeMemoryTop);\r
 \r
     //\r
     // Create a memory allocation HOB.\r
     //\r
     BuildMemoryAllocationHob (\r
-      Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
+      *(FreeMemoryTop),\r
       Pages * EFI_PAGE_SIZE,\r
       MemoryType\r
       );\r
@@ -272,7 +208,20 @@ Returns:
   }\r
 }\r
 \r
+/**\r
+\r
+  Memory allocation service on the CAR.\r
 \r
+\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param Size            Amount of memory required\r
+  @param Buffer          Address of pointer to the buffer\r
+\r
+  @retval EFI_SUCCESS              The allocation was successful\r
+  @retval EFI_OUT_OF_RESOURCES     There is not enough heap to satisfy the requirement\r
+                                   to allocate the requested size.\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiAllocatePool (\r
@@ -280,27 +229,6 @@ PeiAllocatePool (
   IN UINTN                      Size,\r
   OUT VOID                      **Buffer\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Memory allocation service on the CAR.  \r
-\r
-Arguments:\r
-\r
-  PeiServices - The PEI core services table.\r
-\r
-  Size        - Amount of memory required\r
-\r
-  Buffer      - Address of pointer to the buffer\r
-\r
-Returns:\r
-\r
-  Status - EFI_SUCCESS              The allocation was successful\r
-           EFI_OUT_OF_RESOURCES     There is not enough heap to satisfy the requirement\r
-                                    to allocate the requested size.\r
-                                    \r
---*/\r
 {\r
   EFI_STATUS               Status;\r
   EFI_HOB_MEMORY_POOL      *Hob;\r