4 * Copyright (C) 2006-2008 Qumranet Technologies
5 * Copyright IBM, Corp. 2008
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 "qapi/error.h"
17 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/hw_accel.h"
27 #include "sysemu/kvm_int.h"
30 #include "hyperv-proto.h"
32 #include "exec/gdbstub.h"
33 #include "qemu/host-utils.h"
34 #include "qemu/config-file.h"
35 #include "qemu/error-report.h"
36 #include "hw/i386/pc.h"
37 #include "hw/i386/apic.h"
38 #include "hw/i386/apic_internal.h"
39 #include "hw/i386/apic-msidef.h"
40 #include "hw/i386/intel_iommu.h"
41 #include "hw/i386/x86-iommu.h"
43 #include "exec/ioport.h"
44 #include "hw/pci/pci.h"
45 #include "hw/pci/msi.h"
46 #include "hw/pci/msix.h"
47 #include "migration/blocker.h"
48 #include "exec/memattrs.h"
54 #define DPRINTF(fmt, ...) \
55 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
57 #define DPRINTF(fmt, ...) \
61 #define MSR_KVM_WALL_CLOCK 0x11
62 #define MSR_KVM_SYSTEM_TIME 0x12
64 /* A 4096-byte buffer can hold the 8-byte kvm_msrs header, plus
65 * 255 kvm_msr_entry structs */
66 #define MSR_BUF_SIZE 4096
68 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
69 KVM_CAP_INFO(SET_TSS_ADDR
),
70 KVM_CAP_INFO(EXT_CPUID
),
71 KVM_CAP_INFO(MP_STATE
),
75 static bool has_msr_star
;
76 static bool has_msr_hsave_pa
;
77 static bool has_msr_tsc_aux
;
78 static bool has_msr_tsc_adjust
;
79 static bool has_msr_tsc_deadline
;
80 static bool has_msr_feature_control
;
81 static bool has_msr_misc_enable
;
82 static bool has_msr_smbase
;
83 static bool has_msr_bndcfgs
;
84 static int lm_capable_kernel
;
85 static bool has_msr_hv_hypercall
;
86 static bool has_msr_hv_crash
;
87 static bool has_msr_hv_reset
;
88 static bool has_msr_hv_vpindex
;
89 static bool has_msr_hv_runtime
;
90 static bool has_msr_hv_synic
;
91 static bool has_msr_hv_stimer
;
92 static bool has_msr_hv_frequencies
;
93 static bool has_msr_xss
;
94 static bool has_msr_spec_ctrl
;
96 static uint32_t has_architectural_pmu_version
;
97 static uint32_t num_architectural_pmu_gp_counters
;
98 static uint32_t num_architectural_pmu_fixed_counters
;
100 static int has_xsave
;
102 static int has_pit_state2
;
104 static bool has_msr_mcg_ext_ctl
;
106 static struct kvm_cpuid2
*cpuid_cache
;
108 int kvm_has_pit_state2(void)
110 return has_pit_state2
;
113 bool kvm_has_smm(void)
115 return kvm_check_extension(kvm_state
, KVM_CAP_X86_SMM
);
118 bool kvm_has_adjust_clock_stable(void)
120 int ret
= kvm_check_extension(kvm_state
, KVM_CAP_ADJUST_CLOCK
);
122 return (ret
== KVM_CLOCK_TSC_STABLE
);
125 bool kvm_allows_irq0_override(void)
127 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
130 static bool kvm_x2apic_api_set_flags(uint64_t flags
)
132 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
134 return !kvm_vm_enable_cap(s
, KVM_CAP_X2APIC_API
, 0, flags
);
137 #define MEMORIZE(fn, _result) \
139 static bool _memorized; \
148 static bool has_x2apic_api
;
150 bool kvm_has_x2apic_api(void)
152 return has_x2apic_api
;
155 bool kvm_enable_x2apic(void)
158 kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS
|
159 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK
),
163 static int kvm_get_tsc(CPUState
*cs
)
165 X86CPU
*cpu
= X86_CPU(cs
);
166 CPUX86State
*env
= &cpu
->env
;
168 struct kvm_msrs info
;
169 struct kvm_msr_entry entries
[1];
173 if (env
->tsc_valid
) {
177 msr_data
.info
.nmsrs
= 1;
178 msr_data
.entries
[0].index
= MSR_IA32_TSC
;
179 env
->tsc_valid
= !runstate_is_running();
181 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
187 env
->tsc
= msr_data
.entries
[0].data
;
191 static inline void do_kvm_synchronize_tsc(CPUState
*cpu
, run_on_cpu_data arg
)
196 void kvm_synchronize_all_tsc(void)
202 run_on_cpu(cpu
, do_kvm_synchronize_tsc
, RUN_ON_CPU_NULL
);
207 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
209 struct kvm_cpuid2
*cpuid
;
212 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
213 cpuid
= g_malloc0(size
);
215 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
216 if (r
== 0 && cpuid
->nent
>= max
) {
224 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
232 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
235 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
237 struct kvm_cpuid2
*cpuid
;
240 if (cpuid_cache
!= NULL
) {
243 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
250 static const struct kvm_para_features
{
253 } para_features
[] = {
254 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
255 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
256 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
257 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
260 static int get_para_features(KVMState
*s
)
264 for (i
= 0; i
< ARRAY_SIZE(para_features
); i
++) {
265 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
266 features
|= (1 << para_features
[i
].feature
);
273 static bool host_tsx_blacklisted(void)
275 int family
, model
, stepping
;\
276 char vendor
[CPUID_VENDOR_SZ
+ 1];
278 host_vendor_fms(vendor
, &family
, &model
, &stepping
);
280 /* Check if we are running on a Haswell host known to have broken TSX */
281 return !strcmp(vendor
, CPUID_VENDOR_INTEL
) &&
283 ((model
== 63 && stepping
< 4) ||
284 model
== 60 || model
== 69 || model
== 70);
287 /* Returns the value for a specific register on the cpuid entry
289 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
309 /* Find matching entry for function/index on kvm_cpuid2 struct
311 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
316 for (i
= 0; i
< cpuid
->nent
; ++i
) {
317 if (cpuid
->entries
[i
].function
== function
&&
318 cpuid
->entries
[i
].index
== index
) {
319 return &cpuid
->entries
[i
];
326 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
327 uint32_t index
, int reg
)
329 struct kvm_cpuid2
*cpuid
;
331 uint32_t cpuid_1_edx
;
334 cpuid
= get_supported_cpuid(s
);
336 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
339 ret
= cpuid_entry_get_reg(entry
, reg
);
342 /* Fixups for the data returned by KVM, below */
344 if (function
== 1 && reg
== R_EDX
) {
345 /* KVM before 2.6.30 misreports the following features */
346 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
347 } else if (function
== 1 && reg
== R_ECX
) {
348 /* We can set the hypervisor flag, even if KVM does not return it on
349 * GET_SUPPORTED_CPUID
351 ret
|= CPUID_EXT_HYPERVISOR
;
352 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
353 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
354 * and the irqchip is in the kernel.
356 if (kvm_irqchip_in_kernel() &&
357 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
358 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
361 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
362 * without the in-kernel irqchip
364 if (!kvm_irqchip_in_kernel()) {
365 ret
&= ~CPUID_EXT_X2APIC
;
367 } else if (function
== 6 && reg
== R_EAX
) {
368 ret
|= CPUID_6_EAX_ARAT
; /* safe to allow because of emulated APIC */
369 } else if (function
== 7 && index
== 0 && reg
== R_EBX
) {
370 if (host_tsx_blacklisted()) {
371 ret
&= ~(CPUID_7_0_EBX_RTM
| CPUID_7_0_EBX_HLE
);
373 } else if (function
== 0x80000001 && reg
== R_EDX
) {
374 /* On Intel, kvm returns cpuid according to the Intel spec,
375 * so add missing bits according to the AMD spec:
377 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
378 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
379 } else if (function
== KVM_CPUID_FEATURES
&& reg
== R_EAX
) {
380 /* kvm_pv_unhalt is reported by GET_SUPPORTED_CPUID, but it can't
381 * be enabled without the in-kernel irqchip
383 if (!kvm_irqchip_in_kernel()) {
384 ret
&= ~(1U << KVM_FEATURE_PV_UNHALT
);
388 /* fallback for older kernels */
389 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
390 ret
= get_para_features(s
);
396 typedef struct HWPoisonPage
{
398 QLIST_ENTRY(HWPoisonPage
) list
;
401 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
402 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
404 static void kvm_unpoison_all(void *param
)
406 HWPoisonPage
*page
, *next_page
;
408 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
409 QLIST_REMOVE(page
, list
);
410 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
415 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
419 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
420 if (page
->ram_addr
== ram_addr
) {
424 page
= g_new(HWPoisonPage
, 1);
425 page
->ram_addr
= ram_addr
;
426 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
429 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
434 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
437 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
442 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
444 CPUState
*cs
= CPU(cpu
);
445 CPUX86State
*env
= &cpu
->env
;
446 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
447 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
448 uint64_t mcg_status
= MCG_STATUS_MCIP
;
451 if (code
== BUS_MCEERR_AR
) {
452 status
|= MCI_STATUS_AR
| 0x134;
453 mcg_status
|= MCG_STATUS_EIPV
;
456 mcg_status
|= MCG_STATUS_RIPV
;
459 flags
= cpu_x86_support_mca_broadcast(env
) ? MCE_INJECT_BROADCAST
: 0;
460 /* We need to read back the value of MSR_EXT_MCG_CTL that was set by the
461 * guest kernel back into env->mcg_ext_ctl.
463 cpu_synchronize_state(cs
);
464 if (env
->mcg_ext_ctl
& MCG_EXT_CTL_LMCE_EN
) {
465 mcg_status
|= MCG_STATUS_LMCE
;
469 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
470 (MCM_ADDR_PHYS
<< 6) | 0xc, flags
);
473 static void hardware_memory_error(void)
475 fprintf(stderr
, "Hardware memory error!\n");
479 void kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
481 X86CPU
*cpu
= X86_CPU(c
);
482 CPUX86State
*env
= &cpu
->env
;
486 /* If we get an action required MCE, it has been injected by KVM
487 * while the VM was running. An action optional MCE instead should
488 * be coming from the main thread, which qemu_init_sigbus identifies
489 * as the "early kill" thread.
491 assert(code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
);
493 if ((env
->mcg_cap
& MCG_SER_P
) && addr
) {
494 ram_addr
= qemu_ram_addr_from_host(addr
);
495 if (ram_addr
!= RAM_ADDR_INVALID
&&
496 kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
497 kvm_hwpoison_page_add(ram_addr
);
498 kvm_mce_inject(cpu
, paddr
, code
);
502 fprintf(stderr
, "Hardware memory error for memory used by "
503 "QEMU itself instead of guest system!\n");
506 if (code
== BUS_MCEERR_AR
) {
507 hardware_memory_error();
510 /* Hope we are lucky for AO MCE */
513 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
515 CPUX86State
*env
= &cpu
->env
;
517 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
518 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
519 struct kvm_x86_mce mce
;
521 env
->exception_injected
= -1;
524 * There must be at least one bank in use if an MCE is pending.
525 * Find it and use its values for the event injection.
527 for (bank
= 0; bank
< bank_num
; bank
++) {
528 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
532 assert(bank
< bank_num
);
535 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
536 mce
.mcg_status
= env
->mcg_status
;
537 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
538 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
540 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
545 static void cpu_update_state(void *opaque
, int running
, RunState state
)
547 CPUX86State
*env
= opaque
;
550 env
->tsc_valid
= false;
554 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
556 X86CPU
*cpu
= X86_CPU(cs
);
560 #ifndef KVM_CPUID_SIGNATURE_NEXT
561 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
564 static bool hyperv_hypercall_available(X86CPU
*cpu
)
566 return cpu
->hyperv_vapic
||
567 (cpu
->hyperv_spinlock_attempts
!= HYPERV_SPINLOCK_NEVER_RETRY
);
570 static bool hyperv_enabled(X86CPU
*cpu
)
572 CPUState
*cs
= CPU(cpu
);
573 return kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV
) > 0 &&
574 (hyperv_hypercall_available(cpu
) ||
576 cpu
->hyperv_relaxed_timing
||
579 cpu
->hyperv_vpindex
||
580 cpu
->hyperv_runtime
||
585 static int kvm_arch_set_tsc_khz(CPUState
*cs
)
587 X86CPU
*cpu
= X86_CPU(cs
);
588 CPUX86State
*env
= &cpu
->env
;
595 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
) ?
596 kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
) :
599 /* When KVM_SET_TSC_KHZ fails, it's an error only if the current
600 * TSC frequency doesn't match the one we want.
602 int cur_freq
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GET_TSC_KHZ
) ?
603 kvm_vcpu_ioctl(cs
, KVM_GET_TSC_KHZ
) :
605 if (cur_freq
<= 0 || cur_freq
!= env
->tsc_khz
) {
606 warn_report("TSC frequency mismatch between "
607 "VM (%" PRId64
" kHz) and host (%d kHz), "
608 "and TSC scaling unavailable",
609 env
->tsc_khz
, cur_freq
);
617 static bool tsc_is_stable_and_known(CPUX86State
*env
)
622 return (env
->features
[FEAT_8000_0007_EDX
] & CPUID_APM_INVTSC
)
623 || env
->user_tsc_khz
;
626 static int hyperv_handle_properties(CPUState
*cs
)
628 X86CPU
*cpu
= X86_CPU(cs
);
629 CPUX86State
*env
= &cpu
->env
;
631 if (cpu
->hyperv_time
&&
632 kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV_TIME
) <= 0) {
633 cpu
->hyperv_time
= false;
636 if (cpu
->hyperv_relaxed_timing
) {
637 env
->features
[FEAT_HYPERV_EAX
] |= HV_HYPERCALL_AVAILABLE
;
639 if (cpu
->hyperv_vapic
) {
640 env
->features
[FEAT_HYPERV_EAX
] |= HV_HYPERCALL_AVAILABLE
;
641 env
->features
[FEAT_HYPERV_EAX
] |= HV_APIC_ACCESS_AVAILABLE
;
643 if (cpu
->hyperv_time
) {
644 env
->features
[FEAT_HYPERV_EAX
] |= HV_HYPERCALL_AVAILABLE
;
645 env
->features
[FEAT_HYPERV_EAX
] |= HV_TIME_REF_COUNT_AVAILABLE
;
646 env
->features
[FEAT_HYPERV_EAX
] |= HV_REFERENCE_TSC_AVAILABLE
;
648 if (has_msr_hv_frequencies
&& tsc_is_stable_and_known(env
)) {
649 env
->features
[FEAT_HYPERV_EAX
] |= HV_ACCESS_FREQUENCY_MSRS
;
650 env
->features
[FEAT_HYPERV_EDX
] |= HV_FREQUENCY_MSRS_AVAILABLE
;
653 if (cpu
->hyperv_crash
&& has_msr_hv_crash
) {
654 env
->features
[FEAT_HYPERV_EDX
] |= HV_GUEST_CRASH_MSR_AVAILABLE
;
656 env
->features
[FEAT_HYPERV_EDX
] |= HV_CPU_DYNAMIC_PARTITIONING_AVAILABLE
;
657 if (cpu
->hyperv_reset
&& has_msr_hv_reset
) {
658 env
->features
[FEAT_HYPERV_EAX
] |= HV_RESET_AVAILABLE
;
660 if (cpu
->hyperv_vpindex
&& has_msr_hv_vpindex
) {
661 env
->features
[FEAT_HYPERV_EAX
] |= HV_VP_INDEX_AVAILABLE
;
663 if (cpu
->hyperv_runtime
&& has_msr_hv_runtime
) {
664 env
->features
[FEAT_HYPERV_EAX
] |= HV_VP_RUNTIME_AVAILABLE
;
666 if (cpu
->hyperv_synic
) {
667 if (!has_msr_hv_synic
||
668 kvm_vcpu_enable_cap(cs
, KVM_CAP_HYPERV_SYNIC
, 0)) {
669 fprintf(stderr
, "Hyper-V SynIC is not supported by kernel\n");
673 env
->features
[FEAT_HYPERV_EAX
] |= HV_SYNIC_AVAILABLE
;
675 if (cpu
->hyperv_stimer
) {
676 if (!has_msr_hv_stimer
) {
677 fprintf(stderr
, "Hyper-V timers aren't supported by kernel\n");
680 env
->features
[FEAT_HYPERV_EAX
] |= HV_SYNTIMERS_AVAILABLE
;
685 static Error
*invtsc_mig_blocker
;
687 #define KVM_MAX_CPUID_ENTRIES 100
689 int kvm_arch_init_vcpu(CPUState
*cs
)
692 struct kvm_cpuid2 cpuid
;
693 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
694 } QEMU_PACKED cpuid_data
;
695 X86CPU
*cpu
= X86_CPU(cs
);
696 CPUX86State
*env
= &cpu
->env
;
697 uint32_t limit
, i
, j
, cpuid_i
;
699 struct kvm_cpuid_entry2
*c
;
700 uint32_t signature
[3];
701 int kvm_base
= KVM_CPUID_SIGNATURE
;
703 Error
*local_err
= NULL
;
705 memset(&cpuid_data
, 0, sizeof(cpuid_data
));
709 r
= kvm_arch_set_tsc_khz(cs
);
714 /* vcpu's TSC frequency is either specified by user, or following
715 * the value used by KVM if the former is not present. In the
716 * latter case, we query it from KVM and record in env->tsc_khz,
717 * so that vcpu's TSC frequency can be migrated later via this field.
720 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GET_TSC_KHZ
) ?
721 kvm_vcpu_ioctl(cs
, KVM_GET_TSC_KHZ
) :
728 /* Paravirtualization CPUIDs */
729 if (hyperv_enabled(cpu
)) {
730 c
= &cpuid_data
.entries
[cpuid_i
++];
731 c
->function
= HV_CPUID_VENDOR_AND_MAX_FUNCTIONS
;
732 if (!cpu
->hyperv_vendor_id
) {
733 memcpy(signature
, "Microsoft Hv", 12);
735 size_t len
= strlen(cpu
->hyperv_vendor_id
);
738 error_report("hv-vendor-id truncated to 12 characters");
741 memset(signature
, 0, 12);
742 memcpy(signature
, cpu
->hyperv_vendor_id
, len
);
744 c
->eax
= HV_CPUID_MIN
;
745 c
->ebx
= signature
[0];
746 c
->ecx
= signature
[1];
747 c
->edx
= signature
[2];
749 c
= &cpuid_data
.entries
[cpuid_i
++];
750 c
->function
= HV_CPUID_INTERFACE
;
751 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
752 c
->eax
= signature
[0];
757 c
= &cpuid_data
.entries
[cpuid_i
++];
758 c
->function
= HV_CPUID_VERSION
;
762 c
= &cpuid_data
.entries
[cpuid_i
++];
763 c
->function
= HV_CPUID_FEATURES
;
764 r
= hyperv_handle_properties(cs
);
768 c
->eax
= env
->features
[FEAT_HYPERV_EAX
];
769 c
->ebx
= env
->features
[FEAT_HYPERV_EBX
];
770 c
->edx
= env
->features
[FEAT_HYPERV_EDX
];
772 c
= &cpuid_data
.entries
[cpuid_i
++];
773 c
->function
= HV_CPUID_ENLIGHTMENT_INFO
;
774 if (cpu
->hyperv_relaxed_timing
) {
775 c
->eax
|= HV_RELAXED_TIMING_RECOMMENDED
;
777 if (cpu
->hyperv_vapic
) {
778 c
->eax
|= HV_APIC_ACCESS_RECOMMENDED
;
780 c
->ebx
= cpu
->hyperv_spinlock_attempts
;
782 c
= &cpuid_data
.entries
[cpuid_i
++];
783 c
->function
= HV_CPUID_IMPLEMENT_LIMITS
;
785 c
->eax
= cpu
->hv_max_vps
;
788 kvm_base
= KVM_CPUID_SIGNATURE_NEXT
;
789 has_msr_hv_hypercall
= true;
792 if (cpu
->expose_kvm
) {
793 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
794 c
= &cpuid_data
.entries
[cpuid_i
++];
795 c
->function
= KVM_CPUID_SIGNATURE
| kvm_base
;
796 c
->eax
= KVM_CPUID_FEATURES
| kvm_base
;
797 c
->ebx
= signature
[0];
798 c
->ecx
= signature
[1];
799 c
->edx
= signature
[2];
801 c
= &cpuid_data
.entries
[cpuid_i
++];
802 c
->function
= KVM_CPUID_FEATURES
| kvm_base
;
803 c
->eax
= env
->features
[FEAT_KVM
];
806 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
808 for (i
= 0; i
<= limit
; i
++) {
809 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
810 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
813 c
= &cpuid_data
.entries
[cpuid_i
++];
817 /* Keep reading function 2 till all the input is received */
821 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
822 KVM_CPUID_FLAG_STATE_READ_NEXT
;
823 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
824 times
= c
->eax
& 0xff;
826 for (j
= 1; j
< times
; ++j
) {
827 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
828 fprintf(stderr
, "cpuid_data is full, no space for "
829 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
832 c
= &cpuid_data
.entries
[cpuid_i
++];
834 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
835 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
843 if (i
== 0xd && j
== 64) {
847 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
849 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
851 if (i
== 4 && c
->eax
== 0) {
854 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
857 if (i
== 0xd && c
->eax
== 0) {
860 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
861 fprintf(stderr
, "cpuid_data is full, no space for "
862 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
865 c
= &cpuid_data
.entries
[cpuid_i
++];
871 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
879 cpu_x86_cpuid(env
, 0x0a, 0, &eax
, &unused
, &unused
, &edx
);
881 has_architectural_pmu_version
= eax
& 0xff;
882 if (has_architectural_pmu_version
> 0) {
883 num_architectural_pmu_gp_counters
= (eax
& 0xff00) >> 8;
885 /* Shouldn't be more than 32, since that's the number of bits
886 * available in EBX to tell us _which_ counters are available.
889 if (num_architectural_pmu_gp_counters
> MAX_GP_COUNTERS
) {
890 num_architectural_pmu_gp_counters
= MAX_GP_COUNTERS
;
893 if (has_architectural_pmu_version
> 1) {
894 num_architectural_pmu_fixed_counters
= edx
& 0x1f;
896 if (num_architectural_pmu_fixed_counters
> MAX_FIXED_COUNTERS
) {
897 num_architectural_pmu_fixed_counters
= MAX_FIXED_COUNTERS
;
903 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
905 for (i
= 0x80000000; i
<= limit
; i
++) {
906 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
907 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
910 c
= &cpuid_data
.entries
[cpuid_i
++];
914 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
917 /* Call Centaur's CPUID instructions they are supported. */
918 if (env
->cpuid_xlevel2
> 0) {
919 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
921 for (i
= 0xC0000000; i
<= limit
; i
++) {
922 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
923 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
926 c
= &cpuid_data
.entries
[cpuid_i
++];
930 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
934 cpuid_data
.cpuid
.nent
= cpuid_i
;
936 if (((env
->cpuid_version
>> 8)&0xF) >= 6
937 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
938 (CPUID_MCE
| CPUID_MCA
)
939 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
940 uint64_t mcg_cap
, unsupported_caps
;
944 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
946 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
950 if (banks
< (env
->mcg_cap
& MCG_CAP_BANKS_MASK
)) {
951 error_report("kvm: Unsupported MCE bank count (QEMU = %d, KVM = %d)",
952 (int)(env
->mcg_cap
& MCG_CAP_BANKS_MASK
), banks
);
956 unsupported_caps
= env
->mcg_cap
& ~(mcg_cap
| MCG_CAP_BANKS_MASK
);
957 if (unsupported_caps
) {
958 if (unsupported_caps
& MCG_LMCE_P
) {
959 error_report("kvm: LMCE not supported");
962 warn_report("Unsupported MCG_CAP bits: 0x%" PRIx64
,
966 env
->mcg_cap
&= mcg_cap
| MCG_CAP_BANKS_MASK
;
967 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &env
->mcg_cap
);
969 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
974 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
976 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 1, 0);
978 has_msr_feature_control
= !!(c
->ecx
& CPUID_EXT_VMX
) ||
979 !!(c
->ecx
& CPUID_EXT_SMX
);
982 if (env
->mcg_cap
& MCG_LMCE_P
) {
983 has_msr_mcg_ext_ctl
= has_msr_feature_control
= true;
986 if (!env
->user_tsc_khz
) {
987 if ((env
->features
[FEAT_8000_0007_EDX
] & CPUID_APM_INVTSC
) &&
988 invtsc_mig_blocker
== NULL
) {
990 error_setg(&invtsc_mig_blocker
,
991 "State blocked by non-migratable CPU device"
993 r
= migrate_add_blocker(invtsc_mig_blocker
, &local_err
);
995 error_report_err(local_err
);
996 error_free(invtsc_mig_blocker
);
1000 vmstate_x86_cpu
.unmigratable
= 1;
1004 if (cpu
->vmware_cpuid_freq
1005 /* Guests depend on 0x40000000 to detect this feature, so only expose
1006 * it if KVM exposes leaf 0x40000000. (Conflicts with Hyper-V) */
1008 && kvm_base
== KVM_CPUID_SIGNATURE
1009 /* TSC clock must be stable and known for this feature. */
1010 && tsc_is_stable_and_known(env
)) {
1012 c
= &cpuid_data
.entries
[cpuid_i
++];
1013 c
->function
= KVM_CPUID_SIGNATURE
| 0x10;
1014 c
->eax
= env
->tsc_khz
;
1015 /* LAPIC resolution of 1ns (freq: 1GHz) is hardcoded in KVM's
1016 * APIC_BUS_CYCLE_NS */
1018 c
->ecx
= c
->edx
= 0;
1020 c
= cpuid_find_entry(&cpuid_data
.cpuid
, kvm_base
, 0);
1021 c
->eax
= MAX(c
->eax
, KVM_CPUID_SIGNATURE
| 0x10);
1024 cpuid_data
.cpuid
.nent
= cpuid_i
;
1026 cpuid_data
.cpuid
.padding
= 0;
1027 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
1033 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
1035 cpu
->kvm_msr_buf
= g_malloc0(MSR_BUF_SIZE
);
1037 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_RDTSCP
)) {
1038 has_msr_tsc_aux
= false;
1044 migrate_del_blocker(invtsc_mig_blocker
);
1048 void kvm_arch_reset_vcpu(X86CPU
*cpu
)
1050 CPUX86State
*env
= &cpu
->env
;
1053 if (kvm_irqchip_in_kernel()) {
1054 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
1055 KVM_MP_STATE_UNINITIALIZED
;
1057 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1060 if (cpu
->hyperv_synic
) {
1062 for (i
= 0; i
< ARRAY_SIZE(env
->msr_hv_synic_sint
); i
++) {
1063 env
->msr_hv_synic_sint
[i
] = HV_SINT_MASKED
;
1068 void kvm_arch_do_init_vcpu(X86CPU
*cpu
)
1070 CPUX86State
*env
= &cpu
->env
;
1072 /* APs get directly into wait-for-SIPI state. */
1073 if (env
->mp_state
== KVM_MP_STATE_UNINITIALIZED
) {
1074 env
->mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
1078 static int kvm_get_supported_msrs(KVMState
*s
)
1080 static int kvm_supported_msrs
;
1084 if (kvm_supported_msrs
== 0) {
1085 struct kvm_msr_list msr_list
, *kvm_msr_list
;
1087 kvm_supported_msrs
= -1;
1089 /* Obtain MSR list from KVM. These are the MSRs that we must
1092 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
1093 if (ret
< 0 && ret
!= -E2BIG
) {
1096 /* Old kernel modules had a bug and could write beyond the provided
1097 memory. Allocate at least a safe amount of 1K. */
1098 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
1100 sizeof(msr_list
.indices
[0])));
1102 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
1103 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
1107 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
1108 switch (kvm_msr_list
->indices
[i
]) {
1110 has_msr_star
= true;
1112 case MSR_VM_HSAVE_PA
:
1113 has_msr_hsave_pa
= true;
1116 has_msr_tsc_aux
= true;
1118 case MSR_TSC_ADJUST
:
1119 has_msr_tsc_adjust
= true;
1121 case MSR_IA32_TSCDEADLINE
:
1122 has_msr_tsc_deadline
= true;
1124 case MSR_IA32_SMBASE
:
1125 has_msr_smbase
= true;
1127 case MSR_IA32_MISC_ENABLE
:
1128 has_msr_misc_enable
= true;
1130 case MSR_IA32_BNDCFGS
:
1131 has_msr_bndcfgs
= true;
1136 case HV_X64_MSR_CRASH_CTL
:
1137 has_msr_hv_crash
= true;
1139 case HV_X64_MSR_RESET
:
1140 has_msr_hv_reset
= true;
1142 case HV_X64_MSR_VP_INDEX
:
1143 has_msr_hv_vpindex
= true;
1145 case HV_X64_MSR_VP_RUNTIME
:
1146 has_msr_hv_runtime
= true;
1148 case HV_X64_MSR_SCONTROL
:
1149 has_msr_hv_synic
= true;
1151 case HV_X64_MSR_STIMER0_CONFIG
:
1152 has_msr_hv_stimer
= true;
1154 case HV_X64_MSR_TSC_FREQUENCY
:
1155 has_msr_hv_frequencies
= true;
1157 case MSR_IA32_SPEC_CTRL
:
1158 has_msr_spec_ctrl
= true;
1164 g_free(kvm_msr_list
);
1170 static Notifier smram_machine_done
;
1171 static KVMMemoryListener smram_listener
;
1172 static AddressSpace smram_address_space
;
1173 static MemoryRegion smram_as_root
;
1174 static MemoryRegion smram_as_mem
;
1176 static void register_smram_listener(Notifier
*n
, void *unused
)
1178 MemoryRegion
*smram
=
1179 (MemoryRegion
*) object_resolve_path("/machine/smram", NULL
);
1181 /* Outer container... */
1182 memory_region_init(&smram_as_root
, OBJECT(kvm_state
), "mem-container-smram", ~0ull);
1183 memory_region_set_enabled(&smram_as_root
, true);
1185 /* ... with two regions inside: normal system memory with low
1188 memory_region_init_alias(&smram_as_mem
, OBJECT(kvm_state
), "mem-smram",
1189 get_system_memory(), 0, ~0ull);
1190 memory_region_add_subregion_overlap(&smram_as_root
, 0, &smram_as_mem
, 0);
1191 memory_region_set_enabled(&smram_as_mem
, true);
1194 /* ... SMRAM with higher priority */
1195 memory_region_add_subregion_overlap(&smram_as_root
, 0, smram
, 10);
1196 memory_region_set_enabled(smram
, true);
1199 address_space_init(&smram_address_space
, &smram_as_root
, "KVM-SMRAM");
1200 kvm_memory_listener_register(kvm_state
, &smram_listener
,
1201 &smram_address_space
, 1);
1204 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
1206 uint64_t identity_base
= 0xfffbc000;
1207 uint64_t shadow_mem
;
1209 struct utsname utsname
;
1211 #ifdef KVM_CAP_XSAVE
1212 has_xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
1216 has_xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
1219 #ifdef KVM_CAP_PIT_STATE2
1220 has_pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
1223 ret
= kvm_get_supported_msrs(s
);
1229 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
1232 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
1233 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
1234 * Since these must be part of guest physical memory, we need to allocate
1235 * them, both by setting their start addresses in the kernel and by
1236 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
1238 * Older KVM versions may not support setting the identity map base. In
1239 * that case we need to stick with the default, i.e. a 256K maximum BIOS
1242 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
1243 /* Allows up to 16M BIOSes. */
1244 identity_base
= 0xfeffc000;
1246 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
1252 /* Set TSS base one page after EPT identity map. */
1253 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
1258 /* Tell fw_cfg to notify the BIOS to reserve the range. */
1259 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
1261 fprintf(stderr
, "e820_add_entry() table is full\n");
1264 qemu_register_reset(kvm_unpoison_all
, NULL
);
1266 shadow_mem
= machine_kvm_shadow_mem(ms
);
1267 if (shadow_mem
!= -1) {
1269 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
1275 if (kvm_check_extension(s
, KVM_CAP_X86_SMM
) &&
1276 object_dynamic_cast(OBJECT(ms
), TYPE_PC_MACHINE
) &&
1277 pc_machine_is_smm_enabled(PC_MACHINE(ms
))) {
1278 smram_machine_done
.notify
= register_smram_listener
;
1279 qemu_add_machine_init_done_notifier(&smram_machine_done
);
1284 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
1286 lhs
->selector
= rhs
->selector
;
1287 lhs
->base
= rhs
->base
;
1288 lhs
->limit
= rhs
->limit
;
1300 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
1302 unsigned flags
= rhs
->flags
;
1303 lhs
->selector
= rhs
->selector
;
1304 lhs
->base
= rhs
->base
;
1305 lhs
->limit
= rhs
->limit
;
1306 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
1307 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
1308 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
1309 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
1310 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
1311 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
1312 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
1313 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
1314 lhs
->unusable
= !lhs
->present
;
1318 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
1320 lhs
->selector
= rhs
->selector
;
1321 lhs
->base
= rhs
->base
;
1322 lhs
->limit
= rhs
->limit
;
1323 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
1324 ((rhs
->present
&& !rhs
->unusable
) * DESC_P_MASK
) |
1325 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
1326 (rhs
->db
<< DESC_B_SHIFT
) |
1327 (rhs
->s
* DESC_S_MASK
) |
1328 (rhs
->l
<< DESC_L_SHIFT
) |
1329 (rhs
->g
* DESC_G_MASK
) |
1330 (rhs
->avl
* DESC_AVL_MASK
);
1333 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
1336 *kvm_reg
= *qemu_reg
;
1338 *qemu_reg
= *kvm_reg
;
1342 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
1344 CPUX86State
*env
= &cpu
->env
;
1345 struct kvm_regs regs
;
1349 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
1355 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
1356 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
1357 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
1358 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
1359 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
1360 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
1361 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
1362 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
1363 #ifdef TARGET_X86_64
1364 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
1365 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
1366 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
1367 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
1368 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
1369 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
1370 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
1371 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
1374 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
1375 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
1378 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
1384 static int kvm_put_fpu(X86CPU
*cpu
)
1386 CPUX86State
*env
= &cpu
->env
;
1390 memset(&fpu
, 0, sizeof fpu
);
1391 fpu
.fsw
= env
->fpus
& ~(7 << 11);
1392 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
1393 fpu
.fcw
= env
->fpuc
;
1394 fpu
.last_opcode
= env
->fpop
;
1395 fpu
.last_ip
= env
->fpip
;
1396 fpu
.last_dp
= env
->fpdp
;
1397 for (i
= 0; i
< 8; ++i
) {
1398 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
1400 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
1401 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
1402 stq_p(&fpu
.xmm
[i
][0], env
->xmm_regs
[i
].ZMM_Q(0));
1403 stq_p(&fpu
.xmm
[i
][8], env
->xmm_regs
[i
].ZMM_Q(1));
1405 fpu
.mxcsr
= env
->mxcsr
;
1407 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
1410 #define XSAVE_FCW_FSW 0
1411 #define XSAVE_FTW_FOP 1
1412 #define XSAVE_CWD_RIP 2
1413 #define XSAVE_CWD_RDP 4
1414 #define XSAVE_MXCSR 6
1415 #define XSAVE_ST_SPACE 8
1416 #define XSAVE_XMM_SPACE 40
1417 #define XSAVE_XSTATE_BV 128
1418 #define XSAVE_YMMH_SPACE 144
1419 #define XSAVE_BNDREGS 240
1420 #define XSAVE_BNDCSR 256
1421 #define XSAVE_OPMASK 272
1422 #define XSAVE_ZMM_Hi256 288
1423 #define XSAVE_Hi16_ZMM 416
1424 #define XSAVE_PKRU 672
1426 #define XSAVE_BYTE_OFFSET(word_offset) \
1427 ((word_offset) * sizeof(((struct kvm_xsave *)0)->region[0]))
1429 #define ASSERT_OFFSET(word_offset, field) \
1430 QEMU_BUILD_BUG_ON(XSAVE_BYTE_OFFSET(word_offset) != \
1431 offsetof(X86XSaveArea, field))
1433 ASSERT_OFFSET(XSAVE_FCW_FSW
, legacy
.fcw
);
1434 ASSERT_OFFSET(XSAVE_FTW_FOP
, legacy
.ftw
);
1435 ASSERT_OFFSET(XSAVE_CWD_RIP
, legacy
.fpip
);
1436 ASSERT_OFFSET(XSAVE_CWD_RDP
, legacy
.fpdp
);
1437 ASSERT_OFFSET(XSAVE_MXCSR
, legacy
.mxcsr
);
1438 ASSERT_OFFSET(XSAVE_ST_SPACE
, legacy
.fpregs
);
1439 ASSERT_OFFSET(XSAVE_XMM_SPACE
, legacy
.xmm_regs
);
1440 ASSERT_OFFSET(XSAVE_XSTATE_BV
, header
.xstate_bv
);
1441 ASSERT_OFFSET(XSAVE_YMMH_SPACE
, avx_state
);
1442 ASSERT_OFFSET(XSAVE_BNDREGS
, bndreg_state
);
1443 ASSERT_OFFSET(XSAVE_BNDCSR
, bndcsr_state
);
1444 ASSERT_OFFSET(XSAVE_OPMASK
, opmask_state
);
1445 ASSERT_OFFSET(XSAVE_ZMM_Hi256
, zmm_hi256_state
);
1446 ASSERT_OFFSET(XSAVE_Hi16_ZMM
, hi16_zmm_state
);
1447 ASSERT_OFFSET(XSAVE_PKRU
, pkru_state
);
1449 static int kvm_put_xsave(X86CPU
*cpu
)
1451 CPUX86State
*env
= &cpu
->env
;
1452 X86XSaveArea
*xsave
= env
->kvm_xsave_buf
;
1455 return kvm_put_fpu(cpu
);
1457 x86_cpu_xsave_all_areas(cpu
, xsave
);
1459 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
1462 static int kvm_put_xcrs(X86CPU
*cpu
)
1464 CPUX86State
*env
= &cpu
->env
;
1465 struct kvm_xcrs xcrs
= {};
1473 xcrs
.xcrs
[0].xcr
= 0;
1474 xcrs
.xcrs
[0].value
= env
->xcr0
;
1475 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
1478 static int kvm_put_sregs(X86CPU
*cpu
)
1480 CPUX86State
*env
= &cpu
->env
;
1481 struct kvm_sregs sregs
;
1483 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
1484 if (env
->interrupt_injected
>= 0) {
1485 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
1486 (uint64_t)1 << (env
->interrupt_injected
% 64);
1489 if ((env
->eflags
& VM_MASK
)) {
1490 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1491 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1492 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1493 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1494 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1495 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1497 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1498 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1499 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1500 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1501 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1502 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1505 set_seg(&sregs
.tr
, &env
->tr
);
1506 set_seg(&sregs
.ldt
, &env
->ldt
);
1508 sregs
.idt
.limit
= env
->idt
.limit
;
1509 sregs
.idt
.base
= env
->idt
.base
;
1510 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1511 sregs
.gdt
.limit
= env
->gdt
.limit
;
1512 sregs
.gdt
.base
= env
->gdt
.base
;
1513 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1515 sregs
.cr0
= env
->cr
[0];
1516 sregs
.cr2
= env
->cr
[2];
1517 sregs
.cr3
= env
->cr
[3];
1518 sregs
.cr4
= env
->cr
[4];
1520 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
1521 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
1523 sregs
.efer
= env
->efer
;
1525 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1528 static void kvm_msr_buf_reset(X86CPU
*cpu
)
1530 memset(cpu
->kvm_msr_buf
, 0, MSR_BUF_SIZE
);
1533 static void kvm_msr_entry_add(X86CPU
*cpu
, uint32_t index
, uint64_t value
)
1535 struct kvm_msrs
*msrs
= cpu
->kvm_msr_buf
;
1536 void *limit
= ((void *)msrs
) + MSR_BUF_SIZE
;
1537 struct kvm_msr_entry
*entry
= &msrs
->entries
[msrs
->nmsrs
];
1539 assert((void *)(entry
+ 1) <= limit
);
1541 entry
->index
= index
;
1542 entry
->reserved
= 0;
1543 entry
->data
= value
;
1547 static int kvm_put_one_msr(X86CPU
*cpu
, int index
, uint64_t value
)
1549 kvm_msr_buf_reset(cpu
);
1550 kvm_msr_entry_add(cpu
, index
, value
);
1552 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, cpu
->kvm_msr_buf
);
1555 void kvm_put_apicbase(X86CPU
*cpu
, uint64_t value
)
1559 ret
= kvm_put_one_msr(cpu
, MSR_IA32_APICBASE
, value
);
1563 static int kvm_put_tscdeadline_msr(X86CPU
*cpu
)
1565 CPUX86State
*env
= &cpu
->env
;
1568 if (!has_msr_tsc_deadline
) {
1572 ret
= kvm_put_one_msr(cpu
, MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1582 * Provide a separate write service for the feature control MSR in order to
1583 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1584 * before writing any other state because forcibly leaving nested mode
1585 * invalidates the VCPU state.
1587 static int kvm_put_msr_feature_control(X86CPU
*cpu
)
1591 if (!has_msr_feature_control
) {
1595 ret
= kvm_put_one_msr(cpu
, MSR_IA32_FEATURE_CONTROL
,
1596 cpu
->env
.msr_ia32_feature_control
);
1605 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1607 CPUX86State
*env
= &cpu
->env
;
1611 kvm_msr_buf_reset(cpu
);
1613 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1614 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1615 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1616 kvm_msr_entry_add(cpu
, MSR_PAT
, env
->pat
);
1618 kvm_msr_entry_add(cpu
, MSR_STAR
, env
->star
);
1620 if (has_msr_hsave_pa
) {
1621 kvm_msr_entry_add(cpu
, MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1623 if (has_msr_tsc_aux
) {
1624 kvm_msr_entry_add(cpu
, MSR_TSC_AUX
, env
->tsc_aux
);
1626 if (has_msr_tsc_adjust
) {
1627 kvm_msr_entry_add(cpu
, MSR_TSC_ADJUST
, env
->tsc_adjust
);
1629 if (has_msr_misc_enable
) {
1630 kvm_msr_entry_add(cpu
, MSR_IA32_MISC_ENABLE
,
1631 env
->msr_ia32_misc_enable
);
1633 if (has_msr_smbase
) {
1634 kvm_msr_entry_add(cpu
, MSR_IA32_SMBASE
, env
->smbase
);
1636 if (has_msr_bndcfgs
) {
1637 kvm_msr_entry_add(cpu
, MSR_IA32_BNDCFGS
, env
->msr_bndcfgs
);
1640 kvm_msr_entry_add(cpu
, MSR_IA32_XSS
, env
->xss
);
1642 if (has_msr_spec_ctrl
) {
1643 kvm_msr_entry_add(cpu
, MSR_IA32_SPEC_CTRL
, env
->spec_ctrl
);
1645 #ifdef TARGET_X86_64
1646 if (lm_capable_kernel
) {
1647 kvm_msr_entry_add(cpu
, MSR_CSTAR
, env
->cstar
);
1648 kvm_msr_entry_add(cpu
, MSR_KERNELGSBASE
, env
->kernelgsbase
);
1649 kvm_msr_entry_add(cpu
, MSR_FMASK
, env
->fmask
);
1650 kvm_msr_entry_add(cpu
, MSR_LSTAR
, env
->lstar
);
1655 * The following MSRs have side effects on the guest or are too heavy
1656 * for normal writeback. Limit them to reset or full state updates.
1658 if (level
>= KVM_PUT_RESET_STATE
) {
1659 kvm_msr_entry_add(cpu
, MSR_IA32_TSC
, env
->tsc
);
1660 kvm_msr_entry_add(cpu
, MSR_KVM_SYSTEM_TIME
, env
->system_time_msr
);
1661 kvm_msr_entry_add(cpu
, MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1662 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF
)) {
1663 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_EN
, env
->async_pf_en_msr
);
1665 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_PV_EOI
)) {
1666 kvm_msr_entry_add(cpu
, MSR_KVM_PV_EOI_EN
, env
->pv_eoi_en_msr
);
1668 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_STEAL_TIME
)) {
1669 kvm_msr_entry_add(cpu
, MSR_KVM_STEAL_TIME
, env
->steal_time_msr
);
1671 if (has_architectural_pmu_version
> 0) {
1672 if (has_architectural_pmu_version
> 1) {
1673 /* Stop the counter. */
1674 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
1675 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
, 0);
1678 /* Set the counter values. */
1679 for (i
= 0; i
< num_architectural_pmu_fixed_counters
; i
++) {
1680 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR0
+ i
,
1681 env
->msr_fixed_counters
[i
]);
1683 for (i
= 0; i
< num_architectural_pmu_gp_counters
; i
++) {
1684 kvm_msr_entry_add(cpu
, MSR_P6_PERFCTR0
+ i
,
1685 env
->msr_gp_counters
[i
]);
1686 kvm_msr_entry_add(cpu
, MSR_P6_EVNTSEL0
+ i
,
1687 env
->msr_gp_evtsel
[i
]);
1689 if (has_architectural_pmu_version
> 1) {
1690 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_STATUS
,
1691 env
->msr_global_status
);
1692 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_OVF_CTRL
,
1693 env
->msr_global_ovf_ctrl
);
1695 /* Now start the PMU. */
1696 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
,
1697 env
->msr_fixed_ctr_ctrl
);
1698 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
,
1699 env
->msr_global_ctrl
);
1703 * Hyper-V partition-wide MSRs: to avoid clearing them on cpu hot-add,
1704 * only sync them to KVM on the first cpu
1706 if (current_cpu
== first_cpu
) {
1707 if (has_msr_hv_hypercall
) {
1708 kvm_msr_entry_add(cpu
, HV_X64_MSR_GUEST_OS_ID
,
1709 env
->msr_hv_guest_os_id
);
1710 kvm_msr_entry_add(cpu
, HV_X64_MSR_HYPERCALL
,
1711 env
->msr_hv_hypercall
);
1713 if (cpu
->hyperv_time
) {
1714 kvm_msr_entry_add(cpu
, HV_X64_MSR_REFERENCE_TSC
,
1718 if (cpu
->hyperv_vapic
) {
1719 kvm_msr_entry_add(cpu
, HV_X64_MSR_APIC_ASSIST_PAGE
,
1722 if (has_msr_hv_crash
) {
1725 for (j
= 0; j
< HV_CRASH_PARAMS
; j
++)
1726 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_P0
+ j
,
1727 env
->msr_hv_crash_params
[j
]);
1729 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_CTL
, HV_CRASH_CTL_NOTIFY
);
1731 if (has_msr_hv_runtime
) {
1732 kvm_msr_entry_add(cpu
, HV_X64_MSR_VP_RUNTIME
, env
->msr_hv_runtime
);
1734 if (cpu
->hyperv_synic
) {
1737 kvm_msr_entry_add(cpu
, HV_X64_MSR_SVERSION
, HV_SYNIC_VERSION
);
1739 kvm_msr_entry_add(cpu
, HV_X64_MSR_SCONTROL
,
1740 env
->msr_hv_synic_control
);
1741 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIEFP
,
1742 env
->msr_hv_synic_evt_page
);
1743 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIMP
,
1744 env
->msr_hv_synic_msg_page
);
1746 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_synic_sint
); j
++) {
1747 kvm_msr_entry_add(cpu
, HV_X64_MSR_SINT0
+ j
,
1748 env
->msr_hv_synic_sint
[j
]);
1751 if (has_msr_hv_stimer
) {
1754 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_stimer_config
); j
++) {
1755 kvm_msr_entry_add(cpu
, HV_X64_MSR_STIMER0_CONFIG
+ j
* 2,
1756 env
->msr_hv_stimer_config
[j
]);
1759 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_stimer_count
); j
++) {
1760 kvm_msr_entry_add(cpu
, HV_X64_MSR_STIMER0_COUNT
+ j
* 2,
1761 env
->msr_hv_stimer_count
[j
]);
1764 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
1765 uint64_t phys_mask
= MAKE_64BIT_MASK(0, cpu
->phys_bits
);
1767 kvm_msr_entry_add(cpu
, MSR_MTRRdefType
, env
->mtrr_deftype
);
1768 kvm_msr_entry_add(cpu
, MSR_MTRRfix64K_00000
, env
->mtrr_fixed
[0]);
1769 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_80000
, env
->mtrr_fixed
[1]);
1770 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_A0000
, env
->mtrr_fixed
[2]);
1771 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C0000
, env
->mtrr_fixed
[3]);
1772 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C8000
, env
->mtrr_fixed
[4]);
1773 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D0000
, env
->mtrr_fixed
[5]);
1774 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D8000
, env
->mtrr_fixed
[6]);
1775 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E0000
, env
->mtrr_fixed
[7]);
1776 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E8000
, env
->mtrr_fixed
[8]);
1777 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F0000
, env
->mtrr_fixed
[9]);
1778 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F8000
, env
->mtrr_fixed
[10]);
1779 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
1780 /* The CPU GPs if we write to a bit above the physical limit of
1781 * the host CPU (and KVM emulates that)
1783 uint64_t mask
= env
->mtrr_var
[i
].mask
;
1786 kvm_msr_entry_add(cpu
, MSR_MTRRphysBase(i
),
1787 env
->mtrr_var
[i
].base
);
1788 kvm_msr_entry_add(cpu
, MSR_MTRRphysMask(i
), mask
);
1792 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1793 * kvm_put_msr_feature_control. */
1798 kvm_msr_entry_add(cpu
, MSR_MCG_STATUS
, env
->mcg_status
);
1799 kvm_msr_entry_add(cpu
, MSR_MCG_CTL
, env
->mcg_ctl
);
1800 if (has_msr_mcg_ext_ctl
) {
1801 kvm_msr_entry_add(cpu
, MSR_MCG_EXT_CTL
, env
->mcg_ext_ctl
);
1803 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1804 kvm_msr_entry_add(cpu
, MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1808 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, cpu
->kvm_msr_buf
);
1813 if (ret
< cpu
->kvm_msr_buf
->nmsrs
) {
1814 struct kvm_msr_entry
*e
= &cpu
->kvm_msr_buf
->entries
[ret
];
1815 error_report("error: failed to set MSR 0x%" PRIx32
" to 0x%" PRIx64
,
1816 (uint32_t)e
->index
, (uint64_t)e
->data
);
1819 assert(ret
== cpu
->kvm_msr_buf
->nmsrs
);
1824 static int kvm_get_fpu(X86CPU
*cpu
)
1826 CPUX86State
*env
= &cpu
->env
;
1830 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1835 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1836 env
->fpus
= fpu
.fsw
;
1837 env
->fpuc
= fpu
.fcw
;
1838 env
->fpop
= fpu
.last_opcode
;
1839 env
->fpip
= fpu
.last_ip
;
1840 env
->fpdp
= fpu
.last_dp
;
1841 for (i
= 0; i
< 8; ++i
) {
1842 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1844 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1845 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
1846 env
->xmm_regs
[i
].ZMM_Q(0) = ldq_p(&fpu
.xmm
[i
][0]);
1847 env
->xmm_regs
[i
].ZMM_Q(1) = ldq_p(&fpu
.xmm
[i
][8]);
1849 env
->mxcsr
= fpu
.mxcsr
;
1854 static int kvm_get_xsave(X86CPU
*cpu
)
1856 CPUX86State
*env
= &cpu
->env
;
1857 X86XSaveArea
*xsave
= env
->kvm_xsave_buf
;
1861 return kvm_get_fpu(cpu
);
1864 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1868 x86_cpu_xrstor_all_areas(cpu
, xsave
);
1873 static int kvm_get_xcrs(X86CPU
*cpu
)
1875 CPUX86State
*env
= &cpu
->env
;
1877 struct kvm_xcrs xcrs
;
1883 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1888 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1889 /* Only support xcr0 now */
1890 if (xcrs
.xcrs
[i
].xcr
== 0) {
1891 env
->xcr0
= xcrs
.xcrs
[i
].value
;
1898 static int kvm_get_sregs(X86CPU
*cpu
)
1900 CPUX86State
*env
= &cpu
->env
;
1901 struct kvm_sregs sregs
;
1904 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1909 /* There can only be one pending IRQ set in the bitmap at a time, so try
1910 to find it and save its number instead (-1 for none). */
1911 env
->interrupt_injected
= -1;
1912 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1913 if (sregs
.interrupt_bitmap
[i
]) {
1914 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1915 env
->interrupt_injected
= i
* 64 + bit
;
1920 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1921 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1922 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1923 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1924 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1925 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1927 get_seg(&env
->tr
, &sregs
.tr
);
1928 get_seg(&env
->ldt
, &sregs
.ldt
);
1930 env
->idt
.limit
= sregs
.idt
.limit
;
1931 env
->idt
.base
= sregs
.idt
.base
;
1932 env
->gdt
.limit
= sregs
.gdt
.limit
;
1933 env
->gdt
.base
= sregs
.gdt
.base
;
1935 env
->cr
[0] = sregs
.cr0
;
1936 env
->cr
[2] = sregs
.cr2
;
1937 env
->cr
[3] = sregs
.cr3
;
1938 env
->cr
[4] = sregs
.cr4
;
1940 env
->efer
= sregs
.efer
;
1942 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1943 x86_update_hflags(env
);
1948 static int kvm_get_msrs(X86CPU
*cpu
)
1950 CPUX86State
*env
= &cpu
->env
;
1951 struct kvm_msr_entry
*msrs
= cpu
->kvm_msr_buf
->entries
;
1953 uint64_t mtrr_top_bits
;
1955 kvm_msr_buf_reset(cpu
);
1957 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_CS
, 0);
1958 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_ESP
, 0);
1959 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_EIP
, 0);
1960 kvm_msr_entry_add(cpu
, MSR_PAT
, 0);
1962 kvm_msr_entry_add(cpu
, MSR_STAR
, 0);
1964 if (has_msr_hsave_pa
) {
1965 kvm_msr_entry_add(cpu
, MSR_VM_HSAVE_PA
, 0);
1967 if (has_msr_tsc_aux
) {
1968 kvm_msr_entry_add(cpu
, MSR_TSC_AUX
, 0);
1970 if (has_msr_tsc_adjust
) {
1971 kvm_msr_entry_add(cpu
, MSR_TSC_ADJUST
, 0);
1973 if (has_msr_tsc_deadline
) {
1974 kvm_msr_entry_add(cpu
, MSR_IA32_TSCDEADLINE
, 0);
1976 if (has_msr_misc_enable
) {
1977 kvm_msr_entry_add(cpu
, MSR_IA32_MISC_ENABLE
, 0);
1979 if (has_msr_smbase
) {
1980 kvm_msr_entry_add(cpu
, MSR_IA32_SMBASE
, 0);
1982 if (has_msr_feature_control
) {
1983 kvm_msr_entry_add(cpu
, MSR_IA32_FEATURE_CONTROL
, 0);
1985 if (has_msr_bndcfgs
) {
1986 kvm_msr_entry_add(cpu
, MSR_IA32_BNDCFGS
, 0);
1989 kvm_msr_entry_add(cpu
, MSR_IA32_XSS
, 0);
1991 if (has_msr_spec_ctrl
) {
1992 kvm_msr_entry_add(cpu
, MSR_IA32_SPEC_CTRL
, 0);
1996 if (!env
->tsc_valid
) {
1997 kvm_msr_entry_add(cpu
, MSR_IA32_TSC
, 0);
1998 env
->tsc_valid
= !runstate_is_running();
2001 #ifdef TARGET_X86_64
2002 if (lm_capable_kernel
) {
2003 kvm_msr_entry_add(cpu
, MSR_CSTAR
, 0);
2004 kvm_msr_entry_add(cpu
, MSR_KERNELGSBASE
, 0);
2005 kvm_msr_entry_add(cpu
, MSR_FMASK
, 0);
2006 kvm_msr_entry_add(cpu
, MSR_LSTAR
, 0);
2009 kvm_msr_entry_add(cpu
, MSR_KVM_SYSTEM_TIME
, 0);
2010 kvm_msr_entry_add(cpu
, MSR_KVM_WALL_CLOCK
, 0);
2011 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF
)) {
2012 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_EN
, 0);
2014 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_PV_EOI
)) {
2015 kvm_msr_entry_add(cpu
, MSR_KVM_PV_EOI_EN
, 0);
2017 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_STEAL_TIME
)) {
2018 kvm_msr_entry_add(cpu
, MSR_KVM_STEAL_TIME
, 0);
2020 if (has_architectural_pmu_version
> 0) {
2021 if (has_architectural_pmu_version
> 1) {
2022 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
2023 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
, 0);
2024 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_STATUS
, 0);
2025 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_OVF_CTRL
, 0);
2027 for (i
= 0; i
< num_architectural_pmu_fixed_counters
; i
++) {
2028 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR0
+ i
, 0);
2030 for (i
= 0; i
< num_architectural_pmu_gp_counters
; i
++) {
2031 kvm_msr_entry_add(cpu
, MSR_P6_PERFCTR0
+ i
, 0);
2032 kvm_msr_entry_add(cpu
, MSR_P6_EVNTSEL0
+ i
, 0);
2037 kvm_msr_entry_add(cpu
, MSR_MCG_STATUS
, 0);
2038 kvm_msr_entry_add(cpu
, MSR_MCG_CTL
, 0);
2039 if (has_msr_mcg_ext_ctl
) {
2040 kvm_msr_entry_add(cpu
, MSR_MCG_EXT_CTL
, 0);
2042 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
2043 kvm_msr_entry_add(cpu
, MSR_MC0_CTL
+ i
, 0);
2047 if (has_msr_hv_hypercall
) {
2048 kvm_msr_entry_add(cpu
, HV_X64_MSR_HYPERCALL
, 0);
2049 kvm_msr_entry_add(cpu
, HV_X64_MSR_GUEST_OS_ID
, 0);
2051 if (cpu
->hyperv_vapic
) {
2052 kvm_msr_entry_add(cpu
, HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
2054 if (cpu
->hyperv_time
) {
2055 kvm_msr_entry_add(cpu
, HV_X64_MSR_REFERENCE_TSC
, 0);
2057 if (has_msr_hv_crash
) {
2060 for (j
= 0; j
< HV_CRASH_PARAMS
; j
++) {
2061 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_P0
+ j
, 0);
2064 if (has_msr_hv_runtime
) {
2065 kvm_msr_entry_add(cpu
, HV_X64_MSR_VP_RUNTIME
, 0);
2067 if (cpu
->hyperv_synic
) {
2070 kvm_msr_entry_add(cpu
, HV_X64_MSR_SCONTROL
, 0);
2071 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIEFP
, 0);
2072 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIMP
, 0);
2073 for (msr
= HV_X64_MSR_SINT0
; msr
<= HV_X64_MSR_SINT15
; msr
++) {
2074 kvm_msr_entry_add(cpu
, msr
, 0);
2077 if (has_msr_hv_stimer
) {
2080 for (msr
= HV_X64_MSR_STIMER0_CONFIG
; msr
<= HV_X64_MSR_STIMER3_COUNT
;
2082 kvm_msr_entry_add(cpu
, msr
, 0);
2085 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
2086 kvm_msr_entry_add(cpu
, MSR_MTRRdefType
, 0);
2087 kvm_msr_entry_add(cpu
, MSR_MTRRfix64K_00000
, 0);
2088 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_80000
, 0);
2089 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_A0000
, 0);
2090 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C0000
, 0);
2091 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C8000
, 0);
2092 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D0000
, 0);
2093 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D8000
, 0);
2094 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E0000
, 0);
2095 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E8000
, 0);
2096 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F0000
, 0);
2097 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F8000
, 0);
2098 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
2099 kvm_msr_entry_add(cpu
, MSR_MTRRphysBase(i
), 0);
2100 kvm_msr_entry_add(cpu
, MSR_MTRRphysMask(i
), 0);
2104 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, cpu
->kvm_msr_buf
);
2109 if (ret
< cpu
->kvm_msr_buf
->nmsrs
) {
2110 struct kvm_msr_entry
*e
= &cpu
->kvm_msr_buf
->entries
[ret
];
2111 error_report("error: failed to get MSR 0x%" PRIx32
,
2112 (uint32_t)e
->index
);
2115 assert(ret
== cpu
->kvm_msr_buf
->nmsrs
);
2117 * MTRR masks: Each mask consists of 5 parts
2118 * a 10..0: must be zero
2120 * c n-1.12: actual mask bits
2121 * d 51..n: reserved must be zero
2122 * e 63.52: reserved must be zero
2124 * 'n' is the number of physical bits supported by the CPU and is
2125 * apparently always <= 52. We know our 'n' but don't know what
2126 * the destinations 'n' is; it might be smaller, in which case
2127 * it masks (c) on loading. It might be larger, in which case
2128 * we fill 'd' so that d..c is consistent irrespetive of the 'n'
2129 * we're migrating to.
2132 if (cpu
->fill_mtrr_mask
) {
2133 QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS
> 52);
2134 assert(cpu
->phys_bits
<= TARGET_PHYS_ADDR_SPACE_BITS
);
2135 mtrr_top_bits
= MAKE_64BIT_MASK(cpu
->phys_bits
, 52 - cpu
->phys_bits
);
2140 for (i
= 0; i
< ret
; i
++) {
2141 uint32_t index
= msrs
[i
].index
;
2143 case MSR_IA32_SYSENTER_CS
:
2144 env
->sysenter_cs
= msrs
[i
].data
;
2146 case MSR_IA32_SYSENTER_ESP
:
2147 env
->sysenter_esp
= msrs
[i
].data
;
2149 case MSR_IA32_SYSENTER_EIP
:
2150 env
->sysenter_eip
= msrs
[i
].data
;
2153 env
->pat
= msrs
[i
].data
;
2156 env
->star
= msrs
[i
].data
;
2158 #ifdef TARGET_X86_64
2160 env
->cstar
= msrs
[i
].data
;
2162 case MSR_KERNELGSBASE
:
2163 env
->kernelgsbase
= msrs
[i
].data
;
2166 env
->fmask
= msrs
[i
].data
;
2169 env
->lstar
= msrs
[i
].data
;
2173 env
->tsc
= msrs
[i
].data
;
2176 env
->tsc_aux
= msrs
[i
].data
;
2178 case MSR_TSC_ADJUST
:
2179 env
->tsc_adjust
= msrs
[i
].data
;
2181 case MSR_IA32_TSCDEADLINE
:
2182 env
->tsc_deadline
= msrs
[i
].data
;
2184 case MSR_VM_HSAVE_PA
:
2185 env
->vm_hsave
= msrs
[i
].data
;
2187 case MSR_KVM_SYSTEM_TIME
:
2188 env
->system_time_msr
= msrs
[i
].data
;
2190 case MSR_KVM_WALL_CLOCK
:
2191 env
->wall_clock_msr
= msrs
[i
].data
;
2193 case MSR_MCG_STATUS
:
2194 env
->mcg_status
= msrs
[i
].data
;
2197 env
->mcg_ctl
= msrs
[i
].data
;
2199 case MSR_MCG_EXT_CTL
:
2200 env
->mcg_ext_ctl
= msrs
[i
].data
;
2202 case MSR_IA32_MISC_ENABLE
:
2203 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
2205 case MSR_IA32_SMBASE
:
2206 env
->smbase
= msrs
[i
].data
;
2208 case MSR_IA32_FEATURE_CONTROL
:
2209 env
->msr_ia32_feature_control
= msrs
[i
].data
;
2211 case MSR_IA32_BNDCFGS
:
2212 env
->msr_bndcfgs
= msrs
[i
].data
;
2215 env
->xss
= msrs
[i
].data
;
2218 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
2219 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
2220 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
2223 case MSR_KVM_ASYNC_PF_EN
:
2224 env
->async_pf_en_msr
= msrs
[i
].data
;
2226 case MSR_KVM_PV_EOI_EN
:
2227 env
->pv_eoi_en_msr
= msrs
[i
].data
;
2229 case MSR_KVM_STEAL_TIME
:
2230 env
->steal_time_msr
= msrs
[i
].data
;
2232 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
2233 env
->msr_fixed_ctr_ctrl
= msrs
[i
].data
;
2235 case MSR_CORE_PERF_GLOBAL_CTRL
:
2236 env
->msr_global_ctrl
= msrs
[i
].data
;
2238 case MSR_CORE_PERF_GLOBAL_STATUS
:
2239 env
->msr_global_status
= msrs
[i
].data
;
2241 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
2242 env
->msr_global_ovf_ctrl
= msrs
[i
].data
;
2244 case MSR_CORE_PERF_FIXED_CTR0
... MSR_CORE_PERF_FIXED_CTR0
+ MAX_FIXED_COUNTERS
- 1:
2245 env
->msr_fixed_counters
[index
- MSR_CORE_PERF_FIXED_CTR0
] = msrs
[i
].data
;
2247 case MSR_P6_PERFCTR0
... MSR_P6_PERFCTR0
+ MAX_GP_COUNTERS
- 1:
2248 env
->msr_gp_counters
[index
- MSR_P6_PERFCTR0
] = msrs
[i
].data
;
2250 case MSR_P6_EVNTSEL0
... MSR_P6_EVNTSEL0
+ MAX_GP_COUNTERS
- 1:
2251 env
->msr_gp_evtsel
[index
- MSR_P6_EVNTSEL0
] = msrs
[i
].data
;
2253 case HV_X64_MSR_HYPERCALL
:
2254 env
->msr_hv_hypercall
= msrs
[i
].data
;
2256 case HV_X64_MSR_GUEST_OS_ID
:
2257 env
->msr_hv_guest_os_id
= msrs
[i
].data
;
2259 case HV_X64_MSR_APIC_ASSIST_PAGE
:
2260 env
->msr_hv_vapic
= msrs
[i
].data
;
2262 case HV_X64_MSR_REFERENCE_TSC
:
2263 env
->msr_hv_tsc
= msrs
[i
].data
;
2265 case HV_X64_MSR_CRASH_P0
... HV_X64_MSR_CRASH_P4
:
2266 env
->msr_hv_crash_params
[index
- HV_X64_MSR_CRASH_P0
] = msrs
[i
].data
;
2268 case HV_X64_MSR_VP_RUNTIME
:
2269 env
->msr_hv_runtime
= msrs
[i
].data
;
2271 case HV_X64_MSR_SCONTROL
:
2272 env
->msr_hv_synic_control
= msrs
[i
].data
;
2274 case HV_X64_MSR_SIEFP
:
2275 env
->msr_hv_synic_evt_page
= msrs
[i
].data
;
2277 case HV_X64_MSR_SIMP
:
2278 env
->msr_hv_synic_msg_page
= msrs
[i
].data
;
2280 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
2281 env
->msr_hv_synic_sint
[index
- HV_X64_MSR_SINT0
] = msrs
[i
].data
;
2283 case HV_X64_MSR_STIMER0_CONFIG
:
2284 case HV_X64_MSR_STIMER1_CONFIG
:
2285 case HV_X64_MSR_STIMER2_CONFIG
:
2286 case HV_X64_MSR_STIMER3_CONFIG
:
2287 env
->msr_hv_stimer_config
[(index
- HV_X64_MSR_STIMER0_CONFIG
)/2] =
2290 case HV_X64_MSR_STIMER0_COUNT
:
2291 case HV_X64_MSR_STIMER1_COUNT
:
2292 case HV_X64_MSR_STIMER2_COUNT
:
2293 case HV_X64_MSR_STIMER3_COUNT
:
2294 env
->msr_hv_stimer_count
[(index
- HV_X64_MSR_STIMER0_COUNT
)/2] =
2297 case MSR_MTRRdefType
:
2298 env
->mtrr_deftype
= msrs
[i
].data
;
2300 case MSR_MTRRfix64K_00000
:
2301 env
->mtrr_fixed
[0] = msrs
[i
].data
;
2303 case MSR_MTRRfix16K_80000
:
2304 env
->mtrr_fixed
[1] = msrs
[i
].data
;
2306 case MSR_MTRRfix16K_A0000
:
2307 env
->mtrr_fixed
[2] = msrs
[i
].data
;
2309 case MSR_MTRRfix4K_C0000
:
2310 env
->mtrr_fixed
[3] = msrs
[i
].data
;
2312 case MSR_MTRRfix4K_C8000
:
2313 env
->mtrr_fixed
[4] = msrs
[i
].data
;
2315 case MSR_MTRRfix4K_D0000
:
2316 env
->mtrr_fixed
[5] = msrs
[i
].data
;
2318 case MSR_MTRRfix4K_D8000
:
2319 env
->mtrr_fixed
[6] = msrs
[i
].data
;
2321 case MSR_MTRRfix4K_E0000
:
2322 env
->mtrr_fixed
[7] = msrs
[i
].data
;
2324 case MSR_MTRRfix4K_E8000
:
2325 env
->mtrr_fixed
[8] = msrs
[i
].data
;
2327 case MSR_MTRRfix4K_F0000
:
2328 env
->mtrr_fixed
[9] = msrs
[i
].data
;
2330 case MSR_MTRRfix4K_F8000
:
2331 env
->mtrr_fixed
[10] = msrs
[i
].data
;
2333 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT
- 1):
2335 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].mask
= msrs
[i
].data
|
2338 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].base
= msrs
[i
].data
;
2341 case MSR_IA32_SPEC_CTRL
:
2342 env
->spec_ctrl
= msrs
[i
].data
;
2350 static int kvm_put_mp_state(X86CPU
*cpu
)
2352 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
2354 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
2357 static int kvm_get_mp_state(X86CPU
*cpu
)
2359 CPUState
*cs
= CPU(cpu
);
2360 CPUX86State
*env
= &cpu
->env
;
2361 struct kvm_mp_state mp_state
;
2364 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
2368 env
->mp_state
= mp_state
.mp_state
;
2369 if (kvm_irqchip_in_kernel()) {
2370 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
2375 static int kvm_get_apic(X86CPU
*cpu
)
2377 DeviceState
*apic
= cpu
->apic_state
;
2378 struct kvm_lapic_state kapic
;
2381 if (apic
&& kvm_irqchip_in_kernel()) {
2382 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
2387 kvm_get_apic_state(apic
, &kapic
);
2392 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
2394 CPUState
*cs
= CPU(cpu
);
2395 CPUX86State
*env
= &cpu
->env
;
2396 struct kvm_vcpu_events events
= {};
2398 if (!kvm_has_vcpu_events()) {
2402 events
.exception
.injected
= (env
->exception_injected
>= 0);
2403 events
.exception
.nr
= env
->exception_injected
;
2404 events
.exception
.has_error_code
= env
->has_error_code
;
2405 events
.exception
.error_code
= env
->error_code
;
2406 events
.exception
.pad
= 0;
2408 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
2409 events
.interrupt
.nr
= env
->interrupt_injected
;
2410 events
.interrupt
.soft
= env
->soft_interrupt
;
2412 events
.nmi
.injected
= env
->nmi_injected
;
2413 events
.nmi
.pending
= env
->nmi_pending
;
2414 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
2417 events
.sipi_vector
= env
->sipi_vector
;
2420 if (has_msr_smbase
) {
2421 events
.smi
.smm
= !!(env
->hflags
& HF_SMM_MASK
);
2422 events
.smi
.smm_inside_nmi
= !!(env
->hflags2
& HF2_SMM_INSIDE_NMI_MASK
);
2423 if (kvm_irqchip_in_kernel()) {
2424 /* As soon as these are moved to the kernel, remove them
2425 * from cs->interrupt_request.
2427 events
.smi
.pending
= cs
->interrupt_request
& CPU_INTERRUPT_SMI
;
2428 events
.smi
.latched_init
= cs
->interrupt_request
& CPU_INTERRUPT_INIT
;
2429 cs
->interrupt_request
&= ~(CPU_INTERRUPT_INIT
| CPU_INTERRUPT_SMI
);
2431 /* Keep these in cs->interrupt_request. */
2432 events
.smi
.pending
= 0;
2433 events
.smi
.latched_init
= 0;
2435 /* Stop SMI delivery on old machine types to avoid a reboot
2436 * on an inward migration of an old VM.
2438 if (!cpu
->kvm_no_smi_migration
) {
2439 events
.flags
|= KVM_VCPUEVENT_VALID_SMM
;
2443 if (level
>= KVM_PUT_RESET_STATE
) {
2444 events
.flags
|= KVM_VCPUEVENT_VALID_NMI_PENDING
;
2445 if (env
->mp_state
== KVM_MP_STATE_SIPI_RECEIVED
) {
2446 events
.flags
|= KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
2450 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
2453 static int kvm_get_vcpu_events(X86CPU
*cpu
)
2455 CPUX86State
*env
= &cpu
->env
;
2456 struct kvm_vcpu_events events
;
2459 if (!kvm_has_vcpu_events()) {
2463 memset(&events
, 0, sizeof(events
));
2464 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
2468 env
->exception_injected
=
2469 events
.exception
.injected
? events
.exception
.nr
: -1;
2470 env
->has_error_code
= events
.exception
.has_error_code
;
2471 env
->error_code
= events
.exception
.error_code
;
2473 env
->interrupt_injected
=
2474 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
2475 env
->soft_interrupt
= events
.interrupt
.soft
;
2477 env
->nmi_injected
= events
.nmi
.injected
;
2478 env
->nmi_pending
= events
.nmi
.pending
;
2479 if (events
.nmi
.masked
) {
2480 env
->hflags2
|= HF2_NMI_MASK
;
2482 env
->hflags2
&= ~HF2_NMI_MASK
;
2485 if (events
.flags
& KVM_VCPUEVENT_VALID_SMM
) {
2486 if (events
.smi
.smm
) {
2487 env
->hflags
|= HF_SMM_MASK
;
2489 env
->hflags
&= ~HF_SMM_MASK
;
2491 if (events
.smi
.pending
) {
2492 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_SMI
);
2494 cpu_reset_interrupt(CPU(cpu
), CPU_INTERRUPT_SMI
);
2496 if (events
.smi
.smm_inside_nmi
) {
2497 env
->hflags2
|= HF2_SMM_INSIDE_NMI_MASK
;
2499 env
->hflags2
&= ~HF2_SMM_INSIDE_NMI_MASK
;
2501 if (events
.smi
.latched_init
) {
2502 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_INIT
);
2504 cpu_reset_interrupt(CPU(cpu
), CPU_INTERRUPT_INIT
);
2508 env
->sipi_vector
= events
.sipi_vector
;
2513 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
2515 CPUState
*cs
= CPU(cpu
);
2516 CPUX86State
*env
= &cpu
->env
;
2518 unsigned long reinject_trap
= 0;
2520 if (!kvm_has_vcpu_events()) {
2521 if (env
->exception_injected
== 1) {
2522 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
2523 } else if (env
->exception_injected
== 3) {
2524 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
2526 env
->exception_injected
= -1;
2530 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
2531 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
2532 * by updating the debug state once again if single-stepping is on.
2533 * Another reason to call kvm_update_guest_debug here is a pending debug
2534 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
2535 * reinject them via SET_GUEST_DEBUG.
2537 if (reinject_trap
||
2538 (!kvm_has_robust_singlestep() && cs
->singlestep_enabled
)) {
2539 ret
= kvm_update_guest_debug(cs
, reinject_trap
);
2544 static int kvm_put_debugregs(X86CPU
*cpu
)
2546 CPUX86State
*env
= &cpu
->env
;
2547 struct kvm_debugregs dbgregs
;
2550 if (!kvm_has_debugregs()) {
2554 for (i
= 0; i
< 4; i
++) {
2555 dbgregs
.db
[i
] = env
->dr
[i
];
2557 dbgregs
.dr6
= env
->dr
[6];
2558 dbgregs
.dr7
= env
->dr
[7];
2561 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
2564 static int kvm_get_debugregs(X86CPU
*cpu
)
2566 CPUX86State
*env
= &cpu
->env
;
2567 struct kvm_debugregs dbgregs
;
2570 if (!kvm_has_debugregs()) {
2574 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
2578 for (i
= 0; i
< 4; i
++) {
2579 env
->dr
[i
] = dbgregs
.db
[i
];
2581 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
2582 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
2587 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
2589 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2592 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
2594 if (level
>= KVM_PUT_RESET_STATE
) {
2595 ret
= kvm_put_msr_feature_control(x86_cpu
);
2601 if (level
== KVM_PUT_FULL_STATE
) {
2602 /* We don't check for kvm_arch_set_tsc_khz() errors here,
2603 * because TSC frequency mismatch shouldn't abort migration,
2604 * unless the user explicitly asked for a more strict TSC
2605 * setting (e.g. using an explicit "tsc-freq" option).
2607 kvm_arch_set_tsc_khz(cpu
);
2610 ret
= kvm_getput_regs(x86_cpu
, 1);
2614 ret
= kvm_put_xsave(x86_cpu
);
2618 ret
= kvm_put_xcrs(x86_cpu
);
2622 ret
= kvm_put_sregs(x86_cpu
);
2626 /* must be before kvm_put_msrs */
2627 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
2631 ret
= kvm_put_msrs(x86_cpu
, level
);
2635 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
2639 if (level
>= KVM_PUT_RESET_STATE
) {
2640 ret
= kvm_put_mp_state(x86_cpu
);
2646 ret
= kvm_put_tscdeadline_msr(x86_cpu
);
2650 ret
= kvm_put_debugregs(x86_cpu
);
2655 ret
= kvm_guest_debug_workarounds(x86_cpu
);
2662 int kvm_arch_get_registers(CPUState
*cs
)
2664 X86CPU
*cpu
= X86_CPU(cs
);
2667 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
2669 ret
= kvm_get_vcpu_events(cpu
);
2674 * KVM_GET_MPSTATE can modify CS and RIP, call it before
2675 * KVM_GET_REGS and KVM_GET_SREGS.
2677 ret
= kvm_get_mp_state(cpu
);
2681 ret
= kvm_getput_regs(cpu
, 0);
2685 ret
= kvm_get_xsave(cpu
);
2689 ret
= kvm_get_xcrs(cpu
);
2693 ret
= kvm_get_sregs(cpu
);
2697 ret
= kvm_get_msrs(cpu
);
2701 ret
= kvm_get_apic(cpu
);
2705 ret
= kvm_get_debugregs(cpu
);
2711 cpu_sync_bndcs_hflags(&cpu
->env
);
2715 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
2717 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2718 CPUX86State
*env
= &x86_cpu
->env
;
2722 if (cpu
->interrupt_request
& (CPU_INTERRUPT_NMI
| CPU_INTERRUPT_SMI
)) {
2723 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
2724 qemu_mutex_lock_iothread();
2725 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
2726 qemu_mutex_unlock_iothread();
2727 DPRINTF("injected NMI\n");
2728 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
2730 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
2734 if (cpu
->interrupt_request
& CPU_INTERRUPT_SMI
) {
2735 qemu_mutex_lock_iothread();
2736 cpu
->interrupt_request
&= ~CPU_INTERRUPT_SMI
;
2737 qemu_mutex_unlock_iothread();
2738 DPRINTF("injected SMI\n");
2739 ret
= kvm_vcpu_ioctl(cpu
, KVM_SMI
);
2741 fprintf(stderr
, "KVM: injection failed, SMI lost (%s)\n",
2747 if (!kvm_pic_in_kernel()) {
2748 qemu_mutex_lock_iothread();
2751 /* Force the VCPU out of its inner loop to process any INIT requests
2752 * or (for userspace APIC, but it is cheap to combine the checks here)
2753 * pending TPR access reports.
2755 if (cpu
->interrupt_request
& (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
2756 if ((cpu
->interrupt_request
& CPU_INTERRUPT_INIT
) &&
2757 !(env
->hflags
& HF_SMM_MASK
)) {
2758 cpu
->exit_request
= 1;
2760 if (cpu
->interrupt_request
& CPU_INTERRUPT_TPR
) {
2761 cpu
->exit_request
= 1;
2765 if (!kvm_pic_in_kernel()) {
2766 /* Try to inject an interrupt if the guest can accept it */
2767 if (run
->ready_for_interrupt_injection
&&
2768 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2769 (env
->eflags
& IF_MASK
)) {
2772 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
2773 irq
= cpu_get_pic_interrupt(env
);
2775 struct kvm_interrupt intr
;
2778 DPRINTF("injected interrupt %d\n", irq
);
2779 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
2782 "KVM: injection failed, interrupt lost (%s)\n",
2788 /* If we have an interrupt but the guest is not ready to receive an
2789 * interrupt, request an interrupt window exit. This will
2790 * cause a return to userspace as soon as the guest is ready to
2791 * receive interrupts. */
2792 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
2793 run
->request_interrupt_window
= 1;
2795 run
->request_interrupt_window
= 0;
2798 DPRINTF("setting tpr\n");
2799 run
->cr8
= cpu_get_apic_tpr(x86_cpu
->apic_state
);
2801 qemu_mutex_unlock_iothread();
2805 MemTxAttrs
kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
2807 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2808 CPUX86State
*env
= &x86_cpu
->env
;
2810 if (run
->flags
& KVM_RUN_X86_SMM
) {
2811 env
->hflags
|= HF_SMM_MASK
;
2813 env
->hflags
&= ~HF_SMM_MASK
;
2816 env
->eflags
|= IF_MASK
;
2818 env
->eflags
&= ~IF_MASK
;
2821 /* We need to protect the apic state against concurrent accesses from
2822 * different threads in case the userspace irqchip is used. */
2823 if (!kvm_irqchip_in_kernel()) {
2824 qemu_mutex_lock_iothread();
2826 cpu_set_apic_tpr(x86_cpu
->apic_state
, run
->cr8
);
2827 cpu_set_apic_base(x86_cpu
->apic_state
, run
->apic_base
);
2828 if (!kvm_irqchip_in_kernel()) {
2829 qemu_mutex_unlock_iothread();
2831 return cpu_get_mem_attrs(env
);
2834 int kvm_arch_process_async_events(CPUState
*cs
)
2836 X86CPU
*cpu
= X86_CPU(cs
);
2837 CPUX86State
*env
= &cpu
->env
;
2839 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
2840 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2841 assert(env
->mcg_cap
);
2843 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
2845 kvm_cpu_synchronize_state(cs
);
2847 if (env
->exception_injected
== EXCP08_DBLE
) {
2848 /* this means triple fault */
2849 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
2850 cs
->exit_request
= 1;
2853 env
->exception_injected
= EXCP12_MCHK
;
2854 env
->has_error_code
= 0;
2857 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
2858 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
2862 if ((cs
->interrupt_request
& CPU_INTERRUPT_INIT
) &&
2863 !(env
->hflags
& HF_SMM_MASK
)) {
2864 kvm_cpu_synchronize_state(cs
);
2868 if (kvm_irqchip_in_kernel()) {
2872 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
2873 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
2874 apic_poll_irq(cpu
->apic_state
);
2876 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2877 (env
->eflags
& IF_MASK
)) ||
2878 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2881 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
2882 kvm_cpu_synchronize_state(cs
);
2885 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
2886 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
2887 kvm_cpu_synchronize_state(cs
);
2888 apic_handle_tpr_access_report(cpu
->apic_state
, env
->eip
,
2889 env
->tpr_access_type
);
2895 static int kvm_handle_halt(X86CPU
*cpu
)
2897 CPUState
*cs
= CPU(cpu
);
2898 CPUX86State
*env
= &cpu
->env
;
2900 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2901 (env
->eflags
& IF_MASK
)) &&
2902 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2910 static int kvm_handle_tpr_access(X86CPU
*cpu
)
2912 CPUState
*cs
= CPU(cpu
);
2913 struct kvm_run
*run
= cs
->kvm_run
;
2915 apic_handle_tpr_access_report(cpu
->apic_state
, run
->tpr_access
.rip
,
2916 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
2921 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2923 static const uint8_t int3
= 0xcc;
2925 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
2926 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
2932 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2936 if (cpu_memory_rw_debug(cs
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
2937 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
2949 static int nb_hw_breakpoint
;
2951 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
2955 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2956 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
2957 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
2964 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
2965 target_ulong len
, int type
)
2968 case GDB_BREAKPOINT_HW
:
2971 case GDB_WATCHPOINT_WRITE
:
2972 case GDB_WATCHPOINT_ACCESS
:
2979 if (addr
& (len
- 1)) {
2991 if (nb_hw_breakpoint
== 4) {
2994 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2997 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2998 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2999 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
3005 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
3006 target_ulong len
, int type
)
3010 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
3015 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
3020 void kvm_arch_remove_all_hw_breakpoints(void)
3022 nb_hw_breakpoint
= 0;
3025 static CPUWatchpoint hw_watchpoint
;
3027 static int kvm_handle_debug(X86CPU
*cpu
,
3028 struct kvm_debug_exit_arch
*arch_info
)
3030 CPUState
*cs
= CPU(cpu
);
3031 CPUX86State
*env
= &cpu
->env
;
3035 if (arch_info
->exception
== 1) {
3036 if (arch_info
->dr6
& (1 << 14)) {
3037 if (cs
->singlestep_enabled
) {
3041 for (n
= 0; n
< 4; n
++) {
3042 if (arch_info
->dr6
& (1 << n
)) {
3043 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
3049 cs
->watchpoint_hit
= &hw_watchpoint
;
3050 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
3051 hw_watchpoint
.flags
= BP_MEM_WRITE
;
3055 cs
->watchpoint_hit
= &hw_watchpoint
;
3056 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
3057 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
3063 } else if (kvm_find_sw_breakpoint(cs
, arch_info
->pc
)) {
3067 cpu_synchronize_state(cs
);
3068 assert(env
->exception_injected
== -1);
3071 env
->exception_injected
= arch_info
->exception
;
3072 env
->has_error_code
= 0;
3078 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
3080 const uint8_t type_code
[] = {
3081 [GDB_BREAKPOINT_HW
] = 0x0,
3082 [GDB_WATCHPOINT_WRITE
] = 0x1,
3083 [GDB_WATCHPOINT_ACCESS
] = 0x3
3085 const uint8_t len_code
[] = {
3086 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
3090 if (kvm_sw_breakpoints_active(cpu
)) {
3091 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
3093 if (nb_hw_breakpoint
> 0) {
3094 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
3095 dbg
->arch
.debugreg
[7] = 0x0600;
3096 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
3097 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
3098 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
3099 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
3100 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
3105 static bool host_supports_vmx(void)
3107 uint32_t ecx
, unused
;
3109 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
3110 return ecx
& CPUID_EXT_VMX
;
3113 #define VMX_INVALID_GUEST_STATE 0x80000021
3115 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
3117 X86CPU
*cpu
= X86_CPU(cs
);
3121 switch (run
->exit_reason
) {
3123 DPRINTF("handle_hlt\n");
3124 qemu_mutex_lock_iothread();
3125 ret
= kvm_handle_halt(cpu
);
3126 qemu_mutex_unlock_iothread();
3128 case KVM_EXIT_SET_TPR
:
3131 case KVM_EXIT_TPR_ACCESS
:
3132 qemu_mutex_lock_iothread();
3133 ret
= kvm_handle_tpr_access(cpu
);
3134 qemu_mutex_unlock_iothread();
3136 case KVM_EXIT_FAIL_ENTRY
:
3137 code
= run
->fail_entry
.hardware_entry_failure_reason
;
3138 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
3140 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
3142 "\nIf you're running a guest on an Intel machine without "
3143 "unrestricted mode\n"
3144 "support, the failure can be most likely due to the guest "
3145 "entering an invalid\n"
3146 "state for Intel VT. For example, the guest maybe running "
3147 "in big real mode\n"
3148 "which is not supported on less recent Intel processors."
3153 case KVM_EXIT_EXCEPTION
:
3154 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
3155 run
->ex
.exception
, run
->ex
.error_code
);
3158 case KVM_EXIT_DEBUG
:
3159 DPRINTF("kvm_exit_debug\n");
3160 qemu_mutex_lock_iothread();
3161 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
3162 qemu_mutex_unlock_iothread();
3164 case KVM_EXIT_HYPERV
:
3165 ret
= kvm_hv_handle_exit(cpu
, &run
->hyperv
);
3167 case KVM_EXIT_IOAPIC_EOI
:
3168 ioapic_eoi_broadcast(run
->eoi
.vector
);
3172 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
3180 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
3182 X86CPU
*cpu
= X86_CPU(cs
);
3183 CPUX86State
*env
= &cpu
->env
;
3185 kvm_cpu_synchronize_state(cs
);
3186 return !(env
->cr
[0] & CR0_PE_MASK
) ||
3187 ((env
->segs
[R_CS
].selector
& 3) != 3);
3190 void kvm_arch_init_irq_routing(KVMState
*s
)
3192 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
3193 /* If kernel can't do irq routing, interrupt source
3194 * override 0->2 cannot be set up as required by HPET.
3195 * So we have to disable it.
3199 /* We know at this point that we're using the in-kernel
3200 * irqchip, so we can use irqfds, and on x86 we know
3201 * we can use msi via irqfd and GSI routing.
3203 kvm_msi_via_irqfd_allowed
= true;
3204 kvm_gsi_routing_allowed
= true;
3206 if (kvm_irqchip_is_split()) {
3209 /* If the ioapic is in QEMU and the lapics are in KVM, reserve
3210 MSI routes for signaling interrupts to the local apics. */
3211 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
3212 if (kvm_irqchip_add_msi_route(s
, 0, NULL
) < 0) {
3213 error_report("Could not enable split IRQ mode.");
3220 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
3223 if (machine_kernel_irqchip_split(ms
)) {
3224 ret
= kvm_vm_enable_cap(s
, KVM_CAP_SPLIT_IRQCHIP
, 0, 24);
3226 error_report("Could not enable split irqchip mode: %s",
3230 DPRINTF("Enabled KVM_CAP_SPLIT_IRQCHIP\n");
3231 kvm_split_irqchip
= true;
3239 /* Classic KVM device assignment interface. Will remain x86 only. */
3240 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
3241 uint32_t flags
, uint32_t *dev_id
)
3243 struct kvm_assigned_pci_dev dev_data
= {
3244 .segnr
= dev_addr
->domain
,
3245 .busnr
= dev_addr
->bus
,
3246 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
3251 dev_data
.assigned_dev_id
=
3252 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
3254 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
3259 *dev_id
= dev_data
.assigned_dev_id
;
3264 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
3266 struct kvm_assigned_pci_dev dev_data
= {
3267 .assigned_dev_id
= dev_id
,
3270 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
3273 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
3274 uint32_t irq_type
, uint32_t guest_irq
)
3276 struct kvm_assigned_irq assigned_irq
= {
3277 .assigned_dev_id
= dev_id
,
3278 .guest_irq
= guest_irq
,
3282 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
3283 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
3285 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
3289 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
3292 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
3293 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
3295 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
3298 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
3300 struct kvm_assigned_pci_dev dev_data
= {
3301 .assigned_dev_id
= dev_id
,
3302 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
3305 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
3308 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
3311 struct kvm_assigned_irq assigned_irq
= {
3312 .assigned_dev_id
= dev_id
,
3316 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
3319 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
3321 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
3322 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
3325 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
3327 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
3328 KVM_DEV_IRQ_GUEST_MSI
, virq
);
3331 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
3333 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
3334 KVM_DEV_IRQ_HOST_MSI
);
3337 bool kvm_device_msix_supported(KVMState
*s
)
3339 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
3340 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
3341 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
3344 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
3345 uint32_t nr_vectors
)
3347 struct kvm_assigned_msix_nr msix_nr
= {
3348 .assigned_dev_id
= dev_id
,
3349 .entry_nr
= nr_vectors
,
3352 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
3355 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
3358 struct kvm_assigned_msix_entry msix_entry
= {
3359 .assigned_dev_id
= dev_id
,
3364 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
3367 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
3369 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
3370 KVM_DEV_IRQ_GUEST_MSIX
, 0);
3373 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
3375 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
3376 KVM_DEV_IRQ_HOST_MSIX
);
3379 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
3380 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
3382 X86IOMMUState
*iommu
= x86_iommu_get_default();
3386 MSIMessage src
, dst
;
3387 X86IOMMUClass
*class = X86_IOMMU_GET_CLASS(iommu
);
3389 src
.address
= route
->u
.msi
.address_hi
;
3390 src
.address
<<= VTD_MSI_ADDR_HI_SHIFT
;
3391 src
.address
|= route
->u
.msi
.address_lo
;
3392 src
.data
= route
->u
.msi
.data
;
3394 ret
= class->int_remap(iommu
, &src
, &dst
, dev
? \
3395 pci_requester_id(dev
) : \
3396 X86_IOMMU_SID_INVALID
);
3398 trace_kvm_x86_fixup_msi_error(route
->gsi
);
3402 route
->u
.msi
.address_hi
= dst
.address
>> VTD_MSI_ADDR_HI_SHIFT
;
3403 route
->u
.msi
.address_lo
= dst
.address
& VTD_MSI_ADDR_LO_MASK
;
3404 route
->u
.msi
.data
= dst
.data
;
3410 typedef struct MSIRouteEntry MSIRouteEntry
;
3412 struct MSIRouteEntry
{
3413 PCIDevice
*dev
; /* Device pointer */
3414 int vector
; /* MSI/MSIX vector index */
3415 int virq
; /* Virtual IRQ index */
3416 QLIST_ENTRY(MSIRouteEntry
) list
;
3419 /* List of used GSI routes */
3420 static QLIST_HEAD(, MSIRouteEntry
) msi_route_list
= \
3421 QLIST_HEAD_INITIALIZER(msi_route_list
);
3423 static void kvm_update_msi_routes_all(void *private, bool global
,
3424 uint32_t index
, uint32_t mask
)
3427 MSIRouteEntry
*entry
;
3431 /* TODO: explicit route update */
3432 QLIST_FOREACH(entry
, &msi_route_list
, list
) {
3435 if (!msix_enabled(dev
) && !msi_enabled(dev
)) {
3438 msg
= pci_get_msi_message(dev
, entry
->vector
);
3439 kvm_irqchip_update_msi_route(kvm_state
, entry
->virq
, msg
, dev
);
3441 kvm_irqchip_commit_routes(kvm_state
);
3442 trace_kvm_x86_update_msi_routes(cnt
);
3445 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
3446 int vector
, PCIDevice
*dev
)
3448 static bool notify_list_inited
= false;
3449 MSIRouteEntry
*entry
;
3452 /* These are (possibly) IOAPIC routes only used for split
3453 * kernel irqchip mode, while what we are housekeeping are
3454 * PCI devices only. */
3458 entry
= g_new0(MSIRouteEntry
, 1);
3460 entry
->vector
= vector
;
3461 entry
->virq
= route
->gsi
;
3462 QLIST_INSERT_HEAD(&msi_route_list
, entry
, list
);
3464 trace_kvm_x86_add_msi_route(route
->gsi
);
3466 if (!notify_list_inited
) {
3467 /* For the first time we do add route, add ourselves into
3468 * IOMMU's IEC notify list if needed. */
3469 X86IOMMUState
*iommu
= x86_iommu_get_default();
3471 x86_iommu_iec_register_notifier(iommu
,
3472 kvm_update_msi_routes_all
,
3475 notify_list_inited
= true;
3480 int kvm_arch_release_virq_post(int virq
)
3482 MSIRouteEntry
*entry
, *next
;
3483 QLIST_FOREACH_SAFE(entry
, &msi_route_list
, list
, next
) {
3484 if (entry
->virq
== virq
) {
3485 trace_kvm_x86_remove_msi_route(virq
);
3486 QLIST_REMOVE(entry
, list
);
3494 int kvm_arch_msi_data_to_gsi(uint32_t data
)