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