kvmclock.c 9.3 KB

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