Skip to content

Commit 093db78

Browse files
vishals4ghsean-jc
authored andcommitted
KVM: selftests: x86: Use "this_cpu" prefix for cpu vendor queries
Replace is_intel/amd_cpu helpers with this_cpu_* helpers to better convey the intent of querying vendor of the current cpu. Suggested-by: Sean Christopherson <seanjc@google.com> Reviewed-by: David Matlack <dmatlack@google.com> Signed-off-by: Vishal Annapurve <vannapurve@google.com> Link: https://lore.kernel.org/r/20230111004445.416840-2-vannapurve@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 589713e commit 093db78

File tree

6 files changed

+30
-33
lines changed

6 files changed

+30
-33
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,28 @@ static inline uint32_t this_cpu_model(void)
555555
return x86_model(this_cpu_fms());
556556
}
557557

558+
static inline bool this_cpu_vendor_string_is(const char *vendor)
559+
{
560+
const uint32_t *chunk = (const uint32_t *)vendor;
561+
uint32_t eax, ebx, ecx, edx;
562+
563+
cpuid(0, &eax, &ebx, &ecx, &edx);
564+
return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
565+
}
566+
567+
static inline bool this_cpu_is_intel(void)
568+
{
569+
return this_cpu_vendor_string_is("GenuineIntel");
570+
}
571+
572+
/*
573+
* Exclude early K5 samples with a vendor string of "AMDisbetter!"
574+
*/
575+
static inline bool this_cpu_is_amd(void)
576+
{
577+
return this_cpu_vendor_string_is("AuthenticAMD");
578+
}
579+
558580
static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index,
559581
uint8_t reg, uint8_t lo, uint8_t hi)
560582
{
@@ -691,9 +713,6 @@ static inline void cpu_relax(void)
691713
"hlt\n" \
692714
)
693715

694-
bool is_intel_cpu(void);
695-
bool is_amd_cpu(void);
696-
697716
struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu);
698717
void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state);
699718
void kvm_x86_state_cleanup(struct kvm_x86_state *state);

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent)
113113

114114
bool kvm_is_tdp_enabled(void)
115115
{
116-
if (is_intel_cpu())
116+
if (this_cpu_is_intel())
117117
return get_kvm_intel_param_bool("ept");
118118
else
119119
return get_kvm_amd_param_bool("npt");
@@ -1006,28 +1006,6 @@ void kvm_x86_state_cleanup(struct kvm_x86_state *state)
10061006
free(state);
10071007
}
10081008

1009-
static bool cpu_vendor_string_is(const char *vendor)
1010-
{
1011-
const uint32_t *chunk = (const uint32_t *)vendor;
1012-
uint32_t eax, ebx, ecx, edx;
1013-
1014-
cpuid(0, &eax, &ebx, &ecx, &edx);
1015-
return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
1016-
}
1017-
1018-
bool is_intel_cpu(void)
1019-
{
1020-
return cpu_vendor_string_is("GenuineIntel");
1021-
}
1022-
1023-
/*
1024-
* Exclude early K5 samples with a vendor string of "AMDisbetter!"
1025-
*/
1026-
bool is_amd_cpu(void)
1027-
{
1028-
return cpu_vendor_string_is("AuthenticAMD");
1029-
}
1030-
10311009
void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
10321010
{
10331011
if (!kvm_cpu_has_p(X86_PROPERTY_MAX_PHY_ADDR)) {
@@ -1236,7 +1214,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12361214
max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
12371215

12381216
/* Avoid reserved HyperTransport region on AMD processors. */
1239-
if (!is_amd_cpu())
1217+
if (!this_cpu_is_amd())
12401218
return max_gfn;
12411219

12421220
/* On parts with <40 physical address bits, the area is fully hidden */

tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ static void guest_main(void)
4848
const uint8_t *other_hypercall_insn;
4949
uint64_t ret;
5050

51-
if (is_intel_cpu()) {
51+
if (this_cpu_is_intel()) {
5252
native_hypercall_insn = vmx_vmcall;
5353
other_hypercall_insn = svm_vmmcall;
54-
} else if (is_amd_cpu()) {
54+
} else if (this_cpu_is_amd()) {
5555
native_hypercall_insn = svm_vmmcall;
5656
other_hypercall_insn = vmx_vmcall;
5757
} else {

tools/testing/selftests/kvm/x86_64/mmio_warning_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int main(void)
9393
{
9494
int warnings_before, warnings_after;
9595

96-
TEST_REQUIRE(is_intel_cpu());
96+
TEST_REQUIRE(this_cpu_is_intel());
9797

9898
TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
9999

tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ static void test_pmu_config_disable(void (*guest_code)(void))
363363
*/
364364
static bool use_intel_pmu(void)
365365
{
366-
return is_intel_cpu() &&
366+
return this_cpu_is_intel() &&
367367
kvm_cpu_property(X86_PROPERTY_PMU_VERSION) &&
368368
kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS) &&
369369
kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED);
@@ -397,7 +397,7 @@ static bool use_amd_pmu(void)
397397
uint32_t family = kvm_cpu_family();
398398
uint32_t model = kvm_cpu_model();
399399

400-
return is_amd_cpu() &&
400+
return this_cpu_is_amd() &&
401401
(is_zen1(family, model) ||
402402
is_zen2(family, model) ||
403403
is_zen3(family, model));

tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
111111
struct kvm_vcpu *vcpu;
112112
struct kvm_vm *vm;
113113

114-
TEST_REQUIRE(is_intel_cpu());
114+
TEST_REQUIRE(this_cpu_is_intel());
115115
TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
116116

117117
vm = vm_create_with_one_vcpu(&vcpu, guest_code);

0 commit comments

Comments
 (0)