From nobody Sun Apr 28 13:13:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1591354995; cv=none; d=zohomail.com; s=zohoarc; b=g1uGKOGUGtRUkUuFzBPn6dwNzu8rjrAV9se5vDNGa88914m1CH0RfAY9lMnzzYdT7HRO5MWcJXa73PLO4+lGxjZRce9IiNpTKJxE0bzDoTtC3GmZ7eOFGOjkl3I/CKWrfaCnSybI5vXIsyBvEvAOIYBeBBztRdDvBdUDpV9PvFU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1591354995; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=R7ofmDey//KAfJG88exzxzTwBLYQVxyk6pQrBQM7tiQ=; b=hci6YIwQQdJ9opL5ai4Olsj+N0AMsn89QvsTfjF+My/cYNoOM1b/yNHcRIuORJcXrnU7wyc6AIGmoxxUn+xbWWs7QvgWHQbUC+n8SnMkLg+NuXqikZLzyOMbmo1YB31xHhYNLGarL51k2FWZ68Y9gCOGsYR8iGapIopDWixRGDg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1591354995541972.4427401291081; Fri, 5 Jun 2020 04:03:15 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jhA7f-0004P9-M3; Fri, 05 Jun 2020 11:02:51 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jhA7e-0004P4-BR for xen-devel@lists.xenproject.org; Fri, 05 Jun 2020 11:02:50 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 17bb47d6-a71c-11ea-af9d-12813bfff9fa; Fri, 05 Jun 2020 11:02:49 +0000 (UTC) X-Inumbo-ID: 17bb47d6-a71c-11ea-af9d-12813bfff9fa Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: ocq4NPS5Lxg6hYuvKZHSF2ee9D1Y0mCrj0p2EcWgZT7OsyifM2RmZeaSDSIDnJl3hIS6906KgS fc6bYPw005hY9H6LSaGimQKzv0zMWjKbWytSWi2nS43l3B7AYaus+FuUYelcAXuVTexGlVJQRB MwlgYeqONzpSXYBo84rEMKZl3BmCyPotSb9b5Wcy6b4BE8fq9m1TQZHefotnzlzlCl9sm6wfRt 2jwAmdvD4XnF3kCJUU6eHDJHyo6uZLWkjv/eUvpaFQvaXO22qSAxK4ncwsxJJGIarW4FmN35zN E50= X-SBRS: 2.7 X-MesageID: 19313218 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.73,476,1583211600"; d="scan'208";a="19313218" From: Roger Pau Monne To: Subject: [PATCH for-4.14 v2] x86/rtc: provide mediated access to RTC for PVH dom0 Date: Fri, 5 Jun 2020 13:02:39 +0200 Message-ID: <20200605110240.52545-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Roger Pau Monne , Wei Liu , Jan Beulich , paul@xen.org Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Mediated access to the RTC was provided for PVHv1 dom0 using the PV code paths (guest_io_{write/read}), but those accesses where never implemented for PVHv2 dom0. This patch provides such mediated accesses to the RTC for PVH dom0, just like it's provided for a classic PV dom0. Pull out some of the RTC logic from guest_io_{read/write} into specific helpers that can be used by both PV and HVM guests. The setup of the handlers for PVH is done in rtc_init, which is already used to initialize the fully emulated RTC. Without this a Linux PVH dom0 will read garbage when trying to access the RTC, and one vCPU will be constantly looping in rtc_timer_do_work. Note that such issue doesn't happen on domUs because the ACPI NO_CMOS_RTC flag is set in FADT, which prevents the OS from accessing the RTC. Also the X86_EMU_RTC flag is not set for PVH dom0, as the accesses are not emulated but rather forwarded to the physical hardware. No functional change expected for classic PV dom0. Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Wei Liu --- for-4.14 reasoning: the fix is mostly isolated to PVH dom0, and as such the risk is very low of causing issues to other guests types, but without this fix one vCPU when using a Linux dom0 will be constantly looping over rtc_timer_do_work with 100% CPU usage, at least when using Linux 4.19 or newer. --- Changes since v1: - Share the code with PV. - Add access checks to the IO ports. --- xen/arch/x86/hvm/rtc.c | 34 ++++++++++++++++++ xen/arch/x86/pv/emul-priv-op.c | 28 +++------------ xen/arch/x86/time.c | 59 +++++++++++++++++++++++++++++++ xen/include/asm-x86/mc146818rtc.h | 3 ++ 4 files changed, 100 insertions(+), 24 deletions(-) diff --git a/xen/arch/x86/hvm/rtc.c b/xen/arch/x86/hvm/rtc.c index 5bbbdc0e0f..9f304a0fb4 100644 --- a/xen/arch/x86/hvm/rtc.c +++ b/xen/arch/x86/hvm/rtc.c @@ -22,6 +22,7 @@ * IN THE SOFTWARE. */ =20 +#include #include #include #include @@ -808,10 +809,43 @@ void rtc_reset(struct domain *d) s->pt.source =3D PTSRC_isa; } =20 +/* RTC mediator for HVM hardware domain. */ +static int hw_rtc_io(int dir, unsigned int port, unsigned int size, + uint32_t *val) +{ + if ( dir =3D=3D IOREQ_READ ) + *val =3D ~0; + + if ( size !=3D 1 ) + { + gdprintk(XENLOG_WARNING, "bad RTC access size (%u)\n", size); + return X86EMUL_OKAY; + } + if ( !ioports_access_permitted(current->domain, port, port) ) + { + gdprintk(XENLOG_WARNING, "access to RTC port %x not allowed\n", po= rt); + return X86EMUL_OKAY; + } + + if ( dir =3D=3D IOREQ_WRITE ) + rtc_guest_write(port, *val); + else + *val =3D rtc_guest_read(port); + + return X86EMUL_OKAY; +} + void rtc_init(struct domain *d) { RTCState *s =3D domain_vrtc(d); =20 + if ( is_hardware_domain(d) ) + { + /* Hardware domain gets mediated access to the physical RTC. */ + register_portio_handler(d, RTC_PORT(0), 2, hw_rtc_io); + return; + } + if ( !has_vrtc(d) ) return; =20 diff --git a/xen/arch/x86/pv/emul-priv-op.c b/xen/arch/x86/pv/emul-priv-op.c index fad6c754ad..1597f27ed9 100644 --- a/xen/arch/x86/pv/emul-priv-op.c +++ b/xen/arch/x86/pv/emul-priv-op.c @@ -280,19 +280,10 @@ static uint32_t guest_io_read(unsigned int port, unsi= gned int bytes, { sub_data =3D pv_pit_handler(port, 0, 0); } - else if ( port =3D=3D RTC_PORT(0) ) - { - sub_data =3D currd->arch.cmos_idx; - } - else if ( (port =3D=3D RTC_PORT(1)) && + else if ( (port =3D=3D RTC_PORT(0) || port =3D=3D RTC_PORT(1)) && ioports_access_permitted(currd, RTC_PORT(0), RTC_PORT(1)= ) ) { - unsigned long flags; - - spin_lock_irqsave(&rtc_lock, flags); - outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); - sub_data =3D inb(RTC_PORT(1)); - spin_unlock_irqrestore(&rtc_lock, flags); + sub_data =3D rtc_guest_read(port); } else if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) ) { @@ -413,21 +404,10 @@ static void guest_io_write(unsigned int port, unsigne= d int bytes, { pv_pit_handler(port, (uint8_t)data, 1); } - else if ( port =3D=3D RTC_PORT(0) ) - { - currd->arch.cmos_idx =3D data; - } - else if ( (port =3D=3D RTC_PORT(1)) && + else if ( (port =3D=3D RTC_PORT(0) || port =3D=3D RTC_PORT(1)) && ioports_access_permitted(currd, RTC_PORT(0), RTC_PORT(1)= ) ) { - unsigned long flags; - - if ( pv_rtc_handler ) - pv_rtc_handler(currd->arch.cmos_idx & 0x7f, data); - spin_lock_irqsave(&rtc_lock, flags); - outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); - outb(data, RTC_PORT(1)); - spin_unlock_irqrestore(&rtc_lock, flags); + rtc_guest_write(port, data); } else if ( (port =3D=3D 0xcf8) && (bytes =3D=3D 4) ) { diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index bbaea3aa65..e1c81067ce 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -1110,6 +1111,64 @@ static unsigned long get_cmos_time(void) return mktime(rtc.year, rtc.mon, rtc.day, rtc.hour, rtc.min, rtc.sec); } =20 +/* Helpers for guest accesses to the physical RTC. */ +unsigned int rtc_guest_read(unsigned int port) +{ + const struct domain *currd =3D current->domain; + unsigned long flags; + unsigned int data =3D ~0; + + ASSERT(port =3D=3D RTC_PORT(0) || port =3D=3D RTC_PORT(1)); + if ( !ioports_access_permitted(currd, port, port) ) + { + ASSERT_UNREACHABLE(); + return data; + } + + switch ( port ) + { + case RTC_PORT(0): + data =3D currd->arch.cmos_idx; + break; + + case RTC_PORT(1): + spin_lock_irqsave(&rtc_lock, flags); + outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); + data =3D inb(RTC_PORT(1)); + spin_unlock_irqrestore(&rtc_lock, flags); + break; + } + + return data; +} + +void rtc_guest_write(unsigned int port, unsigned int data) +{ + struct domain *currd =3D current->domain; + unsigned long flags; + + ASSERT(port =3D=3D RTC_PORT(0) || port =3D=3D RTC_PORT(1)); + if ( !ioports_access_permitted(currd, port, port) ) + { + ASSERT_UNREACHABLE(); + return; + } + + switch ( port ) + { + case RTC_PORT(0): + currd->arch.cmos_idx =3D data; + break; + + case RTC_PORT(1): + spin_lock_irqsave(&rtc_lock, flags); + outb(currd->arch.cmos_idx & 0x7f, RTC_PORT(0)); + outb(data, RTC_PORT(1)); + spin_unlock_irqrestore(&rtc_lock, flags); + break; + } +} + static unsigned long get_wallclock_time(void) { #ifdef CONFIG_XEN_GUEST diff --git a/xen/include/asm-x86/mc146818rtc.h b/xen/include/asm-x86/mc1468= 18rtc.h index 8758528f7c..803b236c0a 100644 --- a/xen/include/asm-x86/mc146818rtc.h +++ b/xen/include/asm-x86/mc146818rtc.h @@ -110,4 +110,7 @@ outb_p((val),RTC_PORT(1)); \ =20 #define RTC_IRQ 8 =20 +unsigned int rtc_guest_read(unsigned int port); +void rtc_guest_write(unsigned int port, unsigned int data); + #endif /* _ASM_MC146818RTC_H */ --=20 2.26.2