]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
UefiCpuPkg/PiSmmCpuDxeSmm: Add SmmMemoryAttribute protocol
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / PageTbl.c
index a7d790fd8aa044662ac40608c2b293a54fd95c05..1da4b5506c965c58ce68326e48b11062042f09c7 100644 (file)
@@ -1,7 +1,9 @@
 /** @file\r
 Page Fault (#PF) handler for X64 processors\r
 \r
-Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
+\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -16,9 +18,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #define PAGE_TABLE_PAGES            8\r
 #define ACC_MAX_BIT                 BIT3\r
+\r
 LIST_ENTRY                          mPagePool = INITIALIZE_LIST_HEAD_VARIABLE (mPagePool);\r
-SPIN_LOCK                           mPFLock;\r
 BOOLEAN                             m1GPageTableSupport = FALSE;\r
+BOOLEAN                             mCpuSmmStaticPageTable;\r
 \r
 /**\r
   Check if 1-GByte pages is supported by processor or not.\r
@@ -86,6 +89,146 @@ GetSubEntriesNum (
   return BitFieldRead64 (*Entry, 52, 60);\r
 }\r
 \r
+/**\r
+  Calculate the maximum support address.\r
+\r
+  @return the maximum support address.\r
+**/\r
+UINT8\r
+CalculateMaximumSupportAddress (\r
+  VOID\r
+  )\r
+{\r
+  UINT32                                        RegEax;\r
+  UINT8                                         PhysicalAddressBits;\r
+  VOID                                          *Hob;\r
+\r
+  //\r
+  // Get physical address bits supported.\r
+  //\r
+  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
+  if (Hob != NULL) {\r
+    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;\r
+  } else {\r
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
+    if (RegEax >= 0x80000008) {\r
+      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
+      PhysicalAddressBits = (UINT8) RegEax;\r
+    } else {\r
+      PhysicalAddressBits = 36;\r
+    }\r
+  }\r
+\r
+  //\r
+  // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.\r
+  //\r
+  ASSERT (PhysicalAddressBits <= 52);\r
+  if (PhysicalAddressBits > 48) {\r
+    PhysicalAddressBits = 48;\r
+  }\r
+  return PhysicalAddressBits;\r
+}\r
+\r
+/**\r
+  Set static page table.\r
+\r
+  @param[in] PageTable     Address of page table.\r
+**/\r
+VOID\r
+SetStaticPageTable (\r
+  IN UINTN               PageTable\r
+  )\r
+{\r
+  UINT64                                        PageAddress;\r
+  UINTN                                         NumberOfPml4EntriesNeeded;\r
+  UINTN                                         NumberOfPdpEntriesNeeded;\r
+  UINTN                                         IndexOfPml4Entries;\r
+  UINTN                                         IndexOfPdpEntries;\r
+  UINTN                                         IndexOfPageDirectoryEntries;\r
+  UINT64                                        *PageMapLevel4Entry;\r
+  UINT64                                        *PageMap;\r
+  UINT64                                        *PageDirectoryPointerEntry;\r
+  UINT64                                        *PageDirectory1GEntry;\r
+  UINT64                                        *PageDirectoryEntry;\r
+\r
+  if (mPhysicalAddressBits <= 39 ) {\r
+    NumberOfPml4EntriesNeeded = 1;\r
+    NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (mPhysicalAddressBits - 30));\r
+  } else {\r
+    NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (mPhysicalAddressBits - 39));\r
+    NumberOfPdpEntriesNeeded = 512;\r
+  }\r
+\r
+  //\r
+  // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.\r
+  //\r
+  PageMap         = (VOID *) PageTable;\r
+\r
+  PageMapLevel4Entry = PageMap;\r
+  PageAddress        = 0;\r
+  for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
+    //\r
+    // Each PML4 entry points to a page of Page Directory Pointer entries.\r
+    //\r
+    PageDirectoryPointerEntry = (UINT64 *) ((*PageMapLevel4Entry) & ~mAddressEncMask & gPhyMask);\r
+    if (PageDirectoryPointerEntry == NULL) {\r
+      PageDirectoryPointerEntry = AllocatePageTableMemory (1);\r
+      ASSERT(PageDirectoryPointerEntry != NULL);\r
+      ZeroMem (PageDirectoryPointerEntry, EFI_PAGES_TO_SIZE(1));\r
+\r
+      *PageMapLevel4Entry = (UINT64)(UINTN)PageDirectoryPointerEntry | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
+    }\r
+\r
+    if (m1GPageTableSupport) {\r
+      PageDirectory1GEntry = PageDirectoryPointerEntry;\r
+      for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {\r
+        if (IndexOfPml4Entries == 0 && IndexOfPageDirectoryEntries < 4) {\r
+          //\r
+          // Skip the < 4G entries\r
+          //\r
+          continue;\r
+        }\r
+        //\r
+        // Fill in the Page Directory entries\r
+        //\r
+        *PageDirectory1GEntry = PageAddress | mAddressEncMask | IA32_PG_PS | PAGE_ATTRIBUTE_BITS;\r
+      }\r
+    } else {\r
+      PageAddress = BASE_4GB;\r
+      for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
+        if (IndexOfPml4Entries == 0 && IndexOfPdpEntries < 4) {\r
+          //\r
+          // Skip the < 4G entries\r
+          //\r
+          continue;\r
+        }\r
+        //\r
+        // Each Directory Pointer entries points to a page of Page Directory entires.\r
+        // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
+        //\r
+        PageDirectoryEntry = (UINT64 *) ((*PageDirectoryPointerEntry) & ~mAddressEncMask & gPhyMask);\r
+        if (PageDirectoryEntry == NULL) {\r
+          PageDirectoryEntry = AllocatePageTableMemory (1);\r
+          ASSERT(PageDirectoryEntry != NULL);\r
+          ZeroMem (PageDirectoryEntry, EFI_PAGES_TO_SIZE(1));\r
+\r
+          //\r
+          // Fill in a Page Directory Pointer Entries\r
+          //\r
+          *PageDirectoryPointerEntry = (UINT64)(UINTN)PageDirectoryEntry | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
+        }\r
+\r
+        for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {\r
+          //\r
+          // Fill in the Page Directory entries\r
+          //\r
+          *PageDirectoryEntry = PageAddress | mAddressEncMask | IA32_PG_PS | PAGE_ATTRIBUTE_BITS;\r
+        }\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Create PageTable for SMM use.\r
 \r
@@ -103,17 +246,24 @@ SmmInitPageTable (
   UINTN                             Index;\r
   UINTN                             PageFaultHandlerHookAddress;\r
   IA32_IDT_GATE_DESCRIPTOR          *IdtEntry;\r
+  EFI_STATUS                        Status;\r
 \r
   //\r
   // Initialize spin lock\r
   //\r
-  InitializeSpinLock (&mPFLock);\r
+  InitializeSpinLock (mPFLock);\r
 \r
+  mCpuSmmStaticPageTable = PcdGetBool (PcdCpuSmmStaticPageTable);\r
   m1GPageTableSupport = Is1GPageSupport ();\r
+  DEBUG ((DEBUG_INFO, "1GPageTableSupport - 0x%x\n", m1GPageTableSupport));\r
+  DEBUG ((DEBUG_INFO, "PcdCpuSmmStaticPageTable - 0x%x\n", mCpuSmmStaticPageTable));\r
+\r
+  mPhysicalAddressBits = CalculateMaximumSupportAddress ();\r
+  DEBUG ((DEBUG_INFO, "PhysicalAddressBits - 0x%x\n", mPhysicalAddressBits));\r
   //\r
   // Generate PAE page table for the first 4GB memory space\r
   //\r
-  Pages = Gen4GPageTable (PAGE_TABLE_PAGES + 1);\r
+  Pages = Gen4GPageTable (FALSE);\r
 \r
   //\r
   // Set IA32_PG_PMNT bit to mask this entry\r
@@ -126,21 +276,28 @@ SmmInitPageTable (
   //\r
   // Fill Page-Table-Level4 (PML4) entry\r
   //\r
-  PTEntry = (UINT64*)(UINTN)(Pages - EFI_PAGES_TO_SIZE (PAGE_TABLE_PAGES + 1));\r
-  *PTEntry = Pages + IA32_PG_P;\r
+  PTEntry = (UINT64*)AllocatePageTableMemory (1);\r
+  ASSERT (PTEntry != NULL);\r
+  *PTEntry = Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
   ZeroMem (PTEntry + 1, EFI_PAGE_SIZE - sizeof (*PTEntry));\r
+\r
   //\r
   // Set sub-entries number\r
   //\r
   SetSubEntriesNum (PTEntry, 3);\r
 \r
-  //\r
-  // Add remaining pages to page pool\r
-  //\r
-  FreePage = (LIST_ENTRY*)(PTEntry + EFI_PAGE_SIZE / sizeof (*PTEntry));\r
-  while ((UINTN)FreePage < Pages) {\r
-    InsertTailList (&mPagePool, FreePage);\r
-    FreePage += EFI_PAGE_SIZE / sizeof (*FreePage);\r
+  if (mCpuSmmStaticPageTable) {\r
+    SetStaticPageTable ((UINTN)PTEntry);\r
+  } else {\r
+    //\r
+    // Add pages to page pool\r
+    //\r
+    FreePage = (LIST_ENTRY*)AllocatePageTableMemory (PAGE_TABLE_PAGES);\r
+    ASSERT (FreePage != NULL);\r
+    for (Index = 0; Index < PAGE_TABLE_PAGES; Index++) {\r
+      InsertTailList (&mPagePool, FreePage);\r
+      FreePage += EFI_PAGE_SIZE / sizeof (*FreePage);\r
+    }\r
   }\r
 \r
   if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
@@ -161,7 +318,8 @@ SmmInitPageTable (
     //\r
     // Register Smm Page Fault Handler\r
     //\r
-    SmmRegisterExceptionHandler (&mSmmCpuService, EXCEPT_IA32_PAGE_FAULT, SmiPFHandler);\r
+    Status = SmmRegisterExceptionHandler (&mSmmCpuService, EXCEPT_IA32_PAGE_FAULT, SmiPFHandler);\r
+    ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
   //\r
@@ -301,7 +459,7 @@ ReclaimPages (
       //\r
       continue;\r
     }\r
-    Pdpt = (UINT64*)(UINTN)(Pml4[Pml4Index] & gPhyMask);\r
+    Pdpt = (UINT64*)(UINTN)(Pml4[Pml4Index] & ~mAddressEncMask & gPhyMask);\r
     PML4EIgnore = FALSE;\r
     for (PdptIndex = 0; PdptIndex < EFI_PAGE_SIZE / sizeof (*Pdpt); PdptIndex++) {\r
       if ((Pdpt[PdptIndex] & IA32_PG_P) == 0 || (Pdpt[PdptIndex] & IA32_PG_PMNT) != 0) {\r
@@ -322,7 +480,7 @@ ReclaimPages (
         // we will not check PML4 entry more\r
         //\r
         PML4EIgnore = TRUE;\r
-        Pdt =  (UINT64*)(UINTN)(Pdpt[PdptIndex] & gPhyMask);\r
+        Pdt =  (UINT64*)(UINTN)(Pdpt[PdptIndex] & ~mAddressEncMask & gPhyMask);\r
         PDPTEIgnore = FALSE;\r
         for (PdtIndex = 0; PdtIndex < EFI_PAGE_SIZE / sizeof(*Pdt); PdtIndex++) {\r
           if ((Pdt[PdtIndex] & IA32_PG_P) == 0 || (Pdt[PdtIndex] & IA32_PG_PMNT) != 0) {\r
@@ -404,7 +562,7 @@ ReclaimPages (
   //\r
   // Secondly, insert the page pointed by this entry into page pool and clear this entry\r
   //\r
-  InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(*ReleasePageAddress & gPhyMask));\r
+  InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(*ReleasePageAddress & ~mAddressEncMask & gPhyMask));\r
   *ReleasePageAddress = 0;\r
 \r
   //\r
@@ -416,14 +574,14 @@ ReclaimPages (
       //\r
       // If 4 KByte Page Table is released, check the PDPT entry\r
       //\r
-      Pdpt = (UINT64*)(UINTN)(Pml4[MinPml4] & gPhyMask);\r
+      Pdpt = (UINT64*)(UINTN)(Pml4[MinPml4] & ~mAddressEncMask & gPhyMask);\r
       SubEntriesNum = GetSubEntriesNum(Pdpt + MinPdpt);\r
       if (SubEntriesNum == 0) {\r
         //\r
         // Release the empty Page Directory table if there was no more 4 KByte Page Table entry\r
         // clear the Page directory entry\r
         //\r
-        InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(Pdpt[MinPdpt] & gPhyMask));\r
+        InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(Pdpt[MinPdpt] & ~mAddressEncMask & gPhyMask));\r
         Pdpt[MinPdpt] = 0;\r
         //\r
         // Go on checking the PML4 table\r
@@ -447,7 +605,7 @@ ReclaimPages (
         // Release the empty PML4 table if there was no more 1G KByte Page Table entry\r
         // clear the Page directory entry\r
         //\r
-        InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(Pml4[MinPml4] & gPhyMask));\r
+        InsertTailList (&mPagePool, (LIST_ENTRY*)(UINTN)(Pml4[MinPml4] & ~mAddressEncMask & gPhyMask));\r
         Pml4[MinPml4] = 0;\r
         MinPdpt = (UINTN)-1;\r
         continue;\r
@@ -562,7 +720,7 @@ SmiDefaultPFHandler (
     break;\r
   case SmmPageSize1G:\r
     if (!m1GPageTableSupport) {\r
-      DEBUG ((EFI_D_ERROR, "1-GByte pages is not supported!"));\r
+      DEBUG ((DEBUG_ERROR, "1-GByte pages is not supported!"));\r
       ASSERT (FALSE);\r
     }\r
     //\r
@@ -591,7 +749,7 @@ SmiDefaultPFHandler (
         //\r
         // If the entry is not present, allocate one page from page pool for it\r
         //\r
-        PageTable[PTIndex] = AllocPage () | IA32_PG_RW | IA32_PG_P;\r
+        PageTable[PTIndex] = AllocPage () | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
       } else {\r
         //\r
         // Save the upper entry address\r
@@ -604,7 +762,7 @@ SmiDefaultPFHandler (
       //\r
       PageTable[PTIndex] |= (UINT64)IA32_PG_A;\r
       SetAccNum (PageTable + PTIndex, 7);\r
-      PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask);\r
+      PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & gPhyMask);\r
     }\r
 \r
     PTIndex = BitFieldRead64 (PFAddress, StartBit, StartBit + 8);\r
@@ -613,15 +771,15 @@ SmiDefaultPFHandler (
       // Check if the entry has already existed, this issue may occur when the different\r
       // size page entries created under the same entry\r
       //\r
-      DEBUG ((EFI_D_ERROR, "PageTable = %lx, PTIndex = %x, PageTable[PTIndex] = %lx\n", PageTable, PTIndex, PageTable[PTIndex]));\r
-      DEBUG ((EFI_D_ERROR, "New page table overlapped with old page table!\n"));\r
+      DEBUG ((DEBUG_ERROR, "PageTable = %lx, PTIndex = %x, PageTable[PTIndex] = %lx\n", PageTable, PTIndex, PageTable[PTIndex]));\r
+      DEBUG ((DEBUG_ERROR, "New page table overlapped with old page table!\n"));\r
       ASSERT (FALSE);\r
     }\r
     //\r
     // Fill the new entry\r
     //\r
-    PageTable[PTIndex] = (PFAddress & gPhyMask & ~((1ull << EndBit) - 1)) |\r
-                         PageAttribute | IA32_PG_A | IA32_PG_RW | IA32_PG_P;\r
+    PageTable[PTIndex] = ((PFAddress | mAddressEncMask) & gPhyMask & ~((1ull << EndBit) - 1)) |\r
+                         PageAttribute | IA32_PG_A | PAGE_ATTRIBUTE_BITS;\r
     if (UpperEntry != NULL) {\r
       SetSubEntriesNum (UpperEntry, GetSubEntriesNum (UpperEntry) + 1);\r
     }\r
@@ -643,25 +801,52 @@ SmiDefaultPFHandler (
 VOID\r
 EFIAPI\r
 SmiPFHandler (\r
-    IN EFI_EXCEPTION_TYPE   InterruptType,\r
-    IN EFI_SYSTEM_CONTEXT   SystemContext\r
+  IN EFI_EXCEPTION_TYPE   InterruptType,\r
+  IN EFI_SYSTEM_CONTEXT   SystemContext\r
   )\r
 {\r
   UINTN             PFAddress;\r
+  UINTN             GuardPageAddress;\r
+  UINTN             CpuIndex;\r
 \r
   ASSERT (InterruptType == EXCEPT_IA32_PAGE_FAULT);\r
 \r
-  AcquireSpinLock (&mPFLock);\r
+  AcquireSpinLock (mPFLock);\r
 \r
   PFAddress = AsmReadCr2 ();\r
 \r
+  if (mCpuSmmStaticPageTable && (PFAddress >= LShiftU64 (1, (mPhysicalAddressBits - 1)))) {\r
+    DumpCpuContext (InterruptType, SystemContext);\r
+    DEBUG ((DEBUG_ERROR, "Do not support address 0x%lx by processor!\n", PFAddress));\r
+    CpuDeadLoop ();\r
+  }\r
+\r
   //\r
-  // If a page fault occurs in SMRAM range, it should be in a SMM stack guard page.\r
+  // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,\r
+  // or SMM page protection violation.\r
   //\r
-  if ((FeaturePcdGet (PcdCpuSmmStackGuard)) &&\r
-      (PFAddress >= mCpuHotPlugData.SmrrBase) &&\r
+  if ((PFAddress >= mCpuHotPlugData.SmrrBase) &&\r
       (PFAddress < (mCpuHotPlugData.SmrrBase + mCpuHotPlugData.SmrrSize))) {\r
-    DEBUG ((EFI_D_ERROR, "SMM stack overflow!\n"));\r
+    DumpCpuContext (InterruptType, SystemContext);\r
+    CpuIndex = GetCpuIndex ();\r
+    GuardPageAddress = (mSmmStackArrayBase + EFI_PAGE_SIZE + CpuIndex * mSmmStackSize);\r
+    if ((FeaturePcdGet (PcdCpuSmmStackGuard)) &&\r
+        (PFAddress >= GuardPageAddress) &&\r
+        (PFAddress < (GuardPageAddress + EFI_PAGE_SIZE))) {\r
+      DEBUG ((DEBUG_ERROR, "SMM stack overflow!\n"));\r
+    } else {\r
+      if ((SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != 0) {\r
+        DEBUG ((DEBUG_ERROR, "SMM exception at execution (0x%lx)\n", PFAddress));\r
+        DEBUG_CODE (\r
+          DumpModuleInfoByIp (*(UINTN *)(UINTN)SystemContext.SystemContextX64->Rsp);\r
+        );\r
+      } else {\r
+        DEBUG ((DEBUG_ERROR, "SMM exception at access (0x%lx)\n", PFAddress));\r
+        DEBUG_CODE (\r
+          DumpModuleInfoByIp ((UINTN)SystemContext.SystemContextX64->Rip);\r
+        );\r
+      }\r
+    }\r
     CpuDeadLoop ();\r
   }\r
 \r
@@ -670,13 +855,33 @@ SmiPFHandler (
   //\r
   if ((PFAddress < mCpuHotPlugData.SmrrBase) ||\r
       (PFAddress >= mCpuHotPlugData.SmrrBase + mCpuHotPlugData.SmrrSize)) {\r
+    DumpCpuContext (InterruptType, SystemContext);\r
     if ((SystemContext.SystemContextX64->ExceptionData & IA32_PF_EC_ID) != 0) {\r
-      DEBUG ((EFI_D_ERROR, "Code executed on IP(0x%lx) out of SMM range after SMM is locked!\n", PFAddress));\r
+      DEBUG ((DEBUG_ERROR, "Code executed on IP(0x%lx) out of SMM range after SMM is locked!\n", PFAddress));\r
       DEBUG_CODE (\r
         DumpModuleInfoByIp (*(UINTN *)(UINTN)SystemContext.SystemContextX64->Rsp);\r
       );\r
       CpuDeadLoop ();\r
     }\r
+    if (IsSmmCommBufferForbiddenAddress (PFAddress)) {\r
+      DEBUG ((DEBUG_ERROR, "Access SMM communication forbidden address (0x%lx)!\n", PFAddress));\r
+      DEBUG_CODE (\r
+        DumpModuleInfoByIp ((UINTN)SystemContext.SystemContextX64->Rip);\r
+      );\r
+      CpuDeadLoop ();\r
+    }\r
+  }\r
+\r
+  //\r
+  // If NULL pointer was just accessed\r
+  //\r
+  if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT1) != 0 &&\r
+      (PFAddress < EFI_PAGE_SIZE)) {\r
+    DEBUG ((DEBUG_ERROR, "!!! NULL pointer access !!!\n"));\r
+    DEBUG_CODE (\r
+      DumpModuleInfoByIp ((UINTN)SystemContext.SystemContextX64->Rip);\r
+    );\r
+    CpuDeadLoop ();\r
   }\r
 \r
   if (FeaturePcdGet (PcdCpuSmmProfileEnable)) {\r
@@ -688,5 +893,102 @@ SmiPFHandler (
     SmiDefaultPFHandler ();\r
   }\r
 \r
-  ReleaseSpinLock (&mPFLock);\r
+  ReleaseSpinLock (mPFLock);\r
+}\r
+\r
+/**\r
+  This function sets memory attribute for page table.\r
+**/\r
+VOID\r
+SetPageTableAttributes (\r
+  VOID\r
+  )\r
+{\r
+  UINTN                 Index2;\r
+  UINTN                 Index3;\r
+  UINTN                 Index4;\r
+  UINT64                *L1PageTable;\r
+  UINT64                *L2PageTable;\r
+  UINT64                *L3PageTable;\r
+  UINT64                *L4PageTable;\r
+  BOOLEAN               IsSplitted;\r
+  BOOLEAN               PageTableSplitted;\r
+\r
+  //
+  // Don't do this if
+  //  - no static page table; or
+  //  - SMM heap guard feature enabled
+  //      BIT2: SMM page guard enabled
+  //      BIT3: SMM pool guard enabled
+  //
+  if (!mCpuSmmStaticPageTable ||
+      (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT3 | BIT2)) != 0) {
+    //
+    // Static paging and heap guard should not be enabled at the same time.
+    //
+    ASSERT (!(mCpuSmmStaticPageTable &&
+              (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT3 | BIT2)) != 0));
+    return ;\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "SetPageTableAttributes\n"));\r
+\r
+  //\r
+  // Disable write protection, because we need mark page table to be write protected.\r
+  // We need *write* page table memory, to mark itself to be *read only*.\r
+  //\r
+  AsmWriteCr0 (AsmReadCr0() & ~CR0_WP);\r
+\r
+  do {\r
+    DEBUG ((DEBUG_INFO, "Start...\n"));\r
+    PageTableSplitted = FALSE;\r
+\r
+    L4PageTable = (UINT64 *)GetPageTableBase ();\r
+    SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L4PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
+    PageTableSplitted = (PageTableSplitted || IsSplitted);\r
+\r
+    for (Index4 = 0; Index4 < SIZE_4KB/sizeof(UINT64); Index4++) {\r
+      L3PageTable = (UINT64 *)(UINTN)(L4PageTable[Index4] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
+      if (L3PageTable == NULL) {\r
+        continue;\r
+      }\r
+\r
+      SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L3PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
+      PageTableSplitted = (PageTableSplitted || IsSplitted);\r
+\r
+      for (Index3 = 0; Index3 < SIZE_4KB/sizeof(UINT64); Index3++) {\r
+        if ((L3PageTable[Index3] & IA32_PG_PS) != 0) {\r
+          // 1G\r
+          continue;\r
+        }\r
+        L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
+        if (L2PageTable == NULL) {\r
+          continue;\r
+        }\r
+\r
+        SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L2PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
+        PageTableSplitted = (PageTableSplitted || IsSplitted);\r
+\r
+        for (Index2 = 0; Index2 < SIZE_4KB/sizeof(UINT64); Index2++) {\r
+          if ((L2PageTable[Index2] & IA32_PG_PS) != 0) {\r
+            // 2M\r
+            continue;\r
+          }\r
+          L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r
+          if (L1PageTable == NULL) {\r
+            continue;\r
+          }\r
+          SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS)(UINTN)L1PageTable, SIZE_4KB, EFI_MEMORY_RO, &IsSplitted);\r
+          PageTableSplitted = (PageTableSplitted || IsSplitted);\r
+        }\r
+      }\r
+    }\r
+  } while (PageTableSplitted);\r
+\r
+  //\r
+  // Enable write protection, after page table updated.\r
+  //\r
+  AsmWriteCr0 (AsmReadCr0() | CR0_WP);\r
+\r
+  return ;\r
 }\r