1 /* KVM paravirtual clock driver. A clocksource implementation
2 Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/clocksource.h>
20 #include <linux/kvm_para.h>
21 #include <asm/pvclock.h>
24 #include <linux/percpu.h>
25 #include <linux/hardirq.h>
26 #include <linux/cpuhotplug.h>
27 #include <linux/sched.h>
28 #include <linux/sched/clock.h>
31 #include <asm/hypervisor.h>
32 #include <asm/mem_encrypt.h>
33 #include <asm/x86_init.h>
34 #include <asm/reboot.h>
35 #include <asm/kvmclock.h>
37 static int kvmclock __initdata = 1;
38 static int kvmclock_vsyscall __initdata = 1;
39 static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
40 static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
41 static u64 kvm_sched_clock_offset __ro_after_init;
43 static int __init parse_no_kvmclock(char *arg)
48 early_param("no-kvmclock", parse_no_kvmclock);
50 static int __init parse_no_kvmclock_vsyscall(char *arg)
52 kvmclock_vsyscall = 0;
55 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
57 /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
58 #define HV_CLOCK_SIZE (sizeof(struct pvclock_vsyscall_time_info) * NR_CPUS)
59 #define HVC_BOOT_ARRAY_SIZE \
60 (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
62 static struct pvclock_vsyscall_time_info
63 hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __aligned(PAGE_SIZE);
64 static struct pvclock_wall_clock wall_clock;
65 static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
67 static inline struct pvclock_vcpu_time_info *this_cpu_pvti(void)
69 return &this_cpu_read(hv_clock_per_cpu)->pvti;
72 static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void)
74 return this_cpu_read(hv_clock_per_cpu);
78 * The wallclock is the time of day when we booted. Since then, some time may
79 * have elapsed since the hypervisor wrote the data. So we try to account for
80 * that with system time
82 static void kvm_get_wallclock(struct timespec64 *now)
84 wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
86 pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
90 static int kvm_set_wallclock(const struct timespec64 *now)
95 static u64 kvm_clock_read(void)
99 preempt_disable_notrace();
100 ret = pvclock_clocksource_read(this_cpu_pvti());
101 preempt_enable_notrace();
105 static u64 kvm_clock_get_cycles(struct clocksource *cs)
107 return kvm_clock_read();
110 static u64 kvm_sched_clock_read(void)
112 return kvm_clock_read() - kvm_sched_clock_offset;
115 static inline void kvm_sched_clock_init(bool stable)
118 pv_time_ops.sched_clock = kvm_clock_read;
119 clear_sched_clock_stable();
123 kvm_sched_clock_offset = kvm_clock_read();
124 pv_time_ops.sched_clock = kvm_sched_clock_read;
126 pr_info("kvm-clock: using sched offset of %llu cycles",
127 kvm_sched_clock_offset);
129 BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
130 sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
134 * If we don't do that, there is the possibility that the guest
135 * will calibrate under heavy load - thus, getting a lower lpj -
136 * and execute the delays themselves without load. This is wrong,
137 * because no delay loop can finish beforehand.
138 * Any heuristics is subject to fail, because ultimately, a large
139 * poll of guests can be running and trouble each other. So we preset
142 static unsigned long kvm_get_tsc_khz(void)
144 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
145 return pvclock_tsc_khz(this_cpu_pvti());
148 static void kvm_get_preset_lpj(void)
153 khz = kvm_get_tsc_khz();
155 lpj = ((u64)khz * 1000);
160 bool kvm_check_and_clear_guest_paused(void)
162 struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
168 if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
169 src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
170 pvclock_touch_watchdogs();
176 struct clocksource kvm_clock = {
178 .read = kvm_clock_get_cycles,
180 .mask = CLOCKSOURCE_MASK(64),
181 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
183 EXPORT_SYMBOL_GPL(kvm_clock);
185 static void kvm_register_clock(char *txt)
187 struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
193 pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
194 wrmsrl(msr_kvm_system_time, pa);
195 pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
198 static void kvm_save_sched_clock_state(void)
202 static void kvm_restore_sched_clock_state(void)
204 kvm_register_clock("primary cpu clock, resume");
207 #ifdef CONFIG_X86_LOCAL_APIC
208 static void kvm_setup_secondary_clock(void)
210 kvm_register_clock("secondary cpu clock");
215 * After the clock is registered, the host will keep writing to the
216 * registered memory location. If the guest happens to shutdown, this memory
217 * won't be valid. In cases like kexec, in which you install a new kernel, this
218 * means a random memory location will be kept being written. So before any
219 * kind of shutdown from our side, we unregister the clock by writing anything
220 * that does not have the 'enable' bit set in the msr
222 #ifdef CONFIG_KEXEC_CORE
223 static void kvm_crash_shutdown(struct pt_regs *regs)
225 native_write_msr(msr_kvm_system_time, 0, 0);
226 kvm_disable_steal_time();
227 native_machine_crash_shutdown(regs);
231 static void kvm_shutdown(void)
233 native_write_msr(msr_kvm_system_time, 0, 0);
234 kvm_disable_steal_time();
235 native_machine_shutdown();
238 static int __init kvm_setup_vsyscall_timeinfo(void)
243 if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
246 flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
247 if (!(flags & PVCLOCK_TSC_STABLE_BIT))
250 kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
254 early_initcall(kvm_setup_vsyscall_timeinfo);
256 static int kvmclock_setup_percpu(unsigned int cpu)
258 struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
261 * The per cpu area setup replicates CPU0 data to all cpu
262 * pointers. So carefully check. CPU0 has been set up in init
265 if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
268 /* Use the static page for the first CPUs, allocate otherwise */
269 if (cpu < HVC_BOOT_ARRAY_SIZE)
270 p = &hv_clock_boot[cpu];
272 p = kzalloc(sizeof(*p), GFP_KERNEL);
274 per_cpu(hv_clock_per_cpu, cpu) = p;
275 return p ? 0 : -ENOMEM;
278 void __init kvmclock_init(void)
282 if (!kvm_para_available() || !kvmclock)
285 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
286 msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
287 msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
288 } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
292 if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
293 kvmclock_setup_percpu, NULL) < 0) {
297 pr_info("kvm-clock: Using msrs %x and %x",
298 msr_kvm_system_time, msr_kvm_wall_clock);
300 this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
301 kvm_register_clock("primary cpu clock");
302 pvclock_set_pvti_cpu0_va(hv_clock_boot);
304 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
305 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
307 flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
308 kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
310 x86_platform.calibrate_tsc = kvm_get_tsc_khz;
311 x86_platform.calibrate_cpu = kvm_get_tsc_khz;
312 x86_platform.get_wallclock = kvm_get_wallclock;
313 x86_platform.set_wallclock = kvm_set_wallclock;
314 #ifdef CONFIG_X86_LOCAL_APIC
315 x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
317 x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
318 x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
319 machine_ops.shutdown = kvm_shutdown;
320 #ifdef CONFIG_KEXEC_CORE
321 machine_ops.crash_shutdown = kvm_crash_shutdown;
323 kvm_get_preset_lpj();
324 clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
325 pv_info.name = "KVM";