]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / SmmCorePerformanceLib / SmmCorePerformanceLib.c
index ca953df932ebc3e0c3934b5c61f0dab3b1aa3ef9..3efe56e0565c15b0289d0b6f17c62dc9b459d432 100644 (file)
   This library is mainly used by SMM Core to start performance logging to ensure that\r
   SMM Performance and PerformanceEx Protocol are installed at the very beginning of SMM phase.\r
 \r
-Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
+ Caution: This module requires additional review when modified.\r
+ This driver will have external input - performance data and communicate buffer in SMM mode.\r
+ This external input must be validated carefully to avoid security issue like\r
+ buffer overflow, integer overflow.\r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+ SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted input and do basic validation.\r
 \r
-**/\r
+Copyright (c) 2011 - 2021, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
+**/\r
 \r
 #include "SmmCorePerformanceLibInternal.h"\r
 \r
-//\r
-// The data structure to hold global performance data.\r
-//\r
-GAUGE_DATA_HEADER       *mGaugeData;\r
+#define STRING_SIZE              (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))\r
+#define FIRMWARE_RECORD_BUFFER   0x1000\r
+#define CACHE_HANDLE_GUID_COUNT  0x100\r
 \r
-//\r
-// The current maximum number of logging entries. If current number of \r
-// entries exceeds this value, it will re-allocate a larger array and\r
-// migration the old data to the larger array.\r
-//\r
-UINT32                  mMaxGaugeRecords;\r
+SMM_BOOT_PERFORMANCE_TABLE  *mSmmBootPerformanceTable = NULL;\r
 \r
-//\r
-// The handle to install Performance Protocol instance.\r
-//\r
-EFI_HANDLE              mHandle = NULL;\r
-\r
-BOOLEAN                 mPerformanceMeasurementEnabled;\r
-\r
-SPIN_LOCK               mSmmPerfLock;\r
+typedef struct {\r
+  EFI_HANDLE    Handle;\r
+  CHAR8         NameString[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
+  EFI_GUID      ModuleGuid;\r
+} HANDLE_GUID_MAP;\r
 \r
-EFI_SMRAM_DESCRIPTOR    *mSmramRanges;\r
-UINTN                   mSmramRangeCount;\r
+HANDLE_GUID_MAP  mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];\r
+UINTN            mCachePairCount = 0;\r
 \r
-//\r
-// Interfaces for SMM Performance Protocol.\r
-//\r
-PERFORMANCE_PROTOCOL mPerformanceInterface = {\r
-  StartGauge,\r
-  EndGauge,\r
-  GetGauge\r
-};\r
+UINT32                mPerformanceLength    = sizeof (SMM_BOOT_PERFORMANCE_TABLE);\r
+UINT32                mMaxPerformanceLength = 0;\r
+UINT32                mLoadImageCount       = 0;\r
+BOOLEAN               mFpdtDataIsReported   = FALSE;\r
+BOOLEAN               mLackSpaceIsReport    = FALSE;\r
+CHAR8                 *mPlatformLanguage    = NULL;\r
+SPIN_LOCK             mSmmFpdtLock;\r
+PERFORMANCE_PROPERTY  mPerformanceProperty;\r
+UINT32                mCachedLength   = 0;\r
+UINT32                mBootRecordSize = 0;\r
 \r
 //\r
-// Interfaces for SMM PerformanceEx Protocol.\r
+// Interfaces for SMM PerformanceMeasurement Protocol.\r
 //\r
-PERFORMANCE_EX_PROTOCOL mPerformanceExInterface = {\r
-  StartGaugeEx,\r
-  EndGaugeEx,\r
-  GetGaugeEx\r
+EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL  mPerformanceMeasurementInterface = {\r
+  CreatePerformanceMeasurement,\r
 };\r
 \r
 /**\r
-  Searches in the gauge array with keyword Handle, Token, Module and Identfier.\r
+  Return the pointer to the FPDT record in the allocated memory.\r
 \r
-  This internal function searches for the gauge entry in the gauge array.\r
-  If there is an entry that exactly matches the given keywords\r
-  and its end time stamp is zero, then the index of that gauge entry is returned;\r
-  otherwise, the the number of gauge entries in the array is returned.\r
-\r
-  @param  Handle                  Pointer to environment specific context used\r
-                                  to identify the component being measured.\r
-  @param  Token                   Pointer to a Null-terminated ASCII string\r
-                                  that identifies the component being measured.\r
-  @param  Module                  Pointer to a Null-terminated ASCII string\r
-                                  that identifies the module being measured.\r
-  @param  Identifier              32-bit identifier.\r
-\r
-  @retval The index of gauge entry in the array.\r
+  @param  RecordSize             The size of FPDT record.\r
+  @param  FpdtRecordPtr          Pointer the FPDT record in the allocated memory.\r
 \r
+  @retval EFI_SUCCESS            Successfully get the pointer to the FPDT record.\r
+  @retval EFI_OUT_OF_RESOURCES   Ran out of space to store the records.\r
 **/\r
-UINT32\r
-SmmSearchForGaugeEntry (\r
-  IN CONST VOID                 *Handle,  OPTIONAL\r
-  IN CONST CHAR8                *Token,   OPTIONAL\r
-  IN CONST CHAR8                *Module,   OPTIONAL\r
-  IN CONST UINT32               Identifier\r
+EFI_STATUS\r
+GetFpdtRecordPtr (\r
+  IN     UINT8            RecordSize,\r
+  IN OUT FPDT_RECORD_PTR  *FpdtRecordPtr\r
   )\r
 {\r
-  UINT32                    Index;\r
-  UINT32                    NumberOfEntries;\r
-  GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;\r
-\r
-  if (Token == NULL) {\r
-    Token = "";\r
-  }\r
-  if (Module == NULL) {\r
-    Module = "";\r
-  }\r
-\r
-  NumberOfEntries = mGaugeData->NumberOfEntries;\r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
+  if (mFpdtDataIsReported) {\r
+    //\r
+    // Append Boot records after Smm boot performance records have been reported.\r
+    //\r
+    if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {\r
+      if (!mLackSpaceIsReport) {\r
+        DEBUG ((DEBUG_INFO, "SmmCorePerformanceLib: No enough space to save boot records\n"));\r
+        mLackSpaceIsReport = TRUE;\r
+      }\r
 \r
-  for (Index = 0; Index < NumberOfEntries; Index++) {\r
-    if ((GaugeEntryExArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&\r
-         AsciiStrnCmp (GaugeEntryExArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
-         AsciiStrnCmp (GaugeEntryExArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
-         (GaugeEntryExArray[Index].Identifier == Identifier) &&\r
-         GaugeEntryExArray[Index].EndTimeStamp == 0) {\r
-      break;\r
+      return EFI_OUT_OF_RESOURCES;\r
+    } else {\r
+      //\r
+      // Covert buffer to FPDT Ptr Union type.\r
+      //\r
+      FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8 *)mSmmBootPerformanceTable + mSmmBootPerformanceTable->Header.Length);\r
     }\r
+  } else {\r
+    //\r
+    // Check if pre-allocated buffer is full\r
+    //\r
+    if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {\r
+      mSmmBootPerformanceTable = ReallocatePool (\r
+                                   mPerformanceLength,\r
+                                   mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER,\r
+                                   mSmmBootPerformanceTable\r
+                                   );\r
+\r
+      if (mSmmBootPerformanceTable == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+\r
+      mSmmBootPerformanceTable->Header.Length = mPerformanceLength;\r
+      mMaxPerformanceLength                   = mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER;\r
+    }\r
+\r
+    //\r
+    // Covert buffer to FPDT Ptr Union type.\r
+    //\r
+    FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8 *)mSmmBootPerformanceTable + mSmmBootPerformanceTable->Header.Length);\r
   }\r
 \r
-  return Index;\r
+  FpdtRecordPtr->RecordHeader->Length = 0;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Adds a record at the end of the performance measurement log\r
-  that records the start time of a performance measurement.\r
-\r
-  Adds a record to the end of the performance measurement log\r
-  that contains the Handle, Token, Module and Identifier.\r
-  The end time of the new record must be set to zero.\r
-  If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
-  If TimeStamp is zero, the start time in the record is filled in with the value\r
-  read from the current time stamp.\r
+Check whether the Token is a known one which is uesed by core.\r
 \r
-  @param  Handle                  Pointer to environment specific context used\r
-                                  to identify the component being measured.\r
-  @param  Token                   Pointer to a Null-terminated ASCII string\r
-                                  that identifies the component being measured.\r
-  @param  Module                  Pointer to a Null-terminated ASCII string\r
-                                  that identifies the module being measured.\r
-  @param  TimeStamp               64-bit time stamp.\r
-  @param  Identifier              32-bit identifier. If the value is 0, the created record\r
-                                  is same as the one created by StartGauge of PERFORMANCE_PROTOCOL.\r
+@param  Token      Pointer to a Null-terminated ASCII string\r
 \r
-  @retval EFI_SUCCESS             The data was read correctly from the device.\r
-  @retval EFI_OUT_OF_RESOURCES    There are not enough resources to record the measurement.\r
+@retval TRUE       Is a known one used by core.\r
+@retval FALSE      Not a known one.\r
 \r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
-StartGaugeEx (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
-  IN UINT64       TimeStamp,\r
-  IN UINT32       Identifier\r
+BOOLEAN\r
+IsKnownTokens (\r
+  IN CONST CHAR8  *Token\r
   )\r
 {\r
-  GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;\r
-  UINTN                     GaugeDataSize;\r
-  GAUGE_DATA_HEADER         *NewGaugeData;\r
-  UINTN                     OldGaugeDataSize;\r
-  GAUGE_DATA_HEADER         *OldGaugeData;\r
-  UINT32                    Index;\r
-\r
-  AcquireSpinLock (&mSmmPerfLock);\r
-\r
-  Index = mGaugeData->NumberOfEntries;\r
-  if (Index >= mMaxGaugeRecords) {\r
-    //\r
-    // Try to enlarge the scale of gauge array.\r
-    //\r
-    OldGaugeData      = mGaugeData;\r
-    OldGaugeDataSize  = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords;\r
-\r
-    GaugeDataSize     = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords * 2;\r
-\r
-    NewGaugeData = AllocateZeroPool (GaugeDataSize);\r
-    if (NewGaugeData == NULL) {\r
-      ReleaseSpinLock (&mSmmPerfLock);\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-\r
-    mGaugeData = NewGaugeData;\r
-    mMaxGaugeRecords *= 2;\r
-\r
-    //\r
-    // Initialize new data array and migrate old data one.\r
-    //\r
-    mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);\r
-\r
-    FreePool (OldGaugeData);\r
+  if (Token == NULL) {\r
+    return FALSE;\r
   }\r
 \r
-  GaugeEntryExArray               = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-  GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;\r
-\r
-  if (Token != NULL) {\r
-    AsciiStrnCpy (GaugeEntryExArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH);\r
-  }\r
-  if (Module != NULL) {\r
-    AsciiStrnCpy (GaugeEntryExArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH);\r
+  if ((AsciiStrCmp (Token, SEC_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, PEI_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, DXE_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, BDS_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, START_IMAGE_TOK) == 0) ||\r
+      (AsciiStrCmp (Token, PEIM_TOK) == 0))\r
+  {\r
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
   }\r
+}\r
 \r
-  GaugeEntryExArray[Index].EndTimeStamp = 0;\r
-  GaugeEntryExArray[Index].Identifier = Identifier;\r
-\r
-  if (TimeStamp == 0) {\r
-    TimeStamp = GetPerformanceCounter ();\r
-  }\r
-  GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;\r
+/**\r
+Check whether the ID is a known one which map to the known Token.\r
 \r
-  mGaugeData->NumberOfEntries++;\r
+@param  Identifier  32-bit identifier.\r
 \r
-  ReleaseSpinLock (&mSmmPerfLock);\r
+@retval TRUE        Is a known one used by core.\r
+@retval FALSE       Not a known one.\r
 \r
-  return EFI_SUCCESS;\r
+**/\r
+BOOLEAN\r
+IsKnownID (\r
+  IN UINT32  Identifier\r
+  )\r
+{\r
+  if ((Identifier == MODULE_START_ID) ||\r
+      (Identifier == MODULE_END_ID) ||\r
+      (Identifier == MODULE_LOADIMAGE_START_ID) ||\r
+      (Identifier == MODULE_LOADIMAGE_END_ID) ||\r
+      (Identifier == MODULE_DB_START_ID) ||\r
+      (Identifier == MODULE_DB_END_ID) ||\r
+      (Identifier == MODULE_DB_SUPPORT_START_ID) ||\r
+      (Identifier == MODULE_DB_SUPPORT_END_ID) ||\r
+      (Identifier == MODULE_DB_STOP_START_ID) ||\r
+      (Identifier == MODULE_DB_STOP_END_ID))\r
+  {\r
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
 }\r
 \r
 /**\r
-  Searches the performance measurement log from the beginning of the log\r
-  for the first matching record that contains a zero end time and fills in a valid end time.\r
+  Get the FPDT record identifier.\r
 \r
-  Searches the performance measurement log from the beginning of the log\r
-  for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.\r
-  If the record can not be found then return EFI_NOT_FOUND.\r
-  If the record is found and TimeStamp is not zero,\r
-  then the end time in the record is filled in with the value specified by TimeStamp.\r
-  If the record is found and TimeStamp is zero, then the end time in the matching record\r
-  is filled in with the current time stamp value.\r
+  @param Attribute                The attribute of the Record.\r
+                                  PerfStartEntry: Start Record.\r
+                                  PerfEndEntry: End Record.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component being measured.\r
+  @param  String                  Pointer to a Null-terminated ASCII string that identifies the component being measured.\r
+  @param  ProgressID              On return, pointer to the ProgressID.\r
 \r
-  @param  Handle                  Pointer to environment specific context used\r
-                                  to identify the component being measured.\r
-  @param  Token                   Pointer to a Null-terminated ASCII string\r
-                                  that identifies the component being measured.\r
-  @param  Module                  Pointer to a Null-terminated ASCII string\r
-                                  that identifies the module being measured.\r
-  @param  TimeStamp               64-bit time stamp.\r
-  @param  Identifier              32-bit identifier. If the value is 0, the found record\r
-                                  is same as the one found by EndGauge of PERFORMANCE_PROTOCOL.\r
-\r
-  @retval EFI_SUCCESS             The end of  the measurement was recorded.\r
-  @retval EFI_NOT_FOUND           The specified measurement record could not be found.\r
+  @retval EFI_SUCCESS              Get record info successfully.\r
+  @retval EFI_INVALID_PARAMETER    No matched FPDT record.\r
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-EndGaugeEx (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
-  IN UINT64       TimeStamp,\r
-  IN UINT32       Identifier\r
+GetFpdtRecordId (\r
+  IN       PERF_MEASUREMENT_ATTRIBUTE  Attribute,\r
+  IN CONST VOID                        *Handle,\r
+  IN CONST CHAR8                       *String,\r
+  OUT UINT16                           *ProgressID\r
   )\r
 {\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
-  UINT32              Index;\r
-\r
-  AcquireSpinLock (&mSmmPerfLock);\r
-\r
-  if (TimeStamp == 0) {\r
-    TimeStamp = GetPerformanceCounter ();\r
-  }\r
-\r
-  Index = SmmSearchForGaugeEntry (Handle, Token, Module, Identifier);\r
-  if (Index >= mGaugeData->NumberOfEntries) {\r
-    ReleaseSpinLock (&mSmmPerfLock);\r
-    return EFI_NOT_FOUND;\r
+  //\r
+  // Token to Id.\r
+  //\r
+  if (String != NULL) {\r
+    if (AsciiStrCmp (String, START_IMAGE_TOK) == 0) {\r
+      // "StartImage:"\r
+      if (Attribute == PerfStartEntry) {\r
+        *ProgressID = MODULE_START_ID;\r
+      } else {\r
+        *ProgressID = MODULE_END_ID;\r
+      }\r
+    } else if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) {\r
+      // "LoadImage:"\r
+      if (Attribute == PerfStartEntry) {\r
+        *ProgressID = MODULE_LOADIMAGE_START_ID;\r
+      } else {\r
+        *ProgressID = MODULE_LOADIMAGE_END_ID;\r
+      }\r
+    } else {\r
+      // Pref used in Modules\r
+      if (Attribute == PerfStartEntry) {\r
+        *ProgressID = PERF_INMODULE_START_ID;\r
+      } else {\r
+        *ProgressID = PERF_INMODULE_END_ID;\r
+      }\r
+    }\r
+  } else if (Handle != NULL) {\r
+    // Pref used in Modules\r
+    if (Attribute == PerfStartEntry) {\r
+      *ProgressID = PERF_INMODULE_START_ID;\r
+    } else {\r
+      *ProgressID = PERF_INMODULE_END_ID;\r
+    }\r
+  } else {\r
+    return EFI_UNSUPPORTED;\r
   }\r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-  GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;\r
-\r
-  ReleaseSpinLock (&mSmmPerfLock);\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  Retrieves a previously logged performance measurement.\r
-  It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,\r
-  and then assign the Identifier with 0.\r
-\r
-  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
-  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
-  GaugeDataEntryEx stores the pointer to that entry.\r
-\r
-  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
-                                  If 0, then the first performance measurement log entry is retrieved.\r
-  @param  GaugeDataEntryEx        The indirect pointer to the extended gauge data entry specified by LogEntryKey\r
-                                  if the retrieval is successful.\r
-\r
-  @retval EFI_SUCCESS             The GuageDataEntryEx is successfully found based on LogEntryKey.\r
-  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
-  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
-  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntryEx is NULL.\r
-\r
+  Get a human readable module name and module guid for the given image handle.\r
+  If module name can't be found, "" string will return.\r
+  If module guid can't be found, Zero Guid will return.\r
+\r
+  @param    Handle        Image handle or Controller handle.\r
+  @param    NameString    The ascii string will be filled into it. If not found, null string will return.\r
+  @param    BufferSize    Size of the input NameString buffer.\r
+  @param    ModuleGuid    Point to the guid buffer to store the got module guid value.\r
+\r
+  @retval EFI_SUCCESS     Successfully get module name and guid.\r
+  @retval EFI_INVALID_PARAMETER  The input parameter NameString is NULL.\r
+  @retval other value  Module Name can't be got.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-GetGaugeEx (\r
-  IN  UINTN                 LogEntryKey,\r
-  OUT GAUGE_DATA_ENTRY_EX   **GaugeDataEntryEx\r
+GetModuleInfoFromHandle (\r
+  IN EFI_HANDLE  Handle,\r
+  OUT CHAR8      *NameString,\r
+  IN UINTN       BufferSize,\r
+  OUT EFI_GUID   *ModuleGuid OPTIONAL\r
   )\r
 {\r
-  UINTN               NumberOfEntries;\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
-\r
-  NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);\r
-  if (LogEntryKey > NumberOfEntries) {\r
+  EFI_STATUS                         Status;\r
+  EFI_LOADED_IMAGE_PROTOCOL          *LoadedImage;\r
+  EFI_DRIVER_BINDING_PROTOCOL        *DriverBinding;\r
+  CHAR8                              *PdbFileName;\r
+  EFI_GUID                           *TempGuid;\r
+  UINTN                              StartIndex;\r
+  UINTN                              Index;\r
+  INTN                               Count;\r
+  BOOLEAN                            ModuleGuidIsGet;\r
+  UINTN                              StringSize;\r
+  CHAR16                             *StringPtr;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH  *FvFilePath;\r
+\r
+  if ((NameString == NULL) || (BufferSize == 0)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (LogEntryKey == NumberOfEntries) {\r
-    return EFI_NOT_FOUND;\r
+\r
+  //\r
+  // Try to get the ModuleGuid and name string form the caached array.\r
+  //\r
+  if (mCachePairCount > 0) {\r
+    for (Count = mCachePairCount - 1; Count >= 0; Count--) {\r
+      if (Handle == mCacheHandleGuidTable[Count].Handle) {\r
+        CopyGuid (ModuleGuid, &mCacheHandleGuidTable[Count].ModuleGuid);\r
+        AsciiStrCpyS (NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, mCacheHandleGuidTable[Count].NameString);\r
+        return EFI_SUCCESS;\r
+      }\r
+    }\r
   }\r
 \r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
+  Status          = EFI_INVALID_PARAMETER;\r
+  LoadedImage     = NULL;\r
+  ModuleGuidIsGet = FALSE;\r
 \r
-  if (GaugeDataEntryEx == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
+  //\r
+  // Initialize GUID as zero value.\r
+  //\r
+  TempGuid = &gZeroGuid;\r
+  //\r
+  // Initialize it as "" string.\r
+  //\r
+  NameString[0] = 0;\r
+\r
+  if (Handle != NULL) {\r
+    //\r
+    // Try Handle as ImageHandle.\r
+    //\r
+    Status = gBS->HandleProtocol (\r
+                    Handle,\r
+                    &gEfiLoadedImageProtocolGuid,\r
+                    (VOID **)&LoadedImage\r
+                    );\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Try Handle as Controller Handle\r
+      //\r
+      Status = gBS->OpenProtocol (\r
+                      Handle,\r
+                      &gEfiDriverBindingProtocolGuid,\r
+                      (VOID **)&DriverBinding,\r
+                      NULL,\r
+                      NULL,\r
+                      EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                      );\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Get Image protocol from ImageHandle\r
+        //\r
+        Status = gBS->HandleProtocol (\r
+                        DriverBinding->ImageHandle,\r
+                        &gEfiLoadedImageProtocolGuid,\r
+                        (VOID **)&LoadedImage\r
+                        );\r
+      }\r
+    }\r
   }\r
-  *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+  if (!EFI_ERROR (Status) && (LoadedImage != NULL)) {\r
+    //\r
+    // Get Module Guid from DevicePath.\r
+    //\r
+    if ((LoadedImage->FilePath != NULL) &&\r
+        (LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH) &&\r
+        (LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP)\r
+        )\r
+    {\r
+      //\r
+      // Determine GUID associated with module logging performance\r
+      //\r
+      ModuleGuidIsGet = TRUE;\r
+      FvFilePath      = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)LoadedImage->FilePath;\r
+      TempGuid        = &FvFilePath->FvFileName;\r
+    }\r
 \r
-/**\r
-  Adds a record at the end of the performance measurement log\r
-  that records the start time of a performance measurement.\r
+    //\r
+    // Method 1 Get Module Name from PDB string.\r
+    //\r
+    PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);\r
+    if ((PdbFileName != NULL) && (BufferSize > 0)) {\r
+      StartIndex = 0;\r
+      for (Index = 0; PdbFileName[Index] != 0; Index++) {\r
+        if ((PdbFileName[Index] == '\\') || (PdbFileName[Index] == '/')) {\r
+          StartIndex = Index + 1;\r
+        }\r
+      }\r
+\r
+      //\r
+      // Copy the PDB file name to our temporary string.\r
+      // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.\r
+      //\r
+      for (Index = 0; Index < BufferSize - 1; Index++) {\r
+        NameString[Index] = PdbFileName[Index + StartIndex];\r
+        if ((NameString[Index] == 0) || (NameString[Index] == '.')) {\r
+          NameString[Index] = 0;\r
+          break;\r
+        }\r
+      }\r
+\r
+      if (Index == BufferSize - 1) {\r
+        NameString[Index] = 0;\r
+      }\r
+\r
+      //\r
+      // Module Name is got.\r
+      //\r
+      goto Done;\r
+    }\r
+  }\r
 \r
-  Adds a record to the end of the performance measurement log\r
-  that contains the Handle, Token, and Module.\r
-  The end time of the new record must be set to zero.\r
-  If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
-  If TimeStamp is zero, the start time in the record is filled in with the value\r
-  read from the current time stamp.\r
+  if (ModuleGuidIsGet) {\r
+    //\r
+    // Method 2 Try to get the image's FFS UI section by image GUID\r
+    //\r
+    StringPtr  = NULL;\r
+    StringSize = 0;\r
+    Status     = GetSectionFromAnyFv (\r
+                   TempGuid,\r
+                   EFI_SECTION_USER_INTERFACE,\r
+                   0,\r
+                   (VOID **)&StringPtr,\r
+                   &StringSize\r
+                   );\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Method 3. Get the name string from FFS UI section\r
+      //\r
+      for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {\r
+        NameString[Index] = (CHAR8)StringPtr[Index];\r
+      }\r
+\r
+      NameString[Index] = 0;\r
+      FreePool (StringPtr);\r
+    }\r
+  }\r
 \r
-  @param  Handle                  Pointer to environment specific context used\r
-                                  to identify the component being measured.\r
-  @param  Token                   Pointer to a Null-terminated ASCII string\r
-                                  that identifies the component being measured.\r
-  @param  Module                  Pointer to a Null-terminated ASCII string\r
-                                  that identifies the module being measured.\r
-  @param  TimeStamp               64-bit time stamp.\r
+Done:\r
+  //\r
+  // Copy Module Guid\r
+  //\r
+  if (ModuleGuid != NULL) {\r
+    CopyGuid (ModuleGuid, TempGuid);\r
+    if (IsZeroGuid (TempGuid) && (Handle != NULL) && !ModuleGuidIsGet) {\r
+      // Handle is GUID\r
+      CopyGuid (ModuleGuid, (EFI_GUID *)Handle);\r
+    }\r
+  }\r
 \r
-  @retval EFI_SUCCESS             The data was read correctly from the device.\r
-  @retval EFI_OUT_OF_RESOURCES    There are not enough resources to record the measurement.\r
+  //\r
+  // Cache the Handle and Guid pairs.\r
+  //\r
+  if (mCachePairCount < CACHE_HANDLE_GUID_COUNT) {\r
+    mCacheHandleGuidTable[mCachePairCount].Handle = Handle;\r
+    CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, ModuleGuid);\r
+    AsciiStrCpyS (mCacheHandleGuidTable[mCachePairCount].NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, NameString);\r
+    mCachePairCount++;\r
+  }\r
 \r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-StartGauge (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
-  IN UINT64       TimeStamp\r
-  )\r
-{\r
-  return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
+  return Status;\r
 }\r
 \r
 /**\r
-  Searches the performance measurement log from the beginning of the log\r
-  for the first matching record that contains a zero end time and fills in a valid end time.\r
-\r
-  Searches the performance measurement log from the beginning of the log\r
-  for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
-  If the record can not be found then return EFI_NOT_FOUND.\r
-  If the record is found and TimeStamp is not zero,\r
-  then the end time in the record is filled in with the value specified by TimeStamp.\r
-  If the record is found and TimeStamp is zero, then the end time in the matching record\r
-  is filled in with the current time stamp value.\r
-\r
-  @param  Handle                  Pointer to environment specific context used\r
-                                  to identify the component being measured.\r
-  @param  Token                   Pointer to a Null-terminated ASCII string\r
-                                  that identifies the component being measured.\r
-  @param  Module                  Pointer to a Null-terminated ASCII string\r
-                                  that identifies the module being measured.\r
-  @param  TimeStamp               64-bit time stamp.\r
+  Copies the string from Source into Destination and updates Length with the\r
+  size of the string.\r
 \r
-  @retval EFI_SUCCESS             The end of  the measurement was recorded.\r
-  @retval EFI_NOT_FOUND           The specified measurement record could not be found.\r
+  @param Destination - destination of the string copy\r
+  @param Source      - pointer to the source string which will get copied\r
+  @param Length      - pointer to a length variable to be updated\r
 \r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
-EndGauge (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
-  IN UINT64       TimeStamp\r
+VOID\r
+CopyStringIntoPerfRecordAndUpdateLength (\r
+  IN OUT CHAR8        *Destination,\r
+  IN     CONST CHAR8  *Source,\r
+  IN OUT UINT8        *Length\r
   )\r
 {\r
-  return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
-}\r
+  UINTN  StringLen;\r
+  UINTN  DestMax;\r
 \r
-/**\r
-  Retrieves a previously logged performance measurement.\r
-  It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,\r
-  and then eliminate the Identifier.\r
+  ASSERT (Source != NULL);\r
+\r
+  if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+    DestMax = STRING_SIZE;\r
+  } else {\r
+    DestMax = AsciiStrSize (Source);\r
+    if (DestMax > STRING_SIZE) {\r
+      DestMax = STRING_SIZE;\r
+    }\r
+  }\r
 \r
-  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
-  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
-  GaugeDataEntry stores the pointer to that entry.\r
+  StringLen = AsciiStrLen (Source);\r
+  if (StringLen >= DestMax) {\r
+    StringLen = DestMax -1;\r
+  }\r
 \r
-  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
-                                  If 0, then the first performance measurement log entry is retrieved.\r
-  @param  GaugeDataEntry          The indirect pointer to the gauge data entry specified by LogEntryKey\r
-                                  if the retrieval is successful.\r
+  AsciiStrnCpyS (Destination, DestMax, Source, StringLen);\r
+  *Length += (UINT8)DestMax;\r
 \r
-  @retval EFI_SUCCESS             The GuageDataEntry is successfully found based on LogEntryKey.\r
-  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
-  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
-  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntry is NULL.\r
+  return;\r
+}\r
+\r
+/**\r
+  Create performance record with event description and a timestamp.\r
+\r
+  @param CallerIdentifier  - Image handle or pointer to caller ID GUID.\r
+  @param Guid              - Pointer to a GUID.\r
+  @param String            - Pointer to a string describing the measurement.\r
+  @param Ticker            - 64-bit time stamp.\r
+  @param Address           - Pointer to a location in memory relevant to the measurement.\r
+  @param PerfId            - Performance identifier describing the type of measurement.\r
+  @param Attribute         - The attribute of the measurement. According to attribute can create a start\r
+                             record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
+                             or a general record for other Perf macros.\r
+\r
+  @retval EFI_SUCCESS           - Successfully created performance record.\r
+  @retval EFI_OUT_OF_RESOURCES  - Ran out of space to store the records.\r
+  @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
+                                  pointer or invalid PerfId.\r
+\r
+  @retval EFI_SUCCESS           - Successfully created performance record\r
+  @retval EFI_OUT_OF_RESOURCES  - Ran out of space to store the records\r
+  @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
+                                  pointer or invalid PerfId\r
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-GetGauge (\r
-  IN  UINTN               LogEntryKey,\r
-  OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
+InsertFpdtRecord (\r
+  IN CONST VOID                        *CallerIdentifier   OPTIONAL,\r
+  IN CONST VOID                        *Guid     OPTIONAL,\r
+  IN CONST CHAR8                       *String   OPTIONAL,\r
+  IN       UINT64                      Ticker,\r
+  IN       UINT64                      Address   OPTIONAL,\r
+  IN       UINT16                      PerfId,\r
+  IN       PERF_MEASUREMENT_ATTRIBUTE  Attribute\r
   )\r
+\r
 {\r
-  EFI_STATUS          Status;\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryEx;\r
+  EFI_STATUS       Status;\r
+  EFI_GUID         ModuleGuid;\r
+  CHAR8            ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
+  FPDT_RECORD_PTR  FpdtRecordPtr;\r
+  FPDT_RECORD_PTR  CachedFpdtRecordPtr;\r
+  UINT64           TimeStamp;\r
+  CONST CHAR8      *StringPtr;\r
+  UINTN            DestMax;\r
+  UINTN            StringLen;\r
+  UINT16           ProgressId;\r
+\r
+  StringPtr = NULL;\r
+  ZeroMem (ModuleName, sizeof (ModuleName));\r
+\r
+  //\r
+  // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.\r
+  //    notes: For other Perf macros (Attribute == PerfEntry), their Id is known.\r
+  //\r
+  if (Attribute != PerfEntry) {\r
+    //\r
+    // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.\r
+    // !!! Note: If the Perf is not the known Token used in the core but have same\r
+    // ID with the core Token, this case will not be supported.\r
+    // And in currtnt usage mode, for the unkown ID, there is a general rule:\r
+    // If it is start pref: the lower 4 bits of the ID should be 0.\r
+    // If it is end pref: the lower 4 bits of the ID should not be 0.\r
+    // If input ID doesn't follow the rule, we will adjust it.\r
+    //\r
+    if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
+      if ((Attribute == PerfStartEntry) && ((PerfId & 0x000F) != 0)) {\r
+        PerfId &= 0xFFF0;\r
+      } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {\r
+        PerfId += 1;\r
+      }\r
+    }\r
 \r
-  GaugeEntryEx = NULL;\r
+    if (PerfId == 0) {\r
+      //\r
+      // Get ProgressID form the String Token.\r
+      //\r
+      Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &ProgressId);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
 \r
-  Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);\r
+      PerfId = ProgressId;\r
+    }\r
+  }\r
+\r
+  //\r
+  // 2. Get the buffer to store the FPDT record.\r
+  //\r
+  Status = GetFpdtRecordPtr (FPDT_MAX_PERF_RECORD_SIZE, &FpdtRecordPtr);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  if (GaugeDataEntry == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
+  //\r
+  // 3. Get the TimeStamp.\r
+  //\r
+  if (Ticker == 0) {\r
+    Ticker    = GetPerformanceCounter ();\r
+    TimeStamp = GetTimeInNanoSecond (Ticker);\r
+  } else if (Ticker == 1) {\r
+    TimeStamp = 0;\r
+  } else {\r
+    TimeStamp = GetTimeInNanoSecond (Ticker);\r
   }\r
 \r
-  *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;\r
+  //\r
+  // 4. Fill in the FPDT record according to different Performance Identifier.\r
+  //\r
+  switch (PerfId) {\r
+    case MODULE_START_ID:\r
+    case MODULE_END_ID:\r
+      GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
+      StringPtr = ModuleName;\r
+      //\r
+      // Cache the offset of start image start record and use to update the start image end record if needed.\r
+      //\r
+      if ((PerfId == MODULE_START_ID) && (Attribute == PerfEntry)) {\r
+        mCachedLength = mSmmBootPerformanceTable->Header.Length;\r
+      }\r
+\r
+      if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+        FpdtRecordPtr.GuidEvent->Header.Type     = FPDT_GUID_EVENT_TYPE;\r
+        FpdtRecordPtr.GuidEvent->Header.Length   = sizeof (FPDT_GUID_EVENT_RECORD);\r
+        FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+        FpdtRecordPtr.GuidEvent->ProgressID      = PerfId;\r
+        FpdtRecordPtr.GuidEvent->Timestamp       = TimeStamp;\r
+        CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
+        if ((CallerIdentifier == NULL) && (PerfId == MODULE_END_ID) && (mCachedLength != 0)) {\r
+          CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8 *)mSmmBootPerformanceTable + mCachedLength);\r
+          CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &CachedFpdtRecordPtr.GuidEvent->Guid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
+          mCachedLength = 0;\r
+        }\r
+      }\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+      break;\r
 \r
-/**\r
-  This function check if the address is in SMRAM.\r
+    case MODULE_LOADIMAGE_START_ID:\r
+    case MODULE_LOADIMAGE_END_ID:\r
+      GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
+      StringPtr = ModuleName;\r
+      if (PerfId == MODULE_LOADIMAGE_START_ID) {\r
+        mLoadImageCount++;\r
+        //\r
+        // Cache the offset of load image start record and use to be updated by the load image end record if needed.\r
+        //\r
+        if ((CallerIdentifier == NULL) && (Attribute == PerfEntry)) {\r
+          mCachedLength = mSmmBootPerformanceTable->Header.Length;\r
+        }\r
+      }\r
+\r
+      if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+        FpdtRecordPtr.GuidQwordEvent->Header.Type     = FPDT_GUID_QWORD_EVENT_TYPE;\r
+        FpdtRecordPtr.GuidQwordEvent->Header.Length   = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
+        FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+        FpdtRecordPtr.GuidQwordEvent->ProgressID      = PerfId;\r
+        FpdtRecordPtr.GuidQwordEvent->Timestamp       = TimeStamp;\r
+        FpdtRecordPtr.GuidQwordEvent->Qword           = mLoadImageCount;\r
+        CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));\r
+        if ((PerfId == MODULE_LOADIMAGE_END_ID) && (mCachedLength != 0)) {\r
+          CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8 *)mSmmBootPerformanceTable + mCachedLength);\r
+          CopyMem (&CachedFpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.GuidQwordEvent->Guid));\r
+          mCachedLength = 0;\r
+        }\r
+      }\r
 \r
-  @param Buffer  the buffer address to be checked.\r
-  @param Length  the buffer length to be checked.\r
+      break;\r
 \r
-  @retval TRUE  this address is in SMRAM.\r
-  @retval FALSE this address is NOT in SMRAM.\r
-**/\r
-BOOLEAN\r
-IsAddressInSmram (\r
-  IN EFI_PHYSICAL_ADDRESS  Buffer,\r
-  IN UINT64                Length\r
-  )\r
-{\r
-  UINTN  Index;\r
+    case PERF_EVENTSIGNAL_START_ID:\r
+    case PERF_EVENTSIGNAL_END_ID:\r
+    case PERF_CALLBACK_START_ID:\r
+    case PERF_CALLBACK_END_ID:\r
+      if ((String == NULL) || (Guid == NULL)) {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+\r
+      StringPtr = String;\r
+      if (AsciiStrLen (String) == 0) {\r
+        StringPtr = "unknown name";\r
+      }\r
+\r
+      if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+        FpdtRecordPtr.DualGuidStringEvent->Header.Type     = FPDT_DUAL_GUID_STRING_EVENT_TYPE;\r
+        FpdtRecordPtr.DualGuidStringEvent->Header.Length   = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);\r
+        FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+        FpdtRecordPtr.DualGuidStringEvent->ProgressID      = PerfId;\r
+        FpdtRecordPtr.DualGuidStringEvent->Timestamp       = TimeStamp;\r
+        CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, CallerIdentifier, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));\r
+        CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));\r
+        CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);\r
+      }\r
 \r
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r
-        ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r
-      return TRUE;\r
-    }\r
+      break;\r
+\r
+    case PERF_EVENT_ID:\r
+    case PERF_FUNCTION_START_ID:\r
+    case PERF_FUNCTION_END_ID:\r
+    case PERF_INMODULE_START_ID:\r
+    case PERF_INMODULE_END_ID:\r
+    case PERF_CROSSMODULE_START_ID:\r
+    case PERF_CROSSMODULE_END_ID:\r
+      GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
+      if (String != NULL) {\r
+        StringPtr = String;\r
+      } else {\r
+        StringPtr = ModuleName;\r
+      }\r
+\r
+      if (AsciiStrLen (StringPtr) == 0) {\r
+        StringPtr = "unknown name";\r
+      }\r
+\r
+      if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+        FpdtRecordPtr.DynamicStringEvent->Header.Type     = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+        FpdtRecordPtr.DynamicStringEvent->Header.Length   = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
+        FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+        FpdtRecordPtr.DynamicStringEvent->ProgressID      = PerfId;\r
+        FpdtRecordPtr.DynamicStringEvent->Timestamp       = TimeStamp;\r
+        CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
+        CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
+      }\r
+\r
+      break;\r
+\r
+    default:\r
+      if (Attribute != PerfEntry) {\r
+        GetModuleInfoFromHandle ((EFI_HANDLE)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
+        if (String != NULL) {\r
+          StringPtr = String;\r
+        } else {\r
+          StringPtr = ModuleName;\r
+        }\r
+\r
+        if (AsciiStrLen (StringPtr) == 0) {\r
+          StringPtr = "unknown name";\r
+        }\r
+\r
+        if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+          FpdtRecordPtr.DynamicStringEvent->Header.Type     = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+          FpdtRecordPtr.DynamicStringEvent->Header.Length   = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
+          FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+          FpdtRecordPtr.DynamicStringEvent->ProgressID      = PerfId;\r
+          FpdtRecordPtr.DynamicStringEvent->Timestamp       = TimeStamp;\r
+          CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
+          CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
+        }\r
+      } else {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+\r
+      break;\r
   }\r
 \r
-  return FALSE;\r
-}\r
+  //\r
+  // 4.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.\r
+  //\r
+  if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+    if (StringPtr == NULL) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
 \r
-/**\r
-  Communication service SMI Handler entry.\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Type     = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Length   = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
+    FpdtRecordPtr.DynamicStringEvent->ProgressID      = PerfId;\r
+    FpdtRecordPtr.DynamicStringEvent->Timestamp       = TimeStamp;\r
+    if (Guid != NULL) {\r
+      //\r
+      // Cache the event guid in string event record.\r
+      //\r
+      CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, Guid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
+    } else {\r
+      CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
+    }\r
 \r
-  This SMI handler provides services for the performance wrapper driver.\r
+    if (AsciiStrLen (StringPtr) == 0) {\r
+      StringPtr = "unknown name";\r
+    }\r
 \r
-  @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
-  @param[in]     RegisterContext Points to an optional handler context which was specified when the\r
-                                 handler was registered.\r
-  @param[in, out] CommBuffer     A pointer to a collection of data in memory that will\r
-                                 be conveyed from a non-SMM environment into an SMM environment.\r
-  @param[in, out] CommBufferSize The size of the CommBuffer.\r
+    CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
 \r
-  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers \r
-                                              should still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should \r
-                                              still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still \r
-                                              be called.\r
-  @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-SmmPerformanceHandlerEx (\r
-  IN     EFI_HANDLE                    DispatchHandle,\r
-  IN     CONST VOID                   *RegisterContext,\r
-  IN OUT VOID                          *CommBuffer,\r
-  IN OUT UINTN                         *CommBufferSize\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  SMM_PERF_COMMUNICATE_EX   *SmmPerfCommData;\r
-  GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;\r
-  UINTN                     DataSize;\r
-\r
-  GaugeEntryExArray = NULL;\r
-\r
-  ASSERT (CommBuffer != NULL);\r
-\r
-  SmmPerfCommData = (SMM_PERF_COMMUNICATE_EX *)CommBuffer;\r
-\r
-  switch (SmmPerfCommData->Function) {\r
-    case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :\r
-       SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;\r
-       Status = EFI_SUCCESS;\r
-       break;\r
-\r
-    case SMM_PERF_FUNCTION_GET_GAUGE_DATA :\r
-       if ( SmmPerfCommData->GaugeDataEx == NULL || SmmPerfCommData->NumberOfEntries == 0 ||\r
-         (SmmPerfCommData->LogEntryKey + SmmPerfCommData->NumberOfEntries) > mGaugeData->NumberOfEntries) {\r
-         Status = EFI_INVALID_PARAMETER;\r
-         break;\r
-       }\r
-\r
-       //\r
-       // Sanity check\r
-       //\r
-       DataSize = SmmPerfCommData->NumberOfEntries * sizeof(GAUGE_DATA_ENTRY_EX);\r
-       if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmPerfCommData->GaugeDataEx, DataSize)) {\r
-         DEBUG ((EFI_D_ERROR, "Smm Performance Data buffer is in SMRAM!\n"));\r
-         Status = EFI_ACCESS_DENIED;\r
-         break ;\r
-       }\r
-\r
-       GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-       CopyMem(\r
-         (UINT8 *) (SmmPerfCommData->GaugeDataEx),\r
-         (UINT8 *) &GaugeEntryExArray[SmmPerfCommData->LogEntryKey],\r
-         DataSize\r
-         );\r
-       Status = EFI_SUCCESS;\r
-       break;\r
+    if ((PerfId == MODULE_LOADIMAGE_START_ID) || (PerfId == MODULE_END_ID)) {\r
+      FpdtRecordPtr.DynamicStringEvent->Header.Length = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)+ STRING_SIZE);\r
+    }\r
 \r
-    default:\r
-       ASSERT (FALSE);\r
-       Status = EFI_UNSUPPORTED;\r
+    if (((PerfId == MODULE_LOADIMAGE_END_ID) || (PerfId == MODULE_END_ID)) && (mCachedLength != 0)) {\r
+      CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8 *)mSmmBootPerformanceTable + mCachedLength);\r
+      if (PerfId == MODULE_LOADIMAGE_END_ID) {\r
+        DestMax   = CachedFpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
+        StringLen = AsciiStrLen (StringPtr);\r
+        if (StringLen >= DestMax) {\r
+          StringLen = DestMax -1;\r
+        }\r
+\r
+        CopyMem (&CachedFpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
+        AsciiStrnCpyS (CachedFpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StringLen);\r
+      } else if (PerfId == MODULE_END_ID) {\r
+        DestMax   = FpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
+        StringLen = AsciiStrLen (CachedFpdtRecordPtr.DynamicStringEvent->String);\r
+        if (StringLen >= DestMax) {\r
+          StringLen = DestMax -1;\r
+        }\r
+\r
+        CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &CachedFpdtRecordPtr.DynamicStringEvent->Guid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
+        AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, CachedFpdtRecordPtr.DynamicStringEvent->String, StringLen);\r
+      }\r
+\r
+      mCachedLength = 0;\r
+    }\r
   }\r
 \r
-  SmmPerfCommData->ReturnStatus = Status;\r
+  //\r
+  // 5. Update the length of the used buffer after fill in the record.\r
+  //\r
+  mPerformanceLength                      += FpdtRecordPtr.RecordHeader->Length;\r
+  mSmmBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Communication service SMI Handler entry.\r
 \r
-  This SMI handler provides services for the performance wrapper driver.\r
+  This SMI handler provides services for report MM boot records.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  Communicate buffer and buffer size are external input, so this function will do basic validation.\r
 \r
   @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
   @param[in]     RegisterContext Points to an optional handler context which was specified when the\r
                                  handler was registered.\r
   @param[in, out] CommBuffer     A pointer to a collection of data in memory that will\r
-                                 be conveyed from a non-SMM environment into an SMM environment.\r
+                                 be conveyed from a non-MM environment into an MM environment.\r
   @param[in, out] CommBufferSize The size of the CommBuffer.\r
 \r
-  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers \r
+  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers\r
                                               should still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should \r
+  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should\r
                                               still be called.\r
-  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still \r
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still\r
                                               be called.\r
   @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-SmmPerformanceHandler (\r
-  IN     EFI_HANDLE                    DispatchHandle,\r
-  IN     CONST VOID                   *RegisterContext,\r
-  IN OUT VOID                          *CommBuffer,\r
-  IN OUT UINTN                         *CommBufferSize\r
+FpdtSmiHandler (\r
+  IN     EFI_HANDLE  DispatchHandle,\r
+  IN     CONST VOID  *RegisterContext,\r
+  IN OUT VOID        *CommBuffer,\r
+  IN OUT UINTN       *CommBufferSize\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  SMM_PERF_COMMUNICATE  *SmmPerfCommData;\r
-  GAUGE_DATA_ENTRY_EX   *GaugeEntryExArray;\r
-  UINTN                 DataSize;\r
-  UINTN                 Index;\r
-  UINTN                 LogEntryKey;\r
-\r
-  GaugeEntryExArray = NULL;\r
-\r
-  ASSERT (CommBuffer != NULL);\r
-\r
-  SmmPerfCommData = (SMM_PERF_COMMUNICATE *)CommBuffer;\r
-\r
-  switch (SmmPerfCommData->Function) {\r
-    case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :\r
-       SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;\r
-       Status = EFI_SUCCESS;\r
-       break;\r
-\r
-    case SMM_PERF_FUNCTION_GET_GAUGE_DATA :\r
-       if ( SmmPerfCommData->GaugeData == NULL || SmmPerfCommData->NumberOfEntries == 0 ||\r
-         (SmmPerfCommData->LogEntryKey + SmmPerfCommData->NumberOfEntries) > mGaugeData->NumberOfEntries) {\r
-         Status = EFI_INVALID_PARAMETER;\r
-         break;\r
-       }\r
-\r
-       //\r
-       // Sanity check\r
-       //\r
-       DataSize = SmmPerfCommData->NumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
-       if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmPerfCommData->GaugeData, DataSize)) {\r
-         DEBUG ((EFI_D_ERROR, "Smm Performance Data buffer is in SMRAM!\n"));\r
-         Status = EFI_ACCESS_DENIED;\r
-         break ;\r
-       }\r
-\r
-       GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-\r
-       LogEntryKey = SmmPerfCommData->LogEntryKey;\r
-       for (Index = 0; Index < SmmPerfCommData->NumberOfEntries; Index++) {\r
-         CopyMem(\r
-           (UINT8 *) &(SmmPerfCommData->GaugeData[Index]),\r
-           (UINT8 *) &GaugeEntryExArray[LogEntryKey++],\r
-           sizeof (GAUGE_DATA_ENTRY)\r
-           );\r
-       }\r
-       Status = EFI_SUCCESS;\r
-       break;\r
+  EFI_STATUS                   Status;\r
+  SMM_BOOT_RECORD_COMMUNICATE  *SmmCommData;\r
+  UINTN                        BootRecordOffset;\r
+  UINTN                        BootRecordSize;\r
+  VOID                         *BootRecordData;\r
+  UINTN                        TempCommBufferSize;\r
+  UINT8                        *BootRecordBuffer;\r
+\r
+  //\r
+  // If input is invalid, stop processing this SMI\r
+  //\r
+  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  TempCommBufferSize = *CommBufferSize;\r
+\r
+  if (TempCommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {\r
+    DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM communication data buffer in MMRAM or overflow!\n"));\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE *)CommBuffer;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  switch (SmmCommData->Function) {\r
+    case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE:\r
+      if (mSmmBootPerformanceTable != NULL) {\r
+        mBootRecordSize = mSmmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);\r
+      }\r
+\r
+      SmmCommData->BootRecordSize = mBootRecordSize;\r
+      break;\r
+\r
+    case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA:\r
+      Status = EFI_UNSUPPORTED;\r
+      break;\r
+\r
+    case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET:\r
+      BootRecordOffset = SmmCommData->BootRecordOffset;\r
+      BootRecordData   = SmmCommData->BootRecordData;\r
+      BootRecordSize   = SmmCommData->BootRecordSize;\r
+      if ((BootRecordData == NULL) || (BootRecordOffset >= mBootRecordSize)) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+        break;\r
+      }\r
+\r
+      //\r
+      // Sanity check\r
+      //\r
+      if (BootRecordSize > mBootRecordSize - BootRecordOffset) {\r
+        BootRecordSize = mBootRecordSize - BootRecordOffset;\r
+      }\r
+\r
+      SmmCommData->BootRecordSize = BootRecordSize;\r
+      if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, BootRecordSize)) {\r
+        DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM Data buffer in MMRAM or overflow!\n"));\r
+        Status = EFI_ACCESS_DENIED;\r
+        break;\r
+      }\r
+\r
+      BootRecordBuffer = ((UINT8 *)(mSmmBootPerformanceTable)) + sizeof (SMM_BOOT_PERFORMANCE_TABLE);\r
+      CopyMem (\r
+        (UINT8 *)BootRecordData,\r
+        BootRecordBuffer + BootRecordOffset,\r
+        BootRecordSize\r
+        );\r
+      mFpdtDataIsReported = TRUE;\r
+      break;\r
 \r
     default:\r
-       ASSERT (FALSE);\r
-       Status = EFI_UNSUPPORTED;\r
+      Status = EFI_UNSUPPORTED;\r
   }\r
 \r
-  SmmPerfCommData->ReturnStatus = Status;\r
+  SmmCommData->ReturnStatus = Status;\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
-  SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized \r
-  this function is callbacked to initialize the Smm Performance Lib \r
+  SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized\r
+  this function is callbacked to initialize the Smm Performance Lib\r
 \r
   @param  Event    The event of notify protocol.\r
   @param  Context  Notify event context.\r
@@ -644,79 +940,57 @@ SmmPerformanceHandler (
 VOID\r
 EFIAPI\r
 InitializeSmmCorePerformanceLib (\r
-  IN EFI_EVENT     Event,\r
-  IN VOID          *Context\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
-  EFI_HANDLE                Handle;\r
-  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;\r
-  UINTN                     Size;\r
-\r
+  EFI_HANDLE            Handle;\r
+  EFI_HANDLE            SmiHandle;\r
+  EFI_STATUS            Status;\r
+  PERFORMANCE_PROPERTY  *PerformanceProperty;\r
 \r
   //\r
   // Initialize spin lock\r
   //\r
-  InitializeSpinLock (&mSmmPerfLock);\r
-\r
-  mMaxGaugeRecords = INIT_SMM_GAUGE_DATA_ENTRIES;\r
+  InitializeSpinLock (&mSmmFpdtLock);\r
 \r
-  mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY_EX) * mMaxGaugeRecords));\r
-  ASSERT (mGaugeData != NULL);\r
-  \r
   //\r
-  // Get SMRAM information\r
-  //\r
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Size = 0;\r
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
-\r
-  Status = gSmst->SmmAllocatePool (\r
-                    EfiRuntimeServicesData,\r
-                    Size,\r
-                    (VOID **)&mSmramRanges\r
-                    );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
-\r
-  //\r
-  // Install the protocol interfaces.\r
+  // Install the protocol interfaces for SMM performance library instance.\r
   //\r
+  Handle = NULL;\r
   Status = gSmst->SmmInstallProtocolInterface (\r
-                    &mHandle,\r
-                    &gSmmPerformanceProtocolGuid,\r
+                    &Handle,\r
+                    &gEdkiiSmmPerformanceMeasurementProtocolGuid,\r
                     EFI_NATIVE_INTERFACE,\r
-                    &mPerformanceInterface\r
+                    &mPerformanceMeasurementInterface\r
                     );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  Status = gSmst->SmmInstallProtocolInterface (\r
-                    &mHandle,\r
-                    &gSmmPerformanceExProtocolGuid,\r
-                    EFI_NATIVE_INTERFACE,\r
-                    &mPerformanceExInterface\r
-                    );\r
+  //\r
+  // Register SMI handler.\r
+  //\r
+  SmiHandle = NULL;\r
+  Status    = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &SmiHandle);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  ///\r
-  /// Register SMM Performance SMI handler\r
-  ///\r
-  Handle = NULL;\r
-  Status = gSmst->SmiHandlerRegister (SmmPerformanceHandler, &gSmmPerformanceProtocolGuid, &Handle);\r
-  ASSERT_EFI_ERROR (Status);\r
-  Status = gSmst->SmiHandlerRegister (SmmPerformanceHandlerEx, &gSmmPerformanceExProtocolGuid, &Handle);\r
-  ASSERT_EFI_ERROR (Status);\r
+  Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **)&PerformanceProperty);\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Install configuration table for performance property.\r
+    //\r
+    mPerformanceProperty.Revision  = PERFORMANCE_PROPERTY_REVISION;\r
+    mPerformanceProperty.Reserved  = 0;\r
+    mPerformanceProperty.Frequency = GetPerformanceCounterProperties (\r
+                                       &mPerformanceProperty.TimerStartValue,\r
+                                       &mPerformanceProperty.TimerEndValue\r
+                                       );\r
+    Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
 }\r
 \r
 /**\r
-  The constructor function initializes the Performance Measurement Enable flag and \r
+  The constructor function initializes the Performance Measurement Enable flag and\r
   registers SmmBase2 protocol notify callback.\r
   It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
 \r
@@ -737,8 +1011,7 @@ SmmCorePerformanceLibConstructor (
   EFI_EVENT   Event;\r
   VOID        *Registration;\r
 \r
-  mPerformanceMeasurementEnabled =  (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
-  if (!mPerformanceMeasurementEnabled) {\r
+  if (!PerformanceMeasurementEnabled ()) {\r
     //\r
     // Do not initialize performance infrastructure if not required.\r
     //\r
@@ -771,6 +1044,46 @@ SmmCorePerformanceLibConstructor (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Create performance record with event description and a timestamp.\r
+\r
+  @param CallerIdentifier  - Image handle or pointer to caller ID GUID.\r
+  @param Guid              - Pointer to a GUID.\r
+  @param String            - Pointer to a string describing the measurement.\r
+  @param TimeStamp         - 64-bit time stamp.\r
+  @param Address           - Pointer to a location in memory relevant to the measurement.\r
+  @param Identifier        - Performance identifier describing the type of measurement.\r
+  @param Attribute         - The attribute of the measurement. According to attribute can create a start\r
+                             record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
+                             or a general record for other Perf macros.\r
+\r
+  @retval EFI_SUCCESS           - Successfully created performance record.\r
+  @retval EFI_OUT_OF_RESOURCES  - Ran out of space to store the records.\r
+  @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
+                                  pointer or invalid PerfId.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CreatePerformanceMeasurement (\r
+  IN CONST VOID                        *CallerIdentifier  OPTIONAL,\r
+  IN CONST VOID                        *Guid      OPTIONAL,\r
+  IN CONST CHAR8                       *String    OPTIONAL,\r
+  IN       UINT64                      TimeStamp  OPTIONAL,\r
+  IN       UINT64                      Address    OPTIONAL,\r
+  IN       UINT32                      Identifier,\r
+  IN       PERF_MEASUREMENT_ATTRIBUTE  Attribute\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  AcquireSpinLock (&mSmmFpdtLock);\r
+  Status = InsertFpdtRecord (CallerIdentifier, Guid, String, TimeStamp, Address, (UINT16)Identifier, Attribute);\r
+  ReleaseSpinLock (&mSmmFpdtLock);\r
+  return Status;\r
+}\r
+\r
 /**\r
   Adds a record at the end of the performance measurement log\r
   that records the start time of a performance measurement.\r
@@ -799,14 +1112,24 @@ SmmCorePerformanceLibConstructor (
 RETURN_STATUS\r
 EFIAPI\r
 StartPerformanceMeasurementEx (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
   IN UINT64       TimeStamp,\r
   IN UINT32       Identifier\r
   )\r
 {\r
-  return (RETURN_STATUS) StartGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
+  CONST CHAR8  *String;\r
+\r
+  if (Token != NULL) {\r
+    String = Token;\r
+  } else if (Module != NULL) {\r
+    String = Module;\r
+  } else {\r
+    String = NULL;\r
+  }\r
+\r
+  return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);\r
 }\r
 \r
 /**\r
@@ -838,14 +1161,24 @@ StartPerformanceMeasurementEx (
 RETURN_STATUS\r
 EFIAPI\r
 EndPerformanceMeasurementEx (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
   IN UINT64       TimeStamp,\r
   IN UINT32       Identifier\r
   )\r
 {\r
-  return (RETURN_STATUS) EndGaugeEx (Handle, Token, Module, TimeStamp, Identifier);\r
+  CONST CHAR8  *String;\r
+\r
+  if (Token != NULL) {\r
+    String = Token;\r
+  } else if (Module != NULL) {\r
+    String = Module;\r
+  } else {\r
+    String = NULL;\r
+  }\r
+\r
+  return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);\r
 }\r
 \r
 /**\r
@@ -853,6 +1186,8 @@ EndPerformanceMeasurementEx (
   It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
   and then assign the Identifier with 0.\r
 \r
+  !!! Not Support!!!\r
+\r
   Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
   zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
   and the key for the second entry in the log is returned.  If the performance log is empty,\r
@@ -892,51 +1227,16 @@ EndPerformanceMeasurementEx (
 UINTN\r
 EFIAPI\r
 GetPerformanceMeasurementEx (\r
-  IN  UINTN       LogEntryKey, \r
-  OUT CONST VOID  **Handle,\r
-  OUT CONST CHAR8 **Token,\r
-  OUT CONST CHAR8 **Module,\r
-  OUT UINT64      *StartTimeStamp,\r
-  OUT UINT64      *EndTimeStamp,\r
-  OUT UINT32      *Identifier\r
+  IN  UINTN        LogEntryKey,\r
+  OUT CONST VOID   **Handle,\r
+  OUT CONST CHAR8  **Token,\r
+  OUT CONST CHAR8  **Module,\r
+  OUT UINT64       *StartTimeStamp,\r
+  OUT UINT64       *EndTimeStamp,\r
+  OUT UINT32       *Identifier\r
   )\r
 {\r
-  EFI_STATUS           Status;\r
-  GAUGE_DATA_ENTRY_EX  *GaugeData;\r
-\r
-  GaugeData = NULL;\r
-  \r
-  ASSERT (Handle != NULL);\r
-  ASSERT (Token != NULL);\r
-  ASSERT (Module != NULL);\r
-  ASSERT (StartTimeStamp != NULL);\r
-  ASSERT (EndTimeStamp != NULL);\r
-  ASSERT (Identifier != NULL);\r
-\r
-  Status = GetGaugeEx (LogEntryKey++, &GaugeData);\r
-\r
-  //\r
-  // Make sure that LogEntryKey is a valid log entry key,\r
-  //\r
-  ASSERT (Status != EFI_INVALID_PARAMETER);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    //\r
-    // The LogEntryKey is the last entry (equals to the total entry number).\r
-    //\r
-    return 0;\r
-  }\r
-\r
-  ASSERT (GaugeData != NULL);\r
-\r
-  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
-  *Token          = GaugeData->Token;\r
-  *Module         = GaugeData->Module;\r
-  *StartTimeStamp = GaugeData->StartTimeStamp;\r
-  *EndTimeStamp   = GaugeData->EndTimeStamp;\r
-  *Identifier     = GaugeData->Identifier;\r
-\r
-  return LogEntryKey;\r
+  return 0;\r
 }\r
 \r
 /**\r
@@ -965,9 +1265,9 @@ GetPerformanceMeasurementEx (
 RETURN_STATUS\r
 EFIAPI\r
 StartPerformanceMeasurement (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
   IN UINT64       TimeStamp\r
   )\r
 {\r
@@ -1001,9 +1301,9 @@ StartPerformanceMeasurement (
 RETURN_STATUS\r
 EFIAPI\r
 EndPerformanceMeasurement (\r
-  IN CONST VOID   *Handle,  OPTIONAL\r
-  IN CONST CHAR8  *Token,   OPTIONAL\r
-  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN CONST VOID   *Handle   OPTIONAL,\r
+  IN CONST CHAR8  *Token    OPTIONAL,\r
+  IN CONST CHAR8  *Module   OPTIONAL,\r
   IN UINT64       TimeStamp\r
   )\r
 {\r
@@ -1015,6 +1315,8 @@ EndPerformanceMeasurement (
   It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
   and then eliminate the Identifier.\r
 \r
+  !!! Not Support!!!\r
+\r
   Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
   zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
   and the key for the second entry in the log is returned.  If the performance log is empty,\r
@@ -1052,16 +1354,15 @@ EndPerformanceMeasurement (
 UINTN\r
 EFIAPI\r
 GetPerformanceMeasurement (\r
-  IN  UINTN       LogEntryKey,\r
-  OUT CONST VOID  **Handle,\r
-  OUT CONST CHAR8 **Token,\r
-  OUT CONST CHAR8 **Module,\r
-  OUT UINT64      *StartTimeStamp,\r
-  OUT UINT64      *EndTimeStamp\r
+  IN  UINTN        LogEntryKey,\r
+  OUT CONST VOID   **Handle,\r
+  OUT CONST CHAR8  **Token,\r
+  OUT CONST CHAR8  **Module,\r
+  OUT UINT64       *StartTimeStamp,\r
+  OUT UINT64       *EndTimeStamp\r
   )\r
 {\r
-  UINT32 Identifier;\r
-  return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);\r
+  return 0;\r
 }\r
 \r
 /**\r
@@ -1082,5 +1383,61 @@ PerformanceMeasurementEnabled (
   VOID\r
   )\r
 {\r
-  return mPerformanceMeasurementEnabled;\r
+  return (BOOLEAN)((PcdGet8 (PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
+}\r
+\r
+/**\r
+  Create performance record with event description and a timestamp.\r
+\r
+  @param CallerIdentifier  - Image handle or pointer to caller ID GUID\r
+  @param Guid              - Pointer to a GUID\r
+  @param String            - Pointer to a string describing the measurement\r
+  @param Address           - Pointer to a location in memory relevant to the measurement\r
+  @param Identifier        - Performance identifier describing the type of measurement\r
+\r
+  @retval RETURN_SUCCESS           - Successfully created performance record\r
+  @retval RETURN_OUT_OF_RESOURCES  - Ran out of space to store the records\r
+  @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
+                                     pointer or invalid PerfId\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+LogPerformanceMeasurement (\r
+  IN CONST VOID   *CallerIdentifier,\r
+  IN CONST VOID   *Guid     OPTIONAL,\r
+  IN CONST CHAR8  *String   OPTIONAL,\r
+  IN UINT64       Address  OPTIONAL,\r
+  IN UINT32       Identifier\r
+  )\r
+{\r
+  return (RETURN_STATUS)CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);\r
+}\r
+\r
+/**\r
+  Check whether the specified performance measurement can be logged.\r
+\r
+  This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
+  and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
+\r
+  @param Type        - Type of the performance measurement entry.\r
+\r
+  @retval TRUE         The performance measurement can be logged.\r
+  @retval FALSE        The performance measurement can NOT be logged.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+LogPerformanceMeasurementEnabled (\r
+  IN  CONST UINTN  Type\r
+  )\r
+{\r
+  //\r
+  // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
+  //\r
+  if (PerformanceMeasurementEnabled () && ((PcdGet8 (PcdPerformanceLibraryPropertyMask) & Type) == 0)) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
 }\r