]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
MdeModulePkg: remove PE/COFF header workaround for ELILO on IPF
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / MemoryProtection.c
index 455ed35f9a36541272ea21effd7d13f23f5caf84..fa8f8fe91ac7a8e9f7f5e9805bf2ac63c77f4d5e 100644 (file)
@@ -19,7 +19,7 @@
 \r
   Once the image is unloaded, the protection is removed automatically.\r
 \r
-Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2017 - 2018, 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
@@ -44,7 +44,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Guid/PropertiesTable.h>\r
 \r
 #include <Protocol/FirmwareVolume2.h>\r
-#include <Protocol/BlockIo.h>\r
 #include <Protocol/SimpleFileSystem.h>\r
 \r
 #include "DxeMain.h"\r
@@ -407,7 +406,6 @@ ProtectUefiImage (
   IMAGE_PROPERTIES_RECORD              *ImageRecord;\r
   CHAR8                                *PdbPointer;\r
   IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection;\r
-  UINT16                               Magic;\r
   BOOLEAN                              IsAligned;\r
   UINT32                               ProtectionPolicy;\r
 \r
@@ -467,21 +465,7 @@ ProtectUefiImage (
   //\r
   // Get SectionAlignment\r
   //\r
-  if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
-    //\r
-    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value\r
-    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the\r
-    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
-    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
-    //\r
-    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
-  } else {\r
-    //\r
-    // Get the magic value from the PE/COFF Optional Header\r
-    //\r
-    Magic = Hdr.Pe32->OptionalHeader.Magic;\r
-  }\r
-  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+  if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     SectionAlignment  = Hdr.Pe32->OptionalHeader.SectionAlignment;\r
   } else {\r
     SectionAlignment  = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;\r
@@ -801,6 +785,9 @@ InitializeDxeNxMemoryProtectionPolicy (
   UINT64                            Attributes;\r
   LIST_ENTRY                        *Link;\r
   EFI_GCD_MAP_ENTRY                 *Entry;\r
+  EFI_PEI_HOB_POINTERS              Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION         *MemoryHob;\r
+  EFI_PHYSICAL_ADDRESS              StackBase;\r
 \r
   //\r
   // Get the EFI memory map.\r
@@ -832,6 +819,40 @@ InitializeDxeNxMemoryProtectionPolicy (
   } while (Status == EFI_BUFFER_TOO_SMALL);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  StackBase = 0;\r
+  if (PcdGetBool (PcdCpuStackGuard)) {\r
+    //\r
+    // Get the base of stack from Hob.\r
+    //\r
+    Hob.Raw = GetHobList ();\r
+    while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {\r
+      MemoryHob = Hob.MemoryAllocation;\r
+      if (CompareGuid(&gEfiHobMemoryAllocStackGuid, &MemoryHob->AllocDescriptor.Name)) {\r
+        DEBUG ((\r
+          DEBUG_INFO,\r
+          "%a: StackBase = 0x%016lx  StackSize = 0x%016lx\n",\r
+          __FUNCTION__,\r
+          MemoryHob->AllocDescriptor.MemoryBaseAddress,\r
+          MemoryHob->AllocDescriptor.MemoryLength\r
+          ));\r
+\r
+        StackBase = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+        //\r
+        // Ensure the base of the stack is page-size aligned.\r
+        //\r
+        ASSERT ((StackBase & EFI_PAGE_MASK) == 0);\r
+        break;\r
+      }\r
+      Hob.Raw = GET_NEXT_HOB (Hob);\r
+    }\r
+\r
+    //\r
+    // Ensure the base of stack can be found from Hob when stack guard is\r
+    // enabled.\r
+    //\r
+    ASSERT (StackBase != 0);\r
+  }\r
+\r
   DEBUG ((\r
     DEBUG_INFO,\r
     "%a: applying strict permissions to active memory regions\n",\r
@@ -846,24 +867,41 @@ InitializeDxeNxMemoryProtectionPolicy (
 \r
     Attributes = GetPermissionAttributeForMemoryType (MemoryMapEntry->Type);\r
     if (Attributes != 0) {\r
+      SetUefiImageMemoryAttributes (\r
+        MemoryMapEntry->PhysicalStart,\r
+        LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT),\r
+        Attributes);\r
+\r
+      //\r
+      // Add EFI_MEMORY_RP attribute for page 0 if NULL pointer detection is\r
+      // enabled.\r
+      //\r
       if (MemoryMapEntry->PhysicalStart == 0 &&\r
           PcdGet8 (PcdNullPointerDetectionPropertyMask) != 0) {\r
 \r
         ASSERT (MemoryMapEntry->NumberOfPages > 0);\r
-        //\r
-        // Skip page 0 if NULL pointer detection is enabled to avoid attributes\r
-        // overwritten.\r
-        //\r
         SetUefiImageMemoryAttributes (\r
-          MemoryMapEntry->PhysicalStart + EFI_PAGE_SIZE,\r
-          LShiftU64 (MemoryMapEntry->NumberOfPages - 1, EFI_PAGE_SHIFT),\r
-          Attributes);\r
-      } else {\r
+          0,\r
+          EFI_PAGES_TO_SIZE (1),\r
+          EFI_MEMORY_RP | Attributes);\r
+      }\r
+\r
+      //\r
+      // Add EFI_MEMORY_RP attribute for the first page of the stack if stack\r
+      // guard is enabled.\r
+      //\r
+      if (StackBase != 0 &&\r
+          (StackBase >= MemoryMapEntry->PhysicalStart &&\r
+           StackBase <  MemoryMapEntry->PhysicalStart +\r
+                        LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT)) &&\r
+          PcdGetBool (PcdCpuStackGuard)) {\r
+\r
         SetUefiImageMemoryAttributes (\r
-          MemoryMapEntry->PhysicalStart,\r
-          LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT),\r
-          Attributes);\r
+          StackBase,\r
+          EFI_PAGES_TO_SIZE (1),\r
+          EFI_MEMORY_RP | Attributes);\r
       }\r
+\r
     }\r
     MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
   }\r
@@ -947,6 +985,11 @@ MemoryProtectionCpuArchProtocolNotify (
     InitializeDxeNxMemoryProtectionPolicy ();\r
   }\r
 \r
+  //\r
+  // Call notify function meant for Heap Guard.\r
+  //\r
+  HeapGuardCpuArchProtocolNotify ();\r
+\r
   if (mImageProtectionPolicy == 0) {\r
     return;\r
   }\r