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>
31 #include <linux/set_memory.h>
33 #include <asm/hypervisor.h>
34 #include <asm/mem_encrypt.h>
35 #include <asm/x86_init.h>
36 #include <asm/reboot.h>
37 #include <asm/kvmclock.h>
39 static int kvmclock __initdata
= 1;
40 static int kvmclock_vsyscall __initdata
= 1;
41 static int msr_kvm_system_time __ro_after_init
= MSR_KVM_SYSTEM_TIME
;
42 static int msr_kvm_wall_clock __ro_after_init
= MSR_KVM_WALL_CLOCK
;
43 static u64 kvm_sched_clock_offset __ro_after_init
;
45 static int __init
parse_no_kvmclock(char *arg
)
50 early_param("no-kvmclock", parse_no_kvmclock
);
52 static int __init
parse_no_kvmclock_vsyscall(char *arg
)
54 kvmclock_vsyscall
= 0;
57 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall
);
59 /* Aligned to page sizes to match whats mapped via vsyscalls to userspace */
60 #define HV_CLOCK_SIZE (sizeof(struct pvclock_vsyscall_time_info) * NR_CPUS)
61 #define HVC_BOOT_ARRAY_SIZE \
62 (PAGE_SIZE / sizeof(struct pvclock_vsyscall_time_info))
64 static struct pvclock_vsyscall_time_info
65 hv_clock_boot
[HVC_BOOT_ARRAY_SIZE
] __bss_decrypted
__aligned(PAGE_SIZE
);
66 static struct pvclock_wall_clock wall_clock __bss_decrypted
;
67 static DEFINE_PER_CPU(struct pvclock_vsyscall_time_info
*, hv_clock_per_cpu
);
68 static struct pvclock_vsyscall_time_info
*hvclock_mem
;
70 static inline struct pvclock_vcpu_time_info
*this_cpu_pvti(void)
72 return &this_cpu_read(hv_clock_per_cpu
)->pvti
;
75 static inline struct pvclock_vsyscall_time_info
*this_cpu_hvclock(void)
77 return this_cpu_read(hv_clock_per_cpu
);
81 * The wallclock is the time of day when we booted. Since then, some time may
82 * have elapsed since the hypervisor wrote the data. So we try to account for
83 * that with system time
85 static void kvm_get_wallclock(struct timespec64
*now
)
87 wrmsrl(msr_kvm_wall_clock
, slow_virt_to_phys(&wall_clock
));
89 pvclock_read_wallclock(&wall_clock
, this_cpu_pvti(), now
);
93 static int kvm_set_wallclock(const struct timespec64
*now
)
98 static u64
kvm_clock_read(void)
102 preempt_disable_notrace();
103 ret
= pvclock_clocksource_read(this_cpu_pvti());
104 preempt_enable_notrace();
108 static u64
kvm_clock_get_cycles(struct clocksource
*cs
)
110 return kvm_clock_read();
113 static u64
kvm_sched_clock_read(void)
115 return kvm_clock_read() - kvm_sched_clock_offset
;
118 static inline void kvm_sched_clock_init(bool stable
)
121 pv_ops
.time
.sched_clock
= kvm_clock_read
;
122 clear_sched_clock_stable();
126 kvm_sched_clock_offset
= kvm_clock_read();
127 pv_ops
.time
.sched_clock
= kvm_sched_clock_read
;
129 pr_info("kvm-clock: using sched offset of %llu cycles",
130 kvm_sched_clock_offset
);
132 BUILD_BUG_ON(sizeof(kvm_sched_clock_offset
) >
133 sizeof(((struct pvclock_vcpu_time_info
*)NULL
)->system_time
));
137 * If we don't do that, there is the possibility that the guest
138 * will calibrate under heavy load - thus, getting a lower lpj -
139 * and execute the delays themselves without load. This is wrong,
140 * because no delay loop can finish beforehand.
141 * Any heuristics is subject to fail, because ultimately, a large
142 * poll of guests can be running and trouble each other. So we preset
145 static unsigned long kvm_get_tsc_khz(void)
147 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ
);
148 return pvclock_tsc_khz(this_cpu_pvti());
151 static void __init
kvm_get_preset_lpj(void)
156 khz
= kvm_get_tsc_khz();
158 lpj
= ((u64
)khz
* 1000);
163 bool kvm_check_and_clear_guest_paused(void)
165 struct pvclock_vsyscall_time_info
*src
= this_cpu_hvclock();
171 if ((src
->pvti
.flags
& PVCLOCK_GUEST_STOPPED
) != 0) {
172 src
->pvti
.flags
&= ~PVCLOCK_GUEST_STOPPED
;
173 pvclock_touch_watchdogs();
179 struct clocksource kvm_clock
= {
181 .read
= kvm_clock_get_cycles
,
183 .mask
= CLOCKSOURCE_MASK(64),
184 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
186 EXPORT_SYMBOL_GPL(kvm_clock
);
188 static void kvm_register_clock(char *txt
)
190 struct pvclock_vsyscall_time_info
*src
= this_cpu_hvclock();
196 pa
= slow_virt_to_phys(&src
->pvti
) | 0x01ULL
;
197 wrmsrl(msr_kvm_system_time
, pa
);
198 pr_info("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa
, txt
);
201 static void kvm_save_sched_clock_state(void)
205 static void kvm_restore_sched_clock_state(void)
207 kvm_register_clock("primary cpu clock, resume");
210 #ifdef CONFIG_X86_LOCAL_APIC
211 static void kvm_setup_secondary_clock(void)
213 kvm_register_clock("secondary cpu clock");
218 * After the clock is registered, the host will keep writing to the
219 * registered memory location. If the guest happens to shutdown, this memory
220 * won't be valid. In cases like kexec, in which you install a new kernel, this
221 * means a random memory location will be kept being written. So before any
222 * kind of shutdown from our side, we unregister the clock by writing anything
223 * that does not have the 'enable' bit set in the msr
225 #ifdef CONFIG_KEXEC_CORE
226 static void kvm_crash_shutdown(struct pt_regs
*regs
)
228 native_write_msr(msr_kvm_system_time
, 0, 0);
229 kvm_disable_steal_time();
230 native_machine_crash_shutdown(regs
);
234 static void kvm_shutdown(void)
236 native_write_msr(msr_kvm_system_time
, 0, 0);
237 kvm_disable_steal_time();
238 native_machine_shutdown();
241 static void __init
kvmclock_init_mem(void)
248 if (HVC_BOOT_ARRAY_SIZE
>= num_possible_cpus())
251 ncpus
= num_possible_cpus() - HVC_BOOT_ARRAY_SIZE
;
252 order
= get_order(ncpus
* sizeof(*hvclock_mem
));
254 p
= alloc_pages(GFP_KERNEL
, order
);
256 pr_warn("%s: failed to alloc %d pages", __func__
, (1U << order
));
260 hvclock_mem
= page_address(p
);
263 * hvclock is shared between the guest and the hypervisor, must
264 * be mapped decrypted.
267 r
= set_memory_decrypted((unsigned long) hvclock_mem
,
270 __free_pages(p
, order
);
272 pr_warn("kvmclock: set_memory_decrypted() failed. Disabling\n");
277 memset(hvclock_mem
, 0, PAGE_SIZE
<< order
);
280 static int __init
kvm_setup_vsyscall_timeinfo(void)
285 if (!per_cpu(hv_clock_per_cpu
, 0) || !kvmclock_vsyscall
)
288 flags
= pvclock_read_flags(&hv_clock_boot
[0].pvti
);
289 if (!(flags
& PVCLOCK_TSC_STABLE_BIT
))
292 kvm_clock
.archdata
.vclock_mode
= VCLOCK_PVCLOCK
;
299 early_initcall(kvm_setup_vsyscall_timeinfo
);
301 static int kvmclock_setup_percpu(unsigned int cpu
)
303 struct pvclock_vsyscall_time_info
*p
= per_cpu(hv_clock_per_cpu
, cpu
);
306 * The per cpu area setup replicates CPU0 data to all cpu
307 * pointers. So carefully check. CPU0 has been set up in init
310 if (!cpu
|| (p
&& p
!= per_cpu(hv_clock_per_cpu
, 0)))
313 /* Use the static page for the first CPUs, allocate otherwise */
314 if (cpu
< HVC_BOOT_ARRAY_SIZE
)
315 p
= &hv_clock_boot
[cpu
];
316 else if (hvclock_mem
)
317 p
= hvclock_mem
+ cpu
- HVC_BOOT_ARRAY_SIZE
;
321 per_cpu(hv_clock_per_cpu
, cpu
) = p
;
322 return p
? 0 : -ENOMEM
;
325 void __init
kvmclock_init(void)
329 if (!kvm_para_available() || !kvmclock
)
332 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2
)) {
333 msr_kvm_system_time
= MSR_KVM_SYSTEM_TIME_NEW
;
334 msr_kvm_wall_clock
= MSR_KVM_WALL_CLOCK_NEW
;
335 } else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE
)) {
339 if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN
, "kvmclock:setup_percpu",
340 kvmclock_setup_percpu
, NULL
) < 0) {
344 pr_info("kvm-clock: Using msrs %x and %x",
345 msr_kvm_system_time
, msr_kvm_wall_clock
);
347 this_cpu_write(hv_clock_per_cpu
, &hv_clock_boot
[0]);
348 kvm_register_clock("primary cpu clock");
349 pvclock_set_pvti_cpu0_va(hv_clock_boot
);
351 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT
))
352 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT
);
354 flags
= pvclock_read_flags(&hv_clock_boot
[0].pvti
);
355 kvm_sched_clock_init(flags
& PVCLOCK_TSC_STABLE_BIT
);
357 x86_platform
.calibrate_tsc
= kvm_get_tsc_khz
;
358 x86_platform
.calibrate_cpu
= kvm_get_tsc_khz
;
359 x86_platform
.get_wallclock
= kvm_get_wallclock
;
360 x86_platform
.set_wallclock
= kvm_set_wallclock
;
361 #ifdef CONFIG_X86_LOCAL_APIC
362 x86_cpuinit
.early_percpu_clock_init
= kvm_setup_secondary_clock
;
364 x86_platform
.save_sched_clock_state
= kvm_save_sched_clock_state
;
365 x86_platform
.restore_sched_clock_state
= kvm_restore_sched_clock_state
;
366 machine_ops
.shutdown
= kvm_shutdown
;
367 #ifdef CONFIG_KEXEC_CORE
368 machine_ops
.crash_shutdown
= kvm_crash_shutdown
;
370 kvm_get_preset_lpj();
371 clocksource_register_hz(&kvm_clock
, NSEC_PER_SEC
);
372 pv_info
.name
= "KVM";