pvclock.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
  74. shadow->tsc_shift);
  75. }
  76. /*
  77. * Reads a consistent set of time-base values from hypervisor,
  78. * into a shadow data area.
  79. */
  80. static unsigned pvclock_get_time_values(struct pvclock_shadow_time *dst,
  81. struct pvclock_vcpu_time_info *src)
  82. {
  83. do {
  84. dst->version = src->version;
  85. rmb(); /* fetch version before data */
  86. dst->tsc_timestamp = src->tsc_timestamp;
  87. dst->system_timestamp = src->system_time;
  88. dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
  89. dst->tsc_shift = src->tsc_shift;
  90. dst->flags = src->flags;
  91. rmb(); /* test version after fetching data */
  92. } while ((src->version & 1) || (dst->version != src->version));
  93. return dst->version;
  94. }
  95. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  96. {
  97. u64 pv_tsc_khz = 1000000ULL << 32;
  98. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  99. if (src->tsc_shift < 0)
  100. pv_tsc_khz <<= -src->tsc_shift;
  101. else
  102. pv_tsc_khz >>= src->tsc_shift;
  103. return pv_tsc_khz;
  104. }
  105. static atomic64_t last_value = ATOMIC64_INIT(0);
  106. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  107. {
  108. struct pvclock_shadow_time shadow;
  109. unsigned version;
  110. cycle_t ret, offset;
  111. u64 last;
  112. do {
  113. version = pvclock_get_time_values(&shadow, src);
  114. barrier();
  115. offset = pvclock_get_nsec_offset(&shadow);
  116. ret = shadow.system_timestamp + offset;
  117. barrier();
  118. } while (version != src->version);
  119. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  120. (shadow.flags & PVCLOCK_TSC_STABLE_BIT))
  121. return ret;
  122. /*
  123. * Assumption here is that last_value, a global accumulator, always goes
  124. * forward. If we are less than that, we should not be much smaller.
  125. * We assume there is an error marging we're inside, and then the correction
  126. * does not sacrifice accuracy.
  127. *
  128. * For reads: global may have changed between test and return,
  129. * but this means someone else updated poked the clock at a later time.
  130. * We just need to make sure we are not seeing a backwards event.
  131. *
  132. * For updates: last_value = ret is not enough, since two vcpus could be
  133. * updating at the same time, and one of them could be slightly behind,
  134. * making the assumption that last_value always go forward fail to hold.
  135. */
  136. last = atomic64_read(&last_value);
  137. do {
  138. if (ret < last)
  139. return last;
  140. last = atomic64_cmpxchg(&last_value, last, ret);
  141. } while (unlikely(last != ret));
  142. return ret;
  143. }
  144. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  145. struct pvclock_vcpu_time_info *vcpu_time,
  146. struct timespec *ts)
  147. {
  148. u32 version;
  149. u64 delta;
  150. struct timespec now;
  151. /* get wallclock at system boot */
  152. do {
  153. version = wall_clock->version;
  154. rmb(); /* fetch version before time */
  155. now.tv_sec = wall_clock->sec;
  156. now.tv_nsec = wall_clock->nsec;
  157. rmb(); /* fetch time before checking version */
  158. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  159. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  160. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  161. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  162. now.tv_sec = delta;
  163. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  164. }