2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
14 #include "qemu-common.h"
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 typedef struct KVMClockState {
29 static void kvmclock_pre_save(void *opaque)
31 KVMClockState *s = opaque;
32 struct kvm_clock_data data;
38 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
40 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
43 s->clock = data.clock;
45 * If the VM is stopped, declare the clock state valid to avoid re-reading
46 * it on next vmsave (which would return a different value). Will be reset
47 * when the VM is continued.
49 s->clock_valid = !runstate_is_running();
52 static int kvmclock_post_load(void *opaque, int version_id)
54 KVMClockState *s = opaque;
55 struct kvm_clock_data data;
57 data.clock = s->clock;
59 return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
62 static void kvmclock_vm_state_change(void *opaque, int running,
65 KVMClockState *s = opaque;
68 s->clock_valid = false;
72 static int kvmclock_init(SysBusDevice *dev)
74 KVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
76 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
80 static const VMStateDescription kvmclock_vmsd = {
83 .minimum_version_id = 1,
84 .minimum_version_id_old = 1,
85 .pre_save = kvmclock_pre_save,
86 .post_load = kvmclock_post_load,
87 .fields = (VMStateField[]) {
88 VMSTATE_UINT64(clock, KVMClockState),
93 static SysBusDeviceInfo kvmclock_info = {
94 .qdev.name = "kvmclock",
95 .qdev.size = sizeof(KVMClockState),
96 .qdev.vmsd = &kvmclock_vmsd,
98 .init = kvmclock_init,
101 /* Note: Must be called after VCPU initialization. */
102 void kvmclock_create(void)
105 first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
106 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
107 sysbus_create_simple("kvmclock", -1, NULL);
111 static void kvmclock_register_device(void)
114 sysbus_register_withprop(&kvmclock_info);
118 device_init(kvmclock_register_device);