TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryProfileRecord.c
Go to the documentation of this file.
1
9#include "DxeMain.h"
10#include "Imem.h"
11
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
40 MEMORY_PROFILE_CONTEXT_SIGNATURE,
41 {
42 {
43 MEMORY_PROFILE_CONTEXT_SIGNATURE,
45 MEMORY_PROFILE_CONTEXT_REVISION
46 },
47 0,
48 0,
49 { 0 },
50 { 0 },
51 0,
52 0,
53 0
54 },
55 &mImageQueue,
56};
58
60GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mMemoryProfileGettingStatus = FALSE;
61GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mMemoryProfileRecordingEnable = MEMORY_PROFILE_RECORDING_DISABLE;
63GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMemoryProfileDriverPathSize;
64
80EFIAPI
83 IN OUT UINT64 *ProfileSize,
84 OUT VOID *ProfileBuffer
85 );
86
103EFIAPI
107 IN PHYSICAL_ADDRESS ImageBase,
108 IN UINT64 ImageSize,
109 IN EFI_FV_FILETYPE FileType
110 );
111
127EFIAPI
131 IN PHYSICAL_ADDRESS ImageBase,
132 IN UINT64 ImageSize
133 );
134
147EFIAPI
150 OUT BOOLEAN *RecordingState
151 );
152
164EFIAPI
167 IN BOOLEAN RecordingState
168 );
169
194EFIAPI
197 IN PHYSICAL_ADDRESS CallerAddress,
198 IN MEMORY_PROFILE_ACTION Action,
199 IN EFI_MEMORY_TYPE MemoryType,
200 IN VOID *Buffer,
201 IN UINTN Size,
202 IN CHAR8 *ActionString OPTIONAL
203 );
204
212};
213
217VOID
219 VOID
220 )
221{
222 CoreAcquireLock (&mMemoryProfileLock);
223}
224
228VOID
230 VOID
231 )
232{
233 CoreReleaseLock (&mMemoryProfileLock);
234}
235
244 VOID
245 )
246{
247 return mMemoryProfileContextPtr;
248}
249
259UINT16
261 IN VOID *Pe32Data
262 )
263{
265 EFI_IMAGE_DOS_HEADER *DosHdr;
266 UINT16 Magic;
267
268 ASSERT (Pe32Data != NULL);
269
270 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
271 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
272 //
273 // DOS image header is present, so read the PE header after the DOS image header.
274 //
275 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
276 } else {
277 //
278 // DOS image header is not present, so PE header is at the image base.
279 //
280 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
281 }
282
283 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
284 return Hdr.Te->Subsystem;
285 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
286 Magic = Hdr.Pe32->OptionalHeader.Magic;
288 return Hdr.Pe32->OptionalHeader.Subsystem;
289 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
290 return Hdr.Pe32Plus->OptionalHeader.Subsystem;
291 }
292 }
293
294 return 0x0000;
295}
296
314RETURN_STATUS
316 IN VOID *Pe32Data,
317 OUT VOID **EntryPoint
318 )
319{
320 EFI_IMAGE_DOS_HEADER *DosHdr;
322
323 ASSERT (Pe32Data != NULL);
324 ASSERT (EntryPoint != NULL);
325
326 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
327 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
328 //
329 // DOS image header is present, so read the PE header after the DOS image header.
330 //
331 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHdr->e_lfanew) & 0x0ffff));
332 } else {
333 //
334 // DOS image header is not present, so PE header is at the image base.
335 //
336 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
337 }
338
339 //
340 // Calculate the entry point relative to the start of the image.
341 // AddressOfEntryPoint is common for PE32 & PE32+
342 //
343 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
344 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);
345 return RETURN_SUCCESS;
346 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
347 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));
348 return RETURN_SUCCESS;
349 }
350
351 return RETURN_UNSUPPORTED;
352}
353
370 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
371 IN EFI_GUID *FileName,
372 IN PHYSICAL_ADDRESS ImageBase,
373 IN UINT64 ImageSize,
374 IN PHYSICAL_ADDRESS EntryPoint,
375 IN UINT16 ImageSubsystem,
376 IN EFI_FV_FILETYPE FileType
377 )
378{
379 EFI_STATUS Status;
380 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
381 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
382 VOID *EntryPointInImage;
383 CHAR8 *PdbString;
384 UINTN PdbSize;
385 UINTN PdbOccupiedSize;
386
387 PdbSize = 0;
388 PdbOccupiedSize = 0;
389 PdbString = NULL;
390 if (ImageBase != 0) {
391 PdbString = PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageBase);
392 if (PdbString != NULL) {
393 PdbSize = AsciiStrSize (PdbString);
394 PdbOccupiedSize = GET_OCCUPIED_SIZE (PdbSize, sizeof (UINT64));
395 }
396 }
397
398 //
399 // Use CoreInternalAllocatePool() that will not update profile for this AllocatePool action.
400 //
401 Status = CoreInternalAllocatePool (
403 sizeof (*DriverInfoData) + sizeof (LIST_ENTRY) + PdbSize,
404 (VOID **)&DriverInfoData
405 );
406 if (EFI_ERROR (Status)) {
407 return NULL;
408 }
409
410 ASSERT (DriverInfoData != NULL);
411
412 ZeroMem (DriverInfoData, sizeof (*DriverInfoData));
413
414 DriverInfo = &DriverInfoData->DriverInfo;
415 DriverInfoData->Signature = MEMORY_PROFILE_DRIVER_INFO_SIGNATURE;
416 DriverInfo->Header.Signature = MEMORY_PROFILE_DRIVER_INFO_SIGNATURE;
417 DriverInfo->Header.Length = (UINT16)(sizeof (MEMORY_PROFILE_DRIVER_INFO) + PdbOccupiedSize);
418 DriverInfo->Header.Revision = MEMORY_PROFILE_DRIVER_INFO_REVISION;
419 if (FileName != NULL) {
420 CopyMem (&DriverInfo->FileName, FileName, sizeof (EFI_GUID));
421 }
422
423 DriverInfo->ImageBase = ImageBase;
424 DriverInfo->ImageSize = ImageSize;
425 DriverInfo->EntryPoint = EntryPoint;
426 DriverInfo->ImageSubsystem = ImageSubsystem;
427 if ((EntryPoint != 0) && ((EntryPoint < ImageBase) || (EntryPoint >= (ImageBase + ImageSize)))) {
428 //
429 // If the EntryPoint is not in the range of image buffer, it should come from emulation environment.
430 // So patch ImageBuffer here to align the EntryPoint.
431 //
432 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageBase, &EntryPointInImage);
433 ASSERT_EFI_ERROR (Status);
434 DriverInfo->ImageBase = ImageBase + EntryPoint - (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
435 }
436
437 DriverInfo->FileType = FileType;
438 DriverInfoData->AllocInfoList = (LIST_ENTRY *)(DriverInfoData + 1);
439 InitializeListHead (DriverInfoData->AllocInfoList);
440 DriverInfo->CurrentUsage = 0;
441 DriverInfo->PeakUsage = 0;
442 DriverInfo->AllocRecordCount = 0;
443 if (PdbSize != 0) {
444 DriverInfo->PdbStringOffset = (UINT16)sizeof (MEMORY_PROFILE_DRIVER_INFO);
445 DriverInfoData->PdbString = (CHAR8 *)(DriverInfoData->AllocInfoList + 1);
446 CopyMem (DriverInfoData->PdbString, PdbString, PdbSize);
447 } else {
448 DriverInfo->PdbStringOffset = 0;
449 DriverInfoData->PdbString = NULL;
450 }
451
452 InsertTailList (ContextData->DriverInfoList, &DriverInfoData->Link);
453 ContextData->Context.ImageCount++;
454 ContextData->Context.TotalImageSize += DriverInfo->ImageSize;
455
456 return DriverInfoData;
457}
458
468BOOLEAN
470 IN EFI_DEVICE_PATH_PROTOCOL *DriverFilePath
471 )
472{
473 EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;
474 EFI_DEVICE_PATH_PROTOCOL *DevicePathInstance;
475 UINTN DevicePathSize;
476 UINTN FilePathSize;
477
478 if (!IsDevicePathValid (mMemoryProfileDriverPath, mMemoryProfileDriverPathSize)) {
479 //
480 // Invalid Device Path means record all.
481 //
482 return TRUE;
483 }
484
485 //
486 // Record FilePath without END node.
487 //
488 FilePathSize = GetDevicePathSize (DriverFilePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);
489
490 DevicePathInstance = mMemoryProfileDriverPath;
491 do {
492 //
493 // Find END node (it might be END_ENTIRE or END_INSTANCE).
494 //
495 TmpDevicePath = DevicePathInstance;
496 while (!IsDevicePathEndType (TmpDevicePath)) {
497 TmpDevicePath = NextDevicePathNode (TmpDevicePath);
498 }
499
500 //
501 // Do not compare END node.
502 //
503 DevicePathSize = (UINTN)TmpDevicePath - (UINTN)DevicePathInstance;
504 if ((FilePathSize == DevicePathSize) &&
505 (CompareMem (DriverFilePath, DevicePathInstance, DevicePathSize) == 0))
506 {
507 return TRUE;
508 }
509
510 //
511 // Get next instance.
512 //
513 DevicePathInstance = (EFI_DEVICE_PATH_PROTOCOL *)((UINTN)DevicePathInstance + DevicePathSize + DevicePathNodeLength (TmpDevicePath));
514 } while (DevicePathSubType (TmpDevicePath) != END_ENTIRE_DEVICE_PATH_SUBTYPE);
515
516 return FALSE;
517}
518
529BOOLEAN
531 IN VOID *HobStart,
533 )
534{
535 EFI_PEI_HOB_POINTERS DxeCoreHob;
536 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
537 PHYSICAL_ADDRESS ImageBase;
538 UINT8 TempBuffer[sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + sizeof (EFI_DEVICE_PATH_PROTOCOL)];
540
541 ASSERT (ContextData != NULL);
542
543 //
544 // Searching for image hob
545 //
546 DxeCoreHob.Raw = HobStart;
547 while ((DxeCoreHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, DxeCoreHob.Raw)) != NULL) {
548 if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {
549 //
550 // Find Dxe Core HOB
551 //
552 if (CompareGuid (&DxeCoreHob.MemoryAllocationModule->ModuleName, &gEfiCallerIdGuid)) {
553 break;
554 }
555 }
556
557 DxeCoreHob.Raw = GET_NEXT_HOB (DxeCoreHob);
558 }
559
560 ASSERT (DxeCoreHob.Raw != NULL);
561
562 FilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)TempBuffer;
563 EfiInitializeFwVolDevicepathNode (FilePath, &DxeCoreHob.MemoryAllocationModule->ModuleName);
564 SetDevicePathEndNode (FilePath + 1);
565
567 return FALSE;
568 }
569
570 ImageBase = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryBaseAddress;
571 DriverInfoData = BuildDriverInfo (
572 ContextData,
573 &DxeCoreHob.MemoryAllocationModule->ModuleName,
574 ImageBase,
575 DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength,
576 DxeCoreHob.MemoryAllocationModule->EntryPoint,
577 InternalPeCoffGetSubsystem ((VOID *)(UINTN)ImageBase),
578 EFI_FV_FILETYPE_DXE_CORE
579 );
580 if (DriverInfoData == NULL) {
581 return FALSE;
582 }
583
584 return TRUE;
585}
586
593VOID
595 IN VOID *HobStart
596 )
597{
598 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
599
600 if (!IS_UEFI_MEMORY_PROFILE_ENABLED) {
601 return;
602 }
603
604 ContextData = GetMemoryProfileContext ();
605 if (ContextData != NULL) {
606 return;
607 }
608
609 mMemoryProfileGettingStatus = FALSE;
610 if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT7) != 0) {
611 mMemoryProfileRecordingEnable = MEMORY_PROFILE_RECORDING_DISABLE;
612 } else {
613 mMemoryProfileRecordingEnable = MEMORY_PROFILE_RECORDING_ENABLE;
614 }
615
616 mMemoryProfileDriverPathSize = PcdGetSize (PcdMemoryProfileDriverPath);
617 mMemoryProfileDriverPath = AllocateCopyPool (mMemoryProfileDriverPathSize, PcdGetPtr (PcdMemoryProfileDriverPath));
618 mMemoryProfileContextPtr = &mMemoryProfileContext;
619
620 RegisterDxeCore (HobStart, &mMemoryProfileContext);
621
622 DEBUG ((DEBUG_INFO, "MemoryProfileInit MemoryProfileContext - 0x%x\n", &mMemoryProfileContext));
623}
624
629VOID
631 VOID
632 )
633{
634 EFI_HANDLE Handle;
635 EFI_STATUS Status;
636
637 if (!IS_UEFI_MEMORY_PROFILE_ENABLED) {
638 return;
639 }
640
641 Handle = NULL;
643 &Handle,
644 &gEdkiiMemoryProfileGuid,
645 &mProfileProtocol,
646 NULL
647 );
648 ASSERT_EFI_ERROR (Status);
649}
650
659EFI_GUID *
662 )
663{
665 EFI_GUID *FileName;
666
667 FileName = NULL;
668 if (FilePath != NULL) {
669 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)FilePath;
670 while (!IsDevicePathEnd (ThisFilePath)) {
671 FileName = EfiGetNameGuidFromFwVolDevicePathNode (ThisFilePath);
672 if (FileName != NULL) {
673 break;
674 }
675
676 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)NextDevicePathNode (ThisFilePath);
677 }
678 }
679
680 return FileName;
681}
682
698 IN EFI_FV_FILETYPE FileType
699 )
700{
701 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
702 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
703
704 if (!IS_UEFI_MEMORY_PROFILE_ENABLED) {
705 return EFI_UNSUPPORTED;
706 }
707
708 if (!NeedRecordThisDriver (DriverEntry->Info.FilePath)) {
709 return EFI_UNSUPPORTED;
710 }
711
712 ContextData = GetMemoryProfileContext ();
713 if (ContextData == NULL) {
714 return EFI_UNSUPPORTED;
715 }
716
717 DriverInfoData = BuildDriverInfo (
718 ContextData,
719 GetFileNameFromFilePath (DriverEntry->Info.FilePath),
720 DriverEntry->ImageContext.ImageAddress,
721 DriverEntry->ImageContext.ImageSize,
722 DriverEntry->ImageContext.EntryPoint,
723 DriverEntry->ImageContext.ImageType,
724 FileType
725 );
726 if (DriverInfoData == NULL) {
727 return EFI_OUT_OF_RESOURCES;
728 }
729
730 return EFI_SUCCESS;
731}
732
745 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
746 IN EFI_GUID *FileName,
747 IN PHYSICAL_ADDRESS Address
748 )
749{
750 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
751 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
752 LIST_ENTRY *DriverLink;
753 LIST_ENTRY *DriverInfoList;
754
755 DriverInfoList = ContextData->DriverInfoList;
756
757 for (DriverLink = DriverInfoList->ForwardLink;
758 DriverLink != DriverInfoList;
759 DriverLink = DriverLink->ForwardLink)
760 {
761 DriverInfoData = CR (
762 DriverLink,
764 Link,
765 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
766 );
767 DriverInfo = &DriverInfoData->DriverInfo;
768 if ((CompareGuid (&DriverInfo->FileName, FileName)) &&
769 (Address >= DriverInfo->ImageBase) &&
770 (Address < (DriverInfo->ImageBase + DriverInfo->ImageSize)))
771 {
772 return DriverInfoData;
773 }
774 }
775
776 return NULL;
777}
778
791 IN MEMORY_PROFILE_CONTEXT_DATA *ContextData,
792 IN PHYSICAL_ADDRESS Address
793 )
794{
795 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
796 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
797 LIST_ENTRY *DriverLink;
798 LIST_ENTRY *DriverInfoList;
799
800 DriverInfoList = ContextData->DriverInfoList;
801
802 for (DriverLink = DriverInfoList->ForwardLink;
803 DriverLink != DriverInfoList;
804 DriverLink = DriverLink->ForwardLink)
805 {
806 DriverInfoData = CR (
807 DriverLink,
809 Link,
810 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
811 );
812 DriverInfo = &DriverInfoData->DriverInfo;
813 if ((Address >= DriverInfo->ImageBase) &&
814 (Address < (DriverInfo->ImageBase + DriverInfo->ImageSize)))
815 {
816 return DriverInfoData;
817 }
818 }
819
820 return NULL;
821}
822
837 )
838{
839 EFI_STATUS Status;
840 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
841 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
842 EFI_GUID *FileName;
843 PHYSICAL_ADDRESS ImageAddress;
844 VOID *EntryPointInImage;
845
846 if (!IS_UEFI_MEMORY_PROFILE_ENABLED) {
847 return EFI_UNSUPPORTED;
848 }
849
850 if (!NeedRecordThisDriver (DriverEntry->Info.FilePath)) {
851 return EFI_UNSUPPORTED;
852 }
853
854 ContextData = GetMemoryProfileContext ();
855 if (ContextData == NULL) {
856 return EFI_UNSUPPORTED;
857 }
858
859 DriverInfoData = NULL;
860 FileName = GetFileNameFromFilePath (DriverEntry->Info.FilePath);
861 ImageAddress = DriverEntry->ImageContext.ImageAddress;
862 if ((DriverEntry->ImageContext.EntryPoint < ImageAddress) || (DriverEntry->ImageContext.EntryPoint >= (ImageAddress + DriverEntry->ImageContext.ImageSize))) {
863 //
864 // If the EntryPoint is not in the range of image buffer, it should come from emulation environment.
865 // So patch ImageAddress here to align the EntryPoint.
866 //
867 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageAddress, &EntryPointInImage);
868 ASSERT_EFI_ERROR (Status);
869 ImageAddress = ImageAddress + (UINTN)DriverEntry->ImageContext.EntryPoint - (UINTN)EntryPointInImage;
870 }
871
872 if (FileName != NULL) {
873 DriverInfoData = GetMemoryProfileDriverInfoByFileNameAndAddress (ContextData, FileName, ImageAddress);
874 }
875
876 if (DriverInfoData == NULL) {
877 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, ImageAddress);
878 }
879
880 if (DriverInfoData == NULL) {
881 return EFI_NOT_FOUND;
882 }
883
884 ContextData->Context.TotalImageSize -= DriverInfoData->DriverInfo.ImageSize;
885
886 // Keep the ImageBase for RVA calculation in Application.
887 // DriverInfoData->DriverInfo.ImageBase = 0;
888 DriverInfoData->DriverInfo.ImageSize = 0;
889
890 if (DriverInfoData->DriverInfo.PeakUsage == 0) {
891 ContextData->Context.ImageCount--;
892 RemoveEntryList (&DriverInfoData->Link);
893 //
894 // Use CoreInternalFreePool() that will not update profile for this FreePool action.
895 //
896 CoreInternalFreePool (DriverInfoData, NULL);
897 }
898
899 return EFI_SUCCESS;
900}
901
914BOOLEAN
916 IN EFI_MEMORY_TYPE MemoryType
917 )
918{
919 UINT64 TestBit;
920
921 if ((UINT32)MemoryType >= MEMORY_TYPE_OS_RESERVED_MIN) {
922 TestBit = BIT63;
923 } else if ((UINT32)MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
924 TestBit = BIT62;
925 } else {
926 TestBit = LShiftU64 (1, MemoryType);
927 }
928
929 if ((PcdGet64 (PcdMemoryProfileMemoryType) & TestBit) != 0) {
930 return TRUE;
931 } else {
932 return FALSE;
933 }
934}
935
947UINTN
949 IN EFI_MEMORY_TYPE MemoryType
950 )
951{
952 if ((UINT32)MemoryType >= MEMORY_TYPE_OS_RESERVED_MIN) {
953 return EfiMaxMemoryType;
954 } else if ((UINT32)MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
955 return EfiMaxMemoryType + 1;
956 } else {
957 return MemoryType;
958 }
959}
960
979 IN PHYSICAL_ADDRESS CallerAddress,
980 IN MEMORY_PROFILE_ACTION Action,
981 IN EFI_MEMORY_TYPE MemoryType,
982 IN UINTN Size,
983 IN VOID *Buffer,
984 IN CHAR8 *ActionString OPTIONAL
985 )
986{
987 EFI_STATUS Status;
988 MEMORY_PROFILE_CONTEXT *Context;
989 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
990 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
991 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
992 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
993 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
994 UINTN ProfileMemoryIndex;
995 MEMORY_PROFILE_ACTION BasicAction;
996 UINTN ActionStringSize;
997 UINTN ActionStringOccupiedSize;
998
999 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1000
1001 ContextData = GetMemoryProfileContext ();
1002 if (ContextData == NULL) {
1003 return EFI_UNSUPPORTED;
1004 }
1005
1006 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, CallerAddress);
1007 if (DriverInfoData == NULL) {
1008 return EFI_UNSUPPORTED;
1009 }
1010
1011 ActionStringSize = 0;
1012 ActionStringOccupiedSize = 0;
1013 if (ActionString != NULL) {
1014 ActionStringSize = AsciiStrSize (ActionString);
1015 ActionStringOccupiedSize = GET_OCCUPIED_SIZE (ActionStringSize, sizeof (UINT64));
1016 }
1017
1018 //
1019 // Use CoreInternalAllocatePool() that will not update profile for this AllocatePool action.
1020 //
1021 AllocInfoData = NULL;
1022 Status = CoreInternalAllocatePool (
1024 sizeof (*AllocInfoData) + ActionStringSize,
1025 (VOID **)&AllocInfoData
1026 );
1027 if (EFI_ERROR (Status)) {
1028 return EFI_OUT_OF_RESOURCES;
1029 }
1030
1031 ASSERT (AllocInfoData != NULL);
1032
1033 //
1034 // Only update SequenceCount if and only if it is basic action.
1035 //
1036 if (Action == BasicAction) {
1037 ContextData->Context.SequenceCount++;
1038 }
1039
1040 AllocInfo = &AllocInfoData->AllocInfo;
1041 AllocInfoData->Signature = MEMORY_PROFILE_ALLOC_INFO_SIGNATURE;
1042 AllocInfo->Header.Signature = MEMORY_PROFILE_ALLOC_INFO_SIGNATURE;
1043 AllocInfo->Header.Length = (UINT16)(sizeof (MEMORY_PROFILE_ALLOC_INFO) + ActionStringOccupiedSize);
1044 AllocInfo->Header.Revision = MEMORY_PROFILE_ALLOC_INFO_REVISION;
1045 AllocInfo->CallerAddress = CallerAddress;
1046 AllocInfo->SequenceId = ContextData->Context.SequenceCount;
1047 AllocInfo->Action = Action;
1048 AllocInfo->MemoryType = MemoryType;
1049 AllocInfo->Buffer = (PHYSICAL_ADDRESS)(UINTN)Buffer;
1050 AllocInfo->Size = Size;
1051 if (ActionString != NULL) {
1052 AllocInfo->ActionStringOffset = (UINT16)sizeof (MEMORY_PROFILE_ALLOC_INFO);
1053 AllocInfoData->ActionString = (CHAR8 *)(AllocInfoData + 1);
1054 CopyMem (AllocInfoData->ActionString, ActionString, ActionStringSize);
1055 } else {
1056 AllocInfo->ActionStringOffset = 0;
1057 AllocInfoData->ActionString = NULL;
1058 }
1059
1060 InsertTailList (DriverInfoData->AllocInfoList, &AllocInfoData->Link);
1061
1062 Context = &ContextData->Context;
1063 DriverInfo = &DriverInfoData->DriverInfo;
1064 DriverInfo->AllocRecordCount++;
1065
1066 //
1067 // Update summary if and only if it is basic action.
1068 //
1069 if (Action == BasicAction) {
1070 ProfileMemoryIndex = GetProfileMemoryIndex (MemoryType);
1071
1072 DriverInfo->CurrentUsage += Size;
1073 if (DriverInfo->PeakUsage < DriverInfo->CurrentUsage) {
1074 DriverInfo->PeakUsage = DriverInfo->CurrentUsage;
1075 }
1076
1077 DriverInfo->CurrentUsageByType[ProfileMemoryIndex] += Size;
1078 if (DriverInfo->PeakUsageByType[ProfileMemoryIndex] < DriverInfo->CurrentUsageByType[ProfileMemoryIndex]) {
1079 DriverInfo->PeakUsageByType[ProfileMemoryIndex] = DriverInfo->CurrentUsageByType[ProfileMemoryIndex];
1080 }
1081
1082 Context->CurrentTotalUsage += Size;
1083 if (Context->PeakTotalUsage < Context->CurrentTotalUsage) {
1084 Context->PeakTotalUsage = Context->CurrentTotalUsage;
1085 }
1086
1087 Context->CurrentTotalUsageByType[ProfileMemoryIndex] += Size;
1088 if (Context->PeakTotalUsageByType[ProfileMemoryIndex] < Context->CurrentTotalUsageByType[ProfileMemoryIndex]) {
1089 Context->PeakTotalUsageByType[ProfileMemoryIndex] = Context->CurrentTotalUsageByType[ProfileMemoryIndex];
1090 }
1091 }
1092
1093 return EFI_SUCCESS;
1094}
1095
1109 IN MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData,
1110 IN MEMORY_PROFILE_ACTION BasicAction,
1111 IN UINTN Size,
1112 IN VOID *Buffer
1113 )
1114{
1115 LIST_ENTRY *AllocInfoList;
1116 LIST_ENTRY *AllocLink;
1117 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1118 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1119
1120 AllocInfoList = DriverInfoData->AllocInfoList;
1121
1122 for (AllocLink = AllocInfoList->ForwardLink;
1123 AllocLink != AllocInfoList;
1124 AllocLink = AllocLink->ForwardLink)
1125 {
1126 AllocInfoData = CR (
1127 AllocLink,
1129 Link,
1130 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1131 );
1132 AllocInfo = &AllocInfoData->AllocInfo;
1133 if ((AllocInfo->Action & MEMORY_PROFILE_ACTION_BASIC_MASK) != BasicAction) {
1134 continue;
1135 }
1136
1137 switch (BasicAction) {
1138 case MemoryProfileActionAllocatePages:
1139 if ((AllocInfo->Buffer <= (PHYSICAL_ADDRESS)(UINTN)Buffer) &&
1140 ((AllocInfo->Buffer + AllocInfo->Size) >= ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)))
1141 {
1142 return AllocInfoData;
1143 }
1144
1145 break;
1146 case MemoryProfileActionAllocatePool:
1147 if (AllocInfo->Buffer == (PHYSICAL_ADDRESS)(UINTN)Buffer) {
1148 return AllocInfoData;
1149 }
1150
1151 break;
1152 default:
1153 ASSERT (FALSE);
1154 break;
1155 }
1156 }
1157
1158 return NULL;
1159}
1160
1176 IN PHYSICAL_ADDRESS CallerAddress,
1177 IN MEMORY_PROFILE_ACTION Action,
1178 IN UINTN Size,
1179 IN VOID *Buffer
1180 )
1181{
1182 MEMORY_PROFILE_CONTEXT *Context;
1183 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
1184 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1185 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1186 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1187 LIST_ENTRY *DriverLink;
1188 LIST_ENTRY *DriverInfoList;
1189 MEMORY_PROFILE_DRIVER_INFO_DATA *ThisDriverInfoData;
1190 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1191 UINTN ProfileMemoryIndex;
1192 MEMORY_PROFILE_ACTION BasicAction;
1193 BOOLEAN Found;
1194
1195 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1196
1197 ContextData = GetMemoryProfileContext ();
1198 if (ContextData == NULL) {
1199 return EFI_UNSUPPORTED;
1200 }
1201
1202 DriverInfoData = GetMemoryProfileDriverInfoFromAddress (ContextData, CallerAddress);
1203
1204 //
1205 // Do not return if DriverInfoData == NULL here,
1206 // because driver A might free memory allocated by driver B.
1207 //
1208
1209 //
1210 // Need use do-while loop to find all possible records,
1211 // because one address might be recorded multiple times.
1212 //
1213 Found = FALSE;
1214 AllocInfoData = NULL;
1215 do {
1216 if (DriverInfoData != NULL) {
1217 switch (BasicAction) {
1218 case MemoryProfileActionFreePages:
1219 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (DriverInfoData, MemoryProfileActionAllocatePages, Size, Buffer);
1220 break;
1221 case MemoryProfileActionFreePool:
1222 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (DriverInfoData, MemoryProfileActionAllocatePool, 0, Buffer);
1223 break;
1224 default:
1225 ASSERT (FALSE);
1226 AllocInfoData = NULL;
1227 break;
1228 }
1229 }
1230
1231 if (AllocInfoData == NULL) {
1232 //
1233 // Legal case, because driver A might free memory allocated by driver B, by some protocol.
1234 //
1235 DriverInfoList = ContextData->DriverInfoList;
1236
1237 for (DriverLink = DriverInfoList->ForwardLink;
1238 DriverLink != DriverInfoList;
1239 DriverLink = DriverLink->ForwardLink)
1240 {
1241 ThisDriverInfoData = CR (
1242 DriverLink,
1244 Link,
1245 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1246 );
1247 switch (BasicAction) {
1248 case MemoryProfileActionFreePages:
1249 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (ThisDriverInfoData, MemoryProfileActionAllocatePages, Size, Buffer);
1250 break;
1251 case MemoryProfileActionFreePool:
1252 AllocInfoData = GetMemoryProfileAllocInfoFromAddress (ThisDriverInfoData, MemoryProfileActionAllocatePool, 0, Buffer);
1253 break;
1254 default:
1255 ASSERT (FALSE);
1256 AllocInfoData = NULL;
1257 break;
1258 }
1259
1260 if (AllocInfoData != NULL) {
1261 DriverInfoData = ThisDriverInfoData;
1262 break;
1263 }
1264 }
1265
1266 if (AllocInfoData == NULL) {
1267 //
1268 // If (!Found), no matched allocate info is found for this free action.
1269 // It is because the specified memory type allocate actions have been filtered by
1270 // CoreNeedRecordProfile(), but free actions may have no memory type information,
1271 // they can not be filtered by CoreNeedRecordProfile(). Then, they will be
1272 // filtered here.
1273 //
1274 // If (Found), it is normal exit path.
1275 return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);
1276 }
1277 }
1278
1279 if ((DriverInfoData == NULL) || (AllocInfoData == NULL)) {
1280 ASSERT (DriverInfoData != NULL);
1281 ASSERT (AllocInfoData != NULL);
1282 return EFI_NOT_FOUND;
1283 }
1284
1285 Found = TRUE;
1286
1287 Context = &ContextData->Context;
1288 DriverInfo = &DriverInfoData->DriverInfo;
1289 AllocInfo = &AllocInfoData->AllocInfo;
1290
1291 DriverInfo->AllocRecordCount--;
1292 //
1293 // Update summary if and only if it is basic action.
1294 //
1295 if (AllocInfo->Action == (AllocInfo->Action & MEMORY_PROFILE_ACTION_BASIC_MASK)) {
1296 ProfileMemoryIndex = GetProfileMemoryIndex (AllocInfo->MemoryType);
1297
1298 Context->CurrentTotalUsage -= AllocInfo->Size;
1299 Context->CurrentTotalUsageByType[ProfileMemoryIndex] -= AllocInfo->Size;
1300
1301 DriverInfo->CurrentUsage -= AllocInfo->Size;
1302 DriverInfo->CurrentUsageByType[ProfileMemoryIndex] -= AllocInfo->Size;
1303 }
1304
1305 RemoveEntryList (&AllocInfoData->Link);
1306
1307 if (BasicAction == MemoryProfileActionFreePages) {
1308 if (AllocInfo->Buffer != (PHYSICAL_ADDRESS)(UINTN)Buffer) {
1310 AllocInfo->CallerAddress,
1311 AllocInfo->Action,
1312 AllocInfo->MemoryType,
1313 (UINTN)((PHYSICAL_ADDRESS)(UINTN)Buffer - AllocInfo->Buffer),
1314 (VOID *)(UINTN)AllocInfo->Buffer,
1315 AllocInfoData->ActionString
1316 );
1317 }
1318
1319 if (AllocInfo->Buffer + AllocInfo->Size != ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)) {
1321 AllocInfo->CallerAddress,
1322 AllocInfo->Action,
1323 AllocInfo->MemoryType,
1324 (UINTN)((AllocInfo->Buffer + AllocInfo->Size) - ((PHYSICAL_ADDRESS)(UINTN)Buffer + Size)),
1325 (VOID *)((UINTN)Buffer + Size),
1326 AllocInfoData->ActionString
1327 );
1328 }
1329 }
1330
1331 //
1332 // Use CoreInternalFreePool() that will not update profile for this FreePool action.
1333 //
1334 CoreInternalFreePool (AllocInfoData, NULL);
1335 } while (TRUE);
1336}
1337
1361EFIAPI
1363 IN PHYSICAL_ADDRESS CallerAddress,
1364 IN MEMORY_PROFILE_ACTION Action,
1365 IN EFI_MEMORY_TYPE MemoryType,
1366 IN UINTN Size, // Valid for AllocatePages/FreePages/AllocatePool
1367 IN VOID *Buffer,
1368 IN CHAR8 *ActionString OPTIONAL
1369 )
1370{
1371 EFI_STATUS Status;
1372 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1373 MEMORY_PROFILE_ACTION BasicAction;
1374
1375 if (!IS_UEFI_MEMORY_PROFILE_ENABLED) {
1376 return EFI_UNSUPPORTED;
1377 }
1378
1379 if (mMemoryProfileGettingStatus) {
1380 return EFI_ACCESS_DENIED;
1381 }
1382
1383 if (!mMemoryProfileRecordingEnable) {
1384 return EFI_ABORTED;
1385 }
1386
1387 //
1388 // Get the basic action to know how to process the record
1389 //
1390 BasicAction = Action & MEMORY_PROFILE_ACTION_BASIC_MASK;
1391
1392 //
1393 // EfiMaxMemoryType means the MemoryType is unknown.
1394 //
1395 if (MemoryType != EfiMaxMemoryType) {
1396 //
1397 // Only record limited MemoryType.
1398 //
1399 if (!CoreNeedRecordProfile (MemoryType)) {
1400 return EFI_UNSUPPORTED;
1401 }
1402 }
1403
1404 ContextData = GetMemoryProfileContext ();
1405 if (ContextData == NULL) {
1406 return EFI_UNSUPPORTED;
1407 }
1408
1410 switch (BasicAction) {
1411 case MemoryProfileActionAllocatePages:
1412 Status = CoreUpdateProfileAllocate (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
1413 break;
1414 case MemoryProfileActionFreePages:
1415 Status = CoreUpdateProfileFree (CallerAddress, Action, Size, Buffer);
1416 break;
1417 case MemoryProfileActionAllocatePool:
1418 Status = CoreUpdateProfileAllocate (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
1419 break;
1420 case MemoryProfileActionFreePool:
1421 Status = CoreUpdateProfileFree (CallerAddress, Action, 0, Buffer);
1422 break;
1423 default:
1424 ASSERT (FALSE);
1425 Status = EFI_UNSUPPORTED;
1426 break;
1427 }
1428
1430
1431 return Status;
1432}
1433
1435
1442UINTN
1444 VOID
1445 )
1446{
1447 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1448 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1449 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1450 LIST_ENTRY *DriverInfoList;
1451 LIST_ENTRY *DriverLink;
1452 LIST_ENTRY *AllocInfoList;
1453 LIST_ENTRY *AllocLink;
1454 UINTN TotalSize;
1455
1456 ContextData = GetMemoryProfileContext ();
1457 if (ContextData == NULL) {
1458 return 0;
1459 }
1460
1461 TotalSize = sizeof (MEMORY_PROFILE_CONTEXT);
1462
1463 DriverInfoList = ContextData->DriverInfoList;
1464 for (DriverLink = DriverInfoList->ForwardLink;
1465 DriverLink != DriverInfoList;
1466 DriverLink = DriverLink->ForwardLink)
1467 {
1468 DriverInfoData = CR (
1469 DriverLink,
1471 Link,
1472 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1473 );
1474 TotalSize += DriverInfoData->DriverInfo.Header.Length;
1475
1476 AllocInfoList = DriverInfoData->AllocInfoList;
1477 for (AllocLink = AllocInfoList->ForwardLink;
1478 AllocLink != AllocInfoList;
1479 AllocLink = AllocLink->ForwardLink)
1480 {
1481 AllocInfoData = CR (
1482 AllocLink,
1484 Link,
1485 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1486 );
1487 TotalSize += AllocInfoData->AllocInfo.Header.Length;
1488 }
1489 }
1490
1491 return TotalSize;
1492}
1493
1500VOID
1502 IN VOID *ProfileBuffer
1503 )
1504{
1505 MEMORY_PROFILE_CONTEXT *Context;
1506 MEMORY_PROFILE_DRIVER_INFO *DriverInfo;
1507 MEMORY_PROFILE_ALLOC_INFO *AllocInfo;
1508 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1509 MEMORY_PROFILE_DRIVER_INFO_DATA *DriverInfoData;
1510 MEMORY_PROFILE_ALLOC_INFO_DATA *AllocInfoData;
1511 LIST_ENTRY *DriverInfoList;
1512 LIST_ENTRY *DriverLink;
1513 LIST_ENTRY *AllocInfoList;
1514 LIST_ENTRY *AllocLink;
1515 UINTN PdbSize;
1516 UINTN ActionStringSize;
1517
1518 ContextData = GetMemoryProfileContext ();
1519 if (ContextData == NULL) {
1520 return;
1521 }
1522
1523 Context = ProfileBuffer;
1524 CopyMem (Context, &ContextData->Context, sizeof (MEMORY_PROFILE_CONTEXT));
1525 DriverInfo = (MEMORY_PROFILE_DRIVER_INFO *)(Context + 1);
1526
1527 DriverInfoList = ContextData->DriverInfoList;
1528 for (DriverLink = DriverInfoList->ForwardLink;
1529 DriverLink != DriverInfoList;
1530 DriverLink = DriverLink->ForwardLink)
1531 {
1532 DriverInfoData = CR (
1533 DriverLink,
1535 Link,
1536 MEMORY_PROFILE_DRIVER_INFO_SIGNATURE
1537 );
1538 CopyMem (DriverInfo, &DriverInfoData->DriverInfo, sizeof (MEMORY_PROFILE_DRIVER_INFO));
1539 if (DriverInfo->PdbStringOffset != 0) {
1540 PdbSize = AsciiStrSize (DriverInfoData->PdbString);
1541 CopyMem ((VOID *)((UINTN)DriverInfo + DriverInfo->PdbStringOffset), DriverInfoData->PdbString, PdbSize);
1542 }
1543
1544 AllocInfo = (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)DriverInfo + DriverInfo->Header.Length);
1545
1546 AllocInfoList = DriverInfoData->AllocInfoList;
1547 for (AllocLink = AllocInfoList->ForwardLink;
1548 AllocLink != AllocInfoList;
1549 AllocLink = AllocLink->ForwardLink)
1550 {
1551 AllocInfoData = CR (
1552 AllocLink,
1554 Link,
1555 MEMORY_PROFILE_ALLOC_INFO_SIGNATURE
1556 );
1557 CopyMem (AllocInfo, &AllocInfoData->AllocInfo, sizeof (MEMORY_PROFILE_ALLOC_INFO));
1558 if (AllocInfo->ActionStringOffset != 0) {
1559 ActionStringSize = AsciiStrSize (AllocInfoData->ActionString);
1560 CopyMem ((VOID *)((UINTN)AllocInfo + AllocInfo->ActionStringOffset), AllocInfoData->ActionString, ActionStringSize);
1561 }
1562
1563 AllocInfo = (MEMORY_PROFILE_ALLOC_INFO *)((UINTN)AllocInfo + AllocInfo->Header.Length);
1564 }
1565
1566 DriverInfo = (MEMORY_PROFILE_DRIVER_INFO *)AllocInfo;
1567 }
1568}
1569
1585EFIAPI
1588 IN OUT UINT64 *ProfileSize,
1589 OUT VOID *ProfileBuffer
1590 )
1591{
1592 UINTN Size;
1593 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1594 BOOLEAN MemoryProfileGettingStatus;
1595
1596 ContextData = GetMemoryProfileContext ();
1597 if (ContextData == NULL) {
1598 return EFI_UNSUPPORTED;
1599 }
1600
1601 MemoryProfileGettingStatus = mMemoryProfileGettingStatus;
1602 mMemoryProfileGettingStatus = TRUE;
1603
1604 Size = MemoryProfileGetDataSize ();
1605
1606 if (*ProfileSize < Size) {
1607 *ProfileSize = Size;
1608 mMemoryProfileGettingStatus = MemoryProfileGettingStatus;
1609 return EFI_BUFFER_TOO_SMALL;
1610 }
1611
1612 *ProfileSize = Size;
1613 MemoryProfileCopyData (ProfileBuffer);
1614
1615 mMemoryProfileGettingStatus = MemoryProfileGettingStatus;
1616 return EFI_SUCCESS;
1617}
1618
1635EFIAPI
1638 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1639 IN PHYSICAL_ADDRESS ImageBase,
1640 IN UINT64 ImageSize,
1641 IN EFI_FV_FILETYPE FileType
1642 )
1643{
1644 EFI_STATUS Status;
1646 VOID *EntryPointInImage;
1647
1648 ZeroMem (&DriverEntry, sizeof (DriverEntry));
1649 DriverEntry.Info.FilePath = FilePath;
1650 DriverEntry.ImageContext.ImageAddress = ImageBase;
1651 DriverEntry.ImageContext.ImageSize = ImageSize;
1652 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageBase, &EntryPointInImage);
1653 ASSERT_EFI_ERROR (Status);
1654 DriverEntry.ImageContext.EntryPoint = (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
1655 DriverEntry.ImageContext.ImageType = InternalPeCoffGetSubsystem ((VOID *)(UINTN)ImageBase);
1656
1657 return RegisterMemoryProfileImage (&DriverEntry, FileType);
1658}
1659
1675EFIAPI
1678 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
1679 IN PHYSICAL_ADDRESS ImageBase,
1680 IN UINT64 ImageSize
1681 )
1682{
1683 EFI_STATUS Status;
1685 VOID *EntryPointInImage;
1686
1687 ZeroMem (&DriverEntry, sizeof (DriverEntry));
1688 DriverEntry.Info.FilePath = FilePath;
1689 DriverEntry.ImageContext.ImageAddress = ImageBase;
1690 DriverEntry.ImageContext.ImageSize = ImageSize;
1691 Status = InternalPeCoffGetEntryPoint ((VOID *)(UINTN)ImageBase, &EntryPointInImage);
1692 ASSERT_EFI_ERROR (Status);
1693 DriverEntry.ImageContext.EntryPoint = (PHYSICAL_ADDRESS)(UINTN)EntryPointInImage;
1694
1696}
1697
1710EFIAPI
1713 OUT BOOLEAN *RecordingState
1714 )
1715{
1716 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1717
1718 ContextData = GetMemoryProfileContext ();
1719 if (ContextData == NULL) {
1720 return EFI_UNSUPPORTED;
1721 }
1722
1723 if (RecordingState == NULL) {
1724 return EFI_INVALID_PARAMETER;
1725 }
1726
1727 *RecordingState = mMemoryProfileRecordingEnable;
1728 return EFI_SUCCESS;
1729}
1730
1742EFIAPI
1745 IN BOOLEAN RecordingState
1746 )
1747{
1748 MEMORY_PROFILE_CONTEXT_DATA *ContextData;
1749
1750 ContextData = GetMemoryProfileContext ();
1751 if (ContextData == NULL) {
1752 return EFI_UNSUPPORTED;
1753 }
1754
1755 mMemoryProfileRecordingEnable = RecordingState;
1756 return EFI_SUCCESS;
1757}
1758
1783EFIAPI
1786 IN PHYSICAL_ADDRESS CallerAddress,
1787 IN MEMORY_PROFILE_ACTION Action,
1788 IN EFI_MEMORY_TYPE MemoryType,
1789 IN VOID *Buffer,
1790 IN UINTN Size,
1791 IN CHAR8 *ActionString OPTIONAL
1792 )
1793{
1794 return CoreUpdateProfile (CallerAddress, Action, MemoryType, Size, Buffer, ActionString);
1795}
1796
UINT64 UINTN
VOID *EFIAPI GetNextHob(IN UINT16 Type, IN CONST VOID *HobStart)
Definition: HobLib.c:103
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)
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)
EFI_STATUS EFIAPI CoreInternalFreePool(IN VOID *Buffer, OUT EFI_MEMORY_TYPE *PoolType OPTIONAL)
Definition: Pool.c:567
VOID CoreAcquireLock(IN EFI_LOCK *Lock)
Definition: Library.c:59
VOID CoreReleaseLock(IN EFI_LOCK *Lock)
Definition: Library.c:80
EFI_STATUS EFIAPI CoreInstallMultipleProtocolInterfaces(IN OUT EFI_HANDLE *Handle,...)
Definition: Handle.c:630
EFI_STATUS EFIAPI CoreInternalAllocatePool(IN EFI_MEMORY_TYPE PoolType, IN UINTN Size, OUT VOID **Buffer)
Definition: Pool.c:203
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define NULL
Definition: Base.h:319
#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 IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:463
#define DEBUG(Expression)
Definition: DebugLib.h:435
#define CR(Record, TYPE, Field, TestSignature)
Definition: DebugLib.h:660
VOID MemoryProfileCopyData(IN VOID *ProfileBuffer)
EFI_GUID * GetFileNameFromFilePath(IN EFI_DEVICE_PATH_PROTOCOL *FilePath)
UINTN MemoryProfileGetDataSize(VOID)
VOID CoreReleaseMemoryProfileLock(VOID)
MEMORY_PROFILE_CONTEXT_DATA * GetMemoryProfileContext(VOID)
EFI_STATUS EFIAPI CoreUpdateProfile(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 ProfileProtocolUnregisterImage(IN EDKII_MEMORY_PROFILE_PROTOCOL *This, IN EFI_DEVICE_PATH_PROTOCOL *FilePath, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize)
EFI_STATUS CoreUpdateProfileAllocate(IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Size, IN VOID *Buffer, IN CHAR8 *ActionString OPTIONAL)
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)
EFI_STATUS EFIAPI ProfileProtocolGetData(IN EDKII_MEMORY_PROFILE_PROTOCOL *This, IN OUT UINT64 *ProfileSize, OUT VOID *ProfileBuffer)
EFI_STATUS UnregisterMemoryProfileImage(IN LOADED_IMAGE_PRIVATE_DATA *DriverEntry)
EFI_STATUS EFIAPI ProfileProtocolRecord(IN EDKII_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 MemoryProfileInstallProtocol(VOID)
VOID CoreAcquireMemoryProfileLock(VOID)
MEMORY_PROFILE_DRIVER_INFO_DATA * GetMemoryProfileDriverInfoFromAddress(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData, IN PHYSICAL_ADDRESS Address)
EFI_STATUS RegisterMemoryProfileImage(IN LOADED_IMAGE_PRIVATE_DATA *DriverEntry, IN EFI_FV_FILETYPE FileType)
MEMORY_PROFILE_DRIVER_INFO_DATA * GetMemoryProfileDriverInfoByFileNameAndAddress(IN MEMORY_PROFILE_CONTEXT_DATA *ContextData, IN EFI_GUID *FileName, IN PHYSICAL_ADDRESS Address)
BOOLEAN CoreNeedRecordProfile(IN EFI_MEMORY_TYPE MemoryType)
UINTN GetProfileMemoryIndex(IN EFI_MEMORY_TYPE MemoryType)
BOOLEAN RegisterDxeCore(IN VOID *HobStart, IN MEMORY_PROFILE_CONTEXT_DATA *ContextData)
EFI_STATUS CoreUpdateProfileFree(IN PHYSICAL_ADDRESS CallerAddress, IN MEMORY_PROFILE_ACTION Action, IN UINTN Size, IN VOID *Buffer)
VOID MemoryProfileInit(IN VOID *HobStart)
BOOLEAN NeedRecordThisDriver(IN EFI_DEVICE_PATH_PROTOCOL *DriverFilePath)
EFI_STATUS EFIAPI ProfileProtocolSetRecordingState(IN EDKII_MEMORY_PROFILE_PROTOCOL *This, IN BOOLEAN RecordingState)
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 ProfileProtocolGetRecordingState(IN EDKII_MEMORY_PROFILE_PROTOCOL *This, OUT BOOLEAN *RecordingState)
EFI_STATUS EFIAPI ProfileProtocolRegisterImage(IN EDKII_MEMORY_PROFILE_PROTOCOL *This, IN EFI_DEVICE_PATH_PROTOCOL *FilePath, IN PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN EFI_FV_FILETYPE FileType)
RETURN_STATUS InternalPeCoffGetEntryPoint(IN VOID *Pe32Data, OUT VOID **EntryPoint)
#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 DriverEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: TdTcg2Dxe.c:2387
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
#define EFI_INITIALIZE_LOCK_VARIABLE(Priority)
Definition: UefiLib.h:313
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
@ EfiBootServicesData
EFI_PHYSICAL_ADDRESS MemoryBaseAddress
Definition: PiHob.h:119
EFI_PHYSICAL_ADDRESS EntryPoint
Definition: PiHob.h:217
EFI_HOB_MEMORY_ALLOCATION_HEADER MemoryAllocationHeader
Definition: PiHob.h:207
UINT32 e_lfanew
File address of new exe header.
Definition: PeImage.h:75
UINT16 e_magic
Magic number.
Definition: PeImage.h:57
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