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