hyperv_cpuid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
  23. {
  24. const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
  25. const struct kvm_cpuid2 *hv_cpuid_entries;
  26. int i;
  27. int nent_expected = 10;
  28. u32 test_val;
  29. if (vcpu)
  30. hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
  31. else
  32. hv_cpuid_entries = kvm_get_supported_hv_cpuid();
  33. TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
  34. "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
  35. " (returned %d)",
  36. nent_expected, hv_cpuid_entries->nent);
  37. for (i = 0; i < hv_cpuid_entries->nent; i++) {
  38. const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
  39. TEST_ASSERT((entry->function >= 0x40000000) &&
  40. (entry->function <= 0x40000082),
  41. "function %x is out of supported range",
  42. entry->function);
  43. TEST_ASSERT(entry->index == 0,
  44. ".index field should be zero");
  45. TEST_ASSERT(entry->flags == 0,
  46. ".flags field should be zero");
  47. TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
  48. !entry->padding[2], "padding should be zero");
  49. switch (entry->function) {
  50. case 0x40000000:
  51. test_val = 0x40000082;
  52. TEST_ASSERT(entry->eax == test_val,
  53. "Wrong max leaf report in 0x40000000.EAX: %x"
  54. " (evmcs=%d)",
  55. entry->eax, evmcs_expected
  56. );
  57. break;
  58. case 0x40000003:
  59. TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),
  60. "\"Direct\" Synthetic Timers should require in-kernel APIC");
  61. break;
  62. case 0x40000004:
  63. test_val = entry->eax & (1UL << 18);
  64. TEST_ASSERT(!!test_val == !is_smt_possible(),
  65. "NoNonArchitecturalCoreSharing bit"
  66. " doesn't reflect SMT setting");
  67. TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),
  68. "Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");
  69. break;
  70. case 0x4000000A:
  71. TEST_ASSERT(entry->eax & (1UL << 19),
  72. "Enlightened MSR-Bitmap should always be supported"
  73. " 0x40000000.EAX: %x", entry->eax);
  74. if (evmcs_expected)
  75. TEST_ASSERT((entry->eax & 0xffff) == 0x101,
  76. "Supported Enlightened VMCS version range is supposed to be 1:1"
  77. " 0x40000000.EAX: %x", entry->eax);
  78. break;
  79. default:
  80. break;
  81. }
  82. /*
  83. * If needed for debug:
  84. * fprintf(stdout,
  85. * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
  86. * entry->function, entry->eax, entry->ebx, entry->ecx,
  87. * entry->edx);
  88. */
  89. }
  90. /*
  91. * Note, the CPUID array returned by the system-scoped helper is a one-
  92. * time allocation, i.e. must not be freed.
  93. */
  94. if (vcpu)
  95. free((void *)hv_cpuid_entries);
  96. }
  97. static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
  98. {
  99. static struct kvm_cpuid2 cpuid = {.nent = 0};
  100. int ret;
  101. if (vcpu)
  102. ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
  103. else
  104. ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
  105. TEST_ASSERT(ret == -1 && errno == E2BIG,
  106. "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
  107. " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
  108. }
  109. int main(int argc, char *argv[])
  110. {
  111. struct kvm_vm *vm;
  112. struct kvm_vcpu *vcpu;
  113. TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
  114. /* Test the vCPU ioctl without an in-kernel local APIC. */
  115. vm = vm_create_barebones();
  116. vcpu = __vm_vcpu_add(vm, 0);
  117. test_hv_cpuid(vcpu, false);
  118. kvm_vm_free(vm);
  119. /* Test vCPU ioctl version */
  120. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  121. test_hv_cpuid_e2big(vm, vcpu);
  122. test_hv_cpuid(vcpu, false);
  123. if (!kvm_cpu_has(X86_FEATURE_VMX) ||
  124. !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
  125. print_skip("Enlightened VMCS is unsupported");
  126. goto do_sys;
  127. }
  128. vcpu_enable_evmcs(vcpu);
  129. test_hv_cpuid(vcpu, true);
  130. do_sys:
  131. /* Test system ioctl version */
  132. if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
  133. print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
  134. goto out;
  135. }
  136. test_hv_cpuid_e2big(vm, NULL);
  137. test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
  138. out:
  139. kvm_vm_free(vm);
  140. return 0;
  141. }