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