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
->cpuid_kvm_features
;
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
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
617 && kvm_check_extension(cs
->kvm_state
, KVM_CAP_MCE
) > 0) {
622 ret
= kvm_get_mce_cap_supported(cs
->kvm_state
, &mcg_cap
, &banks
);
624 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
628 if (banks
> MCE_BANKS_DEF
) {
629 banks
= MCE_BANKS_DEF
;
631 mcg_cap
&= MCE_CAP_DEF
;
633 ret
= kvm_vcpu_ioctl(cs
, KVM_X86_SETUP_MCE
, &mcg_cap
);
635 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
639 env
->mcg_cap
= mcg_cap
;
642 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
644 cpuid_data
.cpuid
.padding
= 0;
645 r
= kvm_vcpu_ioctl(cs
, KVM_SET_CPUID2
, &cpuid_data
);
650 r
= kvm_check_extension(cs
->kvm_state
, KVM_CAP_TSC_CONTROL
);
651 if (r
&& env
->tsc_khz
) {
652 r
= kvm_vcpu_ioctl(cs
, KVM_SET_TSC_KHZ
, env
->tsc_khz
);
654 fprintf(stderr
, "KVM_SET_TSC_KHZ failed\n");
659 if (kvm_has_xsave()) {
660 env
->kvm_xsave_buf
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
666 void kvm_arch_reset_vcpu(CPUState
*cs
)
668 X86CPU
*cpu
= X86_CPU(cs
);
669 CPUX86State
*env
= &cpu
->env
;
671 env
->exception_injected
= -1;
672 env
->interrupt_injected
= -1;
674 if (kvm_irqchip_in_kernel()) {
675 env
->mp_state
= cpu_is_bsp(cpu
) ? KVM_MP_STATE_RUNNABLE
:
676 KVM_MP_STATE_UNINITIALIZED
;
678 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
682 static int kvm_get_supported_msrs(KVMState
*s
)
684 static int kvm_supported_msrs
;
688 if (kvm_supported_msrs
== 0) {
689 struct kvm_msr_list msr_list
, *kvm_msr_list
;
691 kvm_supported_msrs
= -1;
693 /* Obtain MSR list from KVM. These are the MSRs that we must
696 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
697 if (ret
< 0 && ret
!= -E2BIG
) {
700 /* Old kernel modules had a bug and could write beyond the provided
701 memory. Allocate at least a safe amount of 1K. */
702 kvm_msr_list
= g_malloc0(MAX(1024, sizeof(msr_list
) +
704 sizeof(msr_list
.indices
[0])));
706 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
707 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
711 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
712 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
716 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
717 has_msr_hsave_pa
= true;
720 if (kvm_msr_list
->indices
[i
] == MSR_TSC_ADJUST
) {
721 has_msr_tsc_adjust
= true;
724 if (kvm_msr_list
->indices
[i
] == MSR_IA32_TSCDEADLINE
) {
725 has_msr_tsc_deadline
= true;
728 if (kvm_msr_list
->indices
[i
] == MSR_IA32_MISC_ENABLE
) {
729 has_msr_misc_enable
= true;
735 g_free(kvm_msr_list
);
741 int kvm_arch_init(KVMState
*s
)
743 QemuOptsList
*list
= qemu_find_opts("machine");
744 uint64_t identity_base
= 0xfffbc000;
747 struct utsname utsname
;
749 ret
= kvm_get_supported_msrs(s
);
755 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
758 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
759 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
760 * Since these must be part of guest physical memory, we need to allocate
761 * them, both by setting their start addresses in the kernel and by
762 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
764 * Older KVM versions may not support setting the identity map base. In
765 * that case we need to stick with the default, i.e. a 256K maximum BIOS
768 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
769 /* Allows up to 16M BIOSes. */
770 identity_base
= 0xfeffc000;
772 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
778 /* Set TSS base one page after EPT identity map. */
779 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
784 /* Tell fw_cfg to notify the BIOS to reserve the range. */
785 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
787 fprintf(stderr
, "e820_add_entry() table is full\n");
790 qemu_register_reset(kvm_unpoison_all
, NULL
);
792 if (!QTAILQ_EMPTY(&list
->head
)) {
793 shadow_mem
= qemu_opt_get_size(QTAILQ_FIRST(&list
->head
),
794 "kvm_shadow_mem", -1);
795 if (shadow_mem
!= -1) {
797 ret
= kvm_vm_ioctl(s
, KVM_SET_NR_MMU_PAGES
, shadow_mem
);
806 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
808 lhs
->selector
= rhs
->selector
;
809 lhs
->base
= rhs
->base
;
810 lhs
->limit
= rhs
->limit
;
822 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
824 unsigned flags
= rhs
->flags
;
825 lhs
->selector
= rhs
->selector
;
826 lhs
->base
= rhs
->base
;
827 lhs
->limit
= rhs
->limit
;
828 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
829 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
830 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
831 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
832 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
833 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
834 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
835 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
840 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
842 lhs
->selector
= rhs
->selector
;
843 lhs
->base
= rhs
->base
;
844 lhs
->limit
= rhs
->limit
;
845 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
846 (rhs
->present
* DESC_P_MASK
) |
847 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
848 (rhs
->db
<< DESC_B_SHIFT
) |
849 (rhs
->s
* DESC_S_MASK
) |
850 (rhs
->l
<< DESC_L_SHIFT
) |
851 (rhs
->g
* DESC_G_MASK
) |
852 (rhs
->avl
* DESC_AVL_MASK
);
855 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
858 *kvm_reg
= *qemu_reg
;
860 *qemu_reg
= *kvm_reg
;
864 static int kvm_getput_regs(X86CPU
*cpu
, int set
)
866 CPUX86State
*env
= &cpu
->env
;
867 struct kvm_regs regs
;
871 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_REGS
, ®s
);
877 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
878 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
879 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
880 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
881 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
882 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
883 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
884 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
886 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
887 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
888 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
889 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
890 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
891 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
892 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
893 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
896 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
897 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
900 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_REGS
, ®s
);
906 static int kvm_put_fpu(X86CPU
*cpu
)
908 CPUX86State
*env
= &cpu
->env
;
912 memset(&fpu
, 0, sizeof fpu
);
913 fpu
.fsw
= env
->fpus
& ~(7 << 11);
914 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
916 fpu
.last_opcode
= env
->fpop
;
917 fpu
.last_ip
= env
->fpip
;
918 fpu
.last_dp
= env
->fpdp
;
919 for (i
= 0; i
< 8; ++i
) {
920 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
922 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
923 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
924 fpu
.mxcsr
= env
->mxcsr
;
926 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_FPU
, &fpu
);
929 #define XSAVE_FCW_FSW 0
930 #define XSAVE_FTW_FOP 1
931 #define XSAVE_CWD_RIP 2
932 #define XSAVE_CWD_RDP 4
933 #define XSAVE_MXCSR 6
934 #define XSAVE_ST_SPACE 8
935 #define XSAVE_XMM_SPACE 40
936 #define XSAVE_XSTATE_BV 128
937 #define XSAVE_YMMH_SPACE 144
939 static int kvm_put_xsave(X86CPU
*cpu
)
941 CPUX86State
*env
= &cpu
->env
;
942 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
943 uint16_t cwd
, swd
, twd
;
946 if (!kvm_has_xsave()) {
947 return kvm_put_fpu(cpu
);
950 memset(xsave
, 0, sizeof(struct kvm_xsave
));
952 swd
= env
->fpus
& ~(7 << 11);
953 swd
|= (env
->fpstt
& 7) << 11;
955 for (i
= 0; i
< 8; ++i
) {
956 twd
|= (!env
->fptags
[i
]) << i
;
958 xsave
->region
[XSAVE_FCW_FSW
] = (uint32_t)(swd
<< 16) + cwd
;
959 xsave
->region
[XSAVE_FTW_FOP
] = (uint32_t)(env
->fpop
<< 16) + twd
;
960 memcpy(&xsave
->region
[XSAVE_CWD_RIP
], &env
->fpip
, sizeof(env
->fpip
));
961 memcpy(&xsave
->region
[XSAVE_CWD_RDP
], &env
->fpdp
, sizeof(env
->fpdp
));
962 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
964 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
965 sizeof env
->xmm_regs
);
966 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
967 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
968 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
969 sizeof env
->ymmh_regs
);
970 r
= kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XSAVE
, xsave
);
974 static int kvm_put_xcrs(X86CPU
*cpu
)
976 CPUX86State
*env
= &cpu
->env
;
977 struct kvm_xcrs xcrs
;
979 if (!kvm_has_xcrs()) {
985 xcrs
.xcrs
[0].xcr
= 0;
986 xcrs
.xcrs
[0].value
= env
->xcr0
;
987 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_XCRS
, &xcrs
);
990 static int kvm_put_sregs(X86CPU
*cpu
)
992 CPUX86State
*env
= &cpu
->env
;
993 struct kvm_sregs sregs
;
995 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
996 if (env
->interrupt_injected
>= 0) {
997 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
998 (uint64_t)1 << (env
->interrupt_injected
% 64);
1001 if ((env
->eflags
& VM_MASK
)) {
1002 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1003 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1004 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1005 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1006 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1007 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1009 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
1010 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
1011 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
1012 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
1013 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
1014 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
1017 set_seg(&sregs
.tr
, &env
->tr
);
1018 set_seg(&sregs
.ldt
, &env
->ldt
);
1020 sregs
.idt
.limit
= env
->idt
.limit
;
1021 sregs
.idt
.base
= env
->idt
.base
;
1022 memset(sregs
.idt
.padding
, 0, sizeof sregs
.idt
.padding
);
1023 sregs
.gdt
.limit
= env
->gdt
.limit
;
1024 sregs
.gdt
.base
= env
->gdt
.base
;
1025 memset(sregs
.gdt
.padding
, 0, sizeof sregs
.gdt
.padding
);
1027 sregs
.cr0
= env
->cr
[0];
1028 sregs
.cr2
= env
->cr
[2];
1029 sregs
.cr3
= env
->cr
[3];
1030 sregs
.cr4
= env
->cr
[4];
1032 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
1033 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
1035 sregs
.efer
= env
->efer
;
1037 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
1040 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
1041 uint32_t index
, uint64_t value
)
1043 entry
->index
= index
;
1044 entry
->data
= value
;
1047 static int kvm_put_msrs(X86CPU
*cpu
, int level
)
1049 CPUX86State
*env
= &cpu
->env
;
1051 struct kvm_msrs info
;
1052 struct kvm_msr_entry entries
[100];
1054 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1057 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
1058 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
1059 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
1060 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
1062 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
1064 if (has_msr_hsave_pa
) {
1065 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
1067 if (has_msr_tsc_adjust
) {
1068 kvm_msr_entry_set(&msrs
[n
++], MSR_TSC_ADJUST
, env
->tsc_adjust
);
1070 if (has_msr_tsc_deadline
) {
1071 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSCDEADLINE
, env
->tsc_deadline
);
1073 if (has_msr_misc_enable
) {
1074 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_MISC_ENABLE
,
1075 env
->msr_ia32_misc_enable
);
1077 #ifdef TARGET_X86_64
1078 if (lm_capable_kernel
) {
1079 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
1080 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
1081 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
1082 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
1085 if (level
== KVM_PUT_FULL_STATE
) {
1087 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
1088 * writeback. Until this is fixed, we only write the offset to SMP
1089 * guests after migration, desynchronizing the VCPUs, but avoiding
1090 * huge jump-backs that would occur without any writeback at all.
1092 if (smp_cpus
== 1 || env
->tsc
!= 0) {
1093 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
1097 * The following paravirtual MSRs have side effects on the guest or are
1098 * too heavy for normal writeback. Limit them to reset or full state
1101 if (level
>= KVM_PUT_RESET_STATE
) {
1102 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
1103 env
->system_time_msr
);
1104 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
1105 if (has_msr_async_pf_en
) {
1106 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
1107 env
->async_pf_en_msr
);
1109 if (has_msr_pv_eoi_en
) {
1110 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_PV_EOI_EN
,
1111 env
->pv_eoi_en_msr
);
1113 if (has_msr_kvm_steal_time
) {
1114 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_STEAL_TIME
,
1115 env
->steal_time_msr
);
1117 if (hyperv_hypercall_available()) {
1118 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_GUEST_OS_ID
, 0);
1119 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_HYPERCALL
, 0);
1121 if (hyperv_vapic_recommended()) {
1122 kvm_msr_entry_set(&msrs
[n
++], HV_X64_MSR_APIC_ASSIST_PAGE
, 0);
1128 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
1129 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
1130 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1131 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
1135 msr_data
.info
.nmsrs
= n
;
1137 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MSRS
, &msr_data
);
1142 static int kvm_get_fpu(X86CPU
*cpu
)
1144 CPUX86State
*env
= &cpu
->env
;
1148 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_FPU
, &fpu
);
1153 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
1154 env
->fpus
= fpu
.fsw
;
1155 env
->fpuc
= fpu
.fcw
;
1156 env
->fpop
= fpu
.last_opcode
;
1157 env
->fpip
= fpu
.last_ip
;
1158 env
->fpdp
= fpu
.last_dp
;
1159 for (i
= 0; i
< 8; ++i
) {
1160 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
1162 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
1163 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
1164 env
->mxcsr
= fpu
.mxcsr
;
1169 static int kvm_get_xsave(X86CPU
*cpu
)
1171 CPUX86State
*env
= &cpu
->env
;
1172 struct kvm_xsave
* xsave
= env
->kvm_xsave_buf
;
1174 uint16_t cwd
, swd
, twd
;
1176 if (!kvm_has_xsave()) {
1177 return kvm_get_fpu(cpu
);
1180 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XSAVE
, xsave
);
1185 cwd
= (uint16_t)xsave
->region
[XSAVE_FCW_FSW
];
1186 swd
= (uint16_t)(xsave
->region
[XSAVE_FCW_FSW
] >> 16);
1187 twd
= (uint16_t)xsave
->region
[XSAVE_FTW_FOP
];
1188 env
->fpop
= (uint16_t)(xsave
->region
[XSAVE_FTW_FOP
] >> 16);
1189 env
->fpstt
= (swd
>> 11) & 7;
1192 for (i
= 0; i
< 8; ++i
) {
1193 env
->fptags
[i
] = !((twd
>> i
) & 1);
1195 memcpy(&env
->fpip
, &xsave
->region
[XSAVE_CWD_RIP
], sizeof(env
->fpip
));
1196 memcpy(&env
->fpdp
, &xsave
->region
[XSAVE_CWD_RDP
], sizeof(env
->fpdp
));
1197 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
1198 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
1199 sizeof env
->fpregs
);
1200 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
1201 sizeof env
->xmm_regs
);
1202 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
1203 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
1204 sizeof env
->ymmh_regs
);
1208 static int kvm_get_xcrs(X86CPU
*cpu
)
1210 CPUX86State
*env
= &cpu
->env
;
1212 struct kvm_xcrs xcrs
;
1214 if (!kvm_has_xcrs()) {
1218 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_XCRS
, &xcrs
);
1223 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1224 /* Only support xcr0 now */
1225 if (xcrs
.xcrs
[0].xcr
== 0) {
1226 env
->xcr0
= xcrs
.xcrs
[0].value
;
1233 static int kvm_get_sregs(X86CPU
*cpu
)
1235 CPUX86State
*env
= &cpu
->env
;
1236 struct kvm_sregs sregs
;
1240 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1245 /* There can only be one pending IRQ set in the bitmap at a time, so try
1246 to find it and save its number instead (-1 for none). */
1247 env
->interrupt_injected
= -1;
1248 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1249 if (sregs
.interrupt_bitmap
[i
]) {
1250 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1251 env
->interrupt_injected
= i
* 64 + bit
;
1256 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1257 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1258 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1259 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1260 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1261 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1263 get_seg(&env
->tr
, &sregs
.tr
);
1264 get_seg(&env
->ldt
, &sregs
.ldt
);
1266 env
->idt
.limit
= sregs
.idt
.limit
;
1267 env
->idt
.base
= sregs
.idt
.base
;
1268 env
->gdt
.limit
= sregs
.gdt
.limit
;
1269 env
->gdt
.base
= sregs
.gdt
.base
;
1271 env
->cr
[0] = sregs
.cr0
;
1272 env
->cr
[2] = sregs
.cr2
;
1273 env
->cr
[3] = sregs
.cr3
;
1274 env
->cr
[4] = sregs
.cr4
;
1276 env
->efer
= sregs
.efer
;
1278 /* changes to apic base and cr8/tpr are read back via kvm_arch_post_run */
1280 #define HFLAG_COPY_MASK \
1281 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1282 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1283 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1284 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1286 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1287 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1288 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1289 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1290 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1291 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1292 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1294 if (env
->efer
& MSR_EFER_LMA
) {
1295 hflags
|= HF_LMA_MASK
;
1298 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1299 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1301 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1302 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1303 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1304 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1305 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1306 !(hflags
& HF_CS32_MASK
)) {
1307 hflags
|= HF_ADDSEG_MASK
;
1309 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1310 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1313 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1318 static int kvm_get_msrs(X86CPU
*cpu
)
1320 CPUX86State
*env
= &cpu
->env
;
1322 struct kvm_msrs info
;
1323 struct kvm_msr_entry entries
[100];
1325 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1329 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1330 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1331 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1332 msrs
[n
++].index
= MSR_PAT
;
1334 msrs
[n
++].index
= MSR_STAR
;
1336 if (has_msr_hsave_pa
) {
1337 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1339 if (has_msr_tsc_adjust
) {
1340 msrs
[n
++].index
= MSR_TSC_ADJUST
;
1342 if (has_msr_tsc_deadline
) {
1343 msrs
[n
++].index
= MSR_IA32_TSCDEADLINE
;
1345 if (has_msr_misc_enable
) {
1346 msrs
[n
++].index
= MSR_IA32_MISC_ENABLE
;
1349 if (!env
->tsc_valid
) {
1350 msrs
[n
++].index
= MSR_IA32_TSC
;
1351 env
->tsc_valid
= !runstate_is_running();
1354 #ifdef TARGET_X86_64
1355 if (lm_capable_kernel
) {
1356 msrs
[n
++].index
= MSR_CSTAR
;
1357 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1358 msrs
[n
++].index
= MSR_FMASK
;
1359 msrs
[n
++].index
= MSR_LSTAR
;
1362 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1363 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1364 if (has_msr_async_pf_en
) {
1365 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1367 if (has_msr_pv_eoi_en
) {
1368 msrs
[n
++].index
= MSR_KVM_PV_EOI_EN
;
1370 if (has_msr_kvm_steal_time
) {
1371 msrs
[n
++].index
= MSR_KVM_STEAL_TIME
;
1375 msrs
[n
++].index
= MSR_MCG_STATUS
;
1376 msrs
[n
++].index
= MSR_MCG_CTL
;
1377 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1378 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1382 msr_data
.info
.nmsrs
= n
;
1383 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_MSRS
, &msr_data
);
1388 for (i
= 0; i
< ret
; i
++) {
1389 switch (msrs
[i
].index
) {
1390 case MSR_IA32_SYSENTER_CS
:
1391 env
->sysenter_cs
= msrs
[i
].data
;
1393 case MSR_IA32_SYSENTER_ESP
:
1394 env
->sysenter_esp
= msrs
[i
].data
;
1396 case MSR_IA32_SYSENTER_EIP
:
1397 env
->sysenter_eip
= msrs
[i
].data
;
1400 env
->pat
= msrs
[i
].data
;
1403 env
->star
= msrs
[i
].data
;
1405 #ifdef TARGET_X86_64
1407 env
->cstar
= msrs
[i
].data
;
1409 case MSR_KERNELGSBASE
:
1410 env
->kernelgsbase
= msrs
[i
].data
;
1413 env
->fmask
= msrs
[i
].data
;
1416 env
->lstar
= msrs
[i
].data
;
1420 env
->tsc
= msrs
[i
].data
;
1422 case MSR_TSC_ADJUST
:
1423 env
->tsc_adjust
= msrs
[i
].data
;
1425 case MSR_IA32_TSCDEADLINE
:
1426 env
->tsc_deadline
= msrs
[i
].data
;
1428 case MSR_VM_HSAVE_PA
:
1429 env
->vm_hsave
= msrs
[i
].data
;
1431 case MSR_KVM_SYSTEM_TIME
:
1432 env
->system_time_msr
= msrs
[i
].data
;
1434 case MSR_KVM_WALL_CLOCK
:
1435 env
->wall_clock_msr
= msrs
[i
].data
;
1437 case MSR_MCG_STATUS
:
1438 env
->mcg_status
= msrs
[i
].data
;
1441 env
->mcg_ctl
= msrs
[i
].data
;
1443 case MSR_IA32_MISC_ENABLE
:
1444 env
->msr_ia32_misc_enable
= msrs
[i
].data
;
1447 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1448 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1449 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1452 case MSR_KVM_ASYNC_PF_EN
:
1453 env
->async_pf_en_msr
= msrs
[i
].data
;
1455 case MSR_KVM_PV_EOI_EN
:
1456 env
->pv_eoi_en_msr
= msrs
[i
].data
;
1458 case MSR_KVM_STEAL_TIME
:
1459 env
->steal_time_msr
= msrs
[i
].data
;
1467 static int kvm_put_mp_state(X86CPU
*cpu
)
1469 struct kvm_mp_state mp_state
= { .mp_state
= cpu
->env
.mp_state
};
1471 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_MP_STATE
, &mp_state
);
1474 static int kvm_get_mp_state(X86CPU
*cpu
)
1476 CPUState
*cs
= CPU(cpu
);
1477 CPUX86State
*env
= &cpu
->env
;
1478 struct kvm_mp_state mp_state
;
1481 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_MP_STATE
, &mp_state
);
1485 env
->mp_state
= mp_state
.mp_state
;
1486 if (kvm_irqchip_in_kernel()) {
1487 cs
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1492 static int kvm_get_apic(X86CPU
*cpu
)
1494 CPUX86State
*env
= &cpu
->env
;
1495 DeviceState
*apic
= env
->apic_state
;
1496 struct kvm_lapic_state kapic
;
1499 if (apic
&& kvm_irqchip_in_kernel()) {
1500 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_LAPIC
, &kapic
);
1505 kvm_get_apic_state(apic
, &kapic
);
1510 static int kvm_put_apic(X86CPU
*cpu
)
1512 CPUX86State
*env
= &cpu
->env
;
1513 DeviceState
*apic
= env
->apic_state
;
1514 struct kvm_lapic_state kapic
;
1516 if (apic
&& kvm_irqchip_in_kernel()) {
1517 kvm_put_apic_state(apic
, &kapic
);
1519 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_LAPIC
, &kapic
);
1524 static int kvm_put_vcpu_events(X86CPU
*cpu
, int level
)
1526 CPUX86State
*env
= &cpu
->env
;
1527 struct kvm_vcpu_events events
;
1529 if (!kvm_has_vcpu_events()) {
1533 events
.exception
.injected
= (env
->exception_injected
>= 0);
1534 events
.exception
.nr
= env
->exception_injected
;
1535 events
.exception
.has_error_code
= env
->has_error_code
;
1536 events
.exception
.error_code
= env
->error_code
;
1537 events
.exception
.pad
= 0;
1539 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1540 events
.interrupt
.nr
= env
->interrupt_injected
;
1541 events
.interrupt
.soft
= env
->soft_interrupt
;
1543 events
.nmi
.injected
= env
->nmi_injected
;
1544 events
.nmi
.pending
= env
->nmi_pending
;
1545 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1548 events
.sipi_vector
= env
->sipi_vector
;
1551 if (level
>= KVM_PUT_RESET_STATE
) {
1553 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1556 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_VCPU_EVENTS
, &events
);
1559 static int kvm_get_vcpu_events(X86CPU
*cpu
)
1561 CPUX86State
*env
= &cpu
->env
;
1562 struct kvm_vcpu_events events
;
1565 if (!kvm_has_vcpu_events()) {
1569 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_VCPU_EVENTS
, &events
);
1573 env
->exception_injected
=
1574 events
.exception
.injected
? events
.exception
.nr
: -1;
1575 env
->has_error_code
= events
.exception
.has_error_code
;
1576 env
->error_code
= events
.exception
.error_code
;
1578 env
->interrupt_injected
=
1579 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1580 env
->soft_interrupt
= events
.interrupt
.soft
;
1582 env
->nmi_injected
= events
.nmi
.injected
;
1583 env
->nmi_pending
= events
.nmi
.pending
;
1584 if (events
.nmi
.masked
) {
1585 env
->hflags2
|= HF2_NMI_MASK
;
1587 env
->hflags2
&= ~HF2_NMI_MASK
;
1590 env
->sipi_vector
= events
.sipi_vector
;
1595 static int kvm_guest_debug_workarounds(X86CPU
*cpu
)
1597 CPUX86State
*env
= &cpu
->env
;
1599 unsigned long reinject_trap
= 0;
1601 if (!kvm_has_vcpu_events()) {
1602 if (env
->exception_injected
== 1) {
1603 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1604 } else if (env
->exception_injected
== 3) {
1605 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1607 env
->exception_injected
= -1;
1611 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1612 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1613 * by updating the debug state once again if single-stepping is on.
1614 * Another reason to call kvm_update_guest_debug here is a pending debug
1615 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1616 * reinject them via SET_GUEST_DEBUG.
1618 if (reinject_trap
||
1619 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1620 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1625 static int kvm_put_debugregs(X86CPU
*cpu
)
1627 CPUX86State
*env
= &cpu
->env
;
1628 struct kvm_debugregs dbgregs
;
1631 if (!kvm_has_debugregs()) {
1635 for (i
= 0; i
< 4; i
++) {
1636 dbgregs
.db
[i
] = env
->dr
[i
];
1638 dbgregs
.dr6
= env
->dr
[6];
1639 dbgregs
.dr7
= env
->dr
[7];
1642 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_DEBUGREGS
, &dbgregs
);
1645 static int kvm_get_debugregs(X86CPU
*cpu
)
1647 CPUX86State
*env
= &cpu
->env
;
1648 struct kvm_debugregs dbgregs
;
1651 if (!kvm_has_debugregs()) {
1655 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_DEBUGREGS
, &dbgregs
);
1659 for (i
= 0; i
< 4; i
++) {
1660 env
->dr
[i
] = dbgregs
.db
[i
];
1662 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1663 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1668 int kvm_arch_put_registers(CPUState
*cpu
, int level
)
1670 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1673 assert(cpu_is_stopped(cpu
) || qemu_cpu_is_self(cpu
));
1675 ret
= kvm_getput_regs(x86_cpu
, 1);
1679 ret
= kvm_put_xsave(x86_cpu
);
1683 ret
= kvm_put_xcrs(x86_cpu
);
1687 ret
= kvm_put_sregs(x86_cpu
);
1691 /* must be before kvm_put_msrs */
1692 ret
= kvm_inject_mce_oldstyle(x86_cpu
);
1696 ret
= kvm_put_msrs(x86_cpu
, level
);
1700 if (level
>= KVM_PUT_RESET_STATE
) {
1701 ret
= kvm_put_mp_state(x86_cpu
);
1705 ret
= kvm_put_apic(x86_cpu
);
1710 ret
= kvm_put_vcpu_events(x86_cpu
, level
);
1714 ret
= kvm_put_debugregs(x86_cpu
);
1719 ret
= kvm_guest_debug_workarounds(x86_cpu
);
1726 int kvm_arch_get_registers(CPUState
*cs
)
1728 X86CPU
*cpu
= X86_CPU(cs
);
1731 assert(cpu_is_stopped(cs
) || qemu_cpu_is_self(cs
));
1733 ret
= kvm_getput_regs(cpu
, 0);
1737 ret
= kvm_get_xsave(cpu
);
1741 ret
= kvm_get_xcrs(cpu
);
1745 ret
= kvm_get_sregs(cpu
);
1749 ret
= kvm_get_msrs(cpu
);
1753 ret
= kvm_get_mp_state(cpu
);
1757 ret
= kvm_get_apic(cpu
);
1761 ret
= kvm_get_vcpu_events(cpu
);
1765 ret
= kvm_get_debugregs(cpu
);
1772 void kvm_arch_pre_run(CPUState
*cpu
, struct kvm_run
*run
)
1774 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1775 CPUX86State
*env
= &x86_cpu
->env
;
1779 if (cpu
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1780 cpu
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1781 DPRINTF("injected NMI\n");
1782 ret
= kvm_vcpu_ioctl(cpu
, KVM_NMI
);
1784 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
1789 if (!kvm_irqchip_in_kernel()) {
1790 /* Force the VCPU out of its inner loop to process any INIT requests
1791 * or pending TPR access reports. */
1792 if (cpu
->interrupt_request
&
1793 (CPU_INTERRUPT_INIT
| CPU_INTERRUPT_TPR
)) {
1794 cpu
->exit_request
= 1;
1797 /* Try to inject an interrupt if the guest can accept it */
1798 if (run
->ready_for_interrupt_injection
&&
1799 (cpu
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1800 (env
->eflags
& IF_MASK
)) {
1803 cpu
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1804 irq
= cpu_get_pic_interrupt(env
);
1806 struct kvm_interrupt intr
;
1809 DPRINTF("injected interrupt %d\n", irq
);
1810 ret
= kvm_vcpu_ioctl(cpu
, KVM_INTERRUPT
, &intr
);
1813 "KVM: injection failed, interrupt lost (%s)\n",
1819 /* If we have an interrupt but the guest is not ready to receive an
1820 * interrupt, request an interrupt window exit. This will
1821 * cause a return to userspace as soon as the guest is ready to
1822 * receive interrupts. */
1823 if ((cpu
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
1824 run
->request_interrupt_window
= 1;
1826 run
->request_interrupt_window
= 0;
1829 DPRINTF("setting tpr\n");
1830 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1834 void kvm_arch_post_run(CPUState
*cpu
, struct kvm_run
*run
)
1836 X86CPU
*x86_cpu
= X86_CPU(cpu
);
1837 CPUX86State
*env
= &x86_cpu
->env
;
1840 env
->eflags
|= IF_MASK
;
1842 env
->eflags
&= ~IF_MASK
;
1844 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1845 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1848 int kvm_arch_process_async_events(CPUState
*cs
)
1850 X86CPU
*cpu
= X86_CPU(cs
);
1851 CPUX86State
*env
= &cpu
->env
;
1853 if (cs
->interrupt_request
& CPU_INTERRUPT_MCE
) {
1854 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
1855 assert(env
->mcg_cap
);
1857 cs
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
1859 kvm_cpu_synchronize_state(env
);
1861 if (env
->exception_injected
== EXCP08_DBLE
) {
1862 /* this means triple fault */
1863 qemu_system_reset_request();
1864 cs
->exit_request
= 1;
1867 env
->exception_injected
= EXCP12_MCHK
;
1868 env
->has_error_code
= 0;
1871 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
1872 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1876 if (kvm_irqchip_in_kernel()) {
1880 if (cs
->interrupt_request
& CPU_INTERRUPT_POLL
) {
1881 cs
->interrupt_request
&= ~CPU_INTERRUPT_POLL
;
1882 apic_poll_irq(env
->apic_state
);
1884 if (((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1885 (env
->eflags
& IF_MASK
)) ||
1886 (cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1889 if (cs
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1890 kvm_cpu_synchronize_state(env
);
1893 if (cs
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1894 kvm_cpu_synchronize_state(env
);
1897 if (cs
->interrupt_request
& CPU_INTERRUPT_TPR
) {
1898 cs
->interrupt_request
&= ~CPU_INTERRUPT_TPR
;
1899 kvm_cpu_synchronize_state(env
);
1900 apic_handle_tpr_access_report(env
->apic_state
, env
->eip
,
1901 env
->tpr_access_type
);
1907 static int kvm_handle_halt(X86CPU
*cpu
)
1909 CPUState
*cs
= CPU(cpu
);
1910 CPUX86State
*env
= &cpu
->env
;
1912 if (!((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1913 (env
->eflags
& IF_MASK
)) &&
1914 !(cs
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1922 static int kvm_handle_tpr_access(X86CPU
*cpu
)
1924 CPUX86State
*env
= &cpu
->env
;
1925 CPUState
*cs
= CPU(cpu
);
1926 struct kvm_run
*run
= cs
->kvm_run
;
1928 apic_handle_tpr_access_report(env
->apic_state
, run
->tpr_access
.rip
,
1929 run
->tpr_access
.is_write
? TPR_ACCESS_WRITE
1934 int kvm_arch_insert_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1936 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1937 static const uint8_t int3
= 0xcc;
1939 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1940 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
1946 int kvm_arch_remove_sw_breakpoint(CPUState
*cpu
, struct kvm_sw_breakpoint
*bp
)
1948 CPUX86State
*env
= &X86_CPU(cpu
)->env
;
1951 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1952 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
1964 static int nb_hw_breakpoint
;
1966 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1970 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1971 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1972 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
1979 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1980 target_ulong len
, int type
)
1983 case GDB_BREAKPOINT_HW
:
1986 case GDB_WATCHPOINT_WRITE
:
1987 case GDB_WATCHPOINT_ACCESS
:
1994 if (addr
& (len
- 1)) {
2006 if (nb_hw_breakpoint
== 4) {
2009 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
2012 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
2013 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
2014 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
2020 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
2021 target_ulong len
, int type
)
2025 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
2030 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
2035 void kvm_arch_remove_all_hw_breakpoints(void)
2037 nb_hw_breakpoint
= 0;
2040 static CPUWatchpoint hw_watchpoint
;
2042 static int kvm_handle_debug(X86CPU
*cpu
,
2043 struct kvm_debug_exit_arch
*arch_info
)
2045 CPUX86State
*env
= &cpu
->env
;
2049 if (arch_info
->exception
== 1) {
2050 if (arch_info
->dr6
& (1 << 14)) {
2051 if (env
->singlestep_enabled
) {
2055 for (n
= 0; n
< 4; n
++) {
2056 if (arch_info
->dr6
& (1 << n
)) {
2057 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
2063 env
->watchpoint_hit
= &hw_watchpoint
;
2064 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2065 hw_watchpoint
.flags
= BP_MEM_WRITE
;
2069 env
->watchpoint_hit
= &hw_watchpoint
;
2070 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
2071 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
2077 } else if (kvm_find_sw_breakpoint(CPU(cpu
), arch_info
->pc
)) {
2081 cpu_synchronize_state(env
);
2082 assert(env
->exception_injected
== -1);
2085 env
->exception_injected
= arch_info
->exception
;
2086 env
->has_error_code
= 0;
2092 void kvm_arch_update_guest_debug(CPUState
*cpu
, struct kvm_guest_debug
*dbg
)
2094 const uint8_t type_code
[] = {
2095 [GDB_BREAKPOINT_HW
] = 0x0,
2096 [GDB_WATCHPOINT_WRITE
] = 0x1,
2097 [GDB_WATCHPOINT_ACCESS
] = 0x3
2099 const uint8_t len_code
[] = {
2100 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
2104 if (kvm_sw_breakpoints_active(cpu
)) {
2105 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
2107 if (nb_hw_breakpoint
> 0) {
2108 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
2109 dbg
->arch
.debugreg
[7] = 0x0600;
2110 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
2111 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
2112 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
2113 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
2114 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
2119 static bool host_supports_vmx(void)
2121 uint32_t ecx
, unused
;
2123 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
2124 return ecx
& CPUID_EXT_VMX
;
2127 #define VMX_INVALID_GUEST_STATE 0x80000021
2129 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
2131 X86CPU
*cpu
= X86_CPU(cs
);
2135 switch (run
->exit_reason
) {
2137 DPRINTF("handle_hlt\n");
2138 ret
= kvm_handle_halt(cpu
);
2140 case KVM_EXIT_SET_TPR
:
2143 case KVM_EXIT_TPR_ACCESS
:
2144 ret
= kvm_handle_tpr_access(cpu
);
2146 case KVM_EXIT_FAIL_ENTRY
:
2147 code
= run
->fail_entry
.hardware_entry_failure_reason
;
2148 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
2150 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
2152 "\nIf you're running a guest on an Intel machine without "
2153 "unrestricted mode\n"
2154 "support, the failure can be most likely due to the guest "
2155 "entering an invalid\n"
2156 "state for Intel VT. For example, the guest maybe running "
2157 "in big real mode\n"
2158 "which is not supported on less recent Intel processors."
2163 case KVM_EXIT_EXCEPTION
:
2164 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
2165 run
->ex
.exception
, run
->ex
.error_code
);
2168 case KVM_EXIT_DEBUG
:
2169 DPRINTF("kvm_exit_debug\n");
2170 ret
= kvm_handle_debug(cpu
, &run
->debug
.arch
);
2173 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
2181 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
2183 X86CPU
*cpu
= X86_CPU(cs
);
2184 CPUX86State
*env
= &cpu
->env
;
2186 kvm_cpu_synchronize_state(env
);
2187 return !(env
->cr
[0] & CR0_PE_MASK
) ||
2188 ((env
->segs
[R_CS
].selector
& 3) != 3);
2191 void kvm_arch_init_irq_routing(KVMState
*s
)
2193 if (!kvm_check_extension(s
, KVM_CAP_IRQ_ROUTING
)) {
2194 /* If kernel can't do irq routing, interrupt source
2195 * override 0->2 cannot be set up as required by HPET.
2196 * So we have to disable it.
2200 /* We know at this point that we're using the in-kernel
2201 * irqchip, so we can use irqfds, and on x86 we know
2202 * we can use msi via irqfd and GSI routing.
2204 kvm_irqfds_allowed
= true;
2205 kvm_msi_via_irqfd_allowed
= true;
2206 kvm_gsi_routing_allowed
= true;
2209 /* Classic KVM device assignment interface. Will remain x86 only. */
2210 int kvm_device_pci_assign(KVMState
*s
, PCIHostDeviceAddress
*dev_addr
,
2211 uint32_t flags
, uint32_t *dev_id
)
2213 struct kvm_assigned_pci_dev dev_data
= {
2214 .segnr
= dev_addr
->domain
,
2215 .busnr
= dev_addr
->bus
,
2216 .devfn
= PCI_DEVFN(dev_addr
->slot
, dev_addr
->function
),
2221 dev_data
.assigned_dev_id
=
2222 (dev_addr
->domain
<< 16) | (dev_addr
->bus
<< 8) | dev_data
.devfn
;
2224 ret
= kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, &dev_data
);
2229 *dev_id
= dev_data
.assigned_dev_id
;
2234 int kvm_device_pci_deassign(KVMState
*s
, uint32_t dev_id
)
2236 struct kvm_assigned_pci_dev dev_data
= {
2237 .assigned_dev_id
= dev_id
,
2240 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, &dev_data
);
2243 static int kvm_assign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2244 uint32_t irq_type
, uint32_t guest_irq
)
2246 struct kvm_assigned_irq assigned_irq
= {
2247 .assigned_dev_id
= dev_id
,
2248 .guest_irq
= guest_irq
,
2252 if (kvm_check_extension(s
, KVM_CAP_ASSIGN_DEV_IRQ
)) {
2253 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, &assigned_irq
);
2255 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, &assigned_irq
);
2259 int kvm_device_intx_assign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
,
2262 uint32_t irq_type
= KVM_DEV_IRQ_GUEST_INTX
|
2263 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
);
2265 return kvm_assign_irq_internal(s
, dev_id
, irq_type
, guest_irq
);
2268 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
2270 struct kvm_assigned_pci_dev dev_data
= {
2271 .assigned_dev_id
= dev_id
,
2272 .flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0,
2275 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &dev_data
);
2278 static int kvm_deassign_irq_internal(KVMState
*s
, uint32_t dev_id
,
2281 struct kvm_assigned_irq assigned_irq
= {
2282 .assigned_dev_id
= dev_id
,
2286 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, &assigned_irq
);
2289 int kvm_device_intx_deassign(KVMState
*s
, uint32_t dev_id
, bool use_host_msi
)
2291 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_INTX
|
2292 (use_host_msi
? KVM_DEV_IRQ_HOST_MSI
: KVM_DEV_IRQ_HOST_INTX
));
2295 int kvm_device_msi_assign(KVMState
*s
, uint32_t dev_id
, int virq
)
2297 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSI
|
2298 KVM_DEV_IRQ_GUEST_MSI
, virq
);
2301 int kvm_device_msi_deassign(KVMState
*s
, uint32_t dev_id
)
2303 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSI
|
2304 KVM_DEV_IRQ_HOST_MSI
);
2307 bool kvm_device_msix_supported(KVMState
*s
)
2309 /* The kernel lacks a corresponding KVM_CAP, so we probe by calling
2310 * KVM_ASSIGN_SET_MSIX_NR with an invalid parameter. */
2311 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, NULL
) == -EFAULT
;
2314 int kvm_device_msix_init_vectors(KVMState
*s
, uint32_t dev_id
,
2315 uint32_t nr_vectors
)
2317 struct kvm_assigned_msix_nr msix_nr
= {
2318 .assigned_dev_id
= dev_id
,
2319 .entry_nr
= nr_vectors
,
2322 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, &msix_nr
);
2325 int kvm_device_msix_set_vector(KVMState
*s
, uint32_t dev_id
, uint32_t vector
,
2328 struct kvm_assigned_msix_entry msix_entry
= {
2329 .assigned_dev_id
= dev_id
,
2334 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, &msix_entry
);
2337 int kvm_device_msix_assign(KVMState
*s
, uint32_t dev_id
)
2339 return kvm_assign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_HOST_MSIX
|
2340 KVM_DEV_IRQ_GUEST_MSIX
, 0);
2343 int kvm_device_msix_deassign(KVMState
*s
, uint32_t dev_id
)
2345 return kvm_deassign_irq_internal(s
, dev_id
, KVM_DEV_IRQ_GUEST_MSIX
|
2346 KVM_DEV_IRQ_HOST_MSIX
);