hyperv_clock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "libcflat.h"
  2. #include "smp.h"
  3. #include "atomic.h"
  4. #include "processor.h"
  5. #include "hyperv.h"
  6. #include "vm.h"
  7. #define MAX_CPU 4
  8. #define TICKS_PER_SEC (1000000000 / 100)
  9. struct hv_reference_tsc_page *hv_clock;
  10. /*
  11. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  12. * yielding a 64-bit result.
  13. */
  14. static inline u64 scale_delta(u64 delta, u64 mul_frac)
  15. {
  16. u64 product, unused;
  17. __asm__ (
  18. "mulq %3"
  19. : "=d" (product), "=a" (unused) : "1" (delta), "rm" ((u64)mul_frac) );
  20. return product;
  21. }
  22. static u64 hvclock_tsc_to_ticks(struct hv_reference_tsc_page *shadow, uint64_t tsc)
  23. {
  24. u64 delta = tsc;
  25. return scale_delta(delta, shadow->tsc_scale) + shadow->tsc_offset;
  26. }
  27. /*
  28. * Reads a consistent set of time-base values from hypervisor,
  29. * into a shadow data area.
  30. */
  31. static void hvclock_get_time_values(struct hv_reference_tsc_page *shadow,
  32. struct hv_reference_tsc_page *page)
  33. {
  34. int seq;
  35. do {
  36. seq = page->tsc_sequence;
  37. rmb(); /* fetch version before data */
  38. *shadow = *page;
  39. rmb(); /* test version after fetching data */
  40. } while (shadow->tsc_sequence != seq);
  41. }
  42. uint64_t hv_clock_read(void)
  43. {
  44. struct hv_reference_tsc_page shadow;
  45. hvclock_get_time_values(&shadow, hv_clock);
  46. return hvclock_tsc_to_ticks(&shadow, rdtsc());
  47. }
  48. bool ok[MAX_CPU];
  49. uint64_t loops[MAX_CPU];
  50. #define iabs(x) ((x) < 0 ? -(x) : (x))
  51. static void hv_clock_test(void *data)
  52. {
  53. int i = smp_id();
  54. uint64_t t = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
  55. uint64_t end = t + 3 * TICKS_PER_SEC;
  56. uint64_t msr_sample = t + TICKS_PER_SEC;
  57. int min_delta = 123456, max_delta = -123456;
  58. bool got_drift = false;
  59. bool got_warp = false;
  60. ok[i] = true;
  61. do {
  62. uint64_t now = hv_clock_read();
  63. int delta = rdmsr(HV_X64_MSR_TIME_REF_COUNT) - now;
  64. min_delta = delta < min_delta ? delta : min_delta;
  65. if (t < msr_sample) {
  66. max_delta = delta > max_delta ? delta: max_delta;
  67. } else if (delta < 0 || delta > max_delta * 3 / 2) {
  68. printf("suspecting drift on CPU %d? delta = %d, acceptable [0, %d)\n", smp_id(),
  69. delta, max_delta);
  70. ok[i] = false;
  71. got_drift = true;
  72. max_delta *= 2;
  73. }
  74. if (now < t && !got_warp) {
  75. printf("warp on CPU %d!\n", smp_id());
  76. ok[i] = false;
  77. got_warp = true;
  78. break;
  79. }
  80. t = now;
  81. } while(t < end);
  82. if (!got_drift)
  83. printf("delta on CPU %d was %d...%d\n", smp_id(), min_delta, max_delta);
  84. barrier();
  85. }
  86. static void check_test(int ncpus)
  87. {
  88. int i;
  89. bool pass;
  90. on_cpus(hv_clock_test, NULL);
  91. pass = true;
  92. for (i = ncpus - 1; i >= 0; i--)
  93. pass &= ok[i];
  94. report("TSC reference precision test", pass);
  95. }
  96. static void hv_perf_test(void *data)
  97. {
  98. uint64_t t = hv_clock_read();
  99. uint64_t end = t + 1000000000 / 100;
  100. uint64_t local_loops = 0;
  101. do {
  102. t = hv_clock_read();
  103. local_loops++;
  104. } while(t < end);
  105. loops[smp_id()] = local_loops;
  106. }
  107. static void perf_test(int ncpus)
  108. {
  109. int i;
  110. uint64_t total_loops;
  111. on_cpus(hv_perf_test, NULL);
  112. total_loops = 0;
  113. for (i = ncpus - 1; i >= 0; i--)
  114. total_loops += loops[i];
  115. printf("iterations/sec: %" PRId64"\n", total_loops / ncpus);
  116. }
  117. int main(int ac, char **av)
  118. {
  119. int nerr = 0;
  120. int ncpus;
  121. struct hv_reference_tsc_page shadow;
  122. uint64_t tsc1, t1, tsc2, t2;
  123. uint64_t ref1, ref2;
  124. setup_vm();
  125. smp_init();
  126. ncpus = cpu_count();
  127. if (ncpus > MAX_CPU)
  128. report_abort("number cpus exceeds %d", MAX_CPU);
  129. hv_clock = alloc_page();
  130. wrmsr(HV_X64_MSR_REFERENCE_TSC, (u64)(uintptr_t)hv_clock | 1);
  131. report("MSR value after enabling",
  132. rdmsr(HV_X64_MSR_REFERENCE_TSC) == ((u64)(uintptr_t)hv_clock | 1));
  133. hvclock_get_time_values(&shadow, hv_clock);
  134. if (shadow.tsc_sequence == 0 || shadow.tsc_sequence == 0xFFFFFFFF) {
  135. printf("Reference TSC page not available\n");
  136. exit(1);
  137. }
  138. printf("scale: %" PRIx64" offset: %" PRId64"\n", shadow.tsc_scale, shadow.tsc_offset);
  139. ref1 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
  140. tsc1 = rdtsc();
  141. t1 = hvclock_tsc_to_ticks(&shadow, tsc1);
  142. printf("refcnt %" PRId64", TSC %" PRIx64", TSC reference %" PRId64"\n",
  143. ref1, tsc1, t1);
  144. do
  145. ref2 = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
  146. while (ref2 < ref1 + 2 * TICKS_PER_SEC);
  147. tsc2 = rdtsc();
  148. t2 = hvclock_tsc_to_ticks(&shadow, tsc2);
  149. printf("refcnt %" PRId64" (delta %" PRId64"), TSC %" PRIx64", "
  150. "TSC reference %" PRId64" (delta %" PRId64")\n",
  151. ref2, ref2 - ref1, tsc2, t2, t2 - t1);
  152. check_test(ncpus);
  153. perf_test(ncpus);
  154. wrmsr(HV_X64_MSR_REFERENCE_TSC, 0LL);
  155. report("MSR value after disabling", rdmsr(HV_X64_MSR_REFERENCE_TSC) == 0);
  156. return nerr > 0 ? 1 : 0;
  157. }