pvclock.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  73. {
  74. struct pvclock_shadow_time shadow;
  75. unsigned version;
  76. cycle_t ret, offset;
  77. u64 last;
  78. do {
  79. version = pvclock_get_time_values(&shadow, src);
  80. barrier();
  81. offset = pvclock_get_nsec_offset(&shadow);
  82. ret = shadow.system_timestamp + offset;
  83. barrier();
  84. } while (version != src->version);
  85. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  86. (shadow.flags & PVCLOCK_TSC_STABLE_BIT))
  87. return ret;
  88. /*
  89. * Assumption here is that last_value, a global accumulator, always goes
  90. * forward. If we are less than that, we should not be much smaller.
  91. * We assume there is an error marging we're inside, and then the correction
  92. * does not sacrifice accuracy.
  93. *
  94. * For reads: global may have changed between test and return,
  95. * but this means someone else updated poked the clock at a later time.
  96. * We just need to make sure we are not seeing a backwards event.
  97. *
  98. * For updates: last_value = ret is not enough, since two vcpus could be
  99. * updating at the same time, and one of them could be slightly behind,
  100. * making the assumption that last_value always go forward fail to hold.
  101. */
  102. last = atomic64_read(&last_value);
  103. do {
  104. if (ret < last)
  105. return last;
  106. last = atomic64_cmpxchg(&last_value, last, ret);
  107. } while (unlikely(last != ret));
  108. return ret;
  109. }
  110. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  111. struct pvclock_vcpu_time_info *vcpu_time,
  112. struct timespec *ts)
  113. {
  114. u32 version;
  115. u64 delta;
  116. struct timespec now;
  117. /* get wallclock at system boot */
  118. do {
  119. version = wall_clock->version;
  120. rmb(); /* fetch version before time */
  121. now.tv_sec = wall_clock->sec;
  122. now.tv_nsec = wall_clock->nsec;
  123. rmb(); /* fetch time before checking version */
  124. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  125. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  126. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  127. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  128. now.tv_sec = delta;
  129. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  130. }