]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/UefiDebugLibStdErr: Make it runtime safe
authorAaron Antone <aanton@microsoft.com>
Mon, 8 Apr 2019 03:03:26 +0000 (11:03 +0800)
committerLiming Gao <liming.gao@intel.com>
Mon, 22 Apr 2019 01:52:54 +0000 (09:52 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1416

After ExitBootServices, some pointer would be invalid such as
the Protocol pointer and gST. The function depend on those should
be prevent. So disable the related function while after
ExitBootServices.
Change the gST to a internal one, because there will be a cycle
consume between UefiBootServicesTableLib and DebugLib due to the
library constructors.
Also remove the SMM support for this instance.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Michael Turner <Michael.Turner@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Signed-off-by: Zhichao Gao <zhichao.gao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/UefiDebugLibStdErr/DebugLib.c
MdePkg/Library/UefiDebugLibStdErr/DebugLibConstructor.c [new file with mode: 0644]
MdePkg/Library/UefiDebugLibStdErr/UefiDebugLibStdErr.inf

index 29f93cf3e3fba3dba635f5ef1af699b2bfd57a55..40eb697e7e2cab2e1762d1e7904b0a7c782e14d0 100644 (file)
@@ -10,7 +10,6 @@
 #include <Uefi.h>\r
 \r
 #include <Library/DebugLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/PrintLib.h>\r
 #include <Library/PcdLib.h>\r
 #include <Library/BaseLib.h>\r
@@ -29,6 +28,8 @@
 //\r
 VA_LIST     mVaListNull;\r
 \r
+extern BOOLEAN                mPostEBS;\r
+extern EFI_SYSTEM_TABLE       *mDebugST;\r
 \r
 /**\r
   Prints a debug message to the debug output device if the specified error level is enabled.\r
@@ -88,32 +89,34 @@ DebugPrintMarker (
 {\r
   CHAR16   Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
 \r
-  //\r
-  // If Format is NULL, then ASSERT().\r
-  //\r
-  ASSERT (Format != NULL);\r
-\r
-  //\r
-  // Check driver debug mask value and global mask\r
-  //\r
-  if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
-    return;\r
-  }\r
-\r
-  //\r
-  // Convert the DEBUG() message to a Unicode String\r
-  //\r
-  if (BaseListMarker == NULL) {\r
-    UnicodeVSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, VaListMarker);\r
-  } else {\r
-    UnicodeBSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, BaseListMarker);\r
-  }\r
-\r
-  //\r
-  // Send the print string to the Standard Error device\r
-  //\r
-  if ((gST != NULL) && (gST->StdErr != NULL)) {\r
-    gST->StdErr->OutputString (gST->StdErr, Buffer);\r
+  if (!mPostEBS) {\r
+    //\r
+    // If Format is NULL, then ASSERT().\r
+    //\r
+    ASSERT (Format != NULL);\r
+\r
+    //\r
+    // Check driver debug mask value and global mask\r
+    //\r
+    if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
+      return;\r
+    }\r
+\r
+    //\r
+    // Convert the DEBUG() message to a Unicode String\r
+    //\r
+    if (BaseListMarker == NULL) {\r
+      UnicodeVSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, VaListMarker);\r
+    } else {\r
+      UnicodeBSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, BaseListMarker);\r
+    }\r
+\r
+    //\r
+    // Send the print string to the Standard Error device\r
+    //\r
+    if ((mDebugST != NULL) && (mDebugST->StdErr != NULL)) {\r
+      mDebugST->StdErr->OutputString (mDebugST->StdErr, Buffer);\r
+    }\r
   }\r
 }\r
 \r
@@ -207,33 +210,35 @@ DebugAssert (
 {\r
   CHAR16  Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
 \r
-  //\r
-  // Generate the ASSERT() message in Unicode format\r
-  //\r
-  UnicodeSPrintAsciiFormat (\r
-    Buffer,\r
-    sizeof (Buffer),\r
-    "ASSERT [%a] %a(%d): %a\n",\r
-    gEfiCallerBaseName,\r
-    FileName,\r
-    LineNumber,\r
-    Description\r
-    );\r
-\r
-  //\r
-  // Send the print string to the Standard Error device\r
-  //\r
-  if ((gST != NULL) && (gST->StdErr != NULL)) {\r
-    gST->StdErr->OutputString (gST->StdErr, Buffer);\r
-  }\r
-\r
-  //\r
-  // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
-  //\r
-  if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
-    CpuBreakpoint ();\r
-  } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
-    CpuDeadLoop ();\r
+  if (!mPostEBS) {\r
+    //\r
+    // Generate the ASSERT() message in Unicode format\r
+    //\r
+    UnicodeSPrintAsciiFormat (\r
+      Buffer,\r
+      sizeof (Buffer),\r
+      "ASSERT [%a] %a(%d): %a\n",\r
+      gEfiCallerBaseName,\r
+      FileName,\r
+      LineNumber,\r
+      Description\r
+      );\r
+\r
+    //\r
+    // Send the print string to the Standard Error device\r
+    //\r
+    if ((mDebugST != NULL) && (mDebugST->StdErr != NULL)) {\r
+      mDebugST->StdErr->OutputString (mDebugST->StdErr, Buffer);\r
+    }\r
+\r
+    //\r
+    // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
+    //\r
+    if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
+      CpuBreakpoint ();\r
+    } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
+      CpuDeadLoop ();\r
+    }\r
   }\r
 }\r
 \r
diff --git a/MdePkg/Library/UefiDebugLibStdErr/DebugLibConstructor.c b/MdePkg/Library/UefiDebugLibStdErr/DebugLibConstructor.c
new file mode 100644 (file)
index 0000000..d4fdfba
--- /dev/null
@@ -0,0 +1,77 @@
+/** @file\r
+  UEFI Dxe DebugLib constructor that prevent some debug service after ExitBootServices event,\r
+  because some pointer is nulled at that phase.\r
+\r
+  Copyright (c) 2018, Microsoft Corporation\r
+  Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+\r
+//\r
+// BOOLEAN value to indicate if it is at the post ExitBootServices pahse\r
+//\r
+BOOLEAN     mPostEBS = FALSE;\r
+\r
+EFI_EVENT   mExitBootServicesEvent;\r
+\r
+//\r
+// Pointer to SystemTable\r
+// This library instance may have a cycle consume with UefiBootServicesTableLib\r
+// because of the constructors.\r
+//\r
+EFI_SYSTEM_TABLE      *mDebugST;\r
+\r
+/**\r
+  This routine sets the mPostEBS for exit boot servies true\r
+  to prevent DebugPort protocol dereferences when the pointer is nulled.\r
+\r
+  @param  Event        Event whose notification function is being invoked.\r
+  @param  Context      Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ExitBootServicesCallback (\r
+  EFI_EVENT   Event,\r
+  VOID*       Context\r
+  )\r
+{\r
+  mPostEBS = TRUE;\r
+  return;\r
+}\r
+\r
+/**\r
+  The constructor gets the pointers to the system table.\r
+  And create a event to indicate it is after ExitBootServices.\r
+\r
+  @param  ImageHandle     The firmware allocated handle for the EFI image.\r
+  @param  SystemTable     A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS     The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DxeDebugLibConstructor(\r
+  IN EFI_HANDLE                 ImageHandle,\r
+  IN EFI_SYSTEM_TABLE           *SystemTable\r
+  )\r
+{\r
+  mDebugST = SystemTable;\r
+\r
+  SystemTable->BootServices->CreateEventEx (\r
+                                EVT_NOTIFY_SIGNAL,\r
+                                TPL_NOTIFY,\r
+                                ExitBootServicesCallback,\r
+                                NULL,\r
+                                &gEfiEventExitBootServicesGuid,\r
+                                &mExitBootServicesEvent\r
+                                );\r
+\r
+  return EFI_SUCCESS;\r
+}\r
index 865cc98753b9297e9357bb79952d058969c9c5db..deaa3427f62b1596fbf0de55eb826672253b8e09 100644 (file)
@@ -3,7 +3,9 @@
 #\r
 #  Debug Lib that sends messages to the the Standard Error Device in the EFI System Table.\r
 #\r
-#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2018, Microsoft Corporation\r
+#\r
+#  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
@@ -17,8 +19,9 @@
   FILE_GUID                      = b57a1df6-ffdb-4247-a3df-3a562176751a\r
   MODULE_TYPE                    = UEFI_DRIVER\r
   VERSION_STRING                 = 1.0\r
-  LIBRARY_CLASS                  = DebugLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
+  LIBRARY_CLASS                  = DebugLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_APPLICATION UEFI_DRIVER\r
 \r
+  CONSTRUCTOR                    = DxeDebugLibConstructor\r
 \r
 #\r
 #  VALID_ARCHITECTURES           = IA32 X64 EBC\r
@@ -26,6 +29,7 @@
 \r
 [Sources]\r
   DebugLib.c\r
+  DebugLibConstructor.c\r
 \r
 \r
 [Packages]\r
   BaseLib\r
   PcdLib\r
   PrintLib\r
-  UefiBootServicesTableLib\r
   DebugPrintErrorLevelLib\r
 \r
+[Guids]\r
+  gEfiEventExitBootServicesGuid                 ## CONSUMES\r
+\r
 [Pcd]\r
   gEfiMdePkgTokenSpaceGuid.PcdDebugClearMemoryValue   ## SOMETIMES_CONSUMES\r
   gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask       ## CONSUMES\r