Skip to content

Commit

Permalink
KVM: selftests: x86: Cache host CPU vendor (AMD vs. Intel)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
vishals4gh authored and sean-jc committed Jan 19, 2023
1 parent 093db78 commit 832ffb3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions tools/testing/selftests/kvm/include/x86_64/processor.h
Expand Up @@ -19,6 +19,9 @@

#include "../kvm_util.h"

extern bool host_cpu_is_intel;
extern bool host_cpu_is_amd;

#define NMI_VECTOR 0x02

#define X86_EFLAGS_FIXED (1u << 1)
Expand Down
14 changes: 12 additions & 2 deletions tools/testing/selftests/kvm/lib/x86_64/processor.c
Expand Up @@ -19,6 +19,8 @@
#define MAX_NR_CPUID_ENTRIES 100

vm_vaddr_t exception_handlers;
bool host_cpu_is_amd;
bool host_cpu_is_intel;

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

bool kvm_is_tdp_enabled(void)
{
if (this_cpu_is_intel())
if (host_cpu_is_intel)
return get_kvm_intel_param_bool("ept");
else
return get_kvm_amd_param_bool("npt");
Expand Down Expand Up @@ -555,6 +557,8 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
void kvm_arch_vm_post_create(struct kvm_vm *vm)
{
vm_create_irqchip(vm);
sync_global_to_guest(vm, host_cpu_is_intel);
sync_global_to_guest(vm, host_cpu_is_amd);
}

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

/* Avoid reserved HyperTransport region on AMD processors. */
if (!this_cpu_is_amd())
if (!host_cpu_is_amd)
return max_gfn;

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

return get_kvm_intel_param_bool("unrestricted_guest");
}

void kvm_selftest_arch_init(void)
{
host_cpu_is_intel = this_cpu_is_intel();
host_cpu_is_amd = this_cpu_is_amd();
}
4 changes: 2 additions & 2 deletions tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c
Expand Up @@ -48,10 +48,10 @@ static void guest_main(void)
const uint8_t *other_hypercall_insn;
uint64_t ret;

if (this_cpu_is_intel()) {
if (host_cpu_is_intel) {
native_hypercall_insn = vmx_vmcall;
other_hypercall_insn = svm_vmmcall;
} else if (this_cpu_is_amd()) {
} else if (host_cpu_is_amd) {
native_hypercall_insn = svm_vmmcall;
other_hypercall_insn = vmx_vmcall;
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/kvm/x86_64/mmio_warning_test.c
Expand Up @@ -93,7 +93,7 @@ int main(void)
{
int warnings_before, warnings_after;

TEST_REQUIRE(this_cpu_is_intel());
TEST_REQUIRE(host_cpu_is_intel);

TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));

Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
Expand Up @@ -363,7 +363,7 @@ static void test_pmu_config_disable(void (*guest_code)(void))
*/
static bool use_intel_pmu(void)
{
return this_cpu_is_intel() &&
return host_cpu_is_intel &&
kvm_cpu_property(X86_PROPERTY_PMU_VERSION) &&
kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS) &&
kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED);
Expand Down Expand Up @@ -397,7 +397,7 @@ static bool use_amd_pmu(void)
uint32_t family = kvm_cpu_family();
uint32_t model = kvm_cpu_model();

return this_cpu_is_amd() &&
return host_cpu_is_amd &&
(is_zen1(family, model) ||
is_zen2(family, model) ||
is_zen3(family, model));
Expand Down
Expand Up @@ -111,7 +111,7 @@ int main(int argc, char *argv[])
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;

TEST_REQUIRE(this_cpu_is_intel());
TEST_REQUIRE(host_cpu_is_intel);
TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));

vm = vm_create_with_one_vcpu(&vcpu, guest_code);
Expand Down

0 comments on commit 832ffb3

Please sign in to comment.