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"
28 #ifdef CONFIG_KVM_PARA
29 #include <linux/kvm_para.h>
35 #define dprintf(fmt, ...) \
36 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
38 #define dprintf(fmt, ...) \
42 #define MSR_KVM_WALL_CLOCK 0x11
43 #define MSR_KVM_SYSTEM_TIME 0x12
45 #ifdef KVM_CAP_EXT_CPUID
47 static struct kvm_cpuid2
*try_get_cpuid(KVMState
*s
, int max
)
49 struct kvm_cpuid2
*cpuid
;
52 size
= sizeof(*cpuid
) + max
* sizeof(*cpuid
->entries
);
53 cpuid
= (struct kvm_cpuid2
*)qemu_mallocz(size
);
55 r
= kvm_ioctl(s
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
56 if (r
== 0 && cpuid
->nent
>= max
) {
64 fprintf(stderr
, "KVM_GET_SUPPORTED_CPUID failed: %s\n",
72 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
74 struct kvm_cpuid2
*cpuid
;
79 if (!kvm_check_extension(env
->kvm_state
, KVM_CAP_EXT_CPUID
)) {
84 while ((cpuid
= try_get_cpuid(env
->kvm_state
, max
)) == NULL
) {
88 for (i
= 0; i
< cpuid
->nent
; ++i
) {
89 if (cpuid
->entries
[i
].function
== function
) {
92 ret
= cpuid
->entries
[i
].eax
;
95 ret
= cpuid
->entries
[i
].ebx
;
98 ret
= cpuid
->entries
[i
].ecx
;
101 ret
= cpuid
->entries
[i
].edx
;
102 if (function
== 0x80000001) {
103 /* On Intel, kvm returns cpuid according to the Intel spec,
104 * so add missing bits according to the AMD spec:
106 cpuid_1_edx
= kvm_arch_get_supported_cpuid(env
, 1, R_EDX
);
107 ret
|= cpuid_1_edx
& 0xdfeff7ff;
121 uint32_t kvm_arch_get_supported_cpuid(CPUState
*env
, uint32_t function
, int reg
)
128 static void kvm_trim_features(uint32_t *features
, uint32_t supported
)
133 for (i
= 0; i
< 32; ++i
) {
135 if ((*features
& mask
) && !(supported
& mask
)) {
141 #ifdef CONFIG_KVM_PARA
142 struct kvm_para_features
{
145 } para_features
[] = {
146 #ifdef KVM_CAP_CLOCKSOURCE
147 { KVM_CAP_CLOCKSOURCE
, KVM_FEATURE_CLOCKSOURCE
},
149 #ifdef KVM_CAP_NOP_IO_DELAY
150 { KVM_CAP_NOP_IO_DELAY
, KVM_FEATURE_NOP_IO_DELAY
},
152 #ifdef KVM_CAP_PV_MMU
153 { KVM_CAP_PV_MMU
, KVM_FEATURE_MMU_OP
},
155 #ifdef KVM_CAP_CR3_CACHE
156 { KVM_CAP_CR3_CACHE
, KVM_FEATURE_CR3_CACHE
},
161 static int get_para_features(CPUState
*env
)
165 for (i
= 0; i
< ARRAY_SIZE(para_features
) - 1; i
++) {
166 if (kvm_check_extension(env
->kvm_state
, para_features
[i
].cap
))
167 features
|= (1 << para_features
[i
].feature
);
174 int kvm_arch_init_vcpu(CPUState
*env
)
177 struct kvm_cpuid2 cpuid
;
178 struct kvm_cpuid_entry2 entries
[100];
179 } __attribute__((packed
)) cpuid_data
;
180 uint32_t limit
, i
, j
, cpuid_i
;
182 struct kvm_cpuid_entry2
*c
;
183 #ifdef KVM_CPUID_SIGNATURE
184 uint32_t signature
[3];
187 env
->mp_state
= KVM_MP_STATE_RUNNABLE
;
189 kvm_trim_features(&env
->cpuid_features
,
190 kvm_arch_get_supported_cpuid(env
, 1, R_EDX
));
192 i
= env
->cpuid_ext_features
& CPUID_EXT_HYPERVISOR
;
193 kvm_trim_features(&env
->cpuid_ext_features
,
194 kvm_arch_get_supported_cpuid(env
, 1, R_ECX
));
195 env
->cpuid_ext_features
|= i
;
197 kvm_trim_features(&env
->cpuid_ext2_features
,
198 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_EDX
));
199 kvm_trim_features(&env
->cpuid_ext3_features
,
200 kvm_arch_get_supported_cpuid(env
, 0x80000001, R_ECX
));
204 #ifdef CONFIG_KVM_PARA
205 /* Paravirtualization CPUIDs */
206 memcpy(signature
, "KVMKVMKVM\0\0\0", 12);
207 c
= &cpuid_data
.entries
[cpuid_i
++];
208 memset(c
, 0, sizeof(*c
));
209 c
->function
= KVM_CPUID_SIGNATURE
;
211 c
->ebx
= signature
[0];
212 c
->ecx
= signature
[1];
213 c
->edx
= signature
[2];
215 c
= &cpuid_data
.entries
[cpuid_i
++];
216 memset(c
, 0, sizeof(*c
));
217 c
->function
= KVM_CPUID_FEATURES
;
218 c
->eax
= env
->cpuid_kvm_features
& get_para_features(env
);
221 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
223 for (i
= 0; i
<= limit
; i
++) {
224 c
= &cpuid_data
.entries
[cpuid_i
++];
228 /* Keep reading function 2 till all the input is received */
232 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
233 KVM_CPUID_FLAG_STATE_READ_NEXT
;
234 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
235 times
= c
->eax
& 0xff;
237 for (j
= 1; j
< times
; ++j
) {
238 c
= &cpuid_data
.entries
[cpuid_i
++];
240 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
241 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
250 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
252 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
254 if (i
== 4 && c
->eax
== 0)
256 if (i
== 0xb && !(c
->ecx
& 0xff00))
258 if (i
== 0xd && c
->eax
== 0)
261 c
= &cpuid_data
.entries
[cpuid_i
++];
267 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
271 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
273 for (i
= 0x80000000; i
<= limit
; i
++) {
274 c
= &cpuid_data
.entries
[cpuid_i
++];
278 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
281 cpuid_data
.cpuid
.nent
= cpuid_i
;
283 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
286 void kvm_arch_reset_vcpu(CPUState
*env
)
288 env
->exception_injected
= -1;
289 env
->interrupt_injected
= -1;
290 env
->nmi_injected
= 0;
291 env
->nmi_pending
= 0;
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 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
343 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
344 * directly. In order to use vm86 mode, a TSS is needed. Since this
345 * must be part of guest physical memory, we need to allocate it. Older
346 * versions of KVM just assumed that it would be at the end of physical
347 * memory but that doesn't work with more than 4GB of memory. We simply
348 * refuse to work with those older versions of KVM. */
349 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
351 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
355 /* this address is 3 pages before the bios, and the bios should present
356 * as unavaible memory. FIXME, need to ensure the e820 map deals with
359 return kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
362 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
364 lhs
->selector
= rhs
->selector
;
365 lhs
->base
= rhs
->base
;
366 lhs
->limit
= rhs
->limit
;
378 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
380 unsigned flags
= rhs
->flags
;
381 lhs
->selector
= rhs
->selector
;
382 lhs
->base
= rhs
->base
;
383 lhs
->limit
= rhs
->limit
;
384 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
385 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
386 lhs
->dpl
= rhs
->selector
& 3;
387 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
388 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
389 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
390 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
391 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
395 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
397 lhs
->selector
= rhs
->selector
;
398 lhs
->base
= rhs
->base
;
399 lhs
->limit
= rhs
->limit
;
401 (rhs
->type
<< DESC_TYPE_SHIFT
)
402 | (rhs
->present
* DESC_P_MASK
)
403 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
404 | (rhs
->db
<< DESC_B_SHIFT
)
405 | (rhs
->s
* DESC_S_MASK
)
406 | (rhs
->l
<< DESC_L_SHIFT
)
407 | (rhs
->g
* DESC_G_MASK
)
408 | (rhs
->avl
* DESC_AVL_MASK
);
411 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
414 *kvm_reg
= *qemu_reg
;
416 *qemu_reg
= *kvm_reg
;
419 static int kvm_getput_regs(CPUState
*env
, int set
)
421 struct kvm_regs regs
;
425 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
430 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
431 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
432 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
433 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
434 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
435 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
436 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
437 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
439 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
440 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
441 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
442 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
443 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
444 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
445 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
446 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
449 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
450 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
453 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
458 static int kvm_put_fpu(CPUState
*env
)
463 memset(&fpu
, 0, sizeof fpu
);
464 fpu
.fsw
= env
->fpus
& ~(7 << 11);
465 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
467 for (i
= 0; i
< 8; ++i
)
468 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
469 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
470 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
471 fpu
.mxcsr
= env
->mxcsr
;
473 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
476 static int kvm_put_sregs(CPUState
*env
)
478 struct kvm_sregs sregs
;
480 memset(sregs
.interrupt_bitmap
, 0, sizeof(sregs
.interrupt_bitmap
));
481 if (env
->interrupt_injected
>= 0) {
482 sregs
.interrupt_bitmap
[env
->interrupt_injected
/ 64] |=
483 (uint64_t)1 << (env
->interrupt_injected
% 64);
486 if ((env
->eflags
& VM_MASK
)) {
487 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
488 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
489 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
490 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
491 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
492 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
494 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
495 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
496 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
497 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
498 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
499 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
501 if (env
->cr
[0] & CR0_PE_MASK
) {
502 /* force ss cpl to cs cpl */
503 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
504 (sregs
.cs
.selector
& 3);
505 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
509 set_seg(&sregs
.tr
, &env
->tr
);
510 set_seg(&sregs
.ldt
, &env
->ldt
);
512 sregs
.idt
.limit
= env
->idt
.limit
;
513 sregs
.idt
.base
= env
->idt
.base
;
514 sregs
.gdt
.limit
= env
->gdt
.limit
;
515 sregs
.gdt
.base
= env
->gdt
.base
;
517 sregs
.cr0
= env
->cr
[0];
518 sregs
.cr2
= env
->cr
[2];
519 sregs
.cr3
= env
->cr
[3];
520 sregs
.cr4
= env
->cr
[4];
522 sregs
.cr8
= cpu_get_apic_tpr(env
);
523 sregs
.apic_base
= cpu_get_apic_base(env
);
525 sregs
.efer
= env
->efer
;
527 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
530 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
531 uint32_t index
, uint64_t value
)
533 entry
->index
= index
;
537 static int kvm_put_msrs(CPUState
*env
)
540 struct kvm_msrs info
;
541 struct kvm_msr_entry entries
[100];
543 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
546 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
547 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
548 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
549 if (kvm_has_msr_star(env
))
550 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
551 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
553 /* FIXME if lm capable */
554 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
555 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
556 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
557 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
559 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_SYSTEM_TIME
, env
->system_time_msr
);
560 kvm_msr_entry_set(&msrs
[n
++], MSR_KVM_WALL_CLOCK
, env
->wall_clock_msr
);
562 msr_data
.info
.nmsrs
= n
;
564 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
569 static int kvm_get_fpu(CPUState
*env
)
574 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
578 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
581 for (i
= 0; i
< 8; ++i
)
582 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
583 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
584 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
585 env
->mxcsr
= fpu
.mxcsr
;
590 static int kvm_get_sregs(CPUState
*env
)
592 struct kvm_sregs sregs
;
596 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
600 /* There can only be one pending IRQ set in the bitmap at a time, so try
601 to find it and save its number instead (-1 for none). */
602 env
->interrupt_injected
= -1;
603 for (i
= 0; i
< ARRAY_SIZE(sregs
.interrupt_bitmap
); i
++) {
604 if (sregs
.interrupt_bitmap
[i
]) {
605 bit
= ctz64(sregs
.interrupt_bitmap
[i
]);
606 env
->interrupt_injected
= i
* 64 + bit
;
611 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
612 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
613 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
614 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
615 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
616 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
618 get_seg(&env
->tr
, &sregs
.tr
);
619 get_seg(&env
->ldt
, &sregs
.ldt
);
621 env
->idt
.limit
= sregs
.idt
.limit
;
622 env
->idt
.base
= sregs
.idt
.base
;
623 env
->gdt
.limit
= sregs
.gdt
.limit
;
624 env
->gdt
.base
= sregs
.gdt
.base
;
626 env
->cr
[0] = sregs
.cr0
;
627 env
->cr
[2] = sregs
.cr2
;
628 env
->cr
[3] = sregs
.cr3
;
629 env
->cr
[4] = sregs
.cr4
;
631 cpu_set_apic_base(env
, sregs
.apic_base
);
633 env
->efer
= sregs
.efer
;
634 //cpu_set_apic_tpr(env, sregs.cr8);
636 #define HFLAG_COPY_MASK ~( \
637 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
638 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
639 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
640 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
644 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
645 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
646 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
647 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
648 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
649 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
650 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
652 if (env
->efer
& MSR_EFER_LMA
) {
653 hflags
|= HF_LMA_MASK
;
656 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
657 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
659 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
660 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
661 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
662 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
663 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
664 (env
->eflags
& VM_MASK
) ||
665 !(hflags
& HF_CS32_MASK
)) {
666 hflags
|= HF_ADDSEG_MASK
;
668 hflags
|= ((env
->segs
[R_DS
].base
|
669 env
->segs
[R_ES
].base
|
670 env
->segs
[R_SS
].base
) != 0) <<
674 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
679 static int kvm_get_msrs(CPUState
*env
)
682 struct kvm_msrs info
;
683 struct kvm_msr_entry entries
[100];
685 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
689 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
690 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
691 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
692 if (kvm_has_msr_star(env
))
693 msrs
[n
++].index
= MSR_STAR
;
694 msrs
[n
++].index
= MSR_IA32_TSC
;
696 /* FIXME lm_capable_kernel */
697 msrs
[n
++].index
= MSR_CSTAR
;
698 msrs
[n
++].index
= MSR_KERNELGSBASE
;
699 msrs
[n
++].index
= MSR_FMASK
;
700 msrs
[n
++].index
= MSR_LSTAR
;
702 msrs
[n
++].index
= MSR_KVM_SYSTEM_TIME
;
703 msrs
[n
++].index
= MSR_KVM_WALL_CLOCK
;
705 msr_data
.info
.nmsrs
= n
;
706 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
710 for (i
= 0; i
< ret
; i
++) {
711 switch (msrs
[i
].index
) {
712 case MSR_IA32_SYSENTER_CS
:
713 env
->sysenter_cs
= msrs
[i
].data
;
715 case MSR_IA32_SYSENTER_ESP
:
716 env
->sysenter_esp
= msrs
[i
].data
;
718 case MSR_IA32_SYSENTER_EIP
:
719 env
->sysenter_eip
= msrs
[i
].data
;
722 env
->star
= msrs
[i
].data
;
726 env
->cstar
= msrs
[i
].data
;
728 case MSR_KERNELGSBASE
:
729 env
->kernelgsbase
= msrs
[i
].data
;
732 env
->fmask
= msrs
[i
].data
;
735 env
->lstar
= msrs
[i
].data
;
739 env
->tsc
= msrs
[i
].data
;
741 case MSR_KVM_SYSTEM_TIME
:
742 env
->system_time_msr
= msrs
[i
].data
;
744 case MSR_KVM_WALL_CLOCK
:
745 env
->wall_clock_msr
= msrs
[i
].data
;
753 static int kvm_put_mp_state(CPUState
*env
)
755 struct kvm_mp_state mp_state
= { .mp_state
= env
->mp_state
};
757 return kvm_vcpu_ioctl(env
, KVM_SET_MP_STATE
, &mp_state
);
760 static int kvm_get_mp_state(CPUState
*env
)
762 struct kvm_mp_state mp_state
;
765 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MP_STATE
, &mp_state
);
769 env
->mp_state
= mp_state
.mp_state
;
773 static int kvm_put_vcpu_events(CPUState
*env
)
775 #ifdef KVM_CAP_VCPU_EVENTS
776 struct kvm_vcpu_events events
;
778 if (!kvm_has_vcpu_events()) {
782 events
.exception
.injected
= (env
->exception_injected
>= 0);
783 events
.exception
.nr
= env
->exception_injected
;
784 events
.exception
.has_error_code
= env
->has_error_code
;
785 events
.exception
.error_code
= env
->error_code
;
787 events
.interrupt
.injected
= (env
->interrupt_injected
>= 0);
788 events
.interrupt
.nr
= env
->interrupt_injected
;
789 events
.interrupt
.soft
= env
->soft_interrupt
;
791 events
.nmi
.injected
= env
->nmi_injected
;
792 events
.nmi
.pending
= env
->nmi_pending
;
793 events
.nmi
.masked
= !!(env
->hflags2
& HF2_NMI_MASK
);
795 events
.sipi_vector
= env
->sipi_vector
;
797 return kvm_vcpu_ioctl(env
, KVM_SET_VCPU_EVENTS
, &events
);
803 static int kvm_get_vcpu_events(CPUState
*env
)
805 #ifdef KVM_CAP_VCPU_EVENTS
806 struct kvm_vcpu_events events
;
809 if (!kvm_has_vcpu_events()) {
813 ret
= kvm_vcpu_ioctl(env
, KVM_GET_VCPU_EVENTS
, &events
);
817 env
->exception_injected
=
818 events
.exception
.injected
? events
.exception
.nr
: -1;
819 env
->has_error_code
= events
.exception
.has_error_code
;
820 env
->error_code
= events
.exception
.error_code
;
822 env
->interrupt_injected
=
823 events
.interrupt
.injected
? events
.interrupt
.nr
: -1;
824 env
->soft_interrupt
= events
.interrupt
.soft
;
826 env
->nmi_injected
= events
.nmi
.injected
;
827 env
->nmi_pending
= events
.nmi
.pending
;
828 if (events
.nmi
.masked
) {
829 env
->hflags2
|= HF2_NMI_MASK
;
831 env
->hflags2
&= ~HF2_NMI_MASK
;
834 env
->sipi_vector
= events
.sipi_vector
;
840 int kvm_arch_put_registers(CPUState
*env
)
844 ret
= kvm_getput_regs(env
, 1);
848 ret
= kvm_put_fpu(env
);
852 ret
= kvm_put_sregs(env
);
856 ret
= kvm_put_msrs(env
);
860 ret
= kvm_put_mp_state(env
);
864 ret
= kvm_put_vcpu_events(env
);
871 int kvm_arch_get_registers(CPUState
*env
)
875 ret
= kvm_getput_regs(env
, 0);
879 ret
= kvm_get_fpu(env
);
883 ret
= kvm_get_sregs(env
);
887 ret
= kvm_get_msrs(env
);
891 ret
= kvm_get_mp_state(env
);
895 ret
= kvm_get_vcpu_events(env
);
902 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
904 /* Try to inject an interrupt if the guest can accept it */
905 if (run
->ready_for_interrupt_injection
&&
906 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
907 (env
->eflags
& IF_MASK
)) {
910 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
911 irq
= cpu_get_pic_interrupt(env
);
913 struct kvm_interrupt intr
;
916 dprintf("injected interrupt %d\n", irq
);
917 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
921 /* If we have an interrupt but the guest is not ready to receive an
922 * interrupt, request an interrupt window exit. This will
923 * cause a return to userspace as soon as the guest is ready to
924 * receive interrupts. */
925 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
926 run
->request_interrupt_window
= 1;
928 run
->request_interrupt_window
= 0;
930 dprintf("setting tpr\n");
931 run
->cr8
= cpu_get_apic_tpr(env
);
936 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
939 env
->eflags
|= IF_MASK
;
941 env
->eflags
&= ~IF_MASK
;
943 cpu_set_apic_tpr(env
, run
->cr8
);
944 cpu_set_apic_base(env
, run
->apic_base
);
949 static int kvm_handle_halt(CPUState
*env
)
951 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
952 (env
->eflags
& IF_MASK
)) &&
953 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
955 env
->exception_index
= EXCP_HLT
;
962 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
966 switch (run
->exit_reason
) {
968 dprintf("handle_hlt\n");
969 ret
= kvm_handle_halt(env
);
976 #ifdef KVM_CAP_SET_GUEST_DEBUG
977 int kvm_arch_insert_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
979 static const uint8_t int3
= 0xcc;
981 if (cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 0) ||
982 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&int3
, 1, 1))
987 int kvm_arch_remove_sw_breakpoint(CPUState
*env
, struct kvm_sw_breakpoint
*bp
)
991 if (cpu_memory_rw_debug(env
, bp
->pc
, &int3
, 1, 0) || int3
!= 0xcc ||
992 cpu_memory_rw_debug(env
, bp
->pc
, (uint8_t *)&bp
->saved_insn
, 1, 1))
1003 static int nb_hw_breakpoint
;
1005 static int find_hw_breakpoint(target_ulong addr
, int len
, int type
)
1009 for (n
= 0; n
< nb_hw_breakpoint
; n
++)
1010 if (hw_breakpoint
[n
].addr
== addr
&& hw_breakpoint
[n
].type
== type
&&
1011 (hw_breakpoint
[n
].len
== len
|| len
== -1))
1016 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1017 target_ulong len
, int type
)
1020 case GDB_BREAKPOINT_HW
:
1023 case GDB_WATCHPOINT_WRITE
:
1024 case GDB_WATCHPOINT_ACCESS
:
1031 if (addr
& (len
- 1))
1042 if (nb_hw_breakpoint
== 4)
1045 if (find_hw_breakpoint(addr
, len
, type
) >= 0)
1048 hw_breakpoint
[nb_hw_breakpoint
].addr
= addr
;
1049 hw_breakpoint
[nb_hw_breakpoint
].len
= len
;
1050 hw_breakpoint
[nb_hw_breakpoint
].type
= type
;
1056 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1057 target_ulong len
, int type
)
1061 n
= find_hw_breakpoint(addr
, (type
== GDB_BREAKPOINT_HW
) ? 1 : len
, type
);
1066 hw_breakpoint
[n
] = hw_breakpoint
[nb_hw_breakpoint
];
1071 void kvm_arch_remove_all_hw_breakpoints(void)
1073 nb_hw_breakpoint
= 0;
1076 static CPUWatchpoint hw_watchpoint
;
1078 int kvm_arch_debug(struct kvm_debug_exit_arch
*arch_info
)
1083 if (arch_info
->exception
== 1) {
1084 if (arch_info
->dr6
& (1 << 14)) {
1085 if (cpu_single_env
->singlestep_enabled
)
1088 for (n
= 0; n
< 4; n
++)
1089 if (arch_info
->dr6
& (1 << n
))
1090 switch ((arch_info
->dr7
>> (16 + n
*4)) & 0x3) {
1096 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1097 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1098 hw_watchpoint
.flags
= BP_MEM_WRITE
;
1102 cpu_single_env
->watchpoint_hit
= &hw_watchpoint
;
1103 hw_watchpoint
.vaddr
= hw_breakpoint
[n
].addr
;
1104 hw_watchpoint
.flags
= BP_MEM_ACCESS
;
1108 } else if (kvm_find_sw_breakpoint(cpu_single_env
, arch_info
->pc
))
1112 kvm_update_guest_debug(cpu_single_env
,
1113 (arch_info
->exception
== 1) ?
1114 KVM_GUESTDBG_INJECT_DB
: KVM_GUESTDBG_INJECT_BP
);
1119 void kvm_arch_update_guest_debug(CPUState
*env
, struct kvm_guest_debug
*dbg
)
1121 const uint8_t type_code
[] = {
1122 [GDB_BREAKPOINT_HW
] = 0x0,
1123 [GDB_WATCHPOINT_WRITE
] = 0x1,
1124 [GDB_WATCHPOINT_ACCESS
] = 0x3
1126 const uint8_t len_code
[] = {
1127 [1] = 0x0, [2] = 0x1, [4] = 0x3, [8] = 0x2
1131 if (kvm_sw_breakpoints_active(env
))
1132 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1134 if (nb_hw_breakpoint
> 0) {
1135 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1136 dbg
->arch
.debugreg
[7] = 0x0600;
1137 for (n
= 0; n
< nb_hw_breakpoint
; n
++) {
1138 dbg
->arch
.debugreg
[n
] = hw_breakpoint
[n
].addr
;
1139 dbg
->arch
.debugreg
[7] |= (2 << (n
* 2)) |
1140 (type_code
[hw_breakpoint
[n
].type
] << (16 + n
*4)) |
1141 (len_code
[hw_breakpoint
[n
].len
] << (18 + n
*4));
1145 #endif /* KVM_CAP_SET_GUEST_DEBUG */