]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
MdeModulePkg: Clean up source files
[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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "DxeMain.h"
17
18
19 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {
20 0, // volatile UINT32 UpdateStatus;
21 0, // UINT32 TableSize;
22 NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
23 };
24
25 UINTN mMaxTableEntries = 0;
26
27 EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;
28
29 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
30
31 /**
32 Creates and initializes the DebugImageInfo Table. Also creates the configuration
33 table and registers it into the system table.
34
35 **/
36 VOID
37 CoreInitializeDebugImageInfoTable (
38 VOID
39 )
40 {
41 EFI_STATUS Status;
42 UINTN Pages;
43 EFI_PHYSICAL_ADDRESS Memory;
44 UINTN AlignedMemory;
45 UINTN AlignmentMask;
46 UINTN UnalignedPages;
47 UINTN RealPages;
48
49 //
50 // Allocate 4M aligned page for the structure and fill in the data.
51 // Ideally we would update the CRC now as well, but the service may not yet be available.
52 // See comments in the CoreUpdateDebugTableCrc32() function below for details.
53 //
54 Pages = EFI_SIZE_TO_PAGES (sizeof (EFI_SYSTEM_TABLE_POINTER));
55 AlignmentMask = SIZE_4MB - 1;
56 RealPages = Pages + EFI_SIZE_TO_PAGES (SIZE_4MB);
57
58 //
59 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
60 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
61 // MAX_ADDRESS
62 //
63 Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);
64 if (Memory == 0) {
65 Memory = MAX_ADDRESS;
66 }
67 Status = CoreAllocatePages (
68 AllocateMaxAddress,
69 EfiBootServicesData,
70 RealPages,
71 &Memory
72 );
73 if (EFI_ERROR (Status)) {
74 if (PcdGet64 (PcdMaxEfiSystemTablePointerAddress) != 0) {
75 DEBUG ((EFI_D_INFO, "Allocate memory for EFI_SYSTEM_TABLE_POINTER below PcdMaxEfiSystemTablePointerAddress failed. \
76 Retry to allocate memroy as close to the top of memory as feasible.\n"));
77 }
78 //
79 // If the initial memory allocation fails, then reattempt allocation
80 // as close to the top of memory as feasible.
81 //
82 Status = CoreAllocatePages (
83 AllocateAnyPages,
84 EfiBootServicesData,
85 RealPages,
86 &Memory
87 );
88 ASSERT_EFI_ERROR (Status);
89 if (EFI_ERROR (Status)) {
90 return;
91 }
92 }
93
94 //
95 // Free overallocated pages
96 //
97 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
98 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);
99 if (UnalignedPages > 0) {
100 //
101 // Free first unaligned page(s).
102 //
103 Status = CoreFreePages (Memory, UnalignedPages);
104 ASSERT_EFI_ERROR (Status);
105 }
106 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
107 UnalignedPages = RealPages - Pages - UnalignedPages;
108 if (UnalignedPages > 0) {
109 //
110 // Free last unaligned page(s).
111 //
112 Status = CoreFreePages (Memory, UnalignedPages);
113 ASSERT_EFI_ERROR (Status);
114 }
115
116 //
117 // Set mDebugTable to the 4MB aligned allocated pages
118 //
119 mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(AlignedMemory);
120 ASSERT (mDebugTable != NULL);
121
122 //
123 // Initialize EFI_SYSTEM_TABLE_POINTER structure
124 //
125 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;
126 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) gDxeCoreST;
127 mDebugTable->Crc32 = 0;
128
129 //
130 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
131 // Configuration Table
132 //
133 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);
134 ASSERT_EFI_ERROR (Status);
135 }
136
137
138 /**
139 Update the CRC32 in the Debug Table.
140 Since the CRC32 service is made available by the Runtime driver, we have to
141 wait for the Runtime Driver to be installed before the CRC32 can be computed.
142 This function is called elsewhere by the core when the runtime architectural
143 protocol is produced.
144
145 **/
146 VOID
147 CoreUpdateDebugTableCrc32 (
148 VOID
149 )
150 {
151 ASSERT(mDebugTable != NULL);
152 mDebugTable->Crc32 = 0;
153 gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);
154 }
155
156
157 /**
158 Adds a new DebugImageInfo structure to the DebugImageInfo Table. Re-Allocates
159 the table if it's not large enough to accomidate another entry.
160
161 @param ImageInfoType type of debug image information
162 @param LoadedImage pointer to the loaded image protocol for the image being
163 loaded
164 @param ImageHandle image handle for the image being loaded
165
166 **/
167 VOID
168 CoreNewDebugImageInfoEntry (
169 IN UINT32 ImageInfoType,
170 IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,
171 IN EFI_HANDLE ImageHandle
172 )
173 {
174 EFI_DEBUG_IMAGE_INFO *Table;
175 EFI_DEBUG_IMAGE_INFO *NewTable;
176 UINTN Index;
177 UINTN TableSize;
178
179 //
180 // Set the flag indicating that we're in the process of updating the table.
181 //
182 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
183
184 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
185
186 if (mDebugInfoTableHeader.TableSize < mMaxTableEntries) {
187 //
188 // We still have empty entires in the Table, find the first empty entry.
189 //
190 Index = 0;
191 while (Table[Index].NormalImage != NULL) {
192 Index++;
193 }
194 //
195 // There must be an empty entry in the in the table.
196 //
197 ASSERT (Index < mMaxTableEntries);
198 } else {
199 //
200 // Table is full, so re-allocate another page for a larger table...
201 //
202 TableSize = mMaxTableEntries * EFI_DEBUG_TABLE_ENTRY_SIZE;
203 NewTable = AllocateZeroPool (TableSize + EFI_PAGE_SIZE);
204 if (NewTable == NULL) {
205 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
206 return;
207 }
208 //
209 // Copy the old table into the new one
210 //
211 CopyMem (NewTable, Table, TableSize);
212 //
213 // Free the old table
214 //
215 CoreFreePool (Table);
216 //
217 // Update the table header
218 //
219 Table = NewTable;
220 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;
221 //
222 // Enlarge the max table entries and set the first empty entry index to
223 // be the original max table entries.
224 //
225 Index = mMaxTableEntries;
226 mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
227 }
228
229 //
230 // Allocate data for new entry
231 //
232 Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));
233 if (Table[Index].NormalImage != NULL) {
234 //
235 // Update the entry
236 //
237 Table[Index].NormalImage->ImageInfoType = (UINT32) ImageInfoType;
238 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;
239 Table[Index].NormalImage->ImageHandle = ImageHandle;
240 //
241 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
242 //
243 mDebugInfoTableHeader.TableSize++;
244 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
245 }
246 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
247 }
248
249
250
251 /**
252 Removes and frees an entry from the DebugImageInfo Table.
253
254 @param ImageHandle image handle for the image being unloaded
255
256 **/
257 VOID
258 CoreRemoveDebugImageInfoEntry (
259 EFI_HANDLE ImageHandle
260 )
261 {
262 EFI_DEBUG_IMAGE_INFO *Table;
263 UINTN Index;
264
265 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
266
267 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
268
269 for (Index = 0; Index < mMaxTableEntries; Index++) {
270 if (Table[Index].NormalImage != NULL && Table[Index].NormalImage->ImageHandle == ImageHandle) {
271 //
272 // Found a match. Free up the record, then NULL the pointer to indicate the slot
273 // is free.
274 //
275 CoreFreePool (Table[Index].NormalImage);
276 Table[Index].NormalImage = NULL;
277 //
278 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
279 //
280 mDebugInfoTableHeader.TableSize--;
281 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
282 break;
283 }
284 }
285 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
286 }
287
288