From patchwork Thu Oct 26 17:03:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Euan Harris X-Patchwork-Id: 10028643 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 9260E601E8 for ; Thu, 26 Oct 2017 17:06:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F51428E75 for ; Thu, 26 Oct 2017 17:06:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7429028E81; Thu, 26 Oct 2017 17:06:18 +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 139EE28E7F for ; Thu, 26 Oct 2017 17:06:18 +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 1e7lZW-0002ex-3S; Thu, 26 Oct 2017 17:03:58 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e7lZU-0002e4-P6 for xen-devel@lists.xenproject.org; Thu, 26 Oct 2017 17:03:56 +0000 Received: from [85.158.139.211] by server-3.bemta-5.messagelabs.com id 4E/97-29298-C7512F95; Thu, 26 Oct 2017 17:03:56 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprDIsWRWlGSWpSXmKPExsXitHSDvW616Kd IgyWHVC2+b5nM5MDocfjDFZYAxijWzLyk/IoE1oyXk+6xFCyXqWj4co2tgfGqWBcjJ4eEgL/E k6vPmEFsNgEtid0fFrJ3MXJwiAioSNzea9DFyMXBLNDLKLH0/QUmkBphAU+JY39OgNWzCKhKr Ps5iQXE5hVwk9h48yUTxEwFiSkP34PVcAq4SxxauIkNxBYCqrn47RcrhK0s8eHKGnaIXkGJkz OfgM1hFpCQOPjiBfMERt5ZSFKzkKQWMDKtYlQvTi0qSy3SNddLKspMzyjJTczM0TU0MNXLTS0 uTkxPzUlMKtZLzs/dxAgMHQYg2MF4bLLzIUZJDiYlUV6GPR8ihfiS8lMqMxKLM+KLSnNSiw8x ynBwKEnwThL5FCkkWJSanlqRlpkDDGKYtAQHj5IIbwdImre4IDG3ODMdInWK0ZLj2KbLf5g4O m7eBZLPZr5uYBZiycvPS5US5zUFaRAAacgozYMbB4u0S4yyUsK8jEAHCvEUpBblZpagyr9iFO dgVBLmbQaZwpOZVwK39RXQQUxABzWpfgA5qCQRISXVwKjSGPBPQjv7vMiShXaqW+Iu7JQWPbZ IUTRooXeLpH6NJNu2jtXJK+68vxh9c+1qm6ef9a6emvbjVJr66+lilemfbz6xfFpknjUrhV04 3G7yggtOoWcvLyqZ4+K5rW9O0NrL600rZs628hZbo1OZ6re8PH/qB/89RkzJ6UZsd3OveVsrm e6SDVFiKc5INNRiLipOBAD4wQHFrwIAAA== X-Env-Sender: prvs=465eced58=euan.harris@citrix.com X-Msg-Ref: server-8.tower-206.messagelabs.com!1509037432!108704426!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 4983 invoked from network); 26 Oct 2017 17:03:55 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-8.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 26 Oct 2017 17:03:55 -0000 X-IronPort-AV: E=Sophos;i="5.44,301,1505779200"; d="scan'208";a="456259613" From: Euan Harris To: Date: Thu, 26 Oct 2017 18:03:18 +0100 Message-ID: <1509037399-48926-9-git-send-email-euan.harris@citrix.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1509037399-48926-1-git-send-email-euan.harris@citrix.com> References: <1509037399-48926-1-git-send-email-euan.harris@citrix.com> MIME-Version: 1.0 Cc: andrew.cooper3@citrix.com, kevin.tian@intel.com, Euan Harris , jun.nakajima@intel.com, jbeulich@suse.com Subject: [Xen-devel] [PATCH 8/9] x86/hvm: Add hvm_copy_{to, from}_guest_virt() helpers 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: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP hvm_copy_{to,from}_guest_virt() copy data to and from a guest, performing segmentatino and paging checks on the provided seg:offset virtual address. Signed-off-by: Euan Harris --- xen/arch/x86/hvm/hvm.c | 57 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/support.h | 12 +++++++++ 2 files changed, 69 insertions(+) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 205b4cb685..5d2bdd6b2b 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3312,6 +3312,63 @@ unsigned long copy_from_user_hvm(void *to, const void *from, unsigned len) return rc ? len : 0; /* fake a copy_from_user() return code */ } +static int _hvm_copy_guest_virt( + enum x86_segment seg, unsigned long offset, void *buf, unsigned int bytes, + uint32_t pfec, unsigned int flags) +{ + struct vcpu *curr = current; + struct segment_register sreg, cs; + enum hvm_translation_result res; + pagefault_info_t pfinfo; + unsigned long linear; + + ASSERT(is_x86_user_segment(seg)); + + hvm_get_segment_register(curr, seg, &sreg); + hvm_get_segment_register(curr, x86_seg_cs, &cs); + + if ( !hvm_virtual_to_linear_addr( + seg, &sreg, offset, bytes, + flags & HVMCOPY_to_guest ? hvm_access_write : hvm_access_read, + &cs, &linear) ) + { + hvm_inject_hw_exception( + (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault, 0); + return X86EMUL_EXCEPTION; + } + + if ( flags & HVMCOPY_to_guest ) + res = hvm_copy_to_guest_linear(linear, buf, bytes, pfec, &pfinfo); + else + res = hvm_copy_from_guest_linear(buf, linear, bytes, pfec, &pfinfo); + + if ( res == HVMTRANS_bad_linear_to_gfn ) + { + hvm_inject_page_fault(pfinfo.ec, pfinfo.linear); + return X86EMUL_EXCEPTION; + } + else if ( res ) + return X86EMUL_RETRY; + + return X86EMUL_OKAY; +} + +int hvm_copy_to_guest_virt( + enum x86_segment seg, unsigned long offset, void *buf, unsigned int bytes, + uint32_t pfec) +{ + return _hvm_copy_guest_virt(seg, offset, buf, bytes, pfec, + HVMCOPY_to_guest); +} + +int hvm_copy_from_guest_virt( + void *buf, enum x86_segment seg, unsigned long offset, unsigned int bytes, + uint32_t pfec) +{ + return _hvm_copy_guest_virt(seg, offset, buf, bytes, pfec, + HVMCOPY_from_guest); +} + bool hvm_check_cpuid_faulting(struct vcpu *v) { const struct msr_vcpu_policy *vp = v->arch.msr; diff --git a/xen/include/asm-x86/hvm/support.h b/xen/include/asm-x86/hvm/support.h index d784fc1856..9af2ae77b7 100644 --- a/xen/include/asm-x86/hvm/support.h +++ b/xen/include/asm-x86/hvm/support.h @@ -115,6 +115,18 @@ enum hvm_translation_result hvm_translate_get_page( pagefault_info_t *pfinfo, struct page_info **page_p, gfn_t *gfn_p, p2m_type_t *p2mt_p); +/* + * Copy data to and from a guest, performing segmentation and paging checks + * on the provided seg:offset virtual address. + * Returns X86EMUL_* and raises exceptions with the current vcpu. + */ +int hvm_copy_to_guest_virt( + enum x86_segment seg, unsigned long offset, void *buf, unsigned int bytes, + uint32_t pfec); +int hvm_copy_from_guest_virt( + void *buf, enum x86_segment seg, unsigned long offset, unsigned int bytes, + uint32_t pfec); + #define HVM_HCALL_completed 0 /* hypercall completed - no further action */ #define HVM_HCALL_preempted 1 /* hypercall preempted - re-execute VMCALL */ int hvm_hypercall(struct cpu_user_regs *regs);