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"
30 #define dprintf(fmt, ...) \
31 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
33 #define dprintf(fmt, ...) \
37 #ifdef KVM_CAP_EXT_CPUID
39 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
41 struct kvm_cpuid2
*cpuid
;
44 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
45 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
47 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
48 if (r
== 0 && cpuid
->nent
>= max
) {
56 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
64 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
66 struct kvm_cpuid2
*cpuid
;
71 if (!kvm_check_extension(env
->kvm_state
, KVM_CAP_EXT_CPUID
)) {
76 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
80 for (i
= 0; i
< cpuid
->nent
; ++i
) {
81 if (cpuid
->entries
[i
].function
== function
) {
84 ret
= cpuid
->entries
[i
].eax
;
87 ret
= cpuid
->entries
[i
].ebx
;
90 ret
= cpuid
->entries
[i
].ecx
;
93 ret
= cpuid
->entries
[i
].edx
;
94 if (function
== 0x80000001) {
95 /* On Intel, kvm returns cpuid according to the Intel spec,
96 * so add missing bits according to the AMD spec:
98 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, R_EDX
);
99 ret
|= cpuid_1_edx
& 0xdfeff7ff;
113 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
120 static void kvm_trim_features(uint32_t *features
, uint32_t supported
)
125 for (i
= 0; i
< 32; ++i
) {
127 if ((*features
& mask
) && !(supported
& mask
)) {
133 int kvm_arch_init_vcpu(CPUState
*env
)
136 struct kvm_cpuid2 cpuid
;
137 struct kvm_cpuid_entry2 entries
[100];
138 } __attribute__((packed
)) cpuid_data
;
139 uint32_t limit
, i
, j
, cpuid_i
;
142 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
144 kvm_trim_features(&env
->cpuid_features
,
145 kvm_arch_get_supported_cpuid(env
, 1, R_EDX
));
147 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
148 kvm_trim_features(&env
->cpuid_ext_features
,
149 kvm_arch_get_supported_cpuid(env
, 1, R_ECX
));
150 env
->cpuid_ext_features
|= i
;
152 kvm_trim_features(&env
->cpuid_ext2_features
,
153 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_EDX
));
154 kvm_trim_features(&env
->cpuid_ext3_features
,
155 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_ECX
));
159 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
161 for (i
= 0; i
<= limit
; i
++) {
162 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
166 /* Keep reading function 2 till all the input is received */
170 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
171 KVM_CPUID_FLAG_STATE_READ_NEXT
;
172 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
173 times
= c
->eax
& 0xff;
175 for (j
= 1; j
< times
; ++j
) {
176 c
= &cpuid_data
.entries
[cpuid_i
++];
178 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
179 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
188 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
190 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
192 if (i
== 4 && c
->eax
== 0)
194 if (i
== 0xb && !(c
->ecx
& 0xff00))
196 if (i
== 0xd && c
->eax
== 0)
199 c
= &cpuid_data
.entries
[cpuid_i
++];
205 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
209 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
211 for (i
= 0x80000000; i
<= limit
; i
++) {
212 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
216 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
219 cpuid_data
.cpuid
.nent
= cpuid_i
;
221 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
224 static int kvm_has_msr_star(CPUState
*env
)
226 static int has_msr_star
;
230 if (has_msr_star
== 0) {
231 struct kvm_msr_list msr_list
, *kvm_msr_list
;
235 /* Obtain MSR list from KVM. These are the MSRs that we must
238 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
242 kvm_msr_list
= qemu_mallocz(sizeof(msr_list
) +
243 msr_list
.nmsrs
* sizeof(msr_list
.indices
[0]));
245 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
246 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
250 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
251 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
261 if (has_msr_star
== 1)
266 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
270 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
271 * directly. In order to use vm86 mode, a TSS is needed. Since this
272 * must be part of guest physical memory, we need to allocate it. Older
273 * versions of KVM just assumed that it would be at the end of physical
274 * memory but that doesn't work with more than 4GB of memory. We simply
275 * refuse to work with those older versions of KVM. */
276 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
278 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
282 /* this address is 3 pages before the bios, and the bios should present
283 * as unavaible memory. FIXME, need to ensure the e820 map deals with
286 return kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
289 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
291 lhs
->selector
= rhs
->selector
;
292 lhs
->base
= rhs
->base
;
293 lhs
->limit
= rhs
->limit
;
305 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
307 unsigned flags
= rhs
->flags
;
308 lhs
->selector
= rhs
->selector
;
309 lhs
->base
= rhs
->base
;
310 lhs
->limit
= rhs
->limit
;
311 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
312 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
313 lhs
->dpl
= rhs
->selector
& 3;
314 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
315 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
316 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
317 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
318 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
322 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
324 lhs
->selector
= rhs
->selector
;
325 lhs
->base
= rhs
->base
;
326 lhs
->limit
= rhs
->limit
;
328 (rhs
->type
<< DESC_TYPE_SHIFT
)
329 | (rhs
->present
* DESC_P_MASK
)
330 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
331 | (rhs
->db
<< DESC_B_SHIFT
)
332 | (rhs
->s
* DESC_S_MASK
)
333 | (rhs
->l
<< DESC_L_SHIFT
)
334 | (rhs
->g
* DESC_G_MASK
)
335 | (rhs
->avl
* DESC_AVL_MASK
);
338 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
341 *kvm_reg
= *qemu_reg
;
343 *qemu_reg
= *kvm_reg
;
346 static int kvm_getput_regs(CPUState
*env
, int set
)
348 struct kvm_regs regs
;
352 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
357 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
358 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
359 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
360 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
361 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
362 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
363 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
364 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
366 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
367 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
368 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
369 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
370 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
371 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
372 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
373 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
376 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
377 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
380 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
385 static int kvm_put_fpu(CPUState
*env
)
390 memset(&fpu
, 0, sizeof fpu
);
391 fpu
.fsw
= env
->fpus
& ~(7 << 11);
392 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
394 for (i
= 0; i
< 8; ++i
)
395 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
396 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
397 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
398 fpu
.mxcsr
= env
->mxcsr
;
400 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
403 static int kvm_put_sregs(CPUState
*env
)
405 struct kvm_sregs sregs
;
407 memcpy(sregs
.interrupt_bitmap
,
408 env
->interrupt_bitmap
,
409 sizeof(sregs
.interrupt_bitmap
));
411 if ((env
->eflags
& VM_MASK
)) {
412 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
413 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
414 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
415 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
416 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
417 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
419 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
420 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
421 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
422 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
423 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
424 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
426 if (env
->cr
[0] & CR0_PE_MASK
) {
427 /* force ss cpl to cs cpl */
428 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
429 (sregs
.cs
.selector
& 3);
430 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
434 set_seg(&sregs
.tr
, &env
->tr
);
435 set_seg(&sregs
.ldt
, &env
->ldt
);
437 sregs
.idt
.limit
= env
->idt
.limit
;
438 sregs
.idt
.base
= env
->idt
.base
;
439 sregs
.gdt
.limit
= env
->gdt
.limit
;
440 sregs
.gdt
.base
= env
->gdt
.base
;
442 sregs
.cr0
= env
->cr
[0];
443 sregs
.cr2
= env
->cr
[2];
444 sregs
.cr3
= env
->cr
[3];
445 sregs
.cr4
= env
->cr
[4];
447 sregs
.cr8
= cpu_get_apic_tpr(env
);
448 sregs
.apic_base
= cpu_get_apic_base(env
);
450 sregs
.efer
= env
->efer
;
452 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
455 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
456 uint32_t index
, uint64_t value
)
458 entry
->index
= index
;
462 static int kvm_put_msrs(CPUState
*env
)
465 struct kvm_msrs info
;
466 struct kvm_msr_entry entries
[100];
468 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
471 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
472 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
473 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
474 if (kvm_has_msr_star(env
))
475 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
476 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
478 /* FIXME if lm capable */
479 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
480 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
481 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
482 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
484 msr_data
.info
.nmsrs
= n
;
486 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
491 static int kvm_get_fpu(CPUState
*env
)
496 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
500 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
503 for (i
= 0; i
< 8; ++i
)
504 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
505 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
506 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
507 env
->mxcsr
= fpu
.mxcsr
;
512 static int kvm_get_sregs(CPUState
*env
)
514 struct kvm_sregs sregs
;
518 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
522 memcpy(env
->interrupt_bitmap
,
523 sregs
.interrupt_bitmap
,
524 sizeof(sregs
.interrupt_bitmap
));
526 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
527 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
528 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
529 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
530 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
531 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
533 get_seg(&env
->tr
, &sregs
.tr
);
534 get_seg(&env
->ldt
, &sregs
.ldt
);
536 env
->idt
.limit
= sregs
.idt
.limit
;
537 env
->idt
.base
= sregs
.idt
.base
;
538 env
->gdt
.limit
= sregs
.gdt
.limit
;
539 env
->gdt
.base
= sregs
.gdt
.base
;
541 env
->cr
[0] = sregs
.cr0
;
542 env
->cr
[2] = sregs
.cr2
;
543 env
->cr
[3] = sregs
.cr3
;
544 env
->cr
[4] = sregs
.cr4
;
546 cpu_set_apic_base(env
, sregs
.apic_base
);
548 env
->efer
= sregs
.efer
;
549 //cpu_set_apic_tpr(env, sregs.cr8);
551 #define HFLAG_COPY_MASK ~( \
552 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
553 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
554 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
555 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
559 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
560 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
561 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
562 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
563 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
564 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
565 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
567 if (env
->efer
& MSR_EFER_LMA
) {
568 hflags
|= HF_LMA_MASK
;
571 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
572 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
574 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
575 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
576 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
577 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
578 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
579 (env
->eflags
& VM_MASK
) ||
580 !(hflags
& HF_CS32_MASK
)) {
581 hflags
|= HF_ADDSEG_MASK
;
583 hflags
|= ((env
->segs
[R_DS
].base
|
584 env
->segs
[R_ES
].base
|
585 env
->segs
[R_SS
].base
) != 0) <<
589 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
594 static int kvm_get_msrs(CPUState
*env
)
597 struct kvm_msrs info
;
598 struct kvm_msr_entry entries
[100];
600 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
604 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
605 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
606 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
607 if (kvm_has_msr_star(env
))
608 msrs
[n
++].index
= MSR_STAR
;
609 msrs
[n
++].index
= MSR_IA32_TSC
;
611 /* FIXME lm_capable_kernel */
612 msrs
[n
++].index
= MSR_CSTAR
;
613 msrs
[n
++].index
= MSR_KERNELGSBASE
;
614 msrs
[n
++].index
= MSR_FMASK
;
615 msrs
[n
++].index
= MSR_LSTAR
;
617 msr_data
.info
.nmsrs
= n
;
618 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
622 for (i
= 0; i
< ret
; i
++) {
623 switch (msrs
[i
].index
) {
624 case MSR_IA32_SYSENTER_CS
:
625 env
->sysenter_cs
= msrs
[i
].data
;
627 case MSR_IA32_SYSENTER_ESP
:
628 env
->sysenter_esp
= msrs
[i
].data
;
630 case MSR_IA32_SYSENTER_EIP
:
631 env
->sysenter_eip
= msrs
[i
].data
;
634 env
->star
= msrs
[i
].data
;
638 env
->cstar
= msrs
[i
].data
;
640 case MSR_KERNELGSBASE
:
641 env
->kernelgsbase
= msrs
[i
].data
;
644 env
->fmask
= msrs
[i
].data
;
647 env
->lstar
= msrs
[i
].data
;
651 env
->tsc
= msrs
[i
].data
;
659 int kvm_arch_put_registers(CPUState
*env
)
663 ret
= kvm_getput_regs(env
, 1);
667 ret
= kvm_put_fpu(env
);
671 ret
= kvm_put_sregs(env
);
675 ret
= kvm_put_msrs(env
);
679 ret
= kvm_put_mp_state(env
);
683 ret
= kvm_get_mp_state(env
);
690 int kvm_arch_get_registers(CPUState
*env
)
694 ret
= kvm_getput_regs(env
, 0);
698 ret
= kvm_get_fpu(env
);
702 ret
= kvm_get_sregs(env
);
706 ret
= kvm_get_msrs(env
);
713 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
715 /* Try to inject an interrupt if the guest can accept it */
716 if (run
->ready_for_interrupt_injection
&&
717 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
718 (env
->eflags
& IF_MASK
)) {
721 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
722 irq
= cpu_get_pic_interrupt(env
);
724 struct kvm_interrupt intr
;
727 dprintf("injected interrupt %d\n", irq
);
728 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
732 /* If we have an interrupt but the guest is not ready to receive an
733 * interrupt, request an interrupt window exit. This will
734 * cause a return to userspace as soon as the guest is ready to
735 * receive interrupts. */
736 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
737 run
->request_interrupt_window
= 1;
739 run
->request_interrupt_window
= 0;
741 dprintf("setting tpr\n");
742 run
->cr8
= cpu_get_apic_tpr(env
);
747 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
750 env
->eflags
|= IF_MASK
;
752 env
->eflags
&= ~IF_MASK
;
754 cpu_set_apic_tpr(env
, run
->cr8
);
755 cpu_set_apic_base(env
, run
->apic_base
);
760 static int kvm_handle_halt(CPUState
*env
)
762 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
763 (env
->eflags
& IF_MASK
)) &&
764 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
766 env
->exception_index
= EXCP_HLT
;
773 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
777 switch (run
->exit_reason
) {
779 dprintf("handle_hlt\n");
780 ret
= kvm_handle_halt(env
);
787 #ifdef KVM_CAP_SET_GUEST_DEBUG
788 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
790 const static uint8_t int3
= 0xcc;
792 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
793 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1))
798 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
802 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
803 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
814 static int nb_hw_breakpoint
;
816 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
820 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
821 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
822 (hw_breakpoint
[n
].len
== len
|| len
== -1))
827 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
828 target_ulong len
, int type
)
831 case GDB_BREAKPOINT_HW
:
834 case GDB_WATCHPOINT_WRITE
:
835 case GDB_WATCHPOINT_ACCESS
:
842 if (addr
& (len
- 1))
853 if (nb_hw_breakpoint
== 4)
856 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
859 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
860 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
861 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
867 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
868 target_ulong len
, int type
)
872 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
877 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
882 void kvm_arch_remove_all_hw_breakpoints(void)
884 nb_hw_breakpoint
= 0;
887 static CPUWatchpoint hw_watchpoint
;
889 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
894 if (arch_info
->exception
== 1) {
895 if (arch_info
->dr6
& (1 << 14)) {
896 if (cpu_single_env
->singlestep_enabled
)
899 for (n
= 0; n
< 4; n
++)
900 if (arch_info
->dr6
& (1 << n
))
901 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
907 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
908 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
909 hw_watchpoint
.flags
= BP_MEM_WRITE
;
913 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
914 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
915 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
919 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
923 kvm_update_guest_debug(cpu_single_env
,
924 (arch_info
->exception
== 1) ?
925 KVM_GUESTDBG_INJECT_DB
: KVM_GUESTDBG_INJECT_BP
);
930 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
932 const uint8_t type_code
[] = {
933 [GDB_BREAKPOINT_HW
] = 0x0,
934 [GDB_WATCHPOINT_WRITE
] = 0x1,
935 [GDB_WATCHPOINT_ACCESS
] = 0x3
937 const uint8_t len_code
[] = {
938 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
942 if (kvm_sw_breakpoints_active(env
))
943 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
945 if (nb_hw_breakpoint
> 0) {
946 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
947 dbg
->arch
.debugreg
[7] = 0x0600;
948 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
949 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
950 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
951 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
952 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
956 #endif /* KVM_CAP_SET_GUEST_DEBUG */