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 "cpu.h" 18 #include "qemu/host-utils.h" 19 #include "qemu/module.h" 20 #include "sysemu/kvm.h" 21 #include "sysemu/runstate.h" 22 #include "sysemu/hw_accel.h" 23 #include "kvm_i386.h" 24 #include "migration/vmstate.h" 25 #include "hw/sysbus.h" 26 #include "hw/kvm/clock.h" 27 #include "hw/qdev-properties.h" 28 #include "qapi/error.h" 29 30 #include <linux/kvm.h> 31 #include "standard-headers/asm-x86/kvm_para.h" 32 #include "qom/object.h" 33 34 #define TYPE_KVM_CLOCK "kvmclock" 35 typedef struct KVMClockState KVMClockState; 36 DECLARE_INSTANCE_CHECKER(KVMClockState, KVM_CLOCK, 37 TYPE_KVM_CLOCK) 38 39 struct KVMClockState { 40 /*< private >*/ 41 SysBusDevice busdev; 42 /*< public >*/ 43 44 uint64_t clock; 45 bool clock_valid; 46 47 /* whether the 'clock' value was obtained in the 'paused' state */ 48 bool runstate_paused; 49 50 /* whether machine type supports reliable KVM_GET_CLOCK */ 51 bool mach_use_reliable_get_clock; 52 53 /* whether the 'clock' value was obtained in a host with 54 * reliable KVM_GET_CLOCK */ 55 bool clock_is_reliable; 56 }; 57 58 struct pvclock_vcpu_time_info { 59 uint32_t version; 60 uint32_t pad0; 61 uint64_t tsc_timestamp; 62 uint64_t system_time; 63 uint32_t tsc_to_system_mul; 64 int8_t tsc_shift; 65 uint8_t flags; 66 uint8_t pad[2]; 67 } __attribute__((__packed__)); /* 32 bytes */ 68 69 static uint64_t kvmclock_current_nsec(KVMClockState *s) 70 { 71 CPUState *cpu = first_cpu; 72 CPUX86State *env = cpu->env_ptr; 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 cpu_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, int 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 void kvmclock_realize(DeviceState *dev, Error **errp) 226 { 227 KVMClockState *s = KVM_CLOCK(dev); 228 229 if (!kvm_enabled()) { 230 error_setg(errp, "kvmclock device requires KVM"); 231 return; 232 } 233 234 kvm_update_clock(s); 235 236 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s); 237 } 238 239 static bool kvmclock_clock_is_reliable_needed(void *opaque) 240 { 241 KVMClockState *s = opaque; 242 243 return s->mach_use_reliable_get_clock; 244 } 245 246 static const VMStateDescription kvmclock_reliable_get_clock = { 247 .name = "kvmclock/clock_is_reliable", 248 .version_id = 1, 249 .minimum_version_id = 1, 250 .needed = kvmclock_clock_is_reliable_needed, 251 .fields = (VMStateField[]) { 252 VMSTATE_BOOL(clock_is_reliable, KVMClockState), 253 VMSTATE_END_OF_LIST() 254 } 255 }; 256 257 /* 258 * When migrating, assume the source has an unreliable 259 * KVM_GET_CLOCK unless told otherwise. 260 */ 261 static int kvmclock_pre_load(void *opaque) 262 { 263 KVMClockState *s = opaque; 264 265 s->clock_is_reliable = false; 266 267 return 0; 268 } 269 270 /* 271 * When migrating a running guest, read the clock just 272 * before migration, so that the guest clock counts 273 * during the events between: 274 * 275 * * vm_stop() 276 * * 277 * * pre_save() 278 * 279 * This reduces kvmclock difference on migration from 5s 280 * to 0.1s (when max_downtime == 5s), because sending the 281 * final pages of memory (which happens between vm_stop() 282 * and pre_save()) takes max_downtime. 283 */ 284 static int kvmclock_pre_save(void *opaque) 285 { 286 KVMClockState *s = opaque; 287 288 if (!s->runstate_paused) { 289 kvm_update_clock(s); 290 } 291 292 return 0; 293 } 294 295 static const VMStateDescription kvmclock_vmsd = { 296 .name = "kvmclock", 297 .version_id = 1, 298 .minimum_version_id = 1, 299 .pre_load = kvmclock_pre_load, 300 .pre_save = kvmclock_pre_save, 301 .fields = (VMStateField[]) { 302 VMSTATE_UINT64(clock, KVMClockState), 303 VMSTATE_END_OF_LIST() 304 }, 305 .subsections = (const VMStateDescription * []) { 306 &kvmclock_reliable_get_clock, 307 NULL 308 } 309 }; 310 311 static Property kvmclock_properties[] = { 312 DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState, 313 mach_use_reliable_get_clock, true), 314 DEFINE_PROP_END_OF_LIST(), 315 }; 316 317 static void kvmclock_class_init(ObjectClass *klass, void *data) 318 { 319 DeviceClass *dc = DEVICE_CLASS(klass); 320 321 dc->realize = kvmclock_realize; 322 dc->vmsd = &kvmclock_vmsd; 323 device_class_set_props(dc, kvmclock_properties); 324 } 325 326 static const TypeInfo kvmclock_info = { 327 .name = TYPE_KVM_CLOCK, 328 .parent = TYPE_SYS_BUS_DEVICE, 329 .instance_size = sizeof(KVMClockState), 330 .class_init = kvmclock_class_init, 331 }; 332 333 /* Note: Must be called after VCPU initialization. */ 334 void kvmclock_create(void) 335 { 336 X86CPU *cpu = X86_CPU(first_cpu); 337 338 if (kvm_enabled() && 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 345 static void kvmclock_register_types(void) 346 { 347 type_register_static(&kvmclock_info); 348 } 349 350 type_init(kvmclock_register_types) 351