4 * Copyright (C) 2006-2008 Qumranet Technologies
5 * Copyright IBM, Corp. 2008
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
18 #include <sys/utsname.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_para.h>
23 #include "qemu-common.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/kvm.h"
28 #include "exec/gdbstub.h"
29 #include "qemu/host-utils.h"
30 #include "qemu/config-file.h"
31 #include "hw/i386/pc.h"
32 #include "hw/i386/apic.h"
33 #include "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 bool has_msr_kvm_steal_time
;
72 static int lm_capable_kernel
;
74 bool kvm_allows_irq0_override(void)
76 return !kvm_irqchip_in_kernel() || kvm_has_gsi_routing();
79 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
81 struct kvm_cpuid2
*cpuid
;
84 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
85 cpuid
= (struct kvm_cpuid2
*)g_malloc0(size
);
87 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
88 if (r
== 0 && cpuid
->nent
>= max
) {
96 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
104 /* Run KVM_GET_SUPPORTED_CPUID ioctl(), allocating a buffer large enough
107 static struct kvm_cpuid2
*get_supported_cpuid(KVMState
*s
)
109 struct kvm_cpuid2
*cpuid
;
111 while ((cpuid
= try_get_cpuid(s
, max
)) == NULL
) {
117 struct kvm_para_features
{
120 } para_features
[] = {
121 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
122 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
123 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
124 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
128 static int get_para_features(KVMState
*s
)
132 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
133 if (kvm_check_extension(s
, para_features
[i
].cap
)) {
134 features
|= (1 << para_features
[i
].feature
);
142 /* Returns the value for a specific register on the cpuid entry
144 static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2
*entry
, int reg
)
164 /* Find matching entry for function/index on kvm_cpuid2 struct
166 static struct kvm_cpuid_entry2
*cpuid_find_entry(struct kvm_cpuid2
*cpuid
,
171 for (i
= 0; i
< cpuid
->nent
; ++i
) {
172 if (cpuid
->entries
[i
].function
== function
&&
173 cpuid
->entries
[i
].index
== index
) {
174 return &cpuid
->entries
[i
];
181 uint32_t kvm_arch_get_supported_cpuid(KVMState
*s
, uint32_t function
,
182 uint32_t index
, int reg
)
184 struct kvm_cpuid2
*cpuid
;
186 uint32_t cpuid_1_edx
;
189 cpuid
= get_supported_cpuid(s
);
191 struct kvm_cpuid_entry2
*entry
= cpuid_find_entry(cpuid
, function
, index
);
194 ret
= cpuid_entry_get_reg(entry
, reg
);
197 /* Fixups for the data returned by KVM, below */
199 if (function
== 1 && reg
== R_EDX
) {
200 /* KVM before 2.6.30 misreports the following features */
201 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
202 } else if (function
== 1 && reg
== R_ECX
) {
203 /* We can set the hypervisor flag, even if KVM does not return it on
204 * GET_SUPPORTED_CPUID
206 ret
|= CPUID_EXT_HYPERVISOR
;
207 /* tsc-deadline flag is not returned by GET_SUPPORTED_CPUID, but it
208 * can be enabled if the kernel has KVM_CAP_TSC_DEADLINE_TIMER,
209 * and the irqchip is in the kernel.
211 if (kvm_irqchip_in_kernel() &&
212 kvm_check_extension(s
, KVM_CAP_TSC_DEADLINE_TIMER
)) {
213 ret
|= CPUID_EXT_TSC_DEADLINE_TIMER
;
216 /* x2apic is reported by GET_SUPPORTED_CPUID, but it can't be enabled
217 * without the in-kernel irqchip
219 if (!kvm_irqchip_in_kernel()) {
220 ret
&= ~CPUID_EXT_X2APIC
;
222 } else if (function
== 0x80000001 && reg
== R_EDX
) {
223 /* On Intel, kvm returns cpuid according to the Intel spec,
224 * so add missing bits according to the AMD spec:
226 cpuid_1_edx
= kvm_arch_get_supported_cpuid(s
, 1, 0, R_EDX
);
227 ret
|= cpuid_1_edx
& CPUID_EXT2_AMD_ALIASES
;
232 /* fallback for older kernels */
233 if ((function
== KVM_CPUID_FEATURES
) && !found
) {
234 ret
= get_para_features(s
);
240 typedef struct HWPoisonPage
{
242 QLIST_ENTRY(HWPoisonPage
) list
;
245 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
246 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
248 static void kvm_unpoison_all(void *param
)
250 HWPoisonPage
*page
, *next_page
;
252 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
253 QLIST_REMOVE(page
, list
);
254 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
259 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
263 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
264 if (page
->ram_addr
== ram_addr
) {
268 page
= g_malloc(sizeof(HWPoisonPage
));
269 page
->ram_addr
= ram_addr
;
270 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
273 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
278 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
281 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
286 static void kvm_mce_inject(X86CPU
*cpu
, hwaddr paddr
, int code
)
288 CPUX86State
*env
= &cpu
->env
;
289 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
290 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
291 uint64_t mcg_status
= MCG_STATUS_MCIP
;
293 if (code
== BUS_MCEERR_AR
) {
294 status
|= MCI_STATUS_AR
| 0x134;
295 mcg_status
|= MCG_STATUS_EIPV
;
298 mcg_status
|= MCG_STATUS_RIPV
;
300 cpu_x86_inject_mce(NULL
, cpu
, 9, status
, mcg_status
, paddr
,
301 (MCM_ADDR_PHYS
<< 6) | 0xc,
302 cpu_x86_support_mca_broadcast(env
) ?
303 MCE_INJECT_BROADCAST
: 0);
306 static void hardware_memory_error(void)
308 fprintf(stderr
, "Hardware memory error!\n");
312 int kvm_arch_on_sigbus_vcpu(CPUState
*c
, int code
, void *addr
)
314 X86CPU
*cpu
= X86_CPU(c
);
315 CPUX86State
*env
= &cpu
->env
;
319 if ((env
->mcg_cap
& MCG_SER_P
) && addr
320 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
321 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
322 !kvm_physical_memory_addr_from_host(c
->kvm_state
, addr
, &paddr
)) {
323 fprintf(stderr
, "Hardware memory error for memory used by "
324 "QEMU itself instead of guest system!\n");
325 /* Hope we are lucky for AO MCE */
326 if (code
== BUS_MCEERR_AO
) {
329 hardware_memory_error();
332 kvm_hwpoison_page_add(ram_addr
);
333 kvm_mce_inject(cpu
, paddr
, code
);
335 if (code
== BUS_MCEERR_AO
) {
337 } else if (code
== BUS_MCEERR_AR
) {
338 hardware_memory_error();
346 int kvm_arch_on_sigbus(int code
, void *addr
)
348 if ((first_cpu
->mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
352 /* Hope we are lucky for AO MCE */
353 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
354 !kvm_physical_memory_addr_from_host(CPU(first_cpu
)->kvm_state
,
356 fprintf(stderr
, "Hardware memory error for memory used by "
357 "QEMU itself instead of guest system!: %p\n", addr
);
360 kvm_hwpoison_page_add(ram_addr
);
361 kvm_mce_inject(x86_env_get_cpu(first_cpu
), paddr
, code
);
363 if (code
== BUS_MCEERR_AO
) {
365 } else if (code
== BUS_MCEERR_AR
) {
366 hardware_memory_error();
374 static int kvm_inject_mce_oldstyle(X86CPU
*cpu
)
376 CPUX86State
*env
= &cpu
->env
;
378 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
379 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
380 struct kvm_x86_mce mce
;
382 env
->exception_injected
= -1;
385 * There must be at least one bank in use if an MCE is pending.
386 * Find it and use its values for the event injection.
388 for (bank
= 0; bank
< bank_num
; bank
++) {
389 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
393 assert(bank
< bank_num
);
396 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
397 mce
.mcg_status
= env
->mcg_status
;
398 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
399 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
401 return kvm_vcpu_ioctl(CPU(cpu
), KVM_X86_SET_MCE
, &mce
);
406 static void cpu_update_state(void *opaque
, int running
, RunState state
)
408 CPUX86State
*env
= opaque
;
411 env
->tsc_valid
= false;
415 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
417 X86CPU
*cpu
= X86_CPU(cs
);
418 return cpu
->env
.cpuid_apic_id
;
421 #define KVM_MAX_CPUID_ENTRIES 100
423 int kvm_arch_init_vcpu(CPUState
*cs
)
426 struct kvm_cpuid2 cpuid
;
427 struct kvm_cpuid_entry2 entries
[KVM_MAX_CPUID_ENTRIES
];
428 } QEMU_PACKED cpuid_data
;
429 X86CPU
*cpu
= X86_CPU(cs
);
430 CPUX86State
*env
= &cpu
->env
;
431 uint32_t limit
, i
, j
, cpuid_i
;
433 struct kvm_cpuid_entry2
*c
;
434 uint32_t signature
[3];
439 /* Paravirtualization CPUIDs */
440 c
= &cpuid_data
.entries
[cpuid_i
++];
441 memset(c
, 0, sizeof(*c
));
442 c
->function
= KVM_CPUID_SIGNATURE
;
443 if (!hyperv_enabled()) {
444 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
447 memcpy(signature
, "Microsoft Hv", 12);
448 c
->eax
= HYPERV_CPUID_MIN
;
450 c
->ebx
= signature
[0];
451 c
->ecx
= signature
[1];
452 c
->edx
= signature
[2];
454 c
= &cpuid_data
.entries
[cpuid_i
++];
455 memset(c
, 0, sizeof(*c
));
456 c
->function
= KVM_CPUID_FEATURES
;
457 c
->eax
= env
->features
[FEAT_KVM
];
459 if (hyperv_enabled()) {
460 memcpy(signature
, "Hv#1\0\0\0\0\0\0\0\0", 12);
461 c
->eax
= signature
[0];
463 c
= &cpuid_data
.entries
[cpuid_i
++];
464 memset(c
, 0, sizeof(*c
));
465 c
->function
= HYPERV_CPUID_VERSION
;
469 c
= &cpuid_data
.entries
[cpuid_i
++];
470 memset(c
, 0, sizeof(*c
));
471 c
->function
= HYPERV_CPUID_FEATURES
;
472 if (hyperv_relaxed_timing_enabled()) {
473 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
475 if (hyperv_vapic_recommended()) {
476 c
->eax
|= HV_X64_MSR_HYPERCALL_AVAILABLE
;
477 c
->eax
|= HV_X64_MSR_APIC_ACCESS_AVAILABLE
;
480 c
= &cpuid_data
.entries
[cpuid_i
++];
481 memset(c
, 0, sizeof(*c
));
482 c
->function
= HYPERV_CPUID_ENLIGHTMENT_INFO
;
483 if (hyperv_relaxed_timing_enabled()) {
484 c
->eax
|= HV_X64_RELAXED_TIMING_RECOMMENDED
;
486 if (hyperv_vapic_recommended()) {
487 c
->eax
|= HV_X64_APIC_ACCESS_RECOMMENDED
;
489 c
->ebx
= hyperv_get_spinlock_retries();
491 c
= &cpuid_data
.entries
[cpuid_i
++];
492 memset(c
, 0, sizeof(*c
));
493 c
->function
= HYPERV_CPUID_IMPLEMENT_LIMITS
;
497 c
= &cpuid_data
.entries
[cpuid_i
++];
498 memset(c
, 0, sizeof(*c
));
499 c
->function
= KVM_CPUID_SIGNATURE_NEXT
;
500 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
502 c
->ebx
= signature
[0];
503 c
->ecx
= signature
[1];
504 c
->edx
= signature
[2];
507 has_msr_async_pf_en
= c
->eax
& (1 << KVM_FEATURE_ASYNC_PF
);
509 has_msr_pv_eoi_en
= c
->eax
& (1 << KVM_FEATURE_PV_EOI
);
511 has_msr_kvm_steal_time
= c
->eax
& (1 << KVM_FEATURE_STEAL_TIME
);
513 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
515 for (i
= 0; i
<= limit
; i
++) {
516 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
517 fprintf(stderr
, "unsupported level value: 0x%x\n", limit
);
520 c
= &cpuid_data
.entries
[cpuid_i
++];
524 /* Keep reading function 2 till all the input is received */
528 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
529 KVM_CPUID_FLAG_STATE_READ_NEXT
;
530 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
531 times
= c
->eax
& 0xff;
533 for (j
= 1; j
< times
; ++j
) {
534 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
535 fprintf(stderr
, "cpuid_data is full, no space for "
536 "cpuid(eax:2):eax & 0xf = 0x%x\n", times
);
539 c
= &cpuid_data
.entries
[cpuid_i
++];
541 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
542 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
550 if (i
== 0xd && j
== 64) {
554 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
556 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
558 if (i
== 4 && c
->eax
== 0) {
561 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
564 if (i
== 0xd && c
->eax
== 0) {
567 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
568 fprintf(stderr
, "cpuid_data is full, no space for "
569 "cpuid(eax:0x%x,ecx:0x%x)\n", i
, j
);
572 c
= &cpuid_data
.entries
[cpuid_i
++];
578 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
582 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
584 for (i
= 0x80000000; i
<= limit
; i
++) {
585 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
586 fprintf(stderr
, "unsupported xlevel value: 0x%x\n", limit
);
589 c
= &cpuid_data
.entries
[cpuid_i
++];
593 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
596 /* Call Centaur's CPUID instructions they are supported. */
597 if (env
->cpuid_xlevel2
> 0) {
598 cpu_x86_cpuid(env
, 0xC0000000, 0, &limit
, &unused
, &unused
, &unused
);
600 for (i
= 0xC0000000; i
<= limit
; i
++) {
601 if (cpuid_i
== KVM_MAX_CPUID_ENTRIES
) {
602 fprintf(stderr
, "unsupported xlevel2 value: 0x%x\n", limit
);
605 c
= &cpuid_data
.entries
[cpuid_i
++];
609 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
613 cpuid_data
.cpuid
.nent
= cpuid_i
;
615 if (((env
->cpuid_version
>> 8)&0xF) >= 6
616 && (env
->features
[FEAT_1_EDX
] & (CPUID_MCE
| CPUID_MCA
)) ==
617 (CPUID_MCE
| CPUID_MCA
)
618 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
623 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
625 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
629 if (banks
> MCE_BANKS_DEF
) {
630 banks
= MCE_BANKS_DEF
;
632 mcg_cap
&= MCE_CAP_DEF
;
634 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
636 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
640 env
->mcg_cap
= mcg_cap
;
643 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
645 cpuid_data
.cpuid
.padding
= 0;
646 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
651 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
652 if (r
&& env
->tsc_khz
) {
653 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
655 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
660 if (kvm_has_xsave()) {
661 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
667 void kvm_arch_reset_vcpu(CPUState
*cs
)
669 X86CPU
*cpu
= X86_CPU(cs
);
670 CPUX86State
*env
= &cpu
->env
;
672 env
->exception_injected
= -1;
673 env
->interrupt_injected
= -1;
675 if (kvm_irqchip_in_kernel()) {
676 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
677 KVM_MP_STATE_UNINITIALIZED
;
679 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
683 static int kvm_get_supported_msrs(KVMState
*s
)
685 static int kvm_supported_msrs
;
689 if (kvm_supported_msrs
== 0) {
690 struct kvm_msr_list msr_list
, *kvm_msr_list
;
692 kvm_supported_msrs
= -1;
694 /* Obtain MSR list from KVM. These are the MSRs that we must
697 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
698 if (ret
< 0 && ret
!= -E2BIG
) {
701 /* Old kernel modules had a bug and could write beyond the provided
702 memory. Allocate at least a safe amount of 1K. */
703 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
705 sizeof(msr_list
.indices
[0])));
707 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
708 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
712 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
713 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
717 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
718 has_msr_hsave_pa
= true;
721 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
722 has_msr_tsc_adjust
= true;
725 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
726 has_msr_tsc_deadline
= true;
729 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
730 has_msr_misc_enable
= true;
736 g_free(kvm_msr_list
);
742 int kvm_arch_init(KVMState
*s
)
744 QemuOptsList
*list
= qemu_find_opts("machine");
745 uint64_t identity_base
= 0xfffbc000;
748 struct utsname utsname
;
750 ret
= kvm_get_supported_msrs(s
);
756 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
759 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
760 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
761 * Since these must be part of guest physical memory, we need to allocate
762 * them, both by setting their start addresses in the kernel and by
763 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
765 * Older KVM versions may not support setting the identity map base. In
766 * that case we need to stick with the default, i.e. a 256K maximum BIOS
769 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
770 /* Allows up to 16M BIOSes. */
771 identity_base
= 0xfeffc000;
773 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
779 /* Set TSS base one page after EPT identity map. */
780 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
785 /* Tell fw_cfg to notify the BIOS to reserve the range. */
786 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
788 fprintf(stderr
, "e820_add_entry() table is full\n");
791 qemu_register_reset(kvm_unpoison_all
, NULL
);
793 if (!QTAILQ_EMPTY(&list
->head
)) {
794 shadow_mem
= qemu_opt_get_size(QTAILQ_FIRST(&list
->head
),
795 "kvm_shadow_mem", -1);
796 if (shadow_mem
!= -1) {
798 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
807 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
809 lhs
->selector
= rhs
->selector
;
810 lhs
->base
= rhs
->base
;
811 lhs
->limit
= rhs
->limit
;
823 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
825 unsigned flags
= rhs
->flags
;
826 lhs
->selector
= rhs
->selector
;
827 lhs
->base
= rhs
->base
;
828 lhs
->limit
= rhs
->limit
;
829 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
830 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
831 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
832 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
833 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
834 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
835 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
836 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
841 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
843 lhs
->selector
= rhs
->selector
;
844 lhs
->base
= rhs
->base
;
845 lhs
->limit
= rhs
->limit
;
846 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
847 (rhs
->present
* DESC_P_MASK
) |
848 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
849 (rhs
->db
<< DESC_B_SHIFT
) |
850 (rhs
->s
* DESC_S_MASK
) |
851 (rhs
->l
<< DESC_L_SHIFT
) |
852 (rhs
->g
* DESC_G_MASK
) |
853 (rhs
->avl
* DESC_AVL_MASK
);
856 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
859 *kvm_reg
= *qemu_reg
;
861 *qemu_reg
= *kvm_reg
;
865 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
867 CPUX86State
*env
= &cpu
->env
;
868 struct kvm_regs regs
;
872 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
878 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
879 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
880 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
881 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
882 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
883 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
884 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
885 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
887 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
888 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
889 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
890 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
891 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
892 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
893 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
894 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
897 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
898 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
901 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
907 static int kvm_put_fpu(X86CPU
*cpu
)
909 CPUX86State
*env
= &cpu
->env
;
913 memset(&fpu
, 0, sizeof fpu
);
914 fpu
.fsw
= env
->fpus
& ~(7 << 11);
915 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
917 fpu
.last_opcode
= env
->fpop
;
918 fpu
.last_ip
= env
->fpip
;
919 fpu
.last_dp
= env
->fpdp
;
920 for (i
= 0; i
< 8; ++i
) {
921 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
923 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
924 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
925 fpu
.mxcsr
= env
->mxcsr
;
927 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
930 #define XSAVE_FCW_FSW 0
931 #define XSAVE_FTW_FOP 1
932 #define XSAVE_CWD_RIP 2
933 #define XSAVE_CWD_RDP 4
934 #define XSAVE_MXCSR 6
935 #define XSAVE_ST_SPACE 8
936 #define XSAVE_XMM_SPACE 40
937 #define XSAVE_XSTATE_BV 128
938 #define XSAVE_YMMH_SPACE 144
940 static int kvm_put_xsave(X86CPU
*cpu
)
942 CPUX86State
*env
= &cpu
->env
;
943 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
944 uint16_t cwd
, swd
, twd
;
947 if (!kvm_has_xsave()) {
948 return kvm_put_fpu(cpu
);
951 memset(xsave
, 0, sizeof(struct kvm_xsave
));
953 swd
= env
->fpus
& ~(7 << 11);
954 swd
|= (env
->fpstt
& 7) << 11;
956 for (i
= 0; i
< 8; ++i
) {
957 twd
|= (!env
->fptags
[i
]) << i
;
959 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
960 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
961 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
962 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
963 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
965 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
966 sizeof env
->xmm_regs
);
967 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
968 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
969 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
970 sizeof env
->ymmh_regs
);
971 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
975 static int kvm_put_xcrs(X86CPU
*cpu
)
977 CPUX86State
*env
= &cpu
->env
;
978 struct kvm_xcrs xcrs
;
980 if (!kvm_has_xcrs()) {
986 xcrs
.xcrs
[0].xcr
= 0;
987 xcrs
.xcrs
[0].value
= env
->xcr0
;
988 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
991 static int kvm_put_sregs(X86CPU
*cpu
)
993 CPUX86State
*env
= &cpu
->env
;
994 struct kvm_sregs sregs
;
996 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
997 if (env
->interrupt_injected
>= 0) {
998 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
999 (uint64_t)1 << (env
->interrupt_injected
% 64);
1002 if ((env
->eflags
& VM_MASK
)) {
1003 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1004 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1005 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1006 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1007 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1008 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1010 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1011 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1012 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1013 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1014 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1015 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1018 set_seg(&sregs
.tr
, &env
->tr
);
1019 set_seg(&sregs
.ldt
, &env
->ldt
);
1021 sregs
.idt
.limit
= env
->idt
.limit
;
1022 sregs
.idt
.base
= env
->idt
.base
;
1023 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1024 sregs
.gdt
.limit
= env
->gdt
.limit
;
1025 sregs
.gdt
.base
= env
->gdt
.base
;
1026 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1028 sregs
.cr0
= env
->cr
[0];
1029 sregs
.cr2
= env
->cr
[2];
1030 sregs
.cr3
= env
->cr
[3];
1031 sregs
.cr4
= env
->cr
[4];
1033 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
1034 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
1036 sregs
.efer
= env
->efer
;
1038 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1041 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1042 uint32_t index
, uint64_t value
)
1044 entry
->index
= index
;
1045 entry
->data
= value
;
1048 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1050 CPUX86State
*env
= &cpu
->env
;
1052 struct kvm_msrs info
;
1053 struct kvm_msr_entry entries
[100];
1055 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1058 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1059 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1060 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1061 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1063 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1065 if (has_msr_hsave_pa
) {
1066 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1068 if (has_msr_tsc_adjust
) {
1069 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1071 if (has_msr_tsc_deadline
) {
1072 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1074 if (has_msr_misc_enable
) {
1075 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1076 env
->msr_ia32_misc_enable
);
1078 #ifdef TARGET_X86_64
1079 if (lm_capable_kernel
) {
1080 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1081 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1082 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1083 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1086 if (level
== KVM_PUT_FULL_STATE
) {
1088 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
1089 * writeback. Until this is fixed, we only write the offset to SMP
1090 * guests after migration, desynchronizing the VCPUs, but avoiding
1091 * huge jump-backs that would occur without any writeback at all.
1093 if (smp_cpus
== 1 || env
->tsc
!= 0) {
1094 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1098 * The following paravirtual MSRs have side effects on the guest or are
1099 * too heavy for normal writeback. Limit them to reset or full state
1102 if (level
>= KVM_PUT_RESET_STATE
) {
1103 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1104 env
->system_time_msr
);
1105 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1106 if (has_msr_async_pf_en
) {
1107 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1108 env
->async_pf_en_msr
);
1110 if (has_msr_pv_eoi_en
) {
1111 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1112 env
->pv_eoi_en_msr
);
1114 if (has_msr_kvm_steal_time
) {
1115 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_STEAL_TIME
,
1116 env
->steal_time_msr
);
1118 if (hyperv_hypercall_available()) {
1119 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
, 0);
1120 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
, 0);
1122 if (hyperv_vapic_recommended()) {
1123 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
1129 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1130 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1131 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1132 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1136 msr_data
.info
.nmsrs
= n
;
1138 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1143 static int kvm_get_fpu(X86CPU
*cpu
)
1145 CPUX86State
*env
= &cpu
->env
;
1149 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1154 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1155 env
->fpus
= fpu
.fsw
;
1156 env
->fpuc
= fpu
.fcw
;
1157 env
->fpop
= fpu
.last_opcode
;
1158 env
->fpip
= fpu
.last_ip
;
1159 env
->fpdp
= fpu
.last_dp
;
1160 for (i
= 0; i
< 8; ++i
) {
1161 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1163 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1164 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1165 env
->mxcsr
= fpu
.mxcsr
;
1170 static int kvm_get_xsave(X86CPU
*cpu
)
1172 CPUX86State
*env
= &cpu
->env
;
1173 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1175 uint16_t cwd
, swd
, twd
;
1177 if (!kvm_has_xsave()) {
1178 return kvm_get_fpu(cpu
);
1181 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1186 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1187 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1188 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1189 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1190 env
->fpstt
= (swd
>> 11) & 7;
1193 for (i
= 0; i
< 8; ++i
) {
1194 env
->fptags
[i
] = !((twd
>> i
) & 1);
1196 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1197 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1198 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1199 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1200 sizeof env
->fpregs
);
1201 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1202 sizeof env
->xmm_regs
);
1203 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1204 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1205 sizeof env
->ymmh_regs
);
1209 static int kvm_get_xcrs(X86CPU
*cpu
)
1211 CPUX86State
*env
= &cpu
->env
;
1213 struct kvm_xcrs xcrs
;
1215 if (!kvm_has_xcrs()) {
1219 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1224 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1225 /* Only support xcr0 now */
1226 if (xcrs
.xcrs
[0].xcr
== 0) {
1227 env
->xcr0
= xcrs
.xcrs
[0].value
;
1234 static int kvm_get_sregs(X86CPU
*cpu
)
1236 CPUX86State
*env
= &cpu
->env
;
1237 struct kvm_sregs sregs
;
1241 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1246 /* There can only be one pending IRQ set in the bitmap at a time, so try
1247 to find it and save its number instead (-1 for none). */
1248 env
->interrupt_injected
= -1;
1249 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1250 if (sregs
.interrupt_bitmap
[i
]) {
1251 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1252 env
->interrupt_injected
= i
* 64 + bit
;
1257 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1258 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1259 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1260 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1261 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1262 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1264 get_seg(&env
->tr
, &sregs
.tr
);
1265 get_seg(&env
->ldt
, &sregs
.ldt
);
1267 env
->idt
.limit
= sregs
.idt
.limit
;
1268 env
->idt
.base
= sregs
.idt
.base
;
1269 env
->gdt
.limit
= sregs
.gdt
.limit
;
1270 env
->gdt
.base
= sregs
.gdt
.base
;
1272 env
->cr
[0] = sregs
.cr0
;
1273 env
->cr
[2] = sregs
.cr2
;
1274 env
->cr
[3] = sregs
.cr3
;
1275 env
->cr
[4] = sregs
.cr4
;
1277 env
->efer
= sregs
.efer
;
1279 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1281 #define HFLAG_COPY_MASK \
1282 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1283 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1284 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1285 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1287 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1288 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1289 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1290 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1291 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1292 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1293 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1295 if (env
->efer
& MSR_EFER_LMA
) {
1296 hflags
|= HF_LMA_MASK
;
1299 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1300 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1302 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1303 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1304 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1305 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1306 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1307 !(hflags
& HF_CS32_MASK
)) {
1308 hflags
|= HF_ADDSEG_MASK
;
1310 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1311 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1314 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1319 static int kvm_get_msrs(X86CPU
*cpu
)
1321 CPUX86State
*env
= &cpu
->env
;
1323 struct kvm_msrs info
;
1324 struct kvm_msr_entry entries
[100];
1326 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1330 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1331 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1332 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1333 msrs
[n
++].index
= MSR_PAT
;
1335 msrs
[n
++].index
= MSR_STAR
;
1337 if (has_msr_hsave_pa
) {
1338 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1340 if (has_msr_tsc_adjust
) {
1341 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1343 if (has_msr_tsc_deadline
) {
1344 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1346 if (has_msr_misc_enable
) {
1347 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1350 if (!env
->tsc_valid
) {
1351 msrs
[n
++].index
= MSR_IA32_TSC
;
1352 env
->tsc_valid
= !runstate_is_running();
1355 #ifdef TARGET_X86_64
1356 if (lm_capable_kernel
) {
1357 msrs
[n
++].index
= MSR_CSTAR
;
1358 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1359 msrs
[n
++].index
= MSR_FMASK
;
1360 msrs
[n
++].index
= MSR_LSTAR
;
1363 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1364 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1365 if (has_msr_async_pf_en
) {
1366 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1368 if (has_msr_pv_eoi_en
) {
1369 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1371 if (has_msr_kvm_steal_time
) {
1372 msrs
[n
++].index
= MSR_KVM_STEAL_TIME
;
1376 msrs
[n
++].index
= MSR_MCG_STATUS
;
1377 msrs
[n
++].index
= MSR_MCG_CTL
;
1378 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1379 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1383 msr_data
.info
.nmsrs
= n
;
1384 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1389 for (i
= 0; i
< ret
; i
++) {
1390 switch (msrs
[i
].index
) {
1391 case MSR_IA32_SYSENTER_CS
:
1392 env
->sysenter_cs
= msrs
[i
].data
;
1394 case MSR_IA32_SYSENTER_ESP
:
1395 env
->sysenter_esp
= msrs
[i
].data
;
1397 case MSR_IA32_SYSENTER_EIP
:
1398 env
->sysenter_eip
= msrs
[i
].data
;
1401 env
->pat
= msrs
[i
].data
;
1404 env
->star
= msrs
[i
].data
;
1406 #ifdef TARGET_X86_64
1408 env
->cstar
= msrs
[i
].data
;
1410 case MSR_KERNELGSBASE
:
1411 env
->kernelgsbase
= msrs
[i
].data
;
1414 env
->fmask
= msrs
[i
].data
;
1417 env
->lstar
= msrs
[i
].data
;
1421 env
->tsc
= msrs
[i
].data
;
1423 case MSR_TSC_ADJUST
:
1424 env
->tsc_adjust
= msrs
[i
].data
;
1426 case MSR_IA32_TSCDEADLINE
:
1427 env
->tsc_deadline
= msrs
[i
].data
;
1429 case MSR_VM_HSAVE_PA
:
1430 env
->vm_hsave
= msrs
[i
].data
;
1432 case MSR_KVM_SYSTEM_TIME
:
1433 env
->system_time_msr
= msrs
[i
].data
;
1435 case MSR_KVM_WALL_CLOCK
:
1436 env
->wall_clock_msr
= msrs
[i
].data
;
1438 case MSR_MCG_STATUS
:
1439 env
->mcg_status
= msrs
[i
].data
;
1442 env
->mcg_ctl
= msrs
[i
].data
;
1444 case MSR_IA32_MISC_ENABLE
:
1445 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1448 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1449 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1450 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1453 case MSR_KVM_ASYNC_PF_EN
:
1454 env
->async_pf_en_msr
= msrs
[i
].data
;
1456 case MSR_KVM_PV_EOI_EN
:
1457 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1459 case MSR_KVM_STEAL_TIME
:
1460 env
->steal_time_msr
= msrs
[i
].data
;
1468 static int kvm_put_mp_state(X86CPU
*cpu
)
1470 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1472 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1475 static int kvm_get_mp_state(X86CPU
*cpu
)
1477 CPUState
*cs
= CPU(cpu
);
1478 CPUX86State
*env
= &cpu
->env
;
1479 struct kvm_mp_state mp_state
;
1482 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
1486 env
->mp_state
= mp_state
.mp_state
;
1487 if (kvm_irqchip_in_kernel()) {
1488 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1493 static int kvm_get_apic(X86CPU
*cpu
)
1495 CPUX86State
*env
= &cpu
->env
;
1496 DeviceState
*apic
= env
->apic_state
;
1497 struct kvm_lapic_state kapic
;
1500 if (apic
&& kvm_irqchip_in_kernel()) {
1501 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1506 kvm_get_apic_state(apic
, &kapic
);
1511 static int kvm_put_apic(X86CPU
*cpu
)
1513 CPUX86State
*env
= &cpu
->env
;
1514 DeviceState
*apic
= env
->apic_state
;
1515 struct kvm_lapic_state kapic
;
1517 if (apic
&& kvm_irqchip_in_kernel()) {
1518 kvm_put_apic_state(apic
, &kapic
);
1520 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1525 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1527 CPUX86State
*env
= &cpu
->env
;
1528 struct kvm_vcpu_events events
;
1530 if (!kvm_has_vcpu_events()) {
1534 events
.exception
.injected
= (env
->exception_injected
>= 0);
1535 events
.exception
.nr
= env
->exception_injected
;
1536 events
.exception
.has_error_code
= env
->has_error_code
;
1537 events
.exception
.error_code
= env
->error_code
;
1538 events
.exception
.pad
= 0;
1540 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1541 events
.interrupt
.nr
= env
->interrupt_injected
;
1542 events
.interrupt
.soft
= env
->soft_interrupt
;
1544 events
.nmi
.injected
= env
->nmi_injected
;
1545 events
.nmi
.pending
= env
->nmi_pending
;
1546 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1549 events
.sipi_vector
= env
->sipi_vector
;
1552 if (level
>= KVM_PUT_RESET_STATE
) {
1554 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1557 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1560 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1562 CPUX86State
*env
= &cpu
->env
;
1563 struct kvm_vcpu_events events
;
1566 if (!kvm_has_vcpu_events()) {
1570 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1574 env
->exception_injected
=
1575 events
.exception
.injected
? events
.exception
.nr
: -1;
1576 env
->has_error_code
= events
.exception
.has_error_code
;
1577 env
->error_code
= events
.exception
.error_code
;
1579 env
->interrupt_injected
=
1580 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1581 env
->soft_interrupt
= events
.interrupt
.soft
;
1583 env
->nmi_injected
= events
.nmi
.injected
;
1584 env
->nmi_pending
= events
.nmi
.pending
;
1585 if (events
.nmi
.masked
) {
1586 env
->hflags2
|= HF2_NMI_MASK
;
1588 env
->hflags2
&= ~HF2_NMI_MASK
;
1591 env
->sipi_vector
= events
.sipi_vector
;
1596 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1598 CPUX86State
*env
= &cpu
->env
;
1600 unsigned long reinject_trap
= 0;
1602 if (!kvm_has_vcpu_events()) {
1603 if (env
->exception_injected
== 1) {
1604 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1605 } else if (env
->exception_injected
== 3) {
1606 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1608 env
->exception_injected
= -1;
1612 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1613 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1614 * by updating the debug state once again if single-stepping is on.
1615 * Another reason to call kvm_update_guest_debug here is a pending debug
1616 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1617 * reinject them via SET_GUEST_DEBUG.
1619 if (reinject_trap
||
1620 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1621 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1626 static int kvm_put_debugregs(X86CPU
*cpu
)
1628 CPUX86State
*env
= &cpu
->env
;
1629 struct kvm_debugregs dbgregs
;
1632 if (!kvm_has_debugregs()) {
1636 for (i
= 0; i
< 4; i
++) {
1637 dbgregs
.db
[i
] = env
->dr
[i
];
1639 dbgregs
.dr6
= env
->dr
[6];
1640 dbgregs
.dr7
= env
->dr
[7];
1643 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1646 static int kvm_get_debugregs(X86CPU
*cpu
)
1648 CPUX86State
*env
= &cpu
->env
;
1649 struct kvm_debugregs dbgregs
;
1652 if (!kvm_has_debugregs()) {
1656 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1660 for (i
= 0; i
< 4; i
++) {
1661 env
->dr
[i
] = dbgregs
.db
[i
];
1663 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1664 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1669 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1671 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1674 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1676 ret
= kvm_getput_regs(x86_cpu
, 1);
1680 ret
= kvm_put_xsave(x86_cpu
);
1684 ret
= kvm_put_xcrs(x86_cpu
);
1688 ret
= kvm_put_sregs(x86_cpu
);
1692 /* must be before kvm_put_msrs */
1693 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1697 ret
= kvm_put_msrs(x86_cpu
, level
);
1701 if (level
>= KVM_PUT_RESET_STATE
) {
1702 ret
= kvm_put_mp_state(x86_cpu
);
1706 ret
= kvm_put_apic(x86_cpu
);
1711 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1715 ret
= kvm_put_debugregs(x86_cpu
);
1720 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1727 int kvm_arch_get_registers(CPUState
*cs
)
1729 X86CPU
*cpu
= X86_CPU(cs
);
1732 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1734 ret
= kvm_getput_regs(cpu
, 0);
1738 ret
= kvm_get_xsave(cpu
);
1742 ret
= kvm_get_xcrs(cpu
);
1746 ret
= kvm_get_sregs(cpu
);
1750 ret
= kvm_get_msrs(cpu
);
1754 ret
= kvm_get_mp_state(cpu
);
1758 ret
= kvm_get_apic(cpu
);
1762 ret
= kvm_get_vcpu_events(cpu
);
1766 ret
= kvm_get_debugregs(cpu
);
1773 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1775 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1776 CPUX86State
*env
= &x86_cpu
->env
;
1780 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1781 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1782 DPRINTF("injected NMI\n");
1783 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
1785 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
1790 if (!kvm_irqchip_in_kernel()) {
1791 /* Force the VCPU out of its inner loop to process any INIT requests
1792 * or pending TPR access reports. */
1793 if (cpu
->interrupt_request
&
1794 (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
1795 cpu
->exit_request
= 1;
1798 /* Try to inject an interrupt if the guest can accept it */
1799 if (run
->ready_for_interrupt_injection
&&
1800 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1801 (env
->eflags
& IF_MASK
)) {
1804 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1805 irq
= cpu_get_pic_interrupt(env
);
1807 struct kvm_interrupt intr
;
1810 DPRINTF("injected interrupt %d\n", irq
);
1811 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
1814 "KVM: injection failed, interrupt lost (%s)\n",
1820 /* If we have an interrupt but the guest is not ready to receive an
1821 * interrupt, request an interrupt window exit. This will
1822 * cause a return to userspace as soon as the guest is ready to
1823 * receive interrupts. */
1824 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
1825 run
->request_interrupt_window
= 1;
1827 run
->request_interrupt_window
= 0;
1830 DPRINTF("setting tpr\n");
1831 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1835 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
1837 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1838 CPUX86State
*env
= &x86_cpu
->env
;
1841 env
->eflags
|= IF_MASK
;
1843 env
->eflags
&= ~IF_MASK
;
1845 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1846 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1849 int kvm_arch_process_async_events(CPUState
*cs
)
1851 X86CPU
*cpu
= X86_CPU(cs
);
1852 CPUX86State
*env
= &cpu
->env
;
1854 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
1855 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
1856 assert(env
->mcg_cap
);
1858 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
1860 kvm_cpu_synchronize_state(env
);
1862 if (env
->exception_injected
== EXCP08_DBLE
) {
1863 /* this means triple fault */
1864 qemu_system_reset_request();
1865 cs
->exit_request
= 1;
1868 env
->exception_injected
= EXCP12_MCHK
;
1869 env
->has_error_code
= 0;
1872 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
1873 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1877 if (kvm_irqchip_in_kernel()) {
1881 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
1882 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
1883 apic_poll_irq(env
->apic_state
);
1885 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1886 (env
->eflags
& IF_MASK
)) ||
1887 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1890 if (cs
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1891 kvm_cpu_synchronize_state(env
);
1894 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1895 kvm_cpu_synchronize_state(env
);
1898 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
1899 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
1900 kvm_cpu_synchronize_state(env
);
1901 apic_handle_tpr_access_report(env
->apic_state
, env
->eip
,
1902 env
->tpr_access_type
);
1908 static int kvm_handle_halt(X86CPU
*cpu
)
1910 CPUState
*cs
= CPU(cpu
);
1911 CPUX86State
*env
= &cpu
->env
;
1913 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1914 (env
->eflags
& IF_MASK
)) &&
1915 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1923 static int kvm_handle_tpr_access(X86CPU
*cpu
)
1925 CPUX86State
*env
= &cpu
->env
;
1926 CPUState
*cs
= CPU(cpu
);
1927 struct kvm_run
*run
= cs
->kvm_run
;
1929 apic_handle_tpr_access_report(env
->apic_state
, run
->tpr_access
.rip
,
1930 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
1935 int kvm_arch_insert_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1937 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1938 static const uint8_t int3
= 0xcc;
1940 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1941 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
1947 int kvm_arch_remove_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1949 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1952 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1953 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
1965 static int nb_hw_breakpoint
;
1967 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1971 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1972 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1973 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
1980 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1981 target_ulong len
, int type
)
1984 case GDB_BREAKPOINT_HW
:
1987 case GDB_WATCHPOINT_WRITE
:
1988 case GDB_WATCHPOINT_ACCESS
:
1995 if (addr
& (len
- 1)) {
2007 if (nb_hw_breakpoint
== 4) {
2010 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2013 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2014 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2015 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2021 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2022 target_ulong len
, int type
)
2026 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2031 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2036 void kvm_arch_remove_all_hw_breakpoints(void)
2038 nb_hw_breakpoint
= 0;
2041 static CPUWatchpoint hw_watchpoint
;
2043 static int kvm_handle_debug(X86CPU
*cpu
,
2044 struct kvm_debug_exit_arch
*arch_info
)
2046 CPUX86State
*env
= &cpu
->env
;
2050 if (arch_info
->exception
== 1) {
2051 if (arch_info
->dr6
& (1 << 14)) {
2052 if (env
->singlestep_enabled
) {
2056 for (n
= 0; n
< 4; n
++) {
2057 if (arch_info
->dr6
& (1 << n
)) {
2058 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2064 env
->watchpoint_hit
= &hw_watchpoint
;
2065 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2066 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2070 env
->watchpoint_hit
= &hw_watchpoint
;
2071 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2072 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2078 } else if (kvm_find_sw_breakpoint(CPU(cpu
), arch_info
->pc
)) {
2082 cpu_synchronize_state(env
);
2083 assert(env
->exception_injected
== -1);
2086 env
->exception_injected
= arch_info
->exception
;
2087 env
->has_error_code
= 0;
2093 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2095 const uint8_t type_code
[] = {
2096 [GDB_BREAKPOINT_HW
] = 0x0,
2097 [GDB_WATCHPOINT_WRITE
] = 0x1,
2098 [GDB_WATCHPOINT_ACCESS
] = 0x3
2100 const uint8_t len_code
[] = {
2101 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2105 if (kvm_sw_breakpoints_active(cpu
)) {
2106 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2108 if (nb_hw_breakpoint
> 0) {
2109 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2110 dbg
->arch
.debugreg
[7] = 0x0600;
2111 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2112 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2113 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2114 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2115 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2120 static bool host_supports_vmx(void)
2122 uint32_t ecx
, unused
;
2124 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2125 return ecx
& CPUID_EXT_VMX
;
2128 #define VMX_INVALID_GUEST_STATE 0x80000021
2130 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2132 X86CPU
*cpu
= X86_CPU(cs
);
2136 switch (run
->exit_reason
) {
2138 DPRINTF("handle_hlt\n");
2139 ret
= kvm_handle_halt(cpu
);
2141 case KVM_EXIT_SET_TPR
:
2144 case KVM_EXIT_TPR_ACCESS
:
2145 ret
= kvm_handle_tpr_access(cpu
);
2147 case KVM_EXIT_FAIL_ENTRY
:
2148 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2149 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2151 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2153 "\nIf you're running a guest on an Intel machine without "
2154 "unrestricted mode\n"
2155 "support, the failure can be most likely due to the guest "
2156 "entering an invalid\n"
2157 "state for Intel VT. For example, the guest maybe running "
2158 "in big real mode\n"
2159 "which is not supported on less recent Intel processors."
2164 case KVM_EXIT_EXCEPTION
:
2165 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2166 run
->ex
.exception
, run
->ex
.error_code
);
2169 case KVM_EXIT_DEBUG
:
2170 DPRINTF("kvm_exit_debug\n");
2171 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2174 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2182 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2184 X86CPU
*cpu
= X86_CPU(cs
);
2185 CPUX86State
*env
= &cpu
->env
;
2187 kvm_cpu_synchronize_state(env
);
2188 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2189 ((env
->segs
[R_CS
].selector
& 3) != 3);
2192 void kvm_arch_init_irq_routing(KVMState
*s
)
2194 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2195 /* If kernel can't do irq routing, interrupt source
2196 * override 0->2 cannot be set up as required by HPET.
2197 * So we have to disable it.
2201 /* We know at this point that we're using the in-kernel
2202 * irqchip, so we can use irqfds, and on x86 we know
2203 * we can use msi via irqfd and GSI routing.
2205 kvm_irqfds_allowed
= true;
2206 kvm_msi_via_irqfd_allowed
= true;
2207 kvm_gsi_routing_allowed
= true;
2210 /* Classic KVM device assignment interface. Will remain x86 only. */
2211 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2212 uint32_t flags
, uint32_t *dev_id
)
2214 struct kvm_assigned_pci_dev dev_data
= {
2215 .segnr
= dev_addr
->domain
,
2216 .busnr
= dev_addr
->bus
,
2217 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2222 dev_data
.assigned_dev_id
=
2223 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2225 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2230 *dev_id
= dev_data
.assigned_dev_id
;
2235 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2237 struct kvm_assigned_pci_dev dev_data
= {
2238 .assigned_dev_id
= dev_id
,
2241 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2244 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2245 uint32_t irq_type
, uint32_t guest_irq
)
2247 struct kvm_assigned_irq assigned_irq
= {
2248 .assigned_dev_id
= dev_id
,
2249 .guest_irq
= guest_irq
,
2253 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2254 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2256 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2260 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2263 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2264 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2266 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2269 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2271 struct kvm_assigned_pci_dev dev_data
= {
2272 .assigned_dev_id
= dev_id
,
2273 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2276 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2279 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2282 struct kvm_assigned_irq assigned_irq
= {
2283 .assigned_dev_id
= dev_id
,
2287 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2290 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2292 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2293 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2296 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2298 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2299 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2302 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2304 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2305 KVM_DEV_IRQ_HOST_MSI
);
2308 bool kvm_device_msix_supported(KVMState
*s
)
2310 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2311 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2312 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2315 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2316 uint32_t nr_vectors
)
2318 struct kvm_assigned_msix_nr msix_nr
= {
2319 .assigned_dev_id
= dev_id
,
2320 .entry_nr
= nr_vectors
,
2323 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2326 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2329 struct kvm_assigned_msix_entry msix_entry
= {
2330 .assigned_dev_id
= dev_id
,
2335 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2338 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2340 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2341 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2344 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2346 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2347 KVM_DEV_IRQ_HOST_MSIX
);