Merge remote-tracking branch 'mst/for_anthony' into staging
[qemu/wangdongxu.git] / hw / kvmclock.c
blob004c4add8c8aac7dd9b10d2ef9826f61cd8e5657
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 #if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ADJUST_CLOCK)
22 #include <linux/kvm.h>
23 #include <linux/kvm_para.h>
25 typedef struct KVMClockState {
26 SysBusDevice busdev;
27 uint64_t clock;
28 bool clock_valid;
29 } KVMClockState;
31 static void kvmclock_pre_save(void *opaque)
33 KVMClockState *s = opaque;
34 struct kvm_clock_data data;
35 int ret;
37 if (s->clock_valid) {
38 return;
40 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
41 if (ret < 0) {
42 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
43 data.clock = 0;
45 s->clock = data.clock;
47 * If the VM is stopped, declare the clock state valid to avoid re-reading
48 * it on next vmsave (which would return a different value). Will be reset
49 * when the VM is continued.
51 s->clock_valid = !vm_running;
54 static int kvmclock_post_load(void *opaque, int version_id)
56 KVMClockState *s = opaque;
57 struct kvm_clock_data data;
59 data.clock = s->clock;
60 data.flags = 0;
61 return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
64 static void kvmclock_vm_state_change(void *opaque, int running, int reason)
66 KVMClockState *s = opaque;
68 if (running) {
69 s->clock_valid = false;
73 static int kvmclock_init(SysBusDevice *dev)
75 KVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
77 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
78 return 0;
81 static const VMStateDescription kvmclock_vmsd = {
82 .name = "kvmclock",
83 .version_id = 1,
84 .minimum_version_id = 1,
85 .minimum_version_id_old = 1,
86 .pre_save = kvmclock_pre_save,
87 .post_load = kvmclock_post_load,
88 .fields = (VMStateField[]) {
89 VMSTATE_UINT64(clock, KVMClockState),
90 VMSTATE_END_OF_LIST()
94 static SysBusDeviceInfo kvmclock_info = {
95 .qdev.name = "kvmclock",
96 .qdev.size = sizeof(KVMClockState),
97 .qdev.vmsd = &kvmclock_vmsd,
98 .qdev.no_user = 1,
99 .init = kvmclock_init,
102 /* Note: Must be called after VCPU initialization. */
103 void kvmclock_create(void)
105 if (kvm_enabled() &&
106 first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE)
107 #ifdef KVM_FEATURE_CLOCKSOURCE2
108 || (1ULL << KVM_FEATURE_CLOCKSOURCE2)
109 #endif
110 )) {
111 sysbus_create_simple("kvmclock", -1, NULL);
115 static void kvmclock_register_device(void)
117 if (kvm_enabled()) {
118 sysbus_register_withprop(&kvmclock_info);
122 device_init(kvmclock_register_device);
124 #else /* !(CONFIG_KVM_PARA && KVM_CAP_ADJUST_CLOCK) */
126 void kvmclock_create(void)
129 #endif /* !(CONFIG_KVM_PARA && KVM_CAP_ADJUST_CLOCK) */