]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
MdeModulePkg/PerfLib: Add NULL pointer check for "Token"
[mirror_edk2.git] / MdeModulePkg / Library / SmmCorePerformanceLib / SmmCorePerformanceLib.c
index 214d044bcb4dad1c2ebdba74fa0ee582aa39b868..dbc1166f254a2cd3c3d4cc823b58c9c7c7d602ab 100644 (file)
@@ -9,7 +9,14 @@
   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
+ 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
+ SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted input and do basic validation.\r
+\r
+Copyright (c) 2011 - 2018, 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
@@ -23,29 +30,28 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \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
-\r
-//\r
-// The handle to install Performance Protocol instance.\r
-//\r
-EFI_HANDLE              mHandle = NULL;\r
+SMM_BOOT_PERFORMANCE_TABLE    *mSmmBootPerformanceTable = NULL;\r
 \r
-BOOLEAN                 mPerformanceMeasurementEnabled;\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
-SPIN_LOCK               mSmmPerfLock;\r
+HANDLE_GUID_MAP      mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];\r
+UINTN                mCachePairCount = 0;\r
 \r
-EFI_SMRAM_DESCRIPTOR    *mSmramRanges;\r
-UINTN                   mSmramRangeCount;\r
+UINT32               mPerformanceLength    = 0;\r
+UINT32               mMaxPerformanceLength = 0;\r
+BOOLEAN              mFpdtDataIsReported   = FALSE;\r
+BOOLEAN              mLackSpaceIsReport    = FALSE;\r
+CHAR8                *mPlatformLanguage    = NULL;\r
+SPIN_LOCK            mSmmFpdtLock;\r
+PERFORMANCE_PROPERTY  mPerformanceProperty;\r
 \r
 //\r
 // Interfaces for SMM Performance Protocol.\r
@@ -66,158 +72,621 @@ PERFORMANCE_EX_PROTOCOL mPerformanceExInterface = {
 };\r
 \r
 /**\r
-  Searches in the gauge array with keyword Handle, Token, Module and Identfier.\r
+Check whether the Token is a known one which is uesed by core.\r
+\r
+@param  Token      Pointer to a Null-terminated ASCII string\r
+\r
+@retval TRUE       Is a known one used by core.\r
+@retval FALSE      Not a known one.\r
+\r
+**/\r
+BOOLEAN\r
+IsKnownTokens (\r
+  IN CONST CHAR8  *Token\r
+  )\r
+{\r
+  if (Token == NULL) {\r
+    return FALSE;\r
+  }\r
+\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
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\r
+\r
+/**\r
+Check whether the ID is a known one which map to the known Token.\r
+\r
+@param  Identifier  32-bit identifier.\r
+\r
+@retval TRUE        Is a known one used by core.\r
+@retval FALSE       Not a known one.\r
+\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
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\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
+  Get the FPDT record info.\r
 \r
+  @param  IsStart                 TRUE if the performance log is start log.\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
+  @param  RecordInfo              On return, pointer to the info of the record.\r
+  @param  UseModuleName           Only useful for FPDT_DYNAMIC_STRING_EVENT_TYPE, indicate that whether need use\r
+                                  Module name to fill the string field in the FPDT_DYNAMIC_STRING_EVENT_RECORD.\r
 \r
-  @retval The index of gauge entry in the array.\r
+  @retval EFI_SUCCESS             Get record info successfully.\r
+  @retval EFI_UNSUPPORTED         No matched FPDT record.\r
 \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
+GetFpdtRecordInfo (\r
+  IN BOOLEAN                  IsStart,\r
+  IN CONST VOID               *Handle,\r
+  IN CONST CHAR8              *Token,\r
+  IN CONST CHAR8              *Module,\r
+  OUT FPDT_BASIC_RECORD_INFO  *RecordInfo,\r
+  IN OUT BOOLEAN              *UseModuleName\r
   )\r
 {\r
-  UINT32                    Index;\r
-  UINT32                    Index2;\r
-  UINT32                    NumberOfEntries;\r
-  GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;\r
+  UINT16              RecordType;\r
+  UINTN               StringSize;\r
 \r
-  if (Token == NULL) {\r
-    Token = "";\r
-  }\r
-  if (Module == NULL) {\r
-    Module = "";\r
+  RecordType = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+\r
+  //\r
+  // Token to Type and Id.\r
+  //\r
+  if (Token != NULL) {\r
+    if (AsciiStrCmp (Token, START_IMAGE_TOK) == 0) {              // "StartImage:"\r
+      *UseModuleName = TRUE;\r
+      RecordType     = FPDT_GUID_EVENT_TYPE;\r
+      if (IsStart) {\r
+        RecordInfo->ProgressID  = MODULE_START_ID;\r
+      } else {\r
+        RecordInfo->ProgressID  = MODULE_END_ID;\r
+      }\r
+    } else if (AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0) {        // "LoadImage:"\r
+      *UseModuleName = TRUE;\r
+      RecordType     = FPDT_GUID_QWORD_EVENT_TYPE;\r
+      if (IsStart) {\r
+        RecordInfo->ProgressID  = MODULE_LOADIMAGE_START_ID;\r
+      } else {\r
+        RecordInfo->ProgressID  = MODULE_LOADIMAGE_END_ID;\r
+      }\r
+    } else {                                                      // Pref used in Modules\r
+      if (IsStart) {\r
+        RecordInfo->ProgressID  = PERF_INMODULE_START_ID;\r
+      } else {\r
+        RecordInfo->ProgressID  = PERF_INMODULE_END_ID;\r
+      }\r
+    }\r
+  } else if (Handle != NULL || Module != NULL) {                 // Pref used in Modules\r
+    if (IsStart) {\r
+      RecordInfo->ProgressID    = PERF_INMODULE_START_ID;\r
+    } else {\r
+      RecordInfo->ProgressID    = PERF_INMODULE_END_ID;\r
+    }\r
+  } else {\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
-  NumberOfEntries = mGaugeData->NumberOfEntries;\r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
+  if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
+    RecordType               = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+    RecordInfo->RecordSize   = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD) + STRING_SIZE;\r
+  } else {\r
+    switch (RecordType) {\r
+    case FPDT_GUID_EVENT_TYPE:\r
+      RecordInfo->RecordSize  = sizeof (FPDT_GUID_EVENT_RECORD);\r
+      break;\r
 \r
-  Index2 = 0;\r
+    case FPDT_DYNAMIC_STRING_EVENT_TYPE:\r
+      if (*UseModuleName) {\r
+        StringSize   = STRING_SIZE;\r
+      } else if (Token != NULL) {\r
+        StringSize  = AsciiStrSize (Token);\r
+      } else if (Module != NULL) {\r
+        StringSize  = AsciiStrSize (Module);\r
+      } else {\r
+        StringSize  = STRING_SIZE;\r
+      }\r
+      if (StringSize > STRING_SIZE) {\r
+        StringSize  = STRING_SIZE;\r
+      }\r
+      RecordInfo->RecordSize  = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD) + StringSize);\r
+      break;\r
 \r
-  for (Index = 0; Index < NumberOfEntries; Index++) {\r
-    Index2 = NumberOfEntries - 1 - Index;\r
-    if (GaugeEntryExArray[Index2].EndTimeStamp == 0 &&\r
-        (GaugeEntryExArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&\r
-        AsciiStrnCmp (GaugeEntryExArray[Index2].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
-        AsciiStrnCmp (GaugeEntryExArray[Index2].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
-        (GaugeEntryExArray[Index2].Identifier == Identifier)) {\r
-      Index = Index2;\r
+    case FPDT_GUID_QWORD_EVENT_TYPE:\r
+      RecordInfo->RecordSize  = (UINT8)sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
       break;\r
+\r
+    default:\r
+      //\r
+      // Record type is unsupported in SMM phase.\r
+      //\r
+      return EFI_UNSUPPORTED;\r
     }\r
   }\r
 \r
-  return Index;\r
+  RecordInfo->Type = RecordType;\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
+  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
+GetModuleInfoFromHandle (\r
+  IN EFI_HANDLE        Handle,\r
+  OUT CHAR8            *NameString,\r
+  IN UINTN             BufferSize,\r
+  OUT EFI_GUID         *ModuleGuid OPTIONAL\r
+  )\r
+{\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
 \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
+  //\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
+  Status = EFI_INVALID_PARAMETER;\r
+  LoadedImage     = NULL;\r
+  ModuleGuidIsGet = FALSE;\r
+\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
+\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
+      // 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
+    // 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
+      // 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
+      // Module Name is got.\r
+      //\r
+      goto Done;\r
+    }\r
+  }\r
+\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
+      NameString[Index] = 0;\r
+      FreePool (StringPtr);\r
+    }\r
+  }\r
+\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
+  //\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
+  return Status;\r
+}\r
+\r
+/**\r
+  Add performance log to FPDT boot record table.\r
+\r
+  @param  IsStart                 TRUE if the performance log is start log.\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  Ticker                  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
 \r
-  @retval EFI_SUCCESS             The data was read correctly from the device.\r
+  @retval EFI_SUCCESS             Add FPDT boot record.\r
   @retval EFI_OUT_OF_RESOURCES    There are not enough resources to record the measurement.\r
+  @retval EFI_UNSUPPORTED         No matched FPDT record.\r
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-StartGaugeEx (\r
+InsertFpdtMeasurement (\r
+  IN BOOLEAN      IsStart,\r
   IN CONST VOID   *Handle,  OPTIONAL\r
   IN CONST CHAR8  *Token,   OPTIONAL\r
   IN CONST CHAR8  *Module,  OPTIONAL\r
-  IN UINT64       TimeStamp,\r
+  IN UINT64       Ticker,\r
   IN UINT32       Identifier\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
+  EFI_GUID                     ModuleGuid;\r
+  CHAR8                        ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
+  EFI_STATUS                   Status;\r
+  FPDT_RECORD_PTR              FpdtRecordPtr;\r
+  UINT64                       TimeStamp;\r
+  FPDT_BASIC_RECORD_INFO       RecordInfo;\r
+  UINTN                        DestMax;\r
+  UINTN                        StrLength;\r
+  CONST CHAR8                  *StringPtr;\r
+  BOOLEAN                      UseModuleName;\r
+\r
+  StringPtr     = NULL;\r
+  UseModuleName = FALSE;\r
+  ZeroMem (ModuleName, sizeof (ModuleName));\r
+\r
+  //\r
+  // Get record info includes type, size, ProgressID.\r
+  //\r
+  Status = GetFpdtRecordInfo (IsStart, Handle, Token, Module, &RecordInfo, &UseModuleName);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
-  AcquireSpinLock (&mSmmPerfLock);\r
+  //\r
+  // If PERF_START()/PERF_END() 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 ((Identifier != 0) && (IsKnownID (Identifier)) && (!IsKnownTokens (Token))) {\r
+    return EFI_UNSUPPORTED;\r
+  } else if ((Identifier != 0) && (!IsKnownID (Identifier)) && (!IsKnownTokens (Token))) {\r
+    if (IsStart && ((Identifier & 0x000F) != 0)) {\r
+      Identifier &= 0xFFF0;\r
+    } else if ((!IsStart) && ((Identifier & 0x000F) == 0)) {\r
+      Identifier += 1;\r
+    }\r
+    RecordInfo.ProgressID = (UINT16)Identifier;\r
+  }\r
 \r
-  Index = mGaugeData->NumberOfEntries;\r
-  if (Index >= mMaxGaugeRecords) {\r
+  if (mFpdtDataIsReported) {\r
     //\r
-    // Try to enlarge the scale of gauge array.\r
+    // Append Boot records after Smm boot performance records have been reported.\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
+    if (mPerformanceLength + RecordInfo.RecordSize > mMaxPerformanceLength) {\r
+      if (!mLackSpaceIsReport) {\r
+        DEBUG ((DEBUG_INFO, "SmmCorePerformanceLib: No enough space to save boot records\n"));\r
+        mLackSpaceIsReport = TRUE;\r
+      }\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 + RecordInfo.RecordSize > mMaxPerformanceLength) {\r
+      mSmmBootPerformanceTable = ReallocatePool (\r
+                                   mPerformanceLength,\r
+                                   mPerformanceLength + sizeof (SMM_BOOT_PERFORMANCE_TABLE) + RecordInfo.RecordSize + FIRMWARE_RECORD_BUFFER,\r
+                                   mSmmBootPerformanceTable\r
+                              );\r
+\r
+      if (mSmmBootPerformanceTable == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      mSmmBootPerformanceTable->Header.Length = sizeof (SMM_BOOT_PERFORMANCE_TABLE) + mPerformanceLength;\r
+      mMaxPerformanceLength = mPerformanceLength + sizeof (SMM_BOOT_PERFORMANCE_TABLE) + RecordInfo.RecordSize + FIRMWARE_RECORD_BUFFER;\r
     }\r
-\r
-    mGaugeData = NewGaugeData;\r
-    mMaxGaugeRecords *= 2;\r
-\r
     //\r
-    // Initialize new data array and migrate old data one.\r
+    // Covert buffer to FPDT Ptr Union type.\r
     //\r
-    mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);\r
+    FpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)((UINT8*)mSmmBootPerformanceTable + mSmmBootPerformanceTable->Header.Length);\r
+  }\r
+  FpdtRecordPtr.RecordHeader->Length = 0;\r
 \r
-    FreePool (OldGaugeData);\r
+  //\r
+  // 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
-  GaugeEntryExArray               = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-  GaugeEntryExArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;\r
+  //\r
+  // Get the ModuleName and ModuleGuid form the handle.\r
+  //\r
+  GetModuleInfoFromHandle ((EFI_HANDLE *)Handle, ModuleName, sizeof (ModuleName), &ModuleGuid);\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
+  //\r
+  // Fill in the record information.\r
+  //\r
+  switch (RecordInfo.Type) {\r
+  case FPDT_GUID_EVENT_TYPE:\r
+    FpdtRecordPtr.GuidEvent->Header.Type                = FPDT_GUID_EVENT_TYPE;\r
+    FpdtRecordPtr.GuidEvent->Header.Length              = RecordInfo.RecordSize;\r
+    FpdtRecordPtr.GuidEvent->Header.Revision            = FPDT_RECORD_REVISION_1;\r
+    FpdtRecordPtr.GuidEvent->ProgressID                 = RecordInfo.ProgressID;\r
+    FpdtRecordPtr.GuidEvent->Timestamp                  = TimeStamp;\r
+    CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
+    break;\r
+\r
+  case FPDT_DYNAMIC_STRING_EVENT_TYPE:\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Type       = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Length     = RecordInfo.RecordSize;\r
+    FpdtRecordPtr.DynamicStringEvent->Header.Revision   = FPDT_RECORD_REVISION_1;\r
+    FpdtRecordPtr.DynamicStringEvent->ProgressID        = RecordInfo.ProgressID;\r
+    FpdtRecordPtr.DynamicStringEvent->Timestamp         = TimeStamp;\r
+    CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
+\r
+    if (UseModuleName) {\r
+      StringPtr     = ModuleName;\r
+    } else if (Token != NULL) {\r
+      StringPtr     = Token;\r
+    } else if (Module != NULL) {\r
+      StringPtr     = Module;\r
+    } else if (ModuleName != NULL) {\r
+      StringPtr     = ModuleName;\r
+    }\r
+    if (StringPtr != NULL && AsciiStrLen (StringPtr) != 0) {\r
+      StrLength     = AsciiStrLen (StringPtr);\r
+      DestMax       = (RecordInfo.RecordSize - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)) / sizeof (CHAR8);\r
+      if (StrLength >= DestMax) {\r
+        StrLength   = DestMax -1;\r
+      }\r
+      AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StrLength);\r
+    } else {\r
+      AsciiStrCpyS (FpdtRecordPtr.DynamicStringEvent->String, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, "unknown name");\r
+    }\r
+    break;\r
+\r
+  case FPDT_GUID_QWORD_EVENT_TYPE:\r
+    FpdtRecordPtr.GuidQwordEvent->Header.Type           = FPDT_GUID_QWORD_EVENT_TYPE;\r
+    FpdtRecordPtr.GuidQwordEvent->Header.Length         = RecordInfo.RecordSize;\r
+    FpdtRecordPtr.GuidQwordEvent->Header.Revision       = FPDT_RECORD_REVISION_1;\r
+    FpdtRecordPtr.GuidQwordEvent->ProgressID            = RecordInfo.ProgressID;\r
+    FpdtRecordPtr.GuidQwordEvent->Timestamp             = TimeStamp;\r
+    CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));\r
+    break;\r
+\r
+  default:\r
+    //\r
+    // Record is not supported in current SMM phase, return EFI_UNSUPPORTED\r
+    //\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
-  GaugeEntryExArray[Index].EndTimeStamp = 0;\r
-  GaugeEntryExArray[Index].Identifier = Identifier;\r
+  //\r
+  // Update the cached FPDT record buffer.\r
+  //\r
+  mPerformanceLength += FpdtRecordPtr.RecordHeader->Length;\r
+  mSmmBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;\r
 \r
-  if (TimeStamp == 0) {\r
-    TimeStamp = GetPerformanceCounter ();\r
-  }\r
-  GaugeEntryExArray[Index].StartTimeStamp = TimeStamp;\r
+  return EFI_SUCCESS;\r
+}\r
 \r
-  mGaugeData->NumberOfEntries++;\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
-  ReleaseSpinLock (&mSmmPerfLock);\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
 \r
-  return EFI_SUCCESS;\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
+\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
+**/\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
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  AcquireSpinLock (&mSmmFpdtLock);\r
+\r
+  Status = InsertFpdtMeasurement (TRUE, Handle, Token, Module, TimeStamp, Identifier);\r
+\r
+  ReleaseSpinLock (&mSmmFpdtLock);\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -256,26 +725,15 @@ EndGaugeEx (
   IN UINT32       Identifier\r
   )\r
 {\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
-  UINT32              Index;\r
-\r
-  AcquireSpinLock (&mSmmPerfLock);\r
+  EFI_STATUS     Status;\r
 \r
-  if (TimeStamp == 0) {\r
-    TimeStamp = GetPerformanceCounter ();\r
-  }\r
+  AcquireSpinLock (&mSmmFpdtLock);\r
 \r
-  Index = SmmSearchForGaugeEntry (Handle, Token, Module, Identifier);\r
-  if (Index >= mGaugeData->NumberOfEntries) {\r
-    ReleaseSpinLock (&mSmmPerfLock);\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-  GaugeEntryExArray[Index].EndTimeStamp = TimeStamp;\r
+  Status = InsertFpdtMeasurement (FALSE, Handle, Token, Module, TimeStamp, Identifier);\r
 \r
-  ReleaseSpinLock (&mSmmPerfLock);\r
+  ReleaseSpinLock (&mSmmFpdtLock);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -283,6 +741,8 @@ EndGaugeEx (
   It can also retrieve the log created by StartGauge and EndGauge of PERFORMANCE_PROTOCOL,\r
   and then assign the Identifier with 0.\r
 \r
+  !!! Not Support!!!\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
@@ -305,25 +765,7 @@ GetGaugeEx (
   OUT GAUGE_DATA_ENTRY_EX   **GaugeDataEntryEx\r
   )\r
 {\r
-  UINTN               NumberOfEntries;\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryExArray;\r
-\r
-  NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);\r
-  if (LogEntryKey > NumberOfEntries) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  if (LogEntryKey == NumberOfEntries) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);\r
-\r
-  if (GaugeDataEntryEx == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-  *GaugeDataEntryEx = &GaugeEntryExArray[LogEntryKey];\r
-\r
-  return EFI_SUCCESS;\r
+  return EFI_UNSUPPORTED;\r
 }\r
 \r
 /**\r
@@ -402,6 +844,8 @@ EndGauge (
   It can also retrieve the log created by StartGaugeEx and EndGaugeEx of PERFORMANCE_EX_PROTOCOL,\r
   and then eliminate the Identifier.\r
 \r
+  !!! Not Support!!!\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
@@ -424,223 +868,52 @@ GetGauge (
   OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
   )\r
 {\r
-  EFI_STATUS          Status;\r
-  GAUGE_DATA_ENTRY_EX *GaugeEntryEx;\r
-\r
-  GaugeEntryEx = NULL;\r
-\r
-  Status = GetGaugeEx (LogEntryKey, &GaugeEntryEx);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  if (GaugeDataEntry == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  *GaugeDataEntry = (GAUGE_DATA_ENTRY *) GaugeEntryEx;\r
-\r
-  return EFI_SUCCESS;\r
+  return EFI_UNSUPPORTED;\r
 }\r
 \r
-/**\r
-  This function check if the address is in SMRAM.\r
-\r
-  @param Buffer  the buffer address to be checked.\r
-  @param Length  the buffer length to be checked.\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
-\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
-  }\r
-\r
-  return FALSE;\r
-}\r
 \r
 /**\r
-  Communication service SMI Handler entry.\r
-\r
-  This SMI handler provides services for the performance wrapper driver.\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
-\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
+  SmmReadyToBoot protocol notification event handler.\r
 \r
-    default:\r
-       ASSERT (FALSE);\r
-       Status = EFI_UNSUPPORTED;\r
-  }\r
+  @param  Protocol   Points to the protocol's unique identifier\r
+  @param  Interface  Points to the interface instance\r
+  @param  Handle     The handle on which the interface was installed\r
 \r
-  SmmPerfCommData->ReturnStatus = Status;\r
-  return EFI_SUCCESS;\r
-}\r
+  @retval EFI_SUCCESS   SmmReadyToBootCallback runs successfully\r
 \r
-/**\r
-  Communication service SMI Handler entry.\r
-\r
-  This SMI handler provides services for the performance wrapper driver.\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
-\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
-SmmPerformanceHandler (\r
-  IN     EFI_HANDLE                    DispatchHandle,\r
-  IN     CONST VOID                   *RegisterContext,\r
-  IN OUT VOID                          *CommBuffer,\r
-  IN OUT UINTN                         *CommBufferSize\r
+SmmReportFpdtRecordData (\r
+  IN CONST EFI_GUID                       *Protocol,\r
+  IN VOID                                 *Interface,\r
+  IN EFI_HANDLE                           Handle\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
-\r
-    default:\r
-       ASSERT (FALSE);\r
-       Status = EFI_UNSUPPORTED;\r
+  UINT64          SmmBPDTddr;\r
+\r
+  if (!mFpdtDataIsReported && mSmmBootPerformanceTable != NULL) {\r
+    SmmBPDTddr = (UINT64)(UINTN)mSmmBootPerformanceTable;\r
+    REPORT_STATUS_CODE_EX (\r
+        EFI_PROGRESS_CODE,\r
+        EFI_SOFTWARE_SMM_DRIVER,\r
+        0,\r
+        NULL,\r
+        &gEdkiiFpdtExtendedFirmwarePerformanceGuid,\r
+        &SmmBPDTddr,\r
+        sizeof (UINT64)\r
+        );\r
+    //\r
+    // Set FPDT report state to TRUE.\r
+    //\r
+    mFpdtDataIsReported = TRUE;\r
   }\r
-\r
-  SmmPerfCommData->ReturnStatus = Status;\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
@@ -653,75 +926,58 @@ InitializeSmmCorePerformanceLib (
   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_STATUS                Status;\r
+  VOID                      *SmmReadyToBootRegistration;\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
+                    &Handle,\r
                     &gSmmPerformanceProtocolGuid,\r
                     EFI_NATIVE_INTERFACE,\r
                     &mPerformanceInterface\r
                     );\r
   ASSERT_EFI_ERROR (Status);\r
-\r
   Status = gSmst->SmmInstallProtocolInterface (\r
-                    &mHandle,\r
+                    &Handle,\r
                     &gSmmPerformanceExProtocolGuid,\r
                     EFI_NATIVE_INTERFACE,\r
                     &mPerformanceExInterface\r
                     );\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 = gSmst->SmmRegisterProtocolNotify (\r
+                    &gEdkiiSmmReadyToBootProtocolGuid,\r
+                    SmmReportFpdtRecordData,\r
+                    &SmmReadyToBootRegistration\r
+                    );\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
@@ -742,8 +998,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
@@ -858,6 +1113,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
@@ -897,7 +1154,7 @@ EndPerformanceMeasurementEx (
 UINTN\r
 EFIAPI\r
 GetPerformanceMeasurementEx (\r
-  IN  UINTN       LogEntryKey, \r
+  IN  UINTN       LogEntryKey,\r
   OUT CONST VOID  **Handle,\r
   OUT CONST CHAR8 **Token,\r
   OUT CONST CHAR8 **Module,\r
@@ -906,42 +1163,7 @@ GetPerformanceMeasurementEx (
   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
@@ -976,7 +1198,7 @@ StartPerformanceMeasurement (
   IN UINT64       TimeStamp\r
   )\r
 {\r
-  return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
+  return StartGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
 }\r
 \r
 /**\r
@@ -1012,7 +1234,7 @@ EndPerformanceMeasurement (
   IN UINT64       TimeStamp\r
   )\r
 {\r
-  return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
+  return EndGaugeEx (Handle, Token, Module, TimeStamp, 0);\r
 }\r
 \r
 /**\r
@@ -1020,6 +1242,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
@@ -1065,8 +1289,7 @@ GetPerformanceMeasurement (
   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
@@ -1087,5 +1310,5 @@ PerformanceMeasurementEnabled (
   VOID\r
   )\r
 {\r
-  return mPerformanceMeasurementEnabled;\r
+  return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
 }\r