pvclock.c 3.9 KB

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