kvmclock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/cpuhotplug.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/clock.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/set_memory.h>
  28. #include <asm/hypervisor.h>
  29. #include <asm/mem_encrypt.h>
  30. #include <asm/x86_init.h>
  31. #include <asm/reboot.h>
  32. #include <asm/kvmclock.h>
  33. static int kvmclock __initdata = 1;
  34. static int kvmclock_vsyscall __initdata = 1;
  35. static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
  36. static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
  37. static u64 kvm_sched_clock_offset __ro_after_init;
  38. static int __init parse_no_kvmclock(char *arg)
  39. {
  40. kvmclock = 0;
  41. return 0;
  42. }
  43. early_param("no-kvmclock", parse_no_kvmclock);
  44. static int __init parse_no_kvmclock_vsyscall(char *arg)
  45. {
  46. kvmclock_vsyscall = 0;
  47. return 0;
  48. }
  49. early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
  50. /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
  51. #define HV_CLOCK_SIZE (sizeof(struct pvclock_vsyscall_time_info) * NR_CPUS)
  52. #define HVC_BOOT_ARRAY_SIZE \
  53. (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
  54. static struct pvclock_vsyscall_time_info
  55. hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
  56. static struct pvclock_wall_clock wall_clock __bss_decrypted;
  57. static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
  58. static struct pvclock_vsyscall_time_info *hvclock_mem;
  59. static inline struct pvclock_vcpu_time_info *this_cpu_pvti(void)
  60. {
  61. return &this_cpu_read(hv_clock_per_cpu)->pvti;
  62. }
  63. static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void)
  64. {
  65. return this_cpu_read(hv_clock_per_cpu);
  66. }
  67. /*
  68. * The wallclock is the time of day when we booted. Since then, some time may
  69. * have elapsed since the hypervisor wrote the data. So we try to account for
  70. * that with system time
  71. */
  72. static void kvm_get_wallclock(struct timespec64 *now)
  73. {
  74. wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
  75. preempt_disable();
  76. pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
  77. preempt_enable();
  78. }
  79. static int kvm_set_wallclock(const struct timespec64 *now)
  80. {
  81. return -ENODEV;
  82. }
  83. static u64 kvm_clock_read(void)
  84. {
  85. u64 ret;
  86. preempt_disable_notrace();
  87. ret = pvclock_clocksource_read(this_cpu_pvti());
  88. preempt_enable_notrace();
  89. return ret;
  90. }
  91. static u64 kvm_clock_get_cycles(struct clocksource *cs)
  92. {
  93. return kvm_clock_read();
  94. }
  95. static u64 kvm_sched_clock_read(void)
  96. {
  97. return kvm_clock_read() - kvm_sched_clock_offset;
  98. }
  99. static inline void kvm_sched_clock_init(bool stable)
  100. {
  101. if (!stable) {
  102. pv_time_ops.sched_clock = kvm_clock_read;
  103. clear_sched_clock_stable();
  104. return;
  105. }
  106. kvm_sched_clock_offset = kvm_clock_read();
  107. pv_time_ops.sched_clock = kvm_sched_clock_read;
  108. pr_info("kvm-clock: using sched offset of %llu cycles",
  109. kvm_sched_clock_offset);
  110. BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
  111. sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
  112. }
  113. /*
  114. * If we don't do that, there is the possibility that the guest
  115. * will calibrate under heavy load - thus, getting a lower lpj -
  116. * and execute the delays themselves without load. This is wrong,
  117. * because no delay loop can finish beforehand.
  118. * Any heuristics is subject to fail, because ultimately, a large
  119. * poll of guests can be running and trouble each other. So we preset
  120. * lpj here
  121. */
  122. static unsigned long kvm_get_tsc_khz(void)
  123. {
  124. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  125. return pvclock_tsc_khz(this_cpu_pvti());
  126. }
  127. static void __init kvm_get_preset_lpj(void)
  128. {
  129. unsigned long khz;
  130. u64 lpj;
  131. khz = kvm_get_tsc_khz();
  132. lpj = ((u64)khz * 1000);
  133. do_div(lpj, HZ);
  134. preset_lpj = lpj;
  135. }
  136. bool kvm_check_and_clear_guest_paused(void)
  137. {
  138. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  139. bool ret = false;
  140. if (!src)
  141. return ret;
  142. if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
  143. src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
  144. pvclock_touch_watchdogs();
  145. ret = true;
  146. }
  147. return ret;
  148. }
  149. struct clocksource kvm_clock = {
  150. .name = "kvm-clock",
  151. .read = kvm_clock_get_cycles,
  152. .rating = 400,
  153. .mask = CLOCKSOURCE_MASK(64),
  154. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  155. };
  156. EXPORT_SYMBOL_GPL(kvm_clock);
  157. static void kvm_register_clock(char *txt)
  158. {
  159. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  160. u64 pa;
  161. if (!src)
  162. return;
  163. pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
  164. wrmsrl(msr_kvm_system_time, pa);
  165. pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
  166. }
  167. static void kvm_save_sched_clock_state(void)
  168. {
  169. }
  170. static void kvm_restore_sched_clock_state(void)
  171. {
  172. kvm_register_clock("primary cpu clock, resume");
  173. }
  174. #ifdef CONFIG_X86_LOCAL_APIC
  175. static void kvm_setup_secondary_clock(void)
  176. {
  177. kvm_register_clock("secondary cpu clock");
  178. }
  179. #endif
  180. /*
  181. * After the clock is registered, the host will keep writing to the
  182. * registered memory location. If the guest happens to shutdown, this memory
  183. * won't be valid. In cases like kexec, in which you install a new kernel, this
  184. * means a random memory location will be kept being written. So before any
  185. * kind of shutdown from our side, we unregister the clock by writing anything
  186. * that does not have the 'enable' bit set in the msr
  187. */
  188. #ifdef CONFIG_KEXEC_CORE
  189. static void kvm_crash_shutdown(struct pt_regs *regs)
  190. {
  191. native_write_msr(msr_kvm_system_time, 0, 0);
  192. kvm_disable_steal_time();
  193. native_machine_crash_shutdown(regs);
  194. }
  195. #endif
  196. static void kvm_shutdown(void)
  197. {
  198. native_write_msr(msr_kvm_system_time, 0, 0);
  199. kvm_disable_steal_time();
  200. native_machine_shutdown();
  201. }
  202. static void __init kvmclock_init_mem(void)
  203. {
  204. unsigned long ncpus;
  205. unsigned int order;
  206. struct page *p;
  207. int r;
  208. if (HVC_BOOT_ARRAY_SIZE >= num_possible_cpus())
  209. return;
  210. ncpus = num_possible_cpus() - HVC_BOOT_ARRAY_SIZE;
  211. order = get_order(ncpus * sizeof(*hvclock_mem));
  212. p = alloc_pages(GFP_KERNEL, order);
  213. if (!p) {
  214. pr_warn("%s: failed to alloc %d pages", __func__, (1U << order));
  215. return;
  216. }
  217. hvclock_mem = page_address(p);
  218. /*
  219. * hvclock is shared between the guest and the hypervisor, must
  220. * be mapped decrypted.
  221. */
  222. if (sev_active()) {
  223. r = set_memory_decrypted((unsigned long) hvclock_mem,
  224. 1UL << order);
  225. if (r) {
  226. __free_pages(p, order);
  227. hvclock_mem = NULL;
  228. pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
  229. return;
  230. }
  231. }
  232. memset(hvclock_mem, 0, PAGE_SIZE << order);
  233. }
  234. static int __init kvm_setup_vsyscall_timeinfo(void)
  235. {
  236. #ifdef CONFIG_X86_64
  237. u8 flags;
  238. if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
  239. return 0;
  240. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  241. if (!(flags & PVCLOCK_TSC_STABLE_BIT))
  242. return 0;
  243. kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
  244. #endif
  245. kvmclock_init_mem();
  246. return 0;
  247. }
  248. early_initcall(kvm_setup_vsyscall_timeinfo);
  249. static int kvmclock_setup_percpu(unsigned int cpu)
  250. {
  251. struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
  252. /*
  253. * The per cpu area setup replicates CPU0 data to all cpu
  254. * pointers. So carefully check. CPU0 has been set up in init
  255. * already.
  256. */
  257. if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
  258. return 0;
  259. /* Use the static page for the first CPUs, allocate otherwise */
  260. if (cpu < HVC_BOOT_ARRAY_SIZE)
  261. p = &hv_clock_boot[cpu];
  262. else if (hvclock_mem)
  263. p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
  264. else
  265. return -ENOMEM;
  266. per_cpu(hv_clock_per_cpu, cpu) = p;
  267. return p ? 0 : -ENOMEM;
  268. }
  269. void __init kvmclock_init(void)
  270. {
  271. u8 flags;
  272. if (!kvm_para_available() || !kvmclock)
  273. return;
  274. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
  275. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  276. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  277. } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  278. return;
  279. }
  280. if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
  281. kvmclock_setup_percpu, NULL) < 0) {
  282. return;
  283. }
  284. pr_info("kvm-clock: Using msrs %x and %x",
  285. msr_kvm_system_time, msr_kvm_wall_clock);
  286. this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
  287. kvm_register_clock("primary cpu clock");
  288. pvclock_set_pvti_cpu0_va(hv_clock_boot);
  289. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
  290. pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
  291. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  292. kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
  293. x86_platform.calibrate_tsc = kvm_get_tsc_khz;
  294. x86_platform.calibrate_cpu = kvm_get_tsc_khz;
  295. x86_platform.get_wallclock = kvm_get_wallclock;
  296. x86_platform.set_wallclock = kvm_set_wallclock;
  297. #ifdef CONFIG_X86_LOCAL_APIC
  298. x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
  299. #endif
  300. x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
  301. x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
  302. machine_ops.shutdown = kvm_shutdown;
  303. #ifdef CONFIG_KEXEC_CORE
  304. machine_ops.crash_shutdown = kvm_crash_shutdown;
  305. #endif
  306. kvm_get_preset_lpj();
  307. clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
  308. pv_info.name = "KVM";
  309. }