kvmclock.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <asm/reboot.h>
  22. #define KVM_SCALE 22
  23. static int kvmclock = 1;
  24. static int parse_no_kvmclock(char *arg)
  25. {
  26. kvmclock = 0;
  27. return 0;
  28. }
  29. early_param("no-kvmclock", parse_no_kvmclock);
  30. /* The hypervisor will put information about time periodically here */
  31. static DEFINE_PER_CPU_SHARED_ALIGNED(struct kvm_vcpu_time_info, hv_clock);
  32. #define get_clock(cpu, field) per_cpu(hv_clock, cpu).field
  33. static inline u64 kvm_get_delta(u64 last_tsc)
  34. {
  35. int cpu = smp_processor_id();
  36. u64 delta = native_read_tsc() - last_tsc;
  37. return (delta * get_clock(cpu, tsc_to_system_mul)) >> KVM_SCALE;
  38. }
  39. static struct kvm_wall_clock wall_clock;
  40. static cycle_t kvm_clock_read(void);
  41. /*
  42. * The wallclock is the time of day when we booted. Since then, some time may
  43. * have elapsed since the hypervisor wrote the data. So we try to account for
  44. * that with system time
  45. */
  46. unsigned long kvm_get_wallclock(void)
  47. {
  48. u32 wc_sec, wc_nsec;
  49. u64 delta;
  50. struct timespec ts;
  51. int version, nsec;
  52. int low, high;
  53. low = (int)__pa(&wall_clock);
  54. high = ((u64)__pa(&wall_clock) >> 32);
  55. delta = kvm_clock_read();
  56. native_write_msr(MSR_KVM_WALL_CLOCK, low, high);
  57. do {
  58. version = wall_clock.wc_version;
  59. rmb();
  60. wc_sec = wall_clock.wc_sec;
  61. wc_nsec = wall_clock.wc_nsec;
  62. rmb();
  63. } while ((wall_clock.wc_version != version) || (version & 1));
  64. delta = kvm_clock_read() - delta;
  65. delta += wc_nsec;
  66. nsec = do_div(delta, NSEC_PER_SEC);
  67. set_normalized_timespec(&ts, wc_sec + delta, nsec);
  68. /*
  69. * Of all mechanisms of time adjustment I've tested, this one
  70. * was the champion!
  71. */
  72. return ts.tv_sec + 1;
  73. }
  74. int kvm_set_wallclock(unsigned long now)
  75. {
  76. return 0;
  77. }
  78. /*
  79. * This is our read_clock function. The host puts an tsc timestamp each time
  80. * it updates a new time. Without the tsc adjustment, we can have a situation
  81. * in which a vcpu starts to run earlier (smaller system_time), but probes
  82. * time later (compared to another vcpu), leading to backwards time
  83. */
  84. static cycle_t kvm_clock_read(void)
  85. {
  86. u64 last_tsc, now;
  87. int cpu;
  88. preempt_disable();
  89. cpu = smp_processor_id();
  90. last_tsc = get_clock(cpu, tsc_timestamp);
  91. now = get_clock(cpu, system_time);
  92. now += kvm_get_delta(last_tsc);
  93. preempt_enable();
  94. return now;
  95. }
  96. static struct clocksource kvm_clock = {
  97. .name = "kvm-clock",
  98. .read = kvm_clock_read,
  99. .rating = 400,
  100. .mask = CLOCKSOURCE_MASK(64),
  101. .mult = 1 << KVM_SCALE,
  102. .shift = KVM_SCALE,
  103. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  104. };
  105. static int kvm_register_clock(void)
  106. {
  107. int cpu = smp_processor_id();
  108. int low, high;
  109. low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
  110. high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
  111. return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
  112. }
  113. #ifdef CONFIG_X86_LOCAL_APIC
  114. static void kvm_setup_secondary_clock(void)
  115. {
  116. /*
  117. * Now that the first cpu already had this clocksource initialized,
  118. * we shouldn't fail.
  119. */
  120. WARN_ON(kvm_register_clock());
  121. /* ok, done with our trickery, call native */
  122. setup_secondary_APIC_clock();
  123. }
  124. #endif
  125. /*
  126. * After the clock is registered, the host will keep writing to the
  127. * registered memory location. If the guest happens to shutdown, this memory
  128. * won't be valid. In cases like kexec, in which you install a new kernel, this
  129. * means a random memory location will be kept being written. So before any
  130. * kind of shutdown from our side, we unregister the clock by writting anything
  131. * that does not have the 'enable' bit set in the msr
  132. */
  133. #ifdef CONFIG_KEXEC
  134. static void kvm_crash_shutdown(struct pt_regs *regs)
  135. {
  136. native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
  137. native_machine_crash_shutdown(regs);
  138. }
  139. #endif
  140. static void kvm_shutdown(void)
  141. {
  142. native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
  143. native_machine_shutdown();
  144. }
  145. void __init kvmclock_init(void)
  146. {
  147. if (!kvm_para_available())
  148. return;
  149. if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  150. if (kvm_register_clock())
  151. return;
  152. pv_time_ops.get_wallclock = kvm_get_wallclock;
  153. pv_time_ops.set_wallclock = kvm_set_wallclock;
  154. pv_time_ops.sched_clock = kvm_clock_read;
  155. #ifdef CONFIG_X86_LOCAL_APIC
  156. pv_apic_ops.setup_secondary_clock = kvm_setup_secondary_clock;
  157. #endif
  158. machine_ops.shutdown = kvm_shutdown;
  159. #ifdef CONFIG_KEXEC
  160. machine_ops.crash_shutdown = kvm_crash_shutdown;
  161. #endif
  162. clocksource_register(&kvm_clock);
  163. }
  164. }