kvmclock.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <linux/hardirq.h>
  22. #include <linux/memblock.h>
  23. #include <asm/x86_init.h>
  24. #include <asm/reboot.h>
  25. static int kvmclock = 1;
  26. static int msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
  27. static int msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
  28. static int parse_no_kvmclock(char *arg)
  29. {
  30. kvmclock = 0;
  31. return 0;
  32. }
  33. early_param("no-kvmclock", parse_no_kvmclock);
  34. /* The hypervisor will put information about time periodically here */
  35. static struct pvclock_vsyscall_time_info *hv_clock;
  36. static struct pvclock_wall_clock wall_clock;
  37. /*
  38. * The wallclock is the time of day when we booted. Since then, some time may
  39. * have elapsed since the hypervisor wrote the data. So we try to account for
  40. * that with system time
  41. */
  42. static unsigned long kvm_get_wallclock(void)
  43. {
  44. struct pvclock_vcpu_time_info *vcpu_time;
  45. struct timespec ts;
  46. int low, high;
  47. int cpu;
  48. low = (int)__pa_symbol(&wall_clock);
  49. high = ((u64)__pa_symbol(&wall_clock) >> 32);
  50. native_write_msr(msr_kvm_wall_clock, low, high);
  51. preempt_disable();
  52. cpu = smp_processor_id();
  53. vcpu_time = &hv_clock[cpu].pvti;
  54. pvclock_read_wallclock(&wall_clock, vcpu_time, &ts);
  55. preempt_enable();
  56. return ts.tv_sec;
  57. }
  58. static int kvm_set_wallclock(unsigned long now)
  59. {
  60. return -1;
  61. }
  62. static cycle_t kvm_clock_read(void)
  63. {
  64. struct pvclock_vcpu_time_info *src;
  65. cycle_t ret;
  66. int cpu;
  67. preempt_disable_notrace();
  68. cpu = smp_processor_id();
  69. src = &hv_clock[cpu].pvti;
  70. ret = pvclock_clocksource_read(src);
  71. preempt_enable_notrace();
  72. return ret;
  73. }
  74. static cycle_t kvm_clock_get_cycles(struct clocksource *cs)
  75. {
  76. return kvm_clock_read();
  77. }
  78. /*
  79. * If we don't do that, there is the possibility that the guest
  80. * will calibrate under heavy load - thus, getting a lower lpj -
  81. * and execute the delays themselves without load. This is wrong,
  82. * because no delay loop can finish beforehand.
  83. * Any heuristics is subject to fail, because ultimately, a large
  84. * poll of guests can be running and trouble each other. So we preset
  85. * lpj here
  86. */
  87. static unsigned long kvm_get_tsc_khz(void)
  88. {
  89. struct pvclock_vcpu_time_info *src;
  90. int cpu;
  91. unsigned long tsc_khz;
  92. preempt_disable();
  93. cpu = smp_processor_id();
  94. src = &hv_clock[cpu].pvti;
  95. tsc_khz = pvclock_tsc_khz(src);
  96. preempt_enable();
  97. return tsc_khz;
  98. }
  99. static void kvm_get_preset_lpj(void)
  100. {
  101. unsigned long khz;
  102. u64 lpj;
  103. khz = kvm_get_tsc_khz();
  104. lpj = ((u64)khz * 1000);
  105. do_div(lpj, HZ);
  106. preset_lpj = lpj;
  107. }
  108. bool kvm_check_and_clear_guest_paused(void)
  109. {
  110. bool ret = false;
  111. struct pvclock_vcpu_time_info *src;
  112. int cpu = smp_processor_id();
  113. if (!hv_clock)
  114. return ret;
  115. src = &hv_clock[cpu].pvti;
  116. if ((src->flags & PVCLOCK_GUEST_STOPPED) != 0) {
  117. src->flags &= ~PVCLOCK_GUEST_STOPPED;
  118. ret = true;
  119. }
  120. return ret;
  121. }
  122. static struct clocksource kvm_clock = {
  123. .name = "kvm-clock",
  124. .read = kvm_clock_get_cycles,
  125. .rating = 400,
  126. .mask = CLOCKSOURCE_MASK(64),
  127. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  128. };
  129. int kvm_register_clock(char *txt)
  130. {
  131. int cpu = smp_processor_id();
  132. int low, high, ret;
  133. struct pvclock_vcpu_time_info *src = &hv_clock[cpu].pvti;
  134. low = (int)slow_virt_to_phys(src) | 1;
  135. high = ((u64)slow_virt_to_phys(src) >> 32);
  136. ret = native_write_msr_safe(msr_kvm_system_time, low, high);
  137. printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
  138. cpu, high, low, txt);
  139. return ret;
  140. }
  141. static void kvm_save_sched_clock_state(void)
  142. {
  143. }
  144. static void kvm_restore_sched_clock_state(void)
  145. {
  146. kvm_register_clock("primary cpu clock, resume");
  147. }
  148. #ifdef CONFIG_X86_LOCAL_APIC
  149. static void __cpuinit kvm_setup_secondary_clock(void)
  150. {
  151. /*
  152. * Now that the first cpu already had this clocksource initialized,
  153. * we shouldn't fail.
  154. */
  155. WARN_ON(kvm_register_clock("secondary cpu clock"));
  156. }
  157. #endif
  158. /*
  159. * After the clock is registered, the host will keep writing to the
  160. * registered memory location. If the guest happens to shutdown, this memory
  161. * won't be valid. In cases like kexec, in which you install a new kernel, this
  162. * means a random memory location will be kept being written. So before any
  163. * kind of shutdown from our side, we unregister the clock by writting anything
  164. * that does not have the 'enable' bit set in the msr
  165. */
  166. #ifdef CONFIG_KEXEC
  167. static void kvm_crash_shutdown(struct pt_regs *regs)
  168. {
  169. native_write_msr(msr_kvm_system_time, 0, 0);
  170. kvm_disable_steal_time();
  171. native_machine_crash_shutdown(regs);
  172. }
  173. #endif
  174. static void kvm_shutdown(void)
  175. {
  176. native_write_msr(msr_kvm_system_time, 0, 0);
  177. kvm_disable_steal_time();
  178. native_machine_shutdown();
  179. }
  180. void __init kvmclock_init(void)
  181. {
  182. unsigned long mem;
  183. int size;
  184. size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
  185. if (!kvm_para_available())
  186. return;
  187. if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
  188. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  189. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  190. } else if (!(kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)))
  191. return;
  192. printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
  193. msr_kvm_system_time, msr_kvm_wall_clock);
  194. mem = memblock_alloc(size, PAGE_SIZE);
  195. if (!mem)
  196. return;
  197. hv_clock = __va(mem);
  198. if (kvm_register_clock("boot clock")) {
  199. hv_clock = NULL;
  200. memblock_free(mem, size);
  201. return;
  202. }
  203. pv_time_ops.sched_clock = kvm_clock_read;
  204. x86_platform.calibrate_tsc = kvm_get_tsc_khz;
  205. x86_platform.get_wallclock = kvm_get_wallclock;
  206. x86_platform.set_wallclock = kvm_set_wallclock;
  207. #ifdef CONFIG_X86_LOCAL_APIC
  208. x86_cpuinit.early_percpu_clock_init =
  209. kvm_setup_secondary_clock;
  210. #endif
  211. x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
  212. x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
  213. machine_ops.shutdown = kvm_shutdown;
  214. #ifdef CONFIG_KEXEC
  215. machine_ops.crash_shutdown = kvm_crash_shutdown;
  216. #endif
  217. kvm_get_preset_lpj();
  218. clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
  219. pv_info.paravirt_enabled = 1;
  220. pv_info.name = "KVM";
  221. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
  222. pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
  223. }
  224. int __init kvm_setup_vsyscall_timeinfo(void)
  225. {
  226. #ifdef CONFIG_X86_64
  227. int cpu;
  228. int ret;
  229. u8 flags;
  230. struct pvclock_vcpu_time_info *vcpu_time;
  231. unsigned int size;
  232. size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
  233. preempt_disable();
  234. cpu = smp_processor_id();
  235. vcpu_time = &hv_clock[cpu].pvti;
  236. flags = pvclock_read_flags(vcpu_time);
  237. if (!(flags & PVCLOCK_TSC_STABLE_BIT)) {
  238. preempt_enable();
  239. return 1;
  240. }
  241. if ((ret = pvclock_init_vsyscall(hv_clock, size))) {
  242. preempt_enable();
  243. return ret;
  244. }
  245. preempt_enable();
  246. kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
  247. #endif
  248. return 0;
  249. }