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 the 'clock' value was obtained in the 'paused' state */
47 /* whether machine type supports reliable KVM_GET_CLOCK */
48 bool mach_use_reliable_get_clock
;
50 /* whether the 'clock' value was obtained in a host with
51 * reliable KVM_GET_CLOCK */
52 bool clock_is_reliable
;
55 struct pvclock_vcpu_time_info
{
58 uint64_t tsc_timestamp
;
60 uint32_t tsc_to_system_mul
;
64 } __attribute__((__packed__
)); /* 32 bytes */
66 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
68 CPUState
*cpu
= first_cpu
;
69 CPUX86State
*env
= cpu
->env_ptr
;
70 hwaddr kvmclock_struct_pa
;
71 uint64_t migration_tsc
= env
->tsc
;
72 struct pvclock_vcpu_time_info time
;
78 cpu_synchronize_state(cpu
);
80 if (!(env
->system_time_msr
& 1ULL)) {
81 /* KVM clock not active */
85 kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
86 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
88 assert(time
.tsc_timestamp
<= migration_tsc
);
89 delta
= migration_tsc
- time
.tsc_timestamp
;
90 if (time
.tsc_shift
< 0) {
91 delta
>>= -time
.tsc_shift
;
93 delta
<<= time
.tsc_shift
;
96 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
97 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
98 return nsec
+ time
.system_time
;
101 static void kvm_update_clock(KVMClockState
*s
)
103 struct kvm_clock_data data
;
106 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
108 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
111 s
->clock
= data
.clock
;
113 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
114 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
115 * can drift from the TSC-based value that is computed by the guest,
116 * so we need to go through kvmclock_current_nsec(). If
117 * kvm_has_adjust_clock_stable() is true, and the flags contain
118 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
119 * and kvmclock_current_nsec() is not necessary.
121 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
123 * - if the host has disabled the kvmclock master clock, the guest already
124 * has protection against time going backwards. This "safety net" is only
125 * absent when kvmclock is stable;
127 * - therefore, we can replace a check like
129 * if last KVM_GET_CLOCK was not reliable then
134 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
139 * - if kvm_has_adjust_clock_stable() returns false, the left side is
140 * always true (KVM_GET_CLOCK is never reliable), and the right side is
141 * unknown (because we don't have data.flags). We must assume it's true
142 * and read from memory.
144 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
145 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
147 * So we can just use this instead:
149 * if !kvm_has_adjust_clock_stable() then
152 s
->clock_is_reliable
= kvm_has_adjust_clock_stable();
155 static void do_kvmclock_ctrl(CPUState
*cpu
, run_on_cpu_data data
)
157 int ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
159 if (ret
&& ret
!= -EINVAL
) {
160 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
164 static void kvmclock_vm_state_change(void *opaque
, int running
,
167 KVMClockState
*s
= opaque
;
169 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
173 struct kvm_clock_data data
= {};
176 * If the host where s->clock was read did not support reliable
177 * KVM_GET_CLOCK, read kvmclock value from memory.
179 if (!s
->clock_is_reliable
) {
180 uint64_t pvclock_via_mem
= kvmclock_current_nsec(s
);
181 /* We can't rely on the saved clock value, just discard it */
182 if (pvclock_via_mem
) {
183 s
->clock
= pvclock_via_mem
;
187 s
->clock_valid
= false;
189 data
.clock
= s
->clock
;
190 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
192 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
196 if (!cap_clock_ctrl
) {
200 run_on_cpu(cpu
, do_kvmclock_ctrl
, RUN_ON_CPU_NULL
);
204 if (s
->clock_valid
) {
208 s
->runstate_paused
= runstate_check(RUN_STATE_PAUSED
);
210 kvm_synchronize_all_tsc();
214 * If the VM is stopped, declare the clock state valid to
215 * avoid re-reading it on next vmsave (which would return
216 * a different value). Will be reset when the VM is continued.
218 s
->clock_valid
= true;
222 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
224 KVMClockState
*s
= KVM_CLOCK(dev
);
226 if (!kvm_enabled()) {
227 error_setg(errp
, "kvmclock device requires KVM");
233 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
236 static bool kvmclock_clock_is_reliable_needed(void *opaque
)
238 KVMClockState
*s
= opaque
;
240 return s
->mach_use_reliable_get_clock
;
243 static const VMStateDescription kvmclock_reliable_get_clock
= {
244 .name
= "kvmclock/clock_is_reliable",
246 .minimum_version_id
= 1,
247 .needed
= kvmclock_clock_is_reliable_needed
,
248 .fields
= (VMStateField
[]) {
249 VMSTATE_BOOL(clock_is_reliable
, KVMClockState
),
250 VMSTATE_END_OF_LIST()
255 * When migrating, assume the source has an unreliable
256 * KVM_GET_CLOCK unless told otherwise.
258 static int kvmclock_pre_load(void *opaque
)
260 KVMClockState
*s
= opaque
;
262 s
->clock_is_reliable
= false;
268 * When migrating a running guest, read the clock just
269 * before migration, so that the guest clock counts
270 * during the events between:
276 * This reduces kvmclock difference on migration from 5s
277 * to 0.1s (when max_downtime == 5s), because sending the
278 * final pages of memory (which happens between vm_stop()
279 * and pre_save()) takes max_downtime.
281 static int kvmclock_pre_save(void *opaque
)
283 KVMClockState
*s
= opaque
;
285 if (!s
->runstate_paused
) {
292 static const VMStateDescription kvmclock_vmsd
= {
295 .minimum_version_id
= 1,
296 .pre_load
= kvmclock_pre_load
,
297 .pre_save
= kvmclock_pre_save
,
298 .fields
= (VMStateField
[]) {
299 VMSTATE_UINT64(clock
, KVMClockState
),
300 VMSTATE_END_OF_LIST()
302 .subsections
= (const VMStateDescription
* []) {
303 &kvmclock_reliable_get_clock
,
308 static Property kvmclock_properties
[] = {
309 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState
,
310 mach_use_reliable_get_clock
, true),
311 DEFINE_PROP_END_OF_LIST(),
314 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
316 DeviceClass
*dc
= DEVICE_CLASS(klass
);
318 dc
->realize
= kvmclock_realize
;
319 dc
->vmsd
= &kvmclock_vmsd
;
320 device_class_set_props(dc
, kvmclock_properties
);
323 static const TypeInfo kvmclock_info
= {
324 .name
= TYPE_KVM_CLOCK
,
325 .parent
= TYPE_SYS_BUS_DEVICE
,
326 .instance_size
= sizeof(KVMClockState
),
327 .class_init
= kvmclock_class_init
,
330 /* Note: Must be called after VCPU initialization. */
331 void kvmclock_create(void)
333 X86CPU
*cpu
= X86_CPU(first_cpu
);
336 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
337 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
338 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
342 static void kvmclock_register_types(void)
344 type_register_static(&kvmclock_info
);
347 type_init(kvmclock_register_types
)