]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
MdeModulePkg: Add performance property configuration table
[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 - 2016, 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 //
55 // Interfaces for SMM Performance Protocol.
56 //
57 PERFORMANCE_PROTOCOL mPerformanceInterface = {
58 StartGauge,
59 EndGauge,
60 GetGauge
61 };
62
63 //
64 // Interfaces for SMM PerformanceEx Protocol.
65 //
66 PERFORMANCE_EX_PROTOCOL mPerformanceExInterface = {
67 StartGaugeEx,
68 EndGaugeEx,
69 GetGaugeEx
70 };
71
72 PERFORMANCE_PROPERTY mPerformanceProperty;
73
74 /**
75 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
76
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.
81
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.
89
90 @retval The index of gauge entry in the array.
91
92 **/
93 UINT32
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
99 )
100 {
101 UINT32 Index;
102 UINT32 Index2;
103 UINT32 NumberOfEntries;
104 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
105
106 if (Token == NULL) {
107 Token = "";
108 }
109 if (Module == NULL) {
110 Module = "";
111 }
112
113 NumberOfEntries = mGaugeData->NumberOfEntries;
114 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
115
116 Index2 = 0;
117
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) {
124 Index = Index2;
125 break;
126 }
127 }
128
129 return Index;
130 }
131
132 /**
133 Adds a record at the end of the performance measurement log
134 that records the start time of a performance measurement.
135
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.
142
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.
152
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.
155
156 **/
157 EFI_STATUS
158 EFIAPI
159 StartGaugeEx (
160 IN CONST VOID *Handle, OPTIONAL
161 IN CONST CHAR8 *Token, OPTIONAL
162 IN CONST CHAR8 *Module, OPTIONAL
163 IN UINT64 TimeStamp,
164 IN UINT32 Identifier
165 )
166 {
167 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
168 UINTN GaugeDataSize;
169 GAUGE_DATA_HEADER *NewGaugeData;
170 UINTN OldGaugeDataSize;
171 GAUGE_DATA_HEADER *OldGaugeData;
172 UINT32 Index;
173
174 AcquireSpinLock (&mSmmPerfLock);
175
176 Index = mGaugeData->NumberOfEntries;
177 if (Index >= mMaxGaugeRecords) {
178 //
179 // Try to enlarge the scale of gauge array.
180 //
181 OldGaugeData = mGaugeData;
182 OldGaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords;
183
184 GaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords * 2;
185
186 NewGaugeData = AllocateZeroPool (GaugeDataSize);
187 if (NewGaugeData == NULL) {
188 ReleaseSpinLock (&mSmmPerfLock);
189 return EFI_OUT_OF_RESOURCES;
190 }
191
192 mGaugeData = NewGaugeData;
193 mMaxGaugeRecords *= 2;
194
195 //
196 // Initialize new data array and migrate old data one.
197 //
198 mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);
199
200 FreePool (OldGaugeData);
201 }
202
203 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
204 GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
205
206 if (Token != NULL) {
207 AsciiStrnCpyS (GaugeEntryExArray[Index].Token, SMM_PERFORMANCE_STRING_SIZE, Token, SMM_PERFORMANCE_STRING_LENGTH);
208 }
209 if (Module != NULL) {
210 AsciiStrnCpyS (GaugeEntryExArray[Index].Module, SMM_PERFORMANCE_STRING_SIZE, Module, SMM_PERFORMANCE_STRING_LENGTH);
211 }
212
213 GaugeEntryExArray[Index].EndTimeStamp = 0;
214 GaugeEntryExArray[Index].Identifier = Identifier;
215
216 if (TimeStamp == 0) {
217 TimeStamp = GetPerformanceCounter ();
218 }
219 GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;
220
221 mGaugeData->NumberOfEntries++;
222
223 ReleaseSpinLock (&mSmmPerfLock);
224
225 return EFI_SUCCESS;
226 }
227
228 /**
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.
231
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.
239
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.
249
250 @retval EFI_SUCCESS The end of the measurement was recorded.
251 @retval EFI_NOT_FOUND The specified measurement record could not be found.
252
253 **/
254 EFI_STATUS
255 EFIAPI
256 EndGaugeEx (
257 IN CONST VOID *Handle, OPTIONAL
258 IN CONST CHAR8 *Token, OPTIONAL
259 IN CONST CHAR8 *Module, OPTIONAL
260 IN UINT64 TimeStamp,
261 IN UINT32 Identifier
262 )
263 {
264 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
265 UINT32 Index;
266
267 AcquireSpinLock (&mSmmPerfLock);
268
269 if (TimeStamp == 0) {
270 TimeStamp = GetPerformanceCounter ();
271 }
272
273 Index = SmmSearchForGaugeEntry (Handle, Token, Module, Identifier);
274 if (Index >= mGaugeData->NumberOfEntries) {
275 ReleaseSpinLock (&mSmmPerfLock);
276 return EFI_NOT_FOUND;
277 }
278 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
279 GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;
280
281 ReleaseSpinLock (&mSmmPerfLock);
282
283 return EFI_SUCCESS;
284 }
285
286 /**
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.
290
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.
294
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.
299
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.
304
305 **/
306 EFI_STATUS
307 EFIAPI
308 GetGaugeEx (
309 IN UINTN LogEntryKey,
310 OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx
311 )
312 {
313 UINTN NumberOfEntries;
314 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
315
316 NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);
317 if (LogEntryKey > NumberOfEntries) {
318 return EFI_INVALID_PARAMETER;
319 }
320 if (LogEntryKey == NumberOfEntries) {
321 return EFI_NOT_FOUND;
322 }
323
324 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
325
326 if (GaugeDataEntryEx == NULL) {
327 return EFI_INVALID_PARAMETER;
328 }
329 *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];
330
331 return EFI_SUCCESS;
332 }
333
334 /**
335 Adds a record at the end of the performance measurement log
336 that records the start time of a performance measurement.
337
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.
344
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.
352
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.
355
356 **/
357 EFI_STATUS
358 EFIAPI
359 StartGauge (
360 IN CONST VOID *Handle, OPTIONAL
361 IN CONST CHAR8 *Token, OPTIONAL
362 IN CONST CHAR8 *Module, OPTIONAL
363 IN UINT64 TimeStamp
364 )
365 {
366 return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);
367 }
368
369 /**
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.
372
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.
380
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.
388
389 @retval EFI_SUCCESS The end of the measurement was recorded.
390 @retval EFI_NOT_FOUND The specified measurement record could not be found.
391
392 **/
393 EFI_STATUS
394 EFIAPI
395 EndGauge (
396 IN CONST VOID *Handle, OPTIONAL
397 IN CONST CHAR8 *Token, OPTIONAL
398 IN CONST CHAR8 *Module, OPTIONAL
399 IN UINT64 TimeStamp
400 )
401 {
402 return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);
403 }
404
405 /**
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.
409
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.
413
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.
418
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.
423
424 **/
425 EFI_STATUS
426 EFIAPI
427 GetGauge (
428 IN UINTN LogEntryKey,
429 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
430 )
431 {
432 EFI_STATUS Status;
433 GAUGE_DATA_ENTRY_EX *GaugeEntryEx;
434
435 GaugeEntryEx = NULL;
436
437 Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);
438 if (EFI_ERROR (Status)) {
439 return Status;
440 }
441
442 if (GaugeDataEntry == NULL) {
443 return EFI_INVALID_PARAMETER;
444 }
445
446 *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;
447
448 return EFI_SUCCESS;
449 }
450
451 /**
452 Communication service SMI Handler entry.
453
454 This SMI handler provides services for the performance wrapper driver.
455
456 Caution: This function may receive untrusted input.
457 Communicate buffer and buffer size are external input, so this function will do basic validation.
458
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.
465
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
469 still be called.
470 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
471 be called.
472 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
473 **/
474 EFI_STATUS
475 EFIAPI
476 SmmPerformanceHandlerEx (
477 IN EFI_HANDLE DispatchHandle,
478 IN CONST VOID *RegisterContext,
479 IN OUT VOID *CommBuffer,
480 IN OUT UINTN *CommBufferSize
481 )
482 {
483 EFI_STATUS Status;
484 SMM_PERF_COMMUNICATE_EX *SmmPerfCommData;
485 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
486 UINT64 DataSize;
487 UINTN Index;
488 GAUGE_DATA_ENTRY_EX *GaugeDataEx;
489 UINTN NumberOfEntries;
490 UINTN LogEntryKey;
491 UINTN TempCommBufferSize;
492
493 GaugeEntryExArray = NULL;
494
495 //
496 // If input is invalid, stop processing this SMI
497 //
498 if (CommBuffer == NULL || CommBufferSize == NULL) {
499 return EFI_SUCCESS;
500 }
501
502 TempCommBufferSize = *CommBufferSize;
503
504 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE_EX)) {
505 return EFI_SUCCESS;
506 }
507
508 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
509 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
510 return EFI_SUCCESS;
511 }
512
513 SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)CommBuffer;
514
515 switch (SmmPerfCommData->Function) {
516 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
517 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
518 Status = EFI_SUCCESS;
519 break;
520
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;
528 break;
529 }
530
531 //
532 // Sanity check
533 //
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;
538 break;
539 }
540
541 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
542
543 for (Index = 0; Index < NumberOfEntries; Index++) {
544 CopyMem (
545 (UINT8 *) &GaugeDataEx[Index],
546 (UINT8 *) &GaugeEntryExArray[LogEntryKey++],
547 sizeof (GAUGE_DATA_ENTRY_EX)
548 );
549 }
550 Status = EFI_SUCCESS;
551 break;
552
553 default:
554 Status = EFI_UNSUPPORTED;
555 }
556
557
558 SmmPerfCommData->ReturnStatus = Status;
559
560 return EFI_SUCCESS;
561 }
562
563 /**
564 Communication service SMI Handler entry.
565
566 This SMI handler provides services for the performance wrapper driver.
567
568 Caution: This function may receive untrusted input.
569 Communicate buffer and buffer size are external input, so this function will do basic validation.
570
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.
577
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
581 still be called.
582 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
583 be called.
584 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
585 **/
586 EFI_STATUS
587 EFIAPI
588 SmmPerformanceHandler (
589 IN EFI_HANDLE DispatchHandle,
590 IN CONST VOID *RegisterContext,
591 IN OUT VOID *CommBuffer,
592 IN OUT UINTN *CommBufferSize
593 )
594 {
595 EFI_STATUS Status;
596 SMM_PERF_COMMUNICATE *SmmPerfCommData;
597 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
598 UINT64 DataSize;
599 UINTN Index;
600 GAUGE_DATA_ENTRY *GaugeData;
601 UINTN NumberOfEntries;
602 UINTN LogEntryKey;
603 UINTN TempCommBufferSize;
604
605 GaugeEntryExArray = NULL;
606
607 //
608 // If input is invalid, stop processing this SMI
609 //
610 if (CommBuffer == NULL || CommBufferSize == NULL) {
611 return EFI_SUCCESS;
612 }
613
614 TempCommBufferSize = *CommBufferSize;
615
616 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE)) {
617 return EFI_SUCCESS;
618 }
619
620 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
621 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
622 return EFI_SUCCESS;
623 }
624
625 SmmPerfCommData = (SMM_PERF_COMMUNICATE *)CommBuffer;
626
627 switch (SmmPerfCommData->Function) {
628 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
629 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
630 Status = EFI_SUCCESS;
631 break;
632
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;
640 break;
641 }
642
643 //
644 // Sanity check
645 //
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;
650 break;
651 }
652
653 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
654
655 for (Index = 0; Index < NumberOfEntries; Index++) {
656 CopyMem (
657 (UINT8 *) &GaugeData[Index],
658 (UINT8 *) &GaugeEntryExArray[LogEntryKey++],
659 sizeof (GAUGE_DATA_ENTRY)
660 );
661 }
662 Status = EFI_SUCCESS;
663 break;
664
665 default:
666 Status = EFI_UNSUPPORTED;
667 }
668
669
670 SmmPerfCommData->ReturnStatus = Status;
671
672 return EFI_SUCCESS;
673 }
674
675 /**
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
678
679 @param Event The event of notify protocol.
680 @param Context Notify event context.
681
682 **/
683 VOID
684 EFIAPI
685 InitializeSmmCorePerformanceLib (
686 IN EFI_EVENT Event,
687 IN VOID *Context
688 )
689 {
690 EFI_STATUS Status;
691 EFI_HANDLE Handle;
692 PERFORMANCE_PROPERTY *PerformanceProperty;
693
694 //
695 // Initialize spin lock
696 //
697 InitializeSpinLock (&mSmmPerfLock);
698
699 mMaxGaugeRecords = INIT_SMM_GAUGE_DATA_ENTRIES;
700
701 mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords));
702 ASSERT (mGaugeData != NULL);
703
704 //
705 // Install the protocol interfaces.
706 //
707 Status = gSmst->SmmInstallProtocolInterface (
708 &mHandle,
709 &gSmmPerformanceProtocolGuid,
710 EFI_NATIVE_INTERFACE,
711 &mPerformanceInterface
712 );
713 ASSERT_EFI_ERROR (Status);
714
715 Status = gSmst->SmmInstallProtocolInterface (
716 &mHandle,
717 &gSmmPerformanceExProtocolGuid,
718 EFI_NATIVE_INTERFACE,
719 &mPerformanceExInterface
720 );
721 ASSERT_EFI_ERROR (Status);
722
723 ///
724 /// Register SMM Performance SMI handler
725 ///
726 Handle = NULL;
727 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandler, &gSmmPerformanceProtocolGuid, &Handle);
728 ASSERT_EFI_ERROR (Status);
729 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandlerEx, &gSmmPerformanceExProtocolGuid, &Handle);
730 ASSERT_EFI_ERROR (Status);
731
732 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, &PerformanceProperty);
733 if (EFI_ERROR (Status)) {
734 //
735 // Install configuration table for performance property.
736 //
737 mPerformanceProperty.Revision = PERFORMANCE_PROPERTY_REVISION;
738 mPerformanceProperty.Reserved = 0;
739 mPerformanceProperty.Frequency = GetPerformanceCounterProperties (
740 &mPerformanceProperty.TimerStartValue,
741 &mPerformanceProperty.TimerEndValue
742 );
743 Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);
744 ASSERT_EFI_ERROR (Status);
745 }
746 }
747
748 /**
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.
752
753 @param ImageHandle The firmware allocated handle for the EFI image.
754 @param SystemTable A pointer to the EFI System Table.
755
756 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
757
758 **/
759 EFI_STATUS
760 EFIAPI
761 SmmCorePerformanceLibConstructor (
762 IN EFI_HANDLE ImageHandle,
763 IN EFI_SYSTEM_TABLE *SystemTable
764 )
765 {
766 EFI_STATUS Status;
767 EFI_EVENT Event;
768 VOID *Registration;
769
770 mPerformanceMeasurementEnabled = (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
771 if (!mPerformanceMeasurementEnabled) {
772 //
773 // Do not initialize performance infrastructure if not required.
774 //
775 return EFI_SUCCESS;
776 }
777
778 //
779 // Create the events to do the library init.
780 //
781 Status = gBS->CreateEvent (
782 EVT_NOTIFY_SIGNAL,
783 TPL_CALLBACK,
784 InitializeSmmCorePerformanceLib,
785 NULL,
786 &Event
787 );
788 ASSERT_EFI_ERROR (Status);
789
790 //
791 // Register for protocol notifications on this event
792 //
793 Status = gBS->RegisterProtocolNotify (
794 &gEfiSmmBase2ProtocolGuid,
795 Event,
796 &Registration
797 );
798
799 ASSERT_EFI_ERROR (Status);
800
801 return EFI_SUCCESS;
802 }
803
804 /**
805 Adds a record at the end of the performance measurement log
806 that records the start time of a performance measurement.
807
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.
814
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.
824
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.
827
828 **/
829 RETURN_STATUS
830 EFIAPI
831 StartPerformanceMeasurementEx (
832 IN CONST VOID *Handle, OPTIONAL
833 IN CONST CHAR8 *Token, OPTIONAL
834 IN CONST CHAR8 *Module, OPTIONAL
835 IN UINT64 TimeStamp,
836 IN UINT32 Identifier
837 )
838 {
839 return (RETURN_STATUS) StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
840 }
841
842 /**
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.
845
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.
853
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.
863
864 @retval RETURN_SUCCESS The end of the measurement was recorded.
865 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
866
867 **/
868 RETURN_STATUS
869 EFIAPI
870 EndPerformanceMeasurementEx (
871 IN CONST VOID *Handle, OPTIONAL
872 IN CONST CHAR8 *Token, OPTIONAL
873 IN CONST CHAR8 *Module, OPTIONAL
874 IN UINT64 TimeStamp,
875 IN UINT32 Identifier
876 )
877 {
878 return (RETURN_STATUS) EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
879 }
880
881 /**
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.
885
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().
903
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
908 being measured.
909 @param Token Pointer to a Null-terminated ASCII string that identifies the component
910 being measured.
911 @param Module Pointer to a Null-terminated ASCII string that identifies the module
912 being measured.
913 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
914 was started.
915 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
916 was ended.
917 @param Identifier Pointer to the 32-bit identifier that was recorded.
918
919 @return The key for the next performance log entry (in general case).
920
921 **/
922 UINTN
923 EFIAPI
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
932 )
933 {
934 EFI_STATUS Status;
935 GAUGE_DATA_ENTRY_EX *GaugeData;
936
937 GaugeData = NULL;
938
939 ASSERT (Handle != NULL);
940 ASSERT (Token != NULL);
941 ASSERT (Module != NULL);
942 ASSERT (StartTimeStamp != NULL);
943 ASSERT (EndTimeStamp != NULL);
944 ASSERT (Identifier != NULL);
945
946 Status = GetGaugeEx (LogEntryKey++, &GaugeData);
947
948 //
949 // Make sure that LogEntryKey is a valid log entry key,
950 //
951 ASSERT (Status != EFI_INVALID_PARAMETER);
952
953 if (EFI_ERROR (Status)) {
954 //
955 // The LogEntryKey is the last entry (equals to the total entry number).
956 //
957 return 0;
958 }
959
960 ASSERT (GaugeData != NULL);
961
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;
968
969 return LogEntryKey;
970 }
971
972 /**
973 Adds a record at the end of the performance measurement log
974 that records the start time of a performance measurement.
975
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.
982
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.
990
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.
993
994 **/
995 RETURN_STATUS
996 EFIAPI
997 StartPerformanceMeasurement (
998 IN CONST VOID *Handle, OPTIONAL
999 IN CONST CHAR8 *Token, OPTIONAL
1000 IN CONST CHAR8 *Module, OPTIONAL
1001 IN UINT64 TimeStamp
1002 )
1003 {
1004 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1005 }
1006
1007 /**
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.
1010
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.
1018
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.
1026
1027 @retval RETURN_SUCCESS The end of the measurement was recorded.
1028 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1029
1030 **/
1031 RETURN_STATUS
1032 EFIAPI
1033 EndPerformanceMeasurement (
1034 IN CONST VOID *Handle, OPTIONAL
1035 IN CONST CHAR8 *Token, OPTIONAL
1036 IN CONST CHAR8 *Module, OPTIONAL
1037 IN UINT64 TimeStamp
1038 )
1039 {
1040 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1041 }
1042
1043 /**
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.
1047
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().
1064
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
1069 being measured.
1070 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1071 being measured.
1072 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1073 being measured.
1074 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1075 was started.
1076 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1077 was ended.
1078
1079 @return The key for the next performance log entry (in general case).
1080
1081 **/
1082 UINTN
1083 EFIAPI
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
1091 )
1092 {
1093 UINT32 Identifier;
1094 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);
1095 }
1096
1097 /**
1098 Returns TRUE if the performance measurement macros are enabled.
1099
1100 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1101 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1102
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.
1107
1108 **/
1109 BOOLEAN
1110 EFIAPI
1111 PerformanceMeasurementEnabled (
1112 VOID
1113 )
1114 {
1115 return mPerformanceMeasurementEnabled;
1116 }