<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hi Jiewen,</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Do you have further concerns about this specific patch? I did created a Bugzilla ticket to track the OS memory protection concern here:
<a href="https://bugzilla.tianocore.org/show_bug.cgi?id=3168">3168 – Add non-MMRAM memory protection for Standalone MM environment (tianocore.org)</a>. It introduces a new proposal to allow access of DXE/RT regions from MMRAM, but requires non trivial change.
 Please let me know if you hold different opinions towards the proposal or this patch.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,</p>
<p class="MsoNormal">Kun</p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:kun.q@outlook.com">Kun Qin</a><br>
<b>Sent: </b>Thursday, January 14, 2021 14:36<br>
<b>To: </b><a href="mailto:devel@edk2.groups.io">devel@edk2.groups.io</a><br>
<b>Cc: </b><a href="mailto:ard.biesheuvel@arm.com">Ard Biesheuvel</a>; <a href="mailto:sami.mujawar@arm.com">
Sami Mujawar</a>; <a href="mailto:jiewen.yao@intel.com">Jiewen Yao</a>; <a href="mailto:supreeth.venkatesh@arm.com">
Supreeth Venkatesh</a><br>
<b>Subject: </b>[edk2-devel] [PATCH v3 05/18] StandaloneMmPkg: StandaloneMmMemLib: Extends support for X64 architecture</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">This change extends StandaloneMmMemLib library to support X64<br>
architecture. The implementation is ported from MdePkg/Library/SmmMemLib.<br>
<br>
Cc: Ard Biesheuvel <ard.biesheuvel@arm.com><br>
Cc: Sami Mujawar <sami.mujawar@arm.com><br>
Cc: Jiewen Yao <jiewen.yao@intel.com><br>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com><br>
<br>
Signed-off-by: Kun Qin <kun.q@outlook.com><br>
---<br>
<br>
Notes:<br>
    v3:<br>
    - Updated destructor description of varibales to pass CI build.<br>
    <br>
    v2:<br>
    - Added routine to fix bug of not initializing MmRanges [Jiewen]<br>
    - Extends support to x86 instead of x64 only [Hao]<br>
<br>
 StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c |  27 ++++<br>
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c                 |  52 +++++++<br>
 StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c      | 155 ++++++++++++++++++++<br>
 StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf               |  13 +-<br>
 4 files changed, 246 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c<br>
index cb7c5e677a6b..4124959e0435 100644<br>
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c<br>
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/AArch64/StandaloneMmMemLibInternal.c<br>
@@ -40,4 +40,31 @@ MmMemLibInternalCalculateMaximumSupportAddress (<br>
   DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));<br>
 }<br>
 <br>
+/**<br>
+  Initialize cached Mmram Ranges from HOB.<br>
+<br>
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.<br>
+  @retval EFI_SUCCESS       MmRanges are populated successfully.<br>
+<br>
+**/<br>
+EFI_STATUS<br>
+MmMemLibInternalPopulateMmramRanges (<br>
+  VOID<br>
+  )<br>
+{<br>
+  // Not implemented for AARCH64.<br>
+  return EFI_SUCCESS;<br>
+}<br>
+<br>
+/**<br>
+  Deinitialize cached Mmram Ranges.<br>
+<br>
+**/<br>
+VOID<br>
+MmMemLibInternalFreeMmramRanges (<br>
+  VOID<br>
+  )<br>
+{<br>
+  // Not implemented for AARCH64.<br>
+}<br>
 <br>
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c<br>
index b533bd8390cd..2737f95315eb 100644<br>
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c<br>
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c<br>
@@ -37,6 +37,27 @@ MmMemLibInternalCalculateMaximumSupportAddress (<br>
   VOID<br>
   );<br>
 <br>
+/**<br>
+  Initialize cached Mmram Ranges from HOB.<br>
+<br>
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.<br>
+  @retval EFI_SUCCESS       MmRanges are populated successfully.<br>
+<br>
+**/<br>
+EFI_STATUS<br>
+MmMemLibInternalPopulateMmramRanges (<br>
+  VOID<br>
+  );<br>
+<br>
+/**<br>
+  Deinitialize cached Mmram Ranges.<br>
+<br>
+**/<br>
+VOID<br>
+MmMemLibInternalFreeMmramRanges (<br>
+  VOID<br>
+  );<br>
+<br>
 /**<br>
   This function check if the buffer is valid per processor architecture and not overlap with MMRAM.<br>
 <br>
@@ -253,11 +274,42 @@ MemLibConstructor (<br>
   IN EFI_MM_SYSTEM_TABLE    *MmSystemTable<br>
   )<br>
 {<br>
+  EFI_STATUS Status;<br>
 <br>
   //<br>
   // Calculate and save maximum support address<br>
   //<br>
   MmMemLibInternalCalculateMaximumSupportAddress ();<br>
 <br>
+  //<br>
+  // Initialize cached Mmram Ranges from HOB.<br>
+  //<br>
+  Status = MmMemLibInternalPopulateMmramRanges ();<br>
+<br>
+  return Status;<br>
+}<br>
+<br>
+/**<br>
+  Destructor for Mm Mem library.<br>
+<br>
+  @param ImageHandle    The image handle of the process.<br>
+  @param MmSystemTable  The EFI System Table pointer.<br>
+<br>
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.<br>
+<br>
+**/<br>
+EFI_STATUS<br>
+EFIAPI<br>
+MemLibDestructor (<br>
+  IN EFI_HANDLE             ImageHandle,<br>
+  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable<br>
+  )<br>
+{<br>
+<br>
+  //<br>
+  // Deinitialize cached Mmram Ranges.<br>
+  //<br>
+  MmMemLibInternalFreeMmramRanges ();<br>
+<br>
   return EFI_SUCCESS;<br>
 }<br>
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c<br>
new file mode 100644<br>
index 000000000000..1a978541716a<br>
--- /dev/null<br>
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/X86StandaloneMmMemLibInternal.c<br>
@@ -0,0 +1,155 @@<br>
+/** @file<br>
+  Internal ARCH Specific file of MM memory check library.<br>
+<br>
+  MM memory check library implementation. This library consumes MM_ACCESS_PROTOCOL<br>
+  to get MMRAM information. In order to use this library instance, the platform should produce<br>
+  all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core<br>
+  and MM driver) and/or specific dedicated hardware.<br>
+<br>
+  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR><br>
+  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR><br>
+  Copyright (c) Microsoft Corporation.<br>
+<br>
+  SPDX-License-Identifier: BSD-2-Clause-Patent<br>
+<br>
+**/<br>
+#include <PiMm.h><br>
+#include <Library/BaseLib.h><br>
+#include <Library/BaseMemoryLib.h><br>
+#include <Library/MemoryAllocationLib.h><br>
+#include <Library/DebugLib.h><br>
+#include <Library/HobLib.h><br>
+<br>
+#include <Guid/MmCoreData.h><br>
+#include <Guid/MmramMemoryReserve.h><br>
+<br>
+//<br>
+// Maximum support address used to check input buffer<br>
+//<br>
+extern EFI_PHYSICAL_ADDRESS  mMmMemLibInternalMaximumSupportAddress;<br>
+extern EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;<br>
+extern UINTN                 mMmMemLibInternalMmramCount;<br>
+<br>
+/**<br>
+  Calculate and save the maximum support address.<br>
+<br>
+**/<br>
+VOID<br>
+MmMemLibInternalCalculateMaximumSupportAddress (<br>
+  VOID<br>
+  )<br>
+{<br>
+  VOID         *Hob;<br>
+  UINT32       RegEax;<br>
+  UINT8        PhysicalAddressBits;<br>
+<br>
+  //<br>
+  // Get physical address bits supported.<br>
+  //<br>
+  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);<br>
+  if (Hob != NULL) {<br>
+    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;<br>
+  } else {<br>
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);<br>
+    if (RegEax >= 0x80000008) {<br>
+      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);<br>
+      PhysicalAddressBits = (UINT8) RegEax;<br>
+    } else {<br>
+      PhysicalAddressBits = 36;<br>
+    }<br>
+  }<br>
+  //<br>
+  // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.<br>
+  //<br>
+  ASSERT (PhysicalAddressBits <= 52);<br>
+  if (PhysicalAddressBits > 48) {<br>
+    PhysicalAddressBits = 48;<br>
+  }<br>
+<br>
+  //<br>
+  // Save the maximum support address in one global variable<br>
+  //<br>
+  mMmMemLibInternalMaximumSupportAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, PhysicalAddressBits) - 1);<br>
+  DEBUG ((DEBUG_INFO, "mMmMemLibInternalMaximumSupportAddress = 0x%lx\n", mMmMemLibInternalMaximumSupportAddress));<br>
+}<br>
+<br>
+/**<br>
+  Initialize cached Mmram Ranges from HOB.<br>
+<br>
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.<br>
+  @retval EFI_SUCCESS       MmRanges are populated successfully.<br>
+<br>
+**/<br>
+EFI_STATUS<br>
+MmMemLibInternalPopulateMmramRanges (<br>
+  VOID<br>
+  )<br>
+{<br>
+  VOID                            *HobStart;<br>
+  EFI_HOB_GUID_TYPE               *GuidHob;<br>
+  MM_CORE_DATA_HOB_DATA           *DataInHob;<br>
+  MM_CORE_PRIVATE_DATA            *MmCorePrivateData;<br>
+  EFI_HOB_GUID_TYPE               *MmramRangesHob;<br>
+  EFI_MMRAM_HOB_DESCRIPTOR_BLOCK  *MmramRangesHobData;<br>
+  EFI_MMRAM_DESCRIPTOR            *MmramDescriptors;<br>
+<br>
+  HobStart = GetHobList ();<br>
+  DEBUG ((DEBUG_INFO, "%a - 0x%x\n", __FUNCTION__, HobStart));<br>
+<br>
+  //<br>
+  // Extract MM Core Private context from the Hob. If absent search for<br>
+  // a Hob containing the MMRAM ranges<br>
+  //<br>
+  GuidHob = GetNextGuidHob (&gMmCoreDataHobGuid, HobStart);<br>
+  if (GuidHob == NULL) {<br>
+    MmramRangesHob = GetFirstGuidHob (&gEfiMmPeiMmramMemoryReserveGuid);<br>
+    if (MmramRangesHob == NULL) {<br>
+      return EFI_UNSUPPORTED;<br>
+    }<br>
+<br>
+    MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob);<br>
+    if (MmramRangesHobData == NULL || MmramRangesHobData->Descriptor == NULL) {<br>
+      return EFI_UNSUPPORTED;<br>
+    }<br>
+<br>
+    mMmMemLibInternalMmramCount = MmramRangesHobData->NumberOfMmReservedRegions;<br>
+    MmramDescriptors = MmramRangesHobData->Descriptor;<br>
+  } else {<br>
+    DataInHob = GET_GUID_HOB_DATA (GuidHob);<br>
+    if (DataInHob == NULL) {<br>
+      return EFI_UNSUPPORTED;<br>
+    }<br>
+<br>
+    MmCorePrivateData = (MM_CORE_PRIVATE_DATA *) (UINTN) DataInHob->Address;<br>
+    if (MmCorePrivateData == NULL || MmCorePrivateData->MmramRanges == 0) {<br>
+      return EFI_UNSUPPORTED;<br>
+    }<br>
+<br>
+    mMmMemLibInternalMmramCount = (UINTN) MmCorePrivateData->MmramRangeCount;<br>
+    MmramDescriptors = (EFI_MMRAM_DESCRIPTOR *) (UINTN) MmCorePrivateData->MmramRanges;<br>
+  }<br>
+<br>
+  mMmMemLibInternalMmramRanges = AllocatePool (mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));<br>
+  if (mMmMemLibInternalMmramRanges) {<br>
+    CopyMem (mMmMemLibInternalMmramRanges,<br>
+             MmramDescriptors,<br>
+             mMmMemLibInternalMmramCount * sizeof (EFI_MMRAM_DESCRIPTOR));<br>
+  }<br>
+<br>
+  return EFI_SUCCESS;<br>
+}<br>
+<br>
+/**<br>
+  Deinitialize cached Mmram Ranges.<br>
+<br>
+**/<br>
+VOID<br>
+MmMemLibInternalFreeMmramRanges (<br>
+  VOID<br>
+  )<br>
+{<br>
+  if (mMmMemLibInternalMmramRanges != NULL) {<br>
+    FreePool (mMmMemLibInternalMmramRanges);<br>
+  }<br>
+}<br>
+<br>
diff --git a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf<br>
index 49da02e54e6d..062b0d7a1161 100644<br>
--- a/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf<br>
+++ b/StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf<br>
@@ -8,6 +8,7 @@<br>
 #<br>
 #  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR><br>
 #  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR><br>
+#  Copyright (c) Microsoft Corporation.<br>
 #<br>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent<br>
 #<br>
@@ -22,16 +23,20 @@ [Defines]<br>
   PI_SPECIFICATION_VERSION       = 0x00010032<br>
   LIBRARY_CLASS                  = MemLib|MM_STANDALONE MM_CORE_STANDALONE<br>
   CONSTRUCTOR                    = MemLibConstructor<br>
+  DESTRUCTOR                     = MemLibDestructor<br>
 <br>
 #<br>
 # The following information is for reference only and not required by the build tools.<br>
 #<br>
-#  VALID_ARCHITECTURES           = AARCH64<br>
+#  VALID_ARCHITECTURES           = IA32 X64 AARCH64<br>
 #<br>
 <br>
 [Sources.Common]<br>
   StandaloneMmMemLib.c<br>
 <br>
+[Sources.IA32, Sources.X64]<br>
+  X86StandaloneMmMemLibInternal.c<br>
+<br>
 [Sources.AARCH64]<br>
   AArch64/StandaloneMmMemLibInternal.c<br>
 <br>
@@ -42,3 +47,9 @@ [Packages]<br>
 [LibraryClasses]<br>
   BaseMemoryLib<br>
   DebugLib<br>
+  HobLib<br>
+  MemoryAllocationLib<br>
+<br>
+[Guids]<br>
+  gMmCoreDataHobGuid                  ## SOMETIMES_CONSUMES ## HOB<br>
+  gEfiMmPeiMmramMemoryReserveGuid     ## SOMETIMES_CONSUMES ## HOB<br>
-- <br>
2.30.0.windows.1<br>
<br>
<br>
<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr> Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/70606">View/Reply Online (#70606)</a> |    |  <a target="_blank" href="https://groups.io/mt/79688584/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>