clock.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
14 15 16
 */

#include "qemu-common.h"
17
#include "qemu/host-utils.h"
18 19
#include "sysemu/sysemu.h"
#include "sysemu/kvm.h"
20
#include "sysemu/cpus.h"
21 22
#include "hw/sysbus.h"
#include "hw/kvm/clock.h"
23 24 25 26

#include <linux/kvm.h>
#include <linux/kvm_para.h>

H
Hu Tao 已提交
27 28 29
#define TYPE_KVM_CLOCK "kvmclock"
#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)

30
typedef struct KVMClockState {
H
Hu Tao 已提交
31
    /*< private >*/
32
    SysBusDevice busdev;
H
Hu Tao 已提交
33 34
    /*< public >*/

35 36 37 38
    uint64_t clock;
    bool clock_valid;
} KVMClockState;

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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));

69
    assert(time.tsc_timestamp <= migration_tsc);
70 71 72 73 74 75 76 77 78 79 80
    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;
}
81

82 83
static void kvmclock_vm_state_change(void *opaque, int running,
                                     RunState state)
84 85
{
    KVMClockState *s = opaque;
S
Stefan Weil 已提交
86
    CPUState *cpu;
87 88
    int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
    int ret;
89 90

    if (running) {
91
        struct kvm_clock_data data;
92
        uint64_t time_at_migration = kvmclock_current_nsec(s);
93

94
        s->clock_valid = false;
95

96 97 98 99 100
	/* We can't rely on the migrated clock value, just discard it */
	if (time_at_migration) {
	        s->clock = time_at_migration;
	}

101 102 103 104 105 106 107 108
        data.clock = s->clock;
        data.flags = 0;
        ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
        if (ret < 0) {
            fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
            abort();
        }

109 110 111
        if (!cap_clock_ctrl) {
            return;
        }
A
Andreas Färber 已提交
112
        CPU_FOREACH(cpu) {
113
            ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
114 115 116 117 118 119 120
            if (ret) {
                if (ret != -EINVAL) {
                    fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
                }
                return;
            }
        }
121 122 123 124 125 126 127
    } else {
        struct kvm_clock_data data;
        int ret;

        if (s->clock_valid) {
            return;
        }
128 129

        cpu_synchronize_all_states();
130 131 132 133 134 135 136 137 138 139 140 141 142
        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;
143 144 145
    }
}

H
Hu Tao 已提交
146
static void kvmclock_realize(DeviceState *dev, Error **errp)
147
{
H
Hu Tao 已提交
148
    KVMClockState *s = KVM_CLOCK(dev);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    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,
    .minimum_version_id_old = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(clock, KVMClockState),
        VMSTATE_END_OF_LIST()
    }
};

164 165
static void kvmclock_class_init(ObjectClass *klass, void *data)
{
166
    DeviceClass *dc = DEVICE_CLASS(klass);
167

H
Hu Tao 已提交
168
    dc->realize = kvmclock_realize;
169
    dc->vmsd = &kvmclock_vmsd;
170 171
}

172
static const TypeInfo kvmclock_info = {
H
Hu Tao 已提交
173
    .name          = TYPE_KVM_CLOCK,
174 175 176
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(KVMClockState),
    .class_init    = kvmclock_class_init,
177 178 179 180 181
};

/* Note: Must be called after VCPU initialization. */
void kvmclock_create(void)
{
182 183
    X86CPU *cpu = X86_CPU(first_cpu);

184
    if (kvm_enabled() &&
185 186
        cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
                                       (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
H
Hu Tao 已提交
187
        sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
188 189 190
    }
}

A
Andreas Färber 已提交
191
static void kvmclock_register_types(void)
192
{
193
    type_register_static(&kvmclock_info);
194 195
}

A
Andreas Färber 已提交
196
type_init(kvmclock_register_types)