xref: /qemu/hw/i386/kvm/clock.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * QEMU KVM support, paravirtual clock device
3  *
4  * Copyright (C) 2011 Siemens AG
5  *
6  * Authors:
7  *  Jan Kiszka        <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL version 2.
10  * See the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "qemu/osdep.h"
17 #include "cpu.h"
18 #include "qemu/host-utils.h"
19 #include "qemu/module.h"
20 #include "sysemu/kvm.h"
21 #include "sysemu/runstate.h"
22 #include "sysemu/hw_accel.h"
23 #include "kvm_i386.h"
24 #include "migration/vmstate.h"
25 #include "hw/sysbus.h"
26 #include "hw/kvm/clock.h"
27 #include "hw/qdev-properties.h"
28 #include "qapi/error.h"
29 
30 #include <linux/kvm.h>
31 #include "standard-headers/asm-x86/kvm_para.h"
32 #include "qom/object.h"
33 
34 #define TYPE_KVM_CLOCK "kvmclock"
35 typedef struct KVMClockState KVMClockState;
36 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
37 
38 struct KVMClockState {
39     /*< private >*/
40     SysBusDevice busdev;
41     /*< public >*/
42 
43     uint64_t clock;
44     bool clock_valid;
45 
46     /* whether the 'clock' value was obtained in the 'paused' state */
47     bool runstate_paused;
48 
49     /* whether machine type supports reliable KVM_GET_CLOCK */
50     bool mach_use_reliable_get_clock;
51 
52     /* whether the 'clock' value was obtained in a host with
53      * reliable KVM_GET_CLOCK */
54     bool clock_is_reliable;
55 };
56 
57 struct pvclock_vcpu_time_info {
58     uint32_t   version;
59     uint32_t   pad0;
60     uint64_t   tsc_timestamp;
61     uint64_t   system_time;
62     uint32_t   tsc_to_system_mul;
63     int8_t     tsc_shift;
64     uint8_t    flags;
65     uint8_t    pad[2];
66 } __attribute__((__packed__)); /* 32 bytes */
67 
68 static uint64_t kvmclock_current_nsec(KVMClockState *s)
69 {
70     CPUState *cpu = first_cpu;
71     CPUX86State *env = cpu->env_ptr;
72     hwaddr kvmclock_struct_pa;
73     uint64_t migration_tsc = env->tsc;
74     struct pvclock_vcpu_time_info time;
75     uint64_t delta;
76     uint64_t nsec_lo;
77     uint64_t nsec_hi;
78     uint64_t nsec;
79 
80     cpu_synchronize_state(cpu);
81 
82     if (!(env->system_time_msr & 1ULL)) {
83         /* KVM clock not active */
84         return 0;
85     }
86 
87     kvmclock_struct_pa = env->system_time_msr & ~1ULL;
88     cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
89 
90     assert(time.tsc_timestamp <= migration_tsc);
91     delta = migration_tsc - time.tsc_timestamp;
92     if (time.tsc_shift < 0) {
93         delta >>= -time.tsc_shift;
94     } else {
95         delta <<= time.tsc_shift;
96     }
97 
98     mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
99     nsec = (nsec_lo >> 32) | (nsec_hi << 32);
100     return nsec + time.system_time;
101 }
102 
103 static void kvm_update_clock(KVMClockState *s)
104 {
105     struct kvm_clock_data data;
106     int ret;
107 
108     ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
109     if (ret < 0) {
110         fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
111                 abort();
112     }
113     s->clock = data.clock;
114 
115     /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
116      * essentially CLOCK_MONOTONIC plus a guest-specific adjustment.  This
117      * can drift from the TSC-based value that is computed by the guest,
118      * so we need to go through kvmclock_current_nsec().  If
119      * kvm_has_adjust_clock_stable() is true, and the flags contain
120      * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
121      * and kvmclock_current_nsec() is not necessary.
122      *
123      * Here, however, we need not check KVM_CLOCK_TSC_STABLE.  This is because:
124      *
125      * - if the host has disabled the kvmclock master clock, the guest already
126      *   has protection against time going backwards.  This "safety net" is only
127      *   absent when kvmclock is stable;
128      *
129      * - therefore, we can replace a check like
130      *
131      *       if last KVM_GET_CLOCK was not reliable then
132      *               read from memory
133      *
134      *   with
135      *
136      *       if last KVM_GET_CLOCK was not reliable && masterclock is enabled
137      *               read from memory
138      *
139      * However:
140      *
141      * - if kvm_has_adjust_clock_stable() returns false, the left side is
142      *   always true (KVM_GET_CLOCK is never reliable), and the right side is
143      *   unknown (because we don't have data.flags).  We must assume it's true
144      *   and read from memory.
145      *
146      * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
147      *   is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
148      *
149      * So we can just use this instead:
150      *
151      *       if !kvm_has_adjust_clock_stable() then
152      *               read from memory
153      */
154     s->clock_is_reliable = kvm_has_adjust_clock_stable();
155 }
156 
157 static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data)
158 {
159     int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
160 
161     if (ret && ret != -EINVAL) {
162         fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
163     }
164 }
165 
166 static void kvmclock_vm_state_change(void *opaque, int running,
167                                      RunState state)
168 {
169     KVMClockState *s = opaque;
170     CPUState *cpu;
171     int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
172     int ret;
173 
174     if (running) {
175         struct kvm_clock_data data = {};
176 
177         /*
178          * If the host where s->clock was read did not support reliable
179          * KVM_GET_CLOCK, read kvmclock value from memory.
180          */
181         if (!s->clock_is_reliable) {
182             uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
183             /* We can't rely on the saved clock value, just discard it */
184             if (pvclock_via_mem) {
185                 s->clock = pvclock_via_mem;
186             }
187         }
188 
189         s->clock_valid = false;
190 
191         data.clock = s->clock;
192         ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
193         if (ret < 0) {
194             fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
195             abort();
196         }
197 
198         if (!cap_clock_ctrl) {
199             return;
200         }
201         CPU_FOREACH(cpu) {
202             run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
203         }
204     } else {
205 
206         if (s->clock_valid) {
207             return;
208         }
209 
210         s->runstate_paused = runstate_check(RUN_STATE_PAUSED);
211 
212         kvm_synchronize_all_tsc();
213 
214         kvm_update_clock(s);
215         /*
216          * If the VM is stopped, declare the clock state valid to
217          * avoid re-reading it on next vmsave (which would return
218          * a different value). Will be reset when the VM is continued.
219          */
220         s->clock_valid = true;
221     }
222 }
223 
224 static void kvmclock_realize(DeviceState *dev, Error **errp)
225 {
226     KVMClockState *s = KVM_CLOCK(dev);
227 
228     if (!kvm_enabled()) {
229         error_setg(errp, "kvmclock device requires KVM");
230         return;
231     }
232 
233     kvm_update_clock(s);
234 
235     qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
236 }
237 
238 static bool kvmclock_clock_is_reliable_needed(void *opaque)
239 {
240     KVMClockState *s = opaque;
241 
242     return s->mach_use_reliable_get_clock;
243 }
244 
245 static const VMStateDescription kvmclock_reliable_get_clock = {
246     .name = "kvmclock/clock_is_reliable",
247     .version_id = 1,
248     .minimum_version_id = 1,
249     .needed = kvmclock_clock_is_reliable_needed,
250     .fields = (VMStateField[]) {
251         VMSTATE_BOOL(clock_is_reliable, KVMClockState),
252         VMSTATE_END_OF_LIST()
253     }
254 };
255 
256 /*
257  * When migrating, assume the source has an unreliable
258  * KVM_GET_CLOCK unless told otherwise.
259  */
260 static int kvmclock_pre_load(void *opaque)
261 {
262     KVMClockState *s = opaque;
263 
264     s->clock_is_reliable = false;
265 
266     return 0;
267 }
268 
269 /*
270  * When migrating a running guest, read the clock just
271  * before migration, so that the guest clock counts
272  * during the events between:
273  *
274  *  * vm_stop()
275  *  *
276  *  * pre_save()
277  *
278  *  This reduces kvmclock difference on migration from 5s
279  *  to 0.1s (when max_downtime == 5s), because sending the
280  *  final pages of memory (which happens between vm_stop()
281  *  and pre_save()) takes max_downtime.
282  */
283 static int kvmclock_pre_save(void *opaque)
284 {
285     KVMClockState *s = opaque;
286 
287     if (!s->runstate_paused) {
288         kvm_update_clock(s);
289     }
290 
291     return 0;
292 }
293 
294 static const VMStateDescription kvmclock_vmsd = {
295     .name = "kvmclock",
296     .version_id = 1,
297     .minimum_version_id = 1,
298     .pre_load = kvmclock_pre_load,
299     .pre_save = kvmclock_pre_save,
300     .fields = (VMStateField[]) {
301         VMSTATE_UINT64(clock, KVMClockState),
302         VMSTATE_END_OF_LIST()
303     },
304     .subsections = (const VMStateDescription * []) {
305         &kvmclock_reliable_get_clock,
306         NULL
307     }
308 };
309 
310 static Property kvmclock_properties[] = {
311     DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
312                       mach_use_reliable_get_clock, true),
313     DEFINE_PROP_END_OF_LIST(),
314 };
315 
316 static void kvmclock_class_init(ObjectClass *klass, void *data)
317 {
318     DeviceClass *dc = DEVICE_CLASS(klass);
319 
320     dc->realize = kvmclock_realize;
321     dc->vmsd = &kvmclock_vmsd;
322     device_class_set_props(dc, kvmclock_properties);
323 }
324 
325 static const TypeInfo kvmclock_info = {
326     .name          = TYPE_KVM_CLOCK,
327     .parent        = TYPE_SYS_BUS_DEVICE,
328     .instance_size = sizeof(KVMClockState),
329     .class_init    = kvmclock_class_init,
330 };
331 
332 /* Note: Must be called after VCPU initialization. */
333 void kvmclock_create(void)
334 {
335     X86CPU *cpu = X86_CPU(first_cpu);
336 
337     if (kvm_enabled() &&
338         cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
339                                        (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
340         sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
341     }
342 }
343 
344 static void kvmclock_register_types(void)
345 {
346     type_register_static(&kvmclock_info);
347 }
348 
349 type_init(kvmclock_register_types)
350