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>
22 #include "qemu-common.h"
27 #include "host-utils.h"
32 #ifdef CONFIG_KVM_PARA
33 #include <linux/kvm_para.h>
39 #define DPRINTF(fmt, ...) \
40 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
42 #define DPRINTF(fmt, ...) \
46 #define MSR_KVM_WALL_CLOCK 0x11
47 #define MSR_KVM_SYSTEM_TIME 0x12
50 #define BUS_MCEERR_AR 4
53 #define BUS_MCEERR_AO 5
56 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
57 KVM_CAP_INFO(SET_TSS_ADDR
),
58 KVM_CAP_INFO(EXT_CPUID
),
59 KVM_CAP_INFO(MP_STATE
),
63 static bool has_msr_star
;
64 static bool has_msr_hsave_pa
;
65 #if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ASYNC_PF)
66 static bool has_msr_async_pf_en
;
68 static int lm_capable_kernel
;
70 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
72 struct kvm_cpuid2
*cpuid
;
75 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
76 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
78 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
79 if (r
== 0 && cpuid
->nent
>= max
) {
87 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
95 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
96 uint32_t index
, int reg
)
98 struct kvm_cpuid2
*cpuid
;
101 uint32_t cpuid_1_edx
;
104 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
108 for (i
= 0; i
< cpuid
->nent
; ++i
) {
109 if (cpuid
->entries
[i
].function
== function
&&
110 cpuid
->entries
[i
].index
== index
) {
113 ret
= cpuid
->entries
[i
].eax
;
116 ret
= cpuid
->entries
[i
].ebx
;
119 ret
= cpuid
->entries
[i
].ecx
;
122 ret
= cpuid
->entries
[i
].edx
;
125 /* KVM before 2.6.30 misreports the following features */
126 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
129 /* On Intel, kvm returns cpuid according to the Intel spec,
130 * so add missing bits according to the AMD spec:
132 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
133 ret
|= cpuid_1_edx
& 0x183f7ff;
146 #ifdef CONFIG_KVM_PARA
147 struct kvm_para_features
{
150 } para_features
[] = {
151 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
152 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
153 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
154 #ifdef KVM_CAP_ASYNC_PF
155 { KVM_CAP_ASYNC_PF
, KVM_FEATURE_ASYNC_PF
},
160 static int get_para_features(CPUState
*env
)
164 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
165 if (kvm_check_extension(env
->kvm_state
, para_features
[i
].cap
)) {
166 features
|= (1 << para_features
[i
].feature
);
169 #ifdef KVM_CAP_ASYNC_PF
170 has_msr_async_pf_en
= features
& (1 << KVM_FEATURE_ASYNC_PF
);
174 #endif /* CONFIG_KVM_PARA */
176 typedef struct HWPoisonPage
{
178 QLIST_ENTRY(HWPoisonPage
) list
;
181 static QLIST_HEAD(, HWPoisonPage
) hwpoison_page_list
=
182 QLIST_HEAD_INITIALIZER(hwpoison_page_list
);
184 static void kvm_unpoison_all(void *param
)
186 HWPoisonPage
*page
, *next_page
;
188 QLIST_FOREACH_SAFE(page
, &hwpoison_page_list
, list
, next_page
) {
189 QLIST_REMOVE(page
, list
);
190 qemu_ram_remap(page
->ram_addr
, TARGET_PAGE_SIZE
);
196 static void kvm_hwpoison_page_add(ram_addr_t ram_addr
)
200 QLIST_FOREACH(page
, &hwpoison_page_list
, list
) {
201 if (page
->ram_addr
== ram_addr
) {
205 page
= qemu_malloc(sizeof(HWPoisonPage
));
206 page
->ram_addr
= ram_addr
;
207 QLIST_INSERT_HEAD(&hwpoison_page_list
, page
, list
);
210 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
215 r
= kvm_check_extension(s
, KVM_CAP_MCE
);
218 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
223 static void kvm_mce_inject(CPUState
*env
, target_phys_addr_t paddr
, int code
)
225 uint64_t status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
|
226 MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
;
227 uint64_t mcg_status
= MCG_STATUS_MCIP
;
229 if (code
== BUS_MCEERR_AR
) {
230 status
|= MCI_STATUS_AR
| 0x134;
231 mcg_status
|= MCG_STATUS_EIPV
;
234 mcg_status
|= MCG_STATUS_RIPV
;
236 cpu_x86_inject_mce(NULL
, env
, 9, status
, mcg_status
, paddr
,
237 (MCM_ADDR_PHYS
<< 6) | 0xc,
238 cpu_x86_support_mca_broadcast(env
) ?
239 MCE_INJECT_BROADCAST
: 0);
241 #endif /* KVM_CAP_MCE */
243 static void hardware_memory_error(void)
245 fprintf(stderr
, "Hardware memory error!\n");
249 int kvm_arch_on_sigbus_vcpu(CPUState
*env
, int code
, void *addr
)
253 target_phys_addr_t paddr
;
255 if ((env
->mcg_cap
& MCG_SER_P
) && addr
256 && (code
== BUS_MCEERR_AR
|| code
== BUS_MCEERR_AO
)) {
257 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
258 !kvm_physical_memory_addr_from_ram(env
->kvm_state
, ram_addr
,
260 fprintf(stderr
, "Hardware memory error for memory used by "
261 "QEMU itself instead of guest system!\n");
262 /* Hope we are lucky for AO MCE */
263 if (code
== BUS_MCEERR_AO
) {
266 hardware_memory_error();
269 kvm_hwpoison_page_add(ram_addr
);
270 kvm_mce_inject(env
, paddr
, code
);
272 #endif /* KVM_CAP_MCE */
274 if (code
== BUS_MCEERR_AO
) {
276 } else if (code
== BUS_MCEERR_AR
) {
277 hardware_memory_error();
285 int kvm_arch_on_sigbus(int code
, void *addr
)
288 if ((first_cpu
->mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
290 target_phys_addr_t paddr
;
292 /* Hope we are lucky for AO MCE */
293 if (qemu_ram_addr_from_host(addr
, &ram_addr
) ||
294 !kvm_physical_memory_addr_from_ram(first_cpu
->kvm_state
, ram_addr
,
296 fprintf(stderr
, "Hardware memory error for memory used by "
297 "QEMU itself instead of guest system!: %p\n", addr
);
300 kvm_hwpoison_page_add(ram_addr
);
301 kvm_mce_inject(first_cpu
, paddr
, code
);
303 #endif /* KVM_CAP_MCE */
305 if (code
== BUS_MCEERR_AO
) {
307 } else if (code
== BUS_MCEERR_AR
) {
308 hardware_memory_error();
316 static int kvm_inject_mce_oldstyle(CPUState
*env
)
319 if (!kvm_has_vcpu_events() && env
->exception_injected
== EXCP12_MCHK
) {
320 unsigned int bank
, bank_num
= env
->mcg_cap
& 0xff;
321 struct kvm_x86_mce mce
;
323 env
->exception_injected
= -1;
326 * There must be at least one bank in use if an MCE is pending.
327 * Find it and use its values for the event injection.
329 for (bank
= 0; bank
< bank_num
; bank
++) {
330 if (env
->mce_banks
[bank
* 4 + 1] & MCI_STATUS_VAL
) {
334 assert(bank
< bank_num
);
337 mce
.status
= env
->mce_banks
[bank
* 4 + 1];
338 mce
.mcg_status
= env
->mcg_status
;
339 mce
.addr
= env
->mce_banks
[bank
* 4 + 2];
340 mce
.misc
= env
->mce_banks
[bank
* 4 + 3];
342 return kvm_vcpu_ioctl(env
, KVM_X86_SET_MCE
, &mce
);
344 #endif /* KVM_CAP_MCE */
348 static void cpu_update_state(void *opaque
, int running
, int reason
)
350 CPUState
*env
= opaque
;
353 env
->tsc_valid
= false;
357 int kvm_arch_init_vcpu(CPUState
*env
)
360 struct kvm_cpuid2 cpuid
;
361 struct kvm_cpuid_entry2 entries
[100];
362 } __attribute__((packed
)) cpuid_data
;
363 uint32_t limit
, i
, j
, cpuid_i
;
365 struct kvm_cpuid_entry2
*c
;
366 #ifdef CONFIG_KVM_PARA
367 uint32_t signature
[3];
370 env
->cpuid_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
372 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
373 env
->cpuid_ext_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_ECX
);
374 env
->cpuid_ext_features
|= i
;
376 env
->cpuid_ext2_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
378 env
->cpuid_ext3_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
380 env
->cpuid_svm_features
&= kvm_arch_get_supported_cpuid(env
, 0x8000000A,
386 #ifdef CONFIG_KVM_PARA
387 /* Paravirtualization CPUIDs */
388 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
389 c
= &cpuid_data
.entries
[cpuid_i
++];
390 memset(c
, 0, sizeof(*c
));
391 c
->function
= KVM_CPUID_SIGNATURE
;
393 c
->ebx
= signature
[0];
394 c
->ecx
= signature
[1];
395 c
->edx
= signature
[2];
397 c
= &cpuid_data
.entries
[cpuid_i
++];
398 memset(c
, 0, sizeof(*c
));
399 c
->function
= KVM_CPUID_FEATURES
;
400 c
->eax
= env
->cpuid_kvm_features
& get_para_features(env
);
403 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
405 for (i
= 0; i
<= limit
; i
++) {
406 c
= &cpuid_data
.entries
[cpuid_i
++];
410 /* Keep reading function 2 till all the input is received */
414 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
415 KVM_CPUID_FLAG_STATE_READ_NEXT
;
416 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
417 times
= c
->eax
& 0xff;
419 for (j
= 1; j
< times
; ++j
) {
420 c
= &cpuid_data
.entries
[cpuid_i
++];
422 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
423 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
432 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
434 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
436 if (i
== 4 && c
->eax
== 0) {
439 if (i
== 0xb && !(c
->ecx
& 0xff00)) {
442 if (i
== 0xd && c
->eax
== 0) {
445 c
= &cpuid_data
.entries
[cpuid_i
++];
451 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
455 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
457 for (i
= 0x80000000; i
<= limit
; i
++) {
458 c
= &cpuid_data
.entries
[cpuid_i
++];
462 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
465 cpuid_data
.cpuid
.nent
= cpuid_i
;
468 if (((env
->cpuid_version
>> 8)&0xF) >= 6
469 && (env
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
470 && kvm_check_extension(env
->kvm_state
, KVM_CAP_MCE
) > 0) {
475 ret
= kvm_get_mce_cap_supported(env
->kvm_state
, &mcg_cap
, &banks
);
477 fprintf(stderr
, "kvm_get_mce_cap_supported: %s", strerror(-ret
));
481 if (banks
> MCE_BANKS_DEF
) {
482 banks
= MCE_BANKS_DEF
;
484 mcg_cap
&= MCE_CAP_DEF
;
486 ret
= kvm_vcpu_ioctl(env
, KVM_X86_SETUP_MCE
, &mcg_cap
);
488 fprintf(stderr
, "KVM_X86_SETUP_MCE: %s", strerror(-ret
));
492 env
->mcg_cap
= mcg_cap
;
496 qemu_add_vm_change_state_handler(cpu_update_state
, env
);
498 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
501 void kvm_arch_reset_vcpu(CPUState
*env
)
503 env
->exception_injected
= -1;
504 env
->interrupt_injected
= -1;
506 if (kvm_irqchip_in_kernel()) {
507 env
->mp_state
= cpu_is_bsp(env
) ? KVM_MP_STATE_RUNNABLE
:
508 KVM_MP_STATE_UNINITIALIZED
;
510 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
514 static int kvm_get_supported_msrs(KVMState
*s
)
516 static int kvm_supported_msrs
;
520 if (kvm_supported_msrs
== 0) {
521 struct kvm_msr_list msr_list
, *kvm_msr_list
;
523 kvm_supported_msrs
= -1;
525 /* Obtain MSR list from KVM. These are the MSRs that we must
528 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
529 if (ret
< 0 && ret
!= -E2BIG
) {
532 /* Old kernel modules had a bug and could write beyond the provided
533 memory. Allocate at least a safe amount of 1K. */
534 kvm_msr_list
= qemu_mallocz(MAX(1024, sizeof(msr_list
) +
536 sizeof(msr_list
.indices
[0])));
538 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
539 ret
= kvm_ioctl(s
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
543 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
544 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
548 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
549 has_msr_hsave_pa
= true;
561 int kvm_arch_init(KVMState
*s
)
563 uint64_t identity_base
= 0xfffbc000;
565 struct utsname utsname
;
567 ret
= kvm_get_supported_msrs(s
);
573 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
576 * On older Intel CPUs, KVM uses vm86 mode to emulate 16-bit code directly.
577 * In order to use vm86 mode, an EPT identity map and a TSS are needed.
578 * Since these must be part of guest physical memory, we need to allocate
579 * them, both by setting their start addresses in the kernel and by
580 * creating a corresponding e820 entry. We need 4 pages before the BIOS.
582 * Older KVM versions may not support setting the identity map base. In
583 * that case we need to stick with the default, i.e. a 256K maximum BIOS
586 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
587 if (kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
588 /* Allows up to 16M BIOSes. */
589 identity_base
= 0xfeffc000;
591 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &identity_base
);
597 /* Set TSS base one page after EPT identity map. */
598 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, identity_base
+ 0x1000);
603 /* Tell fw_cfg to notify the BIOS to reserve the range. */
604 ret
= e820_add_entry(identity_base
, 0x4000, E820_RESERVED
);
606 fprintf(stderr
, "e820_add_entry() table is full\n");
609 qemu_register_reset(kvm_unpoison_all
, NULL
);
614 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
616 lhs
->selector
= rhs
->selector
;
617 lhs
->base
= rhs
->base
;
618 lhs
->limit
= rhs
->limit
;
630 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
632 unsigned flags
= rhs
->flags
;
633 lhs
->selector
= rhs
->selector
;
634 lhs
->base
= rhs
->base
;
635 lhs
->limit
= rhs
->limit
;
636 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
637 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
638 lhs
->dpl
= (flags
>> DESC_DPL_SHIFT
) & 3;
639 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
640 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
641 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
642 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
643 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
647 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
649 lhs
->selector
= rhs
->selector
;
650 lhs
->base
= rhs
->base
;
651 lhs
->limit
= rhs
->limit
;
652 lhs
->flags
= (rhs
->type
<< DESC_TYPE_SHIFT
) |
653 (rhs
->present
* DESC_P_MASK
) |
654 (rhs
->dpl
<< DESC_DPL_SHIFT
) |
655 (rhs
->db
<< DESC_B_SHIFT
) |
656 (rhs
->s
* DESC_S_MASK
) |
657 (rhs
->l
<< DESC_L_SHIFT
) |
658 (rhs
->g
* DESC_G_MASK
) |
659 (rhs
->avl
* DESC_AVL_MASK
);
662 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
665 *kvm_reg
= *qemu_reg
;
667 *qemu_reg
= *kvm_reg
;
671 static int kvm_getput_regs(CPUState
*env
, int set
)
673 struct kvm_regs regs
;
677 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
683 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
684 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
685 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
686 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
687 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
688 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
689 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
690 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
692 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
693 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
694 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
695 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
696 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
697 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
698 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
699 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
702 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
703 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
706 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
712 static int kvm_put_fpu(CPUState
*env
)
717 memset(&fpu
, 0, sizeof fpu
);
718 fpu
.fsw
= env
->fpus
& ~(7 << 11);
719 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
721 for (i
= 0; i
< 8; ++i
) {
722 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
724 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
725 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
726 fpu
.mxcsr
= env
->mxcsr
;
728 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
732 #define XSAVE_CWD_RIP 2
733 #define XSAVE_CWD_RDP 4
734 #define XSAVE_MXCSR 6
735 #define XSAVE_ST_SPACE 8
736 #define XSAVE_XMM_SPACE 40
737 #define XSAVE_XSTATE_BV 128
738 #define XSAVE_YMMH_SPACE 144
741 static int kvm_put_xsave(CPUState
*env
)
745 struct kvm_xsave
* xsave
;
746 uint16_t cwd
, swd
, twd
, fop
;
748 if (!kvm_has_xsave()) {
749 return kvm_put_fpu(env
);
752 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
753 memset(xsave
, 0, sizeof(struct kvm_xsave
));
754 cwd
= swd
= twd
= fop
= 0;
755 swd
= env
->fpus
& ~(7 << 11);
756 swd
|= (env
->fpstt
& 7) << 11;
758 for (i
= 0; i
< 8; ++i
) {
759 twd
|= (!env
->fptags
[i
]) << i
;
761 xsave
->region
[0] = (uint32_t)(swd
<< 16) + cwd
;
762 xsave
->region
[1] = (uint32_t)(fop
<< 16) + twd
;
763 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
765 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
766 sizeof env
->xmm_regs
);
767 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
768 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
769 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
770 sizeof env
->ymmh_regs
);
771 r
= kvm_vcpu_ioctl(env
, KVM_SET_XSAVE
, xsave
);
775 return kvm_put_fpu(env
);
779 static int kvm_put_xcrs(CPUState
*env
)
782 struct kvm_xcrs xcrs
;
784 if (!kvm_has_xcrs()) {
790 xcrs
.xcrs
[0].xcr
= 0;
791 xcrs
.xcrs
[0].value
= env
->xcr0
;
792 return kvm_vcpu_ioctl(env
, KVM_SET_XCRS
, &xcrs
);
798 static int kvm_put_sregs(CPUState
*env
)
800 struct kvm_sregs sregs
;
802 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
803 if (env
->interrupt_injected
>= 0) {
804 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
805 (uint64_t)1 << (env
->interrupt_injected
% 64);
808 if ((env
->eflags
& VM_MASK
)) {
809 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
810 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
811 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
812 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
813 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
814 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
816 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
817 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
818 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
819 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
820 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
821 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
824 set_seg(&sregs
.tr
, &env
->tr
);
825 set_seg(&sregs
.ldt
, &env
->ldt
);
827 sregs
.idt
.limit
= env
->idt
.limit
;
828 sregs
.idt
.base
= env
->idt
.base
;
829 sregs
.gdt
.limit
= env
->gdt
.limit
;
830 sregs
.gdt
.base
= env
->gdt
.base
;
832 sregs
.cr0
= env
->cr
[0];
833 sregs
.cr2
= env
->cr
[2];
834 sregs
.cr3
= env
->cr
[3];
835 sregs
.cr4
= env
->cr
[4];
837 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
838 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
840 sregs
.efer
= env
->efer
;
842 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
845 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
846 uint32_t index
, uint64_t value
)
848 entry
->index
= index
;
852 static int kvm_put_msrs(CPUState
*env
, int level
)
855 struct kvm_msrs info
;
856 struct kvm_msr_entry entries
[100];
858 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
861 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
862 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
863 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
864 kvm_msr_entry_set(&msrs
[n
++], MSR_PAT
, env
->pat
);
866 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
868 if (has_msr_hsave_pa
) {
869 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
872 if (lm_capable_kernel
) {
873 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
874 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
875 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
876 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
879 if (level
== KVM_PUT_FULL_STATE
) {
881 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
882 * writeback. Until this is fixed, we only write the offset to SMP
883 * guests after migration, desynchronizing the VCPUs, but avoiding
884 * huge jump-backs that would occur without any writeback at all.
886 if (smp_cpus
== 1 || env
->tsc
!= 0) {
887 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
891 * The following paravirtual MSRs have side effects on the guest or are
892 * too heavy for normal writeback. Limit them to reset or full state
895 if (level
>= KVM_PUT_RESET_STATE
) {
896 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
897 env
->system_time_msr
);
898 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
899 #if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ASYNC_PF)
900 if (has_msr_async_pf_en
) {
901 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_ASYNC_PF_EN
,
902 env
->async_pf_en_msr
);
910 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
911 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
912 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
913 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
918 msr_data
.info
.nmsrs
= n
;
920 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
925 static int kvm_get_fpu(CPUState
*env
)
930 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
935 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
938 for (i
= 0; i
< 8; ++i
) {
939 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
941 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
942 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
943 env
->mxcsr
= fpu
.mxcsr
;
948 static int kvm_get_xsave(CPUState
*env
)
951 struct kvm_xsave
* xsave
;
953 uint16_t cwd
, swd
, twd
, fop
;
955 if (!kvm_has_xsave()) {
956 return kvm_get_fpu(env
);
959 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
960 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XSAVE
, xsave
);
966 cwd
= (uint16_t)xsave
->region
[0];
967 swd
= (uint16_t)(xsave
->region
[0] >> 16);
968 twd
= (uint16_t)xsave
->region
[1];
969 fop
= (uint16_t)(xsave
->region
[1] >> 16);
970 env
->fpstt
= (swd
>> 11) & 7;
973 for (i
= 0; i
< 8; ++i
) {
974 env
->fptags
[i
] = !((twd
>> i
) & 1);
976 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
977 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
979 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
980 sizeof env
->xmm_regs
);
981 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
982 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
983 sizeof env
->ymmh_regs
);
987 return kvm_get_fpu(env
);
991 static int kvm_get_xcrs(CPUState
*env
)
995 struct kvm_xcrs xcrs
;
997 if (!kvm_has_xcrs()) {
1001 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XCRS
, &xcrs
);
1006 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++) {
1007 /* Only support xcr0 now */
1008 if (xcrs
.xcrs
[0].xcr
== 0) {
1009 env
->xcr0
= xcrs
.xcrs
[0].value
;
1019 static int kvm_get_sregs(CPUState
*env
)
1021 struct kvm_sregs sregs
;
1025 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
1030 /* There can only be one pending IRQ set in the bitmap at a time, so try
1031 to find it and save its number instead (-1 for none). */
1032 env
->interrupt_injected
= -1;
1033 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
1034 if (sregs
.interrupt_bitmap
[i
]) {
1035 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
1036 env
->interrupt_injected
= i
* 64 + bit
;
1041 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
1042 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
1043 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
1044 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
1045 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
1046 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
1048 get_seg(&env
->tr
, &sregs
.tr
);
1049 get_seg(&env
->ldt
, &sregs
.ldt
);
1051 env
->idt
.limit
= sregs
.idt
.limit
;
1052 env
->idt
.base
= sregs
.idt
.base
;
1053 env
->gdt
.limit
= sregs
.gdt
.limit
;
1054 env
->gdt
.base
= sregs
.gdt
.base
;
1056 env
->cr
[0] = sregs
.cr0
;
1057 env
->cr
[2] = sregs
.cr2
;
1058 env
->cr
[3] = sregs
.cr3
;
1059 env
->cr
[4] = sregs
.cr4
;
1061 cpu_set_apic_base(env
->apic_state
, sregs
.apic_base
);
1063 env
->efer
= sregs
.efer
;
1064 //cpu_set_apic_tpr(env->apic_state, sregs.cr8);
1066 #define HFLAG_COPY_MASK \
1067 ~( HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
1068 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
1069 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1070 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1072 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1073 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1074 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1075 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1076 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1077 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1078 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1080 if (env
->efer
& MSR_EFER_LMA
) {
1081 hflags
|= HF_LMA_MASK
;
1084 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1085 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1087 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1088 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1089 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1090 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1091 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
) ||
1092 !(hflags
& HF_CS32_MASK
)) {
1093 hflags
|= HF_ADDSEG_MASK
;
1095 hflags
|= ((env
->segs
[R_DS
].base
| env
->segs
[R_ES
].base
|
1096 env
->segs
[R_SS
].base
) != 0) << HF_ADDSEG_SHIFT
;
1099 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1104 static int kvm_get_msrs(CPUState
*env
)
1107 struct kvm_msrs info
;
1108 struct kvm_msr_entry entries
[100];
1110 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1114 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1115 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1116 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1117 msrs
[n
++].index
= MSR_PAT
;
1119 msrs
[n
++].index
= MSR_STAR
;
1121 if (has_msr_hsave_pa
) {
1122 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1125 if (!env
->tsc_valid
) {
1126 msrs
[n
++].index
= MSR_IA32_TSC
;
1127 env
->tsc_valid
= !vm_running
;
1130 #ifdef TARGET_X86_64
1131 if (lm_capable_kernel
) {
1132 msrs
[n
++].index
= MSR_CSTAR
;
1133 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1134 msrs
[n
++].index
= MSR_FMASK
;
1135 msrs
[n
++].index
= MSR_LSTAR
;
1138 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1139 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1140 #if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ASYNC_PF)
1141 if (has_msr_async_pf_en
) {
1142 msrs
[n
++].index
= MSR_KVM_ASYNC_PF_EN
;
1148 msrs
[n
++].index
= MSR_MCG_STATUS
;
1149 msrs
[n
++].index
= MSR_MCG_CTL
;
1150 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++) {
1151 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1156 msr_data
.info
.nmsrs
= n
;
1157 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
1162 for (i
= 0; i
< ret
; i
++) {
1163 switch (msrs
[i
].index
) {
1164 case MSR_IA32_SYSENTER_CS
:
1165 env
->sysenter_cs
= msrs
[i
].data
;
1167 case MSR_IA32_SYSENTER_ESP
:
1168 env
->sysenter_esp
= msrs
[i
].data
;
1170 case MSR_IA32_SYSENTER_EIP
:
1171 env
->sysenter_eip
= msrs
[i
].data
;
1174 env
->pat
= msrs
[i
].data
;
1177 env
->star
= msrs
[i
].data
;
1179 #ifdef TARGET_X86_64
1181 env
->cstar
= msrs
[i
].data
;
1183 case MSR_KERNELGSBASE
:
1184 env
->kernelgsbase
= msrs
[i
].data
;
1187 env
->fmask
= msrs
[i
].data
;
1190 env
->lstar
= msrs
[i
].data
;
1194 env
->tsc
= msrs
[i
].data
;
1196 case MSR_VM_HSAVE_PA
:
1197 env
->vm_hsave
= msrs
[i
].data
;
1199 case MSR_KVM_SYSTEM_TIME
:
1200 env
->system_time_msr
= msrs
[i
].data
;
1202 case MSR_KVM_WALL_CLOCK
:
1203 env
->wall_clock_msr
= msrs
[i
].data
;
1206 case MSR_MCG_STATUS
:
1207 env
->mcg_status
= msrs
[i
].data
;
1210 env
->mcg_ctl
= msrs
[i
].data
;
1215 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1216 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1217 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1221 #if defined(CONFIG_KVM_PARA) && defined(KVM_CAP_ASYNC_PF)
1222 case MSR_KVM_ASYNC_PF_EN
:
1223 env
->async_pf_en_msr
= msrs
[i
].data
;
1232 static int kvm_put_mp_state(CPUState
*env
)
1234 struct kvm_mp_state mp_state
= { .mp_state
= env
->mp_state
};
1236 return kvm_vcpu_ioctl(env
, KVM_SET_MP_STATE
, &mp_state
);
1239 static int kvm_get_mp_state(CPUState
*env
)
1241 struct kvm_mp_state mp_state
;
1244 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MP_STATE
, &mp_state
);
1248 env
->mp_state
= mp_state
.mp_state
;
1249 if (kvm_irqchip_in_kernel()) {
1250 env
->halted
= (mp_state
.mp_state
== KVM_MP_STATE_HALTED
);
1255 static int kvm_put_vcpu_events(CPUState
*env
, int level
)
1257 #ifdef KVM_CAP_VCPU_EVENTS
1258 struct kvm_vcpu_events events
;
1260 if (!kvm_has_vcpu_events()) {
1264 events
.exception
.injected
= (env
->exception_injected
>= 0);
1265 events
.exception
.nr
= env
->exception_injected
;
1266 events
.exception
.has_error_code
= env
->has_error_code
;
1267 events
.exception
.error_code
= env
->error_code
;
1269 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1270 events
.interrupt
.nr
= env
->interrupt_injected
;
1271 events
.interrupt
.soft
= env
->soft_interrupt
;
1273 events
.nmi
.injected
= env
->nmi_injected
;
1274 events
.nmi
.pending
= env
->nmi_pending
;
1275 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1277 events
.sipi_vector
= env
->sipi_vector
;
1280 if (level
>= KVM_PUT_RESET_STATE
) {
1282 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1285 return kvm_vcpu_ioctl(env
, KVM_SET_VCPU_EVENTS
, &events
);
1291 static int kvm_get_vcpu_events(CPUState
*env
)
1293 #ifdef KVM_CAP_VCPU_EVENTS
1294 struct kvm_vcpu_events events
;
1297 if (!kvm_has_vcpu_events()) {
1301 ret
= kvm_vcpu_ioctl(env
, KVM_GET_VCPU_EVENTS
, &events
);
1305 env
->exception_injected
=
1306 events
.exception
.injected
? events
.exception
.nr
: -1;
1307 env
->has_error_code
= events
.exception
.has_error_code
;
1308 env
->error_code
= events
.exception
.error_code
;
1310 env
->interrupt_injected
=
1311 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1312 env
->soft_interrupt
= events
.interrupt
.soft
;
1314 env
->nmi_injected
= events
.nmi
.injected
;
1315 env
->nmi_pending
= events
.nmi
.pending
;
1316 if (events
.nmi
.masked
) {
1317 env
->hflags2
|= HF2_NMI_MASK
;
1319 env
->hflags2
&= ~HF2_NMI_MASK
;
1322 env
->sipi_vector
= events
.sipi_vector
;
1328 static int kvm_guest_debug_workarounds(CPUState
*env
)
1331 #ifdef KVM_CAP_SET_GUEST_DEBUG
1332 unsigned long reinject_trap
= 0;
1334 if (!kvm_has_vcpu_events()) {
1335 if (env
->exception_injected
== 1) {
1336 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1337 } else if (env
->exception_injected
== 3) {
1338 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1340 env
->exception_injected
= -1;
1344 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1345 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1346 * by updating the debug state once again if single-stepping is on.
1347 * Another reason to call kvm_update_guest_debug here is a pending debug
1348 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1349 * reinject them via SET_GUEST_DEBUG.
1351 if (reinject_trap
||
1352 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1353 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1355 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1359 static int kvm_put_debugregs(CPUState
*env
)
1361 #ifdef KVM_CAP_DEBUGREGS
1362 struct kvm_debugregs dbgregs
;
1365 if (!kvm_has_debugregs()) {
1369 for (i
= 0; i
< 4; i
++) {
1370 dbgregs
.db
[i
] = env
->dr
[i
];
1372 dbgregs
.dr6
= env
->dr
[6];
1373 dbgregs
.dr7
= env
->dr
[7];
1376 return kvm_vcpu_ioctl(env
, KVM_SET_DEBUGREGS
, &dbgregs
);
1382 static int kvm_get_debugregs(CPUState
*env
)
1384 #ifdef KVM_CAP_DEBUGREGS
1385 struct kvm_debugregs dbgregs
;
1388 if (!kvm_has_debugregs()) {
1392 ret
= kvm_vcpu_ioctl(env
, KVM_GET_DEBUGREGS
, &dbgregs
);
1396 for (i
= 0; i
< 4; i
++) {
1397 env
->dr
[i
] = dbgregs
.db
[i
];
1399 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1400 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1406 int kvm_arch_put_registers(CPUState
*env
, int level
)
1410 assert(cpu_is_stopped(env
) || qemu_cpu_is_self(env
));
1412 ret
= kvm_getput_regs(env
, 1);
1416 ret
= kvm_put_xsave(env
);
1420 ret
= kvm_put_xcrs(env
);
1424 ret
= kvm_put_sregs(env
);
1428 /* must be before kvm_put_msrs */
1429 ret
= kvm_inject_mce_oldstyle(env
);
1433 ret
= kvm_put_msrs(env
, level
);
1437 if (level
>= KVM_PUT_RESET_STATE
) {
1438 ret
= kvm_put_mp_state(env
);
1443 ret
= kvm_put_vcpu_events(env
, level
);
1447 ret
= kvm_put_debugregs(env
);
1452 ret
= kvm_guest_debug_workarounds(env
);
1459 int kvm_arch_get_registers(CPUState
*env
)
1463 assert(cpu_is_stopped(env
) || qemu_cpu_is_self(env
));
1465 ret
= kvm_getput_regs(env
, 0);
1469 ret
= kvm_get_xsave(env
);
1473 ret
= kvm_get_xcrs(env
);
1477 ret
= kvm_get_sregs(env
);
1481 ret
= kvm_get_msrs(env
);
1485 ret
= kvm_get_mp_state(env
);
1489 ret
= kvm_get_vcpu_events(env
);
1493 ret
= kvm_get_debugregs(env
);
1500 void kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
1505 if (env
->interrupt_request
& CPU_INTERRUPT_NMI
) {
1506 env
->interrupt_request
&= ~CPU_INTERRUPT_NMI
;
1507 DPRINTF("injected NMI\n");
1508 ret
= kvm_vcpu_ioctl(env
, KVM_NMI
);
1510 fprintf(stderr
, "KVM: injection failed, NMI lost (%s)\n",
1515 if (!kvm_irqchip_in_kernel()) {
1516 /* Force the VCPU out of its inner loop to process the INIT request */
1517 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1518 env
->exit_request
= 1;
1521 /* Try to inject an interrupt if the guest can accept it */
1522 if (run
->ready_for_interrupt_injection
&&
1523 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1524 (env
->eflags
& IF_MASK
)) {
1527 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1528 irq
= cpu_get_pic_interrupt(env
);
1530 struct kvm_interrupt intr
;
1533 DPRINTF("injected interrupt %d\n", irq
);
1534 ret
= kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
1537 "KVM: injection failed, interrupt lost (%s)\n",
1543 /* If we have an interrupt but the guest is not ready to receive an
1544 * interrupt, request an interrupt window exit. This will
1545 * cause a return to userspace as soon as the guest is ready to
1546 * receive interrupts. */
1547 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
1548 run
->request_interrupt_window
= 1;
1550 run
->request_interrupt_window
= 0;
1553 DPRINTF("setting tpr\n");
1554 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1558 void kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
1561 env
->eflags
|= IF_MASK
;
1563 env
->eflags
&= ~IF_MASK
;
1565 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1566 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1569 int kvm_arch_process_async_events(CPUState
*env
)
1571 if (env
->interrupt_request
& CPU_INTERRUPT_MCE
) {
1572 /* We must not raise CPU_INTERRUPT_MCE if it's not supported. */
1573 assert(env
->mcg_cap
);
1575 env
->interrupt_request
&= ~CPU_INTERRUPT_MCE
;
1577 kvm_cpu_synchronize_state(env
);
1579 if (env
->exception_injected
== EXCP08_DBLE
) {
1580 /* this means triple fault */
1581 qemu_system_reset_request();
1582 env
->exit_request
= 1;
1585 env
->exception_injected
= EXCP12_MCHK
;
1586 env
->has_error_code
= 0;
1589 if (kvm_irqchip_in_kernel() && env
->mp_state
== KVM_MP_STATE_HALTED
) {
1590 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
1594 if (kvm_irqchip_in_kernel()) {
1598 if (((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1599 (env
->eflags
& IF_MASK
)) ||
1600 (env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1603 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1604 kvm_cpu_synchronize_state(env
);
1607 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1608 kvm_cpu_synchronize_state(env
);
1615 static int kvm_handle_halt(CPUState
*env
)
1617 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1618 (env
->eflags
& IF_MASK
)) &&
1619 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1627 #ifdef KVM_CAP_SET_GUEST_DEBUG
1628 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1630 static const uint8_t int3
= 0xcc;
1632 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1633 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1)) {
1639 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1643 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1644 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1)) {
1656 static int nb_hw_breakpoint
;
1658 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1662 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1663 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1664 (hw_breakpoint
[n
].len
== len
|| len
== -1)) {
1671 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1672 target_ulong len
, int type
)
1675 case GDB_BREAKPOINT_HW
:
1678 case GDB_WATCHPOINT_WRITE
:
1679 case GDB_WATCHPOINT_ACCESS
:
1686 if (addr
& (len
- 1)) {
1698 if (nb_hw_breakpoint
== 4) {
1701 if (find_hw_breakpoint(addr
, len
, type
) >= 0) {
1704 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1705 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1706 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1712 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1713 target_ulong len
, int type
)
1717 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1722 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1727 void kvm_arch_remove_all_hw_breakpoints(void)
1729 nb_hw_breakpoint
= 0;
1732 static CPUWatchpoint hw_watchpoint
;
1734 static int kvm_handle_debug(struct kvm_debug_exit_arch
*arch_info
)
1739 if (arch_info
->exception
== 1) {
1740 if (arch_info
->dr6
& (1 << 14)) {
1741 if (cpu_single_env
->singlestep_enabled
) {
1745 for (n
= 0; n
< 4; n
++) {
1746 if (arch_info
->dr6
& (1 << n
)) {
1747 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
1753 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1754 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1755 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1759 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1760 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1761 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
1767 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
)) {
1771 cpu_synchronize_state(cpu_single_env
);
1772 assert(cpu_single_env
->exception_injected
== -1);
1775 cpu_single_env
->exception_injected
= arch_info
->exception
;
1776 cpu_single_env
->has_error_code
= 0;
1782 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
1784 const uint8_t type_code
[] = {
1785 [GDB_BREAKPOINT_HW
] = 0x0,
1786 [GDB_WATCHPOINT_WRITE
] = 0x1,
1787 [GDB_WATCHPOINT_ACCESS
] = 0x3
1789 const uint8_t len_code
[] = {
1790 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1794 if (kvm_sw_breakpoints_active(env
)) {
1795 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1797 if (nb_hw_breakpoint
> 0) {
1798 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1799 dbg
->arch
.debugreg
[7] = 0x0600;
1800 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1801 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
1802 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
1803 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
1804 ((uint32_t)len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
1808 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1810 static bool host_supports_vmx(void)
1812 uint32_t ecx
, unused
;
1814 host_cpuid(1, 0, &unused
, &unused
, &ecx
, &unused
);
1815 return ecx
& CPUID_EXT_VMX
;
1818 #define VMX_INVALID_GUEST_STATE 0x80000021
1820 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
1825 switch (run
->exit_reason
) {
1827 DPRINTF("handle_hlt\n");
1828 ret
= kvm_handle_halt(env
);
1830 case KVM_EXIT_SET_TPR
:
1833 case KVM_EXIT_FAIL_ENTRY
:
1834 code
= run
->fail_entry
.hardware_entry_failure_reason
;
1835 fprintf(stderr
, "KVM: entry failed, hardware error 0x%" PRIx64
"\n",
1837 if (host_supports_vmx() && code
== VMX_INVALID_GUEST_STATE
) {
1839 "\nIf you're runnning a guest on an Intel machine without "
1840 "unrestricted mode\n"
1841 "support, the failure can be most likely due to the guest "
1842 "entering an invalid\n"
1843 "state for Intel VT. For example, the guest maybe running "
1844 "in big real mode\n"
1845 "which is not supported on less recent Intel processors."
1850 case KVM_EXIT_EXCEPTION
:
1851 fprintf(stderr
, "KVM: exception %d exit (error code 0x%x)\n",
1852 run
->ex
.exception
, run
->ex
.error_code
);
1855 #ifdef KVM_CAP_SET_GUEST_DEBUG
1856 case KVM_EXIT_DEBUG
:
1857 DPRINTF("kvm_exit_debug\n");
1858 ret
= kvm_handle_debug(&run
->debug
.arch
);
1860 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1862 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
1870 bool kvm_arch_stop_on_emulation_error(CPUState
*env
)
1872 return !(env
->cr
[0] & CR0_PE_MASK
) ||
1873 ((env
->segs
[R_CS
].selector
& 3) != 3);