Commit aa10a7dc authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: x86: Consolidate CPUID array max num entries checking

Move the nent vs. maxnent check and nent increment into do_host_cpuid()
to consolidate what is now identical code.  To signal success vs.
failure, return the entry and NULL respectively.  A future patch will
build on this to also move the entry retrieval into do_host_cpuid().

No functional change intended.
Reviewed-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: default avatarSean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent acfad336
...@@ -287,9 +287,14 @@ static __always_inline void cpuid_mask(u32 *word, int wordnum) ...@@ -287,9 +287,14 @@ static __always_inline void cpuid_mask(u32 *word, int wordnum)
*word &= boot_cpu_data.x86_capability[wordnum]; *word &= boot_cpu_data.x86_capability[wordnum];
} }
static void do_host_cpuid(struct kvm_cpuid_entry2 *entry, u32 function, static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_entry2 *entry,
u32 index) int *nent, int maxnent,
u32 function, u32 index)
{ {
if (*nent >= maxnent)
return NULL;
++*nent;
entry->function = function; entry->function = function;
entry->index = index; entry->index = index;
entry->flags = 0; entry->flags = 0;
...@@ -316,6 +321,8 @@ static void do_host_cpuid(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -316,6 +321,8 @@ static void do_host_cpuid(struct kvm_cpuid_entry2 *entry, u32 function,
entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
break; break;
} }
return entry;
} }
static int __do_cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, static int __do_cpuid_func_emulated(struct kvm_cpuid_entry2 *entry,
...@@ -507,12 +514,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -507,12 +514,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
r = -E2BIG; r = -E2BIG;
if (WARN_ON(*nent >= maxnent)) if (WARN_ON(!do_host_cpuid(entry, nent, maxnent, function, 0)))
goto out; goto out;
do_host_cpuid(entry, function, 0);
++*nent;
switch (function) { switch (function) {
case 0: case 0:
/* Limited to the highest leaf implemented in KVM. */ /* Limited to the highest leaf implemented in KVM. */
...@@ -536,11 +540,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -536,11 +540,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT; entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
for (t = 1; t < times; ++t) { for (t = 1; t < times; ++t) {
if (*nent >= maxnent) if (!do_host_cpuid(&entry[t], nent, maxnent, function, 0))
goto out; goto out;
do_host_cpuid(&entry[t], function, 0);
++*nent;
} }
break; break;
} }
...@@ -555,10 +556,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -555,10 +556,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
if (!cache_type) if (!cache_type)
break; break;
if (*nent >= maxnent) if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
goto out; goto out;
do_host_cpuid(&entry[i], function, i);
++*nent;
} }
break; break;
} }
...@@ -575,12 +574,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -575,12 +574,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
do_cpuid_7_mask(entry); do_cpuid_7_mask(entry);
for (i = 1; i <= entry->eax; i++) { for (i = 1; i <= entry->eax; i++) {
if (*nent >= maxnent) if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
goto out; goto out;
do_host_cpuid(&entry[i], function, i);
++*nent;
do_cpuid_7_mask(&entry[i]); do_cpuid_7_mask(&entry[i]);
} }
break; break;
...@@ -633,11 +629,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -633,11 +629,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
* added entry is zero. * added entry is zero.
*/ */
for (i = 1; entry[i - 1].ecx & 0xff00; ++i) { for (i = 1; entry[i - 1].ecx & 0xff00; ++i) {
if (*nent >= maxnent) if (!do_host_cpuid(&entry[i], nent, maxnent, function, i))
goto out; goto out;
do_host_cpuid(&entry[i], function, i);
++*nent;
} }
break; break;
} }
...@@ -652,12 +645,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -652,12 +645,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
if (!supported) if (!supported)
break; break;
if (*nent >= maxnent) if (!do_host_cpuid(&entry[1], nent, maxnent, function, 1))
goto out; goto out;
do_host_cpuid(&entry[1], function, 1);
++*nent;
entry[1].eax &= kvm_cpuid_D_1_eax_x86_features; entry[1].eax &= kvm_cpuid_D_1_eax_x86_features;
cpuid_mask(&entry[1].eax, CPUID_D_1_EAX); cpuid_mask(&entry[1].eax, CPUID_D_1_EAX);
if (entry[1].eax & (F(XSAVES)|F(XSAVEC))) if (entry[1].eax & (F(XSAVES)|F(XSAVEC)))
...@@ -672,12 +662,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -672,12 +662,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
if (!(supported & BIT_ULL(idx))) if (!(supported & BIT_ULL(idx)))
continue; continue;
if (*nent >= maxnent) if (!do_host_cpuid(&entry[i], nent, maxnent, function, idx))
goto out; goto out;
do_host_cpuid(&entry[i], function, idx);
++*nent;
/* /*
* The @supported check above should have filtered out * The @supported check above should have filtered out
* invalid sub-leafs as well as sub-leafs managed by * invalid sub-leafs as well as sub-leafs managed by
...@@ -704,10 +691,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function, ...@@ -704,10 +691,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_entry2 *entry, u32 function,
break; break;
for (t = 1; t <= times; ++t) { for (t = 1; t <= times; ++t) {
if (*nent >= maxnent) if (!do_host_cpuid(&entry[t], nent, maxnent, function, t))
goto out; goto out;
do_host_cpuid(&entry[t], function, t);
++*nent;
} }
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment