Skip to content

Commit 832ffb3

Browse files
vishals4ghsean-jc
authored andcommitted
KVM: selftests: x86: Cache host CPU vendor (AMD vs. Intel)
Cache the host CPU vendor for userspace and share it with guest code. All the current callers of this_cpu* actually care about host cpu so they are updated to check host_cpu_is*. 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-3-vannapurve@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 093db78 commit 832ffb3

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "../kvm_util.h"
2121

22+
extern bool host_cpu_is_intel;
23+
extern bool host_cpu_is_amd;
24+
2225
#define NMI_VECTOR 0x02
2326

2427
#define X86_EFLAGS_FIXED (1u << 1)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define MAX_NR_CPUID_ENTRIES 100
2020

2121
vm_vaddr_t exception_handlers;
22+
bool host_cpu_is_amd;
23+
bool host_cpu_is_intel;
2224

2325
static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
2426
{
@@ -113,7 +115,7 @@ static void sregs_dump(FILE *stream, struct kvm_sregs *sregs, uint8_t indent)
113115

114116
bool kvm_is_tdp_enabled(void)
115117
{
116-
if (this_cpu_is_intel())
118+
if (host_cpu_is_intel)
117119
return get_kvm_intel_param_bool("ept");
118120
else
119121
return get_kvm_amd_param_bool("npt");
@@ -555,6 +557,8 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
555557
void kvm_arch_vm_post_create(struct kvm_vm *vm)
556558
{
557559
vm_create_irqchip(vm);
560+
sync_global_to_guest(vm, host_cpu_is_intel);
561+
sync_global_to_guest(vm, host_cpu_is_amd);
558562
}
559563

560564
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
@@ -1214,7 +1218,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12141218
max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
12151219

12161220
/* Avoid reserved HyperTransport region on AMD processors. */
1217-
if (!this_cpu_is_amd())
1221+
if (!host_cpu_is_amd)
12181222
return max_gfn;
12191223

12201224
/* On parts with <40 physical address bits, the area is fully hidden */
@@ -1254,3 +1258,9 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)
12541258

12551259
return get_kvm_intel_param_bool("unrestricted_guest");
12561260
}
1261+
1262+
void kvm_selftest_arch_init(void)
1263+
{
1264+
host_cpu_is_intel = this_cpu_is_intel();
1265+
host_cpu_is_amd = this_cpu_is_amd();
1266+
}

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 (this_cpu_is_intel()) {
51+
if (host_cpu_is_intel) {
5252
native_hypercall_insn = vmx_vmcall;
5353
other_hypercall_insn = svm_vmmcall;
54-
} else if (this_cpu_is_amd()) {
54+
} else if (host_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(this_cpu_is_intel());
96+
TEST_REQUIRE(host_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 this_cpu_is_intel() &&
366+
return host_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 this_cpu_is_amd() &&
400+
return host_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(this_cpu_is_intel());
114+
TEST_REQUIRE(host_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)