kvmclock.c 6.0 KB

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