]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / DxeCorePerformanceLib / DxeCorePerformanceLib.c
1 /** @file
2 Performance library instance mainly used by DxeCore.
3
4 This library provides the performance measurement interfaces and initializes performance
5 logging for DXE phase. It first initializes its private global data structure for
6 performance logging and saves the performance GUIDed HOB passed from PEI phase.
7 It initializes DXE phase performance logging by publishing the Performance and PerformanceEx Protocol,
8 which are consumed by DxePerformanceLib to logging performance data in DXE phase.
9
10 This library is mainly used by DxeCore to start performance logging to ensure that
11 Performance Protocol is installed at the very beginning of DXE phase.
12
13 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
14 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
15 SPDX-License-Identifier: BSD-2-Clause-Patent
16
17 **/
18
19 #include "DxeCorePerformanceLibInternal.h"
20
21 //
22 // Data for FPDT performance records.
23 //
24 #define SMM_BOOT_RECORD_COMM_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_BOOT_RECORD_COMMUNICATE))
25 #define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))
26 #define FIRMWARE_RECORD_BUFFER 0x10000
27 #define CACHE_HANDLE_GUID_COUNT 0x800
28
29 BOOT_PERFORMANCE_TABLE *mAcpiBootPerformanceTable = NULL;
30 BOOT_PERFORMANCE_TABLE mBootPerformanceTableTemplate = {
31 {
32 EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_SIGNATURE,
33 sizeof (BOOT_PERFORMANCE_TABLE)
34 },
35 {
36 {
37 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_FIRMWARE_BASIC_BOOT, // Type
38 sizeof (EFI_ACPI_5_0_FPDT_FIRMWARE_BASIC_BOOT_RECORD), // Length
39 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_FIRMWARE_BASIC_BOOT // Revision
40 },
41 0, // Reserved
42 //
43 // These values will be updated at runtime.
44 //
45 0, // ResetEnd
46 0, // OsLoaderLoadImageStart
47 0, // OsLoaderStartImageStart
48 0, // ExitBootServicesEntry
49 0 // ExitBootServicesExit
50 }
51 };
52
53 typedef struct {
54 EFI_HANDLE Handle;
55 CHAR8 NameString[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];
56 EFI_GUID ModuleGuid;
57 } HANDLE_GUID_MAP;
58
59 HANDLE_GUID_MAP mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];
60 UINTN mCachePairCount = 0;
61
62 UINT32 mLoadImageCount = 0;
63 UINT32 mPerformanceLength = 0;
64 UINT32 mMaxPerformanceLength = 0;
65 UINT32 mBootRecordSize = 0;
66 UINTN mBootRecordMaxSize = 0;
67 UINT32 mCachedLength = 0;
68
69 BOOLEAN mFpdtBufferIsReported = FALSE;
70 BOOLEAN mLackSpaceIsReported = FALSE;
71 CHAR8 *mPlatformLanguage = NULL;
72 UINT8 *mPerformancePointer = NULL;
73 UINT8 *mBootRecordBuffer = NULL;
74 BOOLEAN mLockInsertRecord = FALSE;
75 CHAR8 *mDevicePathString = NULL;
76
77 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathToText = NULL;
78
79 //
80 // Interfaces for PerformanceMeasurement Protocol.
81 //
82 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL mPerformanceMeasurementInterface = {
83 CreatePerformanceMeasurement,
84 };
85
86 PERFORMANCE_PROPERTY mPerformanceProperty;
87
88 /**
89 Return the pointer to the FPDT record in the allocated memory.
90
91 @param RecordSize The size of FPDT record.
92 @param FpdtRecordPtr Pointer the FPDT record in the allocated memory.
93
94 @retval EFI_SUCCESS Successfully get the pointer to the FPDT record.
95 @retval EFI_OUT_OF_RESOURCES Ran out of space to store the records.
96 **/
97 EFI_STATUS
98 GetFpdtRecordPtr (
99 IN UINT8 RecordSize,
100 IN OUT FPDT_RECORD_PTR *FpdtRecordPtr
101 )
102 {
103 if (mFpdtBufferIsReported) {
104 //
105 // Append Boot records to the boot performance table.
106 //
107 if (mBootRecordSize + RecordSize > mBootRecordMaxSize) {
108 if (!mLackSpaceIsReported) {
109 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: No enough space to save boot records\n"));
110 mLackSpaceIsReported = TRUE;
111 }
112
113 return EFI_OUT_OF_RESOURCES;
114 } else {
115 //
116 // Save boot record into BootPerformance table
117 //
118 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mBootRecordSize);
119 }
120 } else {
121 //
122 // Check if pre-allocated buffer is full
123 //
124 if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {
125 mPerformancePointer = ReallocatePool (
126 mPerformanceLength,
127 mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER,
128 mPerformancePointer
129 );
130 if (mPerformancePointer == NULL) {
131 return EFI_OUT_OF_RESOURCES;
132 }
133
134 mMaxPerformanceLength = mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER;
135 }
136
137 //
138 // Covert buffer to FPDT Ptr Union type.
139 //
140 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mPerformanceLength);
141 }
142
143 return EFI_SUCCESS;
144 }
145
146 /**
147 Check whether the Token is a known one which is uesed by core.
148
149 @param Token Pointer to a Null-terminated ASCII string
150
151 @retval TRUE Is a known one used by core.
152 @retval FALSE Not a known one.
153
154 **/
155 BOOLEAN
156 IsKnownTokens (
157 IN CONST CHAR8 *Token
158 )
159 {
160 if (Token == NULL) {
161 return FALSE;
162 }
163
164 if ((AsciiStrCmp (Token, SEC_TOK) == 0) ||
165 (AsciiStrCmp (Token, PEI_TOK) == 0) ||
166 (AsciiStrCmp (Token, DXE_TOK) == 0) ||
167 (AsciiStrCmp (Token, BDS_TOK) == 0) ||
168 (AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0) ||
169 (AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0) ||
170 (AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0) ||
171 (AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0) ||
172 (AsciiStrCmp (Token, START_IMAGE_TOK) == 0) ||
173 (AsciiStrCmp (Token, PEIM_TOK) == 0))
174 {
175 return TRUE;
176 } else {
177 return FALSE;
178 }
179 }
180
181 /**
182 Check whether the ID is a known one which map to the known Token.
183
184 @param Identifier 32-bit identifier.
185
186 @retval TRUE Is a known one used by core.
187 @retval FALSE Not a known one.
188
189 **/
190 BOOLEAN
191 IsKnownID (
192 IN UINT32 Identifier
193 )
194 {
195 if ((Identifier == MODULE_START_ID) ||
196 (Identifier == MODULE_END_ID) ||
197 (Identifier == MODULE_LOADIMAGE_START_ID) ||
198 (Identifier == MODULE_LOADIMAGE_END_ID) ||
199 (Identifier == MODULE_DB_START_ID) ||
200 (Identifier == MODULE_DB_END_ID) ||
201 (Identifier == MODULE_DB_SUPPORT_START_ID) ||
202 (Identifier == MODULE_DB_SUPPORT_END_ID) ||
203 (Identifier == MODULE_DB_STOP_START_ID) ||
204 (Identifier == MODULE_DB_STOP_END_ID))
205 {
206 return TRUE;
207 } else {
208 return FALSE;
209 }
210 }
211
212 /**
213 This internal function dumps all the SMM performance data and size.
214
215 @param SmmPerfData Smm Performance data. The buffer contain the SMM perf data is allocated by this function and caller needs to free it.
216 @param SmmPerfDataSize Smm Performance data size.
217 @param SkipGetPerfData Skip to get performance data, just get the size.
218
219 **/
220 VOID
221 InternalGetSmmPerfData (
222 OUT VOID **SmmPerfData,
223 OUT UINTN *SmmPerfDataSize,
224 IN BOOLEAN SkipGetPerfData
225 )
226 {
227 EFI_STATUS Status;
228 UINT8 *SmmBootRecordCommBuffer;
229 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
230 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
231 UINTN CommSize;
232 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;
233 EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *SmmCommRegionTable;
234 EFI_MEMORY_DESCRIPTOR *SmmCommMemRegion;
235 UINTN Index;
236 VOID *SmmBootRecordData;
237 UINTN SmmBootRecordDataSize;
238 UINTN ReservedMemSize;
239
240 //
241 // Collect boot records from SMM drivers.
242 //
243 SmmBootRecordCommBuffer = NULL;
244 SmmCommData = NULL;
245 SmmBootRecordData = NULL;
246 ReservedMemSize = 0;
247 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **)&Communication);
248 if (!EFI_ERROR (Status)) {
249 //
250 // Initialize communicate buffer
251 // Get the prepared Reserved Memory Range
252 //
253 Status = EfiGetSystemConfigurationTable (
254 &gEdkiiPiSmmCommunicationRegionTableGuid,
255 (VOID **)&SmmCommRegionTable
256 );
257 if (!EFI_ERROR (Status)) {
258 ASSERT (SmmCommRegionTable != NULL);
259 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *)(SmmCommRegionTable + 1);
260 for (Index = 0; Index < SmmCommRegionTable->NumberOfEntries; Index++) {
261 if (SmmCommMemRegion->Type == EfiConventionalMemory) {
262 break;
263 }
264
265 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)SmmCommMemRegion + SmmCommRegionTable->DescriptorSize);
266 }
267
268 ASSERT (Index < SmmCommRegionTable->NumberOfEntries);
269 ASSERT (SmmCommMemRegion->PhysicalStart > 0);
270 ASSERT (SmmCommMemRegion->NumberOfPages > 0);
271 ReservedMemSize = (UINTN)SmmCommMemRegion->NumberOfPages * EFI_PAGE_SIZE;
272
273 //
274 // Check enough reserved memory space
275 //
276 if (ReservedMemSize > SMM_BOOT_RECORD_COMM_SIZE) {
277 SmmBootRecordCommBuffer = (VOID *)(UINTN)SmmCommMemRegion->PhysicalStart;
278 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER *)SmmBootRecordCommBuffer;
279 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE *)SmmCommBufferHeader->Data;
280 ZeroMem ((UINT8 *)SmmCommData, sizeof (SMM_BOOT_RECORD_COMMUNICATE));
281
282 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gEfiFirmwarePerformanceGuid);
283 SmmCommBufferHeader->MessageLength = sizeof (SMM_BOOT_RECORD_COMMUNICATE);
284 CommSize = SMM_BOOT_RECORD_COMM_SIZE;
285
286 //
287 // Get the size of boot records.
288 //
289 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE;
290 SmmCommData->BootRecordData = NULL;
291 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
292
293 if (!EFI_ERROR (Status) && !EFI_ERROR (SmmCommData->ReturnStatus) && (SmmCommData->BootRecordSize != 0)) {
294 if (SkipGetPerfData) {
295 *SmmPerfDataSize = SmmCommData->BootRecordSize;
296 return;
297 }
298
299 //
300 // Get all boot records
301 //
302 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET;
303 SmmBootRecordDataSize = SmmCommData->BootRecordSize;
304 SmmBootRecordData = AllocateZeroPool (SmmBootRecordDataSize);
305 ASSERT (SmmBootRecordData != NULL);
306 SmmCommData->BootRecordOffset = 0;
307 SmmCommData->BootRecordData = (VOID *)((UINTN)SmmCommMemRegion->PhysicalStart + SMM_BOOT_RECORD_COMM_SIZE);
308 SmmCommData->BootRecordSize = ReservedMemSize - SMM_BOOT_RECORD_COMM_SIZE;
309 while (SmmCommData->BootRecordOffset < SmmBootRecordDataSize) {
310 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
311 ASSERT_EFI_ERROR (Status);
312 ASSERT_EFI_ERROR (SmmCommData->ReturnStatus);
313 if (SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize > SmmBootRecordDataSize) {
314 CopyMem ((UINT8 *)SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmBootRecordDataSize - SmmCommData->BootRecordOffset);
315 } else {
316 CopyMem ((UINT8 *)SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmCommData->BootRecordSize);
317 }
318
319 SmmCommData->BootRecordOffset = SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize;
320 }
321
322 *SmmPerfData = SmmBootRecordData;
323 *SmmPerfDataSize = SmmBootRecordDataSize;
324 }
325 }
326 }
327 }
328 }
329
330 /**
331 Allocate buffer for Boot Performance table.
332
333 @return Status code.
334
335 **/
336 EFI_STATUS
337 AllocateBootPerformanceTable (
338 VOID
339 )
340 {
341 EFI_STATUS Status;
342 UINTN Size;
343 UINTN BootPerformanceDataSize;
344 UINT8 *BootPerformanceData;
345 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
346 UINTN SmmBootRecordDataSize;
347
348 SmmBootRecordDataSize = 0;
349
350 //
351 // Get SMM performance data size at the point of EndOfDxe in order to allocate the boot performance table.
352 // Will Get all the data at ReadyToBoot.
353 //
354 InternalGetSmmPerfData (NULL, &SmmBootRecordDataSize, TRUE);
355
356 //
357 // Prepare memory for Boot Performance table.
358 // Boot Performance table includes BasicBoot record, and one or more appended Boot Records.
359 //
360 BootPerformanceDataSize = sizeof (BOOT_PERFORMANCE_TABLE) + mPerformanceLength + SmmBootRecordDataSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);
361
362 //
363 // Try to allocate the same runtime buffer as last time boot.
364 //
365 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
366 Size = sizeof (PerformanceVariable);
367 Status = gRT->GetVariable (
368 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
369 &gEfiFirmwarePerformanceGuid,
370 NULL,
371 &Size,
372 &PerformanceVariable
373 );
374 if (!EFI_ERROR (Status)) {
375 Status = gBS->AllocatePages (
376 AllocateAddress,
377 EfiReservedMemoryType,
378 EFI_SIZE_TO_PAGES (BootPerformanceDataSize),
379 &PerformanceVariable.BootPerformanceTablePointer
380 );
381 if (!EFI_ERROR (Status)) {
382 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *)(UINTN)PerformanceVariable.BootPerformanceTablePointer;
383 }
384 }
385
386 if (mAcpiBootPerformanceTable == NULL) {
387 //
388 // Fail to allocate at specified address, continue to allocate at any address.
389 //
390 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *)AllocatePeiAccessiblePages (
391 EfiReservedMemoryType,
392 EFI_SIZE_TO_PAGES (BootPerformanceDataSize)
393 );
394 if (mAcpiBootPerformanceTable != NULL) {
395 ZeroMem (mAcpiBootPerformanceTable, BootPerformanceDataSize);
396 }
397 }
398
399 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: ACPI Boot Performance Table address = 0x%x\n", mAcpiBootPerformanceTable));
400
401 if (mAcpiBootPerformanceTable == NULL) {
402 return EFI_OUT_OF_RESOURCES;
403 }
404
405 //
406 // Prepare Boot Performance Table.
407 //
408 BootPerformanceData = (UINT8 *)mAcpiBootPerformanceTable;
409 //
410 // Fill Basic Boot record to Boot Performance Table.
411 //
412 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));
413 BootPerformanceData = BootPerformanceData + mAcpiBootPerformanceTable->Header.Length;
414 //
415 // Fill Boot records from boot drivers.
416 //
417 if (mPerformancePointer != NULL) {
418 CopyMem (BootPerformanceData, mPerformancePointer, mPerformanceLength);
419 mAcpiBootPerformanceTable->Header.Length += mPerformanceLength;
420 BootPerformanceData = BootPerformanceData + mPerformanceLength;
421 FreePool (mPerformancePointer);
422 mPerformancePointer = NULL;
423 mPerformanceLength = 0;
424 mMaxPerformanceLength = 0;
425 }
426
427 mBootRecordBuffer = (UINT8 *)mAcpiBootPerformanceTable;
428 mBootRecordSize = mAcpiBootPerformanceTable->Header.Length;
429 mBootRecordMaxSize = BootPerformanceDataSize;
430
431 return EFI_SUCCESS;
432 }
433
434 /**
435 Get a human readable module name and module guid for the given image handle.
436 If module name can't be found, "" string will return.
437 If module guid can't be found, Zero Guid will return.
438
439 @param Handle Image handle or Controller handle.
440 @param NameString The ascii string will be filled into it. If not found, null string will return.
441 @param BufferSize Size of the input NameString buffer.
442 @param ModuleGuid Point to the guid buffer to store the got module guid value.
443
444 @retval EFI_SUCCESS Successfully get module name and guid.
445 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.
446 @retval other value Module Name can't be got.
447 **/
448 EFI_STATUS
449 GetModuleInfoFromHandle (
450 IN EFI_HANDLE Handle,
451 OUT CHAR8 *NameString,
452 IN UINTN BufferSize,
453 OUT EFI_GUID *ModuleGuid OPTIONAL
454 )
455 {
456 EFI_STATUS Status;
457 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
458 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
459 CHAR8 *PdbFileName;
460 EFI_GUID *TempGuid;
461 UINTN StartIndex;
462 UINTN Index;
463 INTN Count;
464 BOOLEAN ModuleGuidIsGet;
465 UINTN StringSize;
466 CHAR16 *StringPtr;
467 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
468 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;
469
470 if ((NameString == NULL) || (BufferSize == 0)) {
471 return EFI_INVALID_PARAMETER;
472 }
473
474 //
475 // Try to get the ModuleGuid and name string form the caached array.
476 //
477 if (mCachePairCount > 0) {
478 for (Count = mCachePairCount -1; Count >= 0; Count--) {
479 if (Handle == mCacheHandleGuidTable[Count].Handle) {
480 CopyGuid (ModuleGuid, &mCacheHandleGuidTable[Count].ModuleGuid);
481 AsciiStrCpyS (NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, mCacheHandleGuidTable[Count].NameString);
482 return EFI_SUCCESS;
483 }
484 }
485 }
486
487 Status = EFI_INVALID_PARAMETER;
488 LoadedImage = NULL;
489 ModuleGuidIsGet = FALSE;
490
491 //
492 // Initialize GUID as zero value.
493 //
494 TempGuid = &gZeroGuid;
495 //
496 // Initialize it as "" string.
497 //
498 NameString[0] = 0;
499
500 if (Handle != NULL) {
501 //
502 // Try Handle as ImageHandle.
503 //
504 Status = gBS->HandleProtocol (
505 Handle,
506 &gEfiLoadedImageProtocolGuid,
507 (VOID **)&LoadedImage
508 );
509
510 if (EFI_ERROR (Status)) {
511 //
512 // Try Handle as Controller Handle
513 //
514 Status = gBS->OpenProtocol (
515 Handle,
516 &gEfiDriverBindingProtocolGuid,
517 (VOID **)&DriverBinding,
518 NULL,
519 NULL,
520 EFI_OPEN_PROTOCOL_GET_PROTOCOL
521 );
522 if (!EFI_ERROR (Status)) {
523 //
524 // Get Image protocol from ImageHandle
525 //
526 Status = gBS->HandleProtocol (
527 DriverBinding->ImageHandle,
528 &gEfiLoadedImageProtocolGuid,
529 (VOID **)&LoadedImage
530 );
531 }
532 }
533 }
534
535 if (!EFI_ERROR (Status) && (LoadedImage != NULL)) {
536 //
537 // Get Module Guid from DevicePath.
538 //
539 if ((LoadedImage->FilePath != NULL) &&
540 (LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH) &&
541 (LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP)
542 )
543 {
544 //
545 // Determine GUID associated with module logging performance
546 //
547 ModuleGuidIsGet = TRUE;
548 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)LoadedImage->FilePath;
549 TempGuid = &FvFilePath->FvFileName;
550 }
551
552 //
553 // Method 1 Get Module Name from PDB string.
554 //
555 PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);
556 if ((PdbFileName != NULL) && (BufferSize > 0)) {
557 StartIndex = 0;
558 for (Index = 0; PdbFileName[Index] != 0; Index++) {
559 if ((PdbFileName[Index] == '\\') || (PdbFileName[Index] == '/')) {
560 StartIndex = Index + 1;
561 }
562 }
563
564 //
565 // Copy the PDB file name to our temporary string.
566 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.
567 //
568 for (Index = 0; Index < BufferSize - 1; Index++) {
569 NameString[Index] = PdbFileName[Index + StartIndex];
570 if ((NameString[Index] == 0) || (NameString[Index] == '.')) {
571 NameString[Index] = 0;
572 break;
573 }
574 }
575
576 if (Index == BufferSize - 1) {
577 NameString[Index] = 0;
578 }
579
580 //
581 // Module Name is got.
582 //
583 goto Done;
584 }
585 }
586
587 //
588 // Method 2: Get the name string from ComponentName2 protocol
589 //
590 Status = gBS->HandleProtocol (
591 Handle,
592 &gEfiComponentName2ProtocolGuid,
593 (VOID **)&ComponentName2
594 );
595 if (!EFI_ERROR (Status)) {
596 //
597 // Get the current platform language setting
598 //
599 if (mPlatformLanguage == NULL) {
600 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&mPlatformLanguage, NULL);
601 }
602
603 if (mPlatformLanguage != NULL) {
604 Status = ComponentName2->GetDriverName (
605 ComponentName2,
606 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",
607 &StringPtr
608 );
609 if (!EFI_ERROR (Status)) {
610 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {
611 NameString[Index] = (CHAR8)StringPtr[Index];
612 }
613
614 NameString[Index] = 0;
615 //
616 // Module Name is got.
617 //
618 goto Done;
619 }
620 }
621 }
622
623 if (ModuleGuidIsGet) {
624 //
625 // Method 3 Try to get the image's FFS UI section by image GUID
626 //
627 StringPtr = NULL;
628 StringSize = 0;
629 Status = GetSectionFromAnyFv (
630 TempGuid,
631 EFI_SECTION_USER_INTERFACE,
632 0,
633 (VOID **)&StringPtr,
634 &StringSize
635 );
636
637 if (!EFI_ERROR (Status)) {
638 //
639 // Method 3. Get the name string from FFS UI section
640 //
641 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {
642 NameString[Index] = (CHAR8)StringPtr[Index];
643 }
644
645 NameString[Index] = 0;
646 FreePool (StringPtr);
647 }
648 }
649
650 Done:
651 //
652 // Copy Module Guid
653 //
654 if (ModuleGuid != NULL) {
655 CopyGuid (ModuleGuid, TempGuid);
656 if (IsZeroGuid (TempGuid) && (Handle != NULL) && !ModuleGuidIsGet) {
657 // Handle is GUID
658 CopyGuid (ModuleGuid, (EFI_GUID *)Handle);
659 }
660 }
661
662 //
663 // Cache the Handle and Guid pairs.
664 //
665 if (mCachePairCount < CACHE_HANDLE_GUID_COUNT) {
666 mCacheHandleGuidTable[mCachePairCount].Handle = Handle;
667 CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, ModuleGuid);
668 AsciiStrCpyS (mCacheHandleGuidTable[mCachePairCount].NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, NameString);
669 mCachePairCount++;
670 }
671
672 return Status;
673 }
674
675 /**
676 Get the FPDT record identifier.
677
678 @param Attribute The attribute of the Record.
679 PerfStartEntry: Start Record.
680 PerfEndEntry: End Record.
681 @param Handle Pointer to environment specific context used to identify the component being measured.
682 @param String Pointer to a Null-terminated ASCII string that identifies the component being measured.
683 @param ProgressID On return, pointer to the ProgressID.
684
685 @retval EFI_SUCCESS Get record info successfully.
686 @retval EFI_INVALID_PARAMETER No matched FPDT record.
687
688 **/
689 EFI_STATUS
690 GetFpdtRecordId (
691 IN PERF_MEASUREMENT_ATTRIBUTE Attribute,
692 IN CONST VOID *Handle,
693 IN CONST CHAR8 *String,
694 OUT UINT16 *ProgressID
695 )
696 {
697 //
698 // Token to PerfId.
699 //
700 if (String != NULL) {
701 if (AsciiStrCmp (String, START_IMAGE_TOK) == 0) {
702 // "StartImage:"
703 if (Attribute == PerfStartEntry) {
704 *ProgressID = MODULE_START_ID;
705 } else {
706 *ProgressID = MODULE_END_ID;
707 }
708 } else if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) {
709 // "LoadImage:"
710 if (Attribute == PerfStartEntry) {
711 *ProgressID = MODULE_LOADIMAGE_START_ID;
712 } else {
713 *ProgressID = MODULE_LOADIMAGE_END_ID;
714 }
715 } else if (AsciiStrCmp (String, DRIVERBINDING_START_TOK) == 0) {
716 // "DB:Start:"
717 if (Attribute == PerfStartEntry) {
718 *ProgressID = MODULE_DB_START_ID;
719 } else {
720 *ProgressID = MODULE_DB_END_ID;
721 }
722 } else if (AsciiStrCmp (String, DRIVERBINDING_SUPPORT_TOK) == 0) {
723 // "DB:Support:"
724 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
725 return RETURN_UNSUPPORTED;
726 }
727
728 if (Attribute == PerfStartEntry) {
729 *ProgressID = MODULE_DB_SUPPORT_START_ID;
730 } else {
731 *ProgressID = MODULE_DB_SUPPORT_END_ID;
732 }
733 } else if (AsciiStrCmp (String, DRIVERBINDING_STOP_TOK) == 0) {
734 // "DB:Stop:"
735 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
736 return RETURN_UNSUPPORTED;
737 }
738
739 if (Attribute == PerfStartEntry) {
740 *ProgressID = MODULE_DB_STOP_START_ID;
741 } else {
742 *ProgressID = MODULE_DB_STOP_END_ID;
743 }
744 } else if ((AsciiStrCmp (String, PEI_TOK) == 0) || // "PEI"
745 (AsciiStrCmp (String, DXE_TOK) == 0) || // "DXE"
746 (AsciiStrCmp (String, BDS_TOK) == 0)) // "BDS"
747 {
748 if (Attribute == PerfStartEntry) {
749 *ProgressID = PERF_CROSSMODULE_START_ID;
750 } else {
751 *ProgressID = PERF_CROSSMODULE_END_ID;
752 }
753 } else {
754 // Pref used in Modules.
755 if (Attribute == PerfStartEntry) {
756 *ProgressID = PERF_INMODULE_START_ID;
757 } else {
758 *ProgressID = PERF_INMODULE_END_ID;
759 }
760 }
761 } else if (Handle != NULL) {
762 // Pref used in Modules.
763 if (Attribute == PerfStartEntry) {
764 *ProgressID = PERF_INMODULE_START_ID;
765 } else {
766 *ProgressID = PERF_INMODULE_END_ID;
767 }
768 } else {
769 return EFI_INVALID_PARAMETER;
770 }
771
772 return EFI_SUCCESS;
773 }
774
775 /**
776 Copies the string from Source into Destination and updates Length with the
777 size of the string.
778
779 @param Destination - destination of the string copy
780 @param Source - pointer to the source string which will get copied
781 @param Length - pointer to a length variable to be updated
782
783 **/
784 VOID
785 CopyStringIntoPerfRecordAndUpdateLength (
786 IN OUT CHAR8 *Destination,
787 IN CONST CHAR8 *Source,
788 IN OUT UINT8 *Length
789 )
790 {
791 UINTN StringLen;
792 UINTN DestMax;
793
794 ASSERT (Source != NULL);
795
796 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
797 DestMax = STRING_SIZE;
798 } else {
799 DestMax = AsciiStrSize (Source);
800 if (DestMax > STRING_SIZE) {
801 DestMax = STRING_SIZE;
802 }
803 }
804
805 StringLen = AsciiStrLen (Source);
806 if (StringLen >= DestMax) {
807 StringLen = DestMax -1;
808 }
809
810 AsciiStrnCpyS (Destination, DestMax, Source, StringLen);
811 *Length += (UINT8)DestMax;
812
813 return;
814 }
815
816 /**
817 Get a string description for device for the given controller handle and update record
818 length. If ComponentName2 GetControllerName is supported, the value is included in the string,
819 followed by device path, otherwise just device path.
820
821 @param Handle - Image handle
822 @param ControllerHandle - Controller handle.
823 @param ComponentNameString - Pointer to a location where the string will be saved
824 @param Length - Pointer to record length to be updated
825
826 @retval EFI_SUCCESS - Successfully got string description for device
827 @retval EFI_UNSUPPORTED - Neither ComponentName2 ControllerName nor DevicePath were found
828
829 **/
830 EFI_STATUS
831 GetDeviceInfoFromHandleAndUpdateLength (
832 IN CONST VOID *Handle,
833 IN EFI_HANDLE ControllerHandle,
834 OUT CHAR8 *ComponentNameString,
835 IN OUT UINT8 *Length
836 )
837 {
838 EFI_DEVICE_PATH_PROTOCOL *DevicePathProtocol;
839 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
840 EFI_STATUS Status;
841 CHAR16 *StringPtr;
842 CHAR8 *AsciiStringPtr;
843 UINTN ControllerNameStringSize;
844 UINTN DevicePathStringSize;
845
846 ControllerNameStringSize = 0;
847
848 Status = gBS->HandleProtocol (
849 (EFI_HANDLE)Handle,
850 &gEfiComponentName2ProtocolGuid,
851 (VOID **)&ComponentName2
852 );
853
854 if (!EFI_ERROR (Status)) {
855 //
856 // Get the current platform language setting
857 //
858 if (mPlatformLanguage == NULL) {
859 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&mPlatformLanguage, NULL);
860 }
861
862 Status = ComponentName2->GetControllerName (
863 ComponentName2,
864 ControllerHandle,
865 NULL,
866 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",
867 &StringPtr
868 );
869 }
870
871 if (!EFI_ERROR (Status)) {
872 //
873 // This will produce the size of the unicode string, which is twice as large as the ASCII one
874 // This must be an even number, so ok to divide by 2
875 //
876 ControllerNameStringSize = StrSize (StringPtr) / 2;
877
878 //
879 // The + 1 is because we want to add a space between the ControllerName and the device path
880 //
881 if ((ControllerNameStringSize + (*Length) + 1) > FPDT_MAX_PERF_RECORD_SIZE) {
882 //
883 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record
884 //
885 ControllerNameStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length) - 1;
886 }
887
888 UnicodeStrnToAsciiStrS (StringPtr, ControllerNameStringSize - 1, ComponentNameString, ControllerNameStringSize, &ControllerNameStringSize);
889
890 //
891 // Add a space in the end of the ControllerName
892 //
893 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;
894 *AsciiStringPtr = 0x20;
895 AsciiStringPtr++;
896 *AsciiStringPtr = 0;
897 ControllerNameStringSize++;
898
899 *Length += (UINT8)ControllerNameStringSize;
900 }
901
902 //
903 // This function returns the device path protocol from the handle specified by Handle. If Handle is
904 // NULL or Handle does not contain a device path protocol, then NULL is returned.
905 //
906 DevicePathProtocol = DevicePathFromHandle (ControllerHandle);
907
908 if (DevicePathProtocol != NULL) {
909 StringPtr = ConvertDevicePathToText (DevicePathProtocol, TRUE, FALSE);
910 if (StringPtr != NULL) {
911 //
912 // This will produce the size of the unicode string, which is twice as large as the ASCII one
913 // This must be an even number, so ok to divide by 2
914 //
915 DevicePathStringSize = StrSize (StringPtr) / 2;
916
917 if ((DevicePathStringSize + (*Length)) > FPDT_MAX_PERF_RECORD_SIZE) {
918 //
919 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record
920 //
921 DevicePathStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length);
922 }
923
924 if (ControllerNameStringSize != 0) {
925 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;
926 } else {
927 AsciiStringPtr = ComponentNameString;
928 }
929
930 UnicodeStrnToAsciiStrS (StringPtr, DevicePathStringSize - 1, AsciiStringPtr, DevicePathStringSize, &DevicePathStringSize);
931 *Length += (UINT8)DevicePathStringSize;
932 return EFI_SUCCESS;
933 }
934 }
935
936 return EFI_UNSUPPORTED;
937 }
938
939 /**
940 Create performance record with event description and a timestamp.
941
942 @param CallerIdentifier - Image handle or pointer to caller ID GUID.
943 @param Guid - Pointer to a GUID.
944 @param String - Pointer to a string describing the measurement.
945 @param Ticker - 64-bit time stamp.
946 @param Address - Pointer to a location in memory relevant to the measurement.
947 @param PerfId - Performance identifier describing the type of measurement.
948 @param Attribute - The attribute of the measurement. According to attribute can create a start
949 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,
950 or a general record for other Perf macros.
951
952 @retval EFI_SUCCESS - Successfully created performance record.
953 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.
954 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
955 pointer or invalid PerfId.
956
957 @retval EFI_SUCCESS - Successfully created performance record
958 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records
959 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
960 pointer or invalid PerfId
961
962 **/
963 EFI_STATUS
964 InsertFpdtRecord (
965 IN CONST VOID *CallerIdentifier OPTIONAL,
966 IN CONST VOID *Guid OPTIONAL,
967 IN CONST CHAR8 *String OPTIONAL,
968 IN UINT64 Ticker,
969 IN UINT64 Address OPTIONAL,
970 IN UINT16 PerfId,
971 IN PERF_MEASUREMENT_ATTRIBUTE Attribute
972 )
973 {
974 EFI_GUID ModuleGuid;
975 CHAR8 ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];
976 FPDT_RECORD_PTR FpdtRecordPtr;
977 FPDT_RECORD_PTR CachedFpdtRecordPtr;
978 UINT64 TimeStamp;
979 CONST CHAR8 *StringPtr;
980 UINTN DestMax;
981 UINTN StringLen;
982 EFI_STATUS Status;
983 UINT16 ProgressId;
984
985 StringPtr = NULL;
986 ProgressId = 0;
987 ZeroMem (ModuleName, sizeof (ModuleName));
988
989 //
990 // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.
991 // notes: For other Perf macros (Attribute == PerfEntry), their Id is known.
992 //
993 if (Attribute != PerfEntry) {
994 //
995 // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.
996 // !!! Note: If the Perf is not the known Token used in the core but have same
997 // ID with the core Token, this case will not be supported.
998 // And in currtnt usage mode, for the unkown ID, there is a general rule:
999 // If it is start pref: the lower 4 bits of the ID should be 0.
1000 // If it is end pref: the lower 4 bits of the ID should not be 0.
1001 // If input ID doesn't follow the rule, we will adjust it.
1002 //
1003 if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {
1004 return EFI_INVALID_PARAMETER;
1005 } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {
1006 if ((Attribute == PerfStartEntry) && ((PerfId & 0x000F) != 0)) {
1007 PerfId &= 0xFFF0;
1008 } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {
1009 PerfId += 1;
1010 }
1011 } else if (PerfId == 0) {
1012 //
1013 // Get ProgressID form the String Token.
1014 //
1015 Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &ProgressId);
1016 if (EFI_ERROR (Status)) {
1017 return Status;
1018 }
1019
1020 PerfId = ProgressId;
1021 }
1022 }
1023
1024 //
1025 // 2. Get the buffer to store the FPDT record.
1026 //
1027 Status = GetFpdtRecordPtr (FPDT_MAX_PERF_RECORD_SIZE, &FpdtRecordPtr);
1028 if (EFI_ERROR (Status)) {
1029 return Status;
1030 }
1031
1032 //
1033 // 3. Get the TimeStamp.
1034 //
1035 if (Ticker == 0) {
1036 Ticker = GetPerformanceCounter ();
1037 TimeStamp = GetTimeInNanoSecond (Ticker);
1038 } else if (Ticker == 1) {
1039 TimeStamp = 0;
1040 } else {
1041 TimeStamp = GetTimeInNanoSecond (Ticker);
1042 }
1043
1044 //
1045 // 4. Fill in the FPDT record according to different Performance Identifier.
1046 //
1047 switch (PerfId) {
1048 case MODULE_START_ID:
1049 case MODULE_END_ID:
1050 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1051 StringPtr = ModuleName;
1052 //
1053 // Cache the offset of start image start record and use to update the start image end record if needed.
1054 //
1055 if ((Attribute == PerfEntry) && (PerfId == MODULE_START_ID)) {
1056 if (mFpdtBufferIsReported) {
1057 mCachedLength = mBootRecordSize;
1058 } else {
1059 mCachedLength = mPerformanceLength;
1060 }
1061 }
1062
1063 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1064 FpdtRecordPtr.GuidEvent->Header.Type = FPDT_GUID_EVENT_TYPE;
1065 FpdtRecordPtr.GuidEvent->Header.Length = sizeof (FPDT_GUID_EVENT_RECORD);
1066 FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1067 FpdtRecordPtr.GuidEvent->ProgressID = PerfId;
1068 FpdtRecordPtr.GuidEvent->Timestamp = TimeStamp;
1069 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));
1070 if ((CallerIdentifier == NULL) && (PerfId == MODULE_END_ID) && (mCachedLength != 0)) {
1071 if (mFpdtBufferIsReported) {
1072 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1073 } else {
1074 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1075 }
1076
1077 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &CachedFpdtRecordPtr.GuidEvent->Guid, sizeof (FpdtRecordPtr.GuidEvent->Guid));
1078 mCachedLength = 0;
1079 }
1080 }
1081
1082 break;
1083
1084 case MODULE_LOADIMAGE_START_ID:
1085 case MODULE_LOADIMAGE_END_ID:
1086 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1087 StringPtr = ModuleName;
1088 if (PerfId == MODULE_LOADIMAGE_START_ID) {
1089 mLoadImageCount++;
1090 //
1091 // Cache the offset of load image start record and use to be updated by the load image end record if needed.
1092 //
1093 if ((CallerIdentifier == NULL) && (Attribute == PerfEntry)) {
1094 if (mFpdtBufferIsReported) {
1095 mCachedLength = mBootRecordSize;
1096 } else {
1097 mCachedLength = mPerformanceLength;
1098 }
1099 }
1100 }
1101
1102 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1103 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;
1104 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);
1105 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1106 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;
1107 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;
1108 FpdtRecordPtr.GuidQwordEvent->Qword = mLoadImageCount;
1109 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));
1110 if ((PerfId == MODULE_LOADIMAGE_END_ID) && (mCachedLength != 0)) {
1111 if (mFpdtBufferIsReported) {
1112 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1113 } else {
1114 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1115 }
1116
1117 CopyMem (&CachedFpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.GuidQwordEvent->Guid));
1118 mCachedLength = 0;
1119 }
1120 }
1121
1122 break;
1123
1124 case MODULE_DB_START_ID:
1125 case MODULE_DB_SUPPORT_START_ID:
1126 case MODULE_DB_SUPPORT_END_ID:
1127 case MODULE_DB_STOP_START_ID:
1128 case MODULE_DB_STOP_END_ID:
1129 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1130 StringPtr = ModuleName;
1131 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1132 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;
1133 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);
1134 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1135 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;
1136 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;
1137 FpdtRecordPtr.GuidQwordEvent->Qword = Address;
1138 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));
1139 }
1140
1141 break;
1142
1143 case MODULE_DB_END_ID:
1144 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1145 StringPtr = ModuleName;
1146 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1147 FpdtRecordPtr.GuidQwordStringEvent->Header.Type = FPDT_GUID_QWORD_STRING_EVENT_TYPE;
1148 FpdtRecordPtr.GuidQwordStringEvent->Header.Length = sizeof (FPDT_GUID_QWORD_STRING_EVENT_RECORD);
1149 FpdtRecordPtr.GuidQwordStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1150 FpdtRecordPtr.GuidQwordStringEvent->ProgressID = PerfId;
1151 FpdtRecordPtr.GuidQwordStringEvent->Timestamp = TimeStamp;
1152 FpdtRecordPtr.GuidQwordStringEvent->Qword = Address;
1153 CopyMem (&FpdtRecordPtr.GuidQwordStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordStringEvent->Guid));
1154 if (Address != 0) {
1155 GetDeviceInfoFromHandleAndUpdateLength (CallerIdentifier, (EFI_HANDLE)(UINTN)Address, FpdtRecordPtr.GuidQwordStringEvent->String, &FpdtRecordPtr.GuidQwordStringEvent->Header.Length);
1156 }
1157 }
1158
1159 break;
1160
1161 case PERF_EVENTSIGNAL_START_ID:
1162 case PERF_EVENTSIGNAL_END_ID:
1163 case PERF_CALLBACK_START_ID:
1164 case PERF_CALLBACK_END_ID:
1165 if ((String == NULL) || (Guid == NULL)) {
1166 return EFI_INVALID_PARAMETER;
1167 }
1168
1169 StringPtr = String;
1170 if (AsciiStrLen (String) == 0) {
1171 StringPtr = "unknown name";
1172 }
1173
1174 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1175 FpdtRecordPtr.DualGuidStringEvent->Header.Type = FPDT_DUAL_GUID_STRING_EVENT_TYPE;
1176 FpdtRecordPtr.DualGuidStringEvent->Header.Length = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);
1177 FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1178 FpdtRecordPtr.DualGuidStringEvent->ProgressID = PerfId;
1179 FpdtRecordPtr.DualGuidStringEvent->Timestamp = TimeStamp;
1180 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, CallerIdentifier, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));
1181 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));
1182 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);
1183 }
1184
1185 break;
1186
1187 case PERF_EVENT_ID:
1188 case PERF_FUNCTION_START_ID:
1189 case PERF_FUNCTION_END_ID:
1190 case PERF_INMODULE_START_ID:
1191 case PERF_INMODULE_END_ID:
1192 case PERF_CROSSMODULE_START_ID:
1193 case PERF_CROSSMODULE_END_ID:
1194 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1195 if (String != NULL) {
1196 StringPtr = String;
1197 } else {
1198 StringPtr = ModuleName;
1199 }
1200
1201 if (AsciiStrLen (StringPtr) == 0) {
1202 StringPtr = "unknown name";
1203 }
1204
1205 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1206 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1207 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1208 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1209 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1210 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1211 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1212 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1213 }
1214
1215 break;
1216
1217 default:
1218 if (Attribute != PerfEntry) {
1219 GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);
1220 if (String != NULL) {
1221 StringPtr = String;
1222 } else {
1223 StringPtr = ModuleName;
1224 }
1225
1226 if (AsciiStrLen (StringPtr) == 0) {
1227 StringPtr = "unknown name";
1228 }
1229
1230 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1231 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1232 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1233 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1234 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1235 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1236 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1237 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1238 }
1239 } else {
1240 return EFI_INVALID_PARAMETER;
1241 }
1242
1243 break;
1244 }
1245
1246 //
1247 // 4.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.
1248 //
1249 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {
1250 if ((StringPtr == NULL) || (PerfId == MODULE_DB_SUPPORT_START_ID) || (PerfId == MODULE_DB_SUPPORT_END_ID)) {
1251 return EFI_INVALID_PARAMETER;
1252 }
1253
1254 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;
1255 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1256 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;
1257 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;
1258 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;
1259 if (Guid != NULL) {
1260 //
1261 // Cache the event guid in string event record.
1262 //
1263 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, Guid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1264 } else {
1265 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));
1266 }
1267
1268 if (AsciiStrLen (StringPtr) == 0) {
1269 StringPtr = "unknown name";
1270 }
1271
1272 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);
1273
1274 if ((PerfId == MODULE_LOADIMAGE_START_ID) || (PerfId == MODULE_END_ID)) {
1275 FpdtRecordPtr.DynamicStringEvent->Header.Length = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)+ STRING_SIZE);
1276 }
1277
1278 if (((PerfId == MODULE_LOADIMAGE_END_ID) || (PerfId == MODULE_END_ID)) && (mCachedLength != 0)) {
1279 if (mFpdtBufferIsReported) {
1280 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);
1281 } else {
1282 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);
1283 }
1284
1285 if (PerfId == MODULE_LOADIMAGE_END_ID) {
1286 DestMax = CachedFpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1287 StringLen = AsciiStrLen (StringPtr);
1288 if (StringLen >= DestMax) {
1289 StringLen = DestMax -1;
1290 }
1291
1292 CopyMem (&CachedFpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));
1293 AsciiStrnCpyS (CachedFpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StringLen);
1294 } else if (PerfId == MODULE_END_ID) {
1295 DestMax = FpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);
1296 StringLen = AsciiStrLen (CachedFpdtRecordPtr.DynamicStringEvent->String);
1297 if (StringLen >= DestMax) {
1298 StringLen = DestMax -1;
1299 }
1300
1301 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &CachedFpdtRecordPtr.DynamicStringEvent->Guid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));
1302 AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, CachedFpdtRecordPtr.DynamicStringEvent->String, StringLen);
1303 }
1304
1305 mCachedLength = 0;
1306 }
1307 }
1308
1309 //
1310 // 5. Update the length of the used buffer after fill in the record.
1311 //
1312 if (mFpdtBufferIsReported) {
1313 mBootRecordSize += FpdtRecordPtr.RecordHeader->Length;
1314 mAcpiBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;
1315 } else {
1316 mPerformanceLength += FpdtRecordPtr.RecordHeader->Length;
1317 }
1318
1319 return EFI_SUCCESS;
1320 }
1321
1322 /**
1323 Dumps all the PEI performance.
1324
1325 @param HobStart A pointer to a Guid.
1326
1327 This internal function dumps all the PEI performance log to the DXE performance gauge array.
1328 It retrieves the optional GUID HOB for PEI performance and then saves the performance data
1329 to DXE performance data structures.
1330
1331 **/
1332 VOID
1333 InternalGetPeiPerformance (
1334 VOID *HobStart
1335 )
1336 {
1337 UINT8 *FirmwarePerformanceHob;
1338 FPDT_PEI_EXT_PERF_HEADER *PeiPerformanceLogHeader;
1339 UINT8 *EventRec;
1340 EFI_HOB_GUID_TYPE *GuidHob;
1341
1342 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, HobStart);
1343 while (GuidHob != NULL) {
1344 FirmwarePerformanceHob = GET_GUID_HOB_DATA (GuidHob);
1345 PeiPerformanceLogHeader = (FPDT_PEI_EXT_PERF_HEADER *)FirmwarePerformanceHob;
1346
1347 if (mPerformanceLength + PeiPerformanceLogHeader->SizeOfAllEntries > mMaxPerformanceLength) {
1348 mPerformancePointer = ReallocatePool (
1349 mPerformanceLength,
1350 mPerformanceLength +
1351 (UINTN)PeiPerformanceLogHeader->SizeOfAllEntries +
1352 FIRMWARE_RECORD_BUFFER,
1353 mPerformancePointer
1354 );
1355 ASSERT (mPerformancePointer != NULL);
1356 mMaxPerformanceLength = mPerformanceLength +
1357 (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries) +
1358 FIRMWARE_RECORD_BUFFER;
1359 }
1360
1361 EventRec = mPerformancePointer + mPerformanceLength;
1362 CopyMem (EventRec, FirmwarePerformanceHob + sizeof (FPDT_PEI_EXT_PERF_HEADER), (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries));
1363 //
1364 // Update the used buffer size.
1365 //
1366 mPerformanceLength += (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries);
1367 mLoadImageCount += PeiPerformanceLogHeader->LoadImageCount;
1368
1369 //
1370 // Get next performance guid hob
1371 //
1372 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, GET_NEXT_HOB (GuidHob));
1373 }
1374 }
1375
1376 /**
1377 Report Boot Perforamnce table address as report status code.
1378
1379 @param Event The event of notify protocol.
1380 @param Context Notify event context.
1381
1382 **/
1383 VOID
1384 EFIAPI
1385 ReportFpdtRecordBuffer (
1386 IN EFI_EVENT Event,
1387 IN VOID *Context
1388 )
1389 {
1390 EFI_STATUS Status;
1391 UINT64 BPDTAddr;
1392
1393 if (!mFpdtBufferIsReported) {
1394 Status = AllocateBootPerformanceTable ();
1395 if (!EFI_ERROR (Status)) {
1396 BPDTAddr = (UINT64)(UINTN)mAcpiBootPerformanceTable;
1397 REPORT_STATUS_CODE_EX (
1398 EFI_PROGRESS_CODE,
1399 EFI_SOFTWARE_DXE_BS_DRIVER,
1400 0,
1401 NULL,
1402 &gEdkiiFpdtExtendedFirmwarePerformanceGuid,
1403 &BPDTAddr,
1404 sizeof (UINT64)
1405 );
1406 }
1407
1408 //
1409 // Set FPDT report state to TRUE.
1410 //
1411 mFpdtBufferIsReported = TRUE;
1412 }
1413 }
1414
1415 /**
1416 Update Boot Performance table.
1417
1418 @param Event The event of notify protocol.
1419 @param Context Notify event context.
1420
1421 **/
1422 VOID
1423 EFIAPI
1424 UpdateBootPerformanceTable (
1425 IN EFI_EVENT Event,
1426 IN VOID *Context
1427 )
1428 {
1429 VOID *SmmBootRecordData;
1430 UINTN SmmBootRecordDataSize;
1431 UINTN AppendSize;
1432 UINT8 *FirmwarePerformanceTablePtr;
1433
1434 SmmBootRecordDataSize = 0;
1435
1436 //
1437 // Get SMM performance data.
1438 //
1439 SmmBootRecordData = NULL;
1440 InternalGetSmmPerfData (&SmmBootRecordData, &SmmBootRecordDataSize, FALSE);
1441
1442 FirmwarePerformanceTablePtr = (UINT8 *)mAcpiBootPerformanceTable + mAcpiBootPerformanceTable->Header.Length;
1443
1444 if (mAcpiBootPerformanceTable->Header.Length + SmmBootRecordDataSize > mBootRecordMaxSize) {
1445 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: No enough space to save all SMM boot performance data\n"));
1446 AppendSize = mBootRecordMaxSize - mAcpiBootPerformanceTable->Header.Length;
1447 } else {
1448 AppendSize = SmmBootRecordDataSize;
1449 }
1450
1451 if (SmmBootRecordData != NULL) {
1452 CopyMem (FirmwarePerformanceTablePtr, SmmBootRecordData, AppendSize);
1453 mAcpiBootPerformanceTable->Header.Length += (UINT32)AppendSize;
1454 mBootRecordSize += (UINT32)AppendSize;
1455 FreePool (SmmBootRecordData);
1456 }
1457 }
1458
1459 /**
1460 The constructor function initializes Performance infrastructure for DXE phase.
1461
1462 The constructor function publishes Performance and PerformanceEx protocol, allocates memory to log DXE performance
1463 and merges PEI performance data to DXE performance log.
1464 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
1465
1466 @param ImageHandle The firmware allocated handle for the EFI image.
1467 @param SystemTable A pointer to the EFI System Table.
1468
1469 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
1470
1471 **/
1472 EFI_STATUS
1473 EFIAPI
1474 DxeCorePerformanceLibConstructor (
1475 IN EFI_HANDLE ImageHandle,
1476 IN EFI_SYSTEM_TABLE *SystemTable
1477 )
1478 {
1479 EFI_STATUS Status;
1480 EFI_HANDLE Handle;
1481 EFI_EVENT EndOfDxeEvent;
1482 EFI_EVENT ReadyToBootEvent;
1483 PERFORMANCE_PROPERTY *PerformanceProperty;
1484
1485 if (!PerformanceMeasurementEnabled ()) {
1486 //
1487 // Do not initialize performance infrastructure if not required.
1488 //
1489 return EFI_SUCCESS;
1490 }
1491
1492 //
1493 // Dump normal PEI performance records
1494 //
1495 InternalGetPeiPerformance (GetHobList ());
1496
1497 //
1498 // Install the protocol interfaces for DXE performance library instance.
1499 //
1500 Handle = NULL;
1501 Status = gBS->InstallMultipleProtocolInterfaces (
1502 &Handle,
1503 &gEdkiiPerformanceMeasurementProtocolGuid,
1504 &mPerformanceMeasurementInterface,
1505 NULL
1506 );
1507 ASSERT_EFI_ERROR (Status);
1508
1509 //
1510 // Register EndOfDxe event to allocate the boot performance table and report the table address through status code.
1511 //
1512 Status = gBS->CreateEventEx (
1513 EVT_NOTIFY_SIGNAL,
1514 TPL_NOTIFY,
1515 ReportFpdtRecordBuffer,
1516 NULL,
1517 &gEfiEndOfDxeEventGroupGuid,
1518 &EndOfDxeEvent
1519 );
1520
1521 //
1522 // Register ReadyToBoot event to update the boot performance table for SMM performance data.
1523 //
1524 Status = gBS->CreateEventEx (
1525 EVT_NOTIFY_SIGNAL,
1526 TPL_CALLBACK,
1527 UpdateBootPerformanceTable,
1528 NULL,
1529 &gEfiEventReadyToBootGuid,
1530 &ReadyToBootEvent
1531 );
1532
1533 ASSERT_EFI_ERROR (Status);
1534
1535 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **)&PerformanceProperty);
1536 if (EFI_ERROR (Status)) {
1537 //
1538 // Install configuration table for performance property.
1539 //
1540 mPerformanceProperty.Revision = PERFORMANCE_PROPERTY_REVISION;
1541 mPerformanceProperty.Reserved = 0;
1542 mPerformanceProperty.Frequency = GetPerformanceCounterProperties (
1543 &mPerformanceProperty.TimerStartValue,
1544 &mPerformanceProperty.TimerEndValue
1545 );
1546 Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);
1547 ASSERT_EFI_ERROR (Status);
1548 }
1549
1550 return EFI_SUCCESS;
1551 }
1552
1553 /**
1554 Create performance record with event description and a timestamp.
1555
1556 @param CallerIdentifier - Image handle or pointer to caller ID GUID.
1557 @param Guid - Pointer to a GUID.
1558 @param String - Pointer to a string describing the measurement.
1559 @param TimeStamp - 64-bit time stamp.
1560 @param Address - Pointer to a location in memory relevant to the measurement.
1561 @param Identifier - Performance identifier describing the type of measurement.
1562 @param Attribute - The attribute of the measurement. According to attribute can create a start
1563 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,
1564 or a general record for other Perf macros.
1565
1566 @retval EFI_SUCCESS - Successfully created performance record.
1567 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.
1568 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL
1569 pointer or invalid PerfId.
1570 **/
1571 EFI_STATUS
1572 EFIAPI
1573 CreatePerformanceMeasurement (
1574 IN CONST VOID *CallerIdentifier,
1575 IN CONST VOID *Guid OPTIONAL,
1576 IN CONST CHAR8 *String OPTIONAL,
1577 IN UINT64 TimeStamp,
1578 IN UINT64 Address OPTIONAL,
1579 IN UINT32 Identifier,
1580 IN PERF_MEASUREMENT_ATTRIBUTE Attribute
1581 )
1582 {
1583 EFI_STATUS Status;
1584
1585 Status = EFI_SUCCESS;
1586
1587 if (mLockInsertRecord) {
1588 return EFI_INVALID_PARAMETER;
1589 }
1590
1591 mLockInsertRecord = TRUE;
1592
1593 Status = InsertFpdtRecord (CallerIdentifier, Guid, String, TimeStamp, Address, (UINT16)Identifier, Attribute);
1594
1595 mLockInsertRecord = FALSE;
1596
1597 return Status;
1598 }
1599
1600 /**
1601 Adds a record at the end of the performance measurement log
1602 that records the start time of a performance measurement.
1603
1604 Adds a record to the end of the performance measurement log
1605 that contains the Handle, Token, Module and Identifier.
1606 The end time of the new record must be set to zero.
1607 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1608 If TimeStamp is zero, the start time in the record is filled in with the value
1609 read from the current time stamp.
1610
1611 @param Handle Pointer to environment specific context used
1612 to identify the component being measured.
1613 @param Token Pointer to a Null-terminated ASCII string
1614 that identifies the component being measured.
1615 @param Module Pointer to a Null-terminated ASCII string
1616 that identifies the module being measured.
1617 @param TimeStamp 64-bit time stamp.
1618 @param Identifier 32-bit identifier. If the value is 0, the created record
1619 is same as the one created by StartPerformanceMeasurement.
1620
1621 @retval RETURN_SUCCESS The start of the measurement was recorded.
1622 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1623
1624 **/
1625 RETURN_STATUS
1626 EFIAPI
1627 StartPerformanceMeasurementEx (
1628 IN CONST VOID *Handle OPTIONAL,
1629 IN CONST CHAR8 *Token OPTIONAL,
1630 IN CONST CHAR8 *Module OPTIONAL,
1631 IN UINT64 TimeStamp,
1632 IN UINT32 Identifier
1633 )
1634 {
1635 CONST CHAR8 *String;
1636
1637 if (Token != NULL) {
1638 String = Token;
1639 } else if (Module != NULL) {
1640 String = Module;
1641 } else {
1642 String = NULL;
1643 }
1644
1645 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);
1646 }
1647
1648 /**
1649 Searches the performance measurement log from the beginning of the log
1650 for the first matching record that contains a zero end time and fills in a valid end time.
1651
1652 Searches the performance measurement log from the beginning of the log
1653 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
1654 If the record can not be found then return RETURN_NOT_FOUND.
1655 If the record is found and TimeStamp is not zero,
1656 then the end time in the record is filled in with the value specified by TimeStamp.
1657 If the record is found and TimeStamp is zero, then the end time in the matching record
1658 is filled in with the current time stamp value.
1659
1660 @param Handle Pointer to environment specific context used
1661 to identify the component being measured.
1662 @param Token Pointer to a Null-terminated ASCII string
1663 that identifies the component being measured.
1664 @param Module Pointer to a Null-terminated ASCII string
1665 that identifies the module being measured.
1666 @param TimeStamp 64-bit time stamp.
1667 @param Identifier 32-bit identifier. If the value is 0, the found record
1668 is same as the one found by EndPerformanceMeasurement.
1669
1670 @retval RETURN_SUCCESS The end of the measurement was recorded.
1671 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1672
1673 **/
1674 RETURN_STATUS
1675 EFIAPI
1676 EndPerformanceMeasurementEx (
1677 IN CONST VOID *Handle OPTIONAL,
1678 IN CONST CHAR8 *Token OPTIONAL,
1679 IN CONST CHAR8 *Module OPTIONAL,
1680 IN UINT64 TimeStamp,
1681 IN UINT32 Identifier
1682 )
1683 {
1684 CONST CHAR8 *String;
1685
1686 if (Token != NULL) {
1687 String = Token;
1688 } else if (Module != NULL) {
1689 String = Module;
1690 } else {
1691 String = NULL;
1692 }
1693
1694 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);
1695 }
1696
1697 /**
1698 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1699 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
1700 and then assign the Identifier with 0.
1701
1702 !!! Not support!!!
1703
1704 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1705 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1706 and the key for the second entry in the log is returned. If the performance log is empty,
1707 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1708 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1709 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1710 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1711 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1712 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1713 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
1714 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1715 If Handle is NULL, then ASSERT().
1716 If Token is NULL, then ASSERT().
1717 If Module is NULL, then ASSERT().
1718 If StartTimeStamp is NULL, then ASSERT().
1719 If EndTimeStamp is NULL, then ASSERT().
1720 If Identifier is NULL, then ASSERT().
1721
1722 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1723 0, then the first performance measurement log entry is retrieved.
1724 On exit, the key of the next performance log entry.
1725 @param Handle Pointer to environment specific context used to identify the component
1726 being measured.
1727 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1728 being measured.
1729 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1730 being measured.
1731 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1732 was started.
1733 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1734 was ended.
1735 @param Identifier Pointer to the 32-bit identifier that was recorded when the measurement
1736 was ended.
1737
1738 @return The key for the next performance log entry (in general case).
1739
1740 **/
1741 UINTN
1742 EFIAPI
1743 GetPerformanceMeasurementEx (
1744 IN UINTN LogEntryKey,
1745 OUT CONST VOID **Handle,
1746 OUT CONST CHAR8 **Token,
1747 OUT CONST CHAR8 **Module,
1748 OUT UINT64 *StartTimeStamp,
1749 OUT UINT64 *EndTimeStamp,
1750 OUT UINT32 *Identifier
1751 )
1752 {
1753 return 0;
1754 }
1755
1756 /**
1757 Adds a record at the end of the performance measurement log
1758 that records the start time of a performance measurement.
1759
1760 Adds a record to the end of the performance measurement log
1761 that contains the Handle, Token, and Module.
1762 The end time of the new record must be set to zero.
1763 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1764 If TimeStamp is zero, the start time in the record is filled in with the value
1765 read from the current time stamp.
1766
1767 @param Handle Pointer to environment specific context used
1768 to identify the component being measured.
1769 @param Token Pointer to a Null-terminated ASCII string
1770 that identifies the component being measured.
1771 @param Module Pointer to a Null-terminated ASCII string
1772 that identifies the module being measured.
1773 @param TimeStamp 64-bit time stamp.
1774
1775 @retval RETURN_SUCCESS The start of the measurement was recorded.
1776 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1777
1778 **/
1779 RETURN_STATUS
1780 EFIAPI
1781 StartPerformanceMeasurement (
1782 IN CONST VOID *Handle OPTIONAL,
1783 IN CONST CHAR8 *Token OPTIONAL,
1784 IN CONST CHAR8 *Module OPTIONAL,
1785 IN UINT64 TimeStamp
1786 )
1787 {
1788 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1789 }
1790
1791 /**
1792 Searches the performance measurement log from the beginning of the log
1793 for the first matching record that contains a zero end time and fills in a valid end time.
1794
1795 Searches the performance measurement log from the beginning of the log
1796 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1797 If the record can not be found then return RETURN_NOT_FOUND.
1798 If the record is found and TimeStamp is not zero,
1799 then the end time in the record is filled in with the value specified by TimeStamp.
1800 If the record is found and TimeStamp is zero, then the end time in the matching record
1801 is filled in with the current time stamp value.
1802
1803 @param Handle Pointer to environment specific context used
1804 to identify the component being measured.
1805 @param Token Pointer to a Null-terminated ASCII string
1806 that identifies the component being measured.
1807 @param Module Pointer to a Null-terminated ASCII string
1808 that identifies the module being measured.
1809 @param TimeStamp 64-bit time stamp.
1810
1811 @retval RETURN_SUCCESS The end of the measurement was recorded.
1812 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1813
1814 **/
1815 RETURN_STATUS
1816 EFIAPI
1817 EndPerformanceMeasurement (
1818 IN CONST VOID *Handle OPTIONAL,
1819 IN CONST CHAR8 *Token OPTIONAL,
1820 IN CONST CHAR8 *Module OPTIONAL,
1821 IN UINT64 TimeStamp
1822 )
1823 {
1824 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1825 }
1826
1827 /**
1828 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1829 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1830 and then eliminate the Identifier.
1831
1832 !!! Not support!!!
1833
1834 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1835 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1836 and the key for the second entry in the log is returned. If the performance log is empty,
1837 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1838 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1839 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1840 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1841 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1842 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1843 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1844 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1845 If Handle is NULL, then ASSERT().
1846 If Token is NULL, then ASSERT().
1847 If Module is NULL, then ASSERT().
1848 If StartTimeStamp is NULL, then ASSERT().
1849 If EndTimeStamp is NULL, then ASSERT().
1850
1851 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1852 0, then the first performance measurement log entry is retrieved.
1853 On exit, the key of the next performance log entry.
1854 @param Handle Pointer to environment specific context used to identify the component
1855 being measured.
1856 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1857 being measured.
1858 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1859 being measured.
1860 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1861 was started.
1862 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1863 was ended.
1864
1865 @return The key for the next performance log entry (in general case).
1866
1867 **/
1868 UINTN
1869 EFIAPI
1870 GetPerformanceMeasurement (
1871 IN UINTN LogEntryKey,
1872 OUT CONST VOID **Handle,
1873 OUT CONST CHAR8 **Token,
1874 OUT CONST CHAR8 **Module,
1875 OUT UINT64 *StartTimeStamp,
1876 OUT UINT64 *EndTimeStamp
1877 )
1878 {
1879 return 0;
1880 }
1881
1882 /**
1883 Returns TRUE if the performance measurement macros are enabled.
1884
1885 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1886 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1887
1888 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1889 PcdPerformanceLibraryPropertyMask is set.
1890 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1891 PcdPerformanceLibraryPropertyMask is clear.
1892
1893 **/
1894 BOOLEAN
1895 EFIAPI
1896 PerformanceMeasurementEnabled (
1897 VOID
1898 )
1899 {
1900 return (BOOLEAN)((PcdGet8 (PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
1901 }
1902
1903 /**
1904 Create performance record with event description and a timestamp.
1905
1906 @param CallerIdentifier - Image handle or pointer to caller ID GUID
1907 @param Guid - Pointer to a GUID
1908 @param String - Pointer to a string describing the measurement
1909 @param Address - Pointer to a location in memory relevant to the measurement
1910 @param Identifier - Performance identifier describing the type of measurement
1911
1912 @retval RETURN_SUCCESS - Successfully created performance record
1913 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records
1914 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL
1915 pointer or invalid PerfId
1916
1917 **/
1918 RETURN_STATUS
1919 EFIAPI
1920 LogPerformanceMeasurement (
1921 IN CONST VOID *CallerIdentifier,
1922 IN CONST VOID *Guid OPTIONAL,
1923 IN CONST CHAR8 *String OPTIONAL,
1924 IN UINT64 Address OPTIONAL,
1925 IN UINT32 Identifier
1926 )
1927 {
1928 return (RETURN_STATUS)CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);
1929 }
1930
1931 /**
1932 Check whether the specified performance measurement can be logged.
1933
1934 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set
1935 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.
1936
1937 @param Type - Type of the performance measurement entry.
1938
1939 @retval TRUE The performance measurement can be logged.
1940 @retval FALSE The performance measurement can NOT be logged.
1941
1942 **/
1943 BOOLEAN
1944 EFIAPI
1945 LogPerformanceMeasurementEnabled (
1946 IN CONST UINTN Type
1947 )
1948 {
1949 //
1950 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.
1951 //
1952 if (PerformanceMeasurementEnabled () && ((PcdGet8 (PcdPerformanceLibraryPropertyMask) & Type) == 0)) {
1953 return TRUE;
1954 }
1955
1956 return FALSE;
1957 }