]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
MdeModulePkg-FPDT(4): Use fixed buffer for SMM_PERF_COMMUNICATE in PerfLib.
[mirror_edk2.git] / MdeModulePkg / Library / DxeSmmPerformanceLib / DxeSmmPerformanceLib.c
index 3ceda5ef6ee3417933c66411a268c39f0ab52851..d8d06842734570df03bc933c386c2972c248b456 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
-  Performance library instance used in DXE phase to dump SMM performance data.\r
+  Performance library instance used in DXE phase to dump both PEI/DXE and SMM performance data.\r
 \r
-  This library instance allows a DXE driver or UEFI application to dump the SMM performance data.\r
+  This library instance allows a DXE driver or UEFI application to dump both PEI/DXE and SMM performance data.\r
   StartPerformanceMeasurement(), EndPerformanceMeasurement(), StartPerformanceMeasurementEx()\r
   and EndPerformanceMeasurementEx() are not implemented.\r
 \r
-  Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2011 - 2016, 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
@@ -32,17 +32,27 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <Protocol/SmmCommunication.h>\r
 \r
+#include <Guid/PiSmmCommunicationRegionTable.h>\r
+#include <Library/UefiLib.h>\r
+\r
 #define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)  + sizeof (SMM_PERF_COMMUNICATE))\r
-//\r
-// The cached performance protocol interface.\r
-//\r
+\r
 EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication = NULL;\r
-UINT8                           mSmmPerformanceBuffer[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE];\r
+UINT8                           *mSmmPerformanceBuffer;\r
 GAUGE_DATA_ENTRY                *mGaugeData = NULL;\r
 UINTN                           mGaugeNumberOfEntries = 0;\r
 GAUGE_DATA_ENTRY_EX             *mGaugeDataEx = NULL;\r
 UINTN                           mGaugeNumberOfEntriesEx = 0;\r
 \r
+BOOLEAN                         mNoSmmPerfHandler = FALSE;\r
+BOOLEAN                         mNoSmmPerfExHandler = FALSE;\r
+\r
+//\r
+// The cached Performance Protocol and PerformanceEx Protocol interface.\r
+//\r
+PERFORMANCE_PROTOCOL            *mPerformance = NULL;\r
+PERFORMANCE_EX_PROTOCOL         *mPerformanceEx = NULL;\r
+\r
 /**\r
   The function caches the pointer to SMM Communication protocol.\r
 \r
@@ -76,6 +86,51 @@ GetCommunicationProtocol (
   return Status;\r
 }\r
 \r
+/**\r
+  The function caches the pointers to PerformanceEx protocol and Performance Protocol.\r
+\r
+  The function locates PerformanceEx protocol and Performance Protocol from protocol database.\r
+\r
+  @retval EFI_SUCCESS     PerformanceEx protocol or Performance Protocol is successfully located.\r
+  @retval EFI_NOT_FOUND   Both PerformanceEx protocol and Performance Protocol are not located to log performance.\r
+\r
+**/\r
+EFI_STATUS\r
+GetPerformanceProtocol (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  PERFORMANCE_PROTOCOL      *Performance;\r
+  PERFORMANCE_EX_PROTOCOL   *PerformanceEx;\r
+\r
+  if (mPerformanceEx != NULL || mPerformance != NULL) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gPerformanceExProtocolGuid, NULL, (VOID **) &PerformanceEx);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (PerformanceEx != NULL);\r
+    //\r
+    // Cache PerformanceEx Protocol.\r
+    //\r
+    mPerformanceEx = PerformanceEx;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gPerformanceProtocolGuid, NULL, (VOID **) &Performance);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (Performance != NULL);\r
+    //\r
+    // Cache performance protocol.\r
+    //\r
+    mPerformance = Performance;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
 /**\r
   Creates a record for the beginning of a performance measurement.\r
 \r
@@ -114,7 +169,7 @@ StartPerformanceMeasurementEx (
 /**\r
   Fills in the end time of a performance measurement.\r
 \r
-  Looks up the record that matches Handle, Token, Module and Identifier.\r
+  Looks up the record that matches Handle, Token and Module.\r
   If the record can not be found then return RETURN_NOT_FOUND.\r
   If the record is found and TimeStamp is not zero,\r
   then TimeStamp is added to the record as the end time.\r
@@ -214,26 +269,157 @@ EndPerformanceMeasurement (
   return RETURN_SUCCESS;\r
 }\r
 \r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+  It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
+  and then assign the Identifier with 0.\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
+  then no entry is retrieved and zero is returned.  If LogEntryKey is not zero, then the performance\r
+  log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
+  returned.  If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
+  retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
+  log is returned.  If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
+  is retrieved and zero is returned.  In the cases where a performance log entry can be returned,\r
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
+  If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
+  If Handle is NULL, then ASSERT().\r
+  If Token is NULL, then ASSERT().\r
+  If Module is NULL, then ASSERT().\r
+  If StartTimeStamp is NULL, then ASSERT().\r
+  If EndTimeStamp is NULL, then ASSERT().\r
+  If Identifier is NULL, then ASSERT().\r
+\r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component\r
+                                  being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string that identifies the component\r
+                                  being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string that identifies the module\r
+                                  being measured.\r
+  @param  StartTimeStamp          Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was started.\r
+  @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was ended.\r
+  @param  Identifier              Pointer to the 32-bit identifier that was recorded.\r
+\r
+  @return The key for the next performance log entry (in general case).\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetByPerformanceProtocol (\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
+  Status = GetPerformanceProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return 0;\r
+  }\r
+\r
+  if (mPerformanceEx != NULL) {\r
+    Status = mPerformanceEx->GetGaugeEx (LogEntryKey++, &GaugeData);\r
+  } else if (mPerformance != NULL) {\r
+    Status = mPerformance->GetGauge (LogEntryKey++, (GAUGE_DATA_ENTRY **) &GaugeData);\r
+  } else {\r
+    ASSERT (FALSE);\r
+    return 0;\r
+  }\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
+  if (mPerformanceEx != NULL) {\r
+    *Identifier   = GaugeData->Identifier;\r
+  } else {\r
+    *Identifier   = 0;\r
+  }\r
+\r
+  return LogEntryKey;\r
+}\r
+\r
+\r
 /**\r
   Retrieves all previous logged performance measurement.\r
   Function will use SMM communicate protocol to get all previous SMM performance measurement data.\r
   If success, data buffer will be returned. If fail function will return NULL.\r
 \r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+\r
   @retval !NULL           Get all gauge data success.\r
-  @retval NULL            Get all guage data failed.\r
+  @retval NULL            Get all gauge data failed.\r
 **/\r
 GAUGE_DATA_ENTRY *\r
 EFIAPI\r
-GetAllSmmGaugeData (VOID)\r
+GetAllSmmGaugeData (\r
+  IN UINTN      LogEntryKey\r
+  )\r
 {\r
-  EFI_STATUS                  Status;\r
-  EFI_SMM_COMMUNICATE_HEADER  *SmmCommBufferHeader;\r
-  SMM_PERF_COMMUNICATE        *SmmPerfCommData;\r
-  UINTN                       CommSize;\r
-  UINTN                       DataSize;\r
-\r
-  if (mGaugeData != NULL) {\r
-    return mGaugeData;\r
+  EFI_STATUS                                Status;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommBufferHeader;\r
+  SMM_PERF_COMMUNICATE                      *SmmPerfCommData;\r
+  UINTN                                     CommSize;\r
+  UINTN                                     DataSize;\r
+  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE   *PiSmmCommunicationRegionTable;\r
+  UINT32                                    Index;\r
+  EFI_MEMORY_DESCRIPTOR                     *Entry;\r
+  UINT8                                     *Buffer;\r
+  UINTN                                     Size;\r
+  UINTN                                     NumberOfEntries;\r
+  UINTN                                     EntriesGot;\r
+\r
+  if (mNoSmmPerfHandler) {\r
+    //\r
+    // Not try to get the SMM gauge data again\r
+    // if no SMM Performance handler found.\r
+    //\r
+    return NULL;\r
+  }\r
+\r
+  if (LogEntryKey != 0) {\r
+    if (mGaugeData != NULL) {\r
+      return mGaugeData;\r
+    }\r
+  } else {\r
+    //\r
+    // Reget the SMM gauge data at the first entry get.\r
+    //\r
+    if (mGaugeData != NULL) {\r
+      FreePool (mGaugeData);\r
+      mGaugeData = NULL;\r
+      mGaugeNumberOfEntries = 0;\r
+    }\r
   }\r
 \r
   Status = GetCommunicationProtocol ();\r
@@ -241,6 +427,28 @@ GetAllSmmGaugeData (VOID)
     return NULL;\r
   }\r
 \r
+  Status = EfiGetSystemConfigurationTable (\r
+             &gEdkiiPiSmmCommunicationRegionTableGuid,\r
+             (VOID **) &PiSmmCommunicationRegionTable\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+  ASSERT (PiSmmCommunicationRegionTable != NULL);\r
+  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable + 1);\r
+  Size = 0;\r
+  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {\r
+    if (Entry->Type == EfiConventionalMemory) {\r
+      Size = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);\r
+      if (Size >= (SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE + sizeof (GAUGE_DATA_ENTRY))) {\r
+        break;\r
+      }\r
+    }\r
+    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry + PiSmmCommunicationRegionTable->DescriptorSize);\r
+  }\r
+  ASSERT (Index < PiSmmCommunicationRegionTable->NumberOfEntries);\r
+  mSmmPerformanceBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;\r
+\r
   //\r
   // Initialize communicate buffer \r
   //\r
@@ -253,33 +461,49 @@ GetAllSmmGaugeData (VOID)
   CommSize = SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;\r
 \r
   //\r
-  // Get totol number of SMM gauge entries\r
+  // Get total number of SMM gauge entries\r
   //\r
   SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;\r
   Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  if (Status == EFI_NOT_FOUND) {\r
+    mNoSmmPerfHandler = TRUE;\r
+  }\r
   if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {\r
     return NULL;\r
   }\r
 \r
   mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;\r
-  \r
+\r
+  Buffer = mSmmPerformanceBuffer + SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;\r
+  NumberOfEntries = (Size - SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE) / sizeof (GAUGE_DATA_ENTRY);\r
   DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
   mGaugeData = AllocateZeroPool(DataSize);\r
   ASSERT (mGaugeData != NULL);\r
-  \r
+\r
   //\r
   // Get all SMM gauge data\r
   //  \r
   SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;\r
-  SmmPerfCommData->LogEntryKey = 0;\r
-  SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;\r
-  SmmPerfCommData->GaugeData = mGaugeData;\r
-  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
-  if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {\r
-    FreePool (mGaugeData);\r
-    mGaugeData = NULL;\r
-    mGaugeNumberOfEntries = 0;\r
-  }\r
+  SmmPerfCommData->GaugeData = (GAUGE_DATA_ENTRY *) Buffer;\r
+  EntriesGot = 0;\r
+  do {\r
+    SmmPerfCommData->LogEntryKey = EntriesGot;\r
+    if ((mGaugeNumberOfEntries - EntriesGot) >= NumberOfEntries) {\r
+      SmmPerfCommData->NumberOfEntries = NumberOfEntries;\r
+    } else {\r
+      SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries - EntriesGot;\r
+    }\r
+    Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+    if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {\r
+      FreePool (mGaugeData);\r
+      mGaugeData = NULL;\r
+      mGaugeNumberOfEntries = 0;\r
+      return NULL;\r
+    } else {\r
+      CopyMem (&mGaugeData[EntriesGot], Buffer, SmmPerfCommData->NumberOfEntries * sizeof (GAUGE_DATA_ENTRY));\r
+    }\r
+    EntriesGot += SmmPerfCommData->NumberOfEntries;\r
+  } while (EntriesGot < mGaugeNumberOfEntries);\r
 \r
   return mGaugeData;\r
 }\r
@@ -289,21 +513,53 @@ GetAllSmmGaugeData (VOID)
   Function will use SMM communicate protocol to get all previous SMM performance measurement data.\r
   If success, data buffer will be returned. If fail function will return NULL.\r
 \r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+\r
   @retval !NULL           Get all gauge data success.\r
-  @retval NULL            Get all guage data failed.\r
+  @retval NULL            Get all gauge data failed.\r
 **/\r
 GAUGE_DATA_ENTRY_EX *\r
 EFIAPI\r
-GetAllSmmGaugeDataEx (VOID)\r
+GetAllSmmGaugeDataEx (\r
+  IN UINTN      LogEntryKey\r
+  )\r
 {\r
-  EFI_STATUS                  Status;\r
-  EFI_SMM_COMMUNICATE_HEADER  *SmmCommBufferHeader;\r
-  SMM_PERF_COMMUNICATE_EX     *SmmPerfCommData;\r
-  UINTN                       CommSize;\r
-  UINTN                       DataSize;\r
+  EFI_STATUS                                Status;\r
+  EFI_SMM_COMMUNICATE_HEADER                *SmmCommBufferHeader;\r
+  SMM_PERF_COMMUNICATE_EX                   *SmmPerfCommData;\r
+  UINTN                                     CommSize;\r
+  UINTN                                     DataSize;\r
+  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE   *PiSmmCommunicationRegionTable;\r
+  UINT32                                    Index;\r
+  EFI_MEMORY_DESCRIPTOR                     *Entry;\r
+  UINT8                                     *Buffer;\r
+  UINTN                                     Size;\r
+  UINTN                                     NumberOfEntries;\r
+  UINTN                                     EntriesGot;\r
+\r
+  if (mNoSmmPerfExHandler) {\r
+    //\r
+    // Not try to get the SMM gauge data again\r
+    // if no SMM PerformanceEx handler found.\r
+    //\r
+    return NULL;\r
+  }\r
 \r
-  if (mGaugeDataEx != NULL) {\r
-    return mGaugeDataEx;\r
+  if (LogEntryKey != 0) {\r
+    if (mGaugeDataEx != NULL) {\r
+      return mGaugeDataEx;\r
+    }\r
+  } else {\r
+    //\r
+    // Reget the SMM gauge data at the first entry get.\r
+    //\r
+    if (mGaugeDataEx != NULL) {\r
+      FreePool (mGaugeDataEx);\r
+      mGaugeDataEx = NULL;\r
+      mGaugeNumberOfEntriesEx = 0;\r
+    }\r
   }\r
 \r
   Status = GetCommunicationProtocol ();\r
@@ -311,6 +567,27 @@ GetAllSmmGaugeDataEx (VOID)
     return NULL;\r
   }\r
 \r
+  Status = EfiGetSystemConfigurationTable (\r
+             &gEdkiiPiSmmCommunicationRegionTableGuid,\r
+             (VOID **) &PiSmmCommunicationRegionTable\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+  ASSERT (PiSmmCommunicationRegionTable != NULL);\r
+  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable + 1);\r
+  Size = 0;\r
+  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {\r
+    if (Entry->Type == EfiConventionalMemory) {\r
+      Size = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);\r
+      if (Size >= (SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE + sizeof (GAUGE_DATA_ENTRY_EX))) {\r
+        break;\r
+      }\r
+    }\r
+    Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry + PiSmmCommunicationRegionTable->DescriptorSize);\r
+  }\r
+  ASSERT (Index < PiSmmCommunicationRegionTable->NumberOfEntries);\r
+  mSmmPerformanceBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;\r
   //\r
   // Initialize communicate buffer \r
   //\r
@@ -323,34 +600,50 @@ GetAllSmmGaugeDataEx (VOID)
   CommSize = SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;\r
 \r
   //\r
-  // Get totol number of SMM gauge entries\r
+  // Get total number of SMM gauge entries\r
   //\r
   SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;\r
   Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  if (Status == EFI_NOT_FOUND) {\r
+    mNoSmmPerfExHandler = TRUE;\r
+  }\r
   if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {\r
     return NULL;\r
   }\r
 \r
   mGaugeNumberOfEntriesEx = SmmPerfCommData->NumberOfEntries;\r
-  \r
+\r
+  Buffer = mSmmPerformanceBuffer + SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE;\r
+  NumberOfEntries = (Size - SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE) / sizeof (GAUGE_DATA_ENTRY_EX);\r
   DataSize = mGaugeNumberOfEntriesEx * sizeof(GAUGE_DATA_ENTRY_EX);\r
   mGaugeDataEx = AllocateZeroPool(DataSize);\r
   ASSERT (mGaugeDataEx != NULL);\r
-  \r
+\r
   //\r
   // Get all SMM gauge data\r
   //  \r
   SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;\r
-  SmmPerfCommData->LogEntryKey = 0;\r
-  SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntriesEx;\r
-  SmmPerfCommData->GaugeDataEx = mGaugeDataEx;\r
-  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
-  if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {\r
-    FreePool (mGaugeDataEx);\r
-    mGaugeDataEx = NULL;\r
-    mGaugeNumberOfEntriesEx = 0;\r
-  }\r
\r
+  SmmPerfCommData->GaugeDataEx = (GAUGE_DATA_ENTRY_EX *) Buffer;\r
+  EntriesGot = 0;\r
+  do {\r
+    SmmPerfCommData->LogEntryKey = EntriesGot;\r
+    if ((mGaugeNumberOfEntriesEx - EntriesGot) >= NumberOfEntries) {\r
+      SmmPerfCommData->NumberOfEntries = NumberOfEntries;\r
+    } else {\r
+      SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntriesEx - EntriesGot;\r
+    }\r
+    Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+    if (EFI_ERROR (Status) || EFI_ERROR (SmmPerfCommData->ReturnStatus)) {\r
+      FreePool (mGaugeDataEx);\r
+      mGaugeDataEx = NULL;\r
+      mGaugeNumberOfEntriesEx = 0;\r
+      return NULL;\r
+    } else {\r
+      CopyMem (&mGaugeDataEx[EntriesGot], Buffer, SmmPerfCommData->NumberOfEntries * sizeof (GAUGE_DATA_ENTRY_EX));\r
+    }\r
+    EntriesGot += SmmPerfCommData->NumberOfEntries;\r
+  } while (EntriesGot < mGaugeNumberOfEntriesEx);\r
+\r
   return mGaugeDataEx;\r
 }\r
 \r
@@ -418,35 +711,73 @@ GetPerformanceMeasurementEx (
   ASSERT (EndTimeStamp != NULL);\r
   ASSERT (Identifier != NULL);\r
 \r
-  mGaugeDataEx = GetAllSmmGaugeDataEx();\r
+  mGaugeDataEx = GetAllSmmGaugeDataEx (LogEntryKey);\r
   if (mGaugeDataEx != NULL) {\r
-    //\r
-    // Make sure that LogEntryKey is a valid log entry key.\r
-    //\r
-    ASSERT (LogEntryKey <= mGaugeNumberOfEntriesEx);\r
-\r
-    if (LogEntryKey == mGaugeNumberOfEntriesEx) {\r
-      return 0;\r
+    if (LogEntryKey >= mGaugeNumberOfEntriesEx) {\r
+      //\r
+      // Try to get the data by Performance Protocol.\r
+      //\r
+      LogEntryKey = LogEntryKey - mGaugeNumberOfEntriesEx;\r
+      LogEntryKey = GetByPerformanceProtocol (\r
+                      LogEntryKey,\r
+                      Handle,\r
+                      Token,\r
+                      Module,\r
+                      StartTimeStamp,\r
+                      EndTimeStamp,\r
+                      Identifier\r
+                      );\r
+      if (LogEntryKey == 0) {\r
+        //\r
+        // Last entry.\r
+        //\r
+        return LogEntryKey;\r
+      } else {\r
+        return (LogEntryKey + mGaugeNumberOfEntriesEx);\r
+      }\r
     }\r
 \r
     GaugeData = &mGaugeDataEx[LogEntryKey++];\r
     *Identifier = GaugeData->Identifier;\r
   } else {\r
-    mGaugeData = GetAllSmmGaugeData();\r
+    mGaugeData = GetAllSmmGaugeData (LogEntryKey);\r
     if (mGaugeData != NULL) {\r
-      //\r
-      // Make sure that LogEntryKey is a valid log entry key.\r
-      //\r
-      ASSERT (LogEntryKey <= mGaugeNumberOfEntries);\r
-\r
-      if (LogEntryKey == mGaugeNumberOfEntries) {\r
-        return 0;\r
+      if (LogEntryKey >= mGaugeNumberOfEntries) {\r
+        //\r
+        // Try to get the data by Performance Protocol.\r
+        //\r
+        LogEntryKey = LogEntryKey - mGaugeNumberOfEntries;\r
+        LogEntryKey = GetByPerformanceProtocol (\r
+                        LogEntryKey,\r
+                        Handle,\r
+                        Token,\r
+                        Module,\r
+                        StartTimeStamp,\r
+                        EndTimeStamp,\r
+                        Identifier\r
+                        );\r
+        if (LogEntryKey == 0) {\r
+          //\r
+          // Last entry.\r
+          //\r
+          return LogEntryKey;\r
+        } else {\r
+          return (LogEntryKey + mGaugeNumberOfEntries);\r
+        }\r
       }\r
 \r
       GaugeData = (GAUGE_DATA_ENTRY_EX *) &mGaugeData[LogEntryKey++];\r
       *Identifier = 0;\r
     } else {\r
-      return 0;\r
+      return GetByPerformanceProtocol (\r
+               LogEntryKey,\r
+               Handle,\r
+               Token,\r
+               Module,\r
+               StartTimeStamp,\r
+               EndTimeStamp,\r
+               Identifier\r
+               );\r
     }\r
   }\r
 \r