kvmclock.c 8.2 KB

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