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>
19 #include <linux/kvm.h>
21 #include "qemu-common.h"
26 #include "host-utils.h"
31 #ifdef CONFIG_KVM_PARA
32 #include <linux/kvm_para.h>
38 #define DPRINTF(fmt, ...) \
39 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...) \
45 #define MSR_KVM_WALL_CLOCK 0x11
46 #define MSR_KVM_SYSTEM_TIME 0x12
48 #ifdef KVM_CAP_EXT_CPUID
50 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
52 struct kvm_cpuid2
*cpuid
;
55 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
56 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
58 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
59 if (r
== 0 && cpuid
->nent
>= max
) {
67 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
75 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
76 uint32_t index
, int reg
)
78 struct kvm_cpuid2
*cpuid
;
83 if (!kvm_check_extension(env
->kvm_state
, KVM_CAP_EXT_CPUID
)) {
88 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
92 for (i
= 0; i
< cpuid
->nent
; ++i
) {
93 if (cpuid
->entries
[i
].function
== function
&&
94 cpuid
->entries
[i
].index
== index
) {
97 ret
= cpuid
->entries
[i
].eax
;
100 ret
= cpuid
->entries
[i
].ebx
;
103 ret
= cpuid
->entries
[i
].ecx
;
106 ret
= cpuid
->entries
[i
].edx
;
109 /* KVM before 2.6.30 misreports the following features */
110 ret
|= CPUID_MTRR
| CPUID_PAT
| CPUID_MCE
| CPUID_MCA
;
113 /* On Intel, kvm returns cpuid according to the Intel spec,
114 * so add missing bits according to the AMD spec:
116 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
117 ret
|= cpuid_1_edx
& 0x183f7ff;
132 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
,
133 uint32_t index
, int reg
)
140 #ifdef CONFIG_KVM_PARA
141 struct kvm_para_features
{
144 } para_features
[] = {
145 #ifdef KVM_CAP_CLOCKSOURCE
146 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
148 #ifdef KVM_CAP_NOP_IO_DELAY
149 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
151 #ifdef KVM_CAP_PV_MMU
152 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
157 static int get_para_features(CPUState
*env
)
161 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
162 if (kvm_check_extension(env
->kvm_state
, para_features
[i
].cap
))
163 features
|= (1 << para_features
[i
].feature
);
170 int kvm_arch_init_vcpu(CPUState
*env
)
173 struct kvm_cpuid2 cpuid
;
174 struct kvm_cpuid_entry2 entries
[100];
175 } __attribute__((packed
)) cpuid_data
;
176 uint32_t limit
, i
, j
, cpuid_i
;
178 struct kvm_cpuid_entry2
*c
;
179 #ifdef KVM_CPUID_SIGNATURE
180 uint32_t signature
[3];
183 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
185 env
->cpuid_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_EDX
);
187 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
188 env
->cpuid_ext_features
&= kvm_arch_get_supported_cpuid(env
, 1, 0, R_ECX
);
189 env
->cpuid_ext_features
|= i
;
191 env
->cpuid_ext2_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
193 env
->cpuid_ext3_features
&= kvm_arch_get_supported_cpuid(env
, 0x80000001,
198 #ifdef CONFIG_KVM_PARA
199 /* Paravirtualization CPUIDs */
200 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
201 c
= &cpuid_data
.entries
[cpuid_i
++];
202 memset(c
, 0, sizeof(*c
));
203 c
->function
= KVM_CPUID_SIGNATURE
;
205 c
->ebx
= signature
[0];
206 c
->ecx
= signature
[1];
207 c
->edx
= signature
[2];
209 c
= &cpuid_data
.entries
[cpuid_i
++];
210 memset(c
, 0, sizeof(*c
));
211 c
->function
= KVM_CPUID_FEATURES
;
212 c
->eax
= env
->cpuid_kvm_features
& get_para_features(env
);
215 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
217 for (i
= 0; i
<= limit
; i
++) {
218 c
= &cpuid_data
.entries
[cpuid_i
++];
222 /* Keep reading function 2 till all the input is received */
226 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
227 KVM_CPUID_FLAG_STATE_READ_NEXT
;
228 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
229 times
= c
->eax
& 0xff;
231 for (j
= 1; j
< times
; ++j
) {
232 c
= &cpuid_data
.entries
[cpuid_i
++];
234 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
235 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
244 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
246 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
248 if (i
== 4 && c
->eax
== 0)
250 if (i
== 0xb && !(c
->ecx
& 0xff00))
252 if (i
== 0xd && c
->eax
== 0)
255 c
= &cpuid_data
.entries
[cpuid_i
++];
261 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
265 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
267 for (i
= 0x80000000; i
<= limit
; i
++) {
268 c
= &cpuid_data
.entries
[cpuid_i
++];
272 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
275 cpuid_data
.cpuid
.nent
= cpuid_i
;
277 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
280 void kvm_arch_reset_vcpu(CPUState
*env
)
282 env
->exception_injected
= -1;
283 env
->interrupt_injected
= -1;
284 env
->nmi_injected
= 0;
285 env
->nmi_pending
= 0;
286 if (kvm_irqchip_in_kernel()) {
287 env
->mp_state
= cpu_is_bsp(env
) ? KVM_MP_STATE_RUNNABLE
:
288 KVM_MP_STATE_UNINITIALIZED
;
290 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
294 static int kvm_has_msr_star(CPUState
*env
)
296 static int has_msr_star
;
300 if (has_msr_star
== 0) {
301 struct kvm_msr_list msr_list
, *kvm_msr_list
;
305 /* Obtain MSR list from KVM. These are the MSRs that we must
308 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
309 if (ret
< 0 && ret
!= -E2BIG
) {
312 /* Old kernel modules had a bug and could write beyond the provided
313 memory. Allocate at least a safe amount of 1K. */
314 kvm_msr_list
= qemu_mallocz(MAX(1024, sizeof(msr_list
) +
316 sizeof(msr_list
.indices
[0])));
318 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
319 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
323 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
324 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
334 if (has_msr_star
== 1)
339 static int kvm_init_identity_map_page(KVMState
*s
)
341 #ifdef KVM_CAP_SET_IDENTITY_MAP_ADDR
343 uint64_t addr
= 0xfffbc000;
345 if (!kvm_check_extension(s
, KVM_CAP_SET_IDENTITY_MAP_ADDR
)) {
349 ret
= kvm_vm_ioctl(s
, KVM_SET_IDENTITY_MAP_ADDR
, &addr
);
351 fprintf(stderr
, "kvm_set_identity_map_addr: %s\n", strerror(ret
));
358 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
362 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
363 * directly. In order to use vm86 mode, a TSS is needed. Since this
364 * must be part of guest physical memory, we need to allocate it. Older
365 * versions of KVM just assumed that it would be at the end of physical
366 * memory but that doesn't work with more than 4GB of memory. We simply
367 * refuse to work with those older versions of KVM. */
368 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
370 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
374 /* this address is 3 pages before the bios, and the bios should present
375 * as unavaible memory. FIXME, need to ensure the e820 map deals with
379 * Tell fw_cfg to notify the BIOS to reserve the range.
381 if (e820_add_entry(0xfffbc000, 0x4000, E820_RESERVED
) < 0) {
382 perror("e820_add_entry() table is full");
385 ret
= kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
390 return kvm_init_identity_map_page(s
);
393 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
395 lhs
->selector
= rhs
->selector
;
396 lhs
->base
= rhs
->base
;
397 lhs
->limit
= rhs
->limit
;
409 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
411 unsigned flags
= rhs
->flags
;
412 lhs
->selector
= rhs
->selector
;
413 lhs
->base
= rhs
->base
;
414 lhs
->limit
= rhs
->limit
;
415 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
416 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
417 lhs
->dpl
= rhs
->selector
& 3;
418 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
419 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
420 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
421 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
422 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
426 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
428 lhs
->selector
= rhs
->selector
;
429 lhs
->base
= rhs
->base
;
430 lhs
->limit
= rhs
->limit
;
432 (rhs
->type
<< DESC_TYPE_SHIFT
)
433 | (rhs
->present
* DESC_P_MASK
)
434 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
435 | (rhs
->db
<< DESC_B_SHIFT
)
436 | (rhs
->s
* DESC_S_MASK
)
437 | (rhs
->l
<< DESC_L_SHIFT
)
438 | (rhs
->g
* DESC_G_MASK
)
439 | (rhs
->avl
* DESC_AVL_MASK
);
442 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
445 *kvm_reg
= *qemu_reg
;
447 *qemu_reg
= *kvm_reg
;
450 static int kvm_getput_regs(CPUState
*env
, int set
)
452 struct kvm_regs regs
;
456 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
461 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
462 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
463 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
464 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
465 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
466 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
467 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
468 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
470 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
471 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
472 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
473 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
474 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
475 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
476 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
477 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
480 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
481 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
484 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
489 static int kvm_put_fpu(CPUState
*env
)
494 memset(&fpu
, 0, sizeof fpu
);
495 fpu
.fsw
= env
->fpus
& ~(7 << 11);
496 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
498 for (i
= 0; i
< 8; ++i
)
499 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
500 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
501 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
502 fpu
.mxcsr
= env
->mxcsr
;
504 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
508 #define XSAVE_CWD_RIP 2
509 #define XSAVE_CWD_RDP 4
510 #define XSAVE_MXCSR 6
511 #define XSAVE_ST_SPACE 8
512 #define XSAVE_XMM_SPACE 40
513 #define XSAVE_XSTATE_BV 128
514 #define XSAVE_YMMH_SPACE 144
517 static int kvm_put_xsave(CPUState
*env
)
521 struct kvm_xsave
* xsave
;
522 uint16_t cwd
, swd
, twd
, fop
;
524 if (!kvm_has_xsave())
525 return kvm_put_fpu(env
);
527 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
528 memset(xsave
, 0, sizeof(struct kvm_xsave
));
529 cwd
= swd
= twd
= fop
= 0;
530 swd
= env
->fpus
& ~(7 << 11);
531 swd
|= (env
->fpstt
& 7) << 11;
533 for (i
= 0; i
< 8; ++i
)
534 twd
|= (!env
->fptags
[i
]) << i
;
535 xsave
->region
[0] = (uint32_t)(swd
<< 16) + cwd
;
536 xsave
->region
[1] = (uint32_t)(fop
<< 16) + twd
;
537 memcpy(&xsave
->region
[XSAVE_ST_SPACE
], env
->fpregs
,
539 memcpy(&xsave
->region
[XSAVE_XMM_SPACE
], env
->xmm_regs
,
540 sizeof env
->xmm_regs
);
541 xsave
->region
[XSAVE_MXCSR
] = env
->mxcsr
;
542 *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
] = env
->xstate_bv
;
543 memcpy(&xsave
->region
[XSAVE_YMMH_SPACE
], env
->ymmh_regs
,
544 sizeof env
->ymmh_regs
);
545 return kvm_vcpu_ioctl(env
, KVM_SET_XSAVE
, xsave
);
547 return kvm_put_fpu(env
);
551 static int kvm_put_xcrs(CPUState
*env
)
554 struct kvm_xcrs xcrs
;
561 xcrs
.xcrs
[0].xcr
= 0;
562 xcrs
.xcrs
[0].value
= env
->xcr0
;
563 return kvm_vcpu_ioctl(env
, KVM_SET_XCRS
, &xcrs
);
569 static int kvm_put_sregs(CPUState
*env
)
571 struct kvm_sregs sregs
;
573 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
574 if (env
->interrupt_injected
>= 0) {
575 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
576 (uint64_t)1 << (env
->interrupt_injected
% 64);
579 if ((env
->eflags
& VM_MASK
)) {
580 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
581 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
582 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
583 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
584 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
585 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
587 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
588 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
589 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
590 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
591 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
592 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
594 if (env
->cr
[0] & CR0_PE_MASK
) {
595 /* force ss cpl to cs cpl */
596 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
597 (sregs
.cs
.selector
& 3);
598 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
602 set_seg(&sregs
.tr
, &env
->tr
);
603 set_seg(&sregs
.ldt
, &env
->ldt
);
605 sregs
.idt
.limit
= env
->idt
.limit
;
606 sregs
.idt
.base
= env
->idt
.base
;
607 sregs
.gdt
.limit
= env
->gdt
.limit
;
608 sregs
.gdt
.base
= env
->gdt
.base
;
610 sregs
.cr0
= env
->cr
[0];
611 sregs
.cr2
= env
->cr
[2];
612 sregs
.cr3
= env
->cr
[3];
613 sregs
.cr4
= env
->cr
[4];
615 sregs
.cr8
= cpu_get_apic_tpr(env
->apic_state
);
616 sregs
.apic_base
= cpu_get_apic_base(env
->apic_state
);
618 sregs
.efer
= env
->efer
;
620 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
623 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
624 uint32_t index
, uint64_t value
)
626 entry
->index
= index
;
630 static int kvm_put_msrs(CPUState
*env
, int level
)
633 struct kvm_msrs info
;
634 struct kvm_msr_entry entries
[100];
636 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
639 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
640 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
641 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
642 if (kvm_has_msr_star(env
))
643 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
645 /* FIXME if lm capable */
646 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
647 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
648 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
649 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
651 if (level
== KVM_PUT_FULL_STATE
) {
652 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
653 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
,
654 env
->system_time_msr
);
655 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
658 msr_data
.info
.nmsrs
= n
;
660 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
665 static int kvm_get_fpu(CPUState
*env
)
670 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
674 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
677 for (i
= 0; i
< 8; ++i
)
678 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
679 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
680 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
681 env
->mxcsr
= fpu
.mxcsr
;
686 static int kvm_get_xsave(CPUState
*env
)
689 struct kvm_xsave
* xsave
;
691 uint16_t cwd
, swd
, twd
, fop
;
693 if (!kvm_has_xsave())
694 return kvm_get_fpu(env
);
696 xsave
= qemu_memalign(4096, sizeof(struct kvm_xsave
));
697 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XSAVE
, xsave
);
701 cwd
= (uint16_t)xsave
->region
[0];
702 swd
= (uint16_t)(xsave
->region
[0] >> 16);
703 twd
= (uint16_t)xsave
->region
[1];
704 fop
= (uint16_t)(xsave
->region
[1] >> 16);
705 env
->fpstt
= (swd
>> 11) & 7;
708 for (i
= 0; i
< 8; ++i
)
709 env
->fptags
[i
] = !((twd
>> i
) & 1);
710 env
->mxcsr
= xsave
->region
[XSAVE_MXCSR
];
711 memcpy(env
->fpregs
, &xsave
->region
[XSAVE_ST_SPACE
],
713 memcpy(env
->xmm_regs
, &xsave
->region
[XSAVE_XMM_SPACE
],
714 sizeof env
->xmm_regs
);
715 env
->xstate_bv
= *(uint64_t *)&xsave
->region
[XSAVE_XSTATE_BV
];
716 memcpy(env
->ymmh_regs
, &xsave
->region
[XSAVE_YMMH_SPACE
],
717 sizeof env
->ymmh_regs
);
720 return kvm_get_fpu(env
);
724 static int kvm_get_xcrs(CPUState
*env
)
728 struct kvm_xcrs xcrs
;
733 ret
= kvm_vcpu_ioctl(env
, KVM_GET_XCRS
, &xcrs
);
737 for (i
= 0; i
< xcrs
.nr_xcrs
; i
++)
738 /* Only support xcr0 now */
739 if (xcrs
.xcrs
[0].xcr
== 0) {
740 env
->xcr0
= xcrs
.xcrs
[0].value
;
749 static int kvm_get_sregs(CPUState
*env
)
751 struct kvm_sregs sregs
;
755 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
759 /* There can only be one pending IRQ set in the bitmap at a time, so try
760 to find it and save its number instead (-1 for none). */
761 env
->interrupt_injected
= -1;
762 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
763 if (sregs
.interrupt_bitmap
[i
]) {
764 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
765 env
->interrupt_injected
= i
* 64 + bit
;
770 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
771 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
772 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
773 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
774 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
775 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
777 get_seg(&env
->tr
, &sregs
.tr
);
778 get_seg(&env
->ldt
, &sregs
.ldt
);
780 env
->idt
.limit
= sregs
.idt
.limit
;
781 env
->idt
.base
= sregs
.idt
.base
;
782 env
->gdt
.limit
= sregs
.gdt
.limit
;
783 env
->gdt
.base
= sregs
.gdt
.base
;
785 env
->cr
[0] = sregs
.cr0
;
786 env
->cr
[2] = sregs
.cr2
;
787 env
->cr
[3] = sregs
.cr3
;
788 env
->cr
[4] = sregs
.cr4
;
790 cpu_set_apic_base(env
->apic_state
, sregs
.apic_base
);
792 env
->efer
= sregs
.efer
;
793 //cpu_set_apic_tpr(env->apic_state, sregs.cr8);
795 #define HFLAG_COPY_MASK ~( \
796 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
797 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
798 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
799 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
803 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
804 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
805 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
806 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
807 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
808 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
809 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
811 if (env
->efer
& MSR_EFER_LMA
) {
812 hflags
|= HF_LMA_MASK
;
815 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
816 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
818 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
819 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
820 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
821 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
822 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
823 (env
->eflags
& VM_MASK
) ||
824 !(hflags
& HF_CS32_MASK
)) {
825 hflags
|= HF_ADDSEG_MASK
;
827 hflags
|= ((env
->segs
[R_DS
].base
|
828 env
->segs
[R_ES
].base
|
829 env
->segs
[R_SS
].base
) != 0) <<
833 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
838 static int kvm_get_msrs(CPUState
*env
)
841 struct kvm_msrs info
;
842 struct kvm_msr_entry entries
[100];
844 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
848 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
849 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
850 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
851 if (kvm_has_msr_star(env
))
852 msrs
[n
++].index
= MSR_STAR
;
853 msrs
[n
++].index
= MSR_IA32_TSC
;
855 /* FIXME lm_capable_kernel */
856 msrs
[n
++].index
= MSR_CSTAR
;
857 msrs
[n
++].index
= MSR_KERNELGSBASE
;
858 msrs
[n
++].index
= MSR_FMASK
;
859 msrs
[n
++].index
= MSR_LSTAR
;
861 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
862 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
864 msr_data
.info
.nmsrs
= n
;
865 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
869 for (i
= 0; i
< ret
; i
++) {
870 switch (msrs
[i
].index
) {
871 case MSR_IA32_SYSENTER_CS
:
872 env
->sysenter_cs
= msrs
[i
].data
;
874 case MSR_IA32_SYSENTER_ESP
:
875 env
->sysenter_esp
= msrs
[i
].data
;
877 case MSR_IA32_SYSENTER_EIP
:
878 env
->sysenter_eip
= msrs
[i
].data
;
881 env
->star
= msrs
[i
].data
;
885 env
->cstar
= msrs
[i
].data
;
887 case MSR_KERNELGSBASE
:
888 env
->kernelgsbase
= msrs
[i
].data
;
891 env
->fmask
= msrs
[i
].data
;
894 env
->lstar
= msrs
[i
].data
;
898 env
->tsc
= msrs
[i
].data
;
900 case MSR_KVM_SYSTEM_TIME
:
901 env
->system_time_msr
= msrs
[i
].data
;
903 case MSR_KVM_WALL_CLOCK
:
904 env
->wall_clock_msr
= msrs
[i
].data
;
912 static int kvm_put_mp_state(CPUState
*env
)
914 struct kvm_mp_state mp_state
= { .mp_state
= env
->mp_state
};
916 return kvm_vcpu_ioctl(env
, KVM_SET_MP_STATE
, &mp_state
);
919 static int kvm_get_mp_state(CPUState
*env
)
921 struct kvm_mp_state mp_state
;
924 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MP_STATE
, &mp_state
);
928 env
->mp_state
= mp_state
.mp_state
;
932 static int kvm_put_vcpu_events(CPUState
*env
, int level
)
934 #ifdef KVM_CAP_VCPU_EVENTS
935 struct kvm_vcpu_events events
;
937 if (!kvm_has_vcpu_events()) {
941 events
.exception
.injected
= (env
->exception_injected
>= 0);
942 events
.exception
.nr
= env
->exception_injected
;
943 events
.exception
.has_error_code
= env
->has_error_code
;
944 events
.exception
.error_code
= env
->error_code
;
946 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
947 events
.interrupt
.nr
= env
->interrupt_injected
;
948 events
.interrupt
.soft
= env
->soft_interrupt
;
950 events
.nmi
.injected
= env
->nmi_injected
;
951 events
.nmi
.pending
= env
->nmi_pending
;
952 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
954 events
.sipi_vector
= env
->sipi_vector
;
957 if (level
>= KVM_PUT_RESET_STATE
) {
959 KVM_VCPUEVENT_VALID_NMI_PENDING
| KVM_VCPUEVENT_VALID_SIPI_VECTOR
;
962 return kvm_vcpu_ioctl(env
, KVM_SET_VCPU_EVENTS
, &events
);
968 static int kvm_get_vcpu_events(CPUState
*env
)
970 #ifdef KVM_CAP_VCPU_EVENTS
971 struct kvm_vcpu_events events
;
974 if (!kvm_has_vcpu_events()) {
978 ret
= kvm_vcpu_ioctl(env
, KVM_GET_VCPU_EVENTS
, &events
);
982 env
->exception_injected
=
983 events
.exception
.injected
? events
.exception
.nr
: -1;
984 env
->has_error_code
= events
.exception
.has_error_code
;
985 env
->error_code
= events
.exception
.error_code
;
987 env
->interrupt_injected
=
988 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
989 env
->soft_interrupt
= events
.interrupt
.soft
;
991 env
->nmi_injected
= events
.nmi
.injected
;
992 env
->nmi_pending
= events
.nmi
.pending
;
993 if (events
.nmi
.masked
) {
994 env
->hflags2
|= HF2_NMI_MASK
;
996 env
->hflags2
&= ~HF2_NMI_MASK
;
999 env
->sipi_vector
= events
.sipi_vector
;
1005 static int kvm_guest_debug_workarounds(CPUState
*env
)
1008 #ifdef KVM_CAP_SET_GUEST_DEBUG
1009 unsigned long reinject_trap
= 0;
1011 if (!kvm_has_vcpu_events()) {
1012 if (env
->exception_injected
== 1) {
1013 reinject_trap
= KVM_GUESTDBG_INJECT_DB
;
1014 } else if (env
->exception_injected
== 3) {
1015 reinject_trap
= KVM_GUESTDBG_INJECT_BP
;
1017 env
->exception_injected
= -1;
1021 * Kernels before KVM_CAP_X86_ROBUST_SINGLESTEP overwrote flags.TF
1022 * injected via SET_GUEST_DEBUG while updating GP regs. Work around this
1023 * by updating the debug state once again if single-stepping is on.
1024 * Another reason to call kvm_update_guest_debug here is a pending debug
1025 * trap raise by the guest. On kernels without SET_VCPU_EVENTS we have to
1026 * reinject them via SET_GUEST_DEBUG.
1028 if (reinject_trap
||
1029 (!kvm_has_robust_singlestep() && env
->singlestep_enabled
)) {
1030 ret
= kvm_update_guest_debug(env
, reinject_trap
);
1032 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1036 static int kvm_put_debugregs(CPUState
*env
)
1038 #ifdef KVM_CAP_DEBUGREGS
1039 struct kvm_debugregs dbgregs
;
1042 if (!kvm_has_debugregs()) {
1046 for (i
= 0; i
< 4; i
++) {
1047 dbgregs
.db
[i
] = env
->dr
[i
];
1049 dbgregs
.dr6
= env
->dr
[6];
1050 dbgregs
.dr7
= env
->dr
[7];
1053 return kvm_vcpu_ioctl(env
, KVM_SET_DEBUGREGS
, &dbgregs
);
1059 static int kvm_get_debugregs(CPUState
*env
)
1061 #ifdef KVM_CAP_DEBUGREGS
1062 struct kvm_debugregs dbgregs
;
1065 if (!kvm_has_debugregs()) {
1069 ret
= kvm_vcpu_ioctl(env
, KVM_GET_DEBUGREGS
, &dbgregs
);
1073 for (i
= 0; i
< 4; i
++) {
1074 env
->dr
[i
] = dbgregs
.db
[i
];
1076 env
->dr
[4] = env
->dr
[6] = dbgregs
.dr6
;
1077 env
->dr
[5] = env
->dr
[7] = dbgregs
.dr7
;
1083 int kvm_arch_put_registers(CPUState
*env
, int level
)
1087 assert(cpu_is_stopped(env
) || qemu_cpu_self(env
));
1089 ret
= kvm_getput_regs(env
, 1);
1093 ret
= kvm_put_xsave(env
);
1097 ret
= kvm_put_xcrs(env
);
1101 ret
= kvm_put_sregs(env
);
1105 ret
= kvm_put_msrs(env
, level
);
1109 if (level
>= KVM_PUT_RESET_STATE
) {
1110 ret
= kvm_put_mp_state(env
);
1115 ret
= kvm_put_vcpu_events(env
, level
);
1120 ret
= kvm_guest_debug_workarounds(env
);
1124 ret
= kvm_put_debugregs(env
);
1131 int kvm_arch_get_registers(CPUState
*env
)
1135 assert(cpu_is_stopped(env
) || qemu_cpu_self(env
));
1137 ret
= kvm_getput_regs(env
, 0);
1141 ret
= kvm_get_xsave(env
);
1145 ret
= kvm_get_xcrs(env
);
1149 ret
= kvm_get_sregs(env
);
1153 ret
= kvm_get_msrs(env
);
1157 ret
= kvm_get_mp_state(env
);
1161 ret
= kvm_get_vcpu_events(env
);
1165 ret
= kvm_get_debugregs(env
);
1172 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
1174 /* Try to inject an interrupt if the guest can accept it */
1175 if (run
->ready_for_interrupt_injection
&&
1176 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1177 (env
->eflags
& IF_MASK
)) {
1180 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
1181 irq
= cpu_get_pic_interrupt(env
);
1183 struct kvm_interrupt intr
;
1186 DPRINTF("injected interrupt %d\n", irq
);
1187 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
1191 /* If we have an interrupt but the guest is not ready to receive an
1192 * interrupt, request an interrupt window exit. This will
1193 * cause a return to userspace as soon as the guest is ready to
1194 * receive interrupts. */
1195 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
1196 run
->request_interrupt_window
= 1;
1198 run
->request_interrupt_window
= 0;
1200 DPRINTF("setting tpr\n");
1201 run
->cr8
= cpu_get_apic_tpr(env
->apic_state
);
1206 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
1209 env
->eflags
|= IF_MASK
;
1211 env
->eflags
&= ~IF_MASK
;
1213 cpu_set_apic_tpr(env
->apic_state
, run
->cr8
);
1214 cpu_set_apic_base(env
->apic_state
, run
->apic_base
);
1219 int kvm_arch_process_irqchip_events(CPUState
*env
)
1221 if (env
->interrupt_request
& CPU_INTERRUPT_INIT
) {
1222 kvm_cpu_synchronize_state(env
);
1224 env
->exception_index
= EXCP_HALTED
;
1227 if (env
->interrupt_request
& CPU_INTERRUPT_SIPI
) {
1228 kvm_cpu_synchronize_state(env
);
1235 static int kvm_handle_halt(CPUState
*env
)
1237 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1238 (env
->eflags
& IF_MASK
)) &&
1239 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
1241 env
->exception_index
= EXCP_HLT
;
1248 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
1252 switch (run
->exit_reason
) {
1254 DPRINTF("handle_hlt\n");
1255 ret
= kvm_handle_halt(env
);
1262 #ifdef KVM_CAP_SET_GUEST_DEBUG
1263 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1265 static const uint8_t int3
= 0xcc;
1267 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
1268 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1))
1273 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
1277 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
1278 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
1289 static int nb_hw_breakpoint
;
1291 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1295 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
1296 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1297 (hw_breakpoint
[n
].len
== len
|| len
== -1))
1302 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1303 target_ulong len
, int type
)
1306 case GDB_BREAKPOINT_HW
:
1309 case GDB_WATCHPOINT_WRITE
:
1310 case GDB_WATCHPOINT_ACCESS
:
1317 if (addr
& (len
- 1))
1328 if (nb_hw_breakpoint
== 4)
1331 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
1334 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1335 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1336 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1342 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1343 target_ulong len
, int type
)
1347 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1352 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1357 void kvm_arch_remove_all_hw_breakpoints(void)
1359 nb_hw_breakpoint
= 0;
1362 static CPUWatchpoint hw_watchpoint
;
1364 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
1369 if (arch_info
->exception
== 1) {
1370 if (arch_info
->dr6
& (1 << 14)) {
1371 if (cpu_single_env
->singlestep_enabled
)
1374 for (n
= 0; n
< 4; n
++)
1375 if (arch_info
->dr6
& (1 << n
))
1376 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
1382 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1383 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1384 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1388 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1389 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1390 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
1394 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
1398 cpu_synchronize_state(cpu_single_env
);
1399 assert(cpu_single_env
->exception_injected
== -1);
1401 cpu_single_env
->exception_injected
= arch_info
->exception
;
1402 cpu_single_env
->has_error_code
= 0;
1408 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
1410 const uint8_t type_code
[] = {
1411 [GDB_BREAKPOINT_HW
] = 0x0,
1412 [GDB_WATCHPOINT_WRITE
] = 0x1,
1413 [GDB_WATCHPOINT_ACCESS
] = 0x3
1415 const uint8_t len_code
[] = {
1416 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1420 if (kvm_sw_breakpoints_active(env
))
1421 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1423 if (nb_hw_breakpoint
> 0) {
1424 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1425 dbg
->arch
.debugreg
[7] = 0x0600;
1426 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1427 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
1428 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
1429 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
1430 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
1433 /* Legal xcr0 for loading */
1436 #endif /* KVM_CAP_SET_GUEST_DEBUG */
1438 bool kvm_arch_stop_on_emulation_error(CPUState
*env
)
1440 return !(env
->cr
[0] & CR0_PE_MASK
) ||
1441 ((env
->segs
[R_CS
].selector
& 3) != 3);