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>
30 #include <linux/slab.h>
32 #include <asm/hypervisor.h>
33 #include <asm/mem_encrypt.h>
34 #include <asm/x86_init.h>
35 #include <asm/reboot.h>
36 #include <asm/kvmclock.h>
38 static int kvmclock __initdata = 1;
39 static int kvmclock_vsyscall __initdata = 1;
40 static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
41 static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
42 static u64 kvm_sched_clock_offset __ro_after_init;
44 static int __init parse_no_kvmclock(char *arg)
49 early_param("no-kvmclock", parse_no_kvmclock);
51 static int __init parse_no_kvmclock_vsyscall(char *arg)
53 kvmclock_vsyscall = 0;
56 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
58 /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
59 #define HV_CLOCK_SIZE (sizeof(struct pvclock_vsyscall_time_info) * NR_CPUS)
60 #define HVC_BOOT_ARRAY_SIZE \
61 (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
63 static struct pvclock_vsyscall_time_info
64 hv_clock_boot[HVC_BOOT_ARRAY_SIZE] __aligned(PAGE_SIZE);
65 static struct pvclock_wall_clock wall_clock;
66 static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info *, hv_clock_per_cpu);
68 static inline struct pvclock_vcpu_time_info *this_cpu_pvti(void)
70 return &this_cpu_read(hv_clock_per_cpu)->pvti;
73 static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void)
75 return this_cpu_read(hv_clock_per_cpu);
79 * The wallclock is the time of day when we booted. Since then, some time may
80 * have elapsed since the hypervisor wrote the data. So we try to account for
81 * that with system time
83 static void kvm_get_wallclock(struct timespec64 *now)
85 wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
87 pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
91 static int kvm_set_wallclock(const struct timespec64 *now)
96 static u64 kvm_clock_read(void)
100 preempt_disable_notrace();
101 ret = pvclock_clocksource_read(this_cpu_pvti());
102 preempt_enable_notrace();
106 static u64 kvm_clock_get_cycles(struct clocksource *cs)
108 return kvm_clock_read();
111 static u64 kvm_sched_clock_read(void)
113 return kvm_clock_read() - kvm_sched_clock_offset;
116 static inline void kvm_sched_clock_init(bool stable)
119 pv_time_ops.sched_clock = kvm_clock_read;
120 clear_sched_clock_stable();
124 kvm_sched_clock_offset = kvm_clock_read();
125 pv_time_ops.sched_clock = kvm_sched_clock_read;
127 pr_info("kvm-clock: using sched offset of %llu cycles",
128 kvm_sched_clock_offset);
130 BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
131 sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
135 * If we don't do that, there is the possibility that the guest
136 * will calibrate under heavy load - thus, getting a lower lpj -
137 * and execute the delays themselves without load. This is wrong,
138 * because no delay loop can finish beforehand.
139 * Any heuristics is subject to fail, because ultimately, a large
140 * poll of guests can be running and trouble each other. So we preset
143 static unsigned long kvm_get_tsc_khz(void)
145 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
146 return pvclock_tsc_khz(this_cpu_pvti());
149 static void __init kvm_get_preset_lpj(void)
154 khz = kvm_get_tsc_khz();
156 lpj = ((u64)khz * 1000);
161 bool kvm_check_and_clear_guest_paused(void)
163 struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
169 if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
170 src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
171 pvclock_touch_watchdogs();
177 struct clocksource kvm_clock = {
179 .read = kvm_clock_get_cycles,
181 .mask = CLOCKSOURCE_MASK(64),
182 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
184 EXPORT_SYMBOL_GPL(kvm_clock);
186 static void kvm_register_clock(char *txt)
188 struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
194 pa = slow_virt_to_phys(&src->pvti) | 0x01ULL;
195 wrmsrl(msr_kvm_system_time, pa);
196 pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt);
199 static void kvm_save_sched_clock_state(void)
203 static void kvm_restore_sched_clock_state(void)
205 kvm_register_clock("primary cpu clock, resume");
208 #ifdef CONFIG_X86_LOCAL_APIC
209 static void kvm_setup_secondary_clock(void)
211 kvm_register_clock("secondary cpu clock");
216 * After the clock is registered, the host will keep writing to the
217 * registered memory location. If the guest happens to shutdown, this memory
218 * won't be valid. In cases like kexec, in which you install a new kernel, this
219 * means a random memory location will be kept being written. So before any
220 * kind of shutdown from our side, we unregister the clock by writing anything
221 * that does not have the 'enable' bit set in the msr
223 #ifdef CONFIG_KEXEC_CORE
224 static void kvm_crash_shutdown(struct pt_regs *regs)
226 native_write_msr(msr_kvm_system_time, 0, 0);
227 kvm_disable_steal_time();
228 native_machine_crash_shutdown(regs);
232 static void kvm_shutdown(void)
234 native_write_msr(msr_kvm_system_time, 0, 0);
235 kvm_disable_steal_time();
236 native_machine_shutdown();
239 static int __init kvm_setup_vsyscall_timeinfo(void)
244 if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
247 flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
248 if (!(flags & PVCLOCK_TSC_STABLE_BIT))
251 kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
255 early_initcall(kvm_setup_vsyscall_timeinfo);
257 static int kvmclock_setup_percpu(unsigned int cpu)
259 struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
262 * The per cpu area setup replicates CPU0 data to all cpu
263 * pointers. So carefully check. CPU0 has been set up in init
266 if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
269 /* Use the static page for the first CPUs, allocate otherwise */
270 if (cpu < HVC_BOOT_ARRAY_SIZE)
271 p = &hv_clock_boot[cpu];
273 p = kzalloc(sizeof(*p), GFP_KERNEL);
275 per_cpu(hv_clock_per_cpu, cpu) = p;
276 return p ? 0 : -ENOMEM;
279 void __init kvmclock_init(void)
283 if (!kvm_para_available() || !kvmclock)
286 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
287 msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
288 msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
289 } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
293 if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
294 kvmclock_setup_percpu, NULL) < 0) {
298 pr_info("kvm-clock: Using msrs %x and %x",
299 msr_kvm_system_time, msr_kvm_wall_clock);
301 this_cpu_write(hv_clock_per_cpu, &hv_clock_boot[0]);
302 kvm_register_clock("primary cpu clock");
303 pvclock_set_pvti_cpu0_va(hv_clock_boot);
305 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
306 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
308 flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
309 kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
311 x86_platform.calibrate_tsc = kvm_get_tsc_khz;
312 x86_platform.calibrate_cpu = kvm_get_tsc_khz;
313 x86_platform.get_wallclock = kvm_get_wallclock;
314 x86_platform.set_wallclock = kvm_set_wallclock;
315 #ifdef CONFIG_X86_LOCAL_APIC
316 x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock;
318 x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
319 x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
320 machine_ops.shutdown = kvm_shutdown;
321 #ifdef CONFIG_KEXEC_CORE
322 machine_ops.crash_shutdown = kvm_crash_shutdown;
324 kvm_get_preset_lpj();
325 clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
326 pv_info.name = "KVM";