kvmclock.c 8.3 KB

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