kvmclock.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* KVM paravirtual clock driver. A clocksource implementation
  2. Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <linux/clocksource.h>
  16. #include <linux/kvm_para.h>
  17. #include <asm/arch_hooks.h>
  18. #include <asm/msr.h>
  19. #include <asm/apic.h>
  20. #include <linux/percpu.h>
  21. #define KVM_SCALE 22
  22. static int kvmclock = 1;
  23. static int parse_no_kvmclock(char *arg)
  24. {
  25. kvmclock = 0;
  26. return 0;
  27. }
  28. early_param("no-kvmclock", parse_no_kvmclock);
  29. /* The hypervisor will put information about time periodically here */
  30. static DEFINE_PER_CPU_SHARED_ALIGNED(struct kvm_vcpu_time_info, hv_clock);
  31. #define get_clock(cpu, field) per_cpu(hv_clock, cpu).field
  32. static inline u64 kvm_get_delta(u64 last_tsc)
  33. {
  34. int cpu = smp_processor_id();
  35. u64 delta = native_read_tsc() - last_tsc;
  36. return (delta * get_clock(cpu, tsc_to_system_mul)) >> KVM_SCALE;
  37. }
  38. static struct kvm_wall_clock wall_clock;
  39. static cycle_t kvm_clock_read(void);
  40. /*
  41. * The wallclock is the time of day when we booted. Since then, some time may
  42. * have elapsed since the hypervisor wrote the data. So we try to account for
  43. * that with system time
  44. */
  45. unsigned long kvm_get_wallclock(void)
  46. {
  47. u32 wc_sec, wc_nsec;
  48. u64 delta;
  49. struct timespec ts;
  50. int version, nsec;
  51. int low, high;
  52. low = (int)__pa(&wall_clock);
  53. high = ((u64)__pa(&wall_clock) >> 32);
  54. delta = kvm_clock_read();
  55. native_write_msr(MSR_KVM_WALL_CLOCK, low, high);
  56. do {
  57. version = wall_clock.wc_version;
  58. rmb();
  59. wc_sec = wall_clock.wc_sec;
  60. wc_nsec = wall_clock.wc_nsec;
  61. rmb();
  62. } while ((wall_clock.wc_version != version) || (version & 1));
  63. delta = kvm_clock_read() - delta;
  64. delta += wc_nsec;
  65. nsec = do_div(delta, NSEC_PER_SEC);
  66. set_normalized_timespec(&ts, wc_sec + delta, nsec);
  67. /*
  68. * Of all mechanisms of time adjustment I've tested, this one
  69. * was the champion!
  70. */
  71. return ts.tv_sec + 1;
  72. }
  73. int kvm_set_wallclock(unsigned long now)
  74. {
  75. return 0;
  76. }
  77. /*
  78. * This is our read_clock function. The host puts an tsc timestamp each time
  79. * it updates a new time. Without the tsc adjustment, we can have a situation
  80. * in which a vcpu starts to run earlier (smaller system_time), but probes
  81. * time later (compared to another vcpu), leading to backwards time
  82. */
  83. static cycle_t kvm_clock_read(void)
  84. {
  85. u64 last_tsc, now;
  86. int cpu;
  87. preempt_disable();
  88. cpu = smp_processor_id();
  89. last_tsc = get_clock(cpu, tsc_timestamp);
  90. now = get_clock(cpu, system_time);
  91. now += kvm_get_delta(last_tsc);
  92. preempt_enable();
  93. return now;
  94. }
  95. static struct clocksource kvm_clock = {
  96. .name = "kvm-clock",
  97. .read = kvm_clock_read,
  98. .rating = 400,
  99. .mask = CLOCKSOURCE_MASK(64),
  100. .mult = 1 << KVM_SCALE,
  101. .shift = KVM_SCALE,
  102. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  103. };
  104. static int kvm_register_clock(void)
  105. {
  106. int cpu = smp_processor_id();
  107. int low, high;
  108. low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
  109. high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
  110. return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
  111. }
  112. static void kvm_setup_secondary_clock(void)
  113. {
  114. /*
  115. * Now that the first cpu already had this clocksource initialized,
  116. * we shouldn't fail.
  117. */
  118. WARN_ON(kvm_register_clock());
  119. /* ok, done with our trickery, call native */
  120. setup_secondary_APIC_clock();
  121. }
  122. void __init kvmclock_init(void)
  123. {
  124. if (!kvm_para_available())
  125. return;
  126. if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  127. if (kvm_register_clock())
  128. return;
  129. pv_time_ops.get_wallclock = kvm_get_wallclock;
  130. pv_time_ops.set_wallclock = kvm_set_wallclock;
  131. pv_time_ops.sched_clock = kvm_clock_read;
  132. pv_apic_ops.setup_secondary_clock = kvm_setup_secondary_clock;
  133. clocksource_register(&kvm_clock);
  134. }
  135. }