2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
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"
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"
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"
32 #include "qom/object.h"
34 #define TYPE_KVM_CLOCK "kvmclock"
35 typedef struct KVMClockState KVMClockState
;
36 DECLARE_INSTANCE_CHECKER(KVMClockState
, KVM_CLOCK
,
39 struct KVMClockState
{
47 /* whether the 'clock' value was obtained in the 'paused' state */
50 /* whether machine type supports reliable KVM_GET_CLOCK */
51 bool mach_use_reliable_get_clock
;
53 /* whether the 'clock' value was obtained in a host with
54 * reliable KVM_GET_CLOCK */
55 bool clock_is_reliable
;
58 struct pvclock_vcpu_time_info
{
61 uint64_t tsc_timestamp
;
63 uint32_t tsc_to_system_mul
;
67 } __attribute__((__packed__
)); /* 32 bytes */
69 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
71 CPUState
*cpu
= first_cpu
;
72 CPUX86State
*env
= cpu
->env_ptr
;
73 hwaddr kvmclock_struct_pa
;
74 uint64_t migration_tsc
= env
->tsc
;
75 struct pvclock_vcpu_time_info time
;
81 cpu_synchronize_state(cpu
);
83 if (!(env
->system_time_msr
& 1ULL)) {
84 /* KVM clock not active */
88 kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
89 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
91 assert(time
.tsc_timestamp
<= migration_tsc
);
92 delta
= migration_tsc
- time
.tsc_timestamp
;
93 if (time
.tsc_shift
< 0) {
94 delta
>>= -time
.tsc_shift
;
96 delta
<<= time
.tsc_shift
;
99 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
100 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
101 return nsec
+ time
.system_time
;
104 static void kvm_update_clock(KVMClockState
*s
)
106 struct kvm_clock_data data
;
109 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
111 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
114 s
->clock
= data
.clock
;
116 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
117 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
118 * can drift from the TSC-based value that is computed by the guest,
119 * so we need to go through kvmclock_current_nsec(). If
120 * kvm_has_adjust_clock_stable() is true, and the flags contain
121 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
122 * and kvmclock_current_nsec() is not necessary.
124 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
126 * - if the host has disabled the kvmclock master clock, the guest already
127 * has protection against time going backwards. This "safety net" is only
128 * absent when kvmclock is stable;
130 * - therefore, we can replace a check like
132 * if last KVM_GET_CLOCK was not reliable then
137 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
142 * - if kvm_has_adjust_clock_stable() returns false, the left side is
143 * always true (KVM_GET_CLOCK is never reliable), and the right side is
144 * unknown (because we don't have data.flags). We must assume it's true
145 * and read from memory.
147 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
148 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
150 * So we can just use this instead:
152 * if !kvm_has_adjust_clock_stable() then
155 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
158 static void do_kvmclock_ctrl(CPUState
*cpu
, run_on_cpu_data data
)
160 int ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
162 if (ret
&& ret
!= -EINVAL
) {
163 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
167 static void kvmclock_vm_state_change(void *opaque
, int running
,
170 KVMClockState
*s
= opaque
;
172 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
176 struct kvm_clock_data data
= {};
179 * If the host where s->clock was read did not support reliable
180 * KVM_GET_CLOCK, read kvmclock value from memory.
182 if (!s
->clock_is_reliable
) {
183 uint64_t pvclock_via_mem
= kvmclock_current_nsec(s
);
184 /* We can't rely on the saved clock value, just discard it */
185 if (pvclock_via_mem
) {
186 s
->clock
= pvclock_via_mem
;
190 s
->clock_valid
= false;
192 data
.clock
= s
->clock
;
193 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
195 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
199 if (!cap_clock_ctrl
) {
203 run_on_cpu(cpu
, do_kvmclock_ctrl
, RUN_ON_CPU_NULL
);
207 if (s
->clock_valid
) {
211 s
->runstate_paused
= runstate_check(RUN_STATE_PAUSED
);
213 kvm_synchronize_all_tsc();
217 * If the VM is stopped, declare the clock state valid to
218 * avoid re-reading it on next vmsave (which would return
219 * a different value). Will be reset when the VM is continued.
221 s
->clock_valid
= true;
225 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
227 KVMClockState
*s
= KVM_CLOCK(dev
);
229 if (!kvm_enabled()) {
230 error_setg(errp
, "kvmclock device requires KVM");
236 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
239 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
241 KVMClockState
*s
= opaque
;
243 return s
->mach_use_reliable_get_clock
;
246 static const VMStateDescription kvmclock_reliable_get_clock
= {
247 .name
= "kvmclock/clock_is_reliable",
249 .minimum_version_id
= 1,
250 .needed
= kvmclock_clock_is_reliable_needed
,
251 .fields
= (VMStateField
[]) {
252 VMSTATE_BOOL(clock_is_reliable
, KVMClockState
),
253 VMSTATE_END_OF_LIST()
258 * When migrating, assume the source has an unreliable
259 * KVM_GET_CLOCK unless told otherwise.
261 static int kvmclock_pre_load(void *opaque
)
263 KVMClockState
*s
= opaque
;
265 s
->clock_is_reliable
= false;
271 * When migrating a running guest, read the clock just
272 * before migration, so that the guest clock counts
273 * during the events between:
279 * This reduces kvmclock difference on migration from 5s
280 * to 0.1s (when max_downtime == 5s), because sending the
281 * final pages of memory (which happens between vm_stop()
282 * and pre_save()) takes max_downtime.
284 static int kvmclock_pre_save(void *opaque
)
286 KVMClockState
*s
= opaque
;
288 if (!s
->runstate_paused
) {
295 static const VMStateDescription kvmclock_vmsd
= {
298 .minimum_version_id
= 1,
299 .pre_load
= kvmclock_pre_load
,
300 .pre_save
= kvmclock_pre_save
,
301 .fields
= (VMStateField
[]) {
302 VMSTATE_UINT64(clock
, KVMClockState
),
303 VMSTATE_END_OF_LIST()
305 .subsections
= (const VMStateDescription
* []) {
306 &kvmclock_reliable_get_clock
,
311 static Property kvmclock_properties
[] = {
312 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState
,
313 mach_use_reliable_get_clock
, true),
314 DEFINE_PROP_END_OF_LIST(),
317 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
319 DeviceClass
*dc
= DEVICE_CLASS(klass
);
321 dc
->realize
= kvmclock_realize
;
322 dc
->vmsd
= &kvmclock_vmsd
;
323 device_class_set_props(dc
, kvmclock_properties
);
326 static const TypeInfo kvmclock_info
= {
327 .name
= TYPE_KVM_CLOCK
,
328 .parent
= TYPE_SYS_BUS_DEVICE
,
329 .instance_size
= sizeof(KVMClockState
),
330 .class_init
= kvmclock_class_init
,
333 /* Note: Must be called after VCPU initialization. */
334 void kvmclock_create(void)
336 X86CPU
*cpu
= X86_CPU(first_cpu
);
339 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
340 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
341 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
345 static void kvmclock_register_types(void)
347 type_register_static(&kvmclock_info
);
350 type_init(kvmclock_register_types
)