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 - 2015, 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"
34 // The data structure to hold global performance data.
36 GAUGE_DATA_HEADER
*mGaugeData
;
39 // The current maximum number of logging entries. If current number of
40 // entries exceeds this value, it will re-allocate a larger array and
41 // migration the old data to the larger array.
43 UINT32 mMaxGaugeRecords
;
46 // The handle to install Performance Protocol instance.
48 EFI_HANDLE mHandle
= NULL
;
50 BOOLEAN mPerformanceMeasurementEnabled
;
52 SPIN_LOCK mSmmPerfLock
;
55 // Interfaces for SMM Performance Protocol.
57 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
64 // Interfaces for SMM PerformanceEx Protocol.
66 PERFORMANCE_EX_PROTOCOL mPerformanceExInterface
= {
73 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
75 This internal function searches for the gauge entry in the gauge array.
76 If there is an entry that exactly matches the given keywords
77 and its end time stamp is zero, then the index of that gauge entry is returned;
78 otherwise, the the number of gauge entries in the array is returned.
80 @param Handle Pointer to environment specific context used
81 to identify the component being measured.
82 @param Token Pointer to a Null-terminated ASCII string
83 that identifies the component being measured.
84 @param Module Pointer to a Null-terminated ASCII string
85 that identifies the module being measured.
86 @param Identifier 32-bit identifier.
88 @retval The index of gauge entry in the array.
92 SmmSearchForGaugeEntry (
93 IN CONST VOID
*Handle
, OPTIONAL
94 IN CONST CHAR8
*Token
, OPTIONAL
95 IN CONST CHAR8
*Module
, OPTIONAL
96 IN CONST UINT32 Identifier
101 UINT32 NumberOfEntries
;
102 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
107 if (Module
== NULL
) {
111 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
112 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
116 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
117 Index2
= NumberOfEntries
- 1 - Index
;
118 if (GaugeEntryExArray
[Index2
].EndTimeStamp
== 0 &&
119 (GaugeEntryExArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
120 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Token
, Token
, SMM_PERFORMANCE_STRING_LENGTH
) == 0 &&
121 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Module
, Module
, SMM_PERFORMANCE_STRING_LENGTH
) == 0 &&
122 (GaugeEntryExArray
[Index2
].Identifier
== Identifier
)) {
132 Adds a record at the end of the performance measurement log
133 that records the start time of a performance measurement.
135 Adds a record to the end of the performance measurement log
136 that contains the Handle, Token, Module and Identifier.
137 The end time of the new record must be set to zero.
138 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
139 If TimeStamp is zero, the start time in the record is filled in with the value
140 read from the current time stamp.
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 TimeStamp 64-bit time stamp.
149 @param Identifier 32-bit identifier. If the value is 0, the created record
150 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
152 @retval EFI_SUCCESS The data was read correctly from the device.
153 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
159 IN CONST VOID
*Handle
, OPTIONAL
160 IN CONST CHAR8
*Token
, OPTIONAL
161 IN CONST CHAR8
*Module
, OPTIONAL
166 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
168 GAUGE_DATA_HEADER
*NewGaugeData
;
169 UINTN OldGaugeDataSize
;
170 GAUGE_DATA_HEADER
*OldGaugeData
;
173 AcquireSpinLock (&mSmmPerfLock
);
175 Index
= mGaugeData
->NumberOfEntries
;
176 if (Index
>= mMaxGaugeRecords
) {
178 // Try to enlarge the scale of gauge array.
180 OldGaugeData
= mGaugeData
;
181 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
;
183 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
* 2;
185 NewGaugeData
= AllocateZeroPool (GaugeDataSize
);
186 if (NewGaugeData
== NULL
) {
187 ReleaseSpinLock (&mSmmPerfLock
);
188 return EFI_OUT_OF_RESOURCES
;
191 mGaugeData
= NewGaugeData
;
192 mMaxGaugeRecords
*= 2;
195 // Initialize new data array and migrate old data one.
197 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
199 FreePool (OldGaugeData
);
202 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
203 GaugeEntryExArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
206 AsciiStrCpyS (GaugeEntryExArray
[Index
].Token
, SMM_PERFORMANCE_STRING_SIZE
, Token
);
208 if (Module
!= NULL
) {
209 AsciiStrCpyS (GaugeEntryExArray
[Index
].Module
, SMM_PERFORMANCE_STRING_SIZE
, Module
);
212 GaugeEntryExArray
[Index
].EndTimeStamp
= 0;
213 GaugeEntryExArray
[Index
].Identifier
= Identifier
;
215 if (TimeStamp
== 0) {
216 TimeStamp
= GetPerformanceCounter ();
218 GaugeEntryExArray
[Index
].StartTimeStamp
= TimeStamp
;
220 mGaugeData
->NumberOfEntries
++;
222 ReleaseSpinLock (&mSmmPerfLock
);
228 Searches the performance measurement log from the beginning of the log
229 for the first matching record that contains a zero end time and fills in a valid end time.
231 Searches the performance measurement log from the beginning of the log
232 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
233 If the record can not be found then return EFI_NOT_FOUND.
234 If the record is found and TimeStamp is not zero,
235 then the end time in the record is filled in with the value specified by TimeStamp.
236 If the record is found and TimeStamp is zero, then the end time in the matching record
237 is filled in with the current time stamp value.
239 @param Handle Pointer to environment specific context used
240 to identify the component being measured.
241 @param Token Pointer to a Null-terminated ASCII string
242 that identifies the component being measured.
243 @param Module Pointer to a Null-terminated ASCII string
244 that identifies the module being measured.
245 @param TimeStamp 64-bit time stamp.
246 @param Identifier 32-bit identifier. If the value is 0, the found record
247 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
249 @retval EFI_SUCCESS The end of the measurement was recorded.
250 @retval EFI_NOT_FOUND The specified measurement record could not be found.
256 IN CONST VOID
*Handle
, OPTIONAL
257 IN CONST CHAR8
*Token
, OPTIONAL
258 IN CONST CHAR8
*Module
, OPTIONAL
263 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
266 AcquireSpinLock (&mSmmPerfLock
);
268 if (TimeStamp
== 0) {
269 TimeStamp
= GetPerformanceCounter ();
272 Index
= SmmSearchForGaugeEntry (Handle
, Token
, Module
, Identifier
);
273 if (Index
>= mGaugeData
->NumberOfEntries
) {
274 ReleaseSpinLock (&mSmmPerfLock
);
275 return EFI_NOT_FOUND
;
277 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
278 GaugeEntryExArray
[Index
].EndTimeStamp
= TimeStamp
;
280 ReleaseSpinLock (&mSmmPerfLock
);
286 Retrieves a previously logged performance measurement.
287 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
288 and then assign the Identifier with 0.
290 Retrieves the performance log entry from the performance log specified by LogEntryKey.
291 If it stands for a valid entry, then EFI_SUCCESS is returned and
292 GaugeDataEntryEx stores the pointer to that entry.
294 @param LogEntryKey The key for the previous performance measurement log entry.
295 If 0, then the first performance measurement log entry is retrieved.
296 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
297 if the retrieval is successful.
299 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
300 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
301 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
302 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
308 IN UINTN LogEntryKey
,
309 OUT GAUGE_DATA_ENTRY_EX
**GaugeDataEntryEx
312 UINTN NumberOfEntries
;
313 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
315 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
316 if (LogEntryKey
> NumberOfEntries
) {
317 return EFI_INVALID_PARAMETER
;
319 if (LogEntryKey
== NumberOfEntries
) {
320 return EFI_NOT_FOUND
;
323 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
325 if (GaugeDataEntryEx
== NULL
) {
326 return EFI_INVALID_PARAMETER
;
328 *GaugeDataEntryEx
= &GaugeEntryExArray
[LogEntryKey
];
334 Adds a record at the end of the performance measurement log
335 that records the start time of a performance measurement.
337 Adds a record to the end of the performance measurement log
338 that contains the Handle, Token, and Module.
339 The end time of the new record must be set to zero.
340 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
341 If TimeStamp is zero, the start time in the record is filled in with the value
342 read from the current time stamp.
344 @param Handle Pointer to environment specific context used
345 to identify the component being measured.
346 @param Token Pointer to a Null-terminated ASCII string
347 that identifies the component being measured.
348 @param Module Pointer to a Null-terminated ASCII string
349 that identifies the module being measured.
350 @param TimeStamp 64-bit time stamp.
352 @retval EFI_SUCCESS The data was read correctly from the device.
353 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
359 IN CONST VOID
*Handle
, OPTIONAL
360 IN CONST CHAR8
*Token
, OPTIONAL
361 IN CONST CHAR8
*Module
, OPTIONAL
365 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
369 Searches the performance measurement log from the beginning of the log
370 for the first matching record that contains a zero end time and fills in a valid end time.
372 Searches the performance measurement log from the beginning of the log
373 for the first record that matches Handle, Token, and Module and has an end time value of zero.
374 If the record can not be found then return EFI_NOT_FOUND.
375 If the record is found and TimeStamp is not zero,
376 then the end time in the record is filled in with the value specified by TimeStamp.
377 If the record is found and TimeStamp is zero, then the end time in the matching record
378 is filled in with the current time stamp value.
380 @param Handle Pointer to environment specific context used
381 to identify the component being measured.
382 @param Token Pointer to a Null-terminated ASCII string
383 that identifies the component being measured.
384 @param Module Pointer to a Null-terminated ASCII string
385 that identifies the module being measured.
386 @param TimeStamp 64-bit time stamp.
388 @retval EFI_SUCCESS The end of the measurement was recorded.
389 @retval EFI_NOT_FOUND The specified measurement record could not be found.
395 IN CONST VOID
*Handle
, OPTIONAL
396 IN CONST CHAR8
*Token
, OPTIONAL
397 IN CONST CHAR8
*Module
, OPTIONAL
401 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
405 Retrieves a previously logged performance measurement.
406 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
407 and then eliminate the Identifier.
409 Retrieves the performance log entry from the performance log specified by LogEntryKey.
410 If it stands for a valid entry, then EFI_SUCCESS is returned and
411 GaugeDataEntry stores the pointer to that entry.
413 @param LogEntryKey The key for the previous performance measurement log entry.
414 If 0, then the first performance measurement log entry is retrieved.
415 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
416 if the retrieval is successful.
418 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
419 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
420 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
421 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
427 IN UINTN LogEntryKey
,
428 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
432 GAUGE_DATA_ENTRY_EX
*GaugeEntryEx
;
436 Status
= GetGaugeEx (LogEntryKey
, &GaugeEntryEx
);
437 if (EFI_ERROR (Status
)) {
441 if (GaugeDataEntry
== NULL
) {
442 return EFI_INVALID_PARAMETER
;
445 *GaugeDataEntry
= (GAUGE_DATA_ENTRY
*) GaugeEntryEx
;
451 Communication service SMI Handler entry.
453 This SMI handler provides services for the performance wrapper driver.
455 Caution: This function may receive untrusted input.
456 Communicate buffer and buffer size are external input, so this function will do basic validation.
458 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
459 @param[in] RegisterContext Points to an optional handler context which was specified when the
460 handler was registered.
461 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
462 be conveyed from a non-SMM environment into an SMM environment.
463 @param[in, out] CommBufferSize The size of the CommBuffer.
465 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
466 should still be called.
467 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
469 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
471 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
475 SmmPerformanceHandlerEx (
476 IN EFI_HANDLE DispatchHandle
,
477 IN CONST VOID
*RegisterContext
,
478 IN OUT VOID
*CommBuffer
,
479 IN OUT UINTN
*CommBufferSize
483 SMM_PERF_COMMUNICATE_EX
*SmmPerfCommData
;
484 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
486 GAUGE_DATA_ENTRY_EX
*GaugeDataEx
;
487 UINTN NumberOfEntries
;
489 UINTN TempCommBufferSize
;
491 GaugeEntryExArray
= NULL
;
494 // If input is invalid, stop processing this SMI
496 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
500 TempCommBufferSize
= *CommBufferSize
;
502 if(TempCommBufferSize
< sizeof (SMM_PERF_COMMUNICATE_EX
)) {
506 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
507 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
511 SmmPerfCommData
= (SMM_PERF_COMMUNICATE_EX
*)CommBuffer
;
513 switch (SmmPerfCommData
->Function
) {
514 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
515 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
516 Status
= EFI_SUCCESS
;
519 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
520 GaugeDataEx
= SmmPerfCommData
->GaugeDataEx
;
521 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
522 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
523 if (GaugeDataEx
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
524 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| (LogEntryKey
+ NumberOfEntries
) > mGaugeData
->NumberOfEntries
) {
525 Status
= EFI_INVALID_PARAMETER
;
532 DataSize
= NumberOfEntries
* sizeof(GAUGE_DATA_ENTRY_EX
);
533 if (!SmmIsBufferOutsideSmmValid ((UINTN
)GaugeDataEx
, DataSize
)) {
534 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n"));
535 Status
= EFI_ACCESS_DENIED
;
539 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
541 (UINT8
*) GaugeDataEx
,
542 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
],
545 Status
= EFI_SUCCESS
;
549 Status
= EFI_UNSUPPORTED
;
553 SmmPerfCommData
->ReturnStatus
= Status
;
559 Communication service SMI Handler entry.
561 This SMI handler provides services for the performance wrapper driver.
563 Caution: This function may receive untrusted input.
564 Communicate buffer and buffer size are external input, so this function will do basic validation.
566 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
567 @param[in] RegisterContext Points to an optional handler context which was specified when the
568 handler was registered.
569 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
570 be conveyed from a non-SMM environment into an SMM environment.
571 @param[in, out] CommBufferSize The size of the CommBuffer.
573 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
574 should still be called.
575 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
577 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
579 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
583 SmmPerformanceHandler (
584 IN EFI_HANDLE DispatchHandle
,
585 IN CONST VOID
*RegisterContext
,
586 IN OUT VOID
*CommBuffer
,
587 IN OUT UINTN
*CommBufferSize
591 SMM_PERF_COMMUNICATE
*SmmPerfCommData
;
592 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
595 GAUGE_DATA_ENTRY
*GaugeData
;
596 UINTN NumberOfEntries
;
598 UINTN TempCommBufferSize
;
600 GaugeEntryExArray
= NULL
;
603 // If input is invalid, stop processing this SMI
605 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
609 TempCommBufferSize
= *CommBufferSize
;
611 if(TempCommBufferSize
< sizeof (SMM_PERF_COMMUNICATE
)) {
615 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
616 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
620 SmmPerfCommData
= (SMM_PERF_COMMUNICATE
*)CommBuffer
;
622 switch (SmmPerfCommData
->Function
) {
623 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
624 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
625 Status
= EFI_SUCCESS
;
628 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
629 GaugeData
= SmmPerfCommData
->GaugeData
;
630 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
631 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
632 if (GaugeData
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
633 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| (LogEntryKey
+ NumberOfEntries
) > mGaugeData
->NumberOfEntries
) {
634 Status
= EFI_INVALID_PARAMETER
;
641 DataSize
= NumberOfEntries
* sizeof(GAUGE_DATA_ENTRY
);
642 if (!SmmIsBufferOutsideSmmValid ((UINTN
)GaugeData
, DataSize
)) {
643 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n"));
644 Status
= EFI_ACCESS_DENIED
;
648 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
650 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
652 (UINT8
*) &GaugeData
[Index
],
653 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
++],
654 sizeof (GAUGE_DATA_ENTRY
)
657 Status
= EFI_SUCCESS
;
661 Status
= EFI_UNSUPPORTED
;
665 SmmPerfCommData
->ReturnStatus
= Status
;
671 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
672 this function is callbacked to initialize the Smm Performance Lib
674 @param Event The event of notify protocol.
675 @param Context Notify event context.
680 InitializeSmmCorePerformanceLib (
689 // Initialize spin lock
691 InitializeSpinLock (&mSmmPerfLock
);
693 mMaxGaugeRecords
= INIT_SMM_GAUGE_DATA_ENTRIES
;
695 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
));
696 ASSERT (mGaugeData
!= NULL
);
699 // Install the protocol interfaces.
701 Status
= gSmst
->SmmInstallProtocolInterface (
703 &gSmmPerformanceProtocolGuid
,
704 EFI_NATIVE_INTERFACE
,
705 &mPerformanceInterface
707 ASSERT_EFI_ERROR (Status
);
709 Status
= gSmst
->SmmInstallProtocolInterface (
711 &gSmmPerformanceExProtocolGuid
,
712 EFI_NATIVE_INTERFACE
,
713 &mPerformanceExInterface
715 ASSERT_EFI_ERROR (Status
);
718 /// Register SMM Performance SMI handler
721 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandler
, &gSmmPerformanceProtocolGuid
, &Handle
);
722 ASSERT_EFI_ERROR (Status
);
723 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandlerEx
, &gSmmPerformanceExProtocolGuid
, &Handle
);
724 ASSERT_EFI_ERROR (Status
);
728 The constructor function initializes the Performance Measurement Enable flag and
729 registers SmmBase2 protocol notify callback.
730 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
732 @param ImageHandle The firmware allocated handle for the EFI image.
733 @param SystemTable A pointer to the EFI System Table.
735 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
740 SmmCorePerformanceLibConstructor (
741 IN EFI_HANDLE ImageHandle
,
742 IN EFI_SYSTEM_TABLE
*SystemTable
749 mPerformanceMeasurementEnabled
= (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);
750 if (!mPerformanceMeasurementEnabled
) {
752 // Do not initialize performance infrastructure if not required.
758 // Create the events to do the library init.
760 Status
= gBS
->CreateEvent (
763 InitializeSmmCorePerformanceLib
,
767 ASSERT_EFI_ERROR (Status
);
770 // Register for protocol notifications on this event
772 Status
= gBS
->RegisterProtocolNotify (
773 &gEfiSmmBase2ProtocolGuid
,
778 ASSERT_EFI_ERROR (Status
);
784 Adds a record at the end of the performance measurement log
785 that records the start time of a performance measurement.
787 Adds a record to the end of the performance measurement log
788 that contains the Handle, Token, Module and Identifier.
789 The end time of the new record must be set to zero.
790 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
791 If TimeStamp is zero, the start time in the record is filled in with the value
792 read from the current time stamp.
794 @param Handle Pointer to environment specific context used
795 to identify the component being measured.
796 @param Token Pointer to a Null-terminated ASCII string
797 that identifies the component being measured.
798 @param Module Pointer to a Null-terminated ASCII string
799 that identifies the module being measured.
800 @param TimeStamp 64-bit time stamp.
801 @param Identifier 32-bit identifier. If the value is 0, the created record
802 is same as the one created by StartPerformanceMeasurement.
804 @retval RETURN_SUCCESS The start of the measurement was recorded.
805 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
810 StartPerformanceMeasurementEx (
811 IN CONST VOID
*Handle
, OPTIONAL
812 IN CONST CHAR8
*Token
, OPTIONAL
813 IN CONST CHAR8
*Module
, OPTIONAL
818 return (RETURN_STATUS
) StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
822 Searches the performance measurement log from the beginning of the log
823 for the first matching record that contains a zero end time and fills in a valid end time.
825 Searches the performance measurement log from the beginning of the log
826 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
827 If the record can not be found then return RETURN_NOT_FOUND.
828 If the record is found and TimeStamp is not zero,
829 then the end time in the record is filled in with the value specified by TimeStamp.
830 If the record is found and TimeStamp is zero, then the end time in the matching record
831 is filled in with the current time stamp value.
833 @param Handle Pointer to environment specific context used
834 to identify the component being measured.
835 @param Token Pointer to a Null-terminated ASCII string
836 that identifies the component being measured.
837 @param Module Pointer to a Null-terminated ASCII string
838 that identifies the module being measured.
839 @param TimeStamp 64-bit time stamp.
840 @param Identifier 32-bit identifier. If the value is 0, the found record
841 is same as the one found by EndPerformanceMeasurement.
843 @retval RETURN_SUCCESS The end of the measurement was recorded.
844 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
849 EndPerformanceMeasurementEx (
850 IN CONST VOID
*Handle
, OPTIONAL
851 IN CONST CHAR8
*Token
, OPTIONAL
852 IN CONST CHAR8
*Module
, OPTIONAL
857 return (RETURN_STATUS
) EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
861 Attempts to retrieve a performance measurement log entry from the performance measurement log.
862 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
863 and then assign the Identifier with 0.
865 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
866 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
867 and the key for the second entry in the log is returned. If the performance log is empty,
868 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
869 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
870 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
871 retrieved and an implementation specific non-zero key value that specifies the end of the performance
872 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
873 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
874 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
875 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
876 If Handle is NULL, then ASSERT().
877 If Token is NULL, then ASSERT().
878 If Module is NULL, then ASSERT().
879 If StartTimeStamp is NULL, then ASSERT().
880 If EndTimeStamp is NULL, then ASSERT().
881 If Identifier is NULL, then ASSERT().
883 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
884 0, then the first performance measurement log entry is retrieved.
885 On exit, the key of the next performance log entry.
886 @param Handle Pointer to environment specific context used to identify the component
888 @param Token Pointer to a Null-terminated ASCII string that identifies the component
890 @param Module Pointer to a Null-terminated ASCII string that identifies the module
892 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
894 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
896 @param Identifier Pointer to the 32-bit identifier that was recorded.
898 @return The key for the next performance log entry (in general case).
903 GetPerformanceMeasurementEx (
904 IN UINTN LogEntryKey
,
905 OUT CONST VOID
**Handle
,
906 OUT CONST CHAR8
**Token
,
907 OUT CONST CHAR8
**Module
,
908 OUT UINT64
*StartTimeStamp
,
909 OUT UINT64
*EndTimeStamp
,
910 OUT UINT32
*Identifier
914 GAUGE_DATA_ENTRY_EX
*GaugeData
;
918 ASSERT (Handle
!= NULL
);
919 ASSERT (Token
!= NULL
);
920 ASSERT (Module
!= NULL
);
921 ASSERT (StartTimeStamp
!= NULL
);
922 ASSERT (EndTimeStamp
!= NULL
);
923 ASSERT (Identifier
!= NULL
);
925 Status
= GetGaugeEx (LogEntryKey
++, &GaugeData
);
928 // Make sure that LogEntryKey is a valid log entry key,
930 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
932 if (EFI_ERROR (Status
)) {
934 // The LogEntryKey is the last entry (equals to the total entry number).
939 ASSERT (GaugeData
!= NULL
);
941 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
942 *Token
= GaugeData
->Token
;
943 *Module
= GaugeData
->Module
;
944 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
945 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
946 *Identifier
= GaugeData
->Identifier
;
952 Adds a record at the end of the performance measurement log
953 that records the start time of a performance measurement.
955 Adds a record to the end of the performance measurement log
956 that contains the Handle, Token, and Module.
957 The end time of the new record must be set to zero.
958 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
959 If TimeStamp is zero, the start time in the record is filled in with the value
960 read from the current time stamp.
962 @param Handle Pointer to environment specific context used
963 to identify the component being measured.
964 @param Token Pointer to a Null-terminated ASCII string
965 that identifies the component being measured.
966 @param Module Pointer to a Null-terminated ASCII string
967 that identifies the module being measured.
968 @param TimeStamp 64-bit time stamp.
970 @retval RETURN_SUCCESS The start of the measurement was recorded.
971 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
976 StartPerformanceMeasurement (
977 IN CONST VOID
*Handle
, OPTIONAL
978 IN CONST CHAR8
*Token
, OPTIONAL
979 IN CONST CHAR8
*Module
, OPTIONAL
983 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
987 Searches the performance measurement log from the beginning of the log
988 for the first matching record that contains a zero end time and fills in a valid end time.
990 Searches the performance measurement log from the beginning of the log
991 for the first record that matches Handle, Token, and Module and has an end time value of zero.
992 If the record can not be found then return RETURN_NOT_FOUND.
993 If the record is found and TimeStamp is not zero,
994 then the end time in the record is filled in with the value specified by TimeStamp.
995 If the record is found and TimeStamp is zero, then the end time in the matching record
996 is filled in with the current time stamp value.
998 @param Handle Pointer to environment specific context used
999 to identify the component being measured.
1000 @param Token Pointer to a Null-terminated ASCII string
1001 that identifies the component being measured.
1002 @param Module Pointer to a Null-terminated ASCII string
1003 that identifies the module being measured.
1004 @param TimeStamp 64-bit time stamp.
1006 @retval RETURN_SUCCESS The end of the measurement was recorded.
1007 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1012 EndPerformanceMeasurement (
1013 IN CONST VOID
*Handle
, OPTIONAL
1014 IN CONST CHAR8
*Token
, OPTIONAL
1015 IN CONST CHAR8
*Module
, OPTIONAL
1019 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
1023 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1024 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1025 and then eliminate the Identifier.
1027 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1028 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1029 and the key for the second entry in the log is returned. If the performance log is empty,
1030 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1031 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1032 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1033 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1034 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1035 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1036 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1037 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1038 If Handle is NULL, then ASSERT().
1039 If Token is NULL, then ASSERT().
1040 If Module is NULL, then ASSERT().
1041 If StartTimeStamp is NULL, then ASSERT().
1042 If EndTimeStamp is NULL, then ASSERT().
1044 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1045 0, then the first performance measurement log entry is retrieved.
1046 On exit, the key of the next performance log entry.
1047 @param Handle Pointer to environment specific context used to identify the component
1049 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1051 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1053 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1055 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1058 @return The key for the next performance log entry (in general case).
1063 GetPerformanceMeasurement (
1064 IN UINTN LogEntryKey
,
1065 OUT CONST VOID
**Handle
,
1066 OUT CONST CHAR8
**Token
,
1067 OUT CONST CHAR8
**Module
,
1068 OUT UINT64
*StartTimeStamp
,
1069 OUT UINT64
*EndTimeStamp
1073 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
1077 Returns TRUE if the performance measurement macros are enabled.
1079 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1080 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1082 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1083 PcdPerformanceLibraryPropertyMask is set.
1084 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1085 PcdPerformanceLibraryPropertyMask is clear.
1090 PerformanceMeasurementEnabled (
1094 return mPerformanceMeasurementEnabled
;