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 "kvm_cache_regs.h"
34 #include <asm/virtext.h>
36 #define __ex(x) __kvm_handle_fault_on_reboot(x)
38 MODULE_AUTHOR("Qumranet");
39 MODULE_LICENSE("GPL");
41 static int bypass_guest_pf
= 1;
42 module_param(bypass_guest_pf
, bool, 0);
44 static int enable_vpid
= 1;
45 module_param(enable_vpid
, bool, 0);
47 static int flexpriority_enabled
= 1;
48 module_param(flexpriority_enabled
, bool, 0);
50 static int enable_ept
= 1;
51 module_param(enable_ept
, bool, 0);
53 static int emulate_invalid_guest_state
= 0;
54 module_param(emulate_invalid_guest_state
, bool, 0);
64 struct list_head local_vcpus_link
;
65 unsigned long host_rsp
;
68 u32 idt_vectoring_info
;
69 struct kvm_msr_entry
*guest_msrs
;
70 struct kvm_msr_entry
*host_msrs
;
75 int msr_offset_kernel_gs_base
;
80 u16 fs_sel
, gs_sel
, ldt_sel
;
81 int gs_ldt_reload_needed
;
83 int guest_efer_loaded
;
93 bool emulation_required
;
95 /* Support for vnmi-less CPUs */
96 int soft_vnmi_blocked
;
98 s64 vnmi_blocked_time
;
101 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
103 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
106 static int init_rmode(struct kvm
*kvm
);
107 static u64
construct_eptp(unsigned long root_hpa
);
109 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
110 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
111 static DEFINE_PER_CPU(struct list_head
, vcpus_on_cpu
);
113 static struct page
*vmx_io_bitmap_a
;
114 static struct page
*vmx_io_bitmap_b
;
115 static struct page
*vmx_msr_bitmap
;
117 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
118 static DEFINE_SPINLOCK(vmx_vpid_lock
);
120 static struct vmcs_config
{
124 u32 pin_based_exec_ctrl
;
125 u32 cpu_based_exec_ctrl
;
126 u32 cpu_based_2nd_exec_ctrl
;
131 static struct vmx_capability
{
136 #define VMX_SEGMENT_FIELD(seg) \
137 [VCPU_SREG_##seg] = { \
138 .selector = GUEST_##seg##_SELECTOR, \
139 .base = GUEST_##seg##_BASE, \
140 .limit = GUEST_##seg##_LIMIT, \
141 .ar_bytes = GUEST_##seg##_AR_BYTES, \
144 static struct kvm_vmx_segment_field
{
149 } kvm_vmx_segment_fields
[] = {
150 VMX_SEGMENT_FIELD(CS
),
151 VMX_SEGMENT_FIELD(DS
),
152 VMX_SEGMENT_FIELD(ES
),
153 VMX_SEGMENT_FIELD(FS
),
154 VMX_SEGMENT_FIELD(GS
),
155 VMX_SEGMENT_FIELD(SS
),
156 VMX_SEGMENT_FIELD(TR
),
157 VMX_SEGMENT_FIELD(LDTR
),
161 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
162 * away by decrementing the array size.
164 static const u32 vmx_msr_index
[] = {
166 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
168 MSR_EFER
, MSR_K6_STAR
,
170 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
172 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
176 for (i
= 0; i
< n
; ++i
)
177 wrmsrl(e
[i
].index
, e
[i
].data
);
180 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
184 for (i
= 0; i
< n
; ++i
)
185 rdmsrl(e
[i
].index
, e
[i
].data
);
188 static inline int is_page_fault(u32 intr_info
)
190 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
191 INTR_INFO_VALID_MASK
)) ==
192 (INTR_TYPE_HARD_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
195 static inline int is_no_device(u32 intr_info
)
197 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
198 INTR_INFO_VALID_MASK
)) ==
199 (INTR_TYPE_HARD_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
202 static inline int is_invalid_opcode(u32 intr_info
)
204 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
205 INTR_INFO_VALID_MASK
)) ==
206 (INTR_TYPE_HARD_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
209 static inline int is_external_interrupt(u32 intr_info
)
211 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
212 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
215 static inline int cpu_has_vmx_msr_bitmap(void)
217 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
);
220 static inline int cpu_has_vmx_tpr_shadow(void)
222 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
);
225 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
227 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
)));
230 static inline int cpu_has_secondary_exec_ctrls(void)
232 return (vmcs_config
.cpu_based_exec_ctrl
&
233 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
);
236 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
238 return flexpriority_enabled
239 && (vmcs_config
.cpu_based_2nd_exec_ctrl
&
240 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
243 static inline int cpu_has_vmx_invept_individual_addr(void)
245 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
));
248 static inline int cpu_has_vmx_invept_context(void)
250 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
));
253 static inline int cpu_has_vmx_invept_global(void)
255 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
));
258 static inline int cpu_has_vmx_ept(void)
260 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
261 SECONDARY_EXEC_ENABLE_EPT
);
264 static inline int vm_need_ept(void)
266 return (cpu_has_vmx_ept() && enable_ept
);
269 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
271 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
272 (irqchip_in_kernel(kvm
)));
275 static inline int cpu_has_vmx_vpid(void)
277 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
278 SECONDARY_EXEC_ENABLE_VPID
);
281 static inline int cpu_has_virtual_nmis(void)
283 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
286 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
290 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
291 if (vmx
->guest_msrs
[i
].index
== msr
)
296 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
302 } operand
= { vpid
, 0, gva
};
304 asm volatile (__ex(ASM_VMX_INVVPID
)
305 /* CF==1 or ZF==1 --> rc = -1 */
307 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
310 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
314 } operand
= {eptp
, gpa
};
316 asm volatile (__ex(ASM_VMX_INVEPT
)
317 /* CF==1 or ZF==1 --> rc = -1 */
318 "; ja 1f ; ud2 ; 1:\n"
319 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
322 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
326 i
= __find_msr_index(vmx
, msr
);
328 return &vmx
->guest_msrs
[i
];
332 static void vmcs_clear(struct vmcs
*vmcs
)
334 u64 phys_addr
= __pa(vmcs
);
337 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
338 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
341 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
345 static void __vcpu_clear(void *arg
)
347 struct vcpu_vmx
*vmx
= arg
;
348 int cpu
= raw_smp_processor_id();
350 if (vmx
->vcpu
.cpu
== cpu
)
351 vmcs_clear(vmx
->vmcs
);
352 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
353 per_cpu(current_vmcs
, cpu
) = NULL
;
354 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
355 list_del(&vmx
->local_vcpus_link
);
360 static void vcpu_clear(struct vcpu_vmx
*vmx
)
362 if (vmx
->vcpu
.cpu
== -1)
364 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 1);
367 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
372 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
375 static inline void ept_sync_global(void)
377 if (cpu_has_vmx_invept_global())
378 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
381 static inline void ept_sync_context(u64 eptp
)
384 if (cpu_has_vmx_invept_context())
385 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
391 static inline void ept_sync_individual_addr(u64 eptp
, gpa_t gpa
)
394 if (cpu_has_vmx_invept_individual_addr())
395 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR
,
398 ept_sync_context(eptp
);
402 static unsigned long vmcs_readl(unsigned long field
)
406 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX
)
407 : "=a"(value
) : "d"(field
) : "cc");
411 static u16
vmcs_read16(unsigned long field
)
413 return vmcs_readl(field
);
416 static u32
vmcs_read32(unsigned long field
)
418 return vmcs_readl(field
);
421 static u64
vmcs_read64(unsigned long field
)
424 return vmcs_readl(field
);
426 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
430 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
432 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
433 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
437 static void vmcs_writel(unsigned long field
, unsigned long value
)
441 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
442 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
444 vmwrite_error(field
, value
);
447 static void vmcs_write16(unsigned long field
, u16 value
)
449 vmcs_writel(field
, value
);
452 static void vmcs_write32(unsigned long field
, u32 value
)
454 vmcs_writel(field
, value
);
457 static void vmcs_write64(unsigned long field
, u64 value
)
459 vmcs_writel(field
, value
);
460 #ifndef CONFIG_X86_64
462 vmcs_writel(field
+1, value
>> 32);
466 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
468 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
471 static void vmcs_set_bits(unsigned long field
, u32 mask
)
473 vmcs_writel(field
, vmcs_readl(field
) | mask
);
476 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
480 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
);
481 if (!vcpu
->fpu_active
)
482 eb
|= 1u << NM_VECTOR
;
483 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
484 if (vcpu
->guest_debug
&
485 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
486 eb
|= 1u << DB_VECTOR
;
487 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
488 eb
|= 1u << BP_VECTOR
;
490 if (vcpu
->arch
.rmode
.active
)
493 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
494 vmcs_write32(EXCEPTION_BITMAP
, eb
);
497 static void reload_tss(void)
500 * VT restores TR but not its size. Useless.
502 struct descriptor_table gdt
;
503 struct desc_struct
*descs
;
506 descs
= (void *)gdt
.base
;
507 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
511 static void load_transition_efer(struct vcpu_vmx
*vmx
)
513 int efer_offset
= vmx
->msr_offset_efer
;
514 u64 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
515 u64 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
521 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
524 ignore_bits
= EFER_NX
| EFER_SCE
;
526 ignore_bits
|= EFER_LMA
| EFER_LME
;
527 /* SCE is meaningful only in long mode on Intel */
528 if (guest_efer
& EFER_LMA
)
529 ignore_bits
&= ~(u64
)EFER_SCE
;
531 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
534 vmx
->host_state
.guest_efer_loaded
= 1;
535 guest_efer
&= ~ignore_bits
;
536 guest_efer
|= host_efer
& ignore_bits
;
537 wrmsrl(MSR_EFER
, guest_efer
);
538 vmx
->vcpu
.stat
.efer_reload
++;
541 static void reload_host_efer(struct vcpu_vmx
*vmx
)
543 if (vmx
->host_state
.guest_efer_loaded
) {
544 vmx
->host_state
.guest_efer_loaded
= 0;
545 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
549 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
551 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
553 if (vmx
->host_state
.loaded
)
556 vmx
->host_state
.loaded
= 1;
558 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
559 * allow segment selectors with cpl > 0 or ti == 1.
561 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
562 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
563 vmx
->host_state
.fs_sel
= kvm_read_fs();
564 if (!(vmx
->host_state
.fs_sel
& 7)) {
565 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
566 vmx
->host_state
.fs_reload_needed
= 0;
568 vmcs_write16(HOST_FS_SELECTOR
, 0);
569 vmx
->host_state
.fs_reload_needed
= 1;
571 vmx
->host_state
.gs_sel
= kvm_read_gs();
572 if (!(vmx
->host_state
.gs_sel
& 7))
573 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
575 vmcs_write16(HOST_GS_SELECTOR
, 0);
576 vmx
->host_state
.gs_ldt_reload_needed
= 1;
580 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
581 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
583 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
584 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
588 if (is_long_mode(&vmx
->vcpu
))
589 save_msrs(vmx
->host_msrs
+
590 vmx
->msr_offset_kernel_gs_base
, 1);
593 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
594 load_transition_efer(vmx
);
597 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
601 if (!vmx
->host_state
.loaded
)
604 ++vmx
->vcpu
.stat
.host_state_reload
;
605 vmx
->host_state
.loaded
= 0;
606 if (vmx
->host_state
.fs_reload_needed
)
607 kvm_load_fs(vmx
->host_state
.fs_sel
);
608 if (vmx
->host_state
.gs_ldt_reload_needed
) {
609 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
611 * If we have to reload gs, we must take care to
612 * preserve our gs base.
614 local_irq_save(flags
);
615 kvm_load_gs(vmx
->host_state
.gs_sel
);
617 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
619 local_irq_restore(flags
);
622 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
623 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
624 reload_host_efer(vmx
);
627 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
630 __vmx_load_host_state(vmx
);
635 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
636 * vcpu mutex is already taken.
638 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
640 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
641 u64 phys_addr
= __pa(vmx
->vmcs
);
642 u64 tsc_this
, delta
, new_offset
;
644 if (vcpu
->cpu
!= cpu
) {
646 kvm_migrate_timers(vcpu
);
647 vpid_sync_vcpu_all(vmx
);
649 list_add(&vmx
->local_vcpus_link
,
650 &per_cpu(vcpus_on_cpu
, cpu
));
654 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
657 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
658 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
659 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
662 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
663 vmx
->vmcs
, phys_addr
);
666 if (vcpu
->cpu
!= cpu
) {
667 struct descriptor_table dt
;
668 unsigned long sysenter_esp
;
672 * Linux uses per-cpu TSS and GDT, so set these when switching
675 vmcs_writel(HOST_TR_BASE
, kvm_read_tr_base()); /* 22.2.4 */
677 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
679 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
680 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
683 * Make sure the time stamp counter is monotonous.
686 if (tsc_this
< vcpu
->arch
.host_tsc
) {
687 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
688 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
689 vmcs_write64(TSC_OFFSET
, new_offset
);
694 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
696 __vmx_load_host_state(to_vmx(vcpu
));
699 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
701 if (vcpu
->fpu_active
)
703 vcpu
->fpu_active
= 1;
704 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
705 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
706 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
707 update_exception_bitmap(vcpu
);
710 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
712 if (!vcpu
->fpu_active
)
714 vcpu
->fpu_active
= 0;
715 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
716 update_exception_bitmap(vcpu
);
719 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
721 return vmcs_readl(GUEST_RFLAGS
);
724 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
726 if (vcpu
->arch
.rmode
.active
)
727 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
728 vmcs_writel(GUEST_RFLAGS
, rflags
);
731 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
734 u32 interruptibility
;
736 rip
= kvm_rip_read(vcpu
);
737 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
738 kvm_rip_write(vcpu
, rip
);
741 * We emulated an instruction, so temporary interrupt blocking
742 * should be removed, if set.
744 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
745 if (interruptibility
& 3)
746 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
747 interruptibility
& ~3);
748 vcpu
->arch
.interrupt_window_open
= 1;
751 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
752 bool has_error_code
, u32 error_code
)
754 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
755 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
757 if (has_error_code
) {
758 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
759 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
762 if (vcpu
->arch
.rmode
.active
) {
763 vmx
->rmode
.irq
.pending
= true;
764 vmx
->rmode
.irq
.vector
= nr
;
765 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
766 if (nr
== BP_VECTOR
|| nr
== OF_VECTOR
)
767 vmx
->rmode
.irq
.rip
++;
768 intr_info
|= INTR_TYPE_SOFT_INTR
;
769 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
770 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
771 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
775 if (nr
== BP_VECTOR
|| nr
== OF_VECTOR
) {
776 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
777 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
779 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
781 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
784 static bool vmx_exception_injected(struct kvm_vcpu
*vcpu
)
790 * Swap MSR entry in host/guest MSR entry array.
793 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
795 struct kvm_msr_entry tmp
;
797 tmp
= vmx
->guest_msrs
[to
];
798 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
799 vmx
->guest_msrs
[from
] = tmp
;
800 tmp
= vmx
->host_msrs
[to
];
801 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
802 vmx
->host_msrs
[from
] = tmp
;
807 * Set up the vmcs to automatically save and restore system
808 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
809 * mode, as fiddling with msrs is very expensive.
811 static void setup_msrs(struct vcpu_vmx
*vmx
)
815 vmx_load_host_state(vmx
);
818 if (is_long_mode(&vmx
->vcpu
)) {
821 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
823 move_msr_up(vmx
, index
, save_nmsrs
++);
824 index
= __find_msr_index(vmx
, MSR_LSTAR
);
826 move_msr_up(vmx
, index
, save_nmsrs
++);
827 index
= __find_msr_index(vmx
, MSR_CSTAR
);
829 move_msr_up(vmx
, index
, save_nmsrs
++);
830 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
832 move_msr_up(vmx
, index
, save_nmsrs
++);
834 * MSR_K6_STAR is only needed on long mode guests, and only
835 * if efer.sce is enabled.
837 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
838 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
839 move_msr_up(vmx
, index
, save_nmsrs
++);
842 vmx
->save_nmsrs
= save_nmsrs
;
845 vmx
->msr_offset_kernel_gs_base
=
846 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
848 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
852 * reads and returns guest's timestamp counter "register"
853 * guest_tsc = host_tsc + tsc_offset -- 21.3
855 static u64
guest_read_tsc(void)
857 u64 host_tsc
, tsc_offset
;
860 tsc_offset
= vmcs_read64(TSC_OFFSET
);
861 return host_tsc
+ tsc_offset
;
865 * writes 'guest_tsc' into guest's timestamp counter "register"
866 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
868 static void guest_write_tsc(u64 guest_tsc
, u64 host_tsc
)
870 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
874 * Reads an msr value (of 'msr_index') into 'pdata'.
875 * Returns 0 on success, non-0 otherwise.
876 * Assumes vcpu_load() was already called.
878 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
881 struct kvm_msr_entry
*msr
;
884 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
891 data
= vmcs_readl(GUEST_FS_BASE
);
894 data
= vmcs_readl(GUEST_GS_BASE
);
897 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
899 case MSR_IA32_TIME_STAMP_COUNTER
:
900 data
= guest_read_tsc();
902 case MSR_IA32_SYSENTER_CS
:
903 data
= vmcs_read32(GUEST_SYSENTER_CS
);
905 case MSR_IA32_SYSENTER_EIP
:
906 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
908 case MSR_IA32_SYSENTER_ESP
:
909 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
912 vmx_load_host_state(to_vmx(vcpu
));
913 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
918 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
926 * Writes msr value into into the appropriate "register".
927 * Returns 0 on success, non-0 otherwise.
928 * Assumes vcpu_load() was already called.
930 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
932 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
933 struct kvm_msr_entry
*msr
;
940 vmx_load_host_state(vmx
);
941 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
944 vmcs_writel(GUEST_FS_BASE
, data
);
947 vmcs_writel(GUEST_GS_BASE
, data
);
950 case MSR_IA32_SYSENTER_CS
:
951 vmcs_write32(GUEST_SYSENTER_CS
, data
);
953 case MSR_IA32_SYSENTER_EIP
:
954 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
956 case MSR_IA32_SYSENTER_ESP
:
957 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
959 case MSR_IA32_TIME_STAMP_COUNTER
:
961 guest_write_tsc(data
, host_tsc
);
963 case MSR_P6_PERFCTR0
:
964 case MSR_P6_PERFCTR1
:
965 case MSR_P6_EVNTSEL0
:
966 case MSR_P6_EVNTSEL1
:
968 * Just discard all writes to the performance counters; this
969 * should keep both older linux and windows 64-bit guests
972 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index
, data
);
975 case MSR_IA32_CR_PAT
:
976 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
977 vmcs_write64(GUEST_IA32_PAT
, data
);
978 vcpu
->arch
.pat
= data
;
981 /* Otherwise falls through to kvm_set_msr_common */
983 vmx_load_host_state(vmx
);
984 msr
= find_msr_entry(vmx
, msr_index
);
989 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
995 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
997 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
1000 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
1003 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
1010 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1012 int old_debug
= vcpu
->guest_debug
;
1013 unsigned long flags
;
1015 vcpu
->guest_debug
= dbg
->control
;
1016 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
))
1017 vcpu
->guest_debug
= 0;
1019 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1020 vmcs_writel(GUEST_DR7
, dbg
->arch
.debugreg
[7]);
1022 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
1024 flags
= vmcs_readl(GUEST_RFLAGS
);
1025 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
1026 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
1027 else if (old_debug
& KVM_GUESTDBG_SINGLESTEP
)
1028 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1029 vmcs_writel(GUEST_RFLAGS
, flags
);
1031 update_exception_bitmap(vcpu
);
1036 static int vmx_get_irq(struct kvm_vcpu
*vcpu
)
1038 if (!vcpu
->arch
.interrupt
.pending
)
1040 return vcpu
->arch
.interrupt
.nr
;
1043 static __init
int cpu_has_kvm_support(void)
1045 return cpu_has_vmx();
1048 static __init
int vmx_disabled_by_bios(void)
1052 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
1053 return (msr
& (FEATURE_CONTROL_LOCKED
|
1054 FEATURE_CONTROL_VMXON_ENABLED
))
1055 == FEATURE_CONTROL_LOCKED
;
1056 /* locked but not enabled */
1059 static void hardware_enable(void *garbage
)
1061 int cpu
= raw_smp_processor_id();
1062 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
1065 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu
, cpu
));
1066 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
1067 if ((old
& (FEATURE_CONTROL_LOCKED
|
1068 FEATURE_CONTROL_VMXON_ENABLED
))
1069 != (FEATURE_CONTROL_LOCKED
|
1070 FEATURE_CONTROL_VMXON_ENABLED
))
1071 /* enable and lock */
1072 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
1073 FEATURE_CONTROL_LOCKED
|
1074 FEATURE_CONTROL_VMXON_ENABLED
);
1075 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
1076 asm volatile (ASM_VMX_VMXON_RAX
1077 : : "a"(&phys_addr
), "m"(phys_addr
)
1081 static void vmclear_local_vcpus(void)
1083 int cpu
= raw_smp_processor_id();
1084 struct vcpu_vmx
*vmx
, *n
;
1086 list_for_each_entry_safe(vmx
, n
, &per_cpu(vcpus_on_cpu
, cpu
),
1092 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1095 static void kvm_cpu_vmxoff(void)
1097 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
1098 write_cr4(read_cr4() & ~X86_CR4_VMXE
);
1101 static void hardware_disable(void *garbage
)
1103 vmclear_local_vcpus();
1107 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1108 u32 msr
, u32
*result
)
1110 u32 vmx_msr_low
, vmx_msr_high
;
1111 u32 ctl
= ctl_min
| ctl_opt
;
1113 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1115 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1116 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1118 /* Ensure minimum (required) set of control bits are supported. */
1126 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1128 u32 vmx_msr_low
, vmx_msr_high
;
1129 u32 min
, opt
, min2
, opt2
;
1130 u32 _pin_based_exec_control
= 0;
1131 u32 _cpu_based_exec_control
= 0;
1132 u32 _cpu_based_2nd_exec_control
= 0;
1133 u32 _vmexit_control
= 0;
1134 u32 _vmentry_control
= 0;
1136 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1137 opt
= PIN_BASED_VIRTUAL_NMIS
;
1138 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1139 &_pin_based_exec_control
) < 0)
1142 min
= CPU_BASED_HLT_EXITING
|
1143 #ifdef CONFIG_X86_64
1144 CPU_BASED_CR8_LOAD_EXITING
|
1145 CPU_BASED_CR8_STORE_EXITING
|
1147 CPU_BASED_CR3_LOAD_EXITING
|
1148 CPU_BASED_CR3_STORE_EXITING
|
1149 CPU_BASED_USE_IO_BITMAPS
|
1150 CPU_BASED_MOV_DR_EXITING
|
1151 CPU_BASED_USE_TSC_OFFSETING
|
1152 CPU_BASED_INVLPG_EXITING
;
1153 opt
= CPU_BASED_TPR_SHADOW
|
1154 CPU_BASED_USE_MSR_BITMAPS
|
1155 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1156 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1157 &_cpu_based_exec_control
) < 0)
1159 #ifdef CONFIG_X86_64
1160 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1161 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1162 ~CPU_BASED_CR8_STORE_EXITING
;
1164 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1166 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1167 SECONDARY_EXEC_WBINVD_EXITING
|
1168 SECONDARY_EXEC_ENABLE_VPID
|
1169 SECONDARY_EXEC_ENABLE_EPT
;
1170 if (adjust_vmx_controls(min2
, opt2
,
1171 MSR_IA32_VMX_PROCBASED_CTLS2
,
1172 &_cpu_based_2nd_exec_control
) < 0)
1175 #ifndef CONFIG_X86_64
1176 if (!(_cpu_based_2nd_exec_control
&
1177 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1178 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1180 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1181 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1183 min
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1184 CPU_BASED_CR3_STORE_EXITING
|
1185 CPU_BASED_INVLPG_EXITING
);
1186 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1187 &_cpu_based_exec_control
) < 0)
1189 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1190 vmx_capability
.ept
, vmx_capability
.vpid
);
1194 #ifdef CONFIG_X86_64
1195 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1197 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
;
1198 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1199 &_vmexit_control
) < 0)
1203 opt
= VM_ENTRY_LOAD_IA32_PAT
;
1204 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1205 &_vmentry_control
) < 0)
1208 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1210 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1211 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1214 #ifdef CONFIG_X86_64
1215 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1216 if (vmx_msr_high
& (1u<<16))
1220 /* Require Write-Back (WB) memory type for VMCS accesses. */
1221 if (((vmx_msr_high
>> 18) & 15) != 6)
1224 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1225 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1226 vmcs_conf
->revision_id
= vmx_msr_low
;
1228 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1229 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1230 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1231 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1232 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1237 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1239 int node
= cpu_to_node(cpu
);
1243 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1246 vmcs
= page_address(pages
);
1247 memset(vmcs
, 0, vmcs_config
.size
);
1248 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1252 static struct vmcs
*alloc_vmcs(void)
1254 return alloc_vmcs_cpu(raw_smp_processor_id());
1257 static void free_vmcs(struct vmcs
*vmcs
)
1259 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1262 static void free_kvm_area(void)
1266 for_each_online_cpu(cpu
)
1267 free_vmcs(per_cpu(vmxarea
, cpu
));
1270 static __init
int alloc_kvm_area(void)
1274 for_each_online_cpu(cpu
) {
1277 vmcs
= alloc_vmcs_cpu(cpu
);
1283 per_cpu(vmxarea
, cpu
) = vmcs
;
1288 static __init
int hardware_setup(void)
1290 if (setup_vmcs_config(&vmcs_config
) < 0)
1293 if (boot_cpu_has(X86_FEATURE_NX
))
1294 kvm_enable_efer_bits(EFER_NX
);
1296 return alloc_kvm_area();
1299 static __exit
void hardware_unsetup(void)
1304 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1306 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1308 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1309 vmcs_write16(sf
->selector
, save
->selector
);
1310 vmcs_writel(sf
->base
, save
->base
);
1311 vmcs_write32(sf
->limit
, save
->limit
);
1312 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1314 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1316 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1320 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1322 unsigned long flags
;
1323 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1325 vmx
->emulation_required
= 1;
1326 vcpu
->arch
.rmode
.active
= 0;
1328 vmcs_writel(GUEST_TR_BASE
, vcpu
->arch
.rmode
.tr
.base
);
1329 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->arch
.rmode
.tr
.limit
);
1330 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->arch
.rmode
.tr
.ar
);
1332 flags
= vmcs_readl(GUEST_RFLAGS
);
1333 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1334 flags
|= (vcpu
->arch
.rmode
.save_iopl
<< IOPL_SHIFT
);
1335 vmcs_writel(GUEST_RFLAGS
, flags
);
1337 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1338 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1340 update_exception_bitmap(vcpu
);
1342 if (emulate_invalid_guest_state
)
1345 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1346 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1347 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1348 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1350 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1351 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1353 vmcs_write16(GUEST_CS_SELECTOR
,
1354 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1355 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1358 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1360 if (!kvm
->arch
.tss_addr
) {
1361 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1362 kvm
->memslots
[0].npages
- 3;
1363 return base_gfn
<< PAGE_SHIFT
;
1365 return kvm
->arch
.tss_addr
;
1368 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1370 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1372 save
->selector
= vmcs_read16(sf
->selector
);
1373 save
->base
= vmcs_readl(sf
->base
);
1374 save
->limit
= vmcs_read32(sf
->limit
);
1375 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1376 vmcs_write16(sf
->selector
, save
->base
>> 4);
1377 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1378 vmcs_write32(sf
->limit
, 0xffff);
1379 vmcs_write32(sf
->ar_bytes
, 0xf3);
1382 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1384 unsigned long flags
;
1385 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1387 vmx
->emulation_required
= 1;
1388 vcpu
->arch
.rmode
.active
= 1;
1390 vcpu
->arch
.rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1391 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1393 vcpu
->arch
.rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1394 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1396 vcpu
->arch
.rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1397 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1399 flags
= vmcs_readl(GUEST_RFLAGS
);
1400 vcpu
->arch
.rmode
.save_iopl
1401 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1403 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1405 vmcs_writel(GUEST_RFLAGS
, flags
);
1406 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1407 update_exception_bitmap(vcpu
);
1409 if (emulate_invalid_guest_state
)
1410 goto continue_rmode
;
1412 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1413 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1414 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1416 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1417 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1418 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1419 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1420 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1422 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1423 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1424 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1425 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1428 kvm_mmu_reset_context(vcpu
);
1429 init_rmode(vcpu
->kvm
);
1432 #ifdef CONFIG_X86_64
1434 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1438 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1439 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1440 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1442 vmcs_write32(GUEST_TR_AR_BYTES
,
1443 (guest_tr_ar
& ~AR_TYPE_MASK
)
1444 | AR_TYPE_BUSY_64_TSS
);
1447 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1449 find_msr_entry(to_vmx(vcpu
), MSR_EFER
)->data
|= EFER_LMA
| EFER_LME
;
1450 vmcs_write32(VM_ENTRY_CONTROLS
,
1451 vmcs_read32(VM_ENTRY_CONTROLS
)
1452 | VM_ENTRY_IA32E_MODE
);
1455 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1457 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1459 vmcs_write32(VM_ENTRY_CONTROLS
,
1460 vmcs_read32(VM_ENTRY_CONTROLS
)
1461 & ~VM_ENTRY_IA32E_MODE
);
1466 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1468 vpid_sync_vcpu_all(to_vmx(vcpu
));
1470 ept_sync_context(construct_eptp(vcpu
->arch
.mmu
.root_hpa
));
1473 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1475 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1476 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1479 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
1481 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1482 if (!load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
1483 printk(KERN_ERR
"EPT: Fail to load pdptrs!\n");
1486 vmcs_write64(GUEST_PDPTR0
, vcpu
->arch
.pdptrs
[0]);
1487 vmcs_write64(GUEST_PDPTR1
, vcpu
->arch
.pdptrs
[1]);
1488 vmcs_write64(GUEST_PDPTR2
, vcpu
->arch
.pdptrs
[2]);
1489 vmcs_write64(GUEST_PDPTR3
, vcpu
->arch
.pdptrs
[3]);
1493 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
1495 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
1497 struct kvm_vcpu
*vcpu
)
1499 if (!(cr0
& X86_CR0_PG
)) {
1500 /* From paging/starting to nonpaging */
1501 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1502 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
1503 (CPU_BASED_CR3_LOAD_EXITING
|
1504 CPU_BASED_CR3_STORE_EXITING
));
1505 vcpu
->arch
.cr0
= cr0
;
1506 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1507 *hw_cr0
|= X86_CR0_PE
| X86_CR0_PG
;
1508 *hw_cr0
&= ~X86_CR0_WP
;
1509 } else if (!is_paging(vcpu
)) {
1510 /* From nonpaging to paging */
1511 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1512 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
1513 ~(CPU_BASED_CR3_LOAD_EXITING
|
1514 CPU_BASED_CR3_STORE_EXITING
));
1515 vcpu
->arch
.cr0
= cr0
;
1516 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1517 if (!(vcpu
->arch
.cr0
& X86_CR0_WP
))
1518 *hw_cr0
&= ~X86_CR0_WP
;
1522 static void ept_update_paging_mode_cr4(unsigned long *hw_cr4
,
1523 struct kvm_vcpu
*vcpu
)
1525 if (!is_paging(vcpu
)) {
1526 *hw_cr4
&= ~X86_CR4_PAE
;
1527 *hw_cr4
|= X86_CR4_PSE
;
1528 } else if (!(vcpu
->arch
.cr4
& X86_CR4_PAE
))
1529 *hw_cr4
&= ~X86_CR4_PAE
;
1532 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1534 unsigned long hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
) |
1535 KVM_VM_CR0_ALWAYS_ON
;
1537 vmx_fpu_deactivate(vcpu
);
1539 if (vcpu
->arch
.rmode
.active
&& (cr0
& X86_CR0_PE
))
1542 if (!vcpu
->arch
.rmode
.active
&& !(cr0
& X86_CR0_PE
))
1545 #ifdef CONFIG_X86_64
1546 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1547 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1549 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1555 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
1557 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1558 vmcs_writel(GUEST_CR0
, hw_cr0
);
1559 vcpu
->arch
.cr0
= cr0
;
1561 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1562 vmx_fpu_activate(vcpu
);
1565 static u64
construct_eptp(unsigned long root_hpa
)
1569 /* TODO write the value reading from MSR */
1570 eptp
= VMX_EPT_DEFAULT_MT
|
1571 VMX_EPT_DEFAULT_GAW
<< VMX_EPT_GAW_EPTP_SHIFT
;
1572 eptp
|= (root_hpa
& PAGE_MASK
);
1577 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1579 unsigned long guest_cr3
;
1583 if (vm_need_ept()) {
1584 eptp
= construct_eptp(cr3
);
1585 vmcs_write64(EPT_POINTER
, eptp
);
1586 ept_sync_context(eptp
);
1587 ept_load_pdptrs(vcpu
);
1588 guest_cr3
= is_paging(vcpu
) ? vcpu
->arch
.cr3
:
1589 VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
1592 vmx_flush_tlb(vcpu
);
1593 vmcs_writel(GUEST_CR3
, guest_cr3
);
1594 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1595 vmx_fpu_deactivate(vcpu
);
1598 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1600 unsigned long hw_cr4
= cr4
| (vcpu
->arch
.rmode
.active
?
1601 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
1603 vcpu
->arch
.cr4
= cr4
;
1605 ept_update_paging_mode_cr4(&hw_cr4
, vcpu
);
1607 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1608 vmcs_writel(GUEST_CR4
, hw_cr4
);
1611 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1613 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1614 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1616 vcpu
->arch
.shadow_efer
= efer
;
1619 if (efer
& EFER_LMA
) {
1620 vmcs_write32(VM_ENTRY_CONTROLS
,
1621 vmcs_read32(VM_ENTRY_CONTROLS
) |
1622 VM_ENTRY_IA32E_MODE
);
1626 vmcs_write32(VM_ENTRY_CONTROLS
,
1627 vmcs_read32(VM_ENTRY_CONTROLS
) &
1628 ~VM_ENTRY_IA32E_MODE
);
1630 msr
->data
= efer
& ~EFER_LME
;
1635 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1637 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1639 return vmcs_readl(sf
->base
);
1642 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1643 struct kvm_segment
*var
, int seg
)
1645 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1648 var
->base
= vmcs_readl(sf
->base
);
1649 var
->limit
= vmcs_read32(sf
->limit
);
1650 var
->selector
= vmcs_read16(sf
->selector
);
1651 ar
= vmcs_read32(sf
->ar_bytes
);
1652 if (ar
& AR_UNUSABLE_MASK
)
1654 var
->type
= ar
& 15;
1655 var
->s
= (ar
>> 4) & 1;
1656 var
->dpl
= (ar
>> 5) & 3;
1657 var
->present
= (ar
>> 7) & 1;
1658 var
->avl
= (ar
>> 12) & 1;
1659 var
->l
= (ar
>> 13) & 1;
1660 var
->db
= (ar
>> 14) & 1;
1661 var
->g
= (ar
>> 15) & 1;
1662 var
->unusable
= (ar
>> 16) & 1;
1665 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1667 struct kvm_segment kvm_seg
;
1669 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1672 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1675 vmx_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_CS
);
1676 return kvm_seg
.selector
& 3;
1679 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1686 ar
= var
->type
& 15;
1687 ar
|= (var
->s
& 1) << 4;
1688 ar
|= (var
->dpl
& 3) << 5;
1689 ar
|= (var
->present
& 1) << 7;
1690 ar
|= (var
->avl
& 1) << 12;
1691 ar
|= (var
->l
& 1) << 13;
1692 ar
|= (var
->db
& 1) << 14;
1693 ar
|= (var
->g
& 1) << 15;
1695 if (ar
== 0) /* a 0 value means unusable */
1696 ar
= AR_UNUSABLE_MASK
;
1701 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1702 struct kvm_segment
*var
, int seg
)
1704 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1707 if (vcpu
->arch
.rmode
.active
&& seg
== VCPU_SREG_TR
) {
1708 vcpu
->arch
.rmode
.tr
.selector
= var
->selector
;
1709 vcpu
->arch
.rmode
.tr
.base
= var
->base
;
1710 vcpu
->arch
.rmode
.tr
.limit
= var
->limit
;
1711 vcpu
->arch
.rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1714 vmcs_writel(sf
->base
, var
->base
);
1715 vmcs_write32(sf
->limit
, var
->limit
);
1716 vmcs_write16(sf
->selector
, var
->selector
);
1717 if (vcpu
->arch
.rmode
.active
&& var
->s
) {
1719 * Hack real-mode segments into vm86 compatibility.
1721 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1722 vmcs_writel(sf
->base
, 0xf0000);
1725 ar
= vmx_segment_access_rights(var
);
1726 vmcs_write32(sf
->ar_bytes
, ar
);
1729 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1731 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1733 *db
= (ar
>> 14) & 1;
1734 *l
= (ar
>> 13) & 1;
1737 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1739 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1740 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1743 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1745 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1746 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1749 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1751 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1752 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1755 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1757 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1758 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1761 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1763 struct kvm_segment var
;
1766 vmx_get_segment(vcpu
, &var
, seg
);
1767 ar
= vmx_segment_access_rights(&var
);
1769 if (var
.base
!= (var
.selector
<< 4))
1771 if (var
.limit
!= 0xffff)
1779 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
1781 struct kvm_segment cs
;
1782 unsigned int cs_rpl
;
1784 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1785 cs_rpl
= cs
.selector
& SELECTOR_RPL_MASK
;
1789 if (~cs
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_ACCESSES_MASK
))
1793 if (cs
.type
& AR_TYPE_WRITEABLE_MASK
) {
1794 if (cs
.dpl
> cs_rpl
)
1797 if (cs
.dpl
!= cs_rpl
)
1803 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1807 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
1809 struct kvm_segment ss
;
1810 unsigned int ss_rpl
;
1812 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1813 ss_rpl
= ss
.selector
& SELECTOR_RPL_MASK
;
1817 if (ss
.type
!= 3 && ss
.type
!= 7)
1821 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
1829 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1831 struct kvm_segment var
;
1834 vmx_get_segment(vcpu
, &var
, seg
);
1835 rpl
= var
.selector
& SELECTOR_RPL_MASK
;
1843 if (~var
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_WRITEABLE_MASK
)) {
1844 if (var
.dpl
< rpl
) /* DPL < RPL */
1848 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1854 static bool tr_valid(struct kvm_vcpu
*vcpu
)
1856 struct kvm_segment tr
;
1858 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
1862 if (tr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
1864 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
1872 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
1874 struct kvm_segment ldtr
;
1876 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
1880 if (ldtr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
1890 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
1892 struct kvm_segment cs
, ss
;
1894 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1895 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1897 return ((cs
.selector
& SELECTOR_RPL_MASK
) ==
1898 (ss
.selector
& SELECTOR_RPL_MASK
));
1902 * Check if guest state is valid. Returns true if valid, false if
1904 * We assume that registers are always usable
1906 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
1908 /* real mode guest state checks */
1909 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) {
1910 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
1912 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
1914 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
1916 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
1918 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
1920 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
1923 /* protected mode guest state checks */
1924 if (!cs_ss_rpl_check(vcpu
))
1926 if (!code_segment_valid(vcpu
))
1928 if (!stack_segment_valid(vcpu
))
1930 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
1932 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
1934 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
1936 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
1938 if (!tr_valid(vcpu
))
1940 if (!ldtr_valid(vcpu
))
1944 * - Add checks on RIP
1945 * - Add checks on RFLAGS
1951 static int init_rmode_tss(struct kvm
*kvm
)
1953 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
1958 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1961 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
1962 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
1963 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
1966 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
1969 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1973 r
= kvm_write_guest_page(kvm
, fn
, &data
,
1974 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
1984 static int init_rmode_identity_map(struct kvm
*kvm
)
1987 pfn_t identity_map_pfn
;
1992 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
1993 printk(KERN_ERR
"EPT: identity-mapping pagetable "
1994 "haven't been allocated!\n");
1997 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
2000 identity_map_pfn
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
;
2001 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
2004 /* Set up identity-mapping pagetable for EPT in real mode */
2005 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
2006 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
2007 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
2008 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
2009 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
2013 kvm
->arch
.ept_identity_pagetable_done
= true;
2019 static void seg_setup(int seg
)
2021 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
2023 vmcs_write16(sf
->selector
, 0);
2024 vmcs_writel(sf
->base
, 0);
2025 vmcs_write32(sf
->limit
, 0xffff);
2026 vmcs_write32(sf
->ar_bytes
, 0xf3);
2029 static int alloc_apic_access_page(struct kvm
*kvm
)
2031 struct kvm_userspace_memory_region kvm_userspace_mem
;
2034 down_write(&kvm
->slots_lock
);
2035 if (kvm
->arch
.apic_access_page
)
2037 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
2038 kvm_userspace_mem
.flags
= 0;
2039 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
2040 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2041 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2045 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
2047 up_write(&kvm
->slots_lock
);
2051 static int alloc_identity_pagetable(struct kvm
*kvm
)
2053 struct kvm_userspace_memory_region kvm_userspace_mem
;
2056 down_write(&kvm
->slots_lock
);
2057 if (kvm
->arch
.ept_identity_pagetable
)
2059 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
2060 kvm_userspace_mem
.flags
= 0;
2061 kvm_userspace_mem
.guest_phys_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
2062 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2063 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2067 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
2068 VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
);
2070 up_write(&kvm
->slots_lock
);
2074 static void allocate_vpid(struct vcpu_vmx
*vmx
)
2079 if (!enable_vpid
|| !cpu_has_vmx_vpid())
2081 spin_lock(&vmx_vpid_lock
);
2082 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
2083 if (vpid
< VMX_NR_VPIDS
) {
2085 __set_bit(vpid
, vmx_vpid_bitmap
);
2087 spin_unlock(&vmx_vpid_lock
);
2090 static void vmx_disable_intercept_for_msr(struct page
*msr_bitmap
, u32 msr
)
2094 if (!cpu_has_vmx_msr_bitmap())
2098 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2099 * have the write-low and read-high bitmap offsets the wrong way round.
2100 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2102 va
= kmap(msr_bitmap
);
2103 if (msr
<= 0x1fff) {
2104 __clear_bit(msr
, va
+ 0x000); /* read-low */
2105 __clear_bit(msr
, va
+ 0x800); /* write-low */
2106 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
2108 __clear_bit(msr
, va
+ 0x400); /* read-high */
2109 __clear_bit(msr
, va
+ 0xc00); /* write-high */
2115 * Sets up the vmcs for emulated real mode.
2117 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
2119 u32 host_sysenter_cs
, msr_low
, msr_high
;
2121 u64 host_pat
, tsc_this
, tsc_base
;
2123 struct descriptor_table dt
;
2125 unsigned long kvm_vmx_return
;
2129 vmcs_write64(IO_BITMAP_A
, page_to_phys(vmx_io_bitmap_a
));
2130 vmcs_write64(IO_BITMAP_B
, page_to_phys(vmx_io_bitmap_b
));
2132 if (cpu_has_vmx_msr_bitmap())
2133 vmcs_write64(MSR_BITMAP
, page_to_phys(vmx_msr_bitmap
));
2135 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
2138 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
2139 vmcs_config
.pin_based_exec_ctrl
);
2141 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
2142 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
2143 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
2144 #ifdef CONFIG_X86_64
2145 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
2146 CPU_BASED_CR8_LOAD_EXITING
;
2150 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
2151 CPU_BASED_CR3_LOAD_EXITING
|
2152 CPU_BASED_INVLPG_EXITING
;
2153 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
2155 if (cpu_has_secondary_exec_ctrls()) {
2156 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
2157 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2159 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
2161 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
2163 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
2164 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
2167 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
2168 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
2169 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
2171 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
2172 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
2173 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2175 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
2176 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2177 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2178 vmcs_write16(HOST_FS_SELECTOR
, kvm_read_fs()); /* 22.2.4 */
2179 vmcs_write16(HOST_GS_SELECTOR
, kvm_read_gs()); /* 22.2.4 */
2180 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2181 #ifdef CONFIG_X86_64
2182 rdmsrl(MSR_FS_BASE
, a
);
2183 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
2184 rdmsrl(MSR_GS_BASE
, a
);
2185 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
2187 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
2188 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
2191 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
2194 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
2196 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
2197 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
2198 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
2199 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
2200 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
2202 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
2203 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
2204 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
2205 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
2206 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
2207 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
2209 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
2210 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2211 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2212 vmcs_write64(HOST_IA32_PAT
, host_pat
);
2214 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
2215 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2216 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2217 /* Write the default value follow host pat */
2218 vmcs_write64(GUEST_IA32_PAT
, host_pat
);
2219 /* Keep arch.pat sync with GUEST_IA32_PAT */
2220 vmx
->vcpu
.arch
.pat
= host_pat
;
2223 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
2224 u32 index
= vmx_msr_index
[i
];
2225 u32 data_low
, data_high
;
2229 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
2231 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
2233 data
= data_low
| ((u64
)data_high
<< 32);
2234 vmx
->host_msrs
[j
].index
= index
;
2235 vmx
->host_msrs
[j
].reserved
= 0;
2236 vmx
->host_msrs
[j
].data
= data
;
2237 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
2241 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
2243 /* 22.2.1, 20.8.1 */
2244 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
2246 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
2247 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
2249 tsc_base
= vmx
->vcpu
.kvm
->arch
.vm_init_tsc
;
2251 if (tsc_this
< vmx
->vcpu
.kvm
->arch
.vm_init_tsc
)
2252 tsc_base
= tsc_this
;
2254 guest_write_tsc(0, tsc_base
);
2259 static int init_rmode(struct kvm
*kvm
)
2261 if (!init_rmode_tss(kvm
))
2263 if (!init_rmode_identity_map(kvm
))
2268 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
2270 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2274 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
2275 down_read(&vcpu
->kvm
->slots_lock
);
2276 if (!init_rmode(vmx
->vcpu
.kvm
)) {
2281 vmx
->vcpu
.arch
.rmode
.active
= 0;
2283 vmx
->soft_vnmi_blocked
= 0;
2285 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
2286 kvm_set_cr8(&vmx
->vcpu
, 0);
2287 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
2288 if (vmx
->vcpu
.vcpu_id
== 0)
2289 msr
|= MSR_IA32_APICBASE_BSP
;
2290 kvm_set_apic_base(&vmx
->vcpu
, msr
);
2292 fx_init(&vmx
->vcpu
);
2294 seg_setup(VCPU_SREG_CS
);
2296 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2297 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2299 if (vmx
->vcpu
.vcpu_id
== 0) {
2300 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
2301 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
2303 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
2304 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
2307 seg_setup(VCPU_SREG_DS
);
2308 seg_setup(VCPU_SREG_ES
);
2309 seg_setup(VCPU_SREG_FS
);
2310 seg_setup(VCPU_SREG_GS
);
2311 seg_setup(VCPU_SREG_SS
);
2313 vmcs_write16(GUEST_TR_SELECTOR
, 0);
2314 vmcs_writel(GUEST_TR_BASE
, 0);
2315 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
2316 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
2318 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
2319 vmcs_writel(GUEST_LDTR_BASE
, 0);
2320 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
2321 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
2323 vmcs_write32(GUEST_SYSENTER_CS
, 0);
2324 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
2325 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
2327 vmcs_writel(GUEST_RFLAGS
, 0x02);
2328 if (vmx
->vcpu
.vcpu_id
== 0)
2329 kvm_rip_write(vcpu
, 0xfff0);
2331 kvm_rip_write(vcpu
, 0);
2332 kvm_register_write(vcpu
, VCPU_REGS_RSP
, 0);
2334 vmcs_writel(GUEST_DR7
, 0x400);
2336 vmcs_writel(GUEST_GDTR_BASE
, 0);
2337 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
2339 vmcs_writel(GUEST_IDTR_BASE
, 0);
2340 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
2342 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
2343 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
2344 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
2346 /* Special registers */
2347 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
2351 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
2353 if (cpu_has_vmx_tpr_shadow()) {
2354 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
2355 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
2356 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
2357 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
2358 vmcs_write32(TPR_THRESHOLD
, 0);
2361 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2362 vmcs_write64(APIC_ACCESS_ADDR
,
2363 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
2366 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
2368 vmx
->vcpu
.arch
.cr0
= 0x60000010;
2369 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
2370 vmx_set_cr4(&vmx
->vcpu
, 0);
2371 vmx_set_efer(&vmx
->vcpu
, 0);
2372 vmx_fpu_activate(&vmx
->vcpu
);
2373 update_exception_bitmap(&vmx
->vcpu
);
2375 vpid_sync_vcpu_all(vmx
);
2379 /* HACK: Don't enable emulation on guest boot/reset */
2380 vmx
->emulation_required
= 0;
2383 up_read(&vcpu
->kvm
->slots_lock
);
2387 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2389 u32 cpu_based_vm_exec_control
;
2391 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2392 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2393 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2396 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2398 u32 cpu_based_vm_exec_control
;
2400 if (!cpu_has_virtual_nmis()) {
2401 enable_irq_window(vcpu
);
2405 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2406 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_NMI_PENDING
;
2407 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2410 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
, int irq
)
2412 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2414 KVMTRACE_1D(INJ_VIRQ
, vcpu
, (u32
)irq
, handler
);
2416 ++vcpu
->stat
.irq_injections
;
2417 if (vcpu
->arch
.rmode
.active
) {
2418 vmx
->rmode
.irq
.pending
= true;
2419 vmx
->rmode
.irq
.vector
= irq
;
2420 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2421 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2422 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
2423 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2424 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2427 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2428 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
2431 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
2433 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2435 if (!cpu_has_virtual_nmis()) {
2437 * Tracking the NMI-blocked state in software is built upon
2438 * finding the next open IRQ window. This, in turn, depends on
2439 * well-behaving guests: They have to keep IRQs disabled at
2440 * least as long as the NMI handler runs. Otherwise we may
2441 * cause NMI nesting, maybe breaking the guest. But as this is
2442 * highly unlikely, we can live with the residual risk.
2444 vmx
->soft_vnmi_blocked
= 1;
2445 vmx
->vnmi_blocked_time
= 0;
2448 ++vcpu
->stat
.nmi_injections
;
2449 if (vcpu
->arch
.rmode
.active
) {
2450 vmx
->rmode
.irq
.pending
= true;
2451 vmx
->rmode
.irq
.vector
= NMI_VECTOR
;
2452 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2453 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2454 NMI_VECTOR
| INTR_TYPE_SOFT_INTR
|
2455 INTR_INFO_VALID_MASK
);
2456 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2457 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2460 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2461 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
2464 static void vmx_update_window_states(struct kvm_vcpu
*vcpu
)
2466 u32 guest_intr
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2468 vcpu
->arch
.nmi_window_open
=
2469 !(guest_intr
& (GUEST_INTR_STATE_STI
|
2470 GUEST_INTR_STATE_MOV_SS
|
2471 GUEST_INTR_STATE_NMI
));
2472 if (!cpu_has_virtual_nmis() && to_vmx(vcpu
)->soft_vnmi_blocked
)
2473 vcpu
->arch
.nmi_window_open
= 0;
2475 vcpu
->arch
.interrupt_window_open
=
2476 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2477 !(guest_intr
& (GUEST_INTR_STATE_STI
|
2478 GUEST_INTR_STATE_MOV_SS
)));
2481 static void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
2483 int word_index
= __ffs(vcpu
->arch
.irq_summary
);
2484 int bit_index
= __ffs(vcpu
->arch
.irq_pending
[word_index
]);
2485 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
2487 clear_bit(bit_index
, &vcpu
->arch
.irq_pending
[word_index
]);
2488 if (!vcpu
->arch
.irq_pending
[word_index
])
2489 clear_bit(word_index
, &vcpu
->arch
.irq_summary
);
2490 kvm_queue_interrupt(vcpu
, irq
);
2493 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
2494 struct kvm_run
*kvm_run
)
2496 vmx_update_window_states(vcpu
);
2498 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
2499 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
2500 GUEST_INTR_STATE_STI
|
2501 GUEST_INTR_STATE_MOV_SS
);
2503 if (vcpu
->arch
.nmi_pending
&& !vcpu
->arch
.nmi_injected
) {
2504 if (vcpu
->arch
.interrupt
.pending
) {
2505 enable_nmi_window(vcpu
);
2506 } else if (vcpu
->arch
.nmi_window_open
) {
2507 vcpu
->arch
.nmi_pending
= false;
2508 vcpu
->arch
.nmi_injected
= true;
2510 enable_nmi_window(vcpu
);
2514 if (vcpu
->arch
.nmi_injected
) {
2515 vmx_inject_nmi(vcpu
);
2516 if (vcpu
->arch
.nmi_pending
)
2517 enable_nmi_window(vcpu
);
2518 else if (vcpu
->arch
.irq_summary
2519 || kvm_run
->request_interrupt_window
)
2520 enable_irq_window(vcpu
);
2524 if (vcpu
->arch
.interrupt_window_open
) {
2525 if (vcpu
->arch
.irq_summary
&& !vcpu
->arch
.interrupt
.pending
)
2526 kvm_do_inject_irq(vcpu
);
2528 if (vcpu
->arch
.interrupt
.pending
)
2529 vmx_inject_irq(vcpu
, vcpu
->arch
.interrupt
.nr
);
2531 if (!vcpu
->arch
.interrupt_window_open
&&
2532 (vcpu
->arch
.irq_summary
|| kvm_run
->request_interrupt_window
))
2533 enable_irq_window(vcpu
);
2536 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2539 struct kvm_userspace_memory_region tss_mem
= {
2540 .slot
= TSS_PRIVATE_MEMSLOT
,
2541 .guest_phys_addr
= addr
,
2542 .memory_size
= PAGE_SIZE
* 3,
2546 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
2549 kvm
->arch
.tss_addr
= addr
;
2553 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
2554 int vec
, u32 err_code
)
2557 * Instruction with address size override prefix opcode 0x67
2558 * Cause the #SS fault with 0 error code in VM86 mode.
2560 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
2561 if (emulate_instruction(vcpu
, NULL
, 0, 0, 0) == EMULATE_DONE
)
2564 * Forward all other exceptions that are valid in real mode.
2565 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2566 * the required debugging infrastructure rework.
2570 if (vcpu
->guest_debug
&
2571 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
2573 kvm_queue_exception(vcpu
, vec
);
2576 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
2587 kvm_queue_exception(vcpu
, vec
);
2593 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2595 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2596 u32 intr_info
, ex_no
, error_code
;
2597 unsigned long cr2
, rip
, dr6
;
2599 enum emulation_result er
;
2601 vect_info
= vmx
->idt_vectoring_info
;
2602 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2604 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
2605 !is_page_fault(intr_info
))
2606 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
2607 "intr info 0x%x\n", __func__
, vect_info
, intr_info
);
2609 if (!irqchip_in_kernel(vcpu
->kvm
) && is_external_interrupt(vect_info
)) {
2610 int irq
= vect_info
& VECTORING_INFO_VECTOR_MASK
;
2611 set_bit(irq
, vcpu
->arch
.irq_pending
);
2612 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
2615 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
)
2616 return 1; /* already handled by vmx_vcpu_run() */
2618 if (is_no_device(intr_info
)) {
2619 vmx_fpu_activate(vcpu
);
2623 if (is_invalid_opcode(intr_info
)) {
2624 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
2625 if (er
!= EMULATE_DONE
)
2626 kvm_queue_exception(vcpu
, UD_VECTOR
);
2631 rip
= kvm_rip_read(vcpu
);
2632 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2633 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2634 if (is_page_fault(intr_info
)) {
2635 /* EPT won't cause page fault directly */
2638 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2639 KVMTRACE_3D(PAGE_FAULT
, vcpu
, error_code
, (u32
)cr2
,
2640 (u32
)((u64
)cr2
>> 32), handler
);
2641 if (vcpu
->arch
.interrupt
.pending
|| vcpu
->arch
.exception
.pending
)
2642 kvm_mmu_unprotect_page_virt(vcpu
, cr2
);
2643 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2646 if (vcpu
->arch
.rmode
.active
&&
2647 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2649 if (vcpu
->arch
.halt_request
) {
2650 vcpu
->arch
.halt_request
= 0;
2651 return kvm_emulate_halt(vcpu
);
2656 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
2659 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
2660 if (!(vcpu
->guest_debug
&
2661 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
2662 vcpu
->arch
.dr6
= dr6
| DR6_FIXED_1
;
2663 kvm_queue_exception(vcpu
, DB_VECTOR
);
2666 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
2667 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
2670 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2671 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
2672 kvm_run
->debug
.arch
.exception
= ex_no
;
2675 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2676 kvm_run
->ex
.exception
= ex_no
;
2677 kvm_run
->ex
.error_code
= error_code
;
2683 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2684 struct kvm_run
*kvm_run
)
2686 ++vcpu
->stat
.irq_exits
;
2687 KVMTRACE_1D(INTR
, vcpu
, vmcs_read32(VM_EXIT_INTR_INFO
), handler
);
2691 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2693 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2697 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2699 unsigned long exit_qualification
;
2700 int size
, down
, in
, string
, rep
;
2703 ++vcpu
->stat
.io_exits
;
2704 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2705 string
= (exit_qualification
& 16) != 0;
2708 if (emulate_instruction(vcpu
,
2709 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2714 size
= (exit_qualification
& 7) + 1;
2715 in
= (exit_qualification
& 8) != 0;
2716 down
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_DF
) != 0;
2717 rep
= (exit_qualification
& 32) != 0;
2718 port
= exit_qualification
>> 16;
2720 skip_emulated_instruction(vcpu
);
2721 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2725 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2728 * Patch in the VMCALL instruction:
2730 hypercall
[0] = 0x0f;
2731 hypercall
[1] = 0x01;
2732 hypercall
[2] = 0xc1;
2735 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2737 unsigned long exit_qualification
;
2741 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2742 cr
= exit_qualification
& 15;
2743 reg
= (exit_qualification
>> 8) & 15;
2744 switch ((exit_qualification
>> 4) & 3) {
2745 case 0: /* mov to cr */
2746 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
,
2747 (u32
)kvm_register_read(vcpu
, reg
),
2748 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2752 kvm_set_cr0(vcpu
, kvm_register_read(vcpu
, reg
));
2753 skip_emulated_instruction(vcpu
);
2756 kvm_set_cr3(vcpu
, kvm_register_read(vcpu
, reg
));
2757 skip_emulated_instruction(vcpu
);
2760 kvm_set_cr4(vcpu
, kvm_register_read(vcpu
, reg
));
2761 skip_emulated_instruction(vcpu
);
2764 kvm_set_cr8(vcpu
, kvm_register_read(vcpu
, reg
));
2765 skip_emulated_instruction(vcpu
);
2766 if (irqchip_in_kernel(vcpu
->kvm
))
2768 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2773 vmx_fpu_deactivate(vcpu
);
2774 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2775 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2776 vmx_fpu_activate(vcpu
);
2777 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2778 skip_emulated_instruction(vcpu
);
2780 case 1: /*mov from cr*/
2783 kvm_register_write(vcpu
, reg
, vcpu
->arch
.cr3
);
2784 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
,
2785 (u32
)kvm_register_read(vcpu
, reg
),
2786 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2788 skip_emulated_instruction(vcpu
);
2791 kvm_register_write(vcpu
, reg
, kvm_get_cr8(vcpu
));
2792 KVMTRACE_2D(CR_READ
, vcpu
, (u32
)cr
,
2793 (u32
)kvm_register_read(vcpu
, reg
), handler
);
2794 skip_emulated_instruction(vcpu
);
2799 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2801 skip_emulated_instruction(vcpu
);
2806 kvm_run
->exit_reason
= 0;
2807 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2808 (int)(exit_qualification
>> 4) & 3, cr
);
2812 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2814 unsigned long exit_qualification
;
2818 dr
= vmcs_readl(GUEST_DR7
);
2821 * As the vm-exit takes precedence over the debug trap, we
2822 * need to emulate the latter, either for the host or the
2823 * guest debugging itself.
2825 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
2826 kvm_run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
2827 kvm_run
->debug
.arch
.dr7
= dr
;
2828 kvm_run
->debug
.arch
.pc
=
2829 vmcs_readl(GUEST_CS_BASE
) +
2830 vmcs_readl(GUEST_RIP
);
2831 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
2832 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2835 vcpu
->arch
.dr7
&= ~DR7_GD
;
2836 vcpu
->arch
.dr6
|= DR6_BD
;
2837 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
2838 kvm_queue_exception(vcpu
, DB_VECTOR
);
2843 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2844 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
2845 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
2846 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
2849 val
= vcpu
->arch
.db
[dr
];
2852 val
= vcpu
->arch
.dr6
;
2855 val
= vcpu
->arch
.dr7
;
2860 kvm_register_write(vcpu
, reg
, val
);
2861 KVMTRACE_2D(DR_READ
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2863 val
= vcpu
->arch
.regs
[reg
];
2866 vcpu
->arch
.db
[dr
] = val
;
2867 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
2868 vcpu
->arch
.eff_db
[dr
] = val
;
2871 if (vcpu
->arch
.cr4
& X86_CR4_DE
)
2872 kvm_queue_exception(vcpu
, UD_VECTOR
);
2875 if (val
& 0xffffffff00000000ULL
) {
2876 kvm_queue_exception(vcpu
, GP_VECTOR
);
2879 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
2882 if (val
& 0xffffffff00000000ULL
) {
2883 kvm_queue_exception(vcpu
, GP_VECTOR
);
2886 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
2887 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
2888 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
2889 vcpu
->arch
.switch_db_regs
=
2890 (val
& DR7_BP_EN_MASK
);
2894 KVMTRACE_2D(DR_WRITE
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2896 skip_emulated_instruction(vcpu
);
2900 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2902 kvm_emulate_cpuid(vcpu
);
2906 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2908 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2911 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
2912 kvm_inject_gp(vcpu
, 0);
2916 KVMTRACE_3D(MSR_READ
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2919 /* FIXME: handling of bits 32:63 of rax, rdx */
2920 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
2921 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
2922 skip_emulated_instruction(vcpu
);
2926 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2928 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2929 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
2930 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2932 KVMTRACE_3D(MSR_WRITE
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2935 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
2936 kvm_inject_gp(vcpu
, 0);
2940 skip_emulated_instruction(vcpu
);
2944 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
2945 struct kvm_run
*kvm_run
)
2950 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
2951 struct kvm_run
*kvm_run
)
2953 u32 cpu_based_vm_exec_control
;
2955 /* clear pending irq */
2956 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2957 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2958 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2960 KVMTRACE_0D(PEND_INTR
, vcpu
, handler
);
2961 ++vcpu
->stat
.irq_window_exits
;
2964 * If the user space waits to inject interrupts, exit as soon as
2967 if (kvm_run
->request_interrupt_window
&&
2968 !vcpu
->arch
.irq_summary
) {
2969 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2975 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2977 skip_emulated_instruction(vcpu
);
2978 return kvm_emulate_halt(vcpu
);
2981 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2983 skip_emulated_instruction(vcpu
);
2984 kvm_emulate_hypercall(vcpu
);
2988 static int handle_invlpg(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2990 u64 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2992 kvm_mmu_invlpg(vcpu
, exit_qualification
);
2993 skip_emulated_instruction(vcpu
);
2997 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2999 skip_emulated_instruction(vcpu
);
3000 /* TODO: Add support for VT-d/pass-through device */
3004 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3006 u64 exit_qualification
;
3007 enum emulation_result er
;
3008 unsigned long offset
;
3010 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
3011 offset
= exit_qualification
& 0xffful
;
3013 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3015 if (er
!= EMULATE_DONE
) {
3017 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3024 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3026 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3027 unsigned long exit_qualification
;
3031 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3033 reason
= (u32
)exit_qualification
>> 30;
3034 if (reason
== TASK_SWITCH_GATE
&& vmx
->vcpu
.arch
.nmi_injected
&&
3035 (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3036 (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
)
3037 == INTR_TYPE_NMI_INTR
) {
3038 vcpu
->arch
.nmi_injected
= false;
3039 if (cpu_has_virtual_nmis())
3040 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3041 GUEST_INTR_STATE_NMI
);
3043 tss_selector
= exit_qualification
;
3045 if (!kvm_task_switch(vcpu
, tss_selector
, reason
))
3048 /* clear all local breakpoint enable flags */
3049 vmcs_writel(GUEST_DR7
, vmcs_readl(GUEST_DR7
) & ~55);
3052 * TODO: What about debug traps on tss switch?
3053 * Are we supposed to inject them and update dr6?
3059 static int handle_ept_violation(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3061 u64 exit_qualification
;
3062 enum emulation_result er
;
3068 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
3070 if (exit_qualification
& (1 << 6)) {
3071 printk(KERN_ERR
"EPT: GPA exceeds GAW!\n");
3075 gla_validity
= (exit_qualification
>> 7) & 0x3;
3076 if (gla_validity
!= 0x3 && gla_validity
!= 0x1 && gla_validity
!= 0) {
3077 printk(KERN_ERR
"EPT: Handling EPT violation failed!\n");
3078 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3079 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
3080 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS
));
3081 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
3082 (long unsigned int)exit_qualification
);
3083 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3084 kvm_run
->hw
.hardware_exit_reason
= 0;
3088 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3089 hva
= gfn_to_hva(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
3090 if (!kvm_is_error_hva(hva
)) {
3091 r
= kvm_mmu_page_fault(vcpu
, gpa
& PAGE_MASK
, 0);
3093 printk(KERN_ERR
"EPT: Not enough memory!\n");
3099 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3101 if (er
== EMULATE_FAIL
) {
3103 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
3105 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3106 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
3107 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS
));
3108 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
3109 (long unsigned int)exit_qualification
);
3111 } else if (er
== EMULATE_DO_MMIO
)
3117 static int handle_nmi_window(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3119 u32 cpu_based_vm_exec_control
;
3121 /* clear pending NMI */
3122 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3123 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
3124 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3125 ++vcpu
->stat
.nmi_window_exits
;
3130 static void handle_invalid_guest_state(struct kvm_vcpu
*vcpu
,
3131 struct kvm_run
*kvm_run
)
3133 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3139 while (!guest_state_valid(vcpu
)) {
3140 err
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3142 if (err
== EMULATE_DO_MMIO
)
3145 if (err
!= EMULATE_DONE
) {
3146 kvm_report_emulation_failure(vcpu
, "emulation failure");
3150 if (signal_pending(current
))
3156 local_irq_disable();
3159 /* Guest state should be valid now except if we need to
3160 * emulate an MMIO */
3161 if (guest_state_valid(vcpu
))
3162 vmx
->emulation_required
= 0;
3166 * The exit handlers return 1 if the exit was handled fully and guest execution
3167 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3168 * to be done to userspace and return 0.
3170 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
3171 struct kvm_run
*kvm_run
) = {
3172 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
3173 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
3174 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
3175 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
3176 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
3177 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
3178 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
3179 [EXIT_REASON_CPUID
] = handle_cpuid
,
3180 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
3181 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
3182 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
3183 [EXIT_REASON_HLT
] = handle_halt
,
3184 [EXIT_REASON_INVLPG
] = handle_invlpg
,
3185 [EXIT_REASON_VMCALL
] = handle_vmcall
,
3186 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
3187 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
3188 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
3189 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
3190 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
3193 static const int kvm_vmx_max_exit_handlers
=
3194 ARRAY_SIZE(kvm_vmx_exit_handlers
);
3197 * The guest has exited. See if we can fix it or if we need userspace
3200 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
3202 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
3203 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3204 u32 vectoring_info
= vmx
->idt_vectoring_info
;
3206 KVMTRACE_3D(VMEXIT
, vcpu
, exit_reason
, (u32
)kvm_rip_read(vcpu
),
3207 (u32
)((u64
)kvm_rip_read(vcpu
) >> 32), entryexit
);
3209 /* If we need to emulate an MMIO from handle_invalid_guest_state
3210 * we just return 0 */
3211 if (vmx
->emulation_required
&& emulate_invalid_guest_state
)
3214 /* Access CR3 don't cause VMExit in paging mode, so we need
3215 * to sync with guest real CR3. */
3216 if (vm_need_ept() && is_paging(vcpu
)) {
3217 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
3218 ept_load_pdptrs(vcpu
);
3221 if (unlikely(vmx
->fail
)) {
3222 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3223 kvm_run
->fail_entry
.hardware_entry_failure_reason
3224 = vmcs_read32(VM_INSTRUCTION_ERROR
);
3228 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3229 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
3230 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
3231 exit_reason
!= EXIT_REASON_TASK_SWITCH
))
3232 printk(KERN_WARNING
"%s: unexpected, valid vectoring info "
3233 "(0x%x) and exit reason is 0x%x\n",
3234 __func__
, vectoring_info
, exit_reason
);
3236 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
)) {
3237 if (vcpu
->arch
.interrupt_window_open
) {
3238 vmx
->soft_vnmi_blocked
= 0;
3239 vcpu
->arch
.nmi_window_open
= 1;
3240 } else if (vmx
->vnmi_blocked_time
> 1000000000LL &&
3241 vcpu
->arch
.nmi_pending
) {
3243 * This CPU don't support us in finding the end of an
3244 * NMI-blocked window if the guest runs with IRQs
3245 * disabled. So we pull the trigger after 1 s of
3246 * futile waiting, but inform the user about this.
3248 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
3249 "state on VCPU %d after 1 s timeout\n",
3250 __func__
, vcpu
->vcpu_id
);
3251 vmx
->soft_vnmi_blocked
= 0;
3252 vmx
->vcpu
.arch
.nmi_window_open
= 1;
3256 if (exit_reason
< kvm_vmx_max_exit_handlers
3257 && kvm_vmx_exit_handlers
[exit_reason
])
3258 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
3260 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3261 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
3266 static void update_tpr_threshold(struct kvm_vcpu
*vcpu
)
3270 if (!vm_need_tpr_shadow(vcpu
->kvm
))
3273 if (!kvm_lapic_enabled(vcpu
) ||
3274 ((max_irr
= kvm_lapic_find_highest_irr(vcpu
)) == -1)) {
3275 vmcs_write32(TPR_THRESHOLD
, 0);
3279 tpr
= (kvm_lapic_get_cr8(vcpu
) & 0x0f) << 4;
3280 vmcs_write32(TPR_THRESHOLD
, (max_irr
> tpr
) ? tpr
>> 4 : max_irr
>> 4);
3283 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
3286 u32 idt_vectoring_info
;
3290 bool idtv_info_valid
;
3293 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3294 if (cpu_has_virtual_nmis()) {
3295 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
3296 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
3299 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3300 * a guest IRET fault.
3302 if (unblock_nmi
&& vector
!= DF_VECTOR
)
3303 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3304 GUEST_INTR_STATE_NMI
);
3305 } else if (unlikely(vmx
->soft_vnmi_blocked
))
3306 vmx
->vnmi_blocked_time
+=
3307 ktime_to_ns(ktime_sub(ktime_get(), vmx
->entry_time
));
3309 idt_vectoring_info
= vmx
->idt_vectoring_info
;
3310 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
3311 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
3312 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
3313 if (vmx
->vcpu
.arch
.nmi_injected
) {
3316 * Clear bit "block by NMI" before VM entry if a NMI delivery
3319 if (idtv_info_valid
&& type
== INTR_TYPE_NMI_INTR
)
3320 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3321 GUEST_INTR_STATE_NMI
);
3323 vmx
->vcpu
.arch
.nmi_injected
= false;
3325 kvm_clear_exception_queue(&vmx
->vcpu
);
3326 if (idtv_info_valid
&& (type
== INTR_TYPE_HARD_EXCEPTION
||
3327 type
== INTR_TYPE_SOFT_EXCEPTION
)) {
3328 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
3329 error
= vmcs_read32(IDT_VECTORING_ERROR_CODE
);
3330 kvm_queue_exception_e(&vmx
->vcpu
, vector
, error
);
3332 kvm_queue_exception(&vmx
->vcpu
, vector
);
3333 vmx
->idt_vectoring_info
= 0;
3335 kvm_clear_interrupt_queue(&vmx
->vcpu
);
3336 if (idtv_info_valid
&& type
== INTR_TYPE_EXT_INTR
) {
3337 kvm_queue_interrupt(&vmx
->vcpu
, vector
);
3338 vmx
->idt_vectoring_info
= 0;
3342 static void vmx_intr_assist(struct kvm_vcpu
*vcpu
)
3344 update_tpr_threshold(vcpu
);
3346 vmx_update_window_states(vcpu
);
3348 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3349 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3350 GUEST_INTR_STATE_STI
|
3351 GUEST_INTR_STATE_MOV_SS
);
3353 if (vcpu
->arch
.nmi_pending
&& !vcpu
->arch
.nmi_injected
) {
3354 if (vcpu
->arch
.interrupt
.pending
) {
3355 enable_nmi_window(vcpu
);
3356 } else if (vcpu
->arch
.nmi_window_open
) {
3357 vcpu
->arch
.nmi_pending
= false;
3358 vcpu
->arch
.nmi_injected
= true;
3360 enable_nmi_window(vcpu
);
3364 if (vcpu
->arch
.nmi_injected
) {
3365 vmx_inject_nmi(vcpu
);
3366 if (vcpu
->arch
.nmi_pending
)
3367 enable_nmi_window(vcpu
);
3368 else if (kvm_cpu_has_interrupt(vcpu
))
3369 enable_irq_window(vcpu
);
3372 if (!vcpu
->arch
.interrupt
.pending
&& kvm_cpu_has_interrupt(vcpu
)) {
3373 if (vcpu
->arch
.interrupt_window_open
)
3374 kvm_queue_interrupt(vcpu
, kvm_cpu_get_interrupt(vcpu
));
3376 enable_irq_window(vcpu
);
3378 if (vcpu
->arch
.interrupt
.pending
) {
3379 vmx_inject_irq(vcpu
, vcpu
->arch
.interrupt
.nr
);
3380 if (kvm_cpu_has_interrupt(vcpu
))
3381 enable_irq_window(vcpu
);
3386 * Failure to inject an interrupt should give us the information
3387 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3388 * when fetching the interrupt redirection bitmap in the real-mode
3389 * tss, this doesn't happen. So we do it ourselves.
3391 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
3393 vmx
->rmode
.irq
.pending
= 0;
3394 if (kvm_rip_read(&vmx
->vcpu
) + 1 != vmx
->rmode
.irq
.rip
)
3396 kvm_rip_write(&vmx
->vcpu
, vmx
->rmode
.irq
.rip
);
3397 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
3398 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
3399 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
3402 vmx
->idt_vectoring_info
=
3403 VECTORING_INFO_VALID_MASK
3404 | INTR_TYPE_EXT_INTR
3405 | vmx
->rmode
.irq
.vector
;
3408 #ifdef CONFIG_X86_64
3416 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3418 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3421 /* Record the guest's net vcpu time for enforced NMI injections. */
3422 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
))
3423 vmx
->entry_time
= ktime_get();
3425 /* Handle invalid guest state instead of entering VMX */
3426 if (vmx
->emulation_required
&& emulate_invalid_guest_state
) {
3427 handle_invalid_guest_state(vcpu
, kvm_run
);
3431 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3432 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
3433 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3434 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
3437 * Loading guest fpu may have cleared host cr0.ts
3439 vmcs_writel(HOST_CR0
, read_cr0());
3441 set_debugreg(vcpu
->arch
.dr6
, 6);
3444 /* Store host registers */
3445 "push %%"R
"dx; push %%"R
"bp;"
3447 "cmp %%"R
"sp, %c[host_rsp](%0) \n\t"
3449 "mov %%"R
"sp, %c[host_rsp](%0) \n\t"
3450 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
3452 /* Check if vmlaunch of vmresume is needed */
3453 "cmpl $0, %c[launched](%0) \n\t"
3454 /* Load guest registers. Don't clobber flags. */
3455 "mov %c[cr2](%0), %%"R
"ax \n\t"
3456 "mov %%"R
"ax, %%cr2 \n\t"
3457 "mov %c[rax](%0), %%"R
"ax \n\t"
3458 "mov %c[rbx](%0), %%"R
"bx \n\t"
3459 "mov %c[rdx](%0), %%"R
"dx \n\t"
3460 "mov %c[rsi](%0), %%"R
"si \n\t"
3461 "mov %c[rdi](%0), %%"R
"di \n\t"
3462 "mov %c[rbp](%0), %%"R
"bp \n\t"
3463 #ifdef CONFIG_X86_64
3464 "mov %c[r8](%0), %%r8 \n\t"
3465 "mov %c[r9](%0), %%r9 \n\t"
3466 "mov %c[r10](%0), %%r10 \n\t"
3467 "mov %c[r11](%0), %%r11 \n\t"
3468 "mov %c[r12](%0), %%r12 \n\t"
3469 "mov %c[r13](%0), %%r13 \n\t"
3470 "mov %c[r14](%0), %%r14 \n\t"
3471 "mov %c[r15](%0), %%r15 \n\t"
3473 "mov %c[rcx](%0), %%"R
"cx \n\t" /* kills %0 (ecx) */
3475 /* Enter guest mode */
3476 "jne .Llaunched \n\t"
3477 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
3478 "jmp .Lkvm_vmx_return \n\t"
3479 ".Llaunched: " __ex(ASM_VMX_VMRESUME
) "\n\t"
3480 ".Lkvm_vmx_return: "
3481 /* Save guest registers, load host registers, keep flags */
3482 "xchg %0, (%%"R
"sp) \n\t"
3483 "mov %%"R
"ax, %c[rax](%0) \n\t"
3484 "mov %%"R
"bx, %c[rbx](%0) \n\t"
3485 "push"Q
" (%%"R
"sp); pop"Q
" %c[rcx](%0) \n\t"
3486 "mov %%"R
"dx, %c[rdx](%0) \n\t"
3487 "mov %%"R
"si, %c[rsi](%0) \n\t"
3488 "mov %%"R
"di, %c[rdi](%0) \n\t"
3489 "mov %%"R
"bp, %c[rbp](%0) \n\t"
3490 #ifdef CONFIG_X86_64
3491 "mov %%r8, %c[r8](%0) \n\t"
3492 "mov %%r9, %c[r9](%0) \n\t"
3493 "mov %%r10, %c[r10](%0) \n\t"
3494 "mov %%r11, %c[r11](%0) \n\t"
3495 "mov %%r12, %c[r12](%0) \n\t"
3496 "mov %%r13, %c[r13](%0) \n\t"
3497 "mov %%r14, %c[r14](%0) \n\t"
3498 "mov %%r15, %c[r15](%0) \n\t"
3500 "mov %%cr2, %%"R
"ax \n\t"
3501 "mov %%"R
"ax, %c[cr2](%0) \n\t"
3503 "pop %%"R
"bp; pop %%"R
"bp; pop %%"R
"dx \n\t"
3504 "setbe %c[fail](%0) \n\t"
3505 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
3506 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
3507 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
3508 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
3509 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
3510 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3511 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3512 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3513 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3514 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3515 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
3516 #ifdef CONFIG_X86_64
3517 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3518 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3519 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3520 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3521 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3522 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3523 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3524 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
3526 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
3528 , R
"bx", R
"di", R
"si"
3529 #ifdef CONFIG_X86_64
3530 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3534 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
3535 vcpu
->arch
.regs_dirty
= 0;
3537 get_debugreg(vcpu
->arch
.dr6
, 6);
3539 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
3540 if (vmx
->rmode
.irq
.pending
)
3541 fixup_rmode_irq(vmx
);
3543 vmx_update_window_states(vcpu
);
3545 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
3548 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3550 /* We need to handle NMIs before interrupts are enabled */
3551 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
&&
3552 (intr_info
& INTR_INFO_VALID_MASK
)) {
3553 KVMTRACE_0D(NMI
, vcpu
, handler
);
3557 vmx_complete_interrupts(vmx
);
3563 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
3565 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3569 free_vmcs(vmx
->vmcs
);
3574 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
3576 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3578 spin_lock(&vmx_vpid_lock
);
3580 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
3581 spin_unlock(&vmx_vpid_lock
);
3582 vmx_free_vmcs(vcpu
);
3583 kfree(vmx
->host_msrs
);
3584 kfree(vmx
->guest_msrs
);
3585 kvm_vcpu_uninit(vcpu
);
3586 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3589 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
3592 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
3596 return ERR_PTR(-ENOMEM
);
3600 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
3604 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3605 if (!vmx
->guest_msrs
) {
3610 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3611 if (!vmx
->host_msrs
)
3612 goto free_guest_msrs
;
3614 vmx
->vmcs
= alloc_vmcs();
3618 vmcs_clear(vmx
->vmcs
);
3621 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
3622 err
= vmx_vcpu_setup(vmx
);
3623 vmx_vcpu_put(&vmx
->vcpu
);
3627 if (vm_need_virtualize_apic_accesses(kvm
))
3628 if (alloc_apic_access_page(kvm
) != 0)
3632 if (alloc_identity_pagetable(kvm
) != 0)
3638 free_vmcs(vmx
->vmcs
);
3640 kfree(vmx
->host_msrs
);
3642 kfree(vmx
->guest_msrs
);
3644 kvm_vcpu_uninit(&vmx
->vcpu
);
3646 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3647 return ERR_PTR(err
);
3650 static void __init
vmx_check_processor_compat(void *rtn
)
3652 struct vmcs_config vmcs_conf
;
3655 if (setup_vmcs_config(&vmcs_conf
) < 0)
3657 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
3658 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
3659 smp_processor_id());
3664 static int get_ept_level(void)
3666 return VMX_EPT_DEFAULT_GAW
+ 1;
3669 static int vmx_get_mt_mask_shift(void)
3671 return VMX_EPT_MT_EPTE_SHIFT
;
3674 static struct kvm_x86_ops vmx_x86_ops
= {
3675 .cpu_has_kvm_support
= cpu_has_kvm_support
,
3676 .disabled_by_bios
= vmx_disabled_by_bios
,
3677 .hardware_setup
= hardware_setup
,
3678 .hardware_unsetup
= hardware_unsetup
,
3679 .check_processor_compatibility
= vmx_check_processor_compat
,
3680 .hardware_enable
= hardware_enable
,
3681 .hardware_disable
= hardware_disable
,
3682 .cpu_has_accelerated_tpr
= cpu_has_vmx_virtualize_apic_accesses
,
3684 .vcpu_create
= vmx_create_vcpu
,
3685 .vcpu_free
= vmx_free_vcpu
,
3686 .vcpu_reset
= vmx_vcpu_reset
,
3688 .prepare_guest_switch
= vmx_save_host_state
,
3689 .vcpu_load
= vmx_vcpu_load
,
3690 .vcpu_put
= vmx_vcpu_put
,
3692 .set_guest_debug
= set_guest_debug
,
3693 .get_msr
= vmx_get_msr
,
3694 .set_msr
= vmx_set_msr
,
3695 .get_segment_base
= vmx_get_segment_base
,
3696 .get_segment
= vmx_get_segment
,
3697 .set_segment
= vmx_set_segment
,
3698 .get_cpl
= vmx_get_cpl
,
3699 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
3700 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
3701 .set_cr0
= vmx_set_cr0
,
3702 .set_cr3
= vmx_set_cr3
,
3703 .set_cr4
= vmx_set_cr4
,
3704 .set_efer
= vmx_set_efer
,
3705 .get_idt
= vmx_get_idt
,
3706 .set_idt
= vmx_set_idt
,
3707 .get_gdt
= vmx_get_gdt
,
3708 .set_gdt
= vmx_set_gdt
,
3709 .cache_reg
= vmx_cache_reg
,
3710 .get_rflags
= vmx_get_rflags
,
3711 .set_rflags
= vmx_set_rflags
,
3713 .tlb_flush
= vmx_flush_tlb
,
3715 .run
= vmx_vcpu_run
,
3716 .handle_exit
= kvm_handle_exit
,
3717 .skip_emulated_instruction
= skip_emulated_instruction
,
3718 .patch_hypercall
= vmx_patch_hypercall
,
3719 .get_irq
= vmx_get_irq
,
3720 .set_irq
= vmx_inject_irq
,
3721 .queue_exception
= vmx_queue_exception
,
3722 .exception_injected
= vmx_exception_injected
,
3723 .inject_pending_irq
= vmx_intr_assist
,
3724 .inject_pending_vectors
= do_interrupt_requests
,
3726 .set_tss_addr
= vmx_set_tss_addr
,
3727 .get_tdp_level
= get_ept_level
,
3728 .get_mt_mask_shift
= vmx_get_mt_mask_shift
,
3731 static int __init
vmx_init(void)
3736 vmx_io_bitmap_a
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3737 if (!vmx_io_bitmap_a
)
3740 vmx_io_bitmap_b
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3741 if (!vmx_io_bitmap_b
) {
3746 vmx_msr_bitmap
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3747 if (!vmx_msr_bitmap
) {
3753 * Allow direct access to the PC debug port (it is often used for I/O
3754 * delays, but the vmexits simply slow things down).
3756 va
= kmap(vmx_io_bitmap_a
);
3757 memset(va
, 0xff, PAGE_SIZE
);
3758 clear_bit(0x80, va
);
3759 kunmap(vmx_io_bitmap_a
);
3761 va
= kmap(vmx_io_bitmap_b
);
3762 memset(va
, 0xff, PAGE_SIZE
);
3763 kunmap(vmx_io_bitmap_b
);
3765 va
= kmap(vmx_msr_bitmap
);
3766 memset(va
, 0xff, PAGE_SIZE
);
3767 kunmap(vmx_msr_bitmap
);
3769 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
3771 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
3775 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_FS_BASE
);
3776 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_GS_BASE
);
3777 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_CS
);
3778 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_ESP
);
3779 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_EIP
);
3781 if (vm_need_ept()) {
3782 bypass_guest_pf
= 0;
3783 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK
|
3784 VMX_EPT_WRITABLE_MASK
);
3785 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
3786 VMX_EPT_EXECUTABLE_MASK
,
3787 VMX_EPT_DEFAULT_MT
<< VMX_EPT_MT_EPTE_SHIFT
);
3792 if (bypass_guest_pf
)
3793 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
3800 __free_page(vmx_msr_bitmap
);
3802 __free_page(vmx_io_bitmap_b
);
3804 __free_page(vmx_io_bitmap_a
);
3808 static void __exit
vmx_exit(void)
3810 __free_page(vmx_msr_bitmap
);
3811 __free_page(vmx_io_bitmap_b
);
3812 __free_page(vmx_io_bitmap_a
);
3817 module_init(vmx_init
)
3818 module_exit(vmx_exit
)