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