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"
33 #ifdef CONFIG_KVM_PARA
34 #include <linux/kvm_para.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 static int lm_capable_kernel
;
59 #ifdef KVM_CAP_EXT_CPUID
61 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
63 struct kvm_cpuid2
*cpuid
;
66 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
67 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
69 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
70 if (r
== 0 && cpuid
->nent
>= max
) {
78 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
86 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
87 uint32_t index
, int reg
)
89 struct kvm_cpuid2
*cpuid
;
94 if (!kvm_check_extension(env
->kvm_state
, KVM_CAP_EXT_CPUID
)) {
99 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
103 for (i
= 0; i
< cpuid
->nent
; ++i
) {
104 if (cpuid
->entries
[i
].function
== function
&&
105 cpuid
->entries
[i
].index
== index
) {
108 ret
= cpuid
->entries
[i
].eax
;
111 ret
= cpuid
->entries
[i
].ebx
;
114 ret
= cpuid
->entries
[i
].ecx
;
117 ret
= cpuid
->entries
[i
].edx
;
120 /* KVM before 2.6.30 misreports the following features */
121 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
124 /* On Intel, kvm returns cpuid according to the Intel spec,
125 * so add missing bits according to the AMD spec:
127 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
128 ret
|= cpuid_1_edx
& 0x183f7ff;
143 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
144 uint32_t index
, int reg
)
151 #ifdef CONFIG_KVM_PARA
152 struct kvm_para_features
{
155 } para_features
[] = {
156 #ifdef KVM_CAP_CLOCKSOURCE
157 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
159 #ifdef KVM_CAP_NOP_IO_DELAY
160 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
162 #ifdef KVM_CAP_PV_MMU
163 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
168 static int get_para_features(CPUState
*env
)
172 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
173 if (kvm_check_extension(env
->kvm_state
, para_features
[i
].cap
))
174 features
|= (1 << para_features
[i
].feature
);
182 static int kvm_get_mce_cap_supported(KVMState
*s
, uint64_t *mce_cap
,
187 r
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_MCE
);
190 return kvm_ioctl(s
, KVM_X86_GET_MCE_CAP_SUPPORTED
, mce_cap
);
195 static int kvm_setup_mce(CPUState
*env
, uint64_t *mcg_cap
)
197 return kvm_vcpu_ioctl(env
, KVM_X86_SETUP_MCE
, mcg_cap
);
200 static int kvm_set_mce(CPUState
*env
, struct kvm_x86_mce
*m
)
202 return kvm_vcpu_ioctl(env
, KVM_X86_SET_MCE
, m
);
205 static int kvm_get_msr(CPUState
*env
, struct kvm_msr_entry
*msrs
, int n
)
207 struct kvm_msrs
*kmsrs
= qemu_malloc(sizeof *kmsrs
+ n
* sizeof *msrs
);
211 memcpy(kmsrs
->entries
, msrs
, n
* sizeof *msrs
);
212 r
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, kmsrs
);
213 memcpy(msrs
, kmsrs
->entries
, n
* sizeof *msrs
);
218 /* FIXME: kill this and kvm_get_msr, use env->mcg_status instead */
219 static int kvm_mce_in_exception(CPUState
*env
)
221 struct kvm_msr_entry msr_mcg_status
= {
222 .index
= MSR_MCG_STATUS
,
226 r
= kvm_get_msr(env
, &msr_mcg_status
, 1);
227 if (r
== -1 || r
== 0) {
230 return !!(msr_mcg_status
.data
& MCG_STATUS_MCIP
);
233 struct kvm_x86_mce_data
236 struct kvm_x86_mce
*mce
;
240 static void kvm_do_inject_x86_mce(void *_data
)
242 struct kvm_x86_mce_data
*data
= _data
;
245 /* If there is an MCE exception being processed, ignore this SRAO MCE */
246 if ((data
->env
->mcg_cap
& MCG_SER_P
) &&
247 !(data
->mce
->status
& MCI_STATUS_AR
)) {
248 r
= kvm_mce_in_exception(data
->env
);
250 fprintf(stderr
, "Failed to get MCE status\n");
256 r
= kvm_set_mce(data
->env
, data
->mce
);
258 perror("kvm_set_mce FAILED");
259 if (data
->abort_on_error
) {
266 void kvm_inject_x86_mce(CPUState
*cenv
, int bank
, uint64_t status
,
267 uint64_t mcg_status
, uint64_t addr
, uint64_t misc
,
271 struct kvm_x86_mce mce
= {
274 .mcg_status
= mcg_status
,
278 struct kvm_x86_mce_data data
= {
283 if (!cenv
->mcg_cap
) {
284 fprintf(stderr
, "MCE support is not enabled!\n");
288 run_on_cpu(cenv
, kvm_do_inject_x86_mce
, &data
);
295 int kvm_arch_init_vcpu(CPUState
*env
)
298 struct kvm_cpuid2 cpuid
;
299 struct kvm_cpuid_entry2 entries
[100];
300 } __attribute__((packed
)) cpuid_data
;
301 uint32_t limit
, i
, j
, cpuid_i
;
303 struct kvm_cpuid_entry2
*c
;
304 #ifdef KVM_CPUID_SIGNATURE
305 uint32_t signature
[3];
308 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
310 env
->cpuid_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
312 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
313 env
->cpuid_ext_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_ECX
);
314 env
->cpuid_ext_features
|= i
;
316 env
->cpuid_ext2_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
318 env
->cpuid_ext3_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
320 env
->cpuid_svm_features
&= kvm_arch_get_supported_cpuid(env
, 0x8000000A,
326 #ifdef CONFIG_KVM_PARA
327 /* Paravirtualization CPUIDs */
328 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
329 c
= &cpuid_data
.entries
[cpuid_i
++];
330 memset(c
, 0, sizeof(*c
));
331 c
->function
= KVM_CPUID_SIGNATURE
;
333 c
->ebx
= signature
[0];
334 c
->ecx
= signature
[1];
335 c
->edx
= signature
[2];
337 c
= &cpuid_data
.entries
[cpuid_i
++];
338 memset(c
, 0, sizeof(*c
));
339 c
->function
= KVM_CPUID_FEATURES
;
340 c
->eax
= env
->cpuid_kvm_features
& get_para_features(env
);
343 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
345 for (i
= 0; i
<= limit
; i
++) {
346 c
= &cpuid_data
.entries
[cpuid_i
++];
350 /* Keep reading function 2 till all the input is received */
354 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
355 KVM_CPUID_FLAG_STATE_READ_NEXT
;
356 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
357 times
= c
->eax
& 0xff;
359 for (j
= 1; j
< times
; ++j
) {
360 c
= &cpuid_data
.entries
[cpuid_i
++];
362 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
363 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
372 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
374 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
376 if (i
== 4 && c
->eax
== 0)
378 if (i
== 0xb && !(c
->ecx
& 0xff00))
380 if (i
== 0xd && c
->eax
== 0)
383 c
= &cpuid_data
.entries
[cpuid_i
++];
389 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
393 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
395 for (i
= 0x80000000; i
<= limit
; i
++) {
396 c
= &cpuid_data
.entries
[cpuid_i
++];
400 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
403 cpuid_data
.cpuid
.nent
= cpuid_i
;
406 if (((env
->cpuid_version
>> 8)&0xF) >= 6
407 && (env
->cpuid_features
&(CPUID_MCE
|CPUID_MCA
)) == (CPUID_MCE
|CPUID_MCA
)
408 && kvm_check_extension(env
->kvm_state
, KVM_CAP_MCE
) > 0) {
412 if (kvm_get_mce_cap_supported(env
->kvm_state
, &mcg_cap
, &banks
))
413 perror("kvm_get_mce_cap_supported FAILED");
415 if (banks
> MCE_BANKS_DEF
)
416 banks
= MCE_BANKS_DEF
;
417 mcg_cap
&= MCE_CAP_DEF
;
419 if (kvm_setup_mce(env
, &mcg_cap
))
420 perror("kvm_setup_mce FAILED");
422 env
->mcg_cap
= mcg_cap
;
427 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
430 void kvm_arch_reset_vcpu(CPUState
*env
)
432 env
->exception_injected
= -1;
433 env
->interrupt_injected
= -1;
434 env
->nmi_injected
= 0;
435 env
->nmi_pending
= 0;
436 if (kvm_irqchip_in_kernel()) {
437 env
->mp_state
= cpu_is_bsp(env
) ? KVM_MP_STATE_RUNNABLE
:
438 KVM_MP_STATE_UNINITIALIZED
;
440 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
445 int has_msr_hsave_pa
;
447 static void kvm_supported_msrs(CPUState
*env
)
449 static int kvm_supported_msrs
;
453 if (kvm_supported_msrs
== 0) {
454 struct kvm_msr_list msr_list
, *kvm_msr_list
;
456 kvm_supported_msrs
= -1;
458 /* Obtain MSR list from KVM. These are the MSRs that we must
461 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
462 if (ret
< 0 && ret
!= -E2BIG
) {
465 /* Old kernel modules had a bug and could write beyond the provided
466 memory. Allocate at least a safe amount of 1K. */
467 kvm_msr_list
= qemu_mallocz(MAX(1024, sizeof(msr_list
) +
469 sizeof(msr_list
.indices
[0])));
471 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
472 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
476 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
477 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
481 if (kvm_msr_list
->indices
[i
] == MSR_VM_HSAVE_PA
) {
482 has_msr_hsave_pa
= 1;
494 static int kvm_has_msr_hsave_pa(CPUState
*env
)
496 kvm_supported_msrs(env
);
497 return has_msr_hsave_pa
;
500 static int kvm_has_msr_star(CPUState
*env
)
502 kvm_supported_msrs(env
);
506 static int kvm_init_identity_map_page(KVMState
*s
)
508 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
510 uint64_t addr
= 0xfffbc000;
512 if (!kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
516 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &addr
);
518 fprintf(stderr
, "kvm_set_identity_map_addr: %s\n", strerror(ret
));
525 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
529 struct utsname utsname
;
532 lm_capable_kernel
= strcmp(utsname
.machine
, "x86_64") == 0;
534 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
535 * directly. In order to use vm86 mode, a TSS is needed. Since this
536 * must be part of guest physical memory, we need to allocate it. Older
537 * versions of KVM just assumed that it would be at the end of physical
538 * memory but that doesn't work with more than 4GB of memory. We simply
539 * refuse to work with those older versions of KVM. */
540 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
542 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
546 /* this address is 3 pages before the bios, and the bios should present
547 * as unavaible memory. FIXME, need to ensure the e820 map deals with
551 * Tell fw_cfg to notify the BIOS to reserve the range.
553 if (e820_add_entry(0xfffbc000, 0x4000, E820_RESERVED
) < 0) {
554 perror("e820_add_entry() table is full");
557 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
562 return kvm_init_identity_map_page(s
);
565 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
567 lhs
->selector
= rhs
->selector
;
568 lhs
->base
= rhs
->base
;
569 lhs
->limit
= rhs
->limit
;
581 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
583 unsigned flags
= rhs
->flags
;
584 lhs
->selector
= rhs
->selector
;
585 lhs
->base
= rhs
->base
;
586 lhs
->limit
= rhs
->limit
;
587 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
588 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
589 lhs
->dpl
= rhs
->selector
& 3;
590 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
591 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
592 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
593 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
594 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
598 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
600 lhs
->selector
= rhs
->selector
;
601 lhs
->base
= rhs
->base
;
602 lhs
->limit
= rhs
->limit
;
604 (rhs
->type
<< DESC_TYPE_SHIFT
)
605 | (rhs
->present
* DESC_P_MASK
)
606 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
607 | (rhs
->db
<< DESC_B_SHIFT
)
608 | (rhs
->s
* DESC_S_MASK
)
609 | (rhs
->l
<< DESC_L_SHIFT
)
610 | (rhs
->g
* DESC_G_MASK
)
611 | (rhs
->avl
* DESC_AVL_MASK
);
614 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
617 *kvm_reg
= *qemu_reg
;
619 *qemu_reg
= *kvm_reg
;
622 static int kvm_getput_regs(CPUState
*env
, int set
)
624 struct kvm_regs regs
;
628 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
633 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
634 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
635 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
636 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
637 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
638 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
639 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
640 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
642 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
643 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
644 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
645 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
646 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
647 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
648 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
649 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
652 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
653 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
656 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
661 static int kvm_put_fpu(CPUState
*env
)
666 memset(&fpu
, 0, sizeof fpu
);
667 fpu
.fsw
= env
->fpus
& ~(7 << 11);
668 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
670 for (i
= 0; i
< 8; ++i
)
671 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
672 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
673 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
674 fpu
.mxcsr
= env
->mxcsr
;
676 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
680 #define XSAVE_CWD_RIP 2
681 #define XSAVE_CWD_RDP 4
682 #define XSAVE_MXCSR 6
683 #define XSAVE_ST_SPACE 8
684 #define XSAVE_XMM_SPACE 40
685 #define XSAVE_XSTATE_BV 128
686 #define XSAVE_YMMH_SPACE 144
689 static int kvm_put_xsave(CPUState
*env
)
693 struct kvm_xsave
* xsave
;
694 uint16_t cwd
, swd
, twd
, fop
;
696 if (!kvm_has_xsave())
697 return kvm_put_fpu(env
);
699 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
700 memset(xsave
, 0, sizeof(struct kvm_xsave
));
701 cwd
= swd
= twd
= fop
= 0;
702 swd
= env
->fpus
& ~(7 << 11);
703 swd
|= (env
->fpstt
& 7) << 11;
705 for (i
= 0; i
< 8; ++i
)
706 twd
|= (!env
->fptags
[i
]) << i
;
707 xsave
->region
[0] = (uint32_t)(swd
<< 16) + cwd
;
708 xsave
->region
[1] = (uint32_t)(fop
<< 16) + twd
;
709 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
711 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
712 sizeof env
->xmm_regs
);
713 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
714 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
715 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
716 sizeof env
->ymmh_regs
);
717 r
= kvm_vcpu_ioctl(env
, KVM_SET_XSAVE
, xsave
);
721 return kvm_put_fpu(env
);
725 static int kvm_put_xcrs(CPUState
*env
)
728 struct kvm_xcrs xcrs
;
735 xcrs
.xcrs
[0].xcr
= 0;
736 xcrs
.xcrs
[0].value
= env
->xcr0
;
737 return kvm_vcpu_ioctl(env
, KVM_SET_XCRS
, &xcrs
);
743 static int kvm_put_sregs(CPUState
*env
)
745 struct kvm_sregs sregs
;
747 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
748 if (env
->interrupt_injected
>= 0) {
749 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
750 (uint64_t)1 << (env
->interrupt_injected
% 64);
753 if ((env
->eflags
& VM_MASK
)) {
754 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
755 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
756 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
757 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
758 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
759 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
761 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
762 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
763 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
764 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
765 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
766 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
768 if (env
->cr
[0] & CR0_PE_MASK
) {
769 /* force ss cpl to cs cpl */
770 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
771 (sregs
.cs
.selector
& 3);
772 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
776 set_seg(&sregs
.tr
, &env
->tr
);
777 set_seg(&sregs
.ldt
, &env
->ldt
);
779 sregs
.idt
.limit
= env
->idt
.limit
;
780 sregs
.idt
.base
= env
->idt
.base
;
781 sregs
.gdt
.limit
= env
->gdt
.limit
;
782 sregs
.gdt
.base
= env
->gdt
.base
;
784 sregs
.cr0
= env
->cr
[0];
785 sregs
.cr2
= env
->cr
[2];
786 sregs
.cr3
= env
->cr
[3];
787 sregs
.cr4
= env
->cr
[4];
789 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
790 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
792 sregs
.efer
= env
->efer
;
794 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
797 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
798 uint32_t index
, uint64_t value
)
800 entry
->index
= index
;
804 static int kvm_put_msrs(CPUState
*env
, int level
)
807 struct kvm_msrs info
;
808 struct kvm_msr_entry entries
[100];
810 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
813 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
814 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
815 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
816 if (kvm_has_msr_star(env
))
817 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
818 if (kvm_has_msr_hsave_pa(env
))
819 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
821 if (lm_capable_kernel
) {
822 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
823 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
824 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
825 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
828 if (level
== KVM_PUT_FULL_STATE
) {
830 * KVM is yet unable to synchronize TSC values of multiple VCPUs on
831 * writeback. Until this is fixed, we only write the offset to SMP
832 * guests after migration, desynchronizing the VCPUs, but avoiding
833 * huge jump-backs that would occur without any writeback at all.
835 if (smp_cpus
== 1 || env
->tsc
!= 0) {
836 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
838 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
839 env
->system_time_msr
);
840 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
845 if (level
== KVM_PUT_RESET_STATE
)
846 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
847 else if (level
== KVM_PUT_FULL_STATE
) {
848 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_STATUS
, env
->mcg_status
);
849 kvm_msr_entry_set(&msrs
[n
++], MSR_MCG_CTL
, env
->mcg_ctl
);
850 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++)
851 kvm_msr_entry_set(&msrs
[n
++], MSR_MC0_CTL
+ i
, env
->mce_banks
[i
]);
856 msr_data
.info
.nmsrs
= n
;
858 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
863 static int kvm_get_fpu(CPUState
*env
)
868 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
872 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
875 for (i
= 0; i
< 8; ++i
)
876 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
877 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
878 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
879 env
->mxcsr
= fpu
.mxcsr
;
884 static int kvm_get_xsave(CPUState
*env
)
887 struct kvm_xsave
* xsave
;
889 uint16_t cwd
, swd
, twd
, fop
;
891 if (!kvm_has_xsave())
892 return kvm_get_fpu(env
);
894 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
895 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XSAVE
, xsave
);
901 cwd
= (uint16_t)xsave
->region
[0];
902 swd
= (uint16_t)(xsave
->region
[0] >> 16);
903 twd
= (uint16_t)xsave
->region
[1];
904 fop
= (uint16_t)(xsave
->region
[1] >> 16);
905 env
->fpstt
= (swd
>> 11) & 7;
908 for (i
= 0; i
< 8; ++i
)
909 env
->fptags
[i
] = !((twd
>> i
) & 1);
910 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
911 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
913 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
914 sizeof env
->xmm_regs
);
915 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
916 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
917 sizeof env
->ymmh_regs
);
921 return kvm_get_fpu(env
);
925 static int kvm_get_xcrs(CPUState
*env
)
929 struct kvm_xcrs xcrs
;
934 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XCRS
, &xcrs
);
938 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++)
939 /* Only support xcr0 now */
940 if (xcrs
.xcrs
[0].xcr
== 0) {
941 env
->xcr0
= xcrs
.xcrs
[0].value
;
950 static int kvm_get_sregs(CPUState
*env
)
952 struct kvm_sregs sregs
;
956 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
960 /* There can only be one pending IRQ set in the bitmap at a time, so try
961 to find it and save its number instead (-1 for none). */
962 env
->interrupt_injected
= -1;
963 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
964 if (sregs
.interrupt_bitmap
[i
]) {
965 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
966 env
->interrupt_injected
= i
* 64 + bit
;
971 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
972 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
973 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
974 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
975 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
976 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
978 get_seg(&env
->tr
, &sregs
.tr
);
979 get_seg(&env
->ldt
, &sregs
.ldt
);
981 env
->idt
.limit
= sregs
.idt
.limit
;
982 env
->idt
.base
= sregs
.idt
.base
;
983 env
->gdt
.limit
= sregs
.gdt
.limit
;
984 env
->gdt
.base
= sregs
.gdt
.base
;
986 env
->cr
[0] = sregs
.cr0
;
987 env
->cr
[2] = sregs
.cr2
;
988 env
->cr
[3] = sregs
.cr3
;
989 env
->cr
[4] = sregs
.cr4
;
991 cpu_set_apic_base(env
->apic_state
, sregs
.apic_base
);
993 env
->efer
= sregs
.efer
;
994 //cpu_set_apic_tpr(env->apic_state, sregs.cr8);
996 #define HFLAG_COPY_MASK ~( \
997 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
998 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
999 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
1000 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
1004 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
1005 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
1006 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
1007 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
1008 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
1009 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
1010 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
1012 if (env
->efer
& MSR_EFER_LMA
) {
1013 hflags
|= HF_LMA_MASK
;
1016 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
1017 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
1019 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
1020 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
1021 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
1022 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
1023 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
1024 (env
->eflags
& VM_MASK
) ||
1025 !(hflags
& HF_CS32_MASK
)) {
1026 hflags
|= HF_ADDSEG_MASK
;
1028 hflags
|= ((env
->segs
[R_DS
].base
|
1029 env
->segs
[R_ES
].base
|
1030 env
->segs
[R_SS
].base
) != 0) <<
1034 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
1039 static int kvm_get_msrs(CPUState
*env
)
1042 struct kvm_msrs info
;
1043 struct kvm_msr_entry entries
[100];
1045 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
1049 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
1050 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
1051 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
1052 if (kvm_has_msr_star(env
))
1053 msrs
[n
++].index
= MSR_STAR
;
1054 if (kvm_has_msr_hsave_pa(env
))
1055 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
1056 msrs
[n
++].index
= MSR_IA32_TSC
;
1057 #ifdef TARGET_X86_64
1058 if (lm_capable_kernel
) {
1059 msrs
[n
++].index
= MSR_CSTAR
;
1060 msrs
[n
++].index
= MSR_KERNELGSBASE
;
1061 msrs
[n
++].index
= MSR_FMASK
;
1062 msrs
[n
++].index
= MSR_LSTAR
;
1065 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
1066 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
1070 msrs
[n
++].index
= MSR_MCG_STATUS
;
1071 msrs
[n
++].index
= MSR_MCG_CTL
;
1072 for (i
= 0; i
< (env
->mcg_cap
& 0xff) * 4; i
++)
1073 msrs
[n
++].index
= MSR_MC0_CTL
+ i
;
1077 msr_data
.info
.nmsrs
= n
;
1078 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
1082 for (i
= 0; i
< ret
; i
++) {
1083 switch (msrs
[i
].index
) {
1084 case MSR_IA32_SYSENTER_CS
:
1085 env
->sysenter_cs
= msrs
[i
].data
;
1087 case MSR_IA32_SYSENTER_ESP
:
1088 env
->sysenter_esp
= msrs
[i
].data
;
1090 case MSR_IA32_SYSENTER_EIP
:
1091 env
->sysenter_eip
= msrs
[i
].data
;
1094 env
->star
= msrs
[i
].data
;
1096 #ifdef TARGET_X86_64
1098 env
->cstar
= msrs
[i
].data
;
1100 case MSR_KERNELGSBASE
:
1101 env
->kernelgsbase
= msrs
[i
].data
;
1104 env
->fmask
= msrs
[i
].data
;
1107 env
->lstar
= msrs
[i
].data
;
1111 env
->tsc
= msrs
[i
].data
;
1113 case MSR_VM_HSAVE_PA
:
1114 env
->vm_hsave
= msrs
[i
].data
;
1116 case MSR_KVM_SYSTEM_TIME
:
1117 env
->system_time_msr
= msrs
[i
].data
;
1119 case MSR_KVM_WALL_CLOCK
:
1120 env
->wall_clock_msr
= msrs
[i
].data
;
1123 case MSR_MCG_STATUS
:
1124 env
->mcg_status
= msrs
[i
].data
;
1127 env
->mcg_ctl
= msrs
[i
].data
;
1132 if (msrs
[i
].index
>= MSR_MC0_CTL
&&
1133 msrs
[i
].index
< MSR_MC0_CTL
+ (env
->mcg_cap
& 0xff) * 4) {
1134 env
->mce_banks
[msrs
[i
].index
- MSR_MC0_CTL
] = msrs
[i
].data
;
1144 static int kvm_put_mp_state(CPUState
*env
)
1146 struct kvm_mp_state mp_state
= { .mp_state
= env
->mp_state
};
1148 return kvm_vcpu_ioctl(env
, KVM_SET_MP_STATE
, &mp_state
);
1151 static int kvm_get_mp_state(CPUState
*env
)
1153 struct kvm_mp_state mp_state
;
1156 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MP_STATE
, &mp_state
);
1160 env
->mp_state
= mp_state
.mp_state
;
1164 static int kvm_put_vcpu_events(CPUState
*env
, int level
)
1166 #ifdef KVM_CAP_VCPU_EVENTS
1167 struct kvm_vcpu_events events
;
1169 if (!kvm_has_vcpu_events()) {
1173 events
.exception
.injected
= (env
->exception_injected
>= 0);
1174 events
.exception
.nr
= env
->exception_injected
;
1175 events
.exception
.has_error_code
= env
->has_error_code
;
1176 events
.exception
.error_code
= env
->error_code
;
1178 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
1179 events
.interrupt
.nr
= env
->interrupt_injected
;
1180 events
.interrupt
.soft
= env
->soft_interrupt
;
1182 events
.nmi
.injected
= env
->nmi_injected
;
1183 events
.nmi
.pending
= env
->nmi_pending
;
1184 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
1186 events
.sipi_vector
= env
->sipi_vector
;
1189 if (level
>= KVM_PUT_RESET_STATE
) {
1191 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
1194 return kvm_vcpu_ioctl(env
, KVM_SET_VCPU_EVENTS
, &events
);
1200 static int kvm_get_vcpu_events(CPUState
*env
)
1202 #ifdef KVM_CAP_VCPU_EVENTS
1203 struct kvm_vcpu_events events
;
1206 if (!kvm_has_vcpu_events()) {
1210 ret
= kvm_vcpu_ioctl(env
, KVM_GET_VCPU_EVENTS
, &events
);
1214 env
->exception_injected
=
1215 events
.exception
.injected
? events
.exception
.nr
: -1;
1216 env
->has_error_code
= events
.exception
.has_error_code
;
1217 env
->error_code
= events
.exception
.error_code
;
1219 env
->interrupt_injected
=
1220 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
1221 env
->soft_interrupt
= events
.interrupt
.soft
;
1223 env
->nmi_injected
= events
.nmi
.injected
;
1224 env
->nmi_pending
= events
.nmi
.pending
;
1225 if (events
.nmi
.masked
) {
1226 env
->hflags2
|= HF2_NMI_MASK
;
1228 env
->hflags2
&= ~HF2_NMI_MASK
;
1231 env
->sipi_vector
= events
.sipi_vector
;
1237 static int kvm_guest_debug_workarounds(CPUState
*env
)
1240 #ifdef KVM_CAP_SET_GUEST_DEBUG
1241 unsigned long reinject_trap
= 0;
1243 if (!kvm_has_vcpu_events()) {
1244 if (env
->exception_injected
== 1) {
1245 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1246 } else if (env
->exception_injected
== 3) {
1247 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1249 env
->exception_injected
= -1;
1253 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1254 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1255 * by updating the debug state once again if single-stepping is on.
1256 * Another reason to call kvm_update_guest_debug here is a pending debug
1257 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1258 * reinject them via SET_GUEST_DEBUG.
1260 if (reinject_trap
||
1261 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1262 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1264 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1268 static int kvm_put_debugregs(CPUState
*env
)
1270 #ifdef KVM_CAP_DEBUGREGS
1271 struct kvm_debugregs dbgregs
;
1274 if (!kvm_has_debugregs()) {
1278 for (i
= 0; i
< 4; i
++) {
1279 dbgregs
.db
[i
] = env
->dr
[i
];
1281 dbgregs
.dr6
= env
->dr
[6];
1282 dbgregs
.dr7
= env
->dr
[7];
1285 return kvm_vcpu_ioctl(env
, KVM_SET_DEBUGREGS
, &dbgregs
);
1291 static int kvm_get_debugregs(CPUState
*env
)
1293 #ifdef KVM_CAP_DEBUGREGS
1294 struct kvm_debugregs dbgregs
;
1297 if (!kvm_has_debugregs()) {
1301 ret
= kvm_vcpu_ioctl(env
, KVM_GET_DEBUGREGS
, &dbgregs
);
1305 for (i
= 0; i
< 4; i
++) {
1306 env
->dr
[i
] = dbgregs
.db
[i
];
1308 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1309 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1315 int kvm_arch_put_registers(CPUState
*env
, int level
)
1319 assert(cpu_is_stopped(env
) || qemu_cpu_self(env
));
1321 ret
= kvm_getput_regs(env
, 1);
1325 ret
= kvm_put_xsave(env
);
1329 ret
= kvm_put_xcrs(env
);
1333 ret
= kvm_put_sregs(env
);
1337 ret
= kvm_put_msrs(env
, level
);
1341 if (level
>= KVM_PUT_RESET_STATE
) {
1342 ret
= kvm_put_mp_state(env
);
1347 ret
= kvm_put_vcpu_events(env
, level
);
1352 ret
= kvm_guest_debug_workarounds(env
);
1356 ret
= kvm_put_debugregs(env
);
1363 int kvm_arch_get_registers(CPUState
*env
)
1367 assert(cpu_is_stopped(env
) || qemu_cpu_self(env
));
1369 ret
= kvm_getput_regs(env
, 0);
1373 ret
= kvm_get_xsave(env
);
1377 ret
= kvm_get_xcrs(env
);
1381 ret
= kvm_get_sregs(env
);
1385 ret
= kvm_get_msrs(env
);
1389 ret
= kvm_get_mp_state(env
);
1393 ret
= kvm_get_vcpu_events(env
);
1397 ret
= kvm_get_debugregs(env
);
1404 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
1406 /* Try to inject an interrupt if the guest can accept it */
1407 if (run
->ready_for_interrupt_injection
&&
1408 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1409 (env
->eflags
& IF_MASK
)) {
1412 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1413 irq
= cpu_get_pic_interrupt(env
);
1415 struct kvm_interrupt intr
;
1418 DPRINTF("injected interrupt %d\n", irq
);
1419 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
1423 /* If we have an interrupt but the guest is not ready to receive an
1424 * interrupt, request an interrupt window exit. This will
1425 * cause a return to userspace as soon as the guest is ready to
1426 * receive interrupts. */
1427 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
1428 run
->request_interrupt_window
= 1;
1430 run
->request_interrupt_window
= 0;
1432 DPRINTF("setting tpr\n");
1433 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1438 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
1441 env
->eflags
|= IF_MASK
;
1443 env
->eflags
&= ~IF_MASK
;
1445 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1446 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1451 int kvm_arch_process_irqchip_events(CPUState
*env
)
1453 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1454 kvm_cpu_synchronize_state(env
);
1456 env
->exception_index
= EXCP_HALTED
;
1459 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1460 kvm_cpu_synchronize_state(env
);
1467 static int kvm_handle_halt(CPUState
*env
)
1469 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1470 (env
->eflags
& IF_MASK
)) &&
1471 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1473 env
->exception_index
= EXCP_HLT
;
1480 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
1484 switch (run
->exit_reason
) {
1486 DPRINTF("handle_hlt\n");
1487 ret
= kvm_handle_halt(env
);
1494 #ifdef KVM_CAP_SET_GUEST_DEBUG
1495 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1497 static const uint8_t int3
= 0xcc;
1499 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1500 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1))
1505 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1509 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1510 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
1521 static int nb_hw_breakpoint
;
1523 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1527 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
1528 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1529 (hw_breakpoint
[n
].len
== len
|| len
== -1))
1534 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1535 target_ulong len
, int type
)
1538 case GDB_BREAKPOINT_HW
:
1541 case GDB_WATCHPOINT_WRITE
:
1542 case GDB_WATCHPOINT_ACCESS
:
1549 if (addr
& (len
- 1))
1560 if (nb_hw_breakpoint
== 4)
1563 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
1566 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1567 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1568 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1574 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1575 target_ulong len
, int type
)
1579 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1584 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1589 void kvm_arch_remove_all_hw_breakpoints(void)
1591 nb_hw_breakpoint
= 0;
1594 static CPUWatchpoint hw_watchpoint
;
1596 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
1601 if (arch_info
->exception
== 1) {
1602 if (arch_info
->dr6
& (1 << 14)) {
1603 if (cpu_single_env
->singlestep_enabled
)
1606 for (n
= 0; n
< 4; n
++)
1607 if (arch_info
->dr6
& (1 << n
))
1608 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
1614 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1615 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1616 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1620 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1621 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1622 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
1626 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
1630 cpu_synchronize_state(cpu_single_env
);
1631 assert(cpu_single_env
->exception_injected
== -1);
1633 cpu_single_env
->exception_injected
= arch_info
->exception
;
1634 cpu_single_env
->has_error_code
= 0;
1640 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
1642 const uint8_t type_code
[] = {
1643 [GDB_BREAKPOINT_HW
] = 0x0,
1644 [GDB_WATCHPOINT_WRITE
] = 0x1,
1645 [GDB_WATCHPOINT_ACCESS
] = 0x3
1647 const uint8_t len_code
[] = {
1648 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1652 if (kvm_sw_breakpoints_active(env
))
1653 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1655 if (nb_hw_breakpoint
> 0) {
1656 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1657 dbg
->arch
.debugreg
[7] = 0x0600;
1658 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1659 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
1660 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
1661 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
1662 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
1665 /* Legal xcr0 for loading */
1668 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1670 bool kvm_arch_stop_on_emulation_error(CPUState
*env
)
1672 return !(env
->cr
[0] & CR0_PE_MASK
) ||
1673 ((env
->segs
[R_CS
].selector
& 3) != 3);
1676 static void hardware_memory_error(void)
1678 fprintf(stderr
, "Hardware memory error!\n");
1683 static void kvm_mce_broadcast_rest(CPUState
*env
)
1686 int family
, model
, cpuver
= env
->cpuid_version
;
1688 family
= (cpuver
>> 8) & 0xf;
1689 model
= ((cpuver
>> 12) & 0xf0) + ((cpuver
>> 4) & 0xf);
1691 /* Broadcast MCA signal for processor version 06H_EH and above */
1692 if ((family
== 6 && model
>= 14) || family
> 6) {
1693 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
1697 kvm_inject_x86_mce(cenv
, 1, MCI_STATUS_VAL
| MCI_STATUS_UC
,
1698 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, 0, 0, 1);
1704 int kvm_on_sigbus_vcpu(CPUState
*env
, int code
, void *addr
)
1706 #if defined(KVM_CAP_MCE)
1707 struct kvm_x86_mce mce
= {
1711 ram_addr_t ram_addr
;
1712 target_phys_addr_t paddr
;
1715 if ((env
->mcg_cap
& MCG_SER_P
) && addr
1716 && (code
== BUS_MCEERR_AR
1717 || code
== BUS_MCEERR_AO
)) {
1718 if (code
== BUS_MCEERR_AR
) {
1719 /* Fake an Intel architectural Data Load SRAR UCR */
1720 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1721 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1722 | MCI_STATUS_AR
| 0x134;
1723 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1724 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_EIPV
;
1727 * If there is an MCE excpetion being processed, ignore
1730 r
= kvm_mce_in_exception(env
);
1732 fprintf(stderr
, "Failed to get MCE status\n");
1736 /* Fake an Intel architectural Memory scrubbing UCR */
1737 mce
.status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1738 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1740 mce
.misc
= (MCM_ADDR_PHYS
<< 6) | 0xc;
1741 mce
.mcg_status
= MCG_STATUS_MCIP
| MCG_STATUS_RIPV
;
1743 vaddr
= (void *)addr
;
1744 if (qemu_ram_addr_from_host(vaddr
, &ram_addr
) ||
1745 !kvm_physical_memory_addr_from_ram(env
->kvm_state
, ram_addr
, &paddr
)) {
1746 fprintf(stderr
, "Hardware memory error for memory used by "
1747 "QEMU itself instead of guest system!\n");
1748 /* Hope we are lucky for AO MCE */
1749 if (code
== BUS_MCEERR_AO
) {
1752 hardware_memory_error();
1756 r
= kvm_set_mce(env
, &mce
);
1758 fprintf(stderr
, "kvm_set_mce: %s\n", strerror(errno
));
1761 kvm_mce_broadcast_rest(env
);
1765 if (code
== BUS_MCEERR_AO
) {
1767 } else if (code
== BUS_MCEERR_AR
) {
1768 hardware_memory_error();
1776 int kvm_on_sigbus(int code
, void *addr
)
1778 #if defined(KVM_CAP_MCE)
1779 if ((first_cpu
->mcg_cap
& MCG_SER_P
) && addr
&& code
== BUS_MCEERR_AO
) {
1782 ram_addr_t ram_addr
;
1783 target_phys_addr_t paddr
;
1785 /* Hope we are lucky for AO MCE */
1787 if (qemu_ram_addr_from_host(vaddr
, &ram_addr
) ||
1788 !kvm_physical_memory_addr_from_ram(first_cpu
->kvm_state
, ram_addr
, &paddr
)) {
1789 fprintf(stderr
, "Hardware memory error for memory used by "
1790 "QEMU itself instead of guest system!: %p\n", addr
);
1793 status
= MCI_STATUS_VAL
| MCI_STATUS_UC
| MCI_STATUS_EN
1794 | MCI_STATUS_MISCV
| MCI_STATUS_ADDRV
| MCI_STATUS_S
1796 kvm_inject_x86_mce(first_cpu
, 9, status
,
1797 MCG_STATUS_MCIP
| MCG_STATUS_RIPV
, paddr
,
1798 (MCM_ADDR_PHYS
<< 6) | 0xc, 1);
1799 kvm_mce_broadcast_rest(first_cpu
);
1803 if (code
== BUS_MCEERR_AO
) {
1805 } else if (code
== BUS_MCEERR_AR
) {
1806 hardware_memory_error();