]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/UefidebugLibConOut: Make it runtime safe
authorAaron Antone <aanton@microsoft.com>
Mon, 8 Apr 2019 03:00:28 +0000 (11:00 +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/UefiDebugLibConOut/DebugLib.c
MdePkg/Library/UefiDebugLibConOut/DebugLibConstructor.c [new file with mode: 0644]
MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf

index c430419c998ab927db6860a7bf1b506033d1f5fa..cf168d05cf21d18d0a687948a68a3192de105642 100644 (file)
@@ -9,7 +9,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
@@ -27,6 +26,9 @@
 //\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
 \r
@@ -85,33 +87,35 @@ 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
-  //\r
-  // Send the print string to the Console Output device\r
-  //\r
-  if ((gST != NULL) && (gST->ConOut != NULL)) {\r
-    gST->ConOut->OutputString (gST->ConOut, 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
+    //\r
+    // Send the print string to the Console Output device\r
+    //\r
+    if ((mDebugST != NULL) && (mDebugST->ConOut != NULL)) {\r
+      mDebugST->ConOut->OutputString (mDebugST->ConOut, Buffer);\r
+    }\r
   }\r
 }\r
 \r
@@ -205,33 +209,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 Console Output device\r
-  //\r
-  if ((gST != NULL) && (gST->ConOut != NULL)) {\r
-    gST->ConOut->OutputString (gST->ConOut, 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 Console Output device\r
+    //\r
+    if ((mDebugST != NULL) && (mDebugST->ConOut != NULL)) {\r
+      mDebugST->ConOut->OutputString (mDebugST->ConOut, 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/UefiDebugLibConOut/DebugLibConstructor.c b/MdePkg/Library/UefiDebugLibConOut/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 12af4a9e98f0fe55917ed74d3623761c2ebbf7ee..4c279a5bf2f90211da35199c174d76a795cacf51 100644 (file)
@@ -3,7 +3,9 @@
 #\r
 #  Debug Lib that sends messages to the Console Output 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                      = 5cddfaf3-e9a7-4d16-bdce-1e002df475bb\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
@@ -27,6 +30,7 @@
 \r
 [Sources]\r
   DebugLib.c\r
+  DebugLibConstructor.c\r
 \r
 \r
 \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