kvmclock.c 8.2 KB

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