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"
33 #define TYPE_KVM_CLOCK "kvmclock"
34 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
36 typedef struct KVMClockState
{
44 /* whether machine type supports reliable KVM_GET_CLOCK */
45 bool mach_use_reliable_get_clock
;
47 /* whether the 'clock' value was obtained in a host with
48 * reliable KVM_GET_CLOCK */
49 bool clock_is_reliable
;
52 struct pvclock_vcpu_time_info
{
55 uint64_t tsc_timestamp
;
57 uint32_t tsc_to_system_mul
;
61 } __attribute__((__packed__
)); /* 32 bytes */
63 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
65 CPUState
*cpu
= first_cpu
;
66 CPUX86State
*env
= cpu
->env_ptr
;
67 hwaddr kvmclock_struct_pa
;
68 uint64_t migration_tsc
= env
->tsc
;
69 struct pvclock_vcpu_time_info time
;
75 cpu_synchronize_state(cpu
);
77 if (!(env
->system_time_msr
& 1ULL)) {
78 /* KVM clock not active */
82 kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
83 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
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
;
90 delta
<<= time
.tsc_shift
;
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
;
98 static void kvm_update_clock(KVMClockState
*s
)
100 struct kvm_clock_data data
;
103 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
105 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
108 s
->clock
= data
.clock
;
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.
118 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
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;
124 * - therefore, we can replace a check like
126 * if last KVM_GET_CLOCK was not reliable then
131 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
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.
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)
144 * So we can just use this instead:
146 * if !kvm_has_adjust_clock_stable() then
149 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
152 static void do_kvmclock_ctrl(CPUState
*cpu
, run_on_cpu_data data
)
154 int ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
156 if (ret
&& ret
!= -EINVAL
) {
157 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
161 static void kvmclock_vm_state_change(void *opaque
, int running
,
164 KVMClockState
*s
= opaque
;
166 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
170 struct kvm_clock_data data
= {};
173 * If the host where s->clock was read did not support reliable
174 * KVM_GET_CLOCK, read kvmclock value from memory.
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
;
184 s
->clock_valid
= false;
186 data
.clock
= s
->clock
;
187 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
189 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
193 if (!cap_clock_ctrl
) {
197 run_on_cpu(cpu
, do_kvmclock_ctrl
, RUN_ON_CPU_NULL
);
201 if (s
->clock_valid
) {
205 kvm_synchronize_all_tsc();
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.
213 s
->clock_valid
= true;
217 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
219 KVMClockState
*s
= KVM_CLOCK(dev
);
221 if (!kvm_enabled()) {
222 error_setg(errp
, "kvmclock device requires KVM");
228 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
231 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
233 KVMClockState
*s
= opaque
;
235 return s
->mach_use_reliable_get_clock
;
238 static const VMStateDescription kvmclock_reliable_get_clock
= {
239 .name
= "kvmclock/clock_is_reliable",
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()
250 * When migrating, assume the source has an unreliable
251 * KVM_GET_CLOCK unless told otherwise.
253 static int kvmclock_pre_load(void *opaque
)
255 KVMClockState
*s
= opaque
;
257 s
->clock_is_reliable
= false;
263 * When migrating, read the clock just before migration,
264 * so that the guest clock counts during the events
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.
276 static int kvmclock_pre_save(void *opaque
)
278 KVMClockState
*s
= opaque
;
285 static const VMStateDescription kvmclock_vmsd
= {
288 .minimum_version_id
= 1,
289 .pre_load
= kvmclock_pre_load
,
290 .pre_save
= kvmclock_pre_save
,
291 .fields
= (VMStateField
[]) {
292 VMSTATE_UINT64(clock
, KVMClockState
),
293 VMSTATE_END_OF_LIST()
295 .subsections
= (const VMStateDescription
* []) {
296 &kvmclock_reliable_get_clock
,
301 static 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(),
307 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
309 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 dc
->realize
= kvmclock_realize
;
312 dc
->vmsd
= &kvmclock_vmsd
;
313 dc
->props
= kvmclock_properties
;
316 static const TypeInfo kvmclock_info
= {
317 .name
= TYPE_KVM_CLOCK
,
318 .parent
= TYPE_SYS_BUS_DEVICE
,
319 .instance_size
= sizeof(KVMClockState
),
320 .class_init
= kvmclock_class_init
,
323 /* Note: Must be called after VCPU initialization. */
324 void kvmclock_create(void)
326 X86CPU
*cpu
= X86_CPU(first_cpu
);
329 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
330 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
331 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
335 static void kvmclock_register_types(void)
337 type_register_static(&kvmclock_info
);
340 type_init(kvmclock_register_types
)