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 - 2013, 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
;
54 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
55 UINTN mSmramRangeCount
;
58 // Interfaces for SMM Performance Protocol.
60 PERFORMANCE_PROTOCOL mPerformanceInterface
= {
67 // Interfaces for SMM PerformanceEx Protocol.
69 PERFORMANCE_EX_PROTOCOL mPerformanceExInterface
= {
76 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
78 This internal function searches for the gauge entry in the gauge array.
79 If there is an entry that exactly matches the given keywords
80 and its end time stamp is zero, then the index of that gauge entry is returned;
81 otherwise, the the number of gauge entries in the array is returned.
83 @param Handle Pointer to environment specific context used
84 to identify the component being measured.
85 @param Token Pointer to a Null-terminated ASCII string
86 that identifies the component being measured.
87 @param Module Pointer to a Null-terminated ASCII string
88 that identifies the module being measured.
89 @param Identifier 32-bit identifier.
91 @retval The index of gauge entry in the array.
95 SmmSearchForGaugeEntry (
96 IN CONST VOID
*Handle
, OPTIONAL
97 IN CONST CHAR8
*Token
, OPTIONAL
98 IN CONST CHAR8
*Module
, OPTIONAL
99 IN CONST UINT32 Identifier
104 UINT32 NumberOfEntries
;
105 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
110 if (Module
== NULL
) {
114 NumberOfEntries
= mGaugeData
->NumberOfEntries
;
115 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
119 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
120 Index2
= NumberOfEntries
- 1 - Index
;
121 if (GaugeEntryExArray
[Index2
].EndTimeStamp
== 0 &&
122 (GaugeEntryExArray
[Index2
].Handle
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
) &&
123 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Token
, Token
, SMM_PERFORMANCE_STRING_LENGTH
) == 0 &&
124 AsciiStrnCmp (GaugeEntryExArray
[Index2
].Module
, Module
, SMM_PERFORMANCE_STRING_LENGTH
) == 0 &&
125 (GaugeEntryExArray
[Index2
].Identifier
== Identifier
)) {
135 Adds a record at the end of the performance measurement log
136 that records the start time of a performance measurement.
138 Adds a record to the end of the performance measurement log
139 that contains the Handle, Token, Module and Identifier.
140 The end time of the new record must be set to zero.
141 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
142 If TimeStamp is zero, the start time in the record is filled in with the value
143 read from the current time stamp.
145 @param Handle Pointer to environment specific context used
146 to identify the component being measured.
147 @param Token Pointer to a Null-terminated ASCII string
148 that identifies the component being measured.
149 @param Module Pointer to a Null-terminated ASCII string
150 that identifies the module being measured.
151 @param TimeStamp 64-bit time stamp.
152 @param Identifier 32-bit identifier. If the value is 0, the created record
153 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
155 @retval EFI_SUCCESS The data was read correctly from the device.
156 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
162 IN CONST VOID
*Handle
, OPTIONAL
163 IN CONST CHAR8
*Token
, OPTIONAL
164 IN CONST CHAR8
*Module
, OPTIONAL
169 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
171 GAUGE_DATA_HEADER
*NewGaugeData
;
172 UINTN OldGaugeDataSize
;
173 GAUGE_DATA_HEADER
*OldGaugeData
;
176 AcquireSpinLock (&mSmmPerfLock
);
178 Index
= mGaugeData
->NumberOfEntries
;
179 if (Index
>= mMaxGaugeRecords
) {
181 // Try to enlarge the scale of gauge array.
183 OldGaugeData
= mGaugeData
;
184 OldGaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
;
186 GaugeDataSize
= sizeof (GAUGE_DATA_HEADER
) + sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
* 2;
188 NewGaugeData
= AllocateZeroPool (GaugeDataSize
);
189 if (NewGaugeData
== NULL
) {
190 ReleaseSpinLock (&mSmmPerfLock
);
191 return EFI_OUT_OF_RESOURCES
;
194 mGaugeData
= NewGaugeData
;
195 mMaxGaugeRecords
*= 2;
198 // Initialize new data array and migrate old data one.
200 mGaugeData
= CopyMem (mGaugeData
, OldGaugeData
, OldGaugeDataSize
);
202 FreePool (OldGaugeData
);
205 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
206 GaugeEntryExArray
[Index
].Handle
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Handle
;
209 AsciiStrnCpy (GaugeEntryExArray
[Index
].Token
, Token
, SMM_PERFORMANCE_STRING_LENGTH
);
211 if (Module
!= NULL
) {
212 AsciiStrnCpy (GaugeEntryExArray
[Index
].Module
, Module
, SMM_PERFORMANCE_STRING_LENGTH
);
215 GaugeEntryExArray
[Index
].EndTimeStamp
= 0;
216 GaugeEntryExArray
[Index
].Identifier
= Identifier
;
218 if (TimeStamp
== 0) {
219 TimeStamp
= GetPerformanceCounter ();
221 GaugeEntryExArray
[Index
].StartTimeStamp
= TimeStamp
;
223 mGaugeData
->NumberOfEntries
++;
225 ReleaseSpinLock (&mSmmPerfLock
);
231 Searches the performance measurement log from the beginning of the log
232 for the first matching record that contains a zero end time and fills in a valid end time.
234 Searches the performance measurement log from the beginning of the log
235 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
236 If the record can not be found then return EFI_NOT_FOUND.
237 If the record is found and TimeStamp is not zero,
238 then the end time in the record is filled in with the value specified by TimeStamp.
239 If the record is found and TimeStamp is zero, then the end time in the matching record
240 is filled in with the current time stamp value.
242 @param Handle Pointer to environment specific context used
243 to identify the component being measured.
244 @param Token Pointer to a Null-terminated ASCII string
245 that identifies the component being measured.
246 @param Module Pointer to a Null-terminated ASCII string
247 that identifies the module being measured.
248 @param TimeStamp 64-bit time stamp.
249 @param Identifier 32-bit identifier. If the value is 0, the found record
250 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
252 @retval EFI_SUCCESS The end of the measurement was recorded.
253 @retval EFI_NOT_FOUND The specified measurement record could not be found.
259 IN CONST VOID
*Handle
, OPTIONAL
260 IN CONST CHAR8
*Token
, OPTIONAL
261 IN CONST CHAR8
*Module
, OPTIONAL
266 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
269 AcquireSpinLock (&mSmmPerfLock
);
271 if (TimeStamp
== 0) {
272 TimeStamp
= GetPerformanceCounter ();
275 Index
= SmmSearchForGaugeEntry (Handle
, Token
, Module
, Identifier
);
276 if (Index
>= mGaugeData
->NumberOfEntries
) {
277 ReleaseSpinLock (&mSmmPerfLock
);
278 return EFI_NOT_FOUND
;
280 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
281 GaugeEntryExArray
[Index
].EndTimeStamp
= TimeStamp
;
283 ReleaseSpinLock (&mSmmPerfLock
);
289 Retrieves a previously logged performance measurement.
290 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
291 and then assign the Identifier with 0.
293 Retrieves the performance log entry from the performance log specified by LogEntryKey.
294 If it stands for a valid entry, then EFI_SUCCESS is returned and
295 GaugeDataEntryEx stores the pointer to that entry.
297 @param LogEntryKey The key for the previous performance measurement log entry.
298 If 0, then the first performance measurement log entry is retrieved.
299 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
300 if the retrieval is successful.
302 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
303 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
304 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
305 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
311 IN UINTN LogEntryKey
,
312 OUT GAUGE_DATA_ENTRY_EX
**GaugeDataEntryEx
315 UINTN NumberOfEntries
;
316 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
318 NumberOfEntries
= (UINTN
) (mGaugeData
->NumberOfEntries
);
319 if (LogEntryKey
> NumberOfEntries
) {
320 return EFI_INVALID_PARAMETER
;
322 if (LogEntryKey
== NumberOfEntries
) {
323 return EFI_NOT_FOUND
;
326 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
328 if (GaugeDataEntryEx
== NULL
) {
329 return EFI_INVALID_PARAMETER
;
331 *GaugeDataEntryEx
= &GaugeEntryExArray
[LogEntryKey
];
337 Adds a record at the end of the performance measurement log
338 that records the start time of a performance measurement.
340 Adds a record to the end of the performance measurement log
341 that contains the Handle, Token, and Module.
342 The end time of the new record must be set to zero.
343 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
344 If TimeStamp is zero, the start time in the record is filled in with the value
345 read from the current time stamp.
347 @param Handle Pointer to environment specific context used
348 to identify the component being measured.
349 @param Token Pointer to a Null-terminated ASCII string
350 that identifies the component being measured.
351 @param Module Pointer to a Null-terminated ASCII string
352 that identifies the module being measured.
353 @param TimeStamp 64-bit time stamp.
355 @retval EFI_SUCCESS The data was read correctly from the device.
356 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
362 IN CONST VOID
*Handle
, OPTIONAL
363 IN CONST CHAR8
*Token
, OPTIONAL
364 IN CONST CHAR8
*Module
, OPTIONAL
368 return StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
372 Searches the performance measurement log from the beginning of the log
373 for the first matching record that contains a zero end time and fills in a valid end time.
375 Searches the performance measurement log from the beginning of the log
376 for the first record that matches Handle, Token, and Module and has an end time value of zero.
377 If the record can not be found then return EFI_NOT_FOUND.
378 If the record is found and TimeStamp is not zero,
379 then the end time in the record is filled in with the value specified by TimeStamp.
380 If the record is found and TimeStamp is zero, then the end time in the matching record
381 is filled in with the current time stamp value.
383 @param Handle Pointer to environment specific context used
384 to identify the component being measured.
385 @param Token Pointer to a Null-terminated ASCII string
386 that identifies the component being measured.
387 @param Module Pointer to a Null-terminated ASCII string
388 that identifies the module being measured.
389 @param TimeStamp 64-bit time stamp.
391 @retval EFI_SUCCESS The end of the measurement was recorded.
392 @retval EFI_NOT_FOUND The specified measurement record could not be found.
398 IN CONST VOID
*Handle
, OPTIONAL
399 IN CONST CHAR8
*Token
, OPTIONAL
400 IN CONST CHAR8
*Module
, OPTIONAL
404 return EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, 0);
408 Retrieves a previously logged performance measurement.
409 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
410 and then eliminate the Identifier.
412 Retrieves the performance log entry from the performance log specified by LogEntryKey.
413 If it stands for a valid entry, then EFI_SUCCESS is returned and
414 GaugeDataEntry stores the pointer to that entry.
416 @param LogEntryKey The key for the previous performance measurement log entry.
417 If 0, then the first performance measurement log entry is retrieved.
418 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
419 if the retrieval is successful.
421 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
422 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
423 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
424 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
430 IN UINTN LogEntryKey
,
431 OUT GAUGE_DATA_ENTRY
**GaugeDataEntry
435 GAUGE_DATA_ENTRY_EX
*GaugeEntryEx
;
439 Status
= GetGaugeEx (LogEntryKey
, &GaugeEntryEx
);
440 if (EFI_ERROR (Status
)) {
444 if (GaugeDataEntry
== NULL
) {
445 return EFI_INVALID_PARAMETER
;
448 *GaugeDataEntry
= (GAUGE_DATA_ENTRY
*) GaugeEntryEx
;
454 This function check if the address is in SMRAM.
456 @param Buffer the buffer address to be checked.
457 @param Length the buffer length to be checked.
459 @retval TRUE this address is in SMRAM.
460 @retval FALSE this address is NOT in SMRAM.
464 IN EFI_PHYSICAL_ADDRESS Buffer
,
470 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
471 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
472 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
481 This function check if the address refered by Buffer and Length is valid.
483 @param Buffer the buffer address to be checked.
484 @param Length the buffer length to be checked.
486 @retval TRUE this address is valid.
487 @retval FALSE this address is NOT valid.
495 if (Buffer
> (MAX_ADDRESS
- Length
)) {
501 if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)Buffer
, (UINT64
)Length
)) {
508 Communication service SMI Handler entry.
510 This SMI handler provides services for the performance wrapper driver.
512 Caution: This function may receive untrusted input.
513 Communicate buffer and buffer size are external input, so this function will do basic validation.
515 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
516 @param[in] RegisterContext Points to an optional handler context which was specified when the
517 handler was registered.
518 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
519 be conveyed from a non-SMM environment into an SMM environment.
520 @param[in, out] CommBufferSize The size of the CommBuffer.
522 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
523 should still be called.
524 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
526 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
528 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
532 SmmPerformanceHandlerEx (
533 IN EFI_HANDLE DispatchHandle
,
534 IN CONST VOID
*RegisterContext
,
535 IN OUT VOID
*CommBuffer
,
536 IN OUT UINTN
*CommBufferSize
540 SMM_PERF_COMMUNICATE_EX
*SmmPerfCommData
;
541 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
543 GAUGE_DATA_ENTRY_EX
*GaugeDataEx
;
544 UINTN NumberOfEntries
;
547 GaugeEntryExArray
= NULL
;
550 // If input is invalid, stop processing this SMI
552 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
556 if(*CommBufferSize
< sizeof (SMM_PERF_COMMUNICATE_EX
)) {
560 if (!IsAddressValid ((UINTN
)CommBuffer
, *CommBufferSize
)) {
561 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
565 SmmPerfCommData
= (SMM_PERF_COMMUNICATE_EX
*)CommBuffer
;
567 switch (SmmPerfCommData
->Function
) {
568 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
569 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
570 Status
= EFI_SUCCESS
;
573 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
574 GaugeDataEx
= SmmPerfCommData
->GaugeDataEx
;
575 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
576 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
577 if (GaugeDataEx
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
578 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| (LogEntryKey
+ NumberOfEntries
) > mGaugeData
->NumberOfEntries
) {
579 Status
= EFI_INVALID_PARAMETER
;
586 DataSize
= NumberOfEntries
* sizeof(GAUGE_DATA_ENTRY_EX
);
587 if (!IsAddressValid ((UINTN
)GaugeDataEx
, DataSize
)) {
588 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n"));
589 Status
= EFI_ACCESS_DENIED
;
593 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
595 (UINT8
*) GaugeDataEx
,
596 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
],
599 Status
= EFI_SUCCESS
;
603 Status
= EFI_UNSUPPORTED
;
607 SmmPerfCommData
->ReturnStatus
= Status
;
613 Communication service SMI Handler entry.
615 This SMI handler provides services for the performance wrapper driver.
617 Caution: This function may receive untrusted input.
618 Communicate buffer and buffer size are external input, so this function will do basic validation.
620 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
621 @param[in] RegisterContext Points to an optional handler context which was specified when the
622 handler was registered.
623 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
624 be conveyed from a non-SMM environment into an SMM environment.
625 @param[in, out] CommBufferSize The size of the CommBuffer.
627 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
628 should still be called.
629 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
631 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
633 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
637 SmmPerformanceHandler (
638 IN EFI_HANDLE DispatchHandle
,
639 IN CONST VOID
*RegisterContext
,
640 IN OUT VOID
*CommBuffer
,
641 IN OUT UINTN
*CommBufferSize
645 SMM_PERF_COMMUNICATE
*SmmPerfCommData
;
646 GAUGE_DATA_ENTRY_EX
*GaugeEntryExArray
;
649 GAUGE_DATA_ENTRY
*GaugeData
;
650 UINTN NumberOfEntries
;
653 GaugeEntryExArray
= NULL
;
656 // If input is invalid, stop processing this SMI
658 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
662 if(*CommBufferSize
< sizeof (SMM_PERF_COMMUNICATE
)) {
666 if (!IsAddressValid ((UINTN
)CommBuffer
, *CommBufferSize
)) {
667 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
671 SmmPerfCommData
= (SMM_PERF_COMMUNICATE
*)CommBuffer
;
673 switch (SmmPerfCommData
->Function
) {
674 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER
:
675 SmmPerfCommData
->NumberOfEntries
= mGaugeData
->NumberOfEntries
;
676 Status
= EFI_SUCCESS
;
679 case SMM_PERF_FUNCTION_GET_GAUGE_DATA
:
680 GaugeData
= SmmPerfCommData
->GaugeData
;
681 NumberOfEntries
= SmmPerfCommData
->NumberOfEntries
;
682 LogEntryKey
= SmmPerfCommData
->LogEntryKey
;
683 if (GaugeData
== NULL
|| NumberOfEntries
== 0 || LogEntryKey
> mGaugeData
->NumberOfEntries
||
684 NumberOfEntries
> mGaugeData
->NumberOfEntries
|| (LogEntryKey
+ NumberOfEntries
) > mGaugeData
->NumberOfEntries
) {
685 Status
= EFI_INVALID_PARAMETER
;
692 DataSize
= NumberOfEntries
* sizeof(GAUGE_DATA_ENTRY
);
693 if (!IsAddressValid ((UINTN
)GaugeData
, DataSize
)) {
694 DEBUG ((EFI_D_ERROR
, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n"));
695 Status
= EFI_ACCESS_DENIED
;
699 GaugeEntryExArray
= (GAUGE_DATA_ENTRY_EX
*) (mGaugeData
+ 1);
701 for (Index
= 0; Index
< NumberOfEntries
; Index
++) {
703 (UINT8
*) &GaugeData
[Index
],
704 (UINT8
*) &GaugeEntryExArray
[LogEntryKey
++],
705 sizeof (GAUGE_DATA_ENTRY
)
708 Status
= EFI_SUCCESS
;
712 Status
= EFI_UNSUPPORTED
;
716 SmmPerfCommData
->ReturnStatus
= Status
;
722 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
723 this function is callbacked to initialize the Smm Performance Lib
725 @param Event The event of notify protocol.
726 @param Context Notify event context.
731 InitializeSmmCorePerformanceLib (
738 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
743 // Initialize spin lock
745 InitializeSpinLock (&mSmmPerfLock
);
747 mMaxGaugeRecords
= INIT_SMM_GAUGE_DATA_ENTRIES
;
749 mGaugeData
= AllocateZeroPool (sizeof (GAUGE_DATA_HEADER
) + (sizeof (GAUGE_DATA_ENTRY_EX
) * mMaxGaugeRecords
));
750 ASSERT (mGaugeData
!= NULL
);
753 // Get SMRAM information
755 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
756 ASSERT_EFI_ERROR (Status
);
759 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
760 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
762 Status
= gSmst
->SmmAllocatePool (
763 EfiRuntimeServicesData
,
765 (VOID
**)&mSmramRanges
767 ASSERT_EFI_ERROR (Status
);
769 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
770 ASSERT_EFI_ERROR (Status
);
772 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
775 // Install the protocol interfaces.
777 Status
= gSmst
->SmmInstallProtocolInterface (
779 &gSmmPerformanceProtocolGuid
,
780 EFI_NATIVE_INTERFACE
,
781 &mPerformanceInterface
783 ASSERT_EFI_ERROR (Status
);
785 Status
= gSmst
->SmmInstallProtocolInterface (
787 &gSmmPerformanceExProtocolGuid
,
788 EFI_NATIVE_INTERFACE
,
789 &mPerformanceExInterface
791 ASSERT_EFI_ERROR (Status
);
794 /// Register SMM Performance SMI handler
797 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandler
, &gSmmPerformanceProtocolGuid
, &Handle
);
798 ASSERT_EFI_ERROR (Status
);
799 Status
= gSmst
->SmiHandlerRegister (SmmPerformanceHandlerEx
, &gSmmPerformanceExProtocolGuid
, &Handle
);
800 ASSERT_EFI_ERROR (Status
);
804 The constructor function initializes the Performance Measurement Enable flag and
805 registers SmmBase2 protocol notify callback.
806 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
808 @param ImageHandle The firmware allocated handle for the EFI image.
809 @param SystemTable A pointer to the EFI System Table.
811 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
816 SmmCorePerformanceLibConstructor (
817 IN EFI_HANDLE ImageHandle
,
818 IN EFI_SYSTEM_TABLE
*SystemTable
825 mPerformanceMeasurementEnabled
= (BOOLEAN
) ((PcdGet8(PcdPerformanceLibraryPropertyMask
) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED
) != 0);
826 if (!mPerformanceMeasurementEnabled
) {
828 // Do not initialize performance infrastructure if not required.
834 // Create the events to do the library init.
836 Status
= gBS
->CreateEvent (
839 InitializeSmmCorePerformanceLib
,
843 ASSERT_EFI_ERROR (Status
);
846 // Register for protocol notifications on this event
848 Status
= gBS
->RegisterProtocolNotify (
849 &gEfiSmmBase2ProtocolGuid
,
854 ASSERT_EFI_ERROR (Status
);
860 Adds a record at the end of the performance measurement log
861 that records the start time of a performance measurement.
863 Adds a record to the end of the performance measurement log
864 that contains the Handle, Token, Module and Identifier.
865 The end time of the new record must be set to zero.
866 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
867 If TimeStamp is zero, the start time in the record is filled in with the value
868 read from the current time stamp.
870 @param Handle Pointer to environment specific context used
871 to identify the component being measured.
872 @param Token Pointer to a Null-terminated ASCII string
873 that identifies the component being measured.
874 @param Module Pointer to a Null-terminated ASCII string
875 that identifies the module being measured.
876 @param TimeStamp 64-bit time stamp.
877 @param Identifier 32-bit identifier. If the value is 0, the created record
878 is same as the one created by StartPerformanceMeasurement.
880 @retval RETURN_SUCCESS The start of the measurement was recorded.
881 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
886 StartPerformanceMeasurementEx (
887 IN CONST VOID
*Handle
, OPTIONAL
888 IN CONST CHAR8
*Token
, OPTIONAL
889 IN CONST CHAR8
*Module
, OPTIONAL
894 return (RETURN_STATUS
) StartGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
898 Searches the performance measurement log from the beginning of the log
899 for the first matching record that contains a zero end time and fills in a valid end time.
901 Searches the performance measurement log from the beginning of the log
902 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
903 If the record can not be found then return RETURN_NOT_FOUND.
904 If the record is found and TimeStamp is not zero,
905 then the end time in the record is filled in with the value specified by TimeStamp.
906 If the record is found and TimeStamp is zero, then the end time in the matching record
907 is filled in with the current time stamp value.
909 @param Handle Pointer to environment specific context used
910 to identify the component being measured.
911 @param Token Pointer to a Null-terminated ASCII string
912 that identifies the component being measured.
913 @param Module Pointer to a Null-terminated ASCII string
914 that identifies the module being measured.
915 @param TimeStamp 64-bit time stamp.
916 @param Identifier 32-bit identifier. If the value is 0, the found record
917 is same as the one found by EndPerformanceMeasurement.
919 @retval RETURN_SUCCESS The end of the measurement was recorded.
920 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
925 EndPerformanceMeasurementEx (
926 IN CONST VOID
*Handle
, OPTIONAL
927 IN CONST CHAR8
*Token
, OPTIONAL
928 IN CONST CHAR8
*Module
, OPTIONAL
933 return (RETURN_STATUS
) EndGaugeEx (Handle
, Token
, Module
, TimeStamp
, Identifier
);
937 Attempts to retrieve a performance measurement log entry from the performance measurement log.
938 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
939 and then assign the Identifier with 0.
941 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
942 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
943 and the key for the second entry in the log is returned. If the performance log is empty,
944 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
945 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
946 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
947 retrieved and an implementation specific non-zero key value that specifies the end of the performance
948 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
949 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
950 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
951 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
952 If Handle is NULL, then ASSERT().
953 If Token is NULL, then ASSERT().
954 If Module is NULL, then ASSERT().
955 If StartTimeStamp is NULL, then ASSERT().
956 If EndTimeStamp is NULL, then ASSERT().
957 If Identifier is NULL, then ASSERT().
959 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
960 0, then the first performance measurement log entry is retrieved.
961 On exit, the key of the next performance log entry.
962 @param Handle Pointer to environment specific context used to identify the component
964 @param Token Pointer to a Null-terminated ASCII string that identifies the component
966 @param Module Pointer to a Null-terminated ASCII string that identifies the module
968 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
970 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
972 @param Identifier Pointer to the 32-bit identifier that was recorded.
974 @return The key for the next performance log entry (in general case).
979 GetPerformanceMeasurementEx (
980 IN UINTN LogEntryKey
,
981 OUT CONST VOID
**Handle
,
982 OUT CONST CHAR8
**Token
,
983 OUT CONST CHAR8
**Module
,
984 OUT UINT64
*StartTimeStamp
,
985 OUT UINT64
*EndTimeStamp
,
986 OUT UINT32
*Identifier
990 GAUGE_DATA_ENTRY_EX
*GaugeData
;
994 ASSERT (Handle
!= NULL
);
995 ASSERT (Token
!= NULL
);
996 ASSERT (Module
!= NULL
);
997 ASSERT (StartTimeStamp
!= NULL
);
998 ASSERT (EndTimeStamp
!= NULL
);
999 ASSERT (Identifier
!= NULL
);
1001 Status
= GetGaugeEx (LogEntryKey
++, &GaugeData
);
1004 // Make sure that LogEntryKey is a valid log entry key,
1006 ASSERT (Status
!= EFI_INVALID_PARAMETER
);
1008 if (EFI_ERROR (Status
)) {
1010 // The LogEntryKey is the last entry (equals to the total entry number).
1015 ASSERT (GaugeData
!= NULL
);
1017 *Handle
= (VOID
*) (UINTN
) GaugeData
->Handle
;
1018 *Token
= GaugeData
->Token
;
1019 *Module
= GaugeData
->Module
;
1020 *StartTimeStamp
= GaugeData
->StartTimeStamp
;
1021 *EndTimeStamp
= GaugeData
->EndTimeStamp
;
1022 *Identifier
= GaugeData
->Identifier
;
1028 Adds a record at the end of the performance measurement log
1029 that records the start time of a performance measurement.
1031 Adds a record to the end of the performance measurement log
1032 that contains the Handle, Token, and Module.
1033 The end time of the new record must be set to zero.
1034 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1035 If TimeStamp is zero, the start time in the record is filled in with the value
1036 read from the current time stamp.
1038 @param Handle Pointer to environment specific context used
1039 to identify the component being measured.
1040 @param Token Pointer to a Null-terminated ASCII string
1041 that identifies the component being measured.
1042 @param Module Pointer to a Null-terminated ASCII string
1043 that identifies the module being measured.
1044 @param TimeStamp 64-bit time stamp.
1046 @retval RETURN_SUCCESS The start of the measurement was recorded.
1047 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1052 StartPerformanceMeasurement (
1053 IN CONST VOID
*Handle
, OPTIONAL
1054 IN CONST CHAR8
*Token
, OPTIONAL
1055 IN CONST CHAR8
*Module
, OPTIONAL
1059 return StartPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
1063 Searches the performance measurement log from the beginning of the log
1064 for the first matching record that contains a zero end time and fills in a valid end time.
1066 Searches the performance measurement log from the beginning of the log
1067 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1068 If the record can not be found then return RETURN_NOT_FOUND.
1069 If the record is found and TimeStamp is not zero,
1070 then the end time in the record is filled in with the value specified by TimeStamp.
1071 If the record is found and TimeStamp is zero, then the end time in the matching record
1072 is filled in with the current time stamp value.
1074 @param Handle Pointer to environment specific context used
1075 to identify the component being measured.
1076 @param Token Pointer to a Null-terminated ASCII string
1077 that identifies the component being measured.
1078 @param Module Pointer to a Null-terminated ASCII string
1079 that identifies the module being measured.
1080 @param TimeStamp 64-bit time stamp.
1082 @retval RETURN_SUCCESS The end of the measurement was recorded.
1083 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1088 EndPerformanceMeasurement (
1089 IN CONST VOID
*Handle
, OPTIONAL
1090 IN CONST CHAR8
*Token
, OPTIONAL
1091 IN CONST CHAR8
*Module
, OPTIONAL
1095 return EndPerformanceMeasurementEx (Handle
, Token
, Module
, TimeStamp
, 0);
1099 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1100 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1101 and then eliminate the Identifier.
1103 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1104 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1105 and the key for the second entry in the log is returned. If the performance log is empty,
1106 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1107 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1108 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1109 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1110 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1111 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1112 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1113 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1114 If Handle is NULL, then ASSERT().
1115 If Token is NULL, then ASSERT().
1116 If Module is NULL, then ASSERT().
1117 If StartTimeStamp is NULL, then ASSERT().
1118 If EndTimeStamp is NULL, then ASSERT().
1120 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1121 0, then the first performance measurement log entry is retrieved.
1122 On exit, the key of the next performance log entry.
1123 @param Handle Pointer to environment specific context used to identify the component
1125 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1127 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1129 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1131 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1134 @return The key for the next performance log entry (in general case).
1139 GetPerformanceMeasurement (
1140 IN UINTN LogEntryKey
,
1141 OUT CONST VOID
**Handle
,
1142 OUT CONST CHAR8
**Token
,
1143 OUT CONST CHAR8
**Module
,
1144 OUT UINT64
*StartTimeStamp
,
1145 OUT UINT64
*EndTimeStamp
1149 return GetPerformanceMeasurementEx (LogEntryKey
, Handle
, Token
, Module
, StartTimeStamp
, EndTimeStamp
, &Identifier
);
1153 Returns TRUE if the performance measurement macros are enabled.
1155 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1156 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1158 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1159 PcdPerformanceLibraryPropertyMask is set.
1160 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1161 PcdPerformanceLibraryPropertyMask is clear.
1166 PerformanceMeasurementEnabled (
1170 return mPerformanceMeasurementEnabled
;