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"
33 #include "exec/ioport.h"
35 #include "hw/pci/pci.h"
40 #define DPRINTF(fmt, ...) \
41 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
47 #define MSR_KVM_WALL_CLOCK 0x11
48 #define MSR_KVM_SYSTEM_TIME 0x12
51 #define BUS_MCEERR_AR 4
54 #define BUS_MCEERR_AO 5
57 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
58 KVM_CAP_INFO(SET_TSS_ADDR
),
59 KVM_CAP_INFO(EXT_CPUID
),
60 KVM_CAP_INFO(MP_STATE
),
64 static bool has_msr_star
;
65 static bool has_msr_hsave_pa
;
66 static bool has_msr_tsc_adjust
;
67 static bool has_msr_tsc_deadline
;
68 static bool has_msr_async_pf_en
;
69 static bool has_msr_pv_eoi_en
;
70 static bool has_msr_misc_enable
;
71 static int lm_capable_kernel
;
73 bool kvm_allows_irq0_override(void)
75 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
78 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
80 struct kvm_cpuid2
*cpuid
;
83 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
84 cpuid
= (struct kvm_cpuid2
*)g_malloc0(size
);
86 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
87 if (r
== 0 && cpuid
->nent
>= max
) {
95 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
103 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
106 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
108 struct kvm_cpuid2
*cpuid
;
110 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
116 struct kvm_para_features
{
119 } para_features
[] = {
120 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
121 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
122 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
123 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
127 static int get_para_features(KVMState
*s
)
131 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
132 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
133 features
|= (1 << para_features
[i
].feature
);
141 /* Returns the value for a specific register on the cpuid entry
143 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
163 /* Find matching entry for function/index on kvm_cpuid2 struct
165 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
170 for (i
= 0; i
< cpuid
->nent
; ++i
) {
171 if (cpuid
->entries
[i
].function
== function
&&
172 cpuid
->entries
[i
].index
== index
) {
173 return &cpuid
->entries
[i
];
180 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
181 uint32_t index
, int reg
)
183 struct kvm_cpuid2
*cpuid
;
185 uint32_t cpuid_1_edx
;
188 cpuid
= get_supported_cpuid(s
);
190 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
193 ret
= cpuid_entry_get_reg(entry
, reg
);
196 /* Fixups for the data returned by KVM, below */
198 if (function
== 1 && reg
== R_EDX
) {
199 /* KVM before 2.6.30 misreports the following features */
200 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
201 } else if (function
== 1 && reg
== R_ECX
) {
202 /* We can set the hypervisor flag, even if KVM does not return it on
203 * GET_SUPPORTED_CPUID
205 ret
|= CPUID_EXT_HYPERVISOR
;
206 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
207 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
208 * and the irqchip is in the kernel.
210 if (kvm_irqchip_in_kernel() &&
211 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
212 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
215 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
216 * without the in-kernel irqchip
218 if (!kvm_irqchip_in_kernel()) {
219 ret
&= ~CPUID_EXT_X2APIC
;
221 } else if (function
== 0x80000001 && reg
== R_EDX
) {
222 /* On Intel, kvm returns cpuid according to the Intel spec,
223 * so add missing bits according to the AMD spec:
225 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
226 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
231 /* fallback for older kernels */
232 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
233 ret
= get_para_features(s
);
239 typedef struct HWPoisonPage
{
241 QLIST_ENTRY(HWPoisonPage
) list
;
244 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
245 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
247 static void kvm_unpoison_all(void *param
)
249 HWPoisonPage
*page
, *next_page
;
251 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
252 QLIST_REMOVE(page
, list
);
253 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
258 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
262 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
263 if (page
->ram_addr
== ram_addr
) {
267 page
= g_malloc(sizeof(HWPoisonPage
));
268 page
->ram_addr
= ram_addr
;
269 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
272 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
277 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
280 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
285 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
287 CPUX86State
*env
= &cpu
->env
;
288 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
289 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
290 uint64_t mcg_status
= MCG_STATUS_MCIP
;
292 if (code
== BUS_MCEERR_AR
) {
293 status
|= MCI_STATUS_AR
| 0x134;
294 mcg_status
|= MCG_STATUS_EIPV
;
297 mcg_status
|= MCG_STATUS_RIPV
;
299 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
300 (MCM_ADDR_PHYS
<< 6) | 0xc,
301 cpu_x86_support_mca_broadcast(env
) ?
302 MCE_INJECT_BROADCAST
: 0);
305 static void hardware_memory_error(void)
307 fprintf(stderr
, "Hardware memory error!\n");
311 int kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
313 X86CPU
*cpu
= X86_CPU(c
);
314 CPUX86State
*env
= &cpu
->env
;
318 if ((env
->mcg_cap
& MCG_SER_P
) && addr
319 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
320 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
321 !kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
322 fprintf(stderr
, "Hardware memory error for memory used by "
323 "QEMU itself instead of guest system!\n");
324 /* Hope we are lucky for AO MCE */
325 if (code
== BUS_MCEERR_AO
) {
328 hardware_memory_error();
331 kvm_hwpoison_page_add(ram_addr
);
332 kvm_mce_inject(cpu
, paddr
, code
);
334 if (code
== BUS_MCEERR_AO
) {
336 } else if (code
== BUS_MCEERR_AR
) {
337 hardware_memory_error();
345 int kvm_arch_on_sigbus(int code
, void *addr
)
347 if ((first_cpu
->mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
351 /* Hope we are lucky for AO MCE */
352 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
353 !kvm_physical_memory_addr_from_host(CPU(first_cpu
)->kvm_state
,
355 fprintf(stderr
, "Hardware memory error for memory used by "
356 "QEMU itself instead of guest system!: %p\n", addr
);
359 kvm_hwpoison_page_add(ram_addr
);
360 kvm_mce_inject(x86_env_get_cpu(first_cpu
), paddr
, code
);
362 if (code
== BUS_MCEERR_AO
) {
364 } else if (code
== BUS_MCEERR_AR
) {
365 hardware_memory_error();
373 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
375 CPUX86State
*env
= &cpu
->env
;
377 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
378 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
379 struct kvm_x86_mce mce
;
381 env
->exception_injected
= -1;
384 * There must be at least one bank in use if an MCE is pending.
385 * Find it and use its values for the event injection.
387 for (bank
= 0; bank
< bank_num
; bank
++) {
388 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
392 assert(bank
< bank_num
);
395 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
396 mce
.mcg_status
= env
->mcg_status
;
397 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
398 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
400 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
405 static void cpu_update_state(void *opaque
, int running
, RunState state
)
407 CPUX86State
*env
= opaque
;
410 env
->tsc_valid
= false;
414 int kvm_arch_init_vcpu(CPUState
*cs
)
417 struct kvm_cpuid2 cpuid
;
418 struct kvm_cpuid_entry2 entries
[100];
419 } QEMU_PACKED cpuid_data
;
420 X86CPU
*cpu
= X86_CPU(cs
);
421 CPUX86State
*env
= &cpu
->env
;
422 uint32_t limit
, i
, j
, cpuid_i
;
424 struct kvm_cpuid_entry2
*c
;
425 uint32_t signature
[3];
430 /* Paravirtualization CPUIDs */
431 c
= &cpuid_data
.entries
[cpuid_i
++];
432 memset(c
, 0, sizeof(*c
));
433 c
->function
= KVM_CPUID_SIGNATURE
;
434 if (!hyperv_enabled()) {
435 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
438 memcpy(signature
, "Microsoft Hv", 12);
439 c
->eax
= HYPERV_CPUID_MIN
;
441 c
->ebx
= signature
[0];
442 c
->ecx
= signature
[1];
443 c
->edx
= signature
[2];
445 c
= &cpuid_data
.entries
[cpuid_i
++];
446 memset(c
, 0, sizeof(*c
));
447 c
->function
= KVM_CPUID_FEATURES
;
448 c
->eax
= env
->cpuid_kvm_features
;
450 if (hyperv_enabled()) {
451 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
452 c
->eax
= signature
[0];
454 c
= &cpuid_data
.entries
[cpuid_i
++];
455 memset(c
, 0, sizeof(*c
));
456 c
->function
= HYPERV_CPUID_VERSION
;
460 c
= &cpuid_data
.entries
[cpuid_i
++];
461 memset(c
, 0, sizeof(*c
));
462 c
->function
= HYPERV_CPUID_FEATURES
;
463 if (hyperv_relaxed_timing_enabled()) {
464 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
466 if (hyperv_vapic_recommended()) {
467 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
468 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
471 c
= &cpuid_data
.entries
[cpuid_i
++];
472 memset(c
, 0, sizeof(*c
));
473 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
474 if (hyperv_relaxed_timing_enabled()) {
475 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
477 if (hyperv_vapic_recommended()) {
478 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
480 c
->ebx
= hyperv_get_spinlock_retries();
482 c
= &cpuid_data
.entries
[cpuid_i
++];
483 memset(c
, 0, sizeof(*c
));
484 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
488 c
= &cpuid_data
.entries
[cpuid_i
++];
489 memset(c
, 0, sizeof(*c
));
490 c
->function
= KVM_CPUID_SIGNATURE_NEXT
;
491 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
493 c
->ebx
= signature
[0];
494 c
->ecx
= signature
[1];
495 c
->edx
= signature
[2];
498 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
500 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
502 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
504 for (i
= 0; i
<= limit
; i
++) {
505 c
= &cpuid_data
.entries
[cpuid_i
++];
509 /* Keep reading function 2 till all the input is received */
513 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
514 KVM_CPUID_FLAG_STATE_READ_NEXT
;
515 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
516 times
= c
->eax
& 0xff;
518 for (j
= 1; j
< times
; ++j
) {
519 c
= &cpuid_data
.entries
[cpuid_i
++];
521 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
522 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
530 if (i
== 0xd && j
== 64) {
534 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
536 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
538 if (i
== 4 && c
->eax
== 0) {
541 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
544 if (i
== 0xd && c
->eax
== 0) {
547 c
= &cpuid_data
.entries
[cpuid_i
++];
553 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
557 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
559 for (i
= 0x80000000; i
<= limit
; i
++) {
560 c
= &cpuid_data
.entries
[cpuid_i
++];
564 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
567 /* Call Centaur's CPUID instructions they are supported. */
568 if (env
->cpuid_xlevel2
> 0) {
569 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
571 for (i
= 0xC0000000; i
<= limit
; i
++) {
572 c
= &cpuid_data
.entries
[cpuid_i
++];
576 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
580 cpuid_data
.cpuid
.nent
= cpuid_i
;
582 if (((env
->cpuid_version
>> 8)&0xF) >= 6
583 && (env
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
584 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
589 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
591 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
595 if (banks
> MCE_BANKS_DEF
) {
596 banks
= MCE_BANKS_DEF
;
598 mcg_cap
&= MCE_CAP_DEF
;
600 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
602 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
606 env
->mcg_cap
= mcg_cap
;
609 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
611 cpuid_data
.cpuid
.padding
= 0;
612 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
617 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
618 if (r
&& env
->tsc_khz
) {
619 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
621 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
626 if (kvm_has_xsave()) {
627 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
633 void kvm_arch_reset_vcpu(CPUState
*cs
)
635 X86CPU
*cpu
= X86_CPU(cs
);
636 CPUX86State
*env
= &cpu
->env
;
638 env
->exception_injected
= -1;
639 env
->interrupt_injected
= -1;
641 if (kvm_irqchip_in_kernel()) {
642 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
643 KVM_MP_STATE_UNINITIALIZED
;
645 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
649 static int kvm_get_supported_msrs(KVMState
*s
)
651 static int kvm_supported_msrs
;
655 if (kvm_supported_msrs
== 0) {
656 struct kvm_msr_list msr_list
, *kvm_msr_list
;
658 kvm_supported_msrs
= -1;
660 /* Obtain MSR list from KVM. These are the MSRs that we must
663 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
664 if (ret
< 0 && ret
!= -E2BIG
) {
667 /* Old kernel modules had a bug and could write beyond the provided
668 memory. Allocate at least a safe amount of 1K. */
669 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
671 sizeof(msr_list
.indices
[0])));
673 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
674 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
678 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
679 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
683 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
684 has_msr_hsave_pa
= true;
687 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
688 has_msr_tsc_adjust
= true;
691 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
692 has_msr_tsc_deadline
= true;
695 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
696 has_msr_misc_enable
= true;
702 g_free(kvm_msr_list
);
708 int kvm_arch_init(KVMState
*s
)
710 QemuOptsList
*list
= qemu_find_opts("machine");
711 uint64_t identity_base
= 0xfffbc000;
714 struct utsname utsname
;
716 ret
= kvm_get_supported_msrs(s
);
722 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
725 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
726 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
727 * Since these must be part of guest physical memory, we need to allocate
728 * them, both by setting their start addresses in the kernel and by
729 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
731 * Older KVM versions may not support setting the identity map base. In
732 * that case we need to stick with the default, i.e. a 256K maximum BIOS
735 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
736 /* Allows up to 16M BIOSes. */
737 identity_base
= 0xfeffc000;
739 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
745 /* Set TSS base one page after EPT identity map. */
746 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
751 /* Tell fw_cfg to notify the BIOS to reserve the range. */
752 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
754 fprintf(stderr
, "e820_add_entry() table is full\n");
757 qemu_register_reset(kvm_unpoison_all
, NULL
);
759 if (!QTAILQ_EMPTY(&list
->head
)) {
760 shadow_mem
= qemu_opt_get_size(QTAILQ_FIRST(&list
->head
),
761 "kvm_shadow_mem", -1);
762 if (shadow_mem
!= -1) {
764 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
773 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
775 lhs
->selector
= rhs
->selector
;
776 lhs
->base
= rhs
->base
;
777 lhs
->limit
= rhs
->limit
;
789 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
791 unsigned flags
= rhs
->flags
;
792 lhs
->selector
= rhs
->selector
;
793 lhs
->base
= rhs
->base
;
794 lhs
->limit
= rhs
->limit
;
795 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
796 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
797 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
798 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
799 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
800 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
801 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
802 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
807 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
809 lhs
->selector
= rhs
->selector
;
810 lhs
->base
= rhs
->base
;
811 lhs
->limit
= rhs
->limit
;
812 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
813 (rhs
->present
* DESC_P_MASK
) |
814 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
815 (rhs
->db
<< DESC_B_SHIFT
) |
816 (rhs
->s
* DESC_S_MASK
) |
817 (rhs
->l
<< DESC_L_SHIFT
) |
818 (rhs
->g
* DESC_G_MASK
) |
819 (rhs
->avl
* DESC_AVL_MASK
);
822 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
825 *kvm_reg
= *qemu_reg
;
827 *qemu_reg
= *kvm_reg
;
831 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
833 CPUX86State
*env
= &cpu
->env
;
834 struct kvm_regs regs
;
838 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
844 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
845 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
846 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
847 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
848 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
849 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
850 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
851 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
853 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
854 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
855 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
856 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
857 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
858 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
859 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
860 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
863 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
864 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
867 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
873 static int kvm_put_fpu(X86CPU
*cpu
)
875 CPUX86State
*env
= &cpu
->env
;
879 memset(&fpu
, 0, sizeof fpu
);
880 fpu
.fsw
= env
->fpus
& ~(7 << 11);
881 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
883 fpu
.last_opcode
= env
->fpop
;
884 fpu
.last_ip
= env
->fpip
;
885 fpu
.last_dp
= env
->fpdp
;
886 for (i
= 0; i
< 8; ++i
) {
887 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
889 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
890 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
891 fpu
.mxcsr
= env
->mxcsr
;
893 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
896 #define XSAVE_FCW_FSW 0
897 #define XSAVE_FTW_FOP 1
898 #define XSAVE_CWD_RIP 2
899 #define XSAVE_CWD_RDP 4
900 #define XSAVE_MXCSR 6
901 #define XSAVE_ST_SPACE 8
902 #define XSAVE_XMM_SPACE 40
903 #define XSAVE_XSTATE_BV 128
904 #define XSAVE_YMMH_SPACE 144
906 static int kvm_put_xsave(X86CPU
*cpu
)
908 CPUX86State
*env
= &cpu
->env
;
909 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
910 uint16_t cwd
, swd
, twd
;
913 if (!kvm_has_xsave()) {
914 return kvm_put_fpu(cpu
);
917 memset(xsave
, 0, sizeof(struct kvm_xsave
));
919 swd
= env
->fpus
& ~(7 << 11);
920 swd
|= (env
->fpstt
& 7) << 11;
922 for (i
= 0; i
< 8; ++i
) {
923 twd
|= (!env
->fptags
[i
]) << i
;
925 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
926 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
927 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
928 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
929 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
931 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
932 sizeof env
->xmm_regs
);
933 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
934 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
935 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
936 sizeof env
->ymmh_regs
);
937 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
941 static int kvm_put_xcrs(X86CPU
*cpu
)
943 CPUX86State
*env
= &cpu
->env
;
944 struct kvm_xcrs xcrs
;
946 if (!kvm_has_xcrs()) {
952 xcrs
.xcrs
[0].xcr
= 0;
953 xcrs
.xcrs
[0].value
= env
->xcr0
;
954 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
957 static int kvm_put_sregs(X86CPU
*cpu
)
959 CPUX86State
*env
= &cpu
->env
;
960 struct kvm_sregs sregs
;
962 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
963 if (env
->interrupt_injected
>= 0) {
964 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
965 (uint64_t)1 << (env
->interrupt_injected
% 64);
968 if ((env
->eflags
& VM_MASK
)) {
969 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
970 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
971 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
972 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
973 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
974 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
976 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
977 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
978 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
979 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
980 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
981 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
984 set_seg(&sregs
.tr
, &env
->tr
);
985 set_seg(&sregs
.ldt
, &env
->ldt
);
987 sregs
.idt
.limit
= env
->idt
.limit
;
988 sregs
.idt
.base
= env
->idt
.base
;
989 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
990 sregs
.gdt
.limit
= env
->gdt
.limit
;
991 sregs
.gdt
.base
= env
->gdt
.base
;
992 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
994 sregs
.cr0
= env
->cr
[0];
995 sregs
.cr2
= env
->cr
[2];
996 sregs
.cr3
= env
->cr
[3];
997 sregs
.cr4
= env
->cr
[4];
999 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
1000 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
1002 sregs
.efer
= env
->efer
;
1004 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1007 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1008 uint32_t index
, uint64_t value
)
1010 entry
->index
= index
;
1011 entry
->data
= value
;
1014 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1016 CPUX86State
*env
= &cpu
->env
;
1018 struct kvm_msrs info
;
1019 struct kvm_msr_entry entries
[100];
1021 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1024 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1025 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1026 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1027 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1029 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1031 if (has_msr_hsave_pa
) {
1032 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1034 if (has_msr_tsc_adjust
) {
1035 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1037 if (has_msr_tsc_deadline
) {
1038 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1040 if (has_msr_misc_enable
) {
1041 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1042 env
->msr_ia32_misc_enable
);
1044 #ifdef TARGET_X86_64
1045 if (lm_capable_kernel
) {
1046 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1047 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1048 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1049 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1052 if (level
== KVM_PUT_FULL_STATE
) {
1054 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
1055 * writeback. Until this is fixed, we only write the offset to SMP
1056 * guests after migration, desynchronizing the VCPUs, but avoiding
1057 * huge jump-backs that would occur without any writeback at all.
1059 if (smp_cpus
== 1 || env
->tsc
!= 0) {
1060 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1064 * The following paravirtual MSRs have side effects on the guest or are
1065 * too heavy for normal writeback. Limit them to reset or full state
1068 if (level
>= KVM_PUT_RESET_STATE
) {
1069 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1070 env
->system_time_msr
);
1071 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1072 if (has_msr_async_pf_en
) {
1073 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1074 env
->async_pf_en_msr
);
1076 if (has_msr_pv_eoi_en
) {
1077 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1078 env
->pv_eoi_en_msr
);
1080 if (hyperv_hypercall_available()) {
1081 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
, 0);
1082 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
, 0);
1084 if (hyperv_vapic_recommended()) {
1085 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
1091 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1092 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1093 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1094 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1098 msr_data
.info
.nmsrs
= n
;
1100 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1105 static int kvm_get_fpu(X86CPU
*cpu
)
1107 CPUX86State
*env
= &cpu
->env
;
1111 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1116 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1117 env
->fpus
= fpu
.fsw
;
1118 env
->fpuc
= fpu
.fcw
;
1119 env
->fpop
= fpu
.last_opcode
;
1120 env
->fpip
= fpu
.last_ip
;
1121 env
->fpdp
= fpu
.last_dp
;
1122 for (i
= 0; i
< 8; ++i
) {
1123 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1125 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1126 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1127 env
->mxcsr
= fpu
.mxcsr
;
1132 static int kvm_get_xsave(X86CPU
*cpu
)
1134 CPUX86State
*env
= &cpu
->env
;
1135 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1137 uint16_t cwd
, swd
, twd
;
1139 if (!kvm_has_xsave()) {
1140 return kvm_get_fpu(cpu
);
1143 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1148 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1149 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1150 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1151 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1152 env
->fpstt
= (swd
>> 11) & 7;
1155 for (i
= 0; i
< 8; ++i
) {
1156 env
->fptags
[i
] = !((twd
>> i
) & 1);
1158 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1159 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1160 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1161 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1162 sizeof env
->fpregs
);
1163 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1164 sizeof env
->xmm_regs
);
1165 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1166 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1167 sizeof env
->ymmh_regs
);
1171 static int kvm_get_xcrs(X86CPU
*cpu
)
1173 CPUX86State
*env
= &cpu
->env
;
1175 struct kvm_xcrs xcrs
;
1177 if (!kvm_has_xcrs()) {
1181 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1186 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1187 /* Only support xcr0 now */
1188 if (xcrs
.xcrs
[0].xcr
== 0) {
1189 env
->xcr0
= xcrs
.xcrs
[0].value
;
1196 static int kvm_get_sregs(X86CPU
*cpu
)
1198 CPUX86State
*env
= &cpu
->env
;
1199 struct kvm_sregs sregs
;
1203 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1208 /* There can only be one pending IRQ set in the bitmap at a time, so try
1209 to find it and save its number instead (-1 for none). */
1210 env
->interrupt_injected
= -1;
1211 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1212 if (sregs
.interrupt_bitmap
[i
]) {
1213 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1214 env
->interrupt_injected
= i
* 64 + bit
;
1219 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1220 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1221 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1222 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1223 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1224 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1226 get_seg(&env
->tr
, &sregs
.tr
);
1227 get_seg(&env
->ldt
, &sregs
.ldt
);
1229 env
->idt
.limit
= sregs
.idt
.limit
;
1230 env
->idt
.base
= sregs
.idt
.base
;
1231 env
->gdt
.limit
= sregs
.gdt
.limit
;
1232 env
->gdt
.base
= sregs
.gdt
.base
;
1234 env
->cr
[0] = sregs
.cr0
;
1235 env
->cr
[2] = sregs
.cr2
;
1236 env
->cr
[3] = sregs
.cr3
;
1237 env
->cr
[4] = sregs
.cr4
;
1239 env
->efer
= sregs
.efer
;
1241 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1243 #define HFLAG_COPY_MASK \
1244 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1245 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1246 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1247 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1249 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1250 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1251 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1252 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1253 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1254 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1255 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1257 if (env
->efer
& MSR_EFER_LMA
) {
1258 hflags
|= HF_LMA_MASK
;
1261 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1262 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1264 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1265 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1266 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1267 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1268 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1269 !(hflags
& HF_CS32_MASK
)) {
1270 hflags
|= HF_ADDSEG_MASK
;
1272 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1273 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1276 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1281 static int kvm_get_msrs(X86CPU
*cpu
)
1283 CPUX86State
*env
= &cpu
->env
;
1285 struct kvm_msrs info
;
1286 struct kvm_msr_entry entries
[100];
1288 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1292 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1293 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1294 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1295 msrs
[n
++].index
= MSR_PAT
;
1297 msrs
[n
++].index
= MSR_STAR
;
1299 if (has_msr_hsave_pa
) {
1300 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1302 if (has_msr_tsc_adjust
) {
1303 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1305 if (has_msr_tsc_deadline
) {
1306 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1308 if (has_msr_misc_enable
) {
1309 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1312 if (!env
->tsc_valid
) {
1313 msrs
[n
++].index
= MSR_IA32_TSC
;
1314 env
->tsc_valid
= !runstate_is_running();
1317 #ifdef TARGET_X86_64
1318 if (lm_capable_kernel
) {
1319 msrs
[n
++].index
= MSR_CSTAR
;
1320 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1321 msrs
[n
++].index
= MSR_FMASK
;
1322 msrs
[n
++].index
= MSR_LSTAR
;
1325 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1326 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1327 if (has_msr_async_pf_en
) {
1328 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1330 if (has_msr_pv_eoi_en
) {
1331 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1335 msrs
[n
++].index
= MSR_MCG_STATUS
;
1336 msrs
[n
++].index
= MSR_MCG_CTL
;
1337 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1338 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1342 msr_data
.info
.nmsrs
= n
;
1343 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1348 for (i
= 0; i
< ret
; i
++) {
1349 switch (msrs
[i
].index
) {
1350 case MSR_IA32_SYSENTER_CS
:
1351 env
->sysenter_cs
= msrs
[i
].data
;
1353 case MSR_IA32_SYSENTER_ESP
:
1354 env
->sysenter_esp
= msrs
[i
].data
;
1356 case MSR_IA32_SYSENTER_EIP
:
1357 env
->sysenter_eip
= msrs
[i
].data
;
1360 env
->pat
= msrs
[i
].data
;
1363 env
->star
= msrs
[i
].data
;
1365 #ifdef TARGET_X86_64
1367 env
->cstar
= msrs
[i
].data
;
1369 case MSR_KERNELGSBASE
:
1370 env
->kernelgsbase
= msrs
[i
].data
;
1373 env
->fmask
= msrs
[i
].data
;
1376 env
->lstar
= msrs
[i
].data
;
1380 env
->tsc
= msrs
[i
].data
;
1382 case MSR_TSC_ADJUST
:
1383 env
->tsc_adjust
= msrs
[i
].data
;
1385 case MSR_IA32_TSCDEADLINE
:
1386 env
->tsc_deadline
= msrs
[i
].data
;
1388 case MSR_VM_HSAVE_PA
:
1389 env
->vm_hsave
= msrs
[i
].data
;
1391 case MSR_KVM_SYSTEM_TIME
:
1392 env
->system_time_msr
= msrs
[i
].data
;
1394 case MSR_KVM_WALL_CLOCK
:
1395 env
->wall_clock_msr
= msrs
[i
].data
;
1397 case MSR_MCG_STATUS
:
1398 env
->mcg_status
= msrs
[i
].data
;
1401 env
->mcg_ctl
= msrs
[i
].data
;
1403 case MSR_IA32_MISC_ENABLE
:
1404 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1407 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1408 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1409 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1412 case MSR_KVM_ASYNC_PF_EN
:
1413 env
->async_pf_en_msr
= msrs
[i
].data
;
1415 case MSR_KVM_PV_EOI_EN
:
1416 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1424 static int kvm_put_mp_state(X86CPU
*cpu
)
1426 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1428 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1431 static int kvm_get_mp_state(X86CPU
*cpu
)
1433 CPUX86State
*env
= &cpu
->env
;
1434 struct kvm_mp_state mp_state
;
1437 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MP_STATE
, &mp_state
);
1441 env
->mp_state
= mp_state
.mp_state
;
1442 if (kvm_irqchip_in_kernel()) {
1443 env
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1448 static int kvm_get_apic(X86CPU
*cpu
)
1450 CPUX86State
*env
= &cpu
->env
;
1451 DeviceState
*apic
= env
->apic_state
;
1452 struct kvm_lapic_state kapic
;
1455 if (apic
&& kvm_irqchip_in_kernel()) {
1456 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1461 kvm_get_apic_state(apic
, &kapic
);
1466 static int kvm_put_apic(X86CPU
*cpu
)
1468 CPUX86State
*env
= &cpu
->env
;
1469 DeviceState
*apic
= env
->apic_state
;
1470 struct kvm_lapic_state kapic
;
1472 if (apic
&& kvm_irqchip_in_kernel()) {
1473 kvm_put_apic_state(apic
, &kapic
);
1475 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1480 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1482 CPUX86State
*env
= &cpu
->env
;
1483 struct kvm_vcpu_events events
;
1485 if (!kvm_has_vcpu_events()) {
1489 events
.exception
.injected
= (env
->exception_injected
>= 0);
1490 events
.exception
.nr
= env
->exception_injected
;
1491 events
.exception
.has_error_code
= env
->has_error_code
;
1492 events
.exception
.error_code
= env
->error_code
;
1493 events
.exception
.pad
= 0;
1495 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1496 events
.interrupt
.nr
= env
->interrupt_injected
;
1497 events
.interrupt
.soft
= env
->soft_interrupt
;
1499 events
.nmi
.injected
= env
->nmi_injected
;
1500 events
.nmi
.pending
= env
->nmi_pending
;
1501 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1504 events
.sipi_vector
= env
->sipi_vector
;
1507 if (level
>= KVM_PUT_RESET_STATE
) {
1509 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1512 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1515 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1517 CPUX86State
*env
= &cpu
->env
;
1518 struct kvm_vcpu_events events
;
1521 if (!kvm_has_vcpu_events()) {
1525 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1529 env
->exception_injected
=
1530 events
.exception
.injected
? events
.exception
.nr
: -1;
1531 env
->has_error_code
= events
.exception
.has_error_code
;
1532 env
->error_code
= events
.exception
.error_code
;
1534 env
->interrupt_injected
=
1535 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1536 env
->soft_interrupt
= events
.interrupt
.soft
;
1538 env
->nmi_injected
= events
.nmi
.injected
;
1539 env
->nmi_pending
= events
.nmi
.pending
;
1540 if (events
.nmi
.masked
) {
1541 env
->hflags2
|= HF2_NMI_MASK
;
1543 env
->hflags2
&= ~HF2_NMI_MASK
;
1546 env
->sipi_vector
= events
.sipi_vector
;
1551 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1553 CPUX86State
*env
= &cpu
->env
;
1555 unsigned long reinject_trap
= 0;
1557 if (!kvm_has_vcpu_events()) {
1558 if (env
->exception_injected
== 1) {
1559 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1560 } else if (env
->exception_injected
== 3) {
1561 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1563 env
->exception_injected
= -1;
1567 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1568 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1569 * by updating the debug state once again if single-stepping is on.
1570 * Another reason to call kvm_update_guest_debug here is a pending debug
1571 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1572 * reinject them via SET_GUEST_DEBUG.
1574 if (reinject_trap
||
1575 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1576 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1581 static int kvm_put_debugregs(X86CPU
*cpu
)
1583 CPUX86State
*env
= &cpu
->env
;
1584 struct kvm_debugregs dbgregs
;
1587 if (!kvm_has_debugregs()) {
1591 for (i
= 0; i
< 4; i
++) {
1592 dbgregs
.db
[i
] = env
->dr
[i
];
1594 dbgregs
.dr6
= env
->dr
[6];
1595 dbgregs
.dr7
= env
->dr
[7];
1598 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1601 static int kvm_get_debugregs(X86CPU
*cpu
)
1603 CPUX86State
*env
= &cpu
->env
;
1604 struct kvm_debugregs dbgregs
;
1607 if (!kvm_has_debugregs()) {
1611 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1615 for (i
= 0; i
< 4; i
++) {
1616 env
->dr
[i
] = dbgregs
.db
[i
];
1618 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1619 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1624 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1626 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1629 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1631 ret
= kvm_getput_regs(x86_cpu
, 1);
1635 ret
= kvm_put_xsave(x86_cpu
);
1639 ret
= kvm_put_xcrs(x86_cpu
);
1643 ret
= kvm_put_sregs(x86_cpu
);
1647 /* must be before kvm_put_msrs */
1648 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1652 ret
= kvm_put_msrs(x86_cpu
, level
);
1656 if (level
>= KVM_PUT_RESET_STATE
) {
1657 ret
= kvm_put_mp_state(x86_cpu
);
1661 ret
= kvm_put_apic(x86_cpu
);
1666 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1670 ret
= kvm_put_debugregs(x86_cpu
);
1675 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1682 int kvm_arch_get_registers(CPUState
*cs
)
1684 X86CPU
*cpu
= X86_CPU(cs
);
1687 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1689 ret
= kvm_getput_regs(cpu
, 0);
1693 ret
= kvm_get_xsave(cpu
);
1697 ret
= kvm_get_xcrs(cpu
);
1701 ret
= kvm_get_sregs(cpu
);
1705 ret
= kvm_get_msrs(cpu
);
1709 ret
= kvm_get_mp_state(cpu
);
1713 ret
= kvm_get_apic(cpu
);
1717 ret
= kvm_get_vcpu_events(cpu
);
1721 ret
= kvm_get_debugregs(cpu
);
1728 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1730 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1731 CPUX86State
*env
= &x86_cpu
->env
;
1735 if (env
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1736 env
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1737 DPRINTF("injected NMI\n");
1738 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
1740 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
1745 if (!kvm_irqchip_in_kernel()) {
1746 /* Force the VCPU out of its inner loop to process any INIT requests
1747 * or pending TPR access reports. */
1748 if (env
->interrupt_request
&
1749 (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
1750 env
->exit_request
= 1;
1753 /* Try to inject an interrupt if the guest can accept it */
1754 if (run
->ready_for_interrupt_injection
&&
1755 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1756 (env
->eflags
& IF_MASK
)) {
1759 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1760 irq
= cpu_get_pic_interrupt(env
);
1762 struct kvm_interrupt intr
;
1765 DPRINTF("injected interrupt %d\n", irq
);
1766 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
1769 "KVM: injection failed, interrupt lost (%s)\n",
1775 /* If we have an interrupt but the guest is not ready to receive an
1776 * interrupt, request an interrupt window exit. This will
1777 * cause a return to userspace as soon as the guest is ready to
1778 * receive interrupts. */
1779 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
1780 run
->request_interrupt_window
= 1;
1782 run
->request_interrupt_window
= 0;
1785 DPRINTF("setting tpr\n");
1786 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1790 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
1792 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1793 CPUX86State
*env
= &x86_cpu
->env
;
1796 env
->eflags
|= IF_MASK
;
1798 env
->eflags
&= ~IF_MASK
;
1800 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1801 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1804 int kvm_arch_process_async_events(CPUState
*cs
)
1806 X86CPU
*cpu
= X86_CPU(cs
);
1807 CPUX86State
*env
= &cpu
->env
;
1809 if (env
->interrupt_request
& CPU_INTERRUPT_MCE
) {
1810 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
1811 assert(env
->mcg_cap
);
1813 env
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
1815 kvm_cpu_synchronize_state(env
);
1817 if (env
->exception_injected
== EXCP08_DBLE
) {
1818 /* this means triple fault */
1819 qemu_system_reset_request();
1820 env
->exit_request
= 1;
1823 env
->exception_injected
= EXCP12_MCHK
;
1824 env
->has_error_code
= 0;
1827 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
1828 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1832 if (kvm_irqchip_in_kernel()) {
1836 if (env
->interrupt_request
& CPU_INTERRUPT_POLL
) {
1837 env
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
1838 apic_poll_irq(env
->apic_state
);
1840 if (((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1841 (env
->eflags
& IF_MASK
)) ||
1842 (env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1845 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1846 kvm_cpu_synchronize_state(env
);
1849 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1850 kvm_cpu_synchronize_state(env
);
1853 if (env
->interrupt_request
& CPU_INTERRUPT_TPR
) {
1854 env
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
1855 kvm_cpu_synchronize_state(env
);
1856 apic_handle_tpr_access_report(env
->apic_state
, env
->eip
,
1857 env
->tpr_access_type
);
1863 static int kvm_handle_halt(X86CPU
*cpu
)
1865 CPUX86State
*env
= &cpu
->env
;
1867 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1868 (env
->eflags
& IF_MASK
)) &&
1869 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1877 static int kvm_handle_tpr_access(X86CPU
*cpu
)
1879 CPUX86State
*env
= &cpu
->env
;
1880 CPUState
*cs
= CPU(cpu
);
1881 struct kvm_run
*run
= cs
->kvm_run
;
1883 apic_handle_tpr_access_report(env
->apic_state
, run
->tpr_access
.rip
,
1884 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
1889 int kvm_arch_insert_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1891 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1892 static const uint8_t int3
= 0xcc;
1894 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1895 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
1901 int kvm_arch_remove_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1903 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1906 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1907 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
1919 static int nb_hw_breakpoint
;
1921 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1925 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1926 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1927 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
1934 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1935 target_ulong len
, int type
)
1938 case GDB_BREAKPOINT_HW
:
1941 case GDB_WATCHPOINT_WRITE
:
1942 case GDB_WATCHPOINT_ACCESS
:
1949 if (addr
& (len
- 1)) {
1961 if (nb_hw_breakpoint
== 4) {
1964 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
1967 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1968 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1969 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1975 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1976 target_ulong len
, int type
)
1980 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1985 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1990 void kvm_arch_remove_all_hw_breakpoints(void)
1992 nb_hw_breakpoint
= 0;
1995 static CPUWatchpoint hw_watchpoint
;
1997 static int kvm_handle_debug(X86CPU
*cpu
,
1998 struct kvm_debug_exit_arch
*arch_info
)
2000 CPUX86State
*env
= &cpu
->env
;
2004 if (arch_info
->exception
== 1) {
2005 if (arch_info
->dr6
& (1 << 14)) {
2006 if (env
->singlestep_enabled
) {
2010 for (n
= 0; n
< 4; n
++) {
2011 if (arch_info
->dr6
& (1 << n
)) {
2012 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2018 env
->watchpoint_hit
= &hw_watchpoint
;
2019 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2020 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2024 env
->watchpoint_hit
= &hw_watchpoint
;
2025 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2026 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2032 } else if (kvm_find_sw_breakpoint(CPU(cpu
), arch_info
->pc
)) {
2036 cpu_synchronize_state(env
);
2037 assert(env
->exception_injected
== -1);
2040 env
->exception_injected
= arch_info
->exception
;
2041 env
->has_error_code
= 0;
2047 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2049 const uint8_t type_code
[] = {
2050 [GDB_BREAKPOINT_HW
] = 0x0,
2051 [GDB_WATCHPOINT_WRITE
] = 0x1,
2052 [GDB_WATCHPOINT_ACCESS
] = 0x3
2054 const uint8_t len_code
[] = {
2055 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2059 if (kvm_sw_breakpoints_active(cpu
)) {
2060 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2062 if (nb_hw_breakpoint
> 0) {
2063 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2064 dbg
->arch
.debugreg
[7] = 0x0600;
2065 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2066 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2067 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2068 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2069 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2074 static bool host_supports_vmx(void)
2076 uint32_t ecx
, unused
;
2078 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2079 return ecx
& CPUID_EXT_VMX
;
2082 #define VMX_INVALID_GUEST_STATE 0x80000021
2084 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2086 X86CPU
*cpu
= X86_CPU(cs
);
2090 switch (run
->exit_reason
) {
2092 DPRINTF("handle_hlt\n");
2093 ret
= kvm_handle_halt(cpu
);
2095 case KVM_EXIT_SET_TPR
:
2098 case KVM_EXIT_TPR_ACCESS
:
2099 ret
= kvm_handle_tpr_access(cpu
);
2101 case KVM_EXIT_FAIL_ENTRY
:
2102 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2103 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2105 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2107 "\nIf you're running a guest on an Intel machine without "
2108 "unrestricted mode\n"
2109 "support, the failure can be most likely due to the guest "
2110 "entering an invalid\n"
2111 "state for Intel VT. For example, the guest maybe running "
2112 "in big real mode\n"
2113 "which is not supported on less recent Intel processors."
2118 case KVM_EXIT_EXCEPTION
:
2119 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2120 run
->ex
.exception
, run
->ex
.error_code
);
2123 case KVM_EXIT_DEBUG
:
2124 DPRINTF("kvm_exit_debug\n");
2125 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2128 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2136 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2138 X86CPU
*cpu
= X86_CPU(cs
);
2139 CPUX86State
*env
= &cpu
->env
;
2141 kvm_cpu_synchronize_state(env
);
2142 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2143 ((env
->segs
[R_CS
].selector
& 3) != 3);
2146 void kvm_arch_init_irq_routing(KVMState
*s
)
2148 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2149 /* If kernel can't do irq routing, interrupt source
2150 * override 0->2 cannot be set up as required by HPET.
2151 * So we have to disable it.
2155 /* We know at this point that we're using the in-kernel
2156 * irqchip, so we can use irqfds, and on x86 we know
2157 * we can use msi via irqfd and GSI routing.
2159 kvm_irqfds_allowed
= true;
2160 kvm_msi_via_irqfd_allowed
= true;
2161 kvm_gsi_routing_allowed
= true;
2164 /* Classic KVM device assignment interface. Will remain x86 only. */
2165 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2166 uint32_t flags
, uint32_t *dev_id
)
2168 struct kvm_assigned_pci_dev dev_data
= {
2169 .segnr
= dev_addr
->domain
,
2170 .busnr
= dev_addr
->bus
,
2171 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2176 dev_data
.assigned_dev_id
=
2177 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2179 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2184 *dev_id
= dev_data
.assigned_dev_id
;
2189 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2191 struct kvm_assigned_pci_dev dev_data
= {
2192 .assigned_dev_id
= dev_id
,
2195 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2198 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2199 uint32_t irq_type
, uint32_t guest_irq
)
2201 struct kvm_assigned_irq assigned_irq
= {
2202 .assigned_dev_id
= dev_id
,
2203 .guest_irq
= guest_irq
,
2207 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2208 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2210 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2214 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2217 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2218 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2220 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2223 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2225 struct kvm_assigned_pci_dev dev_data
= {
2226 .assigned_dev_id
= dev_id
,
2227 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2230 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2233 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2236 struct kvm_assigned_irq assigned_irq
= {
2237 .assigned_dev_id
= dev_id
,
2241 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2244 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2246 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2247 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2250 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2252 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2253 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2256 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2258 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2259 KVM_DEV_IRQ_HOST_MSI
);
2262 bool kvm_device_msix_supported(KVMState
*s
)
2264 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2265 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2266 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2269 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2270 uint32_t nr_vectors
)
2272 struct kvm_assigned_msix_nr msix_nr
= {
2273 .assigned_dev_id
= dev_id
,
2274 .entry_nr
= nr_vectors
,
2277 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2280 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2283 struct kvm_assigned_msix_entry msix_entry
= {
2284 .assigned_dev_id
= dev_id
,
2289 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2292 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2294 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2295 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2298 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2300 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2301 KVM_DEV_IRQ_HOST_MSIX
);