xref: /qemu/hw/i386/kvm/clock.c (revision 52ea63de)
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 } KVMClockState;
40 
41 struct pvclock_vcpu_time_info {
42     uint32_t   version;
43     uint32_t   pad0;
44     uint64_t   tsc_timestamp;
45     uint64_t   system_time;
46     uint32_t   tsc_to_system_mul;
47     int8_t     tsc_shift;
48     uint8_t    flags;
49     uint8_t    pad[2];
50 } __attribute__((__packed__)); /* 32 bytes */
51 
52 static uint64_t kvmclock_current_nsec(KVMClockState *s)
53 {
54     CPUState *cpu = first_cpu;
55     CPUX86State *env = cpu->env_ptr;
56     hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
57     uint64_t migration_tsc = env->tsc;
58     struct pvclock_vcpu_time_info time;
59     uint64_t delta;
60     uint64_t nsec_lo;
61     uint64_t nsec_hi;
62     uint64_t nsec;
63 
64     if (!(env->system_time_msr & 1ULL)) {
65         /* KVM clock not active */
66         return 0;
67     }
68 
69     cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
70 
71     assert(time.tsc_timestamp <= migration_tsc);
72     delta = migration_tsc - time.tsc_timestamp;
73     if (time.tsc_shift < 0) {
74         delta >>= -time.tsc_shift;
75     } else {
76         delta <<= time.tsc_shift;
77     }
78 
79     mulu64(&nsec_lo, &nsec_hi, delta, time.tsc_to_system_mul);
80     nsec = (nsec_lo >> 32) | (nsec_hi << 32);
81     return nsec + time.system_time;
82 }
83 
84 static void kvmclock_vm_state_change(void *opaque, int running,
85                                      RunState state)
86 {
87     KVMClockState *s = opaque;
88     CPUState *cpu;
89     int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL);
90     int ret;
91 
92     if (running) {
93         struct kvm_clock_data data = {};
94         uint64_t time_at_migration = kvmclock_current_nsec(s);
95 
96         s->clock_valid = false;
97 
98         /* We can't rely on the migrated clock value, just discard it */
99         if (time_at_migration) {
100             s->clock = time_at_migration;
101         }
102 
103         data.clock = s->clock;
104         ret = kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
105         if (ret < 0) {
106             fprintf(stderr, "KVM_SET_CLOCK failed: %s\n", strerror(ret));
107             abort();
108         }
109 
110         if (!cap_clock_ctrl) {
111             return;
112         }
113         CPU_FOREACH(cpu) {
114             ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0);
115             if (ret) {
116                 if (ret != -EINVAL) {
117                     fprintf(stderr, "%s: %s\n", __func__, strerror(-ret));
118                 }
119                 return;
120             }
121         }
122     } else {
123         struct kvm_clock_data data;
124         int ret;
125 
126         if (s->clock_valid) {
127             return;
128         }
129 
130         kvm_synchronize_all_tsc();
131 
132         ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
133         if (ret < 0) {
134             fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
135             abort();
136         }
137         s->clock = data.clock;
138 
139         /*
140          * If the VM is stopped, declare the clock state valid to
141          * avoid re-reading it on next vmsave (which would return
142          * a different value). Will be reset when the VM is continued.
143          */
144         s->clock_valid = true;
145     }
146 }
147 
148 static void kvmclock_realize(DeviceState *dev, Error **errp)
149 {
150     KVMClockState *s = KVM_CLOCK(dev);
151 
152     qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
153 }
154 
155 static const VMStateDescription kvmclock_vmsd = {
156     .name = "kvmclock",
157     .version_id = 1,
158     .minimum_version_id = 1,
159     .fields = (VMStateField[]) {
160         VMSTATE_UINT64(clock, KVMClockState),
161         VMSTATE_END_OF_LIST()
162     }
163 };
164 
165 static void kvmclock_class_init(ObjectClass *klass, void *data)
166 {
167     DeviceClass *dc = DEVICE_CLASS(klass);
168 
169     dc->realize = kvmclock_realize;
170     dc->vmsd = &kvmclock_vmsd;
171 }
172 
173 static const TypeInfo kvmclock_info = {
174     .name          = TYPE_KVM_CLOCK,
175     .parent        = TYPE_SYS_BUS_DEVICE,
176     .instance_size = sizeof(KVMClockState),
177     .class_init    = kvmclock_class_init,
178 };
179 
180 /* Note: Must be called after VCPU initialization. */
181 void kvmclock_create(void)
182 {
183     X86CPU *cpu = X86_CPU(first_cpu);
184 
185     if (kvm_enabled() &&
186         cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
187                                        (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
188         sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL);
189     }
190 }
191 
192 static void kvmclock_register_types(void)
193 {
194     type_register_static(&kvmclock_info);
195 }
196 
197 type_init(kvmclock_register_types)
198