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"
|
2014-09-05 17:52:45 +04:00
|
|
|
#include "qemu/host-utils.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
|
|
|
#include "sysemu/kvm.h"
|
2014-09-05 17:52:47 +04:00
|
|
|
#include "sysemu/cpus.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>
|
|
|
|
|
2013-07-01 14:18:38 +04:00
|
|
|
#define TYPE_KVM_CLOCK "kvmclock"
|
|
|
|
#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
|
|
|
|
|
2011-02-07 14:19:26 +03:00
|
|
|
typedef struct KVMClockState {
|
2013-07-01 14:18:38 +04:00
|
|
|
/*< private >*/
|
2011-02-07 14:19:26 +03:00
|
|
|
SysBusDevice busdev;
|
2013-07-01 14:18:38 +04:00
|
|
|
/*< public >*/
|
|
|
|
|
2011-02-07 14:19:26 +03:00
|
|
|
uint64_t clock;
|
|
|
|
bool clock_valid;
|
|
|
|
} KVMClockState;
|
|
|
|
|
2014-09-05 17:52:45 +04:00
|
|
|
struct pvclock_vcpu_time_info {
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t pad0;
|
|
|
|
uint64_t tsc_timestamp;
|
|
|
|
uint64_t system_time;
|
|
|
|
uint32_t tsc_to_system_mul;
|
|
|
|
int8_t tsc_shift;
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t pad[2];
|
|
|
|
} __attribute__((__packed__)); /* 32 bytes */
|
|
|
|
|
|
|
|
static uint64_t kvmclock_current_nsec(KVMClockState *s)
|
|
|
|
{
|
|
|
|
CPUState *cpu = first_cpu;
|
|
|
|
CPUX86State *env = cpu->env_ptr;
|
|
|
|
hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
|
|
|
|
uint64_t migration_tsc = env->tsc;
|
|
|
|
struct pvclock_vcpu_time_info time;
|
|
|
|
uint64_t delta;
|
|
|
|
uint64_t nsec_lo;
|
|
|
|
uint64_t nsec_hi;
|
|
|
|
uint64_t nsec;
|
|
|
|
|
|
|
|
if (!(env->system_time_msr & 1ULL)) {
|
|
|
|
/* KVM clock not active */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
|
|
|
|
|
|
|
|
assert(time.tsc_timestamp <= migration_tsc);
|
|
|
|
delta = migration_tsc - time.tsc_timestamp;
|
|
|
|
if (time.tsc_shift < 0) {
|
|
|
|
delta >>= -time.tsc_shift;
|
|
|
|
} else {
|
|
|
|
delta <<= time.tsc_shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
|
|
|
|
nsec = (nsec_lo >> 32) | (nsec_hi << 32);
|
|
|
|
return nsec + time.system_time;
|
|
|
|
}
|
2011-02-07 14:19:26 +03:00
|
|
|
|
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;
|
2013-09-18 00:39:55 +04:00
|
|
|
CPUState *cpu;
|
2012-04-07 04:47:47 +04:00
|
|
|
int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
|
|
|
|
int ret;
|
2011-02-07 14:19:26 +03:00
|
|
|
|
|
|
|
if (running) {
|
2014-10-14 13:55:49 +04:00
|
|
|
struct kvm_clock_data data = {};
|
2014-09-05 17:52:45 +04:00
|
|
|
uint64_t time_at_migration = kvmclock_current_nsec(s);
|
2013-06-19 03:38:25 +04:00
|
|
|
|
2011-02-07 14:19:26 +03:00
|
|
|
s->clock_valid = false;
|
2012-04-07 04:47:47 +04:00
|
|
|
|
2014-09-05 17:52:45 +04:00
|
|
|
/* We can't rely on the migrated clock value, just discard it */
|
|
|
|
if (time_at_migration) {
|
|
|
|
s->clock = time_at_migration;
|
|
|
|
}
|
|
|
|
|
2013-06-19 03:38:25 +04:00
|
|
|
data.clock = s->clock;
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2012-04-07 04:47:47 +04:00
|
|
|
if (!cap_clock_ctrl) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-25 01:50:24 +04:00
|
|
|
CPU_FOREACH(cpu) {
|
2013-05-30 00:29:20 +04:00
|
|
|
ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
|
2012-04-07 04:47:47 +04:00
|
|
|
if (ret) {
|
|
|
|
if (ret != -EINVAL) {
|
|
|
|
fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 03:38:25 +04:00
|
|
|
} else {
|
|
|
|
struct kvm_clock_data data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (s->clock_valid) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-05 17:52:47 +04:00
|
|
|
|
|
|
|
cpu_synchronize_all_states();
|
2014-11-03 20:45:34 +03:00
|
|
|
/* In theory, the cpu_synchronize_all_states() call above wouldn't
|
|
|
|
* affect the rest of the code, as the VCPU state inside CPUState
|
|
|
|
* is supposed to always match the VCPU state on the kernel side.
|
|
|
|
*
|
|
|
|
* In practice, calling cpu_synchronize_state() too soon will load the
|
|
|
|
* kernel-side APIC state into X86CPU.apic_state too early, APIC state
|
|
|
|
* won't be reloaded later because CPUState.vcpu_dirty==true, and
|
|
|
|
* outdated APIC state may be migrated to another host.
|
|
|
|
*
|
|
|
|
* The real fix would be to make sure outdated APIC state is read
|
|
|
|
* from the kernel again when necessary. While this is not fixed, we
|
|
|
|
* need the cpu_clean_all_dirty() call below.
|
|
|
|
*/
|
2014-09-05 17:52:47 +04:00
|
|
|
cpu_clean_all_dirty();
|
2014-11-03 20:45:34 +03:00
|
|
|
|
2013-06-19 03:38:25 +04:00
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
s->clock_valid = true;
|
2011-02-07 14:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:18:39 +04:00
|
|
|
static void kvmclock_realize(DeviceState *dev, Error **errp)
|
2011-02-07 14:19:26 +03:00
|
|
|
{
|
2013-07-01 14:18:38 +04:00
|
|
|
KVMClockState *s = KVM_CLOCK(dev);
|
2011-02-07 14:19:26 +03:00
|
|
|
|
|
|
|
qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription kvmclock_vmsd = {
|
|
|
|
.name = "kvmclock",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.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
|
|
|
|
2013-07-01 14:18:39 +04:00
|
|
|
dc->realize = kvmclock_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &kvmclock_vmsd;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo kvmclock_info = {
|
2013-07-01 14:18:38 +04:00
|
|
|
.name = TYPE_KVM_CLOCK,
|
2011-12-08 07:34:16 +04:00
|
|
|
.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)
|
|
|
|
{
|
2013-05-30 00:29:20 +04:00
|
|
|
X86CPU *cpu = X86_CPU(first_cpu);
|
|
|
|
|
2011-02-07 14:19:26 +03:00
|
|
|
if (kvm_enabled() &&
|
2013-05-30 00:29:20 +04:00
|
|
|
cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
|
|
|
|
(1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
|
2013-07-01 14:18:38 +04:00
|
|
|
sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
|
2011-02-07 14:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void kvmclock_register_types(void)
|
2011-02-07 14:19:26 +03:00
|
|
|
{
|
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)
|