]>
Commit | Line | Data |
---|---|---|
05330448 AL |
1 | /* |
2 | * QEMU KVM support | |
3 | * | |
4 | * Copyright (C) 2006-2008 Qumranet Technologies | |
5 | * Copyright IBM, Corp. 2008 | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <sys/types.h> | |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/mman.h> | |
25d2e361 | 18 | #include <sys/utsname.h> |
05330448 AL |
19 | |
20 | #include <linux/kvm.h> | |
5802e066 | 21 | #include <linux/kvm_para.h> |
05330448 AL |
22 | |
23 | #include "qemu-common.h" | |
9c17d615 | 24 | #include "sysemu/sysemu.h" |
6410848b | 25 | #include "sysemu/kvm_int.h" |
1d31f66b | 26 | #include "kvm_i386.h" |
05330448 | 27 | #include "cpu.h" |
022c62cb | 28 | #include "exec/gdbstub.h" |
1de7afc9 PB |
29 | #include "qemu/host-utils.h" |
30 | #include "qemu/config-file.h" | |
0d09e41a PB |
31 | #include "hw/i386/pc.h" |
32 | #include "hw/i386/apic.h" | |
e0723c45 PB |
33 | #include "hw/i386/apic_internal.h" |
34 | #include "hw/i386/apic-msidef.h" | |
022c62cb | 35 | #include "exec/ioport.h" |
73aa529a | 36 | #include "standard-headers/asm-x86/hyperv.h" |
a2cb15b0 | 37 | #include "hw/pci/pci.h" |
68bfd0ad | 38 | #include "migration/migration.h" |
4c663752 | 39 | #include "exec/memattrs.h" |
05330448 AL |
40 | |
41 | //#define DEBUG_KVM | |
42 | ||
43 | #ifdef DEBUG_KVM | |
8c0d577e | 44 | #define DPRINTF(fmt, ...) \ |
05330448 AL |
45 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
46 | #else | |
8c0d577e | 47 | #define DPRINTF(fmt, ...) \ |
05330448 AL |
48 | do { } while (0) |
49 | #endif | |
50 | ||
1a03675d GC |
51 | #define MSR_KVM_WALL_CLOCK 0x11 |
52 | #define MSR_KVM_SYSTEM_TIME 0x12 | |
53 | ||
c0532a76 MT |
54 | #ifndef BUS_MCEERR_AR |
55 | #define BUS_MCEERR_AR 4 | |
56 | #endif | |
57 | #ifndef BUS_MCEERR_AO | |
58 | #define BUS_MCEERR_AO 5 | |
59 | #endif | |
60 | ||
94a8d39a JK |
61 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
62 | KVM_CAP_INFO(SET_TSS_ADDR), | |
63 | KVM_CAP_INFO(EXT_CPUID), | |
64 | KVM_CAP_INFO(MP_STATE), | |
65 | KVM_CAP_LAST_INFO | |
66 | }; | |
25d2e361 | 67 | |
c3a3a7d3 JK |
68 | static bool has_msr_star; |
69 | static bool has_msr_hsave_pa; | |
f28558d3 | 70 | static bool has_msr_tsc_adjust; |
aa82ba54 | 71 | static bool has_msr_tsc_deadline; |
df67696e | 72 | static bool has_msr_feature_control; |
c5999bfc | 73 | static bool has_msr_async_pf_en; |
bc9a839d | 74 | static bool has_msr_pv_eoi_en; |
21e87c46 | 75 | static bool has_msr_misc_enable; |
fc12d72e | 76 | static bool has_msr_smbase; |
79e9ebeb | 77 | static bool has_msr_bndcfgs; |
917367aa | 78 | static bool has_msr_kvm_steal_time; |
25d2e361 | 79 | static int lm_capable_kernel; |
7bc3d711 PB |
80 | static bool has_msr_hv_hypercall; |
81 | static bool has_msr_hv_vapic; | |
48a5f3bc | 82 | static bool has_msr_hv_tsc; |
f2a53c9e | 83 | static bool has_msr_hv_crash; |
d1ae67f6 | 84 | static bool has_msr_mtrr; |
18cd2c17 | 85 | static bool has_msr_xss; |
b827df58 | 86 | |
0d894367 PB |
87 | static bool has_msr_architectural_pmu; |
88 | static uint32_t num_architectural_pmu_counters; | |
89 | ||
355023f2 PB |
90 | bool kvm_has_smm(void) |
91 | { | |
92 | return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM); | |
93 | } | |
94 | ||
1d31f66b PM |
95 | bool kvm_allows_irq0_override(void) |
96 | { | |
97 | return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing(); | |
98 | } | |
99 | ||
b827df58 AK |
100 | static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max) |
101 | { | |
102 | struct kvm_cpuid2 *cpuid; | |
103 | int r, size; | |
104 | ||
105 | size = sizeof(*cpuid) + max * sizeof(*cpuid->entries); | |
e42a92ae | 106 | cpuid = g_malloc0(size); |
b827df58 AK |
107 | cpuid->nent = max; |
108 | r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid); | |
76ae317f MM |
109 | if (r == 0 && cpuid->nent >= max) { |
110 | r = -E2BIG; | |
111 | } | |
b827df58 AK |
112 | if (r < 0) { |
113 | if (r == -E2BIG) { | |
7267c094 | 114 | g_free(cpuid); |
b827df58 AK |
115 | return NULL; |
116 | } else { | |
117 | fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n", | |
118 | strerror(-r)); | |
119 | exit(1); | |
120 | } | |
121 | } | |
122 | return cpuid; | |
123 | } | |
124 | ||
dd87f8a6 EH |
125 | /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough |
126 | * for all entries. | |
127 | */ | |
128 | static struct kvm_cpuid2 *get_supported_cpuid(KVMState *s) | |
129 | { | |
130 | struct kvm_cpuid2 *cpuid; | |
131 | int max = 1; | |
132 | while ((cpuid = try_get_cpuid(s, max)) == NULL) { | |
133 | max *= 2; | |
134 | } | |
135 | return cpuid; | |
136 | } | |
137 | ||
a443bc34 | 138 | static const struct kvm_para_features { |
0c31b744 GC |
139 | int cap; |
140 | int feature; | |
141 | } para_features[] = { | |
142 | { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE }, | |
143 | { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY }, | |
144 | { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP }, | |
0c31b744 | 145 | { KVM_CAP_ASYNC_PF, KVM_FEATURE_ASYNC_PF }, |
0c31b744 GC |
146 | }; |
147 | ||
ba9bc59e | 148 | static int get_para_features(KVMState *s) |
0c31b744 GC |
149 | { |
150 | int i, features = 0; | |
151 | ||
8e03c100 | 152 | for (i = 0; i < ARRAY_SIZE(para_features); i++) { |
ba9bc59e | 153 | if (kvm_check_extension(s, para_features[i].cap)) { |
0c31b744 GC |
154 | features |= (1 << para_features[i].feature); |
155 | } | |
156 | } | |
157 | ||
158 | return features; | |
159 | } | |
0c31b744 GC |
160 | |
161 | ||
829ae2f9 EH |
162 | /* Returns the value for a specific register on the cpuid entry |
163 | */ | |
164 | static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg) | |
165 | { | |
166 | uint32_t ret = 0; | |
167 | switch (reg) { | |
168 | case R_EAX: | |
169 | ret = entry->eax; | |
170 | break; | |
171 | case R_EBX: | |
172 | ret = entry->ebx; | |
173 | break; | |
174 | case R_ECX: | |
175 | ret = entry->ecx; | |
176 | break; | |
177 | case R_EDX: | |
178 | ret = entry->edx; | |
179 | break; | |
180 | } | |
181 | return ret; | |
182 | } | |
183 | ||
4fb73f1d EH |
184 | /* Find matching entry for function/index on kvm_cpuid2 struct |
185 | */ | |
186 | static struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid, | |
187 | uint32_t function, | |
188 | uint32_t index) | |
189 | { | |
190 | int i; | |
191 | for (i = 0; i < cpuid->nent; ++i) { | |
192 | if (cpuid->entries[i].function == function && | |
193 | cpuid->entries[i].index == index) { | |
194 | return &cpuid->entries[i]; | |
195 | } | |
196 | } | |
197 | /* not found: */ | |
198 | return NULL; | |
199 | } | |
200 | ||
ba9bc59e | 201 | uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function, |
c958a8bd | 202 | uint32_t index, int reg) |
b827df58 AK |
203 | { |
204 | struct kvm_cpuid2 *cpuid; | |
b827df58 AK |
205 | uint32_t ret = 0; |
206 | uint32_t cpuid_1_edx; | |
8c723b79 | 207 | bool found = false; |
b827df58 | 208 | |
dd87f8a6 | 209 | cpuid = get_supported_cpuid(s); |
b827df58 | 210 | |
4fb73f1d EH |
211 | struct kvm_cpuid_entry2 *entry = cpuid_find_entry(cpuid, function, index); |
212 | if (entry) { | |
213 | found = true; | |
214 | ret = cpuid_entry_get_reg(entry, reg); | |
b827df58 AK |
215 | } |
216 | ||
7b46e5ce EH |
217 | /* Fixups for the data returned by KVM, below */ |
218 | ||
c2acb022 EH |
219 | if (function == 1 && reg == R_EDX) { |
220 | /* KVM before 2.6.30 misreports the following features */ | |
221 | ret |= CPUID_MTRR | CPUID_PAT | CPUID_MCE | CPUID_MCA; | |
84bd945c EH |
222 | } else if (function == 1 && reg == R_ECX) { |
223 | /* We can set the hypervisor flag, even if KVM does not return it on | |
224 | * GET_SUPPORTED_CPUID | |
225 | */ | |
226 | ret |= CPUID_EXT_HYPERVISOR; | |
ac67ee26 EH |
227 | /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it |
228 | * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER, | |
229 | * and the irqchip is in the kernel. | |
230 | */ | |
231 | if (kvm_irqchip_in_kernel() && | |
232 | kvm_check_extension(s, KVM_CAP_TSC_DEADLINE_TIMER)) { | |
233 | ret |= CPUID_EXT_TSC_DEADLINE_TIMER; | |
234 | } | |
41e5e76d EH |
235 | |
236 | /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled | |
237 | * without the in-kernel irqchip | |
238 | */ | |
239 | if (!kvm_irqchip_in_kernel()) { | |
240 | ret &= ~CPUID_EXT_X2APIC; | |
b827df58 | 241 | } |
28b8e4d0 JK |
242 | } else if (function == 6 && reg == R_EAX) { |
243 | ret |= CPUID_6_EAX_ARAT; /* safe to allow because of emulated APIC */ | |
c2acb022 EH |
244 | } else if (function == 0x80000001 && reg == R_EDX) { |
245 | /* On Intel, kvm returns cpuid according to the Intel spec, | |
246 | * so add missing bits according to the AMD spec: | |
247 | */ | |
248 | cpuid_1_edx = kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX); | |
249 | ret |= cpuid_1_edx & CPUID_EXT2_AMD_ALIASES; | |
b827df58 AK |
250 | } |
251 | ||
7267c094 | 252 | g_free(cpuid); |
b827df58 | 253 | |
0c31b744 | 254 | /* fallback for older kernels */ |
8c723b79 | 255 | if ((function == KVM_CPUID_FEATURES) && !found) { |
ba9bc59e | 256 | ret = get_para_features(s); |
b9bec74b | 257 | } |
0c31b744 GC |
258 | |
259 | return ret; | |
bb0300dc | 260 | } |
bb0300dc | 261 | |
3c85e74f HY |
262 | typedef struct HWPoisonPage { |
263 | ram_addr_t ram_addr; | |
264 | QLIST_ENTRY(HWPoisonPage) list; | |
265 | } HWPoisonPage; | |
266 | ||
267 | static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list = | |
268 | QLIST_HEAD_INITIALIZER(hwpoison_page_list); | |
269 | ||
270 | static void kvm_unpoison_all(void *param) | |
271 | { | |
272 | HWPoisonPage *page, *next_page; | |
273 | ||
274 | QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) { | |
275 | QLIST_REMOVE(page, list); | |
276 | qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE); | |
7267c094 | 277 | g_free(page); |
3c85e74f HY |
278 | } |
279 | } | |
280 | ||
3c85e74f HY |
281 | static void kvm_hwpoison_page_add(ram_addr_t ram_addr) |
282 | { | |
283 | HWPoisonPage *page; | |
284 | ||
285 | QLIST_FOREACH(page, &hwpoison_page_list, list) { | |
286 | if (page->ram_addr == ram_addr) { | |
287 | return; | |
288 | } | |
289 | } | |
ab3ad07f | 290 | page = g_new(HWPoisonPage, 1); |
3c85e74f HY |
291 | page->ram_addr = ram_addr; |
292 | QLIST_INSERT_HEAD(&hwpoison_page_list, page, list); | |
293 | } | |
294 | ||
e7701825 MT |
295 | static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap, |
296 | int *max_banks) | |
297 | { | |
298 | int r; | |
299 | ||
14a09518 | 300 | r = kvm_check_extension(s, KVM_CAP_MCE); |
e7701825 MT |
301 | if (r > 0) { |
302 | *max_banks = r; | |
303 | return kvm_ioctl(s, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap); | |
304 | } | |
305 | return -ENOSYS; | |
306 | } | |
307 | ||
bee615d4 | 308 | static void kvm_mce_inject(X86CPU *cpu, hwaddr paddr, int code) |
e7701825 | 309 | { |
bee615d4 | 310 | CPUX86State *env = &cpu->env; |
c34d440a JK |
311 | uint64_t status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN | |
312 | MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S; | |
313 | uint64_t mcg_status = MCG_STATUS_MCIP; | |
e7701825 | 314 | |
c34d440a JK |
315 | if (code == BUS_MCEERR_AR) { |
316 | status |= MCI_STATUS_AR | 0x134; | |
317 | mcg_status |= MCG_STATUS_EIPV; | |
318 | } else { | |
319 | status |= 0xc0; | |
320 | mcg_status |= MCG_STATUS_RIPV; | |
419fb20a | 321 | } |
8c5cf3b6 | 322 | cpu_x86_inject_mce(NULL, cpu, 9, status, mcg_status, paddr, |
c34d440a JK |
323 | (MCM_ADDR_PHYS << 6) | 0xc, |
324 | cpu_x86_support_mca_broadcast(env) ? | |
325 | MCE_INJECT_BROADCAST : 0); | |
419fb20a | 326 | } |
419fb20a JK |
327 | |
328 | static void hardware_memory_error(void) | |
329 | { | |
330 | fprintf(stderr, "Hardware memory error!\n"); | |
331 | exit(1); | |
332 | } | |
333 | ||
20d695a9 | 334 | int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr) |
419fb20a | 335 | { |
20d695a9 AF |
336 | X86CPU *cpu = X86_CPU(c); |
337 | CPUX86State *env = &cpu->env; | |
419fb20a | 338 | ram_addr_t ram_addr; |
a8170e5e | 339 | hwaddr paddr; |
419fb20a JK |
340 | |
341 | if ((env->mcg_cap & MCG_SER_P) && addr | |
c34d440a | 342 | && (code == BUS_MCEERR_AR || code == BUS_MCEERR_AO)) { |
1b5ec234 | 343 | if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL || |
a60f24b5 | 344 | !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) { |
419fb20a JK |
345 | fprintf(stderr, "Hardware memory error for memory used by " |
346 | "QEMU itself instead of guest system!\n"); | |
347 | /* Hope we are lucky for AO MCE */ | |
348 | if (code == BUS_MCEERR_AO) { | |
349 | return 0; | |
350 | } else { | |
351 | hardware_memory_error(); | |
352 | } | |
353 | } | |
3c85e74f | 354 | kvm_hwpoison_page_add(ram_addr); |
bee615d4 | 355 | kvm_mce_inject(cpu, paddr, code); |
e56ff191 | 356 | } else { |
419fb20a JK |
357 | if (code == BUS_MCEERR_AO) { |
358 | return 0; | |
359 | } else if (code == BUS_MCEERR_AR) { | |
360 | hardware_memory_error(); | |
361 | } else { | |
362 | return 1; | |
363 | } | |
364 | } | |
365 | return 0; | |
366 | } | |
367 | ||
368 | int kvm_arch_on_sigbus(int code, void *addr) | |
369 | { | |
182735ef AF |
370 | X86CPU *cpu = X86_CPU(first_cpu); |
371 | ||
372 | if ((cpu->env.mcg_cap & MCG_SER_P) && addr && code == BUS_MCEERR_AO) { | |
419fb20a | 373 | ram_addr_t ram_addr; |
a8170e5e | 374 | hwaddr paddr; |
419fb20a JK |
375 | |
376 | /* Hope we are lucky for AO MCE */ | |
1b5ec234 | 377 | if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL || |
182735ef | 378 | !kvm_physical_memory_addr_from_host(first_cpu->kvm_state, |
a60f24b5 | 379 | addr, &paddr)) { |
419fb20a JK |
380 | fprintf(stderr, "Hardware memory error for memory used by " |
381 | "QEMU itself instead of guest system!: %p\n", addr); | |
382 | return 0; | |
383 | } | |
3c85e74f | 384 | kvm_hwpoison_page_add(ram_addr); |
182735ef | 385 | kvm_mce_inject(X86_CPU(first_cpu), paddr, code); |
e56ff191 | 386 | } else { |
419fb20a JK |
387 | if (code == BUS_MCEERR_AO) { |
388 | return 0; | |
389 | } else if (code == BUS_MCEERR_AR) { | |
390 | hardware_memory_error(); | |
391 | } else { | |
392 | return 1; | |
393 | } | |
394 | } | |
395 | return 0; | |
396 | } | |
e7701825 | 397 | |
1bc22652 | 398 | static int kvm_inject_mce_oldstyle(X86CPU *cpu) |
ab443475 | 399 | { |
1bc22652 AF |
400 | CPUX86State *env = &cpu->env; |
401 | ||
ab443475 JK |
402 | if (!kvm_has_vcpu_events() && env->exception_injected == EXCP12_MCHK) { |
403 | unsigned int bank, bank_num = env->mcg_cap & 0xff; | |
404 | struct kvm_x86_mce mce; | |
405 | ||
406 | env->exception_injected = -1; | |
407 | ||
408 | /* | |
409 | * There must be at least one bank in use if an MCE is pending. | |
410 | * Find it and use its values for the event injection. | |
411 | */ | |
412 | for (bank = 0; bank < bank_num; bank++) { | |
413 | if (env->mce_banks[bank * 4 + 1] & MCI_STATUS_VAL) { | |
414 | break; | |
415 | } | |
416 | } | |
417 | assert(bank < bank_num); | |
418 | ||
419 | mce.bank = bank; | |
420 | mce.status = env->mce_banks[bank * 4 + 1]; | |
421 | mce.mcg_status = env->mcg_status; | |
422 | mce.addr = env->mce_banks[bank * 4 + 2]; | |
423 | mce.misc = env->mce_banks[bank * 4 + 3]; | |
424 | ||
1bc22652 | 425 | return kvm_vcpu_ioctl(CPU(cpu), KVM_X86_SET_MCE, &mce); |
ab443475 | 426 | } |
ab443475 JK |
427 | return 0; |
428 | } | |
429 | ||
1dfb4dd9 | 430 | static void cpu_update_state(void *opaque, int running, RunState state) |
b8cc45d6 | 431 | { |
317ac620 | 432 | CPUX86State *env = opaque; |
b8cc45d6 GC |
433 | |
434 | if (running) { | |
435 | env->tsc_valid = false; | |
436 | } | |
437 | } | |
438 | ||
83b17af5 | 439 | unsigned long kvm_arch_vcpu_id(CPUState *cs) |
b164e48e | 440 | { |
83b17af5 | 441 | X86CPU *cpu = X86_CPU(cs); |
7e72a45c | 442 | return cpu->apic_id; |
b164e48e EH |
443 | } |
444 | ||
92067bf4 IM |
445 | #ifndef KVM_CPUID_SIGNATURE_NEXT |
446 | #define KVM_CPUID_SIGNATURE_NEXT 0x40000100 | |
447 | #endif | |
448 | ||
449 | static bool hyperv_hypercall_available(X86CPU *cpu) | |
450 | { | |
451 | return cpu->hyperv_vapic || | |
452 | (cpu->hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY); | |
453 | } | |
454 | ||
455 | static bool hyperv_enabled(X86CPU *cpu) | |
456 | { | |
7bc3d711 PB |
457 | CPUState *cs = CPU(cpu); |
458 | return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 && | |
459 | (hyperv_hypercall_available(cpu) || | |
48a5f3bc | 460 | cpu->hyperv_time || |
f2a53c9e AS |
461 | cpu->hyperv_relaxed_timing || |
462 | cpu->hyperv_crash); | |
92067bf4 IM |
463 | } |
464 | ||
68bfd0ad MT |
465 | static Error *invtsc_mig_blocker; |
466 | ||
f8bb0565 | 467 | #define KVM_MAX_CPUID_ENTRIES 100 |
0893d460 | 468 | |
20d695a9 | 469 | int kvm_arch_init_vcpu(CPUState *cs) |
05330448 AL |
470 | { |
471 | struct { | |
486bd5a2 | 472 | struct kvm_cpuid2 cpuid; |
f8bb0565 | 473 | struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES]; |
541dc0d4 | 474 | } QEMU_PACKED cpuid_data; |
20d695a9 AF |
475 | X86CPU *cpu = X86_CPU(cs); |
476 | CPUX86State *env = &cpu->env; | |
486bd5a2 | 477 | uint32_t limit, i, j, cpuid_i; |
a33609ca | 478 | uint32_t unused; |
bb0300dc | 479 | struct kvm_cpuid_entry2 *c; |
bb0300dc | 480 | uint32_t signature[3]; |
234cc647 | 481 | int kvm_base = KVM_CPUID_SIGNATURE; |
e7429073 | 482 | int r; |
05330448 | 483 | |
ef4cbe14 SW |
484 | memset(&cpuid_data, 0, sizeof(cpuid_data)); |
485 | ||
05330448 AL |
486 | cpuid_i = 0; |
487 | ||
bb0300dc | 488 | /* Paravirtualization CPUIDs */ |
234cc647 PB |
489 | if (hyperv_enabled(cpu)) { |
490 | c = &cpuid_data.entries[cpuid_i++]; | |
491 | c->function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS; | |
eab70139 VR |
492 | memcpy(signature, "Microsoft Hv", 12); |
493 | c->eax = HYPERV_CPUID_MIN; | |
234cc647 PB |
494 | c->ebx = signature[0]; |
495 | c->ecx = signature[1]; | |
496 | c->edx = signature[2]; | |
0c31b744 | 497 | |
234cc647 PB |
498 | c = &cpuid_data.entries[cpuid_i++]; |
499 | c->function = HYPERV_CPUID_INTERFACE; | |
eab70139 VR |
500 | memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12); |
501 | c->eax = signature[0]; | |
234cc647 PB |
502 | c->ebx = 0; |
503 | c->ecx = 0; | |
504 | c->edx = 0; | |
eab70139 VR |
505 | |
506 | c = &cpuid_data.entries[cpuid_i++]; | |
eab70139 VR |
507 | c->function = HYPERV_CPUID_VERSION; |
508 | c->eax = 0x00001bbc; | |
509 | c->ebx = 0x00060001; | |
510 | ||
511 | c = &cpuid_data.entries[cpuid_i++]; | |
eab70139 | 512 | c->function = HYPERV_CPUID_FEATURES; |
92067bf4 | 513 | if (cpu->hyperv_relaxed_timing) { |
eab70139 VR |
514 | c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE; |
515 | } | |
92067bf4 | 516 | if (cpu->hyperv_vapic) { |
eab70139 VR |
517 | c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE; |
518 | c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE; | |
7bc3d711 | 519 | has_msr_hv_vapic = true; |
eab70139 | 520 | } |
48a5f3bc VR |
521 | if (cpu->hyperv_time && |
522 | kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) { | |
523 | c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE; | |
524 | c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE; | |
525 | c->eax |= 0x200; | |
526 | has_msr_hv_tsc = true; | |
527 | } | |
f2a53c9e AS |
528 | if (cpu->hyperv_crash && has_msr_hv_crash) { |
529 | c->edx |= HV_X64_GUEST_CRASH_MSR_AVAILABLE; | |
530 | } | |
531 | ||
eab70139 | 532 | c = &cpuid_data.entries[cpuid_i++]; |
eab70139 | 533 | c->function = HYPERV_CPUID_ENLIGHTMENT_INFO; |
92067bf4 | 534 | if (cpu->hyperv_relaxed_timing) { |
eab70139 VR |
535 | c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; |
536 | } | |
7bc3d711 | 537 | if (has_msr_hv_vapic) { |
eab70139 VR |
538 | c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; |
539 | } | |
92067bf4 | 540 | c->ebx = cpu->hyperv_spinlock_attempts; |
eab70139 VR |
541 | |
542 | c = &cpuid_data.entries[cpuid_i++]; | |
eab70139 VR |
543 | c->function = HYPERV_CPUID_IMPLEMENT_LIMITS; |
544 | c->eax = 0x40; | |
545 | c->ebx = 0x40; | |
546 | ||
234cc647 | 547 | kvm_base = KVM_CPUID_SIGNATURE_NEXT; |
7bc3d711 | 548 | has_msr_hv_hypercall = true; |
eab70139 VR |
549 | } |
550 | ||
f522d2ac AW |
551 | if (cpu->expose_kvm) { |
552 | memcpy(signature, "KVMKVMKVM\0\0\0", 12); | |
553 | c = &cpuid_data.entries[cpuid_i++]; | |
554 | c->function = KVM_CPUID_SIGNATURE | kvm_base; | |
79b6f2f6 | 555 | c->eax = KVM_CPUID_FEATURES | kvm_base; |
f522d2ac AW |
556 | c->ebx = signature[0]; |
557 | c->ecx = signature[1]; | |
558 | c->edx = signature[2]; | |
234cc647 | 559 | |
f522d2ac AW |
560 | c = &cpuid_data.entries[cpuid_i++]; |
561 | c->function = KVM_CPUID_FEATURES | kvm_base; | |
562 | c->eax = env->features[FEAT_KVM]; | |
234cc647 | 563 | |
f522d2ac | 564 | has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF); |
bb0300dc | 565 | |
f522d2ac | 566 | has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI); |
bc9a839d | 567 | |
f522d2ac AW |
568 | has_msr_kvm_steal_time = c->eax & (1 << KVM_FEATURE_STEAL_TIME); |
569 | } | |
917367aa | 570 | |
a33609ca | 571 | cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused); |
05330448 AL |
572 | |
573 | for (i = 0; i <= limit; i++) { | |
f8bb0565 IM |
574 | if (cpuid_i == KVM_MAX_CPUID_ENTRIES) { |
575 | fprintf(stderr, "unsupported level value: 0x%x\n", limit); | |
576 | abort(); | |
577 | } | |
bb0300dc | 578 | c = &cpuid_data.entries[cpuid_i++]; |
486bd5a2 AL |
579 | |
580 | switch (i) { | |
a36b1029 AL |
581 | case 2: { |
582 | /* Keep reading function 2 till all the input is received */ | |
583 | int times; | |
584 | ||
a36b1029 | 585 | c->function = i; |
a33609ca AL |
586 | c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC | |
587 | KVM_CPUID_FLAG_STATE_READ_NEXT; | |
588 | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx); | |
589 | times = c->eax & 0xff; | |
a36b1029 AL |
590 | |
591 | for (j = 1; j < times; ++j) { | |
f8bb0565 IM |
592 | if (cpuid_i == KVM_MAX_CPUID_ENTRIES) { |
593 | fprintf(stderr, "cpuid_data is full, no space for " | |
594 | "cpuid(eax:2):eax & 0xf = 0x%x\n", times); | |
595 | abort(); | |
596 | } | |
a33609ca | 597 | c = &cpuid_data.entries[cpuid_i++]; |
a36b1029 | 598 | c->function = i; |
a33609ca AL |
599 | c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC; |
600 | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx); | |
a36b1029 AL |
601 | } |
602 | break; | |
603 | } | |
486bd5a2 AL |
604 | case 4: |
605 | case 0xb: | |
606 | case 0xd: | |
607 | for (j = 0; ; j++) { | |
31e8c696 AP |
608 | if (i == 0xd && j == 64) { |
609 | break; | |
610 | } | |
486bd5a2 AL |
611 | c->function = i; |
612 | c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX; | |
613 | c->index = j; | |
a33609ca | 614 | cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx); |
486bd5a2 | 615 | |
b9bec74b | 616 | if (i == 4 && c->eax == 0) { |
486bd5a2 | 617 | break; |
b9bec74b JK |
618 | } |
619 | if (i == 0xb && !(c->ecx & 0xff00)) { | |
486bd5a2 | 620 | break; |
b9bec74b JK |
621 | } |
622 | if (i == 0xd && c->eax == 0) { | |
31e8c696 | 623 | continue; |
b9bec74b | 624 | } |
f8bb0565 IM |
625 | if (cpuid_i == KVM_MAX_CPUID_ENTRIES) { |
626 | fprintf(stderr, "cpuid_data is full, no space for " | |
627 | "cpuid(eax:0x%x,ecx:0x%x)\n", i, j); | |
628 | abort(); | |
629 | } | |
a33609ca | 630 | c = &cpuid_data.entries[cpuid_i++]; |
486bd5a2 AL |
631 | } |
632 | break; | |
633 | default: | |
486bd5a2 | 634 | c->function = i; |
a33609ca AL |
635 | c->flags = 0; |
636 | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx); | |
486bd5a2 AL |
637 | break; |
638 | } | |
05330448 | 639 | } |
0d894367 PB |
640 | |
641 | if (limit >= 0x0a) { | |
642 | uint32_t ver; | |
643 | ||
644 | cpu_x86_cpuid(env, 0x0a, 0, &ver, &unused, &unused, &unused); | |
645 | if ((ver & 0xff) > 0) { | |
646 | has_msr_architectural_pmu = true; | |
647 | num_architectural_pmu_counters = (ver & 0xff00) >> 8; | |
648 | ||
649 | /* Shouldn't be more than 32, since that's the number of bits | |
650 | * available in EBX to tell us _which_ counters are available. | |
651 | * Play it safe. | |
652 | */ | |
653 | if (num_architectural_pmu_counters > MAX_GP_COUNTERS) { | |
654 | num_architectural_pmu_counters = MAX_GP_COUNTERS; | |
655 | } | |
656 | } | |
657 | } | |
658 | ||
a33609ca | 659 | cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused); |
05330448 AL |
660 | |
661 | for (i = 0x80000000; i <= limit; i++) { | |
f8bb0565 IM |
662 | if (cpuid_i == KVM_MAX_CPUID_ENTRIES) { |
663 | fprintf(stderr, "unsupported xlevel value: 0x%x\n", limit); | |
664 | abort(); | |
665 | } | |
bb0300dc | 666 | c = &cpuid_data.entries[cpuid_i++]; |
05330448 | 667 | |
05330448 | 668 | c->function = i; |
a33609ca AL |
669 | c->flags = 0; |
670 | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx); | |
05330448 AL |
671 | } |
672 | ||
b3baa152 BW |
673 | /* Call Centaur's CPUID instructions they are supported. */ |
674 | if (env->cpuid_xlevel2 > 0) { | |
b3baa152 BW |
675 | cpu_x86_cpuid(env, 0xC0000000, 0, &limit, &unused, &unused, &unused); |
676 | ||
677 | for (i = 0xC0000000; i <= limit; i++) { | |
f8bb0565 IM |
678 | if (cpuid_i == KVM_MAX_CPUID_ENTRIES) { |
679 | fprintf(stderr, "unsupported xlevel2 value: 0x%x\n", limit); | |
680 | abort(); | |
681 | } | |
b3baa152 BW |
682 | c = &cpuid_data.entries[cpuid_i++]; |
683 | ||
684 | c->function = i; | |
685 | c->flags = 0; | |
686 | cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx); | |
687 | } | |
688 | } | |
689 | ||
05330448 AL |
690 | cpuid_data.cpuid.nent = cpuid_i; |
691 | ||
e7701825 | 692 | if (((env->cpuid_version >> 8)&0xF) >= 6 |
0514ef2f | 693 | && (env->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) == |
fc7a504c | 694 | (CPUID_MCE | CPUID_MCA) |
a60f24b5 | 695 | && kvm_check_extension(cs->kvm_state, KVM_CAP_MCE) > 0) { |
e7701825 MT |
696 | uint64_t mcg_cap; |
697 | int banks; | |
32a42024 | 698 | int ret; |
e7701825 | 699 | |
a60f24b5 | 700 | ret = kvm_get_mce_cap_supported(cs->kvm_state, &mcg_cap, &banks); |
75d49497 JK |
701 | if (ret < 0) { |
702 | fprintf(stderr, "kvm_get_mce_cap_supported: %s", strerror(-ret)); | |
703 | return ret; | |
e7701825 | 704 | } |
75d49497 JK |
705 | |
706 | if (banks > MCE_BANKS_DEF) { | |
707 | banks = MCE_BANKS_DEF; | |
708 | } | |
709 | mcg_cap &= MCE_CAP_DEF; | |
710 | mcg_cap |= banks; | |
1bc22652 | 711 | ret = kvm_vcpu_ioctl(cs, KVM_X86_SETUP_MCE, &mcg_cap); |
75d49497 JK |
712 | if (ret < 0) { |
713 | fprintf(stderr, "KVM_X86_SETUP_MCE: %s", strerror(-ret)); | |
714 | return ret; | |
715 | } | |
716 | ||
717 | env->mcg_cap = mcg_cap; | |
e7701825 | 718 | } |
e7701825 | 719 | |
b8cc45d6 GC |
720 | qemu_add_vm_change_state_handler(cpu_update_state, env); |
721 | ||
df67696e LJ |
722 | c = cpuid_find_entry(&cpuid_data.cpuid, 1, 0); |
723 | if (c) { | |
724 | has_msr_feature_control = !!(c->ecx & CPUID_EXT_VMX) || | |
725 | !!(c->ecx & CPUID_EXT_SMX); | |
726 | } | |
727 | ||
68bfd0ad MT |
728 | c = cpuid_find_entry(&cpuid_data.cpuid, 0x80000007, 0); |
729 | if (c && (c->edx & 1<<8) && invtsc_mig_blocker == NULL) { | |
730 | /* for migration */ | |
731 | error_setg(&invtsc_mig_blocker, | |
732 | "State blocked by non-migratable CPU device" | |
733 | " (invtsc flag)"); | |
734 | migrate_add_blocker(invtsc_mig_blocker); | |
735 | /* for savevm */ | |
736 | vmstate_x86_cpu.unmigratable = 1; | |
737 | } | |
738 | ||
7e680753 | 739 | cpuid_data.cpuid.padding = 0; |
1bc22652 | 740 | r = kvm_vcpu_ioctl(cs, KVM_SET_CPUID2, &cpuid_data); |
fdc9c41a JK |
741 | if (r) { |
742 | return r; | |
743 | } | |
e7429073 | 744 | |
a60f24b5 | 745 | r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL); |
e7429073 | 746 | if (r && env->tsc_khz) { |
1bc22652 | 747 | r = kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz); |
e7429073 JR |
748 | if (r < 0) { |
749 | fprintf(stderr, "KVM_SET_TSC_KHZ failed\n"); | |
750 | return r; | |
751 | } | |
752 | } | |
e7429073 | 753 | |
fabacc0f JK |
754 | if (kvm_has_xsave()) { |
755 | env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave)); | |
756 | } | |
757 | ||
d1ae67f6 AW |
758 | if (env->features[FEAT_1_EDX] & CPUID_MTRR) { |
759 | has_msr_mtrr = true; | |
760 | } | |
761 | ||
e7429073 | 762 | return 0; |
05330448 AL |
763 | } |
764 | ||
50a2c6e5 | 765 | void kvm_arch_reset_vcpu(X86CPU *cpu) |
caa5af0f | 766 | { |
20d695a9 | 767 | CPUX86State *env = &cpu->env; |
dd673288 | 768 | |
e73223a5 | 769 | env->exception_injected = -1; |
0e607a80 | 770 | env->interrupt_injected = -1; |
1a5e9d2f | 771 | env->xcr0 = 1; |
ddced198 | 772 | if (kvm_irqchip_in_kernel()) { |
dd673288 | 773 | env->mp_state = cpu_is_bsp(cpu) ? KVM_MP_STATE_RUNNABLE : |
ddced198 MT |
774 | KVM_MP_STATE_UNINITIALIZED; |
775 | } else { | |
776 | env->mp_state = KVM_MP_STATE_RUNNABLE; | |
777 | } | |
caa5af0f JK |
778 | } |
779 | ||
e0723c45 PB |
780 | void kvm_arch_do_init_vcpu(X86CPU *cpu) |
781 | { | |
782 | CPUX86State *env = &cpu->env; | |
783 | ||
784 | /* APs get directly into wait-for-SIPI state. */ | |
785 | if (env->mp_state == KVM_MP_STATE_UNINITIALIZED) { | |
786 | env->mp_state = KVM_MP_STATE_INIT_RECEIVED; | |
787 | } | |
788 | } | |
789 | ||
c3a3a7d3 | 790 | static int kvm_get_supported_msrs(KVMState *s) |
05330448 | 791 | { |
75b10c43 | 792 | static int kvm_supported_msrs; |
c3a3a7d3 | 793 | int ret = 0; |
05330448 AL |
794 | |
795 | /* first time */ | |
75b10c43 | 796 | if (kvm_supported_msrs == 0) { |
05330448 AL |
797 | struct kvm_msr_list msr_list, *kvm_msr_list; |
798 | ||
75b10c43 | 799 | kvm_supported_msrs = -1; |
05330448 AL |
800 | |
801 | /* Obtain MSR list from KVM. These are the MSRs that we must | |
802 | * save/restore */ | |
4c9f7372 | 803 | msr_list.nmsrs = 0; |
c3a3a7d3 | 804 | ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, &msr_list); |
6fb6d245 | 805 | if (ret < 0 && ret != -E2BIG) { |
c3a3a7d3 | 806 | return ret; |
6fb6d245 | 807 | } |
d9db889f JK |
808 | /* Old kernel modules had a bug and could write beyond the provided |
809 | memory. Allocate at least a safe amount of 1K. */ | |
7267c094 | 810 | kvm_msr_list = g_malloc0(MAX(1024, sizeof(msr_list) + |
d9db889f JK |
811 | msr_list.nmsrs * |
812 | sizeof(msr_list.indices[0]))); | |
05330448 | 813 | |
55308450 | 814 | kvm_msr_list->nmsrs = msr_list.nmsrs; |
c3a3a7d3 | 815 | ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, kvm_msr_list); |
05330448 AL |
816 | if (ret >= 0) { |
817 | int i; | |
818 | ||
819 | for (i = 0; i < kvm_msr_list->nmsrs; i++) { | |
820 | if (kvm_msr_list->indices[i] == MSR_STAR) { | |
c3a3a7d3 | 821 | has_msr_star = true; |
75b10c43 MT |
822 | continue; |
823 | } | |
824 | if (kvm_msr_list->indices[i] == MSR_VM_HSAVE_PA) { | |
c3a3a7d3 | 825 | has_msr_hsave_pa = true; |
75b10c43 | 826 | continue; |
05330448 | 827 | } |
f28558d3 WA |
828 | if (kvm_msr_list->indices[i] == MSR_TSC_ADJUST) { |
829 | has_msr_tsc_adjust = true; | |
830 | continue; | |
831 | } | |
aa82ba54 LJ |
832 | if (kvm_msr_list->indices[i] == MSR_IA32_TSCDEADLINE) { |
833 | has_msr_tsc_deadline = true; | |
834 | continue; | |
835 | } | |
fc12d72e PB |
836 | if (kvm_msr_list->indices[i] == MSR_IA32_SMBASE) { |
837 | has_msr_smbase = true; | |
838 | continue; | |
839 | } | |
21e87c46 AK |
840 | if (kvm_msr_list->indices[i] == MSR_IA32_MISC_ENABLE) { |
841 | has_msr_misc_enable = true; | |
842 | continue; | |
843 | } | |
79e9ebeb LJ |
844 | if (kvm_msr_list->indices[i] == MSR_IA32_BNDCFGS) { |
845 | has_msr_bndcfgs = true; | |
846 | continue; | |
847 | } | |
18cd2c17 WL |
848 | if (kvm_msr_list->indices[i] == MSR_IA32_XSS) { |
849 | has_msr_xss = true; | |
850 | continue; | |
851 | } | |
f2a53c9e AS |
852 | if (kvm_msr_list->indices[i] == HV_X64_MSR_CRASH_CTL) { |
853 | has_msr_hv_crash = true; | |
854 | continue; | |
855 | } | |
05330448 AL |
856 | } |
857 | } | |
858 | ||
7267c094 | 859 | g_free(kvm_msr_list); |
05330448 AL |
860 | } |
861 | ||
c3a3a7d3 | 862 | return ret; |
05330448 AL |
863 | } |
864 | ||
6410848b PB |
865 | static Notifier smram_machine_done; |
866 | static KVMMemoryListener smram_listener; | |
867 | static AddressSpace smram_address_space; | |
868 | static MemoryRegion smram_as_root; | |
869 | static MemoryRegion smram_as_mem; | |
870 | ||
871 | static void register_smram_listener(Notifier *n, void *unused) | |
872 | { | |
873 | MemoryRegion *smram = | |
874 | (MemoryRegion *) object_resolve_path("/machine/smram", NULL); | |
875 | ||
876 | /* Outer container... */ | |
877 | memory_region_init(&smram_as_root, OBJECT(kvm_state), "mem-container-smram", ~0ull); | |
878 | memory_region_set_enabled(&smram_as_root, true); | |
879 | ||
880 | /* ... with two regions inside: normal system memory with low | |
881 | * priority, and... | |
882 | */ | |
883 | memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram", | |
884 | get_system_memory(), 0, ~0ull); | |
885 | memory_region_add_subregion_overlap(&smram_as_root, 0, &smram_as_mem, 0); | |
886 | memory_region_set_enabled(&smram_as_mem, true); | |
887 | ||
888 | if (smram) { | |
889 | /* ... SMRAM with higher priority */ | |
890 | memory_region_add_subregion_overlap(&smram_as_root, 0, smram, 10); | |
891 | memory_region_set_enabled(smram, true); | |
892 | } | |
893 | ||
894 | address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM"); | |
895 | kvm_memory_listener_register(kvm_state, &smram_listener, | |
896 | &smram_address_space, 1); | |
897 | } | |
898 | ||
b16565b3 | 899 | int kvm_arch_init(MachineState *ms, KVMState *s) |
20420430 | 900 | { |
11076198 | 901 | uint64_t identity_base = 0xfffbc000; |
39d6960a | 902 | uint64_t shadow_mem; |
20420430 | 903 | int ret; |
25d2e361 | 904 | struct utsname utsname; |
20420430 | 905 | |
c3a3a7d3 | 906 | ret = kvm_get_supported_msrs(s); |
20420430 | 907 | if (ret < 0) { |
20420430 SY |
908 | return ret; |
909 | } | |
25d2e361 MT |
910 | |
911 | uname(&utsname); | |
912 | lm_capable_kernel = strcmp(utsname.machine, "x86_64") == 0; | |
913 | ||
4c5b10b7 | 914 | /* |
11076198 JK |
915 | * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly. |
916 | * In order to use vm86 mode, an EPT identity map and a TSS are needed. | |
917 | * Since these must be part of guest physical memory, we need to allocate | |
918 | * them, both by setting their start addresses in the kernel and by | |
919 | * creating a corresponding e820 entry. We need 4 pages before the BIOS. | |
920 | * | |
921 | * Older KVM versions may not support setting the identity map base. In | |
922 | * that case we need to stick with the default, i.e. a 256K maximum BIOS | |
923 | * size. | |
4c5b10b7 | 924 | */ |
11076198 JK |
925 | if (kvm_check_extension(s, KVM_CAP_SET_IDENTITY_MAP_ADDR)) { |
926 | /* Allows up to 16M BIOSes. */ | |
927 | identity_base = 0xfeffc000; | |
928 | ||
929 | ret = kvm_vm_ioctl(s, KVM_SET_IDENTITY_MAP_ADDR, &identity_base); | |
930 | if (ret < 0) { | |
931 | return ret; | |
932 | } | |
4c5b10b7 | 933 | } |
e56ff191 | 934 | |
11076198 JK |
935 | /* Set TSS base one page after EPT identity map. */ |
936 | ret = kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, identity_base + 0x1000); | |
20420430 SY |
937 | if (ret < 0) { |
938 | return ret; | |
939 | } | |
940 | ||
11076198 JK |
941 | /* Tell fw_cfg to notify the BIOS to reserve the range. */ |
942 | ret = e820_add_entry(identity_base, 0x4000, E820_RESERVED); | |
20420430 | 943 | if (ret < 0) { |
11076198 | 944 | fprintf(stderr, "e820_add_entry() table is full\n"); |
20420430 SY |
945 | return ret; |
946 | } | |
3c85e74f | 947 | qemu_register_reset(kvm_unpoison_all, NULL); |
20420430 | 948 | |
4689b77b | 949 | shadow_mem = machine_kvm_shadow_mem(ms); |
36ad0e94 MA |
950 | if (shadow_mem != -1) { |
951 | shadow_mem /= 4096; | |
952 | ret = kvm_vm_ioctl(s, KVM_SET_NR_MMU_PAGES, shadow_mem); | |
953 | if (ret < 0) { | |
954 | return ret; | |
39d6960a JK |
955 | } |
956 | } | |
6410848b PB |
957 | |
958 | if (kvm_check_extension(s, KVM_CAP_X86_SMM)) { | |
959 | smram_machine_done.notify = register_smram_listener; | |
960 | qemu_add_machine_init_done_notifier(&smram_machine_done); | |
961 | } | |
11076198 | 962 | return 0; |
05330448 | 963 | } |
b9bec74b | 964 | |
05330448 AL |
965 | static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs) |
966 | { | |
967 | lhs->selector = rhs->selector; | |
968 | lhs->base = rhs->base; | |
969 | lhs->limit = rhs->limit; | |
970 | lhs->type = 3; | |
971 | lhs->present = 1; | |
972 | lhs->dpl = 3; | |
973 | lhs->db = 0; | |
974 | lhs->s = 1; | |
975 | lhs->l = 0; | |
976 | lhs->g = 0; | |
977 | lhs->avl = 0; | |
978 | lhs->unusable = 0; | |
979 | } | |
980 | ||
981 | static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs) | |
982 | { | |
983 | unsigned flags = rhs->flags; | |
984 | lhs->selector = rhs->selector; | |
985 | lhs->base = rhs->base; | |
986 | lhs->limit = rhs->limit; | |
987 | lhs->type = (flags >> DESC_TYPE_SHIFT) & 15; | |
988 | lhs->present = (flags & DESC_P_MASK) != 0; | |
acaa7550 | 989 | lhs->dpl = (flags >> DESC_DPL_SHIFT) & 3; |
05330448 AL |
990 | lhs->db = (flags >> DESC_B_SHIFT) & 1; |
991 | lhs->s = (flags & DESC_S_MASK) != 0; | |
992 | lhs->l = (flags >> DESC_L_SHIFT) & 1; | |
993 | lhs->g = (flags & DESC_G_MASK) != 0; | |
994 | lhs->avl = (flags & DESC_AVL_MASK) != 0; | |
995 | lhs->unusable = 0; | |
7e680753 | 996 | lhs->padding = 0; |
05330448 AL |
997 | } |
998 | ||
999 | static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs) | |
1000 | { | |
1001 | lhs->selector = rhs->selector; | |
1002 | lhs->base = rhs->base; | |
1003 | lhs->limit = rhs->limit; | |
b9bec74b JK |
1004 | lhs->flags = (rhs->type << DESC_TYPE_SHIFT) | |
1005 | (rhs->present * DESC_P_MASK) | | |
1006 | (rhs->dpl << DESC_DPL_SHIFT) | | |
1007 | (rhs->db << DESC_B_SHIFT) | | |
1008 | (rhs->s * DESC_S_MASK) | | |
1009 | (rhs->l << DESC_L_SHIFT) | | |
1010 | (rhs->g * DESC_G_MASK) | | |
1011 | (rhs->avl * DESC_AVL_MASK); | |
05330448 AL |
1012 | } |
1013 | ||
1014 | static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set) | |
1015 | { | |
b9bec74b | 1016 | if (set) { |
05330448 | 1017 | *kvm_reg = *qemu_reg; |
b9bec74b | 1018 | } else { |
05330448 | 1019 | *qemu_reg = *kvm_reg; |
b9bec74b | 1020 | } |
05330448 AL |
1021 | } |
1022 | ||
1bc22652 | 1023 | static int kvm_getput_regs(X86CPU *cpu, int set) |
05330448 | 1024 | { |
1bc22652 | 1025 | CPUX86State *env = &cpu->env; |
05330448 AL |
1026 | struct kvm_regs regs; |
1027 | int ret = 0; | |
1028 | ||
1029 | if (!set) { | |
1bc22652 | 1030 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_REGS, ®s); |
b9bec74b | 1031 | if (ret < 0) { |
05330448 | 1032 | return ret; |
b9bec74b | 1033 | } |
05330448 AL |
1034 | } |
1035 | ||
1036 | kvm_getput_reg(®s.rax, &env->regs[R_EAX], set); | |
1037 | kvm_getput_reg(®s.rbx, &env->regs[R_EBX], set); | |
1038 | kvm_getput_reg(®s.rcx, &env->regs[R_ECX], set); | |
1039 | kvm_getput_reg(®s.rdx, &env->regs[R_EDX], set); | |
1040 | kvm_getput_reg(®s.rsi, &env->regs[R_ESI], set); | |
1041 | kvm_getput_reg(®s.rdi, &env->regs[R_EDI], set); | |
1042 | kvm_getput_reg(®s.rsp, &env->regs[R_ESP], set); | |
1043 | kvm_getput_reg(®s.rbp, &env->regs[R_EBP], set); | |
1044 | #ifdef TARGET_X86_64 | |
1045 | kvm_getput_reg(®s.r8, &env->regs[8], set); | |
1046 | kvm_getput_reg(®s.r9, &env->regs[9], set); | |
1047 | kvm_getput_reg(®s.r10, &env->regs[10], set); | |
1048 | kvm_getput_reg(®s.r11, &env->regs[11], set); | |
1049 | kvm_getput_reg(®s.r12, &env->regs[12], set); | |
1050 | kvm_getput_reg(®s.r13, &env->regs[13], set); | |
1051 | kvm_getput_reg(®s.r14, &env->regs[14], set); | |
1052 | kvm_getput_reg(®s.r15, &env->regs[15], set); | |
1053 | #endif | |
1054 | ||
1055 | kvm_getput_reg(®s.rflags, &env->eflags, set); | |
1056 | kvm_getput_reg(®s.rip, &env->eip, set); | |
1057 | ||
b9bec74b | 1058 | if (set) { |
1bc22652 | 1059 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_REGS, ®s); |
b9bec74b | 1060 | } |
05330448 AL |
1061 | |
1062 | return ret; | |
1063 | } | |
1064 | ||
1bc22652 | 1065 | static int kvm_put_fpu(X86CPU *cpu) |
05330448 | 1066 | { |
1bc22652 | 1067 | CPUX86State *env = &cpu->env; |
05330448 AL |
1068 | struct kvm_fpu fpu; |
1069 | int i; | |
1070 | ||
1071 | memset(&fpu, 0, sizeof fpu); | |
1072 | fpu.fsw = env->fpus & ~(7 << 11); | |
1073 | fpu.fsw |= (env->fpstt & 7) << 11; | |
1074 | fpu.fcw = env->fpuc; | |
42cc8fa6 JK |
1075 | fpu.last_opcode = env->fpop; |
1076 | fpu.last_ip = env->fpip; | |
1077 | fpu.last_dp = env->fpdp; | |
b9bec74b JK |
1078 | for (i = 0; i < 8; ++i) { |
1079 | fpu.ftwx |= (!env->fptags[i]) << i; | |
1080 | } | |
05330448 | 1081 | memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs); |
bee81887 PB |
1082 | for (i = 0; i < CPU_NB_REGS; i++) { |
1083 | stq_p(&fpu.xmm[i][0], env->xmm_regs[i].XMM_Q(0)); | |
1084 | stq_p(&fpu.xmm[i][8], env->xmm_regs[i].XMM_Q(1)); | |
1085 | } | |
05330448 AL |
1086 | fpu.mxcsr = env->mxcsr; |
1087 | ||
1bc22652 | 1088 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_FPU, &fpu); |
05330448 AL |
1089 | } |
1090 | ||
6b42494b JK |
1091 | #define XSAVE_FCW_FSW 0 |
1092 | #define XSAVE_FTW_FOP 1 | |
f1665b21 SY |
1093 | #define XSAVE_CWD_RIP 2 |
1094 | #define XSAVE_CWD_RDP 4 | |
1095 | #define XSAVE_MXCSR 6 | |
1096 | #define XSAVE_ST_SPACE 8 | |
1097 | #define XSAVE_XMM_SPACE 40 | |
1098 | #define XSAVE_XSTATE_BV 128 | |
1099 | #define XSAVE_YMMH_SPACE 144 | |
79e9ebeb LJ |
1100 | #define XSAVE_BNDREGS 240 |
1101 | #define XSAVE_BNDCSR 256 | |
9aecd6f8 CP |
1102 | #define XSAVE_OPMASK 272 |
1103 | #define XSAVE_ZMM_Hi256 288 | |
1104 | #define XSAVE_Hi16_ZMM 416 | |
f1665b21 | 1105 | |
1bc22652 | 1106 | static int kvm_put_xsave(X86CPU *cpu) |
f1665b21 | 1107 | { |
1bc22652 | 1108 | CPUX86State *env = &cpu->env; |
fabacc0f | 1109 | struct kvm_xsave* xsave = env->kvm_xsave_buf; |
42cc8fa6 | 1110 | uint16_t cwd, swd, twd; |
b7711471 | 1111 | uint8_t *xmm, *ymmh, *zmmh; |
fabacc0f | 1112 | int i, r; |
f1665b21 | 1113 | |
b9bec74b | 1114 | if (!kvm_has_xsave()) { |
1bc22652 | 1115 | return kvm_put_fpu(cpu); |
b9bec74b | 1116 | } |
f1665b21 | 1117 | |
f1665b21 | 1118 | memset(xsave, 0, sizeof(struct kvm_xsave)); |
6115c0a8 | 1119 | twd = 0; |
f1665b21 SY |
1120 | swd = env->fpus & ~(7 << 11); |
1121 | swd |= (env->fpstt & 7) << 11; | |
1122 | cwd = env->fpuc; | |
b9bec74b | 1123 | for (i = 0; i < 8; ++i) { |
f1665b21 | 1124 | twd |= (!env->fptags[i]) << i; |
b9bec74b | 1125 | } |
6b42494b JK |
1126 | xsave->region[XSAVE_FCW_FSW] = (uint32_t)(swd << 16) + cwd; |
1127 | xsave->region[XSAVE_FTW_FOP] = (uint32_t)(env->fpop << 16) + twd; | |
42cc8fa6 JK |
1128 | memcpy(&xsave->region[XSAVE_CWD_RIP], &env->fpip, sizeof(env->fpip)); |
1129 | memcpy(&xsave->region[XSAVE_CWD_RDP], &env->fpdp, sizeof(env->fpdp)); | |
f1665b21 SY |
1130 | memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs, |
1131 | sizeof env->fpregs); | |
f1665b21 SY |
1132 | xsave->region[XSAVE_MXCSR] = env->mxcsr; |
1133 | *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv; | |
79e9ebeb LJ |
1134 | memcpy(&xsave->region[XSAVE_BNDREGS], env->bnd_regs, |
1135 | sizeof env->bnd_regs); | |
1136 | memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs, | |
1137 | sizeof(env->bndcs_regs)); | |
9aecd6f8 CP |
1138 | memcpy(&xsave->region[XSAVE_OPMASK], env->opmask_regs, |
1139 | sizeof env->opmask_regs); | |
bee81887 PB |
1140 | |
1141 | xmm = (uint8_t *)&xsave->region[XSAVE_XMM_SPACE]; | |
b7711471 PB |
1142 | ymmh = (uint8_t *)&xsave->region[XSAVE_YMMH_SPACE]; |
1143 | zmmh = (uint8_t *)&xsave->region[XSAVE_ZMM_Hi256]; | |
1144 | for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) { | |
bee81887 PB |
1145 | stq_p(xmm, env->xmm_regs[i].XMM_Q(0)); |
1146 | stq_p(xmm+8, env->xmm_regs[i].XMM_Q(1)); | |
b7711471 PB |
1147 | stq_p(ymmh, env->xmm_regs[i].XMM_Q(2)); |
1148 | stq_p(ymmh+8, env->xmm_regs[i].XMM_Q(3)); | |
1149 | stq_p(zmmh, env->xmm_regs[i].XMM_Q(4)); | |
1150 | stq_p(zmmh+8, env->xmm_regs[i].XMM_Q(5)); | |
1151 | stq_p(zmmh+16, env->xmm_regs[i].XMM_Q(6)); | |
1152 | stq_p(zmmh+24, env->xmm_regs[i].XMM_Q(7)); | |
bee81887 PB |
1153 | } |
1154 | ||
9aecd6f8 | 1155 | #ifdef TARGET_X86_64 |
b7711471 PB |
1156 | memcpy(&xsave->region[XSAVE_Hi16_ZMM], &env->xmm_regs[16], |
1157 | 16 * sizeof env->xmm_regs[16]); | |
9aecd6f8 | 1158 | #endif |
1bc22652 | 1159 | r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave); |
0f53994f | 1160 | return r; |
f1665b21 SY |
1161 | } |
1162 | ||
1bc22652 | 1163 | static int kvm_put_xcrs(X86CPU *cpu) |
f1665b21 | 1164 | { |
1bc22652 | 1165 | CPUX86State *env = &cpu->env; |
bdfc8480 | 1166 | struct kvm_xcrs xcrs = {}; |
f1665b21 | 1167 | |
b9bec74b | 1168 | if (!kvm_has_xcrs()) { |
f1665b21 | 1169 | return 0; |
b9bec74b | 1170 | } |
f1665b21 SY |
1171 | |
1172 | xcrs.nr_xcrs = 1; | |
1173 | xcrs.flags = 0; | |
1174 | xcrs.xcrs[0].xcr = 0; | |
1175 | xcrs.xcrs[0].value = env->xcr0; | |
1bc22652 | 1176 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XCRS, &xcrs); |
f1665b21 SY |
1177 | } |
1178 | ||
1bc22652 | 1179 | static int kvm_put_sregs(X86CPU *cpu) |
05330448 | 1180 | { |
1bc22652 | 1181 | CPUX86State *env = &cpu->env; |
05330448 AL |
1182 | struct kvm_sregs sregs; |
1183 | ||
0e607a80 JK |
1184 | memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap)); |
1185 | if (env->interrupt_injected >= 0) { | |
1186 | sregs.interrupt_bitmap[env->interrupt_injected / 64] |= | |
1187 | (uint64_t)1 << (env->interrupt_injected % 64); | |
1188 | } | |
05330448 AL |
1189 | |
1190 | if ((env->eflags & VM_MASK)) { | |
b9bec74b JK |
1191 | set_v8086_seg(&sregs.cs, &env->segs[R_CS]); |
1192 | set_v8086_seg(&sregs.ds, &env->segs[R_DS]); | |
1193 | set_v8086_seg(&sregs.es, &env->segs[R_ES]); | |
1194 | set_v8086_seg(&sregs.fs, &env->segs[R_FS]); | |
1195 | set_v8086_seg(&sregs.gs, &env->segs[R_GS]); | |
1196 | set_v8086_seg(&sregs.ss, &env->segs[R_SS]); | |
05330448 | 1197 | } else { |
b9bec74b JK |
1198 | set_seg(&sregs.cs, &env->segs[R_CS]); |
1199 | set_seg(&sregs.ds, &env->segs[R_DS]); | |
1200 | set_seg(&sregs.es, &env->segs[R_ES]); | |
1201 | set_seg(&sregs.fs, &env->segs[R_FS]); | |
1202 | set_seg(&sregs.gs, &env->segs[R_GS]); | |
1203 | set_seg(&sregs.ss, &env->segs[R_SS]); | |
05330448 AL |
1204 | } |
1205 | ||
1206 | set_seg(&sregs.tr, &env->tr); | |
1207 | set_seg(&sregs.ldt, &env->ldt); | |
1208 | ||
1209 | sregs.idt.limit = env->idt.limit; | |
1210 | sregs.idt.base = env->idt.base; | |
7e680753 | 1211 | memset(sregs.idt.padding, 0, sizeof sregs.idt.padding); |
05330448 AL |
1212 | sregs.gdt.limit = env->gdt.limit; |
1213 | sregs.gdt.base = env->gdt.base; | |
7e680753 | 1214 | memset(sregs.gdt.padding, 0, sizeof sregs.gdt.padding); |
05330448 AL |
1215 | |
1216 | sregs.cr0 = env->cr[0]; | |
1217 | sregs.cr2 = env->cr[2]; | |
1218 | sregs.cr3 = env->cr[3]; | |
1219 | sregs.cr4 = env->cr[4]; | |
1220 | ||
02e51483 CF |
1221 | sregs.cr8 = cpu_get_apic_tpr(cpu->apic_state); |
1222 | sregs.apic_base = cpu_get_apic_base(cpu->apic_state); | |
05330448 AL |
1223 | |
1224 | sregs.efer = env->efer; | |
1225 | ||
1bc22652 | 1226 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs); |
05330448 AL |
1227 | } |
1228 | ||
1229 | static void kvm_msr_entry_set(struct kvm_msr_entry *entry, | |
1230 | uint32_t index, uint64_t value) | |
1231 | { | |
1232 | entry->index = index; | |
c7fe4b12 | 1233 | entry->reserved = 0; |
05330448 AL |
1234 | entry->data = value; |
1235 | } | |
1236 | ||
7477cd38 MT |
1237 | static int kvm_put_tscdeadline_msr(X86CPU *cpu) |
1238 | { | |
1239 | CPUX86State *env = &cpu->env; | |
1240 | struct { | |
1241 | struct kvm_msrs info; | |
1242 | struct kvm_msr_entry entries[1]; | |
1243 | } msr_data; | |
1244 | struct kvm_msr_entry *msrs = msr_data.entries; | |
1245 | ||
1246 | if (!has_msr_tsc_deadline) { | |
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | kvm_msr_entry_set(&msrs[0], MSR_IA32_TSCDEADLINE, env->tsc_deadline); | |
1251 | ||
c7fe4b12 CB |
1252 | msr_data.info = (struct kvm_msrs) { |
1253 | .nmsrs = 1, | |
1254 | }; | |
7477cd38 MT |
1255 | |
1256 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data); | |
1257 | } | |
1258 | ||
6bdf863d JK |
1259 | /* |
1260 | * Provide a separate write service for the feature control MSR in order to | |
1261 | * kick the VCPU out of VMXON or even guest mode on reset. This has to be done | |
1262 | * before writing any other state because forcibly leaving nested mode | |
1263 | * invalidates the VCPU state. | |
1264 | */ | |
1265 | static int kvm_put_msr_feature_control(X86CPU *cpu) | |
1266 | { | |
1267 | struct { | |
1268 | struct kvm_msrs info; | |
1269 | struct kvm_msr_entry entry; | |
1270 | } msr_data; | |
1271 | ||
1272 | kvm_msr_entry_set(&msr_data.entry, MSR_IA32_FEATURE_CONTROL, | |
1273 | cpu->env.msr_ia32_feature_control); | |
c7fe4b12 CB |
1274 | |
1275 | msr_data.info = (struct kvm_msrs) { | |
1276 | .nmsrs = 1, | |
1277 | }; | |
1278 | ||
6bdf863d JK |
1279 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data); |
1280 | } | |
1281 | ||
1bc22652 | 1282 | static int kvm_put_msrs(X86CPU *cpu, int level) |
05330448 | 1283 | { |
1bc22652 | 1284 | CPUX86State *env = &cpu->env; |
05330448 AL |
1285 | struct { |
1286 | struct kvm_msrs info; | |
d1ae67f6 | 1287 | struct kvm_msr_entry entries[150]; |
05330448 AL |
1288 | } msr_data; |
1289 | struct kvm_msr_entry *msrs = msr_data.entries; | |
0d894367 | 1290 | int n = 0, i; |
05330448 AL |
1291 | |
1292 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs); | |
1293 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp); | |
1294 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip); | |
0c03266a | 1295 | kvm_msr_entry_set(&msrs[n++], MSR_PAT, env->pat); |
c3a3a7d3 | 1296 | if (has_msr_star) { |
b9bec74b JK |
1297 | kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star); |
1298 | } | |
c3a3a7d3 | 1299 | if (has_msr_hsave_pa) { |
75b10c43 | 1300 | kvm_msr_entry_set(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave); |
b9bec74b | 1301 | } |
f28558d3 WA |
1302 | if (has_msr_tsc_adjust) { |
1303 | kvm_msr_entry_set(&msrs[n++], MSR_TSC_ADJUST, env->tsc_adjust); | |
1304 | } | |
21e87c46 AK |
1305 | if (has_msr_misc_enable) { |
1306 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_MISC_ENABLE, | |
1307 | env->msr_ia32_misc_enable); | |
1308 | } | |
fc12d72e PB |
1309 | if (has_msr_smbase) { |
1310 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_SMBASE, env->smbase); | |
1311 | } | |
439d19f2 PB |
1312 | if (has_msr_bndcfgs) { |
1313 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs); | |
1314 | } | |
18cd2c17 WL |
1315 | if (has_msr_xss) { |
1316 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_XSS, env->xss); | |
1317 | } | |
05330448 | 1318 | #ifdef TARGET_X86_64 |
25d2e361 MT |
1319 | if (lm_capable_kernel) { |
1320 | kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar); | |
1321 | kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase); | |
1322 | kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask); | |
1323 | kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar); | |
1324 | } | |
05330448 | 1325 | #endif |
ff5c186b | 1326 | /* |
0d894367 PB |
1327 | * The following MSRs have side effects on the guest or are too heavy |
1328 | * for normal writeback. Limit them to reset or full state updates. | |
ff5c186b JK |
1329 | */ |
1330 | if (level >= KVM_PUT_RESET_STATE) { | |
0522604b | 1331 | kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc); |
ea643051 JK |
1332 | kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME, |
1333 | env->system_time_msr); | |
1334 | kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr); | |
c5999bfc JK |
1335 | if (has_msr_async_pf_en) { |
1336 | kvm_msr_entry_set(&msrs[n++], MSR_KVM_ASYNC_PF_EN, | |
1337 | env->async_pf_en_msr); | |
1338 | } | |
bc9a839d MT |
1339 | if (has_msr_pv_eoi_en) { |
1340 | kvm_msr_entry_set(&msrs[n++], MSR_KVM_PV_EOI_EN, | |
1341 | env->pv_eoi_en_msr); | |
1342 | } | |
917367aa MT |
1343 | if (has_msr_kvm_steal_time) { |
1344 | kvm_msr_entry_set(&msrs[n++], MSR_KVM_STEAL_TIME, | |
1345 | env->steal_time_msr); | |
1346 | } | |
0d894367 PB |
1347 | if (has_msr_architectural_pmu) { |
1348 | /* Stop the counter. */ | |
1349 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL, 0); | |
1350 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL, 0); | |
1351 | ||
1352 | /* Set the counter values. */ | |
1353 | for (i = 0; i < MAX_FIXED_COUNTERS; i++) { | |
1354 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR0 + i, | |
1355 | env->msr_fixed_counters[i]); | |
1356 | } | |
1357 | for (i = 0; i < num_architectural_pmu_counters; i++) { | |
1358 | kvm_msr_entry_set(&msrs[n++], MSR_P6_PERFCTR0 + i, | |
1359 | env->msr_gp_counters[i]); | |
1360 | kvm_msr_entry_set(&msrs[n++], MSR_P6_EVNTSEL0 + i, | |
1361 | env->msr_gp_evtsel[i]); | |
1362 | } | |
1363 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_STATUS, | |
1364 | env->msr_global_status); | |
1365 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_OVF_CTRL, | |
1366 | env->msr_global_ovf_ctrl); | |
1367 | ||
1368 | /* Now start the PMU. */ | |
1369 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL, | |
1370 | env->msr_fixed_ctr_ctrl); | |
1371 | kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL, | |
1372 | env->msr_global_ctrl); | |
1373 | } | |
7bc3d711 | 1374 | if (has_msr_hv_hypercall) { |
1c90ef26 VR |
1375 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID, |
1376 | env->msr_hv_guest_os_id); | |
1377 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL, | |
1378 | env->msr_hv_hypercall); | |
eab70139 | 1379 | } |
7bc3d711 | 1380 | if (has_msr_hv_vapic) { |
5ef68987 VR |
1381 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE, |
1382 | env->msr_hv_vapic); | |
eab70139 | 1383 | } |
48a5f3bc VR |
1384 | if (has_msr_hv_tsc) { |
1385 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC, | |
1386 | env->msr_hv_tsc); | |
1387 | } | |
f2a53c9e AS |
1388 | if (has_msr_hv_crash) { |
1389 | int j; | |
1390 | ||
1391 | for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++) | |
1392 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_P0 + j, | |
1393 | env->msr_hv_crash_params[j]); | |
1394 | ||
1395 | kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_CTL, | |
1396 | HV_X64_MSR_CRASH_CTL_NOTIFY); | |
1397 | } | |
d1ae67f6 AW |
1398 | if (has_msr_mtrr) { |
1399 | kvm_msr_entry_set(&msrs[n++], MSR_MTRRdefType, env->mtrr_deftype); | |
1400 | kvm_msr_entry_set(&msrs[n++], | |
1401 | MSR_MTRRfix64K_00000, env->mtrr_fixed[0]); | |
1402 | kvm_msr_entry_set(&msrs[n++], | |
1403 | MSR_MTRRfix16K_80000, env->mtrr_fixed[1]); | |
1404 | kvm_msr_entry_set(&msrs[n++], | |
1405 | MSR_MTRRfix16K_A0000, env->mtrr_fixed[2]); | |
1406 | kvm_msr_entry_set(&msrs[n++], | |
1407 | MSR_MTRRfix4K_C0000, env->mtrr_fixed[3]); | |
1408 | kvm_msr_entry_set(&msrs[n++], | |
1409 | MSR_MTRRfix4K_C8000, env->mtrr_fixed[4]); | |
1410 | kvm_msr_entry_set(&msrs[n++], | |
1411 | MSR_MTRRfix4K_D0000, env->mtrr_fixed[5]); | |
1412 | kvm_msr_entry_set(&msrs[n++], | |
1413 | MSR_MTRRfix4K_D8000, env->mtrr_fixed[6]); | |
1414 | kvm_msr_entry_set(&msrs[n++], | |
1415 | MSR_MTRRfix4K_E0000, env->mtrr_fixed[7]); | |
1416 | kvm_msr_entry_set(&msrs[n++], | |
1417 | MSR_MTRRfix4K_E8000, env->mtrr_fixed[8]); | |
1418 | kvm_msr_entry_set(&msrs[n++], | |
1419 | MSR_MTRRfix4K_F0000, env->mtrr_fixed[9]); | |
1420 | kvm_msr_entry_set(&msrs[n++], | |
1421 | MSR_MTRRfix4K_F8000, env->mtrr_fixed[10]); | |
1422 | for (i = 0; i < MSR_MTRRcap_VCNT; i++) { | |
1423 | kvm_msr_entry_set(&msrs[n++], | |
1424 | MSR_MTRRphysBase(i), env->mtrr_var[i].base); | |
1425 | kvm_msr_entry_set(&msrs[n++], | |
1426 | MSR_MTRRphysMask(i), env->mtrr_var[i].mask); | |
1427 | } | |
1428 | } | |
6bdf863d JK |
1429 | |
1430 | /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see | |
1431 | * kvm_put_msr_feature_control. */ | |
ea643051 | 1432 | } |
57780495 | 1433 | if (env->mcg_cap) { |
d8da8574 | 1434 | int i; |
b9bec74b | 1435 | |
c34d440a JK |
1436 | kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status); |
1437 | kvm_msr_entry_set(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl); | |
1438 | for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) { | |
1439 | kvm_msr_entry_set(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]); | |
57780495 MT |
1440 | } |
1441 | } | |
1a03675d | 1442 | |
c7fe4b12 CB |
1443 | msr_data.info = (struct kvm_msrs) { |
1444 | .nmsrs = n, | |
1445 | }; | |
05330448 | 1446 | |
1bc22652 | 1447 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data); |
05330448 AL |
1448 | |
1449 | } | |
1450 | ||
1451 | ||
1bc22652 | 1452 | static int kvm_get_fpu(X86CPU *cpu) |
05330448 | 1453 | { |
1bc22652 | 1454 | CPUX86State *env = &cpu->env; |
05330448 AL |
1455 | struct kvm_fpu fpu; |
1456 | int i, ret; | |
1457 | ||
1bc22652 | 1458 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_FPU, &fpu); |
b9bec74b | 1459 | if (ret < 0) { |
05330448 | 1460 | return ret; |
b9bec74b | 1461 | } |
05330448 AL |
1462 | |
1463 | env->fpstt = (fpu.fsw >> 11) & 7; | |
1464 | env->fpus = fpu.fsw; | |
1465 | env->fpuc = fpu.fcw; | |
42cc8fa6 JK |
1466 | env->fpop = fpu.last_opcode; |
1467 | env->fpip = fpu.last_ip; | |
1468 | env->fpdp = fpu.last_dp; | |
b9bec74b JK |
1469 | for (i = 0; i < 8; ++i) { |
1470 | env->fptags[i] = !((fpu.ftwx >> i) & 1); | |
1471 | } | |
05330448 | 1472 | memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs); |
bee81887 PB |
1473 | for (i = 0; i < CPU_NB_REGS; i++) { |
1474 | env->xmm_regs[i].XMM_Q(0) = ldq_p(&fpu.xmm[i][0]); | |
1475 | env->xmm_regs[i].XMM_Q(1) = ldq_p(&fpu.xmm[i][8]); | |
1476 | } | |
05330448 AL |
1477 | env->mxcsr = fpu.mxcsr; |
1478 | ||
1479 | return 0; | |
1480 | } | |
1481 | ||
1bc22652 | 1482 | static int kvm_get_xsave(X86CPU *cpu) |
f1665b21 | 1483 | { |
1bc22652 | 1484 | CPUX86State *env = &cpu->env; |
fabacc0f | 1485 | struct kvm_xsave* xsave = env->kvm_xsave_buf; |
f1665b21 | 1486 | int ret, i; |
b7711471 | 1487 | const uint8_t *xmm, *ymmh, *zmmh; |
42cc8fa6 | 1488 | uint16_t cwd, swd, twd; |
f1665b21 | 1489 | |
b9bec74b | 1490 | if (!kvm_has_xsave()) { |
1bc22652 | 1491 | return kvm_get_fpu(cpu); |
b9bec74b | 1492 | } |
f1665b21 | 1493 | |
1bc22652 | 1494 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XSAVE, xsave); |
0f53994f | 1495 | if (ret < 0) { |
f1665b21 | 1496 | return ret; |
0f53994f | 1497 | } |
f1665b21 | 1498 | |
6b42494b JK |
1499 | cwd = (uint16_t)xsave->region[XSAVE_FCW_FSW]; |
1500 | swd = (uint16_t)(xsave->region[XSAVE_FCW_FSW] >> 16); | |
1501 | twd = (uint16_t)xsave->region[XSAVE_FTW_FOP]; | |
1502 | env->fpop = (uint16_t)(xsave->region[XSAVE_FTW_FOP] >> 16); | |
f1665b21 SY |
1503 | env->fpstt = (swd >> 11) & 7; |
1504 | env->fpus = swd; | |
1505 | env->fpuc = cwd; | |
b9bec74b | 1506 | for (i = 0; i < 8; ++i) { |
f1665b21 | 1507 | env->fptags[i] = !((twd >> i) & 1); |
b9bec74b | 1508 | } |
42cc8fa6 JK |
1509 | memcpy(&env->fpip, &xsave->region[XSAVE_CWD_RIP], sizeof(env->fpip)); |
1510 | memcpy(&env->fpdp, &xsave->region[XSAVE_CWD_RDP], sizeof(env->fpdp)); | |
f1665b21 SY |
1511 | env->mxcsr = xsave->region[XSAVE_MXCSR]; |
1512 | memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE], | |
1513 | sizeof env->fpregs); | |
f1665b21 | 1514 | env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV]; |
79e9ebeb LJ |
1515 | memcpy(env->bnd_regs, &xsave->region[XSAVE_BNDREGS], |
1516 | sizeof env->bnd_regs); | |
1517 | memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR], | |
1518 | sizeof(env->bndcs_regs)); | |
9aecd6f8 CP |
1519 | memcpy(env->opmask_regs, &xsave->region[XSAVE_OPMASK], |
1520 | sizeof env->opmask_regs); | |
bee81887 PB |
1521 | |
1522 | xmm = (const uint8_t *)&xsave->region[XSAVE_XMM_SPACE]; | |
b7711471 PB |
1523 | ymmh = (const uint8_t *)&xsave->region[XSAVE_YMMH_SPACE]; |
1524 | zmmh = (const uint8_t *)&xsave->region[XSAVE_ZMM_Hi256]; | |
1525 | for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) { | |
bee81887 PB |
1526 | env->xmm_regs[i].XMM_Q(0) = ldq_p(xmm); |
1527 | env->xmm_regs[i].XMM_Q(1) = ldq_p(xmm+8); | |
b7711471 PB |
1528 | env->xmm_regs[i].XMM_Q(2) = ldq_p(ymmh); |
1529 | env->xmm_regs[i].XMM_Q(3) = ldq_p(ymmh+8); | |
1530 | env->xmm_regs[i].XMM_Q(4) = ldq_p(zmmh); | |
1531 | env->xmm_regs[i].XMM_Q(5) = ldq_p(zmmh+8); | |
1532 | env->xmm_regs[i].XMM_Q(6) = ldq_p(zmmh+16); | |
1533 | env->xmm_regs[i].XMM_Q(7) = ldq_p(zmmh+24); | |
bee81887 PB |
1534 | } |
1535 | ||
9aecd6f8 | 1536 | #ifdef TARGET_X86_64 |
b7711471 PB |
1537 | memcpy(&env->xmm_regs[16], &xsave->region[XSAVE_Hi16_ZMM], |
1538 | 16 * sizeof env->xmm_regs[16]); | |
9aecd6f8 | 1539 | #endif |
f1665b21 | 1540 | return 0; |
f1665b21 SY |
1541 | } |
1542 | ||
1bc22652 | 1543 | static int kvm_get_xcrs(X86CPU *cpu) |
f1665b21 | 1544 | { |
1bc22652 | 1545 | CPUX86State *env = &cpu->env; |
f1665b21 SY |
1546 | int i, ret; |
1547 | struct kvm_xcrs xcrs; | |
1548 | ||
b9bec74b | 1549 | if (!kvm_has_xcrs()) { |
f1665b21 | 1550 | return 0; |
b9bec74b | 1551 | } |
f1665b21 | 1552 | |
1bc22652 | 1553 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XCRS, &xcrs); |
b9bec74b | 1554 | if (ret < 0) { |
f1665b21 | 1555 | return ret; |
b9bec74b | 1556 | } |
f1665b21 | 1557 | |
b9bec74b | 1558 | for (i = 0; i < xcrs.nr_xcrs; i++) { |
f1665b21 | 1559 | /* Only support xcr0 now */ |
0fd53fec PB |
1560 | if (xcrs.xcrs[i].xcr == 0) { |
1561 | env->xcr0 = xcrs.xcrs[i].value; | |
f1665b21 SY |
1562 | break; |
1563 | } | |
b9bec74b | 1564 | } |
f1665b21 | 1565 | return 0; |
f1665b21 SY |
1566 | } |
1567 | ||
1bc22652 | 1568 | static int kvm_get_sregs(X86CPU *cpu) |
05330448 | 1569 | { |
1bc22652 | 1570 | CPUX86State *env = &cpu->env; |
05330448 AL |
1571 | struct kvm_sregs sregs; |
1572 | uint32_t hflags; | |
0e607a80 | 1573 | int bit, i, ret; |
05330448 | 1574 | |
1bc22652 | 1575 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs); |
b9bec74b | 1576 | if (ret < 0) { |
05330448 | 1577 | return ret; |
b9bec74b | 1578 | } |
05330448 | 1579 | |
0e607a80 JK |
1580 | /* There can only be one pending IRQ set in the bitmap at a time, so try |
1581 | to find it and save its number instead (-1 for none). */ | |
1582 | env->interrupt_injected = -1; | |
1583 | for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) { | |
1584 | if (sregs.interrupt_bitmap[i]) { | |
1585 | bit = ctz64(sregs.interrupt_bitmap[i]); | |
1586 | env->interrupt_injected = i * 64 + bit; | |
1587 | break; | |
1588 | } | |
1589 | } | |
05330448 AL |
1590 | |
1591 | get_seg(&env->segs[R_CS], &sregs.cs); | |
1592 | get_seg(&env->segs[R_DS], &sregs.ds); | |
1593 | get_seg(&env->segs[R_ES], &sregs.es); | |
1594 | get_seg(&env->segs[R_FS], &sregs.fs); | |
1595 | get_seg(&env->segs[R_GS], &sregs.gs); | |
1596 | get_seg(&env->segs[R_SS], &sregs.ss); | |
1597 | ||
1598 | get_seg(&env->tr, &sregs.tr); | |
1599 | get_seg(&env->ldt, &sregs.ldt); | |
1600 | ||
1601 | env->idt.limit = sregs.idt.limit; | |
1602 | env->idt.base = sregs.idt.base; | |
1603 | env->gdt.limit = sregs.gdt.limit; | |
1604 | env->gdt.base = sregs.gdt.base; | |
1605 | ||
1606 | env->cr[0] = sregs.cr0; | |
1607 | env->cr[2] = sregs.cr2; | |
1608 | env->cr[3] = sregs.cr3; | |
1609 | env->cr[4] = sregs.cr4; | |
1610 | ||
05330448 | 1611 | env->efer = sregs.efer; |
cce47516 JK |
1612 | |
1613 | /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */ | |
05330448 | 1614 | |
b9bec74b JK |
1615 | #define HFLAG_COPY_MASK \ |
1616 | ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \ | |
1617 | HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \ | |
1618 | HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \ | |
1619 | HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK) | |
05330448 | 1620 | |
7125c937 | 1621 | hflags = (env->segs[R_SS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK; |
05330448 AL |
1622 | hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT); |
1623 | hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) & | |
b9bec74b | 1624 | (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK); |
05330448 AL |
1625 | hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK)); |
1626 | hflags |= (env->cr[4] & CR4_OSFXSR_MASK) << | |
b9bec74b | 1627 | (HF_OSFXSR_SHIFT - CR4_OSFXSR_SHIFT); |
05330448 AL |
1628 | |
1629 | if (env->efer & MSR_EFER_LMA) { | |
1630 | hflags |= HF_LMA_MASK; | |
1631 | } | |
1632 | ||
1633 | if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) { | |
1634 | hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK; | |
1635 | } else { | |
1636 | hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >> | |
b9bec74b | 1637 | (DESC_B_SHIFT - HF_CS32_SHIFT); |
05330448 | 1638 | hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >> |
b9bec74b JK |
1639 | (DESC_B_SHIFT - HF_SS32_SHIFT); |
1640 | if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK) || | |
1641 | !(hflags & HF_CS32_MASK)) { | |
1642 | hflags |= HF_ADDSEG_MASK; | |
1643 | } else { | |
1644 | hflags |= ((env->segs[R_DS].base | env->segs[R_ES].base | | |
1645 | env->segs[R_SS].base) != 0) << HF_ADDSEG_SHIFT; | |
1646 | } | |
05330448 AL |
1647 | } |
1648 | env->hflags = (env->hflags & HFLAG_COPY_MASK) | hflags; | |
05330448 AL |
1649 | |
1650 | return 0; | |
1651 | } | |
1652 | ||
1bc22652 | 1653 | static int kvm_get_msrs(X86CPU *cpu) |
05330448 | 1654 | { |
1bc22652 | 1655 | CPUX86State *env = &cpu->env; |
05330448 AL |
1656 | struct { |
1657 | struct kvm_msrs info; | |
d1ae67f6 | 1658 | struct kvm_msr_entry entries[150]; |
05330448 AL |
1659 | } msr_data; |
1660 | struct kvm_msr_entry *msrs = msr_data.entries; | |
1661 | int ret, i, n; | |
1662 | ||
1663 | n = 0; | |
1664 | msrs[n++].index = MSR_IA32_SYSENTER_CS; | |
1665 | msrs[n++].index = MSR_IA32_SYSENTER_ESP; | |
1666 | msrs[n++].index = MSR_IA32_SYSENTER_EIP; | |
0c03266a | 1667 | msrs[n++].index = MSR_PAT; |
c3a3a7d3 | 1668 | if (has_msr_star) { |
b9bec74b JK |
1669 | msrs[n++].index = MSR_STAR; |
1670 | } | |
c3a3a7d3 | 1671 | if (has_msr_hsave_pa) { |
75b10c43 | 1672 | msrs[n++].index = MSR_VM_HSAVE_PA; |
b9bec74b | 1673 | } |
f28558d3 WA |
1674 | if (has_msr_tsc_adjust) { |
1675 | msrs[n++].index = MSR_TSC_ADJUST; | |
1676 | } | |
aa82ba54 LJ |
1677 | if (has_msr_tsc_deadline) { |
1678 | msrs[n++].index = MSR_IA32_TSCDEADLINE; | |
1679 | } | |
21e87c46 AK |
1680 | if (has_msr_misc_enable) { |
1681 | msrs[n++].index = MSR_IA32_MISC_ENABLE; | |
1682 | } | |
fc12d72e PB |
1683 | if (has_msr_smbase) { |
1684 | msrs[n++].index = MSR_IA32_SMBASE; | |
1685 | } | |
df67696e LJ |
1686 | if (has_msr_feature_control) { |
1687 | msrs[n++].index = MSR_IA32_FEATURE_CONTROL; | |
1688 | } | |
79e9ebeb LJ |
1689 | if (has_msr_bndcfgs) { |
1690 | msrs[n++].index = MSR_IA32_BNDCFGS; | |
1691 | } | |
18cd2c17 WL |
1692 | if (has_msr_xss) { |
1693 | msrs[n++].index = MSR_IA32_XSS; | |
1694 | } | |
1695 | ||
b8cc45d6 GC |
1696 | |
1697 | if (!env->tsc_valid) { | |
1698 | msrs[n++].index = MSR_IA32_TSC; | |
1354869c | 1699 | env->tsc_valid = !runstate_is_running(); |
b8cc45d6 GC |
1700 | } |
1701 | ||
05330448 | 1702 | #ifdef TARGET_X86_64 |
25d2e361 MT |
1703 | if (lm_capable_kernel) { |
1704 | msrs[n++].index = MSR_CSTAR; | |
1705 | msrs[n++].index = MSR_KERNELGSBASE; | |
1706 | msrs[n++].index = MSR_FMASK; | |
1707 | msrs[n++].index = MSR_LSTAR; | |
1708 | } | |
05330448 | 1709 | #endif |
1a03675d GC |
1710 | msrs[n++].index = MSR_KVM_SYSTEM_TIME; |
1711 | msrs[n++].index = MSR_KVM_WALL_CLOCK; | |
c5999bfc JK |
1712 | if (has_msr_async_pf_en) { |
1713 | msrs[n++].index = MSR_KVM_ASYNC_PF_EN; | |
1714 | } | |
bc9a839d MT |
1715 | if (has_msr_pv_eoi_en) { |
1716 | msrs[n++].index = MSR_KVM_PV_EOI_EN; | |
1717 | } | |
917367aa MT |
1718 | if (has_msr_kvm_steal_time) { |
1719 | msrs[n++].index = MSR_KVM_STEAL_TIME; | |
1720 | } | |
0d894367 PB |
1721 | if (has_msr_architectural_pmu) { |
1722 | msrs[n++].index = MSR_CORE_PERF_FIXED_CTR_CTRL; | |
1723 | msrs[n++].index = MSR_CORE_PERF_GLOBAL_CTRL; | |
1724 | msrs[n++].index = MSR_CORE_PERF_GLOBAL_STATUS; | |
1725 | msrs[n++].index = MSR_CORE_PERF_GLOBAL_OVF_CTRL; | |
1726 | for (i = 0; i < MAX_FIXED_COUNTERS; i++) { | |
1727 | msrs[n++].index = MSR_CORE_PERF_FIXED_CTR0 + i; | |
1728 | } | |
1729 | for (i = 0; i < num_architectural_pmu_counters; i++) { | |
1730 | msrs[n++].index = MSR_P6_PERFCTR0 + i; | |
1731 | msrs[n++].index = MSR_P6_EVNTSEL0 + i; | |
1732 | } | |
1733 | } | |
1a03675d | 1734 | |
57780495 MT |
1735 | if (env->mcg_cap) { |
1736 | msrs[n++].index = MSR_MCG_STATUS; | |
1737 | msrs[n++].index = MSR_MCG_CTL; | |
b9bec74b | 1738 | for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) { |
57780495 | 1739 | msrs[n++].index = MSR_MC0_CTL + i; |
b9bec74b | 1740 | } |
57780495 | 1741 | } |
57780495 | 1742 | |
1c90ef26 VR |
1743 | if (has_msr_hv_hypercall) { |
1744 | msrs[n++].index = HV_X64_MSR_HYPERCALL; | |
1745 | msrs[n++].index = HV_X64_MSR_GUEST_OS_ID; | |
1746 | } | |
5ef68987 VR |
1747 | if (has_msr_hv_vapic) { |
1748 | msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE; | |
1749 | } | |
48a5f3bc VR |
1750 | if (has_msr_hv_tsc) { |
1751 | msrs[n++].index = HV_X64_MSR_REFERENCE_TSC; | |
1752 | } | |
f2a53c9e AS |
1753 | if (has_msr_hv_crash) { |
1754 | int j; | |
1755 | ||
1756 | for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++) { | |
1757 | msrs[n++].index = HV_X64_MSR_CRASH_P0 + j; | |
1758 | } | |
1759 | } | |
d1ae67f6 AW |
1760 | if (has_msr_mtrr) { |
1761 | msrs[n++].index = MSR_MTRRdefType; | |
1762 | msrs[n++].index = MSR_MTRRfix64K_00000; | |
1763 | msrs[n++].index = MSR_MTRRfix16K_80000; | |
1764 | msrs[n++].index = MSR_MTRRfix16K_A0000; | |
1765 | msrs[n++].index = MSR_MTRRfix4K_C0000; | |
1766 | msrs[n++].index = MSR_MTRRfix4K_C8000; | |
1767 | msrs[n++].index = MSR_MTRRfix4K_D0000; | |
1768 | msrs[n++].index = MSR_MTRRfix4K_D8000; | |
1769 | msrs[n++].index = MSR_MTRRfix4K_E0000; | |
1770 | msrs[n++].index = MSR_MTRRfix4K_E8000; | |
1771 | msrs[n++].index = MSR_MTRRfix4K_F0000; | |
1772 | msrs[n++].index = MSR_MTRRfix4K_F8000; | |
1773 | for (i = 0; i < MSR_MTRRcap_VCNT; i++) { | |
1774 | msrs[n++].index = MSR_MTRRphysBase(i); | |
1775 | msrs[n++].index = MSR_MTRRphysMask(i); | |
1776 | } | |
1777 | } | |
5ef68987 | 1778 | |
d19ae73e CB |
1779 | msr_data.info = (struct kvm_msrs) { |
1780 | .nmsrs = n, | |
1781 | }; | |
1782 | ||
1bc22652 | 1783 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data); |
b9bec74b | 1784 | if (ret < 0) { |
05330448 | 1785 | return ret; |
b9bec74b | 1786 | } |
05330448 AL |
1787 | |
1788 | for (i = 0; i < ret; i++) { | |
0d894367 PB |
1789 | uint32_t index = msrs[i].index; |
1790 | switch (index) { | |
05330448 AL |
1791 | case MSR_IA32_SYSENTER_CS: |
1792 | env->sysenter_cs = msrs[i].data; | |
1793 | break; | |
1794 | case MSR_IA32_SYSENTER_ESP: | |
1795 | env->sysenter_esp = msrs[i].data; | |
1796 | break; | |
1797 | case MSR_IA32_SYSENTER_EIP: | |
1798 | env->sysenter_eip = msrs[i].data; | |
1799 | break; | |
0c03266a JK |
1800 | case MSR_PAT: |
1801 | env->pat = msrs[i].data; | |
1802 | break; | |
05330448 AL |
1803 | case MSR_STAR: |
1804 | env->star = msrs[i].data; | |
1805 | break; | |
1806 | #ifdef TARGET_X86_64 | |
1807 | case MSR_CSTAR: | |
1808 | env->cstar = msrs[i].data; | |
1809 | break; | |
1810 | case MSR_KERNELGSBASE: | |
1811 | env->kernelgsbase = msrs[i].data; | |
1812 | break; | |
1813 | case MSR_FMASK: | |
1814 | env->fmask = msrs[i].data; | |
1815 | break; | |
1816 | case MSR_LSTAR: | |
1817 | env->lstar = msrs[i].data; | |
1818 | break; | |
1819 | #endif | |
1820 | case MSR_IA32_TSC: | |
1821 | env->tsc = msrs[i].data; | |
1822 | break; | |
f28558d3 WA |
1823 | case MSR_TSC_ADJUST: |
1824 | env->tsc_adjust = msrs[i].data; | |
1825 | break; | |
aa82ba54 LJ |
1826 | case MSR_IA32_TSCDEADLINE: |
1827 | env->tsc_deadline = msrs[i].data; | |
1828 | break; | |
aa851e36 MT |
1829 | case MSR_VM_HSAVE_PA: |
1830 | env->vm_hsave = msrs[i].data; | |
1831 | break; | |
1a03675d GC |
1832 | case MSR_KVM_SYSTEM_TIME: |
1833 | env->system_time_msr = msrs[i].data; | |
1834 | break; | |
1835 | case MSR_KVM_WALL_CLOCK: | |
1836 | env->wall_clock_msr = msrs[i].data; | |
1837 | break; | |
57780495 MT |
1838 | case MSR_MCG_STATUS: |
1839 | env->mcg_status = msrs[i].data; | |
1840 | break; | |
1841 | case MSR_MCG_CTL: | |
1842 | env->mcg_ctl = msrs[i].data; | |
1843 | break; | |
21e87c46 AK |
1844 | case MSR_IA32_MISC_ENABLE: |
1845 | env->msr_ia32_misc_enable = msrs[i].data; | |
1846 | break; | |
fc12d72e PB |
1847 | case MSR_IA32_SMBASE: |
1848 | env->smbase = msrs[i].data; | |
1849 | break; | |
0779caeb ACL |
1850 | case MSR_IA32_FEATURE_CONTROL: |
1851 | env->msr_ia32_feature_control = msrs[i].data; | |
df67696e | 1852 | break; |
79e9ebeb LJ |
1853 | case MSR_IA32_BNDCFGS: |
1854 | env->msr_bndcfgs = msrs[i].data; | |
1855 | break; | |
18cd2c17 WL |
1856 | case MSR_IA32_XSS: |
1857 | env->xss = msrs[i].data; | |
1858 | break; | |
57780495 | 1859 | default: |
57780495 MT |
1860 | if (msrs[i].index >= MSR_MC0_CTL && |
1861 | msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) { | |
1862 | env->mce_banks[msrs[i].index - MSR_MC0_CTL] = msrs[i].data; | |
57780495 | 1863 | } |
d8da8574 | 1864 | break; |
f6584ee2 GN |
1865 | case MSR_KVM_ASYNC_PF_EN: |
1866 | env->async_pf_en_msr = msrs[i].data; | |
1867 | break; | |
bc9a839d MT |
1868 | case MSR_KVM_PV_EOI_EN: |
1869 | env->pv_eoi_en_msr = msrs[i].data; | |
1870 | break; | |
917367aa MT |
1871 | case MSR_KVM_STEAL_TIME: |
1872 | env->steal_time_msr = msrs[i].data; | |
1873 | break; | |
0d894367 PB |
1874 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
1875 | env->msr_fixed_ctr_ctrl = msrs[i].data; | |
1876 | break; | |
1877 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1878 | env->msr_global_ctrl = msrs[i].data; | |
1879 | break; | |
1880 | case MSR_CORE_PERF_GLOBAL_STATUS: | |
1881 | env->msr_global_status = msrs[i].data; | |
1882 | break; | |
1883 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: | |
1884 | env->msr_global_ovf_ctrl = msrs[i].data; | |
1885 | break; | |
1886 | case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR0 + MAX_FIXED_COUNTERS - 1: | |
1887 | env->msr_fixed_counters[index - MSR_CORE_PERF_FIXED_CTR0] = msrs[i].data; | |
1888 | break; | |
1889 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR0 + MAX_GP_COUNTERS - 1: | |
1890 | env->msr_gp_counters[index - MSR_P6_PERFCTR0] = msrs[i].data; | |
1891 | break; | |
1892 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL0 + MAX_GP_COUNTERS - 1: | |
1893 | env->msr_gp_evtsel[index - MSR_P6_EVNTSEL0] = msrs[i].data; | |
1894 | break; | |
1c90ef26 VR |
1895 | case HV_X64_MSR_HYPERCALL: |
1896 | env->msr_hv_hypercall = msrs[i].data; | |
1897 | break; | |
1898 | case HV_X64_MSR_GUEST_OS_ID: | |
1899 | env->msr_hv_guest_os_id = msrs[i].data; | |
1900 | break; | |
5ef68987 VR |
1901 | case HV_X64_MSR_APIC_ASSIST_PAGE: |
1902 | env->msr_hv_vapic = msrs[i].data; | |
1903 | break; | |
48a5f3bc VR |
1904 | case HV_X64_MSR_REFERENCE_TSC: |
1905 | env->msr_hv_tsc = msrs[i].data; | |
1906 | break; | |
f2a53c9e AS |
1907 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
1908 | env->msr_hv_crash_params[index - HV_X64_MSR_CRASH_P0] = msrs[i].data; | |
1909 | break; | |
d1ae67f6 AW |
1910 | case MSR_MTRRdefType: |
1911 | env->mtrr_deftype = msrs[i].data; | |
1912 | break; | |
1913 | case MSR_MTRRfix64K_00000: | |
1914 | env->mtrr_fixed[0] = msrs[i].data; | |
1915 | break; | |
1916 | case MSR_MTRRfix16K_80000: | |
1917 | env->mtrr_fixed[1] = msrs[i].data; | |
1918 | break; | |
1919 | case MSR_MTRRfix16K_A0000: | |
1920 | env->mtrr_fixed[2] = msrs[i].data; | |
1921 | break; | |
1922 | case MSR_MTRRfix4K_C0000: | |
1923 | env->mtrr_fixed[3] = msrs[i].data; | |
1924 | break; | |
1925 | case MSR_MTRRfix4K_C8000: | |
1926 | env->mtrr_fixed[4] = msrs[i].data; | |
1927 | break; | |
1928 | case MSR_MTRRfix4K_D0000: | |
1929 | env->mtrr_fixed[5] = msrs[i].data; | |
1930 | break; | |
1931 | case MSR_MTRRfix4K_D8000: | |
1932 | env->mtrr_fixed[6] = msrs[i].data; | |
1933 | break; | |
1934 | case MSR_MTRRfix4K_E0000: | |
1935 | env->mtrr_fixed[7] = msrs[i].data; | |
1936 | break; | |
1937 | case MSR_MTRRfix4K_E8000: | |
1938 | env->mtrr_fixed[8] = msrs[i].data; | |
1939 | break; | |
1940 | case MSR_MTRRfix4K_F0000: | |
1941 | env->mtrr_fixed[9] = msrs[i].data; | |
1942 | break; | |
1943 | case MSR_MTRRfix4K_F8000: | |
1944 | env->mtrr_fixed[10] = msrs[i].data; | |
1945 | break; | |
1946 | case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT - 1): | |
1947 | if (index & 1) { | |
1948 | env->mtrr_var[MSR_MTRRphysIndex(index)].mask = msrs[i].data; | |
1949 | } else { | |
1950 | env->mtrr_var[MSR_MTRRphysIndex(index)].base = msrs[i].data; | |
1951 | } | |
1952 | break; | |
05330448 AL |
1953 | } |
1954 | } | |
1955 | ||
1956 | return 0; | |
1957 | } | |
1958 | ||
1bc22652 | 1959 | static int kvm_put_mp_state(X86CPU *cpu) |
9bdbe550 | 1960 | { |
1bc22652 | 1961 | struct kvm_mp_state mp_state = { .mp_state = cpu->env.mp_state }; |
9bdbe550 | 1962 | |
1bc22652 | 1963 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); |
9bdbe550 HB |
1964 | } |
1965 | ||
23d02d9b | 1966 | static int kvm_get_mp_state(X86CPU *cpu) |
9bdbe550 | 1967 | { |
259186a7 | 1968 | CPUState *cs = CPU(cpu); |
23d02d9b | 1969 | CPUX86State *env = &cpu->env; |
9bdbe550 HB |
1970 | struct kvm_mp_state mp_state; |
1971 | int ret; | |
1972 | ||
259186a7 | 1973 | ret = kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state); |
9bdbe550 HB |
1974 | if (ret < 0) { |
1975 | return ret; | |
1976 | } | |
1977 | env->mp_state = mp_state.mp_state; | |
c14750e8 | 1978 | if (kvm_irqchip_in_kernel()) { |
259186a7 | 1979 | cs->halted = (mp_state.mp_state == KVM_MP_STATE_HALTED); |
c14750e8 | 1980 | } |
9bdbe550 HB |
1981 | return 0; |
1982 | } | |
1983 | ||
1bc22652 | 1984 | static int kvm_get_apic(X86CPU *cpu) |
680c1c6f | 1985 | { |
02e51483 | 1986 | DeviceState *apic = cpu->apic_state; |
680c1c6f JK |
1987 | struct kvm_lapic_state kapic; |
1988 | int ret; | |
1989 | ||
3d4b2649 | 1990 | if (apic && kvm_irqchip_in_kernel()) { |
1bc22652 | 1991 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_LAPIC, &kapic); |
680c1c6f JK |
1992 | if (ret < 0) { |
1993 | return ret; | |
1994 | } | |
1995 | ||
1996 | kvm_get_apic_state(apic, &kapic); | |
1997 | } | |
1998 | return 0; | |
1999 | } | |
2000 | ||
1bc22652 | 2001 | static int kvm_put_apic(X86CPU *cpu) |
680c1c6f | 2002 | { |
02e51483 | 2003 | DeviceState *apic = cpu->apic_state; |
680c1c6f JK |
2004 | struct kvm_lapic_state kapic; |
2005 | ||
3d4b2649 | 2006 | if (apic && kvm_irqchip_in_kernel()) { |
680c1c6f JK |
2007 | kvm_put_apic_state(apic, &kapic); |
2008 | ||
1bc22652 | 2009 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_LAPIC, &kapic); |
680c1c6f JK |
2010 | } |
2011 | return 0; | |
2012 | } | |
2013 | ||
1bc22652 | 2014 | static int kvm_put_vcpu_events(X86CPU *cpu, int level) |
a0fb002c | 2015 | { |
fc12d72e | 2016 | CPUState *cs = CPU(cpu); |
1bc22652 | 2017 | CPUX86State *env = &cpu->env; |
076796f8 | 2018 | struct kvm_vcpu_events events = {}; |
a0fb002c JK |
2019 | |
2020 | if (!kvm_has_vcpu_events()) { | |
2021 | return 0; | |
2022 | } | |
2023 | ||
31827373 JK |
2024 | events.exception.injected = (env->exception_injected >= 0); |
2025 | events.exception.nr = env->exception_injected; | |
a0fb002c JK |
2026 | events.exception.has_error_code = env->has_error_code; |
2027 | events.exception.error_code = env->error_code; | |
7e680753 | 2028 | events.exception.pad = 0; |
a0fb002c JK |
2029 | |
2030 | events.interrupt.injected = (env->interrupt_injected >= 0); | |
2031 | events.interrupt.nr = env->interrupt_injected; | |
2032 | events.interrupt.soft = env->soft_interrupt; | |
2033 | ||
2034 | events.nmi.injected = env->nmi_injected; | |
2035 | events.nmi.pending = env->nmi_pending; | |
2036 | events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK); | |
7e680753 | 2037 | events.nmi.pad = 0; |
a0fb002c JK |
2038 | |
2039 | events.sipi_vector = env->sipi_vector; | |
2040 | ||
fc12d72e PB |
2041 | if (has_msr_smbase) { |
2042 | events.smi.smm = !!(env->hflags & HF_SMM_MASK); | |
2043 | events.smi.smm_inside_nmi = !!(env->hflags2 & HF2_SMM_INSIDE_NMI_MASK); | |
2044 | if (kvm_irqchip_in_kernel()) { | |
2045 | /* As soon as these are moved to the kernel, remove them | |
2046 | * from cs->interrupt_request. | |
2047 | */ | |
2048 | events.smi.pending = cs->interrupt_request & CPU_INTERRUPT_SMI; | |
2049 | events.smi.latched_init = cs->interrupt_request & CPU_INTERRUPT_INIT; | |
2050 | cs->interrupt_request &= ~(CPU_INTERRUPT_INIT | CPU_INTERRUPT_SMI); | |
2051 | } else { | |
2052 | /* Keep these in cs->interrupt_request. */ | |
2053 | events.smi.pending = 0; | |
2054 | events.smi.latched_init = 0; | |
2055 | } | |
2056 | events.flags |= KVM_VCPUEVENT_VALID_SMM; | |
2057 | } | |
2058 | ||
ea643051 JK |
2059 | events.flags = 0; |
2060 | if (level >= KVM_PUT_RESET_STATE) { | |
2061 | events.flags |= | |
2062 | KVM_VCPUEVENT_VALID_NMI_PENDING | KVM_VCPUEVENT_VALID_SIPI_VECTOR; | |
2063 | } | |
aee028b9 | 2064 | |
1bc22652 | 2065 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events); |
a0fb002c JK |
2066 | } |
2067 | ||
1bc22652 | 2068 | static int kvm_get_vcpu_events(X86CPU *cpu) |
a0fb002c | 2069 | { |
1bc22652 | 2070 | CPUX86State *env = &cpu->env; |
a0fb002c JK |
2071 | struct kvm_vcpu_events events; |
2072 | int ret; | |
2073 | ||
2074 | if (!kvm_has_vcpu_events()) { | |
2075 | return 0; | |
2076 | } | |
2077 | ||
fc12d72e | 2078 | memset(&events, 0, sizeof(events)); |
1bc22652 | 2079 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events); |
a0fb002c JK |
2080 | if (ret < 0) { |
2081 | return ret; | |
2082 | } | |
31827373 | 2083 | env->exception_injected = |
a0fb002c JK |
2084 | events.exception.injected ? events.exception.nr : -1; |
2085 | env->has_error_code = events.exception.has_error_code; | |
2086 | env->error_code = events.exception.error_code; | |
2087 | ||
2088 | env->interrupt_injected = | |
2089 | events.interrupt.injected ? events.interrupt.nr : -1; | |
2090 | env->soft_interrupt = events.interrupt.soft; | |
2091 | ||
2092 | env->nmi_injected = events.nmi.injected; | |
2093 | env->nmi_pending = events.nmi.pending; | |
2094 | if (events.nmi.masked) { | |
2095 | env->hflags2 |= HF2_NMI_MASK; | |
2096 | } else { | |
2097 | env->hflags2 &= ~HF2_NMI_MASK; | |
2098 | } | |
2099 | ||
fc12d72e PB |
2100 | if (events.flags & KVM_VCPUEVENT_VALID_SMM) { |
2101 | if (events.smi.smm) { | |
2102 | env->hflags |= HF_SMM_MASK; | |
2103 | } else { | |
2104 | env->hflags &= ~HF_SMM_MASK; | |
2105 | } | |
2106 | if (events.smi.pending) { | |
2107 | cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI); | |
2108 | } else { | |
2109 | cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_SMI); | |
2110 | } | |
2111 | if (events.smi.smm_inside_nmi) { | |
2112 | env->hflags2 |= HF2_SMM_INSIDE_NMI_MASK; | |
2113 | } else { | |
2114 | env->hflags2 &= ~HF2_SMM_INSIDE_NMI_MASK; | |
2115 | } | |
2116 | if (events.smi.latched_init) { | |
2117 | cpu_interrupt(CPU(cpu), CPU_INTERRUPT_INIT); | |
2118 | } else { | |
2119 | cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_INIT); | |
2120 | } | |
2121 | } | |
2122 | ||
a0fb002c | 2123 | env->sipi_vector = events.sipi_vector; |
a0fb002c JK |
2124 | |
2125 | return 0; | |
2126 | } | |
2127 | ||
1bc22652 | 2128 | static int kvm_guest_debug_workarounds(X86CPU *cpu) |
b0b1d690 | 2129 | { |
ed2803da | 2130 | CPUState *cs = CPU(cpu); |
1bc22652 | 2131 | CPUX86State *env = &cpu->env; |
b0b1d690 | 2132 | int ret = 0; |
b0b1d690 JK |
2133 | unsigned long reinject_trap = 0; |
2134 | ||
2135 | if (!kvm_has_vcpu_events()) { | |
2136 | if (env->exception_injected == 1) { | |
2137 | reinject_trap = KVM_GUESTDBG_INJECT_DB; | |
2138 | } else if (env->exception_injected == 3) { | |
2139 | reinject_trap = KVM_GUESTDBG_INJECT_BP; | |
2140 | } | |
2141 | env->exception_injected = -1; | |
2142 | } | |
2143 | ||
2144 | /* | |
2145 | * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF | |
2146 | * injected via SET_GUEST_DEBUG while updating GP regs. Work around this | |
2147 | * by updating the debug state once again if single-stepping is on. | |
2148 | * Another reason to call kvm_update_guest_debug here is a pending debug | |
2149 | * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to | |
2150 | * reinject them via SET_GUEST_DEBUG. | |
2151 | */ | |
2152 | if (reinject_trap || | |
ed2803da | 2153 | (!kvm_has_robust_singlestep() && cs->singlestep_enabled)) { |
38e478ec | 2154 | ret = kvm_update_guest_debug(cs, reinject_trap); |
b0b1d690 | 2155 | } |
b0b1d690 JK |
2156 | return ret; |
2157 | } | |
2158 | ||
1bc22652 | 2159 | static int kvm_put_debugregs(X86CPU *cpu) |
ff44f1a3 | 2160 | { |
1bc22652 | 2161 | CPUX86State *env = &cpu->env; |
ff44f1a3 JK |
2162 | struct kvm_debugregs dbgregs; |
2163 | int i; | |
2164 | ||
2165 | if (!kvm_has_debugregs()) { | |
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | for (i = 0; i < 4; i++) { | |
2170 | dbgregs.db[i] = env->dr[i]; | |
2171 | } | |
2172 | dbgregs.dr6 = env->dr[6]; | |
2173 | dbgregs.dr7 = env->dr[7]; | |
2174 | dbgregs.flags = 0; | |
2175 | ||
1bc22652 | 2176 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_DEBUGREGS, &dbgregs); |
ff44f1a3 JK |
2177 | } |
2178 | ||
1bc22652 | 2179 | static int kvm_get_debugregs(X86CPU *cpu) |
ff44f1a3 | 2180 | { |
1bc22652 | 2181 | CPUX86State *env = &cpu->env; |
ff44f1a3 JK |
2182 | struct kvm_debugregs dbgregs; |
2183 | int i, ret; | |
2184 | ||
2185 | if (!kvm_has_debugregs()) { | |
2186 | return 0; | |
2187 | } | |
2188 | ||
1bc22652 | 2189 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_DEBUGREGS, &dbgregs); |
ff44f1a3 | 2190 | if (ret < 0) { |
b9bec74b | 2191 | return ret; |
ff44f1a3 JK |
2192 | } |
2193 | for (i = 0; i < 4; i++) { | |
2194 | env->dr[i] = dbgregs.db[i]; | |
2195 | } | |
2196 | env->dr[4] = env->dr[6] = dbgregs.dr6; | |
2197 | env->dr[5] = env->dr[7] = dbgregs.dr7; | |
ff44f1a3 JK |
2198 | |
2199 | return 0; | |
2200 | } | |
2201 | ||
20d695a9 | 2202 | int kvm_arch_put_registers(CPUState *cpu, int level) |
05330448 | 2203 | { |
20d695a9 | 2204 | X86CPU *x86_cpu = X86_CPU(cpu); |
05330448 AL |
2205 | int ret; |
2206 | ||
2fa45344 | 2207 | assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu)); |
dbaa07c4 | 2208 | |
6bdf863d JK |
2209 | if (level >= KVM_PUT_RESET_STATE && has_msr_feature_control) { |
2210 | ret = kvm_put_msr_feature_control(x86_cpu); | |
2211 | if (ret < 0) { | |
2212 | return ret; | |
2213 | } | |
2214 | } | |
2215 | ||
1bc22652 | 2216 | ret = kvm_getput_regs(x86_cpu, 1); |
b9bec74b | 2217 | if (ret < 0) { |
05330448 | 2218 | return ret; |
b9bec74b | 2219 | } |
1bc22652 | 2220 | ret = kvm_put_xsave(x86_cpu); |
b9bec74b | 2221 | if (ret < 0) { |
f1665b21 | 2222 | return ret; |
b9bec74b | 2223 | } |
1bc22652 | 2224 | ret = kvm_put_xcrs(x86_cpu); |
b9bec74b | 2225 | if (ret < 0) { |
05330448 | 2226 | return ret; |
b9bec74b | 2227 | } |
1bc22652 | 2228 | ret = kvm_put_sregs(x86_cpu); |
b9bec74b | 2229 | if (ret < 0) { |
05330448 | 2230 | return ret; |
b9bec74b | 2231 | } |
ab443475 | 2232 | /* must be before kvm_put_msrs */ |
1bc22652 | 2233 | ret = kvm_inject_mce_oldstyle(x86_cpu); |
ab443475 JK |
2234 | if (ret < 0) { |
2235 | return ret; | |
2236 | } | |
1bc22652 | 2237 | ret = kvm_put_msrs(x86_cpu, level); |
b9bec74b | 2238 | if (ret < 0) { |
05330448 | 2239 | return ret; |
b9bec74b | 2240 | } |
ea643051 | 2241 | if (level >= KVM_PUT_RESET_STATE) { |
1bc22652 | 2242 | ret = kvm_put_mp_state(x86_cpu); |
b9bec74b | 2243 | if (ret < 0) { |
ea643051 | 2244 | return ret; |
b9bec74b | 2245 | } |
1bc22652 | 2246 | ret = kvm_put_apic(x86_cpu); |
680c1c6f JK |
2247 | if (ret < 0) { |
2248 | return ret; | |
2249 | } | |
ea643051 | 2250 | } |
7477cd38 MT |
2251 | |
2252 | ret = kvm_put_tscdeadline_msr(x86_cpu); | |
2253 | if (ret < 0) { | |
2254 | return ret; | |
2255 | } | |
2256 | ||
1bc22652 | 2257 | ret = kvm_put_vcpu_events(x86_cpu, level); |
b9bec74b | 2258 | if (ret < 0) { |
a0fb002c | 2259 | return ret; |
b9bec74b | 2260 | } |
1bc22652 | 2261 | ret = kvm_put_debugregs(x86_cpu); |
b9bec74b | 2262 | if (ret < 0) { |
b0b1d690 | 2263 | return ret; |
b9bec74b | 2264 | } |
b0b1d690 | 2265 | /* must be last */ |
1bc22652 | 2266 | ret = kvm_guest_debug_workarounds(x86_cpu); |
b9bec74b | 2267 | if (ret < 0) { |
ff44f1a3 | 2268 | return ret; |
b9bec74b | 2269 | } |
05330448 AL |
2270 | return 0; |
2271 | } | |
2272 | ||
20d695a9 | 2273 | int kvm_arch_get_registers(CPUState *cs) |
05330448 | 2274 | { |
20d695a9 | 2275 | X86CPU *cpu = X86_CPU(cs); |
05330448 AL |
2276 | int ret; |
2277 | ||
20d695a9 | 2278 | assert(cpu_is_stopped(cs) || qemu_cpu_is_self(cs)); |
dbaa07c4 | 2279 | |
1bc22652 | 2280 | ret = kvm_getput_regs(cpu, 0); |
b9bec74b | 2281 | if (ret < 0) { |
05330448 | 2282 | return ret; |
b9bec74b | 2283 | } |
1bc22652 | 2284 | ret = kvm_get_xsave(cpu); |
b9bec74b | 2285 | if (ret < 0) { |
f1665b21 | 2286 | return ret; |
b9bec74b | 2287 | } |
1bc22652 | 2288 | ret = kvm_get_xcrs(cpu); |
b9bec74b | 2289 | if (ret < 0) { |
05330448 | 2290 | return ret; |
b9bec74b | 2291 | } |
1bc22652 | 2292 | ret = kvm_get_sregs(cpu); |
b9bec74b | 2293 | if (ret < 0) { |
05330448 | 2294 | return ret; |
b9bec74b | 2295 | } |
1bc22652 | 2296 | ret = kvm_get_msrs(cpu); |
b9bec74b | 2297 | if (ret < 0) { |
05330448 | 2298 | return ret; |
b9bec74b | 2299 | } |
23d02d9b | 2300 | ret = kvm_get_mp_state(cpu); |
b9bec74b | 2301 | if (ret < 0) { |
5a2e3c2e | 2302 | return ret; |
b9bec74b | 2303 | } |
1bc22652 | 2304 | ret = kvm_get_apic(cpu); |
680c1c6f JK |
2305 | if (ret < 0) { |
2306 | return ret; | |
2307 | } | |
1bc22652 | 2308 | ret = kvm_get_vcpu_events(cpu); |
b9bec74b | 2309 | if (ret < 0) { |
a0fb002c | 2310 | return ret; |
b9bec74b | 2311 | } |
1bc22652 | 2312 | ret = kvm_get_debugregs(cpu); |
b9bec74b | 2313 | if (ret < 0) { |
ff44f1a3 | 2314 | return ret; |
b9bec74b | 2315 | } |
05330448 AL |
2316 | return 0; |
2317 | } | |
2318 | ||
20d695a9 | 2319 | void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run) |
05330448 | 2320 | { |
20d695a9 AF |
2321 | X86CPU *x86_cpu = X86_CPU(cpu); |
2322 | CPUX86State *env = &x86_cpu->env; | |
ce377af3 JK |
2323 | int ret; |
2324 | ||
276ce815 | 2325 | /* Inject NMI */ |
fc12d72e PB |
2326 | if (cpu->interrupt_request & (CPU_INTERRUPT_NMI | CPU_INTERRUPT_SMI)) { |
2327 | if (cpu->interrupt_request & CPU_INTERRUPT_NMI) { | |
2328 | qemu_mutex_lock_iothread(); | |
2329 | cpu->interrupt_request &= ~CPU_INTERRUPT_NMI; | |
2330 | qemu_mutex_unlock_iothread(); | |
2331 | DPRINTF("injected NMI\n"); | |
2332 | ret = kvm_vcpu_ioctl(cpu, KVM_NMI); | |
2333 | if (ret < 0) { | |
2334 | fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n", | |
2335 | strerror(-ret)); | |
2336 | } | |
2337 | } | |
2338 | if (cpu->interrupt_request & CPU_INTERRUPT_SMI) { | |
2339 | qemu_mutex_lock_iothread(); | |
2340 | cpu->interrupt_request &= ~CPU_INTERRUPT_SMI; | |
2341 | qemu_mutex_unlock_iothread(); | |
2342 | DPRINTF("injected SMI\n"); | |
2343 | ret = kvm_vcpu_ioctl(cpu, KVM_SMI); | |
2344 | if (ret < 0) { | |
2345 | fprintf(stderr, "KVM: injection failed, SMI lost (%s)\n", | |
2346 | strerror(-ret)); | |
2347 | } | |
ce377af3 | 2348 | } |
276ce815 LJ |
2349 | } |
2350 | ||
4b8523ee JK |
2351 | if (!kvm_irqchip_in_kernel()) { |
2352 | qemu_mutex_lock_iothread(); | |
2353 | } | |
2354 | ||
e0723c45 PB |
2355 | /* Force the VCPU out of its inner loop to process any INIT requests |
2356 | * or (for userspace APIC, but it is cheap to combine the checks here) | |
2357 | * pending TPR access reports. | |
2358 | */ | |
2359 | if (cpu->interrupt_request & (CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR)) { | |
fc12d72e PB |
2360 | if ((cpu->interrupt_request & CPU_INTERRUPT_INIT) && |
2361 | !(env->hflags & HF_SMM_MASK)) { | |
2362 | cpu->exit_request = 1; | |
2363 | } | |
2364 | if (cpu->interrupt_request & CPU_INTERRUPT_TPR) { | |
2365 | cpu->exit_request = 1; | |
2366 | } | |
e0723c45 | 2367 | } |
05330448 | 2368 | |
e0723c45 | 2369 | if (!kvm_irqchip_in_kernel()) { |
db1669bc JK |
2370 | /* Try to inject an interrupt if the guest can accept it */ |
2371 | if (run->ready_for_interrupt_injection && | |
259186a7 | 2372 | (cpu->interrupt_request & CPU_INTERRUPT_HARD) && |
db1669bc JK |
2373 | (env->eflags & IF_MASK)) { |
2374 | int irq; | |
2375 | ||
259186a7 | 2376 | cpu->interrupt_request &= ~CPU_INTERRUPT_HARD; |
db1669bc JK |
2377 | irq = cpu_get_pic_interrupt(env); |
2378 | if (irq >= 0) { | |
2379 | struct kvm_interrupt intr; | |
2380 | ||
2381 | intr.irq = irq; | |
db1669bc | 2382 | DPRINTF("injected interrupt %d\n", irq); |
1bc22652 | 2383 | ret = kvm_vcpu_ioctl(cpu, KVM_INTERRUPT, &intr); |
ce377af3 JK |
2384 | if (ret < 0) { |
2385 | fprintf(stderr, | |
2386 | "KVM: injection failed, interrupt lost (%s)\n", | |
2387 | strerror(-ret)); | |
2388 | } | |
db1669bc JK |
2389 | } |
2390 | } | |
05330448 | 2391 | |
db1669bc JK |
2392 | /* If we have an interrupt but the guest is not ready to receive an |
2393 | * interrupt, request an interrupt window exit. This will | |
2394 | * cause a return to userspace as soon as the guest is ready to | |
2395 | * receive interrupts. */ | |
259186a7 | 2396 | if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) { |
db1669bc JK |
2397 | run->request_interrupt_window = 1; |
2398 | } else { | |
2399 | run->request_interrupt_window = 0; | |
2400 | } | |
2401 | ||
2402 | DPRINTF("setting tpr\n"); | |
02e51483 | 2403 | run->cr8 = cpu_get_apic_tpr(x86_cpu->apic_state); |
4b8523ee JK |
2404 | |
2405 | qemu_mutex_unlock_iothread(); | |
db1669bc | 2406 | } |
05330448 AL |
2407 | } |
2408 | ||
4c663752 | 2409 | MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run) |
05330448 | 2410 | { |
20d695a9 AF |
2411 | X86CPU *x86_cpu = X86_CPU(cpu); |
2412 | CPUX86State *env = &x86_cpu->env; | |
2413 | ||
fc12d72e PB |
2414 | if (run->flags & KVM_RUN_X86_SMM) { |
2415 | env->hflags |= HF_SMM_MASK; | |
2416 | } else { | |
2417 | env->hflags &= HF_SMM_MASK; | |
2418 | } | |
b9bec74b | 2419 | if (run->if_flag) { |
05330448 | 2420 | env->eflags |= IF_MASK; |
b9bec74b | 2421 | } else { |
05330448 | 2422 | env->eflags &= ~IF_MASK; |
b9bec74b | 2423 | } |
4b8523ee JK |
2424 | |
2425 | /* We need to protect the apic state against concurrent accesses from | |
2426 | * different threads in case the userspace irqchip is used. */ | |
2427 | if (!kvm_irqchip_in_kernel()) { | |
2428 | qemu_mutex_lock_iothread(); | |
2429 | } | |
02e51483 CF |
2430 | cpu_set_apic_tpr(x86_cpu->apic_state, run->cr8); |
2431 | cpu_set_apic_base(x86_cpu->apic_state, run->apic_base); | |
4b8523ee JK |
2432 | if (!kvm_irqchip_in_kernel()) { |
2433 | qemu_mutex_unlock_iothread(); | |
2434 | } | |
f794aa4a | 2435 | return cpu_get_mem_attrs(env); |
05330448 AL |
2436 | } |
2437 | ||
20d695a9 | 2438 | int kvm_arch_process_async_events(CPUState *cs) |
0af691d7 | 2439 | { |
20d695a9 AF |
2440 | X86CPU *cpu = X86_CPU(cs); |
2441 | CPUX86State *env = &cpu->env; | |
232fc23b | 2442 | |
259186a7 | 2443 | if (cs->interrupt_request & CPU_INTERRUPT_MCE) { |
ab443475 JK |
2444 | /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */ |
2445 | assert(env->mcg_cap); | |
2446 | ||
259186a7 | 2447 | cs->interrupt_request &= ~CPU_INTERRUPT_MCE; |
ab443475 | 2448 | |
dd1750d7 | 2449 | kvm_cpu_synchronize_state(cs); |
ab443475 JK |
2450 | |
2451 | if (env->exception_injected == EXCP08_DBLE) { | |
2452 | /* this means triple fault */ | |
2453 | qemu_system_reset_request(); | |
fcd7d003 | 2454 | cs->exit_request = 1; |
ab443475 JK |
2455 | return 0; |
2456 | } | |
2457 | env->exception_injected = EXCP12_MCHK; | |
2458 | env->has_error_code = 0; | |
2459 | ||
259186a7 | 2460 | cs->halted = 0; |
ab443475 JK |
2461 | if (kvm_irqchip_in_kernel() && env->mp_state == KVM_MP_STATE_HALTED) { |
2462 | env->mp_state = KVM_MP_STATE_RUNNABLE; | |
2463 | } | |
2464 | } | |
2465 | ||
fc12d72e PB |
2466 | if ((cs->interrupt_request & CPU_INTERRUPT_INIT) && |
2467 | !(env->hflags & HF_SMM_MASK)) { | |
e0723c45 PB |
2468 | kvm_cpu_synchronize_state(cs); |
2469 | do_cpu_init(cpu); | |
2470 | } | |
2471 | ||
db1669bc JK |
2472 | if (kvm_irqchip_in_kernel()) { |
2473 | return 0; | |
2474 | } | |
2475 | ||
259186a7 AF |
2476 | if (cs->interrupt_request & CPU_INTERRUPT_POLL) { |
2477 | cs->interrupt_request &= ~CPU_INTERRUPT_POLL; | |
02e51483 | 2478 | apic_poll_irq(cpu->apic_state); |
5d62c43a | 2479 | } |
259186a7 | 2480 | if (((cs->interrupt_request & CPU_INTERRUPT_HARD) && |
4601f7b0 | 2481 | (env->eflags & IF_MASK)) || |
259186a7 AF |
2482 | (cs->interrupt_request & CPU_INTERRUPT_NMI)) { |
2483 | cs->halted = 0; | |
6792a57b | 2484 | } |
259186a7 | 2485 | if (cs->interrupt_request & CPU_INTERRUPT_SIPI) { |
dd1750d7 | 2486 | kvm_cpu_synchronize_state(cs); |
232fc23b | 2487 | do_cpu_sipi(cpu); |
0af691d7 | 2488 | } |
259186a7 AF |
2489 | if (cs->interrupt_request & CPU_INTERRUPT_TPR) { |
2490 | cs->interrupt_request &= ~CPU_INTERRUPT_TPR; | |
dd1750d7 | 2491 | kvm_cpu_synchronize_state(cs); |
02e51483 | 2492 | apic_handle_tpr_access_report(cpu->apic_state, env->eip, |
d362e757 JK |
2493 | env->tpr_access_type); |
2494 | } | |
0af691d7 | 2495 | |
259186a7 | 2496 | return cs->halted; |
0af691d7 MT |
2497 | } |
2498 | ||
839b5630 | 2499 | static int kvm_handle_halt(X86CPU *cpu) |
05330448 | 2500 | { |
259186a7 | 2501 | CPUState *cs = CPU(cpu); |
839b5630 AF |
2502 | CPUX86State *env = &cpu->env; |
2503 | ||
259186a7 | 2504 | if (!((cs->interrupt_request & CPU_INTERRUPT_HARD) && |
05330448 | 2505 | (env->eflags & IF_MASK)) && |
259186a7 AF |
2506 | !(cs->interrupt_request & CPU_INTERRUPT_NMI)) { |
2507 | cs->halted = 1; | |
bb4ea393 | 2508 | return EXCP_HLT; |
05330448 AL |
2509 | } |
2510 | ||
bb4ea393 | 2511 | return 0; |
05330448 AL |
2512 | } |
2513 | ||
f7575c96 | 2514 | static int kvm_handle_tpr_access(X86CPU *cpu) |
d362e757 | 2515 | { |
f7575c96 AF |
2516 | CPUState *cs = CPU(cpu); |
2517 | struct kvm_run *run = cs->kvm_run; | |
d362e757 | 2518 | |
02e51483 | 2519 | apic_handle_tpr_access_report(cpu->apic_state, run->tpr_access.rip, |
d362e757 JK |
2520 | run->tpr_access.is_write ? TPR_ACCESS_WRITE |
2521 | : TPR_ACCESS_READ); | |
2522 | return 1; | |
2523 | } | |
2524 | ||
f17ec444 | 2525 | int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
e22a25c9 | 2526 | { |
38972938 | 2527 | static const uint8_t int3 = 0xcc; |
64bf3f4e | 2528 | |
f17ec444 AF |
2529 | if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) || |
2530 | cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&int3, 1, 1)) { | |
e22a25c9 | 2531 | return -EINVAL; |
b9bec74b | 2532 | } |
e22a25c9 AL |
2533 | return 0; |
2534 | } | |
2535 | ||
f17ec444 | 2536 | int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
e22a25c9 AL |
2537 | { |
2538 | uint8_t int3; | |
2539 | ||
f17ec444 AF |
2540 | if (cpu_memory_rw_debug(cs, bp->pc, &int3, 1, 0) || int3 != 0xcc || |
2541 | cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) { | |
e22a25c9 | 2542 | return -EINVAL; |
b9bec74b | 2543 | } |
e22a25c9 AL |
2544 | return 0; |
2545 | } | |
2546 | ||
2547 | static struct { | |
2548 | target_ulong addr; | |
2549 | int len; | |
2550 | int type; | |
2551 | } hw_breakpoint[4]; | |
2552 | ||
2553 | static int nb_hw_breakpoint; | |
2554 | ||
2555 | static int find_hw_breakpoint(target_ulong addr, int len, int type) | |
2556 | { | |
2557 | int n; | |
2558 | ||
b9bec74b | 2559 | for (n = 0; n < nb_hw_breakpoint; n++) { |
e22a25c9 | 2560 | if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type && |
b9bec74b | 2561 | (hw_breakpoint[n].len == len || len == -1)) { |
e22a25c9 | 2562 | return n; |
b9bec74b JK |
2563 | } |
2564 | } | |
e22a25c9 AL |
2565 | return -1; |
2566 | } | |
2567 | ||
2568 | int kvm_arch_insert_hw_breakpoint(target_ulong addr, | |
2569 | target_ulong len, int type) | |
2570 | { | |
2571 | switch (type) { | |
2572 | case GDB_BREAKPOINT_HW: | |
2573 | len = 1; | |
2574 | break; | |
2575 | case GDB_WATCHPOINT_WRITE: | |
2576 | case GDB_WATCHPOINT_ACCESS: | |
2577 | switch (len) { | |
2578 | case 1: | |
2579 | break; | |
2580 | case 2: | |
2581 | case 4: | |
2582 | case 8: | |
b9bec74b | 2583 | if (addr & (len - 1)) { |
e22a25c9 | 2584 | return -EINVAL; |
b9bec74b | 2585 | } |
e22a25c9 AL |
2586 | break; |
2587 | default: | |
2588 | return -EINVAL; | |
2589 | } | |
2590 | break; | |
2591 | default: | |
2592 | return -ENOSYS; | |
2593 | } | |
2594 | ||
b9bec74b | 2595 | if (nb_hw_breakpoint == 4) { |
e22a25c9 | 2596 | return -ENOBUFS; |
b9bec74b JK |
2597 | } |
2598 | if (find_hw_breakpoint(addr, len, type) >= 0) { | |
e22a25c9 | 2599 | return -EEXIST; |
b9bec74b | 2600 | } |
e22a25c9 AL |
2601 | hw_breakpoint[nb_hw_breakpoint].addr = addr; |
2602 | hw_breakpoint[nb_hw_breakpoint].len = len; | |
2603 | hw_breakpoint[nb_hw_breakpoint].type = type; | |
2604 | nb_hw_breakpoint++; | |
2605 | ||
2606 | return 0; | |
2607 | } | |
2608 | ||
2609 | int kvm_arch_remove_hw_breakpoint(target_ulong addr, | |
2610 | target_ulong len, int type) | |
2611 | { | |
2612 | int n; | |
2613 | ||
2614 | n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type); | |
b9bec74b | 2615 | if (n < 0) { |
e22a25c9 | 2616 | return -ENOENT; |
b9bec74b | 2617 | } |
e22a25c9 AL |
2618 | nb_hw_breakpoint--; |
2619 | hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint]; | |
2620 | ||
2621 | return 0; | |
2622 | } | |
2623 | ||
2624 | void kvm_arch_remove_all_hw_breakpoints(void) | |
2625 | { | |
2626 | nb_hw_breakpoint = 0; | |
2627 | } | |
2628 | ||
2629 | static CPUWatchpoint hw_watchpoint; | |
2630 | ||
a60f24b5 | 2631 | static int kvm_handle_debug(X86CPU *cpu, |
48405526 | 2632 | struct kvm_debug_exit_arch *arch_info) |
e22a25c9 | 2633 | { |
ed2803da | 2634 | CPUState *cs = CPU(cpu); |
a60f24b5 | 2635 | CPUX86State *env = &cpu->env; |
f2574737 | 2636 | int ret = 0; |
e22a25c9 AL |
2637 | int n; |
2638 | ||
2639 | if (arch_info->exception == 1) { | |
2640 | if (arch_info->dr6 & (1 << 14)) { | |
ed2803da | 2641 | if (cs->singlestep_enabled) { |
f2574737 | 2642 | ret = EXCP_DEBUG; |
b9bec74b | 2643 | } |
e22a25c9 | 2644 | } else { |
b9bec74b JK |
2645 | for (n = 0; n < 4; n++) { |
2646 | if (arch_info->dr6 & (1 << n)) { | |
e22a25c9 AL |
2647 | switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) { |
2648 | case 0x0: | |
f2574737 | 2649 | ret = EXCP_DEBUG; |
e22a25c9 AL |
2650 | break; |
2651 | case 0x1: | |
f2574737 | 2652 | ret = EXCP_DEBUG; |
ff4700b0 | 2653 | cs->watchpoint_hit = &hw_watchpoint; |
e22a25c9 AL |
2654 | hw_watchpoint.vaddr = hw_breakpoint[n].addr; |
2655 | hw_watchpoint.flags = BP_MEM_WRITE; | |
2656 | break; | |
2657 | case 0x3: | |
f2574737 | 2658 | ret = EXCP_DEBUG; |
ff4700b0 | 2659 | cs->watchpoint_hit = &hw_watchpoint; |
e22a25c9 AL |
2660 | hw_watchpoint.vaddr = hw_breakpoint[n].addr; |
2661 | hw_watchpoint.flags = BP_MEM_ACCESS; | |
2662 | break; | |
2663 | } | |
b9bec74b JK |
2664 | } |
2665 | } | |
e22a25c9 | 2666 | } |
ff4700b0 | 2667 | } else if (kvm_find_sw_breakpoint(cs, arch_info->pc)) { |
f2574737 | 2668 | ret = EXCP_DEBUG; |
b9bec74b | 2669 | } |
f2574737 | 2670 | if (ret == 0) { |
ff4700b0 | 2671 | cpu_synchronize_state(cs); |
48405526 | 2672 | assert(env->exception_injected == -1); |
b0b1d690 | 2673 | |
f2574737 | 2674 | /* pass to guest */ |
48405526 BS |
2675 | env->exception_injected = arch_info->exception; |
2676 | env->has_error_code = 0; | |
b0b1d690 | 2677 | } |
e22a25c9 | 2678 | |
f2574737 | 2679 | return ret; |
e22a25c9 AL |
2680 | } |
2681 | ||
20d695a9 | 2682 | void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg) |
e22a25c9 AL |
2683 | { |
2684 | const uint8_t type_code[] = { | |
2685 | [GDB_BREAKPOINT_HW] = 0x0, | |
2686 | [GDB_WATCHPOINT_WRITE] = 0x1, | |
2687 | [GDB_WATCHPOINT_ACCESS] = 0x3 | |
2688 | }; | |
2689 | const uint8_t len_code[] = { | |
2690 | [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2 | |
2691 | }; | |
2692 | int n; | |
2693 | ||
a60f24b5 | 2694 | if (kvm_sw_breakpoints_active(cpu)) { |
e22a25c9 | 2695 | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; |
b9bec74b | 2696 | } |
e22a25c9 AL |
2697 | if (nb_hw_breakpoint > 0) { |
2698 | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; | |
2699 | dbg->arch.debugreg[7] = 0x0600; | |
2700 | for (n = 0; n < nb_hw_breakpoint; n++) { | |
2701 | dbg->arch.debugreg[n] = hw_breakpoint[n].addr; | |
2702 | dbg->arch.debugreg[7] |= (2 << (n * 2)) | | |
2703 | (type_code[hw_breakpoint[n].type] << (16 + n*4)) | | |
95c077c9 | 2704 | ((uint32_t)len_code[hw_breakpoint[n].len] << (18 + n*4)); |
e22a25c9 AL |
2705 | } |
2706 | } | |
2707 | } | |
4513d923 | 2708 | |
2a4dac83 JK |
2709 | static bool host_supports_vmx(void) |
2710 | { | |
2711 | uint32_t ecx, unused; | |
2712 | ||
2713 | host_cpuid(1, 0, &unused, &unused, &ecx, &unused); | |
2714 | return ecx & CPUID_EXT_VMX; | |
2715 | } | |
2716 | ||
2717 | #define VMX_INVALID_GUEST_STATE 0x80000021 | |
2718 | ||
20d695a9 | 2719 | int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) |
2a4dac83 | 2720 | { |
20d695a9 | 2721 | X86CPU *cpu = X86_CPU(cs); |
2a4dac83 JK |
2722 | uint64_t code; |
2723 | int ret; | |
2724 | ||
2725 | switch (run->exit_reason) { | |
2726 | case KVM_EXIT_HLT: | |
2727 | DPRINTF("handle_hlt\n"); | |
4b8523ee | 2728 | qemu_mutex_lock_iothread(); |
839b5630 | 2729 | ret = kvm_handle_halt(cpu); |
4b8523ee | 2730 | qemu_mutex_unlock_iothread(); |
2a4dac83 JK |
2731 | break; |
2732 | case KVM_EXIT_SET_TPR: | |
2733 | ret = 0; | |
2734 | break; | |
d362e757 | 2735 | case KVM_EXIT_TPR_ACCESS: |
4b8523ee | 2736 | qemu_mutex_lock_iothread(); |
f7575c96 | 2737 | ret = kvm_handle_tpr_access(cpu); |
4b8523ee | 2738 | qemu_mutex_unlock_iothread(); |
d362e757 | 2739 | break; |
2a4dac83 JK |
2740 | case KVM_EXIT_FAIL_ENTRY: |
2741 | code = run->fail_entry.hardware_entry_failure_reason; | |
2742 | fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n", | |
2743 | code); | |
2744 | if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) { | |
2745 | fprintf(stderr, | |
12619721 | 2746 | "\nIf you're running a guest on an Intel machine without " |
2a4dac83 JK |
2747 | "unrestricted mode\n" |
2748 | "support, the failure can be most likely due to the guest " | |
2749 | "entering an invalid\n" | |
2750 | "state for Intel VT. For example, the guest maybe running " | |
2751 | "in big real mode\n" | |
2752 | "which is not supported on less recent Intel processors." | |
2753 | "\n\n"); | |
2754 | } | |
2755 | ret = -1; | |
2756 | break; | |
2757 | case KVM_EXIT_EXCEPTION: | |
2758 | fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n", | |
2759 | run->ex.exception, run->ex.error_code); | |
2760 | ret = -1; | |
2761 | break; | |
f2574737 JK |
2762 | case KVM_EXIT_DEBUG: |
2763 | DPRINTF("kvm_exit_debug\n"); | |
4b8523ee | 2764 | qemu_mutex_lock_iothread(); |
a60f24b5 | 2765 | ret = kvm_handle_debug(cpu, &run->debug.arch); |
4b8523ee | 2766 | qemu_mutex_unlock_iothread(); |
f2574737 | 2767 | break; |
2a4dac83 JK |
2768 | default: |
2769 | fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); | |
2770 | ret = -1; | |
2771 | break; | |
2772 | } | |
2773 | ||
2774 | return ret; | |
2775 | } | |
2776 | ||
20d695a9 | 2777 | bool kvm_arch_stop_on_emulation_error(CPUState *cs) |
4513d923 | 2778 | { |
20d695a9 AF |
2779 | X86CPU *cpu = X86_CPU(cs); |
2780 | CPUX86State *env = &cpu->env; | |
2781 | ||
dd1750d7 | 2782 | kvm_cpu_synchronize_state(cs); |
b9bec74b JK |
2783 | return !(env->cr[0] & CR0_PE_MASK) || |
2784 | ((env->segs[R_CS].selector & 3) != 3); | |
4513d923 | 2785 | } |
84b058d7 JK |
2786 | |
2787 | void kvm_arch_init_irq_routing(KVMState *s) | |
2788 | { | |
2789 | if (!kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) { | |
2790 | /* If kernel can't do irq routing, interrupt source | |
2791 | * override 0->2 cannot be set up as required by HPET. | |
2792 | * So we have to disable it. | |
2793 | */ | |
2794 | no_hpet = 1; | |
2795 | } | |
cc7e0ddf | 2796 | /* We know at this point that we're using the in-kernel |
614e41bc | 2797 | * irqchip, so we can use irqfds, and on x86 we know |
f3e1bed8 | 2798 | * we can use msi via irqfd and GSI routing. |
cc7e0ddf | 2799 | */ |
614e41bc | 2800 | kvm_msi_via_irqfd_allowed = true; |
f3e1bed8 | 2801 | kvm_gsi_routing_allowed = true; |
84b058d7 | 2802 | } |
b139bd30 JK |
2803 | |
2804 | /* Classic KVM device assignment interface. Will remain x86 only. */ | |
2805 | int kvm_device_pci_assign(KVMState *s, PCIHostDeviceAddress *dev_addr, | |
2806 | uint32_t flags, uint32_t *dev_id) | |
2807 | { | |
2808 | struct kvm_assigned_pci_dev dev_data = { | |
2809 | .segnr = dev_addr->domain, | |
2810 | .busnr = dev_addr->bus, | |
2811 | .devfn = PCI_DEVFN(dev_addr->slot, dev_addr->function), | |
2812 | .flags = flags, | |
2813 | }; | |
2814 | int ret; | |
2815 | ||
2816 | dev_data.assigned_dev_id = | |
2817 | (dev_addr->domain << 16) | (dev_addr->bus << 8) | dev_data.devfn; | |
2818 | ||
2819 | ret = kvm_vm_ioctl(s, KVM_ASSIGN_PCI_DEVICE, &dev_data); | |
2820 | if (ret < 0) { | |
2821 | return ret; | |
2822 | } | |
2823 | ||
2824 | *dev_id = dev_data.assigned_dev_id; | |
2825 | ||
2826 | return 0; | |
2827 | } | |
2828 | ||
2829 | int kvm_device_pci_deassign(KVMState *s, uint32_t dev_id) | |
2830 | { | |
2831 | struct kvm_assigned_pci_dev dev_data = { | |
2832 | .assigned_dev_id = dev_id, | |
2833 | }; | |
2834 | ||
2835 | return kvm_vm_ioctl(s, KVM_DEASSIGN_PCI_DEVICE, &dev_data); | |
2836 | } | |
2837 | ||
2838 | static int kvm_assign_irq_internal(KVMState *s, uint32_t dev_id, | |
2839 | uint32_t irq_type, uint32_t guest_irq) | |
2840 | { | |
2841 | struct kvm_assigned_irq assigned_irq = { | |
2842 | .assigned_dev_id = dev_id, | |
2843 | .guest_irq = guest_irq, | |
2844 | .flags = irq_type, | |
2845 | }; | |
2846 | ||
2847 | if (kvm_check_extension(s, KVM_CAP_ASSIGN_DEV_IRQ)) { | |
2848 | return kvm_vm_ioctl(s, KVM_ASSIGN_DEV_IRQ, &assigned_irq); | |
2849 | } else { | |
2850 | return kvm_vm_ioctl(s, KVM_ASSIGN_IRQ, &assigned_irq); | |
2851 | } | |
2852 | } | |
2853 | ||
2854 | int kvm_device_intx_assign(KVMState *s, uint32_t dev_id, bool use_host_msi, | |
2855 | uint32_t guest_irq) | |
2856 | { | |
2857 | uint32_t irq_type = KVM_DEV_IRQ_GUEST_INTX | | |
2858 | (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX); | |
2859 | ||
2860 | return kvm_assign_irq_internal(s, dev_id, irq_type, guest_irq); | |
2861 | } | |
2862 | ||
2863 | int kvm_device_intx_set_mask(KVMState *s, uint32_t dev_id, bool masked) | |
2864 | { | |
2865 | struct kvm_assigned_pci_dev dev_data = { | |
2866 | .assigned_dev_id = dev_id, | |
2867 | .flags = masked ? KVM_DEV_ASSIGN_MASK_INTX : 0, | |
2868 | }; | |
2869 | ||
2870 | return kvm_vm_ioctl(s, KVM_ASSIGN_SET_INTX_MASK, &dev_data); | |
2871 | } | |
2872 | ||
2873 | static int kvm_deassign_irq_internal(KVMState *s, uint32_t dev_id, | |
2874 | uint32_t type) | |
2875 | { | |
2876 | struct kvm_assigned_irq assigned_irq = { | |
2877 | .assigned_dev_id = dev_id, | |
2878 | .flags = type, | |
2879 | }; | |
2880 | ||
2881 | return kvm_vm_ioctl(s, KVM_DEASSIGN_DEV_IRQ, &assigned_irq); | |
2882 | } | |
2883 | ||
2884 | int kvm_device_intx_deassign(KVMState *s, uint32_t dev_id, bool use_host_msi) | |
2885 | { | |
2886 | return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_INTX | | |
2887 | (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX)); | |
2888 | } | |
2889 | ||
2890 | int kvm_device_msi_assign(KVMState *s, uint32_t dev_id, int virq) | |
2891 | { | |
2892 | return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSI | | |
2893 | KVM_DEV_IRQ_GUEST_MSI, virq); | |
2894 | } | |
2895 | ||
2896 | int kvm_device_msi_deassign(KVMState *s, uint32_t dev_id) | |
2897 | { | |
2898 | return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSI | | |
2899 | KVM_DEV_IRQ_HOST_MSI); | |
2900 | } | |
2901 | ||
2902 | bool kvm_device_msix_supported(KVMState *s) | |
2903 | { | |
2904 | /* The kernel lacks a corresponding KVM_CAP, so we probe by calling | |
2905 | * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */ | |
2906 | return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, NULL) == -EFAULT; | |
2907 | } | |
2908 | ||
2909 | int kvm_device_msix_init_vectors(KVMState *s, uint32_t dev_id, | |
2910 | uint32_t nr_vectors) | |
2911 | { | |
2912 | struct kvm_assigned_msix_nr msix_nr = { | |
2913 | .assigned_dev_id = dev_id, | |
2914 | .entry_nr = nr_vectors, | |
2915 | }; | |
2916 | ||
2917 | return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, &msix_nr); | |
2918 | } | |
2919 | ||
2920 | int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector, | |
2921 | int virq) | |
2922 | { | |
2923 | struct kvm_assigned_msix_entry msix_entry = { | |
2924 | .assigned_dev_id = dev_id, | |
2925 | .gsi = virq, | |
2926 | .entry = vector, | |
2927 | }; | |
2928 | ||
2929 | return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_ENTRY, &msix_entry); | |
2930 | } | |
2931 | ||
2932 | int kvm_device_msix_assign(KVMState *s, uint32_t dev_id) | |
2933 | { | |
2934 | return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSIX | | |
2935 | KVM_DEV_IRQ_GUEST_MSIX, 0); | |
2936 | } | |
2937 | ||
2938 | int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id) | |
2939 | { | |
2940 | return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX | | |
2941 | KVM_DEV_IRQ_HOST_MSIX); | |
2942 | } | |
9e03a040 FB |
2943 | |
2944 | int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, | |
2945 | uint64_t address, uint32_t data) | |
2946 | { | |
2947 | return 0; | |
2948 | } | |
1850b6b7 EA |
2949 | |
2950 | int kvm_arch_msi_data_to_gsi(uint32_t data) | |
2951 | { | |
2952 | abort(); | |
2953 | } |