| 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 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | #include "qemu/host-utils.h" |
| 18 | #include "qemu/module.h" |
| 19 | #include "system/kvm.h" |
| 20 | #include "system/runstate.h" |
| 21 | #include "system/hw_accel.h" |
| 22 | #include "system/physmem.h" |
| 23 | #include "kvm/kvm_i386.h" |
| 24 | #include "migration/vmstate.h" |
| 25 | #include "hw/core/sysbus.h" |
| 26 | #include "hw/i386/kvm/clock.h" |
| 27 | #include "exec/cpu-common.h" |
| 28 | #include "qapi/error.h" |
| 29 | |
| 30 | #include <linux/kvm.h> |
| 31 | #include "qom/object.h" |
| 32 | |
| 33 | #define TYPE_KVM_CLOCK "kvmclock" |
| 34 | OBJECT_DECLARE_SIMPLE_TYPE(KVMClockState, KVM_CLOCK) |
| 35 | |
| 36 | struct KVMClockState { |
| 37 | /*< private >*/ |
| 38 | SysBusDevice busdev; |
| 39 | /*< public >*/ |
| 40 | |
| 41 | uint64_t clock; |
| 42 | bool clock_valid; |
| 43 | |
| 44 | /* whether the 'clock' value was obtained in the 'paused' state */ |
| 45 | bool runstate_paused; |
| 46 | |
| 47 | /* whether the 'clock' value was obtained in a host with |
| 48 | * reliable KVM_GET_CLOCK */ |
| 49 | bool clock_is_reliable; |
| 50 | |
| 51 | NotifierWithReturn kvmclock_vcpufd_change_notifier; |
| 52 | NotifierWithReturn kvmclock_vmfd_change_notifier; |
| 53 | }; |
| 54 | |
| 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 */ |
| 65 | |
| 66 | static int kvmclock_set_clock(NotifierWithReturn *notifier, |
| 67 | void *data, Error** errp); |
| 68 | |
| 69 | static uint64_t kvmclock_current_nsec(KVMClockState *s) |
| 70 | { |
| 71 | CPUState *cpu = first_cpu; |
| 72 | CPUX86State *env = cpu_env(cpu); |
| 73 | hwaddr kvmclock_struct_pa; |
| 74 | uint64_t migration_tsc = env->tsc; |
| 75 | struct pvclock_vcpu_time_info time; |
| 76 | uint64_t delta; |
| 77 | uint64_t nsec_lo; |
| 78 | uint64_t nsec_hi; |
| 79 | uint64_t nsec; |
| 80 | |
| 81 | cpu_synchronize_state(cpu); |
| 82 | |
| 83 | if (!(env->system_time_msr & 1ULL)) { |
| 84 | /* KVM clock not active */ |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | kvmclock_struct_pa = env->system_time_msr & ~1ULL; |
| 89 | physical_memory_read(kvmclock_struct_pa, &time, sizeof(time)); |
| 90 | |
| 91 | assert(time.tsc_timestamp <= migration_tsc); |
| 92 | delta = migration_tsc - time.tsc_timestamp; |
| 93 | if (time.tsc_shift < 0) { |
| 94 | delta >>= -time.tsc_shift; |
| 95 | } else { |
| 96 | delta <<= time.tsc_shift; |
| 97 | } |
| 98 | |
| 99 | mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul); |
| 100 | nsec = (nsec_lo >> 32) | (nsec_hi << 32); |
| 101 | return nsec + time.system_time; |
| 102 | } |
| 103 | |
| 104 | static void kvm_update_clock(KVMClockState *s) |
| 105 | { |
| 106 | struct kvm_clock_data data; |
| 107 | int ret; |
| 108 | |
| 109 | ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data); |
| 110 | if (ret < 0) { |
| 111 | fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(-ret)); |
| 112 | abort(); |
| 113 | } |
| 114 | s->clock = data.clock; |
| 115 | |
| 116 | /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns |
| 117 | * essentially CLOCK_MONOTONIC plus a guest-specific adjustment. This |
| 118 | * can drift from the TSC-based value that is computed by the guest, |
| 119 | * so we need to go through kvmclock_current_nsec(). If |
| 120 | * kvm_has_adjust_clock_stable() is true, and the flags contain |
| 121 | * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value |
| 122 | * and kvmclock_current_nsec() is not necessary. |
| 123 | * |
| 124 | * Here, however, we need not check KVM_CLOCK_TSC_STABLE. This is because: |
| 125 | * |
| 126 | * - if the host has disabled the kvmclock master clock, the guest already |
| 127 | * has protection against time going backwards. This "safety net" is only |
| 128 | * absent when kvmclock is stable; |
| 129 | * |
| 130 | * - therefore, we can replace a check like |
| 131 | * |
| 132 | * if last KVM_GET_CLOCK was not reliable then |
| 133 | * read from memory |
| 134 | * |
| 135 | * with |
| 136 | * |
| 137 | * if last KVM_GET_CLOCK was not reliable && masterclock is enabled |
| 138 | * read from memory |
| 139 | * |
| 140 | * However: |
| 141 | * |
| 142 | * - if kvm_has_adjust_clock_stable() returns false, the left side is |
| 143 | * always true (KVM_GET_CLOCK is never reliable), and the right side is |
| 144 | * unknown (because we don't have data.flags). We must assume it's true |
| 145 | * and read from memory. |
| 146 | * |
| 147 | * - if kvm_has_adjust_clock_stable() returns true, the result of the && |
| 148 | * is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable) |
| 149 | * |
| 150 | * So we can just use this instead: |
| 151 | * |
| 152 | * if !kvm_has_adjust_clock_stable() then |
| 153 | * read from memory |
| 154 | */ |
| 155 | s->clock_is_reliable = kvm_has_adjust_clock_stable(); |
| 156 | } |
| 157 | |
| 158 | static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data) |
| 159 | { |
| 160 | int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0); |
| 161 | |
| 162 | if (ret && ret != -EINVAL) { |
| 163 | fprintf(stderr, "%s: %s\n", __func__, strerror(-ret)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void kvmclock_vm_state_change(void *opaque, bool running, |
| 168 | RunState state) |
| 169 | { |
| 170 | KVMClockState *s = opaque; |
| 171 | CPUState *cpu; |
| 172 | int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL); |
| 173 | int ret; |
| 174 | |
| 175 | if (running) { |
| 176 | struct kvm_clock_data data = {}; |
| 177 | |
| 178 | /* |
| 179 | * If the host where s->clock was read did not support reliable |
| 180 | * KVM_GET_CLOCK, read kvmclock value from memory. |
| 181 | */ |
| 182 | if (!s->clock_is_reliable) { |
| 183 | uint64_t pvclock_via_mem = kvmclock_current_nsec(s); |
| 184 | /* We can't rely on the saved clock value, just discard it */ |
| 185 | if (pvclock_via_mem) { |
| 186 | s->clock = pvclock_via_mem; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | s->clock_valid = false; |
| 191 | |
| 192 | data.clock = s->clock; |
| 193 | ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data); |
| 194 | if (ret < 0) { |
| 195 | fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(-ret)); |
| 196 | abort(); |
| 197 | } |
| 198 | |
| 199 | if (!cap_clock_ctrl) { |
| 200 | return; |
| 201 | } |
| 202 | CPU_FOREACH(cpu) { |
| 203 | run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL); |
| 204 | } |
| 205 | } else { |
| 206 | |
| 207 | if (s->clock_valid) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | s->runstate_paused = runstate_check(RUN_STATE_PAUSED); |
| 212 | |
| 213 | kvm_synchronize_all_tsc(); |
| 214 | |
| 215 | kvm_update_clock(s); |
| 216 | /* |
| 217 | * If the VM is stopped, declare the clock state valid to |
| 218 | * avoid re-reading it on next vmsave (which would return |
| 219 | * a different value). Will be reset when the VM is continued. |
| 220 | */ |
| 221 | s->clock_valid = true; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static int kvmclock_save_clock(NotifierWithReturn *notifier, |
| 226 | void *data, Error** errp) |
| 227 | { |
| 228 | if (!((VmfdChangeNotifier *)data)->pre) { |
| 229 | return 0; |
| 230 | } |
| 231 | KVMClockState *s = container_of(notifier, KVMClockState, |
| 232 | kvmclock_vmfd_change_notifier); |
| 233 | kvm_update_clock(s); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int kvmclock_set_clock(NotifierWithReturn *notifier, |
| 238 | void *data, Error** errp) |
| 239 | { |
| 240 | struct kvm_clock_data clock_data = {}; |
| 241 | CPUState *cpu; |
| 242 | int ret; |
| 243 | KVMClockState *s = container_of(notifier, KVMClockState, |
| 244 | kvmclock_vcpufd_change_notifier); |
| 245 | int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL); |
| 246 | |
| 247 | if (!s->clock_is_reliable) { |
| 248 | uint64_t pvclock_via_mem = kvmclock_current_nsec(s); |
| 249 | /* saved clock value before vmfd change is not reliable */ |
| 250 | if (pvclock_via_mem) { |
| 251 | s->clock = pvclock_via_mem; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | clock_data.clock = s->clock; |
| 256 | ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &clock_data); |
| 257 | if (ret < 0) { |
| 258 | fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(-ret)); |
| 259 | abort(); |
| 260 | } |
| 261 | |
| 262 | if (!cap_clock_ctrl) { |
| 263 | return 0; |
| 264 | } |
| 265 | CPU_FOREACH(cpu) { |
| 266 | run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL); |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | static void kvmclock_realize(DeviceState *dev, Error **errp) |
| 274 | { |
| 275 | KVMClockState *s = KVM_CLOCK(dev); |
| 276 | |
| 277 | if (!kvm_enabled()) { |
| 278 | error_setg(errp, "kvmclock device requires KVM"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | kvm_update_clock(s); |
| 283 | |
| 284 | s->kvmclock_vcpufd_change_notifier.notify = kvmclock_set_clock; |
| 285 | s->kvmclock_vmfd_change_notifier.notify = kvmclock_save_clock; |
| 286 | |
| 287 | qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s); |
| 288 | kvm_vcpufd_add_change_notifier(&s->kvmclock_vcpufd_change_notifier); |
| 289 | kvm_vmfd_add_change_notifier(&s->kvmclock_vmfd_change_notifier); |
| 290 | } |
| 291 | |
| 292 | static const VMStateDescription kvmclock_reliable_get_clock = { |
| 293 | .name = "kvmclock/clock_is_reliable", |
| 294 | .version_id = 1, |
| 295 | .minimum_version_id = 1, |
| 296 | .fields = (const VMStateField[]) { |
| 297 | VMSTATE_BOOL(clock_is_reliable, KVMClockState), |
| 298 | VMSTATE_END_OF_LIST() |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | /* |
| 303 | * When migrating, assume the source has an unreliable |
| 304 | * KVM_GET_CLOCK unless told otherwise. |
| 305 | */ |
| 306 | static int kvmclock_pre_load(void *opaque) |
| 307 | { |
| 308 | KVMClockState *s = opaque; |
| 309 | |
| 310 | s->clock_is_reliable = false; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * When migrating a running guest, read the clock just |
| 317 | * before migration, so that the guest clock counts |
| 318 | * during the events between: |
| 319 | * |
| 320 | * * vm_stop() |
| 321 | * * |
| 322 | * * pre_save() |
| 323 | * |
| 324 | * This reduces kvmclock difference on migration from 5s |
| 325 | * to 0.1s (when max_downtime == 5s), because sending the |
| 326 | * final pages of memory (which happens between vm_stop() |
| 327 | * and pre_save()) takes max_downtime. |
| 328 | */ |
| 329 | static int kvmclock_pre_save(void *opaque) |
| 330 | { |
| 331 | KVMClockState *s = opaque; |
| 332 | |
| 333 | if (!s->runstate_paused) { |
| 334 | kvm_update_clock(s); |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static const VMStateDescription kvmclock_vmsd = { |
| 341 | .name = "kvmclock", |
| 342 | .version_id = 1, |
| 343 | .minimum_version_id = 1, |
| 344 | .pre_load = kvmclock_pre_load, |
| 345 | .pre_save = kvmclock_pre_save, |
| 346 | .fields = (const VMStateField[]) { |
| 347 | VMSTATE_UINT64(clock, KVMClockState), |
| 348 | VMSTATE_END_OF_LIST() |
| 349 | }, |
| 350 | .subsections = (const VMStateDescription * const []) { |
| 351 | &kvmclock_reliable_get_clock, |
| 352 | NULL |
| 353 | } |
| 354 | }; |
| 355 | |
| 356 | static void kvmclock_class_init(ObjectClass *klass, const void *data) |
| 357 | { |
| 358 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 359 | |
| 360 | dc->realize = kvmclock_realize; |
| 361 | dc->vmsd = &kvmclock_vmsd; |
| 362 | } |
| 363 | |
| 364 | static const TypeInfo kvmclock_info = { |
| 365 | .name = TYPE_KVM_CLOCK, |
| 366 | .parent = TYPE_SYS_BUS_DEVICE, |
| 367 | .instance_size = sizeof(KVMClockState), |
| 368 | .class_init = kvmclock_class_init, |
| 369 | }; |
| 370 | |
| 371 | /* Note: Must be called after VCPU initialization. */ |
| 372 | void kvmclock_create(bool create_always) |
| 373 | { |
| 374 | X86CPU *cpu = X86_CPU(first_cpu); |
| 375 | |
| 376 | assert(kvm_enabled()); |
| 377 | if (create_always || |
| 378 | cpu->env.features[FEAT_KVM] & (CPUID_KVM_CLOCK | |
| 379 | CPUID_KVM_CLOCK2)) { |
| 380 | sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void kvmclock_register_types(void) |
| 385 | { |
| 386 | type_register_static(&kvmclock_info); |
| 387 | } |
| 388 | |
| 389 | type_init(kvmclock_register_types) |