clock.c 9.4 KB

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