pvclock.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. static u8 valid_flags __read_mostly = 0;
  18. void pvclock_set_flags(u8 flags)
  19. {
  20. valid_flags = flags;
  21. }
  22. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  23. {
  24. u64 pv_tsc_khz = 1000000ULL << 32;
  25. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  26. if (src->tsc_shift < 0)
  27. pv_tsc_khz <<= -src->tsc_shift;
  28. else
  29. pv_tsc_khz >>= src->tsc_shift;
  30. return pv_tsc_khz;
  31. }
  32. static atomic64_t last_value = ATOMIC64_INIT(0);
  33. void pvclock_resume(void)
  34. {
  35. atomic64_set(&last_value, 0);
  36. }
  37. u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
  38. {
  39. unsigned version;
  40. cycle_t ret;
  41. u8 flags;
  42. do {
  43. version = __pvclock_read_cycles(src, &ret, &flags);
  44. } while ((src->version & 1) || version != src->version);
  45. return flags & valid_flags;
  46. }
  47. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  48. {
  49. unsigned version;
  50. cycle_t ret;
  51. u64 last;
  52. u8 flags;
  53. do {
  54. version = __pvclock_read_cycles(src, &ret, &flags);
  55. } while ((src->version & 1) || version != src->version);
  56. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  57. (flags & PVCLOCK_TSC_STABLE_BIT))
  58. return ret;
  59. /*
  60. * Assumption here is that last_value, a global accumulator, always goes
  61. * forward. If we are less than that, we should not be much smaller.
  62. * We assume there is an error marging we're inside, and then the correction
  63. * does not sacrifice accuracy.
  64. *
  65. * For reads: global may have changed between test and return,
  66. * but this means someone else updated poked the clock at a later time.
  67. * We just need to make sure we are not seeing a backwards event.
  68. *
  69. * For updates: last_value = ret is not enough, since two vcpus could be
  70. * updating at the same time, and one of them could be slightly behind,
  71. * making the assumption that last_value always go forward fail to hold.
  72. */
  73. last = atomic64_read(&last_value);
  74. do {
  75. if (ret < last)
  76. return last;
  77. last = atomic64_cmpxchg(&last_value, last, ret);
  78. } while (unlikely(last != ret));
  79. return ret;
  80. }
  81. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  82. struct pvclock_vcpu_time_info *vcpu_time,
  83. struct timespec *ts)
  84. {
  85. u32 version;
  86. u64 delta;
  87. struct timespec now;
  88. /* get wallclock at system boot */
  89. do {
  90. version = wall_clock->version;
  91. rmb(); /* fetch version before time */
  92. now.tv_sec = wall_clock->sec;
  93. now.tv_nsec = wall_clock->nsec;
  94. rmb(); /* fetch time before checking version */
  95. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  96. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  97. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  98. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  99. now.tv_sec = delta;
  100. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  101. }