2 Support functions for managing debug image info table when loading and unloading
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader
= {
14 0, // volatile UINT32 UpdateStatus;
15 0, // UINT32 TableSize;
16 NULL
// EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
19 UINTN mMaxTableEntries
= 0;
21 EFI_SYSTEM_TABLE_POINTER
*mDebugTable
= NULL
;
23 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
26 Creates and initializes the DebugImageInfo Table. Also creates the configuration
27 table and registers it into the system table.
31 CoreInitializeDebugImageInfoTable (
37 EFI_PHYSICAL_ADDRESS Memory
;
44 // Allocate 4M aligned page for the structure and fill in the data.
45 // Ideally we would update the CRC now as well, but the service may not yet be available.
46 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
48 Pages
= EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER
));
49 AlignmentMask
= SIZE_4MB
- 1;
50 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (SIZE_4MB
);
53 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
54 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
57 Memory
= PcdGet64 (PcdMaxEfiSystemTablePointerAddress
);
61 Status
= CoreAllocatePages (
67 if (EFI_ERROR (Status
)) {
68 if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress
) != 0) {
69 DEBUG ((DEBUG_INFO
, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \
70 Retry to allocate memroy as close to the top of memory as feasible.\n"));
73 // If the initial memory allocation fails, then reattempt allocation
74 // as close to the top of memory as feasible.
76 Status
= CoreAllocatePages (
82 ASSERT_EFI_ERROR (Status
);
83 if (EFI_ERROR (Status
)) {
89 // Free overallocated pages
91 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
92 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
)Memory
);
93 if (UnalignedPages
> 0) {
95 // Free first unaligned page(s).
97 Status
= CoreFreePages (Memory
, UnalignedPages
);
98 ASSERT_EFI_ERROR (Status
);
100 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
101 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
102 if (UnalignedPages
> 0) {
104 // Free last unaligned page(s).
106 Status
= CoreFreePages (Memory
, UnalignedPages
);
107 ASSERT_EFI_ERROR (Status
);
111 // Set mDebugTable to the 4MB aligned allocated pages
113 mDebugTable
= (EFI_SYSTEM_TABLE_POINTER
*)(AlignedMemory
);
114 ASSERT (mDebugTable
!= NULL
);
117 // Initialize EFI_SYSTEM_TABLE_POINTER structure
119 mDebugTable
->Signature
= EFI_SYSTEM_TABLE_SIGNATURE
;
120 mDebugTable
->EfiSystemTableBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) gDxeCoreST
;
121 mDebugTable
->Crc32
= 0;
124 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
125 // Configuration Table
127 Status
= CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid
, &mDebugInfoTableHeader
);
128 ASSERT_EFI_ERROR (Status
);
133 Update the CRC32 in the Debug Table.
134 Since the CRC32 service is made available by the Runtime driver, we have to
135 wait for the Runtime Driver to be installed before the CRC32 can be computed.
136 This function is called elsewhere by the core when the runtime architectural
137 protocol is produced.
141 CoreUpdateDebugTableCrc32 (
145 ASSERT(mDebugTable
!= NULL
);
146 mDebugTable
->Crc32
= 0;
147 gBS
->CalculateCrc32 ((VOID
*)mDebugTable
, sizeof (EFI_SYSTEM_TABLE_POINTER
), &mDebugTable
->Crc32
);
152 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates
153 the table if it's not large enough to accomidate another entry.
155 @param ImageInfoType type of debug image information
156 @param LoadedImage pointer to the loaded image protocol for the image being
158 @param ImageHandle image handle for the image being loaded
162 CoreNewDebugImageInfoEntry (
163 IN UINT32 ImageInfoType
,
164 IN EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
,
165 IN EFI_HANDLE ImageHandle
168 EFI_DEBUG_IMAGE_INFO
*Table
;
169 EFI_DEBUG_IMAGE_INFO
*NewTable
;
174 // Set the flag indicating that we're in the process of updating the table.
176 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
178 Table
= mDebugInfoTableHeader
.EfiDebugImageInfoTable
;
180 if (mDebugInfoTableHeader
.TableSize
< mMaxTableEntries
) {
182 // We still have empty entires in the Table, find the first empty entry.
185 while (Table
[Index
].NormalImage
!= NULL
) {
189 // There must be an empty entry in the in the table.
191 ASSERT (Index
< mMaxTableEntries
);
194 // Table is full, so re-allocate another page for a larger table...
196 TableSize
= mMaxTableEntries
* EFI_DEBUG_TABLE_ENTRY_SIZE
;
197 NewTable
= AllocateZeroPool (TableSize
+ EFI_PAGE_SIZE
);
198 if (NewTable
== NULL
) {
199 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
203 // Copy the old table into the new one
205 CopyMem (NewTable
, Table
, TableSize
);
207 // Free the old table
209 CoreFreePool (Table
);
211 // Update the table header
214 mDebugInfoTableHeader
.EfiDebugImageInfoTable
= NewTable
;
216 // Enlarge the max table entries and set the first empty entry index to
217 // be the original max table entries.
219 Index
= mMaxTableEntries
;
220 mMaxTableEntries
+= EFI_PAGE_SIZE
/ EFI_DEBUG_TABLE_ENTRY_SIZE
;
224 // Allocate data for new entry
226 Table
[Index
].NormalImage
= AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL
));
227 if (Table
[Index
].NormalImage
!= NULL
) {
231 Table
[Index
].NormalImage
->ImageInfoType
= (UINT32
) ImageInfoType
;
232 Table
[Index
].NormalImage
->LoadedImageProtocolInstance
= LoadedImage
;
233 Table
[Index
].NormalImage
->ImageHandle
= ImageHandle
;
235 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
237 mDebugInfoTableHeader
.TableSize
++;
238 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
240 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
246 Removes and frees an entry from the DebugImageInfo Table.
248 @param ImageHandle image handle for the image being unloaded
252 CoreRemoveDebugImageInfoEntry (
253 EFI_HANDLE ImageHandle
256 EFI_DEBUG_IMAGE_INFO
*Table
;
259 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
261 Table
= mDebugInfoTableHeader
.EfiDebugImageInfoTable
;
263 for (Index
= 0; Index
< mMaxTableEntries
; Index
++) {
264 if (Table
[Index
].NormalImage
!= NULL
&& Table
[Index
].NormalImage
->ImageHandle
== ImageHandle
) {
266 // Found a match. Free up the record, then NULL the pointer to indicate the slot
269 CoreFreePool (Table
[Index
].NormalImage
);
270 Table
[Index
].NormalImage
= NULL
;
272 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
274 mDebugInfoTableHeader
.TableSize
--;
275 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
279 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;