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.
92 if (AsciiStrCmp (Token
, SEC_TOK
) == 0 ||
93 AsciiStrCmp (Token
, PEI_TOK
) == 0 ||
94 AsciiStrCmp (Token
, DXE_TOK
) == 0 ||
95 AsciiStrCmp (Token
, BDS_TOK
) == 0 ||
96 AsciiStrCmp (Token
, DRIVERBINDING_START_TOK
) == 0 ||
97 AsciiStrCmp (Token
, DRIVERBINDING_SUPPORT_TOK
) == 0 ||
98 AsciiStrCmp (Token
, DRIVERBINDING_STOP_TOK
) == 0 ||
99 AsciiStrCmp (Token
, LOAD_IMAGE_TOK
) == 0 ||
100 AsciiStrCmp (Token
, START_IMAGE_TOK
) == 0 ||
101 AsciiStrCmp (Token
, PEIM_TOK
) == 0) {
109 Check whether the ID is a known one which map to the known Token.
111 @param Identifier 32-bit identifier.
113 @retval TRUE Is a known one used by core.
114 @retval FALSE Not a known one.
122 if (Identifier
== MODULE_START_ID
||
123 Identifier
== MODULE_END_ID
||
124 Identifier
== MODULE_LOADIMAGE_START_ID
||
125 Identifier
== MODULE_LOADIMAGE_END_ID
||
126 Identifier
== MODULE_DB_START_ID
||
127 Identifier
== MODULE_DB_END_ID
||
128 Identifier
== MODULE_DB_SUPPORT_START_ID
||
129 Identifier
== MODULE_DB_SUPPORT_END_ID
||
130 Identifier
== MODULE_DB_STOP_START_ID
||
131 Identifier
== MODULE_DB_STOP_END_ID
) {
139 Get the FPDT record info.
141 @param IsStart TRUE if the performance log is start log.
142 @param Handle Pointer to environment specific context used
143 to identify the component being measured.
144 @param Token Pointer to a Null-terminated ASCII string
145 that identifies the component being measured.
146 @param Module Pointer to a Null-terminated ASCII string
147 that identifies the module being measured.
148 @param RecordInfo On return, pointer to the info of the record.
149 @param UseModuleName Only useful for FPDT_DYNAMIC_STRING_EVENT_TYPE, indicate that whether need use
150 Module name to fill the string field in the FPDT_DYNAMIC_STRING_EVENT_RECORD.
152 @retval EFI_SUCCESS Get record info successfully.
153 @retval EFI_UNSUPPORTED No matched FPDT record.
159 IN CONST VOID
*Handle
,
160 IN CONST CHAR8
*Token
,
161 IN CONST CHAR8
*Module
,
162 OUT FPDT_BASIC_RECORD_INFO
*RecordInfo
,
163 IN OUT BOOLEAN
*UseModuleName
169 RecordType
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
172 // Token to Type and Id.
175 if (AsciiStrCmp (Token
, START_IMAGE_TOK
) == 0) { // "StartImage:"
176 *UseModuleName
= TRUE
;
177 RecordType
= FPDT_GUID_EVENT_TYPE
;
179 RecordInfo
->ProgressID
= MODULE_START_ID
;
181 RecordInfo
->ProgressID
= MODULE_END_ID
;
183 } else if (AsciiStrCmp (Token
, LOAD_IMAGE_TOK
) == 0) { // "LoadImage:"
184 *UseModuleName
= TRUE
;
185 RecordType
= FPDT_GUID_QWORD_EVENT_TYPE
;
187 RecordInfo
->ProgressID
= MODULE_LOADIMAGE_START_ID
;
189 RecordInfo
->ProgressID
= MODULE_LOADIMAGE_END_ID
;
191 } else { // Pref used in Modules
193 RecordInfo
->ProgressID
= PERF_INMODULE_START_ID
;
195 RecordInfo
->ProgressID
= PERF_INMODULE_END_ID
;
198 } else if (Handle
!= NULL
|| Module
!= NULL
) { // Pref used in Modules
200 RecordInfo
->ProgressID
= PERF_INMODULE_START_ID
;
202 RecordInfo
->ProgressID
= PERF_INMODULE_END_ID
;
205 return EFI_UNSUPPORTED
;
208 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly
)) {
209 RecordType
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
210 RecordInfo
->RecordSize
= sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
) + STRING_SIZE
;
212 switch (RecordType
) {
213 case FPDT_GUID_EVENT_TYPE
:
214 RecordInfo
->RecordSize
= sizeof (FPDT_GUID_EVENT_RECORD
);
217 case FPDT_DYNAMIC_STRING_EVENT_TYPE
:
218 if (*UseModuleName
) {
219 StringSize
= STRING_SIZE
;
220 } else if (Token
!= NULL
) {
221 StringSize
= AsciiStrSize (Token
);
222 } else if (Module
!= NULL
) {
223 StringSize
= AsciiStrSize (Module
);
225 StringSize
= STRING_SIZE
;
227 if (StringSize
> STRING_SIZE
) {
228 StringSize
= STRING_SIZE
;
230 RecordInfo
->RecordSize
= (UINT8
)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
) + StringSize
);
233 case FPDT_GUID_QWORD_EVENT_TYPE
:
234 RecordInfo
->RecordSize
= (UINT8
)sizeof (FPDT_GUID_QWORD_EVENT_RECORD
);
239 // Record type is unsupported in SMM phase.
241 return EFI_UNSUPPORTED
;
245 RecordInfo
->Type
= RecordType
;
250 Get a human readable module name and module guid for the given image handle.
251 If module name can't be found, "" string will return.
252 If module guid can't be found, Zero Guid will return.
254 @param Handle Image handle or Controller handle.
255 @param NameString The ascii string will be filled into it. If not found, null string will return.
256 @param BufferSize Size of the input NameString buffer.
257 @param ModuleGuid Point to the guid buffer to store the got module guid value.
259 @retval EFI_SUCCESS Successfully get module name and guid.
260 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.
261 @retval other value Module Name can't be got.
265 GetModuleInfoFromHandle (
266 IN EFI_HANDLE Handle
,
267 OUT CHAR8
*NameString
,
269 OUT EFI_GUID
*ModuleGuid OPTIONAL
273 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
274 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
280 BOOLEAN ModuleGuidIsGet
;
283 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FvFilePath
;
285 if (NameString
== NULL
|| BufferSize
== 0) {
286 return EFI_INVALID_PARAMETER
;
290 // Try to get the ModuleGuid and name string form the caached array.
292 if (mCachePairCount
> 0) {
293 for (Count
= mCachePairCount
- 1; Count
>= 0; Count
--) {
294 if (Handle
== mCacheHandleGuidTable
[Count
].Handle
) {
295 CopyGuid (ModuleGuid
, &mCacheHandleGuidTable
[Count
].ModuleGuid
);
296 AsciiStrCpyS (NameString
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, mCacheHandleGuidTable
[Count
].NameString
);
302 Status
= EFI_INVALID_PARAMETER
;
304 ModuleGuidIsGet
= FALSE
;
307 // Initialize GUID as zero value.
309 TempGuid
= &gZeroGuid
;
311 // Initialize it as "" string.
315 if (Handle
!= NULL
) {
317 // Try Handle as ImageHandle.
319 Status
= gBS
->HandleProtocol (
321 &gEfiLoadedImageProtocolGuid
,
322 (VOID
**) &LoadedImage
325 if (EFI_ERROR (Status
)) {
327 // Try Handle as Controller Handle
329 Status
= gBS
->OpenProtocol (
331 &gEfiDriverBindingProtocolGuid
,
332 (VOID
**) &DriverBinding
,
335 EFI_OPEN_PROTOCOL_GET_PROTOCOL
337 if (!EFI_ERROR (Status
)) {
339 // Get Image protocol from ImageHandle
341 Status
= gBS
->HandleProtocol (
342 DriverBinding
->ImageHandle
,
343 &gEfiLoadedImageProtocolGuid
,
344 (VOID
**) &LoadedImage
350 if (!EFI_ERROR (Status
) && LoadedImage
!= NULL
) {
352 // Get Module Guid from DevicePath.
354 if (LoadedImage
->FilePath
!= NULL
&&
355 LoadedImage
->FilePath
->Type
== MEDIA_DEVICE_PATH
&&
356 LoadedImage
->FilePath
->SubType
== MEDIA_PIWG_FW_FILE_DP
359 // Determine GUID associated with module logging performance
361 ModuleGuidIsGet
= TRUE
;
362 FvFilePath
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LoadedImage
->FilePath
;
363 TempGuid
= &FvFilePath
->FvFileName
;
367 // Method 1 Get Module Name from PDB string.
369 PdbFileName
= PeCoffLoaderGetPdbPointer (LoadedImage
->ImageBase
);
370 if (PdbFileName
!= NULL
&& BufferSize
> 0) {
372 for (Index
= 0; PdbFileName
[Index
] != 0; Index
++) {
373 if ((PdbFileName
[Index
] == '\\') || (PdbFileName
[Index
] == '/')) {
374 StartIndex
= Index
+ 1;
378 // Copy the PDB file name to our temporary string.
379 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.
381 for (Index
= 0; Index
< BufferSize
- 1; Index
++) {
382 NameString
[Index
] = PdbFileName
[Index
+ StartIndex
];
383 if (NameString
[Index
] == 0 || NameString
[Index
] == '.') {
384 NameString
[Index
] = 0;
389 if (Index
== BufferSize
- 1) {
390 NameString
[Index
] = 0;
393 // Module Name is got.
399 if (ModuleGuidIsGet
) {
401 // Method 2 Try to get the image's FFS UI section by image GUID
405 Status
= GetSectionFromAnyFv (
407 EFI_SECTION_USER_INTERFACE
,
409 (VOID
**) &StringPtr
,
413 if (!EFI_ERROR (Status
)) {
415 // Method 3. Get the name string from FFS UI section
417 for (Index
= 0; Index
< BufferSize
- 1 && StringPtr
[Index
] != 0; Index
++) {
418 NameString
[Index
] = (CHAR8
) StringPtr
[Index
];
420 NameString
[Index
] = 0;
421 FreePool (StringPtr
);
429 if (ModuleGuid
!= NULL
) {
430 CopyGuid (ModuleGuid
, TempGuid
);
431 if (IsZeroGuid(TempGuid
) && (Handle
!= NULL
) && !ModuleGuidIsGet
) {
433 CopyGuid (ModuleGuid
, (EFI_GUID
*) Handle
);
438 // Cache the Handle and Guid pairs.
440 if (mCachePairCount
< CACHE_HANDLE_GUID_COUNT
) {
441 mCacheHandleGuidTable
[mCachePairCount
].Handle
= Handle
;
442 CopyGuid (&mCacheHandleGuidTable
[mCachePairCount
].ModuleGuid
, ModuleGuid
);
443 AsciiStrCpyS (mCacheHandleGuidTable
[mCachePairCount
].NameString
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, NameString
);
451 Add performance log to FPDT boot record table.
453 @param IsStart TRUE if the performance log is start log.
454 @param Handle Pointer to environment specific context used
455 to identify the component being measured.
456 @param Token Pointer to a Null-terminated ASCII string
457 that identifies the component being measured.
458 @param Module Pointer to a Null-terminated ASCII string
459 that identifies the module being measured.
460 @param Ticker 64-bit time stamp.
461 @param Identifier 32-bit identifier. If the value is 0, the created record
462 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
464 @retval EFI_SUCCESS Add FPDT boot record.
465 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
466 @retval EFI_UNSUPPORTED No matched FPDT record.
470 InsertFpdtMeasurement (
472 IN CONST VOID
*Handle
, OPTIONAL
473 IN CONST CHAR8
*Token
, OPTIONAL
474 IN CONST CHAR8
*Module
, OPTIONAL
480 CHAR8 ModuleName
[FPDT_STRING_EVENT_RECORD_NAME_LENGTH
];
482 FPDT_RECORD_PTR FpdtRecordPtr
;
484 FPDT_BASIC_RECORD_INFO RecordInfo
;
487 CONST CHAR8
*StringPtr
;
488 BOOLEAN UseModuleName
;
491 UseModuleName
= FALSE
;
492 ZeroMem (ModuleName
, sizeof (ModuleName
));
495 // Get record info includes type, size, ProgressID.
497 Status
= GetFpdtRecordInfo (IsStart
, Handle
, Token
, Module
, &RecordInfo
, &UseModuleName
);
498 if (EFI_ERROR (Status
)) {
503 // If PERF_START()/PERF_END() have specified the ProgressID,it has high priority.
504 // !!! Note: If the Perf is not the known Token used in the core but have same
505 // ID with the core Token, this case will not be supported.
506 // And in currtnt usage mode, for the unkown ID, there is a general rule:
507 // If it is start pref: the lower 4 bits of the ID should be 0.
508 // If it is end pref: the lower 4 bits of the ID should not be 0.
509 // If input ID doesn't follow the rule, we will adjust it.
511 if ((Identifier
!= 0) && (IsKnownID (Identifier
)) && (!IsKnownTokens (Token
))) {
512 return EFI_UNSUPPORTED
;
513 } else if ((Identifier
!= 0) && (!IsKnownID (Identifier
)) && (!IsKnownTokens (Token
))) {
514 if (IsStart
&& ((Identifier
& 0x000F) != 0)) {
515 Identifier
&= 0xFFF0;
516 } else if ((!IsStart
) && ((Identifier
& 0x000F) == 0)) {
519 RecordInfo
.ProgressID
= (UINT16
)Identifier
;
522 if (mFpdtDataIsReported
) {
524 // Append Boot records after Smm boot performance records have been reported.
526 if (mPerformanceLength
+ RecordInfo
.RecordSize
> mMaxPerformanceLength
) {
527 if (!mLackSpaceIsReport
) {
528 DEBUG ((DEBUG_INFO
, "SmmCorePerformanceLib: No enough space to save boot records\n"));
529 mLackSpaceIsReport
= TRUE
;
531 return EFI_OUT_OF_RESOURCES
;
534 // Covert buffer to FPDT Ptr Union type.
536 FpdtRecordPtr
.RecordHeader
= (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER
*)((UINT8
*)mSmmBootPerformanceTable
+ mSmmBootPerformanceTable
->Header
.Length
);
540 // Check if pre-allocated buffer is full
542 if (mPerformanceLength
+ RecordInfo
.RecordSize
> mMaxPerformanceLength
) {
543 mSmmBootPerformanceTable
= ReallocatePool (
545 mPerformanceLength
+ sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + RecordInfo
.RecordSize
+ FIRMWARE_RECORD_BUFFER
,
546 mSmmBootPerformanceTable
549 if (mSmmBootPerformanceTable
== NULL
) {
550 return EFI_OUT_OF_RESOURCES
;
552 mSmmBootPerformanceTable
->Header
.Length
= sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + mPerformanceLength
;
553 mMaxPerformanceLength
= mPerformanceLength
+ sizeof (SMM_BOOT_PERFORMANCE_TABLE
) + RecordInfo
.RecordSize
+ FIRMWARE_RECORD_BUFFER
;
556 // Covert buffer to FPDT Ptr Union type.
558 FpdtRecordPtr
.RecordHeader
= (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER
*)((UINT8
*)mSmmBootPerformanceTable
+ mSmmBootPerformanceTable
->Header
.Length
);
560 FpdtRecordPtr
.RecordHeader
->Length
= 0;
563 // Get the TimeStamp.
566 Ticker
= GetPerformanceCounter ();
567 TimeStamp
= GetTimeInNanoSecond (Ticker
);
568 } else if (Ticker
== 1) {
571 TimeStamp
= GetTimeInNanoSecond (Ticker
);
575 // Get the ModuleName and ModuleGuid form the handle.
577 GetModuleInfoFromHandle ((EFI_HANDLE
*)Handle
, ModuleName
, sizeof (ModuleName
), &ModuleGuid
);
580 // Fill in the record information.
582 switch (RecordInfo
.Type
) {
583 case FPDT_GUID_EVENT_TYPE
:
584 FpdtRecordPtr
.GuidEvent
->Header
.Type
= FPDT_GUID_EVENT_TYPE
;
585 FpdtRecordPtr
.GuidEvent
->Header
.Length
= RecordInfo
.RecordSize
;
586 FpdtRecordPtr
.GuidEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
587 FpdtRecordPtr
.GuidEvent
->ProgressID
= RecordInfo
.ProgressID
;
588 FpdtRecordPtr
.GuidEvent
->Timestamp
= TimeStamp
;
589 CopyMem (&FpdtRecordPtr
.GuidEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.GuidEvent
->Guid
));
592 case FPDT_DYNAMIC_STRING_EVENT_TYPE
:
593 FpdtRecordPtr
.DynamicStringEvent
->Header
.Type
= FPDT_DYNAMIC_STRING_EVENT_TYPE
;
594 FpdtRecordPtr
.DynamicStringEvent
->Header
.Length
= RecordInfo
.RecordSize
;
595 FpdtRecordPtr
.DynamicStringEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
596 FpdtRecordPtr
.DynamicStringEvent
->ProgressID
= RecordInfo
.ProgressID
;
597 FpdtRecordPtr
.DynamicStringEvent
->Timestamp
= TimeStamp
;
598 CopyMem (&FpdtRecordPtr
.DynamicStringEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.DynamicStringEvent
->Guid
));
601 StringPtr
= ModuleName
;
602 } else if (Token
!= NULL
) {
604 } else if (Module
!= NULL
) {
606 } else if (ModuleName
!= NULL
) {
607 StringPtr
= ModuleName
;
609 if (StringPtr
!= NULL
&& AsciiStrLen (StringPtr
) != 0) {
610 StrLength
= AsciiStrLen (StringPtr
);
611 DestMax
= (RecordInfo
.RecordSize
- sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD
)) / sizeof (CHAR8
);
612 if (StrLength
>= DestMax
) {
613 StrLength
= DestMax
-1;
615 AsciiStrnCpyS (FpdtRecordPtr
.DynamicStringEvent
->String
, DestMax
, StringPtr
, StrLength
);
617 AsciiStrCpyS (FpdtRecordPtr
.DynamicStringEvent
->String
, FPDT_STRING_EVENT_RECORD_NAME_LENGTH
, "unknown name");
621 case FPDT_GUID_QWORD_EVENT_TYPE
:
622 FpdtRecordPtr
.GuidQwordEvent
->Header
.Type
= FPDT_GUID_QWORD_EVENT_TYPE
;
623 FpdtRecordPtr
.GuidQwordEvent
->Header
.Length
= RecordInfo
.RecordSize
;
624 FpdtRecordPtr
.GuidQwordEvent
->Header
.Revision
= FPDT_RECORD_REVISION_1
;
625 FpdtRecordPtr
.GuidQwordEvent
->ProgressID
= RecordInfo
.ProgressID
;
626 FpdtRecordPtr
.GuidQwordEvent
->Timestamp
= TimeStamp
;
627 CopyMem (&FpdtRecordPtr
.GuidQwordEvent
->Guid
, &ModuleGuid
, sizeof (FpdtRecordPtr
.GuidQwordEvent
->Guid
));
632 // Record is not supported in current SMM phase, return EFI_UNSUPPORTED
634 return EFI_UNSUPPORTED
;
638 // Update the cached FPDT record buffer.
640 mPerformanceLength
+= FpdtRecordPtr
.RecordHeader
->Length
;
641 mSmmBootPerformanceTable
->Header
.Length
+= FpdtRecordPtr
.RecordHeader
->Length
;
647 Adds a record at the end of the performance measurement log
648 that records the start time of a performance measurement.
650 Adds a record to the end of the performance measurement log
651 that contains the Handle, Token, Module and Identifier.
652 The end time of the new record must be set to zero.
653 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
654 If TimeStamp is zero, the start time in the record is filled in with the value
655 read from the current time stamp.
657 @param Handle Pointer to environment specific context used
658 to identify the component being measured.
659 @param Token Pointer to a Null-terminated ASCII string
660 that identifies the component being measured.
661 @param Module Pointer to a Null-terminated ASCII string
662 that identifies the module being measured.
663 @param TimeStamp 64-bit time stamp.
664 @param Identifier 32-bit identifier. If the value is 0, the created record
665 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
667 @retval EFI_SUCCESS The data was read correctly from the device.
668 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
674 IN CONST VOID
*Handle
, OPTIONAL
675 IN CONST CHAR8
*Token
, OPTIONAL
676 IN CONST CHAR8
*Module
, OPTIONAL
683 AcquireSpinLock (&mSmmFpdtLock
);
685 Status
= InsertFpdtMeasurement (TRUE
, Handle
, Token
, Module
, TimeStamp
, Identifier
);
687 ReleaseSpinLock (&mSmmFpdtLock
);
693 Searches the performance measurement log from the beginning of the log
694 for the first matching record that contains a zero end time and fills in a valid end time.
696 Searches the performance measurement log from the beginning of the log
697 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
698 If the record can not be found then return EFI_NOT_FOUND.
699 If the record is found and TimeStamp is not zero,
700 then the end time in the record is filled in with the value specified by TimeStamp.
701 If the record is found and TimeStamp is zero, then the end time in the matching record
702 is filled in with the current time stamp value.
704 @param Handle Pointer to environment specific context used
705 to identify the component being measured.
706 @param Token Pointer to a Null-terminated ASCII string
707 that identifies the component being measured.
708 @param Module Pointer to a Null-terminated ASCII string
709 that identifies the module being measured.
710 @param TimeStamp 64-bit time stamp.
711 @param Identifier 32-bit identifier. If the value is 0, the found record
712 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
714 @retval EFI_SUCCESS The end of the measurement was recorded.
715 @retval EFI_NOT_FOUND The specified measurement record could not be found.
721 IN CONST VOID
*Handle
, OPTIONAL
722 IN CONST CHAR8
*Token
, OPTIONAL
723 IN CONST CHAR8
*Module
, OPTIONAL
730 AcquireSpinLock (&mSmmFpdtLock
);
732 Status
= InsertFpdtMeasurement (FALSE
, Handle
, Token
, Module
, TimeStamp
, Identifier
);
734 ReleaseSpinLock (&mSmmFpdtLock
);
740 Retrieves a previously logged performance measurement.
741 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
742 and then assign the Identifier with 0.
746 Retrieves the performance log entry from the performance log specified by LogEntryKey.
747 If it stands for a valid entry, then EFI_SUCCESS is returned and
748 GaugeDataEntryEx stores the pointer to that entry.
750 @param LogEntryKey The key for the previous performance measurement log entry.
751 If 0, then the first performance measurement log entry is retrieved.
752 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
753 if the retrieval is successful.
755 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
756 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
757 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
758 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
764 IN UINTN LogEntryKey
,
765 OUT GAUGE_DATA_ENTRY_EX
**GaugeDataEntryEx
768 return EFI_UNSUPPORTED
;
772 Adds a record at the end of the performance measurement log
773 that records the start time of a performance measurement.
775 Adds a record to the end of the performance measurement log
776 that contains the Handle, Token, and Module.
777 The end time of the new record must be set to zero.
778 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
779 If TimeStamp is zero, the start time in the record is filled in with the value
780 read from the current time stamp.
782 @param Handle Pointer to environment specific context used
783 to identify the component being measured.
784 @param Token Pointer to a Null-terminated ASCII string
785 that identifies the component being measured.
786 @param Module Pointer to a Null-terminated ASCII string
787 that identifies the module being measured.
788 @param TimeStamp 64-bit time stamp.
790 @retval EFI_SUCCESS The data was read correctly from the device.
791 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
797 IN CONST VOID
*Handle
, OPTIONAL
798 IN CONST CHAR8
*Token
, OPTIONAL
799 IN CONST CHAR8
*Module
, OPTIONAL
803 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
807 Searches the performance measurement log from the beginning of the log
808 for the first matching record that contains a zero end time and fills in a valid end time.
810 Searches the performance measurement log from the beginning of the log
811 for the first record that matches Handle, Token, and Module and has an end time value of zero.
812 If the record can not be found then return EFI_NOT_FOUND.
813 If the record is found and TimeStamp is not zero,
814 then the end time in the record is filled in with the value specified by TimeStamp.
815 If the record is found and TimeStamp is zero, then the end time in the matching record
816 is filled in with the current time stamp value.
818 @param Handle Pointer to environment specific context used
819 to identify the component being measured.
820 @param Token Pointer to a Null-terminated ASCII string
821 that identifies the component being measured.
822 @param Module Pointer to a Null-terminated ASCII string
823 that identifies the module being measured.
824 @param TimeStamp 64-bit time stamp.
826 @retval EFI_SUCCESS The end of the measurement was recorded.
827 @retval EFI_NOT_FOUND The specified measurement record could not be found.
833 IN CONST VOID
*Handle
, OPTIONAL
834 IN CONST CHAR8
*Token
, OPTIONAL
835 IN CONST CHAR8
*Module
, OPTIONAL
839 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
843 Retrieves a previously logged performance measurement.
844 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
845 and then eliminate the Identifier.
849 Retrieves the performance log entry from the performance log specified by LogEntryKey.
850 If it stands for a valid entry, then EFI_SUCCESS is returned and
851 GaugeDataEntry stores the pointer to that entry.
853 @param LogEntryKey The key for the previous performance measurement log entry.
854 If 0, then the first performance measurement log entry is retrieved.
855 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
856 if the retrieval is successful.
858 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
859 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
860 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
861 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
867 IN UINTN LogEntryKey
,
868 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
871 return EFI_UNSUPPORTED
;
876 SmmReadyToBoot protocol notification event handler.
878 @param Protocol Points to the protocol's unique identifier
879 @param Interface Points to the interface instance
880 @param Handle The handle on which the interface was installed
882 @retval EFI_SUCCESS SmmReadyToBootCallback runs successfully
887 SmmReportFpdtRecordData (
888 IN CONST EFI_GUID
*Protocol
,
895 if (!mFpdtDataIsReported
&& mSmmBootPerformanceTable
!= NULL
) {
896 SmmBPDTddr
= (UINT64
)(UINTN
)mSmmBootPerformanceTable
;
897 REPORT_STATUS_CODE_EX (
899 EFI_SOFTWARE_SMM_DRIVER
,
902 &gEdkiiFpdtExtendedFirmwarePerformanceGuid
,
907 // Set FPDT report state to TRUE.
909 mFpdtDataIsReported
= TRUE
;
915 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
916 this function is callbacked to initialize the Smm Performance Lib
918 @param Event The event of notify protocol.
919 @param Context Notify event context.
924 InitializeSmmCorePerformanceLib (
931 VOID
*SmmReadyToBootRegistration
;
932 PERFORMANCE_PROPERTY
*PerformanceProperty
;
935 // Initialize spin lock
937 InitializeSpinLock (&mSmmFpdtLock
);
940 // Install the protocol interfaces for SMM performance library instance.
943 Status
= gSmst
->SmmInstallProtocolInterface (
945 &gSmmPerformanceProtocolGuid
,
946 EFI_NATIVE_INTERFACE
,
947 &mPerformanceInterface
949 ASSERT_EFI_ERROR (Status
);
950 Status
= gSmst
->SmmInstallProtocolInterface (
952 &gSmmPerformanceExProtocolGuid
,
953 EFI_NATIVE_INTERFACE
,
954 &mPerformanceExInterface
956 ASSERT_EFI_ERROR (Status
);
958 Status
= gSmst
->SmmRegisterProtocolNotify (
959 &gEdkiiSmmReadyToBootProtocolGuid
,
960 SmmReportFpdtRecordData
,
961 &SmmReadyToBootRegistration
963 Status
= EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid
, (VOID
**) &PerformanceProperty
);
964 if (EFI_ERROR (Status
)) {
966 // Install configuration table for performance property.
968 mPerformanceProperty
.Revision
= PERFORMANCE_PROPERTY_REVISION
;
969 mPerformanceProperty
.Reserved
= 0;
970 mPerformanceProperty
.Frequency
= GetPerformanceCounterProperties (
971 &mPerformanceProperty
.TimerStartValue
,
972 &mPerformanceProperty
.TimerEndValue
974 Status
= gBS
->InstallConfigurationTable (&gPerformanceProtocolGuid
, &mPerformanceProperty
);
975 ASSERT_EFI_ERROR (Status
);
980 The constructor function initializes the Performance Measurement Enable flag and
981 registers SmmBase2 protocol notify callback.
982 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
984 @param ImageHandle The firmware allocated handle for the EFI image.
985 @param SystemTable A pointer to the EFI System Table.
987 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
992 SmmCorePerformanceLibConstructor (
993 IN EFI_HANDLE ImageHandle
,
994 IN EFI_SYSTEM_TABLE
*SystemTable
1001 if (!PerformanceMeasurementEnabled ()) {
1003 // Do not initialize performance infrastructure if not required.
1009 // Create the events to do the library init.
1011 Status
= gBS
->CreateEvent (
1014 InitializeSmmCorePerformanceLib
,
1018 ASSERT_EFI_ERROR (Status
);
1021 // Register for protocol notifications on this event
1023 Status
= gBS
->RegisterProtocolNotify (
1024 &gEfiSmmBase2ProtocolGuid
,
1029 ASSERT_EFI_ERROR (Status
);
1035 Adds a record at the end of the performance measurement log
1036 that records the start time of a performance measurement.
1038 Adds a record to the end of the performance measurement log
1039 that contains the Handle, Token, Module and Identifier.
1040 The end time of the new record must be set to zero.
1041 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1042 If TimeStamp is zero, the start time in the record is filled in with the value
1043 read from the current time stamp.
1045 @param Handle Pointer to environment specific context used
1046 to identify the component being measured.
1047 @param Token Pointer to a Null-terminated ASCII string
1048 that identifies the component being measured.
1049 @param Module Pointer to a Null-terminated ASCII string
1050 that identifies the module being measured.
1051 @param TimeStamp 64-bit time stamp.
1052 @param Identifier 32-bit identifier. If the value is 0, the created record
1053 is same as the one created by StartPerformanceMeasurement.
1055 @retval RETURN_SUCCESS The start of the measurement was recorded.
1056 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1061 StartPerformanceMeasurementEx (
1062 IN CONST VOID
*Handle
, OPTIONAL
1063 IN CONST CHAR8
*Token
, OPTIONAL
1064 IN CONST CHAR8
*Module
, OPTIONAL
1065 IN UINT64 TimeStamp
,
1066 IN UINT32 Identifier
1069 return (RETURN_STATUS
) StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
1073 Searches the performance measurement log from the beginning of the log
1074 for the first matching record that contains a zero end time and fills in a valid end time.
1076 Searches the performance measurement log from the beginning of the log
1077 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
1078 If the record can not be found then return RETURN_NOT_FOUND.
1079 If the record is found and TimeStamp is not zero,
1080 then the end time in the record is filled in with the value specified by TimeStamp.
1081 If the record is found and TimeStamp is zero, then the end time in the matching record
1082 is filled in with the current time stamp value.
1084 @param Handle Pointer to environment specific context used
1085 to identify the component being measured.
1086 @param Token Pointer to a Null-terminated ASCII string
1087 that identifies the component being measured.
1088 @param Module Pointer to a Null-terminated ASCII string
1089 that identifies the module being measured.
1090 @param TimeStamp 64-bit time stamp.
1091 @param Identifier 32-bit identifier. If the value is 0, the found record
1092 is same as the one found by EndPerformanceMeasurement.
1094 @retval RETURN_SUCCESS The end of the measurement was recorded.
1095 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1100 EndPerformanceMeasurementEx (
1101 IN CONST VOID
*Handle
, OPTIONAL
1102 IN CONST CHAR8
*Token
, OPTIONAL
1103 IN CONST CHAR8
*Module
, OPTIONAL
1104 IN UINT64 TimeStamp
,
1105 IN UINT32 Identifier
1108 return (RETURN_STATUS
) EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
1112 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1113 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
1114 and then assign the Identifier with 0.
1118 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1119 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1120 and the key for the second entry in the log is returned. If the performance log is empty,
1121 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1122 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1123 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1124 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1125 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1126 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1127 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
1128 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1129 If Handle is NULL, then ASSERT().
1130 If Token is NULL, then ASSERT().
1131 If Module is NULL, then ASSERT().
1132 If StartTimeStamp is NULL, then ASSERT().
1133 If EndTimeStamp is NULL, then ASSERT().
1134 If Identifier is NULL, then ASSERT().
1136 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1137 0, then the first performance measurement log entry is retrieved.
1138 On exit, the key of the next performance log entry.
1139 @param Handle Pointer to environment specific context used to identify the component
1141 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1143 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1145 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1147 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1149 @param Identifier Pointer to the 32-bit identifier that was recorded.
1151 @return The key for the next performance log entry (in general case).
1156 GetPerformanceMeasurementEx (
1157 IN UINTN LogEntryKey
,
1158 OUT CONST VOID
**Handle
,
1159 OUT CONST CHAR8
**Token
,
1160 OUT CONST CHAR8
**Module
,
1161 OUT UINT64
*StartTimeStamp
,
1162 OUT UINT64
*EndTimeStamp
,
1163 OUT UINT32
*Identifier
1170 Adds a record at the end of the performance measurement log
1171 that records the start time of a performance measurement.
1173 Adds a record to the end of the performance measurement log
1174 that contains the Handle, Token, and Module.
1175 The end time of the new record must be set to zero.
1176 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1177 If TimeStamp is zero, the start time in the record is filled in with the value
1178 read from the current time stamp.
1180 @param Handle Pointer to environment specific context used
1181 to identify the component being measured.
1182 @param Token Pointer to a Null-terminated ASCII string
1183 that identifies the component being measured.
1184 @param Module Pointer to a Null-terminated ASCII string
1185 that identifies the module being measured.
1186 @param TimeStamp 64-bit time stamp.
1188 @retval RETURN_SUCCESS The start of the measurement was recorded.
1189 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1194 StartPerformanceMeasurement (
1195 IN CONST VOID
*Handle
, OPTIONAL
1196 IN CONST CHAR8
*Token
, OPTIONAL
1197 IN CONST CHAR8
*Module
, OPTIONAL
1201 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
1205 Searches the performance measurement log from the beginning of the log
1206 for the first matching record that contains a zero end time and fills in a valid end time.
1208 Searches the performance measurement log from the beginning of the log
1209 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1210 If the record can not be found then return RETURN_NOT_FOUND.
1211 If the record is found and TimeStamp is not zero,
1212 then the end time in the record is filled in with the value specified by TimeStamp.
1213 If the record is found and TimeStamp is zero, then the end time in the matching record
1214 is filled in with the current time stamp value.
1216 @param Handle Pointer to environment specific context used
1217 to identify the component being measured.
1218 @param Token Pointer to a Null-terminated ASCII string
1219 that identifies the component being measured.
1220 @param Module Pointer to a Null-terminated ASCII string
1221 that identifies the module being measured.
1222 @param TimeStamp 64-bit time stamp.
1224 @retval RETURN_SUCCESS The end of the measurement was recorded.
1225 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1230 EndPerformanceMeasurement (
1231 IN CONST VOID
*Handle
, OPTIONAL
1232 IN CONST CHAR8
*Token
, OPTIONAL
1233 IN CONST CHAR8
*Module
, OPTIONAL
1237 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
1241 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1242 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1243 and then eliminate the Identifier.
1247 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1248 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1249 and the key for the second entry in the log is returned. If the performance log is empty,
1250 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1251 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1252 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1253 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1254 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1255 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1256 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1257 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1258 If Handle is NULL, then ASSERT().
1259 If Token is NULL, then ASSERT().
1260 If Module is NULL, then ASSERT().
1261 If StartTimeStamp is NULL, then ASSERT().
1262 If EndTimeStamp is NULL, then ASSERT().
1264 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1265 0, then the first performance measurement log entry is retrieved.
1266 On exit, the key of the next performance log entry.
1267 @param Handle Pointer to environment specific context used to identify the component
1269 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1271 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1273 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1275 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1278 @return The key for the next performance log entry (in general case).
1283 GetPerformanceMeasurement (
1284 IN UINTN LogEntryKey
,
1285 OUT CONST VOID
**Handle
,
1286 OUT CONST CHAR8
**Token
,
1287 OUT CONST CHAR8
**Module
,
1288 OUT UINT64
*StartTimeStamp
,
1289 OUT UINT64
*EndTimeStamp
1296 Returns TRUE if the performance measurement macros are enabled.
1298 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1299 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1301 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1302 PcdPerformanceLibraryPropertyMask is set.
1303 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1304 PcdPerformanceLibraryPropertyMask is clear.
1309 PerformanceMeasurementEnabled (
1313 return (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);