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_cpuid cpuid
;
40 struct kvm_cpuid_entry entries
[100];
41 } __attribute__((packed
)) cpuid_data
;
42 int limit
, i
, cpuid_i
;
43 uint32_t eax
, ebx
, ecx
, edx
;
47 cpu_x86_cpuid(env
, 0, &eax
, &ebx
, &ecx
, &edx
);
50 for (i
= 0; i
<= limit
; i
++) {
51 struct kvm_cpuid_entry
*c
= &cpuid_data
.entries
[cpuid_i
++];
53 cpu_x86_cpuid(env
, i
, &eax
, &ebx
, &ecx
, &edx
);
61 cpu_x86_cpuid(env
, 0x80000000, &eax
, &ebx
, &ecx
, &edx
);
64 for (i
= 0x80000000; i
<= limit
; i
++) {
65 struct kvm_cpuid_entry
*c
= &cpuid_data
.entries
[cpuid_i
++];
67 cpu_x86_cpuid(env
, i
, &eax
, &ebx
, &ecx
, &edx
);
75 cpuid_data
.cpuid
.nent
= cpuid_i
;
77 return kvm_vcpu_ioctl(env
, KVM_SET_CPUID
, &cpuid_data
);
80 static int kvm_has_msr_star(CPUState
*env
)
82 static int has_msr_star
;
86 if (has_msr_star
== 0) {
87 struct kvm_msr_list msr_list
, *kvm_msr_list
;
91 /* Obtain MSR list from KVM. These are the MSRs that we must
93 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, &msr_list
);
98 kvm_msr_list
= qemu_mallocz(sizeof(msr_list
) +
99 msr_list
.nmsrs
* sizeof(msr_list
.indices
[0]));
100 if (kvm_msr_list
== NULL
)
103 ret
= kvm_ioctl(env
->kvm_state
, KVM_GET_MSR_INDEX_LIST
, kvm_msr_list
);
107 for (i
= 0; i
< kvm_msr_list
->nmsrs
; i
++) {
108 if (kvm_msr_list
->indices
[i
] == MSR_STAR
) {
118 if (has_msr_star
== 1)
123 int kvm_arch_init(KVMState
*s
, int smp_cpus
)
127 /* create vm86 tss. KVM uses vm86 mode to emulate 16-bit code
128 * directly. In order to use vm86 mode, a TSS is needed. Since this
129 * must be part of guest physical memory, we need to allocate it. Older
130 * versions of KVM just assumed that it would be at the end of physical
131 * memory but that doesn't work with more than 4GB of memory. We simply
132 * refuse to work with those older versions of KVM. */
133 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, (void *)KVM_CAP_SET_TSS_ADDR
);
135 fprintf(stderr
, "kvm does not support KVM_CAP_SET_TSS_ADDR\n");
139 /* this address is 3 pages before the bios, and the bios should present
140 * as unavaible memory. FIXME, need to ensure the e820 map deals with
143 return kvm_vm_ioctl(s
, KVM_SET_TSS_ADDR
, (void *)0xfffbd000);
146 static void set_v8086_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
148 lhs
->selector
= rhs
->selector
;
149 lhs
->base
= rhs
->base
;
150 lhs
->limit
= rhs
->limit
;
162 static void set_seg(struct kvm_segment
*lhs
, const SegmentCache
*rhs
)
164 unsigned flags
= rhs
->flags
;
165 lhs
->selector
= rhs
->selector
;
166 lhs
->base
= rhs
->base
;
167 lhs
->limit
= rhs
->limit
;
168 lhs
->type
= (flags
>> DESC_TYPE_SHIFT
) & 15;
169 lhs
->present
= (flags
& DESC_P_MASK
) != 0;
170 lhs
->dpl
= rhs
->selector
& 3;
171 lhs
->db
= (flags
>> DESC_B_SHIFT
) & 1;
172 lhs
->s
= (flags
& DESC_S_MASK
) != 0;
173 lhs
->l
= (flags
>> DESC_L_SHIFT
) & 1;
174 lhs
->g
= (flags
& DESC_G_MASK
) != 0;
175 lhs
->avl
= (flags
& DESC_AVL_MASK
) != 0;
179 static void get_seg(SegmentCache
*lhs
, const struct kvm_segment
*rhs
)
181 lhs
->selector
= rhs
->selector
;
182 lhs
->base
= rhs
->base
;
183 lhs
->limit
= rhs
->limit
;
185 (rhs
->type
<< DESC_TYPE_SHIFT
)
186 | (rhs
->present
* DESC_P_MASK
)
187 | (rhs
->dpl
<< DESC_DPL_SHIFT
)
188 | (rhs
->db
<< DESC_B_SHIFT
)
189 | (rhs
->s
* DESC_S_MASK
)
190 | (rhs
->l
<< DESC_L_SHIFT
)
191 | (rhs
->g
* DESC_G_MASK
)
192 | (rhs
->avl
* DESC_AVL_MASK
);
195 static void kvm_getput_reg(__u64
*kvm_reg
, target_ulong
*qemu_reg
, int set
)
198 *kvm_reg
= *qemu_reg
;
200 *qemu_reg
= *kvm_reg
;
203 static int kvm_getput_regs(CPUState
*env
, int set
)
205 struct kvm_regs regs
;
209 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
214 kvm_getput_reg(®s
.rax
, &env
->regs
[R_EAX
], set
);
215 kvm_getput_reg(®s
.rbx
, &env
->regs
[R_EBX
], set
);
216 kvm_getput_reg(®s
.rcx
, &env
->regs
[R_ECX
], set
);
217 kvm_getput_reg(®s
.rdx
, &env
->regs
[R_EDX
], set
);
218 kvm_getput_reg(®s
.rsi
, &env
->regs
[R_ESI
], set
);
219 kvm_getput_reg(®s
.rdi
, &env
->regs
[R_EDI
], set
);
220 kvm_getput_reg(®s
.rsp
, &env
->regs
[R_ESP
], set
);
221 kvm_getput_reg(®s
.rbp
, &env
->regs
[R_EBP
], set
);
223 kvm_getput_reg(®s
.r8
, &env
->regs
[8], set
);
224 kvm_getput_reg(®s
.r9
, &env
->regs
[9], set
);
225 kvm_getput_reg(®s
.r10
, &env
->regs
[10], set
);
226 kvm_getput_reg(®s
.r11
, &env
->regs
[11], set
);
227 kvm_getput_reg(®s
.r12
, &env
->regs
[12], set
);
228 kvm_getput_reg(®s
.r13
, &env
->regs
[13], set
);
229 kvm_getput_reg(®s
.r14
, &env
->regs
[14], set
);
230 kvm_getput_reg(®s
.r15
, &env
->regs
[15], set
);
233 kvm_getput_reg(®s
.rflags
, &env
->eflags
, set
);
234 kvm_getput_reg(®s
.rip
, &env
->eip
, set
);
237 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
242 static int kvm_put_fpu(CPUState
*env
)
247 memset(&fpu
, 0, sizeof fpu
);
248 fpu
.fsw
= env
->fpus
& ~(7 << 11);
249 fpu
.fsw
|= (env
->fpstt
& 7) << 11;
251 for (i
= 0; i
< 8; ++i
)
252 fpu
.ftwx
|= (!env
->fptags
[i
]) << i
;
253 memcpy(fpu
.fpr
, env
->fpregs
, sizeof env
->fpregs
);
254 memcpy(fpu
.xmm
, env
->xmm_regs
, sizeof env
->xmm_regs
);
255 fpu
.mxcsr
= env
->mxcsr
;
257 return kvm_vcpu_ioctl(env
, KVM_SET_FPU
, &fpu
);
260 static int kvm_put_sregs(CPUState
*env
)
262 struct kvm_sregs sregs
;
264 memcpy(sregs
.interrupt_bitmap
,
265 env
->interrupt_bitmap
,
266 sizeof(sregs
.interrupt_bitmap
));
268 if ((env
->eflags
& VM_MASK
)) {
269 set_v8086_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
270 set_v8086_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
271 set_v8086_seg(&sregs
.es
, &env
->segs
[R_ES
]);
272 set_v8086_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
273 set_v8086_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
274 set_v8086_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
276 set_seg(&sregs
.cs
, &env
->segs
[R_CS
]);
277 set_seg(&sregs
.ds
, &env
->segs
[R_DS
]);
278 set_seg(&sregs
.es
, &env
->segs
[R_ES
]);
279 set_seg(&sregs
.fs
, &env
->segs
[R_FS
]);
280 set_seg(&sregs
.gs
, &env
->segs
[R_GS
]);
281 set_seg(&sregs
.ss
, &env
->segs
[R_SS
]);
283 if (env
->cr
[0] & CR0_PE_MASK
) {
284 /* force ss cpl to cs cpl */
285 sregs
.ss
.selector
= (sregs
.ss
.selector
& ~3) |
286 (sregs
.cs
.selector
& 3);
287 sregs
.ss
.dpl
= sregs
.ss
.selector
& 3;
291 set_seg(&sregs
.tr
, &env
->tr
);
292 set_seg(&sregs
.ldt
, &env
->ldt
);
294 sregs
.idt
.limit
= env
->idt
.limit
;
295 sregs
.idt
.base
= env
->idt
.base
;
296 sregs
.gdt
.limit
= env
->gdt
.limit
;
297 sregs
.gdt
.base
= env
->gdt
.base
;
299 sregs
.cr0
= env
->cr
[0];
300 sregs
.cr2
= env
->cr
[2];
301 sregs
.cr3
= env
->cr
[3];
302 sregs
.cr4
= env
->cr
[4];
304 sregs
.cr8
= cpu_get_apic_tpr(env
);
305 sregs
.apic_base
= cpu_get_apic_base(env
);
307 sregs
.efer
= env
->efer
;
309 return kvm_vcpu_ioctl(env
, KVM_SET_SREGS
, &sregs
);
312 static void kvm_msr_entry_set(struct kvm_msr_entry
*entry
,
313 uint32_t index
, uint64_t value
)
315 entry
->index
= index
;
319 static int kvm_put_msrs(CPUState
*env
)
322 struct kvm_msrs info
;
323 struct kvm_msr_entry entries
[100];
325 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
328 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_CS
, env
->sysenter_cs
);
329 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_ESP
, env
->sysenter_esp
);
330 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_SYSENTER_EIP
, env
->sysenter_eip
);
331 if (kvm_has_msr_star(env
))
332 kvm_msr_entry_set(&msrs
[n
++], MSR_STAR
, env
->star
);
333 kvm_msr_entry_set(&msrs
[n
++], MSR_IA32_TSC
, env
->tsc
);
335 /* FIXME if lm capable */
336 kvm_msr_entry_set(&msrs
[n
++], MSR_CSTAR
, env
->cstar
);
337 kvm_msr_entry_set(&msrs
[n
++], MSR_KERNELGSBASE
, env
->kernelgsbase
);
338 kvm_msr_entry_set(&msrs
[n
++], MSR_FMASK
, env
->fmask
);
339 kvm_msr_entry_set(&msrs
[n
++], MSR_LSTAR
, env
->lstar
);
341 msr_data
.info
.nmsrs
= n
;
343 return kvm_vcpu_ioctl(env
, KVM_SET_MSRS
, &msr_data
);
348 static int kvm_get_fpu(CPUState
*env
)
353 ret
= kvm_vcpu_ioctl(env
, KVM_GET_FPU
, &fpu
);
357 env
->fpstt
= (fpu
.fsw
>> 11) & 7;
360 for (i
= 0; i
< 8; ++i
)
361 env
->fptags
[i
] = !((fpu
.ftwx
>> i
) & 1);
362 memcpy(env
->fpregs
, fpu
.fpr
, sizeof env
->fpregs
);
363 memcpy(env
->xmm_regs
, fpu
.xmm
, sizeof env
->xmm_regs
);
364 env
->mxcsr
= fpu
.mxcsr
;
369 static int kvm_get_sregs(CPUState
*env
)
371 struct kvm_sregs sregs
;
375 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
379 memcpy(env
->interrupt_bitmap
,
380 sregs
.interrupt_bitmap
,
381 sizeof(sregs
.interrupt_bitmap
));
383 get_seg(&env
->segs
[R_CS
], &sregs
.cs
);
384 get_seg(&env
->segs
[R_DS
], &sregs
.ds
);
385 get_seg(&env
->segs
[R_ES
], &sregs
.es
);
386 get_seg(&env
->segs
[R_FS
], &sregs
.fs
);
387 get_seg(&env
->segs
[R_GS
], &sregs
.gs
);
388 get_seg(&env
->segs
[R_SS
], &sregs
.ss
);
390 get_seg(&env
->tr
, &sregs
.tr
);
391 get_seg(&env
->ldt
, &sregs
.ldt
);
393 env
->idt
.limit
= sregs
.idt
.limit
;
394 env
->idt
.base
= sregs
.idt
.base
;
395 env
->gdt
.limit
= sregs
.gdt
.limit
;
396 env
->gdt
.base
= sregs
.gdt
.base
;
398 env
->cr
[0] = sregs
.cr0
;
399 env
->cr
[2] = sregs
.cr2
;
400 env
->cr
[3] = sregs
.cr3
;
401 env
->cr
[4] = sregs
.cr4
;
403 cpu_set_apic_base(env
, sregs
.apic_base
);
405 env
->efer
= sregs
.efer
;
406 //cpu_set_apic_tpr(env, sregs.cr8);
408 #define HFLAG_COPY_MASK ~( \
409 HF_CPL_MASK | HF_PE_MASK | HF_MP_MASK | HF_EM_MASK | \
410 HF_TS_MASK | HF_TF_MASK | HF_VM_MASK | HF_IOPL_MASK | \
411 HF_OSFXSR_MASK | HF_LMA_MASK | HF_CS32_MASK | \
412 HF_SS32_MASK | HF_CS64_MASK | HF_ADDSEG_MASK)
416 hflags
= (env
->segs
[R_CS
].flags
>> DESC_DPL_SHIFT
) & HF_CPL_MASK
;
417 hflags
|= (env
->cr
[0] & CR0_PE_MASK
) << (HF_PE_SHIFT
- CR0_PE_SHIFT
);
418 hflags
|= (env
->cr
[0] << (HF_MP_SHIFT
- CR0_MP_SHIFT
)) &
419 (HF_MP_MASK
| HF_EM_MASK
| HF_TS_MASK
);
420 hflags
|= (env
->eflags
& (HF_TF_MASK
| HF_VM_MASK
| HF_IOPL_MASK
));
421 hflags
|= (env
->cr
[4] & CR4_OSFXSR_MASK
) <<
422 (HF_OSFXSR_SHIFT
- CR4_OSFXSR_SHIFT
);
424 if (env
->efer
& MSR_EFER_LMA
) {
425 hflags
|= HF_LMA_MASK
;
428 if ((hflags
& HF_LMA_MASK
) && (env
->segs
[R_CS
].flags
& DESC_L_MASK
)) {
429 hflags
|= HF_CS32_MASK
| HF_SS32_MASK
| HF_CS64_MASK
;
431 hflags
|= (env
->segs
[R_CS
].flags
& DESC_B_MASK
) >>
432 (DESC_B_SHIFT
- HF_CS32_SHIFT
);
433 hflags
|= (env
->segs
[R_SS
].flags
& DESC_B_MASK
) >>
434 (DESC_B_SHIFT
- HF_SS32_SHIFT
);
435 if (!(env
->cr
[0] & CR0_PE_MASK
) ||
436 (env
->eflags
& VM_MASK
) ||
437 !(hflags
& HF_CS32_MASK
)) {
438 hflags
|= HF_ADDSEG_MASK
;
440 hflags
|= ((env
->segs
[R_DS
].base
|
441 env
->segs
[R_ES
].base
|
442 env
->segs
[R_SS
].base
) != 0) <<
446 env
->hflags
= (env
->hflags
& HFLAG_COPY_MASK
) | hflags
;
447 env
->cc_src
= env
->eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
448 env
->df
= 1 - (2 * ((env
->eflags
>> 10) & 1));
449 env
->cc_op
= CC_OP_EFLAGS
;
450 env
->eflags
&= ~(DF_MASK
| CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
455 static int kvm_get_msrs(CPUState
*env
)
458 struct kvm_msrs info
;
459 struct kvm_msr_entry entries
[100];
461 struct kvm_msr_entry
*msrs
= msr_data
.entries
;
465 msrs
[n
++].index
= MSR_IA32_SYSENTER_CS
;
466 msrs
[n
++].index
= MSR_IA32_SYSENTER_ESP
;
467 msrs
[n
++].index
= MSR_IA32_SYSENTER_EIP
;
468 if (kvm_has_msr_star(env
))
469 msrs
[n
++].index
= MSR_STAR
;
470 msrs
[n
++].index
= MSR_IA32_TSC
;
472 /* FIXME lm_capable_kernel */
473 msrs
[n
++].index
= MSR_CSTAR
;
474 msrs
[n
++].index
= MSR_KERNELGSBASE
;
475 msrs
[n
++].index
= MSR_FMASK
;
476 msrs
[n
++].index
= MSR_LSTAR
;
478 msr_data
.info
.nmsrs
= n
;
479 ret
= kvm_vcpu_ioctl(env
, KVM_GET_MSRS
, &msr_data
);
483 for (i
= 0; i
< ret
; i
++) {
484 switch (msrs
[i
].index
) {
485 case MSR_IA32_SYSENTER_CS
:
486 env
->sysenter_cs
= msrs
[i
].data
;
488 case MSR_IA32_SYSENTER_ESP
:
489 env
->sysenter_esp
= msrs
[i
].data
;
491 case MSR_IA32_SYSENTER_EIP
:
492 env
->sysenter_eip
= msrs
[i
].data
;
495 env
->star
= msrs
[i
].data
;
499 env
->cstar
= msrs
[i
].data
;
501 case MSR_KERNELGSBASE
:
502 env
->kernelgsbase
= msrs
[i
].data
;
505 env
->fmask
= msrs
[i
].data
;
508 env
->lstar
= msrs
[i
].data
;
512 env
->tsc
= msrs
[i
].data
;
520 int kvm_arch_put_registers(CPUState
*env
)
524 ret
= kvm_getput_regs(env
, 1);
528 ret
= kvm_put_fpu(env
);
532 ret
= kvm_put_sregs(env
);
536 ret
= kvm_put_msrs(env
);
543 int kvm_arch_get_registers(CPUState
*env
)
547 ret
= kvm_getput_regs(env
, 0);
551 ret
= kvm_get_fpu(env
);
555 ret
= kvm_get_sregs(env
);
559 ret
= kvm_get_msrs(env
);
566 int kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
568 /* Try to inject an interrupt if the guest can accept it */
569 if (run
->ready_for_interrupt_injection
&&
570 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
571 (env
->eflags
& IF_MASK
)) {
574 env
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
575 irq
= cpu_get_pic_interrupt(env
);
577 struct kvm_interrupt intr
;
580 dprintf("injected interrupt %d\n", irq
);
581 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &intr
);
585 /* If we have an interrupt but the guest is not ready to receive an
586 * interrupt, request an interrupt window exit. This will
587 * cause a return to userspace as soon as the guest is ready to
588 * receive interrupts. */
589 if ((env
->interrupt_request
& CPU_INTERRUPT_HARD
))
590 run
->request_interrupt_window
= 1;
592 run
->request_interrupt_window
= 0;
594 dprintf("setting tpr\n");
595 run
->cr8
= cpu_get_apic_tpr(env
);
600 int kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
603 env
->eflags
|= IF_MASK
;
605 env
->eflags
&= ~IF_MASK
;
607 cpu_set_apic_tpr(env
, run
->cr8
);
608 cpu_set_apic_base(env
, run
->apic_base
);
613 static int kvm_handle_halt(CPUState
*env
)
615 if (!((env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
616 (env
->eflags
& IF_MASK
)) &&
617 !(env
->interrupt_request
& CPU_INTERRUPT_NMI
)) {
619 env
->exception_index
= EXCP_HLT
;
626 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
630 switch (run
->exit_reason
) {
632 dprintf("handle_hlt\n");
633 ret
= kvm_handle_halt(env
);