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"
29 #define dprintf(fmt, ...) \
30 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
32 #define dprintf(fmt, ...) \
36 int kvm_arch_init_vcpu(CPUState
*env
)
39 struct kvm_cpuid2 cpuid
;
40 struct kvm_cpuid_entry2 entries
[100];
41 } __attribute__((packed
)) cpuid_data
;
42 uint32_t limit
, i
, j
, cpuid_i
;
47 cpu_x86_cpuid(env
, 0, 0, &limit
, &unused
, &unused
, &unused
);
49 for (i
= 0; i
<= limit
; i
++) {
50 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
54 /* Keep reading function 2 till all the input is received */
58 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
|
59 KVM_CPUID_FLAG_STATE_READ_NEXT
;
60 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
61 times
= c
->eax
& 0xff;
63 for (j
= 1; j
< times
; ++j
) {
64 c
= &cpuid_data
.entries
[cpuid_i
++];
66 c
->flags
= KVM_CPUID_FLAG_STATEFUL_FUNC
;
67 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
76 c
->flags
= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
78 cpu_x86_cpuid(env
, i
, j
, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
80 if (i
== 4 && c
->eax
== 0)
82 if (i
== 0xb && !(c
->ecx
& 0xff00))
84 if (i
== 0xd && c
->eax
== 0)
87 c
= &cpuid_data
.entries
[cpuid_i
++];
93 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
97 cpu_x86_cpuid(env
, 0x80000000, 0, &limit
, &unused
, &unused
, &unused
);
99 for (i
= 0x80000000; i
<= limit
; i
++) {
100 struct kvm_cpuid_entry2
*c
= &cpuid_data
.entries
[cpuid_i
++];
104 cpu_x86_cpuid(env
, i
, 0, &c
->eax
, &c
->ebx
, &c
->ecx
, &c
->edx
);
107 cpuid_data
.cpuid
.nent
= cpuid_i
;
109 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID2
, &cpuid_data
);
112 static int kvm_has_msr_star(CPUState
*env
)
114 static int has_msr_star
;
118 if (has_msr_star
== 0) {
119 struct kvm_msr_list msr_list
, *kvm_msr_list
;
123 /* Obtain MSR list from KVM. These are the MSRs that we must
126 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
130 kvm_msr_list
= qemu_mallocz(sizeof(msr_list
) +
131 msr_list
.nmsrs
* sizeof(msr_list
.indices
[0]));
133 kvm_msr_list
->nmsrs
= msr_list
.nmsrs
;
134 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
138 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
139 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
149 if (has_msr_star
== 1)
154 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
158 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
159 * directly. In order to use vm86 mode, a TSS is needed. Since this
160 * must be part of guest physical memory, we need to allocate it. Older
161 * versions of KVM just assumed that it would be at the end of physical
162 * memory but that doesn't work with more than 4GB of memory. We simply
163 * refuse to work with those older versions of KVM. */
164 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_SET_TSS_ADDR
);
166 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
170 /* this address is 3 pages before the bios, and the bios should present
171 * as unavaible memory. FIXME, need to ensure the e820 map deals with
174 return kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, 0xfffbd000);
177 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
179 lhs
->selector
= rhs
->selector
;
180 lhs
->base
= rhs
->base
;
181 lhs
->limit
= rhs
->limit
;
193 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
195 unsigned flags
= rhs
->flags
;
196 lhs
->selector
= rhs
->selector
;
197 lhs
->base
= rhs
->base
;
198 lhs
->limit
= rhs
->limit
;
199 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
200 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
201 lhs
->dpl
= rhs
->selector
& 3;
202 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
203 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
204 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
205 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
206 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
210 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
212 lhs
->selector
= rhs
->selector
;
213 lhs
->base
= rhs
->base
;
214 lhs
->limit
= rhs
->limit
;
216 (rhs
->type
<< DESC_TYPE_SHIFT
)
217 | (rhs
->present
* DESC_P_MASK
)
218 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
219 | (rhs
->db
<< DESC_B_SHIFT
)
220 | (rhs
->s
* DESC_S_MASK
)
221 | (rhs
->l
<< DESC_L_SHIFT
)
222 | (rhs
->g
* DESC_G_MASK
)
223 | (rhs
->avl
* DESC_AVL_MASK
);
226 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
229 *kvm_reg
= *qemu_reg
;
231 *qemu_reg
= *kvm_reg
;
234 static int kvm_getput_regs(CPUState
*env
, int set
)
236 struct kvm_regs regs
;
240 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
245 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
246 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
247 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
248 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
249 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
250 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
251 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
252 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
254 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
255 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
256 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
257 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
258 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
259 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
260 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
261 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
264 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
265 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
268 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
273 static int kvm_put_fpu(CPUState
*env
)
278 memset(&fpu
, 0, sizeof fpu
);
279 fpu
.fsw
= env
->fpus
& ~(7 << 11);
280 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
282 for (i
= 0; i
< 8; ++i
)
283 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
284 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
285 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
286 fpu
.mxcsr
= env
->mxcsr
;
288 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
291 static int kvm_put_sregs(CPUState
*env
)
293 struct kvm_sregs sregs
;
295 memcpy(sregs
.interrupt_bitmap
,
296 env
->interrupt_bitmap
,
297 sizeof(sregs
.interrupt_bitmap
));
299 if ((env
->eflags
& VM_MASK
)) {
300 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
301 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
302 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
303 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
304 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
305 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
307 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
308 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
309 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
310 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
311 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
312 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
314 if (env
->cr
[0] & CR0_PE_MASK
) {
315 /* force ss cpl to cs cpl */
316 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
317 (sregs
.cs
.selector
& 3);
318 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
322 set_seg(&sregs
.tr
, &env
->tr
);
323 set_seg(&sregs
.ldt
, &env
->ldt
);
325 sregs
.idt
.limit
= env
->idt
.limit
;
326 sregs
.idt
.base
= env
->idt
.base
;
327 sregs
.gdt
.limit
= env
->gdt
.limit
;
328 sregs
.gdt
.base
= env
->gdt
.base
;
330 sregs
.cr0
= env
->cr
[0];
331 sregs
.cr2
= env
->cr
[2];
332 sregs
.cr3
= env
->cr
[3];
333 sregs
.cr4
= env
->cr
[4];
335 sregs
.cr8
= cpu_get_apic_tpr(env
);
336 sregs
.apic_base
= cpu_get_apic_base(env
);
338 sregs
.efer
= env
->efer
;
340 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
343 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
344 uint32_t index
, uint64_t value
)
346 entry
->index
= index
;
350 static int kvm_put_msrs(CPUState
*env
)
353 struct kvm_msrs info
;
354 struct kvm_msr_entry entries
[100];
356 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
359 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
360 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
361 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
362 if (kvm_has_msr_star(env
))
363 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
364 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
366 /* FIXME if lm capable */
367 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
368 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
369 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
370 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
372 msr_data
.info
.nmsrs
= n
;
374 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
379 static int kvm_get_fpu(CPUState
*env
)
384 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
388 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
391 for (i
= 0; i
< 8; ++i
)
392 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
393 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
394 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
395 env
->mxcsr
= fpu
.mxcsr
;
400 static int kvm_get_sregs(CPUState
*env
)
402 struct kvm_sregs sregs
;
406 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
410 memcpy(env
->interrupt_bitmap
,
411 sregs
.interrupt_bitmap
,
412 sizeof(sregs
.interrupt_bitmap
));
414 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
415 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
416 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
417 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
418 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
419 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
421 get_seg(&env
->tr
, &sregs
.tr
);
422 get_seg(&env
->ldt
, &sregs
.ldt
);
424 env
->idt
.limit
= sregs
.idt
.limit
;
425 env
->idt
.base
= sregs
.idt
.base
;
426 env
->gdt
.limit
= sregs
.gdt
.limit
;
427 env
->gdt
.base
= sregs
.gdt
.base
;
429 env
->cr
[0] = sregs
.cr0
;
430 env
->cr
[2] = sregs
.cr2
;
431 env
->cr
[3] = sregs
.cr3
;
432 env
->cr
[4] = sregs
.cr4
;
434 cpu_set_apic_base(env
, sregs
.apic_base
);
436 env
->efer
= sregs
.efer
;
437 //cpu_set_apic_tpr(env, sregs.cr8);
439 #define HFLAG_COPY_MASK ~( \
440 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
441 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
442 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
443 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
447 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
448 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
449 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
450 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
451 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
452 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
453 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
455 if (env
->efer
& MSR_EFER_LMA
) {
456 hflags
|= HF_LMA_MASK
;
459 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
460 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
462 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
463 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
464 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
465 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
466 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
467 (env
->eflags
& VM_MASK
) ||
468 !(hflags
& HF_CS32_MASK
)) {
469 hflags
|= HF_ADDSEG_MASK
;
471 hflags
|= ((env
->segs
[R_DS
].base
|
472 env
->segs
[R_ES
].base
|
473 env
->segs
[R_SS
].base
) != 0) <<
477 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
482 static int kvm_get_msrs(CPUState
*env
)
485 struct kvm_msrs info
;
486 struct kvm_msr_entry entries
[100];
488 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
492 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
493 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
494 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
495 if (kvm_has_msr_star(env
))
496 msrs
[n
++].index
= MSR_STAR
;
497 msrs
[n
++].index
= MSR_IA32_TSC
;
499 /* FIXME lm_capable_kernel */
500 msrs
[n
++].index
= MSR_CSTAR
;
501 msrs
[n
++].index
= MSR_KERNELGSBASE
;
502 msrs
[n
++].index
= MSR_FMASK
;
503 msrs
[n
++].index
= MSR_LSTAR
;
505 msr_data
.info
.nmsrs
= n
;
506 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
510 for (i
= 0; i
< ret
; i
++) {
511 switch (msrs
[i
].index
) {
512 case MSR_IA32_SYSENTER_CS
:
513 env
->sysenter_cs
= msrs
[i
].data
;
515 case MSR_IA32_SYSENTER_ESP
:
516 env
->sysenter_esp
= msrs
[i
].data
;
518 case MSR_IA32_SYSENTER_EIP
:
519 env
->sysenter_eip
= msrs
[i
].data
;
522 env
->star
= msrs
[i
].data
;
526 env
->cstar
= msrs
[i
].data
;
528 case MSR_KERNELGSBASE
:
529 env
->kernelgsbase
= msrs
[i
].data
;
532 env
->fmask
= msrs
[i
].data
;
535 env
->lstar
= msrs
[i
].data
;
539 env
->tsc
= msrs
[i
].data
;
547 int kvm_arch_put_registers(CPUState
*env
)
551 ret
= kvm_getput_regs(env
, 1);
555 ret
= kvm_put_fpu(env
);
559 ret
= kvm_put_sregs(env
);
563 ret
= kvm_put_msrs(env
);
570 int kvm_arch_get_registers(CPUState
*env
)
574 ret
= kvm_getput_regs(env
, 0);
578 ret
= kvm_get_fpu(env
);
582 ret
= kvm_get_sregs(env
);
586 ret
= kvm_get_msrs(env
);
593 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
595 /* Try to inject an interrupt if the guest can accept it */
596 if (run
->ready_for_interrupt_injection
&&
597 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
598 (env
->eflags
& IF_MASK
)) {
601 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
602 irq
= cpu_get_pic_interrupt(env
);
604 struct kvm_interrupt intr
;
607 dprintf("injected interrupt %d\n", irq
);
608 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
612 /* If we have an interrupt but the guest is not ready to receive an
613 * interrupt, request an interrupt window exit. This will
614 * cause a return to userspace as soon as the guest is ready to
615 * receive interrupts. */
616 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
617 run
->request_interrupt_window
= 1;
619 run
->request_interrupt_window
= 0;
621 dprintf("setting tpr\n");
622 run
->cr8
= cpu_get_apic_tpr(env
);
627 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
630 env
->eflags
|= IF_MASK
;
632 env
->eflags
&= ~IF_MASK
;
634 cpu_set_apic_tpr(env
, run
->cr8
);
635 cpu_set_apic_base(env
, run
->apic_base
);
640 static int kvm_handle_halt(CPUState
*env
)
642 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
643 (env
->eflags
& IF_MASK
)) &&
644 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
646 env
->exception_index
= EXCP_HLT
;
653 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
657 switch (run
->exit_reason
) {
659 dprintf("handle_hlt\n");
660 ret
= kvm_handle_halt(env
);