Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*  KVM paravirtual clock driver. A clocksource implementation
0003     Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
0004 */
0005 
0006 #include <linux/clocksource.h>
0007 #include <linux/kvm_para.h>
0008 #include <asm/pvclock.h>
0009 #include <asm/msr.h>
0010 #include <asm/apic.h>
0011 #include <linux/percpu.h>
0012 #include <linux/hardirq.h>
0013 #include <linux/cpuhotplug.h>
0014 #include <linux/sched.h>
0015 #include <linux/sched/clock.h>
0016 #include <linux/mm.h>
0017 #include <linux/slab.h>
0018 #include <linux/set_memory.h>
0019 #include <linux/cc_platform.h>
0020 
0021 #include <asm/hypervisor.h>
0022 #include <asm/x86_init.h>
0023 #include <asm/kvmclock.h>
0024 
0025 static int kvmclock __initdata = 1;
0026 static int kvmclock_vsyscall __initdata = 1;
0027 static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
0028 static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
0029 static u64 kvm_sched_clock_offset __ro_after_init;
0030 
0031 static int __init parse_no_kvmclock(char *arg)
0032 {
0033     kvmclock = 0;
0034     return 0;
0035 }
0036 early_param("no-kvmclock", parse_no_kvmclock);
0037 
0038 static int __init parse_no_kvmclock_vsyscall(char *arg)
0039 {
0040     kvmclock_vsyscall = 0;
0041     return 0;
0042 }
0043 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
0044 
0045 /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
0046 #define HVC_BOOT_ARRAY_SIZE \
0047     (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
0048 
0049 static struct pvclock_vsyscall_time_info
0050             hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __bss_decrypted __aligned(PAGE_SIZE);
0051 static struct pvclock_wall_clock wall_clock __bss_decrypted;
0052 static struct pvclock_vsyscall_time_info *hvclock_mem;
0053 DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
0054 EXPORT_PER_CPU_SYMBOL_GPL(hv_clock_per_cpu);
0055 
0056 /*
0057  * The wallclock is the time of day when we booted. Since then, some time may
0058  * have elapsed since the hypervisor wrote the data. So we try to account for
0059  * that with system time
0060  */
0061 static void kvm_get_wallclock(struct timespec64 *now)
0062 {
0063     wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
0064     preempt_disable();
0065     pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
0066     preempt_enable();
0067 }
0068 
0069 static int kvm_set_wallclock(const struct timespec64 *now)
0070 {
0071     return -ENODEV;
0072 }
0073 
0074 static u64 kvm_clock_read(void)
0075 {
0076     u64 ret;
0077 
0078     preempt_disable_notrace();
0079     ret = pvclock_clocksource_read(this_cpu_pvti());
0080     preempt_enable_notrace();
0081     return ret;
0082 }
0083 
0084 static u64 kvm_clock_get_cycles(struct clocksource *cs)
0085 {
0086     return kvm_clock_read();
0087 }
0088 
0089 static u64 kvm_sched_clock_read(void)
0090 {
0091     return kvm_clock_read() - kvm_sched_clock_offset;
0092 }
0093 
0094 static inline void kvm_sched_clock_init(bool stable)
0095 {
0096     if (!stable)
0097         clear_sched_clock_stable();
0098     kvm_sched_clock_offset = kvm_clock_read();
0099     paravirt_set_sched_clock(kvm_sched_clock_read);
0100 
0101     pr_info("kvm-clock: using sched offset of %llu cycles",
0102         kvm_sched_clock_offset);
0103 
0104     BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
0105         sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
0106 }
0107 
0108 /*
0109  * If we don't do that, there is the possibility that the guest
0110  * will calibrate under heavy load - thus, getting a lower lpj -
0111  * and execute the delays themselves without load. This is wrong,
0112  * because no delay loop can finish beforehand.
0113  * Any heuristics is subject to fail, because ultimately, a large
0114  * poll of guests can be running and trouble each other. So we preset
0115  * lpj here
0116  */
0117 static unsigned long kvm_get_tsc_khz(void)
0118 {
0119     setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
0120     return pvclock_tsc_khz(this_cpu_pvti());
0121 }
0122 
0123 static void __init kvm_get_preset_lpj(void)
0124 {
0125     unsigned long khz;
0126     u64 lpj;
0127 
0128     khz = kvm_get_tsc_khz();
0129 
0130     lpj = ((u64)khz * 1000);
0131     do_div(lpj, HZ);
0132     preset_lpj = lpj;
0133 }
0134 
0135 bool kvm_check_and_clear_guest_paused(void)
0136 {
0137     struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
0138     bool ret = false;
0139 
0140     if (!src)
0141         return ret;
0142 
0143     if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
0144         src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
0145         pvclock_touch_watchdogs();
0146         ret = true;
0147     }
0148     return ret;
0149 }
0150 
0151 static int kvm_cs_enable(struct clocksource *cs)
0152 {
0153     vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
0154     return 0;
0155 }
0156 
0157 struct clocksource kvm_clock = {
0158     .name   = "kvm-clock",
0159     .read   = kvm_clock_get_cycles,
0160     .rating = 400,
0161     .mask   = CLOCKSOURCE_MASK(64),
0162     .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
0163     .enable = kvm_cs_enable,
0164 };
0165 EXPORT_SYMBOL_GPL(kvm_clock);
0166 
0167 static void kvm_register_clock(char *txt)
0168 {
0169     struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
0170     u64 pa;
0171 
0172     if (!src)
0173         return;
0174 
0175     pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
0176     wrmsrl(msr_kvm_system_time, pa);
0177     pr_debug("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
0178 }
0179 
0180 static void kvm_save_sched_clock_state(void)
0181 {
0182 }
0183 
0184 static void kvm_restore_sched_clock_state(void)
0185 {
0186     kvm_register_clock("primary cpu clock, resume");
0187 }
0188 
0189 #ifdef CONFIG_X86_LOCAL_APIC
0190 static void kvm_setup_secondary_clock(void)
0191 {
0192     kvm_register_clock("secondary cpu clock");
0193 }
0194 #endif
0195 
0196 void kvmclock_disable(void)
0197 {
0198     native_write_msr(msr_kvm_system_time, 0, 0);
0199 }
0200 
0201 static void __init kvmclock_init_mem(void)
0202 {
0203     unsigned long ncpus;
0204     unsigned int order;
0205     struct page *p;
0206     int r;
0207 
0208     if (HVC_BOOT_ARRAY_SIZE >= num_possible_cpus())
0209         return;
0210 
0211     ncpus = num_possible_cpus() - HVC_BOOT_ARRAY_SIZE;
0212     order = get_order(ncpus * sizeof(*hvclock_mem));
0213 
0214     p = alloc_pages(GFP_KERNEL, order);
0215     if (!p) {
0216         pr_warn("%s: failed to alloc %d pages", __func__, (1U << order));
0217         return;
0218     }
0219 
0220     hvclock_mem = page_address(p);
0221 
0222     /*
0223      * hvclock is shared between the guest and the hypervisor, must
0224      * be mapped decrypted.
0225      */
0226     if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
0227         r = set_memory_decrypted((unsigned long) hvclock_mem,
0228                      1UL << order);
0229         if (r) {
0230             __free_pages(p, order);
0231             hvclock_mem = NULL;
0232             pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
0233             return;
0234         }
0235     }
0236 
0237     memset(hvclock_mem, 0, PAGE_SIZE << order);
0238 }
0239 
0240 static int __init kvm_setup_vsyscall_timeinfo(void)
0241 {
0242     if (!kvm_para_available() || !kvmclock || nopv)
0243         return 0;
0244 
0245     kvmclock_init_mem();
0246 
0247 #ifdef CONFIG_X86_64
0248     if (per_cpu(hv_clock_per_cpu, 0) && kvmclock_vsyscall) {
0249         u8 flags;
0250 
0251         flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
0252         if (!(flags & PVCLOCK_TSC_STABLE_BIT))
0253             return 0;
0254 
0255         kvm_clock.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
0256     }
0257 #endif
0258 
0259     return 0;
0260 }
0261 early_initcall(kvm_setup_vsyscall_timeinfo);
0262 
0263 static int kvmclock_setup_percpu(unsigned int cpu)
0264 {
0265     struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
0266 
0267     /*
0268      * The per cpu area setup replicates CPU0 data to all cpu
0269      * pointers. So carefully check. CPU0 has been set up in init
0270      * already.
0271      */
0272     if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
0273         return 0;
0274 
0275     /* Use the static page for the first CPUs, allocate otherwise */
0276     if (cpu < HVC_BOOT_ARRAY_SIZE)
0277         p = &hv_clock_boot[cpu];
0278     else if (hvclock_mem)
0279         p = hvclock_mem + cpu - HVC_BOOT_ARRAY_SIZE;
0280     else
0281         return -ENOMEM;
0282 
0283     per_cpu(hv_clock_per_cpu, cpu) = p;
0284     return p ? 0 : -ENOMEM;
0285 }
0286 
0287 void __init kvmclock_init(void)
0288 {
0289     u8 flags;
0290 
0291     if (!kvm_para_available() || !kvmclock)
0292         return;
0293 
0294     if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
0295         msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
0296         msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
0297     } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
0298         return;
0299     }
0300 
0301     if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
0302                   kvmclock_setup_percpu, NULL) < 0) {
0303         return;
0304     }
0305 
0306     pr_info("kvm-clock: Using msrs %x and %x",
0307         msr_kvm_system_time, msr_kvm_wall_clock);
0308 
0309     this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
0310     kvm_register_clock("primary cpu clock");
0311     pvclock_set_pvti_cpu0_va(hv_clock_boot);
0312 
0313     if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
0314         pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
0315 
0316     flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
0317     kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
0318 
0319     x86_platform.calibrate_tsc = kvm_get_tsc_khz;
0320     x86_platform.calibrate_cpu = kvm_get_tsc_khz;
0321     x86_platform.get_wallclock = kvm_get_wallclock;
0322     x86_platform.set_wallclock = kvm_set_wallclock;
0323 #ifdef CONFIG_X86_LOCAL_APIC
0324     x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
0325 #endif
0326     x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
0327     x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
0328     kvm_get_preset_lpj();
0329 
0330     /*
0331      * X86_FEATURE_NONSTOP_TSC is TSC runs at constant rate
0332      * with P/T states and does not stop in deep C-states.
0333      *
0334      * Invariant TSC exposed by host means kvmclock is not necessary:
0335      * can use TSC as clocksource.
0336      *
0337      */
0338     if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
0339         boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
0340         !check_tsc_unstable())
0341         kvm_clock.rating = 299;
0342 
0343     clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
0344     pv_info.name = "KVM";
0345 }