]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Application/MemoryProfileInfo/MemoryProfileInfo.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Application / MemoryProfileInfo / MemoryProfileInfo.c
CommitLineData
84edd20b 1/** @file\r
c5155f29 2\r
a0284a9a 3 Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>\r
9d510e61 4 SPDX-License-Identifier: BSD-2-Clause-Patent\r
84edd20b
SZ
5\r
6**/\r
7\r
8#include <Uefi.h>\r
9#include <PiDxe.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/BaseMemoryLib.h>\r
12#include <Library/MemoryAllocationLib.h>\r
13#include <Library/UefiLib.h>\r
14#include <Library/UefiApplicationEntryPoint.h>\r
15#include <Library/UefiBootServicesTableLib.h>\r
16#include <Library/UefiRuntimeServicesTableLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/DxeServicesLib.h>\r
84edd20b
SZ
19#include <Library/PrintLib.h>\r
20\r
21#include <Protocol/SmmCommunication.h>\r
22#include <Protocol/SmmAccess2.h>\r
23\r
84edd20b 24#include <Guid/MemoryProfile.h>\r
2f7961c7 25#include <Guid/PiSmmCommunicationRegionTable.h>\r
84edd20b 26\r
1436aea4 27CHAR8 *mActionString[] = {\r
c5155f29
SZ
28 "Unknown",\r
29 "gBS->AllocatePages",\r
30 "gBS->FreePages",\r
31 "gBS->AllocatePool",\r
32 "gBS->FreePool",\r
33};\r
34\r
1436aea4 35CHAR8 *mSmmActionString[] = {\r
c5155f29
SZ
36 "SmmUnknown",\r
37 "gSmst->SmmAllocatePages",\r
38 "gSmst->SmmFreePages",\r
39 "gSmst->SmmAllocatePool",\r
40 "gSmst->SmmFreePool",\r
41};\r
42\r
43typedef struct {\r
1436aea4
MK
44 MEMORY_PROFILE_ACTION Action;\r
45 CHAR8 *String;\r
c5155f29
SZ
46} ACTION_STRING;\r
47\r
1436aea4
MK
48ACTION_STRING mExtActionString[] = {\r
49 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES, "Lib:AllocatePages" },\r
50 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES, "Lib:AllocateRuntimePages" },\r
51 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES, "Lib:AllocateReservedPages" },\r
52 { MEMORY_PROFILE_ACTION_LIB_FREE_PAGES, "Lib:FreePages" },\r
53 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES, "Lib:AllocateAlignedPages" },\r
54 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES, "Lib:AllocateAlignedRuntimePages" },\r
55 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES, "Lib:AllocateAlignedReservedPages" },\r
56 { MEMORY_PROFILE_ACTION_LIB_FREE_ALIGNED_PAGES, "Lib:FreeAlignedPages" },\r
57 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL, "Lib:AllocatePool" },\r
58 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL, "Lib:AllocateRuntimePool" },\r
59 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL, "Lib:AllocateReservedPool" },\r
60 { MEMORY_PROFILE_ACTION_LIB_FREE_POOL, "Lib:FreePool" },\r
61 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL, "Lib:AllocateZeroPool" },\r
62 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL, "Lib:AllocateRuntimeZeroPool" },\r
63 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL, "Lib:AllocateReservedZeroPool" },\r
64 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL, "Lib:AllocateCopyPool" },\r
65 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL, "Lib:AllocateRuntimeCopyPool" },\r
66 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL, "Lib:AllocateReservedCopyPool" },\r
67 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL, "Lib:ReallocatePool" },\r
68 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL, "Lib:ReallocateRuntimePool" },\r
69 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL, "Lib:ReallocateReservedPool" },\r
84edd20b
SZ
70};\r
71\r
1436aea4 72CHAR8 mUserDefinedActionString[] = { "UserDefined-0x80000000" };\r
c5155f29 73\r
1436aea4 74CHAR8 *mMemoryTypeString[] = {\r
c5155f29
SZ
75 "EfiReservedMemoryType",\r
76 "EfiLoaderCode",\r
77 "EfiLoaderData",\r
78 "EfiBootServicesCode",\r
79 "EfiBootServicesData",\r
80 "EfiRuntimeServicesCode",\r
81 "EfiRuntimeServicesData",\r
82 "EfiConventionalMemory",\r
83 "EfiUnusableMemory",\r
84 "EfiACPIReclaimMemory",\r
85 "EfiACPIMemoryNVS",\r
86 "EfiMemoryMappedIO",\r
87 "EfiMemoryMappedIOPortSpace",\r
88 "EfiPalCode",\r
89 "EfiPersistentMemory",\r
90 "EfiOSReserved",\r
91 "EfiOemReserved",\r
84edd20b
SZ
92};\r
93\r
1436aea4 94CHAR8 *mSubsystemString[] = {\r
c5155f29
SZ
95 "Unknown",\r
96 "NATIVE",\r
97 "WINDOWS_GUI",\r
98 "WINDOWS_CUI",\r
99 "Unknown",\r
100 "Unknown",\r
101 "Unknown",\r
102 "POSIX_CUI",\r
103 "Unknown",\r
104 "WINDOWS_CE_GUI",\r
105 "EFI_APPLICATION",\r
106 "EFI_BOOT_SERVICE_DRIVER",\r
107 "EFI_RUNTIME_DRIVER",\r
108 "EFI_ROM",\r
109 "XBOX",\r
110 "Unknown",\r
84edd20b
SZ
111};\r
112\r
1436aea4 113CHAR8 *mFileTypeString[] = {\r
c5155f29
SZ
114 "Unknown",\r
115 "RAW",\r
116 "FREEFORM",\r
117 "SECURITY_CORE",\r
118 "PEI_CORE",\r
119 "DXE_CORE",\r
120 "PEIM",\r
121 "DRIVER",\r
122 "COMBINED_PEIM_DRIVER",\r
123 "APPLICATION",\r
124 "SMM",\r
125 "FIRMWARE_VOLUME_IMAGE",\r
126 "COMBINED_SMM_DXE",\r
127 "SMM_CORE",\r
84edd20b
SZ
128};\r
129\r
c5155f29 130#define PROFILE_NAME_STRING_LENGTH 64\r
1436aea4 131CHAR8 mNameString[PROFILE_NAME_STRING_LENGTH + 1];\r
c5155f29
SZ
132\r
133//\r
134// Profile summary information\r
135//\r
1436aea4
MK
136#define MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE SIGNATURE_32 ('M','P','A','S')\r
137#define MEMORY_PROFILE_ALLOC_SUMMARY_INFO_REVISION 0x0001\r
c5155f29
SZ
138\r
139typedef struct {\r
1436aea4
MK
140 MEMORY_PROFILE_COMMON_HEADER Header;\r
141 PHYSICAL_ADDRESS CallerAddress;\r
142 MEMORY_PROFILE_ACTION Action;\r
143 CHAR8 *ActionString;\r
144 UINT32 AllocateCount;\r
145 UINT64 TotalSize;\r
c5155f29
SZ
146} MEMORY_PROFILE_ALLOC_SUMMARY_INFO;\r
147\r
148typedef struct {\r
1436aea4
MK
149 UINT32 Signature;\r
150 MEMORY_PROFILE_ALLOC_SUMMARY_INFO AllocSummaryInfo;\r
151 LIST_ENTRY Link;\r
c5155f29
SZ
152} MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA;\r
153\r
154typedef struct {\r
155 UINT32 Signature;\r
156 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;\r
157 LIST_ENTRY *AllocSummaryInfoList;\r
158 LIST_ENTRY Link;\r
159} MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA;\r
160\r
161typedef struct {\r
1436aea4
MK
162 UINT32 Signature;\r
163 MEMORY_PROFILE_CONTEXT *Context;\r
164 LIST_ENTRY *DriverSummaryInfoList;\r
c5155f29 165} MEMORY_PROFILE_CONTEXT_SUMMARY_DATA;\r
84edd20b 166\r
1436aea4
MK
167LIST_ENTRY mImageSummaryQueue = INITIALIZE_LIST_HEAD_VARIABLE (mImageSummaryQueue);\r
168MEMORY_PROFILE_CONTEXT_SUMMARY_DATA mMemoryProfileContextSummary;\r
c5155f29
SZ
169\r
170/**\r
84edd20b 171 Get the file name portion of the Pdb File Name.\r
c5155f29 172\r
84edd20b 173 The portion of the Pdb File Name between the last backslash and\r
c5155f29
SZ
174 either a following period or the end of the string is copied into\r
175 AsciiBuffer. The name is truncated, if necessary, to ensure that\r
176 AsciiBuffer is not overrun.\r
177\r
84edd20b 178 @param[in] PdbFileName Pdb file name.\r
c5155f29
SZ
179 @param[out] AsciiBuffer The resultant Ascii File Name.\r
180\r
84edd20b
SZ
181**/\r
182VOID\r
183GetShortPdbFileName (\r
1436aea4
MK
184 IN CHAR8 *PdbFileName,\r
185 OUT CHAR8 *AsciiBuffer\r
84edd20b
SZ
186 )\r
187{\r
1436aea4
MK
188 UINTN IndexPdb; // Current work location within a Pdb string.\r
189 UINTN IndexBuffer; // Current work location within a Buffer string.\r
190 UINTN StartIndex;\r
191 UINTN EndIndex;\r
84edd20b 192\r
c5155f29 193 ZeroMem (AsciiBuffer, PROFILE_NAME_STRING_LENGTH + 1);\r
84edd20b
SZ
194\r
195 if (PdbFileName == NULL) {\r
c5155f29 196 AsciiStrnCpyS (AsciiBuffer, PROFILE_NAME_STRING_LENGTH + 1, " ", 1);\r
84edd20b
SZ
197 } else {\r
198 StartIndex = 0;\r
1436aea4
MK
199 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++) {\r
200 }\r
201\r
c5155f29 202 for (IndexPdb = 0; PdbFileName[IndexPdb] != 0; IndexPdb++) {\r
3d33cb1e 203 if ((PdbFileName[IndexPdb] == '\\') || (PdbFileName[IndexPdb] == '/')) {\r
c5155f29 204 StartIndex = IndexPdb + 1;\r
84edd20b
SZ
205 }\r
206\r
c5155f29
SZ
207 if (PdbFileName[IndexPdb] == '.') {\r
208 EndIndex = IndexPdb;\r
84edd20b
SZ
209 }\r
210 }\r
211\r
c5155f29
SZ
212 IndexBuffer = 0;\r
213 for (IndexPdb = StartIndex; IndexPdb < EndIndex; IndexPdb++) {\r
214 AsciiBuffer[IndexBuffer] = PdbFileName[IndexPdb];\r
215 IndexBuffer++;\r
216 if (IndexBuffer >= PROFILE_NAME_STRING_LENGTH) {\r
217 AsciiBuffer[PROFILE_NAME_STRING_LENGTH] = 0;\r
84edd20b
SZ
218 break;\r
219 }\r
220 }\r
221 }\r
222}\r
223\r
c5155f29 224/**\r
84edd20b
SZ
225 Get a human readable name for an image.\r
226 The following methods will be tried orderly:\r
227 1. Image PDB\r
228 2. FFS UI section\r
229 3. Image GUID\r
230\r
231 @param[in] DriverInfo Pointer to memory profile driver info.\r
232\r
c5155f29 233 @return The resulting Ascii name string is stored in the mNameString global array.\r
84edd20b
SZ
234\r
235**/\r
c5155f29 236CHAR8 *\r
84edd20b 237GetDriverNameString (\r
1436aea4
MK
238 IN MEMORY_PROFILE_DRIVER_INFO *DriverInfo\r
239 )\r
84edd20b 240{\r
1436aea4
MK
241 EFI_STATUS Status;\r
242 CHAR16 *NameString;\r
243 UINTN StringSize;\r
84edd20b
SZ
244\r
245 //\r
246 // Method 1: Get the name string from image PDB\r
247 //\r
a0284a9a 248 if (DriverInfo->PdbStringOffset != 0) {\r
1436aea4 249 GetShortPdbFileName ((CHAR8 *)((UINTN)DriverInfo + DriverInfo->PdbStringOffset), mNameString);\r
c5155f29 250 return mNameString;\r
84edd20b
SZ
251 }\r
252\r
39cde03c 253 if (!IsZeroGuid (&DriverInfo->FileName)) {\r
84edd20b
SZ
254 //\r
255 // Try to get the image's FFS UI section by image GUID\r
256 //\r
257 NameString = NULL;\r
258 StringSize = 0;\r
1436aea4
MK
259 Status = GetSectionFromAnyFv (\r
260 &DriverInfo->FileName,\r
261 EFI_SECTION_USER_INTERFACE,\r
262 0,\r
263 (VOID **)&NameString,\r
264 &StringSize\r
265 );\r
84edd20b
SZ
266 if (!EFI_ERROR (Status)) {\r
267 //\r
268 // Method 2: Get the name string from FFS UI section\r
269 //\r
c5155f29
SZ
270 if (StrLen (NameString) > PROFILE_NAME_STRING_LENGTH) {\r
271 NameString[PROFILE_NAME_STRING_LENGTH] = 0;\r
272 }\r
1436aea4 273\r
c5155f29 274 UnicodeStrToAsciiStrS (NameString, mNameString, sizeof (mNameString));\r
84edd20b 275 FreePool (NameString);\r
c5155f29 276 return mNameString;\r
84edd20b
SZ
277 }\r
278 }\r
279\r
280 //\r
281 // Method 3: Get the name string from image GUID\r
282 //\r
c5155f29
SZ
283 AsciiSPrint (mNameString, sizeof (mNameString), "%g", &DriverInfo->FileName);\r
284 return mNameString;\r
84edd20b
SZ
285}\r
286\r
db9b00f1
SZ
287/**\r
288 Memory type to string.\r
289\r
290 @param[in] MemoryType Memory type.\r
291\r
292 @return Pointer to string.\r
293\r
294**/\r
c5155f29 295CHAR8 *\r
db9b00f1 296ProfileMemoryTypeToStr (\r
1436aea4 297 IN EFI_MEMORY_TYPE MemoryType\r
db9b00f1
SZ
298 )\r
299{\r
1436aea4 300 UINTN Index;\r
db9b00f1 301\r
1436aea4 302 if ((UINT32)MemoryType >= 0x80000000) {\r
db9b00f1
SZ
303 //\r
304 // OS reserved memory type.\r
305 //\r
306 Index = EfiMaxMemoryType;\r
1436aea4 307 } else if ((UINT32)MemoryType >= 0x70000000) {\r
db9b00f1
SZ
308 //\r
309 // OEM reserved memory type.\r
310 //\r
311 Index = EfiMaxMemoryType + 1;\r
312 } else {\r
313 Index = MemoryType;\r
314 }\r
315\r
316 return mMemoryTypeString[Index];\r
317}\r
318\r
c5155f29
SZ
319/**\r
320 Action to string.\r
321\r
322 @param[in] Action Profile action.\r
323 @param[in] UserDefinedActionString Pointer to user defined action string.\r
324 @param[in] IsForSmm TRUE - SMRAM profile.\r
325 FALSE - UEFI memory profile.\r
326\r
327 @return Pointer to string.\r
328\r
329**/\r
330CHAR8 *\r
331ProfileActionToStr (\r
332 IN MEMORY_PROFILE_ACTION Action,\r
333 IN CHAR8 *UserDefinedActionString,\r
334 IN BOOLEAN IsForSmm\r
335 )\r
336{\r
1436aea4
MK
337 UINTN Index;\r
338 UINTN ActionStringCount;\r
339 CHAR8 **ActionString;\r
c5155f29
SZ
340\r
341 if (IsForSmm) {\r
1436aea4 342 ActionString = mSmmActionString;\r
32223fd8 343 ActionStringCount = ARRAY_SIZE (mSmmActionString);\r
c5155f29 344 } else {\r
1436aea4 345 ActionString = mActionString;\r
32223fd8 346 ActionStringCount = ARRAY_SIZE (mActionString);\r
c5155f29
SZ
347 }\r
348\r
1436aea4 349 if ((UINTN)(UINT32)Action < ActionStringCount) {\r
c5155f29
SZ
350 return ActionString[Action];\r
351 }\r
1436aea4 352\r
32223fd8 353 for (Index = 0; Index < ARRAY_SIZE (mExtActionString); Index++) {\r
c5155f29
SZ
354 if (mExtActionString[Index].Action == Action) {\r
355 return mExtActionString[Index].String;\r
356 }\r
357 }\r
1436aea4 358\r
c5155f29
SZ
359 if ((Action & MEMORY_PROFILE_ACTION_USER_DEFINED_MASK) != 0) {\r
360 if (UserDefinedActionString != NULL) {\r
361 return UserDefinedActionString;\r
362 }\r
1436aea4 363\r
c5155f29
SZ
364 AsciiSPrint (mUserDefinedActionString, sizeof (mUserDefinedActionString), "UserDefined-0x%08x", Action);\r
365 return mUserDefinedActionString;\r
366 }\r
367\r
368 return ActionString[0];\r
369}\r
370\r
84edd20b
SZ
371/**\r
372 Dump memory profile allocate information.\r
373\r
374 @param[in] DriverInfo Pointer to memory profile driver info.\r
375 @param[in] AllocIndex Memory profile alloc info index.\r
376 @param[in] AllocInfo Pointer to memory profile alloc info.\r
c5155f29
SZ
377 @param[in] IsForSmm TRUE - SMRAM profile.\r
378 FALSE - UEFI memory profile.\r
84edd20b
SZ
379\r
380 @return Pointer to next memory profile alloc info.\r
381\r
382**/\r
383MEMORY_PROFILE_ALLOC_INFO *\r
384DumpMemoryProfileAllocInfo (\r
1436aea4
MK
385 IN MEMORY_PROFILE_DRIVER_INFO *DriverInfo,\r
386 IN UINTN AllocIndex,\r
387 IN MEMORY_PROFILE_ALLOC_INFO *AllocInfo,\r
388 IN BOOLEAN IsForSmm\r
84edd20b
SZ
389 )\r
390{\r
1436aea4 391 CHAR8 *ActionString;\r
c5155f29 392\r
84edd20b
SZ
393 if (AllocInfo->Header.Signature != MEMORY_PROFILE_ALLOC_INFO_SIGNATURE) {\r
394 return NULL;\r
395 }\r
c5155f29
SZ
396\r
397 if (AllocInfo->ActionStringOffset != 0) {\r
1436aea4 398 ActionString = (CHAR8 *)((UINTN)AllocInfo + AllocInfo->ActionStringOffset);\r
c5155f29
SZ
399 } else {\r
400 ActionString = NULL;\r
401 }\r
402\r
84edd20b
SZ
403 Print (L" MEMORY_PROFILE_ALLOC_INFO (0x%x)\n", AllocIndex);\r
404 Print (L" Signature - 0x%08x\n", AllocInfo->Header.Signature);\r
405 Print (L" Length - 0x%04x\n", AllocInfo->Header.Length);\r
c5155f29 406 Print (L" Revision - 0x%04x\n", AllocInfo->Header.Revision);\r
1436aea4 407 Print (L" CallerAddress - 0x%016lx (Offset: 0x%08x)\n", AllocInfo->CallerAddress, (UINTN)(AllocInfo->CallerAddress - DriverInfo->ImageBase));\r
84edd20b 408 Print (L" SequenceId - 0x%08x\n", AllocInfo->SequenceId);\r
c5155f29
SZ
409 Print (L" Action - 0x%08x (%a)\n", AllocInfo->Action, ProfileActionToStr (AllocInfo->Action, ActionString, IsForSmm));\r
410 Print (L" MemoryType - 0x%08x (%a)\n", AllocInfo->MemoryType, ProfileMemoryTypeToStr (AllocInfo->MemoryType));\r
84edd20b
SZ
411 Print (L" Buffer - 0x%016lx\n", AllocInfo->Buffer);\r
412 Print (L" Size - 0x%016lx\n", AllocInfo->Size);\r
413\r
1436aea4 414 return (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)AllocInfo + AllocInfo->Header.Length);\r
84edd20b
SZ
415}\r
416\r
417/**\r
418 Dump memory profile driver information.\r
419\r
420 @param[in] DriverIndex Memory profile driver info index.\r
421 @param[in] DriverInfo Pointer to memory profile driver info.\r
c5155f29
SZ
422 @param[in] IsForSmm TRUE - SMRAM profile.\r
423 FALSE - UEFI memory profile.\r
84edd20b
SZ
424\r
425 @return Pointer to next memory profile driver info.\r
426\r
427**/\r
428MEMORY_PROFILE_DRIVER_INFO *\r
429DumpMemoryProfileDriverInfo (\r
1436aea4
MK
430 IN UINTN DriverIndex,\r
431 IN MEMORY_PROFILE_DRIVER_INFO *DriverInfo,\r
432 IN BOOLEAN IsForSmm\r
84edd20b
SZ
433 )\r
434{\r
1436aea4
MK
435 UINTN TypeIndex;\r
436 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;\r
437 UINTN AllocIndex;\r
438 CHAR8 *NameString;\r
84edd20b
SZ
439\r
440 if (DriverInfo->Header.Signature != MEMORY_PROFILE_DRIVER_INFO_SIGNATURE) {\r
441 return NULL;\r
442 }\r
1436aea4 443\r
84edd20b
SZ
444 Print (L" MEMORY_PROFILE_DRIVER_INFO (0x%x)\n", DriverIndex);\r
445 Print (L" Signature - 0x%08x\n", DriverInfo->Header.Signature);\r
446 Print (L" Length - 0x%04x\n", DriverInfo->Header.Length);\r
c5155f29
SZ
447 Print (L" Revision - 0x%04x\n", DriverInfo->Header.Revision);\r
448 NameString = GetDriverNameString (DriverInfo);\r
449 Print (L" FileName - %a\n", NameString);\r
450 if (DriverInfo->PdbStringOffset != 0) {\r
1436aea4 451 Print (L" Pdb - %a\n", (CHAR8 *)((UINTN)DriverInfo + DriverInfo->PdbStringOffset));\r
c5155f29 452 }\r
1436aea4 453\r
84edd20b
SZ
454 Print (L" ImageBase - 0x%016lx\n", DriverInfo->ImageBase);\r
455 Print (L" ImageSize - 0x%016lx\n", DriverInfo->ImageSize);\r
456 Print (L" EntryPoint - 0x%016lx\n", DriverInfo->EntryPoint);\r
1436aea4
MK
457 Print (L" ImageSubsystem - 0x%04x (%a)\n", DriverInfo->ImageSubsystem, mSubsystemString[(DriverInfo->ImageSubsystem < sizeof (mSubsystemString)/sizeof (mSubsystemString[0])) ? DriverInfo->ImageSubsystem : 0]);\r
458 Print (L" FileType - 0x%02x (%a)\n", DriverInfo->FileType, mFileTypeString[(DriverInfo->FileType < sizeof (mFileTypeString)/sizeof (mFileTypeString[0])) ? DriverInfo->FileType : 0]);\r
84edd20b
SZ
459 Print (L" CurrentUsage - 0x%016lx\n", DriverInfo->CurrentUsage);\r
460 Print (L" PeakUsage - 0x%016lx\n", DriverInfo->PeakUsage);\r
db9b00f1 461 for (TypeIndex = 0; TypeIndex < sizeof (DriverInfo->CurrentUsageByType) / sizeof (DriverInfo->CurrentUsageByType[0]); TypeIndex++) {\r
84edd20b 462 if ((DriverInfo->CurrentUsageByType[TypeIndex] != 0) ||\r
1436aea4
MK
463 (DriverInfo->PeakUsageByType[TypeIndex] != 0))\r
464 {\r
c5155f29
SZ
465 Print (L" CurrentUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, DriverInfo->CurrentUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
466 Print (L" PeakUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, DriverInfo->PeakUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
84edd20b
SZ
467 }\r
468 }\r
1436aea4 469\r
84edd20b
SZ
470 Print (L" AllocRecordCount - 0x%08x\n", DriverInfo->AllocRecordCount);\r
471\r
1436aea4 472 AllocInfo = (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)DriverInfo + DriverInfo->Header.Length);\r
84edd20b 473 for (AllocIndex = 0; AllocIndex < DriverInfo->AllocRecordCount; AllocIndex++) {\r
c5155f29 474 AllocInfo = DumpMemoryProfileAllocInfo (DriverInfo, AllocIndex, AllocInfo, IsForSmm);\r
84edd20b
SZ
475 if (AllocInfo == NULL) {\r
476 return NULL;\r
477 }\r
478 }\r
1436aea4
MK
479\r
480 return (MEMORY_PROFILE_DRIVER_INFO *)AllocInfo;\r
84edd20b
SZ
481}\r
482\r
483/**\r
484 Dump memory profile context information.\r
485\r
486 @param[in] Context Pointer to memory profile context.\r
c5155f29
SZ
487 @param[in] IsForSmm TRUE - SMRAM profile.\r
488 FALSE - UEFI memory profile.\r
84edd20b
SZ
489\r
490 @return Pointer to the end of memory profile context buffer.\r
491\r
492**/\r
493VOID *\r
494DumpMemoryProfileContext (\r
1436aea4
MK
495 IN MEMORY_PROFILE_CONTEXT *Context,\r
496 IN BOOLEAN IsForSmm\r
84edd20b
SZ
497 )\r
498{\r
1436aea4
MK
499 UINTN TypeIndex;\r
500 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;\r
501 UINTN DriverIndex;\r
84edd20b
SZ
502\r
503 if (Context->Header.Signature != MEMORY_PROFILE_CONTEXT_SIGNATURE) {\r
504 return NULL;\r
505 }\r
1436aea4 506\r
84edd20b
SZ
507 Print (L"MEMORY_PROFILE_CONTEXT\n");\r
508 Print (L" Signature - 0x%08x\n", Context->Header.Signature);\r
509 Print (L" Length - 0x%04x\n", Context->Header.Length);\r
c5155f29 510 Print (L" Revision - 0x%04x\n", Context->Header.Revision);\r
84edd20b
SZ
511 Print (L" CurrentTotalUsage - 0x%016lx\n", Context->CurrentTotalUsage);\r
512 Print (L" PeakTotalUsage - 0x%016lx\n", Context->PeakTotalUsage);\r
db9b00f1 513 for (TypeIndex = 0; TypeIndex < sizeof (Context->CurrentTotalUsageByType) / sizeof (Context->CurrentTotalUsageByType[0]); TypeIndex++) {\r
84edd20b 514 if ((Context->CurrentTotalUsageByType[TypeIndex] != 0) ||\r
1436aea4
MK
515 (Context->PeakTotalUsageByType[TypeIndex] != 0))\r
516 {\r
c5155f29
SZ
517 Print (L" CurrentTotalUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, Context->CurrentTotalUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
518 Print (L" PeakTotalUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, Context->PeakTotalUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
84edd20b
SZ
519 }\r
520 }\r
1436aea4 521\r
84edd20b
SZ
522 Print (L" TotalImageSize - 0x%016lx\n", Context->TotalImageSize);\r
523 Print (L" ImageCount - 0x%08x\n", Context->ImageCount);\r
524 Print (L" SequenceCount - 0x%08x\n", Context->SequenceCount);\r
525\r
1436aea4 526 DriverInfo = (MEMORY_PROFILE_DRIVER_INFO *)((UINTN)Context + Context->Header.Length);\r
84edd20b 527 for (DriverIndex = 0; DriverIndex < Context->ImageCount; DriverIndex++) {\r
c5155f29 528 DriverInfo = DumpMemoryProfileDriverInfo (DriverIndex, DriverInfo, IsForSmm);\r
84edd20b
SZ
529 if (DriverInfo == NULL) {\r
530 return NULL;\r
531 }\r
532 }\r
1436aea4
MK
533\r
534 return (VOID *)DriverInfo;\r
84edd20b
SZ
535}\r
536\r
537/**\r
538 Dump memory profile descriptor information.\r
539\r
540 @param[in] DescriptorIndex Memory profile descriptor index.\r
541 @param[in] Descriptor Pointer to memory profile descriptor.\r
542\r
543 @return Pointer to next memory profile descriptor.\r
544\r
545**/\r
546MEMORY_PROFILE_DESCRIPTOR *\r
547DumpMemoryProfileDescriptor (\r
548 IN UINTN DescriptorIndex,\r
549 IN MEMORY_PROFILE_DESCRIPTOR *Descriptor\r
550 )\r
551{\r
552 if (Descriptor->Header.Signature != MEMORY_PROFILE_DESCRIPTOR_SIGNATURE) {\r
553 return NULL;\r
554 }\r
1436aea4 555\r
84edd20b
SZ
556 Print (L" MEMORY_PROFILE_DESCRIPTOR (0x%x)\n", DescriptorIndex);\r
557 Print (L" Signature - 0x%08x\n", Descriptor->Header.Signature);\r
558 Print (L" Length - 0x%04x\n", Descriptor->Header.Length);\r
c5155f29 559 Print (L" Revision - 0x%04x\n", Descriptor->Header.Revision);\r
84edd20b
SZ
560 Print (L" Address - 0x%016lx\n", Descriptor->Address);\r
561 Print (L" Size - 0x%016lx\n", Descriptor->Size);\r
562\r
1436aea4 563 return (MEMORY_PROFILE_DESCRIPTOR *)((UINTN)Descriptor + Descriptor->Header.Length);\r
84edd20b
SZ
564}\r
565\r
566/**\r
567 Dump memory profile free memory information.\r
568\r
569 @param[in] FreeMemory Pointer to memory profile free memory.\r
570\r
571 @return Pointer to the end of memory profile free memory buffer.\r
572\r
573**/\r
574VOID *\r
575DumpMemoryProfileFreeMemory (\r
1436aea4 576 IN MEMORY_PROFILE_FREE_MEMORY *FreeMemory\r
84edd20b
SZ
577 )\r
578{\r
1436aea4
MK
579 MEMORY_PROFILE_DESCRIPTOR *Descriptor;\r
580 UINTN DescriptorIndex;\r
84edd20b
SZ
581\r
582 if (FreeMemory->Header.Signature != MEMORY_PROFILE_FREE_MEMORY_SIGNATURE) {\r
583 return NULL;\r
584 }\r
1436aea4 585\r
84edd20b
SZ
586 Print (L"MEMORY_PROFILE_FREE_MEMORY\n");\r
587 Print (L" Signature - 0x%08x\n", FreeMemory->Header.Signature);\r
588 Print (L" Length - 0x%04x\n", FreeMemory->Header.Length);\r
c5155f29 589 Print (L" Revision - 0x%04x\n", FreeMemory->Header.Revision);\r
84edd20b
SZ
590 Print (L" TotalFreeMemoryPages - 0x%016lx\n", FreeMemory->TotalFreeMemoryPages);\r
591 Print (L" FreeMemoryEntryCount - 0x%08x\n", FreeMemory->FreeMemoryEntryCount);\r
592\r
1436aea4 593 Descriptor = (MEMORY_PROFILE_DESCRIPTOR *)((UINTN)FreeMemory + FreeMemory->Header.Length);\r
84edd20b
SZ
594 for (DescriptorIndex = 0; DescriptorIndex < FreeMemory->FreeMemoryEntryCount; DescriptorIndex++) {\r
595 Descriptor = DumpMemoryProfileDescriptor (DescriptorIndex, Descriptor);\r
596 if (Descriptor == NULL) {\r
597 return NULL;\r
598 }\r
599 }\r
600\r
1436aea4 601 return (VOID *)Descriptor;\r
84edd20b
SZ
602}\r
603\r
604/**\r
605 Dump memory profile memory range information.\r
606\r
607 @param[in] MemoryRange Pointer to memory profile memory range.\r
608\r
609 @return Pointer to the end of memory profile memory range buffer.\r
610\r
611**/\r
612VOID *\r
613DumpMemoryProfileMemoryRange (\r
1436aea4 614 IN MEMORY_PROFILE_MEMORY_RANGE *MemoryRange\r
84edd20b
SZ
615 )\r
616{\r
1436aea4
MK
617 MEMORY_PROFILE_DESCRIPTOR *Descriptor;\r
618 UINTN DescriptorIndex;\r
84edd20b
SZ
619\r
620 if (MemoryRange->Header.Signature != MEMORY_PROFILE_MEMORY_RANGE_SIGNATURE) {\r
621 return NULL;\r
622 }\r
1436aea4 623\r
84edd20b
SZ
624 Print (L"MEMORY_PROFILE_MEMORY_RANGE\n");\r
625 Print (L" Signature - 0x%08x\n", MemoryRange->Header.Signature);\r
626 Print (L" Length - 0x%04x\n", MemoryRange->Header.Length);\r
c5155f29 627 Print (L" Revision - 0x%04x\n", MemoryRange->Header.Revision);\r
84edd20b
SZ
628 Print (L" MemoryRangeCount - 0x%08x\n", MemoryRange->MemoryRangeCount);\r
629\r
1436aea4 630 Descriptor = (MEMORY_PROFILE_DESCRIPTOR *)((UINTN)MemoryRange + MemoryRange->Header.Length);\r
84edd20b
SZ
631 for (DescriptorIndex = 0; DescriptorIndex < MemoryRange->MemoryRangeCount; DescriptorIndex++) {\r
632 Descriptor = DumpMemoryProfileDescriptor (DescriptorIndex, Descriptor);\r
633 if (Descriptor == NULL) {\r
634 return NULL;\r
635 }\r
636 }\r
637\r
1436aea4 638 return (VOID *)Descriptor;\r
84edd20b
SZ
639}\r
640\r
641/**\r
642 Scan memory profile by Signature.\r
643\r
644 @param[in] ProfileBuffer Memory profile base address.\r
645 @param[in] ProfileSize Memory profile size.\r
646 @param[in] Signature Signature.\r
647\r
02d7b797 648 @return Pointer to the structure with the signature.\r
84edd20b
SZ
649\r
650**/\r
651VOID *\r
652ScanMemoryProfileBySignature (\r
1436aea4
MK
653 IN PHYSICAL_ADDRESS ProfileBuffer,\r
654 IN UINT64 ProfileSize,\r
655 IN UINT32 Signature\r
84edd20b
SZ
656 )\r
657{\r
658 MEMORY_PROFILE_COMMON_HEADER *CommonHeader;\r
1436aea4 659 UINTN ProfileEnd;\r
84edd20b 660\r
1436aea4
MK
661 ProfileEnd = (UINTN)(ProfileBuffer + ProfileSize);\r
662 CommonHeader = (MEMORY_PROFILE_COMMON_HEADER *)(UINTN)ProfileBuffer;\r
663 while ((UINTN)CommonHeader < ProfileEnd) {\r
84edd20b
SZ
664 if (CommonHeader->Signature == Signature) {\r
665 //\r
666 // Found it.\r
667 //\r
1436aea4 668 return (VOID *)CommonHeader;\r
84edd20b 669 }\r
1436aea4 670\r
c5155f29
SZ
671 if (CommonHeader->Length == 0) {\r
672 ASSERT (FALSE);\r
673 return NULL;\r
674 }\r
1436aea4
MK
675\r
676 CommonHeader = (MEMORY_PROFILE_COMMON_HEADER *)((UINTN)CommonHeader + CommonHeader->Length);\r
84edd20b
SZ
677 }\r
678\r
679 return NULL;\r
680}\r
681\r
682/**\r
683 Dump memory profile information.\r
684\r
685 @param[in] ProfileBuffer Memory profile base address.\r
686 @param[in] ProfileSize Memory profile size.\r
c5155f29
SZ
687 @param[in] IsForSmm TRUE - SMRAM profile.\r
688 FALSE - UEFI memory profile.\r
84edd20b
SZ
689\r
690**/\r
691VOID\r
692DumpMemoryProfile (\r
1436aea4
MK
693 IN PHYSICAL_ADDRESS ProfileBuffer,\r
694 IN UINT64 ProfileSize,\r
695 IN BOOLEAN IsForSmm\r
84edd20b
SZ
696 )\r
697{\r
1436aea4
MK
698 MEMORY_PROFILE_CONTEXT *Context;\r
699 MEMORY_PROFILE_FREE_MEMORY *FreeMemory;\r
700 MEMORY_PROFILE_MEMORY_RANGE *MemoryRange;\r
84edd20b 701\r
1436aea4 702 Context = (MEMORY_PROFILE_CONTEXT *)ScanMemoryProfileBySignature (ProfileBuffer, ProfileSize, MEMORY_PROFILE_CONTEXT_SIGNATURE);\r
84edd20b 703 if (Context != NULL) {\r
c5155f29 704 DumpMemoryProfileContext (Context, IsForSmm);\r
84edd20b
SZ
705 }\r
706\r
1436aea4 707 FreeMemory = (MEMORY_PROFILE_FREE_MEMORY *)ScanMemoryProfileBySignature (ProfileBuffer, ProfileSize, MEMORY_PROFILE_FREE_MEMORY_SIGNATURE);\r
84edd20b
SZ
708 if (FreeMemory != NULL) {\r
709 DumpMemoryProfileFreeMemory (FreeMemory);\r
710 }\r
711\r
1436aea4 712 MemoryRange = (MEMORY_PROFILE_MEMORY_RANGE *)ScanMemoryProfileBySignature (ProfileBuffer, ProfileSize, MEMORY_PROFILE_MEMORY_RANGE_SIGNATURE);\r
84edd20b
SZ
713 if (MemoryRange != NULL) {\r
714 DumpMemoryProfileMemoryRange (MemoryRange);\r
715 }\r
716}\r
717\r
c5155f29
SZ
718/**\r
719 Get Allocate summary information structure by caller address.\r
720\r
721 @param[in] CallerAddress Caller address.\r
722 @param[in] DriverSummaryInfoData Driver summary information data structure.\r
723\r
724 @return Allocate summary information structure by caller address.\r
725\r
726**/\r
727MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA *\r
728GetAllocSummaryInfoByCallerAddress (\r
1436aea4
MK
729 IN PHYSICAL_ADDRESS CallerAddress,\r
730 IN MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA *DriverSummaryInfoData\r
c5155f29
SZ
731 )\r
732{\r
1436aea4
MK
733 LIST_ENTRY *AllocSummaryInfoList;\r
734 LIST_ENTRY *AllocSummaryLink;\r
735 MEMORY_PROFILE_ALLOC_SUMMARY_INFO *AllocSummaryInfo;\r
736 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA *AllocSummaryInfoData;\r
c5155f29
SZ
737\r
738 AllocSummaryInfoList = DriverSummaryInfoData->AllocSummaryInfoList;\r
739\r
740 for (AllocSummaryLink = AllocSummaryInfoList->ForwardLink;\r
741 AllocSummaryLink != AllocSummaryInfoList;\r
1436aea4
MK
742 AllocSummaryLink = AllocSummaryLink->ForwardLink)\r
743 {\r
c5155f29
SZ
744 AllocSummaryInfoData = CR (\r
745 AllocSummaryLink,\r
746 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA,\r
747 Link,\r
748 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE\r
749 );\r
750 AllocSummaryInfo = &AllocSummaryInfoData->AllocSummaryInfo;\r
751 if (AllocSummaryInfo->CallerAddress == CallerAddress) {\r
752 return AllocSummaryInfoData;\r
753 }\r
754 }\r
1436aea4 755\r
c5155f29
SZ
756 return NULL;\r
757}\r
758\r
759/**\r
760 Create Allocate summary information structure and\r
761 link to Driver summary information data structure.\r
762\r
763 @param[in, out] DriverSummaryInfoData Driver summary information data structure.\r
764 @param[in] AllocInfo Pointer to memory profile alloc info.\r
765\r
766 @return Pointer to next memory profile alloc info.\r
767\r
768**/\r
769MEMORY_PROFILE_ALLOC_INFO *\r
770CreateAllocSummaryInfo (\r
1436aea4
MK
771 IN OUT MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA *DriverSummaryInfoData,\r
772 IN MEMORY_PROFILE_ALLOC_INFO *AllocInfo\r
c5155f29
SZ
773 )\r
774{\r
1436aea4
MK
775 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA *AllocSummaryInfoData;\r
776 MEMORY_PROFILE_ALLOC_SUMMARY_INFO *AllocSummaryInfo;\r
c5155f29
SZ
777\r
778 if (AllocInfo->Header.Signature != MEMORY_PROFILE_ALLOC_INFO_SIGNATURE) {\r
779 return NULL;\r
780 }\r
781\r
782 AllocSummaryInfoData = GetAllocSummaryInfoByCallerAddress (AllocInfo->CallerAddress, DriverSummaryInfoData);\r
783 if (AllocSummaryInfoData == NULL) {\r
784 AllocSummaryInfoData = AllocatePool (sizeof (*AllocSummaryInfoData));\r
785 if (AllocSummaryInfoData == NULL) {\r
786 return NULL;\r
787 }\r
788\r
1436aea4
MK
789 AllocSummaryInfoData->Signature = MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE;\r
790 AllocSummaryInfo = &AllocSummaryInfoData->AllocSummaryInfo;\r
c5155f29
SZ
791 AllocSummaryInfo->Header.Signature = MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE;\r
792 AllocSummaryInfo->Header.Length = sizeof (*AllocSummaryInfo);\r
793 AllocSummaryInfo->Header.Revision = MEMORY_PROFILE_ALLOC_SUMMARY_INFO_REVISION;\r
1436aea4
MK
794 AllocSummaryInfo->CallerAddress = AllocInfo->CallerAddress;\r
795 AllocSummaryInfo->Action = AllocInfo->Action;\r
c5155f29 796 if (AllocInfo->ActionStringOffset != 0) {\r
1436aea4 797 AllocSummaryInfo->ActionString = (CHAR8 *)((UINTN)AllocInfo + AllocInfo->ActionStringOffset);\r
c5155f29 798 } else {\r
1436aea4 799 AllocSummaryInfo->ActionString = NULL;\r
c5155f29 800 }\r
1436aea4 801\r
c5155f29
SZ
802 AllocSummaryInfo->AllocateCount = 0;\r
803 AllocSummaryInfo->TotalSize = 0;\r
804 InsertTailList (DriverSummaryInfoData->AllocSummaryInfoList, &AllocSummaryInfoData->Link);\r
805 }\r
1436aea4 806\r
c5155f29 807 AllocSummaryInfo = &AllocSummaryInfoData->AllocSummaryInfo;\r
1436aea4 808 AllocSummaryInfo->AllocateCount++;\r
c5155f29
SZ
809 AllocSummaryInfo->TotalSize += AllocInfo->Size;\r
810\r
1436aea4 811 return (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)AllocInfo + AllocInfo->Header.Length);\r
c5155f29
SZ
812}\r
813\r
814/**\r
815 Create Driver summary information structure and\r
816 link to Context summary information data structure.\r
817\r
818 @param[in, out] ContextSummaryData Context summary information data structure.\r
819 @param[in] DriverInfo Pointer to memory profile driver info.\r
820\r
821 @return Pointer to next memory profile driver info.\r
822\r
823**/\r
824MEMORY_PROFILE_DRIVER_INFO *\r
825CreateDriverSummaryInfo (\r
1436aea4
MK
826 IN OUT MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *ContextSummaryData,\r
827 IN MEMORY_PROFILE_DRIVER_INFO *DriverInfo\r
c5155f29
SZ
828 )\r
829{\r
1436aea4
MK
830 MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA *DriverSummaryInfoData;\r
831 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;\r
832 UINTN AllocIndex;\r
c5155f29
SZ
833\r
834 if (DriverInfo->Header.Signature != MEMORY_PROFILE_DRIVER_INFO_SIGNATURE) {\r
835 return NULL;\r
836 }\r
837\r
838 DriverSummaryInfoData = AllocatePool (sizeof (*DriverSummaryInfoData) + sizeof (LIST_ENTRY));\r
839 if (DriverSummaryInfoData == NULL) {\r
840 return NULL;\r
841 }\r
1436aea4
MK
842\r
843 DriverSummaryInfoData->Signature = MEMORY_PROFILE_DRIVER_INFO_SIGNATURE;\r
844 DriverSummaryInfoData->DriverInfo = DriverInfo;\r
845 DriverSummaryInfoData->AllocSummaryInfoList = (LIST_ENTRY *)(DriverSummaryInfoData + 1);\r
c5155f29
SZ
846 InitializeListHead (DriverSummaryInfoData->AllocSummaryInfoList);\r
847 InsertTailList (ContextSummaryData->DriverSummaryInfoList, &DriverSummaryInfoData->Link);\r
848\r
1436aea4 849 AllocInfo = (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)DriverInfo + DriverInfo->Header.Length);\r
c5155f29
SZ
850 for (AllocIndex = 0; AllocIndex < DriverInfo->AllocRecordCount; AllocIndex++) {\r
851 AllocInfo = CreateAllocSummaryInfo (DriverSummaryInfoData, AllocInfo);\r
852 if (AllocInfo == NULL) {\r
853 return NULL;\r
854 }\r
855 }\r
1436aea4
MK
856\r
857 return (MEMORY_PROFILE_DRIVER_INFO *)AllocInfo;\r
c5155f29
SZ
858}\r
859\r
860/**\r
861 Create Context summary information structure.\r
862\r
863 @param[in] ProfileBuffer Memory profile base address.\r
864 @param[in] ProfileSize Memory profile size.\r
865\r
866 @return Context summary information structure.\r
867\r
868**/\r
869MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *\r
870CreateContextSummaryData (\r
1436aea4
MK
871 IN PHYSICAL_ADDRESS ProfileBuffer,\r
872 IN UINT64 ProfileSize\r
c5155f29
SZ
873 )\r
874{\r
1436aea4
MK
875 MEMORY_PROFILE_CONTEXT *Context;\r
876 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;\r
877 UINTN DriverIndex;\r
c5155f29 878\r
1436aea4 879 Context = (MEMORY_PROFILE_CONTEXT *)ScanMemoryProfileBySignature (ProfileBuffer, ProfileSize, MEMORY_PROFILE_CONTEXT_SIGNATURE);\r
c5155f29
SZ
880 if (Context == NULL) {\r
881 return NULL;\r
882 }\r
883\r
1436aea4
MK
884 mMemoryProfileContextSummary.Signature = MEMORY_PROFILE_CONTEXT_SIGNATURE;\r
885 mMemoryProfileContextSummary.Context = Context;\r
c5155f29
SZ
886 mMemoryProfileContextSummary.DriverSummaryInfoList = &mImageSummaryQueue;\r
887\r
1436aea4 888 DriverInfo = (MEMORY_PROFILE_DRIVER_INFO *)((UINTN)Context + Context->Header.Length);\r
c5155f29
SZ
889 for (DriverIndex = 0; DriverIndex < Context->ImageCount; DriverIndex++) {\r
890 DriverInfo = CreateDriverSummaryInfo (&mMemoryProfileContextSummary, DriverInfo);\r
891 if (DriverInfo == NULL) {\r
892 return NULL;\r
893 }\r
894 }\r
895\r
896 return &mMemoryProfileContextSummary;\r
897}\r
898\r
899/**\r
900 Dump Context summary information.\r
901\r
902 @param[in] ContextSummaryData Context summary information data.\r
903 @param[in] IsForSmm TRUE - SMRAM profile.\r
904 FALSE - UEFI memory profile.\r
905\r
906**/\r
907VOID\r
908DumpContextSummaryData (\r
1436aea4
MK
909 IN MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *ContextSummaryData,\r
910 IN BOOLEAN IsForSmm\r
c5155f29
SZ
911 )\r
912{\r
1436aea4
MK
913 MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA *DriverSummaryInfoData;\r
914 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA *AllocSummaryInfoData;\r
915 LIST_ENTRY *DriverSummaryInfoList;\r
916 LIST_ENTRY *DriverSummaryLink;\r
917 LIST_ENTRY *AllocSummaryInfoList;\r
918 LIST_ENTRY *AllocSummaryLink;\r
919 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;\r
920 MEMORY_PROFILE_ALLOC_SUMMARY_INFO *AllocSummaryInfo;\r
921 CHAR8 *NameString;\r
c5155f29
SZ
922\r
923 if (ContextSummaryData == NULL) {\r
1436aea4 924 return;\r
c5155f29
SZ
925 }\r
926\r
927 Print (L"\nSummary Data:\n");\r
928\r
929 DriverSummaryInfoList = ContextSummaryData->DriverSummaryInfoList;\r
930 for (DriverSummaryLink = DriverSummaryInfoList->ForwardLink;\r
931 DriverSummaryLink != DriverSummaryInfoList;\r
1436aea4
MK
932 DriverSummaryLink = DriverSummaryLink->ForwardLink)\r
933 {\r
c5155f29
SZ
934 DriverSummaryInfoData = CR (\r
935 DriverSummaryLink,\r
936 MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA,\r
937 Link,\r
938 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE\r
939 );\r
940 DriverInfo = DriverSummaryInfoData->DriverInfo;\r
941\r
942 NameString = GetDriverNameString (DriverInfo);\r
943 Print (L"\nDriver - %a (Usage - 0x%08x)", NameString, DriverInfo->CurrentUsage);\r
944 if (DriverInfo->CurrentUsage == 0) {\r
945 Print (L"\n");\r
946 continue;\r
947 }\r
948\r
949 if (DriverInfo->PdbStringOffset != 0) {\r
1436aea4 950 Print (L" (Pdb - %a)\n", (CHAR8 *)((UINTN)DriverInfo + DriverInfo->PdbStringOffset));\r
c5155f29
SZ
951 } else {\r
952 Print (L"\n");\r
953 }\r
1436aea4 954\r
c5155f29 955 Print (L"Caller List:\n");\r
1436aea4
MK
956 Print (L" Count Size RVA Action\n");\r
957 Print (L"========== ================== ================== (================================)\n");\r
c5155f29
SZ
958 AllocSummaryInfoList = DriverSummaryInfoData->AllocSummaryInfoList;\r
959 for (AllocSummaryLink = AllocSummaryInfoList->ForwardLink;\r
960 AllocSummaryLink != AllocSummaryInfoList;\r
1436aea4
MK
961 AllocSummaryLink = AllocSummaryLink->ForwardLink)\r
962 {\r
c5155f29
SZ
963 AllocSummaryInfoData = CR (\r
964 AllocSummaryLink,\r
965 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA,\r
966 Link,\r
967 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE\r
968 );\r
969 AllocSummaryInfo = &AllocSummaryInfoData->AllocSummaryInfo;\r
970\r
1436aea4
MK
971 Print (\r
972 L"0x%08x 0x%016lx <== 0x%016lx",\r
c5155f29
SZ
973 AllocSummaryInfo->AllocateCount,\r
974 AllocSummaryInfo->TotalSize,\r
975 AllocSummaryInfo->CallerAddress - DriverInfo->ImageBase\r
976 );\r
977 Print (L" (%a)\n", ProfileActionToStr (AllocSummaryInfo->Action, AllocSummaryInfo->ActionString, IsForSmm));\r
978 }\r
979 }\r
1436aea4
MK
980\r
981 return;\r
c5155f29
SZ
982}\r
983\r
984/**\r
985 Destroy Context summary information.\r
986\r
987 @param[in, out] ContextSummaryData Context summary information data.\r
988\r
989**/\r
990VOID\r
991DestroyContextSummaryData (\r
1436aea4 992 IN OUT MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *ContextSummaryData\r
c5155f29
SZ
993 )\r
994{\r
1436aea4
MK
995 MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA *DriverSummaryInfoData;\r
996 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA *AllocSummaryInfoData;\r
997 LIST_ENTRY *DriverSummaryInfoList;\r
998 LIST_ENTRY *DriverSummaryLink;\r
999 LIST_ENTRY *AllocSummaryInfoList;\r
1000 LIST_ENTRY *AllocSummaryLink;\r
c5155f29
SZ
1001\r
1002 if (ContextSummaryData == NULL) {\r
1436aea4 1003 return;\r
c5155f29
SZ
1004 }\r
1005\r
1006 DriverSummaryInfoList = ContextSummaryData->DriverSummaryInfoList;\r
1007 for (DriverSummaryLink = DriverSummaryInfoList->ForwardLink;\r
1008 DriverSummaryLink != DriverSummaryInfoList;\r
1436aea4
MK
1009 )\r
1010 {\r
c5155f29
SZ
1011 DriverSummaryInfoData = CR (\r
1012 DriverSummaryLink,\r
1013 MEMORY_PROFILE_DRIVER_SUMMARY_INFO_DATA,\r
1014 Link,\r
1015 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE\r
1016 );\r
1017 DriverSummaryLink = DriverSummaryLink->ForwardLink;\r
1018\r
1019 AllocSummaryInfoList = DriverSummaryInfoData->AllocSummaryInfoList;\r
1020 for (AllocSummaryLink = AllocSummaryInfoList->ForwardLink;\r
1021 AllocSummaryLink != AllocSummaryInfoList;\r
1436aea4
MK
1022 )\r
1023 {\r
c5155f29
SZ
1024 AllocSummaryInfoData = CR (\r
1025 AllocSummaryLink,\r
1026 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_DATA,\r
1027 Link,\r
1028 MEMORY_PROFILE_ALLOC_SUMMARY_INFO_SIGNATURE\r
1029 );\r
1030 AllocSummaryLink = AllocSummaryLink->ForwardLink;\r
1031\r
1032 RemoveEntryList (&AllocSummaryInfoData->Link);\r
1033 FreePool (AllocSummaryInfoData);\r
1034 }\r
1035\r
1036 RemoveEntryList (&DriverSummaryInfoData->Link);\r
1037 FreePool (DriverSummaryInfoData);\r
1038 }\r
1436aea4
MK
1039\r
1040 return;\r
c5155f29
SZ
1041}\r
1042\r
84edd20b
SZ
1043/**\r
1044 Get and dump UEFI memory profile data.\r
1045\r
1046 @return EFI_SUCCESS Get the memory profile data successfully.\r
1047 @return other Fail to get the memory profile data.\r
1048\r
1049**/\r
1050EFI_STATUS\r
1051GetUefiMemoryProfileData (\r
1052 VOID\r
1053 )\r
1054{\r
1436aea4
MK
1055 EFI_STATUS Status;\r
1056 EDKII_MEMORY_PROFILE_PROTOCOL *ProfileProtocol;\r
1057 VOID *Data;\r
1058 UINT64 Size;\r
1059 MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *MemoryProfileContextSummaryData;\r
1060 BOOLEAN RecordingState;\r
1061\r
1062 Status = gBS->LocateProtocol (&gEdkiiMemoryProfileGuid, NULL, (VOID **)&ProfileProtocol);\r
84edd20b 1063 if (EFI_ERROR (Status)) {\r
87000d77 1064 DEBUG ((DEBUG_ERROR, "UefiMemoryProfile: Locate MemoryProfile protocol - %r\n", Status));\r
84edd20b
SZ
1065 return Status;\r
1066 }\r
1067\r
c5155f29
SZ
1068 //\r
1069 // Set recording state if needed.\r
1070 //\r
1071 RecordingState = MEMORY_PROFILE_RECORDING_DISABLE;\r
1436aea4 1072 Status = ProfileProtocol->GetRecordingState (ProfileProtocol, &RecordingState);\r
c5155f29
SZ
1073 if (RecordingState == MEMORY_PROFILE_RECORDING_ENABLE) {\r
1074 ProfileProtocol->SetRecordingState (ProfileProtocol, MEMORY_PROFILE_RECORDING_DISABLE);\r
1075 }\r
1076\r
1436aea4
MK
1077 Size = 0;\r
1078 Data = NULL;\r
84edd20b
SZ
1079 Status = ProfileProtocol->GetData (\r
1080 ProfileProtocol,\r
1081 &Size,\r
1082 Data\r
1083 );\r
1084 if (Status != EFI_BUFFER_TOO_SMALL) {\r
1085 Print (L"UefiMemoryProfile: GetData - %r\n", Status);\r
c5155f29 1086 goto Done;\r
84edd20b
SZ
1087 }\r
1088\r
1436aea4 1089 Data = AllocateZeroPool ((UINTN)Size);\r
84edd20b 1090 if (Data == NULL) {\r
f4420027 1091 Status = EFI_OUT_OF_RESOURCES;\r
84edd20b
SZ
1092 Print (L"UefiMemoryProfile: AllocateZeroPool (0x%x) - %r\n", Size, Status);\r
1093 return Status;\r
1094 }\r
1095\r
1096 Status = ProfileProtocol->GetData (\r
1097 ProfileProtocol,\r
1098 &Size,\r
1099 Data\r
1100 );\r
1101 if (EFI_ERROR (Status)) {\r
84edd20b 1102 Print (L"UefiMemoryProfile: GetData - %r\n", Status);\r
c5155f29 1103 goto Done;\r
84edd20b
SZ
1104 }\r
1105\r
84edd20b
SZ
1106 Print (L"UefiMemoryProfileSize - 0x%x\n", Size);\r
1107 Print (L"======= UefiMemoryProfile begin =======\n");\r
1436aea4 1108 DumpMemoryProfile ((PHYSICAL_ADDRESS)(UINTN)Data, Size, FALSE);\r
c5155f29
SZ
1109\r
1110 //\r
1111 // Dump summary information\r
1112 //\r
1436aea4 1113 MemoryProfileContextSummaryData = CreateContextSummaryData ((PHYSICAL_ADDRESS)(UINTN)Data, Size);\r
c5155f29
SZ
1114 if (MemoryProfileContextSummaryData != NULL) {\r
1115 DumpContextSummaryData (MemoryProfileContextSummaryData, FALSE);\r
1116 DestroyContextSummaryData (MemoryProfileContextSummaryData);\r
1117 }\r
1118\r
84edd20b
SZ
1119 Print (L"======= UefiMemoryProfile end =======\n\n\n");\r
1120\r
c5155f29
SZ
1121Done:\r
1122 if (Data != NULL) {\r
1123 FreePool (Data);\r
1124 }\r
84edd20b 1125\r
c5155f29
SZ
1126 //\r
1127 // Restore recording state if needed.\r
1128 //\r
1129 if (RecordingState == MEMORY_PROFILE_RECORDING_ENABLE) {\r
1130 ProfileProtocol->SetRecordingState (ProfileProtocol, MEMORY_PROFILE_RECORDING_ENABLE);\r
1131 }\r
1132\r
1133 return Status;\r
84edd20b
SZ
1134}\r
1135\r
1136/**\r
1137 Get and dump SMRAM profile data.\r
1138\r
1139 @return EFI_SUCCESS Get the SMRAM profile data successfully.\r
1140 @return other Fail to get the SMRAM profile data.\r
1141\r
1142**/\r
1143EFI_STATUS\r
1144GetSmramProfileData (\r
1145 VOID\r
1146 )\r
1147{\r
1436aea4
MK
1148 EFI_STATUS Status;\r
1149 UINTN CommSize;\r
1150 UINT8 *CommBuffer;\r
1151 EFI_SMM_COMMUNICATE_HEADER *CommHeader;\r
1152 SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO *CommGetProfileInfo;\r
1153 SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET *CommGetProfileData;\r
1154 SMRAM_PROFILE_PARAMETER_RECORDING_STATE *CommRecordingState;\r
1155 UINTN ProfileSize;\r
1156 VOID *ProfileBuffer;\r
1157 EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication;\r
1158 UINTN MinimalSizeNeeded;\r
1159 EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *PiSmmCommunicationRegionTable;\r
1160 UINT32 Index;\r
1161 EFI_MEMORY_DESCRIPTOR *Entry;\r
1162 VOID *Buffer;\r
1163 UINTN Size;\r
1164 UINTN Offset;\r
1165 MEMORY_PROFILE_CONTEXT_SUMMARY_DATA *MemoryProfileContextSummaryData;\r
1166 BOOLEAN RecordingState;\r
c5155f29
SZ
1167\r
1168 ProfileBuffer = NULL;\r
84edd20b 1169\r
1436aea4 1170 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **)&SmmCommunication);\r
84edd20b 1171 if (EFI_ERROR (Status)) {\r
87000d77 1172 DEBUG ((DEBUG_ERROR, "SmramProfile: Locate SmmCommunication protocol - %r\n", Status));\r
84edd20b
SZ
1173 return Status;\r
1174 }\r
1175\r
2f7961c7
SZ
1176 MinimalSizeNeeded = sizeof (EFI_GUID) +\r
1177 sizeof (UINTN) +\r
1436aea4
MK
1178 MAX (\r
1179 sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO),\r
1180 MAX (\r
1181 sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET),\r
1182 sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE)\r
1183 )\r
1184 );\r
1185 MinimalSizeNeeded += MAX (\r
1186 sizeof (MEMORY_PROFILE_CONTEXT),\r
1187 MAX (\r
1188 sizeof (MEMORY_PROFILE_DRIVER_INFO),\r
1189 MAX (\r
1190 sizeof (MEMORY_PROFILE_ALLOC_INFO),\r
1191 MAX (\r
1192 sizeof (MEMORY_PROFILE_DESCRIPTOR),\r
1193 MAX (\r
1194 sizeof (MEMORY_PROFILE_FREE_MEMORY),\r
1195 sizeof (MEMORY_PROFILE_MEMORY_RANGE)\r
1196 )\r
1197 )\r
1198 )\r
1199 )\r
1200 );\r
2f7961c7
SZ
1201\r
1202 Status = EfiGetSystemConfigurationTable (\r
1203 &gEdkiiPiSmmCommunicationRegionTableGuid,\r
1436aea4 1204 (VOID **)&PiSmmCommunicationRegionTable\r
2f7961c7
SZ
1205 );\r
1206 if (EFI_ERROR (Status)) {\r
87000d77 1207 DEBUG ((DEBUG_ERROR, "SmramProfile: Get PiSmmCommunicationRegionTable - %r\n", Status));\r
f4420027
SZ
1208 return Status;\r
1209 }\r
1436aea4 1210\r
2f7961c7 1211 ASSERT (PiSmmCommunicationRegionTable != NULL);\r
1436aea4
MK
1212 Entry = (EFI_MEMORY_DESCRIPTOR *)(PiSmmCommunicationRegionTable + 1);\r
1213 Size = 0;\r
2f7961c7
SZ
1214 for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {\r
1215 if (Entry->Type == EfiConventionalMemory) {\r
1436aea4 1216 Size = EFI_PAGES_TO_SIZE ((UINTN)Entry->NumberOfPages);\r
2f7961c7
SZ
1217 if (Size >= MinimalSizeNeeded) {\r
1218 break;\r
1219 }\r
1220 }\r
1436aea4
MK
1221\r
1222 Entry = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Entry + PiSmmCommunicationRegionTable->DescriptorSize);\r
2f7961c7 1223 }\r
1436aea4 1224\r
2f7961c7 1225 ASSERT (Index < PiSmmCommunicationRegionTable->NumberOfEntries);\r
1436aea4 1226 CommBuffer = (UINT8 *)(UINTN)Entry->PhysicalStart;\r
f4420027 1227\r
c5155f29
SZ
1228 //\r
1229 // Set recording state if needed.\r
1230 //\r
1231 RecordingState = MEMORY_PROFILE_RECORDING_DISABLE;\r
1232\r
1436aea4 1233 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
c5155f29
SZ
1234 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));\r
1235 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE);\r
1236\r
1436aea4 1237 CommRecordingState = (SMRAM_PROFILE_PARAMETER_RECORDING_STATE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
c5155f29
SZ
1238 CommRecordingState->Header.Command = SMRAM_PROFILE_COMMAND_GET_RECORDING_STATE;\r
1239 CommRecordingState->Header.DataLength = sizeof (*CommRecordingState);\r
1240 CommRecordingState->Header.ReturnStatus = (UINT64)-1;\r
1241 CommRecordingState->RecordingState = MEMORY_PROFILE_RECORDING_DISABLE;\r
1242\r
1243 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
1436aea4 1244 Status = SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
c5155f29 1245 if (EFI_ERROR (Status)) {\r
87000d77 1246 DEBUG ((DEBUG_ERROR, "SmramProfile: SmmCommunication - %r\n", Status));\r
c5155f29
SZ
1247 return Status;\r
1248 }\r
1249\r
1250 if (CommRecordingState->Header.ReturnStatus != 0) {\r
1251 Print (L"SmramProfile: GetRecordingState - 0x%0x\n", CommRecordingState->Header.ReturnStatus);\r
1252 return EFI_SUCCESS;\r
1253 }\r
1436aea4 1254\r
c5155f29
SZ
1255 RecordingState = CommRecordingState->RecordingState;\r
1256 if (RecordingState == MEMORY_PROFILE_RECORDING_ENABLE) {\r
1436aea4 1257 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
c5155f29
SZ
1258 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));\r
1259 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE);\r
1260\r
1436aea4 1261 CommRecordingState = (SMRAM_PROFILE_PARAMETER_RECORDING_STATE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
c5155f29
SZ
1262 CommRecordingState->Header.Command = SMRAM_PROFILE_COMMAND_SET_RECORDING_STATE;\r
1263 CommRecordingState->Header.DataLength = sizeof (*CommRecordingState);\r
1264 CommRecordingState->Header.ReturnStatus = (UINT64)-1;\r
1265 CommRecordingState->RecordingState = MEMORY_PROFILE_RECORDING_DISABLE;\r
1266\r
1267 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
1268 SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
1269 }\r
1270\r
84edd20b
SZ
1271 //\r
1272 // Get Size\r
1273 //\r
1436aea4 1274 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
84edd20b
SZ
1275 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));\r
1276 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO);\r
1277\r
1436aea4 1278 CommGetProfileInfo = (SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
84edd20b
SZ
1279 CommGetProfileInfo->Header.Command = SMRAM_PROFILE_COMMAND_GET_PROFILE_INFO;\r
1280 CommGetProfileInfo->Header.DataLength = sizeof (*CommGetProfileInfo);\r
1281 CommGetProfileInfo->Header.ReturnStatus = (UINT64)-1;\r
1282 CommGetProfileInfo->ProfileSize = 0;\r
1283\r
1284 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
1436aea4 1285 Status = SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
c5155f29 1286 ASSERT_EFI_ERROR (Status);\r
84edd20b
SZ
1287\r
1288 if (CommGetProfileInfo->Header.ReturnStatus != 0) {\r
c5155f29 1289 Status = EFI_SUCCESS;\r
84edd20b 1290 Print (L"SmramProfile: GetProfileInfo - 0x%0x\n", CommGetProfileInfo->Header.ReturnStatus);\r
c5155f29 1291 goto Done;\r
84edd20b
SZ
1292 }\r
1293\r
1436aea4 1294 ProfileSize = (UINTN)CommGetProfileInfo->ProfileSize;\r
84edd20b
SZ
1295\r
1296 //\r
1297 // Get Data\r
1298 //\r
2f7961c7 1299 ProfileBuffer = AllocateZeroPool (ProfileSize);\r
c5155f29 1300 if (ProfileBuffer == NULL) {\r
f4420027 1301 Status = EFI_OUT_OF_RESOURCES;\r
2f7961c7 1302 Print (L"SmramProfile: AllocateZeroPool (0x%x) for profile buffer - %r\n", ProfileSize, Status);\r
c5155f29 1303 goto Done;\r
84edd20b
SZ
1304 }\r
1305\r
1436aea4
MK
1306 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
1307 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));\r
2f7961c7 1308 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET);\r
84edd20b 1309\r
1436aea4 1310 CommGetProfileData = (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
2f7961c7 1311 CommGetProfileData->Header.Command = SMRAM_PROFILE_COMMAND_GET_PROFILE_DATA_BY_OFFSET;\r
84edd20b
SZ
1312 CommGetProfileData->Header.DataLength = sizeof (*CommGetProfileData);\r
1313 CommGetProfileData->Header.ReturnStatus = (UINT64)-1;\r
84edd20b
SZ
1314\r
1315 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
1436aea4
MK
1316 Buffer = (UINT8 *)CommHeader + CommSize;\r
1317 Size -= CommSize;\r
2f7961c7 1318\r
1436aea4
MK
1319 CommGetProfileData->ProfileBuffer = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r
1320 CommGetProfileData->ProfileOffset = 0;\r
2f7961c7 1321 while (CommGetProfileData->ProfileOffset < ProfileSize) {\r
1436aea4 1322 Offset = (UINTN)CommGetProfileData->ProfileOffset;\r
2f7961c7 1323 if (Size <= (ProfileSize - CommGetProfileData->ProfileOffset)) {\r
1436aea4 1324 CommGetProfileData->ProfileSize = (UINT64)Size;\r
2f7961c7 1325 } else {\r
1436aea4 1326 CommGetProfileData->ProfileSize = (UINT64)(ProfileSize - CommGetProfileData->ProfileOffset);\r
2f7961c7 1327 }\r
1436aea4 1328\r
2f7961c7
SZ
1329 Status = SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
1330 ASSERT_EFI_ERROR (Status);\r
84edd20b 1331\r
2f7961c7 1332 if (CommGetProfileData->Header.ReturnStatus != 0) {\r
c5155f29 1333 Status = EFI_SUCCESS;\r
2f7961c7 1334 Print (L"GetProfileData - 0x%x\n", CommGetProfileData->Header.ReturnStatus);\r
c5155f29 1335 goto Done;\r
2f7961c7 1336 }\r
84edd20b 1337\r
1436aea4
MK
1338 CopyMem ((UINT8 *)ProfileBuffer + Offset, (VOID *)(UINTN)CommGetProfileData->ProfileBuffer, (UINTN)CommGetProfileData->ProfileSize);\r
1339 }\r
84edd20b 1340\r
2f7961c7 1341 Print (L"SmramProfileSize - 0x%x\n", ProfileSize);\r
84edd20b 1342 Print (L"======= SmramProfile begin =======\n");\r
1436aea4 1343 DumpMemoryProfile ((PHYSICAL_ADDRESS)(UINTN)ProfileBuffer, ProfileSize, TRUE);\r
c5155f29
SZ
1344\r
1345 //\r
1346 // Dump summary information\r
1347 //\r
1436aea4 1348 MemoryProfileContextSummaryData = CreateContextSummaryData ((PHYSICAL_ADDRESS)(UINTN)ProfileBuffer, ProfileSize);\r
c5155f29
SZ
1349 if (MemoryProfileContextSummaryData != NULL) {\r
1350 DumpContextSummaryData (MemoryProfileContextSummaryData, TRUE);\r
1351 DestroyContextSummaryData (MemoryProfileContextSummaryData);\r
1352 }\r
1353\r
84edd20b
SZ
1354 Print (L"======= SmramProfile end =======\n\n\n");\r
1355\r
c5155f29
SZ
1356Done:\r
1357 if (ProfileBuffer != NULL) {\r
1358 FreePool (ProfileBuffer);\r
1359 }\r
84edd20b 1360\r
c5155f29
SZ
1361 //\r
1362 // Restore recording state if needed.\r
1363 //\r
1364 if (RecordingState == MEMORY_PROFILE_RECORDING_ENABLE) {\r
1436aea4 1365 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];\r
c5155f29
SZ
1366 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));\r
1367 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE);\r
1368\r
1436aea4 1369 CommRecordingState = (SMRAM_PROFILE_PARAMETER_RECORDING_STATE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
c5155f29
SZ
1370 CommRecordingState->Header.Command = SMRAM_PROFILE_COMMAND_SET_RECORDING_STATE;\r
1371 CommRecordingState->Header.DataLength = sizeof (*CommRecordingState);\r
1372 CommRecordingState->Header.ReturnStatus = (UINT64)-1;\r
1373 CommRecordingState->RecordingState = MEMORY_PROFILE_RECORDING_ENABLE;\r
1374\r
1375 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
1376 SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
1377 }\r
1378\r
1379 return Status;\r
84edd20b
SZ
1380}\r
1381\r
1382/**\r
1383 The user Entry Point for Application. The user code starts with this function\r
1384 as the real entry point for the image goes into a library that calls this function.\r
1385\r
c5155f29 1386 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
84edd20b 1387 @param[in] SystemTable A pointer to the EFI System Table.\r
c5155f29 1388\r
84edd20b
SZ
1389 @retval EFI_SUCCESS The entry point is executed successfully.\r
1390 @retval other Some error occurs when executing this entry point.\r
1391\r
1392**/\r
1393EFI_STATUS\r
1394EFIAPI\r
1395UefiMain (\r
1436aea4
MK
1396 IN EFI_HANDLE ImageHandle,\r
1397 IN EFI_SYSTEM_TABLE *SystemTable\r
84edd20b
SZ
1398 )\r
1399{\r
1436aea4 1400 EFI_STATUS Status;\r
84edd20b
SZ
1401\r
1402 Status = GetUefiMemoryProfileData ();\r
1403 if (EFI_ERROR (Status)) {\r
87000d77 1404 DEBUG ((DEBUG_ERROR, "GetUefiMemoryProfileData - %r\n", Status));\r
84edd20b
SZ
1405 }\r
1406\r
1407 Status = GetSmramProfileData ();\r
1408 if (EFI_ERROR (Status)) {\r
87000d77 1409 DEBUG ((DEBUG_ERROR, "GetSmramProfileData - %r\n", Status));\r
84edd20b
SZ
1410 }\r
1411\r
1412 return EFI_SUCCESS;\r
1413}\r