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 <sys/types.h>
16 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm.h"
28 #include "exec/gdbstub.h"
29 #include "qemu/host-utils.h"
30 #include "qemu/config-file.h"
31 #include "hw/i386/pc.h"
32 #include "hw/i386/apic.h"
33 #include "hw/i386/apic_internal.h"
34 #include "hw/i386/apic-msidef.h"
35 #include "exec/ioport.h"
36 #include <asm/hyperv.h>
37 #include "hw/pci/pci.h"
38 #include "migration/migration.h"
39 #include "qapi/qmp/qerror.h"
40 #include "exec/memattrs.h"
45 #define DPRINTF(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
48 #define DPRINTF(fmt, ...) \
52 #define MSR_KVM_WALL_CLOCK 0x11
53 #define MSR_KVM_SYSTEM_TIME 0x12
56 #define BUS_MCEERR_AR 4
59 #define BUS_MCEERR_AO 5
62 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
63 KVM_CAP_INFO(SET_TSS_ADDR
),
64 KVM_CAP_INFO(EXT_CPUID
),
65 KVM_CAP_INFO(MP_STATE
),
69 static bool has_msr_star
;
70 static bool has_msr_hsave_pa
;
71 static bool has_msr_tsc_adjust
;
72 static bool has_msr_tsc_deadline
;
73 static bool has_msr_feature_control
;
74 static bool has_msr_async_pf_en
;
75 static bool has_msr_pv_eoi_en
;
76 static bool has_msr_misc_enable
;
77 static bool has_msr_bndcfgs
;
78 static bool has_msr_kvm_steal_time
;
79 static int lm_capable_kernel
;
80 static bool has_msr_hv_hypercall
;
81 static bool has_msr_hv_vapic
;
82 static bool has_msr_hv_tsc
;
83 static bool has_msr_mtrr
;
84 static bool has_msr_xss
;
86 static bool has_msr_architectural_pmu
;
87 static uint32_t num_architectural_pmu_counters
;
89 bool kvm_allows_irq0_override(void)
91 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
94 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
96 struct kvm_cpuid2
*cpuid
;
99 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
100 cpuid
= g_malloc0(size
);
102 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
103 if (r
== 0 && cpuid
->nent
>= max
) {
111 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
119 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
122 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
124 struct kvm_cpuid2
*cpuid
;
126 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
132 static const struct kvm_para_features
{
135 } para_features
[] = {
136 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
137 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
138 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
139 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
142 static int get_para_features(KVMState
*s
)
146 for (i
= 0; i
< ARRAY_SIZE(para_features
); i
++) {
147 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
148 features
|= (1 << para_features
[i
].feature
);
156 /* Returns the value for a specific register on the cpuid entry
158 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
178 /* Find matching entry for function/index on kvm_cpuid2 struct
180 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
185 for (i
= 0; i
< cpuid
->nent
; ++i
) {
186 if (cpuid
->entries
[i
].function
== function
&&
187 cpuid
->entries
[i
].index
== index
) {
188 return &cpuid
->entries
[i
];
195 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
196 uint32_t index
, int reg
)
198 struct kvm_cpuid2
*cpuid
;
200 uint32_t cpuid_1_edx
;
203 cpuid
= get_supported_cpuid(s
);
205 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
208 ret
= cpuid_entry_get_reg(entry
, reg
);
211 /* Fixups for the data returned by KVM, below */
213 if (function
== 1 && reg
== R_EDX
) {
214 /* KVM before 2.6.30 misreports the following features */
215 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
216 } else if (function
== 1 && reg
== R_ECX
) {
217 /* We can set the hypervisor flag, even if KVM does not return it on
218 * GET_SUPPORTED_CPUID
220 ret
|= CPUID_EXT_HYPERVISOR
;
221 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
222 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
223 * and the irqchip is in the kernel.
225 if (kvm_irqchip_in_kernel() &&
226 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
227 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
230 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
231 * without the in-kernel irqchip
233 if (!kvm_irqchip_in_kernel()) {
234 ret
&= ~CPUID_EXT_X2APIC
;
236 } else if (function
== 0x80000001 && reg
== R_EDX
) {
237 /* On Intel, kvm returns cpuid according to the Intel spec,
238 * so add missing bits according to the AMD spec:
240 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
241 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
246 /* fallback for older kernels */
247 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
248 ret
= get_para_features(s
);
254 typedef struct HWPoisonPage
{
256 QLIST_ENTRY(HWPoisonPage
) list
;
259 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
260 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
262 static void kvm_unpoison_all(void *param
)
264 HWPoisonPage
*page
, *next_page
;
266 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
267 QLIST_REMOVE(page
, list
);
268 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
273 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
277 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
278 if (page
->ram_addr
== ram_addr
) {
282 page
= g_new(HWPoisonPage
, 1);
283 page
->ram_addr
= ram_addr
;
284 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
287 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
292 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
295 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
300 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
302 CPUX86State
*env
= &cpu
->env
;
303 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
304 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
305 uint64_t mcg_status
= MCG_STATUS_MCIP
;
307 if (code
== BUS_MCEERR_AR
) {
308 status
|= MCI_STATUS_AR
| 0x134;
309 mcg_status
|= MCG_STATUS_EIPV
;
312 mcg_status
|= MCG_STATUS_RIPV
;
314 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
315 (MCM_ADDR_PHYS
<< 6) | 0xc,
316 cpu_x86_support_mca_broadcast(env
) ?
317 MCE_INJECT_BROADCAST
: 0);
320 static void hardware_memory_error(void)
322 fprintf(stderr
, "Hardware memory error!\n");
326 int kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
328 X86CPU
*cpu
= X86_CPU(c
);
329 CPUX86State
*env
= &cpu
->env
;
333 if ((env
->mcg_cap
& MCG_SER_P
) && addr
334 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
335 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
336 !kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
337 fprintf(stderr
, "Hardware memory error for memory used by "
338 "QEMU itself instead of guest system!\n");
339 /* Hope we are lucky for AO MCE */
340 if (code
== BUS_MCEERR_AO
) {
343 hardware_memory_error();
346 kvm_hwpoison_page_add(ram_addr
);
347 kvm_mce_inject(cpu
, paddr
, code
);
349 if (code
== BUS_MCEERR_AO
) {
351 } else if (code
== BUS_MCEERR_AR
) {
352 hardware_memory_error();
360 int kvm_arch_on_sigbus(int code
, void *addr
)
362 X86CPU
*cpu
= X86_CPU(first_cpu
);
364 if ((cpu
->env
.mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
368 /* Hope we are lucky for AO MCE */
369 if (qemu_ram_addr_from_host(addr
, &ram_addr
) == NULL
||
370 !kvm_physical_memory_addr_from_host(first_cpu
->kvm_state
,
372 fprintf(stderr
, "Hardware memory error for memory used by "
373 "QEMU itself instead of guest system!: %p\n", addr
);
376 kvm_hwpoison_page_add(ram_addr
);
377 kvm_mce_inject(X86_CPU(first_cpu
), paddr
, code
);
379 if (code
== BUS_MCEERR_AO
) {
381 } else if (code
== BUS_MCEERR_AR
) {
382 hardware_memory_error();
390 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
392 CPUX86State
*env
= &cpu
->env
;
394 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
395 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
396 struct kvm_x86_mce mce
;
398 env
->exception_injected
= -1;
401 * There must be at least one bank in use if an MCE is pending.
402 * Find it and use its values for the event injection.
404 for (bank
= 0; bank
< bank_num
; bank
++) {
405 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
409 assert(bank
< bank_num
);
412 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
413 mce
.mcg_status
= env
->mcg_status
;
414 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
415 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
417 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
422 static void cpu_update_state(void *opaque
, int running
, RunState state
)
424 CPUX86State
*env
= opaque
;
427 env
->tsc_valid
= false;
431 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
433 X86CPU
*cpu
= X86_CPU(cs
);
437 #ifndef KVM_CPUID_SIGNATURE_NEXT
438 #define KVM_CPUID_SIGNATURE_NEXT 0x40000100
441 static bool hyperv_hypercall_available(X86CPU
*cpu
)
443 return cpu
->hyperv_vapic
||
444 (cpu
->hyperv_spinlock_attempts
!= HYPERV_SPINLOCK_NEVER_RETRY
);
447 static bool hyperv_enabled(X86CPU
*cpu
)
449 CPUState
*cs
= CPU(cpu
);
450 return kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV
) > 0 &&
451 (hyperv_hypercall_available(cpu
) ||
453 cpu
->hyperv_relaxed_timing
);
456 static Error
*invtsc_mig_blocker
;
458 #define KVM_MAX_CPUID_ENTRIES 100
460 int kvm_arch_init_vcpu(CPUState
*cs
)
463 struct kvm_cpuid2 cpuid
;
464 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
465 } QEMU_PACKED cpuid_data
;
466 X86CPU
*cpu
= X86_CPU(cs
);
467 CPUX86State
*env
= &cpu
->env
;
468 uint32_t limit
, i
, j
, cpuid_i
;
470 struct kvm_cpuid_entry2
*c
;
471 uint32_t signature
[3];
472 int kvm_base
= KVM_CPUID_SIGNATURE
;
475 memset(&cpuid_data
, 0, sizeof(cpuid_data
));
479 /* Paravirtualization CPUIDs */
480 if (hyperv_enabled(cpu
)) {
481 c
= &cpuid_data
.entries
[cpuid_i
++];
482 c
->function
= HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS
;
483 memcpy(signature
, "Microsoft Hv", 12);
484 c
->eax
= HYPERV_CPUID_MIN
;
485 c
->ebx
= signature
[0];
486 c
->ecx
= signature
[1];
487 c
->edx
= signature
[2];
489 c
= &cpuid_data
.entries
[cpuid_i
++];
490 c
->function
= HYPERV_CPUID_INTERFACE
;
491 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
492 c
->eax
= signature
[0];
497 c
= &cpuid_data
.entries
[cpuid_i
++];
498 c
->function
= HYPERV_CPUID_VERSION
;
502 c
= &cpuid_data
.entries
[cpuid_i
++];
503 c
->function
= HYPERV_CPUID_FEATURES
;
504 if (cpu
->hyperv_relaxed_timing
) {
505 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
507 if (cpu
->hyperv_vapic
) {
508 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
509 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
510 has_msr_hv_vapic
= true;
512 if (cpu
->hyperv_time
&&
513 kvm_check_extension(cs
->kvm_state
, KVM_CAP_HYPERV_TIME
) > 0) {
514 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
515 c
->eax
|= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE
;
517 has_msr_hv_tsc
= true;
519 c
= &cpuid_data
.entries
[cpuid_i
++];
520 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
521 if (cpu
->hyperv_relaxed_timing
) {
522 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
524 if (has_msr_hv_vapic
) {
525 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
527 c
->ebx
= cpu
->hyperv_spinlock_attempts
;
529 c
= &cpuid_data
.entries
[cpuid_i
++];
530 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
534 kvm_base
= KVM_CPUID_SIGNATURE_NEXT
;
535 has_msr_hv_hypercall
= true;
538 if (cpu
->expose_kvm
) {
539 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
540 c
= &cpuid_data
.entries
[cpuid_i
++];
541 c
->function
= KVM_CPUID_SIGNATURE
| kvm_base
;
542 c
->eax
= KVM_CPUID_FEATURES
| kvm_base
;
543 c
->ebx
= signature
[0];
544 c
->ecx
= signature
[1];
545 c
->edx
= signature
[2];
547 c
= &cpuid_data
.entries
[cpuid_i
++];
548 c
->function
= KVM_CPUID_FEATURES
| kvm_base
;
549 c
->eax
= env
->features
[FEAT_KVM
];
551 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
553 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
555 has_msr_kvm_steal_time
= c
->eax
& (1 << KVM_FEATURE_STEAL_TIME
);
558 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
560 for (i
= 0; i
<= limit
; i
++) {
561 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
562 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
565 c
= &cpuid_data
.entries
[cpuid_i
++];
569 /* Keep reading function 2 till all the input is received */
573 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
574 KVM_CPUID_FLAG_STATE_READ_NEXT
;
575 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
576 times
= c
->eax
& 0xff;
578 for (j
= 1; j
< times
; ++j
) {
579 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
580 fprintf(stderr
, "cpuid_data is full, no space for "
581 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
584 c
= &cpuid_data
.entries
[cpuid_i
++];
586 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
587 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
595 if (i
== 0xd && j
== 64) {
599 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
601 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
603 if (i
== 4 && c
->eax
== 0) {
606 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
609 if (i
== 0xd && c
->eax
== 0) {
612 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
613 fprintf(stderr
, "cpuid_data is full, no space for "
614 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
617 c
= &cpuid_data
.entries
[cpuid_i
++];
623 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
631 cpu_x86_cpuid(env
, 0x0a, 0, &ver
, &unused
, &unused
, &unused
);
632 if ((ver
& 0xff) > 0) {
633 has_msr_architectural_pmu
= true;
634 num_architectural_pmu_counters
= (ver
& 0xff00) >> 8;
636 /* Shouldn't be more than 32, since that's the number of bits
637 * available in EBX to tell us _which_ counters are available.
640 if (num_architectural_pmu_counters
> MAX_GP_COUNTERS
) {
641 num_architectural_pmu_counters
= MAX_GP_COUNTERS
;
646 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
648 for (i
= 0x80000000; i
<= limit
; i
++) {
649 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
650 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
653 c
= &cpuid_data
.entries
[cpuid_i
++];
657 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
660 /* Call Centaur's CPUID instructions they are supported. */
661 if (env
->cpuid_xlevel2
> 0) {
662 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
664 for (i
= 0xC0000000; i
<= limit
; i
++) {
665 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
666 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
669 c
= &cpuid_data
.entries
[cpuid_i
++];
673 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
677 cpuid_data
.cpuid
.nent
= cpuid_i
;
679 if (((env
->cpuid_version
>> 8)&0xF) >= 6
680 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
681 (CPUID_MCE
| CPUID_MCA
)
682 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
687 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
689 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
693 if (banks
> MCE_BANKS_DEF
) {
694 banks
= MCE_BANKS_DEF
;
696 mcg_cap
&= MCE_CAP_DEF
;
698 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
700 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
704 env
->mcg_cap
= mcg_cap
;
707 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
709 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 1, 0);
711 has_msr_feature_control
= !!(c
->ecx
& CPUID_EXT_VMX
) ||
712 !!(c
->ecx
& CPUID_EXT_SMX
);
715 c
= cpuid_find_entry(&cpuid_data
.cpuid
, 0x80000007, 0);
716 if (c
&& (c
->edx
& 1<<8) && invtsc_mig_blocker
== NULL
) {
718 error_setg(&invtsc_mig_blocker
,
719 "State blocked by non-migratable CPU device"
721 migrate_add_blocker(invtsc_mig_blocker
);
723 vmstate_x86_cpu
.unmigratable
= 1;
726 cpuid_data
.cpuid
.padding
= 0;
727 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
732 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
733 if (r
&& env
->tsc_khz
) {
734 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
736 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
741 if (kvm_has_xsave()) {
742 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
745 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
752 void kvm_arch_reset_vcpu(X86CPU
*cpu
)
754 CPUX86State
*env
= &cpu
->env
;
756 env
->exception_injected
= -1;
757 env
->interrupt_injected
= -1;
759 if (kvm_irqchip_in_kernel()) {
760 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
761 KVM_MP_STATE_UNINITIALIZED
;
763 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
767 void kvm_arch_do_init_vcpu(X86CPU
*cpu
)
769 CPUX86State
*env
= &cpu
->env
;
771 /* APs get directly into wait-for-SIPI state. */
772 if (env
->mp_state
== KVM_MP_STATE_UNINITIALIZED
) {
773 env
->mp_state
= KVM_MP_STATE_INIT_RECEIVED
;
777 static int kvm_get_supported_msrs(KVMState
*s
)
779 static int kvm_supported_msrs
;
783 if (kvm_supported_msrs
== 0) {
784 struct kvm_msr_list msr_list
, *kvm_msr_list
;
786 kvm_supported_msrs
= -1;
788 /* Obtain MSR list from KVM. These are the MSRs that we must
791 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
792 if (ret
< 0 && ret
!= -E2BIG
) {
795 /* Old kernel modules had a bug and could write beyond the provided
796 memory. Allocate at least a safe amount of 1K. */
797 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
799 sizeof(msr_list
.indices
[0])));
801 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
802 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
806 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
807 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
811 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
812 has_msr_hsave_pa
= true;
815 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
816 has_msr_tsc_adjust
= true;
819 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
820 has_msr_tsc_deadline
= true;
823 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
824 has_msr_misc_enable
= true;
827 if (kvm_msr_list
->indices
[i
] == MSR_IA32_BNDCFGS
) {
828 has_msr_bndcfgs
= true;
831 if (kvm_msr_list
->indices
[i
] == MSR_IA32_XSS
) {
838 g_free(kvm_msr_list
);
844 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
846 uint64_t identity_base
= 0xfffbc000;
849 struct utsname utsname
;
851 ret
= kvm_get_supported_msrs(s
);
857 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
860 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
861 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
862 * Since these must be part of guest physical memory, we need to allocate
863 * them, both by setting their start addresses in the kernel and by
864 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
866 * Older KVM versions may not support setting the identity map base. In
867 * that case we need to stick with the default, i.e. a 256K maximum BIOS
870 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
871 /* Allows up to 16M BIOSes. */
872 identity_base
= 0xfeffc000;
874 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
880 /* Set TSS base one page after EPT identity map. */
881 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
886 /* Tell fw_cfg to notify the BIOS to reserve the range. */
887 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
889 fprintf(stderr
, "e820_add_entry() table is full\n");
892 qemu_register_reset(kvm_unpoison_all
, NULL
);
894 shadow_mem
= machine_kvm_shadow_mem(ms
);
895 if (shadow_mem
!= -1) {
897 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
905 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
907 lhs
->selector
= rhs
->selector
;
908 lhs
->base
= rhs
->base
;
909 lhs
->limit
= rhs
->limit
;
921 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
923 unsigned flags
= rhs
->flags
;
924 lhs
->selector
= rhs
->selector
;
925 lhs
->base
= rhs
->base
;
926 lhs
->limit
= rhs
->limit
;
927 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
928 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
929 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
930 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
931 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
932 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
933 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
934 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
939 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
941 lhs
->selector
= rhs
->selector
;
942 lhs
->base
= rhs
->base
;
943 lhs
->limit
= rhs
->limit
;
944 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
945 (rhs
->present
* DESC_P_MASK
) |
946 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
947 (rhs
->db
<< DESC_B_SHIFT
) |
948 (rhs
->s
* DESC_S_MASK
) |
949 (rhs
->l
<< DESC_L_SHIFT
) |
950 (rhs
->g
* DESC_G_MASK
) |
951 (rhs
->avl
* DESC_AVL_MASK
);
954 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
957 *kvm_reg
= *qemu_reg
;
959 *qemu_reg
= *kvm_reg
;
963 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
965 CPUX86State
*env
= &cpu
->env
;
966 struct kvm_regs regs
;
970 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
976 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
977 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
978 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
979 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
980 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
981 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
982 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
983 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
985 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
986 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
987 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
988 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
989 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
990 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
991 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
992 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
995 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
996 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
999 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
1005 static int kvm_put_fpu(X86CPU
*cpu
)
1007 CPUX86State
*env
= &cpu
->env
;
1011 memset(&fpu
, 0, sizeof fpu
);
1012 fpu
.fsw
= env
->fpus
& ~(7 << 11);
1013 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
1014 fpu
.fcw
= env
->fpuc
;
1015 fpu
.last_opcode
= env
->fpop
;
1016 fpu
.last_ip
= env
->fpip
;
1017 fpu
.last_dp
= env
->fpdp
;
1018 for (i
= 0; i
< 8; ++i
) {
1019 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
1021 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
1022 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
1023 stq_p(&fpu
.xmm
[i
][0], env
->xmm_regs
[i
].XMM_Q(0));
1024 stq_p(&fpu
.xmm
[i
][8], env
->xmm_regs
[i
].XMM_Q(1));
1026 fpu
.mxcsr
= env
->mxcsr
;
1028 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
1031 #define XSAVE_FCW_FSW 0
1032 #define XSAVE_FTW_FOP 1
1033 #define XSAVE_CWD_RIP 2
1034 #define XSAVE_CWD_RDP 4
1035 #define XSAVE_MXCSR 6
1036 #define XSAVE_ST_SPACE 8
1037 #define XSAVE_XMM_SPACE 40
1038 #define XSAVE_XSTATE_BV 128
1039 #define XSAVE_YMMH_SPACE 144
1040 #define XSAVE_BNDREGS 240
1041 #define XSAVE_BNDCSR 256
1042 #define XSAVE_OPMASK 272
1043 #define XSAVE_ZMM_Hi256 288
1044 #define XSAVE_Hi16_ZMM 416
1046 static int kvm_put_xsave(X86CPU
*cpu
)
1048 CPUX86State
*env
= &cpu
->env
;
1049 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1050 uint16_t cwd
, swd
, twd
;
1051 uint8_t *xmm
, *ymmh
, *zmmh
;
1054 if (!kvm_has_xsave()) {
1055 return kvm_put_fpu(cpu
);
1058 memset(xsave
, 0, sizeof(struct kvm_xsave
));
1060 swd
= env
->fpus
& ~(7 << 11);
1061 swd
|= (env
->fpstt
& 7) << 11;
1063 for (i
= 0; i
< 8; ++i
) {
1064 twd
|= (!env
->fptags
[i
]) << i
;
1066 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
1067 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
1068 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
1069 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
1070 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
1071 sizeof env
->fpregs
);
1072 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
1073 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
1074 memcpy(&xsave
->region
[XSAVE_BNDREGS
], env
->bnd_regs
,
1075 sizeof env
->bnd_regs
);
1076 memcpy(&xsave
->region
[XSAVE_BNDCSR
], &env
->bndcs_regs
,
1077 sizeof(env
->bndcs_regs
));
1078 memcpy(&xsave
->region
[XSAVE_OPMASK
], env
->opmask_regs
,
1079 sizeof env
->opmask_regs
);
1081 xmm
= (uint8_t *)&xsave
->region
[XSAVE_XMM_SPACE
];
1082 ymmh
= (uint8_t *)&xsave
->region
[XSAVE_YMMH_SPACE
];
1083 zmmh
= (uint8_t *)&xsave
->region
[XSAVE_ZMM_Hi256
];
1084 for (i
= 0; i
< CPU_NB_REGS
; i
++, xmm
+= 16, ymmh
+= 16, zmmh
+= 32) {
1085 stq_p(xmm
, env
->xmm_regs
[i
].XMM_Q(0));
1086 stq_p(xmm
+8, env
->xmm_regs
[i
].XMM_Q(1));
1087 stq_p(ymmh
, env
->xmm_regs
[i
].XMM_Q(2));
1088 stq_p(ymmh
+8, env
->xmm_regs
[i
].XMM_Q(3));
1089 stq_p(zmmh
, env
->xmm_regs
[i
].XMM_Q(4));
1090 stq_p(zmmh
+8, env
->xmm_regs
[i
].XMM_Q(5));
1091 stq_p(zmmh
+16, env
->xmm_regs
[i
].XMM_Q(6));
1092 stq_p(zmmh
+24, env
->xmm_regs
[i
].XMM_Q(7));
1095 #ifdef TARGET_X86_64
1096 memcpy(&xsave
->region
[XSAVE_Hi16_ZMM
], &env
->xmm_regs
[16],
1097 16 * sizeof env
->xmm_regs
[16]);
1099 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
1103 static int kvm_put_xcrs(X86CPU
*cpu
)
1105 CPUX86State
*env
= &cpu
->env
;
1106 struct kvm_xcrs xcrs
= {};
1108 if (!kvm_has_xcrs()) {
1114 xcrs
.xcrs
[0].xcr
= 0;
1115 xcrs
.xcrs
[0].value
= env
->xcr0
;
1116 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
1119 static int kvm_put_sregs(X86CPU
*cpu
)
1121 CPUX86State
*env
= &cpu
->env
;
1122 struct kvm_sregs sregs
;
1124 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
1125 if (env
->interrupt_injected
>= 0) {
1126 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
1127 (uint64_t)1 << (env
->interrupt_injected
% 64);
1130 if ((env
->eflags
& VM_MASK
)) {
1131 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1132 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1133 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1134 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1135 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1136 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1138 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1139 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1140 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1141 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1142 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1143 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1146 set_seg(&sregs
.tr
, &env
->tr
);
1147 set_seg(&sregs
.ldt
, &env
->ldt
);
1149 sregs
.idt
.limit
= env
->idt
.limit
;
1150 sregs
.idt
.base
= env
->idt
.base
;
1151 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1152 sregs
.gdt
.limit
= env
->gdt
.limit
;
1153 sregs
.gdt
.base
= env
->gdt
.base
;
1154 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1156 sregs
.cr0
= env
->cr
[0];
1157 sregs
.cr2
= env
->cr
[2];
1158 sregs
.cr3
= env
->cr
[3];
1159 sregs
.cr4
= env
->cr
[4];
1161 sregs
.cr8
= cpu_get_apic_tpr(cpu
->apic_state
);
1162 sregs
.apic_base
= cpu_get_apic_base(cpu
->apic_state
);
1164 sregs
.efer
= env
->efer
;
1166 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1169 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1170 uint32_t index
, uint64_t value
)
1172 entry
->index
= index
;
1173 entry
->reserved
= 0;
1174 entry
->data
= value
;
1177 static int kvm_put_tscdeadline_msr(X86CPU
*cpu
)
1179 CPUX86State
*env
= &cpu
->env
;
1181 struct kvm_msrs info
;
1182 struct kvm_msr_entry entries
[1];
1184 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1186 if (!has_msr_tsc_deadline
) {
1190 kvm_msr_entry_set(&msrs
[0], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1192 msr_data
.info
= (struct kvm_msrs
) {
1196 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1200 * Provide a separate write service for the feature control MSR in order to
1201 * kick the VCPU out of VMXON or even guest mode on reset. This has to be done
1202 * before writing any other state because forcibly leaving nested mode
1203 * invalidates the VCPU state.
1205 static int kvm_put_msr_feature_control(X86CPU
*cpu
)
1208 struct kvm_msrs info
;
1209 struct kvm_msr_entry entry
;
1212 kvm_msr_entry_set(&msr_data
.entry
, MSR_IA32_FEATURE_CONTROL
,
1213 cpu
->env
.msr_ia32_feature_control
);
1215 msr_data
.info
= (struct kvm_msrs
) {
1219 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1222 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1224 CPUX86State
*env
= &cpu
->env
;
1226 struct kvm_msrs info
;
1227 struct kvm_msr_entry entries
[150];
1229 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1232 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1233 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1234 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1235 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1237 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1239 if (has_msr_hsave_pa
) {
1240 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1242 if (has_msr_tsc_adjust
) {
1243 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1245 if (has_msr_misc_enable
) {
1246 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1247 env
->msr_ia32_misc_enable
);
1249 if (has_msr_bndcfgs
) {
1250 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_BNDCFGS
, env
->msr_bndcfgs
);
1253 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_XSS
, env
->xss
);
1255 #ifdef TARGET_X86_64
1256 if (lm_capable_kernel
) {
1257 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1258 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1259 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1260 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1264 * The following MSRs have side effects on the guest or are too heavy
1265 * for normal writeback. Limit them to reset or full state updates.
1267 if (level
>= KVM_PUT_RESET_STATE
) {
1268 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1269 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1270 env
->system_time_msr
);
1271 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1272 if (has_msr_async_pf_en
) {
1273 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1274 env
->async_pf_en_msr
);
1276 if (has_msr_pv_eoi_en
) {
1277 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1278 env
->pv_eoi_en_msr
);
1280 if (has_msr_kvm_steal_time
) {
1281 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_STEAL_TIME
,
1282 env
->steal_time_msr
);
1284 if (has_msr_architectural_pmu
) {
1285 /* Stop the counter. */
1286 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
, 0);
1287 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
, 0);
1289 /* Set the counter values. */
1290 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1291 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR0
+ i
,
1292 env
->msr_fixed_counters
[i
]);
1294 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1295 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_PERFCTR0
+ i
,
1296 env
->msr_gp_counters
[i
]);
1297 kvm_msr_entry_set(&msrs
[n
++], MSR_P6_EVNTSEL0
+ i
,
1298 env
->msr_gp_evtsel
[i
]);
1300 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_STATUS
,
1301 env
->msr_global_status
);
1302 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_OVF_CTRL
,
1303 env
->msr_global_ovf_ctrl
);
1305 /* Now start the PMU. */
1306 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_FIXED_CTR_CTRL
,
1307 env
->msr_fixed_ctr_ctrl
);
1308 kvm_msr_entry_set(&msrs
[n
++], MSR_CORE_PERF_GLOBAL_CTRL
,
1309 env
->msr_global_ctrl
);
1311 if (has_msr_hv_hypercall
) {
1312 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
,
1313 env
->msr_hv_guest_os_id
);
1314 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
,
1315 env
->msr_hv_hypercall
);
1317 if (has_msr_hv_vapic
) {
1318 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
,
1321 if (has_msr_hv_tsc
) {
1322 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_REFERENCE_TSC
,
1326 kvm_msr_entry_set(&msrs
[n
++], MSR_MTRRdefType
, env
->mtrr_deftype
);
1327 kvm_msr_entry_set(&msrs
[n
++],
1328 MSR_MTRRfix64K_00000
, env
->mtrr_fixed
[0]);
1329 kvm_msr_entry_set(&msrs
[n
++],
1330 MSR_MTRRfix16K_80000
, env
->mtrr_fixed
[1]);
1331 kvm_msr_entry_set(&msrs
[n
++],
1332 MSR_MTRRfix16K_A0000
, env
->mtrr_fixed
[2]);
1333 kvm_msr_entry_set(&msrs
[n
++],
1334 MSR_MTRRfix4K_C0000
, env
->mtrr_fixed
[3]);
1335 kvm_msr_entry_set(&msrs
[n
++],
1336 MSR_MTRRfix4K_C8000
, env
->mtrr_fixed
[4]);
1337 kvm_msr_entry_set(&msrs
[n
++],
1338 MSR_MTRRfix4K_D0000
, env
->mtrr_fixed
[5]);
1339 kvm_msr_entry_set(&msrs
[n
++],
1340 MSR_MTRRfix4K_D8000
, env
->mtrr_fixed
[6]);
1341 kvm_msr_entry_set(&msrs
[n
++],
1342 MSR_MTRRfix4K_E0000
, env
->mtrr_fixed
[7]);
1343 kvm_msr_entry_set(&msrs
[n
++],
1344 MSR_MTRRfix4K_E8000
, env
->mtrr_fixed
[8]);
1345 kvm_msr_entry_set(&msrs
[n
++],
1346 MSR_MTRRfix4K_F0000
, env
->mtrr_fixed
[9]);
1347 kvm_msr_entry_set(&msrs
[n
++],
1348 MSR_MTRRfix4K_F8000
, env
->mtrr_fixed
[10]);
1349 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
1350 kvm_msr_entry_set(&msrs
[n
++],
1351 MSR_MTRRphysBase(i
), env
->mtrr_var
[i
].base
);
1352 kvm_msr_entry_set(&msrs
[n
++],
1353 MSR_MTRRphysMask(i
), env
->mtrr_var
[i
].mask
);
1357 /* Note: MSR_IA32_FEATURE_CONTROL is written separately, see
1358 * kvm_put_msr_feature_control. */
1363 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1364 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1365 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1366 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1370 msr_data
.info
= (struct kvm_msrs
) {
1374 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1379 static int kvm_get_fpu(X86CPU
*cpu
)
1381 CPUX86State
*env
= &cpu
->env
;
1385 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1390 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1391 env
->fpus
= fpu
.fsw
;
1392 env
->fpuc
= fpu
.fcw
;
1393 env
->fpop
= fpu
.last_opcode
;
1394 env
->fpip
= fpu
.last_ip
;
1395 env
->fpdp
= fpu
.last_dp
;
1396 for (i
= 0; i
< 8; ++i
) {
1397 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1399 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1400 for (i
= 0; i
< CPU_NB_REGS
; i
++) {
1401 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(&fpu
.xmm
[i
][0]);
1402 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(&fpu
.xmm
[i
][8]);
1404 env
->mxcsr
= fpu
.mxcsr
;
1409 static int kvm_get_xsave(X86CPU
*cpu
)
1411 CPUX86State
*env
= &cpu
->env
;
1412 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1414 const uint8_t *xmm
, *ymmh
, *zmmh
;
1415 uint16_t cwd
, swd
, twd
;
1417 if (!kvm_has_xsave()) {
1418 return kvm_get_fpu(cpu
);
1421 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1426 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1427 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1428 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1429 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1430 env
->fpstt
= (swd
>> 11) & 7;
1433 for (i
= 0; i
< 8; ++i
) {
1434 env
->fptags
[i
] = !((twd
>> i
) & 1);
1436 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1437 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1438 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1439 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1440 sizeof env
->fpregs
);
1441 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1442 memcpy(env
->bnd_regs
, &xsave
->region
[XSAVE_BNDREGS
],
1443 sizeof env
->bnd_regs
);
1444 memcpy(&env
->bndcs_regs
, &xsave
->region
[XSAVE_BNDCSR
],
1445 sizeof(env
->bndcs_regs
));
1446 memcpy(env
->opmask_regs
, &xsave
->region
[XSAVE_OPMASK
],
1447 sizeof env
->opmask_regs
);
1449 xmm
= (const uint8_t *)&xsave
->region
[XSAVE_XMM_SPACE
];
1450 ymmh
= (const uint8_t *)&xsave
->region
[XSAVE_YMMH_SPACE
];
1451 zmmh
= (const uint8_t *)&xsave
->region
[XSAVE_ZMM_Hi256
];
1452 for (i
= 0; i
< CPU_NB_REGS
; i
++, xmm
+= 16, ymmh
+= 16, zmmh
+= 32) {
1453 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(xmm
);
1454 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(xmm
+8);
1455 env
->xmm_regs
[i
].XMM_Q(2) = ldq_p(ymmh
);
1456 env
->xmm_regs
[i
].XMM_Q(3) = ldq_p(ymmh
+8);
1457 env
->xmm_regs
[i
].XMM_Q(4) = ldq_p(zmmh
);
1458 env
->xmm_regs
[i
].XMM_Q(5) = ldq_p(zmmh
+8);
1459 env
->xmm_regs
[i
].XMM_Q(6) = ldq_p(zmmh
+16);
1460 env
->xmm_regs
[i
].XMM_Q(7) = ldq_p(zmmh
+24);
1463 #ifdef TARGET_X86_64
1464 memcpy(&env
->xmm_regs
[16], &xsave
->region
[XSAVE_Hi16_ZMM
],
1465 16 * sizeof env
->xmm_regs
[16]);
1470 static int kvm_get_xcrs(X86CPU
*cpu
)
1472 CPUX86State
*env
= &cpu
->env
;
1474 struct kvm_xcrs xcrs
;
1476 if (!kvm_has_xcrs()) {
1480 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1485 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1486 /* Only support xcr0 now */
1487 if (xcrs
.xcrs
[i
].xcr
== 0) {
1488 env
->xcr0
= xcrs
.xcrs
[i
].value
;
1495 static int kvm_get_sregs(X86CPU
*cpu
)
1497 CPUX86State
*env
= &cpu
->env
;
1498 struct kvm_sregs sregs
;
1502 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1507 /* There can only be one pending IRQ set in the bitmap at a time, so try
1508 to find it and save its number instead (-1 for none). */
1509 env
->interrupt_injected
= -1;
1510 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1511 if (sregs
.interrupt_bitmap
[i
]) {
1512 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1513 env
->interrupt_injected
= i
* 64 + bit
;
1518 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1519 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1520 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1521 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1522 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1523 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1525 get_seg(&env
->tr
, &sregs
.tr
);
1526 get_seg(&env
->ldt
, &sregs
.ldt
);
1528 env
->idt
.limit
= sregs
.idt
.limit
;
1529 env
->idt
.base
= sregs
.idt
.base
;
1530 env
->gdt
.limit
= sregs
.gdt
.limit
;
1531 env
->gdt
.base
= sregs
.gdt
.base
;
1533 env
->cr
[0] = sregs
.cr0
;
1534 env
->cr
[2] = sregs
.cr2
;
1535 env
->cr
[3] = sregs
.cr3
;
1536 env
->cr
[4] = sregs
.cr4
;
1538 env
->efer
= sregs
.efer
;
1540 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1542 #define HFLAG_COPY_MASK \
1543 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1544 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1545 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1546 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1548 hflags
= (env
->segs
[R_SS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1549 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1550 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1551 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1552 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1553 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1554 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1556 if (env
->efer
& MSR_EFER_LMA
) {
1557 hflags
|= HF_LMA_MASK
;
1560 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1561 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1563 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1564 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1565 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1566 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1567 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1568 !(hflags
& HF_CS32_MASK
)) {
1569 hflags
|= HF_ADDSEG_MASK
;
1571 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1572 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1575 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1580 static int kvm_get_msrs(X86CPU
*cpu
)
1582 CPUX86State
*env
= &cpu
->env
;
1584 struct kvm_msrs info
;
1585 struct kvm_msr_entry entries
[150];
1587 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1591 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1592 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1593 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1594 msrs
[n
++].index
= MSR_PAT
;
1596 msrs
[n
++].index
= MSR_STAR
;
1598 if (has_msr_hsave_pa
) {
1599 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1601 if (has_msr_tsc_adjust
) {
1602 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1604 if (has_msr_tsc_deadline
) {
1605 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1607 if (has_msr_misc_enable
) {
1608 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1610 if (has_msr_feature_control
) {
1611 msrs
[n
++].index
= MSR_IA32_FEATURE_CONTROL
;
1613 if (has_msr_bndcfgs
) {
1614 msrs
[n
++].index
= MSR_IA32_BNDCFGS
;
1617 msrs
[n
++].index
= MSR_IA32_XSS
;
1621 if (!env
->tsc_valid
) {
1622 msrs
[n
++].index
= MSR_IA32_TSC
;
1623 env
->tsc_valid
= !runstate_is_running();
1626 #ifdef TARGET_X86_64
1627 if (lm_capable_kernel
) {
1628 msrs
[n
++].index
= MSR_CSTAR
;
1629 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1630 msrs
[n
++].index
= MSR_FMASK
;
1631 msrs
[n
++].index
= MSR_LSTAR
;
1634 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1635 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1636 if (has_msr_async_pf_en
) {
1637 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1639 if (has_msr_pv_eoi_en
) {
1640 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1642 if (has_msr_kvm_steal_time
) {
1643 msrs
[n
++].index
= MSR_KVM_STEAL_TIME
;
1645 if (has_msr_architectural_pmu
) {
1646 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR_CTRL
;
1647 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_CTRL
;
1648 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_STATUS
;
1649 msrs
[n
++].index
= MSR_CORE_PERF_GLOBAL_OVF_CTRL
;
1650 for (i
= 0; i
< MAX_FIXED_COUNTERS
; i
++) {
1651 msrs
[n
++].index
= MSR_CORE_PERF_FIXED_CTR0
+ i
;
1653 for (i
= 0; i
< num_architectural_pmu_counters
; i
++) {
1654 msrs
[n
++].index
= MSR_P6_PERFCTR0
+ i
;
1655 msrs
[n
++].index
= MSR_P6_EVNTSEL0
+ i
;
1660 msrs
[n
++].index
= MSR_MCG_STATUS
;
1661 msrs
[n
++].index
= MSR_MCG_CTL
;
1662 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1663 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1667 if (has_msr_hv_hypercall
) {
1668 msrs
[n
++].index
= HV_X64_MSR_HYPERCALL
;
1669 msrs
[n
++].index
= HV_X64_MSR_GUEST_OS_ID
;
1671 if (has_msr_hv_vapic
) {
1672 msrs
[n
++].index
= HV_X64_MSR_APIC_ASSIST_PAGE
;
1674 if (has_msr_hv_tsc
) {
1675 msrs
[n
++].index
= HV_X64_MSR_REFERENCE_TSC
;
1678 msrs
[n
++].index
= MSR_MTRRdefType
;
1679 msrs
[n
++].index
= MSR_MTRRfix64K_00000
;
1680 msrs
[n
++].index
= MSR_MTRRfix16K_80000
;
1681 msrs
[n
++].index
= MSR_MTRRfix16K_A0000
;
1682 msrs
[n
++].index
= MSR_MTRRfix4K_C0000
;
1683 msrs
[n
++].index
= MSR_MTRRfix4K_C8000
;
1684 msrs
[n
++].index
= MSR_MTRRfix4K_D0000
;
1685 msrs
[n
++].index
= MSR_MTRRfix4K_D8000
;
1686 msrs
[n
++].index
= MSR_MTRRfix4K_E0000
;
1687 msrs
[n
++].index
= MSR_MTRRfix4K_E8000
;
1688 msrs
[n
++].index
= MSR_MTRRfix4K_F0000
;
1689 msrs
[n
++].index
= MSR_MTRRfix4K_F8000
;
1690 for (i
= 0; i
< MSR_MTRRcap_VCNT
; i
++) {
1691 msrs
[n
++].index
= MSR_MTRRphysBase(i
);
1692 msrs
[n
++].index
= MSR_MTRRphysMask(i
);
1696 msr_data
.info
= (struct kvm_msrs
) {
1700 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1705 for (i
= 0; i
< ret
; i
++) {
1706 uint32_t index
= msrs
[i
].index
;
1708 case MSR_IA32_SYSENTER_CS
:
1709 env
->sysenter_cs
= msrs
[i
].data
;
1711 case MSR_IA32_SYSENTER_ESP
:
1712 env
->sysenter_esp
= msrs
[i
].data
;
1714 case MSR_IA32_SYSENTER_EIP
:
1715 env
->sysenter_eip
= msrs
[i
].data
;
1718 env
->pat
= msrs
[i
].data
;
1721 env
->star
= msrs
[i
].data
;
1723 #ifdef TARGET_X86_64
1725 env
->cstar
= msrs
[i
].data
;
1727 case MSR_KERNELGSBASE
:
1728 env
->kernelgsbase
= msrs
[i
].data
;
1731 env
->fmask
= msrs
[i
].data
;
1734 env
->lstar
= msrs
[i
].data
;
1738 env
->tsc
= msrs
[i
].data
;
1740 case MSR_TSC_ADJUST
:
1741 env
->tsc_adjust
= msrs
[i
].data
;
1743 case MSR_IA32_TSCDEADLINE
:
1744 env
->tsc_deadline
= msrs
[i
].data
;
1746 case MSR_VM_HSAVE_PA
:
1747 env
->vm_hsave
= msrs
[i
].data
;
1749 case MSR_KVM_SYSTEM_TIME
:
1750 env
->system_time_msr
= msrs
[i
].data
;
1752 case MSR_KVM_WALL_CLOCK
:
1753 env
->wall_clock_msr
= msrs
[i
].data
;
1755 case MSR_MCG_STATUS
:
1756 env
->mcg_status
= msrs
[i
].data
;
1759 env
->mcg_ctl
= msrs
[i
].data
;
1761 case MSR_IA32_MISC_ENABLE
:
1762 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1764 case MSR_IA32_FEATURE_CONTROL
:
1765 env
->msr_ia32_feature_control
= msrs
[i
].data
;
1767 case MSR_IA32_BNDCFGS
:
1768 env
->msr_bndcfgs
= msrs
[i
].data
;
1771 env
->xss
= msrs
[i
].data
;
1774 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1775 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1776 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1779 case MSR_KVM_ASYNC_PF_EN
:
1780 env
->async_pf_en_msr
= msrs
[i
].data
;
1782 case MSR_KVM_PV_EOI_EN
:
1783 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1785 case MSR_KVM_STEAL_TIME
:
1786 env
->steal_time_msr
= msrs
[i
].data
;
1788 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
1789 env
->msr_fixed_ctr_ctrl
= msrs
[i
].data
;
1791 case MSR_CORE_PERF_GLOBAL_CTRL
:
1792 env
->msr_global_ctrl
= msrs
[i
].data
;
1794 case MSR_CORE_PERF_GLOBAL_STATUS
:
1795 env
->msr_global_status
= msrs
[i
].data
;
1797 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
1798 env
->msr_global_ovf_ctrl
= msrs
[i
].data
;
1800 case MSR_CORE_PERF_FIXED_CTR0
... MSR_CORE_PERF_FIXED_CTR0
+ MAX_FIXED_COUNTERS
- 1:
1801 env
->msr_fixed_counters
[index
- MSR_CORE_PERF_FIXED_CTR0
] = msrs
[i
].data
;
1803 case MSR_P6_PERFCTR0
... MSR_P6_PERFCTR0
+ MAX_GP_COUNTERS
- 1:
1804 env
->msr_gp_counters
[index
- MSR_P6_PERFCTR0
] = msrs
[i
].data
;
1806 case MSR_P6_EVNTSEL0
... MSR_P6_EVNTSEL0
+ MAX_GP_COUNTERS
- 1:
1807 env
->msr_gp_evtsel
[index
- MSR_P6_EVNTSEL0
] = msrs
[i
].data
;
1809 case HV_X64_MSR_HYPERCALL
:
1810 env
->msr_hv_hypercall
= msrs
[i
].data
;
1812 case HV_X64_MSR_GUEST_OS_ID
:
1813 env
->msr_hv_guest_os_id
= msrs
[i
].data
;
1815 case HV_X64_MSR_APIC_ASSIST_PAGE
:
1816 env
->msr_hv_vapic
= msrs
[i
].data
;
1818 case HV_X64_MSR_REFERENCE_TSC
:
1819 env
->msr_hv_tsc
= msrs
[i
].data
;
1821 case MSR_MTRRdefType
:
1822 env
->mtrr_deftype
= msrs
[i
].data
;
1824 case MSR_MTRRfix64K_00000
:
1825 env
->mtrr_fixed
[0] = msrs
[i
].data
;
1827 case MSR_MTRRfix16K_80000
:
1828 env
->mtrr_fixed
[1] = msrs
[i
].data
;
1830 case MSR_MTRRfix16K_A0000
:
1831 env
->mtrr_fixed
[2] = msrs
[i
].data
;
1833 case MSR_MTRRfix4K_C0000
:
1834 env
->mtrr_fixed
[3] = msrs
[i
].data
;
1836 case MSR_MTRRfix4K_C8000
:
1837 env
->mtrr_fixed
[4] = msrs
[i
].data
;
1839 case MSR_MTRRfix4K_D0000
:
1840 env
->mtrr_fixed
[5] = msrs
[i
].data
;
1842 case MSR_MTRRfix4K_D8000
:
1843 env
->mtrr_fixed
[6] = msrs
[i
].data
;
1845 case MSR_MTRRfix4K_E0000
:
1846 env
->mtrr_fixed
[7] = msrs
[i
].data
;
1848 case MSR_MTRRfix4K_E8000
:
1849 env
->mtrr_fixed
[8] = msrs
[i
].data
;
1851 case MSR_MTRRfix4K_F0000
:
1852 env
->mtrr_fixed
[9] = msrs
[i
].data
;
1854 case MSR_MTRRfix4K_F8000
:
1855 env
->mtrr_fixed
[10] = msrs
[i
].data
;
1857 case MSR_MTRRphysBase(0) ... MSR_MTRRphysMask(MSR_MTRRcap_VCNT
- 1):
1859 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].mask
= msrs
[i
].data
;
1861 env
->mtrr_var
[MSR_MTRRphysIndex(index
)].base
= msrs
[i
].data
;
1870 static int kvm_put_mp_state(X86CPU
*cpu
)
1872 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1874 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1877 static int kvm_get_mp_state(X86CPU
*cpu
)
1879 CPUState
*cs
= CPU(cpu
);
1880 CPUX86State
*env
= &cpu
->env
;
1881 struct kvm_mp_state mp_state
;
1884 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
1888 env
->mp_state
= mp_state
.mp_state
;
1889 if (kvm_irqchip_in_kernel()) {
1890 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1895 static int kvm_get_apic(X86CPU
*cpu
)
1897 DeviceState
*apic
= cpu
->apic_state
;
1898 struct kvm_lapic_state kapic
;
1901 if (apic
&& kvm_irqchip_in_kernel()) {
1902 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1907 kvm_get_apic_state(apic
, &kapic
);
1912 static int kvm_put_apic(X86CPU
*cpu
)
1914 DeviceState
*apic
= cpu
->apic_state
;
1915 struct kvm_lapic_state kapic
;
1917 if (apic
&& kvm_irqchip_in_kernel()) {
1918 kvm_put_apic_state(apic
, &kapic
);
1920 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1925 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1927 CPUX86State
*env
= &cpu
->env
;
1928 struct kvm_vcpu_events events
= {};
1930 if (!kvm_has_vcpu_events()) {
1934 events
.exception
.injected
= (env
->exception_injected
>= 0);
1935 events
.exception
.nr
= env
->exception_injected
;
1936 events
.exception
.has_error_code
= env
->has_error_code
;
1937 events
.exception
.error_code
= env
->error_code
;
1938 events
.exception
.pad
= 0;
1940 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1941 events
.interrupt
.nr
= env
->interrupt_injected
;
1942 events
.interrupt
.soft
= env
->soft_interrupt
;
1944 events
.nmi
.injected
= env
->nmi_injected
;
1945 events
.nmi
.pending
= env
->nmi_pending
;
1946 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1949 events
.sipi_vector
= env
->sipi_vector
;
1952 if (level
>= KVM_PUT_RESET_STATE
) {
1954 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1957 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1960 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1962 CPUX86State
*env
= &cpu
->env
;
1963 struct kvm_vcpu_events events
;
1966 if (!kvm_has_vcpu_events()) {
1970 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1974 env
->exception_injected
=
1975 events
.exception
.injected
? events
.exception
.nr
: -1;
1976 env
->has_error_code
= events
.exception
.has_error_code
;
1977 env
->error_code
= events
.exception
.error_code
;
1979 env
->interrupt_injected
=
1980 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1981 env
->soft_interrupt
= events
.interrupt
.soft
;
1983 env
->nmi_injected
= events
.nmi
.injected
;
1984 env
->nmi_pending
= events
.nmi
.pending
;
1985 if (events
.nmi
.masked
) {
1986 env
->hflags2
|= HF2_NMI_MASK
;
1988 env
->hflags2
&= ~HF2_NMI_MASK
;
1991 env
->sipi_vector
= events
.sipi_vector
;
1996 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1998 CPUState
*cs
= CPU(cpu
);
1999 CPUX86State
*env
= &cpu
->env
;
2001 unsigned long reinject_trap
= 0;
2003 if (!kvm_has_vcpu_events()) {
2004 if (env
->exception_injected
== 1) {
2005 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
2006 } else if (env
->exception_injected
== 3) {
2007 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
2009 env
->exception_injected
= -1;
2013 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
2014 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
2015 * by updating the debug state once again if single-stepping is on.
2016 * Another reason to call kvm_update_guest_debug here is a pending debug
2017 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
2018 * reinject them via SET_GUEST_DEBUG.
2020 if (reinject_trap
||
2021 (!kvm_has_robust_singlestep() && cs
->singlestep_enabled
)) {
2022 ret
= kvm_update_guest_debug(cs
, reinject_trap
);
2027 static int kvm_put_debugregs(X86CPU
*cpu
)
2029 CPUX86State
*env
= &cpu
->env
;
2030 struct kvm_debugregs dbgregs
;
2033 if (!kvm_has_debugregs()) {
2037 for (i
= 0; i
< 4; i
++) {
2038 dbgregs
.db
[i
] = env
->dr
[i
];
2040 dbgregs
.dr6
= env
->dr
[6];
2041 dbgregs
.dr7
= env
->dr
[7];
2044 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
2047 static int kvm_get_debugregs(X86CPU
*cpu
)
2049 CPUX86State
*env
= &cpu
->env
;
2050 struct kvm_debugregs dbgregs
;
2053 if (!kvm_has_debugregs()) {
2057 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
2061 for (i
= 0; i
< 4; i
++) {
2062 env
->dr
[i
] = dbgregs
.db
[i
];
2064 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
2065 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
2070 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
2072 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2075 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
2077 if (level
>= KVM_PUT_RESET_STATE
&& has_msr_feature_control
) {
2078 ret
= kvm_put_msr_feature_control(x86_cpu
);
2084 ret
= kvm_getput_regs(x86_cpu
, 1);
2088 ret
= kvm_put_xsave(x86_cpu
);
2092 ret
= kvm_put_xcrs(x86_cpu
);
2096 ret
= kvm_put_sregs(x86_cpu
);
2100 /* must be before kvm_put_msrs */
2101 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
2105 ret
= kvm_put_msrs(x86_cpu
, level
);
2109 if (level
>= KVM_PUT_RESET_STATE
) {
2110 ret
= kvm_put_mp_state(x86_cpu
);
2114 ret
= kvm_put_apic(x86_cpu
);
2120 ret
= kvm_put_tscdeadline_msr(x86_cpu
);
2125 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
2129 ret
= kvm_put_debugregs(x86_cpu
);
2134 ret
= kvm_guest_debug_workarounds(x86_cpu
);
2141 int kvm_arch_get_registers(CPUState
*cs
)
2143 X86CPU
*cpu
= X86_CPU(cs
);
2146 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
2148 ret
= kvm_getput_regs(cpu
, 0);
2152 ret
= kvm_get_xsave(cpu
);
2156 ret
= kvm_get_xcrs(cpu
);
2160 ret
= kvm_get_sregs(cpu
);
2164 ret
= kvm_get_msrs(cpu
);
2168 ret
= kvm_get_mp_state(cpu
);
2172 ret
= kvm_get_apic(cpu
);
2176 ret
= kvm_get_vcpu_events(cpu
);
2180 ret
= kvm_get_debugregs(cpu
);
2187 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
2189 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2190 CPUX86State
*env
= &x86_cpu
->env
;
2194 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
2195 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
2196 DPRINTF("injected NMI\n");
2197 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
2199 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
2204 /* Force the VCPU out of its inner loop to process any INIT requests
2205 * or (for userspace APIC, but it is cheap to combine the checks here)
2206 * pending TPR access reports.
2208 if (cpu
->interrupt_request
& (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
2209 cpu
->exit_request
= 1;
2212 if (!kvm_irqchip_in_kernel()) {
2213 /* Try to inject an interrupt if the guest can accept it */
2214 if (run
->ready_for_interrupt_injection
&&
2215 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2216 (env
->eflags
& IF_MASK
)) {
2219 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
2220 irq
= cpu_get_pic_interrupt(env
);
2222 struct kvm_interrupt intr
;
2225 DPRINTF("injected interrupt %d\n", irq
);
2226 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
2229 "KVM: injection failed, interrupt lost (%s)\n",
2235 /* If we have an interrupt but the guest is not ready to receive an
2236 * interrupt, request an interrupt window exit. This will
2237 * cause a return to userspace as soon as the guest is ready to
2238 * receive interrupts. */
2239 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
2240 run
->request_interrupt_window
= 1;
2242 run
->request_interrupt_window
= 0;
2245 DPRINTF("setting tpr\n");
2246 run
->cr8
= cpu_get_apic_tpr(x86_cpu
->apic_state
);
2250 MemTxAttrs
kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
2252 X86CPU
*x86_cpu
= X86_CPU(cpu
);
2253 CPUX86State
*env
= &x86_cpu
->env
;
2256 env
->eflags
|= IF_MASK
;
2258 env
->eflags
&= ~IF_MASK
;
2260 cpu_set_apic_tpr(x86_cpu
->apic_state
, run
->cr8
);
2261 cpu_set_apic_base(x86_cpu
->apic_state
, run
->apic_base
);
2262 return MEMTXATTRS_UNSPECIFIED
;
2265 int kvm_arch_process_async_events(CPUState
*cs
)
2267 X86CPU
*cpu
= X86_CPU(cs
);
2268 CPUX86State
*env
= &cpu
->env
;
2270 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
2271 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
2272 assert(env
->mcg_cap
);
2274 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
2276 kvm_cpu_synchronize_state(cs
);
2278 if (env
->exception_injected
== EXCP08_DBLE
) {
2279 /* this means triple fault */
2280 qemu_system_reset_request();
2281 cs
->exit_request
= 1;
2284 env
->exception_injected
= EXCP12_MCHK
;
2285 env
->has_error_code
= 0;
2288 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
2289 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
2293 if (cs
->interrupt_request
& CPU_INTERRUPT_INIT
) {
2294 kvm_cpu_synchronize_state(cs
);
2298 if (kvm_irqchip_in_kernel()) {
2302 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
2303 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
2304 apic_poll_irq(cpu
->apic_state
);
2306 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2307 (env
->eflags
& IF_MASK
)) ||
2308 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2311 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
2312 kvm_cpu_synchronize_state(cs
);
2315 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
2316 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
2317 kvm_cpu_synchronize_state(cs
);
2318 apic_handle_tpr_access_report(cpu
->apic_state
, env
->eip
,
2319 env
->tpr_access_type
);
2325 static int kvm_handle_halt(X86CPU
*cpu
)
2327 CPUState
*cs
= CPU(cpu
);
2328 CPUX86State
*env
= &cpu
->env
;
2330 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
2331 (env
->eflags
& IF_MASK
)) &&
2332 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
2340 static int kvm_handle_tpr_access(X86CPU
*cpu
)
2342 CPUState
*cs
= CPU(cpu
);
2343 struct kvm_run
*run
= cs
->kvm_run
;
2345 apic_handle_tpr_access_report(cpu
->apic_state
, run
->tpr_access
.rip
,
2346 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
2351 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2353 static const uint8_t int3
= 0xcc;
2355 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
2356 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
2362 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
2366 if (cpu_memory_rw_debug(cs
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
2367 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
2379 static int nb_hw_breakpoint
;
2381 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
2385 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2386 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
2387 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
2394 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
2395 target_ulong len
, int type
)
2398 case GDB_BREAKPOINT_HW
:
2401 case GDB_WATCHPOINT_WRITE
:
2402 case GDB_WATCHPOINT_ACCESS
:
2409 if (addr
& (len
- 1)) {
2421 if (nb_hw_breakpoint
== 4) {
2424 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2427 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2428 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2429 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2435 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2436 target_ulong len
, int type
)
2440 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2445 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2450 void kvm_arch_remove_all_hw_breakpoints(void)
2452 nb_hw_breakpoint
= 0;
2455 static CPUWatchpoint hw_watchpoint
;
2457 static int kvm_handle_debug(X86CPU
*cpu
,
2458 struct kvm_debug_exit_arch
*arch_info
)
2460 CPUState
*cs
= CPU(cpu
);
2461 CPUX86State
*env
= &cpu
->env
;
2465 if (arch_info
->exception
== 1) {
2466 if (arch_info
->dr6
& (1 << 14)) {
2467 if (cs
->singlestep_enabled
) {
2471 for (n
= 0; n
< 4; n
++) {
2472 if (arch_info
->dr6
& (1 << n
)) {
2473 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2479 cs
->watchpoint_hit
= &hw_watchpoint
;
2480 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2481 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2485 cs
->watchpoint_hit
= &hw_watchpoint
;
2486 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2487 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2493 } else if (kvm_find_sw_breakpoint(cs
, arch_info
->pc
)) {
2497 cpu_synchronize_state(cs
);
2498 assert(env
->exception_injected
== -1);
2501 env
->exception_injected
= arch_info
->exception
;
2502 env
->has_error_code
= 0;
2508 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2510 const uint8_t type_code
[] = {
2511 [GDB_BREAKPOINT_HW
] = 0x0,
2512 [GDB_WATCHPOINT_WRITE
] = 0x1,
2513 [GDB_WATCHPOINT_ACCESS
] = 0x3
2515 const uint8_t len_code
[] = {
2516 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2520 if (kvm_sw_breakpoints_active(cpu
)) {
2521 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2523 if (nb_hw_breakpoint
> 0) {
2524 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2525 dbg
->arch
.debugreg
[7] = 0x0600;
2526 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2527 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2528 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2529 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2530 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2535 static bool host_supports_vmx(void)
2537 uint32_t ecx
, unused
;
2539 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2540 return ecx
& CPUID_EXT_VMX
;
2543 #define VMX_INVALID_GUEST_STATE 0x80000021
2545 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2547 X86CPU
*cpu
= X86_CPU(cs
);
2551 switch (run
->exit_reason
) {
2553 DPRINTF("handle_hlt\n");
2554 ret
= kvm_handle_halt(cpu
);
2556 case KVM_EXIT_SET_TPR
:
2559 case KVM_EXIT_TPR_ACCESS
:
2560 ret
= kvm_handle_tpr_access(cpu
);
2562 case KVM_EXIT_FAIL_ENTRY
:
2563 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2564 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2566 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2568 "\nIf you're running a guest on an Intel machine without "
2569 "unrestricted mode\n"
2570 "support, the failure can be most likely due to the guest "
2571 "entering an invalid\n"
2572 "state for Intel VT. For example, the guest maybe running "
2573 "in big real mode\n"
2574 "which is not supported on less recent Intel processors."
2579 case KVM_EXIT_EXCEPTION
:
2580 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2581 run
->ex
.exception
, run
->ex
.error_code
);
2584 case KVM_EXIT_DEBUG
:
2585 DPRINTF("kvm_exit_debug\n");
2586 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2589 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2597 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2599 X86CPU
*cpu
= X86_CPU(cs
);
2600 CPUX86State
*env
= &cpu
->env
;
2602 kvm_cpu_synchronize_state(cs
);
2603 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2604 ((env
->segs
[R_CS
].selector
& 3) != 3);
2607 void kvm_arch_init_irq_routing(KVMState
*s
)
2609 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2610 /* If kernel can't do irq routing, interrupt source
2611 * override 0->2 cannot be set up as required by HPET.
2612 * So we have to disable it.
2616 /* We know at this point that we're using the in-kernel
2617 * irqchip, so we can use irqfds, and on x86 we know
2618 * we can use msi via irqfd and GSI routing.
2620 kvm_msi_via_irqfd_allowed
= true;
2621 kvm_gsi_routing_allowed
= true;
2624 /* Classic KVM device assignment interface. Will remain x86 only. */
2625 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2626 uint32_t flags
, uint32_t *dev_id
)
2628 struct kvm_assigned_pci_dev dev_data
= {
2629 .segnr
= dev_addr
->domain
,
2630 .busnr
= dev_addr
->bus
,
2631 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2636 dev_data
.assigned_dev_id
=
2637 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2639 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2644 *dev_id
= dev_data
.assigned_dev_id
;
2649 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2651 struct kvm_assigned_pci_dev dev_data
= {
2652 .assigned_dev_id
= dev_id
,
2655 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2658 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2659 uint32_t irq_type
, uint32_t guest_irq
)
2661 struct kvm_assigned_irq assigned_irq
= {
2662 .assigned_dev_id
= dev_id
,
2663 .guest_irq
= guest_irq
,
2667 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2668 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2670 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2674 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2677 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2678 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2680 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2683 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2685 struct kvm_assigned_pci_dev dev_data
= {
2686 .assigned_dev_id
= dev_id
,
2687 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2690 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2693 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2696 struct kvm_assigned_irq assigned_irq
= {
2697 .assigned_dev_id
= dev_id
,
2701 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2704 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2706 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2707 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2710 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2712 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2713 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2716 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2718 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2719 KVM_DEV_IRQ_HOST_MSI
);
2722 bool kvm_device_msix_supported(KVMState
*s
)
2724 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2725 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2726 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2729 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2730 uint32_t nr_vectors
)
2732 struct kvm_assigned_msix_nr msix_nr
= {
2733 .assigned_dev_id
= dev_id
,
2734 .entry_nr
= nr_vectors
,
2737 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2740 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2743 struct kvm_assigned_msix_entry msix_entry
= {
2744 .assigned_dev_id
= dev_id
,
2749 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2752 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2754 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2755 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2758 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2760 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2761 KVM_DEV_IRQ_HOST_MSIX
);
2764 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2765 uint64_t address
, uint32_t data
)