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
12 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader
= {
13 0, // volatile UINT32 UpdateStatus;
14 0, // UINT32 TableSize;
15 NULL
// EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
18 UINTN mMaxTableEntries
= 0;
20 EFI_SYSTEM_TABLE_POINTER
*mDebugTable
= NULL
;
22 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
25 Creates and initializes the DebugImageInfo Table. Also creates the configuration
26 table and registers it into the system table.
30 CoreInitializeDebugImageInfoTable (
36 EFI_PHYSICAL_ADDRESS Memory
;
43 // Allocate 4M aligned page for the structure and fill in the data.
44 // Ideally we would update the CRC now as well, but the service may not yet be available.
45 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
47 Pages
= EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER
));
48 AlignmentMask
= SIZE_4MB
- 1;
49 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (SIZE_4MB
);
52 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
53 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
56 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"));
74 // If the initial memory allocation fails, then reattempt allocation
75 // as close to the top of memory as feasible.
77 Status
= CoreAllocatePages (
83 ASSERT_EFI_ERROR (Status
);
84 if (EFI_ERROR (Status
)) {
90 // Free overallocated pages
92 AlignedMemory
= ((UINTN
)Memory
+ AlignmentMask
) & ~AlignmentMask
;
93 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
)Memory
);
94 if (UnalignedPages
> 0) {
96 // Free first unaligned page(s).
98 Status
= CoreFreePages (Memory
, UnalignedPages
);
99 ASSERT_EFI_ERROR (Status
);
102 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
103 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
104 if (UnalignedPages
> 0) {
106 // Free last unaligned page(s).
108 Status
= CoreFreePages (Memory
, UnalignedPages
);
109 ASSERT_EFI_ERROR (Status
);
113 // Set mDebugTable to the 4MB aligned allocated pages
115 mDebugTable
= (EFI_SYSTEM_TABLE_POINTER
*)(AlignedMemory
);
116 ASSERT (mDebugTable
!= NULL
);
119 // Initialize EFI_SYSTEM_TABLE_POINTER structure
121 mDebugTable
->Signature
= EFI_SYSTEM_TABLE_SIGNATURE
;
122 mDebugTable
->EfiSystemTableBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gDxeCoreST
;
123 mDebugTable
->Crc32
= 0;
126 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
127 // Configuration Table
129 Status
= CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid
, &mDebugInfoTableHeader
);
130 ASSERT_EFI_ERROR (Status
);
134 Update the CRC32 in the Debug Table.
135 Since the CRC32 service is made available by the Runtime driver, we have to
136 wait for the Runtime Driver to be installed before the CRC32 can be computed.
137 This function is called elsewhere by the core when the runtime architectural
138 protocol is produced.
142 CoreUpdateDebugTableCrc32 (
146 ASSERT (mDebugTable
!= NULL
);
147 mDebugTable
->Crc32
= 0;
148 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
) {
190 // There must be an empty entry in the in the table.
192 ASSERT (Index
< mMaxTableEntries
);
195 // Table is full, so re-allocate another page for a larger table...
197 TableSize
= mMaxTableEntries
* EFI_DEBUG_TABLE_ENTRY_SIZE
;
198 NewTable
= AllocateZeroPool (TableSize
+ EFI_PAGE_SIZE
);
199 if (NewTable
== NULL
) {
200 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
205 // Copy the old table into the new one
207 CopyMem (NewTable
, Table
, TableSize
);
209 // Free the old table
211 CoreFreePool (Table
);
213 // Update the table header
216 mDebugInfoTableHeader
.EfiDebugImageInfoTable
= NewTable
;
218 // Enlarge the max table entries and set the first empty entry index to
219 // be the original max table entries.
221 Index
= mMaxTableEntries
;
222 mMaxTableEntries
+= EFI_PAGE_SIZE
/ EFI_DEBUG_TABLE_ENTRY_SIZE
;
226 // Allocate data for new entry
228 Table
[Index
].NormalImage
= AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL
));
229 if (Table
[Index
].NormalImage
!= NULL
) {
233 Table
[Index
].NormalImage
->ImageInfoType
= (UINT32
)ImageInfoType
;
234 Table
[Index
].NormalImage
->LoadedImageProtocolInstance
= LoadedImage
;
235 Table
[Index
].NormalImage
->ImageHandle
= ImageHandle
;
237 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
239 mDebugInfoTableHeader
.TableSize
++;
240 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
243 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
247 Removes and frees an entry from the DebugImageInfo Table.
249 @param ImageHandle image handle for the image being unloaded
253 CoreRemoveDebugImageInfoEntry (
254 EFI_HANDLE ImageHandle
257 EFI_DEBUG_IMAGE_INFO
*Table
;
260 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;
262 Table
= mDebugInfoTableHeader
.EfiDebugImageInfoTable
;
264 for (Index
= 0; Index
< mMaxTableEntries
; Index
++) {
265 if ((Table
[Index
].NormalImage
!= NULL
) && (Table
[Index
].NormalImage
->ImageHandle
== ImageHandle
)) {
267 // Found a match. Free up the record, then NULL the pointer to indicate the slot
270 CoreFreePool (Table
[Index
].NormalImage
);
271 Table
[Index
].NormalImage
= NULL
;
273 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
275 mDebugInfoTableHeader
.TableSize
--;
276 mDebugInfoTableHeader
.UpdateStatus
|= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED
;
281 mDebugInfoTableHeader
.UpdateStatus
&= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS
;