Commit 8fe09d6a authored by Sean Christopherson's avatar Sean Christopherson

KVM: selftests: Set input function/index in raw CPUID helper(s)

Set the function/index for CPUID in the helper instead of relying on the
caller to do so.  In addition to reducing the risk of consuming an
uninitialized ECX, having the function/index embedded in the call makes
it easier to understand what is being checked.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20220614200707.3315957-32-seanjc@google.com
parent 813e38cd
...@@ -404,10 +404,13 @@ static inline void outl(uint16_t port, uint32_t value) ...@@ -404,10 +404,13 @@ static inline void outl(uint16_t port, uint32_t value)
__asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value)); __asm__ __volatile__("outl %%eax, %%dx" : : "d"(port), "a"(value));
} }
static inline void cpuid(uint32_t *eax, uint32_t *ebx, static inline void __cpuid(uint32_t function, uint32_t index,
uint32_t *eax, uint32_t *ebx,
uint32_t *ecx, uint32_t *edx) uint32_t *ecx, uint32_t *edx)
{ {
/* ecx is often an input as well as an output. */ *eax = function;
*ecx = index;
asm volatile("cpuid" asm volatile("cpuid"
: "=a" (*eax), : "=a" (*eax),
"=b" (*ebx), "=b" (*ebx),
...@@ -417,6 +420,13 @@ static inline void cpuid(uint32_t *eax, uint32_t *ebx, ...@@ -417,6 +420,13 @@ static inline void cpuid(uint32_t *eax, uint32_t *ebx,
: "memory"); : "memory");
} }
static inline void cpuid(uint32_t function,
uint32_t *eax, uint32_t *ebx,
uint32_t *ecx, uint32_t *edx)
{
return __cpuid(function, 0, eax, ebx, ecx, edx);
}
#define SET_XMM(__var, __xmm) \ #define SET_XMM(__var, __xmm) \
asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm) asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
......
...@@ -1301,9 +1301,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm) ...@@ -1301,9 +1301,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
/* Before family 17h, the HyperTransport area is just below 1T. */ /* Before family 17h, the HyperTransport area is just below 1T. */
ht_gfn = (1 << 28) - num_ht_pages; ht_gfn = (1 << 28) - num_ht_pages;
eax = 1; cpuid(1, &eax, &ebx, &ecx, &edx);
ecx = 0;
cpuid(&eax, &ebx, &ecx, &edx);
if (x86_family(eax) < 0x17) if (x86_family(eax) < 0x17)
goto done; goto done;
...@@ -1312,18 +1310,15 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm) ...@@ -1312,18 +1310,15 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
* reduced due to SME by bits 11:6 of CPUID[0x8000001f].EBX. Use * reduced due to SME by bits 11:6 of CPUID[0x8000001f].EBX. Use
* the old conservative value if MAXPHYADDR is not enumerated. * the old conservative value if MAXPHYADDR is not enumerated.
*/ */
eax = 0x80000000; cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
cpuid(&eax, &ebx, &ecx, &edx);
max_ext_leaf = eax; max_ext_leaf = eax;
if (max_ext_leaf < 0x80000008) if (max_ext_leaf < 0x80000008)
goto done; goto done;
eax = 0x80000008; cpuid(0x80000008, &eax, &ebx, &ecx, &edx);
cpuid(&eax, &ebx, &ecx, &edx);
max_pfn = (1ULL << ((eax & 0xff) - vm->page_shift)) - 1; max_pfn = (1ULL << ((eax & 0xff) - vm->page_shift)) - 1;
if (max_ext_leaf >= 0x8000001f) { if (max_ext_leaf >= 0x8000001f) {
eax = 0x8000001f; cpuid(0x8000001f, &eax, &ebx, &ecx, &edx);
cpuid(&eax, &ebx, &ecx, &edx);
max_pfn >>= (ebx >> 6) & 0x3f; max_pfn >>= (ebx >> 6) & 0x3f;
} }
......
...@@ -122,9 +122,7 @@ static inline void check_cpuid_xsave(void) ...@@ -122,9 +122,7 @@ static inline void check_cpuid_xsave(void)
{ {
uint32_t eax, ebx, ecx, edx; uint32_t eax, ebx, ecx, edx;
eax = 1; cpuid(1, &eax, &ebx, &ecx, &edx);
ecx = 0;
cpuid(&eax, &ebx, &ecx, &edx);
if (!(ecx & CPUID_XSAVE)) if (!(ecx & CPUID_XSAVE))
GUEST_ASSERT(!"cpuid: no CPU xsave support!"); GUEST_ASSERT(!"cpuid: no CPU xsave support!");
if (!(ecx & CPUID_OSXSAVE)) if (!(ecx & CPUID_OSXSAVE))
...@@ -140,10 +138,7 @@ static bool enum_xtile_config(void) ...@@ -140,10 +138,7 @@ static bool enum_xtile_config(void)
{ {
u32 eax, ebx, ecx, edx; u32 eax, ebx, ecx, edx;
eax = TILE_CPUID; __cpuid(TILE_CPUID, TILE_PALETTE_CPUID_SUBLEAVE, &eax, &ebx, &ecx, &edx);
ecx = TILE_PALETTE_CPUID_SUBLEAVE;
cpuid(&eax, &ebx, &ecx, &edx);
if (!eax || !ebx || !ecx) if (!eax || !ebx || !ecx)
return false; return false;
...@@ -165,10 +160,7 @@ static bool enum_xsave_tile(void) ...@@ -165,10 +160,7 @@ static bool enum_xsave_tile(void)
{ {
u32 eax, ebx, ecx, edx; u32 eax, ebx, ecx, edx;
eax = XSTATE_CPUID; __cpuid(XSTATE_CPUID, XFEATURE_XTILEDATA, &eax, &ebx, &ecx, &edx);
ecx = XFEATURE_XTILEDATA;
cpuid(&eax, &ebx, &ecx, &edx);
if (!eax || !ebx) if (!eax || !ebx)
return false; return false;
...@@ -183,10 +175,7 @@ static bool check_xsave_size(void) ...@@ -183,10 +175,7 @@ static bool check_xsave_size(void)
u32 eax, ebx, ecx, edx; u32 eax, ebx, ecx, edx;
bool valid = false; bool valid = false;
eax = XSTATE_CPUID; __cpuid(XSTATE_CPUID, XSTATE_USER_STATE_SUBLEAVE, &eax, &ebx, &ecx, &edx);
ecx = XSTATE_USER_STATE_SUBLEAVE;
cpuid(&eax, &ebx, &ecx, &edx);
if (ebx && ebx <= XSAVE_SIZE) if (ebx && ebx <= XSAVE_SIZE)
valid = true; valid = true;
......
...@@ -31,10 +31,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid) ...@@ -31,10 +31,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
u32 eax, ebx, ecx, edx; u32 eax, ebx, ecx, edx;
for (i = 0; i < guest_cpuid->nent; i++) { for (i = 0; i < guest_cpuid->nent; i++) {
eax = guest_cpuid->entries[i].function; __cpuid(guest_cpuid->entries[i].function,
ecx = guest_cpuid->entries[i].index; guest_cpuid->entries[i].index,
&eax, &ebx, &ecx, &edx);
cpuid(&eax, &ebx, &ecx, &edx);
GUEST_ASSERT(eax == guest_cpuid->entries[i].eax && GUEST_ASSERT(eax == guest_cpuid->entries[i].eax &&
ebx == guest_cpuid->entries[i].ebx && ebx == guest_cpuid->entries[i].ebx &&
...@@ -46,9 +45,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid) ...@@ -46,9 +45,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
static void test_cpuid_40000000(struct kvm_cpuid2 *guest_cpuid) static void test_cpuid_40000000(struct kvm_cpuid2 *guest_cpuid)
{ {
u32 eax = 0x40000000, ebx, ecx = 0, edx; u32 eax, ebx, ecx, edx;
cpuid(&eax, &ebx, &ecx, &edx); cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
GUEST_ASSERT(eax == 0x40000001); GUEST_ASSERT(eax == 0x40000001);
} }
......
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