]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Core/Dxe: Add EndOfDxe workaround for NULL pointer detection
authorJian J Wang <jian.j.wang@intel.com>
Mon, 9 Oct 2017 13:58:55 +0000 (21:58 +0800)
committerEric Dong <eric.dong@intel.com>
Wed, 11 Oct 2017 08:39:01 +0000 (16:39 +0800)
One of issue caused by enabling NULL pointer detection is that some PCI
device OptionROM, binary drivers and binary OS boot loaders may have NULL
pointer access bugs, which will prevent BIOS from booting and is almost
impossible to fix. BIT7 of PCD PcdNullPointerDetectionPropertyMask is used
as a workaround to indicate BIOS to disable NULL pointer detection right
after event gEfiEndOfDxeEventGroupGuid, and then let boot continue.

Cc: Star Zeng <star.zeng@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Ayellet Wolman <ayellet.wolman@intel.com>
Suggested-by: Ayellet Wolman <ayellet.wolman@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
MdeModulePkg/Core/Dxe/DxeMain.inf
MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c

index e29d6c83ae583b3939e376c912fe843e74ab9b26..15f4b03d3ceea2c6b26576d9b9c0e5e9893984e8 100644 (file)
   gEfiMdeModulePkgTokenSpaceGuid.PcdPropertiesTableEnable                   ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdImageProtectionPolicy                   ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdDxeNxMemoryProtectionPolicy             ## CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask        ## CONSUMES\r
 \r
 # [Hob]\r
 # RESOURCE_DESCRIPTOR   ## CONSUMES\r
index 3dd6d1b4a0da4511bfdb38dfcaeaac5b9a303872..c9219cc068b392c14052d77f0548715a86399146 100644 (file)
@@ -188,7 +188,9 @@ CoreAddRange (
   // used for other purposes.\r
   //  \r
   if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
-    SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
+    if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) == 0) {\r
+      SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
+    }\r
   }\r
   \r
   //\r
index a73c4ccd64d8718b2c762f1f73d87a421538ddbd..0fa89e4437f09c54b17db3b4e5c0221dbe0b2f69 100644 (file)
@@ -995,6 +995,53 @@ MemoryProtectionExitBootServicesCallback (
   }\r
 }\r
 \r
+/**\r
+  Disable NULL pointer detection after EndOfDxe. This is a workaround resort in\r
+  order to skip unfixable NULL pointer access issues detected in OptionROM or\r
+  boot loaders.\r
+\r
+  @param[in]  Event     The Event this notify function registered to.\r
+  @param[in]  Context   Pointer to the context data registered to the Event.\r
+**/\r
+VOID\r
+EFIAPI\r
+DisableNullDetectionAtTheEndOfDxe (\r
+  EFI_EVENT                               Event,\r
+  VOID                                    *Context\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR   Desc;\r
+\r
+  DEBUG ((DEBUG_INFO, "DisableNullDetectionAtTheEndOfDxe(): start\r\n"));\r
+  //\r
+  // Disable NULL pointer detection by enabling first 4K page\r
+  //\r
+  Status = CoreGetMemorySpaceDescriptor (0, &Desc);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if ((Desc.Capabilities & EFI_MEMORY_RP) == 0) {\r
+    Status = CoreSetMemorySpaceCapabilities (\r
+              0,\r
+              EFI_PAGE_SIZE,\r
+              Desc.Capabilities | EFI_MEMORY_RP\r
+              );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  Status = CoreSetMemorySpaceAttributes (\r
+            0,\r
+            EFI_PAGE_SIZE,\r
+            Desc.Attributes & ~EFI_MEMORY_RP\r
+            );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  CoreCloseEvent (Event);\r
+  DEBUG ((DEBUG_INFO, "DisableNullDetectionAtTheEndOfDxe(): end\r\n"));\r
+\r
+  return;\r
+}\r
+\r
 /**\r
   Initialize Memory Protection support.\r
 **/\r
@@ -1006,6 +1053,7 @@ CoreInitializeMemoryProtection (
 {\r
   EFI_STATUS  Status;\r
   EFI_EVENT   Event;\r
+  EFI_EVENT   EndOfDxeEvent;\r
   VOID        *Registration;\r
 \r
   mImageProtectionPolicy = PcdGet32(PcdImageProtectionPolicy);\r
@@ -1044,6 +1092,23 @@ CoreInitializeMemoryProtection (
                );\r
     ASSERT_EFI_ERROR(Status);\r
   }\r
+\r
+  //\r
+  // Register a callback to disable NULL pointer detection at EndOfDxe\r
+  //\r
+  if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & (BIT0|BIT7))\r
+       == (BIT0|BIT7)) {\r
+    Status = CoreCreateEventEx (\r
+                    EVT_NOTIFY_SIGNAL,\r
+                    TPL_NOTIFY,\r
+                    DisableNullDetectionAtTheEndOfDxe,\r
+                    NULL,\r
+                    &gEfiEndOfDxeEventGroupGuid,\r
+                    &EndOfDxeEvent\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
   return ;\r
 }\r
 \r