xref: /linux/arch/x86/kernel/kvmclock.c (revision 5ab1de932e2923f490645ad017a689c5b58dc433)
1 /*  KVM paravirtual clock driver. A clocksource implementation
2     Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
3 
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.
8 
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.
13 
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
17 */
18 
19 #include <linux/clocksource.h>
20 #include <linux/kvm_para.h>
21 #include <asm/pvclock.h>
22 #include <asm/msr.h>
23 #include <asm/apic.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>
29 #include <linux/mm.h>
30 
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>
36 
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;
42 
43 static int __init parse_no_kvmclock(char *arg)
44 {
45 	kvmclock = 0;
46 	return 0;
47 }
48 early_param("no-kvmclock", parse_no_kvmclock);
49 
50 static int __init parse_no_kvmclock_vsyscall(char *arg)
51 {
52 	kvmclock_vsyscall = 0;
53 	return 0;
54 }
55 early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
56 
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))
61 
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);
66 
67 static inline struct pvclock_vcpu_time_info *this_cpu_pvti(void)
68 {
69 	return &this_cpu_read(hv_clock_per_cpu)->pvti;
70 }
71 
72 static inline struct pvclock_vsyscall_time_info *this_cpu_hvclock(void)
73 {
74 	return this_cpu_read(hv_clock_per_cpu);
75 }
76 
77 /*
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
81  */
82 static void kvm_get_wallclock(struct timespec64 *now)
83 {
84 	wrmsrl(msr_kvm_wall_clock, slow_virt_to_phys(&wall_clock));
85 	preempt_disable();
86 	pvclock_read_wallclock(&wall_clock, this_cpu_pvti(), now);
87 	preempt_enable();
88 }
89 
90 static int kvm_set_wallclock(const struct timespec64 *now)
91 {
92 	return -ENODEV;
93 }
94 
95 static u64 kvm_clock_read(void)
96 {
97 	u64 ret;
98 
99 	preempt_disable_notrace();
100 	ret = pvclock_clocksource_read(this_cpu_pvti());
101 	preempt_enable_notrace();
102 	return ret;
103 }
104 
105 static u64 kvm_clock_get_cycles(struct clocksource *cs)
106 {
107 	return kvm_clock_read();
108 }
109 
110 static u64 kvm_sched_clock_read(void)
111 {
112 	return kvm_clock_read() - kvm_sched_clock_offset;
113 }
114 
115 static inline void kvm_sched_clock_init(bool stable)
116 {
117 	if (!stable) {
118 		pv_time_ops.sched_clock = kvm_clock_read;
119 		clear_sched_clock_stable();
120 		return;
121 	}
122 
123 	kvm_sched_clock_offset = kvm_clock_read();
124 	pv_time_ops.sched_clock = kvm_sched_clock_read;
125 
126 	pr_info("kvm-clock: using sched offset of %llu cycles",
127 		kvm_sched_clock_offset);
128 
129 	BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
130 		sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
131 }
132 
133 /*
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
140  * lpj here
141  */
142 static unsigned long kvm_get_tsc_khz(void)
143 {
144 	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
145 	return pvclock_tsc_khz(this_cpu_pvti());
146 }
147 
148 static void __init kvm_get_preset_lpj(void)
149 {
150 	unsigned long khz;
151 	u64 lpj;
152 
153 	khz = kvm_get_tsc_khz();
154 
155 	lpj = ((u64)khz * 1000);
156 	do_div(lpj, HZ);
157 	preset_lpj = lpj;
158 }
159 
160 bool kvm_check_and_clear_guest_paused(void)
161 {
162 	struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
163 	bool ret = false;
164 
165 	if (!src)
166 		return ret;
167 
168 	if ((src->pvti.flags & PVCLOCK_GUEST_STOPPED) != 0) {
169 		src->pvti.flags &= ~PVCLOCK_GUEST_STOPPED;
170 		pvclock_touch_watchdogs();
171 		ret = true;
172 	}
173 	return ret;
174 }
175 
176 struct clocksource kvm_clock = {
177 	.name	= "kvm-clock",
178 	.read	= kvm_clock_get_cycles,
179 	.rating	= 400,
180 	.mask	= CLOCKSOURCE_MASK(64),
181 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
182 };
183 EXPORT_SYMBOL_GPL(kvm_clock);
184 
185 static void kvm_register_clock(char *txt)
186 {
187 	struct pvclock_vsyscall_time_info *src = this_cpu_hvclock();
188 	u64 pa;
189 
190 	if (!src)
191 		return;
192 
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);
196 }
197 
198 static void kvm_save_sched_clock_state(void)
199 {
200 }
201 
202 static void kvm_restore_sched_clock_state(void)
203 {
204 	kvm_register_clock("primary cpu clock, resume");
205 }
206 
207 #ifdef CONFIG_X86_LOCAL_APIC
208 static void kvm_setup_secondary_clock(void)
209 {
210 	kvm_register_clock("secondary cpu clock");
211 }
212 #endif
213 
214 /*
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
221  */
222 #ifdef CONFIG_KEXEC_CORE
223 static void kvm_crash_shutdown(struct pt_regs *regs)
224 {
225 	native_write_msr(msr_kvm_system_time, 0, 0);
226 	kvm_disable_steal_time();
227 	native_machine_crash_shutdown(regs);
228 }
229 #endif
230 
231 static void kvm_shutdown(void)
232 {
233 	native_write_msr(msr_kvm_system_time, 0, 0);
234 	kvm_disable_steal_time();
235 	native_machine_shutdown();
236 }
237 
238 static int __init kvm_setup_vsyscall_timeinfo(void)
239 {
240 #ifdef CONFIG_X86_64
241 	u8 flags;
242 
243 	if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
244 		return 0;
245 
246 	flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
247 	if (!(flags & PVCLOCK_TSC_STABLE_BIT))
248 		return 0;
249 
250 	kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
251 #endif
252 	return 0;
253 }
254 early_initcall(kvm_setup_vsyscall_timeinfo);
255 
256 static int kvmclock_setup_percpu(unsigned int cpu)
257 {
258 	struct pvclock_vsyscall_time_info *p = per_cpu(hv_clock_per_cpu, cpu);
259 
260 	/*
261 	 * The per cpu area setup replicates CPU0 data to all cpu
262 	 * pointers. So carefully check. CPU0 has been set up in init
263 	 * already.
264 	 */
265 	if (!cpu || (p && p != per_cpu(hv_clock_per_cpu, 0)))
266 		return 0;
267 
268 	/* Use the static page for the first CPUs, allocate otherwise */
269 	if (cpu < HVC_BOOT_ARRAY_SIZE)
270 		p = &hv_clock_boot[cpu];
271 	else
272 		p = kzalloc(sizeof(*p), GFP_KERNEL);
273 
274 	per_cpu(hv_clock_per_cpu, cpu) = p;
275 	return p ? 0 : -ENOMEM;
276 }
277 
278 void __init kvmclock_init(void)
279 {
280 	u8 flags;
281 
282 	if (!kvm_para_available() || !kvmclock)
283 		return;
284 
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)) {
289 		return;
290 	}
291 
292 	if (cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "kvmclock:setup_percpu",
293 			      kvmclock_setup_percpu, NULL) < 0) {
294 		return;
295 	}
296 
297 	pr_info("kvm-clock: Using msrs %x and %x",
298 		msr_kvm_system_time, msr_kvm_wall_clock);
299 
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);
303 
304 	if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
305 		pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
306 
307 	flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
308 	kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
309 
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;
316 #endif
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;
322 #endif
323 	kvm_get_preset_lpj();
324 	clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
325 	pv_info.name = "KVM";
326 }
327