]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
MdeModulePkg/Core/Dxe: Add EndOfDxe workaround for NULL pointer detection
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / MemoryProtection.c
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