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