]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/kvm/clock.c
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20190903' into staging
[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 43
41733249
MG
44 /* whether the 'clock' value was obtained in the 'paused' state */
45 bool runstate_paused;
46
6053a86f
MT
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;
0ec329da
JK
53} KVMClockState;
54
9a48bcd1
AG
55struct 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
66static uint64_t kvmclock_current_nsec(KVMClockState *s)
67{
68 CPUState *cpu = first_cpu;
69 CPUX86State *env = cpu->env_ptr;
346b1215 70 hwaddr kvmclock_struct_pa;
9a48bcd1
AG
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
e2b6c171
DP
78 cpu_synchronize_state(cpu);
79
9a48bcd1
AG
80 if (!(env->system_time_msr & 1ULL)) {
81 /* KVM clock not active */
82 return 0;
83 }
84
346b1215 85 kvmclock_struct_pa = env->system_time_msr & ~1ULL;
9a48bcd1
AG
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}
0ec329da 100
6053a86f
MT
101static 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
4c3e2506
YX
155static 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
1dfb4dd9
LC
164static void kvmclock_vm_state_change(void *opaque, int running,
165 RunState state)
0ec329da
JK
166{
167 KVMClockState *s = opaque;
e76d05c2 168 CPUState *cpu;
f349c12c
EM
169 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
170 int ret;
0ec329da
JK
171
172 if (running) {
5e0b7d88 173 struct kvm_clock_data data = {};
f349c12c 174
6053a86f
MT
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 }
9a48bcd1
AG
185 }
186
6053a86f
MT
187 s->clock_valid = false;
188
00f4d64e 189 data.clock = s->clock;
00f4d64e
MT
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
f349c12c
EM
196 if (!cap_clock_ctrl) {
197 return;
198 }
bdc44640 199 CPU_FOREACH(cpu) {
4c3e2506 200 run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
f349c12c 201 }
00f4d64e 202 } else {
00f4d64e
MT
203
204 if (s->clock_valid) {
205 return;
206 }
317b0a6d 207
41733249
MG
208 s->runstate_paused = runstate_check(RUN_STATE_PAUSED);
209
0fd7e098 210 kvm_synchronize_all_tsc();
1154d84d 211
6053a86f 212 kvm_update_clock(s);
00f4d64e
MT
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;
0ec329da
JK
219 }
220}
221
913bc638 222static void kvmclock_realize(DeviceState *dev, Error **errp)
0ec329da 223{
98bdc0d7 224 KVMClockState *s = KVM_CLOCK(dev);
0ec329da 225
ca2edcd3
EH
226 if (!kvm_enabled()) {
227 error_setg(errp, "kvmclock device requires KVM");
228 return;
229 }
230
6053a86f
MT
231 kvm_update_clock(s);
232
0ec329da 233 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
0ec329da
JK
234}
235
6053a86f
MT
236static bool kvmclock_clock_is_reliable_needed(void *opaque)
237{
238 KVMClockState *s = opaque;
239
240 return s->mach_use_reliable_get_clock;
241}
242
243static 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
c2b01cfe
MC
254/*
255 * When migrating, assume the source has an unreliable
256 * KVM_GET_CLOCK unless told otherwise.
257 */
258static int kvmclock_pre_load(void *opaque)
259{
260 KVMClockState *s = opaque;
261
262 s->clock_is_reliable = false;
263
264 return 0;
265}
266
6053a86f 267/*
41733249
MG
268 * When migrating a running guest, read the clock just
269 * before migration, so that the guest clock counts
270 * during the events between:
6053a86f
MT
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 */
44b1ff31 281static int kvmclock_pre_save(void *opaque)
6053a86f
MT
282{
283 KVMClockState *s = opaque;
284
41733249
MG
285 if (!s->runstate_paused) {
286 kvm_update_clock(s);
287 }
44b1ff31
DDAG
288
289 return 0;
6053a86f
MT
290}
291
0ec329da
JK
292static const VMStateDescription kvmclock_vmsd = {
293 .name = "kvmclock",
294 .version_id = 1,
295 .minimum_version_id = 1,
c2b01cfe 296 .pre_load = kvmclock_pre_load,
6053a86f 297 .pre_save = kvmclock_pre_save,
0ec329da
JK
298 .fields = (VMStateField[]) {
299 VMSTATE_UINT64(clock, KVMClockState),
300 VMSTATE_END_OF_LIST()
6053a86f
MT
301 },
302 .subsections = (const VMStateDescription * []) {
303 &kvmclock_reliable_get_clock,
304 NULL
0ec329da
JK
305 }
306};
307
6053a86f
MT
308static 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
999e12bb
AL
314static void kvmclock_class_init(ObjectClass *klass, void *data)
315{
39bffca2 316 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 317
913bc638 318 dc->realize = kvmclock_realize;
39bffca2 319 dc->vmsd = &kvmclock_vmsd;
6053a86f 320 dc->props = kvmclock_properties;
999e12bb
AL
321}
322
8c43a6f0 323static const TypeInfo kvmclock_info = {
98bdc0d7 324 .name = TYPE_KVM_CLOCK,
39bffca2
AL
325 .parent = TYPE_SYS_BUS_DEVICE,
326 .instance_size = sizeof(KVMClockState),
327 .class_init = kvmclock_class_init,
0ec329da
JK
328};
329
330/* Note: Must be called after VCPU initialization. */
331void kvmclock_create(void)
332{
182735ef
AF
333 X86CPU *cpu = X86_CPU(first_cpu);
334
0ec329da 335 if (kvm_enabled() &&
182735ef
AF
336 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
337 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
98bdc0d7 338 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
0ec329da
JK
339 }
340}
341
83f7d43a 342static void kvmclock_register_types(void)
0ec329da 343{
39bffca2 344 type_register_static(&kvmclock_info);
0ec329da
JK
345}
346
83f7d43a 347type_init(kvmclock_register_types)