From patchwork Mon Feb 13 14:21:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Dyasli X-Patchwork-Id: 9569829 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 EAE0D60578 for ; Mon, 13 Feb 2017 14:23:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D9F562521E for ; Mon, 13 Feb 2017 14:23:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CE6F126E76; Mon, 13 Feb 2017 14:23:30 +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 49FC526E56 for ; Mon, 13 Feb 2017 14:23:30 +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 1cdHVM-00078G-Jh; Mon, 13 Feb 2017 14:21:24 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cdHVL-00077T-Ad for xen-devel@lists.xen.org; Mon, 13 Feb 2017 14:21:23 +0000 Received: from [193.109.254.147] by server-3.bemta-6.messagelabs.com id 57/BE-22349-2E0C1A85; Mon, 13 Feb 2017 14:21:22 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42JxWrohUvfRgYU RBq+PCFgs+biYxYHR4+ju30wBjFGsmXlJ+RUJrBk9/26zFkzRqFjS/ZKpgXGdQhcjJ4eEgL/E vXfLGEFsNgE9iY2zXzGB2CICshKru+awdzFycTALHGGUmLrqCDtIQljAWmLnwodgDSwCqhItO 1+wgti8ArYSdw9dYoUYKidxadsX5i5GDg5OATuJw5MSQcJCQCXzbt1mhbBVJV6/2MUC0SoocX LmEzCbWUBC4uCLF8wTGHlnIUnNQpJawMi0ilGjOLWoLLVI19hAL6koMz2jJDcxM0fX0MBMLze 1uDgxPTUnMalYLzk/dxMjMHgYgGAH49+1gYcYJTmYlER5YzctjBDiS8pPqcxILM6ILyrNSS0+ xCjDwaEkwXt0P1BOsCg1PbUiLTMHGMYwaQkOHiUR3nSQNG9xQWJucWY6ROoUo6KUOG8OSEIAJ JFRmgfXBoudS4yyUsK8jECHCPEUpBblZpagyr9iFOdgVBLmfQQyhSczrwRu+iugxUxAi1njwB aXJCKkpBoYY27EzlrN85Nf2vp3cEjEi6YfrtvfTPp6/NTlRc6h3NJzH14oOy3cKXFy+9aPXPz KR5wWdyaZenTLVM9ezj9pisXKLcJPVq8I/vH2ZeSELdNNvmtdnSq7M+rejKC0Kv3g/qz57ySK Tx+5uO9SuuyXicWVu8+tv75yQ+up4v+O017pF+saZBnuvafEUpyRaKjFXFScCABnplVBmAIAA A== X-Env-Sender: prvs=210fafdd7=sergey.dyasli@citrix.com X-Msg-Ref: server-16.tower-27.messagelabs.com!1486995679!86482422!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 49830 invoked from network); 13 Feb 2017 14:21:21 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-16.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 13 Feb 2017 14:21:21 -0000 X-IronPort-AV: E=Sophos;i="5.35,156,1484006400"; d="scan'208";a="406558636" From: Sergey Dyasli To: Date: Mon, 13 Feb 2017 14:21:09 +0000 Message-ID: <20170213142110.26467-3-sergey.dyasli@citrix.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170213142110.26467-1-sergey.dyasli@citrix.com> References: <20170213142110.26467-1-sergey.dyasli@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Kevin Tian , Jan Beulich , Jun Nakajima , Sergey Dyasli Subject: [Xen-devel] [PATCH v4 2/3] x86/vvmx: correctly emulate VMWRITE 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 __vmwrite() in nested vmx mode: emulation of a guest's VMWRITE with invalid arguments leads to BUG(). Fix this by using vmwrite_safe() and reporting any kind of VMfail back to the guest. A new safe versions of set_vvmcs() macro and related functions are introduced because of new function signatures and lots of existing users. Signed-off-by: Sergey Dyasli Reviewed-by: Jan Beulich Acked-by: Kevin Tian --- v3 --> v4: * VMX_INSN_SUCCEED is used instead of 0 * added TODO comment into set_vvmcs_virtual_safe() based on Andrew's email xen/arch/x86/hvm/vmx/vmcs.c | 12 ++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 27 ++++++++++++++++++++++++++- xen/include/asm-x86/hvm/vmx/vmcs.h | 2 ++ xen/include/asm-x86/hvm/vmx/vvmx.h | 8 ++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 0b77dbc..c4ba45e 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -950,6 +950,18 @@ void virtual_vmcs_vmwrite(const struct vcpu *v, u32 vmcs_encoding, u64 val) virtual_vmcs_exit(v); } +enum vmx_insn_errno virtual_vmcs_vmwrite_safe(const struct vcpu *v, + u32 vmcs_encoding, u64 val) +{ + enum vmx_insn_errno ret; + + virtual_vmcs_enter(v); + ret = vmwrite_safe(vmcs_encoding, val); + virtual_vmcs_exit(v); + + return ret; +} + /* * This function is only called in a vCPU's initialization phase, * so we can update the posted-interrupt descriptor in non-atomic way. diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 7172621..c5d7262 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -305,6 +305,25 @@ void set_vvmcs_real(const struct vcpu *v, u32 encoding, u64 val) virtual_vmcs_vmwrite(v, encoding, val); } +enum vmx_insn_errno set_vvmcs_virtual_safe(void *vvmcs, u32 encoding, u64 val) +{ + set_vvmcs_virtual(vvmcs, encoding, val); + + /* + * TODO: This should not always succeed. Fields and values need to be + * audited against the features offered to the guest in the VT-x MSRs. + * This should be fixed when the MSR levelling work is started, at which + * point there will be a cpuid_policy-like object. + */ + return VMX_INSN_SUCCEED; +} + +enum vmx_insn_errno set_vvmcs_real_safe(const struct vcpu *v, u32 encoding, + u64 val) +{ + return virtual_vmcs_vmwrite_safe(v, encoding, val); +} + static unsigned long reg_read(struct cpu_user_regs *regs, enum vmx_regs_enc index) { @@ -1740,13 +1759,19 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs) unsigned long operand; u64 vmcs_encoding; bool_t okay = 1; + enum vmx_insn_errno err; if ( decode_vmx_inst(regs, &decode, &operand, 0) != X86EMUL_OKAY ) return X86EMUL_EXCEPTION; vmcs_encoding = reg_read(regs, decode.reg2); - set_vvmcs(v, vmcs_encoding, operand); + err = set_vvmcs_safe(v, vmcs_encoding, operand); + if ( err != VMX_INSN_SUCCEED ) + { + vmfail(regs, err); + return X86EMUL_OKAY; + } switch ( vmcs_encoding & ~VMCS_HIGH(0) ) { diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/vmx/vmcs.h index 005e7e1..1212609 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -539,6 +539,8 @@ void virtual_vmcs_enter(const struct vcpu *); void virtual_vmcs_exit(const struct vcpu *); u64 virtual_vmcs_vmread(const struct vcpu *, u32 encoding); void virtual_vmcs_vmwrite(const struct vcpu *, u32 encoding, u64 val); +enum vmx_insn_errno virtual_vmcs_vmwrite_safe(const struct vcpu *v, + u32 vmcs_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 242e524..e49a000 100644 --- a/xen/include/asm-x86/hvm/vmx/vvmx.h +++ b/xen/include/asm-x86/hvm/vmx/vvmx.h @@ -183,6 +183,9 @@ u64 get_vvmcs_virtual(void *vvmcs, u32 encoding); u64 get_vvmcs_real(const struct vcpu *, u32 encoding); void set_vvmcs_virtual(void *vvmcs, u32 encoding, u64 val); void set_vvmcs_real(const struct vcpu *, u32 encoding, u64 val); +enum vmx_insn_errno set_vvmcs_virtual_safe(void *vvmcs, u32 encoding, u64 val); +enum vmx_insn_errno set_vvmcs_real_safe(const struct vcpu *, u32 encoding, + u64 val); #define get_vvmcs(vcpu, encoding) \ (cpu_has_vmx_vmcs_shadowing ? \ @@ -194,6 +197,11 @@ void 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 set_vvmcs_safe(vcpu, encoding, val) \ + (cpu_has_vmx_vmcs_shadowing ? \ + set_vvmcs_real_safe(vcpu, encoding, val) : \ + set_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);