Linux 4.18.10
[linux/fpc-iii.git] / arch / x86 / kernel / kvmclock.c
blob3b8e7c13c614a41fcf4533bd840630c4e3912d8f
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>
22 #include <asm/msr.h>
23 #include <asm/apic.h>
24 #include <linux/percpu.h>
25 #include <linux/hardirq.h>
26 #include <linux/memblock.h>
27 #include <linux/sched.h>
28 #include <linux/sched/clock.h>
30 #include <asm/mem_encrypt.h>
31 #include <asm/x86_init.h>
32 #include <asm/reboot.h>
33 #include <asm/kvmclock.h>
35 static int kvmclock __ro_after_init = 1;
36 static int msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
37 static int msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
38 static u64 kvm_sched_clock_offset;
40 static int parse_no_kvmclock(char *arg)
42 kvmclock = 0;
43 return 0;
45 early_param("no-kvmclock", parse_no_kvmclock);
47 /* The hypervisor will put information about time periodically here */
48 static struct pvclock_vsyscall_time_info *hv_clock;
49 static struct pvclock_wall_clock *wall_clock;
52 * The wallclock is the time of day when we booted. Since then, some time may
53 * have elapsed since the hypervisor wrote the data. So we try to account for
54 * that with system time
56 static void kvm_get_wallclock(struct timespec64 *now)
58 struct pvclock_vcpu_time_info *vcpu_time;
59 int low, high;
60 int cpu;
62 low = (int)slow_virt_to_phys(wall_clock);
63 high = ((u64)slow_virt_to_phys(wall_clock) >> 32);
65 native_write_msr(msr_kvm_wall_clock, low, high);
67 cpu = get_cpu();
69 vcpu_time = &hv_clock[cpu].pvti;
70 pvclock_read_wallclock(wall_clock, vcpu_time, now);
72 put_cpu();
75 static int kvm_set_wallclock(const struct timespec64 *now)
77 return -ENODEV;
80 static u64 kvm_clock_read(void)
82 struct pvclock_vcpu_time_info *src;
83 u64 ret;
84 int cpu;
86 preempt_disable_notrace();
87 cpu = smp_processor_id();
88 src = &hv_clock[cpu].pvti;
89 ret = pvclock_clocksource_read(src);
90 preempt_enable_notrace();
91 return ret;
94 static u64 kvm_clock_get_cycles(struct clocksource *cs)
96 return kvm_clock_read();
99 static u64 kvm_sched_clock_read(void)
101 return kvm_clock_read() - kvm_sched_clock_offset;
104 static inline void kvm_sched_clock_init(bool stable)
106 if (!stable) {
107 pv_time_ops.sched_clock = kvm_clock_read;
108 clear_sched_clock_stable();
109 return;
112 kvm_sched_clock_offset = kvm_clock_read();
113 pv_time_ops.sched_clock = kvm_sched_clock_read;
115 printk(KERN_INFO "kvm-clock: using sched offset of %llu cycles\n",
116 kvm_sched_clock_offset);
118 BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
119 sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
123 * If we don't do that, there is the possibility that the guest
124 * will calibrate under heavy load - thus, getting a lower lpj -
125 * and execute the delays themselves without load. This is wrong,
126 * because no delay loop can finish beforehand.
127 * Any heuristics is subject to fail, because ultimately, a large
128 * poll of guests can be running and trouble each other. So we preset
129 * lpj here
131 static unsigned long kvm_get_tsc_khz(void)
133 struct pvclock_vcpu_time_info *src;
134 int cpu;
135 unsigned long tsc_khz;
137 cpu = get_cpu();
138 src = &hv_clock[cpu].pvti;
139 tsc_khz = pvclock_tsc_khz(src);
140 put_cpu();
141 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
142 return tsc_khz;
145 static void kvm_get_preset_lpj(void)
147 unsigned long khz;
148 u64 lpj;
150 khz = kvm_get_tsc_khz();
152 lpj = ((u64)khz * 1000);
153 do_div(lpj, HZ);
154 preset_lpj = lpj;
157 bool kvm_check_and_clear_guest_paused(void)
159 bool ret = false;
160 struct pvclock_vcpu_time_info *src;
161 int cpu = smp_processor_id();
163 if (!hv_clock)
164 return ret;
166 src = &hv_clock[cpu].pvti;
167 if ((src->flags & PVCLOCK_GUEST_STOPPED) != 0) {
168 src->flags &= ~PVCLOCK_GUEST_STOPPED;
169 pvclock_touch_watchdogs();
170 ret = true;
173 return ret;
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,
183 EXPORT_SYMBOL_GPL(kvm_clock);
185 int kvm_register_clock(char *txt)
187 int cpu = smp_processor_id();
188 int low, high, ret;
189 struct pvclock_vcpu_time_info *src;
191 if (!hv_clock)
192 return 0;
194 src = &hv_clock[cpu].pvti;
195 low = (int)slow_virt_to_phys(src) | 1;
196 high = ((u64)slow_virt_to_phys(src) >> 32);
197 ret = native_write_msr_safe(msr_kvm_system_time, low, high);
198 printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
199 cpu, high, low, txt);
201 return ret;
204 static void kvm_save_sched_clock_state(void)
208 static void kvm_restore_sched_clock_state(void)
210 kvm_register_clock("primary cpu clock, resume");
213 #ifdef CONFIG_X86_LOCAL_APIC
214 static void kvm_setup_secondary_clock(void)
217 * Now that the first cpu already had this clocksource initialized,
218 * we shouldn't fail.
220 WARN_ON(kvm_register_clock("secondary cpu clock"));
222 #endif
225 * After the clock is registered, the host will keep writing to the
226 * registered memory location. If the guest happens to shutdown, this memory
227 * won't be valid. In cases like kexec, in which you install a new kernel, this
228 * means a random memory location will be kept being written. So before any
229 * kind of shutdown from our side, we unregister the clock by writing anything
230 * that does not have the 'enable' bit set in the msr
232 #ifdef CONFIG_KEXEC_CORE
233 static void kvm_crash_shutdown(struct pt_regs *regs)
235 native_write_msr(msr_kvm_system_time, 0, 0);
236 kvm_disable_steal_time();
237 native_machine_crash_shutdown(regs);
239 #endif
241 static void kvm_shutdown(void)
243 native_write_msr(msr_kvm_system_time, 0, 0);
244 kvm_disable_steal_time();
245 native_machine_shutdown();
248 static phys_addr_t __init kvm_memblock_alloc(phys_addr_t size,
249 phys_addr_t align)
251 phys_addr_t mem;
253 mem = memblock_alloc(size, align);
254 if (!mem)
255 return 0;
257 if (sev_active()) {
258 if (early_set_memory_decrypted((unsigned long)__va(mem), size))
259 goto e_free;
262 return mem;
263 e_free:
264 memblock_free(mem, size);
265 return 0;
268 static void __init kvm_memblock_free(phys_addr_t addr, phys_addr_t size)
270 if (sev_active())
271 early_set_memory_encrypted((unsigned long)__va(addr), size);
273 memblock_free(addr, size);
276 void __init kvmclock_init(void)
278 struct pvclock_vcpu_time_info *vcpu_time;
279 unsigned long mem, mem_wall_clock;
280 int size, cpu, wall_clock_size;
281 u8 flags;
283 size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
285 if (!kvm_para_available())
286 return;
288 if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
289 msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
290 msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
291 } else if (!(kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)))
292 return;
294 wall_clock_size = PAGE_ALIGN(sizeof(struct pvclock_wall_clock));
295 mem_wall_clock = kvm_memblock_alloc(wall_clock_size, PAGE_SIZE);
296 if (!mem_wall_clock)
297 return;
299 wall_clock = __va(mem_wall_clock);
300 memset(wall_clock, 0, wall_clock_size);
302 mem = kvm_memblock_alloc(size, PAGE_SIZE);
303 if (!mem) {
304 kvm_memblock_free(mem_wall_clock, wall_clock_size);
305 wall_clock = NULL;
306 return;
309 hv_clock = __va(mem);
310 memset(hv_clock, 0, size);
312 if (kvm_register_clock("primary cpu clock")) {
313 hv_clock = NULL;
314 kvm_memblock_free(mem, size);
315 kvm_memblock_free(mem_wall_clock, wall_clock_size);
316 wall_clock = NULL;
317 return;
320 printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
321 msr_kvm_system_time, msr_kvm_wall_clock);
323 pvclock_set_pvti_cpu0_va(hv_clock);
325 if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
326 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
328 cpu = get_cpu();
329 vcpu_time = &hv_clock[cpu].pvti;
330 flags = pvclock_read_flags(vcpu_time);
332 kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
333 put_cpu();
335 x86_platform.calibrate_tsc = kvm_get_tsc_khz;
336 x86_platform.calibrate_cpu = kvm_get_tsc_khz;
337 x86_platform.get_wallclock = kvm_get_wallclock;
338 x86_platform.set_wallclock = kvm_set_wallclock;
339 #ifdef CONFIG_X86_LOCAL_APIC
340 x86_cpuinit.early_percpu_clock_init =
341 kvm_setup_secondary_clock;
342 #endif
343 x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
344 x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
345 machine_ops.shutdown = kvm_shutdown;
346 #ifdef CONFIG_KEXEC_CORE
347 machine_ops.crash_shutdown = kvm_crash_shutdown;
348 #endif
349 kvm_get_preset_lpj();
350 clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
351 pv_info.name = "KVM";
354 int __init kvm_setup_vsyscall_timeinfo(void)
356 #ifdef CONFIG_X86_64
357 int cpu;
358 u8 flags;
359 struct pvclock_vcpu_time_info *vcpu_time;
360 unsigned int size;
362 if (!hv_clock)
363 return 0;
365 size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
367 cpu = get_cpu();
369 vcpu_time = &hv_clock[cpu].pvti;
370 flags = pvclock_read_flags(vcpu_time);
372 put_cpu();
374 if (!(flags & PVCLOCK_TSC_STABLE_BIT))
375 return 1;
377 kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
378 #endif
379 return 0;