From patchwork Mon Jun 26 10:44:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Dyasli X-Patchwork-Id: 9809115 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 E3A5960209 for ; Mon, 26 Jun 2017 10:47:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E7DC226E74 for ; Mon, 26 Jun 2017 10:47:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC0F3283F2; Mon, 26 Jun 2017 10:47:23 +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 21D5226E74 for ; Mon, 26 Jun 2017 10:47:23 +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 1dPRVb-00029y-KZ; Mon, 26 Jun 2017 10:44:43 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dPRVa-00028V-70 for xen-devel@lists.xen.org; Mon, 26 Jun 2017 10:44:42 +0000 Received: from [85.158.143.35] by server-6.bemta-6.messagelabs.com id C2/B3-03920-995E0595; Mon, 26 Jun 2017 10:44:41 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrOIsWRWlGSWpSXmKPExsXitHSDve7MpwG RBlNaZSyWfFzM4sDocXT3b6YAxijWzLyk/IoE1owrf34wFWzJrGjbepK5gfGlbxcjB4eEgL/E jokRXYycHGwCehIbZ79iArFFBGQlVnfNYe9i5OJgFjjCKDF11RF2kISwgIXE5TXfWUBsFgFVi WeTDjOCzOEVsJXYvRmsREJAXmJX20VWEJtTwE5i7oLTYOVCQCVNTxexgdi8AoISJ2c+AYszC2 hKtG7/zQ5hy0s0b53NDFGvKvH6xS6WCYx8s5C0zELSMgtJywJG5lWM6sWpRWWpRboWeklFmek ZJbmJmTm6hgZmermpxcWJ6ak5iUnFesn5uZsYgYHGAAQ7GGdf9j/EKMnBpCTKy/EkIFKILyk/ pTIjsTgjvqg0J7X4EKMGB4fAlXNzpzNJseTl56UqSfBqgtQJFqWmp1akZeYAYwGmVIKDR0mE9 +hDoDRvcUFibnFmOkTqFKOilDivC0ifAEgiozQPrg0Wf5cYZaWEeRmBjhLiKUgtys0sQZV/xS jOwagkzJsLMoUnM68EbvoroMVMQItZ5oEtLklESEk1MNrpMbNO/d2h+VJ4+myf+Unzn11+xT7 DP/ancZhrhINofrNbXWh8fbJZeewWI4ES/d9udoKr173f86j+b7v2Sj/pTrtFnfuTllxT3ry1 50zt9fkFoS+W7/0s2/6YjSvcOujFhFqJLnMNu2XH5kzeNT86TJXxWucfo/bov2GGc2TdHsYt3 1qxRYmlOCPRUIu5qDgRANR4pU26AgAA X-Env-Sender: prvs=343936acc=sergey.dyasli@citrix.com X-Msg-Ref: server-11.tower-21.messagelabs.com!1498473878!75551093!3 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.19; banners=-,-,- X-VirusChecked: Checked Received: (qmail 60252 invoked from network); 26 Jun 2017 10:44:41 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-11.tower-21.messagelabs.com with RC4-SHA encrypted SMTP; 26 Jun 2017 10:44:41 -0000 X-IronPort-AV: E=Sophos;i="5.39,395,1493683200"; d="scan'208";a="437812351" From: Sergey Dyasli To: Date: Mon, 26 Jun 2017 11:44:33 +0100 Message-ID: <20170626104435.25508-5-sergey.dyasli@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170626104435.25508-1-sergey.dyasli@citrix.com> References: <20170626104435.25508-1-sergey.dyasli@citrix.com> MIME-Version: 1.0 Cc: Andrew Cooper , Kevin Tian , Jan Beulich , Jun Nakajima , Sergey Dyasli Subject: [Xen-devel] [PATCH v1 4/6] vvmx: add hvm_max_vmx_msr_policy 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 Currently, when nested virt is enabled, the set of L1 VMX features is fixed and calculated by nvmx_msr_read_intercept() as an intersection between the full set of Xen's supported L1 VMX features, the set of actual H/W features and, for MSR_IA32_VMX_EPT_VPID_CAP, the set of features that Xen uses. Add hvm_max_vmx_msr_policy object which represents the end result of nvmx_msr_read_intercept() on current H/W. Most of the code is moved from nvmx_msr_read_intercept() to calculate_hvm_max_policy() which is called only once during the startup. There is no functional change to what L1 sees in VMX MSRs. Signed-off-by: Sergey Dyasli --- xen/arch/x86/hvm/vmx/vmcs.c | 3 + xen/arch/x86/hvm/vmx/vvmx.c | 297 +++++++++++++++++++++----------------------- 2 files changed, 147 insertions(+), 153 deletions(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index dbf6eb7433..da6ddf52f1 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -244,6 +244,8 @@ static u32 adjust_vmx_controls( return ctl; } +void calculate_hvm_max_policy(void); + static int vmx_init_vmcs_config(void) { u32 min, opt; @@ -463,6 +465,7 @@ static int vmx_init_vmcs_config(void) vmx_virt_exception = !!(_vmx_secondary_exec_control & SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS); vmx_display_features(); + calculate_hvm_max_policy(); /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ if ( raw_vmx_msr_policy.basic.vmcs_region_size > PAGE_SIZE ) diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 3560faec6d..657371ec69 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -1941,6 +1941,8 @@ int nvmx_handle_invvpid(struct cpu_user_regs *regs) return X86EMUL_OKAY; } +struct vmx_msr_policy __read_mostly hvm_max_vmx_msr_policy; + #define __emul_value(enable1, default1) \ ((enable1 | default1) << 32 | (default1)) @@ -1948,6 +1950,134 @@ int nvmx_handle_invvpid(struct cpu_user_regs *regs) (((__emul_value(enable1, default1) & host_value) & (~0ul << 32)) | \ ((uint32_t)(__emul_value(enable1, default1) | host_value))) +void __init calculate_hvm_max_policy(void) +{ + struct vmx_msr_policy *p = &hvm_max_vmx_msr_policy; + uint64_t data, *msr; + u32 default1_bits; + + *p = raw_vmx_msr_policy; + + /* XXX: vmcs_revision_id for nested virt */ + + /* Pinbased controls 1-settings */ + data = PIN_BASED_EXT_INTR_MASK | + PIN_BASED_NMI_EXITING | + PIN_BASED_PREEMPT_TIMER; + + msr = &p->msr[MSR_IA32_VMX_PINBASED_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_PINBASED_CTLS_DEFAULT1, *msr); + msr = &p->msr[MSR_IA32_VMX_TRUE_PINBASED_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_PINBASED_CTLS_DEFAULT1, *msr); + + /* Procbased controls 1-settings */ + default1_bits = VMX_PROCBASED_CTLS_DEFAULT1; + data = CPU_BASED_HLT_EXITING | + CPU_BASED_VIRTUAL_INTR_PENDING | + CPU_BASED_CR8_LOAD_EXITING | + CPU_BASED_CR8_STORE_EXITING | + CPU_BASED_INVLPG_EXITING | + CPU_BASED_CR3_LOAD_EXITING | + CPU_BASED_CR3_STORE_EXITING | + CPU_BASED_MONITOR_EXITING | + CPU_BASED_MWAIT_EXITING | + CPU_BASED_MOV_DR_EXITING | + CPU_BASED_ACTIVATE_IO_BITMAP | + CPU_BASED_USE_TSC_OFFSETING | + CPU_BASED_UNCOND_IO_EXITING | + CPU_BASED_RDTSC_EXITING | + CPU_BASED_MONITOR_TRAP_FLAG | + CPU_BASED_VIRTUAL_NMI_PENDING | + CPU_BASED_ACTIVATE_MSR_BITMAP | + CPU_BASED_PAUSE_EXITING | + CPU_BASED_RDPMC_EXITING | + CPU_BASED_TPR_SHADOW | + CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; + + msr = &p->msr[MSR_IA32_VMX_PROCBASED_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, default1_bits, *msr); + + default1_bits &= ~(CPU_BASED_CR3_LOAD_EXITING | + CPU_BASED_CR3_STORE_EXITING | + CPU_BASED_INVLPG_EXITING); + + msr = &p->msr[MSR_IA32_VMX_TRUE_PROCBASED_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, default1_bits, *msr); + + /* Procbased-2 controls 1-settings */ + data = SECONDARY_EXEC_DESCRIPTOR_TABLE_EXITING | + SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | + SECONDARY_EXEC_ENABLE_VPID | + SECONDARY_EXEC_UNRESTRICTED_GUEST | + SECONDARY_EXEC_ENABLE_EPT; + msr = &p->msr[MSR_IA32_VMX_PROCBASED_CTLS2 - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, 0, *msr); + + /* Vmexit controls 1-settings */ + data = VM_EXIT_ACK_INTR_ON_EXIT | + VM_EXIT_IA32E_MODE | + VM_EXIT_SAVE_PREEMPT_TIMER | + VM_EXIT_SAVE_GUEST_PAT | + VM_EXIT_LOAD_HOST_PAT | + VM_EXIT_SAVE_GUEST_EFER | + VM_EXIT_LOAD_HOST_EFER | + VM_EXIT_LOAD_PERF_GLOBAL_CTRL; + msr = &p->msr[MSR_IA32_VMX_EXIT_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_EXIT_CTLS_DEFAULT1, *msr); + msr = &p->msr[MSR_IA32_VMX_TRUE_EXIT_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_EXIT_CTLS_DEFAULT1, *msr); + + /* Vmentry controls 1-settings */ + data = VM_ENTRY_LOAD_GUEST_PAT | + VM_ENTRY_LOAD_GUEST_EFER | + VM_ENTRY_LOAD_PERF_GLOBAL_CTRL | + VM_ENTRY_IA32E_MODE; + msr = &p->msr[MSR_IA32_VMX_ENTRY_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_ENTRY_CTLS_DEFAULT1, *msr); + msr = &p->msr[MSR_IA32_VMX_TRUE_ENTRY_CTLS - MSR_IA32_VMX_BASIC]; + *msr = gen_vmx_msr(data, VMX_ENTRY_CTLS_DEFAULT1, *msr); + + /* MSR_IA32_VMX_VMCS_ENUM */ + /* The max index of VVMCS encoding is 0x1f. */ + data = 0x1f << 1; + msr = &p->msr[MSR_IA32_VMX_VMCS_ENUM - MSR_IA32_VMX_BASIC]; + *msr = data; + + /* MSR_IA32_VMX_CR0_FIXED0 */ + /* PG, PE bits must be 1 in VMX operation */ + data = X86_CR0_PE | X86_CR0_PG; + msr = &p->msr[MSR_IA32_VMX_CR0_FIXED0 - MSR_IA32_VMX_BASIC]; + *msr = data; + + /* MSR_IA32_VMX_CR0_FIXED1 */ + /* allow 0-settings for all bits */ + data = 0xffffffff; + msr = &p->msr[MSR_IA32_VMX_CR0_FIXED1 - MSR_IA32_VMX_BASIC]; + *msr = data; + + /* MSR_IA32_VMX_CR4_FIXED0 */ + /* VMXE bit must be 1 in VMX operation */ + data = X86_CR4_VMXE; + msr = &p->msr[MSR_IA32_VMX_CR4_FIXED0 - MSR_IA32_VMX_BASIC]; + *msr = data; + + /* MSR_IA32_VMX_CR4_FIXED1 */ + /* Treated dynamically */ + + /* MSR_IA32_VMX_MISC */ + /* Do not support CR3-target feature now */ + msr = &p->msr[MSR_IA32_VMX_MISC - MSR_IA32_VMX_BASIC]; + *msr = *msr & ~VMX_MISC_CR3_TARGET; + + /* MSR_IA32_VMX_EPT_VPID_CAP */ + data = nept_get_ept_vpid_cap(); + msr = &p->msr[MSR_IA32_VMX_EPT_VPID_CAP - MSR_IA32_VMX_BASIC]; + *msr = data; + + /* MSR_IA32_VMX_VMFUNC is N/A */ + p->available &= ~0x20000; +} + /* * Capability reporting */ @@ -1955,171 +2085,32 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content) { struct vcpu *v = current; struct domain *d = v->domain; - u64 data = 0, host_data = 0; + struct vmx_msr_policy *p = &hvm_max_vmx_msr_policy; + u64 data; int r = 1; /* VMX capablity MSRs are available only when guest supports VMX. */ if ( !nestedhvm_enabled(d) || !d->arch.cpuid->basic.vmx ) return 0; - /* - * These MSRs are only available when flags in other MSRs are set. - * These prerequisites are listed in the Intel 64 and IA-32 - * Architectures Software Developer’s Manual, Vol 3, Appendix A. - */ - switch ( msr ) + /* TODO: disentangle feature control from nested virt */ + if ( msr == MSR_IA32_FEATURE_CONTROL ) { - case MSR_IA32_VMX_PROCBASED_CTLS2: - if ( !cpu_has_vmx_secondary_exec_control ) - return 0; - break; - - case MSR_IA32_VMX_EPT_VPID_CAP: - if ( !(cpu_has_vmx_ept || cpu_has_vmx_vpid) ) - return 0; - break; - - case MSR_IA32_VMX_TRUE_PINBASED_CTLS: - case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: - case MSR_IA32_VMX_TRUE_EXIT_CTLS: - case MSR_IA32_VMX_TRUE_ENTRY_CTLS: - if ( !(vmx_basic_msr & VMX_BASIC_DEFAULT1_ZERO) ) - return 0; - break; + data = IA32_FEATURE_CONTROL_LOCK | + IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX; + *msr_content = data; - case MSR_IA32_VMX_VMFUNC: - if ( !cpu_has_vmx_vmfunc ) - return 0; - break; + return r; } - rdmsrl(msr, host_data); - - /* - * Remove unsupport features from n1 guest capability MSR - */ - switch (msr) { - case MSR_IA32_VMX_BASIC: - { - const struct vmcs_struct *vmcs = - map_domain_page(_mfn(PFN_DOWN(v->arch.hvm_vmx.vmcs_pa))); - - data = (host_data & (~0ul << 32)) | - (vmcs->vmcs_revision_id & 0x7fffffff); - unmap_domain_page(vmcs); - break; - } - case MSR_IA32_VMX_PINBASED_CTLS: - case MSR_IA32_VMX_TRUE_PINBASED_CTLS: - /* 1-settings */ - data = PIN_BASED_EXT_INTR_MASK | - PIN_BASED_NMI_EXITING | - PIN_BASED_PREEMPT_TIMER; - data = gen_vmx_msr(data, VMX_PINBASED_CTLS_DEFAULT1, host_data); - break; - case MSR_IA32_VMX_PROCBASED_CTLS: - case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: - { - u32 default1_bits = VMX_PROCBASED_CTLS_DEFAULT1; - /* 1-settings */ - data = CPU_BASED_HLT_EXITING | - CPU_BASED_VIRTUAL_INTR_PENDING | - CPU_BASED_CR8_LOAD_EXITING | - CPU_BASED_CR8_STORE_EXITING | - CPU_BASED_INVLPG_EXITING | - CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING | - CPU_BASED_MONITOR_EXITING | - CPU_BASED_MWAIT_EXITING | - CPU_BASED_MOV_DR_EXITING | - CPU_BASED_ACTIVATE_IO_BITMAP | - CPU_BASED_USE_TSC_OFFSETING | - CPU_BASED_UNCOND_IO_EXITING | - CPU_BASED_RDTSC_EXITING | - CPU_BASED_MONITOR_TRAP_FLAG | - CPU_BASED_VIRTUAL_NMI_PENDING | - CPU_BASED_ACTIVATE_MSR_BITMAP | - CPU_BASED_PAUSE_EXITING | - CPU_BASED_RDPMC_EXITING | - CPU_BASED_TPR_SHADOW | - CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; - - if ( msr == MSR_IA32_VMX_TRUE_PROCBASED_CTLS ) - default1_bits &= ~(CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING | - CPU_BASED_INVLPG_EXITING); - - data = gen_vmx_msr(data, default1_bits, host_data); - break; - } - case MSR_IA32_VMX_PROCBASED_CTLS2: - /* 1-settings */ - data = SECONDARY_EXEC_DESCRIPTOR_TABLE_EXITING | - SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | - SECONDARY_EXEC_ENABLE_VPID | - SECONDARY_EXEC_UNRESTRICTED_GUEST | - SECONDARY_EXEC_ENABLE_EPT; - data = gen_vmx_msr(data, 0, host_data); - break; - case MSR_IA32_VMX_EXIT_CTLS: - case MSR_IA32_VMX_TRUE_EXIT_CTLS: - /* 1-settings */ - data = VM_EXIT_ACK_INTR_ON_EXIT | - VM_EXIT_IA32E_MODE | - VM_EXIT_SAVE_PREEMPT_TIMER | - VM_EXIT_SAVE_GUEST_PAT | - VM_EXIT_LOAD_HOST_PAT | - VM_EXIT_SAVE_GUEST_EFER | - VM_EXIT_LOAD_HOST_EFER | - VM_EXIT_LOAD_PERF_GLOBAL_CTRL; - data = gen_vmx_msr(data, VMX_EXIT_CTLS_DEFAULT1, host_data); - break; - case MSR_IA32_VMX_ENTRY_CTLS: - case MSR_IA32_VMX_TRUE_ENTRY_CTLS: - /* 1-settings */ - data = VM_ENTRY_LOAD_GUEST_PAT | - VM_ENTRY_LOAD_GUEST_EFER | - VM_ENTRY_LOAD_PERF_GLOBAL_CTRL | - VM_ENTRY_IA32E_MODE; - data = gen_vmx_msr(data, VMX_ENTRY_CTLS_DEFAULT1, host_data); - break; + if ( !vmx_msr_available(p, msr) ) + return 0; - case MSR_IA32_FEATURE_CONTROL: - data = IA32_FEATURE_CONTROL_LOCK | - IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX; - break; - case MSR_IA32_VMX_VMCS_ENUM: - /* The max index of VVMCS encoding is 0x1f. */ - data = 0x1f << 1; - break; - case MSR_IA32_VMX_CR0_FIXED0: - /* PG, PE bits must be 1 in VMX operation */ - data = X86_CR0_PE | X86_CR0_PG; - break; - case MSR_IA32_VMX_CR0_FIXED1: - /* allow 0-settings for all bits */ - data = 0xffffffff; - break; - case MSR_IA32_VMX_CR4_FIXED0: - /* VMXE bit must be 1 in VMX operation */ - data = X86_CR4_VMXE; - break; - case MSR_IA32_VMX_CR4_FIXED1: - data = hvm_cr4_guest_valid_bits(v, 0); - break; - case MSR_IA32_VMX_MISC: - /* Do not support CR3-target feature now */ - data = host_data & ~VMX_MISC_CR3_TARGET; - break; - case MSR_IA32_VMX_EPT_VPID_CAP: - data = nept_get_ept_vpid_cap(); - break; - default: - r = 0; - break; - } + if ( msr == MSR_IA32_VMX_CR4_FIXED1 ) + *msr_content = hvm_cr4_guest_valid_bits(v, 0); + else + *msr_content = p->msr[msr - MSR_IA32_VMX_BASIC]; - *msr_content = data; return r; }