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"
17 #include "qemu-common.h"
19 #include "qemu/host-utils.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/kvm.h"
22 #include "sysemu/hw_accel.h"
24 #include "hw/sysbus.h"
25 #include "hw/kvm/clock.h"
26 #include "qapi/error.h"
28 #include <linux/kvm.h>
29 #include <linux/kvm_para.h>
31 #define TYPE_KVM_CLOCK "kvmclock"
32 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
34 typedef struct KVMClockState
{
42 /* whether machine type supports reliable KVM_GET_CLOCK */
43 bool mach_use_reliable_get_clock
;
45 /* whether the 'clock' value was obtained in a host with
46 * reliable KVM_GET_CLOCK */
47 bool clock_is_reliable
;
50 struct pvclock_vcpu_time_info
{
53 uint64_t tsc_timestamp
;
55 uint32_t tsc_to_system_mul
;
59 } __attribute__((__packed__
)); /* 32 bytes */
61 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
63 CPUState
*cpu
= first_cpu
;
64 CPUX86State
*env
= cpu
->env_ptr
;
65 hwaddr kvmclock_struct_pa
;
66 uint64_t migration_tsc
= env
->tsc
;
67 struct pvclock_vcpu_time_info time
;
73 cpu_synchronize_state(cpu
);
75 if (!(env
->system_time_msr
& 1ULL)) {
76 /* KVM clock not active */
80 kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
81 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
83 assert(time
.tsc_timestamp
<= migration_tsc
);
84 delta
= migration_tsc
- time
.tsc_timestamp
;
85 if (time
.tsc_shift
< 0) {
86 delta
>>= -time
.tsc_shift
;
88 delta
<<= time
.tsc_shift
;
91 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
92 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
93 return nsec
+ time
.system_time
;
96 static void kvm_update_clock(KVMClockState
*s
)
98 struct kvm_clock_data data
;
101 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
103 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
106 s
->clock
= data
.clock
;
108 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
109 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
110 * can drift from the TSC-based value that is computed by the guest,
111 * so we need to go through kvmclock_current_nsec(). If
112 * kvm_has_adjust_clock_stable() is true, and the flags contain
113 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
114 * and kvmclock_current_nsec() is not necessary.
116 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
118 * - if the host has disabled the kvmclock master clock, the guest already
119 * has protection against time going backwards. This "safety net" is only
120 * absent when kvmclock is stable;
122 * - therefore, we can replace a check like
124 * if last KVM_GET_CLOCK was not reliable then
129 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
134 * - if kvm_has_adjust_clock_stable() returns false, the left side is
135 * always true (KVM_GET_CLOCK is never reliable), and the right side is
136 * unknown (because we don't have data.flags). We must assume it's true
137 * and read from memory.
139 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
140 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
142 * So we can just use this instead:
144 * if !kvm_has_adjust_clock_stable() then
147 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
150 static void kvmclock_vm_state_change(void *opaque
, int running
,
153 KVMClockState
*s
= opaque
;
155 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
159 struct kvm_clock_data data
= {};
162 * If the host where s->clock was read did not support reliable
163 * KVM_GET_CLOCK, read kvmclock value from memory.
165 if (!s
->clock_is_reliable
) {
166 uint64_t pvclock_via_mem
= kvmclock_current_nsec(s
);
167 /* We can't rely on the saved clock value, just discard it */
168 if (pvclock_via_mem
) {
169 s
->clock
= pvclock_via_mem
;
173 s
->clock_valid
= false;
175 data
.clock
= s
->clock
;
176 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
178 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
182 if (!cap_clock_ctrl
) {
186 ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
188 if (ret
!= -EINVAL
) {
189 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
196 if (s
->clock_valid
) {
200 kvm_synchronize_all_tsc();
204 * If the VM is stopped, declare the clock state valid to
205 * avoid re-reading it on next vmsave (which would return
206 * a different value). Will be reset when the VM is continued.
208 s
->clock_valid
= true;
212 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
214 KVMClockState
*s
= KVM_CLOCK(dev
);
216 if (!kvm_enabled()) {
217 error_setg(errp
, "kvmclock device requires KVM");
223 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
226 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
228 KVMClockState
*s
= opaque
;
230 return s
->mach_use_reliable_get_clock
;
233 static const VMStateDescription kvmclock_reliable_get_clock
= {
234 .name
= "kvmclock/clock_is_reliable",
236 .minimum_version_id
= 1,
237 .needed
= kvmclock_clock_is_reliable_needed
,
238 .fields
= (VMStateField
[]) {
239 VMSTATE_BOOL(clock_is_reliable
, KVMClockState
),
240 VMSTATE_END_OF_LIST()
245 * When migrating, assume the source has an unreliable
246 * KVM_GET_CLOCK unless told otherwise.
248 static int kvmclock_pre_load(void *opaque
)
250 KVMClockState
*s
= opaque
;
252 s
->clock_is_reliable
= false;
258 * When migrating, read the clock just before migration,
259 * so that the guest clock counts during the events
266 * This reduces kvmclock difference on migration from 5s
267 * to 0.1s (when max_downtime == 5s), because sending the
268 * final pages of memory (which happens between vm_stop()
269 * and pre_save()) takes max_downtime.
271 static int kvmclock_pre_save(void *opaque
)
273 KVMClockState
*s
= opaque
;
280 static const VMStateDescription kvmclock_vmsd
= {
283 .minimum_version_id
= 1,
284 .pre_load
= kvmclock_pre_load
,
285 .pre_save
= kvmclock_pre_save
,
286 .fields
= (VMStateField
[]) {
287 VMSTATE_UINT64(clock
, KVMClockState
),
288 VMSTATE_END_OF_LIST()
290 .subsections
= (const VMStateDescription
* []) {
291 &kvmclock_reliable_get_clock
,
296 static Property kvmclock_properties
[] = {
297 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState
,
298 mach_use_reliable_get_clock
, true),
299 DEFINE_PROP_END_OF_LIST(),
302 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
304 DeviceClass
*dc
= DEVICE_CLASS(klass
);
306 dc
->realize
= kvmclock_realize
;
307 dc
->vmsd
= &kvmclock_vmsd
;
308 dc
->props
= kvmclock_properties
;
311 static const TypeInfo kvmclock_info
= {
312 .name
= TYPE_KVM_CLOCK
,
313 .parent
= TYPE_SYS_BUS_DEVICE
,
314 .instance_size
= sizeof(KVMClockState
),
315 .class_init
= kvmclock_class_init
,
318 /* Note: Must be called after VCPU initialization. */
319 void kvmclock_create(void)
321 X86CPU
*cpu
= X86_CPU(first_cpu
);
324 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
325 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
326 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
330 static void kvmclock_register_types(void)
332 type_register_static(&kvmclock_info
);
335 type_init(kvmclock_register_types
)