modules: do not include gmodule-2.0 in static builds
[qemu/rayw.git] / hw / i386 / kvm / clock.c
blob892aa025f4f3df4c5e54e0c5734cc4e34c98cf8e
1 /*
2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
6 * Authors:
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-common.h"
17 #include "sysemu/sysemu.h"
18 #include "sysemu/kvm.h"
19 #include "hw/sysbus.h"
20 #include "hw/kvm/clock.h"
22 #include <linux/kvm.h>
23 #include <linux/kvm_para.h>
25 #define TYPE_KVM_CLOCK "kvmclock"
26 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
28 typedef struct KVMClockState {
29 /*< private >*/
30 SysBusDevice busdev;
31 /*< public >*/
33 uint64_t clock;
34 bool clock_valid;
35 } KVMClockState;
38 static void kvmclock_vm_state_change(void *opaque, int running,
39 RunState state)
41 KVMClockState *s = opaque;
42 CPUState *cpu;
43 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
44 int ret;
46 if (running) {
47 struct kvm_clock_data data;
49 s->clock_valid = false;
51 data.clock = s->clock;
52 data.flags = 0;
53 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
54 if (ret < 0) {
55 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
56 abort();
59 if (!cap_clock_ctrl) {
60 return;
62 CPU_FOREACH(cpu) {
63 ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
64 if (ret) {
65 if (ret != -EINVAL) {
66 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
68 return;
71 } else {
72 struct kvm_clock_data data;
73 int ret;
75 if (s->clock_valid) {
76 return;
78 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
79 if (ret < 0) {
80 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
81 abort();
83 s->clock = data.clock;
86 * If the VM is stopped, declare the clock state valid to
87 * avoid re-reading it on next vmsave (which would return
88 * a different value). Will be reset when the VM is continued.
90 s->clock_valid = true;
94 static void kvmclock_realize(DeviceState *dev, Error **errp)
96 KVMClockState *s = KVM_CLOCK(dev);
98 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
101 static const VMStateDescription kvmclock_vmsd = {
102 .name = "kvmclock",
103 .version_id = 1,
104 .minimum_version_id = 1,
105 .minimum_version_id_old = 1,
106 .fields = (VMStateField[]) {
107 VMSTATE_UINT64(clock, KVMClockState),
108 VMSTATE_END_OF_LIST()
112 static void kvmclock_class_init(ObjectClass *klass, void *data)
114 DeviceClass *dc = DEVICE_CLASS(klass);
116 dc->realize = kvmclock_realize;
117 dc->vmsd = &kvmclock_vmsd;
120 static const TypeInfo kvmclock_info = {
121 .name = TYPE_KVM_CLOCK,
122 .parent = TYPE_SYS_BUS_DEVICE,
123 .instance_size = sizeof(KVMClockState),
124 .class_init = kvmclock_class_init,
127 /* Note: Must be called after VCPU initialization. */
128 void kvmclock_create(void)
130 X86CPU *cpu = X86_CPU(first_cpu);
132 if (kvm_enabled() &&
133 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
134 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
135 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
139 static void kvmclock_register_types(void)
141 type_register_static(&kvmclock_info);
144 type_init(kvmclock_register_types)