pvclock.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <asm/vgtod.h>
  24. static u8 valid_flags __read_mostly = 0;
  25. static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly;
  26. void pvclock_set_flags(u8 flags)
  27. {
  28. valid_flags = flags;
  29. }
  30. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  31. {
  32. u64 pv_tsc_khz = 1000000ULL << 32;
  33. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  34. if (src->tsc_shift < 0)
  35. pv_tsc_khz <<= -src->tsc_shift;
  36. else
  37. pv_tsc_khz >>= src->tsc_shift;
  38. return pv_tsc_khz;
  39. }
  40. void pvclock_touch_watchdogs(void)
  41. {
  42. touch_softlockup_watchdog_sync();
  43. clocksource_touch_watchdog();
  44. rcu_cpu_stall_reset();
  45. reset_hung_task_detector();
  46. }
  47. static atomic64_t last_value = ATOMIC64_INIT(0);
  48. void pvclock_resume(void)
  49. {
  50. atomic64_set(&last_value, 0);
  51. }
  52. u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
  53. {
  54. unsigned version;
  55. u8 flags;
  56. do {
  57. version = pvclock_read_begin(src);
  58. flags = src->flags;
  59. } while (pvclock_read_retry(src, version));
  60. return flags & valid_flags;
  61. }
  62. u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  63. {
  64. unsigned version;
  65. u64 ret;
  66. u64 last;
  67. u8 flags;
  68. do {
  69. version = pvclock_read_begin(src);
  70. ret = __pvclock_read_cycles(src, rdtsc_ordered());
  71. flags = src->flags;
  72. } while (pvclock_read_retry(src, version));
  73. if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
  74. src->flags &= ~PVCLOCK_GUEST_STOPPED;
  75. pvclock_touch_watchdogs();
  76. }
  77. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  78. (flags & PVCLOCK_TSC_STABLE_BIT))
  79. return ret;
  80. /*
  81. * Assumption here is that last_value, a global accumulator, always goes
  82. * forward. If we are less than that, we should not be much smaller.
  83. * We assume there is an error marging we're inside, and then the correction
  84. * does not sacrifice accuracy.
  85. *
  86. * For reads: global may have changed between test and return,
  87. * but this means someone else updated poked the clock at a later time.
  88. * We just need to make sure we are not seeing a backwards event.
  89. *
  90. * For updates: last_value = ret is not enough, since two vcpus could be
  91. * updating at the same time, and one of them could be slightly behind,
  92. * making the assumption that last_value always go forward fail to hold.
  93. */
  94. last = atomic64_read(&last_value);
  95. do {
  96. if (ret < last)
  97. return last;
  98. last = atomic64_cmpxchg(&last_value, last, ret);
  99. } while (unlikely(last != ret));
  100. return ret;
  101. }
  102. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  103. struct pvclock_vcpu_time_info *vcpu_time,
  104. struct timespec64 *ts)
  105. {
  106. u32 version;
  107. u64 delta;
  108. struct timespec64 now;
  109. /* get wallclock at system boot */
  110. do {
  111. version = wall_clock->version;
  112. rmb(); /* fetch version before time */
  113. /*
  114. * Note: wall_clock->sec is a u32 value, so it can
  115. * only store dates between 1970 and 2106. To allow
  116. * times beyond that, we need to create a new hypercall
  117. * interface with an extended pvclock_wall_clock structure
  118. * like ARM has.
  119. */
  120. now.tv_sec = wall_clock->sec;
  121. now.tv_nsec = wall_clock->nsec;
  122. rmb(); /* fetch time before checking version */
  123. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  124. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  125. delta += now.tv_sec * NSEC_PER_SEC + now.tv_nsec;
  126. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  127. now.tv_sec = delta;
  128. set_normalized_timespec64(ts, now.tv_sec, now.tv_nsec);
  129. }
  130. void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti)
  131. {
  132. WARN_ON(vclock_was_used(VCLOCK_PVCLOCK));
  133. pvti_cpu0_va = pvti;
  134. }
  135. struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
  136. {
  137. return pvti_cpu0_va;
  138. }
  139. EXPORT_SYMBOL_GPL(pvclock_get_pvti_cpu0_va);