2 Performance library instance used by SMM Core.
4 This library provides the performance measurement interfaces and initializes performance
5 logging for the SMM phase.
6 It initializes SMM phase performance logging by publishing the SMM Performance and PerformanceEx Protocol,
7 which is consumed by SmmPerformanceLib to logging performance data in SMM phase.
9 This library is mainly used by SMM Core to start performance logging to ensure that
10 SMM Performance and PerformanceEx Protocol are installed at the very beginning of SMM phase.
12 Caution: This module requires additional review when modified.
13 This driver will have external input - performance data and communicate buffer in SMM mode.
14 This external input must be validated carefully to avoid security issue like
15 buffer overflow, integer overflow.
17 SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted input and do basic validation.
19 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
20 This program and the accompanying materials
21 are licensed and made available under the terms and conditions of the BSD License
22 which accompanies this distribution. The full text of the license may be found at
23 http://opensource.org/licenses/bsd-license.php
25 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
26 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
31 #include "SmmCorePerformanceLibInternal.h"
33 #define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))
34 #define FIRMWARE_RECORD_BUFFER 0x1000
35 #define CACHE_HANDLE_GUID_COUNT 0x100
37 SMM_BOOT_PERFORMANCE_TABLE
*mSmmBootPerformanceTable
= NULL
;
41 CHAR8 NameString
[FPDT_STRING_EVENT_RECORD_NAME_LENGTH
];
45 HANDLE_GUID_MAP mCacheHandleGuidTable
[CACHE_HANDLE_GUID_COUNT
];
46 UINTN mCachePairCount
= 0;
48 UINT32 mPerformanceLength
= 0;
49 UINT32 mMaxPerformanceLength
= 0;
50 BOOLEAN mFpdtDataIsReported
= FALSE
;
51 BOOLEAN mLackSpaceIsReport
= FALSE
;
52 CHAR8
*mPlatformLanguage
= NULL
;
53 SPIN_LOCK mSmmFpdtLock
;
54 PERFORMANCE_PROPERTY mPerformanceProperty
;
57 // Interfaces for SMM Performance Protocol.
59 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
66 // Interfaces for SMM PerformanceEx Protocol.
68 PERFORMANCE_EX_PROTOCOL mPerformanceExInterface
= {
75 Check whether the Token is a known one which is uesed by core.
77 @param Token Pointer to a Null-terminated ASCII string
79 @retval TRUE Is a known one used by core.
80 @retval FALSE Not a known one.
88 if (AsciiStrCmp (Token
, SEC_TOK
) == 0 ||
89 AsciiStrCmp (Token
, PEI_TOK
) == 0 ||
90 AsciiStrCmp (Token
, DXE_TOK
) == 0 ||
91 AsciiStrCmp (Token
, BDS_TOK
) == 0 ||
92 AsciiStrCmp (Token
, DRIVERBINDING_START_TOK
) == 0 ||
93 AsciiStrCmp (Token
, DRIVERBINDING_SUPPORT_TOK
) == 0 ||
94 AsciiStrCmp (Token
, DRIVERBINDING_STOP_TOK
) == 0 ||
95 AsciiStrCmp (Token
, LOAD_IMAGE_TOK
) == 0 ||
96 AsciiStrCmp (Token
, START_IMAGE_TOK
) == 0 ||
97 AsciiStrCmp (Token
, PEIM_TOK
) == 0) {
105 Check whether the ID is a known one which map to the known Token.
107 @param Identifier 32-bit identifier.
109 @retval TRUE Is a known one used by core.
110 @retval FALSE Not a known one.
118 if (Identifier
== MODULE_START_ID
||
119 Identifier
== MODULE_END_ID
||
120 Identifier
== MODULE_LOADIMAGE_START_ID
||
121 Identifier
== MODULE_LOADIMAGE_END_ID
||
122 Identifier
== MODULE_DB_START_ID
||
123 Identifier
== MODULE_DB_END_ID
||
124 Identifier
== MODULE_DB_SUPPORT_START_ID
||
125 Identifier
== MODULE_DB_SUPPORT_END_ID
||
126 Identifier
== MODULE_DB_STOP_START_ID
||
127 Identifier
== MODULE_DB_STOP_END_ID
) {
135 Get the FPDT record info.
137 @param IsStart TRUE if the performance log is start log.
138 @param Handle Pointer to environment specific context used
139 to identify the component being measured.
140 @param Token Pointer to a Null-terminated ASCII string
141 that identifies the component being measured.
142 @param Module Pointer to a Null-terminated ASCII string
143 that identifies the module being measured.
144 @param RecordInfo On return, pointer to the info of the record.
145 @param UseModuleName Only useful for FPDT_DYNAMIC_STRING_EVENT_TYPE, indicate that whether need use
146 Module name to fill the string field in the FPDT_DYNAMIC_STRING_EVENT_RECORD.
148 @retval EFI_SUCCESS Get record info successfully.
149 @retval EFI_UNSUPPORTED No matched FPDT record.
155 IN CONST VOID
*Handle
,
156 IN CONST CHAR8
*Token
,
157 IN CONST CHAR8
*Module
,
158 OUT FPDT_BASIC_RECORD_INFO
*RecordInfo
,
159 IN OUT BOOLEAN
*UseModuleName
165 RecordType
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
168 // Token to Type and Id.
171 if (AsciiStrCmp (Token
, START_IMAGE_TOK
) == 0) { // "StartImage:"
172 *UseModuleName
= TRUE
;
173 RecordType
= FPDT_GUID_EVENT_TYPE
;
175 RecordInfo
->ProgressID
= MODULE_START_ID
;
177 RecordInfo
->ProgressID
= MODULE_END_ID
;
179 } else if (AsciiStrCmp (Token
, LOAD_IMAGE_TOK
) == 0) { // "LoadImage:"
180 *UseModuleName
= TRUE
;
181 RecordType
= FPDT_GUID_QWORD_EVENT_TYPE
;
183 RecordInfo
->ProgressID
= MODULE_LOADIMAGE_START_ID
;
185 RecordInfo
->ProgressID
= MODULE_LOADIMAGE_END_ID
;
187 } else { // Pref used in Modules
189 RecordInfo
->ProgressID
= PERF_INMODULE_START_ID
;
191 RecordInfo
->ProgressID
= PERF_INMODULE_END_ID
;
194 } else if (Handle
!= NULL
|| Module
!= NULL
) { // Pref used in Modules
196 RecordInfo
->ProgressID
= PERF_INMODULE_START_ID
;
198 RecordInfo
->ProgressID
= PERF_INMODULE_END_ID
;
201 return EFI_UNSUPPORTED
;
204 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly
)) {
205 RecordType
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
206 RecordInfo
->RecordSize
= sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
) + STRING_SIZE
;
208 switch (RecordType
) {
209 case FPDT_GUID_EVENT_TYPE
:
210 RecordInfo
->RecordSize
= sizeof (FPDT_GUID_EVENT_RECORD
);
213 case FPDT_DYNAMIC_STRING_EVENT_TYPE
:
214 if (*UseModuleName
) {
215 StringSize
= STRING_SIZE
;
216 } else if (Token
!= NULL
) {
217 StringSize
= AsciiStrSize (Token
);
218 } else if (Module
!= NULL
) {
219 StringSize
= AsciiStrSize (Module
);
221 StringSize
= STRING_SIZE
;
223 if (StringSize
> STRING_SIZE
) {
224 StringSize
= STRING_SIZE
;
226 RecordInfo
->RecordSize
= (UINT8
)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
) + StringSize
);
229 case FPDT_GUID_QWORD_EVENT_TYPE
:
230 RecordInfo
->RecordSize
= (UINT8
)sizeof (FPDT_GUID_QWORD_EVENT_RECORD
);
235 // Record type is unsupported in SMM phase.
237 return EFI_UNSUPPORTED
;
241 RecordInfo
->Type
= RecordType
;
246 Get a human readable module name and module guid for the given image handle.
247 If module name can't be found, "" string will return.
248 If module guid can't be found, Zero Guid will return.
250 @param Handle Image handle or Controller handle.
251 @param NameString The ascii string will be filled into it. If not found, null string will return.
252 @param BufferSize Size of the input NameString buffer.
253 @param ModuleGuid Point to the guid buffer to store the got module guid value.
255 @retval EFI_SUCCESS Successfully get module name and guid.
256 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.
257 @retval other value Module Name can't be got.
261 GetModuleInfoFromHandle (
262 IN EFI_HANDLE Handle
,
263 OUT CHAR8
*NameString
,
265 OUT EFI_GUID
*ModuleGuid OPTIONAL
269 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
270 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
276 BOOLEAN ModuleGuidIsGet
;
279 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FvFilePath
;
281 if (NameString
== NULL
|| BufferSize
== 0) {
282 return EFI_INVALID_PARAMETER
;
286 // Try to get the ModuleGuid and name string form the caached array.
288 if (mCachePairCount
> 0) {
289 for (Count
= mCachePairCount
- 1; Count
>= 0; Count
--) {
290 if (Handle
== mCacheHandleGuidTable
[Count
].Handle
) {
291 CopyGuid (ModuleGuid
, &mCacheHandleGuidTable
[Count
].ModuleGuid
);
292 AsciiStrCpyS (NameString
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, mCacheHandleGuidTable
[Count
].NameString
);
298 Status
= EFI_INVALID_PARAMETER
;
300 ModuleGuidIsGet
= FALSE
;
303 // Initialize GUID as zero value.
305 TempGuid
= &gZeroGuid
;
307 // Initialize it as "" string.
311 if (Handle
!= NULL
) {
313 // Try Handle as ImageHandle.
315 Status
= gBS
->HandleProtocol (
317 &gEfiLoadedImageProtocolGuid
,
318 (VOID
**) &LoadedImage
321 if (EFI_ERROR (Status
)) {
323 // Try Handle as Controller Handle
325 Status
= gBS
->OpenProtocol (
327 &gEfiDriverBindingProtocolGuid
,
328 (VOID
**) &DriverBinding
,
331 EFI_OPEN_PROTOCOL_GET_PROTOCOL
333 if (!EFI_ERROR (Status
)) {
335 // Get Image protocol from ImageHandle
337 Status
= gBS
->HandleProtocol (
338 DriverBinding
->ImageHandle
,
339 &gEfiLoadedImageProtocolGuid
,
340 (VOID
**) &LoadedImage
346 if (!EFI_ERROR (Status
) && LoadedImage
!= NULL
) {
348 // Get Module Guid from DevicePath.
350 if (LoadedImage
->FilePath
!= NULL
&&
351 LoadedImage
->FilePath
->Type
== MEDIA_DEVICE_PATH
&&
352 LoadedImage
->FilePath
->SubType
== MEDIA_PIWG_FW_FILE_DP
355 // Determine GUID associated with module logging performance
357 ModuleGuidIsGet
= TRUE
;
358 FvFilePath
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LoadedImage
->FilePath
;
359 TempGuid
= &FvFilePath
->FvFileName
;
363 // Method 1 Get Module Name from PDB string.
365 PdbFileName
= PeCoffLoaderGetPdbPointer (LoadedImage
->ImageBase
);
366 if (PdbFileName
!= NULL
&& BufferSize
> 0) {
368 for (Index
= 0; PdbFileName
[Index
] != 0; Index
++) {
369 if ((PdbFileName
[Index
] == '\\') || (PdbFileName
[Index
] == '/')) {
370 StartIndex
= Index
+ 1;
374 // Copy the PDB file name to our temporary string.
375 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.
377 for (Index
= 0; Index
< BufferSize
- 1; Index
++) {
378 NameString
[Index
] = PdbFileName
[Index
+ StartIndex
];
379 if (NameString
[Index
] == 0 || NameString
[Index
] == '.') {
380 NameString
[Index
] = 0;
385 if (Index
== BufferSize
- 1) {
386 NameString
[Index
] = 0;
389 // Module Name is got.
395 if (ModuleGuidIsGet
) {
397 // Method 2 Try to get the image's FFS UI section by image GUID
401 Status
= GetSectionFromAnyFv (
403 EFI_SECTION_USER_INTERFACE
,
405 (VOID
**) &StringPtr
,
409 if (!EFI_ERROR (Status
)) {
411 // Method 3. Get the name string from FFS UI section
413 for (Index
= 0; Index
< BufferSize
- 1 && StringPtr
[Index
] != 0; Index
++) {
414 NameString
[Index
] = (CHAR8
) StringPtr
[Index
];
416 NameString
[Index
] = 0;
417 FreePool (StringPtr
);
425 if (ModuleGuid
!= NULL
) {
426 CopyGuid (ModuleGuid
, TempGuid
);
427 if (IsZeroGuid(TempGuid
) && (Handle
!= NULL
) && !ModuleGuidIsGet
) {
429 CopyGuid (ModuleGuid
, (EFI_GUID
*) Handle
);
434 // Cache the Handle and Guid pairs.
436 if (mCachePairCount
< CACHE_HANDLE_GUID_COUNT
) {
437 mCacheHandleGuidTable
[mCachePairCount
].Handle
= Handle
;
438 CopyGuid (&mCacheHandleGuidTable
[mCachePairCount
].ModuleGuid
, ModuleGuid
);
439 AsciiStrCpyS (mCacheHandleGuidTable
[mCachePairCount
].NameString
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, NameString
);
447 Add performance log to FPDT boot record table.
449 @param IsStart TRUE if the performance log is start log.
450 @param Handle Pointer to environment specific context used
451 to identify the component being measured.
452 @param Token Pointer to a Null-terminated ASCII string
453 that identifies the component being measured.
454 @param Module Pointer to a Null-terminated ASCII string
455 that identifies the module being measured.
456 @param Ticker 64-bit time stamp.
457 @param Identifier 32-bit identifier. If the value is 0, the created record
458 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
460 @retval EFI_SUCCESS Add FPDT boot record.
461 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
462 @retval EFI_UNSUPPORTED No matched FPDT record.
466 InsertFpdtMeasurement (
468 IN CONST VOID
*Handle
, OPTIONAL
469 IN CONST CHAR8
*Token
, OPTIONAL
470 IN CONST CHAR8
*Module
, OPTIONAL
476 CHAR8 ModuleName
[FPDT_STRING_EVENT_RECORD_NAME_LENGTH
];
478 FPDT_RECORD_PTR FpdtRecordPtr
;
480 FPDT_BASIC_RECORD_INFO RecordInfo
;
483 CONST CHAR8
*StringPtr
;
484 BOOLEAN UseModuleName
;
487 UseModuleName
= FALSE
;
488 ZeroMem (ModuleName
, sizeof (ModuleName
));
491 // Get record info includes type, size, ProgressID.
493 Status
= GetFpdtRecordInfo (IsStart
, Handle
, Token
, Module
, &RecordInfo
, &UseModuleName
);
494 if (EFI_ERROR (Status
)) {
499 // If PERF_START()/PERF_END() have specified the ProgressID,it has high priority.
500 // !!! Note: If the Pref is not the known Token used in the core but have same
501 // ID with the core Token, this case will not be supported.
502 // And in currtnt usage mode, for the unkown ID, there is a general rule:
503 // If it is start pref: the lower 4 bits of the ID should be 0.
504 // If it is end pref: the lower 4 bits of the ID should not be 0.
505 // If input ID doesn't follow the rule, we will adjust it.
507 if ((Identifier
!= 0) && (IsKnownID (Identifier
)) && (!IsKnownTokens (Token
))) {
508 return EFI_UNSUPPORTED
;
509 } else if ((Identifier
!= 0) && (!IsKnownID (Identifier
)) && (!IsKnownTokens (Token
))) {
510 if (IsStart
&& ((Identifier
& 0x000F) != 0)) {
511 Identifier
&= 0xFFF0;
512 } else if ((!IsStart
) && ((Identifier
& 0x000F) == 0)) {
515 RecordInfo
.ProgressID
= (UINT16
)Identifier
;
518 if (mFpdtDataIsReported
) {
520 // Append Boot records after Smm boot performance records have been reported.
522 if (mPerformanceLength
+ RecordInfo
.RecordSize
> mMaxPerformanceLength
) {
523 if (!mLackSpaceIsReport
) {
524 DEBUG ((DEBUG_INFO
, "SmmCorePerformanceLib: No enough space to save boot records\n"));
525 mLackSpaceIsReport
= TRUE
;
527 return EFI_OUT_OF_RESOURCES
;
530 // Covert buffer to FPDT Ptr Union type.
532 FpdtRecordPtr
.RecordHeader
= (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER
*)((UINT8
*)mSmmBootPerformanceTable
+ mSmmBootPerformanceTable
->Header
.Length
);
536 // Check if pre-allocated buffer is full
538 if (mPerformanceLength
+ RecordInfo
.RecordSize
> mMaxPerformanceLength
) {
539 mSmmBootPerformanceTable
= ReallocatePool (
541 mPerformanceLength
+ sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + RecordInfo
.RecordSize
+ FIRMWARE_RECORD_BUFFER
,
542 mSmmBootPerformanceTable
545 if (mSmmBootPerformanceTable
== NULL
) {
546 return EFI_OUT_OF_RESOURCES
;
548 mSmmBootPerformanceTable
->Header
.Length
= sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + mPerformanceLength
;
549 mMaxPerformanceLength
= mPerformanceLength
+ sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + RecordInfo
.RecordSize
+ FIRMWARE_RECORD_BUFFER
;
552 // Covert buffer to FPDT Ptr Union type.
554 FpdtRecordPtr
.RecordHeader
= (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER
*)((UINT8
*)mSmmBootPerformanceTable
+ mSmmBootPerformanceTable
->Header
.Length
);
556 FpdtRecordPtr
.RecordHeader
->Length
= 0;
559 // Get the TimeStamp.
562 Ticker
= GetPerformanceCounter ();
563 TimeStamp
= GetTimeInNanoSecond (Ticker
);
564 } else if (Ticker
== 1) {
567 TimeStamp
= GetTimeInNanoSecond (Ticker
);
571 // Get the ModuleName and ModuleGuid form the handle.
573 GetModuleInfoFromHandle ((EFI_HANDLE
*)Handle
, ModuleName
, sizeof (ModuleName
), &ModuleGuid
);
576 // Fill in the record information.
578 switch (RecordInfo
.Type
) {
579 case FPDT_GUID_EVENT_TYPE
:
580 FpdtRecordPtr
.GuidEvent
->Header
.Type
= FPDT_GUID_EVENT_TYPE
;
581 FpdtRecordPtr
.GuidEvent
->Header
.Length
= RecordInfo
.RecordSize
;
582 FpdtRecordPtr
.GuidEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
583 FpdtRecordPtr
.GuidEvent
->ProgressID
= RecordInfo
.ProgressID
;
584 FpdtRecordPtr
.GuidEvent
->Timestamp
= TimeStamp
;
585 CopyMem (&FpdtRecordPtr
.GuidEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.GuidEvent
->Guid
));
588 case FPDT_DYNAMIC_STRING_EVENT_TYPE
:
589 FpdtRecordPtr
.DynamicStringEvent
->Header
.Type
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
590 FpdtRecordPtr
.DynamicStringEvent
->Header
.Length
= RecordInfo
.RecordSize
;
591 FpdtRecordPtr
.DynamicStringEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
592 FpdtRecordPtr
.DynamicStringEvent
->ProgressID
= RecordInfo
.ProgressID
;
593 FpdtRecordPtr
.DynamicStringEvent
->Timestamp
= TimeStamp
;
594 CopyMem (&FpdtRecordPtr
.DynamicStringEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.DynamicStringEvent
->Guid
));
597 StringPtr
= ModuleName
;
598 } else if (Token
!= NULL
) {
600 } else if (Module
!= NULL
) {
602 } else if (ModuleName
!= NULL
) {
603 StringPtr
= ModuleName
;
605 if (StringPtr
!= NULL
&& AsciiStrLen (StringPtr
) != 0) {
606 StrLength
= AsciiStrLen (StringPtr
);
607 DestMax
= (RecordInfo
.RecordSize
- sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
)) / sizeof (CHAR8
);
608 if (StrLength
>= DestMax
) {
609 StrLength
= DestMax
-1;
611 AsciiStrnCpyS (FpdtRecordPtr
.DynamicStringEvent
->String
, DestMax
, StringPtr
, StrLength
);
613 AsciiStrCpyS (FpdtRecordPtr
.DynamicStringEvent
->String
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, "unknown name");
617 case FPDT_GUID_QWORD_EVENT_TYPE
:
618 FpdtRecordPtr
.GuidQwordEvent
->Header
.Type
= FPDT_GUID_QWORD_EVENT_TYPE
;
619 FpdtRecordPtr
.GuidQwordEvent
->Header
.Length
= RecordInfo
.RecordSize
;
620 FpdtRecordPtr
.GuidQwordEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
621 FpdtRecordPtr
.GuidQwordEvent
->ProgressID
= RecordInfo
.ProgressID
;
622 FpdtRecordPtr
.GuidQwordEvent
->Timestamp
= TimeStamp
;
623 CopyMem (&FpdtRecordPtr
.GuidQwordEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.GuidQwordEvent
->Guid
));
628 // Record is not supported in current SMM phase, return EFI_UNSUPPORTED
630 return EFI_UNSUPPORTED
;
634 // Update the cached FPDT record buffer.
636 mPerformanceLength
+= FpdtRecordPtr
.RecordHeader
->Length
;
637 mSmmBootPerformanceTable
->Header
.Length
+= FpdtRecordPtr
.RecordHeader
->Length
;
643 Adds a record at the end of the performance measurement log
644 that records the start time of a performance measurement.
646 Adds a record to the end of the performance measurement log
647 that contains the Handle, Token, Module and Identifier.
648 The end time of the new record must be set to zero.
649 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
650 If TimeStamp is zero, the start time in the record is filled in with the value
651 read from the current time stamp.
653 @param Handle Pointer to environment specific context used
654 to identify the component being measured.
655 @param Token Pointer to a Null-terminated ASCII string
656 that identifies the component being measured.
657 @param Module Pointer to a Null-terminated ASCII string
658 that identifies the module being measured.
659 @param TimeStamp 64-bit time stamp.
660 @param Identifier 32-bit identifier. If the value is 0, the created record
661 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
663 @retval EFI_SUCCESS The data was read correctly from the device.
664 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
670 IN CONST VOID
*Handle
, OPTIONAL
671 IN CONST CHAR8
*Token
, OPTIONAL
672 IN CONST CHAR8
*Module
, OPTIONAL
679 AcquireSpinLock (&mSmmFpdtLock
);
681 Status
= InsertFpdtMeasurement (TRUE
, Handle
, Token
, Module
, TimeStamp
, Identifier
);
683 ReleaseSpinLock (&mSmmFpdtLock
);
689 Searches the performance measurement log from the beginning of the log
690 for the first matching record that contains a zero end time and fills in a valid end time.
692 Searches the performance measurement log from the beginning of the log
693 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
694 If the record can not be found then return EFI_NOT_FOUND.
695 If the record is found and TimeStamp is not zero,
696 then the end time in the record is filled in with the value specified by TimeStamp.
697 If the record is found and TimeStamp is zero, then the end time in the matching record
698 is filled in with the current time stamp value.
700 @param Handle Pointer to environment specific context used
701 to identify the component being measured.
702 @param Token Pointer to a Null-terminated ASCII string
703 that identifies the component being measured.
704 @param Module Pointer to a Null-terminated ASCII string
705 that identifies the module being measured.
706 @param TimeStamp 64-bit time stamp.
707 @param Identifier 32-bit identifier. If the value is 0, the found record
708 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
710 @retval EFI_SUCCESS The end of the measurement was recorded.
711 @retval EFI_NOT_FOUND The specified measurement record could not be found.
717 IN CONST VOID
*Handle
, OPTIONAL
718 IN CONST CHAR8
*Token
, OPTIONAL
719 IN CONST CHAR8
*Module
, OPTIONAL
726 AcquireSpinLock (&mSmmFpdtLock
);
728 Status
= InsertFpdtMeasurement (FALSE
, Handle
, Token
, Module
, TimeStamp
, Identifier
);
730 ReleaseSpinLock (&mSmmFpdtLock
);
736 Retrieves a previously logged performance measurement.
737 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
738 and then assign the Identifier with 0.
742 Retrieves the performance log entry from the performance log specified by LogEntryKey.
743 If it stands for a valid entry, then EFI_SUCCESS is returned and
744 GaugeDataEntryEx stores the pointer to that entry.
746 @param LogEntryKey The key for the previous performance measurement log entry.
747 If 0, then the first performance measurement log entry is retrieved.
748 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
749 if the retrieval is successful.
751 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
752 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
753 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
754 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
760 IN UINTN LogEntryKey
,
761 OUT GAUGE_DATA_ENTRY_EX
**GaugeDataEntryEx
764 return EFI_UNSUPPORTED
;
768 Adds a record at the end of the performance measurement log
769 that records the start time of a performance measurement.
771 Adds a record to the end of the performance measurement log
772 that contains the Handle, Token, and Module.
773 The end time of the new record must be set to zero.
774 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
775 If TimeStamp is zero, the start time in the record is filled in with the value
776 read from the current time stamp.
778 @param Handle Pointer to environment specific context used
779 to identify the component being measured.
780 @param Token Pointer to a Null-terminated ASCII string
781 that identifies the component being measured.
782 @param Module Pointer to a Null-terminated ASCII string
783 that identifies the module being measured.
784 @param TimeStamp 64-bit time stamp.
786 @retval EFI_SUCCESS The data was read correctly from the device.
787 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
793 IN CONST VOID
*Handle
, OPTIONAL
794 IN CONST CHAR8
*Token
, OPTIONAL
795 IN CONST CHAR8
*Module
, OPTIONAL
799 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
803 Searches the performance measurement log from the beginning of the log
804 for the first matching record that contains a zero end time and fills in a valid end time.
806 Searches the performance measurement log from the beginning of the log
807 for the first record that matches Handle, Token, and Module and has an end time value of zero.
808 If the record can not be found then return EFI_NOT_FOUND.
809 If the record is found and TimeStamp is not zero,
810 then the end time in the record is filled in with the value specified by TimeStamp.
811 If the record is found and TimeStamp is zero, then the end time in the matching record
812 is filled in with the current time stamp value.
814 @param Handle Pointer to environment specific context used
815 to identify the component being measured.
816 @param Token Pointer to a Null-terminated ASCII string
817 that identifies the component being measured.
818 @param Module Pointer to a Null-terminated ASCII string
819 that identifies the module being measured.
820 @param TimeStamp 64-bit time stamp.
822 @retval EFI_SUCCESS The end of the measurement was recorded.
823 @retval EFI_NOT_FOUND The specified measurement record could not be found.
829 IN CONST VOID
*Handle
, OPTIONAL
830 IN CONST CHAR8
*Token
, OPTIONAL
831 IN CONST CHAR8
*Module
, OPTIONAL
835 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
839 Retrieves a previously logged performance measurement.
840 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
841 and then eliminate the Identifier.
845 Retrieves the performance log entry from the performance log specified by LogEntryKey.
846 If it stands for a valid entry, then EFI_SUCCESS is returned and
847 GaugeDataEntry stores the pointer to that entry.
849 @param LogEntryKey The key for the previous performance measurement log entry.
850 If 0, then the first performance measurement log entry is retrieved.
851 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
852 if the retrieval is successful.
854 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
855 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
856 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
857 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
863 IN UINTN LogEntryKey
,
864 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
867 return EFI_UNSUPPORTED
;
872 SmmReadyToBoot protocol notification event handler.
874 @param Protocol Points to the protocol's unique identifier
875 @param Interface Points to the interface instance
876 @param Handle The handle on which the interface was installed
878 @retval EFI_SUCCESS SmmReadyToBootCallback runs successfully
883 SmmReportFpdtRecordData (
884 IN CONST EFI_GUID
*Protocol
,
891 if (!mFpdtDataIsReported
&& mSmmBootPerformanceTable
!= NULL
) {
892 SmmBPDTddr
= (UINT64
)(UINTN
)mSmmBootPerformanceTable
;
893 REPORT_STATUS_CODE_EX (
895 EFI_SOFTWARE_SMM_DRIVER
,
898 &gEdkiiFpdtExtendedFirmwarePerformanceGuid
,
903 // Set FPDT report state to TRUE.
905 mFpdtDataIsReported
= TRUE
;
911 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
912 this function is callbacked to initialize the Smm Performance Lib
914 @param Event The event of notify protocol.
915 @param Context Notify event context.
920 InitializeSmmCorePerformanceLib (
927 VOID
*SmmReadyToBootRegistration
;
928 PERFORMANCE_PROPERTY
*PerformanceProperty
;
931 // Initialize spin lock
933 InitializeSpinLock (&mSmmFpdtLock
);
936 // Install the protocol interfaces for SMM performance library instance.
939 Status
= gSmst
->SmmInstallProtocolInterface (
941 &gSmmPerformanceProtocolGuid
,
942 EFI_NATIVE_INTERFACE
,
943 &mPerformanceInterface
945 ASSERT_EFI_ERROR (Status
);
946 Status
= gSmst
->SmmInstallProtocolInterface (
948 &gSmmPerformanceExProtocolGuid
,
949 EFI_NATIVE_INTERFACE
,
950 &mPerformanceExInterface
952 ASSERT_EFI_ERROR (Status
);
954 Status
= gSmst
->SmmRegisterProtocolNotify (
955 &gEdkiiSmmReadyToBootProtocolGuid
,
956 SmmReportFpdtRecordData
,
957 &SmmReadyToBootRegistration
959 Status
= EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid
, (VOID
**) &PerformanceProperty
);
960 if (EFI_ERROR (Status
)) {
962 // Install configuration table for performance property.
964 mPerformanceProperty
.Revision
= PERFORMANCE_PROPERTY_REVISION
;
965 mPerformanceProperty
.Reserved
= 0;
966 mPerformanceProperty
.Frequency
= GetPerformanceCounterProperties (
967 &mPerformanceProperty
.TimerStartValue
,
968 &mPerformanceProperty
.TimerEndValue
970 Status
= gBS
->InstallConfigurationTable (&gPerformanceProtocolGuid
, &mPerformanceProperty
);
971 ASSERT_EFI_ERROR (Status
);
976 The constructor function initializes the Performance Measurement Enable flag and
977 registers SmmBase2 protocol notify callback.
978 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
980 @param ImageHandle The firmware allocated handle for the EFI image.
981 @param SystemTable A pointer to the EFI System Table.
983 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
988 SmmCorePerformanceLibConstructor (
989 IN EFI_HANDLE ImageHandle
,
990 IN EFI_SYSTEM_TABLE
*SystemTable
997 if (!PerformanceMeasurementEnabled ()) {
999 // Do not initialize performance infrastructure if not required.
1005 // Create the events to do the library init.
1007 Status
= gBS
->CreateEvent (
1010 InitializeSmmCorePerformanceLib
,
1014 ASSERT_EFI_ERROR (Status
);
1017 // Register for protocol notifications on this event
1019 Status
= gBS
->RegisterProtocolNotify (
1020 &gEfiSmmBase2ProtocolGuid
,
1025 ASSERT_EFI_ERROR (Status
);
1031 Adds a record at the end of the performance measurement log
1032 that records the start time of a performance measurement.
1034 Adds a record to the end of the performance measurement log
1035 that contains the Handle, Token, Module and Identifier.
1036 The end time of the new record must be set to zero.
1037 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1038 If TimeStamp is zero, the start time in the record is filled in with the value
1039 read from the current time stamp.
1041 @param Handle Pointer to environment specific context used
1042 to identify the component being measured.
1043 @param Token Pointer to a Null-terminated ASCII string
1044 that identifies the component being measured.
1045 @param Module Pointer to a Null-terminated ASCII string
1046 that identifies the module being measured.
1047 @param TimeStamp 64-bit time stamp.
1048 @param Identifier 32-bit identifier. If the value is 0, the created record
1049 is same as the one created by StartPerformanceMeasurement.
1051 @retval RETURN_SUCCESS The start of the measurement was recorded.
1052 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1057 StartPerformanceMeasurementEx (
1058 IN CONST VOID
*Handle
, OPTIONAL
1059 IN CONST CHAR8
*Token
, OPTIONAL
1060 IN CONST CHAR8
*Module
, OPTIONAL
1061 IN UINT64 TimeStamp
,
1062 IN UINT32 Identifier
1065 return (RETURN_STATUS
) StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
1069 Searches the performance measurement log from the beginning of the log
1070 for the first matching record that contains a zero end time and fills in a valid end time.
1072 Searches the performance measurement log from the beginning of the log
1073 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
1074 If the record can not be found then return RETURN_NOT_FOUND.
1075 If the record is found and TimeStamp is not zero,
1076 then the end time in the record is filled in with the value specified by TimeStamp.
1077 If the record is found and TimeStamp is zero, then the end time in the matching record
1078 is filled in with the current time stamp value.
1080 @param Handle Pointer to environment specific context used
1081 to identify the component being measured.
1082 @param Token Pointer to a Null-terminated ASCII string
1083 that identifies the component being measured.
1084 @param Module Pointer to a Null-terminated ASCII string
1085 that identifies the module being measured.
1086 @param TimeStamp 64-bit time stamp.
1087 @param Identifier 32-bit identifier. If the value is 0, the found record
1088 is same as the one found by EndPerformanceMeasurement.
1090 @retval RETURN_SUCCESS The end of the measurement was recorded.
1091 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1096 EndPerformanceMeasurementEx (
1097 IN CONST VOID
*Handle
, OPTIONAL
1098 IN CONST CHAR8
*Token
, OPTIONAL
1099 IN CONST CHAR8
*Module
, OPTIONAL
1100 IN UINT64 TimeStamp
,
1101 IN UINT32 Identifier
1104 return (RETURN_STATUS
) EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
1108 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1109 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
1110 and then assign the Identifier with 0.
1114 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1115 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1116 and the key for the second entry in the log is returned. If the performance log is empty,
1117 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1118 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1119 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1120 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1121 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1122 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1123 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
1124 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1125 If Handle is NULL, then ASSERT().
1126 If Token is NULL, then ASSERT().
1127 If Module is NULL, then ASSERT().
1128 If StartTimeStamp is NULL, then ASSERT().
1129 If EndTimeStamp is NULL, then ASSERT().
1130 If Identifier is NULL, then ASSERT().
1132 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1133 0, then the first performance measurement log entry is retrieved.
1134 On exit, the key of the next performance log entry.
1135 @param Handle Pointer to environment specific context used to identify the component
1137 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1139 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1141 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1143 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1145 @param Identifier Pointer to the 32-bit identifier that was recorded.
1147 @return The key for the next performance log entry (in general case).
1152 GetPerformanceMeasurementEx (
1153 IN UINTN LogEntryKey
,
1154 OUT CONST VOID
**Handle
,
1155 OUT CONST CHAR8
**Token
,
1156 OUT CONST CHAR8
**Module
,
1157 OUT UINT64
*StartTimeStamp
,
1158 OUT UINT64
*EndTimeStamp
,
1159 OUT UINT32
*Identifier
1166 Adds a record at the end of the performance measurement log
1167 that records the start time of a performance measurement.
1169 Adds a record to the end of the performance measurement log
1170 that contains the Handle, Token, and Module.
1171 The end time of the new record must be set to zero.
1172 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1173 If TimeStamp is zero, the start time in the record is filled in with the value
1174 read from the current time stamp.
1176 @param Handle Pointer to environment specific context used
1177 to identify the component being measured.
1178 @param Token Pointer to a Null-terminated ASCII string
1179 that identifies the component being measured.
1180 @param Module Pointer to a Null-terminated ASCII string
1181 that identifies the module being measured.
1182 @param TimeStamp 64-bit time stamp.
1184 @retval RETURN_SUCCESS The start of the measurement was recorded.
1185 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1190 StartPerformanceMeasurement (
1191 IN CONST VOID
*Handle
, OPTIONAL
1192 IN CONST CHAR8
*Token
, OPTIONAL
1193 IN CONST CHAR8
*Module
, OPTIONAL
1197 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
1201 Searches the performance measurement log from the beginning of the log
1202 for the first matching record that contains a zero end time and fills in a valid end time.
1204 Searches the performance measurement log from the beginning of the log
1205 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1206 If the record can not be found then return RETURN_NOT_FOUND.
1207 If the record is found and TimeStamp is not zero,
1208 then the end time in the record is filled in with the value specified by TimeStamp.
1209 If the record is found and TimeStamp is zero, then the end time in the matching record
1210 is filled in with the current time stamp value.
1212 @param Handle Pointer to environment specific context used
1213 to identify the component being measured.
1214 @param Token Pointer to a Null-terminated ASCII string
1215 that identifies the component being measured.
1216 @param Module Pointer to a Null-terminated ASCII string
1217 that identifies the module being measured.
1218 @param TimeStamp 64-bit time stamp.
1220 @retval RETURN_SUCCESS The end of the measurement was recorded.
1221 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1226 EndPerformanceMeasurement (
1227 IN CONST VOID
*Handle
, OPTIONAL
1228 IN CONST CHAR8
*Token
, OPTIONAL
1229 IN CONST CHAR8
*Module
, OPTIONAL
1233 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
1237 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1238 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1239 and then eliminate the Identifier.
1243 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1244 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1245 and the key for the second entry in the log is returned. If the performance log is empty,
1246 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1247 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1248 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1249 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1250 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1251 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1252 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1253 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1254 If Handle is NULL, then ASSERT().
1255 If Token is NULL, then ASSERT().
1256 If Module is NULL, then ASSERT().
1257 If StartTimeStamp is NULL, then ASSERT().
1258 If EndTimeStamp is NULL, then ASSERT().
1260 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1261 0, then the first performance measurement log entry is retrieved.
1262 On exit, the key of the next performance log entry.
1263 @param Handle Pointer to environment specific context used to identify the component
1265 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1267 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1269 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1271 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1274 @return The key for the next performance log entry (in general case).
1279 GetPerformanceMeasurement (
1280 IN UINTN LogEntryKey
,
1281 OUT CONST VOID
**Handle
,
1282 OUT CONST CHAR8
**Token
,
1283 OUT CONST CHAR8
**Module
,
1284 OUT UINT64
*StartTimeStamp
,
1285 OUT UINT64
*EndTimeStamp
1292 Returns TRUE if the performance measurement macros are enabled.
1294 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1295 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1297 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1298 PcdPerformanceLibraryPropertyMask is set.
1299 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1300 PcdPerformanceLibraryPropertyMask is clear.
1305 PerformanceMeasurementEnabled (
1309 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);