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