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"
18 #include "qemu/host-utils.h"
19 #include "sysemu/sysemu.h"
20 #include "sysemu/kvm.h"
22 #include "hw/sysbus.h"
23 #include "hw/kvm/clock.h"
25 #include <linux/kvm.h>
26 #include <linux/kvm_para.h>
28 #define TYPE_KVM_CLOCK "kvmclock"
29 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
31 typedef struct KVMClockState {
40 struct pvclock_vcpu_time_info {
43 uint64_t tsc_timestamp;
45 uint32_t tsc_to_system_mul;
49 } __attribute__((__packed__)); /* 32 bytes */
51 static uint64_t kvmclock_current_nsec(KVMClockState *s)
53 CPUState *cpu = first_cpu;
54 CPUX86State *env = cpu->env_ptr;
55 hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
56 uint64_t migration_tsc = env->tsc;
57 struct pvclock_vcpu_time_info time;
63 if (!(env->system_time_msr & 1ULL)) {
64 /* KVM clock not active */
68 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
70 assert(time.tsc_timestamp <= migration_tsc);
71 delta = migration_tsc - time.tsc_timestamp;
72 if (time.tsc_shift < 0) {
73 delta >>= -time.tsc_shift;
75 delta <<= time.tsc_shift;
78 mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
79 nsec = (nsec_lo >> 32) | (nsec_hi << 32);
80 return nsec + time.system_time;
83 static void kvmclock_vm_state_change(void *opaque, int running,
86 KVMClockState *s = opaque;
88 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
92 struct kvm_clock_data data = {};
93 uint64_t time_at_migration = kvmclock_current_nsec(s);
95 s->clock_valid = false;
97 /* We can't rely on the migrated clock value, just discard it */
98 if (time_at_migration) {
99 s->clock = time_at_migration;
102 data.clock = s->clock;
103 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
105 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
109 if (!cap_clock_ctrl) {
113 ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
115 if (ret != -EINVAL) {
116 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
122 struct kvm_clock_data data;
125 if (s->clock_valid) {
129 kvm_synchronize_all_tsc();
131 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
133 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
136 s->clock = data.clock;
139 * If the VM is stopped, declare the clock state valid to
140 * avoid re-reading it on next vmsave (which would return
141 * a different value). Will be reset when the VM is continued.
143 s->clock_valid = true;
147 static void kvmclock_realize(DeviceState *dev, Error **errp)
149 KVMClockState *s = KVM_CLOCK(dev);
151 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
154 static const VMStateDescription kvmclock_vmsd = {
157 .minimum_version_id = 1,
158 .fields = (VMStateField[]) {
159 VMSTATE_UINT64(clock, KVMClockState),
160 VMSTATE_END_OF_LIST()
164 static void kvmclock_class_init(ObjectClass *klass, void *data)
166 DeviceClass *dc = DEVICE_CLASS(klass);
168 dc->realize = kvmclock_realize;
169 dc->vmsd = &kvmclock_vmsd;
172 static const TypeInfo kvmclock_info = {
173 .name = TYPE_KVM_CLOCK,
174 .parent = TYPE_SYS_BUS_DEVICE,
175 .instance_size = sizeof(KVMClockState),
176 .class_init = kvmclock_class_init,
179 /* Note: Must be called after VCPU initialization. */
180 void kvmclock_create(void)
182 X86CPU *cpu = X86_CPU(first_cpu);
185 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
186 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
187 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
191 static void kvmclock_register_types(void)
193 type_register_static(&kvmclock_info);
196 type_init(kvmclock_register_types)