]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
BaseTools: Clear build versions to sync with buildtools/BaseTools
[mirror_edk2.git] / MdeModulePkg / Library / SmmCorePerformanceLib / SmmCorePerformanceLib.c
1 /** @file
2 Performance library instance used by SMM Core.
3
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.
8
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.
11
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.
16
17 SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted input and do basic validation.
18
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
24
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.
27
28 **/
29
30
31 #include "SmmCorePerformanceLibInternal.h"
32
33 //
34 // The data structure to hold global performance data.
35 //
36 GAUGE_DATA_HEADER *mGaugeData;
37
38 //
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.
42 //
43 UINT32 mMaxGaugeRecords;
44
45 //
46 // The handle to install Performance Protocol instance.
47 //
48 EFI_HANDLE mHandle = NULL;
49
50 BOOLEAN mPerformanceMeasurementEnabled;
51
52 SPIN_LOCK mSmmPerfLock;
53
54 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
55 UINTN mSmramRangeCount;
56
57 //
58 // Interfaces for SMM Performance Protocol.
59 //
60 PERFORMANCE_PROTOCOL mPerformanceInterface = {
61 StartGauge,
62 EndGauge,
63 GetGauge
64 };
65
66 //
67 // Interfaces for SMM PerformanceEx Protocol.
68 //
69 PERFORMANCE_EX_PROTOCOL mPerformanceExInterface = {
70 StartGaugeEx,
71 EndGaugeEx,
72 GetGaugeEx
73 };
74
75 /**
76 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
77
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.
82
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.
90
91 @retval The index of gauge entry in the array.
92
93 **/
94 UINT32
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
100 )
101 {
102 UINT32 Index;
103 UINT32 Index2;
104 UINT32 NumberOfEntries;
105 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
106
107 if (Token == NULL) {
108 Token = "";
109 }
110 if (Module == NULL) {
111 Module = "";
112 }
113
114 NumberOfEntries = mGaugeData->NumberOfEntries;
115 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
116
117 Index2 = 0;
118
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)) {
126 Index = Index2;
127 break;
128 }
129 }
130
131 return Index;
132 }
133
134 /**
135 Adds a record at the end of the performance measurement log
136 that records the start time of a performance measurement.
137
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.
144
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.
154
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.
157
158 **/
159 EFI_STATUS
160 EFIAPI
161 StartGaugeEx (
162 IN CONST VOID *Handle, OPTIONAL
163 IN CONST CHAR8 *Token, OPTIONAL
164 IN CONST CHAR8 *Module, OPTIONAL
165 IN UINT64 TimeStamp,
166 IN UINT32 Identifier
167 )
168 {
169 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
170 UINTN GaugeDataSize;
171 GAUGE_DATA_HEADER *NewGaugeData;
172 UINTN OldGaugeDataSize;
173 GAUGE_DATA_HEADER *OldGaugeData;
174 UINT32 Index;
175
176 AcquireSpinLock (&mSmmPerfLock);
177
178 Index = mGaugeData->NumberOfEntries;
179 if (Index >= mMaxGaugeRecords) {
180 //
181 // Try to enlarge the scale of gauge array.
182 //
183 OldGaugeData = mGaugeData;
184 OldGaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords;
185
186 GaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords * 2;
187
188 NewGaugeData = AllocateZeroPool (GaugeDataSize);
189 if (NewGaugeData == NULL) {
190 ReleaseSpinLock (&mSmmPerfLock);
191 return EFI_OUT_OF_RESOURCES;
192 }
193
194 mGaugeData = NewGaugeData;
195 mMaxGaugeRecords *= 2;
196
197 //
198 // Initialize new data array and migrate old data one.
199 //
200 mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);
201
202 FreePool (OldGaugeData);
203 }
204
205 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
206 GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
207
208 if (Token != NULL) {
209 AsciiStrnCpy (GaugeEntryExArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH);
210 }
211 if (Module != NULL) {
212 AsciiStrnCpy (GaugeEntryExArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH);
213 }
214
215 GaugeEntryExArray[Index].EndTimeStamp = 0;
216 GaugeEntryExArray[Index].Identifier = Identifier;
217
218 if (TimeStamp == 0) {
219 TimeStamp = GetPerformanceCounter ();
220 }
221 GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;
222
223 mGaugeData->NumberOfEntries++;
224
225 ReleaseSpinLock (&mSmmPerfLock);
226
227 return EFI_SUCCESS;
228 }
229
230 /**
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.
233
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.
241
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.
251
252 @retval EFI_SUCCESS The end of the measurement was recorded.
253 @retval EFI_NOT_FOUND The specified measurement record could not be found.
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 EndGaugeEx (
259 IN CONST VOID *Handle, OPTIONAL
260 IN CONST CHAR8 *Token, OPTIONAL
261 IN CONST CHAR8 *Module, OPTIONAL
262 IN UINT64 TimeStamp,
263 IN UINT32 Identifier
264 )
265 {
266 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
267 UINT32 Index;
268
269 AcquireSpinLock (&mSmmPerfLock);
270
271 if (TimeStamp == 0) {
272 TimeStamp = GetPerformanceCounter ();
273 }
274
275 Index = SmmSearchForGaugeEntry (Handle, Token, Module, Identifier);
276 if (Index >= mGaugeData->NumberOfEntries) {
277 ReleaseSpinLock (&mSmmPerfLock);
278 return EFI_NOT_FOUND;
279 }
280 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
281 GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;
282
283 ReleaseSpinLock (&mSmmPerfLock);
284
285 return EFI_SUCCESS;
286 }
287
288 /**
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.
292
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.
296
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.
301
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.
306
307 **/
308 EFI_STATUS
309 EFIAPI
310 GetGaugeEx (
311 IN UINTN LogEntryKey,
312 OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx
313 )
314 {
315 UINTN NumberOfEntries;
316 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
317
318 NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);
319 if (LogEntryKey > NumberOfEntries) {
320 return EFI_INVALID_PARAMETER;
321 }
322 if (LogEntryKey == NumberOfEntries) {
323 return EFI_NOT_FOUND;
324 }
325
326 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
327
328 if (GaugeDataEntryEx == NULL) {
329 return EFI_INVALID_PARAMETER;
330 }
331 *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];
332
333 return EFI_SUCCESS;
334 }
335
336 /**
337 Adds a record at the end of the performance measurement log
338 that records the start time of a performance measurement.
339
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.
346
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.
354
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.
357
358 **/
359 EFI_STATUS
360 EFIAPI
361 StartGauge (
362 IN CONST VOID *Handle, OPTIONAL
363 IN CONST CHAR8 *Token, OPTIONAL
364 IN CONST CHAR8 *Module, OPTIONAL
365 IN UINT64 TimeStamp
366 )
367 {
368 return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);
369 }
370
371 /**
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.
374
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.
382
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.
390
391 @retval EFI_SUCCESS The end of the measurement was recorded.
392 @retval EFI_NOT_FOUND The specified measurement record could not be found.
393
394 **/
395 EFI_STATUS
396 EFIAPI
397 EndGauge (
398 IN CONST VOID *Handle, OPTIONAL
399 IN CONST CHAR8 *Token, OPTIONAL
400 IN CONST CHAR8 *Module, OPTIONAL
401 IN UINT64 TimeStamp
402 )
403 {
404 return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);
405 }
406
407 /**
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.
411
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.
415
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.
420
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.
425
426 **/
427 EFI_STATUS
428 EFIAPI
429 GetGauge (
430 IN UINTN LogEntryKey,
431 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
432 )
433 {
434 EFI_STATUS Status;
435 GAUGE_DATA_ENTRY_EX *GaugeEntryEx;
436
437 GaugeEntryEx = NULL;
438
439 Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);
440 if (EFI_ERROR (Status)) {
441 return Status;
442 }
443
444 if (GaugeDataEntry == NULL) {
445 return EFI_INVALID_PARAMETER;
446 }
447
448 *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;
449
450 return EFI_SUCCESS;
451 }
452
453 /**
454 This function check if the address is in SMRAM.
455
456 @param Buffer the buffer address to be checked.
457 @param Length the buffer length to be checked.
458
459 @retval TRUE this address is in SMRAM.
460 @retval FALSE this address is NOT in SMRAM.
461 **/
462 BOOLEAN
463 IsAddressInSmram (
464 IN EFI_PHYSICAL_ADDRESS Buffer,
465 IN UINT64 Length
466 )
467 {
468 UINTN Index;
469
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))) {
473 return TRUE;
474 }
475 }
476
477 return FALSE;
478 }
479
480 /**
481 This function check if the address refered by Buffer and Length is valid.
482
483 @param Buffer the buffer address to be checked.
484 @param Length the buffer length to be checked.
485
486 @retval TRUE this address is valid.
487 @retval FALSE this address is NOT valid.
488 **/
489 BOOLEAN
490 IsAddressValid (
491 IN UINTN Buffer,
492 IN UINTN Length
493 )
494 {
495 if (Buffer > (MAX_ADDRESS - Length)) {
496 //
497 // Overflow happen
498 //
499 return FALSE;
500 }
501 if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {
502 return FALSE;
503 }
504 return TRUE;
505 }
506
507 /**
508 Communication service SMI Handler entry.
509
510 This SMI handler provides services for the performance wrapper driver.
511
512 Caution: This function may receive untrusted input.
513 Communicate buffer and buffer size are external input, so this function will do basic validation.
514
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.
521
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
525 still be called.
526 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
527 be called.
528 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
529 **/
530 EFI_STATUS
531 EFIAPI
532 SmmPerformanceHandlerEx (
533 IN EFI_HANDLE DispatchHandle,
534 IN CONST VOID *RegisterContext,
535 IN OUT VOID *CommBuffer,
536 IN OUT UINTN *CommBufferSize
537 )
538 {
539 EFI_STATUS Status;
540 SMM_PERF_COMMUNICATE_EX *SmmPerfCommData;
541 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
542 UINTN DataSize;
543 GAUGE_DATA_ENTRY_EX *GaugeDataEx;
544 UINTN NumberOfEntries;
545 UINTN LogEntryKey;
546 UINTN TempCommBufferSize;
547
548 GaugeEntryExArray = NULL;
549
550 //
551 // If input is invalid, stop processing this SMI
552 //
553 if (CommBuffer == NULL || CommBufferSize == NULL) {
554 return EFI_SUCCESS;
555 }
556
557 TempCommBufferSize = *CommBufferSize;
558
559 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE_EX)) {
560 return EFI_SUCCESS;
561 }
562
563 if (!IsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
564 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
565 return EFI_SUCCESS;
566 }
567
568 SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)CommBuffer;
569
570 switch (SmmPerfCommData->Function) {
571 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
572 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
573 Status = EFI_SUCCESS;
574 break;
575
576 case SMM_PERF_FUNCTION_GET_GAUGE_DATA :
577 GaugeDataEx = SmmPerfCommData->GaugeDataEx;
578 NumberOfEntries = SmmPerfCommData->NumberOfEntries;
579 LogEntryKey = SmmPerfCommData->LogEntryKey;
580 if (GaugeDataEx == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries ||
581 NumberOfEntries > mGaugeData->NumberOfEntries || (LogEntryKey + NumberOfEntries) > mGaugeData->NumberOfEntries) {
582 Status = EFI_INVALID_PARAMETER;
583 break;
584 }
585
586 //
587 // Sanity check
588 //
589 DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY_EX);
590 if (!IsAddressValid ((UINTN)GaugeDataEx, DataSize)) {
591 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n"));
592 Status = EFI_ACCESS_DENIED;
593 break;
594 }
595
596 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
597 CopyMem(
598 (UINT8 *) GaugeDataEx,
599 (UINT8 *) &GaugeEntryExArray[LogEntryKey],
600 DataSize
601 );
602 Status = EFI_SUCCESS;
603 break;
604
605 default:
606 Status = EFI_UNSUPPORTED;
607 }
608
609
610 SmmPerfCommData->ReturnStatus = Status;
611
612 return EFI_SUCCESS;
613 }
614
615 /**
616 Communication service SMI Handler entry.
617
618 This SMI handler provides services for the performance wrapper driver.
619
620 Caution: This function may receive untrusted input.
621 Communicate buffer and buffer size are external input, so this function will do basic validation.
622
623 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
624 @param[in] RegisterContext Points to an optional handler context which was specified when the
625 handler was registered.
626 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
627 be conveyed from a non-SMM environment into an SMM environment.
628 @param[in, out] CommBufferSize The size of the CommBuffer.
629
630 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
631 should still be called.
632 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
633 still be called.
634 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
635 be called.
636 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
637 **/
638 EFI_STATUS
639 EFIAPI
640 SmmPerformanceHandler (
641 IN EFI_HANDLE DispatchHandle,
642 IN CONST VOID *RegisterContext,
643 IN OUT VOID *CommBuffer,
644 IN OUT UINTN *CommBufferSize
645 )
646 {
647 EFI_STATUS Status;
648 SMM_PERF_COMMUNICATE *SmmPerfCommData;
649 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
650 UINTN DataSize;
651 UINTN Index;
652 GAUGE_DATA_ENTRY *GaugeData;
653 UINTN NumberOfEntries;
654 UINTN LogEntryKey;
655 UINTN TempCommBufferSize;
656
657 GaugeEntryExArray = NULL;
658
659 //
660 // If input is invalid, stop processing this SMI
661 //
662 if (CommBuffer == NULL || CommBufferSize == NULL) {
663 return EFI_SUCCESS;
664 }
665
666 TempCommBufferSize = *CommBufferSize;
667
668 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE)) {
669 return EFI_SUCCESS;
670 }
671
672 if (!IsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
673 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
674 return EFI_SUCCESS;
675 }
676
677 SmmPerfCommData = (SMM_PERF_COMMUNICATE *)CommBuffer;
678
679 switch (SmmPerfCommData->Function) {
680 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
681 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
682 Status = EFI_SUCCESS;
683 break;
684
685 case SMM_PERF_FUNCTION_GET_GAUGE_DATA :
686 GaugeData = SmmPerfCommData->GaugeData;
687 NumberOfEntries = SmmPerfCommData->NumberOfEntries;
688 LogEntryKey = SmmPerfCommData->LogEntryKey;
689 if (GaugeData == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries ||
690 NumberOfEntries > mGaugeData->NumberOfEntries || (LogEntryKey + NumberOfEntries) > mGaugeData->NumberOfEntries) {
691 Status = EFI_INVALID_PARAMETER;
692 break;
693 }
694
695 //
696 // Sanity check
697 //
698 DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
699 if (!IsAddressValid ((UINTN)GaugeData, DataSize)) {
700 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n"));
701 Status = EFI_ACCESS_DENIED;
702 break;
703 }
704
705 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
706
707 for (Index = 0; Index < NumberOfEntries; Index++) {
708 CopyMem(
709 (UINT8 *) &GaugeData[Index],
710 (UINT8 *) &GaugeEntryExArray[LogEntryKey++],
711 sizeof (GAUGE_DATA_ENTRY)
712 );
713 }
714 Status = EFI_SUCCESS;
715 break;
716
717 default:
718 Status = EFI_UNSUPPORTED;
719 }
720
721
722 SmmPerfCommData->ReturnStatus = Status;
723
724 return EFI_SUCCESS;
725 }
726
727 /**
728 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
729 this function is callbacked to initialize the Smm Performance Lib
730
731 @param Event The event of notify protocol.
732 @param Context Notify event context.
733
734 **/
735 VOID
736 EFIAPI
737 InitializeSmmCorePerformanceLib (
738 IN EFI_EVENT Event,
739 IN VOID *Context
740 )
741 {
742 EFI_STATUS Status;
743 EFI_HANDLE Handle;
744 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
745 UINTN Size;
746
747
748 //
749 // Initialize spin lock
750 //
751 InitializeSpinLock (&mSmmPerfLock);
752
753 mMaxGaugeRecords = INIT_SMM_GAUGE_DATA_ENTRIES;
754
755 mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords));
756 ASSERT (mGaugeData != NULL);
757
758 //
759 // Get SMRAM information
760 //
761 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
762 ASSERT_EFI_ERROR (Status);
763
764 Size = 0;
765 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
766 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
767
768 Status = gSmst->SmmAllocatePool (
769 EfiRuntimeServicesData,
770 Size,
771 (VOID **)&mSmramRanges
772 );
773 ASSERT_EFI_ERROR (Status);
774
775 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
776 ASSERT_EFI_ERROR (Status);
777
778 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
779
780 //
781 // Install the protocol interfaces.
782 //
783 Status = gSmst->SmmInstallProtocolInterface (
784 &mHandle,
785 &gSmmPerformanceProtocolGuid,
786 EFI_NATIVE_INTERFACE,
787 &mPerformanceInterface
788 );
789 ASSERT_EFI_ERROR (Status);
790
791 Status = gSmst->SmmInstallProtocolInterface (
792 &mHandle,
793 &gSmmPerformanceExProtocolGuid,
794 EFI_NATIVE_INTERFACE,
795 &mPerformanceExInterface
796 );
797 ASSERT_EFI_ERROR (Status);
798
799 ///
800 /// Register SMM Performance SMI handler
801 ///
802 Handle = NULL;
803 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandler, &gSmmPerformanceProtocolGuid, &Handle);
804 ASSERT_EFI_ERROR (Status);
805 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandlerEx, &gSmmPerformanceExProtocolGuid, &Handle);
806 ASSERT_EFI_ERROR (Status);
807 }
808
809 /**
810 The constructor function initializes the Performance Measurement Enable flag and
811 registers SmmBase2 protocol notify callback.
812 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
813
814 @param ImageHandle The firmware allocated handle for the EFI image.
815 @param SystemTable A pointer to the EFI System Table.
816
817 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
818
819 **/
820 EFI_STATUS
821 EFIAPI
822 SmmCorePerformanceLibConstructor (
823 IN EFI_HANDLE ImageHandle,
824 IN EFI_SYSTEM_TABLE *SystemTable
825 )
826 {
827 EFI_STATUS Status;
828 EFI_EVENT Event;
829 VOID *Registration;
830
831 mPerformanceMeasurementEnabled = (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
832 if (!mPerformanceMeasurementEnabled) {
833 //
834 // Do not initialize performance infrastructure if not required.
835 //
836 return EFI_SUCCESS;
837 }
838
839 //
840 // Create the events to do the library init.
841 //
842 Status = gBS->CreateEvent (
843 EVT_NOTIFY_SIGNAL,
844 TPL_CALLBACK,
845 InitializeSmmCorePerformanceLib,
846 NULL,
847 &Event
848 );
849 ASSERT_EFI_ERROR (Status);
850
851 //
852 // Register for protocol notifications on this event
853 //
854 Status = gBS->RegisterProtocolNotify (
855 &gEfiSmmBase2ProtocolGuid,
856 Event,
857 &Registration
858 );
859
860 ASSERT_EFI_ERROR (Status);
861
862 return EFI_SUCCESS;
863 }
864
865 /**
866 Adds a record at the end of the performance measurement log
867 that records the start time of a performance measurement.
868
869 Adds a record to the end of the performance measurement log
870 that contains the Handle, Token, Module and Identifier.
871 The end time of the new record must be set to zero.
872 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
873 If TimeStamp is zero, the start time in the record is filled in with the value
874 read from the current time stamp.
875
876 @param Handle Pointer to environment specific context used
877 to identify the component being measured.
878 @param Token Pointer to a Null-terminated ASCII string
879 that identifies the component being measured.
880 @param Module Pointer to a Null-terminated ASCII string
881 that identifies the module being measured.
882 @param TimeStamp 64-bit time stamp.
883 @param Identifier 32-bit identifier. If the value is 0, the created record
884 is same as the one created by StartPerformanceMeasurement.
885
886 @retval RETURN_SUCCESS The start of the measurement was recorded.
887 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
888
889 **/
890 RETURN_STATUS
891 EFIAPI
892 StartPerformanceMeasurementEx (
893 IN CONST VOID *Handle, OPTIONAL
894 IN CONST CHAR8 *Token, OPTIONAL
895 IN CONST CHAR8 *Module, OPTIONAL
896 IN UINT64 TimeStamp,
897 IN UINT32 Identifier
898 )
899 {
900 return (RETURN_STATUS) StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
901 }
902
903 /**
904 Searches the performance measurement log from the beginning of the log
905 for the first matching record that contains a zero end time and fills in a valid end time.
906
907 Searches the performance measurement log from the beginning of the log
908 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
909 If the record can not be found then return RETURN_NOT_FOUND.
910 If the record is found and TimeStamp is not zero,
911 then the end time in the record is filled in with the value specified by TimeStamp.
912 If the record is found and TimeStamp is zero, then the end time in the matching record
913 is filled in with the current time stamp value.
914
915 @param Handle Pointer to environment specific context used
916 to identify the component being measured.
917 @param Token Pointer to a Null-terminated ASCII string
918 that identifies the component being measured.
919 @param Module Pointer to a Null-terminated ASCII string
920 that identifies the module being measured.
921 @param TimeStamp 64-bit time stamp.
922 @param Identifier 32-bit identifier. If the value is 0, the found record
923 is same as the one found by EndPerformanceMeasurement.
924
925 @retval RETURN_SUCCESS The end of the measurement was recorded.
926 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
927
928 **/
929 RETURN_STATUS
930 EFIAPI
931 EndPerformanceMeasurementEx (
932 IN CONST VOID *Handle, OPTIONAL
933 IN CONST CHAR8 *Token, OPTIONAL
934 IN CONST CHAR8 *Module, OPTIONAL
935 IN UINT64 TimeStamp,
936 IN UINT32 Identifier
937 )
938 {
939 return (RETURN_STATUS) EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
940 }
941
942 /**
943 Attempts to retrieve a performance measurement log entry from the performance measurement log.
944 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
945 and then assign the Identifier with 0.
946
947 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
948 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
949 and the key for the second entry in the log is returned. If the performance log is empty,
950 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
951 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
952 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
953 retrieved and an implementation specific non-zero key value that specifies the end of the performance
954 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
955 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
956 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
957 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
958 If Handle is NULL, then ASSERT().
959 If Token is NULL, then ASSERT().
960 If Module is NULL, then ASSERT().
961 If StartTimeStamp is NULL, then ASSERT().
962 If EndTimeStamp is NULL, then ASSERT().
963 If Identifier is NULL, then ASSERT().
964
965 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
966 0, then the first performance measurement log entry is retrieved.
967 On exit, the key of the next performance log entry.
968 @param Handle Pointer to environment specific context used to identify the component
969 being measured.
970 @param Token Pointer to a Null-terminated ASCII string that identifies the component
971 being measured.
972 @param Module Pointer to a Null-terminated ASCII string that identifies the module
973 being measured.
974 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
975 was started.
976 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
977 was ended.
978 @param Identifier Pointer to the 32-bit identifier that was recorded.
979
980 @return The key for the next performance log entry (in general case).
981
982 **/
983 UINTN
984 EFIAPI
985 GetPerformanceMeasurementEx (
986 IN UINTN LogEntryKey,
987 OUT CONST VOID **Handle,
988 OUT CONST CHAR8 **Token,
989 OUT CONST CHAR8 **Module,
990 OUT UINT64 *StartTimeStamp,
991 OUT UINT64 *EndTimeStamp,
992 OUT UINT32 *Identifier
993 )
994 {
995 EFI_STATUS Status;
996 GAUGE_DATA_ENTRY_EX *GaugeData;
997
998 GaugeData = NULL;
999
1000 ASSERT (Handle != NULL);
1001 ASSERT (Token != NULL);
1002 ASSERT (Module != NULL);
1003 ASSERT (StartTimeStamp != NULL);
1004 ASSERT (EndTimeStamp != NULL);
1005 ASSERT (Identifier != NULL);
1006
1007 Status = GetGaugeEx (LogEntryKey++, &GaugeData);
1008
1009 //
1010 // Make sure that LogEntryKey is a valid log entry key,
1011 //
1012 ASSERT (Status != EFI_INVALID_PARAMETER);
1013
1014 if (EFI_ERROR (Status)) {
1015 //
1016 // The LogEntryKey is the last entry (equals to the total entry number).
1017 //
1018 return 0;
1019 }
1020
1021 ASSERT (GaugeData != NULL);
1022
1023 *Handle = (VOID *) (UINTN) GaugeData->Handle;
1024 *Token = GaugeData->Token;
1025 *Module = GaugeData->Module;
1026 *StartTimeStamp = GaugeData->StartTimeStamp;
1027 *EndTimeStamp = GaugeData->EndTimeStamp;
1028 *Identifier = GaugeData->Identifier;
1029
1030 return LogEntryKey;
1031 }
1032
1033 /**
1034 Adds a record at the end of the performance measurement log
1035 that records the start time of a performance measurement.
1036
1037 Adds a record to the end of the performance measurement log
1038 that contains the Handle, Token, and Module.
1039 The end time of the new record must be set to zero.
1040 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
1041 If TimeStamp is zero, the start time in the record is filled in with the value
1042 read from the current time stamp.
1043
1044 @param Handle Pointer to environment specific context used
1045 to identify the component being measured.
1046 @param Token Pointer to a Null-terminated ASCII string
1047 that identifies the component being measured.
1048 @param Module Pointer to a Null-terminated ASCII string
1049 that identifies the module being measured.
1050 @param TimeStamp 64-bit time stamp.
1051
1052 @retval RETURN_SUCCESS The start of the measurement was recorded.
1053 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
1054
1055 **/
1056 RETURN_STATUS
1057 EFIAPI
1058 StartPerformanceMeasurement (
1059 IN CONST VOID *Handle, OPTIONAL
1060 IN CONST CHAR8 *Token, OPTIONAL
1061 IN CONST CHAR8 *Module, OPTIONAL
1062 IN UINT64 TimeStamp
1063 )
1064 {
1065 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1066 }
1067
1068 /**
1069 Searches the performance measurement log from the beginning of the log
1070 for the first matching record that contains a zero end time and fills in a valid end time.
1071
1072 Searches the performance measurement log from the beginning of the log
1073 for the first record that matches Handle, Token, and Module and has an end time value of zero.
1074 If the record can not be found then return RETURN_NOT_FOUND.
1075 If the record is found and TimeStamp is not zero,
1076 then the end time in the record is filled in with the value specified by TimeStamp.
1077 If the record is found and TimeStamp is zero, then the end time in the matching record
1078 is filled in with the current time stamp value.
1079
1080 @param Handle Pointer to environment specific context used
1081 to identify the component being measured.
1082 @param Token Pointer to a Null-terminated ASCII string
1083 that identifies the component being measured.
1084 @param Module Pointer to a Null-terminated ASCII string
1085 that identifies the module being measured.
1086 @param TimeStamp 64-bit time stamp.
1087
1088 @retval RETURN_SUCCESS The end of the measurement was recorded.
1089 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1090
1091 **/
1092 RETURN_STATUS
1093 EFIAPI
1094 EndPerformanceMeasurement (
1095 IN CONST VOID *Handle, OPTIONAL
1096 IN CONST CHAR8 *Token, OPTIONAL
1097 IN CONST CHAR8 *Module, OPTIONAL
1098 IN UINT64 TimeStamp
1099 )
1100 {
1101 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1102 }
1103
1104 /**
1105 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1106 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1107 and then eliminate the Identifier.
1108
1109 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1110 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1111 and the key for the second entry in the log is returned. If the performance log is empty,
1112 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1113 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1114 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1115 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1116 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1117 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1118 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1119 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1120 If Handle is NULL, then ASSERT().
1121 If Token is NULL, then ASSERT().
1122 If Module is NULL, then ASSERT().
1123 If StartTimeStamp is NULL, then ASSERT().
1124 If EndTimeStamp is NULL, then ASSERT().
1125
1126 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1127 0, then the first performance measurement log entry is retrieved.
1128 On exit, the key of the next performance log entry.
1129 @param Handle Pointer to environment specific context used to identify the component
1130 being measured.
1131 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1132 being measured.
1133 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1134 being measured.
1135 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1136 was started.
1137 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1138 was ended.
1139
1140 @return The key for the next performance log entry (in general case).
1141
1142 **/
1143 UINTN
1144 EFIAPI
1145 GetPerformanceMeasurement (
1146 IN UINTN LogEntryKey,
1147 OUT CONST VOID **Handle,
1148 OUT CONST CHAR8 **Token,
1149 OUT CONST CHAR8 **Module,
1150 OUT UINT64 *StartTimeStamp,
1151 OUT UINT64 *EndTimeStamp
1152 )
1153 {
1154 UINT32 Identifier;
1155 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);
1156 }
1157
1158 /**
1159 Returns TRUE if the performance measurement macros are enabled.
1160
1161 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1162 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1163
1164 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1165 PcdPerformanceLibraryPropertyMask is set.
1166 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1167 PcdPerformanceLibraryPropertyMask is clear.
1168
1169 **/
1170 BOOLEAN
1171 EFIAPI
1172 PerformanceMeasurementEnabled (
1173 VOID
1174 )
1175 {
1176 return mPerformanceMeasurementEnabled;
1177 }