]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: save on I/O port accesses when the debug port is not in use
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 16 Nov 2017 20:31:00 +0000 (21:31 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 17 Nov 2017 17:35:12 +0000 (18:35 +0100)
When SEV is enabled, every debug message printed by OVMF to the
QEMU debug port traps from the guest to QEMU character by character
because "REP OUTSB" cannot be used by IoWriteFifo8.  Furthermore,
when OVMF is built with the DEBUG_VERBOSE bit (value 0x00400000)
enabled in "gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel", then the
OvmfPkg/IoMmuDxe driver, and the OvmfPkg/Library/BaseMemEncryptSevLib
library instance that is built into it, produce a huge amount of
log messages.  Therefore, in SEV guests, the boot time impact is huge
(about 45 seconds _additional_ time spent writing to the debug port).

While these messages are very useful for analyzing guest behavior,
most of the time the user won't be capturing the OVMF debug log.
In fact libvirt does not provide a method for configuring log capture;
users that wish to do this (or are instructed to do this) have to resort
to <qemu:arg>.

The debug console device provides a handy detection mechanism; when read,
it returns 0xE9 (which is very much unlike the 0xFF that is returned by
an unused port).  Use it to skip the possibly expensive OUT instructions
when the debug I/O port isn't plugged anywhere.

For SEC, the debug port has to be read before each full message.
However:

- if the debug port is available, then reading one byte before writing
a full message isn't tragic, especially because SEC doesn't print many
messages

- if the debug port is not available, then reading one byte instead of
writing a full message is still a win.

Contributed-under: TianoCore Contribution Agreement 1.0
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen (Intel address) <jordan.l.justen@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Tested-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c
OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c
OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h [new file with mode: 0644]
OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetectRom.c

index 5a1c86f2c3fda554ff66ba5f3ad847787d511745..36cde54976d8a78816044cc32c9c732ef180bc18 100644 (file)
@@ -22,6 +22,7 @@
 #include <Library/PcdLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugPrintErrorLevelLib.h>\r
+#include "DebugLibDetect.h"\r
 \r
 //\r
 // Define the maximum debug and assert message length that this library supports\r
@@ -61,9 +62,10 @@ DebugPrint (
   ASSERT (Format != NULL);\r
 \r
   //\r
-  // Check driver debug mask value and global mask\r
+  // Check if the global mask disables this message or the device is inactive\r
   //\r
-  if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
+  if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0 ||\r
+      !PlatformDebugLibIoPortFound ()) {\r
     return;\r
   }\r
 \r
@@ -120,9 +122,11 @@ DebugAssert (
              FileName, (UINT64)LineNumber, Description);\r
 \r
   //\r
-  // Send the print string to the debug I/O port\r
+  // Send the print string to the debug I/O port, if present\r
   //\r
-  IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);\r
+  if (PlatformDebugLibIoPortFound ()) {\r
+    IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer);\r
+  }\r
 \r
   //\r
   // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
@@ -265,3 +269,19 @@ DebugPrintLevelEnabled (
 {\r
   return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
 }\r
+\r
+/**\r
+  Return the result of detecting the debug I/O port device.\r
+\r
+  @retval TRUE   if the debug I/O port device was detected.\r
+  @retval FALSE  otherwise\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformDebugLibIoPortDetect (\r
+  VOID\r
+  )\r
+{\r
+  return IoRead8 (PcdGet16 (PcdDebugIoPort)) == BOCHS_DEBUG_PORT_MAGIC;\r
+}\r
index bad054f2867993b24d838403c16b73898bcfe216..81c44eece95f04baa1978ae894b3f30a2f90b05c 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
-  Constructor code for QEMU debug port library.\r
-  Non-SEC instance.\r
+  Detection code for QEMU debug port.\r
+  Non-SEC instance, caches the result of detection.\r
 \r
   Copyright (c) 2017, Red Hat, Inc.<BR>\r
   This program and the accompanying materials\r
 **/\r
 \r
 #include <Base.h>\r
+#include "DebugLibDetect.h"\r
+\r
+//\r
+// Set to TRUE if the debug I/O port is enabled\r
+//\r
+STATIC BOOLEAN mDebugIoPortFound = FALSE;\r
 \r
 /**\r
-  This constructor function does not have anything to do.\r
+  This constructor function checks if the debug I/O port device is present,\r
+  caching the result for later use.\r
 \r
   @retval RETURN_SUCCESS   The constructor always returns RETURN_SUCCESS.\r
 \r
@@ -27,5 +34,22 @@ PlatformDebugLibIoPortConstructor (
   VOID\r
   )\r
 {\r
+  mDebugIoPortFound = PlatformDebugLibIoPortDetect();\r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Return the cached result of detecting the debug I/O port device.\r
+\r
+  @retval TRUE   if the debug I/O port device was detected.\r
+  @retval FALSE  otherwise\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformDebugLibIoPortFound (\r
+  VOID\r
+  )\r
+{\r
+  return mDebugIoPortFound;\r
+}\r
diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h
new file mode 100644 (file)
index 0000000..1f739b5
--- /dev/null
@@ -0,0 +1,57 @@
+/** @file\r
+  Base Debug library instance for QEMU debug port.\r
+  It uses PrintLib to send debug messages to a fixed I/O port.\r
+\r
+  Copyright (c) 2017, Red Hat, Inc.<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
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#ifndef __DEBUG_IO_PORT_DETECT_H__\r
+#define __DEBUG_IO_PORT_DETECT_H__\r
+\r
+#include <Base.h>\r
+\r
+//\r
+// The constant value that is read from the debug I/O port\r
+//\r
+#define BOCHS_DEBUG_PORT_MAGIC    0xE9\r
+\r
+\r
+/**\r
+  Helper function to return whether the virtual machine has a debug I/O port.\r
+  PlatformDebugLibIoPortFound can call this function directly or cache the\r
+  result.\r
+\r
+  @retval TRUE   if the debug I/O port device was detected.\r
+  @retval FALSE  otherwise\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformDebugLibIoPortDetect (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Return whether the virtual machine has a debug I/O port.  DebugLib.c\r
+  calls this function instead of PlatformDebugLibIoPortDetect, to allow\r
+  caching if possible.\r
+\r
+  @retval TRUE   if the debug I/O port device was detected.\r
+  @retval FALSE  otherwise\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformDebugLibIoPortFound (\r
+  VOID\r
+  );\r
+\r
+#endif\r
index 83a118a0f78f260ad39b5e018a624ec452de82dd..b950919675d3d67e809a1f29840cad32a046a920 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
-  Constructor code for QEMU debug port library.\r
-  SEC instance.\r
+  Detection code for QEMU debug port.\r
+  SEC instance, cannot cache the result of detection.\r
 \r
   Copyright (c) 2017, Red Hat, Inc.<BR>\r
   This program and the accompanying materials\r
@@ -14,6 +14,7 @@
 **/\r
 \r
 #include <Base.h>\r
+#include "DebugLibDetect.h"\r
 \r
 /**\r
   This constructor function does not have anything to do.\r
@@ -29,3 +30,19 @@ PlatformRomDebugLibIoPortConstructor (
 {\r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Return the result of detecting the debug I/O port device.\r
+\r
+  @retval TRUE   if the debug I/O port device was detected.\r
+  @retval FALSE  otherwise\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformDebugLibIoPortFound (\r
+  VOID\r
+  )\r
+{\r
+  return PlatformDebugLibIoPortDetect ();\r
+}\r