]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / DebugImageInfo.c
1 /** @file
2 Support functions for managing debug image info table when loading and unloading
3 images.
4
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "DxeMain.h"
11
12
13 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {
14 0, // volatile UINT32 UpdateStatus;
15 0, // UINT32 TableSize;
16 NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
17 };
18
19 UINTN mMaxTableEntries = 0;
20
21 EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;
22
23 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
24
25 /**
26 Creates and initializes the DebugImageInfo Table. Also creates the configuration
27 table and registers it into the system table.
28
29 **/
30 VOID
31 CoreInitializeDebugImageInfoTable (
32 VOID
33 )
34 {
35 EFI_STATUS Status;
36 UINTN Pages;
37 EFI_PHYSICAL_ADDRESS Memory;
38 UINTN AlignedMemory;
39 UINTN AlignmentMask;
40 UINTN UnalignedPages;
41 UINTN RealPages;
42
43 //
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.
47 //
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);
51
52 //
53 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
54 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
55 // MAX_ADDRESS
56 //
57 Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);
58 if (Memory == 0) {
59 Memory = MAX_ADDRESS;
60 }
61 Status = CoreAllocatePages (
62 AllocateMaxAddress,
63 EfiBootServicesData,
64 RealPages,
65 &Memory
66 );
67 if (EFI_ERROR (Status)) {
68 if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress) != 0) {
69 DEBUG ((EFI_D_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"));
71 }
72 //
73 // If the initial memory allocation fails, then reattempt allocation
74 // as close to the top of memory as feasible.
75 //
76 Status = CoreAllocatePages (
77 AllocateAnyPages,
78 EfiBootServicesData,
79 RealPages,
80 &Memory
81 );
82 ASSERT_EFI_ERROR (Status);
83 if (EFI_ERROR (Status)) {
84 return;
85 }
86 }
87
88 //
89 // Free overallocated pages
90 //
91 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
92 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);
93 if (UnalignedPages > 0) {
94 //
95 // Free first unaligned page(s).
96 //
97 Status = CoreFreePages (Memory, UnalignedPages);
98 ASSERT_EFI_ERROR (Status);
99 }
100 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
101 UnalignedPages = RealPages - Pages - UnalignedPages;
102 if (UnalignedPages > 0) {
103 //
104 // Free last unaligned page(s).
105 //
106 Status = CoreFreePages (Memory, UnalignedPages);
107 ASSERT_EFI_ERROR (Status);
108 }
109
110 //
111 // Set mDebugTable to the 4MB aligned allocated pages
112 //
113 mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(AlignedMemory);
114 ASSERT (mDebugTable != NULL);
115
116 //
117 // Initialize EFI_SYSTEM_TABLE_POINTER structure
118 //
119 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;
120 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;
121 mDebugTable->Crc32 = 0;
122
123 //
124 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
125 // Configuration Table
126 //
127 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);
128 ASSERT_EFI_ERROR (Status);
129 }
130
131
132 /**
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.
138
139 **/
140 VOID
141 CoreUpdateDebugTableCrc32 (
142 VOID
143 )
144 {
145 ASSERT(mDebugTable != NULL);
146 mDebugTable->Crc32 = 0;
147 gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);
148 }
149
150
151 /**
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.
154
155 @param ImageInfoType type of debug image information
156 @param LoadedImage pointer to the loaded image protocol for the image being
157 loaded
158 @param ImageHandle image handle for the image being loaded
159
160 **/
161 VOID
162 CoreNewDebugImageInfoEntry (
163 IN UINT32 ImageInfoType,
164 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,
165 IN EFI_HANDLE ImageHandle
166 )
167 {
168 EFI_DEBUG_IMAGE_INFO *Table;
169 EFI_DEBUG_IMAGE_INFO *NewTable;
170 UINTN Index;
171 UINTN TableSize;
172
173 //
174 // Set the flag indicating that we're in the process of updating the table.
175 //
176 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
177
178 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
179
180 if (mDebugInfoTableHeader.TableSize < mMaxTableEntries) {
181 //
182 // We still have empty entires in the Table, find the first empty entry.
183 //
184 Index = 0;
185 while (Table[Index].NormalImage != NULL) {
186 Index++;
187 }
188 //
189 // There must be an empty entry in the in the table.
190 //
191 ASSERT (Index < mMaxTableEntries);
192 } else {
193 //
194 // Table is full, so re-allocate another page for a larger table...
195 //
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;
200 return;
201 }
202 //
203 // Copy the old table into the new one
204 //
205 CopyMem (NewTable, Table, TableSize);
206 //
207 // Free the old table
208 //
209 CoreFreePool (Table);
210 //
211 // Update the table header
212 //
213 Table = NewTable;
214 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;
215 //
216 // Enlarge the max table entries and set the first empty entry index to
217 // be the original max table entries.
218 //
219 Index = mMaxTableEntries;
220 mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
221 }
222
223 //
224 // Allocate data for new entry
225 //
226 Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));
227 if (Table[Index].NormalImage != NULL) {
228 //
229 // Update the entry
230 //
231 Table[Index].NormalImage->ImageInfoType = (UINT32) ImageInfoType;
232 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;
233 Table[Index].NormalImage->ImageHandle = ImageHandle;
234 //
235 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
236 //
237 mDebugInfoTableHeader.TableSize++;
238 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
239 }
240 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
241 }
242
243
244
245 /**
246 Removes and frees an entry from the DebugImageInfo Table.
247
248 @param ImageHandle image handle for the image being unloaded
249
250 **/
251 VOID
252 CoreRemoveDebugImageInfoEntry (
253 EFI_HANDLE ImageHandle
254 )
255 {
256 EFI_DEBUG_IMAGE_INFO *Table;
257 UINTN Index;
258
259 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
260
261 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
262
263 for (Index = 0; Index < mMaxTableEntries; Index++) {
264 if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {
265 //
266 // Found a match. Free up the record, then NULL the pointer to indicate the slot
267 // is free.
268 //
269 CoreFreePool (Table[Index].NormalImage);
270 Table[Index].NormalImage = NULL;
271 //
272 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
273 //
274 mDebugInfoTableHeader.TableSize--;
275 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
276 break;
277 }
278 }
279 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
280 }
281
282