kvm_buslock_test.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 Advanced Micro Devices, Inc.
  4. */
  5. #include <linux/atomic.h>
  6. #include "kvm_util.h"
  7. #include "processor.h"
  8. #include "svm_util.h"
  9. #include "vmx.h"
  10. #include "test_util.h"
  11. #define NR_BUS_LOCKS_PER_LEVEL 100
  12. #define CACHE_LINE_SIZE 64
  13. /*
  14. * To generate a bus lock, carve out a buffer that precisely occupies two cache
  15. * lines and perform an atomic access that splits the two lines.
  16. */
  17. static u8 buffer[CACHE_LINE_SIZE * 2] __aligned(CACHE_LINE_SIZE);
  18. static atomic_t *val = (void *)&buffer[CACHE_LINE_SIZE - (sizeof(*val) / 2)];
  19. static void guest_generate_buslocks(void)
  20. {
  21. for (int i = 0; i < NR_BUS_LOCKS_PER_LEVEL; i++)
  22. atomic_inc(val);
  23. }
  24. #define L2_GUEST_STACK_SIZE 64
  25. static void l2_guest_code(void)
  26. {
  27. guest_generate_buslocks();
  28. GUEST_DONE();
  29. }
  30. static void l1_svm_code(struct svm_test_data *svm)
  31. {
  32. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  33. struct vmcb *vmcb = svm->vmcb;
  34. generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  35. run_guest(vmcb, svm->vmcb_gpa);
  36. }
  37. static void l1_vmx_code(struct vmx_pages *vmx)
  38. {
  39. unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
  40. GUEST_ASSERT_EQ(prepare_for_vmx_operation(vmx), true);
  41. GUEST_ASSERT_EQ(load_vmcs(vmx), true);
  42. prepare_vmcs(vmx, NULL, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
  43. GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code));
  44. GUEST_ASSERT(!vmlaunch());
  45. }
  46. static void guest_code(void *test_data)
  47. {
  48. guest_generate_buslocks();
  49. if (this_cpu_has(X86_FEATURE_SVM))
  50. l1_svm_code(test_data);
  51. else if (this_cpu_has(X86_FEATURE_VMX))
  52. l1_vmx_code(test_data);
  53. else
  54. GUEST_DONE();
  55. TEST_FAIL("L2 should have signaled 'done'");
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. const bool has_nested = kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX);
  60. vm_vaddr_t nested_test_data_gva;
  61. struct kvm_vcpu *vcpu;
  62. struct kvm_run *run;
  63. struct kvm_vm *vm;
  64. int i, bus_locks = 0;
  65. TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));
  66. vm = vm_create(1);
  67. vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);
  68. vcpu = vm_vcpu_add(vm, 0, guest_code);
  69. if (kvm_cpu_has(X86_FEATURE_SVM))
  70. vcpu_alloc_svm(vm, &nested_test_data_gva);
  71. else
  72. vcpu_alloc_vmx(vm, &nested_test_data_gva);
  73. vcpu_args_set(vcpu, 1, nested_test_data_gva);
  74. run = vcpu->run;
  75. for (i = 0; i <= NR_BUS_LOCKS_PER_LEVEL * (1 + has_nested); i++) {
  76. struct ucall uc;
  77. vcpu_run(vcpu);
  78. if (run->exit_reason == KVM_EXIT_IO) {
  79. switch (get_ucall(vcpu, &uc)) {
  80. case UCALL_ABORT:
  81. REPORT_GUEST_ASSERT(uc);
  82. goto done;
  83. case UCALL_SYNC:
  84. continue;
  85. case UCALL_DONE:
  86. goto done;
  87. default:
  88. TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
  89. }
  90. }
  91. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_X86_BUS_LOCK);
  92. /*
  93. * Verify the counter is actually getting incremented, e.g. that
  94. * KVM isn't skipping the instruction. On Intel, the exit is
  95. * trap-like, i.e. the counter should already have been
  96. * incremented. On AMD, it's fault-like, i.e. the counter will
  97. * be incremented when the guest re-executes the instruction.
  98. */
  99. sync_global_from_guest(vm, *val);
  100. TEST_ASSERT_EQ(atomic_read(val), bus_locks + host_cpu_is_intel);
  101. bus_locks++;
  102. }
  103. TEST_FAIL("Didn't receive UCALL_DONE, took %u bus lock exits\n", bus_locks);
  104. done:
  105. TEST_ASSERT_EQ(i, bus_locks);
  106. kvm_vm_free(vm);
  107. return 0;
  108. }