]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
MdeModulePkg: Refine code to use Strn**S safe functions instead of Str**S ones in...
[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 - 2015, Intel Corporation. All rights reserved.<BR>
20 This program and the accompanying materials
21 are licensed and made available under the terms and conditions of the BSD License
22 which accompanies this distribution. The full text of the license may be found at
23 http://opensource.org/licenses/bsd-license.php
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 /**
73 Searches in the gauge array with keyword Handle, Token, Module and Identfier.
74
75 This internal function searches for the gauge entry in the gauge array.
76 If there is an entry that exactly matches the given keywords
77 and its end time stamp is zero, then the index of that gauge entry is returned;
78 otherwise, the the number of gauge entries in the array is returned.
79
80 @param Handle Pointer to environment specific context used
81 to identify the component being measured.
82 @param Token Pointer to a Null-terminated ASCII string
83 that identifies the component being measured.
84 @param Module Pointer to a Null-terminated ASCII string
85 that identifies the module being measured.
86 @param Identifier 32-bit identifier.
87
88 @retval The index of gauge entry in the array.
89
90 **/
91 UINT32
92 SmmSearchForGaugeEntry (
93 IN CONST VOID *Handle, OPTIONAL
94 IN CONST CHAR8 *Token, OPTIONAL
95 IN CONST CHAR8 *Module, OPTIONAL
96 IN CONST UINT32 Identifier
97 )
98 {
99 UINT32 Index;
100 UINT32 Index2;
101 UINT32 NumberOfEntries;
102 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
103
104 if (Token == NULL) {
105 Token = "";
106 }
107 if (Module == NULL) {
108 Module = "";
109 }
110
111 NumberOfEntries = mGaugeData->NumberOfEntries;
112 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
113
114 Index2 = 0;
115
116 for (Index = 0; Index < NumberOfEntries; Index++) {
117 Index2 = NumberOfEntries - 1 - Index;
118 if (GaugeEntryExArray[Index2].EndTimeStamp == 0 &&
119 (GaugeEntryExArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&
120 AsciiStrnCmp (GaugeEntryExArray[Index2].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&
121 AsciiStrnCmp (GaugeEntryExArray[Index2].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&
122 (GaugeEntryExArray[Index2].Identifier == Identifier)) {
123 Index = Index2;
124 break;
125 }
126 }
127
128 return Index;
129 }
130
131 /**
132 Adds a record at the end of the performance measurement log
133 that records the start time of a performance measurement.
134
135 Adds a record to the end of the performance measurement log
136 that contains the Handle, Token, Module and Identifier.
137 The end time of the new record must be set to zero.
138 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
139 If TimeStamp is zero, the start time in the record is filled in with the value
140 read from the current time stamp.
141
142 @param Handle Pointer to environment specific context used
143 to identify the component being measured.
144 @param Token Pointer to a Null-terminated ASCII string
145 that identifies the component being measured.
146 @param Module Pointer to a Null-terminated ASCII string
147 that identifies the module being measured.
148 @param TimeStamp 64-bit time stamp.
149 @param Identifier 32-bit identifier. If the value is 0, the created record
150 is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.
151
152 @retval EFI_SUCCESS The data was read correctly from the device.
153 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
154
155 **/
156 EFI_STATUS
157 EFIAPI
158 StartGaugeEx (
159 IN CONST VOID *Handle, OPTIONAL
160 IN CONST CHAR8 *Token, OPTIONAL
161 IN CONST CHAR8 *Module, OPTIONAL
162 IN UINT64 TimeStamp,
163 IN UINT32 Identifier
164 )
165 {
166 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
167 UINTN GaugeDataSize;
168 GAUGE_DATA_HEADER *NewGaugeData;
169 UINTN OldGaugeDataSize;
170 GAUGE_DATA_HEADER *OldGaugeData;
171 UINT32 Index;
172
173 AcquireSpinLock (&mSmmPerfLock);
174
175 Index = mGaugeData->NumberOfEntries;
176 if (Index >= mMaxGaugeRecords) {
177 //
178 // Try to enlarge the scale of gauge array.
179 //
180 OldGaugeData = mGaugeData;
181 OldGaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords;
182
183 GaugeDataSize = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords * 2;
184
185 NewGaugeData = AllocateZeroPool (GaugeDataSize);
186 if (NewGaugeData == NULL) {
187 ReleaseSpinLock (&mSmmPerfLock);
188 return EFI_OUT_OF_RESOURCES;
189 }
190
191 mGaugeData = NewGaugeData;
192 mMaxGaugeRecords *= 2;
193
194 //
195 // Initialize new data array and migrate old data one.
196 //
197 mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);
198
199 FreePool (OldGaugeData);
200 }
201
202 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
203 GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
204
205 if (Token != NULL) {
206 AsciiStrnCpyS (GaugeEntryExArray[Index].Token, SMM_PERFORMANCE_STRING_SIZE, Token, SMM_PERFORMANCE_STRING_LENGTH);
207 }
208 if (Module != NULL) {
209 AsciiStrnCpyS (GaugeEntryExArray[Index].Module, SMM_PERFORMANCE_STRING_SIZE, Module, SMM_PERFORMANCE_STRING_LENGTH);
210 }
211
212 GaugeEntryExArray[Index].EndTimeStamp = 0;
213 GaugeEntryExArray[Index].Identifier = Identifier;
214
215 if (TimeStamp == 0) {
216 TimeStamp = GetPerformanceCounter ();
217 }
218 GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;
219
220 mGaugeData->NumberOfEntries++;
221
222 ReleaseSpinLock (&mSmmPerfLock);
223
224 return EFI_SUCCESS;
225 }
226
227 /**
228 Searches the performance measurement log from the beginning of the log
229 for the first matching record that contains a zero end time and fills in a valid end time.
230
231 Searches the performance measurement log from the beginning of the log
232 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
233 If the record can not be found then return EFI_NOT_FOUND.
234 If the record is found and TimeStamp is not zero,
235 then the end time in the record is filled in with the value specified by TimeStamp.
236 If the record is found and TimeStamp is zero, then the end time in the matching record
237 is filled in with the current time stamp value.
238
239 @param Handle Pointer to environment specific context used
240 to identify the component being measured.
241 @param Token Pointer to a Null-terminated ASCII string
242 that identifies the component being measured.
243 @param Module Pointer to a Null-terminated ASCII string
244 that identifies the module being measured.
245 @param TimeStamp 64-bit time stamp.
246 @param Identifier 32-bit identifier. If the value is 0, the found record
247 is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.
248
249 @retval EFI_SUCCESS The end of the measurement was recorded.
250 @retval EFI_NOT_FOUND The specified measurement record could not be found.
251
252 **/
253 EFI_STATUS
254 EFIAPI
255 EndGaugeEx (
256 IN CONST VOID *Handle, OPTIONAL
257 IN CONST CHAR8 *Token, OPTIONAL
258 IN CONST CHAR8 *Module, OPTIONAL
259 IN UINT64 TimeStamp,
260 IN UINT32 Identifier
261 )
262 {
263 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
264 UINT32 Index;
265
266 AcquireSpinLock (&mSmmPerfLock);
267
268 if (TimeStamp == 0) {
269 TimeStamp = GetPerformanceCounter ();
270 }
271
272 Index = SmmSearchForGaugeEntry (Handle, Token, Module, Identifier);
273 if (Index >= mGaugeData->NumberOfEntries) {
274 ReleaseSpinLock (&mSmmPerfLock);
275 return EFI_NOT_FOUND;
276 }
277 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
278 GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;
279
280 ReleaseSpinLock (&mSmmPerfLock);
281
282 return EFI_SUCCESS;
283 }
284
285 /**
286 Retrieves a previously logged performance measurement.
287 It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,
288 and then assign the Identifier with 0.
289
290 Retrieves the performance log entry from the performance log specified by LogEntryKey.
291 If it stands for a valid entry, then EFI_SUCCESS is returned and
292 GaugeDataEntryEx stores the pointer to that entry.
293
294 @param LogEntryKey The key for the previous performance measurement log entry.
295 If 0, then the first performance measurement log entry is retrieved.
296 @param GaugeDataEntryEx The indirect pointer to the extended gauge data entry specified by LogEntryKey
297 if the retrieval is successful.
298
299 @retval EFI_SUCCESS The GuageDataEntryEx is successfully found based on LogEntryKey.
300 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
301 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
302 @retval EFI_INVALIDE_PARAMETER GaugeDataEntryEx is NULL.
303
304 **/
305 EFI_STATUS
306 EFIAPI
307 GetGaugeEx (
308 IN UINTN LogEntryKey,
309 OUT GAUGE_DATA_ENTRY_EX **GaugeDataEntryEx
310 )
311 {
312 UINTN NumberOfEntries;
313 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
314
315 NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);
316 if (LogEntryKey > NumberOfEntries) {
317 return EFI_INVALID_PARAMETER;
318 }
319 if (LogEntryKey == NumberOfEntries) {
320 return EFI_NOT_FOUND;
321 }
322
323 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
324
325 if (GaugeDataEntryEx == NULL) {
326 return EFI_INVALID_PARAMETER;
327 }
328 *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];
329
330 return EFI_SUCCESS;
331 }
332
333 /**
334 Adds a record at the end of the performance measurement log
335 that records the start time of a performance measurement.
336
337 Adds a record to the end of the performance measurement log
338 that contains the Handle, Token, and Module.
339 The end time of the new record must be set to zero.
340 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
341 If TimeStamp is zero, the start time in the record is filled in with the value
342 read from the current time stamp.
343
344 @param Handle Pointer to environment specific context used
345 to identify the component being measured.
346 @param Token Pointer to a Null-terminated ASCII string
347 that identifies the component being measured.
348 @param Module Pointer to a Null-terminated ASCII string
349 that identifies the module being measured.
350 @param TimeStamp 64-bit time stamp.
351
352 @retval EFI_SUCCESS The data was read correctly from the device.
353 @retval EFI_OUT_OF_RESOURCES There are not enough resources to record the measurement.
354
355 **/
356 EFI_STATUS
357 EFIAPI
358 StartGauge (
359 IN CONST VOID *Handle, OPTIONAL
360 IN CONST CHAR8 *Token, OPTIONAL
361 IN CONST CHAR8 *Module, OPTIONAL
362 IN UINT64 TimeStamp
363 )
364 {
365 return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);
366 }
367
368 /**
369 Searches the performance measurement log from the beginning of the log
370 for the first matching record that contains a zero end time and fills in a valid end time.
371
372 Searches the performance measurement log from the beginning of the log
373 for the first record that matches Handle, Token, and Module and has an end time value of zero.
374 If the record can not be found then return EFI_NOT_FOUND.
375 If the record is found and TimeStamp is not zero,
376 then the end time in the record is filled in with the value specified by TimeStamp.
377 If the record is found and TimeStamp is zero, then the end time in the matching record
378 is filled in with the current time stamp value.
379
380 @param Handle Pointer to environment specific context used
381 to identify the component being measured.
382 @param Token Pointer to a Null-terminated ASCII string
383 that identifies the component being measured.
384 @param Module Pointer to a Null-terminated ASCII string
385 that identifies the module being measured.
386 @param TimeStamp 64-bit time stamp.
387
388 @retval EFI_SUCCESS The end of the measurement was recorded.
389 @retval EFI_NOT_FOUND The specified measurement record could not be found.
390
391 **/
392 EFI_STATUS
393 EFIAPI
394 EndGauge (
395 IN CONST VOID *Handle, OPTIONAL
396 IN CONST CHAR8 *Token, OPTIONAL
397 IN CONST CHAR8 *Module, OPTIONAL
398 IN UINT64 TimeStamp
399 )
400 {
401 return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);
402 }
403
404 /**
405 Retrieves a previously logged performance measurement.
406 It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,
407 and then eliminate the Identifier.
408
409 Retrieves the performance log entry from the performance log specified by LogEntryKey.
410 If it stands for a valid entry, then EFI_SUCCESS is returned and
411 GaugeDataEntry stores the pointer to that entry.
412
413 @param LogEntryKey The key for the previous performance measurement log entry.
414 If 0, then the first performance measurement log entry is retrieved.
415 @param GaugeDataEntry The indirect pointer to the gauge data entry specified by LogEntryKey
416 if the retrieval is successful.
417
418 @retval EFI_SUCCESS The GuageDataEntry is successfully found based on LogEntryKey.
419 @retval EFI_NOT_FOUND The LogEntryKey is the last entry (equals to the total entry number).
420 @retval EFI_INVALIDE_PARAMETER The LogEntryKey is not a valid entry (greater than the total entry number).
421 @retval EFI_INVALIDE_PARAMETER GaugeDataEntry is NULL.
422
423 **/
424 EFI_STATUS
425 EFIAPI
426 GetGauge (
427 IN UINTN LogEntryKey,
428 OUT GAUGE_DATA_ENTRY **GaugeDataEntry
429 )
430 {
431 EFI_STATUS Status;
432 GAUGE_DATA_ENTRY_EX *GaugeEntryEx;
433
434 GaugeEntryEx = NULL;
435
436 Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);
437 if (EFI_ERROR (Status)) {
438 return Status;
439 }
440
441 if (GaugeDataEntry == NULL) {
442 return EFI_INVALID_PARAMETER;
443 }
444
445 *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;
446
447 return EFI_SUCCESS;
448 }
449
450 /**
451 Communication service SMI Handler entry.
452
453 This SMI handler provides services for the performance wrapper driver.
454
455 Caution: This function may receive untrusted input.
456 Communicate buffer and buffer size are external input, so this function will do basic validation.
457
458 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
459 @param[in] RegisterContext Points to an optional handler context which was specified when the
460 handler was registered.
461 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
462 be conveyed from a non-SMM environment into an SMM environment.
463 @param[in, out] CommBufferSize The size of the CommBuffer.
464
465 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
466 should still be called.
467 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
468 still be called.
469 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
470 be called.
471 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
472 **/
473 EFI_STATUS
474 EFIAPI
475 SmmPerformanceHandlerEx (
476 IN EFI_HANDLE DispatchHandle,
477 IN CONST VOID *RegisterContext,
478 IN OUT VOID *CommBuffer,
479 IN OUT UINTN *CommBufferSize
480 )
481 {
482 EFI_STATUS Status;
483 SMM_PERF_COMMUNICATE_EX *SmmPerfCommData;
484 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
485 UINTN DataSize;
486 GAUGE_DATA_ENTRY_EX *GaugeDataEx;
487 UINTN NumberOfEntries;
488 UINTN LogEntryKey;
489 UINTN TempCommBufferSize;
490
491 GaugeEntryExArray = NULL;
492
493 //
494 // If input is invalid, stop processing this SMI
495 //
496 if (CommBuffer == NULL || CommBufferSize == NULL) {
497 return EFI_SUCCESS;
498 }
499
500 TempCommBufferSize = *CommBufferSize;
501
502 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE_EX)) {
503 return EFI_SUCCESS;
504 }
505
506 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
507 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM communcation data buffer in SMRAM or overflow!\n"));
508 return EFI_SUCCESS;
509 }
510
511 SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)CommBuffer;
512
513 switch (SmmPerfCommData->Function) {
514 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
515 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
516 Status = EFI_SUCCESS;
517 break;
518
519 case SMM_PERF_FUNCTION_GET_GAUGE_DATA :
520 GaugeDataEx = SmmPerfCommData->GaugeDataEx;
521 NumberOfEntries = SmmPerfCommData->NumberOfEntries;
522 LogEntryKey = SmmPerfCommData->LogEntryKey;
523 if (GaugeDataEx == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries ||
524 NumberOfEntries > mGaugeData->NumberOfEntries || (LogEntryKey + NumberOfEntries) > mGaugeData->NumberOfEntries) {
525 Status = EFI_INVALID_PARAMETER;
526 break;
527 }
528
529 //
530 // Sanity check
531 //
532 DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY_EX);
533 if (!SmmIsBufferOutsideSmmValid ((UINTN)GaugeDataEx, DataSize)) {
534 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM Performance Data buffer in SMRAM or overflow!\n"));
535 Status = EFI_ACCESS_DENIED;
536 break;
537 }
538
539 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
540 CopyMem(
541 (UINT8 *) GaugeDataEx,
542 (UINT8 *) &GaugeEntryExArray[LogEntryKey],
543 DataSize
544 );
545 Status = EFI_SUCCESS;
546 break;
547
548 default:
549 Status = EFI_UNSUPPORTED;
550 }
551
552
553 SmmPerfCommData->ReturnStatus = Status;
554
555 return EFI_SUCCESS;
556 }
557
558 /**
559 Communication service SMI Handler entry.
560
561 This SMI handler provides services for the performance wrapper driver.
562
563 Caution: This function may receive untrusted input.
564 Communicate buffer and buffer size are external input, so this function will do basic validation.
565
566 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
567 @param[in] RegisterContext Points to an optional handler context which was specified when the
568 handler was registered.
569 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
570 be conveyed from a non-SMM environment into an SMM environment.
571 @param[in, out] CommBufferSize The size of the CommBuffer.
572
573 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
574 should still be called.
575 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
576 still be called.
577 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
578 be called.
579 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
580 **/
581 EFI_STATUS
582 EFIAPI
583 SmmPerformanceHandler (
584 IN EFI_HANDLE DispatchHandle,
585 IN CONST VOID *RegisterContext,
586 IN OUT VOID *CommBuffer,
587 IN OUT UINTN *CommBufferSize
588 )
589 {
590 EFI_STATUS Status;
591 SMM_PERF_COMMUNICATE *SmmPerfCommData;
592 GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;
593 UINTN DataSize;
594 UINTN Index;
595 GAUGE_DATA_ENTRY *GaugeData;
596 UINTN NumberOfEntries;
597 UINTN LogEntryKey;
598 UINTN TempCommBufferSize;
599
600 GaugeEntryExArray = NULL;
601
602 //
603 // If input is invalid, stop processing this SMI
604 //
605 if (CommBuffer == NULL || CommBufferSize == NULL) {
606 return EFI_SUCCESS;
607 }
608
609 TempCommBufferSize = *CommBufferSize;
610
611 if(TempCommBufferSize < sizeof (SMM_PERF_COMMUNICATE)) {
612 return EFI_SUCCESS;
613 }
614
615 if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
616 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM communcation data buffer in SMRAM or overflow!\n"));
617 return EFI_SUCCESS;
618 }
619
620 SmmPerfCommData = (SMM_PERF_COMMUNICATE *)CommBuffer;
621
622 switch (SmmPerfCommData->Function) {
623 case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :
624 SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;
625 Status = EFI_SUCCESS;
626 break;
627
628 case SMM_PERF_FUNCTION_GET_GAUGE_DATA :
629 GaugeData = SmmPerfCommData->GaugeData;
630 NumberOfEntries = SmmPerfCommData->NumberOfEntries;
631 LogEntryKey = SmmPerfCommData->LogEntryKey;
632 if (GaugeData == NULL || NumberOfEntries == 0 || LogEntryKey > mGaugeData->NumberOfEntries ||
633 NumberOfEntries > mGaugeData->NumberOfEntries || (LogEntryKey + NumberOfEntries) > mGaugeData->NumberOfEntries) {
634 Status = EFI_INVALID_PARAMETER;
635 break;
636 }
637
638 //
639 // Sanity check
640 //
641 DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
642 if (!SmmIsBufferOutsideSmmValid ((UINTN)GaugeData, DataSize)) {
643 DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM Performance Data buffer in SMRAM or overflow!\n"));
644 Status = EFI_ACCESS_DENIED;
645 break;
646 }
647
648 GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);
649
650 for (Index = 0; Index < NumberOfEntries; Index++) {
651 CopyMem(
652 (UINT8 *) &GaugeData[Index],
653 (UINT8 *) &GaugeEntryExArray[LogEntryKey++],
654 sizeof (GAUGE_DATA_ENTRY)
655 );
656 }
657 Status = EFI_SUCCESS;
658 break;
659
660 default:
661 Status = EFI_UNSUPPORTED;
662 }
663
664
665 SmmPerfCommData->ReturnStatus = Status;
666
667 return EFI_SUCCESS;
668 }
669
670 /**
671 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized
672 this function is callbacked to initialize the Smm Performance Lib
673
674 @param Event The event of notify protocol.
675 @param Context Notify event context.
676
677 **/
678 VOID
679 EFIAPI
680 InitializeSmmCorePerformanceLib (
681 IN EFI_EVENT Event,
682 IN VOID *Context
683 )
684 {
685 EFI_STATUS Status;
686 EFI_HANDLE Handle;
687
688 //
689 // Initialize spin lock
690 //
691 InitializeSpinLock (&mSmmPerfLock);
692
693 mMaxGaugeRecords = INIT_SMM_GAUGE_DATA_ENTRIES;
694
695 mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords));
696 ASSERT (mGaugeData != NULL);
697
698 //
699 // Install the protocol interfaces.
700 //
701 Status = gSmst->SmmInstallProtocolInterface (
702 &mHandle,
703 &gSmmPerformanceProtocolGuid,
704 EFI_NATIVE_INTERFACE,
705 &mPerformanceInterface
706 );
707 ASSERT_EFI_ERROR (Status);
708
709 Status = gSmst->SmmInstallProtocolInterface (
710 &mHandle,
711 &gSmmPerformanceExProtocolGuid,
712 EFI_NATIVE_INTERFACE,
713 &mPerformanceExInterface
714 );
715 ASSERT_EFI_ERROR (Status);
716
717 ///
718 /// Register SMM Performance SMI handler
719 ///
720 Handle = NULL;
721 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandler, &gSmmPerformanceProtocolGuid, &Handle);
722 ASSERT_EFI_ERROR (Status);
723 Status = gSmst->SmiHandlerRegister (SmmPerformanceHandlerEx, &gSmmPerformanceExProtocolGuid, &Handle);
724 ASSERT_EFI_ERROR (Status);
725 }
726
727 /**
728 The constructor function initializes the Performance Measurement Enable flag and
729 registers SmmBase2 protocol notify callback.
730 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.
731
732 @param ImageHandle The firmware allocated handle for the EFI image.
733 @param SystemTable A pointer to the EFI System Table.
734
735 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
736
737 **/
738 EFI_STATUS
739 EFIAPI
740 SmmCorePerformanceLibConstructor (
741 IN EFI_HANDLE ImageHandle,
742 IN EFI_SYSTEM_TABLE *SystemTable
743 )
744 {
745 EFI_STATUS Status;
746 EFI_EVENT Event;
747 VOID *Registration;
748
749 mPerformanceMeasurementEnabled = (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
750 if (!mPerformanceMeasurementEnabled) {
751 //
752 // Do not initialize performance infrastructure if not required.
753 //
754 return EFI_SUCCESS;
755 }
756
757 //
758 // Create the events to do the library init.
759 //
760 Status = gBS->CreateEvent (
761 EVT_NOTIFY_SIGNAL,
762 TPL_CALLBACK,
763 InitializeSmmCorePerformanceLib,
764 NULL,
765 &Event
766 );
767 ASSERT_EFI_ERROR (Status);
768
769 //
770 // Register for protocol notifications on this event
771 //
772 Status = gBS->RegisterProtocolNotify (
773 &gEfiSmmBase2ProtocolGuid,
774 Event,
775 &Registration
776 );
777
778 ASSERT_EFI_ERROR (Status);
779
780 return EFI_SUCCESS;
781 }
782
783 /**
784 Adds a record at the end of the performance measurement log
785 that records the start time of a performance measurement.
786
787 Adds a record to the end of the performance measurement log
788 that contains the Handle, Token, Module and Identifier.
789 The end time of the new record must be set to zero.
790 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
791 If TimeStamp is zero, the start time in the record is filled in with the value
792 read from the current time stamp.
793
794 @param Handle Pointer to environment specific context used
795 to identify the component being measured.
796 @param Token Pointer to a Null-terminated ASCII string
797 that identifies the component being measured.
798 @param Module Pointer to a Null-terminated ASCII string
799 that identifies the module being measured.
800 @param TimeStamp 64-bit time stamp.
801 @param Identifier 32-bit identifier. If the value is 0, the created record
802 is same as the one created by StartPerformanceMeasurement.
803
804 @retval RETURN_SUCCESS The start of the measurement was recorded.
805 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
806
807 **/
808 RETURN_STATUS
809 EFIAPI
810 StartPerformanceMeasurementEx (
811 IN CONST VOID *Handle, OPTIONAL
812 IN CONST CHAR8 *Token, OPTIONAL
813 IN CONST CHAR8 *Module, OPTIONAL
814 IN UINT64 TimeStamp,
815 IN UINT32 Identifier
816 )
817 {
818 return (RETURN_STATUS) StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
819 }
820
821 /**
822 Searches the performance measurement log from the beginning of the log
823 for the first matching record that contains a zero end time and fills in a valid end time.
824
825 Searches the performance measurement log from the beginning of the log
826 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.
827 If the record can not be found then return RETURN_NOT_FOUND.
828 If the record is found and TimeStamp is not zero,
829 then the end time in the record is filled in with the value specified by TimeStamp.
830 If the record is found and TimeStamp is zero, then the end time in the matching record
831 is filled in with the current time stamp value.
832
833 @param Handle Pointer to environment specific context used
834 to identify the component being measured.
835 @param Token Pointer to a Null-terminated ASCII string
836 that identifies the component being measured.
837 @param Module Pointer to a Null-terminated ASCII string
838 that identifies the module being measured.
839 @param TimeStamp 64-bit time stamp.
840 @param Identifier 32-bit identifier. If the value is 0, the found record
841 is same as the one found by EndPerformanceMeasurement.
842
843 @retval RETURN_SUCCESS The end of the measurement was recorded.
844 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
845
846 **/
847 RETURN_STATUS
848 EFIAPI
849 EndPerformanceMeasurementEx (
850 IN CONST VOID *Handle, OPTIONAL
851 IN CONST CHAR8 *Token, OPTIONAL
852 IN CONST CHAR8 *Module, OPTIONAL
853 IN UINT64 TimeStamp,
854 IN UINT32 Identifier
855 )
856 {
857 return (RETURN_STATUS) EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);
858 }
859
860 /**
861 Attempts to retrieve a performance measurement log entry from the performance measurement log.
862 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
863 and then assign the Identifier with 0.
864
865 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
866 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
867 and the key for the second entry in the log is returned. If the performance log is empty,
868 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
869 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
870 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
871 retrieved and an implementation specific non-zero key value that specifies the end of the performance
872 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
873 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
874 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
875 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
876 If Handle is NULL, then ASSERT().
877 If Token is NULL, then ASSERT().
878 If Module is NULL, then ASSERT().
879 If StartTimeStamp is NULL, then ASSERT().
880 If EndTimeStamp is NULL, then ASSERT().
881 If Identifier is NULL, then ASSERT().
882
883 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
884 0, then the first performance measurement log entry is retrieved.
885 On exit, the key of the next performance log entry.
886 @param Handle Pointer to environment specific context used to identify the component
887 being measured.
888 @param Token Pointer to a Null-terminated ASCII string that identifies the component
889 being measured.
890 @param Module Pointer to a Null-terminated ASCII string that identifies the module
891 being measured.
892 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
893 was started.
894 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
895 was ended.
896 @param Identifier Pointer to the 32-bit identifier that was recorded.
897
898 @return The key for the next performance log entry (in general case).
899
900 **/
901 UINTN
902 EFIAPI
903 GetPerformanceMeasurementEx (
904 IN UINTN LogEntryKey,
905 OUT CONST VOID **Handle,
906 OUT CONST CHAR8 **Token,
907 OUT CONST CHAR8 **Module,
908 OUT UINT64 *StartTimeStamp,
909 OUT UINT64 *EndTimeStamp,
910 OUT UINT32 *Identifier
911 )
912 {
913 EFI_STATUS Status;
914 GAUGE_DATA_ENTRY_EX *GaugeData;
915
916 GaugeData = NULL;
917
918 ASSERT (Handle != NULL);
919 ASSERT (Token != NULL);
920 ASSERT (Module != NULL);
921 ASSERT (StartTimeStamp != NULL);
922 ASSERT (EndTimeStamp != NULL);
923 ASSERT (Identifier != NULL);
924
925 Status = GetGaugeEx (LogEntryKey++, &GaugeData);
926
927 //
928 // Make sure that LogEntryKey is a valid log entry key,
929 //
930 ASSERT (Status != EFI_INVALID_PARAMETER);
931
932 if (EFI_ERROR (Status)) {
933 //
934 // The LogEntryKey is the last entry (equals to the total entry number).
935 //
936 return 0;
937 }
938
939 ASSERT (GaugeData != NULL);
940
941 *Handle = (VOID *) (UINTN) GaugeData->Handle;
942 *Token = GaugeData->Token;
943 *Module = GaugeData->Module;
944 *StartTimeStamp = GaugeData->StartTimeStamp;
945 *EndTimeStamp = GaugeData->EndTimeStamp;
946 *Identifier = GaugeData->Identifier;
947
948 return LogEntryKey;
949 }
950
951 /**
952 Adds a record at the end of the performance measurement log
953 that records the start time of a performance measurement.
954
955 Adds a record to the end of the performance measurement log
956 that contains the Handle, Token, and Module.
957 The end time of the new record must be set to zero.
958 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.
959 If TimeStamp is zero, the start time in the record is filled in with the value
960 read from the current time stamp.
961
962 @param Handle Pointer to environment specific context used
963 to identify the component being measured.
964 @param Token Pointer to a Null-terminated ASCII string
965 that identifies the component being measured.
966 @param Module Pointer to a Null-terminated ASCII string
967 that identifies the module being measured.
968 @param TimeStamp 64-bit time stamp.
969
970 @retval RETURN_SUCCESS The start of the measurement was recorded.
971 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
972
973 **/
974 RETURN_STATUS
975 EFIAPI
976 StartPerformanceMeasurement (
977 IN CONST VOID *Handle, OPTIONAL
978 IN CONST CHAR8 *Token, OPTIONAL
979 IN CONST CHAR8 *Module, OPTIONAL
980 IN UINT64 TimeStamp
981 )
982 {
983 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
984 }
985
986 /**
987 Searches the performance measurement log from the beginning of the log
988 for the first matching record that contains a zero end time and fills in a valid end time.
989
990 Searches the performance measurement log from the beginning of the log
991 for the first record that matches Handle, Token, and Module and has an end time value of zero.
992 If the record can not be found then return RETURN_NOT_FOUND.
993 If the record is found and TimeStamp is not zero,
994 then the end time in the record is filled in with the value specified by TimeStamp.
995 If the record is found and TimeStamp is zero, then the end time in the matching record
996 is filled in with the current time stamp value.
997
998 @param Handle Pointer to environment specific context used
999 to identify the component being measured.
1000 @param Token Pointer to a Null-terminated ASCII string
1001 that identifies the component being measured.
1002 @param Module Pointer to a Null-terminated ASCII string
1003 that identifies the module being measured.
1004 @param TimeStamp 64-bit time stamp.
1005
1006 @retval RETURN_SUCCESS The end of the measurement was recorded.
1007 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
1008
1009 **/
1010 RETURN_STATUS
1011 EFIAPI
1012 EndPerformanceMeasurement (
1013 IN CONST VOID *Handle, OPTIONAL
1014 IN CONST CHAR8 *Token, OPTIONAL
1015 IN CONST CHAR8 *Module, OPTIONAL
1016 IN UINT64 TimeStamp
1017 )
1018 {
1019 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
1020 }
1021
1022 /**
1023 Attempts to retrieve a performance measurement log entry from the performance measurement log.
1024 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
1025 and then eliminate the Identifier.
1026
1027 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
1028 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
1029 and the key for the second entry in the log is returned. If the performance log is empty,
1030 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
1031 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
1032 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
1033 retrieved and an implementation specific non-zero key value that specifies the end of the performance
1034 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
1035 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
1036 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
1037 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
1038 If Handle is NULL, then ASSERT().
1039 If Token is NULL, then ASSERT().
1040 If Module is NULL, then ASSERT().
1041 If StartTimeStamp is NULL, then ASSERT().
1042 If EndTimeStamp is NULL, then ASSERT().
1043
1044 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
1045 0, then the first performance measurement log entry is retrieved.
1046 On exit, the key of the next performance log entry.
1047 @param Handle Pointer to environment specific context used to identify the component
1048 being measured.
1049 @param Token Pointer to a Null-terminated ASCII string that identifies the component
1050 being measured.
1051 @param Module Pointer to a Null-terminated ASCII string that identifies the module
1052 being measured.
1053 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1054 was started.
1055 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
1056 was ended.
1057
1058 @return The key for the next performance log entry (in general case).
1059
1060 **/
1061 UINTN
1062 EFIAPI
1063 GetPerformanceMeasurement (
1064 IN UINTN LogEntryKey,
1065 OUT CONST VOID **Handle,
1066 OUT CONST CHAR8 **Token,
1067 OUT CONST CHAR8 **Module,
1068 OUT UINT64 *StartTimeStamp,
1069 OUT UINT64 *EndTimeStamp
1070 )
1071 {
1072 UINT32 Identifier;
1073 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);
1074 }
1075
1076 /**
1077 Returns TRUE if the performance measurement macros are enabled.
1078
1079 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1080 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
1081
1082 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1083 PcdPerformanceLibraryPropertyMask is set.
1084 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
1085 PcdPerformanceLibraryPropertyMask is clear.
1086
1087 **/
1088 BOOLEAN
1089 EFIAPI
1090 PerformanceMeasurementEnabled (
1091 VOID
1092 )
1093 {
1094 return mPerformanceMeasurementEnabled;
1095 }