kvmclock.c 8.5 KB

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