KVM: x86: generalize guest cpuid helpers
v2 cover KVM: x86: generalize guest cpuid helpers
RFC v1: https://www.spinics.net/lists/kvm/msg153753.html
x86_64 and i386 compiles for me, but let's see what build bots think of it.
Radim Krčmář (3):
KVM: x86: X86_FEATURE_NRIPS is not scattered anymore
KVM: x86: generalize guest_cpuid_has_ helpers
KVM: x86: use general helpers for some cpuid manipulation
arch/x86/kvm/cpuid.h | 183 +++++++++++++++++---------------------------------- arch/x86/kvm/mmu.c | 7 +- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm.c | 7 +- arch/x86/kvm/vmx.c | 32 ++++----- arch/x86/kvm/x86.c | 52 +++++++-------- 6 files changed, 105 insertions(+), 178 deletions(-)
Signature · 1 line
2.13.3
PATCH v2 1/3 KVM: x86: X86_FEATURE_NRIPS is not scattered anymore
bit(X86_FEATURE_NRIPS) is 3 since 2ccd71f1b278 ("x86/cpufeature: Move some of the scattered feature bits to x86_capability"), so we can simplify the code.
---
v2: new, explain why it's ok to use X86_FEATURE_NRIPS (David)
--- arch/x86/kvm/cpuid.h | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
arch/x86/kvm/cpuid.h +1 -13
@@ -160,25 +160,13 @@ static inline bool guest_cpuid_has_rdtscp(struct kvm_vcpu *vcpu)return best && (best->edx &bit (X86_FEATURE_RDTSCP )); } -/* -* NRIPS is provided through cpuidfn 0x8000000a.edx bit 3 -*/ -#define BIT_NRIPS 3 -static inline bool guest_cpuid_has_nrips (struct kvm_vcpu *vcpu) {struct kvm_cpuid_entry2 *best; best =kvm_find_cpuid_entry (vcpu,0x8000000a ,0 ); - -/* -* NRIPS is a scattered cpuid feature, so we can't use -* X86_FEATURE_NRIPS here (X86_FEATURE_NRIPS would be bit -* position 8, not 3). -*/ -return best && (best->edx &bit (BIT_NRIPS )); +return best && (best->edx &bit (X86_FEATURE_NRIPS )); } -#undef BIT_NRIPSstatic inline int guest_cpuid_family (struct kvm_vcpu *vcpu) {
Signature · 1 line
2.13.3
PATCH v2 2/3 KVM: x86: generalize guest_cpuid_has_ helpers
In reply to Radim KrčmářThis patch turns guest_cpuid_has_XYZ(cpuid) into guest_cpuid_has(cpuid, X86_FEATURE_XYZ), which gets rid of many very similar helpers.
When seeing a X86_FEATURE_*, we can know which cpuid it belongs to, but this information isn't in common code, so we recreate it for KVM.
Add some BUILD_BUG_ONs to make sure that it runs nicely.
---
v2:
- fixed X86_FEATURE_RDTSCP (David)
- renamed best to entry (David)
- added const (David)
- added __always_inline (Paolo)
--- arch/x86/kvm/cpuid.h | 170 +++++++++++++++++---------------------------------- arch/x86/kvm/mmu.c | 7 ++- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm.c | 2 +- arch/x86/kvm/vmx.c | 26 ++++---- arch/x86/kvm/x86.c | 38 ++++++------ 6 files changed, 95 insertions(+), 150 deletions(-)
arch/x86/kvm/cpuid.h +57 -113
@@ -3,6 +3,7 @@#include "x86.h" #include <asm/cpu.h> +#include <asm/processor.h> int kvm_update_cpuid (struct kvm_vcpu *vcpu);bool kvm_mpx_supported (void );
@@ -29,95 +30,78 @@ static inline int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)return vcpu->arch.maxphyaddr; } -static inline bool guest_cpuid_has_xsave (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; +struct cpuid_reg { +u32 function; +u32 index; +int reg; +}; -if (!static_cpu_has (X86_FEATURE_XSAVE )) -return false; - - best =kvm_find_cpuid_entry (vcpu,1 ,0 ); -return best && (best->ecx &bit (X86_FEATURE_XSAVE )); -} +static const struct cpuid_reg reverse_cpuid[] = { + [CPUID_1_EDX ] = {1 ,0 ,CPUID_EDX }, + [CPUID_8000_0001_EDX ] = {0x80000001 ,0 ,CPUID_EDX }, + [CPUID_8086_0001_EDX ] = {0x80860001 ,0 ,CPUID_EDX }, + [CPUID_1_ECX ] = {1 ,0 ,CPUID_ECX }, + [CPUID_C000_0001_EDX ] = {0xc0000001 ,0 ,CPUID_EDX }, + [CPUID_8000_0001_ECX ] = {0xc0000001 ,0 ,CPUID_ECX }, + [CPUID_7_0_EBX ] = {7 ,0 ,CPUID_EBX }, + [CPUID_D_1_EAX ] = {0xd ,1 ,CPUID_EAX }, + [CPUID_F_0_EDX ] = {0xf ,0 ,CPUID_EDX }, + [CPUID_F_1_EDX ] = {0xf ,1 ,CPUID_EDX }, + [CPUID_8000_0008_EBX ] = {0x80000008 ,0 ,CPUID_EBX }, + [CPUID_6_EAX ] = {6 ,0 ,CPUID_EAX }, + [CPUID_8000_000A_EDX ] = {0x8000000a ,0 ,CPUID_EDX }, + [CPUID_7_ECX ] = {7 ,0 ,CPUID_ECX }, + [CPUID_8000_0007_EBX ] = {0x80000007 ,0 ,CPUID_EBX }, +}; -static inline bool guest_cpuid_has_mtrr (struct kvm_vcpu *vcpu) +static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned x86_feature) { -struct kvm_cpuid_entry2 *best; +unsigned x86_leaf = x86_feature /32 ; - best =kvm_find_cpuid_entry (vcpu,1 ,0 ); -return best && (best->edx &bit (X86_FEATURE_MTRR )); -} +BUILD_BUG_ON (!__builtin_constant_p (x86_leaf)); +BUILD_BUG_ON (x86_leaf >=ARRAY_SIZE (reverse_cpuid)); +BUILD_BUG_ON (reverse_cpuid[x86_leaf].function ==0 ); -static inline bool guest_cpuid_has_tsc_adjust (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_TSC_ADJUST )); -} - -static inline bool guest_cpuid_has_smep (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_SMEP )); +return reverse_cpuid[x86_leaf]; } -static inline bool guest_cpuid_has_smap (struct kvm_vcpu *vcpu) +static __always_inline int *guest_cpuid_get_register(struct kvm_vcpu *vcpu, unsigned x86_feature) { -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_SMAP )); -} - -static inline bool guest_cpuid_has_fsgsbase (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_FSGSBASE )); -} - -static inline bool guest_cpuid_has_pku (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ecx &bit (X86_FEATURE_PKU )); -} - -static inline bool guest_cpuid_has_longmode (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -return best && (best->edx &bit (X86_FEATURE_LM )); -} +struct kvm_cpuid_entry2 *entry; +const struct cpuid_reg cpuid =x86_feature_cpuid (x86_feature); -static inline bool guest_cpuid_has_osvw (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; + entry =kvm_find_cpuid_entry (vcpu, cpuid.function, cpuid.index); +if (!entry) +return NULL ; - best =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -return best && (best->ecx &bit (X86_FEATURE_OSVW )); +switch (cpuid.reg) { +case CPUID_EAX : +return &entry->eax; +case CPUID_EBX : +return &entry->ebx; +case CPUID_ECX : +return &entry->ecx; +case CPUID_EDX : +return &entry->edx; +default : +BUILD_BUG (); +return NULL ; + } } -static inline bool guest_cpuid_has_pcid (struct kvm_vcpu *vcpu) +static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_feature) { -struct kvm_cpuid_entry2 *best; +int *reg; - best =kvm_find_cpuid_entry (vcpu,1 ,0 ); -return best && (best->ecx &bit (X86_FEATURE_PCID )); -} +if (x86_feature ==X86_FEATURE_XSAVE && + !static_cpu_has (X86_FEATURE_XSAVE )) +return false; -static inline bool guest_cpuid_has_x2apic (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; + reg =guest_cpuid_get_register (vcpu, x86_feature); +if (!reg) +return false; - best =kvm_find_cpuid_entry (vcpu,1 ,0 ); -return best && (best->ecx &bit (X86_FEATURE_X2APIC )); +return *reg &bit (x86_feature); }static inline bool guest_cpuid_is_amd (struct kvm_vcpu *vcpu)
@@ -128,46 +112,6 @@ static inline bool guest_cpuid_is_amd(struct kvm_vcpu *vcpu)return best && best->ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx; } -static inline bool guest_cpuid_has_gbpages (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -return best && (best->edx &bit (X86_FEATURE_GBPAGES )); -} - -static inline bool guest_cpuid_has_rtm (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_RTM )); -} - -static inline bool guest_cpuid_has_mpx (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,7 ,0 ); -return best && (best->ebx &bit (X86_FEATURE_MPX )); -} - -static inline bool guest_cpuid_has_rdtscp (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -return best && (best->edx &bit (X86_FEATURE_RDTSCP )); -} - -static inline bool guest_cpuid_has_nrips (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best; - - best =kvm_find_cpuid_entry (vcpu,0x8000000a ,0 ); -return best && (best->edx &bit (X86_FEATURE_NRIPS )); -} -static inline int guest_cpuid_family (struct kvm_vcpu *vcpu) {struct kvm_cpuid_entry2 *best;
arch/x86/kvm/mmu.c +4 -3
@@ -4052,7 +4052,8 @@ static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, {__reset_rsvds_bits_mask (vcpu, &context->guest_rsvd_check,cpuid_maxphyaddr (vcpu), context->root_level, - context->nx,guest_cpuid_has_gbpages (vcpu), + context->nx, +guest_cpuid_has (vcpu,X86_FEATURE_GBPAGES ),is_pse (vcpu),guest_cpuid_is_amd (vcpu)); }
@@ -4114,8 +4115,8 @@ reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)__reset_rsvds_bits_mask (vcpu, &context->shadow_zero_check, boot_cpu_data.x86_phys_bits, context->shadow_root_level, uses_nx, -guest_cpuid_has_gbpages (vcpu),is_pse (vcpu), - true); +guest_cpuid_has (vcpu,X86_FEATURE_GBPAGES ), +is_pse (vcpu), true); }EXPORT_SYMBOL_GPL (reset_shadow_zero_bits_mask);
arch/x86/kvm/mtrr.c +1 -1
@@ -130,7 +130,7 @@ static u8 mtrr_disabled_type(struct kvm_vcpu *vcpu)* enable MTRRs and it is obviously undesirable to run the * guest entirely with UC memory and we use WB. */ -if (guest_cpuid_has_mtrr (vcpu)) +if (guest_cpuid_has (vcpu,X86_FEATURE_MTRR ))return MTRR_TYPE_UNCACHABLE ;else return MTRR_TYPE_WRBACK ;
arch/x86/kvm/svm.c +1 -1
@@ -5078,7 +5078,7 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu)struct kvm_cpuid_entry2 *entry;/* Update nrips enabled cache */ - svm->nrips_enabled = !!guest_cpuid_has_nrips (&svm->vcpu); + svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );if (!kvm_vcpu_apicv_active (vcpu))return ;
arch/x86/kvm/vmx.c +12 -14
@@ -2588,7 +2588,7 @@ static void setup_msrs(struct vcpu_vmx *vmx)if (index >=0 )move_msr_up (vmx, index, save_nmsrs++); index =__find_msr_index (vmx,MSR_TSC_AUX ); -if (index >=0 &&guest_cpuid_has_rdtscp (&vmx->vcpu)) +if (index >=0 &&guest_cpuid_has (&vmx->vcpu,X86_FEATURE_RDTSCP ))move_msr_up (vmx, index, save_nmsrs++);/* * MSR_STAR is only needed on long mode guests, and only
@@ -2648,12 +2648,6 @@ static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) } } -static bool guest_cpuid_has_vmx (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid_entry2 *best =kvm_find_cpuid_entry (vcpu,1 ,0 ); -return best && (best->ecx & (1 << (X86_FEATURE_VMX &31 ))); -} -/* * nested_vmx_allowed() checks whether a guest should be allowed to use VMX * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
@@ -2662,7 +2656,7 @@ static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)*/ static inline bool nested_vmx_allowed (struct kvm_vcpu *vcpu) { -return nested &&guest_cpuid_has_vmx (vcpu); +return nested &&guest_cpuid_has (vcpu,X86_FEATURE_VMX ); }/*
@@ -3244,7 +3238,8 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_BNDCFGS :if (!kvm_mpx_supported () || - (!msr_info->host_initiated && !guest_cpuid_has_mpx (vcpu))) + (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_MPX )))return 1 ; msr_info->data =vmcs_read64 (GUEST_BNDCFGS );break ;
@@ -3268,7 +3263,8 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vcpu->arch.ia32_xss;break ;case MSR_TSC_AUX : -if (!guest_cpuid_has_rdtscp (vcpu) && !msr_info->host_initiated) +if (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))return 1 ;/* Otherwise falls through */ default :
@@ -3327,7 +3323,8 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_BNDCFGS :if (!kvm_mpx_supported () || - (!msr_info->host_initiated && !guest_cpuid_has_mpx (vcpu))) + (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_MPX )))return 1 ;if (is_noncanonical_address (data &PAGE_MASK ) || (data &MSR_IA32_BNDCFGS_RSVD ))
@@ -3390,7 +3387,8 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)clear_atomic_switch_msr (vmx,MSR_IA32_XSS );break ;case MSR_TSC_AUX : -if (!guest_cpuid_has_rdtscp (vcpu) && !msr_info->host_initiated) +if (!msr_info->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))return 1 ;/* Check reserved bit, higher 32 bits should be zero */ if ((data >>32 ) !=0 )
@@ -9458,7 +9456,7 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)u32 secondary_exec_ctl =vmx_secondary_exec_control (vmx);if (vmx_rdtscp_supported ()) { -bool rdtscp_enabled =guest_cpuid_has_rdtscp (vcpu); +bool rdtscp_enabled =guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP );if (!rdtscp_enabled) secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP ;
@@ -9477,7 +9475,7 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)struct kvm_cpuid_entry2 *best =kvm_find_cpuid_entry (vcpu,0x7 ,0 );bool invpcid_enabled = best && best->ebx &bit (X86_FEATURE_INVPCID ) && -guest_cpuid_has_pcid (vcpu); +guest_cpuid_has (vcpu,X86_FEATURE_PCID );if (!invpcid_enabled) { secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID ;
arch/x86/kvm/x86.c +20 -18
@@ -310,8 +310,8 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (MSR_IA32_APICBASE_ENABLE |X2APIC_ENABLE );u64 new_state = msr_info->data & (MSR_IA32_APICBASE_ENABLE |X2APIC_ENABLE ); -u64 reserved_bits = ((~0ULL ) <<cpuid_maxphyaddr (vcpu)) | -0x2ff | (guest_cpuid_has_x2apic (vcpu) ?0 :X2APIC_ENABLE ); +u64 reserved_bits = ((~0ULL ) <<cpuid_maxphyaddr (vcpu)) |0x2ff | + (guest_cpuid_has (vcpu,X86_FEATURE_X2APIC ) ?0 :X2APIC_ENABLE ); if (!msr_info->host_initiated && ((msr_info->data & reserved_bits) != 0 ||
@@ -754,19 +754,19 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)if (cr4 &CR4_RESERVED_BITS )return 1 ; -if (!guest_cpuid_has_xsave (vcpu) && (cr4 &X86_CR4_OSXSAVE )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && (cr4 &X86_CR4_OSXSAVE ))return 1 ; -if (!guest_cpuid_has_smep (vcpu) && (cr4 &X86_CR4_SMEP )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_SMEP ) && (cr4 &X86_CR4_SMEP ))return 1 ; -if (!guest_cpuid_has_smap (vcpu) && (cr4 &X86_CR4_SMAP )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_SMAP ) && (cr4 &X86_CR4_SMAP ))return 1 ; -if (!guest_cpuid_has_fsgsbase (vcpu) && (cr4 &X86_CR4_FSGSBASE )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_FSGSBASE ) && (cr4 &X86_CR4_FSGSBASE ))return 1 ; -if (!guest_cpuid_has_pku (vcpu) && (cr4 &X86_CR4_PKE )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_PKU ) && (cr4 &X86_CR4_PKE ))return 1 ;if (is_long_mode (vcpu)) {
@@ -779,7 +779,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ;if ((cr4 &X86_CR4_PCIDE ) && !(old_cr4 &X86_CR4_PCIDE )) { -if (!guest_cpuid_has_pcid (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_PCID ))return 1 ;/* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
@@ -883,7 +883,7 @@ static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) {u64 fixed =DR6_FIXED_1 ; -if (!guest_cpuid_has_rtm (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_RTM )) fixed |=DR6_RTM ;return fixed; }
@@ -1534,8 +1534,9 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; -if (guest_cpuid_has_tsc_adjust (vcpu) && !msr->host_initiated) +if (!msr->host_initiated &&guest_cpuid_has (vcpu,X86_FEATURE_TSC_ADJUST ))update_ia32_tsc_adjust_msr (vcpu, offset); +kvm_vcpu_write_tsc_offset (vcpu, offset);raw_spin_unlock_irqrestore (&kvm->arch.tsc_write_lock, flags);
@@ -2185,7 +2186,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)kvm_set_lapic_tscdeadline_msr (vcpu, data);break ;case MSR_IA32_TSC_ADJUST : -if (guest_cpuid_has_tsc_adjust (vcpu)) { +if (guest_cpuid_has (vcpu,X86_FEATURE_TSC_ADJUST )) {if (!msr_info->host_initiated) {s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;adjust_tsc_offset_guest (vcpu, adj);
@@ -2307,12 +2308,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)vcpu_unimpl (vcpu,"ignored wrmsr: 0x%x data 0x%llx\n" , msr, data);break ;case MSR_AMD64_OSVW_ID_LENGTH : -if (!guest_cpuid_has_osvw (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))return 1 ; vcpu->arch.osvw.length = data;break ;case MSR_AMD64_OSVW_STATUS : -if (!guest_cpuid_has_osvw (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))return 1 ; vcpu->arch.osvw.status = data;break ;
@@ -2537,12 +2538,12 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =0xbe702111 ;break ;case MSR_AMD64_OSVW_ID_LENGTH : -if (!guest_cpuid_has_osvw (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))return 1 ; msr_info->data = vcpu->arch.osvw.length;break ;case MSR_AMD64_OSVW_STATUS : -if (!guest_cpuid_has_osvw (vcpu)) +if (!guest_cpuid_has (vcpu,X86_FEATURE_OSVW ))return 1 ; msr_info->data = vcpu->arch.osvw.status;break ;
@@ -6606,7 +6607,7 @@ static void enter_smm(struct kvm_vcpu *vcpu)trace_kvm_enter_smm (vcpu->vcpu_id, vcpu->arch.smbase, true); vcpu->arch.hflags |=HF_SMM_MASK ;memset (buf,0 ,512 ); -if (guest_cpuid_has_longmode (vcpu)) +if (guest_cpuid_has (vcpu,X86_FEATURE_LM ))enter_smm_save_state_64 (vcpu, buf);else enter_smm_save_state_32 (vcpu, buf);
@@ -6658,7 +6659,7 @@ static void enter_smm(struct kvm_vcpu *vcpu)kvm_set_segment (vcpu, &ds,VCPU_SREG_GS );kvm_set_segment (vcpu, &ds,VCPU_SREG_SS ); -if (guest_cpuid_has_longmode (vcpu)) +if (guest_cpuid_has (vcpu,X86_FEATURE_LM )) kvm_x86_ops->set_efer (vcpu,0 );kvm_update_cpuid (vcpu);
@@ -7424,7 +7425,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,int pending_vec, max_bits, idx;struct desc_ptr dt; -if (!guest_cpuid_has_xsave (vcpu) && (sregs->cr4 &X86_CR4_OSXSAVE )) +if (!guest_cpuid_has (vcpu,X86_FEATURE_XSAVE ) && + (sregs->cr4 &X86_CR4_OSXSAVE ))return -EINVAL ; dt.size = sregs->idt.limit;
Signature · 1 line
2.13.3
PATCH v2 3/3 KVM: x86: use general helpers for some cpuid manipulation
In reply to Radim KrčmářAdd guest_cpuid_clear() and use it instead of kvm_find_cpuid_entry(). Also replace some uses of kvm_find_cpuid_entry() with guest_cpuid_has().
---
v2: added __always_inline (Paolo)
--- arch/x86/kvm/cpuid.h | 9 +++++++++ arch/x86/kvm/svm.c | 5 +---- arch/x86/kvm/vmx.c | 6 ++---- arch/x86/kvm/x86.c | 14 ++------------ 4 files changed, 14 insertions(+), 20 deletions(-)
arch/x86/kvm/cpuid.h +9 -0
@@ -104,6 +104,15 @@ static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_return *reg &bit (x86_feature); } +static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, unsigned x86_feature) +{ +int *reg; + + reg =guest_cpuid_get_register (vcpu, x86_feature); +if (reg) + *reg &= ~bit (x86_feature); +} +static inline bool guest_cpuid_is_amd (struct kvm_vcpu *vcpu) {struct kvm_cpuid_entry2 *best;
arch/x86/kvm/svm.c +1 -4
@@ -5075,7 +5075,6 @@ static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)static void svm_cpuid_update (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_cpuid_entry2 *entry;/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );
@@ -5083,9 +5082,7 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu)if (!kvm_vcpu_apicv_active (vcpu))return ; - entry =kvm_find_cpuid_entry (vcpu,1 ,0 ); -if (entry) - entry->ecx &= ~bit (X86_FEATURE_X2APIC ); +guest_cpuid_clear (vcpu,X86_FEATURE_X2APIC ); }static void svm_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry)
arch/x86/kvm/vmx.c +2 -4
@@ -9472,15 +9472,13 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (vmx_invpcid_supported ()) {/* Exposing INVPCID only when PCID is exposed */ -struct kvm_cpuid_entry2 *best =kvm_find_cpuid_entry (vcpu,0x7 ,0 );bool invpcid_enabled = - best && best->ebx &bit (X86_FEATURE_INVPCID ) && +guest_cpuid_has (vcpu,X86_FEATURE_INVPCID ) &&guest_cpuid_has (vcpu,X86_FEATURE_PCID );if (!invpcid_enabled) { secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID ; -if (best) - best->ebx &= ~bit (X86_FEATURE_INVPCID ); +guest_cpuid_clear (vcpu,X86_FEATURE_INVPCID ); }if (nested) {
arch/x86/kvm/x86.c +2 -12
@@ -1022,21 +1022,11 @@ bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)if (efer & efer_reserved_bits)return false; -if (efer &EFER_FFXSR ) { -struct kvm_cpuid_entry2 *feat; - - feat =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -if (!feat || !(feat->edx &bit (X86_FEATURE_FXSR_OPT ))) +if (efer &EFER_FFXSR && !guest_cpuid_has (vcpu,X86_FEATURE_FXSR_OPT ))return false; - } -if (efer &EFER_SVME ) { -struct kvm_cpuid_entry2 *feat; - - feat =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -if (!feat || !(feat->ecx &bit (X86_FEATURE_SVM ))) +if (efer &EFER_SVME && !guest_cpuid_has (vcpu,X86_FEATURE_SVM ))return false; - }return true; }
Signature · 1 line
2.13.3
Re: [PATCH v2 1/3] KVM: x86: X86_FEATURE_NRIPS is not scattered anymore
In reply to Radim KrčmářQuoted 33 lines · Radim Krčmář
bit(X86_FEATURE_NRIPS)is 3 since2ccd71f1b278("x86/cpufeature: Move some of the scattered feature bits tox86_capability"), so we can simplify the code.Signed-off-by Radim Krčmář <[email protected]>---
v2: new, explain why it's ok to useX86_FEATURE_NRIPS(David)--- arch/x86/kvm/cpuid.h | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)arch/x86/kvm/cpuid.h +0
-8@@ -160,25 +160,13 @@ static inline bool guest_cpuid_has_rdtscp(struct kvm_vcpu *vcpu)return best && (best->edx &bit (X86_FEATURE_RDTSCP )); } -/* -* NRIPS is provided through cpuidfn 0x8000000a.edx bit 3 -*/ -#define BIT_NRIPS 3 -static inline bool guest_cpuid_has_nrips (struct kvm_vcpu *vcpu) {struct kvm_cpuid_entry2 *best; best =kvm_find_cpuid_entry (vcpu,0x8000000a ,0 ); - -/* -* NRIPS is a scattered cpuid feature, so we can't use
arch/x86/kvm/cpuid.h static inline bool guest_cpuid_has_rdtscp(struct kvm_vcpu *vcpu) · line 172
Quoted 10 lines
arch/x86/kvm/cpuid.h +1
-5-* X86_FEATURE_NRIPS here (X86_FEATURE_NRIPS would be bit -* position 8, not 3). -*/ -return best && (best->edx &bit (BIT_NRIPS )); +return best && (best->edx &bit (X86_FEATURE_NRIPS )); } -#undef BIT_NRIPSstatic inline int guest_cpuid_family (struct kvm_vcpu *vcpu) {
Signature · 3 lines
Thanks, David
Re: [PATCH v2 2/3] KVM: x86: generalize guest_cpuid_has_ helpers
In reply to Radim KrčmářQuoted 15 lines · Radim Krčmář
This patch turns
guest_cpuid_has_XYZ(cpuid)intoguest_cpuid_has(cpuid, X86_FEATURE_XYZ), which gets rid of many very similar helpers.When seeing a X86_FEATURE_*, we can know which cpuid it belongs to, but this information isn't in common code, so we recreate it for KVM.
Add some
BUILD_BUG_ONsto make sure that it runs nicely.Signed-off-by Radim Krčmář <[email protected]>---
v2:
- fixedX86_FEATURE_RDTSCP(David)
- renamed best to entry (David)
- added const (David)
- added__always_inline(Paolo)
Quoted 9 lines
--- arch/x86/kvm/cpuid.h | 170 +++++++++++++++++---------------------------------- arch/x86/kvm/mmu.c | 7 ++- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm.c | 2 +- arch/x86/kvm/vmx.c | 26 ++++---- arch/x86/kvm/x86.c | 38 ++++++------ 6 files changed, 95 insertions(+), 150 deletions(-)
Signature · 3 lines
Thanks, David
Re: [PATCH v2 3/3] KVM: x86: use general helpers for some cpuid manipulation
In reply to Radim KrčmářQuoted 18 lines · Radim Krčmář
Add
guest_cpuid_clear()and use it instead ofkvm_find_cpuid_entry(). Also replace some uses ofkvm_find_cpuid_entry()withguest_cpuid_has().Signed-off-by Radim Krčmář <[email protected]>---
v2: added__always_inline(Paolo)--- arch/x86/kvm/cpuid.h | 9 +++++++++ arch/x86/kvm/svm.c | 5 +---- arch/x86/kvm/vmx.c | 6 ++---- arch/x86/kvm/x86.c | 14 ++------------ 4 files changed, 14 insertions(+), 20 deletions(-)arch/x86/kvm/cpuid.h +0
-0
arch/x86/kvm/cpuid.h static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_ · line 109
Quoted 6 lines
arch/x86/kvm/cpuid.h +3
-0@@ -104,6 +104,15 @@ static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_return *reg &bit (x86_feature); } +static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, unsigned x86_feature) +{ +int *reg;
you could instantiate it directly.
Quoted 41 lines
arch/x86/kvm/cpuid.h +6
-0+ + reg =guest_cpuid_get_register (vcpu, x86_feature); +if (reg) + *reg &= ~bit (x86_feature); +} +static inline bool guest_cpuid_is_amd (struct kvm_vcpu *vcpu) {struct kvm_cpuid_entry2 *best;arch/x86/kvm/svm.c +1
-4@@ -5075,7 +5075,6 @@ static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)static void svm_cpuid_update (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_cpuid_entry2 *entry;/* Update nrips enabled cache */ svm->nrips_enabled = !!guest_cpuid_has (&svm->vcpu,X86_FEATURE_NRIPS );@@ -5083,9 +5082,7 @@ static void svm_cpuid_update(struct kvm_vcpu *vcpu)if (!kvm_vcpu_apicv_active (vcpu))return ; - entry =kvm_find_cpuid_entry (vcpu,1 ,0 ); -if (entry) - entry->ecx &= ~bit (X86_FEATURE_X2APIC ); +guest_cpuid_clear (vcpu,X86_FEATURE_X2APIC ); }static void svm_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry)arch/x86/kvm/vmx.c +0
-1@@ -9472,15 +9472,13 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (vmx_invpcid_supported ()) {/* Exposing INVPCID only when PCID is exposed */ -struct kvm_cpuid_entry2 *best =kvm_find_cpuid_entry (vcpu,0x7 ,0 );
arch/x86/kvm/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 9481
Quoted 10 lines
arch/x86/kvm/vmx.c +2
-3bool invpcid_enabled = - best && best->ebx &bit (X86_FEATURE_INVPCID ) && +guest_cpuid_has (vcpu,X86_FEATURE_INVPCID ) &&guest_cpuid_has (vcpu,X86_FEATURE_PCID );if (!invpcid_enabled) { secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID ; -if (best) - best->ebx &= ~bit (X86_FEATURE_INVPCID ); +guest_cpuid_clear (vcpu,X86_FEATURE_INVPCID );
just how I wanted it :)
Quoted 22 lines
arch/x86/kvm/vmx.c +0
-0}if (nested) {arch/x86/kvm/x86.c +1
-7@@ -1022,21 +1022,11 @@ bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)if (efer & efer_reserved_bits)return false; -if (efer &EFER_FFXSR ) { -struct kvm_cpuid_entry2 *feat; - - feat =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -if (!feat || !(feat->edx &bit (X86_FEATURE_FXSR_OPT ))) +if (efer &EFER_FFXSR && !guest_cpuid_has (vcpu,X86_FEATURE_FXSR_OPT ))return false; - } -if (efer &EFER_SVME ) {
arch/x86/kvm/x86.c bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) · line 1032
Quoted 10 lines
arch/x86/kvm/x86.c +1
-5-struct kvm_cpuid_entry2 *feat; - - feat =kvm_find_cpuid_entry (vcpu,0x80000001 ,0 ); -if (!feat || !(feat->ecx &bit (X86_FEATURE_SVM ))) +if (efer &EFER_SVME && !guest_cpuid_has (vcpu,X86_FEATURE_SVM ))return false; - }return true; }
Very nice cleanup.
Signature · 3 lines
Thanks, David
Quoted 10 lines · Radim Krčmář
RFC v1: https://www.spinics.net/lists/kvm/msg153753.html
x86_64and i386 compiles for me, but let's see what build bots think of it.Radim Krčmář (3):
KVM: x86:X86_FEATURE_NRIPSis not scattered anymore
KVM: x86: generalizeguest_cpuid_has_helpers
KVM: x86: use general helpers for some cpuid manipulation
Quoted 8 lines
arch/x86/kvm/cpuid.h | 183 +++++++++++++++++---------------------------------- arch/x86/kvm/mmu.c | 7 +- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm.c | 7 +- arch/x86/kvm/vmx.c | 32 ++++----- arch/x86/kvm/x86.c | 52 +++++++-------- 6 files changed, 105 insertions(+), 178 deletions(-)
Queued, thanks.
Paolo