From patchwork Thu Apr 6 15:53:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhang X-Patchwork-Id: 9667833 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4EBFC602B8 for ; Thu, 6 Apr 2017 16:15:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 91E7C2859F for ; Thu, 6 Apr 2017 16:15:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 86D18285A4; Thu, 6 Apr 2017 16:15:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 686102859F for ; Thu, 6 Apr 2017 16:15:13 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cwA27-0005i5-4e; Thu, 06 Apr 2017 16:13:15 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cwA25-0005hH-SN for xen-devel@lists.xen.org; Thu, 06 Apr 2017 16:13:13 +0000 Received: from [85.158.139.211] by server-9.bemta-5.messagelabs.com id 9C/5E-32461-91966E85; Thu, 06 Apr 2017 16:13:13 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrHLMWRWlGSWpSXmKPExsXS1tYhryuR+Sz C4O5kGYslHxezODB6HN39mymAMYo1My8pvyKBNePEkzWsBfukK1Ye7WduYNwq0sXIySEkUCmx eudvNhBbQoBX4siyGawQtr/EmT0H2LsYuYBq2hklDp2fwQiSYBPQlvix+jeYLSIgLXHt82VGk CJmgX5GiXmPmsC6hQVSJS4seMgMYrMIqEr8O/4OrIFXwEvi0rKZzBAb5CROHpsMVs8p4C2xsX kiK8RFXhIdCyayTGDkXcDIsIpRvTi1qCy1SNdML6koMz2jJDcxM0fX0MBULze1uDgxPTUnMal YLzk/dxMjMBwYgGAH49QG50OMkhxMSqK8Cj5PIoT4kvJTKjMSizPii0pzUosPMcpwcChJ8Aan P4sQEixKTU+tSMvMAQYmTFqCg0dJhNceJM1bXJCYW5yZDpE6xagoJc57JQ0oIQCSyCjNg2uDR cMlRlkpYV5GoEOEeApSi3IzS1DlXzGKczAqCfO2gYznycwrgZv+CmgxE9Bin1tPQRaXJCKkpB oYq+5EWDHu6m7hZmC/fay1ObfZ0jT+qc+eHREfZ4YwzWjP6OH2sprV9N8z4KuT+6pXL69GGMu 3Xe689niimdavzNTPQqpznofoSDxcMX+b8M76Lfzv/7YwyP/d3X9i2Rs/JcOcbT/9DqUZ3b8m u7fC3Y/1+aSDryZpfGpuPlN4kunzi4fhjLeclFiKMxINtZiLihMBZAB9D4ECAAA= X-Env-Sender: yu.c.zhang@linux.intel.com X-Msg-Ref: server-15.tower-206.messagelabs.com!1491495187!78261313!2 X-Originating-IP: [134.134.136.31] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 9.4.12; banners=-,-,- X-VirusChecked: Checked Received: (qmail 50891 invoked from network); 6 Apr 2017 16:13:12 -0000 Received: from mga06.intel.com (HELO mga06.intel.com) (134.134.136.31) by server-15.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 6 Apr 2017 16:13:12 -0000 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP; 06 Apr 2017 09:13:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.37,160,1488873600"; d="scan'208";a="842784711" Received: from zhangyu-optiplex-9020.bj.intel.com ([10.238.135.159]) by FMSMGA003.fm.intel.com with ESMTP; 06 Apr 2017 09:13:10 -0700 From: Yu Zhang To: xen-devel@lists.xen.org Date: Thu, 6 Apr 2017 23:53:35 +0800 Message-Id: <1491494017-30743-5-git-send-email-yu.c.zhang@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1491494017-30743-1-git-send-email-yu.c.zhang@linux.intel.com> References: <1491494017-30743-1-git-send-email-yu.c.zhang@linux.intel.com> Cc: Andrew Cooper , Paul Durrant , zhiyuan.lv@intel.com, Jan Beulich Subject: [Xen-devel] [PATCH v12 4/6] x86/ioreq server: Handle read-modify-write cases for p2m_ioreq_server pages. X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP In ept_handle_violation(), write violations are also treated as read violations. And when a VM is accessing a write-protected address with read-modify-write instructions, the read emulation process is triggered first. For p2m_ioreq_server pages, current ioreq server only forwards the write operations to the device model. Therefore when such page is being accessed by a read-modify-write instruction, the read operations should be emulated here in hypervisor. This patch provides such a handler to copy the data to the buffer. Note: MMIOs with p2m_mmio_dm type do not need such special treatment because both reads and writes will go to the device mode. Signed-off-by: Paul Durrant Signed-off-by: Yu Zhang Reviewed-by: Jan Beulich --- Cc: Paul Durrant Cc: Jan Beulich Cc: Andrew Cooper changes in v4: - Added "Reviewed-by: Jan Beulich " with one comment change in hvmemul_do_io(). changes in v3: - According to comments from Jan: clarify comments in hvmemul_do_io(). changes in v2: - According to comments from Jan: rename mem_ops to ioreq_server_ops. - According to comments from Jan: use hvm_copy_from_guest_phys() in ioreq_server_read(), instead of do it by myself. --- xen/arch/x86/hvm/emulate.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index dc6f1f2..4de3936 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -95,6 +95,26 @@ static const struct hvm_io_handler null_handler = { .ops = &null_ops }; +static int ioreq_server_read(const struct hvm_io_handler *io_handler, + uint64_t addr, + uint32_t size, + uint64_t *data) +{ + if ( hvm_copy_from_guest_phys(data, addr, size) != HVMCOPY_okay ) + return X86EMUL_UNHANDLEABLE; + + return X86EMUL_OKAY; +} + +static const struct hvm_io_ops ioreq_server_ops = { + .read = ioreq_server_read, + .write = null_write +}; + +static const struct hvm_io_handler ioreq_server_handler = { + .ops = &ioreq_server_ops +}; + static int hvmemul_do_io( bool_t is_mmio, paddr_t addr, unsigned long *reps, unsigned int size, uint8_t dir, bool_t df, bool_t data_is_addr, uintptr_t data) @@ -195,6 +215,9 @@ static int hvmemul_do_io( * a race with an unmap operation on the ioreq server, so re-try the * instruction. * + * - If the accesss is a read, this could be part of a + * read-modify-write instruction, emulate the read first. + * * Note: Even when an ioreq server is found, its value could become * stale later, because it is possible that * @@ -228,6 +251,17 @@ static int hvmemul_do_io( vio->io_req.state = STATE_IOREQ_NONE; break; } + + /* + * This is part of a read-modify-write instruction. + * Emulate the read part so we have the value available. + */ + if ( dir == IOREQ_READ ) + { + rc = hvm_process_io_intercept(&ioreq_server_handler, &p); + vio->io_req.state = STATE_IOREQ_NONE; + break; + } } }