aperfmperf_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test for KVM_X86_DISABLE_EXITS_APERFMPERF
  4. *
  5. * Copyright (C) 2025, Google LLC.
  6. *
  7. * Test the ability to disable VM-exits for rdmsr of IA32_APERF and
  8. * IA32_MPERF. When these VM-exits are disabled, reads of these MSRs
  9. * return the host's values.
  10. *
  11. * Note: Requires read access to /dev/cpu/<lpu>/msr to read host MSRs.
  12. */
  13. #include <fcntl.h>
  14. #include <limits.h>
  15. #include <stdbool.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <unistd.h>
  19. #include <asm/msr-index.h>
  20. #include "kvm_util.h"
  21. #include "processor.h"
  22. #include "svm_util.h"
  23. #include "test_util.h"
  24. #include "vmx.h"
  25. #define NUM_ITERATIONS 10000
  26. static int open_dev_msr(int cpu)
  27. {
  28. char path[PATH_MAX];
  29. snprintf(path, sizeof(path), "/dev/cpu/%d/msr", cpu);
  30. return open_path_or_exit(path, O_RDONLY);
  31. }
  32. static uint64_t read_dev_msr(int msr_fd, uint32_t msr)
  33. {
  34. uint64_t data;
  35. ssize_t rc;
  36. rc = pread(msr_fd, &data, sizeof(data), msr);
  37. TEST_ASSERT(rc == sizeof(data), "Read of MSR 0x%x failed", msr);
  38. return data;
  39. }
  40. static void guest_read_aperf_mperf(void)
  41. {
  42. int i;
  43. for (i = 0; i < NUM_ITERATIONS; i++)
  44. GUEST_SYNC2(rdmsr(MSR_IA32_APERF), rdmsr(MSR_IA32_MPERF));
  45. }
  46. #define L2_GUEST_STACK_SIZE 64
  47. static void l2_guest_code(void)
  48. {
  49. guest_read_aperf_mperf();
  50. GUEST_DONE();
  51. }
  52. static void l1_svm_code(struct svm_test_data *svm)
  53. {
  54. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  55. struct vmcb *vmcb = svm->vmcb;
  56. generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  57. run_guest(vmcb, svm->vmcb_gpa);
  58. }
  59. static void l1_vmx_code(struct vmx_pages *vmx)
  60. {
  61. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  62. GUEST_ASSERT_EQ(prepare_for_vmx_operation(vmx), true);
  63. GUEST_ASSERT_EQ(load_vmcs(vmx), true);
  64. prepare_vmcs(vmx, NULL, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  65. /*
  66. * Enable MSR bitmaps (the bitmap itself is allocated, zeroed, and set
  67. * in the VMCS by prepare_vmcs()), as MSR exiting mandatory on Intel.
  68. */
  69. vmwrite(CPU_BASED_VM_EXEC_CONTROL,
  70. vmreadz(CPU_BASED_VM_EXEC_CONTROL) | CPU_BASED_USE_MSR_BITMAPS);
  71. GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code));
  72. GUEST_ASSERT(!vmlaunch());
  73. }
  74. static void guest_code(void *nested_test_data)
  75. {
  76. guest_read_aperf_mperf();
  77. if (this_cpu_has(X86_FEATURE_SVM))
  78. l1_svm_code(nested_test_data);
  79. else if (this_cpu_has(X86_FEATURE_VMX))
  80. l1_vmx_code(nested_test_data);
  81. else
  82. GUEST_DONE();
  83. TEST_FAIL("L2 should have signaled 'done'");
  84. }
  85. static void guest_no_aperfmperf(void)
  86. {
  87. uint64_t msr_val;
  88. uint8_t vector;
  89. vector = rdmsr_safe(MSR_IA32_APERF, &msr_val);
  90. GUEST_ASSERT(vector == GP_VECTOR);
  91. vector = rdmsr_safe(MSR_IA32_APERF, &msr_val);
  92. GUEST_ASSERT(vector == GP_VECTOR);
  93. GUEST_DONE();
  94. }
  95. int main(int argc, char *argv[])
  96. {
  97. const bool has_nested = kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX);
  98. uint64_t host_aperf_before, host_mperf_before;
  99. vm_vaddr_t nested_test_data_gva;
  100. struct kvm_vcpu *vcpu;
  101. struct kvm_vm *vm;
  102. int msr_fd, cpu, i;
  103. /* Sanity check that APERF/MPERF are unsupported by default. */
  104. vm = vm_create_with_one_vcpu(&vcpu, guest_no_aperfmperf);
  105. vcpu_run(vcpu);
  106. TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
  107. kvm_vm_free(vm);
  108. cpu = pin_self_to_any_cpu();
  109. msr_fd = open_dev_msr(cpu);
  110. /*
  111. * This test requires a non-standard VM initialization, because
  112. * KVM_ENABLE_CAP cannot be used on a VM file descriptor after
  113. * a VCPU has been created.
  114. */
  115. vm = vm_create(1);
  116. TEST_REQUIRE(vm_check_cap(vm, KVM_CAP_X86_DISABLE_EXITS) &
  117. KVM_X86_DISABLE_EXITS_APERFMPERF);
  118. vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS,
  119. KVM_X86_DISABLE_EXITS_APERFMPERF);
  120. vcpu = vm_vcpu_add(vm, 0, guest_code);
  121. if (!has_nested)
  122. nested_test_data_gva = NONCANONICAL;
  123. else if (kvm_cpu_has(X86_FEATURE_SVM))
  124. vcpu_alloc_svm(vm, &nested_test_data_gva);
  125. else
  126. vcpu_alloc_vmx(vm, &nested_test_data_gva);
  127. vcpu_args_set(vcpu, 1, nested_test_data_gva);
  128. host_aperf_before = read_dev_msr(msr_fd, MSR_IA32_APERF);
  129. host_mperf_before = read_dev_msr(msr_fd, MSR_IA32_MPERF);
  130. for (i = 0; i <= NUM_ITERATIONS * (1 + has_nested); i++) {
  131. uint64_t host_aperf_after, host_mperf_after;
  132. uint64_t guest_aperf, guest_mperf;
  133. struct ucall uc;
  134. vcpu_run(vcpu);
  135. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
  136. switch (get_ucall(vcpu, &uc)) {
  137. case UCALL_DONE:
  138. goto done;
  139. case UCALL_ABORT:
  140. REPORT_GUEST_ASSERT(uc);
  141. case UCALL_SYNC:
  142. guest_aperf = uc.args[0];
  143. guest_mperf = uc.args[1];
  144. host_aperf_after = read_dev_msr(msr_fd, MSR_IA32_APERF);
  145. host_mperf_after = read_dev_msr(msr_fd, MSR_IA32_MPERF);
  146. TEST_ASSERT(host_aperf_before < guest_aperf,
  147. "APERF: host_before (0x%" PRIx64 ") >= guest (0x%" PRIx64 ")",
  148. host_aperf_before, guest_aperf);
  149. TEST_ASSERT(guest_aperf < host_aperf_after,
  150. "APERF: guest (0x%" PRIx64 ") >= host_after (0x%" PRIx64 ")",
  151. guest_aperf, host_aperf_after);
  152. TEST_ASSERT(host_mperf_before < guest_mperf,
  153. "MPERF: host_before (0x%" PRIx64 ") >= guest (0x%" PRIx64 ")",
  154. host_mperf_before, guest_mperf);
  155. TEST_ASSERT(guest_mperf < host_mperf_after,
  156. "MPERF: guest (0x%" PRIx64 ") >= host_after (0x%" PRIx64 ")",
  157. guest_mperf, host_mperf_after);
  158. host_aperf_before = host_aperf_after;
  159. host_mperf_before = host_mperf_after;
  160. break;
  161. }
  162. }
  163. TEST_FAIL("Didn't receive UCALL_DONE\n");
  164. done:
  165. kvm_vm_free(vm);
  166. close(msr_fd);
  167. return 0;
  168. }