public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v2 01/16] StandaloneMmPkg: StandaloneMmCoreEntryPoint: Extends support for X64
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 02/16] StandaloneMmPkg: StandaloneMmCoreHobLib: Extend support for x64 Mm Core Kun Qin
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Sami Mujawar, Jiewen Yao, Supreeth Venkatesh,
	Jiewen Yao

This change extends StandaloneMmCoreEntryPoint library to support X64
architecture.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
---

Notes:
    v2:
    - Added Reviewed-by tag [Jiewen]

 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/X64/StandaloneMmCoreEntryPoint.c | 71 ++++++++++++++++++++
 StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf   |  3 +
 2 files changed, 74 insertions(+)

diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/X64/StandaloneMmCoreEntryPoint.c b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/X64/StandaloneMmCoreEntryPoint.c
new file mode 100644
index 000000000000..dffa965b8425
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/X64/StandaloneMmCoreEntryPoint.c
@@ -0,0 +1,71 @@
+/** @file
+  Entry point to the Standalone Mm Core.
+
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#include <PiMm.h>
+
+#include <Library/StandaloneMmCoreEntryPoint.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseLib.h>
+
+//
+// Cache copy of HobList pointer.
+//
+VOID *gHobList = NULL;
+
+/**
+  The entry point of PE/COFF Image for the STANDALONE MM Core.
+
+  This function is the entry point for the STANDALONE MM Core. This function is required to call
+  ProcessModuleEntryPointList() and ProcessModuleEntryPointList() is never expected to return.
+  The STANDALONE MM Core is responsible for calling ProcessLibraryConstructorList() as soon as the EFI
+  System Table and the image handle for the STANDALONE MM Core itself have been established.
+  If ProcessModuleEntryPointList() returns, then ASSERT() and halt the system.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+_ModuleEntryPoint (
+  IN VOID  *HobStart
+  )
+{
+  //
+  // Cache a pointer to the HobList
+  //
+  gHobList = HobStart;
+
+  //
+  // Call the Standalone MM Core entry point
+  //
+  ProcessModuleEntryPointList (HobStart);
+
+  //
+  // TODO: Set page table here?? AARCH64 has this step for some reason
+  //
+}
+
+
+/**
+  Required by the EBC compiler and identical in functionality to _ModuleEntryPoint().
+
+  This function is required to call _ModuleEntryPoint() passing in HobStart.
+
+  @param  HobStart  Pointer to the beginning of the HOB List passed in from the PEI Phase.
+
+**/
+VOID
+EFIAPI
+EfiMain (
+  IN VOID  *HobStart
+  )
+{
+  _ModuleEntryPoint (HobStart);
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
index 7d6ee4e08ecb..3b7629bc8fb9 100644
--- a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
@@ -26,6 +26,9 @@ [Sources.AARCH64]
   AArch64/SetPermissions.c
   AArch64/CreateHobList.c
 
+[Sources.X64]
+  X64/StandaloneMmCoreEntryPoint.c
+
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 02/16] StandaloneMmPkg: StandaloneMmCoreHobLib: Extend support for x64 Mm Core
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
  2021-01-05 18:59 ` [PATCH v2 01/16] StandaloneMmPkg: StandaloneMmCoreEntryPoint: Extends support for X64 Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning Kun Qin
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Ard Biesheuvel, Sami Mujawar, Jiewen Yao, Supreeth Venkatesh

This change adds support of x64 version of StandaloneMmCoreHobLib. It
brings in global variable "gHobList" through StandaloneMmCoreEntryPoint,
imports implementation from DxeCoreHobLib.inf to support x64 Mm Core and
moved shared functional plementations into a common file.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Moved common function implementations into Common.c [Jiewen]

 StandaloneMmPkg/Library/StandaloneMmCoreHobLib/{ => AArch64}/StandaloneMmCoreHobLib.c | 278 +-----------------
 StandaloneMmPkg/Library/StandaloneMmCoreHobLib/Common.c                               | 291 +++++++++++++++++++
 StandaloneMmPkg/Library/StandaloneMmCoreHobLib/X64/StandaloneMmCoreHobLib.c           | 299 ++++++++++++++++++++
 StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.inf             |  11 +-
 4 files changed, 599 insertions(+), 280 deletions(-)

diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.c b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/AArch64/StandaloneMmCoreHobLib.c
similarity index 54%
rename from StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.c
rename to StandaloneMmPkg/Library/StandaloneMmCoreHobLib/AArch64/StandaloneMmCoreHobLib.c
index e3d4743b63f2..8c830e805b5c 100644
--- a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.c
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/AArch64/StandaloneMmCoreHobLib.c
@@ -13,196 +13,10 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/HobLib.h>
 #include <Library/DebugLib.h>
 #include <Library/BaseMemoryLib.h>
+#include <Library/StandaloneMmCoreEntryPoint.h>
 
 #include <Guid/MemoryAllocationHob.h>
 
-//
-// Cache copy of HobList pointer.
-//
-VOID *gHobList = NULL;
-
-/**
-  Returns the pointer to the HOB list.
-
-  This function returns the pointer to first HOB in the list.
-  If the pointer to the HOB list is NULL, then ASSERT().
-
-  @return The pointer to the HOB list.
-
-**/
-VOID *
-EFIAPI
-GetHobList (
-  VOID
-  )
-{
-  ASSERT (gHobList != NULL);
-  return gHobList;
-}
-
-/**
-  Returns the next instance of a HOB type from the starting HOB.
-
-  This function searches the first instance of a HOB type from the starting HOB pointer.
-  If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
-  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
-  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
-  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
-
-  If HobStart is NULL, then ASSERT().
-
-  @param  Type          The HOB type to return.
-  @param  HobStart      The starting HOB pointer to search from.
-
-  @return The next instance of a HOB type from the starting HOB.
-
-**/
-VOID *
-EFIAPI
-GetNextHob (
-  IN UINT16                 Type,
-  IN CONST VOID             *HobStart
-  )
-{
-  EFI_PEI_HOB_POINTERS  Hob;
-
-  ASSERT (HobStart != NULL);
-
-  Hob.Raw = (UINT8 *) HobStart;
-  //
-  // Parse the HOB list until end of list or matching type is found.
-  //
-  while (!END_OF_HOB_LIST (Hob)) {
-    if (Hob.Header->HobType == Type) {
-      return Hob.Raw;
-    }
-    Hob.Raw = GET_NEXT_HOB (Hob);
-  }
-  return NULL;
-}
-
-/**
-  Returns the first instance of a HOB type among the whole HOB list.
-
-  This function searches the first instance of a HOB type among the whole HOB list.
-  If there does not exist such HOB type in the HOB list, it will return NULL.
-
-  If the pointer to the HOB list is NULL, then ASSERT().
-
-  @param  Type          The HOB type to return.
-
-  @return The next instance of a HOB type from the starting HOB.
-
-**/
-VOID *
-EFIAPI
-GetFirstHob (
-  IN UINT16                 Type
-  )
-{
-  VOID      *HobList;
-
-  HobList = GetHobList ();
-  return GetNextHob (Type, HobList);
-}
-
-/**
-  Returns the next instance of the matched GUID HOB from the starting HOB.
-
-  This function searches the first instance of a HOB from the starting HOB pointer.
-  Such HOB should satisfy two conditions:
-  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
-  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
-  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
-  to extract the data section and its size information, respectively.
-  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
-  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
-  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
-
-  If Guid is NULL, then ASSERT().
-  If HobStart is NULL, then ASSERT().
-
-  @param  Guid          The GUID to match with in the HOB list.
-  @param  HobStart      A pointer to a Guid.
-
-  @return The next instance of the matched GUID HOB from the starting HOB.
-
-**/
-VOID *
-EFIAPI
-GetNextGuidHob (
-  IN CONST EFI_GUID         *Guid,
-  IN CONST VOID             *HobStart
-  )
-{
-  EFI_PEI_HOB_POINTERS  GuidHob;
-
-  GuidHob.Raw = (UINT8 *) HobStart;
-  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
-    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
-      break;
-    }
-    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
-  }
-  return GuidHob.Raw;
-}
-
-/**
-  Returns the first instance of the matched GUID HOB among the whole HOB list.
-
-  This function searches the first instance of a HOB among the whole HOB list.
-  Such HOB should satisfy two conditions:
-  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
-  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
-  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
-  to extract the data section and its size information, respectively.
-
-  If the pointer to the HOB list is NULL, then ASSERT().
-  If Guid is NULL, then ASSERT().
-
-  @param  Guid          The GUID to match with in the HOB list.
-
-  @return The first instance of the matched GUID HOB among the whole HOB list.
-
-**/
-VOID *
-EFIAPI
-GetFirstGuidHob (
-  IN CONST EFI_GUID         *Guid
-  )
-{
-  VOID      *HobList;
-
-  HobList = GetHobList ();
-  return GetNextGuidHob (Guid, HobList);
-}
-
-/**
-  Get the system boot mode from the HOB list.
-
-  This function returns the system boot mode information from the
-  PHIT HOB in HOB list.
-
-  If the pointer to the HOB list is NULL, then ASSERT().
-
-  @param  VOID
-
-  @return The Boot Mode.
-
-**/
-EFI_BOOT_MODE
-EFIAPI
-GetBootModeHob (
-  VOID
-  )
-{
-  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
-
-  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
-
-  return HandOffHob->BootMode;
-}
-
 VOID *
 CreateHob (
   IN  UINT16    HobType,
@@ -510,93 +324,3 @@ BuildMemoryAllocationHob (
   //
   ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
 }
-
-/**
-  Builds a HOB that describes a chunk of system memory with Owner GUID.
-
-  This function builds a HOB that describes a chunk of system memory.
-  If there is no additional space for HOB creation, then ASSERT().
-
-  @param  ResourceType        The type of resource described by this HOB.
-  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
-  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
-  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
-  @param  OwnerGUID           GUID for the owner of this resource.
-
-**/
-VOID
-EFIAPI
-BuildResourceDescriptorWithOwnerHob (
-  IN EFI_RESOURCE_TYPE            ResourceType,
-  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
-  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
-  IN UINT64                       NumberOfBytes,
-  IN EFI_GUID                     *OwnerGUID
-  )
-{
-  ASSERT (FALSE);
-}
-
-/**
-  Builds a Capsule Volume HOB.
-
-  This function builds a Capsule Volume HOB.
-  If the platform does not support Capsule Volume HOBs, then ASSERT().
-  If there is no additional space for HOB creation, then ASSERT().
-
-  @param  BaseAddress   The base address of the Capsule Volume.
-  @param  Length        The size of the Capsule Volume in bytes.
-
-**/
-VOID
-EFIAPI
-BuildCvHob (
-  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
-  IN UINT64                      Length
-  )
-{
-  ASSERT (FALSE);
-}
-
-
-/**
-  Builds a HOB for the BSP store.
-
-  This function builds a HOB for BSP store.
-  If there is no additional space for HOB creation, then ASSERT().
-
-  @param  BaseAddress   The 64 bit physical address of the BSP.
-  @param  Length        The length of the BSP store in bytes.
-  @param  MemoryType    Type of memory allocated by this HOB.
-
-**/
-VOID
-EFIAPI
-BuildBspStoreHob (
-  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
-  IN UINT64                      Length,
-  IN EFI_MEMORY_TYPE             MemoryType
-  )
-{
-  ASSERT (FALSE);
-}
-
-/**
-  Builds a HOB for the Stack.
-
-  This function builds a HOB for the stack.
-  If there is no additional space for HOB creation, then ASSERT().
-
-  @param  BaseAddress   The 64 bit physical address of the Stack.
-  @param  Length        The length of the stack in bytes.
-
-**/
-VOID
-EFIAPI
-BuildStackHob (
-  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
-  IN UINT64                      Length
-  )
-{
-  ASSERT (FALSE);
-}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/Common.c b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/Common.c
new file mode 100644
index 000000000000..8c535032315a
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/Common.c
@@ -0,0 +1,291 @@
+/** @file
+  HOB Library implementation for Standalone MM Core.
+
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/HobLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/StandaloneMmCoreEntryPoint.h>
+
+#include <Guid/MemoryAllocationHob.h>
+
+/**
+  Returns the pointer to the HOB list.
+
+  This function returns the pointer to first HOB in the list.
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @return The pointer to the HOB list.
+
+**/
+VOID *
+EFIAPI
+GetHobList (
+  VOID
+  )
+{
+  ASSERT (gHobList != NULL);
+  return gHobList;
+}
+
+/**
+  Returns the next instance of a HOB type from the starting HOB.
+
+  This function searches the first instance of a HOB type from the starting HOB pointer.
+  If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
+  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+
+  If HobStart is NULL, then ASSERT().
+
+  @param  Type          The HOB type to return.
+  @param  HobStart      The starting HOB pointer to search from.
+
+  @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextHob (
+  IN UINT16                 Type,
+  IN CONST VOID             *HobStart
+  )
+{
+  EFI_PEI_HOB_POINTERS  Hob;
+
+  ASSERT (HobStart != NULL);
+
+  Hob.Raw = (UINT8 *) HobStart;
+  //
+  // Parse the HOB list until end of list or matching type is found.
+  //
+  while (!END_OF_HOB_LIST (Hob)) {
+    if (Hob.Header->HobType == Type) {
+      return Hob.Raw;
+    }
+    Hob.Raw = GET_NEXT_HOB (Hob);
+  }
+  return NULL;
+}
+
+/**
+  Returns the first instance of a HOB type among the whole HOB list.
+
+  This function searches the first instance of a HOB type among the whole HOB list.
+  If there does not exist such HOB type in the HOB list, it will return NULL.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @param  Type          The HOB type to return.
+
+  @return The next instance of a HOB type from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetFirstHob (
+  IN UINT16                 Type
+  )
+{
+  VOID      *HobList;
+
+  HobList = GetHobList ();
+  return GetNextHob (Type, HobList);
+}
+
+/**
+  Returns the next instance of the matched GUID HOB from the starting HOB.
+
+  This function searches the first instance of a HOB from the starting HOB pointer.
+  Such HOB should satisfy two conditions:
+  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
+  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
+  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+  to extract the data section and its size information, respectively.
+  In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
+  unconditionally: it returns HobStart back if HobStart itself meets the requirement;
+  caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
+
+  If Guid is NULL, then ASSERT().
+  If HobStart is NULL, then ASSERT().
+
+  @param  Guid          The GUID to match with in the HOB list.
+  @param  HobStart      A pointer to a Guid.
+
+  @return The next instance of the matched GUID HOB from the starting HOB.
+
+**/
+VOID *
+EFIAPI
+GetNextGuidHob (
+  IN CONST EFI_GUID         *Guid,
+  IN CONST VOID             *HobStart
+  )
+{
+  EFI_PEI_HOB_POINTERS  GuidHob;
+
+  GuidHob.Raw = (UINT8 *) HobStart;
+  while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
+    if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
+      break;
+    }
+    GuidHob.Raw = GET_NEXT_HOB (GuidHob);
+  }
+  return GuidHob.Raw;
+}
+
+/**
+  Returns the first instance of the matched GUID HOB among the whole HOB list.
+
+  This function searches the first instance of a HOB among the whole HOB list.
+  Such HOB should satisfy two conditions:
+  its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
+  If such a HOB from the starting HOB pointer does not exist, it will return NULL.
+  Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
+  to extract the data section and its size information, respectively.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+  If Guid is NULL, then ASSERT().
+
+  @param  Guid          The GUID to match with in the HOB list.
+
+  @return The first instance of the matched GUID HOB among the whole HOB list.
+
+**/
+VOID *
+EFIAPI
+GetFirstGuidHob (
+  IN CONST EFI_GUID         *Guid
+  )
+{
+  VOID      *HobList;
+
+  HobList = GetHobList ();
+  return GetNextGuidHob (Guid, HobList);
+}
+
+/**
+  Get the system boot mode from the HOB list.
+
+  This function returns the system boot mode information from the
+  PHIT HOB in HOB list.
+
+  If the pointer to the HOB list is NULL, then ASSERT().
+
+  @param  VOID
+
+  @return The Boot Mode.
+
+**/
+EFI_BOOT_MODE
+EFIAPI
+GetBootModeHob (
+  VOID
+  )
+{
+  EFI_HOB_HANDOFF_INFO_TABLE    *HandOffHob;
+
+  HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();
+
+  return HandOffHob->BootMode;
+}
+
+
+/**
+  Builds a HOB that describes a chunk of system memory with Owner GUID.
+
+  This function builds a HOB that describes a chunk of system memory.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ResourceType        The type of resource described by this HOB.
+  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
+  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
+  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
+  @param  OwnerGUID           GUID for the owner of this resource.
+
+**/
+VOID
+EFIAPI
+BuildResourceDescriptorWithOwnerHob (
+  IN EFI_RESOURCE_TYPE            ResourceType,
+  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
+  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
+  IN UINT64                       NumberOfBytes,
+  IN EFI_GUID                     *OwnerGUID
+  )
+{
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a Capsule Volume HOB.
+
+  This function builds a Capsule Volume HOB.
+  If the platform does not support Capsule Volume HOBs, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The base address of the Capsule Volume.
+  @param  Length        The size of the Capsule Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildCvHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  ASSERT (FALSE);
+}
+
+
+/**
+  Builds a HOB for the BSP store.
+
+  This function builds a HOB for BSP store.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the BSP.
+  @param  Length        The length of the BSP store in bytes.
+  @param  MemoryType    Type of memory allocated by this HOB.
+
+**/
+VOID
+EFIAPI
+BuildBspStoreHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length,
+  IN EFI_MEMORY_TYPE             MemoryType
+  )
+{
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a HOB for the Stack.
+
+  This function builds a HOB for the stack.
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the Stack.
+  @param  Length        The length of the stack in bytes.
+
+**/
+VOID
+EFIAPI
+BuildStackHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  ASSERT (FALSE);
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/X64/StandaloneMmCoreHobLib.c b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/X64/StandaloneMmCoreHobLib.c
new file mode 100644
index 000000000000..10b0ef586894
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/X64/StandaloneMmCoreHobLib.c
@@ -0,0 +1,299 @@
+/** @file
+  HOB Library implementation for Standalone MM Core.
+
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/HobLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/StandaloneMmCoreEntryPoint.h>
+
+#include <Guid/MemoryAllocationHob.h>
+
+/**
+  Builds a HOB for a loaded PE32 module.
+
+  This function builds a HOB for a loaded PE32 module.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If ModuleName is NULL, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ModuleName              The GUID File Name of the module.
+  @param  MemoryAllocationModule  The 64 bit physical address of the module.
+  @param  ModuleLength            The length of the module in bytes.
+  @param  EntryPoint              The 64 bit physical address of the module entry point.
+
+**/
+VOID
+EFIAPI
+BuildModuleHob (
+  IN CONST EFI_GUID         *ModuleName,
+  IN EFI_PHYSICAL_ADDRESS   MemoryAllocationModule,
+  IN UINT64                 ModuleLength,
+  IN EFI_PHYSICAL_ADDRESS   EntryPoint
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a HOB that describes a chunk of system memory.
+
+  This function builds a HOB that describes a chunk of system memory.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  ResourceType        The type of resource described by this HOB.
+  @param  ResourceAttribute   The resource attributes of the memory described by this HOB.
+  @param  PhysicalStart       The 64 bit physical address of memory described by this HOB.
+  @param  NumberOfBytes       The length of the memory described by this HOB in bytes.
+
+**/
+VOID
+EFIAPI
+BuildResourceDescriptorHob (
+  IN EFI_RESOURCE_TYPE            ResourceType,
+  IN EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttribute,
+  IN EFI_PHYSICAL_ADDRESS         PhysicalStart,
+  IN UINT64                       NumberOfBytes
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a customized HOB tagged with a GUID for identification and returns
+  the start address of GUID HOB data.
+
+  This function builds a customized HOB tagged with a GUID for identification
+  and returns the start address of GUID HOB data so that caller can fill the customized data.
+  The HOB Header and Name field is already stripped.
+  It can only be invoked during PEI phase.
+  For MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If Guid is NULL, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+  If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+  HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
+
+  @param  Guid          The GUID to tag the customized HOB.
+  @param  DataLength    The size of the data payload for the GUID HOB.
+
+  @retval  NULL         The GUID HOB could not be allocated.
+  @retval  others       The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidHob (
+  IN CONST EFI_GUID              *Guid,
+  IN UINTN                       DataLength
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+  return NULL;
+}
+
+/**
+  Builds a customized HOB tagged with a GUID for identification, copies the input data to the HOB
+  data field, and returns the start address of the GUID HOB data.
+
+  This function builds a customized HOB tagged with a GUID for identification and copies the input
+  data to the HOB data field and returns the start address of the GUID HOB data.  It can only be
+  invoked during PEI phase; for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+  The HOB Header and Name field is already stripped.
+  It can only be invoked during PEI phase.
+  For MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If Guid is NULL, then ASSERT().
+  If Data is NULL and DataLength > 0, then ASSERT().
+  If there is no additional space for HOB creation, then ASSERT().
+  If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
+  HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
+
+  @param  Guid          The GUID to tag the customized HOB.
+  @param  Data          The data to be copied into the data field of the GUID HOB.
+  @param  DataLength    The size of the data payload for the GUID HOB.
+
+  @retval  NULL         The GUID HOB could not be allocated.
+  @retval  others       The start address of GUID HOB data.
+
+**/
+VOID *
+EFIAPI
+BuildGuidDataHob (
+  IN CONST EFI_GUID              *Guid,
+  IN VOID                        *Data,
+  IN UINTN                       DataLength
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+  return NULL;
+}
+
+/**
+  Builds a Firmware Volume HOB.
+
+  This function builds a Firmware Volume HOB.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+  If the FvImage buffer is not at its required alignment, then ASSERT().
+
+  @param  BaseAddress   The base address of the Firmware Volume.
+  @param  Length        The size of the Firmware Volume in bytes.
+
+**/
+VOID
+EFIAPI
+BuildFvHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a EFI_HOB_TYPE_FV2 HOB.
+
+  This function builds a EFI_HOB_TYPE_FV2 HOB.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+  If the FvImage buffer is not at its required alignment, then ASSERT().
+
+  @param  BaseAddress   The base address of the Firmware Volume.
+  @param  Length        The size of the Firmware Volume in bytes.
+  @param  FvName        The name of the Firmware Volume.
+  @param  FileName      The name of the file.
+
+**/
+VOID
+EFIAPI
+BuildFv2Hob (
+  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN          UINT64                      Length,
+  IN CONST    EFI_GUID                    *FvName,
+  IN CONST    EFI_GUID                    *FileName
+  )
+{
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a EFI_HOB_TYPE_FV3 HOB.
+
+  This function builds a EFI_HOB_TYPE_FV3 HOB.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() since PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+  If the FvImage buffer is not at its required alignment, then ASSERT().
+
+  @param BaseAddress            The base address of the Firmware Volume.
+  @param Length                 The size of the Firmware Volume in bytes.
+  @param AuthenticationStatus   The authentication status.
+  @param ExtractedFv            TRUE if the FV was extracted as a file within
+                                another firmware volume. FALSE otherwise.
+  @param FvName                 The name of the Firmware Volume.
+                                Valid only if IsExtractedFv is TRUE.
+  @param FileName               The name of the file.
+                                Valid only if IsExtractedFv is TRUE.
+
+**/
+VOID
+EFIAPI
+BuildFv3Hob (
+  IN          EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN          UINT64                      Length,
+  IN          UINT32                      AuthenticationStatus,
+  IN          BOOLEAN                     ExtractedFv,
+  IN CONST    EFI_GUID                    *FvName, OPTIONAL
+  IN CONST    EFI_GUID                    *FileName OPTIONAL
+  )
+{
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a HOB for the CPU.
+
+  This function builds a HOB for the CPU.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  SizeOfMemorySpace   The maximum physical memory addressability of the processor.
+  @param  SizeOfIoSpace       The maximum physical I/O addressability of the processor.
+
+**/
+VOID
+EFIAPI
+BuildCpuHob (
+  IN UINT8                       SizeOfMemorySpace,
+  IN UINT8                       SizeOfIoSpace
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+}
+
+/**
+  Builds a HOB for the memory allocation.
+
+  This function builds a HOB for the memory allocation.
+  It can only be invoked during PEI phase;
+  for MM phase, it will ASSERT() because PEI HOB is read-only for MM phase.
+
+  If there is no additional space for HOB creation, then ASSERT().
+
+  @param  BaseAddress   The 64 bit physical address of the memory.
+  @param  Length        The length of the memory allocation in bytes.
+  @param  MemoryType    Type of memory allocated by this HOB.
+
+**/
+VOID
+EFIAPI
+BuildMemoryAllocationHob (
+  IN EFI_PHYSICAL_ADDRESS        BaseAddress,
+  IN UINT64                      Length,
+  IN EFI_MEMORY_TYPE             MemoryType
+  )
+{
+  //
+  // PEI HOB is read only for MM phase
+  //
+  ASSERT (FALSE);
+}
diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.inf b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.inf
index 0046cd804def..a2559920e887 100644
--- a/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.inf
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreHobLib/StandaloneMmCoreHobLib.inf
@@ -22,16 +22,21 @@ [Defines]
   LIBRARY_CLASS                  = HobLib|MM_CORE_STANDALONE
 
 #
-#  VALID_ARCHITECTURES           = AARCH64
+#  VALID_ARCHITECTURES           = X64 AARCH64
 #
-[Sources.Common]
-  StandaloneMmCoreHobLib.c
+[Sources.common]
+  Common.c
+
+[Sources.X64]
+  X64/StandaloneMmCoreHobLib.c
 
 [Sources.AARCH64]
+  AArch64/StandaloneMmCoreHobLib.c
   AArch64/StandaloneMmCoreHobLibInternal.c
 
 [Packages]
   MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
 
 
 [LibraryClasses]
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
  2021-01-05 18:59 ` [PATCH v2 01/16] StandaloneMmPkg: StandaloneMmCoreEntryPoint: Extends support for X64 Kun Qin
  2021-01-05 18:59 ` [PATCH v2 02/16] StandaloneMmPkg: StandaloneMmCoreHobLib: Extend support for x64 Mm Core Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:33   ` Yao, Jiewen
  2021-01-05 18:59 ` [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Kun Qin
                   ` (14 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Ard Biesheuvel, Sami Mujawar, Jiewen Yao, Supreeth Venkatesh

Assigning MmramRangeCount from MmCorePrivate (UINT64) to local variable
MmramRangeCount (UINT32) will cause compilation failure due to "warning
C4244: '=': conversion from 'UINT64' to 'UINT32', possible loss of data".
This changes defines local MmramRangeCount as UINTN type and adds type
cast before value assignment.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Changed variable type to UINTN and cast before assignments [Jiewen]

 StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
index eb3d772e18c5..c6feb25a3821 100644
--- a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
+++ b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c
@@ -841,7 +841,7 @@ MemoryAllocationLibConstructor (
   VOID                            *HobStart;
   EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
   EFI_MMRAM_DESCRIPTOR            *MmramRanges;
-  UINT32                           MmramRangeCount;
+  UINTN                            MmramRangeCount;
   EFI_HOB_GUID_TYPE               *MmramRangesHob;
 
   HobStart = GetHobList ();
@@ -868,7 +868,7 @@ MemoryAllocationLibConstructor (
       return EFI_UNSUPPORTED;
     }
 
-    MmramRangeCount = MmramRangesHobData->NumberOfMmReservedRegions;
+    MmramRangeCount = (UINTN) MmramRangesHobData->NumberOfMmReservedRegions;
     if (MmramRanges == NULL) {
       return EFI_UNSUPPORTED;
     }
@@ -877,7 +877,7 @@ MemoryAllocationLibConstructor (
     DataInHob      = GET_GUID_HOB_DATA (GuidHob);
     MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob->Address;
     MmramRanges     = (EFI_MMRAM_DESCRIPTOR *)(UINTN)MmCorePrivate->MmramRanges;
-    MmramRangeCount = MmCorePrivate->MmramRangeCount;
+    MmramRangeCount = (UINTN) MmCorePrivate->MmramRangeCount;
   }
 
   {
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (2 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:38   ` Yao, Jiewen
  2021-01-05 18:59 ` [PATCH v2 05/16] MdeModulePkg: SmmLockBoxSmmLib: Support StandaloneMm for SmmLockBoxLib Kun Qin
                   ` (13 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Ard Biesheuvel, Sami Mujawar, Jiewen Yao, Supreeth Venkatesh

This change extends StandaloneMmMemLib library to support X64
architecture. The implementation is ported from MdePkg/Library/SmmMemLib.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Added routine to fix bug of not initializing MmRanges [Jiewen]
    - Extends support to x86 instead of x64 only [Hao]

 StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c |  26 ++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c                 |  52 +++++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c      | 155 ++++++++++++++++++++
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf               |  13 +-
 4 files changed, 245 insertions(+), 1 deletion(-)

diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c
index cb7c5e677a6b..46dfce5cac86 100644
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c
@@ -40,4 +40,30 @@ MmMemLibInternalCalculateMaximumSupportAddress (
   DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
 }
 
+/**
+  Initialize cached Mmram Ranges from HOB.
+
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.
+  @retval EFI_SUCCESS       MmRanges are populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  )
+{
+  // Not implemented for AARCH64.
+}
+
+/**
+  Deinitialize cached Mmram Ranges.
+
+**/
+VOID
+MmMemLibInternalFreeMmramRanges (
+  VOID
+  )
+{
+  // Not implemented for AARCH64.
+}
 
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
index f82cdb3ba816..f43af2b1cc9b 100644
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
@@ -37,6 +37,27 @@ MmMemLibInternalCalculateMaximumSupportAddress (
   VOID
   );
 
+/**
+  Initialize cached Mmram Ranges from HOB.
+
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.
+  @retval EFI_SUCCESS       MmRanges are populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  );
+
+/**
+  Deinitialize cached Mmram Ranges.
+
+**/
+VOID
+MmMemLibInternalFreeMmramRanges (
+  VOID
+  );
+
 /**
   This function check if the buffer is valid per processor architecture and not overlap with MMRAM.
 
@@ -253,11 +274,42 @@ MemLibConstructor (
   IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
   )
 {
+  EFI_STATUS Status;
 
   //
   // Calculate and save maximum support address
   //
   MmMemLibInternalCalculateMaximumSupportAddress ();
 
+  //
+  // Initialize cached Mmram Ranges from HOB.
+  //
+  Status = MmMemLibInternalPopulateMmramRanges ();
+
+  return Status;
+}
+
+/**
+  Destructor for the library.  free any resources for Mm Mem library
+
+  @param ImageHandle    The image handle of the process.
+  @param SystemTable    The EFI System Table pointer.
+
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+MemLibDestructor (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
+  )
+{
+
+  //
+  // Deinitialize cached Mmram Ranges.
+  //
+  MmMemLibInternalFreeMmramRanges ();
+
   return EFI_SUCCESS;
 }
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c
new file mode 100644
index 000000000000..1a978541716a
--- /dev/null
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c
@@ -0,0 +1,155 @@
+/** @file
+  Internal ARCH Specific file of MM memory check library.
+
+  MM memory check library implementation. This library consumes MM_ACCESS_PROTOCOL
+  to get MMRAM information. In order to use this library instance, the platform should produce
+  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
+  and MM driver) and/or specific dedicated hardware.
+
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+#include <PiMm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+
+#include <Guid/MmCoreData.h>
+#include <Guid/MmramMemoryReserve.h>
+
+//
+// Maximum support address used to check input buffer
+//
+extern EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress;
+extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
+extern UINTN                 mMmMemLibInternalMmramCount;
+
+/**
+  Calculate and save the maximum support address.
+
+**/
+VOID
+MmMemLibInternalCalculateMaximumSupportAddress (
+  VOID
+  )
+{
+  VOID         *Hob;
+  UINT32       RegEax;
+  UINT8        PhysicalAddressBits;
+
+  //
+  // Get physical address bits supported.
+  //
+  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
+  if (Hob != NULL) {
+    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
+  } else {
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
+    if (RegEax >= 0x80000008) {
+      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
+      PhysicalAddressBits = (UINT8) RegEax;
+    } else {
+      PhysicalAddressBits = 36;
+    }
+  }
+  //
+  // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (PhysicalAddressBits > 48) {
+    PhysicalAddressBits = 48;
+  }
+
+  //
+  // Save the maximum support address in one global variable
+  //
+  mMmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
+  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
+}
+
+/**
+  Initialize cached Mmram Ranges from HOB.
+
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.
+  @retval EFI_SUCCESS       MmRanges are populated successfully.
+
+**/
+EFI_STATUS
+MmMemLibInternalPopulateMmramRanges (
+  VOID
+  )
+{
+  VOID                            *HobStart;
+  EFI_HOB_GUID_TYPE               *GuidHob;
+  MM_CORE_DATA_HOB_DATA           *DataInHob;
+  MM_CORE_PRIVATE_DATA            *MmCorePrivateData;
+  EFI_HOB_GUID_TYPE               *MmramRangesHob;
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
+  EFI_MMRAM_DESCRIPTOR            *MmramDescriptors;
+
+  HobStart = GetHobList ();
+  DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart));
+
+  //
+  // Extract MM Core Private context from the Hob. If absent search for
+  // a Hob containing the MMRAM ranges
+  //
+  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
+  if (GuidHob == NULL) {
+    MmramRangesHob = GetFirstGuidHob (&gEfiMmPeiMmramMemoryReserveGuid);
+    if (MmramRangesHob == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
+    if (MmramRangesHobData == NULL || MmramRangesHobData->Descriptor == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    mMmMemLibInternalMmramCount = MmramRangesHobData->NumberOfMmReservedRegions;
+    MmramDescriptors = MmramRangesHobData->Descriptor;
+  } else {
+    DataInHob = GET_GUID_HOB_DATA (GuidHob);
+    if (DataInHob == NULL) {
+      return EFI_UNSUPPORTED;
+    }
+
+    MmCorePrivateData = (MM_CORE_PRIVATE_DATA *) (UINTN) DataInHob->Address;
+    if (MmCorePrivateData == NULL || MmCorePrivateData->MmramRanges == 0) {
+      return EFI_UNSUPPORTED;
+    }
+
+    mMmMemLibInternalMmramCount = (UINTN) MmCorePrivateData->MmramRangeCount;
+    MmramDescriptors = (EFI_MMRAM_DESCRIPTOR *) (UINTN) MmCorePrivateData->MmramRanges;
+  }
+
+  mMmMemLibInternalMmramRanges = AllocatePool (mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));
+  if (mMmMemLibInternalMmramRanges) {
+    CopyMem (mMmMemLibInternalMmramRanges,
+             MmramDescriptors,
+             mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Deinitialize cached Mmram Ranges.
+
+**/
+VOID
+MmMemLibInternalFreeMmramRanges (
+  VOID
+  )
+{
+  if (mMmMemLibInternalMmramRanges != NULL) {
+    FreePool (mMmMemLibInternalMmramRanges);
+  }
+}
+
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf
index 49da02e54e6d..062b0d7a1161 100644
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf
@@ -8,6 +8,7 @@
 #
 #  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
 #  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -22,16 +23,20 @@ [Defines]
   PI_SPECIFICATION_VERSION       = 0x00010032
   LIBRARY_CLASS                  = MemLib|MM_STANDALONE MM_CORE_STANDALONE
   CONSTRUCTOR                    = MemLibConstructor
+  DESTRUCTOR                     = MemLibDestructor
 
 #
 # The following information is for reference only and not required by the build tools.
 #
-#  VALID_ARCHITECTURES           = AARCH64
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
 #
 
 [Sources.Common]
   StandaloneMmMemLib.c
 
+[Sources.IA32, Sources.X64]
+  X86StandaloneMmMemLibInternal.c
+
 [Sources.AARCH64]
   AArch64/StandaloneMmMemLibInternal.c
 
@@ -42,3 +47,9 @@ [Packages]
 [LibraryClasses]
   BaseMemoryLib
   DebugLib
+  HobLib
+  MemoryAllocationLib
+
+[Guids]
+  gMmCoreDataHobGuid                  ## SOMETIMES_CONSUMES ## HOB
+  gEfiMmPeiMmramMemoryReserveGuid     ## SOMETIMES_CONSUMES ## HOB
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 05/16] MdeModulePkg: SmmLockBoxSmmLib: Support StandaloneMm for SmmLockBoxLib
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (3 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm Kun Qin
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Eric Dong

This change added support of StandaloneMm for SmmLockBoxLib. It replaces
gSmst with gMmst to support both traditional MM and standalone MM. The
contructor and desctructor functions are abstracted to support different
function prototype definitions.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Eric Dong <eric.dong@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internal library functions [Hao]
    - Added Reviewed-by tag [Hao]

 MdeModulePkg/Library/SmmLockBoxLib/{SmmLockBoxSmmLib.c => SmmLockBoxMmLib.c}               | 82 +++++++++-----------
 MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.c                             | 53 +++++++++++++
 MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxTraditionalMmLib.c                            | 53 +++++++++++++
 MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxLibPrivate.h                                  | 25 ++++++
 MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf                                    | 15 ++--
 MdeModulePkg/Library/SmmLockBoxLib/{SmmLockBoxSmmLib.inf => SmmLockBoxStandaloneMmLib.inf} | 26 ++++---
 MdeModulePkg/MdeModulePkg.dsc                                                              |  2 +
 7 files changed, 191 insertions(+), 65 deletions(-)

diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.c b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxMmLib.c
similarity index 89%
rename from MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.c
rename to MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxMmLib.c
index 4cc0e8b78e5b..a709851806eb 100644
--- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.c
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxMmLib.c
@@ -6,16 +6,16 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <PiSmm.h>
-#include <Library/SmmServicesTableLib.h>
+#include <PiMm.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/LockBoxLib.h>
 #include <Library/DebugLib.h>
 #include <Guid/SmmLockBox.h>
 #include <Guid/EndOfS3Resume.h>
-#include <Protocol/SmmReadyToLock.h>
-#include <Protocol/SmmEndOfDxe.h>
+#include <Protocol/MmReadyToLock.h>
+#include <Protocol/MmEndOfDxe.h>
 #include <Protocol/SmmSxDispatch2.h>
 
 #include "SmmLockBoxLibPrivate.h"
@@ -49,13 +49,13 @@ InternalGetSmmLockBoxContext (
   //
   // Check if gEfiSmmLockBoxCommunicationGuid is installed by someone
   //
-  for (Index = 0; Index < gSmst->NumberOfTableEntries; Index++) {
-    if (CompareGuid (&gSmst->SmmConfigurationTable[Index].VendorGuid, &gEfiSmmLockBoxCommunicationGuid)) {
+  for (Index = 0; Index < gMmst->NumberOfTableEntries; Index++) {
+    if (CompareGuid (&gMmst->MmConfigurationTable[Index].VendorGuid, &gEfiSmmLockBoxCommunicationGuid)) {
       //
       // Found. That means some other library instance is already run.
       // No need to install again, just return.
       //
-      return (SMM_LOCK_BOX_CONTEXT *)gSmst->SmmConfigurationTable[Index].VendorTable;
+      return (SMM_LOCK_BOX_CONTEXT *)gMmst->MmConfigurationTable[Index].VendorTable;
     }
   }
 
@@ -142,8 +142,8 @@ SmmLockBoxSmmEndOfDxeNotify (
   //
   // Locate SmmSxDispatch2 protocol.
   //
-  Status = gSmst->SmmLocateProtocol (
-                    &gEfiSmmSxDispatch2ProtocolGuid,
+  Status = gMmst->MmLocateProtocol (
+                    &gEfiMmSxDispatchProtocolGuid,
                     NULL,
                     (VOID **)&SxDispatch
                     );
@@ -191,29 +191,24 @@ SmmLockBoxEndOfS3ResumeNotify (
   Constructor for SmmLockBox library.
   This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later.
 
-  @param[in] ImageHandle  Image handle of this driver.
-  @param[in] SystemTable  A Pointer to the EFI System Table.
-
   @retval EFI_SUCEESS
   @return Others          Some error occurs.
 **/
 EFI_STATUS
-EFIAPI
-SmmLockBoxSmmConstructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
+SmmLockBoxMmConstructor (
+  VOID
   )
 {
   EFI_STATUS           Status;
   SMM_LOCK_BOX_CONTEXT *SmmLockBoxContext;
 
-  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructor - Enter\n"));
+  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxMmConstructor - Enter\n"));
 
   //
   // Register SmmReadyToLock notification.
   //
-  Status = gSmst->SmmRegisterProtocolNotify (
-                    &gEfiSmmReadyToLockProtocolGuid,
+  Status = gMmst->MmRegisterProtocolNotify (
+                    &gEfiMmReadyToLockProtocolGuid,
                     SmmLockBoxSmmReadyToLockNotify,
                     &mSmmLockBoxRegistrationSmmReadyToLock
                     );
@@ -222,8 +217,8 @@ SmmLockBoxSmmConstructor (
   //
   // Register SmmEndOfDxe notification.
   //
-  Status = gSmst->SmmRegisterProtocolNotify (
-                    &gEfiSmmEndOfDxeProtocolGuid,
+  Status = gMmst->MmRegisterProtocolNotify (
+                    &gEfiMmEndOfDxeProtocolGuid,
                     SmmLockBoxSmmEndOfDxeNotify,
                     &mSmmLockBoxRegistrationSmmEndOfDxe
                     );
@@ -232,7 +227,7 @@ SmmLockBoxSmmConstructor (
   //
   // Register EndOfS3Resume notification.
   //
-  Status = gSmst->SmmRegisterProtocolNotify (
+  Status = gMmst->MmRegisterProtocolNotify (
                     &gEdkiiEndOfS3ResumeGuid,
                     SmmLockBoxEndOfS3ResumeNotify,
                     &mSmmLockBoxRegistrationEndOfS3Resume
@@ -249,7 +244,7 @@ SmmLockBoxSmmConstructor (
     // No need to install again, just return.
     //
     DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxContext - already installed\n"));
-    DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructor - Exit\n"));
+    DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxMmConstructor - Exit\n"));
     return EFI_SUCCESS;
   }
 
@@ -263,8 +258,8 @@ SmmLockBoxSmmConstructor (
   }
   mSmmLockBoxContext.LockBoxDataAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)&mLockBoxQueue;
 
-  Status = gSmst->SmmInstallConfigurationTable (
-                    gSmst,
+  Status = gMmst->MmInstallConfigurationTable (
+                    gMmst,
                     &gEfiSmmLockBoxCommunicationGuid,
                     &mSmmLockBoxContext,
                     sizeof(mSmmLockBoxContext)
@@ -274,7 +269,7 @@ SmmLockBoxSmmConstructor (
 
   DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxContext - %x\n", (UINTN)&mSmmLockBoxContext));
   DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib LockBoxDataAddress - %x\n", (UINTN)&mLockBoxQueue));
-  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmConstructor - Exit\n"));
+  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxMmConstructor - Exit\n"));
 
   return Status;
 }
@@ -284,26 +279,21 @@ SmmLockBoxSmmConstructor (
   This is used to uninstall SmmLockBoxCommunication configuration table
   if it has been installed in Constructor.
 
-  @param[in] ImageHandle    Image handle of this driver.
-  @param[in] SystemTable    A Pointer to the EFI System Table.
-
   @retval EFI_SUCEESS       The destructor always returns EFI_SUCCESS.
 
 **/
 EFI_STATUS
-EFIAPI
-SmmLockBoxSmmDestructor (
-  IN EFI_HANDLE         ImageHandle,
-  IN EFI_SYSTEM_TABLE   *SystemTable
+SmmLockBoxMmDestructor (
+  VOID
   )
 {
   EFI_STATUS            Status;
 
-  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxSmmDestructor in %a module\n", gEfiCallerBaseName));
+  DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SmmLockBoxMmDestructor in %a module\n", gEfiCallerBaseName));
 
   if (mSmmConfigurationTableInstalled) {
-    Status = gSmst->SmmInstallConfigurationTable (
-                      gSmst,
+    Status = gMmst->MmInstallConfigurationTable (
+                      gMmst,
                       &gEfiSmmLockBoxCommunicationGuid,
                       NULL,
                       0
@@ -316,8 +306,8 @@ SmmLockBoxSmmDestructor (
     //
     // Unregister SmmReadyToLock notification.
     //
-    Status = gSmst->SmmRegisterProtocolNotify (
-                      &gEfiSmmReadyToLockProtocolGuid,
+    Status = gMmst->MmRegisterProtocolNotify (
+                      &gEfiMmReadyToLockProtocolGuid,
                       NULL,
                       &mSmmLockBoxRegistrationSmmReadyToLock
                       );
@@ -327,8 +317,8 @@ SmmLockBoxSmmDestructor (
     //
     // Unregister SmmEndOfDxe notification.
     //
-    Status = gSmst->SmmRegisterProtocolNotify (
-                      &gEfiSmmEndOfDxeProtocolGuid,
+    Status = gMmst->MmRegisterProtocolNotify (
+                      &gEfiMmEndOfDxeProtocolGuid,
                       NULL,
                       &mSmmLockBoxRegistrationSmmEndOfDxe
                       );
@@ -338,7 +328,7 @@ SmmLockBoxSmmDestructor (
     //
     // Unregister EndOfS3Resume notification.
     //
-    Status = gSmst->SmmRegisterProtocolNotify (
+    Status = gMmst->MmRegisterProtocolNotify (
                       &gEdkiiEndOfS3ResumeGuid,
                       NULL,
                       &mSmmLockBoxRegistrationEndOfS3Resume
@@ -453,7 +443,7 @@ SaveLockBox (
   //
   // Allocate SMRAM buffer
   //
-  Status = gSmst->SmmAllocatePages (
+  Status = gMmst->MmAllocatePages (
                     AllocateAnyPages,
                     EfiRuntimeServicesData,
                     EFI_SIZE_TO_PAGES (Length),
@@ -468,14 +458,14 @@ SaveLockBox (
   //
   // Allocate LockBox
   //
-  Status = gSmst->SmmAllocatePool (
+  Status = gMmst->MmAllocatePool (
                     EfiRuntimeServicesData,
                     sizeof(*LockBox),
                     (VOID **)&LockBox
                     );
   ASSERT_EFI_ERROR (Status);
   if (EFI_ERROR (Status)) {
-    gSmst->SmmFreePages (SmramBuffer, EFI_SIZE_TO_PAGES (Length));
+    gMmst->MmFreePages (SmramBuffer, EFI_SIZE_TO_PAGES (Length));
     DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib SaveLockBox - Exit (%r)\n", EFI_OUT_OF_RESOURCES));
     return EFI_OUT_OF_RESOURCES;
   }
@@ -662,7 +652,7 @@ UpdateLockBox (
           DEBUG_INFO,
           "SmmLockBoxSmmLib UpdateLockBox - Allocate new buffer to enlarge.\n"
           ));
-        Status = gSmst->SmmAllocatePages (
+        Status = gMmst->MmAllocatePages (
                           AllocateAnyPages,
                           EfiRuntimeServicesData,
                           EFI_SIZE_TO_PAGES (Offset + Length),
@@ -679,7 +669,7 @@ UpdateLockBox (
         //
         CopyMem ((VOID *)(UINTN)SmramBuffer, (VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);
         ZeroMem ((VOID *)(UINTN)LockBox->SmramBuffer, (UINTN)LockBox->Length);
-        gSmst->SmmFreePages (LockBox->SmramBuffer, EFI_SIZE_TO_PAGES ((UINTN)LockBox->Length));
+        gMmst->MmFreePages (LockBox->SmramBuffer, EFI_SIZE_TO_PAGES ((UINTN)LockBox->Length));
 
         LockBox->SmramBuffer = SmramBuffer;
       }
diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.c b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.c
new file mode 100644
index 000000000000..722121da430b
--- /dev/null
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.c
@@ -0,0 +1,53 @@
+/** @file
+
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
+Copyright (c) Microsoft Corporation.
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include "SmmLockBoxLibPrivate.h"
+
+/**
+  Constructor for SmmLockBox library.
+  This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later.
+
+  @param[in] ImageHandle  Image handle of this driver.
+  @param[in] SystemTable  A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS
+  @return Others          Some error occurs.
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxStandaloneMmConstructor (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return SmmLockBoxMmConstructor ();
+}
+
+/**
+  Destructor for SmmLockBox library.
+  This is used to uninstall SmmLockBoxCommunication configuration table
+  if it has been installed in Constructor.
+
+  @param[in] ImageHandle    Image handle of this driver.
+  @param[in] SystemTable    A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS       The destructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxStandaloneMmDestructor (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *SystemTable
+  )
+{
+  return SmmLockBoxMmDestructor ();
+}
diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxTraditionalMmLib.c b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxTraditionalMmLib.c
new file mode 100644
index 000000000000..a748abb16c54
--- /dev/null
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxTraditionalMmLib.c
@@ -0,0 +1,53 @@
+/** @file
+
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
+Copyright (c) Microsoft Corporation.
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+
+#include "SmmLockBoxLibPrivate.h"
+
+/**
+  Constructor for SmmLockBox library.
+  This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later.
+
+  @param[in] ImageHandle  Image handle of this driver.
+  @param[in] SystemTable  A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS
+  @return Others          Some error occurs.
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxTraditionalConstructor (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return SmmLockBoxMmConstructor ();
+}
+
+/**
+  Destructor for SmmLockBox library.
+  This is used to uninstall SmmLockBoxCommunication configuration table
+  if it has been installed in Constructor.
+
+  @param[in] ImageHandle    Image handle of this driver.
+  @param[in] SystemTable    A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS       The destructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxTraditionalDestructor (
+  IN EFI_HANDLE          ImageHandle,
+  IN EFI_SYSTEM_TABLE    *SystemTable
+  )
+{
+  return SmmLockBoxMmDestructor ();
+}
diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxLibPrivate.h b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxLibPrivate.h
index 35311cb85d69..449333e94bc6 100644
--- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxLibPrivate.h
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxLibPrivate.h
@@ -43,5 +43,30 @@ typedef struct {
 
 #pragma pack()
 
+/**
+  Constructor for SmmLockBox library.
+  This is used to set SmmLockBox context, which will be used in PEI phase in S3 boot path later.
+
+  @retval EFI_SUCEESS
+  @return Others          Some error occurs.
+**/
+EFI_STATUS
+SmmLockBoxMmConstructor (
+  VOID
+  );
+
+/**
+  Destructor for SmmLockBox library.
+  This is used to uninstall SmmLockBoxCommunication configuration table
+  if it has been installed in Constructor.
+
+  @retval EFI_SUCEESS       The destructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+SmmLockBoxMmDestructor (
+  VOID
+  );
+
 #endif
 
diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
index 2b6cc983b988..44f7b63c5d20 100644
--- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
@@ -15,8 +15,8 @@ [Defines]
   MODULE_TYPE                    = DXE_SMM_DRIVER
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = LockBoxLib|DXE_SMM_DRIVER
-  CONSTRUCTOR                    = SmmLockBoxSmmConstructor
-  DESTRUCTOR                     = SmmLockBoxSmmDestructor
+  CONSTRUCTOR                    = SmmLockBoxTraditionalConstructor
+  DESTRUCTOR                     = SmmLockBoxTraditionalDestructor
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -25,7 +25,8 @@ [Defines]
 #
 
 [Sources]
-  SmmLockBoxSmmLib.c
+  SmmLockBoxTraditionalMmLib.c
+  SmmLockBoxMmLib.c
   SmmLockBoxLibPrivate.h
 
 [Packages]
@@ -33,14 +34,14 @@ [Packages]
   MdeModulePkg/MdeModulePkg.dec
 
 [LibraryClasses]
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseLib
   DebugLib
 
 [Protocols]
-  gEfiSmmReadyToLockProtocolGuid    ## NOTIFY
-  gEfiSmmEndOfDxeProtocolGuid       ## NOTIFY
-  gEfiSmmSxDispatch2ProtocolGuid    ## NOTIFY
+  gEfiMmReadyToLockProtocolGuid    ## NOTIFY
+  gEfiMmEndOfDxeProtocolGuid       ## NOTIFY
+  gEfiMmSxDispatchProtocolGuid     ## NOTIFY
 
 [Guids]
   ## SOMETIMES_CONSUMES   ## UNDEFINED # SmmSystemTable
diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
similarity index 52%
copy from MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
copy to MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
index 2b6cc983b988..d46dfb3f9aa8 100644
--- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
@@ -2,6 +2,7 @@
 #  SMM LockBox library instance.
 #
 #  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -9,14 +10,14 @@
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = SmmLockBoxSmmLib
-  MODULE_UNI_FILE                = SmmLockBoxSmmLib.uni
-  FILE_GUID                      = E04894D6-290D-4171-A362-0ACFD939F3C8
-  MODULE_TYPE                    = DXE_SMM_DRIVER
+  BASE_NAME                      = SmmLockBoxStandaloneMmLib
+  FILE_GUID                      = 3C05978B-30CA-4544-9C5A-AB99265EFC31
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = LockBoxLib|DXE_SMM_DRIVER
-  CONSTRUCTOR                    = SmmLockBoxSmmConstructor
-  DESTRUCTOR                     = SmmLockBoxSmmDestructor
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = LockBoxLib|MM_STANDALONE
+  CONSTRUCTOR                    = SmmLockBoxStandaloneMmConstructor
+  DESTRUCTOR                     = SmmLockBoxStandaloneMmDestructor
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -25,7 +26,8 @@ [Defines]
 #
 
 [Sources]
-  SmmLockBoxSmmLib.c
+  SmmLockBoxStandaloneMmLib.c
+  SmmLockBoxMmLib.c
   SmmLockBoxLibPrivate.h
 
 [Packages]
@@ -33,14 +35,14 @@ [Packages]
   MdeModulePkg/MdeModulePkg.dec
 
 [LibraryClasses]
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseLib
   DebugLib
 
 [Protocols]
-  gEfiSmmReadyToLockProtocolGuid    ## NOTIFY
-  gEfiSmmEndOfDxeProtocolGuid       ## NOTIFY
-  gEfiSmmSxDispatch2ProtocolGuid    ## NOTIFY
+  gEfiMmReadyToLockProtocolGuid    ## NOTIFY
+  gEfiMmEndOfDxeProtocolGuid       ## NOTIFY
+  gEfiMmSxDispatchProtocolGuid     ## NOTIFY
 
 [Guids]
   ## SOMETIMES_CONSUMES   ## UNDEFINED # SmmSystemTable
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 6d4e361afd51..9afd40eeed46 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -167,6 +167,7 @@ [LibraryClasses.common.MM_STANDALONE]
   MemoryAllocationLib|MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLibNull.inf
   StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
   MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
+  LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
 
 [LibraryClasses.ARM, LibraryClasses.AARCH64]
   ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
@@ -484,6 +485,7 @@ [Components.IA32, Components.X64]
   MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxPeiLib.inf
   MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf
   MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
+  MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
   MdeModulePkg/Library/SmmCorePlatformHookLibNull/SmmCorePlatformHookLibNull.inf
   MdeModulePkg/Library/SmmSmiHandlerProfileLib/SmmSmiHandlerProfileLib.inf
   MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaArchCustomDecompressLib.inf
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (4 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 05/16] MdeModulePkg: SmmLockBoxSmmLib: Support StandaloneMm for SmmLockBoxLib Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:24   ` [edk2-devel] " Wu, Hao A
  2021-01-05 18:59 ` [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver " Kun Qin
                   ` (11 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Dandan Bi, Liming Gao, Jiewen Yao

This change added support of StandaloneMm for ReportStatusCodeLib. It
adds a new instance of ReportStatusCodeLib for MM_STANDALONE type, and
abstracts the references of gMmst and gSmst functionalities into separate
files in order to link in proper Service Table for SMM core/drivers.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiewen Yao <jiewen.yao@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internal functions.
    - Minor new file description update.

 MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c                                               | 16 +++++----
 MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibStandaloneMm.c                                   | 38 ++++++++++++++++++++
 MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibTraditional.c                                    | 38 ++++++++++++++++++++
 MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.h                                               | 36 +++++++++++++++++++
 MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf                                          |  4 ++-
 MdeModulePkg/Library/SmmReportStatusCodeLib/{SmmReportStatusCodeLib.inf => StandaloneMmReportStatusCodeLib.inf} | 22 ++++++------
 MdeModulePkg/MdeModulePkg.dsc                                                                                   |  1 +
 7 files changed, 137 insertions(+), 18 deletions(-)

diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
index 3a1772538cdf..fb1769db9223 100644
--- a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
@@ -1,5 +1,5 @@
 /** @file
-  Report Status Code Library for SMM Phase.
+  Report Status Code Library for MM Phase.
 
   Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -8,7 +8,7 @@
 
 #include <Library/ReportStatusCodeLib.h>
 #include <Library/DebugLib.h>
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/PcdLib.h>
@@ -16,10 +16,12 @@
 
 #include <Guid/StatusCodeDataTypeId.h>
 #include <Guid/StatusCodeDataTypeDebug.h>
-#include <Protocol/SmmStatusCode.h>
+#include <Protocol/MmStatusCode.h>
 
-EFI_SMM_REPORT_STATUS_CODE     mReportStatusCode = NULL;
-EFI_SMM_STATUS_CODE_PROTOCOL   *mStatusCodeProtocol = NULL;
+#include "ReportStatusCodeLib.h"
+
+EFI_MM_REPORT_STATUS_CODE     mReportStatusCode = NULL;
+EFI_MM_STATUS_CODE_PROTOCOL   *mStatusCodeProtocol = NULL;
 
 
 /**
@@ -29,14 +31,14 @@ EFI_SMM_STATUS_CODE_PROTOCOL   *mStatusCodeProtocol = NULL;
             NULL is returned if no status code service is available.
 
 **/
-EFI_SMM_REPORT_STATUS_CODE
+EFI_MM_REPORT_STATUS_CODE
 InternalGetReportStatusCode (
   VOID
   )
 {
   EFI_STATUS                    Status;
 
-  Status = gSmst->SmmLocateProtocol (&gEfiSmmStatusCodeProtocolGuid, NULL, (VOID**)&mStatusCodeProtocol);
+  Status = InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid, NULL, (VOID**)&mStatusCodeProtocol);
   if (!EFI_ERROR (Status) && mStatusCodeProtocol != NULL) {
     return mStatusCodeProtocol->ReportStatusCode;
   }
diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibStandaloneMm.c b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibStandaloneMm.c
new file mode 100644
index 000000000000..a4c428dc88a9
--- /dev/null
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibStandaloneMm.c
@@ -0,0 +1,38 @@
+/** @file
+  Abstraction layer for MM service table used by MM ReportStatusCodeLib.
+
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/MmServicesTableLib.h>
+
+/**
+  Returns the first protocol instance that matches the given protocol.
+
+  @param[in]  Protocol          Provides the protocol to search for.
+  @param[in]  Registration      Optional registration key returned from
+                                RegisterProtocolNotify().
+  @param[out]  Interface        On return, a pointer to the first interface that matches Protocol and
+                                Registration.
+
+  @retval EFI_SUCCESS           A protocol instance matching Protocol was found and returned in
+                                Interface.
+  @retval EFI_NOT_FOUND         No protocol instances were found that match Protocol and
+                                Registration.
+  @retval EFI_INVALID_PARAMETER Interface is NULL.
+                                Protocol is NULL.
+
+**/
+EFI_STATUS
+InternalLocateProtocol (
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration, OPTIONAL
+  OUT VOID      **Interface
+  )
+{
+  return gMmst->MmLocateProtocol (Protocol, Registration, Interface);
+}
diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibTraditional.c b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibTraditional.c
new file mode 100644
index 000000000000..603e222f5508
--- /dev/null
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibTraditional.c
@@ -0,0 +1,38 @@
+/** @file
+  Abstraction layer for SMM service table used by SMM ReportStatusCodeLib.
+
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/SmmServicesTableLib.h>
+
+/**
+  Returns the first protocol instance that matches the given protocol.
+
+  @param[in]  Protocol          Provides the protocol to search for.
+  @param[in]  Registration      Optional registration key returned from
+                                RegisterProtocolNotify().
+  @param[out]  Interface        On return, a pointer to the first interface that matches Protocol and
+                                Registration.
+
+  @retval EFI_SUCCESS           A protocol instance matching Protocol was found and returned in
+                                Interface.
+  @retval EFI_NOT_FOUND         No protocol instances were found that match Protocol and
+                                Registration.
+  @retval EFI_INVALID_PARAMETER Interface is NULL.
+                                Protocol is NULL.
+
+**/
+EFI_STATUS
+InternalLocateProtocol (
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration, OPTIONAL
+  OUT VOID      **Interface
+  )
+{
+  return gSmst->SmmLocateProtocol (Protocol, Registration, Interface);
+}
diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.h b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.h
new file mode 100644
index 000000000000..2820e40dde19
--- /dev/null
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.h
@@ -0,0 +1,36 @@
+/** @file
+  Report Status Code Library for MM Phase.
+
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _MM_RSC_LIB_H_
+#define _MM_RSC_LIB_H_
+
+/**
+  Returns the first protocol instance that matches the given protocol.
+
+  @param[in]  Protocol          Provides the protocol to search for.
+  @param[in]  Registration      Optional registration key returned from
+                                RegisterProtocolNotify().
+  @param[out]  Interface        On return, a pointer to the first interface that matches Protocol and
+                                Registration.
+
+  @retval EFI_SUCCESS           A protocol instance matching Protocol was found and returned in
+                                Interface.
+  @retval EFI_NOT_FOUND         No protocol instances were found that match Protocol and
+                                Registration.
+  @retval EFI_INVALID_PARAMETER Interface is NULL.
+                                Protocol is NULL.
+
+**/
+EFI_STATUS
+InternalLocateProtocol (
+  IN  EFI_GUID  *Protocol,
+  IN  VOID      *Registration, OPTIONAL
+  OUT VOID      **Interface
+  );
+
+#endif // _MM_RSC_LIB_H_
diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf b/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
index 72496bfababd..02dce09a199d 100644
--- a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
@@ -28,6 +28,8 @@ [Defines]
 
 [Sources]
   ReportStatusCodeLib.c
+  ReportStatusCodeLib.h
+  ReportStatusCodeLibTraditional.c
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -45,7 +47,7 @@ [Guids]
   gEfiStatusCodeDataTypeDebugGuid               ## SOMETIMES_CONSUMES   ## UNDEFINED
 
 [Protocols]
-  gEfiSmmStatusCodeProtocolGuid                 ## CONSUMES
+  gEfiMmStatusCodeProtocolGuid                  ## CONSUMES
 
 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask  ## CONSUMES
diff --git a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf b/MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportStatusCodeLib.inf
similarity index 56%
copy from MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
copy to MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportStatusCodeLib.inf
index 72496bfababd..866e09249a6a 100644
--- a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
+++ b/MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportStatusCodeLib.inf
@@ -1,9 +1,10 @@
 ## @file
-#  SMM report status code library.
+#  Standalone MM report status code library.
 #
-#  Retrieve status code and report status code in SMM phase.
+#  Retrieve status code and report status code in MM phase.
 #
 #  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -12,13 +13,12 @@
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = SmmReportStatusCodeLib
-  MODULE_UNI_FILE                = SmmReportStatusCodeLib.uni
-  FILE_GUID                      = 67089D19-B3D6-4d9e-A0EB-FEDC1F83A1EE
-  MODULE_TYPE                    = DXE_SMM_DRIVER
+  BASE_NAME                      = StandaloneMmReportStatusCodeLib
+  FILE_GUID                      = 17C7FC8C-8C5D-497E-9C0E-C21255B30E04
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  PI_SPECIFICATION_VERSION       = 0x0001000A
-  LIBRARY_CLASS                  = ReportStatusCodeLib|DXE_SMM_DRIVER SMM_CORE
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = ReportStatusCodeLib|MM_STANDALONE
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -28,6 +28,8 @@ [Defines]
 
 [Sources]
   ReportStatusCodeLib.c
+  ReportStatusCodeLib.h
+  ReportStatusCodeLibStandaloneMm.c
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -36,7 +38,7 @@ [Packages]
 [LibraryClasses]
   PcdLib
   BaseMemoryLib
-  SmmServicesTableLib
+  MmServicesTableLib
   DebugLib
   MemoryAllocationLib
 
@@ -45,7 +47,7 @@ [Guids]
   gEfiStatusCodeDataTypeDebugGuid               ## SOMETIMES_CONSUMES   ## UNDEFINED
 
 [Protocols]
-  gEfiSmmStatusCodeProtocolGuid                 ## CONSUMES
+  gEfiMmStatusCodeProtocolGuid                  ## CONSUMES
 
 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask  ## CONSUMES
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 9afd40eeed46..200fbcc18a18 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -474,6 +474,7 @@ [Components.IA32, Components.X64]
   }
   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf
   MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
+  MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportStatusCodeLib.inf
   MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
   MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver in StandaloneMm
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (5 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:24   ` Wu, Hao A
  2021-01-05 18:59 ` [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support Kun Qin
                   ` (10 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Dandan Bi, Liming Gao, Jiewen Yao

This change added support of StandaloneMm for StatusCodeHandler. It
adds a new instance of StatusCodeHandler of MM_STANDALONE type, and
abstracts the driver entrypoint into separate files, replaced gSmst with
gMmst, and switched to MM version of RscHandlerProtocol.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiewen Yao <jiewen.yao@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - New patch to support StatusCodeHandler in standalone mm [Liming]

 MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeWorker.c                                        | 36 ++++++++++----------
 MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWorker.c                                        |  2 +-
 MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerSmm.c => StatusCodeHandlerMm.c}               | 23 +++++--------
 MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandalone.c                                   | 31 +++++++++++++++++
 MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerTraditional.c                                  | 31 +++++++++++++++++
 MdeModulePkg/MdeModulePkg.dsc                                                                                |  1 +
 MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerSmm.h => StatusCodeHandlerMm.h}               | 23 ++++++++++---
 MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf                                        | 15 ++++----
 MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerSmm.inf => StatusCodeHandlerStandaloneMm.inf} | 32 ++++++++---------
 9 files changed, 132 insertions(+), 62 deletions(-)

diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeWorker.c b/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeWorker.c
index c9b43fd2468f..14bac8ec3c18 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeWorker.c
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeWorker.c
@@ -7,15 +7,15 @@
 
 **/
 
-#include "StatusCodeHandlerSmm.h"
+#include "StatusCodeHandlerMm.h"
 
-RUNTIME_MEMORY_STATUSCODE_HEADER  *mSmmMemoryStatusCodeTable;
+RUNTIME_MEMORY_STATUSCODE_HEADER  *mMmMemoryStatusCodeTable;
 
 /**
-  Initialize SMM memory status code table as initialization for memory status code worker
+  Initialize MM memory status code table as initialization for memory status code worker
 
-  @retval EFI_SUCCESS  SMM memory status code table successfully initialized.
-  @retval others       Errors from gSmst->SmmInstallConfigurationTable().
+  @retval EFI_SUCCESS  MM memory status code table successfully initialized.
+  @retval others       Errors from gMmst->MmInstallConfigurationTable().
 **/
 EFI_STATUS
 MemoryStatusCodeInitializeWorker (
@@ -25,17 +25,17 @@ MemoryStatusCodeInitializeWorker (
   EFI_STATUS                        Status;
 
   //
-  // Allocate SMM memory status code pool.
+  // Allocate MM memory status code pool.
   //
-  mSmmMemoryStatusCodeTable = (RUNTIME_MEMORY_STATUSCODE_HEADER *)AllocateZeroPool (sizeof (RUNTIME_MEMORY_STATUSCODE_HEADER) + PcdGet16 (PcdStatusCodeMemorySize) * 1024);
-  ASSERT (mSmmMemoryStatusCodeTable != NULL);
+  mMmMemoryStatusCodeTable = (RUNTIME_MEMORY_STATUSCODE_HEADER *)AllocateZeroPool (sizeof (RUNTIME_MEMORY_STATUSCODE_HEADER) + PcdGet16 (PcdStatusCodeMemorySize) * 1024);
+  ASSERT (mMmMemoryStatusCodeTable != NULL);
 
-  mSmmMemoryStatusCodeTable->MaxRecordsNumber = (PcdGet16 (PcdStatusCodeMemorySize) * 1024) / sizeof (MEMORY_STATUSCODE_RECORD);
-  Status = gSmst->SmmInstallConfigurationTable (
-                    gSmst,
+  mMmMemoryStatusCodeTable->MaxRecordsNumber = (PcdGet16 (PcdStatusCodeMemorySize) * 1024) / sizeof (MEMORY_STATUSCODE_RECORD);
+  Status = gMmst->MmInstallConfigurationTable (
+                    gMmst,
                     &gMemoryStatusCodeRecordGuid,
-                    &mSmmMemoryStatusCodeTable,
-                    sizeof (mSmmMemoryStatusCodeTable)
+                    &mMmMemoryStatusCodeTable,
+                    sizeof (mMmMemoryStatusCodeTable)
                     );
   return Status;
 }
@@ -74,8 +74,8 @@ MemoryStatusCodeReportWorker (
   //
   // Locate current record buffer.
   //
-  Record = (MEMORY_STATUSCODE_RECORD *) (mSmmMemoryStatusCodeTable + 1);
-  Record = &Record[mSmmMemoryStatusCodeTable->RecordIndex++];
+  Record = (MEMORY_STATUSCODE_RECORD *) (mMmMemoryStatusCodeTable + 1);
+  Record = &Record[mMmMemoryStatusCodeTable->RecordIndex++];
 
   //
   // Save status code.
@@ -92,12 +92,12 @@ MemoryStatusCodeReportWorker (
   // so the first record is pointed by record index.
   // If it is less then max number, index of the first record is zero.
   //
-  mSmmMemoryStatusCodeTable->NumberOfRecords++;
-  if (mSmmMemoryStatusCodeTable->RecordIndex == mSmmMemoryStatusCodeTable->MaxRecordsNumber) {
+  mMmMemoryStatusCodeTable->NumberOfRecords++;
+  if (mMmMemoryStatusCodeTable->RecordIndex == mMmMemoryStatusCodeTable->MaxRecordsNumber) {
     //
     // Wrap around record index.
     //
-    mSmmMemoryStatusCodeTable->RecordIndex = 0;
+    mMmMemoryStatusCodeTable->RecordIndex = 0;
   }
 
   return EFI_SUCCESS;
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWorker.c b/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWorker.c
index 3df0a6712611..bcb75bc7b170 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWorker.c
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWorker.c
@@ -6,7 +6,7 @@
 
 **/
 
-#include "StatusCodeHandlerSmm.h"
+#include "StatusCodeHandlerMm.h"
 
 /**
   Convert status code value and extended data to readable ASCII string, send string to serial I/O device.
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.c b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.c
similarity index 69%
rename from MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.c
rename to MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.c
index 20271571ded4..4948d3d99ad6 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.c
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.c
@@ -1,15 +1,15 @@
 /** @file
   Status Code Handler Driver which produces general handlers and hook them
-  onto the SMM status code router.
+  onto the MM status code router.
 
   Copyright (c) 2009 - 2020, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include "StatusCodeHandlerSmm.h"
+#include "StatusCodeHandlerMm.h"
 
-EFI_SMM_RSC_HANDLER_PROTOCOL  *mRscHandlerProtocol       = NULL;
+EFI_MM_RSC_HANDLER_PROTOCOL   *mRscHandlerProtocol       = NULL;
 
 
 /**
@@ -42,27 +42,22 @@ InitializationDispatcherWorker (
 }
 
 /**
-  Entry point of SMM Status Code Driver.
+  Entry point of Common MM Status Code Driver.
 
-  This function is the entry point of SMM Status Code Driver.
-
-  @param  ImageHandle       The firmware allocated handle for the EFI image.
-  @param  SystemTable       A pointer to the EFI System Table.
+  This function is the entry point of MM Status Code Driver.
 
   @retval EFI_SUCCESS       The entry point is executed successfully.
 
 **/
 EFI_STATUS
-EFIAPI
-StatusCodeHandlerSmmEntry (
-  IN EFI_HANDLE         ImageHandle,
-  IN EFI_SYSTEM_TABLE   *SystemTable
+StatusCodeHandlerCommonEntry (
+  VOID
   )
 {
   EFI_STATUS                Status;
 
-  Status = gSmst->SmmLocateProtocol (
-                    &gEfiSmmRscHandlerProtocolGuid,
+  Status = gMmst->MmLocateProtocol (
+                    &gEfiMmRscHandlerProtocolGuid,
                     NULL,
                     (VOID **) &mRscHandlerProtocol
                     );
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandalone.c b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandalone.c
new file mode 100644
index 000000000000..11a61705d612
--- /dev/null
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandalone.c
@@ -0,0 +1,31 @@
+/** @file
+  Abstraction layer that contains Standalone MM specific implementation for
+  Status Code Handler Driver.
+
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "StatusCodeHandlerMm.h"
+
+/**
+  Entry point of Standalone MM Status Code Driver.
+
+  This function is the entry point of Standalone MM Status Code Driver.
+
+  @param  ImageHandle       The firmware allocated handle for the EFI image.
+  @param  SystemTable       A pointer to the EFI MM System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+StatusCodeHandlerStandaloneMmEntry (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return StatusCodeHandlerCommonEntry ();
+}
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerTraditional.c b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerTraditional.c
new file mode 100644
index 000000000000..1105f184b08e
--- /dev/null
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerTraditional.c
@@ -0,0 +1,31 @@
+/** @file
+  Abstraction layer that contains Standalone MM specific implementation for
+  Status Code Handler Driver.
+
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "StatusCodeHandlerMm.h"
+
+/**
+  Entry point of Traditional MM Status Code Driver.
+
+  This function is the entry point of Traditional MM Status Code Driver.
+
+  @param  ImageHandle       The firmware allocated handle for the EFI image.
+  @param  SystemTable       A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+StatusCodeHandlerTraditionalMmEntry (
+  IN EFI_HANDLE         ImageHandle,
+  IN EFI_SYSTEM_TABLE   *SystemTable
+  )
+{
+  return StatusCodeHandlerCommonEntry ();
+}
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 200fbcc18a18..098909490095 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -476,6 +476,7 @@ [Components.IA32, Components.X64]
   MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
   MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportStatusCodeLib.inf
   MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
+  MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandaloneMm.inf
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
   MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
   MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllocationProfileLib.inf
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.h b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.h
similarity index 87%
rename from MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.h
rename to MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.h
index 6b5d53a4fee3..7871ee404046 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.h
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerMm.h
@@ -7,10 +7,10 @@
 
 **/
 
-#ifndef __STATUS_CODE_HANDLER_SMM_H__
-#define __STATUS_CODE_HANDLER_SMM_H__
+#ifndef __STATUS_CODE_HANDLER_MM_H__
+#define __STATUS_CODE_HANDLER_MM_H__
 
-#include <Protocol/SmmReportStatusCodeHandler.h>
+#include <Protocol/MmReportStatusCodeHandler.h>
 
 #include <Guid/MemoryStatusCodeRecord.h>
 #include <Guid/StatusCodeDataTypeId.h>
@@ -22,7 +22,7 @@
 #include <Library/PrintLib.h>
 #include <Library/PcdLib.h>
 #include <Library/UefiDriverEntryPoint.h>
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/SerialPortLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/BaseMemoryLib.h>
@@ -32,7 +32,7 @@
 //
 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
 
-extern RUNTIME_MEMORY_STATUSCODE_HEADER  *mSmmMemoryStatusCodeTable;
+extern RUNTIME_MEMORY_STATUSCODE_HEADER  *mMmMemoryStatusCodeTable;
 
 /**
   Locates Serial I/O Protocol as initialization for serial status code worker.
@@ -114,4 +114,17 @@ MemoryStatusCodeReportWorker (
   IN EFI_STATUS_CODE_DATA               *Data OPTIONAL
   );
 
+/**
+  Entry point of Common MM Status Code Driver.
+
+  This function is the entry point of MM Status Code Driver.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+StatusCodeHandlerCommonEntry (
+  VOID
+  );
+
 #endif
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
index 4e24d87e55d1..90abe662d291 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
@@ -1,5 +1,5 @@
 ## @file
-#  Status Code Handler Driver which produces general handlers and hook them onto the SMM status code router.
+#  Status Code Handler Driver which produces general handlers and hook them onto the MM status code router.
 #
 #  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
@@ -17,7 +17,7 @@ [Defines]
   MODULE_TYPE                    = DXE_SMM_DRIVER
   PI_SPECIFICATION_VERSION       = 0x0001000A
   VERSION_STRING                 = 1.0
-  ENTRY_POINT                    = StatusCodeHandlerSmmEntry
+  ENTRY_POINT                    = StatusCodeHandlerTraditionalMmEntry
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -26,8 +26,9 @@ [Defines]
 #
 
 [Sources]
-  StatusCodeHandlerSmm.c
-  StatusCodeHandlerSmm.h
+  StatusCodeHandlerMm.c
+  StatusCodeHandlerMm.h
+  StatusCodeHandlerTraditional.c
   SerialStatusCodeWorker.c
   MemoryStatusCodeWorker.c
 
@@ -37,7 +38,7 @@ [Packages]
 
 [LibraryClasses]
   SerialPortLib
-  SmmServicesTableLib
+  MmServicesTableLib
   UefiDriverEntryPoint
   PcdLib
   PrintLib
@@ -51,7 +52,7 @@ [Guids]
   gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ## UNDEFINED # SmmSystemTable
 
 [Protocols]
-  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
+  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial ## CONSUMES
@@ -59,7 +60,7 @@ [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeMemorySize |128| gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory   ## SOMETIMES_CONSUMES
 
 [Depex]
-  gEfiSmmRscHandlerProtocolGuid
+  gEfiMmRscHandlerProtocolGuid
 
 [UserExtensions.TianoCore."ExtraFiles"]
   StatusCodeHandlerSmmExtra.uni
diff --git a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandaloneMm.inf
similarity index 62%
copy from MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
copy to MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandaloneMm.inf
index 4e24d87e55d1..d7c863bf064c 100644
--- a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
+++ b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandaloneMm.inf
@@ -1,8 +1,9 @@
 ## @file
-#  Status Code Handler Driver which produces general handlers and hook them onto the SMM status code router.
+#  Status Code Handler Driver which produces general handlers and hook them onto the MM status code router.
 #
 #  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
+#  Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -11,13 +12,12 @@
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = StatusCodeHandlerSmm
-  MODULE_UNI_FILE                = StatusCodeHandlerSmm.uni
-  FILE_GUID                      = 79CD78D8-6EDC-4978-BD02-3299C387AB17
-  MODULE_TYPE                    = DXE_SMM_DRIVER
-  PI_SPECIFICATION_VERSION       = 0x0001000A
+  BASE_NAME                      = StatusCodeHandlerStandaloneMm
+  FILE_GUID                      = EBE7802F-5E11-4D4E-B463-22D2425D156B
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
   VERSION_STRING                 = 1.0
-  ENTRY_POINT                    = StatusCodeHandlerSmmEntry
+  ENTRY_POINT                    = StatusCodeHandlerStandaloneMmEntry
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -26,8 +26,9 @@ [Defines]
 #
 
 [Sources]
-  StatusCodeHandlerSmm.c
-  StatusCodeHandlerSmm.h
+  StatusCodeHandlerMm.c
+  StatusCodeHandlerMm.h
+  StatusCodeHandlerStandalone.c
   SerialStatusCodeWorker.c
   MemoryStatusCodeWorker.c
 
@@ -37,8 +38,8 @@ [Packages]
 
 [LibraryClasses]
   SerialPortLib
-  SmmServicesTableLib
-  UefiDriverEntryPoint
+  MmServicesTableLib
+  StandaloneMmDriverEntryPoint
   PcdLib
   PrintLib
   ReportStatusCodeLib
@@ -48,10 +49,10 @@ [LibraryClasses]
 
 [Guids]
   gEfiStatusCodeDataTypeStringGuid              ## SOMETIMES_CONSUMES   ## UNDEFINED
-  gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ## UNDEFINED # SmmSystemTable
+  gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ## UNDEFINED # MmSystemTable
 
 [Protocols]
-  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
+  gEfiMmRscHandlerProtocolGuid                  ## CONSUMES
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial ## CONSUMES
@@ -59,7 +60,4 @@ [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeMemorySize |128| gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory   ## SOMETIMES_CONSUMES
 
 [Depex]
-  gEfiSmmRscHandlerProtocolGuid
-
-[UserExtensions.TianoCore."ExtraFiles"]
-  StatusCodeHandlerSmmExtra.uni
+  gEfiMmRscHandlerProtocolGuid
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (6 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver " Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:24   ` Wu, Hao A
  2021-01-05 18:59 ` [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router Kun Qin
                   ` (9 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Dandan Bi, Liming Gao

This change added support of FPDT driver under StandaloneMm. It replaces
SMM version ReportStatusCode protocol with MM version. This patch also
abstracts standalone and traditional MM interfaces into separate files to
support each corresponding function prototypes and implementations.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internally abstracted functions [Hao]
    - Support driver for both IA32 and X64 in dsc file [Hao]

 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/{FirmwarePerformanceSmm.c => FirmwarePerformanceCommon.c}           | 76 +++++++++-----------
 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.c                                   | 61 ++++++++++++++++
 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceTraditional.c                                    | 61 ++++++++++++++++
 MdeModulePkg/MdeModulePkg.dsc                                                                                                   |  2 +
 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.h                                         | 50 +++++++++++++
 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf                                          | 11 +--
 MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/{FirmwarePerformanceSmm.inf => FirmwarePerformanceStandaloneMm.inf} | 31 ++++----
 7 files changed, 230 insertions(+), 62 deletions(-)

diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.c
similarity index 75%
rename from MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
rename to MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.c
index d6c6e7693e4d..ecadef8711ed 100644
--- a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.c
@@ -1,11 +1,11 @@
 /** @file
-  This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
+  This module collects performance data for MM driver boot records and S3 Suspend Performance Record.
 
   This module registers report status code listener to collect performance data
-  for SMM driver boot records and S3 Suspend Performance Record.
+  for MM driver boot records and S3 Suspend Performance Record.
 
   Caution: This module requires additional review when modified.
-  This driver will have external input - communicate buffer in SMM mode.
+  This driver will have external input - communicate buffer in MM mode.
   This external input must be validated carefully to avoid security issue like
   buffer overflow, integer overflow.
 
@@ -16,13 +16,13 @@
 
 **/
 
-#include <PiSmm.h>
+#include <PiMm.h>
 
-#include <Protocol/SmmReportStatusCodeHandler.h>
+#include <Protocol/MmReportStatusCodeHandler.h>
 
 #include <Guid/FirmwarePerformance.h>
 
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/BaseLib.h>
 #include <Library/DebugLib.h>
 #include <Library/TimerLib.h>
@@ -30,23 +30,22 @@
 #include <Library/PcdLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/MemoryAllocationLib.h>
-#include <Library/UefiBootServicesTableLib.h>
 #include <Library/SynchronizationLib.h>
-#include <Library/SmmMemLib.h>
+#include "FirmwarePerformanceCommon.h"
 
-SMM_BOOT_PERFORMANCE_TABLE    *mSmmBootPerformanceTable = NULL;
+SMM_BOOT_PERFORMANCE_TABLE    *mMmBootPerformanceTable = NULL;
 
-EFI_SMM_RSC_HANDLER_PROTOCOL  *mRscHandlerProtocol    = NULL;
+EFI_MM_RSC_HANDLER_PROTOCOL   *mRscHandlerProtocol    = NULL;
 UINT64                        mSuspendStartTime       = 0;
 BOOLEAN                       mS3SuspendLockBoxSaved  = FALSE;
 UINT32                        mBootRecordSize = 0;
 UINT8                         *mBootRecordBuffer = NULL;
 
-SPIN_LOCK                     mSmmFpdtLock;
-BOOLEAN                       mSmramIsOutOfResource = FALSE;
+SPIN_LOCK                     mMmFpdtLock;
+BOOLEAN                       mMmramIsOutOfResource = FALSE;
 
 /**
-  Report status code listener for SMM. This is used to record the performance
+  Report status code listener for MM. This is used to record the performance
   data for S3 Suspend Start and S3 Suspend End in FPDT.
 
   @param[in]  CodeType            Indicates the type of status code being reported.
@@ -66,7 +65,7 @@ BOOLEAN                       mSmramIsOutOfResource = FALSE;
 **/
 EFI_STATUS
 EFIAPI
-FpdtStatusCodeListenerSmm (
+FpdtStatusCodeListenerMm (
   IN EFI_STATUS_CODE_TYPE     CodeType,
   IN EFI_STATUS_CODE_VALUE    Value,
   IN UINT32                   Instance,
@@ -89,19 +88,19 @@ FpdtStatusCodeListenerSmm (
   // Collect one or more Boot records in boot time
   //
   if (Data != NULL && CompareGuid (&Data->Type, &gEdkiiFpdtExtendedFirmwarePerformanceGuid)) {
-    AcquireSpinLock (&mSmmFpdtLock);
+    AcquireSpinLock (&mMmFpdtLock);
     //
     // Get the boot performance data.
     //
-    CopyMem (&mSmmBootPerformanceTable, Data + 1, Data->Size);
-    mBootRecordBuffer = ((UINT8 *) (mSmmBootPerformanceTable)) + sizeof (SMM_BOOT_PERFORMANCE_TABLE);
+    CopyMem (&mMmBootPerformanceTable, Data + 1, Data->Size);
+    mBootRecordBuffer = ((UINT8 *) (mMmBootPerformanceTable)) + sizeof (SMM_BOOT_PERFORMANCE_TABLE);
 
-    ReleaseSpinLock (&mSmmFpdtLock);
+    ReleaseSpinLock (&mMmFpdtLock);
     return EFI_SUCCESS;
   }
 
   if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
-    DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerSmm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableSmm\n"));
+    DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerMm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableMm\n"));
     return EFI_UNSUPPORTED;
   }
 
@@ -157,7 +156,7 @@ FpdtStatusCodeListenerSmm (
 /**
   Communication service SMI Handler entry.
 
-  This SMI handler provides services for report SMM boot records.
+  This SMI handler provides services for report MM boot records.
 
   Caution: This function may receive untrusted input.
   Communicate buffer and buffer size are external input, so this function will do basic validation.
@@ -166,7 +165,7 @@ FpdtStatusCodeListenerSmm (
   @param[in]     RegisterContext Points to an optional handler context which was specified when the
                                  handler was registered.
   @param[in, out] CommBuffer     A pointer to a collection of data in memory that will
-                                 be conveyed from a non-SMM environment into an SMM environment.
+                                 be conveyed from a non-MM environment into an MM environment.
   @param[in, out] CommBufferSize The size of the CommBuffer.
 
   @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers
@@ -207,8 +206,8 @@ FpdtSmiHandler (
     return EFI_SUCCESS;
   }
 
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
-    DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in SMRAM or overflow!\n"));
+  if (!IsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+    DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM communication data buffer in MMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
 
@@ -218,8 +217,8 @@ FpdtSmiHandler (
 
   switch (SmmCommData->Function) {
     case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
-      if (mSmmBootPerformanceTable != NULL) {
-        mBootRecordSize = mSmmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);
+      if (mMmBootPerformanceTable != NULL) {
+        mBootRecordSize = mMmBootPerformanceTable->Header.Length - sizeof (SMM_BOOT_PERFORMANCE_TABLE);
       }
       SmmCommData->BootRecordSize = mBootRecordSize;
       break;
@@ -244,8 +243,8 @@ FpdtSmiHandler (
         BootRecordSize = mBootRecordSize - BootRecordOffset;
       }
       SmmCommData->BootRecordSize = BootRecordSize;
-      if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, BootRecordSize)) {
-        DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or overflow!\n"));
+      if (!IsBufferOutsideMmValid ((UINTN)BootRecordData, BootRecordSize)) {
+        DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM Data buffer in MMRAM or overflow!\n"));
         Status = EFI_ACCESS_DENIED;
         break;
       }
@@ -267,20 +266,15 @@ FpdtSmiHandler (
 }
 
 /**
-  The module Entry Point of the Firmware Performance Data Table SMM driver.
-
-  @param[in]  ImageHandle    The firmware allocated handle for the EFI image.
-  @param[in]  SystemTable    A pointer to the EFI System Table.
+  The module Entry Point of the Firmware Performance Data Table MM driver.
 
   @retval EFI_SUCCESS    The entry point is executed successfully.
   @retval Other          Some error occurs when executing this entry point.
 
 **/
 EFI_STATUS
-EFIAPI
-FirmwarePerformanceSmmEntryPoint (
-  IN EFI_HANDLE          ImageHandle,
-  IN EFI_SYSTEM_TABLE    *SystemTable
+FirmwarePerformanceCommonEntryPoint (
+  VOID
   )
 {
   EFI_STATUS                Status;
@@ -289,13 +283,13 @@ FirmwarePerformanceSmmEntryPoint (
   //
   // Initialize spin lock
   //
-  InitializeSpinLock (&mSmmFpdtLock);
+  InitializeSpinLock (&mMmFpdtLock);
 
   //
-  // Get SMM Report Status Code Handler Protocol.
+  // Get MM Report Status Code Handler Protocol.
   //
-  Status = gSmst->SmmLocateProtocol (
-                    &gEfiSmmRscHandlerProtocolGuid,
+  Status = gMmst->MmLocateProtocol (
+                    &gEfiMmRscHandlerProtocolGuid,
                     NULL,
                     (VOID **) &mRscHandlerProtocol
                     );
@@ -304,14 +298,14 @@ FirmwarePerformanceSmmEntryPoint (
   //
   // Register report status code listener for BootRecords and S3 Suspend Start and End.
   //
-  Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
+  Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerMm);
   ASSERT_EFI_ERROR (Status);
 
   //
   // Register SMI handler.
   //
   Handle = NULL;
-  Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
+  Status = gMmst->MmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
   ASSERT_EFI_ERROR (Status);
 
   return Status;
diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.c b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.c
new file mode 100644
index 000000000000..d7da61c98c66
--- /dev/null
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.c
@@ -0,0 +1,61 @@
+/** @file
+  This module collects performance data for MM driver boot records and S3 Suspend Performance Record.
+
+  This module registers report status code listener to collect performance data
+  for MM driver boot records and S3 Suspend Performance Record.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in MM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  FpdtSmiHandler() will receive untrusted input and do basic validation.
+
+  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c), Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include <Library/StandaloneMmMemLib.h>
+#include "FirmwarePerformanceCommon.h"
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  )
+{
+  return MmIsBufferOutsideMmValid (Buffer, Length);
+}
+
+/**
+  The module Entry Point of the Firmware Performance Data Table MM driver.
+
+  @param[in]  ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in]  SystemTable    A pointer to the EFI MM System Table.
+
+  @retval EFI_SUCCESS    The entry point is executed successfully.
+  @retval Other          Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+FirmwarePerformanceStandaloneMmEntryPoint (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *SystemTable
+  )
+{
+  return FirmwarePerformanceCommonEntryPoint ();
+}
diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceTraditional.c b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceTraditional.c
new file mode 100644
index 000000000000..43c050d6b516
--- /dev/null
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceTraditional.c
@@ -0,0 +1,61 @@
+/** @file
+  This module collects performance data for MM driver boot records and S3 Suspend Performance Record.
+
+  This module registers report status code listener to collect performance data
+  for MM driver boot records and S3 Suspend Performance Record.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in MM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  FpdtSmiHandler() will receive untrusted input and do basic validation.
+
+  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c), Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+
+#include <Library/SmmMemLib.h>
+#include "FirmwarePerformanceCommon.h"
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  )
+{
+  return SmmIsBufferOutsideSmmValid (Buffer, Length);
+}
+
+/**
+  The module Entry Point of the Firmware Performance Data Table MM driver.
+
+  @param[in]  ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in]  SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS    The entry point is executed successfully.
+  @retval Other          Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+FirmwarePerformanceSmmEntryPoint (
+  IN EFI_HANDLE          ImageHandle,
+  IN EFI_SYSTEM_TABLE    *SystemTable
+  )
+{
+  return FirmwarePerformanceCommonEntryPoint ();
+}
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 098909490095..34ca571ca662 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -168,6 +168,7 @@ [LibraryClasses.common.MM_STANDALONE]
   StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
   MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
   LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandaloneMmLib.inf
+  MemLib|StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf
 
 [LibraryClasses.ARM, LibraryClasses.AARCH64]
   ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
@@ -495,6 +496,7 @@ [Components.IA32, Components.X64]
   MdeModulePkg/Universal/Acpi/S3SaveStateDxe/S3SaveStateDxe.inf
   MdeModulePkg/Universal/Acpi/SmmS3SaveState/SmmS3SaveState.inf
   MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
+  MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.inf
   MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
   MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmDxe.inf
   MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.h b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.h
new file mode 100644
index 000000000000..0fbdac02de4c
--- /dev/null
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.h
@@ -0,0 +1,50 @@
+/** @file
+  This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
+
+  This module registers report status code listener to collect performance data
+  for SMM driver boot records and S3 Suspend Performance Record.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  FpdtSmiHandler() will receive untrusted input and do basic validation.
+
+  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c), Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _FW_PERF_COMMON_H_
+#define _FW_PERF_COMMON_H_
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  );
+
+/**
+  The module Entry Point of the Firmware Performance Data Table MM driver.
+
+  @retval EFI_SUCCESS    The entry point is executed successfully.
+  @retval Other          Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+FirmwarePerformanceCommonEntryPoint (
+  VOID
+  );
+
+#endif // _FW_PERF_COMMON_H_
diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
index 618cbd56ca59..b7194bd899dd 100644
--- a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
@@ -26,7 +26,9 @@ [Defines]
 #
 
 [Sources]
-  FirmwarePerformanceSmm.c
+  FirmwarePerformanceCommon.c
+  FirmwarePerformanceCommon.h
+  FirmwarePerformanceTraditional.c
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -34,7 +36,7 @@ [Packages]
 
 [LibraryClasses]
   UefiDriverEntryPoint
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseLib
   DebugLib
   TimerLib
@@ -42,12 +44,11 @@ [LibraryClasses]
   PcdLib
   BaseMemoryLib
   MemoryAllocationLib
-  UefiBootServicesTableLib
   SynchronizationLib
   SmmMemLib
 
 [Protocols]
-  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
+  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
 
 [Guids]
   ## SOMETIMES_PRODUCES   ## UNDEFINED # SaveLockBox
@@ -61,7 +62,7 @@ [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdProgressCodeS3SuspendEnd    ## CONSUMES
 
 [Depex]
-  gEfiSmmRscHandlerProtocolGuid
+  gEfiMmRscHandlerProtocolGuid
 
 [UserExtensions.TianoCore."ExtraFiles"]
   FirmwarePerformanceSmmExtra.uni
diff --git a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.inf
similarity index 65%
copy from MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
copy to MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.inf
index 618cbd56ca59..e6aad88be0ef 100644
--- a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
+++ b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceStandaloneMm.inf
@@ -5,19 +5,19 @@
 #  for SMM boot performance records and S3 Suspend Performance Record.
 #
 #  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = FirmwarePerformanceSmm
-  MODULE_UNI_FILE                = FirmwarePerformanceSmm.uni
-  FILE_GUID                      = 044310AB-77FD-402a-AF1A-87D4120E7329
-  MODULE_TYPE                    = DXE_SMM_DRIVER
+  BASE_NAME                      = FirmwarePerformanceStandaloneMm
+  FILE_GUID                      = 827AC29D-E52D-4B1A-874A-C6577E0699CF
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  PI_SPECIFICATION_VERSION       = 0x0001000A
-  ENTRY_POINT                    = FirmwarePerformanceSmmEntryPoint
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = FirmwarePerformanceStandaloneMmEntryPoint
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -26,15 +26,18 @@ [Defines]
 #
 
 [Sources]
-  FirmwarePerformanceSmm.c
+  FirmwarePerformanceCommon.c
+  FirmwarePerformanceCommon.h
+  FirmwarePerformanceStandaloneMm.c
 
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
 
 [LibraryClasses]
-  UefiDriverEntryPoint
-  SmmServicesTableLib
+  StandaloneMmDriverEntryPoint
+  MmServicesTableLib
   BaseLib
   DebugLib
   TimerLib
@@ -42,12 +45,11 @@ [LibraryClasses]
   PcdLib
   BaseMemoryLib
   MemoryAllocationLib
-  UefiBootServicesTableLib
   SynchronizationLib
-  SmmMemLib
+  MemLib
 
 [Protocols]
-  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
+  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
 
 [Guids]
   ## SOMETIMES_PRODUCES   ## UNDEFINED # SaveLockBox
@@ -61,7 +63,4 @@ [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdProgressCodeS3SuspendEnd    ## CONSUMES
 
 [Depex]
-  gEfiSmmRscHandlerProtocolGuid
-
-[UserExtensions.TianoCore."ExtraFiles"]
-  FirmwarePerformanceSmmExtra.uni
+  gEfiMmRscHandlerProtocolGuid
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (7 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06  3:24   ` Wu, Hao A
  2021-01-05 18:59 ` [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm Kun Qin
                   ` (8 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Dandan Bi, Liming Gao

This change added support of RSC router under StandaloneMm. It replaces
SMM version ReportStatusCode protocol definitions with MM version. This
patch also switched to use gMmst instead of gSmst. Lastly, it abstracts
standalone and traditional MM driver entrypoints into separate files to
allow maximal common implementations.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internally abstracted functions [Hao]
    - Updated function descriptions [Hao]
    - Updated ReportDispatcher to make *.h and *.c consistent [Hao]

 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/{ReportStatusCodeRouterSmm.c => ReportStatusCodeRouterCommon.c} | 55 +++++++++-----------
 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.c                            | 33 ++++++++++++
 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterTraditional.c                             | 33 ++++++++++++
 MdeModulePkg/MdeModulePkg.dsc                                                                                     |  1 +
 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/{ReportStatusCodeRouterSmm.h => ReportStatusCodeRouterCommon.h} | 44 ++++++++++------
 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf                                   | 13 ++---
 MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf                          | 49 +++++++++++++++++
 7 files changed, 176 insertions(+), 52 deletions(-)

diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.c b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.c
similarity index 75%
rename from MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.c
rename to MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.c
index c3ab5cd05045..d84efaa79b7f 100644
--- a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.c
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.c
@@ -7,7 +7,7 @@
 
 **/
 
-#include "ReportStatusCodeRouterSmm.h"
+#include "ReportStatusCodeRouterCommon.h"
 
 LIST_ENTRY   mCallbackListHead          = INITIALIZE_LIST_HEAD_VARIABLE (mCallbackListHead);
 
@@ -17,11 +17,11 @@ LIST_ENTRY   mCallbackListHead          = INITIALIZE_LIST_HEAD_VARIABLE (mCallba
 //
 UINT32       mStatusCodeNestStatus = 0;
 
-EFI_SMM_STATUS_CODE_PROTOCOL  mSmmStatusCodeProtocol  = {
+EFI_MM_STATUS_CODE_PROTOCOL   mSmmStatusCodeProtocol  = {
   ReportDispatcher
 };
 
-EFI_SMM_RSC_HANDLER_PROTOCOL  mSmmRscHandlerProtocol = {
+EFI_MM_RSC_HANDLER_PROTOCOL   mSmmRscHandlerProtocol = {
   Register,
   Unregister
   };
@@ -45,18 +45,18 @@ EFI_SMM_RSC_HANDLER_PROTOCOL  mSmmRscHandlerProtocol = {
 EFI_STATUS
 EFIAPI
 Register (
-  IN EFI_SMM_RSC_HANDLER_CALLBACK   Callback
+  IN EFI_MM_RSC_HANDLER_CALLBACK    Callback
   )
 {
   LIST_ENTRY                      *Link;
-  SMM_RSC_HANDLER_CALLBACK_ENTRY  *CallbackEntry;
+  MM_RSC_HANDLER_CALLBACK_ENTRY  *CallbackEntry;
 
   if (Callback == NULL) {
     return EFI_INVALID_PARAMETER;
   }
 
   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
-    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
+    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node, MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
     if (CallbackEntry->RscHandlerCallback == Callback) {
       //
       // If the function was already registered. It can't be registered again.
@@ -65,10 +65,10 @@ Register (
     }
   }
 
-  CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
+  CallbackEntry = (MM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool (sizeof (MM_RSC_HANDLER_CALLBACK_ENTRY));
   ASSERT (CallbackEntry != NULL);
 
-  CallbackEntry->Signature          = SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
+  CallbackEntry->Signature          = MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
   CallbackEntry->RscHandlerCallback = Callback;
 
   InsertTailList (&mCallbackListHead, &CallbackEntry->Node);
@@ -92,18 +92,18 @@ Register (
 EFI_STATUS
 EFIAPI
 Unregister (
-  IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
+  IN EFI_MM_RSC_HANDLER_CALLBACK  Callback
   )
 {
   LIST_ENTRY                        *Link;
-  SMM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
+  MM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
 
   if (Callback == NULL) {
     return EFI_INVALID_PARAMETER;
   }
 
   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link); Link = GetNextNode (&mCallbackListHead, Link)) {
-    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
+    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node, MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
     if (CallbackEntry->RscHandlerCallback == Callback) {
       //
       // If the function is found in list, delete it and return.
@@ -140,16 +140,16 @@ Unregister (
 EFI_STATUS
 EFIAPI
 ReportDispatcher (
-  IN CONST EFI_SMM_STATUS_CODE_PROTOCOL  *This,
-  IN EFI_STATUS_CODE_TYPE                Type,
+  IN CONST EFI_MM_STATUS_CODE_PROTOCOL   *This,
+  IN EFI_STATUS_CODE_TYPE                CodeType,
   IN EFI_STATUS_CODE_VALUE               Value,
   IN UINT32                              Instance,
-  IN CONST EFI_GUID                      *CallerId  OPTIONAL,
+  IN CONST EFI_GUID                      *CallerId,
   IN EFI_STATUS_CODE_DATA                *Data      OPTIONAL
   )
 {
   LIST_ENTRY                        *Link;
-  SMM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
+  MM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
 
   //
   // Use atom operation to avoid the reentant of report.
@@ -160,13 +160,13 @@ ReportDispatcher (
   }
 
   for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead, Link);) {
-    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node, SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
+    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node, MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
     //
     // The handler may remove itself, so get the next handler in advance.
     //
     Link = GetNextNode (&mCallbackListHead, Link);
     CallbackEntry->RscHandlerCallback (
-                     Type,
+                     CodeType,
                      Value,
                      Instance,
                      (EFI_GUID*)CallerId,
@@ -186,20 +186,15 @@ ReportDispatcher (
 /**
   Entry point of Generic Status Code Driver.
 
-  This function is the entry point of SMM Status Code Router .
-  It produces SMM Report Stataus Code Handler and Status Code protocol.
-
-  @param  ImageHandle       The firmware allocated handle for the EFI image.
-  @param  SystemTable       A pointer to the EFI System Table.
+  This function is the common entry point of MM Status Code Router.
+  It produces MM Report Status Code Handler and Status Code protocol.
 
   @retval EFI_SUCCESS       The entry point is executed successfully.
 
 **/
 EFI_STATUS
-EFIAPI
-GenericStatusCodeSmmEntry (
-  IN EFI_HANDLE         ImageHandle,
-  IN EFI_SYSTEM_TABLE   *SystemTable
+GenericStatusCodeCommonEntry (
+  VOID
   )
 {
   EFI_STATUS     Status;
@@ -210,9 +205,9 @@ GenericStatusCodeSmmEntry (
   //
   // Install SmmRscHandler Protocol
   //
-  Status = gSmst->SmmInstallProtocolInterface (
+  Status = gMmst->MmInstallProtocolInterface (
                     &Handle,
-                    &gEfiSmmRscHandlerProtocolGuid,
+                    &gEfiMmRscHandlerProtocolGuid,
                     EFI_NATIVE_INTERFACE,
                     &mSmmRscHandlerProtocol
                     );
@@ -221,9 +216,9 @@ GenericStatusCodeSmmEntry (
   //
   // Install SmmStatusCode Protocol
   //
-  Status = gSmst->SmmInstallProtocolInterface (
+  Status = gMmst->MmInstallProtocolInterface (
                     &Handle,
-                    &gEfiSmmStatusCodeProtocolGuid,
+                    &gEfiMmStatusCodeProtocolGuid,
                     EFI_NATIVE_INTERFACE,
                     &mSmmStatusCodeProtocol
                     );
diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.c b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.c
new file mode 100644
index 000000000000..bd1519fa1506
--- /dev/null
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.c
@@ -0,0 +1,33 @@
+/** @file
+  Report Status Code Router Driver which produces MM Report Stataus Code Handler Protocol
+  and MM Status Code Protocol.
+
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "ReportStatusCodeRouterCommon.h"
+
+/**
+  Entry point of Generic Status Code Driver.
+
+  This function is the entry point of MM Status Code Router .
+  It produces MM Report Stataus Code Handler and Status Code protocol.
+
+  @param  ImageHandle       The firmware allocated handle for the EFI image.
+  @param  SystemTable       A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+GenericStatusCodeStandaloneMmEntry (
+  IN EFI_HANDLE             ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE    *SystemTable
+  )
+{
+  return GenericStatusCodeCommonEntry ();
+}
diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterTraditional.c b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterTraditional.c
new file mode 100644
index 000000000000..360a0eef6b6d
--- /dev/null
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterTraditional.c
@@ -0,0 +1,33 @@
+/** @file
+  Report Status Code Router Driver which produces MM Report Stataus Code Handler Protocol
+  and MM Status Code Protocol.
+
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "ReportStatusCodeRouterCommon.h"
+
+/**
+  Entry point of Generic Status Code Driver.
+
+  This function is the entry point of SMM Status Code Router .
+  It produces SMM Report Stataus Code Handler and Status Code protocol.
+
+  @param  ImageHandle       The firmware allocated handle for the EFI image.
+  @param  SystemTable       A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+GenericStatusCodeTraditionalEntry (
+  IN EFI_HANDLE         ImageHandle,
+  IN EFI_SYSTEM_TABLE   *SystemTable
+  )
+{
+  return GenericStatusCodeCommonEntry ();
+}
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 34ca571ca662..f95c7cd69ee1 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -479,6 +479,7 @@ [Components.IA32, Components.X64]
   MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf
   MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerStandaloneMm.inf
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
+  MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf
   MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
   MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllocationProfileLib.inf
   MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationProfileLib.inf
diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.h b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.h
similarity index 73%
rename from MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.h
rename to MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.h
index f8c48c62e790..4ead2dcbdf2c 100644
--- a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.h
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterCommon.h
@@ -6,28 +6,26 @@
 
 **/
 
-#ifndef __REPORT_STATUS_CODE_ROUTER_SMM_H__
-#define __REPORT_STATUS_CODE_ROUTER_SMM_H__
+#ifndef __REPORT_STATUS_CODE_ROUTER_COMMON_H__
+#define __REPORT_STATUS_CODE_ROUTER_COMMON_H__
 
-
-#include <Protocol/SmmReportStatusCodeHandler.h>
-#include <Protocol/SmmStatusCode.h>
+#include <Protocol/MmReportStatusCodeHandler.h>
+#include <Protocol/MmStatusCode.h>
 
 #include <Library/BaseLib.h>
 #include <Library/SynchronizationLib.h>
 #include <Library/DebugLib.h>
 #include <Library/PcdLib.h>
-#include <Library/UefiDriverEntryPoint.h>
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/MemoryAllocationLib.h>
 
-#define SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE  SIGNATURE_32 ('s', 'h', 'c', 'e')
+#define MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE  SIGNATURE_32 ('s', 'h', 'c', 'e')
 
 typedef struct {
   UINTN                         Signature;
-  EFI_SMM_RSC_HANDLER_CALLBACK  RscHandlerCallback;
+  EFI_MM_RSC_HANDLER_CALLBACK   RscHandlerCallback;
   LIST_ENTRY                    Node;
-} SMM_RSC_HANDLER_CALLBACK_ENTRY;
+} MM_RSC_HANDLER_CALLBACK_ENTRY;
 
 /**
   Register the callback function for ReportStatusCode() notification.
@@ -48,7 +46,7 @@ typedef struct {
 EFI_STATUS
 EFIAPI
 Register (
-  IN EFI_SMM_RSC_HANDLER_CALLBACK   Callback
+  IN EFI_MM_RSC_HANDLER_CALLBACK    Callback
   );
 
 /**
@@ -67,13 +65,13 @@ Register (
 EFI_STATUS
 EFIAPI
 Unregister (
-  IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
+  IN EFI_MM_RSC_HANDLER_CALLBACK  Callback
   );
 
 /**
   Provides an interface that a software module can call to report a status code.
 
-  @param  This             EFI_SMM_STATUS_CODE_PROTOCOL instance.
+  @param  This             EFI_MM_RSC_HANDLER_CALLBACK instance.
   @param  Type             Indicates the type of status code being reported.
   @param  Value            Describes the current status of a hardware or software entity.
                            This included information about the class and subclass that is used to
@@ -92,12 +90,26 @@ Unregister (
 EFI_STATUS
 EFIAPI
 ReportDispatcher (
-  IN CONST EFI_SMM_STATUS_CODE_PROTOCOL  *This,
-  IN EFI_STATUS_CODE_TYPE                Type,
+  IN CONST EFI_MM_STATUS_CODE_PROTOCOL   *This,
+  IN EFI_STATUS_CODE_TYPE                CodeType,
   IN EFI_STATUS_CODE_VALUE               Value,
   IN UINT32                              Instance,
-  IN CONST EFI_GUID                      *CallerId  OPTIONAL,
+  IN CONST EFI_GUID                      *CallerId,
   IN EFI_STATUS_CODE_DATA                *Data      OPTIONAL
   );
 
+/**
+  Entry point of Generic Status Code Driver.
+
+  This function is the common entry point of MM Status Code Router.
+  It produces MM Report Status Code Handler and Status Code protocol.
+
+  @retval EFI_SUCCESS       The entry point is executed successfully.
+
+**/
+EFI_STATUS
+GenericStatusCodeCommonEntry (
+  VOID
+  );
+
 #endif
diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
index 46fdcb7bf959..539badc4c755 100644
--- a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
@@ -16,7 +16,7 @@ [Defines]
   MODULE_TYPE                    = DXE_SMM_DRIVER
   PI_SPECIFICATION_VERSION       = 0x0001000A
   VERSION_STRING                 = 1.0
-  ENTRY_POINT                    = GenericStatusCodeSmmEntry
+  ENTRY_POINT                    = GenericStatusCodeTraditionalEntry
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -25,15 +25,16 @@ [Defines]
 #
 
 [Sources]
-  ReportStatusCodeRouterSmm.c
-  ReportStatusCodeRouterSmm.h
+  ReportStatusCodeRouterCommon.c
+  ReportStatusCodeRouterCommon.h
+  ReportStatusCodeRouterTraditional.c
 
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
 
 [LibraryClasses]
-  SmmServicesTableLib
+  MmServicesTableLib
   UefiDriverEntryPoint
   DebugLib
   BaseLib
@@ -41,8 +42,8 @@ [LibraryClasses]
   MemoryAllocationLib
 
 [Protocols]
-  gEfiSmmRscHandlerProtocolGuid               ## PRODUCES
-  gEfiSmmStatusCodeProtocolGuid               ## PRODUCES
+  gEfiMmRscHandlerProtocolGuid               ## PRODUCES
+  gEfiMmStatusCodeProtocolGuid               ## PRODUCES
 
 [Depex]
   TRUE
diff --git a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf
new file mode 100644
index 000000000000..7aa0127e0bec
--- /dev/null
+++ b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf
@@ -0,0 +1,49 @@
+## @file
+#  Report Status Code Router Driver which produces MM Report Stataus Code Handler Protocol and MM Status Code Protocol.
+#
+#  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = ReportStatusCodeRouterStandaloneMm
+  FILE_GUID                      = EAEEDEF9-ABE7-4B95-82B0-5A534C899B46
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = GenericStatusCodeStandaloneMmEntry
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  ReportStatusCodeRouterCommon.c
+  ReportStatusCodeRouterCommon.h
+  ReportStatusCodeRouterStandaloneMm.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  MmServicesTableLib
+  StandaloneMmDriverEntryPoint
+  DebugLib
+  BaseLib
+  SynchronizationLib
+  MemoryAllocationLib
+
+[Protocols]
+  gEfiMmRscHandlerProtocolGuid               ## PRODUCES
+  gEfiMmStatusCodeProtocolGuid               ## PRODUCES
+
+[Depex]
+  TRUE
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (8 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib Kun Qin
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Michael D Kinney, Liming Gao, Zhiguang Liu

This change added an instance of UefiDevicePathLib for StandaloneMm. It
abstracts DevicePathFromHandle function into different files for
Standalone MM and other instances to avoid linking gBS into MM_STANDALONE
drivers.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - No review, no change.

 MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c                                        | 33 -------------
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c                                  | 51 ++++++++++++++++++++
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c                            | 40 +++++++++++++++
 MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf                                        |  1 +
 MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf              |  1 +
 MdePkg/Library/UefiDevicePathLib/{UefiDevicePathLib.inf => UefiDevicePathLibStandaloneMm.inf} | 11 +++--
 MdePkg/MdePkg.dsc                                                                             |  1 +
 7 files changed, 100 insertions(+), 38 deletions(-)

diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
index 9274ef8dda98..7d5fb18d2516 100644
--- a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
@@ -806,39 +806,6 @@ UefiDevicePathLibIsDevicePathMultiInstance (
 }
 
 
-/**
-  Retrieves the device path protocol from a handle.
-
-  This function returns the device path protocol from the handle specified by Handle.
-  If Handle is NULL or Handle does not contain a device path protocol, then NULL
-  is returned.
-
-  @param  Handle                     The handle from which to retrieve the device
-                                     path protocol.
-
-  @return The device path protocol from the handle specified by Handle.
-
-**/
-EFI_DEVICE_PATH_PROTOCOL *
-EFIAPI
-DevicePathFromHandle (
-  IN EFI_HANDLE                      Handle
-  )
-{
-  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
-  EFI_STATUS                Status;
-
-  Status = gBS->HandleProtocol (
-                  Handle,
-                  &gEfiDevicePathProtocolGuid,
-                  (VOID *) &DevicePath
-                  );
-  if (EFI_ERROR (Status)) {
-    DevicePath = NULL;
-  }
-  return DevicePath;
-}
-
 /**
   Allocates a device path for a file and appends it to an existing device path.
 
diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
new file mode 100644
index 000000000000..7f3b6076ef34
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
@@ -0,0 +1,51 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
+  EFI_STATUS                Status;
+
+  Status = gBS->HandleProtocol (
+                  Handle,
+                  &gEfiDevicePathProtocolGuid,
+                  (VOID *) &DevicePath
+                  );
+  if (EFI_ERROR (Status)) {
+    DevicePath = NULL;
+  }
+  return DevicePath;
+}
diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
new file mode 100644
index 000000000000..930e778d373a
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
@@ -0,0 +1,40 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  return NULL;
+}
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
index 3c41c0695a72..eb85a54a74c3 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
@@ -26,6 +26,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
index e812e3e1d41e..81323bc70061 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
@@ -29,6 +29,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLibOptionalDevicePathProtocol.c
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
similarity index 79%
copy from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
copy to MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
index 3c41c0695a72..23fedf38b7eb 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
@@ -4,6 +4,7 @@
 # Device Path Library that layers on top of the Memory Allocation Library.
 #
 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -14,10 +15,11 @@ [Defines]
   INF_VERSION                    = 0x00010005
   BASE_NAME                      = UefiDevicePathLib
   MODULE_UNI_FILE                = UefiDevicePathLib.uni
-  FILE_GUID                      = 91c1677a-e57f-4191-8b8e-eb7711a716e0
-  MODULE_TYPE                    = UEFI_DRIVER
+  FILE_GUID                      = D8E58437-44D3-4154-B7A7-EB794923EF12
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = DevicePathLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE MM_STANDALONE
+  LIBRARY_CLASS                  = DevicePathLib | MM_STANDALONE MM_CORE_STANDALONE
 
 
 #
@@ -26,6 +28,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesStandaloneMm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
@@ -34,7 +37,6 @@ [Sources]
 [Packages]
   MdePkg/MdePkg.dec
 
-
 [LibraryClasses]
   BaseLib
   MemoryAllocationLib
@@ -71,4 +73,3 @@ [Protocols]
 
 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdMaximumDevicePathNodeCount    ## SOMETIMES_CONSUMES
-
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 8d1a5b20c9cd..ce009086815f 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -109,6 +109,7 @@ [Components]
   MdePkg/Library/UefiDebugLibDebugPortProtocol/UefiDebugLibDebugPortProtocol.inf
   MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+  MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
   MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
   MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf
   MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (9 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-13  6:50   ` [edk2-devel] " Ni, Ray
  2021-01-05 18:59 ` [PATCH v2 12/16] SecurityPkg: Tcg2PhysicalPresenceLib: Introduce StandaloneMm instance Kun Qin
                   ` (6 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni

This change added a new instance of AcpiTimerLib for StandaloneMm core
and drivers. It centralizes the common routines into shared files and
abstract the library constructor into corresponding files to accommodate
each constructor function prototypes.

Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internal functions.

 PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.c => CommonAcpiTimerLib.c}         |  9 +--
 PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c                                   | 81 +-------------------
 PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c                            | 31 ++++++++
 PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h                                | 24 ++++++
 PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf                                 |  2 +
 PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.inf => StandaloneAcpiTimerLib.inf} | 19 +++--
 PcAtChipsetPkg/PcAtChipsetPkg.dsc                                                       |  1 +
 7 files changed, 74 insertions(+), 93 deletions(-)

diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
similarity index 86%
copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
copy to PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
index 3ad831b15e8a..0e401194d01d 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
@@ -72,17 +72,12 @@ InternalGetPerformanceCounterFrequency (
 /**
   The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
 
-  @param  ImageHandle   The firmware allocated handle for the EFI image.
-  @param  SystemTable   A pointer to the EFI System Table.
-
   @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
 
 **/
 EFI_STATUS
-EFIAPI
-DxeAcpiTimerLibConstructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
+CommonAcpiTimerLibConstructor (
+  VOID
   )
 {
   EFI_HOB_GUID_TYPE   *GuidHob;
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
index 3ad831b15e8a..e519ad8ed07e 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
@@ -2,72 +2,14 @@
   ACPI Timer implements one instance of Timer Library.
 
   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include <PiDxe.h>
-#include <Library/TimerLib.h>
-#include <Library/BaseLib.h>
-#include <Library/HobLib.h>
 
-extern GUID mFrequencyHobGuid;
-
-/**
-  The constructor function enables ACPI IO space.
-
-  If ACPI I/O space not enabled, this function will enable it.
-  It will always return RETURN_SUCCESS.
-
-  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
-
-**/
-RETURN_STATUS
-EFIAPI
-AcpiTimerLibConstructor (
-  VOID
-  );
-
-/**
-  Calculate TSC frequency.
-
-  The TSC counting frequency is determined by comparing how far it counts
-  during a 101.4 us period as determined by the ACPI timer.
-  The ACPI timer is used because it counts at a known frequency.
-  The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
-  or 101.4 us. The TSC is then sampled again. The difference multiplied by
-  9861 is the TSC frequency. There will be a small error because of the
-  overhead of reading the ACPI timer. An attempt is made to determine and
-  compensate for this error.
-
-  @return The number of TSC counts per second.
-
-**/
-UINT64
-InternalCalculateTscFrequency (
-  VOID
-  );
-
-//
-// Cached performance counter frequency
-//
-UINT64  mPerformanceCounterFrequency = 0;
-
-/**
-  Internal function to retrieves the 64-bit frequency in Hz.
-
-  Internal function to retrieves the 64-bit frequency in Hz.
-
-  @return The frequency in Hz.
-
-**/
-UINT64
-InternalGetPerformanceCounterFrequency (
-  VOID
-  )
-{
-  return  mPerformanceCounterFrequency;
-}
+#include "CommonAcpiTimerLib.h"
 
 /**
   The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
@@ -85,22 +27,5 @@ DxeAcpiTimerLibConstructor (
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_HOB_GUID_TYPE   *GuidHob;
-
-  //
-  // Enable ACPI IO space.
-  //
-  AcpiTimerLibConstructor ();
-
-  //
-  // Initialize PerformanceCounterFrequency
-  //
-  GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
-  if (GuidHob != NULL) {
-    mPerformanceCounterFrequency = *(UINT64*)GET_GUID_HOB_DATA (GuidHob);
-  } else {
-    mPerformanceCounterFrequency = InternalCalculateTscFrequency ();
-  }
-
-  return EFI_SUCCESS;
+  return CommonAcpiTimerLibConstructor ();
 }
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
new file mode 100644
index 000000000000..8509f94ba7cb
--- /dev/null
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
@@ -0,0 +1,31 @@
+/** @file
+  ACPI Timer implements one instance of Timer Library.
+
+  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include "CommonAcpiTimerLib.h"
+
+/**
+  The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+StandaloneMmAcpiTimerLibConstructor (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return CommonAcpiTimerLibConstructor ();
+}
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
new file mode 100644
index 000000000000..1bbdaef04cce
--- /dev/null
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
@@ -0,0 +1,24 @@
+/** @file
+  Header file internal to ACPI TimerLib.
+
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#ifndef _COMMON_ACPI_TIMER_LIB_H_
+#define _COMMON_ACPI_TIMER_LIB_H_
+
+/**
+  The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
+
+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
+
+**/
+EFI_STATUS
+CommonAcpiTimerLibConstructor (
+  VOID
+  );
+
+#endif
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
index d86356f4ff17..bbf14b3b5b53 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
@@ -25,6 +25,8 @@ [Defines]
 [Sources]
   AcpiTimerLib.c
   DxeAcpiTimerLib.c
+  CommonAcpiTimerLib.c
+  CommonAcpiTimerLib.h
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
similarity index 70%
copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
copy to PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
index d86356f4ff17..aa8f0012f744 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
@@ -1,5 +1,5 @@
 ## @file
-#  DXE ACPI Timer Library
+#  Standalone MM ACPI Timer Library
 #
 #  Provides basic timer support using the ACPI timer hardware.  The performance
 #  counter features are provided by the processors time stamp counter.
@@ -8,23 +8,26 @@
 #  is compatible with both 24-bit and 32-bit ACPI timers.
 #
 #  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = DxeAcpiTimerLib
-  FILE_GUID                      = E624B98C-845A-4b94-9B50-B20475D552B9
-  MODULE_TYPE                    = DXE_DRIVER
+  BASE_NAME                      = StandaloneAcpiTimerLib
+  FILE_GUID                      = C771858D-AF09-4D1A-B2F3-C7F081C3F076
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = TimerLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE
-  CONSTRUCTOR                    = DxeAcpiTimerLibConstructor
-  MODULE_UNI_FILE                = DxeAcpiTimerLib.uni
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = TimerLib|MM_CORE_STANDALONE MM_STANDALONE
+  CONSTRUCTOR                    = StandaloneMmAcpiTimerLibConstructor
 
 [Sources]
   AcpiTimerLib.c
-  DxeAcpiTimerLib.c
+  StandaloneAcpiTimerLib.c
+  CommonAcpiTimerLib.c
+  CommonAcpiTimerLib.h
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/PcAtChipsetPkg/PcAtChipsetPkg.dsc b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
index b61b7d1f528e..8862ca7a0006 100644
--- a/PcAtChipsetPkg/PcAtChipsetPkg.dsc
+++ b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
@@ -53,6 +53,7 @@ [Components]
   PcAtChipsetPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf
   PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
   PcAtChipsetPkg/Library/AcpiTimerLib/PeiAcpiTimerLib.inf
+  PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
   PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf
 
 [BuildOptions]
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 12/16] SecurityPkg: Tcg2PhysicalPresenceLib: Introduce StandaloneMm instance
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (10 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 13/16] SecurityPkg: Tcg2PpVendorLibNull: Added support for MM_STANDALONE type Kun Qin
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jiewen Yao, Jian J Wang, Qi Zhang, Rahul Kumar, Jiewen Yao

This change added a new instance of Tcg2PhysicalPresenceLib to support
MM_STANDALONE type drivers. It centralizes the common routines into
shared files and abstract the library constructor into corresponding
files to implement each constructor function prototypes.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Qi Zhang <qi1.zhang@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
---

Notes:
    v2:
    - Added Reviewed-by tag [Jiewen]
    - Removed "EFIAPI" for internal functions
    - Updated internal function description

 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/{SmmTcg2PhysicalPresenceLib.c => MmTcg2PhysicalPresenceLibCommon.c}         |  33 +-
 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c                                                | 368 +-------------------
 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.c                                       |  42 +++
 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.h                                           |  34 ++
 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf                                              |   6 +-
 SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/{SmmTcg2PhysicalPresenceLib.inf => StandaloneMmTcg2PhysicalPresenceLib.inf} |  22 +-
 SecurityPkg/SecurityPkg.dsc                                                                                                |   2 +
 7 files changed, 113 insertions(+), 394 deletions(-)

diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.c
similarity index 90%
copy from SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c
copy to SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.c
index 8afaa0a7857d..3788537db318 100644
--- a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.c
@@ -15,7 +15,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include <PiSmm.h>
+#include <PiMm.h>
 
 #include <Guid/Tcg2PhysicalPresenceData.h>
 
@@ -25,7 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/DebugLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/Tcg2PpVendorLib.h>
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 
 #define     PP_INF_VERSION_1_2    "1.2"
 
@@ -55,7 +55,7 @@ Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (
   UINTN                             DataSize;
   EFI_TCG2_PHYSICAL_PRESENCE        PpData;
 
-  DEBUG ((EFI_D_INFO, "[TPM2] ReturnOperationResponseToOsFunction\n"));
+  DEBUG ((DEBUG_INFO, "[TPM2] ReturnOperationResponseToOsFunction\n"));
 
   //
   // Get the Physical Presence variable
@@ -71,7 +71,7 @@ Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (
   if (EFI_ERROR (Status)) {
     *MostRecentRequest = 0;
     *Response          = 0;
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
+    DEBUG ((DEBUG_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
     return TCG_PP_RETURN_TPM_OPERATION_RESPONSE_FAILURE;
   }
 
@@ -108,7 +108,7 @@ Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
   EFI_TCG2_PHYSICAL_PRESENCE        PpData;
   EFI_TCG2_PHYSICAL_PRESENCE_FLAGS  Flags;
 
-  DEBUG ((EFI_D_INFO, "[TPM2] SubmitRequestToPreOSFunction, Request = %x, %x\n", *OperationRequest, *RequestParameter));
+  DEBUG ((DEBUG_INFO, "[TPM2] SubmitRequestToPreOSFunction, Request = %x, %x\n", *OperationRequest, *RequestParameter));
   ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS;
 
   //
@@ -123,7 +123,7 @@ Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
                                  &PpData
                                  );
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
+    DEBUG ((DEBUG_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
     ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;
     goto EXIT;
   }
@@ -147,7 +147,7 @@ Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
                                    &PpData
                                    );
     if (EFI_ERROR (Status)) {
-      DEBUG ((EFI_D_ERROR, "[TPM2] Set PP variable failure! Status = %r\n", Status));
+      DEBUG ((DEBUG_ERROR, "[TPM2] Set PP variable failure! Status = %r\n", Status));
       ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;
       goto EXIT;
     }
@@ -173,7 +173,7 @@ Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
   // Sync PPRQ/PPRM from PP Variable if PP submission fails
   //
   if (ReturnCode != TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Submit PP Request failure! Sync PPRQ/PPRM with PP variable.\n", Status));
+    DEBUG ((DEBUG_ERROR, "[TPM2] Submit PP Request failure! Sync PPRQ/PPRM with PP variable.\n", Status));
     DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
     ZeroMem(&PpData, DataSize);
     Status = mTcg2PpSmmVariable->SmmGetVariable (
@@ -245,7 +245,7 @@ Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
   EFI_TCG2_PHYSICAL_PRESENCE_FLAGS  Flags;
   BOOLEAN                           RequestConfirmed;
 
-  DEBUG ((EFI_D_INFO, "[TPM2] GetUserConfirmationStatusFunction, Request = %x\n", OperationRequest));
+  DEBUG ((DEBUG_INFO, "[TPM2] GetUserConfirmationStatusFunction, Request = %x\n", OperationRequest));
 
   //
   // Get the Physical Presence variable
@@ -259,7 +259,7 @@ Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
                                  &PpData
                                  );
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
+    DEBUG ((DEBUG_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
     return TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;
   }
   //
@@ -274,7 +274,7 @@ Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
                                  &Flags
                                  );
   if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP flags failure! Status = %r\n", Status));
+    DEBUG ((DEBUG_ERROR, "[TPM2] Get PP flags failure! Status = %r\n", Status));
     return TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;
   }
 
@@ -372,17 +372,12 @@ Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
 
   It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
 
-  @param  ImageHandle   The firmware allocated handle for the EFI image.
-  @param  SystemTable   A pointer to the EFI System Table.
-
   @retval EFI_SUCCESS   The constructor successfully added string package.
   @retval Other value   The constructor can't add string package.
 **/
 EFI_STATUS
-EFIAPI
-Tcg2PhysicalPresenceLibConstructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
+Tcg2PhysicalPresenceLibCommonConstructor (
+  VOID
   )
 {
   EFI_STATUS  Status;
@@ -394,7 +389,7 @@ Tcg2PhysicalPresenceLibConstructor (
   //
   // Locate SmmVariableProtocol.
   //
-  Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mTcg2PpSmmVariable);
+  Status = gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mTcg2PpSmmVariable);
   ASSERT_EFI_ERROR (Status);
 
   mTcg2PhysicalPresenceFlags = PcdGet32(PcdTcg2PhysicalPresenceFlags);
diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c
index 8afaa0a7857d..36d8b89dcdd9 100644
--- a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.c
@@ -17,355 +17,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include <PiSmm.h>
 
-#include <Guid/Tcg2PhysicalPresenceData.h>
-
-#include <Protocol/SmmVariable.h>
-
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/Tcg2PpVendorLib.h>
-#include <Library/SmmServicesTableLib.h>
-
-#define     PP_INF_VERSION_1_2    "1.2"
-
-EFI_SMM_VARIABLE_PROTOCOL  *mTcg2PpSmmVariable;
-BOOLEAN                    mIsTcg2PPVerLowerThan_1_3 = FALSE;
-UINT32                     mTcg2PhysicalPresenceFlags;
-
-/**
-  The handler for TPM physical presence function:
-  Return TPM Operation Response to OS Environment.
-
-  This API should be invoked in OS runtime phase to interface with ACPI method.
-
-  @param[out]     MostRecentRequest Most recent operation request.
-  @param[out]     Response          Response to the most recent operation request.
-
-  @return Return Code for Return TPM Operation Response to OS Environment.
-**/
-UINT32
-EFIAPI
-Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (
-  OUT UINT32                *MostRecentRequest,
-  OUT UINT32                *Response
-  )
-{
-  EFI_STATUS                        Status;
-  UINTN                             DataSize;
-  EFI_TCG2_PHYSICAL_PRESENCE        PpData;
-
-  DEBUG ((EFI_D_INFO, "[TPM2] ReturnOperationResponseToOsFunction\n"));
-
-  //
-  // Get the Physical Presence variable
-  //
-  DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
-  Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                 TCG2_PHYSICAL_PRESENCE_VARIABLE,
-                                 &gEfiTcg2PhysicalPresenceGuid,
-                                 NULL,
-                                 &DataSize,
-                                 &PpData
-                                 );
-  if (EFI_ERROR (Status)) {
-    *MostRecentRequest = 0;
-    *Response          = 0;
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
-    return TCG_PP_RETURN_TPM_OPERATION_RESPONSE_FAILURE;
-  }
-
-  *MostRecentRequest = PpData.LastPPRequest;
-  *Response          = PpData.PPResponse;
-
-  return TCG_PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS;
-}
-
-/**
-  The handler for TPM physical presence function:
-  Submit TPM Operation Request to Pre-OS Environment and
-  Submit TPM Operation Request to Pre-OS Environment 2.
-
-  This API should be invoked in OS runtime phase to interface with ACPI method.
-
-  Caution: This function may receive untrusted input.
-
-  @param[in, out]  Pointer to OperationRequest TPM physical presence operation request.
-  @param[in, out]  Pointer to RequestParameter TPM physical presence operation request parameter.
-
-  @return Return Code for Submit TPM Operation Request to Pre-OS Environment and
-        Submit TPM Operation Request to Pre-OS Environment 2.
-  **/
-UINT32
-Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx (
-  IN OUT UINT32               *OperationRequest,
-  IN OUT UINT32               *RequestParameter
-  )
-{
-  EFI_STATUS                        Status;
-  UINT32                            ReturnCode;
-  UINTN                             DataSize;
-  EFI_TCG2_PHYSICAL_PRESENCE        PpData;
-  EFI_TCG2_PHYSICAL_PRESENCE_FLAGS  Flags;
-
-  DEBUG ((EFI_D_INFO, "[TPM2] SubmitRequestToPreOSFunction, Request = %x, %x\n", *OperationRequest, *RequestParameter));
-  ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS;
-
-  //
-  // Get the Physical Presence variable
-  //
-  DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
-  Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                 TCG2_PHYSICAL_PRESENCE_VARIABLE,
-                                 &gEfiTcg2PhysicalPresenceGuid,
-                                 NULL,
-                                 &DataSize,
-                                 &PpData
-                                 );
-  if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
-    ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;
-    goto EXIT;
-  }
-
-  if ((*OperationRequest > TCG2_PHYSICAL_PRESENCE_NO_ACTION_MAX) &&
-      (*OperationRequest < TCG2_PHYSICAL_PRESENCE_STORAGE_MANAGEMENT_BEGIN) ) {
-    ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED;
-    goto EXIT;
-  }
-
-  if ((PpData.PPRequest != *OperationRequest) ||
-      (PpData.PPRequestParameter != *RequestParameter)) {
-    PpData.PPRequest = (UINT8)*OperationRequest;
-    PpData.PPRequestParameter = *RequestParameter;
-    DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
-    Status = mTcg2PpSmmVariable->SmmSetVariable (
-                                   TCG2_PHYSICAL_PRESENCE_VARIABLE,
-                                   &gEfiTcg2PhysicalPresenceGuid,
-                                   EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
-                                   DataSize,
-                                   &PpData
-                                   );
-    if (EFI_ERROR (Status)) {
-      DEBUG ((EFI_D_ERROR, "[TPM2] Set PP variable failure! Status = %r\n", Status));
-      ReturnCode = TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE;
-      goto EXIT;
-    }
-  }
-
-  if (*OperationRequest >= TCG2_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
-    DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE_FLAGS);
-    Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                   TCG2_PHYSICAL_PRESENCE_FLAGS_VARIABLE,
-                                   &gEfiTcg2PhysicalPresenceGuid,
-                                   NULL,
-                                   &DataSize,
-                                   &Flags
-                                   );
-    if (EFI_ERROR (Status)) {
-      Flags.PPFlags = mTcg2PhysicalPresenceFlags;
-    }
-    ReturnCode = Tcg2PpVendorLibSubmitRequestToPreOSFunction (*OperationRequest, Flags.PPFlags, *RequestParameter);
-  }
-
-EXIT:
-  //
-  // Sync PPRQ/PPRM from PP Variable if PP submission fails
-  //
-  if (ReturnCode != TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Submit PP Request failure! Sync PPRQ/PPRM with PP variable.\n", Status));
-    DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
-    ZeroMem(&PpData, DataSize);
-    Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                   TCG2_PHYSICAL_PRESENCE_VARIABLE,
-                                   &gEfiTcg2PhysicalPresenceGuid,
-                                   NULL,
-                                   &DataSize,
-                                   &PpData
-                                   );
-    *OperationRequest = (UINT32)PpData.PPRequest;
-    *RequestParameter = PpData.PPRequestParameter;
-  }
-
-  return ReturnCode;
-}
-
-/**
-  The handler for TPM physical presence function:
-  Submit TPM Operation Request to Pre-OS Environment and
-  Submit TPM Operation Request to Pre-OS Environment 2.
-
-  This API should be invoked in OS runtime phase to interface with ACPI method.
-
-  Caution: This function may receive untrusted input.
-
-  @param[in]      OperationRequest TPM physical presence operation request.
-  @param[in]      RequestParameter TPM physical presence operation request parameter.
-
-  @return Return Code for Submit TPM Operation Request to Pre-OS Environment and
-          Submit TPM Operation Request to Pre-OS Environment 2.
-**/
-UINT32
-EFIAPI
-Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (
-  IN UINT32                 OperationRequest,
-  IN UINT32                 RequestParameter
-  )
-{
-  UINT32                 TempOperationRequest;
-  UINT32                 TempRequestParameter;
-
-  TempOperationRequest = OperationRequest;
-  TempRequestParameter = RequestParameter;
-
-  return Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx(&TempOperationRequest, &TempRequestParameter);
-}
-
-/**
-  The handler for TPM physical presence function:
-  Get User Confirmation Status for Operation.
-
-  This API should be invoked in OS runtime phase to interface with ACPI method.
-
-  Caution: This function may receive untrusted input.
-
-  @param[in]      OperationRequest TPM physical presence operation request.
-
-  @return Return Code for Get User Confirmation Status for Operation.
-**/
-UINT32
-EFIAPI
-Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
-  IN UINT32                 OperationRequest
-  )
-{
-  EFI_STATUS                        Status;
-  UINTN                             DataSize;
-  EFI_TCG2_PHYSICAL_PRESENCE        PpData;
-  EFI_TCG2_PHYSICAL_PRESENCE_FLAGS  Flags;
-  BOOLEAN                           RequestConfirmed;
-
-  DEBUG ((EFI_D_INFO, "[TPM2] GetUserConfirmationStatusFunction, Request = %x\n", OperationRequest));
-
-  //
-  // Get the Physical Presence variable
-  //
-  DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE);
-  Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                 TCG2_PHYSICAL_PRESENCE_VARIABLE,
-                                 &gEfiTcg2PhysicalPresenceGuid,
-                                 NULL,
-                                 &DataSize,
-                                 &PpData
-                                 );
-  if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP variable failure! Status = %r\n", Status));
-    return TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;
-  }
-  //
-  // Get the Physical Presence flags
-  //
-  DataSize = sizeof (EFI_TCG2_PHYSICAL_PRESENCE_FLAGS);
-  Status = mTcg2PpSmmVariable->SmmGetVariable (
-                                 TCG2_PHYSICAL_PRESENCE_FLAGS_VARIABLE,
-                                 &gEfiTcg2PhysicalPresenceGuid,
-                                 NULL,
-                                 &DataSize,
-                                 &Flags
-                                 );
-  if (EFI_ERROR (Status)) {
-    DEBUG ((EFI_D_ERROR, "[TPM2] Get PP flags failure! Status = %r\n", Status));
-    return TCG_PP_GET_USER_CONFIRMATION_BLOCKED_BY_BIOS_CONFIGURATION;
-  }
-
-  RequestConfirmed = FALSE;
-
-  switch (OperationRequest) {
-    case TCG2_PHYSICAL_PRESENCE_CLEAR:
-    case TCG2_PHYSICAL_PRESENCE_ENABLE_CLEAR:
-    case TCG2_PHYSICAL_PRESENCE_ENABLE_CLEAR_2:
-    case TCG2_PHYSICAL_PRESENCE_ENABLE_CLEAR_3:
-      if ((Flags.PPFlags & TCG2_BIOS_TPM_MANAGEMENT_FLAG_PP_REQUIRED_FOR_CLEAR) == 0) {
-        RequestConfirmed = TRUE;
-      }
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_NO_ACTION:
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_CLEAR_TRUE:
-      RequestConfirmed = TRUE;
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_CLEAR_FALSE:
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS:
-      if ((Flags.PPFlags & TCG2_BIOS_TPM_MANAGEMENT_FLAG_PP_REQUIRED_FOR_CHANGE_PCRS) == 0) {
-        RequestConfirmed = TRUE;
-      }
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_CHANGE_EPS:
-      if ((Flags.PPFlags & TCG2_BIOS_TPM_MANAGEMENT_FLAG_PP_REQUIRED_FOR_CHANGE_EPS) == 0) {
-        RequestConfirmed = TRUE;
-      }
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_LOG_ALL_DIGESTS:
-      RequestConfirmed = TRUE;
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_ENABLE_BLOCK_SID:
-      if ((Flags.PPFlags & TCG2_BIOS_STORAGE_MANAGEMENT_FLAG_PP_REQUIRED_FOR_ENABLE_BLOCK_SID) == 0) {
-        RequestConfirmed = TRUE;
-      }
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_DISABLE_BLOCK_SID:
-      if ((Flags.PPFlags & TCG2_BIOS_STORAGE_MANAGEMENT_FLAG_PP_REQUIRED_FOR_DISABLE_BLOCK_SID) == 0) {
-        RequestConfirmed = TRUE;
-      }
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_ENABLE_BLOCK_SID_FUNC_TRUE:
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_DISABLE_BLOCK_SID_FUNC_TRUE:
-      RequestConfirmed = TRUE;
-      break;
-
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_ENABLE_BLOCK_SID_FUNC_FALSE:
-    case TCG2_PHYSICAL_PRESENCE_SET_PP_REQUIRED_FOR_DISABLE_BLOCK_SID_FUNC_FALSE:
-      break;
-
-    default:
-      if (!mIsTcg2PPVerLowerThan_1_3) {
-        if (OperationRequest < TCG2_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
-          //
-          // TCG2 PP1.3 spec defined operations that are reserved or un-implemented
-          //
-          return TCG_PP_GET_USER_CONFIRMATION_NOT_IMPLEMENTED;
-        }
-      } else {
-       //
-       // TCG PP lower than 1.3. (1.0, 1.1, 1.2)
-       //
-       if (OperationRequest <= TCG2_PHYSICAL_PRESENCE_NO_ACTION_MAX) {
-         RequestConfirmed = TRUE;
-       } else if (OperationRequest < TCG2_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
-         return TCG_PP_GET_USER_CONFIRMATION_NOT_IMPLEMENTED;
-       }
-      }
-      break;
-  }
-
-  if (OperationRequest >= TCG2_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
-    return Tcg2PpVendorLibGetUserConfirmationStatusFunction (OperationRequest, Flags.PPFlags);
-  }
-
-  if (RequestConfirmed) {
-    return TCG_PP_GET_USER_CONFIRMATION_ALLOWED_AND_PPUSER_NOT_REQUIRED;
-  } else {
-    return TCG_PP_GET_USER_CONFIRMATION_ALLOWED_AND_PPUSER_REQUIRED;
-  }
-}
+#include "MmTcg2PhysicalPresenceLibCommon.h"
 
 /**
   The constructor function locates SmmVariable protocol.
@@ -380,24 +32,10 @@ Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (
 **/
 EFI_STATUS
 EFIAPI
-Tcg2PhysicalPresenceLibConstructor (
+Tcg2PhysicalPresenceLibTraditionalConstructor (
   IN EFI_HANDLE        ImageHandle,
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_STATUS  Status;
-
-  if (AsciiStrnCmp(PP_INF_VERSION_1_2, (CHAR8 *)PcdGetPtr(PcdTcgPhysicalPresenceInterfaceVer), sizeof(PP_INF_VERSION_1_2) - 1) >= 0) {
-    mIsTcg2PPVerLowerThan_1_3 = TRUE;
-  }
-
-  //
-  // Locate SmmVariableProtocol.
-  //
-  Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mTcg2PpSmmVariable);
-  ASSERT_EFI_ERROR (Status);
-
-  mTcg2PhysicalPresenceFlags = PcdGet32(PcdTcg2PhysicalPresenceFlags);
-
-  return EFI_SUCCESS;
+  return Tcg2PhysicalPresenceLibCommonConstructor ();
 }
diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.c b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.c
new file mode 100644
index 000000000000..5c298a8d5720
--- /dev/null
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.c
@@ -0,0 +1,42 @@
+/** @file
+  Handle TPM 2.0 physical presence requests from OS.
+
+  This library will handle TPM 2.0 physical presence request from OS.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - variable.
+  This external input must be validated carefully to avoid security issue.
+
+  Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction() and Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction()
+  will receive untrusted input and do validation.
+
+Copyright (c) 2015 - 2020, Intel Corporation. All rights reserved.<BR>
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include "MmTcg2PhysicalPresenceLibCommon.h"
+
+/**
+  The constructor function locates SmmVariable protocol.
+
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The constructor successfully added string package.
+  @retval Other value   The constructor can't add string package.
+**/
+EFI_STATUS
+EFIAPI
+Tcg2PhysicalPresenceLibStandaloneMmConstructor (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return Tcg2PhysicalPresenceLibCommonConstructor ();
+}
diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.h b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.h
new file mode 100644
index 000000000000..a0182739e9c8
--- /dev/null
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/MmTcg2PhysicalPresenceLibCommon.h
@@ -0,0 +1,34 @@
+/** @file
+  Handle TPM 2.0 physical presence requests from OS.
+
+  This library will handle TPM 2.0 physical presence request from OS.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - variable.
+  This external input must be validated carefully to avoid security issue.
+
+  Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction() and Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction()
+  will receive untrusted input and do validation.
+
+Copyright (c) 2015 - 2020, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _MM_TCG2_PHYSICAL_PRESENCE_LIB_COMMON_H_
+#define _MM_TCG2_PHYSICAL_PRESENCE_LIB_COMMON_H_
+
+/**
+  The constructor function locates MmVariable protocol.
+
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
+
+  @retval EFI_SUCCESS   The constructor successfully added string package.
+  @retval Other value   The constructor can't add string package.
+**/
+EFI_STATUS
+Tcg2PhysicalPresenceLibCommonConstructor (
+  VOID
+  );
+
+#endif
diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
index 6a9bdf66f0a6..d911adbdb648 100644
--- a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
@@ -20,7 +20,7 @@ [Defines]
   MODULE_TYPE                    = DXE_SMM_DRIVER
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = Tcg2PhysicalPresenceLib|DXE_SMM_DRIVER
-  CONSTRUCTOR                    = Tcg2PhysicalPresenceLibConstructor
+  CONSTRUCTOR                    = Tcg2PhysicalPresenceLibTraditionalConstructor
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -30,6 +30,8 @@ [Defines]
 
 [Sources]
   SmmTcg2PhysicalPresenceLib.c
+  MmTcg2PhysicalPresenceLibCommon.c
+  MmTcg2PhysicalPresenceLibCommon.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -39,7 +41,7 @@ [Packages]
 [LibraryClasses]
   DebugLib
   Tcg2PpVendorLib
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseMemoryLib
 
 [Guids]
diff --git a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.inf
similarity index 64%
copy from SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
copy to SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.inf
index 6a9bdf66f0a6..6d11b6b9f198 100644
--- a/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
+++ b/SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.inf
@@ -8,19 +8,20 @@
 #  This external input must be validated carefully to avoid security issue.
 #
 # Copyright (c) 2015 - 2020, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = SmmTcg2PhysicalPresenceLib
-  MODULE_UNI_FILE                = SmmTcg2PhysicalPresenceLib.uni
-  FILE_GUID                      = AAE02741-858B-4964-9887-CA870489D944
-  MODULE_TYPE                    = DXE_SMM_DRIVER
+  BASE_NAME                      = StandaloneMmTcg2PhysicalPresenceLib
+  FILE_GUID                      = 75E3D07B-689C-4F42-A8A0-46AFAE868A6F
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = Tcg2PhysicalPresenceLib|DXE_SMM_DRIVER
-  CONSTRUCTOR                    = Tcg2PhysicalPresenceLibConstructor
+  LIBRARY_CLASS                  = Tcg2PhysicalPresenceLib|MM_STANDALONE
+  CONSTRUCTOR                    = Tcg2PhysicalPresenceLibStandaloneMmConstructor
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -29,7 +30,9 @@ [Defines]
 #
 
 [Sources]
-  SmmTcg2PhysicalPresenceLib.c
+  StandaloneMmTcg2PhysicalPresenceLib.c
+  MmTcg2PhysicalPresenceLibCommon.c
+  MmTcg2PhysicalPresenceLibCommon.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -39,7 +42,7 @@ [Packages]
 [LibraryClasses]
   DebugLib
   Tcg2PpVendorLib
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseMemoryLib
 
 [Guids]
@@ -48,6 +51,9 @@ [Guids]
   ## SOMETIMES_CONSUMES ## Variable:L"PhysicalPresenceFlags"
   gEfiTcg2PhysicalPresenceGuid
 
+[Protocols]
+  gEfiSmmVariableProtocolGuid                                       ## CONSUMES
+
 [Pcd]
   gEfiSecurityPkgTokenSpaceGuid.PcdTcgPhysicalPresenceInterfaceVer  ## CONSUMES
   gEfiSecurityPkgTokenSpaceGuid.PcdTcg2PhysicalPresenceFlags        ## SOMETIMES_CONSUMES
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index 36d15b79f928..7240b2573e4e 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -150,6 +150,7 @@ [LibraryClasses.common.UEFI_DRIVER, LibraryClasses.common.UEFI_APPLICATION]
 [LibraryClasses.common.DXE_SMM_DRIVER]
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
   SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
+  MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
   MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
   ReportStatusCodeLib|MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf
   SmmMemLib|MdePkg/Library/SmmMemLib/SmmMemLib.inf
@@ -316,6 +317,7 @@ [Components.IA32, Components.X64]
   SecurityPkg/Tcg/TcgSmm/TcgSmm.inf
   SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.inf
   SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/SmmTcg2PhysicalPresenceLib.inf
+  SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/StandaloneMmTcg2PhysicalPresenceLib.inf
 
   #
   # Random Number Generator
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 13/16] SecurityPkg: Tcg2PpVendorLibNull: Added support for MM_STANDALONE type
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (11 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 12/16] SecurityPkg: Tcg2PhysicalPresenceLib: Introduce StandaloneMm instance Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 14/16] SecurityPkg: Tpm2DeviceLibDTpm: Introduce StandaloneMm instance Kun Qin
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jiewen Yao, Jian J Wang, Qi Zhang, Rahul Kumar, Jiewen Yao

This change extends this null instance of Tcg2PpVendorLib to support
MM_STANDALONE drivers.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Qi Zhang <qi1.zhang@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
---

Notes:
    v2:
    - Added Reviewed-by tag [Jiewen]

 SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf b/SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf
index b969cbf9afff..9b18a1ab82f1 100644
--- a/SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf
+++ b/SecurityPkg/Library/Tcg2PpVendorLibNull/Tcg2PpVendorLibNull.inf
@@ -13,7 +13,7 @@ [Defines]
   FILE_GUID                      = 51924AE9-BE81-4820-94BA-7C9546E702D0
   MODULE_TYPE                    = DXE_DRIVER
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = Tcg2PpVendorLib|DXE_RUNTIME_DRIVER DXE_SMM_DRIVER DXE_DRIVER
+  LIBRARY_CLASS                  = Tcg2PpVendorLib|DXE_RUNTIME_DRIVER DXE_SMM_DRIVER DXE_DRIVER MM_STANDALONE
 
 #
 # The following information is for reference only and not required by the build tools.
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 14/16] SecurityPkg: Tpm2DeviceLibDTpm: Introduce StandaloneMm instance
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (12 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 13/16] SecurityPkg: Tcg2PpVendorLibNull: Added support for MM_STANDALONE type Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-05 18:59 ` [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm Kun Qin
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Jiewen Yao, Jian J Wang, Qi Zhang, Rahul Kumar, Jiewen Yao

This change added a new instance of Tpm2DeviceLibDTpm to support drivers
of type MM_STANDALONE. It abstracts dynamic Pcd access into separate file
for different instances to avoid dynamic usage for StandaloneMm modules.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Qi Zhang <qi1.zhang@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>
---

Notes:
    v2:
    - Added Reviewed-by tag [Jiewen]
    - Removed "EFIAPI" for internal functions

 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c                                          | 42 +-----------
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmBase.c                                      | 68 ++++++++++++++++++++
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.c                              | 66 +++++++++++++++++++
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c                                        | 40 +-----------
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c                                                    | 15 +++--
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.h                                          | 67 +++++++++++++++++++
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf                                        |  3 +
 SecurityPkg/Library/Tpm2DeviceLibDTpm/{Tpm2DeviceLibDTpm.inf => Tpm2DeviceLibDTpmStandaloneMm.inf} | 13 ++--
 SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf                                      |  3 +
 SecurityPkg/SecurityPkg.dsc                                                                        |  1 +
 10 files changed, 228 insertions(+), 90 deletions(-)

diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c
index 42e1ecbce95a..238389dbdb1b 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.c
@@ -13,29 +13,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/Tpm2DeviceLib.h>
 #include <Library/PcdLib.h>
 
-/**
-  Return PTP interface type.
-
-  @param[in] Register                Pointer to PTP register.
-
-  @return PTP interface type.
-**/
-TPM2_PTP_INTERFACE_TYPE
-Tpm2GetPtpInterface (
-  IN VOID *Register
-  );
-
-/**
-  Return PTP CRB interface IdleByPass state.
-
-  @param[in] Register                Pointer to PTP register.
-
-  @return PTP CRB interface IdleByPass state.
-**/
-UINT8
-Tpm2GetIdleByPass (
-  IN VOID *Register
-  );
+#include "Tpm2DeviceLibDTpm.h"
 
 /**
   This service enables the sending of commands to the TPM2.
@@ -145,21 +123,5 @@ Tpm2DeviceLibConstructor (
   VOID
   )
 {
-  TPM2_PTP_INTERFACE_TYPE  PtpInterface;
-  UINT8                    IdleByPass;
-
-  //
-  // Cache current active TpmInterfaceType only when needed
-  //
-  if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
-    PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
-    PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
-  }
-
-  if (PcdGet8(PcdActiveTpmInterfaceType) == Tpm2PtpInterfaceCrb && PcdGet8(PcdCRBIdleByPass) == 0xFF) {
-    IdleByPass = Tpm2GetIdleByPass((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
-    PcdSet8S(PcdCRBIdleByPass, IdleByPass);
-  }
-
-  return EFI_SUCCESS;
+  return InternalTpm2DeviceLibDTpmCommonConstructor ();
 }
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmBase.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmBase.c
new file mode 100644
index 000000000000..bc35e257e105
--- /dev/null
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmBase.c
@@ -0,0 +1,68 @@
+/** @file
+  This file abstract internal interfaces of which implementation differs per library instance.
+
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/Tpm2DeviceLib.h>
+#include <Library/PcdLib.h>
+
+#include "Tpm2DeviceLibDTpm.h"
+
+/**
+  Return cached PTP CRB interface IdleByPass state.
+
+  @return Cached PTP CRB interface IdleByPass state.
+**/
+UINT8
+GetCachedIdleByPass (
+  VOID
+  )
+{
+  return PcdGet8(PcdCRBIdleByPass);
+}
+
+/**
+  Return cached PTP interface type.
+
+  @return Cached PTP interface type.
+**/
+TPM2_PTP_INTERFACE_TYPE
+GetCachedPtpInterface (
+  VOID
+  )
+{
+  return PcdGet8(PcdActiveTpmInterfaceType);
+}
+
+/**
+  The common function cache current active TpmInterfaceType when needed.
+
+  @retval EFI_SUCCESS   DTPM2.0 instance is registered, or system does not support register DTPM2.0 instance
+**/
+EFI_STATUS
+InternalTpm2DeviceLibDTpmCommonConstructor (
+  VOID
+  )
+{
+  TPM2_PTP_INTERFACE_TYPE  PtpInterface;
+  UINT8                    IdleByPass;
+
+  //
+  // Cache current active TpmInterfaceType only when needed
+  //
+  if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
+    PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
+    PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
+  }
+
+  if (PcdGet8(PcdActiveTpmInterfaceType) == Tpm2PtpInterfaceCrb && PcdGet8(PcdCRBIdleByPass) == 0xFF) {
+    IdleByPass = Tpm2GetIdleByPass((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
+    PcdSet8S(PcdCRBIdleByPass, IdleByPass);
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.c
new file mode 100644
index 000000000000..eac866d2a77a
--- /dev/null
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.c
@@ -0,0 +1,66 @@
+/** @file
+  This file abstract internal interfaces of which implementation differs per library instance.
+
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/Tpm2DeviceLib.h>
+
+#include "Tpm2DeviceLibDTpm.h"
+
+TPM2_PTP_INTERFACE_TYPE  mActiveTpmInterfaceType;
+UINT8                    mCRBIdleByPass;
+
+/**
+  Return cached PTP CRB interface IdleByPass state.
+
+  @return Cached PTP CRB interface IdleByPass state.
+**/
+UINT8
+GetCachedIdleByPass (
+  VOID
+  )
+{
+  return mCRBIdleByPass;
+}
+
+/**
+  Return cached PTP interface type.
+
+  @return Cached PTP interface type.
+**/
+TPM2_PTP_INTERFACE_TYPE
+GetCachedPtpInterface (
+  VOID
+  )
+{
+  return mActiveTpmInterfaceType;
+}
+
+/**
+  The common function cache current active TpmInterfaceType when needed.
+
+  @retval EFI_SUCCESS   DTPM2.0 instance is registered, or system does not support register DTPM2.0 instance
+**/
+EFI_STATUS
+InternalTpm2DeviceLibDTpmCommonConstructor (
+  VOID
+  )
+{
+  mActiveTpmInterfaceType = 0xFF;
+  mCRBIdleByPass = 0xFF;
+
+  //
+  // Always cache current active TpmInterfaceType for StandaloneMm implementation
+  //
+  mActiveTpmInterfaceType = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
+
+  if (mActiveTpmInterfaceType == Tpm2PtpInterfaceCrb) {
+    mCRBIdleByPass = Tpm2GetIdleByPass((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c
index 691eaa40c045..053e597d2ee2 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.c
@@ -16,29 +16,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include <Guid/TpmInstance.h>
 
-/**
-  Return PTP interface type.
-
-  @param[in] Register                Pointer to PTP register.
-
-  @return PTP interface type.
-**/
-TPM2_PTP_INTERFACE_TYPE
-Tpm2GetPtpInterface (
-  IN VOID *Register
-  );
-
-/**
-  Return PTP CRB interface IdleByPass state.
-
-  @param[in] Register                Pointer to PTP register.
-
-  @return PTP CRB interface IdleByPass state.
-**/
-UINT8
-Tpm2GetIdleByPass (
-  IN VOID *Register
-  );
+#include "Tpm2DeviceLibDTpm.h"
 
 /**
   Dump PTP register information.
@@ -102,8 +80,6 @@ Tpm2InstanceLibDTpmConstructor (
   )
 {
   EFI_STATUS               Status;
-  TPM2_PTP_INTERFACE_TYPE  PtpInterface;
-  UINT8                    IdleByPass;
 
   Status = Tpm2RegisterTpm2DeviceLib (&mDTpm2InternalTpm2Device);
   if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
@@ -111,19 +87,7 @@ Tpm2InstanceLibDTpmConstructor (
     // Unsupported means platform policy does not need this instance enabled.
     //
     if (Status == EFI_SUCCESS) {
-      //
-      // Cache current active TpmInterfaceType only when needed
-      //
-      if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
-        PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
-        PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
-      }
-
-      if (PcdGet8(PcdActiveTpmInterfaceType) == Tpm2PtpInterfaceCrb && PcdGet8(PcdCRBIdleByPass) == 0xFF) {
-        IdleByPass = Tpm2GetIdleByPass((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
-        PcdSet8S(PcdCRBIdleByPass, IdleByPass);
-      }
-
+      Status = InternalTpm2DeviceLibDTpmCommonConstructor ();
       DumpPtpInfo ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
     }
     return EFI_SUCCESS;
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c
index 2c73385b6ce5..f1f80916834f 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Ptp.c
@@ -2,6 +2,7 @@
   PTP (Platform TPM Profile) CRB (Command Response Buffer) interface used by dTPM2.0 library.
 
 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c), Microsoft Corporation.
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -19,6 +20,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <IndustryStandard/TpmPtp.h>
 #include <IndustryStandard/TpmTis.h>
 
+#include "Tpm2DeviceLibDTpm.h"
+
 //
 // Execution of the command may take from several seconds to minutes for certain
 // commands, such as key generation.
@@ -174,7 +177,7 @@ PtpCrbTpmCommand (
   // STEP 0:
   // if CapCRbIdelByPass == 0, enforce Idle state before sending command
   //
-  if (PcdGet8(PcdCRBIdleByPass) == 0 && (MmioRead32((UINTN)&CrbReg->CrbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) == 0){
+  if (GetCachedIdleByPass () == 0 && (MmioRead32((UINTN)&CrbReg->CrbControlStatus) & PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE) == 0){
     Status = PtpCrbWaitRegisterBits (
               &CrbReg->CrbControlStatus,
               PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,
@@ -330,7 +333,7 @@ PtpCrbTpmCommand (
   // Goto Ready State if command is completed successfully and TPM support IdleBypass
   // If not supported. flow down to GoIdle
   //
-  if (PcdGet8(PcdCRBIdleByPass) == 1) {
+  if (GetCachedIdleByPass () == 1) {
     MmioWrite32((UINTN)&CrbReg->CrbControlRequest, PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY);
     return Status;
   }
@@ -350,7 +353,7 @@ PtpCrbTpmCommand (
   // Only enforce Idle state transition if execution fails when CRBIdleBypass==1
   // Leave regular Idle delay at the beginning of next command execution
   //
-  if (PcdGet8(PcdCRBIdleByPass) == 1){
+  if (GetCachedIdleByPass () == 1){
     Status = PtpCrbWaitRegisterBits (
                &CrbReg->CrbControlStatus,
                PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE,
@@ -519,7 +522,7 @@ DumpPtpInfo (
   Vid = 0xFFFF;
   Did = 0xFFFF;
   Rid = 0xFF;
-  PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
+  PtpInterface = GetCachedPtpInterface ();
   DEBUG ((EFI_D_INFO, "PtpInterface - %x\n", PtpInterface));
   switch (PtpInterface) {
   case Tpm2PtpInterfaceCrb:
@@ -564,7 +567,7 @@ DTpm2SubmitCommand (
 {
   TPM2_PTP_INTERFACE_TYPE  PtpInterface;
 
-  PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
+  PtpInterface = GetCachedPtpInterface ();
   switch (PtpInterface) {
   case Tpm2PtpInterfaceCrb:
     return PtpCrbTpmCommand (
@@ -603,7 +606,7 @@ DTpm2RequestUseTpm (
 {
   TPM2_PTP_INTERFACE_TYPE  PtpInterface;
 
-  PtpInterface = PcdGet8(PcdActiveTpmInterfaceType);
+  PtpInterface = GetCachedPtpInterface ();
   switch (PtpInterface) {
   case Tpm2PtpInterfaceCrb:
     return PtpCrbRequestUseTpm ((PTP_CRB_REGISTERS_PTR) (UINTN) PcdGet64 (PcdTpmBaseAddress));
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.h b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.h
new file mode 100644
index 000000000000..9fff98952251
--- /dev/null
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.h
@@ -0,0 +1,67 @@
+/** @file
+  This header file includes common internal fuction prototypes.
+
+Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _TPM2_DEVICE_LIB_DTPM_H_
+#define _TPM2_DEVICE_LIB_DTPM_H_
+
+/**
+  Return PTP interface type.
+
+  @param[in] Register                Pointer to PTP register.
+
+  @return PTP interface type.
+**/
+TPM2_PTP_INTERFACE_TYPE
+Tpm2GetPtpInterface (
+  IN VOID *Register
+  );
+
+/**
+  Return PTP CRB interface IdleByPass state.
+
+  @param[in] Register                Pointer to PTP register.
+
+  @return PTP CRB interface IdleByPass state.
+**/
+UINT8
+Tpm2GetIdleByPass (
+  IN VOID *Register
+  );
+
+/**
+  Return cached PTP interface type.
+
+  @return Cached PTP interface type.
+**/
+TPM2_PTP_INTERFACE_TYPE
+GetCachedPtpInterface (
+  VOID
+  );
+
+/**
+  Return cached PTP CRB interface IdleByPass state.
+
+  @return Cached PTP CRB interface IdleByPass state.
+**/
+UINT8
+GetCachedIdleByPass (
+  VOID
+  );
+
+/**
+  The common function cache current active TpmInterfaceType when needed.
+
+  @retval EFI_SUCCESS   DTPM2.0 instance is registered, or system does not support register DTPM2.0 instance
+**/
+EFI_STATUS
+InternalTpm2DeviceLibDTpmCommonConstructor (
+  VOID
+  );
+
+#endif // _TPM2_DEVICE_LIB_DTPM_H_
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
index 2b627504634d..be3a0053ccce 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
@@ -11,6 +11,7 @@
 #  only uses TPM 2.0 DTPM device.
 #
 # Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -34,6 +35,8 @@ [Sources]
   Tpm2Tis.c
   Tpm2Ptp.c
   Tpm2DeviceLibDTpm.c
+  Tpm2DeviceLibDTpmBase.c
+  Tpm2DeviceLibDTpm.h
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.inf
similarity index 70%
copy from SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
copy to SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.inf
index 2b627504634d..18c08ad8bdcc 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.inf
@@ -11,19 +11,20 @@
 #  only uses TPM 2.0 DTPM device.
 #
 # Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = Tpm2DeviceLibDTpm
-  MODULE_UNI_FILE                = Tpm2DeviceLibDTpm.uni
-  FILE_GUID                      = E54A3327-A345-4068-8842-70AC0D519855
+  BASE_NAME                      = Tpm2DeviceLibDTpmStandaloneMm
+  FILE_GUID                      = 9A5DB21A-FF0B-46D0-8672-B4F83FEF1F0E
   MODULE_TYPE                    = BASE
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = Tpm2DeviceLib|PEIM DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER
+  LIBRARY_CLASS                  = Tpm2DeviceLib|MM_STANDALONE
   CONSTRUCTOR                    = Tpm2DeviceLibConstructor
+
 #
 # The following information is for reference only and not required by the build tools.
 #
@@ -34,6 +35,8 @@ [Sources]
   Tpm2Tis.c
   Tpm2Ptp.c
   Tpm2DeviceLibDTpm.c
+  Tpm2DeviceLibDTpmStandaloneMm.c
+  Tpm2DeviceLibDTpm.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -49,5 +52,3 @@ [LibraryClasses]
 
 [Pcd]
   gEfiSecurityPkgTokenSpaceGuid.PcdTpmBaseAddress            ## CONSUMES
-  gEfiSecurityPkgTokenSpaceGuid.PcdActiveTpmInterfaceType    ## PRODUCES
-  gEfiSecurityPkgTokenSpaceGuid.PcdCRBIdleByPass             ## PRODUCES
diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf
index 5f267f552ce3..31113d93ee41 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf
@@ -6,6 +6,7 @@
 #  and PTP (Platform TPM Profile) functions.
 #
 # Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -30,6 +31,8 @@ [Sources]
   Tpm2Tis.c
   Tpm2Ptp.c
   Tpm2InstanceLibDTpm.c
+  Tpm2DeviceLibDTpmBase.c
+  Tpm2DeviceLibDTpm.h
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/SecurityPkg/SecurityPkg.dsc b/SecurityPkg/SecurityPkg.dsc
index 7240b2573e4e..618420a56c33 100644
--- a/SecurityPkg/SecurityPkg.dsc
+++ b/SecurityPkg/SecurityPkg.dsc
@@ -211,6 +211,7 @@ [Components]
   SecurityPkg/Library/Tpm2DeviceLibTcg2/Tpm2DeviceLibTcg2.inf
   SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpm.inf
   SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2InstanceLibDTpm.inf
+  SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2DeviceLibDTpmStandaloneMm.inf
   SecurityPkg/Library/Tpm2DeviceLibRouter/Tpm2DeviceLibRouterDxe.inf
   SecurityPkg/Library/Tpm2DeviceLibRouter/Tpm2DeviceLibRouterPei.inf
 
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (13 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 14/16] SecurityPkg: Tpm2DeviceLibDTpm: Introduce StandaloneMm instance Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06 15:46   ` Laszlo Ersek
  2021-01-05 18:59 ` [PATCH v2 16/16] UefiCpuPkg: SmmCpuExceptionHandlerLib: Added StandaloneMm module support Kun Qin
                   ` (2 subsequent siblings)
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong, Ray Ni, Laszlo Ersek, Rahul Kumar

This change added support of CpuIo driver under StandaloneMm. The core
services switched to gMmst based instead of gMmst. It also abstracts
standalone and traditional MM driver entrypoints into separate files to
allow maximal common implementations.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internal functions.

 UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.c => CpuIo2Common.c}           |  15 +-
 UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c                               | 384 +-------------------
 UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c                      |  31 ++
 UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.h => CpuIo2Common.h}           |  17 +-
 UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf                             |   7 +-
 UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.inf => CpuIo2StandaloneMm.inf} |  23 +-
 UefiCpuPkg/UefiCpuPkg.dsc                                      |   6 +
 7 files changed, 75 insertions(+), 408 deletions(-)

diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
similarity index 94%
copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
copy to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
index b840d3e10cae..fef434795908 100644
--- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
@@ -6,7 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include "CpuIo2Smm.h"
+#include "CpuIo2Common.h"
 
 //
 // Handle for the SMM CPU I/O Protocol
@@ -371,18 +371,13 @@ CpuIoServiceWrite (
 /**
   The module Entry Point SmmCpuIoProtocol driver
 
-  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
-  @param[in] SystemTable  A pointer to the EFI System Table.
-
   @retval EFI_SUCCESS  The entry point is executed successfully.
   @retval Other        Some error occurs when executing this entry point.
 
 **/
 EFI_STATUS
-EFIAPI
-SmmCpuIo2Initialize (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
+CommonCpuIo2Initialize (
+  VOID
   )
 {
   EFI_STATUS  Status;
@@ -390,12 +385,12 @@ SmmCpuIo2Initialize (
   //
   // Copy the SMM CPU I/O Protocol instance into the System Management System Table
   //
-  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
+  CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
 
   //
   // Install the SMM CPU I/O Protocol into the SMM protocol database
   //
-  Status = gSmst->SmmInstallProtocolInterface (
+  Status = gMmst->MmInstallProtocolInterface (
                     &mHandle,
                     &gEfiSmmCpuIo2ProtocolGuid,
                     EFI_NATIVE_INTERFACE,
diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
index b840d3e10cae..062d1a47a09f 100644
--- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
@@ -6,367 +6,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
-#include "CpuIo2Smm.h"
+#include <PiSmm.h>
 
-//
-// Handle for the SMM CPU I/O Protocol
-//
-EFI_HANDLE  mHandle = NULL;
-
-//
-// SMM CPU I/O Protocol instance
-//
-EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {
-  {
-    CpuMemoryServiceRead,
-    CpuMemoryServiceWrite
-  },
-  {
-    CpuIoServiceRead,
-    CpuIoServiceWrite
-  }
-};
-
-//
-// Lookup table for increment values based on transfer widths
-//
-UINT8 mStride[] = {
-  1, // SMM_IO_UINT8
-  2, // SMM_IO_UINT16
-  4, // SMM_IO_UINT32
-  8  // SMM_IO_UINT64
-};
-
-/**
-  Check parameters to a SMM CPU I/O Protocol service request.
-
-  @param[in]  MmioOperation  TRUE for an MMIO operation, FALSE for I/O Port operation.
-  @param[in]  Width          Signifies the width of the I/O operations.
-  @param[in]  Address        The base address of the I/O operations.  The caller is
-                             responsible for aligning the Address if required.
-  @param[in]  Count          The number of I/O operations to perform.
-  @param[in]  Buffer         For read operations, the destination buffer to store
-                             the results.  For write operations, the source buffer
-                             from which to write data.
-
-  @retval EFI_SUCCESS            The data was read from or written to the device.
-  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
-  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
-
-**/
-EFI_STATUS
-CpuIoCheckParameter (
-  IN BOOLEAN           MmioOperation,
-  IN EFI_SMM_IO_WIDTH  Width,
-  IN UINT64            Address,
-  IN UINTN             Count,
-  IN VOID              *Buffer
-  )
-{
-  UINT64  MaxCount;
-  UINT64  Limit;
-
-  //
-  // Check to see if Buffer is NULL
-  //
-  if (Buffer == NULL) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  //
-  // Check to see if Width is in the valid range
-  //
-  if ((UINT32)Width > SMM_IO_UINT64) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  //
-  // Check to see if Width is in the valid range for I/O Port operations
-  //
-  if (!MmioOperation && (Width == SMM_IO_UINT64)) {
-    return EFI_INVALID_PARAMETER;
-  }
-
-  //
-  // Check to see if any address associated with this transfer exceeds the maximum
-  // allowed address.  The maximum address implied by the parameters passed in is
-  // Address + Size * Count.  If the following condition is met, then the transfer
-  // is not supported.
-  //
-  //    Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
-  //
-  // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
-  // can also be the maximum integer value supported by the CPU, this range
-  // check must be adjusted to avoid all overflow conditions.
-  //
-  // The following form of the range check is equivalent but assumes that
-  // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
-  //
-  Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);
-  if (Count == 0) {
-    if (Address > Limit) {
-      return EFI_UNSUPPORTED;
-    }
-  } else {
-    MaxCount = RShiftU64 (Limit, Width);
-    if (MaxCount < (Count - 1)) {
-      return EFI_UNSUPPORTED;
-    }
-    if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
-      return EFI_UNSUPPORTED;
-    }
-  }
-
-  //
-  // Check to see if Address is aligned
-  //
-  if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {
-    return EFI_UNSUPPORTED;
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Reads memory-mapped registers.
-
-  The I/O operations are carried out exactly as requested.  The caller is
-  responsible for any alignment and I/O width issues that the bus, device,
-  platform, or type of I/O might require.
-
-  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
-  @param[in]  Width    Signifies the width of the I/O operations.
-  @param[in]  Address  The base address of the I/O operations.  The caller is
-                       responsible for aligning the Address if required.
-  @param[in]  Count    The number of I/O operations to perform.
-  @param[out] Buffer   For read operations, the destination buffer to store
-                       the results.  For write operations, the source buffer
-                       from which to write data.
-
-  @retval EFI_SUCCESS            The data was read from or written to the device.
-  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
-  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
-  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
-                                 lack of resources
-
-**/
-EFI_STATUS
-EFIAPI
-CpuMemoryServiceRead (
-  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
-  IN  EFI_SMM_IO_WIDTH                Width,
-  IN  UINT64                          Address,
-  IN  UINTN                           Count,
-  OUT VOID                            *Buffer
-  )
-{
-  EFI_STATUS  Status;
-  UINT8       Stride;
-  UINT8       *Uint8Buffer;
-
-  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Select loop based on the width of the transfer
-  //
-  Stride = mStride[Width];
-  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
-    if (Width == SMM_IO_UINT8) {
-      *Uint8Buffer = MmioRead8 ((UINTN)Address);
-    } else if (Width == SMM_IO_UINT16) {
-      *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
-    } else if (Width == SMM_IO_UINT32) {
-      *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
-    } else if (Width == SMM_IO_UINT64) {
-      *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
-    }
-  }
-  return EFI_SUCCESS;
-}
-
-/**
-  Writes memory-mapped registers.
-
-  The I/O operations are carried out exactly as requested.  The caller is
-  responsible for any alignment and I/O width issues that the bus, device,
-  platform, or type of I/O might require.
-
-  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
-  @param[in]  Width    Signifies the width of the I/O operations.
-  @param[in]  Address  The base address of the I/O operations.  The caller is
-                       responsible for aligning the Address if required.
-  @param[in]  Count    The number of I/O operations to perform.
-  @param[in]  Buffer   For read operations, the destination buffer to store
-                       the results.  For write operations, the source buffer
-                       from which to write data.
-
-  @retval EFI_SUCCESS            The data was read from or written to the device.
-  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
-  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
-  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
-                                 lack of resources
-
-**/
-EFI_STATUS
-EFIAPI
-CpuMemoryServiceWrite (
-  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
-  IN EFI_SMM_IO_WIDTH                Width,
-  IN UINT64                          Address,
-  IN UINTN                           Count,
-  IN VOID                            *Buffer
-  )
-{
-  EFI_STATUS  Status;
-  UINT8       Stride;
-  UINT8       *Uint8Buffer;
-
-  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Select loop based on the width of the transfer
-  //
-  Stride = mStride[Width];
-  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
-    if (Width == SMM_IO_UINT8) {
-      MmioWrite8 ((UINTN)Address, *Uint8Buffer);
-    } else if (Width == SMM_IO_UINT16) {
-      MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
-    } else if (Width == SMM_IO_UINT32) {
-      MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
-    } else if (Width == SMM_IO_UINT64) {
-      MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
-    }
-  }
-  return EFI_SUCCESS;
-}
-
-/**
-  Reads I/O registers.
-
-  The I/O operations are carried out exactly as requested.  The caller is
-  responsible for any alignment and I/O width issues that the bus, device,
-  platform, or type of I/O might require.
-
-  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
-  @param[in]  Width    Signifies the width of the I/O operations.
-  @param[in]  Address  The base address of the I/O operations.  The caller is
-                       responsible for aligning the Address if required.
-  @param[in]  Count    The number of I/O operations to perform.
-  @param[out] Buffer   For read operations, the destination buffer to store
-                       the results.  For write operations, the source buffer
-                       from which to write data.
-
-  @retval EFI_SUCCESS            The data was read from or written to the device.
-  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
-  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
-  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
-                                 lack of resources
-
-**/
-EFI_STATUS
-EFIAPI
-CpuIoServiceRead (
-  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
-  IN  EFI_SMM_IO_WIDTH                Width,
-  IN  UINT64                          Address,
-  IN  UINTN                           Count,
-  OUT VOID                            *Buffer
-  )
-{
-  EFI_STATUS  Status;
-  UINT8       Stride;
-  UINT8       *Uint8Buffer;
-
-  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Select loop based on the width of the transfer
-  //
-  Stride = mStride[Width];
-  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
-    if (Width == SMM_IO_UINT8) {
-      *Uint8Buffer = IoRead8 ((UINTN)Address);
-    } else if (Width == SMM_IO_UINT16) {
-      *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);
-    } else if (Width == SMM_IO_UINT32) {
-      *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);
-    }
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Write I/O registers.
-
-  The I/O operations are carried out exactly as requested.  The caller is
-  responsible for any alignment and I/O width issues that the bus, device,
-  platform, or type of I/O might require.
-
-  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
-  @param[in]  Width    Signifies the width of the I/O operations.
-  @param[in]  Address  The base address of the I/O operations.  The caller is
-                       responsible for aligning the Address if required.
-  @param[in]  Count    The number of I/O operations to perform.
-  @param[in]  Buffer   For read operations, the destination buffer to store
-                       the results.  For write operations, the source buffer
-                       from which to write data.
-
-  @retval EFI_SUCCESS            The data was read from or written to the device.
-  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
-  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
-  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
-                                 lack of resources
-
-**/
-EFI_STATUS
-EFIAPI
-CpuIoServiceWrite (
-  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
-  IN EFI_SMM_IO_WIDTH                Width,
-  IN UINT64                          Address,
-  IN UINTN                           Count,
-  IN VOID                            *Buffer
-  )
-{
-  EFI_STATUS  Status;
-  UINT8       Stride;
-  UINT8       *Uint8Buffer;
-
-  //
-  // Make sure the parameters are valid
-  //
-  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Select loop based on the width of the transfer
-  //
-  Stride = mStride[Width];
-  for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
-    if (Width == SMM_IO_UINT8) {
-      IoWrite8 ((UINTN)Address, *Uint8Buffer);
-    } else if (Width == SMM_IO_UINT16) {
-      IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
-    } else if (Width == SMM_IO_UINT32) {
-      IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
-    }
-  }
-
-  return EFI_SUCCESS;
-}
+#include "CpuIo2Common.h"
 
 /**
   The module Entry Point SmmCpuIoProtocol driver
@@ -380,28 +22,10 @@ CpuIoServiceWrite (
 **/
 EFI_STATUS
 EFIAPI
-SmmCpuIo2Initialize (
+TraditionalMmCpuIo2Initialize (
   IN EFI_HANDLE        ImageHandle,
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_STATUS  Status;
-
-  //
-  // Copy the SMM CPU I/O Protocol instance into the System Management System Table
-  //
-  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
-
-  //
-  // Install the SMM CPU I/O Protocol into the SMM protocol database
-  //
-  Status = gSmst->SmmInstallProtocolInterface (
-                    &mHandle,
-                    &gEfiSmmCpuIo2ProtocolGuid,
-                    EFI_NATIVE_INTERFACE,
-                    &mSmmCpuIo2
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  return Status;
+  return CommonCpuIo2Initialize ();
 }
diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
new file mode 100644
index 000000000000..2978f82eb815
--- /dev/null
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
@@ -0,0 +1,31 @@
+/** @file
+  Produces the SMM CPU I/O Protocol.
+
+Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include "CpuIo2Common.h"
+
+/**
+  The module Entry Point SmmCpuIoProtocol driver
+
+  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
+  @param[in] SystemTable  A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS  The entry point is executed successfully.
+  @retval Other        Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+EFIAPI
+StandaloneMmCpuIo2Initialize (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return CommonCpuIo2Initialize ();
+}
diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
similarity index 92%
rename from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
rename to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
index 4c133b58c9f4..6759cc1c8777 100644
--- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
@@ -9,14 +9,14 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #ifndef _CPU_IO2_SMM_H_
 #define _CPU_IO2_SMM_H_
 
-#include <PiSmm.h>
+#include <PiMm.h>
 
 #include <Protocol/SmmCpuIo2.h>
 
 #include <Library/BaseLib.h>
 #include <Library/DebugLib.h>
 #include <Library/IoLib.h>
-#include <Library/SmmServicesTableLib.h>
+#include <Library/MmServicesTableLib.h>
 #include <Library/BaseMemoryLib.h>
 
 #define MAX_IO_PORT_ADDRESS   0xFFFF
@@ -153,4 +153,17 @@ CpuIoServiceWrite (
   IN VOID                            *Buffer
   );
 
+
+/**
+  The module Entry Point SmmCpuIoProtocol driver
+
+  @retval EFI_SUCCESS  The entry point is executed successfully.
+  @retval Other        Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+CommonCpuIo2Initialize (
+  VOID
+  );
+
 #endif
diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
index bc78fa4e42d2..031db1d100b9 100644
--- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
@@ -14,7 +14,7 @@ [Defines]
   MODULE_TYPE                    = DXE_SMM_DRIVER
   VERSION_STRING                 = 1.0
   PI_SPECIFICATION_VERSION       = 0x0001000A
-  ENTRY_POINT                    = SmmCpuIo2Initialize
+  ENTRY_POINT                    = TraditionalMmCpuIo2Initialize
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -24,7 +24,8 @@ [Defines]
 
 [Sources]
   CpuIo2Smm.c
-  CpuIo2Smm.h
+  CpuIo2Common.c
+  CpuIo2Common.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -34,7 +35,7 @@ [LibraryClasses]
   BaseLib
   DebugLib
   IoLib
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseMemoryLib
 
 [Protocols]
diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
similarity index 56%
copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
copy to UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
index bc78fa4e42d2..cce7742bee7a 100644
--- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
+++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
@@ -8,13 +8,12 @@
 
 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = CpuIo2Smm
-  MODULE_UNI_FILE                = CpuIo2Smm.uni
-  FILE_GUID                      = A47EE2D8-F60E-42fd-8E58-7BD65EE4C29B
-  MODULE_TYPE                    = DXE_SMM_DRIVER
+  BASE_NAME                      = CpuIo2StandaloneMm
+  FILE_GUID                      = E3121A26-BB1C-4A18-8E23-2EA3F0412248
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  PI_SPECIFICATION_VERSION       = 0x0001000A
-  ENTRY_POINT                    = SmmCpuIo2Initialize
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = StandaloneMmCpuIo2Initialize
 
 #
 # The following information is for reference only and not required by the build tools.
@@ -23,18 +22,19 @@ [Defines]
 #
 
 [Sources]
-  CpuIo2Smm.c
-  CpuIo2Smm.h
+  CpuIo2StandaloneMm.c
+  CpuIo2Common.c
+  CpuIo2Common.h
 
 [Packages]
   MdePkg/MdePkg.dec
 
 [LibraryClasses]
-  UefiDriverEntryPoint
+  StandaloneMmDriverEntryPoint
   BaseLib
   DebugLib
   IoLib
-  SmmServicesTableLib
+  MmServicesTableLib
   BaseMemoryLib
 
 [Protocols]
@@ -42,6 +42,3 @@ [Protocols]
 
 [Depex]
   TRUE
-
-[UserExtensions.TianoCore."ExtraFiles"]
-  CpuIo2SmmExtra.uni
diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
index b2b6d78a71b0..4e46c600788f 100644
--- a/UefiCpuPkg/UefiCpuPkg.dsc
+++ b/UefiCpuPkg/UefiCpuPkg.dsc
@@ -38,6 +38,7 @@ [LibraryClasses]
   UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
   UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
   UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
+  StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
   DxeServicesTableLib|MdePkg/Library/DxeServicesTableLib/DxeServicesTableLib.inf
   PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
   PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
@@ -89,10 +90,14 @@ [LibraryClasses.common.DXE_DRIVER]
 
 [LibraryClasses.common.DXE_SMM_DRIVER]
   SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
+  MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
   MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
 
+[LibraryClasses.common.MM_STANDALONE]
+  MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
+
 [LibraryClasses.common.UEFI_APPLICATION]
   UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
@@ -121,6 +126,7 @@ [Components.IA32, Components.X64]
       NULL|UefiCpuPkg/Library/CpuCommonFeaturesLib/CpuCommonFeaturesLib.inf
   }
   UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
+  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
   UefiCpuPkg/CpuMpPei/CpuMpPei.inf
   UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
   UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* [PATCH v2 16/16] UefiCpuPkg: SmmCpuExceptionHandlerLib: Added StandaloneMm module support
       [not found] <20210105185935.3769-1-kun.q@outlook.com>
                   ` (14 preceding siblings ...)
  2021-01-05 18:59 ` [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm Kun Qin
@ 2021-01-05 18:59 ` Kun Qin
  2021-01-06 15:48   ` Laszlo Ersek
       [not found] ` <16576B257D31F1E6.24224@groups.io>
       [not found] ` <16576B256B69BE5C.24224@groups.io>
  17 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-05 18:59 UTC (permalink / raw)
  To: devel; +Cc: Eric Dong, Ray Ni, Laszlo Ersek, Rahul Kumar

This change of SmmCpuExceptionHandlerLib adds support for StandaloneMm
components to allow x64 StandaloneMm environment setting up exception
handlers.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - No review, no change.

 UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
index 4cdb11c04ea0..ea5b10b5c8e4 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
@@ -13,7 +13,7 @@ [Defines]
   FILE_GUID                      = 8D2C439B-3981-42ff-9CE5-1B50ECA502D6
   MODULE_TYPE                    = DXE_SMM_DRIVER
   VERSION_STRING                 = 1.1
-  LIBRARY_CLASS                  = CpuExceptionHandlerLib|DXE_SMM_DRIVER
+  LIBRARY_CLASS                  = CpuExceptionHandlerLib|DXE_SMM_DRIVER MM_STANDALONE MM_CORE_STANDALONE
 
 #
 # The following information is for reference only and not required by the build tools.
-- 
2.30.0.windows.1


^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm
  2021-01-05 18:59 ` [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm Kun Qin
@ 2021-01-06  3:24   ` Wu, Hao A
  0 siblings, 0 replies; 34+ messages in thread
From: Wu, Hao A @ 2021-01-06  3:24 UTC (permalink / raw)
  To: devel@edk2.groups.io, kun.q@outlook.com
  Cc: Wang, Jian J, Bi, Dandan, Liming Gao, Yao, Jiewen

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Kun Qin
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>;
> Bi, Dandan <dandan.bi@intel.com>; Liming Gao
> <gaoliming@byosoft.com.cn>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: [edk2-devel] [PATCH v2 06/16] MdeModulePkg:
> SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm
> 
> This change added support of StandaloneMm for ReportStatusCodeLib. It
> adds a new instance of ReportStatusCodeLib for MM_STANDALONE type,
> and abstracts the references of gMmst and gSmst functionalities into
> separate files in order to link in proper Service Table for SMM core/drivers.


Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Removed "EFIAPI" for internal functions.
>     - Minor new file description update.
> 
>  MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
> | 16 +++++----
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibSta
> ndaloneMm.c                                   | 38 ++++++++++++++++++++
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibTra
> ditional.c                                    | 38 ++++++++++++++++++++
>  MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.h
> | 36 +++++++++++++++++++
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLi
> b.inf                                          |  4 ++-
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/{SmmReportStatusCode
> Lib.inf => StandaloneMmReportStatusCodeLib.inf} | 22 ++++++------
>  MdeModulePkg/MdeModulePkg.dsc
> |  1 +
>  7 files changed, 137 insertions(+), 18 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.
> c
> index 3a1772538cdf..fb1769db9223 100644
> ---
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.c
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.
> c
> @@ -1,5 +1,5 @@
>  /** @file
> -  Report Status Code Library for SMM Phase.
> +  Report Status Code Library for MM Phase.
> 
>    Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent @@ -8,7 +8,7 @@
> 
>  #include <Library/ReportStatusCodeLib.h>  #include <Library/DebugLib.h> -
> #include <Library/SmmServicesTableLib.h>
> +#include <Library/MmServicesTableLib.h>
>  #include <Library/BaseLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/PcdLib.h>
> @@ -16,10 +16,12 @@
> 
>  #include <Guid/StatusCodeDataTypeId.h>
>  #include <Guid/StatusCodeDataTypeDebug.h> -#include
> <Protocol/SmmStatusCode.h>
> +#include <Protocol/MmStatusCode.h>
> 
> -EFI_SMM_REPORT_STATUS_CODE     mReportStatusCode = NULL;
> -EFI_SMM_STATUS_CODE_PROTOCOL   *mStatusCodeProtocol = NULL;
> +#include "ReportStatusCodeLib.h"
> +
> +EFI_MM_REPORT_STATUS_CODE     mReportStatusCode = NULL;
> +EFI_MM_STATUS_CODE_PROTOCOL   *mStatusCodeProtocol = NULL;
> 
> 
>  /**
> @@ -29,14 +31,14 @@ EFI_SMM_STATUS_CODE_PROTOCOL
> *mStatusCodeProtocol = NULL;
>              NULL is returned if no status code service is available.
> 
>  **/
> -EFI_SMM_REPORT_STATUS_CODE
> +EFI_MM_REPORT_STATUS_CODE
>  InternalGetReportStatusCode (
>    VOID
>    )
>  {
>    EFI_STATUS                    Status;
> 
> -  Status = gSmst->SmmLocateProtocol (&gEfiSmmStatusCodeProtocolGuid,
> NULL, (VOID**)&mStatusCodeProtocol);
> +  Status = InternalLocateProtocol (&gEfiMmStatusCodeProtocolGuid, NULL,
> + (VOID**)&mStatusCodeProtocol);
>    if (!EFI_ERROR (Status) && mStatusCodeProtocol != NULL) {
>      return mStatusCodeProtocol->ReportStatusCode;
>    }
> diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibS
> tandaloneMm.c
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibS
> tandaloneMm.c
> new file mode 100644
> index 000000000000..a4c428dc88a9
> --- /dev/null
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibS
> ta
> +++ ndaloneMm.c
> @@ -0,0 +1,38 @@
> +/** @file
> +  Abstraction layer for MM service table used by MM ReportStatusCodeLib.
> +
> +  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Library/MmServicesTableLib.h>
> +
> +/**
> +  Returns the first protocol instance that matches the given protocol.
> +
> +  @param[in]  Protocol          Provides the protocol to search for.
> +  @param[in]  Registration      Optional registration key returned from
> +                                RegisterProtocolNotify().
> +  @param[out]  Interface        On return, a pointer to the first interface that
> matches Protocol and
> +                                Registration.
> +
> +  @retval EFI_SUCCESS           A protocol instance matching Protocol was
> found and returned in
> +                                Interface.
> +  @retval EFI_NOT_FOUND         No protocol instances were found that
> match Protocol and
> +                                Registration.
> +  @retval EFI_INVALID_PARAMETER Interface is NULL.
> +                                Protocol is NULL.
> +
> +**/
> +EFI_STATUS
> +InternalLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration, OPTIONAL
> +  OUT VOID      **Interface
> +  )
> +{
> +  return gMmst->MmLocateProtocol (Protocol, Registration, Interface); }
> diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibT
> raditional.c
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibT
> raditional.c
> new file mode 100644
> index 000000000000..603e222f5508
> --- /dev/null
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLibT
> ra
> +++ ditional.c
> @@ -0,0 +1,38 @@
> +/** @file
> +  Abstraction layer for SMM service table used by SMM
> ReportStatusCodeLib.
> +
> +  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Library/SmmServicesTableLib.h>
> +
> +/**
> +  Returns the first protocol instance that matches the given protocol.
> +
> +  @param[in]  Protocol          Provides the protocol to search for.
> +  @param[in]  Registration      Optional registration key returned from
> +                                RegisterProtocolNotify().
> +  @param[out]  Interface        On return, a pointer to the first interface that
> matches Protocol and
> +                                Registration.
> +
> +  @retval EFI_SUCCESS           A protocol instance matching Protocol was
> found and returned in
> +                                Interface.
> +  @retval EFI_NOT_FOUND         No protocol instances were found that
> match Protocol and
> +                                Registration.
> +  @retval EFI_INVALID_PARAMETER Interface is NULL.
> +                                Protocol is NULL.
> +
> +**/
> +EFI_STATUS
> +InternalLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration, OPTIONAL
> +  OUT VOID      **Interface
> +  )
> +{
> +  return gSmst->SmmLocateProtocol (Protocol, Registration, Interface);
> +}
> diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.
> h
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.
> h
> new file mode 100644
> index 000000000000..2820e40dde19
> --- /dev/null
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/ReportStatusCodeLib.
> h
> @@ -0,0 +1,36 @@
> +/** @file
> +  Report Status Code Library for MM Phase.
> +
> +  Copyright (c) 2009 - 2018, Intel Corporation. All rights
> + reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _MM_RSC_LIB_H_
> +#define _MM_RSC_LIB_H_
> +
> +/**
> +  Returns the first protocol instance that matches the given protocol.
> +
> +  @param[in]  Protocol          Provides the protocol to search for.
> +  @param[in]  Registration      Optional registration key returned from
> +                                RegisterProtocolNotify().
> +  @param[out]  Interface        On return, a pointer to the first interface that
> matches Protocol and
> +                                Registration.
> +
> +  @retval EFI_SUCCESS           A protocol instance matching Protocol was
> found and returned in
> +                                Interface.
> +  @retval EFI_NOT_FOUND         No protocol instances were found that
> match Protocol and
> +                                Registration.
> +  @retval EFI_INVALID_PARAMETER Interface is NULL.
> +                                Protocol is NULL.
> +
> +**/
> +EFI_STATUS
> +InternalLocateProtocol (
> +  IN  EFI_GUID  *Protocol,
> +  IN  VOID      *Registration, OPTIONAL
> +  OUT VOID      **Interface
> +  );
> +
> +#endif // _MM_RSC_LIB_H_
> diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib.inf
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib.inf
> index 72496bfababd..02dce09a199d 100644
> ---
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib.inf
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib
> +++ .inf
> @@ -28,6 +28,8 @@ [Defines]
> 
>  [Sources]
>    ReportStatusCodeLib.c
> +  ReportStatusCodeLib.h
> +  ReportStatusCodeLibTraditional.c
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> @@ -45,7 +47,7 @@ [Guids]
>    gEfiStatusCodeDataTypeDebugGuid               ## SOMETIMES_CONSUMES
> ## UNDEFINED
> 
>  [Protocols]
> -  gEfiSmmStatusCodeProtocolGuid                 ## CONSUMES
> +  gEfiMmStatusCodeProtocolGuid                  ## CONSUMES
> 
>  [Pcd]
>    gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask  ##
> CONSUMES diff --git
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib.inf
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReport
> StatusCodeLib.inf
> similarity index 56%
> copy from
> MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLi
> b.inf
> copy to
> MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportSt
> atusCodeLib.inf
> index 72496bfababd..866e09249a6a 100644
> ---
> a/MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCod
> eLib.inf
> +++
> b/MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReport
> Stat
> +++ usCodeLib.inf
> @@ -1,9 +1,10 @@
>  ## @file
> -#  SMM report status code library.
> +#  Standalone MM report status code library.
>  #
> -#  Retrieve status code and report status code in SMM phase.
> +#  Retrieve status code and report status code in MM phase.
>  #
>  #  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent  # @@ -12,13 +13,12 @@
> 
>  [Defines]
>    INF_VERSION                    = 0x00010005
> -  BASE_NAME                      = SmmReportStatusCodeLib
> -  MODULE_UNI_FILE                = SmmReportStatusCodeLib.uni
> -  FILE_GUID                      = 67089D19-B3D6-4d9e-A0EB-FEDC1F83A1EE
> -  MODULE_TYPE                    = DXE_SMM_DRIVER
> +  BASE_NAME                      = StandaloneMmReportStatusCodeLib
> +  FILE_GUID                      = 17C7FC8C-8C5D-497E-9C0E-C21255B30E04
> +  MODULE_TYPE                    = MM_STANDALONE
>    VERSION_STRING                 = 1.0
> -  PI_SPECIFICATION_VERSION       = 0x0001000A
> -  LIBRARY_CLASS                  = ReportStatusCodeLib|DXE_SMM_DRIVER
> SMM_CORE
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = ReportStatusCodeLib|MM_STANDALONE
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
> @@ -28,6 +28,8 @@ [Defines]
> 
>  [Sources]
>    ReportStatusCodeLib.c
> +  ReportStatusCodeLib.h
> +  ReportStatusCodeLibStandaloneMm.c
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> @@ -36,7 +38,7 @@ [Packages]
>  [LibraryClasses]
>    PcdLib
>    BaseMemoryLib
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    DebugLib
>    MemoryAllocationLib
> 
> @@ -45,7 +47,7 @@ [Guids]
>    gEfiStatusCodeDataTypeDebugGuid               ## SOMETIMES_CONSUMES
> ## UNDEFINED
> 
>  [Protocols]
> -  gEfiSmmStatusCodeProtocolGuid                 ## CONSUMES
> +  gEfiMmStatusCodeProtocolGuid                  ## CONSUMES
> 
>  [Pcd]
>    gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask  ##
> CONSUMES diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc index 9afd40eeed46..200fbcc18a18
> 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -474,6 +474,7 @@ [Components.IA32, Components.X64]
>    }
> 
> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i
> nf
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLi
> b.inf
> +
> +
> MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportSt
> atusCo
> + deLib.inf
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.inf
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.inf
>    MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> --
> 2.30.0.windows.1
> 
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver in StandaloneMm
  2021-01-05 18:59 ` [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver " Kun Qin
@ 2021-01-06  3:24   ` Wu, Hao A
  0 siblings, 0 replies; 34+ messages in thread
From: Wu, Hao A @ 2021-01-06  3:24 UTC (permalink / raw)
  To: Kun Qin, devel@edk2.groups.io
  Cc: Wang, Jian J, Bi, Dandan, Liming Gao, Yao, Jiewen

> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>;
> Bi, Dandan <dandan.bi@intel.com>; Liming Gao
> <gaoliming@byosoft.com.cn>; Yao, Jiewen <jiewen.yao@intel.com>
> Subject: [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler:
> StatusCodeHandler driver in StandaloneMm
> 
> This change added support of StandaloneMm for StatusCodeHandler. It adds
> a new instance of StatusCodeHandler of MM_STANDALONE type, and
> abstracts the driver entrypoint into separate files, replaced gSmst with
> gMmst, and switched to MM version of RscHandlerProtocol.


Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - New patch to support StatusCodeHandler in standalone mm [Liming]
> 
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCodeW
> orker.c                                        | 36 ++++++++++----------
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWork
> er.c                                        |  2 +-
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerS
> mm.c => StatusCodeHandlerMm.c}               | 23 +++++--------
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSta
> ndalone.c                                   | 31 +++++++++++++++++
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerTra
> ditional.c                                  | 31 +++++++++++++++++
>  MdeModulePkg/MdeModulePkg.dsc
> |  1 +
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerS
> mm.h => StatusCodeHandlerMm.h}               | 23 ++++++++++---
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.inf                                        | 15 ++++----
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/{StatusCodeHandlerS
> mm.inf => StatusCodeHandlerStandaloneMm.inf} | 32 ++++++++---------
>  9 files changed, 132 insertions(+), 62 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCode
> Worker.c
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCode
> Worker.c
> index c9b43fd2468f..14bac8ec3c18 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCode
> Worker.c
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/MemoryStatusCode
> Worke
> +++ r.c
> @@ -7,15 +7,15 @@
> 
>  **/
> 
> -#include "StatusCodeHandlerSmm.h"
> +#include "StatusCodeHandlerMm.h"
> 
> -RUNTIME_MEMORY_STATUSCODE_HEADER
> *mSmmMemoryStatusCodeTable;
> +RUNTIME_MEMORY_STATUSCODE_HEADER
> *mMmMemoryStatusCodeTable;
> 
>  /**
> -  Initialize SMM memory status code table as initialization for memory status
> code worker
> +  Initialize MM memory status code table as initialization for memory
> + status code worker
> 
> -  @retval EFI_SUCCESS  SMM memory status code table successfully
> initialized.
> -  @retval others       Errors from gSmst->SmmInstallConfigurationTable().
> +  @retval EFI_SUCCESS  MM memory status code table successfully
> initialized.
> +  @retval others       Errors from gMmst->MmInstallConfigurationTable().
>  **/
>  EFI_STATUS
>  MemoryStatusCodeInitializeWorker (
> @@ -25,17 +25,17 @@ MemoryStatusCodeInitializeWorker (
>    EFI_STATUS                        Status;
> 
>    //
> -  // Allocate SMM memory status code pool.
> +  // Allocate MM memory status code pool.
>    //
> -  mSmmMemoryStatusCodeTable =
> (RUNTIME_MEMORY_STATUSCODE_HEADER *)AllocateZeroPool (sizeof
> (RUNTIME_MEMORY_STATUSCODE_HEADER) + PcdGet16
> (PcdStatusCodeMemorySize) * 1024);
> -  ASSERT (mSmmMemoryStatusCodeTable != NULL);
> +  mMmMemoryStatusCodeTable =
> (RUNTIME_MEMORY_STATUSCODE_HEADER
> + *)AllocateZeroPool (sizeof (RUNTIME_MEMORY_STATUSCODE_HEADER) +
> + PcdGet16 (PcdStatusCodeMemorySize) * 1024);  ASSERT
> + (mMmMemoryStatusCodeTable != NULL);
> 
> -  mSmmMemoryStatusCodeTable->MaxRecordsNumber = (PcdGet16
> (PcdStatusCodeMemorySize) * 1024) / sizeof
> (MEMORY_STATUSCODE_RECORD);
> -  Status = gSmst->SmmInstallConfigurationTable (
> -                    gSmst,
> +  mMmMemoryStatusCodeTable->MaxRecordsNumber = (PcdGet16
> + (PcdStatusCodeMemorySize) * 1024) / sizeof
> (MEMORY_STATUSCODE_RECORD);  Status = gMmst-
> >MmInstallConfigurationTable (
> +                    gMmst,
>                      &gMemoryStatusCodeRecordGuid,
> -                    &mSmmMemoryStatusCodeTable,
> -                    sizeof (mSmmMemoryStatusCodeTable)
> +                    &mMmMemoryStatusCodeTable,
> +                    sizeof (mMmMemoryStatusCodeTable)
>                      );
>    return Status;
>  }
> @@ -74,8 +74,8 @@ MemoryStatusCodeReportWorker (
>    //
>    // Locate current record buffer.
>    //
> -  Record = (MEMORY_STATUSCODE_RECORD *)
> (mSmmMemoryStatusCodeTable + 1);
> -  Record = &Record[mSmmMemoryStatusCodeTable->RecordIndex++];
> +  Record = (MEMORY_STATUSCODE_RECORD *)
> (mMmMemoryStatusCodeTable + 1);
> + Record = &Record[mMmMemoryStatusCodeTable->RecordIndex++];
> 
>    //
>    // Save status code.
> @@ -92,12 +92,12 @@ MemoryStatusCodeReportWorker (
>    // so the first record is pointed by record index.
>    // If it is less then max number, index of the first record is zero.
>    //
> -  mSmmMemoryStatusCodeTable->NumberOfRecords++;
> -  if (mSmmMemoryStatusCodeTable->RecordIndex ==
> mSmmMemoryStatusCodeTable->MaxRecordsNumber) {
> +  mMmMemoryStatusCodeTable->NumberOfRecords++;
> +  if (mMmMemoryStatusCodeTable->RecordIndex ==
> + mMmMemoryStatusCodeTable->MaxRecordsNumber) {
>      //
>      // Wrap around record index.
>      //
> -    mSmmMemoryStatusCodeTable->RecordIndex = 0;
> +    mMmMemoryStatusCodeTable->RecordIndex = 0;
>    }
> 
>    return EFI_SUCCESS;
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWo
> rker.c
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWo
> rker.c
> index 3df0a6712611..bcb75bc7b170 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWo
> rker.c
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/SerialStatusCodeWo
> rke
> +++ r.c
> @@ -6,7 +6,7 @@
> 
>  **/
> 
> -#include "StatusCodeHandlerSmm.h"
> +#include "StatusCodeHandlerMm.h"
> 
>  /**
>    Convert status code value and extended data to readable ASCII string, send
> string to serial I/O device.
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.c
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandler
> Mm.c
> similarity index 69%
> rename from
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.c
> rename to
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerM
> m.c
> index 20271571ded4..4948d3d99ad6 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.c
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandler
> Mm.c
> @@ -1,15 +1,15 @@
>  /** @file
>    Status Code Handler Driver which produces general handlers and hook
> them
> -  onto the SMM status code router.
> +  onto the MM status code router.
> 
>    Copyright (c) 2009 - 2020, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> -#include "StatusCodeHandlerSmm.h"
> +#include "StatusCodeHandlerMm.h"
> 
> -EFI_SMM_RSC_HANDLER_PROTOCOL  *mRscHandlerProtocol       = NULL;
> +EFI_MM_RSC_HANDLER_PROTOCOL   *mRscHandlerProtocol       = NULL;
> 
> 
>  /**
> @@ -42,27 +42,22 @@ InitializationDispatcherWorker (  }
> 
>  /**
> -  Entry point of SMM Status Code Driver.
> +  Entry point of Common MM Status Code Driver.
> 
> -  This function is the entry point of SMM Status Code Driver.
> -
> -  @param  ImageHandle       The firmware allocated handle for the EFI image.
> -  @param  SystemTable       A pointer to the EFI System Table.
> +  This function is the entry point of MM Status Code Driver.
> 
>    @retval EFI_SUCCESS       The entry point is executed successfully.
> 
>  **/
>  EFI_STATUS
> -EFIAPI
> -StatusCodeHandlerSmmEntry (
> -  IN EFI_HANDLE         ImageHandle,
> -  IN EFI_SYSTEM_TABLE   *SystemTable
> +StatusCodeHandlerCommonEntry (
> +  VOID
>    )
>  {
>    EFI_STATUS                Status;
> 
> -  Status = gSmst->SmmLocateProtocol (
> -                    &gEfiSmmRscHandlerProtocolGuid,
> +  Status = gMmst->MmLocateProtocol (
> +                    &gEfiMmRscHandlerProtocolGuid,
>                      NULL,
>                      (VOID **) &mRscHandlerProtocol
>                      );
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> tandalone.c
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> tandalone.c
> new file mode 100644
> index 000000000000..11a61705d612
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> tan
> +++ dalone.c
> @@ -0,0 +1,31 @@
> +/** @file
> +  Abstraction layer that contains Standalone MM specific implementation
> +for
> +  Status Code Handler Driver.
> +
> +  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "StatusCodeHandlerMm.h"
> +
> +/**
> +  Entry point of Standalone MM Status Code Driver.
> +
> +  This function is the entry point of Standalone MM Status Code Driver.
> +
> +  @param  ImageHandle       The firmware allocated handle for the EFI image.
> +  @param  SystemTable       A pointer to the EFI MM System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StatusCodeHandlerStandaloneMmEntry (
> +  IN EFI_HANDLE           ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  return StatusCodeHandlerCommonEntry (); }
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerT
> raditional.c
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerT
> raditional.c
> new file mode 100644
> index 000000000000..1105f184b08e
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerT
> rad
> +++ itional.c
> @@ -0,0 +1,31 @@
> +/** @file
> +  Abstraction layer that contains Standalone MM specific implementation
> +for
> +  Status Code Handler Driver.
> +
> +  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "StatusCodeHandlerMm.h"
> +
> +/**
> +  Entry point of Traditional MM Status Code Driver.
> +
> +  This function is the entry point of Traditional MM Status Code Driver.
> +
> +  @param  ImageHandle       The firmware allocated handle for the EFI image.
> +  @param  SystemTable       A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StatusCodeHandlerTraditionalMmEntry (
> +  IN EFI_HANDLE         ImageHandle,
> +  IN EFI_SYSTEM_TABLE   *SystemTable
> +  )
> +{
> +  return StatusCodeHandlerCommonEntry (); }
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc index 200fbcc18a18..098909490095
> 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -476,6 +476,7 @@ [Components.IA32, Components.X64]
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLi
> b.inf
> 
> MdeModulePkg/Library/SmmReportStatusCodeLib/StandaloneMmReportSt
> atusCodeLib.inf
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.inf
> +
> +
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSta
> ndalo
> + neMm.inf
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.inf
>    MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> 
> MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllo
> cationProfileLib.inf
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.h
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandler
> Mm.h
> similarity index 87%
> rename from
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.h
> rename to
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerM
> m.h
> index 6b5d53a4fee3..7871ee404046 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.h
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandler
> Mm.h
> @@ -7,10 +7,10 @@
> 
>  **/
> 
> -#ifndef __STATUS_CODE_HANDLER_SMM_H__
> -#define __STATUS_CODE_HANDLER_SMM_H__
> +#ifndef __STATUS_CODE_HANDLER_MM_H__
> +#define __STATUS_CODE_HANDLER_MM_H__
> 
> -#include <Protocol/SmmReportStatusCodeHandler.h>
> +#include <Protocol/MmReportStatusCodeHandler.h>
> 
>  #include <Guid/MemoryStatusCodeRecord.h>  #include
> <Guid/StatusCodeDataTypeId.h> @@ -22,7 +22,7 @@  #include
> <Library/PrintLib.h>  #include <Library/PcdLib.h>  #include
> <Library/UefiDriverEntryPoint.h> -#include <Library/SmmServicesTableLib.h>
> +#include <Library/MmServicesTableLib.h>
>  #include <Library/SerialPortLib.h>
>  #include <Library/MemoryAllocationLib.h>  #include
> <Library/BaseMemoryLib.h> @@ -32,7 +32,7 @@  //  #define
> MAX_DEBUG_MESSAGE_LENGTH 0x100
> 
> -extern RUNTIME_MEMORY_STATUSCODE_HEADER
> *mSmmMemoryStatusCodeTable;
> +extern RUNTIME_MEMORY_STATUSCODE_HEADER
> *mMmMemoryStatusCodeTable;
> 
>  /**
>    Locates Serial I/O Protocol as initialization for serial status code worker.
> @@ -114,4 +114,17 @@ MemoryStatusCodeReportWorker (
>    IN EFI_STATUS_CODE_DATA               *Data OPTIONAL
>    );
> 
> +/**
> +  Entry point of Common MM Status Code Driver.
> +
> +  This function is the entry point of MM Status Code Driver.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +StatusCodeHandlerCommonEntry (
> +  VOID
> +  );
> +
>  #endif
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.inf
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.inf
> index 4e24d87e55d1..90abe662d291 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.inf
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.
> +++ inf
> @@ -1,5 +1,5 @@
>  ## @file
> -#  Status Code Handler Driver which produces general handlers and hook
> them onto the SMM status code router.
> +#  Status Code Handler Driver which produces general handlers and hook
> them onto the MM status code router.
>  #
>  #  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>  #  (C)
> Copyright 2016 Hewlett Packard Enterprise Development LP<BR> @@ -17,7
> +17,7 @@ [Defines]
>    MODULE_TYPE                    = DXE_SMM_DRIVER
>    PI_SPECIFICATION_VERSION       = 0x0001000A
>    VERSION_STRING                 = 1.0
> -  ENTRY_POINT                    = StatusCodeHandlerSmmEntry
> +  ENTRY_POINT                    = StatusCodeHandlerTraditionalMmEntry
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
> @@ -26,8 +26,9 @@ [Defines]
>  #
> 
>  [Sources]
> -  StatusCodeHandlerSmm.c
> -  StatusCodeHandlerSmm.h
> +  StatusCodeHandlerMm.c
> +  StatusCodeHandlerMm.h
> +  StatusCodeHandlerTraditional.c
>    SerialStatusCodeWorker.c
>    MemoryStatusCodeWorker.c
> 
> @@ -37,7 +38,7 @@ [Packages]
> 
>  [LibraryClasses]
>    SerialPortLib
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    UefiDriverEntryPoint
>    PcdLib
>    PrintLib
> @@ -51,7 +52,7 @@ [Guids]
>    gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ##
> UNDEFINED # SmmSystemTable
> 
>  [Protocols]
> -  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
> +  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
> 
>  [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial ##
> CONSUMES @@ -59,7 +60,7 @@ [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeMemorySize |128|
> gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory   ##
> SOMETIMES_CONSUMES
> 
>  [Depex]
> -  gEfiSmmRscHandlerProtocolGuid
> +  gEfiMmRscHandlerProtocolGuid
> 
>  [UserExtensions.TianoCore."ExtraFiles"]
>    StatusCodeHandlerSmmExtra.uni
> diff --git
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.inf
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> tandaloneMm.inf
> similarity index 62%
> copy from
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.inf
> copy to
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSta
> ndaloneMm.inf
> index 4e24d87e55d1..d7c863bf064c 100644
> ---
> a/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> mm.inf
> +++
> b/MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerS
> tan
> +++ daloneMm.inf
> @@ -1,8 +1,9 @@
>  ## @file
> -#  Status Code Handler Driver which produces general handlers and hook
> them onto the SMM status code router.
> +#  Status Code Handler Driver which produces general handlers and hook
> them onto the MM status code router.
>  #
>  #  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>  #  (C)
> Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent  # @@ -11,13 +12,12 @@
> 
>  [Defines]
>    INF_VERSION                    = 0x00010005
> -  BASE_NAME                      = StatusCodeHandlerSmm
> -  MODULE_UNI_FILE                = StatusCodeHandlerSmm.uni
> -  FILE_GUID                      = 79CD78D8-6EDC-4978-BD02-3299C387AB17
> -  MODULE_TYPE                    = DXE_SMM_DRIVER
> -  PI_SPECIFICATION_VERSION       = 0x0001000A
> +  BASE_NAME                      = StatusCodeHandlerStandaloneMm
> +  FILE_GUID                      = EBE7802F-5E11-4D4E-B463-22D2425D156B
> +  MODULE_TYPE                    = MM_STANDALONE
> +  PI_SPECIFICATION_VERSION       = 0x00010032
>    VERSION_STRING                 = 1.0
> -  ENTRY_POINT                    = StatusCodeHandlerSmmEntry
> +  ENTRY_POINT                    = StatusCodeHandlerStandaloneMmEntry
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
> @@ -26,8 +26,9 @@ [Defines]
>  #
> 
>  [Sources]
> -  StatusCodeHandlerSmm.c
> -  StatusCodeHandlerSmm.h
> +  StatusCodeHandlerMm.c
> +  StatusCodeHandlerMm.h
> +  StatusCodeHandlerStandalone.c
>    SerialStatusCodeWorker.c
>    MemoryStatusCodeWorker.c
> 
> @@ -37,8 +38,8 @@ [Packages]
> 
>  [LibraryClasses]
>    SerialPortLib
> -  SmmServicesTableLib
> -  UefiDriverEntryPoint
> +  MmServicesTableLib
> +  StandaloneMmDriverEntryPoint
>    PcdLib
>    PrintLib
>    ReportStatusCodeLib
> @@ -48,10 +49,10 @@ [LibraryClasses]
> 
>  [Guids]
>    gEfiStatusCodeDataTypeStringGuid              ## SOMETIMES_CONSUMES   ##
> UNDEFINED
> -  gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ##
> UNDEFINED # SmmSystemTable
> +  gMemoryStatusCodeRecordGuid                   ## SOMETIMES_PRODUCES   ##
> UNDEFINED # MmSystemTable
> 
>  [Protocols]
> -  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
> +  gEfiMmRscHandlerProtocolGuid                  ## CONSUMES
> 
>  [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial ##
> CONSUMES @@ -59,7 +60,4 @@ [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeMemorySize |128|
> gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory   ##
> SOMETIMES_CONSUMES
> 
>  [Depex]
> -  gEfiSmmRscHandlerProtocolGuid
> -
> -[UserExtensions.TianoCore."ExtraFiles"]
> -  StatusCodeHandlerSmmExtra.uni
> +  gEfiMmRscHandlerProtocolGuid
> --
> 2.30.0.windows.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support
  2021-01-05 18:59 ` [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support Kun Qin
@ 2021-01-06  3:24   ` Wu, Hao A
  0 siblings, 0 replies; 34+ messages in thread
From: Wu, Hao A @ 2021-01-06  3:24 UTC (permalink / raw)
  To: Kun Qin, devel@edk2.groups.io; +Cc: Wang, Jian J, Bi, Dandan, Liming Gao

> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>;
> Bi, Dandan <dandan.bi@intel.com>; Liming Gao <gaoliming@byosoft.com.cn>
> Subject: [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable:
> Added StandaloneMm support
> 
> This change added support of FPDT driver under StandaloneMm. It replaces
> SMM version ReportStatusCode protocol with MM version. This patch also
> abstracts standalone and traditional MM interfaces into separate files to
> support each corresponding function prototypes and implementations.


Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Removed "EFIAPI" for internally abstracted functions [Hao]
>     - Support driver for both IA32 and X64 in dsc file [Hao]
> 
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/{Firm
> warePerformanceSmm.c => FirmwarePerformanceCommon.c}           | 76
> +++++++++-----------
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceStandaloneMm.c                                   | 61
> ++++++++++++++++
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceTraditional.c                                    | 61 ++++++++++++++++
>  MdeModulePkg/MdeModulePkg.dsc
> |  2 +
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceCommon.h                                         | 50 +++++++++++++
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceSmm.inf                                          | 11 +--
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/{Firm
> warePerformanceSmm.inf => FirmwarePerformanceStandaloneMm.inf} | 31
> ++++----
>  7 files changed, 230 insertions(+), 62 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.c
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceCommon.c
> similarity index 75%
> rename from
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceSmm.c
> rename to
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceCommon.c
> index d6c6e7693e4d..ecadef8711ed 100644
> ---
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.c
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceCommon.c
> @@ -1,11 +1,11 @@
>  /** @file
> -  This module collects performance data for SMM driver boot records and S3
> Suspend Performance Record.
> +  This module collects performance data for MM driver boot records and S3
> Suspend Performance Record.
> 
>    This module registers report status code listener to collect performance
> data
> -  for SMM driver boot records and S3 Suspend Performance Record.
> +  for MM driver boot records and S3 Suspend Performance Record.
> 
>    Caution: This module requires additional review when modified.
> -  This driver will have external input - communicate buffer in SMM mode.
> +  This driver will have external input - communicate buffer in MM mode.
>    This external input must be validated carefully to avoid security issue like
>    buffer overflow, integer overflow.
> 
> @@ -16,13 +16,13 @@
> 
>  **/
> 
> -#include <PiSmm.h>
> +#include <PiMm.h>
> 
> -#include <Protocol/SmmReportStatusCodeHandler.h>
> +#include <Protocol/MmReportStatusCodeHandler.h>
> 
>  #include <Guid/FirmwarePerformance.h>
> 
> -#include <Library/SmmServicesTableLib.h>
> +#include <Library/MmServicesTableLib.h>
>  #include <Library/BaseLib.h>
>  #include <Library/DebugLib.h>
>  #include <Library/TimerLib.h>
> @@ -30,23 +30,22 @@
>  #include <Library/PcdLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/MemoryAllocationLib.h> -#include
> <Library/UefiBootServicesTableLib.h>
>  #include <Library/SynchronizationLib.h> -#include <Library/SmmMemLib.h>
> +#include "FirmwarePerformanceCommon.h"
> 
> -SMM_BOOT_PERFORMANCE_TABLE    *mSmmBootPerformanceTable =
> NULL;
> +SMM_BOOT_PERFORMANCE_TABLE    *mMmBootPerformanceTable =
> NULL;
> 
> -EFI_SMM_RSC_HANDLER_PROTOCOL  *mRscHandlerProtocol    = NULL;
> +EFI_MM_RSC_HANDLER_PROTOCOL   *mRscHandlerProtocol    = NULL;
>  UINT64                        mSuspendStartTime       = 0;
>  BOOLEAN                       mS3SuspendLockBoxSaved  = FALSE;
>  UINT32                        mBootRecordSize = 0;
>  UINT8                         *mBootRecordBuffer = NULL;
> 
> -SPIN_LOCK                     mSmmFpdtLock;
> -BOOLEAN                       mSmramIsOutOfResource = FALSE;
> +SPIN_LOCK                     mMmFpdtLock;
> +BOOLEAN                       mMmramIsOutOfResource = FALSE;
> 
>  /**
> -  Report status code listener for SMM. This is used to record the
> performance
> +  Report status code listener for MM. This is used to record the
> + performance
>    data for S3 Suspend Start and S3 Suspend End in FPDT.
> 
>    @param[in]  CodeType            Indicates the type of status code being
> reported.
> @@ -66,7 +65,7 @@ BOOLEAN                       mSmramIsOutOfResource = FALSE;
>  **/
>  EFI_STATUS
>  EFIAPI
> -FpdtStatusCodeListenerSmm (
> +FpdtStatusCodeListenerMm (
>    IN EFI_STATUS_CODE_TYPE     CodeType,
>    IN EFI_STATUS_CODE_VALUE    Value,
>    IN UINT32                   Instance,
> @@ -89,19 +88,19 @@ FpdtStatusCodeListenerSmm (
>    // Collect one or more Boot records in boot time
>    //
>    if (Data != NULL && CompareGuid (&Data->Type,
> &gEdkiiFpdtExtendedFirmwarePerformanceGuid)) {
> -    AcquireSpinLock (&mSmmFpdtLock);
> +    AcquireSpinLock (&mMmFpdtLock);
>      //
>      // Get the boot performance data.
>      //
> -    CopyMem (&mSmmBootPerformanceTable, Data + 1, Data->Size);
> -    mBootRecordBuffer = ((UINT8 *) (mSmmBootPerformanceTable)) +
> sizeof (SMM_BOOT_PERFORMANCE_TABLE);
> +    CopyMem (&mMmBootPerformanceTable, Data + 1, Data->Size);
> +    mBootRecordBuffer = ((UINT8 *) (mMmBootPerformanceTable)) + sizeof
> + (SMM_BOOT_PERFORMANCE_TABLE);
> 
> -    ReleaseSpinLock (&mSmmFpdtLock);
> +    ReleaseSpinLock (&mMmFpdtLock);
>      return EFI_SUCCESS;
>    }
> 
>    if (Data != NULL && CompareGuid (&Data->Type,
> &gEfiFirmwarePerformanceGuid)) {
> -    DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerSmm: Performance
> data reported through gEfiFirmwarePerformanceGuid will not be collected
> by FirmwarePerformanceDataTableSmm\n"));
> +    DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerMm: Performance
> data
> + reported through gEfiFirmwarePerformanceGuid will not be collected by
> + FirmwarePerformanceDataTableMm\n"));
>      return EFI_UNSUPPORTED;
>    }
> 
> @@ -157,7 +156,7 @@ FpdtStatusCodeListenerSmm (
>  /**
>    Communication service SMI Handler entry.
> 
> -  This SMI handler provides services for report SMM boot records.
> +  This SMI handler provides services for report MM boot records.
> 
>    Caution: This function may receive untrusted input.
>    Communicate buffer and buffer size are external input, so this function will
> do basic validation.
> @@ -166,7 +165,7 @@ FpdtStatusCodeListenerSmm (
>    @param[in]     RegisterContext Points to an optional handler context which
> was specified when the
>                                   handler was registered.
>    @param[in, out] CommBuffer     A pointer to a collection of data in memory
> that will
> -                                 be conveyed from a non-SMM environment into an SMM
> environment.
> +                                 be conveyed from a non-MM environment into an MM
> environment.
>    @param[in, out] CommBufferSize The size of the CommBuffer.
> 
>    @retval EFI_SUCCESS                         The interrupt was handled and quiesced.
> No other handlers
> @@ -207,8 +206,8 @@ FpdtSmiHandler (
>      return EFI_SUCCESS;
>    }
> 
> -  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer,
> TempCommBufferSize)) {
> -    DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data
> buffer in SMRAM or overflow!\n"));
> +  if (!IsBufferOutsideMmValid ((UINTN)CommBuffer,
> TempCommBufferSize)) {
> +    DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM communication data
> buffer
> + in MMRAM or overflow!\n"));
>      return EFI_SUCCESS;
>    }
> 
> @@ -218,8 +217,8 @@ FpdtSmiHandler (
> 
>    switch (SmmCommData->Function) {
>      case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
> -      if (mSmmBootPerformanceTable != NULL) {
> -        mBootRecordSize = mSmmBootPerformanceTable->Header.Length -
> sizeof (SMM_BOOT_PERFORMANCE_TABLE);
> +      if (mMmBootPerformanceTable != NULL) {
> +        mBootRecordSize = mMmBootPerformanceTable->Header.Length -
> + sizeof (SMM_BOOT_PERFORMANCE_TABLE);
>        }
>        SmmCommData->BootRecordSize = mBootRecordSize;
>        break;
> @@ -244,8 +243,8 @@ FpdtSmiHandler (
>          BootRecordSize = mBootRecordSize - BootRecordOffset;
>        }
>        SmmCommData->BootRecordSize = BootRecordSize;
> -      if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData,
> BootRecordSize)) {
> -        DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM
> or overflow!\n"));
> +      if (!IsBufferOutsideMmValid ((UINTN)BootRecordData, BootRecordSize))
> {
> +        DEBUG ((DEBUG_ERROR, "FpdtSmiHandler: MM Data buffer in
> MMRAM
> + or overflow!\n"));
>          Status = EFI_ACCESS_DENIED;
>          break;
>        }
> @@ -267,20 +266,15 @@ FpdtSmiHandler (
>  }
> 
>  /**
> -  The module Entry Point of the Firmware Performance Data Table SMM
> driver.
> -
> -  @param[in]  ImageHandle    The firmware allocated handle for the EFI
> image.
> -  @param[in]  SystemTable    A pointer to the EFI System Table.
> +  The module Entry Point of the Firmware Performance Data Table MM
> driver.
> 
>    @retval EFI_SUCCESS    The entry point is executed successfully.
>    @retval Other          Some error occurs when executing this entry point.
> 
>  **/
>  EFI_STATUS
> -EFIAPI
> -FirmwarePerformanceSmmEntryPoint (
> -  IN EFI_HANDLE          ImageHandle,
> -  IN EFI_SYSTEM_TABLE    *SystemTable
> +FirmwarePerformanceCommonEntryPoint (
> +  VOID
>    )
>  {
>    EFI_STATUS                Status;
> @@ -289,13 +283,13 @@ FirmwarePerformanceSmmEntryPoint (
>    //
>    // Initialize spin lock
>    //
> -  InitializeSpinLock (&mSmmFpdtLock);
> +  InitializeSpinLock (&mMmFpdtLock);
> 
>    //
> -  // Get SMM Report Status Code Handler Protocol.
> +  // Get MM Report Status Code Handler Protocol.
>    //
> -  Status = gSmst->SmmLocateProtocol (
> -                    &gEfiSmmRscHandlerProtocolGuid,
> +  Status = gMmst->MmLocateProtocol (
> +                    &gEfiMmRscHandlerProtocolGuid,
>                      NULL,
>                      (VOID **) &mRscHandlerProtocol
>                      );
> @@ -304,14 +298,14 @@ FirmwarePerformanceSmmEntryPoint (
>    //
>    // Register report status code listener for BootRecords and S3 Suspend
> Start and End.
>    //
> -  Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
> +  Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerMm);
>    ASSERT_EFI_ERROR (Status);
> 
>    //
>    // Register SMI handler.
>    //
>    Handle = NULL;
> -  Status = gSmst->SmiHandlerRegister (FpdtSmiHandler,
> &gEfiFirmwarePerformanceGuid, &Handle);
> +  Status = gMmst->MmiHandlerRegister (FpdtSmiHandler,
> + &gEfiFirmwarePerformanceGuid, &Handle);
>    ASSERT_EFI_ERROR (Status);
> 
>    return Status;
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceStandaloneMm.c
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceStandaloneMm.c
> new file mode 100644
> index 000000000000..d7da61c98c66
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceStandaloneMm.c
> @@ -0,0 +1,61 @@
> +/** @file
> +  This module collects performance data for MM driver boot records and S3
> Suspend Performance Record.
> +
> +  This module registers report status code listener to collect
> + performance data  for MM driver boot records and S3 Suspend
> Performance Record.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - communicate buffer in MM mode.
> +  This external input must be validated carefully to avoid security
> + issue like  buffer overflow, integer overflow.
> +
> +  FpdtSmiHandler() will receive untrusted input and do basic validation.
> +
> +  Copyright (c) 2011 - 2018, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c), Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include <Library/StandaloneMmMemLib.h> #include
> +"FirmwarePerformanceCommon.h"
> +
> +/**
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not
> overlap with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
> +**/
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  )
> +{
> +  return MmIsBufferOutsideMmValid (Buffer, Length); }
> +
> +/**
> +  The module Entry Point of the Firmware Performance Data Table MM
> driver.
> +
> +  @param[in]  ImageHandle    The firmware allocated handle for the EFI
> image.
> +  @param[in]  SystemTable    A pointer to the EFI MM System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +FirmwarePerformanceStandaloneMmEntryPoint (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *SystemTable
> +  )
> +{
> +  return FirmwarePerformanceCommonEntryPoint (); }
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceTraditional.c
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceTraditional.c
> new file mode 100644
> index 000000000000..43c050d6b516
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceTraditional.c
> @@ -0,0 +1,61 @@
> +/** @file
> +  This module collects performance data for MM driver boot records and S3
> Suspend Performance Record.
> +
> +  This module registers report status code listener to collect
> + performance data  for MM driver boot records and S3 Suspend
> Performance Record.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - communicate buffer in MM mode.
> +  This external input must be validated carefully to avoid security
> + issue like  buffer overflow, integer overflow.
> +
> +  FpdtSmiHandler() will receive untrusted input and do basic validation.
> +
> +  Copyright (c) 2011 - 2018, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c), Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiSmm.h>
> +
> +#include <Library/SmmMemLib.h>
> +#include "FirmwarePerformanceCommon.h"
> +
> +/**
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not
> overlap with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
> +**/
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  )
> +{
> +  return SmmIsBufferOutsideSmmValid (Buffer, Length); }
> +
> +/**
> +  The module Entry Point of the Firmware Performance Data Table MM
> driver.
> +
> +  @param[in]  ImageHandle    The firmware allocated handle for the EFI
> image.
> +  @param[in]  SystemTable    A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +FirmwarePerformanceSmmEntryPoint (
> +  IN EFI_HANDLE          ImageHandle,
> +  IN EFI_SYSTEM_TABLE    *SystemTable
> +  )
> +{
> +  return FirmwarePerformanceCommonEntryPoint (); }
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc index 098909490095..34ca571ca662
> 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -168,6 +168,7 @@ [LibraryClasses.common.MM_STANDALONE]
> 
> StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntry
> Point/StandaloneMmDriverEntryPoint.inf
> 
> MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/Stan
> daloneMmServicesTableLib.inf
> 
> LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxStandalo
> neMmLib.inf
> +
> +
> MemLib|StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMm
> MemLib.i
> + nf
> 
>  [LibraryClasses.ARM, LibraryClasses.AARCH64]
>    ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
> @@ -495,6 +496,7 @@ [Components.IA32, Components.X64]
>    MdeModulePkg/Universal/Acpi/S3SaveStateDxe/S3SaveStateDxe.inf
>    MdeModulePkg/Universal/Acpi/SmmS3SaveState/SmmS3SaveState.inf
> 
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceSmm.inf
> +
> +
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePe
> + rformanceStandaloneMm.inf
> 
> MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.i
> nf
> 
> MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmmD
> xe.inf
> 
> MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceCommon.h
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceCommon.h
> new file mode 100644
> index 000000000000..0fbdac02de4c
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceCommon.h
> @@ -0,0 +1,50 @@
> +/** @file
> +  This module collects performance data for SMM driver boot records and S3
> Suspend Performance Record.
> +
> +  This module registers report status code listener to collect
> + performance data  for SMM driver boot records and S3 Suspend
> Performance Record.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - communicate buffer in SMM mode.
> +  This external input must be validated carefully to avoid security
> + issue like  buffer overflow, integer overflow.
> +
> +  FpdtSmiHandler() will receive untrusted input and do basic validation.
> +
> +  Copyright (c) 2011 - 2018, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c), Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#ifndef _FW_PERF_COMMON_H_
> +#define _FW_PERF_COMMON_H_
> +
> +/**
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not
> overlap with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
> +**/
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  );
> +
> +/**
> +  The module Entry Point of the Firmware Performance Data Table MM
> driver.
> +
> +  @retval EFI_SUCCESS    The entry point is executed successfully.
> +  @retval Other          Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +FirmwarePerformanceCommonEntryPoint (
> +  VOID
> +  );
> +
> +#endif // _FW_PERF_COMMON_H_
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.inf
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.inf
> index 618cbd56ca59..b7194bd899dd 100644
> ---
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.inf
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceSmm.inf
> @@ -26,7 +26,9 @@ [Defines]
>  #
> 
>  [Sources]
> -  FirmwarePerformanceSmm.c
> +  FirmwarePerformanceCommon.c
> +  FirmwarePerformanceCommon.h
> +  FirmwarePerformanceTraditional.c
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> @@ -34,7 +36,7 @@ [Packages]
> 
>  [LibraryClasses]
>    UefiDriverEntryPoint
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    BaseLib
>    DebugLib
>    TimerLib
> @@ -42,12 +44,11 @@ [LibraryClasses]
>    PcdLib
>    BaseMemoryLib
>    MemoryAllocationLib
> -  UefiBootServicesTableLib
>    SynchronizationLib
>    SmmMemLib
> 
>  [Protocols]
> -  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
> +  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
> 
>  [Guids]
>    ## SOMETIMES_PRODUCES   ## UNDEFINED # SaveLockBox
> @@ -61,7 +62,7 @@ [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdProgressCodeS3SuspendEnd    ##
> CONSUMES
> 
>  [Depex]
> -  gEfiSmmRscHandlerProtocolGuid
> +  gEfiMmRscHandlerProtocolGuid
> 
>  [UserExtensions.TianoCore."ExtraFiles"]
>    FirmwarePerformanceSmmExtra.uni
> diff --git
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.inf
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceStandaloneMm.inf
> similarity index 65%
> copy from
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceSmm.inf
> copy to
> MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Firm
> warePerformanceStandaloneMm.inf
> index 618cbd56ca59..e6aad88be0ef 100644
> ---
> a/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwarePerformanceSmm.inf
> +++
> b/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/Fir
> mwa
> +++ rePerformanceStandaloneMm.inf
> @@ -5,19 +5,19 @@
>  #  for SMM boot performance records and S3 Suspend Performance Record.
>  #
>  #  Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent  #  ##
> 
>  [Defines]
>    INF_VERSION                    = 0x00010005
> -  BASE_NAME                      = FirmwarePerformanceSmm
> -  MODULE_UNI_FILE                = FirmwarePerformanceSmm.uni
> -  FILE_GUID                      = 044310AB-77FD-402a-AF1A-87D4120E7329
> -  MODULE_TYPE                    = DXE_SMM_DRIVER
> +  BASE_NAME                      = FirmwarePerformanceStandaloneMm
> +  FILE_GUID                      = 827AC29D-E52D-4B1A-874A-C6577E0699CF
> +  MODULE_TYPE                    = MM_STANDALONE
>    VERSION_STRING                 = 1.0
> -  PI_SPECIFICATION_VERSION       = 0x0001000A
> -  ENTRY_POINT                    = FirmwarePerformanceSmmEntryPoint
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = FirmwarePerformanceStandaloneMmEntryPoint
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
> @@ -26,15 +26,18 @@ [Defines]
>  #
> 
>  [Sources]
> -  FirmwarePerformanceSmm.c
> +  FirmwarePerformanceCommon.c
> +  FirmwarePerformanceCommon.h
> +  FirmwarePerformanceStandaloneMm.c
> 
>  [Packages]
>    MdePkg/MdePkg.dec
>    MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> 
>  [LibraryClasses]
> -  UefiDriverEntryPoint
> -  SmmServicesTableLib
> +  StandaloneMmDriverEntryPoint
> +  MmServicesTableLib
>    BaseLib
>    DebugLib
>    TimerLib
> @@ -42,12 +45,11 @@ [LibraryClasses]
>    PcdLib
>    BaseMemoryLib
>    MemoryAllocationLib
> -  UefiBootServicesTableLib
>    SynchronizationLib
> -  SmmMemLib
> +  MemLib
> 
>  [Protocols]
> -  gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES
> +  gEfiMmRscHandlerProtocolGuid                 ## CONSUMES
> 
>  [Guids]
>    ## SOMETIMES_PRODUCES   ## UNDEFINED # SaveLockBox
> @@ -61,7 +63,4 @@ [Pcd]
>    gEfiMdeModulePkgTokenSpaceGuid.PcdProgressCodeS3SuspendEnd    ##
> CONSUMES
> 
>  [Depex]
> -  gEfiSmmRscHandlerProtocolGuid
> -
> -[UserExtensions.TianoCore."ExtraFiles"]
> -  FirmwarePerformanceSmmExtra.uni
> +  gEfiMmRscHandlerProtocolGuid
> --
> 2.30.0.windows.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router
  2021-01-05 18:59 ` [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router Kun Qin
@ 2021-01-06  3:24   ` Wu, Hao A
  0 siblings, 0 replies; 34+ messages in thread
From: Wu, Hao A @ 2021-01-06  3:24 UTC (permalink / raw)
  To: Kun Qin, devel@edk2.groups.io; +Cc: Wang, Jian J, Bi, Dandan, Liming Gao

> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>;
> Bi, Dandan <dandan.bi@intel.com>; Liming Gao <gaoliming@byosoft.com.cn>
> Subject: [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter:
> Support StandaloneMm RSC Router
> 
> This change added support of RSC router under StandaloneMm. It replaces
> SMM version ReportStatusCode protocol definitions with MM version. This
> patch also switched to use gMmst instead of gSmst. Lastly, it abstracts
> standalone and traditional MM driver entrypoints into separate files to allow
> maximal common implementations.


A minor comment:
Could you help to update the function description of ReportDispatcher() for the 2nd input parameter:
Type -> CodeType

With this handled:
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Removed "EFIAPI" for internally abstracted functions [Hao]
>     - Updated function descriptions [Hao]
>     - Updated ReportDispatcher to make *.h and *.c consistent [Hao]
> 
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/{ReportStatusCo
> deRouterSmm.c => ReportStatusCodeRouterCommon.c} | 55 +++++++++----
> -------
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterStandaloneMm.c                            | 33 ++++++++++++
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterTraditional.c                             | 33 ++++++++++++
>  MdeModulePkg/MdeModulePkg.dsc
> |  1 +
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/{ReportStatusCo
> deRouterSmm.h => ReportStatusCodeRouterCommon.h} | 44 ++++++++++--
> ----
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.inf                                   | 13 ++---
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterStandaloneMm.inf                          | 49 +++++++++++++++++
>  7 files changed, 176 insertions(+), 52 deletions(-)
> 
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.c
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterCommon.c
> similarity index 75%
> rename from
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.c
> rename to
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterCommon.c
> index c3ab5cd05045..d84efaa79b7f 100644
> ---
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.c
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterCommon.c
> @@ -7,7 +7,7 @@
> 
>  **/
> 
> -#include "ReportStatusCodeRouterSmm.h"
> +#include "ReportStatusCodeRouterCommon.h"
> 
>  LIST_ENTRY   mCallbackListHead          = INITIALIZE_LIST_HEAD_VARIABLE
> (mCallbackListHead);
> 
> @@ -17,11 +17,11 @@ LIST_ENTRY   mCallbackListHead          =
> INITIALIZE_LIST_HEAD_VARIABLE (mCallba
>  //
>  UINT32       mStatusCodeNestStatus = 0;
> 
> -EFI_SMM_STATUS_CODE_PROTOCOL  mSmmStatusCodeProtocol  = {
> +EFI_MM_STATUS_CODE_PROTOCOL   mSmmStatusCodeProtocol  = {
>    ReportDispatcher
>  };
> 
> -EFI_SMM_RSC_HANDLER_PROTOCOL  mSmmRscHandlerProtocol = {
> +EFI_MM_RSC_HANDLER_PROTOCOL   mSmmRscHandlerProtocol = {
>    Register,
>    Unregister
>    };
> @@ -45,18 +45,18 @@ EFI_SMM_RSC_HANDLER_PROTOCOL
> mSmmRscHandlerProtocol = {  EFI_STATUS  EFIAPI  Register (
> -  IN EFI_SMM_RSC_HANDLER_CALLBACK   Callback
> +  IN EFI_MM_RSC_HANDLER_CALLBACK    Callback
>    )
>  {
>    LIST_ENTRY                      *Link;
> -  SMM_RSC_HANDLER_CALLBACK_ENTRY  *CallbackEntry;
> +  MM_RSC_HANDLER_CALLBACK_ENTRY  *CallbackEntry;
> 
>    if (Callback == NULL) {
>      return EFI_INVALID_PARAMETER;
>    }
> 
>    for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead,
> Link); Link = GetNextNode (&mCallbackListHead, Link)) {
> -    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
> +    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> + MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
>      if (CallbackEntry->RscHandlerCallback == Callback) {
>        //
>        // If the function was already registered. It can't be registered again.
> @@ -65,10 +65,10 @@ Register (
>      }
>    }
> 
> -  CallbackEntry = (SMM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool
> (sizeof (SMM_RSC_HANDLER_CALLBACK_ENTRY));
> +  CallbackEntry = (MM_RSC_HANDLER_CALLBACK_ENTRY *)AllocatePool
> (sizeof
> + (MM_RSC_HANDLER_CALLBACK_ENTRY));
>    ASSERT (CallbackEntry != NULL);
> 
> -  CallbackEntry->Signature          =
> SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
> +  CallbackEntry->Signature          =
> MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE;
>    CallbackEntry->RscHandlerCallback = Callback;
> 
>    InsertTailList (&mCallbackListHead, &CallbackEntry->Node); @@ -92,18
> +92,18 @@ Register (  EFI_STATUS  EFIAPI  Unregister (
> -  IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
> +  IN EFI_MM_RSC_HANDLER_CALLBACK  Callback
>    )
>  {
>    LIST_ENTRY                        *Link;
> -  SMM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
> +  MM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
> 
>    if (Callback == NULL) {
>      return EFI_INVALID_PARAMETER;
>    }
> 
>    for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead,
> Link); Link = GetNextNode (&mCallbackListHead, Link)) {
> -    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
> +    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> + MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
>      if (CallbackEntry->RscHandlerCallback == Callback) {
>        //
>        // If the function is found in list, delete it and return.
> @@ -140,16 +140,16 @@ Unregister (
>  EFI_STATUS
>  EFIAPI
>  ReportDispatcher (
> -  IN CONST EFI_SMM_STATUS_CODE_PROTOCOL  *This,
> -  IN EFI_STATUS_CODE_TYPE                Type,
> +  IN CONST EFI_MM_STATUS_CODE_PROTOCOL   *This,
> +  IN EFI_STATUS_CODE_TYPE                CodeType,
>    IN EFI_STATUS_CODE_VALUE               Value,
>    IN UINT32                              Instance,
> -  IN CONST EFI_GUID                      *CallerId  OPTIONAL,
> +  IN CONST EFI_GUID                      *CallerId,
>    IN EFI_STATUS_CODE_DATA                *Data      OPTIONAL
>    )
>  {
>    LIST_ENTRY                        *Link;
> -  SMM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
> +  MM_RSC_HANDLER_CALLBACK_ENTRY    *CallbackEntry;
> 
>    //
>    // Use atom operation to avoid the reentant of report.
> @@ -160,13 +160,13 @@ ReportDispatcher (
>    }
> 
>    for (Link = GetFirstNode (&mCallbackListHead); !IsNull (&mCallbackListHead,
> Link);) {
> -    CallbackEntry = CR (Link, SMM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
> +    CallbackEntry = CR (Link, MM_RSC_HANDLER_CALLBACK_ENTRY, Node,
> + MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE);
>      //
>      // The handler may remove itself, so get the next handler in advance.
>      //
>      Link = GetNextNode (&mCallbackListHead, Link);
>      CallbackEntry->RscHandlerCallback (
> -                     Type,
> +                     CodeType,
>                       Value,
>                       Instance,
>                       (EFI_GUID*)CallerId, @@ -186,20 +186,15 @@ ReportDispatcher (
>  /**
>    Entry point of Generic Status Code Driver.
> 
> -  This function is the entry point of SMM Status Code Router .
> -  It produces SMM Report Stataus Code Handler and Status Code protocol.
> -
> -  @param  ImageHandle       The firmware allocated handle for the EFI image.
> -  @param  SystemTable       A pointer to the EFI System Table.
> +  This function is the common entry point of MM Status Code Router.
> +  It produces MM Report Status Code Handler and Status Code protocol.
> 
>    @retval EFI_SUCCESS       The entry point is executed successfully.
> 
>  **/
>  EFI_STATUS
> -EFIAPI
> -GenericStatusCodeSmmEntry (
> -  IN EFI_HANDLE         ImageHandle,
> -  IN EFI_SYSTEM_TABLE   *SystemTable
> +GenericStatusCodeCommonEntry (
> +  VOID
>    )
>  {
>    EFI_STATUS     Status;
> @@ -210,9 +205,9 @@ GenericStatusCodeSmmEntry (
>    //
>    // Install SmmRscHandler Protocol
>    //
> -  Status = gSmst->SmmInstallProtocolInterface (
> +  Status = gMmst->MmInstallProtocolInterface (
>                      &Handle,
> -                    &gEfiSmmRscHandlerProtocolGuid,
> +                    &gEfiMmRscHandlerProtocolGuid,
>                      EFI_NATIVE_INTERFACE,
>                      &mSmmRscHandlerProtocol
>                      );
> @@ -221,9 +216,9 @@ GenericStatusCodeSmmEntry (
>    //
>    // Install SmmStatusCode Protocol
>    //
> -  Status = gSmst->SmmInstallProtocolInterface (
> +  Status = gMmst->MmInstallProtocolInterface (
>                      &Handle,
> -                    &gEfiSmmStatusCodeProtocolGuid,
> +                    &gEfiMmStatusCodeProtocolGuid,
>                      EFI_NATIVE_INTERFACE,
>                      &mSmmStatusCodeProtocol
>                      );
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterStandaloneMm.c
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterStandaloneMm.c
> new file mode 100644
> index 000000000000..bd1519fa1506
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterStandaloneMm.c
> @@ -0,0 +1,33 @@
> +/** @file
> +  Report Status Code Router Driver which produces MM Report Stataus
> +Code Handler Protocol
> +  and MM Status Code Protocol.
> +
> +  Copyright (c) 2009 - 2018, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "ReportStatusCodeRouterCommon.h"
> +
> +/**
> +  Entry point of Generic Status Code Driver.
> +
> +  This function is the entry point of MM Status Code Router .
> +  It produces MM Report Stataus Code Handler and Status Code protocol.
> +
> +  @param  ImageHandle       The firmware allocated handle for the EFI image.
> +  @param  SystemTable       A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +GenericStatusCodeStandaloneMmEntry (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *SystemTable
> +  )
> +{
> +  return GenericStatusCodeCommonEntry (); }
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterTraditional.c
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterTraditional.c
> new file mode 100644
> index 000000000000..360a0eef6b6d
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterTraditional.c
> @@ -0,0 +1,33 @@
> +/** @file
> +  Report Status Code Router Driver which produces MM Report Stataus
> +Code Handler Protocol
> +  and MM Status Code Protocol.
> +
> +  Copyright (c) 2009 - 2018, Intel Corporation. All rights
> + reserved.<BR>  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include "ReportStatusCodeRouterCommon.h"
> +
> +/**
> +  Entry point of Generic Status Code Driver.
> +
> +  This function is the entry point of SMM Status Code Router .
> +  It produces SMM Report Stataus Code Handler and Status Code protocol.
> +
> +  @param  ImageHandle       The firmware allocated handle for the EFI image.
> +  @param  SystemTable       A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +GenericStatusCodeTraditionalEntry (
> +  IN EFI_HANDLE         ImageHandle,
> +  IN EFI_SYSTEM_TABLE   *SystemTable
> +  )
> +{
> +  return GenericStatusCodeCommonEntry (); }
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc index 34ca571ca662..f95c7cd69ee1
> 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -479,6 +479,7 @@ [Components.IA32, Components.X64]
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSm
> m.inf
> 
> MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSta
> ndaloneMm.inf
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.inf
> +
> +
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRout
> + erStandaloneMm.inf
>    MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> 
> MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllo
> cationProfileLib.inf
> 
> MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMem
> oryAllocationProfileLib.inf
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.h
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterCommon.h
> similarity index 73%
> rename from
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterSmm.h
> rename to
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCod
> eRouterCommon.h
> index f8c48c62e790..4ead2dcbdf2c 100644
> ---
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.h
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterCommon.h
> @@ -6,28 +6,26 @@
> 
>  **/
> 
> -#ifndef __REPORT_STATUS_CODE_ROUTER_SMM_H__
> -#define __REPORT_STATUS_CODE_ROUTER_SMM_H__
> +#ifndef __REPORT_STATUS_CODE_ROUTER_COMMON_H__
> +#define __REPORT_STATUS_CODE_ROUTER_COMMON_H__
> 
> -
> -#include <Protocol/SmmReportStatusCodeHandler.h>
> -#include <Protocol/SmmStatusCode.h>
> +#include <Protocol/MmReportStatusCodeHandler.h>
> +#include <Protocol/MmStatusCode.h>
> 
>  #include <Library/BaseLib.h>
>  #include <Library/SynchronizationLib.h>  #include <Library/DebugLib.h>
> #include <Library/PcdLib.h> -#include <Library/UefiDriverEntryPoint.h> -
> #include <Library/SmmServicesTableLib.h>
> +#include <Library/MmServicesTableLib.h>
>  #include <Library/MemoryAllocationLib.h>
> 
> -#define SMM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
> SIGNATURE_32 ('s', 'h', 'c', 'e')
> +#define MM_RSC_HANDLER_CALLBACK_ENTRY_SIGNATURE
> SIGNATURE_32 ('s',
> +'h', 'c', 'e')
> 
>  typedef struct {
>    UINTN                         Signature;
> -  EFI_SMM_RSC_HANDLER_CALLBACK  RscHandlerCallback;
> +  EFI_MM_RSC_HANDLER_CALLBACK   RscHandlerCallback;
>    LIST_ENTRY                    Node;
> -} SMM_RSC_HANDLER_CALLBACK_ENTRY;
> +} MM_RSC_HANDLER_CALLBACK_ENTRY;
> 
>  /**
>    Register the callback function for ReportStatusCode() notification.
> @@ -48,7 +46,7 @@ typedef struct {
>  EFI_STATUS
>  EFIAPI
>  Register (
> -  IN EFI_SMM_RSC_HANDLER_CALLBACK   Callback
> +  IN EFI_MM_RSC_HANDLER_CALLBACK    Callback
>    );
> 
>  /**
> @@ -67,13 +65,13 @@ Register (
>  EFI_STATUS
>  EFIAPI
>  Unregister (
> -  IN EFI_SMM_RSC_HANDLER_CALLBACK Callback
> +  IN EFI_MM_RSC_HANDLER_CALLBACK  Callback
>    );
> 
>  /**
>    Provides an interface that a software module can call to report a status
> code.
> 
> -  @param  This             EFI_SMM_STATUS_CODE_PROTOCOL instance.
> +  @param  This             EFI_MM_RSC_HANDLER_CALLBACK instance.
>    @param  Type             Indicates the type of status code being reported.
>    @param  Value            Describes the current status of a hardware or
> software entity.
>                             This included information about the class and subclass that is
> used to @@ -92,12 +90,26 @@ Unregister (  EFI_STATUS  EFIAPI
> ReportDispatcher (
> -  IN CONST EFI_SMM_STATUS_CODE_PROTOCOL  *This,
> -  IN EFI_STATUS_CODE_TYPE                Type,
> +  IN CONST EFI_MM_STATUS_CODE_PROTOCOL   *This,
> +  IN EFI_STATUS_CODE_TYPE                CodeType,
>    IN EFI_STATUS_CODE_VALUE               Value,
>    IN UINT32                              Instance,
> -  IN CONST EFI_GUID                      *CallerId  OPTIONAL,
> +  IN CONST EFI_GUID                      *CallerId,
>    IN EFI_STATUS_CODE_DATA                *Data      OPTIONAL
>    );
> 
> +/**
> +  Entry point of Generic Status Code Driver.
> +
> +  This function is the common entry point of MM Status Code Router.
> +  It produces MM Report Status Code Handler and Status Code protocol.
> +
> +  @retval EFI_SUCCESS       The entry point is executed successfully.
> +
> +**/
> +EFI_STATUS
> +GenericStatusCodeCommonEntry (
> +  VOID
> +  );
> +
>  #endif
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.inf
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.inf
> index 46fdcb7bf959..539badc4c755 100644
> ---
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterSmm.inf
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterSmm.inf
> @@ -16,7 +16,7 @@ [Defines]
>    MODULE_TYPE                    = DXE_SMM_DRIVER
>    PI_SPECIFICATION_VERSION       = 0x0001000A
>    VERSION_STRING                 = 1.0
> -  ENTRY_POINT                    = GenericStatusCodeSmmEntry
> +  ENTRY_POINT                    = GenericStatusCodeTraditionalEntry
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
> @@ -25,15 +25,16 @@ [Defines]
>  #
> 
>  [Sources]
> -  ReportStatusCodeRouterSmm.c
> -  ReportStatusCodeRouterSmm.h
> +  ReportStatusCodeRouterCommon.c
> +  ReportStatusCodeRouterCommon.h
> +  ReportStatusCodeRouterTraditional.c
> 
>  [Packages]
>    MdePkg/MdePkg.dec
>    MdeModulePkg/MdeModulePkg.dec
> 
>  [LibraryClasses]
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    UefiDriverEntryPoint
>    DebugLib
>    BaseLib
> @@ -41,8 +42,8 @@ [LibraryClasses]
>    MemoryAllocationLib
> 
>  [Protocols]
> -  gEfiSmmRscHandlerProtocolGuid               ## PRODUCES
> -  gEfiSmmStatusCodeProtocolGuid               ## PRODUCES
> +  gEfiMmRscHandlerProtocolGuid               ## PRODUCES
> +  gEfiMmStatusCodeProtocolGuid               ## PRODUCES
> 
>  [Depex]
>    TRUE
> diff --git
> a/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterStandaloneMm.inf
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> odeRouterStandaloneMm.inf
> new file mode 100644
> index 000000000000..7aa0127e0bec
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusC
> ode
> +++ RouterStandaloneMm.inf
> @@ -0,0 +1,49 @@
> +## @file
> +#  Report Status Code Router Driver which produces MM Report Stataus
> Code Handler Protocol and MM Status Code Protocol.
> +#
> +#  Copyright (c) 2009 - 2018, Intel Corporation. All rights
> +reserved.<BR> #  Copyright (c) Microsoft Corporation.
> +#
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x00010005
> +  BASE_NAME                      = ReportStatusCodeRouterStandaloneMm
> +  FILE_GUID                      = EAEEDEF9-ABE7-4B95-82B0-5A534C899B46
> +  MODULE_TYPE                    = MM_STANDALONE
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  VERSION_STRING                 = 1.0
> +  ENTRY_POINT                    = GenericStatusCodeStandaloneMmEntry
> +
> +#
> +# The following information is for reference only and not required by the
> build tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64
> +#
> +
> +[Sources]
> +  ReportStatusCodeRouterCommon.c
> +  ReportStatusCodeRouterCommon.h
> +  ReportStatusCodeRouterStandaloneMm.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +
> +[LibraryClasses]
> +  MmServicesTableLib
> +  StandaloneMmDriverEntryPoint
> +  DebugLib
> +  BaseLib
> +  SynchronizationLib
> +  MemoryAllocationLib
> +
> +[Protocols]
> +  gEfiMmRscHandlerProtocolGuid               ## PRODUCES
> +  gEfiMmStatusCodeProtocolGuid               ## PRODUCES
> +
> +[Depex]
> +  TRUE
> --
> 2.30.0.windows.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning
  2021-01-05 18:59 ` [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning Kun Qin
@ 2021-01-06  3:33   ` Yao, Jiewen
  0 siblings, 0 replies; 34+ messages in thread
From: Yao, Jiewen @ 2021-01-06  3:33 UTC (permalink / raw)
  To: Kun Qin, devel@edk2.groups.io
  Cc: Ard Biesheuvel, Sami Mujawar, Supreeth Venkatesh

Reviewed-by: Jiewen Yao <Jiewen.yao@intel.com>

> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>; Sami Mujawar
> <sami.mujawar@arm.com>; Yao, Jiewen <jiewen.yao@intel.com>; Supreeth
> Venkatesh <supreeth.venkatesh@arm.com>
> Subject: [PATCH v2 03/16] StandaloneMmPkg:
> StandaloneMmCoreMemoryAllocationLib: Fix compiler warning
> 
> Assigning MmramRangeCount from MmCorePrivate (UINT64) to local
> variable
> MmramRangeCount (UINT32) will cause compilation failure due to "warning
> C4244: '=': conversion from 'UINT64' to 'UINT32', possible loss of data".
> This changes defines local MmramRangeCount as UINTN type and adds type
> cast before value assignment.
> 
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Changed variable type to UINTN and cast before assignments [Jiewen]
> 
> 
> StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/Stan
> daloneMmCoreMemoryAllocationLib.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/Sta
> ndaloneMmCoreMemoryAllocationLib.c
> b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/Sta
> ndaloneMmCoreMemoryAllocationLib.c
> index eb3d772e18c5..c6feb25a3821 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/Sta
> ndaloneMmCoreMemoryAllocationLib.c
> +++
> b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/Sta
> ndaloneMmCoreMemoryAllocationLib.c
> @@ -841,7 +841,7 @@ MemoryAllocationLibConstructor (
>    VOID                            *HobStart;
>    EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
>    EFI_MMRAM_DESCRIPTOR            *MmramRanges;
> -  UINT32                           MmramRangeCount;
> +  UINTN                            MmramRangeCount;
>    EFI_HOB_GUID_TYPE               *MmramRangesHob;
> 
>    HobStart = GetHobList ();
> @@ -868,7 +868,7 @@ MemoryAllocationLibConstructor (
>        return EFI_UNSUPPORTED;
>      }
> 
> -    MmramRangeCount = MmramRangesHobData-
> >NumberOfMmReservedRegions;
> +    MmramRangeCount = (UINTN) MmramRangesHobData-
> >NumberOfMmReservedRegions;
>      if (MmramRanges == NULL) {
>        return EFI_UNSUPPORTED;
>      }
> @@ -877,7 +877,7 @@ MemoryAllocationLibConstructor (
>      DataInHob      = GET_GUID_HOB_DATA (GuidHob);
>      MmCorePrivate = (MM_CORE_PRIVATE_DATA *)(UINTN)DataInHob-
> >Address;
>      MmramRanges     = (EFI_MMRAM_DESCRIPTOR
> *)(UINTN)MmCorePrivate->MmramRanges;
> -    MmramRangeCount = MmCorePrivate->MmramRangeCount;
> +    MmramRangeCount = (UINTN) MmCorePrivate->MmramRangeCount;
>    }
> 
>    {
> --
> 2.30.0.windows.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture
  2021-01-05 18:59 ` [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Kun Qin
@ 2021-01-06  3:38   ` Yao, Jiewen
  2021-01-06  6:37     ` [edk2-devel] " Kun Qin
  0 siblings, 1 reply; 34+ messages in thread
From: Yao, Jiewen @ 2021-01-06  3:38 UTC (permalink / raw)
  To: Kun Qin, devel@edk2.groups.io
  Cc: Ard Biesheuvel, Sami Mujawar, Supreeth Venkatesh

Thanks Qin.
A quick question: May I know what test you have run for this change?

Also, I think this patch only protect the MM memory, but not OS memory. Is that expected?
Will you consider adding OS memory protection later?


> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>; Sami Mujawar
> <sami.mujawar@arm.com>; Yao, Jiewen <jiewen.yao@intel.com>; Supreeth
> Venkatesh <supreeth.venkatesh@arm.com>
> Subject: [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib:
> Extends support for X64 architecture
> 
> This change extends StandaloneMmMemLib library to support X64
> architecture. The implementation is ported from
> MdePkg/Library/SmmMemLib.
> 
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Added routine to fix bug of not initializing MmRanges [Jiewen]
>     - Extends support to x86 instead of x64 only [Hao]
> 
> 
> StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneM
> mMemLibInternal.c |  26 ++++
> 
> StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib
> .c                 |  52 +++++++
> 
> StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMe
> mLibInternal.c      | 155 ++++++++++++++++++++
> 
> StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib
> .inf               |  13 +-
>  4 files changed, 245 insertions(+), 1 deletion(-)
> 
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> index cb7c5e677a6b..46dfce5cac86 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> @@ -40,4 +40,30 @@
> MmMemLibInternalCalculateMaximumSupportAddress (
>    DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress
> = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
>  }
> 
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  )
> +{
> +  // Not implemented for AARCH64.
> +}
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  )
> +{
> +  // Not implemented for AARCH64.
> +}
> 
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> index f82cdb3ba816..f43af2b1cc9b 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> @@ -37,6 +37,27 @@
> MmMemLibInternalCalculateMaximumSupportAddress (
>    VOID
>    );
> 
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  );
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  );
> +
>  /**
>    This function check if the buffer is valid per processor architecture and not
> overlap with MMRAM.
> 
> @@ -253,11 +274,42 @@ MemLibConstructor (
>    IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
>    )
>  {
> +  EFI_STATUS Status;
> 
>    //
>    // Calculate and save maximum support address
>    //
>    MmMemLibInternalCalculateMaximumSupportAddress ();
> 
> +  //
> +  // Initialize cached Mmram Ranges from HOB.
> +  //
> +  Status = MmMemLibInternalPopulateMmramRanges ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Destructor for the library.  free any resources for Mm Mem library
> +
> +  @param ImageHandle    The image handle of the process.
> +  @param SystemTable    The EFI System Table pointer.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemLibDestructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +
> +  //
> +  // Deinitialize cached Mmram Ranges.
> +  //
> +  MmMemLibInternalFreeMmramRanges ();
> +
>    return EFI_SUCCESS;
>  }
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmM
> emLibInternal.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMm
> MemLibInternal.c
> new file mode 100644
> index 000000000000..1a978541716a
> --- /dev/null
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMm
> MemLibInternal.c
> @@ -0,0 +1,155 @@
> +/** @file
> +  Internal ARCH Specific file of MM memory check library.
> +
> +  MM memory check library implementation. This library consumes
> MM_ACCESS_PROTOCOL
> +  to get MMRAM information. In order to use this library instance, the
> platform should produce
> +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> firmware (like MM Core
> +  and MM driver) and/or specific dedicated hardware.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +  Copyright (c) Microsoft Corporation.
> +
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +#include <PiMm.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Guid/MmCoreData.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +//
> +// Maximum support address used to check input buffer
> +//
> +extern EFI_PHYSICAL_ADDRESS
> mMmMemLibInternalMaximumSupportAddress;
> +extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> +extern UINTN                 mMmMemLibInternalMmramCount;
> +
> +/**
> +  Calculate and save the maximum support address.
> +
> +**/
> +VOID
> +MmMemLibInternalCalculateMaximumSupportAddress (
> +  VOID
> +  )
> +{
> +  VOID         *Hob;
> +  UINT32       RegEax;
> +  UINT8        PhysicalAddressBits;
> +
> +  //
> +  // Get physical address bits supported.
> +  //
> +  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
> +  if (Hob != NULL) {
> +    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
> +  } else {
> +    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> +    if (RegEax >= 0x80000008) {
> +      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
> +      PhysicalAddressBits = (UINT8) RegEax;
> +    } else {
> +      PhysicalAddressBits = 36;
> +    }
> +  }
> +  //
> +  // IA-32e paging translates 48-bit linear addresses to 52-bit physical
> addresses.
> +  //
> +  ASSERT (PhysicalAddressBits <= 52);
> +  if (PhysicalAddressBits > 48) {
> +    PhysicalAddressBits = 48;
> +  }
> +
> +  //
> +  // Save the maximum support address in one global variable
> +  //
> +  mMmMemLibInternalMaximumSupportAddress =
> (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress
> = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> +}
> +
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  )
> +{
> +  VOID                            *HobStart;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
> +  MM_CORE_PRIVATE_DATA            *MmCorePrivateData;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramDescriptors;
> +
> +  HobStart = GetHobList ();
> +  DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart));
> +
> +  //
> +  // Extract MM Core Private context from the Hob. If absent search for
> +  // a Hob containing the MMRAM ranges
> +  //
> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> +  if (GuidHob == NULL) {
> +    MmramRangesHob = GetFirstGuidHob
> (&gEfiMmPeiMmramMemoryReserveGuid);
> +    if (MmramRangesHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    if (MmramRangesHobData == NULL || MmramRangesHobData-
> >Descriptor == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    mMmMemLibInternalMmramCount = MmramRangesHobData-
> >NumberOfMmReservedRegions;
> +    MmramDescriptors = MmramRangesHobData->Descriptor;
> +  } else {
> +    DataInHob = GET_GUID_HOB_DATA (GuidHob);
> +    if (DataInHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmCorePrivateData = (MM_CORE_PRIVATE_DATA *) (UINTN)
> DataInHob->Address;
> +    if (MmCorePrivateData == NULL || MmCorePrivateData-
> >MmramRanges == 0) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    mMmMemLibInternalMmramCount = (UINTN) MmCorePrivateData-
> >MmramRangeCount;
> +    MmramDescriptors = (EFI_MMRAM_DESCRIPTOR *) (UINTN)
> MmCorePrivateData->MmramRanges;
> +  }
> +
> +  mMmMemLibInternalMmramRanges = AllocatePool
> (mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +  if (mMmMemLibInternalMmramRanges) {
> +    CopyMem (mMmMemLibInternalMmramRanges,
> +             MmramDescriptors,
> +             mMmMemLibInternalMmramCount * sizeof
> (EFI_MMRAM_DESCRIPTOR));
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  )
> +{
> +  if (mMmMemLibInternalMmramRanges != NULL) {
> +    FreePool (mMmMemLibInternalMmramRanges);
> +  }
> +}
> +
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> index 49da02e54e6d..062b0d7a1161 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> @@ -8,6 +8,7 @@
>  #
>  #  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
>  #  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
> @@ -22,16 +23,20 @@ [Defines]
>    PI_SPECIFICATION_VERSION       = 0x00010032
>    LIBRARY_CLASS                  = MemLib|MM_STANDALONE
> MM_CORE_STANDALONE
>    CONSTRUCTOR                    = MemLibConstructor
> +  DESTRUCTOR                     = MemLibDestructor
> 
>  #
>  # The following information is for reference only and not required by the
> build tools.
>  #
> -#  VALID_ARCHITECTURES           = AARCH64
> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
>  #
> 
>  [Sources.Common]
>    StandaloneMmMemLib.c
> 
> +[Sources.IA32, Sources.X64]
> +  X86StandaloneMmMemLibInternal.c
> +
>  [Sources.AARCH64]
>    AArch64/StandaloneMmMemLibInternal.c
> 
> @@ -42,3 +47,9 @@ [Packages]
>  [LibraryClasses]
>    BaseMemoryLib
>    DebugLib
> +  HobLib
> +  MemoryAllocationLib
> +
> +[Guids]
> +  gMmCoreDataHobGuid                  ## SOMETIMES_CONSUMES ## HOB
> +  gEfiMmPeiMmramMemoryReserveGuid     ## SOMETIMES_CONSUMES
> ## HOB
> --
> 2.30.0.windows.1


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture
  2021-01-06  3:38   ` Yao, Jiewen
@ 2021-01-06  6:37     ` Kun Qin
  0 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-06  6:37 UTC (permalink / raw)
  To: devel@edk2.groups.io, jiewen.yao@intel.com
  Cc: Ard Biesheuvel, Sami Mujawar, Supreeth Venkatesh

[-- Attachment #1: Type: text/plain, Size: 13626 bytes --]

Hi Jiewen,

Before introducing the MmRange initialization logic, I was hitting a bug caused by `MmIsBufferOutsideMmValid` since it returns TRUE regardless of what I passed in. After the patch, this function start to return FALSE when I verify with regions overlapping with MmRanges.

As per OS memory protection, I guess you mean the logic around block everything but runtime, ACPI, and reserved memory after ready to lock? I think we should bring in those logic at some point. But as of now, that routine would need information on DXE memory map, which needs extra efforts (such as a designated handler) to fetch under standalone MM environment.

On the other hand, one proposal is for standalone MM, only MmRam and regions requested through a specific protocol/handler are allowed. So that we can implement this MmMemLib from a different approach. We can discuss more about this proposal separately if you would like to.

Thanks,
Kun

From: Yao, Jiewen<mailto:jiewen.yao@intel.com>
Sent: Tuesday, January 5, 2021 19:38
To: Kun Qin<mailto:kun.q@outlook.com>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
Cc: Ard Biesheuvel<mailto:ard.biesheuvel@arm.com>; Sami Mujawar<mailto:sami.mujawar@arm.com>; Supreeth Venkatesh<mailto:supreeth.venkatesh@arm.com>
Subject: Re: [edk2-devel] [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture

Thanks Qin.
A quick question: May I know what test you have run for this change?

Also, I think this patch only protect the MM memory, but not OS memory. Is that expected?
Will you consider adding OS memory protection later?


> -----Original Message-----
> From: Kun Qin <kun.q@outlook.com>
> Sent: Wednesday, January 6, 2021 2:59 AM
> To: devel@edk2.groups.io
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>; Sami Mujawar
> <sami.mujawar@arm.com>; Yao, Jiewen <jiewen.yao@intel.com>; Supreeth
> Venkatesh <supreeth.venkatesh@arm.com>
> Subject: [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib:
> Extends support for X64 architecture
>
> This change extends StandaloneMmMemLib library to support X64
> architecture. The implementation is ported from
> MdePkg/Library/SmmMemLib.
>
> Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
>
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
>
> Notes:
>     v2:
>     - Added routine to fix bug of not initializing MmRanges [Jiewen]
>     - Extends support to x86 instead of x64 only [Hao]
>
>
> StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneM
> mMemLibInternal.c |  26 ++++
>
> StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib
> .c                 |  52 +++++++
>
> StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMe
> mLibInternal.c      | 155 ++++++++++++++++++++
>
> StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib
> .inf               |  13 +-
>  4 files changed, 245 insertions(+), 1 deletion(-)
>
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> index cb7c5e677a6b..46dfce5cac86 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/Standalone
> MmMemLibInternal.c
> @@ -40,4 +40,30 @@
> MmMemLibInternalCalculateMaximumSupportAddress (
>    DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress
> = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
>  }
>
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  )
> +{
> +  // Not implemented for AARCH64.
> +}
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  )
> +{
> +  // Not implemented for AARCH64.
> +}
>
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> index f82cdb3ba816..f43af2b1cc9b 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.c
> @@ -37,6 +37,27 @@
> MmMemLibInternalCalculateMaximumSupportAddress (
>    VOID
>    );
>
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  );
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  );
> +
>  /**
>    This function check if the buffer is valid per processor architecture and not
> overlap with MMRAM.
>
> @@ -253,11 +274,42 @@ MemLibConstructor (
>    IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
>    )
>  {
> +  EFI_STATUS Status;
>
>    //
>    // Calculate and save maximum support address
>    //
>    MmMemLibInternalCalculateMaximumSupportAddress ();
>
> +  //
> +  // Initialize cached Mmram Ranges from HOB.
> +  //
> +  Status = MmMemLibInternalPopulateMmramRanges ();
> +
> +  return Status;
> +}
> +
> +/**
> +  Destructor for the library.  free any resources for Mm Mem library
> +
> +  @param ImageHandle    The image handle of the process.
> +  @param SystemTable    The EFI System Table pointer.
> +
> +  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +MemLibDestructor (
> +  IN EFI_HANDLE             ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable
> +  )
> +{
> +
> +  //
> +  // Deinitialize cached Mmram Ranges.
> +  //
> +  MmMemLibInternalFreeMmramRanges ();
> +
>    return EFI_SUCCESS;
>  }
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmM
> emLibInternal.c
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMm
> MemLibInternal.c
> new file mode 100644
> index 000000000000..1a978541716a
> --- /dev/null
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMm
> MemLibInternal.c
> @@ -0,0 +1,155 @@
> +/** @file
> +  Internal ARCH Specific file of MM memory check library.
> +
> +  MM memory check library implementation. This library consumes
> MM_ACCESS_PROTOCOL
> +  to get MMRAM information. In order to use this library instance, the
> platform should produce
> +  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for
> firmware (like MM Core
> +  and MM driver) and/or specific dedicated hardware.
> +
> +  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +  Copyright (c) Microsoft Corporation.
> +
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +#include <PiMm.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/MemoryAllocationLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/HobLib.h>
> +
> +#include <Guid/MmCoreData.h>
> +#include <Guid/MmramMemoryReserve.h>
> +
> +//
> +// Maximum support address used to check input buffer
> +//
> +extern EFI_PHYSICAL_ADDRESS
> mMmMemLibInternalMaximumSupportAddress;
> +extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
> +extern UINTN                 mMmMemLibInternalMmramCount;
> +
> +/**
> +  Calculate and save the maximum support address.
> +
> +**/
> +VOID
> +MmMemLibInternalCalculateMaximumSupportAddress (
> +  VOID
> +  )
> +{
> +  VOID         *Hob;
> +  UINT32       RegEax;
> +  UINT8        PhysicalAddressBits;
> +
> +  //
> +  // Get physical address bits supported.
> +  //
> +  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
> +  if (Hob != NULL) {
> +    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
> +  } else {
> +    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> +    if (RegEax >= 0x80000008) {
> +      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
> +      PhysicalAddressBits = (UINT8) RegEax;
> +    } else {
> +      PhysicalAddressBits = 36;
> +    }
> +  }
> +  //
> +  // IA-32e paging translates 48-bit linear addresses to 52-bit physical
> addresses.
> +  //
> +  ASSERT (PhysicalAddressBits <= 52);
> +  if (PhysicalAddressBits > 48) {
> +    PhysicalAddressBits = 48;
> +  }
> +
> +  //
> +  // Save the maximum support address in one global variable
> +  //
> +  mMmMemLibInternalMaximumSupportAddress =
> (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);
> +  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress
> = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));
> +}
> +
> +/**
> +  Initialize cached Mmram Ranges from HOB.
> +
> +  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM
> information.
> +  @retval EFI_SUCCESS       MmRanges are populated successfully.
> +
> +**/
> +EFI_STATUS
> +MmMemLibInternalPopulateMmramRanges (
> +  VOID
> +  )
> +{
> +  VOID                            *HobStart;
> +  EFI_HOB_GUID_TYPE               *GuidHob;
> +  MM_CORE_DATA_HOB_DATA           *DataInHob;
> +  MM_CORE_PRIVATE_DATA            *MmCorePrivateData;
> +  EFI_HOB_GUID_TYPE               *MmramRangesHob;
> +  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;
> +  EFI_MMRAM_DESCRIPTOR            *MmramDescriptors;
> +
> +  HobStart = GetHobList ();
> +  DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart));
> +
> +  //
> +  // Extract MM Core Private context from the Hob. If absent search for
> +  // a Hob containing the MMRAM ranges
> +  //
> +  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);
> +  if (GuidHob == NULL) {
> +    MmramRangesHob = GetFirstGuidHob
> (&gEfiMmPeiMmramMemoryReserveGuid);
> +    if (MmramRangesHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);
> +    if (MmramRangesHobData == NULL || MmramRangesHobData-
> >Descriptor == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    mMmMemLibInternalMmramCount = MmramRangesHobData-
> >NumberOfMmReservedRegions;
> +    MmramDescriptors = MmramRangesHobData->Descriptor;
> +  } else {
> +    DataInHob = GET_GUID_HOB_DATA (GuidHob);
> +    if (DataInHob == NULL) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    MmCorePrivateData = (MM_CORE_PRIVATE_DATA *) (UINTN)
> DataInHob->Address;
> +    if (MmCorePrivateData == NULL || MmCorePrivateData-
> >MmramRanges == 0) {
> +      return EFI_UNSUPPORTED;
> +    }
> +
> +    mMmMemLibInternalMmramCount = (UINTN) MmCorePrivateData-
> >MmramRangeCount;
> +    MmramDescriptors = (EFI_MMRAM_DESCRIPTOR *) (UINTN)
> MmCorePrivateData->MmramRanges;
> +  }
> +
> +  mMmMemLibInternalMmramRanges = AllocatePool
> (mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));
> +  if (mMmMemLibInternalMmramRanges) {
> +    CopyMem (mMmMemLibInternalMmramRanges,
> +             MmramDescriptors,
> +             mMmMemLibInternalMmramCount * sizeof
> (EFI_MMRAM_DESCRIPTOR));
> +  }
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Deinitialize cached Mmram Ranges.
> +
> +**/
> +VOID
> +MmMemLibInternalFreeMmramRanges (
> +  VOID
> +  )
> +{
> +  if (mMmMemLibInternalMmramRanges != NULL) {
> +    FreePool (mMmMemLibInternalMmramRanges);
> +  }
> +}
> +
> diff --git
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> index 49da02e54e6d..062b0d7a1161 100644
> ---
> a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> +++
> b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMem
> Lib.inf
> @@ -8,6 +8,7 @@
>  #
>  #  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
>  #  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
> @@ -22,16 +23,20 @@ [Defines]
>    PI_SPECIFICATION_VERSION       = 0x00010032
>    LIBRARY_CLASS                  = MemLib|MM_STANDALONE
> MM_CORE_STANDALONE
>    CONSTRUCTOR                    = MemLibConstructor
> +  DESTRUCTOR                     = MemLibDestructor
>
>  #
>  # The following information is for reference only and not required by the
> build tools.
>  #
> -#  VALID_ARCHITECTURES           = AARCH64
> +#  VALID_ARCHITECTURES           = IA32 X64 AARCH64
>  #
>
>  [Sources.Common]
>    StandaloneMmMemLib.c
>
> +[Sources.IA32, Sources.X64]
> +  X86StandaloneMmMemLibInternal.c
> +
>  [Sources.AARCH64]
>    AArch64/StandaloneMmMemLibInternal.c
>
> @@ -42,3 +47,9 @@ [Packages]
>  [LibraryClasses]
>    BaseMemoryLib
>    DebugLib
> +  HobLib
> +  MemoryAllocationLib
> +
> +[Guids]
> +  gMmCoreDataHobGuid                  ## SOMETIMES_CONSUMES ## HOB
> +  gEfiMmPeiMmramMemoryReserveGuid     ## SOMETIMES_CONSUMES
> ## HOB
> --
> 2.30.0.windows.1







[-- Attachment #2: Type: text/html, Size: 22084 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
  2021-01-05 18:59 ` [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm Kun Qin
@ 2021-01-06 15:46   ` Laszlo Ersek
  2021-01-06 15:51     ` Laszlo Ersek
  2021-01-07 19:05     ` Leif Lindholm
  0 siblings, 2 replies; 34+ messages in thread
From: Laszlo Ersek @ 2021-01-06 15:46 UTC (permalink / raw)
  To: Kun Qin, devel
  Cc: Eric Dong, Ray Ni, Rahul Kumar, Michael Kinney,
	Leif Lindholm (Nuvia address), Ard Biesheuvel (ARM address)

On 01/05/21 19:59, Kun Qin wrote:
> This change added support of CpuIo driver under StandaloneMm. The core
> services switched to gMmst based instead of gMmst. It also abstracts

(1) typo: "gMmst based instead of gMmst" -- that's the same thing

(2) renames like this should not be mixed with logic changes /
refactorings, so this would be two patches minimally

(3) in fact I disagree with renaming gSmst to gMmst (and the resultant
fallout). I see very little value in this, it only complicates the git
history, takes up reviewer resources, and risks regressions. Unless
absolutely necessary for getting the driver build in the new context, I
would strongly advise against the rename. If the rename is unavoidable,
then see (2) please -- do it in a separate patch.

(4) If I understand correctly, this is the first time when UefiCpuPkg
content is consumed in AARCH64 platforms. I wonder if that means that
the CpuIo2Smm should no longer exist under UefiCpuPkg at all. UefiCpuPkg
has been traditionally IA32/X64 only, and while the *name* of the
package certainly does not require or imply that, over time those two
things *have* become synonymous. For example, the SMM Core lives under
MdeModulePkg; I wonder if the same should happen to CpuIo2Smm as well.
Adding Ard, Leif and Mike.

Thanks
Laszlo

> standalone and traditional MM driver entrypoints into separate files to
> allow maximal common implementations.
> 
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Removed "EFIAPI" for internal functions.
> 
>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.c => CpuIo2Common.c}           |  15 +-
>  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c                               | 384 +-------------------
>  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c                      |  31 ++
>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.h => CpuIo2Common.h}           |  17 +-
>  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf                             |   7 +-
>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.inf => CpuIo2StandaloneMm.inf} |  23 +-
>  UefiCpuPkg/UefiCpuPkg.dsc                                      |   6 +
>  7 files changed, 75 insertions(+), 408 deletions(-)
> 
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> similarity index 94%
> copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> copy to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> index b840d3e10cae..fef434795908 100644
> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> @@ -6,7 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
> -#include "CpuIo2Smm.h"
> +#include "CpuIo2Common.h"
>  
>  //
>  // Handle for the SMM CPU I/O Protocol
> @@ -371,18 +371,13 @@ CpuIoServiceWrite (
>  /**
>    The module Entry Point SmmCpuIoProtocol driver
>  
> -  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
> -  @param[in] SystemTable  A pointer to the EFI System Table.
> -
>    @retval EFI_SUCCESS  The entry point is executed successfully.
>    @retval Other        Some error occurs when executing this entry point.
>  
>  **/
>  EFI_STATUS
> -EFIAPI
> -SmmCpuIo2Initialize (
> -  IN EFI_HANDLE        ImageHandle,
> -  IN EFI_SYSTEM_TABLE  *SystemTable
> +CommonCpuIo2Initialize (
> +  VOID
>    )
>  {
>    EFI_STATUS  Status;
> @@ -390,12 +385,12 @@ SmmCpuIo2Initialize (
>    //
>    // Copy the SMM CPU I/O Protocol instance into the System Management System Table
>    //
> -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
> +  CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
>  
>    //
>    // Install the SMM CPU I/O Protocol into the SMM protocol database
>    //
> -  Status = gSmst->SmmInstallProtocolInterface (
> +  Status = gMmst->MmInstallProtocolInterface (
>                      &mHandle,
>                      &gEfiSmmCpuIo2ProtocolGuid,
>                      EFI_NATIVE_INTERFACE,
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> index b840d3e10cae..062d1a47a09f 100644
> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> @@ -6,367 +6,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
> -#include "CpuIo2Smm.h"
> +#include <PiSmm.h>
>  
> -//
> -// Handle for the SMM CPU I/O Protocol
> -//
> -EFI_HANDLE  mHandle = NULL;
> -
> -//
> -// SMM CPU I/O Protocol instance
> -//
> -EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {
> -  {
> -    CpuMemoryServiceRead,
> -    CpuMemoryServiceWrite
> -  },
> -  {
> -    CpuIoServiceRead,
> -    CpuIoServiceWrite
> -  }
> -};
> -
> -//
> -// Lookup table for increment values based on transfer widths
> -//
> -UINT8 mStride[] = {
> -  1, // SMM_IO_UINT8
> -  2, // SMM_IO_UINT16
> -  4, // SMM_IO_UINT32
> -  8  // SMM_IO_UINT64
> -};
> -
> -/**
> -  Check parameters to a SMM CPU I/O Protocol service request.
> -
> -  @param[in]  MmioOperation  TRUE for an MMIO operation, FALSE for I/O Port operation.
> -  @param[in]  Width          Signifies the width of the I/O operations.
> -  @param[in]  Address        The base address of the I/O operations.  The caller is
> -                             responsible for aligning the Address if required.
> -  @param[in]  Count          The number of I/O operations to perform.
> -  @param[in]  Buffer         For read operations, the destination buffer to store
> -                             the results.  For write operations, the source buffer
> -                             from which to write data.
> -
> -  @retval EFI_SUCCESS            The data was read from or written to the device.
> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> -
> -**/
> -EFI_STATUS
> -CpuIoCheckParameter (
> -  IN BOOLEAN           MmioOperation,
> -  IN EFI_SMM_IO_WIDTH  Width,
> -  IN UINT64            Address,
> -  IN UINTN             Count,
> -  IN VOID              *Buffer
> -  )
> -{
> -  UINT64  MaxCount;
> -  UINT64  Limit;
> -
> -  //
> -  // Check to see if Buffer is NULL
> -  //
> -  if (Buffer == NULL) {
> -    return EFI_INVALID_PARAMETER;
> -  }
> -
> -  //
> -  // Check to see if Width is in the valid range
> -  //
> -  if ((UINT32)Width > SMM_IO_UINT64) {
> -    return EFI_INVALID_PARAMETER;
> -  }
> -
> -  //
> -  // Check to see if Width is in the valid range for I/O Port operations
> -  //
> -  if (!MmioOperation && (Width == SMM_IO_UINT64)) {
> -    return EFI_INVALID_PARAMETER;
> -  }
> -
> -  //
> -  // Check to see if any address associated with this transfer exceeds the maximum
> -  // allowed address.  The maximum address implied by the parameters passed in is
> -  // Address + Size * Count.  If the following condition is met, then the transfer
> -  // is not supported.
> -  //
> -  //    Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
> -  //
> -  // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
> -  // can also be the maximum integer value supported by the CPU, this range
> -  // check must be adjusted to avoid all overflow conditions.
> -  //
> -  // The following form of the range check is equivalent but assumes that
> -  // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
> -  //
> -  Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);
> -  if (Count == 0) {
> -    if (Address > Limit) {
> -      return EFI_UNSUPPORTED;
> -    }
> -  } else {
> -    MaxCount = RShiftU64 (Limit, Width);
> -    if (MaxCount < (Count - 1)) {
> -      return EFI_UNSUPPORTED;
> -    }
> -    if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
> -      return EFI_UNSUPPORTED;
> -    }
> -  }
> -
> -  //
> -  // Check to see if Address is aligned
> -  //
> -  if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {
> -    return EFI_UNSUPPORTED;
> -  }
> -
> -  return EFI_SUCCESS;
> -}
> -
> -/**
> -  Reads memory-mapped registers.
> -
> -  The I/O operations are carried out exactly as requested.  The caller is
> -  responsible for any alignment and I/O width issues that the bus, device,
> -  platform, or type of I/O might require.
> -
> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> -  @param[in]  Width    Signifies the width of the I/O operations.
> -  @param[in]  Address  The base address of the I/O operations.  The caller is
> -                       responsible for aligning the Address if required.
> -  @param[in]  Count    The number of I/O operations to perform.
> -  @param[out] Buffer   For read operations, the destination buffer to store
> -                       the results.  For write operations, the source buffer
> -                       from which to write data.
> -
> -  @retval EFI_SUCCESS            The data was read from or written to the device.
> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> -                                 lack of resources
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -CpuMemoryServiceRead (
> -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> -  IN  EFI_SMM_IO_WIDTH                Width,
> -  IN  UINT64                          Address,
> -  IN  UINTN                           Count,
> -  OUT VOID                            *Buffer
> -  )
> -{
> -  EFI_STATUS  Status;
> -  UINT8       Stride;
> -  UINT8       *Uint8Buffer;
> -
> -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
> -  if (EFI_ERROR (Status)) {
> -    return Status;
> -  }
> -
> -  //
> -  // Select loop based on the width of the transfer
> -  //
> -  Stride = mStride[Width];
> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> -    if (Width == SMM_IO_UINT8) {
> -      *Uint8Buffer = MmioRead8 ((UINTN)Address);
> -    } else if (Width == SMM_IO_UINT16) {
> -      *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
> -    } else if (Width == SMM_IO_UINT32) {
> -      *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
> -    } else if (Width == SMM_IO_UINT64) {
> -      *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
> -    }
> -  }
> -  return EFI_SUCCESS;
> -}
> -
> -/**
> -  Writes memory-mapped registers.
> -
> -  The I/O operations are carried out exactly as requested.  The caller is
> -  responsible for any alignment and I/O width issues that the bus, device,
> -  platform, or type of I/O might require.
> -
> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> -  @param[in]  Width    Signifies the width of the I/O operations.
> -  @param[in]  Address  The base address of the I/O operations.  The caller is
> -                       responsible for aligning the Address if required.
> -  @param[in]  Count    The number of I/O operations to perform.
> -  @param[in]  Buffer   For read operations, the destination buffer to store
> -                       the results.  For write operations, the source buffer
> -                       from which to write data.
> -
> -  @retval EFI_SUCCESS            The data was read from or written to the device.
> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> -                                 lack of resources
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -CpuMemoryServiceWrite (
> -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> -  IN EFI_SMM_IO_WIDTH                Width,
> -  IN UINT64                          Address,
> -  IN UINTN                           Count,
> -  IN VOID                            *Buffer
> -  )
> -{
> -  EFI_STATUS  Status;
> -  UINT8       Stride;
> -  UINT8       *Uint8Buffer;
> -
> -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
> -  if (EFI_ERROR (Status)) {
> -    return Status;
> -  }
> -
> -  //
> -  // Select loop based on the width of the transfer
> -  //
> -  Stride = mStride[Width];
> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> -    if (Width == SMM_IO_UINT8) {
> -      MmioWrite8 ((UINTN)Address, *Uint8Buffer);
> -    } else if (Width == SMM_IO_UINT16) {
> -      MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
> -    } else if (Width == SMM_IO_UINT32) {
> -      MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
> -    } else if (Width == SMM_IO_UINT64) {
> -      MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
> -    }
> -  }
> -  return EFI_SUCCESS;
> -}
> -
> -/**
> -  Reads I/O registers.
> -
> -  The I/O operations are carried out exactly as requested.  The caller is
> -  responsible for any alignment and I/O width issues that the bus, device,
> -  platform, or type of I/O might require.
> -
> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> -  @param[in]  Width    Signifies the width of the I/O operations.
> -  @param[in]  Address  The base address of the I/O operations.  The caller is
> -                       responsible for aligning the Address if required.
> -  @param[in]  Count    The number of I/O operations to perform.
> -  @param[out] Buffer   For read operations, the destination buffer to store
> -                       the results.  For write operations, the source buffer
> -                       from which to write data.
> -
> -  @retval EFI_SUCCESS            The data was read from or written to the device.
> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> -                                 lack of resources
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -CpuIoServiceRead (
> -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> -  IN  EFI_SMM_IO_WIDTH                Width,
> -  IN  UINT64                          Address,
> -  IN  UINTN                           Count,
> -  OUT VOID                            *Buffer
> -  )
> -{
> -  EFI_STATUS  Status;
> -  UINT8       Stride;
> -  UINT8       *Uint8Buffer;
> -
> -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
> -  if (EFI_ERROR (Status)) {
> -    return Status;
> -  }
> -
> -  //
> -  // Select loop based on the width of the transfer
> -  //
> -  Stride = mStride[Width];
> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> -    if (Width == SMM_IO_UINT8) {
> -      *Uint8Buffer = IoRead8 ((UINTN)Address);
> -    } else if (Width == SMM_IO_UINT16) {
> -      *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);
> -    } else if (Width == SMM_IO_UINT32) {
> -      *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);
> -    }
> -  }
> -
> -  return EFI_SUCCESS;
> -}
> -
> -/**
> -  Write I/O registers.
> -
> -  The I/O operations are carried out exactly as requested.  The caller is
> -  responsible for any alignment and I/O width issues that the bus, device,
> -  platform, or type of I/O might require.
> -
> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> -  @param[in]  Width    Signifies the width of the I/O operations.
> -  @param[in]  Address  The base address of the I/O operations.  The caller is
> -                       responsible for aligning the Address if required.
> -  @param[in]  Count    The number of I/O operations to perform.
> -  @param[in]  Buffer   For read operations, the destination buffer to store
> -                       the results.  For write operations, the source buffer
> -                       from which to write data.
> -
> -  @retval EFI_SUCCESS            The data was read from or written to the device.
> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> -                                 lack of resources
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -CpuIoServiceWrite (
> -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> -  IN EFI_SMM_IO_WIDTH                Width,
> -  IN UINT64                          Address,
> -  IN UINTN                           Count,
> -  IN VOID                            *Buffer
> -  )
> -{
> -  EFI_STATUS  Status;
> -  UINT8       Stride;
> -  UINT8       *Uint8Buffer;
> -
> -  //
> -  // Make sure the parameters are valid
> -  //
> -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
> -  if (EFI_ERROR (Status)) {
> -    return Status;
> -  }
> -
> -  //
> -  // Select loop based on the width of the transfer
> -  //
> -  Stride = mStride[Width];
> -  for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> -    if (Width == SMM_IO_UINT8) {
> -      IoWrite8 ((UINTN)Address, *Uint8Buffer);
> -    } else if (Width == SMM_IO_UINT16) {
> -      IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
> -    } else if (Width == SMM_IO_UINT32) {
> -      IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
> -    }
> -  }
> -
> -  return EFI_SUCCESS;
> -}
> +#include "CpuIo2Common.h"
>  
>  /**
>    The module Entry Point SmmCpuIoProtocol driver
> @@ -380,28 +22,10 @@ CpuIoServiceWrite (
>  **/
>  EFI_STATUS
>  EFIAPI
> -SmmCpuIo2Initialize (
> +TraditionalMmCpuIo2Initialize (
>    IN EFI_HANDLE        ImageHandle,
>    IN EFI_SYSTEM_TABLE  *SystemTable
>    )
>  {
> -  EFI_STATUS  Status;
> -
> -  //
> -  // Copy the SMM CPU I/O Protocol instance into the System Management System Table
> -  //
> -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
> -
> -  //
> -  // Install the SMM CPU I/O Protocol into the SMM protocol database
> -  //
> -  Status = gSmst->SmmInstallProtocolInterface (
> -                    &mHandle,
> -                    &gEfiSmmCpuIo2ProtocolGuid,
> -                    EFI_NATIVE_INTERFACE,
> -                    &mSmmCpuIo2
> -                    );
> -  ASSERT_EFI_ERROR (Status);
> -
> -  return Status;
> +  return CommonCpuIo2Initialize ();
>  }
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
> new file mode 100644
> index 000000000000..2978f82eb815
> --- /dev/null
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
> @@ -0,0 +1,31 @@
> +/** @file
> +  Produces the SMM CPU I/O Protocol.
> +
> +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include "CpuIo2Common.h"
> +
> +/**
> +  The module Entry Point SmmCpuIoProtocol driver
> +
> +  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
> +  @param[in] SystemTable  A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS  The entry point is executed successfully.
> +  @retval Other        Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StandaloneMmCpuIo2Initialize (
> +  IN EFI_HANDLE           ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  return CommonCpuIo2Initialize ();
> +}
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> similarity index 92%
> rename from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
> rename to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> index 4c133b58c9f4..6759cc1c8777 100644
> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> @@ -9,14 +9,14 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #ifndef _CPU_IO2_SMM_H_
>  #define _CPU_IO2_SMM_H_
>  
> -#include <PiSmm.h>
> +#include <PiMm.h>
>  
>  #include <Protocol/SmmCpuIo2.h>
>  
>  #include <Library/BaseLib.h>
>  #include <Library/DebugLib.h>
>  #include <Library/IoLib.h>
> -#include <Library/SmmServicesTableLib.h>
> +#include <Library/MmServicesTableLib.h>
>  #include <Library/BaseMemoryLib.h>
>  
>  #define MAX_IO_PORT_ADDRESS   0xFFFF
> @@ -153,4 +153,17 @@ CpuIoServiceWrite (
>    IN VOID                            *Buffer
>    );
>  
> +
> +/**
> +  The module Entry Point SmmCpuIoProtocol driver
> +
> +  @retval EFI_SUCCESS  The entry point is executed successfully.
> +  @retval Other        Some error occurs when executing this entry point.
> +
> +**/
> +EFI_STATUS
> +CommonCpuIo2Initialize (
> +  VOID
> +  );
> +
>  #endif
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> index bc78fa4e42d2..031db1d100b9 100644
> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> @@ -14,7 +14,7 @@ [Defines]
>    MODULE_TYPE                    = DXE_SMM_DRIVER
>    VERSION_STRING                 = 1.0
>    PI_SPECIFICATION_VERSION       = 0x0001000A
> -  ENTRY_POINT                    = SmmCpuIo2Initialize
> +  ENTRY_POINT                    = TraditionalMmCpuIo2Initialize
>  
>  #
>  # The following information is for reference only and not required by the build tools.
> @@ -24,7 +24,8 @@ [Defines]
>  
>  [Sources]
>    CpuIo2Smm.c
> -  CpuIo2Smm.h
> +  CpuIo2Common.c
> +  CpuIo2Common.h
>  
>  [Packages]
>    MdePkg/MdePkg.dec
> @@ -34,7 +35,7 @@ [LibraryClasses]
>    BaseLib
>    DebugLib
>    IoLib
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    BaseMemoryLib
>  
>  [Protocols]
> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> similarity index 56%
> copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> copy to UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> index bc78fa4e42d2..cce7742bee7a 100644
> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> @@ -8,13 +8,12 @@
>  
>  [Defines]
>    INF_VERSION                    = 0x00010005
> -  BASE_NAME                      = CpuIo2Smm
> -  MODULE_UNI_FILE                = CpuIo2Smm.uni
> -  FILE_GUID                      = A47EE2D8-F60E-42fd-8E58-7BD65EE4C29B
> -  MODULE_TYPE                    = DXE_SMM_DRIVER
> +  BASE_NAME                      = CpuIo2StandaloneMm
> +  FILE_GUID                      = E3121A26-BB1C-4A18-8E23-2EA3F0412248
> +  MODULE_TYPE                    = MM_STANDALONE
>    VERSION_STRING                 = 1.0
> -  PI_SPECIFICATION_VERSION       = 0x0001000A
> -  ENTRY_POINT                    = SmmCpuIo2Initialize
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = StandaloneMmCpuIo2Initialize
>  
>  #
>  # The following information is for reference only and not required by the build tools.
> @@ -23,18 +22,19 @@ [Defines]
>  #
>  
>  [Sources]
> -  CpuIo2Smm.c
> -  CpuIo2Smm.h
> +  CpuIo2StandaloneMm.c
> +  CpuIo2Common.c
> +  CpuIo2Common.h
>  
>  [Packages]
>    MdePkg/MdePkg.dec
>  
>  [LibraryClasses]
> -  UefiDriverEntryPoint
> +  StandaloneMmDriverEntryPoint
>    BaseLib
>    DebugLib
>    IoLib
> -  SmmServicesTableLib
> +  MmServicesTableLib
>    BaseMemoryLib
>  
>  [Protocols]
> @@ -42,6 +42,3 @@ [Protocols]
>  
>  [Depex]
>    TRUE
> -
> -[UserExtensions.TianoCore."ExtraFiles"]
> -  CpuIo2SmmExtra.uni
> diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
> index b2b6d78a71b0..4e46c600788f 100644
> --- a/UefiCpuPkg/UefiCpuPkg.dsc
> +++ b/UefiCpuPkg/UefiCpuPkg.dsc
> @@ -38,6 +38,7 @@ [LibraryClasses]
>    UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
>    UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
>    UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
> +  StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
>    DxeServicesTableLib|MdePkg/Library/DxeServicesTableLib/DxeServicesTableLib.inf
>    PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
>    PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
> @@ -89,10 +90,14 @@ [LibraryClasses.common.DXE_DRIVER]
>  
>  [LibraryClasses.common.DXE_SMM_DRIVER]
>    SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
> +  MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
>    MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
>    HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
>    CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
>  
> +[LibraryClasses.common.MM_STANDALONE]
> +  MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
> +
>  [LibraryClasses.common.UEFI_APPLICATION]
>    UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
>    MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
> @@ -121,6 +126,7 @@ [Components.IA32, Components.X64]
>        NULL|UefiCpuPkg/Library/CpuCommonFeaturesLib/CpuCommonFeaturesLib.inf
>    }
>    UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> +  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
>    UefiCpuPkg/CpuMpPei/CpuMpPei.inf
>    UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
>    UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
> 


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 16/16] UefiCpuPkg: SmmCpuExceptionHandlerLib: Added StandaloneMm module support
  2021-01-05 18:59 ` [PATCH v2 16/16] UefiCpuPkg: SmmCpuExceptionHandlerLib: Added StandaloneMm module support Kun Qin
@ 2021-01-06 15:48   ` Laszlo Ersek
  0 siblings, 0 replies; 34+ messages in thread
From: Laszlo Ersek @ 2021-01-06 15:48 UTC (permalink / raw)
  To: Kun Qin, devel; +Cc: Eric Dong, Ray Ni, Rahul Kumar

On 01/05/21 19:59, Kun Qin wrote:
> This change of SmmCpuExceptionHandlerLib adds support for StandaloneMm
> components to allow x64 StandaloneMm environment setting up exception
> handlers.
> 
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - No review, no change.
> 
>  UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
> index 4cdb11c04ea0..ea5b10b5c8e4 100644
> --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
> +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
> @@ -13,7 +13,7 @@ [Defines]
>    FILE_GUID                      = 8D2C439B-3981-42ff-9CE5-1B50ECA502D6
>    MODULE_TYPE                    = DXE_SMM_DRIVER
>    VERSION_STRING                 = 1.1
> -  LIBRARY_CLASS                  = CpuExceptionHandlerLib|DXE_SMM_DRIVER
> +  LIBRARY_CLASS                  = CpuExceptionHandlerLib|DXE_SMM_DRIVER MM_STANDALONE MM_CORE_STANDALONE
>  
>  #
>  # The following information is for reference only and not required by the build tools.
> 

Looks innocent enough to me.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
  2021-01-06 15:46   ` Laszlo Ersek
@ 2021-01-06 15:51     ` Laszlo Ersek
  2021-01-06 19:02       ` [edk2-devel] " Kun Qin
  2021-01-07 19:05     ` Leif Lindholm
  1 sibling, 1 reply; 34+ messages in thread
From: Laszlo Ersek @ 2021-01-06 15:51 UTC (permalink / raw)
  To: Kun Qin, devel
  Cc: Eric Dong, Ray Ni, Rahul Kumar, Michael Kinney,
	Leif Lindholm (Nuvia address), Ard Biesheuvel (ARM address)

On 01/06/21 16:46, Laszlo Ersek wrote:
> On 01/05/21 19:59, Kun Qin wrote:
>> This change added support of CpuIo driver under StandaloneMm. The core
>> services switched to gMmst based instead of gMmst. It also abstracts
> 
> (1) typo: "gMmst based instead of gMmst" -- that's the same thing
> 
> (2) renames like this should not be mixed with logic changes /
> refactorings, so this would be two patches minimally
> 
> (3) in fact I disagree with renaming gSmst to gMmst (and the resultant
> fallout). I see very little value in this, it only complicates the git
> history, takes up reviewer resources, and risks regressions. Unless
> absolutely necessary for getting the driver build in the new context, I
> would strongly advise against the rename. If the rename is unavoidable,
> then see (2) please -- do it in a separate patch.
> 
> (4) If I understand correctly, this is the first time when UefiCpuPkg
> content is consumed in AARCH64 platforms. I wonder if that means that
> the CpuIo2Smm should no longer exist under UefiCpuPkg at all. UefiCpuPkg
> has been traditionally IA32/X64 only, and while the *name* of the
> package certainly does not require or imply that, over time those two
> things *have* become synonymous. For example, the SMM Core lives under
> MdeModulePkg; I wonder if the same should happen to CpuIo2Smm as well.
> Adding Ard, Leif and Mike.

You messed up the threading between the blurb

  [edk2-devel] [PATCH v2 00/16]
  Extends Support of MM_STANDALONE Type Modules to X64

and this patch, and so I missed that the context was strictly X64.

Therefore, please ignore point (4) above.

In turn, I feel this only strengthens my point (3). If Ray or Eric can
review these renames (as a separate patch!), then I guess I'm OK with
them; otherwise I'd just drop them.

Laszlo

> 
> Thanks
> Laszlo
> 
>> standalone and traditional MM driver entrypoints into separate files to
>> allow maximal common implementations.
>>
>> Cc: Eric Dong <eric.dong@intel.com>
>> Cc: Ray Ni <ray.ni@intel.com>
>> Cc: Laszlo Ersek <lersek@redhat.com>
>> Cc: Rahul Kumar <rahul1.kumar@intel.com>
>>
>> Signed-off-by: Kun Qin <kun.q@outlook.com>
>> ---
>>
>> Notes:
>>     v2:
>>     - Removed "EFIAPI" for internal functions.
>>
>>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.c => CpuIo2Common.c}           |  15 +-
>>  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c                               | 384 +-------------------
>>  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c                      |  31 ++
>>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.h => CpuIo2Common.h}           |  17 +-
>>  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf                             |   7 +-
>>  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.inf => CpuIo2StandaloneMm.inf} |  23 +-
>>  UefiCpuPkg/UefiCpuPkg.dsc                                      |   6 +
>>  7 files changed, 75 insertions(+), 408 deletions(-)
>>
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
>> similarity index 94%
>> copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
>> copy to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
>> index b840d3e10cae..fef434795908 100644
>> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
>> @@ -6,7 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>>  
>>  **/
>>  
>> -#include "CpuIo2Smm.h"
>> +#include "CpuIo2Common.h"
>>  
>>  //
>>  // Handle for the SMM CPU I/O Protocol
>> @@ -371,18 +371,13 @@ CpuIoServiceWrite (
>>  /**
>>    The module Entry Point SmmCpuIoProtocol driver
>>  
>> -  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
>> -  @param[in] SystemTable  A pointer to the EFI System Table.
>> -
>>    @retval EFI_SUCCESS  The entry point is executed successfully.
>>    @retval Other        Some error occurs when executing this entry point.
>>  
>>  **/
>>  EFI_STATUS
>> -EFIAPI
>> -SmmCpuIo2Initialize (
>> -  IN EFI_HANDLE        ImageHandle,
>> -  IN EFI_SYSTEM_TABLE  *SystemTable
>> +CommonCpuIo2Initialize (
>> +  VOID
>>    )
>>  {
>>    EFI_STATUS  Status;
>> @@ -390,12 +385,12 @@ SmmCpuIo2Initialize (
>>    //
>>    // Copy the SMM CPU I/O Protocol instance into the System Management System Table
>>    //
>> -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
>> +  CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
>>  
>>    //
>>    // Install the SMM CPU I/O Protocol into the SMM protocol database
>>    //
>> -  Status = gSmst->SmmInstallProtocolInterface (
>> +  Status = gMmst->MmInstallProtocolInterface (
>>                      &mHandle,
>>                      &gEfiSmmCpuIo2ProtocolGuid,
>>                      EFI_NATIVE_INTERFACE,
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
>> index b840d3e10cae..062d1a47a09f 100644
>> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
>> @@ -6,367 +6,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>>  
>>  **/
>>  
>> -#include "CpuIo2Smm.h"
>> +#include <PiSmm.h>
>>  
>> -//
>> -// Handle for the SMM CPU I/O Protocol
>> -//
>> -EFI_HANDLE  mHandle = NULL;
>> -
>> -//
>> -// SMM CPU I/O Protocol instance
>> -//
>> -EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {
>> -  {
>> -    CpuMemoryServiceRead,
>> -    CpuMemoryServiceWrite
>> -  },
>> -  {
>> -    CpuIoServiceRead,
>> -    CpuIoServiceWrite
>> -  }
>> -};
>> -
>> -//
>> -// Lookup table for increment values based on transfer widths
>> -//
>> -UINT8 mStride[] = {
>> -  1, // SMM_IO_UINT8
>> -  2, // SMM_IO_UINT16
>> -  4, // SMM_IO_UINT32
>> -  8  // SMM_IO_UINT64
>> -};
>> -
>> -/**
>> -  Check parameters to a SMM CPU I/O Protocol service request.
>> -
>> -  @param[in]  MmioOperation  TRUE for an MMIO operation, FALSE for I/O Port operation.
>> -  @param[in]  Width          Signifies the width of the I/O operations.
>> -  @param[in]  Address        The base address of the I/O operations.  The caller is
>> -                             responsible for aligning the Address if required.
>> -  @param[in]  Count          The number of I/O operations to perform.
>> -  @param[in]  Buffer         For read operations, the destination buffer to store
>> -                             the results.  For write operations, the source buffer
>> -                             from which to write data.
>> -
>> -  @retval EFI_SUCCESS            The data was read from or written to the device.
>> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
>> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
>> -
>> -**/
>> -EFI_STATUS
>> -CpuIoCheckParameter (
>> -  IN BOOLEAN           MmioOperation,
>> -  IN EFI_SMM_IO_WIDTH  Width,
>> -  IN UINT64            Address,
>> -  IN UINTN             Count,
>> -  IN VOID              *Buffer
>> -  )
>> -{
>> -  UINT64  MaxCount;
>> -  UINT64  Limit;
>> -
>> -  //
>> -  // Check to see if Buffer is NULL
>> -  //
>> -  if (Buffer == NULL) {
>> -    return EFI_INVALID_PARAMETER;
>> -  }
>> -
>> -  //
>> -  // Check to see if Width is in the valid range
>> -  //
>> -  if ((UINT32)Width > SMM_IO_UINT64) {
>> -    return EFI_INVALID_PARAMETER;
>> -  }
>> -
>> -  //
>> -  // Check to see if Width is in the valid range for I/O Port operations
>> -  //
>> -  if (!MmioOperation && (Width == SMM_IO_UINT64)) {
>> -    return EFI_INVALID_PARAMETER;
>> -  }
>> -
>> -  //
>> -  // Check to see if any address associated with this transfer exceeds the maximum
>> -  // allowed address.  The maximum address implied by the parameters passed in is
>> -  // Address + Size * Count.  If the following condition is met, then the transfer
>> -  // is not supported.
>> -  //
>> -  //    Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
>> -  //
>> -  // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
>> -  // can also be the maximum integer value supported by the CPU, this range
>> -  // check must be adjusted to avoid all overflow conditions.
>> -  //
>> -  // The following form of the range check is equivalent but assumes that
>> -  // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
>> -  //
>> -  Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);
>> -  if (Count == 0) {
>> -    if (Address > Limit) {
>> -      return EFI_UNSUPPORTED;
>> -    }
>> -  } else {
>> -    MaxCount = RShiftU64 (Limit, Width);
>> -    if (MaxCount < (Count - 1)) {
>> -      return EFI_UNSUPPORTED;
>> -    }
>> -    if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
>> -      return EFI_UNSUPPORTED;
>> -    }
>> -  }
>> -
>> -  //
>> -  // Check to see if Address is aligned
>> -  //
>> -  if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {
>> -    return EFI_UNSUPPORTED;
>> -  }
>> -
>> -  return EFI_SUCCESS;
>> -}
>> -
>> -/**
>> -  Reads memory-mapped registers.
>> -
>> -  The I/O operations are carried out exactly as requested.  The caller is
>> -  responsible for any alignment and I/O width issues that the bus, device,
>> -  platform, or type of I/O might require.
>> -
>> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
>> -  @param[in]  Width    Signifies the width of the I/O operations.
>> -  @param[in]  Address  The base address of the I/O operations.  The caller is
>> -                       responsible for aligning the Address if required.
>> -  @param[in]  Count    The number of I/O operations to perform.
>> -  @param[out] Buffer   For read operations, the destination buffer to store
>> -                       the results.  For write operations, the source buffer
>> -                       from which to write data.
>> -
>> -  @retval EFI_SUCCESS            The data was read from or written to the device.
>> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
>> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
>> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
>> -                                 lack of resources
>> -
>> -**/
>> -EFI_STATUS
>> -EFIAPI
>> -CpuMemoryServiceRead (
>> -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
>> -  IN  EFI_SMM_IO_WIDTH                Width,
>> -  IN  UINT64                          Address,
>> -  IN  UINTN                           Count,
>> -  OUT VOID                            *Buffer
>> -  )
>> -{
>> -  EFI_STATUS  Status;
>> -  UINT8       Stride;
>> -  UINT8       *Uint8Buffer;
>> -
>> -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
>> -  if (EFI_ERROR (Status)) {
>> -    return Status;
>> -  }
>> -
>> -  //
>> -  // Select loop based on the width of the transfer
>> -  //
>> -  Stride = mStride[Width];
>> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
>> -    if (Width == SMM_IO_UINT8) {
>> -      *Uint8Buffer = MmioRead8 ((UINTN)Address);
>> -    } else if (Width == SMM_IO_UINT16) {
>> -      *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
>> -    } else if (Width == SMM_IO_UINT32) {
>> -      *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
>> -    } else if (Width == SMM_IO_UINT64) {
>> -      *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
>> -    }
>> -  }
>> -  return EFI_SUCCESS;
>> -}
>> -
>> -/**
>> -  Writes memory-mapped registers.
>> -
>> -  The I/O operations are carried out exactly as requested.  The caller is
>> -  responsible for any alignment and I/O width issues that the bus, device,
>> -  platform, or type of I/O might require.
>> -
>> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
>> -  @param[in]  Width    Signifies the width of the I/O operations.
>> -  @param[in]  Address  The base address of the I/O operations.  The caller is
>> -                       responsible for aligning the Address if required.
>> -  @param[in]  Count    The number of I/O operations to perform.
>> -  @param[in]  Buffer   For read operations, the destination buffer to store
>> -                       the results.  For write operations, the source buffer
>> -                       from which to write data.
>> -
>> -  @retval EFI_SUCCESS            The data was read from or written to the device.
>> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
>> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
>> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
>> -                                 lack of resources
>> -
>> -**/
>> -EFI_STATUS
>> -EFIAPI
>> -CpuMemoryServiceWrite (
>> -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
>> -  IN EFI_SMM_IO_WIDTH                Width,
>> -  IN UINT64                          Address,
>> -  IN UINTN                           Count,
>> -  IN VOID                            *Buffer
>> -  )
>> -{
>> -  EFI_STATUS  Status;
>> -  UINT8       Stride;
>> -  UINT8       *Uint8Buffer;
>> -
>> -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
>> -  if (EFI_ERROR (Status)) {
>> -    return Status;
>> -  }
>> -
>> -  //
>> -  // Select loop based on the width of the transfer
>> -  //
>> -  Stride = mStride[Width];
>> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
>> -    if (Width == SMM_IO_UINT8) {
>> -      MmioWrite8 ((UINTN)Address, *Uint8Buffer);
>> -    } else if (Width == SMM_IO_UINT16) {
>> -      MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
>> -    } else if (Width == SMM_IO_UINT32) {
>> -      MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
>> -    } else if (Width == SMM_IO_UINT64) {
>> -      MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
>> -    }
>> -  }
>> -  return EFI_SUCCESS;
>> -}
>> -
>> -/**
>> -  Reads I/O registers.
>> -
>> -  The I/O operations are carried out exactly as requested.  The caller is
>> -  responsible for any alignment and I/O width issues that the bus, device,
>> -  platform, or type of I/O might require.
>> -
>> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
>> -  @param[in]  Width    Signifies the width of the I/O operations.
>> -  @param[in]  Address  The base address of the I/O operations.  The caller is
>> -                       responsible for aligning the Address if required.
>> -  @param[in]  Count    The number of I/O operations to perform.
>> -  @param[out] Buffer   For read operations, the destination buffer to store
>> -                       the results.  For write operations, the source buffer
>> -                       from which to write data.
>> -
>> -  @retval EFI_SUCCESS            The data was read from or written to the device.
>> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
>> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
>> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
>> -                                 lack of resources
>> -
>> -**/
>> -EFI_STATUS
>> -EFIAPI
>> -CpuIoServiceRead (
>> -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
>> -  IN  EFI_SMM_IO_WIDTH                Width,
>> -  IN  UINT64                          Address,
>> -  IN  UINTN                           Count,
>> -  OUT VOID                            *Buffer
>> -  )
>> -{
>> -  EFI_STATUS  Status;
>> -  UINT8       Stride;
>> -  UINT8       *Uint8Buffer;
>> -
>> -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
>> -  if (EFI_ERROR (Status)) {
>> -    return Status;
>> -  }
>> -
>> -  //
>> -  // Select loop based on the width of the transfer
>> -  //
>> -  Stride = mStride[Width];
>> -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
>> -    if (Width == SMM_IO_UINT8) {
>> -      *Uint8Buffer = IoRead8 ((UINTN)Address);
>> -    } else if (Width == SMM_IO_UINT16) {
>> -      *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);
>> -    } else if (Width == SMM_IO_UINT32) {
>> -      *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);
>> -    }
>> -  }
>> -
>> -  return EFI_SUCCESS;
>> -}
>> -
>> -/**
>> -  Write I/O registers.
>> -
>> -  The I/O operations are carried out exactly as requested.  The caller is
>> -  responsible for any alignment and I/O width issues that the bus, device,
>> -  platform, or type of I/O might require.
>> -
>> -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
>> -  @param[in]  Width    Signifies the width of the I/O operations.
>> -  @param[in]  Address  The base address of the I/O operations.  The caller is
>> -                       responsible for aligning the Address if required.
>> -  @param[in]  Count    The number of I/O operations to perform.
>> -  @param[in]  Buffer   For read operations, the destination buffer to store
>> -                       the results.  For write operations, the source buffer
>> -                       from which to write data.
>> -
>> -  @retval EFI_SUCCESS            The data was read from or written to the device.
>> -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
>> -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
>> -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
>> -                                 lack of resources
>> -
>> -**/
>> -EFI_STATUS
>> -EFIAPI
>> -CpuIoServiceWrite (
>> -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
>> -  IN EFI_SMM_IO_WIDTH                Width,
>> -  IN UINT64                          Address,
>> -  IN UINTN                           Count,
>> -  IN VOID                            *Buffer
>> -  )
>> -{
>> -  EFI_STATUS  Status;
>> -  UINT8       Stride;
>> -  UINT8       *Uint8Buffer;
>> -
>> -  //
>> -  // Make sure the parameters are valid
>> -  //
>> -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
>> -  if (EFI_ERROR (Status)) {
>> -    return Status;
>> -  }
>> -
>> -  //
>> -  // Select loop based on the width of the transfer
>> -  //
>> -  Stride = mStride[Width];
>> -  for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
>> -    if (Width == SMM_IO_UINT8) {
>> -      IoWrite8 ((UINTN)Address, *Uint8Buffer);
>> -    } else if (Width == SMM_IO_UINT16) {
>> -      IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
>> -    } else if (Width == SMM_IO_UINT32) {
>> -      IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
>> -    }
>> -  }
>> -
>> -  return EFI_SUCCESS;
>> -}
>> +#include "CpuIo2Common.h"
>>  
>>  /**
>>    The module Entry Point SmmCpuIoProtocol driver
>> @@ -380,28 +22,10 @@ CpuIoServiceWrite (
>>  **/
>>  EFI_STATUS
>>  EFIAPI
>> -SmmCpuIo2Initialize (
>> +TraditionalMmCpuIo2Initialize (
>>    IN EFI_HANDLE        ImageHandle,
>>    IN EFI_SYSTEM_TABLE  *SystemTable
>>    )
>>  {
>> -  EFI_STATUS  Status;
>> -
>> -  //
>> -  // Copy the SMM CPU I/O Protocol instance into the System Management System Table
>> -  //
>> -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
>> -
>> -  //
>> -  // Install the SMM CPU I/O Protocol into the SMM protocol database
>> -  //
>> -  Status = gSmst->SmmInstallProtocolInterface (
>> -                    &mHandle,
>> -                    &gEfiSmmCpuIo2ProtocolGuid,
>> -                    EFI_NATIVE_INTERFACE,
>> -                    &mSmmCpuIo2
>> -                    );
>> -  ASSERT_EFI_ERROR (Status);
>> -
>> -  return Status;
>> +  return CommonCpuIo2Initialize ();
>>  }
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
>> new file mode 100644
>> index 000000000000..2978f82eb815
>> --- /dev/null
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
>> @@ -0,0 +1,31 @@
>> +/** @file
>> +  Produces the SMM CPU I/O Protocol.
>> +
>> +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
>> +SPDX-License-Identifier: BSD-2-Clause-Patent
>> +
>> +**/
>> +
>> +#include <PiMm.h>
>> +
>> +#include "CpuIo2Common.h"
>> +
>> +/**
>> +  The module Entry Point SmmCpuIoProtocol driver
>> +
>> +  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
>> +  @param[in] SystemTable  A pointer to the EFI System Table.
>> +
>> +  @retval EFI_SUCCESS  The entry point is executed successfully.
>> +  @retval Other        Some error occurs when executing this entry point.
>> +
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +StandaloneMmCpuIo2Initialize (
>> +  IN EFI_HANDLE           ImageHandle,
>> +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
>> +  )
>> +{
>> +  return CommonCpuIo2Initialize ();
>> +}
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
>> similarity index 92%
>> rename from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
>> rename to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
>> index 4c133b58c9f4..6759cc1c8777 100644
>> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
>> @@ -9,14 +9,14 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>>  #ifndef _CPU_IO2_SMM_H_
>>  #define _CPU_IO2_SMM_H_
>>  
>> -#include <PiSmm.h>
>> +#include <PiMm.h>
>>  
>>  #include <Protocol/SmmCpuIo2.h>
>>  
>>  #include <Library/BaseLib.h>
>>  #include <Library/DebugLib.h>
>>  #include <Library/IoLib.h>
>> -#include <Library/SmmServicesTableLib.h>
>> +#include <Library/MmServicesTableLib.h>
>>  #include <Library/BaseMemoryLib.h>
>>  
>>  #define MAX_IO_PORT_ADDRESS   0xFFFF
>> @@ -153,4 +153,17 @@ CpuIoServiceWrite (
>>    IN VOID                            *Buffer
>>    );
>>  
>> +
>> +/**
>> +  The module Entry Point SmmCpuIoProtocol driver
>> +
>> +  @retval EFI_SUCCESS  The entry point is executed successfully.
>> +  @retval Other        Some error occurs when executing this entry point.
>> +
>> +**/
>> +EFI_STATUS
>> +CommonCpuIo2Initialize (
>> +  VOID
>> +  );
>> +
>>  #endif
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> index bc78fa4e42d2..031db1d100b9 100644
>> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> @@ -14,7 +14,7 @@ [Defines]
>>    MODULE_TYPE                    = DXE_SMM_DRIVER
>>    VERSION_STRING                 = 1.0
>>    PI_SPECIFICATION_VERSION       = 0x0001000A
>> -  ENTRY_POINT                    = SmmCpuIo2Initialize
>> +  ENTRY_POINT                    = TraditionalMmCpuIo2Initialize
>>  
>>  #
>>  # The following information is for reference only and not required by the build tools.
>> @@ -24,7 +24,8 @@ [Defines]
>>  
>>  [Sources]
>>    CpuIo2Smm.c
>> -  CpuIo2Smm.h
>> +  CpuIo2Common.c
>> +  CpuIo2Common.h
>>  
>>  [Packages]
>>    MdePkg/MdePkg.dec
>> @@ -34,7 +35,7 @@ [LibraryClasses]
>>    BaseLib
>>    DebugLib
>>    IoLib
>> -  SmmServicesTableLib
>> +  MmServicesTableLib
>>    BaseMemoryLib
>>  
>>  [Protocols]
>> diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
>> similarity index 56%
>> copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> copy to UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
>> index bc78fa4e42d2..cce7742bee7a 100644
>> --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
>> @@ -8,13 +8,12 @@
>>  
>>  [Defines]
>>    INF_VERSION                    = 0x00010005
>> -  BASE_NAME                      = CpuIo2Smm
>> -  MODULE_UNI_FILE                = CpuIo2Smm.uni
>> -  FILE_GUID                      = A47EE2D8-F60E-42fd-8E58-7BD65EE4C29B
>> -  MODULE_TYPE                    = DXE_SMM_DRIVER
>> +  BASE_NAME                      = CpuIo2StandaloneMm
>> +  FILE_GUID                      = E3121A26-BB1C-4A18-8E23-2EA3F0412248
>> +  MODULE_TYPE                    = MM_STANDALONE
>>    VERSION_STRING                 = 1.0
>> -  PI_SPECIFICATION_VERSION       = 0x0001000A
>> -  ENTRY_POINT                    = SmmCpuIo2Initialize
>> +  PI_SPECIFICATION_VERSION       = 0x00010032
>> +  ENTRY_POINT                    = StandaloneMmCpuIo2Initialize
>>  
>>  #
>>  # The following information is for reference only and not required by the build tools.
>> @@ -23,18 +22,19 @@ [Defines]
>>  #
>>  
>>  [Sources]
>> -  CpuIo2Smm.c
>> -  CpuIo2Smm.h
>> +  CpuIo2StandaloneMm.c
>> +  CpuIo2Common.c
>> +  CpuIo2Common.h
>>  
>>  [Packages]
>>    MdePkg/MdePkg.dec
>>  
>>  [LibraryClasses]
>> -  UefiDriverEntryPoint
>> +  StandaloneMmDriverEntryPoint
>>    BaseLib
>>    DebugLib
>>    IoLib
>> -  SmmServicesTableLib
>> +  MmServicesTableLib
>>    BaseMemoryLib
>>  
>>  [Protocols]
>> @@ -42,6 +42,3 @@ [Protocols]
>>  
>>  [Depex]
>>    TRUE
>> -
>> -[UserExtensions.TianoCore."ExtraFiles"]
>> -  CpuIo2SmmExtra.uni
>> diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
>> index b2b6d78a71b0..4e46c600788f 100644
>> --- a/UefiCpuPkg/UefiCpuPkg.dsc
>> +++ b/UefiCpuPkg/UefiCpuPkg.dsc
>> @@ -38,6 +38,7 @@ [LibraryClasses]
>>    UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
>>    UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
>>    UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
>> +  StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
>>    DxeServicesTableLib|MdePkg/Library/DxeServicesTableLib/DxeServicesTableLib.inf
>>    PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
>>    PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
>> @@ -89,10 +90,14 @@ [LibraryClasses.common.DXE_DRIVER]
>>  
>>  [LibraryClasses.common.DXE_SMM_DRIVER]
>>    SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
>> +  MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
>>    MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
>>    HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
>>    CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
>>  
>> +[LibraryClasses.common.MM_STANDALONE]
>> +  MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
>> +
>>  [LibraryClasses.common.UEFI_APPLICATION]
>>    UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
>>    MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
>> @@ -121,6 +126,7 @@ [Components.IA32, Components.X64]
>>        NULL|UefiCpuPkg/Library/CpuCommonFeaturesLib/CpuCommonFeaturesLib.inf
>>    }
>>    UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
>> +  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
>>    UefiCpuPkg/CpuMpPei/CpuMpPei.inf
>>    UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
>>    UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
>>
> 


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
  2021-01-06 15:51     ` Laszlo Ersek
@ 2021-01-06 19:02       ` Kun Qin
  2021-01-06 19:07         ` Laszlo Ersek
  0 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-06 19:02 UTC (permalink / raw)
  To: Laszlo Ersek, devel

[-- Attachment #1: Type: text/plain, Size: 1130 bytes --]

Hi Laszlo,

I did the patch mostly following the pattern of this commit: MdeModulePkg/FaultTolerantWriteDxe: factor out boot service accesses · tianocore/edk2@22cedf5 (github.com) ( https://github.com/tianocore/edk2/commit/22cedf5bbde616e0369e9f8649964f175a0cb95e ). The extra step I made was renaming the CpuIo2Smm.c to CpuIo2Common.c for semantic reason.

The needed changes here are:
1. Replacing gSmst with gMmst. Because SmmServicesTableLib does not exist for MM_STANDALONE type drivers, but MmServicesTableLib has library instances for both MM_STANDALONE and DXE_SMM_DRIVER as is. Both MmServicesTableLib and SmmServicesTableLib retrieve services table through the same protocol GUID, reference to substantially the same data structure.
2. Abstract the driver entry point for Traditional MM and Standalone MM instances. Because these drivers have different entry point interface definitions.

I will update the commit message to fix the typo. In addition, I can revert the file renaming change and break the above 2 changes into 2 patches, if that make the git history and review process cleaner.

Regards,
Kun

[-- Attachment #2: Type: text/html, Size: 1689 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
  2021-01-06 19:02       ` [edk2-devel] " Kun Qin
@ 2021-01-06 19:07         ` Laszlo Ersek
  0 siblings, 0 replies; 34+ messages in thread
From: Laszlo Ersek @ 2021-01-06 19:07 UTC (permalink / raw)
  To: Kun Qin, devel

On 01/06/21 20:02, Kun Qin wrote:
> Hi Laszlo,
> 
> I did the patch mostly following the pattern of this commit: MdeModulePkg/FaultTolerantWriteDxe: factor out boot service accesses · tianocore/edk2@22cedf5 (github.com) ( https://github.com/tianocore/edk2/commit/22cedf5bbde616e0369e9f8649964f175a0cb95e ). The extra step I made was renaming the CpuIo2Smm.c to CpuIo2Common.c for semantic reason.
> 
> The needed changes here are:
> 1. Replacing gSmst with gMmst. Because SmmServicesTableLib does not exist for MM_STANDALONE type drivers, but MmServicesTableLib has library instances for both MM_STANDALONE and DXE_SMM_DRIVER as is. Both MmServicesTableLib and SmmServicesTableLib retrieve services table through the same protocol GUID, reference to substantially the same data structure.

OK. But please perform this step stand-alone. There should be a patch
that *only* moves from gSmst to gMmst.

> 2. Abstract the driver entry point for Traditional MM and Standalone MM instances. Because these drivers have different entry point interface definitions.
> 
> I will update the commit message to fix the typo. In addition, I can revert the file renaming change and break the above 2 changes into 2 patches, if that make the git history and review process cleaner.

Yes, please.

Thanks!
Laszlo


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm
  2021-01-06 15:46   ` Laszlo Ersek
  2021-01-06 15:51     ` Laszlo Ersek
@ 2021-01-07 19:05     ` Leif Lindholm
  1 sibling, 0 replies; 34+ messages in thread
From: Leif Lindholm @ 2021-01-07 19:05 UTC (permalink / raw)
  To: Laszlo Ersek
  Cc: Kun Qin, devel, Eric Dong, Ray Ni, Rahul Kumar, Michael Kinney,
	Ard Biesheuvel (ARM address)

On Wed, Jan 06, 2021 at 16:46:57 +0100, Laszlo Ersek wrote:
> On 01/05/21 19:59, Kun Qin wrote:
> > This change added support of CpuIo driver under StandaloneMm. The core
> > services switched to gMmst based instead of gMmst. It also abstracts
> 
> (1) typo: "gMmst based instead of gMmst" -- that's the same thing
> 
> (2) renames like this should not be mixed with logic changes /
> refactorings, so this would be two patches minimally
> 
> (3) in fact I disagree with renaming gSmst to gMmst (and the resultant
> fallout). I see very little value in this, it only complicates the git
> history, takes up reviewer resources, and risks regressions. Unless
> absolutely necessary for getting the driver build in the new context, I
> would strongly advise against the rename. If the rename is unavoidable,
> then see (2) please -- do it in a separate patch.
> 
> (4) If I understand correctly, this is the first time when UefiCpuPkg
> content is consumed in AARCH64 platforms. I wonder if that means that
> the CpuIo2Smm should no longer exist under UefiCpuPkg at all. UefiCpuPkg
> has been traditionally IA32/X64 only, and while the *name* of the
> package certainly does not require or imply that, over time those two
> things *have* become synonymous. For example, the SMM Core lives under
> MdeModulePkg; I wonder if the same should happen to CpuIo2Smm as well.
> Adding Ard, Leif and Mike.

I don't really have an opinion of CpuIo2Smm (but I guess it makes
sense to move it if the SMM core lives in MdeModulePkg).

I do think most of ArmPkg ultimately belongs under UefiCpuPkg, with
the rest under MdeModulePkg.

Best Regards,

Leif

> Thanks
> Laszlo
> 
> > standalone and traditional MM driver entrypoints into separate files to
> > allow maximal common implementations.
> > 
> > Cc: Eric Dong <eric.dong@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > Cc: Rahul Kumar <rahul1.kumar@intel.com>
> > 
> > Signed-off-by: Kun Qin <kun.q@outlook.com>
> > ---
> > 
> > Notes:
> >     v2:
> >     - Removed "EFIAPI" for internal functions.
> > 
> >  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.c => CpuIo2Common.c}           |  15 +-
> >  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c                               | 384 +-------------------
> >  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c                      |  31 ++
> >  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.h => CpuIo2Common.h}           |  17 +-
> >  UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf                             |   7 +-
> >  UefiCpuPkg/CpuIo2Smm/{CpuIo2Smm.inf => CpuIo2StandaloneMm.inf} |  23 +-
> >  UefiCpuPkg/UefiCpuPkg.dsc                                      |   6 +
> >  7 files changed, 75 insertions(+), 408 deletions(-)
> > 
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> > similarity index 94%
> > copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> > copy to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> > index b840d3e10cae..fef434795908 100644
> > --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.c
> > @@ -6,7 +6,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  
> >  **/
> >  
> > -#include "CpuIo2Smm.h"
> > +#include "CpuIo2Common.h"
> >  
> >  //
> >  // Handle for the SMM CPU I/O Protocol
> > @@ -371,18 +371,13 @@ CpuIoServiceWrite (
> >  /**
> >    The module Entry Point SmmCpuIoProtocol driver
> >  
> > -  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
> > -  @param[in] SystemTable  A pointer to the EFI System Table.
> > -
> >    @retval EFI_SUCCESS  The entry point is executed successfully.
> >    @retval Other        Some error occurs when executing this entry point.
> >  
> >  **/
> >  EFI_STATUS
> > -EFIAPI
> > -SmmCpuIo2Initialize (
> > -  IN EFI_HANDLE        ImageHandle,
> > -  IN EFI_SYSTEM_TABLE  *SystemTable
> > +CommonCpuIo2Initialize (
> > +  VOID
> >    )
> >  {
> >    EFI_STATUS  Status;
> > @@ -390,12 +385,12 @@ SmmCpuIo2Initialize (
> >    //
> >    // Copy the SMM CPU I/O Protocol instance into the System Management System Table
> >    //
> > -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
> > +  CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
> >  
> >    //
> >    // Install the SMM CPU I/O Protocol into the SMM protocol database
> >    //
> > -  Status = gSmst->SmmInstallProtocolInterface (
> > +  Status = gMmst->MmInstallProtocolInterface (
> >                      &mHandle,
> >                      &gEfiSmmCpuIo2ProtocolGuid,
> >                      EFI_NATIVE_INTERFACE,
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> > index b840d3e10cae..062d1a47a09f 100644
> > --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
> > @@ -6,367 +6,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  
> >  **/
> >  
> > -#include "CpuIo2Smm.h"
> > +#include <PiSmm.h>
> >  
> > -//
> > -// Handle for the SMM CPU I/O Protocol
> > -//
> > -EFI_HANDLE  mHandle = NULL;
> > -
> > -//
> > -// SMM CPU I/O Protocol instance
> > -//
> > -EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {
> > -  {
> > -    CpuMemoryServiceRead,
> > -    CpuMemoryServiceWrite
> > -  },
> > -  {
> > -    CpuIoServiceRead,
> > -    CpuIoServiceWrite
> > -  }
> > -};
> > -
> > -//
> > -// Lookup table for increment values based on transfer widths
> > -//
> > -UINT8 mStride[] = {
> > -  1, // SMM_IO_UINT8
> > -  2, // SMM_IO_UINT16
> > -  4, // SMM_IO_UINT32
> > -  8  // SMM_IO_UINT64
> > -};
> > -
> > -/**
> > -  Check parameters to a SMM CPU I/O Protocol service request.
> > -
> > -  @param[in]  MmioOperation  TRUE for an MMIO operation, FALSE for I/O Port operation.
> > -  @param[in]  Width          Signifies the width of the I/O operations.
> > -  @param[in]  Address        The base address of the I/O operations.  The caller is
> > -                             responsible for aligning the Address if required.
> > -  @param[in]  Count          The number of I/O operations to perform.
> > -  @param[in]  Buffer         For read operations, the destination buffer to store
> > -                             the results.  For write operations, the source buffer
> > -                             from which to write data.
> > -
> > -  @retval EFI_SUCCESS            The data was read from or written to the device.
> > -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> > -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> > -
> > -**/
> > -EFI_STATUS
> > -CpuIoCheckParameter (
> > -  IN BOOLEAN           MmioOperation,
> > -  IN EFI_SMM_IO_WIDTH  Width,
> > -  IN UINT64            Address,
> > -  IN UINTN             Count,
> > -  IN VOID              *Buffer
> > -  )
> > -{
> > -  UINT64  MaxCount;
> > -  UINT64  Limit;
> > -
> > -  //
> > -  // Check to see if Buffer is NULL
> > -  //
> > -  if (Buffer == NULL) {
> > -    return EFI_INVALID_PARAMETER;
> > -  }
> > -
> > -  //
> > -  // Check to see if Width is in the valid range
> > -  //
> > -  if ((UINT32)Width > SMM_IO_UINT64) {
> > -    return EFI_INVALID_PARAMETER;
> > -  }
> > -
> > -  //
> > -  // Check to see if Width is in the valid range for I/O Port operations
> > -  //
> > -  if (!MmioOperation && (Width == SMM_IO_UINT64)) {
> > -    return EFI_INVALID_PARAMETER;
> > -  }
> > -
> > -  //
> > -  // Check to see if any address associated with this transfer exceeds the maximum
> > -  // allowed address.  The maximum address implied by the parameters passed in is
> > -  // Address + Size * Count.  If the following condition is met, then the transfer
> > -  // is not supported.
> > -  //
> > -  //    Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
> > -  //
> > -  // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
> > -  // can also be the maximum integer value supported by the CPU, this range
> > -  // check must be adjusted to avoid all overflow conditions.
> > -  //
> > -  // The following form of the range check is equivalent but assumes that
> > -  // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
> > -  //
> > -  Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);
> > -  if (Count == 0) {
> > -    if (Address > Limit) {
> > -      return EFI_UNSUPPORTED;
> > -    }
> > -  } else {
> > -    MaxCount = RShiftU64 (Limit, Width);
> > -    if (MaxCount < (Count - 1)) {
> > -      return EFI_UNSUPPORTED;
> > -    }
> > -    if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
> > -      return EFI_UNSUPPORTED;
> > -    }
> > -  }
> > -
> > -  //
> > -  // Check to see if Address is aligned
> > -  //
> > -  if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {
> > -    return EFI_UNSUPPORTED;
> > -  }
> > -
> > -  return EFI_SUCCESS;
> > -}
> > -
> > -/**
> > -  Reads memory-mapped registers.
> > -
> > -  The I/O operations are carried out exactly as requested.  The caller is
> > -  responsible for any alignment and I/O width issues that the bus, device,
> > -  platform, or type of I/O might require.
> > -
> > -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> > -  @param[in]  Width    Signifies the width of the I/O operations.
> > -  @param[in]  Address  The base address of the I/O operations.  The caller is
> > -                       responsible for aligning the Address if required.
> > -  @param[in]  Count    The number of I/O operations to perform.
> > -  @param[out] Buffer   For read operations, the destination buffer to store
> > -                       the results.  For write operations, the source buffer
> > -                       from which to write data.
> > -
> > -  @retval EFI_SUCCESS            The data was read from or written to the device.
> > -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> > -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> > -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> > -                                 lack of resources
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -CpuMemoryServiceRead (
> > -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> > -  IN  EFI_SMM_IO_WIDTH                Width,
> > -  IN  UINT64                          Address,
> > -  IN  UINTN                           Count,
> > -  OUT VOID                            *Buffer
> > -  )
> > -{
> > -  EFI_STATUS  Status;
> > -  UINT8       Stride;
> > -  UINT8       *Uint8Buffer;
> > -
> > -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
> > -  if (EFI_ERROR (Status)) {
> > -    return Status;
> > -  }
> > -
> > -  //
> > -  // Select loop based on the width of the transfer
> > -  //
> > -  Stride = mStride[Width];
> > -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> > -    if (Width == SMM_IO_UINT8) {
> > -      *Uint8Buffer = MmioRead8 ((UINTN)Address);
> > -    } else if (Width == SMM_IO_UINT16) {
> > -      *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
> > -    } else if (Width == SMM_IO_UINT32) {
> > -      *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
> > -    } else if (Width == SMM_IO_UINT64) {
> > -      *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
> > -    }
> > -  }
> > -  return EFI_SUCCESS;
> > -}
> > -
> > -/**
> > -  Writes memory-mapped registers.
> > -
> > -  The I/O operations are carried out exactly as requested.  The caller is
> > -  responsible for any alignment and I/O width issues that the bus, device,
> > -  platform, or type of I/O might require.
> > -
> > -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> > -  @param[in]  Width    Signifies the width of the I/O operations.
> > -  @param[in]  Address  The base address of the I/O operations.  The caller is
> > -                       responsible for aligning the Address if required.
> > -  @param[in]  Count    The number of I/O operations to perform.
> > -  @param[in]  Buffer   For read operations, the destination buffer to store
> > -                       the results.  For write operations, the source buffer
> > -                       from which to write data.
> > -
> > -  @retval EFI_SUCCESS            The data was read from or written to the device.
> > -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> > -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> > -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> > -                                 lack of resources
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -CpuMemoryServiceWrite (
> > -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> > -  IN EFI_SMM_IO_WIDTH                Width,
> > -  IN UINT64                          Address,
> > -  IN UINTN                           Count,
> > -  IN VOID                            *Buffer
> > -  )
> > -{
> > -  EFI_STATUS  Status;
> > -  UINT8       Stride;
> > -  UINT8       *Uint8Buffer;
> > -
> > -  Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
> > -  if (EFI_ERROR (Status)) {
> > -    return Status;
> > -  }
> > -
> > -  //
> > -  // Select loop based on the width of the transfer
> > -  //
> > -  Stride = mStride[Width];
> > -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> > -    if (Width == SMM_IO_UINT8) {
> > -      MmioWrite8 ((UINTN)Address, *Uint8Buffer);
> > -    } else if (Width == SMM_IO_UINT16) {
> > -      MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
> > -    } else if (Width == SMM_IO_UINT32) {
> > -      MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
> > -    } else if (Width == SMM_IO_UINT64) {
> > -      MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
> > -    }
> > -  }
> > -  return EFI_SUCCESS;
> > -}
> > -
> > -/**
> > -  Reads I/O registers.
> > -
> > -  The I/O operations are carried out exactly as requested.  The caller is
> > -  responsible for any alignment and I/O width issues that the bus, device,
> > -  platform, or type of I/O might require.
> > -
> > -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> > -  @param[in]  Width    Signifies the width of the I/O operations.
> > -  @param[in]  Address  The base address of the I/O operations.  The caller is
> > -                       responsible for aligning the Address if required.
> > -  @param[in]  Count    The number of I/O operations to perform.
> > -  @param[out] Buffer   For read operations, the destination buffer to store
> > -                       the results.  For write operations, the source buffer
> > -                       from which to write data.
> > -
> > -  @retval EFI_SUCCESS            The data was read from or written to the device.
> > -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> > -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> > -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> > -                                 lack of resources
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -CpuIoServiceRead (
> > -  IN  CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> > -  IN  EFI_SMM_IO_WIDTH                Width,
> > -  IN  UINT64                          Address,
> > -  IN  UINTN                           Count,
> > -  OUT VOID                            *Buffer
> > -  )
> > -{
> > -  EFI_STATUS  Status;
> > -  UINT8       Stride;
> > -  UINT8       *Uint8Buffer;
> > -
> > -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
> > -  if (EFI_ERROR (Status)) {
> > -    return Status;
> > -  }
> > -
> > -  //
> > -  // Select loop based on the width of the transfer
> > -  //
> > -  Stride = mStride[Width];
> > -  for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> > -    if (Width == SMM_IO_UINT8) {
> > -      *Uint8Buffer = IoRead8 ((UINTN)Address);
> > -    } else if (Width == SMM_IO_UINT16) {
> > -      *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);
> > -    } else if (Width == SMM_IO_UINT32) {
> > -      *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);
> > -    }
> > -  }
> > -
> > -  return EFI_SUCCESS;
> > -}
> > -
> > -/**
> > -  Write I/O registers.
> > -
> > -  The I/O operations are carried out exactly as requested.  The caller is
> > -  responsible for any alignment and I/O width issues that the bus, device,
> > -  platform, or type of I/O might require.
> > -
> > -  @param[in]  This     The EFI_SMM_CPU_IO2_PROTOCOL instance.
> > -  @param[in]  Width    Signifies the width of the I/O operations.
> > -  @param[in]  Address  The base address of the I/O operations.  The caller is
> > -                       responsible for aligning the Address if required.
> > -  @param[in]  Count    The number of I/O operations to perform.
> > -  @param[in]  Buffer   For read operations, the destination buffer to store
> > -                       the results.  For write operations, the source buffer
> > -                       from which to write data.
> > -
> > -  @retval EFI_SUCCESS            The data was read from or written to the device.
> > -  @retval EFI_UNSUPPORTED        The Address is not valid for this system.
> > -  @retval EFI_INVALID_PARAMETER  Width or Count, or both, were invalid.
> > -  @retval EFI_OUT_OF_RESOURCES   The request could not be completed due to a
> > -                                 lack of resources
> > -
> > -**/
> > -EFI_STATUS
> > -EFIAPI
> > -CpuIoServiceWrite (
> > -  IN CONST EFI_SMM_CPU_IO2_PROTOCOL  *This,
> > -  IN EFI_SMM_IO_WIDTH                Width,
> > -  IN UINT64                          Address,
> > -  IN UINTN                           Count,
> > -  IN VOID                            *Buffer
> > -  )
> > -{
> > -  EFI_STATUS  Status;
> > -  UINT8       Stride;
> > -  UINT8       *Uint8Buffer;
> > -
> > -  //
> > -  // Make sure the parameters are valid
> > -  //
> > -  Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
> > -  if (EFI_ERROR (Status)) {
> > -    return Status;
> > -  }
> > -
> > -  //
> > -  // Select loop based on the width of the transfer
> > -  //
> > -  Stride = mStride[Width];
> > -  for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {
> > -    if (Width == SMM_IO_UINT8) {
> > -      IoWrite8 ((UINTN)Address, *Uint8Buffer);
> > -    } else if (Width == SMM_IO_UINT16) {
> > -      IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
> > -    } else if (Width == SMM_IO_UINT32) {
> > -      IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
> > -    }
> > -  }
> > -
> > -  return EFI_SUCCESS;
> > -}
> > +#include "CpuIo2Common.h"
> >  
> >  /**
> >    The module Entry Point SmmCpuIoProtocol driver
> > @@ -380,28 +22,10 @@ CpuIoServiceWrite (
> >  **/
> >  EFI_STATUS
> >  EFIAPI
> > -SmmCpuIo2Initialize (
> > +TraditionalMmCpuIo2Initialize (
> >    IN EFI_HANDLE        ImageHandle,
> >    IN EFI_SYSTEM_TABLE  *SystemTable
> >    )
> >  {
> > -  EFI_STATUS  Status;
> > -
> > -  //
> > -  // Copy the SMM CPU I/O Protocol instance into the System Management System Table
> > -  //
> > -  CopyMem (&gSmst->SmmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));
> > -
> > -  //
> > -  // Install the SMM CPU I/O Protocol into the SMM protocol database
> > -  //
> > -  Status = gSmst->SmmInstallProtocolInterface (
> > -                    &mHandle,
> > -                    &gEfiSmmCpuIo2ProtocolGuid,
> > -                    EFI_NATIVE_INTERFACE,
> > -                    &mSmmCpuIo2
> > -                    );
> > -  ASSERT_EFI_ERROR (Status);
> > -
> > -  return Status;
> > +  return CommonCpuIo2Initialize ();
> >  }
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
> > new file mode 100644
> > index 000000000000..2978f82eb815
> > --- /dev/null
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.c
> > @@ -0,0 +1,31 @@
> > +/** @file
> > +  Produces the SMM CPU I/O Protocol.
> > +
> > +Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
> > +SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <PiMm.h>
> > +
> > +#include "CpuIo2Common.h"
> > +
> > +/**
> > +  The module Entry Point SmmCpuIoProtocol driver
> > +
> > +  @param[in] ImageHandle  The firmware allocated handle for the EFI image.
> > +  @param[in] SystemTable  A pointer to the EFI System Table.
> > +
> > +  @retval EFI_SUCCESS  The entry point is executed successfully.
> > +  @retval Other        Some error occurs when executing this entry point.
> > +
> > +**/
> > +EFI_STATUS
> > +EFIAPI
> > +StandaloneMmCpuIo2Initialize (
> > +  IN EFI_HANDLE           ImageHandle,
> > +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
> > +  )
> > +{
> > +  return CommonCpuIo2Initialize ();
> > +}
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> > similarity index 92%
> > rename from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
> > rename to UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> > index 4c133b58c9f4..6759cc1c8777 100644
> > --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.h
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Common.h
> > @@ -9,14 +9,14 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
> >  #ifndef _CPU_IO2_SMM_H_
> >  #define _CPU_IO2_SMM_H_
> >  
> > -#include <PiSmm.h>
> > +#include <PiMm.h>
> >  
> >  #include <Protocol/SmmCpuIo2.h>
> >  
> >  #include <Library/BaseLib.h>
> >  #include <Library/DebugLib.h>
> >  #include <Library/IoLib.h>
> > -#include <Library/SmmServicesTableLib.h>
> > +#include <Library/MmServicesTableLib.h>
> >  #include <Library/BaseMemoryLib.h>
> >  
> >  #define MAX_IO_PORT_ADDRESS   0xFFFF
> > @@ -153,4 +153,17 @@ CpuIoServiceWrite (
> >    IN VOID                            *Buffer
> >    );
> >  
> > +
> > +/**
> > +  The module Entry Point SmmCpuIoProtocol driver
> > +
> > +  @retval EFI_SUCCESS  The entry point is executed successfully.
> > +  @retval Other        Some error occurs when executing this entry point.
> > +
> > +**/
> > +EFI_STATUS
> > +CommonCpuIo2Initialize (
> > +  VOID
> > +  );
> > +
> >  #endif
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > index bc78fa4e42d2..031db1d100b9 100644
> > --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > @@ -14,7 +14,7 @@ [Defines]
> >    MODULE_TYPE                    = DXE_SMM_DRIVER
> >    VERSION_STRING                 = 1.0
> >    PI_SPECIFICATION_VERSION       = 0x0001000A
> > -  ENTRY_POINT                    = SmmCpuIo2Initialize
> > +  ENTRY_POINT                    = TraditionalMmCpuIo2Initialize
> >  
> >  #
> >  # The following information is for reference only and not required by the build tools.
> > @@ -24,7 +24,8 @@ [Defines]
> >  
> >  [Sources]
> >    CpuIo2Smm.c
> > -  CpuIo2Smm.h
> > +  CpuIo2Common.c
> > +  CpuIo2Common.h
> >  
> >  [Packages]
> >    MdePkg/MdePkg.dec
> > @@ -34,7 +35,7 @@ [LibraryClasses]
> >    BaseLib
> >    DebugLib
> >    IoLib
> > -  SmmServicesTableLib
> > +  MmServicesTableLib
> >    BaseMemoryLib
> >  
> >  [Protocols]
> > diff --git a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> > similarity index 56%
> > copy from UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > copy to UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> > index bc78fa4e42d2..cce7742bee7a 100644
> > --- a/UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > +++ b/UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> > @@ -8,13 +8,12 @@
> >  
> >  [Defines]
> >    INF_VERSION                    = 0x00010005
> > -  BASE_NAME                      = CpuIo2Smm
> > -  MODULE_UNI_FILE                = CpuIo2Smm.uni
> > -  FILE_GUID                      = A47EE2D8-F60E-42fd-8E58-7BD65EE4C29B
> > -  MODULE_TYPE                    = DXE_SMM_DRIVER
> > +  BASE_NAME                      = CpuIo2StandaloneMm
> > +  FILE_GUID                      = E3121A26-BB1C-4A18-8E23-2EA3F0412248
> > +  MODULE_TYPE                    = MM_STANDALONE
> >    VERSION_STRING                 = 1.0
> > -  PI_SPECIFICATION_VERSION       = 0x0001000A
> > -  ENTRY_POINT                    = SmmCpuIo2Initialize
> > +  PI_SPECIFICATION_VERSION       = 0x00010032
> > +  ENTRY_POINT                    = StandaloneMmCpuIo2Initialize
> >  
> >  #
> >  # The following information is for reference only and not required by the build tools.
> > @@ -23,18 +22,19 @@ [Defines]
> >  #
> >  
> >  [Sources]
> > -  CpuIo2Smm.c
> > -  CpuIo2Smm.h
> > +  CpuIo2StandaloneMm.c
> > +  CpuIo2Common.c
> > +  CpuIo2Common.h
> >  
> >  [Packages]
> >    MdePkg/MdePkg.dec
> >  
> >  [LibraryClasses]
> > -  UefiDriverEntryPoint
> > +  StandaloneMmDriverEntryPoint
> >    BaseLib
> >    DebugLib
> >    IoLib
> > -  SmmServicesTableLib
> > +  MmServicesTableLib
> >    BaseMemoryLib
> >  
> >  [Protocols]
> > @@ -42,6 +42,3 @@ [Protocols]
> >  
> >  [Depex]
> >    TRUE
> > -
> > -[UserExtensions.TianoCore."ExtraFiles"]
> > -  CpuIo2SmmExtra.uni
> > diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
> > index b2b6d78a71b0..4e46c600788f 100644
> > --- a/UefiCpuPkg/UefiCpuPkg.dsc
> > +++ b/UefiCpuPkg/UefiCpuPkg.dsc
> > @@ -38,6 +38,7 @@ [LibraryClasses]
> >    UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
> >    UefiBootServicesTableLib|MdePkg/Library/UefiBootServicesTableLib/UefiBootServicesTableLib.inf
> >    UefiDriverEntryPoint|MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
> > +  StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint/StandaloneMmDriverEntryPoint.inf
> >    DxeServicesTableLib|MdePkg/Library/DxeServicesTableLib/DxeServicesTableLib.inf
> >    PeimEntryPoint|MdePkg/Library/PeimEntryPoint/PeimEntryPoint.inf
> >    PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
> > @@ -89,10 +90,14 @@ [LibraryClasses.common.DXE_DRIVER]
> >  
> >  [LibraryClasses.common.DXE_SMM_DRIVER]
> >    SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
> > +  MmServicesTableLib|MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
> >    MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
> >    HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
> >    CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
> >  
> > +[LibraryClasses.common.MM_STANDALONE]
> > +  MmServicesTableLib|MdePkg/Library/StandaloneMmServicesTableLib/StandaloneMmServicesTableLib.inf
> > +
> >  [LibraryClasses.common.UEFI_APPLICATION]
> >    UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiApplicationEntryPoint.inf
> >    MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
> > @@ -121,6 +126,7 @@ [Components.IA32, Components.X64]
> >        NULL|UefiCpuPkg/Library/CpuCommonFeaturesLib/CpuCommonFeaturesLib.inf
> >    }
> >    UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.inf
> > +  UefiCpuPkg/CpuIo2Smm/CpuIo2StandaloneMm.inf
> >    UefiCpuPkg/CpuMpPei/CpuMpPei.inf
> >    UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
> >    UefiCpuPkg/Library/BaseUefiCpuLib/BaseUefiCpuLib.inf
> > 
> 

^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib
       [not found] ` <16576B257D31F1E6.24224@groups.io>
@ 2021-01-12  3:24   ` Kun Qin
  0 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-12  3:24 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ray Ni

[-- Attachment #1: Type: text/plain, Size: 10540 bytes --]

Hi Ray,

Is there any feedback on this patch before I send out v3 patch series? Any input is appreciated.

Regards,
Kun

From: Kun Qin<mailto:kun.q@outlook.com>
Sent: Tuesday, January 5, 2021 11:00
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
Cc: Ray Ni<mailto:ray.ni@intel.com>
Subject: [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib

This change added a new instance of AcpiTimerLib for StandaloneMm core
and drivers. It centralizes the common routines into shared files and
abstract the library constructor into corresponding files to accommodate
each constructor function prototypes.

Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - Removed "EFIAPI" for internal functions.

 PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.c => CommonAcpiTimerLib.c}         |  9 +--
 PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c                                   | 81 +-------------------
 PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c                            | 31 ++++++++
 PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h                                | 24 ++++++
 PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf                                 |  2 +
 PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.inf => StandaloneAcpiTimerLib.inf} | 19 +++--
 PcAtChipsetPkg/PcAtChipsetPkg.dsc                                                       |  1 +
 7 files changed, 74 insertions(+), 93 deletions(-)

diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
similarity index 86%
copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
copy to PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
index 3ad831b15e8a..0e401194d01d 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
@@ -72,17 +72,12 @@ InternalGetPerformanceCounterFrequency (
 /**
   The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.

-  @param  ImageHandle   The firmware allocated handle for the EFI image.
-  @param  SystemTable   A pointer to the EFI System Table.
-
   @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.

 **/
 EFI_STATUS
-EFIAPI
-DxeAcpiTimerLibConstructor (
-  IN EFI_HANDLE        ImageHandle,
-  IN EFI_SYSTEM_TABLE  *SystemTable
+CommonAcpiTimerLibConstructor (
+  VOID
   )
 {
   EFI_HOB_GUID_TYPE   *GuidHob;
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
index 3ad831b15e8a..e519ad8ed07e 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
@@ -2,72 +2,14 @@
   ACPI Timer implements one instance of Timer Library.

   Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
   SPDX-License-Identifier: BSD-2-Clause-Patent

 **/

 #include <PiDxe.h>
-#include <Library/TimerLib.h>
-#include <Library/BaseLib.h>
-#include <Library/HobLib.h>

-extern GUID mFrequencyHobGuid;
-
-/**
-  The constructor function enables ACPI IO space.
-
-  If ACPI I/O space not enabled, this function will enable it.
-  It will always return RETURN_SUCCESS.
-
-  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
-
-**/
-RETURN_STATUS
-EFIAPI
-AcpiTimerLibConstructor (
-  VOID
-  );
-
-/**
-  Calculate TSC frequency.
-
-  The TSC counting frequency is determined by comparing how far it counts
-  during a 101.4 us period as determined by the ACPI timer.
-  The ACPI timer is used because it counts at a known frequency.
-  The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
-  or 101.4 us. The TSC is then sampled again. The difference multiplied by
-  9861 is the TSC frequency. There will be a small error because of the
-  overhead of reading the ACPI timer. An attempt is made to determine and
-  compensate for this error.
-
-  @return The number of TSC counts per second.
-
-**/
-UINT64
-InternalCalculateTscFrequency (
-  VOID
-  );
-
-//
-// Cached performance counter frequency
-//
-UINT64  mPerformanceCounterFrequency = 0;
-
-/**
-  Internal function to retrieves the 64-bit frequency in Hz.
-
-  Internal function to retrieves the 64-bit frequency in Hz.
-
-  @return The frequency in Hz.
-
-**/
-UINT64
-InternalGetPerformanceCounterFrequency (
-  VOID
-  )
-{
-  return  mPerformanceCounterFrequency;
-}
+#include "CommonAcpiTimerLib.h"

 /**
   The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
@@ -85,22 +27,5 @@ DxeAcpiTimerLibConstructor (
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_HOB_GUID_TYPE   *GuidHob;
-
-  //
-  // Enable ACPI IO space.
-  //
-  AcpiTimerLibConstructor ();
-
-  //
-  // Initialize PerformanceCounterFrequency
-  //
-  GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
-  if (GuidHob != NULL) {
-    mPerformanceCounterFrequency = *(UINT64*)GET_GUID_HOB_DATA (GuidHob);
-  } else {
-    mPerformanceCounterFrequency = InternalCalculateTscFrequency ();
-  }
-
-  return EFI_SUCCESS;
+  return CommonAcpiTimerLibConstructor ();
 }
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
new file mode 100644
index 000000000000..8509f94ba7cb
--- /dev/null
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
@@ -0,0 +1,31 @@
+/** @file
+  ACPI Timer implements one instance of Timer Library.
+
+  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiMm.h>
+
+#include "CommonAcpiTimerLib.h"
+
+/**
+  The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+StandaloneMmAcpiTimerLibConstructor (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  return CommonAcpiTimerLibConstructor ();
+}
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
new file mode 100644
index 000000000000..1bbdaef04cce
--- /dev/null
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
@@ -0,0 +1,24 @@
+/** @file
+  Header file internal to ACPI TimerLib.
+
+Copyright (c) Microsoft Corporation.
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#ifndef _COMMON_ACPI_TIMER_LIB_H_
+#define _COMMON_ACPI_TIMER_LIB_H_
+
+/**
+  The constructor function enables ACPI IO space, and caches PerformanceCounterFrequency.
+
+  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
+
+**/
+EFI_STATUS
+CommonAcpiTimerLibConstructor (
+  VOID
+  );
+
+#endif
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
index d86356f4ff17..bbf14b3b5b53 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
@@ -25,6 +25,8 @@ [Defines]
 [Sources]
   AcpiTimerLib.c
   DxeAcpiTimerLib.c
+  CommonAcpiTimerLib.c
+  CommonAcpiTimerLib.h

 [Packages]
   MdePkg/MdePkg.dec
diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
similarity index 70%
copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
copy to PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
index d86356f4ff17..aa8f0012f744 100644
--- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
+++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
@@ -1,5 +1,5 @@
 ## @file
-#  DXE ACPI Timer Library
+#  Standalone MM ACPI Timer Library
 #
 #  Provides basic timer support using the ACPI timer hardware.  The performance
 #  counter features are provided by the processors time stamp counter.
@@ -8,23 +8,26 @@
 #  is compatible with both 24-bit and 32-bit ACPI timers.
 #
 #  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) Microsoft Corporation.
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##

 [Defines]
   INF_VERSION                    = 0x00010005
-  BASE_NAME                      = DxeAcpiTimerLib
-  FILE_GUID                      = E624B98C-845A-4b94-9B50-B20475D552B9
-  MODULE_TYPE                    = DXE_DRIVER
+  BASE_NAME                      = StandaloneAcpiTimerLib
+  FILE_GUID                      = C771858D-AF09-4D1A-B2F3-C7F081C3F076
+  MODULE_TYPE                    = MM_STANDALONE
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = TimerLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE
-  CONSTRUCTOR                    = DxeAcpiTimerLibConstructor
-  MODULE_UNI_FILE                = DxeAcpiTimerLib.uni
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  LIBRARY_CLASS                  = TimerLib|MM_CORE_STANDALONE MM_STANDALONE
+  CONSTRUCTOR                    = StandaloneMmAcpiTimerLibConstructor

 [Sources]
   AcpiTimerLib.c
-  DxeAcpiTimerLib.c
+  StandaloneAcpiTimerLib.c
+  CommonAcpiTimerLib.c
+  CommonAcpiTimerLib.h

 [Packages]
   MdePkg/MdePkg.dec
diff --git a/PcAtChipsetPkg/PcAtChipsetPkg.dsc b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
index b61b7d1f528e..8862ca7a0006 100644
--- a/PcAtChipsetPkg/PcAtChipsetPkg.dsc
+++ b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
@@ -53,6 +53,7 @@ [Components]
   PcAtChipsetPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf
   PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
   PcAtChipsetPkg/Library/AcpiTimerLib/PeiAcpiTimerLib.inf
+  PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
   PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.inf

 [BuildOptions]
--
2.30.0.windows.1







[-- Attachment #2: Type: text/html, Size: 17101 bytes --]

^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm
       [not found] ` <16576B256B69BE5C.24224@groups.io>
@ 2021-01-12  3:26   ` Kun Qin
  2021-01-13  1:14     ` 回复: " gaoliming
  0 siblings, 1 reply; 34+ messages in thread
From: Kun Qin @ 2021-01-12  3:26 UTC (permalink / raw)
  To: devel@edk2.groups.io, Michael D Kinney, 'Liming Gao',
	Zhiguang Liu

[-- Attachment #1: Type: text/plain, Size: 10574 bytes --]

Hi Michael/Liming/Zhiguang,

Do you have any suggestions/feedback on this specific patch? It would be helpful if I can coalesce the changes before sending out v3 patch.

Thanks in advance.

Regards,
Kun

From: Kun Qin<mailto:kun.q@outlook.com>
Sent: Tuesday, January 5, 2021 11:00
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
Cc: Michael D Kinney<mailto:michael.d.kinney@intel.com>; Liming Gao<mailto:gaoliming@byosoft.com.cn>; Zhiguang Liu<mailto:zhiguang.liu@intel.com>
Subject: [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm

This change added an instance of UefiDevicePathLib for StandaloneMm. It
abstracts DevicePathFromHandle function into different files for
Standalone MM and other instances to avoid linking gBS into MM_STANDALONE
drivers.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>

Signed-off-by: Kun Qin <kun.q@outlook.com>
---

Notes:
    v2:
    - No review, no change.

 MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c                                        | 33 -------------
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c                                  | 51 ++++++++++++++++++++
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c                            | 40 +++++++++++++++
 MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf                                        |  1 +
 MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf              |  1 +
 MdePkg/Library/UefiDevicePathLib/{UefiDevicePathLib.inf => UefiDevicePathLibStandaloneMm.inf} | 11 +++--
 MdePkg/MdePkg.dsc                                                                             |  1 +
 7 files changed, 100 insertions(+), 38 deletions(-)

diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
index 9274ef8dda98..7d5fb18d2516 100644
--- a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
@@ -806,39 +806,6 @@ UefiDevicePathLibIsDevicePathMultiInstance (
 }


-/**
-  Retrieves the device path protocol from a handle.
-
-  This function returns the device path protocol from the handle specified by Handle.
-  If Handle is NULL or Handle does not contain a device path protocol, then NULL
-  is returned.
-
-  @param  Handle                     The handle from which to retrieve the device
-                                     path protocol.
-
-  @return The device path protocol from the handle specified by Handle.
-
-**/
-EFI_DEVICE_PATH_PROTOCOL *
-EFIAPI
-DevicePathFromHandle (
-  IN EFI_HANDLE                      Handle
-  )
-{
-  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
-  EFI_STATUS                Status;
-
-  Status = gBS->HandleProtocol (
-                  Handle,
-                  &gEfiDevicePathProtocolGuid,
-                  (VOID *) &DevicePath
-                  );
-  if (EFI_ERROR (Status)) {
-    DevicePath = NULL;
-  }
-  return DevicePath;
-}
-
 /**
   Allocates a device path for a file and appends it to an existing device path.

diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
new file mode 100644
index 000000000000..7f3b6076ef34
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
@@ -0,0 +1,51 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
+  EFI_STATUS                Status;
+
+  Status = gBS->HandleProtocol (
+                  Handle,
+                  &gEfiDevicePathProtocolGuid,
+                  (VOID *) &DevicePath
+                  );
+  if (EFI_ERROR (Status)) {
+    DevicePath = NULL;
+  }
+  return DevicePath;
+}
diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
new file mode 100644
index 000000000000..930e778d373a
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
@@ -0,0 +1,40 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  return NULL;
+}
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
index 3c41c0695a72..eb85a54a74c3 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
@@ -26,6 +26,7 @@ [Defines]

 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
index e812e3e1d41e..81323bc70061 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
@@ -29,6 +29,7 @@ [Defines]

 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLibOptionalDevicePathProtocol.c
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
similarity index 79%
copy from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
copy to MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
index 3c41c0695a72..23fedf38b7eb 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
@@ -4,6 +4,7 @@
 # Device Path Library that layers on top of the Memory Allocation Library.
 #
 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -14,10 +15,11 @@ [Defines]
   INF_VERSION                    = 0x00010005
   BASE_NAME                      = UefiDevicePathLib
   MODULE_UNI_FILE                = UefiDevicePathLib.uni
-  FILE_GUID                      = 91c1677a-e57f-4191-8b8e-eb7711a716e0
-  MODULE_TYPE                    = UEFI_DRIVER
+  FILE_GUID                      = D8E58437-44D3-4154-B7A7-EB794923EF12
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = DevicePathLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE MM_STANDALONE
+  LIBRARY_CLASS                  = DevicePathLib | MM_STANDALONE MM_CORE_STANDALONE


 #
@@ -26,6 +28,7 @@ [Defines]

 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesStandaloneMm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
@@ -34,7 +37,6 @@ [Sources]
 [Packages]
   MdePkg/MdePkg.dec

-
 [LibraryClasses]
   BaseLib
   MemoryAllocationLib
@@ -71,4 +73,3 @@ [Protocols]

 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdMaximumDevicePathNodeCount    ## SOMETIMES_CONSUMES
-
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 8d1a5b20c9cd..ce009086815f 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -109,6 +109,7 @@ [Components]
   MdePkg/Library/UefiDebugLibDebugPortProtocol/UefiDebugLibDebugPortProtocol.inf
   MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+  MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
   MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol.inf
   MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePathProtocol.inf
   MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
--
2.30.0.windows.1







[-- Attachment #2: Type: text/html, Size: 18684 bytes --]

^ permalink raw reply related	[flat|nested] 34+ messages in thread

* 回复: [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm
  2021-01-12  3:26   ` [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm Kun Qin
@ 2021-01-13  1:14     ` gaoliming
  0 siblings, 0 replies; 34+ messages in thread
From: gaoliming @ 2021-01-13  1:14 UTC (permalink / raw)
  To: devel, kun.q, 'Michael D Kinney', 'Liming Gao',
	'Zhiguang Liu'

[-- Attachment #1: Type: text/plain, Size: 11141 bytes --]

Kun:

 I am OK for this patch. Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>

 

Thanks

Liming

发件人: bounce+27952+70137+4905953+8761045@groups.io
<bounce+27952+70137+4905953+8761045@groups.io> 代表 Kun Qin
发送时间: 2021年1月12日 11:27
收件人: devel@edk2.groups.io; Michael D Kinney <michael.d.kinney@intel.com>;
'Liming Gao' <liming.gao@intel.com>; Zhiguang Liu <zhiguang.liu@intel.com>
主题: Re: [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support
UefiDevicePathLib under StandaloneMm

 

Hi Michael/Liming/Zhiguang,

 

Do you have any suggestions/feedback on this specific patch? It would be
helpful if I can coalesce the changes before sending out v3 patch.

 

Thanks in advance.

 

Regards,

Kun

 

From: Kun Qin <mailto:kun.q@outlook.com> 
Sent: Tuesday, January 5, 2021 11:00
To: devel@edk2.groups.io <mailto:devel@edk2.groups.io> 
Cc: Michael D Kinney <mailto:michael.d.kinney@intel.com> ; Liming Gao
<mailto:gaoliming@byosoft.com.cn> ; Zhiguang Liu <mailto:zhiguang.liu@intel.
com> 
Subject: [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support
UefiDevicePathLib under StandaloneMm

 

This change added an instance of UefiDevicePathLib for StandaloneMm. It
abstracts DevicePathFromHandle function into different files for
Standalone MM and other instances to avoid linking gBS into MM_STANDALONE
drivers.

Cc: Michael D Kinney <michael.d.kinney@intel.com
<mailto:michael.d.kinney@intel.com> >
Cc: Liming Gao <gaoliming@byosoft.com.cn <mailto:gaoliming@byosoft.com.cn> >
Cc: Zhiguang Liu <zhiguang.liu@intel.com <mailto:zhiguang.liu@intel.com> >

Signed-off-by: Kun Qin <kun.q@outlook.com <mailto:kun.q@outlook.com> >
---

Notes:
    v2:
    - No review, no change.

 MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
| 33 -------------
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
| 51 ++++++++++++++++++++
 MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
| 40 +++++++++++++++
 MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
|  1 +
 
MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol
.inf              |  1 +
 MdePkg/Library/UefiDevicePathLib/{UefiDevicePathLib.inf =>
UefiDevicePathLibStandaloneMm.inf} | 11 +++--
 MdePkg/MdePkg.dsc
|  1 +
 7 files changed, 100 insertions(+), 38 deletions(-)

diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
index 9274ef8dda98..7d5fb18d2516 100644
--- a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilities.c
@@ -806,39 +806,6 @@ UefiDevicePathLibIsDevicePathMultiInstance (
 }
 
 
-/**
-  Retrieves the device path protocol from a handle.
-
-  This function returns the device path protocol from the handle specified
by Handle.
-  If Handle is NULL or Handle does not contain a device path protocol, then
NULL
-  is returned.
-
-  @param  Handle                     The handle from which to retrieve the
device
-                                     path protocol.
-
-  @return The device path protocol from the handle specified by Handle.
-
-**/
-EFI_DEVICE_PATH_PROTOCOL *
-EFIAPI
-DevicePathFromHandle (
-  IN EFI_HANDLE                      Handle
-  )
-{
-  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
-  EFI_STATUS                Status;
-
-  Status = gBS->HandleProtocol (
-                  Handle,
-                  &gEfiDevicePathProtocolGuid,
-                  (VOID *) &DevicePath
-                  );
-  if (EFI_ERROR (Status)) {
-    DevicePath = NULL;
-  }
-  return DevicePath;
-}
-
 /**
   Allocates a device path for a file and appends it to an existing device
path.
 
diff --git a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
new file mode 100644
index 000000000000..7f3b6076ef34
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesDxeSmm.c
@@ -0,0 +1,51 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out
of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is
sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified
by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then
NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the
device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
+  EFI_STATUS                Status;
+
+  Status = gBS->HandleProtocol (
+                  Handle,
+                  &gEfiDevicePathProtocolGuid,
+                  (VOID *) &DevicePath
+                  );
+  if (EFI_ERROR (Status)) {
+    DevicePath = NULL;
+  }
+  return DevicePath;
+}
diff --git
a/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
new file mode 100644
index 000000000000..930e778d373a
--- /dev/null
+++ b/MdePkg/Library/UefiDevicePathLib/DevicePathUtilitiesStandaloneMm.c
@@ -0,0 +1,40 @@
+/** @file
+  Device Path services. The thing to remember is device paths are built out
of
+  nodes. The device path is terminated by an end node that is length
+  sizeof(EFI_DEVICE_PATH_PROTOCOL). That would be why there is
sizeof(EFI_DEVICE_PATH_PROTOCOL)
+  all over this file.
+
+  The only place where multi-instance device paths are supported is in
+  environment varibles. Multi-instance device paths should never be placed
+  on a Handle.
+
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) Microsoft Corporation.
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "UefiDevicePathLib.h"
+
+
+/**
+  Retrieves the device path protocol from a handle.
+
+  This function returns the device path protocol from the handle specified
by Handle.
+  If Handle is NULL or Handle does not contain a device path protocol, then
NULL
+  is returned.
+
+  @param  Handle                     The handle from which to retrieve the
device
+                                     path protocol.
+
+  @return The device path protocol from the handle specified by Handle.
+
+**/
+EFI_DEVICE_PATH_PROTOCOL *
+EFIAPI
+DevicePathFromHandle (
+  IN EFI_HANDLE                      Handle
+  )
+{
+  return NULL;
+}
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
index 3c41c0695a72..eb85a54a74c3 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
@@ -26,6 +26,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
diff --git
a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtoc
ol.inf
b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtoc
ol.inf
index e812e3e1d41e..81323bc70061 100644
---
a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtoc
ol.inf
+++
b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtoc
ol.inf
@@ -29,6 +29,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesDxeSmm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLibOptionalDevicePathProtocol.c
diff --git a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
similarity index 79%
copy from MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
copy to MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
index 3c41c0695a72..23fedf38b7eb 100644
--- a/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+++ b/MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
@@ -4,6 +4,7 @@
 # Device Path Library that layers on top of the Memory Allocation Library.
 #
 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) Microsoft Corporation.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -14,10 +15,11 @@ [Defines]
   INF_VERSION                    = 0x00010005
   BASE_NAME                      = UefiDevicePathLib
   MODULE_UNI_FILE                = UefiDevicePathLib.uni
-  FILE_GUID                      = 91c1677a-e57f-4191-8b8e-eb7711a716e0
-  MODULE_TYPE                    = UEFI_DRIVER
+  FILE_GUID                      = D8E58437-44D3-4154-B7A7-EB794923EF12
+  MODULE_TYPE                    = MM_STANDALONE
+  PI_SPECIFICATION_VERSION       = 0x00010032
   VERSION_STRING                 = 1.0
-  LIBRARY_CLASS                  = DevicePathLib|DXE_CORE DXE_DRIVER
DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE
MM_STANDALONE
+  LIBRARY_CLASS                  = DevicePathLib | MM_STANDALONE
MM_CORE_STANDALONE
 
 
 #
@@ -26,6 +28,7 @@ [Defines]
 
 [Sources]
   DevicePathUtilities.c
+  DevicePathUtilitiesStandaloneMm.c
   DevicePathToText.c
   DevicePathFromText.c
   UefiDevicePathLib.c
@@ -34,7 +37,6 @@ [Sources]
 [Packages]
   MdePkg/MdePkg.dec
 
-
 [LibraryClasses]
   BaseLib
   MemoryAllocationLib
@@ -71,4 +73,3 @@ [Protocols]
 
 [Pcd]
   gEfiMdePkgTokenSpaceGuid.PcdMaximumDevicePathNodeCount    ##
SOMETIMES_CONSUMES
-
diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 8d1a5b20c9cd..ce009086815f 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -109,6 +109,7 @@ [Components]
 
MdePkg/Library/UefiDebugLibDebugPortProtocol/UefiDebugLibDebugPortProtocol.i
nf
   MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf
   MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
+  MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibStandaloneMm.inf
 
MdePkg/Library/UefiDevicePathLib/UefiDevicePathLibOptionalDevicePathProtocol
.inf
 
MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLibDevicePa
thProtocol.inf
   MdePkg/Library/UefiDriverEntryPoint/UefiDriverEntryPoint.inf
-- 
2.30.0.windows.1






 




[-- Attachment #2: Type: text/html, Size: 21029 bytes --]

^ permalink raw reply related	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib
  2021-01-05 18:59 ` [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib Kun Qin
@ 2021-01-13  6:50   ` Ni, Ray
  2021-01-13 18:38     ` Kun Qin
  0 siblings, 1 reply; 34+ messages in thread
From: Ni, Ray @ 2021-01-13  6:50 UTC (permalink / raw)
  To: devel@edk2.groups.io, kun.q@outlook.com

1. With this change, there will be two common C files which look confusing to me:
AcpiTimerLib.c
CommonAcpiTimerLib.c

With a deep look, the first one is shared by Base/Pei/Dxe instances.
The second one is shared by Dxe and StandaloneMm instances.
Can you rename CommonAcpiTimerLib.c to DxeStandaloneMmAcpiTimerLib.c?
Or do you have any better idea to avoid the confusion?

2. Can you rename the BASE NAME from "StandaloneAcpiTimerLib" to
"StandaloneMmAcpiTimerLib"? I am not sure everyone could map "Standalone"
to "StandaloneMm".

Thanks,
Ray

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Kun Qin
> Sent: Wednesday, January 6, 2021 3:00 AM
> To: devel@edk2.groups.io
> Cc: Ni, Ray <ray.ni@intel.com>
> Subject: [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added
> StandaloneMm instance of AcpiTimerLib
> 
> This change added a new instance of AcpiTimerLib for StandaloneMm core
> and drivers. It centralizes the common routines into shared files and
> abstract the library constructor into corresponding files to accommodate
> each constructor function prototypes.
> 
> Cc: Ray Ni <ray.ni@intel.com>
> 
> Signed-off-by: Kun Qin <kun.q@outlook.com>
> ---
> 
> Notes:
>     v2:
>     - Removed "EFIAPI" for internal functions.
> 
>  PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.c =>
> CommonAcpiTimerLib.c}         |  9 +--
>  PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> | 81 +-------------------
>  PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
> | 31 ++++++++
>  PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
> | 24 ++++++
>  PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> |  2 +
>  PcAtChipsetPkg/Library/AcpiTimerLib/{DxeAcpiTimerLib.inf =>
> StandaloneAcpiTimerLib.inf} | 19 +++--
>  PcAtChipsetPkg/PcAtChipsetPkg.dsc                                                       |  1 +
>  7 files changed, 74 insertions(+), 93 deletions(-)
> 
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
> similarity index 86%
> copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> copy to PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
> index 3ad831b15e8a..0e401194d01d 100644
> --- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.c
> @@ -72,17 +72,12 @@ InternalGetPerformanceCounterFrequency (
>  /**
>    The constructor function enables ACPI IO space, and caches
> PerformanceCounterFrequency.
> 
> -  @param  ImageHandle   The firmware allocated handle for the EFI image.
> -  @param  SystemTable   A pointer to the EFI System Table.
> -
>    @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
> 
>  **/
>  EFI_STATUS
> -EFIAPI
> -DxeAcpiTimerLibConstructor (
> -  IN EFI_HANDLE        ImageHandle,
> -  IN EFI_SYSTEM_TABLE  *SystemTable
> +CommonAcpiTimerLibConstructor (
> +  VOID
>    )
>  {
>    EFI_HOB_GUID_TYPE   *GuidHob;
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> index 3ad831b15e8a..e519ad8ed07e 100644
> --- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.c
> @@ -2,72 +2,14 @@
>    ACPI Timer implements one instance of Timer Library.
> 
>    Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) Microsoft Corporation.
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
>  #include <PiDxe.h>
> -#include <Library/TimerLib.h>
> -#include <Library/BaseLib.h>
> -#include <Library/HobLib.h>
> 
> -extern GUID mFrequencyHobGuid;
> -
> -/**
> -  The constructor function enables ACPI IO space.
> -
> -  If ACPI I/O space not enabled, this function will enable it.
> -  It will always return RETURN_SUCCESS.
> -
> -  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
> -
> -**/
> -RETURN_STATUS
> -EFIAPI
> -AcpiTimerLibConstructor (
> -  VOID
> -  );
> -
> -/**
> -  Calculate TSC frequency.
> -
> -  The TSC counting frequency is determined by comparing how far it counts
> -  during a 101.4 us period as determined by the ACPI timer.
> -  The ACPI timer is used because it counts at a known frequency.
> -  The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
> -  or 101.4 us. The TSC is then sampled again. The difference multiplied by
> -  9861 is the TSC frequency. There will be a small error because of the
> -  overhead of reading the ACPI timer. An attempt is made to determine and
> -  compensate for this error.
> -
> -  @return The number of TSC counts per second.
> -
> -**/
> -UINT64
> -InternalCalculateTscFrequency (
> -  VOID
> -  );
> -
> -//
> -// Cached performance counter frequency
> -//
> -UINT64  mPerformanceCounterFrequency = 0;
> -
> -/**
> -  Internal function to retrieves the 64-bit frequency in Hz.
> -
> -  Internal function to retrieves the 64-bit frequency in Hz.
> -
> -  @return The frequency in Hz.
> -
> -**/
> -UINT64
> -InternalGetPerformanceCounterFrequency (
> -  VOID
> -  )
> -{
> -  return  mPerformanceCounterFrequency;
> -}
> +#include "CommonAcpiTimerLib.h"
> 
>  /**
>    The constructor function enables ACPI IO space, and caches
> PerformanceCounterFrequency.
> @@ -85,22 +27,5 @@ DxeAcpiTimerLibConstructor (
>    IN EFI_SYSTEM_TABLE  *SystemTable
>    )
>  {
> -  EFI_HOB_GUID_TYPE   *GuidHob;
> -
> -  //
> -  // Enable ACPI IO space.
> -  //
> -  AcpiTimerLibConstructor ();
> -
> -  //
> -  // Initialize PerformanceCounterFrequency
> -  //
> -  GuidHob = GetFirstGuidHob (&mFrequencyHobGuid);
> -  if (GuidHob != NULL) {
> -    mPerformanceCounterFrequency = *(UINT64*)GET_GUID_HOB_DATA
> (GuidHob);
> -  } else {
> -    mPerformanceCounterFrequency = InternalCalculateTscFrequency ();
> -  }
> -
> -  return EFI_SUCCESS;
> +  return CommonAcpiTimerLibConstructor ();
>  }
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
> b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
> new file mode 100644
> index 000000000000..8509f94ba7cb
> --- /dev/null
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.c
> @@ -0,0 +1,31 @@
> +/** @file
> +  ACPI Timer implements one instance of Timer Library.
> +
> +  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) Microsoft Corporation.
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiMm.h>
> +
> +#include "CommonAcpiTimerLib.h"
> +
> +/**
> +  The constructor function enables ACPI IO space, and caches
> PerformanceCounterFrequency.
> +
> +  @param  ImageHandle   The firmware allocated handle for the EFI image.
> +  @param  SystemTable   A pointer to the EFI System Table.
> +
> +  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +StandaloneMmAcpiTimerLibConstructor (
> +  IN EFI_HANDLE           ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  return CommonAcpiTimerLibConstructor ();
> +}
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
> b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
> new file mode 100644
> index 000000000000..1bbdaef04cce
> --- /dev/null
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/CommonAcpiTimerLib.h
> @@ -0,0 +1,24 @@
> +/** @file
> +  Header file internal to ACPI TimerLib.
> +
> +Copyright (c) Microsoft Corporation.
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +
> +#ifndef _COMMON_ACPI_TIMER_LIB_H_
> +#define _COMMON_ACPI_TIMER_LIB_H_
> +
> +/**
> +  The constructor function enables ACPI IO space, and caches
> PerformanceCounterFrequency.
> +
> +  @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
> +
> +**/
> +EFI_STATUS
> +CommonAcpiTimerLibConstructor (
> +  VOID
> +  );
> +
> +#endif
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> index d86356f4ff17..bbf14b3b5b53 100644
> --- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> @@ -25,6 +25,8 @@ [Defines]
>  [Sources]
>    AcpiTimerLib.c
>    DxeAcpiTimerLib.c
> +  CommonAcpiTimerLib.c
> +  CommonAcpiTimerLib.h
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> diff --git a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
> similarity index 70%
> copy from PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> copy to PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
> index d86356f4ff17..aa8f0012f744 100644
> --- a/PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
> +++ b/PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
> @@ -1,5 +1,5 @@
>  ## @file
> -#  DXE ACPI Timer Library
> +#  Standalone MM ACPI Timer Library
>  #
>  #  Provides basic timer support using the ACPI timer hardware.  The
> performance
>  #  counter features are provided by the processors time stamp counter.
> @@ -8,23 +8,26 @@
>  #  is compatible with both 24-bit and 32-bit ACPI timers.
>  #
>  #  Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) Microsoft Corporation.
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
>  ##
> 
>  [Defines]
>    INF_VERSION                    = 0x00010005
> -  BASE_NAME                      = DxeAcpiTimerLib
> -  FILE_GUID                      = E624B98C-845A-4b94-9B50-B20475D552B9
> -  MODULE_TYPE                    = DXE_DRIVER
> +  BASE_NAME                      = StandaloneAcpiTimerLib
> +  FILE_GUID                      = C771858D-AF09-4D1A-B2F3-C7F081C3F076
> +  MODULE_TYPE                    = MM_STANDALONE
>    VERSION_STRING                 = 1.0
> -  LIBRARY_CLASS                  = TimerLib|DXE_CORE DXE_DRIVER
> DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER
> SMM_CORE
> -  CONSTRUCTOR                    = DxeAcpiTimerLibConstructor
> -  MODULE_UNI_FILE                = DxeAcpiTimerLib.uni
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  LIBRARY_CLASS                  = TimerLib|MM_CORE_STANDALONE
> MM_STANDALONE
> +  CONSTRUCTOR                    = StandaloneMmAcpiTimerLibConstructor
> 
>  [Sources]
>    AcpiTimerLib.c
> -  DxeAcpiTimerLib.c
> +  StandaloneAcpiTimerLib.c
> +  CommonAcpiTimerLib.c
> +  CommonAcpiTimerLib.h
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> diff --git a/PcAtChipsetPkg/PcAtChipsetPkg.dsc
> b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
> index b61b7d1f528e..8862ca7a0006 100644
> --- a/PcAtChipsetPkg/PcAtChipsetPkg.dsc
> +++ b/PcAtChipsetPkg/PcAtChipsetPkg.dsc
> @@ -53,6 +53,7 @@ [Components]
>    PcAtChipsetPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf
>    PcAtChipsetPkg/Library/AcpiTimerLib/DxeAcpiTimerLib.inf
>    PcAtChipsetPkg/Library/AcpiTimerLib/PeiAcpiTimerLib.inf
> +  PcAtChipsetPkg/Library/AcpiTimerLib/StandaloneAcpiTimerLib.inf
> 
> PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDx
> e.inf
> 
>  [BuildOptions]
> --
> 2.30.0.windows.1
> 
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 34+ messages in thread

* Re: [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib
  2021-01-13  6:50   ` [edk2-devel] " Ni, Ray
@ 2021-01-13 18:38     ` Kun Qin
  0 siblings, 0 replies; 34+ messages in thread
From: Kun Qin @ 2021-01-13 18:38 UTC (permalink / raw)
  To: Ni, Ray, devel

[-- Attachment #1: Type: text/plain, Size: 228 bytes --]

Thanks for the feedback.

How about changing the "CommonAcpiTimerLib.*" to "DxeMmAcpiTimerLib.*" to cover Dxe/Smm and StandaloneMM instances?

I will update the BASE NAME to StandaloneMmAcpiTimerLib in v3.

Regards,
Kun

[-- Attachment #2: Type: text/html, Size: 349 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2021-01-13 18:38 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20210105185935.3769-1-kun.q@outlook.com>
2021-01-05 18:59 ` [PATCH v2 01/16] StandaloneMmPkg: StandaloneMmCoreEntryPoint: Extends support for X64 Kun Qin
2021-01-05 18:59 ` [PATCH v2 02/16] StandaloneMmPkg: StandaloneMmCoreHobLib: Extend support for x64 Mm Core Kun Qin
2021-01-05 18:59 ` [PATCH v2 03/16] StandaloneMmPkg: StandaloneMmCoreMemoryAllocationLib: Fix compiler warning Kun Qin
2021-01-06  3:33   ` Yao, Jiewen
2021-01-05 18:59 ` [PATCH v2 04/16] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture Kun Qin
2021-01-06  3:38   ` Yao, Jiewen
2021-01-06  6:37     ` [edk2-devel] " Kun Qin
2021-01-05 18:59 ` [PATCH v2 05/16] MdeModulePkg: SmmLockBoxSmmLib: Support StandaloneMm for SmmLockBoxLib Kun Qin
2021-01-05 18:59 ` [PATCH v2 06/16] MdeModulePkg: SmmReportStatusCodeLib: ReportStatusCodeLib in StandaloneMm Kun Qin
2021-01-06  3:24   ` [edk2-devel] " Wu, Hao A
2021-01-05 18:59 ` [PATCH v2 07/16] MdeModulePkg: StatusCodeHandler: StatusCodeHandler driver " Kun Qin
2021-01-06  3:24   ` Wu, Hao A
2021-01-05 18:59 ` [PATCH v2 08/16] MdeModulePkg: FirmwarePerformanceDataTable: Added StandaloneMm support Kun Qin
2021-01-06  3:24   ` Wu, Hao A
2021-01-05 18:59 ` [PATCH v2 09/16] MdeModulePkg: ReportStatusCodeRouter: Support StandaloneMm RSC Router Kun Qin
2021-01-06  3:24   ` Wu, Hao A
2021-01-05 18:59 ` [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm Kun Qin
2021-01-05 18:59 ` [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib Kun Qin
2021-01-13  6:50   ` [edk2-devel] " Ni, Ray
2021-01-13 18:38     ` Kun Qin
2021-01-05 18:59 ` [PATCH v2 12/16] SecurityPkg: Tcg2PhysicalPresenceLib: Introduce StandaloneMm instance Kun Qin
2021-01-05 18:59 ` [PATCH v2 13/16] SecurityPkg: Tcg2PpVendorLibNull: Added support for MM_STANDALONE type Kun Qin
2021-01-05 18:59 ` [PATCH v2 14/16] SecurityPkg: Tpm2DeviceLibDTpm: Introduce StandaloneMm instance Kun Qin
2021-01-05 18:59 ` [PATCH v2 15/16] UefiCpuPkg: CpuIo2Smm: Support of CpuIo driver under StandaloneMm Kun Qin
2021-01-06 15:46   ` Laszlo Ersek
2021-01-06 15:51     ` Laszlo Ersek
2021-01-06 19:02       ` [edk2-devel] " Kun Qin
2021-01-06 19:07         ` Laszlo Ersek
2021-01-07 19:05     ` Leif Lindholm
2021-01-05 18:59 ` [PATCH v2 16/16] UefiCpuPkg: SmmCpuExceptionHandlerLib: Added StandaloneMm module support Kun Qin
2021-01-06 15:48   ` Laszlo Ersek
     [not found] ` <16576B257D31F1E6.24224@groups.io>
2021-01-12  3:24   ` [edk2-devel] [PATCH v2 11/16] PcAtChipsetPkg: AcpiTimerLib: Added StandaloneMm instance of AcpiTimerLib Kun Qin
     [not found] ` <16576B256B69BE5C.24224@groups.io>
2021-01-12  3:26   ` [edk2-devel] [PATCH v2 10/16] MdePkg: UefiDevicePathLib: Support UefiDevicePathLib under StandaloneMm Kun Qin
2021-01-13  1:14     ` 回复: " gaoliming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox