]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
MdeModulePkg DxeIpl: Add stack NX support
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / X64 / VirtualMemory.c
index 795ae7034c10fdcb18fe2fb859ee07ef28a9b45c..ab1e91462ea7a563fdf15bdb3bdc586421369b0f 100644 (file)
@@ -15,7 +15,7 @@
     2) IA-32 Intel(R) Architecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel\r
     3) IA-32 Intel(R) Architecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -29,21 +29,126 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "DxeIpl.h"\r
 #include "VirtualMemory.h"\r
 \r
+/**\r
+  Enable Execute Disable Bit.\r
+\r
+**/\r
+VOID\r
+EnableExecuteDisableBit (\r
+  VOID\r
+  )\r
+{\r
+  UINT64           MsrRegisters;\r
+\r
+  MsrRegisters = AsmReadMsr64 (0xC0000080);\r
+  MsrRegisters |= BIT11;\r
+  AsmWriteMsr64 (0xC0000080, MsrRegisters);\r
+}\r
+\r
+/**\r
+  Split 2M page to 4K.\r
+\r
+  @param[in]      PhysicalAddress       Start physical address the 2M page covered.\r
+  @param[in, out] PageEntry2M           Pointer to 2M page entry.\r
+  @param[in]      StackBase             Stack base address.\r
+  @param[in]      StackSize             Stack size.\r
+\r
+**/\r
+VOID\r
+Split2MPageTo4K (\r
+  IN EFI_PHYSICAL_ADDRESS               PhysicalAddress,\r
+  IN OUT UINT64                         *PageEntry2M,\r
+  IN EFI_PHYSICAL_ADDRESS               StackBase,\r
+  IN UINTN                              StackSize\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS                  PhysicalAddress4K;\r
+  UINTN                                 IndexOfPageTableEntries;\r
+  PAGE_TABLE_4K_ENTRY                   *PageTableEntry;\r
+\r
+  PageTableEntry = AllocatePages (1);\r
+  //\r
+  // Fill in 2M page entry.\r
+  //\r
+  *PageEntry2M = (UINT64) (UINTN) PageTableEntry | IA32_PG_P | IA32_PG_RW;\r
+\r
+  PhysicalAddress4K = PhysicalAddress;\r
+  for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512; IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K += SIZE_4KB) {\r
+    //\r
+    // Fill in the Page Table entries\r
+    //\r
+    PageTableEntry->Uint64 = (UINT64) PhysicalAddress4K;\r
+    PageTableEntry->Bits.ReadWrite = 1;\r
+    PageTableEntry->Bits.Present = 1;\r
+    if ((PhysicalAddress4K >= StackBase) && (PhysicalAddress4K < StackBase + StackSize)) {\r
+      //\r
+      // Set Nx bit for stack.\r
+      //\r
+      PageTableEntry->Bits.Nx = 1;\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Split 1G page to 2M.\r
+\r
+  @param[in]      PhysicalAddress       Start physical address the 1G page covered.\r
+  @param[in, out] PageEntry1G           Pointer to 1G page entry.\r
+  @param[in]      StackBase             Stack base address.\r
+  @param[in]      StackSize             Stack size.\r
+\r
+**/\r
+VOID\r
+Split1GPageTo2M (\r
+  IN EFI_PHYSICAL_ADDRESS               PhysicalAddress,\r
+  IN OUT UINT64                         *PageEntry1G,\r
+  IN EFI_PHYSICAL_ADDRESS               StackBase,\r
+  IN UINTN                              StackSize\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS                  PhysicalAddress2M;\r
+  UINTN                                 IndexOfPageDirectoryEntries;\r
+  PAGE_TABLE_ENTRY                      *PageDirectoryEntry;\r
+\r
+  PageDirectoryEntry = AllocatePages (1);\r
+  //\r
+  // Fill in 1G page entry.\r
+  //\r
+  *PageEntry1G = (UINT64) (UINTN) PageDirectoryEntry | IA32_PG_P | IA32_PG_RW;\r
+\r
+  PhysicalAddress2M = PhysicalAddress;\r
+  for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += SIZE_2MB) {\r
+    if ((PhysicalAddress2M < StackBase + StackSize) && ((PhysicalAddress2M + SIZE_2MB) > StackBase)) {\r
+      //\r
+      // Need to split this 2M page that covers stack range.\r
+      //\r
+      Split2MPageTo4K (PhysicalAddress2M, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);\r
+    } else {\r
+      //\r
+      // Fill in the Page Directory entries\r
+      //\r
+      PageDirectoryEntry->Uint64 = (UINT64) PhysicalAddress2M;\r
+      PageDirectoryEntry->Bits.ReadWrite = 1;\r
+      PageDirectoryEntry->Bits.Present = 1;\r
+      PageDirectoryEntry->Bits.MustBe1 = 1;\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Allocates and fills in the Page Directory and Page Table Entries to\r
   establish a 1:1 Virtual to Physical mapping.\r
 \r
-  @param  NumberOfProcessorPhysicalAddressBits  Number of processor address bits \r
-                                                to use. Limits the number of page \r
-                                                table entries  to the physical \r
-                                                address space. \r
+  @param[in] StackBase  Stack base address.\r
+  @param[in] StackSize  Stack size.\r
 \r
   @return The address of 4 level page map.\r
 \r
 **/\r
 UINTN\r
 CreateIdentityMappingPageTables (\r
-  VOID\r
+  IN EFI_PHYSICAL_ADDRESS   StackBase,\r
+  IN UINTN                  StackSize\r
   )\r
 {  \r
   UINT32                                        RegEax;\r
@@ -149,13 +254,17 @@ CreateIdentityMappingPageTables (
       PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry;\r
     \r
       for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {\r
-        //\r
-        // Fill in the Page Directory entries\r
-        //\r
-        PageDirectory1GEntry->Uint64 = (UINT64)PageAddress;\r
-        PageDirectory1GEntry->Bits.ReadWrite = 1;\r
-        PageDirectory1GEntry->Bits.Present = 1;\r
-        PageDirectory1GEntry->Bits.MustBe1 = 1;\r
+        if (PcdGetBool (PcdSetNxForStack) && (PageAddress < StackBase + StackSize) && ((PageAddress + SIZE_1GB) > StackBase)) {\r
+          Split1GPageTo2M (PageAddress, (UINT64 *) PageDirectory1GEntry, StackBase, StackSize);\r
+        } else {\r
+          //\r
+          // Fill in the Page Directory entries\r
+          //\r
+          PageDirectory1GEntry->Uint64 = (UINT64)PageAddress;\r
+          PageDirectory1GEntry->Bits.ReadWrite = 1;\r
+          PageDirectory1GEntry->Bits.Present = 1;\r
+          PageDirectory1GEntry->Bits.MustBe1 = 1;\r
+        }\r
       }\r
     } else {\r
       for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
@@ -174,13 +283,20 @@ CreateIdentityMappingPageTables (
         PageDirectoryPointerEntry->Bits.Present = 1;\r
 \r
         for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {\r
-          //\r
-          // Fill in the Page Directory entries\r
-          //\r
-          PageDirectoryEntry->Uint64 = (UINT64)PageAddress;\r
-          PageDirectoryEntry->Bits.ReadWrite = 1;\r
-          PageDirectoryEntry->Bits.Present = 1;\r
-          PageDirectoryEntry->Bits.MustBe1 = 1;\r
+          if (PcdGetBool (PcdSetNxForStack) && (PageAddress < StackBase + StackSize) && ((PageAddress + SIZE_2MB) > StackBase)) {\r
+            //\r
+            // Need to split this 2M page that covers stack range.\r
+            //\r
+            Split2MPageTo4K (PageAddress, (UINT64 *) PageDirectoryEntry, StackBase, StackSize);\r
+          } else {\r
+            //\r
+            // Fill in the Page Directory entries\r
+            //\r
+            PageDirectoryEntry->Uint64 = (UINT64)PageAddress;\r
+            PageDirectoryEntry->Bits.ReadWrite = 1;\r
+            PageDirectoryEntry->Bits.Present = 1;\r
+            PageDirectoryEntry->Bits.MustBe1 = 1;\r
+          }\r
         }\r
       }\r
 \r
@@ -203,6 +319,10 @@ CreateIdentityMappingPageTables (
       );\r
   }\r
 \r
+  if (PcdGetBool (PcdSetNxForStack)) {\r
+    EnableExecuteDisableBit ();\r
+  }\r
+\r
   return (UINTN)PageMap;\r
 }\r
 \r