Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[sfrench/cifs-2.6.git] / arch / x86 / kernel / kvmclock.c
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/memblock.h>
27 #include <linux/sched.h>
28 #include <linux/sched/clock.h>
29
30 #include <asm/mem_encrypt.h>
31 #include <asm/x86_init.h>
32 #include <asm/reboot.h>
33 #include <asm/kvmclock.h>
34
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;
39
40 static int parse_no_kvmclock(char *arg)
41 {
42         kvmclock = 0;
43         return 0;
44 }
45 early_param("no-kvmclock", parse_no_kvmclock);
46
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;
50
51 /*
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
55  */
56 static void kvm_get_wallclock(struct timespec64 *now)
57 {
58         struct pvclock_vcpu_time_info *vcpu_time;
59         int low, high;
60         int cpu;
61
62         low = (int)slow_virt_to_phys(wall_clock);
63         high = ((u64)slow_virt_to_phys(wall_clock) >> 32);
64
65         native_write_msr(msr_kvm_wall_clock, low, high);
66
67         cpu = get_cpu();
68
69         vcpu_time = &hv_clock[cpu].pvti;
70         pvclock_read_wallclock(wall_clock, vcpu_time, now);
71
72         put_cpu();
73 }
74
75 static int kvm_set_wallclock(const struct timespec64 *now)
76 {
77         return -ENODEV;
78 }
79
80 static u64 kvm_clock_read(void)
81 {
82         struct pvclock_vcpu_time_info *src;
83         u64 ret;
84         int cpu;
85
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;
92 }
93
94 static u64 kvm_clock_get_cycles(struct clocksource *cs)
95 {
96         return kvm_clock_read();
97 }
98
99 static u64 kvm_sched_clock_read(void)
100 {
101         return kvm_clock_read() - kvm_sched_clock_offset;
102 }
103
104 static inline void kvm_sched_clock_init(bool stable)
105 {
106         if (!stable) {
107                 pv_time_ops.sched_clock = kvm_clock_read;
108                 clear_sched_clock_stable();
109                 return;
110         }
111
112         kvm_sched_clock_offset = kvm_clock_read();
113         pv_time_ops.sched_clock = kvm_sched_clock_read;
114
115         printk(KERN_INFO "kvm-clock: using sched offset of %llu cycles\n",
116                         kvm_sched_clock_offset);
117
118         BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) >
119                  sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time));
120 }
121
122 /*
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
130  */
131 static unsigned long kvm_get_tsc_khz(void)
132 {
133         struct pvclock_vcpu_time_info *src;
134         int cpu;
135         unsigned long tsc_khz;
136
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;
143 }
144
145 static void kvm_get_preset_lpj(void)
146 {
147         unsigned long khz;
148         u64 lpj;
149
150         khz = kvm_get_tsc_khz();
151
152         lpj = ((u64)khz * 1000);
153         do_div(lpj, HZ);
154         preset_lpj = lpj;
155 }
156
157 bool kvm_check_and_clear_guest_paused(void)
158 {
159         bool ret = false;
160         struct pvclock_vcpu_time_info *src;
161         int cpu = smp_processor_id();
162
163         if (!hv_clock)
164                 return ret;
165
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;
171         }
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 int kvm_register_clock(char *txt)
186 {
187         int cpu = smp_processor_id();
188         int low, high, ret;
189         struct pvclock_vcpu_time_info *src;
190
191         if (!hv_clock)
192                 return 0;
193
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);
200
201         return ret;
202 }
203
204 static void kvm_save_sched_clock_state(void)
205 {
206 }
207
208 static void kvm_restore_sched_clock_state(void)
209 {
210         kvm_register_clock("primary cpu clock, resume");
211 }
212
213 #ifdef CONFIG_X86_LOCAL_APIC
214 static void kvm_setup_secondary_clock(void)
215 {
216         /*
217          * Now that the first cpu already had this clocksource initialized,
218          * we shouldn't fail.
219          */
220         WARN_ON(kvm_register_clock("secondary cpu clock"));
221 }
222 #endif
223
224 /*
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
231  */
232 #ifdef CONFIG_KEXEC_CORE
233 static void kvm_crash_shutdown(struct pt_regs *regs)
234 {
235         native_write_msr(msr_kvm_system_time, 0, 0);
236         kvm_disable_steal_time();
237         native_machine_crash_shutdown(regs);
238 }
239 #endif
240
241 static void kvm_shutdown(void)
242 {
243         native_write_msr(msr_kvm_system_time, 0, 0);
244         kvm_disable_steal_time();
245         native_machine_shutdown();
246 }
247
248 static phys_addr_t __init kvm_memblock_alloc(phys_addr_t size,
249                                              phys_addr_t align)
250 {
251         phys_addr_t mem;
252
253         mem = memblock_alloc(size, align);
254         if (!mem)
255                 return 0;
256
257         if (sev_active()) {
258                 if (early_set_memory_decrypted((unsigned long)__va(mem), size))
259                         goto e_free;
260         }
261
262         return mem;
263 e_free:
264         memblock_free(mem, size);
265         return 0;
266 }
267
268 static void __init kvm_memblock_free(phys_addr_t addr, phys_addr_t size)
269 {
270         if (sev_active())
271                 early_set_memory_encrypted((unsigned long)__va(addr), size);
272
273         memblock_free(addr, size);
274 }
275
276 void __init kvmclock_init(void)
277 {
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;
282
283         size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
284
285         if (!kvm_para_available())
286                 return;
287
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;
293
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;
298
299         wall_clock = __va(mem_wall_clock);
300         memset(wall_clock, 0, wall_clock_size);
301
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;
307         }
308
309         hv_clock = __va(mem);
310         memset(hv_clock, 0, size);
311
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;
318         }
319
320         printk(KERN_INFO "kvm-clock: Using msrs %x and %x",
321                 msr_kvm_system_time, msr_kvm_wall_clock);
322
323         pvclock_set_pvti_cpu0_va(hv_clock);
324
325         if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
326                 pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
327
328         cpu = get_cpu();
329         vcpu_time = &hv_clock[cpu].pvti;
330         flags = pvclock_read_flags(vcpu_time);
331
332         kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
333         put_cpu();
334
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";
352 }
353
354 int __init kvm_setup_vsyscall_timeinfo(void)
355 {
356 #ifdef CONFIG_X86_64
357         int cpu;
358         u8 flags;
359         struct pvclock_vcpu_time_info *vcpu_time;
360         unsigned int size;
361
362         if (!hv_clock)
363                 return 0;
364
365         size = PAGE_ALIGN(sizeof(struct pvclock_vsyscall_time_info)*NR_CPUS);
366
367         cpu = get_cpu();
368
369         vcpu_time = &hv_clock[cpu].pvti;
370         flags = pvclock_read_flags(vcpu_time);
371
372         put_cpu();
373
374         if (!(flags & PVCLOCK_TSC_STABLE_BIT))
375                 return 1;
376
377         kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
378 #endif
379         return 0;
380 }