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 __read_mostly bypass_guest_pf
= 1;
42 module_param(bypass_guest_pf
, bool, S_IRUGO
);
44 static int __read_mostly enable_vpid
= 1;
45 module_param_named(vpid
, enable_vpid
, bool, 0444);
47 static int __read_mostly flexpriority_enabled
= 1;
48 module_param_named(flexpriority
, flexpriority_enabled
, bool, S_IRUGO
);
50 static int __read_mostly enable_ept
= 1;
51 module_param_named(ept
, enable_ept
, bool, S_IRUGO
);
53 static int __read_mostly emulate_invalid_guest_state
= 0;
54 module_param(emulate_invalid_guest_state
, bool, S_IRUGO
);
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
;
94 enum emulation_result invalid_state_emulation_result
;
96 /* Support for vnmi-less CPUs */
97 int soft_vnmi_blocked
;
99 s64 vnmi_blocked_time
;
102 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
104 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
107 static int init_rmode(struct kvm
*kvm
);
108 static u64
construct_eptp(unsigned long root_hpa
);
110 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
111 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
112 static DEFINE_PER_CPU(struct list_head
, vcpus_on_cpu
);
114 static unsigned long *vmx_io_bitmap_a
;
115 static unsigned long *vmx_io_bitmap_b
;
116 static unsigned long *vmx_msr_bitmap_legacy
;
117 static unsigned long *vmx_msr_bitmap_longmode
;
119 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
120 static DEFINE_SPINLOCK(vmx_vpid_lock
);
122 static struct vmcs_config
{
126 u32 pin_based_exec_ctrl
;
127 u32 cpu_based_exec_ctrl
;
128 u32 cpu_based_2nd_exec_ctrl
;
133 static struct vmx_capability
{
138 #define VMX_SEGMENT_FIELD(seg) \
139 [VCPU_SREG_##seg] = { \
140 .selector = GUEST_##seg##_SELECTOR, \
141 .base = GUEST_##seg##_BASE, \
142 .limit = GUEST_##seg##_LIMIT, \
143 .ar_bytes = GUEST_##seg##_AR_BYTES, \
146 static struct kvm_vmx_segment_field
{
151 } kvm_vmx_segment_fields
[] = {
152 VMX_SEGMENT_FIELD(CS
),
153 VMX_SEGMENT_FIELD(DS
),
154 VMX_SEGMENT_FIELD(ES
),
155 VMX_SEGMENT_FIELD(FS
),
156 VMX_SEGMENT_FIELD(GS
),
157 VMX_SEGMENT_FIELD(SS
),
158 VMX_SEGMENT_FIELD(TR
),
159 VMX_SEGMENT_FIELD(LDTR
),
163 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
164 * away by decrementing the array size.
166 static const u32 vmx_msr_index
[] = {
168 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
170 MSR_EFER
, MSR_K6_STAR
,
172 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
174 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
178 for (i
= 0; i
< n
; ++i
)
179 wrmsrl(e
[i
].index
, e
[i
].data
);
182 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
186 for (i
= 0; i
< n
; ++i
)
187 rdmsrl(e
[i
].index
, e
[i
].data
);
190 static inline int is_page_fault(u32 intr_info
)
192 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
193 INTR_INFO_VALID_MASK
)) ==
194 (INTR_TYPE_HARD_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
197 static inline int is_no_device(u32 intr_info
)
199 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
200 INTR_INFO_VALID_MASK
)) ==
201 (INTR_TYPE_HARD_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
204 static inline int is_invalid_opcode(u32 intr_info
)
206 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
207 INTR_INFO_VALID_MASK
)) ==
208 (INTR_TYPE_HARD_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
211 static inline int is_external_interrupt(u32 intr_info
)
213 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
214 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
217 static inline int cpu_has_vmx_msr_bitmap(void)
219 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
);
222 static inline int cpu_has_vmx_tpr_shadow(void)
224 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
);
227 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
229 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
)));
232 static inline int cpu_has_secondary_exec_ctrls(void)
234 return (vmcs_config
.cpu_based_exec_ctrl
&
235 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
);
238 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
240 return flexpriority_enabled
241 && (vmcs_config
.cpu_based_2nd_exec_ctrl
&
242 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
245 static inline int cpu_has_vmx_invept_individual_addr(void)
247 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
));
250 static inline int cpu_has_vmx_invept_context(void)
252 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
));
255 static inline int cpu_has_vmx_invept_global(void)
257 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
));
260 static inline int cpu_has_vmx_ept(void)
262 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
263 SECONDARY_EXEC_ENABLE_EPT
);
266 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
268 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
269 (irqchip_in_kernel(kvm
)));
272 static inline int cpu_has_vmx_vpid(void)
274 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
275 SECONDARY_EXEC_ENABLE_VPID
);
278 static inline int cpu_has_virtual_nmis(void)
280 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
283 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
287 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
288 if (vmx
->guest_msrs
[i
].index
== msr
)
293 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
299 } operand
= { vpid
, 0, gva
};
301 asm volatile (__ex(ASM_VMX_INVVPID
)
302 /* CF==1 or ZF==1 --> rc = -1 */
304 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
307 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
311 } operand
= {eptp
, gpa
};
313 asm volatile (__ex(ASM_VMX_INVEPT
)
314 /* CF==1 or ZF==1 --> rc = -1 */
315 "; ja 1f ; ud2 ; 1:\n"
316 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
319 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
323 i
= __find_msr_index(vmx
, msr
);
325 return &vmx
->guest_msrs
[i
];
329 static void vmcs_clear(struct vmcs
*vmcs
)
331 u64 phys_addr
= __pa(vmcs
);
334 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
335 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
338 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
342 static void __vcpu_clear(void *arg
)
344 struct vcpu_vmx
*vmx
= arg
;
345 int cpu
= raw_smp_processor_id();
347 if (vmx
->vcpu
.cpu
== cpu
)
348 vmcs_clear(vmx
->vmcs
);
349 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
350 per_cpu(current_vmcs
, cpu
) = NULL
;
351 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
352 list_del(&vmx
->local_vcpus_link
);
357 static void vcpu_clear(struct vcpu_vmx
*vmx
)
359 if (vmx
->vcpu
.cpu
== -1)
361 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 1);
364 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
369 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
372 static inline void ept_sync_global(void)
374 if (cpu_has_vmx_invept_global())
375 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
378 static inline void ept_sync_context(u64 eptp
)
381 if (cpu_has_vmx_invept_context())
382 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
388 static inline void ept_sync_individual_addr(u64 eptp
, gpa_t gpa
)
391 if (cpu_has_vmx_invept_individual_addr())
392 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR
,
395 ept_sync_context(eptp
);
399 static unsigned long vmcs_readl(unsigned long field
)
403 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX
)
404 : "=a"(value
) : "d"(field
) : "cc");
408 static u16
vmcs_read16(unsigned long field
)
410 return vmcs_readl(field
);
413 static u32
vmcs_read32(unsigned long field
)
415 return vmcs_readl(field
);
418 static u64
vmcs_read64(unsigned long field
)
421 return vmcs_readl(field
);
423 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
427 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
429 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
430 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
434 static void vmcs_writel(unsigned long field
, unsigned long value
)
438 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
439 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
441 vmwrite_error(field
, value
);
444 static void vmcs_write16(unsigned long field
, u16 value
)
446 vmcs_writel(field
, value
);
449 static void vmcs_write32(unsigned long field
, u32 value
)
451 vmcs_writel(field
, value
);
454 static void vmcs_write64(unsigned long field
, u64 value
)
456 vmcs_writel(field
, value
);
457 #ifndef CONFIG_X86_64
459 vmcs_writel(field
+1, value
>> 32);
463 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
465 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
468 static void vmcs_set_bits(unsigned long field
, u32 mask
)
470 vmcs_writel(field
, vmcs_readl(field
) | mask
);
473 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
477 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
);
478 if (!vcpu
->fpu_active
)
479 eb
|= 1u << NM_VECTOR
;
480 if (vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
) {
481 if (vcpu
->guest_debug
&
482 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))
483 eb
|= 1u << DB_VECTOR
;
484 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_SW_BP
)
485 eb
|= 1u << BP_VECTOR
;
487 if (vcpu
->arch
.rmode
.active
)
490 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
491 vmcs_write32(EXCEPTION_BITMAP
, eb
);
494 static void reload_tss(void)
497 * VT restores TR but not its size. Useless.
499 struct descriptor_table gdt
;
500 struct desc_struct
*descs
;
503 descs
= (void *)gdt
.base
;
504 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
508 static void load_transition_efer(struct vcpu_vmx
*vmx
)
510 int efer_offset
= vmx
->msr_offset_efer
;
511 u64 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
512 u64 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
518 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
521 ignore_bits
= EFER_NX
| EFER_SCE
;
523 ignore_bits
|= EFER_LMA
| EFER_LME
;
524 /* SCE is meaningful only in long mode on Intel */
525 if (guest_efer
& EFER_LMA
)
526 ignore_bits
&= ~(u64
)EFER_SCE
;
528 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
531 vmx
->host_state
.guest_efer_loaded
= 1;
532 guest_efer
&= ~ignore_bits
;
533 guest_efer
|= host_efer
& ignore_bits
;
534 wrmsrl(MSR_EFER
, guest_efer
);
535 vmx
->vcpu
.stat
.efer_reload
++;
538 static void reload_host_efer(struct vcpu_vmx
*vmx
)
540 if (vmx
->host_state
.guest_efer_loaded
) {
541 vmx
->host_state
.guest_efer_loaded
= 0;
542 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
546 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
548 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
550 if (vmx
->host_state
.loaded
)
553 vmx
->host_state
.loaded
= 1;
555 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
556 * allow segment selectors with cpl > 0 or ti == 1.
558 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
559 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
560 vmx
->host_state
.fs_sel
= kvm_read_fs();
561 if (!(vmx
->host_state
.fs_sel
& 7)) {
562 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
563 vmx
->host_state
.fs_reload_needed
= 0;
565 vmcs_write16(HOST_FS_SELECTOR
, 0);
566 vmx
->host_state
.fs_reload_needed
= 1;
568 vmx
->host_state
.gs_sel
= kvm_read_gs();
569 if (!(vmx
->host_state
.gs_sel
& 7))
570 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
572 vmcs_write16(HOST_GS_SELECTOR
, 0);
573 vmx
->host_state
.gs_ldt_reload_needed
= 1;
577 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
578 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
580 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
581 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
585 if (is_long_mode(&vmx
->vcpu
))
586 save_msrs(vmx
->host_msrs
+
587 vmx
->msr_offset_kernel_gs_base
, 1);
590 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
591 load_transition_efer(vmx
);
594 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
598 if (!vmx
->host_state
.loaded
)
601 ++vmx
->vcpu
.stat
.host_state_reload
;
602 vmx
->host_state
.loaded
= 0;
603 if (vmx
->host_state
.fs_reload_needed
)
604 kvm_load_fs(vmx
->host_state
.fs_sel
);
605 if (vmx
->host_state
.gs_ldt_reload_needed
) {
606 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
608 * If we have to reload gs, we must take care to
609 * preserve our gs base.
611 local_irq_save(flags
);
612 kvm_load_gs(vmx
->host_state
.gs_sel
);
614 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
616 local_irq_restore(flags
);
619 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
620 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
621 reload_host_efer(vmx
);
624 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
627 __vmx_load_host_state(vmx
);
632 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
633 * vcpu mutex is already taken.
635 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
637 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
638 u64 phys_addr
= __pa(vmx
->vmcs
);
639 u64 tsc_this
, delta
, new_offset
;
641 if (vcpu
->cpu
!= cpu
) {
643 kvm_migrate_timers(vcpu
);
644 vpid_sync_vcpu_all(vmx
);
646 list_add(&vmx
->local_vcpus_link
,
647 &per_cpu(vcpus_on_cpu
, cpu
));
651 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
654 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
655 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
656 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
659 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
660 vmx
->vmcs
, phys_addr
);
663 if (vcpu
->cpu
!= cpu
) {
664 struct descriptor_table dt
;
665 unsigned long sysenter_esp
;
669 * Linux uses per-cpu TSS and GDT, so set these when switching
672 vmcs_writel(HOST_TR_BASE
, kvm_read_tr_base()); /* 22.2.4 */
674 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
676 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
677 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
680 * Make sure the time stamp counter is monotonous.
683 if (tsc_this
< vcpu
->arch
.host_tsc
) {
684 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
685 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
686 vmcs_write64(TSC_OFFSET
, new_offset
);
691 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
693 __vmx_load_host_state(to_vmx(vcpu
));
696 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
698 if (vcpu
->fpu_active
)
700 vcpu
->fpu_active
= 1;
701 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
702 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
703 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
704 update_exception_bitmap(vcpu
);
707 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
709 if (!vcpu
->fpu_active
)
711 vcpu
->fpu_active
= 0;
712 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
713 update_exception_bitmap(vcpu
);
716 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
718 return vmcs_readl(GUEST_RFLAGS
);
721 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
723 if (vcpu
->arch
.rmode
.active
)
724 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
725 vmcs_writel(GUEST_RFLAGS
, rflags
);
728 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
731 u32 interruptibility
;
733 rip
= kvm_rip_read(vcpu
);
734 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
735 kvm_rip_write(vcpu
, rip
);
738 * We emulated an instruction, so temporary interrupt blocking
739 * should be removed, if set.
741 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
742 if (interruptibility
& 3)
743 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
744 interruptibility
& ~3);
745 vcpu
->arch
.interrupt_window_open
= 1;
748 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
749 bool has_error_code
, u32 error_code
)
751 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
752 u32 intr_info
= nr
| INTR_INFO_VALID_MASK
;
754 if (has_error_code
) {
755 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
756 intr_info
|= INTR_INFO_DELIVER_CODE_MASK
;
759 if (vcpu
->arch
.rmode
.active
) {
760 vmx
->rmode
.irq
.pending
= true;
761 vmx
->rmode
.irq
.vector
= nr
;
762 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
763 if (nr
== BP_VECTOR
|| nr
== OF_VECTOR
)
764 vmx
->rmode
.irq
.rip
++;
765 intr_info
|= INTR_TYPE_SOFT_INTR
;
766 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
767 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
768 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
772 if (nr
== BP_VECTOR
|| nr
== OF_VECTOR
) {
773 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
774 intr_info
|= INTR_TYPE_SOFT_EXCEPTION
;
776 intr_info
|= INTR_TYPE_HARD_EXCEPTION
;
778 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, intr_info
);
781 static bool vmx_exception_injected(struct kvm_vcpu
*vcpu
)
787 * Swap MSR entry in host/guest MSR entry array.
790 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
792 struct kvm_msr_entry tmp
;
794 tmp
= vmx
->guest_msrs
[to
];
795 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
796 vmx
->guest_msrs
[from
] = tmp
;
797 tmp
= vmx
->host_msrs
[to
];
798 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
799 vmx
->host_msrs
[from
] = tmp
;
804 * Set up the vmcs to automatically save and restore system
805 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
806 * mode, as fiddling with msrs is very expensive.
808 static void setup_msrs(struct vcpu_vmx
*vmx
)
811 unsigned long *msr_bitmap
;
813 vmx_load_host_state(vmx
);
816 if (is_long_mode(&vmx
->vcpu
)) {
819 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
821 move_msr_up(vmx
, index
, save_nmsrs
++);
822 index
= __find_msr_index(vmx
, MSR_LSTAR
);
824 move_msr_up(vmx
, index
, save_nmsrs
++);
825 index
= __find_msr_index(vmx
, MSR_CSTAR
);
827 move_msr_up(vmx
, index
, save_nmsrs
++);
828 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
830 move_msr_up(vmx
, index
, save_nmsrs
++);
832 * MSR_K6_STAR is only needed on long mode guests, and only
833 * if efer.sce is enabled.
835 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
836 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
837 move_msr_up(vmx
, index
, save_nmsrs
++);
840 vmx
->save_nmsrs
= save_nmsrs
;
843 vmx
->msr_offset_kernel_gs_base
=
844 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
846 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
848 if (cpu_has_vmx_msr_bitmap()) {
849 if (is_long_mode(&vmx
->vcpu
))
850 msr_bitmap
= vmx_msr_bitmap_longmode
;
852 msr_bitmap
= vmx_msr_bitmap_legacy
;
854 vmcs_write64(MSR_BITMAP
, __pa(msr_bitmap
));
859 * reads and returns guest's timestamp counter "register"
860 * guest_tsc = host_tsc + tsc_offset -- 21.3
862 static u64
guest_read_tsc(void)
864 u64 host_tsc
, tsc_offset
;
867 tsc_offset
= vmcs_read64(TSC_OFFSET
);
868 return host_tsc
+ tsc_offset
;
872 * writes 'guest_tsc' into guest's timestamp counter "register"
873 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
875 static void guest_write_tsc(u64 guest_tsc
, u64 host_tsc
)
877 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
881 * Reads an msr value (of 'msr_index') into 'pdata'.
882 * Returns 0 on success, non-0 otherwise.
883 * Assumes vcpu_load() was already called.
885 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
888 struct kvm_msr_entry
*msr
;
891 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
898 data
= vmcs_readl(GUEST_FS_BASE
);
901 data
= vmcs_readl(GUEST_GS_BASE
);
904 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
906 case MSR_IA32_TIME_STAMP_COUNTER
:
907 data
= guest_read_tsc();
909 case MSR_IA32_SYSENTER_CS
:
910 data
= vmcs_read32(GUEST_SYSENTER_CS
);
912 case MSR_IA32_SYSENTER_EIP
:
913 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
915 case MSR_IA32_SYSENTER_ESP
:
916 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
919 vmx_load_host_state(to_vmx(vcpu
));
920 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
925 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
933 * Writes msr value into into the appropriate "register".
934 * Returns 0 on success, non-0 otherwise.
935 * Assumes vcpu_load() was already called.
937 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
939 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
940 struct kvm_msr_entry
*msr
;
946 vmx_load_host_state(vmx
);
947 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
951 vmcs_writel(GUEST_FS_BASE
, data
);
954 vmcs_writel(GUEST_GS_BASE
, data
);
957 case MSR_IA32_SYSENTER_CS
:
958 vmcs_write32(GUEST_SYSENTER_CS
, data
);
960 case MSR_IA32_SYSENTER_EIP
:
961 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
963 case MSR_IA32_SYSENTER_ESP
:
964 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
966 case MSR_IA32_TIME_STAMP_COUNTER
:
968 guest_write_tsc(data
, host_tsc
);
970 case MSR_P6_PERFCTR0
:
971 case MSR_P6_PERFCTR1
:
972 case MSR_P6_EVNTSEL0
:
973 case MSR_P6_EVNTSEL1
:
975 * Just discard all writes to the performance counters; this
976 * should keep both older linux and windows 64-bit guests
979 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index
, data
);
982 case MSR_IA32_CR_PAT
:
983 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
984 vmcs_write64(GUEST_IA32_PAT
, data
);
985 vcpu
->arch
.pat
= data
;
988 /* Otherwise falls through to kvm_set_msr_common */
990 vmx_load_host_state(vmx
);
991 msr
= find_msr_entry(vmx
, msr_index
);
996 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
1002 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
1004 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
1007 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
1010 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
1017 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_guest_debug
*dbg
)
1019 int old_debug
= vcpu
->guest_debug
;
1020 unsigned long flags
;
1022 vcpu
->guest_debug
= dbg
->control
;
1023 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_ENABLE
))
1024 vcpu
->guest_debug
= 0;
1026 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)
1027 vmcs_writel(GUEST_DR7
, dbg
->arch
.debugreg
[7]);
1029 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
1031 flags
= vmcs_readl(GUEST_RFLAGS
);
1032 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
1033 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
1034 else if (old_debug
& KVM_GUESTDBG_SINGLESTEP
)
1035 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
1036 vmcs_writel(GUEST_RFLAGS
, flags
);
1038 update_exception_bitmap(vcpu
);
1043 static int vmx_get_irq(struct kvm_vcpu
*vcpu
)
1045 if (!vcpu
->arch
.interrupt
.pending
)
1047 return vcpu
->arch
.interrupt
.nr
;
1050 static __init
int cpu_has_kvm_support(void)
1052 return cpu_has_vmx();
1055 static __init
int vmx_disabled_by_bios(void)
1059 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
1060 return (msr
& (FEATURE_CONTROL_LOCKED
|
1061 FEATURE_CONTROL_VMXON_ENABLED
))
1062 == FEATURE_CONTROL_LOCKED
;
1063 /* locked but not enabled */
1066 static void hardware_enable(void *garbage
)
1068 int cpu
= raw_smp_processor_id();
1069 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
1072 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu
, cpu
));
1073 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
1074 if ((old
& (FEATURE_CONTROL_LOCKED
|
1075 FEATURE_CONTROL_VMXON_ENABLED
))
1076 != (FEATURE_CONTROL_LOCKED
|
1077 FEATURE_CONTROL_VMXON_ENABLED
))
1078 /* enable and lock */
1079 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
1080 FEATURE_CONTROL_LOCKED
|
1081 FEATURE_CONTROL_VMXON_ENABLED
);
1082 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
1083 asm volatile (ASM_VMX_VMXON_RAX
1084 : : "a"(&phys_addr
), "m"(phys_addr
)
1088 static void vmclear_local_vcpus(void)
1090 int cpu
= raw_smp_processor_id();
1091 struct vcpu_vmx
*vmx
, *n
;
1093 list_for_each_entry_safe(vmx
, n
, &per_cpu(vcpus_on_cpu
, cpu
),
1099 /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1102 static void kvm_cpu_vmxoff(void)
1104 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
1105 write_cr4(read_cr4() & ~X86_CR4_VMXE
);
1108 static void hardware_disable(void *garbage
)
1110 vmclear_local_vcpus();
1114 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1115 u32 msr
, u32
*result
)
1117 u32 vmx_msr_low
, vmx_msr_high
;
1118 u32 ctl
= ctl_min
| ctl_opt
;
1120 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1122 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1123 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1125 /* Ensure minimum (required) set of control bits are supported. */
1133 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1135 u32 vmx_msr_low
, vmx_msr_high
;
1136 u32 min
, opt
, min2
, opt2
;
1137 u32 _pin_based_exec_control
= 0;
1138 u32 _cpu_based_exec_control
= 0;
1139 u32 _cpu_based_2nd_exec_control
= 0;
1140 u32 _vmexit_control
= 0;
1141 u32 _vmentry_control
= 0;
1143 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1144 opt
= PIN_BASED_VIRTUAL_NMIS
;
1145 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1146 &_pin_based_exec_control
) < 0)
1149 min
= CPU_BASED_HLT_EXITING
|
1150 #ifdef CONFIG_X86_64
1151 CPU_BASED_CR8_LOAD_EXITING
|
1152 CPU_BASED_CR8_STORE_EXITING
|
1154 CPU_BASED_CR3_LOAD_EXITING
|
1155 CPU_BASED_CR3_STORE_EXITING
|
1156 CPU_BASED_USE_IO_BITMAPS
|
1157 CPU_BASED_MOV_DR_EXITING
|
1158 CPU_BASED_USE_TSC_OFFSETING
|
1159 CPU_BASED_INVLPG_EXITING
;
1160 opt
= CPU_BASED_TPR_SHADOW
|
1161 CPU_BASED_USE_MSR_BITMAPS
|
1162 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1163 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1164 &_cpu_based_exec_control
) < 0)
1166 #ifdef CONFIG_X86_64
1167 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1168 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1169 ~CPU_BASED_CR8_STORE_EXITING
;
1171 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1173 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1174 SECONDARY_EXEC_WBINVD_EXITING
|
1175 SECONDARY_EXEC_ENABLE_VPID
|
1176 SECONDARY_EXEC_ENABLE_EPT
;
1177 if (adjust_vmx_controls(min2
, opt2
,
1178 MSR_IA32_VMX_PROCBASED_CTLS2
,
1179 &_cpu_based_2nd_exec_control
) < 0)
1182 #ifndef CONFIG_X86_64
1183 if (!(_cpu_based_2nd_exec_control
&
1184 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1185 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1187 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1188 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1190 min
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1191 CPU_BASED_CR3_STORE_EXITING
|
1192 CPU_BASED_INVLPG_EXITING
);
1193 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1194 &_cpu_based_exec_control
) < 0)
1196 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1197 vmx_capability
.ept
, vmx_capability
.vpid
);
1200 if (!cpu_has_vmx_vpid())
1203 if (!cpu_has_vmx_ept())
1207 #ifdef CONFIG_X86_64
1208 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1210 opt
= VM_EXIT_SAVE_IA32_PAT
| VM_EXIT_LOAD_IA32_PAT
;
1211 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1212 &_vmexit_control
) < 0)
1216 opt
= VM_ENTRY_LOAD_IA32_PAT
;
1217 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1218 &_vmentry_control
) < 0)
1221 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1223 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1224 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1227 #ifdef CONFIG_X86_64
1228 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1229 if (vmx_msr_high
& (1u<<16))
1233 /* Require Write-Back (WB) memory type for VMCS accesses. */
1234 if (((vmx_msr_high
>> 18) & 15) != 6)
1237 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1238 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1239 vmcs_conf
->revision_id
= vmx_msr_low
;
1241 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1242 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1243 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1244 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1245 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1250 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1252 int node
= cpu_to_node(cpu
);
1256 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1259 vmcs
= page_address(pages
);
1260 memset(vmcs
, 0, vmcs_config
.size
);
1261 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1265 static struct vmcs
*alloc_vmcs(void)
1267 return alloc_vmcs_cpu(raw_smp_processor_id());
1270 static void free_vmcs(struct vmcs
*vmcs
)
1272 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1275 static void free_kvm_area(void)
1279 for_each_online_cpu(cpu
)
1280 free_vmcs(per_cpu(vmxarea
, cpu
));
1283 static __init
int alloc_kvm_area(void)
1287 for_each_online_cpu(cpu
) {
1290 vmcs
= alloc_vmcs_cpu(cpu
);
1296 per_cpu(vmxarea
, cpu
) = vmcs
;
1301 static __init
int hardware_setup(void)
1303 if (setup_vmcs_config(&vmcs_config
) < 0)
1306 if (boot_cpu_has(X86_FEATURE_NX
))
1307 kvm_enable_efer_bits(EFER_NX
);
1309 return alloc_kvm_area();
1312 static __exit
void hardware_unsetup(void)
1317 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1319 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1321 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1322 vmcs_write16(sf
->selector
, save
->selector
);
1323 vmcs_writel(sf
->base
, save
->base
);
1324 vmcs_write32(sf
->limit
, save
->limit
);
1325 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1327 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1329 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1333 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1335 unsigned long flags
;
1336 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1338 vmx
->emulation_required
= 1;
1339 vcpu
->arch
.rmode
.active
= 0;
1341 vmcs_writel(GUEST_TR_BASE
, vcpu
->arch
.rmode
.tr
.base
);
1342 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->arch
.rmode
.tr
.limit
);
1343 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->arch
.rmode
.tr
.ar
);
1345 flags
= vmcs_readl(GUEST_RFLAGS
);
1346 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1347 flags
|= (vcpu
->arch
.rmode
.save_iopl
<< IOPL_SHIFT
);
1348 vmcs_writel(GUEST_RFLAGS
, flags
);
1350 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1351 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1353 update_exception_bitmap(vcpu
);
1355 if (emulate_invalid_guest_state
)
1358 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1359 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1360 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1361 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1363 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1364 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1366 vmcs_write16(GUEST_CS_SELECTOR
,
1367 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1368 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1371 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1373 if (!kvm
->arch
.tss_addr
) {
1374 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1375 kvm
->memslots
[0].npages
- 3;
1376 return base_gfn
<< PAGE_SHIFT
;
1378 return kvm
->arch
.tss_addr
;
1381 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1383 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1385 save
->selector
= vmcs_read16(sf
->selector
);
1386 save
->base
= vmcs_readl(sf
->base
);
1387 save
->limit
= vmcs_read32(sf
->limit
);
1388 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1389 vmcs_write16(sf
->selector
, save
->base
>> 4);
1390 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1391 vmcs_write32(sf
->limit
, 0xffff);
1392 vmcs_write32(sf
->ar_bytes
, 0xf3);
1395 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1397 unsigned long flags
;
1398 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1400 vmx
->emulation_required
= 1;
1401 vcpu
->arch
.rmode
.active
= 1;
1403 vcpu
->arch
.rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1404 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1406 vcpu
->arch
.rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1407 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1409 vcpu
->arch
.rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1410 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1412 flags
= vmcs_readl(GUEST_RFLAGS
);
1413 vcpu
->arch
.rmode
.save_iopl
1414 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1416 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1418 vmcs_writel(GUEST_RFLAGS
, flags
);
1419 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1420 update_exception_bitmap(vcpu
);
1422 if (emulate_invalid_guest_state
)
1423 goto continue_rmode
;
1425 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1426 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1427 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1429 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1430 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1431 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1432 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1433 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1435 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1436 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1437 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1438 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1441 kvm_mmu_reset_context(vcpu
);
1442 init_rmode(vcpu
->kvm
);
1445 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1447 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1448 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1450 vcpu
->arch
.shadow_efer
= efer
;
1453 if (efer
& EFER_LMA
) {
1454 vmcs_write32(VM_ENTRY_CONTROLS
,
1455 vmcs_read32(VM_ENTRY_CONTROLS
) |
1456 VM_ENTRY_IA32E_MODE
);
1459 vmcs_write32(VM_ENTRY_CONTROLS
,
1460 vmcs_read32(VM_ENTRY_CONTROLS
) &
1461 ~VM_ENTRY_IA32E_MODE
);
1463 msr
->data
= efer
& ~EFER_LME
;
1468 #ifdef CONFIG_X86_64
1470 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1474 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1475 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1476 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1478 vmcs_write32(GUEST_TR_AR_BYTES
,
1479 (guest_tr_ar
& ~AR_TYPE_MASK
)
1480 | AR_TYPE_BUSY_64_TSS
);
1482 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1483 vmx_set_efer(vcpu
, vcpu
->arch
.shadow_efer
);
1486 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1488 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1490 vmcs_write32(VM_ENTRY_CONTROLS
,
1491 vmcs_read32(VM_ENTRY_CONTROLS
)
1492 & ~VM_ENTRY_IA32E_MODE
);
1497 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1499 vpid_sync_vcpu_all(to_vmx(vcpu
));
1501 ept_sync_context(construct_eptp(vcpu
->arch
.mmu
.root_hpa
));
1504 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1506 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1507 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1510 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
1512 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1513 if (!load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
1514 printk(KERN_ERR
"EPT: Fail to load pdptrs!\n");
1517 vmcs_write64(GUEST_PDPTR0
, vcpu
->arch
.pdptrs
[0]);
1518 vmcs_write64(GUEST_PDPTR1
, vcpu
->arch
.pdptrs
[1]);
1519 vmcs_write64(GUEST_PDPTR2
, vcpu
->arch
.pdptrs
[2]);
1520 vmcs_write64(GUEST_PDPTR3
, vcpu
->arch
.pdptrs
[3]);
1524 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
1526 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
1528 struct kvm_vcpu
*vcpu
)
1530 if (!(cr0
& X86_CR0_PG
)) {
1531 /* From paging/starting to nonpaging */
1532 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1533 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
1534 (CPU_BASED_CR3_LOAD_EXITING
|
1535 CPU_BASED_CR3_STORE_EXITING
));
1536 vcpu
->arch
.cr0
= cr0
;
1537 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1538 *hw_cr0
|= X86_CR0_PE
| X86_CR0_PG
;
1539 *hw_cr0
&= ~X86_CR0_WP
;
1540 } else if (!is_paging(vcpu
)) {
1541 /* From nonpaging to paging */
1542 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1543 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
1544 ~(CPU_BASED_CR3_LOAD_EXITING
|
1545 CPU_BASED_CR3_STORE_EXITING
));
1546 vcpu
->arch
.cr0
= cr0
;
1547 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1548 if (!(vcpu
->arch
.cr0
& X86_CR0_WP
))
1549 *hw_cr0
&= ~X86_CR0_WP
;
1553 static void ept_update_paging_mode_cr4(unsigned long *hw_cr4
,
1554 struct kvm_vcpu
*vcpu
)
1556 if (!is_paging(vcpu
)) {
1557 *hw_cr4
&= ~X86_CR4_PAE
;
1558 *hw_cr4
|= X86_CR4_PSE
;
1559 } else if (!(vcpu
->arch
.cr4
& X86_CR4_PAE
))
1560 *hw_cr4
&= ~X86_CR4_PAE
;
1563 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1565 unsigned long hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
) |
1566 KVM_VM_CR0_ALWAYS_ON
;
1568 vmx_fpu_deactivate(vcpu
);
1570 if (vcpu
->arch
.rmode
.active
&& (cr0
& X86_CR0_PE
))
1573 if (!vcpu
->arch
.rmode
.active
&& !(cr0
& X86_CR0_PE
))
1576 #ifdef CONFIG_X86_64
1577 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1578 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1580 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1586 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
1588 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1589 vmcs_writel(GUEST_CR0
, hw_cr0
);
1590 vcpu
->arch
.cr0
= cr0
;
1592 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1593 vmx_fpu_activate(vcpu
);
1596 static u64
construct_eptp(unsigned long root_hpa
)
1600 /* TODO write the value reading from MSR */
1601 eptp
= VMX_EPT_DEFAULT_MT
|
1602 VMX_EPT_DEFAULT_GAW
<< VMX_EPT_GAW_EPTP_SHIFT
;
1603 eptp
|= (root_hpa
& PAGE_MASK
);
1608 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1610 unsigned long guest_cr3
;
1615 eptp
= construct_eptp(cr3
);
1616 vmcs_write64(EPT_POINTER
, eptp
);
1617 ept_sync_context(eptp
);
1618 ept_load_pdptrs(vcpu
);
1619 guest_cr3
= is_paging(vcpu
) ? vcpu
->arch
.cr3
:
1620 VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
1623 vmx_flush_tlb(vcpu
);
1624 vmcs_writel(GUEST_CR3
, guest_cr3
);
1625 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1626 vmx_fpu_deactivate(vcpu
);
1629 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1631 unsigned long hw_cr4
= cr4
| (vcpu
->arch
.rmode
.active
?
1632 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
1634 vcpu
->arch
.cr4
= cr4
;
1636 ept_update_paging_mode_cr4(&hw_cr4
, vcpu
);
1638 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1639 vmcs_writel(GUEST_CR4
, hw_cr4
);
1642 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1644 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1646 return vmcs_readl(sf
->base
);
1649 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1650 struct kvm_segment
*var
, int seg
)
1652 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1655 var
->base
= vmcs_readl(sf
->base
);
1656 var
->limit
= vmcs_read32(sf
->limit
);
1657 var
->selector
= vmcs_read16(sf
->selector
);
1658 ar
= vmcs_read32(sf
->ar_bytes
);
1659 if ((ar
& AR_UNUSABLE_MASK
) && !emulate_invalid_guest_state
)
1661 var
->type
= ar
& 15;
1662 var
->s
= (ar
>> 4) & 1;
1663 var
->dpl
= (ar
>> 5) & 3;
1664 var
->present
= (ar
>> 7) & 1;
1665 var
->avl
= (ar
>> 12) & 1;
1666 var
->l
= (ar
>> 13) & 1;
1667 var
->db
= (ar
>> 14) & 1;
1668 var
->g
= (ar
>> 15) & 1;
1669 var
->unusable
= (ar
>> 16) & 1;
1672 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1674 struct kvm_segment kvm_seg
;
1676 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1679 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1682 vmx_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_CS
);
1683 return kvm_seg
.selector
& 3;
1686 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1693 ar
= var
->type
& 15;
1694 ar
|= (var
->s
& 1) << 4;
1695 ar
|= (var
->dpl
& 3) << 5;
1696 ar
|= (var
->present
& 1) << 7;
1697 ar
|= (var
->avl
& 1) << 12;
1698 ar
|= (var
->l
& 1) << 13;
1699 ar
|= (var
->db
& 1) << 14;
1700 ar
|= (var
->g
& 1) << 15;
1702 if (ar
== 0) /* a 0 value means unusable */
1703 ar
= AR_UNUSABLE_MASK
;
1708 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1709 struct kvm_segment
*var
, int seg
)
1711 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1714 if (vcpu
->arch
.rmode
.active
&& seg
== VCPU_SREG_TR
) {
1715 vcpu
->arch
.rmode
.tr
.selector
= var
->selector
;
1716 vcpu
->arch
.rmode
.tr
.base
= var
->base
;
1717 vcpu
->arch
.rmode
.tr
.limit
= var
->limit
;
1718 vcpu
->arch
.rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1721 vmcs_writel(sf
->base
, var
->base
);
1722 vmcs_write32(sf
->limit
, var
->limit
);
1723 vmcs_write16(sf
->selector
, var
->selector
);
1724 if (vcpu
->arch
.rmode
.active
&& var
->s
) {
1726 * Hack real-mode segments into vm86 compatibility.
1728 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1729 vmcs_writel(sf
->base
, 0xf0000);
1732 ar
= vmx_segment_access_rights(var
);
1733 vmcs_write32(sf
->ar_bytes
, ar
);
1736 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1738 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1740 *db
= (ar
>> 14) & 1;
1741 *l
= (ar
>> 13) & 1;
1744 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1746 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1747 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1750 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1752 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1753 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1756 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1758 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1759 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1762 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1764 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1765 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1768 static bool rmode_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1770 struct kvm_segment var
;
1773 vmx_get_segment(vcpu
, &var
, seg
);
1774 ar
= vmx_segment_access_rights(&var
);
1776 if (var
.base
!= (var
.selector
<< 4))
1778 if (var
.limit
!= 0xffff)
1786 static bool code_segment_valid(struct kvm_vcpu
*vcpu
)
1788 struct kvm_segment cs
;
1789 unsigned int cs_rpl
;
1791 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1792 cs_rpl
= cs
.selector
& SELECTOR_RPL_MASK
;
1796 if (~cs
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_ACCESSES_MASK
))
1800 if (cs
.type
& AR_TYPE_WRITEABLE_MASK
) {
1801 if (cs
.dpl
> cs_rpl
)
1804 if (cs
.dpl
!= cs_rpl
)
1810 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1814 static bool stack_segment_valid(struct kvm_vcpu
*vcpu
)
1816 struct kvm_segment ss
;
1817 unsigned int ss_rpl
;
1819 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1820 ss_rpl
= ss
.selector
& SELECTOR_RPL_MASK
;
1824 if (ss
.type
!= 3 && ss
.type
!= 7)
1828 if (ss
.dpl
!= ss_rpl
) /* DPL != RPL */
1836 static bool data_segment_valid(struct kvm_vcpu
*vcpu
, int seg
)
1838 struct kvm_segment var
;
1841 vmx_get_segment(vcpu
, &var
, seg
);
1842 rpl
= var
.selector
& SELECTOR_RPL_MASK
;
1850 if (~var
.type
& (AR_TYPE_CODE_MASK
|AR_TYPE_WRITEABLE_MASK
)) {
1851 if (var
.dpl
< rpl
) /* DPL < RPL */
1855 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1861 static bool tr_valid(struct kvm_vcpu
*vcpu
)
1863 struct kvm_segment tr
;
1865 vmx_get_segment(vcpu
, &tr
, VCPU_SREG_TR
);
1869 if (tr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
1871 if (tr
.type
!= 3 && tr
.type
!= 11) /* TODO: Check if guest is in IA32e mode */
1879 static bool ldtr_valid(struct kvm_vcpu
*vcpu
)
1881 struct kvm_segment ldtr
;
1883 vmx_get_segment(vcpu
, &ldtr
, VCPU_SREG_LDTR
);
1887 if (ldtr
.selector
& SELECTOR_TI_MASK
) /* TI = 1 */
1897 static bool cs_ss_rpl_check(struct kvm_vcpu
*vcpu
)
1899 struct kvm_segment cs
, ss
;
1901 vmx_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
1902 vmx_get_segment(vcpu
, &ss
, VCPU_SREG_SS
);
1904 return ((cs
.selector
& SELECTOR_RPL_MASK
) ==
1905 (ss
.selector
& SELECTOR_RPL_MASK
));
1909 * Check if guest state is valid. Returns true if valid, false if
1911 * We assume that registers are always usable
1913 static bool guest_state_valid(struct kvm_vcpu
*vcpu
)
1915 /* real mode guest state checks */
1916 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) {
1917 if (!rmode_segment_valid(vcpu
, VCPU_SREG_CS
))
1919 if (!rmode_segment_valid(vcpu
, VCPU_SREG_SS
))
1921 if (!rmode_segment_valid(vcpu
, VCPU_SREG_DS
))
1923 if (!rmode_segment_valid(vcpu
, VCPU_SREG_ES
))
1925 if (!rmode_segment_valid(vcpu
, VCPU_SREG_FS
))
1927 if (!rmode_segment_valid(vcpu
, VCPU_SREG_GS
))
1930 /* protected mode guest state checks */
1931 if (!cs_ss_rpl_check(vcpu
))
1933 if (!code_segment_valid(vcpu
))
1935 if (!stack_segment_valid(vcpu
))
1937 if (!data_segment_valid(vcpu
, VCPU_SREG_DS
))
1939 if (!data_segment_valid(vcpu
, VCPU_SREG_ES
))
1941 if (!data_segment_valid(vcpu
, VCPU_SREG_FS
))
1943 if (!data_segment_valid(vcpu
, VCPU_SREG_GS
))
1945 if (!tr_valid(vcpu
))
1947 if (!ldtr_valid(vcpu
))
1951 * - Add checks on RIP
1952 * - Add checks on RFLAGS
1958 static int init_rmode_tss(struct kvm
*kvm
)
1960 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
1965 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1968 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
1969 r
= kvm_write_guest_page(kvm
, fn
++, &data
,
1970 TSS_IOPB_BASE_OFFSET
, sizeof(u16
));
1973 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
1976 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1980 r
= kvm_write_guest_page(kvm
, fn
, &data
,
1981 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
1991 static int init_rmode_identity_map(struct kvm
*kvm
)
1994 pfn_t identity_map_pfn
;
1999 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
2000 printk(KERN_ERR
"EPT: identity-mapping pagetable "
2001 "haven't been allocated!\n");
2004 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
2007 identity_map_pfn
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
;
2008 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
2011 /* Set up identity-mapping pagetable for EPT in real mode */
2012 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
2013 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
2014 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
2015 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
2016 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
2020 kvm
->arch
.ept_identity_pagetable_done
= true;
2026 static void seg_setup(int seg
)
2028 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
2030 vmcs_write16(sf
->selector
, 0);
2031 vmcs_writel(sf
->base
, 0);
2032 vmcs_write32(sf
->limit
, 0xffff);
2033 vmcs_write32(sf
->ar_bytes
, 0xf3);
2036 static int alloc_apic_access_page(struct kvm
*kvm
)
2038 struct kvm_userspace_memory_region kvm_userspace_mem
;
2041 down_write(&kvm
->slots_lock
);
2042 if (kvm
->arch
.apic_access_page
)
2044 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
2045 kvm_userspace_mem
.flags
= 0;
2046 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
2047 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2048 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2052 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
2054 up_write(&kvm
->slots_lock
);
2058 static int alloc_identity_pagetable(struct kvm
*kvm
)
2060 struct kvm_userspace_memory_region kvm_userspace_mem
;
2063 down_write(&kvm
->slots_lock
);
2064 if (kvm
->arch
.ept_identity_pagetable
)
2066 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
2067 kvm_userspace_mem
.flags
= 0;
2068 kvm_userspace_mem
.guest_phys_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
2069 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
2070 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
2074 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
2075 VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
);
2077 up_write(&kvm
->slots_lock
);
2081 static void allocate_vpid(struct vcpu_vmx
*vmx
)
2088 spin_lock(&vmx_vpid_lock
);
2089 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
2090 if (vpid
< VMX_NR_VPIDS
) {
2092 __set_bit(vpid
, vmx_vpid_bitmap
);
2094 spin_unlock(&vmx_vpid_lock
);
2097 static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap
, u32 msr
)
2099 int f
= sizeof(unsigned long);
2101 if (!cpu_has_vmx_msr_bitmap())
2105 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2106 * have the write-low and read-high bitmap offsets the wrong way round.
2107 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2109 if (msr
<= 0x1fff) {
2110 __clear_bit(msr
, msr_bitmap
+ 0x000 / f
); /* read-low */
2111 __clear_bit(msr
, msr_bitmap
+ 0x800 / f
); /* write-low */
2112 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
2114 __clear_bit(msr
, msr_bitmap
+ 0x400 / f
); /* read-high */
2115 __clear_bit(msr
, msr_bitmap
+ 0xc00 / f
); /* write-high */
2119 static void vmx_disable_intercept_for_msr(u32 msr
, bool longmode_only
)
2122 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy
, msr
);
2123 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode
, msr
);
2127 * Sets up the vmcs for emulated real mode.
2129 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
2131 u32 host_sysenter_cs
, msr_low
, msr_high
;
2133 u64 host_pat
, tsc_this
, tsc_base
;
2135 struct descriptor_table dt
;
2137 unsigned long kvm_vmx_return
;
2141 vmcs_write64(IO_BITMAP_A
, __pa(vmx_io_bitmap_a
));
2142 vmcs_write64(IO_BITMAP_B
, __pa(vmx_io_bitmap_b
));
2144 if (cpu_has_vmx_msr_bitmap())
2145 vmcs_write64(MSR_BITMAP
, __pa(vmx_msr_bitmap_legacy
));
2147 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
2150 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
2151 vmcs_config
.pin_based_exec_ctrl
);
2153 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
2154 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
2155 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
2156 #ifdef CONFIG_X86_64
2157 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
2158 CPU_BASED_CR8_LOAD_EXITING
;
2162 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
2163 CPU_BASED_CR3_LOAD_EXITING
|
2164 CPU_BASED_INVLPG_EXITING
;
2165 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
2167 if (cpu_has_secondary_exec_ctrls()) {
2168 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
2169 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2171 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
2173 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
2175 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
2176 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
2179 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
2180 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
2181 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
2183 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
2184 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
2185 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2187 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
2188 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2189 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2190 vmcs_write16(HOST_FS_SELECTOR
, kvm_read_fs()); /* 22.2.4 */
2191 vmcs_write16(HOST_GS_SELECTOR
, kvm_read_gs()); /* 22.2.4 */
2192 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
2193 #ifdef CONFIG_X86_64
2194 rdmsrl(MSR_FS_BASE
, a
);
2195 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
2196 rdmsrl(MSR_GS_BASE
, a
);
2197 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
2199 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
2200 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
2203 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
2206 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
2208 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
2209 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
2210 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
2211 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
2212 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
2214 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
2215 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
2216 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
2217 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
2218 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
2219 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
2221 if (vmcs_config
.vmexit_ctrl
& VM_EXIT_LOAD_IA32_PAT
) {
2222 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2223 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2224 vmcs_write64(HOST_IA32_PAT
, host_pat
);
2226 if (vmcs_config
.vmentry_ctrl
& VM_ENTRY_LOAD_IA32_PAT
) {
2227 rdmsr(MSR_IA32_CR_PAT
, msr_low
, msr_high
);
2228 host_pat
= msr_low
| ((u64
) msr_high
<< 32);
2229 /* Write the default value follow host pat */
2230 vmcs_write64(GUEST_IA32_PAT
, host_pat
);
2231 /* Keep arch.pat sync with GUEST_IA32_PAT */
2232 vmx
->vcpu
.arch
.pat
= host_pat
;
2235 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
2236 u32 index
= vmx_msr_index
[i
];
2237 u32 data_low
, data_high
;
2241 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
2243 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
2245 data
= data_low
| ((u64
)data_high
<< 32);
2246 vmx
->host_msrs
[j
].index
= index
;
2247 vmx
->host_msrs
[j
].reserved
= 0;
2248 vmx
->host_msrs
[j
].data
= data
;
2249 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
2253 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
2255 /* 22.2.1, 20.8.1 */
2256 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
2258 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
2259 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
2261 tsc_base
= vmx
->vcpu
.kvm
->arch
.vm_init_tsc
;
2263 if (tsc_this
< vmx
->vcpu
.kvm
->arch
.vm_init_tsc
)
2264 tsc_base
= tsc_this
;
2266 guest_write_tsc(0, tsc_base
);
2271 static int init_rmode(struct kvm
*kvm
)
2273 if (!init_rmode_tss(kvm
))
2275 if (!init_rmode_identity_map(kvm
))
2280 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
2282 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2286 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
2287 down_read(&vcpu
->kvm
->slots_lock
);
2288 if (!init_rmode(vmx
->vcpu
.kvm
)) {
2293 vmx
->vcpu
.arch
.rmode
.active
= 0;
2295 vmx
->soft_vnmi_blocked
= 0;
2297 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
2298 kvm_set_cr8(&vmx
->vcpu
, 0);
2299 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
2300 if (vmx
->vcpu
.vcpu_id
== 0)
2301 msr
|= MSR_IA32_APICBASE_BSP
;
2302 kvm_set_apic_base(&vmx
->vcpu
, msr
);
2304 fx_init(&vmx
->vcpu
);
2306 seg_setup(VCPU_SREG_CS
);
2308 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2309 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2311 if (vmx
->vcpu
.vcpu_id
== 0) {
2312 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
2313 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
2315 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
2316 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
2319 seg_setup(VCPU_SREG_DS
);
2320 seg_setup(VCPU_SREG_ES
);
2321 seg_setup(VCPU_SREG_FS
);
2322 seg_setup(VCPU_SREG_GS
);
2323 seg_setup(VCPU_SREG_SS
);
2325 vmcs_write16(GUEST_TR_SELECTOR
, 0);
2326 vmcs_writel(GUEST_TR_BASE
, 0);
2327 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
2328 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
2330 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
2331 vmcs_writel(GUEST_LDTR_BASE
, 0);
2332 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
2333 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
2335 vmcs_write32(GUEST_SYSENTER_CS
, 0);
2336 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
2337 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
2339 vmcs_writel(GUEST_RFLAGS
, 0x02);
2340 if (vmx
->vcpu
.vcpu_id
== 0)
2341 kvm_rip_write(vcpu
, 0xfff0);
2343 kvm_rip_write(vcpu
, 0);
2344 kvm_register_write(vcpu
, VCPU_REGS_RSP
, 0);
2346 vmcs_writel(GUEST_DR7
, 0x400);
2348 vmcs_writel(GUEST_GDTR_BASE
, 0);
2349 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
2351 vmcs_writel(GUEST_IDTR_BASE
, 0);
2352 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
2354 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
2355 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
2356 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
2358 /* Special registers */
2359 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
2363 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
2365 if (cpu_has_vmx_tpr_shadow()) {
2366 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
2367 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
2368 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
2369 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
2370 vmcs_write32(TPR_THRESHOLD
, 0);
2373 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2374 vmcs_write64(APIC_ACCESS_ADDR
,
2375 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
2378 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
2380 vmx
->vcpu
.arch
.cr0
= 0x60000010;
2381 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
2382 vmx_set_cr4(&vmx
->vcpu
, 0);
2383 vmx_set_efer(&vmx
->vcpu
, 0);
2384 vmx_fpu_activate(&vmx
->vcpu
);
2385 update_exception_bitmap(&vmx
->vcpu
);
2387 vpid_sync_vcpu_all(vmx
);
2391 /* HACK: Don't enable emulation on guest boot/reset */
2392 vmx
->emulation_required
= 0;
2395 up_read(&vcpu
->kvm
->slots_lock
);
2399 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2401 u32 cpu_based_vm_exec_control
;
2403 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2404 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2405 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2408 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2410 u32 cpu_based_vm_exec_control
;
2412 if (!cpu_has_virtual_nmis()) {
2413 enable_irq_window(vcpu
);
2417 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2418 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_NMI_PENDING
;
2419 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2422 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
, int irq
)
2424 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2426 KVMTRACE_1D(INJ_VIRQ
, vcpu
, (u32
)irq
, handler
);
2428 ++vcpu
->stat
.irq_injections
;
2429 if (vcpu
->arch
.rmode
.active
) {
2430 vmx
->rmode
.irq
.pending
= true;
2431 vmx
->rmode
.irq
.vector
= irq
;
2432 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2433 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2434 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
2435 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2436 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2439 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2440 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
2443 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
2445 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2447 if (!cpu_has_virtual_nmis()) {
2449 * Tracking the NMI-blocked state in software is built upon
2450 * finding the next open IRQ window. This, in turn, depends on
2451 * well-behaving guests: They have to keep IRQs disabled at
2452 * least as long as the NMI handler runs. Otherwise we may
2453 * cause NMI nesting, maybe breaking the guest. But as this is
2454 * highly unlikely, we can live with the residual risk.
2456 vmx
->soft_vnmi_blocked
= 1;
2457 vmx
->vnmi_blocked_time
= 0;
2460 ++vcpu
->stat
.nmi_injections
;
2461 if (vcpu
->arch
.rmode
.active
) {
2462 vmx
->rmode
.irq
.pending
= true;
2463 vmx
->rmode
.irq
.vector
= NMI_VECTOR
;
2464 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2465 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2466 NMI_VECTOR
| INTR_TYPE_SOFT_INTR
|
2467 INTR_INFO_VALID_MASK
);
2468 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2469 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2472 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2473 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
2476 static void vmx_update_window_states(struct kvm_vcpu
*vcpu
)
2478 u32 guest_intr
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2480 vcpu
->arch
.nmi_window_open
=
2481 !(guest_intr
& (GUEST_INTR_STATE_STI
|
2482 GUEST_INTR_STATE_MOV_SS
|
2483 GUEST_INTR_STATE_NMI
));
2484 if (!cpu_has_virtual_nmis() && to_vmx(vcpu
)->soft_vnmi_blocked
)
2485 vcpu
->arch
.nmi_window_open
= 0;
2487 vcpu
->arch
.interrupt_window_open
=
2488 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2489 !(guest_intr
& (GUEST_INTR_STATE_STI
|
2490 GUEST_INTR_STATE_MOV_SS
)));
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_queue_interrupt(vcpu
, kvm_pop_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 kvm_push_irq(vcpu
, irq
);
2614 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
)
2615 return 1; /* already handled by vmx_vcpu_run() */
2617 if (is_no_device(intr_info
)) {
2618 vmx_fpu_activate(vcpu
);
2622 if (is_invalid_opcode(intr_info
)) {
2623 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
2624 if (er
!= EMULATE_DONE
)
2625 kvm_queue_exception(vcpu
, UD_VECTOR
);
2630 rip
= kvm_rip_read(vcpu
);
2631 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2632 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2633 if (is_page_fault(intr_info
)) {
2634 /* EPT won't cause page fault directly */
2637 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2638 KVMTRACE_3D(PAGE_FAULT
, vcpu
, error_code
, (u32
)cr2
,
2639 (u32
)((u64
)cr2
>> 32), handler
);
2640 if (vcpu
->arch
.interrupt
.pending
|| vcpu
->arch
.exception
.pending
)
2641 kvm_mmu_unprotect_page_virt(vcpu
, cr2
);
2642 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2645 if (vcpu
->arch
.rmode
.active
&&
2646 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2648 if (vcpu
->arch
.halt_request
) {
2649 vcpu
->arch
.halt_request
= 0;
2650 return kvm_emulate_halt(vcpu
);
2655 ex_no
= intr_info
& INTR_INFO_VECTOR_MASK
;
2658 dr6
= vmcs_readl(EXIT_QUALIFICATION
);
2659 if (!(vcpu
->guest_debug
&
2660 (KVM_GUESTDBG_SINGLESTEP
| KVM_GUESTDBG_USE_HW_BP
))) {
2661 vcpu
->arch
.dr6
= dr6
| DR6_FIXED_1
;
2662 kvm_queue_exception(vcpu
, DB_VECTOR
);
2665 kvm_run
->debug
.arch
.dr6
= dr6
| DR6_FIXED_1
;
2666 kvm_run
->debug
.arch
.dr7
= vmcs_readl(GUEST_DR7
);
2669 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2670 kvm_run
->debug
.arch
.pc
= vmcs_readl(GUEST_CS_BASE
) + rip
;
2671 kvm_run
->debug
.arch
.exception
= ex_no
;
2674 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2675 kvm_run
->ex
.exception
= ex_no
;
2676 kvm_run
->ex
.error_code
= error_code
;
2682 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2683 struct kvm_run
*kvm_run
)
2685 ++vcpu
->stat
.irq_exits
;
2686 KVMTRACE_1D(INTR
, vcpu
, vmcs_read32(VM_EXIT_INTR_INFO
), handler
);
2690 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2692 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2696 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2698 unsigned long exit_qualification
;
2699 int size
, in
, string
;
2702 ++vcpu
->stat
.io_exits
;
2703 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2704 string
= (exit_qualification
& 16) != 0;
2707 if (emulate_instruction(vcpu
,
2708 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2713 size
= (exit_qualification
& 7) + 1;
2714 in
= (exit_qualification
& 8) != 0;
2715 port
= exit_qualification
>> 16;
2717 skip_emulated_instruction(vcpu
);
2718 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2722 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2725 * Patch in the VMCALL instruction:
2727 hypercall
[0] = 0x0f;
2728 hypercall
[1] = 0x01;
2729 hypercall
[2] = 0xc1;
2732 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2734 unsigned long exit_qualification
;
2738 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2739 cr
= exit_qualification
& 15;
2740 reg
= (exit_qualification
>> 8) & 15;
2741 switch ((exit_qualification
>> 4) & 3) {
2742 case 0: /* mov to cr */
2743 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
,
2744 (u32
)kvm_register_read(vcpu
, reg
),
2745 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2749 kvm_set_cr0(vcpu
, kvm_register_read(vcpu
, reg
));
2750 skip_emulated_instruction(vcpu
);
2753 kvm_set_cr3(vcpu
, kvm_register_read(vcpu
, reg
));
2754 skip_emulated_instruction(vcpu
);
2757 kvm_set_cr4(vcpu
, kvm_register_read(vcpu
, reg
));
2758 skip_emulated_instruction(vcpu
);
2761 kvm_set_cr8(vcpu
, kvm_register_read(vcpu
, reg
));
2762 skip_emulated_instruction(vcpu
);
2763 if (irqchip_in_kernel(vcpu
->kvm
))
2765 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2770 vmx_fpu_deactivate(vcpu
);
2771 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2772 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2773 vmx_fpu_activate(vcpu
);
2774 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2775 skip_emulated_instruction(vcpu
);
2777 case 1: /*mov from cr*/
2780 kvm_register_write(vcpu
, reg
, vcpu
->arch
.cr3
);
2781 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
,
2782 (u32
)kvm_register_read(vcpu
, reg
),
2783 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2785 skip_emulated_instruction(vcpu
);
2788 kvm_register_write(vcpu
, reg
, kvm_get_cr8(vcpu
));
2789 KVMTRACE_2D(CR_READ
, vcpu
, (u32
)cr
,
2790 (u32
)kvm_register_read(vcpu
, reg
), handler
);
2791 skip_emulated_instruction(vcpu
);
2796 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2798 skip_emulated_instruction(vcpu
);
2803 kvm_run
->exit_reason
= 0;
2804 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2805 (int)(exit_qualification
>> 4) & 3, cr
);
2809 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2811 unsigned long exit_qualification
;
2815 dr
= vmcs_readl(GUEST_DR7
);
2818 * As the vm-exit takes precedence over the debug trap, we
2819 * need to emulate the latter, either for the host or the
2820 * guest debugging itself.
2822 if (vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
) {
2823 kvm_run
->debug
.arch
.dr6
= vcpu
->arch
.dr6
;
2824 kvm_run
->debug
.arch
.dr7
= dr
;
2825 kvm_run
->debug
.arch
.pc
=
2826 vmcs_readl(GUEST_CS_BASE
) +
2827 vmcs_readl(GUEST_RIP
);
2828 kvm_run
->debug
.arch
.exception
= DB_VECTOR
;
2829 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2832 vcpu
->arch
.dr7
&= ~DR7_GD
;
2833 vcpu
->arch
.dr6
|= DR6_BD
;
2834 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
2835 kvm_queue_exception(vcpu
, DB_VECTOR
);
2840 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2841 dr
= exit_qualification
& DEBUG_REG_ACCESS_NUM
;
2842 reg
= DEBUG_REG_ACCESS_REG(exit_qualification
);
2843 if (exit_qualification
& TYPE_MOV_FROM_DR
) {
2846 val
= vcpu
->arch
.db
[dr
];
2849 val
= vcpu
->arch
.dr6
;
2852 val
= vcpu
->arch
.dr7
;
2857 kvm_register_write(vcpu
, reg
, val
);
2858 KVMTRACE_2D(DR_READ
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2860 val
= vcpu
->arch
.regs
[reg
];
2863 vcpu
->arch
.db
[dr
] = val
;
2864 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
))
2865 vcpu
->arch
.eff_db
[dr
] = val
;
2868 if (vcpu
->arch
.cr4
& X86_CR4_DE
)
2869 kvm_queue_exception(vcpu
, UD_VECTOR
);
2872 if (val
& 0xffffffff00000000ULL
) {
2873 kvm_queue_exception(vcpu
, GP_VECTOR
);
2876 vcpu
->arch
.dr6
= (val
& DR6_VOLATILE
) | DR6_FIXED_1
;
2879 if (val
& 0xffffffff00000000ULL
) {
2880 kvm_queue_exception(vcpu
, GP_VECTOR
);
2883 vcpu
->arch
.dr7
= (val
& DR7_VOLATILE
) | DR7_FIXED_1
;
2884 if (!(vcpu
->guest_debug
& KVM_GUESTDBG_USE_HW_BP
)) {
2885 vmcs_writel(GUEST_DR7
, vcpu
->arch
.dr7
);
2886 vcpu
->arch
.switch_db_regs
=
2887 (val
& DR7_BP_EN_MASK
);
2891 KVMTRACE_2D(DR_WRITE
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2893 skip_emulated_instruction(vcpu
);
2897 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2899 kvm_emulate_cpuid(vcpu
);
2903 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2905 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2908 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
2909 kvm_inject_gp(vcpu
, 0);
2913 KVMTRACE_3D(MSR_READ
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2916 /* FIXME: handling of bits 32:63 of rax, rdx */
2917 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
2918 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
2919 skip_emulated_instruction(vcpu
);
2923 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2925 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2926 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
2927 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2929 KVMTRACE_3D(MSR_WRITE
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2932 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
2933 kvm_inject_gp(vcpu
, 0);
2937 skip_emulated_instruction(vcpu
);
2941 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
2942 struct kvm_run
*kvm_run
)
2947 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
2948 struct kvm_run
*kvm_run
)
2950 u32 cpu_based_vm_exec_control
;
2952 /* clear pending irq */
2953 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2954 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2955 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2957 KVMTRACE_0D(PEND_INTR
, vcpu
, handler
);
2958 ++vcpu
->stat
.irq_window_exits
;
2961 * If the user space waits to inject interrupts, exit as soon as
2964 if (kvm_run
->request_interrupt_window
&&
2965 !vcpu
->arch
.irq_summary
) {
2966 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2972 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2974 skip_emulated_instruction(vcpu
);
2975 return kvm_emulate_halt(vcpu
);
2978 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2980 skip_emulated_instruction(vcpu
);
2981 kvm_emulate_hypercall(vcpu
);
2985 static int handle_invlpg(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2987 u64 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2989 kvm_mmu_invlpg(vcpu
, exit_qualification
);
2990 skip_emulated_instruction(vcpu
);
2994 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2996 skip_emulated_instruction(vcpu
);
2997 /* TODO: Add support for VT-d/pass-through device */
3001 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3003 u64 exit_qualification
;
3004 enum emulation_result er
;
3005 unsigned long offset
;
3007 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
3008 offset
= exit_qualification
& 0xffful
;
3010 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3012 if (er
!= EMULATE_DONE
) {
3014 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3021 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3023 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3024 unsigned long exit_qualification
;
3028 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
3030 reason
= (u32
)exit_qualification
>> 30;
3031 if (reason
== TASK_SWITCH_GATE
&& vmx
->vcpu
.arch
.nmi_injected
&&
3032 (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3033 (vmx
->idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
)
3034 == INTR_TYPE_NMI_INTR
) {
3035 vcpu
->arch
.nmi_injected
= false;
3036 if (cpu_has_virtual_nmis())
3037 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3038 GUEST_INTR_STATE_NMI
);
3040 tss_selector
= exit_qualification
;
3042 if (!kvm_task_switch(vcpu
, tss_selector
, reason
))
3045 /* clear all local breakpoint enable flags */
3046 vmcs_writel(GUEST_DR7
, vmcs_readl(GUEST_DR7
) & ~55);
3049 * TODO: What about debug traps on tss switch?
3050 * Are we supposed to inject them and update dr6?
3056 static int handle_ept_violation(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3058 u64 exit_qualification
;
3062 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
3064 if (exit_qualification
& (1 << 6)) {
3065 printk(KERN_ERR
"EPT: GPA exceeds GAW!\n");
3069 gla_validity
= (exit_qualification
>> 7) & 0x3;
3070 if (gla_validity
!= 0x3 && gla_validity
!= 0x1 && gla_validity
!= 0) {
3071 printk(KERN_ERR
"EPT: Handling EPT violation failed!\n");
3072 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3073 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
3074 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS
));
3075 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
3076 (long unsigned int)exit_qualification
);
3077 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3078 kvm_run
->hw
.hardware_exit_reason
= 0;
3082 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
3083 return kvm_mmu_page_fault(vcpu
, gpa
& PAGE_MASK
, 0);
3086 static int handle_nmi_window(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3088 u32 cpu_based_vm_exec_control
;
3090 /* clear pending NMI */
3091 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
3092 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
3093 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
3094 ++vcpu
->stat
.nmi_window_exits
;
3099 static void handle_invalid_guest_state(struct kvm_vcpu
*vcpu
,
3100 struct kvm_run
*kvm_run
)
3102 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3103 enum emulation_result err
= EMULATE_DONE
;
3108 while (!guest_state_valid(vcpu
)) {
3109 err
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
3111 if (err
== EMULATE_DO_MMIO
)
3114 if (err
!= EMULATE_DONE
) {
3115 kvm_report_emulation_failure(vcpu
, "emulation failure");
3119 if (signal_pending(current
))
3125 local_irq_disable();
3128 vmx
->invalid_state_emulation_result
= err
;
3132 * The exit handlers return 1 if the exit was handled fully and guest execution
3133 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3134 * to be done to userspace and return 0.
3136 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
3137 struct kvm_run
*kvm_run
) = {
3138 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
3139 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
3140 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
3141 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
3142 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
3143 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
3144 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
3145 [EXIT_REASON_CPUID
] = handle_cpuid
,
3146 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
3147 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
3148 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
3149 [EXIT_REASON_HLT
] = handle_halt
,
3150 [EXIT_REASON_INVLPG
] = handle_invlpg
,
3151 [EXIT_REASON_VMCALL
] = handle_vmcall
,
3152 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
3153 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
3154 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
3155 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
3156 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
3159 static const int kvm_vmx_max_exit_handlers
=
3160 ARRAY_SIZE(kvm_vmx_exit_handlers
);
3163 * The guest has exited. See if we can fix it or if we need userspace
3166 static int vmx_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
3168 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
3169 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3170 u32 vectoring_info
= vmx
->idt_vectoring_info
;
3172 KVMTRACE_3D(VMEXIT
, vcpu
, exit_reason
, (u32
)kvm_rip_read(vcpu
),
3173 (u32
)((u64
)kvm_rip_read(vcpu
) >> 32), entryexit
);
3175 /* If we need to emulate an MMIO from handle_invalid_guest_state
3176 * we just return 0 */
3177 if (vmx
->emulation_required
&& emulate_invalid_guest_state
) {
3178 if (guest_state_valid(vcpu
))
3179 vmx
->emulation_required
= 0;
3180 return vmx
->invalid_state_emulation_result
!= EMULATE_DO_MMIO
;
3183 /* Access CR3 don't cause VMExit in paging mode, so we need
3184 * to sync with guest real CR3. */
3185 if (enable_ept
&& is_paging(vcpu
)) {
3186 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
3187 ept_load_pdptrs(vcpu
);
3190 if (unlikely(vmx
->fail
)) {
3191 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
3192 kvm_run
->fail_entry
.hardware_entry_failure_reason
3193 = vmcs_read32(VM_INSTRUCTION_ERROR
);
3197 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
3198 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
3199 exit_reason
!= EXIT_REASON_EPT_VIOLATION
&&
3200 exit_reason
!= EXIT_REASON_TASK_SWITCH
))
3201 printk(KERN_WARNING
"%s: unexpected, valid vectoring info "
3202 "(0x%x) and exit reason is 0x%x\n",
3203 __func__
, vectoring_info
, exit_reason
);
3205 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
)) {
3206 if (vcpu
->arch
.interrupt_window_open
) {
3207 vmx
->soft_vnmi_blocked
= 0;
3208 vcpu
->arch
.nmi_window_open
= 1;
3209 } else if (vmx
->vnmi_blocked_time
> 1000000000LL &&
3210 vcpu
->arch
.nmi_pending
) {
3212 * This CPU don't support us in finding the end of an
3213 * NMI-blocked window if the guest runs with IRQs
3214 * disabled. So we pull the trigger after 1 s of
3215 * futile waiting, but inform the user about this.
3217 printk(KERN_WARNING
"%s: Breaking out of NMI-blocked "
3218 "state on VCPU %d after 1 s timeout\n",
3219 __func__
, vcpu
->vcpu_id
);
3220 vmx
->soft_vnmi_blocked
= 0;
3221 vmx
->vcpu
.arch
.nmi_window_open
= 1;
3225 if (exit_reason
< kvm_vmx_max_exit_handlers
3226 && kvm_vmx_exit_handlers
[exit_reason
])
3227 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
3229 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
3230 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
3235 static void update_tpr_threshold(struct kvm_vcpu
*vcpu
)
3239 if (!vm_need_tpr_shadow(vcpu
->kvm
))
3242 if (!kvm_lapic_enabled(vcpu
) ||
3243 ((max_irr
= kvm_lapic_find_highest_irr(vcpu
)) == -1)) {
3244 vmcs_write32(TPR_THRESHOLD
, 0);
3248 tpr
= (kvm_lapic_get_cr8(vcpu
) & 0x0f) << 4;
3249 vmcs_write32(TPR_THRESHOLD
, (max_irr
> tpr
) ? tpr
>> 4 : max_irr
>> 4);
3252 static void vmx_complete_interrupts(struct vcpu_vmx
*vmx
)
3255 u32 idt_vectoring_info
;
3259 bool idtv_info_valid
;
3262 exit_intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3263 if (cpu_has_virtual_nmis()) {
3264 unblock_nmi
= (exit_intr_info
& INTR_INFO_UNBLOCK_NMI
) != 0;
3265 vector
= exit_intr_info
& INTR_INFO_VECTOR_MASK
;
3268 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3269 * a guest IRET fault.
3271 if (unblock_nmi
&& vector
!= DF_VECTOR
)
3272 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO
,
3273 GUEST_INTR_STATE_NMI
);
3274 } else if (unlikely(vmx
->soft_vnmi_blocked
))
3275 vmx
->vnmi_blocked_time
+=
3276 ktime_to_ns(ktime_sub(ktime_get(), vmx
->entry_time
));
3278 idt_vectoring_info
= vmx
->idt_vectoring_info
;
3279 idtv_info_valid
= idt_vectoring_info
& VECTORING_INFO_VALID_MASK
;
3280 vector
= idt_vectoring_info
& VECTORING_INFO_VECTOR_MASK
;
3281 type
= idt_vectoring_info
& VECTORING_INFO_TYPE_MASK
;
3282 if (vmx
->vcpu
.arch
.nmi_injected
) {
3285 * Clear bit "block by NMI" before VM entry if a NMI delivery
3288 if (idtv_info_valid
&& type
== INTR_TYPE_NMI_INTR
)
3289 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3290 GUEST_INTR_STATE_NMI
);
3292 vmx
->vcpu
.arch
.nmi_injected
= false;
3294 kvm_clear_exception_queue(&vmx
->vcpu
);
3295 if (idtv_info_valid
&& (type
== INTR_TYPE_HARD_EXCEPTION
||
3296 type
== INTR_TYPE_SOFT_EXCEPTION
)) {
3297 if (idt_vectoring_info
& VECTORING_INFO_DELIVER_CODE_MASK
) {
3298 error
= vmcs_read32(IDT_VECTORING_ERROR_CODE
);
3299 kvm_queue_exception_e(&vmx
->vcpu
, vector
, error
);
3301 kvm_queue_exception(&vmx
->vcpu
, vector
);
3302 vmx
->idt_vectoring_info
= 0;
3304 kvm_clear_interrupt_queue(&vmx
->vcpu
);
3305 if (idtv_info_valid
&& type
== INTR_TYPE_EXT_INTR
) {
3306 kvm_queue_interrupt(&vmx
->vcpu
, vector
);
3307 vmx
->idt_vectoring_info
= 0;
3311 static void vmx_intr_assist(struct kvm_vcpu
*vcpu
)
3313 update_tpr_threshold(vcpu
);
3315 vmx_update_window_states(vcpu
);
3317 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3318 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO
,
3319 GUEST_INTR_STATE_STI
|
3320 GUEST_INTR_STATE_MOV_SS
);
3322 if (vcpu
->arch
.nmi_pending
&& !vcpu
->arch
.nmi_injected
) {
3323 if (vcpu
->arch
.interrupt
.pending
) {
3324 enable_nmi_window(vcpu
);
3325 } else if (vcpu
->arch
.nmi_window_open
) {
3326 vcpu
->arch
.nmi_pending
= false;
3327 vcpu
->arch
.nmi_injected
= true;
3329 enable_nmi_window(vcpu
);
3333 if (vcpu
->arch
.nmi_injected
) {
3334 vmx_inject_nmi(vcpu
);
3335 if (vcpu
->arch
.nmi_pending
)
3336 enable_nmi_window(vcpu
);
3337 else if (kvm_cpu_has_interrupt(vcpu
))
3338 enable_irq_window(vcpu
);
3341 if (!vcpu
->arch
.interrupt
.pending
&& kvm_cpu_has_interrupt(vcpu
)) {
3342 if (vcpu
->arch
.interrupt_window_open
)
3343 kvm_queue_interrupt(vcpu
, kvm_cpu_get_interrupt(vcpu
));
3345 enable_irq_window(vcpu
);
3347 if (vcpu
->arch
.interrupt
.pending
) {
3348 vmx_inject_irq(vcpu
, vcpu
->arch
.interrupt
.nr
);
3349 if (kvm_cpu_has_interrupt(vcpu
))
3350 enable_irq_window(vcpu
);
3355 * Failure to inject an interrupt should give us the information
3356 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3357 * when fetching the interrupt redirection bitmap in the real-mode
3358 * tss, this doesn't happen. So we do it ourselves.
3360 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
3362 vmx
->rmode
.irq
.pending
= 0;
3363 if (kvm_rip_read(&vmx
->vcpu
) + 1 != vmx
->rmode
.irq
.rip
)
3365 kvm_rip_write(&vmx
->vcpu
, vmx
->rmode
.irq
.rip
);
3366 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
3367 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
3368 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
3371 vmx
->idt_vectoring_info
=
3372 VECTORING_INFO_VALID_MASK
3373 | INTR_TYPE_EXT_INTR
3374 | vmx
->rmode
.irq
.vector
;
3377 #ifdef CONFIG_X86_64
3385 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3387 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3390 /* Record the guest's net vcpu time for enforced NMI injections. */
3391 if (unlikely(!cpu_has_virtual_nmis() && vmx
->soft_vnmi_blocked
))
3392 vmx
->entry_time
= ktime_get();
3394 /* Handle invalid guest state instead of entering VMX */
3395 if (vmx
->emulation_required
&& emulate_invalid_guest_state
) {
3396 handle_invalid_guest_state(vcpu
, kvm_run
);
3400 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3401 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
3402 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
3403 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
3406 * Loading guest fpu may have cleared host cr0.ts
3408 vmcs_writel(HOST_CR0
, read_cr0());
3410 set_debugreg(vcpu
->arch
.dr6
, 6);
3413 /* Store host registers */
3414 "push %%"R
"dx; push %%"R
"bp;"
3416 "cmp %%"R
"sp, %c[host_rsp](%0) \n\t"
3418 "mov %%"R
"sp, %c[host_rsp](%0) \n\t"
3419 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
3421 /* Check if vmlaunch of vmresume is needed */
3422 "cmpl $0, %c[launched](%0) \n\t"
3423 /* Load guest registers. Don't clobber flags. */
3424 "mov %c[cr2](%0), %%"R
"ax \n\t"
3425 "mov %%"R
"ax, %%cr2 \n\t"
3426 "mov %c[rax](%0), %%"R
"ax \n\t"
3427 "mov %c[rbx](%0), %%"R
"bx \n\t"
3428 "mov %c[rdx](%0), %%"R
"dx \n\t"
3429 "mov %c[rsi](%0), %%"R
"si \n\t"
3430 "mov %c[rdi](%0), %%"R
"di \n\t"
3431 "mov %c[rbp](%0), %%"R
"bp \n\t"
3432 #ifdef CONFIG_X86_64
3433 "mov %c[r8](%0), %%r8 \n\t"
3434 "mov %c[r9](%0), %%r9 \n\t"
3435 "mov %c[r10](%0), %%r10 \n\t"
3436 "mov %c[r11](%0), %%r11 \n\t"
3437 "mov %c[r12](%0), %%r12 \n\t"
3438 "mov %c[r13](%0), %%r13 \n\t"
3439 "mov %c[r14](%0), %%r14 \n\t"
3440 "mov %c[r15](%0), %%r15 \n\t"
3442 "mov %c[rcx](%0), %%"R
"cx \n\t" /* kills %0 (ecx) */
3444 /* Enter guest mode */
3445 "jne .Llaunched \n\t"
3446 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
3447 "jmp .Lkvm_vmx_return \n\t"
3448 ".Llaunched: " __ex(ASM_VMX_VMRESUME
) "\n\t"
3449 ".Lkvm_vmx_return: "
3450 /* Save guest registers, load host registers, keep flags */
3451 "xchg %0, (%%"R
"sp) \n\t"
3452 "mov %%"R
"ax, %c[rax](%0) \n\t"
3453 "mov %%"R
"bx, %c[rbx](%0) \n\t"
3454 "push"Q
" (%%"R
"sp); pop"Q
" %c[rcx](%0) \n\t"
3455 "mov %%"R
"dx, %c[rdx](%0) \n\t"
3456 "mov %%"R
"si, %c[rsi](%0) \n\t"
3457 "mov %%"R
"di, %c[rdi](%0) \n\t"
3458 "mov %%"R
"bp, %c[rbp](%0) \n\t"
3459 #ifdef CONFIG_X86_64
3460 "mov %%r8, %c[r8](%0) \n\t"
3461 "mov %%r9, %c[r9](%0) \n\t"
3462 "mov %%r10, %c[r10](%0) \n\t"
3463 "mov %%r11, %c[r11](%0) \n\t"
3464 "mov %%r12, %c[r12](%0) \n\t"
3465 "mov %%r13, %c[r13](%0) \n\t"
3466 "mov %%r14, %c[r14](%0) \n\t"
3467 "mov %%r15, %c[r15](%0) \n\t"
3469 "mov %%cr2, %%"R
"ax \n\t"
3470 "mov %%"R
"ax, %c[cr2](%0) \n\t"
3472 "pop %%"R
"bp; pop %%"R
"bp; pop %%"R
"dx \n\t"
3473 "setbe %c[fail](%0) \n\t"
3474 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
3475 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
3476 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
3477 [host_rsp
]"i"(offsetof(struct vcpu_vmx
, host_rsp
)),
3478 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
3479 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3480 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3481 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3482 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3483 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3484 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
3485 #ifdef CONFIG_X86_64
3486 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3487 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3488 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3489 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3490 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3491 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3492 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3493 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
3495 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
3497 , R
"bx", R
"di", R
"si"
3498 #ifdef CONFIG_X86_64
3499 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3503 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
3504 vcpu
->arch
.regs_dirty
= 0;
3506 get_debugreg(vcpu
->arch
.dr6
, 6);
3508 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
3509 if (vmx
->rmode
.irq
.pending
)
3510 fixup_rmode_irq(vmx
);
3512 vmx_update_window_states(vcpu
);
3514 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
3517 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3519 /* We need to handle NMIs before interrupts are enabled */
3520 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == INTR_TYPE_NMI_INTR
&&
3521 (intr_info
& INTR_INFO_VALID_MASK
)) {
3522 KVMTRACE_0D(NMI
, vcpu
, handler
);
3526 vmx_complete_interrupts(vmx
);
3532 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
3534 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3538 free_vmcs(vmx
->vmcs
);
3543 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
3545 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3547 spin_lock(&vmx_vpid_lock
);
3549 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
3550 spin_unlock(&vmx_vpid_lock
);
3551 vmx_free_vmcs(vcpu
);
3552 kfree(vmx
->host_msrs
);
3553 kfree(vmx
->guest_msrs
);
3554 kvm_vcpu_uninit(vcpu
);
3555 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3558 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
3561 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
3565 return ERR_PTR(-ENOMEM
);
3569 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
3573 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3574 if (!vmx
->guest_msrs
) {
3579 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3580 if (!vmx
->host_msrs
)
3581 goto free_guest_msrs
;
3583 vmx
->vmcs
= alloc_vmcs();
3587 vmcs_clear(vmx
->vmcs
);
3590 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
3591 err
= vmx_vcpu_setup(vmx
);
3592 vmx_vcpu_put(&vmx
->vcpu
);
3596 if (vm_need_virtualize_apic_accesses(kvm
))
3597 if (alloc_apic_access_page(kvm
) != 0)
3601 if (alloc_identity_pagetable(kvm
) != 0)
3607 free_vmcs(vmx
->vmcs
);
3609 kfree(vmx
->host_msrs
);
3611 kfree(vmx
->guest_msrs
);
3613 kvm_vcpu_uninit(&vmx
->vcpu
);
3615 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3616 return ERR_PTR(err
);
3619 static void __init
vmx_check_processor_compat(void *rtn
)
3621 struct vmcs_config vmcs_conf
;
3624 if (setup_vmcs_config(&vmcs_conf
) < 0)
3626 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
3627 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
3628 smp_processor_id());
3633 static int get_ept_level(void)
3635 return VMX_EPT_DEFAULT_GAW
+ 1;
3638 static int vmx_get_mt_mask_shift(void)
3640 return VMX_EPT_MT_EPTE_SHIFT
;
3643 static struct kvm_x86_ops vmx_x86_ops
= {
3644 .cpu_has_kvm_support
= cpu_has_kvm_support
,
3645 .disabled_by_bios
= vmx_disabled_by_bios
,
3646 .hardware_setup
= hardware_setup
,
3647 .hardware_unsetup
= hardware_unsetup
,
3648 .check_processor_compatibility
= vmx_check_processor_compat
,
3649 .hardware_enable
= hardware_enable
,
3650 .hardware_disable
= hardware_disable
,
3651 .cpu_has_accelerated_tpr
= cpu_has_vmx_virtualize_apic_accesses
,
3653 .vcpu_create
= vmx_create_vcpu
,
3654 .vcpu_free
= vmx_free_vcpu
,
3655 .vcpu_reset
= vmx_vcpu_reset
,
3657 .prepare_guest_switch
= vmx_save_host_state
,
3658 .vcpu_load
= vmx_vcpu_load
,
3659 .vcpu_put
= vmx_vcpu_put
,
3661 .set_guest_debug
= set_guest_debug
,
3662 .get_msr
= vmx_get_msr
,
3663 .set_msr
= vmx_set_msr
,
3664 .get_segment_base
= vmx_get_segment_base
,
3665 .get_segment
= vmx_get_segment
,
3666 .set_segment
= vmx_set_segment
,
3667 .get_cpl
= vmx_get_cpl
,
3668 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
3669 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
3670 .set_cr0
= vmx_set_cr0
,
3671 .set_cr3
= vmx_set_cr3
,
3672 .set_cr4
= vmx_set_cr4
,
3673 .set_efer
= vmx_set_efer
,
3674 .get_idt
= vmx_get_idt
,
3675 .set_idt
= vmx_set_idt
,
3676 .get_gdt
= vmx_get_gdt
,
3677 .set_gdt
= vmx_set_gdt
,
3678 .cache_reg
= vmx_cache_reg
,
3679 .get_rflags
= vmx_get_rflags
,
3680 .set_rflags
= vmx_set_rflags
,
3682 .tlb_flush
= vmx_flush_tlb
,
3684 .run
= vmx_vcpu_run
,
3685 .handle_exit
= vmx_handle_exit
,
3686 .skip_emulated_instruction
= skip_emulated_instruction
,
3687 .patch_hypercall
= vmx_patch_hypercall
,
3688 .get_irq
= vmx_get_irq
,
3689 .set_irq
= vmx_inject_irq
,
3690 .queue_exception
= vmx_queue_exception
,
3691 .exception_injected
= vmx_exception_injected
,
3692 .inject_pending_irq
= vmx_intr_assist
,
3693 .inject_pending_vectors
= do_interrupt_requests
,
3695 .set_tss_addr
= vmx_set_tss_addr
,
3696 .get_tdp_level
= get_ept_level
,
3697 .get_mt_mask_shift
= vmx_get_mt_mask_shift
,
3700 static int __init
vmx_init(void)
3704 vmx_io_bitmap_a
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3705 if (!vmx_io_bitmap_a
)
3708 vmx_io_bitmap_b
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3709 if (!vmx_io_bitmap_b
) {
3714 vmx_msr_bitmap_legacy
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3715 if (!vmx_msr_bitmap_legacy
) {
3720 vmx_msr_bitmap_longmode
= (unsigned long *)__get_free_page(GFP_KERNEL
);
3721 if (!vmx_msr_bitmap_longmode
) {
3727 * Allow direct access to the PC debug port (it is often used for I/O
3728 * delays, but the vmexits simply slow things down).
3730 memset(vmx_io_bitmap_a
, 0xff, PAGE_SIZE
);
3731 clear_bit(0x80, vmx_io_bitmap_a
);
3733 memset(vmx_io_bitmap_b
, 0xff, PAGE_SIZE
);
3735 memset(vmx_msr_bitmap_legacy
, 0xff, PAGE_SIZE
);
3736 memset(vmx_msr_bitmap_longmode
, 0xff, PAGE_SIZE
);
3738 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
3740 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
3744 vmx_disable_intercept_for_msr(MSR_FS_BASE
, false);
3745 vmx_disable_intercept_for_msr(MSR_GS_BASE
, false);
3746 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE
, true);
3747 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS
, false);
3748 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP
, false);
3749 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP
, false);
3752 bypass_guest_pf
= 0;
3753 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK
|
3754 VMX_EPT_WRITABLE_MASK
);
3755 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
3756 VMX_EPT_EXECUTABLE_MASK
,
3757 VMX_EPT_DEFAULT_MT
<< VMX_EPT_MT_EPTE_SHIFT
);
3762 if (bypass_guest_pf
)
3763 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
3770 free_page((unsigned long)vmx_msr_bitmap_longmode
);
3772 free_page((unsigned long)vmx_msr_bitmap_legacy
);
3774 free_page((unsigned long)vmx_io_bitmap_b
);
3776 free_page((unsigned long)vmx_io_bitmap_a
);
3780 static void __exit
vmx_exit(void)
3782 free_page((unsigned long)vmx_msr_bitmap_legacy
);
3783 free_page((unsigned long)vmx_msr_bitmap_longmode
);
3784 free_page((unsigned long)vmx_io_bitmap_b
);
3785 free_page((unsigned long)vmx_io_bitmap_a
);
3790 module_init(vmx_init
)
3791 module_exit(vmx_exit
)