0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define _GNU_SOURCE
0012 #include <fcntl.h>
0013 #include <stdio.h>
0014 #include <stdlib.h>
0015 #include <string.h>
0016 #include <sys/ioctl.h>
0017
0018 #include "test_util.h"
0019 #include "kvm_util.h"
0020 #include "processor.h"
0021 #include "vmx.h"
0022
0023 static void guest_code(void)
0024 {
0025 }
0026
0027 static bool smt_possible(void)
0028 {
0029 char buf[16];
0030 FILE *f;
0031 bool res = true;
0032
0033 f = fopen("/sys/devices/system/cpu/smt/control", "r");
0034 if (f) {
0035 if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
0036 if (!strncmp(buf, "forceoff", 8) ||
0037 !strncmp(buf, "notsupported", 12))
0038 res = false;
0039 }
0040 fclose(f);
0041 }
0042
0043 return res;
0044 }
0045
0046 static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
0047 bool evmcs_expected)
0048 {
0049 int i;
0050 int nent_expected = 10;
0051 u32 test_val;
0052
0053 TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
0054 "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
0055 " (returned %d)",
0056 nent_expected, hv_cpuid_entries->nent);
0057
0058 for (i = 0; i < hv_cpuid_entries->nent; i++) {
0059 const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
0060
0061 TEST_ASSERT((entry->function >= 0x40000000) &&
0062 (entry->function <= 0x40000082),
0063 "function %x is our of supported range",
0064 entry->function);
0065
0066 TEST_ASSERT(entry->index == 0,
0067 ".index field should be zero");
0068
0069 TEST_ASSERT(entry->flags == 0,
0070 ".flags field should be zero");
0071
0072 TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
0073 !entry->padding[2], "padding should be zero");
0074
0075 switch (entry->function) {
0076 case 0x40000000:
0077 test_val = 0x40000082;
0078
0079 TEST_ASSERT(entry->eax == test_val,
0080 "Wrong max leaf report in 0x40000000.EAX: %x"
0081 " (evmcs=%d)",
0082 entry->eax, evmcs_expected
0083 );
0084 break;
0085 case 0x40000004:
0086 test_val = entry->eax & (1UL << 18);
0087
0088 TEST_ASSERT(!!test_val == !smt_possible(),
0089 "NoNonArchitecturalCoreSharing bit"
0090 " doesn't reflect SMT setting");
0091 break;
0092 case 0x4000000A:
0093 TEST_ASSERT(entry->eax & (1UL << 19),
0094 "Enlightened MSR-Bitmap should always be supported"
0095 " 0x40000000.EAX: %x", entry->eax);
0096 if (evmcs_expected)
0097 TEST_ASSERT((entry->eax & 0xffff) == 0x101,
0098 "Supported Enlightened VMCS version range is supposed to be 1:1"
0099 " 0x40000000.EAX: %x", entry->eax);
0100
0101 break;
0102 default:
0103 break;
0104
0105 }
0106
0107
0108
0109
0110
0111
0112
0113 }
0114 }
0115
0116 void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
0117 {
0118 static struct kvm_cpuid2 cpuid = {.nent = 0};
0119 int ret;
0120
0121 if (vcpu)
0122 ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
0123 else
0124 ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
0125
0126 TEST_ASSERT(ret == -1 && errno == E2BIG,
0127 "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
0128 " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
0129 }
0130
0131 int main(int argc, char *argv[])
0132 {
0133 struct kvm_vm *vm;
0134 const struct kvm_cpuid2 *hv_cpuid_entries;
0135 struct kvm_vcpu *vcpu;
0136
0137
0138 setbuf(stdout, NULL);
0139
0140 TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
0141
0142 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
0143
0144
0145 test_hv_cpuid_e2big(vm, vcpu);
0146
0147 hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
0148 test_hv_cpuid(hv_cpuid_entries, false);
0149 free((void *)hv_cpuid_entries);
0150
0151 if (!kvm_cpu_has(X86_FEATURE_VMX) ||
0152 !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
0153 print_skip("Enlightened VMCS is unsupported");
0154 goto do_sys;
0155 }
0156 vcpu_enable_evmcs(vcpu);
0157 hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
0158 test_hv_cpuid(hv_cpuid_entries, true);
0159 free((void *)hv_cpuid_entries);
0160
0161 do_sys:
0162
0163 if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
0164 print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
0165 goto out;
0166 }
0167
0168 test_hv_cpuid_e2big(vm, NULL);
0169
0170 hv_cpuid_entries = kvm_get_supported_hv_cpuid();
0171 test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
0172
0173 out:
0174 kvm_vm_free(vm);
0175
0176 return 0;
0177 }