2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
22 #include <linux/kvm_host.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
26 #include <linux/highmem.h>
27 #include <linux/sched.h>
28 #include <linux/moduleparam.h>
29 #include "kvm_cache_regs.h"
34 #define __ex(x) __kvm_handle_fault_on_reboot(x)
36 MODULE_AUTHOR("Qumranet");
37 MODULE_LICENSE("GPL");
39 static int bypass_guest_pf
= 1;
40 module_param(bypass_guest_pf
, bool, 0);
42 static int enable_vpid
= 1;
43 module_param(enable_vpid
, bool, 0);
45 static int flexpriority_enabled
= 1;
46 module_param(flexpriority_enabled
, bool, 0);
48 static int enable_ept
= 1;
49 module_param(enable_ept
, bool, 0);
59 struct list_head local_vcpus_link
;
62 u32 idt_vectoring_info
;
63 struct kvm_msr_entry
*guest_msrs
;
64 struct kvm_msr_entry
*host_msrs
;
69 int msr_offset_kernel_gs_base
;
74 u16 fs_sel
, gs_sel
, ldt_sel
;
75 int gs_ldt_reload_needed
;
77 int guest_efer_loaded
;
89 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
91 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
94 static int init_rmode(struct kvm
*kvm
);
95 static u64
construct_eptp(unsigned long root_hpa
);
97 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
98 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
99 static DEFINE_PER_CPU(struct list_head
, vcpus_on_cpu
);
101 static struct page
*vmx_io_bitmap_a
;
102 static struct page
*vmx_io_bitmap_b
;
103 static struct page
*vmx_msr_bitmap
;
105 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
106 static DEFINE_SPINLOCK(vmx_vpid_lock
);
108 static struct vmcs_config
{
112 u32 pin_based_exec_ctrl
;
113 u32 cpu_based_exec_ctrl
;
114 u32 cpu_based_2nd_exec_ctrl
;
119 struct vmx_capability
{
124 #define VMX_SEGMENT_FIELD(seg) \
125 [VCPU_SREG_##seg] = { \
126 .selector = GUEST_##seg##_SELECTOR, \
127 .base = GUEST_##seg##_BASE, \
128 .limit = GUEST_##seg##_LIMIT, \
129 .ar_bytes = GUEST_##seg##_AR_BYTES, \
132 static struct kvm_vmx_segment_field
{
137 } kvm_vmx_segment_fields
[] = {
138 VMX_SEGMENT_FIELD(CS
),
139 VMX_SEGMENT_FIELD(DS
),
140 VMX_SEGMENT_FIELD(ES
),
141 VMX_SEGMENT_FIELD(FS
),
142 VMX_SEGMENT_FIELD(GS
),
143 VMX_SEGMENT_FIELD(SS
),
144 VMX_SEGMENT_FIELD(TR
),
145 VMX_SEGMENT_FIELD(LDTR
),
149 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
150 * away by decrementing the array size.
152 static const u32 vmx_msr_index
[] = {
154 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
156 MSR_EFER
, MSR_K6_STAR
,
158 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
160 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
164 for (i
= 0; i
< n
; ++i
)
165 wrmsrl(e
[i
].index
, e
[i
].data
);
168 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
172 for (i
= 0; i
< n
; ++i
)
173 rdmsrl(e
[i
].index
, e
[i
].data
);
176 static inline int is_page_fault(u32 intr_info
)
178 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
179 INTR_INFO_VALID_MASK
)) ==
180 (INTR_TYPE_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
183 static inline int is_no_device(u32 intr_info
)
185 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
186 INTR_INFO_VALID_MASK
)) ==
187 (INTR_TYPE_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
190 static inline int is_invalid_opcode(u32 intr_info
)
192 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
193 INTR_INFO_VALID_MASK
)) ==
194 (INTR_TYPE_EXCEPTION
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
197 static inline int is_external_interrupt(u32 intr_info
)
199 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
200 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
203 static inline int cpu_has_vmx_msr_bitmap(void)
205 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
);
208 static inline int cpu_has_vmx_tpr_shadow(void)
210 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
);
213 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
215 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
)));
218 static inline int cpu_has_secondary_exec_ctrls(void)
220 return (vmcs_config
.cpu_based_exec_ctrl
&
221 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
);
224 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
226 return flexpriority_enabled
227 && (vmcs_config
.cpu_based_2nd_exec_ctrl
&
228 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
231 static inline int cpu_has_vmx_invept_individual_addr(void)
233 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_INDIVIDUAL_BIT
));
236 static inline int cpu_has_vmx_invept_context(void)
238 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_CONTEXT_BIT
));
241 static inline int cpu_has_vmx_invept_global(void)
243 return (!!(vmx_capability
.ept
& VMX_EPT_EXTENT_GLOBAL_BIT
));
246 static inline int cpu_has_vmx_ept(void)
248 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
249 SECONDARY_EXEC_ENABLE_EPT
);
252 static inline int vm_need_ept(void)
254 return (cpu_has_vmx_ept() && enable_ept
);
257 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
259 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
260 (irqchip_in_kernel(kvm
)));
263 static inline int cpu_has_vmx_vpid(void)
265 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
266 SECONDARY_EXEC_ENABLE_VPID
);
269 static inline int cpu_has_virtual_nmis(void)
271 return vmcs_config
.pin_based_exec_ctrl
& PIN_BASED_VIRTUAL_NMIS
;
274 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
278 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
279 if (vmx
->guest_msrs
[i
].index
== msr
)
284 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
290 } operand
= { vpid
, 0, gva
};
292 asm volatile (__ex(ASM_VMX_INVVPID
)
293 /* CF==1 or ZF==1 --> rc = -1 */
295 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
298 static inline void __invept(int ext
, u64 eptp
, gpa_t gpa
)
302 } operand
= {eptp
, gpa
};
304 asm volatile (__ex(ASM_VMX_INVEPT
)
305 /* CF==1 or ZF==1 --> rc = -1 */
306 "; ja 1f ; ud2 ; 1:\n"
307 : : "a" (&operand
), "c" (ext
) : "cc", "memory");
310 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
314 i
= __find_msr_index(vmx
, msr
);
316 return &vmx
->guest_msrs
[i
];
320 static void vmcs_clear(struct vmcs
*vmcs
)
322 u64 phys_addr
= __pa(vmcs
);
325 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX
) "; setna %0"
326 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
329 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
333 static void __vcpu_clear(void *arg
)
335 struct vcpu_vmx
*vmx
= arg
;
336 int cpu
= raw_smp_processor_id();
338 if (vmx
->vcpu
.cpu
== cpu
)
339 vmcs_clear(vmx
->vmcs
);
340 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
341 per_cpu(current_vmcs
, cpu
) = NULL
;
342 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
343 list_del(&vmx
->local_vcpus_link
);
348 static void vcpu_clear(struct vcpu_vmx
*vmx
)
350 if (vmx
->vcpu
.cpu
== -1)
352 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 1);
355 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
360 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
363 static inline void ept_sync_global(void)
365 if (cpu_has_vmx_invept_global())
366 __invept(VMX_EPT_EXTENT_GLOBAL
, 0, 0);
369 static inline void ept_sync_context(u64 eptp
)
372 if (cpu_has_vmx_invept_context())
373 __invept(VMX_EPT_EXTENT_CONTEXT
, eptp
, 0);
379 static inline void ept_sync_individual_addr(u64 eptp
, gpa_t gpa
)
382 if (cpu_has_vmx_invept_individual_addr())
383 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR
,
386 ept_sync_context(eptp
);
390 static unsigned long vmcs_readl(unsigned long field
)
394 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX
)
395 : "=a"(value
) : "d"(field
) : "cc");
399 static u16
vmcs_read16(unsigned long field
)
401 return vmcs_readl(field
);
404 static u32
vmcs_read32(unsigned long field
)
406 return vmcs_readl(field
);
409 static u64
vmcs_read64(unsigned long field
)
412 return vmcs_readl(field
);
414 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
418 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
420 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
421 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
425 static void vmcs_writel(unsigned long field
, unsigned long value
)
429 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX
) "; setna %0"
430 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
432 vmwrite_error(field
, value
);
435 static void vmcs_write16(unsigned long field
, u16 value
)
437 vmcs_writel(field
, value
);
440 static void vmcs_write32(unsigned long field
, u32 value
)
442 vmcs_writel(field
, value
);
445 static void vmcs_write64(unsigned long field
, u64 value
)
447 vmcs_writel(field
, value
);
448 #ifndef CONFIG_X86_64
450 vmcs_writel(field
+1, value
>> 32);
454 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
456 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
459 static void vmcs_set_bits(unsigned long field
, u32 mask
)
461 vmcs_writel(field
, vmcs_readl(field
) | mask
);
464 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
468 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
);
469 if (!vcpu
->fpu_active
)
470 eb
|= 1u << NM_VECTOR
;
471 if (vcpu
->guest_debug
.enabled
)
473 if (vcpu
->arch
.rmode
.active
)
476 eb
&= ~(1u << PF_VECTOR
); /* bypass_guest_pf = 0 */
477 vmcs_write32(EXCEPTION_BITMAP
, eb
);
480 static void reload_tss(void)
483 * VT restores TR but not its size. Useless.
485 struct descriptor_table gdt
;
486 struct desc_struct
*descs
;
489 descs
= (void *)gdt
.base
;
490 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
494 static void load_transition_efer(struct vcpu_vmx
*vmx
)
496 int efer_offset
= vmx
->msr_offset_efer
;
497 u64 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
498 u64 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
504 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
507 ignore_bits
= EFER_NX
| EFER_SCE
;
509 ignore_bits
|= EFER_LMA
| EFER_LME
;
510 /* SCE is meaningful only in long mode on Intel */
511 if (guest_efer
& EFER_LMA
)
512 ignore_bits
&= ~(u64
)EFER_SCE
;
514 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
517 vmx
->host_state
.guest_efer_loaded
= 1;
518 guest_efer
&= ~ignore_bits
;
519 guest_efer
|= host_efer
& ignore_bits
;
520 wrmsrl(MSR_EFER
, guest_efer
);
521 vmx
->vcpu
.stat
.efer_reload
++;
524 static void reload_host_efer(struct vcpu_vmx
*vmx
)
526 if (vmx
->host_state
.guest_efer_loaded
) {
527 vmx
->host_state
.guest_efer_loaded
= 0;
528 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
532 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
534 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
536 if (vmx
->host_state
.loaded
)
539 vmx
->host_state
.loaded
= 1;
541 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
542 * allow segment selectors with cpl > 0 or ti == 1.
544 vmx
->host_state
.ldt_sel
= kvm_read_ldt();
545 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
546 vmx
->host_state
.fs_sel
= kvm_read_fs();
547 if (!(vmx
->host_state
.fs_sel
& 7)) {
548 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
549 vmx
->host_state
.fs_reload_needed
= 0;
551 vmcs_write16(HOST_FS_SELECTOR
, 0);
552 vmx
->host_state
.fs_reload_needed
= 1;
554 vmx
->host_state
.gs_sel
= kvm_read_gs();
555 if (!(vmx
->host_state
.gs_sel
& 7))
556 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
558 vmcs_write16(HOST_GS_SELECTOR
, 0);
559 vmx
->host_state
.gs_ldt_reload_needed
= 1;
563 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
564 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
566 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
567 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
571 if (is_long_mode(&vmx
->vcpu
))
572 save_msrs(vmx
->host_msrs
+
573 vmx
->msr_offset_kernel_gs_base
, 1);
576 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
577 load_transition_efer(vmx
);
580 static void __vmx_load_host_state(struct vcpu_vmx
*vmx
)
584 if (!vmx
->host_state
.loaded
)
587 ++vmx
->vcpu
.stat
.host_state_reload
;
588 vmx
->host_state
.loaded
= 0;
589 if (vmx
->host_state
.fs_reload_needed
)
590 kvm_load_fs(vmx
->host_state
.fs_sel
);
591 if (vmx
->host_state
.gs_ldt_reload_needed
) {
592 kvm_load_ldt(vmx
->host_state
.ldt_sel
);
594 * If we have to reload gs, we must take care to
595 * preserve our gs base.
597 local_irq_save(flags
);
598 kvm_load_gs(vmx
->host_state
.gs_sel
);
600 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
602 local_irq_restore(flags
);
605 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
606 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
607 reload_host_efer(vmx
);
610 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
613 __vmx_load_host_state(vmx
);
618 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
619 * vcpu mutex is already taken.
621 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
623 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
624 u64 phys_addr
= __pa(vmx
->vmcs
);
625 u64 tsc_this
, delta
, new_offset
;
627 if (vcpu
->cpu
!= cpu
) {
629 kvm_migrate_timers(vcpu
);
630 vpid_sync_vcpu_all(vmx
);
632 list_add(&vmx
->local_vcpus_link
,
633 &per_cpu(vcpus_on_cpu
, cpu
));
637 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
640 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
641 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX
) "; setna %0"
642 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
645 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
646 vmx
->vmcs
, phys_addr
);
649 if (vcpu
->cpu
!= cpu
) {
650 struct descriptor_table dt
;
651 unsigned long sysenter_esp
;
655 * Linux uses per-cpu TSS and GDT, so set these when switching
658 vmcs_writel(HOST_TR_BASE
, kvm_read_tr_base()); /* 22.2.4 */
660 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
662 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
663 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
666 * Make sure the time stamp counter is monotonous.
669 if (tsc_this
< vcpu
->arch
.host_tsc
) {
670 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
671 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
672 vmcs_write64(TSC_OFFSET
, new_offset
);
677 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
679 __vmx_load_host_state(to_vmx(vcpu
));
682 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
684 if (vcpu
->fpu_active
)
686 vcpu
->fpu_active
= 1;
687 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
688 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
689 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
690 update_exception_bitmap(vcpu
);
693 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
695 if (!vcpu
->fpu_active
)
697 vcpu
->fpu_active
= 0;
698 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
699 update_exception_bitmap(vcpu
);
702 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
704 return vmcs_readl(GUEST_RFLAGS
);
707 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
709 if (vcpu
->arch
.rmode
.active
)
710 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
711 vmcs_writel(GUEST_RFLAGS
, rflags
);
714 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
717 u32 interruptibility
;
719 rip
= kvm_rip_read(vcpu
);
720 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
721 kvm_rip_write(vcpu
, rip
);
724 * We emulated an instruction, so temporary interrupt blocking
725 * should be removed, if set.
727 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
728 if (interruptibility
& 3)
729 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
730 interruptibility
& ~3);
731 vcpu
->arch
.interrupt_window_open
= 1;
734 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
735 bool has_error_code
, u32 error_code
)
737 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
738 nr
| INTR_TYPE_EXCEPTION
739 | (has_error_code
? INTR_INFO_DELIVER_CODE_MASK
: 0)
740 | INTR_INFO_VALID_MASK
);
742 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
745 static bool vmx_exception_injected(struct kvm_vcpu
*vcpu
)
747 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
749 return !(vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
753 * Swap MSR entry in host/guest MSR entry array.
756 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
758 struct kvm_msr_entry tmp
;
760 tmp
= vmx
->guest_msrs
[to
];
761 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
762 vmx
->guest_msrs
[from
] = tmp
;
763 tmp
= vmx
->host_msrs
[to
];
764 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
765 vmx
->host_msrs
[from
] = tmp
;
770 * Set up the vmcs to automatically save and restore system
771 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
772 * mode, as fiddling with msrs is very expensive.
774 static void setup_msrs(struct vcpu_vmx
*vmx
)
778 vmx_load_host_state(vmx
);
781 if (is_long_mode(&vmx
->vcpu
)) {
784 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
786 move_msr_up(vmx
, index
, save_nmsrs
++);
787 index
= __find_msr_index(vmx
, MSR_LSTAR
);
789 move_msr_up(vmx
, index
, save_nmsrs
++);
790 index
= __find_msr_index(vmx
, MSR_CSTAR
);
792 move_msr_up(vmx
, index
, save_nmsrs
++);
793 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
795 move_msr_up(vmx
, index
, save_nmsrs
++);
797 * MSR_K6_STAR is only needed on long mode guests, and only
798 * if efer.sce is enabled.
800 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
801 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
802 move_msr_up(vmx
, index
, save_nmsrs
++);
805 vmx
->save_nmsrs
= save_nmsrs
;
808 vmx
->msr_offset_kernel_gs_base
=
809 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
811 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
815 * reads and returns guest's timestamp counter "register"
816 * guest_tsc = host_tsc + tsc_offset -- 21.3
818 static u64
guest_read_tsc(void)
820 u64 host_tsc
, tsc_offset
;
823 tsc_offset
= vmcs_read64(TSC_OFFSET
);
824 return host_tsc
+ tsc_offset
;
828 * writes 'guest_tsc' into guest's timestamp counter "register"
829 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
831 static void guest_write_tsc(u64 guest_tsc
)
836 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
840 * Reads an msr value (of 'msr_index') into 'pdata'.
841 * Returns 0 on success, non-0 otherwise.
842 * Assumes vcpu_load() was already called.
844 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
847 struct kvm_msr_entry
*msr
;
850 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
857 data
= vmcs_readl(GUEST_FS_BASE
);
860 data
= vmcs_readl(GUEST_GS_BASE
);
863 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
865 case MSR_IA32_TIME_STAMP_COUNTER
:
866 data
= guest_read_tsc();
868 case MSR_IA32_SYSENTER_CS
:
869 data
= vmcs_read32(GUEST_SYSENTER_CS
);
871 case MSR_IA32_SYSENTER_EIP
:
872 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
874 case MSR_IA32_SYSENTER_ESP
:
875 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
878 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
883 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
891 * Writes msr value into into the appropriate "register".
892 * Returns 0 on success, non-0 otherwise.
893 * Assumes vcpu_load() was already called.
895 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
897 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
898 struct kvm_msr_entry
*msr
;
904 vmx_load_host_state(vmx
);
905 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
908 vmcs_writel(GUEST_FS_BASE
, data
);
911 vmcs_writel(GUEST_GS_BASE
, data
);
914 case MSR_IA32_SYSENTER_CS
:
915 vmcs_write32(GUEST_SYSENTER_CS
, data
);
917 case MSR_IA32_SYSENTER_EIP
:
918 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
920 case MSR_IA32_SYSENTER_ESP
:
921 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
923 case MSR_IA32_TIME_STAMP_COUNTER
:
924 guest_write_tsc(data
);
926 case MSR_P6_PERFCTR0
:
927 case MSR_P6_PERFCTR1
:
928 case MSR_P6_EVNTSEL0
:
929 case MSR_P6_EVNTSEL1
:
931 * Just discard all writes to the performance counters; this
932 * should keep both older linux and windows 64-bit guests
935 pr_unimpl(vcpu
, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index
, data
);
939 vmx_load_host_state(vmx
);
940 msr
= find_msr_entry(vmx
, msr_index
);
945 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
951 static void vmx_cache_reg(struct kvm_vcpu
*vcpu
, enum kvm_reg reg
)
953 __set_bit(reg
, (unsigned long *)&vcpu
->arch
.regs_avail
);
956 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
959 vcpu
->arch
.regs
[VCPU_REGS_RIP
] = vmcs_readl(GUEST_RIP
);
966 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
968 unsigned long dr7
= 0x400;
971 old_singlestep
= vcpu
->guest_debug
.singlestep
;
973 vcpu
->guest_debug
.enabled
= dbg
->enabled
;
974 if (vcpu
->guest_debug
.enabled
) {
977 dr7
|= 0x200; /* exact */
978 for (i
= 0; i
< 4; ++i
) {
979 if (!dbg
->breakpoints
[i
].enabled
)
981 vcpu
->guest_debug
.bp
[i
] = dbg
->breakpoints
[i
].address
;
982 dr7
|= 2 << (i
*2); /* global enable */
983 dr7
|= 0 << (i
*4+16); /* execution breakpoint */
986 vcpu
->guest_debug
.singlestep
= dbg
->singlestep
;
988 vcpu
->guest_debug
.singlestep
= 0;
990 if (old_singlestep
&& !vcpu
->guest_debug
.singlestep
) {
993 flags
= vmcs_readl(GUEST_RFLAGS
);
994 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
995 vmcs_writel(GUEST_RFLAGS
, flags
);
998 update_exception_bitmap(vcpu
);
999 vmcs_writel(GUEST_DR7
, dr7
);
1004 static int vmx_get_irq(struct kvm_vcpu
*vcpu
)
1006 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1007 u32 idtv_info_field
;
1009 idtv_info_field
= vmx
->idt_vectoring_info
;
1010 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
1011 if (is_external_interrupt(idtv_info_field
))
1012 return idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
1014 printk(KERN_DEBUG
"pending exception: not handled yet\n");
1019 static __init
int cpu_has_kvm_support(void)
1021 unsigned long ecx
= cpuid_ecx(1);
1022 return test_bit(5, &ecx
); /* CPUID.1:ECX.VMX[bit 5] -> VT */
1025 static __init
int vmx_disabled_by_bios(void)
1029 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
1030 return (msr
& (IA32_FEATURE_CONTROL_LOCKED_BIT
|
1031 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT
))
1032 == IA32_FEATURE_CONTROL_LOCKED_BIT
;
1033 /* locked but not enabled */
1036 static void hardware_enable(void *garbage
)
1038 int cpu
= raw_smp_processor_id();
1039 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
1042 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu
, cpu
));
1043 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
1044 if ((old
& (IA32_FEATURE_CONTROL_LOCKED_BIT
|
1045 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT
))
1046 != (IA32_FEATURE_CONTROL_LOCKED_BIT
|
1047 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT
))
1048 /* enable and lock */
1049 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
1050 IA32_FEATURE_CONTROL_LOCKED_BIT
|
1051 IA32_FEATURE_CONTROL_VMXON_ENABLED_BIT
);
1052 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
1053 asm volatile (ASM_VMX_VMXON_RAX
1054 : : "a"(&phys_addr
), "m"(phys_addr
)
1058 static void vmclear_local_vcpus(void)
1060 int cpu
= raw_smp_processor_id();
1061 struct vcpu_vmx
*vmx
, *n
;
1063 list_for_each_entry_safe(vmx
, n
, &per_cpu(vcpus_on_cpu
, cpu
),
1068 static void hardware_disable(void *garbage
)
1070 vmclear_local_vcpus();
1071 asm volatile (__ex(ASM_VMX_VMXOFF
) : : : "cc");
1072 write_cr4(read_cr4() & ~X86_CR4_VMXE
);
1075 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
1076 u32 msr
, u32
*result
)
1078 u32 vmx_msr_low
, vmx_msr_high
;
1079 u32 ctl
= ctl_min
| ctl_opt
;
1081 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
1083 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
1084 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
1086 /* Ensure minimum (required) set of control bits are supported. */
1094 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
1096 u32 vmx_msr_low
, vmx_msr_high
;
1097 u32 min
, opt
, min2
, opt2
;
1098 u32 _pin_based_exec_control
= 0;
1099 u32 _cpu_based_exec_control
= 0;
1100 u32 _cpu_based_2nd_exec_control
= 0;
1101 u32 _vmexit_control
= 0;
1102 u32 _vmentry_control
= 0;
1104 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
1105 opt
= PIN_BASED_VIRTUAL_NMIS
;
1106 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
1107 &_pin_based_exec_control
) < 0)
1110 min
= CPU_BASED_HLT_EXITING
|
1111 #ifdef CONFIG_X86_64
1112 CPU_BASED_CR8_LOAD_EXITING
|
1113 CPU_BASED_CR8_STORE_EXITING
|
1115 CPU_BASED_CR3_LOAD_EXITING
|
1116 CPU_BASED_CR3_STORE_EXITING
|
1117 CPU_BASED_USE_IO_BITMAPS
|
1118 CPU_BASED_MOV_DR_EXITING
|
1119 CPU_BASED_USE_TSC_OFFSETING
;
1120 opt
= CPU_BASED_TPR_SHADOW
|
1121 CPU_BASED_USE_MSR_BITMAPS
|
1122 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1123 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1124 &_cpu_based_exec_control
) < 0)
1126 #ifdef CONFIG_X86_64
1127 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1128 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1129 ~CPU_BASED_CR8_STORE_EXITING
;
1131 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1133 opt2
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1134 SECONDARY_EXEC_WBINVD_EXITING
|
1135 SECONDARY_EXEC_ENABLE_VPID
|
1136 SECONDARY_EXEC_ENABLE_EPT
;
1137 if (adjust_vmx_controls(min2
, opt2
,
1138 MSR_IA32_VMX_PROCBASED_CTLS2
,
1139 &_cpu_based_2nd_exec_control
) < 0)
1142 #ifndef CONFIG_X86_64
1143 if (!(_cpu_based_2nd_exec_control
&
1144 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1145 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1147 if (_cpu_based_2nd_exec_control
& SECONDARY_EXEC_ENABLE_EPT
) {
1148 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1149 min
&= ~(CPU_BASED_CR3_LOAD_EXITING
|
1150 CPU_BASED_CR3_STORE_EXITING
);
1151 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1152 &_cpu_based_exec_control
) < 0)
1154 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP
,
1155 vmx_capability
.ept
, vmx_capability
.vpid
);
1159 #ifdef CONFIG_X86_64
1160 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1163 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1164 &_vmexit_control
) < 0)
1168 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1169 &_vmentry_control
) < 0)
1172 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1174 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1175 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1178 #ifdef CONFIG_X86_64
1179 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1180 if (vmx_msr_high
& (1u<<16))
1184 /* Require Write-Back (WB) memory type for VMCS accesses. */
1185 if (((vmx_msr_high
>> 18) & 15) != 6)
1188 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1189 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1190 vmcs_conf
->revision_id
= vmx_msr_low
;
1192 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1193 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1194 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1195 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1196 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1201 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1203 int node
= cpu_to_node(cpu
);
1207 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1210 vmcs
= page_address(pages
);
1211 memset(vmcs
, 0, vmcs_config
.size
);
1212 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1216 static struct vmcs
*alloc_vmcs(void)
1218 return alloc_vmcs_cpu(raw_smp_processor_id());
1221 static void free_vmcs(struct vmcs
*vmcs
)
1223 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1226 static void free_kvm_area(void)
1230 for_each_online_cpu(cpu
)
1231 free_vmcs(per_cpu(vmxarea
, cpu
));
1234 static __init
int alloc_kvm_area(void)
1238 for_each_online_cpu(cpu
) {
1241 vmcs
= alloc_vmcs_cpu(cpu
);
1247 per_cpu(vmxarea
, cpu
) = vmcs
;
1252 static __init
int hardware_setup(void)
1254 if (setup_vmcs_config(&vmcs_config
) < 0)
1257 if (boot_cpu_has(X86_FEATURE_NX
))
1258 kvm_enable_efer_bits(EFER_NX
);
1260 return alloc_kvm_area();
1263 static __exit
void hardware_unsetup(void)
1268 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1270 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1272 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1273 vmcs_write16(sf
->selector
, save
->selector
);
1274 vmcs_writel(sf
->base
, save
->base
);
1275 vmcs_write32(sf
->limit
, save
->limit
);
1276 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1278 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1280 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1284 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1286 unsigned long flags
;
1288 vcpu
->arch
.rmode
.active
= 0;
1290 vmcs_writel(GUEST_TR_BASE
, vcpu
->arch
.rmode
.tr
.base
);
1291 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->arch
.rmode
.tr
.limit
);
1292 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->arch
.rmode
.tr
.ar
);
1294 flags
= vmcs_readl(GUEST_RFLAGS
);
1295 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1296 flags
|= (vcpu
->arch
.rmode
.save_iopl
<< IOPL_SHIFT
);
1297 vmcs_writel(GUEST_RFLAGS
, flags
);
1299 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1300 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1302 update_exception_bitmap(vcpu
);
1304 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1305 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1306 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1307 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1309 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1310 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1312 vmcs_write16(GUEST_CS_SELECTOR
,
1313 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1314 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1317 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1319 if (!kvm
->arch
.tss_addr
) {
1320 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1321 kvm
->memslots
[0].npages
- 3;
1322 return base_gfn
<< PAGE_SHIFT
;
1324 return kvm
->arch
.tss_addr
;
1327 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1329 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1331 save
->selector
= vmcs_read16(sf
->selector
);
1332 save
->base
= vmcs_readl(sf
->base
);
1333 save
->limit
= vmcs_read32(sf
->limit
);
1334 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1335 vmcs_write16(sf
->selector
, save
->base
>> 4);
1336 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1337 vmcs_write32(sf
->limit
, 0xffff);
1338 vmcs_write32(sf
->ar_bytes
, 0xf3);
1341 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1343 unsigned long flags
;
1345 vcpu
->arch
.rmode
.active
= 1;
1347 vcpu
->arch
.rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1348 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1350 vcpu
->arch
.rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1351 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1353 vcpu
->arch
.rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1354 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1356 flags
= vmcs_readl(GUEST_RFLAGS
);
1357 vcpu
->arch
.rmode
.save_iopl
1358 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1360 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1362 vmcs_writel(GUEST_RFLAGS
, flags
);
1363 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1364 update_exception_bitmap(vcpu
);
1366 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1367 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1368 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1370 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1371 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1372 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1373 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1374 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1376 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1377 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1378 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1379 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1381 kvm_mmu_reset_context(vcpu
);
1382 init_rmode(vcpu
->kvm
);
1385 #ifdef CONFIG_X86_64
1387 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1391 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1392 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1393 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1395 vmcs_write32(GUEST_TR_AR_BYTES
,
1396 (guest_tr_ar
& ~AR_TYPE_MASK
)
1397 | AR_TYPE_BUSY_64_TSS
);
1400 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1402 find_msr_entry(to_vmx(vcpu
), MSR_EFER
)->data
|= EFER_LMA
| EFER_LME
;
1403 vmcs_write32(VM_ENTRY_CONTROLS
,
1404 vmcs_read32(VM_ENTRY_CONTROLS
)
1405 | VM_ENTRY_IA32E_MODE
);
1408 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1410 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1412 vmcs_write32(VM_ENTRY_CONTROLS
,
1413 vmcs_read32(VM_ENTRY_CONTROLS
)
1414 & ~VM_ENTRY_IA32E_MODE
);
1419 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1421 vpid_sync_vcpu_all(to_vmx(vcpu
));
1423 ept_sync_context(construct_eptp(vcpu
->arch
.mmu
.root_hpa
));
1426 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1428 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1429 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1432 static void ept_load_pdptrs(struct kvm_vcpu
*vcpu
)
1434 if (is_paging(vcpu
) && is_pae(vcpu
) && !is_long_mode(vcpu
)) {
1435 if (!load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
1436 printk(KERN_ERR
"EPT: Fail to load pdptrs!\n");
1439 vmcs_write64(GUEST_PDPTR0
, vcpu
->arch
.pdptrs
[0]);
1440 vmcs_write64(GUEST_PDPTR1
, vcpu
->arch
.pdptrs
[1]);
1441 vmcs_write64(GUEST_PDPTR2
, vcpu
->arch
.pdptrs
[2]);
1442 vmcs_write64(GUEST_PDPTR3
, vcpu
->arch
.pdptrs
[3]);
1446 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
);
1448 static void ept_update_paging_mode_cr0(unsigned long *hw_cr0
,
1450 struct kvm_vcpu
*vcpu
)
1452 if (!(cr0
& X86_CR0_PG
)) {
1453 /* From paging/starting to nonpaging */
1454 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1455 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) |
1456 (CPU_BASED_CR3_LOAD_EXITING
|
1457 CPU_BASED_CR3_STORE_EXITING
));
1458 vcpu
->arch
.cr0
= cr0
;
1459 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1460 *hw_cr0
|= X86_CR0_PE
| X86_CR0_PG
;
1461 *hw_cr0
&= ~X86_CR0_WP
;
1462 } else if (!is_paging(vcpu
)) {
1463 /* From nonpaging to paging */
1464 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
,
1465 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
) &
1466 ~(CPU_BASED_CR3_LOAD_EXITING
|
1467 CPU_BASED_CR3_STORE_EXITING
));
1468 vcpu
->arch
.cr0
= cr0
;
1469 vmx_set_cr4(vcpu
, vcpu
->arch
.cr4
);
1470 if (!(vcpu
->arch
.cr0
& X86_CR0_WP
))
1471 *hw_cr0
&= ~X86_CR0_WP
;
1475 static void ept_update_paging_mode_cr4(unsigned long *hw_cr4
,
1476 struct kvm_vcpu
*vcpu
)
1478 if (!is_paging(vcpu
)) {
1479 *hw_cr4
&= ~X86_CR4_PAE
;
1480 *hw_cr4
|= X86_CR4_PSE
;
1481 } else if (!(vcpu
->arch
.cr4
& X86_CR4_PAE
))
1482 *hw_cr4
&= ~X86_CR4_PAE
;
1485 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1487 unsigned long hw_cr0
= (cr0
& ~KVM_GUEST_CR0_MASK
) |
1488 KVM_VM_CR0_ALWAYS_ON
;
1490 vmx_fpu_deactivate(vcpu
);
1492 if (vcpu
->arch
.rmode
.active
&& (cr0
& X86_CR0_PE
))
1495 if (!vcpu
->arch
.rmode
.active
&& !(cr0
& X86_CR0_PE
))
1498 #ifdef CONFIG_X86_64
1499 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1500 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1502 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1508 ept_update_paging_mode_cr0(&hw_cr0
, cr0
, vcpu
);
1510 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1511 vmcs_writel(GUEST_CR0
, hw_cr0
);
1512 vcpu
->arch
.cr0
= cr0
;
1514 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1515 vmx_fpu_activate(vcpu
);
1518 static u64
construct_eptp(unsigned long root_hpa
)
1522 /* TODO write the value reading from MSR */
1523 eptp
= VMX_EPT_DEFAULT_MT
|
1524 VMX_EPT_DEFAULT_GAW
<< VMX_EPT_GAW_EPTP_SHIFT
;
1525 eptp
|= (root_hpa
& PAGE_MASK
);
1530 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1532 unsigned long guest_cr3
;
1536 if (vm_need_ept()) {
1537 eptp
= construct_eptp(cr3
);
1538 vmcs_write64(EPT_POINTER
, eptp
);
1539 ept_sync_context(eptp
);
1540 ept_load_pdptrs(vcpu
);
1541 guest_cr3
= is_paging(vcpu
) ? vcpu
->arch
.cr3
:
1542 VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
1545 vmx_flush_tlb(vcpu
);
1546 vmcs_writel(GUEST_CR3
, guest_cr3
);
1547 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1548 vmx_fpu_deactivate(vcpu
);
1551 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1553 unsigned long hw_cr4
= cr4
| (vcpu
->arch
.rmode
.active
?
1554 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
);
1556 vcpu
->arch
.cr4
= cr4
;
1558 ept_update_paging_mode_cr4(&hw_cr4
, vcpu
);
1560 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1561 vmcs_writel(GUEST_CR4
, hw_cr4
);
1564 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1566 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1567 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1569 vcpu
->arch
.shadow_efer
= efer
;
1572 if (efer
& EFER_LMA
) {
1573 vmcs_write32(VM_ENTRY_CONTROLS
,
1574 vmcs_read32(VM_ENTRY_CONTROLS
) |
1575 VM_ENTRY_IA32E_MODE
);
1579 vmcs_write32(VM_ENTRY_CONTROLS
,
1580 vmcs_read32(VM_ENTRY_CONTROLS
) &
1581 ~VM_ENTRY_IA32E_MODE
);
1583 msr
->data
= efer
& ~EFER_LME
;
1588 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1590 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1592 return vmcs_readl(sf
->base
);
1595 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1596 struct kvm_segment
*var
, int seg
)
1598 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1601 var
->base
= vmcs_readl(sf
->base
);
1602 var
->limit
= vmcs_read32(sf
->limit
);
1603 var
->selector
= vmcs_read16(sf
->selector
);
1604 ar
= vmcs_read32(sf
->ar_bytes
);
1605 if (ar
& AR_UNUSABLE_MASK
)
1607 var
->type
= ar
& 15;
1608 var
->s
= (ar
>> 4) & 1;
1609 var
->dpl
= (ar
>> 5) & 3;
1610 var
->present
= (ar
>> 7) & 1;
1611 var
->avl
= (ar
>> 12) & 1;
1612 var
->l
= (ar
>> 13) & 1;
1613 var
->db
= (ar
>> 14) & 1;
1614 var
->g
= (ar
>> 15) & 1;
1615 var
->unusable
= (ar
>> 16) & 1;
1618 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1620 struct kvm_segment kvm_seg
;
1622 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1625 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1628 vmx_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_CS
);
1629 return kvm_seg
.selector
& 3;
1632 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1639 ar
= var
->type
& 15;
1640 ar
|= (var
->s
& 1) << 4;
1641 ar
|= (var
->dpl
& 3) << 5;
1642 ar
|= (var
->present
& 1) << 7;
1643 ar
|= (var
->avl
& 1) << 12;
1644 ar
|= (var
->l
& 1) << 13;
1645 ar
|= (var
->db
& 1) << 14;
1646 ar
|= (var
->g
& 1) << 15;
1648 if (ar
== 0) /* a 0 value means unusable */
1649 ar
= AR_UNUSABLE_MASK
;
1654 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1655 struct kvm_segment
*var
, int seg
)
1657 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1660 if (vcpu
->arch
.rmode
.active
&& seg
== VCPU_SREG_TR
) {
1661 vcpu
->arch
.rmode
.tr
.selector
= var
->selector
;
1662 vcpu
->arch
.rmode
.tr
.base
= var
->base
;
1663 vcpu
->arch
.rmode
.tr
.limit
= var
->limit
;
1664 vcpu
->arch
.rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1667 vmcs_writel(sf
->base
, var
->base
);
1668 vmcs_write32(sf
->limit
, var
->limit
);
1669 vmcs_write16(sf
->selector
, var
->selector
);
1670 if (vcpu
->arch
.rmode
.active
&& var
->s
) {
1672 * Hack real-mode segments into vm86 compatibility.
1674 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1675 vmcs_writel(sf
->base
, 0xf0000);
1678 ar
= vmx_segment_access_rights(var
);
1679 vmcs_write32(sf
->ar_bytes
, ar
);
1682 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1684 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1686 *db
= (ar
>> 14) & 1;
1687 *l
= (ar
>> 13) & 1;
1690 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1692 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1693 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1696 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1698 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1699 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1702 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1704 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1705 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1708 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1710 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1711 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1714 static int init_rmode_tss(struct kvm
*kvm
)
1716 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
1721 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1724 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
1725 r
= kvm_write_guest_page(kvm
, fn
++, &data
, 0x66, sizeof(u16
));
1728 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
1731 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1735 r
= kvm_write_guest_page(kvm
, fn
, &data
,
1736 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
1746 static int init_rmode_identity_map(struct kvm
*kvm
)
1749 pfn_t identity_map_pfn
;
1754 if (unlikely(!kvm
->arch
.ept_identity_pagetable
)) {
1755 printk(KERN_ERR
"EPT: identity-mapping pagetable "
1756 "haven't been allocated!\n");
1759 if (likely(kvm
->arch
.ept_identity_pagetable_done
))
1762 identity_map_pfn
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
;
1763 r
= kvm_clear_guest_page(kvm
, identity_map_pfn
, 0, PAGE_SIZE
);
1766 /* Set up identity-mapping pagetable for EPT in real mode */
1767 for (i
= 0; i
< PT32_ENT_PER_PAGE
; i
++) {
1768 tmp
= (i
<< 22) + (_PAGE_PRESENT
| _PAGE_RW
| _PAGE_USER
|
1769 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_PSE
);
1770 r
= kvm_write_guest_page(kvm
, identity_map_pfn
,
1771 &tmp
, i
* sizeof(tmp
), sizeof(tmp
));
1775 kvm
->arch
.ept_identity_pagetable_done
= true;
1781 static void seg_setup(int seg
)
1783 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1785 vmcs_write16(sf
->selector
, 0);
1786 vmcs_writel(sf
->base
, 0);
1787 vmcs_write32(sf
->limit
, 0xffff);
1788 vmcs_write32(sf
->ar_bytes
, 0x93);
1791 static int alloc_apic_access_page(struct kvm
*kvm
)
1793 struct kvm_userspace_memory_region kvm_userspace_mem
;
1796 down_write(&kvm
->slots_lock
);
1797 if (kvm
->arch
.apic_access_page
)
1799 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
1800 kvm_userspace_mem
.flags
= 0;
1801 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
1802 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
1803 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1807 down_read(¤t
->mm
->mmap_sem
);
1808 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
1809 up_read(¤t
->mm
->mmap_sem
);
1811 up_write(&kvm
->slots_lock
);
1815 static int alloc_identity_pagetable(struct kvm
*kvm
)
1817 struct kvm_userspace_memory_region kvm_userspace_mem
;
1820 down_write(&kvm
->slots_lock
);
1821 if (kvm
->arch
.ept_identity_pagetable
)
1823 kvm_userspace_mem
.slot
= IDENTITY_PAGETABLE_PRIVATE_MEMSLOT
;
1824 kvm_userspace_mem
.flags
= 0;
1825 kvm_userspace_mem
.guest_phys_addr
= VMX_EPT_IDENTITY_PAGETABLE_ADDR
;
1826 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
1827 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1831 down_read(¤t
->mm
->mmap_sem
);
1832 kvm
->arch
.ept_identity_pagetable
= gfn_to_page(kvm
,
1833 VMX_EPT_IDENTITY_PAGETABLE_ADDR
>> PAGE_SHIFT
);
1834 up_read(¤t
->mm
->mmap_sem
);
1836 up_write(&kvm
->slots_lock
);
1840 static void allocate_vpid(struct vcpu_vmx
*vmx
)
1845 if (!enable_vpid
|| !cpu_has_vmx_vpid())
1847 spin_lock(&vmx_vpid_lock
);
1848 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
1849 if (vpid
< VMX_NR_VPIDS
) {
1851 __set_bit(vpid
, vmx_vpid_bitmap
);
1853 spin_unlock(&vmx_vpid_lock
);
1856 static void vmx_disable_intercept_for_msr(struct page
*msr_bitmap
, u32 msr
)
1860 if (!cpu_has_vmx_msr_bitmap())
1864 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1865 * have the write-low and read-high bitmap offsets the wrong way round.
1866 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1868 va
= kmap(msr_bitmap
);
1869 if (msr
<= 0x1fff) {
1870 __clear_bit(msr
, va
+ 0x000); /* read-low */
1871 __clear_bit(msr
, va
+ 0x800); /* write-low */
1872 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1874 __clear_bit(msr
, va
+ 0x400); /* read-high */
1875 __clear_bit(msr
, va
+ 0xc00); /* write-high */
1881 * Sets up the vmcs for emulated real mode.
1883 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
1885 u32 host_sysenter_cs
;
1888 struct descriptor_table dt
;
1890 unsigned long kvm_vmx_return
;
1894 vmcs_write64(IO_BITMAP_A
, page_to_phys(vmx_io_bitmap_a
));
1895 vmcs_write64(IO_BITMAP_B
, page_to_phys(vmx_io_bitmap_b
));
1897 if (cpu_has_vmx_msr_bitmap())
1898 vmcs_write64(MSR_BITMAP
, page_to_phys(vmx_msr_bitmap
));
1900 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
1903 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
1904 vmcs_config
.pin_based_exec_ctrl
);
1906 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
1907 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
1908 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1909 #ifdef CONFIG_X86_64
1910 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
1911 CPU_BASED_CR8_LOAD_EXITING
;
1915 exec_control
|= CPU_BASED_CR3_STORE_EXITING
|
1916 CPU_BASED_CR3_LOAD_EXITING
;
1917 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
1919 if (cpu_has_secondary_exec_ctrls()) {
1920 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
1921 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
1923 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
1925 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
1927 exec_control
&= ~SECONDARY_EXEC_ENABLE_EPT
;
1928 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
1931 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
1932 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
1933 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
1935 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
1936 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
1937 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1939 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
1940 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1941 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1942 vmcs_write16(HOST_FS_SELECTOR
, kvm_read_fs()); /* 22.2.4 */
1943 vmcs_write16(HOST_GS_SELECTOR
, kvm_read_gs()); /* 22.2.4 */
1944 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1945 #ifdef CONFIG_X86_64
1946 rdmsrl(MSR_FS_BASE
, a
);
1947 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
1948 rdmsrl(MSR_GS_BASE
, a
);
1949 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
1951 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
1952 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
1955 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
1958 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
1960 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
1961 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
1962 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
1963 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
1964 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
1966 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
1967 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
1968 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
1969 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
1970 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
1971 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
1973 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
1974 u32 index
= vmx_msr_index
[i
];
1975 u32 data_low
, data_high
;
1979 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
1981 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
1983 data
= data_low
| ((u64
)data_high
<< 32);
1984 vmx
->host_msrs
[j
].index
= index
;
1985 vmx
->host_msrs
[j
].reserved
= 0;
1986 vmx
->host_msrs
[j
].data
= data
;
1987 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
1991 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
1993 /* 22.2.1, 20.8.1 */
1994 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
1996 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
1997 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
2003 static int init_rmode(struct kvm
*kvm
)
2005 if (!init_rmode_tss(kvm
))
2007 if (!init_rmode_identity_map(kvm
))
2012 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
2014 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2018 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
2019 down_read(&vcpu
->kvm
->slots_lock
);
2020 if (!init_rmode(vmx
->vcpu
.kvm
)) {
2025 vmx
->vcpu
.arch
.rmode
.active
= 0;
2027 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
2028 kvm_set_cr8(&vmx
->vcpu
, 0);
2029 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
2030 if (vmx
->vcpu
.vcpu_id
== 0)
2031 msr
|= MSR_IA32_APICBASE_BSP
;
2032 kvm_set_apic_base(&vmx
->vcpu
, msr
);
2034 fx_init(&vmx
->vcpu
);
2037 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2038 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2040 if (vmx
->vcpu
.vcpu_id
== 0) {
2041 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
2042 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
2044 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
2045 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
2047 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
2048 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
2050 seg_setup(VCPU_SREG_DS
);
2051 seg_setup(VCPU_SREG_ES
);
2052 seg_setup(VCPU_SREG_FS
);
2053 seg_setup(VCPU_SREG_GS
);
2054 seg_setup(VCPU_SREG_SS
);
2056 vmcs_write16(GUEST_TR_SELECTOR
, 0);
2057 vmcs_writel(GUEST_TR_BASE
, 0);
2058 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
2059 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
2061 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
2062 vmcs_writel(GUEST_LDTR_BASE
, 0);
2063 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
2064 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
2066 vmcs_write32(GUEST_SYSENTER_CS
, 0);
2067 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
2068 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
2070 vmcs_writel(GUEST_RFLAGS
, 0x02);
2071 if (vmx
->vcpu
.vcpu_id
== 0)
2072 kvm_rip_write(vcpu
, 0xfff0);
2074 kvm_rip_write(vcpu
, 0);
2075 kvm_register_write(vcpu
, VCPU_REGS_RSP
, 0);
2077 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
2078 vmcs_writel(GUEST_DR7
, 0x400);
2080 vmcs_writel(GUEST_GDTR_BASE
, 0);
2081 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
2083 vmcs_writel(GUEST_IDTR_BASE
, 0);
2084 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
2086 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
2087 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
2088 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
2092 /* Special registers */
2093 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
2097 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
2099 if (cpu_has_vmx_tpr_shadow()) {
2100 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
2101 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
2102 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
2103 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
2104 vmcs_write32(TPR_THRESHOLD
, 0);
2107 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
2108 vmcs_write64(APIC_ACCESS_ADDR
,
2109 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
2112 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
2114 vmx
->vcpu
.arch
.cr0
= 0x60000010;
2115 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
2116 vmx_set_cr4(&vmx
->vcpu
, 0);
2117 vmx_set_efer(&vmx
->vcpu
, 0);
2118 vmx_fpu_activate(&vmx
->vcpu
);
2119 update_exception_bitmap(&vmx
->vcpu
);
2121 vpid_sync_vcpu_all(vmx
);
2126 up_read(&vcpu
->kvm
->slots_lock
);
2130 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
, int irq
)
2132 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2134 KVMTRACE_1D(INJ_VIRQ
, vcpu
, (u32
)irq
, handler
);
2136 if (vcpu
->arch
.rmode
.active
) {
2137 vmx
->rmode
.irq
.pending
= true;
2138 vmx
->rmode
.irq
.vector
= irq
;
2139 vmx
->rmode
.irq
.rip
= kvm_rip_read(vcpu
);
2140 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2141 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
2142 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
2143 kvm_rip_write(vcpu
, vmx
->rmode
.irq
.rip
- 1);
2146 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2147 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
2150 static void vmx_inject_nmi(struct kvm_vcpu
*vcpu
)
2152 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2153 INTR_TYPE_NMI_INTR
| INTR_INFO_VALID_MASK
| NMI_VECTOR
);
2154 vcpu
->arch
.nmi_pending
= 0;
2157 static void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
2159 int word_index
= __ffs(vcpu
->arch
.irq_summary
);
2160 int bit_index
= __ffs(vcpu
->arch
.irq_pending
[word_index
]);
2161 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
2163 clear_bit(bit_index
, &vcpu
->arch
.irq_pending
[word_index
]);
2164 if (!vcpu
->arch
.irq_pending
[word_index
])
2165 clear_bit(word_index
, &vcpu
->arch
.irq_summary
);
2166 vmx_inject_irq(vcpu
, irq
);
2170 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
2171 struct kvm_run
*kvm_run
)
2173 u32 cpu_based_vm_exec_control
;
2175 vcpu
->arch
.interrupt_window_open
=
2176 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2177 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
2179 if (vcpu
->arch
.interrupt_window_open
&&
2180 vcpu
->arch
.irq_summary
&&
2181 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
) & INTR_INFO_VALID_MASK
))
2183 * If interrupts enabled, and not blocked by sti or mov ss. Good.
2185 kvm_do_inject_irq(vcpu
);
2187 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2188 if (!vcpu
->arch
.interrupt_window_open
&&
2189 (vcpu
->arch
.irq_summary
|| kvm_run
->request_interrupt_window
))
2191 * Interrupts blocked. Wait for unblock.
2193 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2195 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2196 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2199 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
2202 struct kvm_userspace_memory_region tss_mem
= {
2204 .guest_phys_addr
= addr
,
2205 .memory_size
= PAGE_SIZE
* 3,
2209 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
2212 kvm
->arch
.tss_addr
= addr
;
2216 static void kvm_guest_debug_pre(struct kvm_vcpu
*vcpu
)
2218 struct kvm_guest_debug
*dbg
= &vcpu
->guest_debug
;
2220 set_debugreg(dbg
->bp
[0], 0);
2221 set_debugreg(dbg
->bp
[1], 1);
2222 set_debugreg(dbg
->bp
[2], 2);
2223 set_debugreg(dbg
->bp
[3], 3);
2225 if (dbg
->singlestep
) {
2226 unsigned long flags
;
2228 flags
= vmcs_readl(GUEST_RFLAGS
);
2229 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
2230 vmcs_writel(GUEST_RFLAGS
, flags
);
2234 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
2235 int vec
, u32 err_code
)
2237 if (!vcpu
->arch
.rmode
.active
)
2241 * Instruction with address size override prefix opcode 0x67
2242 * Cause the #SS fault with 0 error code in VM86 mode.
2244 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
2245 if (emulate_instruction(vcpu
, NULL
, 0, 0, 0) == EMULATE_DONE
)
2250 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2252 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2253 u32 intr_info
, error_code
;
2254 unsigned long cr2
, rip
;
2256 enum emulation_result er
;
2258 vect_info
= vmx
->idt_vectoring_info
;
2259 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2261 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
2262 !is_page_fault(intr_info
))
2263 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
2264 "intr info 0x%x\n", __func__
, vect_info
, intr_info
);
2266 if (!irqchip_in_kernel(vcpu
->kvm
) && is_external_interrupt(vect_info
)) {
2267 int irq
= vect_info
& VECTORING_INFO_VECTOR_MASK
;
2268 set_bit(irq
, vcpu
->arch
.irq_pending
);
2269 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
2272 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) /* nmi */
2273 return 1; /* already handled by vmx_vcpu_run() */
2275 if (is_no_device(intr_info
)) {
2276 vmx_fpu_activate(vcpu
);
2280 if (is_invalid_opcode(intr_info
)) {
2281 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
2282 if (er
!= EMULATE_DONE
)
2283 kvm_queue_exception(vcpu
, UD_VECTOR
);
2288 rip
= kvm_rip_read(vcpu
);
2289 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
2290 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
2291 if (is_page_fault(intr_info
)) {
2292 /* EPT won't cause page fault directly */
2295 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
2296 KVMTRACE_3D(PAGE_FAULT
, vcpu
, error_code
, (u32
)cr2
,
2297 (u32
)((u64
)cr2
>> 32), handler
);
2298 if (vect_info
& VECTORING_INFO_VALID_MASK
)
2299 kvm_mmu_unprotect_page_virt(vcpu
, cr2
);
2300 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2303 if (vcpu
->arch
.rmode
.active
&&
2304 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2306 if (vcpu
->arch
.halt_request
) {
2307 vcpu
->arch
.halt_request
= 0;
2308 return kvm_emulate_halt(vcpu
);
2313 if ((intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
)) ==
2314 (INTR_TYPE_EXCEPTION
| 1)) {
2315 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2318 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2319 kvm_run
->ex
.exception
= intr_info
& INTR_INFO_VECTOR_MASK
;
2320 kvm_run
->ex
.error_code
= error_code
;
2324 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2325 struct kvm_run
*kvm_run
)
2327 ++vcpu
->stat
.irq_exits
;
2328 KVMTRACE_1D(INTR
, vcpu
, vmcs_read32(VM_EXIT_INTR_INFO
), handler
);
2332 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2334 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2338 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2340 unsigned long exit_qualification
;
2341 int size
, down
, in
, string
, rep
;
2344 ++vcpu
->stat
.io_exits
;
2345 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2346 string
= (exit_qualification
& 16) != 0;
2349 if (emulate_instruction(vcpu
,
2350 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2355 size
= (exit_qualification
& 7) + 1;
2356 in
= (exit_qualification
& 8) != 0;
2357 down
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_DF
) != 0;
2358 rep
= (exit_qualification
& 32) != 0;
2359 port
= exit_qualification
>> 16;
2361 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2365 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2368 * Patch in the VMCALL instruction:
2370 hypercall
[0] = 0x0f;
2371 hypercall
[1] = 0x01;
2372 hypercall
[2] = 0xc1;
2375 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2377 unsigned long exit_qualification
;
2381 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2382 cr
= exit_qualification
& 15;
2383 reg
= (exit_qualification
>> 8) & 15;
2384 switch ((exit_qualification
>> 4) & 3) {
2385 case 0: /* mov to cr */
2386 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
,
2387 (u32
)kvm_register_read(vcpu
, reg
),
2388 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2392 kvm_set_cr0(vcpu
, kvm_register_read(vcpu
, reg
));
2393 skip_emulated_instruction(vcpu
);
2396 kvm_set_cr3(vcpu
, kvm_register_read(vcpu
, reg
));
2397 skip_emulated_instruction(vcpu
);
2400 kvm_set_cr4(vcpu
, kvm_register_read(vcpu
, reg
));
2401 skip_emulated_instruction(vcpu
);
2404 kvm_set_cr8(vcpu
, kvm_register_read(vcpu
, reg
));
2405 skip_emulated_instruction(vcpu
);
2406 if (irqchip_in_kernel(vcpu
->kvm
))
2408 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2413 vmx_fpu_deactivate(vcpu
);
2414 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2415 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2416 vmx_fpu_activate(vcpu
);
2417 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2418 skip_emulated_instruction(vcpu
);
2420 case 1: /*mov from cr*/
2423 kvm_register_write(vcpu
, reg
, vcpu
->arch
.cr3
);
2424 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
,
2425 (u32
)kvm_register_read(vcpu
, reg
),
2426 (u32
)((u64
)kvm_register_read(vcpu
, reg
) >> 32),
2428 skip_emulated_instruction(vcpu
);
2431 kvm_register_write(vcpu
, reg
, kvm_get_cr8(vcpu
));
2432 KVMTRACE_2D(CR_READ
, vcpu
, (u32
)cr
,
2433 (u32
)kvm_register_read(vcpu
, reg
), handler
);
2434 skip_emulated_instruction(vcpu
);
2439 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2441 skip_emulated_instruction(vcpu
);
2446 kvm_run
->exit_reason
= 0;
2447 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2448 (int)(exit_qualification
>> 4) & 3, cr
);
2452 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2454 unsigned long exit_qualification
;
2459 * FIXME: this code assumes the host is debugging the guest.
2460 * need to deal with guest debugging itself too.
2462 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2463 dr
= exit_qualification
& 7;
2464 reg
= (exit_qualification
>> 8) & 15;
2465 if (exit_qualification
& 16) {
2477 kvm_register_write(vcpu
, reg
, val
);
2478 KVMTRACE_2D(DR_READ
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2482 skip_emulated_instruction(vcpu
);
2486 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2488 kvm_emulate_cpuid(vcpu
);
2492 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2494 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2497 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
2498 kvm_inject_gp(vcpu
, 0);
2502 KVMTRACE_3D(MSR_READ
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2505 /* FIXME: handling of bits 32:63 of rax, rdx */
2506 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
2507 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
2508 skip_emulated_instruction(vcpu
);
2512 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2514 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2515 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
2516 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2518 KVMTRACE_3D(MSR_WRITE
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2521 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
2522 kvm_inject_gp(vcpu
, 0);
2526 skip_emulated_instruction(vcpu
);
2530 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
2531 struct kvm_run
*kvm_run
)
2536 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
2537 struct kvm_run
*kvm_run
)
2539 u32 cpu_based_vm_exec_control
;
2541 /* clear pending irq */
2542 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2543 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2544 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2546 KVMTRACE_0D(PEND_INTR
, vcpu
, handler
);
2549 * If the user space waits to inject interrupts, exit as soon as
2552 if (kvm_run
->request_interrupt_window
&&
2553 !vcpu
->arch
.irq_summary
) {
2554 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2555 ++vcpu
->stat
.irq_window_exits
;
2561 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2563 skip_emulated_instruction(vcpu
);
2564 return kvm_emulate_halt(vcpu
);
2567 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2569 skip_emulated_instruction(vcpu
);
2570 kvm_emulate_hypercall(vcpu
);
2574 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2576 skip_emulated_instruction(vcpu
);
2577 /* TODO: Add support for VT-d/pass-through device */
2581 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2583 u64 exit_qualification
;
2584 enum emulation_result er
;
2585 unsigned long offset
;
2587 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2588 offset
= exit_qualification
& 0xffful
;
2590 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
2592 if (er
!= EMULATE_DONE
) {
2594 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2601 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2603 unsigned long exit_qualification
;
2607 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2609 reason
= (u32
)exit_qualification
>> 30;
2610 tss_selector
= exit_qualification
;
2612 return kvm_task_switch(vcpu
, tss_selector
, reason
);
2615 static int handle_ept_violation(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2617 u64 exit_qualification
;
2618 enum emulation_result er
;
2624 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2626 if (exit_qualification
& (1 << 6)) {
2627 printk(KERN_ERR
"EPT: GPA exceeds GAW!\n");
2631 gla_validity
= (exit_qualification
>> 7) & 0x3;
2632 if (gla_validity
!= 0x3 && gla_validity
!= 0x1 && gla_validity
!= 0) {
2633 printk(KERN_ERR
"EPT: Handling EPT violation failed!\n");
2634 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2635 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
2636 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS
));
2637 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
2638 (long unsigned int)exit_qualification
);
2639 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
2640 kvm_run
->hw
.hardware_exit_reason
= 0;
2644 gpa
= vmcs_read64(GUEST_PHYSICAL_ADDRESS
);
2645 hva
= gfn_to_hva(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2646 if (!kvm_is_error_hva(hva
)) {
2647 r
= kvm_mmu_page_fault(vcpu
, gpa
& PAGE_MASK
, 0);
2649 printk(KERN_ERR
"EPT: Not enough memory!\n");
2655 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
2657 if (er
== EMULATE_FAIL
) {
2659 "EPT: Fail to handle EPT violation vmexit!er is %d\n",
2661 printk(KERN_ERR
"EPT: GPA: 0x%lx, GVA: 0x%lx\n",
2662 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS
),
2663 (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS
));
2664 printk(KERN_ERR
"EPT: Exit qualification is 0x%lx\n",
2665 (long unsigned int)exit_qualification
);
2667 } else if (er
== EMULATE_DO_MMIO
)
2673 static int handle_nmi_window(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2675 u32 cpu_based_vm_exec_control
;
2677 /* clear pending NMI */
2678 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2679 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_NMI_PENDING
;
2680 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2681 ++vcpu
->stat
.nmi_window_exits
;
2687 * The exit handlers return 1 if the exit was handled fully and guest execution
2688 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2689 * to be done to userspace and return 0.
2691 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
2692 struct kvm_run
*kvm_run
) = {
2693 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
2694 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
2695 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
2696 [EXIT_REASON_NMI_WINDOW
] = handle_nmi_window
,
2697 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
2698 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
2699 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
2700 [EXIT_REASON_CPUID
] = handle_cpuid
,
2701 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
2702 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
2703 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
2704 [EXIT_REASON_HLT
] = handle_halt
,
2705 [EXIT_REASON_VMCALL
] = handle_vmcall
,
2706 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
2707 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
2708 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
2709 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
2710 [EXIT_REASON_EPT_VIOLATION
] = handle_ept_violation
,
2713 static const int kvm_vmx_max_exit_handlers
=
2714 ARRAY_SIZE(kvm_vmx_exit_handlers
);
2717 * The guest has exited. See if we can fix it or if we need userspace
2720 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
2722 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
2723 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2724 u32 vectoring_info
= vmx
->idt_vectoring_info
;
2726 KVMTRACE_3D(VMEXIT
, vcpu
, exit_reason
, (u32
)kvm_rip_read(vcpu
),
2727 (u32
)((u64
)kvm_rip_read(vcpu
) >> 32), entryexit
);
2729 /* Access CR3 don't cause VMExit in paging mode, so we need
2730 * to sync with guest real CR3. */
2731 if (vm_need_ept() && is_paging(vcpu
)) {
2732 vcpu
->arch
.cr3
= vmcs_readl(GUEST_CR3
);
2733 ept_load_pdptrs(vcpu
);
2736 if (unlikely(vmx
->fail
)) {
2737 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
2738 kvm_run
->fail_entry
.hardware_entry_failure_reason
2739 = vmcs_read32(VM_INSTRUCTION_ERROR
);
2743 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
2744 (exit_reason
!= EXIT_REASON_EXCEPTION_NMI
&&
2745 exit_reason
!= EXIT_REASON_EPT_VIOLATION
))
2746 printk(KERN_WARNING
"%s: unexpected, valid vectoring info and "
2747 "exit reason is 0x%x\n", __func__
, exit_reason
);
2748 if (exit_reason
< kvm_vmx_max_exit_handlers
2749 && kvm_vmx_exit_handlers
[exit_reason
])
2750 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
2752 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
2753 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
2758 static void update_tpr_threshold(struct kvm_vcpu
*vcpu
)
2762 if (!vm_need_tpr_shadow(vcpu
->kvm
))
2765 if (!kvm_lapic_enabled(vcpu
) ||
2766 ((max_irr
= kvm_lapic_find_highest_irr(vcpu
)) == -1)) {
2767 vmcs_write32(TPR_THRESHOLD
, 0);
2771 tpr
= (kvm_lapic_get_cr8(vcpu
) & 0x0f) << 4;
2772 vmcs_write32(TPR_THRESHOLD
, (max_irr
> tpr
) ? tpr
>> 4 : max_irr
>> 4);
2775 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2777 u32 cpu_based_vm_exec_control
;
2779 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2780 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2781 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2784 static void enable_nmi_window(struct kvm_vcpu
*vcpu
)
2786 u32 cpu_based_vm_exec_control
;
2788 if (!cpu_has_virtual_nmis())
2791 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2792 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_NMI_PENDING
;
2793 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2796 static int vmx_nmi_enabled(struct kvm_vcpu
*vcpu
)
2798 u32 guest_intr
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2799 return !(guest_intr
& (GUEST_INTR_STATE_NMI
|
2800 GUEST_INTR_STATE_MOV_SS
|
2801 GUEST_INTR_STATE_STI
));
2804 static int vmx_irq_enabled(struct kvm_vcpu
*vcpu
)
2806 u32 guest_intr
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
2807 return (!(guest_intr
& (GUEST_INTR_STATE_MOV_SS
|
2808 GUEST_INTR_STATE_STI
)) &&
2809 (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
));
2812 static void enable_intr_window(struct kvm_vcpu
*vcpu
)
2814 if (vcpu
->arch
.nmi_pending
)
2815 enable_nmi_window(vcpu
);
2816 else if (kvm_cpu_has_interrupt(vcpu
))
2817 enable_irq_window(vcpu
);
2820 static void vmx_intr_assist(struct kvm_vcpu
*vcpu
)
2822 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2823 u32 idtv_info_field
, intr_info_field
, exit_intr_info_field
;
2826 update_tpr_threshold(vcpu
);
2828 intr_info_field
= vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
);
2829 exit_intr_info_field
= vmcs_read32(VM_EXIT_INTR_INFO
);
2830 idtv_info_field
= vmx
->idt_vectoring_info
;
2831 if (intr_info_field
& INTR_INFO_VALID_MASK
) {
2832 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
2833 /* TODO: fault when IDT_Vectoring */
2834 if (printk_ratelimit())
2835 printk(KERN_ERR
"Fault when IDT_Vectoring\n");
2837 enable_intr_window(vcpu
);
2840 if (unlikely(idtv_info_field
& INTR_INFO_VALID_MASK
)) {
2841 if ((idtv_info_field
& VECTORING_INFO_TYPE_MASK
)
2842 == INTR_TYPE_EXT_INTR
2843 && vcpu
->arch
.rmode
.active
) {
2844 u8 vect
= idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
2846 vmx_inject_irq(vcpu
, vect
);
2847 enable_intr_window(vcpu
);
2851 KVMTRACE_1D(REDELIVER_EVT
, vcpu
, idtv_info_field
, handler
);
2855 * Clear bit "block by NMI" before VM entry if a NMI delivery
2858 if ((idtv_info_field
& VECTORING_INFO_TYPE_MASK
)
2859 == INTR_TYPE_NMI_INTR
&& cpu_has_virtual_nmis())
2860 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
2861 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
2862 ~GUEST_INTR_STATE_NMI
);
2864 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, idtv_info_field
2865 & ~INTR_INFO_RESVD_BITS_MASK
);
2866 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2867 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
));
2869 if (unlikely(idtv_info_field
& INTR_INFO_DELIVER_CODE_MASK
))
2870 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
,
2871 vmcs_read32(IDT_VECTORING_ERROR_CODE
));
2872 enable_intr_window(vcpu
);
2875 if (cpu_has_virtual_nmis()) {
2878 * Re-set bit "block by NMI" before VM entry if vmexit caused by
2879 * a guest IRET fault.
2881 if ((exit_intr_info_field
& INTR_INFO_UNBLOCK_NMI
) &&
2882 (exit_intr_info_field
& INTR_INFO_VECTOR_MASK
) != 8)
2883 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
2884 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) |
2885 GUEST_INTR_STATE_NMI
);
2886 else if (vcpu
->arch
.nmi_pending
) {
2887 if (vmx_nmi_enabled(vcpu
))
2888 vmx_inject_nmi(vcpu
);
2889 enable_intr_window(vcpu
);
2894 if (!kvm_cpu_has_interrupt(vcpu
))
2896 if (vmx_irq_enabled(vcpu
)) {
2897 vector
= kvm_cpu_get_interrupt(vcpu
);
2898 vmx_inject_irq(vcpu
, vector
);
2899 kvm_timer_intr_post(vcpu
, vector
);
2901 enable_irq_window(vcpu
);
2905 * Failure to inject an interrupt should give us the information
2906 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2907 * when fetching the interrupt redirection bitmap in the real-mode
2908 * tss, this doesn't happen. So we do it ourselves.
2910 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
2912 vmx
->rmode
.irq
.pending
= 0;
2913 if (kvm_rip_read(&vmx
->vcpu
) + 1 != vmx
->rmode
.irq
.rip
)
2915 kvm_rip_write(&vmx
->vcpu
, vmx
->rmode
.irq
.rip
);
2916 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
2917 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
2918 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
2921 vmx
->idt_vectoring_info
=
2922 VECTORING_INFO_VALID_MASK
2923 | INTR_TYPE_EXT_INTR
2924 | vmx
->rmode
.irq
.vector
;
2927 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2929 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2932 if (test_bit(VCPU_REGS_RSP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
2933 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
2934 if (test_bit(VCPU_REGS_RIP
, (unsigned long *)&vcpu
->arch
.regs_dirty
))
2935 vmcs_writel(GUEST_RIP
, vcpu
->arch
.regs
[VCPU_REGS_RIP
]);
2938 * Loading guest fpu may have cleared host cr0.ts
2940 vmcs_writel(HOST_CR0
, read_cr0());
2943 /* Store host registers */
2944 #ifdef CONFIG_X86_64
2945 "push %%rdx; push %%rbp;"
2948 "push %%edx; push %%ebp;"
2951 __ex(ASM_VMX_VMWRITE_RSP_RDX
) "\n\t"
2952 /* Check if vmlaunch of vmresume is needed */
2953 "cmpl $0, %c[launched](%0) \n\t"
2954 /* Load guest registers. Don't clobber flags. */
2955 #ifdef CONFIG_X86_64
2956 "mov %c[cr2](%0), %%rax \n\t"
2957 "mov %%rax, %%cr2 \n\t"
2958 "mov %c[rax](%0), %%rax \n\t"
2959 "mov %c[rbx](%0), %%rbx \n\t"
2960 "mov %c[rdx](%0), %%rdx \n\t"
2961 "mov %c[rsi](%0), %%rsi \n\t"
2962 "mov %c[rdi](%0), %%rdi \n\t"
2963 "mov %c[rbp](%0), %%rbp \n\t"
2964 "mov %c[r8](%0), %%r8 \n\t"
2965 "mov %c[r9](%0), %%r9 \n\t"
2966 "mov %c[r10](%0), %%r10 \n\t"
2967 "mov %c[r11](%0), %%r11 \n\t"
2968 "mov %c[r12](%0), %%r12 \n\t"
2969 "mov %c[r13](%0), %%r13 \n\t"
2970 "mov %c[r14](%0), %%r14 \n\t"
2971 "mov %c[r15](%0), %%r15 \n\t"
2972 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
2974 "mov %c[cr2](%0), %%eax \n\t"
2975 "mov %%eax, %%cr2 \n\t"
2976 "mov %c[rax](%0), %%eax \n\t"
2977 "mov %c[rbx](%0), %%ebx \n\t"
2978 "mov %c[rdx](%0), %%edx \n\t"
2979 "mov %c[rsi](%0), %%esi \n\t"
2980 "mov %c[rdi](%0), %%edi \n\t"
2981 "mov %c[rbp](%0), %%ebp \n\t"
2982 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
2984 /* Enter guest mode */
2985 "jne .Llaunched \n\t"
2986 __ex(ASM_VMX_VMLAUNCH
) "\n\t"
2987 "jmp .Lkvm_vmx_return \n\t"
2988 ".Llaunched: " __ex(ASM_VMX_VMRESUME
) "\n\t"
2989 ".Lkvm_vmx_return: "
2990 /* Save guest registers, load host registers, keep flags */
2991 #ifdef CONFIG_X86_64
2992 "xchg %0, (%%rsp) \n\t"
2993 "mov %%rax, %c[rax](%0) \n\t"
2994 "mov %%rbx, %c[rbx](%0) \n\t"
2995 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2996 "mov %%rdx, %c[rdx](%0) \n\t"
2997 "mov %%rsi, %c[rsi](%0) \n\t"
2998 "mov %%rdi, %c[rdi](%0) \n\t"
2999 "mov %%rbp, %c[rbp](%0) \n\t"
3000 "mov %%r8, %c[r8](%0) \n\t"
3001 "mov %%r9, %c[r9](%0) \n\t"
3002 "mov %%r10, %c[r10](%0) \n\t"
3003 "mov %%r11, %c[r11](%0) \n\t"
3004 "mov %%r12, %c[r12](%0) \n\t"
3005 "mov %%r13, %c[r13](%0) \n\t"
3006 "mov %%r14, %c[r14](%0) \n\t"
3007 "mov %%r15, %c[r15](%0) \n\t"
3008 "mov %%cr2, %%rax \n\t"
3009 "mov %%rax, %c[cr2](%0) \n\t"
3011 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
3013 "xchg %0, (%%esp) \n\t"
3014 "mov %%eax, %c[rax](%0) \n\t"
3015 "mov %%ebx, %c[rbx](%0) \n\t"
3016 "pushl (%%esp); popl %c[rcx](%0) \n\t"
3017 "mov %%edx, %c[rdx](%0) \n\t"
3018 "mov %%esi, %c[rsi](%0) \n\t"
3019 "mov %%edi, %c[rdi](%0) \n\t"
3020 "mov %%ebp, %c[rbp](%0) \n\t"
3021 "mov %%cr2, %%eax \n\t"
3022 "mov %%eax, %c[cr2](%0) \n\t"
3024 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
3026 "setbe %c[fail](%0) \n\t"
3027 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
3028 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
3029 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
3030 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
3031 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
3032 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
3033 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
3034 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
3035 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
3036 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
3037 #ifdef CONFIG_X86_64
3038 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
3039 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
3040 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
3041 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
3042 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
3043 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
3044 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
3045 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
3047 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
3049 #ifdef CONFIG_X86_64
3050 , "rbx", "rdi", "rsi"
3051 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3053 , "ebx", "edi", "rsi"
3057 vcpu
->arch
.regs_avail
= ~((1 << VCPU_REGS_RIP
) | (1 << VCPU_REGS_RSP
));
3058 vcpu
->arch
.regs_dirty
= 0;
3060 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
3061 if (vmx
->rmode
.irq
.pending
)
3062 fixup_rmode_irq(vmx
);
3064 vcpu
->arch
.interrupt_window_open
=
3065 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) &
3066 (GUEST_INTR_STATE_STI
| GUEST_INTR_STATE_MOV_SS
)) == 0;
3068 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
3071 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
3073 /* We need to handle NMIs before interrupts are enabled */
3074 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200 &&
3075 (intr_info
& INTR_INFO_VALID_MASK
)) {
3076 KVMTRACE_0D(NMI
, vcpu
, handler
);
3081 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
3083 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3087 free_vmcs(vmx
->vmcs
);
3092 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
3094 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
3096 spin_lock(&vmx_vpid_lock
);
3098 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
3099 spin_unlock(&vmx_vpid_lock
);
3100 vmx_free_vmcs(vcpu
);
3101 kfree(vmx
->host_msrs
);
3102 kfree(vmx
->guest_msrs
);
3103 kvm_vcpu_uninit(vcpu
);
3104 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3107 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
3110 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
3114 return ERR_PTR(-ENOMEM
);
3118 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
3122 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3123 if (!vmx
->guest_msrs
) {
3128 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3129 if (!vmx
->host_msrs
)
3130 goto free_guest_msrs
;
3132 vmx
->vmcs
= alloc_vmcs();
3136 vmcs_clear(vmx
->vmcs
);
3139 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
3140 err
= vmx_vcpu_setup(vmx
);
3141 vmx_vcpu_put(&vmx
->vcpu
);
3145 if (vm_need_virtualize_apic_accesses(kvm
))
3146 if (alloc_apic_access_page(kvm
) != 0)
3150 if (alloc_identity_pagetable(kvm
) != 0)
3156 free_vmcs(vmx
->vmcs
);
3158 kfree(vmx
->host_msrs
);
3160 kfree(vmx
->guest_msrs
);
3162 kvm_vcpu_uninit(&vmx
->vcpu
);
3164 kmem_cache_free(kvm_vcpu_cache
, vmx
);
3165 return ERR_PTR(err
);
3168 static void __init
vmx_check_processor_compat(void *rtn
)
3170 struct vmcs_config vmcs_conf
;
3173 if (setup_vmcs_config(&vmcs_conf
) < 0)
3175 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
3176 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
3177 smp_processor_id());
3182 static int get_ept_level(void)
3184 return VMX_EPT_DEFAULT_GAW
+ 1;
3187 static struct kvm_x86_ops vmx_x86_ops
= {
3188 .cpu_has_kvm_support
= cpu_has_kvm_support
,
3189 .disabled_by_bios
= vmx_disabled_by_bios
,
3190 .hardware_setup
= hardware_setup
,
3191 .hardware_unsetup
= hardware_unsetup
,
3192 .check_processor_compatibility
= vmx_check_processor_compat
,
3193 .hardware_enable
= hardware_enable
,
3194 .hardware_disable
= hardware_disable
,
3195 .cpu_has_accelerated_tpr
= cpu_has_vmx_virtualize_apic_accesses
,
3197 .vcpu_create
= vmx_create_vcpu
,
3198 .vcpu_free
= vmx_free_vcpu
,
3199 .vcpu_reset
= vmx_vcpu_reset
,
3201 .prepare_guest_switch
= vmx_save_host_state
,
3202 .vcpu_load
= vmx_vcpu_load
,
3203 .vcpu_put
= vmx_vcpu_put
,
3205 .set_guest_debug
= set_guest_debug
,
3206 .guest_debug_pre
= kvm_guest_debug_pre
,
3207 .get_msr
= vmx_get_msr
,
3208 .set_msr
= vmx_set_msr
,
3209 .get_segment_base
= vmx_get_segment_base
,
3210 .get_segment
= vmx_get_segment
,
3211 .set_segment
= vmx_set_segment
,
3212 .get_cpl
= vmx_get_cpl
,
3213 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
3214 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
3215 .set_cr0
= vmx_set_cr0
,
3216 .set_cr3
= vmx_set_cr3
,
3217 .set_cr4
= vmx_set_cr4
,
3218 .set_efer
= vmx_set_efer
,
3219 .get_idt
= vmx_get_idt
,
3220 .set_idt
= vmx_set_idt
,
3221 .get_gdt
= vmx_get_gdt
,
3222 .set_gdt
= vmx_set_gdt
,
3223 .cache_reg
= vmx_cache_reg
,
3224 .get_rflags
= vmx_get_rflags
,
3225 .set_rflags
= vmx_set_rflags
,
3227 .tlb_flush
= vmx_flush_tlb
,
3229 .run
= vmx_vcpu_run
,
3230 .handle_exit
= kvm_handle_exit
,
3231 .skip_emulated_instruction
= skip_emulated_instruction
,
3232 .patch_hypercall
= vmx_patch_hypercall
,
3233 .get_irq
= vmx_get_irq
,
3234 .set_irq
= vmx_inject_irq
,
3235 .queue_exception
= vmx_queue_exception
,
3236 .exception_injected
= vmx_exception_injected
,
3237 .inject_pending_irq
= vmx_intr_assist
,
3238 .inject_pending_vectors
= do_interrupt_requests
,
3240 .set_tss_addr
= vmx_set_tss_addr
,
3241 .get_tdp_level
= get_ept_level
,
3244 static int __init
vmx_init(void)
3249 vmx_io_bitmap_a
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3250 if (!vmx_io_bitmap_a
)
3253 vmx_io_bitmap_b
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3254 if (!vmx_io_bitmap_b
) {
3259 vmx_msr_bitmap
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
3260 if (!vmx_msr_bitmap
) {
3266 * Allow direct access to the PC debug port (it is often used for I/O
3267 * delays, but the vmexits simply slow things down).
3269 va
= kmap(vmx_io_bitmap_a
);
3270 memset(va
, 0xff, PAGE_SIZE
);
3271 clear_bit(0x80, va
);
3272 kunmap(vmx_io_bitmap_a
);
3274 va
= kmap(vmx_io_bitmap_b
);
3275 memset(va
, 0xff, PAGE_SIZE
);
3276 kunmap(vmx_io_bitmap_b
);
3278 va
= kmap(vmx_msr_bitmap
);
3279 memset(va
, 0xff, PAGE_SIZE
);
3280 kunmap(vmx_msr_bitmap
);
3282 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
3284 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
3288 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_FS_BASE
);
3289 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_GS_BASE
);
3290 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_CS
);
3291 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_ESP
);
3292 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_EIP
);
3294 if (vm_need_ept()) {
3295 bypass_guest_pf
= 0;
3296 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK
|
3297 VMX_EPT_WRITABLE_MASK
|
3298 VMX_EPT_DEFAULT_MT
<< VMX_EPT_MT_EPTE_SHIFT
);
3299 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
3300 VMX_EPT_EXECUTABLE_MASK
);
3305 if (bypass_guest_pf
)
3306 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
3313 __free_page(vmx_msr_bitmap
);
3315 __free_page(vmx_io_bitmap_b
);
3317 __free_page(vmx_io_bitmap_a
);
3321 static void __exit
vmx_exit(void)
3323 __free_page(vmx_msr_bitmap
);
3324 __free_page(vmx_io_bitmap_b
);
3325 __free_page(vmx_io_bitmap_a
);
3330 module_init(vmx_init
)
3331 module_exit(vmx_exit
)