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
);
65 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
66 * ple_gap: upper bound on the amount of time between two successive
67 * executions of PAUSE in a loop. Also indicate if ple enabled.
68 * According to test, this time is usually small than 41 cycles.
69 * ple_window: upper bound on the amount of time a guest is allowed to execute
70 * in a PAUSE loop. Tests indicate that most spinlocks are held for
71 * less than 2^12 cycles
72 * Time is measured based on a counter that runs at the same rate as the TSC,
73 * refer SDM volume 3b section 21.6.13 & 22.1.3.
75 #define KVM_VMX_DEFAULT_PLE_GAP 41
76 #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
77 static int ple_gap
= KVM_VMX_DEFAULT_PLE_GAP
;
78 module_param(ple_gap
, int, S_IRUGO
);
80 static int ple_window
= KVM_VMX_DEFAULT_PLE_WINDOW
;
81 module_param(ple_window
, int, S_IRUGO
);
89 struct shared_msr_entry
{
97 struct list_head local_vcpus_link
;
98 unsigned long host_rsp
;
101 u32 idt_vectoring_info
;
102 struct shared_msr_entry
*guest_msrs
;
106 u64 msr_host_kernel_gs_base
;
107 u64 msr_guest_kernel_gs_base
;
112 u16 fs_sel
, gs_sel
, ldt_sel
;
113 int gs_ldt_reload_needed
;
114 int fs_reload_needed
;
119 struct kvm_save_segment
{
124 } tr
, es
, ds
, fs
, gs
;
132 bool emulation_required
;
134 /* Support for vnmi-less CPUs */
135 int soft_vnmi_blocked
;
137 s64 vnmi_blocked_time
;
141 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
143 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
146 static int init_rmode(struct kvm
*kvm
);
147 static u64
construct_eptp(unsigned long root_hpa
);
149 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
150 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
151 static DEFINE_PER_CPU(struct list_head
, vcpus_on_cpu
);
153 static unsigned long *vmx_io_bitmap_a
;
154 static unsigned long *vmx_io_bitmap_b
;
155 static unsigned long *vmx_msr_bitmap_legacy
;
156 static unsigned long *vmx_msr_bitmap_longmode
;
158 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
159 static DEFINE_SPINLOCK(vmx_vpid_lock
);
161 static struct vmcs_config
{
165 u32 pin_based_exec_ctrl
;
166 u32 cpu_based_exec_ctrl
;
167 u32 cpu_based_2nd_exec_ctrl
;
172 static struct vmx_capability
{
177 #define VMX_SEGMENT_FIELD(seg) \
178 [VCPU_SREG_##seg] = { \
179 .selector = GUEST_##seg##_SELECTOR, \
180 .base = GUEST_##seg##_BASE, \
181 .limit = GUEST_##seg##_LIMIT, \
182 .ar_bytes = GUEST_##seg##_AR_BYTES, \
185 static struct kvm_vmx_segment_field
{
190 } kvm_vmx_segment_fields
[] = {
191 VMX_SEGMENT_FIELD(CS
),
192 VMX_SEGMENT_FIELD(DS
),
193 VMX_SEGMENT_FIELD(ES
),
194 VMX_SEGMENT_FIELD(FS
),
195 VMX_SEGMENT_FIELD(GS
),
196 VMX_SEGMENT_FIELD(SS
),
197 VMX_SEGMENT_FIELD(TR
),
198 VMX_SEGMENT_FIELD(LDTR
),
201 static u64 host_efer
;
203 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
);
206 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
207 * away by decrementing the array size.
209 static const u32 vmx_msr_index
[] = {
211 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
,
213 MSR_EFER
, MSR_K6_STAR
,
215 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
217 static inline int is_page_fault(u32 intr_info
)
219 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
220 INTR_INFO_VALID_MASK
)) ==
221 (INTR_TYPE_HARD_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
224 static inline int is_no_device(u32 intr_info
)
226 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
227 INTR_INFO_VALID_MASK
)) ==
228 (INTR_TYPE_HARD_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
231 static inline int is_invalid_opcode(u32 intr_info
)
233 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
234 INTR_INFO_VALID_MASK
)) ==
235 (INTR_TYPE_HARD_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
238 static inline int is_external_interrupt(u32 intr_info
)
240 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
241 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
244 static inline int is_machine_check(u32 intr_info
)
246 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
247 INTR_INFO_VALID_MASK
)) ==
248 (INTR_TYPE_HARD_EXCEPTION
| MC_VECTOR
| INTR_INFO_VALID_MASK
);
251 static inline int cpu_has_vmx_msr_bitmap(void)
253 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
;
256 static inline int cpu_has_vmx_tpr_shadow(void)
258 return vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
;
261 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
263 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
));
266 static inline int cpu_has_secondary_exec_ctrls(void)
268 return vmcs_config
.cpu_based_exec_ctrl
&
269 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
272 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
274 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
275 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
278 static inline bool cpu_has_vmx_flexpriority(void)
280 return cpu_has_vmx_tpr_shadow() &&
281 cpu_has_vmx_virtualize_apic_accesses();
284 static inline bool cpu_has_vmx_ept_execute_only(void)
286 return !!(vmx_capability
.ept
& VMX_EPT_EXECUTE_ONLY_BIT
);
289 static inline bool cpu_has_vmx_eptp_uncacheable(void)
291 return !!(vmx_capability
.ept
& VMX_EPTP_UC_BIT
);
294 static inline bool cpu_has_vmx_eptp_writeback(void)
296 return !!(vmx_capability
.ept
& VMX_EPTP_WB_BIT
);
299 static inline bool cpu_has_vmx_ept_2m_page(void)
301 return !!(vmx_capability
.ept
& VMX_EPT_2MB_PAGE_BIT
);
304 static inline int cpu_has_vmx_invept_individual_addr(void)
306 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
);
309 static inline int cpu_has_vmx_invept_context(void)
311 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
);
314 static inline int cpu_has_vmx_invept_global(void)
316 return !!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
);
319 static inline int cpu_has_vmx_ept(void)
321 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
322 SECONDARY_EXEC_ENABLE_EPT
;
325 static inline int cpu_has_vmx_unrestricted_guest(void)
327 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
328 SECONDARY_EXEC_UNRESTRICTED_GUEST
;
331 static inline int cpu_has_vmx_ple(void)
333 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
334 SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
337 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
339 return flexpriority_enabled
&&
340 (cpu_has_vmx_virtualize_apic_accesses()) &&
341 (irqchip_in_kernel(kvm
));
344 static inline int cpu_has_vmx_vpid(void)
346 return vmcs_config
.cpu_based_2nd_exec_ctrl
&
347 SECONDARY_EXEC_ENABLE_VPID
;
350 static inline int cpu_has_virtual_nmis(void)
352 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
355 static inline bool report_flexpriority(void)
357 return flexpriority_enabled
;
360 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
364 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
365 if (vmx_msr_index
[vmx
->guest_msrs
[i
].index
] == msr
)
370 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
376 } operand
= { vpid
, 0, gva
};
378 asm volatile (__ex(ASM_VMX_INVVPID
)
379 /* CF==1 or ZF==1 --> rc = -1 */
381 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
384 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
388 } operand
= {eptp
, gpa
};
390 asm volatile (__ex(ASM_VMX_INVEPT
)
391 /* CF==1 or ZF==1 --> rc = -1 */
392 "; ja 1f ; ud2 ; 1:\n"
393 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
396 static struct shared_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
400 i
= __find_msr_index(vmx
, msr
);
402 return &vmx
->guest_msrs
[i
];
406 static void vmcs_clear(struct vmcs
*vmcs
)
408 u64 phys_addr
= __pa(vmcs
);
411 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
412 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
415 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
419 static void __vcpu_clear(void *arg
)
421 struct vcpu_vmx
*vmx
= arg
;
422 int cpu
= raw_smp_processor_id();
424 if (vmx
->vcpu
.cpu
== cpu
)
425 vmcs_clear(vmx
->vmcs
);
426 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
427 per_cpu(current_vmcs
, cpu
) = NULL
;
428 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
429 list_del(&vmx
->local_vcpus_link
);
434 static void vcpu_clear(struct vcpu_vmx
*vmx
)
436 if (vmx
->vcpu
.cpu
== -1)
438 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 1);
441 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
446 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
449 static inline void ept_sync_global(void)
451 if (cpu_has_vmx_invept_global())
452 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
455 static inline void ept_sync_context(u64 eptp
)
458 if (cpu_has_vmx_invept_context())
459 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
465 static inline void ept_sync_individual_addr(u64 eptp
, gpa_t gpa
)
468 if (cpu_has_vmx_invept_individual_addr())
469 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR
,
472 ept_sync_context(eptp
);
476 static unsigned long vmcs_readl(unsigned long field
)
480 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX
)
481 : "=a"(value
) : "d"(field
) : "cc");
485 static u16
vmcs_read16(unsigned long field
)
487 return vmcs_readl(field
);
490 static u32
vmcs_read32(unsigned long field
)
492 return vmcs_readl(field
);
495 static u64
vmcs_read64(unsigned long field
)
498 return vmcs_readl(field
);
500 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
504 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
506 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
507 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
511 static void vmcs_writel(unsigned long field
, unsigned long value
)
515 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
516 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
518 vmwrite_error(field
, value
);
521 static void vmcs_write16(unsigned long field
, u16 value
)
523 vmcs_writel(field
, value
);
526 static void vmcs_write32(unsigned long field
, u32 value
)
528 vmcs_writel(field
, value
);
531 static void vmcs_write64(unsigned long field
, u64 value
)
533 vmcs_writel(field
, value
);
534 #ifndef CONFIG_X86_64
536 vmcs_writel(field
+1, value
>> 32);
540 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
542 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
545 static void vmcs_set_bits(unsigned long field
, u32 mask
)
547 vmcs_writel(field
, vmcs_readl(field
) | mask
);
550 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
554 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
) | (1u << MC_VECTOR
);
555 if (!vcpu
->fpu_active
)
556 eb
|= 1u << NM_VECTOR
;
558 * Unconditionally intercept #DB so we can maintain dr6 without
559 * reading it every exit.
561 eb
|= 1u << DB_VECTOR
;
562 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
563 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
564 eb
|= 1u << BP_VECTOR
;
566 if (to_vmx(vcpu
)->rmode
.vm86_active
)
569 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
570 vmcs_write32(EXCEPTION_BITMAP
, eb
);
573 static void reload_tss(void)
576 * VT restores TR but not its size. Useless.
578 struct descriptor_table gdt
;
579 struct desc_struct
*descs
;
582 descs
= (void *)gdt
.base
;
583 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
587 static bool update_transition_efer(struct vcpu_vmx
*vmx
, int efer_offset
)
592 guest_efer
= vmx
->vcpu
.arch
.shadow_efer
;
595 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
598 ignore_bits
= EFER_NX
| EFER_SCE
;
600 ignore_bits
|= EFER_LMA
| EFER_LME
;
601 /* SCE is meaningful only in long mode on Intel */
602 if (guest_efer
& EFER_LMA
)
603 ignore_bits
&= ~(u64
)EFER_SCE
;
605 guest_efer
&= ~ignore_bits
;
606 guest_efer
|= host_efer
& ignore_bits
;
607 vmx
->guest_msrs
[efer_offset
].data
= guest_efer
;
608 vmx
->guest_msrs
[efer_offset
].mask
= ~ignore_bits
;
612 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
614 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
617 if (vmx
->host_state
.loaded
)
620 vmx
->host_state
.loaded
= 1;
622 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
623 * allow segment selectors with cpl > 0 or ti == 1.
625 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
626 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
627 vmx
->host_state
.fs_sel
= kvm_read_fs();
628 if (!(vmx
->host_state
.fs_sel
& 7)) {
629 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
630 vmx
->host_state
.fs_reload_needed
= 0;
632 vmcs_write16(HOST_FS_SELECTOR
, 0);
633 vmx
->host_state
.fs_reload_needed
= 1;
635 vmx
->host_state
.gs_sel
= kvm_read_gs();
636 if (!(vmx
->host_state
.gs_sel
& 7))
637 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
639 vmcs_write16(HOST_GS_SELECTOR
, 0);
640 vmx
->host_state
.gs_ldt_reload_needed
= 1;
644 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
645 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
647 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
648 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
652 if (is_long_mode(&vmx
->vcpu
)) {
653 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
654 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
657 for (i
= 0; i
< vmx
->save_nmsrs
; ++i
)
658 kvm_set_shared_msr(vmx
->guest_msrs
[i
].index
,
659 vmx
->guest_msrs
[i
].data
,
660 vmx
->guest_msrs
[i
].mask
);
663 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
667 if (!vmx
->host_state
.loaded
)
670 ++vmx
->vcpu
.stat
.host_state_reload
;
671 vmx
->host_state
.loaded
= 0;
672 if (vmx
->host_state
.fs_reload_needed
)
673 kvm_load_fs(vmx
->host_state
.fs_sel
);
674 if (vmx
->host_state
.gs_ldt_reload_needed
) {
675 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
677 * If we have to reload gs, we must take care to
678 * preserve our gs base.
680 local_irq_save(flags
);
681 kvm_load_gs(vmx
->host_state
.gs_sel
);
683 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
685 local_irq_restore(flags
);
689 if (is_long_mode(&vmx
->vcpu
)) {
690 rdmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_guest_kernel_gs_base
);
691 wrmsrl(MSR_KERNEL_GS_BASE
, vmx
->msr_host_kernel_gs_base
);
696 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
699 __vmx_load_host_state(vmx
);
704 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
705 * vcpu mutex is already taken.
707 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
709 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
710 u64 phys_addr
= __pa(vmx
->vmcs
);
711 u64 tsc_this
, delta
, new_offset
;
713 if (vcpu
->cpu
!= cpu
) {
715 kvm_migrate_timers(vcpu
);
716 set_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
);
718 list_add(&vmx
->local_vcpus_link
,
719 &per_cpu(vcpus_on_cpu
, cpu
));
723 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
726 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
727 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
728 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
731 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
732 vmx
->vmcs
, phys_addr
);
735 if (vcpu
->cpu
!= cpu
) {
736 struct descriptor_table dt
;
737 unsigned long sysenter_esp
;
741 * Linux uses per-cpu TSS and GDT, so set these when switching
744 vmcs_writel(HOST_TR_BASE
, kvm_read_tr_base()); /* 22.2.4 */
746 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
748 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
749 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
752 * Make sure the time stamp counter is monotonous.
755 if (tsc_this
< vcpu
->arch
.host_tsc
) {
756 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
757 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
758 vmcs_write64(TSC_OFFSET
, new_offset
);
763 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
765 __vmx_load_host_state(to_vmx(vcpu
));
768 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
770 if (vcpu
->fpu_active
)
772 vcpu
->fpu_active
= 1;
773 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
774 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
775 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
776 update_exception_bitmap(vcpu
);
779 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
781 if (!vcpu
->fpu_active
)
783 vcpu
->fpu_active
= 0;
784 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
785 update_exception_bitmap(vcpu
);
788 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
790 unsigned long rflags
;
792 rflags
= vmcs_readl(GUEST_RFLAGS
);
793 if (to_vmx(vcpu
)->rmode
.vm86_active
)
794 rflags
&= ~(unsigned long)(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
798 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
800 if (to_vmx(vcpu
)->rmode
.vm86_active
)
801 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
802 vmcs_writel(GUEST_RFLAGS
, rflags
);
805 static u32
vmx_get_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
807 u32 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
810 if (interruptibility
& GUEST_INTR_STATE_STI
)
811 ret
|= X86_SHADOW_INT_STI
;
812 if (interruptibility
& GUEST_INTR_STATE_MOV_SS
)
813 ret
|= X86_SHADOW_INT_MOV_SS
;
818 static void vmx_set_interrupt_shadow(struct kvm_vcpu
*vcpu
, int mask
)
820 u32 interruptibility_old
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
821 u32 interruptibility
= interruptibility_old
;
823 interruptibility
&= ~(GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
);
825 if (mask
& X86_SHADOW_INT_MOV_SS
)
826 interruptibility
|= GUEST_INTR_STATE_MOV_SS
;
827 if (mask
& X86_SHADOW_INT_STI
)
828 interruptibility
|= GUEST_INTR_STATE_STI
;
830 if ((interruptibility
!= interruptibility_old
))
831 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, interruptibility
);
834 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
838 rip
= kvm_rip_read(vcpu
);
839 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
840 kvm_rip_write(vcpu
, rip
);
842 /* skipping an emulated instruction also counts */
843 vmx_set_interrupt_shadow(vcpu
, 0);
846 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
847 bool has_error_code
, u32 error_code
)
849 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
850 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
852 if (has_error_code
) {
853 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
854 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
857 if (vmx
->rmode
.vm86_active
) {
858 vmx
->rmode
.irq
.pending
= true;
859 vmx
->rmode
.irq
.vector
= nr
;
860 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
861 if (kvm_exception_is_soft(nr
))
862 vmx
->rmode
.irq
.rip
+=
863 vmx
->vcpu
.arch
.event_exit_inst_len
;
864 intr_info
|= INTR_TYPE_SOFT_INTR
;
865 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
866 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
867 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
871 if (kvm_exception_is_soft(nr
)) {
872 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
873 vmx
->vcpu
.arch
.event_exit_inst_len
);
874 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
876 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
878 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
882 * Swap MSR entry in host/guest MSR entry array.
884 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
886 struct shared_msr_entry tmp
;
888 tmp
= vmx
->guest_msrs
[to
];
889 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
890 vmx
->guest_msrs
[from
] = tmp
;
894 * Set up the vmcs to automatically save and restore system
895 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
896 * mode, as fiddling with msrs is very expensive.
898 static void setup_msrs(struct vcpu_vmx
*vmx
)
900 int save_nmsrs
, index
;
901 unsigned long *msr_bitmap
;
903 vmx_load_host_state(vmx
);
906 if (is_long_mode(&vmx
->vcpu
)) {
907 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
909 move_msr_up(vmx
, index
, save_nmsrs
++);
910 index
= __find_msr_index(vmx
, MSR_LSTAR
);
912 move_msr_up(vmx
, index
, save_nmsrs
++);
913 index
= __find_msr_index(vmx
, MSR_CSTAR
);
915 move_msr_up(vmx
, index
, save_nmsrs
++);
917 * MSR_K6_STAR is only needed on long mode guests, and only
918 * if efer.sce is enabled.
920 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
921 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
922 move_msr_up(vmx
, index
, save_nmsrs
++);
925 index
= __find_msr_index(vmx
, MSR_EFER
);
926 if (index
>= 0 && update_transition_efer(vmx
, index
))
927 move_msr_up(vmx
, index
, save_nmsrs
++);
929 vmx
->save_nmsrs
= save_nmsrs
;
931 if (cpu_has_vmx_msr_bitmap()) {
932 if (is_long_mode(&vmx
->vcpu
))
933 msr_bitmap
= vmx_msr_bitmap_longmode
;
935 msr_bitmap
= vmx_msr_bitmap_legacy
;
937 vmcs_write64(MSR_BITMAP
, __pa(msr_bitmap
));
942 * reads and returns guest's timestamp counter "register"
943 * guest_tsc = host_tsc + tsc_offset -- 21.3
945 static u64
guest_read_tsc(void)
947 u64 host_tsc
, tsc_offset
;
950 tsc_offset
= vmcs_read64(TSC_OFFSET
);
951 return host_tsc
+ tsc_offset
;
955 * writes 'guest_tsc' into guest's timestamp counter "register"
956 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
958 static void guest_write_tsc(u64 guest_tsc
, u64 host_tsc
)
960 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
964 * Reads an msr value (of 'msr_index') into 'pdata'.
965 * Returns 0 on success, non-0 otherwise.
966 * Assumes vcpu_load() was already called.
968 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
971 struct shared_msr_entry
*msr
;
974 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
981 data
= vmcs_readl(GUEST_FS_BASE
);
984 data
= vmcs_readl(GUEST_GS_BASE
);
986 case MSR_KERNEL_GS_BASE
:
987 vmx_load_host_state(to_vmx(vcpu
));
988 data
= to_vmx(vcpu
)->msr_guest_kernel_gs_base
;
992 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
994 data
= guest_read_tsc();
996 case MSR_IA32_SYSENTER_CS
:
997 data
= vmcs_read32(GUEST_SYSENTER_CS
);
999 case MSR_IA32_SYSENTER_EIP
:
1000 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
1002 case MSR_IA32_SYSENTER_ESP
:
1003 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
1006 vmx_load_host_state(to_vmx(vcpu
));
1007 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
1009 vmx_load_host_state(to_vmx(vcpu
));
1013 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
1021 * Writes msr value into into the appropriate "register".
1022 * Returns 0 on success, non-0 otherwise.
1023 * Assumes vcpu_load() was already called.
1025 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
1027 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1028 struct shared_msr_entry
*msr
;
1032 switch (msr_index
) {
1034 vmx_load_host_state(vmx
);
1035 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
1037 #ifdef CONFIG_X86_64
1039 vmcs_writel(GUEST_FS_BASE
, data
);
1042 vmcs_writel(GUEST_GS_BASE
, data
);
1044 case MSR_KERNEL_GS_BASE
:
1045 vmx_load_host_state(vmx
);
1046 vmx
->msr_guest_kernel_gs_base
= data
;
1049 case MSR_IA32_SYSENTER_CS
:
1050 vmcs_write32(GUEST_SYSENTER_CS
, data
);
1052 case MSR_IA32_SYSENTER_EIP
:
1053 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
1055 case MSR_IA32_SYSENTER_ESP
:
1056 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
1060 guest_write_tsc(data
, host_tsc
);
1062 case MSR_IA32_CR_PAT
:
1063 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
1064 vmcs_write64(GUEST_IA32_PAT
, data
);
1065 vcpu
->arch
.pat
= data
;
1068 /* Otherwise falls through to kvm_set_msr_common */
1070 msr
= find_msr_entry(vmx
, msr_index
);
1072 vmx_load_host_state(vmx
);
1076 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
1082 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1084 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
1087 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
1090 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
1092 case VCPU_EXREG_PDPTR
:
1094 ept_save_pdptrs(vcpu
);
1101 static void set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1103 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1104 vmcs_writel(GUEST_DR7
, dbg
->arch
.debugreg
[7]);
1106 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
1108 update_exception_bitmap(vcpu
);
1111 static __init
int cpu_has_kvm_support(void)
1113 return cpu_has_vmx();
1116 static __init
int vmx_disabled_by_bios(void)
1120 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
1121 return (msr
& (FEATURE_CONTROL_LOCKED
|
1122 FEATURE_CONTROL_VMXON_ENABLED
))
1123 == FEATURE_CONTROL_LOCKED
;
1124 /* locked but not enabled */
1127 static int hardware_enable(void *garbage
)
1129 int cpu
= raw_smp_processor_id();
1130 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
1133 if (read_cr4() & X86_CR4_VMXE
)
1136 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu
, cpu
));
1137 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
1138 if ((old
& (FEATURE_CONTROL_LOCKED
|
1139 FEATURE_CONTROL_VMXON_ENABLED
))
1140 != (FEATURE_CONTROL_LOCKED
|
1141 FEATURE_CONTROL_VMXON_ENABLED
))
1142 /* enable and lock */
1143 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
1144 FEATURE_CONTROL_LOCKED
|
1145 FEATURE_CONTROL_VMXON_ENABLED
);
1146 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
1147 asm volatile (ASM_VMX_VMXON_RAX
1148 : : "a"(&phys_addr
), "m"(phys_addr
)
1156 static void vmclear_local_vcpus(void)
1158 int cpu
= raw_smp_processor_id();
1159 struct vcpu_vmx
*vmx
, *n
;
1161 list_for_each_entry_safe(vmx
, n
, &per_cpu(vcpus_on_cpu
, cpu
),
1167 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1170 static void kvm_cpu_vmxoff(void)
1172 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
1173 write_cr4(read_cr4() & ~X86_CR4_VMXE
);
1176 static void hardware_disable(void *garbage
)
1178 vmclear_local_vcpus();
1182 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1183 u32 msr
, u32
*result
)
1185 u32 vmx_msr_low
, vmx_msr_high
;
1186 u32 ctl
= ctl_min
| ctl_opt
;
1188 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1190 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1191 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1193 /* Ensure minimum (required) set of control bits are supported. */
1201 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1203 u32 vmx_msr_low
, vmx_msr_high
;
1204 u32 min
, opt
, min2
, opt2
;
1205 u32 _pin_based_exec_control
= 0;
1206 u32 _cpu_based_exec_control
= 0;
1207 u32 _cpu_based_2nd_exec_control
= 0;
1208 u32 _vmexit_control
= 0;
1209 u32 _vmentry_control
= 0;
1211 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1212 opt
= PIN_BASED_VIRTUAL_NMIS
;
1213 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1214 &_pin_based_exec_control
) < 0)
1217 min
= CPU_BASED_HLT_EXITING
|
1218 #ifdef CONFIG_X86_64
1219 CPU_BASED_CR8_LOAD_EXITING
|
1220 CPU_BASED_CR8_STORE_EXITING
|
1222 CPU_BASED_CR3_LOAD_EXITING
|
1223 CPU_BASED_CR3_STORE_EXITING
|
1224 CPU_BASED_USE_IO_BITMAPS
|
1225 CPU_BASED_MOV_DR_EXITING
|
1226 CPU_BASED_USE_TSC_OFFSETING
|
1227 CPU_BASED_MWAIT_EXITING
|
1228 CPU_BASED_MONITOR_EXITING
|
1229 CPU_BASED_INVLPG_EXITING
;
1230 opt
= CPU_BASED_TPR_SHADOW
|
1231 CPU_BASED_USE_MSR_BITMAPS
|
1232 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1233 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1234 &_cpu_based_exec_control
) < 0)
1236 #ifdef CONFIG_X86_64
1237 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1238 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1239 ~CPU_BASED_CR8_STORE_EXITING
;
1241 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1243 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1244 SECONDARY_EXEC_WBINVD_EXITING
|
1245 SECONDARY_EXEC_ENABLE_VPID
|
1246 SECONDARY_EXEC_ENABLE_EPT
|
1247 SECONDARY_EXEC_UNRESTRICTED_GUEST
|
1248 SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
1249 if (adjust_vmx_controls(min2
, opt2
,
1250 MSR_IA32_VMX_PROCBASED_CTLS2
,
1251 &_cpu_based_2nd_exec_control
) < 0)
1254 #ifndef CONFIG_X86_64
1255 if (!(_cpu_based_2nd_exec_control
&
1256 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1257 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1259 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1260 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1262 _cpu_based_exec_control
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1263 CPU_BASED_CR3_STORE_EXITING
|
1264 CPU_BASED_INVLPG_EXITING
);
1265 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1266 vmx_capability
.ept
, vmx_capability
.vpid
);
1270 #ifdef CONFIG_X86_64
1271 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1273 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
;
1274 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1275 &_vmexit_control
) < 0)
1279 opt
= VM_ENTRY_LOAD_IA32_PAT
;
1280 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1281 &_vmentry_control
) < 0)
1284 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1286 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1287 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1290 #ifdef CONFIG_X86_64
1291 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1292 if (vmx_msr_high
& (1u<<16))
1296 /* Require Write-Back (WB) memory type for VMCS accesses. */
1297 if (((vmx_msr_high
>> 18) & 15) != 6)
1300 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1301 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1302 vmcs_conf
->revision_id
= vmx_msr_low
;
1304 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1305 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1306 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1307 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1308 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1313 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1315 int node
= cpu_to_node(cpu
);
1319 pages
= alloc_pages_exact_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1322 vmcs
= page_address(pages
);
1323 memset(vmcs
, 0, vmcs_config
.size
);
1324 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1328 static struct vmcs
*alloc_vmcs(void)
1330 return alloc_vmcs_cpu(raw_smp_processor_id());
1333 static void free_vmcs(struct vmcs
*vmcs
)
1335 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1338 static void free_kvm_area(void)
1342 for_each_possible_cpu(cpu
) {
1343 free_vmcs(per_cpu(vmxarea
, cpu
));
1344 per_cpu(vmxarea
, cpu
) = NULL
;
1348 static __init
int alloc_kvm_area(void)
1352 for_each_possible_cpu(cpu
) {
1355 vmcs
= alloc_vmcs_cpu(cpu
);
1361 per_cpu(vmxarea
, cpu
) = vmcs
;
1366 static __init
int hardware_setup(void)
1368 if (setup_vmcs_config(&vmcs_config
) < 0)
1371 if (boot_cpu_has(X86_FEATURE_NX
))
1372 kvm_enable_efer_bits(EFER_NX
);
1374 if (!cpu_has_vmx_vpid())
1377 if (!cpu_has_vmx_ept()) {
1379 enable_unrestricted_guest
= 0;
1382 if (!cpu_has_vmx_unrestricted_guest())
1383 enable_unrestricted_guest
= 0;
1385 if (!cpu_has_vmx_flexpriority())
1386 flexpriority_enabled
= 0;
1388 if (!cpu_has_vmx_tpr_shadow())
1389 kvm_x86_ops
->update_cr8_intercept
= NULL
;
1391 if (enable_ept
&& !cpu_has_vmx_ept_2m_page())
1392 kvm_disable_largepages();
1394 if (!cpu_has_vmx_ple())
1397 return alloc_kvm_area();
1400 static __exit
void hardware_unsetup(void)
1405 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1407 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1409 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1410 vmcs_write16(sf
->selector
, save
->selector
);
1411 vmcs_writel(sf
->base
, save
->base
);
1412 vmcs_write32(sf
->limit
, save
->limit
);
1413 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1415 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1417 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1421 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1423 unsigned long flags
;
1424 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1426 vmx
->emulation_required
= 1;
1427 vmx
->rmode
.vm86_active
= 0;
1429 vmcs_writel(GUEST_TR_BASE
, vmx
->rmode
.tr
.base
);
1430 vmcs_write32(GUEST_TR_LIMIT
, vmx
->rmode
.tr
.limit
);
1431 vmcs_write32(GUEST_TR_AR_BYTES
, vmx
->rmode
.tr
.ar
);
1433 flags
= vmcs_readl(GUEST_RFLAGS
);
1434 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1435 flags
|= (vmx
->rmode
.save_iopl
<< IOPL_SHIFT
);
1436 vmcs_writel(GUEST_RFLAGS
, flags
);
1438 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1439 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1441 update_exception_bitmap(vcpu
);
1443 if (emulate_invalid_guest_state
)
1446 fix_pmode_dataseg(VCPU_SREG_ES
, &vmx
->rmode
.es
);
1447 fix_pmode_dataseg(VCPU_SREG_DS
, &vmx
->rmode
.ds
);
1448 fix_pmode_dataseg(VCPU_SREG_GS
, &vmx
->rmode
.gs
);
1449 fix_pmode_dataseg(VCPU_SREG_FS
, &vmx
->rmode
.fs
);
1451 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1452 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1454 vmcs_write16(GUEST_CS_SELECTOR
,
1455 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1456 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1459 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1461 if (!kvm
->arch
.tss_addr
) {
1462 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1463 kvm
->memslots
[0].npages
- 3;
1464 return base_gfn
<< PAGE_SHIFT
;
1466 return kvm
->arch
.tss_addr
;
1469 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1471 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1473 save
->selector
= vmcs_read16(sf
->selector
);
1474 save
->base
= vmcs_readl(sf
->base
);
1475 save
->limit
= vmcs_read32(sf
->limit
);
1476 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1477 vmcs_write16(sf
->selector
, save
->base
>> 4);
1478 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1479 vmcs_write32(sf
->limit
, 0xffff);
1480 vmcs_write32(sf
->ar_bytes
, 0xf3);
1483 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1485 unsigned long flags
;
1486 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1488 if (enable_unrestricted_guest
)
1491 vmx
->emulation_required
= 1;
1492 vmx
->rmode
.vm86_active
= 1;
1494 vmx
->rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1495 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1497 vmx
->rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1498 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1500 vmx
->rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1501 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1503 flags
= vmcs_readl(GUEST_RFLAGS
);
1504 vmx
->rmode
.save_iopl
1505 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1507 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1509 vmcs_writel(GUEST_RFLAGS
, flags
);
1510 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1511 update_exception_bitmap(vcpu
);
1513 if (emulate_invalid_guest_state
)
1514 goto continue_rmode
;
1516 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1517 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1518 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1520 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1521 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1522 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1523 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1524 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1526 fix_rmode_seg(VCPU_SREG_ES
, &vmx
->rmode
.es
);
1527 fix_rmode_seg(VCPU_SREG_DS
, &vmx
->rmode
.ds
);
1528 fix_rmode_seg(VCPU_SREG_GS
, &vmx
->rmode
.gs
);
1529 fix_rmode_seg(VCPU_SREG_FS
, &vmx
->rmode
.fs
);
1532 kvm_mmu_reset_context(vcpu
);
1533 init_rmode(vcpu
->kvm
);
1536 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1538 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1539 struct shared_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1545 * Force kernel_gs_base reloading before EFER changes, as control
1546 * of this msr depends on is_long_mode().
1548 vmx_load_host_state(to_vmx(vcpu
));
1549 vcpu
->arch
.shadow_efer
= efer
;
1552 if (efer
& EFER_LMA
) {
1553 vmcs_write32(VM_ENTRY_CONTROLS
,
1554 vmcs_read32(VM_ENTRY_CONTROLS
) |
1555 VM_ENTRY_IA32E_MODE
);
1558 vmcs_write32(VM_ENTRY_CONTROLS
,
1559 vmcs_read32(VM_ENTRY_CONTROLS
) &
1560 ~VM_ENTRY_IA32E_MODE
);
1562 msr
->data
= efer
& ~EFER_LME
;
1567 #ifdef CONFIG_X86_64
1569 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1573 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1574 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1575 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1577 vmcs_write32(GUEST_TR_AR_BYTES
,
1578 (guest_tr_ar
& ~AR_TYPE_MASK
)
1579 | AR_TYPE_BUSY_64_TSS
);
1581 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1582 vmx_set_efer(vcpu
, vcpu
->arch
.shadow_efer
);
1585 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1587 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1589 vmcs_write32(VM_ENTRY_CONTROLS
,
1590 vmcs_read32(VM_ENTRY_CONTROLS
)
1591 & ~VM_ENTRY_IA32E_MODE
);
1596 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1598 vpid_sync_vcpu_all(to_vmx(vcpu
));
1600 ept_sync_context(construct_eptp(vcpu
->arch
.mmu
.root_hpa
));
1603 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1605 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1606 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1609 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
1611 if (!test_bit(VCPU_EXREG_PDPTR
,
1612 (unsigned long *)&vcpu
->arch
.regs_dirty
))
1615 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1616 vmcs_write64(GUEST_PDPTR0
, vcpu
->arch
.pdptrs
[0]);
1617 vmcs_write64(GUEST_PDPTR1
, vcpu
->arch
.pdptrs
[1]);
1618 vmcs_write64(GUEST_PDPTR2
, vcpu
->arch
.pdptrs
[2]);
1619 vmcs_write64(GUEST_PDPTR3
, vcpu
->arch
.pdptrs
[3]);
1623 static void ept_save_pdptrs(struct kvm_vcpu
*vcpu
)
1625 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1626 vcpu
->arch
.pdptrs
[0] = vmcs_read64(GUEST_PDPTR0
);
1627 vcpu
->arch
.pdptrs
[1] = vmcs_read64(GUEST_PDPTR1
);
1628 vcpu
->arch
.pdptrs
[2] = vmcs_read64(GUEST_PDPTR2
);
1629 vcpu
->arch
.pdptrs
[3] = vmcs_read64(GUEST_PDPTR3
);
1632 __set_bit(VCPU_EXREG_PDPTR
,
1633 (unsigned long *)&vcpu
->arch
.regs_avail
);
1634 __set_bit(VCPU_EXREG_PDPTR
,
1635 (unsigned long *)&vcpu
->arch
.regs_dirty
);
1638 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
1640 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
1642 struct kvm_vcpu
*vcpu
)
1644 if (!(cr0
& X86_CR0_PG
)) {
1645 /* From paging/starting to nonpaging */
1646 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1647 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
1648 (CPU_BASED_CR3_LOAD_EXITING
|
1649 CPU_BASED_CR3_STORE_EXITING
));
1650 vcpu
->arch
.cr0
= cr0
;
1651 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1652 } else if (!is_paging(vcpu
)) {
1653 /* From nonpaging to paging */
1654 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1655 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
1656 ~(CPU_BASED_CR3_LOAD_EXITING
|
1657 CPU_BASED_CR3_STORE_EXITING
));
1658 vcpu
->arch
.cr0
= cr0
;
1659 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1662 if (!(cr0
& X86_CR0_WP
))
1663 *hw_cr0
&= ~X86_CR0_WP
;
1666 static void ept_update_paging_mode_cr4(unsigned long *hw_cr4
,
1667 struct kvm_vcpu
*vcpu
)
1669 if (!is_paging(vcpu
)) {
1670 *hw_cr4
&= ~X86_CR4_PAE
;
1671 *hw_cr4
|= X86_CR4_PSE
;
1672 } else if (!(vcpu
->arch
.cr4
& X86_CR4_PAE
))
1673 *hw_cr4
&= ~X86_CR4_PAE
;
1676 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1678 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1679 unsigned long hw_cr0
;
1681 if (enable_unrestricted_guest
)
1682 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST
)
1683 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST
;
1685 hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
) | KVM_VM_CR0_ALWAYS_ON
;
1687 vmx_fpu_deactivate(vcpu
);
1689 if (vmx
->rmode
.vm86_active
&& (cr0
& X86_CR0_PE
))
1692 if (!vmx
->rmode
.vm86_active
&& !(cr0
& X86_CR0_PE
))
1695 #ifdef CONFIG_X86_64
1696 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1697 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1699 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1705 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
1707 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1708 vmcs_writel(GUEST_CR0
, hw_cr0
);
1709 vcpu
->arch
.cr0
= cr0
;
1711 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1712 vmx_fpu_activate(vcpu
);
1715 static u64
construct_eptp(unsigned long root_hpa
)
1719 /* TODO write the value reading from MSR */
1720 eptp
= VMX_EPT_DEFAULT_MT
|
1721 VMX_EPT_DEFAULT_GAW
<< VMX_EPT_GAW_EPTP_SHIFT
;
1722 eptp
|= (root_hpa
& PAGE_MASK
);
1727 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1729 unsigned long guest_cr3
;
1734 eptp
= construct_eptp(cr3
);
1735 vmcs_write64(EPT_POINTER
, eptp
);
1736 guest_cr3
= is_paging(vcpu
) ? vcpu
->arch
.cr3
:
1737 vcpu
->kvm
->arch
.ept_identity_map_addr
;
1738 ept_load_pdptrs(vcpu
);
1741 vmx_flush_tlb(vcpu
);
1742 vmcs_writel(GUEST_CR3
, guest_cr3
);
1743 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1744 vmx_fpu_deactivate(vcpu
);
1747 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1749 unsigned long hw_cr4
= cr4
| (to_vmx(vcpu
)->rmode
.vm86_active
?
1750 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
1752 vcpu
->arch
.cr4
= cr4
;
1754 ept_update_paging_mode_cr4(&hw_cr4
, vcpu
);
1756 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1757 vmcs_writel(GUEST_CR4
, hw_cr4
);
1760 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1762 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1764 return vmcs_readl(sf
->base
);
1767 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1768 struct kvm_segment
*var
, int seg
)
1770 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1773 var
->base
= vmcs_readl(sf
->base
);
1774 var
->limit
= vmcs_read32(sf
->limit
);
1775 var
->selector
= vmcs_read16(sf
->selector
);
1776 ar
= vmcs_read32(sf
->ar_bytes
);
1777 if ((ar
& AR_UNUSABLE_MASK
) && !emulate_invalid_guest_state
)
1779 var
->type
= ar
& 15;
1780 var
->s
= (ar
>> 4) & 1;
1781 var
->dpl
= (ar
>> 5) & 3;
1782 var
->present
= (ar
>> 7) & 1;
1783 var
->avl
= (ar
>> 12) & 1;
1784 var
->l
= (ar
>> 13) & 1;
1785 var
->db
= (ar
>> 14) & 1;
1786 var
->g
= (ar
>> 15) & 1;
1787 var
->unusable
= (ar
>> 16) & 1;
1790 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1792 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1795 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1798 return vmcs_read16(GUEST_CS_SELECTOR
) & 3;
1801 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1808 ar
= var
->type
& 15;
1809 ar
|= (var
->s
& 1) << 4;
1810 ar
|= (var
->dpl
& 3) << 5;
1811 ar
|= (var
->present
& 1) << 7;
1812 ar
|= (var
->avl
& 1) << 12;
1813 ar
|= (var
->l
& 1) << 13;
1814 ar
|= (var
->db
& 1) << 14;
1815 ar
|= (var
->g
& 1) << 15;
1817 if (ar
== 0) /* a 0 value means unusable */
1818 ar
= AR_UNUSABLE_MASK
;
1823 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1824 struct kvm_segment
*var
, int seg
)
1826 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1827 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1830 if (vmx
->rmode
.vm86_active
&& seg
== VCPU_SREG_TR
) {
1831 vmx
->rmode
.tr
.selector
= var
->selector
;
1832 vmx
->rmode
.tr
.base
= var
->base
;
1833 vmx
->rmode
.tr
.limit
= var
->limit
;
1834 vmx
->rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1837 vmcs_writel(sf
->base
, var
->base
);
1838 vmcs_write32(sf
->limit
, var
->limit
);
1839 vmcs_write16(sf
->selector
, var
->selector
);
1840 if (vmx
->rmode
.vm86_active
&& var
->s
) {
1842 * Hack real-mode segments into vm86 compatibility.
1844 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1845 vmcs_writel(sf
->base
, 0xf0000);
1848 ar
= vmx_segment_access_rights(var
);
1851 * Fix the "Accessed" bit in AR field of segment registers for older
1853 * IA32 arch specifies that at the time of processor reset the
1854 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1855 * is setting it to 0 in the usedland code. This causes invalid guest
1856 * state vmexit when "unrestricted guest" mode is turned on.
1857 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1858 * tree. Newer qemu binaries with that qemu fix would not need this
1861 if (enable_unrestricted_guest
&& (seg
!= VCPU_SREG_LDTR
))
1862 ar
|= 0x1; /* Accessed */
1864 vmcs_write32(sf
->ar_bytes
, ar
);
1867 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1869 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1871 *db
= (ar
>> 14) & 1;
1872 *l
= (ar
>> 13) & 1;
1875 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1877 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1878 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1881 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1883 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1884 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1887 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1889 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1890 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1893 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1895 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1896 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1899 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1901 struct kvm_segment var
;
1904 vmx_get_segment(vcpu
, &var
, seg
);
1905 ar
= vmx_segment_access_rights(&var
);
1907 if (var
.base
!= (var
.selector
<< 4))
1909 if (var
.limit
!= 0xffff)
1917 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
1919 struct kvm_segment cs
;
1920 unsigned int cs_rpl
;
1922 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1923 cs_rpl
= cs
.selector
& SELECTOR_RPL_MASK
;
1927 if (~cs
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_ACCESSES_MASK
))
1931 if (cs
.type
& AR_TYPE_WRITEABLE_MASK
) {
1932 if (cs
.dpl
> cs_rpl
)
1935 if (cs
.dpl
!= cs_rpl
)
1941 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1945 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
1947 struct kvm_segment ss
;
1948 unsigned int ss_rpl
;
1950 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1951 ss_rpl
= ss
.selector
& SELECTOR_RPL_MASK
;
1955 if (ss
.type
!= 3 && ss
.type
!= 7)
1959 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
1967 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1969 struct kvm_segment var
;
1972 vmx_get_segment(vcpu
, &var
, seg
);
1973 rpl
= var
.selector
& SELECTOR_RPL_MASK
;
1981 if (~var
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_WRITEABLE_MASK
)) {
1982 if (var
.dpl
< rpl
) /* DPL < RPL */
1986 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1992 static bool tr_valid(struct kvm_vcpu
*vcpu
)
1994 struct kvm_segment tr
;
1996 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
2000 if (tr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
2002 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
2010 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
2012 struct kvm_segment ldtr
;
2014 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
2018 if (ldtr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
2028 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
2030 struct kvm_segment cs
, ss
;
2032 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2033 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
2035 return ((cs
.selector
& SELECTOR_RPL_MASK
) ==
2036 (ss
.selector
& SELECTOR_RPL_MASK
));
2040 * Check if guest state is valid. Returns true if valid, false if
2042 * We assume that registers are always usable
2044 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
2046 /* real mode guest state checks */
2047 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) {
2048 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
2050 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
2052 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
2054 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
2056 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
2058 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
2061 /* protected mode guest state checks */
2062 if (!cs_ss_rpl_check(vcpu
))
2064 if (!code_segment_valid(vcpu
))
2066 if (!stack_segment_valid(vcpu
))
2068 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
2070 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
2072 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
2074 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
2076 if (!tr_valid(vcpu
))
2078 if (!ldtr_valid(vcpu
))
2082 * - Add checks on RIP
2083 * - Add checks on RFLAGS
2089 static int init_rmode_tss(struct kvm
*kvm
)
2091 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
2096 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
2099 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
2100 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
2101 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
2104 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
2107 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
2111 r
= kvm_write_guest_page(kvm
, fn
, &data
,
2112 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
2122 static int init_rmode_identity_map(struct kvm
*kvm
)
2125 pfn_t identity_map_pfn
;
2130 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
2131 printk(KERN_ERR
"EPT: identity-mapping pagetable "
2132 "haven't been allocated!\n");
2135 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
2138 identity_map_pfn
= kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
;
2139 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
2142 /* Set up identity-mapping pagetable for EPT in real mode */
2143 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
2144 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
2145 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
2146 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
2147 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
2151 kvm
->arch
.ept_identity_pagetable_done
= true;
2157 static void seg_setup(int seg
)
2159 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
2162 vmcs_write16(sf
->selector
, 0);
2163 vmcs_writel(sf
->base
, 0);
2164 vmcs_write32(sf
->limit
, 0xffff);
2165 if (enable_unrestricted_guest
) {
2167 if (seg
== VCPU_SREG_CS
)
2168 ar
|= 0x08; /* code segment */
2172 vmcs_write32(sf
->ar_bytes
, ar
);
2175 static int alloc_apic_access_page(struct kvm
*kvm
)
2177 struct kvm_userspace_memory_region kvm_userspace_mem
;
2180 down_write(&kvm
->slots_lock
);
2181 if (kvm
->arch
.apic_access_page
)
2183 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
2184 kvm_userspace_mem
.flags
= 0;
2185 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
2186 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2187 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2191 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
2193 up_write(&kvm
->slots_lock
);
2197 static int alloc_identity_pagetable(struct kvm
*kvm
)
2199 struct kvm_userspace_memory_region kvm_userspace_mem
;
2202 down_write(&kvm
->slots_lock
);
2203 if (kvm
->arch
.ept_identity_pagetable
)
2205 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
2206 kvm_userspace_mem
.flags
= 0;
2207 kvm_userspace_mem
.guest_phys_addr
=
2208 kvm
->arch
.ept_identity_map_addr
;
2209 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2210 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2214 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
2215 kvm
->arch
.ept_identity_map_addr
>> PAGE_SHIFT
);
2217 up_write(&kvm
->slots_lock
);
2221 static void allocate_vpid(struct vcpu_vmx
*vmx
)
2228 spin_lock(&vmx_vpid_lock
);
2229 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
2230 if (vpid
< VMX_NR_VPIDS
) {
2232 __set_bit(vpid
, vmx_vpid_bitmap
);
2234 spin_unlock(&vmx_vpid_lock
);
2237 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
, u32 msr
)
2239 int f
= sizeof(unsigned long);
2241 if (!cpu_has_vmx_msr_bitmap())
2245 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2246 * have the write-low and read-high bitmap offsets the wrong way round.
2247 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2249 if (msr
<= 0x1fff) {
2250 __clear_bit(msr
, msr_bitmap
+ 0x000 / f
); /* read-low */
2251 __clear_bit(msr
, msr_bitmap
+ 0x800 / f
); /* write-low */
2252 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
2254 __clear_bit(msr
, msr_bitmap
+ 0x400 / f
); /* read-high */
2255 __clear_bit(msr
, msr_bitmap
+ 0xc00 / f
); /* write-high */
2259 static void vmx_disable_intercept_for_msr(u32 msr
, bool longmode_only
)
2262 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy
, msr
);
2263 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode
, msr
);
2267 * Sets up the vmcs for emulated real mode.
2269 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
2271 u32 host_sysenter_cs
, msr_low
, msr_high
;
2273 u64 host_pat
, tsc_this
, tsc_base
;
2275 struct descriptor_table dt
;
2277 unsigned long kvm_vmx_return
;
2281 vmcs_write64(IO_BITMAP_A
, __pa(vmx_io_bitmap_a
));
2282 vmcs_write64(IO_BITMAP_B
, __pa(vmx_io_bitmap_b
));
2284 if (cpu_has_vmx_msr_bitmap())
2285 vmcs_write64(MSR_BITMAP
, __pa(vmx_msr_bitmap_legacy
));
2287 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
2290 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
2291 vmcs_config
.pin_based_exec_ctrl
);
2293 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
2294 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
2295 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
2296 #ifdef CONFIG_X86_64
2297 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
2298 CPU_BASED_CR8_LOAD_EXITING
;
2302 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
2303 CPU_BASED_CR3_LOAD_EXITING
|
2304 CPU_BASED_INVLPG_EXITING
;
2305 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
2307 if (cpu_has_secondary_exec_ctrls()) {
2308 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
2309 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2311 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
2313 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
2315 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
2316 enable_unrestricted_guest
= 0;
2318 if (!enable_unrestricted_guest
)
2319 exec_control
&= ~SECONDARY_EXEC_UNRESTRICTED_GUEST
;
2321 exec_control
&= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING
;
2322 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
2326 vmcs_write32(PLE_GAP
, ple_gap
);
2327 vmcs_write32(PLE_WINDOW
, ple_window
);
2330 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
2331 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
2332 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
2334 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
2335 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
2336 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2338 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
2339 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2340 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2341 vmcs_write16(HOST_FS_SELECTOR
, kvm_read_fs()); /* 22.2.4 */
2342 vmcs_write16(HOST_GS_SELECTOR
, kvm_read_gs()); /* 22.2.4 */
2343 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2344 #ifdef CONFIG_X86_64
2345 rdmsrl(MSR_FS_BASE
, a
);
2346 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
2347 rdmsrl(MSR_GS_BASE
, a
);
2348 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
2350 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
2351 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
2354 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
2357 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
2359 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
2360 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
2361 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
2362 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
2363 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
2365 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
2366 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
2367 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
2368 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
2369 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
2370 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
2372 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
2373 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2374 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2375 vmcs_write64(HOST_IA32_PAT
, host_pat
);
2377 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
2378 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2379 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2380 /* Write the default value follow host pat */
2381 vmcs_write64(GUEST_IA32_PAT
, host_pat
);
2382 /* Keep arch.pat sync with GUEST_IA32_PAT */
2383 vmx
->vcpu
.arch
.pat
= host_pat
;
2386 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
2387 u32 index
= vmx_msr_index
[i
];
2388 u32 data_low
, data_high
;
2392 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
2394 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
2396 data
= data_low
| ((u64
)data_high
<< 32);
2397 vmx
->guest_msrs
[j
].index
= i
;
2398 vmx
->guest_msrs
[j
].data
= 0;
2399 vmx
->guest_msrs
[j
].mask
= -1ull;
2403 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
2405 /* 22.2.1, 20.8.1 */
2406 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
2408 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
2409 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
2411 tsc_base
= vmx
->vcpu
.kvm
->arch
.vm_init_tsc
;
2413 if (tsc_this
< vmx
->vcpu
.kvm
->arch
.vm_init_tsc
)
2414 tsc_base
= tsc_this
;
2416 guest_write_tsc(0, tsc_base
);
2421 static int init_rmode(struct kvm
*kvm
)
2423 if (!init_rmode_tss(kvm
))
2425 if (!init_rmode_identity_map(kvm
))
2430 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
2432 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2436 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
2437 down_read(&vcpu
->kvm
->slots_lock
);
2438 if (!init_rmode(vmx
->vcpu
.kvm
)) {
2443 vmx
->rmode
.vm86_active
= 0;
2445 vmx
->soft_vnmi_blocked
= 0;
2447 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
2448 kvm_set_cr8(&vmx
->vcpu
, 0);
2449 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
2450 if (kvm_vcpu_is_bsp(&vmx
->vcpu
))
2451 msr
|= MSR_IA32_APICBASE_BSP
;
2452 kvm_set_apic_base(&vmx
->vcpu
, msr
);
2454 fx_init(&vmx
->vcpu
);
2456 seg_setup(VCPU_SREG_CS
);
2458 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2459 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2461 if (kvm_vcpu_is_bsp(&vmx
->vcpu
)) {
2462 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
2463 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
2465 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
2466 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
2469 seg_setup(VCPU_SREG_DS
);
2470 seg_setup(VCPU_SREG_ES
);
2471 seg_setup(VCPU_SREG_FS
);
2472 seg_setup(VCPU_SREG_GS
);
2473 seg_setup(VCPU_SREG_SS
);
2475 vmcs_write16(GUEST_TR_SELECTOR
, 0);
2476 vmcs_writel(GUEST_TR_BASE
, 0);
2477 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
2478 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
2480 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
2481 vmcs_writel(GUEST_LDTR_BASE
, 0);
2482 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
2483 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
2485 vmcs_write32(GUEST_SYSENTER_CS
, 0);
2486 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
2487 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
2489 vmcs_writel(GUEST_RFLAGS
, 0x02);
2490 if (kvm_vcpu_is_bsp(&vmx
->vcpu
))
2491 kvm_rip_write(vcpu
, 0xfff0);
2493 kvm_rip_write(vcpu
, 0);
2494 kvm_register_write(vcpu
, VCPU_REGS_RSP
, 0);
2496 vmcs_writel(GUEST_DR7
, 0x400);
2498 vmcs_writel(GUEST_GDTR_BASE
, 0);
2499 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
2501 vmcs_writel(GUEST_IDTR_BASE
, 0);
2502 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
2504 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
2505 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
2506 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
2508 /* Special registers */
2509 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
2513 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
2515 if (cpu_has_vmx_tpr_shadow()) {
2516 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
2517 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
2518 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
2519 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
2520 vmcs_write32(TPR_THRESHOLD
, 0);
2523 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2524 vmcs_write64(APIC_ACCESS_ADDR
,
2525 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
2528 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
2530 vmx
->vcpu
.arch
.cr0
= X86_CR0_NW
| X86_CR0_CD
| X86_CR0_ET
;
2531 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
2532 vmx_set_cr4(&vmx
->vcpu
, 0);
2533 vmx_set_efer(&vmx
->vcpu
, 0);
2534 vmx_fpu_activate(&vmx
->vcpu
);
2535 update_exception_bitmap(&vmx
->vcpu
);
2537 vpid_sync_vcpu_all(vmx
);
2541 /* HACK: Don't enable emulation on guest boot/reset */
2542 vmx
->emulation_required
= 0;
2545 up_read(&vcpu
->kvm
->slots_lock
);
2549 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2551 u32 cpu_based_vm_exec_control
;
2553 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2554 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2555 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2558 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2560 u32 cpu_based_vm_exec_control
;
2562 if (!cpu_has_virtual_nmis()) {
2563 enable_irq_window(vcpu
);
2567 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2568 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_NMI_PENDING
;
2569 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2572 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
)
2574 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2576 int irq
= vcpu
->arch
.interrupt
.nr
;
2578 trace_kvm_inj_virq(irq
);
2580 ++vcpu
->stat
.irq_injections
;
2581 if (vmx
->rmode
.vm86_active
) {
2582 vmx
->rmode
.irq
.pending
= true;
2583 vmx
->rmode
.irq
.vector
= irq
;
2584 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2585 if (vcpu
->arch
.interrupt
.soft
)
2586 vmx
->rmode
.irq
.rip
+=
2587 vmx
->vcpu
.arch
.event_exit_inst_len
;
2588 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2589 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
2590 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2591 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2594 intr
= irq
| INTR_INFO_VALID_MASK
;
2595 if (vcpu
->arch
.interrupt
.soft
) {
2596 intr
|= INTR_TYPE_SOFT_INTR
;
2597 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2598 vmx
->vcpu
.arch
.event_exit_inst_len
);
2600 intr
|= INTR_TYPE_EXT_INTR
;
2601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr
);
2604 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
2606 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2608 if (!cpu_has_virtual_nmis()) {
2610 * Tracking the NMI-blocked state in software is built upon
2611 * finding the next open IRQ window. This, in turn, depends on
2612 * well-behaving guests: They have to keep IRQs disabled at
2613 * least as long as the NMI handler runs. Otherwise we may
2614 * cause NMI nesting, maybe breaking the guest. But as this is
2615 * highly unlikely, we can live with the residual risk.
2617 vmx
->soft_vnmi_blocked
= 1;
2618 vmx
->vnmi_blocked_time
= 0;
2621 ++vcpu
->stat
.nmi_injections
;
2622 if (vmx
->rmode
.vm86_active
) {
2623 vmx
->rmode
.irq
.pending
= true;
2624 vmx
->rmode
.irq
.vector
= NMI_VECTOR
;
2625 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2626 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2627 NMI_VECTOR
| INTR_TYPE_SOFT_INTR
|
2628 INTR_INFO_VALID_MASK
);
2629 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2630 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2633 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2634 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
2637 static int vmx_nmi_allowed(struct kvm_vcpu
*vcpu
)
2639 if (!cpu_has_virtual_nmis() && to_vmx(vcpu
)->soft_vnmi_blocked
)
2642 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2643 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
|
2644 GUEST_INTR_STATE_NMI
));
2647 static bool vmx_get_nmi_mask(struct kvm_vcpu
*vcpu
)
2649 if (!cpu_has_virtual_nmis())
2650 return to_vmx(vcpu
)->soft_vnmi_blocked
;
2652 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2653 GUEST_INTR_STATE_NMI
);
2656 static void vmx_set_nmi_mask(struct kvm_vcpu
*vcpu
, bool masked
)
2658 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2660 if (!cpu_has_virtual_nmis()) {
2661 if (vmx
->soft_vnmi_blocked
!= masked
) {
2662 vmx
->soft_vnmi_blocked
= masked
;
2663 vmx
->vnmi_blocked_time
= 0;
2667 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
2668 GUEST_INTR_STATE_NMI
);
2670 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
2671 GUEST_INTR_STATE_NMI
);
2675 static int vmx_interrupt_allowed(struct kvm_vcpu
*vcpu
)
2677 return (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2678 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2679 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
));
2682 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2685 struct kvm_userspace_memory_region tss_mem
= {
2686 .slot
= TSS_PRIVATE_MEMSLOT
,
2687 .guest_phys_addr
= addr
,
2688 .memory_size
= PAGE_SIZE
* 3,
2692 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
2695 kvm
->arch
.tss_addr
= addr
;
2699 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
2700 int vec
, u32 err_code
)
2703 * Instruction with address size override prefix opcode 0x67
2704 * Cause the #SS fault with 0 error code in VM86 mode.
2706 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
2707 if (emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DONE
)
2710 * Forward all other exceptions that are valid in real mode.
2711 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2712 * the required debugging infrastructure rework.
2716 if (vcpu
->guest_debug
&
2717 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
2719 kvm_queue_exception(vcpu
, vec
);
2723 * Update instruction length as we may reinject the exception
2724 * from user space while in guest debugging mode.
2726 to_vmx(vcpu
)->vcpu
.arch
.event_exit_inst_len
=
2727 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
2728 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
2739 kvm_queue_exception(vcpu
, vec
);
2746 * Trigger machine check on the host. We assume all the MSRs are already set up
2747 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2748 * We pass a fake environment to the machine check handler because we want
2749 * the guest to be always treated like user space, no matter what context
2750 * it used internally.
2752 static void kvm_machine_check(void)
2754 #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2755 struct pt_regs regs
= {
2756 .cs
= 3, /* Fake ring 3 no matter what the guest ran on */
2757 .flags
= X86_EFLAGS_IF
,
2760 do_machine_check(®s
, 0);
2764 static int handle_machine_check(struct kvm_vcpu
*vcpu
)
2766 /* already handled by vcpu_run */
2770 static int handle_exception(struct kvm_vcpu
*vcpu
)
2772 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2773 struct kvm_run
*kvm_run
= vcpu
->run
;
2774 u32 intr_info
, ex_no
, error_code
;
2775 unsigned long cr2
, rip
, dr6
;
2777 enum emulation_result er
;
2779 vect_info
= vmx
->idt_vectoring_info
;
2780 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2782 if (is_machine_check(intr_info
))
2783 return handle_machine_check(vcpu
);
2785 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
2786 !is_page_fault(intr_info
)) {
2787 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
2788 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_SIMUL_EX
;
2789 vcpu
->run
->internal
.ndata
= 2;
2790 vcpu
->run
->internal
.data
[0] = vect_info
;
2791 vcpu
->run
->internal
.data
[1] = intr_info
;
2795 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
)
2796 return 1; /* already handled by vmx_vcpu_run() */
2798 if (is_no_device(intr_info
)) {
2799 vmx_fpu_activate(vcpu
);
2803 if (is_invalid_opcode(intr_info
)) {
2804 er
= emulate_instruction(vcpu
, 0, 0, EMULTYPE_TRAP_UD
);
2805 if (er
!= EMULATE_DONE
)
2806 kvm_queue_exception(vcpu
, UD_VECTOR
);
2811 rip
= kvm_rip_read(vcpu
);
2812 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2813 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2814 if (is_page_fault(intr_info
)) {
2815 /* EPT won't cause page fault directly */
2818 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2819 trace_kvm_page_fault(cr2
, error_code
);
2821 if (kvm_event_needs_reinjection(vcpu
))
2822 kvm_mmu_unprotect_page_virt(vcpu
, cr2
);
2823 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2826 if (vmx
->rmode
.vm86_active
&&
2827 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2829 if (vcpu
->arch
.halt_request
) {
2830 vcpu
->arch
.halt_request
= 0;
2831 return kvm_emulate_halt(vcpu
);
2836 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
2839 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
2840 if (!(vcpu
->guest_debug
&
2841 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
2842 vcpu
->arch
.dr6
= dr6
| DR6_FIXED_1
;
2843 kvm_queue_exception(vcpu
, DB_VECTOR
);
2846 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
2847 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
2851 * Update instruction length as we may reinject #BP from
2852 * user space while in guest debugging mode. Reading it for
2853 * #DB as well causes no harm, it is not used in that case.
2855 vmx
->vcpu
.arch
.event_exit_inst_len
=
2856 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
2857 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2858 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
2859 kvm_run
->debug
.arch
.exception
= ex_no
;
2862 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2863 kvm_run
->ex
.exception
= ex_no
;
2864 kvm_run
->ex
.error_code
= error_code
;
2870 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
)
2872 ++vcpu
->stat
.irq_exits
;
2876 static int handle_triple_fault(struct kvm_vcpu
*vcpu
)
2878 vcpu
->run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2882 static int handle_io(struct kvm_vcpu
*vcpu
)
2884 unsigned long exit_qualification
;
2885 int size
, in
, string
;
2888 ++vcpu
->stat
.io_exits
;
2889 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2890 string
= (exit_qualification
& 16) != 0;
2893 if (emulate_instruction(vcpu
, 0, 0, 0) == EMULATE_DO_MMIO
)
2898 size
= (exit_qualification
& 7) + 1;
2899 in
= (exit_qualification
& 8) != 0;
2900 port
= exit_qualification
>> 16;
2902 skip_emulated_instruction(vcpu
);
2903 return kvm_emulate_pio(vcpu
, in
, size
, port
);
2907 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2910 * Patch in the VMCALL instruction:
2912 hypercall
[0] = 0x0f;
2913 hypercall
[1] = 0x01;
2914 hypercall
[2] = 0xc1;
2917 static int handle_cr(struct kvm_vcpu
*vcpu
)
2919 unsigned long exit_qualification
, val
;
2923 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2924 cr
= exit_qualification
& 15;
2925 reg
= (exit_qualification
>> 8) & 15;
2926 switch ((exit_qualification
>> 4) & 3) {
2927 case 0: /* mov to cr */
2928 val
= kvm_register_read(vcpu
, reg
);
2929 trace_kvm_cr_write(cr
, val
);
2932 kvm_set_cr0(vcpu
, val
);
2933 skip_emulated_instruction(vcpu
);
2936 kvm_set_cr3(vcpu
, val
);
2937 skip_emulated_instruction(vcpu
);
2940 kvm_set_cr4(vcpu
, val
);
2941 skip_emulated_instruction(vcpu
);
2944 u8 cr8_prev
= kvm_get_cr8(vcpu
);
2945 u8 cr8
= kvm_register_read(vcpu
, reg
);
2946 kvm_set_cr8(vcpu
, cr8
);
2947 skip_emulated_instruction(vcpu
);
2948 if (irqchip_in_kernel(vcpu
->kvm
))
2950 if (cr8_prev
<= cr8
)
2952 vcpu
->run
->exit_reason
= KVM_EXIT_SET_TPR
;
2958 vmx_fpu_deactivate(vcpu
);
2959 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2960 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2961 vmx_fpu_activate(vcpu
);
2962 skip_emulated_instruction(vcpu
);
2964 case 1: /*mov from cr*/
2967 kvm_register_write(vcpu
, reg
, vcpu
->arch
.cr3
);
2968 trace_kvm_cr_read(cr
, vcpu
->arch
.cr3
);
2969 skip_emulated_instruction(vcpu
);
2972 val
= kvm_get_cr8(vcpu
);
2973 kvm_register_write(vcpu
, reg
, val
);
2974 trace_kvm_cr_read(cr
, val
);
2975 skip_emulated_instruction(vcpu
);
2980 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2982 skip_emulated_instruction(vcpu
);
2987 vcpu
->run
->exit_reason
= 0;
2988 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2989 (int)(exit_qualification
>> 4) & 3, cr
);
2993 static int handle_dr(struct kvm_vcpu
*vcpu
)
2995 unsigned long exit_qualification
;
2999 if (!kvm_require_cpl(vcpu
, 0))
3001 dr
= vmcs_readl(GUEST_DR7
);
3004 * As the vm-exit takes precedence over the debug trap, we
3005 * need to emulate the latter, either for the host or the
3006 * guest debugging itself.
3008 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
3009 vcpu
->run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
3010 vcpu
->run
->debug
.arch
.dr7
= dr
;
3011 vcpu
->run
->debug
.arch
.pc
=
3012 vmcs_readl(GUEST_CS_BASE
) +
3013 vmcs_readl(GUEST_RIP
);
3014 vcpu
->run
->debug
.arch
.exception
= DB_VECTOR
;
3015 vcpu
->run
->exit_reason
= KVM_EXIT_DEBUG
;
3018 vcpu
->arch
.dr7
&= ~DR7_GD
;
3019 vcpu
->arch
.dr6
|= DR6_BD
;
3020 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
3021 kvm_queue_exception(vcpu
, DB_VECTOR
);
3026 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3027 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
3028 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
3029 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
3032 val
= vcpu
->arch
.db
[dr
];
3035 val
= vcpu
->arch
.dr6
;
3038 val
= vcpu
->arch
.dr7
;
3043 kvm_register_write(vcpu
, reg
, val
);
3045 val
= vcpu
->arch
.regs
[reg
];
3048 vcpu
->arch
.db
[dr
] = val
;
3049 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
3050 vcpu
->arch
.eff_db
[dr
] = val
;
3053 if (vcpu
->arch
.cr4
& X86_CR4_DE
)
3054 kvm_queue_exception(vcpu
, UD_VECTOR
);
3057 if (val
& 0xffffffff00000000ULL
) {
3058 kvm_queue_exception(vcpu
, GP_VECTOR
);
3061 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
3064 if (val
& 0xffffffff00000000ULL
) {
3065 kvm_queue_exception(vcpu
, GP_VECTOR
);
3068 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
3069 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
3070 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
3071 vcpu
->arch
.switch_db_regs
=
3072 (val
& DR7_BP_EN_MASK
);
3077 skip_emulated_instruction(vcpu
);
3081 static int handle_cpuid(struct kvm_vcpu
*vcpu
)
3083 kvm_emulate_cpuid(vcpu
);
3087 static int handle_rdmsr(struct kvm_vcpu
*vcpu
)
3089 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3092 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
3093 kvm_inject_gp(vcpu
, 0);
3097 trace_kvm_msr_read(ecx
, data
);
3099 /* FIXME: handling of bits 32:63 of rax, rdx */
3100 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
3101 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
3102 skip_emulated_instruction(vcpu
);
3106 static int handle_wrmsr(struct kvm_vcpu
*vcpu
)
3108 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3109 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
3110 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
3112 trace_kvm_msr_write(ecx
, data
);
3114 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
3115 kvm_inject_gp(vcpu
, 0);
3119 skip_emulated_instruction(vcpu
);
3123 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
)
3128 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
)
3130 u32 cpu_based_vm_exec_control
;
3132 /* clear pending irq */
3133 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3134 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
3135 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3137 ++vcpu
->stat
.irq_window_exits
;
3140 * If the user space waits to inject interrupts, exit as soon as
3143 if (!irqchip_in_kernel(vcpu
->kvm
) &&
3144 vcpu
->run
->request_interrupt_window
&&
3145 !kvm_cpu_has_interrupt(vcpu
)) {
3146 vcpu
->run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
3152 static int handle_halt(struct kvm_vcpu
*vcpu
)
3154 skip_emulated_instruction(vcpu
);
3155 return kvm_emulate_halt(vcpu
);
3158 static int handle_vmcall(struct kvm_vcpu
*vcpu
)
3160 skip_emulated_instruction(vcpu
);
3161 kvm_emulate_hypercall(vcpu
);
3165 static int handle_vmx_insn(struct kvm_vcpu
*vcpu
)
3167 kvm_queue_exception(vcpu
, UD_VECTOR
);
3171 static int handle_invlpg(struct kvm_vcpu
*vcpu
)
3173 unsigned long exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3175 kvm_mmu_invlpg(vcpu
, exit_qualification
);
3176 skip_emulated_instruction(vcpu
);
3180 static int handle_wbinvd(struct kvm_vcpu
*vcpu
)
3182 skip_emulated_instruction(vcpu
);
3183 /* TODO: Add support for VT-d/pass-through device */
3187 static int handle_apic_access(struct kvm_vcpu
*vcpu
)
3189 unsigned long exit_qualification
;
3190 enum emulation_result er
;
3191 unsigned long offset
;
3193 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3194 offset
= exit_qualification
& 0xffful
;
3196 er
= emulate_instruction(vcpu
, 0, 0, 0);
3198 if (er
!= EMULATE_DONE
) {
3200 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3207 static int handle_task_switch(struct kvm_vcpu
*vcpu
)
3209 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3210 unsigned long exit_qualification
;
3212 int reason
, type
, idt_v
;
3214 idt_v
= (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
3215 type
= (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
);
3217 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3219 reason
= (u32
)exit_qualification
>> 30;
3220 if (reason
== TASK_SWITCH_GATE
&& idt_v
) {
3222 case INTR_TYPE_NMI_INTR
:
3223 vcpu
->arch
.nmi_injected
= false;
3224 if (cpu_has_virtual_nmis())
3225 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3226 GUEST_INTR_STATE_NMI
);
3228 case INTR_TYPE_EXT_INTR
:
3229 case INTR_TYPE_SOFT_INTR
:
3230 kvm_clear_interrupt_queue(vcpu
);
3232 case INTR_TYPE_HARD_EXCEPTION
:
3233 case INTR_TYPE_SOFT_EXCEPTION
:
3234 kvm_clear_exception_queue(vcpu
);
3240 tss_selector
= exit_qualification
;
3242 if (!idt_v
|| (type
!= INTR_TYPE_HARD_EXCEPTION
&&
3243 type
!= INTR_TYPE_EXT_INTR
&&
3244 type
!= INTR_TYPE_NMI_INTR
))
3245 skip_emulated_instruction(vcpu
);
3247 if (!kvm_task_switch(vcpu
, tss_selector
, reason
))
3250 /* clear all local breakpoint enable flags */
3251 vmcs_writel(GUEST_DR7
, vmcs_readl(GUEST_DR7
) & ~55);
3254 * TODO: What about debug traps on tss switch?
3255 * Are we supposed to inject them and update dr6?
3261 static int handle_ept_violation(struct kvm_vcpu
*vcpu
)
3263 unsigned long exit_qualification
;
3267 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3269 if (exit_qualification
& (1 << 6)) {
3270 printk(KERN_ERR
"EPT: GPA exceeds GAW!\n");
3274 gla_validity
= (exit_qualification
>> 7) & 0x3;
3275 if (gla_validity
!= 0x3 && gla_validity
!= 0x1 && gla_validity
!= 0) {
3276 printk(KERN_ERR
"EPT: Handling EPT violation failed!\n");
3277 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3278 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
3279 vmcs_readl(GUEST_LINEAR_ADDRESS
));
3280 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
3281 (long unsigned int)exit_qualification
);
3282 vcpu
->run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3283 vcpu
->run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_VIOLATION
;
3287 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3288 trace_kvm_page_fault(gpa
, exit_qualification
);
3289 return kvm_mmu_page_fault(vcpu
, gpa
& PAGE_MASK
, 0);
3292 static u64
ept_rsvd_mask(u64 spte
, int level
)
3297 for (i
= 51; i
> boot_cpu_data
.x86_phys_bits
; i
--)
3298 mask
|= (1ULL << i
);
3301 /* bits 7:3 reserved */
3303 else if (level
== 2) {
3304 if (spte
& (1ULL << 7))
3305 /* 2MB ref, bits 20:12 reserved */
3308 /* bits 6:3 reserved */
3315 static void ept_misconfig_inspect_spte(struct kvm_vcpu
*vcpu
, u64 spte
,
3318 printk(KERN_ERR
"%s: spte 0x%llx level %d\n", __func__
, spte
, level
);
3320 /* 010b (write-only) */
3321 WARN_ON((spte
& 0x7) == 0x2);
3323 /* 110b (write/execute) */
3324 WARN_ON((spte
& 0x7) == 0x6);
3326 /* 100b (execute-only) and value not supported by logical processor */
3327 if (!cpu_has_vmx_ept_execute_only())
3328 WARN_ON((spte
& 0x7) == 0x4);
3332 u64 rsvd_bits
= spte
& ept_rsvd_mask(spte
, level
);
3334 if (rsvd_bits
!= 0) {
3335 printk(KERN_ERR
"%s: rsvd_bits = 0x%llx\n",
3336 __func__
, rsvd_bits
);
3340 if (level
== 1 || (level
== 2 && (spte
& (1ULL << 7)))) {
3341 u64 ept_mem_type
= (spte
& 0x38) >> 3;
3343 if (ept_mem_type
== 2 || ept_mem_type
== 3 ||
3344 ept_mem_type
== 7) {
3345 printk(KERN_ERR
"%s: ept_mem_type=0x%llx\n",
3346 __func__
, ept_mem_type
);
3353 static int handle_ept_misconfig(struct kvm_vcpu
*vcpu
)
3359 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3361 printk(KERN_ERR
"EPT: Misconfiguration.\n");
3362 printk(KERN_ERR
"EPT: GPA: 0x%llx\n", gpa
);
3364 nr_sptes
= kvm_mmu_get_spte_hierarchy(vcpu
, gpa
, sptes
);
3366 for (i
= PT64_ROOT_LEVEL
; i
> PT64_ROOT_LEVEL
- nr_sptes
; --i
)
3367 ept_misconfig_inspect_spte(vcpu
, sptes
[i
-1], i
);
3369 vcpu
->run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3370 vcpu
->run
->hw
.hardware_exit_reason
= EXIT_REASON_EPT_MISCONFIG
;
3375 static int handle_nmi_window(struct kvm_vcpu
*vcpu
)
3377 u32 cpu_based_vm_exec_control
;
3379 /* clear pending NMI */
3380 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3381 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
3382 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3383 ++vcpu
->stat
.nmi_window_exits
;
3388 static int handle_invalid_guest_state(struct kvm_vcpu
*vcpu
)
3390 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3391 enum emulation_result err
= EMULATE_DONE
;
3394 while (!guest_state_valid(vcpu
)) {
3395 err
= emulate_instruction(vcpu
, 0, 0, 0);
3397 if (err
== EMULATE_DO_MMIO
) {
3402 if (err
!= EMULATE_DONE
) {
3403 kvm_report_emulation_failure(vcpu
, "emulation failure");
3404 vcpu
->run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
3405 vcpu
->run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
3406 vcpu
->run
->internal
.ndata
= 0;
3411 if (signal_pending(current
))
3417 vmx
->emulation_required
= 0;
3423 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3424 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3426 static int handle_pause(struct kvm_vcpu
*vcpu
)
3428 skip_emulated_instruction(vcpu
);
3429 kvm_vcpu_on_spin(vcpu
);
3434 static int handle_invalid_op(struct kvm_vcpu
*vcpu
)
3436 kvm_queue_exception(vcpu
, UD_VECTOR
);
3441 * The exit handlers return 1 if the exit was handled fully and guest execution
3442 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3443 * to be done to userspace and return 0.
3445 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
) = {
3446 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
3447 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
3448 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
3449 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
3450 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
3451 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
3452 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
3453 [EXIT_REASON_CPUID
] = handle_cpuid
,
3454 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
3455 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
3456 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
3457 [EXIT_REASON_HLT
] = handle_halt
,
3458 [EXIT_REASON_INVLPG
] = handle_invlpg
,
3459 [EXIT_REASON_VMCALL
] = handle_vmcall
,
3460 [EXIT_REASON_VMCLEAR
] = handle_vmx_insn
,
3461 [EXIT_REASON_VMLAUNCH
] = handle_vmx_insn
,
3462 [EXIT_REASON_VMPTRLD
] = handle_vmx_insn
,
3463 [EXIT_REASON_VMPTRST
] = handle_vmx_insn
,
3464 [EXIT_REASON_VMREAD
] = handle_vmx_insn
,
3465 [EXIT_REASON_VMRESUME
] = handle_vmx_insn
,
3466 [EXIT_REASON_VMWRITE
] = handle_vmx_insn
,
3467 [EXIT_REASON_VMOFF
] = handle_vmx_insn
,
3468 [EXIT_REASON_VMON
] = handle_vmx_insn
,
3469 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
3470 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
3471 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
3472 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
3473 [EXIT_REASON_MCE_DURING_VMENTRY
] = handle_machine_check
,
3474 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
3475 [EXIT_REASON_EPT_MISCONFIG
] = handle_ept_misconfig
,
3476 [EXIT_REASON_PAUSE_INSTRUCTION
] = handle_pause
,
3477 [EXIT_REASON_MWAIT_INSTRUCTION
] = handle_invalid_op
,
3478 [EXIT_REASON_MONITOR_INSTRUCTION
] = handle_invalid_op
,
3481 static const int kvm_vmx_max_exit_handlers
=
3482 ARRAY_SIZE(kvm_vmx_exit_handlers
);
3485 * The guest has exited. See if we can fix it or if we need userspace
3488 static int vmx_handle_exit(struct kvm_vcpu
*vcpu
)
3490 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3491 u32 exit_reason
= vmx
->exit_reason
;
3492 u32 vectoring_info
= vmx
->idt_vectoring_info
;
3494 trace_kvm_exit(exit_reason
, kvm_rip_read(vcpu
));
3496 /* If guest state is invalid, start emulating */
3497 if (vmx
->emulation_required
&& emulate_invalid_guest_state
)
3498 return handle_invalid_guest_state(vcpu
);
3500 /* Access CR3 don't cause VMExit in paging mode, so we need
3501 * to sync with guest real CR3. */
3502 if (enable_ept
&& is_paging(vcpu
))
3503 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
3505 if (unlikely(vmx
->fail
)) {
3506 vcpu
->run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3507 vcpu
->run
->fail_entry
.hardware_entry_failure_reason
3508 = vmcs_read32(VM_INSTRUCTION_ERROR
);
3512 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3513 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
3514 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
3515 exit_reason
!= EXIT_REASON_TASK_SWITCH
))
3516 printk(KERN_WARNING
"%s: unexpected, valid vectoring info "
3517 "(0x%x) and exit reason is 0x%x\n",
3518 __func__
, vectoring_info
, exit_reason
);
3520 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
)) {
3521 if (vmx_interrupt_allowed(vcpu
)) {
3522 vmx
->soft_vnmi_blocked
= 0;
3523 } else if (vmx
->vnmi_blocked_time
> 1000000000LL &&
3524 vcpu
->arch
.nmi_pending
) {
3526 * This CPU don't support us in finding the end of an
3527 * NMI-blocked window if the guest runs with IRQs
3528 * disabled. So we pull the trigger after 1 s of
3529 * futile waiting, but inform the user about this.
3531 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
3532 "state on VCPU %d after 1 s timeout\n",
3533 __func__
, vcpu
->vcpu_id
);
3534 vmx
->soft_vnmi_blocked
= 0;
3538 if (exit_reason
< kvm_vmx_max_exit_handlers
3539 && kvm_vmx_exit_handlers
[exit_reason
])
3540 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
);
3542 vcpu
->run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3543 vcpu
->run
->hw
.hardware_exit_reason
= exit_reason
;
3548 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
, int tpr
, int irr
)
3550 if (irr
== -1 || tpr
< irr
) {
3551 vmcs_write32(TPR_THRESHOLD
, 0);
3555 vmcs_write32(TPR_THRESHOLD
, irr
);
3558 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
3561 u32 idt_vectoring_info
= vmx
->idt_vectoring_info
;
3565 bool idtv_info_valid
;
3567 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3569 vmx
->exit_reason
= vmcs_read32(VM_EXIT_REASON
);
3571 /* Handle machine checks before interrupts are enabled */
3572 if ((vmx
->exit_reason
== EXIT_REASON_MCE_DURING_VMENTRY
)
3573 || (vmx
->exit_reason
== EXIT_REASON_EXCEPTION_NMI
3574 && is_machine_check(exit_intr_info
)))
3575 kvm_machine_check();
3577 /* We need to handle NMIs before interrupts are enabled */
3578 if ((exit_intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
&&
3579 (exit_intr_info
& INTR_INFO_VALID_MASK
))
3582 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
3584 if (cpu_has_virtual_nmis()) {
3585 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
3586 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
3588 * SDM 3: 27.7.1.2 (September 2008)
3589 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3590 * a guest IRET fault.
3591 * SDM 3: 23.2.2 (September 2008)
3592 * Bit 12 is undefined in any of the following cases:
3593 * If the VM exit sets the valid bit in the IDT-vectoring
3594 * information field.
3595 * If the VM exit is due to a double fault.
3597 if ((exit_intr_info
& INTR_INFO_VALID_MASK
) && unblock_nmi
&&
3598 vector
!= DF_VECTOR
&& !idtv_info_valid
)
3599 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3600 GUEST_INTR_STATE_NMI
);
3601 } else if (unlikely(vmx
->soft_vnmi_blocked
))
3602 vmx
->vnmi_blocked_time
+=
3603 ktime_to_ns(ktime_sub(ktime_get(), vmx
->entry_time
));
3605 vmx
->vcpu
.arch
.nmi_injected
= false;
3606 kvm_clear_exception_queue(&vmx
->vcpu
);
3607 kvm_clear_interrupt_queue(&vmx
->vcpu
);
3609 if (!idtv_info_valid
)
3612 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
3613 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
3616 case INTR_TYPE_NMI_INTR
:
3617 vmx
->vcpu
.arch
.nmi_injected
= true;
3619 * SDM 3: 27.7.1.2 (September 2008)
3620 * Clear bit "block by NMI" before VM entry if a NMI
3623 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3624 GUEST_INTR_STATE_NMI
);
3626 case INTR_TYPE_SOFT_EXCEPTION
:
3627 vmx
->vcpu
.arch
.event_exit_inst_len
=
3628 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
3630 case INTR_TYPE_HARD_EXCEPTION
:
3631 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
3632 u32 err
= vmcs_read32(IDT_VECTORING_ERROR_CODE
);
3633 kvm_queue_exception_e(&vmx
->vcpu
, vector
, err
);
3635 kvm_queue_exception(&vmx
->vcpu
, vector
);
3637 case INTR_TYPE_SOFT_INTR
:
3638 vmx
->vcpu
.arch
.event_exit_inst_len
=
3639 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
3641 case INTR_TYPE_EXT_INTR
:
3642 kvm_queue_interrupt(&vmx
->vcpu
, vector
,
3643 type
== INTR_TYPE_SOFT_INTR
);
3651 * Failure to inject an interrupt should give us the information
3652 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3653 * when fetching the interrupt redirection bitmap in the real-mode
3654 * tss, this doesn't happen. So we do it ourselves.
3656 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
3658 vmx
->rmode
.irq
.pending
= 0;
3659 if (kvm_rip_read(&vmx
->vcpu
) + 1 != vmx
->rmode
.irq
.rip
)
3661 kvm_rip_write(&vmx
->vcpu
, vmx
->rmode
.irq
.rip
);
3662 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
3663 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
3664 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
3667 vmx
->idt_vectoring_info
=
3668 VECTORING_INFO_VALID_MASK
3669 | INTR_TYPE_EXT_INTR
3670 | vmx
->rmode
.irq
.vector
;
3673 #ifdef CONFIG_X86_64
3681 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
)
3683 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3685 /* Record the guest's net vcpu time for enforced NMI injections. */
3686 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
))
3687 vmx
->entry_time
= ktime_get();
3689 /* Don't enter VMX if guest state is invalid, let the exit handler
3690 start emulation until we arrive back to a valid state */
3691 if (vmx
->emulation_required
&& emulate_invalid_guest_state
)
3694 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3695 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
3696 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3697 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
3699 /* When single-stepping over STI and MOV SS, we must clear the
3700 * corresponding interruptibility bits in the guest state. Otherwise
3701 * vmentry fails as it then expects bit 14 (BS) in pending debug
3702 * exceptions being set, but that's not correct for the guest debugging
3704 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3705 vmx_set_interrupt_shadow(vcpu
, 0);
3708 * Loading guest fpu may have cleared host cr0.ts
3710 vmcs_writel(HOST_CR0
, read_cr0());
3712 if (vcpu
->arch
.switch_db_regs
)
3713 set_debugreg(vcpu
->arch
.dr6
, 6);
3716 /* Store host registers */
3717 "push %%"R
"dx; push %%"R
"bp;"
3719 "cmp %%"R
"sp, %c[host_rsp](%0) \n\t"
3721 "mov %%"R
"sp, %c[host_rsp](%0) \n\t"
3722 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
3724 /* Reload cr2 if changed */
3725 "mov %c[cr2](%0), %%"R
"ax \n\t"
3726 "mov %%cr2, %%"R
"dx \n\t"
3727 "cmp %%"R
"ax, %%"R
"dx \n\t"
3729 "mov %%"R
"ax, %%cr2 \n\t"
3731 /* Check if vmlaunch of vmresume is needed */
3732 "cmpl $0, %c[launched](%0) \n\t"
3733 /* Load guest registers. Don't clobber flags. */
3734 "mov %c[rax](%0), %%"R
"ax \n\t"
3735 "mov %c[rbx](%0), %%"R
"bx \n\t"
3736 "mov %c[rdx](%0), %%"R
"dx \n\t"
3737 "mov %c[rsi](%0), %%"R
"si \n\t"
3738 "mov %c[rdi](%0), %%"R
"di \n\t"
3739 "mov %c[rbp](%0), %%"R
"bp \n\t"
3740 #ifdef CONFIG_X86_64
3741 "mov %c[r8](%0), %%r8 \n\t"
3742 "mov %c[r9](%0), %%r9 \n\t"
3743 "mov %c[r10](%0), %%r10 \n\t"
3744 "mov %c[r11](%0), %%r11 \n\t"
3745 "mov %c[r12](%0), %%r12 \n\t"
3746 "mov %c[r13](%0), %%r13 \n\t"
3747 "mov %c[r14](%0), %%r14 \n\t"
3748 "mov %c[r15](%0), %%r15 \n\t"
3750 "mov %c[rcx](%0), %%"R
"cx \n\t" /* kills %0 (ecx) */
3752 /* Enter guest mode */
3753 "jne .Llaunched \n\t"
3754 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
3755 "jmp .Lkvm_vmx_return \n\t"
3756 ".Llaunched: " __ex(ASM_VMX_VMRESUME
) "\n\t"
3757 ".Lkvm_vmx_return: "
3758 /* Save guest registers, load host registers, keep flags */
3759 "xchg %0, (%%"R
"sp) \n\t"
3760 "mov %%"R
"ax, %c[rax](%0) \n\t"
3761 "mov %%"R
"bx, %c[rbx](%0) \n\t"
3762 "push"Q
" (%%"R
"sp); pop"Q
" %c[rcx](%0) \n\t"
3763 "mov %%"R
"dx, %c[rdx](%0) \n\t"
3764 "mov %%"R
"si, %c[rsi](%0) \n\t"
3765 "mov %%"R
"di, %c[rdi](%0) \n\t"
3766 "mov %%"R
"bp, %c[rbp](%0) \n\t"
3767 #ifdef CONFIG_X86_64
3768 "mov %%r8, %c[r8](%0) \n\t"
3769 "mov %%r9, %c[r9](%0) \n\t"
3770 "mov %%r10, %c[r10](%0) \n\t"
3771 "mov %%r11, %c[r11](%0) \n\t"
3772 "mov %%r12, %c[r12](%0) \n\t"
3773 "mov %%r13, %c[r13](%0) \n\t"
3774 "mov %%r14, %c[r14](%0) \n\t"
3775 "mov %%r15, %c[r15](%0) \n\t"
3777 "mov %%cr2, %%"R
"ax \n\t"
3778 "mov %%"R
"ax, %c[cr2](%0) \n\t"
3780 "pop %%"R
"bp; pop %%"R
"bp; pop %%"R
"dx \n\t"
3781 "setbe %c[fail](%0) \n\t"
3782 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
3783 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
3784 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
3785 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
3786 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
3787 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3788 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3789 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3790 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3791 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3792 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
3793 #ifdef CONFIG_X86_64
3794 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3795 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3796 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3797 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3798 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3799 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3800 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3801 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
3803 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
3805 , R
"bx", R
"di", R
"si"
3806 #ifdef CONFIG_X86_64
3807 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3811 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
)
3812 | (1 << VCPU_EXREG_PDPTR
));
3813 vcpu
->arch
.regs_dirty
= 0;
3815 if (vcpu
->arch
.switch_db_regs
)
3816 get_debugreg(vcpu
->arch
.dr6
, 6);
3818 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
3819 if (vmx
->rmode
.irq
.pending
)
3820 fixup_rmode_irq(vmx
);
3822 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
3825 vmx_complete_interrupts(vmx
);
3831 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
3833 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3837 free_vmcs(vmx
->vmcs
);
3842 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
3844 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3846 spin_lock(&vmx_vpid_lock
);
3848 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
3849 spin_unlock(&vmx_vpid_lock
);
3850 vmx_free_vmcs(vcpu
);
3851 kfree(vmx
->guest_msrs
);
3852 kvm_vcpu_uninit(vcpu
);
3853 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3856 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
3859 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
3863 return ERR_PTR(-ENOMEM
);
3867 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
3871 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3872 if (!vmx
->guest_msrs
) {
3877 vmx
->vmcs
= alloc_vmcs();
3881 vmcs_clear(vmx
->vmcs
);
3884 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
3885 err
= vmx_vcpu_setup(vmx
);
3886 vmx_vcpu_put(&vmx
->vcpu
);
3890 if (vm_need_virtualize_apic_accesses(kvm
))
3891 if (alloc_apic_access_page(kvm
) != 0)
3895 if (!kvm
->arch
.ept_identity_map_addr
)
3896 kvm
->arch
.ept_identity_map_addr
=
3897 VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
3898 if (alloc_identity_pagetable(kvm
) != 0)
3905 free_vmcs(vmx
->vmcs
);
3907 kfree(vmx
->guest_msrs
);
3909 kvm_vcpu_uninit(&vmx
->vcpu
);
3911 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3912 return ERR_PTR(err
);
3915 static void __init
vmx_check_processor_compat(void *rtn
)
3917 struct vmcs_config vmcs_conf
;
3920 if (setup_vmcs_config(&vmcs_conf
) < 0)
3922 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
3923 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
3924 smp_processor_id());
3929 static int get_ept_level(void)
3931 return VMX_EPT_DEFAULT_GAW
+ 1;
3934 static u64
vmx_get_mt_mask(struct kvm_vcpu
*vcpu
, gfn_t gfn
, bool is_mmio
)
3938 /* For VT-d and EPT combination
3939 * 1. MMIO: always map as UC
3941 * a. VT-d without snooping control feature: can't guarantee the
3942 * result, try to trust guest.
3943 * b. VT-d with snooping control feature: snooping control feature of
3944 * VT-d engine can guarantee the cache correctness. Just set it
3945 * to WB to keep consistent with host. So the same as item 3.
3946 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3947 * consistent with host MTRR
3950 ret
= MTRR_TYPE_UNCACHABLE
<< VMX_EPT_MT_EPTE_SHIFT
;
3951 else if (vcpu
->kvm
->arch
.iommu_domain
&&
3952 !(vcpu
->kvm
->arch
.iommu_flags
& KVM_IOMMU_CACHE_COHERENCY
))
3953 ret
= kvm_get_guest_memory_type(vcpu
, gfn
) <<
3954 VMX_EPT_MT_EPTE_SHIFT
;
3956 ret
= (MTRR_TYPE_WRBACK
<< VMX_EPT_MT_EPTE_SHIFT
)
3962 static const struct trace_print_flags vmx_exit_reasons_str
[] = {
3963 { EXIT_REASON_EXCEPTION_NMI
, "exception" },
3964 { EXIT_REASON_EXTERNAL_INTERRUPT
, "ext_irq" },
3965 { EXIT_REASON_TRIPLE_FAULT
, "triple_fault" },
3966 { EXIT_REASON_NMI_WINDOW
, "nmi_window" },
3967 { EXIT_REASON_IO_INSTRUCTION
, "io_instruction" },
3968 { EXIT_REASON_CR_ACCESS
, "cr_access" },
3969 { EXIT_REASON_DR_ACCESS
, "dr_access" },
3970 { EXIT_REASON_CPUID
, "cpuid" },
3971 { EXIT_REASON_MSR_READ
, "rdmsr" },
3972 { EXIT_REASON_MSR_WRITE
, "wrmsr" },
3973 { EXIT_REASON_PENDING_INTERRUPT
, "interrupt_window" },
3974 { EXIT_REASON_HLT
, "halt" },
3975 { EXIT_REASON_INVLPG
, "invlpg" },
3976 { EXIT_REASON_VMCALL
, "hypercall" },
3977 { EXIT_REASON_TPR_BELOW_THRESHOLD
, "tpr_below_thres" },
3978 { EXIT_REASON_APIC_ACCESS
, "apic_access" },
3979 { EXIT_REASON_WBINVD
, "wbinvd" },
3980 { EXIT_REASON_TASK_SWITCH
, "task_switch" },
3981 { EXIT_REASON_EPT_VIOLATION
, "ept_violation" },
3985 static bool vmx_gb_page_enable(void)
3990 static struct kvm_x86_ops vmx_x86_ops
= {
3991 .cpu_has_kvm_support
= cpu_has_kvm_support
,
3992 .disabled_by_bios
= vmx_disabled_by_bios
,
3993 .hardware_setup
= hardware_setup
,
3994 .hardware_unsetup
= hardware_unsetup
,
3995 .check_processor_compatibility
= vmx_check_processor_compat
,
3996 .hardware_enable
= hardware_enable
,
3997 .hardware_disable
= hardware_disable
,
3998 .cpu_has_accelerated_tpr
= report_flexpriority
,
4000 .vcpu_create
= vmx_create_vcpu
,
4001 .vcpu_free
= vmx_free_vcpu
,
4002 .vcpu_reset
= vmx_vcpu_reset
,
4004 .prepare_guest_switch
= vmx_save_host_state
,
4005 .vcpu_load
= vmx_vcpu_load
,
4006 .vcpu_put
= vmx_vcpu_put
,
4008 .set_guest_debug
= set_guest_debug
,
4009 .get_msr
= vmx_get_msr
,
4010 .set_msr
= vmx_set_msr
,
4011 .get_segment_base
= vmx_get_segment_base
,
4012 .get_segment
= vmx_get_segment
,
4013 .set_segment
= vmx_set_segment
,
4014 .get_cpl
= vmx_get_cpl
,
4015 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
4016 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
4017 .set_cr0
= vmx_set_cr0
,
4018 .set_cr3
= vmx_set_cr3
,
4019 .set_cr4
= vmx_set_cr4
,
4020 .set_efer
= vmx_set_efer
,
4021 .get_idt
= vmx_get_idt
,
4022 .set_idt
= vmx_set_idt
,
4023 .get_gdt
= vmx_get_gdt
,
4024 .set_gdt
= vmx_set_gdt
,
4025 .cache_reg
= vmx_cache_reg
,
4026 .get_rflags
= vmx_get_rflags
,
4027 .set_rflags
= vmx_set_rflags
,
4029 .tlb_flush
= vmx_flush_tlb
,
4031 .run
= vmx_vcpu_run
,
4032 .handle_exit
= vmx_handle_exit
,
4033 .skip_emulated_instruction
= skip_emulated_instruction
,
4034 .set_interrupt_shadow
= vmx_set_interrupt_shadow
,
4035 .get_interrupt_shadow
= vmx_get_interrupt_shadow
,
4036 .patch_hypercall
= vmx_patch_hypercall
,
4037 .set_irq
= vmx_inject_irq
,
4038 .set_nmi
= vmx_inject_nmi
,
4039 .queue_exception
= vmx_queue_exception
,
4040 .interrupt_allowed
= vmx_interrupt_allowed
,
4041 .nmi_allowed
= vmx_nmi_allowed
,
4042 .get_nmi_mask
= vmx_get_nmi_mask
,
4043 .set_nmi_mask
= vmx_set_nmi_mask
,
4044 .enable_nmi_window
= enable_nmi_window
,
4045 .enable_irq_window
= enable_irq_window
,
4046 .update_cr8_intercept
= update_cr8_intercept
,
4048 .set_tss_addr
= vmx_set_tss_addr
,
4049 .get_tdp_level
= get_ept_level
,
4050 .get_mt_mask
= vmx_get_mt_mask
,
4052 .exit_reasons_str
= vmx_exit_reasons_str
,
4053 .gb_page_enable
= vmx_gb_page_enable
,
4056 static int __init
vmx_init(void)
4060 rdmsrl_safe(MSR_EFER
, &host_efer
);
4062 for (i
= 0; i
< NR_VMX_MSR
; ++i
)
4063 kvm_define_shared_msr(i
, vmx_msr_index
[i
]);
4065 vmx_io_bitmap_a
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4066 if (!vmx_io_bitmap_a
)
4069 vmx_io_bitmap_b
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4070 if (!vmx_io_bitmap_b
) {
4075 vmx_msr_bitmap_legacy
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4076 if (!vmx_msr_bitmap_legacy
) {
4081 vmx_msr_bitmap_longmode
= (unsigned long *)__get_free_page(GFP_KERNEL
);
4082 if (!vmx_msr_bitmap_longmode
) {
4088 * Allow direct access to the PC debug port (it is often used for I/O
4089 * delays, but the vmexits simply slow things down).
4091 memset(vmx_io_bitmap_a
, 0xff, PAGE_SIZE
);
4092 clear_bit(0x80, vmx_io_bitmap_a
);
4094 memset(vmx_io_bitmap_b
, 0xff, PAGE_SIZE
);
4096 memset(vmx_msr_bitmap_legacy
, 0xff, PAGE_SIZE
);
4097 memset(vmx_msr_bitmap_longmode
, 0xff, PAGE_SIZE
);
4099 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
4101 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
4105 vmx_disable_intercept_for_msr(MSR_FS_BASE
, false);
4106 vmx_disable_intercept_for_msr(MSR_GS_BASE
, false);
4107 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE
, true);
4108 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS
, false);
4109 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP
, false);
4110 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP
, false);
4113 bypass_guest_pf
= 0;
4114 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK
|
4115 VMX_EPT_WRITABLE_MASK
);
4116 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
4117 VMX_EPT_EXECUTABLE_MASK
);
4122 if (bypass_guest_pf
)
4123 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
4128 free_page((unsigned long)vmx_msr_bitmap_longmode
);
4130 free_page((unsigned long)vmx_msr_bitmap_legacy
);
4132 free_page((unsigned long)vmx_io_bitmap_b
);
4134 free_page((unsigned long)vmx_io_bitmap_a
);
4138 static void __exit
vmx_exit(void)
4140 free_page((unsigned long)vmx_msr_bitmap_legacy
);
4141 free_page((unsigned long)vmx_msr_bitmap_longmode
);
4142 free_page((unsigned long)vmx_io_bitmap_b
);
4143 free_page((unsigned long)vmx_io_bitmap_a
);
4148 module_init(vmx_init
)
4149 module_exit(vmx_exit
)