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 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
416 X86CPU
*cpu
= X86_CPU(cs
);
417 return cpu
->env
.cpuid_apic_id
;
420 #define KVM_MAX_CPUID_ENTRIES 100
422 int kvm_arch_init_vcpu(CPUState
*cs
)
425 struct kvm_cpuid2 cpuid
;
426 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
427 } QEMU_PACKED cpuid_data
;
428 X86CPU
*cpu
= X86_CPU(cs
);
429 CPUX86State
*env
= &cpu
->env
;
430 uint32_t limit
, i
, j
, cpuid_i
;
432 struct kvm_cpuid_entry2
*c
;
433 uint32_t signature
[3];
438 /* Paravirtualization CPUIDs */
439 c
= &cpuid_data
.entries
[cpuid_i
++];
440 memset(c
, 0, sizeof(*c
));
441 c
->function
= KVM_CPUID_SIGNATURE
;
442 if (!hyperv_enabled()) {
443 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
446 memcpy(signature
, "Microsoft Hv", 12);
447 c
->eax
= HYPERV_CPUID_MIN
;
449 c
->ebx
= signature
[0];
450 c
->ecx
= signature
[1];
451 c
->edx
= signature
[2];
453 c
= &cpuid_data
.entries
[cpuid_i
++];
454 memset(c
, 0, sizeof(*c
));
455 c
->function
= KVM_CPUID_FEATURES
;
456 c
->eax
= env
->cpuid_kvm_features
;
458 if (hyperv_enabled()) {
459 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
460 c
->eax
= signature
[0];
462 c
= &cpuid_data
.entries
[cpuid_i
++];
463 memset(c
, 0, sizeof(*c
));
464 c
->function
= HYPERV_CPUID_VERSION
;
468 c
= &cpuid_data
.entries
[cpuid_i
++];
469 memset(c
, 0, sizeof(*c
));
470 c
->function
= HYPERV_CPUID_FEATURES
;
471 if (hyperv_relaxed_timing_enabled()) {
472 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
474 if (hyperv_vapic_recommended()) {
475 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
476 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
479 c
= &cpuid_data
.entries
[cpuid_i
++];
480 memset(c
, 0, sizeof(*c
));
481 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
482 if (hyperv_relaxed_timing_enabled()) {
483 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
485 if (hyperv_vapic_recommended()) {
486 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
488 c
->ebx
= hyperv_get_spinlock_retries();
490 c
= &cpuid_data
.entries
[cpuid_i
++];
491 memset(c
, 0, sizeof(*c
));
492 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
496 c
= &cpuid_data
.entries
[cpuid_i
++];
497 memset(c
, 0, sizeof(*c
));
498 c
->function
= KVM_CPUID_SIGNATURE_NEXT
;
499 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
501 c
->ebx
= signature
[0];
502 c
->ecx
= signature
[1];
503 c
->edx
= signature
[2];
506 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
508 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
510 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
512 for (i
= 0; i
<= limit
; i
++) {
513 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
514 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
517 c
= &cpuid_data
.entries
[cpuid_i
++];
521 /* Keep reading function 2 till all the input is received */
525 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
526 KVM_CPUID_FLAG_STATE_READ_NEXT
;
527 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
528 times
= c
->eax
& 0xff;
530 for (j
= 1; j
< times
; ++j
) {
531 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
532 fprintf(stderr
, "cpuid_data is full, no space for "
533 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
536 c
= &cpuid_data
.entries
[cpuid_i
++];
538 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
539 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
547 if (i
== 0xd && j
== 64) {
551 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
553 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
555 if (i
== 4 && c
->eax
== 0) {
558 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
561 if (i
== 0xd && c
->eax
== 0) {
564 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
565 fprintf(stderr
, "cpuid_data is full, no space for "
566 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
569 c
= &cpuid_data
.entries
[cpuid_i
++];
575 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
579 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
581 for (i
= 0x80000000; i
<= limit
; i
++) {
582 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
583 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
586 c
= &cpuid_data
.entries
[cpuid_i
++];
590 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
593 /* Call Centaur's CPUID instructions they are supported. */
594 if (env
->cpuid_xlevel2
> 0) {
595 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
597 for (i
= 0xC0000000; i
<= limit
; i
++) {
598 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
599 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
602 c
= &cpuid_data
.entries
[cpuid_i
++];
606 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
610 cpuid_data
.cpuid
.nent
= cpuid_i
;
612 if (((env
->cpuid_version
>> 8)&0xF) >= 6
613 && (env
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
614 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
619 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
621 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
625 if (banks
> MCE_BANKS_DEF
) {
626 banks
= MCE_BANKS_DEF
;
628 mcg_cap
&= MCE_CAP_DEF
;
630 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
632 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
636 env
->mcg_cap
= mcg_cap
;
639 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
641 cpuid_data
.cpuid
.padding
= 0;
642 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
647 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
648 if (r
&& env
->tsc_khz
) {
649 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
651 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
656 if (kvm_has_xsave()) {
657 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
663 void kvm_arch_reset_vcpu(CPUState
*cs
)
665 X86CPU
*cpu
= X86_CPU(cs
);
666 CPUX86State
*env
= &cpu
->env
;
668 env
->exception_injected
= -1;
669 env
->interrupt_injected
= -1;
671 if (kvm_irqchip_in_kernel()) {
672 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
673 KVM_MP_STATE_UNINITIALIZED
;
675 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
679 static int kvm_get_supported_msrs(KVMState
*s
)
681 static int kvm_supported_msrs
;
685 if (kvm_supported_msrs
== 0) {
686 struct kvm_msr_list msr_list
, *kvm_msr_list
;
688 kvm_supported_msrs
= -1;
690 /* Obtain MSR list from KVM. These are the MSRs that we must
693 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
694 if (ret
< 0 && ret
!= -E2BIG
) {
697 /* Old kernel modules had a bug and could write beyond the provided
698 memory. Allocate at least a safe amount of 1K. */
699 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
701 sizeof(msr_list
.indices
[0])));
703 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
704 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
708 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
709 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
713 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
714 has_msr_hsave_pa
= true;
717 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
718 has_msr_tsc_adjust
= true;
721 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
722 has_msr_tsc_deadline
= true;
725 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
726 has_msr_misc_enable
= true;
732 g_free(kvm_msr_list
);
738 int kvm_arch_init(KVMState
*s
)
740 QemuOptsList
*list
= qemu_find_opts("machine");
741 uint64_t identity_base
= 0xfffbc000;
744 struct utsname utsname
;
746 ret
= kvm_get_supported_msrs(s
);
752 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
755 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
756 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
757 * Since these must be part of guest physical memory, we need to allocate
758 * them, both by setting their start addresses in the kernel and by
759 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
761 * Older KVM versions may not support setting the identity map base. In
762 * that case we need to stick with the default, i.e. a 256K maximum BIOS
765 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
766 /* Allows up to 16M BIOSes. */
767 identity_base
= 0xfeffc000;
769 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
775 /* Set TSS base one page after EPT identity map. */
776 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
781 /* Tell fw_cfg to notify the BIOS to reserve the range. */
782 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
784 fprintf(stderr
, "e820_add_entry() table is full\n");
787 qemu_register_reset(kvm_unpoison_all
, NULL
);
789 if (!QTAILQ_EMPTY(&list
->head
)) {
790 shadow_mem
= qemu_opt_get_size(QTAILQ_FIRST(&list
->head
),
791 "kvm_shadow_mem", -1);
792 if (shadow_mem
!= -1) {
794 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
803 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
805 lhs
->selector
= rhs
->selector
;
806 lhs
->base
= rhs
->base
;
807 lhs
->limit
= rhs
->limit
;
819 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
821 unsigned flags
= rhs
->flags
;
822 lhs
->selector
= rhs
->selector
;
823 lhs
->base
= rhs
->base
;
824 lhs
->limit
= rhs
->limit
;
825 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
826 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
827 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
828 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
829 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
830 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
831 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
832 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
837 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
839 lhs
->selector
= rhs
->selector
;
840 lhs
->base
= rhs
->base
;
841 lhs
->limit
= rhs
->limit
;
842 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
843 (rhs
->present
* DESC_P_MASK
) |
844 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
845 (rhs
->db
<< DESC_B_SHIFT
) |
846 (rhs
->s
* DESC_S_MASK
) |
847 (rhs
->l
<< DESC_L_SHIFT
) |
848 (rhs
->g
* DESC_G_MASK
) |
849 (rhs
->avl
* DESC_AVL_MASK
);
852 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
855 *kvm_reg
= *qemu_reg
;
857 *qemu_reg
= *kvm_reg
;
861 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
863 CPUX86State
*env
= &cpu
->env
;
864 struct kvm_regs regs
;
868 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
874 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
875 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
876 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
877 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
878 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
879 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
880 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
881 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
883 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
884 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
885 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
886 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
887 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
888 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
889 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
890 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
893 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
894 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
897 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
903 static int kvm_put_fpu(X86CPU
*cpu
)
905 CPUX86State
*env
= &cpu
->env
;
909 memset(&fpu
, 0, sizeof fpu
);
910 fpu
.fsw
= env
->fpus
& ~(7 << 11);
911 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
913 fpu
.last_opcode
= env
->fpop
;
914 fpu
.last_ip
= env
->fpip
;
915 fpu
.last_dp
= env
->fpdp
;
916 for (i
= 0; i
< 8; ++i
) {
917 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
919 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
920 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
921 fpu
.mxcsr
= env
->mxcsr
;
923 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
926 #define XSAVE_FCW_FSW 0
927 #define XSAVE_FTW_FOP 1
928 #define XSAVE_CWD_RIP 2
929 #define XSAVE_CWD_RDP 4
930 #define XSAVE_MXCSR 6
931 #define XSAVE_ST_SPACE 8
932 #define XSAVE_XMM_SPACE 40
933 #define XSAVE_XSTATE_BV 128
934 #define XSAVE_YMMH_SPACE 144
936 static int kvm_put_xsave(X86CPU
*cpu
)
938 CPUX86State
*env
= &cpu
->env
;
939 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
940 uint16_t cwd
, swd
, twd
;
943 if (!kvm_has_xsave()) {
944 return kvm_put_fpu(cpu
);
947 memset(xsave
, 0, sizeof(struct kvm_xsave
));
949 swd
= env
->fpus
& ~(7 << 11);
950 swd
|= (env
->fpstt
& 7) << 11;
952 for (i
= 0; i
< 8; ++i
) {
953 twd
|= (!env
->fptags
[i
]) << i
;
955 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
956 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
957 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
958 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
959 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
961 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
962 sizeof env
->xmm_regs
);
963 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
964 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
965 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
966 sizeof env
->ymmh_regs
);
967 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
971 static int kvm_put_xcrs(X86CPU
*cpu
)
973 CPUX86State
*env
= &cpu
->env
;
974 struct kvm_xcrs xcrs
;
976 if (!kvm_has_xcrs()) {
982 xcrs
.xcrs
[0].xcr
= 0;
983 xcrs
.xcrs
[0].value
= env
->xcr0
;
984 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
987 static int kvm_put_sregs(X86CPU
*cpu
)
989 CPUX86State
*env
= &cpu
->env
;
990 struct kvm_sregs sregs
;
992 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
993 if (env
->interrupt_injected
>= 0) {
994 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
995 (uint64_t)1 << (env
->interrupt_injected
% 64);
998 if ((env
->eflags
& VM_MASK
)) {
999 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1000 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1001 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1002 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1003 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1004 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1006 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1007 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1008 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1009 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1010 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1011 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1014 set_seg(&sregs
.tr
, &env
->tr
);
1015 set_seg(&sregs
.ldt
, &env
->ldt
);
1017 sregs
.idt
.limit
= env
->idt
.limit
;
1018 sregs
.idt
.base
= env
->idt
.base
;
1019 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1020 sregs
.gdt
.limit
= env
->gdt
.limit
;
1021 sregs
.gdt
.base
= env
->gdt
.base
;
1022 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1024 sregs
.cr0
= env
->cr
[0];
1025 sregs
.cr2
= env
->cr
[2];
1026 sregs
.cr3
= env
->cr
[3];
1027 sregs
.cr4
= env
->cr
[4];
1029 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
1030 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
1032 sregs
.efer
= env
->efer
;
1034 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1037 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1038 uint32_t index
, uint64_t value
)
1040 entry
->index
= index
;
1041 entry
->data
= value
;
1044 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1046 CPUX86State
*env
= &cpu
->env
;
1048 struct kvm_msrs info
;
1049 struct kvm_msr_entry entries
[100];
1051 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1054 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1055 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1056 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1057 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1059 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1061 if (has_msr_hsave_pa
) {
1062 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1064 if (has_msr_tsc_adjust
) {
1065 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1067 if (has_msr_tsc_deadline
) {
1068 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1070 if (has_msr_misc_enable
) {
1071 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1072 env
->msr_ia32_misc_enable
);
1074 #ifdef TARGET_X86_64
1075 if (lm_capable_kernel
) {
1076 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1077 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1078 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1079 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1082 if (level
== KVM_PUT_FULL_STATE
) {
1084 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
1085 * writeback. Until this is fixed, we only write the offset to SMP
1086 * guests after migration, desynchronizing the VCPUs, but avoiding
1087 * huge jump-backs that would occur without any writeback at all.
1089 if (smp_cpus
== 1 || env
->tsc
!= 0) {
1090 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1094 * The following paravirtual MSRs have side effects on the guest or are
1095 * too heavy for normal writeback. Limit them to reset or full state
1098 if (level
>= KVM_PUT_RESET_STATE
) {
1099 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1100 env
->system_time_msr
);
1101 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1102 if (has_msr_async_pf_en
) {
1103 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1104 env
->async_pf_en_msr
);
1106 if (has_msr_pv_eoi_en
) {
1107 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1108 env
->pv_eoi_en_msr
);
1110 if (hyperv_hypercall_available()) {
1111 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
, 0);
1112 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
, 0);
1114 if (hyperv_vapic_recommended()) {
1115 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
1121 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1122 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1123 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1124 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1128 msr_data
.info
.nmsrs
= n
;
1130 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1135 static int kvm_get_fpu(X86CPU
*cpu
)
1137 CPUX86State
*env
= &cpu
->env
;
1141 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1146 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1147 env
->fpus
= fpu
.fsw
;
1148 env
->fpuc
= fpu
.fcw
;
1149 env
->fpop
= fpu
.last_opcode
;
1150 env
->fpip
= fpu
.last_ip
;
1151 env
->fpdp
= fpu
.last_dp
;
1152 for (i
= 0; i
< 8; ++i
) {
1153 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1155 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1156 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1157 env
->mxcsr
= fpu
.mxcsr
;
1162 static int kvm_get_xsave(X86CPU
*cpu
)
1164 CPUX86State
*env
= &cpu
->env
;
1165 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1167 uint16_t cwd
, swd
, twd
;
1169 if (!kvm_has_xsave()) {
1170 return kvm_get_fpu(cpu
);
1173 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1178 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1179 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1180 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1181 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1182 env
->fpstt
= (swd
>> 11) & 7;
1185 for (i
= 0; i
< 8; ++i
) {
1186 env
->fptags
[i
] = !((twd
>> i
) & 1);
1188 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1189 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1190 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1191 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1192 sizeof env
->fpregs
);
1193 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1194 sizeof env
->xmm_regs
);
1195 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1196 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1197 sizeof env
->ymmh_regs
);
1201 static int kvm_get_xcrs(X86CPU
*cpu
)
1203 CPUX86State
*env
= &cpu
->env
;
1205 struct kvm_xcrs xcrs
;
1207 if (!kvm_has_xcrs()) {
1211 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1216 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1217 /* Only support xcr0 now */
1218 if (xcrs
.xcrs
[0].xcr
== 0) {
1219 env
->xcr0
= xcrs
.xcrs
[0].value
;
1226 static int kvm_get_sregs(X86CPU
*cpu
)
1228 CPUX86State
*env
= &cpu
->env
;
1229 struct kvm_sregs sregs
;
1233 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1238 /* There can only be one pending IRQ set in the bitmap at a time, so try
1239 to find it and save its number instead (-1 for none). */
1240 env
->interrupt_injected
= -1;
1241 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1242 if (sregs
.interrupt_bitmap
[i
]) {
1243 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1244 env
->interrupt_injected
= i
* 64 + bit
;
1249 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1250 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1251 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1252 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1253 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1254 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1256 get_seg(&env
->tr
, &sregs
.tr
);
1257 get_seg(&env
->ldt
, &sregs
.ldt
);
1259 env
->idt
.limit
= sregs
.idt
.limit
;
1260 env
->idt
.base
= sregs
.idt
.base
;
1261 env
->gdt
.limit
= sregs
.gdt
.limit
;
1262 env
->gdt
.base
= sregs
.gdt
.base
;
1264 env
->cr
[0] = sregs
.cr0
;
1265 env
->cr
[2] = sregs
.cr2
;
1266 env
->cr
[3] = sregs
.cr3
;
1267 env
->cr
[4] = sregs
.cr4
;
1269 env
->efer
= sregs
.efer
;
1271 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1273 #define HFLAG_COPY_MASK \
1274 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1275 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1276 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1277 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1279 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1280 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1281 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1282 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1283 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1284 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1285 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1287 if (env
->efer
& MSR_EFER_LMA
) {
1288 hflags
|= HF_LMA_MASK
;
1291 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1292 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1294 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1295 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1296 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1297 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1298 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1299 !(hflags
& HF_CS32_MASK
)) {
1300 hflags
|= HF_ADDSEG_MASK
;
1302 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1303 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1306 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1311 static int kvm_get_msrs(X86CPU
*cpu
)
1313 CPUX86State
*env
= &cpu
->env
;
1315 struct kvm_msrs info
;
1316 struct kvm_msr_entry entries
[100];
1318 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1322 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1323 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1324 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1325 msrs
[n
++].index
= MSR_PAT
;
1327 msrs
[n
++].index
= MSR_STAR
;
1329 if (has_msr_hsave_pa
) {
1330 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1332 if (has_msr_tsc_adjust
) {
1333 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1335 if (has_msr_tsc_deadline
) {
1336 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1338 if (has_msr_misc_enable
) {
1339 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1342 if (!env
->tsc_valid
) {
1343 msrs
[n
++].index
= MSR_IA32_TSC
;
1344 env
->tsc_valid
= !runstate_is_running();
1347 #ifdef TARGET_X86_64
1348 if (lm_capable_kernel
) {
1349 msrs
[n
++].index
= MSR_CSTAR
;
1350 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1351 msrs
[n
++].index
= MSR_FMASK
;
1352 msrs
[n
++].index
= MSR_LSTAR
;
1355 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1356 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1357 if (has_msr_async_pf_en
) {
1358 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1360 if (has_msr_pv_eoi_en
) {
1361 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1365 msrs
[n
++].index
= MSR_MCG_STATUS
;
1366 msrs
[n
++].index
= MSR_MCG_CTL
;
1367 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1368 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1372 msr_data
.info
.nmsrs
= n
;
1373 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1378 for (i
= 0; i
< ret
; i
++) {
1379 switch (msrs
[i
].index
) {
1380 case MSR_IA32_SYSENTER_CS
:
1381 env
->sysenter_cs
= msrs
[i
].data
;
1383 case MSR_IA32_SYSENTER_ESP
:
1384 env
->sysenter_esp
= msrs
[i
].data
;
1386 case MSR_IA32_SYSENTER_EIP
:
1387 env
->sysenter_eip
= msrs
[i
].data
;
1390 env
->pat
= msrs
[i
].data
;
1393 env
->star
= msrs
[i
].data
;
1395 #ifdef TARGET_X86_64
1397 env
->cstar
= msrs
[i
].data
;
1399 case MSR_KERNELGSBASE
:
1400 env
->kernelgsbase
= msrs
[i
].data
;
1403 env
->fmask
= msrs
[i
].data
;
1406 env
->lstar
= msrs
[i
].data
;
1410 env
->tsc
= msrs
[i
].data
;
1412 case MSR_TSC_ADJUST
:
1413 env
->tsc_adjust
= msrs
[i
].data
;
1415 case MSR_IA32_TSCDEADLINE
:
1416 env
->tsc_deadline
= msrs
[i
].data
;
1418 case MSR_VM_HSAVE_PA
:
1419 env
->vm_hsave
= msrs
[i
].data
;
1421 case MSR_KVM_SYSTEM_TIME
:
1422 env
->system_time_msr
= msrs
[i
].data
;
1424 case MSR_KVM_WALL_CLOCK
:
1425 env
->wall_clock_msr
= msrs
[i
].data
;
1427 case MSR_MCG_STATUS
:
1428 env
->mcg_status
= msrs
[i
].data
;
1431 env
->mcg_ctl
= msrs
[i
].data
;
1433 case MSR_IA32_MISC_ENABLE
:
1434 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1437 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1438 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1439 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1442 case MSR_KVM_ASYNC_PF_EN
:
1443 env
->async_pf_en_msr
= msrs
[i
].data
;
1445 case MSR_KVM_PV_EOI_EN
:
1446 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1454 static int kvm_put_mp_state(X86CPU
*cpu
)
1456 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1458 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1461 static int kvm_get_mp_state(X86CPU
*cpu
)
1463 CPUX86State
*env
= &cpu
->env
;
1464 struct kvm_mp_state mp_state
;
1467 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MP_STATE
, &mp_state
);
1471 env
->mp_state
= mp_state
.mp_state
;
1472 if (kvm_irqchip_in_kernel()) {
1473 env
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1478 static int kvm_get_apic(X86CPU
*cpu
)
1480 CPUX86State
*env
= &cpu
->env
;
1481 DeviceState
*apic
= env
->apic_state
;
1482 struct kvm_lapic_state kapic
;
1485 if (apic
&& kvm_irqchip_in_kernel()) {
1486 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1491 kvm_get_apic_state(apic
, &kapic
);
1496 static int kvm_put_apic(X86CPU
*cpu
)
1498 CPUX86State
*env
= &cpu
->env
;
1499 DeviceState
*apic
= env
->apic_state
;
1500 struct kvm_lapic_state kapic
;
1502 if (apic
&& kvm_irqchip_in_kernel()) {
1503 kvm_put_apic_state(apic
, &kapic
);
1505 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1510 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1512 CPUX86State
*env
= &cpu
->env
;
1513 struct kvm_vcpu_events events
;
1515 if (!kvm_has_vcpu_events()) {
1519 events
.exception
.injected
= (env
->exception_injected
>= 0);
1520 events
.exception
.nr
= env
->exception_injected
;
1521 events
.exception
.has_error_code
= env
->has_error_code
;
1522 events
.exception
.error_code
= env
->error_code
;
1523 events
.exception
.pad
= 0;
1525 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1526 events
.interrupt
.nr
= env
->interrupt_injected
;
1527 events
.interrupt
.soft
= env
->soft_interrupt
;
1529 events
.nmi
.injected
= env
->nmi_injected
;
1530 events
.nmi
.pending
= env
->nmi_pending
;
1531 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1534 events
.sipi_vector
= env
->sipi_vector
;
1537 if (level
>= KVM_PUT_RESET_STATE
) {
1539 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1542 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1545 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1547 CPUX86State
*env
= &cpu
->env
;
1548 struct kvm_vcpu_events events
;
1551 if (!kvm_has_vcpu_events()) {
1555 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1559 env
->exception_injected
=
1560 events
.exception
.injected
? events
.exception
.nr
: -1;
1561 env
->has_error_code
= events
.exception
.has_error_code
;
1562 env
->error_code
= events
.exception
.error_code
;
1564 env
->interrupt_injected
=
1565 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1566 env
->soft_interrupt
= events
.interrupt
.soft
;
1568 env
->nmi_injected
= events
.nmi
.injected
;
1569 env
->nmi_pending
= events
.nmi
.pending
;
1570 if (events
.nmi
.masked
) {
1571 env
->hflags2
|= HF2_NMI_MASK
;
1573 env
->hflags2
&= ~HF2_NMI_MASK
;
1576 env
->sipi_vector
= events
.sipi_vector
;
1581 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1583 CPUX86State
*env
= &cpu
->env
;
1585 unsigned long reinject_trap
= 0;
1587 if (!kvm_has_vcpu_events()) {
1588 if (env
->exception_injected
== 1) {
1589 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1590 } else if (env
->exception_injected
== 3) {
1591 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1593 env
->exception_injected
= -1;
1597 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1598 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1599 * by updating the debug state once again if single-stepping is on.
1600 * Another reason to call kvm_update_guest_debug here is a pending debug
1601 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1602 * reinject them via SET_GUEST_DEBUG.
1604 if (reinject_trap
||
1605 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1606 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1611 static int kvm_put_debugregs(X86CPU
*cpu
)
1613 CPUX86State
*env
= &cpu
->env
;
1614 struct kvm_debugregs dbgregs
;
1617 if (!kvm_has_debugregs()) {
1621 for (i
= 0; i
< 4; i
++) {
1622 dbgregs
.db
[i
] = env
->dr
[i
];
1624 dbgregs
.dr6
= env
->dr
[6];
1625 dbgregs
.dr7
= env
->dr
[7];
1628 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1631 static int kvm_get_debugregs(X86CPU
*cpu
)
1633 CPUX86State
*env
= &cpu
->env
;
1634 struct kvm_debugregs dbgregs
;
1637 if (!kvm_has_debugregs()) {
1641 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1645 for (i
= 0; i
< 4; i
++) {
1646 env
->dr
[i
] = dbgregs
.db
[i
];
1648 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1649 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1654 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1656 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1659 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1661 ret
= kvm_getput_regs(x86_cpu
, 1);
1665 ret
= kvm_put_xsave(x86_cpu
);
1669 ret
= kvm_put_xcrs(x86_cpu
);
1673 ret
= kvm_put_sregs(x86_cpu
);
1677 /* must be before kvm_put_msrs */
1678 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1682 ret
= kvm_put_msrs(x86_cpu
, level
);
1686 if (level
>= KVM_PUT_RESET_STATE
) {
1687 ret
= kvm_put_mp_state(x86_cpu
);
1691 ret
= kvm_put_apic(x86_cpu
);
1696 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1700 ret
= kvm_put_debugregs(x86_cpu
);
1705 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1712 int kvm_arch_get_registers(CPUState
*cs
)
1714 X86CPU
*cpu
= X86_CPU(cs
);
1717 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1719 ret
= kvm_getput_regs(cpu
, 0);
1723 ret
= kvm_get_xsave(cpu
);
1727 ret
= kvm_get_xcrs(cpu
);
1731 ret
= kvm_get_sregs(cpu
);
1735 ret
= kvm_get_msrs(cpu
);
1739 ret
= kvm_get_mp_state(cpu
);
1743 ret
= kvm_get_apic(cpu
);
1747 ret
= kvm_get_vcpu_events(cpu
);
1751 ret
= kvm_get_debugregs(cpu
);
1758 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1760 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1761 CPUX86State
*env
= &x86_cpu
->env
;
1765 if (env
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1766 env
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1767 DPRINTF("injected NMI\n");
1768 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
1770 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
1775 if (!kvm_irqchip_in_kernel()) {
1776 /* Force the VCPU out of its inner loop to process any INIT requests
1777 * or pending TPR access reports. */
1778 if (env
->interrupt_request
&
1779 (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
1780 cpu
->exit_request
= 1;
1783 /* Try to inject an interrupt if the guest can accept it */
1784 if (run
->ready_for_interrupt_injection
&&
1785 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1786 (env
->eflags
& IF_MASK
)) {
1789 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1790 irq
= cpu_get_pic_interrupt(env
);
1792 struct kvm_interrupt intr
;
1795 DPRINTF("injected interrupt %d\n", irq
);
1796 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
1799 "KVM: injection failed, interrupt lost (%s)\n",
1805 /* If we have an interrupt but the guest is not ready to receive an
1806 * interrupt, request an interrupt window exit. This will
1807 * cause a return to userspace as soon as the guest is ready to
1808 * receive interrupts. */
1809 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
1810 run
->request_interrupt_window
= 1;
1812 run
->request_interrupt_window
= 0;
1815 DPRINTF("setting tpr\n");
1816 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1820 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
1822 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1823 CPUX86State
*env
= &x86_cpu
->env
;
1826 env
->eflags
|= IF_MASK
;
1828 env
->eflags
&= ~IF_MASK
;
1830 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1831 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1834 int kvm_arch_process_async_events(CPUState
*cs
)
1836 X86CPU
*cpu
= X86_CPU(cs
);
1837 CPUX86State
*env
= &cpu
->env
;
1839 if (env
->interrupt_request
& CPU_INTERRUPT_MCE
) {
1840 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
1841 assert(env
->mcg_cap
);
1843 env
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
1845 kvm_cpu_synchronize_state(env
);
1847 if (env
->exception_injected
== EXCP08_DBLE
) {
1848 /* this means triple fault */
1849 qemu_system_reset_request();
1850 cs
->exit_request
= 1;
1853 env
->exception_injected
= EXCP12_MCHK
;
1854 env
->has_error_code
= 0;
1857 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
1858 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1862 if (kvm_irqchip_in_kernel()) {
1866 if (env
->interrupt_request
& CPU_INTERRUPT_POLL
) {
1867 env
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
1868 apic_poll_irq(env
->apic_state
);
1870 if (((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1871 (env
->eflags
& IF_MASK
)) ||
1872 (env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1875 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1876 kvm_cpu_synchronize_state(env
);
1879 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1880 kvm_cpu_synchronize_state(env
);
1883 if (env
->interrupt_request
& CPU_INTERRUPT_TPR
) {
1884 env
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
1885 kvm_cpu_synchronize_state(env
);
1886 apic_handle_tpr_access_report(env
->apic_state
, env
->eip
,
1887 env
->tpr_access_type
);
1893 static int kvm_handle_halt(X86CPU
*cpu
)
1895 CPUX86State
*env
= &cpu
->env
;
1897 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1898 (env
->eflags
& IF_MASK
)) &&
1899 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1907 static int kvm_handle_tpr_access(X86CPU
*cpu
)
1909 CPUX86State
*env
= &cpu
->env
;
1910 CPUState
*cs
= CPU(cpu
);
1911 struct kvm_run
*run
= cs
->kvm_run
;
1913 apic_handle_tpr_access_report(env
->apic_state
, run
->tpr_access
.rip
,
1914 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
1919 int kvm_arch_insert_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1921 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1922 static const uint8_t int3
= 0xcc;
1924 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1925 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
1931 int kvm_arch_remove_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1933 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1936 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1937 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
1949 static int nb_hw_breakpoint
;
1951 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1955 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1956 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1957 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
1964 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1965 target_ulong len
, int type
)
1968 case GDB_BREAKPOINT_HW
:
1971 case GDB_WATCHPOINT_WRITE
:
1972 case GDB_WATCHPOINT_ACCESS
:
1979 if (addr
& (len
- 1)) {
1991 if (nb_hw_breakpoint
== 4) {
1994 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
1997 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1998 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1999 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2005 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2006 target_ulong len
, int type
)
2010 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2015 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2020 void kvm_arch_remove_all_hw_breakpoints(void)
2022 nb_hw_breakpoint
= 0;
2025 static CPUWatchpoint hw_watchpoint
;
2027 static int kvm_handle_debug(X86CPU
*cpu
,
2028 struct kvm_debug_exit_arch
*arch_info
)
2030 CPUX86State
*env
= &cpu
->env
;
2034 if (arch_info
->exception
== 1) {
2035 if (arch_info
->dr6
& (1 << 14)) {
2036 if (env
->singlestep_enabled
) {
2040 for (n
= 0; n
< 4; n
++) {
2041 if (arch_info
->dr6
& (1 << n
)) {
2042 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2048 env
->watchpoint_hit
= &hw_watchpoint
;
2049 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2050 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2054 env
->watchpoint_hit
= &hw_watchpoint
;
2055 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2056 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2062 } else if (kvm_find_sw_breakpoint(CPU(cpu
), arch_info
->pc
)) {
2066 cpu_synchronize_state(env
);
2067 assert(env
->exception_injected
== -1);
2070 env
->exception_injected
= arch_info
->exception
;
2071 env
->has_error_code
= 0;
2077 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2079 const uint8_t type_code
[] = {
2080 [GDB_BREAKPOINT_HW
] = 0x0,
2081 [GDB_WATCHPOINT_WRITE
] = 0x1,
2082 [GDB_WATCHPOINT_ACCESS
] = 0x3
2084 const uint8_t len_code
[] = {
2085 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2089 if (kvm_sw_breakpoints_active(cpu
)) {
2090 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2092 if (nb_hw_breakpoint
> 0) {
2093 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2094 dbg
->arch
.debugreg
[7] = 0x0600;
2095 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2096 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2097 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2098 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2099 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2104 static bool host_supports_vmx(void)
2106 uint32_t ecx
, unused
;
2108 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2109 return ecx
& CPUID_EXT_VMX
;
2112 #define VMX_INVALID_GUEST_STATE 0x80000021
2114 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2116 X86CPU
*cpu
= X86_CPU(cs
);
2120 switch (run
->exit_reason
) {
2122 DPRINTF("handle_hlt\n");
2123 ret
= kvm_handle_halt(cpu
);
2125 case KVM_EXIT_SET_TPR
:
2128 case KVM_EXIT_TPR_ACCESS
:
2129 ret
= kvm_handle_tpr_access(cpu
);
2131 case KVM_EXIT_FAIL_ENTRY
:
2132 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2133 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2135 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2137 "\nIf you're running a guest on an Intel machine without "
2138 "unrestricted mode\n"
2139 "support, the failure can be most likely due to the guest "
2140 "entering an invalid\n"
2141 "state for Intel VT. For example, the guest maybe running "
2142 "in big real mode\n"
2143 "which is not supported on less recent Intel processors."
2148 case KVM_EXIT_EXCEPTION
:
2149 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2150 run
->ex
.exception
, run
->ex
.error_code
);
2153 case KVM_EXIT_DEBUG
:
2154 DPRINTF("kvm_exit_debug\n");
2155 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2158 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2166 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2168 X86CPU
*cpu
= X86_CPU(cs
);
2169 CPUX86State
*env
= &cpu
->env
;
2171 kvm_cpu_synchronize_state(env
);
2172 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2173 ((env
->segs
[R_CS
].selector
& 3) != 3);
2176 void kvm_arch_init_irq_routing(KVMState
*s
)
2178 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2179 /* If kernel can't do irq routing, interrupt source
2180 * override 0->2 cannot be set up as required by HPET.
2181 * So we have to disable it.
2185 /* We know at this point that we're using the in-kernel
2186 * irqchip, so we can use irqfds, and on x86 we know
2187 * we can use msi via irqfd and GSI routing.
2189 kvm_irqfds_allowed
= true;
2190 kvm_msi_via_irqfd_allowed
= true;
2191 kvm_gsi_routing_allowed
= true;
2194 /* Classic KVM device assignment interface. Will remain x86 only. */
2195 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2196 uint32_t flags
, uint32_t *dev_id
)
2198 struct kvm_assigned_pci_dev dev_data
= {
2199 .segnr
= dev_addr
->domain
,
2200 .busnr
= dev_addr
->bus
,
2201 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2206 dev_data
.assigned_dev_id
=
2207 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2209 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2214 *dev_id
= dev_data
.assigned_dev_id
;
2219 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2221 struct kvm_assigned_pci_dev dev_data
= {
2222 .assigned_dev_id
= dev_id
,
2225 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2228 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2229 uint32_t irq_type
, uint32_t guest_irq
)
2231 struct kvm_assigned_irq assigned_irq
= {
2232 .assigned_dev_id
= dev_id
,
2233 .guest_irq
= guest_irq
,
2237 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2238 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2240 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2244 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2247 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2248 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2250 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2253 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2255 struct kvm_assigned_pci_dev dev_data
= {
2256 .assigned_dev_id
= dev_id
,
2257 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2260 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2263 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2266 struct kvm_assigned_irq assigned_irq
= {
2267 .assigned_dev_id
= dev_id
,
2271 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2274 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2276 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2277 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2280 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2282 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2283 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2286 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2288 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2289 KVM_DEV_IRQ_HOST_MSI
);
2292 bool kvm_device_msix_supported(KVMState
*s
)
2294 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2295 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2296 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2299 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2300 uint32_t nr_vectors
)
2302 struct kvm_assigned_msix_nr msix_nr
= {
2303 .assigned_dev_id
= dev_id
,
2304 .entry_nr
= nr_vectors
,
2307 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2310 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2313 struct kvm_assigned_msix_entry msix_entry
= {
2314 .assigned_dev_id
= dev_id
,
2319 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2322 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2324 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2325 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2328 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2330 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2331 KVM_DEV_IRQ_HOST_MSIX
);