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