From patchwork Mon Feb 6 14:57:47 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Dyasli X-Patchwork-Id: 9558179 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 0341D60413 for ; Mon, 6 Feb 2017 15:00:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E4B2F27FA7 for ; Mon, 6 Feb 2017 15:00:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D980127FB1; Mon, 6 Feb 2017 15:00:46 +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 798B127FA7 for ; Mon, 6 Feb 2017 15:00:46 +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 1cakkg-0008DP-Mb; Mon, 06 Feb 2017 14:58:46 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cakkf-0008Ci-CT for xen-devel@lists.xen.org; Mon, 06 Feb 2017 14:58:45 +0000 Received: from [85.158.139.211] by server-9.bemta-5.messagelabs.com id 98/FD-01948-42F88985; Mon, 06 Feb 2017 14:58:44 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpmkeJIrShJLcpLzFFi42JxWrrBXlelf0a Ewc8VbBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa0bjFaeCDtWKI5suMjYwPpfrYuTkkBDwl/ja 9YgZxGYT0JPYOPsVE4gtIiArsbprDnsXIxcHs8ARRompq44AORwcwgJWEjMXCYCYLAIqEp/Oq IGU8wrYSlya+oYdYqScxKVtX8BGcgrYSSxsWA42Ugio5s2Un4wQtqrE6xe7WCB6BSVOznwCZj MLSEgcfPGCeQIj7ywkqVlIUgsYmVYxahSnFpWlFukaWeglFWWmZ5TkJmbm6BoamOrlphYXJ6a n5iQmFesl5+duYgQGTj0DA+MOxr5VfocYJTmYlER5DT2mRgjxJeWnVGYkFmfEF5XmpBYfYpTh 4FCS4DXsmxEhJFiUmp5akZaZAwxhmLQEB4+SCK82SJq3uCAxtzgzHSJ1ilFRSpxXDCQhAJLIK M2Da4PFzSVGWSlhXkYGBgYhnoLUotzMElT5V4ziHIxKwrwBIFN4MvNK4Ka/AlrMBLR425VpII tLEhFSUg2M/EvtXZRj7medOH6uvnHh69mN7p3fLq1S+X1sf65EmuOnZw2B/3bbeL2tcql+Yd3 7oDlfvzXO+3b/YofSt+/NOZ4Xyk2o592UcHD24oDJFya/8y9cqOV96er2J9Z1VXVXo3Yf+bPs qqpQSXT0a9bg8PTFqk7T7BN1bpycEls0v73X5cm7BcIeSizFGYmGWsxFxYkAB3Yj6ZYCAAA= X-Env-Sender: prvs=203acdd53=sergey.dyasli@citrix.com X-Msg-Ref: server-14.tower-206.messagelabs.com!1486393118!44667478!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.1.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 22141 invoked from network); 6 Feb 2017 14:58:43 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-14.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 6 Feb 2017 14:58:43 -0000 X-IronPort-AV: E=Sophos;i="5.33,342,1477958400"; d="scan'208";a="413566037" From: Sergey Dyasli To: Date: Mon, 6 Feb 2017 14:57:47 +0000 Message-ID: <20170206145747.13885-5-sergey.dyasli@citrix.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170206145747.13885-1-sergey.dyasli@citrix.com> References: <20170206145747.13885-1-sergey.dyasli@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Kevin Tian , Jan Beulich , Jun Nakajima , Sergey Dyasli Subject: [Xen-devel] [PATCH v2 4/4] x86/vvmx: correctly emulate VMREAD 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 There is an issue with the original __vmread() in nested vmx mode: emulation of a guest's VMREAD with invalid arguments leads to BUG(). Fix this by using vmread_safe() and reporting any kind of VMfail back to the guest. A new safe versions of get_vvmcs() macro and related functions are introduced because of new function signatures and lots of existing users. Signed-off-by: Sergey Dyasli --- xen/arch/x86/hvm/vmx/vmcs.c | 9 +++++---- xen/arch/x86/hvm/vmx/vvmx.c | 26 +++++++++++++++++++++++--- xen/include/asm-x86/hvm/vmx/vmcs.h | 3 ++- xen/include/asm-x86/hvm/vmx/vvmx.h | 7 +++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 1d83a69..e04b002 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -932,15 +932,16 @@ void virtual_vmcs_exit(const struct vcpu *v) __vmptrld(cur); } -u64 virtual_vmcs_vmread(const struct vcpu *v, u32 vmcs_encoding) +unsigned long virtual_vmcs_vmread(const struct vcpu *v, u32 vmcs_encoding, + u64 *val) { - u64 res; + unsigned long ret; virtual_vmcs_enter(v); - __vmread(vmcs_encoding, &res); + ret = vmread_safe(vmcs_encoding, val); virtual_vmcs_exit(v); - return res; + return ret; } unsigned long virtual_vmcs_vmwrite(const struct vcpu *v, u32 vmcs_encoding, diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 1a3d1d2..6fda44a 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -261,7 +261,23 @@ u64 get_vvmcs_virtual(void *vvmcs, u32 vmcs_encoding) u64 get_vvmcs_real(const struct vcpu *v, u32 encoding) { - return virtual_vmcs_vmread(v, encoding); + u64 val; + + virtual_vmcs_vmread(v, encoding, &val); + + return val; +} + +unsigned long get_vvmcs_virtual_safe(void *vvmcs, u32 encoding, u64 *val) +{ + *val = get_vvmcs_virtual(vvmcs, encoding); + + return 0; +} + +unsigned long get_vvmcs_real_safe(const struct vcpu *v, u32 encoding, u64 *val) +{ + return virtual_vmcs_vmread(v, encoding, val); } unsigned long set_vvmcs_virtual(void *vvmcs, u32 vmcs_encoding, u64 val) @@ -1710,13 +1726,17 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs) struct vmx_inst_decoded decode; pagefault_info_t pfinfo; u64 value = 0; - int rc; + unsigned long rc; rc = decode_vmx_inst(regs, &decode, NULL, 0); if ( rc != X86EMUL_OKAY ) return rc; - value = get_vvmcs(v, reg_read(regs, decode.reg2)); + if ( (rc = get_vvmcs_safe(v, reg_read(regs, decode.reg2), &value)) ) + { + vmfail(regs, rc); + return X86EMUL_OKAY; + } switch ( decode.type ) { case VMX_INST_MEMREG_TYPE_MEMORY: diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/vmx/vmcs.h index 72dc5fc..4f16250 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -540,7 +540,8 @@ void vmx_clear_eoi_exit_bitmap(struct vcpu *v, u8 vector); int vmx_check_msr_bitmap(unsigned long *msr_bitmap, u32 msr, int access_type); void virtual_vmcs_enter(const struct vcpu *); void virtual_vmcs_exit(const struct vcpu *); -u64 virtual_vmcs_vmread(const struct vcpu *, u32 encoding); +unsigned long virtual_vmcs_vmread(const struct vcpu *v, u32 vmcs_encoding, + u64 *val); unsigned long virtual_vmcs_vmwrite(const struct vcpu *, u32 encoding, u64 val); static inline int vmx_add_guest_msr(u32 msr) diff --git a/xen/include/asm-x86/hvm/vmx/vvmx.h b/xen/include/asm-x86/hvm/vmx/vvmx.h index d60e0bb..28e2503 100644 --- a/xen/include/asm-x86/hvm/vmx/vvmx.h +++ b/xen/include/asm-x86/hvm/vmx/vvmx.h @@ -181,6 +181,8 @@ enum vvmcs_encoding_type { u64 get_vvmcs_virtual(void *vvmcs, u32 encoding); u64 get_vvmcs_real(const struct vcpu *, u32 encoding); +unsigned long get_vvmcs_virtual_safe(void *vvmcs, u32 encoding, u64 *val); +unsigned long get_vvmcs_real_safe(const struct vcpu *, u32 encoding, u64 *val); unsigned long set_vvmcs_virtual(void *vvmcs, u32 encoding, u64 val); unsigned long set_vvmcs_real(const struct vcpu *, u32 encoding, u64 val); @@ -194,6 +196,11 @@ unsigned long set_vvmcs_real(const struct vcpu *, u32 encoding, u64 val); set_vvmcs_real(vcpu, encoding, val) : \ set_vvmcs_virtual(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) +#define get_vvmcs_safe(vcpu, encoding, val) \ + (cpu_has_vmx_vmcs_shadowing ? \ + get_vvmcs_real_safe(vcpu, encoding, val) : \ + get_vvmcs_virtual_safe(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) + uint64_t get_shadow_eptp(struct vcpu *v); void nvmx_destroy_vmcs(struct vcpu *v);