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