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