pvclock.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* paravirtual clock -- common code used by kvm/xen
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/percpu.h>
  16. #include <asm/pvclock.h>
  17. /*
  18. * These are perodically updated
  19. * xen: magic shared_info page
  20. * kvm: gpa registered via msr
  21. * and then copied here.
  22. */
  23. struct pvclock_shadow_time {
  24. u64 tsc_timestamp; /* TSC at last update of time vals. */
  25. u64 system_timestamp; /* Time, in nanosecs, since boot. */
  26. u32 tsc_to_nsec_mul;
  27. int tsc_shift;
  28. u32 version;
  29. u8 flags;
  30. };
  31. static u8 valid_flags __read_mostly = 0;
  32. void pvclock_set_flags(u8 flags)
  33. {
  34. valid_flags = flags;
  35. }
  36. /*
  37. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  38. * yielding a 64-bit result.
  39. */
  40. static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift)
  41. {
  42. u64 product;
  43. #ifdef __i386__
  44. u32 tmp1, tmp2;
  45. #endif
  46. if (shift < 0)
  47. delta >>= -shift;
  48. else
  49. delta <<= shift;
  50. #ifdef __i386__
  51. __asm__ (
  52. "mul %5 ; "
  53. "mov %4,%%eax ; "
  54. "mov %%edx,%4 ; "
  55. "mul %5 ; "
  56. "xor %5,%5 ; "
  57. "add %4,%%eax ; "
  58. "adc %5,%%edx ; "
  59. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  60. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  61. #elif defined(__x86_64__)
  62. __asm__ (
  63. "mul %%rdx ; shrd $32,%%rdx,%%rax"
  64. : "=a" (product) : "0" (delta), "d" ((u64)mul_frac) );
  65. #else
  66. #error implement me!
  67. #endif
  68. return product;
  69. }
  70. static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
  71. {
  72. u64 delta = native_read_tsc() - shadow->tsc_timestamp;
  73. return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift);
  74. }
  75. /*
  76. * Reads a consistent set of time-base values from hypervisor,
  77. * into a shadow data area.
  78. */
  79. static unsigned pvclock_get_time_values(struct pvclock_shadow_time *dst,
  80. struct pvclock_vcpu_time_info *src)
  81. {
  82. do {
  83. dst->version = src->version;
  84. rmb(); /* fetch version before data */
  85. dst->tsc_timestamp = src->tsc_timestamp;
  86. dst->system_timestamp = src->system_time;
  87. dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
  88. dst->tsc_shift = src->tsc_shift;
  89. dst->flags = src->flags;
  90. rmb(); /* test version after fetching data */
  91. } while ((src->version & 1) || (dst->version != src->version));
  92. return dst->version;
  93. }
  94. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  95. {
  96. u64 pv_tsc_khz = 1000000ULL << 32;
  97. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  98. if (src->tsc_shift < 0)
  99. pv_tsc_khz <<= -src->tsc_shift;
  100. else
  101. pv_tsc_khz >>= src->tsc_shift;
  102. return pv_tsc_khz;
  103. }
  104. static atomic64_t last_value = ATOMIC64_INIT(0);
  105. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  106. {
  107. struct pvclock_shadow_time shadow;
  108. unsigned version;
  109. cycle_t ret, offset;
  110. u64 last;
  111. do {
  112. version = pvclock_get_time_values(&shadow, src);
  113. barrier();
  114. offset = pvclock_get_nsec_offset(&shadow);
  115. ret = shadow.system_timestamp + offset;
  116. barrier();
  117. } while (version != src->version);
  118. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  119. (shadow.flags & PVCLOCK_TSC_STABLE_BIT))
  120. return ret;
  121. /*
  122. * Assumption here is that last_value, a global accumulator, always goes
  123. * forward. If we are less than that, we should not be much smaller.
  124. * We assume there is an error marging we're inside, and then the correction
  125. * does not sacrifice accuracy.
  126. *
  127. * For reads: global may have changed between test and return,
  128. * but this means someone else updated poked the clock at a later time.
  129. * We just need to make sure we are not seeing a backwards event.
  130. *
  131. * For updates: last_value = ret is not enough, since two vcpus could be
  132. * updating at the same time, and one of them could be slightly behind,
  133. * making the assumption that last_value always go forward fail to hold.
  134. */
  135. last = atomic64_read(&last_value);
  136. do {
  137. if (ret < last)
  138. return last;
  139. last = atomic64_cmpxchg(&last_value, last, ret);
  140. } while (unlikely(last != ret));
  141. return ret;
  142. }
  143. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  144. struct pvclock_vcpu_time_info *vcpu_time,
  145. struct timespec *ts)
  146. {
  147. u32 version;
  148. u64 delta;
  149. struct timespec now;
  150. /* get wallclock at system boot */
  151. do {
  152. version = wall_clock->version;
  153. rmb(); /* fetch version before time */
  154. now.tv_sec = wall_clock->sec;
  155. now.tv_nsec = wall_clock->nsec;
  156. rmb(); /* fetch time before checking version */
  157. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  158. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  159. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  160. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  161. now.tv_sec = delta;
  162. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  163. }