kvmclock.c 5.4 KB

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