]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/kvm/clock.c
valgrind: avoid false positives in KVM_GET_DIRTY_LOG ioctl
[mirror_qemu.git] / hw / i386 / kvm / clock.c
CommitLineData
0ec329da
JK
1/*
2 * QEMU KVM support, paravirtual clock device
3 *
4 * Copyright (C) 2011 Siemens AG
5 *
6 * Authors:
7 * Jan Kiszka <jan.kiszka@siemens.com>
8 *
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
11 *
6b620ca3
PB
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.
0ec329da
JK
14 */
15
16#include "qemu-common.h"
9a48bcd1 17#include "qemu/host-utils.h"
9c17d615
PB
18#include "sysemu/sysemu.h"
19#include "sysemu/kvm.h"
317b0a6d 20#include "sysemu/cpus.h"
3b9a6ee5
JK
21#include "hw/sysbus.h"
22#include "hw/kvm/clock.h"
0ec329da 23
0ec329da
JK
24#include <linux/kvm.h>
25#include <linux/kvm_para.h>
26
98bdc0d7
HT
27#define TYPE_KVM_CLOCK "kvmclock"
28#define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK)
29
0ec329da 30typedef struct KVMClockState {
98bdc0d7 31 /*< private >*/
0ec329da 32 SysBusDevice busdev;
98bdc0d7
HT
33 /*< public >*/
34
0ec329da
JK
35 uint64_t clock;
36 bool clock_valid;
37} KVMClockState;
38
9a48bcd1
AG
39struct pvclock_vcpu_time_info {
40 uint32_t version;
41 uint32_t pad0;
42 uint64_t tsc_timestamp;
43 uint64_t system_time;
44 uint32_t tsc_to_system_mul;
45 int8_t tsc_shift;
46 uint8_t flags;
47 uint8_t pad[2];
48} __attribute__((__packed__)); /* 32 bytes */
49
50static uint64_t kvmclock_current_nsec(KVMClockState *s)
51{
52 CPUState *cpu = first_cpu;
53 CPUX86State *env = cpu->env_ptr;
54 hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
55 uint64_t migration_tsc = env->tsc;
56 struct pvclock_vcpu_time_info time;
57 uint64_t delta;
58 uint64_t nsec_lo;
59 uint64_t nsec_hi;
60 uint64_t nsec;
61
62 if (!(env->system_time_msr & 1ULL)) {
63 /* KVM clock not active */
64 return 0;
65 }
66
67 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
68
69 assert(time.tsc_timestamp <= migration_tsc);
70 delta = migration_tsc - time.tsc_timestamp;
71 if (time.tsc_shift < 0) {
72 delta >>= -time.tsc_shift;
73 } else {
74 delta <<= time.tsc_shift;
75 }
76
77 mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
78 nsec = (nsec_lo >> 32) | (nsec_hi << 32);
79 return nsec + time.system_time;
80}
0ec329da 81
1dfb4dd9
LC
82static void kvmclock_vm_state_change(void *opaque, int running,
83 RunState state)
0ec329da
JK
84{
85 KVMClockState *s = opaque;
e76d05c2 86 CPUState *cpu;
f349c12c
EM
87 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
88 int ret;
0ec329da
JK
89
90 if (running) {
00f4d64e 91 struct kvm_clock_data data;
9a48bcd1 92 uint64_t time_at_migration = kvmclock_current_nsec(s);
00f4d64e 93
0ec329da 94 s->clock_valid = false;
f349c12c 95
9a48bcd1
AG
96 /* We can't rely on the migrated clock value, just discard it */
97 if (time_at_migration) {
98 s->clock = time_at_migration;
99 }
100
00f4d64e
MT
101 data.clock = s->clock;
102 data.flags = 0;
103 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
104 if (ret < 0) {
105 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
106 abort();
107 }
108
f349c12c
EM
109 if (!cap_clock_ctrl) {
110 return;
111 }
bdc44640 112 CPU_FOREACH(cpu) {
182735ef 113 ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
f349c12c
EM
114 if (ret) {
115 if (ret != -EINVAL) {
116 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
117 }
118 return;
119 }
120 }
00f4d64e
MT
121 } else {
122 struct kvm_clock_data data;
123 int ret;
124
125 if (s->clock_valid) {
126 return;
127 }
317b0a6d
MT
128
129 cpu_synchronize_all_states();
1154d84d
EH
130 /* In theory, the cpu_synchronize_all_states() call above wouldn't
131 * affect the rest of the code, as the VCPU state inside CPUState
132 * is supposed to always match the VCPU state on the kernel side.
133 *
134 * In practice, calling cpu_synchronize_state() too soon will load the
135 * kernel-side APIC state into X86CPU.apic_state too early, APIC state
136 * won't be reloaded later because CPUState.vcpu_dirty==true, and
137 * outdated APIC state may be migrated to another host.
138 *
139 * The real fix would be to make sure outdated APIC state is read
140 * from the kernel again when necessary. While this is not fixed, we
141 * need the cpu_clean_all_dirty() call below.
142 */
317b0a6d 143 cpu_clean_all_dirty();
1154d84d 144
00f4d64e
MT
145 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
146 if (ret < 0) {
147 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
148 abort();
149 }
150 s->clock = data.clock;
151
152 /*
153 * If the VM is stopped, declare the clock state valid to
154 * avoid re-reading it on next vmsave (which would return
155 * a different value). Will be reset when the VM is continued.
156 */
157 s->clock_valid = true;
0ec329da
JK
158 }
159}
160
913bc638 161static void kvmclock_realize(DeviceState *dev, Error **errp)
0ec329da 162{
98bdc0d7 163 KVMClockState *s = KVM_CLOCK(dev);
0ec329da
JK
164
165 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
0ec329da
JK
166}
167
168static const VMStateDescription kvmclock_vmsd = {
169 .name = "kvmclock",
170 .version_id = 1,
171 .minimum_version_id = 1,
0ec329da
JK
172 .fields = (VMStateField[]) {
173 VMSTATE_UINT64(clock, KVMClockState),
174 VMSTATE_END_OF_LIST()
175 }
176};
177
999e12bb
AL
178static void kvmclock_class_init(ObjectClass *klass, void *data)
179{
39bffca2 180 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 181
913bc638 182 dc->realize = kvmclock_realize;
39bffca2 183 dc->vmsd = &kvmclock_vmsd;
999e12bb
AL
184}
185
8c43a6f0 186static const TypeInfo kvmclock_info = {
98bdc0d7 187 .name = TYPE_KVM_CLOCK,
39bffca2
AL
188 .parent = TYPE_SYS_BUS_DEVICE,
189 .instance_size = sizeof(KVMClockState),
190 .class_init = kvmclock_class_init,
0ec329da
JK
191};
192
193/* Note: Must be called after VCPU initialization. */
194void kvmclock_create(void)
195{
182735ef
AF
196 X86CPU *cpu = X86_CPU(first_cpu);
197
0ec329da 198 if (kvm_enabled() &&
182735ef
AF
199 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
200 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
98bdc0d7 201 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
0ec329da
JK
202 }
203}
204
83f7d43a 205static void kvmclock_register_types(void)
0ec329da 206{
39bffca2 207 type_register_static(&kvmclock_info);
0ec329da
JK
208}
209
83f7d43a 210type_init(kvmclock_register_types)