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/qapi-events-run-state.h"
17 #include "qapi/error.h"
18 #include "qapi/visitor.h"
19 #include <sys/ioctl.h>
20 #include <sys/utsname.h>
21 #include <sys/syscall.h>
23 #include <linux/kvm.h>
24 #include "standard-headers/asm-x86/kvm_para.h"
25 #include "hw/xen/interface/arch-x86/cpuid.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/hw_accel.h"
31 #include "sysemu/kvm_int.h"
32 #include "sysemu/runstate.h"
37 #include "hyperv-proto.h"
39 #include "exec/gdbstub.h"
40 #include "qemu/host-utils.h"
41 #include "qemu/main-loop.h"
42 #include "qemu/ratelimit.h"
43 #include "qemu/config-file.h"
44 #include "qemu/error-report.h"
45 #include "qemu/memalign.h"
46 #include "hw/i386/x86.h"
47 #include "hw/i386/kvm/xen_evtchn.h"
48 #include "hw/i386/pc.h"
49 #include "hw/i386/apic.h"
50 #include "hw/i386/apic_internal.h"
51 #include "hw/i386/apic-msidef.h"
52 #include "hw/i386/intel_iommu.h"
53 #include "hw/i386/x86-iommu.h"
54 #include "hw/i386/e820_memory_layout.h"
56 #include "hw/xen/xen.h"
58 #include "hw/pci/pci.h"
59 #include "hw/pci/msi.h"
60 #include "hw/pci/msix.h"
61 #include "migration/blocker.h"
62 #include "exec/memattrs.h"
65 #include CONFIG_DEVICES
70 #define DPRINTF(fmt, ...) \
71 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
73 #define DPRINTF(fmt, ...) \
77 /* From arch/x86/kvm/lapic.h */
78 #define KVM_APIC_BUS_CYCLE_NS 1
79 #define KVM_APIC_BUS_FREQUENCY (1000000000ULL / KVM_APIC_BUS_CYCLE_NS)
81 #define MSR_KVM_WALL_CLOCK 0x11
82 #define MSR_KVM_SYSTEM_TIME 0x12
84 /* A 4096-byte buffer can hold the 8-byte kvm_msrs header, plus
85 * 255 kvm_msr_entry structs */
86 #define MSR_BUF_SIZE 4096
88 static void kvm_init_msrs(X86CPU
*cpu
);
90 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
91 KVM_CAP_INFO(SET_TSS_ADDR
),
92 KVM_CAP_INFO(EXT_CPUID
),
93 KVM_CAP_INFO(MP_STATE
),
97 static bool has_msr_star
;
98 static bool has_msr_hsave_pa
;
99 static bool has_msr_tsc_aux
;
100 static bool has_msr_tsc_adjust
;
101 static bool has_msr_tsc_deadline
;
102 static bool has_msr_feature_control
;
103 static bool has_msr_misc_enable
;
104 static bool has_msr_smbase
;
105 static bool has_msr_bndcfgs
;
106 static int lm_capable_kernel
;
107 static bool has_msr_hv_hypercall
;
108 static bool has_msr_hv_crash
;
109 static bool has_msr_hv_reset
;
110 static bool has_msr_hv_vpindex
;
111 static bool hv_vpindex_settable
;
112 static bool has_msr_hv_runtime
;
113 static bool has_msr_hv_synic
;
114 static bool has_msr_hv_stimer
;
115 static bool has_msr_hv_frequencies
;
116 static bool has_msr_hv_reenlightenment
;
117 static bool has_msr_hv_syndbg_options
;
118 static bool has_msr_xss
;
119 static bool has_msr_umwait
;
120 static bool has_msr_spec_ctrl
;
121 static bool has_tsc_scale_msr
;
122 static bool has_msr_tsx_ctrl
;
123 static bool has_msr_virt_ssbd
;
124 static bool has_msr_smi_count
;
125 static bool has_msr_arch_capabs
;
126 static bool has_msr_core_capabs
;
127 static bool has_msr_vmx_vmfunc
;
128 static bool has_msr_ucode_rev
;
129 static bool has_msr_vmx_procbased_ctls2
;
130 static bool has_msr_perf_capabs
;
131 static bool has_msr_pkrs
;
133 static uint32_t has_architectural_pmu_version
;
134 static uint32_t num_architectural_pmu_gp_counters
;
135 static uint32_t num_architectural_pmu_fixed_counters
;
137 static int has_xsave
;
138 static int has_xsave2
;
140 static int has_pit_state2
;
141 static int has_sregs2
;
142 static int has_exception_payload
;
143 static int has_triple_fault_event
;
145 static bool has_msr_mcg_ext_ctl
;
147 static struct kvm_cpuid2
*cpuid_cache
;
148 static struct kvm_cpuid2
*hv_cpuid_cache
;
149 static struct kvm_msr_list
*kvm_feature_msrs
;
151 static KVMMSRHandlers msr_handlers
[KVM_MSR_FILTER_MAX_RANGES
];
153 #define BUS_LOCK_SLICE_TIME 1000000000ULL /* ns */
154 static RateLimit bus_lock_ratelimit_ctrl
;
155 static int kvm_get_one_msr(X86CPU
*cpu
, int index
, uint64_t *value
);
157 int kvm_has_pit_state2(void)
159 return has_pit_state2
;
162 bool kvm_has_smm(void)
164 return kvm_vm_check_extension(kvm_state
, KVM_CAP_X86_SMM
);
167 bool kvm_has_adjust_clock_stable(void)
169 int ret
= kvm_check_extension(kvm_state
, KVM_CAP_ADJUST_CLOCK
);
171 return (ret
& KVM_CLOCK_TSC_STABLE
);
174 bool kvm_has_adjust_clock(void)
176 return kvm_check_extension(kvm_state
, KVM_CAP_ADJUST_CLOCK
);
179 bool kvm_has_exception_payload(void)
181 return has_exception_payload
;
184 static bool kvm_x2apic_api_set_flags(uint64_t flags
)
186 KVMState
*s
= KVM_STATE(current_accel());
188 return !kvm_vm_enable_cap(s
, KVM_CAP_X2APIC_API
, 0, flags
);
191 #define MEMORIZE(fn, _result) \
193 static bool _memorized; \
202 static bool has_x2apic_api
;
204 bool kvm_has_x2apic_api(void)
206 return has_x2apic_api
;
209 bool kvm_enable_x2apic(void)
212 kvm_x2apic_api_set_flags(KVM_X2APIC_API_USE_32BIT_IDS
|
213 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK
),
217 bool kvm_hv_vpindex_settable(void)
219 return hv_vpindex_settable
;
222 static int kvm_get_tsc(CPUState
*cs
)
224 X86CPU
*cpu
= X86_CPU(cs
);
225 CPUX86State
*env
= &cpu
->env
;
229 if (env
->tsc_valid
) {
233 env
->tsc_valid
= !runstate_is_running();
235 ret
= kvm_get_one_msr(cpu
, MSR_IA32_TSC
, &value
);
244 static inline void do_kvm_synchronize_tsc(CPUState
*cpu
, run_on_cpu_data arg
)
249 void kvm_synchronize_all_tsc(void)
255 run_on_cpu(cpu
, do_kvm_synchronize_tsc
, RUN_ON_CPU_NULL
);
260 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
262 struct kvm_cpuid2
*cpuid
;
265 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
266 cpuid
= g_malloc0(size
);
268 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
269 if (r
== 0 && cpuid
->nent
>= max
) {
277 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
285 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
288 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
290 struct kvm_cpuid2
*cpuid
;
293 if (cpuid_cache
!= NULL
) {
296 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
303 static bool host_tsx_broken(void)
305 int family
, model
, stepping
;\
306 char vendor
[CPUID_VENDOR_SZ
+ 1];
308 host_cpu_vendor_fms(vendor
, &family
, &model
, &stepping
);
310 /* Check if we are running on a Haswell host known to have broken TSX */
311 return !strcmp(vendor
, CPUID_VENDOR_INTEL
) &&
313 ((model
== 63 && stepping
< 4) ||
314 model
== 60 || model
== 69 || model
== 70);
317 /* Returns the value for a specific register on the cpuid entry
319 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
339 /* Find matching entry for function/index on kvm_cpuid2 struct
341 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
346 for (i
= 0; i
< cpuid
->nent
; ++i
) {
347 if (cpuid
->entries
[i
].function
== function
&&
348 cpuid
->entries
[i
].index
== index
) {
349 return &cpuid
->entries
[i
];
356 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
357 uint32_t index
, int reg
)
359 struct kvm_cpuid2
*cpuid
;
361 uint32_t cpuid_1_edx
, unused
;
364 cpuid
= get_supported_cpuid(s
);
366 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
368 ret
= cpuid_entry_get_reg(entry
, reg
);
371 /* Fixups for the data returned by KVM, below */
373 if (function
== 1 && reg
== R_EDX
) {
374 /* KVM before 2.6.30 misreports the following features */
375 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
376 } else if (function
== 1 && reg
== R_ECX
) {
377 /* We can set the hypervisor flag, even if KVM does not return it on
378 * GET_SUPPORTED_CPUID
380 ret
|= CPUID_EXT_HYPERVISOR
;
381 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
382 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
383 * and the irqchip is in the kernel.
385 if (kvm_irqchip_in_kernel() &&
386 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
387 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
390 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
391 * without the in-kernel irqchip
393 if (!kvm_irqchip_in_kernel()) {
394 ret
&= ~CPUID_EXT_X2APIC
;
398 int disable_exits
= kvm_check_extension(s
,
399 KVM_CAP_X86_DISABLE_EXITS
);
401 if (disable_exits
& KVM_X86_DISABLE_EXITS_MWAIT
) {
402 ret
|= CPUID_EXT_MONITOR
;
405 } else if (function
== 6 && reg
== R_EAX
) {
406 ret
|= CPUID_6_EAX_ARAT
; /* safe to allow because of emulated APIC */
407 } else if (function
== 7 && index
== 0 && reg
== R_EBX
) {
408 /* Not new instructions, just an optimization. */
410 host_cpuid(7, 0, &unused
, &ebx
, &unused
, &unused
);
411 ret
|= ebx
& CPUID_7_0_EBX_ERMS
;
413 if (host_tsx_broken()) {
414 ret
&= ~(CPUID_7_0_EBX_RTM
| CPUID_7_0_EBX_HLE
);
416 } else if (function
== 7 && index
== 0 && reg
== R_EDX
) {
417 /* Not new instructions, just an optimization. */
419 host_cpuid(7, 0, &unused
, &unused
, &unused
, &edx
);
420 ret
|= edx
& CPUID_7_0_EDX_FSRM
;
423 * Linux v4.17-v4.20 incorrectly return ARCH_CAPABILITIES on SVM hosts.
424 * We can detect the bug by checking if MSR_IA32_ARCH_CAPABILITIES is
425 * returned by KVM_GET_MSR_INDEX_LIST.
427 if (!has_msr_arch_capabs
) {
428 ret
&= ~CPUID_7_0_EDX_ARCH_CAPABILITIES
;
430 } else if (function
== 7 && index
== 1 && reg
== R_EAX
) {
431 /* Not new instructions, just an optimization. */
433 host_cpuid(7, 1, &eax
, &unused
, &unused
, &unused
);
434 ret
|= eax
& (CPUID_7_1_EAX_FZRM
| CPUID_7_1_EAX_FSRS
| CPUID_7_1_EAX_FSRC
);
435 } else if (function
== 0xd && index
== 0 &&
436 (reg
== R_EAX
|| reg
== R_EDX
)) {
438 * The value returned by KVM_GET_SUPPORTED_CPUID does not include
439 * features that still have to be enabled with the arch_prctl
440 * system call. QEMU needs the full value, which is retrieved
441 * with KVM_GET_DEVICE_ATTR.
443 struct kvm_device_attr attr
= {
445 .attr
= KVM_X86_XCOMP_GUEST_SUPP
,
446 .addr
= (unsigned long) &bitmask
449 bool sys_attr
= kvm_check_extension(s
, KVM_CAP_SYS_ATTRIBUTES
);
454 int rc
= kvm_ioctl(s
, KVM_GET_DEVICE_ATTR
, &attr
);
457 warn_report("KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) "
462 ret
= (reg
== R_EAX
) ? bitmask
: bitmask
>> 32;
463 } else if (function
== 0x80000001 && reg
== R_ECX
) {
465 * It's safe to enable TOPOEXT even if it's not returned by
466 * GET_SUPPORTED_CPUID. Unconditionally enabling TOPOEXT here allows
467 * us to keep CPU models including TOPOEXT runnable on older kernels.
469 ret
|= CPUID_EXT3_TOPOEXT
;
470 } else if (function
== 0x80000001 && reg
== R_EDX
) {
471 /* On Intel, kvm returns cpuid according to the Intel spec,
472 * so add missing bits according to the AMD spec:
474 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
475 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
476 } else if (function
== KVM_CPUID_FEATURES
&& reg
== R_EAX
) {
477 /* kvm_pv_unhalt is reported by GET_SUPPORTED_CPUID, but it can't
478 * be enabled without the in-kernel irqchip
480 if (!kvm_irqchip_in_kernel()) {
481 ret
&= ~(1U << KVM_FEATURE_PV_UNHALT
);
483 if (kvm_irqchip_is_split()) {
484 ret
|= 1U << KVM_FEATURE_MSI_EXT_DEST_ID
;
486 } else if (function
== KVM_CPUID_FEATURES
&& reg
== R_EDX
) {
487 ret
|= 1U << KVM_HINTS_REALTIME
;
493 uint64_t kvm_arch_get_supported_msr_feature(KVMState
*s
, uint32_t index
)
496 struct kvm_msrs info
;
497 struct kvm_msr_entry entries
[1];
500 uint32_t ret
, can_be_one
, must_be_one
;
502 if (kvm_feature_msrs
== NULL
) { /* Host doesn't support feature MSRs */
506 /* Check if requested MSR is supported feature MSR */
508 for (i
= 0; i
< kvm_feature_msrs
->nmsrs
; i
++)
509 if (kvm_feature_msrs
->indices
[i
] == index
) {
512 if (i
== kvm_feature_msrs
->nmsrs
) {
513 return 0; /* if the feature MSR is not supported, simply return 0 */
516 msr_data
.info
.nmsrs
= 1;
517 msr_data
.entries
[0].index
= index
;
519 ret
= kvm_ioctl(s
, KVM_GET_MSRS
, &msr_data
);
521 error_report("KVM get MSR (index=0x%x) feature failed, %s",
522 index
, strerror(-ret
));
526 value
= msr_data
.entries
[0].data
;
528 case MSR_IA32_VMX_PROCBASED_CTLS2
:
529 if (!has_msr_vmx_procbased_ctls2
) {
530 /* KVM forgot to add these bits for some time, do this ourselves. */
531 if (kvm_arch_get_supported_cpuid(s
, 0xD, 1, R_ECX
) &
532 CPUID_XSAVE_XSAVES
) {
533 value
|= (uint64_t)VMX_SECONDARY_EXEC_XSAVES
<< 32;
535 if (kvm_arch_get_supported_cpuid(s
, 1, 0, R_ECX
) &
537 value
|= (uint64_t)VMX_SECONDARY_EXEC_RDRAND_EXITING
<< 32;
539 if (kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
) &
540 CPUID_7_0_EBX_INVPCID
) {
541 value
|= (uint64_t)VMX_SECONDARY_EXEC_ENABLE_INVPCID
<< 32;
543 if (kvm_arch_get_supported_cpuid(s
, 7, 0, R_EBX
) &
544 CPUID_7_0_EBX_RDSEED
) {
545 value
|= (uint64_t)VMX_SECONDARY_EXEC_RDSEED_EXITING
<< 32;
547 if (kvm_arch_get_supported_cpuid(s
, 0x80000001, 0, R_EDX
) &
549 value
|= (uint64_t)VMX_SECONDARY_EXEC_RDTSCP
<< 32;
553 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
554 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
555 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
556 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
558 * Return true for bits that can be one, but do not have to be one.
559 * The SDM tells us which bits could have a "must be one" setting,
560 * so we can do the opposite transformation in make_vmx_msr_value.
562 must_be_one
= (uint32_t)value
;
563 can_be_one
= (uint32_t)(value
>> 32);
564 return can_be_one
& ~must_be_one
;
571 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
576 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
579 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
584 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
586 CPUState
*cs
= CPU(cpu
);
587 CPUX86State
*env
= &cpu
->env
;
588 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
589 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
590 uint64_t mcg_status
= MCG_STATUS_MCIP
;
593 if (code
== BUS_MCEERR_AR
) {
594 status
|= MCI_STATUS_AR
| 0x134;
595 mcg_status
|= MCG_STATUS_RIPV
| MCG_STATUS_EIPV
;
598 mcg_status
|= MCG_STATUS_RIPV
;
601 flags
= cpu_x86_support_mca_broadcast(env
) ? MCE_INJECT_BROADCAST
: 0;
602 /* We need to read back the value of MSR_EXT_MCG_CTL that was set by the
603 * guest kernel back into env->mcg_ext_ctl.
605 cpu_synchronize_state(cs
);
606 if (env
->mcg_ext_ctl
& MCG_EXT_CTL_LMCE_EN
) {
607 mcg_status
|= MCG_STATUS_LMCE
;
611 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
612 (MCM_ADDR_PHYS
<< 6) | 0xc, flags
);
615 static void emit_hypervisor_memory_failure(MemoryFailureAction action
, bool ar
)
617 MemoryFailureFlags mff
= {.action_required
= ar
, .recursive
= false};
619 qapi_event_send_memory_failure(MEMORY_FAILURE_RECIPIENT_HYPERVISOR
, action
,
623 static void hardware_memory_error(void *host_addr
)
625 emit_hypervisor_memory_failure(MEMORY_FAILURE_ACTION_FATAL
, true);
626 error_report("QEMU got Hardware memory error at addr %p", host_addr
);
630 void kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
632 X86CPU
*cpu
= X86_CPU(c
);
633 CPUX86State
*env
= &cpu
->env
;
637 /* If we get an action required MCE, it has been injected by KVM
638 * while the VM was running. An action optional MCE instead should
639 * be coming from the main thread, which qemu_init_sigbus identifies
640 * as the "early kill" thread.
642 assert(code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
);
644 if ((env
->mcg_cap
& MCG_SER_P
) && addr
) {
645 ram_addr
= qemu_ram_addr_from_host(addr
);
646 if (ram_addr
!= RAM_ADDR_INVALID
&&
647 kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
648 kvm_hwpoison_page_add(ram_addr
);
649 kvm_mce_inject(cpu
, paddr
, code
);
652 * Use different logging severity based on error type.
653 * If there is additional MCE reporting on the hypervisor, QEMU VA
654 * could be another source to identify the PA and MCE details.
656 if (code
== BUS_MCEERR_AR
) {
657 error_report("Guest MCE Memory Error at QEMU addr %p and "
658 "GUEST addr 0x%" HWADDR_PRIx
" of type %s injected",
659 addr
, paddr
, "BUS_MCEERR_AR");
661 warn_report("Guest MCE Memory Error at QEMU addr %p and "
662 "GUEST addr 0x%" HWADDR_PRIx
" of type %s injected",
663 addr
, paddr
, "BUS_MCEERR_AO");
669 if (code
== BUS_MCEERR_AO
) {
670 warn_report("Hardware memory error at addr %p of type %s "
671 "for memory used by QEMU itself instead of guest system!",
672 addr
, "BUS_MCEERR_AO");
676 if (code
== BUS_MCEERR_AR
) {
677 hardware_memory_error(addr
);
680 /* Hope we are lucky for AO MCE, just notify a event */
681 emit_hypervisor_memory_failure(MEMORY_FAILURE_ACTION_IGNORE
, false);
684 static void kvm_reset_exception(CPUX86State
*env
)
686 env
->exception_nr
= -1;
687 env
->exception_pending
= 0;
688 env
->exception_injected
= 0;
689 env
->exception_has_payload
= false;
690 env
->exception_payload
= 0;
693 static void kvm_queue_exception(CPUX86State
*env
,
694 int32_t exception_nr
,
695 uint8_t exception_has_payload
,
696 uint64_t exception_payload
)
698 assert(env
->exception_nr
== -1);
699 assert(!env
->exception_pending
);
700 assert(!env
->exception_injected
);
701 assert(!env
->exception_has_payload
);
703 env
->exception_nr
= exception_nr
;
705 if (has_exception_payload
) {
706 env
->exception_pending
= 1;
708 env
->exception_has_payload
= exception_has_payload
;
709 env
->exception_payload
= exception_payload
;
711 env
->exception_injected
= 1;
713 if (exception_nr
== EXCP01_DB
) {
714 assert(exception_has_payload
);
715 env
->dr
[6] = exception_payload
;
716 } else if (exception_nr
== EXCP0E_PAGE
) {
717 assert(exception_has_payload
);
718 env
->cr
[2] = exception_payload
;
720 assert(!exception_has_payload
);
725 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
727 CPUX86State
*env
= &cpu
->env
;
729 if (!kvm_has_vcpu_events() && env
->exception_nr
== EXCP12_MCHK
) {
730 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
731 struct kvm_x86_mce mce
;
733 kvm_reset_exception(env
);
736 * There must be at least one bank in use if an MCE is pending.
737 * Find it and use its values for the event injection.
739 for (bank
= 0; bank
< bank_num
; bank
++) {
740 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
744 assert(bank
< bank_num
);
747 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
748 mce
.mcg_status
= env
->mcg_status
;
749 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
750 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
752 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
757 static void cpu_update_state(void *opaque
, bool running
, RunState state
)
759 CPUX86State
*env
= opaque
;
762 env
->tsc_valid
= false;
766 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
768 X86CPU
*cpu
= X86_CPU(cs
);
772 #ifndef KVM_CPUID_SIGNATURE_NEXT
773 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
776 static bool hyperv_enabled(X86CPU
*cpu
)
778 return kvm_check_extension(kvm_state
, KVM_CAP_HYPERV
) > 0 &&
779 ((cpu
->hyperv_spinlock_attempts
!= HYPERV_SPINLOCK_NEVER_NOTIFY
) ||
780 cpu
->hyperv_features
|| cpu
->hyperv_passthrough
);
784 * Check whether target_freq is within conservative
785 * ntp correctable bounds (250ppm) of freq
787 static inline bool freq_within_bounds(int freq
, int target_freq
)
789 int max_freq
= freq
+ (freq
* 250 / 1000000);
790 int min_freq
= freq
- (freq
* 250 / 1000000);
792 if (target_freq
>= min_freq
&& target_freq
<= max_freq
) {
799 static int kvm_arch_set_tsc_khz(CPUState
*cs
)
801 X86CPU
*cpu
= X86_CPU(cs
);
802 CPUX86State
*env
= &cpu
->env
;
804 bool set_ioctl
= false;
810 cur_freq
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GET_TSC_KHZ
) ?
811 kvm_vcpu_ioctl(cs
, KVM_GET_TSC_KHZ
) : -ENOTSUP
;
814 * If TSC scaling is supported, attempt to set TSC frequency.
816 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
)) {
821 * If desired TSC frequency is within bounds of NTP correction,
822 * attempt to set TSC frequency.
824 if (cur_freq
!= -ENOTSUP
&& freq_within_bounds(cur_freq
, env
->tsc_khz
)) {
829 kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
) :
833 /* When KVM_SET_TSC_KHZ fails, it's an error only if the current
834 * TSC frequency doesn't match the one we want.
836 cur_freq
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GET_TSC_KHZ
) ?
837 kvm_vcpu_ioctl(cs
, KVM_GET_TSC_KHZ
) :
839 if (cur_freq
<= 0 || cur_freq
!= env
->tsc_khz
) {
840 warn_report("TSC frequency mismatch between "
841 "VM (%" PRId64
" kHz) and host (%d kHz), "
842 "and TSC scaling unavailable",
843 env
->tsc_khz
, cur_freq
);
851 static bool tsc_is_stable_and_known(CPUX86State
*env
)
856 return (env
->features
[FEAT_8000_0007_EDX
] & CPUID_APM_INVTSC
)
857 || env
->user_tsc_khz
;
860 #define DEFAULT_EVMCS_VERSION ((1 << 8) | 1)
869 uint64_t dependencies
;
870 } kvm_hyperv_properties
[] = {
871 [HYPERV_FEAT_RELAXED
] = {
872 .desc
= "relaxed timing (hv-relaxed)",
874 {.func
= HV_CPUID_ENLIGHTMENT_INFO
, .reg
= R_EAX
,
875 .bits
= HV_RELAXED_TIMING_RECOMMENDED
}
878 [HYPERV_FEAT_VAPIC
] = {
879 .desc
= "virtual APIC (hv-vapic)",
881 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
882 .bits
= HV_APIC_ACCESS_AVAILABLE
}
885 [HYPERV_FEAT_TIME
] = {
886 .desc
= "clocksources (hv-time)",
888 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
889 .bits
= HV_TIME_REF_COUNT_AVAILABLE
| HV_REFERENCE_TSC_AVAILABLE
}
892 [HYPERV_FEAT_CRASH
] = {
893 .desc
= "crash MSRs (hv-crash)",
895 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
896 .bits
= HV_GUEST_CRASH_MSR_AVAILABLE
}
899 [HYPERV_FEAT_RESET
] = {
900 .desc
= "reset MSR (hv-reset)",
902 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
903 .bits
= HV_RESET_AVAILABLE
}
906 [HYPERV_FEAT_VPINDEX
] = {
907 .desc
= "VP_INDEX MSR (hv-vpindex)",
909 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
910 .bits
= HV_VP_INDEX_AVAILABLE
}
913 [HYPERV_FEAT_RUNTIME
] = {
914 .desc
= "VP_RUNTIME MSR (hv-runtime)",
916 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
917 .bits
= HV_VP_RUNTIME_AVAILABLE
}
920 [HYPERV_FEAT_SYNIC
] = {
921 .desc
= "synthetic interrupt controller (hv-synic)",
923 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
924 .bits
= HV_SYNIC_AVAILABLE
}
927 [HYPERV_FEAT_STIMER
] = {
928 .desc
= "synthetic timers (hv-stimer)",
930 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
931 .bits
= HV_SYNTIMERS_AVAILABLE
}
933 .dependencies
= BIT(HYPERV_FEAT_SYNIC
) | BIT(HYPERV_FEAT_TIME
)
935 [HYPERV_FEAT_FREQUENCIES
] = {
936 .desc
= "frequency MSRs (hv-frequencies)",
938 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
939 .bits
= HV_ACCESS_FREQUENCY_MSRS
},
940 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
941 .bits
= HV_FREQUENCY_MSRS_AVAILABLE
}
944 [HYPERV_FEAT_REENLIGHTENMENT
] = {
945 .desc
= "reenlightenment MSRs (hv-reenlightenment)",
947 {.func
= HV_CPUID_FEATURES
, .reg
= R_EAX
,
948 .bits
= HV_ACCESS_REENLIGHTENMENTS_CONTROL
}
951 [HYPERV_FEAT_TLBFLUSH
] = {
952 .desc
= "paravirtualized TLB flush (hv-tlbflush)",
954 {.func
= HV_CPUID_ENLIGHTMENT_INFO
, .reg
= R_EAX
,
955 .bits
= HV_REMOTE_TLB_FLUSH_RECOMMENDED
|
956 HV_EX_PROCESSOR_MASKS_RECOMMENDED
}
958 .dependencies
= BIT(HYPERV_FEAT_VPINDEX
)
960 [HYPERV_FEAT_EVMCS
] = {
961 .desc
= "enlightened VMCS (hv-evmcs)",
963 {.func
= HV_CPUID_ENLIGHTMENT_INFO
, .reg
= R_EAX
,
964 .bits
= HV_ENLIGHTENED_VMCS_RECOMMENDED
}
966 .dependencies
= BIT(HYPERV_FEAT_VAPIC
)
968 [HYPERV_FEAT_IPI
] = {
969 .desc
= "paravirtualized IPI (hv-ipi)",
971 {.func
= HV_CPUID_ENLIGHTMENT_INFO
, .reg
= R_EAX
,
972 .bits
= HV_CLUSTER_IPI_RECOMMENDED
|
973 HV_EX_PROCESSOR_MASKS_RECOMMENDED
}
975 .dependencies
= BIT(HYPERV_FEAT_VPINDEX
)
977 [HYPERV_FEAT_STIMER_DIRECT
] = {
978 .desc
= "direct mode synthetic timers (hv-stimer-direct)",
980 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
981 .bits
= HV_STIMER_DIRECT_MODE_AVAILABLE
}
983 .dependencies
= BIT(HYPERV_FEAT_STIMER
)
985 [HYPERV_FEAT_AVIC
] = {
986 .desc
= "AVIC/APICv support (hv-avic/hv-apicv)",
988 {.func
= HV_CPUID_ENLIGHTMENT_INFO
, .reg
= R_EAX
,
989 .bits
= HV_DEPRECATING_AEOI_RECOMMENDED
}
993 [HYPERV_FEAT_SYNDBG
] = {
994 .desc
= "Enable synthetic kernel debugger channel (hv-syndbg)",
996 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
997 .bits
= HV_FEATURE_DEBUG_MSRS_AVAILABLE
}
999 .dependencies
= BIT(HYPERV_FEAT_SYNIC
) | BIT(HYPERV_FEAT_RELAXED
)
1002 [HYPERV_FEAT_MSR_BITMAP
] = {
1003 .desc
= "enlightened MSR-Bitmap (hv-emsr-bitmap)",
1005 {.func
= HV_CPUID_NESTED_FEATURES
, .reg
= R_EAX
,
1006 .bits
= HV_NESTED_MSR_BITMAP
}
1009 [HYPERV_FEAT_XMM_INPUT
] = {
1010 .desc
= "XMM fast hypercall input (hv-xmm-input)",
1012 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
1013 .bits
= HV_HYPERCALL_XMM_INPUT_AVAILABLE
}
1016 [HYPERV_FEAT_TLBFLUSH_EXT
] = {
1017 .desc
= "Extended gva ranges for TLB flush hypercalls (hv-tlbflush-ext)",
1019 {.func
= HV_CPUID_FEATURES
, .reg
= R_EDX
,
1020 .bits
= HV_EXT_GVA_RANGES_FLUSH_AVAILABLE
}
1022 .dependencies
= BIT(HYPERV_FEAT_TLBFLUSH
)
1024 [HYPERV_FEAT_TLBFLUSH_DIRECT
] = {
1025 .desc
= "direct TLB flush (hv-tlbflush-direct)",
1027 {.func
= HV_CPUID_NESTED_FEATURES
, .reg
= R_EAX
,
1028 .bits
= HV_NESTED_DIRECT_FLUSH
}
1030 .dependencies
= BIT(HYPERV_FEAT_VAPIC
)
1034 static struct kvm_cpuid2
*try_get_hv_cpuid(CPUState
*cs
, int max
,
1037 struct kvm_cpuid2
*cpuid
;
1040 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
1041 cpuid
= g_malloc0(size
);
1045 r
= kvm_ioctl(kvm_state
, KVM_GET_SUPPORTED_HV_CPUID
, cpuid
);
1047 r
= kvm_vcpu_ioctl(cs
, KVM_GET_SUPPORTED_HV_CPUID
, cpuid
);
1049 if (r
== 0 && cpuid
->nent
>= max
) {
1057 fprintf(stderr
, "KVM_GET_SUPPORTED_HV_CPUID failed: %s\n",
1066 * Run KVM_GET_SUPPORTED_HV_CPUID ioctl(), allocating a buffer large enough
1069 static struct kvm_cpuid2
*get_supported_hv_cpuid(CPUState
*cs
)
1071 struct kvm_cpuid2
*cpuid
;
1072 /* 0x40000000..0x40000005, 0x4000000A, 0x40000080..0x40000082 leaves */
1078 kvm_check_extension(kvm_state
, KVM_CAP_SYS_HYPERV_CPUID
) > 0;
1081 * Non-empty KVM context is needed when KVM_CAP_SYS_HYPERV_CPUID is
1082 * unsupported, kvm_hyperv_expand_features() checks for that.
1084 assert(do_sys_ioctl
|| cs
->kvm_state
);
1087 * When the buffer is too small, KVM_GET_SUPPORTED_HV_CPUID fails with
1088 * -E2BIG, however, it doesn't report back the right size. Keep increasing
1089 * it and re-trying until we succeed.
1091 while ((cpuid
= try_get_hv_cpuid(cs
, max
, do_sys_ioctl
)) == NULL
) {
1096 * KVM_GET_SUPPORTED_HV_CPUID does not set EVMCS CPUID bit before
1097 * KVM_CAP_HYPERV_ENLIGHTENED_VMCS is enabled but we want to get the
1098 * information early, just check for the capability and set the bit
1101 if (!do_sys_ioctl
&& kvm_check_extension(cs
->kvm_state
,
1102 KVM_CAP_HYPERV_ENLIGHTENED_VMCS
) > 0) {
1103 for (i
= 0; i
< cpuid
->nent
; i
++) {
1104 if (cpuid
->entries
[i
].function
== HV_CPUID_ENLIGHTMENT_INFO
) {
1105 cpuid
->entries
[i
].eax
|= HV_ENLIGHTENED_VMCS_RECOMMENDED
;
1114 * When KVM_GET_SUPPORTED_HV_CPUID is not supported we fill CPUID feature
1115 * leaves from KVM_CAP_HYPERV* and present MSRs data.
1117 static struct kvm_cpuid2
*get_supported_hv_cpuid_legacy(CPUState
*cs
)
1119 X86CPU
*cpu
= X86_CPU(cs
);
1120 struct kvm_cpuid2
*cpuid
;
1121 struct kvm_cpuid_entry2
*entry_feat
, *entry_recomm
;
1123 /* HV_CPUID_FEATURES, HV_CPUID_ENLIGHTMENT_INFO */
1124 cpuid
= g_malloc0(sizeof(*cpuid
) + 2 * sizeof(*cpuid
->entries
));
1127 /* HV_CPUID_VENDOR_AND_MAX_FUNCTIONS */
1128 entry_feat
= &cpuid
->entries
[0];
1129 entry_feat
->function
= HV_CPUID_FEATURES
;
1131 entry_recomm
= &cpuid
->entries
[1];
1132 entry_recomm
->function
= HV_CPUID_ENLIGHTMENT_INFO
;
1133 entry_recomm
->ebx
= cpu
->hyperv_spinlock_attempts
;
1135 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV
) > 0) {
1136 entry_feat
->eax
|= HV_HYPERCALL_AVAILABLE
;
1137 entry_feat
->eax
|= HV_APIC_ACCESS_AVAILABLE
;
1138 entry_feat
->edx
|= HV_CPU_DYNAMIC_PARTITIONING_AVAILABLE
;
1139 entry_recomm
->eax
|= HV_RELAXED_TIMING_RECOMMENDED
;
1140 entry_recomm
->eax
|= HV_APIC_ACCESS_RECOMMENDED
;
1143 if (kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV_TIME
) > 0) {
1144 entry_feat
->eax
|= HV_TIME_REF_COUNT_AVAILABLE
;
1145 entry_feat
->eax
|= HV_REFERENCE_TSC_AVAILABLE
;
1148 if (has_msr_hv_frequencies
) {
1149 entry_feat
->eax
|= HV_ACCESS_FREQUENCY_MSRS
;
1150 entry_feat
->edx
|= HV_FREQUENCY_MSRS_AVAILABLE
;
1153 if (has_msr_hv_crash
) {
1154 entry_feat
->edx
|= HV_GUEST_CRASH_MSR_AVAILABLE
;
1157 if (has_msr_hv_reenlightenment
) {
1158 entry_feat
->eax
|= HV_ACCESS_REENLIGHTENMENTS_CONTROL
;
1161 if (has_msr_hv_reset
) {
1162 entry_feat
->eax
|= HV_RESET_AVAILABLE
;
1165 if (has_msr_hv_vpindex
) {
1166 entry_feat
->eax
|= HV_VP_INDEX_AVAILABLE
;
1169 if (has_msr_hv_runtime
) {
1170 entry_feat
->eax
|= HV_VP_RUNTIME_AVAILABLE
;
1173 if (has_msr_hv_synic
) {
1174 unsigned int cap
= cpu
->hyperv_synic_kvm_only
?
1175 KVM_CAP_HYPERV_SYNIC
: KVM_CAP_HYPERV_SYNIC2
;
1177 if (kvm_check_extension(cs
->kvm_state
, cap
) > 0) {
1178 entry_feat
->eax
|= HV_SYNIC_AVAILABLE
;
1182 if (has_msr_hv_stimer
) {
1183 entry_feat
->eax
|= HV_SYNTIMERS_AVAILABLE
;
1186 if (has_msr_hv_syndbg_options
) {
1187 entry_feat
->edx
|= HV_GUEST_DEBUGGING_AVAILABLE
;
1188 entry_feat
->edx
|= HV_FEATURE_DEBUG_MSRS_AVAILABLE
;
1189 entry_feat
->ebx
|= HV_PARTITION_DEBUGGING_ALLOWED
;
1192 if (kvm_check_extension(cs
->kvm_state
,
1193 KVM_CAP_HYPERV_TLBFLUSH
) > 0) {
1194 entry_recomm
->eax
|= HV_REMOTE_TLB_FLUSH_RECOMMENDED
;
1195 entry_recomm
->eax
|= HV_EX_PROCESSOR_MASKS_RECOMMENDED
;
1198 if (kvm_check_extension(cs
->kvm_state
,
1199 KVM_CAP_HYPERV_ENLIGHTENED_VMCS
) > 0) {
1200 entry_recomm
->eax
|= HV_ENLIGHTENED_VMCS_RECOMMENDED
;
1203 if (kvm_check_extension(cs
->kvm_state
,
1204 KVM_CAP_HYPERV_SEND_IPI
) > 0) {
1205 entry_recomm
->eax
|= HV_CLUSTER_IPI_RECOMMENDED
;
1206 entry_recomm
->eax
|= HV_EX_PROCESSOR_MASKS_RECOMMENDED
;
1212 static uint32_t hv_cpuid_get_host(CPUState
*cs
, uint32_t func
, int reg
)
1214 struct kvm_cpuid_entry2
*entry
;
1215 struct kvm_cpuid2
*cpuid
;
1217 if (hv_cpuid_cache
) {
1218 cpuid
= hv_cpuid_cache
;
1220 if (kvm_check_extension(kvm_state
, KVM_CAP_HYPERV_CPUID
) > 0) {
1221 cpuid
= get_supported_hv_cpuid(cs
);
1224 * 'cs->kvm_state' may be NULL when Hyper-V features are expanded
1225 * before KVM context is created but this is only done when
1226 * KVM_CAP_SYS_HYPERV_CPUID is supported and it implies
1227 * KVM_CAP_HYPERV_CPUID.
1229 assert(cs
->kvm_state
);
1231 cpuid
= get_supported_hv_cpuid_legacy(cs
);
1233 hv_cpuid_cache
= cpuid
;
1240 entry
= cpuid_find_entry(cpuid
, func
, 0);
1245 return cpuid_entry_get_reg(entry
, reg
);
1248 static bool hyperv_feature_supported(CPUState
*cs
, int feature
)
1250 uint32_t func
, bits
;
1253 for (i
= 0; i
< ARRAY_SIZE(kvm_hyperv_properties
[feature
].flags
); i
++) {
1255 func
= kvm_hyperv_properties
[feature
].flags
[i
].func
;
1256 reg
= kvm_hyperv_properties
[feature
].flags
[i
].reg
;
1257 bits
= kvm_hyperv_properties
[feature
].flags
[i
].bits
;
1263 if ((hv_cpuid_get_host(cs
, func
, reg
) & bits
) != bits
) {
1271 /* Checks that all feature dependencies are enabled */
1272 static bool hv_feature_check_deps(X86CPU
*cpu
, int feature
, Error
**errp
)
1277 deps
= kvm_hyperv_properties
[feature
].dependencies
;
1279 dep_feat
= ctz64(deps
);
1280 if (!(hyperv_feat_enabled(cpu
, dep_feat
))) {
1281 error_setg(errp
, "Hyper-V %s requires Hyper-V %s",
1282 kvm_hyperv_properties
[feature
].desc
,
1283 kvm_hyperv_properties
[dep_feat
].desc
);
1286 deps
&= ~(1ull << dep_feat
);
1292 static uint32_t hv_build_cpuid_leaf(CPUState
*cs
, uint32_t func
, int reg
)
1294 X86CPU
*cpu
= X86_CPU(cs
);
1298 for (i
= 0; i
< ARRAY_SIZE(kvm_hyperv_properties
); i
++) {
1299 if (!hyperv_feat_enabled(cpu
, i
)) {
1303 for (j
= 0; j
< ARRAY_SIZE(kvm_hyperv_properties
[i
].flags
); j
++) {
1304 if (kvm_hyperv_properties
[i
].flags
[j
].func
!= func
) {
1307 if (kvm_hyperv_properties
[i
].flags
[j
].reg
!= reg
) {
1311 r
|= kvm_hyperv_properties
[i
].flags
[j
].bits
;
1315 /* HV_CPUID_NESTED_FEATURES.EAX also encodes the supported eVMCS range */
1316 if (func
== HV_CPUID_NESTED_FEATURES
&& reg
== R_EAX
) {
1317 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_EVMCS
)) {
1318 r
|= DEFAULT_EVMCS_VERSION
;
1326 * Expand Hyper-V CPU features. In partucular, check that all the requested
1327 * features are supported by the host and the sanity of the configuration
1328 * (that all the required dependencies are included). Also, this takes care
1329 * of 'hv_passthrough' mode and fills the environment with all supported
1332 bool kvm_hyperv_expand_features(X86CPU
*cpu
, Error
**errp
)
1334 CPUState
*cs
= CPU(cpu
);
1335 Error
*local_err
= NULL
;
1338 if (!hyperv_enabled(cpu
))
1342 * When kvm_hyperv_expand_features is called at CPU feature expansion
1343 * time per-CPU kvm_state is not available yet so we can only proceed
1344 * when KVM_CAP_SYS_HYPERV_CPUID is supported.
1346 if (!cs
->kvm_state
&&
1347 !kvm_check_extension(kvm_state
, KVM_CAP_SYS_HYPERV_CPUID
))
1350 if (cpu
->hyperv_passthrough
) {
1351 cpu
->hyperv_vendor_id
[0] =
1352 hv_cpuid_get_host(cs
, HV_CPUID_VENDOR_AND_MAX_FUNCTIONS
, R_EBX
);
1353 cpu
->hyperv_vendor_id
[1] =
1354 hv_cpuid_get_host(cs
, HV_CPUID_VENDOR_AND_MAX_FUNCTIONS
, R_ECX
);
1355 cpu
->hyperv_vendor_id
[2] =
1356 hv_cpuid_get_host(cs
, HV_CPUID_VENDOR_AND_MAX_FUNCTIONS
, R_EDX
);
1357 cpu
->hyperv_vendor
= g_realloc(cpu
->hyperv_vendor
,
1358 sizeof(cpu
->hyperv_vendor_id
) + 1);
1359 memcpy(cpu
->hyperv_vendor
, cpu
->hyperv_vendor_id
,
1360 sizeof(cpu
->hyperv_vendor_id
));
1361 cpu
->hyperv_vendor
[sizeof(cpu
->hyperv_vendor_id
)] = 0;
1363 cpu
->hyperv_interface_id
[0] =
1364 hv_cpuid_get_host(cs
, HV_CPUID_INTERFACE
, R_EAX
);
1365 cpu
->hyperv_interface_id
[1] =
1366 hv_cpuid_get_host(cs
, HV_CPUID_INTERFACE
, R_EBX
);
1367 cpu
->hyperv_interface_id
[2] =
1368 hv_cpuid_get_host(cs
, HV_CPUID_INTERFACE
, R_ECX
);
1369 cpu
->hyperv_interface_id
[3] =
1370 hv_cpuid_get_host(cs
, HV_CPUID_INTERFACE
, R_EDX
);
1372 cpu
->hyperv_ver_id_build
=
1373 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_EAX
);
1374 cpu
->hyperv_ver_id_major
=
1375 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_EBX
) >> 16;
1376 cpu
->hyperv_ver_id_minor
=
1377 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_EBX
) & 0xffff;
1378 cpu
->hyperv_ver_id_sp
=
1379 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_ECX
);
1380 cpu
->hyperv_ver_id_sb
=
1381 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_EDX
) >> 24;
1382 cpu
->hyperv_ver_id_sn
=
1383 hv_cpuid_get_host(cs
, HV_CPUID_VERSION
, R_EDX
) & 0xffffff;
1385 cpu
->hv_max_vps
= hv_cpuid_get_host(cs
, HV_CPUID_IMPLEMENT_LIMITS
,
1387 cpu
->hyperv_limits
[0] =
1388 hv_cpuid_get_host(cs
, HV_CPUID_IMPLEMENT_LIMITS
, R_EBX
);
1389 cpu
->hyperv_limits
[1] =
1390 hv_cpuid_get_host(cs
, HV_CPUID_IMPLEMENT_LIMITS
, R_ECX
);
1391 cpu
->hyperv_limits
[2] =
1392 hv_cpuid_get_host(cs
, HV_CPUID_IMPLEMENT_LIMITS
, R_EDX
);
1394 cpu
->hyperv_spinlock_attempts
=
1395 hv_cpuid_get_host(cs
, HV_CPUID_ENLIGHTMENT_INFO
, R_EBX
);
1398 * Mark feature as enabled in 'cpu->hyperv_features' as
1399 * hv_build_cpuid_leaf() uses this info to build guest CPUIDs.
1401 for (feat
= 0; feat
< ARRAY_SIZE(kvm_hyperv_properties
); feat
++) {
1402 if (hyperv_feature_supported(cs
, feat
)) {
1403 cpu
->hyperv_features
|= BIT(feat
);
1407 /* Check features availability and dependencies */
1408 for (feat
= 0; feat
< ARRAY_SIZE(kvm_hyperv_properties
); feat
++) {
1409 /* If the feature was not requested skip it. */
1410 if (!hyperv_feat_enabled(cpu
, feat
)) {
1414 /* Check if the feature is supported by KVM */
1415 if (!hyperv_feature_supported(cs
, feat
)) {
1416 error_setg(errp
, "Hyper-V %s is not supported by kernel",
1417 kvm_hyperv_properties
[feat
].desc
);
1421 /* Check dependencies */
1422 if (!hv_feature_check_deps(cpu
, feat
, &local_err
)) {
1423 error_propagate(errp
, local_err
);
1429 /* Additional dependencies not covered by kvm_hyperv_properties[] */
1430 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
) &&
1431 !cpu
->hyperv_synic_kvm_only
&&
1432 !hyperv_feat_enabled(cpu
, HYPERV_FEAT_VPINDEX
)) {
1433 error_setg(errp
, "Hyper-V %s requires Hyper-V %s",
1434 kvm_hyperv_properties
[HYPERV_FEAT_SYNIC
].desc
,
1435 kvm_hyperv_properties
[HYPERV_FEAT_VPINDEX
].desc
);
1443 * Fill in Hyper-V CPUIDs. Returns the number of entries filled in cpuid_ent.
1445 static int hyperv_fill_cpuids(CPUState
*cs
,
1446 struct kvm_cpuid_entry2
*cpuid_ent
)
1448 X86CPU
*cpu
= X86_CPU(cs
);
1449 struct kvm_cpuid_entry2
*c
;
1450 uint32_t signature
[3];
1451 uint32_t cpuid_i
= 0, max_cpuid_leaf
= 0;
1452 uint32_t nested_eax
=
1453 hv_build_cpuid_leaf(cs
, HV_CPUID_NESTED_FEATURES
, R_EAX
);
1455 max_cpuid_leaf
= nested_eax
? HV_CPUID_NESTED_FEATURES
:
1456 HV_CPUID_IMPLEMENT_LIMITS
;
1458 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNDBG
)) {
1460 MAX(max_cpuid_leaf
, HV_CPUID_SYNDBG_PLATFORM_CAPABILITIES
);
1463 c
= &cpuid_ent
[cpuid_i
++];
1464 c
->function
= HV_CPUID_VENDOR_AND_MAX_FUNCTIONS
;
1465 c
->eax
= max_cpuid_leaf
;
1466 c
->ebx
= cpu
->hyperv_vendor_id
[0];
1467 c
->ecx
= cpu
->hyperv_vendor_id
[1];
1468 c
->edx
= cpu
->hyperv_vendor_id
[2];
1470 c
= &cpuid_ent
[cpuid_i
++];
1471 c
->function
= HV_CPUID_INTERFACE
;
1472 c
->eax
= cpu
->hyperv_interface_id
[0];
1473 c
->ebx
= cpu
->hyperv_interface_id
[1];
1474 c
->ecx
= cpu
->hyperv_interface_id
[2];
1475 c
->edx
= cpu
->hyperv_interface_id
[3];
1477 c
= &cpuid_ent
[cpuid_i
++];
1478 c
->function
= HV_CPUID_VERSION
;
1479 c
->eax
= cpu
->hyperv_ver_id_build
;
1480 c
->ebx
= (uint32_t)cpu
->hyperv_ver_id_major
<< 16 |
1481 cpu
->hyperv_ver_id_minor
;
1482 c
->ecx
= cpu
->hyperv_ver_id_sp
;
1483 c
->edx
= (uint32_t)cpu
->hyperv_ver_id_sb
<< 24 |
1484 (cpu
->hyperv_ver_id_sn
& 0xffffff);
1486 c
= &cpuid_ent
[cpuid_i
++];
1487 c
->function
= HV_CPUID_FEATURES
;
1488 c
->eax
= hv_build_cpuid_leaf(cs
, HV_CPUID_FEATURES
, R_EAX
);
1489 c
->ebx
= hv_build_cpuid_leaf(cs
, HV_CPUID_FEATURES
, R_EBX
);
1490 c
->edx
= hv_build_cpuid_leaf(cs
, HV_CPUID_FEATURES
, R_EDX
);
1492 /* Unconditionally required with any Hyper-V enlightenment */
1493 c
->eax
|= HV_HYPERCALL_AVAILABLE
;
1495 /* SynIC and Vmbus devices require messages/signals hypercalls */
1496 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
) &&
1497 !cpu
->hyperv_synic_kvm_only
) {
1498 c
->ebx
|= HV_POST_MESSAGES
| HV_SIGNAL_EVENTS
;
1502 /* Not exposed by KVM but needed to make CPU hotplug in Windows work */
1503 c
->edx
|= HV_CPU_DYNAMIC_PARTITIONING_AVAILABLE
;
1505 c
= &cpuid_ent
[cpuid_i
++];
1506 c
->function
= HV_CPUID_ENLIGHTMENT_INFO
;
1507 c
->eax
= hv_build_cpuid_leaf(cs
, HV_CPUID_ENLIGHTMENT_INFO
, R_EAX
);
1508 c
->ebx
= cpu
->hyperv_spinlock_attempts
;
1510 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VAPIC
) &&
1511 !hyperv_feat_enabled(cpu
, HYPERV_FEAT_AVIC
)) {
1512 c
->eax
|= HV_APIC_ACCESS_RECOMMENDED
;
1515 if (cpu
->hyperv_no_nonarch_cs
== ON_OFF_AUTO_ON
) {
1516 c
->eax
|= HV_NO_NONARCH_CORESHARING
;
1517 } else if (cpu
->hyperv_no_nonarch_cs
== ON_OFF_AUTO_AUTO
) {
1518 c
->eax
|= hv_cpuid_get_host(cs
, HV_CPUID_ENLIGHTMENT_INFO
, R_EAX
) &
1519 HV_NO_NONARCH_CORESHARING
;
1522 c
= &cpuid_ent
[cpuid_i
++];
1523 c
->function
= HV_CPUID_IMPLEMENT_LIMITS
;
1524 c
->eax
= cpu
->hv_max_vps
;
1525 c
->ebx
= cpu
->hyperv_limits
[0];
1526 c
->ecx
= cpu
->hyperv_limits
[1];
1527 c
->edx
= cpu
->hyperv_limits
[2];
1532 /* Create zeroed 0x40000006..0x40000009 leaves */
1533 for (function
= HV_CPUID_IMPLEMENT_LIMITS
+ 1;
1534 function
< HV_CPUID_NESTED_FEATURES
; function
++) {
1535 c
= &cpuid_ent
[cpuid_i
++];
1536 c
->function
= function
;
1539 c
= &cpuid_ent
[cpuid_i
++];
1540 c
->function
= HV_CPUID_NESTED_FEATURES
;
1541 c
->eax
= nested_eax
;
1544 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNDBG
)) {
1545 c
= &cpuid_ent
[cpuid_i
++];
1546 c
->function
= HV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS
;
1547 c
->eax
= hyperv_feat_enabled(cpu
, HYPERV_FEAT_EVMCS
) ?
1548 HV_CPUID_NESTED_FEATURES
: HV_CPUID_IMPLEMENT_LIMITS
;
1549 memcpy(signature
, "Microsoft VS", 12);
1551 c
->ebx
= signature
[0];
1552 c
->ecx
= signature
[1];
1553 c
->edx
= signature
[2];
1555 c
= &cpuid_ent
[cpuid_i
++];
1556 c
->function
= HV_CPUID_SYNDBG_INTERFACE
;
1557 memcpy(signature
, "VS#1\0\0\0\0\0\0\0\0", 12);
1558 c
->eax
= signature
[0];
1563 c
= &cpuid_ent
[cpuid_i
++];
1564 c
->function
= HV_CPUID_SYNDBG_PLATFORM_CAPABILITIES
;
1565 c
->eax
= HV_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING
;
1574 static Error
*hv_passthrough_mig_blocker
;
1575 static Error
*hv_no_nonarch_cs_mig_blocker
;
1577 /* Checks that the exposed eVMCS version range is supported by KVM */
1578 static bool evmcs_version_supported(uint16_t evmcs_version
,
1579 uint16_t supported_evmcs_version
)
1581 uint8_t min_version
= evmcs_version
& 0xff;
1582 uint8_t max_version
= evmcs_version
>> 8;
1583 uint8_t min_supported_version
= supported_evmcs_version
& 0xff;
1584 uint8_t max_supported_version
= supported_evmcs_version
>> 8;
1586 return (min_version
>= min_supported_version
) &&
1587 (max_version
<= max_supported_version
);
1590 static int hyperv_init_vcpu(X86CPU
*cpu
)
1592 CPUState
*cs
= CPU(cpu
);
1593 Error
*local_err
= NULL
;
1596 if (cpu
->hyperv_passthrough
&& hv_passthrough_mig_blocker
== NULL
) {
1597 error_setg(&hv_passthrough_mig_blocker
,
1598 "'hv-passthrough' CPU flag prevents migration, use explicit"
1599 " set of hv-* flags instead");
1600 ret
= migrate_add_blocker(hv_passthrough_mig_blocker
, &local_err
);
1602 error_report_err(local_err
);
1607 if (cpu
->hyperv_no_nonarch_cs
== ON_OFF_AUTO_AUTO
&&
1608 hv_no_nonarch_cs_mig_blocker
== NULL
) {
1609 error_setg(&hv_no_nonarch_cs_mig_blocker
,
1610 "'hv-no-nonarch-coresharing=auto' CPU flag prevents migration"
1611 " use explicit 'hv-no-nonarch-coresharing=on' instead (but"
1612 " make sure SMT is disabled and/or that vCPUs are properly"
1614 ret
= migrate_add_blocker(hv_no_nonarch_cs_mig_blocker
, &local_err
);
1616 error_report_err(local_err
);
1621 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VPINDEX
) && !hv_vpindex_settable
) {
1623 * the kernel doesn't support setting vp_index; assert that its value
1628 ret
= kvm_get_one_msr(cpu
, HV_X64_MSR_VP_INDEX
, &value
);
1633 if (value
!= hyperv_vp_index(CPU(cpu
))) {
1634 error_report("kernel's vp_index != QEMU's vp_index");
1639 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
)) {
1640 uint32_t synic_cap
= cpu
->hyperv_synic_kvm_only
?
1641 KVM_CAP_HYPERV_SYNIC
: KVM_CAP_HYPERV_SYNIC2
;
1642 ret
= kvm_vcpu_enable_cap(cs
, synic_cap
, 0);
1644 error_report("failed to turn on HyperV SynIC in KVM: %s",
1649 if (!cpu
->hyperv_synic_kvm_only
) {
1650 ret
= hyperv_x86_synic_add(cpu
);
1652 error_report("failed to create HyperV SynIC: %s",
1659 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_EVMCS
)) {
1660 uint16_t evmcs_version
= DEFAULT_EVMCS_VERSION
;
1661 uint16_t supported_evmcs_version
;
1663 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_HYPERV_ENLIGHTENED_VMCS
, 0,
1664 (uintptr_t)&supported_evmcs_version
);
1667 * KVM is required to support EVMCS ver.1. as that's what 'hv-evmcs'
1668 * option sets. Note: we hardcode the maximum supported eVMCS version
1669 * to '1' as well so 'hv-evmcs' feature is migratable even when (and if)
1670 * ver.2 is implemented. A new option (e.g. 'hv-evmcs=2') will then have
1674 error_report("Hyper-V %s is not supported by kernel",
1675 kvm_hyperv_properties
[HYPERV_FEAT_EVMCS
].desc
);
1679 if (!evmcs_version_supported(evmcs_version
, supported_evmcs_version
)) {
1680 error_report("eVMCS version range [%d..%d] is not supported by "
1681 "kernel (supported: [%d..%d])", evmcs_version
& 0xff,
1682 evmcs_version
>> 8, supported_evmcs_version
& 0xff,
1683 supported_evmcs_version
>> 8);
1688 if (cpu
->hyperv_enforce_cpuid
) {
1689 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_HYPERV_ENFORCE_CPUID
, 0, 1);
1691 error_report("failed to enable KVM_CAP_HYPERV_ENFORCE_CPUID: %s",
1700 static Error
*invtsc_mig_blocker
;
1702 #define KVM_MAX_CPUID_ENTRIES 100
1704 static void kvm_init_xsave(CPUX86State
*env
)
1707 env
->xsave_buf_len
= QEMU_ALIGN_UP(has_xsave2
, 4096);
1708 } else if (has_xsave
) {
1709 env
->xsave_buf_len
= sizeof(struct kvm_xsave
);
1714 env
->xsave_buf
= qemu_memalign(4096, env
->xsave_buf_len
);
1715 memset(env
->xsave_buf
, 0, env
->xsave_buf_len
);
1717 * The allocated storage must be large enough for all of the
1718 * possible XSAVE state components.
1720 assert(kvm_arch_get_supported_cpuid(kvm_state
, 0xd, 0, R_ECX
) <=
1721 env
->xsave_buf_len
);
1724 static void kvm_init_nested_state(CPUX86State
*env
)
1726 struct kvm_vmx_nested_state_hdr
*vmx_hdr
;
1729 if (!env
->nested_state
) {
1733 size
= env
->nested_state
->size
;
1735 memset(env
->nested_state
, 0, size
);
1736 env
->nested_state
->size
= size
;
1738 if (cpu_has_vmx(env
)) {
1739 env
->nested_state
->format
= KVM_STATE_NESTED_FORMAT_VMX
;
1740 vmx_hdr
= &env
->nested_state
->hdr
.vmx
;
1741 vmx_hdr
->vmxon_pa
= -1ull;
1742 vmx_hdr
->vmcs12_pa
= -1ull;
1743 } else if (cpu_has_svm(env
)) {
1744 env
->nested_state
->format
= KVM_STATE_NESTED_FORMAT_SVM
;
1748 int kvm_arch_init_vcpu(CPUState
*cs
)
1751 struct kvm_cpuid2 cpuid
;
1752 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
1755 * The kernel defines these structs with padding fields so there
1756 * should be no extra padding in our cpuid_data struct.
1758 QEMU_BUILD_BUG_ON(sizeof(cpuid_data
) !=
1759 sizeof(struct kvm_cpuid2
) +
1760 sizeof(struct kvm_cpuid_entry2
) * KVM_MAX_CPUID_ENTRIES
);
1762 X86CPU
*cpu
= X86_CPU(cs
);
1763 CPUX86State
*env
= &cpu
->env
;
1764 uint32_t limit
, i
, j
, cpuid_i
;
1766 struct kvm_cpuid_entry2
*c
;
1767 uint32_t signature
[3];
1768 int kvm_base
= KVM_CPUID_SIGNATURE
;
1769 int max_nested_state_len
;
1771 Error
*local_err
= NULL
;
1773 memset(&cpuid_data
, 0, sizeof(cpuid_data
));
1777 has_xsave2
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_XSAVE2
);
1779 r
= kvm_arch_set_tsc_khz(cs
);
1784 /* vcpu's TSC frequency is either specified by user, or following
1785 * the value used by KVM if the former is not present. In the
1786 * latter case, we query it from KVM and record in env->tsc_khz,
1787 * so that vcpu's TSC frequency can be migrated later via this field.
1789 if (!env
->tsc_khz
) {
1790 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_GET_TSC_KHZ
) ?
1791 kvm_vcpu_ioctl(cs
, KVM_GET_TSC_KHZ
) :
1798 env
->apic_bus_freq
= KVM_APIC_BUS_FREQUENCY
;
1801 * kvm_hyperv_expand_features() is called here for the second time in case
1802 * KVM_CAP_SYS_HYPERV_CPUID is not supported. While we can't possibly handle
1803 * 'query-cpu-model-expansion' in this case as we don't have a KVM vCPU to
1804 * check which Hyper-V enlightenments are supported and which are not, we
1805 * can still proceed and check/expand Hyper-V enlightenments here so legacy
1806 * behavior is preserved.
1808 if (!kvm_hyperv_expand_features(cpu
, &local_err
)) {
1809 error_report_err(local_err
);
1813 if (hyperv_enabled(cpu
)) {
1814 r
= hyperv_init_vcpu(cpu
);
1819 cpuid_i
= hyperv_fill_cpuids(cs
, cpuid_data
.entries
);
1820 kvm_base
= KVM_CPUID_SIGNATURE_NEXT
;
1821 has_msr_hv_hypercall
= true;
1824 if (cs
->kvm_state
->xen_version
) {
1825 #ifdef CONFIG_XEN_EMU
1826 struct kvm_cpuid_entry2
*xen_max_leaf
;
1828 memcpy(signature
, "XenVMMXenVMM", 12);
1830 xen_max_leaf
= c
= &cpuid_data
.entries
[cpuid_i
++];
1831 c
->function
= kvm_base
+ XEN_CPUID_SIGNATURE
;
1832 c
->eax
= kvm_base
+ XEN_CPUID_TIME
;
1833 c
->ebx
= signature
[0];
1834 c
->ecx
= signature
[1];
1835 c
->edx
= signature
[2];
1837 c
= &cpuid_data
.entries
[cpuid_i
++];
1838 c
->function
= kvm_base
+ XEN_CPUID_VENDOR
;
1839 c
->eax
= cs
->kvm_state
->xen_version
;
1844 c
= &cpuid_data
.entries
[cpuid_i
++];
1845 c
->function
= kvm_base
+ XEN_CPUID_HVM_MSR
;
1846 /* Number of hypercall-transfer pages */
1848 /* Hypercall MSR base address */
1849 if (hyperv_enabled(cpu
)) {
1850 c
->ebx
= XEN_HYPERCALL_MSR_HYPERV
;
1851 kvm_xen_init(cs
->kvm_state
, c
->ebx
);
1853 c
->ebx
= XEN_HYPERCALL_MSR
;
1858 c
= &cpuid_data
.entries
[cpuid_i
++];
1859 c
->function
= kvm_base
+ XEN_CPUID_TIME
;
1860 c
->eax
= ((!!tsc_is_stable_and_known(env
) << 1) |
1861 (!!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_RDTSCP
) << 2));
1862 /* default=0 (emulate if necessary) */
1864 /* guest tsc frequency */
1865 c
->ecx
= env
->user_tsc_khz
;
1866 /* guest tsc incarnation (migration count) */
1869 c
= &cpuid_data
.entries
[cpuid_i
++];
1870 c
->function
= kvm_base
+ XEN_CPUID_HVM
;
1871 xen_max_leaf
->eax
= kvm_base
+ XEN_CPUID_HVM
;
1872 if (cs
->kvm_state
->xen_version
>= XEN_VERSION(4, 5)) {
1873 c
->function
= kvm_base
+ XEN_CPUID_HVM
;
1875 if (cpu
->xen_vapic
) {
1876 c
->eax
|= XEN_HVM_CPUID_APIC_ACCESS_VIRT
;
1877 c
->eax
|= XEN_HVM_CPUID_X2APIC_VIRT
;
1880 c
->eax
|= XEN_HVM_CPUID_IOMMU_MAPPINGS
;
1882 if (cs
->kvm_state
->xen_version
>= XEN_VERSION(4, 6)) {
1883 c
->eax
|= XEN_HVM_CPUID_VCPU_ID_PRESENT
;
1884 c
->ebx
= cs
->cpu_index
;
1888 r
= kvm_xen_init_vcpu(cs
);
1894 #else /* CONFIG_XEN_EMU */
1895 /* This should never happen as kvm_arch_init() would have died first. */
1896 fprintf(stderr
, "Cannot enable Xen CPUID without Xen support\n");
1899 } else if (cpu
->expose_kvm
) {
1900 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
1901 c
= &cpuid_data
.entries
[cpuid_i
++];
1902 c
->function
= KVM_CPUID_SIGNATURE
| kvm_base
;
1903 c
->eax
= KVM_CPUID_FEATURES
| kvm_base
;
1904 c
->ebx
= signature
[0];
1905 c
->ecx
= signature
[1];
1906 c
->edx
= signature
[2];
1908 c
= &cpuid_data
.entries
[cpuid_i
++];
1909 c
->function
= KVM_CPUID_FEATURES
| kvm_base
;
1910 c
->eax
= env
->features
[FEAT_KVM
];
1911 c
->edx
= env
->features
[FEAT_KVM_HINTS
];
1914 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
1916 if (cpu
->kvm_pv_enforce_cpuid
) {
1917 r
= kvm_vcpu_enable_cap(cs
, KVM_CAP_ENFORCE_PV_FEATURE_CPUID
, 0, 1);
1920 "failed to enable KVM_CAP_ENFORCE_PV_FEATURE_CPUID: %s",
1926 for (i
= 0; i
<= limit
; i
++) {
1927 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
1928 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
1931 c
= &cpuid_data
.entries
[cpuid_i
++];
1935 /* Keep reading function 2 till all the input is received */
1939 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
1940 KVM_CPUID_FLAG_STATE_READ_NEXT
;
1941 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
1942 times
= c
->eax
& 0xff;
1944 for (j
= 1; j
< times
; ++j
) {
1945 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
1946 fprintf(stderr
, "cpuid_data is full, no space for "
1947 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
1950 c
= &cpuid_data
.entries
[cpuid_i
++];
1952 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1953 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
1958 if (env
->nr_dies
< 2) {
1965 for (j
= 0; ; j
++) {
1966 if (i
== 0xd && j
== 64) {
1970 if (i
== 0x1f && j
== 64) {
1975 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1977 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
1979 if (i
== 4 && c
->eax
== 0) {
1982 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
1985 if (i
== 0x1f && !(c
->ecx
& 0xff00)) {
1988 if (i
== 0xd && c
->eax
== 0) {
1991 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
1992 fprintf(stderr
, "cpuid_data is full, no space for "
1993 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
1996 c
= &cpuid_data
.entries
[cpuid_i
++];
2001 for (j
= 0; ; j
++) {
2003 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2005 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2007 if (j
> 1 && (c
->eax
& 0xf) != 1) {
2011 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
2012 fprintf(stderr
, "cpuid_data is full, no space for "
2013 "cpuid(eax:0x12,ecx:0x%x)\n", j
);
2016 c
= &cpuid_data
.entries
[cpuid_i
++];
2026 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2027 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2030 for (j
= 1; j
<= times
; ++j
) {
2031 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
2032 fprintf(stderr
, "cpuid_data is full, no space for "
2033 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
2036 c
= &cpuid_data
.entries
[cpuid_i
++];
2039 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2040 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2047 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2048 if (!c
->eax
&& !c
->ebx
&& !c
->ecx
&& !c
->edx
) {
2050 * KVM already returns all zeroes if a CPUID entry is missing,
2051 * so we can omit it and avoid hitting KVM's 80-entry limit.
2059 if (limit
>= 0x0a) {
2062 cpu_x86_cpuid(env
, 0x0a, 0, &eax
, &unused
, &unused
, &edx
);
2064 has_architectural_pmu_version
= eax
& 0xff;
2065 if (has_architectural_pmu_version
> 0) {
2066 num_architectural_pmu_gp_counters
= (eax
& 0xff00) >> 8;
2068 /* Shouldn't be more than 32, since that's the number of bits
2069 * available in EBX to tell us _which_ counters are available.
2072 if (num_architectural_pmu_gp_counters
> MAX_GP_COUNTERS
) {
2073 num_architectural_pmu_gp_counters
= MAX_GP_COUNTERS
;
2076 if (has_architectural_pmu_version
> 1) {
2077 num_architectural_pmu_fixed_counters
= edx
& 0x1f;
2079 if (num_architectural_pmu_fixed_counters
> MAX_FIXED_COUNTERS
) {
2080 num_architectural_pmu_fixed_counters
= MAX_FIXED_COUNTERS
;
2086 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
2088 for (i
= 0x80000000; i
<= limit
; i
++) {
2089 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
2090 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
2093 c
= &cpuid_data
.entries
[cpuid_i
++];
2097 /* Query for all AMD cache information leaves */
2098 for (j
= 0; ; j
++) {
2100 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
2102 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2107 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
2108 fprintf(stderr
, "cpuid_data is full, no space for "
2109 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
2112 c
= &cpuid_data
.entries
[cpuid_i
++];
2118 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2119 if (!c
->eax
&& !c
->ebx
&& !c
->ecx
&& !c
->edx
) {
2121 * KVM already returns all zeroes if a CPUID entry is missing,
2122 * so we can omit it and avoid hitting KVM's 80-entry limit.
2130 /* Call Centaur's CPUID instructions they are supported. */
2131 if (env
->cpuid_xlevel2
> 0) {
2132 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
2134 for (i
= 0xC0000000; i
<= limit
; i
++) {
2135 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
2136 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
2139 c
= &cpuid_data
.entries
[cpuid_i
++];
2143 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
2147 cpuid_data
.cpuid
.nent
= cpuid_i
;
2149 if (((env
->cpuid_version
>> 8)&0xF) >= 6
2150 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
2151 (CPUID_MCE
| CPUID_MCA
)
2152 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
2153 uint64_t mcg_cap
, unsupported_caps
;
2157 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
2159 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
2163 if (banks
< (env
->mcg_cap
& MCG_CAP_BANKS_MASK
)) {
2164 error_report("kvm: Unsupported MCE bank count (QEMU = %d, KVM = %d)",
2165 (int)(env
->mcg_cap
& MCG_CAP_BANKS_MASK
), banks
);
2169 unsupported_caps
= env
->mcg_cap
& ~(mcg_cap
| MCG_CAP_BANKS_MASK
);
2170 if (unsupported_caps
) {
2171 if (unsupported_caps
& MCG_LMCE_P
) {
2172 error_report("kvm: LMCE not supported");
2175 warn_report("Unsupported MCG_CAP bits: 0x%" PRIx64
,
2179 env
->mcg_cap
&= mcg_cap
| MCG_CAP_BANKS_MASK
;
2180 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &env
->mcg_cap
);
2182 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
2187 cpu
->vmsentry
= qemu_add_vm_change_state_handler(cpu_update_state
, env
);
2189 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 1, 0);
2191 has_msr_feature_control
= !!(c
->ecx
& CPUID_EXT_VMX
) ||
2192 !!(c
->ecx
& CPUID_EXT_SMX
);
2195 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 7, 0);
2196 if (c
&& (c
->ebx
& CPUID_7_0_EBX_SGX
)) {
2197 has_msr_feature_control
= true;
2200 if (env
->mcg_cap
& MCG_LMCE_P
) {
2201 has_msr_mcg_ext_ctl
= has_msr_feature_control
= true;
2204 if (!env
->user_tsc_khz
) {
2205 if ((env
->features
[FEAT_8000_0007_EDX
] & CPUID_APM_INVTSC
) &&
2206 invtsc_mig_blocker
== NULL
) {
2207 error_setg(&invtsc_mig_blocker
,
2208 "State blocked by non-migratable CPU device"
2210 r
= migrate_add_blocker(invtsc_mig_blocker
, &local_err
);
2212 error_report_err(local_err
);
2218 if (cpu
->vmware_cpuid_freq
2219 /* Guests depend on 0x40000000 to detect this feature, so only expose
2220 * it if KVM exposes leaf 0x40000000. (Conflicts with Hyper-V) */
2222 && kvm_base
== KVM_CPUID_SIGNATURE
2223 /* TSC clock must be stable and known for this feature. */
2224 && tsc_is_stable_and_known(env
)) {
2226 c
= &cpuid_data
.entries
[cpuid_i
++];
2227 c
->function
= KVM_CPUID_SIGNATURE
| 0x10;
2228 c
->eax
= env
->tsc_khz
;
2229 c
->ebx
= env
->apic_bus_freq
/ 1000; /* Hz to KHz */
2230 c
->ecx
= c
->edx
= 0;
2232 c
= cpuid_find_entry(&cpuid_data
.cpuid
, kvm_base
, 0);
2233 c
->eax
= MAX(c
->eax
, KVM_CPUID_SIGNATURE
| 0x10);
2236 cpuid_data
.cpuid
.nent
= cpuid_i
;
2238 cpuid_data
.cpuid
.padding
= 0;
2239 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
2243 kvm_init_xsave(env
);
2245 max_nested_state_len
= kvm_max_nested_state_length();
2246 if (max_nested_state_len
> 0) {
2247 assert(max_nested_state_len
>= offsetof(struct kvm_nested_state
, data
));
2249 if (cpu_has_vmx(env
) || cpu_has_svm(env
)) {
2250 env
->nested_state
= g_malloc0(max_nested_state_len
);
2251 env
->nested_state
->size
= max_nested_state_len
;
2253 kvm_init_nested_state(env
);
2257 cpu
->kvm_msr_buf
= g_malloc0(MSR_BUF_SIZE
);
2259 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_RDTSCP
)) {
2260 has_msr_tsc_aux
= false;
2268 migrate_del_blocker(invtsc_mig_blocker
);
2273 int kvm_arch_destroy_vcpu(CPUState
*cs
)
2275 X86CPU
*cpu
= X86_CPU(cs
);
2276 CPUX86State
*env
= &cpu
->env
;
2278 g_free(env
->xsave_buf
);
2280 g_free(cpu
->kvm_msr_buf
);
2281 cpu
->kvm_msr_buf
= NULL
;
2283 g_free(env
->nested_state
);
2284 env
->nested_state
= NULL
;
2286 qemu_del_vm_change_state_handler(cpu
->vmsentry
);
2291 void kvm_arch_reset_vcpu(X86CPU
*cpu
)
2293 CPUX86State
*env
= &cpu
->env
;
2296 if (kvm_irqchip_in_kernel()) {
2297 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
2298 KVM_MP_STATE_UNINITIALIZED
;
2300 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
2303 /* enabled by default */
2304 env
->poll_control_msr
= 1;
2306 kvm_init_nested_state(env
);
2308 sev_es_set_reset_vector(CPU(cpu
));
2311 void kvm_arch_after_reset_vcpu(X86CPU
*cpu
)
2313 CPUX86State
*env
= &cpu
->env
;
2317 * Reset SynIC after all other devices have been reset to let them remove
2318 * their SINT routes first.
2320 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
)) {
2321 for (i
= 0; i
< ARRAY_SIZE(env
->msr_hv_synic_sint
); i
++) {
2322 env
->msr_hv_synic_sint
[i
] = HV_SINT_MASKED
;
2325 hyperv_x86_synic_reset(cpu
);
2329 void kvm_arch_do_init_vcpu(X86CPU
*cpu
)
2331 CPUX86State
*env
= &cpu
->env
;
2333 /* APs get directly into wait-for-SIPI state. */
2334 if (env
->mp_state
== KVM_MP_STATE_UNINITIALIZED
) {
2335 env
->mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
2339 static int kvm_get_supported_feature_msrs(KVMState
*s
)
2343 if (kvm_feature_msrs
!= NULL
) {
2347 if (!kvm_check_extension(s
, KVM_CAP_GET_MSR_FEATURES
)) {
2351 struct kvm_msr_list msr_list
;
2354 ret
= kvm_ioctl(s
, KVM_GET_MSR_FEATURE_INDEX_LIST
, &msr_list
);
2355 if (ret
< 0 && ret
!= -E2BIG
) {
2356 error_report("Fetch KVM feature MSR list failed: %s",
2361 assert(msr_list
.nmsrs
> 0);
2362 kvm_feature_msrs
= g_malloc0(sizeof(msr_list
) +
2363 msr_list
.nmsrs
* sizeof(msr_list
.indices
[0]));
2365 kvm_feature_msrs
->nmsrs
= msr_list
.nmsrs
;
2366 ret
= kvm_ioctl(s
, KVM_GET_MSR_FEATURE_INDEX_LIST
, kvm_feature_msrs
);
2369 error_report("Fetch KVM feature MSR list failed: %s",
2371 g_free(kvm_feature_msrs
);
2372 kvm_feature_msrs
= NULL
;
2379 static int kvm_get_supported_msrs(KVMState
*s
)
2382 struct kvm_msr_list msr_list
, *kvm_msr_list
;
2385 * Obtain MSR list from KVM. These are the MSRs that we must
2389 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
2390 if (ret
< 0 && ret
!= -E2BIG
) {
2394 * Old kernel modules had a bug and could write beyond the provided
2395 * memory. Allocate at least a safe amount of 1K.
2397 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
2399 sizeof(msr_list
.indices
[0])));
2401 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
2402 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
2406 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
2407 switch (kvm_msr_list
->indices
[i
]) {
2409 has_msr_star
= true;
2411 case MSR_VM_HSAVE_PA
:
2412 has_msr_hsave_pa
= true;
2415 has_msr_tsc_aux
= true;
2417 case MSR_TSC_ADJUST
:
2418 has_msr_tsc_adjust
= true;
2420 case MSR_IA32_TSCDEADLINE
:
2421 has_msr_tsc_deadline
= true;
2423 case MSR_IA32_SMBASE
:
2424 has_msr_smbase
= true;
2427 has_msr_smi_count
= true;
2429 case MSR_IA32_MISC_ENABLE
:
2430 has_msr_misc_enable
= true;
2432 case MSR_IA32_BNDCFGS
:
2433 has_msr_bndcfgs
= true;
2438 case MSR_IA32_UMWAIT_CONTROL
:
2439 has_msr_umwait
= true;
2441 case HV_X64_MSR_CRASH_CTL
:
2442 has_msr_hv_crash
= true;
2444 case HV_X64_MSR_RESET
:
2445 has_msr_hv_reset
= true;
2447 case HV_X64_MSR_VP_INDEX
:
2448 has_msr_hv_vpindex
= true;
2450 case HV_X64_MSR_VP_RUNTIME
:
2451 has_msr_hv_runtime
= true;
2453 case HV_X64_MSR_SCONTROL
:
2454 has_msr_hv_synic
= true;
2456 case HV_X64_MSR_STIMER0_CONFIG
:
2457 has_msr_hv_stimer
= true;
2459 case HV_X64_MSR_TSC_FREQUENCY
:
2460 has_msr_hv_frequencies
= true;
2462 case HV_X64_MSR_REENLIGHTENMENT_CONTROL
:
2463 has_msr_hv_reenlightenment
= true;
2465 case HV_X64_MSR_SYNDBG_OPTIONS
:
2466 has_msr_hv_syndbg_options
= true;
2468 case MSR_IA32_SPEC_CTRL
:
2469 has_msr_spec_ctrl
= true;
2471 case MSR_AMD64_TSC_RATIO
:
2472 has_tsc_scale_msr
= true;
2474 case MSR_IA32_TSX_CTRL
:
2475 has_msr_tsx_ctrl
= true;
2478 has_msr_virt_ssbd
= true;
2480 case MSR_IA32_ARCH_CAPABILITIES
:
2481 has_msr_arch_capabs
= true;
2483 case MSR_IA32_CORE_CAPABILITY
:
2484 has_msr_core_capabs
= true;
2486 case MSR_IA32_PERF_CAPABILITIES
:
2487 has_msr_perf_capabs
= true;
2489 case MSR_IA32_VMX_VMFUNC
:
2490 has_msr_vmx_vmfunc
= true;
2492 case MSR_IA32_UCODE_REV
:
2493 has_msr_ucode_rev
= true;
2495 case MSR_IA32_VMX_PROCBASED_CTLS2
:
2496 has_msr_vmx_procbased_ctls2
= true;
2499 has_msr_pkrs
= true;
2505 g_free(kvm_msr_list
);
2510 static bool kvm_rdmsr_core_thread_count(X86CPU
*cpu
, uint32_t msr
,
2513 CPUState
*cs
= CPU(cpu
);
2515 *val
= cs
->nr_threads
* cs
->nr_cores
; /* thread count, bits 15..0 */
2516 *val
|= ((uint32_t)cs
->nr_cores
<< 16); /* core count, bits 31..16 */
2521 static Notifier smram_machine_done
;
2522 static KVMMemoryListener smram_listener
;
2523 static AddressSpace smram_address_space
;
2524 static MemoryRegion smram_as_root
;
2525 static MemoryRegion smram_as_mem
;
2527 static void register_smram_listener(Notifier
*n
, void *unused
)
2529 MemoryRegion
*smram
=
2530 (MemoryRegion
*) object_resolve_path("/machine/smram", NULL
);
2532 /* Outer container... */
2533 memory_region_init(&smram_as_root
, OBJECT(kvm_state
), "mem-container-smram", ~0ull);
2534 memory_region_set_enabled(&smram_as_root
, true);
2536 /* ... with two regions inside: normal system memory with low
2539 memory_region_init_alias(&smram_as_mem
, OBJECT(kvm_state
), "mem-smram",
2540 get_system_memory(), 0, ~0ull);
2541 memory_region_add_subregion_overlap(&smram_as_root
, 0, &smram_as_mem
, 0);
2542 memory_region_set_enabled(&smram_as_mem
, true);
2545 /* ... SMRAM with higher priority */
2546 memory_region_add_subregion_overlap(&smram_as_root
, 0, smram
, 10);
2547 memory_region_set_enabled(smram
, true);
2550 address_space_init(&smram_address_space
, &smram_as_root
, "KVM-SMRAM");
2551 kvm_memory_listener_register(kvm_state
, &smram_listener
,
2552 &smram_address_space
, 1, "kvm-smram");
2555 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
2557 uint64_t identity_base
= 0xfffbc000;
2558 uint64_t shadow_mem
;
2560 struct utsname utsname
;
2561 Error
*local_err
= NULL
;
2564 * Initialize SEV context, if required
2566 * If no memory encryption is requested (ms->cgs == NULL) this is
2569 * It's also a no-op if a non-SEV confidential guest support
2570 * mechanism is selected. SEV is the only mechanism available to
2571 * select on x86 at present, so this doesn't arise, but if new
2572 * mechanisms are supported in future (e.g. TDX), they'll need
2573 * their own initialization either here or elsewhere.
2575 ret
= sev_kvm_init(ms
->cgs
, &local_err
);
2577 error_report_err(local_err
);
2581 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2582 error_report("kvm: KVM_CAP_IRQ_ROUTING not supported by KVM");
2586 has_xsave
= kvm_check_extension(s
, KVM_CAP_XSAVE
);
2587 has_xcrs
= kvm_check_extension(s
, KVM_CAP_XCRS
);
2588 has_pit_state2
= kvm_check_extension(s
, KVM_CAP_PIT_STATE2
);
2589 has_sregs2
= kvm_check_extension(s
, KVM_CAP_SREGS2
) > 0;
2591 hv_vpindex_settable
= kvm_check_extension(s
, KVM_CAP_HYPERV_VP_INDEX
);
2593 has_exception_payload
= kvm_check_extension(s
, KVM_CAP_EXCEPTION_PAYLOAD
);
2594 if (has_exception_payload
) {
2595 ret
= kvm_vm_enable_cap(s
, KVM_CAP_EXCEPTION_PAYLOAD
, 0, true);
2597 error_report("kvm: Failed to enable exception payload cap: %s",
2603 has_triple_fault_event
= kvm_check_extension(s
, KVM_CAP_X86_TRIPLE_FAULT_EVENT
);
2604 if (has_triple_fault_event
) {
2605 ret
= kvm_vm_enable_cap(s
, KVM_CAP_X86_TRIPLE_FAULT_EVENT
, 0, true);
2607 error_report("kvm: Failed to enable triple fault event cap: %s",
2613 if (s
->xen_version
) {
2614 #ifdef CONFIG_XEN_EMU
2615 if (!object_dynamic_cast(OBJECT(ms
), TYPE_PC_MACHINE
)) {
2616 error_report("kvm: Xen support only available in PC machine");
2619 /* hyperv_enabled() doesn't work yet. */
2620 uint32_t msr
= XEN_HYPERCALL_MSR
;
2621 ret
= kvm_xen_init(s
, msr
);
2626 error_report("kvm: Xen support not enabled in qemu");
2631 ret
= kvm_get_supported_msrs(s
);
2636 kvm_get_supported_feature_msrs(s
);
2639 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
2642 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
2643 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
2644 * Since these must be part of guest physical memory, we need to allocate
2645 * them, both by setting their start addresses in the kernel and by
2646 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
2648 * Older KVM versions may not support setting the identity map base. In
2649 * that case we need to stick with the default, i.e. a 256K maximum BIOS
2652 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
2653 /* Allows up to 16M BIOSes. */
2654 identity_base
= 0xfeffc000;
2656 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
2662 /* Set TSS base one page after EPT identity map. */
2663 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
2668 /* Tell fw_cfg to notify the BIOS to reserve the range. */
2669 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
2671 fprintf(stderr
, "e820_add_entry() table is full\n");
2675 shadow_mem
= object_property_get_int(OBJECT(s
), "kvm-shadow-mem", &error_abort
);
2676 if (shadow_mem
!= -1) {
2678 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
2684 if (kvm_check_extension(s
, KVM_CAP_X86_SMM
) &&
2685 object_dynamic_cast(OBJECT(ms
), TYPE_X86_MACHINE
) &&
2686 x86_machine_is_smm_enabled(X86_MACHINE(ms
))) {
2687 smram_machine_done
.notify
= register_smram_listener
;
2688 qemu_add_machine_init_done_notifier(&smram_machine_done
);
2691 if (enable_cpu_pm
) {
2692 int disable_exits
= kvm_check_extension(s
, KVM_CAP_X86_DISABLE_EXITS
);
2695 /* Work around for kernel header with a typo. TODO: fix header and drop. */
2696 #if defined(KVM_X86_DISABLE_EXITS_HTL) && !defined(KVM_X86_DISABLE_EXITS_HLT)
2697 #define KVM_X86_DISABLE_EXITS_HLT KVM_X86_DISABLE_EXITS_HTL
2699 if (disable_exits
) {
2700 disable_exits
&= (KVM_X86_DISABLE_EXITS_MWAIT
|
2701 KVM_X86_DISABLE_EXITS_HLT
|
2702 KVM_X86_DISABLE_EXITS_PAUSE
|
2703 KVM_X86_DISABLE_EXITS_CSTATE
);
2706 ret
= kvm_vm_enable_cap(s
, KVM_CAP_X86_DISABLE_EXITS
, 0,
2709 error_report("kvm: guest stopping CPU not supported: %s",
2714 if (object_dynamic_cast(OBJECT(ms
), TYPE_X86_MACHINE
)) {
2715 X86MachineState
*x86ms
= X86_MACHINE(ms
);
2717 if (x86ms
->bus_lock_ratelimit
> 0) {
2718 ret
= kvm_check_extension(s
, KVM_CAP_X86_BUS_LOCK_EXIT
);
2719 if (!(ret
& KVM_BUS_LOCK_DETECTION_EXIT
)) {
2720 error_report("kvm: bus lock detection unsupported");
2723 ret
= kvm_vm_enable_cap(s
, KVM_CAP_X86_BUS_LOCK_EXIT
, 0,
2724 KVM_BUS_LOCK_DETECTION_EXIT
);
2726 error_report("kvm: Failed to enable bus lock detection cap: %s",
2730 ratelimit_init(&bus_lock_ratelimit_ctrl
);
2731 ratelimit_set_speed(&bus_lock_ratelimit_ctrl
,
2732 x86ms
->bus_lock_ratelimit
, BUS_LOCK_SLICE_TIME
);
2736 if (s
->notify_vmexit
!= NOTIFY_VMEXIT_OPTION_DISABLE
&&
2737 kvm_check_extension(s
, KVM_CAP_X86_NOTIFY_VMEXIT
)) {
2738 uint64_t notify_window_flags
=
2739 ((uint64_t)s
->notify_window
<< 32) |
2740 KVM_X86_NOTIFY_VMEXIT_ENABLED
|
2741 KVM_X86_NOTIFY_VMEXIT_USER
;
2742 ret
= kvm_vm_enable_cap(s
, KVM_CAP_X86_NOTIFY_VMEXIT
, 0,
2743 notify_window_flags
);
2745 error_report("kvm: Failed to enable notify vmexit cap: %s",
2750 if (kvm_vm_check_extension(s
, KVM_CAP_X86_USER_SPACE_MSR
)) {
2753 ret
= kvm_vm_enable_cap(s
, KVM_CAP_X86_USER_SPACE_MSR
, 0,
2754 KVM_MSR_EXIT_REASON_FILTER
);
2756 error_report("Could not enable user space MSRs: %s",
2761 r
= kvm_filter_msr(s
, MSR_CORE_THREAD_COUNT
,
2762 kvm_rdmsr_core_thread_count
, NULL
);
2764 error_report("Could not install MSR_CORE_THREAD_COUNT handler: %s",
2773 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
2775 lhs
->selector
= rhs
->selector
;
2776 lhs
->base
= rhs
->base
;
2777 lhs
->limit
= rhs
->limit
;
2789 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
2791 unsigned flags
= rhs
->flags
;
2792 lhs
->selector
= rhs
->selector
;
2793 lhs
->base
= rhs
->base
;
2794 lhs
->limit
= rhs
->limit
;
2795 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
2796 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
2797 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
2798 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
2799 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
2800 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
2801 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
2802 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
2803 lhs
->unusable
= !lhs
->present
;
2807 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
2809 lhs
->selector
= rhs
->selector
;
2810 lhs
->base
= rhs
->base
;
2811 lhs
->limit
= rhs
->limit
;
2812 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
2813 ((rhs
->present
&& !rhs
->unusable
) * DESC_P_MASK
) |
2814 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
2815 (rhs
->db
<< DESC_B_SHIFT
) |
2816 (rhs
->s
* DESC_S_MASK
) |
2817 (rhs
->l
<< DESC_L_SHIFT
) |
2818 (rhs
->g
* DESC_G_MASK
) |
2819 (rhs
->avl
* DESC_AVL_MASK
);
2822 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
2825 *kvm_reg
= *qemu_reg
;
2827 *qemu_reg
= *kvm_reg
;
2831 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
2833 CPUX86State
*env
= &cpu
->env
;
2834 struct kvm_regs regs
;
2838 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
2844 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
2845 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
2846 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
2847 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
2848 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
2849 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
2850 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
2851 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
2852 #ifdef TARGET_X86_64
2853 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
2854 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
2855 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
2856 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
2857 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
2858 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
2859 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
2860 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
2863 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
2864 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
2867 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
2873 static int kvm_put_fpu(X86CPU
*cpu
)
2875 CPUX86State
*env
= &cpu
->env
;
2879 memset(&fpu
, 0, sizeof fpu
);
2880 fpu
.fsw
= env
->fpus
& ~(7 << 11);
2881 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
2882 fpu
.fcw
= env
->fpuc
;
2883 fpu
.last_opcode
= env
->fpop
;
2884 fpu
.last_ip
= env
->fpip
;
2885 fpu
.last_dp
= env
->fpdp
;
2886 for (i
= 0; i
< 8; ++i
) {
2887 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
2889 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
2890 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
2891 stq_p(&fpu
.xmm
[i
][0], env
->xmm_regs
[i
].ZMM_Q(0));
2892 stq_p(&fpu
.xmm
[i
][8], env
->xmm_regs
[i
].ZMM_Q(1));
2894 fpu
.mxcsr
= env
->mxcsr
;
2896 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
2899 static int kvm_put_xsave(X86CPU
*cpu
)
2901 CPUX86State
*env
= &cpu
->env
;
2902 void *xsave
= env
->xsave_buf
;
2905 return kvm_put_fpu(cpu
);
2907 x86_cpu_xsave_all_areas(cpu
, xsave
, env
->xsave_buf_len
);
2909 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
2912 static int kvm_put_xcrs(X86CPU
*cpu
)
2914 CPUX86State
*env
= &cpu
->env
;
2915 struct kvm_xcrs xcrs
= {};
2923 xcrs
.xcrs
[0].xcr
= 0;
2924 xcrs
.xcrs
[0].value
= env
->xcr0
;
2925 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
2928 static int kvm_put_sregs(X86CPU
*cpu
)
2930 CPUX86State
*env
= &cpu
->env
;
2931 struct kvm_sregs sregs
;
2934 * The interrupt_bitmap is ignored because KVM_SET_SREGS is
2935 * always followed by KVM_SET_VCPU_EVENTS.
2937 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
2939 if ((env
->eflags
& VM_MASK
)) {
2940 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
2941 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
2942 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
2943 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
2944 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
2945 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
2947 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
2948 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
2949 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
2950 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
2951 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
2952 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
2955 set_seg(&sregs
.tr
, &env
->tr
);
2956 set_seg(&sregs
.ldt
, &env
->ldt
);
2958 sregs
.idt
.limit
= env
->idt
.limit
;
2959 sregs
.idt
.base
= env
->idt
.base
;
2960 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
2961 sregs
.gdt
.limit
= env
->gdt
.limit
;
2962 sregs
.gdt
.base
= env
->gdt
.base
;
2963 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
2965 sregs
.cr0
= env
->cr
[0];
2966 sregs
.cr2
= env
->cr
[2];
2967 sregs
.cr3
= env
->cr
[3];
2968 sregs
.cr4
= env
->cr
[4];
2970 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
2971 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
2973 sregs
.efer
= env
->efer
;
2975 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
2978 static int kvm_put_sregs2(X86CPU
*cpu
)
2980 CPUX86State
*env
= &cpu
->env
;
2981 struct kvm_sregs2 sregs
;
2986 if ((env
->eflags
& VM_MASK
)) {
2987 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
2988 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
2989 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
2990 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
2991 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
2992 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
2994 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
2995 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
2996 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
2997 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
2998 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
2999 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
3002 set_seg(&sregs
.tr
, &env
->tr
);
3003 set_seg(&sregs
.ldt
, &env
->ldt
);
3005 sregs
.idt
.limit
= env
->idt
.limit
;
3006 sregs
.idt
.base
= env
->idt
.base
;
3007 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
3008 sregs
.gdt
.limit
= env
->gdt
.limit
;
3009 sregs
.gdt
.base
= env
->gdt
.base
;
3010 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
3012 sregs
.cr0
= env
->cr
[0];
3013 sregs
.cr2
= env
->cr
[2];
3014 sregs
.cr3
= env
->cr
[3];
3015 sregs
.cr4
= env
->cr
[4];
3017 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
3018 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
3020 sregs
.efer
= env
->efer
;
3022 if (env
->pdptrs_valid
) {
3023 for (i
= 0; i
< 4; i
++) {
3024 sregs
.pdptrs
[i
] = env
->pdptrs
[i
];
3026 sregs
.flags
|= KVM_SREGS2_FLAGS_PDPTRS_VALID
;
3029 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS2
, &sregs
);
3033 static void kvm_msr_buf_reset(X86CPU
*cpu
)
3035 memset(cpu
->kvm_msr_buf
, 0, MSR_BUF_SIZE
);
3038 static void kvm_msr_entry_add(X86CPU
*cpu
, uint32_t index
, uint64_t value
)
3040 struct kvm_msrs
*msrs
= cpu
->kvm_msr_buf
;
3041 void *limit
= ((void *)msrs
) + MSR_BUF_SIZE
;
3042 struct kvm_msr_entry
*entry
= &msrs
->entries
[msrs
->nmsrs
];
3044 assert((void *)(entry
+ 1) <= limit
);
3046 entry
->index
= index
;
3047 entry
->reserved
= 0;
3048 entry
->data
= value
;
3052 static int kvm_put_one_msr(X86CPU
*cpu
, int index
, uint64_t value
)
3054 kvm_msr_buf_reset(cpu
);
3055 kvm_msr_entry_add(cpu
, index
, value
);
3057 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, cpu
->kvm_msr_buf
);
3060 static int kvm_get_one_msr(X86CPU
*cpu
, int index
, uint64_t *value
)
3064 struct kvm_msrs info
;
3065 struct kvm_msr_entry entries
[1];
3068 .entries
[0].index
= index
,
3071 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
3076 *value
= msr_data
.entries
[0].data
;
3079 void kvm_put_apicbase(X86CPU
*cpu
, uint64_t value
)
3083 ret
= kvm_put_one_msr(cpu
, MSR_IA32_APICBASE
, value
);
3087 static int kvm_put_tscdeadline_msr(X86CPU
*cpu
)
3089 CPUX86State
*env
= &cpu
->env
;
3092 if (!has_msr_tsc_deadline
) {
3096 ret
= kvm_put_one_msr(cpu
, MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
3106 * Provide a separate write service for the feature control MSR in order to
3107 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
3108 * before writing any other state because forcibly leaving nested mode
3109 * invalidates the VCPU state.
3111 static int kvm_put_msr_feature_control(X86CPU
*cpu
)
3115 if (!has_msr_feature_control
) {
3119 ret
= kvm_put_one_msr(cpu
, MSR_IA32_FEATURE_CONTROL
,
3120 cpu
->env
.msr_ia32_feature_control
);
3129 static uint64_t make_vmx_msr_value(uint32_t index
, uint32_t features
)
3131 uint32_t default1
, can_be_one
, can_be_zero
;
3132 uint32_t must_be_one
;
3135 case MSR_IA32_VMX_TRUE_PINBASED_CTLS
:
3136 default1
= 0x00000016;
3138 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS
:
3139 default1
= 0x0401e172;
3141 case MSR_IA32_VMX_TRUE_ENTRY_CTLS
:
3142 default1
= 0x000011ff;
3144 case MSR_IA32_VMX_TRUE_EXIT_CTLS
:
3145 default1
= 0x00036dff;
3147 case MSR_IA32_VMX_PROCBASED_CTLS2
:
3154 /* If a feature bit is set, the control can be either set or clear.
3155 * Otherwise the value is limited to either 0 or 1 by default1.
3157 can_be_one
= features
| default1
;
3158 can_be_zero
= features
| ~default1
;
3159 must_be_one
= ~can_be_zero
;
3162 * Bit 0:31 -> 0 if the control bit can be zero (i.e. 1 if it must be one).
3163 * Bit 32:63 -> 1 if the control bit can be one.
3165 return must_be_one
| (((uint64_t)can_be_one
) << 32);
3168 static void kvm_msr_entry_add_vmx(X86CPU
*cpu
, FeatureWordArray f
)
3170 uint64_t kvm_vmx_basic
=
3171 kvm_arch_get_supported_msr_feature(kvm_state
,
3172 MSR_IA32_VMX_BASIC
);
3174 if (!kvm_vmx_basic
) {
3175 /* If the kernel doesn't support VMX feature (kvm_intel.nested=0),
3176 * then kvm_vmx_basic will be 0 and KVM_SET_MSR will fail.
3181 uint64_t kvm_vmx_misc
=
3182 kvm_arch_get_supported_msr_feature(kvm_state
,
3184 uint64_t kvm_vmx_ept_vpid
=
3185 kvm_arch_get_supported_msr_feature(kvm_state
,
3186 MSR_IA32_VMX_EPT_VPID_CAP
);
3189 * If the guest is 64-bit, a value of 1 is allowed for the host address
3190 * space size vmexit control.
3192 uint64_t fixed_vmx_exit
= f
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
3193 ? (uint64_t)VMX_VM_EXIT_HOST_ADDR_SPACE_SIZE
<< 32 : 0;
3196 * Bits 0-30, 32-44 and 50-53 come from the host. KVM should
3197 * not change them for backwards compatibility.
3199 uint64_t fixed_vmx_basic
= kvm_vmx_basic
&
3200 (MSR_VMX_BASIC_VMCS_REVISION_MASK
|
3201 MSR_VMX_BASIC_VMXON_REGION_SIZE_MASK
|
3202 MSR_VMX_BASIC_VMCS_MEM_TYPE_MASK
);
3205 * Same for bits 0-4 and 25-27. Bits 16-24 (CR3 target count) can
3206 * change in the future but are always zero for now, clear them to be
3207 * future proof. Bits 32-63 in theory could change, though KVM does
3208 * not support dual-monitor treatment and probably never will; mask
3211 uint64_t fixed_vmx_misc
= kvm_vmx_misc
&
3212 (MSR_VMX_MISC_PREEMPTION_TIMER_SHIFT_MASK
|
3213 MSR_VMX_MISC_MAX_MSR_LIST_SIZE_MASK
);
3216 * EPT memory types should not change either, so we do not bother
3217 * adding features for them.
3219 uint64_t fixed_vmx_ept_mask
=
3220 (f
[FEAT_VMX_SECONDARY_CTLS
] & VMX_SECONDARY_EXEC_ENABLE_EPT
?
3221 MSR_VMX_EPT_UC
| MSR_VMX_EPT_WB
: 0);
3222 uint64_t fixed_vmx_ept_vpid
= kvm_vmx_ept_vpid
& fixed_vmx_ept_mask
;
3224 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_TRUE_PROCBASED_CTLS
,
3225 make_vmx_msr_value(MSR_IA32_VMX_TRUE_PROCBASED_CTLS
,
3226 f
[FEAT_VMX_PROCBASED_CTLS
]));
3227 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_TRUE_PINBASED_CTLS
,
3228 make_vmx_msr_value(MSR_IA32_VMX_TRUE_PINBASED_CTLS
,
3229 f
[FEAT_VMX_PINBASED_CTLS
]));
3230 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_TRUE_EXIT_CTLS
,
3231 make_vmx_msr_value(MSR_IA32_VMX_TRUE_EXIT_CTLS
,
3232 f
[FEAT_VMX_EXIT_CTLS
]) | fixed_vmx_exit
);
3233 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_TRUE_ENTRY_CTLS
,
3234 make_vmx_msr_value(MSR_IA32_VMX_TRUE_ENTRY_CTLS
,
3235 f
[FEAT_VMX_ENTRY_CTLS
]));
3236 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_PROCBASED_CTLS2
,
3237 make_vmx_msr_value(MSR_IA32_VMX_PROCBASED_CTLS2
,
3238 f
[FEAT_VMX_SECONDARY_CTLS
]));
3239 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_EPT_VPID_CAP
,
3240 f
[FEAT_VMX_EPT_VPID_CAPS
] | fixed_vmx_ept_vpid
);
3241 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_BASIC
,
3242 f
[FEAT_VMX_BASIC
] | fixed_vmx_basic
);
3243 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_MISC
,
3244 f
[FEAT_VMX_MISC
] | fixed_vmx_misc
);
3245 if (has_msr_vmx_vmfunc
) {
3246 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_VMFUNC
, f
[FEAT_VMX_VMFUNC
]);
3250 * Just to be safe, write these with constant values. The CRn_FIXED1
3251 * MSRs are generated by KVM based on the vCPU's CPUID.
3253 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_CR0_FIXED0
,
3254 CR0_PE_MASK
| CR0_PG_MASK
| CR0_NE_MASK
);
3255 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_CR4_FIXED0
,
3258 if (f
[FEAT_VMX_SECONDARY_CTLS
] & VMX_SECONDARY_EXEC_TSC_SCALING
) {
3259 /* TSC multiplier (0x2032). */
3260 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_VMCS_ENUM
, 0x32);
3262 /* Preemption timer (0x482E). */
3263 kvm_msr_entry_add(cpu
, MSR_IA32_VMX_VMCS_ENUM
, 0x2E);
3267 static void kvm_msr_entry_add_perf(X86CPU
*cpu
, FeatureWordArray f
)
3269 uint64_t kvm_perf_cap
=
3270 kvm_arch_get_supported_msr_feature(kvm_state
,
3271 MSR_IA32_PERF_CAPABILITIES
);
3274 kvm_msr_entry_add(cpu
, MSR_IA32_PERF_CAPABILITIES
,
3275 kvm_perf_cap
& f
[FEAT_PERF_CAPABILITIES
]);
3279 static int kvm_buf_set_msrs(X86CPU
*cpu
)
3281 int ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, cpu
->kvm_msr_buf
);
3286 if (ret
< cpu
->kvm_msr_buf
->nmsrs
) {
3287 struct kvm_msr_entry
*e
= &cpu
->kvm_msr_buf
->entries
[ret
];
3288 error_report("error: failed to set MSR 0x%" PRIx32
" to 0x%" PRIx64
,
3289 (uint32_t)e
->index
, (uint64_t)e
->data
);
3292 assert(ret
== cpu
->kvm_msr_buf
->nmsrs
);
3296 static void kvm_init_msrs(X86CPU
*cpu
)
3298 CPUX86State
*env
= &cpu
->env
;
3300 kvm_msr_buf_reset(cpu
);
3301 if (has_msr_arch_capabs
) {
3302 kvm_msr_entry_add(cpu
, MSR_IA32_ARCH_CAPABILITIES
,
3303 env
->features
[FEAT_ARCH_CAPABILITIES
]);
3306 if (has_msr_core_capabs
) {
3307 kvm_msr_entry_add(cpu
, MSR_IA32_CORE_CAPABILITY
,
3308 env
->features
[FEAT_CORE_CAPABILITY
]);
3311 if (has_msr_perf_capabs
&& cpu
->enable_pmu
) {
3312 kvm_msr_entry_add_perf(cpu
, env
->features
);
3315 if (has_msr_ucode_rev
) {
3316 kvm_msr_entry_add(cpu
, MSR_IA32_UCODE_REV
, cpu
->ucode_rev
);
3320 * Older kernels do not include VMX MSRs in KVM_GET_MSR_INDEX_LIST, but
3321 * all kernels with MSR features should have them.
3323 if (kvm_feature_msrs
&& cpu_has_vmx(env
)) {
3324 kvm_msr_entry_add_vmx(cpu
, env
->features
);
3327 assert(kvm_buf_set_msrs(cpu
) == 0);
3330 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
3332 CPUX86State
*env
= &cpu
->env
;
3335 kvm_msr_buf_reset(cpu
);
3337 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
3338 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
3339 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
3340 kvm_msr_entry_add(cpu
, MSR_PAT
, env
->pat
);
3342 kvm_msr_entry_add(cpu
, MSR_STAR
, env
->star
);
3344 if (has_msr_hsave_pa
) {
3345 kvm_msr_entry_add(cpu
, MSR_VM_HSAVE_PA
, env
->vm_hsave
);
3347 if (has_msr_tsc_aux
) {
3348 kvm_msr_entry_add(cpu
, MSR_TSC_AUX
, env
->tsc_aux
);
3350 if (has_msr_tsc_adjust
) {
3351 kvm_msr_entry_add(cpu
, MSR_TSC_ADJUST
, env
->tsc_adjust
);
3353 if (has_msr_misc_enable
) {
3354 kvm_msr_entry_add(cpu
, MSR_IA32_MISC_ENABLE
,
3355 env
->msr_ia32_misc_enable
);
3357 if (has_msr_smbase
) {
3358 kvm_msr_entry_add(cpu
, MSR_IA32_SMBASE
, env
->smbase
);
3360 if (has_msr_smi_count
) {
3361 kvm_msr_entry_add(cpu
, MSR_SMI_COUNT
, env
->msr_smi_count
);
3364 kvm_msr_entry_add(cpu
, MSR_IA32_PKRS
, env
->pkrs
);
3366 if (has_msr_bndcfgs
) {
3367 kvm_msr_entry_add(cpu
, MSR_IA32_BNDCFGS
, env
->msr_bndcfgs
);
3370 kvm_msr_entry_add(cpu
, MSR_IA32_XSS
, env
->xss
);
3372 if (has_msr_umwait
) {
3373 kvm_msr_entry_add(cpu
, MSR_IA32_UMWAIT_CONTROL
, env
->umwait
);
3375 if (has_msr_spec_ctrl
) {
3376 kvm_msr_entry_add(cpu
, MSR_IA32_SPEC_CTRL
, env
->spec_ctrl
);
3378 if (has_tsc_scale_msr
) {
3379 kvm_msr_entry_add(cpu
, MSR_AMD64_TSC_RATIO
, env
->amd_tsc_scale_msr
);
3382 if (has_msr_tsx_ctrl
) {
3383 kvm_msr_entry_add(cpu
, MSR_IA32_TSX_CTRL
, env
->tsx_ctrl
);
3385 if (has_msr_virt_ssbd
) {
3386 kvm_msr_entry_add(cpu
, MSR_VIRT_SSBD
, env
->virt_ssbd
);
3389 #ifdef TARGET_X86_64
3390 if (lm_capable_kernel
) {
3391 kvm_msr_entry_add(cpu
, MSR_CSTAR
, env
->cstar
);
3392 kvm_msr_entry_add(cpu
, MSR_KERNELGSBASE
, env
->kernelgsbase
);
3393 kvm_msr_entry_add(cpu
, MSR_FMASK
, env
->fmask
);
3394 kvm_msr_entry_add(cpu
, MSR_LSTAR
, env
->lstar
);
3399 * The following MSRs have side effects on the guest or are too heavy
3400 * for normal writeback. Limit them to reset or full state updates.
3402 if (level
>= KVM_PUT_RESET_STATE
) {
3403 kvm_msr_entry_add(cpu
, MSR_IA32_TSC
, env
->tsc
);
3404 kvm_msr_entry_add(cpu
, MSR_KVM_SYSTEM_TIME
, env
->system_time_msr
);
3405 kvm_msr_entry_add(cpu
, MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
3406 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF_INT
)) {
3407 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_INT
, env
->async_pf_int_msr
);
3409 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF
)) {
3410 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_EN
, env
->async_pf_en_msr
);
3412 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_PV_EOI
)) {
3413 kvm_msr_entry_add(cpu
, MSR_KVM_PV_EOI_EN
, env
->pv_eoi_en_msr
);
3415 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_STEAL_TIME
)) {
3416 kvm_msr_entry_add(cpu
, MSR_KVM_STEAL_TIME
, env
->steal_time_msr
);
3419 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_POLL_CONTROL
)) {
3420 kvm_msr_entry_add(cpu
, MSR_KVM_POLL_CONTROL
, env
->poll_control_msr
);
3423 if (has_architectural_pmu_version
> 0) {
3424 if (has_architectural_pmu_version
> 1) {
3425 /* Stop the counter. */
3426 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
3427 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
, 0);
3430 /* Set the counter values. */
3431 for (i
= 0; i
< num_architectural_pmu_fixed_counters
; i
++) {
3432 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR0
+ i
,
3433 env
->msr_fixed_counters
[i
]);
3435 for (i
= 0; i
< num_architectural_pmu_gp_counters
; i
++) {
3436 kvm_msr_entry_add(cpu
, MSR_P6_PERFCTR0
+ i
,
3437 env
->msr_gp_counters
[i
]);
3438 kvm_msr_entry_add(cpu
, MSR_P6_EVNTSEL0
+ i
,
3439 env
->msr_gp_evtsel
[i
]);
3441 if (has_architectural_pmu_version
> 1) {
3442 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_STATUS
,
3443 env
->msr_global_status
);
3444 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_OVF_CTRL
,
3445 env
->msr_global_ovf_ctrl
);
3447 /* Now start the PMU. */
3448 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
,
3449 env
->msr_fixed_ctr_ctrl
);
3450 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
,
3451 env
->msr_global_ctrl
);
3455 * Hyper-V partition-wide MSRs: to avoid clearing them on cpu hot-add,
3456 * only sync them to KVM on the first cpu
3458 if (current_cpu
== first_cpu
) {
3459 if (has_msr_hv_hypercall
) {
3460 kvm_msr_entry_add(cpu
, HV_X64_MSR_GUEST_OS_ID
,
3461 env
->msr_hv_guest_os_id
);
3462 kvm_msr_entry_add(cpu
, HV_X64_MSR_HYPERCALL
,
3463 env
->msr_hv_hypercall
);
3465 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_TIME
)) {
3466 kvm_msr_entry_add(cpu
, HV_X64_MSR_REFERENCE_TSC
,
3469 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_REENLIGHTENMENT
)) {
3470 kvm_msr_entry_add(cpu
, HV_X64_MSR_REENLIGHTENMENT_CONTROL
,
3471 env
->msr_hv_reenlightenment_control
);
3472 kvm_msr_entry_add(cpu
, HV_X64_MSR_TSC_EMULATION_CONTROL
,
3473 env
->msr_hv_tsc_emulation_control
);
3474 kvm_msr_entry_add(cpu
, HV_X64_MSR_TSC_EMULATION_STATUS
,
3475 env
->msr_hv_tsc_emulation_status
);
3477 #ifdef CONFIG_SYNDBG
3478 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNDBG
) &&
3479 has_msr_hv_syndbg_options
) {
3480 kvm_msr_entry_add(cpu
, HV_X64_MSR_SYNDBG_OPTIONS
,
3481 hyperv_syndbg_query_options());
3485 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VAPIC
)) {
3486 kvm_msr_entry_add(cpu
, HV_X64_MSR_APIC_ASSIST_PAGE
,
3489 if (has_msr_hv_crash
) {
3492 for (j
= 0; j
< HV_CRASH_PARAMS
; j
++)
3493 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_P0
+ j
,
3494 env
->msr_hv_crash_params
[j
]);
3496 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_CTL
, HV_CRASH_CTL_NOTIFY
);
3498 if (has_msr_hv_runtime
) {
3499 kvm_msr_entry_add(cpu
, HV_X64_MSR_VP_RUNTIME
, env
->msr_hv_runtime
);
3501 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VPINDEX
)
3502 && hv_vpindex_settable
) {
3503 kvm_msr_entry_add(cpu
, HV_X64_MSR_VP_INDEX
,
3504 hyperv_vp_index(CPU(cpu
)));
3506 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
)) {
3509 kvm_msr_entry_add(cpu
, HV_X64_MSR_SVERSION
, HV_SYNIC_VERSION
);
3511 kvm_msr_entry_add(cpu
, HV_X64_MSR_SCONTROL
,
3512 env
->msr_hv_synic_control
);
3513 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIEFP
,
3514 env
->msr_hv_synic_evt_page
);
3515 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIMP
,
3516 env
->msr_hv_synic_msg_page
);
3518 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_synic_sint
); j
++) {
3519 kvm_msr_entry_add(cpu
, HV_X64_MSR_SINT0
+ j
,
3520 env
->msr_hv_synic_sint
[j
]);
3523 if (has_msr_hv_stimer
) {
3526 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_stimer_config
); j
++) {
3527 kvm_msr_entry_add(cpu
, HV_X64_MSR_STIMER0_CONFIG
+ j
* 2,
3528 env
->msr_hv_stimer_config
[j
]);
3531 for (j
= 0; j
< ARRAY_SIZE(env
->msr_hv_stimer_count
); j
++) {
3532 kvm_msr_entry_add(cpu
, HV_X64_MSR_STIMER0_COUNT
+ j
* 2,
3533 env
->msr_hv_stimer_count
[j
]);
3536 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
3537 uint64_t phys_mask
= MAKE_64BIT_MASK(0, cpu
->phys_bits
);
3539 kvm_msr_entry_add(cpu
, MSR_MTRRdefType
, env
->mtrr_deftype
);
3540 kvm_msr_entry_add(cpu
, MSR_MTRRfix64K_00000
, env
->mtrr_fixed
[0]);
3541 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_80000
, env
->mtrr_fixed
[1]);
3542 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_A0000
, env
->mtrr_fixed
[2]);
3543 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C0000
, env
->mtrr_fixed
[3]);
3544 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C8000
, env
->mtrr_fixed
[4]);
3545 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D0000
, env
->mtrr_fixed
[5]);
3546 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D8000
, env
->mtrr_fixed
[6]);
3547 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E0000
, env
->mtrr_fixed
[7]);
3548 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E8000
, env
->mtrr_fixed
[8]);
3549 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F0000
, env
->mtrr_fixed
[9]);
3550 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F8000
, env
->mtrr_fixed
[10]);
3551 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
3552 /* The CPU GPs if we write to a bit above the physical limit of
3553 * the host CPU (and KVM emulates that)
3555 uint64_t mask
= env
->mtrr_var
[i
].mask
;
3558 kvm_msr_entry_add(cpu
, MSR_MTRRphysBase(i
),
3559 env
->mtrr_var
[i
].base
);
3560 kvm_msr_entry_add(cpu
, MSR_MTRRphysMask(i
), mask
);
3563 if (env
->features
[FEAT_7_0_EBX
] & CPUID_7_0_EBX_INTEL_PT
) {
3564 int addr_num
= kvm_arch_get_supported_cpuid(kvm_state
,
3565 0x14, 1, R_EAX
) & 0x7;
3567 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_CTL
,
3568 env
->msr_rtit_ctrl
);
3569 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_STATUS
,
3570 env
->msr_rtit_status
);
3571 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_OUTPUT_BASE
,
3572 env
->msr_rtit_output_base
);
3573 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_OUTPUT_MASK
,
3574 env
->msr_rtit_output_mask
);
3575 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_CR3_MATCH
,
3576 env
->msr_rtit_cr3_match
);
3577 for (i
= 0; i
< addr_num
; i
++) {
3578 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_ADDR0_A
+ i
,
3579 env
->msr_rtit_addrs
[i
]);
3583 if (env
->features
[FEAT_7_0_ECX
] & CPUID_7_0_ECX_SGX_LC
) {
3584 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH0
,
3585 env
->msr_ia32_sgxlepubkeyhash
[0]);
3586 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH1
,
3587 env
->msr_ia32_sgxlepubkeyhash
[1]);
3588 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH2
,
3589 env
->msr_ia32_sgxlepubkeyhash
[2]);
3590 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH3
,
3591 env
->msr_ia32_sgxlepubkeyhash
[3]);
3594 if (env
->features
[FEAT_XSAVE
] & CPUID_D_1_EAX_XFD
) {
3595 kvm_msr_entry_add(cpu
, MSR_IA32_XFD
,
3597 kvm_msr_entry_add(cpu
, MSR_IA32_XFD_ERR
,
3601 if (kvm_enabled() && cpu
->enable_pmu
&&
3602 (env
->features
[FEAT_7_0_EDX
] & CPUID_7_0_EDX_ARCH_LBR
)) {
3607 * Only migrate Arch LBR states when the host Arch LBR depth
3608 * equals that of source guest's, this is to avoid mismatch
3609 * of guest/host config for the msr hence avoid unexpected
3612 ret
= kvm_get_one_msr(cpu
, MSR_ARCH_LBR_DEPTH
, &depth
);
3614 if (ret
== 1 && !!depth
&& depth
== env
->msr_lbr_depth
) {
3615 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_CTL
, env
->msr_lbr_ctl
);
3616 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_DEPTH
, env
->msr_lbr_depth
);
3618 for (i
= 0; i
< ARCH_LBR_NR_ENTRIES
; i
++) {
3619 if (!env
->lbr_records
[i
].from
) {
3622 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_FROM_0
+ i
,
3623 env
->lbr_records
[i
].from
);
3624 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_TO_0
+ i
,
3625 env
->lbr_records
[i
].to
);
3626 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_INFO_0
+ i
,
3627 env
->lbr_records
[i
].info
);
3632 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
3633 * kvm_put_msr_feature_control. */
3639 kvm_msr_entry_add(cpu
, MSR_MCG_STATUS
, env
->mcg_status
);
3640 kvm_msr_entry_add(cpu
, MSR_MCG_CTL
, env
->mcg_ctl
);
3641 if (has_msr_mcg_ext_ctl
) {
3642 kvm_msr_entry_add(cpu
, MSR_MCG_EXT_CTL
, env
->mcg_ext_ctl
);
3644 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
3645 kvm_msr_entry_add(cpu
, MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
3649 return kvm_buf_set_msrs(cpu
);
3653 static int kvm_get_fpu(X86CPU
*cpu
)
3655 CPUX86State
*env
= &cpu
->env
;
3659 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
3664 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
3665 env
->fpus
= fpu
.fsw
;
3666 env
->fpuc
= fpu
.fcw
;
3667 env
->fpop
= fpu
.last_opcode
;
3668 env
->fpip
= fpu
.last_ip
;
3669 env
->fpdp
= fpu
.last_dp
;
3670 for (i
= 0; i
< 8; ++i
) {
3671 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
3673 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
3674 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
3675 env
->xmm_regs
[i
].ZMM_Q(0) = ldq_p(&fpu
.xmm
[i
][0]);
3676 env
->xmm_regs
[i
].ZMM_Q(1) = ldq_p(&fpu
.xmm
[i
][8]);
3678 env
->mxcsr
= fpu
.mxcsr
;
3683 static int kvm_get_xsave(X86CPU
*cpu
)
3685 CPUX86State
*env
= &cpu
->env
;
3686 void *xsave
= env
->xsave_buf
;
3690 return kvm_get_fpu(cpu
);
3693 type
= has_xsave2
? KVM_GET_XSAVE2
: KVM_GET_XSAVE
;
3694 ret
= kvm_vcpu_ioctl(CPU(cpu
), type
, xsave
);
3698 x86_cpu_xrstor_all_areas(cpu
, xsave
, env
->xsave_buf_len
);
3703 static int kvm_get_xcrs(X86CPU
*cpu
)
3705 CPUX86State
*env
= &cpu
->env
;
3707 struct kvm_xcrs xcrs
;
3713 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
3718 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
3719 /* Only support xcr0 now */
3720 if (xcrs
.xcrs
[i
].xcr
== 0) {
3721 env
->xcr0
= xcrs
.xcrs
[i
].value
;
3728 static int kvm_get_sregs(X86CPU
*cpu
)
3730 CPUX86State
*env
= &cpu
->env
;
3731 struct kvm_sregs sregs
;
3734 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
3740 * The interrupt_bitmap is ignored because KVM_GET_SREGS is
3741 * always preceded by KVM_GET_VCPU_EVENTS.
3744 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
3745 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
3746 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
3747 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
3748 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
3749 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
3751 get_seg(&env
->tr
, &sregs
.tr
);
3752 get_seg(&env
->ldt
, &sregs
.ldt
);
3754 env
->idt
.limit
= sregs
.idt
.limit
;
3755 env
->idt
.base
= sregs
.idt
.base
;
3756 env
->gdt
.limit
= sregs
.gdt
.limit
;
3757 env
->gdt
.base
= sregs
.gdt
.base
;
3759 env
->cr
[0] = sregs
.cr0
;
3760 env
->cr
[2] = sregs
.cr2
;
3761 env
->cr
[3] = sregs
.cr3
;
3762 env
->cr
[4] = sregs
.cr4
;
3764 env
->efer
= sregs
.efer
;
3766 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
3767 x86_update_hflags(env
);
3772 static int kvm_get_sregs2(X86CPU
*cpu
)
3774 CPUX86State
*env
= &cpu
->env
;
3775 struct kvm_sregs2 sregs
;
3778 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS2
, &sregs
);
3783 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
3784 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
3785 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
3786 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
3787 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
3788 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
3790 get_seg(&env
->tr
, &sregs
.tr
);
3791 get_seg(&env
->ldt
, &sregs
.ldt
);
3793 env
->idt
.limit
= sregs
.idt
.limit
;
3794 env
->idt
.base
= sregs
.idt
.base
;
3795 env
->gdt
.limit
= sregs
.gdt
.limit
;
3796 env
->gdt
.base
= sregs
.gdt
.base
;
3798 env
->cr
[0] = sregs
.cr0
;
3799 env
->cr
[2] = sregs
.cr2
;
3800 env
->cr
[3] = sregs
.cr3
;
3801 env
->cr
[4] = sregs
.cr4
;
3803 env
->efer
= sregs
.efer
;
3805 env
->pdptrs_valid
= sregs
.flags
& KVM_SREGS2_FLAGS_PDPTRS_VALID
;
3807 if (env
->pdptrs_valid
) {
3808 for (i
= 0; i
< 4; i
++) {
3809 env
->pdptrs
[i
] = sregs
.pdptrs
[i
];
3813 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
3814 x86_update_hflags(env
);
3819 static int kvm_get_msrs(X86CPU
*cpu
)
3821 CPUX86State
*env
= &cpu
->env
;
3822 struct kvm_msr_entry
*msrs
= cpu
->kvm_msr_buf
->entries
;
3824 uint64_t mtrr_top_bits
;
3826 kvm_msr_buf_reset(cpu
);
3828 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_CS
, 0);
3829 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_ESP
, 0);
3830 kvm_msr_entry_add(cpu
, MSR_IA32_SYSENTER_EIP
, 0);
3831 kvm_msr_entry_add(cpu
, MSR_PAT
, 0);
3833 kvm_msr_entry_add(cpu
, MSR_STAR
, 0);
3835 if (has_msr_hsave_pa
) {
3836 kvm_msr_entry_add(cpu
, MSR_VM_HSAVE_PA
, 0);
3838 if (has_msr_tsc_aux
) {
3839 kvm_msr_entry_add(cpu
, MSR_TSC_AUX
, 0);
3841 if (has_msr_tsc_adjust
) {
3842 kvm_msr_entry_add(cpu
, MSR_TSC_ADJUST
, 0);
3844 if (has_msr_tsc_deadline
) {
3845 kvm_msr_entry_add(cpu
, MSR_IA32_TSCDEADLINE
, 0);
3847 if (has_msr_misc_enable
) {
3848 kvm_msr_entry_add(cpu
, MSR_IA32_MISC_ENABLE
, 0);
3850 if (has_msr_smbase
) {
3851 kvm_msr_entry_add(cpu
, MSR_IA32_SMBASE
, 0);
3853 if (has_msr_smi_count
) {
3854 kvm_msr_entry_add(cpu
, MSR_SMI_COUNT
, 0);
3856 if (has_msr_feature_control
) {
3857 kvm_msr_entry_add(cpu
, MSR_IA32_FEATURE_CONTROL
, 0);
3860 kvm_msr_entry_add(cpu
, MSR_IA32_PKRS
, 0);
3862 if (has_msr_bndcfgs
) {
3863 kvm_msr_entry_add(cpu
, MSR_IA32_BNDCFGS
, 0);
3866 kvm_msr_entry_add(cpu
, MSR_IA32_XSS
, 0);
3868 if (has_msr_umwait
) {
3869 kvm_msr_entry_add(cpu
, MSR_IA32_UMWAIT_CONTROL
, 0);
3871 if (has_msr_spec_ctrl
) {
3872 kvm_msr_entry_add(cpu
, MSR_IA32_SPEC_CTRL
, 0);
3874 if (has_tsc_scale_msr
) {
3875 kvm_msr_entry_add(cpu
, MSR_AMD64_TSC_RATIO
, 0);
3878 if (has_msr_tsx_ctrl
) {
3879 kvm_msr_entry_add(cpu
, MSR_IA32_TSX_CTRL
, 0);
3881 if (has_msr_virt_ssbd
) {
3882 kvm_msr_entry_add(cpu
, MSR_VIRT_SSBD
, 0);
3884 if (!env
->tsc_valid
) {
3885 kvm_msr_entry_add(cpu
, MSR_IA32_TSC
, 0);
3886 env
->tsc_valid
= !runstate_is_running();
3889 #ifdef TARGET_X86_64
3890 if (lm_capable_kernel
) {
3891 kvm_msr_entry_add(cpu
, MSR_CSTAR
, 0);
3892 kvm_msr_entry_add(cpu
, MSR_KERNELGSBASE
, 0);
3893 kvm_msr_entry_add(cpu
, MSR_FMASK
, 0);
3894 kvm_msr_entry_add(cpu
, MSR_LSTAR
, 0);
3897 kvm_msr_entry_add(cpu
, MSR_KVM_SYSTEM_TIME
, 0);
3898 kvm_msr_entry_add(cpu
, MSR_KVM_WALL_CLOCK
, 0);
3899 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF_INT
)) {
3900 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_INT
, 0);
3902 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_ASYNC_PF
)) {
3903 kvm_msr_entry_add(cpu
, MSR_KVM_ASYNC_PF_EN
, 0);
3905 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_PV_EOI
)) {
3906 kvm_msr_entry_add(cpu
, MSR_KVM_PV_EOI_EN
, 0);
3908 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_STEAL_TIME
)) {
3909 kvm_msr_entry_add(cpu
, MSR_KVM_STEAL_TIME
, 0);
3911 if (env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_POLL_CONTROL
)) {
3912 kvm_msr_entry_add(cpu
, MSR_KVM_POLL_CONTROL
, 1);
3914 if (has_architectural_pmu_version
> 0) {
3915 if (has_architectural_pmu_version
> 1) {
3916 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
3917 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_CTRL
, 0);
3918 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_STATUS
, 0);
3919 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_GLOBAL_OVF_CTRL
, 0);
3921 for (i
= 0; i
< num_architectural_pmu_fixed_counters
; i
++) {
3922 kvm_msr_entry_add(cpu
, MSR_CORE_PERF_FIXED_CTR0
+ i
, 0);
3924 for (i
= 0; i
< num_architectural_pmu_gp_counters
; i
++) {
3925 kvm_msr_entry_add(cpu
, MSR_P6_PERFCTR0
+ i
, 0);
3926 kvm_msr_entry_add(cpu
, MSR_P6_EVNTSEL0
+ i
, 0);
3931 kvm_msr_entry_add(cpu
, MSR_MCG_STATUS
, 0);
3932 kvm_msr_entry_add(cpu
, MSR_MCG_CTL
, 0);
3933 if (has_msr_mcg_ext_ctl
) {
3934 kvm_msr_entry_add(cpu
, MSR_MCG_EXT_CTL
, 0);
3936 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
3937 kvm_msr_entry_add(cpu
, MSR_MC0_CTL
+ i
, 0);
3941 if (has_msr_hv_hypercall
) {
3942 kvm_msr_entry_add(cpu
, HV_X64_MSR_HYPERCALL
, 0);
3943 kvm_msr_entry_add(cpu
, HV_X64_MSR_GUEST_OS_ID
, 0);
3945 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_VAPIC
)) {
3946 kvm_msr_entry_add(cpu
, HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
3948 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_TIME
)) {
3949 kvm_msr_entry_add(cpu
, HV_X64_MSR_REFERENCE_TSC
, 0);
3951 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_REENLIGHTENMENT
)) {
3952 kvm_msr_entry_add(cpu
, HV_X64_MSR_REENLIGHTENMENT_CONTROL
, 0);
3953 kvm_msr_entry_add(cpu
, HV_X64_MSR_TSC_EMULATION_CONTROL
, 0);
3954 kvm_msr_entry_add(cpu
, HV_X64_MSR_TSC_EMULATION_STATUS
, 0);
3956 if (has_msr_hv_syndbg_options
) {
3957 kvm_msr_entry_add(cpu
, HV_X64_MSR_SYNDBG_OPTIONS
, 0);
3959 if (has_msr_hv_crash
) {
3962 for (j
= 0; j
< HV_CRASH_PARAMS
; j
++) {
3963 kvm_msr_entry_add(cpu
, HV_X64_MSR_CRASH_P0
+ j
, 0);
3966 if (has_msr_hv_runtime
) {
3967 kvm_msr_entry_add(cpu
, HV_X64_MSR_VP_RUNTIME
, 0);
3969 if (hyperv_feat_enabled(cpu
, HYPERV_FEAT_SYNIC
)) {
3972 kvm_msr_entry_add(cpu
, HV_X64_MSR_SCONTROL
, 0);
3973 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIEFP
, 0);
3974 kvm_msr_entry_add(cpu
, HV_X64_MSR_SIMP
, 0);
3975 for (msr
= HV_X64_MSR_SINT0
; msr
<= HV_X64_MSR_SINT15
; msr
++) {
3976 kvm_msr_entry_add(cpu
, msr
, 0);
3979 if (has_msr_hv_stimer
) {
3982 for (msr
= HV_X64_MSR_STIMER0_CONFIG
; msr
<= HV_X64_MSR_STIMER3_COUNT
;
3984 kvm_msr_entry_add(cpu
, msr
, 0);
3987 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
3988 kvm_msr_entry_add(cpu
, MSR_MTRRdefType
, 0);
3989 kvm_msr_entry_add(cpu
, MSR_MTRRfix64K_00000
, 0);
3990 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_80000
, 0);
3991 kvm_msr_entry_add(cpu
, MSR_MTRRfix16K_A0000
, 0);
3992 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C0000
, 0);
3993 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_C8000
, 0);
3994 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D0000
, 0);
3995 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_D8000
, 0);
3996 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E0000
, 0);
3997 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_E8000
, 0);
3998 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F0000
, 0);
3999 kvm_msr_entry_add(cpu
, MSR_MTRRfix4K_F8000
, 0);
4000 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
4001 kvm_msr_entry_add(cpu
, MSR_MTRRphysBase(i
), 0);
4002 kvm_msr_entry_add(cpu
, MSR_MTRRphysMask(i
), 0);
4006 if (env
->features
[FEAT_7_0_EBX
] & CPUID_7_0_EBX_INTEL_PT
) {
4008 kvm_arch_get_supported_cpuid(kvm_state
, 0x14, 1, R_EAX
) & 0x7;
4010 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_CTL
, 0);
4011 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_STATUS
, 0);
4012 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_OUTPUT_BASE
, 0);
4013 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_OUTPUT_MASK
, 0);
4014 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_CR3_MATCH
, 0);
4015 for (i
= 0; i
< addr_num
; i
++) {
4016 kvm_msr_entry_add(cpu
, MSR_IA32_RTIT_ADDR0_A
+ i
, 0);
4020 if (env
->features
[FEAT_7_0_ECX
] & CPUID_7_0_ECX_SGX_LC
) {
4021 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH0
, 0);
4022 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH1
, 0);
4023 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH2
, 0);
4024 kvm_msr_entry_add(cpu
, MSR_IA32_SGXLEPUBKEYHASH3
, 0);
4027 if (env
->features
[FEAT_XSAVE
] & CPUID_D_1_EAX_XFD
) {
4028 kvm_msr_entry_add(cpu
, MSR_IA32_XFD
, 0);
4029 kvm_msr_entry_add(cpu
, MSR_IA32_XFD_ERR
, 0);
4032 if (kvm_enabled() && cpu
->enable_pmu
&&
4033 (env
->features
[FEAT_7_0_EDX
] & CPUID_7_0_EDX_ARCH_LBR
)) {
4037 ret
= kvm_get_one_msr(cpu
, MSR_ARCH_LBR_DEPTH
, &depth
);
4038 if (ret
== 1 && depth
== ARCH_LBR_NR_ENTRIES
) {
4039 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_CTL
, 0);
4040 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_DEPTH
, 0);
4042 for (i
= 0; i
< ARCH_LBR_NR_ENTRIES
; i
++) {
4043 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_FROM_0
+ i
, 0);
4044 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_TO_0
+ i
, 0);
4045 kvm_msr_entry_add(cpu
, MSR_ARCH_LBR_INFO_0
+ i
, 0);
4050 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, cpu
->kvm_msr_buf
);
4055 if (ret
< cpu
->kvm_msr_buf
->nmsrs
) {
4056 struct kvm_msr_entry
*e
= &cpu
->kvm_msr_buf
->entries
[ret
];
4057 error_report("error: failed to get MSR 0x%" PRIx32
,
4058 (uint32_t)e
->index
);
4061 assert(ret
== cpu
->kvm_msr_buf
->nmsrs
);
4063 * MTRR masks: Each mask consists of 5 parts
4064 * a 10..0: must be zero
4066 * c n-1.12: actual mask bits
4067 * d 51..n: reserved must be zero
4068 * e 63.52: reserved must be zero
4070 * 'n' is the number of physical bits supported by the CPU and is
4071 * apparently always <= 52. We know our 'n' but don't know what
4072 * the destinations 'n' is; it might be smaller, in which case
4073 * it masks (c) on loading. It might be larger, in which case
4074 * we fill 'd' so that d..c is consistent irrespetive of the 'n'
4075 * we're migrating to.
4078 if (cpu
->fill_mtrr_mask
) {
4079 QEMU_BUILD_BUG_ON(TARGET_PHYS_ADDR_SPACE_BITS
> 52);
4080 assert(cpu
->phys_bits
<= TARGET_PHYS_ADDR_SPACE_BITS
);
4081 mtrr_top_bits
= MAKE_64BIT_MASK(cpu
->phys_bits
, 52 - cpu
->phys_bits
);
4086 for (i
= 0; i
< ret
; i
++) {
4087 uint32_t index
= msrs
[i
].index
;
4089 case MSR_IA32_SYSENTER_CS
:
4090 env
->sysenter_cs
= msrs
[i
].data
;
4092 case MSR_IA32_SYSENTER_ESP
:
4093 env
->sysenter_esp
= msrs
[i
].data
;
4095 case MSR_IA32_SYSENTER_EIP
:
4096 env
->sysenter_eip
= msrs
[i
].data
;
4099 env
->pat
= msrs
[i
].data
;
4102 env
->star
= msrs
[i
].data
;
4104 #ifdef TARGET_X86_64
4106 env
->cstar
= msrs
[i
].data
;
4108 case MSR_KERNELGSBASE
:
4109 env
->kernelgsbase
= msrs
[i
].data
;
4112 env
->fmask
= msrs
[i
].data
;
4115 env
->lstar
= msrs
[i
].data
;
4119 env
->tsc
= msrs
[i
].data
;
4122 env
->tsc_aux
= msrs
[i
].data
;
4124 case MSR_TSC_ADJUST
:
4125 env
->tsc_adjust
= msrs
[i
].data
;
4127 case MSR_IA32_TSCDEADLINE
:
4128 env
->tsc_deadline
= msrs
[i
].data
;
4130 case MSR_VM_HSAVE_PA
:
4131 env
->vm_hsave
= msrs
[i
].data
;
4133 case MSR_KVM_SYSTEM_TIME
:
4134 env
->system_time_msr
= msrs
[i
].data
;
4136 case MSR_KVM_WALL_CLOCK
:
4137 env
->wall_clock_msr
= msrs
[i
].data
;
4139 case MSR_MCG_STATUS
:
4140 env
->mcg_status
= msrs
[i
].data
;
4143 env
->mcg_ctl
= msrs
[i
].data
;
4145 case MSR_MCG_EXT_CTL
:
4146 env
->mcg_ext_ctl
= msrs
[i
].data
;
4148 case MSR_IA32_MISC_ENABLE
:
4149 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
4151 case MSR_IA32_SMBASE
:
4152 env
->smbase
= msrs
[i
].data
;
4155 env
->msr_smi_count
= msrs
[i
].data
;
4157 case MSR_IA32_FEATURE_CONTROL
:
4158 env
->msr_ia32_feature_control
= msrs
[i
].data
;
4160 case MSR_IA32_BNDCFGS
:
4161 env
->msr_bndcfgs
= msrs
[i
].data
;
4164 env
->xss
= msrs
[i
].data
;
4166 case MSR_IA32_UMWAIT_CONTROL
:
4167 env
->umwait
= msrs
[i
].data
;
4170 env
->pkrs
= msrs
[i
].data
;
4173 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
4174 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
4175 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
4178 case MSR_KVM_ASYNC_PF_EN
:
4179 env
->async_pf_en_msr
= msrs
[i
].data
;
4181 case MSR_KVM_ASYNC_PF_INT
:
4182 env
->async_pf_int_msr
= msrs
[i
].data
;
4184 case MSR_KVM_PV_EOI_EN
:
4185 env
->pv_eoi_en_msr
= msrs
[i
].data
;
4187 case MSR_KVM_STEAL_TIME
:
4188 env
->steal_time_msr
= msrs
[i
].data
;
4190 case MSR_KVM_POLL_CONTROL
: {
4191 env
->poll_control_msr
= msrs
[i
].data
;
4194 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
4195 env
->msr_fixed_ctr_ctrl
= msrs
[i
].data
;
4197 case MSR_CORE_PERF_GLOBAL_CTRL
:
4198 env
->msr_global_ctrl
= msrs
[i
].data
;
4200 case MSR_CORE_PERF_GLOBAL_STATUS
:
4201 env
->msr_global_status
= msrs
[i
].data
;
4203 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
4204 env
->msr_global_ovf_ctrl
= msrs
[i
].data
;
4206 case MSR_CORE_PERF_FIXED_CTR0
... MSR_CORE_PERF_FIXED_CTR0
+ MAX_FIXED_COUNTERS
- 1:
4207 env
->msr_fixed_counters
[index
- MSR_CORE_PERF_FIXED_CTR0
] = msrs
[i
].data
;
4209 case MSR_P6_PERFCTR0
... MSR_P6_PERFCTR0
+ MAX_GP_COUNTERS
- 1:
4210 env
->msr_gp_counters
[index
- MSR_P6_PERFCTR0
] = msrs
[i
].data
;
4212 case MSR_P6_EVNTSEL0
... MSR_P6_EVNTSEL0
+ MAX_GP_COUNTERS
- 1:
4213 env
->msr_gp_evtsel
[index
- MSR_P6_EVNTSEL0
] = msrs
[i
].data
;
4215 case HV_X64_MSR_HYPERCALL
:
4216 env
->msr_hv_hypercall
= msrs
[i
].data
;
4218 case HV_X64_MSR_GUEST_OS_ID
:
4219 env
->msr_hv_guest_os_id
= msrs
[i
].data
;
4221 case HV_X64_MSR_APIC_ASSIST_PAGE
:
4222 env
->msr_hv_vapic
= msrs
[i
].data
;
4224 case HV_X64_MSR_REFERENCE_TSC
:
4225 env
->msr_hv_tsc
= msrs
[i
].data
;
4227 case HV_X64_MSR_CRASH_P0
... HV_X64_MSR_CRASH_P4
:
4228 env
->msr_hv_crash_params
[index
- HV_X64_MSR_CRASH_P0
] = msrs
[i
].data
;
4230 case HV_X64_MSR_VP_RUNTIME
:
4231 env
->msr_hv_runtime
= msrs
[i
].data
;
4233 case HV_X64_MSR_SCONTROL
:
4234 env
->msr_hv_synic_control
= msrs
[i
].data
;
4236 case HV_X64_MSR_SIEFP
:
4237 env
->msr_hv_synic_evt_page
= msrs
[i
].data
;
4239 case HV_X64_MSR_SIMP
:
4240 env
->msr_hv_synic_msg_page
= msrs
[i
].data
;
4242 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
4243 env
->msr_hv_synic_sint
[index
- HV_X64_MSR_SINT0
] = msrs
[i
].data
;
4245 case HV_X64_MSR_STIMER0_CONFIG
:
4246 case HV_X64_MSR_STIMER1_CONFIG
:
4247 case HV_X64_MSR_STIMER2_CONFIG
:
4248 case HV_X64_MSR_STIMER3_CONFIG
:
4249 env
->msr_hv_stimer_config
[(index
- HV_X64_MSR_STIMER0_CONFIG
)/2] =
4252 case HV_X64_MSR_STIMER0_COUNT
:
4253 case HV_X64_MSR_STIMER1_COUNT
:
4254 case HV_X64_MSR_STIMER2_COUNT
:
4255 case HV_X64_MSR_STIMER3_COUNT
:
4256 env
->msr_hv_stimer_count
[(index
- HV_X64_MSR_STIMER0_COUNT
)/2] =
4259 case HV_X64_MSR_REENLIGHTENMENT_CONTROL
:
4260 env
->msr_hv_reenlightenment_control
= msrs
[i
].data
;
4262 case HV_X64_MSR_TSC_EMULATION_CONTROL
:
4263 env
->msr_hv_tsc_emulation_control
= msrs
[i
].data
;
4265 case HV_X64_MSR_TSC_EMULATION_STATUS
:
4266 env
->msr_hv_tsc_emulation_status
= msrs
[i
].data
;
4268 case HV_X64_MSR_SYNDBG_OPTIONS
:
4269 env
->msr_hv_syndbg_options
= msrs
[i
].data
;
4271 case MSR_MTRRdefType
:
4272 env
->mtrr_deftype
= msrs
[i
].data
;
4274 case MSR_MTRRfix64K_00000
:
4275 env
->mtrr_fixed
[0] = msrs
[i
].data
;
4277 case MSR_MTRRfix16K_80000
:
4278 env
->mtrr_fixed
[1] = msrs
[i
].data
;
4280 case MSR_MTRRfix16K_A0000
:
4281 env
->mtrr_fixed
[2] = msrs
[i
].data
;
4283 case MSR_MTRRfix4K_C0000
:
4284 env
->mtrr_fixed
[3] = msrs
[i
].data
;
4286 case MSR_MTRRfix4K_C8000
:
4287 env
->mtrr_fixed
[4] = msrs
[i
].data
;
4289 case MSR_MTRRfix4K_D0000
:
4290 env
->mtrr_fixed
[5] = msrs
[i
].data
;
4292 case MSR_MTRRfix4K_D8000
:
4293 env
->mtrr_fixed
[6] = msrs
[i
].data
;
4295 case MSR_MTRRfix4K_E0000
:
4296 env
->mtrr_fixed
[7] = msrs
[i
].data
;
4298 case MSR_MTRRfix4K_E8000
:
4299 env
->mtrr_fixed
[8] = msrs
[i
].data
;
4301 case MSR_MTRRfix4K_F0000
:
4302 env
->mtrr_fixed
[9] = msrs
[i
].data
;
4304 case MSR_MTRRfix4K_F8000
:
4305 env
->mtrr_fixed
[10] = msrs
[i
].data
;
4307 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT
- 1):
4309 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].mask
= msrs
[i
].data
|
4312 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].base
= msrs
[i
].data
;
4315 case MSR_IA32_SPEC_CTRL
:
4316 env
->spec_ctrl
= msrs
[i
].data
;
4318 case MSR_AMD64_TSC_RATIO
:
4319 env
->amd_tsc_scale_msr
= msrs
[i
].data
;
4321 case MSR_IA32_TSX_CTRL
:
4322 env
->tsx_ctrl
= msrs
[i
].data
;
4325 env
->virt_ssbd
= msrs
[i
].data
;
4327 case MSR_IA32_RTIT_CTL
:
4328 env
->msr_rtit_ctrl
= msrs
[i
].data
;
4330 case MSR_IA32_RTIT_STATUS
:
4331 env
->msr_rtit_status
= msrs
[i
].data
;
4333 case MSR_IA32_RTIT_OUTPUT_BASE
:
4334 env
->msr_rtit_output_base
= msrs
[i
].data
;
4336 case MSR_IA32_RTIT_OUTPUT_MASK
:
4337 env
->msr_rtit_output_mask
= msrs
[i
].data
;
4339 case MSR_IA32_RTIT_CR3_MATCH
:
4340 env
->msr_rtit_cr3_match
= msrs
[i
].data
;
4342 case MSR_IA32_RTIT_ADDR0_A
... MSR_IA32_RTIT_ADDR3_B
:
4343 env
->msr_rtit_addrs
[index
- MSR_IA32_RTIT_ADDR0_A
] = msrs
[i
].data
;
4345 case MSR_IA32_SGXLEPUBKEYHASH0
... MSR_IA32_SGXLEPUBKEYHASH3
:
4346 env
->msr_ia32_sgxlepubkeyhash
[index
- MSR_IA32_SGXLEPUBKEYHASH0
] =
4350 env
->msr_xfd
= msrs
[i
].data
;
4352 case MSR_IA32_XFD_ERR
:
4353 env
->msr_xfd_err
= msrs
[i
].data
;
4355 case MSR_ARCH_LBR_CTL
:
4356 env
->msr_lbr_ctl
= msrs
[i
].data
;
4358 case MSR_ARCH_LBR_DEPTH
:
4359 env
->msr_lbr_depth
= msrs
[i
].data
;
4361 case MSR_ARCH_LBR_FROM_0
... MSR_ARCH_LBR_FROM_0
+ 31:
4362 env
->lbr_records
[index
- MSR_ARCH_LBR_FROM_0
].from
= msrs
[i
].data
;
4364 case MSR_ARCH_LBR_TO_0
... MSR_ARCH_LBR_TO_0
+ 31:
4365 env
->lbr_records
[index
- MSR_ARCH_LBR_TO_0
].to
= msrs
[i
].data
;
4367 case MSR_ARCH_LBR_INFO_0
... MSR_ARCH_LBR_INFO_0
+ 31:
4368 env
->lbr_records
[index
- MSR_ARCH_LBR_INFO_0
].info
= msrs
[i
].data
;
4376 static int kvm_put_mp_state(X86CPU
*cpu
)
4378 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
4380 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
4383 static int kvm_get_mp_state(X86CPU
*cpu
)
4385 CPUState
*cs
= CPU(cpu
);
4386 CPUX86State
*env
= &cpu
->env
;
4387 struct kvm_mp_state mp_state
;
4390 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
4394 env
->mp_state
= mp_state
.mp_state
;
4395 if (kvm_irqchip_in_kernel()) {
4396 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
4401 static int kvm_get_apic(X86CPU
*cpu
)
4403 DeviceState
*apic
= cpu
->apic_state
;
4404 struct kvm_lapic_state kapic
;
4407 if (apic
&& kvm_irqchip_in_kernel()) {
4408 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
4413 kvm_get_apic_state(apic
, &kapic
);
4418 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
4420 CPUState
*cs
= CPU(cpu
);
4421 CPUX86State
*env
= &cpu
->env
;
4422 struct kvm_vcpu_events events
= {};
4424 if (!kvm_has_vcpu_events()) {
4430 if (has_exception_payload
) {
4431 events
.flags
|= KVM_VCPUEVENT_VALID_PAYLOAD
;
4432 events
.exception
.pending
= env
->exception_pending
;
4433 events
.exception_has_payload
= env
->exception_has_payload
;
4434 events
.exception_payload
= env
->exception_payload
;
4436 events
.exception
.nr
= env
->exception_nr
;
4437 events
.exception
.injected
= env
->exception_injected
;
4438 events
.exception
.has_error_code
= env
->has_error_code
;
4439 events
.exception
.error_code
= env
->error_code
;
4441 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
4442 events
.interrupt
.nr
= env
->interrupt_injected
;
4443 events
.interrupt
.soft
= env
->soft_interrupt
;
4445 events
.nmi
.injected
= env
->nmi_injected
;
4446 events
.nmi
.pending
= env
->nmi_pending
;
4447 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
4449 events
.sipi_vector
= env
->sipi_vector
;
4451 if (has_msr_smbase
) {
4452 events
.smi
.smm
= !!(env
->hflags
& HF_SMM_MASK
);
4453 events
.smi
.smm_inside_nmi
= !!(env
->hflags2
& HF2_SMM_INSIDE_NMI_MASK
);
4454 if (kvm_irqchip_in_kernel()) {
4455 /* As soon as these are moved to the kernel, remove them
4456 * from cs->interrupt_request.
4458 events
.smi
.pending
= cs
->interrupt_request
& CPU_INTERRUPT_SMI
;
4459 events
.smi
.latched_init
= cs
->interrupt_request
& CPU_INTERRUPT_INIT
;
4460 cs
->interrupt_request
&= ~(CPU_INTERRUPT_INIT
| CPU_INTERRUPT_SMI
);
4462 /* Keep these in cs->interrupt_request. */
4463 events
.smi
.pending
= 0;
4464 events
.smi
.latched_init
= 0;
4466 /* Stop SMI delivery on old machine types to avoid a reboot
4467 * on an inward migration of an old VM.
4469 if (!cpu
->kvm_no_smi_migration
) {
4470 events
.flags
|= KVM_VCPUEVENT_VALID_SMM
;
4474 if (level
>= KVM_PUT_RESET_STATE
) {
4475 events
.flags
|= KVM_VCPUEVENT_VALID_NMI_PENDING
;
4476 if (env
->mp_state
== KVM_MP_STATE_SIPI_RECEIVED
) {
4477 events
.flags
|= KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
4481 if (has_triple_fault_event
) {
4482 events
.flags
|= KVM_VCPUEVENT_VALID_TRIPLE_FAULT
;
4483 events
.triple_fault
.pending
= env
->triple_fault_pending
;
4486 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
4489 static int kvm_get_vcpu_events(X86CPU
*cpu
)
4491 CPUX86State
*env
= &cpu
->env
;
4492 struct kvm_vcpu_events events
;
4495 if (!kvm_has_vcpu_events()) {
4499 memset(&events
, 0, sizeof(events
));
4500 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
4505 if (events
.flags
& KVM_VCPUEVENT_VALID_PAYLOAD
) {
4506 env
->exception_pending
= events
.exception
.pending
;
4507 env
->exception_has_payload
= events
.exception_has_payload
;
4508 env
->exception_payload
= events
.exception_payload
;
4510 env
->exception_pending
= 0;
4511 env
->exception_has_payload
= false;
4513 env
->exception_injected
= events
.exception
.injected
;
4515 (env
->exception_pending
|| env
->exception_injected
) ?
4516 events
.exception
.nr
: -1;
4517 env
->has_error_code
= events
.exception
.has_error_code
;
4518 env
->error_code
= events
.exception
.error_code
;
4520 env
->interrupt_injected
=
4521 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
4522 env
->soft_interrupt
= events
.interrupt
.soft
;
4524 env
->nmi_injected
= events
.nmi
.injected
;
4525 env
->nmi_pending
= events
.nmi
.pending
;
4526 if (events
.nmi
.masked
) {
4527 env
->hflags2
|= HF2_NMI_MASK
;
4529 env
->hflags2
&= ~HF2_NMI_MASK
;
4532 if (events
.flags
& KVM_VCPUEVENT_VALID_SMM
) {
4533 if (events
.smi
.smm
) {
4534 env
->hflags
|= HF_SMM_MASK
;
4536 env
->hflags
&= ~HF_SMM_MASK
;
4538 if (events
.smi
.pending
) {
4539 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_SMI
);
4541 cpu_reset_interrupt(CPU(cpu
), CPU_INTERRUPT_SMI
);
4543 if (events
.smi
.smm_inside_nmi
) {
4544 env
->hflags2
|= HF2_SMM_INSIDE_NMI_MASK
;
4546 env
->hflags2
&= ~HF2_SMM_INSIDE_NMI_MASK
;
4548 if (events
.smi
.latched_init
) {
4549 cpu_interrupt(CPU(cpu
), CPU_INTERRUPT_INIT
);
4551 cpu_reset_interrupt(CPU(cpu
), CPU_INTERRUPT_INIT
);
4555 if (events
.flags
& KVM_VCPUEVENT_VALID_TRIPLE_FAULT
) {
4556 env
->triple_fault_pending
= events
.triple_fault
.pending
;
4559 env
->sipi_vector
= events
.sipi_vector
;
4564 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
4566 CPUState
*cs
= CPU(cpu
);
4567 CPUX86State
*env
= &cpu
->env
;
4569 unsigned long reinject_trap
= 0;
4571 if (!kvm_has_vcpu_events()) {
4572 if (env
->exception_nr
== EXCP01_DB
) {
4573 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
4574 } else if (env
->exception_injected
== EXCP03_INT3
) {
4575 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
4577 kvm_reset_exception(env
);
4581 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
4582 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
4583 * by updating the debug state once again if single-stepping is on.
4584 * Another reason to call kvm_update_guest_debug here is a pending debug
4585 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
4586 * reinject them via SET_GUEST_DEBUG.
4588 if (reinject_trap
||
4589 (!kvm_has_robust_singlestep() && cs
->singlestep_enabled
)) {
4590 ret
= kvm_update_guest_debug(cs
, reinject_trap
);
4595 static int kvm_put_debugregs(X86CPU
*cpu
)
4597 CPUX86State
*env
= &cpu
->env
;
4598 struct kvm_debugregs dbgregs
;
4601 if (!kvm_has_debugregs()) {
4605 memset(&dbgregs
, 0, sizeof(dbgregs
));
4606 for (i
= 0; i
< 4; i
++) {
4607 dbgregs
.db
[i
] = env
->dr
[i
];
4609 dbgregs
.dr6
= env
->dr
[6];
4610 dbgregs
.dr7
= env
->dr
[7];
4613 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
4616 static int kvm_get_debugregs(X86CPU
*cpu
)
4618 CPUX86State
*env
= &cpu
->env
;
4619 struct kvm_debugregs dbgregs
;
4622 if (!kvm_has_debugregs()) {
4626 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
4630 for (i
= 0; i
< 4; i
++) {
4631 env
->dr
[i
] = dbgregs
.db
[i
];
4633 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
4634 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
4639 static int kvm_put_nested_state(X86CPU
*cpu
)
4641 CPUX86State
*env
= &cpu
->env
;
4642 int max_nested_state_len
= kvm_max_nested_state_length();
4644 if (!env
->nested_state
) {
4649 * Copy flags that are affected by reset from env->hflags and env->hflags2.
4651 if (env
->hflags
& HF_GUEST_MASK
) {
4652 env
->nested_state
->flags
|= KVM_STATE_NESTED_GUEST_MODE
;
4654 env
->nested_state
->flags
&= ~KVM_STATE_NESTED_GUEST_MODE
;
4657 /* Don't set KVM_STATE_NESTED_GIF_SET on VMX as it is illegal */
4658 if (cpu_has_svm(env
) && (env
->hflags2
& HF2_GIF_MASK
)) {
4659 env
->nested_state
->flags
|= KVM_STATE_NESTED_GIF_SET
;
4661 env
->nested_state
->flags
&= ~KVM_STATE_NESTED_GIF_SET
;
4664 assert(env
->nested_state
->size
<= max_nested_state_len
);
4665 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_NESTED_STATE
, env
->nested_state
);
4668 static int kvm_get_nested_state(X86CPU
*cpu
)
4670 CPUX86State
*env
= &cpu
->env
;
4671 int max_nested_state_len
= kvm_max_nested_state_length();
4674 if (!env
->nested_state
) {
4679 * It is possible that migration restored a smaller size into
4680 * nested_state->hdr.size than what our kernel support.
4681 * We preserve migration origin nested_state->hdr.size for
4682 * call to KVM_SET_NESTED_STATE but wish that our next call
4683 * to KVM_GET_NESTED_STATE will use max size our kernel support.
4685 env
->nested_state
->size
= max_nested_state_len
;
4687 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_NESTED_STATE
, env
->nested_state
);
4693 * Copy flags that are affected by reset to env->hflags and env->hflags2.
4695 if (env
->nested_state
->flags
& KVM_STATE_NESTED_GUEST_MODE
) {
4696 env
->hflags
|= HF_GUEST_MASK
;
4698 env
->hflags
&= ~HF_GUEST_MASK
;
4701 /* Keep HF2_GIF_MASK set on !SVM as x86_cpu_pending_interrupt() needs it */
4702 if (cpu_has_svm(env
)) {
4703 if (env
->nested_state
->flags
& KVM_STATE_NESTED_GIF_SET
) {
4704 env
->hflags2
|= HF2_GIF_MASK
;
4706 env
->hflags2
&= ~HF2_GIF_MASK
;
4713 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
4715 X86CPU
*x86_cpu
= X86_CPU(cpu
);
4718 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
4721 * Put MSR_IA32_FEATURE_CONTROL first, this ensures the VM gets out of VMX
4722 * root operation upon vCPU reset. kvm_put_msr_feature_control() should also
4723 * preceed kvm_put_nested_state() when 'real' nested state is set.
4725 if (level
>= KVM_PUT_RESET_STATE
) {
4726 ret
= kvm_put_msr_feature_control(x86_cpu
);
4732 /* must be before kvm_put_nested_state so that EFER.SVME is set */
4733 ret
= has_sregs2
? kvm_put_sregs2(x86_cpu
) : kvm_put_sregs(x86_cpu
);
4738 if (level
>= KVM_PUT_RESET_STATE
) {
4739 ret
= kvm_put_nested_state(x86_cpu
);
4745 if (level
== KVM_PUT_FULL_STATE
) {
4746 /* We don't check for kvm_arch_set_tsc_khz() errors here,
4747 * because TSC frequency mismatch shouldn't abort migration,
4748 * unless the user explicitly asked for a more strict TSC
4749 * setting (e.g. using an explicit "tsc-freq" option).
4751 kvm_arch_set_tsc_khz(cpu
);
4754 #ifdef CONFIG_XEN_EMU
4755 if (xen_mode
== XEN_EMULATE
&& level
== KVM_PUT_FULL_STATE
) {
4756 ret
= kvm_put_xen_state(cpu
);
4763 ret
= kvm_getput_regs(x86_cpu
, 1);
4767 ret
= kvm_put_xsave(x86_cpu
);
4771 ret
= kvm_put_xcrs(x86_cpu
);
4775 /* must be before kvm_put_msrs */
4776 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
4780 ret
= kvm_put_msrs(x86_cpu
, level
);
4784 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
4788 if (level
>= KVM_PUT_RESET_STATE
) {
4789 ret
= kvm_put_mp_state(x86_cpu
);
4795 ret
= kvm_put_tscdeadline_msr(x86_cpu
);
4799 ret
= kvm_put_debugregs(x86_cpu
);
4804 ret
= kvm_guest_debug_workarounds(x86_cpu
);
4811 int kvm_arch_get_registers(CPUState
*cs
)
4813 X86CPU
*cpu
= X86_CPU(cs
);
4816 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
4818 ret
= kvm_get_vcpu_events(cpu
);
4823 * KVM_GET_MPSTATE can modify CS and RIP, call it before
4824 * KVM_GET_REGS and KVM_GET_SREGS.
4826 ret
= kvm_get_mp_state(cpu
);
4830 ret
= kvm_getput_regs(cpu
, 0);
4834 ret
= kvm_get_xsave(cpu
);
4838 ret
= kvm_get_xcrs(cpu
);
4842 ret
= has_sregs2
? kvm_get_sregs2(cpu
) : kvm_get_sregs(cpu
);
4846 ret
= kvm_get_msrs(cpu
);
4850 ret
= kvm_get_apic(cpu
);
4854 ret
= kvm_get_debugregs(cpu
);
4858 ret
= kvm_get_nested_state(cpu
);
4862 #ifdef CONFIG_XEN_EMU
4863 if (xen_mode
== XEN_EMULATE
) {
4864 ret
= kvm_get_xen_state(cs
);
4872 cpu_sync_bndcs_hflags(&cpu
->env
);
4876 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
4878 X86CPU
*x86_cpu
= X86_CPU(cpu
);
4879 CPUX86State
*env
= &x86_cpu
->env
;
4883 if (cpu
->interrupt_request
& (CPU_INTERRUPT_NMI
| CPU_INTERRUPT_SMI
)) {
4884 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
4885 qemu_mutex_lock_iothread();
4886 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
4887 qemu_mutex_unlock_iothread();
4888 DPRINTF("injected NMI\n");
4889 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
4891 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
4895 if (cpu
->interrupt_request
& CPU_INTERRUPT_SMI
) {
4896 qemu_mutex_lock_iothread();
4897 cpu
->interrupt_request
&= ~CPU_INTERRUPT_SMI
;
4898 qemu_mutex_unlock_iothread();
4899 DPRINTF("injected SMI\n");
4900 ret
= kvm_vcpu_ioctl(cpu
, KVM_SMI
);
4902 fprintf(stderr
, "KVM: injection failed, SMI lost (%s)\n",
4908 if (!kvm_pic_in_kernel()) {
4909 qemu_mutex_lock_iothread();
4912 /* Force the VCPU out of its inner loop to process any INIT requests
4913 * or (for userspace APIC, but it is cheap to combine the checks here)
4914 * pending TPR access reports.
4916 if (cpu
->interrupt_request
& (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
4917 if ((cpu
->interrupt_request
& CPU_INTERRUPT_INIT
) &&
4918 !(env
->hflags
& HF_SMM_MASK
)) {
4919 cpu
->exit_request
= 1;
4921 if (cpu
->interrupt_request
& CPU_INTERRUPT_TPR
) {
4922 cpu
->exit_request
= 1;
4926 if (!kvm_pic_in_kernel()) {
4927 /* Try to inject an interrupt if the guest can accept it */
4928 if (run
->ready_for_interrupt_injection
&&
4929 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
4930 (env
->eflags
& IF_MASK
)) {
4933 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
4934 irq
= cpu_get_pic_interrupt(env
);
4936 struct kvm_interrupt intr
;
4939 DPRINTF("injected interrupt %d\n", irq
);
4940 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
4943 "KVM: injection failed, interrupt lost (%s)\n",
4949 /* If we have an interrupt but the guest is not ready to receive an
4950 * interrupt, request an interrupt window exit. This will
4951 * cause a return to userspace as soon as the guest is ready to
4952 * receive interrupts. */
4953 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
4954 run
->request_interrupt_window
= 1;
4956 run
->request_interrupt_window
= 0;
4959 DPRINTF("setting tpr\n");
4960 run
->cr8
= cpu_get_apic_tpr(x86_cpu
->apic_state
);
4962 qemu_mutex_unlock_iothread();
4966 static void kvm_rate_limit_on_bus_lock(void)
4968 uint64_t delay_ns
= ratelimit_calculate_delay(&bus_lock_ratelimit_ctrl
, 1);
4971 g_usleep(delay_ns
/ SCALE_US
);
4975 MemTxAttrs
kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
4977 X86CPU
*x86_cpu
= X86_CPU(cpu
);
4978 CPUX86State
*env
= &x86_cpu
->env
;
4980 if (run
->flags
& KVM_RUN_X86_SMM
) {
4981 env
->hflags
|= HF_SMM_MASK
;
4983 env
->hflags
&= ~HF_SMM_MASK
;
4986 env
->eflags
|= IF_MASK
;
4988 env
->eflags
&= ~IF_MASK
;
4990 if (run
->flags
& KVM_RUN_X86_BUS_LOCK
) {
4991 kvm_rate_limit_on_bus_lock();
4994 #ifdef CONFIG_XEN_EMU
4996 * If the callback is asserted as a GSI (or PCI INTx) then check if
4997 * vcpu_info->evtchn_upcall_pending has been cleared, and deassert
4998 * the callback IRQ if so. Ideally we could hook into the PIC/IOAPIC
4999 * EOI and only resample then, exactly how the VFIO eventfd pairs
5000 * are designed to work for level triggered interrupts.
5002 if (x86_cpu
->env
.xen_callback_asserted
) {
5003 kvm_xen_maybe_deassert_callback(cpu
);
5007 /* We need to protect the apic state against concurrent accesses from
5008 * different threads in case the userspace irqchip is used. */
5009 if (!kvm_irqchip_in_kernel()) {
5010 qemu_mutex_lock_iothread();
5012 cpu_set_apic_tpr(x86_cpu
->apic_state
, run
->cr8
);
5013 cpu_set_apic_base(x86_cpu
->apic_state
, run
->apic_base
);
5014 if (!kvm_irqchip_in_kernel()) {
5015 qemu_mutex_unlock_iothread();
5017 return cpu_get_mem_attrs(env
);
5020 int kvm_arch_process_async_events(CPUState
*cs
)
5022 X86CPU
*cpu
= X86_CPU(cs
);
5023 CPUX86State
*env
= &cpu
->env
;
5025 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
5026 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
5027 assert(env
->mcg_cap
);
5029 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
5031 kvm_cpu_synchronize_state(cs
);
5033 if (env
->exception_nr
== EXCP08_DBLE
) {
5034 /* this means triple fault */
5035 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
5036 cs
->exit_request
= 1;
5039 kvm_queue_exception(env
, EXCP12_MCHK
, 0, 0);
5040 env
->has_error_code
= 0;
5043 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
5044 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
5048 if ((cs
->interrupt_request
& CPU_INTERRUPT_INIT
) &&
5049 !(env
->hflags
& HF_SMM_MASK
)) {
5050 kvm_cpu_synchronize_state(cs
);
5054 if (kvm_irqchip_in_kernel()) {
5058 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
5059 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
5060 apic_poll_irq(cpu
->apic_state
);
5062 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
5063 (env
->eflags
& IF_MASK
)) ||
5064 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
5067 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
5068 kvm_cpu_synchronize_state(cs
);
5071 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
5072 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
5073 kvm_cpu_synchronize_state(cs
);
5074 apic_handle_tpr_access_report(cpu
->apic_state
, env
->eip
,
5075 env
->tpr_access_type
);
5081 static int kvm_handle_halt(X86CPU
*cpu
)
5083 CPUState
*cs
= CPU(cpu
);
5084 CPUX86State
*env
= &cpu
->env
;
5086 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
5087 (env
->eflags
& IF_MASK
)) &&
5088 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
5096 static int kvm_handle_tpr_access(X86CPU
*cpu
)
5098 CPUState
*cs
= CPU(cpu
);
5099 struct kvm_run
*run
= cs
->kvm_run
;
5101 apic_handle_tpr_access_report(cpu
->apic_state
, run
->tpr_access
.rip
,
5102 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
5107 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
5109 static const uint8_t int3
= 0xcc;
5111 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
5112 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
5118 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
5122 if (cpu_memory_rw_debug(cs
, bp
->pc
, &int3
, 1, 0)) {
5128 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
5140 static int nb_hw_breakpoint
;
5142 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
5146 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
5147 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
5148 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
5155 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
5156 target_ulong len
, int type
)
5159 case GDB_BREAKPOINT_HW
:
5162 case GDB_WATCHPOINT_WRITE
:
5163 case GDB_WATCHPOINT_ACCESS
:
5170 if (addr
& (len
- 1)) {
5182 if (nb_hw_breakpoint
== 4) {
5185 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
5188 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
5189 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
5190 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
5196 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
5197 target_ulong len
, int type
)
5201 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
5206 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
5211 void kvm_arch_remove_all_hw_breakpoints(void)
5213 nb_hw_breakpoint
= 0;
5216 static CPUWatchpoint hw_watchpoint
;
5218 static int kvm_handle_debug(X86CPU
*cpu
,
5219 struct kvm_debug_exit_arch
*arch_info
)
5221 CPUState
*cs
= CPU(cpu
);
5222 CPUX86State
*env
= &cpu
->env
;
5226 if (arch_info
->exception
== EXCP01_DB
) {
5227 if (arch_info
->dr6
& DR6_BS
) {
5228 if (cs
->singlestep_enabled
) {
5232 for (n
= 0; n
< 4; n
++) {
5233 if (arch_info
->dr6
& (1 << n
)) {
5234 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
5240 cs
->watchpoint_hit
= &hw_watchpoint
;
5241 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
5242 hw_watchpoint
.flags
= BP_MEM_WRITE
;
5246 cs
->watchpoint_hit
= &hw_watchpoint
;
5247 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
5248 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
5254 } else if (kvm_find_sw_breakpoint(cs
, arch_info
->pc
)) {
5258 cpu_synchronize_state(cs
);
5259 assert(env
->exception_nr
== -1);
5262 kvm_queue_exception(env
, arch_info
->exception
,
5263 arch_info
->exception
== EXCP01_DB
,
5265 env
->has_error_code
= 0;
5271 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
5273 const uint8_t type_code
[] = {
5274 [GDB_BREAKPOINT_HW
] = 0x0,
5275 [GDB_WATCHPOINT_WRITE
] = 0x1,
5276 [GDB_WATCHPOINT_ACCESS
] = 0x3
5278 const uint8_t len_code
[] = {
5279 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
5283 if (kvm_sw_breakpoints_active(cpu
)) {
5284 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
5286 if (nb_hw_breakpoint
> 0) {
5287 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
5288 dbg
->arch
.debugreg
[7] = 0x0600;
5289 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
5290 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
5291 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
5292 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
5293 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
5298 static bool kvm_install_msr_filters(KVMState
*s
)
5301 struct kvm_msr_filter filter
= {
5302 .flags
= KVM_MSR_FILTER_DEFAULT_ALLOW
,
5306 for (i
= 0; i
< KVM_MSR_FILTER_MAX_RANGES
; i
++) {
5307 KVMMSRHandlers
*handler
= &msr_handlers
[i
];
5309 struct kvm_msr_filter_range
*range
= &filter
.ranges
[j
++];
5311 *range
= (struct kvm_msr_filter_range
) {
5314 .base
= handler
->msr
,
5315 .bitmap
= (__u8
*)&zero
,
5318 if (handler
->rdmsr
) {
5319 range
->flags
|= KVM_MSR_FILTER_READ
;
5322 if (handler
->wrmsr
) {
5323 range
->flags
|= KVM_MSR_FILTER_WRITE
;
5328 r
= kvm_vm_ioctl(s
, KVM_X86_SET_MSR_FILTER
, &filter
);
5336 bool kvm_filter_msr(KVMState
*s
, uint32_t msr
, QEMURDMSRHandler
*rdmsr
,
5337 QEMUWRMSRHandler
*wrmsr
)
5341 for (i
= 0; i
< ARRAY_SIZE(msr_handlers
); i
++) {
5342 if (!msr_handlers
[i
].msr
) {
5343 msr_handlers
[i
] = (KVMMSRHandlers
) {
5349 if (!kvm_install_msr_filters(s
)) {
5350 msr_handlers
[i
] = (KVMMSRHandlers
) { };
5361 static int kvm_handle_rdmsr(X86CPU
*cpu
, struct kvm_run
*run
)
5366 for (i
= 0; i
< ARRAY_SIZE(msr_handlers
); i
++) {
5367 KVMMSRHandlers
*handler
= &msr_handlers
[i
];
5368 if (run
->msr
.index
== handler
->msr
) {
5369 if (handler
->rdmsr
) {
5370 r
= handler
->rdmsr(cpu
, handler
->msr
,
5371 (uint64_t *)&run
->msr
.data
);
5372 run
->msr
.error
= r
? 0 : 1;
5381 static int kvm_handle_wrmsr(X86CPU
*cpu
, struct kvm_run
*run
)
5386 for (i
= 0; i
< ARRAY_SIZE(msr_handlers
); i
++) {
5387 KVMMSRHandlers
*handler
= &msr_handlers
[i
];
5388 if (run
->msr
.index
== handler
->msr
) {
5389 if (handler
->wrmsr
) {
5390 r
= handler
->wrmsr(cpu
, handler
->msr
, run
->msr
.data
);
5391 run
->msr
.error
= r
? 0 : 1;
5400 static bool has_sgx_provisioning
;
5402 static bool __kvm_enable_sgx_provisioning(KVMState
*s
)
5406 if (!kvm_vm_check_extension(s
, KVM_CAP_SGX_ATTRIBUTE
)) {
5410 fd
= qemu_open_old("/dev/sgx_provision", O_RDONLY
);
5415 ret
= kvm_vm_enable_cap(s
, KVM_CAP_SGX_ATTRIBUTE
, 0, fd
);
5417 error_report("Could not enable SGX PROVISIONKEY: %s", strerror(-ret
));
5424 bool kvm_enable_sgx_provisioning(KVMState
*s
)
5426 return MEMORIZE(__kvm_enable_sgx_provisioning(s
), has_sgx_provisioning
);
5429 static bool host_supports_vmx(void)
5431 uint32_t ecx
, unused
;
5433 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
5434 return ecx
& CPUID_EXT_VMX
;
5437 #define VMX_INVALID_GUEST_STATE 0x80000021
5439 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
5441 X86CPU
*cpu
= X86_CPU(cs
);
5448 switch (run
->exit_reason
) {
5450 DPRINTF("handle_hlt\n");
5451 qemu_mutex_lock_iothread();
5452 ret
= kvm_handle_halt(cpu
);
5453 qemu_mutex_unlock_iothread();
5455 case KVM_EXIT_SET_TPR
:
5458 case KVM_EXIT_TPR_ACCESS
:
5459 qemu_mutex_lock_iothread();
5460 ret
= kvm_handle_tpr_access(cpu
);
5461 qemu_mutex_unlock_iothread();
5463 case KVM_EXIT_FAIL_ENTRY
:
5464 code
= run
->fail_entry
.hardware_entry_failure_reason
;
5465 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
5467 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
5469 "\nIf you're running a guest on an Intel machine without "
5470 "unrestricted mode\n"
5471 "support, the failure can be most likely due to the guest "
5472 "entering an invalid\n"
5473 "state for Intel VT. For example, the guest maybe running "
5474 "in big real mode\n"
5475 "which is not supported on less recent Intel processors."
5480 case KVM_EXIT_EXCEPTION
:
5481 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
5482 run
->ex
.exception
, run
->ex
.error_code
);
5485 case KVM_EXIT_DEBUG
:
5486 DPRINTF("kvm_exit_debug\n");
5487 qemu_mutex_lock_iothread();
5488 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
5489 qemu_mutex_unlock_iothread();
5491 case KVM_EXIT_HYPERV
:
5492 ret
= kvm_hv_handle_exit(cpu
, &run
->hyperv
);
5494 case KVM_EXIT_IOAPIC_EOI
:
5495 ioapic_eoi_broadcast(run
->eoi
.vector
);
5498 case KVM_EXIT_X86_BUS_LOCK
:
5499 /* already handled in kvm_arch_post_run */
5502 case KVM_EXIT_NOTIFY
:
5503 ctx_invalid
= !!(run
->notify
.flags
& KVM_NOTIFY_CONTEXT_INVALID
);
5504 state
= KVM_STATE(current_accel());
5505 sprintf(str
, "Encounter a notify exit with %svalid context in"
5506 " guest. There can be possible misbehaves in guest."
5507 " Please have a look.", ctx_invalid
? "in" : "");
5509 state
->notify_vmexit
== NOTIFY_VMEXIT_OPTION_INTERNAL_ERROR
) {
5510 warn_report("KVM internal error: %s", str
);
5513 warn_report_once("KVM: %s", str
);
5517 case KVM_EXIT_X86_RDMSR
:
5518 /* We only enable MSR filtering, any other exit is bogus */
5519 assert(run
->msr
.reason
== KVM_MSR_EXIT_REASON_FILTER
);
5520 ret
= kvm_handle_rdmsr(cpu
, run
);
5522 case KVM_EXIT_X86_WRMSR
:
5523 /* We only enable MSR filtering, any other exit is bogus */
5524 assert(run
->msr
.reason
== KVM_MSR_EXIT_REASON_FILTER
);
5525 ret
= kvm_handle_wrmsr(cpu
, run
);
5527 #ifdef CONFIG_XEN_EMU
5529 ret
= kvm_xen_handle_exit(cpu
, &run
->xen
);
5533 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
5541 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
5543 X86CPU
*cpu
= X86_CPU(cs
);
5544 CPUX86State
*env
= &cpu
->env
;
5546 kvm_cpu_synchronize_state(cs
);
5547 return !(env
->cr
[0] & CR0_PE_MASK
) ||
5548 ((env
->segs
[R_CS
].selector
& 3) != 3);
5551 void kvm_arch_init_irq_routing(KVMState
*s
)
5553 /* We know at this point that we're using the in-kernel
5554 * irqchip, so we can use irqfds, and on x86 we know
5555 * we can use msi via irqfd and GSI routing.
5557 kvm_msi_via_irqfd_allowed
= true;
5558 kvm_gsi_routing_allowed
= true;
5560 if (kvm_irqchip_is_split()) {
5561 KVMRouteChange c
= kvm_irqchip_begin_route_changes(s
);
5564 /* If the ioapic is in QEMU and the lapics are in KVM, reserve
5565 MSI routes for signaling interrupts to the local apics. */
5566 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
5567 if (kvm_irqchip_add_msi_route(&c
, 0, NULL
) < 0) {
5568 error_report("Could not enable split IRQ mode.");
5572 kvm_irqchip_commit_route_changes(&c
);
5576 int kvm_arch_irqchip_create(KVMState
*s
)
5579 if (kvm_kernel_irqchip_split()) {
5580 ret
= kvm_vm_enable_cap(s
, KVM_CAP_SPLIT_IRQCHIP
, 0, 24);
5582 error_report("Could not enable split irqchip mode: %s",
5586 DPRINTF("Enabled KVM_CAP_SPLIT_IRQCHIP\n");
5587 kvm_split_irqchip
= true;
5595 uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address
)
5603 env
= &X86_CPU(first_cpu
)->env
;
5604 if (!(env
->features
[FEAT_KVM
] & (1 << KVM_FEATURE_MSI_EXT_DEST_ID
))) {
5609 * If the remappable format bit is set, or the upper bits are
5610 * already set in address_hi, or the low extended bits aren't
5611 * there anyway, do nothing.
5613 ext_id
= address
& (0xff << MSI_ADDR_DEST_IDX_SHIFT
);
5614 if (!ext_id
|| (ext_id
& (1 << MSI_ADDR_DEST_IDX_SHIFT
)) || (address
>> 32)) {
5619 address
|= ext_id
<< 35;
5623 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
5624 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
5626 X86IOMMUState
*iommu
= x86_iommu_get_default();
5629 X86IOMMUClass
*class = X86_IOMMU_DEVICE_GET_CLASS(iommu
);
5631 if (class->int_remap
) {
5633 MSIMessage src
, dst
;
5635 src
.address
= route
->u
.msi
.address_hi
;
5636 src
.address
<<= VTD_MSI_ADDR_HI_SHIFT
;
5637 src
.address
|= route
->u
.msi
.address_lo
;
5638 src
.data
= route
->u
.msi
.data
;
5640 ret
= class->int_remap(iommu
, &src
, &dst
, dev
? \
5641 pci_requester_id(dev
) : \
5642 X86_IOMMU_SID_INVALID
);
5644 trace_kvm_x86_fixup_msi_error(route
->gsi
);
5649 * Handled untranslated compatibilty format interrupt with
5650 * extended destination ID in the low bits 11-5. */
5651 dst
.address
= kvm_swizzle_msi_ext_dest_id(dst
.address
);
5653 route
->u
.msi
.address_hi
= dst
.address
>> VTD_MSI_ADDR_HI_SHIFT
;
5654 route
->u
.msi
.address_lo
= dst
.address
& VTD_MSI_ADDR_LO_MASK
;
5655 route
->u
.msi
.data
= dst
.data
;
5660 #ifdef CONFIG_XEN_EMU
5661 if (xen_mode
== XEN_EMULATE
) {
5662 int handled
= xen_evtchn_translate_pirq_msi(route
, address
, data
);
5665 * If it was a PIRQ and successfully routed (handled == 0) or it was
5666 * an error (handled < 0), return. If it wasn't a PIRQ, keep going.
5674 address
= kvm_swizzle_msi_ext_dest_id(address
);
5675 route
->u
.msi
.address_hi
= address
>> VTD_MSI_ADDR_HI_SHIFT
;
5676 route
->u
.msi
.address_lo
= address
& VTD_MSI_ADDR_LO_MASK
;
5680 typedef struct MSIRouteEntry MSIRouteEntry
;
5682 struct MSIRouteEntry
{
5683 PCIDevice
*dev
; /* Device pointer */
5684 int vector
; /* MSI/MSIX vector index */
5685 int virq
; /* Virtual IRQ index */
5686 QLIST_ENTRY(MSIRouteEntry
) list
;
5689 /* List of used GSI routes */
5690 static QLIST_HEAD(, MSIRouteEntry
) msi_route_list
= \
5691 QLIST_HEAD_INITIALIZER(msi_route_list
);
5693 void kvm_update_msi_routes_all(void *private, bool global
,
5694 uint32_t index
, uint32_t mask
)
5696 int cnt
= 0, vector
;
5697 MSIRouteEntry
*entry
;
5701 /* TODO: explicit route update */
5702 QLIST_FOREACH(entry
, &msi_route_list
, list
) {
5704 vector
= entry
->vector
;
5706 if (msix_enabled(dev
) && !msix_is_masked(dev
, vector
)) {
5707 msg
= msix_get_message(dev
, vector
);
5708 } else if (msi_enabled(dev
) && !msi_is_masked(dev
, vector
)) {
5709 msg
= msi_get_message(dev
, vector
);
5712 * Either MSI/MSIX is disabled for the device, or the
5713 * specific message was masked out. Skip this one.
5717 kvm_irqchip_update_msi_route(kvm_state
, entry
->virq
, msg
, dev
);
5719 kvm_irqchip_commit_routes(kvm_state
);
5720 trace_kvm_x86_update_msi_routes(cnt
);
5723 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
5724 int vector
, PCIDevice
*dev
)
5726 static bool notify_list_inited
= false;
5727 MSIRouteEntry
*entry
;
5730 /* These are (possibly) IOAPIC routes only used for split
5731 * kernel irqchip mode, while what we are housekeeping are
5732 * PCI devices only. */
5736 entry
= g_new0(MSIRouteEntry
, 1);
5738 entry
->vector
= vector
;
5739 entry
->virq
= route
->gsi
;
5740 QLIST_INSERT_HEAD(&msi_route_list
, entry
, list
);
5742 trace_kvm_x86_add_msi_route(route
->gsi
);
5744 if (!notify_list_inited
) {
5745 /* For the first time we do add route, add ourselves into
5746 * IOMMU's IEC notify list if needed. */
5747 X86IOMMUState
*iommu
= x86_iommu_get_default();
5749 x86_iommu_iec_register_notifier(iommu
,
5750 kvm_update_msi_routes_all
,
5753 notify_list_inited
= true;
5758 int kvm_arch_release_virq_post(int virq
)
5760 MSIRouteEntry
*entry
, *next
;
5761 QLIST_FOREACH_SAFE(entry
, &msi_route_list
, list
, next
) {
5762 if (entry
->virq
== virq
) {
5763 trace_kvm_x86_remove_msi_route(virq
);
5764 QLIST_REMOVE(entry
, list
);
5772 int kvm_arch_msi_data_to_gsi(uint32_t data
)
5777 bool kvm_has_waitpkg(void)
5779 return has_msr_umwait
;
5782 bool kvm_arch_cpu_check_are_resettable(void)
5784 return !sev_es_enabled();
5787 #define ARCH_REQ_XCOMP_GUEST_PERM 0x1025
5789 void kvm_request_xsave_components(X86CPU
*cpu
, uint64_t mask
)
5791 KVMState
*s
= kvm_state
;
5794 mask
&= XSTATE_DYNAMIC_MASK
;
5799 * Just ignore bits that are not in CPUID[EAX=0xD,ECX=0].
5800 * ARCH_REQ_XCOMP_GUEST_PERM would fail, and QEMU has warned
5801 * about them already because they are not supported features.
5803 supported
= kvm_arch_get_supported_cpuid(s
, 0xd, 0, R_EAX
);
5804 supported
|= (uint64_t)kvm_arch_get_supported_cpuid(s
, 0xd, 0, R_EDX
) << 32;
5808 int bit
= ctz64(mask
);
5809 int rc
= syscall(SYS_arch_prctl
, ARCH_REQ_XCOMP_GUEST_PERM
, bit
);
5812 * Older kernel version (<5.17) do not support
5813 * ARCH_REQ_XCOMP_GUEST_PERM, but also do not return
5814 * any dynamic feature from kvm_arch_get_supported_cpuid.
5816 warn_report("prctl(ARCH_REQ_XCOMP_GUEST_PERM) failure "
5817 "for feature bit %d", bit
);
5819 mask
&= ~BIT_ULL(bit
);
5823 static int kvm_arch_get_notify_vmexit(Object
*obj
, Error
**errp
)
5825 KVMState
*s
= KVM_STATE(obj
);
5826 return s
->notify_vmexit
;
5829 static void kvm_arch_set_notify_vmexit(Object
*obj
, int value
, Error
**errp
)
5831 KVMState
*s
= KVM_STATE(obj
);
5834 error_setg(errp
, "Cannot set properties after the accelerator has been initialized");
5838 s
->notify_vmexit
= value
;
5841 static void kvm_arch_get_notify_window(Object
*obj
, Visitor
*v
,
5842 const char *name
, void *opaque
,
5845 KVMState
*s
= KVM_STATE(obj
);
5846 uint32_t value
= s
->notify_window
;
5848 visit_type_uint32(v
, name
, &value
, errp
);
5851 static void kvm_arch_set_notify_window(Object
*obj
, Visitor
*v
,
5852 const char *name
, void *opaque
,
5855 KVMState
*s
= KVM_STATE(obj
);
5859 error_setg(errp
, "Cannot set properties after the accelerator has been initialized");
5863 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
5867 s
->notify_window
= value
;
5870 static void kvm_arch_get_xen_version(Object
*obj
, Visitor
*v
,
5871 const char *name
, void *opaque
,
5874 KVMState
*s
= KVM_STATE(obj
);
5875 uint32_t value
= s
->xen_version
;
5877 visit_type_uint32(v
, name
, &value
, errp
);
5880 static void kvm_arch_set_xen_version(Object
*obj
, Visitor
*v
,
5881 const char *name
, void *opaque
,
5884 KVMState
*s
= KVM_STATE(obj
);
5885 Error
*error
= NULL
;
5888 visit_type_uint32(v
, name
, &value
, &error
);
5890 error_propagate(errp
, error
);
5894 s
->xen_version
= value
;
5895 if (value
&& xen_mode
== XEN_DISABLED
) {
5896 xen_mode
= XEN_EMULATE
;
5900 static void kvm_arch_get_xen_gnttab_max_frames(Object
*obj
, Visitor
*v
,
5901 const char *name
, void *opaque
,
5904 KVMState
*s
= KVM_STATE(obj
);
5905 uint16_t value
= s
->xen_gnttab_max_frames
;
5907 visit_type_uint16(v
, name
, &value
, errp
);
5910 static void kvm_arch_set_xen_gnttab_max_frames(Object
*obj
, Visitor
*v
,
5911 const char *name
, void *opaque
,
5914 KVMState
*s
= KVM_STATE(obj
);
5915 Error
*error
= NULL
;
5918 visit_type_uint16(v
, name
, &value
, &error
);
5920 error_propagate(errp
, error
);
5924 s
->xen_gnttab_max_frames
= value
;
5927 static void kvm_arch_get_xen_evtchn_max_pirq(Object
*obj
, Visitor
*v
,
5928 const char *name
, void *opaque
,
5931 KVMState
*s
= KVM_STATE(obj
);
5932 uint16_t value
= s
->xen_evtchn_max_pirq
;
5934 visit_type_uint16(v
, name
, &value
, errp
);
5937 static void kvm_arch_set_xen_evtchn_max_pirq(Object
*obj
, Visitor
*v
,
5938 const char *name
, void *opaque
,
5941 KVMState
*s
= KVM_STATE(obj
);
5942 Error
*error
= NULL
;
5945 visit_type_uint16(v
, name
, &value
, &error
);
5947 error_propagate(errp
, error
);
5951 s
->xen_evtchn_max_pirq
= value
;
5954 void kvm_arch_accel_class_init(ObjectClass
*oc
)
5956 object_class_property_add_enum(oc
, "notify-vmexit", "NotifyVMexitOption",
5957 &NotifyVmexitOption_lookup
,
5958 kvm_arch_get_notify_vmexit
,
5959 kvm_arch_set_notify_vmexit
);
5960 object_class_property_set_description(oc
, "notify-vmexit",
5961 "Enable notify VM exit");
5963 object_class_property_add(oc
, "notify-window", "uint32",
5964 kvm_arch_get_notify_window
,
5965 kvm_arch_set_notify_window
,
5967 object_class_property_set_description(oc
, "notify-window",
5968 "Clock cycles without an event window "
5969 "after which a notification VM exit occurs");
5971 object_class_property_add(oc
, "xen-version", "uint32",
5972 kvm_arch_get_xen_version
,
5973 kvm_arch_set_xen_version
,
5975 object_class_property_set_description(oc
, "xen-version",
5976 "Xen version to be emulated "
5977 "(in XENVER_version form "
5978 "e.g. 0x4000a for 4.10)");
5980 object_class_property_add(oc
, "xen-gnttab-max-frames", "uint16",
5981 kvm_arch_get_xen_gnttab_max_frames
,
5982 kvm_arch_set_xen_gnttab_max_frames
,
5984 object_class_property_set_description(oc
, "xen-gnttab-max-frames",
5985 "Maximum number of grant table frames");
5987 object_class_property_add(oc
, "xen-evtchn-max-pirq", "uint16",
5988 kvm_arch_get_xen_evtchn_max_pirq
,
5989 kvm_arch_set_xen_evtchn_max_pirq
,
5991 object_class_property_set_description(oc
, "xen-evtchn-max-pirq",
5992 "Maximum number of Xen PIRQs");
5995 void kvm_set_max_apic_id(uint32_t max_apic_id
)
5997 kvm_vm_enable_cap(kvm_state
, KVM_CAP_MAX_VCPU_ID
, 0, max_apic_id
);