clock.c 3.6 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 18
#include "sysemu/sysemu.h"
#include "sysemu/kvm.h"
19 20
#include "hw/sysbus.h"
#include "hw/kvm/clock.h"
21 22 23 24 25 26 27 28 29 30 31

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

typedef struct KVMClockState {
    SysBusDevice busdev;
    uint64_t clock;
    bool clock_valid;
} KVMClockState;


32 33
static void kvmclock_vm_state_change(void *opaque, int running,
                                     RunState state)
34 35
{
    KVMClockState *s = opaque;
36 37 38
    CPUArchState *penv = first_cpu;
    int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
    int ret;
39 40

    if (running) {
41 42
        struct kvm_clock_data data;

43
        s->clock_valid = false;
44

45 46 47 48 49 50 51 52
        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();
        }

53 54 55 56
        if (!cap_clock_ctrl) {
            return;
        }
        for (penv = first_cpu; penv != NULL; penv = penv->next_cpu) {
57
            ret = kvm_vcpu_ioctl(ENV_GET_CPU(penv), KVM_KVMCLOCK_CTRL, 0);
58 59 60 61 62 63 64
            if (ret) {
                if (ret != -EINVAL) {
                    fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
                }
                return;
            }
        }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    } else {
        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));
            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;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    }
}

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

107 108
static void kvmclock_class_init(ObjectClass *klass, void *data)
{
109
    DeviceClass *dc = DEVICE_CLASS(klass);
110 111 112
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);

    k->init = kvmclock_init;
113 114
    dc->no_user = 1;
    dc->vmsd = &kvmclock_vmsd;
115 116
}

117
static const TypeInfo kvmclock_info = {
118 119 120 121
    .name          = "kvmclock",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(KVMClockState),
    .class_init    = kvmclock_class_init,
122 123 124 125 126 127
};

/* Note: Must be called after VCPU initialization. */
void kvmclock_create(void)
{
    if (kvm_enabled() &&
128
        first_cpu->features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
J
Jan Kiszka 已提交
129
                                         (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
130 131 132 133
        sysbus_create_simple("kvmclock", -1, NULL);
    }
}

A
Andreas Färber 已提交
134
static void kvmclock_register_types(void)
135
{
136
    type_register_static(&kvmclock_info);
137 138
}

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