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