1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021, Red Hat Inc.
5 * Generic tests for KVM CPUID set/get ioctls
7 #include <asm/kvm_para.h>
8 #include <linux/kvm_para.h>
11 #include "test_util.h"
13 #include "processor.h"
17 /* CPUIDs known to differ */
21 } mangled_cpuids[] = {
22 {.function = 0xd, .index = 0},
25 static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
28 u32 eax, ebx, ecx, edx;
30 for (i = 0; i < guest_cpuid->nent; i++) {
31 eax = guest_cpuid->entries[i].function;
32 ecx = guest_cpuid->entries[i].index;
34 cpuid(&eax, &ebx, &ecx, &edx);
36 GUEST_ASSERT(eax == guest_cpuid->entries[i].eax &&
37 ebx == guest_cpuid->entries[i].ebx &&
38 ecx == guest_cpuid->entries[i].ecx &&
39 edx == guest_cpuid->entries[i].edx);
44 static void test_cpuid_40000000(struct kvm_cpuid2 *guest_cpuid)
46 u32 eax = 0x40000000, ebx, ecx = 0, edx;
48 cpuid(&eax, &ebx, &ecx, &edx);
50 GUEST_ASSERT(eax == 0x40000001);
53 static void guest_main(struct kvm_cpuid2 *guest_cpuid)
57 test_guest_cpuids(guest_cpuid);
61 test_cpuid_40000000(guest_cpuid);
66 static bool is_cpuid_mangled(struct kvm_cpuid_entry2 *entrie)
70 for (i = 0; i < sizeof(mangled_cpuids); i++) {
71 if (mangled_cpuids[i].function == entrie->function &&
72 mangled_cpuids[i].index == entrie->index)
79 static void check_cpuid(struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 *entrie)
83 for (i = 0; i < cpuid->nent; i++) {
84 if (cpuid->entries[i].function == entrie->function &&
85 cpuid->entries[i].index == entrie->index) {
86 if (is_cpuid_mangled(entrie))
89 TEST_ASSERT(cpuid->entries[i].eax == entrie->eax &&
90 cpuid->entries[i].ebx == entrie->ebx &&
91 cpuid->entries[i].ecx == entrie->ecx &&
92 cpuid->entries[i].edx == entrie->edx,
93 "CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x",
94 entrie->function, entrie->index,
95 cpuid->entries[i].eax, cpuid->entries[i].ebx,
96 cpuid->entries[i].ecx, cpuid->entries[i].edx,
97 entrie->eax, entrie->ebx, entrie->ecx, entrie->edx);
102 TEST_ASSERT(false, "CPUID 0x%x.%x not found", entrie->function, entrie->index);
105 static void compare_cpuids(struct kvm_cpuid2 *cpuid1, struct kvm_cpuid2 *cpuid2)
109 for (i = 0; i < cpuid1->nent; i++)
110 check_cpuid(cpuid2, &cpuid1->entries[i]);
112 for (i = 0; i < cpuid2->nent; i++)
113 check_cpuid(cpuid1, &cpuid2->entries[i]);
116 static void run_vcpu(struct kvm_vm *vm, uint32_t vcpuid, int stage)
120 _vcpu_run(vm, vcpuid);
122 switch (get_ucall(vm, vcpuid, &uc)) {
124 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
125 uc.args[1] == stage + 1,
126 "Stage %d: Unexpected register values vmexit, got %lx",
127 stage + 1, (ulong)uc.args[1]);
132 TEST_ASSERT(false, "%s at %s:%ld\n\tvalues: %#lx, %#lx", (const char *)uc.args[0],
133 __FILE__, uc.args[1], uc.args[2], uc.args[3]);
135 TEST_ASSERT(false, "Unexpected exit: %s",
136 exit_reason_str(vcpu_state(vm, vcpuid)->exit_reason));
140 struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, struct kvm_cpuid2 *cpuid)
142 int size = sizeof(*cpuid) + cpuid->nent * sizeof(cpuid->entries[0]);
143 vm_vaddr_t gva = vm_vaddr_alloc(vm, size,
144 getpagesize(), 0, 0);
145 struct kvm_cpuid2 *guest_cpuids = addr_gva2hva(vm, gva);
147 memcpy(guest_cpuids, cpuid, size);
155 struct kvm_cpuid2 *supp_cpuid, *cpuid2;
156 vm_vaddr_t cpuid_gva;
160 vm = vm_create_default(VCPU_ID, 0, guest_main);
162 supp_cpuid = kvm_get_supported_cpuid();
163 cpuid2 = vcpu_get_cpuid(vm, VCPU_ID);
165 compare_cpuids(supp_cpuid, cpuid2);
167 vcpu_alloc_cpuid(vm, &cpuid_gva, cpuid2);
169 vcpu_args_set(vm, VCPU_ID, 1, cpuid_gva);
171 for (stage = 0; stage < 3; stage++)
172 run_vcpu(vm, VCPU_ID, stage);