Skip to content

Commit a656dac

Browse files
committed
KVM: x86: expose cpuid_entry2_find for TDX
CPUID values are provided for TDX virtual machines as part of the KVM_TDX_INIT_VM ioctl. Unlike KVM_SET_CPUID2, TDX will need to examine the leaves, either to validate against the CPUIDs listed in the TDX modules configuration or to fill other controls with matching values. Since there is an existing function to look up a leaf/index pair into a given list of CPUID entries, export it as kvm_find_cpuid_entry2(). Reviewed-by: Kai Huang <kai.huang@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent f94f4a9 commit a656dac

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,8 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted)
8282
return ret;
8383
}
8484

85-
/*
86-
* Magic value used by KVM when querying userspace-provided CPUID entries and
87-
* doesn't care about the CPIUD index because the index of the function in
88-
* question is not significant. Note, this magic value must have at least one
89-
* bit set in bits[63:32] and must be consumed as a u64 by cpuid_entry2_find()
90-
* to avoid false positives when processing guest CPUID input.
91-
*/
92-
#define KVM_CPUID_INDEX_NOT_SIGNIFICANT -1ull
93-
94-
static struct kvm_cpuid_entry2 *cpuid_entry2_find(struct kvm_vcpu *vcpu,
95-
u32 function, u64 index)
85+
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(
86+
struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index)
9687
{
9788
struct kvm_cpuid_entry2 *e;
9889
int i;
@@ -109,8 +100,8 @@ static struct kvm_cpuid_entry2 *cpuid_entry2_find(struct kvm_vcpu *vcpu,
109100
*/
110101
lockdep_assert_irqs_enabled();
111102

112-
for (i = 0; i < vcpu->arch.cpuid_nent; i++) {
113-
e = &vcpu->arch.cpuid_entries[i];
103+
for (i = 0; i < nent; i++) {
104+
e = &entries[i];
114105

115106
if (e->function != function)
116107
continue;
@@ -141,26 +132,7 @@ static struct kvm_cpuid_entry2 *cpuid_entry2_find(struct kvm_vcpu *vcpu,
141132

142133
return NULL;
143134
}
144-
145-
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu,
146-
u32 function, u32 index)
147-
{
148-
return cpuid_entry2_find(vcpu, function, index);
149-
}
150-
EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry_index);
151-
152-
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
153-
u32 function)
154-
{
155-
return cpuid_entry2_find(vcpu, function, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
156-
}
157-
EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
158-
159-
/*
160-
* cpuid_entry2_find() and KVM_CPUID_INDEX_NOT_SIGNIFICANT should never be used
161-
* directly outside of kvm_find_cpuid_entry() and kvm_find_cpuid_entry_index().
162-
*/
163-
#undef KVM_CPUID_INDEX_NOT_SIGNIFICANT
135+
EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry2);
164136

165137
static int kvm_check_cpuid(struct kvm_vcpu *vcpu)
166138
{

arch/x86/kvm/cpuid.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,34 @@ void kvm_set_cpu_caps(void);
1212

1313
void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
1414
void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu);
15-
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu,
16-
u32 function, u32 index);
17-
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
18-
u32 function);
15+
struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *entries,
16+
int nent, u32 function, u64 index);
17+
/*
18+
* Magic value used by KVM when querying userspace-provided CPUID entries and
19+
* doesn't care about the CPIUD index because the index of the function in
20+
* question is not significant. Note, this magic value must have at least one
21+
* bit set in bits[63:32] and must be consumed as a u64 by kvm_find_cpuid_entry2()
22+
* to avoid false positives when processing guest CPUID input.
23+
*
24+
* KVM_CPUID_INDEX_NOT_SIGNIFICANT should never be used directly outside of
25+
* kvm_find_cpuid_entry2() and kvm_find_cpuid_entry().
26+
*/
27+
#define KVM_CPUID_INDEX_NOT_SIGNIFICANT -1ull
28+
29+
static inline struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu,
30+
u32 function, u32 index)
31+
{
32+
return kvm_find_cpuid_entry2(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
33+
function, index);
34+
}
35+
36+
static inline struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
37+
u32 function)
38+
{
39+
return kvm_find_cpuid_entry2(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
40+
function, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
41+
}
42+
1943
int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
2044
struct kvm_cpuid_entry2 __user *entries,
2145
unsigned int type);

0 commit comments

Comments
 (0)