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