]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / DebugImageInfo.c
index 90e5738532acce6884139a8025ac38ca73c1d575..a75d4158280b9597e5d265d8ce6fecdeeb49598c 100644 (file)
-/** @file \r
-\r
+/** @file\r
   Support functions for managing debug image info table when loading and unloading\r
   images.\r
 \r
-Copyright (c) 2006 - 2008, 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
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-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
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
 \r
 \r
-static EFI_DEBUG_IMAGE_INFO_TABLE_HEADER  mDebugInfoTableHeader = {\r
+EFI_DEBUG_IMAGE_INFO_TABLE_HEADER  mDebugInfoTableHeader = {\r
   0,          // volatile UINT32                 UpdateStatus;\r
   0,          // UINT32                          TableSize;\r
   NULL        // EFI_DEBUG_IMAGE_INFO            *EfiDebugImageInfoTable;\r
 };\r
 \r
-static EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;\r
+UINTN mMaxTableEntries = 0;\r
 \r
-    \r
-VOID\r
-CoreInitializeDebugImageInfoTable (\r
-  VOID\r
-  )\r
-/*++\r
+EFI_SYSTEM_TABLE_POINTER  *mDebugTable = NULL;\r
 \r
-Routine Description:\r
+#define EFI_DEBUG_TABLE_ENTRY_SIZE       (sizeof (VOID *))\r
 \r
+/**\r
   Creates and initializes the DebugImageInfo Table.  Also creates the configuration\r
   table and registers it into the system table.\r
 \r
-Arguments:\r
-  None\r
-\r
-Returns:\r
-  NA\r
-\r
-Notes:\r
-  This function allocates memory, frees it, and then allocates memory at an\r
-  address within the initial allocation. Since this function is called early\r
-  in DXE core initialization (before drivers are dispatched), this should not\r
-  be a problem.\r
-\r
---*/\r
-{ \r
-  EFI_STATUS                          Status;\r
-  EFI_PHYSICAL_ADDRESS                Mem;\r
-  UINTN                               NumberOfPages; \r
+**/\r
+VOID\r
+CoreInitializeDebugImageInfoTable (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  UINTN                 Pages;\r
+  EFI_PHYSICAL_ADDRESS  Memory;\r
+  UINTN                 AlignedMemory;\r
+  UINTN                 AlignmentMask;\r
+  UINTN                 UnalignedPages;\r
+  UINTN                 RealPages;\r
 \r
   //\r
-  // Allocate boot services memory for the structure. It's required to be aligned on\r
-  // a 4M boundary, so allocate a 4M block (plus what we require), free it up, calculate\r
-  // a 4M aligned address within the memory we just freed, and then allocate memory at that\r
-  // address for our initial structure.\r
+  // Allocate 4M aligned page for the structure and fill in the data.\r
+  // Ideally we would update the CRC now as well, but the service may not yet be available.\r
+  // See comments in the CoreUpdateDebugTableCrc32() function below for details.\r
   //\r
-  NumberOfPages = FOUR_MEG_PAGES + EFI_SIZE_TO_PAGES(sizeof (EFI_SYSTEM_TABLE_POINTER));  \r
+  Pages          = EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER));\r
+  AlignmentMask  = SIZE_4MB - 1;\r
+  RealPages      = Pages + EFI_SIZE_TO_PAGES (SIZE_4MB);\r
 \r
-  Status = CoreAllocatePages (AllocateAnyPages, EfiBootServicesData, NumberOfPages , &Mem); \r
-  ASSERT_EFI_ERROR (Status);\r
-  if (EFI_ERROR(Status)) {\r
-    return;\r
+  //\r
+  // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress\r
+  // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below\r
+  // MAX_ADDRESS\r
+  //\r
+  Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);\r
+  if (Memory == 0) {\r
+    Memory = MAX_ADDRESS;\r
   }\r
-  Status = CoreFreePages (Mem, NumberOfPages); \r
-  ASSERT_EFI_ERROR (Status);\r
-  if (EFI_ERROR(Status)) {\r
-    return;\r
+  Status = CoreAllocatePages (\r
+             AllocateMaxAddress,\r
+             EfiBootServicesData,\r
+             RealPages,\r
+             &Memory\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress) != 0) {\r
+      DEBUG ((EFI_D_INFO, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \\r
+                          Retry to allocate memroy as close to the top of memory as feasible.\n"));\r
+    }\r
+    //\r
+    // If the initial memory allocation fails, then reattempt allocation\r
+    // as close to the top of memory as feasible.\r
+    //\r
+    Status = CoreAllocatePages (\r
+               AllocateAnyPages,\r
+               EfiBootServicesData,\r
+               RealPages,\r
+               &Memory\r
+               );\r
+    ASSERT_EFI_ERROR (Status);\r
+    if (EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
   }\r
+\r
   //\r
-  // Now get a 4M aligned address within the memory range we were given.\r
-  // Then allocate memory at that address\r
+  // Free overallocated pages\r
   //\r
-  Mem = (Mem + FOUR_MEG_MASK) & (~FOUR_MEG_MASK);\r
-  \r
-  Status = CoreAllocatePages (AllocateAddress, EfiBootServicesData, NumberOfPages - FOUR_MEG_PAGES, &Mem); \r
-  ASSERT_EFI_ERROR (Status);\r
-  if (EFI_ERROR(Status)) {\r
-    return;\r
+  AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
+  UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
+  if (UnalignedPages > 0) {\r
+    //\r
+    // Free first unaligned page(s).\r
+    //\r
+    Status = CoreFreePages (Memory, UnalignedPages);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+  Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
+  UnalignedPages = RealPages - Pages - UnalignedPages;\r
+  if (UnalignedPages > 0) {\r
+    //\r
+    // Free last unaligned page(s).\r
+    //\r
+    Status = CoreFreePages (Memory, UnalignedPages);\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
+\r
   //\r
-  // We now have a 4M aligned page allocated, so fill in the data structure.\r
-  // Ideally we would update the CRC now as well, but the service may not yet be available.\r
-  // See comments in the CoreUpdateDebugTableCrc32() function below for details.\r
+  // Set mDebugTable to the 4MB aligned allocated pages\r
+  //\r
+  mDebugTable = (EFI_SYSTEM_TABLE_POINTER  *)(AlignedMemory);\r
+  ASSERT (mDebugTable != NULL);\r
+\r
+  //\r
+  // Initialize EFI_SYSTEM_TABLE_POINTER structure\r
   //\r
-  mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(UINTN)Mem;\r
-  mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;\r
+  mDebugTable->Signature          = EFI_SYSTEM_TABLE_SIGNATURE;\r
   mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;\r
-  mDebugTable->Crc32 = 0;\r
+  mDebugTable->Crc32              = 0;\r
+\r
+  //\r
+  // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System\r
+  // Configuration Table\r
+  //\r
   Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
-VOID\r
-CoreUpdateDebugTableCrc32 (\r
-  VOID\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
 \r
+/**\r
   Update the CRC32 in the Debug Table.\r
   Since the CRC32 service is made available by the Runtime driver, we have to\r
   wait for the Runtime Driver to be installed before the CRC32 can be computed.\r
   This function is called elsewhere by the core when the runtime architectural\r
   protocol is produced.\r
 \r
-Arguments:\r
-  None\r
-\r
-Returns:\r
-  NA\r
-\r
---*/\r
+**/\r
+VOID\r
+CoreUpdateDebugTableCrc32 (\r
+  VOID\r
+  )\r
 {\r
   ASSERT(mDebugTable != NULL);\r
   mDebugTable->Crc32 = 0;\r
-  gDxeCoreBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);\r
+  gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);\r
 }\r
 \r
+\r
+/**\r
+  Adds a new DebugImageInfo structure to the DebugImageInfo Table.  Re-Allocates\r
+  the table if it's not large enough to accomidate another entry.\r
+\r
+  @param  ImageInfoType  type of debug image information\r
+  @param  LoadedImage    pointer to the loaded image protocol for the image being\r
+                         loaded\r
+  @param  ImageHandle    image handle for the image being loaded\r
+\r
+**/\r
 VOID\r
 CoreNewDebugImageInfoEntry (\r
   IN  UINT32                      ImageInfoType,\r
   IN  EFI_LOADED_IMAGE_PROTOCOL   *LoadedImage,\r
   IN  EFI_HANDLE                  ImageHandle\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Adds a new DebugImageInfo structure to the DebugImageInfo Table.  Re-Allocates\r
-  the table if it's not large enough to accomidate another entry.\r
-\r
-Arguments:\r
-\r
-  ImageInfoType     - type of debug image information\r
-  LoadedImage       - pointer to the loaded image protocol for the image being loaded\r
-  ImageHandle       - image handle for the image being loaded\r
-\r
-Returns:\r
-  NA\r
-\r
---*/\r
-{    \r
+{\r
   EFI_DEBUG_IMAGE_INFO      *Table;\r
   EFI_DEBUG_IMAGE_INFO      *NewTable;\r
   UINTN                     Index;\r
-  UINTN                     MaxTableIndex;\r
   UINTN                     TableSize;\r
 \r
   //\r
@@ -160,22 +176,25 @@ Returns:
   mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
 \r
   Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;\r
-  MaxTableIndex = mDebugInfoTableHeader.TableSize;\r
 \r
-  for (Index = 0; Index < MaxTableIndex; Index++) {\r
-    if (Table[Index].NormalImage == NULL) {\r
-      //\r
-      // We have found a free entry so exit the loop\r
-      //\r
-      break;\r
+  if (mDebugInfoTableHeader.TableSize < mMaxTableEntries) {\r
+    //\r
+    // We still have empty entires in the Table, find the first empty entry.\r
+    //\r
+    Index = 0;\r
+    while (Table[Index].NormalImage != NULL) {\r
+      Index++;\r
     }\r
-  }\r
-  if (Index == MaxTableIndex) {\r
+    //\r
+    // There must be an empty entry in the in the table.\r
+    //\r
+    ASSERT (Index < mMaxTableEntries);\r
+  } else {\r
     //\r
     //  Table is full, so re-allocate another page for a larger table...\r
     //\r
-    TableSize = MaxTableIndex * EFI_DEBUG_TABLE_ENTRY_SIZE;\r
-    NewTable = CoreAllocateZeroBootServicesPool (TableSize + EFI_PAGE_SIZE);\r
+    TableSize = mMaxTableEntries * EFI_DEBUG_TABLE_ENTRY_SIZE;\r
+    NewTable = AllocateZeroPool (TableSize + EFI_PAGE_SIZE);\r
     if (NewTable == NULL) {\r
       mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
       return;\r
@@ -193,12 +212,18 @@ Returns:
     //\r
     Table = NewTable;\r
     mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;\r
-    mDebugInfoTableHeader.TableSize += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;\r
+    //\r
+    // Enlarge the max table entries and set the first empty entry index to\r
+    // be the original max table entries.\r
+    //\r
+    Index             = mMaxTableEntries;\r
+    mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;\r
   }\r
+\r
   //\r
   // Allocate data for new entry\r
   //\r
-  Table[Index].NormalImage = CoreAllocateZeroBootServicesPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));\r
+  Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));\r
   if (Table[Index].NormalImage != NULL) {\r
     //\r
     // Update the entry\r
@@ -206,31 +231,28 @@ Returns:
     Table[Index].NormalImage->ImageInfoType               = (UINT32) ImageInfoType;\r
     Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;\r
     Table[Index].NormalImage->ImageHandle                 = ImageHandle;\r
+    //\r
+    // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.\r
+    //\r
+    mDebugInfoTableHeader.TableSize++;\r
+    mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;\r
   }\r
   mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
 }\r
 \r
 \r
-VOID\r
-CoreRemoveDebugImageInfoEntry (\r
-  EFI_HANDLE ImageHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
 \r
+/**\r
   Removes and frees an entry from the DebugImageInfo Table.\r
 \r
-Arguments:\r
+  @param  ImageHandle    image handle for the image being unloaded\r
 \r
-  ImageHandle       - image handle for the image being unloaded\r
-\r
-Returns:\r
-\r
-  NA\r
-\r
---*/\r
-{    \r
+**/\r
+VOID\r
+CoreRemoveDebugImageInfoEntry (\r
+  EFI_HANDLE ImageHandle\r
+  )\r
+{\r
   EFI_DEBUG_IMAGE_INFO  *Table;\r
   UINTN                 Index;\r
 \r
@@ -238,7 +260,7 @@ Returns:
 \r
   Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;\r
 \r
-  for (Index = 0; Index < mDebugInfoTableHeader.TableSize; Index++) {\r
+  for (Index = 0; Index < mMaxTableEntries; Index++) {\r
     if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {\r
       //\r
       // Found a match. Free up the record, then NULL the pointer to indicate the slot\r
@@ -246,9 +268,15 @@ Returns:
       //\r
       CoreFreePool (Table[Index].NormalImage);\r
       Table[Index].NormalImage = NULL;\r
+      //\r
+      // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.\r
+      //\r
+      mDebugInfoTableHeader.TableSize--;\r
+      mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;\r
       break;\r
     }\r
   }\r
   mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;\r
 }\r
 \r
+\r