kvmclock.c 8.2 KB

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