tests: acpi: add pvpanic-isa: testcase
[qemu.git] / hw / i386 / kvm / clock.c
blobdf70b4a0338abe0c548dbd668a117b5d9efc476a
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/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"
29 #include <linux/kvm.h>
30 #include "standard-headers/asm-x86/kvm_para.h"
31 #include "qom/object.h"
33 #define TYPE_KVM_CLOCK "kvmclock"
34 OBJECT_DECLARE_SIMPLE_TYPE(KVMClockState, KVM_CLOCK)
36 struct KVMClockState {
37 /*< private >*/
38 SysBusDevice busdev;
39 /*< public >*/
41 uint64_t clock;
42 bool clock_valid;
44 /* whether the 'clock' value was obtained in the 'paused' state */
45 bool runstate_paused;
47 /* whether machine type supports reliable KVM_GET_CLOCK */
48 bool mach_use_reliable_get_clock;
50 /* whether the 'clock' value was obtained in a host with
51 * reliable KVM_GET_CLOCK */
52 bool clock_is_reliable;
55 struct 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__)); /* 32 bytes */
66 static uint64_t kvmclock_current_nsec(KVMClockState *s)
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;
78 cpu_synchronize_state(cpu);
80 if (!(env->system_time_msr & 1ULL)) {
81 /* KVM clock not active */
82 return 0;
85 kvmclock_struct_pa = env->system_time_msr & ~1ULL;
86 cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
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;
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;
101 static void kvm_update_clock(KVMClockState *s)
103 struct kvm_clock_data data;
104 int ret;
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();
111 s->clock = data.clock;
113 /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
114 * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This
115 * can drift from the TSC-based value that is computed by the guest,
116 * so we need to go through kvmclock_current_nsec(). If
117 * kvm_has_adjust_clock_stable() is true, and the flags contain
118 * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value
119 * and kvmclock_current_nsec() is not necessary.
121 * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because:
123 * - if the host has disabled the kvmclock master clock, the guest already
124 * has protection against time going backwards. This "safety net" is only
125 * absent when kvmclock is stable;
127 * - therefore, we can replace a check like
129 * if last KVM_GET_CLOCK was not reliable then
130 * read from memory
132 * with
134 * if last KVM_GET_CLOCK was not reliable && masterclock is enabled
135 * read from memory
137 * However:
139 * - if kvm_has_adjust_clock_stable() returns false, the left side is
140 * always true (KVM_GET_CLOCK is never reliable), and the right side is
141 * unknown (because we don't have data.flags). We must assume it's true
142 * and read from memory.
144 * - if kvm_has_adjust_clock_stable() returns true, the result of the &&
145 * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable)
147 * So we can just use this instead:
149 * if !kvm_has_adjust_clock_stable() then
150 * read from memory
152 s->clock_is_reliable = kvm_has_adjust_clock_stable();
155 static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data)
157 int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
159 if (ret && ret != -EINVAL) {
160 fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
164 static void kvmclock_vm_state_change(void *opaque, bool running,
165 RunState state)
167 KVMClockState *s = opaque;
168 CPUState *cpu;
169 int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
170 int ret;
172 if (running) {
173 struct kvm_clock_data data = {};
176 * If the host where s->clock was read did not support reliable
177 * KVM_GET_CLOCK, read kvmclock value from memory.
179 if (!s->clock_is_reliable) {
180 uint64_t pvclock_via_mem = kvmclock_current_nsec(s);
181 /* We can't rely on the saved clock value, just discard it */
182 if (pvclock_via_mem) {
183 s->clock = pvclock_via_mem;
187 s->clock_valid = false;
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();
196 if (!cap_clock_ctrl) {
197 return;
199 CPU_FOREACH(cpu) {
200 run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL);
202 } else {
204 if (s->clock_valid) {
205 return;
208 s->runstate_paused = runstate_check(RUN_STATE_PAUSED);
210 kvm_synchronize_all_tsc();
212 kvm_update_clock(s);
214 * If the VM is stopped, declare the clock state valid to
215 * avoid re-reading it on next vmsave (which would return
216 * a different value). Will be reset when the VM is continued.
218 s->clock_valid = true;
222 static void kvmclock_realize(DeviceState *dev, Error **errp)
224 KVMClockState *s = KVM_CLOCK(dev);
226 if (!kvm_enabled()) {
227 error_setg(errp, "kvmclock device requires KVM");
228 return;
231 kvm_update_clock(s);
233 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
236 static bool kvmclock_clock_is_reliable_needed(void *opaque)
238 KVMClockState *s = opaque;
240 return s->mach_use_reliable_get_clock;
243 static 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()
255 * When migrating, assume the source has an unreliable
256 * KVM_GET_CLOCK unless told otherwise.
258 static int kvmclock_pre_load(void *opaque)
260 KVMClockState *s = opaque;
262 s->clock_is_reliable = false;
264 return 0;
268 * When migrating a running guest, read the clock just
269 * before migration, so that the guest clock counts
270 * during the events between:
272 * * vm_stop()
274 * * pre_save()
276 * This reduces kvmclock difference on migration from 5s
277 * to 0.1s (when max_downtime == 5s), because sending the
278 * final pages of memory (which happens between vm_stop()
279 * and pre_save()) takes max_downtime.
281 static int kvmclock_pre_save(void *opaque)
283 KVMClockState *s = opaque;
285 if (!s->runstate_paused) {
286 kvm_update_clock(s);
289 return 0;
292 static 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()
302 .subsections = (const VMStateDescription * []) {
303 &kvmclock_reliable_get_clock,
304 NULL
308 static 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(),
314 static void kvmclock_class_init(ObjectClass *klass, void *data)
316 DeviceClass *dc = DEVICE_CLASS(klass);
318 dc->realize = kvmclock_realize;
319 dc->vmsd = &kvmclock_vmsd;
320 device_class_set_props(dc, kvmclock_properties);
323 static 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,
330 /* Note: Must be called after VCPU initialization. */
331 void kvmclock_create(bool create_always)
333 X86CPU *cpu = X86_CPU(first_cpu);
335 if (!kvm_enabled() || !kvm_has_adjust_clock())
336 return;
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);
345 static void kvmclock_register_types(void)
347 type_register_static(&kvmclock_info);
350 type_init(kvmclock_register_types)