tsc_scaling_sync.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2021 Amazon.com, Inc. or its affiliates.
  4. */
  5. #include "test_util.h"
  6. #include "kvm_util.h"
  7. #include "processor.h"
  8. #include <stdint.h>
  9. #include <time.h>
  10. #include <sched.h>
  11. #include <signal.h>
  12. #include <pthread.h>
  13. #define NR_TEST_VCPUS 20
  14. static struct kvm_vm *vm;
  15. pthread_spinlock_t create_lock;
  16. #define TEST_TSC_KHZ 2345678UL
  17. #define TEST_TSC_OFFSET 200000000
  18. uint64_t tsc_sync;
  19. static void guest_code(void)
  20. {
  21. uint64_t start_tsc, local_tsc, tmp;
  22. start_tsc = rdtsc();
  23. do {
  24. tmp = READ_ONCE(tsc_sync);
  25. local_tsc = rdtsc();
  26. WRITE_ONCE(tsc_sync, local_tsc);
  27. if (unlikely(local_tsc < tmp))
  28. GUEST_SYNC_ARGS(0, local_tsc, tmp, 0, 0);
  29. } while (local_tsc - start_tsc < 5000 * TEST_TSC_KHZ);
  30. GUEST_DONE();
  31. }
  32. static void *run_vcpu(void *_cpu_nr)
  33. {
  34. unsigned long vcpu_id = (unsigned long)_cpu_nr;
  35. unsigned long failures = 0;
  36. static bool first_cpu_done;
  37. struct kvm_vcpu *vcpu;
  38. /* The kernel is fine, but vm_vcpu_add() needs locking */
  39. pthread_spin_lock(&create_lock);
  40. vcpu = vm_vcpu_add(vm, vcpu_id, guest_code);
  41. if (!first_cpu_done) {
  42. first_cpu_done = true;
  43. vcpu_set_msr(vcpu, MSR_IA32_TSC, TEST_TSC_OFFSET);
  44. }
  45. pthread_spin_unlock(&create_lock);
  46. for (;;) {
  47. struct ucall uc;
  48. vcpu_run(vcpu);
  49. TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
  50. switch (get_ucall(vcpu, &uc)) {
  51. case UCALL_DONE:
  52. goto out;
  53. case UCALL_SYNC:
  54. printf("Guest %d sync %lx %lx %ld\n", vcpu->id,
  55. uc.args[2], uc.args[3], uc.args[2] - uc.args[3]);
  56. failures++;
  57. break;
  58. default:
  59. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  60. }
  61. }
  62. out:
  63. return (void *)failures;
  64. }
  65. int main(int argc, char *argv[])
  66. {
  67. TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TSC_CONTROL));
  68. vm = vm_create(NR_TEST_VCPUS);
  69. vm_ioctl(vm, KVM_SET_TSC_KHZ, (void *) TEST_TSC_KHZ);
  70. pthread_spin_init(&create_lock, PTHREAD_PROCESS_PRIVATE);
  71. pthread_t cpu_threads[NR_TEST_VCPUS];
  72. unsigned long cpu;
  73. for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++)
  74. pthread_create(&cpu_threads[cpu], NULL, run_vcpu, (void *)cpu);
  75. unsigned long failures = 0;
  76. for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++) {
  77. void *this_cpu_failures;
  78. pthread_join(cpu_threads[cpu], &this_cpu_failures);
  79. failures += (unsigned long)this_cpu_failures;
  80. }
  81. TEST_ASSERT(!failures, "TSC sync failed");
  82. pthread_spin_destroy(&create_lock);
  83. kvm_vm_free(vm);
  84. return 0;
  85. }