clock.c 8.8 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
 */

P
Peter Maydell 已提交
16
#include "qemu/osdep.h"
17
#include "qemu-common.h"
18
#include "cpu.h"
19
#include "qemu/host-utils.h"
20 21
#include "sysemu/sysemu.h"
#include "sysemu/kvm.h"
22
#include "sysemu/hw_accel.h"
23
#include "kvm_i386.h"
24 25
#include "hw/sysbus.h"
#include "hw/kvm/clock.h"
26
#include "qapi/error.h"
27 28 29 30

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

H
Hu Tao 已提交
31 32 33
#define TYPE_KVM_CLOCK "kvmclock"
#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)

34
typedef struct KVMClockState {
H
Hu Tao 已提交
35
    /*< private >*/
36
    SysBusDevice busdev;
H
Hu Tao 已提交
37 38
    /*< public >*/

39 40
    uint64_t clock;
    bool clock_valid;
41 42 43 44 45 46 47

    /* whether machine type supports reliable KVM_GET_CLOCK */
    bool mach_use_reliable_get_clock;

    /* whether the 'clock' value was obtained in a host with
     * reliable KVM_GET_CLOCK */
    bool clock_is_reliable;
48 49
} KVMClockState;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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;
65
    hwaddr kvmclock_struct_pa;
66 67 68 69 70 71 72
    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;

73 74
    cpu_synchronize_state(cpu);

75 76 77 78 79
    if (!(env->system_time_msr & 1ULL)) {
        /* KVM clock not active */
        return 0;
    }

80
    kvmclock_struct_pa = env->system_time_msr & ~1ULL;
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    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;
}
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static void kvm_update_clock(KVMClockState *s)
{
    struct kvm_clock_data data;
    int ret;

    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 kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
     * essentially CLOCK_MONOTONIC plus a guest-specific adjustment.  This
     * can drift from the TSC-based value that is computed by the guest,
     * so we need to go through kvmclock_current_nsec().  If
     * kvm_has_adjust_clock_stable() is true, and the flags contain
     * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
     * and kvmclock_current_nsec() is not necessary.
     *
     * Here, however, we need not check KVM_CLOCK_TSC_STABLE.  This is because:
     *
     * - if the host has disabled the kvmclock master clock, the guest already
     *   has protection against time going backwards.  This "safety net" is only
     *   absent when kvmclock is stable;
     *
     * - therefore, we can replace a check like
     *
     *       if last KVM_GET_CLOCK was not reliable then
     *               read from memory
     *
     *   with
     *
     *       if last KVM_GET_CLOCK was not reliable && masterclock is enabled
     *               read from memory
     *
     * However:
     *
     * - if kvm_has_adjust_clock_stable() returns false, the left side is
     *   always true (KVM_GET_CLOCK is never reliable), and the right side is
     *   unknown (because we don't have data.flags).  We must assume it's true
     *   and read from memory.
     *
     * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
     *   is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
     *
     * So we can just use this instead:
     *
     *       if !kvm_has_adjust_clock_stable() then
     *               read from memory
     */
    s->clock_is_reliable = kvm_has_adjust_clock_stable();
}

150 151
static void kvmclock_vm_state_change(void *opaque, int running,
                                     RunState state)
152 153
{
    KVMClockState *s = opaque;
S
Stefan Weil 已提交
154
    CPUState *cpu;
155 156
    int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
    int ret;
157 158

    if (running) {
159
        struct kvm_clock_data data = {};
160

161 162 163 164 165 166 167 168 169 170
        /*
         * If the host where s->clock was read did not support reliable
         * KVM_GET_CLOCK, read kvmclock value from memory.
         */
        if (!s->clock_is_reliable) {
            uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
            /* We can't rely on the saved clock value, just discard it */
            if (pvclock_via_mem) {
                s->clock = pvclock_via_mem;
            }
171 172
        }

173 174
        s->clock_valid = false;

175 176 177 178 179 180 181
        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();
        }

182 183 184
        if (!cap_clock_ctrl) {
            return;
        }
A
Andreas Färber 已提交
185
        CPU_FOREACH(cpu) {
186
            ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
187 188 189 190 191 192 193
            if (ret) {
                if (ret != -EINVAL) {
                    fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
                }
                return;
            }
        }
194 195 196 197 198
    } else {

        if (s->clock_valid) {
            return;
        }
199

200
        kvm_synchronize_all_tsc();
201

202
        kvm_update_clock(s);
203 204 205 206 207 208
        /*
         * 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;
209 210 211
    }
}

H
Hu Tao 已提交
212
static void kvmclock_realize(DeviceState *dev, Error **errp)
213
{
H
Hu Tao 已提交
214
    KVMClockState *s = KVM_CLOCK(dev);
215

216 217 218 219 220
    if (!kvm_enabled()) {
        error_setg(errp, "kvmclock device requires KVM");
        return;
    }

221 222
    kvm_update_clock(s);

223 224 225
    qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static bool kvmclock_clock_is_reliable_needed(void *opaque)
{
    KVMClockState *s = opaque;

    return s->mach_use_reliable_get_clock;
}

static const VMStateDescription kvmclock_reliable_get_clock = {
    .name = "kvmclock/clock_is_reliable",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = kvmclock_clock_is_reliable_needed,
    .fields = (VMStateField[]) {
        VMSTATE_BOOL(clock_is_reliable, KVMClockState),
        VMSTATE_END_OF_LIST()
    }
};

/*
 * When migrating, read the clock just before migration,
 * so that the guest clock counts during the events
 * between:
 *
 *  * vm_stop()
 *  *
 *  * pre_save()
 *
 *  This reduces kvmclock difference on migration from 5s
 *  to 0.1s (when max_downtime == 5s), because sending the
 *  final pages of memory (which happens between vm_stop()
 *  and pre_save()) takes max_downtime.
 */
258
static int kvmclock_pre_save(void *opaque)
259 260 261 262
{
    KVMClockState *s = opaque;

    kvm_update_clock(s);
263 264

    return 0;
265 266
}

267 268 269 270
static const VMStateDescription kvmclock_vmsd = {
    .name = "kvmclock",
    .version_id = 1,
    .minimum_version_id = 1,
271
    .pre_save = kvmclock_pre_save,
272 273 274
    .fields = (VMStateField[]) {
        VMSTATE_UINT64(clock, KVMClockState),
        VMSTATE_END_OF_LIST()
275 276 277 278
    },
    .subsections = (const VMStateDescription * []) {
        &kvmclock_reliable_get_clock,
        NULL
279 280 281
    }
};

282 283 284 285 286 287
static Property kvmclock_properties[] = {
    DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
                      mach_use_reliable_get_clock, true),
    DEFINE_PROP_END_OF_LIST(),
};

288 289
static void kvmclock_class_init(ObjectClass *klass, void *data)
{
290
    DeviceClass *dc = DEVICE_CLASS(klass);
291

H
Hu Tao 已提交
292
    dc->realize = kvmclock_realize;
293
    dc->vmsd = &kvmclock_vmsd;
294
    dc->props = kvmclock_properties;
295 296
}

297
static const TypeInfo kvmclock_info = {
H
Hu Tao 已提交
298
    .name          = TYPE_KVM_CLOCK,
299 300 301
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(KVMClockState),
    .class_init    = kvmclock_class_init,
302 303 304 305 306
};

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

309
    if (kvm_enabled() &&
310 311
        cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
                                       (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
H
Hu Tao 已提交
312
        sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
313 314 315
    }
}

A
Andreas Färber 已提交
316
static void kvmclock_register_types(void)
317
{
318
    type_register_static(&kvmclock_info);
319 320
}

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