]>
Commit | Line | Data |
---|---|---|
0ec329da JK |
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 | * | |
6b620ca3 PB |
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. | |
0ec329da JK |
14 | */ |
15 | ||
b6a0aa05 | 16 | #include "qemu/osdep.h" |
0ec329da | 17 | #include "qemu-common.h" |
33c11879 | 18 | #include "cpu.h" |
9a48bcd1 | 19 | #include "qemu/host-utils.h" |
9c17d615 PB |
20 | #include "sysemu/sysemu.h" |
21 | #include "sysemu/kvm.h" | |
0fd7e098 | 22 | #include "kvm_i386.h" |
3b9a6ee5 JK |
23 | #include "hw/sysbus.h" |
24 | #include "hw/kvm/clock.h" | |
0ec329da | 25 | |
0ec329da JK |
26 | #include <linux/kvm.h> |
27 | #include <linux/kvm_para.h> | |
28 | ||
98bdc0d7 HT |
29 | #define TYPE_KVM_CLOCK "kvmclock" |
30 | #define KVM_CLOCK(obj) OBJECT_CHECK(KVMClockState, (obj), TYPE_KVM_CLOCK) | |
31 | ||
0ec329da | 32 | typedef struct KVMClockState { |
98bdc0d7 | 33 | /*< private >*/ |
0ec329da | 34 | SysBusDevice busdev; |
98bdc0d7 HT |
35 | /*< public >*/ |
36 | ||
0ec329da JK |
37 | uint64_t clock; |
38 | bool clock_valid; | |
39 | } KVMClockState; | |
40 | ||
9a48bcd1 AG |
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 | } | |
0ec329da | 83 | |
1dfb4dd9 LC |
84 | static void kvmclock_vm_state_change(void *opaque, int running, |
85 | RunState state) | |
0ec329da JK |
86 | { |
87 | KVMClockState *s = opaque; | |
e76d05c2 | 88 | CPUState *cpu; |
f349c12c EM |
89 | int cap_clock_ctrl = kvm_check_extension(kvm_state, KVM_CAP_KVMCLOCK_CTRL); |
90 | int ret; | |
0ec329da JK |
91 | |
92 | if (running) { | |
5e0b7d88 | 93 | struct kvm_clock_data data = {}; |
9a48bcd1 | 94 | uint64_t time_at_migration = kvmclock_current_nsec(s); |
00f4d64e | 95 | |
0ec329da | 96 | s->clock_valid = false; |
f349c12c | 97 | |
9a48bcd1 AG |
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 | ||
00f4d64e | 103 | data.clock = s->clock; |
00f4d64e MT |
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 | ||
f349c12c EM |
110 | if (!cap_clock_ctrl) { |
111 | return; | |
112 | } | |
bdc44640 | 113 | CPU_FOREACH(cpu) { |
182735ef | 114 | ret = kvm_vcpu_ioctl(cpu, KVM_KVMCLOCK_CTRL, 0); |
f349c12c EM |
115 | if (ret) { |
116 | if (ret != -EINVAL) { | |
117 | fprintf(stderr, "%s: %s\n", __func__, strerror(-ret)); | |
118 | } | |
119 | return; | |
120 | } | |
121 | } | |
00f4d64e MT |
122 | } else { |
123 | struct kvm_clock_data data; | |
124 | int ret; | |
125 | ||
126 | if (s->clock_valid) { | |
127 | return; | |
128 | } | |
317b0a6d | 129 | |
0fd7e098 | 130 | kvm_synchronize_all_tsc(); |
1154d84d | 131 | |
00f4d64e MT |
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; | |
0ec329da JK |
145 | } |
146 | } | |
147 | ||
913bc638 | 148 | static void kvmclock_realize(DeviceState *dev, Error **errp) |
0ec329da | 149 | { |
98bdc0d7 | 150 | KVMClockState *s = KVM_CLOCK(dev); |
0ec329da JK |
151 | |
152 | qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s); | |
0ec329da JK |
153 | } |
154 | ||
155 | static const VMStateDescription kvmclock_vmsd = { | |
156 | .name = "kvmclock", | |
157 | .version_id = 1, | |
158 | .minimum_version_id = 1, | |
0ec329da JK |
159 | .fields = (VMStateField[]) { |
160 | VMSTATE_UINT64(clock, KVMClockState), | |
161 | VMSTATE_END_OF_LIST() | |
162 | } | |
163 | }; | |
164 | ||
999e12bb AL |
165 | static void kvmclock_class_init(ObjectClass *klass, void *data) |
166 | { | |
39bffca2 | 167 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 168 | |
913bc638 | 169 | dc->realize = kvmclock_realize; |
39bffca2 | 170 | dc->vmsd = &kvmclock_vmsd; |
999e12bb AL |
171 | } |
172 | ||
8c43a6f0 | 173 | static const TypeInfo kvmclock_info = { |
98bdc0d7 | 174 | .name = TYPE_KVM_CLOCK, |
39bffca2 AL |
175 | .parent = TYPE_SYS_BUS_DEVICE, |
176 | .instance_size = sizeof(KVMClockState), | |
177 | .class_init = kvmclock_class_init, | |
0ec329da JK |
178 | }; |
179 | ||
180 | /* Note: Must be called after VCPU initialization. */ | |
181 | void kvmclock_create(void) | |
182 | { | |
182735ef AF |
183 | X86CPU *cpu = X86_CPU(first_cpu); |
184 | ||
0ec329da | 185 | if (kvm_enabled() && |
182735ef AF |
186 | cpu->env.features[FEAT_KVM] & ((1ULL << KVM_FEATURE_CLOCKSOURCE) | |
187 | (1ULL << KVM_FEATURE_CLOCKSOURCE2))) { | |
98bdc0d7 | 188 | sysbus_create_simple(TYPE_KVM_CLOCK, -1, NULL); |
0ec329da JK |
189 | } |
190 | } | |
191 | ||
83f7d43a | 192 | static void kvmclock_register_types(void) |
0ec329da | 193 | { |
39bffca2 | 194 | type_register_static(&kvmclock_info); |
0ec329da JK |
195 | } |
196 | ||
83f7d43a | 197 | type_init(kvmclock_register_types) |