From patchwork Fri Sep 2 10:47:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhang X-Patchwork-Id: 9310707 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 C9BC860760 for ; Fri, 2 Sep 2016 11:07:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB2B929789 for ; Fri, 2 Sep 2016 11:07:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AFCA62978D; Fri, 2 Sep 2016 11:07:02 +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 5A21029789 for ; Fri, 2 Sep 2016 11:07:01 +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 1bfmHD-0004la-Ge; Fri, 02 Sep 2016 11:04:51 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bfmHC-0004kn-E1 for xen-devel@lists.xen.org; Fri, 02 Sep 2016 11:04:50 +0000 Received: from [85.158.137.68] by server-8.bemta-3.messagelabs.com id 81/84-10540-1DC59C75; Fri, 02 Sep 2016 11:04:49 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrLLMWRWlGSWpSXmKPExsVywNwkVvdCzMl wg9vb+C2WfFzM4sDocXT3b6YAxijWzLyk/IoE1oy9zUuYC06JV8x8fYm9gbFPqIuRk0NIoFLi 3NIVTCC2hACvxJFlM1ghbF+JKZdOMEHU1EvsXX+dDcRmE9CW+LH6NyOILSIgLXHt82Ugm4uDW aCfUWLeoyawZmGBFIknDyaBNbAIqEpc3t/J3sXIwcEr4CVx4KETxHw5iZPHJoOVcwp4S3Sdes AGsctL4sOTU6wTGHkXMDKsYtQoTi0qSy3SNTTTSyrKTM8oyU3MzNE1NDDWy00tLk5MT81JTCr WS87P3cQIDAYGINjBuGq75yFGSQ4mJVHeBwEnw4X4kvJTKjMSizPii0pzUosPMcpwcChJ8E6M BsoJFqWmp1akZeYAwxImLcHBoyTCexkkzVtckJhbnJkOkTrFqCglzrsDJCEAksgozYNrg8XCJ UZZKWFeRqBDhHgKUotyM0tQ5V8xinMwKgnzzgeZwpOZVwI3/RXQYiagxSXXjoMsLklESEk1MC pk3erf1mJ5OiMk5vPTH/aPN+ktni01aUqHtFlNptY5N+kjU68u6H4ldnSfatursuKEZexqrZX vs4+tq5995OgxRUEpbROJmXanROd996jnXfLSma2noNRk+7bD4s9s4yq37O8WjwjZ8No35vOa 1hfCdmvO7GLlbU7892DuLbMrL43fbnc+c0qJpTgj0VCLuag4EQASWOW/gAIAAA== X-Env-Sender: yu.c.zhang@linux.intel.com X-Msg-Ref: server-4.tower-31.messagelabs.com!1472814285!8445133!1 X-Originating-IP: [192.55.52.93] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTkyLjU1LjUyLjkzID0+IDMyNDY2NQ==\n X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 48716 invoked from network); 2 Sep 2016 11:04:47 -0000 Received: from mga11.intel.com (HELO mga11.intel.com) (192.55.52.93) by server-4.tower-31.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 2 Sep 2016 11:04:47 -0000 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP; 02 Sep 2016 04:04:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,270,1470726000"; d="scan'208";a="3948362" Received: from zhangyu-xengt.bj.intel.com ([10.238.154.168]) by fmsmga006.fm.intel.com with ESMTP; 02 Sep 2016 04:04:44 -0700 From: Yu Zhang To: xen-devel@lists.xen.org Date: Fri, 2 Sep 2016 18:47:19 +0800 Message-Id: <1472813240-11011-4-git-send-email-yu.c.zhang@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1472813240-11011-1-git-send-email-yu.c.zhang@linux.intel.com> References: <1472813240-11011-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 v6 3/4] 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 --- xen/arch/x86/hvm/emulate.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index a33346e..f92de48 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -95,6 +95,41 @@ static const struct hvm_io_handler null_handler = { .ops = &null_ops }; +static int mem_read(const struct hvm_io_handler *io_handler, + uint64_t addr, + uint32_t size, + uint64_t *data) +{ + struct domain *currd = current->domain; + unsigned long gmfn = paddr_to_pfn(addr); + unsigned long offset = addr & ~PAGE_MASK; + struct page_info *page = get_page_from_gfn(currd, gmfn, NULL, P2M_UNSHARE); + uint8_t *p; + + ASSERT(offset + size < PAGE_SIZE); + + if ( !page ) + return X86EMUL_UNHANDLEABLE; + + p = __map_domain_page(page); + p += offset; + memcpy(data, p, size); + + unmap_domain_page(p); + put_page(page); + + return X86EMUL_OKAY; +} + +static const struct hvm_io_ops mem_ops = { + .read = mem_read, + .write = null_write +}; + +static const struct hvm_io_handler mem_handler = { + .ops = &mem_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) @@ -204,7 +239,15 @@ static int hvmemul_do_io( /* If there is no suitable backing DM, just ignore accesses */ if ( !s ) { - rc = hvm_process_io_intercept(&null_handler, &p); + /* + * For p2m_ioreq_server pages accessed with read-modify-write + * instructions, we provide a read handler to copy the data to + * the buffer. + */ + if ( p2mt == p2m_ioreq_server ) + rc = hvm_process_io_intercept(&mem_handler, &p); + else + rc = hvm_process_io_intercept(&null_handler, &p); vio->io_req.state = STATE_IOREQ_NONE; } else