hyperv_cpuid.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test for x86 KVM_CAP_HYPERV_CPUID
  4. *
  5. * Copyright (C) 2018, Red Hat, Inc.
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2.
  8. *
  9. */
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/ioctl.h>
  15. #include "test_util.h"
  16. #include "kvm_util.h"
  17. #include "processor.h"
  18. #include "vmx.h"
  19. static void guest_code(void)
  20. {
  21. }
  22. static bool smt_possible(void)
  23. {
  24. char buf[16];
  25. FILE *f;
  26. bool res = true;
  27. f = fopen("/sys/devices/system/cpu/smt/control", "r");
  28. if (f) {
  29. if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
  30. if (!strncmp(buf, "forceoff", 8) ||
  31. !strncmp(buf, "notsupported", 12))
  32. res = false;
  33. }
  34. fclose(f);
  35. }
  36. return res;
  37. }
  38. static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
  39. bool evmcs_expected)
  40. {
  41. int i;
  42. int nent_expected = 10;
  43. u32 test_val;
  44. TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
  45. "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
  46. " (returned %d)",
  47. nent_expected, hv_cpuid_entries->nent);
  48. for (i = 0; i < hv_cpuid_entries->nent; i++) {
  49. const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
  50. TEST_ASSERT((entry->function >= 0x40000000) &&
  51. (entry->function <= 0x40000082),
  52. "function %x is our of supported range",
  53. entry->function);
  54. TEST_ASSERT(entry->index == 0,
  55. ".index field should be zero");
  56. TEST_ASSERT(entry->flags == 0,
  57. ".flags field should be zero");
  58. TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
  59. !entry->padding[2], "padding should be zero");
  60. switch (entry->function) {
  61. case 0x40000000:
  62. test_val = 0x40000082;
  63. TEST_ASSERT(entry->eax == test_val,
  64. "Wrong max leaf report in 0x40000000.EAX: %x"
  65. " (evmcs=%d)",
  66. entry->eax, evmcs_expected
  67. );
  68. break;
  69. case 0x40000004:
  70. test_val = entry->eax & (1UL << 18);
  71. TEST_ASSERT(!!test_val == !smt_possible(),
  72. "NoNonArchitecturalCoreSharing bit"
  73. " doesn't reflect SMT setting");
  74. break;
  75. case 0x4000000A:
  76. TEST_ASSERT(entry->eax & (1UL << 19),
  77. "Enlightened MSR-Bitmap should always be supported"
  78. " 0x40000000.EAX: %x", entry->eax);
  79. if (evmcs_expected)
  80. TEST_ASSERT((entry->eax & 0xffff) == 0x101,
  81. "Supported Enlightened VMCS version range is supposed to be 1:1"
  82. " 0x40000000.EAX: %x", entry->eax);
  83. break;
  84. default:
  85. break;
  86. }
  87. /*
  88. * If needed for debug:
  89. * fprintf(stdout,
  90. * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
  91. * entry->function, entry->eax, entry->ebx, entry->ecx,
  92. * entry->edx);
  93. */
  94. }
  95. }
  96. void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
  97. {
  98. static struct kvm_cpuid2 cpuid = {.nent = 0};
  99. int ret;
  100. if (vcpu)
  101. ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
  102. else
  103. ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
  104. TEST_ASSERT(ret == -1 && errno == E2BIG,
  105. "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
  106. " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
  107. }
  108. int main(int argc, char *argv[])
  109. {
  110. struct kvm_vm *vm;
  111. const struct kvm_cpuid2 *hv_cpuid_entries;
  112. struct kvm_vcpu *vcpu;
  113. TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
  114. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  115. /* Test vCPU ioctl version */
  116. test_hv_cpuid_e2big(vm, vcpu);
  117. hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
  118. test_hv_cpuid(hv_cpuid_entries, false);
  119. free((void *)hv_cpuid_entries);
  120. if (!kvm_cpu_has(X86_FEATURE_VMX) ||
  121. !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
  122. print_skip("Enlightened VMCS is unsupported");
  123. goto do_sys;
  124. }
  125. vcpu_enable_evmcs(vcpu);
  126. hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
  127. test_hv_cpuid(hv_cpuid_entries, true);
  128. free((void *)hv_cpuid_entries);
  129. do_sys:
  130. /* Test system ioctl version */
  131. if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
  132. print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
  133. goto out;
  134. }
  135. test_hv_cpuid_e2big(vm, NULL);
  136. hv_cpuid_entries = kvm_get_supported_hv_cpuid();
  137. test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
  138. out:
  139. kvm_vm_free(vm);
  140. return 0;
  141. }