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