pvclock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
  37. {
  38. u64 delta = native_read_tsc() - shadow->tsc_timestamp;
  39. return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
  40. shadow->tsc_shift);
  41. }
  42. /*
  43. * Reads a consistent set of time-base values from hypervisor,
  44. * into a shadow data area.
  45. */
  46. static unsigned pvclock_get_time_values(struct pvclock_shadow_time *dst,
  47. struct pvclock_vcpu_time_info *src)
  48. {
  49. do {
  50. dst->version = src->version;
  51. rmb(); /* fetch version before data */
  52. dst->tsc_timestamp = src->tsc_timestamp;
  53. dst->system_timestamp = src->system_time;
  54. dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
  55. dst->tsc_shift = src->tsc_shift;
  56. dst->flags = src->flags;
  57. rmb(); /* test version after fetching data */
  58. } while ((src->version & 1) || (dst->version != src->version));
  59. return dst->version;
  60. }
  61. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  62. {
  63. u64 pv_tsc_khz = 1000000ULL << 32;
  64. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  65. if (src->tsc_shift < 0)
  66. pv_tsc_khz <<= -src->tsc_shift;
  67. else
  68. pv_tsc_khz >>= src->tsc_shift;
  69. return pv_tsc_khz;
  70. }
  71. static atomic64_t last_value = ATOMIC64_INIT(0);
  72. void pvclock_resume(void)
  73. {
  74. atomic64_set(&last_value, 0);
  75. }
  76. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  77. {
  78. struct pvclock_shadow_time shadow;
  79. unsigned version;
  80. cycle_t ret, offset;
  81. u64 last;
  82. do {
  83. version = pvclock_get_time_values(&shadow, src);
  84. barrier();
  85. offset = pvclock_get_nsec_offset(&shadow);
  86. ret = shadow.system_timestamp + offset;
  87. barrier();
  88. } while (version != src->version);
  89. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  90. (shadow.flags & PVCLOCK_TSC_STABLE_BIT))
  91. return ret;
  92. /*
  93. * Assumption here is that last_value, a global accumulator, always goes
  94. * forward. If we are less than that, we should not be much smaller.
  95. * We assume there is an error marging we're inside, and then the correction
  96. * does not sacrifice accuracy.
  97. *
  98. * For reads: global may have changed between test and return,
  99. * but this means someone else updated poked the clock at a later time.
  100. * We just need to make sure we are not seeing a backwards event.
  101. *
  102. * For updates: last_value = ret is not enough, since two vcpus could be
  103. * updating at the same time, and one of them could be slightly behind,
  104. * making the assumption that last_value always go forward fail to hold.
  105. */
  106. last = atomic64_read(&last_value);
  107. do {
  108. if (ret < last)
  109. return last;
  110. last = atomic64_cmpxchg(&last_value, last, ret);
  111. } while (unlikely(last != ret));
  112. return ret;
  113. }
  114. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  115. struct pvclock_vcpu_time_info *vcpu_time,
  116. struct timespec *ts)
  117. {
  118. u32 version;
  119. u64 delta;
  120. struct timespec now;
  121. /* get wallclock at system boot */
  122. do {
  123. version = wall_clock->version;
  124. rmb(); /* fetch version before time */
  125. now.tv_sec = wall_clock->sec;
  126. now.tv_nsec = wall_clock->nsec;
  127. rmb(); /* fetch time before checking version */
  128. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  129. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  130. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  131. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  132. now.tv_sec = delta;
  133. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  134. }