io: use qemu_accept to ensure SOCK_CLOEXEC is set
[qemu/ar7.git] / target-i386 / kvm.c
blob7974acb399f7a46350366198d7d994d7c88ee337
1 /*
2 * QEMU KVM support
4 * Copyright (C) 2006-2008 Qumranet Technologies
5 * Copyright IBM, Corp. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
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.
15 #include "qemu/osdep.h"
16 #include <sys/ioctl.h>
17 #include <sys/mman.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm_int.h"
26 #include "kvm_i386.h"
27 #include "cpu.h"
28 #include "hyperv.h"
30 #include "exec/gdbstub.h"
31 #include "qemu/host-utils.h"
32 #include "qemu/config-file.h"
33 #include "qemu/error-report.h"
34 #include "hw/i386/pc.h"
35 #include "hw/i386/apic.h"
36 #include "hw/i386/apic_internal.h"
37 #include "hw/i386/apic-msidef.h"
39 #include "exec/ioport.h"
40 #include "standard-headers/asm-x86/hyperv.h"
41 #include "hw/pci/pci.h"
42 #include "hw/pci/msi.h"
43 #include "migration/migration.h"
44 #include "exec/memattrs.h"
46 //#define DEBUG_KVM
48 #ifdef DEBUG_KVM
49 #define DPRINTF(fmt, ...) \
50 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
51 #else
52 #define DPRINTF(fmt, ...) \
53 do { } while (0)
54 #endif
56 #define MSR_KVM_WALL_CLOCK 0x11
57 #define MSR_KVM_SYSTEM_TIME 0x12
59 #ifndef BUS_MCEERR_AR
60 #define BUS_MCEERR_AR 4
61 #endif
62 #ifndef BUS_MCEERR_AO
63 #define BUS_MCEERR_AO 5
64 #endif
66 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
67 KVM_CAP_INFO(SET_TSS_ADDR),
68 KVM_CAP_INFO(EXT_CPUID),
69 KVM_CAP_INFO(MP_STATE),
70 KVM_CAP_LAST_INFO
73 static bool has_msr_star;
74 static bool has_msr_hsave_pa;
75 static bool has_msr_tsc_aux;
76 static bool has_msr_tsc_adjust;
77 static bool has_msr_tsc_deadline;
78 static bool has_msr_feature_control;
79 static bool has_msr_async_pf_en;
80 static bool has_msr_pv_eoi_en;
81 static bool has_msr_misc_enable;
82 static bool has_msr_smbase;
83 static bool has_msr_bndcfgs;
84 static bool has_msr_kvm_steal_time;
85 static int lm_capable_kernel;
86 static bool has_msr_hv_hypercall;
87 static bool has_msr_hv_vapic;
88 static bool has_msr_hv_tsc;
89 static bool has_msr_hv_crash;
90 static bool has_msr_hv_reset;
91 static bool has_msr_hv_vpindex;
92 static bool has_msr_hv_runtime;
93 static bool has_msr_hv_synic;
94 static bool has_msr_hv_stimer;
95 static bool has_msr_mtrr;
96 static bool has_msr_xss;
98 static bool has_msr_architectural_pmu;
99 static uint32_t num_architectural_pmu_counters;
101 static int has_xsave;
102 static int has_xcrs;
103 static int has_pit_state2;
105 int kvm_has_pit_state2(void)
107 return has_pit_state2;
110 bool kvm_has_smm(void)
112 return kvm_check_extension(kvm_state, KVM_CAP_X86_SMM);
115 bool kvm_allows_irq0_override(void)
117 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
120 static int kvm_get_tsc(CPUState *cs)
122 X86CPU *cpu = X86_CPU(cs);
123 CPUX86State *env = &cpu->env;
124 struct {
125 struct kvm_msrs info;
126 struct kvm_msr_entry entries[1];
127 } msr_data;
128 int ret;
130 if (env->tsc_valid) {
131 return 0;
134 msr_data.info.nmsrs = 1;
135 msr_data.entries[0].index = MSR_IA32_TSC;
136 env->tsc_valid = !runstate_is_running();
138 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
139 if (ret < 0) {
140 return ret;
143 env->tsc = msr_data.entries[0].data;
144 return 0;
147 static inline void do_kvm_synchronize_tsc(void *arg)
149 CPUState *cpu = arg;
151 kvm_get_tsc(cpu);
154 void kvm_synchronize_all_tsc(void)
156 CPUState *cpu;
158 if (kvm_enabled()) {
159 CPU_FOREACH(cpu) {
160 run_on_cpu(cpu, do_kvm_synchronize_tsc, cpu);
165 static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
167 struct kvm_cpuid2 *cpuid;
168 int r, size;
170 size = sizeof(*cpuid) + max * sizeof(*cpuid->entries);
171 cpuid = g_malloc0(size);
172 cpuid->nent = max;
173 r = kvm_ioctl(s, KVM_GET_SUPPORTED_CPUID, cpuid);
174 if (r == 0 && cpuid->nent >= max) {
175 r = -E2BIG;
177 if (r < 0) {
178 if (r == -E2BIG) {
179 g_free(cpuid);
180 return NULL;
181 } else {
182 fprintf(stderr, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
183 strerror(-r));
184 exit(1);
187 return cpuid;
190 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
191 * for all entries.
193 static struct kvm_cpuid2 *get_supported_cpuid(KVMState *s)
195 struct kvm_cpuid2 *cpuid;
196 int max = 1;
197 while ((cpuid = try_get_cpuid(s, max)) == NULL) {
198 max *= 2;
200 return cpuid;
203 static const struct kvm_para_features {
204 int cap;
205 int feature;
206 } para_features[] = {
207 { KVM_CAP_CLOCKSOURCE, KVM_FEATURE_CLOCKSOURCE },
208 { KVM_CAP_NOP_IO_DELAY, KVM_FEATURE_NOP_IO_DELAY },
209 { KVM_CAP_PV_MMU, KVM_FEATURE_MMU_OP },
210 { KVM_CAP_ASYNC_PF, KVM_FEATURE_ASYNC_PF },
213 static int get_para_features(KVMState *s)
215 int i, features = 0;
217 for (i = 0; i < ARRAY_SIZE(para_features); i++) {
218 if (kvm_check_extension(s, para_features[i].cap)) {
219 features |= (1 << para_features[i].feature);
223 return features;
227 /* Returns the value for a specific register on the cpuid entry
229 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg)
231 uint32_t ret = 0;
232 switch (reg) {
233 case R_EAX:
234 ret = entry->eax;
235 break;
236 case R_EBX:
237 ret = entry->ebx;
238 break;
239 case R_ECX:
240 ret = entry->ecx;
241 break;
242 case R_EDX:
243 ret = entry->edx;
244 break;
246 return ret;
249 /* Find matching entry for function/index on kvm_cpuid2 struct
251 static struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid,
252 uint32_t function,
253 uint32_t index)
255 int i;
256 for (i = 0; i < cpuid->nent; ++i) {
257 if (cpuid->entries[i].function == function &&
258 cpuid->entries[i].index == index) {
259 return &cpuid->entries[i];
262 /* not found: */
263 return NULL;
266 uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
267 uint32_t index, int reg)
269 struct kvm_cpuid2 *cpuid;
270 uint32_t ret = 0;
271 uint32_t cpuid_1_edx;
272 bool found = false;
274 cpuid = get_supported_cpuid(s);
276 struct kvm_cpuid_entry2 *entry = cpuid_find_entry(cpuid, function, index);
277 if (entry) {
278 found = true;
279 ret = cpuid_entry_get_reg(entry, reg);
282 /* Fixups for the data returned by KVM, below */
284 if (function == 1 && reg == R_EDX) {
285 /* KVM before 2.6.30 misreports the following features */
286 ret |= CPUID_MTRR | CPUID_PAT | CPUID_MCE | CPUID_MCA;
287 } else if (function == 1 && reg == R_ECX) {
288 /* We can set the hypervisor flag, even if KVM does not return it on
289 * GET_SUPPORTED_CPUID
291 ret |= CPUID_EXT_HYPERVISOR;
292 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
293 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
294 * and the irqchip is in the kernel.
296 if (kvm_irqchip_in_kernel() &&
297 kvm_check_extension(s, KVM_CAP_TSC_DEADLINE_TIMER)) {
298 ret |= CPUID_EXT_TSC_DEADLINE_TIMER;
301 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
302 * without the in-kernel irqchip
304 if (!kvm_irqchip_in_kernel()) {
305 ret &= ~CPUID_EXT_X2APIC;
307 } else if (function == 6 && reg == R_EAX) {
308 ret |= CPUID_6_EAX_ARAT; /* safe to allow because of emulated APIC */
309 } else if (function == 0x80000001 && reg == R_EDX) {
310 /* On Intel, kvm returns cpuid according to the Intel spec,
311 * so add missing bits according to the AMD spec:
313 cpuid_1_edx = kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
314 ret |= cpuid_1_edx & CPUID_EXT2_AMD_ALIASES;
317 g_free(cpuid);
319 /* fallback for older kernels */
320 if ((function == KVM_CPUID_FEATURES) && !found) {
321 ret = get_para_features(s);
324 return ret;
327 typedef struct HWPoisonPage {
328 ram_addr_t ram_addr;
329 QLIST_ENTRY(HWPoisonPage) list;
330 } HWPoisonPage;
332 static QLIST_HEAD(, HWPoisonPage) hwpoison_page_list =
333 QLIST_HEAD_INITIALIZER(hwpoison_page_list);
335 static void kvm_unpoison_all(void *param)
337 HWPoisonPage *page, *next_page;
339 QLIST_FOREACH_SAFE(page, &hwpoison_page_list, list, next_page) {
340 QLIST_REMOVE(page, list);
341 qemu_ram_remap(page->ram_addr, TARGET_PAGE_SIZE);
342 g_free(page);
346 static void kvm_hwpoison_page_add(ram_addr_t ram_addr)
348 HWPoisonPage *page;
350 QLIST_FOREACH(page, &hwpoison_page_list, list) {
351 if (page->ram_addr == ram_addr) {
352 return;
355 page = g_new(HWPoisonPage, 1);
356 page->ram_addr = ram_addr;
357 QLIST_INSERT_HEAD(&hwpoison_page_list, page, list);
360 static int kvm_get_mce_cap_supported(KVMState *s, uint64_t *mce_cap,
361 int *max_banks)
363 int r;
365 r = kvm_check_extension(s, KVM_CAP_MCE);
366 if (r > 0) {
367 *max_banks = r;
368 return kvm_ioctl(s, KVM_X86_GET_MCE_CAP_SUPPORTED, mce_cap);
370 return -ENOSYS;
373 static void kvm_mce_inject(X86CPU *cpu, hwaddr paddr, int code)
375 CPUX86State *env = &cpu->env;
376 uint64_t status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN |
377 MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S;
378 uint64_t mcg_status = MCG_STATUS_MCIP;
380 if (code == BUS_MCEERR_AR) {
381 status |= MCI_STATUS_AR | 0x134;
382 mcg_status |= MCG_STATUS_EIPV;
383 } else {
384 status |= 0xc0;
385 mcg_status |= MCG_STATUS_RIPV;
387 cpu_x86_inject_mce(NULL, cpu, 9, status, mcg_status, paddr,
388 (MCM_ADDR_PHYS << 6) | 0xc,
389 cpu_x86_support_mca_broadcast(env) ?
390 MCE_INJECT_BROADCAST : 0);
393 static void hardware_memory_error(void)
395 fprintf(stderr, "Hardware memory error!\n");
396 exit(1);
399 int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr)
401 X86CPU *cpu = X86_CPU(c);
402 CPUX86State *env = &cpu->env;
403 ram_addr_t ram_addr;
404 hwaddr paddr;
406 if ((env->mcg_cap & MCG_SER_P) && addr
407 && (code == BUS_MCEERR_AR || code == BUS_MCEERR_AO)) {
408 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
409 !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)) {
410 fprintf(stderr, "Hardware memory error for memory used by "
411 "QEMU itself instead of guest system!\n");
412 /* Hope we are lucky for AO MCE */
413 if (code == BUS_MCEERR_AO) {
414 return 0;
415 } else {
416 hardware_memory_error();
419 kvm_hwpoison_page_add(ram_addr);
420 kvm_mce_inject(cpu, paddr, code);
421 } else {
422 if (code == BUS_MCEERR_AO) {
423 return 0;
424 } else if (code == BUS_MCEERR_AR) {
425 hardware_memory_error();
426 } else {
427 return 1;
430 return 0;
433 int kvm_arch_on_sigbus(int code, void *addr)
435 X86CPU *cpu = X86_CPU(first_cpu);
437 if ((cpu->env.mcg_cap & MCG_SER_P) && addr && code == BUS_MCEERR_AO) {
438 ram_addr_t ram_addr;
439 hwaddr paddr;
441 /* Hope we are lucky for AO MCE */
442 if (qemu_ram_addr_from_host(addr, &ram_addr) == NULL ||
443 !kvm_physical_memory_addr_from_host(first_cpu->kvm_state,
444 addr, &paddr)) {
445 fprintf(stderr, "Hardware memory error for memory used by "
446 "QEMU itself instead of guest system!: %p\n", addr);
447 return 0;
449 kvm_hwpoison_page_add(ram_addr);
450 kvm_mce_inject(X86_CPU(first_cpu), paddr, code);
451 } else {
452 if (code == BUS_MCEERR_AO) {
453 return 0;
454 } else if (code == BUS_MCEERR_AR) {
455 hardware_memory_error();
456 } else {
457 return 1;
460 return 0;
463 static int kvm_inject_mce_oldstyle(X86CPU *cpu)
465 CPUX86State *env = &cpu->env;
467 if (!kvm_has_vcpu_events() && env->exception_injected == EXCP12_MCHK) {
468 unsigned int bank, bank_num = env->mcg_cap & 0xff;
469 struct kvm_x86_mce mce;
471 env->exception_injected = -1;
474 * There must be at least one bank in use if an MCE is pending.
475 * Find it and use its values for the event injection.
477 for (bank = 0; bank < bank_num; bank++) {
478 if (env->mce_banks[bank * 4 + 1] & MCI_STATUS_VAL) {
479 break;
482 assert(bank < bank_num);
484 mce.bank = bank;
485 mce.status = env->mce_banks[bank * 4 + 1];
486 mce.mcg_status = env->mcg_status;
487 mce.addr = env->mce_banks[bank * 4 + 2];
488 mce.misc = env->mce_banks[bank * 4 + 3];
490 return kvm_vcpu_ioctl(CPU(cpu), KVM_X86_SET_MCE, &mce);
492 return 0;
495 static void cpu_update_state(void *opaque, int running, RunState state)
497 CPUX86State *env = opaque;
499 if (running) {
500 env->tsc_valid = false;
504 unsigned long kvm_arch_vcpu_id(CPUState *cs)
506 X86CPU *cpu = X86_CPU(cs);
507 return cpu->apic_id;
510 #ifndef KVM_CPUID_SIGNATURE_NEXT
511 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
512 #endif
514 static bool hyperv_hypercall_available(X86CPU *cpu)
516 return cpu->hyperv_vapic ||
517 (cpu->hyperv_spinlock_attempts != HYPERV_SPINLOCK_NEVER_RETRY);
520 static bool hyperv_enabled(X86CPU *cpu)
522 CPUState *cs = CPU(cpu);
523 return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
524 (hyperv_hypercall_available(cpu) ||
525 cpu->hyperv_time ||
526 cpu->hyperv_relaxed_timing ||
527 cpu->hyperv_crash ||
528 cpu->hyperv_reset ||
529 cpu->hyperv_vpindex ||
530 cpu->hyperv_runtime ||
531 cpu->hyperv_synic ||
532 cpu->hyperv_stimer);
535 static int kvm_arch_set_tsc_khz(CPUState *cs)
537 X86CPU *cpu = X86_CPU(cs);
538 CPUX86State *env = &cpu->env;
539 int r;
541 if (!env->tsc_khz) {
542 return 0;
545 r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL) ?
546 kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz) :
547 -ENOTSUP;
548 if (r < 0) {
549 /* When KVM_SET_TSC_KHZ fails, it's an error only if the current
550 * TSC frequency doesn't match the one we want.
552 int cur_freq = kvm_check_extension(cs->kvm_state, KVM_CAP_GET_TSC_KHZ) ?
553 kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ) :
554 -ENOTSUP;
555 if (cur_freq <= 0 || cur_freq != env->tsc_khz) {
556 error_report("warning: TSC frequency mismatch between "
557 "VM and host, and TSC scaling unavailable");
558 return r;
562 return 0;
565 static Error *invtsc_mig_blocker;
567 #define KVM_MAX_CPUID_ENTRIES 100
569 int kvm_arch_init_vcpu(CPUState *cs)
571 struct {
572 struct kvm_cpuid2 cpuid;
573 struct kvm_cpuid_entry2 entries[KVM_MAX_CPUID_ENTRIES];
574 } QEMU_PACKED cpuid_data;
575 X86CPU *cpu = X86_CPU(cs);
576 CPUX86State *env = &cpu->env;
577 uint32_t limit, i, j, cpuid_i;
578 uint32_t unused;
579 struct kvm_cpuid_entry2 *c;
580 uint32_t signature[3];
581 int kvm_base = KVM_CPUID_SIGNATURE;
582 int r;
584 memset(&cpuid_data, 0, sizeof(cpuid_data));
586 cpuid_i = 0;
588 /* Paravirtualization CPUIDs */
589 if (hyperv_enabled(cpu)) {
590 c = &cpuid_data.entries[cpuid_i++];
591 c->function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
592 if (!cpu->hyperv_vendor_id) {
593 memcpy(signature, "Microsoft Hv", 12);
594 } else {
595 size_t len = strlen(cpu->hyperv_vendor_id);
597 if (len > 12) {
598 error_report("hv-vendor-id truncated to 12 characters");
599 len = 12;
601 memset(signature, 0, 12);
602 memcpy(signature, cpu->hyperv_vendor_id, len);
604 c->eax = HYPERV_CPUID_MIN;
605 c->ebx = signature[0];
606 c->ecx = signature[1];
607 c->edx = signature[2];
609 c = &cpuid_data.entries[cpuid_i++];
610 c->function = HYPERV_CPUID_INTERFACE;
611 memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
612 c->eax = signature[0];
613 c->ebx = 0;
614 c->ecx = 0;
615 c->edx = 0;
617 c = &cpuid_data.entries[cpuid_i++];
618 c->function = HYPERV_CPUID_VERSION;
619 c->eax = 0x00001bbc;
620 c->ebx = 0x00060001;
622 c = &cpuid_data.entries[cpuid_i++];
623 c->function = HYPERV_CPUID_FEATURES;
624 if (cpu->hyperv_relaxed_timing) {
625 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
627 if (cpu->hyperv_vapic) {
628 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
629 c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
630 has_msr_hv_vapic = true;
632 if (cpu->hyperv_time &&
633 kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
634 c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
635 c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
636 c->eax |= 0x200;
637 has_msr_hv_tsc = true;
639 if (cpu->hyperv_crash && has_msr_hv_crash) {
640 c->edx |= HV_X64_GUEST_CRASH_MSR_AVAILABLE;
642 if (cpu->hyperv_reset && has_msr_hv_reset) {
643 c->eax |= HV_X64_MSR_RESET_AVAILABLE;
645 if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
646 c->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
648 if (cpu->hyperv_runtime && has_msr_hv_runtime) {
649 c->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE;
651 if (cpu->hyperv_synic) {
652 int sint;
654 if (!has_msr_hv_synic ||
655 kvm_vcpu_enable_cap(cs, KVM_CAP_HYPERV_SYNIC, 0)) {
656 fprintf(stderr, "Hyper-V SynIC is not supported by kernel\n");
657 return -ENOSYS;
660 c->eax |= HV_X64_MSR_SYNIC_AVAILABLE;
661 env->msr_hv_synic_version = HV_SYNIC_VERSION_1;
662 for (sint = 0; sint < ARRAY_SIZE(env->msr_hv_synic_sint); sint++) {
663 env->msr_hv_synic_sint[sint] = HV_SYNIC_SINT_MASKED;
666 if (cpu->hyperv_stimer) {
667 if (!has_msr_hv_stimer) {
668 fprintf(stderr, "Hyper-V timers aren't supported by kernel\n");
669 return -ENOSYS;
671 c->eax |= HV_X64_MSR_SYNTIMER_AVAILABLE;
673 c = &cpuid_data.entries[cpuid_i++];
674 c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
675 if (cpu->hyperv_relaxed_timing) {
676 c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
678 if (has_msr_hv_vapic) {
679 c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
681 c->ebx = cpu->hyperv_spinlock_attempts;
683 c = &cpuid_data.entries[cpuid_i++];
684 c->function = HYPERV_CPUID_IMPLEMENT_LIMITS;
685 c->eax = 0x40;
686 c->ebx = 0x40;
688 kvm_base = KVM_CPUID_SIGNATURE_NEXT;
689 has_msr_hv_hypercall = true;
692 if (cpu->expose_kvm) {
693 memcpy(signature, "KVMKVMKVM\0\0\0", 12);
694 c = &cpuid_data.entries[cpuid_i++];
695 c->function = KVM_CPUID_SIGNATURE | kvm_base;
696 c->eax = KVM_CPUID_FEATURES | kvm_base;
697 c->ebx = signature[0];
698 c->ecx = signature[1];
699 c->edx = signature[2];
701 c = &cpuid_data.entries[cpuid_i++];
702 c->function = KVM_CPUID_FEATURES | kvm_base;
703 c->eax = env->features[FEAT_KVM];
705 has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
707 has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI);
709 has_msr_kvm_steal_time = c->eax & (1 << KVM_FEATURE_STEAL_TIME);
712 cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
714 for (i = 0; i <= limit; i++) {
715 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
716 fprintf(stderr, "unsupported level value: 0x%x\n", limit);
717 abort();
719 c = &cpuid_data.entries[cpuid_i++];
721 switch (i) {
722 case 2: {
723 /* Keep reading function 2 till all the input is received */
724 int times;
726 c->function = i;
727 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC |
728 KVM_CPUID_FLAG_STATE_READ_NEXT;
729 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
730 times = c->eax & 0xff;
732 for (j = 1; j < times; ++j) {
733 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
734 fprintf(stderr, "cpuid_data is full, no space for "
735 "cpuid(eax:2):eax & 0xf = 0x%x\n", times);
736 abort();
738 c = &cpuid_data.entries[cpuid_i++];
739 c->function = i;
740 c->flags = KVM_CPUID_FLAG_STATEFUL_FUNC;
741 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
743 break;
745 case 4:
746 case 0xb:
747 case 0xd:
748 for (j = 0; ; j++) {
749 if (i == 0xd && j == 64) {
750 break;
752 c->function = i;
753 c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
754 c->index = j;
755 cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
757 if (i == 4 && c->eax == 0) {
758 break;
760 if (i == 0xb && !(c->ecx & 0xff00)) {
761 break;
763 if (i == 0xd && c->eax == 0) {
764 continue;
766 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
767 fprintf(stderr, "cpuid_data is full, no space for "
768 "cpuid(eax:0x%x,ecx:0x%x)\n", i, j);
769 abort();
771 c = &cpuid_data.entries[cpuid_i++];
773 break;
774 default:
775 c->function = i;
776 c->flags = 0;
777 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
778 break;
782 if (limit >= 0x0a) {
783 uint32_t ver;
785 cpu_x86_cpuid(env, 0x0a, 0, &ver, &unused, &unused, &unused);
786 if ((ver & 0xff) > 0) {
787 has_msr_architectural_pmu = true;
788 num_architectural_pmu_counters = (ver & 0xff00) >> 8;
790 /* Shouldn't be more than 32, since that's the number of bits
791 * available in EBX to tell us _which_ counters are available.
792 * Play it safe.
794 if (num_architectural_pmu_counters > MAX_GP_COUNTERS) {
795 num_architectural_pmu_counters = MAX_GP_COUNTERS;
800 cpu_x86_cpuid(env, 0x80000000, 0, &limit, &unused, &unused, &unused);
802 for (i = 0x80000000; i <= limit; i++) {
803 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
804 fprintf(stderr, "unsupported xlevel value: 0x%x\n", limit);
805 abort();
807 c = &cpuid_data.entries[cpuid_i++];
809 c->function = i;
810 c->flags = 0;
811 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
814 /* Call Centaur's CPUID instructions they are supported. */
815 if (env->cpuid_xlevel2 > 0) {
816 cpu_x86_cpuid(env, 0xC0000000, 0, &limit, &unused, &unused, &unused);
818 for (i = 0xC0000000; i <= limit; i++) {
819 if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
820 fprintf(stderr, "unsupported xlevel2 value: 0x%x\n", limit);
821 abort();
823 c = &cpuid_data.entries[cpuid_i++];
825 c->function = i;
826 c->flags = 0;
827 cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
831 cpuid_data.cpuid.nent = cpuid_i;
833 if (((env->cpuid_version >> 8)&0xF) >= 6
834 && (env->features[FEAT_1_EDX] & (CPUID_MCE | CPUID_MCA)) ==
835 (CPUID_MCE | CPUID_MCA)
836 && kvm_check_extension(cs->kvm_state, KVM_CAP_MCE) > 0) {
837 uint64_t mcg_cap, unsupported_caps;
838 int banks;
839 int ret;
841 ret = kvm_get_mce_cap_supported(cs->kvm_state, &mcg_cap, &banks);
842 if (ret < 0) {
843 fprintf(stderr, "kvm_get_mce_cap_supported: %s", strerror(-ret));
844 return ret;
847 if (banks < (env->mcg_cap & MCG_CAP_BANKS_MASK)) {
848 error_report("kvm: Unsupported MCE bank count (QEMU = %d, KVM = %d)",
849 (int)(env->mcg_cap & MCG_CAP_BANKS_MASK), banks);
850 return -ENOTSUP;
853 unsupported_caps = env->mcg_cap & ~(mcg_cap | MCG_CAP_BANKS_MASK);
854 if (unsupported_caps) {
855 error_report("warning: Unsupported MCG_CAP bits: 0x%" PRIx64,
856 unsupported_caps);
859 env->mcg_cap &= mcg_cap | MCG_CAP_BANKS_MASK;
860 ret = kvm_vcpu_ioctl(cs, KVM_X86_SETUP_MCE, &env->mcg_cap);
861 if (ret < 0) {
862 fprintf(stderr, "KVM_X86_SETUP_MCE: %s", strerror(-ret));
863 return ret;
867 qemu_add_vm_change_state_handler(cpu_update_state, env);
869 c = cpuid_find_entry(&cpuid_data.cpuid, 1, 0);
870 if (c) {
871 has_msr_feature_control = !!(c->ecx & CPUID_EXT_VMX) ||
872 !!(c->ecx & CPUID_EXT_SMX);
875 c = cpuid_find_entry(&cpuid_data.cpuid, 0x80000007, 0);
876 if (c && (c->edx & 1<<8) && invtsc_mig_blocker == NULL) {
877 /* for migration */
878 error_setg(&invtsc_mig_blocker,
879 "State blocked by non-migratable CPU device"
880 " (invtsc flag)");
881 migrate_add_blocker(invtsc_mig_blocker);
882 /* for savevm */
883 vmstate_x86_cpu.unmigratable = 1;
886 cpuid_data.cpuid.padding = 0;
887 r = kvm_vcpu_ioctl(cs, KVM_SET_CPUID2, &cpuid_data);
888 if (r) {
889 return r;
892 r = kvm_arch_set_tsc_khz(cs);
893 if (r < 0) {
894 return r;
897 /* vcpu's TSC frequency is either specified by user, or following
898 * the value used by KVM if the former is not present. In the
899 * latter case, we query it from KVM and record in env->tsc_khz,
900 * so that vcpu's TSC frequency can be migrated later via this field.
902 if (!env->tsc_khz) {
903 r = kvm_check_extension(cs->kvm_state, KVM_CAP_GET_TSC_KHZ) ?
904 kvm_vcpu_ioctl(cs, KVM_GET_TSC_KHZ) :
905 -ENOTSUP;
906 if (r > 0) {
907 env->tsc_khz = r;
911 if (has_xsave) {
912 env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave));
915 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
916 has_msr_mtrr = true;
919 return 0;
922 void kvm_arch_reset_vcpu(X86CPU *cpu)
924 CPUX86State *env = &cpu->env;
926 env->exception_injected = -1;
927 env->interrupt_injected = -1;
928 env->xcr0 = 1;
929 if (kvm_irqchip_in_kernel()) {
930 env->mp_state = cpu_is_bsp(cpu) ? KVM_MP_STATE_RUNNABLE :
931 KVM_MP_STATE_UNINITIALIZED;
932 } else {
933 env->mp_state = KVM_MP_STATE_RUNNABLE;
937 void kvm_arch_do_init_vcpu(X86CPU *cpu)
939 CPUX86State *env = &cpu->env;
941 /* APs get directly into wait-for-SIPI state. */
942 if (env->mp_state == KVM_MP_STATE_UNINITIALIZED) {
943 env->mp_state = KVM_MP_STATE_INIT_RECEIVED;
947 static int kvm_get_supported_msrs(KVMState *s)
949 static int kvm_supported_msrs;
950 int ret = 0;
952 /* first time */
953 if (kvm_supported_msrs == 0) {
954 struct kvm_msr_list msr_list, *kvm_msr_list;
956 kvm_supported_msrs = -1;
958 /* Obtain MSR list from KVM. These are the MSRs that we must
959 * save/restore */
960 msr_list.nmsrs = 0;
961 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, &msr_list);
962 if (ret < 0 && ret != -E2BIG) {
963 return ret;
965 /* Old kernel modules had a bug and could write beyond the provided
966 memory. Allocate at least a safe amount of 1K. */
967 kvm_msr_list = g_malloc0(MAX(1024, sizeof(msr_list) +
968 msr_list.nmsrs *
969 sizeof(msr_list.indices[0])));
971 kvm_msr_list->nmsrs = msr_list.nmsrs;
972 ret = kvm_ioctl(s, KVM_GET_MSR_INDEX_LIST, kvm_msr_list);
973 if (ret >= 0) {
974 int i;
976 for (i = 0; i < kvm_msr_list->nmsrs; i++) {
977 if (kvm_msr_list->indices[i] == MSR_STAR) {
978 has_msr_star = true;
979 continue;
981 if (kvm_msr_list->indices[i] == MSR_VM_HSAVE_PA) {
982 has_msr_hsave_pa = true;
983 continue;
985 if (kvm_msr_list->indices[i] == MSR_TSC_AUX) {
986 has_msr_tsc_aux = true;
987 continue;
989 if (kvm_msr_list->indices[i] == MSR_TSC_ADJUST) {
990 has_msr_tsc_adjust = true;
991 continue;
993 if (kvm_msr_list->indices[i] == MSR_IA32_TSCDEADLINE) {
994 has_msr_tsc_deadline = true;
995 continue;
997 if (kvm_msr_list->indices[i] == MSR_IA32_SMBASE) {
998 has_msr_smbase = true;
999 continue;
1001 if (kvm_msr_list->indices[i] == MSR_IA32_MISC_ENABLE) {
1002 has_msr_misc_enable = true;
1003 continue;
1005 if (kvm_msr_list->indices[i] == MSR_IA32_BNDCFGS) {
1006 has_msr_bndcfgs = true;
1007 continue;
1009 if (kvm_msr_list->indices[i] == MSR_IA32_XSS) {
1010 has_msr_xss = true;
1011 continue;
1013 if (kvm_msr_list->indices[i] == HV_X64_MSR_CRASH_CTL) {
1014 has_msr_hv_crash = true;
1015 continue;
1017 if (kvm_msr_list->indices[i] == HV_X64_MSR_RESET) {
1018 has_msr_hv_reset = true;
1019 continue;
1021 if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_INDEX) {
1022 has_msr_hv_vpindex = true;
1023 continue;
1025 if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_RUNTIME) {
1026 has_msr_hv_runtime = true;
1027 continue;
1029 if (kvm_msr_list->indices[i] == HV_X64_MSR_SCONTROL) {
1030 has_msr_hv_synic = true;
1031 continue;
1033 if (kvm_msr_list->indices[i] == HV_X64_MSR_STIMER0_CONFIG) {
1034 has_msr_hv_stimer = true;
1035 continue;
1040 g_free(kvm_msr_list);
1043 return ret;
1046 static Notifier smram_machine_done;
1047 static KVMMemoryListener smram_listener;
1048 static AddressSpace smram_address_space;
1049 static MemoryRegion smram_as_root;
1050 static MemoryRegion smram_as_mem;
1052 static void register_smram_listener(Notifier *n, void *unused)
1054 MemoryRegion *smram =
1055 (MemoryRegion *) object_resolve_path("/machine/smram", NULL);
1057 /* Outer container... */
1058 memory_region_init(&smram_as_root, OBJECT(kvm_state), "mem-container-smram", ~0ull);
1059 memory_region_set_enabled(&smram_as_root, true);
1061 /* ... with two regions inside: normal system memory with low
1062 * priority, and...
1064 memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram",
1065 get_system_memory(), 0, ~0ull);
1066 memory_region_add_subregion_overlap(&smram_as_root, 0, &smram_as_mem, 0);
1067 memory_region_set_enabled(&smram_as_mem, true);
1069 if (smram) {
1070 /* ... SMRAM with higher priority */
1071 memory_region_add_subregion_overlap(&smram_as_root, 0, smram, 10);
1072 memory_region_set_enabled(smram, true);
1075 address_space_init(&smram_address_space, &smram_as_root, "KVM-SMRAM");
1076 kvm_memory_listener_register(kvm_state, &smram_listener,
1077 &smram_address_space, 1);
1080 int kvm_arch_init(MachineState *ms, KVMState *s)
1082 uint64_t identity_base = 0xfffbc000;
1083 uint64_t shadow_mem;
1084 int ret;
1085 struct utsname utsname;
1087 #ifdef KVM_CAP_XSAVE
1088 has_xsave = kvm_check_extension(s, KVM_CAP_XSAVE);
1089 #endif
1091 #ifdef KVM_CAP_XCRS
1092 has_xcrs = kvm_check_extension(s, KVM_CAP_XCRS);
1093 #endif
1095 #ifdef KVM_CAP_PIT_STATE2
1096 has_pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
1097 #endif
1099 ret = kvm_get_supported_msrs(s);
1100 if (ret < 0) {
1101 return ret;
1104 uname(&utsname);
1105 lm_capable_kernel = strcmp(utsname.machine, "x86_64") == 0;
1108 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
1109 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
1110 * Since these must be part of guest physical memory, we need to allocate
1111 * them, both by setting their start addresses in the kernel and by
1112 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
1114 * Older KVM versions may not support setting the identity map base. In
1115 * that case we need to stick with the default, i.e. a 256K maximum BIOS
1116 * size.
1118 if (kvm_check_extension(s, KVM_CAP_SET_IDENTITY_MAP_ADDR)) {
1119 /* Allows up to 16M BIOSes. */
1120 identity_base = 0xfeffc000;
1122 ret = kvm_vm_ioctl(s, KVM_SET_IDENTITY_MAP_ADDR, &identity_base);
1123 if (ret < 0) {
1124 return ret;
1128 /* Set TSS base one page after EPT identity map. */
1129 ret = kvm_vm_ioctl(s, KVM_SET_TSS_ADDR, identity_base + 0x1000);
1130 if (ret < 0) {
1131 return ret;
1134 /* Tell fw_cfg to notify the BIOS to reserve the range. */
1135 ret = e820_add_entry(identity_base, 0x4000, E820_RESERVED);
1136 if (ret < 0) {
1137 fprintf(stderr, "e820_add_entry() table is full\n");
1138 return ret;
1140 qemu_register_reset(kvm_unpoison_all, NULL);
1142 shadow_mem = machine_kvm_shadow_mem(ms);
1143 if (shadow_mem != -1) {
1144 shadow_mem /= 4096;
1145 ret = kvm_vm_ioctl(s, KVM_SET_NR_MMU_PAGES, shadow_mem);
1146 if (ret < 0) {
1147 return ret;
1151 if (kvm_check_extension(s, KVM_CAP_X86_SMM)) {
1152 smram_machine_done.notify = register_smram_listener;
1153 qemu_add_machine_init_done_notifier(&smram_machine_done);
1155 return 0;
1158 static void set_v8086_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
1160 lhs->selector = rhs->selector;
1161 lhs->base = rhs->base;
1162 lhs->limit = rhs->limit;
1163 lhs->type = 3;
1164 lhs->present = 1;
1165 lhs->dpl = 3;
1166 lhs->db = 0;
1167 lhs->s = 1;
1168 lhs->l = 0;
1169 lhs->g = 0;
1170 lhs->avl = 0;
1171 lhs->unusable = 0;
1174 static void set_seg(struct kvm_segment *lhs, const SegmentCache *rhs)
1176 unsigned flags = rhs->flags;
1177 lhs->selector = rhs->selector;
1178 lhs->base = rhs->base;
1179 lhs->limit = rhs->limit;
1180 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
1181 lhs->present = (flags & DESC_P_MASK) != 0;
1182 lhs->dpl = (flags >> DESC_DPL_SHIFT) & 3;
1183 lhs->db = (flags >> DESC_B_SHIFT) & 1;
1184 lhs->s = (flags & DESC_S_MASK) != 0;
1185 lhs->l = (flags >> DESC_L_SHIFT) & 1;
1186 lhs->g = (flags & DESC_G_MASK) != 0;
1187 lhs->avl = (flags & DESC_AVL_MASK) != 0;
1188 lhs->unusable = !lhs->present;
1189 lhs->padding = 0;
1192 static void get_seg(SegmentCache *lhs, const struct kvm_segment *rhs)
1194 lhs->selector = rhs->selector;
1195 lhs->base = rhs->base;
1196 lhs->limit = rhs->limit;
1197 if (rhs->unusable) {
1198 lhs->flags = 0;
1199 } else {
1200 lhs->flags = (rhs->type << DESC_TYPE_SHIFT) |
1201 (rhs->present * DESC_P_MASK) |
1202 (rhs->dpl << DESC_DPL_SHIFT) |
1203 (rhs->db << DESC_B_SHIFT) |
1204 (rhs->s * DESC_S_MASK) |
1205 (rhs->l << DESC_L_SHIFT) |
1206 (rhs->g * DESC_G_MASK) |
1207 (rhs->avl * DESC_AVL_MASK);
1211 static void kvm_getput_reg(__u64 *kvm_reg, target_ulong *qemu_reg, int set)
1213 if (set) {
1214 *kvm_reg = *qemu_reg;
1215 } else {
1216 *qemu_reg = *kvm_reg;
1220 static int kvm_getput_regs(X86CPU *cpu, int set)
1222 CPUX86State *env = &cpu->env;
1223 struct kvm_regs regs;
1224 int ret = 0;
1226 if (!set) {
1227 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_REGS, &regs);
1228 if (ret < 0) {
1229 return ret;
1233 kvm_getput_reg(&regs.rax, &env->regs[R_EAX], set);
1234 kvm_getput_reg(&regs.rbx, &env->regs[R_EBX], set);
1235 kvm_getput_reg(&regs.rcx, &env->regs[R_ECX], set);
1236 kvm_getput_reg(&regs.rdx, &env->regs[R_EDX], set);
1237 kvm_getput_reg(&regs.rsi, &env->regs[R_ESI], set);
1238 kvm_getput_reg(&regs.rdi, &env->regs[R_EDI], set);
1239 kvm_getput_reg(&regs.rsp, &env->regs[R_ESP], set);
1240 kvm_getput_reg(&regs.rbp, &env->regs[R_EBP], set);
1241 #ifdef TARGET_X86_64
1242 kvm_getput_reg(&regs.r8, &env->regs[8], set);
1243 kvm_getput_reg(&regs.r9, &env->regs[9], set);
1244 kvm_getput_reg(&regs.r10, &env->regs[10], set);
1245 kvm_getput_reg(&regs.r11, &env->regs[11], set);
1246 kvm_getput_reg(&regs.r12, &env->regs[12], set);
1247 kvm_getput_reg(&regs.r13, &env->regs[13], set);
1248 kvm_getput_reg(&regs.r14, &env->regs[14], set);
1249 kvm_getput_reg(&regs.r15, &env->regs[15], set);
1250 #endif
1252 kvm_getput_reg(&regs.rflags, &env->eflags, set);
1253 kvm_getput_reg(&regs.rip, &env->eip, set);
1255 if (set) {
1256 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_REGS, &regs);
1259 return ret;
1262 static int kvm_put_fpu(X86CPU *cpu)
1264 CPUX86State *env = &cpu->env;
1265 struct kvm_fpu fpu;
1266 int i;
1268 memset(&fpu, 0, sizeof fpu);
1269 fpu.fsw = env->fpus & ~(7 << 11);
1270 fpu.fsw |= (env->fpstt & 7) << 11;
1271 fpu.fcw = env->fpuc;
1272 fpu.last_opcode = env->fpop;
1273 fpu.last_ip = env->fpip;
1274 fpu.last_dp = env->fpdp;
1275 for (i = 0; i < 8; ++i) {
1276 fpu.ftwx |= (!env->fptags[i]) << i;
1278 memcpy(fpu.fpr, env->fpregs, sizeof env->fpregs);
1279 for (i = 0; i < CPU_NB_REGS; i++) {
1280 stq_p(&fpu.xmm[i][0], env->xmm_regs[i].ZMM_Q(0));
1281 stq_p(&fpu.xmm[i][8], env->xmm_regs[i].ZMM_Q(1));
1283 fpu.mxcsr = env->mxcsr;
1285 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_FPU, &fpu);
1288 #define XSAVE_FCW_FSW 0
1289 #define XSAVE_FTW_FOP 1
1290 #define XSAVE_CWD_RIP 2
1291 #define XSAVE_CWD_RDP 4
1292 #define XSAVE_MXCSR 6
1293 #define XSAVE_ST_SPACE 8
1294 #define XSAVE_XMM_SPACE 40
1295 #define XSAVE_XSTATE_BV 128
1296 #define XSAVE_YMMH_SPACE 144
1297 #define XSAVE_BNDREGS 240
1298 #define XSAVE_BNDCSR 256
1299 #define XSAVE_OPMASK 272
1300 #define XSAVE_ZMM_Hi256 288
1301 #define XSAVE_Hi16_ZMM 416
1302 #define XSAVE_PKRU 672
1304 static int kvm_put_xsave(X86CPU *cpu)
1306 CPUX86State *env = &cpu->env;
1307 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1308 uint16_t cwd, swd, twd;
1309 uint8_t *xmm, *ymmh, *zmmh;
1310 int i, r;
1312 if (!has_xsave) {
1313 return kvm_put_fpu(cpu);
1316 memset(xsave, 0, sizeof(struct kvm_xsave));
1317 twd = 0;
1318 swd = env->fpus & ~(7 << 11);
1319 swd |= (env->fpstt & 7) << 11;
1320 cwd = env->fpuc;
1321 for (i = 0; i < 8; ++i) {
1322 twd |= (!env->fptags[i]) << i;
1324 xsave->region[XSAVE_FCW_FSW] = (uint32_t)(swd << 16) + cwd;
1325 xsave->region[XSAVE_FTW_FOP] = (uint32_t)(env->fpop << 16) + twd;
1326 memcpy(&xsave->region[XSAVE_CWD_RIP], &env->fpip, sizeof(env->fpip));
1327 memcpy(&xsave->region[XSAVE_CWD_RDP], &env->fpdp, sizeof(env->fpdp));
1328 memcpy(&xsave->region[XSAVE_ST_SPACE], env->fpregs,
1329 sizeof env->fpregs);
1330 xsave->region[XSAVE_MXCSR] = env->mxcsr;
1331 *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV] = env->xstate_bv;
1332 memcpy(&xsave->region[XSAVE_BNDREGS], env->bnd_regs,
1333 sizeof env->bnd_regs);
1334 memcpy(&xsave->region[XSAVE_BNDCSR], &env->bndcs_regs,
1335 sizeof(env->bndcs_regs));
1336 memcpy(&xsave->region[XSAVE_OPMASK], env->opmask_regs,
1337 sizeof env->opmask_regs);
1339 xmm = (uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1340 ymmh = (uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1341 zmmh = (uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1342 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1343 stq_p(xmm, env->xmm_regs[i].ZMM_Q(0));
1344 stq_p(xmm+8, env->xmm_regs[i].ZMM_Q(1));
1345 stq_p(ymmh, env->xmm_regs[i].ZMM_Q(2));
1346 stq_p(ymmh+8, env->xmm_regs[i].ZMM_Q(3));
1347 stq_p(zmmh, env->xmm_regs[i].ZMM_Q(4));
1348 stq_p(zmmh+8, env->xmm_regs[i].ZMM_Q(5));
1349 stq_p(zmmh+16, env->xmm_regs[i].ZMM_Q(6));
1350 stq_p(zmmh+24, env->xmm_regs[i].ZMM_Q(7));
1353 #ifdef TARGET_X86_64
1354 memcpy(&xsave->region[XSAVE_Hi16_ZMM], &env->xmm_regs[16],
1355 16 * sizeof env->xmm_regs[16]);
1356 memcpy(&xsave->region[XSAVE_PKRU], &env->pkru, sizeof env->pkru);
1357 #endif
1358 r = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XSAVE, xsave);
1359 return r;
1362 static int kvm_put_xcrs(X86CPU *cpu)
1364 CPUX86State *env = &cpu->env;
1365 struct kvm_xcrs xcrs = {};
1367 if (!has_xcrs) {
1368 return 0;
1371 xcrs.nr_xcrs = 1;
1372 xcrs.flags = 0;
1373 xcrs.xcrs[0].xcr = 0;
1374 xcrs.xcrs[0].value = env->xcr0;
1375 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_XCRS, &xcrs);
1378 static int kvm_put_sregs(X86CPU *cpu)
1380 CPUX86State *env = &cpu->env;
1381 struct kvm_sregs sregs;
1383 memset(sregs.interrupt_bitmap, 0, sizeof(sregs.interrupt_bitmap));
1384 if (env->interrupt_injected >= 0) {
1385 sregs.interrupt_bitmap[env->interrupt_injected / 64] |=
1386 (uint64_t)1 << (env->interrupt_injected % 64);
1389 if ((env->eflags & VM_MASK)) {
1390 set_v8086_seg(&sregs.cs, &env->segs[R_CS]);
1391 set_v8086_seg(&sregs.ds, &env->segs[R_DS]);
1392 set_v8086_seg(&sregs.es, &env->segs[R_ES]);
1393 set_v8086_seg(&sregs.fs, &env->segs[R_FS]);
1394 set_v8086_seg(&sregs.gs, &env->segs[R_GS]);
1395 set_v8086_seg(&sregs.ss, &env->segs[R_SS]);
1396 } else {
1397 set_seg(&sregs.cs, &env->segs[R_CS]);
1398 set_seg(&sregs.ds, &env->segs[R_DS]);
1399 set_seg(&sregs.es, &env->segs[R_ES]);
1400 set_seg(&sregs.fs, &env->segs[R_FS]);
1401 set_seg(&sregs.gs, &env->segs[R_GS]);
1402 set_seg(&sregs.ss, &env->segs[R_SS]);
1405 set_seg(&sregs.tr, &env->tr);
1406 set_seg(&sregs.ldt, &env->ldt);
1408 sregs.idt.limit = env->idt.limit;
1409 sregs.idt.base = env->idt.base;
1410 memset(sregs.idt.padding, 0, sizeof sregs.idt.padding);
1411 sregs.gdt.limit = env->gdt.limit;
1412 sregs.gdt.base = env->gdt.base;
1413 memset(sregs.gdt.padding, 0, sizeof sregs.gdt.padding);
1415 sregs.cr0 = env->cr[0];
1416 sregs.cr2 = env->cr[2];
1417 sregs.cr3 = env->cr[3];
1418 sregs.cr4 = env->cr[4];
1420 sregs.cr8 = cpu_get_apic_tpr(cpu->apic_state);
1421 sregs.apic_base = cpu_get_apic_base(cpu->apic_state);
1423 sregs.efer = env->efer;
1425 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
1428 static void kvm_msr_entry_set(struct kvm_msr_entry *entry,
1429 uint32_t index, uint64_t value)
1431 entry->index = index;
1432 entry->reserved = 0;
1433 entry->data = value;
1436 static int kvm_put_tscdeadline_msr(X86CPU *cpu)
1438 CPUX86State *env = &cpu->env;
1439 struct {
1440 struct kvm_msrs info;
1441 struct kvm_msr_entry entries[1];
1442 } msr_data;
1443 struct kvm_msr_entry *msrs = msr_data.entries;
1445 if (!has_msr_tsc_deadline) {
1446 return 0;
1449 kvm_msr_entry_set(&msrs[0], MSR_IA32_TSCDEADLINE, env->tsc_deadline);
1451 msr_data.info = (struct kvm_msrs) {
1452 .nmsrs = 1,
1455 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1459 * Provide a separate write service for the feature control MSR in order to
1460 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1461 * before writing any other state because forcibly leaving nested mode
1462 * invalidates the VCPU state.
1464 static int kvm_put_msr_feature_control(X86CPU *cpu)
1466 struct {
1467 struct kvm_msrs info;
1468 struct kvm_msr_entry entry;
1469 } msr_data;
1471 kvm_msr_entry_set(&msr_data.entry, MSR_IA32_FEATURE_CONTROL,
1472 cpu->env.msr_ia32_feature_control);
1474 msr_data.info = (struct kvm_msrs) {
1475 .nmsrs = 1,
1478 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1481 static int kvm_put_msrs(X86CPU *cpu, int level)
1483 CPUX86State *env = &cpu->env;
1484 struct {
1485 struct kvm_msrs info;
1486 struct kvm_msr_entry entries[150];
1487 } msr_data;
1488 struct kvm_msr_entry *msrs = msr_data.entries;
1489 int n = 0, i;
1491 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
1492 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
1493 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
1494 kvm_msr_entry_set(&msrs[n++], MSR_PAT, env->pat);
1495 if (has_msr_star) {
1496 kvm_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
1498 if (has_msr_hsave_pa) {
1499 kvm_msr_entry_set(&msrs[n++], MSR_VM_HSAVE_PA, env->vm_hsave);
1501 if (has_msr_tsc_aux) {
1502 kvm_msr_entry_set(&msrs[n++], MSR_TSC_AUX, env->tsc_aux);
1504 if (has_msr_tsc_adjust) {
1505 kvm_msr_entry_set(&msrs[n++], MSR_TSC_ADJUST, env->tsc_adjust);
1507 if (has_msr_misc_enable) {
1508 kvm_msr_entry_set(&msrs[n++], MSR_IA32_MISC_ENABLE,
1509 env->msr_ia32_misc_enable);
1511 if (has_msr_smbase) {
1512 kvm_msr_entry_set(&msrs[n++], MSR_IA32_SMBASE, env->smbase);
1514 if (has_msr_bndcfgs) {
1515 kvm_msr_entry_set(&msrs[n++], MSR_IA32_BNDCFGS, env->msr_bndcfgs);
1517 if (has_msr_xss) {
1518 kvm_msr_entry_set(&msrs[n++], MSR_IA32_XSS, env->xss);
1520 #ifdef TARGET_X86_64
1521 if (lm_capable_kernel) {
1522 kvm_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
1523 kvm_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
1524 kvm_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
1525 kvm_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
1527 #endif
1529 * The following MSRs have side effects on the guest or are too heavy
1530 * for normal writeback. Limit them to reset or full state updates.
1532 if (level >= KVM_PUT_RESET_STATE) {
1533 kvm_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
1534 kvm_msr_entry_set(&msrs[n++], MSR_KVM_SYSTEM_TIME,
1535 env->system_time_msr);
1536 kvm_msr_entry_set(&msrs[n++], MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
1537 if (has_msr_async_pf_en) {
1538 kvm_msr_entry_set(&msrs[n++], MSR_KVM_ASYNC_PF_EN,
1539 env->async_pf_en_msr);
1541 if (has_msr_pv_eoi_en) {
1542 kvm_msr_entry_set(&msrs[n++], MSR_KVM_PV_EOI_EN,
1543 env->pv_eoi_en_msr);
1545 if (has_msr_kvm_steal_time) {
1546 kvm_msr_entry_set(&msrs[n++], MSR_KVM_STEAL_TIME,
1547 env->steal_time_msr);
1549 if (has_msr_architectural_pmu) {
1550 /* Stop the counter. */
1551 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
1552 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL, 0);
1554 /* Set the counter values. */
1555 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
1556 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR0 + i,
1557 env->msr_fixed_counters[i]);
1559 for (i = 0; i < num_architectural_pmu_counters; i++) {
1560 kvm_msr_entry_set(&msrs[n++], MSR_P6_PERFCTR0 + i,
1561 env->msr_gp_counters[i]);
1562 kvm_msr_entry_set(&msrs[n++], MSR_P6_EVNTSEL0 + i,
1563 env->msr_gp_evtsel[i]);
1565 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_STATUS,
1566 env->msr_global_status);
1567 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1568 env->msr_global_ovf_ctrl);
1570 /* Now start the PMU. */
1571 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_FIXED_CTR_CTRL,
1572 env->msr_fixed_ctr_ctrl);
1573 kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL,
1574 env->msr_global_ctrl);
1576 if (has_msr_hv_hypercall) {
1577 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID,
1578 env->msr_hv_guest_os_id);
1579 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL,
1580 env->msr_hv_hypercall);
1582 if (has_msr_hv_vapic) {
1583 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
1584 env->msr_hv_vapic);
1586 if (has_msr_hv_tsc) {
1587 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
1588 env->msr_hv_tsc);
1590 if (has_msr_hv_crash) {
1591 int j;
1593 for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++)
1594 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_P0 + j,
1595 env->msr_hv_crash_params[j]);
1597 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_CRASH_CTL,
1598 HV_X64_MSR_CRASH_CTL_NOTIFY);
1600 if (has_msr_hv_runtime) {
1601 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_VP_RUNTIME,
1602 env->msr_hv_runtime);
1604 if (cpu->hyperv_synic) {
1605 int j;
1607 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SCONTROL,
1608 env->msr_hv_synic_control);
1609 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SVERSION,
1610 env->msr_hv_synic_version);
1611 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SIEFP,
1612 env->msr_hv_synic_evt_page);
1613 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SIMP,
1614 env->msr_hv_synic_msg_page);
1616 for (j = 0; j < ARRAY_SIZE(env->msr_hv_synic_sint); j++) {
1617 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_SINT0 + j,
1618 env->msr_hv_synic_sint[j]);
1621 if (has_msr_hv_stimer) {
1622 int j;
1624 for (j = 0; j < ARRAY_SIZE(env->msr_hv_stimer_config); j++) {
1625 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_STIMER0_CONFIG + j*2,
1626 env->msr_hv_stimer_config[j]);
1629 for (j = 0; j < ARRAY_SIZE(env->msr_hv_stimer_count); j++) {
1630 kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_STIMER0_COUNT + j*2,
1631 env->msr_hv_stimer_count[j]);
1634 if (has_msr_mtrr) {
1635 kvm_msr_entry_set(&msrs[n++], MSR_MTRRdefType, env->mtrr_deftype);
1636 kvm_msr_entry_set(&msrs[n++],
1637 MSR_MTRRfix64K_00000, env->mtrr_fixed[0]);
1638 kvm_msr_entry_set(&msrs[n++],
1639 MSR_MTRRfix16K_80000, env->mtrr_fixed[1]);
1640 kvm_msr_entry_set(&msrs[n++],
1641 MSR_MTRRfix16K_A0000, env->mtrr_fixed[2]);
1642 kvm_msr_entry_set(&msrs[n++],
1643 MSR_MTRRfix4K_C0000, env->mtrr_fixed[3]);
1644 kvm_msr_entry_set(&msrs[n++],
1645 MSR_MTRRfix4K_C8000, env->mtrr_fixed[4]);
1646 kvm_msr_entry_set(&msrs[n++],
1647 MSR_MTRRfix4K_D0000, env->mtrr_fixed[5]);
1648 kvm_msr_entry_set(&msrs[n++],
1649 MSR_MTRRfix4K_D8000, env->mtrr_fixed[6]);
1650 kvm_msr_entry_set(&msrs[n++],
1651 MSR_MTRRfix4K_E0000, env->mtrr_fixed[7]);
1652 kvm_msr_entry_set(&msrs[n++],
1653 MSR_MTRRfix4K_E8000, env->mtrr_fixed[8]);
1654 kvm_msr_entry_set(&msrs[n++],
1655 MSR_MTRRfix4K_F0000, env->mtrr_fixed[9]);
1656 kvm_msr_entry_set(&msrs[n++],
1657 MSR_MTRRfix4K_F8000, env->mtrr_fixed[10]);
1658 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
1659 kvm_msr_entry_set(&msrs[n++],
1660 MSR_MTRRphysBase(i), env->mtrr_var[i].base);
1661 kvm_msr_entry_set(&msrs[n++],
1662 MSR_MTRRphysMask(i), env->mtrr_var[i].mask);
1666 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1667 * kvm_put_msr_feature_control. */
1669 if (env->mcg_cap) {
1670 int i;
1672 kvm_msr_entry_set(&msrs[n++], MSR_MCG_STATUS, env->mcg_status);
1673 kvm_msr_entry_set(&msrs[n++], MSR_MCG_CTL, env->mcg_ctl);
1674 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
1675 kvm_msr_entry_set(&msrs[n++], MSR_MC0_CTL + i, env->mce_banks[i]);
1679 msr_data.info = (struct kvm_msrs) {
1680 .nmsrs = n,
1683 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MSRS, &msr_data);
1688 static int kvm_get_fpu(X86CPU *cpu)
1690 CPUX86State *env = &cpu->env;
1691 struct kvm_fpu fpu;
1692 int i, ret;
1694 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_FPU, &fpu);
1695 if (ret < 0) {
1696 return ret;
1699 env->fpstt = (fpu.fsw >> 11) & 7;
1700 env->fpus = fpu.fsw;
1701 env->fpuc = fpu.fcw;
1702 env->fpop = fpu.last_opcode;
1703 env->fpip = fpu.last_ip;
1704 env->fpdp = fpu.last_dp;
1705 for (i = 0; i < 8; ++i) {
1706 env->fptags[i] = !((fpu.ftwx >> i) & 1);
1708 memcpy(env->fpregs, fpu.fpr, sizeof env->fpregs);
1709 for (i = 0; i < CPU_NB_REGS; i++) {
1710 env->xmm_regs[i].ZMM_Q(0) = ldq_p(&fpu.xmm[i][0]);
1711 env->xmm_regs[i].ZMM_Q(1) = ldq_p(&fpu.xmm[i][8]);
1713 env->mxcsr = fpu.mxcsr;
1715 return 0;
1718 static int kvm_get_xsave(X86CPU *cpu)
1720 CPUX86State *env = &cpu->env;
1721 struct kvm_xsave* xsave = env->kvm_xsave_buf;
1722 int ret, i;
1723 const uint8_t *xmm, *ymmh, *zmmh;
1724 uint16_t cwd, swd, twd;
1726 if (!has_xsave) {
1727 return kvm_get_fpu(cpu);
1730 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XSAVE, xsave);
1731 if (ret < 0) {
1732 return ret;
1735 cwd = (uint16_t)xsave->region[XSAVE_FCW_FSW];
1736 swd = (uint16_t)(xsave->region[XSAVE_FCW_FSW] >> 16);
1737 twd = (uint16_t)xsave->region[XSAVE_FTW_FOP];
1738 env->fpop = (uint16_t)(xsave->region[XSAVE_FTW_FOP] >> 16);
1739 env->fpstt = (swd >> 11) & 7;
1740 env->fpus = swd;
1741 env->fpuc = cwd;
1742 for (i = 0; i < 8; ++i) {
1743 env->fptags[i] = !((twd >> i) & 1);
1745 memcpy(&env->fpip, &xsave->region[XSAVE_CWD_RIP], sizeof(env->fpip));
1746 memcpy(&env->fpdp, &xsave->region[XSAVE_CWD_RDP], sizeof(env->fpdp));
1747 env->mxcsr = xsave->region[XSAVE_MXCSR];
1748 memcpy(env->fpregs, &xsave->region[XSAVE_ST_SPACE],
1749 sizeof env->fpregs);
1750 env->xstate_bv = *(uint64_t *)&xsave->region[XSAVE_XSTATE_BV];
1751 memcpy(env->bnd_regs, &xsave->region[XSAVE_BNDREGS],
1752 sizeof env->bnd_regs);
1753 memcpy(&env->bndcs_regs, &xsave->region[XSAVE_BNDCSR],
1754 sizeof(env->bndcs_regs));
1755 memcpy(env->opmask_regs, &xsave->region[XSAVE_OPMASK],
1756 sizeof env->opmask_regs);
1758 xmm = (const uint8_t *)&xsave->region[XSAVE_XMM_SPACE];
1759 ymmh = (const uint8_t *)&xsave->region[XSAVE_YMMH_SPACE];
1760 zmmh = (const uint8_t *)&xsave->region[XSAVE_ZMM_Hi256];
1761 for (i = 0; i < CPU_NB_REGS; i++, xmm += 16, ymmh += 16, zmmh += 32) {
1762 env->xmm_regs[i].ZMM_Q(0) = ldq_p(xmm);
1763 env->xmm_regs[i].ZMM_Q(1) = ldq_p(xmm+8);
1764 env->xmm_regs[i].ZMM_Q(2) = ldq_p(ymmh);
1765 env->xmm_regs[i].ZMM_Q(3) = ldq_p(ymmh+8);
1766 env->xmm_regs[i].ZMM_Q(4) = ldq_p(zmmh);
1767 env->xmm_regs[i].ZMM_Q(5) = ldq_p(zmmh+8);
1768 env->xmm_regs[i].ZMM_Q(6) = ldq_p(zmmh+16);
1769 env->xmm_regs[i].ZMM_Q(7) = ldq_p(zmmh+24);
1772 #ifdef TARGET_X86_64
1773 memcpy(&env->xmm_regs[16], &xsave->region[XSAVE_Hi16_ZMM],
1774 16 * sizeof env->xmm_regs[16]);
1775 memcpy(&env->pkru, &xsave->region[XSAVE_PKRU], sizeof env->pkru);
1776 #endif
1777 return 0;
1780 static int kvm_get_xcrs(X86CPU *cpu)
1782 CPUX86State *env = &cpu->env;
1783 int i, ret;
1784 struct kvm_xcrs xcrs;
1786 if (!has_xcrs) {
1787 return 0;
1790 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_XCRS, &xcrs);
1791 if (ret < 0) {
1792 return ret;
1795 for (i = 0; i < xcrs.nr_xcrs; i++) {
1796 /* Only support xcr0 now */
1797 if (xcrs.xcrs[i].xcr == 0) {
1798 env->xcr0 = xcrs.xcrs[i].value;
1799 break;
1802 return 0;
1805 static int kvm_get_sregs(X86CPU *cpu)
1807 CPUX86State *env = &cpu->env;
1808 struct kvm_sregs sregs;
1809 uint32_t hflags;
1810 int bit, i, ret;
1812 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1813 if (ret < 0) {
1814 return ret;
1817 /* There can only be one pending IRQ set in the bitmap at a time, so try
1818 to find it and save its number instead (-1 for none). */
1819 env->interrupt_injected = -1;
1820 for (i = 0; i < ARRAY_SIZE(sregs.interrupt_bitmap); i++) {
1821 if (sregs.interrupt_bitmap[i]) {
1822 bit = ctz64(sregs.interrupt_bitmap[i]);
1823 env->interrupt_injected = i * 64 + bit;
1824 break;
1828 get_seg(&env->segs[R_CS], &sregs.cs);
1829 get_seg(&env->segs[R_DS], &sregs.ds);
1830 get_seg(&env->segs[R_ES], &sregs.es);
1831 get_seg(&env->segs[R_FS], &sregs.fs);
1832 get_seg(&env->segs[R_GS], &sregs.gs);
1833 get_seg(&env->segs[R_SS], &sregs.ss);
1835 get_seg(&env->tr, &sregs.tr);
1836 get_seg(&env->ldt, &sregs.ldt);
1838 env->idt.limit = sregs.idt.limit;
1839 env->idt.base = sregs.idt.base;
1840 env->gdt.limit = sregs.gdt.limit;
1841 env->gdt.base = sregs.gdt.base;
1843 env->cr[0] = sregs.cr0;
1844 env->cr[2] = sregs.cr2;
1845 env->cr[3] = sregs.cr3;
1846 env->cr[4] = sregs.cr4;
1848 env->efer = sregs.efer;
1850 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1852 #define HFLAG_COPY_MASK \
1853 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1854 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1855 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1856 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1858 hflags = env->hflags & HFLAG_COPY_MASK;
1859 hflags |= (env->segs[R_SS].flags >> DESC_DPL_SHIFT) & HF_CPL_MASK;
1860 hflags |= (env->cr[0] & CR0_PE_MASK) << (HF_PE_SHIFT - CR0_PE_SHIFT);
1861 hflags |= (env->cr[0] << (HF_MP_SHIFT - CR0_MP_SHIFT)) &
1862 (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK);
1863 hflags |= (env->eflags & (HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK));
1865 if (env->cr[4] & CR4_OSFXSR_MASK) {
1866 hflags |= HF_OSFXSR_MASK;
1869 if (env->efer & MSR_EFER_LMA) {
1870 hflags |= HF_LMA_MASK;
1873 if ((hflags & HF_LMA_MASK) && (env->segs[R_CS].flags & DESC_L_MASK)) {
1874 hflags |= HF_CS32_MASK | HF_SS32_MASK | HF_CS64_MASK;
1875 } else {
1876 hflags |= (env->segs[R_CS].flags & DESC_B_MASK) >>
1877 (DESC_B_SHIFT - HF_CS32_SHIFT);
1878 hflags |= (env->segs[R_SS].flags & DESC_B_MASK) >>
1879 (DESC_B_SHIFT - HF_SS32_SHIFT);
1880 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK) ||
1881 !(hflags & HF_CS32_MASK)) {
1882 hflags |= HF_ADDSEG_MASK;
1883 } else {
1884 hflags |= ((env->segs[R_DS].base | env->segs[R_ES].base |
1885 env->segs[R_SS].base) != 0) << HF_ADDSEG_SHIFT;
1888 env->hflags = hflags;
1890 return 0;
1893 static int kvm_get_msrs(X86CPU *cpu)
1895 CPUX86State *env = &cpu->env;
1896 struct {
1897 struct kvm_msrs info;
1898 struct kvm_msr_entry entries[150];
1899 } msr_data;
1900 struct kvm_msr_entry *msrs = msr_data.entries;
1901 int ret, i, n;
1903 n = 0;
1904 msrs[n++].index = MSR_IA32_SYSENTER_CS;
1905 msrs[n++].index = MSR_IA32_SYSENTER_ESP;
1906 msrs[n++].index = MSR_IA32_SYSENTER_EIP;
1907 msrs[n++].index = MSR_PAT;
1908 if (has_msr_star) {
1909 msrs[n++].index = MSR_STAR;
1911 if (has_msr_hsave_pa) {
1912 msrs[n++].index = MSR_VM_HSAVE_PA;
1914 if (has_msr_tsc_aux) {
1915 msrs[n++].index = MSR_TSC_AUX;
1917 if (has_msr_tsc_adjust) {
1918 msrs[n++].index = MSR_TSC_ADJUST;
1920 if (has_msr_tsc_deadline) {
1921 msrs[n++].index = MSR_IA32_TSCDEADLINE;
1923 if (has_msr_misc_enable) {
1924 msrs[n++].index = MSR_IA32_MISC_ENABLE;
1926 if (has_msr_smbase) {
1927 msrs[n++].index = MSR_IA32_SMBASE;
1929 if (has_msr_feature_control) {
1930 msrs[n++].index = MSR_IA32_FEATURE_CONTROL;
1932 if (has_msr_bndcfgs) {
1933 msrs[n++].index = MSR_IA32_BNDCFGS;
1935 if (has_msr_xss) {
1936 msrs[n++].index = MSR_IA32_XSS;
1940 if (!env->tsc_valid) {
1941 msrs[n++].index = MSR_IA32_TSC;
1942 env->tsc_valid = !runstate_is_running();
1945 #ifdef TARGET_X86_64
1946 if (lm_capable_kernel) {
1947 msrs[n++].index = MSR_CSTAR;
1948 msrs[n++].index = MSR_KERNELGSBASE;
1949 msrs[n++].index = MSR_FMASK;
1950 msrs[n++].index = MSR_LSTAR;
1952 #endif
1953 msrs[n++].index = MSR_KVM_SYSTEM_TIME;
1954 msrs[n++].index = MSR_KVM_WALL_CLOCK;
1955 if (has_msr_async_pf_en) {
1956 msrs[n++].index = MSR_KVM_ASYNC_PF_EN;
1958 if (has_msr_pv_eoi_en) {
1959 msrs[n++].index = MSR_KVM_PV_EOI_EN;
1961 if (has_msr_kvm_steal_time) {
1962 msrs[n++].index = MSR_KVM_STEAL_TIME;
1964 if (has_msr_architectural_pmu) {
1965 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR_CTRL;
1966 msrs[n++].index = MSR_CORE_PERF_GLOBAL_CTRL;
1967 msrs[n++].index = MSR_CORE_PERF_GLOBAL_STATUS;
1968 msrs[n++].index = MSR_CORE_PERF_GLOBAL_OVF_CTRL;
1969 for (i = 0; i < MAX_FIXED_COUNTERS; i++) {
1970 msrs[n++].index = MSR_CORE_PERF_FIXED_CTR0 + i;
1972 for (i = 0; i < num_architectural_pmu_counters; i++) {
1973 msrs[n++].index = MSR_P6_PERFCTR0 + i;
1974 msrs[n++].index = MSR_P6_EVNTSEL0 + i;
1978 if (env->mcg_cap) {
1979 msrs[n++].index = MSR_MCG_STATUS;
1980 msrs[n++].index = MSR_MCG_CTL;
1981 for (i = 0; i < (env->mcg_cap & 0xff) * 4; i++) {
1982 msrs[n++].index = MSR_MC0_CTL + i;
1986 if (has_msr_hv_hypercall) {
1987 msrs[n++].index = HV_X64_MSR_HYPERCALL;
1988 msrs[n++].index = HV_X64_MSR_GUEST_OS_ID;
1990 if (has_msr_hv_vapic) {
1991 msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
1993 if (has_msr_hv_tsc) {
1994 msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
1996 if (has_msr_hv_crash) {
1997 int j;
1999 for (j = 0; j < HV_X64_MSR_CRASH_PARAMS; j++) {
2000 msrs[n++].index = HV_X64_MSR_CRASH_P0 + j;
2003 if (has_msr_hv_runtime) {
2004 msrs[n++].index = HV_X64_MSR_VP_RUNTIME;
2006 if (cpu->hyperv_synic) {
2007 uint32_t msr;
2009 msrs[n++].index = HV_X64_MSR_SCONTROL;
2010 msrs[n++].index = HV_X64_MSR_SVERSION;
2011 msrs[n++].index = HV_X64_MSR_SIEFP;
2012 msrs[n++].index = HV_X64_MSR_SIMP;
2013 for (msr = HV_X64_MSR_SINT0; msr <= HV_X64_MSR_SINT15; msr++) {
2014 msrs[n++].index = msr;
2017 if (has_msr_hv_stimer) {
2018 uint32_t msr;
2020 for (msr = HV_X64_MSR_STIMER0_CONFIG; msr <= HV_X64_MSR_STIMER3_COUNT;
2021 msr++) {
2022 msrs[n++].index = msr;
2025 if (has_msr_mtrr) {
2026 msrs[n++].index = MSR_MTRRdefType;
2027 msrs[n++].index = MSR_MTRRfix64K_00000;
2028 msrs[n++].index = MSR_MTRRfix16K_80000;
2029 msrs[n++].index = MSR_MTRRfix16K_A0000;
2030 msrs[n++].index = MSR_MTRRfix4K_C0000;
2031 msrs[n++].index = MSR_MTRRfix4K_C8000;
2032 msrs[n++].index = MSR_MTRRfix4K_D0000;
2033 msrs[n++].index = MSR_MTRRfix4K_D8000;
2034 msrs[n++].index = MSR_MTRRfix4K_E0000;
2035 msrs[n++].index = MSR_MTRRfix4K_E8000;
2036 msrs[n++].index = MSR_MTRRfix4K_F0000;
2037 msrs[n++].index = MSR_MTRRfix4K_F8000;
2038 for (i = 0; i < MSR_MTRRcap_VCNT; i++) {
2039 msrs[n++].index = MSR_MTRRphysBase(i);
2040 msrs[n++].index = MSR_MTRRphysMask(i);
2044 msr_data.info = (struct kvm_msrs) {
2045 .nmsrs = n,
2048 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
2049 if (ret < 0) {
2050 return ret;
2053 for (i = 0; i < ret; i++) {
2054 uint32_t index = msrs[i].index;
2055 switch (index) {
2056 case MSR_IA32_SYSENTER_CS:
2057 env->sysenter_cs = msrs[i].data;
2058 break;
2059 case MSR_IA32_SYSENTER_ESP:
2060 env->sysenter_esp = msrs[i].data;
2061 break;
2062 case MSR_IA32_SYSENTER_EIP:
2063 env->sysenter_eip = msrs[i].data;
2064 break;
2065 case MSR_PAT:
2066 env->pat = msrs[i].data;
2067 break;
2068 case MSR_STAR:
2069 env->star = msrs[i].data;
2070 break;
2071 #ifdef TARGET_X86_64
2072 case MSR_CSTAR:
2073 env->cstar = msrs[i].data;
2074 break;
2075 case MSR_KERNELGSBASE:
2076 env->kernelgsbase = msrs[i].data;
2077 break;
2078 case MSR_FMASK:
2079 env->fmask = msrs[i].data;
2080 break;
2081 case MSR_LSTAR:
2082 env->lstar = msrs[i].data;
2083 break;
2084 #endif
2085 case MSR_IA32_TSC:
2086 env->tsc = msrs[i].data;
2087 break;
2088 case MSR_TSC_AUX:
2089 env->tsc_aux = msrs[i].data;
2090 break;
2091 case MSR_TSC_ADJUST:
2092 env->tsc_adjust = msrs[i].data;
2093 break;
2094 case MSR_IA32_TSCDEADLINE:
2095 env->tsc_deadline = msrs[i].data;
2096 break;
2097 case MSR_VM_HSAVE_PA:
2098 env->vm_hsave = msrs[i].data;
2099 break;
2100 case MSR_KVM_SYSTEM_TIME:
2101 env->system_time_msr = msrs[i].data;
2102 break;
2103 case MSR_KVM_WALL_CLOCK:
2104 env->wall_clock_msr = msrs[i].data;
2105 break;
2106 case MSR_MCG_STATUS:
2107 env->mcg_status = msrs[i].data;
2108 break;
2109 case MSR_MCG_CTL:
2110 env->mcg_ctl = msrs[i].data;
2111 break;
2112 case MSR_IA32_MISC_ENABLE:
2113 env->msr_ia32_misc_enable = msrs[i].data;
2114 break;
2115 case MSR_IA32_SMBASE:
2116 env->smbase = msrs[i].data;
2117 break;
2118 case MSR_IA32_FEATURE_CONTROL:
2119 env->msr_ia32_feature_control = msrs[i].data;
2120 break;
2121 case MSR_IA32_BNDCFGS:
2122 env->msr_bndcfgs = msrs[i].data;
2123 break;
2124 case MSR_IA32_XSS:
2125 env->xss = msrs[i].data;
2126 break;
2127 default:
2128 if (msrs[i].index >= MSR_MC0_CTL &&
2129 msrs[i].index < MSR_MC0_CTL + (env->mcg_cap & 0xff) * 4) {
2130 env->mce_banks[msrs[i].index - MSR_MC0_CTL] = msrs[i].data;
2132 break;
2133 case MSR_KVM_ASYNC_PF_EN:
2134 env->async_pf_en_msr = msrs[i].data;
2135 break;
2136 case MSR_KVM_PV_EOI_EN:
2137 env->pv_eoi_en_msr = msrs[i].data;
2138 break;
2139 case MSR_KVM_STEAL_TIME:
2140 env->steal_time_msr = msrs[i].data;
2141 break;
2142 case MSR_CORE_PERF_FIXED_CTR_CTRL:
2143 env->msr_fixed_ctr_ctrl = msrs[i].data;
2144 break;
2145 case MSR_CORE_PERF_GLOBAL_CTRL:
2146 env->msr_global_ctrl = msrs[i].data;
2147 break;
2148 case MSR_CORE_PERF_GLOBAL_STATUS:
2149 env->msr_global_status = msrs[i].data;
2150 break;
2151 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
2152 env->msr_global_ovf_ctrl = msrs[i].data;
2153 break;
2154 case MSR_CORE_PERF_FIXED_CTR0 ... MSR_CORE_PERF_FIXED_CTR0 + MAX_FIXED_COUNTERS - 1:
2155 env->msr_fixed_counters[index - MSR_CORE_PERF_FIXED_CTR0] = msrs[i].data;
2156 break;
2157 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR0 + MAX_GP_COUNTERS - 1:
2158 env->msr_gp_counters[index - MSR_P6_PERFCTR0] = msrs[i].data;
2159 break;
2160 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL0 + MAX_GP_COUNTERS - 1:
2161 env->msr_gp_evtsel[index - MSR_P6_EVNTSEL0] = msrs[i].data;
2162 break;
2163 case HV_X64_MSR_HYPERCALL:
2164 env->msr_hv_hypercall = msrs[i].data;
2165 break;
2166 case HV_X64_MSR_GUEST_OS_ID:
2167 env->msr_hv_guest_os_id = msrs[i].data;
2168 break;
2169 case HV_X64_MSR_APIC_ASSIST_PAGE:
2170 env->msr_hv_vapic = msrs[i].data;
2171 break;
2172 case HV_X64_MSR_REFERENCE_TSC:
2173 env->msr_hv_tsc = msrs[i].data;
2174 break;
2175 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2176 env->msr_hv_crash_params[index - HV_X64_MSR_CRASH_P0] = msrs[i].data;
2177 break;
2178 case HV_X64_MSR_VP_RUNTIME:
2179 env->msr_hv_runtime = msrs[i].data;
2180 break;
2181 case HV_X64_MSR_SCONTROL:
2182 env->msr_hv_synic_control = msrs[i].data;
2183 break;
2184 case HV_X64_MSR_SVERSION:
2185 env->msr_hv_synic_version = msrs[i].data;
2186 break;
2187 case HV_X64_MSR_SIEFP:
2188 env->msr_hv_synic_evt_page = msrs[i].data;
2189 break;
2190 case HV_X64_MSR_SIMP:
2191 env->msr_hv_synic_msg_page = msrs[i].data;
2192 break;
2193 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2194 env->msr_hv_synic_sint[index - HV_X64_MSR_SINT0] = msrs[i].data;
2195 break;
2196 case HV_X64_MSR_STIMER0_CONFIG:
2197 case HV_X64_MSR_STIMER1_CONFIG:
2198 case HV_X64_MSR_STIMER2_CONFIG:
2199 case HV_X64_MSR_STIMER3_CONFIG:
2200 env->msr_hv_stimer_config[(index - HV_X64_MSR_STIMER0_CONFIG)/2] =
2201 msrs[i].data;
2202 break;
2203 case HV_X64_MSR_STIMER0_COUNT:
2204 case HV_X64_MSR_STIMER1_COUNT:
2205 case HV_X64_MSR_STIMER2_COUNT:
2206 case HV_X64_MSR_STIMER3_COUNT:
2207 env->msr_hv_stimer_count[(index - HV_X64_MSR_STIMER0_COUNT)/2] =
2208 msrs[i].data;
2209 break;
2210 case MSR_MTRRdefType:
2211 env->mtrr_deftype = msrs[i].data;
2212 break;
2213 case MSR_MTRRfix64K_00000:
2214 env->mtrr_fixed[0] = msrs[i].data;
2215 break;
2216 case MSR_MTRRfix16K_80000:
2217 env->mtrr_fixed[1] = msrs[i].data;
2218 break;
2219 case MSR_MTRRfix16K_A0000:
2220 env->mtrr_fixed[2] = msrs[i].data;
2221 break;
2222 case MSR_MTRRfix4K_C0000:
2223 env->mtrr_fixed[3] = msrs[i].data;
2224 break;
2225 case MSR_MTRRfix4K_C8000:
2226 env->mtrr_fixed[4] = msrs[i].data;
2227 break;
2228 case MSR_MTRRfix4K_D0000:
2229 env->mtrr_fixed[5] = msrs[i].data;
2230 break;
2231 case MSR_MTRRfix4K_D8000:
2232 env->mtrr_fixed[6] = msrs[i].data;
2233 break;
2234 case MSR_MTRRfix4K_E0000:
2235 env->mtrr_fixed[7] = msrs[i].data;
2236 break;
2237 case MSR_MTRRfix4K_E8000:
2238 env->mtrr_fixed[8] = msrs[i].data;
2239 break;
2240 case MSR_MTRRfix4K_F0000:
2241 env->mtrr_fixed[9] = msrs[i].data;
2242 break;
2243 case MSR_MTRRfix4K_F8000:
2244 env->mtrr_fixed[10] = msrs[i].data;
2245 break;
2246 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT - 1):
2247 if (index & 1) {
2248 env->mtrr_var[MSR_MTRRphysIndex(index)].mask = msrs[i].data;
2249 } else {
2250 env->mtrr_var[MSR_MTRRphysIndex(index)].base = msrs[i].data;
2252 break;
2256 return 0;
2259 static int kvm_put_mp_state(X86CPU *cpu)
2261 struct kvm_mp_state mp_state = { .mp_state = cpu->env.mp_state };
2263 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state);
2266 static int kvm_get_mp_state(X86CPU *cpu)
2268 CPUState *cs = CPU(cpu);
2269 CPUX86State *env = &cpu->env;
2270 struct kvm_mp_state mp_state;
2271 int ret;
2273 ret = kvm_vcpu_ioctl(cs, KVM_GET_MP_STATE, &mp_state);
2274 if (ret < 0) {
2275 return ret;
2277 env->mp_state = mp_state.mp_state;
2278 if (kvm_irqchip_in_kernel()) {
2279 cs->halted = (mp_state.mp_state == KVM_MP_STATE_HALTED);
2281 return 0;
2284 static int kvm_get_apic(X86CPU *cpu)
2286 DeviceState *apic = cpu->apic_state;
2287 struct kvm_lapic_state kapic;
2288 int ret;
2290 if (apic && kvm_irqchip_in_kernel()) {
2291 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_LAPIC, &kapic);
2292 if (ret < 0) {
2293 return ret;
2296 kvm_get_apic_state(apic, &kapic);
2298 return 0;
2301 static int kvm_put_apic(X86CPU *cpu)
2303 DeviceState *apic = cpu->apic_state;
2304 struct kvm_lapic_state kapic;
2306 if (apic && kvm_irqchip_in_kernel()) {
2307 kvm_put_apic_state(apic, &kapic);
2309 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_LAPIC, &kapic);
2311 return 0;
2314 static int kvm_put_vcpu_events(X86CPU *cpu, int level)
2316 CPUState *cs = CPU(cpu);
2317 CPUX86State *env = &cpu->env;
2318 struct kvm_vcpu_events events = {};
2320 if (!kvm_has_vcpu_events()) {
2321 return 0;
2324 events.exception.injected = (env->exception_injected >= 0);
2325 events.exception.nr = env->exception_injected;
2326 events.exception.has_error_code = env->has_error_code;
2327 events.exception.error_code = env->error_code;
2328 events.exception.pad = 0;
2330 events.interrupt.injected = (env->interrupt_injected >= 0);
2331 events.interrupt.nr = env->interrupt_injected;
2332 events.interrupt.soft = env->soft_interrupt;
2334 events.nmi.injected = env->nmi_injected;
2335 events.nmi.pending = env->nmi_pending;
2336 events.nmi.masked = !!(env->hflags2 & HF2_NMI_MASK);
2337 events.nmi.pad = 0;
2339 events.sipi_vector = env->sipi_vector;
2341 if (has_msr_smbase) {
2342 events.smi.smm = !!(env->hflags & HF_SMM_MASK);
2343 events.smi.smm_inside_nmi = !!(env->hflags2 & HF2_SMM_INSIDE_NMI_MASK);
2344 if (kvm_irqchip_in_kernel()) {
2345 /* As soon as these are moved to the kernel, remove them
2346 * from cs->interrupt_request.
2348 events.smi.pending = cs->interrupt_request & CPU_INTERRUPT_SMI;
2349 events.smi.latched_init = cs->interrupt_request & CPU_INTERRUPT_INIT;
2350 cs->interrupt_request &= ~(CPU_INTERRUPT_INIT | CPU_INTERRUPT_SMI);
2351 } else {
2352 /* Keep these in cs->interrupt_request. */
2353 events.smi.pending = 0;
2354 events.smi.latched_init = 0;
2356 events.flags |= KVM_VCPUEVENT_VALID_SMM;
2359 events.flags = 0;
2360 if (level >= KVM_PUT_RESET_STATE) {
2361 events.flags |=
2362 KVM_VCPUEVENT_VALID_NMI_PENDING | KVM_VCPUEVENT_VALID_SIPI_VECTOR;
2365 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
2368 static int kvm_get_vcpu_events(X86CPU *cpu)
2370 CPUX86State *env = &cpu->env;
2371 struct kvm_vcpu_events events;
2372 int ret;
2374 if (!kvm_has_vcpu_events()) {
2375 return 0;
2378 memset(&events, 0, sizeof(events));
2379 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_VCPU_EVENTS, &events);
2380 if (ret < 0) {
2381 return ret;
2383 env->exception_injected =
2384 events.exception.injected ? events.exception.nr : -1;
2385 env->has_error_code = events.exception.has_error_code;
2386 env->error_code = events.exception.error_code;
2388 env->interrupt_injected =
2389 events.interrupt.injected ? events.interrupt.nr : -1;
2390 env->soft_interrupt = events.interrupt.soft;
2392 env->nmi_injected = events.nmi.injected;
2393 env->nmi_pending = events.nmi.pending;
2394 if (events.nmi.masked) {
2395 env->hflags2 |= HF2_NMI_MASK;
2396 } else {
2397 env->hflags2 &= ~HF2_NMI_MASK;
2400 if (events.flags & KVM_VCPUEVENT_VALID_SMM) {
2401 if (events.smi.smm) {
2402 env->hflags |= HF_SMM_MASK;
2403 } else {
2404 env->hflags &= ~HF_SMM_MASK;
2406 if (events.smi.pending) {
2407 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
2408 } else {
2409 cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
2411 if (events.smi.smm_inside_nmi) {
2412 env->hflags2 |= HF2_SMM_INSIDE_NMI_MASK;
2413 } else {
2414 env->hflags2 &= ~HF2_SMM_INSIDE_NMI_MASK;
2416 if (events.smi.latched_init) {
2417 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_INIT);
2418 } else {
2419 cpu_reset_interrupt(CPU(cpu), CPU_INTERRUPT_INIT);
2423 env->sipi_vector = events.sipi_vector;
2425 return 0;
2428 static int kvm_guest_debug_workarounds(X86CPU *cpu)
2430 CPUState *cs = CPU(cpu);
2431 CPUX86State *env = &cpu->env;
2432 int ret = 0;
2433 unsigned long reinject_trap = 0;
2435 if (!kvm_has_vcpu_events()) {
2436 if (env->exception_injected == 1) {
2437 reinject_trap = KVM_GUESTDBG_INJECT_DB;
2438 } else if (env->exception_injected == 3) {
2439 reinject_trap = KVM_GUESTDBG_INJECT_BP;
2441 env->exception_injected = -1;
2445 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
2446 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
2447 * by updating the debug state once again if single-stepping is on.
2448 * Another reason to call kvm_update_guest_debug here is a pending debug
2449 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
2450 * reinject them via SET_GUEST_DEBUG.
2452 if (reinject_trap ||
2453 (!kvm_has_robust_singlestep() && cs->singlestep_enabled)) {
2454 ret = kvm_update_guest_debug(cs, reinject_trap);
2456 return ret;
2459 static int kvm_put_debugregs(X86CPU *cpu)
2461 CPUX86State *env = &cpu->env;
2462 struct kvm_debugregs dbgregs;
2463 int i;
2465 if (!kvm_has_debugregs()) {
2466 return 0;
2469 for (i = 0; i < 4; i++) {
2470 dbgregs.db[i] = env->dr[i];
2472 dbgregs.dr6 = env->dr[6];
2473 dbgregs.dr7 = env->dr[7];
2474 dbgregs.flags = 0;
2476 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_DEBUGREGS, &dbgregs);
2479 static int kvm_get_debugregs(X86CPU *cpu)
2481 CPUX86State *env = &cpu->env;
2482 struct kvm_debugregs dbgregs;
2483 int i, ret;
2485 if (!kvm_has_debugregs()) {
2486 return 0;
2489 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_DEBUGREGS, &dbgregs);
2490 if (ret < 0) {
2491 return ret;
2493 for (i = 0; i < 4; i++) {
2494 env->dr[i] = dbgregs.db[i];
2496 env->dr[4] = env->dr[6] = dbgregs.dr6;
2497 env->dr[5] = env->dr[7] = dbgregs.dr7;
2499 return 0;
2502 int kvm_arch_put_registers(CPUState *cpu, int level)
2504 X86CPU *x86_cpu = X86_CPU(cpu);
2505 int ret;
2507 assert(cpu_is_stopped(cpu) || qemu_cpu_is_self(cpu));
2509 if (level >= KVM_PUT_RESET_STATE && has_msr_feature_control) {
2510 ret = kvm_put_msr_feature_control(x86_cpu);
2511 if (ret < 0) {
2512 return ret;
2516 if (level == KVM_PUT_FULL_STATE) {
2517 /* We don't check for kvm_arch_set_tsc_khz() errors here,
2518 * because TSC frequency mismatch shouldn't abort migration,
2519 * unless the user explicitly asked for a more strict TSC
2520 * setting (e.g. using an explicit "tsc-freq" option).
2522 kvm_arch_set_tsc_khz(cpu);
2525 ret = kvm_getput_regs(x86_cpu, 1);
2526 if (ret < 0) {
2527 return ret;
2529 ret = kvm_put_xsave(x86_cpu);
2530 if (ret < 0) {
2531 return ret;
2533 ret = kvm_put_xcrs(x86_cpu);
2534 if (ret < 0) {
2535 return ret;
2537 ret = kvm_put_sregs(x86_cpu);
2538 if (ret < 0) {
2539 return ret;
2541 /* must be before kvm_put_msrs */
2542 ret = kvm_inject_mce_oldstyle(x86_cpu);
2543 if (ret < 0) {
2544 return ret;
2546 ret = kvm_put_msrs(x86_cpu, level);
2547 if (ret < 0) {
2548 return ret;
2550 if (level >= KVM_PUT_RESET_STATE) {
2551 ret = kvm_put_mp_state(x86_cpu);
2552 if (ret < 0) {
2553 return ret;
2555 ret = kvm_put_apic(x86_cpu);
2556 if (ret < 0) {
2557 return ret;
2561 ret = kvm_put_tscdeadline_msr(x86_cpu);
2562 if (ret < 0) {
2563 return ret;
2566 ret = kvm_put_vcpu_events(x86_cpu, level);
2567 if (ret < 0) {
2568 return ret;
2570 ret = kvm_put_debugregs(x86_cpu);
2571 if (ret < 0) {
2572 return ret;
2574 /* must be last */
2575 ret = kvm_guest_debug_workarounds(x86_cpu);
2576 if (ret < 0) {
2577 return ret;
2579 return 0;
2582 int kvm_arch_get_registers(CPUState *cs)
2584 X86CPU *cpu = X86_CPU(cs);
2585 int ret;
2587 assert(cpu_is_stopped(cs) || qemu_cpu_is_self(cs));
2589 ret = kvm_getput_regs(cpu, 0);
2590 if (ret < 0) {
2591 goto out;
2593 ret = kvm_get_xsave(cpu);
2594 if (ret < 0) {
2595 goto out;
2597 ret = kvm_get_xcrs(cpu);
2598 if (ret < 0) {
2599 goto out;
2601 ret = kvm_get_sregs(cpu);
2602 if (ret < 0) {
2603 goto out;
2605 ret = kvm_get_msrs(cpu);
2606 if (ret < 0) {
2607 goto out;
2609 ret = kvm_get_mp_state(cpu);
2610 if (ret < 0) {
2611 goto out;
2613 ret = kvm_get_apic(cpu);
2614 if (ret < 0) {
2615 goto out;
2617 ret = kvm_get_vcpu_events(cpu);
2618 if (ret < 0) {
2619 goto out;
2621 ret = kvm_get_debugregs(cpu);
2622 if (ret < 0) {
2623 goto out;
2625 ret = 0;
2626 out:
2627 cpu_sync_bndcs_hflags(&cpu->env);
2628 return ret;
2631 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
2633 X86CPU *x86_cpu = X86_CPU(cpu);
2634 CPUX86State *env = &x86_cpu->env;
2635 int ret;
2637 /* Inject NMI */
2638 if (cpu->interrupt_request & (CPU_INTERRUPT_NMI | CPU_INTERRUPT_SMI)) {
2639 if (cpu->interrupt_request & CPU_INTERRUPT_NMI) {
2640 qemu_mutex_lock_iothread();
2641 cpu->interrupt_request &= ~CPU_INTERRUPT_NMI;
2642 qemu_mutex_unlock_iothread();
2643 DPRINTF("injected NMI\n");
2644 ret = kvm_vcpu_ioctl(cpu, KVM_NMI);
2645 if (ret < 0) {
2646 fprintf(stderr, "KVM: injection failed, NMI lost (%s)\n",
2647 strerror(-ret));
2650 if (cpu->interrupt_request & CPU_INTERRUPT_SMI) {
2651 qemu_mutex_lock_iothread();
2652 cpu->interrupt_request &= ~CPU_INTERRUPT_SMI;
2653 qemu_mutex_unlock_iothread();
2654 DPRINTF("injected SMI\n");
2655 ret = kvm_vcpu_ioctl(cpu, KVM_SMI);
2656 if (ret < 0) {
2657 fprintf(stderr, "KVM: injection failed, SMI lost (%s)\n",
2658 strerror(-ret));
2663 if (!kvm_pic_in_kernel()) {
2664 qemu_mutex_lock_iothread();
2667 /* Force the VCPU out of its inner loop to process any INIT requests
2668 * or (for userspace APIC, but it is cheap to combine the checks here)
2669 * pending TPR access reports.
2671 if (cpu->interrupt_request & (CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR)) {
2672 if ((cpu->interrupt_request & CPU_INTERRUPT_INIT) &&
2673 !(env->hflags & HF_SMM_MASK)) {
2674 cpu->exit_request = 1;
2676 if (cpu->interrupt_request & CPU_INTERRUPT_TPR) {
2677 cpu->exit_request = 1;
2681 if (!kvm_pic_in_kernel()) {
2682 /* Try to inject an interrupt if the guest can accept it */
2683 if (run->ready_for_interrupt_injection &&
2684 (cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
2685 (env->eflags & IF_MASK)) {
2686 int irq;
2688 cpu->interrupt_request &= ~CPU_INTERRUPT_HARD;
2689 irq = cpu_get_pic_interrupt(env);
2690 if (irq >= 0) {
2691 struct kvm_interrupt intr;
2693 intr.irq = irq;
2694 DPRINTF("injected interrupt %d\n", irq);
2695 ret = kvm_vcpu_ioctl(cpu, KVM_INTERRUPT, &intr);
2696 if (ret < 0) {
2697 fprintf(stderr,
2698 "KVM: injection failed, interrupt lost (%s)\n",
2699 strerror(-ret));
2704 /* If we have an interrupt but the guest is not ready to receive an
2705 * interrupt, request an interrupt window exit. This will
2706 * cause a return to userspace as soon as the guest is ready to
2707 * receive interrupts. */
2708 if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
2709 run->request_interrupt_window = 1;
2710 } else {
2711 run->request_interrupt_window = 0;
2714 DPRINTF("setting tpr\n");
2715 run->cr8 = cpu_get_apic_tpr(x86_cpu->apic_state);
2717 qemu_mutex_unlock_iothread();
2721 MemTxAttrs kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
2723 X86CPU *x86_cpu = X86_CPU(cpu);
2724 CPUX86State *env = &x86_cpu->env;
2726 if (run->flags & KVM_RUN_X86_SMM) {
2727 env->hflags |= HF_SMM_MASK;
2728 } else {
2729 env->hflags &= HF_SMM_MASK;
2731 if (run->if_flag) {
2732 env->eflags |= IF_MASK;
2733 } else {
2734 env->eflags &= ~IF_MASK;
2737 /* We need to protect the apic state against concurrent accesses from
2738 * different threads in case the userspace irqchip is used. */
2739 if (!kvm_irqchip_in_kernel()) {
2740 qemu_mutex_lock_iothread();
2742 cpu_set_apic_tpr(x86_cpu->apic_state, run->cr8);
2743 cpu_set_apic_base(x86_cpu->apic_state, run->apic_base);
2744 if (!kvm_irqchip_in_kernel()) {
2745 qemu_mutex_unlock_iothread();
2747 return cpu_get_mem_attrs(env);
2750 int kvm_arch_process_async_events(CPUState *cs)
2752 X86CPU *cpu = X86_CPU(cs);
2753 CPUX86State *env = &cpu->env;
2755 if (cs->interrupt_request & CPU_INTERRUPT_MCE) {
2756 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2757 assert(env->mcg_cap);
2759 cs->interrupt_request &= ~CPU_INTERRUPT_MCE;
2761 kvm_cpu_synchronize_state(cs);
2763 if (env->exception_injected == EXCP08_DBLE) {
2764 /* this means triple fault */
2765 qemu_system_reset_request();
2766 cs->exit_request = 1;
2767 return 0;
2769 env->exception_injected = EXCP12_MCHK;
2770 env->has_error_code = 0;
2772 cs->halted = 0;
2773 if (kvm_irqchip_in_kernel() && env->mp_state == KVM_MP_STATE_HALTED) {
2774 env->mp_state = KVM_MP_STATE_RUNNABLE;
2778 if ((cs->interrupt_request & CPU_INTERRUPT_INIT) &&
2779 !(env->hflags & HF_SMM_MASK)) {
2780 kvm_cpu_synchronize_state(cs);
2781 do_cpu_init(cpu);
2784 if (kvm_irqchip_in_kernel()) {
2785 return 0;
2788 if (cs->interrupt_request & CPU_INTERRUPT_POLL) {
2789 cs->interrupt_request &= ~CPU_INTERRUPT_POLL;
2790 apic_poll_irq(cpu->apic_state);
2792 if (((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2793 (env->eflags & IF_MASK)) ||
2794 (cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2795 cs->halted = 0;
2797 if (cs->interrupt_request & CPU_INTERRUPT_SIPI) {
2798 kvm_cpu_synchronize_state(cs);
2799 do_cpu_sipi(cpu);
2801 if (cs->interrupt_request & CPU_INTERRUPT_TPR) {
2802 cs->interrupt_request &= ~CPU_INTERRUPT_TPR;
2803 kvm_cpu_synchronize_state(cs);
2804 apic_handle_tpr_access_report(cpu->apic_state, env->eip,
2805 env->tpr_access_type);
2808 return cs->halted;
2811 static int kvm_handle_halt(X86CPU *cpu)
2813 CPUState *cs = CPU(cpu);
2814 CPUX86State *env = &cpu->env;
2816 if (!((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
2817 (env->eflags & IF_MASK)) &&
2818 !(cs->interrupt_request & CPU_INTERRUPT_NMI)) {
2819 cs->halted = 1;
2820 return EXCP_HLT;
2823 return 0;
2826 static int kvm_handle_tpr_access(X86CPU *cpu)
2828 CPUState *cs = CPU(cpu);
2829 struct kvm_run *run = cs->kvm_run;
2831 apic_handle_tpr_access_report(cpu->apic_state, run->tpr_access.rip,
2832 run->tpr_access.is_write ? TPR_ACCESS_WRITE
2833 : TPR_ACCESS_READ);
2834 return 1;
2837 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2839 static const uint8_t int3 = 0xcc;
2841 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0) ||
2842 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&int3, 1, 1)) {
2843 return -EINVAL;
2845 return 0;
2848 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
2850 uint8_t int3;
2852 if (cpu_memory_rw_debug(cs, bp->pc, &int3, 1, 0) || int3 != 0xcc ||
2853 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
2854 return -EINVAL;
2856 return 0;
2859 static struct {
2860 target_ulong addr;
2861 int len;
2862 int type;
2863 } hw_breakpoint[4];
2865 static int nb_hw_breakpoint;
2867 static int find_hw_breakpoint(target_ulong addr, int len, int type)
2869 int n;
2871 for (n = 0; n < nb_hw_breakpoint; n++) {
2872 if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type &&
2873 (hw_breakpoint[n].len == len || len == -1)) {
2874 return n;
2877 return -1;
2880 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
2881 target_ulong len, int type)
2883 switch (type) {
2884 case GDB_BREAKPOINT_HW:
2885 len = 1;
2886 break;
2887 case GDB_WATCHPOINT_WRITE:
2888 case GDB_WATCHPOINT_ACCESS:
2889 switch (len) {
2890 case 1:
2891 break;
2892 case 2:
2893 case 4:
2894 case 8:
2895 if (addr & (len - 1)) {
2896 return -EINVAL;
2898 break;
2899 default:
2900 return -EINVAL;
2902 break;
2903 default:
2904 return -ENOSYS;
2907 if (nb_hw_breakpoint == 4) {
2908 return -ENOBUFS;
2910 if (find_hw_breakpoint(addr, len, type) >= 0) {
2911 return -EEXIST;
2913 hw_breakpoint[nb_hw_breakpoint].addr = addr;
2914 hw_breakpoint[nb_hw_breakpoint].len = len;
2915 hw_breakpoint[nb_hw_breakpoint].type = type;
2916 nb_hw_breakpoint++;
2918 return 0;
2921 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
2922 target_ulong len, int type)
2924 int n;
2926 n = find_hw_breakpoint(addr, (type == GDB_BREAKPOINT_HW) ? 1 : len, type);
2927 if (n < 0) {
2928 return -ENOENT;
2930 nb_hw_breakpoint--;
2931 hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint];
2933 return 0;
2936 void kvm_arch_remove_all_hw_breakpoints(void)
2938 nb_hw_breakpoint = 0;
2941 static CPUWatchpoint hw_watchpoint;
2943 static int kvm_handle_debug(X86CPU *cpu,
2944 struct kvm_debug_exit_arch *arch_info)
2946 CPUState *cs = CPU(cpu);
2947 CPUX86State *env = &cpu->env;
2948 int ret = 0;
2949 int n;
2951 if (arch_info->exception == 1) {
2952 if (arch_info->dr6 & (1 << 14)) {
2953 if (cs->singlestep_enabled) {
2954 ret = EXCP_DEBUG;
2956 } else {
2957 for (n = 0; n < 4; n++) {
2958 if (arch_info->dr6 & (1 << n)) {
2959 switch ((arch_info->dr7 >> (16 + n*4)) & 0x3) {
2960 case 0x0:
2961 ret = EXCP_DEBUG;
2962 break;
2963 case 0x1:
2964 ret = EXCP_DEBUG;
2965 cs->watchpoint_hit = &hw_watchpoint;
2966 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
2967 hw_watchpoint.flags = BP_MEM_WRITE;
2968 break;
2969 case 0x3:
2970 ret = EXCP_DEBUG;
2971 cs->watchpoint_hit = &hw_watchpoint;
2972 hw_watchpoint.vaddr = hw_breakpoint[n].addr;
2973 hw_watchpoint.flags = BP_MEM_ACCESS;
2974 break;
2979 } else if (kvm_find_sw_breakpoint(cs, arch_info->pc)) {
2980 ret = EXCP_DEBUG;
2982 if (ret == 0) {
2983 cpu_synchronize_state(cs);
2984 assert(env->exception_injected == -1);
2986 /* pass to guest */
2987 env->exception_injected = arch_info->exception;
2988 env->has_error_code = 0;
2991 return ret;
2994 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
2996 const uint8_t type_code[] = {
2997 [GDB_BREAKPOINT_HW] = 0x0,
2998 [GDB_WATCHPOINT_WRITE] = 0x1,
2999 [GDB_WATCHPOINT_ACCESS] = 0x3
3001 const uint8_t len_code[] = {
3002 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
3004 int n;
3006 if (kvm_sw_breakpoints_active(cpu)) {
3007 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
3009 if (nb_hw_breakpoint > 0) {
3010 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
3011 dbg->arch.debugreg[7] = 0x0600;
3012 for (n = 0; n < nb_hw_breakpoint; n++) {
3013 dbg->arch.debugreg[n] = hw_breakpoint[n].addr;
3014 dbg->arch.debugreg[7] |= (2 << (n * 2)) |
3015 (type_code[hw_breakpoint[n].type] << (16 + n*4)) |
3016 ((uint32_t)len_code[hw_breakpoint[n].len] << (18 + n*4));
3021 static bool host_supports_vmx(void)
3023 uint32_t ecx, unused;
3025 host_cpuid(1, 0, &unused, &unused, &ecx, &unused);
3026 return ecx & CPUID_EXT_VMX;
3029 #define VMX_INVALID_GUEST_STATE 0x80000021
3031 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
3033 X86CPU *cpu = X86_CPU(cs);
3034 uint64_t code;
3035 int ret;
3037 switch (run->exit_reason) {
3038 case KVM_EXIT_HLT:
3039 DPRINTF("handle_hlt\n");
3040 qemu_mutex_lock_iothread();
3041 ret = kvm_handle_halt(cpu);
3042 qemu_mutex_unlock_iothread();
3043 break;
3044 case KVM_EXIT_SET_TPR:
3045 ret = 0;
3046 break;
3047 case KVM_EXIT_TPR_ACCESS:
3048 qemu_mutex_lock_iothread();
3049 ret = kvm_handle_tpr_access(cpu);
3050 qemu_mutex_unlock_iothread();
3051 break;
3052 case KVM_EXIT_FAIL_ENTRY:
3053 code = run->fail_entry.hardware_entry_failure_reason;
3054 fprintf(stderr, "KVM: entry failed, hardware error 0x%" PRIx64 "\n",
3055 code);
3056 if (host_supports_vmx() && code == VMX_INVALID_GUEST_STATE) {
3057 fprintf(stderr,
3058 "\nIf you're running a guest on an Intel machine without "
3059 "unrestricted mode\n"
3060 "support, the failure can be most likely due to the guest "
3061 "entering an invalid\n"
3062 "state for Intel VT. For example, the guest maybe running "
3063 "in big real mode\n"
3064 "which is not supported on less recent Intel processors."
3065 "\n\n");
3067 ret = -1;
3068 break;
3069 case KVM_EXIT_EXCEPTION:
3070 fprintf(stderr, "KVM: exception %d exit (error code 0x%x)\n",
3071 run->ex.exception, run->ex.error_code);
3072 ret = -1;
3073 break;
3074 case KVM_EXIT_DEBUG:
3075 DPRINTF("kvm_exit_debug\n");
3076 qemu_mutex_lock_iothread();
3077 ret = kvm_handle_debug(cpu, &run->debug.arch);
3078 qemu_mutex_unlock_iothread();
3079 break;
3080 case KVM_EXIT_HYPERV:
3081 ret = kvm_hv_handle_exit(cpu, &run->hyperv);
3082 break;
3083 case KVM_EXIT_IOAPIC_EOI:
3084 ioapic_eoi_broadcast(run->eoi.vector);
3085 ret = 0;
3086 break;
3087 default:
3088 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
3089 ret = -1;
3090 break;
3093 return ret;
3096 bool kvm_arch_stop_on_emulation_error(CPUState *cs)
3098 X86CPU *cpu = X86_CPU(cs);
3099 CPUX86State *env = &cpu->env;
3101 kvm_cpu_synchronize_state(cs);
3102 return !(env->cr[0] & CR0_PE_MASK) ||
3103 ((env->segs[R_CS].selector & 3) != 3);
3106 void kvm_arch_init_irq_routing(KVMState *s)
3108 if (!kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) {
3109 /* If kernel can't do irq routing, interrupt source
3110 * override 0->2 cannot be set up as required by HPET.
3111 * So we have to disable it.
3113 no_hpet = 1;
3115 /* We know at this point that we're using the in-kernel
3116 * irqchip, so we can use irqfds, and on x86 we know
3117 * we can use msi via irqfd and GSI routing.
3119 kvm_msi_via_irqfd_allowed = true;
3120 kvm_gsi_routing_allowed = true;
3122 if (kvm_irqchip_is_split()) {
3123 int i;
3125 /* If the ioapic is in QEMU and the lapics are in KVM, reserve
3126 MSI routes for signaling interrupts to the local apics. */
3127 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
3128 struct MSIMessage msg = { 0x0, 0x0 };
3129 if (kvm_irqchip_add_msi_route(s, msg, NULL) < 0) {
3130 error_report("Could not enable split IRQ mode.");
3131 exit(1);
3137 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
3139 int ret;
3140 if (machine_kernel_irqchip_split(ms)) {
3141 ret = kvm_vm_enable_cap(s, KVM_CAP_SPLIT_IRQCHIP, 0, 24);
3142 if (ret) {
3143 error_report("Could not enable split irqchip mode: %s\n",
3144 strerror(-ret));
3145 exit(1);
3146 } else {
3147 DPRINTF("Enabled KVM_CAP_SPLIT_IRQCHIP\n");
3148 kvm_split_irqchip = true;
3149 return 1;
3151 } else {
3152 return 0;
3156 /* Classic KVM device assignment interface. Will remain x86 only. */
3157 int kvm_device_pci_assign(KVMState *s, PCIHostDeviceAddress *dev_addr,
3158 uint32_t flags, uint32_t *dev_id)
3160 struct kvm_assigned_pci_dev dev_data = {
3161 .segnr = dev_addr->domain,
3162 .busnr = dev_addr->bus,
3163 .devfn = PCI_DEVFN(dev_addr->slot, dev_addr->function),
3164 .flags = flags,
3166 int ret;
3168 dev_data.assigned_dev_id =
3169 (dev_addr->domain << 16) | (dev_addr->bus << 8) | dev_data.devfn;
3171 ret = kvm_vm_ioctl(s, KVM_ASSIGN_PCI_DEVICE, &dev_data);
3172 if (ret < 0) {
3173 return ret;
3176 *dev_id = dev_data.assigned_dev_id;
3178 return 0;
3181 int kvm_device_pci_deassign(KVMState *s, uint32_t dev_id)
3183 struct kvm_assigned_pci_dev dev_data = {
3184 .assigned_dev_id = dev_id,
3187 return kvm_vm_ioctl(s, KVM_DEASSIGN_PCI_DEVICE, &dev_data);
3190 static int kvm_assign_irq_internal(KVMState *s, uint32_t dev_id,
3191 uint32_t irq_type, uint32_t guest_irq)
3193 struct kvm_assigned_irq assigned_irq = {
3194 .assigned_dev_id = dev_id,
3195 .guest_irq = guest_irq,
3196 .flags = irq_type,
3199 if (kvm_check_extension(s, KVM_CAP_ASSIGN_DEV_IRQ)) {
3200 return kvm_vm_ioctl(s, KVM_ASSIGN_DEV_IRQ, &assigned_irq);
3201 } else {
3202 return kvm_vm_ioctl(s, KVM_ASSIGN_IRQ, &assigned_irq);
3206 int kvm_device_intx_assign(KVMState *s, uint32_t dev_id, bool use_host_msi,
3207 uint32_t guest_irq)
3209 uint32_t irq_type = KVM_DEV_IRQ_GUEST_INTX |
3210 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX);
3212 return kvm_assign_irq_internal(s, dev_id, irq_type, guest_irq);
3215 int kvm_device_intx_set_mask(KVMState *s, uint32_t dev_id, bool masked)
3217 struct kvm_assigned_pci_dev dev_data = {
3218 .assigned_dev_id = dev_id,
3219 .flags = masked ? KVM_DEV_ASSIGN_MASK_INTX : 0,
3222 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_INTX_MASK, &dev_data);
3225 static int kvm_deassign_irq_internal(KVMState *s, uint32_t dev_id,
3226 uint32_t type)
3228 struct kvm_assigned_irq assigned_irq = {
3229 .assigned_dev_id = dev_id,
3230 .flags = type,
3233 return kvm_vm_ioctl(s, KVM_DEASSIGN_DEV_IRQ, &assigned_irq);
3236 int kvm_device_intx_deassign(KVMState *s, uint32_t dev_id, bool use_host_msi)
3238 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_INTX |
3239 (use_host_msi ? KVM_DEV_IRQ_HOST_MSI : KVM_DEV_IRQ_HOST_INTX));
3242 int kvm_device_msi_assign(KVMState *s, uint32_t dev_id, int virq)
3244 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSI |
3245 KVM_DEV_IRQ_GUEST_MSI, virq);
3248 int kvm_device_msi_deassign(KVMState *s, uint32_t dev_id)
3250 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSI |
3251 KVM_DEV_IRQ_HOST_MSI);
3254 bool kvm_device_msix_supported(KVMState *s)
3256 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
3257 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
3258 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, NULL) == -EFAULT;
3261 int kvm_device_msix_init_vectors(KVMState *s, uint32_t dev_id,
3262 uint32_t nr_vectors)
3264 struct kvm_assigned_msix_nr msix_nr = {
3265 .assigned_dev_id = dev_id,
3266 .entry_nr = nr_vectors,
3269 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, &msix_nr);
3272 int kvm_device_msix_set_vector(KVMState *s, uint32_t dev_id, uint32_t vector,
3273 int virq)
3275 struct kvm_assigned_msix_entry msix_entry = {
3276 .assigned_dev_id = dev_id,
3277 .gsi = virq,
3278 .entry = vector,
3281 return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_ENTRY, &msix_entry);
3284 int kvm_device_msix_assign(KVMState *s, uint32_t dev_id)
3286 return kvm_assign_irq_internal(s, dev_id, KVM_DEV_IRQ_HOST_MSIX |
3287 KVM_DEV_IRQ_GUEST_MSIX, 0);
3290 int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
3292 return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX |
3293 KVM_DEV_IRQ_HOST_MSIX);
3296 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
3297 uint64_t address, uint32_t data, PCIDevice *dev)
3299 return 0;
3302 int kvm_arch_msi_data_to_gsi(uint32_t data)
3304 abort();