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>
33 MODULE_AUTHOR("Qumranet");
34 MODULE_LICENSE("GPL");
36 static int bypass_guest_pf
= 1;
37 module_param(bypass_guest_pf
, bool, 0);
39 static int enable_vpid
= 1;
40 module_param(enable_vpid
, bool, 0);
42 static int flexpriority_enabled
= 1;
43 module_param(flexpriority_enabled
, bool, 0);
55 u32 idt_vectoring_info
;
56 struct kvm_msr_entry
*guest_msrs
;
57 struct kvm_msr_entry
*host_msrs
;
62 int msr_offset_kernel_gs_base
;
67 u16 fs_sel
, gs_sel
, ldt_sel
;
68 int gs_ldt_reload_needed
;
70 int guest_efer_loaded
;
82 static inline struct vcpu_vmx
*to_vmx(struct kvm_vcpu
*vcpu
)
84 return container_of(vcpu
, struct vcpu_vmx
, vcpu
);
87 static int init_rmode_tss(struct kvm
*kvm
);
89 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
90 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
92 static struct page
*vmx_io_bitmap_a
;
93 static struct page
*vmx_io_bitmap_b
;
94 static struct page
*vmx_msr_bitmap
;
96 static DECLARE_BITMAP(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
97 static DEFINE_SPINLOCK(vmx_vpid_lock
);
99 static struct vmcs_config
{
103 u32 pin_based_exec_ctrl
;
104 u32 cpu_based_exec_ctrl
;
105 u32 cpu_based_2nd_exec_ctrl
;
110 #define VMX_SEGMENT_FIELD(seg) \
111 [VCPU_SREG_##seg] = { \
112 .selector = GUEST_##seg##_SELECTOR, \
113 .base = GUEST_##seg##_BASE, \
114 .limit = GUEST_##seg##_LIMIT, \
115 .ar_bytes = GUEST_##seg##_AR_BYTES, \
118 static struct kvm_vmx_segment_field
{
123 } kvm_vmx_segment_fields
[] = {
124 VMX_SEGMENT_FIELD(CS
),
125 VMX_SEGMENT_FIELD(DS
),
126 VMX_SEGMENT_FIELD(ES
),
127 VMX_SEGMENT_FIELD(FS
),
128 VMX_SEGMENT_FIELD(GS
),
129 VMX_SEGMENT_FIELD(SS
),
130 VMX_SEGMENT_FIELD(TR
),
131 VMX_SEGMENT_FIELD(LDTR
),
135 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
136 * away by decrementing the array size.
138 static const u32 vmx_msr_index
[] = {
140 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
142 MSR_EFER
, MSR_K6_STAR
,
144 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
146 static void load_msrs(struct kvm_msr_entry
*e
, int n
)
150 for (i
= 0; i
< n
; ++i
)
151 wrmsrl(e
[i
].index
, e
[i
].data
);
154 static void save_msrs(struct kvm_msr_entry
*e
, int n
)
158 for (i
= 0; i
< n
; ++i
)
159 rdmsrl(e
[i
].index
, e
[i
].data
);
162 static inline int is_page_fault(u32 intr_info
)
164 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
165 INTR_INFO_VALID_MASK
)) ==
166 (INTR_TYPE_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
169 static inline int is_no_device(u32 intr_info
)
171 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
172 INTR_INFO_VALID_MASK
)) ==
173 (INTR_TYPE_EXCEPTION
| NM_VECTOR
| INTR_INFO_VALID_MASK
);
176 static inline int is_invalid_opcode(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
| UD_VECTOR
| INTR_INFO_VALID_MASK
);
183 static inline int is_external_interrupt(u32 intr_info
)
185 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
186 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
189 static inline int cpu_has_vmx_msr_bitmap(void)
191 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_USE_MSR_BITMAPS
);
194 static inline int cpu_has_vmx_tpr_shadow(void)
196 return (vmcs_config
.cpu_based_exec_ctrl
& CPU_BASED_TPR_SHADOW
);
199 static inline int vm_need_tpr_shadow(struct kvm
*kvm
)
201 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm
)));
204 static inline int cpu_has_secondary_exec_ctrls(void)
206 return (vmcs_config
.cpu_based_exec_ctrl
&
207 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
);
210 static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
212 return flexpriority_enabled
213 && (vmcs_config
.cpu_based_2nd_exec_ctrl
&
214 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
);
217 static inline int vm_need_virtualize_apic_accesses(struct kvm
*kvm
)
219 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
220 (irqchip_in_kernel(kvm
)));
223 static inline int cpu_has_vmx_vpid(void)
225 return (vmcs_config
.cpu_based_2nd_exec_ctrl
&
226 SECONDARY_EXEC_ENABLE_VPID
);
229 static int __find_msr_index(struct vcpu_vmx
*vmx
, u32 msr
)
233 for (i
= 0; i
< vmx
->nmsrs
; ++i
)
234 if (vmx
->guest_msrs
[i
].index
== msr
)
239 static inline void __invvpid(int ext
, u16 vpid
, gva_t gva
)
245 } operand
= { vpid
, 0, gva
};
247 asm volatile (ASM_VMX_INVVPID
248 /* CF==1 or ZF==1 --> rc = -1 */
250 : : "a"(&operand
), "c"(ext
) : "cc", "memory");
253 static struct kvm_msr_entry
*find_msr_entry(struct vcpu_vmx
*vmx
, u32 msr
)
257 i
= __find_msr_index(vmx
, msr
);
259 return &vmx
->guest_msrs
[i
];
263 static void vmcs_clear(struct vmcs
*vmcs
)
265 u64 phys_addr
= __pa(vmcs
);
268 asm volatile (ASM_VMX_VMCLEAR_RAX
"; setna %0"
269 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
272 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
276 static void __vcpu_clear(void *arg
)
278 struct vcpu_vmx
*vmx
= arg
;
279 int cpu
= raw_smp_processor_id();
281 if (vmx
->vcpu
.cpu
== cpu
)
282 vmcs_clear(vmx
->vmcs
);
283 if (per_cpu(current_vmcs
, cpu
) == vmx
->vmcs
)
284 per_cpu(current_vmcs
, cpu
) = NULL
;
285 rdtscll(vmx
->vcpu
.arch
.host_tsc
);
288 static void vcpu_clear(struct vcpu_vmx
*vmx
)
290 if (vmx
->vcpu
.cpu
== -1)
292 smp_call_function_single(vmx
->vcpu
.cpu
, __vcpu_clear
, vmx
, 0, 1);
296 static inline void vpid_sync_vcpu_all(struct vcpu_vmx
*vmx
)
301 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT
, vmx
->vpid
, 0);
304 static unsigned long vmcs_readl(unsigned long field
)
308 asm volatile (ASM_VMX_VMREAD_RDX_RAX
309 : "=a"(value
) : "d"(field
) : "cc");
313 static u16
vmcs_read16(unsigned long field
)
315 return vmcs_readl(field
);
318 static u32
vmcs_read32(unsigned long field
)
320 return vmcs_readl(field
);
323 static u64
vmcs_read64(unsigned long field
)
326 return vmcs_readl(field
);
328 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
332 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
334 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
335 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
339 static void vmcs_writel(unsigned long field
, unsigned long value
)
343 asm volatile (ASM_VMX_VMWRITE_RAX_RDX
"; setna %0"
344 : "=q"(error
) : "a"(value
), "d"(field
) : "cc");
346 vmwrite_error(field
, value
);
349 static void vmcs_write16(unsigned long field
, u16 value
)
351 vmcs_writel(field
, value
);
354 static void vmcs_write32(unsigned long field
, u32 value
)
356 vmcs_writel(field
, value
);
359 static void vmcs_write64(unsigned long field
, u64 value
)
362 vmcs_writel(field
, value
);
364 vmcs_writel(field
, value
);
366 vmcs_writel(field
+1, value
>> 32);
370 static void vmcs_clear_bits(unsigned long field
, u32 mask
)
372 vmcs_writel(field
, vmcs_readl(field
) & ~mask
);
375 static void vmcs_set_bits(unsigned long field
, u32 mask
)
377 vmcs_writel(field
, vmcs_readl(field
) | mask
);
380 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
384 eb
= (1u << PF_VECTOR
) | (1u << UD_VECTOR
);
385 if (!vcpu
->fpu_active
)
386 eb
|= 1u << NM_VECTOR
;
387 if (vcpu
->guest_debug
.enabled
)
389 if (vcpu
->arch
.rmode
.active
)
391 vmcs_write32(EXCEPTION_BITMAP
, eb
);
394 static void reload_tss(void)
397 * VT restores TR but not its size. Useless.
399 struct descriptor_table gdt
;
400 struct desc_struct
*descs
;
403 descs
= (void *)gdt
.base
;
404 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
408 static void load_transition_efer(struct vcpu_vmx
*vmx
)
410 int efer_offset
= vmx
->msr_offset_efer
;
411 u64 host_efer
= vmx
->host_msrs
[efer_offset
].data
;
412 u64 guest_efer
= vmx
->guest_msrs
[efer_offset
].data
;
418 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
421 ignore_bits
= EFER_NX
| EFER_SCE
;
423 ignore_bits
|= EFER_LMA
| EFER_LME
;
424 /* SCE is meaningful only in long mode on Intel */
425 if (guest_efer
& EFER_LMA
)
426 ignore_bits
&= ~(u64
)EFER_SCE
;
428 if ((guest_efer
& ~ignore_bits
) == (host_efer
& ~ignore_bits
))
431 vmx
->host_state
.guest_efer_loaded
= 1;
432 guest_efer
&= ~ignore_bits
;
433 guest_efer
|= host_efer
& ignore_bits
;
434 wrmsrl(MSR_EFER
, guest_efer
);
435 vmx
->vcpu
.stat
.efer_reload
++;
438 static void reload_host_efer(struct vcpu_vmx
*vmx
)
440 if (vmx
->host_state
.guest_efer_loaded
) {
441 vmx
->host_state
.guest_efer_loaded
= 0;
442 load_msrs(vmx
->host_msrs
+ vmx
->msr_offset_efer
, 1);
446 static void vmx_save_host_state(struct kvm_vcpu
*vcpu
)
448 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
450 if (vmx
->host_state
.loaded
)
453 vmx
->host_state
.loaded
= 1;
455 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
456 * allow segment selectors with cpl > 0 or ti == 1.
458 vmx
->host_state
.ldt_sel
= read_ldt();
459 vmx
->host_state
.gs_ldt_reload_needed
= vmx
->host_state
.ldt_sel
;
460 vmx
->host_state
.fs_sel
= read_fs();
461 if (!(vmx
->host_state
.fs_sel
& 7)) {
462 vmcs_write16(HOST_FS_SELECTOR
, vmx
->host_state
.fs_sel
);
463 vmx
->host_state
.fs_reload_needed
= 0;
465 vmcs_write16(HOST_FS_SELECTOR
, 0);
466 vmx
->host_state
.fs_reload_needed
= 1;
468 vmx
->host_state
.gs_sel
= read_gs();
469 if (!(vmx
->host_state
.gs_sel
& 7))
470 vmcs_write16(HOST_GS_SELECTOR
, vmx
->host_state
.gs_sel
);
472 vmcs_write16(HOST_GS_SELECTOR
, 0);
473 vmx
->host_state
.gs_ldt_reload_needed
= 1;
477 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
478 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
480 vmcs_writel(HOST_FS_BASE
, segment_base(vmx
->host_state
.fs_sel
));
481 vmcs_writel(HOST_GS_BASE
, segment_base(vmx
->host_state
.gs_sel
));
485 if (is_long_mode(&vmx
->vcpu
))
486 save_msrs(vmx
->host_msrs
+
487 vmx
->msr_offset_kernel_gs_base
, 1);
490 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
491 load_transition_efer(vmx
);
494 static void vmx_load_host_state(struct vcpu_vmx
*vmx
)
498 if (!vmx
->host_state
.loaded
)
501 ++vmx
->vcpu
.stat
.host_state_reload
;
502 vmx
->host_state
.loaded
= 0;
503 if (vmx
->host_state
.fs_reload_needed
)
504 load_fs(vmx
->host_state
.fs_sel
);
505 if (vmx
->host_state
.gs_ldt_reload_needed
) {
506 load_ldt(vmx
->host_state
.ldt_sel
);
508 * If we have to reload gs, we must take care to
509 * preserve our gs base.
511 local_irq_save(flags
);
512 load_gs(vmx
->host_state
.gs_sel
);
514 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
516 local_irq_restore(flags
);
519 save_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
520 load_msrs(vmx
->host_msrs
, vmx
->save_nmsrs
);
521 reload_host_efer(vmx
);
525 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
526 * vcpu mutex is already taken.
528 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
530 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
531 u64 phys_addr
= __pa(vmx
->vmcs
);
532 u64 tsc_this
, delta
, new_offset
;
534 if (vcpu
->cpu
!= cpu
) {
536 kvm_migrate_apic_timer(vcpu
);
537 vpid_sync_vcpu_all(vmx
);
540 if (per_cpu(current_vmcs
, cpu
) != vmx
->vmcs
) {
543 per_cpu(current_vmcs
, cpu
) = vmx
->vmcs
;
544 asm volatile (ASM_VMX_VMPTRLD_RAX
"; setna %0"
545 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
548 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
549 vmx
->vmcs
, phys_addr
);
552 if (vcpu
->cpu
!= cpu
) {
553 struct descriptor_table dt
;
554 unsigned long sysenter_esp
;
558 * Linux uses per-cpu TSS and GDT, so set these when switching
561 vmcs_writel(HOST_TR_BASE
, read_tr_base()); /* 22.2.4 */
563 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
565 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
566 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
569 * Make sure the time stamp counter is monotonous.
572 if (tsc_this
< vcpu
->arch
.host_tsc
) {
573 delta
= vcpu
->arch
.host_tsc
- tsc_this
;
574 new_offset
= vmcs_read64(TSC_OFFSET
) + delta
;
575 vmcs_write64(TSC_OFFSET
, new_offset
);
580 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
582 vmx_load_host_state(to_vmx(vcpu
));
585 static void vmx_fpu_activate(struct kvm_vcpu
*vcpu
)
587 if (vcpu
->fpu_active
)
589 vcpu
->fpu_active
= 1;
590 vmcs_clear_bits(GUEST_CR0
, X86_CR0_TS
);
591 if (vcpu
->arch
.cr0
& X86_CR0_TS
)
592 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
593 update_exception_bitmap(vcpu
);
596 static void vmx_fpu_deactivate(struct kvm_vcpu
*vcpu
)
598 if (!vcpu
->fpu_active
)
600 vcpu
->fpu_active
= 0;
601 vmcs_set_bits(GUEST_CR0
, X86_CR0_TS
);
602 update_exception_bitmap(vcpu
);
605 static void vmx_vcpu_decache(struct kvm_vcpu
*vcpu
)
607 vcpu_clear(to_vmx(vcpu
));
610 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
612 return vmcs_readl(GUEST_RFLAGS
);
615 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
617 if (vcpu
->arch
.rmode
.active
)
618 rflags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
619 vmcs_writel(GUEST_RFLAGS
, rflags
);
622 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
625 u32 interruptibility
;
627 rip
= vmcs_readl(GUEST_RIP
);
628 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
629 vmcs_writel(GUEST_RIP
, rip
);
632 * We emulated an instruction, so temporary interrupt blocking
633 * should be removed, if set.
635 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
636 if (interruptibility
& 3)
637 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
638 interruptibility
& ~3);
639 vcpu
->arch
.interrupt_window_open
= 1;
642 static void vmx_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
,
643 bool has_error_code
, u32 error_code
)
645 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
646 nr
| INTR_TYPE_EXCEPTION
647 | (has_error_code
? INTR_INFO_DELIVER_CODE_MASK
: 0)
648 | INTR_INFO_VALID_MASK
);
650 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
653 static bool vmx_exception_injected(struct kvm_vcpu
*vcpu
)
655 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
657 return !(vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
);
661 * Swap MSR entry in host/guest MSR entry array.
664 static void move_msr_up(struct vcpu_vmx
*vmx
, int from
, int to
)
666 struct kvm_msr_entry tmp
;
668 tmp
= vmx
->guest_msrs
[to
];
669 vmx
->guest_msrs
[to
] = vmx
->guest_msrs
[from
];
670 vmx
->guest_msrs
[from
] = tmp
;
671 tmp
= vmx
->host_msrs
[to
];
672 vmx
->host_msrs
[to
] = vmx
->host_msrs
[from
];
673 vmx
->host_msrs
[from
] = tmp
;
678 * Set up the vmcs to automatically save and restore system
679 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
680 * mode, as fiddling with msrs is very expensive.
682 static void setup_msrs(struct vcpu_vmx
*vmx
)
686 vmx_load_host_state(vmx
);
689 if (is_long_mode(&vmx
->vcpu
)) {
692 index
= __find_msr_index(vmx
, MSR_SYSCALL_MASK
);
694 move_msr_up(vmx
, index
, save_nmsrs
++);
695 index
= __find_msr_index(vmx
, MSR_LSTAR
);
697 move_msr_up(vmx
, index
, save_nmsrs
++);
698 index
= __find_msr_index(vmx
, MSR_CSTAR
);
700 move_msr_up(vmx
, index
, save_nmsrs
++);
701 index
= __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
703 move_msr_up(vmx
, index
, save_nmsrs
++);
705 * MSR_K6_STAR is only needed on long mode guests, and only
706 * if efer.sce is enabled.
708 index
= __find_msr_index(vmx
, MSR_K6_STAR
);
709 if ((index
>= 0) && (vmx
->vcpu
.arch
.shadow_efer
& EFER_SCE
))
710 move_msr_up(vmx
, index
, save_nmsrs
++);
713 vmx
->save_nmsrs
= save_nmsrs
;
716 vmx
->msr_offset_kernel_gs_base
=
717 __find_msr_index(vmx
, MSR_KERNEL_GS_BASE
);
719 vmx
->msr_offset_efer
= __find_msr_index(vmx
, MSR_EFER
);
723 * reads and returns guest's timestamp counter "register"
724 * guest_tsc = host_tsc + tsc_offset -- 21.3
726 static u64
guest_read_tsc(void)
728 u64 host_tsc
, tsc_offset
;
731 tsc_offset
= vmcs_read64(TSC_OFFSET
);
732 return host_tsc
+ tsc_offset
;
736 * writes 'guest_tsc' into guest's timestamp counter "register"
737 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
739 static void guest_write_tsc(u64 guest_tsc
)
744 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
748 * Reads an msr value (of 'msr_index') into 'pdata'.
749 * Returns 0 on success, non-0 otherwise.
750 * Assumes vcpu_load() was already called.
752 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
755 struct kvm_msr_entry
*msr
;
758 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
765 data
= vmcs_readl(GUEST_FS_BASE
);
768 data
= vmcs_readl(GUEST_GS_BASE
);
771 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
773 case MSR_IA32_TIME_STAMP_COUNTER
:
774 data
= guest_read_tsc();
776 case MSR_IA32_SYSENTER_CS
:
777 data
= vmcs_read32(GUEST_SYSENTER_CS
);
779 case MSR_IA32_SYSENTER_EIP
:
780 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
782 case MSR_IA32_SYSENTER_ESP
:
783 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
786 msr
= find_msr_entry(to_vmx(vcpu
), msr_index
);
791 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
799 * Writes msr value into into the appropriate "register".
800 * Returns 0 on success, non-0 otherwise.
801 * Assumes vcpu_load() was already called.
803 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
805 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
806 struct kvm_msr_entry
*msr
;
812 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
813 if (vmx
->host_state
.loaded
) {
814 reload_host_efer(vmx
);
815 load_transition_efer(vmx
);
819 vmcs_writel(GUEST_FS_BASE
, data
);
822 vmcs_writel(GUEST_GS_BASE
, data
);
825 case MSR_IA32_SYSENTER_CS
:
826 vmcs_write32(GUEST_SYSENTER_CS
, data
);
828 case MSR_IA32_SYSENTER_EIP
:
829 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
831 case MSR_IA32_SYSENTER_ESP
:
832 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
834 case MSR_IA32_TIME_STAMP_COUNTER
:
835 guest_write_tsc(data
);
838 msr
= find_msr_entry(vmx
, msr_index
);
841 if (vmx
->host_state
.loaded
)
842 load_msrs(vmx
->guest_msrs
, vmx
->save_nmsrs
);
845 ret
= kvm_set_msr_common(vcpu
, msr_index
, data
);
852 * Sync the rsp and rip registers into the vcpu structure. This allows
853 * registers to be accessed by indexing vcpu->arch.regs.
855 static void vcpu_load_rsp_rip(struct kvm_vcpu
*vcpu
)
857 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
858 vcpu
->arch
.rip
= vmcs_readl(GUEST_RIP
);
862 * Syncs rsp and rip back into the vmcs. Should be called after possible
865 static void vcpu_put_rsp_rip(struct kvm_vcpu
*vcpu
)
867 vmcs_writel(GUEST_RSP
, vcpu
->arch
.regs
[VCPU_REGS_RSP
]);
868 vmcs_writel(GUEST_RIP
, vcpu
->arch
.rip
);
871 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
873 unsigned long dr7
= 0x400;
876 old_singlestep
= vcpu
->guest_debug
.singlestep
;
878 vcpu
->guest_debug
.enabled
= dbg
->enabled
;
879 if (vcpu
->guest_debug
.enabled
) {
882 dr7
|= 0x200; /* exact */
883 for (i
= 0; i
< 4; ++i
) {
884 if (!dbg
->breakpoints
[i
].enabled
)
886 vcpu
->guest_debug
.bp
[i
] = dbg
->breakpoints
[i
].address
;
887 dr7
|= 2 << (i
*2); /* global enable */
888 dr7
|= 0 << (i
*4+16); /* execution breakpoint */
891 vcpu
->guest_debug
.singlestep
= dbg
->singlestep
;
893 vcpu
->guest_debug
.singlestep
= 0;
895 if (old_singlestep
&& !vcpu
->guest_debug
.singlestep
) {
898 flags
= vmcs_readl(GUEST_RFLAGS
);
899 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
900 vmcs_writel(GUEST_RFLAGS
, flags
);
903 update_exception_bitmap(vcpu
);
904 vmcs_writel(GUEST_DR7
, dr7
);
909 static int vmx_get_irq(struct kvm_vcpu
*vcpu
)
911 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
914 idtv_info_field
= vmx
->idt_vectoring_info
;
915 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
916 if (is_external_interrupt(idtv_info_field
))
917 return idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
919 printk(KERN_DEBUG
"pending exception: not handled yet\n");
924 static __init
int cpu_has_kvm_support(void)
926 unsigned long ecx
= cpuid_ecx(1);
927 return test_bit(5, &ecx
); /* CPUID.1:ECX.VMX[bit 5] -> VT */
930 static __init
int vmx_disabled_by_bios(void)
934 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
935 return (msr
& (MSR_IA32_FEATURE_CONTROL_LOCKED
|
936 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
937 == MSR_IA32_FEATURE_CONTROL_LOCKED
;
938 /* locked but not enabled */
941 static void hardware_enable(void *garbage
)
943 int cpu
= raw_smp_processor_id();
944 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
947 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
948 if ((old
& (MSR_IA32_FEATURE_CONTROL_LOCKED
|
949 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
950 != (MSR_IA32_FEATURE_CONTROL_LOCKED
|
951 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
))
952 /* enable and lock */
953 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
|
954 MSR_IA32_FEATURE_CONTROL_LOCKED
|
955 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED
);
956 write_cr4(read_cr4() | X86_CR4_VMXE
); /* FIXME: not cpu hotplug safe */
957 asm volatile (ASM_VMX_VMXON_RAX
: : "a"(&phys_addr
), "m"(phys_addr
)
961 static void hardware_disable(void *garbage
)
963 asm volatile (ASM_VMX_VMXOFF
: : : "cc");
966 static __init
int adjust_vmx_controls(u32 ctl_min
, u32 ctl_opt
,
967 u32 msr
, u32
*result
)
969 u32 vmx_msr_low
, vmx_msr_high
;
970 u32 ctl
= ctl_min
| ctl_opt
;
972 rdmsr(msr
, vmx_msr_low
, vmx_msr_high
);
974 ctl
&= vmx_msr_high
; /* bit == 0 in high word ==> must be zero */
975 ctl
|= vmx_msr_low
; /* bit == 1 in low word ==> must be one */
977 /* Ensure minimum (required) set of control bits are supported. */
985 static __init
int setup_vmcs_config(struct vmcs_config
*vmcs_conf
)
987 u32 vmx_msr_low
, vmx_msr_high
;
989 u32 _pin_based_exec_control
= 0;
990 u32 _cpu_based_exec_control
= 0;
991 u32 _cpu_based_2nd_exec_control
= 0;
992 u32 _vmexit_control
= 0;
993 u32 _vmentry_control
= 0;
995 min
= PIN_BASED_EXT_INTR_MASK
| PIN_BASED_NMI_EXITING
;
997 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PINBASED_CTLS
,
998 &_pin_based_exec_control
) < 0)
1001 min
= CPU_BASED_HLT_EXITING
|
1002 #ifdef CONFIG_X86_64
1003 CPU_BASED_CR8_LOAD_EXITING
|
1004 CPU_BASED_CR8_STORE_EXITING
|
1006 CPU_BASED_USE_IO_BITMAPS
|
1007 CPU_BASED_MOV_DR_EXITING
|
1008 CPU_BASED_USE_TSC_OFFSETING
;
1009 opt
= CPU_BASED_TPR_SHADOW
|
1010 CPU_BASED_USE_MSR_BITMAPS
|
1011 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
;
1012 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS
,
1013 &_cpu_based_exec_control
) < 0)
1015 #ifdef CONFIG_X86_64
1016 if ((_cpu_based_exec_control
& CPU_BASED_TPR_SHADOW
))
1017 _cpu_based_exec_control
&= ~CPU_BASED_CR8_LOAD_EXITING
&
1018 ~CPU_BASED_CR8_STORE_EXITING
;
1020 if (_cpu_based_exec_control
& CPU_BASED_ACTIVATE_SECONDARY_CONTROLS
) {
1022 opt
= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
|
1023 SECONDARY_EXEC_WBINVD_EXITING
|
1024 SECONDARY_EXEC_ENABLE_VPID
;
1025 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_PROCBASED_CTLS2
,
1026 &_cpu_based_2nd_exec_control
) < 0)
1029 #ifndef CONFIG_X86_64
1030 if (!(_cpu_based_2nd_exec_control
&
1031 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
))
1032 _cpu_based_exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1036 #ifdef CONFIG_X86_64
1037 min
|= VM_EXIT_HOST_ADDR_SPACE_SIZE
;
1040 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_EXIT_CTLS
,
1041 &_vmexit_control
) < 0)
1045 if (adjust_vmx_controls(min
, opt
, MSR_IA32_VMX_ENTRY_CTLS
,
1046 &_vmentry_control
) < 0)
1049 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
1051 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1052 if ((vmx_msr_high
& 0x1fff) > PAGE_SIZE
)
1055 #ifdef CONFIG_X86_64
1056 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1057 if (vmx_msr_high
& (1u<<16))
1061 /* Require Write-Back (WB) memory type for VMCS accesses. */
1062 if (((vmx_msr_high
>> 18) & 15) != 6)
1065 vmcs_conf
->size
= vmx_msr_high
& 0x1fff;
1066 vmcs_conf
->order
= get_order(vmcs_config
.size
);
1067 vmcs_conf
->revision_id
= vmx_msr_low
;
1069 vmcs_conf
->pin_based_exec_ctrl
= _pin_based_exec_control
;
1070 vmcs_conf
->cpu_based_exec_ctrl
= _cpu_based_exec_control
;
1071 vmcs_conf
->cpu_based_2nd_exec_ctrl
= _cpu_based_2nd_exec_control
;
1072 vmcs_conf
->vmexit_ctrl
= _vmexit_control
;
1073 vmcs_conf
->vmentry_ctrl
= _vmentry_control
;
1078 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
1080 int node
= cpu_to_node(cpu
);
1084 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_config
.order
);
1087 vmcs
= page_address(pages
);
1088 memset(vmcs
, 0, vmcs_config
.size
);
1089 vmcs
->revision_id
= vmcs_config
.revision_id
; /* vmcs revision id */
1093 static struct vmcs
*alloc_vmcs(void)
1095 return alloc_vmcs_cpu(raw_smp_processor_id());
1098 static void free_vmcs(struct vmcs
*vmcs
)
1100 free_pages((unsigned long)vmcs
, vmcs_config
.order
);
1103 static void free_kvm_area(void)
1107 for_each_online_cpu(cpu
)
1108 free_vmcs(per_cpu(vmxarea
, cpu
));
1111 static __init
int alloc_kvm_area(void)
1115 for_each_online_cpu(cpu
) {
1118 vmcs
= alloc_vmcs_cpu(cpu
);
1124 per_cpu(vmxarea
, cpu
) = vmcs
;
1129 static __init
int hardware_setup(void)
1131 if (setup_vmcs_config(&vmcs_config
) < 0)
1134 if (boot_cpu_has(X86_FEATURE_NX
))
1135 kvm_enable_efer_bits(EFER_NX
);
1137 return alloc_kvm_area();
1140 static __exit
void hardware_unsetup(void)
1145 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
1147 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1149 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
1150 vmcs_write16(sf
->selector
, save
->selector
);
1151 vmcs_writel(sf
->base
, save
->base
);
1152 vmcs_write32(sf
->limit
, save
->limit
);
1153 vmcs_write32(sf
->ar_bytes
, save
->ar
);
1155 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
1157 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
1161 static void enter_pmode(struct kvm_vcpu
*vcpu
)
1163 unsigned long flags
;
1165 vcpu
->arch
.rmode
.active
= 0;
1167 vmcs_writel(GUEST_TR_BASE
, vcpu
->arch
.rmode
.tr
.base
);
1168 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->arch
.rmode
.tr
.limit
);
1169 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->arch
.rmode
.tr
.ar
);
1171 flags
= vmcs_readl(GUEST_RFLAGS
);
1172 flags
&= ~(X86_EFLAGS_IOPL
| X86_EFLAGS_VM
);
1173 flags
|= (vcpu
->arch
.rmode
.save_iopl
<< IOPL_SHIFT
);
1174 vmcs_writel(GUEST_RFLAGS
, flags
);
1176 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~X86_CR4_VME
) |
1177 (vmcs_readl(CR4_READ_SHADOW
) & X86_CR4_VME
));
1179 update_exception_bitmap(vcpu
);
1181 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1182 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1183 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1184 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1186 vmcs_write16(GUEST_SS_SELECTOR
, 0);
1187 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
1189 vmcs_write16(GUEST_CS_SELECTOR
,
1190 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
1191 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1194 static gva_t
rmode_tss_base(struct kvm
*kvm
)
1196 if (!kvm
->arch
.tss_addr
) {
1197 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+
1198 kvm
->memslots
[0].npages
- 3;
1199 return base_gfn
<< PAGE_SHIFT
;
1201 return kvm
->arch
.tss_addr
;
1204 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
1206 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1208 save
->selector
= vmcs_read16(sf
->selector
);
1209 save
->base
= vmcs_readl(sf
->base
);
1210 save
->limit
= vmcs_read32(sf
->limit
);
1211 save
->ar
= vmcs_read32(sf
->ar_bytes
);
1212 vmcs_write16(sf
->selector
, save
->base
>> 4);
1213 vmcs_write32(sf
->base
, save
->base
& 0xfffff);
1214 vmcs_write32(sf
->limit
, 0xffff);
1215 vmcs_write32(sf
->ar_bytes
, 0xf3);
1218 static void enter_rmode(struct kvm_vcpu
*vcpu
)
1220 unsigned long flags
;
1222 vcpu
->arch
.rmode
.active
= 1;
1224 vcpu
->arch
.rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
1225 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
1227 vcpu
->arch
.rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
1228 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
1230 vcpu
->arch
.rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1231 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1233 flags
= vmcs_readl(GUEST_RFLAGS
);
1234 vcpu
->arch
.rmode
.save_iopl
1235 = (flags
& X86_EFLAGS_IOPL
) >> IOPL_SHIFT
;
1237 flags
|= X86_EFLAGS_IOPL
| X86_EFLAGS_VM
;
1239 vmcs_writel(GUEST_RFLAGS
, flags
);
1240 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | X86_CR4_VME
);
1241 update_exception_bitmap(vcpu
);
1243 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
1244 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
1245 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
1247 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
1248 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1249 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
1250 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
1251 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
1253 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->arch
.rmode
.es
);
1254 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->arch
.rmode
.ds
);
1255 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->arch
.rmode
.gs
);
1256 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->arch
.rmode
.fs
);
1258 kvm_mmu_reset_context(vcpu
);
1259 init_rmode_tss(vcpu
->kvm
);
1262 #ifdef CONFIG_X86_64
1264 static void enter_lmode(struct kvm_vcpu
*vcpu
)
1268 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
1269 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
1270 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
1272 vmcs_write32(GUEST_TR_AR_BYTES
,
1273 (guest_tr_ar
& ~AR_TYPE_MASK
)
1274 | AR_TYPE_BUSY_64_TSS
);
1277 vcpu
->arch
.shadow_efer
|= EFER_LMA
;
1279 find_msr_entry(to_vmx(vcpu
), MSR_EFER
)->data
|= EFER_LMA
| EFER_LME
;
1280 vmcs_write32(VM_ENTRY_CONTROLS
,
1281 vmcs_read32(VM_ENTRY_CONTROLS
)
1282 | VM_ENTRY_IA32E_MODE
);
1285 static void exit_lmode(struct kvm_vcpu
*vcpu
)
1287 vcpu
->arch
.shadow_efer
&= ~EFER_LMA
;
1289 vmcs_write32(VM_ENTRY_CONTROLS
,
1290 vmcs_read32(VM_ENTRY_CONTROLS
)
1291 & ~VM_ENTRY_IA32E_MODE
);
1296 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1298 vpid_sync_vcpu_all(to_vmx(vcpu
));
1301 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
1303 vcpu
->arch
.cr4
&= KVM_GUEST_CR4_MASK
;
1304 vcpu
->arch
.cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
1307 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
1309 vmx_fpu_deactivate(vcpu
);
1311 if (vcpu
->arch
.rmode
.active
&& (cr0
& X86_CR0_PE
))
1314 if (!vcpu
->arch
.rmode
.active
&& !(cr0
& X86_CR0_PE
))
1317 #ifdef CONFIG_X86_64
1318 if (vcpu
->arch
.shadow_efer
& EFER_LME
) {
1319 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
))
1321 if (is_paging(vcpu
) && !(cr0
& X86_CR0_PG
))
1326 vmcs_writel(CR0_READ_SHADOW
, cr0
);
1327 vmcs_writel(GUEST_CR0
,
1328 (cr0
& ~KVM_GUEST_CR0_MASK
) | KVM_VM_CR0_ALWAYS_ON
);
1329 vcpu
->arch
.cr0
= cr0
;
1331 if (!(cr0
& X86_CR0_TS
) || !(cr0
& X86_CR0_PE
))
1332 vmx_fpu_activate(vcpu
);
1335 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
1337 vmx_flush_tlb(vcpu
);
1338 vmcs_writel(GUEST_CR3
, cr3
);
1339 if (vcpu
->arch
.cr0
& X86_CR0_PE
)
1340 vmx_fpu_deactivate(vcpu
);
1343 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
1345 vmcs_writel(CR4_READ_SHADOW
, cr4
);
1346 vmcs_writel(GUEST_CR4
, cr4
| (vcpu
->arch
.rmode
.active
?
1347 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
));
1348 vcpu
->arch
.cr4
= cr4
;
1351 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
1353 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1354 struct kvm_msr_entry
*msr
= find_msr_entry(vmx
, MSR_EFER
);
1356 vcpu
->arch
.shadow_efer
= efer
;
1359 if (efer
& EFER_LMA
) {
1360 vmcs_write32(VM_ENTRY_CONTROLS
,
1361 vmcs_read32(VM_ENTRY_CONTROLS
) |
1362 VM_ENTRY_IA32E_MODE
);
1366 vmcs_write32(VM_ENTRY_CONTROLS
,
1367 vmcs_read32(VM_ENTRY_CONTROLS
) &
1368 ~VM_ENTRY_IA32E_MODE
);
1370 msr
->data
= efer
& ~EFER_LME
;
1375 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1377 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1379 return vmcs_readl(sf
->base
);
1382 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
1383 struct kvm_segment
*var
, int seg
)
1385 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1388 var
->base
= vmcs_readl(sf
->base
);
1389 var
->limit
= vmcs_read32(sf
->limit
);
1390 var
->selector
= vmcs_read16(sf
->selector
);
1391 ar
= vmcs_read32(sf
->ar_bytes
);
1392 if (ar
& AR_UNUSABLE_MASK
)
1394 var
->type
= ar
& 15;
1395 var
->s
= (ar
>> 4) & 1;
1396 var
->dpl
= (ar
>> 5) & 3;
1397 var
->present
= (ar
>> 7) & 1;
1398 var
->avl
= (ar
>> 12) & 1;
1399 var
->l
= (ar
>> 13) & 1;
1400 var
->db
= (ar
>> 14) & 1;
1401 var
->g
= (ar
>> 15) & 1;
1402 var
->unusable
= (ar
>> 16) & 1;
1405 static int vmx_get_cpl(struct kvm_vcpu
*vcpu
)
1407 struct kvm_segment kvm_seg
;
1409 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
)) /* if real mode */
1412 if (vmx_get_rflags(vcpu
) & X86_EFLAGS_VM
) /* if virtual 8086 */
1415 vmx_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_CS
);
1416 return kvm_seg
.selector
& 3;
1419 static u32
vmx_segment_access_rights(struct kvm_segment
*var
)
1426 ar
= var
->type
& 15;
1427 ar
|= (var
->s
& 1) << 4;
1428 ar
|= (var
->dpl
& 3) << 5;
1429 ar
|= (var
->present
& 1) << 7;
1430 ar
|= (var
->avl
& 1) << 12;
1431 ar
|= (var
->l
& 1) << 13;
1432 ar
|= (var
->db
& 1) << 14;
1433 ar
|= (var
->g
& 1) << 15;
1435 if (ar
== 0) /* a 0 value means unusable */
1436 ar
= AR_UNUSABLE_MASK
;
1441 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
1442 struct kvm_segment
*var
, int seg
)
1444 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1447 if (vcpu
->arch
.rmode
.active
&& seg
== VCPU_SREG_TR
) {
1448 vcpu
->arch
.rmode
.tr
.selector
= var
->selector
;
1449 vcpu
->arch
.rmode
.tr
.base
= var
->base
;
1450 vcpu
->arch
.rmode
.tr
.limit
= var
->limit
;
1451 vcpu
->arch
.rmode
.tr
.ar
= vmx_segment_access_rights(var
);
1454 vmcs_writel(sf
->base
, var
->base
);
1455 vmcs_write32(sf
->limit
, var
->limit
);
1456 vmcs_write16(sf
->selector
, var
->selector
);
1457 if (vcpu
->arch
.rmode
.active
&& var
->s
) {
1459 * Hack real-mode segments into vm86 compatibility.
1461 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
1462 vmcs_writel(sf
->base
, 0xf0000);
1465 ar
= vmx_segment_access_rights(var
);
1466 vmcs_write32(sf
->ar_bytes
, ar
);
1469 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
1471 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1473 *db
= (ar
>> 14) & 1;
1474 *l
= (ar
>> 13) & 1;
1477 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1479 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
1480 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
1483 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1485 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
1486 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
1489 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1491 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
1492 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
1495 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
1497 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
1498 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
1501 static int init_rmode_tss(struct kvm
*kvm
)
1503 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
1508 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1511 data
= TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
1512 r
= kvm_write_guest_page(kvm
, fn
++, &data
, 0x66, sizeof(u16
));
1515 r
= kvm_clear_guest_page(kvm
, fn
++, 0, PAGE_SIZE
);
1518 r
= kvm_clear_guest_page(kvm
, fn
, 0, PAGE_SIZE
);
1522 r
= kvm_write_guest_page(kvm
, fn
, &data
,
1523 RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1,
1533 static void seg_setup(int seg
)
1535 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1537 vmcs_write16(sf
->selector
, 0);
1538 vmcs_writel(sf
->base
, 0);
1539 vmcs_write32(sf
->limit
, 0xffff);
1540 vmcs_write32(sf
->ar_bytes
, 0x93);
1543 static int alloc_apic_access_page(struct kvm
*kvm
)
1545 struct kvm_userspace_memory_region kvm_userspace_mem
;
1548 down_write(&kvm
->slots_lock
);
1549 if (kvm
->arch
.apic_access_page
)
1551 kvm_userspace_mem
.slot
= APIC_ACCESS_PAGE_PRIVATE_MEMSLOT
;
1552 kvm_userspace_mem
.flags
= 0;
1553 kvm_userspace_mem
.guest_phys_addr
= 0xfee00000ULL
;
1554 kvm_userspace_mem
.memory_size
= PAGE_SIZE
;
1555 r
= __kvm_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1559 down_read(¤t
->mm
->mmap_sem
);
1560 kvm
->arch
.apic_access_page
= gfn_to_page(kvm
, 0xfee00);
1561 up_read(¤t
->mm
->mmap_sem
);
1563 up_write(&kvm
->slots_lock
);
1567 static void allocate_vpid(struct vcpu_vmx
*vmx
)
1572 if (!enable_vpid
|| !cpu_has_vmx_vpid())
1574 spin_lock(&vmx_vpid_lock
);
1575 vpid
= find_first_zero_bit(vmx_vpid_bitmap
, VMX_NR_VPIDS
);
1576 if (vpid
< VMX_NR_VPIDS
) {
1578 __set_bit(vpid
, vmx_vpid_bitmap
);
1580 spin_unlock(&vmx_vpid_lock
);
1583 void vmx_disable_intercept_for_msr(struct page
*msr_bitmap
, u32 msr
)
1587 if (!cpu_has_vmx_msr_bitmap())
1591 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1592 * have the write-low and read-high bitmap offsets the wrong way round.
1593 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1595 va
= kmap(msr_bitmap
);
1596 if (msr
<= 0x1fff) {
1597 __clear_bit(msr
, va
+ 0x000); /* read-low */
1598 __clear_bit(msr
, va
+ 0x800); /* write-low */
1599 } else if ((msr
>= 0xc0000000) && (msr
<= 0xc0001fff)) {
1601 __clear_bit(msr
, va
+ 0x400); /* read-high */
1602 __clear_bit(msr
, va
+ 0xc00); /* write-high */
1608 * Sets up the vmcs for emulated real mode.
1610 static int vmx_vcpu_setup(struct vcpu_vmx
*vmx
)
1612 u32 host_sysenter_cs
;
1615 struct descriptor_table dt
;
1617 unsigned long kvm_vmx_return
;
1621 vmcs_write64(IO_BITMAP_A
, page_to_phys(vmx_io_bitmap_a
));
1622 vmcs_write64(IO_BITMAP_B
, page_to_phys(vmx_io_bitmap_b
));
1624 if (cpu_has_vmx_msr_bitmap())
1625 vmcs_write64(MSR_BITMAP
, page_to_phys(vmx_msr_bitmap
));
1627 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
1630 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL
,
1631 vmcs_config
.pin_based_exec_ctrl
);
1633 exec_control
= vmcs_config
.cpu_based_exec_ctrl
;
1634 if (!vm_need_tpr_shadow(vmx
->vcpu
.kvm
)) {
1635 exec_control
&= ~CPU_BASED_TPR_SHADOW
;
1636 #ifdef CONFIG_X86_64
1637 exec_control
|= CPU_BASED_CR8_STORE_EXITING
|
1638 CPU_BASED_CR8_LOAD_EXITING
;
1641 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, exec_control
);
1643 if (cpu_has_secondary_exec_ctrls()) {
1644 exec_control
= vmcs_config
.cpu_based_2nd_exec_ctrl
;
1645 if (!vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
1647 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
;
1649 exec_control
&= ~SECONDARY_EXEC_ENABLE_VPID
;
1650 vmcs_write32(SECONDARY_VM_EXEC_CONTROL
, exec_control
);
1653 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, !!bypass_guest_pf
);
1654 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, !!bypass_guest_pf
);
1655 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
1657 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
1658 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
1659 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1661 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
1662 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1663 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1664 vmcs_write16(HOST_FS_SELECTOR
, read_fs()); /* 22.2.4 */
1665 vmcs_write16(HOST_GS_SELECTOR
, read_gs()); /* 22.2.4 */
1666 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1667 #ifdef CONFIG_X86_64
1668 rdmsrl(MSR_FS_BASE
, a
);
1669 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
1670 rdmsrl(MSR_GS_BASE
, a
);
1671 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
1673 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
1674 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
1677 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
1680 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
1682 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return
));
1683 vmcs_writel(HOST_RIP
, kvm_vmx_return
); /* 22.2.5 */
1684 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, 0);
1685 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, 0);
1686 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, 0);
1688 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
1689 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
1690 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
1691 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
1692 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
1693 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
1695 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
1696 u32 index
= vmx_msr_index
[i
];
1697 u32 data_low
, data_high
;
1701 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
1703 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
1705 data
= data_low
| ((u64
)data_high
<< 32);
1706 vmx
->host_msrs
[j
].index
= index
;
1707 vmx
->host_msrs
[j
].reserved
= 0;
1708 vmx
->host_msrs
[j
].data
= data
;
1709 vmx
->guest_msrs
[j
] = vmx
->host_msrs
[j
];
1713 vmcs_write32(VM_EXIT_CONTROLS
, vmcs_config
.vmexit_ctrl
);
1715 /* 22.2.1, 20.8.1 */
1716 vmcs_write32(VM_ENTRY_CONTROLS
, vmcs_config
.vmentry_ctrl
);
1718 vmcs_writel(CR0_GUEST_HOST_MASK
, ~0UL);
1719 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
1725 static int vmx_vcpu_reset(struct kvm_vcpu
*vcpu
)
1727 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1731 down_read(&vcpu
->kvm
->slots_lock
);
1732 if (!init_rmode_tss(vmx
->vcpu
.kvm
)) {
1737 vmx
->vcpu
.arch
.rmode
.active
= 0;
1739 vmx
->vcpu
.arch
.regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
1740 kvm_set_cr8(&vmx
->vcpu
, 0);
1741 msr
= 0xfee00000 | MSR_IA32_APICBASE_ENABLE
;
1742 if (vmx
->vcpu
.vcpu_id
== 0)
1743 msr
|= MSR_IA32_APICBASE_BSP
;
1744 kvm_set_apic_base(&vmx
->vcpu
, msr
);
1746 fx_init(&vmx
->vcpu
);
1749 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1750 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1752 if (vmx
->vcpu
.vcpu_id
== 0) {
1753 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
1754 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
1756 vmcs_write16(GUEST_CS_SELECTOR
, vmx
->vcpu
.arch
.sipi_vector
<< 8);
1757 vmcs_writel(GUEST_CS_BASE
, vmx
->vcpu
.arch
.sipi_vector
<< 12);
1759 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1760 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1762 seg_setup(VCPU_SREG_DS
);
1763 seg_setup(VCPU_SREG_ES
);
1764 seg_setup(VCPU_SREG_FS
);
1765 seg_setup(VCPU_SREG_GS
);
1766 seg_setup(VCPU_SREG_SS
);
1768 vmcs_write16(GUEST_TR_SELECTOR
, 0);
1769 vmcs_writel(GUEST_TR_BASE
, 0);
1770 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
1771 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1773 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
1774 vmcs_writel(GUEST_LDTR_BASE
, 0);
1775 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
1776 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
1778 vmcs_write32(GUEST_SYSENTER_CS
, 0);
1779 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
1780 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
1782 vmcs_writel(GUEST_RFLAGS
, 0x02);
1783 if (vmx
->vcpu
.vcpu_id
== 0)
1784 vmcs_writel(GUEST_RIP
, 0xfff0);
1786 vmcs_writel(GUEST_RIP
, 0);
1787 vmcs_writel(GUEST_RSP
, 0);
1789 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
1790 vmcs_writel(GUEST_DR7
, 0x400);
1792 vmcs_writel(GUEST_GDTR_BASE
, 0);
1793 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
1795 vmcs_writel(GUEST_IDTR_BASE
, 0);
1796 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
1798 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
1799 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
1800 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
1804 /* Special registers */
1805 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
1809 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
1811 if (cpu_has_vmx_tpr_shadow()) {
1812 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
, 0);
1813 if (vm_need_tpr_shadow(vmx
->vcpu
.kvm
))
1814 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR
,
1815 page_to_phys(vmx
->vcpu
.arch
.apic
->regs_page
));
1816 vmcs_write32(TPR_THRESHOLD
, 0);
1819 if (vm_need_virtualize_apic_accesses(vmx
->vcpu
.kvm
))
1820 vmcs_write64(APIC_ACCESS_ADDR
,
1821 page_to_phys(vmx
->vcpu
.kvm
->arch
.apic_access_page
));
1824 vmcs_write16(VIRTUAL_PROCESSOR_ID
, vmx
->vpid
);
1826 vmx
->vcpu
.arch
.cr0
= 0x60000010;
1827 vmx_set_cr0(&vmx
->vcpu
, vmx
->vcpu
.arch
.cr0
); /* enter rmode */
1828 vmx_set_cr4(&vmx
->vcpu
, 0);
1829 vmx_set_efer(&vmx
->vcpu
, 0);
1830 vmx_fpu_activate(&vmx
->vcpu
);
1831 update_exception_bitmap(&vmx
->vcpu
);
1833 vpid_sync_vcpu_all(vmx
);
1838 up_read(&vcpu
->kvm
->slots_lock
);
1842 static void vmx_inject_irq(struct kvm_vcpu
*vcpu
, int irq
)
1844 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1846 KVMTRACE_1D(INJ_VIRQ
, vcpu
, (u32
)irq
, handler
);
1848 if (vcpu
->arch
.rmode
.active
) {
1849 vmx
->rmode
.irq
.pending
= true;
1850 vmx
->rmode
.irq
.vector
= irq
;
1851 vmx
->rmode
.irq
.rip
= vmcs_readl(GUEST_RIP
);
1852 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
1853 irq
| INTR_TYPE_SOFT_INTR
| INTR_INFO_VALID_MASK
);
1854 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
, 1);
1855 vmcs_writel(GUEST_RIP
, vmx
->rmode
.irq
.rip
- 1);
1858 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
1859 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
1862 static void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
1864 int word_index
= __ffs(vcpu
->arch
.irq_summary
);
1865 int bit_index
= __ffs(vcpu
->arch
.irq_pending
[word_index
]);
1866 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
1868 clear_bit(bit_index
, &vcpu
->arch
.irq_pending
[word_index
]);
1869 if (!vcpu
->arch
.irq_pending
[word_index
])
1870 clear_bit(word_index
, &vcpu
->arch
.irq_summary
);
1871 vmx_inject_irq(vcpu
, irq
);
1875 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
1876 struct kvm_run
*kvm_run
)
1878 u32 cpu_based_vm_exec_control
;
1880 vcpu
->arch
.interrupt_window_open
=
1881 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
1882 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
1884 if (vcpu
->arch
.interrupt_window_open
&&
1885 vcpu
->arch
.irq_summary
&&
1886 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
) & INTR_INFO_VALID_MASK
))
1888 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1890 kvm_do_inject_irq(vcpu
);
1892 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
1893 if (!vcpu
->arch
.interrupt_window_open
&&
1894 (vcpu
->arch
.irq_summary
|| kvm_run
->request_interrupt_window
))
1896 * Interrupts blocked. Wait for unblock.
1898 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
1900 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
1901 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
1904 static int vmx_set_tss_addr(struct kvm
*kvm
, unsigned int addr
)
1907 struct kvm_userspace_memory_region tss_mem
= {
1909 .guest_phys_addr
= addr
,
1910 .memory_size
= PAGE_SIZE
* 3,
1914 ret
= kvm_set_memory_region(kvm
, &tss_mem
, 0);
1917 kvm
->arch
.tss_addr
= addr
;
1921 static void kvm_guest_debug_pre(struct kvm_vcpu
*vcpu
)
1923 struct kvm_guest_debug
*dbg
= &vcpu
->guest_debug
;
1925 set_debugreg(dbg
->bp
[0], 0);
1926 set_debugreg(dbg
->bp
[1], 1);
1927 set_debugreg(dbg
->bp
[2], 2);
1928 set_debugreg(dbg
->bp
[3], 3);
1930 if (dbg
->singlestep
) {
1931 unsigned long flags
;
1933 flags
= vmcs_readl(GUEST_RFLAGS
);
1934 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
1935 vmcs_writel(GUEST_RFLAGS
, flags
);
1939 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
1940 int vec
, u32 err_code
)
1942 if (!vcpu
->arch
.rmode
.active
)
1946 * Instruction with address size override prefix opcode 0x67
1947 * Cause the #SS fault with 0 error code in VM86 mode.
1949 if (((vec
== GP_VECTOR
) || (vec
== SS_VECTOR
)) && err_code
== 0)
1950 if (emulate_instruction(vcpu
, NULL
, 0, 0, 0) == EMULATE_DONE
)
1955 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1957 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
1958 u32 intr_info
, error_code
;
1959 unsigned long cr2
, rip
;
1961 enum emulation_result er
;
1963 vect_info
= vmx
->idt_vectoring_info
;
1964 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
1966 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
1967 !is_page_fault(intr_info
))
1968 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
1969 "intr info 0x%x\n", __func__
, vect_info
, intr_info
);
1971 if (!irqchip_in_kernel(vcpu
->kvm
) && is_external_interrupt(vect_info
)) {
1972 int irq
= vect_info
& VECTORING_INFO_VECTOR_MASK
;
1973 set_bit(irq
, vcpu
->arch
.irq_pending
);
1974 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1977 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) /* nmi */
1978 return 1; /* already handled by vmx_vcpu_run() */
1980 if (is_no_device(intr_info
)) {
1981 vmx_fpu_activate(vcpu
);
1985 if (is_invalid_opcode(intr_info
)) {
1986 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, EMULTYPE_TRAP_UD
);
1987 if (er
!= EMULATE_DONE
)
1988 kvm_queue_exception(vcpu
, UD_VECTOR
);
1993 rip
= vmcs_readl(GUEST_RIP
);
1994 if (intr_info
& INTR_INFO_DELIVER_CODE_MASK
)
1995 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
1996 if (is_page_fault(intr_info
)) {
1997 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
1998 KVMTRACE_3D(PAGE_FAULT
, vcpu
, error_code
, (u32
)cr2
,
1999 (u32
)((u64
)cr2
>> 32), handler
);
2000 return kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
2003 if (vcpu
->arch
.rmode
.active
&&
2004 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
2006 if (vcpu
->arch
.halt_request
) {
2007 vcpu
->arch
.halt_request
= 0;
2008 return kvm_emulate_halt(vcpu
);
2013 if ((intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
)) ==
2014 (INTR_TYPE_EXCEPTION
| 1)) {
2015 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
2018 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
2019 kvm_run
->ex
.exception
= intr_info
& INTR_INFO_VECTOR_MASK
;
2020 kvm_run
->ex
.error_code
= error_code
;
2024 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
2025 struct kvm_run
*kvm_run
)
2027 ++vcpu
->stat
.irq_exits
;
2028 KVMTRACE_1D(INTR
, vcpu
, vmcs_read32(VM_EXIT_INTR_INFO
), handler
);
2032 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2034 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2038 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2040 unsigned long exit_qualification
;
2041 int size
, down
, in
, string
, rep
;
2044 ++vcpu
->stat
.io_exits
;
2045 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2046 string
= (exit_qualification
& 16) != 0;
2049 if (emulate_instruction(vcpu
,
2050 kvm_run
, 0, 0, 0) == EMULATE_DO_MMIO
)
2055 size
= (exit_qualification
& 7) + 1;
2056 in
= (exit_qualification
& 8) != 0;
2057 down
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_DF
) != 0;
2058 rep
= (exit_qualification
& 32) != 0;
2059 port
= exit_qualification
>> 16;
2061 return kvm_emulate_pio(vcpu
, kvm_run
, in
, size
, port
);
2065 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
2068 * Patch in the VMCALL instruction:
2070 hypercall
[0] = 0x0f;
2071 hypercall
[1] = 0x01;
2072 hypercall
[2] = 0xc1;
2075 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2077 unsigned long exit_qualification
;
2081 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2082 cr
= exit_qualification
& 15;
2083 reg
= (exit_qualification
>> 8) & 15;
2084 switch ((exit_qualification
>> 4) & 3) {
2085 case 0: /* mov to cr */
2086 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
, (u32
)vcpu
->arch
.regs
[reg
],
2087 (u32
)((u64
)vcpu
->arch
.regs
[reg
] >> 32), handler
);
2090 vcpu_load_rsp_rip(vcpu
);
2091 kvm_set_cr0(vcpu
, vcpu
->arch
.regs
[reg
]);
2092 skip_emulated_instruction(vcpu
);
2095 vcpu_load_rsp_rip(vcpu
);
2096 kvm_set_cr3(vcpu
, vcpu
->arch
.regs
[reg
]);
2097 skip_emulated_instruction(vcpu
);
2100 vcpu_load_rsp_rip(vcpu
);
2101 kvm_set_cr4(vcpu
, vcpu
->arch
.regs
[reg
]);
2102 skip_emulated_instruction(vcpu
);
2105 vcpu_load_rsp_rip(vcpu
);
2106 kvm_set_cr8(vcpu
, vcpu
->arch
.regs
[reg
]);
2107 skip_emulated_instruction(vcpu
);
2108 if (irqchip_in_kernel(vcpu
->kvm
))
2110 kvm_run
->exit_reason
= KVM_EXIT_SET_TPR
;
2115 vcpu_load_rsp_rip(vcpu
);
2116 vmx_fpu_deactivate(vcpu
);
2117 vcpu
->arch
.cr0
&= ~X86_CR0_TS
;
2118 vmcs_writel(CR0_READ_SHADOW
, vcpu
->arch
.cr0
);
2119 vmx_fpu_activate(vcpu
);
2120 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2121 skip_emulated_instruction(vcpu
);
2123 case 1: /*mov from cr*/
2126 vcpu_load_rsp_rip(vcpu
);
2127 vcpu
->arch
.regs
[reg
] = vcpu
->arch
.cr3
;
2128 vcpu_put_rsp_rip(vcpu
);
2129 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
,
2130 (u32
)vcpu
->arch
.regs
[reg
],
2131 (u32
)((u64
)vcpu
->arch
.regs
[reg
] >> 32),
2133 skip_emulated_instruction(vcpu
);
2136 vcpu_load_rsp_rip(vcpu
);
2137 vcpu
->arch
.regs
[reg
] = kvm_get_cr8(vcpu
);
2138 vcpu_put_rsp_rip(vcpu
);
2139 KVMTRACE_2D(CR_READ
, vcpu
, (u32
)cr
,
2140 (u32
)vcpu
->arch
.regs
[reg
], handler
);
2141 skip_emulated_instruction(vcpu
);
2146 kvm_lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
2148 skip_emulated_instruction(vcpu
);
2153 kvm_run
->exit_reason
= 0;
2154 pr_unimpl(vcpu
, "unhandled control register: op %d cr %d\n",
2155 (int)(exit_qualification
>> 4) & 3, cr
);
2159 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2161 unsigned long exit_qualification
;
2166 * FIXME: this code assumes the host is debugging the guest.
2167 * need to deal with guest debugging itself too.
2169 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2170 dr
= exit_qualification
& 7;
2171 reg
= (exit_qualification
>> 8) & 15;
2172 vcpu_load_rsp_rip(vcpu
);
2173 if (exit_qualification
& 16) {
2185 vcpu
->arch
.regs
[reg
] = val
;
2186 KVMTRACE_2D(DR_READ
, vcpu
, (u32
)dr
, (u32
)val
, handler
);
2190 vcpu_put_rsp_rip(vcpu
);
2191 skip_emulated_instruction(vcpu
);
2195 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2197 kvm_emulate_cpuid(vcpu
);
2201 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2203 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2206 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
2207 kvm_inject_gp(vcpu
, 0);
2211 KVMTRACE_3D(MSR_READ
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2214 /* FIXME: handling of bits 32:63 of rax, rdx */
2215 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = data
& -1u;
2216 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
2217 skip_emulated_instruction(vcpu
);
2221 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2223 u32 ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2224 u64 data
= (vcpu
->arch
.regs
[VCPU_REGS_RAX
] & -1u)
2225 | ((u64
)(vcpu
->arch
.regs
[VCPU_REGS_RDX
] & -1u) << 32);
2227 KVMTRACE_3D(MSR_WRITE
, vcpu
, ecx
, (u32
)data
, (u32
)(data
>> 32),
2230 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
2231 kvm_inject_gp(vcpu
, 0);
2235 skip_emulated_instruction(vcpu
);
2239 static int handle_tpr_below_threshold(struct kvm_vcpu
*vcpu
,
2240 struct kvm_run
*kvm_run
)
2245 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
2246 struct kvm_run
*kvm_run
)
2248 u32 cpu_based_vm_exec_control
;
2250 /* clear pending irq */
2251 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2252 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
2253 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2255 KVMTRACE_0D(PEND_INTR
, vcpu
, handler
);
2258 * If the user space waits to inject interrupts, exit as soon as
2261 if (kvm_run
->request_interrupt_window
&&
2262 !vcpu
->arch
.irq_summary
) {
2263 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
2264 ++vcpu
->stat
.irq_window_exits
;
2270 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2272 skip_emulated_instruction(vcpu
);
2273 return kvm_emulate_halt(vcpu
);
2276 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2278 skip_emulated_instruction(vcpu
);
2279 kvm_emulate_hypercall(vcpu
);
2283 static int handle_wbinvd(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2285 skip_emulated_instruction(vcpu
);
2286 /* TODO: Add support for VT-d/pass-through device */
2290 static int handle_apic_access(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2292 u64 exit_qualification
;
2293 enum emulation_result er
;
2294 unsigned long offset
;
2296 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
2297 offset
= exit_qualification
& 0xffful
;
2299 KVMTRACE_1D(APIC_ACCESS
, vcpu
, (u32
)offset
, handler
);
2301 er
= emulate_instruction(vcpu
, kvm_run
, 0, 0, 0);
2303 if (er
!= EMULATE_DONE
) {
2305 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2312 static int handle_task_switch(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2314 unsigned long exit_qualification
;
2318 exit_qualification
= vmcs_readl(EXIT_QUALIFICATION
);
2320 reason
= (u32
)exit_qualification
>> 30;
2321 tss_selector
= exit_qualification
;
2323 return kvm_task_switch(vcpu
, tss_selector
, reason
);
2327 * The exit handlers return 1 if the exit was handled fully and guest execution
2328 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2329 * to be done to userspace and return 0.
2331 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
2332 struct kvm_run
*kvm_run
) = {
2333 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
2334 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
2335 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
2336 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
2337 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
2338 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
2339 [EXIT_REASON_CPUID
] = handle_cpuid
,
2340 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
2341 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
2342 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
2343 [EXIT_REASON_HLT
] = handle_halt
,
2344 [EXIT_REASON_VMCALL
] = handle_vmcall
,
2345 [EXIT_REASON_TPR_BELOW_THRESHOLD
] = handle_tpr_below_threshold
,
2346 [EXIT_REASON_APIC_ACCESS
] = handle_apic_access
,
2347 [EXIT_REASON_WBINVD
] = handle_wbinvd
,
2348 [EXIT_REASON_TASK_SWITCH
] = handle_task_switch
,
2351 static const int kvm_vmx_max_exit_handlers
=
2352 ARRAY_SIZE(kvm_vmx_exit_handlers
);
2355 * The guest has exited. See if we can fix it or if we need userspace
2358 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
2360 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
2361 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2362 u32 vectoring_info
= vmx
->idt_vectoring_info
;
2364 KVMTRACE_3D(VMEXIT
, vcpu
, exit_reason
, (u32
)vmcs_readl(GUEST_RIP
),
2365 (u32
)((u64
)vmcs_readl(GUEST_RIP
) >> 32), entryexit
);
2367 if (unlikely(vmx
->fail
)) {
2368 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
2369 kvm_run
->fail_entry
.hardware_entry_failure_reason
2370 = vmcs_read32(VM_INSTRUCTION_ERROR
);
2374 if ((vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
2375 exit_reason
!= EXIT_REASON_EXCEPTION_NMI
)
2376 printk(KERN_WARNING
"%s: unexpected, valid vectoring info and "
2377 "exit reason is 0x%x\n", __func__
, exit_reason
);
2378 if (exit_reason
< kvm_vmx_max_exit_handlers
2379 && kvm_vmx_exit_handlers
[exit_reason
])
2380 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
2382 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
2383 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
2388 static void update_tpr_threshold(struct kvm_vcpu
*vcpu
)
2392 if (!vm_need_tpr_shadow(vcpu
->kvm
))
2395 if (!kvm_lapic_enabled(vcpu
) ||
2396 ((max_irr
= kvm_lapic_find_highest_irr(vcpu
)) == -1)) {
2397 vmcs_write32(TPR_THRESHOLD
, 0);
2401 tpr
= (kvm_lapic_get_cr8(vcpu
) & 0x0f) << 4;
2402 vmcs_write32(TPR_THRESHOLD
, (max_irr
> tpr
) ? tpr
>> 4 : max_irr
>> 4);
2405 static void enable_irq_window(struct kvm_vcpu
*vcpu
)
2407 u32 cpu_based_vm_exec_control
;
2409 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
2410 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
2411 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
2414 static void vmx_intr_assist(struct kvm_vcpu
*vcpu
)
2416 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2417 u32 idtv_info_field
, intr_info_field
;
2418 int has_ext_irq
, interrupt_window_open
;
2421 update_tpr_threshold(vcpu
);
2423 has_ext_irq
= kvm_cpu_has_interrupt(vcpu
);
2424 intr_info_field
= vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
);
2425 idtv_info_field
= vmx
->idt_vectoring_info
;
2426 if (intr_info_field
& INTR_INFO_VALID_MASK
) {
2427 if (idtv_info_field
& INTR_INFO_VALID_MASK
) {
2428 /* TODO: fault when IDT_Vectoring */
2429 if (printk_ratelimit())
2430 printk(KERN_ERR
"Fault when IDT_Vectoring\n");
2433 enable_irq_window(vcpu
);
2436 if (unlikely(idtv_info_field
& INTR_INFO_VALID_MASK
)) {
2437 if ((idtv_info_field
& VECTORING_INFO_TYPE_MASK
)
2438 == INTR_TYPE_EXT_INTR
2439 && vcpu
->arch
.rmode
.active
) {
2440 u8 vect
= idtv_info_field
& VECTORING_INFO_VECTOR_MASK
;
2442 vmx_inject_irq(vcpu
, vect
);
2443 if (unlikely(has_ext_irq
))
2444 enable_irq_window(vcpu
);
2448 KVMTRACE_1D(REDELIVER_EVT
, vcpu
, idtv_info_field
, handler
);
2450 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, idtv_info_field
);
2451 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN
,
2452 vmcs_read32(VM_EXIT_INSTRUCTION_LEN
));
2454 if (unlikely(idtv_info_field
& INTR_INFO_DELIVER_CODE_MASK
))
2455 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
,
2456 vmcs_read32(IDT_VECTORING_ERROR_CODE
));
2457 if (unlikely(has_ext_irq
))
2458 enable_irq_window(vcpu
);
2463 interrupt_window_open
=
2464 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
2465 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
2466 if (interrupt_window_open
) {
2467 vector
= kvm_cpu_get_interrupt(vcpu
);
2468 vmx_inject_irq(vcpu
, vector
);
2469 kvm_timer_intr_post(vcpu
, vector
);
2471 enable_irq_window(vcpu
);
2475 * Failure to inject an interrupt should give us the information
2476 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2477 * when fetching the interrupt redirection bitmap in the real-mode
2478 * tss, this doesn't happen. So we do it ourselves.
2480 static void fixup_rmode_irq(struct vcpu_vmx
*vmx
)
2482 vmx
->rmode
.irq
.pending
= 0;
2483 if (vmcs_readl(GUEST_RIP
) + 1 != vmx
->rmode
.irq
.rip
)
2485 vmcs_writel(GUEST_RIP
, vmx
->rmode
.irq
.rip
);
2486 if (vmx
->idt_vectoring_info
& VECTORING_INFO_VALID_MASK
) {
2487 vmx
->idt_vectoring_info
&= ~VECTORING_INFO_TYPE_MASK
;
2488 vmx
->idt_vectoring_info
|= INTR_TYPE_EXT_INTR
;
2491 vmx
->idt_vectoring_info
=
2492 VECTORING_INFO_VALID_MASK
2493 | INTR_TYPE_EXT_INTR
2494 | vmx
->rmode
.irq
.vector
;
2497 static void vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2499 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2503 * Loading guest fpu may have cleared host cr0.ts
2505 vmcs_writel(HOST_CR0
, read_cr0());
2508 /* Store host registers */
2509 #ifdef CONFIG_X86_64
2510 "push %%rdx; push %%rbp;"
2513 "push %%edx; push %%ebp;"
2516 ASM_VMX_VMWRITE_RSP_RDX
"\n\t"
2517 /* Check if vmlaunch of vmresume is needed */
2518 "cmpl $0, %c[launched](%0) \n\t"
2519 /* Load guest registers. Don't clobber flags. */
2520 #ifdef CONFIG_X86_64
2521 "mov %c[cr2](%0), %%rax \n\t"
2522 "mov %%rax, %%cr2 \n\t"
2523 "mov %c[rax](%0), %%rax \n\t"
2524 "mov %c[rbx](%0), %%rbx \n\t"
2525 "mov %c[rdx](%0), %%rdx \n\t"
2526 "mov %c[rsi](%0), %%rsi \n\t"
2527 "mov %c[rdi](%0), %%rdi \n\t"
2528 "mov %c[rbp](%0), %%rbp \n\t"
2529 "mov %c[r8](%0), %%r8 \n\t"
2530 "mov %c[r9](%0), %%r9 \n\t"
2531 "mov %c[r10](%0), %%r10 \n\t"
2532 "mov %c[r11](%0), %%r11 \n\t"
2533 "mov %c[r12](%0), %%r12 \n\t"
2534 "mov %c[r13](%0), %%r13 \n\t"
2535 "mov %c[r14](%0), %%r14 \n\t"
2536 "mov %c[r15](%0), %%r15 \n\t"
2537 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
2539 "mov %c[cr2](%0), %%eax \n\t"
2540 "mov %%eax, %%cr2 \n\t"
2541 "mov %c[rax](%0), %%eax \n\t"
2542 "mov %c[rbx](%0), %%ebx \n\t"
2543 "mov %c[rdx](%0), %%edx \n\t"
2544 "mov %c[rsi](%0), %%esi \n\t"
2545 "mov %c[rdi](%0), %%edi \n\t"
2546 "mov %c[rbp](%0), %%ebp \n\t"
2547 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
2549 /* Enter guest mode */
2550 "jne .Llaunched \n\t"
2551 ASM_VMX_VMLAUNCH
"\n\t"
2552 "jmp .Lkvm_vmx_return \n\t"
2553 ".Llaunched: " ASM_VMX_VMRESUME
"\n\t"
2554 ".Lkvm_vmx_return: "
2555 /* Save guest registers, load host registers, keep flags */
2556 #ifdef CONFIG_X86_64
2557 "xchg %0, (%%rsp) \n\t"
2558 "mov %%rax, %c[rax](%0) \n\t"
2559 "mov %%rbx, %c[rbx](%0) \n\t"
2560 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2561 "mov %%rdx, %c[rdx](%0) \n\t"
2562 "mov %%rsi, %c[rsi](%0) \n\t"
2563 "mov %%rdi, %c[rdi](%0) \n\t"
2564 "mov %%rbp, %c[rbp](%0) \n\t"
2565 "mov %%r8, %c[r8](%0) \n\t"
2566 "mov %%r9, %c[r9](%0) \n\t"
2567 "mov %%r10, %c[r10](%0) \n\t"
2568 "mov %%r11, %c[r11](%0) \n\t"
2569 "mov %%r12, %c[r12](%0) \n\t"
2570 "mov %%r13, %c[r13](%0) \n\t"
2571 "mov %%r14, %c[r14](%0) \n\t"
2572 "mov %%r15, %c[r15](%0) \n\t"
2573 "mov %%cr2, %%rax \n\t"
2574 "mov %%rax, %c[cr2](%0) \n\t"
2576 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
2578 "xchg %0, (%%esp) \n\t"
2579 "mov %%eax, %c[rax](%0) \n\t"
2580 "mov %%ebx, %c[rbx](%0) \n\t"
2581 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2582 "mov %%edx, %c[rdx](%0) \n\t"
2583 "mov %%esi, %c[rsi](%0) \n\t"
2584 "mov %%edi, %c[rdi](%0) \n\t"
2585 "mov %%ebp, %c[rbp](%0) \n\t"
2586 "mov %%cr2, %%eax \n\t"
2587 "mov %%eax, %c[cr2](%0) \n\t"
2589 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
2591 "setbe %c[fail](%0) \n\t"
2592 : : "c"(vmx
), "d"((unsigned long)HOST_RSP
),
2593 [launched
]"i"(offsetof(struct vcpu_vmx
, launched
)),
2594 [fail
]"i"(offsetof(struct vcpu_vmx
, fail
)),
2595 [rax
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RAX
])),
2596 [rbx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBX
])),
2597 [rcx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RCX
])),
2598 [rdx
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDX
])),
2599 [rsi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RSI
])),
2600 [rdi
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RDI
])),
2601 [rbp
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_RBP
])),
2602 #ifdef CONFIG_X86_64
2603 [r8
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R8
])),
2604 [r9
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R9
])),
2605 [r10
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R10
])),
2606 [r11
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R11
])),
2607 [r12
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R12
])),
2608 [r13
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R13
])),
2609 [r14
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R14
])),
2610 [r15
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.regs
[VCPU_REGS_R15
])),
2612 [cr2
]"i"(offsetof(struct vcpu_vmx
, vcpu
.arch
.cr2
))
2614 #ifdef CONFIG_X86_64
2615 , "rbx", "rdi", "rsi"
2616 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2618 , "ebx", "edi", "rsi"
2622 vmx
->idt_vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
2623 if (vmx
->rmode
.irq
.pending
)
2624 fixup_rmode_irq(vmx
);
2626 vcpu
->arch
.interrupt_window_open
=
2627 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0;
2629 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
2632 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
2634 /* We need to handle NMIs before interrupts are enabled */
2635 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) { /* nmi */
2636 KVMTRACE_0D(NMI
, vcpu
, handler
);
2641 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
2643 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2646 on_each_cpu(__vcpu_clear
, vmx
, 0, 1);
2647 free_vmcs(vmx
->vmcs
);
2652 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
2654 struct vcpu_vmx
*vmx
= to_vmx(vcpu
);
2656 spin_lock(&vmx_vpid_lock
);
2658 __clear_bit(vmx
->vpid
, vmx_vpid_bitmap
);
2659 spin_unlock(&vmx_vpid_lock
);
2660 vmx_free_vmcs(vcpu
);
2661 kfree(vmx
->host_msrs
);
2662 kfree(vmx
->guest_msrs
);
2663 kvm_vcpu_uninit(vcpu
);
2664 kmem_cache_free(kvm_vcpu_cache
, vmx
);
2667 static struct kvm_vcpu
*vmx_create_vcpu(struct kvm
*kvm
, unsigned int id
)
2670 struct vcpu_vmx
*vmx
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
2674 return ERR_PTR(-ENOMEM
);
2678 err
= kvm_vcpu_init(&vmx
->vcpu
, kvm
, id
);
2682 vmx
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2683 if (!vmx
->guest_msrs
) {
2688 vmx
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2689 if (!vmx
->host_msrs
)
2690 goto free_guest_msrs
;
2692 vmx
->vmcs
= alloc_vmcs();
2696 vmcs_clear(vmx
->vmcs
);
2699 vmx_vcpu_load(&vmx
->vcpu
, cpu
);
2700 err
= vmx_vcpu_setup(vmx
);
2701 vmx_vcpu_put(&vmx
->vcpu
);
2705 if (vm_need_virtualize_apic_accesses(kvm
))
2706 if (alloc_apic_access_page(kvm
) != 0)
2712 free_vmcs(vmx
->vmcs
);
2714 kfree(vmx
->host_msrs
);
2716 kfree(vmx
->guest_msrs
);
2718 kvm_vcpu_uninit(&vmx
->vcpu
);
2720 kmem_cache_free(kvm_vcpu_cache
, vmx
);
2721 return ERR_PTR(err
);
2724 static void __init
vmx_check_processor_compat(void *rtn
)
2726 struct vmcs_config vmcs_conf
;
2729 if (setup_vmcs_config(&vmcs_conf
) < 0)
2731 if (memcmp(&vmcs_config
, &vmcs_conf
, sizeof(struct vmcs_config
)) != 0) {
2732 printk(KERN_ERR
"kvm: CPU %d feature inconsistency!\n",
2733 smp_processor_id());
2738 static struct kvm_x86_ops vmx_x86_ops
= {
2739 .cpu_has_kvm_support
= cpu_has_kvm_support
,
2740 .disabled_by_bios
= vmx_disabled_by_bios
,
2741 .hardware_setup
= hardware_setup
,
2742 .hardware_unsetup
= hardware_unsetup
,
2743 .check_processor_compatibility
= vmx_check_processor_compat
,
2744 .hardware_enable
= hardware_enable
,
2745 .hardware_disable
= hardware_disable
,
2746 .cpu_has_accelerated_tpr
= cpu_has_vmx_virtualize_apic_accesses
,
2748 .vcpu_create
= vmx_create_vcpu
,
2749 .vcpu_free
= vmx_free_vcpu
,
2750 .vcpu_reset
= vmx_vcpu_reset
,
2752 .prepare_guest_switch
= vmx_save_host_state
,
2753 .vcpu_load
= vmx_vcpu_load
,
2754 .vcpu_put
= vmx_vcpu_put
,
2755 .vcpu_decache
= vmx_vcpu_decache
,
2757 .set_guest_debug
= set_guest_debug
,
2758 .guest_debug_pre
= kvm_guest_debug_pre
,
2759 .get_msr
= vmx_get_msr
,
2760 .set_msr
= vmx_set_msr
,
2761 .get_segment_base
= vmx_get_segment_base
,
2762 .get_segment
= vmx_get_segment
,
2763 .set_segment
= vmx_set_segment
,
2764 .get_cpl
= vmx_get_cpl
,
2765 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
2766 .decache_cr4_guest_bits
= vmx_decache_cr4_guest_bits
,
2767 .set_cr0
= vmx_set_cr0
,
2768 .set_cr3
= vmx_set_cr3
,
2769 .set_cr4
= vmx_set_cr4
,
2770 .set_efer
= vmx_set_efer
,
2771 .get_idt
= vmx_get_idt
,
2772 .set_idt
= vmx_set_idt
,
2773 .get_gdt
= vmx_get_gdt
,
2774 .set_gdt
= vmx_set_gdt
,
2775 .cache_regs
= vcpu_load_rsp_rip
,
2776 .decache_regs
= vcpu_put_rsp_rip
,
2777 .get_rflags
= vmx_get_rflags
,
2778 .set_rflags
= vmx_set_rflags
,
2780 .tlb_flush
= vmx_flush_tlb
,
2782 .run
= vmx_vcpu_run
,
2783 .handle_exit
= kvm_handle_exit
,
2784 .skip_emulated_instruction
= skip_emulated_instruction
,
2785 .patch_hypercall
= vmx_patch_hypercall
,
2786 .get_irq
= vmx_get_irq
,
2787 .set_irq
= vmx_inject_irq
,
2788 .queue_exception
= vmx_queue_exception
,
2789 .exception_injected
= vmx_exception_injected
,
2790 .inject_pending_irq
= vmx_intr_assist
,
2791 .inject_pending_vectors
= do_interrupt_requests
,
2793 .set_tss_addr
= vmx_set_tss_addr
,
2796 static int __init
vmx_init(void)
2801 vmx_io_bitmap_a
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2802 if (!vmx_io_bitmap_a
)
2805 vmx_io_bitmap_b
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2806 if (!vmx_io_bitmap_b
) {
2811 vmx_msr_bitmap
= alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2812 if (!vmx_msr_bitmap
) {
2818 * Allow direct access to the PC debug port (it is often used for I/O
2819 * delays, but the vmexits simply slow things down).
2821 va
= kmap(vmx_io_bitmap_a
);
2822 memset(va
, 0xff, PAGE_SIZE
);
2823 clear_bit(0x80, va
);
2824 kunmap(vmx_io_bitmap_a
);
2826 va
= kmap(vmx_io_bitmap_b
);
2827 memset(va
, 0xff, PAGE_SIZE
);
2828 kunmap(vmx_io_bitmap_b
);
2830 va
= kmap(vmx_msr_bitmap
);
2831 memset(va
, 0xff, PAGE_SIZE
);
2832 kunmap(vmx_msr_bitmap
);
2834 set_bit(0, vmx_vpid_bitmap
); /* 0 is reserved for host */
2836 r
= kvm_init(&vmx_x86_ops
, sizeof(struct vcpu_vmx
), THIS_MODULE
);
2840 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_FS_BASE
);
2841 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_GS_BASE
);
2842 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_CS
);
2843 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_ESP
);
2844 vmx_disable_intercept_for_msr(vmx_msr_bitmap
, MSR_IA32_SYSENTER_EIP
);
2846 if (bypass_guest_pf
)
2847 kvm_mmu_set_nonpresent_ptes(~0xffeull
, 0ull);
2852 __free_page(vmx_msr_bitmap
);
2854 __free_page(vmx_io_bitmap_b
);
2856 __free_page(vmx_io_bitmap_a
);
2860 static void __exit
vmx_exit(void)
2862 __free_page(vmx_msr_bitmap
);
2863 __free_page(vmx_io_bitmap_b
);
2864 __free_page(vmx_io_bitmap_a
);
2869 module_init(vmx_init
)
2870 module_exit(vmx_exit
)