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.
21 #include <linux/kvm_host.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/highmem.h>
26 #include <linux/sched.h>
27 #include <linux/moduleparam.h>
28 #include <linux/ftrace_event.h>
29 #include "kvm_cache_regs.h"
35 #include <asm/virtext.h>
40 #define __ex(x) __kvm_handle_fault_on_reboot(x)
42 MODULE_AUTHOR("Qumranet");
43 MODULE_LICENSE("GPL");
45 static int __read_mostly bypass_guest_pf
= 1;
46 module_param(bypass_guest_pf
, bool, S_IRUGO
);
48 static int __read_mostly enable_vpid
= 1;
49 module_param_named(vpid
, enable_vpid
, bool, 0444);
51 static int __read_mostly flexpriority_enabled
= 1;
52 module_param_named(flexpriority
, flexpriority_enabled
, bool, S_IRUGO
);
54 static int __read_mostly enable_ept
= 1;
55 module_param_named(ept
, enable_ept
, bool, S_IRUGO
);
57 static int __read_mostly enable_unrestricted_guest
= 1;
58 module_param_named(unrestricted_guest
,
59 enable_unrestricted_guest
, bool, S_IRUGO
);
61 static int __read_mostly emulate_invalid_guest_state
= 0;
62 module_param(emulate_invalid_guest_state
, bool, S_IRUGO
);
72 struct list_head local_vcpus_link
;
73 unsigned long host_rsp
;
76 u32 idt_vectoring_info
;
77 struct kvm_msr_entry
*guest_msrs
;
78 struct kvm_msr_entry
*host_msrs
;
83 int msr_offset_kernel_gs_base
;
88 u16 fs_sel
, gs_sel
, ldt_sel
;
89 int gs_ldt_reload_needed
;
91 int guest_efer_loaded
;
96 struct kvm_save_segment
{
101 } tr
, es
, ds
, fs
, gs
;
109 bool emulation_required
;
110 enum emulation_result invalid_state_emulation_result
;
112 /* Support for vnmi-less CPUs */
113 int soft_vnmi_blocked
;
115 s64 vnmi_blocked_time
;
119 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
121 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
124 static int init_rmode(struct kvm
*kvm
);
125 static u64
construct_eptp(unsigned long root_hpa
);
127 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
128 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
129 static DEFINE_PER_CPU(struct list_head
, vcpus_on_cpu
);
131 static unsigned long *vmx_io_bitmap_a
;
132 static unsigned long *vmx_io_bitmap_b
;
133 static unsigned long *vmx_msr_bitmap_legacy
;
134 static unsigned long *vmx_msr_bitmap_longmode
;
136 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
137 static DEFINE_SPINLOCK(vmx_vpid_lock
);
139 static struct vmcs_config
{
143 u32 pin_based_exec_ctrl
;
144 u32 cpu_based_exec_ctrl
;
145 u32 cpu_based_2nd_exec_ctrl
;
150 static struct vmx_capability
{
155 #define VMX_SEGMENT_FIELD(seg) \
156 [VCPU_SREG_##seg] = { \
157 .selector = GUEST_##seg##_SELECTOR, \
158 .base = GUEST_##seg##_BASE, \
159 .limit = GUEST_##seg##_LIMIT, \
160 .ar_bytes = GUEST_##seg##_AR_BYTES, \
163 static struct kvm_vmx_segment_field
{
168 } kvm_vmx_segment_fields
[] = {
169 VMX_SEGMENT_FIELD(CS
),
170 VMX_SEGMENT_FIELD(DS
),
171 VMX_SEGMENT_FIELD(ES
),
172 VMX_SEGMENT_FIELD(FS
),
173 VMX_SEGMENT_FIELD(GS
),
174 VMX_SEGMENT_FIELD(SS
),
175 VMX_SEGMENT_FIELD(TR
),
176 VMX_SEGMENT_FIELD(LDTR
),
179 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
);
182 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
183 * away by decrementing the array size.
185 static const u32 vmx_msr_index
[] = {
187 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
189 MSR_EFER
, MSR_K6_STAR
,
191 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
193 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
197 for (i
= 0; i
< n
; ++i
)
198 wrmsrl(e
[i
].index
, e
[i
].data
);
201 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
205 for (i
= 0; i
< n
; ++i
)
206 rdmsrl(e
[i
].index
, e
[i
].data
);
209 static inline int is_page_fault(u32 intr_info
)
211 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
212 INTR_INFO_VALID_MASK
)) ==
213 (INTR_TYPE_HARD_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
216 static inline int is_no_device(u32 intr_info
)
218 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
219 INTR_INFO_VALID_MASK
)) ==
220 (INTR_TYPE_HARD_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
223 static inline int is_invalid_opcode(u32 intr_info
)
225 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
226 INTR_INFO_VALID_MASK
)) ==
227 (INTR_TYPE_HARD_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
230 static inline int is_external_interrupt(u32 intr_info
)
232 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
233 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
236 static inline int is_machine_check(u32 intr_info
)
238 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
239 INTR_INFO_VALID_MASK
)) ==
240 (INTR_TYPE_HARD_EXCEPTION
| MC_VECTOR
| INTR_INFO_VALID_MASK
);
243 static inline int cpu_has_vmx_msr_bitmap(void)
245 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
;
248 static inline int cpu_has_vmx_tpr_shadow(void)
250 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
;
253 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
255 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
));
258 static inline int cpu_has_secondary_exec_ctrls(void)
260 return vmcs_config
.cpu_based_exec_ctrl
&
261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
264 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
266 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
267 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
270 static inline bool cpu_has_vmx_flexpriority(void)
272 return cpu_has_vmx_tpr_shadow() &&
273 cpu_has_vmx_virtualize_apic_accesses();
276 static inline bool cpu_has_vmx_ept_execute_only(void)
278 return !!(vmx_capability
.ept
& VMX_EPT_EXECUTE_ONLY_BIT
);
281 static inline bool cpu_has_vmx_eptp_uncacheable(void)
283 return !!(vmx_capability
.ept
& VMX_EPTP_UC_BIT
);
286 static inline bool cpu_has_vmx_eptp_writeback(void)
288 return !!(vmx_capability
.ept
& VMX_EPTP_WB_BIT
);
291 static inline bool cpu_has_vmx_ept_2m_page(void)
293 return !!(vmx_capability
.ept
& VMX_EPT_2MB_PAGE_BIT
);
296 static inline int cpu_has_vmx_invept_individual_addr(void)
298 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
);
301 static inline int cpu_has_vmx_invept_context(void)
303 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
);
306 static inline int cpu_has_vmx_invept_global(void)
308 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
);
311 static inline int cpu_has_vmx_ept(void)
313 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
314 SECONDARY_EXEC_ENABLE_EPT
;
317 static inline int cpu_has_vmx_unrestricted_guest(void)
319 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
320 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
323 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
325 return flexpriority_enabled
&&
326 (cpu_has_vmx_virtualize_apic_accesses()) &&
327 (irqchip_in_kernel(kvm
));
330 static inline int cpu_has_vmx_vpid(void)
332 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
333 SECONDARY_EXEC_ENABLE_VPID
;
336 static inline int cpu_has_virtual_nmis(void)
338 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
341 static inline bool report_flexpriority(void)
343 return flexpriority_enabled
;
346 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
350 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
351 if (vmx
->guest_msrs
[i
].index
== msr
)
356 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
362 } operand
= { vpid
, 0, gva
};
364 asm volatile (__ex(ASM_VMX_INVVPID
)
365 /* CF==1 or ZF==1 --> rc = -1 */
367 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
370 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
374 } operand
= {eptp
, gpa
};
376 asm volatile (__ex(ASM_VMX_INVEPT
)
377 /* CF==1 or ZF==1 --> rc = -1 */
378 "; ja 1f ; ud2 ; 1:\n"
379 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
382 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
386 i
= __find_msr_index(vmx
, msr
);
388 return &vmx
->guest_msrs
[i
];
392 static void vmcs_clear(struct vmcs
*vmcs
)
394 u64 phys_addr
= __pa(vmcs
);
397 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
398 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
401 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
405 static void __vcpu_clear(void *arg
)
407 struct vcpu_vmx
*vmx
= arg
;
408 int cpu
= raw_smp_processor_id();
410 if (vmx
->vcpu
.cpu
== cpu
)
411 vmcs_clear(vmx
->vmcs
);
412 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
413 per_cpu(current_vmcs
, cpu
) = NULL
;
414 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
415 list_del(&vmx
->local_vcpus_link
);
420 static void vcpu_clear(struct vcpu_vmx
*vmx
)
422 if (vmx
->vcpu
.cpu
== -1)
424 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 1);
427 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
432 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
435 static inline void ept_sync_global(void)
437 if (cpu_has_vmx_invept_global())
438 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
441 static inline void ept_sync_context(u64 eptp
)
444 if (cpu_has_vmx_invept_context())
445 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
451 static inline void ept_sync_individual_addr(u64 eptp
, gpa_t gpa
)
454 if (cpu_has_vmx_invept_individual_addr())
455 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR
,
458 ept_sync_context(eptp
);
462 static unsigned long vmcs_readl(unsigned long field
)
466 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX
)
467 : "=a"(value
) : "d"(field
) : "cc");
471 static u16
vmcs_read16(unsigned long field
)
473 return vmcs_readl(field
);
476 static u32
vmcs_read32(unsigned long field
)
478 return vmcs_readl(field
);
481 static u64
vmcs_read64(unsigned long field
)
484 return vmcs_readl(field
);
486 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
490 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
492 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
493 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
497 static void vmcs_writel(unsigned long field
, unsigned long value
)
501 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
502 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
504 vmwrite_error(field
, value
);
507 static void vmcs_write16(unsigned long field
, u16 value
)
509 vmcs_writel(field
, value
);
512 static void vmcs_write32(unsigned long field
, u32 value
)
514 vmcs_writel(field
, value
);
517 static void vmcs_write64(unsigned long field
, u64 value
)
519 vmcs_writel(field
, value
);
520 #ifndef CONFIG_X86_64
522 vmcs_writel(field
+1, value
>> 32);
526 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
528 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
531 static void vmcs_set_bits(unsigned long field
, u32 mask
)
533 vmcs_writel(field
, vmcs_readl(field
) | mask
);
536 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
540 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
) | (1u << MC_VECTOR
);
541 if (!vcpu
->fpu_active
)
542 eb
|= 1u << NM_VECTOR
;
543 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
544 if (vcpu
->guest_debug
&
545 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
546 eb
|= 1u << DB_VECTOR
;
547 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
548 eb
|= 1u << BP_VECTOR
;
550 if (to_vmx(vcpu
)->rmode
.vm86_active
)
553 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
554 vmcs_write32(EXCEPTION_BITMAP
, eb
);
557 static void reload_tss(void)
560 * VT restores TR but not its size. Useless.
562 struct descriptor_table gdt
;
563 struct desc_struct
*descs
;
566 descs
= (void *)gdt
.base
;
567 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
571 static void load_transition_efer(struct vcpu_vmx
*vmx
)
573 int efer_offset
= vmx
->msr_offset_efer
;
580 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
581 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
584 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
587 ignore_bits
= EFER_NX
| EFER_SCE
;
589 ignore_bits
|= EFER_LMA
| EFER_LME
;
590 /* SCE is meaningful only in long mode on Intel */
591 if (guest_efer
& EFER_LMA
)
592 ignore_bits
&= ~(u64
)EFER_SCE
;
594 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
597 vmx
->host_state
.guest_efer_loaded
= 1;
598 guest_efer
&= ~ignore_bits
;
599 guest_efer
|= host_efer
& ignore_bits
;
600 wrmsrl(MSR_EFER
, guest_efer
);
601 vmx
->vcpu
.stat
.efer_reload
++;
604 static void reload_host_efer(struct vcpu_vmx
*vmx
)
606 if (vmx
->host_state
.guest_efer_loaded
) {
607 vmx
->host_state
.guest_efer_loaded
= 0;
608 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
612 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
614 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
616 if (vmx
->host_state
.loaded
)
619 vmx
->host_state
.loaded
= 1;
621 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
622 * allow segment selectors with cpl > 0 or ti == 1.
624 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
625 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
626 vmx
->host_state
.fs_sel
= kvm_read_fs();
627 if (!(vmx
->host_state
.fs_sel
& 7)) {
628 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
629 vmx
->host_state
.fs_reload_needed
= 0;
631 vmcs_write16(HOST_FS_SELECTOR
, 0);
632 vmx
->host_state
.fs_reload_needed
= 1;
634 vmx
->host_state
.gs_sel
= kvm_read_gs();
635 if (!(vmx
->host_state
.gs_sel
& 7))
636 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
638 vmcs_write16(HOST_GS_SELECTOR
, 0);
639 vmx
->host_state
.gs_ldt_reload_needed
= 1;
643 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
644 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
646 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
647 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
651 if (is_long_mode(&vmx
->vcpu
))
652 save_msrs(vmx
->host_msrs
+
653 vmx
->msr_offset_kernel_gs_base
, 1);
656 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
657 load_transition_efer(vmx
);
660 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
664 if (!vmx
->host_state
.loaded
)
667 ++vmx
->vcpu
.stat
.host_state_reload
;
668 vmx
->host_state
.loaded
= 0;
669 if (vmx
->host_state
.fs_reload_needed
)
670 kvm_load_fs(vmx
->host_state
.fs_sel
);
671 if (vmx
->host_state
.gs_ldt_reload_needed
) {
672 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
674 * If we have to reload gs, we must take care to
675 * preserve our gs base.
677 local_irq_save(flags
);
678 kvm_load_gs(vmx
->host_state
.gs_sel
);
680 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
682 local_irq_restore(flags
);
685 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
686 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
687 reload_host_efer(vmx
);
690 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
693 __vmx_load_host_state(vmx
);
698 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
699 * vcpu mutex is already taken.
701 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
703 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
704 u64 phys_addr
= __pa(vmx
->vmcs
);
705 u64 tsc_this
, delta
, new_offset
;
707 if (vcpu
->cpu
!= cpu
) {
709 kvm_migrate_timers(vcpu
);
710 vpid_sync_vcpu_all(vmx
);
712 list_add(&vmx
->local_vcpus_link
,
713 &per_cpu(vcpus_on_cpu
, cpu
));
717 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
720 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
721 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
722 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
725 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
726 vmx
->vmcs
, phys_addr
);
729 if (vcpu
->cpu
!= cpu
) {
730 struct descriptor_table dt
;
731 unsigned long sysenter_esp
;
735 * Linux uses per-cpu TSS and GDT, so set these when switching
738 vmcs_writel(HOST_TR_BASE
, kvm_read_tr_base()); /* 22.2.4 */
740 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
742 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
743 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
746 * Make sure the time stamp counter is monotonous.
749 if (tsc_this
< vcpu
->arch
.host_tsc
) {
750 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
751 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
752 vmcs_write64(TSC_OFFSET
, new_offset
);
757 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
759 __vmx_load_host_state(to_vmx(vcpu
));
762 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
764 if (vcpu
->fpu_active
)
766 vcpu
->fpu_active
= 1;
767 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
768 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
769 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
770 update_exception_bitmap(vcpu
);
773 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
775 if (!vcpu
->fpu_active
)
777 vcpu
->fpu_active
= 0;
778 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
779 update_exception_bitmap(vcpu
);
782 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
784 unsigned long rflags
;
786 rflags
= vmcs_readl(GUEST_RFLAGS
);
787 if (to_vmx(vcpu
)->rmode
.vm86_active
)
788 rflags
&= ~(unsigned long)(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
792 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
794 if (to_vmx(vcpu
)->rmode
.vm86_active
)
795 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
796 vmcs_writel(GUEST_RFLAGS
, rflags
);
799 static u32
vmx_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
801 u32 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
804 if (interruptibility
& GUEST_INTR_STATE_STI
)
805 ret
|= X86_SHADOW_INT_STI
;
806 if (interruptibility
& GUEST_INTR_STATE_MOV_SS
)
807 ret
|= X86_SHADOW_INT_MOV_SS
;
812 static void vmx_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
814 u32 interruptibility_old
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
815 u32 interruptibility
= interruptibility_old
;
817 interruptibility
&= ~(GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
);
819 if (mask
& X86_SHADOW_INT_MOV_SS
)
820 interruptibility
|= GUEST_INTR_STATE_MOV_SS
;
821 if (mask
& X86_SHADOW_INT_STI
)
822 interruptibility
|= GUEST_INTR_STATE_STI
;
824 if ((interruptibility
!= interruptibility_old
))
825 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, interruptibility
);
828 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
832 rip
= kvm_rip_read(vcpu
);
833 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
834 kvm_rip_write(vcpu
, rip
);
836 /* skipping an emulated instruction also counts */
837 vmx_set_interrupt_shadow(vcpu
, 0);
840 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
841 bool has_error_code
, u32 error_code
)
843 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
844 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
846 if (has_error_code
) {
847 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
848 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
851 if (vmx
->rmode
.vm86_active
) {
852 vmx
->rmode
.irq
.pending
= true;
853 vmx
->rmode
.irq
.vector
= nr
;
854 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
855 if (kvm_exception_is_soft(nr
))
856 vmx
->rmode
.irq
.rip
+=
857 vmx
->vcpu
.arch
.event_exit_inst_len
;
858 intr_info
|= INTR_TYPE_SOFT_INTR
;
859 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
860 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
861 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
865 if (kvm_exception_is_soft(nr
)) {
866 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
867 vmx
->vcpu
.arch
.event_exit_inst_len
);
868 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
870 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
872 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
876 * Swap MSR entry in host/guest MSR entry array.
879 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
881 struct kvm_msr_entry tmp
;
883 tmp
= vmx
->guest_msrs
[to
];
884 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
885 vmx
->guest_msrs
[from
] = tmp
;
886 tmp
= vmx
->host_msrs
[to
];
887 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
888 vmx
->host_msrs
[from
] = tmp
;
893 * Set up the vmcs to automatically save and restore system
894 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
895 * mode, as fiddling with msrs is very expensive.
897 static void setup_msrs(struct vcpu_vmx
*vmx
)
900 unsigned long *msr_bitmap
;
902 vmx_load_host_state(vmx
);
905 if (is_long_mode(&vmx
->vcpu
)) {
908 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
910 move_msr_up(vmx
, index
, save_nmsrs
++);
911 index
= __find_msr_index(vmx
, MSR_LSTAR
);
913 move_msr_up(vmx
, index
, save_nmsrs
++);
914 index
= __find_msr_index(vmx
, MSR_CSTAR
);
916 move_msr_up(vmx
, index
, save_nmsrs
++);
917 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
919 move_msr_up(vmx
, index
, save_nmsrs
++);
921 * MSR_K6_STAR is only needed on long mode guests, and only
922 * if efer.sce is enabled.
924 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
925 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
926 move_msr_up(vmx
, index
, save_nmsrs
++);
929 vmx
->save_nmsrs
= save_nmsrs
;
932 vmx
->msr_offset_kernel_gs_base
=
933 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
935 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
937 if (cpu_has_vmx_msr_bitmap()) {
938 if (is_long_mode(&vmx
->vcpu
))
939 msr_bitmap
= vmx_msr_bitmap_longmode
;
941 msr_bitmap
= vmx_msr_bitmap_legacy
;
943 vmcs_write64(MSR_BITMAP
, __pa(msr_bitmap
));
948 * reads and returns guest's timestamp counter "register"
949 * guest_tsc = host_tsc + tsc_offset -- 21.3
951 static u64
guest_read_tsc(void)
953 u64 host_tsc
, tsc_offset
;
956 tsc_offset
= vmcs_read64(TSC_OFFSET
);
957 return host_tsc
+ tsc_offset
;
961 * writes 'guest_tsc' into guest's timestamp counter "register"
962 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
964 static void guest_write_tsc(u64 guest_tsc
, u64 host_tsc
)
966 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
970 * Reads an msr value (of 'msr_index') into 'pdata'.
971 * Returns 0 on success, non-0 otherwise.
972 * Assumes vcpu_load() was already called.
974 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
977 struct kvm_msr_entry
*msr
;
980 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
987 data
= vmcs_readl(GUEST_FS_BASE
);
990 data
= vmcs_readl(GUEST_GS_BASE
);
993 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
996 data
= guest_read_tsc();
998 case MSR_IA32_SYSENTER_CS
:
999 data
= vmcs_read32(GUEST_SYSENTER_CS
);
1001 case MSR_IA32_SYSENTER_EIP
:
1002 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
1004 case MSR_IA32_SYSENTER_ESP
:
1005 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
1008 vmx_load_host_state(to_vmx(vcpu
));
1009 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
1014 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
1022 * Writes msr value into into the appropriate "register".
1023 * Returns 0 on success, non-0 otherwise.
1024 * Assumes vcpu_load() was already called.
1026 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1028 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1029 struct kvm_msr_entry
*msr
;
1033 switch (msr_index
) {
1035 vmx_load_host_state(vmx
);
1036 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
1038 #ifdef CONFIG_X86_64
1040 vmcs_writel(GUEST_FS_BASE
, data
);
1043 vmcs_writel(GUEST_GS_BASE
, data
);
1046 case MSR_IA32_SYSENTER_CS
:
1047 vmcs_write32(GUEST_SYSENTER_CS
, data
);
1049 case MSR_IA32_SYSENTER_EIP
:
1050 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
1052 case MSR_IA32_SYSENTER_ESP
:
1053 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
1057 guest_write_tsc(data
, host_tsc
);
1059 case MSR_IA32_CR_PAT
:
1060 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
1061 vmcs_write64(GUEST_IA32_PAT
, data
);
1062 vcpu
->arch
.pat
= data
;
1065 /* Otherwise falls through to kvm_set_msr_common */
1067 vmx_load_host_state(vmx
);
1068 msr
= find_msr_entry(vmx
, msr_index
);
1073 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
1079 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1081 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
1084 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
1087 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
1089 case VCPU_EXREG_PDPTR
:
1091 ept_save_pdptrs(vcpu
);
1098 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1100 int old_debug
= vcpu
->guest_debug
;
1101 unsigned long flags
;
1103 vcpu
->guest_debug
= dbg
->control
;
1104 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
))
1105 vcpu
->guest_debug
= 0;
1107 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1108 vmcs_writel(GUEST_DR7
, dbg
->arch
.debugreg
[7]);
1110 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
1112 flags
= vmcs_readl(GUEST_RFLAGS
);
1113 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
1114 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
1115 else if (old_debug
& KVM_GUESTDBG_SINGLESTEP
)
1116 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1117 vmcs_writel(GUEST_RFLAGS
, flags
);
1119 update_exception_bitmap(vcpu
);
1124 static __init
int cpu_has_kvm_support(void)
1126 return cpu_has_vmx();
1129 static __init
int vmx_disabled_by_bios(void)
1133 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
1134 return (msr
& (FEATURE_CONTROL_LOCKED
|
1135 FEATURE_CONTROL_VMXON_ENABLED
))
1136 == FEATURE_CONTROL_LOCKED
;
1137 /* locked but not enabled */
1140 static void hardware_enable(void *garbage
)
1142 int cpu
= raw_smp_processor_id();
1143 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
1146 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu
, cpu
));
1147 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
1148 if ((old
& (FEATURE_CONTROL_LOCKED
|
1149 FEATURE_CONTROL_VMXON_ENABLED
))
1150 != (FEATURE_CONTROL_LOCKED
|
1151 FEATURE_CONTROL_VMXON_ENABLED
))
1152 /* enable and lock */
1153 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
1154 FEATURE_CONTROL_LOCKED
|
1155 FEATURE_CONTROL_VMXON_ENABLED
);
1156 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
1157 asm volatile (ASM_VMX_VMXON_RAX
1158 : : "a"(&phys_addr
), "m"(phys_addr
)
1162 static void vmclear_local_vcpus(void)
1164 int cpu
= raw_smp_processor_id();
1165 struct vcpu_vmx
*vmx
, *n
;
1167 list_for_each_entry_safe(vmx
, n
, &per_cpu(vcpus_on_cpu
, cpu
),
1173 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1176 static void kvm_cpu_vmxoff(void)
1178 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
1179 write_cr4(read_cr4() & ~X86_CR4_VMXE
);
1182 static void hardware_disable(void *garbage
)
1184 vmclear_local_vcpus();
1188 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1189 u32 msr
, u32
*result
)
1191 u32 vmx_msr_low
, vmx_msr_high
;
1192 u32 ctl
= ctl_min
| ctl_opt
;
1194 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1196 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1197 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1199 /* Ensure minimum (required) set of control bits are supported. */
1207 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1209 u32 vmx_msr_low
, vmx_msr_high
;
1210 u32 min
, opt
, min2
, opt2
;
1211 u32 _pin_based_exec_control
= 0;
1212 u32 _cpu_based_exec_control
= 0;
1213 u32 _cpu_based_2nd_exec_control
= 0;
1214 u32 _vmexit_control
= 0;
1215 u32 _vmentry_control
= 0;
1217 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1218 opt
= PIN_BASED_VIRTUAL_NMIS
;
1219 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1220 &_pin_based_exec_control
) < 0)
1223 min
= CPU_BASED_HLT_EXITING
|
1224 #ifdef CONFIG_X86_64
1225 CPU_BASED_CR8_LOAD_EXITING
|
1226 CPU_BASED_CR8_STORE_EXITING
|
1228 CPU_BASED_CR3_LOAD_EXITING
|
1229 CPU_BASED_CR3_STORE_EXITING
|
1230 CPU_BASED_USE_IO_BITMAPS
|
1231 CPU_BASED_MOV_DR_EXITING
|
1232 CPU_BASED_USE_TSC_OFFSETING
|
1233 CPU_BASED_INVLPG_EXITING
;
1234 opt
= CPU_BASED_TPR_SHADOW
|
1235 CPU_BASED_USE_MSR_BITMAPS
|
1236 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1237 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1238 &_cpu_based_exec_control
) < 0)
1240 #ifdef CONFIG_X86_64
1241 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1242 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1243 ~CPU_BASED_CR8_STORE_EXITING
;
1245 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1247 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1248 SECONDARY_EXEC_WBINVD_EXITING
|
1249 SECONDARY_EXEC_ENABLE_VPID
|
1250 SECONDARY_EXEC_ENABLE_EPT
|
1251 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
1252 if (adjust_vmx_controls(min2
, opt2
,
1253 MSR_IA32_VMX_PROCBASED_CTLS2
,
1254 &_cpu_based_2nd_exec_control
) < 0)
1257 #ifndef CONFIG_X86_64
1258 if (!(_cpu_based_2nd_exec_control
&
1259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1260 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1262 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1263 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1265 min
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1266 CPU_BASED_CR3_STORE_EXITING
|
1267 CPU_BASED_INVLPG_EXITING
);
1268 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1269 &_cpu_based_exec_control
) < 0)
1271 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1272 vmx_capability
.ept
, vmx_capability
.vpid
);
1276 #ifdef CONFIG_X86_64
1277 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1279 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
;
1280 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1281 &_vmexit_control
) < 0)
1285 opt
= VM_ENTRY_LOAD_IA32_PAT
;
1286 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1287 &_vmentry_control
) < 0)
1290 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1292 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1293 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1296 #ifdef CONFIG_X86_64
1297 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1298 if (vmx_msr_high
& (1u<<16))
1302 /* Require Write-Back (WB) memory type for VMCS accesses. */
1303 if (((vmx_msr_high
>> 18) & 15) != 6)
1306 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1307 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1308 vmcs_conf
->revision_id
= vmx_msr_low
;
1310 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1311 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1312 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1313 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1314 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1319 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1321 int node
= cpu_to_node(cpu
);
1325 pages
= alloc_pages_exact_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1328 vmcs
= page_address(pages
);
1329 memset(vmcs
, 0, vmcs_config
.size
);
1330 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1334 static struct vmcs
*alloc_vmcs(void)
1336 return alloc_vmcs_cpu(raw_smp_processor_id());
1339 static void free_vmcs(struct vmcs
*vmcs
)
1341 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1344 static void free_kvm_area(void)
1348 for_each_online_cpu(cpu
)
1349 free_vmcs(per_cpu(vmxarea
, cpu
));
1352 static __init
int alloc_kvm_area(void)
1356 for_each_online_cpu(cpu
) {
1359 vmcs
= alloc_vmcs_cpu(cpu
);
1365 per_cpu(vmxarea
, cpu
) = vmcs
;
1370 static __init
int hardware_setup(void)
1372 if (setup_vmcs_config(&vmcs_config
) < 0)
1375 if (boot_cpu_has(X86_FEATURE_NX
))
1376 kvm_enable_efer_bits(EFER_NX
);
1378 if (!cpu_has_vmx_vpid())
1381 if (!cpu_has_vmx_ept()) {
1383 enable_unrestricted_guest
= 0;
1386 if (!cpu_has_vmx_unrestricted_guest())
1387 enable_unrestricted_guest
= 0;
1389 if (!cpu_has_vmx_flexpriority())
1390 flexpriority_enabled
= 0;
1392 if (!cpu_has_vmx_tpr_shadow())
1393 kvm_x86_ops
->update_cr8_intercept
= NULL
;
1395 if (enable_ept
&& !cpu_has_vmx_ept_2m_page())
1396 kvm_disable_largepages();
1398 return alloc_kvm_area();
1401 static __exit
void hardware_unsetup(void)
1406 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1408 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1410 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1411 vmcs_write16(sf
->selector
, save
->selector
);
1412 vmcs_writel(sf
->base
, save
->base
);
1413 vmcs_write32(sf
->limit
, save
->limit
);
1414 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1416 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1418 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1422 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1424 unsigned long flags
;
1425 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1427 vmx
->emulation_required
= 1;
1428 vmx
->rmode
.vm86_active
= 0;
1430 vmcs_writel(GUEST_TR_BASE
, vmx
->rmode
.tr
.base
);
1431 vmcs_write32(GUEST_TR_LIMIT
, vmx
->rmode
.tr
.limit
);
1432 vmcs_write32(GUEST_TR_AR_BYTES
, vmx
->rmode
.tr
.ar
);
1434 flags
= vmcs_readl(GUEST_RFLAGS
);
1435 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1436 flags
|= (vmx
->rmode
.save_iopl
<< IOPL_SHIFT
);
1437 vmcs_writel(GUEST_RFLAGS
, flags
);
1439 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1440 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1442 update_exception_bitmap(vcpu
);
1444 if (emulate_invalid_guest_state
)
1447 fix_pmode_dataseg(VCPU_SREG_ES
, &vmx
->rmode
.es
);
1448 fix_pmode_dataseg(VCPU_SREG_DS
, &vmx
->rmode
.ds
);
1449 fix_pmode_dataseg(VCPU_SREG_GS
, &vmx
->rmode
.gs
);
1450 fix_pmode_dataseg(VCPU_SREG_FS
, &vmx
->rmode
.fs
);
1452 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1453 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1455 vmcs_write16(GUEST_CS_SELECTOR
,
1456 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1457 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1460 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1462 if (!kvm
->arch
.tss_addr
) {
1463 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1464 kvm
->memslots
[0].npages
- 3;
1465 return base_gfn
<< PAGE_SHIFT
;
1467 return kvm
->arch
.tss_addr
;
1470 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1472 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1474 save
->selector
= vmcs_read16(sf
->selector
);
1475 save
->base
= vmcs_readl(sf
->base
);
1476 save
->limit
= vmcs_read32(sf
->limit
);
1477 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1478 vmcs_write16(sf
->selector
, save
->base
>> 4);
1479 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1480 vmcs_write32(sf
->limit
, 0xffff);
1481 vmcs_write32(sf
->ar_bytes
, 0xf3);
1484 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1486 unsigned long flags
;
1487 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1489 if (enable_unrestricted_guest
)
1492 vmx
->emulation_required
= 1;
1493 vmx
->rmode
.vm86_active
= 1;
1495 vmx
->rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1496 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1498 vmx
->rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1499 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1501 vmx
->rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1502 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1504 flags
= vmcs_readl(GUEST_RFLAGS
);
1505 vmx
->rmode
.save_iopl
1506 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1508 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1510 vmcs_writel(GUEST_RFLAGS
, flags
);
1511 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1512 update_exception_bitmap(vcpu
);
1514 if (emulate_invalid_guest_state
)
1515 goto continue_rmode
;
1517 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1518 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1519 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1521 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1522 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1523 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1524 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1525 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1527 fix_rmode_seg(VCPU_SREG_ES
, &vmx
->rmode
.es
);
1528 fix_rmode_seg(VCPU_SREG_DS
, &vmx
->rmode
.ds
);
1529 fix_rmode_seg(VCPU_SREG_GS
, &vmx
->rmode
.gs
);
1530 fix_rmode_seg(VCPU_SREG_FS
, &vmx
->rmode
.fs
);
1533 kvm_mmu_reset_context(vcpu
);
1534 init_rmode(vcpu
->kvm
);
1537 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1539 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1540 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1542 vcpu
->arch
.shadow_efer
= efer
;
1545 if (efer
& EFER_LMA
) {
1546 vmcs_write32(VM_ENTRY_CONTROLS
,
1547 vmcs_read32(VM_ENTRY_CONTROLS
) |
1548 VM_ENTRY_IA32E_MODE
);
1551 vmcs_write32(VM_ENTRY_CONTROLS
,
1552 vmcs_read32(VM_ENTRY_CONTROLS
) &
1553 ~VM_ENTRY_IA32E_MODE
);
1555 msr
->data
= efer
& ~EFER_LME
;
1560 #ifdef CONFIG_X86_64
1562 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1566 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1567 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1568 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1570 vmcs_write32(GUEST_TR_AR_BYTES
,
1571 (guest_tr_ar
& ~AR_TYPE_MASK
)
1572 | AR_TYPE_BUSY_64_TSS
);
1574 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1575 vmx_set_efer(vcpu
, vcpu
->arch
.shadow_efer
);
1578 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1580 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1582 vmcs_write32(VM_ENTRY_CONTROLS
,
1583 vmcs_read32(VM_ENTRY_CONTROLS
)
1584 & ~VM_ENTRY_IA32E_MODE
);
1589 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1591 vpid_sync_vcpu_all(to_vmx(vcpu
));
1593 ept_sync_context(construct_eptp(vcpu
->arch
.mmu
.root_hpa
));
1596 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1598 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1599 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1602 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
1604 if (!test_bit(VCPU_EXREG_PDPTR
,
1605 (unsigned long *)&vcpu
->arch
.regs_dirty
))
1608 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1609 vmcs_write64(GUEST_PDPTR0
, vcpu
->arch
.pdptrs
[0]);
1610 vmcs_write64(GUEST_PDPTR1
, vcpu
->arch
.pdptrs
[1]);
1611 vmcs_write64(GUEST_PDPTR2
, vcpu
->arch
.pdptrs
[2]);
1612 vmcs_write64(GUEST_PDPTR3
, vcpu
->arch
.pdptrs
[3]);
1616 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
)
1618 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1619 vcpu
->arch
.pdptrs
[0] = vmcs_read64(GUEST_PDPTR0
);
1620 vcpu
->arch
.pdptrs
[1] = vmcs_read64(GUEST_PDPTR1
);
1621 vcpu
->arch
.pdptrs
[2] = vmcs_read64(GUEST_PDPTR2
);
1622 vcpu
->arch
.pdptrs
[3] = vmcs_read64(GUEST_PDPTR3
);
1625 __set_bit(VCPU_EXREG_PDPTR
,
1626 (unsigned long *)&vcpu
->arch
.regs_avail
);
1627 __set_bit(VCPU_EXREG_PDPTR
,
1628 (unsigned long *)&vcpu
->arch
.regs_dirty
);
1631 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
1633 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
1635 struct kvm_vcpu
*vcpu
)
1637 if (!(cr0
& X86_CR0_PG
)) {
1638 /* From paging/starting to nonpaging */
1639 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1640 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
1641 (CPU_BASED_CR3_LOAD_EXITING
|
1642 CPU_BASED_CR3_STORE_EXITING
));
1643 vcpu
->arch
.cr0
= cr0
;
1644 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1645 *hw_cr0
&= ~X86_CR0_WP
;
1646 } else if (!is_paging(vcpu
)) {
1647 /* From nonpaging to paging */
1648 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1649 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
1650 ~(CPU_BASED_CR3_LOAD_EXITING
|
1651 CPU_BASED_CR3_STORE_EXITING
));
1652 vcpu
->arch
.cr0
= cr0
;
1653 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1654 if (!(vcpu
->arch
.cr0
& X86_CR0_WP
))
1655 *hw_cr0
&= ~X86_CR0_WP
;
1659 static void ept_update_paging_mode_cr4(unsigned long *hw_cr4
,
1660 struct kvm_vcpu
*vcpu
)
1662 if (!is_paging(vcpu
)) {
1663 *hw_cr4
&= ~X86_CR4_PAE
;
1664 *hw_cr4
|= X86_CR4_PSE
;
1665 } else if (!(vcpu
->arch
.cr4
& X86_CR4_PAE
))
1666 *hw_cr4
&= ~X86_CR4_PAE
;
1669 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1671 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1672 unsigned long hw_cr0
;
1674 if (enable_unrestricted_guest
)
1675 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST
)
1676 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST
;
1678 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
) | KVM_VM_CR0_ALWAYS_ON
;
1680 vmx_fpu_deactivate(vcpu
);
1682 if (vmx
->rmode
.vm86_active
&& (cr0
& X86_CR0_PE
))
1685 if (!vmx
->rmode
.vm86_active
&& !(cr0
& X86_CR0_PE
))
1688 #ifdef CONFIG_X86_64
1689 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1690 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1692 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1698 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
1700 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1701 vmcs_writel(GUEST_CR0
, hw_cr0
);
1702 vcpu
->arch
.cr0
= cr0
;
1704 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1705 vmx_fpu_activate(vcpu
);
1708 static u64
construct_eptp(unsigned long root_hpa
)
1712 /* TODO write the value reading from MSR */
1713 eptp
= VMX_EPT_DEFAULT_MT
|
1714 VMX_EPT_DEFAULT_GAW
<< VMX_EPT_GAW_EPTP_SHIFT
;
1715 eptp
|= (root_hpa
& PAGE_MASK
);
1720 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1722 unsigned long guest_cr3
;
1727 eptp
= construct_eptp(cr3
);
1728 vmcs_write64(EPT_POINTER
, eptp
);
1729 guest_cr3
= is_paging(vcpu
) ? vcpu
->arch
.cr3
:
1730 vcpu
->kvm
->arch
.ept_identity_map_addr
;
1733 vmx_flush_tlb(vcpu
);
1734 vmcs_writel(GUEST_CR3
, guest_cr3
);
1735 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1736 vmx_fpu_deactivate(vcpu
);
1739 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1741 unsigned long hw_cr4
= cr4
| (to_vmx(vcpu
)->rmode
.vm86_active
?
1742 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
1744 vcpu
->arch
.cr4
= cr4
;
1746 ept_update_paging_mode_cr4(&hw_cr4
, vcpu
);
1748 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1749 vmcs_writel(GUEST_CR4
, hw_cr4
);
1752 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1754 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1756 return vmcs_readl(sf
->base
);
1759 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1760 struct kvm_segment
*var
, int seg
)
1762 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1765 var
->base
= vmcs_readl(sf
->base
);
1766 var
->limit
= vmcs_read32(sf
->limit
);
1767 var
->selector
= vmcs_read16(sf
->selector
);
1768 ar
= vmcs_read32(sf
->ar_bytes
);
1769 if ((ar
& AR_UNUSABLE_MASK
) && !emulate_invalid_guest_state
)
1771 var
->type
= ar
& 15;
1772 var
->s
= (ar
>> 4) & 1;
1773 var
->dpl
= (ar
>> 5) & 3;
1774 var
->present
= (ar
>> 7) & 1;
1775 var
->avl
= (ar
>> 12) & 1;
1776 var
->l
= (ar
>> 13) & 1;
1777 var
->db
= (ar
>> 14) & 1;
1778 var
->g
= (ar
>> 15) & 1;
1779 var
->unusable
= (ar
>> 16) & 1;
1782 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1784 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1787 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1790 return vmcs_read16(GUEST_CS_SELECTOR
) & 3;
1793 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1800 ar
= var
->type
& 15;
1801 ar
|= (var
->s
& 1) << 4;
1802 ar
|= (var
->dpl
& 3) << 5;
1803 ar
|= (var
->present
& 1) << 7;
1804 ar
|= (var
->avl
& 1) << 12;
1805 ar
|= (var
->l
& 1) << 13;
1806 ar
|= (var
->db
& 1) << 14;
1807 ar
|= (var
->g
& 1) << 15;
1809 if (ar
== 0) /* a 0 value means unusable */
1810 ar
= AR_UNUSABLE_MASK
;
1815 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1816 struct kvm_segment
*var
, int seg
)
1818 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1819 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1822 if (vmx
->rmode
.vm86_active
&& seg
== VCPU_SREG_TR
) {
1823 vmx
->rmode
.tr
.selector
= var
->selector
;
1824 vmx
->rmode
.tr
.base
= var
->base
;
1825 vmx
->rmode
.tr
.limit
= var
->limit
;
1826 vmx
->rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1829 vmcs_writel(sf
->base
, var
->base
);
1830 vmcs_write32(sf
->limit
, var
->limit
);
1831 vmcs_write16(sf
->selector
, var
->selector
);
1832 if (vmx
->rmode
.vm86_active
&& var
->s
) {
1834 * Hack real-mode segments into vm86 compatibility.
1836 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1837 vmcs_writel(sf
->base
, 0xf0000);
1840 ar
= vmx_segment_access_rights(var
);
1843 * Fix the "Accessed" bit in AR field of segment registers for older
1845 * IA32 arch specifies that at the time of processor reset the
1846 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1847 * is setting it to 0 in the usedland code. This causes invalid guest
1848 * state vmexit when "unrestricted guest" mode is turned on.
1849 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1850 * tree. Newer qemu binaries with that qemu fix would not need this
1853 if (enable_unrestricted_guest
&& (seg
!= VCPU_SREG_LDTR
))
1854 ar
|= 0x1; /* Accessed */
1856 vmcs_write32(sf
->ar_bytes
, ar
);
1859 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1861 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1863 *db
= (ar
>> 14) & 1;
1864 *l
= (ar
>> 13) & 1;
1867 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1869 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1870 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1873 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1875 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1876 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1879 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1881 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1882 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1885 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1887 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1888 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1891 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1893 struct kvm_segment var
;
1896 vmx_get_segment(vcpu
, &var
, seg
);
1897 ar
= vmx_segment_access_rights(&var
);
1899 if (var
.base
!= (var
.selector
<< 4))
1901 if (var
.limit
!= 0xffff)
1909 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
1911 struct kvm_segment cs
;
1912 unsigned int cs_rpl
;
1914 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1915 cs_rpl
= cs
.selector
& SELECTOR_RPL_MASK
;
1919 if (~cs
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_ACCESSES_MASK
))
1923 if (cs
.type
& AR_TYPE_WRITEABLE_MASK
) {
1924 if (cs
.dpl
> cs_rpl
)
1927 if (cs
.dpl
!= cs_rpl
)
1933 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1937 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
1939 struct kvm_segment ss
;
1940 unsigned int ss_rpl
;
1942 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1943 ss_rpl
= ss
.selector
& SELECTOR_RPL_MASK
;
1947 if (ss
.type
!= 3 && ss
.type
!= 7)
1951 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
1959 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1961 struct kvm_segment var
;
1964 vmx_get_segment(vcpu
, &var
, seg
);
1965 rpl
= var
.selector
& SELECTOR_RPL_MASK
;
1973 if (~var
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_WRITEABLE_MASK
)) {
1974 if (var
.dpl
< rpl
) /* DPL < RPL */
1978 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1984 static bool tr_valid(struct kvm_vcpu
*vcpu
)
1986 struct kvm_segment tr
;
1988 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
1992 if (tr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
1994 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
2002 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
2004 struct kvm_segment ldtr
;
2006 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
2010 if (ldtr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
2020 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
2022 struct kvm_segment cs
, ss
;
2024 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2025 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
2027 return ((cs
.selector
& SELECTOR_RPL_MASK
) ==
2028 (ss
.selector
& SELECTOR_RPL_MASK
));
2032 * Check if guest state is valid. Returns true if valid, false if
2034 * We assume that registers are always usable
2036 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
2038 /* real mode guest state checks */
2039 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) {
2040 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
2042 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
2044 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
2046 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
2048 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
2050 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
2053 /* protected mode guest state checks */
2054 if (!cs_ss_rpl_check(vcpu
))
2056 if (!code_segment_valid(vcpu
))
2058 if (!stack_segment_valid(vcpu
))
2060 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
2062 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
2064 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
2066 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
2068 if (!tr_valid(vcpu
))
2070 if (!ldtr_valid(vcpu
))
2074 * - Add checks on RIP
2075 * - Add checks on RFLAGS
2081 static int init_rmode_tss(struct kvm
*kvm
)
2083 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
2088 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
2091 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
2092 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
2093 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
2096 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
2099 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
2103 r
= kvm_write_guest_page(kvm
, fn
, &data
,
2104 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
2114 static int init_rmode_identity_map(struct kvm
*kvm
)
2117 pfn_t identity_map_pfn
;
2122 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
2123 printk(KERN_ERR
"EPT: identity-mapping pagetable "
2124 "haven't been allocated!\n");
2127 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
2130 identity_map_pfn
= kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
;
2131 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
2134 /* Set up identity-mapping pagetable for EPT in real mode */
2135 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
2136 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
2137 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
2138 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
2139 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
2143 kvm
->arch
.ept_identity_pagetable_done
= true;
2149 static void seg_setup(int seg
)
2151 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
2154 vmcs_write16(sf
->selector
, 0);
2155 vmcs_writel(sf
->base
, 0);
2156 vmcs_write32(sf
->limit
, 0xffff);
2157 if (enable_unrestricted_guest
) {
2159 if (seg
== VCPU_SREG_CS
)
2160 ar
|= 0x08; /* code segment */
2164 vmcs_write32(sf
->ar_bytes
, ar
);
2167 static int alloc_apic_access_page(struct kvm
*kvm
)
2169 struct kvm_userspace_memory_region kvm_userspace_mem
;
2172 down_write(&kvm
->slots_lock
);
2173 if (kvm
->arch
.apic_access_page
)
2175 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
2176 kvm_userspace_mem
.flags
= 0;
2177 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
2178 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2179 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2183 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
2185 up_write(&kvm
->slots_lock
);
2189 static int alloc_identity_pagetable(struct kvm
*kvm
)
2191 struct kvm_userspace_memory_region kvm_userspace_mem
;
2194 down_write(&kvm
->slots_lock
);
2195 if (kvm
->arch
.ept_identity_pagetable
)
2197 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
2198 kvm_userspace_mem
.flags
= 0;
2199 kvm_userspace_mem
.guest_phys_addr
=
2200 kvm
->arch
.ept_identity_map_addr
;
2201 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2202 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2206 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
2207 kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
);
2209 up_write(&kvm
->slots_lock
);
2213 static void allocate_vpid(struct vcpu_vmx
*vmx
)
2220 spin_lock(&vmx_vpid_lock
);
2221 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
2222 if (vpid
< VMX_NR_VPIDS
) {
2224 __set_bit(vpid
, vmx_vpid_bitmap
);
2226 spin_unlock(&vmx_vpid_lock
);
2229 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
, u32 msr
)
2231 int f
= sizeof(unsigned long);
2233 if (!cpu_has_vmx_msr_bitmap())
2237 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2238 * have the write-low and read-high bitmap offsets the wrong way round.
2239 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2241 if (msr
<= 0x1fff) {
2242 __clear_bit(msr
, msr_bitmap
+ 0x000 / f
); /* read-low */
2243 __clear_bit(msr
, msr_bitmap
+ 0x800 / f
); /* write-low */
2244 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
2246 __clear_bit(msr
, msr_bitmap
+ 0x400 / f
); /* read-high */
2247 __clear_bit(msr
, msr_bitmap
+ 0xc00 / f
); /* write-high */
2251 static void vmx_disable_intercept_for_msr(u32 msr
, bool longmode_only
)
2254 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy
, msr
);
2255 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode
, msr
);
2259 * Sets up the vmcs for emulated real mode.
2261 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
2263 u32 host_sysenter_cs
, msr_low
, msr_high
;
2265 u64 host_pat
, tsc_this
, tsc_base
;
2267 struct descriptor_table dt
;
2269 unsigned long kvm_vmx_return
;
2273 vmcs_write64(IO_BITMAP_A
, __pa(vmx_io_bitmap_a
));
2274 vmcs_write64(IO_BITMAP_B
, __pa(vmx_io_bitmap_b
));
2276 if (cpu_has_vmx_msr_bitmap())
2277 vmcs_write64(MSR_BITMAP
, __pa(vmx_msr_bitmap_legacy
));
2279 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
2282 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
2283 vmcs_config
.pin_based_exec_ctrl
);
2285 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
2286 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
2287 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
2288 #ifdef CONFIG_X86_64
2289 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
2290 CPU_BASED_CR8_LOAD_EXITING
;
2294 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
2295 CPU_BASED_CR3_LOAD_EXITING
|
2296 CPU_BASED_INVLPG_EXITING
;
2297 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
2299 if (cpu_has_secondary_exec_ctrls()) {
2300 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
2301 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2303 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
2305 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
2307 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
2308 if (!enable_unrestricted_guest
)
2309 exec_control
&= ~SECONDARY_EXEC_UNRESTRICTED_GUEST
;
2310 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
2313 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
2314 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
2315 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
2317 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
2318 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
2319 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2321 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
2322 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2323 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2324 vmcs_write16(HOST_FS_SELECTOR
, kvm_read_fs()); /* 22.2.4 */
2325 vmcs_write16(HOST_GS_SELECTOR
, kvm_read_gs()); /* 22.2.4 */
2326 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2327 #ifdef CONFIG_X86_64
2328 rdmsrl(MSR_FS_BASE
, a
);
2329 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
2330 rdmsrl(MSR_GS_BASE
, a
);
2331 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
2333 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
2334 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
2337 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
2340 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
2342 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
2343 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
2344 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
2345 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
2346 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
2348 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
2349 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
2350 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
2351 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
2352 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
2353 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
2355 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
2356 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2357 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2358 vmcs_write64(HOST_IA32_PAT
, host_pat
);
2360 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
2361 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2362 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2363 /* Write the default value follow host pat */
2364 vmcs_write64(GUEST_IA32_PAT
, host_pat
);
2365 /* Keep arch.pat sync with GUEST_IA32_PAT */
2366 vmx
->vcpu
.arch
.pat
= host_pat
;
2369 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
2370 u32 index
= vmx_msr_index
[i
];
2371 u32 data_low
, data_high
;
2375 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
2377 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
2379 data
= data_low
| ((u64
)data_high
<< 32);
2380 vmx
->host_msrs
[j
].index
= index
;
2381 vmx
->host_msrs
[j
].reserved
= 0;
2382 vmx
->host_msrs
[j
].data
= data
;
2383 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
2387 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
2389 /* 22.2.1, 20.8.1 */
2390 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
2392 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
2393 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
2395 tsc_base
= vmx
->vcpu
.kvm
->arch
.vm_init_tsc
;
2397 if (tsc_this
< vmx
->vcpu
.kvm
->arch
.vm_init_tsc
)
2398 tsc_base
= tsc_this
;
2400 guest_write_tsc(0, tsc_base
);
2405 static int init_rmode(struct kvm
*kvm
)
2407 if (!init_rmode_tss(kvm
))
2409 if (!init_rmode_identity_map(kvm
))
2414 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
2416 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2420 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
2421 down_read(&vcpu
->kvm
->slots_lock
);
2422 if (!init_rmode(vmx
->vcpu
.kvm
)) {
2427 vmx
->rmode
.vm86_active
= 0;
2429 vmx
->soft_vnmi_blocked
= 0;
2431 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
2432 kvm_set_cr8(&vmx
->vcpu
, 0);
2433 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
2434 if (kvm_vcpu_is_bsp(&vmx
->vcpu
))
2435 msr
|= MSR_IA32_APICBASE_BSP
;
2436 kvm_set_apic_base(&vmx
->vcpu
, msr
);
2438 fx_init(&vmx
->vcpu
);
2440 seg_setup(VCPU_SREG_CS
);
2442 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2443 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2445 if (kvm_vcpu_is_bsp(&vmx
->vcpu
)) {
2446 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
2447 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
2449 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
2450 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
2453 seg_setup(VCPU_SREG_DS
);
2454 seg_setup(VCPU_SREG_ES
);
2455 seg_setup(VCPU_SREG_FS
);
2456 seg_setup(VCPU_SREG_GS
);
2457 seg_setup(VCPU_SREG_SS
);
2459 vmcs_write16(GUEST_TR_SELECTOR
, 0);
2460 vmcs_writel(GUEST_TR_BASE
, 0);
2461 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
2462 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
2464 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
2465 vmcs_writel(GUEST_LDTR_BASE
, 0);
2466 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
2467 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
2469 vmcs_write32(GUEST_SYSENTER_CS
, 0);
2470 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
2471 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
2473 vmcs_writel(GUEST_RFLAGS
, 0x02);
2474 if (kvm_vcpu_is_bsp(&vmx
->vcpu
))
2475 kvm_rip_write(vcpu
, 0xfff0);
2477 kvm_rip_write(vcpu
, 0);
2478 kvm_register_write(vcpu
, VCPU_REGS_RSP
, 0);
2480 vmcs_writel(GUEST_DR7
, 0x400);
2482 vmcs_writel(GUEST_GDTR_BASE
, 0);
2483 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
2485 vmcs_writel(GUEST_IDTR_BASE
, 0);
2486 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
2488 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
2489 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
2490 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
2492 /* Special registers */
2493 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
2497 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
2499 if (cpu_has_vmx_tpr_shadow()) {
2500 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
2501 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
2502 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
2503 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
2504 vmcs_write32(TPR_THRESHOLD
, 0);
2507 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2508 vmcs_write64(APIC_ACCESS_ADDR
,
2509 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
2512 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
2514 vmx
->vcpu
.arch
.cr0
= 0x60000010;
2515 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
2516 vmx_set_cr4(&vmx
->vcpu
, 0);
2517 vmx_set_efer(&vmx
->vcpu
, 0);
2518 vmx_fpu_activate(&vmx
->vcpu
);
2519 update_exception_bitmap(&vmx
->vcpu
);
2521 vpid_sync_vcpu_all(vmx
);
2525 /* HACK: Don't enable emulation on guest boot/reset */
2526 vmx
->emulation_required
= 0;
2529 up_read(&vcpu
->kvm
->slots_lock
);
2533 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2535 u32 cpu_based_vm_exec_control
;
2537 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2538 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2539 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2542 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2544 u32 cpu_based_vm_exec_control
;
2546 if (!cpu_has_virtual_nmis()) {
2547 enable_irq_window(vcpu
);
2551 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2552 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_NMI_PENDING
;
2553 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2556 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
)
2558 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2560 int irq
= vcpu
->arch
.interrupt
.nr
;
2562 trace_kvm_inj_virq(irq
);
2564 ++vcpu
->stat
.irq_injections
;
2565 if (vmx
->rmode
.vm86_active
) {
2566 vmx
->rmode
.irq
.pending
= true;
2567 vmx
->rmode
.irq
.vector
= irq
;
2568 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2569 if (vcpu
->arch
.interrupt
.soft
)
2570 vmx
->rmode
.irq
.rip
+=
2571 vmx
->vcpu
.arch
.event_exit_inst_len
;
2572 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2573 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
2574 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2575 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2578 intr
= irq
| INTR_INFO_VALID_MASK
;
2579 if (vcpu
->arch
.interrupt
.soft
) {
2580 intr
|= INTR_TYPE_SOFT_INTR
;
2581 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2582 vmx
->vcpu
.arch
.event_exit_inst_len
);
2584 intr
|= INTR_TYPE_EXT_INTR
;
2585 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr
);
2588 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
2590 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2592 if (!cpu_has_virtual_nmis()) {
2594 * Tracking the NMI-blocked state in software is built upon
2595 * finding the next open IRQ window. This, in turn, depends on
2596 * well-behaving guests: They have to keep IRQs disabled at
2597 * least as long as the NMI handler runs. Otherwise we may
2598 * cause NMI nesting, maybe breaking the guest. But as this is
2599 * highly unlikely, we can live with the residual risk.
2601 vmx
->soft_vnmi_blocked
= 1;
2602 vmx
->vnmi_blocked_time
= 0;
2605 ++vcpu
->stat
.nmi_injections
;
2606 if (vmx
->rmode
.vm86_active
) {
2607 vmx
->rmode
.irq
.pending
= true;
2608 vmx
->rmode
.irq
.vector
= NMI_VECTOR
;
2609 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2610 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2611 NMI_VECTOR
| INTR_TYPE_SOFT_INTR
|
2612 INTR_INFO_VALID_MASK
);
2613 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2614 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2617 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2618 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
2621 static int vmx_nmi_allowed(struct kvm_vcpu
*vcpu
)
2623 if (!cpu_has_virtual_nmis() && to_vmx(vcpu
)->soft_vnmi_blocked
)
2626 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2627 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
|
2628 GUEST_INTR_STATE_NMI
));
2631 static int vmx_interrupt_allowed(struct kvm_vcpu
*vcpu
)
2633 return (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2634 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2635 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
));
2638 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2641 struct kvm_userspace_memory_region tss_mem
= {
2642 .slot
= TSS_PRIVATE_MEMSLOT
,
2643 .guest_phys_addr
= addr
,
2644 .memory_size
= PAGE_SIZE
* 3,
2648 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
2651 kvm
->arch
.tss_addr
= addr
;
2655 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
2656 int vec
, u32 err_code
)
2659 * Instruction with address size override prefix opcode 0x67
2660 * Cause the #SS fault with 0 error code in VM86 mode.
2662 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
2663 if (emulate_instruction(vcpu
, NULL
, 0, 0, 0) == EMULATE_DONE
)
2666 * Forward all other exceptions that are valid in real mode.
2667 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2668 * the required debugging infrastructure rework.
2672 if (vcpu
->guest_debug
&
2673 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
2675 kvm_queue_exception(vcpu
, vec
);
2678 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
2689 kvm_queue_exception(vcpu
, vec
);
2696 * Trigger machine check on the host. We assume all the MSRs are already set up
2697 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2698 * We pass a fake environment to the machine check handler because we want
2699 * the guest to be always treated like user space, no matter what context
2700 * it used internally.
2702 static void kvm_machine_check(void)
2704 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2705 struct pt_regs regs
= {
2706 .cs
= 3, /* Fake ring 3 no matter what the guest ran on */
2707 .flags
= X86_EFLAGS_IF
,
2710 do_machine_check(®s
, 0);
2714 static int handle_machine_check(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2716 /* already handled by vcpu_run */
2720 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2722 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2723 u32 intr_info
, ex_no
, error_code
;
2724 unsigned long cr2
, rip
, dr6
;
2726 enum emulation_result er
;
2728 vect_info
= vmx
->idt_vectoring_info
;
2729 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2731 if (is_machine_check(intr_info
))
2732 return handle_machine_check(vcpu
, kvm_run
);
2734 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
2735 !is_page_fault(intr_info
))
2736 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
2737 "intr info 0x%x\n", __func__
, vect_info
, intr_info
);
2739 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
)
2740 return 1; /* already handled by vmx_vcpu_run() */
2742 if (is_no_device(intr_info
)) {
2743 vmx_fpu_activate(vcpu
);
2747 if (is_invalid_opcode(intr_info
)) {
2748 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
2749 if (er
!= EMULATE_DONE
)
2750 kvm_queue_exception(vcpu
, UD_VECTOR
);
2755 rip
= kvm_rip_read(vcpu
);
2756 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2757 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2758 if (is_page_fault(intr_info
)) {
2759 /* EPT won't cause page fault directly */
2762 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2763 trace_kvm_page_fault(cr2
, error_code
);
2765 if (kvm_event_needs_reinjection(vcpu
))
2766 kvm_mmu_unprotect_page_virt(vcpu
, cr2
);
2767 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2770 if (vmx
->rmode
.vm86_active
&&
2771 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2773 if (vcpu
->arch
.halt_request
) {
2774 vcpu
->arch
.halt_request
= 0;
2775 return kvm_emulate_halt(vcpu
);
2780 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
2783 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
2784 if (!(vcpu
->guest_debug
&
2785 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
2786 vcpu
->arch
.dr6
= dr6
| DR6_FIXED_1
;
2787 kvm_queue_exception(vcpu
, DB_VECTOR
);
2790 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
2791 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
2794 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2795 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
2796 kvm_run
->debug
.arch
.exception
= ex_no
;
2799 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2800 kvm_run
->ex
.exception
= ex_no
;
2801 kvm_run
->ex
.error_code
= error_code
;
2807 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2808 struct kvm_run
*kvm_run
)
2810 ++vcpu
->stat
.irq_exits
;
2814 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2816 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2820 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2822 unsigned long exit_qualification
;
2823 int size
, in
, string
;
2826 ++vcpu
->stat
.io_exits
;
2827 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2828 string
= (exit_qualification
& 16) != 0;
2831 if (emulate_instruction(vcpu
,
2832 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2837 size
= (exit_qualification
& 7) + 1;
2838 in
= (exit_qualification
& 8) != 0;
2839 port
= exit_qualification
>> 16;
2841 skip_emulated_instruction(vcpu
);
2842 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2846 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2849 * Patch in the VMCALL instruction:
2851 hypercall
[0] = 0x0f;
2852 hypercall
[1] = 0x01;
2853 hypercall
[2] = 0xc1;
2856 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2858 unsigned long exit_qualification
, val
;
2862 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2863 cr
= exit_qualification
& 15;
2864 reg
= (exit_qualification
>> 8) & 15;
2865 switch ((exit_qualification
>> 4) & 3) {
2866 case 0: /* mov to cr */
2867 val
= kvm_register_read(vcpu
, reg
);
2868 trace_kvm_cr_write(cr
, val
);
2871 kvm_set_cr0(vcpu
, val
);
2872 skip_emulated_instruction(vcpu
);
2875 kvm_set_cr3(vcpu
, val
);
2876 skip_emulated_instruction(vcpu
);
2879 kvm_set_cr4(vcpu
, val
);
2880 skip_emulated_instruction(vcpu
);
2883 u8 cr8_prev
= kvm_get_cr8(vcpu
);
2884 u8 cr8
= kvm_register_read(vcpu
, reg
);
2885 kvm_set_cr8(vcpu
, cr8
);
2886 skip_emulated_instruction(vcpu
);
2887 if (irqchip_in_kernel(vcpu
->kvm
))
2889 if (cr8_prev
<= cr8
)
2891 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2897 vmx_fpu_deactivate(vcpu
);
2898 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2899 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2900 vmx_fpu_activate(vcpu
);
2901 skip_emulated_instruction(vcpu
);
2903 case 1: /*mov from cr*/
2906 kvm_register_write(vcpu
, reg
, vcpu
->arch
.cr3
);
2907 trace_kvm_cr_read(cr
, vcpu
->arch
.cr3
);
2908 skip_emulated_instruction(vcpu
);
2911 val
= kvm_get_cr8(vcpu
);
2912 kvm_register_write(vcpu
, reg
, val
);
2913 trace_kvm_cr_read(cr
, val
);
2914 skip_emulated_instruction(vcpu
);
2919 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2921 skip_emulated_instruction(vcpu
);
2926 kvm_run
->exit_reason
= 0;
2927 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2928 (int)(exit_qualification
>> 4) & 3, cr
);
2932 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2934 unsigned long exit_qualification
;
2938 dr
= vmcs_readl(GUEST_DR7
);
2941 * As the vm-exit takes precedence over the debug trap, we
2942 * need to emulate the latter, either for the host or the
2943 * guest debugging itself.
2945 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
2946 kvm_run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
2947 kvm_run
->debug
.arch
.dr7
= dr
;
2948 kvm_run
->debug
.arch
.pc
=
2949 vmcs_readl(GUEST_CS_BASE
) +
2950 vmcs_readl(GUEST_RIP
);
2951 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
2952 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2955 vcpu
->arch
.dr7
&= ~DR7_GD
;
2956 vcpu
->arch
.dr6
|= DR6_BD
;
2957 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
2958 kvm_queue_exception(vcpu
, DB_VECTOR
);
2963 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2964 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
2965 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
2966 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
2969 val
= vcpu
->arch
.db
[dr
];
2972 val
= vcpu
->arch
.dr6
;
2975 val
= vcpu
->arch
.dr7
;
2980 kvm_register_write(vcpu
, reg
, val
);
2982 val
= vcpu
->arch
.regs
[reg
];
2985 vcpu
->arch
.db
[dr
] = val
;
2986 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
2987 vcpu
->arch
.eff_db
[dr
] = val
;
2990 if (vcpu
->arch
.cr4
& X86_CR4_DE
)
2991 kvm_queue_exception(vcpu
, UD_VECTOR
);
2994 if (val
& 0xffffffff00000000ULL
) {
2995 kvm_queue_exception(vcpu
, GP_VECTOR
);
2998 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
3001 if (val
& 0xffffffff00000000ULL
) {
3002 kvm_queue_exception(vcpu
, GP_VECTOR
);
3005 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
3006 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
3007 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
3008 vcpu
->arch
.switch_db_regs
=
3009 (val
& DR7_BP_EN_MASK
);
3014 skip_emulated_instruction(vcpu
);
3018 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3020 kvm_emulate_cpuid(vcpu
);
3024 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3026 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3029 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
3030 kvm_inject_gp(vcpu
, 0);
3034 trace_kvm_msr_read(ecx
, data
);
3036 /* FIXME: handling of bits 32:63 of rax, rdx */
3037 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
3038 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
3039 skip_emulated_instruction(vcpu
);
3043 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3045 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3046 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
3047 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
3049 trace_kvm_msr_write(ecx
, data
);
3051 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
3052 kvm_inject_gp(vcpu
, 0);
3056 skip_emulated_instruction(vcpu
);
3060 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
3061 struct kvm_run
*kvm_run
)
3066 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
3067 struct kvm_run
*kvm_run
)
3069 u32 cpu_based_vm_exec_control
;
3071 /* clear pending irq */
3072 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3073 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
3074 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3076 ++vcpu
->stat
.irq_window_exits
;
3079 * If the user space waits to inject interrupts, exit as soon as
3082 if (!irqchip_in_kernel(vcpu
->kvm
) &&
3083 kvm_run
->request_interrupt_window
&&
3084 !kvm_cpu_has_interrupt(vcpu
)) {
3085 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
3091 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3093 skip_emulated_instruction(vcpu
);
3094 return kvm_emulate_halt(vcpu
);
3097 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3099 skip_emulated_instruction(vcpu
);
3100 kvm_emulate_hypercall(vcpu
);
3104 static int handle_vmx_insn(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3106 kvm_queue_exception(vcpu
, UD_VECTOR
);
3110 static int handle_invlpg(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3112 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3114 kvm_mmu_invlpg(vcpu
, exit_qualification
);
3115 skip_emulated_instruction(vcpu
);
3119 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3121 skip_emulated_instruction(vcpu
);
3122 /* TODO: Add support for VT-d/pass-through device */
3126 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3128 unsigned long exit_qualification
;
3129 enum emulation_result er
;
3130 unsigned long offset
;
3132 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3133 offset
= exit_qualification
& 0xffful
;
3135 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3137 if (er
!= EMULATE_DONE
) {
3139 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3146 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3148 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3149 unsigned long exit_qualification
;
3151 int reason
, type
, idt_v
;
3153 idt_v
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
3154 type
= (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
);
3156 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3158 reason
= (u32
)exit_qualification
>> 30;
3159 if (reason
== TASK_SWITCH_GATE
&& idt_v
) {
3161 case INTR_TYPE_NMI_INTR
:
3162 vcpu
->arch
.nmi_injected
= false;
3163 if (cpu_has_virtual_nmis())
3164 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3165 GUEST_INTR_STATE_NMI
);
3167 case INTR_TYPE_EXT_INTR
:
3168 case INTR_TYPE_SOFT_INTR
:
3169 kvm_clear_interrupt_queue(vcpu
);
3171 case INTR_TYPE_HARD_EXCEPTION
:
3172 case INTR_TYPE_SOFT_EXCEPTION
:
3173 kvm_clear_exception_queue(vcpu
);
3179 tss_selector
= exit_qualification
;
3181 if (!idt_v
|| (type
!= INTR_TYPE_HARD_EXCEPTION
&&
3182 type
!= INTR_TYPE_EXT_INTR
&&
3183 type
!= INTR_TYPE_NMI_INTR
))
3184 skip_emulated_instruction(vcpu
);
3186 if (!kvm_task_switch(vcpu
, tss_selector
, reason
))
3189 /* clear all local breakpoint enable flags */
3190 vmcs_writel(GUEST_DR7
, vmcs_readl(GUEST_DR7
) & ~55);
3193 * TODO: What about debug traps on tss switch?
3194 * Are we supposed to inject them and update dr6?
3200 static int handle_ept_violation(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3202 unsigned long exit_qualification
;
3206 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3208 if (exit_qualification
& (1 << 6)) {
3209 printk(KERN_ERR
"EPT: GPA exceeds GAW!\n");
3213 gla_validity
= (exit_qualification
>> 7) & 0x3;
3214 if (gla_validity
!= 0x3 && gla_validity
!= 0x1 && gla_validity
!= 0) {
3215 printk(KERN_ERR
"EPT: Handling EPT violation failed!\n");
3216 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3217 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
3218 vmcs_readl(GUEST_LINEAR_ADDRESS
));
3219 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
3220 (long unsigned int)exit_qualification
);
3221 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3222 kvm_run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_VIOLATION
;
3226 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3227 trace_kvm_page_fault(gpa
, exit_qualification
);
3228 return kvm_mmu_page_fault(vcpu
, gpa
& PAGE_MASK
, 0);
3231 static u64
ept_rsvd_mask(u64 spte
, int level
)
3236 for (i
= 51; i
> boot_cpu_data
.x86_phys_bits
; i
--)
3237 mask
|= (1ULL << i
);
3240 /* bits 7:3 reserved */
3242 else if (level
== 2) {
3243 if (spte
& (1ULL << 7))
3244 /* 2MB ref, bits 20:12 reserved */
3247 /* bits 6:3 reserved */
3254 static void ept_misconfig_inspect_spte(struct kvm_vcpu
*vcpu
, u64 spte
,
3257 printk(KERN_ERR
"%s: spte 0x%llx level %d\n", __func__
, spte
, level
);
3259 /* 010b (write-only) */
3260 WARN_ON((spte
& 0x7) == 0x2);
3262 /* 110b (write/execute) */
3263 WARN_ON((spte
& 0x7) == 0x6);
3265 /* 100b (execute-only) and value not supported by logical processor */
3266 if (!cpu_has_vmx_ept_execute_only())
3267 WARN_ON((spte
& 0x7) == 0x4);
3271 u64 rsvd_bits
= spte
& ept_rsvd_mask(spte
, level
);
3273 if (rsvd_bits
!= 0) {
3274 printk(KERN_ERR
"%s: rsvd_bits = 0x%llx\n",
3275 __func__
, rsvd_bits
);
3279 if (level
== 1 || (level
== 2 && (spte
& (1ULL << 7)))) {
3280 u64 ept_mem_type
= (spte
& 0x38) >> 3;
3282 if (ept_mem_type
== 2 || ept_mem_type
== 3 ||
3283 ept_mem_type
== 7) {
3284 printk(KERN_ERR
"%s: ept_mem_type=0x%llx\n",
3285 __func__
, ept_mem_type
);
3292 static int handle_ept_misconfig(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3298 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3300 printk(KERN_ERR
"EPT: Misconfiguration.\n");
3301 printk(KERN_ERR
"EPT: GPA: 0x%llx\n", gpa
);
3303 nr_sptes
= kvm_mmu_get_spte_hierarchy(vcpu
, gpa
, sptes
);
3305 for (i
= PT64_ROOT_LEVEL
; i
> PT64_ROOT_LEVEL
- nr_sptes
; --i
)
3306 ept_misconfig_inspect_spte(vcpu
, sptes
[i
-1], i
);
3308 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3309 kvm_run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
3314 static int handle_nmi_window(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3316 u32 cpu_based_vm_exec_control
;
3318 /* clear pending NMI */
3319 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3320 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
3321 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3322 ++vcpu
->stat
.nmi_window_exits
;
3327 static void handle_invalid_guest_state(struct kvm_vcpu
*vcpu
,
3328 struct kvm_run
*kvm_run
)
3330 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3331 enum emulation_result err
= EMULATE_DONE
;
3336 while (!guest_state_valid(vcpu
)) {
3337 err
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3339 if (err
== EMULATE_DO_MMIO
)
3342 if (err
!= EMULATE_DONE
) {
3343 kvm_report_emulation_failure(vcpu
, "emulation failure");
3347 if (signal_pending(current
))
3354 local_irq_disable();
3356 vmx
->invalid_state_emulation_result
= err
;
3360 * The exit handlers return 1 if the exit was handled fully and guest execution
3361 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3362 * to be done to userspace and return 0.
3364 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
3365 struct kvm_run
*kvm_run
) = {
3366 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
3367 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
3368 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
3369 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
3370 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
3371 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
3372 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
3373 [EXIT_REASON_CPUID
] = handle_cpuid
,
3374 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
3375 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
3376 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
3377 [EXIT_REASON_HLT
] = handle_halt
,
3378 [EXIT_REASON_INVLPG
] = handle_invlpg
,
3379 [EXIT_REASON_VMCALL
] = handle_vmcall
,
3380 [EXIT_REASON_VMCLEAR
] = handle_vmx_insn
,
3381 [EXIT_REASON_VMLAUNCH
] = handle_vmx_insn
,
3382 [EXIT_REASON_VMPTRLD
] = handle_vmx_insn
,
3383 [EXIT_REASON_VMPTRST
] = handle_vmx_insn
,
3384 [EXIT_REASON_VMREAD
] = handle_vmx_insn
,
3385 [EXIT_REASON_VMRESUME
] = handle_vmx_insn
,
3386 [EXIT_REASON_VMWRITE
] = handle_vmx_insn
,
3387 [EXIT_REASON_VMOFF
] = handle_vmx_insn
,
3388 [EXIT_REASON_VMON
] = handle_vmx_insn
,
3389 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
3390 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
3391 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
3392 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
3393 [EXIT_REASON_MCE_DURING_VMENTRY
] = handle_machine_check
,
3394 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
3395 [EXIT_REASON_EPT_MISCONFIG
] = handle_ept_misconfig
,
3398 static const int kvm_vmx_max_exit_handlers
=
3399 ARRAY_SIZE(kvm_vmx_exit_handlers
);
3402 * The guest has exited. See if we can fix it or if we need userspace
3405 static int vmx_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
3407 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3408 u32 exit_reason
= vmx
->exit_reason
;
3409 u32 vectoring_info
= vmx
->idt_vectoring_info
;
3411 trace_kvm_exit(exit_reason
, kvm_rip_read(vcpu
));
3413 /* If we need to emulate an MMIO from handle_invalid_guest_state
3414 * we just return 0 */
3415 if (vmx
->emulation_required
&& emulate_invalid_guest_state
) {
3416 if (guest_state_valid(vcpu
))
3417 vmx
->emulation_required
= 0;
3418 return vmx
->invalid_state_emulation_result
!= EMULATE_DO_MMIO
;
3421 /* Access CR3 don't cause VMExit in paging mode, so we need
3422 * to sync with guest real CR3. */
3423 if (enable_ept
&& is_paging(vcpu
))
3424 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
3426 if (unlikely(vmx
->fail
)) {
3427 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3428 kvm_run
->fail_entry
.hardware_entry_failure_reason
3429 = vmcs_read32(VM_INSTRUCTION_ERROR
);
3433 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3434 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
3435 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
3436 exit_reason
!= EXIT_REASON_TASK_SWITCH
))
3437 printk(KERN_WARNING
"%s: unexpected, valid vectoring info "
3438 "(0x%x) and exit reason is 0x%x\n",
3439 __func__
, vectoring_info
, exit_reason
);
3441 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
)) {
3442 if (vmx_interrupt_allowed(vcpu
)) {
3443 vmx
->soft_vnmi_blocked
= 0;
3444 } else if (vmx
->vnmi_blocked_time
> 1000000000LL &&
3445 vcpu
->arch
.nmi_pending
) {
3447 * This CPU don't support us in finding the end of an
3448 * NMI-blocked window if the guest runs with IRQs
3449 * disabled. So we pull the trigger after 1 s of
3450 * futile waiting, but inform the user about this.
3452 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
3453 "state on VCPU %d after 1 s timeout\n",
3454 __func__
, vcpu
->vcpu_id
);
3455 vmx
->soft_vnmi_blocked
= 0;
3459 if (exit_reason
< kvm_vmx_max_exit_handlers
3460 && kvm_vmx_exit_handlers
[exit_reason
])
3461 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
3463 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3464 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
3469 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3471 if (irr
== -1 || tpr
< irr
) {
3472 vmcs_write32(TPR_THRESHOLD
, 0);
3476 vmcs_write32(TPR_THRESHOLD
, irr
);
3479 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
3482 u32 idt_vectoring_info
= vmx
->idt_vectoring_info
;
3486 bool idtv_info_valid
;
3488 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3490 vmx
->exit_reason
= vmcs_read32(VM_EXIT_REASON
);
3492 /* Handle machine checks before interrupts are enabled */
3493 if ((vmx
->exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
)
3494 || (vmx
->exit_reason
== EXIT_REASON_EXCEPTION_NMI
3495 && is_machine_check(exit_intr_info
)))
3496 kvm_machine_check();
3498 /* We need to handle NMIs before interrupts are enabled */
3499 if ((exit_intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
&&
3500 (exit_intr_info
& INTR_INFO_VALID_MASK
))
3503 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
3505 if (cpu_has_virtual_nmis()) {
3506 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
3507 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
3509 * SDM 3: 27.7.1.2 (September 2008)
3510 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3511 * a guest IRET fault.
3512 * SDM 3: 23.2.2 (September 2008)
3513 * Bit 12 is undefined in any of the following cases:
3514 * If the VM exit sets the valid bit in the IDT-vectoring
3515 * information field.
3516 * If the VM exit is due to a double fault.
3518 if ((exit_intr_info
& INTR_INFO_VALID_MASK
) && unblock_nmi
&&
3519 vector
!= DF_VECTOR
&& !idtv_info_valid
)
3520 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3521 GUEST_INTR_STATE_NMI
);
3522 } else if (unlikely(vmx
->soft_vnmi_blocked
))
3523 vmx
->vnmi_blocked_time
+=
3524 ktime_to_ns(ktime_sub(ktime_get(), vmx
->entry_time
));
3526 vmx
->vcpu
.arch
.nmi_injected
= false;
3527 kvm_clear_exception_queue(&vmx
->vcpu
);
3528 kvm_clear_interrupt_queue(&vmx
->vcpu
);
3530 if (!idtv_info_valid
)
3533 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
3534 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
3537 case INTR_TYPE_NMI_INTR
:
3538 vmx
->vcpu
.arch
.nmi_injected
= true;
3540 * SDM 3: 27.7.1.2 (September 2008)
3541 * Clear bit "block by NMI" before VM entry if a NMI
3544 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3545 GUEST_INTR_STATE_NMI
);
3547 case INTR_TYPE_SOFT_EXCEPTION
:
3548 vmx
->vcpu
.arch
.event_exit_inst_len
=
3549 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
3551 case INTR_TYPE_HARD_EXCEPTION
:
3552 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
3553 u32 err
= vmcs_read32(IDT_VECTORING_ERROR_CODE
);
3554 kvm_queue_exception_e(&vmx
->vcpu
, vector
, err
);
3556 kvm_queue_exception(&vmx
->vcpu
, vector
);
3558 case INTR_TYPE_SOFT_INTR
:
3559 vmx
->vcpu
.arch
.event_exit_inst_len
=
3560 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
3562 case INTR_TYPE_EXT_INTR
:
3563 kvm_queue_interrupt(&vmx
->vcpu
, vector
,
3564 type
== INTR_TYPE_SOFT_INTR
);
3572 * Failure to inject an interrupt should give us the information
3573 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3574 * when fetching the interrupt redirection bitmap in the real-mode
3575 * tss, this doesn't happen. So we do it ourselves.
3577 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
3579 vmx
->rmode
.irq
.pending
= 0;
3580 if (kvm_rip_read(&vmx
->vcpu
) + 1 != vmx
->rmode
.irq
.rip
)
3582 kvm_rip_write(&vmx
->vcpu
, vmx
->rmode
.irq
.rip
);
3583 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
3584 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
3585 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
3588 vmx
->idt_vectoring_info
=
3589 VECTORING_INFO_VALID_MASK
3590 | INTR_TYPE_EXT_INTR
3591 | vmx
->rmode
.irq
.vector
;
3594 #ifdef CONFIG_X86_64
3602 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3604 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3606 if (enable_ept
&& is_paging(vcpu
)) {
3607 vmcs_writel(GUEST_CR3
, vcpu
->arch
.cr3
);
3608 ept_load_pdptrs(vcpu
);
3610 /* Record the guest's net vcpu time for enforced NMI injections. */
3611 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
))
3612 vmx
->entry_time
= ktime_get();
3614 /* Handle invalid guest state instead of entering VMX */
3615 if (vmx
->emulation_required
&& emulate_invalid_guest_state
) {
3616 handle_invalid_guest_state(vcpu
, kvm_run
);
3620 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3621 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
3622 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3623 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
3625 /* When single-stepping over STI and MOV SS, we must clear the
3626 * corresponding interruptibility bits in the guest state. Otherwise
3627 * vmentry fails as it then expects bit 14 (BS) in pending debug
3628 * exceptions being set, but that's not correct for the guest debugging
3630 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3631 vmx_set_interrupt_shadow(vcpu
, 0);
3634 * Loading guest fpu may have cleared host cr0.ts
3636 vmcs_writel(HOST_CR0
, read_cr0());
3638 set_debugreg(vcpu
->arch
.dr6
, 6);
3641 /* Store host registers */
3642 "push %%"R
"dx; push %%"R
"bp;"
3644 "cmp %%"R
"sp, %c[host_rsp](%0) \n\t"
3646 "mov %%"R
"sp, %c[host_rsp](%0) \n\t"
3647 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
3649 /* Reload cr2 if changed */
3650 "mov %c[cr2](%0), %%"R
"ax \n\t"
3651 "mov %%cr2, %%"R
"dx \n\t"
3652 "cmp %%"R
"ax, %%"R
"dx \n\t"
3654 "mov %%"R
"ax, %%cr2 \n\t"
3656 /* Check if vmlaunch of vmresume is needed */
3657 "cmpl $0, %c[launched](%0) \n\t"
3658 /* Load guest registers. Don't clobber flags. */
3659 "mov %c[rax](%0), %%"R
"ax \n\t"
3660 "mov %c[rbx](%0), %%"R
"bx \n\t"
3661 "mov %c[rdx](%0), %%"R
"dx \n\t"
3662 "mov %c[rsi](%0), %%"R
"si \n\t"
3663 "mov %c[rdi](%0), %%"R
"di \n\t"
3664 "mov %c[rbp](%0), %%"R
"bp \n\t"
3665 #ifdef CONFIG_X86_64
3666 "mov %c[r8](%0), %%r8 \n\t"
3667 "mov %c[r9](%0), %%r9 \n\t"
3668 "mov %c[r10](%0), %%r10 \n\t"
3669 "mov %c[r11](%0), %%r11 \n\t"
3670 "mov %c[r12](%0), %%r12 \n\t"
3671 "mov %c[r13](%0), %%r13 \n\t"
3672 "mov %c[r14](%0), %%r14 \n\t"
3673 "mov %c[r15](%0), %%r15 \n\t"
3675 "mov %c[rcx](%0), %%"R
"cx \n\t" /* kills %0 (ecx) */
3677 /* Enter guest mode */
3678 "jne .Llaunched \n\t"
3679 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
3680 "jmp .Lkvm_vmx_return \n\t"
3681 ".Llaunched: " __ex(ASM_VMX_VMRESUME
) "\n\t"
3682 ".Lkvm_vmx_return: "
3683 /* Save guest registers, load host registers, keep flags */
3684 "xchg %0, (%%"R
"sp) \n\t"
3685 "mov %%"R
"ax, %c[rax](%0) \n\t"
3686 "mov %%"R
"bx, %c[rbx](%0) \n\t"
3687 "push"Q
" (%%"R
"sp); pop"Q
" %c[rcx](%0) \n\t"
3688 "mov %%"R
"dx, %c[rdx](%0) \n\t"
3689 "mov %%"R
"si, %c[rsi](%0) \n\t"
3690 "mov %%"R
"di, %c[rdi](%0) \n\t"
3691 "mov %%"R
"bp, %c[rbp](%0) \n\t"
3692 #ifdef CONFIG_X86_64
3693 "mov %%r8, %c[r8](%0) \n\t"
3694 "mov %%r9, %c[r9](%0) \n\t"
3695 "mov %%r10, %c[r10](%0) \n\t"
3696 "mov %%r11, %c[r11](%0) \n\t"
3697 "mov %%r12, %c[r12](%0) \n\t"
3698 "mov %%r13, %c[r13](%0) \n\t"
3699 "mov %%r14, %c[r14](%0) \n\t"
3700 "mov %%r15, %c[r15](%0) \n\t"
3702 "mov %%cr2, %%"R
"ax \n\t"
3703 "mov %%"R
"ax, %c[cr2](%0) \n\t"
3705 "pop %%"R
"bp; pop %%"R
"bp; pop %%"R
"dx \n\t"
3706 "setbe %c[fail](%0) \n\t"
3707 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
3708 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
3709 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
3710 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
3711 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
3712 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3713 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3714 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3715 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3716 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3717 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
3718 #ifdef CONFIG_X86_64
3719 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3720 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3721 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3722 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3723 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3724 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3725 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3726 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
3728 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
3730 , R
"bx", R
"di", R
"si"
3731 #ifdef CONFIG_X86_64
3732 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3736 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
)
3737 | (1 << VCPU_EXREG_PDPTR
));
3738 vcpu
->arch
.regs_dirty
= 0;
3740 get_debugreg(vcpu
->arch
.dr6
, 6);
3742 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
3743 if (vmx
->rmode
.irq
.pending
)
3744 fixup_rmode_irq(vmx
);
3746 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
3749 vmx_complete_interrupts(vmx
);
3755 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
3757 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3761 free_vmcs(vmx
->vmcs
);
3766 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
3768 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3770 spin_lock(&vmx_vpid_lock
);
3772 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
3773 spin_unlock(&vmx_vpid_lock
);
3774 vmx_free_vmcs(vcpu
);
3775 kfree(vmx
->host_msrs
);
3776 kfree(vmx
->guest_msrs
);
3777 kvm_vcpu_uninit(vcpu
);
3778 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3781 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
3784 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
3788 return ERR_PTR(-ENOMEM
);
3792 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
3796 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3797 if (!vmx
->guest_msrs
) {
3802 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3803 if (!vmx
->host_msrs
)
3804 goto free_guest_msrs
;
3806 vmx
->vmcs
= alloc_vmcs();
3810 vmcs_clear(vmx
->vmcs
);
3813 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
3814 err
= vmx_vcpu_setup(vmx
);
3815 vmx_vcpu_put(&vmx
->vcpu
);
3819 if (vm_need_virtualize_apic_accesses(kvm
))
3820 if (alloc_apic_access_page(kvm
) != 0)
3824 if (!kvm
->arch
.ept_identity_map_addr
)
3825 kvm
->arch
.ept_identity_map_addr
=
3826 VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
3827 if (alloc_identity_pagetable(kvm
) != 0)
3834 free_vmcs(vmx
->vmcs
);
3836 kfree(vmx
->host_msrs
);
3838 kfree(vmx
->guest_msrs
);
3840 kvm_vcpu_uninit(&vmx
->vcpu
);
3842 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3843 return ERR_PTR(err
);
3846 static void __init
vmx_check_processor_compat(void *rtn
)
3848 struct vmcs_config vmcs_conf
;
3851 if (setup_vmcs_config(&vmcs_conf
) < 0)
3853 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
3854 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
3855 smp_processor_id());
3860 static int get_ept_level(void)
3862 return VMX_EPT_DEFAULT_GAW
+ 1;
3865 static u64
vmx_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3869 /* For VT-d and EPT combination
3870 * 1. MMIO: always map as UC
3872 * a. VT-d without snooping control feature: can't guarantee the
3873 * result, try to trust guest.
3874 * b. VT-d with snooping control feature: snooping control feature of
3875 * VT-d engine can guarantee the cache correctness. Just set it
3876 * to WB to keep consistent with host. So the same as item 3.
3877 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3878 * consistent with host MTRR
3881 ret
= MTRR_TYPE_UNCACHABLE
<< VMX_EPT_MT_EPTE_SHIFT
;
3882 else if (vcpu
->kvm
->arch
.iommu_domain
&&
3883 !(vcpu
->kvm
->arch
.iommu_flags
& KVM_IOMMU_CACHE_COHERENCY
))
3884 ret
= kvm_get_guest_memory_type(vcpu
, gfn
) <<
3885 VMX_EPT_MT_EPTE_SHIFT
;
3887 ret
= (MTRR_TYPE_WRBACK
<< VMX_EPT_MT_EPTE_SHIFT
)
3893 static const struct trace_print_flags vmx_exit_reasons_str
[] = {
3894 { EXIT_REASON_EXCEPTION_NMI
, "exception" },
3895 { EXIT_REASON_EXTERNAL_INTERRUPT
, "ext_irq" },
3896 { EXIT_REASON_TRIPLE_FAULT
, "triple_fault" },
3897 { EXIT_REASON_NMI_WINDOW
, "nmi_window" },
3898 { EXIT_REASON_IO_INSTRUCTION
, "io_instruction" },
3899 { EXIT_REASON_CR_ACCESS
, "cr_access" },
3900 { EXIT_REASON_DR_ACCESS
, "dr_access" },
3901 { EXIT_REASON_CPUID
, "cpuid" },
3902 { EXIT_REASON_MSR_READ
, "rdmsr" },
3903 { EXIT_REASON_MSR_WRITE
, "wrmsr" },
3904 { EXIT_REASON_PENDING_INTERRUPT
, "interrupt_window" },
3905 { EXIT_REASON_HLT
, "halt" },
3906 { EXIT_REASON_INVLPG
, "invlpg" },
3907 { EXIT_REASON_VMCALL
, "hypercall" },
3908 { EXIT_REASON_TPR_BELOW_THRESHOLD
, "tpr_below_thres" },
3909 { EXIT_REASON_APIC_ACCESS
, "apic_access" },
3910 { EXIT_REASON_WBINVD
, "wbinvd" },
3911 { EXIT_REASON_TASK_SWITCH
, "task_switch" },
3912 { EXIT_REASON_EPT_VIOLATION
, "ept_violation" },
3916 static bool vmx_gb_page_enable(void)
3921 static struct kvm_x86_ops vmx_x86_ops
= {
3922 .cpu_has_kvm_support
= cpu_has_kvm_support
,
3923 .disabled_by_bios
= vmx_disabled_by_bios
,
3924 .hardware_setup
= hardware_setup
,
3925 .hardware_unsetup
= hardware_unsetup
,
3926 .check_processor_compatibility
= vmx_check_processor_compat
,
3927 .hardware_enable
= hardware_enable
,
3928 .hardware_disable
= hardware_disable
,
3929 .cpu_has_accelerated_tpr
= report_flexpriority
,
3931 .vcpu_create
= vmx_create_vcpu
,
3932 .vcpu_free
= vmx_free_vcpu
,
3933 .vcpu_reset
= vmx_vcpu_reset
,
3935 .prepare_guest_switch
= vmx_save_host_state
,
3936 .vcpu_load
= vmx_vcpu_load
,
3937 .vcpu_put
= vmx_vcpu_put
,
3939 .set_guest_debug
= set_guest_debug
,
3940 .get_msr
= vmx_get_msr
,
3941 .set_msr
= vmx_set_msr
,
3942 .get_segment_base
= vmx_get_segment_base
,
3943 .get_segment
= vmx_get_segment
,
3944 .set_segment
= vmx_set_segment
,
3945 .get_cpl
= vmx_get_cpl
,
3946 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
3947 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
3948 .set_cr0
= vmx_set_cr0
,
3949 .set_cr3
= vmx_set_cr3
,
3950 .set_cr4
= vmx_set_cr4
,
3951 .set_efer
= vmx_set_efer
,
3952 .get_idt
= vmx_get_idt
,
3953 .set_idt
= vmx_set_idt
,
3954 .get_gdt
= vmx_get_gdt
,
3955 .set_gdt
= vmx_set_gdt
,
3956 .cache_reg
= vmx_cache_reg
,
3957 .get_rflags
= vmx_get_rflags
,
3958 .set_rflags
= vmx_set_rflags
,
3960 .tlb_flush
= vmx_flush_tlb
,
3962 .run
= vmx_vcpu_run
,
3963 .handle_exit
= vmx_handle_exit
,
3964 .skip_emulated_instruction
= skip_emulated_instruction
,
3965 .set_interrupt_shadow
= vmx_set_interrupt_shadow
,
3966 .get_interrupt_shadow
= vmx_get_interrupt_shadow
,
3967 .patch_hypercall
= vmx_patch_hypercall
,
3968 .set_irq
= vmx_inject_irq
,
3969 .set_nmi
= vmx_inject_nmi
,
3970 .queue_exception
= vmx_queue_exception
,
3971 .interrupt_allowed
= vmx_interrupt_allowed
,
3972 .nmi_allowed
= vmx_nmi_allowed
,
3973 .enable_nmi_window
= enable_nmi_window
,
3974 .enable_irq_window
= enable_irq_window
,
3975 .update_cr8_intercept
= update_cr8_intercept
,
3977 .set_tss_addr
= vmx_set_tss_addr
,
3978 .get_tdp_level
= get_ept_level
,
3979 .get_mt_mask
= vmx_get_mt_mask
,
3981 .exit_reasons_str
= vmx_exit_reasons_str
,
3982 .gb_page_enable
= vmx_gb_page_enable
,
3985 static int __init
vmx_init(void)
3989 vmx_io_bitmap_a
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3990 if (!vmx_io_bitmap_a
)
3993 vmx_io_bitmap_b
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3994 if (!vmx_io_bitmap_b
) {
3999 vmx_msr_bitmap_legacy
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4000 if (!vmx_msr_bitmap_legacy
) {
4005 vmx_msr_bitmap_longmode
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4006 if (!vmx_msr_bitmap_longmode
) {
4012 * Allow direct access to the PC debug port (it is often used for I/O
4013 * delays, but the vmexits simply slow things down).
4015 memset(vmx_io_bitmap_a
, 0xff, PAGE_SIZE
);
4016 clear_bit(0x80, vmx_io_bitmap_a
);
4018 memset(vmx_io_bitmap_b
, 0xff, PAGE_SIZE
);
4020 memset(vmx_msr_bitmap_legacy
, 0xff, PAGE_SIZE
);
4021 memset(vmx_msr_bitmap_longmode
, 0xff, PAGE_SIZE
);
4023 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
4025 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
4029 vmx_disable_intercept_for_msr(MSR_FS_BASE
, false);
4030 vmx_disable_intercept_for_msr(MSR_GS_BASE
, false);
4031 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE
, true);
4032 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS
, false);
4033 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP
, false);
4034 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP
, false);
4037 bypass_guest_pf
= 0;
4038 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK
|
4039 VMX_EPT_WRITABLE_MASK
);
4040 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
4041 VMX_EPT_EXECUTABLE_MASK
);
4046 if (bypass_guest_pf
)
4047 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
4054 free_page((unsigned long)vmx_msr_bitmap_longmode
);
4056 free_page((unsigned long)vmx_msr_bitmap_legacy
);
4058 free_page((unsigned long)vmx_io_bitmap_b
);
4060 free_page((unsigned long)vmx_io_bitmap_a
);
4064 static void __exit
vmx_exit(void)
4066 free_page((unsigned long)vmx_msr_bitmap_legacy
);
4067 free_page((unsigned long)vmx_msr_bitmap_longmode
);
4068 free_page((unsigned long)vmx_io_bitmap_b
);
4069 free_page((unsigned long)vmx_io_bitmap_a
);
4074 module_init(vmx_init
)
4075 module_exit(vmx_exit
)