kvmclock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/pvclock.h>
  18. #include <asm/msr.h>
  19. #include <asm/apic.h>
  20. #include <linux/percpu.h>
  21. #include <asm/x86_init.h>
  22. #include <asm/reboot.h>
  23. static int kvmclock = 1;
  24. static int msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
  25. static int msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
  26. static int parse_no_kvmclock(char *arg)
  27. {
  28. kvmclock = 0;
  29. return 0;
  30. }
  31. early_param("no-kvmclock", parse_no_kvmclock);
  32. /* The hypervisor will put information about time periodically here */
  33. static DEFINE_PER_CPU_SHARED_ALIGNED(struct pvclock_vcpu_time_info, hv_clock);
  34. static struct pvclock_wall_clock wall_clock;
  35. /*
  36. * The wallclock is the time of day when we booted. Since then, some time may
  37. * have elapsed since the hypervisor wrote the data. So we try to account for
  38. * that with system time
  39. */
  40. static unsigned long kvm_get_wallclock(void)
  41. {
  42. struct pvclock_vcpu_time_info *vcpu_time;
  43. struct timespec ts;
  44. int low, high;
  45. low = (int)__pa_symbol(&wall_clock);
  46. high = ((u64)__pa_symbol(&wall_clock) >> 32);
  47. native_write_msr(msr_kvm_wall_clock, low, high);
  48. vcpu_time = &get_cpu_var(hv_clock);
  49. pvclock_read_wallclock(&wall_clock, vcpu_time, &ts);
  50. put_cpu_var(hv_clock);
  51. return ts.tv_sec;
  52. }
  53. static int kvm_set_wallclock(unsigned long now)
  54. {
  55. return -1;
  56. }
  57. static cycle_t kvm_clock_read(void)
  58. {
  59. struct pvclock_vcpu_time_info *src;
  60. cycle_t ret;
  61. src = &get_cpu_var(hv_clock);
  62. ret = pvclock_clocksource_read(src);
  63. put_cpu_var(hv_clock);
  64. return ret;
  65. }
  66. static cycle_t kvm_clock_get_cycles(struct clocksource *cs)
  67. {
  68. return kvm_clock_read();
  69. }
  70. /*
  71. * If we don't do that, there is the possibility that the guest
  72. * will calibrate under heavy load - thus, getting a lower lpj -
  73. * and execute the delays themselves without load. This is wrong,
  74. * because no delay loop can finish beforehand.
  75. * Any heuristics is subject to fail, because ultimately, a large
  76. * poll of guests can be running and trouble each other. So we preset
  77. * lpj here
  78. */
  79. static unsigned long kvm_get_tsc_khz(void)
  80. {
  81. struct pvclock_vcpu_time_info *src;
  82. src = &per_cpu(hv_clock, 0);
  83. return pvclock_tsc_khz(src);
  84. }
  85. static void kvm_get_preset_lpj(void)
  86. {
  87. unsigned long khz;
  88. u64 lpj;
  89. khz = kvm_get_tsc_khz();
  90. lpj = ((u64)khz * 1000);
  91. do_div(lpj, HZ);
  92. preset_lpj = lpj;
  93. }
  94. static struct clocksource kvm_clock = {
  95. .name = "kvm-clock",
  96. .read = kvm_clock_get_cycles,
  97. .rating = 400,
  98. .mask = CLOCKSOURCE_MASK(64),
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. int kvm_register_clock(char *txt)
  102. {
  103. int cpu = smp_processor_id();
  104. int low, high, ret;
  105. low = (int)__pa(&per_cpu(hv_clock, cpu)) | 1;
  106. high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
  107. ret = native_write_msr_safe(msr_kvm_system_time, low, high);
  108. printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
  109. cpu, high, low, txt);
  110. return ret;
  111. }
  112. #ifdef CONFIG_X86_LOCAL_APIC
  113. static void __cpuinit kvm_setup_secondary_clock(void)
  114. {
  115. /*
  116. * Now that the first cpu already had this clocksource initialized,
  117. * we shouldn't fail.
  118. */
  119. WARN_ON(kvm_register_clock("secondary cpu clock"));
  120. /* ok, done with our trickery, call native */
  121. setup_secondary_APIC_clock();
  122. }
  123. #endif
  124. /*
  125. * After the clock is registered, the host will keep writing to the
  126. * registered memory location. If the guest happens to shutdown, this memory
  127. * won't be valid. In cases like kexec, in which you install a new kernel, this
  128. * means a random memory location will be kept being written. So before any
  129. * kind of shutdown from our side, we unregister the clock by writting anything
  130. * that does not have the 'enable' bit set in the msr
  131. */
  132. #ifdef CONFIG_KEXEC
  133. static void kvm_crash_shutdown(struct pt_regs *regs)
  134. {
  135. native_write_msr(msr_kvm_system_time, 0, 0);
  136. kvm_disable_steal_time();
  137. native_machine_crash_shutdown(regs);
  138. }
  139. #endif
  140. static void kvm_shutdown(void)
  141. {
  142. native_write_msr(msr_kvm_system_time, 0, 0);
  143. kvm_disable_steal_time();
  144. native_machine_shutdown();
  145. }
  146. void __init kvmclock_init(void)
  147. {
  148. if (!kvm_para_available())
  149. return;
  150. if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
  151. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  152. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  153. } else if (!(kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)))
  154. return;
  155. printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
  156. msr_kvm_system_time, msr_kvm_wall_clock);
  157. if (kvm_register_clock("boot clock"))
  158. return;
  159. pv_time_ops.sched_clock = kvm_clock_read;
  160. x86_platform.calibrate_tsc = kvm_get_tsc_khz;
  161. x86_platform.get_wallclock = kvm_get_wallclock;
  162. x86_platform.set_wallclock = kvm_set_wallclock;
  163. #ifdef CONFIG_X86_LOCAL_APIC
  164. x86_cpuinit.setup_percpu_clockev =
  165. kvm_setup_secondary_clock;
  166. #endif
  167. machine_ops.shutdown = kvm_shutdown;
  168. #ifdef CONFIG_KEXEC
  169. machine_ops.crash_shutdown = kvm_crash_shutdown;
  170. #endif
  171. kvm_get_preset_lpj();
  172. clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
  173. pv_info.paravirt_enabled = 1;
  174. pv_info.name = "KVM";
  175. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
  176. pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
  177. }