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