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-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 typedef struct KVMClockState
{
32 static void kvmclock_vm_state_change(void *opaque
, int running
,
35 KVMClockState
*s
= opaque
;
36 CPUArchState
*penv
= first_cpu
;
37 int cap_clock_ctrl
= kvm_check_extension(kvm_state
, KVM_CAP_KVMCLOCK_CTRL
);
41 struct kvm_clock_data data
;
43 s
->clock_valid
= false;
45 data
.clock
= s
->clock
;
47 ret
= kvm_vm_ioctl(kvm_state
, KVM_SET_CLOCK
, &data
);
49 fprintf(stderr
, "KVM_SET_CLOCK failed: %s\n", strerror(ret
));
53 if (!cap_clock_ctrl
) {
56 for (penv
= first_cpu
; penv
!= NULL
; penv
= penv
->next_cpu
) {
57 ret
= kvm_vcpu_ioctl(ENV_GET_CPU(penv
), KVM_KVMCLOCK_CTRL
, 0);
60 fprintf(stderr
, "%s: %s\n", __func__
, strerror(-ret
));
66 struct kvm_clock_data data
;
72 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_CLOCK
, &data
);
74 fprintf(stderr
, "KVM_GET_CLOCK failed: %s\n", strerror(ret
));
77 s
->clock
= data
.clock
;
80 * If the VM is stopped, declare the clock state valid to
81 * avoid re-reading it on next vmsave (which would return
82 * a different value). Will be reset when the VM is continued.
84 s
->clock_valid
= true;
88 static int kvmclock_init(SysBusDevice
*dev
)
90 KVMClockState
*s
= FROM_SYSBUS(KVMClockState
, dev
);
92 qemu_add_vm_change_state_handler(kvmclock_vm_state_change
, s
);
96 static const VMStateDescription kvmclock_vmsd
= {
99 .minimum_version_id
= 1,
100 .minimum_version_id_old
= 1,
101 .fields
= (VMStateField
[]) {
102 VMSTATE_UINT64(clock
, KVMClockState
),
103 VMSTATE_END_OF_LIST()
107 static void kvmclock_class_init(ObjectClass
*klass
, void *data
)
109 DeviceClass
*dc
= DEVICE_CLASS(klass
);
110 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
112 k
->init
= kvmclock_init
;
114 dc
->vmsd
= &kvmclock_vmsd
;
117 static const TypeInfo kvmclock_info
= {
119 .parent
= TYPE_SYS_BUS_DEVICE
,
120 .instance_size
= sizeof(KVMClockState
),
121 .class_init
= kvmclock_class_init
,
124 /* Note: Must be called after VCPU initialization. */
125 void kvmclock_create(void)
128 first_cpu
->features
[FEAT_KVM
] & ((1ULL << KVM_FEATURE_CLOCKSOURCE
) |
129 (1ULL << KVM_FEATURE_CLOCKSOURCE2
))) {
130 sysbus_create_simple("kvmclock", -1, NULL
);
134 static void kvmclock_register_types(void)
136 type_register_static(&kvmclock_info
);
139 type_init(kvmclock_register_types
)