kvmclock.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* KVM paravirtual clock driver. A clocksource implementation
  3. Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
  4. */
  5. #include <linux/clocksource.h>
  6. #include <linux/kvm_para.h>
  7. #include <asm/pvclock.h>
  8. #include <asm/msr.h>
  9. #include <asm/apic.h>
  10. #include <linux/percpu.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/cpuhotplug.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/clock.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/set_memory.h>
  18. #include <asm/hypervisor.h>
  19. #include <asm/mem_encrypt.h>
  20. #include <asm/x86_init.h>
  21. #include <asm/kvmclock.h>
  22. static int kvmclock __initdata = 1;
  23. static int kvmclock_vsyscall __initdata = 1;
  24. static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
  25. static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
  26. static u64 kvm_sched_clock_offset __ro_after_init;
  27. static int __init parse_no_kvmclock(char *arg)
  28. {
  29. kvmclock = 0;
  30. return 0;
  31. }
  32. early_param("no-kvmclock", parse_no_kvmclock);
  33. static int __init parse_no_kvmclock_vsyscall(char *arg)
  34. {
  35. kvmclock_vsyscall = 0;
  36. return 0;
  37. }
  38. early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
  39. /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
  40. #define HV_CLOCK_SIZE (sizeof(struct pvclock_vsyscall_time_info) * NR_CPUS)
  41. #define HVC_BOOT_ARRAY_SIZE \
  42. (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
  43. static struct pvclock_vsyscall_time_info
  44. hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
  45. static struct pvclock_wall_clock wall_clock __bss_decrypted;
  46. static struct pvclock_vsyscall_time_info *hvclock_mem;
  47. DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
  48. EXPORT_PER_CPU_SYMBOL_GPL(hv_clock_per_cpu);
  49. /*
  50. * The wallclock is the time of day when we booted. Since then, some time may
  51. * have elapsed since the hypervisor wrote the data. So we try to account for
  52. * that with system time
  53. */
  54. static void kvm_get_wallclock(struct timespec64 *now)
  55. {
  56. wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
  57. preempt_disable();
  58. pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
  59. preempt_enable();
  60. }
  61. static int kvm_set_wallclock(const struct timespec64 *now)
  62. {
  63. return -ENODEV;
  64. }
  65. static u64 kvm_clock_read(void)
  66. {
  67. u64 ret;
  68. preempt_disable_notrace();
  69. ret = pvclock_clocksource_read(this_cpu_pvti());
  70. preempt_enable_notrace();
  71. return ret;
  72. }
  73. static u64 kvm_clock_get_cycles(struct clocksource *cs)
  74. {
  75. return kvm_clock_read();
  76. }
  77. static u64 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. clear_sched_clock_stable();
  85. kvm_sched_clock_offset = kvm_clock_read();
  86. pv_ops.time.sched_clock = kvm_sched_clock_read;
  87. pr_info("kvm-clock: using sched offset of %llu cycles",
  88. kvm_sched_clock_offset);
  89. BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
  90. sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
  91. }
  92. /*
  93. * If we don't do that, there is the possibility that the guest
  94. * will calibrate under heavy load - thus, getting a lower lpj -
  95. * and execute the delays themselves without load. This is wrong,
  96. * because no delay loop can finish beforehand.
  97. * Any heuristics is subject to fail, because ultimately, a large
  98. * poll of guests can be running and trouble each other. So we preset
  99. * lpj here
  100. */
  101. static unsigned long kvm_get_tsc_khz(void)
  102. {
  103. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  104. return pvclock_tsc_khz(this_cpu_pvti());
  105. }
  106. static void __init kvm_get_preset_lpj(void)
  107. {
  108. unsigned long khz;
  109. u64 lpj;
  110. khz = kvm_get_tsc_khz();
  111. lpj = ((u64)khz * 1000);
  112. do_div(lpj, HZ);
  113. preset_lpj = lpj;
  114. }
  115. bool kvm_check_and_clear_guest_paused(void)
  116. {
  117. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  118. bool ret = false;
  119. if (!src)
  120. return ret;
  121. if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
  122. src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
  123. pvclock_touch_watchdogs();
  124. ret = true;
  125. }
  126. return ret;
  127. }
  128. static int kvm_cs_enable(struct clocksource *cs)
  129. {
  130. vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
  131. return 0;
  132. }
  133. struct clocksource kvm_clock = {
  134. .name = "kvm-clock",
  135. .read = kvm_clock_get_cycles,
  136. .rating = 400,
  137. .mask = CLOCKSOURCE_MASK(64),
  138. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  139. .enable = kvm_cs_enable,
  140. };
  141. EXPORT_SYMBOL_GPL(kvm_clock);
  142. static void kvm_register_clock(char *txt)
  143. {
  144. struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
  145. u64 pa;
  146. if (!src)
  147. return;
  148. pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
  149. wrmsrl(msr_kvm_system_time, pa);
  150. pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
  151. }
  152. static void kvm_save_sched_clock_state(void)
  153. {
  154. }
  155. static void kvm_restore_sched_clock_state(void)
  156. {
  157. kvm_register_clock("primary cpu clock, resume");
  158. }
  159. #ifdef CONFIG_X86_LOCAL_APIC
  160. static void kvm_setup_secondary_clock(void)
  161. {
  162. kvm_register_clock("secondary cpu clock");
  163. }
  164. #endif
  165. void kvmclock_disable(void)
  166. {
  167. native_write_msr(msr_kvm_system_time, 0, 0);
  168. }
  169. static void __init kvmclock_init_mem(void)
  170. {
  171. unsigned long ncpus;
  172. unsigned int order;
  173. struct page *p;
  174. int r;
  175. if (HVC_BOOT_ARRAY_SIZE >= num_possible_cpus())
  176. return;
  177. ncpus = num_possible_cpus() - HVC_BOOT_ARRAY_SIZE;
  178. order = get_order(ncpus * sizeof(*hvclock_mem));
  179. p = alloc_pages(GFP_KERNEL, order);
  180. if (!p) {
  181. pr_warn("%s: failed to alloc %d pages", __func__, (1U << order));
  182. return;
  183. }
  184. hvclock_mem = page_address(p);
  185. /*
  186. * hvclock is shared between the guest and the hypervisor, must
  187. * be mapped decrypted.
  188. */
  189. if (sev_active()) {
  190. r = set_memory_decrypted((unsigned long) hvclock_mem,
  191. 1UL << order);
  192. if (r) {
  193. __free_pages(p, order);
  194. hvclock_mem = NULL;
  195. pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
  196. return;
  197. }
  198. }
  199. memset(hvclock_mem, 0, PAGE_SIZE << order);
  200. }
  201. static int __init kvm_setup_vsyscall_timeinfo(void)
  202. {
  203. kvmclock_init_mem();
  204. #ifdef CONFIG_X86_64
  205. if (per_cpu(hv_clock_per_cpu, 0) && kvmclock_vsyscall) {
  206. u8 flags;
  207. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  208. if (!(flags & PVCLOCK_TSC_STABLE_BIT))
  209. return 0;
  210. kvm_clock.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
  211. }
  212. #endif
  213. return 0;
  214. }
  215. early_initcall(kvm_setup_vsyscall_timeinfo);
  216. static int kvmclock_setup_percpu(unsigned int cpu)
  217. {
  218. struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
  219. /*
  220. * The per cpu area setup replicates CPU0 data to all cpu
  221. * pointers. So carefully check. CPU0 has been set up in init
  222. * already.
  223. */
  224. if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
  225. return 0;
  226. /* Use the static page for the first CPUs, allocate otherwise */
  227. if (cpu < HVC_BOOT_ARRAY_SIZE)
  228. p = &hv_clock_boot[cpu];
  229. else if (hvclock_mem)
  230. p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
  231. else
  232. return -ENOMEM;
  233. per_cpu(hv_clock_per_cpu, cpu) = p;
  234. return p ? 0 : -ENOMEM;
  235. }
  236. void __init kvmclock_init(void)
  237. {
  238. u8 flags;
  239. if (!kvm_para_available() || !kvmclock)
  240. return;
  241. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
  242. msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
  243. msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
  244. } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
  245. return;
  246. }
  247. if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
  248. kvmclock_setup_percpu, NULL) < 0) {
  249. return;
  250. }
  251. pr_info("kvm-clock: Using msrs %x and %x",
  252. msr_kvm_system_time, msr_kvm_wall_clock);
  253. this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
  254. kvm_register_clock("primary cpu clock");
  255. pvclock_set_pvti_cpu0_va(hv_clock_boot);
  256. if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
  257. pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
  258. flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
  259. kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
  260. x86_platform.calibrate_tsc = kvm_get_tsc_khz;
  261. x86_platform.calibrate_cpu = kvm_get_tsc_khz;
  262. x86_platform.get_wallclock = kvm_get_wallclock;
  263. x86_platform.set_wallclock = kvm_set_wallclock;
  264. #ifdef CONFIG_X86_LOCAL_APIC
  265. x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
  266. #endif
  267. x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
  268. x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
  269. kvm_get_preset_lpj();
  270. /*
  271. * X86_FEATURE_NONSTOP_TSC is TSC runs at constant rate
  272. * with P/T states and does not stop in deep C-states.
  273. *
  274. * Invariant TSC exposed by host means kvmclock is not necessary:
  275. * can use TSC as clocksource.
  276. *
  277. */
  278. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
  279. boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
  280. !check_tsc_unstable())
  281. kvm_clock.rating = 299;
  282. clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
  283. pv_info.name = "KVM";
  284. }