Path: blob/master/tools/testing/selftests/kvm/x86/cpuid_test.c
38245 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2021, Red Hat Inc.3*4* Generic tests for KVM CPUID set/get ioctls5*/6#include <asm/kvm_para.h>7#include <linux/kvm_para.h>8#include <stdint.h>910#include "test_util.h"11#include "kvm_util.h"12#include "processor.h"1314struct cpuid_mask {15union {16struct {17u32 eax;18u32 ebx;19u32 ecx;20u32 edx;21};22u32 regs[4];23};24};2526static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)27{28int i;29u32 eax, ebx, ecx, edx;3031for (i = 0; i < guest_cpuid->nent; i++) {32__cpuid(guest_cpuid->entries[i].function,33guest_cpuid->entries[i].index,34&eax, &ebx, &ecx, &edx);3536GUEST_ASSERT_EQ(eax, guest_cpuid->entries[i].eax);37GUEST_ASSERT_EQ(ebx, guest_cpuid->entries[i].ebx);38GUEST_ASSERT_EQ(ecx, guest_cpuid->entries[i].ecx);39GUEST_ASSERT_EQ(edx, guest_cpuid->entries[i].edx);40}4142}4344static void guest_main(struct kvm_cpuid2 *guest_cpuid)45{46GUEST_SYNC(1);4748test_guest_cpuids(guest_cpuid);4950GUEST_SYNC(2);5152GUEST_ASSERT_EQ(this_cpu_property(X86_PROPERTY_MAX_KVM_LEAF), 0x40000001);5354GUEST_DONE();55}5657static struct cpuid_mask get_const_cpuid_mask(const struct kvm_cpuid_entry2 *entry)58{59struct cpuid_mask mask;6061memset(&mask, 0xff, sizeof(mask));6263switch (entry->function) {64case 0x1:65mask.regs[X86_FEATURE_OSXSAVE.reg] &= ~BIT(X86_FEATURE_OSXSAVE.bit);66break;67case 0x7:68mask.regs[X86_FEATURE_OSPKE.reg] &= ~BIT(X86_FEATURE_OSPKE.bit);69break;70case 0xd:71/*72* CPUID.0xD.{0,1}.EBX enumerate XSAVE size based on the current73* XCR0 and IA32_XSS MSR values.74*/75if (entry->index < 2)76mask.ebx = 0;77break;78}79return mask;80}8182static void compare_cpuids(const struct kvm_cpuid2 *cpuid1,83const struct kvm_cpuid2 *cpuid2)84{85const struct kvm_cpuid_entry2 *e1, *e2;86int i;8788TEST_ASSERT(cpuid1->nent == cpuid2->nent,89"CPUID nent mismatch: %d vs. %d", cpuid1->nent, cpuid2->nent);9091for (i = 0; i < cpuid1->nent; i++) {92struct cpuid_mask mask;9394e1 = &cpuid1->entries[i];95e2 = &cpuid2->entries[i];9697TEST_ASSERT(e1->function == e2->function &&98e1->index == e2->index && e1->flags == e2->flags,99"CPUID entries[%d] mismtach: 0x%x.%d.%x vs. 0x%x.%d.%x",100i, e1->function, e1->index, e1->flags,101e2->function, e2->index, e2->flags);102103/* Mask off dynamic bits, e.g. OSXSAVE, when comparing entries. */104mask = get_const_cpuid_mask(e1);105106TEST_ASSERT((e1->eax & mask.eax) == (e2->eax & mask.eax) &&107(e1->ebx & mask.ebx) == (e2->ebx & mask.ebx) &&108(e1->ecx & mask.ecx) == (e2->ecx & mask.ecx) &&109(e1->edx & mask.edx) == (e2->edx & mask.edx),110"CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x",111e1->function, e1->index,112e1->eax & mask.eax, e1->ebx & mask.ebx,113e1->ecx & mask.ecx, e1->edx & mask.edx,114e2->eax & mask.eax, e2->ebx & mask.ebx,115e2->ecx & mask.ecx, e2->edx & mask.edx);116}117}118119static void run_vcpu(struct kvm_vcpu *vcpu, int stage)120{121struct ucall uc;122123vcpu_run(vcpu);124125switch (get_ucall(vcpu, &uc)) {126case UCALL_SYNC:127TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&128uc.args[1] == stage + 1,129"Stage %d: Unexpected register values vmexit, got %lx",130stage + 1, (ulong)uc.args[1]);131return;132case UCALL_DONE:133return;134case UCALL_ABORT:135REPORT_GUEST_ASSERT(uc);136default:137TEST_ASSERT(false, "Unexpected exit: %s",138exit_reason_str(vcpu->run->exit_reason));139}140}141142struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct kvm_cpuid2 *cpuid)143{144int size = sizeof(*cpuid) + cpuid->nent * sizeof(cpuid->entries[0]);145vm_vaddr_t gva = vm_vaddr_alloc(vm, size, KVM_UTIL_MIN_VADDR);146struct kvm_cpuid2 *guest_cpuids = addr_gva2hva(vm, gva);147148memcpy(guest_cpuids, cpuid, size);149150*p_gva = gva;151return guest_cpuids;152}153154static void set_cpuid_after_run(struct kvm_vcpu *vcpu)155{156struct kvm_cpuid_entry2 *ent;157int rc;158u32 eax, ebx, x;159160/* Setting unmodified CPUID is allowed */161rc = __vcpu_set_cpuid(vcpu);162TEST_ASSERT(!rc, "Setting unmodified CPUID after KVM_RUN failed: %d", rc);163164/* Changing CPU features is forbidden */165ent = vcpu_get_cpuid_entry(vcpu, 0x7);166ebx = ent->ebx;167ent->ebx--;168rc = __vcpu_set_cpuid(vcpu);169TEST_ASSERT(rc, "Changing CPU features should fail");170ent->ebx = ebx;171172/* Changing MAXPHYADDR is forbidden */173ent = vcpu_get_cpuid_entry(vcpu, 0x80000008);174eax = ent->eax;175x = eax & 0xff;176ent->eax = (eax & ~0xffu) | (x - 1);177rc = __vcpu_set_cpuid(vcpu);178TEST_ASSERT(rc, "Changing MAXPHYADDR should fail");179ent->eax = eax;180}181182static void test_get_cpuid2(struct kvm_vcpu *vcpu)183{184struct kvm_cpuid2 *cpuid = allocate_kvm_cpuid2(vcpu->cpuid->nent + 1);185int i, r;186187vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid);188TEST_ASSERT(cpuid->nent == vcpu->cpuid->nent,189"KVM didn't update nent on success, wanted %u, got %u",190vcpu->cpuid->nent, cpuid->nent);191192for (i = 0; i < vcpu->cpuid->nent; i++) {193cpuid->nent = i;194r = __vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid);195TEST_ASSERT(r && errno == E2BIG, KVM_IOCTL_ERROR(KVM_GET_CPUID2, r));196TEST_ASSERT(cpuid->nent == i, "KVM modified nent on failure");197}198free(cpuid);199}200201int main(void)202{203struct kvm_vcpu *vcpu;204vm_vaddr_t cpuid_gva;205struct kvm_vm *vm;206int stage;207208vm = vm_create_with_one_vcpu(&vcpu, guest_main);209210compare_cpuids(kvm_get_supported_cpuid(), vcpu->cpuid);211212vcpu_alloc_cpuid(vm, &cpuid_gva, vcpu->cpuid);213214vcpu_args_set(vcpu, 1, cpuid_gva);215216for (stage = 0; stage < 3; stage++)217run_vcpu(vcpu, stage);218219set_cpuid_after_run(vcpu);220221test_get_cpuid2(vcpu);222223kvm_vm_free(vm);224}225226227