Skip to content

Commit 1b4d56b

Browse files
rkrcmarbonzini
authored andcommitted
KVM: x86: use general helpers for some cpuid manipulation
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(). Signed-off-by: Radim Krčmář <[email protected]> Reviewed-by: David Hildenbrand <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d6321d4 commit 1b4d56b

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

arch/x86/kvm/cpuid.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned x86_
104104
return *reg & bit(x86_feature);
105105
}
106106

107+
static __always_inline void guest_cpuid_clear(struct kvm_vcpu *vcpu, unsigned x86_feature)
108+
{
109+
int *reg;
110+
111+
reg = guest_cpuid_get_register(vcpu, x86_feature);
112+
if (reg)
113+
*reg &= ~bit(x86_feature);
114+
}
115+
107116
static inline bool guest_cpuid_is_amd(struct kvm_vcpu *vcpu)
108117
{
109118
struct kvm_cpuid_entry2 *best;

arch/x86/kvm/svm.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5075,17 +5075,14 @@ static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
50755075
static void svm_cpuid_update(struct kvm_vcpu *vcpu)
50765076
{
50775077
struct vcpu_svm *svm = to_svm(vcpu);
5078-
struct kvm_cpuid_entry2 *entry;
50795078

50805079
/* Update nrips enabled cache */
50815080
svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
50825081

50835082
if (!kvm_vcpu_apicv_active(vcpu))
50845083
return;
50855084

5086-
entry = kvm_find_cpuid_entry(vcpu, 1, 0);
5087-
if (entry)
5088-
entry->ecx &= ~bit(X86_FEATURE_X2APIC);
5085+
guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
50895086
}
50905087

50915088
static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)

arch/x86/kvm/vmx.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9636,15 +9636,13 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
96369636

96379637
if (vmx_invpcid_supported()) {
96389638
/* Exposing INVPCID only when PCID is exposed */
9639-
struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
96409639
bool invpcid_enabled =
9641-
best && best->ebx & bit(X86_FEATURE_INVPCID) &&
9640+
guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
96429641
guest_cpuid_has(vcpu, X86_FEATURE_PCID);
96439642

96449643
if (!invpcid_enabled) {
96459644
secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
9646-
if (best)
9647-
best->ebx &= ~bit(X86_FEATURE_INVPCID);
9645+
guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
96489646
}
96499647

96509648
if (nested) {

arch/x86/kvm/x86.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,21 +1022,11 @@ bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
10221022
if (efer & efer_reserved_bits)
10231023
return false;
10241024

1025-
if (efer & EFER_FFXSR) {
1026-
struct kvm_cpuid_entry2 *feat;
1027-
1028-
feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
1029-
if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
1025+
if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
10301026
return false;
1031-
}
10321027

1033-
if (efer & EFER_SVME) {
1034-
struct kvm_cpuid_entry2 *feat;
1035-
1036-
feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
1037-
if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
1028+
if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
10381029
return false;
1039-
}
10401030

10411031
return true;
10421032
}

0 commit comments

Comments
 (0)