kvmclock.c 8.3 KB

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