1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu/osdep.h"
17#include "qemu/host-utils.h"
18#include "qemu/module.h"
19#include "sysemu/kvm.h"
20#include "sysemu/runstate.h"
21#include "sysemu/hw_accel.h"
22#include "kvm/kvm_i386.h"
23#include "migration/vmstate.h"
24#include "hw/sysbus.h"
25#include "hw/kvm/clock.h"
26#include "hw/qdev-properties.h"
27#include "qapi/error.h"
28
29#include <linux/kvm.h>
30#include "standard-headers/asm-x86/kvm_para.h"
31#include "qom/object.h"
32
33#define TYPE_KVM_CLOCK "kvmclock"
34OBJECT_DECLARE_SIMPLE_TYPE(KVMClockState, KVM_CLOCK)
35
36struct KVMClockState {
37
38 SysBusDevice busdev;
39
40
41 uint64_t clock;
42 bool clock_valid;
43
44
45 bool runstate_paused;
46
47
48 bool mach_use_reliable_get_clock;
49
50
51
52 bool clock_is_reliable;
53};
54
55struct pvclock_vcpu_time_info {
56 uint32_t version;
57 uint32_t pad0;
58 uint64_t tsc_timestamp;
59 uint64_t system_time;
60 uint32_t tsc_to_system_mul;
61 int8_t tsc_shift;
62 uint8_t flags;
63 uint8_t pad[2];
64} __attribute__((__packed__));
65
66static uint64_t kvmclock_current_nsec(KVMClockState *s)
67{
68 CPUState *cpu = first_cpu;
69 CPUX86State *env = cpu->env_ptr;
70 hwaddr kvmclock_struct_pa;
71 uint64_t migration_tsc = env->tsc;
72 struct pvclock_vcpu_time_info time;
73 uint64_t delta;
74 uint64_t nsec_lo;
75 uint64_t nsec_hi;
76 uint64_t nsec;
77
78 cpu_synchronize_state(cpu);
79
80 if (!(env->system_time_msr & 1ULL)) {
81
82 return 0;
83 }
84
85 kvmclock_struct_pa = env->system_time_msr & ~1ULL;
86 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
87
88 assert(time.tsc_timestamp <= migration_tsc);
89 delta = migration_tsc - time.tsc_timestamp;
90 if (time.tsc_shift < 0) {
91 delta >>= -time.tsc_shift;
92 } else {
93 delta <<= time.tsc_shift;
94 }
95
96 mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
97 nsec = (nsec_lo >> 32) | (nsec_hi << 32);
98 return nsec + time.system_time;
99}
100
101static void kvm_update_clock(KVMClockState *s)
102{
103 struct kvm_clock_data data;
104 int ret;
105
106 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
107 if (ret < 0) {
108 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(-ret));
109 abort();
110 }
111 s->clock = data.clock;
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
150
151
152 s->clock_is_reliable = kvm_has_adjust_clock_stable();
153}
154
155static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data)
156{
157 int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
158
159 if (ret && ret != -EINVAL) {
160 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
161 }
162}
163
164static void kvmclock_vm_state_change(void *opaque, bool running,
165 RunState state)
166{
167 KVMClockState *s = opaque;
168 CPUState *cpu;
169 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
170 int ret;
171
172 if (running) {
173 struct kvm_clock_data data = {};
174
175
176
177
178
179 if (!s->clock_is_reliable) {
180 uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
181
182 if (pvclock_via_mem) {
183 s->clock = pvclock_via_mem;
184 }
185 }
186
187 s->clock_valid = false;
188
189 data.clock = s->clock;
190 ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
191 if (ret < 0) {
192 fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(-ret));
193 abort();
194 }
195
196 if (!cap_clock_ctrl) {
197 return;
198 }
199 CPU_FOREACH(cpu) {
200 run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
201 }
202 } else {
203
204 if (s->clock_valid) {
205 return;
206 }
207
208 s->runstate_paused = runstate_check(RUN_STATE_PAUSED);
209
210 kvm_synchronize_all_tsc();
211
212 kvm_update_clock(s);
213
214
215
216
217
218 s->clock_valid = true;
219 }
220}
221
222static void kvmclock_realize(DeviceState *dev, Error **errp)
223{
224 KVMClockState *s = KVM_CLOCK(dev);
225
226 if (!kvm_enabled()) {
227 error_setg(errp, "kvmclock device requires KVM");
228 return;
229 }
230
231 kvm_update_clock(s);
232
233 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
234}
235
236static bool kvmclock_clock_is_reliable_needed(void *opaque)
237{
238 KVMClockState *s = opaque;
239
240 return s->mach_use_reliable_get_clock;
241}
242
243static const VMStateDescription kvmclock_reliable_get_clock = {
244 .name = "kvmclock/clock_is_reliable",
245 .version_id = 1,
246 .minimum_version_id = 1,
247 .needed = kvmclock_clock_is_reliable_needed,
248 .fields = (VMStateField[]) {
249 VMSTATE_BOOL(clock_is_reliable, KVMClockState),
250 VMSTATE_END_OF_LIST()
251 }
252};
253
254
255
256
257
258static int kvmclock_pre_load(void *opaque)
259{
260 KVMClockState *s = opaque;
261
262 s->clock_is_reliable = false;
263
264 return 0;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281static int kvmclock_pre_save(void *opaque)
282{
283 KVMClockState *s = opaque;
284
285 if (!s->runstate_paused) {
286 kvm_update_clock(s);
287 }
288
289 return 0;
290}
291
292static const VMStateDescription kvmclock_vmsd = {
293 .name = "kvmclock",
294 .version_id = 1,
295 .minimum_version_id = 1,
296 .pre_load = kvmclock_pre_load,
297 .pre_save = kvmclock_pre_save,
298 .fields = (VMStateField[]) {
299 VMSTATE_UINT64(clock, KVMClockState),
300 VMSTATE_END_OF_LIST()
301 },
302 .subsections = (const VMStateDescription * []) {
303 &kvmclock_reliable_get_clock,
304 NULL
305 }
306};
307
308static Property kvmclock_properties[] = {
309 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState,
310 mach_use_reliable_get_clock, true),
311 DEFINE_PROP_END_OF_LIST(),
312};
313
314static void kvmclock_class_init(ObjectClass *klass, void *data)
315{
316 DeviceClass *dc = DEVICE_CLASS(klass);
317
318 dc->realize = kvmclock_realize;
319 dc->vmsd = &kvmclock_vmsd;
320 device_class_set_props(dc, kvmclock_properties);
321}
322
323static const TypeInfo kvmclock_info = {
324 .name = TYPE_KVM_CLOCK,
325 .parent = TYPE_SYS_BUS_DEVICE,
326 .instance_size = sizeof(KVMClockState),
327 .class_init = kvmclock_class_init,
328};
329
330
331void kvmclock_create(bool create_always)
332{
333 X86CPU *cpu = X86_CPU(first_cpu);
334
335 if (!kvm_enabled() || !kvm_has_adjust_clock())
336 return;
337
338 if (create_always ||
339 cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
340 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
341 sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
342 }
343}
344
345static void kvmclock_register_types(void)
346{
347 type_register_static(&kvmclock_info);
348}
349
350type_init(kvmclock_register_types)
351