| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * QEMU KVM support, paravirtual clock device | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2011 Siemens AG | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Authors: | 
					
						
							|  |  |  |  *  Jan Kiszka        <jan.kiszka@siemens.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL version 2. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-01-13 17:44:23 +01:00
										 |  |  |  * Contributions after 2012-01-13 are licensed under the terms of the | 
					
						
							|  |  |  |  * GNU GPL, version 2 or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:03 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  | #include "qemu/host-utils.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:04 +01:00
										 |  |  | #include "sysemu/kvm.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:59 +02:00
										 |  |  | #include "sysemu/runstate.h"
 | 
					
						
							| 
									
										
										
										
											2017-05-29 13:49:04 +03:00
										 |  |  | #include "sysemu/hw_accel.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-12 16:55:08 +01:00
										 |  |  | #include "kvm/kvm_i386.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:45 +02:00
										 |  |  | #include "migration/vmstate.h"
 | 
					
						
							| 
									
										
										
										
											2011-10-15 10:01:27 +02:00
										 |  |  | #include "hw/sysbus.h"
 | 
					
						
							| 
									
										
										
										
											2023-06-20 07:50:26 +02:00
										 |  |  | #include "hw/i386/kvm/clock.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:51 +02:00
										 |  |  | #include "hw/qdev-properties.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 15:50:46 -03:00
										 |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <linux/kvm.h>
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:47:50 +03:00
										 |  |  | #include "standard-headers/asm-x86/kvm_para.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | #include "qom/object.h"
 | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  | #define TYPE_KVM_CLOCK "kvmclock"
 | 
					
						
							| 
									
										
										
										
											2020-09-16 14:25:19 -04:00
										 |  |  | OBJECT_DECLARE_SIMPLE_TYPE(KVMClockState, KVM_CLOCK) | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | struct KVMClockState { | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  |     /*< private >*/ | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     SysBusDevice busdev; | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  |     /*< public >*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     uint64_t clock; | 
					
						
							|  |  |  |     bool clock_valid; | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 18:07:11 -03:00
										 |  |  |     /* whether the 'clock' value was obtained in the 'paused' state */ | 
					
						
							|  |  |  |     bool runstate_paused; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |     /* whether machine type supports reliable KVM_GET_CLOCK */ | 
					
						
							|  |  |  |     bool mach_use_reliable_get_clock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* whether the 'clock' value was obtained in a host with
 | 
					
						
							|  |  |  |      * reliable KVM_GET_CLOCK */ | 
					
						
							|  |  |  |     bool clock_is_reliable; | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  | struct pvclock_vcpu_time_info { | 
					
						
							|  |  |  |     uint32_t   version; | 
					
						
							|  |  |  |     uint32_t   pad0; | 
					
						
							|  |  |  |     uint64_t   tsc_timestamp; | 
					
						
							|  |  |  |     uint64_t   system_time; | 
					
						
							|  |  |  |     uint32_t   tsc_to_system_mul; | 
					
						
							|  |  |  |     int8_t     tsc_shift; | 
					
						
							|  |  |  |     uint8_t    flags; | 
					
						
							|  |  |  |     uint8_t    pad[2]; | 
					
						
							|  |  |  | } __attribute__((__packed__)); /* 32 bytes */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint64_t kvmclock_current_nsec(KVMClockState *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CPUState *cpu = first_cpu; | 
					
						
							| 
									
										
										
										
											2023-09-13 17:22:49 -07:00
										 |  |  |     CPUX86State *env = cpu_env(cpu); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:00:19 -04:00
										 |  |  |     hwaddr kvmclock_struct_pa; | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  |     uint64_t migration_tsc = env->tsc; | 
					
						
							|  |  |  |     struct pvclock_vcpu_time_info time; | 
					
						
							|  |  |  |     uint64_t delta; | 
					
						
							|  |  |  |     uint64_t nsec_lo; | 
					
						
							|  |  |  |     uint64_t nsec_hi; | 
					
						
							|  |  |  |     uint64_t nsec; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-29 13:49:04 +03:00
										 |  |  |     cpu_synchronize_state(cpu); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  |     if (!(env->system_time_msr & 1ULL)) { | 
					
						
							|  |  |  |         /* KVM clock not active */ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:00:19 -04:00
										 |  |  |     kvmclock_struct_pa = env->system_time_msr & ~1ULL; | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  |     cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(time.tsc_timestamp <= migration_tsc); | 
					
						
							|  |  |  |     delta = migration_tsc - time.tsc_timestamp; | 
					
						
							|  |  |  |     if (time.tsc_shift < 0) { | 
					
						
							|  |  |  |         delta >>= -time.tsc_shift; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         delta <<= time.tsc_shift; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul); | 
					
						
							|  |  |  |     nsec = (nsec_lo >> 32) | (nsec_hi << 32); | 
					
						
							|  |  |  |     return nsec + time.system_time; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | static void kvm_update_clock(KVMClockState *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct kvm_clock_data data; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2021-05-19 14:35:28 +03:00
										 |  |  |         fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(-ret)); | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |                 abort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s->clock = data.clock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If kvm_has_adjust_clock_stable() is false, KVM_GET_CLOCK returns
 | 
					
						
							|  |  |  |      * essentially CLOCK_MONOTONIC plus a guest-specific adjustment.  This | 
					
						
							|  |  |  |      * can drift from the TSC-based value that is computed by the guest, | 
					
						
							|  |  |  |      * so we need to go through kvmclock_current_nsec().  If | 
					
						
							|  |  |  |      * kvm_has_adjust_clock_stable() is true, and the flags contain | 
					
						
							|  |  |  |      * KVM_CLOCK_TSC_STABLE, then KVM_GET_CLOCK returns a TSC-based value | 
					
						
							|  |  |  |      * and kvmclock_current_nsec() is not necessary. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Here, however, we need not check KVM_CLOCK_TSC_STABLE.  This is because: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - if the host has disabled the kvmclock master clock, the guest already | 
					
						
							|  |  |  |      *   has protection against time going backwards.  This "safety net" is only | 
					
						
							|  |  |  |      *   absent when kvmclock is stable; | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - therefore, we can replace a check like | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *       if last KVM_GET_CLOCK was not reliable then | 
					
						
							|  |  |  |      *               read from memory | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *   with | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *       if last KVM_GET_CLOCK was not reliable && masterclock is enabled | 
					
						
							|  |  |  |      *               read from memory | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * However: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - if kvm_has_adjust_clock_stable() returns false, the left side is | 
					
						
							|  |  |  |      *   always true (KVM_GET_CLOCK is never reliable), and the right side is | 
					
						
							|  |  |  |      *   unknown (because we don't have data.flags).  We must assume it's true | 
					
						
							|  |  |  |      *   and read from memory. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * - if kvm_has_adjust_clock_stable() returns true, the result of the && | 
					
						
							|  |  |  |      *   is always false (masterclock is enabled iff KVM_GET_CLOCK is reliable) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * So we can just use this instead: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *       if !kvm_has_adjust_clock_stable() then | 
					
						
							|  |  |  |      *               read from memory | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     s->clock_is_reliable = kvm_has_adjust_clock_stable(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 21:22:44 +08:00
										 |  |  | static void do_kvmclock_ctrl(CPUState *cpu, run_on_cpu_data data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ret && ret != -EINVAL) { | 
					
						
							|  |  |  |         fprintf(stderr, "%s: %s\n", __func__, strerror(-ret)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 16:20:20 +01:00
										 |  |  | static void kvmclock_vm_state_change(void *opaque, bool running, | 
					
						
							| 
									
										
										
										
											2011-07-29 14:26:33 -03:00
										 |  |  |                                      RunState state) | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     KVMClockState *s = opaque; | 
					
						
							| 
									
										
										
										
											2013-09-17 22:39:55 +02:00
										 |  |  |     CPUState *cpu; | 
					
						
							| 
									
										
										
										
											2012-04-07 06:17:47 +05:30
										 |  |  |     int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL); | 
					
						
							|  |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (running) { | 
					
						
							| 
									
										
										
										
											2014-10-14 11:55:49 +02:00
										 |  |  |         struct kvm_clock_data data = {}; | 
					
						
							| 
									
										
										
										
											2012-04-07 06:17:47 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If the host where s->clock was read did not support reliable | 
					
						
							|  |  |  |          * KVM_GET_CLOCK, read kvmclock value from memory. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if (!s->clock_is_reliable) { | 
					
						
							|  |  |  |             uint64_t pvclock_via_mem = kvmclock_current_nsec(s); | 
					
						
							|  |  |  |             /* We can't rely on the saved clock value, just discard it */ | 
					
						
							|  |  |  |             if (pvclock_via_mem) { | 
					
						
							|  |  |  |                 s->clock = pvclock_via_mem; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:45 -03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |         s->clock_valid = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 20:38:25 -03:00
										 |  |  |         data.clock = s->clock; | 
					
						
							|  |  |  |         ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data); | 
					
						
							|  |  |  |         if (ret < 0) { | 
					
						
							| 
									
										
										
										
											2021-05-19 14:35:28 +03:00
										 |  |  |             fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(-ret)); | 
					
						
							| 
									
										
										
										
											2013-06-18 20:38:25 -03:00
										 |  |  |             abort(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-07 06:17:47 +05:30
										 |  |  |         if (!cap_clock_ctrl) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-06-24 23:50:24 +02:00
										 |  |  |         CPU_FOREACH(cpu) { | 
					
						
							| 
									
										
										
										
											2018-07-11 21:22:44 +08:00
										 |  |  |             run_on_cpu(cpu, do_kvmclock_ctrl, RUN_ON_CPU_NULL); | 
					
						
							| 
									
										
										
										
											2012-04-07 06:17:47 +05:30
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-06-18 20:38:25 -03:00
										 |  |  |     } else { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (s->clock_valid) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-05 10:52:47 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 18:07:11 -03:00
										 |  |  |         s->runstate_paused = runstate_check(RUN_STATE_PAUSED); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 11:51:03 +08:00
										 |  |  |         kvm_synchronize_all_tsc(); | 
					
						
							| 
									
										
										
										
											2014-11-03 15:45:34 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |         kvm_update_clock(s); | 
					
						
							| 
									
										
										
										
											2013-06-18 20:38:25 -03:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If the VM is stopped, declare the clock state valid to | 
					
						
							|  |  |  |          * avoid re-reading it on next vmsave (which would return | 
					
						
							|  |  |  |          * a different value). Will be reset when the VM is continued. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         s->clock_valid = true; | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:39 +08:00
										 |  |  | static void kvmclock_realize(DeviceState *dev, Error **errp) | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  |     KVMClockState *s = KVM_CLOCK(dev); | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 15:50:46 -03:00
										 |  |  |     if (!kvm_enabled()) { | 
					
						
							|  |  |  |         error_setg(errp, "kvmclock device requires KVM"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |     kvm_update_clock(s); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | static bool kvmclock_clock_is_reliable_needed(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KVMClockState *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return s->mach_use_reliable_get_clock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const VMStateDescription kvmclock_reliable_get_clock = { | 
					
						
							|  |  |  |     .name = "kvmclock/clock_is_reliable", | 
					
						
							|  |  |  |     .version_id = 1, | 
					
						
							|  |  |  |     .minimum_version_id = 1, | 
					
						
							|  |  |  |     .needed = kvmclock_clock_is_reliable_needed, | 
					
						
							| 
									
										
										
										
											2023-12-21 14:16:12 +11:00
										 |  |  |     .fields = (const VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |         VMSTATE_BOOL(clock_is_reliable, KVMClockState), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:34:06 +10:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * When migrating, assume the source has an unreliable | 
					
						
							|  |  |  |  * KVM_GET_CLOCK unless told otherwise. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int kvmclock_pre_load(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KVMClockState *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s->clock_is_reliable = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2019-08-29 18:07:11 -03:00
										 |  |  |  * When migrating a running guest, read the clock just | 
					
						
							|  |  |  |  * before migration, so that the guest clock counts | 
					
						
							|  |  |  |  * during the events between: | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  * vm_stop() | 
					
						
							|  |  |  |  *  * | 
					
						
							|  |  |  |  *  * pre_save() | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This reduces kvmclock difference on migration from 5s | 
					
						
							|  |  |  |  *  to 0.1s (when max_downtime == 5s), because sending the | 
					
						
							|  |  |  |  *  final pages of memory (which happens between vm_stop() | 
					
						
							|  |  |  |  *  and pre_save()) takes max_downtime. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-25 12:29:12 +01:00
										 |  |  | static int kvmclock_pre_save(void *opaque) | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | { | 
					
						
							|  |  |  |     KVMClockState *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 18:07:11 -03:00
										 |  |  |     if (!s->runstate_paused) { | 
					
						
							|  |  |  |         kvm_update_clock(s); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-25 12:29:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | static const VMStateDescription kvmclock_vmsd = { | 
					
						
							|  |  |  |     .name = "kvmclock", | 
					
						
							|  |  |  |     .version_id = 1, | 
					
						
							|  |  |  |     .minimum_version_id = 1, | 
					
						
							| 
									
										
										
										
											2018-04-06 15:34:06 +10:00
										 |  |  |     .pre_load = kvmclock_pre_load, | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |     .pre_save = kvmclock_pre_save, | 
					
						
							| 
									
										
										
										
											2023-12-21 14:16:12 +11:00
										 |  |  |     .fields = (const VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |         VMSTATE_UINT64(clock, KVMClockState), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-12-21 14:16:12 +11:00
										 |  |  |     .subsections = (const VMStateDescription * const []) { | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  |         &kvmclock_reliable_get_clock, | 
					
						
							|  |  |  |         NULL | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 08:50:04 -02:00
										 |  |  | static Property kvmclock_properties[] = { | 
					
						
							|  |  |  |     DEFINE_PROP_BOOL("x-mach-use-reliable-get-clock", KVMClockState, | 
					
						
							|  |  |  |                       mach_use_reliable_get_clock, true), | 
					
						
							|  |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-24 13:12:29 -06:00
										 |  |  | static void kvmclock_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2012-01-24 13:12:29 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:39 +08:00
										 |  |  |     dc->realize = kvmclock_realize; | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     dc->vmsd = &kvmclock_vmsd; | 
					
						
							| 
									
										
										
										
											2020-01-10 19:30:32 +04:00
										 |  |  |     device_class_set_props(dc, kvmclock_properties); | 
					
						
							| 
									
										
										
										
											2012-01-24 13:12:29 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-10 16:19:07 +01:00
										 |  |  | static const TypeInfo kvmclock_info = { | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  |     .name          = TYPE_KVM_CLOCK, | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     .parent        = TYPE_SYS_BUS_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(KVMClockState), | 
					
						
							|  |  |  |     .class_init    = kvmclock_class_init, | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Note: Must be called after VCPU initialization. */ | 
					
						
							| 
									
										
										
										
											2020-09-22 17:19:34 +02:00
										 |  |  | void kvmclock_create(bool create_always) | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-05-29 22:29:20 +02:00
										 |  |  |     X86CPU *cpu = X86_CPU(first_cpu); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-20 07:18:12 +02:00
										 |  |  |     assert(kvm_enabled()); | 
					
						
							| 
									
										
										
										
											2020-09-22 17:19:34 +02:00
										 |  |  |     if (create_always || | 
					
						
							| 
									
										
										
										
											2013-05-29 22:29:20 +02:00
										 |  |  |         cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) | | 
					
						
							|  |  |  |                                        (1ULL << KVM_FEATURE_CLOCKSOURCE2))) { | 
					
						
							| 
									
										
										
										
											2013-07-01 18:18:38 +08:00
										 |  |  |         sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL); | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | static void kvmclock_register_types(void) | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     type_register_static(&kvmclock_info); | 
					
						
							| 
									
										
										
										
											2011-02-07 12:19:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | type_init(kvmclock_register_types) |