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 - 2017, 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
= {
72 PERFORMANCE_PROPERTY mPerformanceProperty
;
75 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
77 This internal function searches for the gauge entry in the gauge array.
78 If there is an entry that exactly matches the given keywords
79 and its end time stamp is zero, then the index of that gauge entry is returned;
80 otherwise, the the number of gauge entries in the array is returned.
82 @param Handle Pointer to environment specific context used
83 to identify the component being measured.
84 @param Token Pointer to a Null-terminated ASCII string
85 that identifies the component being measured.
86 @param Module Pointer to a Null-terminated ASCII string
87 that identifies the module being measured.
88 @param Identifier 32-bit identifier.
90 @retval The index of gauge entry in the array.
94 SmmSearchForGaugeEntry (
95 IN CONST VOID
*Handle
, OPTIONAL
96 IN CONST CHAR8
*Token
, OPTIONAL
97 IN CONST CHAR8
*Module
, OPTIONAL
98 IN CONST UINT32 Identifier
103 UINT32 NumberOfEntries
;
104 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
109 if (Module
== NULL
) {
113 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
114 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
118 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
119 Index2
= NumberOfEntries
- 1 - Index
;
120 if (GaugeEntryExArray
[Index2
].EndTimeStamp
== 0 &&
121 (GaugeEntryExArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
122 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Token
, Token
, SMM_PERFORMANCE_STRING_LENGTH
) == 0 &&
123 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Module
, Module
, SMM_PERFORMANCE_STRING_LENGTH
) == 0) {
133 Adds a record at the end of the performance measurement log
134 that records the start time of a performance measurement.
136 Adds a record to the end of the performance measurement log
137 that contains the Handle, Token, Module and Identifier.
138 The end time of the new record must be set to zero.
139 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
140 If TimeStamp is zero, the start time in the record is filled in with the value
141 read from the current time stamp.
143 @param Handle Pointer to environment specific context used
144 to identify the component being measured.
145 @param Token Pointer to a Null-terminated ASCII string
146 that identifies the component being measured.
147 @param Module Pointer to a Null-terminated ASCII string
148 that identifies the module being measured.
149 @param TimeStamp 64-bit time stamp.
150 @param Identifier 32-bit identifier. If the value is 0, the created record
151 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
153 @retval EFI_SUCCESS The data was read correctly from the device.
154 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
160 IN CONST VOID
*Handle
, OPTIONAL
161 IN CONST CHAR8
*Token
, OPTIONAL
162 IN CONST CHAR8
*Module
, OPTIONAL
167 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
169 GAUGE_DATA_HEADER
*NewGaugeData
;
170 UINTN OldGaugeDataSize
;
171 GAUGE_DATA_HEADER
*OldGaugeData
;
174 AcquireSpinLock (&mSmmPerfLock
);
176 Index
= mGaugeData
->NumberOfEntries
;
177 if (Index
>= mMaxGaugeRecords
) {
179 // Try to enlarge the scale of gauge array.
181 OldGaugeData
= mGaugeData
;
182 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
;
184 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
* 2;
186 NewGaugeData
= AllocateZeroPool (GaugeDataSize
);
187 if (NewGaugeData
== NULL
) {
188 ReleaseSpinLock (&mSmmPerfLock
);
189 return EFI_OUT_OF_RESOURCES
;
192 mGaugeData
= NewGaugeData
;
193 mMaxGaugeRecords
*= 2;
196 // Initialize new data array and migrate old data one.
198 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
200 FreePool (OldGaugeData
);
203 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
204 GaugeEntryExArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
207 AsciiStrnCpyS (GaugeEntryExArray
[Index
].Token
, SMM_PERFORMANCE_STRING_SIZE
, Token
, SMM_PERFORMANCE_STRING_LENGTH
);
209 if (Module
!= NULL
) {
210 AsciiStrnCpyS (GaugeEntryExArray
[Index
].Module
, SMM_PERFORMANCE_STRING_SIZE
, Module
, SMM_PERFORMANCE_STRING_LENGTH
);
213 GaugeEntryExArray
[Index
].EndTimeStamp
= 0;
214 GaugeEntryExArray
[Index
].Identifier
= Identifier
;
216 if (TimeStamp
== 0) {
217 TimeStamp
= GetPerformanceCounter ();
219 GaugeEntryExArray
[Index
].StartTimeStamp
= TimeStamp
;
221 mGaugeData
->NumberOfEntries
++;
223 ReleaseSpinLock (&mSmmPerfLock
);
229 Searches the performance measurement log from the beginning of the log
230 for the first matching record that contains a zero end time and fills in a valid end time.
232 Searches the performance measurement log from the beginning of the log
233 for the first record that matches Handle, Token and Module and has an end time value of zero.
234 If the record can not be found then return EFI_NOT_FOUND.
235 If the record is found and TimeStamp is not zero,
236 then the end time in the record is filled in with the value specified by TimeStamp.
237 If the record is found and TimeStamp is zero, then the end time in the matching record
238 is filled in with the current time stamp value.
240 @param Handle Pointer to environment specific context used
241 to identify the component being measured.
242 @param Token Pointer to a Null-terminated ASCII string
243 that identifies the component being measured.
244 @param Module Pointer to a Null-terminated ASCII string
245 that identifies the module being measured.
246 @param TimeStamp 64-bit time stamp.
247 @param Identifier 32-bit identifier. If the value is 0, the found record
248 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
250 @retval EFI_SUCCESS The end of the measurement was recorded.
251 @retval EFI_NOT_FOUND The specified measurement record could not be found.
257 IN CONST VOID
*Handle
, OPTIONAL
258 IN CONST CHAR8
*Token
, OPTIONAL
259 IN CONST CHAR8
*Module
, OPTIONAL
264 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
267 AcquireSpinLock (&mSmmPerfLock
);
269 if (TimeStamp
== 0) {
270 TimeStamp
= GetPerformanceCounter ();
273 Index
= SmmSearchForGaugeEntry (Handle
, Token
, Module
, Identifier
);
274 if (Index
>= mGaugeData
->NumberOfEntries
) {
275 ReleaseSpinLock (&mSmmPerfLock
);
276 return EFI_NOT_FOUND
;
278 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
279 GaugeEntryExArray
[Index
].EndTimeStamp
= TimeStamp
;
281 ReleaseSpinLock (&mSmmPerfLock
);
287 Retrieves a previously logged performance measurement.
288 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
289 and then assign the Identifier with 0.
291 Retrieves the performance log entry from the performance log specified by LogEntryKey.
292 If it stands for a valid entry, then EFI_SUCCESS is returned and
293 GaugeDataEntryEx stores the pointer to that entry.
295 @param LogEntryKey The key for the previous performance measurement log entry.
296 If 0, then the first performance measurement log entry is retrieved.
297 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
298 if the retrieval is successful.
300 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
301 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
302 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
303 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
309 IN UINTN LogEntryKey
,
310 OUT GAUGE_DATA_ENTRY_EX
**GaugeDataEntryEx
313 UINTN NumberOfEntries
;
314 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
316 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
317 if (LogEntryKey
> NumberOfEntries
) {
318 return EFI_INVALID_PARAMETER
;
320 if (LogEntryKey
== NumberOfEntries
) {
321 return EFI_NOT_FOUND
;
324 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
326 if (GaugeDataEntryEx
== NULL
) {
327 return EFI_INVALID_PARAMETER
;
329 *GaugeDataEntryEx
= &GaugeEntryExArray
[LogEntryKey
];
335 Adds a record at the end of the performance measurement log
336 that records the start time of a performance measurement.
338 Adds a record to the end of the performance measurement log
339 that contains the Handle, Token, and Module.
340 The end time of the new record must be set to zero.
341 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
342 If TimeStamp is zero, the start time in the record is filled in with the value
343 read from the current time stamp.
345 @param Handle Pointer to environment specific context used
346 to identify the component being measured.
347 @param Token Pointer to a Null-terminated ASCII string
348 that identifies the component being measured.
349 @param Module Pointer to a Null-terminated ASCII string
350 that identifies the module being measured.
351 @param TimeStamp 64-bit time stamp.
353 @retval EFI_SUCCESS The data was read correctly from the device.
354 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
360 IN CONST VOID
*Handle
, OPTIONAL
361 IN CONST CHAR8
*Token
, OPTIONAL
362 IN CONST CHAR8
*Module
, OPTIONAL
366 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
370 Searches the performance measurement log from the beginning of the log
371 for the first matching record that contains a zero end time and fills in a valid end time.
373 Searches the performance measurement log from the beginning of the log
374 for the first record that matches Handle, Token, and Module and has an end time value of zero.
375 If the record can not be found then return EFI_NOT_FOUND.
376 If the record is found and TimeStamp is not zero,
377 then the end time in the record is filled in with the value specified by TimeStamp.
378 If the record is found and TimeStamp is zero, then the end time in the matching record
379 is filled in with the current time stamp value.
381 @param Handle Pointer to environment specific context used
382 to identify the component being measured.
383 @param Token Pointer to a Null-terminated ASCII string
384 that identifies the component being measured.
385 @param Module Pointer to a Null-terminated ASCII string
386 that identifies the module being measured.
387 @param TimeStamp 64-bit time stamp.
389 @retval EFI_SUCCESS The end of the measurement was recorded.
390 @retval EFI_NOT_FOUND The specified measurement record could not be found.
396 IN CONST VOID
*Handle
, OPTIONAL
397 IN CONST CHAR8
*Token
, OPTIONAL
398 IN CONST CHAR8
*Module
, OPTIONAL
402 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
406 Retrieves a previously logged performance measurement.
407 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
408 and then eliminate the Identifier.
410 Retrieves the performance log entry from the performance log specified by LogEntryKey.
411 If it stands for a valid entry, then EFI_SUCCESS is returned and
412 GaugeDataEntry stores the pointer to that entry.
414 @param LogEntryKey The key for the previous performance measurement log entry.
415 If 0, then the first performance measurement log entry is retrieved.
416 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
417 if the retrieval is successful.
419 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
420 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
421 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
422 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
428 IN UINTN LogEntryKey
,
429 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
433 GAUGE_DATA_ENTRY_EX
*GaugeEntryEx
;
437 Status
= GetGaugeEx (LogEntryKey
, &GaugeEntryEx
);
438 if (EFI_ERROR (Status
)) {
442 if (GaugeDataEntry
== NULL
) {
443 return EFI_INVALID_PARAMETER
;
446 *GaugeDataEntry
= (GAUGE_DATA_ENTRY
*) GaugeEntryEx
;
452 Communication service SMI Handler entry.
454 This SMI handler provides services for the performance wrapper driver.
456 Caution: This function may receive untrusted input.
457 Communicate buffer and buffer size are external input, so this function will do basic validation.
459 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
460 @param[in] RegisterContext Points to an optional handler context which was specified when the
461 handler was registered.
462 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
463 be conveyed from a non-SMM environment into an SMM environment.
464 @param[in, out] CommBufferSize The size of the CommBuffer.
466 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
467 should still be called.
468 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
470 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
472 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
476 SmmPerformanceHandlerEx (
477 IN EFI_HANDLE DispatchHandle
,
478 IN CONST VOID
*RegisterContext
,
479 IN OUT VOID
*CommBuffer
,
480 IN OUT UINTN
*CommBufferSize
484 SMM_PERF_COMMUNICATE_EX
*SmmPerfCommData
;
485 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
488 GAUGE_DATA_ENTRY_EX
*GaugeDataEx
;
489 UINTN NumberOfEntries
;
491 UINTN TempCommBufferSize
;
493 GaugeEntryExArray
= NULL
;
496 // If input is invalid, stop processing this SMI
498 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
502 TempCommBufferSize
= *CommBufferSize
;
504 if(TempCommBufferSize
< sizeof (SMM_PERF_COMMUNICATE_EX
)) {
508 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
509 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
513 SmmPerfCommData
= (SMM_PERF_COMMUNICATE_EX
*)CommBuffer
;
515 switch (SmmPerfCommData
->Function
) {
516 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
517 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
518 Status
= EFI_SUCCESS
;
521 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
522 GaugeDataEx
= SmmPerfCommData
->GaugeDataEx
;
523 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
524 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
525 if (GaugeDataEx
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
526 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| LogEntryKey
> (mGaugeData
->NumberOfEntries
- NumberOfEntries
)) {
527 Status
= EFI_INVALID_PARAMETER
;
534 DataSize
= MultU64x32 (NumberOfEntries
, sizeof(GAUGE_DATA_ENTRY_EX
));
535 if (!SmmIsBufferOutsideSmmValid ((UINTN
) GaugeDataEx
, DataSize
)) {
536 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n"));
537 Status
= EFI_ACCESS_DENIED
;
541 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
543 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
545 (UINT8
*) &GaugeDataEx
[Index
],
546 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
++],
547 sizeof (GAUGE_DATA_ENTRY_EX
)
550 Status
= EFI_SUCCESS
;
554 Status
= EFI_UNSUPPORTED
;
558 SmmPerfCommData
->ReturnStatus
= Status
;
564 Communication service SMI Handler entry.
566 This SMI handler provides services for the performance wrapper driver.
568 Caution: This function may receive untrusted input.
569 Communicate buffer and buffer size are external input, so this function will do basic validation.
571 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
572 @param[in] RegisterContext Points to an optional handler context which was specified when the
573 handler was registered.
574 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
575 be conveyed from a non-SMM environment into an SMM environment.
576 @param[in, out] CommBufferSize The size of the CommBuffer.
578 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
579 should still be called.
580 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
582 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
584 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
588 SmmPerformanceHandler (
589 IN EFI_HANDLE DispatchHandle
,
590 IN CONST VOID
*RegisterContext
,
591 IN OUT VOID
*CommBuffer
,
592 IN OUT UINTN
*CommBufferSize
596 SMM_PERF_COMMUNICATE
*SmmPerfCommData
;
597 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
600 GAUGE_DATA_ENTRY
*GaugeData
;
601 UINTN NumberOfEntries
;
603 UINTN TempCommBufferSize
;
605 GaugeEntryExArray
= NULL
;
608 // If input is invalid, stop processing this SMI
610 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
614 TempCommBufferSize
= *CommBufferSize
;
616 if(TempCommBufferSize
< sizeof (SMM_PERF_COMMUNICATE
)) {
620 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
621 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
625 SmmPerfCommData
= (SMM_PERF_COMMUNICATE
*)CommBuffer
;
627 switch (SmmPerfCommData
->Function
) {
628 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
629 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
630 Status
= EFI_SUCCESS
;
633 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
634 GaugeData
= SmmPerfCommData
->GaugeData
;
635 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
636 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
637 if (GaugeData
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
638 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| LogEntryKey
> (mGaugeData
->NumberOfEntries
- NumberOfEntries
)) {
639 Status
= EFI_INVALID_PARAMETER
;
646 DataSize
= MultU64x32 (NumberOfEntries
, sizeof(GAUGE_DATA_ENTRY
));
647 if (!SmmIsBufferOutsideSmmValid ((UINTN
) GaugeData
, DataSize
)) {
648 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n"));
649 Status
= EFI_ACCESS_DENIED
;
653 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
655 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
657 (UINT8
*) &GaugeData
[Index
],
658 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
++],
659 sizeof (GAUGE_DATA_ENTRY
)
662 Status
= EFI_SUCCESS
;
666 Status
= EFI_UNSUPPORTED
;
670 SmmPerfCommData
->ReturnStatus
= Status
;
676 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
677 this function is callbacked to initialize the Smm Performance Lib
679 @param Event The event of notify protocol.
680 @param Context Notify event context.
685 InitializeSmmCorePerformanceLib (
692 PERFORMANCE_PROPERTY
*PerformanceProperty
;
695 // Initialize spin lock
697 InitializeSpinLock (&mSmmPerfLock
);
699 mMaxGaugeRecords
= INIT_SMM_GAUGE_DATA_ENTRIES
;
701 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
));
702 ASSERT (mGaugeData
!= NULL
);
705 // Install the protocol interfaces.
707 Status
= gSmst
->SmmInstallProtocolInterface (
709 &gSmmPerformanceProtocolGuid
,
710 EFI_NATIVE_INTERFACE
,
711 &mPerformanceInterface
713 ASSERT_EFI_ERROR (Status
);
715 Status
= gSmst
->SmmInstallProtocolInterface (
717 &gSmmPerformanceExProtocolGuid
,
718 EFI_NATIVE_INTERFACE
,
719 &mPerformanceExInterface
721 ASSERT_EFI_ERROR (Status
);
724 /// Register SMM Performance SMI handler
727 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandler
, &gSmmPerformanceProtocolGuid
, &Handle
);
728 ASSERT_EFI_ERROR (Status
);
729 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandlerEx
, &gSmmPerformanceExProtocolGuid
, &Handle
);
730 ASSERT_EFI_ERROR (Status
);
732 Status
= EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid
, (VOID
**) &PerformanceProperty
);
733 if (EFI_ERROR (Status
)) {
735 // Install configuration table for performance property.
737 mPerformanceProperty
.Revision
= PERFORMANCE_PROPERTY_REVISION
;
738 mPerformanceProperty
.Reserved
= 0;
739 mPerformanceProperty
.Frequency
= GetPerformanceCounterProperties (
740 &mPerformanceProperty
.TimerStartValue
,
741 &mPerformanceProperty
.TimerEndValue
743 Status
= gBS
->InstallConfigurationTable (&gPerformanceProtocolGuid
, &mPerformanceProperty
);
744 ASSERT_EFI_ERROR (Status
);
749 The constructor function initializes the Performance Measurement Enable flag and
750 registers SmmBase2 protocol notify callback.
751 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
753 @param ImageHandle The firmware allocated handle for the EFI image.
754 @param SystemTable A pointer to the EFI System Table.
756 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
761 SmmCorePerformanceLibConstructor (
762 IN EFI_HANDLE ImageHandle
,
763 IN EFI_SYSTEM_TABLE
*SystemTable
770 mPerformanceMeasurementEnabled
= (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);
771 if (!mPerformanceMeasurementEnabled
) {
773 // Do not initialize performance infrastructure if not required.
779 // Create the events to do the library init.
781 Status
= gBS
->CreateEvent (
784 InitializeSmmCorePerformanceLib
,
788 ASSERT_EFI_ERROR (Status
);
791 // Register for protocol notifications on this event
793 Status
= gBS
->RegisterProtocolNotify (
794 &gEfiSmmBase2ProtocolGuid
,
799 ASSERT_EFI_ERROR (Status
);
805 Adds a record at the end of the performance measurement log
806 that records the start time of a performance measurement.
808 Adds a record to the end of the performance measurement log
809 that contains the Handle, Token, Module and Identifier.
810 The end time of the new record must be set to zero.
811 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
812 If TimeStamp is zero, the start time in the record is filled in with the value
813 read from the current time stamp.
815 @param Handle Pointer to environment specific context used
816 to identify the component being measured.
817 @param Token Pointer to a Null-terminated ASCII string
818 that identifies the component being measured.
819 @param Module Pointer to a Null-terminated ASCII string
820 that identifies the module being measured.
821 @param TimeStamp 64-bit time stamp.
822 @param Identifier 32-bit identifier. If the value is 0, the created record
823 is same as the one created by StartPerformanceMeasurement.
825 @retval RETURN_SUCCESS The start of the measurement was recorded.
826 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
831 StartPerformanceMeasurementEx (
832 IN CONST VOID
*Handle
, OPTIONAL
833 IN CONST CHAR8
*Token
, OPTIONAL
834 IN CONST CHAR8
*Module
, OPTIONAL
839 return (RETURN_STATUS
) StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
843 Searches the performance measurement log from the beginning of the log
844 for the first matching record that contains a zero end time and fills in a valid end time.
846 Searches the performance measurement log from the beginning of the log
847 for the first record that matches Handle, Token and Module and has an end time value of zero.
848 If the record can not be found then return RETURN_NOT_FOUND.
849 If the record is found and TimeStamp is not zero,
850 then the end time in the record is filled in with the value specified by TimeStamp.
851 If the record is found and TimeStamp is zero, then the end time in the matching record
852 is filled in with the current time stamp value.
854 @param Handle Pointer to environment specific context used
855 to identify the component being measured.
856 @param Token Pointer to a Null-terminated ASCII string
857 that identifies the component being measured.
858 @param Module Pointer to a Null-terminated ASCII string
859 that identifies the module being measured.
860 @param TimeStamp 64-bit time stamp.
861 @param Identifier 32-bit identifier. If the value is 0, the found record
862 is same as the one found by EndPerformanceMeasurement.
864 @retval RETURN_SUCCESS The end of the measurement was recorded.
865 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
870 EndPerformanceMeasurementEx (
871 IN CONST VOID
*Handle
, OPTIONAL
872 IN CONST CHAR8
*Token
, OPTIONAL
873 IN CONST CHAR8
*Module
, OPTIONAL
878 return (RETURN_STATUS
) EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
882 Attempts to retrieve a performance measurement log entry from the performance measurement log.
883 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
884 and then assign the Identifier with 0.
886 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
887 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
888 and the key for the second entry in the log is returned. If the performance log is empty,
889 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
890 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
891 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
892 retrieved and an implementation specific non-zero key value that specifies the end of the performance
893 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
894 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
895 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
896 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
897 If Handle is NULL, then ASSERT().
898 If Token is NULL, then ASSERT().
899 If Module is NULL, then ASSERT().
900 If StartTimeStamp is NULL, then ASSERT().
901 If EndTimeStamp is NULL, then ASSERT().
902 If Identifier is NULL, then ASSERT().
904 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
905 0, then the first performance measurement log entry is retrieved.
906 On exit, the key of the next performance log entry.
907 @param Handle Pointer to environment specific context used to identify the component
909 @param Token Pointer to a Null-terminated ASCII string that identifies the component
911 @param Module Pointer to a Null-terminated ASCII string that identifies the module
913 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
915 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
917 @param Identifier Pointer to the 32-bit identifier that was recorded.
919 @return The key for the next performance log entry (in general case).
924 GetPerformanceMeasurementEx (
925 IN UINTN LogEntryKey
,
926 OUT CONST VOID
**Handle
,
927 OUT CONST CHAR8
**Token
,
928 OUT CONST CHAR8
**Module
,
929 OUT UINT64
*StartTimeStamp
,
930 OUT UINT64
*EndTimeStamp
,
931 OUT UINT32
*Identifier
935 GAUGE_DATA_ENTRY_EX
*GaugeData
;
939 ASSERT (Handle
!= NULL
);
940 ASSERT (Token
!= NULL
);
941 ASSERT (Module
!= NULL
);
942 ASSERT (StartTimeStamp
!= NULL
);
943 ASSERT (EndTimeStamp
!= NULL
);
944 ASSERT (Identifier
!= NULL
);
946 Status
= GetGaugeEx (LogEntryKey
++, &GaugeData
);
949 // Make sure that LogEntryKey is a valid log entry key,
951 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
953 if (EFI_ERROR (Status
)) {
955 // The LogEntryKey is the last entry (equals to the total entry number).
960 ASSERT (GaugeData
!= NULL
);
962 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
963 *Token
= GaugeData
->Token
;
964 *Module
= GaugeData
->Module
;
965 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
966 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
967 *Identifier
= GaugeData
->Identifier
;
973 Adds a record at the end of the performance measurement log
974 that records the start time of a performance measurement.
976 Adds a record to the end of the performance measurement log
977 that contains the Handle, Token, and Module.
978 The end time of the new record must be set to zero.
979 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
980 If TimeStamp is zero, the start time in the record is filled in with the value
981 read from the current time stamp.
983 @param Handle Pointer to environment specific context used
984 to identify the component being measured.
985 @param Token Pointer to a Null-terminated ASCII string
986 that identifies the component being measured.
987 @param Module Pointer to a Null-terminated ASCII string
988 that identifies the module being measured.
989 @param TimeStamp 64-bit time stamp.
991 @retval RETURN_SUCCESS The start of the measurement was recorded.
992 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
997 StartPerformanceMeasurement (
998 IN CONST VOID
*Handle
, OPTIONAL
999 IN CONST CHAR8
*Token
, OPTIONAL
1000 IN CONST CHAR8
*Module
, OPTIONAL
1004 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
1008 Searches the performance measurement log from the beginning of the log
1009 for the first matching record that contains a zero end time and fills in a valid end time.
1011 Searches the performance measurement log from the beginning of the log
1012 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1013 If the record can not be found then return RETURN_NOT_FOUND.
1014 If the record is found and TimeStamp is not zero,
1015 then the end time in the record is filled in with the value specified by TimeStamp.
1016 If the record is found and TimeStamp is zero, then the end time in the matching record
1017 is filled in with the current time stamp value.
1019 @param Handle Pointer to environment specific context used
1020 to identify the component being measured.
1021 @param Token Pointer to a Null-terminated ASCII string
1022 that identifies the component being measured.
1023 @param Module Pointer to a Null-terminated ASCII string
1024 that identifies the module being measured.
1025 @param TimeStamp 64-bit time stamp.
1027 @retval RETURN_SUCCESS The end of the measurement was recorded.
1028 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1033 EndPerformanceMeasurement (
1034 IN CONST VOID
*Handle
, OPTIONAL
1035 IN CONST CHAR8
*Token
, OPTIONAL
1036 IN CONST CHAR8
*Module
, OPTIONAL
1040 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
1044 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1045 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1046 and then eliminate the Identifier.
1048 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1049 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1050 and the key for the second entry in the log is returned. If the performance log is empty,
1051 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1052 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1053 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1054 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1055 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1056 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1057 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1058 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1059 If Handle is NULL, then ASSERT().
1060 If Token is NULL, then ASSERT().
1061 If Module is NULL, then ASSERT().
1062 If StartTimeStamp is NULL, then ASSERT().
1063 If EndTimeStamp is NULL, then ASSERT().
1065 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1066 0, then the first performance measurement log entry is retrieved.
1067 On exit, the key of the next performance log entry.
1068 @param Handle Pointer to environment specific context used to identify the component
1070 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1072 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1074 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1076 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1079 @return The key for the next performance log entry (in general case).
1084 GetPerformanceMeasurement (
1085 IN UINTN LogEntryKey
,
1086 OUT CONST VOID
**Handle
,
1087 OUT CONST CHAR8
**Token
,
1088 OUT CONST CHAR8
**Module
,
1089 OUT UINT64
*StartTimeStamp
,
1090 OUT UINT64
*EndTimeStamp
1094 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
1098 Returns TRUE if the performance measurement macros are enabled.
1100 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1101 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1103 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1104 PcdPerformanceLibraryPropertyMask is set.
1105 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1106 PcdPerformanceLibraryPropertyMask is clear.
1111 PerformanceMeasurementEnabled (
1115 return mPerformanceMeasurementEnabled
;