Get supported_xss ready for XSS dependent
cover Get supported_xss ready for XSS dependent
Although supported_xss was added long time ago, yet it doesn't get ready for XSS dependent new features usage, e.g., when guest update XSS MSRs, it's necessary to update guest CPUID to reflect the correct info. So post this patchset to get things ready, or at least as a hint to maintainers that there're still a few things left before support feature bits in XSS.
Also added a few helpers to facilitate new features development. This part of code originates from CET KVM patchset, with more and more new features dependent on this part, post this patchset ahead of them.
Sean Christopherson (2):
KVM: x86: Add helpers for {set|clear} bits in supported_xss
KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
Yang Weijiang (1):
KVM: x86: Refresh CPUID when guest modifies MSR_IA32_XSS
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 21 ++++++++++++-- arch/x86/kvm/vmx/vmx.c | 22 +++++++++++++++ arch/x86/kvm/x86.c | 50 +++++++++++++++++++++++++++++++-- 4 files changed, 88 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.2
RFC PATCH 1/3 KVM: x86: Add helpers for {set|clear} bits in supported_xss
From: Sean Christopherson <[email protected]>
KVM supported XSS feature bits are designated in supported_xss, bits could be set/cleared dynamically, add helpers to facilitate the operation. Also add MSR_IA32_XSS to the list of MSRs reported to userspace if supported_xss is non-zero, i.e. KVM supports at least one XSS based feature.
--- arch/x86/kvm/x86.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
arch/x86/kvm/x86.c +20 -0
@@ -1237,6 +1237,8 @@ static const u32 msrs_to_save_all[] = {MSR_ARCH_PERFMON_EVENTSEL0 +12 ,MSR_ARCH_PERFMON_EVENTSEL0 +13 ,MSR_ARCH_PERFMON_EVENTSEL0 +14 ,MSR_ARCH_PERFMON_EVENTSEL0 +15 ,MSR_ARCH_PERFMON_EVENTSEL0 +16 ,MSR_ARCH_PERFMON_EVENTSEL0 +17 , + + MSR_IA32_XSS, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -5728,6 +5730,10 @@ static void kvm_init_msr_list(void) min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) continue;break ; +case MSR_IA32_XSS : +if (!supported_xss) +continue ; +break ;default :break ; }
@@ -10137,6 +10143,18 @@ void kvm_arch_hardware_disable(void)drop_user_return_notifiers (); } +static inline void __maybe_unused kvm_set_xss_bits (u32 low,u32 high) +{ + supported_xss |= low; + supported_xss |= ((u64 )high) <<32 ; +} + +static inline void __maybe_unused kvm_clear_xss_bits (u32 low,u32 high) +{ + supported_xss &= ~low; + supported_xss &= ~(((u64 )high) <<32 ); +} +int kvm_arch_hardware_setup (void *opaque) {struct kvm_x86_init_ops *ops = opaque;
@@ -10155,6 +10173,8 @@ int kvm_arch_hardware_setup(void *opaque)if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ; +else + supported_xss &= host_xss;#define __kvm_cpu_cap_has (UNUSED_ , f) kvm_cpu_cap_has(f) cr4_reserved_bits =__cr4_reserved_bits (__kvm_cpu_cap_has,UNUSED_ );
Signature · 1 line
2.17.2
RFC PATCH 2/3 KVM: x86: Refresh CPUID when guest modifies MSR_IA32_XSS
In reply to Yang WeijiangUpdated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 21 ++++++++++++++++++--- arch/x86/kvm/x86.c | 7 +++++-- 3 files changed, 24 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -611,6 +611,7 @@ struct kvm_vcpu_arch {u64 xcr0;u64 guest_supported_xcr0; +u64 guest_supported_xss;struct kvm_pio_request pio;void *pio_data;
arch/x86/kvm/cpuid.c +18 -3
@@ -115,9 +115,24 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) { +if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ; + } + vcpu->arch.guest_supported_xss = + (((u64 )best->edx <<32 ) | best->ecx) & supported_xss; + + }else { + vcpu->arch.guest_supported_xss =0 ; + } best =kvm_find_cpuid_entry (vcpu,KVM_CPUID_FEATURES ,0 ); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
arch/x86/kvm/x86.c +5 -2
@@ -3124,9 +3124,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than * XSAVES/XRSTORS to save/restore PT MSRs. */ -if (data & ~supported_xss) +if (data & ~vcpu->arch.guest_supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)
Signature · 1 line
2.17.2
RFC PATCH 3/3 KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
When feature MSRs supported in XSS are passed through to the guest they are saved and restored by XSAVES/XRSTORS, i.e. in the guest's FPU state.
Load the guest's FPU state if userspace is accessing MSRs whose values are managed by XSAVES so that the MSR helper, e.g. vmx_{get,set}_xsave_msr(), can simply do {RD,WR}MSR to access the guest's value.
Because is also used for the KVM_GET_MSRS device ioctl(), explicitly check that @vcpu is non-null before attempting to load guest state. The XSS supporting MSRs cannot be retrieved via the device ioctl() without loading guest FPU state (which doesn't exist).
MSRs that are switched through XSAVES are especially annoying due to the possibility of the kernel's FPU being used in IRQ context. Disable IRQs and ensure the guest's FPU state is loaded when accessing such MSRs.
Note that guest_cpuid_has() is not queried as host userspace is allowed to access MSRs that have not been exposed to the guest, e.g. it might do KVM_SET_MSRS prior to KVM_SET_CPUID2.
--- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++++++++++++++ arch/x86/kvm/x86.c | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +22 -0
@@ -1773,6 +1773,28 @@ static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,return !(val & ~valid_bits); } +/* +* Below two helpers are used for XSS managed guest MSRs, accessing the MSRs +* should rely on these helpers to avoid latent guest MSR content overwritten. +*/ +static void __maybe_unused vmx_get_xsave_msr (struct msr_data *msr_info) +{ +local_irq_disable (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +switch_fpu_return (); +rdmsrl (msr_info->index, msr_info->data); +local_irq_enable (); +} + +static void __maybe_unused vmx_set_xsave_msr (struct msr_data *msr_info) +{ +local_irq_disable (); +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +switch_fpu_return (); +wrmsrl (msr_info->index, msr_info->data); +local_irq_enable (); +} +static int vmx_get_msr_feature (struct kvm_msr_entry *msr) {switch (msr->index) {
arch/x86/kvm/x86.c +22 -1
@@ -109,6 +109,8 @@ static void enter_smm(struct kvm_vcpu *vcpu);static void __kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu); +static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu); +static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu);struct kvm_x86_ops kvm_x86_ops__read_mostly ;EXPORT_SYMBOL_GPL (kvm_x86_ops);
@@ -3575,6 +3577,16 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }EXPORT_SYMBOL_GPL (kvm_get_msr_common); +/* +* If new features passthrough XSS managed MSRs to guest, it's required to +* add separate checks here so as to load feature dependent guest MSRs before +* access them. +*/ +static bool is_xsaves_msr (u32 index) +{ +return false; +} +/* * Read or write a bunch of msrs. All parameters are kernel addresses. *
@@ -3585,11 +3597,20 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,int (*do_msr)(struct kvm_vcpu *vcpu, unsigned index, u64 *data)) { +bool fpu_loaded = false;int i; -for (i =0 ; i < msrs->nmsrs; ++i) +for (i =0 ; i < msrs->nmsrs; ++i) { +if (vcpu && !fpu_loaded && supported_xss && +is_xsaves_msr (entries[i].index)) { +kvm_load_guest_fpu (vcpu); + fpu_loaded = true; + }if (do_msr (vcpu, entries[i].index, &entries[i].data))break ; + } +if (fpu_loaded) +kvm_put_guest_fpu (vcpu);return i; }
Signature · 1 line
2.17.2