]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
ShellPkg/dp: Correct case of included file
[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
2001f84e 19Copyright (c) 2011 - 2018, 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
2001f84e
DB
33#define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))\r
34#define FIRMWARE_RECORD_BUFFER 0x1000\r
35#define CACHE_HANDLE_GUID_COUNT 0x100\r
d042c6e8 36\r
2001f84e 37SMM_BOOT_PERFORMANCE_TABLE *mSmmBootPerformanceTable = NULL;\r
d042c6e8 38\r
2001f84e
DB
39typedef struct {\r
40 EFI_HANDLE Handle;\r
41 CHAR8 NameString[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
42 EFI_GUID ModuleGuid;\r
43} HANDLE_GUID_MAP;\r
d042c6e8 44\r
2001f84e
DB
45HANDLE_GUID_MAP mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];\r
46UINTN mCachePairCount = 0;\r
d042c6e8 47\r
2001f84e
DB
48UINT32 mPerformanceLength = 0;\r
49UINT32 mMaxPerformanceLength = 0;\r
6b4d58a1 50UINT32 mLoadImageCount = 0;\r
2001f84e
DB
51BOOLEAN mFpdtDataIsReported = FALSE;\r
52BOOLEAN mLackSpaceIsReport = FALSE;\r
53CHAR8 *mPlatformLanguage = NULL;\r
54SPIN_LOCK mSmmFpdtLock;\r
55PERFORMANCE_PROPERTY mPerformanceProperty;\r
6b4d58a1 56UINT32 mCachedLength = 0;\r
d042c6e8 57\r
d042c6e8 58//\r
137fb13d 59// Interfaces for SMM PerformanceMeasurement Protocol.\r
d042c6e8 60//\r
137fb13d
BD
61EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL mPerformanceMeasurementInterface = {\r
62 CreatePerformanceMeasurement,\r
f0da4d7d
SZ
63};\r
64\r
6b4d58a1
BD
65/**\r
66 Return the pointer to the FPDT record in the allocated memory.\r
67\r
68 @param RecordSize The size of FPDT record.\r
69 @param FpdtRecordPtr Pointer the FPDT record in the allocated memory.\r
70\r
71 @retval EFI_SUCCESS Successfully get the pointer to the FPDT record.\r
72 @retval EFI_OUT_OF_RESOURCES Ran out of space to store the records.\r
73**/\r
74EFI_STATUS\r
75GetFpdtRecordPtr (\r
76 IN UINT8 RecordSize,\r
77 IN OUT FPDT_RECORD_PTR *FpdtRecordPtr\r
78)\r
79{\r
80 if (mFpdtDataIsReported) {\r
81 //\r
82 // Append Boot records after Smm boot performance records have been reported.\r
83 //\r
84 if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {\r
85 if (!mLackSpaceIsReport) {\r
86 DEBUG ((DEBUG_INFO, "SmmCorePerformanceLib: No enough space to save boot records\n"));\r
87 mLackSpaceIsReport = TRUE;\r
88 }\r
89 return EFI_OUT_OF_RESOURCES;\r
90 } else {\r
91 //\r
92 // Covert buffer to FPDT Ptr Union type.\r
93 //\r
94 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mSmmBootPerformanceTable->Header.Length);\r
95 }\r
96 } else {\r
97 //\r
98 // Check if pre-allocated buffer is full\r
99 //\r
100 if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {\r
101 mSmmBootPerformanceTable = ReallocatePool (\r
102 mPerformanceLength,\r
103 mPerformanceLength + sizeof (SMM_BOOT_PERFORMANCE_TABLE) + RecordSize + FIRMWARE_RECORD_BUFFER,\r
104 mSmmBootPerformanceTable\r
105 );\r
106\r
107 if (mSmmBootPerformanceTable == NULL) {\r
108 return EFI_OUT_OF_RESOURCES;\r
109 }\r
110 mSmmBootPerformanceTable->Header.Length = sizeof (SMM_BOOT_PERFORMANCE_TABLE) + mPerformanceLength;\r
111 mMaxPerformanceLength = mPerformanceLength + sizeof (SMM_BOOT_PERFORMANCE_TABLE) + RecordSize + FIRMWARE_RECORD_BUFFER;\r
112 }\r
113 //\r
114 // Covert buffer to FPDT Ptr Union type.\r
115 //\r
116 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mSmmBootPerformanceTable->Header.Length);\r
117 }\r
118 FpdtRecordPtr->RecordHeader->Length = 0;\r
119 return EFI_SUCCESS;\r
120}\r
121\r
122\r
2001f84e
DB
123/**\r
124Check whether the Token is a known one which is uesed by core.\r
125\r
126@param Token Pointer to a Null-terminated ASCII string\r
127\r
128@retval TRUE Is a known one used by core.\r
129@retval FALSE Not a known one.\r
130\r
131**/\r
132BOOLEAN\r
133IsKnownTokens (\r
134 IN CONST CHAR8 *Token\r
135 )\r
136{\r
c9faac27
DB
137 if (Token == NULL) {\r
138 return FALSE;\r
139 }\r
140\r
2001f84e
DB
141 if (AsciiStrCmp (Token, SEC_TOK) == 0 ||\r
142 AsciiStrCmp (Token, PEI_TOK) == 0 ||\r
143 AsciiStrCmp (Token, DXE_TOK) == 0 ||\r
144 AsciiStrCmp (Token, BDS_TOK) == 0 ||\r
145 AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0 ||\r
146 AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0 ||\r
147 AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0 ||\r
148 AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0 ||\r
149 AsciiStrCmp (Token, START_IMAGE_TOK) == 0 ||\r
150 AsciiStrCmp (Token, PEIM_TOK) == 0) {\r
151 return TRUE;\r
152 } else {\r
153 return FALSE;\r
154 }\r
155}\r
cfb0aba7 156\r
d042c6e8 157/**\r
2001f84e 158Check whether the ID is a known one which map to the known Token.\r
d042c6e8 159\r
2001f84e 160@param Identifier 32-bit identifier.\r
d042c6e8 161\r
2001f84e
DB
162@retval TRUE Is a known one used by core.\r
163@retval FALSE Not a known one.\r
164\r
165**/\r
166BOOLEAN\r
167IsKnownID (\r
168 IN UINT32 Identifier\r
169 )\r
170{\r
171 if (Identifier == MODULE_START_ID ||\r
172 Identifier == MODULE_END_ID ||\r
173 Identifier == MODULE_LOADIMAGE_START_ID ||\r
174 Identifier == MODULE_LOADIMAGE_END_ID ||\r
175 Identifier == MODULE_DB_START_ID ||\r
176 Identifier == MODULE_DB_END_ID ||\r
177 Identifier == MODULE_DB_SUPPORT_START_ID ||\r
178 Identifier == MODULE_DB_SUPPORT_END_ID ||\r
179 Identifier == MODULE_DB_STOP_START_ID ||\r
180 Identifier == MODULE_DB_STOP_END_ID) {\r
181 return TRUE;\r
182 } else {\r
183 return FALSE;\r
184 }\r
185}\r
186\r
187/**\r
6b4d58a1 188 Get the FPDT record identifier.\r
2001f84e 189\r
6b4d58a1
BD
190 @param Attribute The attribute of the Record.\r
191 PerfStartEntry: Start Record.\r
192 PerfEndEntry: End Record.\r
193 @param Handle Pointer to environment specific context used to identify the component being measured.\r
194 @param String Pointer to a Null-terminated ASCII string that identifies the component being measured.\r
195 @param ProgressID On return, pointer to the ProgressID.\r
d042c6e8 196\r
6b4d58a1
BD
197 @retval EFI_SUCCESS Get record info successfully.\r
198 @retval EFI_INVALID_PARAMETER No matched FPDT record.\r
d042c6e8 199\r
200**/\r
2001f84e 201EFI_STATUS\r
6b4d58a1
BD
202GetFpdtRecordId (\r
203 IN PERF_MEASUREMENT_ATTRIBUTE Attribute,\r
204 IN CONST VOID *Handle,\r
205 IN CONST CHAR8 *String,\r
206 OUT UINT16 *ProgressID\r
d042c6e8 207 )\r
208{\r
2001f84e 209 //\r
6b4d58a1 210 // Token to Id.\r
2001f84e 211 //\r
6b4d58a1
BD
212 if (String != NULL) {\r
213 if (AsciiStrCmp (String, START_IMAGE_TOK) == 0) { // "StartImage:"\r
214 if (Attribute == PerfStartEntry) {\r
215 *ProgressID = MODULE_START_ID;\r
2001f84e 216 } else {\r
6b4d58a1 217 *ProgressID = MODULE_END_ID;\r
2001f84e 218 }\r
6b4d58a1
BD
219 } else if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) { // "LoadImage:"\r
220 if (Attribute == PerfStartEntry) {\r
221 *ProgressID = MODULE_LOADIMAGE_START_ID;\r
2001f84e 222 } else {\r
6b4d58a1 223 *ProgressID = MODULE_LOADIMAGE_END_ID;\r
2001f84e
DB
224 }\r
225 } else { // Pref used in Modules\r
6b4d58a1
BD
226 if (Attribute == PerfStartEntry) {\r
227 *ProgressID = PERF_INMODULE_START_ID;\r
2001f84e 228 } else {\r
6b4d58a1 229 *ProgressID = PERF_INMODULE_END_ID;\r
2001f84e
DB
230 }\r
231 }\r
6b4d58a1
BD
232 } else if (Handle != NULL) { // Pref used in Modules\r
233 if (Attribute == PerfStartEntry) {\r
234 *ProgressID = PERF_INMODULE_START_ID;\r
2001f84e 235 } else {\r
6b4d58a1 236 *ProgressID = PERF_INMODULE_END_ID;\r
2001f84e
DB
237 }\r
238 } else {\r
239 return EFI_UNSUPPORTED;\r
d042c6e8 240 }\r
2001f84e 241 return EFI_SUCCESS;\r
d042c6e8 242}\r
243\r
244/**\r
2001f84e
DB
245 Get a human readable module name and module guid for the given image handle.\r
246 If module name can't be found, "" string will return.\r
247 If module guid can't be found, Zero Guid will return.\r
248\r
249 @param Handle Image handle or Controller handle.\r
250 @param NameString The ascii string will be filled into it. If not found, null string will return.\r
251 @param BufferSize Size of the input NameString buffer.\r
252 @param ModuleGuid Point to the guid buffer to store the got module guid value.\r
253\r
254 @retval EFI_SUCCESS Successfully get module name and guid.\r
255 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.\r
256 @retval other value Module Name can't be got.\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260GetModuleInfoFromHandle (\r
261 IN EFI_HANDLE Handle,\r
262 OUT CHAR8 *NameString,\r
263 IN UINTN BufferSize,\r
264 OUT EFI_GUID *ModuleGuid OPTIONAL\r
265 )\r
266{\r
267 EFI_STATUS Status;\r
268 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
269 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
270 CHAR8 *PdbFileName;\r
271 EFI_GUID *TempGuid;\r
272 UINTN StartIndex;\r
273 UINTN Index;\r
274 INTN Count;\r
275 BOOLEAN ModuleGuidIsGet;\r
276 UINTN StringSize;\r
277 CHAR16 *StringPtr;\r
278 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;\r
279\r
280 if (NameString == NULL || BufferSize == 0) {\r
281 return EFI_INVALID_PARAMETER;\r
282 }\r
d042c6e8 283\r
2001f84e
DB
284 //\r
285 // Try to get the ModuleGuid and name string form the caached array.\r
286 //\r
287 if (mCachePairCount > 0) {\r
288 for (Count = mCachePairCount - 1; Count >= 0; Count--) {\r
289 if (Handle == mCacheHandleGuidTable[Count].Handle) {\r
290 CopyGuid (ModuleGuid, &mCacheHandleGuidTable[Count].ModuleGuid);\r
291 AsciiStrCpyS (NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, mCacheHandleGuidTable[Count].NameString);\r
292 return EFI_SUCCESS;\r
293 }\r
294 }\r
295 }\r
296\r
297 Status = EFI_INVALID_PARAMETER;\r
298 LoadedImage = NULL;\r
299 ModuleGuidIsGet = FALSE;\r
d042c6e8 300\r
2001f84e
DB
301 //\r
302 // Initialize GUID as zero value.\r
303 //\r
304 TempGuid = &gZeroGuid;\r
305 //\r
306 // Initialize it as "" string.\r
307 //\r
308 NameString[0] = 0;\r
309\r
310 if (Handle != NULL) {\r
311 //\r
312 // Try Handle as ImageHandle.\r
313 //\r
314 Status = gBS->HandleProtocol (\r
315 Handle,\r
316 &gEfiLoadedImageProtocolGuid,\r
317 (VOID**) &LoadedImage\r
318 );\r
319\r
320 if (EFI_ERROR (Status)) {\r
321 //\r
322 // Try Handle as Controller Handle\r
323 //\r
324 Status = gBS->OpenProtocol (\r
325 Handle,\r
326 &gEfiDriverBindingProtocolGuid,\r
327 (VOID **) &DriverBinding,\r
328 NULL,\r
329 NULL,\r
330 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
331 );\r
332 if (!EFI_ERROR (Status)) {\r
333 //\r
334 // Get Image protocol from ImageHandle\r
335 //\r
336 Status = gBS->HandleProtocol (\r
337 DriverBinding->ImageHandle,\r
338 &gEfiLoadedImageProtocolGuid,\r
339 (VOID**) &LoadedImage\r
340 );\r
341 }\r
342 }\r
343 }\r
344\r
345 if (!EFI_ERROR (Status) && LoadedImage != NULL) {\r
346 //\r
347 // Get Module Guid from DevicePath.\r
348 //\r
349 if (LoadedImage->FilePath != NULL &&\r
350 LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH &&\r
351 LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP\r
352 ) {\r
353 //\r
354 // Determine GUID associated with module logging performance\r
355 //\r
356 ModuleGuidIsGet = TRUE;\r
357 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LoadedImage->FilePath;\r
358 TempGuid = &FvFilePath->FvFileName;\r
359 }\r
360\r
361 //\r
362 // Method 1 Get Module Name from PDB string.\r
363 //\r
364 PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);\r
365 if (PdbFileName != NULL && BufferSize > 0) {\r
366 StartIndex = 0;\r
367 for (Index = 0; PdbFileName[Index] != 0; Index++) {\r
368 if ((PdbFileName[Index] == '\\') || (PdbFileName[Index] == '/')) {\r
369 StartIndex = Index + 1;\r
370 }\r
371 }\r
372 //\r
373 // Copy the PDB file name to our temporary string.\r
374 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.\r
375 //\r
376 for (Index = 0; Index < BufferSize - 1; Index++) {\r
377 NameString[Index] = PdbFileName[Index + StartIndex];\r
378 if (NameString[Index] == 0 || NameString[Index] == '.') {\r
379 NameString[Index] = 0;\r
380 break;\r
381 }\r
382 }\r
383\r
384 if (Index == BufferSize - 1) {\r
385 NameString[Index] = 0;\r
386 }\r
387 //\r
388 // Module Name is got.\r
389 //\r
390 goto Done;\r
391 }\r
392 }\r
393\r
394 if (ModuleGuidIsGet) {\r
395 //\r
396 // Method 2 Try to get the image's FFS UI section by image GUID\r
397 //\r
398 StringPtr = NULL;\r
399 StringSize = 0;\r
400 Status = GetSectionFromAnyFv (\r
401 TempGuid,\r
402 EFI_SECTION_USER_INTERFACE,\r
403 0,\r
404 (VOID **) &StringPtr,\r
405 &StringSize\r
406 );\r
407\r
408 if (!EFI_ERROR (Status)) {\r
409 //\r
410 // Method 3. Get the name string from FFS UI section\r
411 //\r
412 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {\r
413 NameString[Index] = (CHAR8) StringPtr[Index];\r
414 }\r
415 NameString[Index] = 0;\r
416 FreePool (StringPtr);\r
417 }\r
418 }\r
419\r
420Done:\r
421 //\r
422 // Copy Module Guid\r
423 //\r
424 if (ModuleGuid != NULL) {\r
425 CopyGuid (ModuleGuid, TempGuid);\r
426 if (IsZeroGuid(TempGuid) && (Handle != NULL) && !ModuleGuidIsGet) {\r
427 // Handle is GUID\r
428 CopyGuid (ModuleGuid, (EFI_GUID *) Handle);\r
429 }\r
430 }\r
431\r
432 //\r
433 // Cache the Handle and Guid pairs.\r
434 //\r
435 if (mCachePairCount < CACHE_HANDLE_GUID_COUNT) {\r
436 mCacheHandleGuidTable[mCachePairCount].Handle = Handle;\r
437 CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, ModuleGuid);\r
438 AsciiStrCpyS (mCacheHandleGuidTable[mCachePairCount].NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, NameString);\r
439 mCachePairCount ++;\r
440 }\r
441\r
442 return Status;\r
443}\r
444\r
445/**\r
6b4d58a1
BD
446 Copies the string from Source into Destination and updates Length with the\r
447 size of the string.\r
2001f84e 448\r
6b4d58a1
BD
449 @param Destination - destination of the string copy\r
450 @param Source - pointer to the source string which will get copied\r
451 @param Length - pointer to a length variable to be updated\r
452\r
453**/\r
454VOID\r
455CopyStringIntoPerfRecordAndUpdateLength (\r
456 IN OUT CHAR8 *Destination,\r
457 IN CONST CHAR8 *Source,\r
458 IN OUT UINT8 *Length\r
459 )\r
460{\r
461 UINTN StringLen;\r
462 UINTN DestMax;\r
463\r
464 ASSERT (Source != NULL);\r
465\r
466 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
467 DestMax = STRING_SIZE;\r
468 } else {\r
469 DestMax = AsciiStrSize (Source);\r
470 if (DestMax > STRING_SIZE) {\r
471 DestMax = STRING_SIZE;\r
472 }\r
473 }\r
474 StringLen = AsciiStrLen (Source);\r
475 if (StringLen >= DestMax) {\r
476 StringLen = DestMax -1;\r
477 }\r
d042c6e8 478\r
6b4d58a1
BD
479 AsciiStrnCpyS(Destination, DestMax, Source, StringLen);\r
480 *Length += (UINT8)DestMax;\r
481\r
482 return;\r
483}\r
484\r
485/**\r
486 Create performance record with event description and a timestamp.\r
487\r
488 @param CallerIdentifier - Image handle or pointer to caller ID GUID.\r
489 @param Guid - Pointer to a GUID.\r
490 @param String - Pointer to a string describing the measurement.\r
491 @param Ticker - 64-bit time stamp.\r
492 @param Address - Pointer to a location in memory relevant to the measurement.\r
493 @param PerfId - Performance identifier describing the type of measurement.\r
494 @param Attribute - The attribute of the measurement. According to attribute can create a start\r
495 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
496 or a general record for other Perf macros.\r
497\r
498 @retval EFI_SUCCESS - Successfully created performance record.\r
499 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.\r
500 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
501 pointer or invalid PerfId.\r
502\r
503 @retval EFI_SUCCESS - Successfully created performance record\r
504 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records\r
505 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
506 pointer or invalid PerfId\r
d042c6e8 507\r
508**/\r
509EFI_STATUS\r
6b4d58a1
BD
510InsertFpdtRecord (\r
511 IN CONST VOID *CallerIdentifier, OPTIONAL\r
512 IN CONST VOID *Guid, OPTIONAL\r
513 IN CONST CHAR8 *String, OPTIONAL\r
514 IN UINT64 Ticker,\r
515 IN UINT64 Address, OPTIONAL\r
516 IN UINT16 PerfId,\r
517 IN PERF_MEASUREMENT_ATTRIBUTE Attribute\r
d042c6e8 518 )\r
6b4d58a1 519\r
d042c6e8 520{\r
6b4d58a1 521 EFI_STATUS Status;\r
2001f84e
DB
522 EFI_GUID ModuleGuid;\r
523 CHAR8 ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
2001f84e 524 FPDT_RECORD_PTR FpdtRecordPtr;\r
6b4d58a1 525 FPDT_RECORD_PTR CachedFpdtRecordPtr;\r
2001f84e 526 UINT64 TimeStamp;\r
2001f84e 527 CONST CHAR8 *StringPtr;\r
6b4d58a1
BD
528 UINTN DestMax;\r
529 UINTN StringLen;\r
530 UINT16 ProgressId;\r
2001f84e
DB
531\r
532 StringPtr = NULL;\r
2001f84e 533 ZeroMem (ModuleName, sizeof (ModuleName));\r
d042c6e8 534\r
2001f84e 535 //\r
6b4d58a1
BD
536 // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.\r
537 // notes: For other Perf macros (Attribute == PerfEntry), their Id is known.\r
2001f84e 538 //\r
6b4d58a1 539 if (Attribute != PerfEntry) {\r
d042c6e8 540 //\r
6b4d58a1
BD
541 // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.\r
542 // !!! Note: If the Perf is not the known Token used in the core but have same\r
543 // ID with the core Token, this case will not be supported.\r
544 // And in currtnt usage mode, for the unkown ID, there is a general rule:\r
545 // If it is start pref: the lower 4 bits of the ID should be 0.\r
546 // If it is end pref: the lower 4 bits of the ID should not be 0.\r
547 // If input ID doesn't follow the rule, we will adjust it.\r
d042c6e8 548 //\r
6b4d58a1
BD
549 if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
550 return EFI_INVALID_PARAMETER;\r
551 } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
552 if ((Attribute == PerfStartEntry) && ((PerfId & 0x000F) != 0)) {\r
553 PerfId &= 0xFFF0;\r
554 } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {\r
555 PerfId += 1;\r
2001f84e 556 }\r
6b4d58a1
BD
557 }\r
558 if (PerfId == 0) {\r
2001f84e 559 //\r
6b4d58a1 560 // Get ProgressID form the String Token.\r
2001f84e 561 //\r
6b4d58a1
BD
562 Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &ProgressId);\r
563 if (EFI_ERROR (Status)) {\r
564 return Status;\r
2001f84e 565 }\r
6b4d58a1 566 PerfId = ProgressId;\r
d042c6e8 567 }\r
2001f84e 568 }\r
d042c6e8 569\r
2001f84e 570 //\r
6b4d58a1
BD
571 // 2. Get the buffer to store the FPDT record.\r
572 //\r
573 Status = GetFpdtRecordPtr (FPDT_MAX_PERF_RECORD_SIZE, &FpdtRecordPtr);\r
574 if (EFI_ERROR (Status)) {\r
575 return Status;\r
576 }\r
577\r
578 //\r
579 // 3. Get the TimeStamp.\r
2001f84e
DB
580 //\r
581 if (Ticker == 0) {\r
582 Ticker = GetPerformanceCounter ();\r
583 TimeStamp = GetTimeInNanoSecond (Ticker);\r
584 } else if (Ticker == 1) {\r
585 TimeStamp = 0;\r
586 } else {\r
587 TimeStamp = GetTimeInNanoSecond (Ticker);\r
d042c6e8 588 }\r
589\r
2001f84e 590 //\r
6b4d58a1 591 // 4. Fill in the FPDT record according to different Performance Identifier.\r
2001f84e 592 //\r
6b4d58a1
BD
593 switch (PerfId) {\r
594 case MODULE_START_ID:\r
595 case MODULE_END_ID:\r
596 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
597 StringPtr = ModuleName;\r
598 //\r
599 // Cache the offset of start image start record and use to update the start image end record if needed.\r
600 //\r
601 if (PerfId == MODULE_START_ID && Attribute == PerfEntry) {\r
602 mCachedLength = mSmmBootPerformanceTable->Header.Length;\r
603 }\r
604 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
605 FpdtRecordPtr.GuidEvent->Header.Type = FPDT_GUID_EVENT_TYPE;\r
606 FpdtRecordPtr.GuidEvent->Header.Length = sizeof (FPDT_GUID_EVENT_RECORD);\r
607 FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
608 FpdtRecordPtr.GuidEvent->ProgressID = PerfId;\r
609 FpdtRecordPtr.GuidEvent->Timestamp = TimeStamp;\r
610 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
611 if (CallerIdentifier == NULL && PerfId == MODULE_END_ID && mCachedLength != 0) {\r
612 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mCachedLength);\r
613 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &CachedFpdtRecordPtr.GuidEvent->Guid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
614 mCachedLength = 0;\r
615 }\r
616 }\r
617 break;\r
d042c6e8 618\r
6b4d58a1
BD
619 case MODULE_LOADIMAGE_START_ID:\r
620 case MODULE_LOADIMAGE_END_ID:\r
621 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
622 StringPtr = ModuleName;\r
623 if (PerfId == MODULE_LOADIMAGE_START_ID) {\r
624 mLoadImageCount++;\r
625 //\r
626 // Cache the offset of load image start record and use to be updated by the load image end record if needed.\r
627 //\r
628 if (CallerIdentifier == NULL && Attribute == PerfEntry) {\r
629 mCachedLength = mSmmBootPerformanceTable->Header.Length;\r
630 }\r
631 }\r
632 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
633 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;\r
634 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
635 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
636 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;\r
637 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;\r
638 FpdtRecordPtr.GuidQwordEvent->Qword = mLoadImageCount;\r
639 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));\r
640 if (PerfId == MODULE_LOADIMAGE_END_ID && mCachedLength != 0) {\r
641 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mCachedLength);\r
642 CopyMem (&CachedFpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.GuidQwordEvent->Guid));\r
643 mCachedLength = 0;\r
644 }\r
645 }\r
2001f84e
DB
646 break;\r
647\r
6b4d58a1
BD
648 case PERF_EVENTSIGNAL_START_ID:\r
649 case PERF_EVENTSIGNAL_END_ID:\r
650 case PERF_CALLBACK_START_ID:\r
651 case PERF_CALLBACK_END_ID:\r
652 if (String == NULL) {\r
653 return EFI_INVALID_PARAMETER;\r
654 }\r
655 //\r
656 // Cache the event guid in string event record when PcdEdkiiFpdtStringRecordEnableOnly == TRUE\r
657 //\r
658 CopyGuid (&ModuleGuid, Guid);\r
659 StringPtr = String;\r
660 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
661 FpdtRecordPtr.DualGuidStringEvent->Header.Type = FPDT_DUAL_GUID_STRING_EVENT_TYPE;\r
662 FpdtRecordPtr.DualGuidStringEvent->Header.Length = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);\r
663 FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
664 FpdtRecordPtr.DualGuidStringEvent->ProgressID = PerfId;\r
665 FpdtRecordPtr.DualGuidStringEvent->Timestamp = TimeStamp;\r
666 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, CallerIdentifier, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));\r
667 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));\r
668 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);\r
669 }\r
670 break;\r
2001f84e 671\r
6b4d58a1
BD
672 case PERF_EVENT_ID:\r
673 case PERF_FUNCTION_START_ID:\r
674 case PERF_FUNCTION_END_ID:\r
675 case PERF_INMODULE_START_ID:\r
676 case PERF_INMODULE_END_ID:\r
677 case PERF_CROSSMODULE_START_ID:\r
678 case PERF_CROSSMODULE_END_ID:\r
679 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
680 if (String != NULL) {\r
681 StringPtr = String;\r
682 } else {\r
683 StringPtr = ModuleName;\r
684 }\r
685 if (AsciiStrLen (StringPtr) == 0) {\r
686 StringPtr = "unknown name";\r
2001f84e 687 }\r
6b4d58a1
BD
688 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
689 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
690 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
691 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
692 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
693 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
694 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
695 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
696 }\r
697 break;\r
698\r
699 default:\r
700 if (Attribute != PerfEntry) {\r
701 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
702 if (String != NULL) {\r
703 StringPtr = String;\r
704 } else {\r
705 StringPtr = ModuleName;\r
706 }\r
707 if (AsciiStrLen (StringPtr) == 0) {\r
708 StringPtr = "unknown name";\r
709 }\r
710 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
711 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
712 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
713 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
714 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
715 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
716 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
717 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
2001f84e 718 }\r
2001f84e 719 } else {\r
6b4d58a1 720 return EFI_INVALID_PARAMETER;\r
2001f84e
DB
721 }\r
722 break;\r
6b4d58a1 723 }\r
2001f84e 724\r
6b4d58a1
BD
725 //\r
726 // 4.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.\r
727 //\r
728 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
729 if (StringPtr == NULL) {\r
730 return EFI_INVALID_PARAMETER;\r
731 }\r
732 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
733 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
734 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
735 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
736 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
737 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
738 if (AsciiStrLen (StringPtr) == 0) {\r
739 StringPtr = "unknown name";\r
740 }\r
741 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
2001f84e 742\r
6b4d58a1
BD
743 if ((PerfId == MODULE_LOADIMAGE_START_ID) || (PerfId == MODULE_END_ID)) {\r
744 FpdtRecordPtr.DynamicStringEvent->Header.Length = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)+ STRING_SIZE);\r
745 }\r
746 if ((PerfId == MODULE_LOADIMAGE_END_ID || PerfId == MODULE_END_ID) && mCachedLength != 0) {\r
747 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mCachedLength);\r
748 if (PerfId == MODULE_LOADIMAGE_END_ID) {\r
749 DestMax = CachedFpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
750 StringLen = AsciiStrLen (StringPtr);\r
751 if (StringLen >= DestMax) {\r
752 StringLen = DestMax -1;\r
753 }\r
754 CopyMem (&CachedFpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
755 AsciiStrnCpyS (CachedFpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StringLen);\r
756 } else if (PerfId == MODULE_END_ID) {\r
757 DestMax = FpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
758 StringLen = AsciiStrLen (CachedFpdtRecordPtr.DynamicStringEvent->String);\r
759 if (StringLen >= DestMax) {\r
760 StringLen = DestMax -1;\r
761 }\r
762 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &CachedFpdtRecordPtr.DynamicStringEvent->Guid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
763 AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, CachedFpdtRecordPtr.DynamicStringEvent->String, StringLen);\r
764 }\r
765 mCachedLength = 0;\r
766 }\r
d042c6e8 767 }\r
768\r
2001f84e 769 //\r
6b4d58a1 770 // 5. Update the length of the used buffer after fill in the record.\r
2001f84e
DB
771 //\r
772 mPerformanceLength += FpdtRecordPtr.RecordHeader->Length;\r
773 mSmmBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;\r
f0da4d7d 774\r
2001f84e
DB
775 return EFI_SUCCESS;\r
776}\r
d042c6e8 777\r
2001f84e
DB
778/**\r
779 SmmReadyToBoot protocol notification event handler.\r
f0da4d7d 780\r
2001f84e
DB
781 @param Protocol Points to the protocol's unique identifier\r
782 @param Interface Points to the interface instance\r
783 @param Handle The handle on which the interface was installed\r
ccd2f6b0 784\r
2001f84e 785 @retval EFI_SUCCESS SmmReadyToBootCallback runs successfully\r
f0da4d7d 786\r
d042c6e8 787**/\r
788EFI_STATUS\r
789EFIAPI\r
2001f84e
DB
790SmmReportFpdtRecordData (\r
791 IN CONST EFI_GUID *Protocol,\r
792 IN VOID *Interface,\r
793 IN EFI_HANDLE Handle\r
d042c6e8 794 )\r
795{\r
2001f84e
DB
796 UINT64 SmmBPDTddr;\r
797\r
798 if (!mFpdtDataIsReported && mSmmBootPerformanceTable != NULL) {\r
799 SmmBPDTddr = (UINT64)(UINTN)mSmmBootPerformanceTable;\r
800 REPORT_STATUS_CODE_EX (\r
801 EFI_PROGRESS_CODE,\r
802 EFI_SOFTWARE_SMM_DRIVER,\r
803 0,\r
804 NULL,\r
805 &gEdkiiFpdtExtendedFirmwarePerformanceGuid,\r
806 &SmmBPDTddr,\r
807 sizeof (UINT64)\r
808 );\r
809 //\r
810 // Set FPDT report state to TRUE.\r
811 //\r
812 mFpdtDataIsReported = TRUE;\r
f0da4d7d 813 }\r
d042c6e8 814 return EFI_SUCCESS;\r
815}\r
816\r
817/**\r
2001f84e
DB
818 SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized\r
819 this function is callbacked to initialize the Smm Performance Lib\r
d042c6e8 820\r
821 @param Event The event of notify protocol.\r
822 @param Context Notify event context.\r
823\r
824**/\r
825VOID\r
826EFIAPI\r
827InitializeSmmCorePerformanceLib (\r
828 IN EFI_EVENT Event,\r
829 IN VOID *Context\r
830 )\r
831{\r
d042c6e8 832 EFI_HANDLE Handle;\r
2001f84e
DB
833 EFI_STATUS Status;\r
834 VOID *SmmReadyToBootRegistration;\r
cfb0aba7 835 PERFORMANCE_PROPERTY *PerformanceProperty;\r
d042c6e8 836\r
837 //\r
838 // Initialize spin lock\r
839 //\r
2001f84e 840 InitializeSpinLock (&mSmmFpdtLock);\r
d042c6e8 841\r
d042c6e8 842 //\r
2001f84e 843 // Install the protocol interfaces for SMM performance library instance.\r
d042c6e8 844 //\r
2001f84e 845 Handle = NULL;\r
d042c6e8 846 Status = gSmst->SmmInstallProtocolInterface (\r
2001f84e 847 &Handle,\r
137fb13d 848 &gEdkiiSmmPerformanceMeasurementProtocolGuid,\r
f0da4d7d 849 EFI_NATIVE_INTERFACE,\r
137fb13d 850 &mPerformanceMeasurementInterface\r
f0da4d7d
SZ
851 );\r
852 ASSERT_EFI_ERROR (Status);\r
853\r
2001f84e
DB
854 Status = gSmst->SmmRegisterProtocolNotify (\r
855 &gEdkiiSmmReadyToBootProtocolGuid,\r
856 SmmReportFpdtRecordData,\r
857 &SmmReadyToBootRegistration\r
858 );\r
9802d6d5 859 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **) &PerformanceProperty);\r
cfb0aba7
SZ
860 if (EFI_ERROR (Status)) {\r
861 //\r
862 // Install configuration table for performance property.\r
863 //\r
864 mPerformanceProperty.Revision = PERFORMANCE_PROPERTY_REVISION;\r
865 mPerformanceProperty.Reserved = 0;\r
866 mPerformanceProperty.Frequency = GetPerformanceCounterProperties (\r
867 &mPerformanceProperty.TimerStartValue,\r
868 &mPerformanceProperty.TimerEndValue\r
869 );\r
870 Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);\r
871 ASSERT_EFI_ERROR (Status);\r
872 }\r
d042c6e8 873}\r
874\r
875/**\r
2001f84e 876 The constructor function initializes the Performance Measurement Enable flag and\r
f0da4d7d 877 registers SmmBase2 protocol notify callback.\r
d042c6e8 878 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
879\r
880 @param ImageHandle The firmware allocated handle for the EFI image.\r
881 @param SystemTable A pointer to the EFI System Table.\r
882\r
883 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
884\r
885**/\r
886EFI_STATUS\r
887EFIAPI\r
888SmmCorePerformanceLibConstructor (\r
889 IN EFI_HANDLE ImageHandle,\r
890 IN EFI_SYSTEM_TABLE *SystemTable\r
891 )\r
892{\r
893 EFI_STATUS Status;\r
894 EFI_EVENT Event;\r
895 VOID *Registration;\r
896\r
2001f84e 897 if (!PerformanceMeasurementEnabled ()) {\r
d042c6e8 898 //\r
899 // Do not initialize performance infrastructure if not required.\r
900 //\r
901 return EFI_SUCCESS;\r
902 }\r
903\r
904 //\r
905 // Create the events to do the library init.\r
906 //\r
907 Status = gBS->CreateEvent (\r
908 EVT_NOTIFY_SIGNAL,\r
909 TPL_CALLBACK,\r
910 InitializeSmmCorePerformanceLib,\r
911 NULL,\r
912 &Event\r
913 );\r
914 ASSERT_EFI_ERROR (Status);\r
915\r
916 //\r
917 // Register for protocol notifications on this event\r
918 //\r
919 Status = gBS->RegisterProtocolNotify (\r
920 &gEfiSmmBase2ProtocolGuid,\r
921 Event,\r
922 &Registration\r
923 );\r
924\r
925 ASSERT_EFI_ERROR (Status);\r
926\r
927 return EFI_SUCCESS;\r
928}\r
929\r
137fb13d
BD
930/**\r
931 Create performance record with event description and a timestamp.\r
932\r
933 @param CallerIdentifier - Image handle or pointer to caller ID GUID.\r
934 @param Guid - Pointer to a GUID.\r
935 @param String - Pointer to a string describing the measurement.\r
936 @param TimeStamp - 64-bit time stamp.\r
937 @param Address - Pointer to a location in memory relevant to the measurement.\r
938 @param Identifier - Performance identifier describing the type of measurement.\r
939 @param Attribute - The attribute of the measurement. According to attribute can create a start\r
940 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
941 or a general record for other Perf macros.\r
942\r
943 @retval EFI_SUCCESS - Successfully created performance record.\r
944 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.\r
945 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
946 pointer or invalid PerfId.\r
947**/\r
948EFI_STATUS\r
949EFIAPI\r
950CreatePerformanceMeasurement(\r
951 IN CONST VOID *CallerIdentifier, OPTIONAL\r
952 IN CONST VOID *Guid, OPTIONAL\r
953 IN CONST CHAR8 *String, OPTIONAL\r
954 IN UINT64 TimeStamp, OPTIONAL\r
955 IN UINT64 Address, OPTIONAL\r
956 IN UINT32 Identifier,\r
957 IN PERF_MEASUREMENT_ATTRIBUTE Attribute\r
958 )\r
959{\r
960 EFI_STATUS Status;\r
961\r
6b4d58a1
BD
962 Status = EFI_SUCCESS;\r
963\r
137fb13d 964 AcquireSpinLock (&mSmmFpdtLock);\r
6b4d58a1 965 Status = InsertFpdtRecord (CallerIdentifier, Guid, String, TimeStamp, Address, (UINT16)Identifier, Attribute);\r
137fb13d
BD
966 ReleaseSpinLock (&mSmmFpdtLock);\r
967 return Status;\r
968}\r
969\r
d042c6e8 970/**\r
971 Adds a record at the end of the performance measurement log\r
972 that records the start time of a performance measurement.\r
973\r
974 Adds a record to the end of the performance measurement log\r
f0da4d7d 975 that contains the Handle, Token, Module and Identifier.\r
d042c6e8 976 The end time of the new record must be set to zero.\r
977 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
978 If TimeStamp is zero, the start time in the record is filled in with the value\r
979 read from the current time stamp.\r
980\r
981 @param Handle Pointer to environment specific context used\r
982 to identify the component being measured.\r
983 @param Token Pointer to a Null-terminated ASCII string\r
984 that identifies the component being measured.\r
985 @param Module Pointer to a Null-terminated ASCII string\r
986 that identifies the module being measured.\r
987 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
988 @param Identifier 32-bit identifier. If the value is 0, the created record\r
989 is same as the one created by StartPerformanceMeasurement.\r
d042c6e8 990\r
991 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
992 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
993\r
994**/\r
995RETURN_STATUS\r
996EFIAPI\r
f0da4d7d 997StartPerformanceMeasurementEx (\r
d042c6e8 998 IN CONST VOID *Handle, OPTIONAL\r
999 IN CONST CHAR8 *Token, OPTIONAL\r
1000 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
1001 IN UINT64 TimeStamp,\r
1002 IN UINT32 Identifier\r
d042c6e8 1003 )\r
1004{\r
137fb13d
BD
1005 CONST CHAR8 *String;\r
1006\r
1007 if (Token != NULL) {\r
1008 String = Token;\r
1009 } else if (Module != NULL) {\r
1010 String = Module;\r
1011 } else {\r
1012 String = NULL;\r
1013 }\r
1014\r
1015 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);\r
d042c6e8 1016}\r
1017\r
1018/**\r
1019 Searches the performance measurement log from the beginning of the log\r
1020 for the first matching record that contains a zero end time and fills in a valid end time.\r
1021\r
1022 Searches the performance measurement log from the beginning of the log\r
2001f84e 1023 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.\r
d042c6e8 1024 If the record can not be found then return RETURN_NOT_FOUND.\r
1025 If the record is found and TimeStamp is not zero,\r
1026 then the end time in the record is filled in with the value specified by TimeStamp.\r
1027 If the record is found and TimeStamp is zero, then the end time in the matching record\r
1028 is filled in with the current time stamp value.\r
1029\r
1030 @param Handle Pointer to environment specific context used\r
1031 to identify the component being measured.\r
1032 @param Token Pointer to a Null-terminated ASCII string\r
1033 that identifies the component being measured.\r
1034 @param Module Pointer to a Null-terminated ASCII string\r
1035 that identifies the module being measured.\r
1036 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
1037 @param Identifier 32-bit identifier. If the value is 0, the found record\r
1038 is same as the one found by EndPerformanceMeasurement.\r
d042c6e8 1039\r
1040 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
1041 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
1042\r
1043**/\r
1044RETURN_STATUS\r
1045EFIAPI\r
f0da4d7d 1046EndPerformanceMeasurementEx (\r
d042c6e8 1047 IN CONST VOID *Handle, OPTIONAL\r
1048 IN CONST CHAR8 *Token, OPTIONAL\r
1049 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
1050 IN UINT64 TimeStamp,\r
1051 IN UINT32 Identifier\r
d042c6e8 1052 )\r
1053{\r
137fb13d
BD
1054 CONST CHAR8 *String;\r
1055\r
1056 if (Token != NULL) {\r
1057 String = Token;\r
1058 } else if (Module != NULL) {\r
1059 String = Module;\r
1060 } else {\r
1061 String = NULL;\r
1062 }\r
1063\r
1064 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);\r
d042c6e8 1065}\r
1066\r
1067/**\r
1068 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
1069 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
1070 and then assign the Identifier with 0.\r
d042c6e8 1071\r
2001f84e
DB
1072 !!! Not Support!!!\r
1073\r
d042c6e8 1074 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
1075 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
1076 and the key for the second entry in the log is returned. If the performance log is empty,\r
1077 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
1078 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
1079 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
1080 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
1081 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
1082 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 1083 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
d042c6e8 1084 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
1085 If Handle is NULL, then ASSERT().\r
1086 If Token is NULL, then ASSERT().\r
1087 If Module is NULL, then ASSERT().\r
1088 If StartTimeStamp is NULL, then ASSERT().\r
1089 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 1090 If Identifier is NULL, then ASSERT().\r
d042c6e8 1091\r
1092 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
1093 0, then the first performance measurement log entry is retrieved.\r
1094 On exit, the key of the next performance log entry.\r
1095 @param Handle Pointer to environment specific context used to identify the component\r
1096 being measured.\r
1097 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
1098 being measured.\r
1099 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
1100 being measured.\r
1101 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1102 was started.\r
1103 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1104 was ended.\r
f0da4d7d 1105 @param Identifier Pointer to the 32-bit identifier that was recorded.\r
d042c6e8 1106\r
1107 @return The key for the next performance log entry (in general case).\r
1108\r
1109**/\r
1110UINTN\r
1111EFIAPI\r
f0da4d7d 1112GetPerformanceMeasurementEx (\r
2001f84e 1113 IN UINTN LogEntryKey,\r
d042c6e8 1114 OUT CONST VOID **Handle,\r
1115 OUT CONST CHAR8 **Token,\r
1116 OUT CONST CHAR8 **Module,\r
1117 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
1118 OUT UINT64 *EndTimeStamp,\r
1119 OUT UINT32 *Identifier\r
d042c6e8 1120 )\r
1121{\r
2001f84e 1122 return 0;\r
d042c6e8 1123}\r
1124\r
f0da4d7d
SZ
1125/**\r
1126 Adds a record at the end of the performance measurement log\r
1127 that records the start time of a performance measurement.\r
1128\r
1129 Adds a record to the end of the performance measurement log\r
1130 that contains the Handle, Token, and Module.\r
1131 The end time of the new record must be set to zero.\r
1132 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
1133 If TimeStamp is zero, the start time in the record is filled in with the value\r
1134 read from the current time stamp.\r
1135\r
1136 @param Handle Pointer to environment specific context used\r
1137 to identify the component being measured.\r
1138 @param Token Pointer to a Null-terminated ASCII string\r
1139 that identifies the component being measured.\r
1140 @param Module Pointer to a Null-terminated ASCII string\r
1141 that identifies the module being measured.\r
1142 @param TimeStamp 64-bit time stamp.\r
1143\r
1144 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
1145 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
1146\r
1147**/\r
1148RETURN_STATUS\r
1149EFIAPI\r
1150StartPerformanceMeasurement (\r
1151 IN CONST VOID *Handle, OPTIONAL\r
1152 IN CONST CHAR8 *Token, OPTIONAL\r
1153 IN CONST CHAR8 *Module, OPTIONAL\r
1154 IN UINT64 TimeStamp\r
1155 )\r
1156{\r
137fb13d 1157 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
1158}\r
1159\r
1160/**\r
1161 Searches the performance measurement log from the beginning of the log\r
1162 for the first matching record that contains a zero end time and fills in a valid end time.\r
1163\r
1164 Searches the performance measurement log from the beginning of the log\r
1165 for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
1166 If the record can not be found then return RETURN_NOT_FOUND.\r
1167 If the record is found and TimeStamp is not zero,\r
1168 then the end time in the record is filled in with the value specified by TimeStamp.\r
1169 If the record is found and TimeStamp is zero, then the end time in the matching record\r
1170 is filled in with the current time stamp value.\r
1171\r
1172 @param Handle Pointer to environment specific context used\r
1173 to identify the component being measured.\r
1174 @param Token Pointer to a Null-terminated ASCII string\r
1175 that identifies the component being measured.\r
1176 @param Module Pointer to a Null-terminated ASCII string\r
1177 that identifies the module being measured.\r
1178 @param TimeStamp 64-bit time stamp.\r
1179\r
1180 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
1181 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
1182\r
1183**/\r
1184RETURN_STATUS\r
1185EFIAPI\r
1186EndPerformanceMeasurement (\r
1187 IN CONST VOID *Handle, OPTIONAL\r
1188 IN CONST CHAR8 *Token, OPTIONAL\r
1189 IN CONST CHAR8 *Module, OPTIONAL\r
1190 IN UINT64 TimeStamp\r
1191 )\r
1192{\r
137fb13d 1193 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
1194}\r
1195\r
1196/**\r
1197 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
1198 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
1199 and then eliminate the Identifier.\r
1200\r
2001f84e
DB
1201 !!! Not Support!!!\r
1202\r
f0da4d7d
SZ
1203 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
1204 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
1205 and the key for the second entry in the log is returned. If the performance log is empty,\r
1206 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
1207 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
1208 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
1209 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
1210 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
1211 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
1212 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
1213 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
1214 If Handle is NULL, then ASSERT().\r
1215 If Token is NULL, then ASSERT().\r
1216 If Module is NULL, then ASSERT().\r
1217 If StartTimeStamp is NULL, then ASSERT().\r
1218 If EndTimeStamp is NULL, then ASSERT().\r
1219\r
1220 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
1221 0, then the first performance measurement log entry is retrieved.\r
1222 On exit, the key of the next performance log entry.\r
1223 @param Handle Pointer to environment specific context used to identify the component\r
1224 being measured.\r
1225 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
1226 being measured.\r
1227 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
1228 being measured.\r
1229 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1230 was started.\r
1231 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1232 was ended.\r
1233\r
1234 @return The key for the next performance log entry (in general case).\r
1235\r
1236**/\r
1237UINTN\r
1238EFIAPI\r
1239GetPerformanceMeasurement (\r
1240 IN UINTN LogEntryKey,\r
1241 OUT CONST VOID **Handle,\r
1242 OUT CONST CHAR8 **Token,\r
1243 OUT CONST CHAR8 **Module,\r
1244 OUT UINT64 *StartTimeStamp,\r
1245 OUT UINT64 *EndTimeStamp\r
1246 )\r
1247{\r
2001f84e 1248 return 0;\r
f0da4d7d
SZ
1249}\r
1250\r
d042c6e8 1251/**\r
1252 Returns TRUE if the performance measurement macros are enabled.\r
1253\r
1254 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1255 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
1256\r
1257 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1258 PcdPerformanceLibraryPropertyMask is set.\r
1259 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1260 PcdPerformanceLibraryPropertyMask is clear.\r
1261\r
1262**/\r
1263BOOLEAN\r
1264EFIAPI\r
1265PerformanceMeasurementEnabled (\r
1266 VOID\r
1267 )\r
1268{\r
2001f84e 1269 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
d042c6e8 1270}\r
6b4d58a1
BD
1271\r
1272/**\r
1273 Create performance record with event description and a timestamp.\r
1274\r
1275 @param CallerIdentifier - Image handle or pointer to caller ID GUID\r
1276 @param Guid - Pointer to a GUID\r
1277 @param String - Pointer to a string describing the measurement\r
1278 @param Address - Pointer to a location in memory relevant to the measurement\r
1279 @param Identifier - Performance identifier describing the type of measurement\r
1280\r
1281 @retval RETURN_SUCCESS - Successfully created performance record\r
1282 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records\r
1283 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
1284 pointer or invalid PerfId\r
1285\r
1286**/\r
1287RETURN_STATUS\r
1288EFIAPI\r
1289LogPerformanceMeasurement (\r
1290 IN CONST VOID *CallerIdentifier,\r
1291 IN CONST VOID *Guid, OPTIONAL\r
1292 IN CONST CHAR8 *String, OPTIONAL\r
1293 IN UINT64 Address, OPTIONAL\r
1294 IN UINT32 Identifier\r
1295 )\r
1296{\r
1297 return (RETURN_STATUS)CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);\r
1298}\r
1299\r
1300/**\r
1301 Check whether the specified performance measurement can be logged.\r
1302\r
1303 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
1304 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
1305\r
1306 @param Type - Type of the performance measurement entry.\r
1307\r
1308 @retval TRUE The performance measurement can be logged.\r
1309 @retval FALSE The performance measurement can NOT be logged.\r
1310\r
1311**/\r
1312BOOLEAN\r
1313EFIAPI\r
1314LogPerformanceMeasurementEnabled (\r
1315 IN CONST UINTN Type\r
1316 )\r
1317{\r
1318 //\r
1319 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
1320 //\r
1321 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {\r
1322 return TRUE;\r
1323 }\r
1324 return FALSE;\r
1325}\r
1326\r