]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 EFI_DEBUG_IMAGE_INFO_TABLE_HEADER mDebugInfoTableHeader = {
13 0, // volatile UINT32 UpdateStatus;
14 0, // UINT32 TableSize;
15 NULL // EFI_DEBUG_IMAGE_INFO *EfiDebugImageInfoTable;
16 };
17
18 UINTN mMaxTableEntries = 0;
19
20 EFI_SYSTEM_TABLE_POINTER *mDebugTable = NULL;
21
22 #define EFI_DEBUG_TABLE_ENTRY_SIZE (sizeof (VOID *))
23
24 /**
25 Creates and initializes the DebugImageInfo Table. Also creates the configuration
26 table and registers it into the system table.
27
28 **/
29 VOID
30 CoreInitializeDebugImageInfoTable (
31 VOID
32 )
33 {
34 EFI_STATUS Status;
35 UINTN Pages;
36 EFI_PHYSICAL_ADDRESS Memory;
37 UINTN AlignedMemory;
38 UINTN AlignmentMask;
39 UINTN UnalignedPages;
40 UINTN RealPages;
41
42 //
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.
46 //
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);
50
51 //
52 // Attempt to allocate memory below PcdMaxEfiSystemTablePointerAddress
53 // If PcdMaxEfiSystemTablePointerAddress is 0, then allocate memory below
54 // MAX_ADDRESS
55 //
56 Memory = PcdGet64 (PcdMaxEfiSystemTablePointerAddress);
57 if (Memory == 0) {
58 Memory = MAX_ADDRESS;
59 }
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 ((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"));
71 }
72
73 //
74 // If the initial memory allocation fails, then reattempt allocation
75 // as close to the top of memory as feasible.
76 //
77 Status = CoreAllocatePages (
78 AllocateAnyPages,
79 EfiBootServicesData,
80 RealPages,
81 &Memory
82 );
83 ASSERT_EFI_ERROR (Status);
84 if (EFI_ERROR (Status)) {
85 return;
86 }
87 }
88
89 //
90 // Free overallocated pages
91 //
92 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;
93 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);
94 if (UnalignedPages > 0) {
95 //
96 // Free first unaligned page(s).
97 //
98 Status = CoreFreePages (Memory, UnalignedPages);
99 ASSERT_EFI_ERROR (Status);
100 }
101
102 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
103 UnalignedPages = RealPages - Pages - UnalignedPages;
104 if (UnalignedPages > 0) {
105 //
106 // Free last unaligned page(s).
107 //
108 Status = CoreFreePages (Memory, UnalignedPages);
109 ASSERT_EFI_ERROR (Status);
110 }
111
112 //
113 // Set mDebugTable to the 4MB aligned allocated pages
114 //
115 mDebugTable = (EFI_SYSTEM_TABLE_POINTER *)(AlignedMemory);
116 ASSERT (mDebugTable != NULL);
117
118 //
119 // Initialize EFI_SYSTEM_TABLE_POINTER structure
120 //
121 mDebugTable->Signature = EFI_SYSTEM_TABLE_SIGNATURE;
122 mDebugTable->EfiSystemTableBase = (EFI_PHYSICAL_ADDRESS)(UINTN)gDxeCoreST;
123 mDebugTable->Crc32 = 0;
124
125 //
126 // Install the EFI_SYSTEM_TABLE_POINTER structure in the EFI System
127 // Configuration Table
128 //
129 Status = CoreInstallConfigurationTable (&gEfiDebugImageInfoTableGuid, &mDebugInfoTableHeader);
130 ASSERT_EFI_ERROR (Status);
131 }
132
133 /**
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.
139
140 **/
141 VOID
142 CoreUpdateDebugTableCrc32 (
143 VOID
144 )
145 {
146 ASSERT (mDebugTable != NULL);
147 mDebugTable->Crc32 = 0;
148 gBS->CalculateCrc32 ((VOID *)mDebugTable, sizeof (EFI_SYSTEM_TABLE_POINTER), &mDebugTable->Crc32);
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 //
190 // There must be an empty entry in the in the table.
191 //
192 ASSERT (Index < mMaxTableEntries);
193 } else {
194 //
195 // Table is full, so re-allocate another page for a larger table...
196 //
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;
201 return;
202 }
203
204 //
205 // Copy the old table into the new one
206 //
207 CopyMem (NewTable, Table, TableSize);
208 //
209 // Free the old table
210 //
211 CoreFreePool (Table);
212 //
213 // Update the table header
214 //
215 Table = NewTable;
216 mDebugInfoTableHeader.EfiDebugImageInfoTable = NewTable;
217 //
218 // Enlarge the max table entries and set the first empty entry index to
219 // be the original max table entries.
220 //
221 Index = mMaxTableEntries;
222 mMaxTableEntries += EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;
223 }
224
225 //
226 // Allocate data for new entry
227 //
228 Table[Index].NormalImage = AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));
229 if (Table[Index].NormalImage != NULL) {
230 //
231 // Update the entry
232 //
233 Table[Index].NormalImage->ImageInfoType = (UINT32)ImageInfoType;
234 Table[Index].NormalImage->LoadedImageProtocolInstance = LoadedImage;
235 Table[Index].NormalImage->ImageHandle = ImageHandle;
236 //
237 // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
238 //
239 mDebugInfoTableHeader.TableSize++;
240 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
241 }
242
243 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
244 }
245
246 /**
247 Removes and frees an entry from the DebugImageInfo Table.
248
249 @param ImageHandle image handle for the image being unloaded
250
251 **/
252 VOID
253 CoreRemoveDebugImageInfoEntry (
254 EFI_HANDLE ImageHandle
255 )
256 {
257 EFI_DEBUG_IMAGE_INFO *Table;
258 UINTN Index;
259
260 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
261
262 Table = mDebugInfoTableHeader.EfiDebugImageInfoTable;
263
264 for (Index = 0; Index < mMaxTableEntries; Index++) {
265 if ((Table[Index].NormalImage != NULL) && (Table[Index].NormalImage->ImageHandle == ImageHandle)) {
266 //
267 // Found a match. Free up the record, then NULL the pointer to indicate the slot
268 // is free.
269 //
270 CoreFreePool (Table[Index].NormalImage);
271 Table[Index].NormalImage = NULL;
272 //
273 // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the mDebugInfoTable in modified status.
274 //
275 mDebugInfoTableHeader.TableSize--;
276 mDebugInfoTableHeader.UpdateStatus |= EFI_DEBUG_IMAGE_INFO_TABLE_MODIFIED;
277 break;
278 }
279 }
280
281 mDebugInfoTableHeader.UpdateStatus &= ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_PROGRESS;
282 }