Add support for XSAVES on AMD and unify it with Intel
v2 cover Add support for XSAVES on AMD and unify it with Intel
Unify AMD's and Intel's approach for supporting XSAVES. To do this change Intel's approach from using the MSR-load areas to writing the guest/host values to IA32_XSS on a VM-enter/VM-exit. Switching to this strategy allows for a common approach between both AMD and Intel. Additionally, define svm_xsaves_supported() based on AMD's feedback, and add IA32_XSS to the emulated_msrs list, in preparation for the day when the guest IA32_XSS may contain a non-zero value.
This change sets up IA32_XSS to be a non-zero value in the future, which may happen sooner than later with support for guest CET feature being added.
v1 -> v2:
- Add the flag xsaves_enabled to kvm_vcpu_arch to track when XSAVES is enabled in the guest, whether or not XSAVES is enumerated in the guest CPUID.
- Remove code that sets the X86_FEATURE_XSAVES bit in the guest CPUID which was added in patch "Enumerate XSAVES in guest CPUID when it is available to the guest". As a result we no longer need that patch.
- Added a comment to kvm_set_msr_common to describe how to save/restore PT MSRS without using XSAVES/XRSTORS.
- Added more comments to the "Add support for XSAVES on AMD" patch.
- Replaced vcpu_set_msr_expect_result() with _vcpu_set_msr() in the test library.
Aaron Lewis (5):
KVM: VMX: Remove unneeded check for X86_FEATURE_XSAVE
KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
kvm: svm: Add support for XSAVES on AMD
kvm: x86: Add IA32_XSS to the emulated_msrs list
kvm: tests: Add test to verify MSR_IA32_XSS
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 19 +++-- arch/x86/kvm/vmx/vmx.c | 46 ++++-------- arch/x86/kvm/x86.c | 46 ++++++++++-- arch/x86/kvm/x86.h | 4 +- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 7 +- .../selftests/kvm/lib/x86_64/processor.c | 72 ++++++++++++++++--- .../selftests/kvm/x86_64/xss_msr_test.c | 70 ++++++++++++++++++ 10 files changed, 210 insertions(+), 57 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/xss_msr_test.c
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
PATCH v2 1/5 KVM: VMX: Remove unneeded check for X86_FEATURE_XSAVE
Volume 4 of the SDM says that IA32_XSS is supported "If(CPUID.(0DH, 1):EAX.[3] = 1", so only the X86_FEATURE_XSAVES check is necessary.
--- arch/x86/kvm/vmx/vmx.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -4
@@ -1823,8 +1823,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_XSS :if (!vmx_xsaves_supported () || (!msr_info->host_initiated && - !(guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && -guest_cpuid_has (vcpu,X86_FEATURE_XSAVES )))) + !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES )))return 1 ; msr_info->data = vcpu->arch.ia32_xss;break ;
@@ -2066,8 +2065,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_XSS :if (!vmx_xsaves_supported () || (!msr_info->host_initiated && - !(guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && -guest_cpuid_has (vcpu,X86_FEATURE_XSAVES )))) + !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES )))return 1 ;/* * The only supported bit as of Skylake is bit 8, but
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
PATCH v2 2/5 KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
In reply to Aaron LewisSet IA32_XSS for the guest and host during VM Enter and VM Exit transitions rather than by using the MSR-load areas.
By moving away from using the MSR-load area we can have a unified solution for both AMD and Intel.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 7 +++++-- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++------------ arch/x86/kvm/x86.c | 23 +++++++++++++++++++---- arch/x86/kvm/x86.h | 4 ++-- 5 files changed, 37 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -562,6 +562,7 @@ struct kvm_vcpu_arch {u64 smbase;u64 smi_count;bool tpr_access_reporting; +bool xsaves_enabled;u64 ia32_xss;u64 microcode_version;u64 arch_capabilities;
arch/x86/kvm/svm.c +5 -2
@@ -5628,7 +5628,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->save.cr2 = vcpu->arch.cr2;clgi (); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu);if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.timer_advance_ns)
@@ -5778,7 +5778,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu); -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu);stgi ();/* Any pending NMI will happen here */
@@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); + vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES ); +/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );
arch/x86/kvm/vmx/vmx.c +10 -12
@@ -106,8 +106,6 @@ module_param(enable_apicv, bool, S_IRUGO);static bool __read_mostly nested =1 ;module_param (nested, bool,S_IRUGO ); -static u64 __read_mostly host_xss; -bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO );
@@ -2074,11 +2072,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data !=0 )return 1 ; vcpu->arch.ia32_xss = data; -if (vcpu->arch.ia32_xss != host_xss) -add_atomic_switch_msr (vmx,MSR_IA32_XSS , - vcpu->arch.ia32_xss, host_xss, false); -else -clear_atomic_switch_msr (vmx,MSR_IA32_XSS );break ;case MSR_IA32_RTIT_CTL : if ((pt_mode != PT_MODE_HOST_GUEST) ||
@@ -4038,6 +4031,8 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) &&guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ); + vcpu->arch.xsaves_enabled = xsaves_enabled; +if (!xsaves_enabled) exec_control &= ~SECONDARY_EXEC_XSAVES ;
@@ -6540,7 +6535,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP )vmx_set_interrupt_shadow (vcpu,0 ); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu); if (static_cpu_has(X86_FEATURE_PKU) && kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
@@ -6647,7 +6642,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)__write_pkru (vmx->host_pkru); } -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu); vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ;
@@ -7091,6 +7086,12 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +/* +* This will be set back to true in vmx_compute_secondary_exec_control() +* if it should be true, so setting it to false here is okay. +*/ + vcpu->arch.xsaves_enabled = false; +if (cpu_has_secondary_exec_ctrls ()) {vmx_compute_secondary_exec_control (vmx);vmcs_set_secondary_exec_control (vmx);
@@ -7599,9 +7600,6 @@ static __init int hardware_setup(void)WARN_ONCE (host_bndcfgs,"KVM: BNDCFGS in host will be lost" ); } -if (boot_cpu_has (X86_FEATURE_XSAVES )) -rdmsrl (MSR_IA32_XSS , host_xss); -if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ())) enable_vpid =0 ;
arch/x86/kvm/x86.c +19 -4
@@ -176,6 +176,8 @@ struct kvm_shared_msrs {static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;static struct kvm_shared_msrs __percpu *shared_msrs; +static u64 __read_mostly host_xss; +struct kvm_stats_debugfs_item debugfs_entries[] = { {"pf_fixed" ,VCPU_STAT (pf_fixed) }, {"pf_guest" ,VCPU_STAT (pf_guest) },
@@ -812,27 +814,37 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) }EXPORT_SYMBOL_GPL (kvm_lmsw); -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu) {if (kvm_read_cr4_bits (vcpu,X86_CR4_OSXSAVE ) && !vcpu->guest_xcr0_loaded) {/* kvm_set_xcr() also depends on this */ if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , vcpu->arch.xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , vcpu->arch.ia32_xss); + vcpu->guest_xcr0_loaded =1 ; } } -EXPORT_SYMBOL_GPL (kvm_load_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_guest_xsave_controls); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu) {if (vcpu->guest_xcr0_loaded) {if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , host_xss); + vcpu->guest_xcr0_loaded =0 ; } } -EXPORT_SYMBOL_GPL (kvm_put_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_host_xsave_controls);static int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr) {
@@ -9293,6 +9305,9 @@ int kvm_arch_hardware_setup(void) kvm_default_tsc_scaling_ratio =1ULL << kvm_tsc_scaling_ratio_frac_bits; } +if (boot_cpu_has (X86_FEATURE_XSAVES )) +rdmsrl (MSR_IA32_XSS , host_xss); +kvm_init_msr_list ();return 0 ; }
arch/x86/kvm/x86.h +2 -2
@@ -366,7 +366,7 @@ static inline bool kvm_pat_valid(u64 data) return (data | ((data & 0x0202020202020202ull) << 1)) == data; } -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu); +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu); +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu);#endif
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
Re: [PATCH v2 2/5] KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
Quoted 51 lines · Aaron Lewis
Set
IA32_XSSfor the guest and host during VM Enter and VM Exit transitions rather than by using the MSR-load areas.By moving away from using the MSR-load area we can have a unified solution for both AMD and Intel.
Reviewed-by Jim Mattson <[email protected]>Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 7 +++++-- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++------------ arch/x86/kvm/x86.c | 23 +++++++++++++++++++---- arch/x86/kvm/x86.h | 4 ++-- 5 files changed, 37 insertions(+), 20 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -562,6 +562,7 @@ struct kvm_vcpu_arch {u64 smbase;u64 smi_count;bool tpr_access_reporting; +bool xsaves_enabled;u64 ia32_xss;u64 microcode_version;u64 arch_capabilities;arch/x86/kvm/svm.c +2
-2@@ -5628,7 +5628,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->save.cr2 = vcpu->arch.cr2;clgi (); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu);if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.timer_advance_ns)@@ -5778,7 +5778,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu); -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu);stgi ();/* Any pending NMI will happen here */
arch/x86/kvm/svm.c static void svm_cpuid_update(struct kvm_vcpu *vcpu) · line 5891
Quoted 5 lines
arch/x86/kvm/svm.c +2
-0@@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); + vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES );
This looks very much like a functional change to SVM, which feels wrong for a patch with a subject of "KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS" feels wrong. Shouldn't this be unconditionally set false in this patch, and then enabled in " kvm: svm: Add support for XSAVES on AMD"?
Quoted 18 lines
arch/x86/kvm/svm.c +1
-0+/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );arch/x86/kvm/vmx/vmx.c +0
-2@@ -106,8 +106,6 @@ module_param(enable_apicv, bool, S_IRUGO);static bool __read_mostly nested =1 ;module_param (nested, bool,S_IRUGO ); -static u64 __read_mostly host_xss; -bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO );
arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2081
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +0
-5@@ -2074,11 +2072,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data !=0 )return 1 ; vcpu->arch.ia32_xss = data; -if (vcpu->arch.ia32_xss != host_xss) -add_atomic_switch_msr (vmx,MSR_IA32_XSS , - vcpu->arch.ia32_xss, host_xss, false); -else -clear_atomic_switch_msr (vmx,MSR_IA32_XSS );
I'm pretty sure the "vmx_xsaves_supported()" check in this case statement can be removed after this patch. The host support check was necessary when the MSR load/save list was being used, but moving to xsaves_enabled means theres no risk of writing MSR_IA32_XSS when it's not supported.
arch/x86/kvm/vmx/vmx.c static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) · line 4034
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0break ;case MSR_IA32_RTIT_CTL : if ((pt_mode != PT_MODE_HOST_GUEST) ||@@ -4038,6 +4031,8 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) &&guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ); + vcpu->arch.xsaves_enabled = xsaves_enabled;
Doesn't this conflict with the direction of the previous patch, "KVM: VMX:
Remove unneeded check for X86_FEATURE_XSAVE"? xsaves_enabled is %true iff both XSAVE and XSAVES are exposed to the guest.
Alternatively, couldn't KVM check arch.xsaves_enabled in the MSR handling?
Quoted 23 lines
arch/x86/kvm/vmx/vmx.c +3
-2+if (!xsaves_enabled) exec_control &= ~SECONDARY_EXEC_XSAVES ;@@ -6540,7 +6535,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP )vmx_set_interrupt_shadow (vcpu,0 ); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu); if (static_cpu_has(X86_FEATURE_PKU) && kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&@@ -6647,7 +6642,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)__write_pkru (vmx->host_pkru); } -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu); vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ;
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7091
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -7091,6 +7086,12 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +/* +* This will be set back to true in vmx_compute_secondary_exec_control() +* if it should be true, so setting it to false here is okay.
Or simply:
/* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
Quoted 72 lines
arch/x86/kvm/vmx/vmx.c +3
-3+*/ + vcpu->arch.xsaves_enabled = false; +if (cpu_has_secondary_exec_ctrls ()) {vmx_compute_secondary_exec_control (vmx);vmcs_set_secondary_exec_control (vmx);@@ -7599,9 +7600,6 @@ static __init int hardware_setup(void)WARN_ONCE (host_bndcfgs,"KVM: BNDCFGS in host will be lost" ); } -if (boot_cpu_has (X86_FEATURE_XSAVES )) -rdmsrl (MSR_IA32_XSS , host_xss); -if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ())) enable_vpid =0 ;arch/x86/kvm/x86.c +16
-4@@ -176,6 +176,8 @@ struct kvm_shared_msrs {static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;static struct kvm_shared_msrs __percpu *shared_msrs; +static u64 __read_mostly host_xss; +struct kvm_stats_debugfs_item debugfs_entries[] = { {"pf_fixed" ,VCPU_STAT (pf_fixed) }, {"pf_guest" ,VCPU_STAT (pf_guest) },@@ -812,27 +814,37 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) }EXPORT_SYMBOL_GPL (kvm_lmsw); -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu) {if (kvm_read_cr4_bits (vcpu,X86_CR4_OSXSAVE ) && !vcpu->guest_xcr0_loaded) {/* kvm_set_xcr() also depends on this */ if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , vcpu->arch.xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , vcpu->arch.ia32_xss); + vcpu->guest_xcr0_loaded =1 ; } } -EXPORT_SYMBOL_GPL (kvm_load_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_guest_xsave_controls); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu) {if (vcpu->guest_xcr0_loaded) {if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , host_xss); + vcpu->guest_xcr0_loaded =0 ; } } -EXPORT_SYMBOL_GPL (kvm_put_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_host_xsave_controls);static int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr) {
arch/x86/kvm/x86.c int kvm_arch_hardware_setup(void) · line 9309
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -9293,6 +9305,9 @@ int kvm_arch_hardware_setup(void) kvm_default_tsc_scaling_ratio =1ULL << kvm_tsc_scaling_ratio_frac_bits; } +if (boot_cpu_has (X86_FEATURE_XSAVES )) +rdmsrl (MSR_IA32_XSS , host_xss);
More code that goes beyond the subject of the patch, as host_xss is now being read and potentially switched on SVM platforms. I don't mind the change itself being part of this patch (unlike setting xsaves_enabled), but IMO the subject should make it more clear that this is a common x86 change. Arguably this should be two patches, one to do what this subject says and a second to move the code to common x86, but that seems like overkill.
Quoted 20 lines
arch/x86/kvm/x86.c +0
-0kvm_init_msr_list ();return 0 ; }arch/x86/kvm/x86.h +2
-2@@ -366,7 +366,7 @@ static inline bool kvm_pat_valid(u64 data) return (data | ((data & 0x0202020202020202ull) << 1)) == data; } -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu); +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu); +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu);#endif Signature · 1 line
2.23.0.700.g56cf767bdb-goog
Re: [PATCH v2 2/5] KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
Quoted 56 lines · Sean Christopherson
Quoted 54 lines · Aaron Lewis
Set
IA32_XSSfor the guest and host during VM Enter and VM Exit transitions rather than by using the MSR-load areas.By moving away from using the MSR-load area we can have a unified solution for both AMD and Intel.
Reviewed-by Jim Mattson <[email protected]>Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 7 +++++-- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++------------ arch/x86/kvm/x86.c | 23 +++++++++++++++++++---- arch/x86/kvm/x86.h | 4 ++-- 5 files changed, 37 insertions(+), 20 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -562,6 +562,7 @@ struct kvm_vcpu_arch {u64 smbase;u64 smi_count;bool tpr_access_reporting; +bool xsaves_enabled;u64 ia32_xss;u64 microcode_version;u64 arch_capabilities;arch/x86/kvm/svm.c +2
-2@@ -5628,7 +5628,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->save.cr2 = vcpu->arch.cr2;clgi (); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu);if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.timer_advance_ns)@@ -5778,7 +5778,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu); -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu);stgi ();/* Any pending NMI will happen here */ @@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
Quoted 8 lines
arch/x86/kvm/svm.c static void svm_cpuid_update(struct kvm_vcpu *vcpu) · line 5891
Quoted 2 lines
arch/x86/kvm/svm.c +2
-0+ vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES );This looks very much like a functional change to SVM, which feels wrong for a patch with a subject of "KVM: VMX: Use wrmsr for switching between guest and host
IA32_XSS" feels wrong. Shouldn't this be unconditionally set false in this patch, and then enabled in " kvm: svm: Add support for XSAVES on AMD"?
Nothing is being enabled here. Vcpu->arch.xsaves_enabled simply tells us whether or not the guest can execute the XSAVES instruction. Any guest with the ability to set CR4.OSXSAVE on an AMD host that supports XSAVES can use the instruction.
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/svm.c +1
-0+/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );arch/x86/kvm/vmx/vmx.c +0
-2@@ -106,8 +106,6 @@ module_param(enable_apicv, bool, S_IRUGO);static bool __read_mostly nested =1 ;module_param (nested, bool,S_IRUGO ); -static u64 __read_mostly host_xss; -bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO );
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2081
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +0
-5@@ -2074,11 +2072,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data !=0 )return 1 ; vcpu->arch.ia32_xss = data; -if (vcpu->arch.ia32_xss != host_xss) -add_atomic_switch_msr (vmx,MSR_IA32_XSS , - vcpu->arch.ia32_xss, host_xss, false); -else -clear_atomic_switch_msr (vmx,MSR_IA32_XSS );I'm pretty sure the "
vmx_xsaves_supported()" check in this case statement can be removed after this patch. The host support check was necessary when the MSR load/save list was being used, but moving toxsaves_enabledmeans theres no risk of writingMSR_IA32_XSSwhen it's not supported.
I agree. vmx_xsaves_supported() only tells us if the XSAVES instruction can be enabled. It does not tell us anything about the existence of IA32_XSS in the guest.
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +0
-0break ;case MSR_IA32_RTIT_CTL : if ((pt_mode != PT_MODE_HOST_GUEST) ||@@ -4038,6 +4031,8 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) &&guest_cpuid_has (vcpu,X86_FEATURE_XSAVES );
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) · line 4034
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+ vcpu->arch.xsaves_enabled = xsaves_enabled;Doesn't this conflict with the direction of the previous patch, "KVM: VMX:
Remove unneeded check forX86_FEATURE_XSAVE"?xsaves_enabledis %true iff both XSAVE and XSAVES are exposed to the guest.
There's no conflict, because the predicates are different. The previous patch removed the test for XSAVE in the emulation of RDMSR/WRMSR(IA32_XSS), because the presence of the IA32_XSS MSR depends only on CPUID.(EAX=0DH, ECX=1):EAX.XSS[bit 3]. However, architecturally, the XSAVES instruction will raise #UD if either CPUID.01H:ECX.XSAVE[bit 26] = 0 or CPUID.(EAX=0DH,ECX=1):EAX.XSS[bit 3] = 0. (Unfortunately, AMD does not allow us to enforce architectural behavior without breaking the existing ABI, as Paolo pointed out previously. This puts us in the bizarre situation where an AMD guest may be able to execute XSAVES even though it cannot read or write IA32_XSS.)
Quoted 1 line
Alternatively, couldn't KVM check arch.xsaves_enabled in the MSR handling?
No. The existence of the IA32_XSS MSR is not the same as the ability of the guest to execute XSAVES.
Quoted 140 lines
Quoted 23 lines
arch/x86/kvm/vmx/vmx.c +3
-2+if (!xsaves_enabled) exec_control &= ~SECONDARY_EXEC_XSAVES ;@@ -6540,7 +6535,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP )vmx_set_interrupt_shadow (vcpu,0 ); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu); if (static_cpu_has(X86_FEATURE_PKU) && kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&@@ -6647,7 +6642,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu)__write_pkru (vmx->host_pkru); } -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu); vmx->nested.nested_run_pending =0 ; vmx->idt_vectoring_info =0 ;arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7091
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -7091,6 +7086,12 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu); +/* +* This will be set back to true in vmx_compute_secondary_exec_control() +* if it should be true, so setting it to false here is okay. Or simply:
/* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */Quoted 72 lines
arch/x86/kvm/vmx/vmx.c +3
-3+*/ + vcpu->arch.xsaves_enabled = false; +if (cpu_has_secondary_exec_ctrls ()) {vmx_compute_secondary_exec_control (vmx);vmcs_set_secondary_exec_control (vmx);@@ -7599,9 +7600,6 @@ static __init int hardware_setup(void)WARN_ONCE (host_bndcfgs,"KVM: BNDCFGS in host will be lost" ); } -if (boot_cpu_has (X86_FEATURE_XSAVES )) -rdmsrl (MSR_IA32_XSS , host_xss); -if (!cpu_has_vmx_vpid () || !cpu_has_vmx_invvpid () || !(cpu_has_vmx_invvpid_single () ||cpu_has_vmx_invvpid_global ())) enable_vpid =0 ;arch/x86/kvm/x86.c +16
-4@@ -176,6 +176,8 @@ struct kvm_shared_msrs {static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;static struct kvm_shared_msrs __percpu *shared_msrs; +static u64 __read_mostly host_xss; +struct kvm_stats_debugfs_item debugfs_entries[] = { {"pf_fixed" ,VCPU_STAT (pf_fixed) }, {"pf_guest" ,VCPU_STAT (pf_guest) },@@ -812,27 +814,37 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) }EXPORT_SYMBOL_GPL (kvm_lmsw); -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu) {if (kvm_read_cr4_bits (vcpu,X86_CR4_OSXSAVE ) && !vcpu->guest_xcr0_loaded) {/* kvm_set_xcr() also depends on this */ if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , vcpu->arch.xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , vcpu->arch.ia32_xss); + vcpu->guest_xcr0_loaded =1 ; } } -EXPORT_SYMBOL_GPL (kvm_load_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_guest_xsave_controls); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu) +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu) {if (vcpu->guest_xcr0_loaded) {if (vcpu->arch.xcr0 != host_xcr0)xsetbv (XCR_XFEATURE_ENABLED_MASK , host_xcr0); + +if (vcpu->arch.xsaves_enabled && + vcpu->arch.ia32_xss != host_xss) +wrmsrl (MSR_IA32_XSS , host_xss); + vcpu->guest_xcr0_loaded =0 ; } } -EXPORT_SYMBOL_GPL (kvm_put_guest_xcr0); +EXPORT_SYMBOL_GPL (kvm_load_host_xsave_controls);static int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr) {arch/x86/kvm/x86.c int kvm_arch_hardware_setup(void) · line 9309
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -9293,6 +9305,9 @@ int kvm_arch_hardware_setup(void) kvm_default_tsc_scaling_ratio =1ULL << kvm_tsc_scaling_ratio_frac_bits; } +if (boot_cpu_has (X86_FEATURE_XSAVES )) +rdmsrl (MSR_IA32_XSS , host_xss);More code that goes beyond the subject of the patch, as
host_xssis now being read and potentially switched on SVM platforms. I don't mind the change itself being part of this patch (unlike settingxsaves_enabled), but IMO the subject should make it more clear that this is a common x86 change. Arguably this should be two patches, one to do what this subject says and a second to move the code to common x86, but that seems like overkill.Quoted 20 lines
arch/x86/kvm/x86.c +0
-0kvm_init_msr_list ();return 0 ; }arch/x86/kvm/x86.h +2
-3@@ -366,7 +366,7 @@ static inline bool kvm_pat_valid(u64 data) return (data | ((data & 0x0202020202020202ull) << 1)) == data; } -void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu); -void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu); +void kvm_load_guest_xsave_controls (struct kvm_vcpu *vcpu); +void kvm_load_host_xsave_controls (struct kvm_vcpu *vcpu);#endif --2.23.0.700.g56cf767bdb-goog
Re: [PATCH v2 2/5] KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
Quoted 67 lines · Jim Mattson
Quoted 56 lines · Sean Christopherson
Quoted 54 lines · Aaron Lewis
Set
IA32_XSSfor the guest and host during VM Enter and VM Exit transitions rather than by using the MSR-load areas.By moving away from using the MSR-load area we can have a unified solution for both AMD and Intel.
Reviewed-by Jim Mattson <[email protected]>Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 7 +++++-- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++------------ arch/x86/kvm/x86.c | 23 +++++++++++++++++++---- arch/x86/kvm/x86.h | 4 ++-- 5 files changed, 37 insertions(+), 20 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -562,6 +562,7 @@ struct kvm_vcpu_arch {u64 smbase;u64 smi_count;bool tpr_access_reporting; +bool xsaves_enabled;u64 ia32_xss;u64 microcode_version;u64 arch_capabilities;arch/x86/kvm/svm.c +2
-2@@ -5628,7 +5628,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->save.cr2 = vcpu->arch.cr2;clgi (); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu);if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.timer_advance_ns)@@ -5778,7 +5778,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu); -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu);stgi ();/* Any pending NMI will happen here */ @@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);Quoted 8 lines
arch/x86/kvm/svm.c static void svm_cpuid_update(struct kvm_vcpu *vcpu) · line 5891
Quoted 2 lines
arch/x86/kvm/svm.c +2
-0+ vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES );This looks very much like a functional change to SVM, which feels wrong for a patch with a subject of "KVM: VMX: Use wrmsr for switching between guest and host
IA32_XSS" feels wrong. Shouldn't this be unconditionally set false in this patch, and then enabled in " kvm: svm: Add support for XSAVES on AMD"?
Quoted 4 lines
Nothing is being enabled here.
Vcpu->arch.xsaves_enabledsimply tells us whether or not the guest can execute the XSAVES instruction. Any guest with the ability to set CR4.OSXSAVE on an AMD host that supports XSAVES can use the instruction.
Not enabling per se, but it's a functional change as it means MSR_IA32_XSS will be written in kvm_load_{guest,host}_xsave_controls() if host_xss!=0.
Quoted 61 lines
Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/svm.c +1
-0+/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );arch/x86/kvm/vmx/vmx.c +0
-2@@ -106,8 +106,6 @@ module_param(enable_apicv, bool, S_IRUGO);static bool __read_mostly nested =1 ;module_param (nested, bool,S_IRUGO ); -static u64 __read_mostly host_xss; -bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO );Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2081
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +0
-5@@ -2074,11 +2072,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (data !=0 )return 1 ; vcpu->arch.ia32_xss = data; -if (vcpu->arch.ia32_xss != host_xss) -add_atomic_switch_msr (vmx,MSR_IA32_XSS , - vcpu->arch.ia32_xss, host_xss, false); -else -clear_atomic_switch_msr (vmx,MSR_IA32_XSS );I'm pretty sure the "
vmx_xsaves_supported()" check in this case statement can be removed after this patch. The host support check was necessary when the MSR load/save list was being used, but moving toxsaves_enabledmeans theres no risk of writingMSR_IA32_XSSwhen it's not supported.I agree.
vmx_xsaves_supported()only tells us if the XSAVES instruction can be enabled. It does not tell us anything about the existence ofIA32_XSSin the guest.Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +0
-0break ;case MSR_IA32_RTIT_CTL : if ((pt_mode != PT_MODE_HOST_GUEST) ||@@ -4038,6 +4031,8 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) &&guest_cpuid_has (vcpu,X86_FEATURE_XSAVES );Quoted 5 lines
arch/x86/kvm/vmx/vmx.c static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) · line 4034
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+ vcpu->arch.xsaves_enabled = xsaves_enabled;Doesn't this conflict with the direction of the previous patch, "KVM: VMX:
Remove unneeded check forX86_FEATURE_XSAVE"?xsaves_enabledis %true iff both XSAVE and XSAVES are exposed to the guest.There's no conflict, because the predicates are different. The previous patch removed the test for XSAVE in the emulation of RDMSR/WRMSR(IA32_XSS), because the presence of the
IA32_XSSMSR depends only on CPUID.(EAX=0DH, ECX=1):EAX.XSS[bit 3]. However, architecturally, the XSAVES instruction will raise #UD if either CPUID.01H:ECX.XSAVE[bit 26] = 0 or CPUID.(EAX=0DH,ECX=1):EAX.XSS[bit 3] = 0. (Unfortunately, AMD does not allow us to enforce architectural behavior without breaking the existing ABI, as Paolo pointed out previously. This puts us in the bizarre situation where an AMD guest may be able to execute XSAVES even though it cannot read or writeIA32_XSS.)
Quoted 4 lines
Quoted 1 line
Alternatively, couldn't KVM check arch.xsaves_enabled in the MSR handling?
No. The existence of the
IA32_XSSMSR is not the same as the ability of the guest to execute XSAVES.
Ugh, that's obnoxious.
Re: [PATCH v2 2/5] KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
Quoted 69 lines · Sean Christopherson
Quoted 67 lines · Jim Mattson
Quoted 64 lines · Sean Christopherson
Quoted 51 lines · Aaron Lewis
Set
IA32_XSSfor the guest and host during VM Enter and VM Exit transitions rather than by using the MSR-load areas.By moving away from using the MSR-load area we can have a unified solution for both AMD and Intel.
Reviewed-by Jim Mattson <[email protected]>Signed-off-by Aaron Lewis <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 7 +++++-- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++------------ arch/x86/kvm/x86.c | 23 +++++++++++++++++++---- arch/x86/kvm/x86.h | 4 ++-- 5 files changed, 37 insertions(+), 20 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -562,6 +562,7 @@ struct kvm_vcpu_arch {u64 smbase;u64 smi_count;bool tpr_access_reporting; +bool xsaves_enabled;u64 ia32_xss;u64 microcode_version;u64 arch_capabilities;arch/x86/kvm/svm.c +2
-2@@ -5628,7 +5628,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) svm->vmcb->save.cr2 = vcpu->arch.cr2;clgi (); -kvm_load_guest_xcr0 (vcpu); +kvm_load_guest_xsave_controls (vcpu);if (lapic_in_kernel (vcpu) && vcpu->arch.apic->lapic_timer.timer_advance_ns)@@ -5778,7 +5778,7 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu); -kvm_put_guest_xcr0 (vcpu); +kvm_load_host_xsave_controls (vcpu);stgi ();/* Any pending NMI will happen here */ arch/x86/kvm/svm.c static void svm_cpuid_update(struct kvm_vcpu *vcpu) · line 5891
Quoted 5 lines
arch/x86/kvm/svm.c +2
-0@@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); + vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES );This looks very much like a functional change to SVM, which feels wrong for a patch with a subject of "KVM: VMX: Use wrmsr for switching between guest and host
IA32_XSS" feels wrong. Shouldn't this be unconditionally set false in this patch, and then enabled in " kvm: svm: Add support for XSAVES on AMD"?
Quoted 7 lines
Quoted 4 lines
Nothing is being enabled here.
Vcpu->arch.xsaves_enabledsimply tells us whether or not the guest can execute the XSAVES instruction. Any guest with the ability to set CR4.OSXSAVE on an AMD host that supports XSAVES can use the instruction.Not enabling per se, but it's a functional change as it means
MSR_IA32_XSSwill be written in kvm_load_{guest,host}_xsave_controls() if host_xss!=0.
Fortunately, host_xss is guaranteed to be zero for the nonce. :-)
Perhaps the commit message just needs to be updated? The only alternatives I see are:
1. Deliberately introducing buggy code to be removed later in the series, or
2. Introducing SVM-specific code first, to be removed later in the series.
Re: [PATCH v2 2/5] KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
Quoted 26 lines · Jim Mattson
Quoted 24 lines · Sean Christopherson
Quoted 21 lines · Jim Mattson
Quoted 6 lines · Sean Christopherson
Quoted 4 lines · Aaron Lewis
arch/x86/kvm/svm.c +0
-0@@ -5887,6 +5887,9 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);Quoted 8 lines
arch/x86/kvm/svm.c static void svm_cpuid_update(struct kvm_vcpu *vcpu) · line 5891
Quoted 2 lines
arch/x86/kvm/svm.c +2
-0+ vcpu->arch.xsaves_enabled =guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && +boot_cpu_has (X86_FEATURE_XSAVES );This looks very much like a functional change to SVM, which feels wrong for a patch with a subject of "KVM: VMX: Use wrmsr for switching between guest and host
IA32_XSS" feels wrong. Shouldn't this be unconditionally set false in this patch, and then enabled in " kvm: svm: Add support for XSAVES on AMD"?Nothing is being enabled here.
Vcpu->arch.xsaves_enabledsimply tells us whether or not the guest can execute the XSAVES instruction. Any guest with the ability to set CR4.OSXSAVE on an AMD host that supports XSAVES can use the instruction.
Quoted 4 lines
Quoted 2 lines
Not enabling per se, but it's a functional change as it means
MSR_IA32_XSSwill be written in kvm_load_{guest,host}_xsave_controls() if host_xss!=0.Fortunately,
host_xssis guaranteed to be zero for the nonce. :-)
And then someone backports something and things go sideways.
Quoted 4 lines
Perhaps the commit message just needs to be updated? The only alternatives I see are:
1. Deliberately introducing buggy code to be removed later in the series, or
2. Introducing SVM-specific code first, to be removed later in the series.
I'd go with:
2a. Make the change actually local to VMX as the subject implies, and
introduce SVM-specific code in a separate patch, to be removed later.
Then the series looks like:
KVM: VMX: Remove unneeded check for X86_FEATURE_XSAVE
KVM: VMX: Use wrmsr for switching between guest and host IA32_XSS
kvm: svm: Add support for XSAVES on AMD
KVM: x86: Move IA32_XSS handling to common x86 code
KVM: x86: Add IA32_XSS to the emulated_msrs list
It'd mean introducing vcpu->arch.xsaves_enabled in the VMX patch and temporarily having it unused by SVM, but that's minor and can't backfire if host_xss suddently is non-zero.
Side topic, I'm pretty sure vcpu->guest_xcr0_loaded is a waste of an 'int' and a conditional branch. VMX and SVM are the only users, and both unconditionally pair kvm_load_guest_xcr0() with kvm_put_guest_xcr0(). Removing the useless flag as another prerequisite patch would make the intermediate VMX and SVM patches slightly less awkward as they wouldn't have to decide between adding a kludgy check on guest_xcr0_loaded and ignoring it altogether.
PATCH v2 3/5 kvm: svm: Add support for XSAVES on AMD
In reply to Aaron LewisHoist support for RDMSR/WRMSR of IA32_XSS from vmx into common code so that it can be used for AMD as well.
AMD has no equivalent of Intel's "Enable XSAVES/XRSTORS" VM-execution control. Instead, XSAVES is always available to the guest when supported on the host.
Unfortunately, right now, kvm only allows the guest IA32_XSS to be zero, so the guest's usage of XSAVES will be exactly the same as XSAVEC.
--- arch/x86/kvm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 20 -------------------- arch/x86/kvm/x86.c | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 21 deletions(-)
arch/x86/kvm/svm.c +1 -1
@@ -5965,7 +5965,7 @@ static bool svm_mpx_supported(void)static bool svm_xsaves_supported (void ) { -return false; +return boot_cpu_has (X86_FEATURE_XSAVES ); }static bool svm_umip_emulated (void )
arch/x86/kvm/vmx/vmx.c +0 -20
@@ -1818,13 +1818,6 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 1 ;return vmx_get_vmx_msr (&vmx->nested.msrs, msr_info->index, &msr_info->data); -case MSR_IA32_XSS : -if (!vmx_xsaves_supported () || - (!msr_info->host_initiated && - !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ))) -return 1 ; - msr_info->data = vcpu->arch.ia32_xss; -break ;case MSR_IA32_RTIT_CTL :if (pt_mode !=PT_MODE_HOST_GUEST )return 1 ;
@@ -2060,19 +2053,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (!nested_vmx_allowed (vcpu))return 1 ;return vmx_set_vmx_msr (vcpu, msr_index, data); -case MSR_IA32_XSS : -if (!vmx_xsaves_supported () || - (!msr_info->host_initiated && - !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ))) -return 1 ; -/* -* The only supported bit as of Skylake is bit 8, but -* it is not supported on KVM. -*/ -if (data !=0 ) -return 1 ; - vcpu->arch.ia32_xss = data; -break ;case MSR_IA32_RTIT_CTL : if ((pt_mode != PT_MODE_HOST_GUEST) || vmx_rtit_ctl_check(vcpu, data) ||
arch/x86/kvm/x86.c +22 -0
@@ -2700,6 +2700,21 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_TSC :kvm_write_tsc (vcpu, msr_info);break ; +case MSR_IA32_XSS : +if (!kvm_x86_ops->xsaves_supported () || + (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ))) +return 1 ; +/* +* We do support PT if kvm_x86_ops->pt_supported(), but we do +* not support IA32_XSS[bit 8]. Guests will have to use +* RDMSR/WRMSR rather than XSAVES/XRSTORS to save/restore PT +* MSRs. +*/ +if (data !=0 ) +return 1 ; + vcpu->arch.ia32_xss = data; +break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)return 1 ;
@@ -3030,6 +3045,13 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:return get_msr_mce (vcpu, msr_info->index, &msr_info->data, msr_info->host_initiated); +case MSR_IA32_XSS : +if (!kvm_x86_ops->xsaves_supported () || + (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_XSAVES ))) +return 1 ; + msr_info->data = vcpu->arch.ia32_xss; +break ;case MSR_K7_CLK_CTL :/* * Provide expected ramp-up count for K7. All other
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
PATCH v2 4/5 kvm: x86: Add IA32_XSS to the emulated_msrs list
In reply to Aaron LewisAdd IA32_XSS to the list of emulated MSRs if it is supported in the guest. At the moment, the guest IA32_XSS must be zero, but this change prepares for expanded support in the future.
--- arch/x86/kvm/svm.c | 12 +++++++----- arch/x86/kvm/vmx/vmx.c | 2 ++ arch/x86/kvm/x86.c | 1 + 3 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kvm/svm.c +7 -5
@@ -498,6 +498,11 @@ static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)return (READ_ONCE (*entry) &AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK ); } +static bool svm_xsaves_supported (void ) +{ +return boot_cpu_has (X86_FEATURE_XSAVES ); +} +static void recalc_intercepts (struct vcpu_svm *svm) {struct vmcb_control_area *c, *h;
@@ -5871,6 +5876,8 @@ static bool svm_has_emulated_msr(int index)case MSR_IA32_MCG_EXT_CTL : case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:return false; +case MSR_IA32_XSS : +return svm_xsaves_supported ();default :break ; }
@@ -5963,11 +5970,6 @@ static bool svm_mpx_supported(void)return false; } -static bool svm_xsaves_supported (void ) -{ -return boot_cpu_has (X86_FEATURE_XSAVES ); -} -static bool svm_umip_emulated (void ) {return false;
arch/x86/kvm/vmx/vmx.c +2 -0
@@ -6272,6 +6272,8 @@ static bool vmx_has_emulated_msr(int index)case MSR_AMD64_VIRT_SPEC_CTRL :/* This is AMD only. */ return false; +case MSR_IA32_XSS : +return vmx_xsaves_supported ();default :return true; }
arch/x86/kvm/x86.c +1 -0
@@ -1227,6 +1227,7 @@ static u32 emulated_msrs[] = {MSR_MISC_FEATURES_ENABLES ,MSR_AMD64_VIRT_SPEC_CTRL ,MSR_IA32_POWER_CTL , + MSR_IA32_XSS,/* * The following list leaves out MSRs whose values are determined
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
PATCH v2 5/5 kvm: tests: Add test to verify MSR_IA32_XSS
In reply to Aaron LewisVerify that calling get and set for MSR_IA32_XSS returns expected results.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 7 +- .../selftests/kvm/lib/x86_64/processor.c | 72 ++++++++++++++++--- .../selftests/kvm/x86_64/xss_msr_test.c | 70 ++++++++++++++++++ 5 files changed, 141 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/xss_msr_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -11,6 +11,7 @@ /x86_64/vmx_close_while_nested_test /x86_64/vmx_set_nested_state_test /x86_64/vmx_tsc_adjust_test +/x86_64/xss_msr_test /clear_dirty_log_test /dirty_log_test /kvm_create_max_vcpus
tools/testing/selftests/kvm/Makefile +1 -0
@@ -25,6 +25,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test +TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test TEST_GEN_PROGS_x86_64 += clear_dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/include/x86_64/processor.h +6 -1
@@ -308,6 +308,8 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid);void vcpu_load_state (struct kvm_vm *vm,uint32_t vcpuid,struct kvm_x86_state *state); +struct kvm_msr_list *kvm_get_msr_index_list (void ); +struct kvm_cpuid2 *kvm_get_supported_cpuid (void );void vcpu_set_cpuid (struct kvm_vm *vm,uint32_t vcpuid,struct kvm_cpuid2 *cpuid);
@@ -322,10 +324,13 @@ kvm_get_supported_cpuid_entry(uint32_t function) }uint64_t vcpu_get_msr (struct kvm_vm *vm,uint32_t vcpuid,uint64_t msr_index); +int _vcpu_set_msr (struct kvm_vm *vm,uint32_t vcpuid,uint64_t msr_index, +uint64_t msr_value);void vcpu_set_msr (struct kvm_vm *vm,uint32_t vcpuid,uint64_t msr_index,uint64_t msr_value); -uint32_t kvm_get_cpuid_max (void ); +uint32_t kvm_get_cpuid_max_basic (void ); +uint32_t kvm_get_cpuid_max_extended (void );void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits);/*
tools/testing/selftests/kvm/lib/x86_64/processor.c +63 -9
121 lines not on this page · raw message
tools/testing/selftests/kvm/x86_64/xss_msr_test.c new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.23.0.700.g56cf767bdb-goog