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"
31 #define dprintf(fmt, ...) \
32 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
34 #define dprintf(fmt, ...) \
38 #ifdef KVM_CAP_EXT_CPUID
40 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
42 struct kvm_cpuid2
*cpuid
;
45 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
46 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
48 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
49 if (r
== 0 && cpuid
->nent
>= max
) {
57 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
65 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
67 struct kvm_cpuid2
*cpuid
;
72 if (!kvm_check_extension(env
->kvm_state
, KVM_CAP_EXT_CPUID
)) {
77 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
81 for (i
= 0; i
< cpuid
->nent
; ++i
) {
82 if (cpuid
->entries
[i
].function
== function
) {
85 ret
= cpuid
->entries
[i
].eax
;
88 ret
= cpuid
->entries
[i
].ebx
;
91 ret
= cpuid
->entries
[i
].ecx
;
94 ret
= cpuid
->entries
[i
].edx
;
95 if (function
== 0x80000001) {
96 /* On Intel, kvm returns cpuid according to the Intel spec,
97 * so add missing bits according to the AMD spec:
99 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, R_EDX
);
100 ret
|= cpuid_1_edx
& 0xdfeff7ff;
114 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
121 static void kvm_trim_features(uint32_t *features
, uint32_t supported
)
126 for (i
= 0; i
< 32; ++i
) {
128 if ((*features
& mask
) && !(supported
& mask
)) {
134 int kvm_arch_init_vcpu(CPUState
*env
)
137 struct kvm_cpuid2 cpuid
;
138 struct kvm_cpuid_entry2 entries
[100];
139 } __attribute__((packed
)) cpuid_data
;
140 uint32_t limit
, i
, j
, cpuid_i
;
143 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
145 kvm_trim_features(&env
->cpuid_features
,
146 kvm_arch_get_supported_cpuid(env
, 1, R_EDX
));
148 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
149 kvm_trim_features(&env
->cpuid_ext_features
,
150 kvm_arch_get_supported_cpuid(env
, 1, R_ECX
));
151 env
->cpuid_ext_features
|= i
;
153 kvm_trim_features(&env
->cpuid_ext2_features
,
154 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_EDX
));
155 kvm_trim_features(&env
->cpuid_ext3_features
,
156 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_ECX
));
160 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
162 for (i
= 0; i
<= limit
; i
++) {
163 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
167 /* Keep reading function 2 till all the input is received */
171 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
172 KVM_CPUID_FLAG_STATE_READ_NEXT
;
173 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
174 times
= c
->eax
& 0xff;
176 for (j
= 1; j
< times
; ++j
) {
177 c
= &cpuid_data
.entries
[cpuid_i
++];
179 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
180 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
189 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
191 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
193 if (i
== 4 && c
->eax
== 0)
195 if (i
== 0xb && !(c
->ecx
& 0xff00))
197 if (i
== 0xd && c
->eax
== 0)
200 c
= &cpuid_data
.entries
[cpuid_i
++];
206 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
210 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
212 for (i
= 0x80000000; i
<= limit
; i
++) {
213 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
217 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
220 cpuid_data
.cpuid
.nent
= cpuid_i
;
222 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
225 static int kvm_has_msr_star(CPUState
*env
)
227 static int has_msr_star
;
231 if (has_msr_star
== 0) {
232 struct kvm_msr_list msr_list
, *kvm_msr_list
;
236 /* Obtain MSR list from KVM. These are the MSRs that we must
239 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
243 /* Old kernel modules had a bug and could write beyond the provided
244 memory. Allocate at least a safe amount of 1K. */
245 kvm_msr_list
= qemu_mallocz(MAX(1024, sizeof(msr_list
) +
247 sizeof(msr_list
.indices
[0])));
249 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
250 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
254 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
255 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
265 if (has_msr_star
== 1)
270 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
274 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
275 * directly. In order to use vm86 mode, a TSS is needed. Since this
276 * must be part of guest physical memory, we need to allocate it. Older
277 * versions of KVM just assumed that it would be at the end of physical
278 * memory but that doesn't work with more than 4GB of memory. We simply
279 * refuse to work with those older versions of KVM. */
280 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
282 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
286 /* this address is 3 pages before the bios, and the bios should present
287 * as unavaible memory. FIXME, need to ensure the e820 map deals with
290 return kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
293 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
295 lhs
->selector
= rhs
->selector
;
296 lhs
->base
= rhs
->base
;
297 lhs
->limit
= rhs
->limit
;
309 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
311 unsigned flags
= rhs
->flags
;
312 lhs
->selector
= rhs
->selector
;
313 lhs
->base
= rhs
->base
;
314 lhs
->limit
= rhs
->limit
;
315 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
316 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
317 lhs
->dpl
= rhs
->selector
& 3;
318 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
319 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
320 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
321 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
322 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
326 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
328 lhs
->selector
= rhs
->selector
;
329 lhs
->base
= rhs
->base
;
330 lhs
->limit
= rhs
->limit
;
332 (rhs
->type
<< DESC_TYPE_SHIFT
)
333 | (rhs
->present
* DESC_P_MASK
)
334 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
335 | (rhs
->db
<< DESC_B_SHIFT
)
336 | (rhs
->s
* DESC_S_MASK
)
337 | (rhs
->l
<< DESC_L_SHIFT
)
338 | (rhs
->g
* DESC_G_MASK
)
339 | (rhs
->avl
* DESC_AVL_MASK
);
342 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
345 *kvm_reg
= *qemu_reg
;
347 *qemu_reg
= *kvm_reg
;
350 static int kvm_getput_regs(CPUState
*env
, int set
)
352 struct kvm_regs regs
;
356 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
361 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
362 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
363 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
364 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
365 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
366 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
367 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
368 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
370 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
371 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
372 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
373 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
374 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
375 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
376 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
377 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
380 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
381 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
384 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
389 static int kvm_put_fpu(CPUState
*env
)
394 memset(&fpu
, 0, sizeof fpu
);
395 fpu
.fsw
= env
->fpus
& ~(7 << 11);
396 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
398 for (i
= 0; i
< 8; ++i
)
399 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
400 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
401 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
402 fpu
.mxcsr
= env
->mxcsr
;
404 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
407 static int kvm_put_sregs(CPUState
*env
)
409 struct kvm_sregs sregs
;
411 memcpy(sregs
.interrupt_bitmap
,
412 env
->interrupt_bitmap
,
413 sizeof(sregs
.interrupt_bitmap
));
415 if ((env
->eflags
& VM_MASK
)) {
416 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
417 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
418 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
419 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
420 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
421 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
423 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
424 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
425 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
426 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
427 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
428 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
430 if (env
->cr
[0] & CR0_PE_MASK
) {
431 /* force ss cpl to cs cpl */
432 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
433 (sregs
.cs
.selector
& 3);
434 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
438 set_seg(&sregs
.tr
, &env
->tr
);
439 set_seg(&sregs
.ldt
, &env
->ldt
);
441 sregs
.idt
.limit
= env
->idt
.limit
;
442 sregs
.idt
.base
= env
->idt
.base
;
443 sregs
.gdt
.limit
= env
->gdt
.limit
;
444 sregs
.gdt
.base
= env
->gdt
.base
;
446 sregs
.cr0
= env
->cr
[0];
447 sregs
.cr2
= env
->cr
[2];
448 sregs
.cr3
= env
->cr
[3];
449 sregs
.cr4
= env
->cr
[4];
451 sregs
.cr8
= cpu_get_apic_tpr(env
);
452 sregs
.apic_base
= cpu_get_apic_base(env
);
454 sregs
.efer
= env
->efer
;
456 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
459 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
460 uint32_t index
, uint64_t value
)
462 entry
->index
= index
;
466 static int kvm_put_msrs(CPUState
*env
)
469 struct kvm_msrs info
;
470 struct kvm_msr_entry entries
[100];
472 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
475 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
476 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
477 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
478 if (kvm_has_msr_star(env
))
479 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
480 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
481 kvm_msr_entry_set(&msrs
[n
++], MSR_VM_HSAVE_PA
, env
->vm_hsave
);
483 /* FIXME if lm capable */
484 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
485 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
486 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
487 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
489 msr_data
.info
.nmsrs
= n
;
491 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
496 static int kvm_get_fpu(CPUState
*env
)
501 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
505 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
508 for (i
= 0; i
< 8; ++i
)
509 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
510 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
511 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
512 env
->mxcsr
= fpu
.mxcsr
;
517 static int kvm_get_sregs(CPUState
*env
)
519 struct kvm_sregs sregs
;
523 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
527 memcpy(env
->interrupt_bitmap
,
528 sregs
.interrupt_bitmap
,
529 sizeof(sregs
.interrupt_bitmap
));
531 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
532 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
533 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
534 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
535 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
536 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
538 get_seg(&env
->tr
, &sregs
.tr
);
539 get_seg(&env
->ldt
, &sregs
.ldt
);
541 env
->idt
.limit
= sregs
.idt
.limit
;
542 env
->idt
.base
= sregs
.idt
.base
;
543 env
->gdt
.limit
= sregs
.gdt
.limit
;
544 env
->gdt
.base
= sregs
.gdt
.base
;
546 env
->cr
[0] = sregs
.cr0
;
547 env
->cr
[2] = sregs
.cr2
;
548 env
->cr
[3] = sregs
.cr3
;
549 env
->cr
[4] = sregs
.cr4
;
551 cpu_set_apic_base(env
, sregs
.apic_base
);
553 env
->efer
= sregs
.efer
;
554 //cpu_set_apic_tpr(env, sregs.cr8);
556 #define HFLAG_COPY_MASK ~( \
557 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
558 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
559 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
560 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
564 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
565 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
566 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
567 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
568 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
569 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
570 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
572 if (env
->efer
& MSR_EFER_LMA
) {
573 hflags
|= HF_LMA_MASK
;
576 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
577 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
579 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
580 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
581 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
582 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
583 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
584 (env
->eflags
& VM_MASK
) ||
585 !(hflags
& HF_CS32_MASK
)) {
586 hflags
|= HF_ADDSEG_MASK
;
588 hflags
|= ((env
->segs
[R_DS
].base
|
589 env
->segs
[R_ES
].base
|
590 env
->segs
[R_SS
].base
) != 0) <<
594 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
599 static int kvm_get_msrs(CPUState
*env
)
602 struct kvm_msrs info
;
603 struct kvm_msr_entry entries
[100];
605 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
609 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
610 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
611 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
612 if (kvm_has_msr_star(env
))
613 msrs
[n
++].index
= MSR_STAR
;
614 msrs
[n
++].index
= MSR_IA32_TSC
;
615 msrs
[n
++].index
= MSR_VM_HSAVE_PA
;
617 /* FIXME lm_capable_kernel */
618 msrs
[n
++].index
= MSR_CSTAR
;
619 msrs
[n
++].index
= MSR_KERNELGSBASE
;
620 msrs
[n
++].index
= MSR_FMASK
;
621 msrs
[n
++].index
= MSR_LSTAR
;
623 msr_data
.info
.nmsrs
= n
;
624 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
628 for (i
= 0; i
< ret
; i
++) {
629 switch (msrs
[i
].index
) {
630 case MSR_IA32_SYSENTER_CS
:
631 env
->sysenter_cs
= msrs
[i
].data
;
633 case MSR_IA32_SYSENTER_ESP
:
634 env
->sysenter_esp
= msrs
[i
].data
;
636 case MSR_IA32_SYSENTER_EIP
:
637 env
->sysenter_eip
= msrs
[i
].data
;
640 env
->star
= msrs
[i
].data
;
644 env
->cstar
= msrs
[i
].data
;
646 case MSR_KERNELGSBASE
:
647 env
->kernelgsbase
= msrs
[i
].data
;
650 env
->fmask
= msrs
[i
].data
;
653 env
->lstar
= msrs
[i
].data
;
657 env
->tsc
= msrs
[i
].data
;
659 case MSR_VM_HSAVE_PA
:
660 env
->vm_hsave
= msrs
[i
].data
;
668 int kvm_arch_put_registers(CPUState
*env
)
672 ret
= kvm_getput_regs(env
, 1);
676 ret
= kvm_put_fpu(env
);
680 ret
= kvm_put_sregs(env
);
684 ret
= kvm_put_msrs(env
);
688 ret
= kvm_put_mp_state(env
);
692 ret
= kvm_get_mp_state(env
);
699 int kvm_arch_get_registers(CPUState
*env
)
703 ret
= kvm_getput_regs(env
, 0);
707 ret
= kvm_get_fpu(env
);
711 ret
= kvm_get_sregs(env
);
715 ret
= kvm_get_msrs(env
);
722 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
724 /* Try to inject an interrupt if the guest can accept it */
725 if (run
->ready_for_interrupt_injection
&&
726 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
727 (env
->eflags
& IF_MASK
)) {
730 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
731 irq
= cpu_get_pic_interrupt(env
);
733 struct kvm_interrupt intr
;
736 dprintf("injected interrupt %d\n", irq
);
737 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
741 /* If we have an interrupt but the guest is not ready to receive an
742 * interrupt, request an interrupt window exit. This will
743 * cause a return to userspace as soon as the guest is ready to
744 * receive interrupts. */
745 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
746 run
->request_interrupt_window
= 1;
748 run
->request_interrupt_window
= 0;
750 dprintf("setting tpr\n");
751 run
->cr8
= cpu_get_apic_tpr(env
);
756 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
759 env
->eflags
|= IF_MASK
;
761 env
->eflags
&= ~IF_MASK
;
763 cpu_set_apic_tpr(env
, run
->cr8
);
764 cpu_set_apic_base(env
, run
->apic_base
);
769 static int kvm_handle_halt(CPUState
*env
)
771 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
772 (env
->eflags
& IF_MASK
)) &&
773 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
775 env
->exception_index
= EXCP_HLT
;
782 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
786 switch (run
->exit_reason
) {
788 dprintf("handle_hlt\n");
789 ret
= kvm_handle_halt(env
);
796 #ifdef KVM_CAP_SET_GUEST_DEBUG
797 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
799 const static uint8_t int3
= 0xcc;
801 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
802 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1))
807 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
811 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
812 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
823 static int nb_hw_breakpoint
;
825 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
829 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
830 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
831 (hw_breakpoint
[n
].len
== len
|| len
== -1))
836 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
837 target_ulong len
, int type
)
840 case GDB_BREAKPOINT_HW
:
843 case GDB_WATCHPOINT_WRITE
:
844 case GDB_WATCHPOINT_ACCESS
:
851 if (addr
& (len
- 1))
862 if (nb_hw_breakpoint
== 4)
865 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
868 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
869 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
870 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
876 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
877 target_ulong len
, int type
)
881 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
886 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
891 void kvm_arch_remove_all_hw_breakpoints(void)
893 nb_hw_breakpoint
= 0;
896 static CPUWatchpoint hw_watchpoint
;
898 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
903 if (arch_info
->exception
== 1) {
904 if (arch_info
->dr6
& (1 << 14)) {
905 if (cpu_single_env
->singlestep_enabled
)
908 for (n
= 0; n
< 4; n
++)
909 if (arch_info
->dr6
& (1 << n
))
910 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
916 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
917 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
918 hw_watchpoint
.flags
= BP_MEM_WRITE
;
922 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
923 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
924 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
928 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
932 kvm_update_guest_debug(cpu_single_env
,
933 (arch_info
->exception
== 1) ?
934 KVM_GUESTDBG_INJECT_DB
: KVM_GUESTDBG_INJECT_BP
);
939 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
941 const uint8_t type_code
[] = {
942 [GDB_BREAKPOINT_HW
] = 0x0,
943 [GDB_WATCHPOINT_WRITE
] = 0x1,
944 [GDB_WATCHPOINT_ACCESS
] = 0x3
946 const uint8_t len_code
[] = {
947 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
951 if (kvm_sw_breakpoints_active(env
))
952 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
954 if (nb_hw_breakpoint
> 0) {
955 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
956 dbg
->arch
.debugreg
[7] = 0x0600;
957 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
958 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
959 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
960 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
961 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
965 #endif /* KVM_CAP_SET_GUEST_DEBUG */
968 #include "qemu-kvm-x86.c"