From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=209.132.183.28; helo=mx1.redhat.com; envelope-from=lersek@redhat.com; receiver=edk2-devel@lists.01.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 36FC82035A7B3 for ; Thu, 16 Nov 2017 11:58:55 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DF03B7EAA5; Thu, 16 Nov 2017 20:03:04 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-80.rdu2.redhat.com [10.10.120.80]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA534B32B8; Thu, 16 Nov 2017 20:03:03 +0000 (UTC) To: Paolo Bonzini , edk2-devel@lists.01.org Cc: Jordan Justen , Ard Biesheuvel References: <20171116104716.15144-1-pbonzini@redhat.com> <20171116104716.15144-3-pbonzini@redhat.com> From: Laszlo Ersek Message-ID: <3b0acb21-9b10-7f34-d599-7b1bbe8ab329@redhat.com> Date: Thu, 16 Nov 2017 21:03:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20171116104716.15144-3-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 16 Nov 2017 20:03:04 +0000 (UTC) Subject: Re: [PATCH 2/2] OvmfPkg: save on I/O port accesses when the debug port is not in use X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Nov 2017 19:58:56 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 11/16/17 11:47, Paolo Bonzini wrote: > 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 . > > 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.1 > Cc: Laszlo Ersek > Cc: Ard Biesheuvel > Cc: Jordan Justen (Intel address) > Signed-off-by: Paolo Bonzini > --- > OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c | 26 ++++++++-- > .../PlatformDebugLibIoPort/DebugLibDetect.c | 29 +++++++++-- > .../PlatformDebugLibIoPort/DebugLibDetect.h | 56 ++++++++++++++++++++++ > .../PlatformDebugLibIoPort/DebugLibDetectRom.c | 20 +++++++- > 4 files changed, 122 insertions(+), 9 deletions(-) > create mode 100644 OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h > > diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c > index a5572a8eeb..79486ac8a6 100644 > --- a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c > +++ b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLib.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include "DebugLibDetect.h" > > // > // Define the maximum debug and assert message length that this library supports > @@ -62,9 +63,9 @@ DebugPrint ( > ASSERT (Format != NULL); > > // > - // Check driver debug mask value and global mask > + // Do nothing if the global mask disables this message or the device is inactive > // > - if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) { > + if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0 || !PlatformDebugLibIoPortFound ()) { (1) I feel terrible for wasting your time with this -- we prefer to keep a line length of 80 characters: if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0 || !PlatformDebugLibIoPortFound ()) { ("we prefer" is a bit of a lie -- I personally totally insist on it :) , while many people ignore it until I raise a stink about their overlong lines. So I guess "we prefer" is an expected value, with large stddev.) > return; > } > > @@ -121,9 +122,11 @@ DebugAssert ( > FileName, (UINT64)LineNumber, Description); > > // > - // Send the print string to the debug I/O port > + // Send the print string to the debug I/O port, if present > // > - IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer); > + if (PlatformDebugLibIoPortFound ()) { > + IoWriteFifo8 (PcdGet16 (PcdDebugIoPort), Length, Buffer); > + } > > // > // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings > @@ -266,3 +269,18 @@ DebugPrintLevelEnabled ( > { > return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0); > } > + > +/** > + Return the result of detecting the debug I/O port device. > + > + @retval BOOLEAN TRUE if the debug I/O port device was detected. (2) Another non-intuitive edk2 quirk; we have two possibilities for retval documentation: @retval VALUE description and @return just description For example, @retval TRUE if the debug I/O port device was detected @retval FALSE otherwise and @return a BOOLEAN indicating whether the debug I/O port device was detected (I apologize -- I know this is annoying.) > + > +**/ > +BOOLEAN > +EFIAPI > +PlatformDebugLibIoPortDetect ( > + VOID > + ) > +{ > + return IoRead8 (PcdGet16 (PcdDebugIoPort)) == BOCHS_DEBUG_PORT_MAGIC; > +} > diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c > index fee908861b..610987aca9 100644 > --- a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c > +++ b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.c > @@ -1,6 +1,6 @@ > /** @file > - Constructor code for QEMU debug port library. > - Non-SEC instance. > + Detection code for QEMU debug port. > + Non-SEC instance, caches the result of detection. > > Copyright (c) 2017, Red Hat, Inc.
> This program and the accompanying materials > @@ -15,9 +15,16 @@ > > #include > #include > +#include "DebugLibDetect.h" > + > +// > +// Set to TRUE if the debug I/O port is enabled > +// > +STATIC BOOLEAN mDebugIoPortFound = FALSE; > > /** > - This constructor function does not have anything to do. > + This constructor function checks if the debug I/O port device is present, > + caching the result for later use. > > @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. > > @@ -28,5 +35,21 @@ PlatformDebugLibIoPortConstructor ( > VOID > ) > { > + mDebugIoPortFound = PlatformDebugLibIoPortDetect(); > return EFI_SUCCESS; > } > + > +/** > + Return the cached result of detecting the debug I/O port device. > + > + @retval BOOLEAN TRUE if the debug I/O port device was detected. > + > +**/ > +BOOLEAN > +EFIAPI > +PlatformDebugLibIoPortFound ( > + VOID > + ) > +{ > + return mDebugIoPortFound; > +} > diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h > new file mode 100644 > index 0000000000..c34ca9c72b > --- /dev/null > +++ b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetect.h > @@ -0,0 +1,56 @@ > +/** @file > + Base Debug library instance for QEMU debug port. > + It uses PrintLib to send debug messages to a fixed I/O port. > + > + Copyright (c) 2017, Red Hat, Inc.
> + This program and the accompanying materials > + are licensed and made available under the terms and conditions of the BSD License > + which accompanies this distribution. The full text of the license may be found at > + http://opensource.org/licenses/bsd-license.php. > + > + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. > + > +**/ > + > +#ifndef __DEBUG_IO_PORT_DETECT_H__ > +#define __DEBUG_IO_PORT_DETECT_H__ > + > +#include > +#include (3) As suggested before, please try to replace this with . I tested the patches (in SEV and non-SEV guests) and they work as advertized. For v3: Tested-by: Laszlo Ersek Thank you! Laszlo > + > +// > +// The constant value that is read from the debug I/O port > +// > +#define BOCHS_DEBUG_PORT_MAGIC 0xE9 > + > + > +/** > + Helper function to return whether the virtual machine has a debug I/O port. > + PlatformDebugLibIoPortFound can call this function directly or cache the > + result. > + > + @retval BOOLEAN TRUE if the debug I/O port device was detected. > + > +**/ > +BOOLEAN > +EFIAPI > +PlatformDebugLibIoPortDetect ( > + VOID > + ); > + > +/** > + Return whether the virtual machine has a debug I/O port. DebugLib.c > + calls this function instead of PlatformDebugLibIoPortDetect, to allow > + caching if possible. > + > + @retval BOOLEAN TRUE if the debug I/O port device was detected. > + > +**/ > +BOOLEAN > +EFIAPI > +PlatformDebugLibIoPortFound ( > + VOID > + ); > + > +#endif > diff --git a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetectRom.c b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetectRom.c > index 407fe613ff..f71b6567dc 100644 > --- a/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetectRom.c > +++ b/OvmfPkg/Library/PlatformDebugLibIoPort/DebugLibDetectRom.c > @@ -1,6 +1,6 @@ > /** @file > - Constructor code for QEMU debug port library. > - SEC instance. > + Detection code for QEMU debug port. > + SEC instance, cannot cache the result of detection. > > Copyright (c) 2017, Red Hat, Inc.
> This program and the accompanying materials > @@ -15,6 +15,7 @@ > > #include > #include > +#include "DebugLibDetect.h" > > /** > This constructor function does not have to do anything. > @@ -30,3 +31,18 @@ PlatformRomDebugLibIoPortConstructor ( > { > return EFI_SUCCESS; > } > + > +/** > + Return the result of detecting the debug I/O port device. > + > + @retval BOOLEAN TRUE if the debug I/O port device was detected. > + > +**/ > +BOOLEAN > +EFIAPI > +PlatformDebugLibIoPortFound ( > + VOID > + ) > +{ > + return PlatformDebugLibIoPortDetect (); > +} >