kvmclock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. clear_sched_clock_stable();
  103. kvm_sched_clock_offset = kvm_clock_read();
  104. pv_time_ops.sched_clock = kvm_sched_clock_read;
  105. pr_info("kvm-clock: using sched offset of %llu cycles",
  106. kvm_sched_clock_offset);
  107. BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
  108. sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
  109. }
  110. /*
  111. * If we don't do that, there is the possibility that the guest
  112. * will calibrate under heavy load - thus, getting a lower lpj -
  113. * and execute the delays themselves without load. This is wrong,
  114. * because no delay loop can finish beforehand.
  115. * Any heuristics is subject to fail, because ultimately, a large
  116. * poll of guests can be running and trouble each other. So we preset
  117. * lpj here
  118. */
  119. static unsigned long kvm_get_tsc_khz(void)
  120. {
  121. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  122. return pvclock_tsc_khz(this_cpu_pvti());
  123. }
  124. static void __init kvm_get_preset_lpj(void)
  125. {
  126. unsigned long khz;
  127. u64 lpj;
  128. khz = kvm_get_tsc_khz();
  129. lpj = ((u64)khz * 1000);
  130. do_div(lpj, HZ);
  131. preset_lpj = lpj;
  132. }
  133. bool kvm_check_and_clear_guest_paused(void)
  134. {
  135. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  136. bool ret = false;
  137. if (!src)
  138. return ret;
  139. if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
  140. src->pvti.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. static void kvm_register_clock(char *txt)
  155. {
  156. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  157. u64 pa;
  158. if (!src)
  159. return;
  160. pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
  161. wrmsrl(msr_kvm_system_time, pa);
  162. pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
  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. kvm_register_clock("secondary cpu clock");
  175. }
  176. #endif
  177. /*
  178. * After the clock is registered, the host will keep writing to the
  179. * registered memory location. If the guest happens to shutdown, this memory
  180. * won't be valid. In cases like kexec, in which you install a new kernel, this
  181. * means a random memory location will be kept being written. So before any
  182. * kind of shutdown from our side, we unregister the clock by writing anything
  183. * that does not have the 'enable' bit set in the msr
  184. */
  185. #ifdef CONFIG_KEXEC_CORE
  186. static void kvm_crash_shutdown(struct pt_regs *regs)
  187. {
  188. native_write_msr(msr_kvm_system_time, 0, 0);
  189. kvm_disable_steal_time();
  190. native_machine_crash_shutdown(regs);
  191. }
  192. #endif
  193. static void kvm_shutdown(void)
  194. {
  195. native_write_msr(msr_kvm_system_time, 0, 0);
  196. kvm_disable_steal_time();
  197. native_machine_shutdown();
  198. }
  199. static void __init kvmclock_init_mem(void)
  200. {
  201. unsigned long ncpus;
  202. unsigned int order;
  203. struct page *p;
  204. int r;
  205. if (HVC_BOOT_ARRAY_SIZE >= num_possible_cpus())
  206. return;
  207. ncpus = num_possible_cpus() - HVC_BOOT_ARRAY_SIZE;
  208. order = get_order(ncpus * sizeof(*hvclock_mem));
  209. p = alloc_pages(GFP_KERNEL, order);
  210. if (!p) {
  211. pr_warn("%s: failed to alloc %d pages", __func__, (1U << order));
  212. return;
  213. }
  214. hvclock_mem = page_address(p);
  215. /*
  216. * hvclock is shared between the guest and the hypervisor, must
  217. * be mapped decrypted.
  218. */
  219. if (sev_active()) {
  220. r = set_memory_decrypted((unsigned long) hvclock_mem,
  221. 1UL << order);
  222. if (r) {
  223. __free_pages(p, order);
  224. hvclock_mem = NULL;
  225. pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
  226. return;
  227. }
  228. }
  229. memset(hvclock_mem, 0, PAGE_SIZE << order);
  230. }
  231. static int __init kvm_setup_vsyscall_timeinfo(void)
  232. {
  233. #ifdef CONFIG_X86_64
  234. u8 flags;
  235. if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
  236. return 0;
  237. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  238. if (!(flags & PVCLOCK_TSC_STABLE_BIT))
  239. return 0;
  240. kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
  241. #endif
  242. kvmclock_init_mem();
  243. return 0;
  244. }
  245. early_initcall(kvm_setup_vsyscall_timeinfo);
  246. static int kvmclock_setup_percpu(unsigned int cpu)
  247. {
  248. struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
  249. /*
  250. * The per cpu area setup replicates CPU0 data to all cpu
  251. * pointers. So carefully check. CPU0 has been set up in init
  252. * already.
  253. */
  254. if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
  255. return 0;
  256. /* Use the static page for the first CPUs, allocate otherwise */
  257. if (cpu < HVC_BOOT_ARRAY_SIZE)
  258. p = &hv_clock_boot[cpu];
  259. else if (hvclock_mem)
  260. p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
  261. else
  262. return -ENOMEM;
  263. per_cpu(hv_clock_per_cpu, cpu) = p;
  264. return p ? 0 : -ENOMEM;
  265. }
  266. void __init kvmclock_init(void)
  267. {
  268. u8 flags;
  269. if (!kvm_para_available() || !kvmclock)
  270. return;
  271. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
  272. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  273. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  274. } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  275. return;
  276. }
  277. if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
  278. kvmclock_setup_percpu, NULL) < 0) {
  279. return;
  280. }
  281. pr_info("kvm-clock: Using msrs %x and %x",
  282. msr_kvm_system_time, msr_kvm_wall_clock);
  283. this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
  284. kvm_register_clock("primary cpu clock");
  285. pvclock_set_pvti_cpu0_va(hv_clock_boot);
  286. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
  287. pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
  288. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  289. kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
  290. x86_platform.calibrate_tsc = kvm_get_tsc_khz;
  291. x86_platform.calibrate_cpu = kvm_get_tsc_khz;
  292. x86_platform.get_wallclock = kvm_get_wallclock;
  293. x86_platform.set_wallclock = kvm_set_wallclock;
  294. #ifdef CONFIG_X86_LOCAL_APIC
  295. x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
  296. #endif
  297. x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
  298. x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
  299. machine_ops.shutdown = kvm_shutdown;
  300. #ifdef CONFIG_KEXEC_CORE
  301. machine_ops.crash_shutdown = kvm_crash_shutdown;
  302. #endif
  303. kvm_get_preset_lpj();
  304. clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
  305. pv_info.name = "KVM";
  306. }