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
{
41 struct pvclock_vcpu_time_info
{
44 uint64_t tsc_timestamp
;
46 uint32_t tsc_to_system_mul
;
50 } __attribute__((__packed__
)); /* 32 bytes */
52 static uint64_t kvmclock_current_nsec(KVMClockState
*s
)
54 CPUState
*cpu
= first_cpu
;
55 CPUX86State
*env
= cpu
->env_ptr
;
56 hwaddr kvmclock_struct_pa
= env
->system_time_msr
& ~1ULL;
57 uint64_t migration_tsc
= env
->tsc
;
58 struct pvclock_vcpu_time_info time
;
64 if (!(env
->system_time_msr
& 1ULL)) {
65 /* KVM clock not active */
69 cpu_physical_memory_read(kvmclock_struct_pa
, &time
, sizeof(time
));
71 assert(time
.tsc_timestamp
<= migration_tsc
);
72 delta
= migration_tsc
- time
.tsc_timestamp
;
73 if (time
.tsc_shift
< 0) {
74 delta
>>= -time
.tsc_shift
;
76 delta
<<= time
.tsc_shift
;
79 mulu64(&nsec_lo
, &nsec_hi
, delta
, time
.tsc_to_system_mul
);
80 nsec
= (nsec_lo
>> 32) | (nsec_hi
<< 32);
81 return nsec
+ time
.system_time
;
84 static void kvmclock_vm_state_change(void *opaque
, int running
,
87 KVMClockState
*s
= opaque
;
89 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
93 struct kvm_clock_data data
= {};
94 uint64_t time_at_migration
= kvmclock_current_nsec(s
);
96 s
->clock_valid
= false;
98 /* We can't rely on the migrated clock value, just discard it */
99 if (time_at_migration
) {
100 s
->clock
= time_at_migration
;
103 data
.clock
= s
->clock
;
104 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
106 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
110 if (!cap_clock_ctrl
) {
114 ret
= kvm_vcpu_ioctl(cpu
, KVM_KVMCLOCK_CTRL
, 0);
116 if (ret
!= -EINVAL
) {
117 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
123 struct kvm_clock_data data
;
126 if (s
->clock_valid
) {
130 kvm_synchronize_all_tsc();
132 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
134 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
137 s
->clock
= data
.clock
;
140 * If the VM is stopped, declare the clock state valid to
141 * avoid re-reading it on next vmsave (which would return
142 * a different value). Will be reset when the VM is continued.
144 s
->clock_valid
= true;
148 static void kvmclock_realize(DeviceState
*dev
, Error
**errp
)
150 KVMClockState
*s
= KVM_CLOCK(dev
);
152 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
155 static const VMStateDescription kvmclock_vmsd
= {
158 .minimum_version_id
= 1,
159 .fields
= (VMStateField
[]) {
160 VMSTATE_UINT64(clock
, KVMClockState
),
161 VMSTATE_END_OF_LIST()
165 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
167 DeviceClass
*dc
= DEVICE_CLASS(klass
);
169 dc
->realize
= kvmclock_realize
;
170 dc
->vmsd
= &kvmclock_vmsd
;
173 static const TypeInfo kvmclock_info
= {
174 .name
= TYPE_KVM_CLOCK
,
175 .parent
= TYPE_SYS_BUS_DEVICE
,
176 .instance_size
= sizeof(KVMClockState
),
177 .class_init
= kvmclock_class_init
,
180 /* Note: Must be called after VCPU initialization. */
181 void kvmclock_create(void)
183 X86CPU
*cpu
= X86_CPU(first_cpu
);
186 cpu
->env
.features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
187 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
188 sysbus_create_simple(TYPE_KVM_CLOCK
, -1, NULL
);
192 static void kvmclock_register_types(void)
194 type_register_static(&kvmclock_info
);
197 type_init(kvmclock_register_types
)