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"
23 #include "hw/sysbus.h"
24 #include "hw/kvm/clock.h"
25 #include "qapi/error.h"
27 #include <linux/kvm.h>
28 #include <linux/kvm_para.h>
30 #define TYPE_KVM_CLOCK "kvmclock"
31 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
33 typedef struct KVMClockState
{
41 /* whether machine type supports reliable KVM_GET_CLOCK */
42 bool mach_use_reliable_get_clock
;
44 /* whether the 'clock' value was obtained in a host with
45 * reliable KVM_GET_CLOCK */
46 bool clock_is_reliable
;
49 struct pvclock_vcpu_time_info
{
52 uint64_t tsc_timestamp
;
54 uint32_t tsc_to_system_mul
;
58 } __attribute__((__packed__
)); /* 32 bytes */
60 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
62 CPUState
*cpu
= first_cpu
;
63 CPUX86State
*env
= cpu
->env_ptr
;
64 hwaddr kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
65 uint64_t migration_tsc
= env
->tsc
;
66 struct pvclock_vcpu_time_info time
;
72 if (!(env
->system_time_msr
& 1ULL)) {
73 /* KVM clock not active */
77 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
79 assert(time
.tsc_timestamp
<= migration_tsc
);
80 delta
= migration_tsc
- time
.tsc_timestamp
;
81 if (time
.tsc_shift
< 0) {
82 delta
>>= -time
.tsc_shift
;
84 delta
<<= time
.tsc_shift
;
87 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
88 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
89 return nsec
+ time
.system_time
;
92 static void kvm_update_clock(KVMClockState
*s
)
94 struct kvm_clock_data data
;
97 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
99 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
102 s
->clock
= data
.clock
;
104 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
105 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
106 * can drift from the TSC-based value that is computed by the guest,
107 * so we need to go through kvmclock_current_nsec(). If
108 * kvm_has_adjust_clock_stable() is true, and the flags contain
109 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
110 * and kvmclock_current_nsec() is not necessary.
112 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
114 * - if the host has disabled the kvmclock master clock, the guest already
115 * has protection against time going backwards. This "safety net" is only
116 * absent when kvmclock is stable;
118 * - therefore, we can replace a check like
120 * if last KVM_GET_CLOCK was not reliable then
125 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
130 * - if kvm_has_adjust_clock_stable() returns false, the left side is
131 * always true (KVM_GET_CLOCK is never reliable), and the right side is
132 * unknown (because we don't have data.flags). We must assume it's true
133 * and read from memory.
135 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
136 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
138 * So we can just use this instead:
140 * if !kvm_has_adjust_clock_stable() then
143 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
146 static void kvmclock_vm_state_change(void *opaque
, int running
,
149 KVMClockState
*s
= opaque
;
151 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
155 struct kvm_clock_data data
= {};
158 * If the host where s->clock was read did not support reliable
159 * KVM_GET_CLOCK, read kvmclock value from memory.
161 if (!s
->clock_is_reliable
) {
162 uint64_t pvclock_via_mem
= kvmclock_current_nsec(s
);
163 /* We can't rely on the saved clock value, just discard it */
164 if (pvclock_via_mem
) {
165 s
->clock
= pvclock_via_mem
;
169 s
->clock_valid
= false;
171 data
.clock
= s
->clock
;
172 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
174 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
178 if (!cap_clock_ctrl
) {
182 ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
184 if (ret
!= -EINVAL
) {
185 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
192 if (s
->clock_valid
) {
196 kvm_synchronize_all_tsc();
200 * If the VM is stopped, declare the clock state valid to
201 * avoid re-reading it on next vmsave (which would return
202 * a different value). Will be reset when the VM is continued.
204 s
->clock_valid
= true;
208 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
210 KVMClockState
*s
= KVM_CLOCK(dev
);
212 if (!kvm_enabled()) {
213 error_setg(errp
, "kvmclock device requires KVM");
219 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
222 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
224 KVMClockState
*s
= opaque
;
226 return s
->mach_use_reliable_get_clock
;
229 static const VMStateDescription kvmclock_reliable_get_clock
= {
230 .name
= "kvmclock/clock_is_reliable",
232 .minimum_version_id
= 1,
233 .needed
= kvmclock_clock_is_reliable_needed
,
234 .fields
= (VMStateField
[]) {
235 VMSTATE_BOOL(clock_is_reliable
, KVMClockState
),
236 VMSTATE_END_OF_LIST()
241 * When migrating, read the clock just before migration,
242 * so that the guest clock counts during the events
249 * This reduces kvmclock difference on migration from 5s
250 * to 0.1s (when max_downtime == 5s), because sending the
251 * final pages of memory (which happens between vm_stop()
252 * and pre_save()) takes max_downtime.
254 static void kvmclock_pre_save(void *opaque
)
256 KVMClockState
*s
= opaque
;
261 static const VMStateDescription kvmclock_vmsd
= {
264 .minimum_version_id
= 1,
265 .pre_save
= kvmclock_pre_save
,
266 .fields
= (VMStateField
[]) {
267 VMSTATE_UINT64(clock
, KVMClockState
),
268 VMSTATE_END_OF_LIST()
270 .subsections
= (const VMStateDescription
* []) {
271 &kvmclock_reliable_get_clock
,
276 static Property kvmclock_properties
[] = {
277 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState
,
278 mach_use_reliable_get_clock
, true),
279 DEFINE_PROP_END_OF_LIST(),
282 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
284 DeviceClass
*dc
= DEVICE_CLASS(klass
);
286 dc
->realize
= kvmclock_realize
;
287 dc
->vmsd
= &kvmclock_vmsd
;
288 dc
->props
= kvmclock_properties
;
291 static const TypeInfo kvmclock_info
= {
292 .name
= TYPE_KVM_CLOCK
,
293 .parent
= TYPE_SYS_BUS_DEVICE
,
294 .instance_size
= sizeof(KVMClockState
),
295 .class_init
= kvmclock_class_init
,
298 /* Note: Must be called after VCPU initialization. */
299 void kvmclock_create(void)
301 X86CPU
*cpu
= X86_CPU(first_cpu
);
304 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
305 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
306 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
310 static void kvmclock_register_types(void)
312 type_register_static(&kvmclock_info
);
315 type_init(kvmclock_register_types
)