2011-02-07 14:19:26 +03:00
|
|
|
/*
|
|
|
|
* QEMU KVM support, paravirtual clock device
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Siemens AG
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Jan Kiszka <jan.kiszka@siemens.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL version 2.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-13 20:44:23 +04:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2011-02-07 14:19:26 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "sysemu.h"
|
|
|
|
#include "kvm.h"
|
2011-10-15 12:01:27 +04:00
|
|
|
#include "hw/sysbus.h"
|
|
|
|
#include "hw/kvm/clock.h"
|
2011-02-07 14:19:26 +03:00
|
|
|
|
|
|
|
#include <linux/kvm.h>
|
|
|
|
#include <linux/kvm_para.h>
|
|
|
|
|
|
|
|
typedef struct KVMClockState {
|
|
|
|
SysBusDevice busdev;
|
|
|
|
uint64_t clock;
|
|
|
|
bool clock_valid;
|
|
|
|
} KVMClockState;
|
|
|
|
|
|
|
|
static void kvmclock_pre_save(void *opaque)
|
|
|
|
{
|
|
|
|
KVMClockState *s = opaque;
|
|
|
|
struct kvm_clock_data data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (s->clock_valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
|
|
|
|
data.clock = 0;
|
|
|
|
}
|
|
|
|
s->clock = data.clock;
|
|
|
|
/*
|
|
|
|
* If the VM is stopped, declare the clock state valid to avoid re-reading
|
|
|
|
* it on next vmsave (which would return a different value). Will be reset
|
|
|
|
* when the VM is continued.
|
|
|
|
*/
|
2011-07-29 22:36:43 +04:00
|
|
|
s->clock_valid = !runstate_is_running();
|
2011-02-07 14:19:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvmclock_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
KVMClockState *s = opaque;
|
|
|
|
struct kvm_clock_data data;
|
|
|
|
|
|
|
|
data.clock = s->clock;
|
|
|
|
data.flags = 0;
|
|
|
|
return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
|
|
|
|
}
|
|
|
|
|
2011-07-29 21:26:33 +04:00
|
|
|
static void kvmclock_vm_state_change(void *opaque, int running,
|
|
|
|
RunState state)
|
2011-02-07 14:19:26 +03:00
|
|
|
{
|
|
|
|
KVMClockState *s = opaque;
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
s->clock_valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kvmclock_init(SysBusDevice *dev)
|
|
|
|
{
|
|
|
|
KVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
|
|
|
|
|
|
|
|
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription kvmclock_vmsd = {
|
|
|
|
.name = "kvmclock",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.pre_save = kvmclock_pre_save,
|
|
|
|
.post_load = kvmclock_post_load,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT64(clock, KVMClockState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void kvmclock_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = kvmclock_init;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->no_user = 1;
|
|
|
|
dc->vmsd = &kvmclock_vmsd;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static TypeInfo kvmclock_info = {
|
|
|
|
.name = "kvmclock",
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(KVMClockState),
|
|
|
|
.class_init = kvmclock_class_init,
|
2011-02-07 14:19:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Note: Must be called after VCPU initialization. */
|
|
|
|
void kvmclock_create(void)
|
|
|
|
{
|
|
|
|
if (kvm_enabled() &&
|
2011-06-23 12:23:10 +04:00
|
|
|
first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
|
|
|
|
(1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
|
2011-02-07 14:19:26 +03:00
|
|
|
sysbus_create_simple("kvmclock", -1, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void kvmclock_register_types(void)
|
2011-02-07 14:19:26 +03:00
|
|
|
{
|
|
|
|
if (kvm_enabled()) {
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&kvmclock_info);
|
2011-02-07 14:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(kvmclock_register_types)
|