2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
22 #include <linux/kvm_host.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
26 #include <linux/highmem.h>
27 #include <linux/sched.h>
28 #include <linux/moduleparam.h>
33 MODULE_AUTHOR("Qumranet");
34 MODULE_LICENSE("GPL");
36 static int bypass_guest_pf
= 1;
37 module_param(bypass_guest_pf
, bool, 0);
39 static int enable_vpid
= 1;
40 module_param(enable_vpid
, bool, 0);
42 static int flexpriority_enabled
= 1;
43 module_param(flexpriority_enabled
, bool, 0);
45 static int enable_ept
;
46 module_param(enable_ept
, bool, 0);
58 u32 idt_vectoring_info
;
59 struct kvm_msr_entry
*guest_msrs
;
60 struct kvm_msr_entry
*host_msrs
;
65 int msr_offset_kernel_gs_base
;
70 u16 fs_sel
, gs_sel
, ldt_sel
;
71 int gs_ldt_reload_needed
;
73 int guest_efer_loaded
;
85 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
87 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
90 static int init_rmode(struct kvm
*kvm
);
92 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
93 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
95 static struct page
*vmx_io_bitmap_a
;
96 static struct page
*vmx_io_bitmap_b
;
97 static struct page
*vmx_msr_bitmap
;
99 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
100 static DEFINE_SPINLOCK(vmx_vpid_lock
);
102 static struct vmcs_config
{
106 u32 pin_based_exec_ctrl
;
107 u32 cpu_based_exec_ctrl
;
108 u32 cpu_based_2nd_exec_ctrl
;
113 struct vmx_capability
{
118 #define VMX_SEGMENT_FIELD(seg) \
119 [VCPU_SREG_##seg] = { \
120 .selector = GUEST_##seg##_SELECTOR, \
121 .base = GUEST_##seg##_BASE, \
122 .limit = GUEST_##seg##_LIMIT, \
123 .ar_bytes = GUEST_##seg##_AR_BYTES, \
126 static struct kvm_vmx_segment_field
{
131 } kvm_vmx_segment_fields
[] = {
132 VMX_SEGMENT_FIELD(CS
),
133 VMX_SEGMENT_FIELD(DS
),
134 VMX_SEGMENT_FIELD(ES
),
135 VMX_SEGMENT_FIELD(FS
),
136 VMX_SEGMENT_FIELD(GS
),
137 VMX_SEGMENT_FIELD(SS
),
138 VMX_SEGMENT_FIELD(TR
),
139 VMX_SEGMENT_FIELD(LDTR
),
143 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
144 * away by decrementing the array size.
146 static const u32 vmx_msr_index
[] = {
148 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
150 MSR_EFER
, MSR_K6_STAR
,
152 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
154 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
158 for (i
= 0; i
< n
; ++i
)
159 wrmsrl(e
[i
].index
, e
[i
].data
);
162 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
166 for (i
= 0; i
< n
; ++i
)
167 rdmsrl(e
[i
].index
, e
[i
].data
);
170 static inline int is_page_fault(u32 intr_info
)
172 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
173 INTR_INFO_VALID_MASK
)) ==
174 (INTR_TYPE_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
177 static inline int is_no_device(u32 intr_info
)
179 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
180 INTR_INFO_VALID_MASK
)) ==
181 (INTR_TYPE_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
184 static inline int is_invalid_opcode(u32 intr_info
)
186 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
187 INTR_INFO_VALID_MASK
)) ==
188 (INTR_TYPE_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
191 static inline int is_external_interrupt(u32 intr_info
)
193 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
194 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
197 static inline int cpu_has_vmx_msr_bitmap(void)
199 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
);
202 static inline int cpu_has_vmx_tpr_shadow(void)
204 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
);
207 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
209 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
)));
212 static inline int cpu_has_secondary_exec_ctrls(void)
214 return (vmcs_config
.cpu_based_exec_ctrl
&
215 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
);
218 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
220 return flexpriority_enabled
221 && (vmcs_config
.cpu_based_2nd_exec_ctrl
&
222 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
225 static inline int cpu_has_vmx_invept_individual_addr(void)
227 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
));
230 static inline int cpu_has_vmx_invept_context(void)
232 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
));
235 static inline int cpu_has_vmx_invept_global(void)
237 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
));
240 static inline int cpu_has_vmx_ept(void)
242 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
243 SECONDARY_EXEC_ENABLE_EPT
);
246 static inline int vm_need_ept(void)
248 return (cpu_has_vmx_ept() && enable_ept
);
251 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
253 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
254 (irqchip_in_kernel(kvm
)));
257 static inline int cpu_has_vmx_vpid(void)
259 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
260 SECONDARY_EXEC_ENABLE_VPID
);
263 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
267 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
268 if (vmx
->guest_msrs
[i
].index
== msr
)
273 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
279 } operand
= { vpid
, 0, gva
};
281 asm volatile (ASM_VMX_INVVPID
282 /* CF==1 or ZF==1 --> rc = -1 */
284 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
287 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
291 i
= __find_msr_index(vmx
, msr
);
293 return &vmx
->guest_msrs
[i
];
297 static void vmcs_clear(struct vmcs
*vmcs
)
299 u64 phys_addr
= __pa(vmcs
);
302 asm volatile (ASM_VMX_VMCLEAR_RAX
"; setna %0"
303 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
306 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
310 static void __vcpu_clear(void *arg
)
312 struct vcpu_vmx
*vmx
= arg
;
313 int cpu
= raw_smp_processor_id();
315 if (vmx
->vcpu
.cpu
== cpu
)
316 vmcs_clear(vmx
->vmcs
);
317 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
318 per_cpu(current_vmcs
, cpu
) = NULL
;
319 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
322 static void vcpu_clear(struct vcpu_vmx
*vmx
)
324 if (vmx
->vcpu
.cpu
== -1)
326 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 0, 1);
330 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
335 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
338 static unsigned long vmcs_readl(unsigned long field
)
342 asm volatile (ASM_VMX_VMREAD_RDX_RAX
343 : "=a"(value
) : "d"(field
) : "cc");
347 static u16
vmcs_read16(unsigned long field
)
349 return vmcs_readl(field
);
352 static u32
vmcs_read32(unsigned long field
)
354 return vmcs_readl(field
);
357 static u64
vmcs_read64(unsigned long field
)
360 return vmcs_readl(field
);
362 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
366 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
368 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
369 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
373 static void vmcs_writel(unsigned long field
, unsigned long value
)
377 asm volatile (ASM_VMX_VMWRITE_RAX_RDX
"; setna %0"
378 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
380 vmwrite_error(field
, value
);
383 static void vmcs_write16(unsigned long field
, u16 value
)
385 vmcs_writel(field
, value
);
388 static void vmcs_write32(unsigned long field
, u32 value
)
390 vmcs_writel(field
, value
);
393 static void vmcs_write64(unsigned long field
, u64 value
)
396 vmcs_writel(field
, value
);
398 vmcs_writel(field
, value
);
400 vmcs_writel(field
+1, value
>> 32);
404 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
406 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
409 static void vmcs_set_bits(unsigned long field
, u32 mask
)
411 vmcs_writel(field
, vmcs_readl(field
) | mask
);
414 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
418 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
);
419 if (!vcpu
->fpu_active
)
420 eb
|= 1u << NM_VECTOR
;
421 if (vcpu
->guest_debug
.enabled
)
423 if (vcpu
->arch
.rmode
.active
)
425 vmcs_write32(EXCEPTION_BITMAP
, eb
);
428 static void reload_tss(void)
431 * VT restores TR but not its size. Useless.
433 struct descriptor_table gdt
;
434 struct desc_struct
*descs
;
437 descs
= (void *)gdt
.base
;
438 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
442 static void load_transition_efer(struct vcpu_vmx
*vmx
)
444 int efer_offset
= vmx
->msr_offset_efer
;
445 u64 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
446 u64 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
452 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
455 ignore_bits
= EFER_NX
| EFER_SCE
;
457 ignore_bits
|= EFER_LMA
| EFER_LME
;
458 /* SCE is meaningful only in long mode on Intel */
459 if (guest_efer
& EFER_LMA
)
460 ignore_bits
&= ~(u64
)EFER_SCE
;
462 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
465 vmx
->host_state
.guest_efer_loaded
= 1;
466 guest_efer
&= ~ignore_bits
;
467 guest_efer
|= host_efer
& ignore_bits
;
468 wrmsrl(MSR_EFER
, guest_efer
);
469 vmx
->vcpu
.stat
.efer_reload
++;
472 static void reload_host_efer(struct vcpu_vmx
*vmx
)
474 if (vmx
->host_state
.guest_efer_loaded
) {
475 vmx
->host_state
.guest_efer_loaded
= 0;
476 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
480 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
482 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
484 if (vmx
->host_state
.loaded
)
487 vmx
->host_state
.loaded
= 1;
489 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
490 * allow segment selectors with cpl > 0 or ti == 1.
492 vmx
->host_state
.ldt_sel
= read_ldt();
493 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
494 vmx
->host_state
.fs_sel
= read_fs();
495 if (!(vmx
->host_state
.fs_sel
& 7)) {
496 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
497 vmx
->host_state
.fs_reload_needed
= 0;
499 vmcs_write16(HOST_FS_SELECTOR
, 0);
500 vmx
->host_state
.fs_reload_needed
= 1;
502 vmx
->host_state
.gs_sel
= read_gs();
503 if (!(vmx
->host_state
.gs_sel
& 7))
504 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
506 vmcs_write16(HOST_GS_SELECTOR
, 0);
507 vmx
->host_state
.gs_ldt_reload_needed
= 1;
511 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
512 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
514 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
515 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
519 if (is_long_mode(&vmx
->vcpu
))
520 save_msrs(vmx
->host_msrs
+
521 vmx
->msr_offset_kernel_gs_base
, 1);
524 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
525 load_transition_efer(vmx
);
528 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
532 if (!vmx
->host_state
.loaded
)
535 ++vmx
->vcpu
.stat
.host_state_reload
;
536 vmx
->host_state
.loaded
= 0;
537 if (vmx
->host_state
.fs_reload_needed
)
538 load_fs(vmx
->host_state
.fs_sel
);
539 if (vmx
->host_state
.gs_ldt_reload_needed
) {
540 load_ldt(vmx
->host_state
.ldt_sel
);
542 * If we have to reload gs, we must take care to
543 * preserve our gs base.
545 local_irq_save(flags
);
546 load_gs(vmx
->host_state
.gs_sel
);
548 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
550 local_irq_restore(flags
);
553 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
554 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
555 reload_host_efer(vmx
);
559 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
560 * vcpu mutex is already taken.
562 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
564 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
565 u64 phys_addr
= __pa(vmx
->vmcs
);
566 u64 tsc_this
, delta
, new_offset
;
568 if (vcpu
->cpu
!= cpu
) {
570 kvm_migrate_apic_timer(vcpu
);
571 vpid_sync_vcpu_all(vmx
);
574 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
577 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
578 asm volatile (ASM_VMX_VMPTRLD_RAX
"; setna %0"
579 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
582 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
583 vmx
->vmcs
, phys_addr
);
586 if (vcpu
->cpu
!= cpu
) {
587 struct descriptor_table dt
;
588 unsigned long sysenter_esp
;
592 * Linux uses per-cpu TSS and GDT, so set these when switching
595 vmcs_writel(HOST_TR_BASE
, read_tr_base()); /* 22.2.4 */
597 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
599 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
600 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
603 * Make sure the time stamp counter is monotonous.
606 if (tsc_this
< vcpu
->arch
.host_tsc
) {
607 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
608 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
609 vmcs_write64(TSC_OFFSET
, new_offset
);
614 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
616 vmx_load_host_state(to_vmx(vcpu
));
619 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
621 if (vcpu
->fpu_active
)
623 vcpu
->fpu_active
= 1;
624 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
625 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
626 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
627 update_exception_bitmap(vcpu
);
630 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
632 if (!vcpu
->fpu_active
)
634 vcpu
->fpu_active
= 0;
635 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
636 update_exception_bitmap(vcpu
);
639 static void vmx_vcpu_decache(struct kvm_vcpu
*vcpu
)
641 vcpu_clear(to_vmx(vcpu
));
644 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
646 return vmcs_readl(GUEST_RFLAGS
);
649 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
651 if (vcpu
->arch
.rmode
.active
)
652 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
653 vmcs_writel(GUEST_RFLAGS
, rflags
);
656 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
659 u32 interruptibility
;
661 rip
= vmcs_readl(GUEST_RIP
);
662 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
663 vmcs_writel(GUEST_RIP
, rip
);
666 * We emulated an instruction, so temporary interrupt blocking
667 * should be removed, if set.
669 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
670 if (interruptibility
& 3)
671 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
672 interruptibility
& ~3);
673 vcpu
->arch
.interrupt_window_open
= 1;
676 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
677 bool has_error_code
, u32 error_code
)
679 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
680 nr
| INTR_TYPE_EXCEPTION
681 | (has_error_code
? INTR_INFO_DELIVER_CODE_MASK
: 0)
682 | INTR_INFO_VALID_MASK
);
684 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
687 static bool vmx_exception_injected(struct kvm_vcpu
*vcpu
)
689 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
691 return !(vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
695 * Swap MSR entry in host/guest MSR entry array.
698 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
700 struct kvm_msr_entry tmp
;
702 tmp
= vmx
->guest_msrs
[to
];
703 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
704 vmx
->guest_msrs
[from
] = tmp
;
705 tmp
= vmx
->host_msrs
[to
];
706 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
707 vmx
->host_msrs
[from
] = tmp
;
712 * Set up the vmcs to automatically save and restore system
713 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
714 * mode, as fiddling with msrs is very expensive.
716 static void setup_msrs(struct vcpu_vmx
*vmx
)
720 vmx_load_host_state(vmx
);
723 if (is_long_mode(&vmx
->vcpu
)) {
726 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
728 move_msr_up(vmx
, index
, save_nmsrs
++);
729 index
= __find_msr_index(vmx
, MSR_LSTAR
);
731 move_msr_up(vmx
, index
, save_nmsrs
++);
732 index
= __find_msr_index(vmx
, MSR_CSTAR
);
734 move_msr_up(vmx
, index
, save_nmsrs
++);
735 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
737 move_msr_up(vmx
, index
, save_nmsrs
++);
739 * MSR_K6_STAR is only needed on long mode guests, and only
740 * if efer.sce is enabled.
742 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
743 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
744 move_msr_up(vmx
, index
, save_nmsrs
++);
747 vmx
->save_nmsrs
= save_nmsrs
;
750 vmx
->msr_offset_kernel_gs_base
=
751 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
753 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
757 * reads and returns guest's timestamp counter "register"
758 * guest_tsc = host_tsc + tsc_offset -- 21.3
760 static u64
guest_read_tsc(void)
762 u64 host_tsc
, tsc_offset
;
765 tsc_offset
= vmcs_read64(TSC_OFFSET
);
766 return host_tsc
+ tsc_offset
;
770 * writes 'guest_tsc' into guest's timestamp counter "register"
771 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
773 static void guest_write_tsc(u64 guest_tsc
)
778 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
782 * Reads an msr value (of 'msr_index') into 'pdata'.
783 * Returns 0 on success, non-0 otherwise.
784 * Assumes vcpu_load() was already called.
786 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
789 struct kvm_msr_entry
*msr
;
792 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
799 data
= vmcs_readl(GUEST_FS_BASE
);
802 data
= vmcs_readl(GUEST_GS_BASE
);
805 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
807 case MSR_IA32_TIME_STAMP_COUNTER
:
808 data
= guest_read_tsc();
810 case MSR_IA32_SYSENTER_CS
:
811 data
= vmcs_read32(GUEST_SYSENTER_CS
);
813 case MSR_IA32_SYSENTER_EIP
:
814 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
816 case MSR_IA32_SYSENTER_ESP
:
817 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
820 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
825 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
833 * Writes msr value into into the appropriate "register".
834 * Returns 0 on success, non-0 otherwise.
835 * Assumes vcpu_load() was already called.
837 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
839 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
840 struct kvm_msr_entry
*msr
;
846 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
847 if (vmx
->host_state
.loaded
) {
848 reload_host_efer(vmx
);
849 load_transition_efer(vmx
);
853 vmcs_writel(GUEST_FS_BASE
, data
);
856 vmcs_writel(GUEST_GS_BASE
, data
);
859 case MSR_IA32_SYSENTER_CS
:
860 vmcs_write32(GUEST_SYSENTER_CS
, data
);
862 case MSR_IA32_SYSENTER_EIP
:
863 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
865 case MSR_IA32_SYSENTER_ESP
:
866 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
868 case MSR_IA32_TIME_STAMP_COUNTER
:
869 guest_write_tsc(data
);
872 msr
= find_msr_entry(vmx
, msr_index
);
875 if (vmx
->host_state
.loaded
)
876 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
879 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
886 * Sync the rsp and rip registers into the vcpu structure. This allows
887 * registers to be accessed by indexing vcpu->arch.regs.
889 static void vcpu_load_rsp_rip(struct kvm_vcpu
*vcpu
)
891 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
892 vcpu
->arch
.rip
= vmcs_readl(GUEST_RIP
);
896 * Syncs rsp and rip back into the vmcs. Should be called after possible
899 static void vcpu_put_rsp_rip(struct kvm_vcpu
*vcpu
)
901 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
902 vmcs_writel(GUEST_RIP
, vcpu
->arch
.rip
);
905 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
907 unsigned long dr7
= 0x400;
910 old_singlestep
= vcpu
->guest_debug
.singlestep
;
912 vcpu
->guest_debug
.enabled
= dbg
->enabled
;
913 if (vcpu
->guest_debug
.enabled
) {
916 dr7
|= 0x200; /* exact */
917 for (i
= 0; i
< 4; ++i
) {
918 if (!dbg
->breakpoints
[i
].enabled
)
920 vcpu
->guest_debug
.bp
[i
] = dbg
->breakpoints
[i
].address
;
921 dr7
|= 2 << (i
*2); /* global enable */
922 dr7
|= 0 << (i
*4+16); /* execution breakpoint */
925 vcpu
->guest_debug
.singlestep
= dbg
->singlestep
;
927 vcpu
->guest_debug
.singlestep
= 0;
929 if (old_singlestep
&& !vcpu
->guest_debug
.singlestep
) {
932 flags
= vmcs_readl(GUEST_RFLAGS
);
933 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
934 vmcs_writel(GUEST_RFLAGS
, flags
);
937 update_exception_bitmap(vcpu
);
938 vmcs_writel(GUEST_DR7
, dr7
);
943 static int vmx_get_irq(struct kvm_vcpu
*vcpu
)
945 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
948 idtv_info_field
= vmx
->idt_vectoring_info
;
949 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
950 if (is_external_interrupt(idtv_info_field
))
951 return idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
953 printk(KERN_DEBUG
"pending exception: not handled yet\n");
958 static __init
int cpu_has_kvm_support(void)
960 unsigned long ecx
= cpuid_ecx(1);
961 return test_bit(5, &ecx
); /* CPUID.1:ECX.VMX[bit 5] -> VT */
964 static __init
int vmx_disabled_by_bios(void)
968 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
969 return (msr
& (MSR_IA32_FEATURE_CONTROL_LOCKED
|
970 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
971 == MSR_IA32_FEATURE_CONTROL_LOCKED
;
972 /* locked but not enabled */
975 static void hardware_enable(void *garbage
)
977 int cpu
= raw_smp_processor_id();
978 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
981 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
982 if ((old
& (MSR_IA32_FEATURE_CONTROL_LOCKED
|
983 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
984 != (MSR_IA32_FEATURE_CONTROL_LOCKED
|
985 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
986 /* enable and lock */
987 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
988 MSR_IA32_FEATURE_CONTROL_LOCKED
|
989 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
);
990 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
991 asm volatile (ASM_VMX_VMXON_RAX
: : "a"(&phys_addr
), "m"(phys_addr
)
995 static void hardware_disable(void *garbage
)
997 asm volatile (ASM_VMX_VMXOFF
: : : "cc");
1000 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1001 u32 msr
, u32
*result
)
1003 u32 vmx_msr_low
, vmx_msr_high
;
1004 u32 ctl
= ctl_min
| ctl_opt
;
1006 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1008 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1009 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1011 /* Ensure minimum (required) set of control bits are supported. */
1019 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1021 u32 vmx_msr_low
, vmx_msr_high
;
1022 u32 min
, opt
, min2
, opt2
;
1023 u32 _pin_based_exec_control
= 0;
1024 u32 _cpu_based_exec_control
= 0;
1025 u32 _cpu_based_2nd_exec_control
= 0;
1026 u32 _vmexit_control
= 0;
1027 u32 _vmentry_control
= 0;
1029 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1031 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1032 &_pin_based_exec_control
) < 0)
1035 min
= CPU_BASED_HLT_EXITING
|
1036 #ifdef CONFIG_X86_64
1037 CPU_BASED_CR8_LOAD_EXITING
|
1038 CPU_BASED_CR8_STORE_EXITING
|
1040 CPU_BASED_CR3_LOAD_EXITING
|
1041 CPU_BASED_CR3_STORE_EXITING
|
1042 CPU_BASED_USE_IO_BITMAPS
|
1043 CPU_BASED_MOV_DR_EXITING
|
1044 CPU_BASED_USE_TSC_OFFSETING
;
1045 opt
= CPU_BASED_TPR_SHADOW
|
1046 CPU_BASED_USE_MSR_BITMAPS
|
1047 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1048 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1049 &_cpu_based_exec_control
) < 0)
1051 #ifdef CONFIG_X86_64
1052 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1053 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1054 ~CPU_BASED_CR8_STORE_EXITING
;
1056 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1058 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1059 SECONDARY_EXEC_WBINVD_EXITING
|
1060 SECONDARY_EXEC_ENABLE_VPID
|
1061 SECONDARY_EXEC_ENABLE_EPT
;
1062 if (adjust_vmx_controls(min2
, opt2
,
1063 MSR_IA32_VMX_PROCBASED_CTLS2
,
1064 &_cpu_based_2nd_exec_control
) < 0)
1067 #ifndef CONFIG_X86_64
1068 if (!(_cpu_based_2nd_exec_control
&
1069 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1070 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1072 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1073 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1074 min
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1075 CPU_BASED_CR3_STORE_EXITING
);
1076 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1077 &_cpu_based_exec_control
) < 0)
1079 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1080 vmx_capability
.ept
, vmx_capability
.vpid
);
1084 #ifdef CONFIG_X86_64
1085 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1088 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1089 &_vmexit_control
) < 0)
1093 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1094 &_vmentry_control
) < 0)
1097 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1099 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1100 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1103 #ifdef CONFIG_X86_64
1104 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1105 if (vmx_msr_high
& (1u<<16))
1109 /* Require Write-Back (WB) memory type for VMCS accesses. */
1110 if (((vmx_msr_high
>> 18) & 15) != 6)
1113 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1114 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1115 vmcs_conf
->revision_id
= vmx_msr_low
;
1117 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1118 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1119 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1120 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1121 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1126 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1128 int node
= cpu_to_node(cpu
);
1132 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1135 vmcs
= page_address(pages
);
1136 memset(vmcs
, 0, vmcs_config
.size
);
1137 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1141 static struct vmcs
*alloc_vmcs(void)
1143 return alloc_vmcs_cpu(raw_smp_processor_id());
1146 static void free_vmcs(struct vmcs
*vmcs
)
1148 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1151 static void free_kvm_area(void)
1155 for_each_online_cpu(cpu
)
1156 free_vmcs(per_cpu(vmxarea
, cpu
));
1159 static __init
int alloc_kvm_area(void)
1163 for_each_online_cpu(cpu
) {
1166 vmcs
= alloc_vmcs_cpu(cpu
);
1172 per_cpu(vmxarea
, cpu
) = vmcs
;
1177 static __init
int hardware_setup(void)
1179 if (setup_vmcs_config(&vmcs_config
) < 0)
1182 if (boot_cpu_has(X86_FEATURE_NX
))
1183 kvm_enable_efer_bits(EFER_NX
);
1185 return alloc_kvm_area();
1188 static __exit
void hardware_unsetup(void)
1193 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1195 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1197 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1198 vmcs_write16(sf
->selector
, save
->selector
);
1199 vmcs_writel(sf
->base
, save
->base
);
1200 vmcs_write32(sf
->limit
, save
->limit
);
1201 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1203 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1205 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1209 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1211 unsigned long flags
;
1213 vcpu
->arch
.rmode
.active
= 0;
1215 vmcs_writel(GUEST_TR_BASE
, vcpu
->arch
.rmode
.tr
.base
);
1216 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->arch
.rmode
.tr
.limit
);
1217 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->arch
.rmode
.tr
.ar
);
1219 flags
= vmcs_readl(GUEST_RFLAGS
);
1220 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1221 flags
|= (vcpu
->arch
.rmode
.save_iopl
<< IOPL_SHIFT
);
1222 vmcs_writel(GUEST_RFLAGS
, flags
);
1224 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1225 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1227 update_exception_bitmap(vcpu
);
1229 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1230 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1231 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1232 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1234 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1235 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1237 vmcs_write16(GUEST_CS_SELECTOR
,
1238 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1239 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1242 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1244 if (!kvm
->arch
.tss_addr
) {
1245 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1246 kvm
->memslots
[0].npages
- 3;
1247 return base_gfn
<< PAGE_SHIFT
;
1249 return kvm
->arch
.tss_addr
;
1252 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1254 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1256 save
->selector
= vmcs_read16(sf
->selector
);
1257 save
->base
= vmcs_readl(sf
->base
);
1258 save
->limit
= vmcs_read32(sf
->limit
);
1259 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1260 vmcs_write16(sf
->selector
, save
->base
>> 4);
1261 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1262 vmcs_write32(sf
->limit
, 0xffff);
1263 vmcs_write32(sf
->ar_bytes
, 0xf3);
1266 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1268 unsigned long flags
;
1270 vcpu
->arch
.rmode
.active
= 1;
1272 vcpu
->arch
.rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1273 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1275 vcpu
->arch
.rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1276 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1278 vcpu
->arch
.rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1279 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1281 flags
= vmcs_readl(GUEST_RFLAGS
);
1282 vcpu
->arch
.rmode
.save_iopl
1283 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1285 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1287 vmcs_writel(GUEST_RFLAGS
, flags
);
1288 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1289 update_exception_bitmap(vcpu
);
1291 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1292 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1293 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1295 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1296 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1297 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1298 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1299 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1301 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1302 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1303 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1304 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1306 kvm_mmu_reset_context(vcpu
);
1307 init_rmode(vcpu
->kvm
);
1310 #ifdef CONFIG_X86_64
1312 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1316 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1317 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1318 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1320 vmcs_write32(GUEST_TR_AR_BYTES
,
1321 (guest_tr_ar
& ~AR_TYPE_MASK
)
1322 | AR_TYPE_BUSY_64_TSS
);
1325 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1327 find_msr_entry(to_vmx(vcpu
), MSR_EFER
)->data
|= EFER_LMA
| EFER_LME
;
1328 vmcs_write32(VM_ENTRY_CONTROLS
,
1329 vmcs_read32(VM_ENTRY_CONTROLS
)
1330 | VM_ENTRY_IA32E_MODE
);
1333 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1335 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1337 vmcs_write32(VM_ENTRY_CONTROLS
,
1338 vmcs_read32(VM_ENTRY_CONTROLS
)
1339 & ~VM_ENTRY_IA32E_MODE
);
1344 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1346 vpid_sync_vcpu_all(to_vmx(vcpu
));
1349 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1351 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1352 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1355 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1357 vmx_fpu_deactivate(vcpu
);
1359 if (vcpu
->arch
.rmode
.active
&& (cr0
& X86_CR0_PE
))
1362 if (!vcpu
->arch
.rmode
.active
&& !(cr0
& X86_CR0_PE
))
1365 #ifdef CONFIG_X86_64
1366 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1367 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1369 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1374 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1375 vmcs_writel(GUEST_CR0
,
1376 (cr0
& ~KVM_GUEST_CR0_MASK
) | KVM_VM_CR0_ALWAYS_ON
);
1377 vcpu
->arch
.cr0
= cr0
;
1379 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1380 vmx_fpu_activate(vcpu
);
1383 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1385 vmx_flush_tlb(vcpu
);
1386 vmcs_writel(GUEST_CR3
, cr3
);
1387 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1388 vmx_fpu_deactivate(vcpu
);
1391 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1393 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1394 vmcs_writel(GUEST_CR4
, cr4
| (vcpu
->arch
.rmode
.active
?
1395 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
));
1396 vcpu
->arch
.cr4
= cr4
;
1399 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1401 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1402 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1404 vcpu
->arch
.shadow_efer
= efer
;
1407 if (efer
& EFER_LMA
) {
1408 vmcs_write32(VM_ENTRY_CONTROLS
,
1409 vmcs_read32(VM_ENTRY_CONTROLS
) |
1410 VM_ENTRY_IA32E_MODE
);
1414 vmcs_write32(VM_ENTRY_CONTROLS
,
1415 vmcs_read32(VM_ENTRY_CONTROLS
) &
1416 ~VM_ENTRY_IA32E_MODE
);
1418 msr
->data
= efer
& ~EFER_LME
;
1423 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1425 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1427 return vmcs_readl(sf
->base
);
1430 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1431 struct kvm_segment
*var
, int seg
)
1433 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1436 var
->base
= vmcs_readl(sf
->base
);
1437 var
->limit
= vmcs_read32(sf
->limit
);
1438 var
->selector
= vmcs_read16(sf
->selector
);
1439 ar
= vmcs_read32(sf
->ar_bytes
);
1440 if (ar
& AR_UNUSABLE_MASK
)
1442 var
->type
= ar
& 15;
1443 var
->s
= (ar
>> 4) & 1;
1444 var
->dpl
= (ar
>> 5) & 3;
1445 var
->present
= (ar
>> 7) & 1;
1446 var
->avl
= (ar
>> 12) & 1;
1447 var
->l
= (ar
>> 13) & 1;
1448 var
->db
= (ar
>> 14) & 1;
1449 var
->g
= (ar
>> 15) & 1;
1450 var
->unusable
= (ar
>> 16) & 1;
1453 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1455 struct kvm_segment kvm_seg
;
1457 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1460 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1463 vmx_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_CS
);
1464 return kvm_seg
.selector
& 3;
1467 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1474 ar
= var
->type
& 15;
1475 ar
|= (var
->s
& 1) << 4;
1476 ar
|= (var
->dpl
& 3) << 5;
1477 ar
|= (var
->present
& 1) << 7;
1478 ar
|= (var
->avl
& 1) << 12;
1479 ar
|= (var
->l
& 1) << 13;
1480 ar
|= (var
->db
& 1) << 14;
1481 ar
|= (var
->g
& 1) << 15;
1483 if (ar
== 0) /* a 0 value means unusable */
1484 ar
= AR_UNUSABLE_MASK
;
1489 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1490 struct kvm_segment
*var
, int seg
)
1492 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1495 if (vcpu
->arch
.rmode
.active
&& seg
== VCPU_SREG_TR
) {
1496 vcpu
->arch
.rmode
.tr
.selector
= var
->selector
;
1497 vcpu
->arch
.rmode
.tr
.base
= var
->base
;
1498 vcpu
->arch
.rmode
.tr
.limit
= var
->limit
;
1499 vcpu
->arch
.rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1502 vmcs_writel(sf
->base
, var
->base
);
1503 vmcs_write32(sf
->limit
, var
->limit
);
1504 vmcs_write16(sf
->selector
, var
->selector
);
1505 if (vcpu
->arch
.rmode
.active
&& var
->s
) {
1507 * Hack real-mode segments into vm86 compatibility.
1509 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1510 vmcs_writel(sf
->base
, 0xf0000);
1513 ar
= vmx_segment_access_rights(var
);
1514 vmcs_write32(sf
->ar_bytes
, ar
);
1517 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1519 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1521 *db
= (ar
>> 14) & 1;
1522 *l
= (ar
>> 13) & 1;
1525 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1527 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1528 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1531 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1533 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1534 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1537 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1539 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1540 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1543 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1545 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1546 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1549 static int init_rmode_tss(struct kvm
*kvm
)
1551 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
1556 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1559 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
1560 r
= kvm_write_guest_page(kvm
, fn
++, &data
, 0x66, sizeof(u16
));
1563 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
1566 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1570 r
= kvm_write_guest_page(kvm
, fn
, &data
,
1571 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
1581 static int init_rmode_identity_map(struct kvm
*kvm
)
1584 pfn_t identity_map_pfn
;
1589 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
1590 printk(KERN_ERR
"EPT: identity-mapping pagetable "
1591 "haven't been allocated!\n");
1594 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
1597 identity_map_pfn
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
;
1598 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
1601 /* Set up identity-mapping pagetable for EPT in real mode */
1602 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
1603 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
1604 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
1605 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
1606 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
1610 kvm
->arch
.ept_identity_pagetable_done
= true;
1616 static void seg_setup(int seg
)
1618 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1620 vmcs_write16(sf
->selector
, 0);
1621 vmcs_writel(sf
->base
, 0);
1622 vmcs_write32(sf
->limit
, 0xffff);
1623 vmcs_write32(sf
->ar_bytes
, 0x93);
1626 static int alloc_apic_access_page(struct kvm
*kvm
)
1628 struct kvm_userspace_memory_region kvm_userspace_mem
;
1631 down_write(&kvm
->slots_lock
);
1632 if (kvm
->arch
.apic_access_page
)
1634 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
1635 kvm_userspace_mem
.flags
= 0;
1636 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
1637 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
1638 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1642 down_read(¤t
->mm
->mmap_sem
);
1643 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
1644 up_read(¤t
->mm
->mmap_sem
);
1646 up_write(&kvm
->slots_lock
);
1650 static int alloc_identity_pagetable(struct kvm
*kvm
)
1652 struct kvm_userspace_memory_region kvm_userspace_mem
;
1655 down_write(&kvm
->slots_lock
);
1656 if (kvm
->arch
.ept_identity_pagetable
)
1658 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
1659 kvm_userspace_mem
.flags
= 0;
1660 kvm_userspace_mem
.guest_phys_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
1661 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
1662 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1666 down_read(¤t
->mm
->mmap_sem
);
1667 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
1668 VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
);
1669 up_read(¤t
->mm
->mmap_sem
);
1671 up_write(&kvm
->slots_lock
);
1675 static void allocate_vpid(struct vcpu_vmx
*vmx
)
1680 if (!enable_vpid
|| !cpu_has_vmx_vpid())
1682 spin_lock(&vmx_vpid_lock
);
1683 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
1684 if (vpid
< VMX_NR_VPIDS
) {
1686 __set_bit(vpid
, vmx_vpid_bitmap
);
1688 spin_unlock(&vmx_vpid_lock
);
1691 void vmx_disable_intercept_for_msr(struct page
*msr_bitmap
, u32 msr
)
1695 if (!cpu_has_vmx_msr_bitmap())
1699 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1700 * have the write-low and read-high bitmap offsets the wrong way round.
1701 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1703 va
= kmap(msr_bitmap
);
1704 if (msr
<= 0x1fff) {
1705 __clear_bit(msr
, va
+ 0x000); /* read-low */
1706 __clear_bit(msr
, va
+ 0x800); /* write-low */
1707 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1709 __clear_bit(msr
, va
+ 0x400); /* read-high */
1710 __clear_bit(msr
, va
+ 0xc00); /* write-high */
1716 * Sets up the vmcs for emulated real mode.
1718 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
1720 u32 host_sysenter_cs
;
1723 struct descriptor_table dt
;
1725 unsigned long kvm_vmx_return
;
1729 vmcs_write64(IO_BITMAP_A
, page_to_phys(vmx_io_bitmap_a
));
1730 vmcs_write64(IO_BITMAP_B
, page_to_phys(vmx_io_bitmap_b
));
1732 if (cpu_has_vmx_msr_bitmap())
1733 vmcs_write64(MSR_BITMAP
, page_to_phys(vmx_msr_bitmap
));
1735 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
1738 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
1739 vmcs_config
.pin_based_exec_ctrl
);
1741 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
1742 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
1743 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1744 #ifdef CONFIG_X86_64
1745 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
1746 CPU_BASED_CR8_LOAD_EXITING
;
1750 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
1751 CPU_BASED_CR3_LOAD_EXITING
;
1752 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
1754 if (cpu_has_secondary_exec_ctrls()) {
1755 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
1756 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
1758 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
1760 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
1762 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
1763 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
1766 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
1767 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
1768 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
1770 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
1771 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
1772 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1774 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
1775 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1776 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1777 vmcs_write16(HOST_FS_SELECTOR
, read_fs()); /* 22.2.4 */
1778 vmcs_write16(HOST_GS_SELECTOR
, read_gs()); /* 22.2.4 */
1779 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1780 #ifdef CONFIG_X86_64
1781 rdmsrl(MSR_FS_BASE
, a
);
1782 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
1783 rdmsrl(MSR_GS_BASE
, a
);
1784 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
1786 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
1787 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
1790 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
1793 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
1795 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
1796 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
1797 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
1798 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
1799 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
1801 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
1802 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
1803 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
1804 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
1805 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
1806 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
1808 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
1809 u32 index
= vmx_msr_index
[i
];
1810 u32 data_low
, data_high
;
1814 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
1816 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
1818 data
= data_low
| ((u64
)data_high
<< 32);
1819 vmx
->host_msrs
[j
].index
= index
;
1820 vmx
->host_msrs
[j
].reserved
= 0;
1821 vmx
->host_msrs
[j
].data
= data
;
1822 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
1826 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
1828 /* 22.2.1, 20.8.1 */
1829 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
1831 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
1832 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
1838 static int init_rmode(struct kvm
*kvm
)
1840 if (!init_rmode_tss(kvm
))
1842 if (!init_rmode_identity_map(kvm
))
1847 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
1849 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1853 down_read(&vcpu
->kvm
->slots_lock
);
1854 if (!init_rmode(vmx
->vcpu
.kvm
)) {
1859 vmx
->vcpu
.arch
.rmode
.active
= 0;
1861 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
1862 kvm_set_cr8(&vmx
->vcpu
, 0);
1863 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1864 if (vmx
->vcpu
.vcpu_id
== 0)
1865 msr
|= MSR_IA32_APICBASE_BSP
;
1866 kvm_set_apic_base(&vmx
->vcpu
, msr
);
1868 fx_init(&vmx
->vcpu
);
1871 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1872 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1874 if (vmx
->vcpu
.vcpu_id
== 0) {
1875 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
1876 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
1878 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
1879 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
1881 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1882 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1884 seg_setup(VCPU_SREG_DS
);
1885 seg_setup(VCPU_SREG_ES
);
1886 seg_setup(VCPU_SREG_FS
);
1887 seg_setup(VCPU_SREG_GS
);
1888 seg_setup(VCPU_SREG_SS
);
1890 vmcs_write16(GUEST_TR_SELECTOR
, 0);
1891 vmcs_writel(GUEST_TR_BASE
, 0);
1892 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
1893 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1895 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
1896 vmcs_writel(GUEST_LDTR_BASE
, 0);
1897 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
1898 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
1900 vmcs_write32(GUEST_SYSENTER_CS
, 0);
1901 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
1902 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
1904 vmcs_writel(GUEST_RFLAGS
, 0x02);
1905 if (vmx
->vcpu
.vcpu_id
== 0)
1906 vmcs_writel(GUEST_RIP
, 0xfff0);
1908 vmcs_writel(GUEST_RIP
, 0);
1909 vmcs_writel(GUEST_RSP
, 0);
1911 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
1912 vmcs_writel(GUEST_DR7
, 0x400);
1914 vmcs_writel(GUEST_GDTR_BASE
, 0);
1915 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
1917 vmcs_writel(GUEST_IDTR_BASE
, 0);
1918 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
1920 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
1921 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
1922 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
1926 /* Special registers */
1927 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
1931 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
1933 if (cpu_has_vmx_tpr_shadow()) {
1934 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
1935 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
1936 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
1937 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
1938 vmcs_write32(TPR_THRESHOLD
, 0);
1941 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
1942 vmcs_write64(APIC_ACCESS_ADDR
,
1943 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
1946 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
1948 vmx
->vcpu
.arch
.cr0
= 0x60000010;
1949 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
1950 vmx_set_cr4(&vmx
->vcpu
, 0);
1951 vmx_set_efer(&vmx
->vcpu
, 0);
1952 vmx_fpu_activate(&vmx
->vcpu
);
1953 update_exception_bitmap(&vmx
->vcpu
);
1955 vpid_sync_vcpu_all(vmx
);
1960 up_read(&vcpu
->kvm
->slots_lock
);
1964 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
, int irq
)
1966 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1968 KVMTRACE_1D(INJ_VIRQ
, vcpu
, (u32
)irq
, handler
);
1970 if (vcpu
->arch
.rmode
.active
) {
1971 vmx
->rmode
.irq
.pending
= true;
1972 vmx
->rmode
.irq
.vector
= irq
;
1973 vmx
->rmode
.irq
.rip
= vmcs_readl(GUEST_RIP
);
1974 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
1975 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
1976 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
1977 vmcs_writel(GUEST_RIP
, vmx
->rmode
.irq
.rip
- 1);
1980 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
1981 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
1984 static void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
1986 int word_index
= __ffs(vcpu
->arch
.irq_summary
);
1987 int bit_index
= __ffs(vcpu
->arch
.irq_pending
[word_index
]);
1988 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
1990 clear_bit(bit_index
, &vcpu
->arch
.irq_pending
[word_index
]);
1991 if (!vcpu
->arch
.irq_pending
[word_index
])
1992 clear_bit(word_index
, &vcpu
->arch
.irq_summary
);
1993 vmx_inject_irq(vcpu
, irq
);
1997 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
1998 struct kvm_run
*kvm_run
)
2000 u32 cpu_based_vm_exec_control
;
2002 vcpu
->arch
.interrupt_window_open
=
2003 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2004 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
2006 if (vcpu
->arch
.interrupt_window_open
&&
2007 vcpu
->arch
.irq_summary
&&
2008 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
) & INTR_INFO_VALID_MASK
))
2010 * If interrupts enabled, and not blocked by sti or mov ss. Good.
2012 kvm_do_inject_irq(vcpu
);
2014 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2015 if (!vcpu
->arch
.interrupt_window_open
&&
2016 (vcpu
->arch
.irq_summary
|| kvm_run
->request_interrupt_window
))
2018 * Interrupts blocked. Wait for unblock.
2020 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2022 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2023 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2026 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2029 struct kvm_userspace_memory_region tss_mem
= {
2031 .guest_phys_addr
= addr
,
2032 .memory_size
= PAGE_SIZE
* 3,
2036 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
2039 kvm
->arch
.tss_addr
= addr
;
2043 static void kvm_guest_debug_pre(struct kvm_vcpu
*vcpu
)
2045 struct kvm_guest_debug
*dbg
= &vcpu
->guest_debug
;
2047 set_debugreg(dbg
->bp
[0], 0);
2048 set_debugreg(dbg
->bp
[1], 1);
2049 set_debugreg(dbg
->bp
[2], 2);
2050 set_debugreg(dbg
->bp
[3], 3);
2052 if (dbg
->singlestep
) {
2053 unsigned long flags
;
2055 flags
= vmcs_readl(GUEST_RFLAGS
);
2056 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
2057 vmcs_writel(GUEST_RFLAGS
, flags
);
2061 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
2062 int vec
, u32 err_code
)
2064 if (!vcpu
->arch
.rmode
.active
)
2068 * Instruction with address size override prefix opcode 0x67
2069 * Cause the #SS fault with 0 error code in VM86 mode.
2071 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
2072 if (emulate_instruction(vcpu
, NULL
, 0, 0, 0) == EMULATE_DONE
)
2077 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2079 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2080 u32 intr_info
, error_code
;
2081 unsigned long cr2
, rip
;
2083 enum emulation_result er
;
2085 vect_info
= vmx
->idt_vectoring_info
;
2086 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2088 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
2089 !is_page_fault(intr_info
))
2090 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
2091 "intr info 0x%x\n", __func__
, vect_info
, intr_info
);
2093 if (!irqchip_in_kernel(vcpu
->kvm
) && is_external_interrupt(vect_info
)) {
2094 int irq
= vect_info
& VECTORING_INFO_VECTOR_MASK
;
2095 set_bit(irq
, vcpu
->arch
.irq_pending
);
2096 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
2099 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) /* nmi */
2100 return 1; /* already handled by vmx_vcpu_run() */
2102 if (is_no_device(intr_info
)) {
2103 vmx_fpu_activate(vcpu
);
2107 if (is_invalid_opcode(intr_info
)) {
2108 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
2109 if (er
!= EMULATE_DONE
)
2110 kvm_queue_exception(vcpu
, UD_VECTOR
);
2115 rip
= vmcs_readl(GUEST_RIP
);
2116 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2117 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2118 if (is_page_fault(intr_info
)) {
2119 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2120 KVMTRACE_3D(PAGE_FAULT
, vcpu
, error_code
, (u32
)cr2
,
2121 (u32
)((u64
)cr2
>> 32), handler
);
2122 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2125 if (vcpu
->arch
.rmode
.active
&&
2126 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2128 if (vcpu
->arch
.halt_request
) {
2129 vcpu
->arch
.halt_request
= 0;
2130 return kvm_emulate_halt(vcpu
);
2135 if ((intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
)) ==
2136 (INTR_TYPE_EXCEPTION
| 1)) {
2137 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2140 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2141 kvm_run
->ex
.exception
= intr_info
& INTR_INFO_VECTOR_MASK
;
2142 kvm_run
->ex
.error_code
= error_code
;
2146 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2147 struct kvm_run
*kvm_run
)
2149 ++vcpu
->stat
.irq_exits
;
2150 KVMTRACE_1D(INTR
, vcpu
, vmcs_read32(VM_EXIT_INTR_INFO
), handler
);
2154 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2156 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2160 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2162 unsigned long exit_qualification
;
2163 int size
, down
, in
, string
, rep
;
2166 ++vcpu
->stat
.io_exits
;
2167 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2168 string
= (exit_qualification
& 16) != 0;
2171 if (emulate_instruction(vcpu
,
2172 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2177 size
= (exit_qualification
& 7) + 1;
2178 in
= (exit_qualification
& 8) != 0;
2179 down
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_DF
) != 0;
2180 rep
= (exit_qualification
& 32) != 0;
2181 port
= exit_qualification
>> 16;
2183 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2187 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2190 * Patch in the VMCALL instruction:
2192 hypercall
[0] = 0x0f;
2193 hypercall
[1] = 0x01;
2194 hypercall
[2] = 0xc1;
2197 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2199 unsigned long exit_qualification
;
2203 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2204 cr
= exit_qualification
& 15;
2205 reg
= (exit_qualification
>> 8) & 15;
2206 switch ((exit_qualification
>> 4) & 3) {
2207 case 0: /* mov to cr */
2208 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
, (u32
)vcpu
->arch
.regs
[reg
],
2209 (u32
)((u64
)vcpu
->arch
.regs
[reg
] >> 32), handler
);
2212 vcpu_load_rsp_rip(vcpu
);
2213 kvm_set_cr0(vcpu
, vcpu
->arch
.regs
[reg
]);
2214 skip_emulated_instruction(vcpu
);
2217 vcpu_load_rsp_rip(vcpu
);
2218 kvm_set_cr3(vcpu
, vcpu
->arch
.regs
[reg
]);
2219 skip_emulated_instruction(vcpu
);
2222 vcpu_load_rsp_rip(vcpu
);
2223 kvm_set_cr4(vcpu
, vcpu
->arch
.regs
[reg
]);
2224 skip_emulated_instruction(vcpu
);
2227 vcpu_load_rsp_rip(vcpu
);
2228 kvm_set_cr8(vcpu
, vcpu
->arch
.regs
[reg
]);
2229 skip_emulated_instruction(vcpu
);
2230 if (irqchip_in_kernel(vcpu
->kvm
))
2232 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2237 vcpu_load_rsp_rip(vcpu
);
2238 vmx_fpu_deactivate(vcpu
);
2239 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2240 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2241 vmx_fpu_activate(vcpu
);
2242 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2243 skip_emulated_instruction(vcpu
);
2245 case 1: /*mov from cr*/
2248 vcpu_load_rsp_rip(vcpu
);
2249 vcpu
->arch
.regs
[reg
] = vcpu
->arch
.cr3
;
2250 vcpu_put_rsp_rip(vcpu
);
2251 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
,
2252 (u32
)vcpu
->arch
.regs
[reg
],
2253 (u32
)((u64
)vcpu
->arch
.regs
[reg
] >> 32),
2255 skip_emulated_instruction(vcpu
);
2258 vcpu_load_rsp_rip(vcpu
);
2259 vcpu
->arch
.regs
[reg
] = kvm_get_cr8(vcpu
);
2260 vcpu_put_rsp_rip(vcpu
);
2261 KVMTRACE_2D(CR_READ
, vcpu
, (u32
)cr
,
2262 (u32
)vcpu
->arch
.regs
[reg
], handler
);
2263 skip_emulated_instruction(vcpu
);
2268 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2270 skip_emulated_instruction(vcpu
);
2275 kvm_run
->exit_reason
= 0;
2276 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2277 (int)(exit_qualification
>> 4) & 3, cr
);
2281 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2283 unsigned long exit_qualification
;
2288 * FIXME: this code assumes the host is debugging the guest.
2289 * need to deal with guest debugging itself too.
2291 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2292 dr
= exit_qualification
& 7;
2293 reg
= (exit_qualification
>> 8) & 15;
2294 vcpu_load_rsp_rip(vcpu
);
2295 if (exit_qualification
& 16) {
2307 vcpu
->arch
.regs
[reg
] = val
;
2308 KVMTRACE_2D(DR_READ
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2312 vcpu_put_rsp_rip(vcpu
);
2313 skip_emulated_instruction(vcpu
);
2317 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2319 kvm_emulate_cpuid(vcpu
);
2323 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2325 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2328 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
2329 kvm_inject_gp(vcpu
, 0);
2333 KVMTRACE_3D(MSR_READ
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2336 /* FIXME: handling of bits 32:63 of rax, rdx */
2337 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
2338 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
2339 skip_emulated_instruction(vcpu
);
2343 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2345 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2346 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
2347 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2349 KVMTRACE_3D(MSR_WRITE
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2352 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
2353 kvm_inject_gp(vcpu
, 0);
2357 skip_emulated_instruction(vcpu
);
2361 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
2362 struct kvm_run
*kvm_run
)
2367 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
2368 struct kvm_run
*kvm_run
)
2370 u32 cpu_based_vm_exec_control
;
2372 /* clear pending irq */
2373 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2374 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2375 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2377 KVMTRACE_0D(PEND_INTR
, vcpu
, handler
);
2380 * If the user space waits to inject interrupts, exit as soon as
2383 if (kvm_run
->request_interrupt_window
&&
2384 !vcpu
->arch
.irq_summary
) {
2385 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2386 ++vcpu
->stat
.irq_window_exits
;
2392 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2394 skip_emulated_instruction(vcpu
);
2395 return kvm_emulate_halt(vcpu
);
2398 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2400 skip_emulated_instruction(vcpu
);
2401 kvm_emulate_hypercall(vcpu
);
2405 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2407 skip_emulated_instruction(vcpu
);
2408 /* TODO: Add support for VT-d/pass-through device */
2412 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2414 u64 exit_qualification
;
2415 enum emulation_result er
;
2416 unsigned long offset
;
2418 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2419 offset
= exit_qualification
& 0xffful
;
2421 KVMTRACE_1D(APIC_ACCESS
, vcpu
, (u32
)offset
, handler
);
2423 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
2425 if (er
!= EMULATE_DONE
) {
2427 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2434 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2436 unsigned long exit_qualification
;
2440 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2442 reason
= (u32
)exit_qualification
>> 30;
2443 tss_selector
= exit_qualification
;
2445 return kvm_task_switch(vcpu
, tss_selector
, reason
);
2449 * The exit handlers return 1 if the exit was handled fully and guest execution
2450 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2451 * to be done to userspace and return 0.
2453 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
2454 struct kvm_run
*kvm_run
) = {
2455 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
2456 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
2457 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
2458 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
2459 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
2460 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
2461 [EXIT_REASON_CPUID
] = handle_cpuid
,
2462 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
2463 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
2464 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
2465 [EXIT_REASON_HLT
] = handle_halt
,
2466 [EXIT_REASON_VMCALL
] = handle_vmcall
,
2467 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
2468 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
2469 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
2470 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
2473 static const int kvm_vmx_max_exit_handlers
=
2474 ARRAY_SIZE(kvm_vmx_exit_handlers
);
2477 * The guest has exited. See if we can fix it or if we need userspace
2480 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
2482 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
2483 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2484 u32 vectoring_info
= vmx
->idt_vectoring_info
;
2486 KVMTRACE_3D(VMEXIT
, vcpu
, exit_reason
, (u32
)vmcs_readl(GUEST_RIP
),
2487 (u32
)((u64
)vmcs_readl(GUEST_RIP
) >> 32), entryexit
);
2489 if (unlikely(vmx
->fail
)) {
2490 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
2491 kvm_run
->fail_entry
.hardware_entry_failure_reason
2492 = vmcs_read32(VM_INSTRUCTION_ERROR
);
2496 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
2497 exit_reason
!= EXIT_REASON_EXCEPTION_NMI
)
2498 printk(KERN_WARNING
"%s: unexpected, valid vectoring info and "
2499 "exit reason is 0x%x\n", __func__
, exit_reason
);
2500 if (exit_reason
< kvm_vmx_max_exit_handlers
2501 && kvm_vmx_exit_handlers
[exit_reason
])
2502 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
2504 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
2505 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
2510 static void update_tpr_threshold(struct kvm_vcpu
*vcpu
)
2514 if (!vm_need_tpr_shadow(vcpu
->kvm
))
2517 if (!kvm_lapic_enabled(vcpu
) ||
2518 ((max_irr
= kvm_lapic_find_highest_irr(vcpu
)) == -1)) {
2519 vmcs_write32(TPR_THRESHOLD
, 0);
2523 tpr
= (kvm_lapic_get_cr8(vcpu
) & 0x0f) << 4;
2524 vmcs_write32(TPR_THRESHOLD
, (max_irr
> tpr
) ? tpr
>> 4 : max_irr
>> 4);
2527 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2529 u32 cpu_based_vm_exec_control
;
2531 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2532 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2533 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2536 static void vmx_intr_assist(struct kvm_vcpu
*vcpu
)
2538 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2539 u32 idtv_info_field
, intr_info_field
;
2540 int has_ext_irq
, interrupt_window_open
;
2543 update_tpr_threshold(vcpu
);
2545 has_ext_irq
= kvm_cpu_has_interrupt(vcpu
);
2546 intr_info_field
= vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
);
2547 idtv_info_field
= vmx
->idt_vectoring_info
;
2548 if (intr_info_field
& INTR_INFO_VALID_MASK
) {
2549 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
2550 /* TODO: fault when IDT_Vectoring */
2551 if (printk_ratelimit())
2552 printk(KERN_ERR
"Fault when IDT_Vectoring\n");
2555 enable_irq_window(vcpu
);
2558 if (unlikely(idtv_info_field
& INTR_INFO_VALID_MASK
)) {
2559 if ((idtv_info_field
& VECTORING_INFO_TYPE_MASK
)
2560 == INTR_TYPE_EXT_INTR
2561 && vcpu
->arch
.rmode
.active
) {
2562 u8 vect
= idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
2564 vmx_inject_irq(vcpu
, vect
);
2565 if (unlikely(has_ext_irq
))
2566 enable_irq_window(vcpu
);
2570 KVMTRACE_1D(REDELIVER_EVT
, vcpu
, idtv_info_field
, handler
);
2572 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, idtv_info_field
);
2573 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2574 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
));
2576 if (unlikely(idtv_info_field
& INTR_INFO_DELIVER_CODE_MASK
))
2577 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
,
2578 vmcs_read32(IDT_VECTORING_ERROR_CODE
));
2579 if (unlikely(has_ext_irq
))
2580 enable_irq_window(vcpu
);
2585 interrupt_window_open
=
2586 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2587 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
2588 if (interrupt_window_open
) {
2589 vector
= kvm_cpu_get_interrupt(vcpu
);
2590 vmx_inject_irq(vcpu
, vector
);
2591 kvm_timer_intr_post(vcpu
, vector
);
2593 enable_irq_window(vcpu
);
2597 * Failure to inject an interrupt should give us the information
2598 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2599 * when fetching the interrupt redirection bitmap in the real-mode
2600 * tss, this doesn't happen. So we do it ourselves.
2602 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
2604 vmx
->rmode
.irq
.pending
= 0;
2605 if (vmcs_readl(GUEST_RIP
) + 1 != vmx
->rmode
.irq
.rip
)
2607 vmcs_writel(GUEST_RIP
, vmx
->rmode
.irq
.rip
);
2608 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
2609 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
2610 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
2613 vmx
->idt_vectoring_info
=
2614 VECTORING_INFO_VALID_MASK
2615 | INTR_TYPE_EXT_INTR
2616 | vmx
->rmode
.irq
.vector
;
2619 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2621 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2625 * Loading guest fpu may have cleared host cr0.ts
2627 vmcs_writel(HOST_CR0
, read_cr0());
2630 /* Store host registers */
2631 #ifdef CONFIG_X86_64
2632 "push %%rdx; push %%rbp;"
2635 "push %%edx; push %%ebp;"
2638 ASM_VMX_VMWRITE_RSP_RDX
"\n\t"
2639 /* Check if vmlaunch of vmresume is needed */
2640 "cmpl $0, %c[launched](%0) \n\t"
2641 /* Load guest registers. Don't clobber flags. */
2642 #ifdef CONFIG_X86_64
2643 "mov %c[cr2](%0), %%rax \n\t"
2644 "mov %%rax, %%cr2 \n\t"
2645 "mov %c[rax](%0), %%rax \n\t"
2646 "mov %c[rbx](%0), %%rbx \n\t"
2647 "mov %c[rdx](%0), %%rdx \n\t"
2648 "mov %c[rsi](%0), %%rsi \n\t"
2649 "mov %c[rdi](%0), %%rdi \n\t"
2650 "mov %c[rbp](%0), %%rbp \n\t"
2651 "mov %c[r8](%0), %%r8 \n\t"
2652 "mov %c[r9](%0), %%r9 \n\t"
2653 "mov %c[r10](%0), %%r10 \n\t"
2654 "mov %c[r11](%0), %%r11 \n\t"
2655 "mov %c[r12](%0), %%r12 \n\t"
2656 "mov %c[r13](%0), %%r13 \n\t"
2657 "mov %c[r14](%0), %%r14 \n\t"
2658 "mov %c[r15](%0), %%r15 \n\t"
2659 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
2661 "mov %c[cr2](%0), %%eax \n\t"
2662 "mov %%eax, %%cr2 \n\t"
2663 "mov %c[rax](%0), %%eax \n\t"
2664 "mov %c[rbx](%0), %%ebx \n\t"
2665 "mov %c[rdx](%0), %%edx \n\t"
2666 "mov %c[rsi](%0), %%esi \n\t"
2667 "mov %c[rdi](%0), %%edi \n\t"
2668 "mov %c[rbp](%0), %%ebp \n\t"
2669 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
2671 /* Enter guest mode */
2672 "jne .Llaunched \n\t"
2673 ASM_VMX_VMLAUNCH
"\n\t"
2674 "jmp .Lkvm_vmx_return \n\t"
2675 ".Llaunched: " ASM_VMX_VMRESUME
"\n\t"
2676 ".Lkvm_vmx_return: "
2677 /* Save guest registers, load host registers, keep flags */
2678 #ifdef CONFIG_X86_64
2679 "xchg %0, (%%rsp) \n\t"
2680 "mov %%rax, %c[rax](%0) \n\t"
2681 "mov %%rbx, %c[rbx](%0) \n\t"
2682 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2683 "mov %%rdx, %c[rdx](%0) \n\t"
2684 "mov %%rsi, %c[rsi](%0) \n\t"
2685 "mov %%rdi, %c[rdi](%0) \n\t"
2686 "mov %%rbp, %c[rbp](%0) \n\t"
2687 "mov %%r8, %c[r8](%0) \n\t"
2688 "mov %%r9, %c[r9](%0) \n\t"
2689 "mov %%r10, %c[r10](%0) \n\t"
2690 "mov %%r11, %c[r11](%0) \n\t"
2691 "mov %%r12, %c[r12](%0) \n\t"
2692 "mov %%r13, %c[r13](%0) \n\t"
2693 "mov %%r14, %c[r14](%0) \n\t"
2694 "mov %%r15, %c[r15](%0) \n\t"
2695 "mov %%cr2, %%rax \n\t"
2696 "mov %%rax, %c[cr2](%0) \n\t"
2698 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
2700 "xchg %0, (%%esp) \n\t"
2701 "mov %%eax, %c[rax](%0) \n\t"
2702 "mov %%ebx, %c[rbx](%0) \n\t"
2703 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2704 "mov %%edx, %c[rdx](%0) \n\t"
2705 "mov %%esi, %c[rsi](%0) \n\t"
2706 "mov %%edi, %c[rdi](%0) \n\t"
2707 "mov %%ebp, %c[rbp](%0) \n\t"
2708 "mov %%cr2, %%eax \n\t"
2709 "mov %%eax, %c[cr2](%0) \n\t"
2711 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
2713 "setbe %c[fail](%0) \n\t"
2714 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
2715 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
2716 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
2717 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
2718 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
2719 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
2720 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
2721 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
2722 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
2723 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
2724 #ifdef CONFIG_X86_64
2725 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
2726 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
2727 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
2728 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
2729 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
2730 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
2731 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
2732 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
2734 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
2736 #ifdef CONFIG_X86_64
2737 , "rbx", "rdi", "rsi"
2738 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2740 , "ebx", "edi", "rsi"
2744 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
2745 if (vmx
->rmode
.irq
.pending
)
2746 fixup_rmode_irq(vmx
);
2748 vcpu
->arch
.interrupt_window_open
=
2749 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0;
2751 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
2754 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2756 /* We need to handle NMIs before interrupts are enabled */
2757 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) { /* nmi */
2758 KVMTRACE_0D(NMI
, vcpu
, handler
);
2763 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
2765 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2768 on_each_cpu(__vcpu_clear
, vmx
, 0, 1);
2769 free_vmcs(vmx
->vmcs
);
2774 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
2776 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2778 spin_lock(&vmx_vpid_lock
);
2780 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
2781 spin_unlock(&vmx_vpid_lock
);
2782 vmx_free_vmcs(vcpu
);
2783 kfree(vmx
->host_msrs
);
2784 kfree(vmx
->guest_msrs
);
2785 kvm_vcpu_uninit(vcpu
);
2786 kmem_cache_free(kvm_vcpu_cache
, vmx
);
2789 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
2792 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
2796 return ERR_PTR(-ENOMEM
);
2800 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
2804 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2805 if (!vmx
->guest_msrs
) {
2810 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2811 if (!vmx
->host_msrs
)
2812 goto free_guest_msrs
;
2814 vmx
->vmcs
= alloc_vmcs();
2818 vmcs_clear(vmx
->vmcs
);
2821 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
2822 err
= vmx_vcpu_setup(vmx
);
2823 vmx_vcpu_put(&vmx
->vcpu
);
2827 if (vm_need_virtualize_apic_accesses(kvm
))
2828 if (alloc_apic_access_page(kvm
) != 0)
2832 if (alloc_identity_pagetable(kvm
) != 0)
2838 free_vmcs(vmx
->vmcs
);
2840 kfree(vmx
->host_msrs
);
2842 kfree(vmx
->guest_msrs
);
2844 kvm_vcpu_uninit(&vmx
->vcpu
);
2846 kmem_cache_free(kvm_vcpu_cache
, vmx
);
2847 return ERR_PTR(err
);
2850 static void __init
vmx_check_processor_compat(void *rtn
)
2852 struct vmcs_config vmcs_conf
;
2855 if (setup_vmcs_config(&vmcs_conf
) < 0)
2857 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
2858 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
2859 smp_processor_id());
2864 static int get_ept_level(void)
2866 return VMX_EPT_DEFAULT_GAW
+ 1;
2869 static struct kvm_x86_ops vmx_x86_ops
= {
2870 .cpu_has_kvm_support
= cpu_has_kvm_support
,
2871 .disabled_by_bios
= vmx_disabled_by_bios
,
2872 .hardware_setup
= hardware_setup
,
2873 .hardware_unsetup
= hardware_unsetup
,
2874 .check_processor_compatibility
= vmx_check_processor_compat
,
2875 .hardware_enable
= hardware_enable
,
2876 .hardware_disable
= hardware_disable
,
2877 .cpu_has_accelerated_tpr
= cpu_has_vmx_virtualize_apic_accesses
,
2879 .vcpu_create
= vmx_create_vcpu
,
2880 .vcpu_free
= vmx_free_vcpu
,
2881 .vcpu_reset
= vmx_vcpu_reset
,
2883 .prepare_guest_switch
= vmx_save_host_state
,
2884 .vcpu_load
= vmx_vcpu_load
,
2885 .vcpu_put
= vmx_vcpu_put
,
2886 .vcpu_decache
= vmx_vcpu_decache
,
2888 .set_guest_debug
= set_guest_debug
,
2889 .guest_debug_pre
= kvm_guest_debug_pre
,
2890 .get_msr
= vmx_get_msr
,
2891 .set_msr
= vmx_set_msr
,
2892 .get_segment_base
= vmx_get_segment_base
,
2893 .get_segment
= vmx_get_segment
,
2894 .set_segment
= vmx_set_segment
,
2895 .get_cpl
= vmx_get_cpl
,
2896 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
2897 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
2898 .set_cr0
= vmx_set_cr0
,
2899 .set_cr3
= vmx_set_cr3
,
2900 .set_cr4
= vmx_set_cr4
,
2901 .set_efer
= vmx_set_efer
,
2902 .get_idt
= vmx_get_idt
,
2903 .set_idt
= vmx_set_idt
,
2904 .get_gdt
= vmx_get_gdt
,
2905 .set_gdt
= vmx_set_gdt
,
2906 .cache_regs
= vcpu_load_rsp_rip
,
2907 .decache_regs
= vcpu_put_rsp_rip
,
2908 .get_rflags
= vmx_get_rflags
,
2909 .set_rflags
= vmx_set_rflags
,
2911 .tlb_flush
= vmx_flush_tlb
,
2913 .run
= vmx_vcpu_run
,
2914 .handle_exit
= kvm_handle_exit
,
2915 .skip_emulated_instruction
= skip_emulated_instruction
,
2916 .patch_hypercall
= vmx_patch_hypercall
,
2917 .get_irq
= vmx_get_irq
,
2918 .set_irq
= vmx_inject_irq
,
2919 .queue_exception
= vmx_queue_exception
,
2920 .exception_injected
= vmx_exception_injected
,
2921 .inject_pending_irq
= vmx_intr_assist
,
2922 .inject_pending_vectors
= do_interrupt_requests
,
2924 .set_tss_addr
= vmx_set_tss_addr
,
2925 .get_tdp_level
= get_ept_level
,
2928 static int __init
vmx_init(void)
2933 vmx_io_bitmap_a
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2934 if (!vmx_io_bitmap_a
)
2937 vmx_io_bitmap_b
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2938 if (!vmx_io_bitmap_b
) {
2943 vmx_msr_bitmap
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2944 if (!vmx_msr_bitmap
) {
2950 * Allow direct access to the PC debug port (it is often used for I/O
2951 * delays, but the vmexits simply slow things down).
2953 va
= kmap(vmx_io_bitmap_a
);
2954 memset(va
, 0xff, PAGE_SIZE
);
2955 clear_bit(0x80, va
);
2956 kunmap(vmx_io_bitmap_a
);
2958 va
= kmap(vmx_io_bitmap_b
);
2959 memset(va
, 0xff, PAGE_SIZE
);
2960 kunmap(vmx_io_bitmap_b
);
2962 va
= kmap(vmx_msr_bitmap
);
2963 memset(va
, 0xff, PAGE_SIZE
);
2964 kunmap(vmx_msr_bitmap
);
2966 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
2968 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
2972 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_FS_BASE
);
2973 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_GS_BASE
);
2974 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_CS
);
2975 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_ESP
);
2976 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_EIP
);
2978 if (bypass_guest_pf
)
2979 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
2984 __free_page(vmx_msr_bitmap
);
2986 __free_page(vmx_io_bitmap_b
);
2988 __free_page(vmx_io_bitmap_a
);
2992 static void __exit
vmx_exit(void)
2994 __free_page(vmx_msr_bitmap
);
2995 __free_page(vmx_io_bitmap_b
);
2996 __free_page(vmx_io_bitmap_a
);
3001 module_init(vmx_init
)
3002 module_exit(vmx_exit
)