main: switch qemu_set_fd_handler to g_io_add_watch
[qemu.git] / hw / kvmclock.c
blobb73aec409c53e9a7c0d15c2e37560f6c6d28307a
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.
14 #include "qemu-common.h"
15 #include "sysemu.h"
16 #include "sysbus.h"
17 #include "kvm.h"
18 #include "kvmclock.h"
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 typedef struct KVMClockState {
24 SysBusDevice busdev;
25 uint64_t clock;
26 bool clock_valid;
27 } KVMClockState;
29 static void kvmclock_pre_save(void *opaque)
31 KVMClockState *s = opaque;
32 struct kvm_clock_data data;
33 int ret;
35 if (s->clock_valid) {
36 return;
38 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
39 if (ret < 0) {
40 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
41 data.clock = 0;
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 = !vm_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;
58 data.flags = 0;
59 return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
62 static void kvmclock_vm_state_change(void *opaque, int running, int reason)
64 KVMClockState *s = opaque;
66 if (running) {
67 s->clock_valid = false;
71 static int kvmclock_init(SysBusDevice *dev)
73 KVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
75 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
76 return 0;
79 static const VMStateDescription kvmclock_vmsd = {
80 .name = "kvmclock",
81 .version_id = 1,
82 .minimum_version_id = 1,
83 .minimum_version_id_old = 1,
84 .pre_save = kvmclock_pre_save,
85 .post_load = kvmclock_post_load,
86 .fields = (VMStateField[]) {
87 VMSTATE_UINT64(clock, KVMClockState),
88 VMSTATE_END_OF_LIST()
92 static SysBusDeviceInfo kvmclock_info = {
93 .qdev.name = "kvmclock",
94 .qdev.size = sizeof(KVMClockState),
95 .qdev.vmsd = &kvmclock_vmsd,
96 .qdev.no_user = 1,
97 .init = kvmclock_init,
100 /* Note: Must be called after VCPU initialization. */
101 void kvmclock_create(void)
103 if (kvm_enabled() &&
104 first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
105 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
106 sysbus_create_simple("kvmclock", -1, NULL);
110 static void kvmclock_register_device(void)
112 if (kvm_enabled()) {
113 sysbus_register_withprop(&kvmclock_info);
117 device_init(kvmclock_register_device);