TianoCore EDK2 master
Loading...
Searching...
No Matches
SmramProfileRecord.c
Go to the documentation of this file.
1
9#include "PiSmmCore.h"
10
11#define IS_SMRAM_PROFILE_ENABLED ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT1) != 0)
12#define IS_UEFI_MEMORY_PROFILE_ENABLED ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT0) != 0)
13
14#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
15 ((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1)))
16
17typedef struct {
18 UINT32 Signature;
20 LIST_ENTRY *DriverInfoList;
22
23typedef struct {
24 UINT32 Signature;
26 LIST_ENTRY *AllocInfoList;
27 CHAR8 *PdbString;
28 LIST_ENTRY Link;
30
31typedef struct {
32 UINT32 Signature;
34 CHAR8 *ActionString;
35 LIST_ENTRY Link;
37
38//
39// When free memory less than 4 pages, dump it.
40//
41#define SMRAM_INFO_DUMP_PAGE_THRESHOLD 4
42
44 {
45 MEMORY_PROFILE_FREE_MEMORY_SIGNATURE,
47 MEMORY_PROFILE_FREE_MEMORY_REVISION
48 },
49 0,
50 0
51};
52
55 MEMORY_PROFILE_CONTEXT_SIGNATURE,
56 {
57 {
58 MEMORY_PROFILE_CONTEXT_SIGNATURE,
60 MEMORY_PROFILE_CONTEXT_REVISION
61 },
62 0,
63 0,
64 { 0 },
65 { 0 },
66 0,
67 0,
68 0
69 },
70 &mImageQueue,
71};
73
74GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSmramReadyToLock;
75GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSmramProfileGettingStatus = FALSE;
76GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSmramProfileRecordingEnable = MEMORY_PROFILE_RECORDING_DISABLE;
78GLOBAL_REMOVE_IF_UNREFERENCED UINTN mSmramProfileDriverPathSize;
79
84VOID
86 VOID
87 );
88
104EFIAPI
107 IN OUT UINT64 *ProfileSize,
108 OUT VOID *ProfileBuffer
109 );
110
127EFIAPI
131 IN PHYSICAL_ADDRESS ImageBase,
132 IN UINT64 ImageSize,
133 IN EFI_FV_FILETYPE FileType
134 );
135
151EFIAPI
155 IN PHYSICAL_ADDRESS ImageBase,
156 IN UINT64 ImageSize
157 );
158
171EFIAPI
174 OUT BOOLEAN *RecordingState
175 );
176
188EFIAPI
191 IN BOOLEAN RecordingState
192 );
193
218EFIAPI
221 IN PHYSICAL_ADDRESS CallerAddress,
222 IN MEMORY_PROFILE_ACTION Action,
223 IN EFI_MEMORY_TYPE MemoryType,
224 IN VOID *Buffer,
225 IN UINTN Size,
226 IN CHAR8 *ActionString OPTIONAL
227 );
228
236};
237
246 VOID
247 )
248{
249 return mSmramProfileContextPtr;
250}
251
261UINT16
263 IN VOID *Pe32Data
264 )
265{
267 EFI_IMAGE_DOS_HEADER *DosHdr;
268 UINT16 Magic;
269
270 ASSERT (Pe32Data != NULL);
271
272 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
273 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
274 //
275 // DOS image header is present, so read the PE header after the DOS image header.
276 //
277 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
278 } else {
279 //
280 // DOS image header is not present, so PE header is at the image base.
281 //
282 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
283 }
284
285 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
286 return Hdr.Te->Subsystem;
287 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
288 Magic = Hdr.Pe32->OptionalHeader.Magic;
290 return Hdr.Pe32->OptionalHeader.Subsystem;
291 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
292 return Hdr.Pe32Plus->OptionalHeader.Subsystem;
293 }
294 }
295
296 return 0x0000;
297}
298
316RETURN_STATUS
318 IN VOID *Pe32Data,
319 OUT VOID **EntryPoint
320 )
321{
322 EFI_IMAGE_DOS_HEADER *DosHdr;
324
325 ASSERT (Pe32Data != NULL);
326 ASSERT (EntryPoint != NULL);
327
328 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
329 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
330 //
331 // DOS image header is present, so read the PE header after the DOS image header.
332 //
333 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
334 } else {
335 //
336 // DOS image header is not present, so PE header is at the image base.
337 //
338 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
339 }
340
341 //
342 // Calculate the entry point relative to the start of the image.
343 // AddressOfEntryPoint is common for PE32 & PE32+
344 //
345 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
346 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);
347 return RETURN_SUCCESS;
348 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
349 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));
350 return RETURN_SUCCESS;
351 }
352
353 return RETURN_UNSUPPORTED;
354}
355
372 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
373 IN EFI_GUID *FileName,
374 IN PHYSICAL_ADDRESS ImageBase,
375 IN UINT64 ImageSize,
376 IN PHYSICAL_ADDRESS EntryPoint,
377 IN UINT16 ImageSubsystem,
378 IN EFI_FV_FILETYPE FileType
379 )
380{
381 EFI_STATUS Status;
382 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
383 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
384 VOID *EntryPointInImage;
385 CHAR8 *PdbString;
386 UINTN PdbSize;
387 UINTN PdbOccupiedSize;
388
389 PdbSize = 0;
390 PdbOccupiedSize = 0;
391 PdbString = NULL;
392 if (ImageBase != 0) {
393 PdbString = PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageBase);
394 if (PdbString != NULL) {
395 PdbSize = AsciiStrSize (PdbString);
396 PdbOccupiedSize = GET_OCCUPIED_SIZE (PdbSize, sizeof (UINT64));
397 }
398 }
399
400 //
401 // Use SmmInternalAllocatePool() that will not update profile for this AllocatePool action.
402 //
403 Status = SmmInternalAllocatePool (
405 sizeof (*DriverInfoData) + sizeof (LIST_ENTRY) + PdbSize,
406 (VOID **)&DriverInfoData
407 );
408 if (EFI_ERROR (Status)) {
409 return NULL;
410 }
411
412 ASSERT (DriverInfoData != NULL);
413
414 ZeroMem (DriverInfoData, sizeof (*DriverInfoData));
415
416 DriverInfo = &DriverInfoData->DriverInfo;
417 DriverInfoData->Signature = MEMORY_PROFILE_DRIVER_INFO_SIGNATURE;
418 DriverInfo->Header.Signature = MEMORY_PROFILE_DRIVER_INFO_SIGNATURE;
419 DriverInfo->Header.Length = (UINT16)(sizeof (MEMORY_PROFILE_DRIVER_INFO) + PdbOccupiedSize);
420 DriverInfo->Header.Revision = MEMORY_PROFILE_DRIVER_INFO_REVISION;
421 if (FileName != NULL) {
422 CopyMem (&DriverInfo->FileName, FileName, sizeof (EFI_GUID));
423 }
424
425 DriverInfo->ImageBase = ImageBase;
426 DriverInfo->ImageSize = ImageSize;
427 DriverInfo->EntryPoint = EntryPoint;
428 DriverInfo->ImageSubsystem = ImageSubsystem;
429 if ((EntryPoint != 0) && ((EntryPoint < ImageBase) || (EntryPoint >= (ImageBase + ImageSize)))) {
430 //
431 // If the EntryPoint is not in the range of image buffer, it should come from emulation environment.
432 // So patch ImageBuffer here to align the EntryPoint.
433 //
434 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageBase, &EntryPointInImage);
435 ASSERT_EFI_ERROR (Status);
436 DriverInfo->ImageBase = ImageBase + EntryPoint - (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
437 }
438
439 DriverInfo->FileType = FileType;
440 DriverInfoData->AllocInfoList = (LIST_ENTRY *)(DriverInfoData + 1);
441 InitializeListHead (DriverInfoData->AllocInfoList);
442 DriverInfo->CurrentUsage = 0;
443 DriverInfo->PeakUsage = 0;
444 DriverInfo->AllocRecordCount = 0;
445 if (PdbSize != 0) {
446 DriverInfo->PdbStringOffset = (UINT16)sizeof (MEMORY_PROFILE_DRIVER_INFO);
447 DriverInfoData->PdbString = (CHAR8 *)(DriverInfoData->AllocInfoList + 1);
448 CopyMem (DriverInfoData->PdbString, PdbString, PdbSize);
449 } else {
450 DriverInfo->PdbStringOffset = 0;
451 DriverInfoData->PdbString = NULL;
452 }
453
454 InsertTailList (ContextData->DriverInfoList, &DriverInfoData->Link);
455 ContextData->Context.ImageCount++;
456 ContextData->Context.TotalImageSize += DriverInfo->ImageSize;
457
458 return DriverInfoData;
459}
460
470VOID
472 IN EFI_GUID *FileName,
473 IN PHYSICAL_ADDRESS ImageBase,
474 IN UINT64 ImageSize,
475 IN EFI_FV_FILETYPE FileType
476 )
477{
478 EFI_STATUS Status;
479 EDKII_MEMORY_PROFILE_PROTOCOL *ProfileProtocol;
481 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
482
483 if (IS_UEFI_MEMORY_PROFILE_ENABLED) {
484 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
485 Status = gBS->LocateProtocol (&gEdkiiMemoryProfileGuid, NULL, (VOID **)&ProfileProtocol);
486 if (!EFI_ERROR (Status)) {
487 EfiInitializeFwVolDevicepathNode (FilePath, FileName);
488 SetDevicePathEndNode (FilePath + 1);
489
490 Status = ProfileProtocol->RegisterImage (
491 ProfileProtocol,
492 (EFI_DEVICE_PATH_PROTOCOL *)FilePath,
493 ImageBase,
494 ImageSize,
495 FileType
496 );
497 }
498 }
499}
500
509VOID
511 IN EFI_GUID *FileName,
512 IN PHYSICAL_ADDRESS ImageBase,
513 IN UINT64 ImageSize
514 )
515{
516 EFI_STATUS Status;
517 EDKII_MEMORY_PROFILE_PROTOCOL *ProfileProtocol;
519 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
520
521 if (IS_UEFI_MEMORY_PROFILE_ENABLED) {
522 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
523 Status = gBS->LocateProtocol (&gEdkiiMemoryProfileGuid, NULL, (VOID *)&ProfileProtocol);
524 if (!EFI_ERROR (Status)) {
525 EfiInitializeFwVolDevicepathNode (FilePath, FileName);
526 SetDevicePathEndNode (FilePath + 1);
527
528 Status = ProfileProtocol->UnregisterImage (
529 ProfileProtocol,
530 (EFI_DEVICE_PATH_PROTOCOL *)FilePath,
531 ImageBase,
532 ImageSize
533 );
534 }
535 }
536}
537
547BOOLEAN
549 IN EFI_DEVICE_PATH_PROTOCOL *DriverFilePath
550 )
551{
552 EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;
553 EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance;
554 UINTN DevicePathSize;
555 UINTN FilePathSize;
556
557 if (!IsDevicePathValid (mSmramProfileDriverPath, mSmramProfileDriverPathSize)) {
558 //
559 // Invalid Device Path means record all.
560 //
561 return TRUE;
562 }
563
564 //
565 // Record FilePath without end node.
566 //
567 FilePathSize = GetDevicePathSize (DriverFilePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);
568
569 DevicePathInstance = mSmramProfileDriverPath;
570 do {
571 //
572 // Find End node (it might be END_ENTIRE or END_INSTANCE)
573 //
574 TmpDevicePath = DevicePathInstance;
575 while (!IsDevicePathEndType (TmpDevicePath)) {
576 TmpDevicePath = NextDevicePathNode (TmpDevicePath);
577 }
578
579 //
580 // Do not compare END node
581 //
582 DevicePathSize = (UINTN)TmpDevicePath - (UINTN)DevicePathInstance;
583 if ((FilePathSize == DevicePathSize) &&
584 (CompareMem (DriverFilePath, DevicePathInstance, DevicePathSize) == 0))
585 {
586 return TRUE;
587 }
588
589 //
590 // Get next instance
591 //
592 DevicePathInstance = (EFI_DEVICE_PATH_PROTOCOL *)((UINTN)DevicePathInstance + DevicePathSize + DevicePathNodeLength (TmpDevicePath));
593 } while (DevicePathSubType (TmpDevicePath) != END_ENTIRE_DEVICE_PATH_SUBTYPE);
594
595 return FALSE;
596}
597
607BOOLEAN
610 )
611{
612 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
613 PHYSICAL_ADDRESS ImageBase;
614 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
616
617 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
618 EfiInitializeFwVolDevicepathNode (FilePath, &gEfiCallerIdGuid);
619 SetDevicePathEndNode (FilePath + 1);
620
622 return FALSE;
623 }
624
625 ImageBase = gSmmCorePrivate->PiSmmCoreImageBase;
626 DriverInfoData = BuildDriverInfo (
627 ContextData,
628 &gEfiCallerIdGuid,
629 ImageBase,
630 gSmmCorePrivate->PiSmmCoreImageSize,
631 gSmmCorePrivate->PiSmmCoreEntryPoint,
632 InternalPeCoffGetSubsystem ((VOID *)(UINTN)ImageBase),
633 EFI_FV_FILETYPE_SMM_CORE
634 );
635 if (DriverInfoData == NULL) {
636 return FALSE;
637 }
638
639 return TRUE;
640}
641
646VOID
648 VOID
649 )
650{
651 MEMORY_PROFILE_CONTEXT_DATA *SmramProfileContext;
652
654 &gEfiCallerIdGuid,
655 gSmmCorePrivate->PiSmmCoreImageBase,
656 gSmmCorePrivate->PiSmmCoreImageSize,
657 EFI_FV_FILETYPE_SMM_CORE
658 );
659
660 if (!IS_SMRAM_PROFILE_ENABLED) {
661 return;
662 }
663
664 SmramProfileContext = GetSmramProfileContext ();
665 if (SmramProfileContext != NULL) {
666 return;
667 }
668
669 mSmramProfileGettingStatus = FALSE;
670 if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT7) != 0) {
671 mSmramProfileRecordingEnable = MEMORY_PROFILE_RECORDING_DISABLE;
672 } else {
673 mSmramProfileRecordingEnable = MEMORY_PROFILE_RECORDING_ENABLE;
674 }
675
676 mSmramProfileDriverPathSize = PcdGetSize (PcdMemoryProfileDriverPath);
677 mSmramProfileDriverPath = AllocateCopyPool (mSmramProfileDriverPathSize, PcdGetPtr (PcdMemoryProfileDriverPath));
678 mSmramProfileContextPtr = &mSmramProfileContext;
679
680 RegisterSmmCore (&mSmramProfileContext);
681
682 DEBUG ((DEBUG_INFO, "SmramProfileInit SmramProfileContext - 0x%x\n", &mSmramProfileContext));
683}
684
689VOID
691 VOID
692 )
693{
694 EFI_HANDLE Handle;
695 EFI_STATUS Status;
696
697 if (!IS_SMRAM_PROFILE_ENABLED) {
698 return;
699 }
700
701 Handle = NULL;
703 &Handle,
704 &gEdkiiSmmMemoryProfileGuid,
706 &mSmmProfileProtocol
707 );
708 ASSERT_EFI_ERROR (Status);
709}
710
719EFI_GUID *
722 )
723{
725 EFI_GUID *FileName;
726
727 FileName = NULL;
728 if (FilePath != NULL) {
729 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)FilePath;
730 while (!IsDevicePathEnd (ThisFilePath)) {
731 FileName = EfiGetNameGuidFromFwVolDevicePathNode (ThisFilePath);
732 if (FileName != NULL) {
733 break;
734 }
735
736 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)NextDevicePathNode (ThisFilePath);
737 }
738 }
739
740 return FileName;
741}
742
758 IN BOOLEAN RegisterToDxe
759 )
760{
761 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
762 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
763 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
765
766 if (RegisterToDxe) {
768 &DriverEntry->FileName,
769 DriverEntry->ImageBuffer,
770 EFI_PAGES_TO_SIZE (DriverEntry->NumberOfPage),
771 EFI_FV_FILETYPE_SMM
772 );
773 }
774
775 if (!IS_SMRAM_PROFILE_ENABLED) {
776 return EFI_UNSUPPORTED;
777 }
778
779 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
780 EfiInitializeFwVolDevicepathNode (FilePath, &DriverEntry->FileName);
781 SetDevicePathEndNode (FilePath + 1);
782
784 return EFI_UNSUPPORTED;
785 }
786
787 ContextData = GetSmramProfileContext ();
788 if (ContextData == NULL) {
789 return EFI_UNSUPPORTED;
790 }
791
792 DriverInfoData = BuildDriverInfo (
793 ContextData,
794 &DriverEntry->FileName,
795 DriverEntry->ImageBuffer,
796 EFI_PAGES_TO_SIZE (DriverEntry->NumberOfPage),
797 DriverEntry->ImageEntryPoint,
799 EFI_FV_FILETYPE_SMM
800 );
801 if (DriverInfoData == NULL) {
802 return EFI_OUT_OF_RESOURCES;
803 }
804
805 return EFI_SUCCESS;
806}
807
820 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
821 IN EFI_GUID *FileName,
822 IN PHYSICAL_ADDRESS Address
823 )
824{
825 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
826 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
827 LIST_ENTRY *DriverLink;
828 LIST_ENTRY *DriverInfoList;
829
830 DriverInfoList = ContextData->DriverInfoList;
831
832 for (DriverLink = DriverInfoList->ForwardLink;
833 DriverLink != DriverInfoList;
834 DriverLink = DriverLink->ForwardLink)
835 {
836 DriverInfoData = CR (
837 DriverLink,
839 Link,
840 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
841 );
842 DriverInfo = &DriverInfoData->DriverInfo;
843 if ((CompareGuid (&DriverInfo->FileName, FileName)) &&
844 (Address >= DriverInfo->ImageBase) &&
845 (Address < (DriverInfo->ImageBase + DriverInfo->ImageSize)))
846 {
847 return DriverInfoData;
848 }
849 }
850
851 return NULL;
852}
853
866 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
867 IN PHYSICAL_ADDRESS Address
868 )
869{
870 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
871 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
872 LIST_ENTRY *DriverLink;
873 LIST_ENTRY *DriverInfoList;
874
875 DriverInfoList = ContextData->DriverInfoList;
876
877 for (DriverLink = DriverInfoList->ForwardLink;
878 DriverLink != DriverInfoList;
879 DriverLink = DriverLink->ForwardLink)
880 {
881 DriverInfoData = CR (
882 DriverLink,
884 Link,
885 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
886 );
887 DriverInfo = &DriverInfoData->DriverInfo;
888 if ((Address >= DriverInfo->ImageBase) &&
889 (Address < (DriverInfo->ImageBase + DriverInfo->ImageSize)))
890 {
891 return DriverInfoData;
892 }
893 }
894
895 return NULL;
896}
897
913 IN BOOLEAN UnregisterFromDxe
914 )
915{
916 EFI_STATUS Status;
917 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
918 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
919 EFI_GUID *FileName;
920 PHYSICAL_ADDRESS ImageAddress;
921 VOID *EntryPointInImage;
922 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
924
925 if (UnregisterFromDxe) {
927 &DriverEntry->FileName,
928 DriverEntry->ImageBuffer,
929 EFI_PAGES_TO_SIZE (DriverEntry->NumberOfPage)
930 );
931 }
932
933 if (!IS_SMRAM_PROFILE_ENABLED) {
934 return EFI_UNSUPPORTED;
935 }
936
937 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
938 EfiInitializeFwVolDevicepathNode (FilePath, &DriverEntry->FileName);
939 SetDevicePathEndNode (FilePath + 1);
940
942 return EFI_UNSUPPORTED;
943 }
944
945 ContextData = GetSmramProfileContext ();
946 if (ContextData == NULL) {
947 return EFI_UNSUPPORTED;
948 }
949
950 DriverInfoData = NULL;
951 FileName = &DriverEntry->FileName;
952 ImageAddress = DriverEntry->ImageBuffer;
953 if ((DriverEntry->ImageEntryPoint < ImageAddress) || (DriverEntry->ImageEntryPoint >= (ImageAddress + EFI_PAGES_TO_SIZE (DriverEntry->NumberOfPage)))) {
954 //
955 // If the EntryPoint is not in the range of image buffer, it should come from emulation environment.
956 // So patch ImageAddress here to align the EntryPoint.
957 //
958 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageAddress, &EntryPointInImage);
959 ASSERT_EFI_ERROR (Status);
960 ImageAddress = ImageAddress + (UINTN)DriverEntry->ImageEntryPoint - (UINTN)EntryPointInImage;
961 }
962
963 if (FileName != NULL) {
964 DriverInfoData = GetMemoryProfileDriverInfoByFileNameAndAddress (ContextData, FileName, ImageAddress);
965 }
966
967 if (DriverInfoData == NULL) {
968 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, ImageAddress);
969 }
970
971 if (DriverInfoData == NULL) {
972 return EFI_NOT_FOUND;
973 }
974
975 ContextData->Context.TotalImageSize -= DriverInfoData->DriverInfo.ImageSize;
976
977 // Keep the ImageBase for RVA calculation in Application.
978 // DriverInfoData->DriverInfo.ImageBase = 0;
979 DriverInfoData->DriverInfo.ImageSize = 0;
980
981 if (DriverInfoData->DriverInfo.PeakUsage == 0) {
982 ContextData->Context.ImageCount--;
983 RemoveEntryList (&DriverInfoData->Link);
984 //
985 // Use SmmInternalFreePool() that will not update profile for this FreePool action.
986 //
987 SmmInternalFreePool (DriverInfoData);
988 }
989
990 return EFI_SUCCESS;
991}
992
1003BOOLEAN
1005 IN EFI_MEMORY_TYPE MemoryType
1006 )
1007{
1008 UINT64 TestBit;
1009
1010 if ((MemoryType != EfiRuntimeServicesCode) &&
1011 (MemoryType != EfiRuntimeServicesData))
1012 {
1013 return FALSE;
1014 }
1015
1016 TestBit = LShiftU64 (1, MemoryType);
1017
1018 if ((PcdGet64 (PcdMemoryProfileMemoryType) & TestBit) != 0) {
1019 return TRUE;
1020 } else {
1021 return FALSE;
1022 }
1023}
1024
1038 IN EFI_MEMORY_TYPE MemoryType
1039 )
1040{
1041 return MemoryType;
1042}
1043
1050VOID
1052 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData
1053 )
1054{
1055 LIST_ENTRY *Node;
1056 FREE_PAGE_LIST *Pages;
1057 LIST_ENTRY *FreePageList;
1058 UINTN NumberOfPages;
1059
1060 NumberOfPages = 0;
1061 FreePageList = &mSmmMemoryMap;
1062 for (Node = FreePageList->BackLink;
1063 Node != FreePageList;
1064 Node = Node->BackLink)
1065 {
1066 Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
1067 NumberOfPages += Pages->NumberOfPages;
1068 }
1069
1070 mSmramFreeMemory.TotalFreeMemoryPages = NumberOfPages;
1071
1072 if (NumberOfPages <= SMRAM_INFO_DUMP_PAGE_THRESHOLD) {
1073 DumpSmramInfo ();
1074 }
1075}
1076
1095 IN PHYSICAL_ADDRESS CallerAddress,
1096 IN MEMORY_PROFILE_ACTION Action,
1097 IN EFI_MEMORY_TYPE MemoryType,
1098 IN UINTN Size,
1099 IN VOID *Buffer,
1100 IN CHAR8 *ActionString OPTIONAL
1101 )
1102{
1103 EFI_STATUS Status;
1104 MEMORY_PROFILE_CONTEXT *Context;
1105 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
1106 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1107 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1108 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1109 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1110 EFI_MEMORY_TYPE ProfileMemoryIndex;
1111 MEMORY_PROFILE_ACTION BasicAction;
1112 UINTN ActionStringSize;
1113 UINTN ActionStringOccupiedSize;
1114
1115 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1116
1117 ContextData = GetSmramProfileContext ();
1118 if (ContextData == NULL) {
1119 return EFI_UNSUPPORTED;
1120 }
1121
1122 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, CallerAddress);
1123 if (DriverInfoData == NULL) {
1124 return EFI_UNSUPPORTED;
1125 }
1126
1127 ActionStringSize = 0;
1128 ActionStringOccupiedSize = 0;
1129 if (ActionString != NULL) {
1130 ActionStringSize = AsciiStrSize (ActionString);
1131 ActionStringOccupiedSize = GET_OCCUPIED_SIZE (ActionStringSize, sizeof (UINT64));
1132 }
1133
1134 //
1135 // Use SmmInternalAllocatePool() that will not update profile for this AllocatePool action.
1136 //
1137 AllocInfoData = NULL;
1138 Status = SmmInternalAllocatePool (
1140 sizeof (*AllocInfoData) + ActionStringSize,
1141 (VOID **)&AllocInfoData
1142 );
1143 if (EFI_ERROR (Status)) {
1144 return EFI_OUT_OF_RESOURCES;
1145 }
1146
1147 ASSERT (AllocInfoData != NULL);
1148
1149 //
1150 // Only update SequenceCount if and only if it is basic action.
1151 //
1152 if (Action == BasicAction) {
1153 ContextData->Context.SequenceCount++;
1154 }
1155
1156 AllocInfo = &AllocInfoData->AllocInfo;
1157 AllocInfoData->Signature = MEMORY_PROFILE_ALLOC_INFO_SIGNATURE;
1158 AllocInfo->Header.Signature = MEMORY_PROFILE_ALLOC_INFO_SIGNATURE;
1159 AllocInfo->Header.Length = (UINT16)(sizeof (MEMORY_PROFILE_ALLOC_INFO) + ActionStringOccupiedSize);
1160 AllocInfo->Header.Revision = MEMORY_PROFILE_ALLOC_INFO_REVISION;
1161 AllocInfo->CallerAddress = CallerAddress;
1162 AllocInfo->SequenceId = ContextData->Context.SequenceCount;
1163 AllocInfo->Action = Action;
1164 AllocInfo->MemoryType = MemoryType;
1165 AllocInfo->Buffer = (PHYSICAL_ADDRESS)(UINTN)Buffer;
1166 AllocInfo->Size = Size;
1167 if (ActionString != NULL) {
1168 AllocInfo->ActionStringOffset = (UINT16)sizeof (MEMORY_PROFILE_ALLOC_INFO);
1169 AllocInfoData->ActionString = (CHAR8 *)(AllocInfoData + 1);
1170 CopyMem (AllocInfoData->ActionString, ActionString, ActionStringSize);
1171 } else {
1172 AllocInfo->ActionStringOffset = 0;
1173 AllocInfoData->ActionString = NULL;
1174 }
1175
1176 InsertTailList (DriverInfoData->AllocInfoList, &AllocInfoData->Link);
1177
1178 Context = &ContextData->Context;
1179 DriverInfo = &DriverInfoData->DriverInfo;
1180 DriverInfo->AllocRecordCount++;
1181
1182 //
1183 // Update summary if and only if it is basic action.
1184 //
1185 if (Action == BasicAction) {
1186 ProfileMemoryIndex = GetProfileMemoryIndex (MemoryType);
1187
1188 DriverInfo->CurrentUsage += Size;
1189 if (DriverInfo->PeakUsage < DriverInfo->CurrentUsage) {
1190 DriverInfo->PeakUsage = DriverInfo->CurrentUsage;
1191 }
1192
1193 DriverInfo->CurrentUsageByType[ProfileMemoryIndex] += Size;
1194 if (DriverInfo->PeakUsageByType[ProfileMemoryIndex] < DriverInfo->CurrentUsageByType[ProfileMemoryIndex]) {
1195 DriverInfo->PeakUsageByType[ProfileMemoryIndex] = DriverInfo->CurrentUsageByType[ProfileMemoryIndex];
1196 }
1197
1198 Context->CurrentTotalUsage += Size;
1199 if (Context->PeakTotalUsage < Context->CurrentTotalUsage) {
1200 Context->PeakTotalUsage = Context->CurrentTotalUsage;
1201 }
1202
1203 Context->CurrentTotalUsageByType[ProfileMemoryIndex] += Size;
1204 if (Context->PeakTotalUsageByType[ProfileMemoryIndex] < Context->CurrentTotalUsageByType[ProfileMemoryIndex]) {
1205 Context->PeakTotalUsageByType[ProfileMemoryIndex] = Context->CurrentTotalUsageByType[ProfileMemoryIndex];
1206 }
1207
1208 SmramProfileUpdateFreePages (ContextData);
1209 }
1210
1211 return EFI_SUCCESS;
1212}
1213
1226 IN MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData,
1227 IN MEMORY_PROFILE_ACTION BasicAction,
1228 IN UINTN Size,
1229 IN VOID *Buffer
1230 )
1231{
1232 LIST_ENTRY *AllocInfoList;
1233 LIST_ENTRY *AllocLink;
1234 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1235 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1236
1237 AllocInfoList = DriverInfoData->AllocInfoList;
1238
1239 for (AllocLink = AllocInfoList->ForwardLink;
1240 AllocLink != AllocInfoList;
1241 AllocLink = AllocLink->ForwardLink)
1242 {
1243 AllocInfoData = CR (
1244 AllocLink,
1246 Link,
1247 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1248 );
1249 AllocInfo = &AllocInfoData->AllocInfo;
1250 if ((AllocInfo->Action & MEMORY_PROFILE_ACTION_BASIC_MASK) != BasicAction) {
1251 continue;
1252 }
1253
1254 switch (BasicAction) {
1255 case MemoryProfileActionAllocatePages:
1256 if ((AllocInfo->Buffer <= (PHYSICAL_ADDRESS)(UINTN)Buffer) &&
1257 ((AllocInfo->Buffer + AllocInfo->Size) >= ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)))
1258 {
1259 return AllocInfoData;
1260 }
1261
1262 break;
1263 case MemoryProfileActionAllocatePool:
1264 if (AllocInfo->Buffer == (PHYSICAL_ADDRESS)(UINTN)Buffer) {
1265 return AllocInfoData;
1266 }
1267
1268 break;
1269 default:
1270 ASSERT (FALSE);
1271 break;
1272 }
1273 }
1274
1275 return NULL;
1276}
1277
1293 IN PHYSICAL_ADDRESS CallerAddress,
1294 IN MEMORY_PROFILE_ACTION Action,
1295 IN UINTN Size,
1296 IN VOID *Buffer
1297 )
1298{
1299 MEMORY_PROFILE_CONTEXT *Context;
1300 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
1301 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1302 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1303 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1304 LIST_ENTRY *DriverLink;
1305 LIST_ENTRY *DriverInfoList;
1306 MEMORY_PROFILE_DRIVER_INFO_DATA *ThisDriverInfoData;
1307 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1308 EFI_MEMORY_TYPE ProfileMemoryIndex;
1309 MEMORY_PROFILE_ACTION BasicAction;
1310 BOOLEAN Found;
1311
1312 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1313
1314 ContextData = GetSmramProfileContext ();
1315 if (ContextData == NULL) {
1316 return EFI_UNSUPPORTED;
1317 }
1318
1319 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, CallerAddress);
1320
1321 //
1322 // Do not return if DriverInfoData == NULL here,
1323 // because driver A might free memory allocated by driver B.
1324 //
1325
1326 //
1327 // Need use do-while loop to find all possible record,
1328 // because one address might be recorded multiple times.
1329 //
1330 Found = FALSE;
1331 AllocInfoData = NULL;
1332 do {
1333 if (DriverInfoData != NULL) {
1334 switch (BasicAction) {
1335 case MemoryProfileActionFreePages:
1336 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (DriverInfoData, MemoryProfileActionAllocatePages, Size, Buffer);
1337 break;
1338 case MemoryProfileActionFreePool:
1339 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (DriverInfoData, MemoryProfileActionAllocatePool, 0, Buffer);
1340 break;
1341 default:
1342 ASSERT (FALSE);
1343 AllocInfoData = NULL;
1344 break;
1345 }
1346 }
1347
1348 if (AllocInfoData == NULL) {
1349 //
1350 // Legal case, because driver A might free memory allocated by driver B, by some protocol.
1351 //
1352 DriverInfoList = ContextData->DriverInfoList;
1353
1354 for (DriverLink = DriverInfoList->ForwardLink;
1355 DriverLink != DriverInfoList;
1356 DriverLink = DriverLink->ForwardLink)
1357 {
1358 ThisDriverInfoData = CR (
1359 DriverLink,
1361 Link,
1362 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1363 );
1364 switch (BasicAction) {
1365 case MemoryProfileActionFreePages:
1366 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (ThisDriverInfoData, MemoryProfileActionAllocatePages, Size, Buffer);
1367 break;
1368 case MemoryProfileActionFreePool:
1369 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (ThisDriverInfoData, MemoryProfileActionAllocatePool, 0, Buffer);
1370 break;
1371 default:
1372 ASSERT (FALSE);
1373 AllocInfoData = NULL;
1374 break;
1375 }
1376
1377 if (AllocInfoData != NULL) {
1378 DriverInfoData = ThisDriverInfoData;
1379 break;
1380 }
1381 }
1382
1383 if (AllocInfoData == NULL) {
1384 //
1385 // If (!Found), no matched allocate info is found for this free action.
1386 // It is because the specified memory type allocate actions have been filtered by
1387 // CoreNeedRecordProfile(), but free actions have no memory type information,
1388 // they can not be filtered by CoreNeedRecordProfile(). Then, they will be
1389 // filtered here.
1390 //
1391 // If (Found), it is normal exit path.
1392 return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);
1393 }
1394 }
1395
1396 if ((DriverInfoData == NULL) || (AllocInfoData == NULL)) {
1397 ASSERT (DriverInfoData != NULL);
1398 ASSERT (AllocInfoData != NULL);
1399 return EFI_NOT_FOUND;
1400 }
1401
1402 Found = TRUE;
1403
1404 Context = &ContextData->Context;
1405 DriverInfo = &DriverInfoData->DriverInfo;
1406 AllocInfo = &AllocInfoData->AllocInfo;
1407
1408 DriverInfo->AllocRecordCount--;
1409 //
1410 // Update summary if and only if it is basic action.
1411 //
1412 if (AllocInfo->Action == (AllocInfo->Action & MEMORY_PROFILE_ACTION_BASIC_MASK)) {
1413 ProfileMemoryIndex = GetProfileMemoryIndex (AllocInfo->MemoryType);
1414
1415 Context->CurrentTotalUsage -= AllocInfo->Size;
1416 Context->CurrentTotalUsageByType[ProfileMemoryIndex] -= AllocInfo->Size;
1417
1418 DriverInfo->CurrentUsage -= AllocInfo->Size;
1419 DriverInfo->CurrentUsageByType[ProfileMemoryIndex] -= AllocInfo->Size;
1420 }
1421
1422 RemoveEntryList (&AllocInfoData->Link);
1423
1424 if (BasicAction == MemoryProfileActionFreePages) {
1425 if (AllocInfo->Buffer != (PHYSICAL_ADDRESS)(UINTN)Buffer) {
1427 AllocInfo->CallerAddress,
1428 AllocInfo->Action,
1429 AllocInfo->MemoryType,
1430 (UINTN)((PHYSICAL_ADDRESS)(UINTN)Buffer - AllocInfo->Buffer),
1431 (VOID *)(UINTN)AllocInfo->Buffer,
1432 AllocInfoData->ActionString
1433 );
1434 }
1435
1436 if (AllocInfo->Buffer + AllocInfo->Size != ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)) {
1438 AllocInfo->CallerAddress,
1439 AllocInfo->Action,
1440 AllocInfo->MemoryType,
1441 (UINTN)((AllocInfo->Buffer + AllocInfo->Size) - ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)),
1442 (VOID *)((UINTN)Buffer + Size),
1443 AllocInfoData->ActionString
1444 );
1445 }
1446 }
1447
1448 //
1449 // Use SmmInternalFreePool() that will not update profile for this FreePool action.
1450 //
1451 SmmInternalFreePool (AllocInfoData);
1452 } while (TRUE);
1453}
1454
1478EFIAPI
1480 IN PHYSICAL_ADDRESS CallerAddress,
1481 IN MEMORY_PROFILE_ACTION Action,
1482 IN EFI_MEMORY_TYPE MemoryType, // Valid for AllocatePages/AllocatePool
1483 IN UINTN Size, // Valid for AllocatePages/FreePages/AllocatePool
1484 IN VOID *Buffer,
1485 IN CHAR8 *ActionString OPTIONAL
1486 )
1487{
1488 EFI_STATUS Status;
1489 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1490 MEMORY_PROFILE_ACTION BasicAction;
1491
1492 if (!IS_SMRAM_PROFILE_ENABLED) {
1493 return EFI_UNSUPPORTED;
1494 }
1495
1496 if (mSmramProfileGettingStatus) {
1497 return EFI_ACCESS_DENIED;
1498 }
1499
1500 if (!mSmramProfileRecordingEnable) {
1501 return EFI_ABORTED;
1502 }
1503
1504 //
1505 // Get the basic action to know how to process the record
1506 //
1507 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1508
1509 //
1510 // Free operations have no memory type information, so skip the check.
1511 //
1512 if ((BasicAction == MemoryProfileActionAllocatePages) || (BasicAction == MemoryProfileActionAllocatePool)) {
1513 //
1514 // Only record limited MemoryType.
1515 //
1516 if (!SmmCoreNeedRecordProfile (MemoryType)) {
1517 return EFI_UNSUPPORTED;
1518 }
1519 }
1520
1521 ContextData = GetSmramProfileContext ();
1522 if (ContextData == NULL) {
1523 return EFI_UNSUPPORTED;
1524 }
1525
1526 switch (BasicAction) {
1527 case MemoryProfileActionAllocatePages:
1528 Status = SmmCoreUpdateProfileAllocate (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
1529 break;
1530 case MemoryProfileActionFreePages:
1531 Status = SmmCoreUpdateProfileFree (CallerAddress, Action, Size, Buffer);
1532 break;
1533 case MemoryProfileActionAllocatePool:
1534 Status = SmmCoreUpdateProfileAllocate (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
1535 break;
1536 case MemoryProfileActionFreePool:
1537 Status = SmmCoreUpdateProfileFree (CallerAddress, Action, 0, Buffer);
1538 break;
1539 default:
1540 ASSERT (FALSE);
1541 Status = EFI_UNSUPPORTED;
1542 break;
1543 }
1544
1545 return Status;
1546}
1547
1552VOID
1554 VOID
1555 )
1556{
1557 if (!IS_SMRAM_PROFILE_ENABLED) {
1558 return;
1559 }
1560
1561 DEBUG ((DEBUG_INFO, "SmramProfileReadyToLock\n"));
1562 mSmramReadyToLock = TRUE;
1563}
1564
1566
1573UINTN
1575 VOID
1576 )
1577{
1578 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1579 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1580 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1581 LIST_ENTRY *DriverInfoList;
1582 LIST_ENTRY *DriverLink;
1583 LIST_ENTRY *AllocInfoList;
1584 LIST_ENTRY *AllocLink;
1585 UINTN TotalSize;
1586 LIST_ENTRY *Node;
1587 LIST_ENTRY *FreePageList;
1588 LIST_ENTRY *FreePoolList;
1589 FREE_POOL_HEADER *Pool;
1590 UINTN PoolListIndex;
1591 UINTN Index;
1592 UINTN SmmPoolTypeIndex;
1593
1594 ContextData = GetSmramProfileContext ();
1595 if (ContextData == NULL) {
1596 return 0;
1597 }
1598
1599 TotalSize = sizeof (MEMORY_PROFILE_CONTEXT);
1600
1601 DriverInfoList = ContextData->DriverInfoList;
1602 for (DriverLink = DriverInfoList->ForwardLink;
1603 DriverLink != DriverInfoList;
1604 DriverLink = DriverLink->ForwardLink)
1605 {
1606 DriverInfoData = CR (
1607 DriverLink,
1609 Link,
1610 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1611 );
1612 TotalSize += DriverInfoData->DriverInfo.Header.Length;
1613
1614 AllocInfoList = DriverInfoData->AllocInfoList;
1615 for (AllocLink = AllocInfoList->ForwardLink;
1616 AllocLink != AllocInfoList;
1617 AllocLink = AllocLink->ForwardLink)
1618 {
1619 AllocInfoData = CR (
1620 AllocLink,
1622 Link,
1623 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1624 );
1625 TotalSize += AllocInfoData->AllocInfo.Header.Length;
1626 }
1627 }
1628
1629 Index = 0;
1630 FreePageList = &mSmmMemoryMap;
1631 for (Node = FreePageList->BackLink;
1632 Node != FreePageList;
1633 Node = Node->BackLink)
1634 {
1635 Index++;
1636 }
1637
1638 for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
1639 for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
1640 FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][PoolListIndex];
1641 for (Node = FreePoolList->BackLink;
1642 Node != FreePoolList;
1643 Node = Node->BackLink)
1644 {
1645 Pool = BASE_CR (Node, FREE_POOL_HEADER, Link);
1646 if (Pool->Header.Available) {
1647 Index++;
1648 }
1649 }
1650 }
1651 }
1652
1653 TotalSize += (sizeof (MEMORY_PROFILE_FREE_MEMORY) + Index * sizeof (MEMORY_PROFILE_DESCRIPTOR));
1654 TotalSize += (sizeof (MEMORY_PROFILE_MEMORY_RANGE) + mFullSmramRangeCount * sizeof (MEMORY_PROFILE_DESCRIPTOR));
1655
1656 return TotalSize;
1657}
1658
1669VOID
1671 OUT VOID *ProfileBuffer,
1672 IN OUT UINT64 *ProfileSize,
1673 IN OUT UINT64 *ProfileOffset
1674 )
1675{
1676 MEMORY_PROFILE_CONTEXT *Context;
1677 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
1678 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1679 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1680 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1681 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1682 LIST_ENTRY *DriverInfoList;
1683 LIST_ENTRY *DriverLink;
1684 LIST_ENTRY *AllocInfoList;
1685 LIST_ENTRY *AllocLink;
1686 LIST_ENTRY *Node;
1687 FREE_PAGE_LIST *Pages;
1688 LIST_ENTRY *FreePageList;
1689 LIST_ENTRY *FreePoolList;
1690 FREE_POOL_HEADER *Pool;
1691 UINTN PoolListIndex;
1692 UINTN Index;
1694 MEMORY_PROFILE_MEMORY_RANGE *MemoryRange;
1695 MEMORY_PROFILE_DESCRIPTOR *MemoryProfileDescriptor;
1696 UINT64 Offset;
1697 UINT64 RemainingSize;
1698 UINTN PdbSize;
1699 UINTN ActionStringSize;
1700 UINTN SmmPoolTypeIndex;
1701
1702 ContextData = GetSmramProfileContext ();
1703 if (ContextData == NULL) {
1704 return;
1705 }
1706
1707 RemainingSize = *ProfileSize;
1708 Offset = 0;
1709
1710 if (*ProfileOffset < sizeof (MEMORY_PROFILE_CONTEXT)) {
1711 if (RemainingSize >= sizeof (MEMORY_PROFILE_CONTEXT)) {
1712 Context = ProfileBuffer;
1713 CopyMem (Context, &ContextData->Context, sizeof (MEMORY_PROFILE_CONTEXT));
1714 RemainingSize -= sizeof (MEMORY_PROFILE_CONTEXT);
1715 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_CONTEXT);
1716 } else {
1717 goto Done;
1718 }
1719 }
1720
1721 Offset += sizeof (MEMORY_PROFILE_CONTEXT);
1722
1723 DriverInfoList = ContextData->DriverInfoList;
1724 for (DriverLink = DriverInfoList->ForwardLink;
1725 DriverLink != DriverInfoList;
1726 DriverLink = DriverLink->ForwardLink)
1727 {
1728 DriverInfoData = CR (
1729 DriverLink,
1731 Link,
1732 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1733 );
1734 if (*ProfileOffset < (Offset + DriverInfoData->DriverInfo.Header.Length)) {
1735 if (RemainingSize >= DriverInfoData->DriverInfo.Header.Length) {
1736 DriverInfo = ProfileBuffer;
1737 CopyMem (DriverInfo, &DriverInfoData->DriverInfo, sizeof (MEMORY_PROFILE_DRIVER_INFO));
1738 if (DriverInfo->PdbStringOffset != 0) {
1739 PdbSize = AsciiStrSize (DriverInfoData->PdbString);
1740 CopyMem ((VOID *)((UINTN)DriverInfo + DriverInfo->PdbStringOffset), DriverInfoData->PdbString, PdbSize);
1741 }
1742
1743 RemainingSize -= DriverInfo->Header.Length;
1744 ProfileBuffer = (UINT8 *)ProfileBuffer + DriverInfo->Header.Length;
1745 } else {
1746 goto Done;
1747 }
1748 }
1749
1750 Offset += DriverInfoData->DriverInfo.Header.Length;
1751
1752 AllocInfoList = DriverInfoData->AllocInfoList;
1753 for (AllocLink = AllocInfoList->ForwardLink;
1754 AllocLink != AllocInfoList;
1755 AllocLink = AllocLink->ForwardLink)
1756 {
1757 AllocInfoData = CR (
1758 AllocLink,
1760 Link,
1761 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1762 );
1763 if (*ProfileOffset < (Offset + AllocInfoData->AllocInfo.Header.Length)) {
1764 if (RemainingSize >= AllocInfoData->AllocInfo.Header.Length) {
1765 AllocInfo = ProfileBuffer;
1766 CopyMem (AllocInfo, &AllocInfoData->AllocInfo, sizeof (MEMORY_PROFILE_ALLOC_INFO));
1767 if (AllocInfo->ActionStringOffset) {
1768 ActionStringSize = AsciiStrSize (AllocInfoData->ActionString);
1769 CopyMem ((VOID *)((UINTN)AllocInfo + AllocInfo->ActionStringOffset), AllocInfoData->ActionString, ActionStringSize);
1770 }
1771
1772 RemainingSize -= AllocInfo->Header.Length;
1773 ProfileBuffer = (UINT8 *)ProfileBuffer + AllocInfo->Header.Length;
1774 } else {
1775 goto Done;
1776 }
1777 }
1778
1779 Offset += AllocInfoData->AllocInfo.Header.Length;
1780 }
1781 }
1782
1783 if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_FREE_MEMORY))) {
1784 if (RemainingSize >= sizeof (MEMORY_PROFILE_FREE_MEMORY)) {
1785 FreeMemory = ProfileBuffer;
1786 CopyMem (FreeMemory, &mSmramFreeMemory, sizeof (MEMORY_PROFILE_FREE_MEMORY));
1787 Index = 0;
1788 FreePageList = &mSmmMemoryMap;
1789 for (Node = FreePageList->BackLink;
1790 Node != FreePageList;
1791 Node = Node->BackLink)
1792 {
1793 Index++;
1794 }
1795
1796 for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
1797 for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
1798 FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][MAX_POOL_INDEX - PoolListIndex - 1];
1799 for (Node = FreePoolList->BackLink;
1800 Node != FreePoolList;
1801 Node = Node->BackLink)
1802 {
1803 Pool = BASE_CR (Node, FREE_POOL_HEADER, Link);
1804 if (Pool->Header.Available) {
1805 Index++;
1806 }
1807 }
1808 }
1809 }
1810
1811 // if this truncates anything, we have bigger problems
1812 FreeMemory->FreeMemoryEntryCount = (UINT32)Index;
1813
1814 RemainingSize -= sizeof (MEMORY_PROFILE_FREE_MEMORY);
1815 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_FREE_MEMORY);
1816 } else {
1817 goto Done;
1818 }
1819 }
1820
1821 Offset += sizeof (MEMORY_PROFILE_FREE_MEMORY);
1822 FreePageList = &mSmmMemoryMap;
1823 for (Node = FreePageList->BackLink;
1824 Node != FreePageList;
1825 Node = Node->BackLink)
1826 {
1827 if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_DESCRIPTOR))) {
1828 if (RemainingSize >= sizeof (MEMORY_PROFILE_DESCRIPTOR)) {
1829 Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
1830 MemoryProfileDescriptor = ProfileBuffer;
1831 MemoryProfileDescriptor->Header.Signature = MEMORY_PROFILE_DESCRIPTOR_SIGNATURE;
1832 MemoryProfileDescriptor->Header.Length = sizeof (MEMORY_PROFILE_DESCRIPTOR);
1833 MemoryProfileDescriptor->Header.Revision = MEMORY_PROFILE_DESCRIPTOR_REVISION;
1834 MemoryProfileDescriptor->Address = (PHYSICAL_ADDRESS)(UINTN)Pages;
1835 MemoryProfileDescriptor->Size = EFI_PAGES_TO_SIZE (Pages->NumberOfPages);
1836
1837 RemainingSize -= sizeof (MEMORY_PROFILE_DESCRIPTOR);
1838 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_DESCRIPTOR);
1839 } else {
1840 goto Done;
1841 }
1842 }
1843
1844 Offset += sizeof (MEMORY_PROFILE_DESCRIPTOR);
1845 }
1846
1847 for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
1848 for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
1849 FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][MAX_POOL_INDEX - PoolListIndex - 1];
1850 for (Node = FreePoolList->BackLink;
1851 Node != FreePoolList;
1852 Node = Node->BackLink)
1853 {
1854 Pool = BASE_CR (Node, FREE_POOL_HEADER, Link);
1855 if (Pool->Header.Available) {
1856 if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_DESCRIPTOR))) {
1857 if (RemainingSize >= sizeof (MEMORY_PROFILE_DESCRIPTOR)) {
1858 MemoryProfileDescriptor = ProfileBuffer;
1859 MemoryProfileDescriptor->Header.Signature = MEMORY_PROFILE_DESCRIPTOR_SIGNATURE;
1860 MemoryProfileDescriptor->Header.Length = sizeof (MEMORY_PROFILE_DESCRIPTOR);
1861 MemoryProfileDescriptor->Header.Revision = MEMORY_PROFILE_DESCRIPTOR_REVISION;
1862 MemoryProfileDescriptor->Address = (PHYSICAL_ADDRESS)(UINTN)Pool;
1863 MemoryProfileDescriptor->Size = Pool->Header.Size;
1864
1865 RemainingSize -= sizeof (MEMORY_PROFILE_DESCRIPTOR);
1866 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_DESCRIPTOR);
1867 } else {
1868 goto Done;
1869 }
1870 }
1871
1872 Offset += sizeof (MEMORY_PROFILE_DESCRIPTOR);
1873 }
1874 }
1875 }
1876 }
1877
1878 if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_MEMORY_RANGE))) {
1879 if (RemainingSize >= sizeof (MEMORY_PROFILE_MEMORY_RANGE)) {
1880 MemoryRange = ProfileBuffer;
1881 MemoryRange->Header.Signature = MEMORY_PROFILE_MEMORY_RANGE_SIGNATURE;
1882 MemoryRange->Header.Length = sizeof (MEMORY_PROFILE_MEMORY_RANGE);
1883 MemoryRange->Header.Revision = MEMORY_PROFILE_MEMORY_RANGE_REVISION;
1884 MemoryRange->MemoryRangeCount = (UINT32)mFullSmramRangeCount;
1885
1886 RemainingSize -= sizeof (MEMORY_PROFILE_MEMORY_RANGE);
1887 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_MEMORY_RANGE);
1888 } else {
1889 goto Done;
1890 }
1891 }
1892
1893 Offset += sizeof (MEMORY_PROFILE_MEMORY_RANGE);
1894 for (Index = 0; Index < mFullSmramRangeCount; Index++) {
1895 if (*ProfileOffset < (Offset + sizeof (MEMORY_PROFILE_DESCRIPTOR))) {
1896 if (RemainingSize >= sizeof (MEMORY_PROFILE_DESCRIPTOR)) {
1897 MemoryProfileDescriptor = ProfileBuffer;
1898 MemoryProfileDescriptor->Header.Signature = MEMORY_PROFILE_DESCRIPTOR_SIGNATURE;
1899 MemoryProfileDescriptor->Header.Length = sizeof (MEMORY_PROFILE_DESCRIPTOR);
1900 MemoryProfileDescriptor->Header.Revision = MEMORY_PROFILE_DESCRIPTOR_REVISION;
1901 MemoryProfileDescriptor->Address = mFullSmramRanges[Index].PhysicalStart;
1902 MemoryProfileDescriptor->Size = mFullSmramRanges[Index].PhysicalSize;
1903
1904 RemainingSize -= sizeof (MEMORY_PROFILE_DESCRIPTOR);
1905 ProfileBuffer = (UINT8 *)ProfileBuffer + sizeof (MEMORY_PROFILE_DESCRIPTOR);
1906 } else {
1907 goto Done;
1908 }
1909 }
1910
1911 Offset += sizeof (MEMORY_PROFILE_DESCRIPTOR);
1912 }
1913
1914Done:
1915 //
1916 // On output, actual profile data size copied.
1917 //
1918 *ProfileSize -= RemainingSize;
1919 //
1920 // On output, next time profile buffer offset to copy.
1921 //
1922 *ProfileOffset = Offset;
1923}
1924
1940EFIAPI
1943 IN OUT UINT64 *ProfileSize,
1944 OUT VOID *ProfileBuffer
1945 )
1946{
1947 UINT64 Size;
1948 UINT64 Offset;
1949 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1950 BOOLEAN SmramProfileGettingStatus;
1951
1952 ContextData = GetSmramProfileContext ();
1953 if (ContextData == NULL) {
1954 return EFI_UNSUPPORTED;
1955 }
1956
1957 SmramProfileGettingStatus = mSmramProfileGettingStatus;
1958 mSmramProfileGettingStatus = TRUE;
1959
1960 Size = SmramProfileGetDataSize ();
1961
1962 if (*ProfileSize < Size) {
1963 *ProfileSize = Size;
1964 mSmramProfileGettingStatus = SmramProfileGettingStatus;
1965 return EFI_BUFFER_TOO_SMALL;
1966 }
1967
1968 Offset = 0;
1969 SmramProfileCopyData (ProfileBuffer, &Size, &Offset);
1970 *ProfileSize = Size;
1971
1972 mSmramProfileGettingStatus = SmramProfileGettingStatus;
1973 return EFI_SUCCESS;
1974}
1975
1992EFIAPI
1995 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1996 IN PHYSICAL_ADDRESS ImageBase,
1997 IN UINT64 ImageSize,
1998 IN EFI_FV_FILETYPE FileType
1999 )
2000{
2001 EFI_STATUS Status;
2003 VOID *EntryPointInImage;
2004 EFI_GUID *Name;
2005
2006 ZeroMem (&DriverEntry, sizeof (DriverEntry));
2007 Name = GetFileNameFromFilePath (FilePath);
2008 if (Name != NULL) {
2009 CopyMem (&DriverEntry.FileName, Name, sizeof (EFI_GUID));
2010 }
2011
2012 DriverEntry.ImageBuffer = ImageBase;
2013 DriverEntry.NumberOfPage = EFI_SIZE_TO_PAGES ((UINTN)ImageSize);
2014 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)DriverEntry.ImageBuffer, &EntryPointInImage);
2015 ASSERT_EFI_ERROR (Status);
2016 DriverEntry.ImageEntryPoint = (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
2017
2019}
2020
2036EFIAPI
2039 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
2040 IN PHYSICAL_ADDRESS ImageBase,
2041 IN UINT64 ImageSize
2042 )
2043{
2044 EFI_STATUS Status;
2046 VOID *EntryPointInImage;
2047 EFI_GUID *Name;
2048
2049 ZeroMem (&DriverEntry, sizeof (DriverEntry));
2050 Name = GetFileNameFromFilePath (FilePath);
2051 if (Name != NULL) {
2052 CopyMem (&DriverEntry.FileName, Name, sizeof (EFI_GUID));
2053 }
2054
2055 DriverEntry.ImageBuffer = ImageBase;
2056 DriverEntry.NumberOfPage = EFI_SIZE_TO_PAGES ((UINTN)ImageSize);
2057 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)DriverEntry.ImageBuffer, &EntryPointInImage);
2058 ASSERT_EFI_ERROR (Status);
2059 DriverEntry.ImageEntryPoint = (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
2060
2062}
2063
2076EFIAPI
2079 OUT BOOLEAN *RecordingState
2080 )
2081{
2082 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2083
2084 ContextData = GetSmramProfileContext ();
2085 if (ContextData == NULL) {
2086 return EFI_UNSUPPORTED;
2087 }
2088
2089 if (RecordingState == NULL) {
2090 return EFI_INVALID_PARAMETER;
2091 }
2092
2093 *RecordingState = mSmramProfileRecordingEnable;
2094 return EFI_SUCCESS;
2095}
2096
2108EFIAPI
2111 IN BOOLEAN RecordingState
2112 )
2113{
2114 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2115
2116 ContextData = GetSmramProfileContext ();
2117 if (ContextData == NULL) {
2118 return EFI_UNSUPPORTED;
2119 }
2120
2121 mSmramProfileRecordingEnable = RecordingState;
2122 return EFI_SUCCESS;
2123}
2124
2149EFIAPI
2152 IN PHYSICAL_ADDRESS CallerAddress,
2153 IN MEMORY_PROFILE_ACTION Action,
2154 IN EFI_MEMORY_TYPE MemoryType,
2155 IN VOID *Buffer,
2156 IN UINTN Size,
2157 IN CHAR8 *ActionString OPTIONAL
2158 )
2159{
2160 return SmmCoreUpdateProfile (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
2161}
2162
2169VOID
2171 IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO *SmramProfileParameterGetInfo
2172 )
2173{
2174 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2175 BOOLEAN SmramProfileGettingStatus;
2176
2177 ContextData = GetSmramProfileContext ();
2178 if (ContextData == NULL) {
2179 return;
2180 }
2181
2182 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2183 mSmramProfileGettingStatus = TRUE;
2184
2185 SmramProfileParameterGetInfo->ProfileSize = SmramProfileGetDataSize ();
2186 SmramProfileParameterGetInfo->Header.ReturnStatus = 0;
2187
2188 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2189}
2190
2197VOID
2199 IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA *SmramProfileParameterGetData
2200 )
2201{
2202 UINT64 ProfileSize;
2203 UINT64 ProfileOffset;
2204 SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA SmramProfileGetData;
2205 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2206 BOOLEAN SmramProfileGettingStatus;
2207
2208 ContextData = GetSmramProfileContext ();
2209 if (ContextData == NULL) {
2210 return;
2211 }
2212
2213 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2214 mSmramProfileGettingStatus = TRUE;
2215
2216 CopyMem (&SmramProfileGetData, SmramProfileParameterGetData, sizeof (SmramProfileGetData));
2217
2218 ProfileSize = SmramProfileGetDataSize ();
2219
2220 //
2221 // Sanity check
2222 //
2223 if (!SmmIsBufferOutsideSmmValid ((UINTN)SmramProfileGetData.ProfileBuffer, (UINTN)ProfileSize)) {
2224 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetData: SMM ProfileBuffer in SMRAM or overflow!\n"));
2225 SmramProfileParameterGetData->ProfileSize = ProfileSize;
2226 SmramProfileParameterGetData->Header.ReturnStatus = (UINT64)(INT64)(INTN)EFI_ACCESS_DENIED;
2227 goto Done;
2228 }
2229
2230 if (SmramProfileGetData.ProfileSize < ProfileSize) {
2231 SmramProfileParameterGetData->ProfileSize = ProfileSize;
2232 SmramProfileParameterGetData->Header.ReturnStatus = (UINT64)(INT64)(INTN)EFI_BUFFER_TOO_SMALL;
2233 goto Done;
2234 }
2235
2236 ProfileOffset = 0;
2237 SmramProfileCopyData ((VOID *)(UINTN)SmramProfileGetData.ProfileBuffer, &ProfileSize, &ProfileOffset);
2238 SmramProfileParameterGetData->ProfileSize = ProfileSize;
2239 SmramProfileParameterGetData->Header.ReturnStatus = 0;
2240
2241Done:
2242 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2243}
2244
2251VOID
2253 IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET *SmramProfileParameterGetDataByOffset
2254 )
2255{
2256 SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET SmramProfileGetDataByOffset;
2257 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2258 BOOLEAN SmramProfileGettingStatus;
2259
2260 ContextData = GetSmramProfileContext ();
2261 if (ContextData == NULL) {
2262 return;
2263 }
2264
2265 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2266 mSmramProfileGettingStatus = TRUE;
2267
2268 CopyMem (&SmramProfileGetDataByOffset, SmramProfileParameterGetDataByOffset, sizeof (SmramProfileGetDataByOffset));
2269
2270 //
2271 // Sanity check
2272 //
2273 if (!SmmIsBufferOutsideSmmValid ((UINTN)SmramProfileGetDataByOffset.ProfileBuffer, (UINTN)SmramProfileGetDataByOffset.ProfileSize)) {
2274 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetDataByOffset: SMM ProfileBuffer in SMRAM or overflow!\n"));
2275 SmramProfileParameterGetDataByOffset->Header.ReturnStatus = (UINT64)(INT64)(INTN)EFI_ACCESS_DENIED;
2276 goto Done;
2277 }
2278
2279 SmramProfileCopyData ((VOID *)(UINTN)SmramProfileGetDataByOffset.ProfileBuffer, &SmramProfileGetDataByOffset.ProfileSize, &SmramProfileGetDataByOffset.ProfileOffset);
2280 CopyMem (SmramProfileParameterGetDataByOffset, &SmramProfileGetDataByOffset, sizeof (SmramProfileGetDataByOffset));
2281 SmramProfileParameterGetDataByOffset->Header.ReturnStatus = 0;
2282
2283Done:
2284 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2285}
2286
2304EFIAPI
2306 IN EFI_HANDLE DispatchHandle,
2307 IN CONST VOID *Context OPTIONAL,
2308 IN OUT VOID *CommBuffer OPTIONAL,
2309 IN OUT UINTN *CommBufferSize OPTIONAL
2310 )
2311{
2312 SMRAM_PROFILE_PARAMETER_HEADER *SmramProfileParameterHeader;
2313 UINTN TempCommBufferSize;
2314 SMRAM_PROFILE_PARAMETER_RECORDING_STATE *ParameterRecordingState;
2315
2316 DEBUG ((DEBUG_ERROR, "SmramProfileHandler Enter\n"));
2317
2318 //
2319 // If input is invalid, stop processing this SMI
2320 //
2321 if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
2322 return EFI_SUCCESS;
2323 }
2324
2325 TempCommBufferSize = *CommBufferSize;
2326
2327 if (TempCommBufferSize < sizeof (SMRAM_PROFILE_PARAMETER_HEADER)) {
2328 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2329 return EFI_SUCCESS;
2330 }
2331
2332 if (mSmramReadyToLock && !SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
2333 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer in SMRAM or overflow!\n"));
2334 return EFI_SUCCESS;
2335 }
2336
2337 SmramProfileParameterHeader = (SMRAM_PROFILE_PARAMETER_HEADER *)((UINTN)CommBuffer);
2338
2339 SmramProfileParameterHeader->ReturnStatus = (UINT64)-1;
2340
2341 if (GetSmramProfileContext () == NULL) {
2342 SmramProfileParameterHeader->ReturnStatus = (UINT64)(INT64)(INTN)EFI_UNSUPPORTED;
2343 return EFI_SUCCESS;
2344 }
2345
2346 switch (SmramProfileParameterHeader->Command) {
2347 case SMRAM_PROFILE_COMMAND_GET_PROFILE_INFO:
2348 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetInfo\n"));
2349 if (TempCommBufferSize != sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO)) {
2350 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2351 return EFI_SUCCESS;
2352 }
2353
2355 break;
2356 case SMRAM_PROFILE_COMMAND_GET_PROFILE_DATA:
2357 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetData\n"));
2358 if (TempCommBufferSize != sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA)) {
2359 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2360 return EFI_SUCCESS;
2361 }
2362
2364 break;
2365 case SMRAM_PROFILE_COMMAND_GET_PROFILE_DATA_BY_OFFSET:
2366 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetDataByOffset\n"));
2367 if (TempCommBufferSize != sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET)) {
2368 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2369 return EFI_SUCCESS;
2370 }
2371
2373 break;
2374 case SMRAM_PROFILE_COMMAND_GET_RECORDING_STATE:
2375 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerGetRecordingState\n"));
2376 if (TempCommBufferSize != sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE)) {
2377 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2378 return EFI_SUCCESS;
2379 }
2380
2381 ParameterRecordingState = (SMRAM_PROFILE_PARAMETER_RECORDING_STATE *)(UINTN)CommBuffer;
2382 ParameterRecordingState->RecordingState = mSmramProfileRecordingEnable;
2383 ParameterRecordingState->Header.ReturnStatus = 0;
2384 break;
2385 case SMRAM_PROFILE_COMMAND_SET_RECORDING_STATE:
2386 DEBUG ((DEBUG_ERROR, "SmramProfileHandlerSetRecordingState\n"));
2387 if (TempCommBufferSize != sizeof (SMRAM_PROFILE_PARAMETER_RECORDING_STATE)) {
2388 DEBUG ((DEBUG_ERROR, "SmramProfileHandler: SMM communication buffer size invalid!\n"));
2389 return EFI_SUCCESS;
2390 }
2391
2392 ParameterRecordingState = (SMRAM_PROFILE_PARAMETER_RECORDING_STATE *)(UINTN)CommBuffer;
2393 mSmramProfileRecordingEnable = ParameterRecordingState->RecordingState;
2394 ParameterRecordingState->Header.ReturnStatus = 0;
2395 break;
2396
2397 //
2398 // Below 2 commands have been deprecated. They may not be (re-)used.
2399 //
2400 case SMRAM_PROFILE_COMMAND_DEPRECATED1:
2401 case SMRAM_PROFILE_COMMAND_DEPRECATED2:
2402 ASSERT (FALSE);
2403 //
2404 // Fall-through to the default (unrecognized command) case.
2405 //
2406 default:
2407 break;
2408 }
2409
2410 DEBUG ((DEBUG_ERROR, "SmramProfileHandler Exit\n"));
2411
2412 return EFI_SUCCESS;
2413}
2414
2419VOID
2421 VOID
2422 )
2423{
2424 EFI_STATUS Status;
2425 EFI_HANDLE DispatchHandle;
2426
2427 if (!IS_SMRAM_PROFILE_ENABLED) {
2428 return;
2429 }
2430
2431 Status = SmiHandlerRegister (
2433 &gEdkiiMemoryProfileGuid,
2434 &DispatchHandle
2435 );
2436 ASSERT_EFI_ERROR (Status);
2437}
2438
2440
2445VOID
2447 VOID
2448 )
2449{
2450 UINTN Index;
2451 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2452 BOOLEAN SmramProfileGettingStatus;
2453
2454 ContextData = GetSmramProfileContext ();
2455 if (ContextData == NULL) {
2456 return;
2457 }
2458
2459 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2460 mSmramProfileGettingStatus = TRUE;
2461
2462 DEBUG ((DEBUG_INFO, "FullSmramRange address - 0x%08x\n", mFullSmramRanges));
2463
2464 DEBUG ((DEBUG_INFO, "======= SmramProfile begin =======\n"));
2465
2466 DEBUG ((DEBUG_INFO, "FullSmramRange:\n"));
2467 for (Index = 0; Index < mFullSmramRangeCount; Index++) {
2468 DEBUG ((DEBUG_INFO, " FullSmramRange (0x%x)\n", Index));
2469 DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", mFullSmramRanges[Index].PhysicalStart));
2470 DEBUG ((DEBUG_INFO, " CpuStart - 0x%016lx\n", mFullSmramRanges[Index].CpuStart));
2471 DEBUG ((DEBUG_INFO, " PhysicalSize - 0x%016lx\n", mFullSmramRanges[Index].PhysicalSize));
2472 DEBUG ((DEBUG_INFO, " RegionState - 0x%016lx\n", mFullSmramRanges[Index].RegionState));
2473 }
2474
2475 DEBUG ((DEBUG_INFO, "======= SmramProfile end =======\n"));
2476
2477 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2478}
2479
2484VOID
2486 VOID
2487 )
2488{
2489 LIST_ENTRY *FreePageList;
2490 LIST_ENTRY *Node;
2491 FREE_PAGE_LIST *Pages;
2492 UINTN Index;
2493 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2494 BOOLEAN SmramProfileGettingStatus;
2495
2496 ContextData = GetSmramProfileContext ();
2497 if (ContextData == NULL) {
2498 return;
2499 }
2500
2501 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2502 mSmramProfileGettingStatus = TRUE;
2503
2504 DEBUG ((DEBUG_INFO, "======= SmramProfile begin =======\n"));
2505
2506 DEBUG ((DEBUG_INFO, "FreePagesList:\n"));
2507 FreePageList = &mSmmMemoryMap;
2508 for (Node = FreePageList->BackLink, Index = 0;
2509 Node != FreePageList;
2510 Node = Node->BackLink, Index++)
2511 {
2512 Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);
2513 DEBUG ((DEBUG_INFO, " Index - 0x%x\n", Index));
2514 DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", (PHYSICAL_ADDRESS)(UINTN)Pages));
2515 DEBUG ((DEBUG_INFO, " NumberOfPages - 0x%08x\n", Pages->NumberOfPages));
2516 }
2517
2518 DEBUG ((DEBUG_INFO, "======= SmramProfile end =======\n"));
2519
2520 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2521}
2522
2527VOID
2529 VOID
2530 )
2531{
2532 LIST_ENTRY *FreePoolList;
2533 LIST_ENTRY *Node;
2534 FREE_POOL_HEADER *Pool;
2535 UINTN Index;
2536 UINTN PoolListIndex;
2537 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2538 BOOLEAN SmramProfileGettingStatus;
2539 UINTN SmmPoolTypeIndex;
2540
2541 ContextData = GetSmramProfileContext ();
2542 if (ContextData == NULL) {
2543 return;
2544 }
2545
2546 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2547 mSmramProfileGettingStatus = TRUE;
2548
2549 DEBUG ((DEBUG_INFO, "======= SmramProfile begin =======\n"));
2550
2551 for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {
2552 for (PoolListIndex = 0; PoolListIndex < MAX_POOL_INDEX; PoolListIndex++) {
2553 DEBUG ((DEBUG_INFO, "FreePoolList(%d)(%d):\n", SmmPoolTypeIndex, PoolListIndex));
2554 FreePoolList = &mSmmPoolLists[SmmPoolTypeIndex][PoolListIndex];
2555 for (Node = FreePoolList->BackLink, Index = 0;
2556 Node != FreePoolList;
2557 Node = Node->BackLink, Index++)
2558 {
2559 Pool = BASE_CR (Node, FREE_POOL_HEADER, Link);
2560 DEBUG ((DEBUG_INFO, " Index - 0x%x\n", Index));
2561 DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", (PHYSICAL_ADDRESS)(UINTN)Pool));
2562 DEBUG ((DEBUG_INFO, " Size - 0x%08x\n", Pool->Header.Size));
2563 DEBUG ((DEBUG_INFO, " Available - 0x%02x\n", Pool->Header.Available));
2564 }
2565 }
2566 }
2567
2568 DEBUG ((DEBUG_INFO, "======= SmramProfile end =======\n"));
2569
2570 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2571}
2572
2573GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mSmmActionString[] = {
2574 "SmmUnknown",
2575 "gSmst->SmmAllocatePages",
2576 "gSmst->SmmFreePages",
2577 "gSmst->SmmAllocatePool",
2578 "gSmst->SmmFreePool",
2579};
2580
2581typedef struct {
2582 MEMORY_PROFILE_ACTION Action;
2583 CHAR8 *String;
2585
2586GLOBAL_REMOVE_IF_UNREFERENCED ACTION_STRING mExtActionString[] = {
2587 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES, "Lib:AllocatePages" },
2588 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES, "Lib:AllocateRuntimePages" },
2589 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES, "Lib:AllocateReservedPages" },
2590 { MEMORY_PROFILE_ACTION_LIB_FREE_PAGES, "Lib:FreePages" },
2591 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES, "Lib:AllocateAlignedPages" },
2592 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES, "Lib:AllocateAlignedRuntimePages" },
2593 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES, "Lib:AllocateAlignedReservedPages" },
2594 { MEMORY_PROFILE_ACTION_LIB_FREE_ALIGNED_PAGES, "Lib:FreeAlignedPages" },
2595 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL, "Lib:AllocatePool" },
2596 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL, "Lib:AllocateRuntimePool" },
2597 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL, "Lib:AllocateReservedPool" },
2598 { MEMORY_PROFILE_ACTION_LIB_FREE_POOL, "Lib:FreePool" },
2599 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL, "Lib:AllocateZeroPool" },
2600 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL, "Lib:AllocateRuntimeZeroPool" },
2601 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL, "Lib:AllocateReservedZeroPool" },
2602 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL, "Lib:AllocateCopyPool" },
2603 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL, "Lib:AllocateRuntimeCopyPool" },
2604 { MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL, "Lib:AllocateReservedCopyPool" },
2605 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL, "Lib:ReallocatePool" },
2606 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL, "Lib:ReallocateRuntimePool" },
2607 { MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL, "Lib:ReallocateReservedPool" },
2608};
2609
2610typedef struct {
2611 EFI_MEMORY_TYPE MemoryType;
2612 CHAR8 *MemoryTypeStr;
2614
2616 { EfiRuntimeServicesCode, "EfiRuntimeServicesCode" },
2617 { EfiRuntimeServicesData, "EfiRuntimeServicesData" }
2618};
2619
2628CHAR8 *
2630 IN EFI_MEMORY_TYPE MemoryType
2631 )
2632{
2633 UINTN Index;
2634
2635 for (Index = 0; Index < ARRAY_SIZE (mMemoryTypeString); Index++) {
2636 if (mMemoryTypeString[Index].MemoryType == MemoryType) {
2637 return mMemoryTypeString[Index].MemoryTypeStr;
2638 }
2639 }
2640
2641 return "UnexpectedMemoryType";
2642}
2643
2652CHAR8 *
2654 IN MEMORY_PROFILE_ACTION Action
2655 )
2656{
2657 UINTN Index;
2658 UINTN ActionStringCount;
2659 CHAR8 **ActionString;
2660
2661 ActionString = mSmmActionString;
2662 ActionStringCount = ARRAY_SIZE (mSmmActionString);
2663
2664 if ((UINTN)(UINT32)Action < ActionStringCount) {
2665 return ActionString[Action];
2666 }
2667
2668 for (Index = 0; Index < ARRAY_SIZE (mExtActionString); Index++) {
2669 if (mExtActionString[Index].Action == Action) {
2670 return mExtActionString[Index].String;
2671 }
2672 }
2673
2674 return ActionString[0];
2675}
2676
2681VOID
2683 VOID
2684 )
2685{
2686 MEMORY_PROFILE_CONTEXT *Context;
2687 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
2688 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
2689 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
2690 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
2691 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
2692 LIST_ENTRY *SmramDriverInfoList;
2693 UINTN DriverIndex;
2694 LIST_ENTRY *DriverLink;
2695 LIST_ENTRY *AllocInfoList;
2696 UINTN AllocIndex;
2697 LIST_ENTRY *AllocLink;
2698 BOOLEAN SmramProfileGettingStatus;
2699 UINTN TypeIndex;
2700
2701 ContextData = GetSmramProfileContext ();
2702 if (ContextData == NULL) {
2703 return;
2704 }
2705
2706 SmramProfileGettingStatus = mSmramProfileGettingStatus;
2707 mSmramProfileGettingStatus = TRUE;
2708
2709 Context = &ContextData->Context;
2710 DEBUG ((DEBUG_INFO, "======= SmramProfile begin =======\n"));
2711 DEBUG ((DEBUG_INFO, "MEMORY_PROFILE_CONTEXT\n"));
2712
2713 DEBUG ((DEBUG_INFO, " CurrentTotalUsage - 0x%016lx\n", Context->CurrentTotalUsage));
2714 DEBUG ((DEBUG_INFO, " PeakTotalUsage - 0x%016lx\n", Context->PeakTotalUsage));
2715 for (TypeIndex = 0; TypeIndex < sizeof (Context->CurrentTotalUsageByType) / sizeof (Context->CurrentTotalUsageByType[0]); TypeIndex++) {
2716 if ((Context->CurrentTotalUsageByType[TypeIndex] != 0) ||
2717 (Context->PeakTotalUsageByType[TypeIndex] != 0))
2718 {
2719 DEBUG ((DEBUG_INFO, " CurrentTotalUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, Context->CurrentTotalUsageByType[TypeIndex], ProfileMemoryTypeToStr (TypeIndex)));
2720 DEBUG ((DEBUG_INFO, " PeakTotalUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, Context->PeakTotalUsageByType[TypeIndex], ProfileMemoryTypeToStr (TypeIndex)));
2721 }
2722 }
2723
2724 DEBUG ((DEBUG_INFO, " TotalImageSize - 0x%016lx\n", Context->TotalImageSize));
2725 DEBUG ((DEBUG_INFO, " ImageCount - 0x%08x\n", Context->ImageCount));
2726 DEBUG ((DEBUG_INFO, " SequenceCount - 0x%08x\n", Context->SequenceCount));
2727
2728 SmramDriverInfoList = ContextData->DriverInfoList;
2729 for (DriverLink = SmramDriverInfoList->ForwardLink, DriverIndex = 0;
2730 DriverLink != SmramDriverInfoList;
2731 DriverLink = DriverLink->ForwardLink, DriverIndex++)
2732 {
2733 DriverInfoData = CR (
2734 DriverLink,
2736 Link,
2737 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
2738 );
2739 DriverInfo = &DriverInfoData->DriverInfo;
2740 DEBUG ((DEBUG_INFO, " MEMORY_PROFILE_DRIVER_INFO (0x%x)\n", DriverIndex));
2741 DEBUG ((DEBUG_INFO, " FileName - %g\n", &DriverInfo->FileName));
2742 DEBUG ((DEBUG_INFO, " ImageBase - 0x%016lx\n", DriverInfo->ImageBase));
2743 DEBUG ((DEBUG_INFO, " ImageSize - 0x%016lx\n", DriverInfo->ImageSize));
2744 DEBUG ((DEBUG_INFO, " EntryPoint - 0x%016lx\n", DriverInfo->EntryPoint));
2745 DEBUG ((DEBUG_INFO, " ImageSubsystem - 0x%04x\n", DriverInfo->ImageSubsystem));
2746 DEBUG ((DEBUG_INFO, " FileType - 0x%02x\n", DriverInfo->FileType));
2747 DEBUG ((DEBUG_INFO, " CurrentUsage - 0x%016lx\n", DriverInfo->CurrentUsage));
2748 DEBUG ((DEBUG_INFO, " PeakUsage - 0x%016lx\n", DriverInfo->PeakUsage));
2749 for (TypeIndex = 0; TypeIndex < sizeof (DriverInfo->CurrentUsageByType) / sizeof (DriverInfo->CurrentUsageByType[0]); TypeIndex++) {
2750 if ((DriverInfo->CurrentUsageByType[TypeIndex] != 0) ||
2751 (DriverInfo->PeakUsageByType[TypeIndex] != 0))
2752 {
2753 DEBUG ((DEBUG_INFO, " CurrentUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, DriverInfo->CurrentUsageByType[TypeIndex], ProfileMemoryTypeToStr (TypeIndex)));
2754 DEBUG ((DEBUG_INFO, " PeakUsage[0x%02x] - 0x%016lx (%a)\n", TypeIndex, DriverInfo->PeakUsageByType[TypeIndex], ProfileMemoryTypeToStr (TypeIndex)));
2755 }
2756 }
2757
2758 DEBUG ((DEBUG_INFO, " AllocRecordCount - 0x%08x\n", DriverInfo->AllocRecordCount));
2759
2760 AllocInfoList = DriverInfoData->AllocInfoList;
2761 for (AllocLink = AllocInfoList->ForwardLink, AllocIndex = 0;
2762 AllocLink != AllocInfoList;
2763 AllocLink = AllocLink->ForwardLink, AllocIndex++)
2764 {
2765 AllocInfoData = CR (
2766 AllocLink,
2768 Link,
2769 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
2770 );
2771 AllocInfo = &AllocInfoData->AllocInfo;
2772 DEBUG ((DEBUG_INFO, " MEMORY_PROFILE_ALLOC_INFO (0x%x)\n", AllocIndex));
2773 DEBUG ((DEBUG_INFO, " CallerAddress - 0x%016lx (Offset: 0x%08x)\n", AllocInfo->CallerAddress, AllocInfo->CallerAddress - DriverInfo->ImageBase));
2774 DEBUG ((DEBUG_INFO, " SequenceId - 0x%08x\n", AllocInfo->SequenceId));
2775 if ((AllocInfo->Action & MEMORY_PROFILE_ACTION_USER_DEFINED_MASK) != 0) {
2776 if (AllocInfoData->ActionString != NULL) {
2777 DEBUG ((DEBUG_INFO, " Action - 0x%08x (%a)\n", AllocInfo->Action, AllocInfoData->ActionString));
2778 } else {
2779 DEBUG ((DEBUG_INFO, " Action - 0x%08x (UserDefined-0x%08x)\n", AllocInfo->Action, AllocInfo->Action));
2780 }
2781 } else {
2782 DEBUG ((DEBUG_INFO, " Action - 0x%08x (%a)\n", AllocInfo->Action, ProfileActionToStr (AllocInfo->Action)));
2783 }
2784
2785 DEBUG ((DEBUG_INFO, " MemoryType - 0x%08x (%a)\n", AllocInfo->MemoryType, ProfileMemoryTypeToStr (AllocInfo->MemoryType)));
2786 DEBUG ((DEBUG_INFO, " Buffer - 0x%016lx\n", AllocInfo->Buffer));
2787 DEBUG ((DEBUG_INFO, " Size - 0x%016lx\n", AllocInfo->Size));
2788 }
2789 }
2790
2791 DEBUG ((DEBUG_INFO, "======= SmramProfile end =======\n"));
2792
2793 mSmramProfileGettingStatus = SmramProfileGettingStatus;
2794}
2795
2800VOID
2802 VOID
2803 )
2804{
2806 if (IS_SMRAM_PROFILE_ENABLED) {
2810 DumpSmramRange ();
2811 }
2812
2813 DEBUG_CODE_END ();
2814}
UINT64 UINTN
INT64 INTN
LIST_ENTRY *EFIAPI RemoveEntryList(IN CONST LIST_ENTRY *Entry)
Definition: LinkedList.c:590
LIST_ENTRY *EFIAPI InitializeListHead(IN OUT LIST_ENTRY *ListHead)
Definition: LinkedList.c:182
#define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)
Definition: BaseLib.h:2884
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
UINTN EFIAPI AsciiStrSize(IN CONST CHAR8 *String)
Definition: String.c:681
LIST_ENTRY *EFIAPI InsertTailList(IN OUT LIST_ENTRY *ListHead, IN OUT LIST_ENTRY *Entry)
Definition: LinkedList.c:259
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID FreeMemory(VOID)
Definition: Compress.c:216
UINTN EFIAPI DevicePathNodeLength(IN CONST VOID *Node)
UINT8 EFIAPI DevicePathSubType(IN CONST VOID *Node)
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
BOOLEAN EFIAPI IsDevicePathValid(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN UINTN MaxSize)
UINTN EFIAPI GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
BOOLEAN EFIAPI IsDevicePathEndType(IN CONST VOID *Node)
VOID EFIAPI SetDevicePathEndNode(OUT VOID *Node)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_STATUS EFIAPI SmmInstallProtocolInterface(IN OUT EFI_HANDLE *UserHandle, IN EFI_GUID *Protocol, IN EFI_INTERFACE_TYPE InterfaceType, IN VOID *Interface)
Definition: Handle.c:174
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define VOID
Definition: Base.h:269
#define RETURN_SUCCESS
Definition: Base.h:1066
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
#define BASE_CR(Record, TYPE, Field)
Definition: Base.h:891
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:463
#define DEBUG_CODE_BEGIN()
Definition: DebugLib.h:565
#define DEBUG(Expression)
Definition: DebugLib.h:435
#define DEBUG_CODE_END()
Definition: DebugLib.h:579
#define CR(Record, TYPE, Field, TestSignature)
Definition: DebugLib.h:660
#define PcdGetSize(TokenName)
Definition: PcdLib.h:440
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
#define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: PeImage.h:142
#define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: PeImage.h:193
EFI_STATUS EFIAPI SmmInternalFreePool(IN VOID *Buffer)
Definition: Pool.c:376
EFI_STATUS EFIAPI SmmInternalAllocatePool(IN EFI_MEMORY_TYPE PoolType, IN UINTN Size, OUT VOID **Buffer)
Definition: Pool.c:239
EFI_STATUS EFIAPI SmiHandlerRegister(IN EFI_SMM_HANDLER_ENTRY_POINT2 Handler, IN CONST EFI_GUID *HandlerType OPTIONAL, OUT EFI_HANDLE *DispatchHandle)
Definition: Smi.c:334
BOOLEAN EFIAPI SmmIsBufferOutsideSmmValid(IN EFI_PHYSICAL_ADDRESS Buffer, IN UINT64 Length)
Definition: SmmMemLib.c:114
BOOLEAN RegisterSmmCore(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData)
VOID SmramProfileReadyToLock(VOID)
EFI_GUID * GetFileNameFromFilePath(IN EFI_DEVICE_PATH_PROTOCOL *FilePath)
CHAR8 * ProfileActionToStr(IN MEMORY_PROFILE_ACTION Action)
EFI_MEMORY_TYPE GetProfileMemoryIndex(IN EFI_MEMORY_TYPE MemoryType)
CHAR8 * ProfileMemoryTypeToStr(IN EFI_MEMORY_TYPE MemoryType)
VOID SmramProfileInstallProtocol(VOID)
VOID RegisterImageToDxe(IN EFI_GUID *FileName, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN EFI_FV_FILETYPE FileType)
EFI_STATUS EFIAPI SmramProfileProtocolGetData(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, IN OUT UINT64 *ProfileSize, OUT VOID *ProfileBuffer)
UINT16 InternalPeCoffGetSubsystem(IN VOID *Pe32Data)
MEMORY_PROFILE_ALLOC_INFO_DATA * GetMemoryProfileAllocInfoFromAddress(IN MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData, IN MEMORY_PROFILE_ACTION BasicAction, IN UINTN Size, IN VOID *Buffer)
BOOLEAN SmmCoreNeedRecordProfile(IN EFI_MEMORY_TYPE MemoryType)
EFI_STATUS SmmCoreUpdateProfileFree(IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN UINTN Size, IN VOID *Buffer)
VOID DumpFreePoolList(VOID)
VOID DumpSmramRange(VOID)
VOID RegisterSmramProfileHandler(VOID)
VOID SmramProfileHandlerGetData(IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA *SmramProfileParameterGetData)
EFI_STATUS EFIAPI SmramProfileProtocolRecord(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN EFI_MEMORY_TYPE MemoryType, IN VOID *Buffer, IN UINTN Size, IN CHAR8 *ActionString OPTIONAL)
VOID UnregisterImageFromDxe(IN EFI_GUID *FileName, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize)
MEMORY_PROFILE_DRIVER_INFO_DATA * GetMemoryProfileDriverInfoFromAddress(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData, IN PHYSICAL_ADDRESS Address)
MEMORY_PROFILE_DRIVER_INFO_DATA * GetMemoryProfileDriverInfoByFileNameAndAddress(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData, IN EFI_GUID *FileName, IN PHYSICAL_ADDRESS Address)
EFI_STATUS UnregisterSmramProfileImage(IN EFI_SMM_DRIVER_ENTRY *DriverEntry, IN BOOLEAN UnregisterFromDxe)
EFI_STATUS EFIAPI SmramProfileProtocolUnregisterImage(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, IN EFI_DEVICE_PATH_PROTOCOL *FilePath, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize)
UINTN SmramProfileGetDataSize(VOID)
VOID DumpFreePagesList(VOID)
VOID SmramProfileInit(VOID)
VOID SmramProfileHandlerGetDataByOffset(IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA_BY_OFFSET *SmramProfileParameterGetDataByOffset)
VOID DumpSmramProfile(VOID)
BOOLEAN NeedRecordThisDriver(IN EFI_DEVICE_PATH_PROTOCOL *DriverFilePath)
VOID SmramProfileUpdateFreePages(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData)
EFI_STATUS EFIAPI SmramProfileHandler(IN EFI_HANDLE DispatchHandle, IN CONST VOID *Context OPTIONAL, IN OUT VOID *CommBuffer OPTIONAL, IN OUT UINTN *CommBufferSize OPTIONAL)
MEMORY_PROFILE_DRIVER_INFO_DATA * BuildDriverInfo(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData, IN EFI_GUID *FileName, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN PHYSICAL_ADDRESS EntryPoint, IN UINT16 ImageSubsystem, IN EFI_FV_FILETYPE FileType)
EFI_STATUS EFIAPI SmramProfileProtocolRegisterImage(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, IN EFI_DEVICE_PATH_PROTOCOL *FilePath, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN EFI_FV_FILETYPE FileType)
EFI_STATUS EFIAPI SmmCoreUpdateProfile(IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Size, IN VOID *Buffer, IN CHAR8 *ActionString OPTIONAL)
EFI_STATUS EFIAPI SmramProfileProtocolGetRecordingState(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, OUT BOOLEAN *RecordingState)
MEMORY_PROFILE_CONTEXT_DATA * GetSmramProfileContext(VOID)
RETURN_STATUS InternalPeCoffGetEntryPoint(IN VOID *Pe32Data, OUT VOID **EntryPoint)
VOID SmramProfileCopyData(OUT VOID *ProfileBuffer, IN OUT UINT64 *ProfileSize, IN OUT UINT64 *ProfileOffset)
EFI_STATUS SmmCoreUpdateProfileAllocate(IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Size, IN VOID *Buffer, IN CHAR8 *ActionString OPTIONAL)
EFI_STATUS EFIAPI SmramProfileProtocolSetRecordingState(IN EDKII_SMM_MEMORY_PROFILE_PROTOCOL *This, IN BOOLEAN RecordingState)
VOID SmramProfileHandlerGetInfo(IN SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO *SmramProfileParameterGetInfo)
EFI_STATUS RegisterSmramProfileImage(IN EFI_SMM_DRIVER_ENTRY *DriverEntry, IN BOOLEAN RegisterToDxe)
VOID DumpSmramInfo(VOID)
EFI_STATUS EFIAPI DriverEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: TdTcg2Dxe.c:2387
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
VOID EFIAPI EfiInitializeFwVolDevicepathNode(IN OUT MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode, IN CONST EFI_GUID *NameGuid)
Definition: UefiNotTiano.c:325
EFI_GUID *EFIAPI EfiGetNameGuidFromFwVolDevicePathNode(IN CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvDevicePathNode)
Definition: UefiNotTiano.c:292
EFI_MEMORY_TYPE
@ EfiRuntimeServicesCode
@ EfiRuntimeServicesData
@ EFI_NATIVE_INTERFACE
Definition: UefiSpec.h:1203
UINT32 e_lfanew
File address of new exe header.
Definition: PeImage.h:75
UINT16 e_magic
Magic number.
Definition: PeImage.h:57
EFI_PHYSICAL_ADDRESS PhysicalStart
Definition: PiMultiPhase.h:130
UINT32 AddressOfEntryPoint
Offset to entry point – from original optional header.
Definition: PeImage.h:785
UINT16 Signature
The signature for TE format = "VZ".
Definition: PeImage.h:780
UINT8 Subsystem
From original optional header.
Definition: PeImage.h:783
UINT16 StrippedSize
Number of bytes we removed from the header.
Definition: PeImage.h:784
Definition: Base.h:213