From patchwork Sat Mar 18 10:53:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhang X-Patchwork-Id: 9632119 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 07421602D6 for ; Sat, 18 Mar 2017 11:15:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE42C28449 for ; Sat, 18 Mar 2017 11:15:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0C8828450; Sat, 18 Mar 2017 11:15:10 +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 5E3A528449 for ; Sat, 18 Mar 2017 11:15:10 +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 1cpCIJ-0001g9-Sh; Sat, 18 Mar 2017 11:13:11 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cpCIJ-0001fq-3n for xen-devel@lists.xen.org; Sat, 18 Mar 2017 11:13:11 +0000 Received: from [85.158.143.35] by server-5.bemta-6.messagelabs.com id EE/A5-27545-6461DC85; Sat, 18 Mar 2017 11:13:10 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrNLMWRWlGSWpSXmKPExsXS1tYhr+sqdjb C4ONsQ4slHxezODB6HN39mymAMYo1My8pvyKBNWPZDOuCFVIV9393MzUw7hbuYuTgEBKolPj/ XKOLkZNDQoBX4siyGawQtp/ErpsvmUFsIYF2RolrLzhAbDYBbYkfq38zgtgiAtIS1z5fBrK5O JgF+hkl5j1qAmsWFkiR2PfrGRuIzSKgKvHy3Q0WkF28Ap4Suy9VQ8yXkzh5bDJYOaeAl8SJG6 +gdnlK7F/3gW0CI+8CRoZVjBrFqUVlqUW6xoZ6SUWZ6RkluYmZObqGBmZ6uanFxYnpqTmJScV 6yfm5mxiBgcAABDsYmxYFHmKU5GBSEuX9PPdghBBfUn5KZUZicUZ8UWlOavEhRhkODiUJ3qf/ gHKCRanpqRVpmTnAkIRJS3DwKInwBv0HSvMWFyTmFmemQ6ROMSpKifMqgSQEQBIZpXlwbbA4u MQoKyXMywh0iBBPQWpRbmYJqvwrRnEORiVh3s8g23ky80rgpr8CWswEtHgaH9jikkSElFQDY3 Dy9nsOr3z2rkjtk00+bvhsjSTrwtKDb5zPxtVwP3idnrMqYp8Xn3LRyohHrf3zfcNO6GU6Rkp umjG95O3PXe6N1jXPfvcYfa369/BD+f+Ty/+fPSj7SHnF16Zz+3wC3ZdeLOS0Ne98H3ron9/3 vr/H7GW8PJfJ7pJLVLqyUvHwluSJ6xx19ZVYijMSDbWYi4oTAUEfWpZ+AgAA X-Env-Sender: yu.c.zhang@linux.intel.com X-Msg-Ref: server-16.tower-21.messagelabs.com!1489835587!58458127!1 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.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 10457 invoked from network); 18 Mar 2017 11:13:09 -0000 Received: from mga06.intel.com (HELO mga06.intel.com) (134.134.136.31) by server-16.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 18 Mar 2017 11:13:09 -0000 Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga104.jf.intel.com with ESMTP; 18 Mar 2017 04:13:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,182,1486454400"; d="scan'208";a="77904873" Received: from zhangyu-optiplex-9020.bj.intel.com ([10.238.135.159]) by fmsmga005.fm.intel.com with ESMTP; 18 Mar 2017 04:13:05 -0700 From: Yu Zhang To: xen-devel@lists.xen.org Date: Sat, 18 Mar 2017 18:53:53 +0800 Message-Id: <1489834435-6166-4-git-send-email-yu.c.zhang@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1489834435-6166-1-git-send-email-yu.c.zhang@linux.intel.com> References: <1489834435-6166-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 v8 3/5] 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 --- Cc: Paul Durrant Cc: Jan Beulich Cc: Andrew Cooper changes in v2: - 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 37139e6..52c726e 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -94,6 +94,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) @@ -193,6 +213,9 @@ static int hvmemul_do_io( * - If the domain ioreq_server is NULL, assume there is a * race between the unbinding of ioreq server and guest fault * 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. */ struct hvm_ioreq_server *s = NULL; p2m_type_t p2mt = p2m_invalid; @@ -226,6 +249,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 cached. + */ + if ( dir == IOREQ_READ ) + { + rc = hvm_process_io_intercept(&ioreq_server_handler, &p); + vio->io_req.state = STATE_IOREQ_NONE; + break; + } } }