]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/kvm/clock.c
sysemu: Split sysemu/runstate.h off sysemu/sysemu.h
[mirror_qemu.git] / hw / i386 / kvm / clock.c
CommitLineData
0ec329da
JK
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 *
6b620ca3
PB
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.
0ec329da
JK
14 */
15
b6a0aa05 16#include "qemu/osdep.h"
33c11879 17#include "cpu.h"
9a48bcd1 18#include "qemu/host-utils.h"
0b8fa32f 19#include "qemu/module.h"
9c17d615 20#include "sysemu/kvm.h"
54d31236 21#include "sysemu/runstate.h"
e2b6c171 22#include "sysemu/hw_accel.h"
0fd7e098 23#include "kvm_i386.h"
d6454270 24#include "migration/vmstate.h"
3b9a6ee5
JK
25#include "hw/sysbus.h"
26#include "hw/kvm/clock.h"
a27bd6c7 27#include "hw/qdev-properties.h"
ca2edcd3 28#include "qapi/error.h"
0ec329da 29
0ec329da 30#include <linux/kvm.h>
1814eab6 31#include "standard-headers/asm-x86/kvm_para.h"
0ec329da 32
98bdc0d7
HT
33#define TYPE_KVM_CLOCK "kvmclock"
34#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
35
0ec329da 36typedef struct KVMClockState {
98bdc0d7 37 /*< private >*/
0ec329da 38 SysBusDevice busdev;
98bdc0d7
HT
39 /*< public >*/
40
0ec329da
JK
41 uint64_t clock;
42 bool clock_valid;
6053a86f
MT
43
44 /* whether machine type supports reliable KVM_GET_CLOCK */
45 bool mach_use_reliable_get_clock;
46
47 /* whether the 'clock' value was obtained in a host with
48 * reliable KVM_GET_CLOCK */
49 bool clock_is_reliable;
0ec329da
JK
50} KVMClockState;
51
9a48bcd1
AG
52struct 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 */
62
63static uint64_t kvmclock_current_nsec(KVMClockState *s)
64{
65 CPUState *cpu = first_cpu;
66 CPUX86State *env = cpu->env_ptr;
346b1215 67 hwaddr kvmclock_struct_pa;
9a48bcd1
AG
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;
74
e2b6c171
DP
75 cpu_synchronize_state(cpu);
76
9a48bcd1
AG
77 if (!(env->system_time_msr & 1ULL)) {
78 /* KVM clock not active */
79 return 0;
80 }
81
346b1215 82 kvmclock_struct_pa = env->system_time_msr & ~1ULL;
9a48bcd1
AG
83 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
84
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;
91 }
92
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;
96}
0ec329da 97
6053a86f
MT
98static void kvm_update_clock(KVMClockState *s)
99{
100 struct kvm_clock_data data;
101 int ret;
102
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();
107 }
108 s->clock = data.clock;
109
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.
117 *
118 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
119 *
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;
123 *
124 * - therefore, we can replace a check like
125 *
126 * if last KVM_GET_CLOCK was not reliable then
127 * read from memory
128 *
129 * with
130 *
131 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
132 * read from memory
133 *
134 * However:
135 *
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.
140 *
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)
143 *
144 * So we can just use this instead:
145 *
146 * if !kvm_has_adjust_clock_stable() then
147 * read from memory
148 */
149 s->clock_is_reliable = kvm_has_adjust_clock_stable();
150}
151
4c3e2506
YX
152static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data)
153{
154 int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
155
156 if (ret && ret != -EINVAL) {
157 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
158 }
159}
160
1dfb4dd9
LC
161static void kvmclock_vm_state_change(void *opaque, int running,
162 RunState state)
0ec329da
JK
163{
164 KVMClockState *s = opaque;
e76d05c2 165 CPUState *cpu;
f349c12c
EM
166 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
167 int ret;
0ec329da
JK
168
169 if (running) {
5e0b7d88 170 struct kvm_clock_data data = {};
f349c12c 171
6053a86f
MT
172 /*
173 * If the host where s->clock was read did not support reliable
174 * KVM_GET_CLOCK, read kvmclock value from memory.
175 */
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;
181 }
9a48bcd1
AG
182 }
183
6053a86f
MT
184 s->clock_valid = false;
185
00f4d64e 186 data.clock = s->clock;
00f4d64e
MT
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();
191 }
192
f349c12c
EM
193 if (!cap_clock_ctrl) {
194 return;
195 }
bdc44640 196 CPU_FOREACH(cpu) {
4c3e2506 197 run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
f349c12c 198 }
00f4d64e 199 } else {
00f4d64e
MT
200
201 if (s->clock_valid) {
202 return;
203 }
317b0a6d 204
0fd7e098 205 kvm_synchronize_all_tsc();
1154d84d 206
6053a86f 207 kvm_update_clock(s);
00f4d64e
MT
208 /*
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.
212 */
213 s->clock_valid = true;
0ec329da
JK
214 }
215}
216
913bc638 217static void kvmclock_realize(DeviceState *dev, Error **errp)
0ec329da 218{
98bdc0d7 219 KVMClockState *s = KVM_CLOCK(dev);
0ec329da 220
ca2edcd3
EH
221 if (!kvm_enabled()) {
222 error_setg(errp, "kvmclock device requires KVM");
223 return;
224 }
225
6053a86f
MT
226 kvm_update_clock(s);
227
0ec329da 228 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
0ec329da
JK
229}
230
6053a86f
MT
231static bool kvmclock_clock_is_reliable_needed(void *opaque)
232{
233 KVMClockState *s = opaque;
234
235 return s->mach_use_reliable_get_clock;
236}
237
238static 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()
246 }
247};
248
c2b01cfe
MC
249/*
250 * When migrating, assume the source has an unreliable
251 * KVM_GET_CLOCK unless told otherwise.
252 */
253static int kvmclock_pre_load(void *opaque)
254{
255 KVMClockState *s = opaque;
256
257 s->clock_is_reliable = false;
258
259 return 0;
260}
261
6053a86f
MT
262/*
263 * When migrating, read the clock just before migration,
264 * so that the guest clock counts during the events
265 * between:
266 *
267 * * vm_stop()
268 * *
269 * * pre_save()
270 *
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.
275 */
44b1ff31 276static int kvmclock_pre_save(void *opaque)
6053a86f
MT
277{
278 KVMClockState *s = opaque;
279
280 kvm_update_clock(s);
44b1ff31
DDAG
281
282 return 0;
6053a86f
MT
283}
284
0ec329da
JK
285static const VMStateDescription kvmclock_vmsd = {
286 .name = "kvmclock",
287 .version_id = 1,
288 .minimum_version_id = 1,
c2b01cfe 289 .pre_load = kvmclock_pre_load,
6053a86f 290 .pre_save = kvmclock_pre_save,
0ec329da
JK
291 .fields = (VMStateField[]) {
292 VMSTATE_UINT64(clock, KVMClockState),
293 VMSTATE_END_OF_LIST()
6053a86f
MT
294 },
295 .subsections = (const VMStateDescription * []) {
296 &kvmclock_reliable_get_clock,
297 NULL
0ec329da
JK
298 }
299};
300
6053a86f
MT
301static 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(),
305};
306
999e12bb
AL
307static void kvmclock_class_init(ObjectClass *klass, void *data)
308{
39bffca2 309 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 310
913bc638 311 dc->realize = kvmclock_realize;
39bffca2 312 dc->vmsd = &kvmclock_vmsd;
6053a86f 313 dc->props = kvmclock_properties;
999e12bb
AL
314}
315
8c43a6f0 316static const TypeInfo kvmclock_info = {
98bdc0d7 317 .name = TYPE_KVM_CLOCK,
39bffca2
AL
318 .parent = TYPE_SYS_BUS_DEVICE,
319 .instance_size = sizeof(KVMClockState),
320 .class_init = kvmclock_class_init,
0ec329da
JK
321};
322
323/* Note: Must be called after VCPU initialization. */
324void kvmclock_create(void)
325{
182735ef
AF
326 X86CPU *cpu = X86_CPU(first_cpu);
327
0ec329da 328 if (kvm_enabled() &&
182735ef
AF
329 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
330 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
98bdc0d7 331 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
0ec329da
JK
332 }
333}
334
83f7d43a 335static void kvmclock_register_types(void)
0ec329da 336{
39bffca2 337 type_register_static(&kvmclock_info);
0ec329da
JK
338}
339
83f7d43a 340type_init(kvmclock_register_types)