tests: cleanup ptimer-test
[qemu/ar7.git] / hw / i386 / kvm / clock.c
blob0f75dd385a465b8be2c953cf29836bb2a683cb29
1 /*
2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
6 * Authors:
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "cpu.h"
19 #include "qemu/host-utils.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/kvm.h"
22 #include "kvm_i386.h"
23 #include "hw/sysbus.h"
24 #include "hw/kvm/clock.h"
26 #include <linux/kvm.h>
27 #include <linux/kvm_para.h>
29 #define TYPE_KVM_CLOCK "kvmclock"
30 #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
32 typedef struct KVMClockState {
33 /*< private >*/
34 SysBusDevice busdev;
35 /*< public >*/
37 uint64_t clock;
38 bool clock_valid;
39 } KVMClockState;
41 struct pvclock_vcpu_time_info {
42 uint32_t version;
43 uint32_t pad0;
44 uint64_t tsc_timestamp;
45 uint64_t system_time;
46 uint32_t tsc_to_system_mul;
47 int8_t tsc_shift;
48 uint8_t flags;
49 uint8_t pad[2];
50 } __attribute__((__packed__)); /* 32 bytes */
52 static uint64_t kvmclock_current_nsec(KVMClockState *s)
54 CPUState *cpu = first_cpu;
55 CPUX86State *env = cpu->env_ptr;
56 hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
57 uint64_t migration_tsc = env->tsc;
58 struct pvclock_vcpu_time_info time;
59 uint64_t delta;
60 uint64_t nsec_lo;
61 uint64_t nsec_hi;
62 uint64_t nsec;
64 if (!(env->system_time_msr & 1ULL)) {
65 /* KVM clock not active */
66 return 0;
69 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
71 assert(time.tsc_timestamp <= migration_tsc);
72 delta = migration_tsc - time.tsc_timestamp;
73 if (time.tsc_shift < 0) {
74 delta >>= -time.tsc_shift;
75 } else {
76 delta <<= time.tsc_shift;
79 mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
80 nsec = (nsec_lo >> 32) | (nsec_hi << 32);
81 return nsec + time.system_time;
84 static void kvmclock_vm_state_change(void *opaque, int running,
85 RunState state)
87 KVMClockState *s = opaque;
88 CPUState *cpu;
89 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
90 int ret;
92 if (running) {
93 struct kvm_clock_data data = {};
94 uint64_t time_at_migration = kvmclock_current_nsec(s);
96 s->clock_valid = false;
98 /* We can't rely on the migrated clock value, just discard it */
99 if (time_at_migration) {
100 s->clock = time_at_migration;
103 data.clock = s->clock;
104 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
105 if (ret < 0) {
106 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
107 abort();
110 if (!cap_clock_ctrl) {
111 return;
113 CPU_FOREACH(cpu) {
114 ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
115 if (ret) {
116 if (ret != -EINVAL) {
117 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
119 return;
122 } else {
123 struct kvm_clock_data data;
124 int ret;
126 if (s->clock_valid) {
127 return;
130 kvm_synchronize_all_tsc();
132 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
133 if (ret < 0) {
134 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
135 abort();
137 s->clock = data.clock;
140 * If the VM is stopped, declare the clock state valid to
141 * avoid re-reading it on next vmsave (which would return
142 * a different value). Will be reset when the VM is continued.
144 s->clock_valid = true;
148 static void kvmclock_realize(DeviceState *dev, Error **errp)
150 KVMClockState *s = KVM_CLOCK(dev);
152 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
155 static const VMStateDescription kvmclock_vmsd = {
156 .name = "kvmclock",
157 .version_id = 1,
158 .minimum_version_id = 1,
159 .fields = (VMStateField[]) {
160 VMSTATE_UINT64(clock, KVMClockState),
161 VMSTATE_END_OF_LIST()
165 static void kvmclock_class_init(ObjectClass *klass, void *data)
167 DeviceClass *dc = DEVICE_CLASS(klass);
169 dc->realize = kvmclock_realize;
170 dc->vmsd = &kvmclock_vmsd;
173 static const TypeInfo kvmclock_info = {
174 .name = TYPE_KVM_CLOCK,
175 .parent = TYPE_SYS_BUS_DEVICE,
176 .instance_size = sizeof(KVMClockState),
177 .class_init = kvmclock_class_init,
180 /* Note: Must be called after VCPU initialization. */
181 void kvmclock_create(void)
183 X86CPU *cpu = X86_CPU(first_cpu);
185 if (kvm_enabled() &&
186 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
187 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
188 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
192 static void kvmclock_register_types(void)
194 type_register_static(&kvmclock_info);
197 type_init(kvmclock_register_types)