pvclock.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <asm/fixmap.h>
  21. #include <asm/pvclock.h>
  22. static u8 valid_flags __read_mostly = 0;
  23. void pvclock_set_flags(u8 flags)
  24. {
  25. valid_flags = flags;
  26. }
  27. unsigned long pvclock_tsc_khz(struct pvclock_vcpu_time_info *src)
  28. {
  29. u64 pv_tsc_khz = 1000000ULL << 32;
  30. do_div(pv_tsc_khz, src->tsc_to_system_mul);
  31. if (src->tsc_shift < 0)
  32. pv_tsc_khz <<= -src->tsc_shift;
  33. else
  34. pv_tsc_khz >>= src->tsc_shift;
  35. return pv_tsc_khz;
  36. }
  37. static atomic64_t last_value = ATOMIC64_INIT(0);
  38. void pvclock_resume(void)
  39. {
  40. atomic64_set(&last_value, 0);
  41. }
  42. u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
  43. {
  44. unsigned version;
  45. cycle_t ret;
  46. u8 flags;
  47. do {
  48. version = __pvclock_read_cycles(src, &ret, &flags);
  49. } while ((src->version & 1) || version != src->version);
  50. return flags & valid_flags;
  51. }
  52. cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
  53. {
  54. unsigned version;
  55. cycle_t ret;
  56. u64 last;
  57. u8 flags;
  58. do {
  59. version = __pvclock_read_cycles(src, &ret, &flags);
  60. } while ((src->version & 1) || version != src->version);
  61. if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
  62. (flags & PVCLOCK_TSC_STABLE_BIT))
  63. return ret;
  64. /*
  65. * Assumption here is that last_value, a global accumulator, always goes
  66. * forward. If we are less than that, we should not be much smaller.
  67. * We assume there is an error marging we're inside, and then the correction
  68. * does not sacrifice accuracy.
  69. *
  70. * For reads: global may have changed between test and return,
  71. * but this means someone else updated poked the clock at a later time.
  72. * We just need to make sure we are not seeing a backwards event.
  73. *
  74. * For updates: last_value = ret is not enough, since two vcpus could be
  75. * updating at the same time, and one of them could be slightly behind,
  76. * making the assumption that last_value always go forward fail to hold.
  77. */
  78. last = atomic64_read(&last_value);
  79. do {
  80. if (ret < last)
  81. return last;
  82. last = atomic64_cmpxchg(&last_value, last, ret);
  83. } while (unlikely(last != ret));
  84. return ret;
  85. }
  86. void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,
  87. struct pvclock_vcpu_time_info *vcpu_time,
  88. struct timespec *ts)
  89. {
  90. u32 version;
  91. u64 delta;
  92. struct timespec now;
  93. /* get wallclock at system boot */
  94. do {
  95. version = wall_clock->version;
  96. rmb(); /* fetch version before time */
  97. now.tv_sec = wall_clock->sec;
  98. now.tv_nsec = wall_clock->nsec;
  99. rmb(); /* fetch time before checking version */
  100. } while ((wall_clock->version & 1) || (version != wall_clock->version));
  101. delta = pvclock_clocksource_read(vcpu_time); /* time since system boot */
  102. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  103. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  104. now.tv_sec = delta;
  105. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  106. }
  107. static struct pvclock_vsyscall_time_info *pvclock_vdso_info;
  108. static struct pvclock_vsyscall_time_info *
  109. pvclock_get_vsyscall_user_time_info(int cpu)
  110. {
  111. if (!pvclock_vdso_info) {
  112. BUG();
  113. return NULL;
  114. }
  115. return &pvclock_vdso_info[cpu];
  116. }
  117. struct pvclock_vcpu_time_info *pvclock_get_vsyscall_time_info(int cpu)
  118. {
  119. return &pvclock_get_vsyscall_user_time_info(cpu)->pvti;
  120. }
  121. #ifdef CONFIG_X86_64
  122. static int pvclock_task_migrate(struct notifier_block *nb, unsigned long l,
  123. void *v)
  124. {
  125. struct task_migration_notifier *mn = v;
  126. struct pvclock_vsyscall_time_info *pvti;
  127. pvti = pvclock_get_vsyscall_user_time_info(mn->from_cpu);
  128. /* this is NULL when pvclock vsyscall is not initialized */
  129. if (unlikely(pvti == NULL))
  130. return NOTIFY_DONE;
  131. pvti->migrate_count++;
  132. return NOTIFY_DONE;
  133. }
  134. static struct notifier_block pvclock_migrate = {
  135. .notifier_call = pvclock_task_migrate,
  136. };
  137. /*
  138. * Initialize the generic pvclock vsyscall state. This will allocate
  139. * a/some page(s) for the per-vcpu pvclock information, set up a
  140. * fixmap mapping for the page(s)
  141. */
  142. int __init pvclock_init_vsyscall(struct pvclock_vsyscall_time_info *i,
  143. int size)
  144. {
  145. int idx;
  146. WARN_ON (size != PVCLOCK_VSYSCALL_NR_PAGES*PAGE_SIZE);
  147. pvclock_vdso_info = i;
  148. for (idx = 0; idx <= (PVCLOCK_FIXMAP_END-PVCLOCK_FIXMAP_BEGIN); idx++) {
  149. __set_fixmap(PVCLOCK_FIXMAP_BEGIN + idx,
  150. __pa(i) + (idx*PAGE_SIZE),
  151. PAGE_KERNEL_VVAR);
  152. }
  153. register_task_migration_notifier(&pvclock_migrate);
  154. return 0;
  155. }
  156. #endif