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"
26 #include <linux/kvm.h>
27 #include <linux/kvm_para.h>
29 #define TYPE_KVM_CLOCK "kvmclock"
30 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
32 typedef struct KVMClockState
{
40 /* whether machine type supports reliable KVM_GET_CLOCK */
41 bool mach_use_reliable_get_clock
;
43 /* whether the 'clock' value was obtained in a host with
44 * reliable KVM_GET_CLOCK */
45 bool clock_is_reliable
;
48 struct pvclock_vcpu_time_info
{
51 uint64_t tsc_timestamp
;
53 uint32_t tsc_to_system_mul
;
57 } __attribute__((__packed__
)); /* 32 bytes */
59 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
61 CPUState
*cpu
= first_cpu
;
62 CPUX86State
*env
= cpu
->env_ptr
;
63 hwaddr kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
64 uint64_t migration_tsc
= env
->tsc
;
65 struct pvclock_vcpu_time_info time
;
71 if (!(env
->system_time_msr
& 1ULL)) {
72 /* KVM clock not active */
76 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
78 assert(time
.tsc_timestamp
<= migration_tsc
);
79 delta
= migration_tsc
- time
.tsc_timestamp
;
80 if (time
.tsc_shift
< 0) {
81 delta
>>= -time
.tsc_shift
;
83 delta
<<= time
.tsc_shift
;
86 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
87 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
88 return nsec
+ time
.system_time
;
91 static void kvm_update_clock(KVMClockState
*s
)
93 struct kvm_clock_data data
;
96 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
98 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
101 s
->clock
= data
.clock
;
103 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
104 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
105 * can drift from the TSC-based value that is computed by the guest,
106 * so we need to go through kvmclock_current_nsec(). If
107 * kvm_has_adjust_clock_stable() is true, and the flags contain
108 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
109 * and kvmclock_current_nsec() is not necessary.
111 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
113 * - if the host has disabled the kvmclock master clock, the guest already
114 * has protection against time going backwards. This "safety net" is only
115 * absent when kvmclock is stable;
117 * - therefore, we can replace a check like
119 * if last KVM_GET_CLOCK was not reliable then
124 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
129 * - if kvm_has_adjust_clock_stable() returns false, the left side is
130 * always true (KVM_GET_CLOCK is never reliable), and the right side is
131 * unknown (because we don't have data.flags). We must assume it's true
132 * and read from memory.
134 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
135 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
137 * So we can just use this instead:
139 * if !kvm_has_adjust_clock_stable() then
142 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
145 static void kvmclock_vm_state_change(void *opaque
, int running
,
148 KVMClockState
*s
= opaque
;
150 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
154 struct kvm_clock_data data
= {};
157 * If the host where s->clock was read did not support reliable
158 * KVM_GET_CLOCK, read kvmclock value from memory.
160 if (!s
->clock_is_reliable
) {
161 uint64_t pvclock_via_mem
= kvmclock_current_nsec(s
);
162 /* We can't rely on the saved clock value, just discard it */
163 if (pvclock_via_mem
) {
164 s
->clock
= pvclock_via_mem
;
168 s
->clock_valid
= false;
170 data
.clock
= s
->clock
;
171 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
173 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
177 if (!cap_clock_ctrl
) {
181 ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
183 if (ret
!= -EINVAL
) {
184 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
191 if (s
->clock_valid
) {
195 kvm_synchronize_all_tsc();
199 * If the VM is stopped, declare the clock state valid to
200 * avoid re-reading it on next vmsave (which would return
201 * a different value). Will be reset when the VM is continued.
203 s
->clock_valid
= true;
207 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
209 KVMClockState
*s
= KVM_CLOCK(dev
);
213 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
216 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
218 KVMClockState
*s
= opaque
;
220 return s
->mach_use_reliable_get_clock
;
223 static const VMStateDescription kvmclock_reliable_get_clock
= {
224 .name
= "kvmclock/clock_is_reliable",
226 .minimum_version_id
= 1,
227 .needed
= kvmclock_clock_is_reliable_needed
,
228 .fields
= (VMStateField
[]) {
229 VMSTATE_BOOL(clock_is_reliable
, KVMClockState
),
230 VMSTATE_END_OF_LIST()
235 * When migrating, read the clock just before migration,
236 * so that the guest clock counts during the events
243 * This reduces kvmclock difference on migration from 5s
244 * to 0.1s (when max_downtime == 5s), because sending the
245 * final pages of memory (which happens between vm_stop()
246 * and pre_save()) takes max_downtime.
248 static void kvmclock_pre_save(void *opaque
)
250 KVMClockState
*s
= opaque
;
255 static const VMStateDescription kvmclock_vmsd
= {
258 .minimum_version_id
= 1,
259 .pre_save
= kvmclock_pre_save
,
260 .fields
= (VMStateField
[]) {
261 VMSTATE_UINT64(clock
, KVMClockState
),
262 VMSTATE_END_OF_LIST()
264 .subsections
= (const VMStateDescription
* []) {
265 &kvmclock_reliable_get_clock
,
270 static Property kvmclock_properties
[] = {
271 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState
,
272 mach_use_reliable_get_clock
, true),
273 DEFINE_PROP_END_OF_LIST(),
276 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
278 DeviceClass
*dc
= DEVICE_CLASS(klass
);
280 dc
->realize
= kvmclock_realize
;
281 dc
->vmsd
= &kvmclock_vmsd
;
282 dc
->props
= kvmclock_properties
;
285 static const TypeInfo kvmclock_info
= {
286 .name
= TYPE_KVM_CLOCK
,
287 .parent
= TYPE_SYS_BUS_DEVICE
,
288 .instance_size
= sizeof(KVMClockState
),
289 .class_init
= kvmclock_class_init
,
292 /* Note: Must be called after VCPU initialization. */
293 void kvmclock_create(void)
295 X86CPU
*cpu
= X86_CPU(first_cpu
);
298 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
299 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
300 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
304 static void kvmclock_register_types(void)
306 type_register_static(&kvmclock_info
);
309 type_init(kvmclock_register_types
)