root / target-i386 / kvm.c @ 0e607a80
History | View | Annotate | Download (26.7 kB)
1 | 05330448 | aliguori | /*
|
---|---|---|---|
2 | 05330448 | aliguori | * QEMU KVM support
|
3 | 05330448 | aliguori | *
|
4 | 05330448 | aliguori | * Copyright (C) 2006-2008 Qumranet Technologies
|
5 | 05330448 | aliguori | * Copyright IBM, Corp. 2008
|
6 | 05330448 | aliguori | *
|
7 | 05330448 | aliguori | * Authors:
|
8 | 05330448 | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
9 | 05330448 | aliguori | *
|
10 | 05330448 | aliguori | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
11 | 05330448 | aliguori | * See the COPYING file in the top-level directory.
|
12 | 05330448 | aliguori | *
|
13 | 05330448 | aliguori | */
|
14 | 05330448 | aliguori | |
15 | 05330448 | aliguori | #include <sys/types.h> |
16 | 05330448 | aliguori | #include <sys/ioctl.h> |
17 | 05330448 | aliguori | #include <sys/mman.h> |
18 | 05330448 | aliguori | |
19 | 05330448 | aliguori | #include <linux/kvm.h> |
20 | 05330448 | aliguori | |
21 | 05330448 | aliguori | #include "qemu-common.h" |
22 | 05330448 | aliguori | #include "sysemu.h" |
23 | 05330448 | aliguori | #include "kvm.h" |
24 | 05330448 | aliguori | #include "cpu.h" |
25 | e22a25c9 | aliguori | #include "gdbstub.h" |
26 | 0e607a80 | Jan Kiszka | #include "host-utils.h" |
27 | 05330448 | aliguori | |
28 | 05330448 | aliguori | //#define DEBUG_KVM
|
29 | 05330448 | aliguori | |
30 | 05330448 | aliguori | #ifdef DEBUG_KVM
|
31 | 05330448 | aliguori | #define dprintf(fmt, ...) \
|
32 | 05330448 | aliguori | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
33 | 05330448 | aliguori | #else
|
34 | 05330448 | aliguori | #define dprintf(fmt, ...) \
|
35 | 05330448 | aliguori | do { } while (0) |
36 | 05330448 | aliguori | #endif
|
37 | 05330448 | aliguori | |
38 | b827df58 | Avi Kivity | #ifdef KVM_CAP_EXT_CPUID
|
39 | b827df58 | Avi Kivity | |
40 | b827df58 | Avi Kivity | static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max) |
41 | b827df58 | Avi Kivity | { |
42 | b827df58 | Avi Kivity | struct kvm_cpuid2 *cpuid;
|
43 | b827df58 | Avi Kivity | int r, size;
|
44 | b827df58 | Avi Kivity | |
45 | b827df58 | Avi Kivity | size = sizeof(*cpuid) + max * sizeof(*cpuid->entries); |
46 | b827df58 | Avi Kivity | cpuid = (struct kvm_cpuid2 *)qemu_mallocz(size);
|
47 | b827df58 | Avi Kivity | cpuid->nent = max; |
48 | b827df58 | Avi Kivity | r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid); |
49 | 76ae317f | Mark McLoughlin | if (r == 0 && cpuid->nent >= max) { |
50 | 76ae317f | Mark McLoughlin | r = -E2BIG; |
51 | 76ae317f | Mark McLoughlin | } |
52 | b827df58 | Avi Kivity | if (r < 0) { |
53 | b827df58 | Avi Kivity | if (r == -E2BIG) {
|
54 | b827df58 | Avi Kivity | qemu_free(cpuid); |
55 | b827df58 | Avi Kivity | return NULL; |
56 | b827df58 | Avi Kivity | } else {
|
57 | b827df58 | Avi Kivity | fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
|
58 | b827df58 | Avi Kivity | strerror(-r)); |
59 | b827df58 | Avi Kivity | exit(1);
|
60 | b827df58 | Avi Kivity | } |
61 | b827df58 | Avi Kivity | } |
62 | b827df58 | Avi Kivity | return cpuid;
|
63 | b827df58 | Avi Kivity | } |
64 | b827df58 | Avi Kivity | |
65 | b827df58 | Avi Kivity | uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function, int reg)
|
66 | b827df58 | Avi Kivity | { |
67 | b827df58 | Avi Kivity | struct kvm_cpuid2 *cpuid;
|
68 | b827df58 | Avi Kivity | int i, max;
|
69 | b827df58 | Avi Kivity | uint32_t ret = 0;
|
70 | b827df58 | Avi Kivity | uint32_t cpuid_1_edx; |
71 | b827df58 | Avi Kivity | |
72 | b827df58 | Avi Kivity | if (!kvm_check_extension(env->kvm_state, KVM_CAP_EXT_CPUID)) {
|
73 | b827df58 | Avi Kivity | return -1U; |
74 | b827df58 | Avi Kivity | } |
75 | b827df58 | Avi Kivity | |
76 | b827df58 | Avi Kivity | max = 1;
|
77 | b827df58 | Avi Kivity | while ((cpuid = try_get_cpuid(env->kvm_state, max)) == NULL) { |
78 | b827df58 | Avi Kivity | max *= 2;
|
79 | b827df58 | Avi Kivity | } |
80 | b827df58 | Avi Kivity | |
81 | b827df58 | Avi Kivity | for (i = 0; i < cpuid->nent; ++i) { |
82 | b827df58 | Avi Kivity | if (cpuid->entries[i].function == function) {
|
83 | b827df58 | Avi Kivity | switch (reg) {
|
84 | b827df58 | Avi Kivity | case R_EAX:
|
85 | b827df58 | Avi Kivity | ret = cpuid->entries[i].eax; |
86 | b827df58 | Avi Kivity | break;
|
87 | b827df58 | Avi Kivity | case R_EBX:
|
88 | b827df58 | Avi Kivity | ret = cpuid->entries[i].ebx; |
89 | b827df58 | Avi Kivity | break;
|
90 | b827df58 | Avi Kivity | case R_ECX:
|
91 | b827df58 | Avi Kivity | ret = cpuid->entries[i].ecx; |
92 | b827df58 | Avi Kivity | break;
|
93 | b827df58 | Avi Kivity | case R_EDX:
|
94 | b827df58 | Avi Kivity | ret = cpuid->entries[i].edx; |
95 | b827df58 | Avi Kivity | if (function == 0x80000001) { |
96 | b827df58 | Avi Kivity | /* On Intel, kvm returns cpuid according to the Intel spec,
|
97 | b827df58 | Avi Kivity | * so add missing bits according to the AMD spec:
|
98 | b827df58 | Avi Kivity | */
|
99 | b827df58 | Avi Kivity | cpuid_1_edx = kvm_arch_get_supported_cpuid(env, 1, R_EDX);
|
100 | b827df58 | Avi Kivity | ret |= cpuid_1_edx & 0xdfeff7ff;
|
101 | b827df58 | Avi Kivity | } |
102 | b827df58 | Avi Kivity | break;
|
103 | b827df58 | Avi Kivity | } |
104 | b827df58 | Avi Kivity | } |
105 | b827df58 | Avi Kivity | } |
106 | b827df58 | Avi Kivity | |
107 | b827df58 | Avi Kivity | qemu_free(cpuid); |
108 | b827df58 | Avi Kivity | |
109 | b827df58 | Avi Kivity | return ret;
|
110 | b827df58 | Avi Kivity | } |
111 | b827df58 | Avi Kivity | |
112 | b827df58 | Avi Kivity | #else
|
113 | b827df58 | Avi Kivity | |
114 | b827df58 | Avi Kivity | uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function, int reg)
|
115 | b827df58 | Avi Kivity | { |
116 | b827df58 | Avi Kivity | return -1U; |
117 | b827df58 | Avi Kivity | } |
118 | b827df58 | Avi Kivity | |
119 | b827df58 | Avi Kivity | #endif
|
120 | b827df58 | Avi Kivity | |
121 | 6c1f42fe | Andre Przywara | static void kvm_trim_features(uint32_t *features, uint32_t supported) |
122 | 6c1f42fe | Andre Przywara | { |
123 | 6c1f42fe | Andre Przywara | int i;
|
124 | 6c1f42fe | Andre Przywara | uint32_t mask; |
125 | 6c1f42fe | Andre Przywara | |
126 | 6c1f42fe | Andre Przywara | for (i = 0; i < 32; ++i) { |
127 | 6c1f42fe | Andre Przywara | mask = 1U << i;
|
128 | 6c1f42fe | Andre Przywara | if ((*features & mask) && !(supported & mask)) {
|
129 | 6c1f42fe | Andre Przywara | *features &= ~mask; |
130 | 6c1f42fe | Andre Przywara | } |
131 | 6c1f42fe | Andre Przywara | } |
132 | 6c1f42fe | Andre Przywara | } |
133 | 6c1f42fe | Andre Przywara | |
134 | 05330448 | aliguori | int kvm_arch_init_vcpu(CPUState *env)
|
135 | 05330448 | aliguori | { |
136 | 05330448 | aliguori | struct {
|
137 | 486bd5a2 | aliguori | struct kvm_cpuid2 cpuid;
|
138 | 486bd5a2 | aliguori | struct kvm_cpuid_entry2 entries[100]; |
139 | 05330448 | aliguori | } __attribute__((packed)) cpuid_data; |
140 | 486bd5a2 | aliguori | uint32_t limit, i, j, cpuid_i; |
141 | a33609ca | aliguori | uint32_t unused; |
142 | 05330448 | aliguori | |
143 | f8d926e9 | Jan Kiszka | env->mp_state = KVM_MP_STATE_RUNNABLE; |
144 | f8d926e9 | Jan Kiszka | |
145 | 6c1f42fe | Andre Przywara | kvm_trim_features(&env->cpuid_features, |
146 | 6c1f42fe | Andre Przywara | kvm_arch_get_supported_cpuid(env, 1, R_EDX));
|
147 | 6c0d7ee8 | Andre Przywara | |
148 | 6c0d7ee8 | Andre Przywara | i = env->cpuid_ext_features & CPUID_EXT_HYPERVISOR; |
149 | 6c1f42fe | Andre Przywara | kvm_trim_features(&env->cpuid_ext_features, |
150 | 6c1f42fe | Andre Przywara | kvm_arch_get_supported_cpuid(env, 1, R_ECX));
|
151 | 6c0d7ee8 | Andre Przywara | env->cpuid_ext_features |= i; |
152 | 6c0d7ee8 | Andre Przywara | |
153 | 6c1f42fe | Andre Przywara | kvm_trim_features(&env->cpuid_ext2_features, |
154 | 6c1f42fe | Andre Przywara | kvm_arch_get_supported_cpuid(env, 0x80000001, R_EDX));
|
155 | 6c1f42fe | Andre Przywara | kvm_trim_features(&env->cpuid_ext3_features, |
156 | 6c1f42fe | Andre Przywara | kvm_arch_get_supported_cpuid(env, 0x80000001, R_ECX));
|
157 | 6c1f42fe | Andre Przywara | |
158 | 05330448 | aliguori | cpuid_i = 0;
|
159 | 05330448 | aliguori | |
160 | a33609ca | aliguori | cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused); |
161 | 05330448 | aliguori | |
162 | 05330448 | aliguori | for (i = 0; i <= limit; i++) { |
163 | 486bd5a2 | aliguori | struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
|
164 | 486bd5a2 | aliguori | |
165 | 486bd5a2 | aliguori | switch (i) {
|
166 | a36b1029 | aliguori | case 2: { |
167 | a36b1029 | aliguori | /* Keep reading function 2 till all the input is received */
|
168 | a36b1029 | aliguori | int times;
|
169 | a36b1029 | aliguori | |
170 | a36b1029 | aliguori | c->function = i; |
171 | a33609ca | aliguori | c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC | |
172 | a33609ca | aliguori | KVM_CPUID_FLAG_STATE_READ_NEXT; |
173 | a33609ca | aliguori | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
|
174 | a33609ca | aliguori | times = c->eax & 0xff;
|
175 | a36b1029 | aliguori | |
176 | a36b1029 | aliguori | for (j = 1; j < times; ++j) { |
177 | a33609ca | aliguori | c = &cpuid_data.entries[cpuid_i++]; |
178 | a36b1029 | aliguori | c->function = i; |
179 | a33609ca | aliguori | c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC; |
180 | a33609ca | aliguori | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
|
181 | a36b1029 | aliguori | } |
182 | a36b1029 | aliguori | break;
|
183 | a36b1029 | aliguori | } |
184 | 486bd5a2 | aliguori | case 4: |
185 | 486bd5a2 | aliguori | case 0xb: |
186 | 486bd5a2 | aliguori | case 0xd: |
187 | 486bd5a2 | aliguori | for (j = 0; ; j++) { |
188 | 486bd5a2 | aliguori | c->function = i; |
189 | 486bd5a2 | aliguori | c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX; |
190 | 486bd5a2 | aliguori | c->index = j; |
191 | a33609ca | aliguori | cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx); |
192 | 486bd5a2 | aliguori | |
193 | a33609ca | aliguori | if (i == 4 && c->eax == 0) |
194 | 486bd5a2 | aliguori | break;
|
195 | a33609ca | aliguori | if (i == 0xb && !(c->ecx & 0xff00)) |
196 | 486bd5a2 | aliguori | break;
|
197 | a33609ca | aliguori | if (i == 0xd && c->eax == 0) |
198 | 486bd5a2 | aliguori | break;
|
199 | a33609ca | aliguori | |
200 | a33609ca | aliguori | c = &cpuid_data.entries[cpuid_i++]; |
201 | 486bd5a2 | aliguori | } |
202 | 486bd5a2 | aliguori | break;
|
203 | 486bd5a2 | aliguori | default:
|
204 | 486bd5a2 | aliguori | c->function = i; |
205 | a33609ca | aliguori | c->flags = 0;
|
206 | a33609ca | aliguori | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
|
207 | 486bd5a2 | aliguori | break;
|
208 | 486bd5a2 | aliguori | } |
209 | 05330448 | aliguori | } |
210 | a33609ca | aliguori | cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused); |
211 | 05330448 | aliguori | |
212 | 05330448 | aliguori | for (i = 0x80000000; i <= limit; i++) { |
213 | 486bd5a2 | aliguori | struct kvm_cpuid_entry2 *c = &cpuid_data.entries[cpuid_i++];
|
214 | 05330448 | aliguori | |
215 | 05330448 | aliguori | c->function = i; |
216 | a33609ca | aliguori | c->flags = 0;
|
217 | a33609ca | aliguori | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
|
218 | 05330448 | aliguori | } |
219 | 05330448 | aliguori | |
220 | 05330448 | aliguori | cpuid_data.cpuid.nent = cpuid_i; |
221 | 05330448 | aliguori | |
222 | 486bd5a2 | aliguori | return kvm_vcpu_ioctl(env, KVM_SET_CPUID2, &cpuid_data);
|
223 | 05330448 | aliguori | } |
224 | 05330448 | aliguori | |
225 | caa5af0f | Jan Kiszka | void kvm_arch_reset_vcpu(CPUState *env)
|
226 | caa5af0f | Jan Kiszka | { |
227 | 0e607a80 | Jan Kiszka | env->interrupt_injected = -1;
|
228 | caa5af0f | Jan Kiszka | } |
229 | caa5af0f | Jan Kiszka | |
230 | 05330448 | aliguori | static int kvm_has_msr_star(CPUState *env) |
231 | 05330448 | aliguori | { |
232 | 05330448 | aliguori | static int has_msr_star; |
233 | 05330448 | aliguori | int ret;
|
234 | 05330448 | aliguori | |
235 | 05330448 | aliguori | /* first time */
|
236 | 05330448 | aliguori | if (has_msr_star == 0) { |
237 | 05330448 | aliguori | struct kvm_msr_list msr_list, *kvm_msr_list;
|
238 | 05330448 | aliguori | |
239 | 05330448 | aliguori | has_msr_star = -1;
|
240 | 05330448 | aliguori | |
241 | 05330448 | aliguori | /* Obtain MSR list from KVM. These are the MSRs that we must
|
242 | 05330448 | aliguori | * save/restore */
|
243 | 4c9f7372 | aliguori | msr_list.nmsrs = 0;
|
244 | 05330448 | aliguori | ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, &msr_list); |
245 | 05330448 | aliguori | if (ret < 0) |
246 | 05330448 | aliguori | return 0; |
247 | 05330448 | aliguori | |
248 | d9db889f | Jan Kiszka | /* Old kernel modules had a bug and could write beyond the provided
|
249 | d9db889f | Jan Kiszka | memory. Allocate at least a safe amount of 1K. */
|
250 | d9db889f | Jan Kiszka | kvm_msr_list = qemu_mallocz(MAX(1024, sizeof(msr_list) + |
251 | d9db889f | Jan Kiszka | msr_list.nmsrs * |
252 | d9db889f | Jan Kiszka | sizeof(msr_list.indices[0]))); |
253 | 05330448 | aliguori | |
254 | 55308450 | aliguori | kvm_msr_list->nmsrs = msr_list.nmsrs; |
255 | 05330448 | aliguori | ret = kvm_ioctl(env->kvm_state, KVM_GET_MSR_INDEX_LIST, kvm_msr_list); |
256 | 05330448 | aliguori | if (ret >= 0) { |
257 | 05330448 | aliguori | int i;
|
258 | 05330448 | aliguori | |
259 | 05330448 | aliguori | for (i = 0; i < kvm_msr_list->nmsrs; i++) { |
260 | 05330448 | aliguori | if (kvm_msr_list->indices[i] == MSR_STAR) {
|
261 | 05330448 | aliguori | has_msr_star = 1;
|
262 | 05330448 | aliguori | break;
|
263 | 05330448 | aliguori | } |
264 | 05330448 | aliguori | } |
265 | 05330448 | aliguori | } |
266 | 05330448 | aliguori | |
267 | 05330448 | aliguori | free(kvm_msr_list); |
268 | 05330448 | aliguori | } |
269 | 05330448 | aliguori | |
270 | 05330448 | aliguori | if (has_msr_star == 1) |
271 | 05330448 | aliguori | return 1; |
272 | 05330448 | aliguori | return 0; |
273 | 05330448 | aliguori | } |
274 | 05330448 | aliguori | |
275 | 05330448 | aliguori | int kvm_arch_init(KVMState *s, int smp_cpus) |
276 | 05330448 | aliguori | { |
277 | 05330448 | aliguori | int ret;
|
278 | 05330448 | aliguori | |
279 | 05330448 | aliguori | /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
|
280 | 05330448 | aliguori | * directly. In order to use vm86 mode, a TSS is needed. Since this
|
281 | 05330448 | aliguori | * must be part of guest physical memory, we need to allocate it. Older
|
282 | 05330448 | aliguori | * versions of KVM just assumed that it would be at the end of physical
|
283 | 05330448 | aliguori | * memory but that doesn't work with more than 4GB of memory. We simply
|
284 | 05330448 | aliguori | * refuse to work with those older versions of KVM. */
|
285 | 984b5181 | aliguori | ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR); |
286 | 05330448 | aliguori | if (ret <= 0) { |
287 | 05330448 | aliguori | fprintf(stderr, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
|
288 | 05330448 | aliguori | return ret;
|
289 | 05330448 | aliguori | } |
290 | 05330448 | aliguori | |
291 | 05330448 | aliguori | /* this address is 3 pages before the bios, and the bios should present
|
292 | 05330448 | aliguori | * as unavaible memory. FIXME, need to ensure the e820 map deals with
|
293 | 05330448 | aliguori | * this?
|
294 | 05330448 | aliguori | */
|
295 | 984b5181 | aliguori | return kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, 0xfffbd000); |
296 | 05330448 | aliguori | } |
297 | 05330448 | aliguori | |
298 | 05330448 | aliguori | static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs) |
299 | 05330448 | aliguori | { |
300 | 05330448 | aliguori | lhs->selector = rhs->selector; |
301 | 05330448 | aliguori | lhs->base = rhs->base; |
302 | 05330448 | aliguori | lhs->limit = rhs->limit; |
303 | 05330448 | aliguori | lhs->type = 3;
|
304 | 05330448 | aliguori | lhs->present = 1;
|
305 | 05330448 | aliguori | lhs->dpl = 3;
|
306 | 05330448 | aliguori | lhs->db = 0;
|
307 | 05330448 | aliguori | lhs->s = 1;
|
308 | 05330448 | aliguori | lhs->l = 0;
|
309 | 05330448 | aliguori | lhs->g = 0;
|
310 | 05330448 | aliguori | lhs->avl = 0;
|
311 | 05330448 | aliguori | lhs->unusable = 0;
|
312 | 05330448 | aliguori | } |
313 | 05330448 | aliguori | |
314 | 05330448 | aliguori | static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs) |
315 | 05330448 | aliguori | { |
316 | 05330448 | aliguori | unsigned flags = rhs->flags;
|
317 | 05330448 | aliguori | lhs->selector = rhs->selector; |
318 | 05330448 | aliguori | lhs->base = rhs->base; |
319 | 05330448 | aliguori | lhs->limit = rhs->limit; |
320 | 05330448 | aliguori | lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
|
321 | 05330448 | aliguori | lhs->present = (flags & DESC_P_MASK) != 0;
|
322 | 05330448 | aliguori | lhs->dpl = rhs->selector & 3;
|
323 | 05330448 | aliguori | lhs->db = (flags >> DESC_B_SHIFT) & 1;
|
324 | 05330448 | aliguori | lhs->s = (flags & DESC_S_MASK) != 0;
|
325 | 05330448 | aliguori | lhs->l = (flags >> DESC_L_SHIFT) & 1;
|
326 | 05330448 | aliguori | lhs->g = (flags & DESC_G_MASK) != 0;
|
327 | 05330448 | aliguori | lhs->avl = (flags & DESC_AVL_MASK) != 0;
|
328 | 05330448 | aliguori | lhs->unusable = 0;
|
329 | 05330448 | aliguori | } |
330 | 05330448 | aliguori | |
331 | 05330448 | aliguori | static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs) |
332 | 05330448 | aliguori | { |
333 | 05330448 | aliguori | lhs->selector = rhs->selector; |
334 | 05330448 | aliguori | lhs->base = rhs->base; |
335 | 05330448 | aliguori | lhs->limit = rhs->limit; |
336 | 05330448 | aliguori | lhs->flags = |
337 | 05330448 | aliguori | (rhs->type << DESC_TYPE_SHIFT) |
338 | 05330448 | aliguori | | (rhs->present * DESC_P_MASK) |
339 | 05330448 | aliguori | | (rhs->dpl << DESC_DPL_SHIFT) |
340 | 05330448 | aliguori | | (rhs->db << DESC_B_SHIFT) |
341 | 05330448 | aliguori | | (rhs->s * DESC_S_MASK) |
342 | 05330448 | aliguori | | (rhs->l << DESC_L_SHIFT) |
343 | 05330448 | aliguori | | (rhs->g * DESC_G_MASK) |
344 | 05330448 | aliguori | | (rhs->avl * DESC_AVL_MASK); |
345 | 05330448 | aliguori | } |
346 | 05330448 | aliguori | |
347 | 05330448 | aliguori | static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set) |
348 | 05330448 | aliguori | { |
349 | 05330448 | aliguori | if (set)
|
350 | 05330448 | aliguori | *kvm_reg = *qemu_reg; |
351 | 05330448 | aliguori | else
|
352 | 05330448 | aliguori | *qemu_reg = *kvm_reg; |
353 | 05330448 | aliguori | } |
354 | 05330448 | aliguori | |
355 | 05330448 | aliguori | static int kvm_getput_regs(CPUState *env, int set) |
356 | 05330448 | aliguori | { |
357 | 05330448 | aliguori | struct kvm_regs regs;
|
358 | 05330448 | aliguori | int ret = 0; |
359 | 05330448 | aliguori | |
360 | 05330448 | aliguori | if (!set) {
|
361 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, ®s); |
362 | 05330448 | aliguori | if (ret < 0) |
363 | 05330448 | aliguori | return ret;
|
364 | 05330448 | aliguori | } |
365 | 05330448 | aliguori | |
366 | 05330448 | aliguori | kvm_getput_reg(®s.rax, &env->regs[R_EAX], set); |
367 | 05330448 | aliguori | kvm_getput_reg(®s.rbx, &env->regs[R_EBX], set); |
368 | 05330448 | aliguori | kvm_getput_reg(®s.rcx, &env->regs[R_ECX], set); |
369 | 05330448 | aliguori | kvm_getput_reg(®s.rdx, &env->regs[R_EDX], set); |
370 | 05330448 | aliguori | kvm_getput_reg(®s.rsi, &env->regs[R_ESI], set); |
371 | 05330448 | aliguori | kvm_getput_reg(®s.rdi, &env->regs[R_EDI], set); |
372 | 05330448 | aliguori | kvm_getput_reg(®s.rsp, &env->regs[R_ESP], set); |
373 | 05330448 | aliguori | kvm_getput_reg(®s.rbp, &env->regs[R_EBP], set); |
374 | 05330448 | aliguori | #ifdef TARGET_X86_64
|
375 | 05330448 | aliguori | kvm_getput_reg(®s.r8, &env->regs[8], set);
|
376 | 05330448 | aliguori | kvm_getput_reg(®s.r9, &env->regs[9], set);
|
377 | 05330448 | aliguori | kvm_getput_reg(®s.r10, &env->regs[10], set);
|
378 | 05330448 | aliguori | kvm_getput_reg(®s.r11, &env->regs[11], set);
|
379 | 05330448 | aliguori | kvm_getput_reg(®s.r12, &env->regs[12], set);
|
380 | 05330448 | aliguori | kvm_getput_reg(®s.r13, &env->regs[13], set);
|
381 | 05330448 | aliguori | kvm_getput_reg(®s.r14, &env->regs[14], set);
|
382 | 05330448 | aliguori | kvm_getput_reg(®s.r15, &env->regs[15], set);
|
383 | 05330448 | aliguori | #endif
|
384 | 05330448 | aliguori | |
385 | 05330448 | aliguori | kvm_getput_reg(®s.rflags, &env->eflags, set); |
386 | 05330448 | aliguori | kvm_getput_reg(®s.rip, &env->eip, set); |
387 | 05330448 | aliguori | |
388 | 05330448 | aliguori | if (set)
|
389 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, ®s); |
390 | 05330448 | aliguori | |
391 | 05330448 | aliguori | return ret;
|
392 | 05330448 | aliguori | } |
393 | 05330448 | aliguori | |
394 | 05330448 | aliguori | static int kvm_put_fpu(CPUState *env) |
395 | 05330448 | aliguori | { |
396 | 05330448 | aliguori | struct kvm_fpu fpu;
|
397 | 05330448 | aliguori | int i;
|
398 | 05330448 | aliguori | |
399 | 05330448 | aliguori | memset(&fpu, 0, sizeof fpu); |
400 | 05330448 | aliguori | fpu.fsw = env->fpus & ~(7 << 11); |
401 | 05330448 | aliguori | fpu.fsw |= (env->fpstt & 7) << 11; |
402 | 05330448 | aliguori | fpu.fcw = env->fpuc; |
403 | 05330448 | aliguori | for (i = 0; i < 8; ++i) |
404 | 05330448 | aliguori | fpu.ftwx |= (!env->fptags[i]) << i; |
405 | 05330448 | aliguori | memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
|
406 | 05330448 | aliguori | memcpy(fpu.xmm, env->xmm_regs, sizeof env->xmm_regs);
|
407 | 05330448 | aliguori | fpu.mxcsr = env->mxcsr; |
408 | 05330448 | aliguori | |
409 | 05330448 | aliguori | return kvm_vcpu_ioctl(env, KVM_SET_FPU, &fpu);
|
410 | 05330448 | aliguori | } |
411 | 05330448 | aliguori | |
412 | 05330448 | aliguori | static int kvm_put_sregs(CPUState *env) |
413 | 05330448 | aliguori | { |
414 | 05330448 | aliguori | struct kvm_sregs sregs;
|
415 | 05330448 | aliguori | |
416 | 0e607a80 | Jan Kiszka | memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap)); |
417 | 0e607a80 | Jan Kiszka | if (env->interrupt_injected >= 0) { |
418 | 0e607a80 | Jan Kiszka | sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
|
419 | 0e607a80 | Jan Kiszka | (uint64_t)1 << (env->interrupt_injected % 64); |
420 | 0e607a80 | Jan Kiszka | } |
421 | 05330448 | aliguori | |
422 | 05330448 | aliguori | if ((env->eflags & VM_MASK)) {
|
423 | 05330448 | aliguori | set_v8086_seg(&sregs.cs, &env->segs[R_CS]); |
424 | 05330448 | aliguori | set_v8086_seg(&sregs.ds, &env->segs[R_DS]); |
425 | 05330448 | aliguori | set_v8086_seg(&sregs.es, &env->segs[R_ES]); |
426 | 05330448 | aliguori | set_v8086_seg(&sregs.fs, &env->segs[R_FS]); |
427 | 05330448 | aliguori | set_v8086_seg(&sregs.gs, &env->segs[R_GS]); |
428 | 05330448 | aliguori | set_v8086_seg(&sregs.ss, &env->segs[R_SS]); |
429 | 05330448 | aliguori | } else {
|
430 | 05330448 | aliguori | set_seg(&sregs.cs, &env->segs[R_CS]); |
431 | 05330448 | aliguori | set_seg(&sregs.ds, &env->segs[R_DS]); |
432 | 05330448 | aliguori | set_seg(&sregs.es, &env->segs[R_ES]); |
433 | 05330448 | aliguori | set_seg(&sregs.fs, &env->segs[R_FS]); |
434 | 05330448 | aliguori | set_seg(&sregs.gs, &env->segs[R_GS]); |
435 | 05330448 | aliguori | set_seg(&sregs.ss, &env->segs[R_SS]); |
436 | 05330448 | aliguori | |
437 | 05330448 | aliguori | if (env->cr[0] & CR0_PE_MASK) { |
438 | 05330448 | aliguori | /* force ss cpl to cs cpl */
|
439 | 05330448 | aliguori | sregs.ss.selector = (sregs.ss.selector & ~3) |
|
440 | 05330448 | aliguori | (sregs.cs.selector & 3);
|
441 | 05330448 | aliguori | sregs.ss.dpl = sregs.ss.selector & 3;
|
442 | 05330448 | aliguori | } |
443 | 05330448 | aliguori | } |
444 | 05330448 | aliguori | |
445 | 05330448 | aliguori | set_seg(&sregs.tr, &env->tr); |
446 | 05330448 | aliguori | set_seg(&sregs.ldt, &env->ldt); |
447 | 05330448 | aliguori | |
448 | 05330448 | aliguori | sregs.idt.limit = env->idt.limit; |
449 | 05330448 | aliguori | sregs.idt.base = env->idt.base; |
450 | 05330448 | aliguori | sregs.gdt.limit = env->gdt.limit; |
451 | 05330448 | aliguori | sregs.gdt.base = env->gdt.base; |
452 | 05330448 | aliguori | |
453 | 05330448 | aliguori | sregs.cr0 = env->cr[0];
|
454 | 05330448 | aliguori | sregs.cr2 = env->cr[2];
|
455 | 05330448 | aliguori | sregs.cr3 = env->cr[3];
|
456 | 05330448 | aliguori | sregs.cr4 = env->cr[4];
|
457 | 05330448 | aliguori | |
458 | 05330448 | aliguori | sregs.cr8 = cpu_get_apic_tpr(env); |
459 | 05330448 | aliguori | sregs.apic_base = cpu_get_apic_base(env); |
460 | 05330448 | aliguori | |
461 | 05330448 | aliguori | sregs.efer = env->efer; |
462 | 05330448 | aliguori | |
463 | 05330448 | aliguori | return kvm_vcpu_ioctl(env, KVM_SET_SREGS, &sregs);
|
464 | 05330448 | aliguori | } |
465 | 05330448 | aliguori | |
466 | 05330448 | aliguori | static void kvm_msr_entry_set(struct kvm_msr_entry *entry, |
467 | 05330448 | aliguori | uint32_t index, uint64_t value) |
468 | 05330448 | aliguori | { |
469 | 05330448 | aliguori | entry->index = index; |
470 | 05330448 | aliguori | entry->data = value; |
471 | 05330448 | aliguori | } |
472 | 05330448 | aliguori | |
473 | 05330448 | aliguori | static int kvm_put_msrs(CPUState *env) |
474 | 05330448 | aliguori | { |
475 | 05330448 | aliguori | struct {
|
476 | 05330448 | aliguori | struct kvm_msrs info;
|
477 | 05330448 | aliguori | struct kvm_msr_entry entries[100]; |
478 | 05330448 | aliguori | } msr_data; |
479 | 05330448 | aliguori | struct kvm_msr_entry *msrs = msr_data.entries;
|
480 | 05330448 | aliguori | int n = 0; |
481 | 05330448 | aliguori | |
482 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs); |
483 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp); |
484 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip); |
485 | 05330448 | aliguori | if (kvm_has_msr_star(env))
|
486 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star); |
487 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc); |
488 | 05330448 | aliguori | #ifdef TARGET_X86_64
|
489 | 05330448 | aliguori | /* FIXME if lm capable */
|
490 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar); |
491 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase); |
492 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask); |
493 | 05330448 | aliguori | kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar); |
494 | 05330448 | aliguori | #endif
|
495 | 05330448 | aliguori | msr_data.info.nmsrs = n; |
496 | 05330448 | aliguori | |
497 | 05330448 | aliguori | return kvm_vcpu_ioctl(env, KVM_SET_MSRS, &msr_data);
|
498 | 05330448 | aliguori | |
499 | 05330448 | aliguori | } |
500 | 05330448 | aliguori | |
501 | 05330448 | aliguori | |
502 | 05330448 | aliguori | static int kvm_get_fpu(CPUState *env) |
503 | 05330448 | aliguori | { |
504 | 05330448 | aliguori | struct kvm_fpu fpu;
|
505 | 05330448 | aliguori | int i, ret;
|
506 | 05330448 | aliguori | |
507 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_GET_FPU, &fpu); |
508 | 05330448 | aliguori | if (ret < 0) |
509 | 05330448 | aliguori | return ret;
|
510 | 05330448 | aliguori | |
511 | 05330448 | aliguori | env->fpstt = (fpu.fsw >> 11) & 7; |
512 | 05330448 | aliguori | env->fpus = fpu.fsw; |
513 | 05330448 | aliguori | env->fpuc = fpu.fcw; |
514 | 05330448 | aliguori | for (i = 0; i < 8; ++i) |
515 | 05330448 | aliguori | env->fptags[i] = !((fpu.ftwx >> i) & 1);
|
516 | 05330448 | aliguori | memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
|
517 | 05330448 | aliguori | memcpy(env->xmm_regs, fpu.xmm, sizeof env->xmm_regs);
|
518 | 05330448 | aliguori | env->mxcsr = fpu.mxcsr; |
519 | 05330448 | aliguori | |
520 | 05330448 | aliguori | return 0; |
521 | 05330448 | aliguori | } |
522 | 05330448 | aliguori | |
523 | 05330448 | aliguori | static int kvm_get_sregs(CPUState *env) |
524 | 05330448 | aliguori | { |
525 | 05330448 | aliguori | struct kvm_sregs sregs;
|
526 | 05330448 | aliguori | uint32_t hflags; |
527 | 0e607a80 | Jan Kiszka | int bit, i, ret;
|
528 | 05330448 | aliguori | |
529 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_GET_SREGS, &sregs); |
530 | 05330448 | aliguori | if (ret < 0) |
531 | 05330448 | aliguori | return ret;
|
532 | 05330448 | aliguori | |
533 | 0e607a80 | Jan Kiszka | /* There can only be one pending IRQ set in the bitmap at a time, so try
|
534 | 0e607a80 | Jan Kiszka | to find it and save its number instead (-1 for none). */
|
535 | 0e607a80 | Jan Kiszka | env->interrupt_injected = -1;
|
536 | 0e607a80 | Jan Kiszka | for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) { |
537 | 0e607a80 | Jan Kiszka | if (sregs.interrupt_bitmap[i]) {
|
538 | 0e607a80 | Jan Kiszka | bit = ctz64(sregs.interrupt_bitmap[i]); |
539 | 0e607a80 | Jan Kiszka | env->interrupt_injected = i * 64 + bit;
|
540 | 0e607a80 | Jan Kiszka | break;
|
541 | 0e607a80 | Jan Kiszka | } |
542 | 0e607a80 | Jan Kiszka | } |
543 | 05330448 | aliguori | |
544 | 05330448 | aliguori | get_seg(&env->segs[R_CS], &sregs.cs); |
545 | 05330448 | aliguori | get_seg(&env->segs[R_DS], &sregs.ds); |
546 | 05330448 | aliguori | get_seg(&env->segs[R_ES], &sregs.es); |
547 | 05330448 | aliguori | get_seg(&env->segs[R_FS], &sregs.fs); |
548 | 05330448 | aliguori | get_seg(&env->segs[R_GS], &sregs.gs); |
549 | 05330448 | aliguori | get_seg(&env->segs[R_SS], &sregs.ss); |
550 | 05330448 | aliguori | |
551 | 05330448 | aliguori | get_seg(&env->tr, &sregs.tr); |
552 | 05330448 | aliguori | get_seg(&env->ldt, &sregs.ldt); |
553 | 05330448 | aliguori | |
554 | 05330448 | aliguori | env->idt.limit = sregs.idt.limit; |
555 | 05330448 | aliguori | env->idt.base = sregs.idt.base; |
556 | 05330448 | aliguori | env->gdt.limit = sregs.gdt.limit; |
557 | 05330448 | aliguori | env->gdt.base = sregs.gdt.base; |
558 | 05330448 | aliguori | |
559 | 05330448 | aliguori | env->cr[0] = sregs.cr0;
|
560 | 05330448 | aliguori | env->cr[2] = sregs.cr2;
|
561 | 05330448 | aliguori | env->cr[3] = sregs.cr3;
|
562 | 05330448 | aliguori | env->cr[4] = sregs.cr4;
|
563 | 05330448 | aliguori | |
564 | 05330448 | aliguori | cpu_set_apic_base(env, sregs.apic_base); |
565 | 05330448 | aliguori | |
566 | 05330448 | aliguori | env->efer = sregs.efer; |
567 | 05330448 | aliguori | //cpu_set_apic_tpr(env, sregs.cr8);
|
568 | 05330448 | aliguori | |
569 | 05330448 | aliguori | #define HFLAG_COPY_MASK ~( \
|
570 | 05330448 | aliguori | HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \ |
571 | 05330448 | aliguori | HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \ |
572 | 05330448 | aliguori | HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \ |
573 | 05330448 | aliguori | HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK) |
574 | 05330448 | aliguori | |
575 | 05330448 | aliguori | |
576 | 05330448 | aliguori | |
577 | 05330448 | aliguori | hflags = (env->segs[R_CS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK; |
578 | 05330448 | aliguori | hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
|
579 | 05330448 | aliguori | hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
|
580 | 05330448 | aliguori | (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK); |
581 | 05330448 | aliguori | hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK)); |
582 | 05330448 | aliguori | hflags |= (env->cr[4] & CR4_OSFXSR_MASK) <<
|
583 | 05330448 | aliguori | (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT); |
584 | 05330448 | aliguori | |
585 | 05330448 | aliguori | if (env->efer & MSR_EFER_LMA) {
|
586 | 05330448 | aliguori | hflags |= HF_LMA_MASK; |
587 | 05330448 | aliguori | } |
588 | 05330448 | aliguori | |
589 | 05330448 | aliguori | if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
|
590 | 05330448 | aliguori | hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK; |
591 | 05330448 | aliguori | } else {
|
592 | 05330448 | aliguori | hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >> |
593 | 05330448 | aliguori | (DESC_B_SHIFT - HF_CS32_SHIFT); |
594 | 05330448 | aliguori | hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >> |
595 | 05330448 | aliguori | (DESC_B_SHIFT - HF_SS32_SHIFT); |
596 | 05330448 | aliguori | if (!(env->cr[0] & CR0_PE_MASK) || |
597 | 05330448 | aliguori | (env->eflags & VM_MASK) || |
598 | 05330448 | aliguori | !(hflags & HF_CS32_MASK)) { |
599 | 05330448 | aliguori | hflags |= HF_ADDSEG_MASK; |
600 | 05330448 | aliguori | } else {
|
601 | 05330448 | aliguori | hflags |= ((env->segs[R_DS].base | |
602 | 05330448 | aliguori | env->segs[R_ES].base | |
603 | 05330448 | aliguori | env->segs[R_SS].base) != 0) <<
|
604 | 05330448 | aliguori | HF_ADDSEG_SHIFT; |
605 | 05330448 | aliguori | } |
606 | 05330448 | aliguori | } |
607 | 05330448 | aliguori | env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags; |
608 | 05330448 | aliguori | |
609 | 05330448 | aliguori | return 0; |
610 | 05330448 | aliguori | } |
611 | 05330448 | aliguori | |
612 | 05330448 | aliguori | static int kvm_get_msrs(CPUState *env) |
613 | 05330448 | aliguori | { |
614 | 05330448 | aliguori | struct {
|
615 | 05330448 | aliguori | struct kvm_msrs info;
|
616 | 05330448 | aliguori | struct kvm_msr_entry entries[100]; |
617 | 05330448 | aliguori | } msr_data; |
618 | 05330448 | aliguori | struct kvm_msr_entry *msrs = msr_data.entries;
|
619 | 05330448 | aliguori | int ret, i, n;
|
620 | 05330448 | aliguori | |
621 | 05330448 | aliguori | n = 0;
|
622 | 05330448 | aliguori | msrs[n++].index = MSR_IA32_SYSENTER_CS; |
623 | 05330448 | aliguori | msrs[n++].index = MSR_IA32_SYSENTER_ESP; |
624 | 05330448 | aliguori | msrs[n++].index = MSR_IA32_SYSENTER_EIP; |
625 | 05330448 | aliguori | if (kvm_has_msr_star(env))
|
626 | 05330448 | aliguori | msrs[n++].index = MSR_STAR; |
627 | 05330448 | aliguori | msrs[n++].index = MSR_IA32_TSC; |
628 | 05330448 | aliguori | #ifdef TARGET_X86_64
|
629 | 05330448 | aliguori | /* FIXME lm_capable_kernel */
|
630 | 05330448 | aliguori | msrs[n++].index = MSR_CSTAR; |
631 | 05330448 | aliguori | msrs[n++].index = MSR_KERNELGSBASE; |
632 | 05330448 | aliguori | msrs[n++].index = MSR_FMASK; |
633 | 05330448 | aliguori | msrs[n++].index = MSR_LSTAR; |
634 | 05330448 | aliguori | #endif
|
635 | 05330448 | aliguori | msr_data.info.nmsrs = n; |
636 | 05330448 | aliguori | ret = kvm_vcpu_ioctl(env, KVM_GET_MSRS, &msr_data); |
637 | 05330448 | aliguori | if (ret < 0) |
638 | 05330448 | aliguori | return ret;
|
639 | 05330448 | aliguori | |
640 | 05330448 | aliguori | for (i = 0; i < ret; i++) { |
641 | 05330448 | aliguori | switch (msrs[i].index) {
|
642 | 05330448 | aliguori | case MSR_IA32_SYSENTER_CS:
|
643 | 05330448 | aliguori | env->sysenter_cs = msrs[i].data; |
644 | 05330448 | aliguori | break;
|
645 | 05330448 | aliguori | case MSR_IA32_SYSENTER_ESP:
|
646 | 05330448 | aliguori | env->sysenter_esp = msrs[i].data; |
647 | 05330448 | aliguori | break;
|
648 | 05330448 | aliguori | case MSR_IA32_SYSENTER_EIP:
|
649 | 05330448 | aliguori | env->sysenter_eip = msrs[i].data; |
650 | 05330448 | aliguori | break;
|
651 | 05330448 | aliguori | case MSR_STAR:
|
652 | 05330448 | aliguori | env->star = msrs[i].data; |
653 | 05330448 | aliguori | break;
|
654 | 05330448 | aliguori | #ifdef TARGET_X86_64
|
655 | 05330448 | aliguori | case MSR_CSTAR:
|
656 | 05330448 | aliguori | env->cstar = msrs[i].data; |
657 | 05330448 | aliguori | break;
|
658 | 05330448 | aliguori | case MSR_KERNELGSBASE:
|
659 | 05330448 | aliguori | env->kernelgsbase = msrs[i].data; |
660 | 05330448 | aliguori | break;
|
661 | 05330448 | aliguori | case MSR_FMASK:
|
662 | 05330448 | aliguori | env->fmask = msrs[i].data; |
663 | 05330448 | aliguori | break;
|
664 | 05330448 | aliguori | case MSR_LSTAR:
|
665 | 05330448 | aliguori | env->lstar = msrs[i].data; |
666 | 05330448 | aliguori | break;
|
667 | 05330448 | aliguori | #endif
|
668 | 05330448 | aliguori | case MSR_IA32_TSC:
|
669 | 05330448 | aliguori | env->tsc = msrs[i].data; |
670 | 05330448 | aliguori | break;
|
671 | 05330448 | aliguori | } |
672 | 05330448 | aliguori | } |
673 | 05330448 | aliguori | |
674 | 05330448 | aliguori | return 0; |
675 | 05330448 | aliguori | } |
676 | 05330448 | aliguori | |
677 | 9bdbe550 | Hollis Blanchard | static int kvm_put_mp_state(CPUState *env) |
678 | 9bdbe550 | Hollis Blanchard | { |
679 | 9bdbe550 | Hollis Blanchard | struct kvm_mp_state mp_state = { .mp_state = env->mp_state };
|
680 | 9bdbe550 | Hollis Blanchard | |
681 | 9bdbe550 | Hollis Blanchard | return kvm_vcpu_ioctl(env, KVM_SET_MP_STATE, &mp_state);
|
682 | 9bdbe550 | Hollis Blanchard | } |
683 | 9bdbe550 | Hollis Blanchard | |
684 | 9bdbe550 | Hollis Blanchard | static int kvm_get_mp_state(CPUState *env) |
685 | 9bdbe550 | Hollis Blanchard | { |
686 | 9bdbe550 | Hollis Blanchard | struct kvm_mp_state mp_state;
|
687 | 9bdbe550 | Hollis Blanchard | int ret;
|
688 | 9bdbe550 | Hollis Blanchard | |
689 | 9bdbe550 | Hollis Blanchard | ret = kvm_vcpu_ioctl(env, KVM_GET_MP_STATE, &mp_state); |
690 | 9bdbe550 | Hollis Blanchard | if (ret < 0) { |
691 | 9bdbe550 | Hollis Blanchard | return ret;
|
692 | 9bdbe550 | Hollis Blanchard | } |
693 | 9bdbe550 | Hollis Blanchard | env->mp_state = mp_state.mp_state; |
694 | 9bdbe550 | Hollis Blanchard | return 0; |
695 | 9bdbe550 | Hollis Blanchard | } |
696 | 9bdbe550 | Hollis Blanchard | |
697 | 05330448 | aliguori | int kvm_arch_put_registers(CPUState *env)
|
698 | 05330448 | aliguori | { |
699 | 05330448 | aliguori | int ret;
|
700 | 05330448 | aliguori | |
701 | 05330448 | aliguori | ret = kvm_getput_regs(env, 1);
|
702 | 05330448 | aliguori | if (ret < 0) |
703 | 05330448 | aliguori | return ret;
|
704 | 05330448 | aliguori | |
705 | 05330448 | aliguori | ret = kvm_put_fpu(env); |
706 | 05330448 | aliguori | if (ret < 0) |
707 | 05330448 | aliguori | return ret;
|
708 | 05330448 | aliguori | |
709 | 05330448 | aliguori | ret = kvm_put_sregs(env); |
710 | 05330448 | aliguori | if (ret < 0) |
711 | 05330448 | aliguori | return ret;
|
712 | 05330448 | aliguori | |
713 | 05330448 | aliguori | ret = kvm_put_msrs(env); |
714 | 05330448 | aliguori | if (ret < 0) |
715 | 05330448 | aliguori | return ret;
|
716 | 05330448 | aliguori | |
717 | f8d926e9 | Jan Kiszka | ret = kvm_put_mp_state(env); |
718 | f8d926e9 | Jan Kiszka | if (ret < 0) |
719 | f8d926e9 | Jan Kiszka | return ret;
|
720 | f8d926e9 | Jan Kiszka | |
721 | f8d926e9 | Jan Kiszka | ret = kvm_get_mp_state(env); |
722 | f8d926e9 | Jan Kiszka | if (ret < 0) |
723 | f8d926e9 | Jan Kiszka | return ret;
|
724 | f8d926e9 | Jan Kiszka | |
725 | 05330448 | aliguori | return 0; |
726 | 05330448 | aliguori | } |
727 | 05330448 | aliguori | |
728 | 05330448 | aliguori | int kvm_arch_get_registers(CPUState *env)
|
729 | 05330448 | aliguori | { |
730 | 05330448 | aliguori | int ret;
|
731 | 05330448 | aliguori | |
732 | 05330448 | aliguori | ret = kvm_getput_regs(env, 0);
|
733 | 05330448 | aliguori | if (ret < 0) |
734 | 05330448 | aliguori | return ret;
|
735 | 05330448 | aliguori | |
736 | 05330448 | aliguori | ret = kvm_get_fpu(env); |
737 | 05330448 | aliguori | if (ret < 0) |
738 | 05330448 | aliguori | return ret;
|
739 | 05330448 | aliguori | |
740 | 05330448 | aliguori | ret = kvm_get_sregs(env); |
741 | 05330448 | aliguori | if (ret < 0) |
742 | 05330448 | aliguori | return ret;
|
743 | 05330448 | aliguori | |
744 | 05330448 | aliguori | ret = kvm_get_msrs(env); |
745 | 05330448 | aliguori | if (ret < 0) |
746 | 05330448 | aliguori | return ret;
|
747 | 05330448 | aliguori | |
748 | 05330448 | aliguori | return 0; |
749 | 05330448 | aliguori | } |
750 | 05330448 | aliguori | |
751 | 05330448 | aliguori | int kvm_arch_pre_run(CPUState *env, struct kvm_run *run) |
752 | 05330448 | aliguori | { |
753 | 05330448 | aliguori | /* Try to inject an interrupt if the guest can accept it */
|
754 | 05330448 | aliguori | if (run->ready_for_interrupt_injection &&
|
755 | 05330448 | aliguori | (env->interrupt_request & CPU_INTERRUPT_HARD) && |
756 | 05330448 | aliguori | (env->eflags & IF_MASK)) { |
757 | 05330448 | aliguori | int irq;
|
758 | 05330448 | aliguori | |
759 | 05330448 | aliguori | env->interrupt_request &= ~CPU_INTERRUPT_HARD; |
760 | 05330448 | aliguori | irq = cpu_get_pic_interrupt(env); |
761 | 05330448 | aliguori | if (irq >= 0) { |
762 | 05330448 | aliguori | struct kvm_interrupt intr;
|
763 | 05330448 | aliguori | intr.irq = irq; |
764 | 05330448 | aliguori | /* FIXME: errors */
|
765 | 05330448 | aliguori | dprintf("injected interrupt %d\n", irq);
|
766 | 05330448 | aliguori | kvm_vcpu_ioctl(env, KVM_INTERRUPT, &intr); |
767 | 05330448 | aliguori | } |
768 | 05330448 | aliguori | } |
769 | 05330448 | aliguori | |
770 | 05330448 | aliguori | /* If we have an interrupt but the guest is not ready to receive an
|
771 | 05330448 | aliguori | * interrupt, request an interrupt window exit. This will
|
772 | 05330448 | aliguori | * cause a return to userspace as soon as the guest is ready to
|
773 | 05330448 | aliguori | * receive interrupts. */
|
774 | 05330448 | aliguori | if ((env->interrupt_request & CPU_INTERRUPT_HARD))
|
775 | 05330448 | aliguori | run->request_interrupt_window = 1;
|
776 | 05330448 | aliguori | else
|
777 | 05330448 | aliguori | run->request_interrupt_window = 0;
|
778 | 05330448 | aliguori | |
779 | 05330448 | aliguori | dprintf("setting tpr\n");
|
780 | 05330448 | aliguori | run->cr8 = cpu_get_apic_tpr(env); |
781 | 05330448 | aliguori | |
782 | 05330448 | aliguori | return 0; |
783 | 05330448 | aliguori | } |
784 | 05330448 | aliguori | |
785 | 05330448 | aliguori | int kvm_arch_post_run(CPUState *env, struct kvm_run *run) |
786 | 05330448 | aliguori | { |
787 | 05330448 | aliguori | if (run->if_flag)
|
788 | 05330448 | aliguori | env->eflags |= IF_MASK; |
789 | 05330448 | aliguori | else
|
790 | 05330448 | aliguori | env->eflags &= ~IF_MASK; |
791 | 05330448 | aliguori | |
792 | 05330448 | aliguori | cpu_set_apic_tpr(env, run->cr8); |
793 | 05330448 | aliguori | cpu_set_apic_base(env, run->apic_base); |
794 | 05330448 | aliguori | |
795 | 05330448 | aliguori | return 0; |
796 | 05330448 | aliguori | } |
797 | 05330448 | aliguori | |
798 | 05330448 | aliguori | static int kvm_handle_halt(CPUState *env) |
799 | 05330448 | aliguori | { |
800 | 05330448 | aliguori | if (!((env->interrupt_request & CPU_INTERRUPT_HARD) &&
|
801 | 05330448 | aliguori | (env->eflags & IF_MASK)) && |
802 | 05330448 | aliguori | !(env->interrupt_request & CPU_INTERRUPT_NMI)) { |
803 | 05330448 | aliguori | env->halted = 1;
|
804 | 05330448 | aliguori | env->exception_index = EXCP_HLT; |
805 | 05330448 | aliguori | return 0; |
806 | 05330448 | aliguori | } |
807 | 05330448 | aliguori | |
808 | 05330448 | aliguori | return 1; |
809 | 05330448 | aliguori | } |
810 | 05330448 | aliguori | |
811 | 05330448 | aliguori | int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run) |
812 | 05330448 | aliguori | { |
813 | 05330448 | aliguori | int ret = 0; |
814 | 05330448 | aliguori | |
815 | 05330448 | aliguori | switch (run->exit_reason) {
|
816 | 05330448 | aliguori | case KVM_EXIT_HLT:
|
817 | 05330448 | aliguori | dprintf("handle_hlt\n");
|
818 | 05330448 | aliguori | ret = kvm_handle_halt(env); |
819 | 05330448 | aliguori | break;
|
820 | 05330448 | aliguori | } |
821 | 05330448 | aliguori | |
822 | 05330448 | aliguori | return ret;
|
823 | 05330448 | aliguori | } |
824 | e22a25c9 | aliguori | |
825 | e22a25c9 | aliguori | #ifdef KVM_CAP_SET_GUEST_DEBUG
|
826 | e22a25c9 | aliguori | int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp) |
827 | e22a25c9 | aliguori | { |
828 | 38972938 | Juan Quintela | static const uint8_t int3 = 0xcc; |
829 | 64bf3f4e | aliguori | |
830 | e22a25c9 | aliguori | if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) || |
831 | 64bf3f4e | aliguori | cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&int3, 1, 1)) |
832 | e22a25c9 | aliguori | return -EINVAL;
|
833 | e22a25c9 | aliguori | return 0; |
834 | e22a25c9 | aliguori | } |
835 | e22a25c9 | aliguori | |
836 | e22a25c9 | aliguori | int kvm_arch_remove_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp) |
837 | e22a25c9 | aliguori | { |
838 | e22a25c9 | aliguori | uint8_t int3; |
839 | e22a25c9 | aliguori | |
840 | e22a25c9 | aliguori | if (cpu_memory_rw_debug(env, bp->pc, &int3, 1, 0) || int3 != 0xcc || |
841 | 64bf3f4e | aliguori | cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) |
842 | e22a25c9 | aliguori | return -EINVAL;
|
843 | e22a25c9 | aliguori | return 0; |
844 | e22a25c9 | aliguori | } |
845 | e22a25c9 | aliguori | |
846 | e22a25c9 | aliguori | static struct { |
847 | e22a25c9 | aliguori | target_ulong addr; |
848 | e22a25c9 | aliguori | int len;
|
849 | e22a25c9 | aliguori | int type;
|
850 | e22a25c9 | aliguori | } hw_breakpoint[4];
|
851 | e22a25c9 | aliguori | |
852 | e22a25c9 | aliguori | static int nb_hw_breakpoint; |
853 | e22a25c9 | aliguori | |
854 | e22a25c9 | aliguori | static int find_hw_breakpoint(target_ulong addr, int len, int type) |
855 | e22a25c9 | aliguori | { |
856 | e22a25c9 | aliguori | int n;
|
857 | e22a25c9 | aliguori | |
858 | e22a25c9 | aliguori | for (n = 0; n < nb_hw_breakpoint; n++) |
859 | e22a25c9 | aliguori | if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
|
860 | e22a25c9 | aliguori | (hw_breakpoint[n].len == len || len == -1))
|
861 | e22a25c9 | aliguori | return n;
|
862 | e22a25c9 | aliguori | return -1; |
863 | e22a25c9 | aliguori | } |
864 | e22a25c9 | aliguori | |
865 | e22a25c9 | aliguori | int kvm_arch_insert_hw_breakpoint(target_ulong addr,
|
866 | e22a25c9 | aliguori | target_ulong len, int type)
|
867 | e22a25c9 | aliguori | { |
868 | e22a25c9 | aliguori | switch (type) {
|
869 | e22a25c9 | aliguori | case GDB_BREAKPOINT_HW:
|
870 | e22a25c9 | aliguori | len = 1;
|
871 | e22a25c9 | aliguori | break;
|
872 | e22a25c9 | aliguori | case GDB_WATCHPOINT_WRITE:
|
873 | e22a25c9 | aliguori | case GDB_WATCHPOINT_ACCESS:
|
874 | e22a25c9 | aliguori | switch (len) {
|
875 | e22a25c9 | aliguori | case 1: |
876 | e22a25c9 | aliguori | break;
|
877 | e22a25c9 | aliguori | case 2: |
878 | e22a25c9 | aliguori | case 4: |
879 | e22a25c9 | aliguori | case 8: |
880 | e22a25c9 | aliguori | if (addr & (len - 1)) |
881 | e22a25c9 | aliguori | return -EINVAL;
|
882 | e22a25c9 | aliguori | break;
|
883 | e22a25c9 | aliguori | default:
|
884 | e22a25c9 | aliguori | return -EINVAL;
|
885 | e22a25c9 | aliguori | } |
886 | e22a25c9 | aliguori | break;
|
887 | e22a25c9 | aliguori | default:
|
888 | e22a25c9 | aliguori | return -ENOSYS;
|
889 | e22a25c9 | aliguori | } |
890 | e22a25c9 | aliguori | |
891 | e22a25c9 | aliguori | if (nb_hw_breakpoint == 4) |
892 | e22a25c9 | aliguori | return -ENOBUFS;
|
893 | e22a25c9 | aliguori | |
894 | e22a25c9 | aliguori | if (find_hw_breakpoint(addr, len, type) >= 0) |
895 | e22a25c9 | aliguori | return -EEXIST;
|
896 | e22a25c9 | aliguori | |
897 | e22a25c9 | aliguori | hw_breakpoint[nb_hw_breakpoint].addr = addr; |
898 | e22a25c9 | aliguori | hw_breakpoint[nb_hw_breakpoint].len = len; |
899 | e22a25c9 | aliguori | hw_breakpoint[nb_hw_breakpoint].type = type; |
900 | e22a25c9 | aliguori | nb_hw_breakpoint++; |
901 | e22a25c9 | aliguori | |
902 | e22a25c9 | aliguori | return 0; |
903 | e22a25c9 | aliguori | } |
904 | e22a25c9 | aliguori | |
905 | e22a25c9 | aliguori | int kvm_arch_remove_hw_breakpoint(target_ulong addr,
|
906 | e22a25c9 | aliguori | target_ulong len, int type)
|
907 | e22a25c9 | aliguori | { |
908 | e22a25c9 | aliguori | int n;
|
909 | e22a25c9 | aliguori | |
910 | e22a25c9 | aliguori | n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
|
911 | e22a25c9 | aliguori | if (n < 0) |
912 | e22a25c9 | aliguori | return -ENOENT;
|
913 | e22a25c9 | aliguori | |
914 | e22a25c9 | aliguori | nb_hw_breakpoint--; |
915 | e22a25c9 | aliguori | hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint]; |
916 | e22a25c9 | aliguori | |
917 | e22a25c9 | aliguori | return 0; |
918 | e22a25c9 | aliguori | } |
919 | e22a25c9 | aliguori | |
920 | e22a25c9 | aliguori | void kvm_arch_remove_all_hw_breakpoints(void) |
921 | e22a25c9 | aliguori | { |
922 | e22a25c9 | aliguori | nb_hw_breakpoint = 0;
|
923 | e22a25c9 | aliguori | } |
924 | e22a25c9 | aliguori | |
925 | e22a25c9 | aliguori | static CPUWatchpoint hw_watchpoint;
|
926 | e22a25c9 | aliguori | |
927 | e22a25c9 | aliguori | int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info) |
928 | e22a25c9 | aliguori | { |
929 | e22a25c9 | aliguori | int handle = 0; |
930 | e22a25c9 | aliguori | int n;
|
931 | e22a25c9 | aliguori | |
932 | e22a25c9 | aliguori | if (arch_info->exception == 1) { |
933 | e22a25c9 | aliguori | if (arch_info->dr6 & (1 << 14)) { |
934 | e22a25c9 | aliguori | if (cpu_single_env->singlestep_enabled)
|
935 | e22a25c9 | aliguori | handle = 1;
|
936 | e22a25c9 | aliguori | } else {
|
937 | e22a25c9 | aliguori | for (n = 0; n < 4; n++) |
938 | e22a25c9 | aliguori | if (arch_info->dr6 & (1 << n)) |
939 | e22a25c9 | aliguori | switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) { |
940 | e22a25c9 | aliguori | case 0x0: |
941 | e22a25c9 | aliguori | handle = 1;
|
942 | e22a25c9 | aliguori | break;
|
943 | e22a25c9 | aliguori | case 0x1: |
944 | e22a25c9 | aliguori | handle = 1;
|
945 | e22a25c9 | aliguori | cpu_single_env->watchpoint_hit = &hw_watchpoint; |
946 | e22a25c9 | aliguori | hw_watchpoint.vaddr = hw_breakpoint[n].addr; |
947 | e22a25c9 | aliguori | hw_watchpoint.flags = BP_MEM_WRITE; |
948 | e22a25c9 | aliguori | break;
|
949 | e22a25c9 | aliguori | case 0x3: |
950 | e22a25c9 | aliguori | handle = 1;
|
951 | e22a25c9 | aliguori | cpu_single_env->watchpoint_hit = &hw_watchpoint; |
952 | e22a25c9 | aliguori | hw_watchpoint.vaddr = hw_breakpoint[n].addr; |
953 | e22a25c9 | aliguori | hw_watchpoint.flags = BP_MEM_ACCESS; |
954 | e22a25c9 | aliguori | break;
|
955 | e22a25c9 | aliguori | } |
956 | e22a25c9 | aliguori | } |
957 | e22a25c9 | aliguori | } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc)) |
958 | e22a25c9 | aliguori | handle = 1;
|
959 | e22a25c9 | aliguori | |
960 | e22a25c9 | aliguori | if (!handle)
|
961 | e22a25c9 | aliguori | kvm_update_guest_debug(cpu_single_env, |
962 | e22a25c9 | aliguori | (arch_info->exception == 1) ?
|
963 | e22a25c9 | aliguori | KVM_GUESTDBG_INJECT_DB : KVM_GUESTDBG_INJECT_BP); |
964 | e22a25c9 | aliguori | |
965 | e22a25c9 | aliguori | return handle;
|
966 | e22a25c9 | aliguori | } |
967 | e22a25c9 | aliguori | |
968 | e22a25c9 | aliguori | void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg) |
969 | e22a25c9 | aliguori | { |
970 | e22a25c9 | aliguori | const uint8_t type_code[] = {
|
971 | e22a25c9 | aliguori | [GDB_BREAKPOINT_HW] = 0x0,
|
972 | e22a25c9 | aliguori | [GDB_WATCHPOINT_WRITE] = 0x1,
|
973 | e22a25c9 | aliguori | [GDB_WATCHPOINT_ACCESS] = 0x3
|
974 | e22a25c9 | aliguori | }; |
975 | e22a25c9 | aliguori | const uint8_t len_code[] = {
|
976 | e22a25c9 | aliguori | [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2 |
977 | e22a25c9 | aliguori | }; |
978 | e22a25c9 | aliguori | int n;
|
979 | e22a25c9 | aliguori | |
980 | e22a25c9 | aliguori | if (kvm_sw_breakpoints_active(env))
|
981 | e22a25c9 | aliguori | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; |
982 | e22a25c9 | aliguori | |
983 | e22a25c9 | aliguori | if (nb_hw_breakpoint > 0) { |
984 | e22a25c9 | aliguori | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; |
985 | e22a25c9 | aliguori | dbg->arch.debugreg[7] = 0x0600; |
986 | e22a25c9 | aliguori | for (n = 0; n < nb_hw_breakpoint; n++) { |
987 | e22a25c9 | aliguori | dbg->arch.debugreg[n] = hw_breakpoint[n].addr; |
988 | e22a25c9 | aliguori | dbg->arch.debugreg[7] |= (2 << (n * 2)) | |
989 | e22a25c9 | aliguori | (type_code[hw_breakpoint[n].type] << (16 + n*4)) | |
990 | e22a25c9 | aliguori | (len_code[hw_breakpoint[n].len] << (18 + n*4)); |
991 | e22a25c9 | aliguori | } |
992 | e22a25c9 | aliguori | } |
993 | e22a25c9 | aliguori | } |
994 | e22a25c9 | aliguori | #endif /* KVM_CAP_SET_GUEST_DEBUG */ |