sysemu: Split sysemu/runstate.h off sysemu/sysemu.h
[qemu/armbru.git] / hw / i386 / kvm / clock.c
blob80c133a7242c0f7122dc3ec1bc48007b21ddb3f6
1 /*
2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
6 * Authors:
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
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.
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"
30 #include <linux/kvm.h>
31 #include "standard-headers/asm-x86/kvm_para.h"
33 #define TYPE_KVM_CLOCK "kvmclock"
34 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
36 typedef struct KVMClockState {
37 /*< private >*/
38 SysBusDevice busdev;
39 /*< public >*/
41 uint64_t clock;
42 bool clock_valid;
44 /* whether machine type supports reliable KVM_GET_CLOCK */
45 bool mach_use_reliable_get_clock;
47 /* whether the 'clock' value was obtained in a host with
48 * reliable KVM_GET_CLOCK */
49 bool clock_is_reliable;
50 } KVMClockState;
52 struct pvclock_vcpu_time_info {
53 uint32_t version;
54 uint32_t pad0;
55 uint64_t tsc_timestamp;
56 uint64_t system_time;
57 uint32_t tsc_to_system_mul;
58 int8_t tsc_shift;
59 uint8_t flags;
60 uint8_t pad[2];
61 } __attribute__((__packed__)); /* 32 bytes */
63 static uint64_t kvmclock_current_nsec(KVMClockState *s)
65 CPUState *cpu = first_cpu;
66 CPUX86State *env = cpu->env_ptr;
67 hwaddr kvmclock_struct_pa;
68 uint64_t migration_tsc = env->tsc;
69 struct pvclock_vcpu_time_info time;
70 uint64_t delta;
71 uint64_t nsec_lo;
72 uint64_t nsec_hi;
73 uint64_t nsec;
75 cpu_synchronize_state(cpu);
77 if (!(env->system_time_msr & 1ULL)) {
78 /* KVM clock not active */
79 return 0;
82 kvmclock_struct_pa = env->system_time_msr & ~1ULL;
83 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
85 assert(time.tsc_timestamp <= migration_tsc);
86 delta = migration_tsc - time.tsc_timestamp;
87 if (time.tsc_shift < 0) {
88 delta >>= -time.tsc_shift;
89 } else {
90 delta <<= time.tsc_shift;
93 mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
94 nsec = (nsec_lo >> 32) | (nsec_hi << 32);
95 return nsec + time.system_time;
98 static void kvm_update_clock(KVMClockState *s)
100 struct kvm_clock_data data;
101 int ret;
103 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
104 if (ret < 0) {
105 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
106 abort();
108 s->clock = data.clock;
110 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
111 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
112 * can drift from the TSC-based value that is computed by the guest,
113 * so we need to go through kvmclock_current_nsec(). If
114 * kvm_has_adjust_clock_stable() is true, and the flags contain
115 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
116 * and kvmclock_current_nsec() is not necessary.
118 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
120 * - if the host has disabled the kvmclock master clock, the guest already
121 * has protection against time going backwards. This "safety net" is only
122 * absent when kvmclock is stable;
124 * - therefore, we can replace a check like
126 * if last KVM_GET_CLOCK was not reliable then
127 * read from memory
129 * with
131 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
132 * read from memory
134 * However:
136 * - if kvm_has_adjust_clock_stable() returns false, the left side is
137 * always true (KVM_GET_CLOCK is never reliable), and the right side is
138 * unknown (because we don't have data.flags). We must assume it's true
139 * and read from memory.
141 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
142 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
144 * So we can just use this instead:
146 * if !kvm_has_adjust_clock_stable() then
147 * read from memory
149 s->clock_is_reliable = kvm_has_adjust_clock_stable();
152 static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data)
154 int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
156 if (ret && ret != -EINVAL) {
157 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
161 static void kvmclock_vm_state_change(void *opaque, int running,
162 RunState state)
164 KVMClockState *s = opaque;
165 CPUState *cpu;
166 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
167 int ret;
169 if (running) {
170 struct kvm_clock_data data = {};
173 * If the host where s->clock was read did not support reliable
174 * KVM_GET_CLOCK, read kvmclock value from memory.
176 if (!s->clock_is_reliable) {
177 uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
178 /* We can't rely on the saved clock value, just discard it */
179 if (pvclock_via_mem) {
180 s->clock = pvclock_via_mem;
184 s->clock_valid = false;
186 data.clock = s->clock;
187 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
188 if (ret < 0) {
189 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
190 abort();
193 if (!cap_clock_ctrl) {
194 return;
196 CPU_FOREACH(cpu) {
197 run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
199 } else {
201 if (s->clock_valid) {
202 return;
205 kvm_synchronize_all_tsc();
207 kvm_update_clock(s);
209 * If the VM is stopped, declare the clock state valid to
210 * avoid re-reading it on next vmsave (which would return
211 * a different value). Will be reset when the VM is continued.
213 s->clock_valid = true;
217 static void kvmclock_realize(DeviceState *dev, Error **errp)
219 KVMClockState *s = KVM_CLOCK(dev);
221 if (!kvm_enabled()) {
222 error_setg(errp, "kvmclock device requires KVM");
223 return;
226 kvm_update_clock(s);
228 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
231 static bool kvmclock_clock_is_reliable_needed(void *opaque)
233 KVMClockState *s = opaque;
235 return s->mach_use_reliable_get_clock;
238 static const VMStateDescription kvmclock_reliable_get_clock = {
239 .name = "kvmclock/clock_is_reliable",
240 .version_id = 1,
241 .minimum_version_id = 1,
242 .needed = kvmclock_clock_is_reliable_needed,
243 .fields = (VMStateField[]) {
244 VMSTATE_BOOL(clock_is_reliable, KVMClockState),
245 VMSTATE_END_OF_LIST()
250 * When migrating, assume the source has an unreliable
251 * KVM_GET_CLOCK unless told otherwise.
253 static int kvmclock_pre_load(void *opaque)
255 KVMClockState *s = opaque;
257 s->clock_is_reliable = false;
259 return 0;
263 * When migrating, read the clock just before migration,
264 * so that the guest clock counts during the events
265 * between:
267 * * vm_stop()
269 * * pre_save()
271 * This reduces kvmclock difference on migration from 5s
272 * to 0.1s (when max_downtime == 5s), because sending the
273 * final pages of memory (which happens between vm_stop()
274 * and pre_save()) takes max_downtime.
276 static int kvmclock_pre_save(void *opaque)
278 KVMClockState *s = opaque;
280 kvm_update_clock(s);
282 return 0;
285 static const VMStateDescription kvmclock_vmsd = {
286 .name = "kvmclock",
287 .version_id = 1,
288 .minimum_version_id = 1,
289 .pre_load = kvmclock_pre_load,
290 .pre_save = kvmclock_pre_save,
291 .fields = (VMStateField[]) {
292 VMSTATE_UINT64(clock, KVMClockState),
293 VMSTATE_END_OF_LIST()
295 .subsections = (const VMStateDescription * []) {
296 &kvmclock_reliable_get_clock,
297 NULL
301 static Property kvmclock_properties[] = {
302 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
303 mach_use_reliable_get_clock, true),
304 DEFINE_PROP_END_OF_LIST(),
307 static void kvmclock_class_init(ObjectClass *klass, void *data)
309 DeviceClass *dc = DEVICE_CLASS(klass);
311 dc->realize = kvmclock_realize;
312 dc->vmsd = &kvmclock_vmsd;
313 dc->props = kvmclock_properties;
316 static const TypeInfo kvmclock_info = {
317 .name = TYPE_KVM_CLOCK,
318 .parent = TYPE_SYS_BUS_DEVICE,
319 .instance_size = sizeof(KVMClockState),
320 .class_init = kvmclock_class_init,
323 /* Note: Must be called after VCPU initialization. */
324 void kvmclock_create(void)
326 X86CPU *cpu = X86_CPU(first_cpu);
328 if (kvm_enabled() &&
329 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
330 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
331 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
335 static void kvmclock_register_types(void)
337 type_register_static(&kvmclock_info);
340 type_init(kvmclock_register_types)