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.
20 #include "segment_descriptor.h"
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/highmem.h>
26 #include <linux/profile.h>
27 #include <linux/sched.h>
32 MODULE_AUTHOR("Qumranet");
33 MODULE_LICENSE("GPL");
44 struct kvm_msr_entry *guest_msrs;
45 struct kvm_msr_entry *host_msrs;
50 int msr_offset_kernel_gs_base;
55 u16 fs_sel, gs_sel, ldt_sel;
56 int fs_gs_ldt_reload_needed;
61 static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
63 return container_of(vcpu, struct vcpu_vmx, vcpu);
66 static int init_rmode_tss(struct kvm *kvm);
68 static DEFINE_PER_CPU(struct vmcs *, vmxarea);
69 static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
71 static struct page *vmx_io_bitmap_a;
72 static struct page *vmx_io_bitmap_b;
74 #define EFER_SAVE_RESTORE_BITS ((u64)EFER_SCE)
76 static struct vmcs_config {
80 u32 pin_based_exec_ctrl;
81 u32 cpu_based_exec_ctrl;
86 #define VMX_SEGMENT_FIELD(seg) \
87 [VCPU_SREG_##seg] = { \
88 .selector = GUEST_##seg##_SELECTOR, \
89 .base = GUEST_##seg##_BASE, \
90 .limit = GUEST_##seg##_LIMIT, \
91 .ar_bytes = GUEST_##seg##_AR_BYTES, \
94 static struct kvm_vmx_segment_field {
99 } kvm_vmx_segment_fields[] = {
100 VMX_SEGMENT_FIELD(CS),
101 VMX_SEGMENT_FIELD(DS),
102 VMX_SEGMENT_FIELD(ES),
103 VMX_SEGMENT_FIELD(FS),
104 VMX_SEGMENT_FIELD(GS),
105 VMX_SEGMENT_FIELD(SS),
106 VMX_SEGMENT_FIELD(TR),
107 VMX_SEGMENT_FIELD(LDTR),
111 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
112 * away by decrementing the array size.
114 static const u32 vmx_msr_index[] = {
116 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
118 MSR_EFER, MSR_K6_STAR,
120 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
122 static void load_msrs(struct kvm_msr_entry *e, int n)
126 for (i = 0; i < n; ++i)
127 wrmsrl(e[i].index, e[i].data);
130 static void save_msrs(struct kvm_msr_entry *e, int n)
134 for (i = 0; i < n; ++i)
135 rdmsrl(e[i].index, e[i].data);
138 static inline u64 msr_efer_save_restore_bits(struct kvm_msr_entry msr)
140 return (u64)msr.data & EFER_SAVE_RESTORE_BITS;
143 static inline int msr_efer_need_save_restore(struct kvm_vcpu *vcpu)
145 struct vcpu_vmx *vmx = to_vmx(vcpu);
146 int efer_offset = vmx->msr_offset_efer;
147 return msr_efer_save_restore_bits(vmx->host_msrs[efer_offset]) !=
148 msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
151 static inline int is_page_fault(u32 intr_info)
153 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
154 INTR_INFO_VALID_MASK)) ==
155 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
158 static inline int is_no_device(u32 intr_info)
160 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
161 INTR_INFO_VALID_MASK)) ==
162 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
165 static inline int is_external_interrupt(u32 intr_info)
167 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
168 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
171 static int __find_msr_index(struct kvm_vcpu *vcpu, u32 msr)
173 struct vcpu_vmx *vmx = to_vmx(vcpu);
176 for (i = 0; i < vmx->nmsrs; ++i)
177 if (vmx->guest_msrs[i].index == msr)
182 static struct kvm_msr_entry *find_msr_entry(struct kvm_vcpu *vcpu, u32 msr)
184 struct vcpu_vmx *vmx = to_vmx(vcpu);
187 i = __find_msr_index(vcpu, msr);
189 return &vmx->guest_msrs[i];
193 static void vmcs_clear(struct vmcs *vmcs)
195 u64 phys_addr = __pa(vmcs);
198 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
199 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
202 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
206 static void __vcpu_clear(void *arg)
208 struct kvm_vcpu *vcpu = arg;
209 struct vcpu_vmx *vmx = to_vmx(vcpu);
210 int cpu = raw_smp_processor_id();
212 if (vcpu->cpu == cpu)
213 vmcs_clear(vmx->vmcs);
214 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
215 per_cpu(current_vmcs, cpu) = NULL;
216 rdtscll(vcpu->host_tsc);
219 static void vcpu_clear(struct kvm_vcpu *vcpu)
221 if (vcpu->cpu != raw_smp_processor_id() && vcpu->cpu != -1)
222 smp_call_function_single(vcpu->cpu, __vcpu_clear, vcpu, 0, 1);
225 to_vmx(vcpu)->launched = 0;
228 static unsigned long vmcs_readl(unsigned long field)
232 asm volatile (ASM_VMX_VMREAD_RDX_RAX
233 : "=a"(value) : "d"(field) : "cc");
237 static u16 vmcs_read16(unsigned long field)
239 return vmcs_readl(field);
242 static u32 vmcs_read32(unsigned long field)
244 return vmcs_readl(field);
247 static u64 vmcs_read64(unsigned long field)
250 return vmcs_readl(field);
252 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
256 static noinline void vmwrite_error(unsigned long field, unsigned long value)
258 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
259 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
263 static void vmcs_writel(unsigned long field, unsigned long value)
267 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
268 : "=q"(error) : "a"(value), "d"(field) : "cc" );
270 vmwrite_error(field, value);
273 static void vmcs_write16(unsigned long field, u16 value)
275 vmcs_writel(field, value);
278 static void vmcs_write32(unsigned long field, u32 value)
280 vmcs_writel(field, value);
283 static void vmcs_write64(unsigned long field, u64 value)
286 vmcs_writel(field, value);
288 vmcs_writel(field, value);
290 vmcs_writel(field+1, value >> 32);
294 static void vmcs_clear_bits(unsigned long field, u32 mask)
296 vmcs_writel(field, vmcs_readl(field) & ~mask);
299 static void vmcs_set_bits(unsigned long field, u32 mask)
301 vmcs_writel(field, vmcs_readl(field) | mask);
304 static void update_exception_bitmap(struct kvm_vcpu *vcpu)
308 eb = 1u << PF_VECTOR;
309 if (!vcpu->fpu_active)
310 eb |= 1u << NM_VECTOR;
311 if (vcpu->guest_debug.enabled)
313 if (vcpu->rmode.active)
315 vmcs_write32(EXCEPTION_BITMAP, eb);
318 static void reload_tss(void)
320 #ifndef CONFIG_X86_64
323 * VT restores TR but not its size. Useless.
325 struct descriptor_table gdt;
326 struct segment_descriptor *descs;
329 descs = (void *)gdt.base;
330 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
335 static void load_transition_efer(struct kvm_vcpu *vcpu)
338 struct vcpu_vmx *vmx = to_vmx(vcpu);
339 int efer_offset = vmx->msr_offset_efer;
341 trans_efer = vmx->host_msrs[efer_offset].data;
342 trans_efer &= ~EFER_SAVE_RESTORE_BITS;
343 trans_efer |= msr_efer_save_restore_bits(vmx->guest_msrs[efer_offset]);
344 wrmsrl(MSR_EFER, trans_efer);
345 vcpu->stat.efer_reload++;
348 static void vmx_save_host_state(struct kvm_vcpu *vcpu)
350 struct vcpu_vmx *vmx = to_vmx(vcpu);
352 if (vmx->host_state.loaded)
355 vmx->host_state.loaded = 1;
357 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
358 * allow segment selectors with cpl > 0 or ti == 1.
360 vmx->host_state.ldt_sel = read_ldt();
361 vmx->host_state.fs_gs_ldt_reload_needed = vmx->host_state.ldt_sel;
362 vmx->host_state.fs_sel = read_fs();
363 if (!(vmx->host_state.fs_sel & 7))
364 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
366 vmcs_write16(HOST_FS_SELECTOR, 0);
367 vmx->host_state.fs_gs_ldt_reload_needed = 1;
369 vmx->host_state.gs_sel = read_gs();
370 if (!(vmx->host_state.gs_sel & 7))
371 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
373 vmcs_write16(HOST_GS_SELECTOR, 0);
374 vmx->host_state.fs_gs_ldt_reload_needed = 1;
378 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
379 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
381 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
382 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
386 if (is_long_mode(vcpu)) {
387 save_msrs(vmx->host_msrs +
388 vmx->msr_offset_kernel_gs_base, 1);
391 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
392 if (msr_efer_need_save_restore(vcpu))
393 load_transition_efer(vcpu);
396 static void vmx_load_host_state(struct kvm_vcpu *vcpu)
398 struct vcpu_vmx *vmx = to_vmx(vcpu);
401 if (!vmx->host_state.loaded)
404 vmx->host_state.loaded = 0;
405 if (vmx->host_state.fs_gs_ldt_reload_needed) {
406 load_ldt(vmx->host_state.ldt_sel);
407 load_fs(vmx->host_state.fs_sel);
409 * If we have to reload gs, we must take care to
410 * preserve our gs base.
412 local_irq_save(flags);
413 load_gs(vmx->host_state.gs_sel);
415 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
417 local_irq_restore(flags);
421 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
422 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
423 if (msr_efer_need_save_restore(vcpu))
424 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
428 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
429 * vcpu mutex is already taken.
431 static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
433 struct vcpu_vmx *vmx = to_vmx(vcpu);
434 u64 phys_addr = __pa(vmx->vmcs);
437 if (vcpu->cpu != cpu)
440 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
443 per_cpu(current_vmcs, cpu) = vmx->vmcs;
444 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
445 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
448 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
449 vmx->vmcs, phys_addr);
452 if (vcpu->cpu != cpu) {
453 struct descriptor_table dt;
454 unsigned long sysenter_esp;
458 * Linux uses per-cpu TSS and GDT, so set these when switching
461 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
463 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
465 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
466 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
469 * Make sure the time stamp counter is monotonous.
472 delta = vcpu->host_tsc - tsc_this;
473 vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta);
477 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
479 vmx_load_host_state(vcpu);
480 kvm_put_guest_fpu(vcpu);
483 static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
485 if (vcpu->fpu_active)
487 vcpu->fpu_active = 1;
488 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
489 if (vcpu->cr0 & X86_CR0_TS)
490 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
491 update_exception_bitmap(vcpu);
494 static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
496 if (!vcpu->fpu_active)
498 vcpu->fpu_active = 0;
499 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
500 update_exception_bitmap(vcpu);
503 static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
508 static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
510 return vmcs_readl(GUEST_RFLAGS);
513 static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
515 vmcs_writel(GUEST_RFLAGS, rflags);
518 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
521 u32 interruptibility;
523 rip = vmcs_readl(GUEST_RIP);
524 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
525 vmcs_writel(GUEST_RIP, rip);
528 * We emulated an instruction, so temporary interrupt blocking
529 * should be removed, if set.
531 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
532 if (interruptibility & 3)
533 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
534 interruptibility & ~3);
535 vcpu->interrupt_window_open = 1;
538 static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
540 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
541 vmcs_readl(GUEST_RIP));
542 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
543 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
545 INTR_TYPE_EXCEPTION |
546 INTR_INFO_DELIEVER_CODE_MASK |
547 INTR_INFO_VALID_MASK);
551 * Swap MSR entry in host/guest MSR entry array.
553 void move_msr_up(struct kvm_vcpu *vcpu, int from, int to)
555 struct vcpu_vmx *vmx = to_vmx(vcpu);
556 struct kvm_msr_entry tmp;
558 tmp = vmx->guest_msrs[to];
559 vmx->guest_msrs[to] = vmx->guest_msrs[from];
560 vmx->guest_msrs[from] = tmp;
561 tmp = vmx->host_msrs[to];
562 vmx->host_msrs[to] = vmx->host_msrs[from];
563 vmx->host_msrs[from] = tmp;
567 * Set up the vmcs to automatically save and restore system
568 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
569 * mode, as fiddling with msrs is very expensive.
571 static void setup_msrs(struct kvm_vcpu *vcpu)
573 struct vcpu_vmx *vmx = to_vmx(vcpu);
578 if (is_long_mode(vcpu)) {
581 index = __find_msr_index(vcpu, MSR_SYSCALL_MASK);
583 move_msr_up(vcpu, index, save_nmsrs++);
584 index = __find_msr_index(vcpu, MSR_LSTAR);
586 move_msr_up(vcpu, index, save_nmsrs++);
587 index = __find_msr_index(vcpu, MSR_CSTAR);
589 move_msr_up(vcpu, index, save_nmsrs++);
590 index = __find_msr_index(vcpu, MSR_KERNEL_GS_BASE);
592 move_msr_up(vcpu, index, save_nmsrs++);
594 * MSR_K6_STAR is only needed on long mode guests, and only
595 * if efer.sce is enabled.
597 index = __find_msr_index(vcpu, MSR_K6_STAR);
598 if ((index >= 0) && (vcpu->shadow_efer & EFER_SCE))
599 move_msr_up(vcpu, index, save_nmsrs++);
602 vmx->save_nmsrs = save_nmsrs;
605 vmx->msr_offset_kernel_gs_base =
606 __find_msr_index(vcpu, MSR_KERNEL_GS_BASE);
608 vmx->msr_offset_efer = __find_msr_index(vcpu, MSR_EFER);
612 * reads and returns guest's timestamp counter "register"
613 * guest_tsc = host_tsc + tsc_offset -- 21.3
615 static u64 guest_read_tsc(void)
617 u64 host_tsc, tsc_offset;
620 tsc_offset = vmcs_read64(TSC_OFFSET);
621 return host_tsc + tsc_offset;
625 * writes 'guest_tsc' into guest's timestamp counter "register"
626 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
628 static void guest_write_tsc(u64 guest_tsc)
633 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
637 * Reads an msr value (of 'msr_index') into 'pdata'.
638 * Returns 0 on success, non-0 otherwise.
639 * Assumes vcpu_load() was already called.
641 static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
644 struct kvm_msr_entry *msr;
647 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
654 data = vmcs_readl(GUEST_FS_BASE);
657 data = vmcs_readl(GUEST_GS_BASE);
660 return kvm_get_msr_common(vcpu, msr_index, pdata);
662 case MSR_IA32_TIME_STAMP_COUNTER:
663 data = guest_read_tsc();
665 case MSR_IA32_SYSENTER_CS:
666 data = vmcs_read32(GUEST_SYSENTER_CS);
668 case MSR_IA32_SYSENTER_EIP:
669 data = vmcs_readl(GUEST_SYSENTER_EIP);
671 case MSR_IA32_SYSENTER_ESP:
672 data = vmcs_readl(GUEST_SYSENTER_ESP);
675 msr = find_msr_entry(vcpu, msr_index);
680 return kvm_get_msr_common(vcpu, msr_index, pdata);
688 * Writes msr value into into the appropriate "register".
689 * Returns 0 on success, non-0 otherwise.
690 * Assumes vcpu_load() was already called.
692 static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
694 struct vcpu_vmx *vmx = to_vmx(vcpu);
695 struct kvm_msr_entry *msr;
701 ret = kvm_set_msr_common(vcpu, msr_index, data);
702 if (vmx->host_state.loaded)
703 load_transition_efer(vcpu);
706 vmcs_writel(GUEST_FS_BASE, data);
709 vmcs_writel(GUEST_GS_BASE, data);
712 case MSR_IA32_SYSENTER_CS:
713 vmcs_write32(GUEST_SYSENTER_CS, data);
715 case MSR_IA32_SYSENTER_EIP:
716 vmcs_writel(GUEST_SYSENTER_EIP, data);
718 case MSR_IA32_SYSENTER_ESP:
719 vmcs_writel(GUEST_SYSENTER_ESP, data);
721 case MSR_IA32_TIME_STAMP_COUNTER:
722 guest_write_tsc(data);
725 msr = find_msr_entry(vcpu, msr_index);
728 if (vmx->host_state.loaded)
729 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
732 ret = kvm_set_msr_common(vcpu, msr_index, data);
739 * Sync the rsp and rip registers into the vcpu structure. This allows
740 * registers to be accessed by indexing vcpu->regs.
742 static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
744 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
745 vcpu->rip = vmcs_readl(GUEST_RIP);
749 * Syncs rsp and rip back into the vmcs. Should be called after possible
752 static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
754 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
755 vmcs_writel(GUEST_RIP, vcpu->rip);
758 static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
760 unsigned long dr7 = 0x400;
763 old_singlestep = vcpu->guest_debug.singlestep;
765 vcpu->guest_debug.enabled = dbg->enabled;
766 if (vcpu->guest_debug.enabled) {
769 dr7 |= 0x200; /* exact */
770 for (i = 0; i < 4; ++i) {
771 if (!dbg->breakpoints[i].enabled)
773 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
774 dr7 |= 2 << (i*2); /* global enable */
775 dr7 |= 0 << (i*4+16); /* execution breakpoint */
778 vcpu->guest_debug.singlestep = dbg->singlestep;
780 vcpu->guest_debug.singlestep = 0;
782 if (old_singlestep && !vcpu->guest_debug.singlestep) {
785 flags = vmcs_readl(GUEST_RFLAGS);
786 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
787 vmcs_writel(GUEST_RFLAGS, flags);
790 update_exception_bitmap(vcpu);
791 vmcs_writel(GUEST_DR7, dr7);
796 static __init int cpu_has_kvm_support(void)
798 unsigned long ecx = cpuid_ecx(1);
799 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
802 static __init int vmx_disabled_by_bios(void)
806 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
807 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
808 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
809 == MSR_IA32_FEATURE_CONTROL_LOCKED;
810 /* locked but not enabled */
813 static void hardware_enable(void *garbage)
815 int cpu = raw_smp_processor_id();
816 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
819 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
820 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
821 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
822 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
823 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
824 /* enable and lock */
825 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
826 MSR_IA32_FEATURE_CONTROL_LOCKED |
827 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
828 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
829 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
833 static void hardware_disable(void *garbage)
835 asm volatile (ASM_VMX_VMXOFF : : : "cc");
838 static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
839 u32 msr, u32* result)
841 u32 vmx_msr_low, vmx_msr_high;
842 u32 ctl = ctl_min | ctl_opt;
844 rdmsr(msr, vmx_msr_low, vmx_msr_high);
846 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
847 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
849 /* Ensure minimum (required) set of control bits are supported. */
857 static __init int setup_vmcs_config(void)
859 u32 vmx_msr_low, vmx_msr_high;
861 u32 _pin_based_exec_control = 0;
862 u32 _cpu_based_exec_control = 0;
863 u32 _vmexit_control = 0;
864 u32 _vmentry_control = 0;
866 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
868 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
869 &_pin_based_exec_control) < 0)
872 min = CPU_BASED_HLT_EXITING |
874 CPU_BASED_CR8_LOAD_EXITING |
875 CPU_BASED_CR8_STORE_EXITING |
877 CPU_BASED_USE_IO_BITMAPS |
878 CPU_BASED_MOV_DR_EXITING |
879 CPU_BASED_USE_TSC_OFFSETING;
881 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
882 &_cpu_based_exec_control) < 0)
887 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
890 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
891 &_vmexit_control) < 0)
895 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
896 &_vmentry_control) < 0)
899 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
901 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
902 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
906 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
907 if (vmx_msr_high & (1u<<16))
911 /* Require Write-Back (WB) memory type for VMCS accesses. */
912 if (((vmx_msr_high >> 18) & 15) != 6)
915 vmcs_config.size = vmx_msr_high & 0x1fff;
916 vmcs_config.order = get_order(vmcs_config.size);
917 vmcs_config.revision_id = vmx_msr_low;
919 vmcs_config.pin_based_exec_ctrl = _pin_based_exec_control;
920 vmcs_config.cpu_based_exec_ctrl = _cpu_based_exec_control;
921 vmcs_config.vmexit_ctrl = _vmexit_control;
922 vmcs_config.vmentry_ctrl = _vmentry_control;
927 static struct vmcs *alloc_vmcs_cpu(int cpu)
929 int node = cpu_to_node(cpu);
933 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
936 vmcs = page_address(pages);
937 memset(vmcs, 0, vmcs_config.size);
938 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
942 static struct vmcs *alloc_vmcs(void)
944 return alloc_vmcs_cpu(raw_smp_processor_id());
947 static void free_vmcs(struct vmcs *vmcs)
949 free_pages((unsigned long)vmcs, vmcs_config.order);
952 static void free_kvm_area(void)
956 for_each_online_cpu(cpu)
957 free_vmcs(per_cpu(vmxarea, cpu));
960 extern struct vmcs *alloc_vmcs_cpu(int cpu);
962 static __init int alloc_kvm_area(void)
966 for_each_online_cpu(cpu) {
969 vmcs = alloc_vmcs_cpu(cpu);
975 per_cpu(vmxarea, cpu) = vmcs;
980 static __init int hardware_setup(void)
982 if (setup_vmcs_config() < 0)
984 return alloc_kvm_area();
987 static __exit void hardware_unsetup(void)
992 static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
994 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
996 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
997 vmcs_write16(sf->selector, save->selector);
998 vmcs_writel(sf->base, save->base);
999 vmcs_write32(sf->limit, save->limit);
1000 vmcs_write32(sf->ar_bytes, save->ar);
1002 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1004 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1008 static void enter_pmode(struct kvm_vcpu *vcpu)
1010 unsigned long flags;
1012 vcpu->rmode.active = 0;
1014 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
1015 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
1016 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
1018 flags = vmcs_readl(GUEST_RFLAGS);
1019 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
1020 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
1021 vmcs_writel(GUEST_RFLAGS, flags);
1023 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1024 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
1026 update_exception_bitmap(vcpu);
1028 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
1029 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
1030 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
1031 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
1033 vmcs_write16(GUEST_SS_SELECTOR, 0);
1034 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1036 vmcs_write16(GUEST_CS_SELECTOR,
1037 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1038 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1041 static int rmode_tss_base(struct kvm* kvm)
1043 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
1044 return base_gfn << PAGE_SHIFT;
1047 static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1049 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1051 save->selector = vmcs_read16(sf->selector);
1052 save->base = vmcs_readl(sf->base);
1053 save->limit = vmcs_read32(sf->limit);
1054 save->ar = vmcs_read32(sf->ar_bytes);
1055 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
1056 vmcs_write32(sf->limit, 0xffff);
1057 vmcs_write32(sf->ar_bytes, 0xf3);
1060 static void enter_rmode(struct kvm_vcpu *vcpu)
1062 unsigned long flags;
1064 vcpu->rmode.active = 1;
1066 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
1067 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1069 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
1070 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1072 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
1073 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1075 flags = vmcs_readl(GUEST_RFLAGS);
1076 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
1078 flags |= IOPL_MASK | X86_EFLAGS_VM;
1080 vmcs_writel(GUEST_RFLAGS, flags);
1081 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
1082 update_exception_bitmap(vcpu);
1084 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1085 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1086 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1088 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
1089 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1090 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1091 vmcs_writel(GUEST_CS_BASE, 0xf0000);
1092 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1094 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
1095 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
1096 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
1097 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
1099 init_rmode_tss(vcpu->kvm);
1102 #ifdef CONFIG_X86_64
1104 static void enter_lmode(struct kvm_vcpu *vcpu)
1108 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1109 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1110 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
1112 vmcs_write32(GUEST_TR_AR_BYTES,
1113 (guest_tr_ar & ~AR_TYPE_MASK)
1114 | AR_TYPE_BUSY_64_TSS);
1117 vcpu->shadow_efer |= EFER_LMA;
1119 find_msr_entry(vcpu, MSR_EFER)->data |= EFER_LMA | EFER_LME;
1120 vmcs_write32(VM_ENTRY_CONTROLS,
1121 vmcs_read32(VM_ENTRY_CONTROLS)
1122 | VM_ENTRY_CONTROLS_IA32E_MASK);
1125 static void exit_lmode(struct kvm_vcpu *vcpu)
1127 vcpu->shadow_efer &= ~EFER_LMA;
1129 vmcs_write32(VM_ENTRY_CONTROLS,
1130 vmcs_read32(VM_ENTRY_CONTROLS)
1131 & ~VM_ENTRY_CONTROLS_IA32E_MASK);
1136 static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
1138 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
1139 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
1142 static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1144 vmx_fpu_deactivate(vcpu);
1146 if (vcpu->rmode.active && (cr0 & X86_CR0_PE))
1149 if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE))
1152 #ifdef CONFIG_X86_64
1153 if (vcpu->shadow_efer & EFER_LME) {
1154 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
1156 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
1161 vmcs_writel(CR0_READ_SHADOW, cr0);
1162 vmcs_writel(GUEST_CR0,
1163 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
1166 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
1167 vmx_fpu_activate(vcpu);
1170 static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1172 vmcs_writel(GUEST_CR3, cr3);
1173 if (vcpu->cr0 & X86_CR0_PE)
1174 vmx_fpu_deactivate(vcpu);
1177 static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1179 vmcs_writel(CR4_READ_SHADOW, cr4);
1180 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
1181 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
1185 #ifdef CONFIG_X86_64
1187 static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1189 struct kvm_msr_entry *msr = find_msr_entry(vcpu, MSR_EFER);
1191 vcpu->shadow_efer = efer;
1192 if (efer & EFER_LMA) {
1193 vmcs_write32(VM_ENTRY_CONTROLS,
1194 vmcs_read32(VM_ENTRY_CONTROLS) |
1195 VM_ENTRY_CONTROLS_IA32E_MASK);
1199 vmcs_write32(VM_ENTRY_CONTROLS,
1200 vmcs_read32(VM_ENTRY_CONTROLS) &
1201 ~VM_ENTRY_CONTROLS_IA32E_MASK);
1203 msr->data = efer & ~EFER_LME;
1210 static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1212 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1214 return vmcs_readl(sf->base);
1217 static void vmx_get_segment(struct kvm_vcpu *vcpu,
1218 struct kvm_segment *var, int seg)
1220 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1223 var->base = vmcs_readl(sf->base);
1224 var->limit = vmcs_read32(sf->limit);
1225 var->selector = vmcs_read16(sf->selector);
1226 ar = vmcs_read32(sf->ar_bytes);
1227 if (ar & AR_UNUSABLE_MASK)
1229 var->type = ar & 15;
1230 var->s = (ar >> 4) & 1;
1231 var->dpl = (ar >> 5) & 3;
1232 var->present = (ar >> 7) & 1;
1233 var->avl = (ar >> 12) & 1;
1234 var->l = (ar >> 13) & 1;
1235 var->db = (ar >> 14) & 1;
1236 var->g = (ar >> 15) & 1;
1237 var->unusable = (ar >> 16) & 1;
1240 static u32 vmx_segment_access_rights(struct kvm_segment *var)
1247 ar = var->type & 15;
1248 ar |= (var->s & 1) << 4;
1249 ar |= (var->dpl & 3) << 5;
1250 ar |= (var->present & 1) << 7;
1251 ar |= (var->avl & 1) << 12;
1252 ar |= (var->l & 1) << 13;
1253 ar |= (var->db & 1) << 14;
1254 ar |= (var->g & 1) << 15;
1256 if (ar == 0) /* a 0 value means unusable */
1257 ar = AR_UNUSABLE_MASK;
1262 static void vmx_set_segment(struct kvm_vcpu *vcpu,
1263 struct kvm_segment *var, int seg)
1265 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1268 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1269 vcpu->rmode.tr.selector = var->selector;
1270 vcpu->rmode.tr.base = var->base;
1271 vcpu->rmode.tr.limit = var->limit;
1272 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1275 vmcs_writel(sf->base, var->base);
1276 vmcs_write32(sf->limit, var->limit);
1277 vmcs_write16(sf->selector, var->selector);
1278 if (vcpu->rmode.active && var->s) {
1280 * Hack real-mode segments into vm86 compatibility.
1282 if (var->base == 0xffff0000 && var->selector == 0xf000)
1283 vmcs_writel(sf->base, 0xf0000);
1286 ar = vmx_segment_access_rights(var);
1287 vmcs_write32(sf->ar_bytes, ar);
1290 static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1292 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1294 *db = (ar >> 14) & 1;
1295 *l = (ar >> 13) & 1;
1298 static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1300 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1301 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1304 static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1306 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1307 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1310 static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1312 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1313 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1316 static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1318 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1319 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1322 static int init_rmode_tss(struct kvm* kvm)
1324 struct page *p1, *p2, *p3;
1325 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1328 p1 = gfn_to_page(kvm, fn++);
1329 p2 = gfn_to_page(kvm, fn++);
1330 p3 = gfn_to_page(kvm, fn);
1332 if (!p1 || !p2 || !p3) {
1333 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1337 page = kmap_atomic(p1, KM_USER0);
1339 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1340 kunmap_atomic(page, KM_USER0);
1342 page = kmap_atomic(p2, KM_USER0);
1344 kunmap_atomic(page, KM_USER0);
1346 page = kmap_atomic(p3, KM_USER0);
1348 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1349 kunmap_atomic(page, KM_USER0);
1354 static void seg_setup(int seg)
1356 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1358 vmcs_write16(sf->selector, 0);
1359 vmcs_writel(sf->base, 0);
1360 vmcs_write32(sf->limit, 0xffff);
1361 vmcs_write32(sf->ar_bytes, 0x93);
1365 * Sets up the vmcs for emulated real mode.
1367 static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)
1369 struct vcpu_vmx *vmx = to_vmx(vcpu);
1370 u32 host_sysenter_cs;
1373 struct descriptor_table dt;
1376 unsigned long kvm_vmx_return;
1378 if (!init_rmode_tss(vcpu->kvm)) {
1383 memset(vcpu->regs, 0, sizeof(vcpu->regs));
1384 vcpu->regs[VCPU_REGS_RDX] = get_rdx_init_val();
1386 vcpu->apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1387 if (vcpu->vcpu_id == 0)
1388 vcpu->apic_base |= MSR_IA32_APICBASE_BSP;
1393 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1394 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1396 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1397 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1398 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1399 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1401 seg_setup(VCPU_SREG_DS);
1402 seg_setup(VCPU_SREG_ES);
1403 seg_setup(VCPU_SREG_FS);
1404 seg_setup(VCPU_SREG_GS);
1405 seg_setup(VCPU_SREG_SS);
1407 vmcs_write16(GUEST_TR_SELECTOR, 0);
1408 vmcs_writel(GUEST_TR_BASE, 0);
1409 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1410 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1412 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1413 vmcs_writel(GUEST_LDTR_BASE, 0);
1414 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1415 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1417 vmcs_write32(GUEST_SYSENTER_CS, 0);
1418 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1419 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1421 vmcs_writel(GUEST_RFLAGS, 0x02);
1422 vmcs_writel(GUEST_RIP, 0xfff0);
1423 vmcs_writel(GUEST_RSP, 0);
1425 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1426 vmcs_writel(GUEST_DR7, 0x400);
1428 vmcs_writel(GUEST_GDTR_BASE, 0);
1429 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1431 vmcs_writel(GUEST_IDTR_BASE, 0);
1432 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1434 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1435 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1436 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1439 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1440 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
1444 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1446 /* Special registers */
1447 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1450 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1451 vmcs_config.pin_based_exec_ctrl);
1452 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
1453 vmcs_config.cpu_based_exec_ctrl);
1455 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1456 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1457 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1459 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1460 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1461 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1463 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1464 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1465 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1466 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1467 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1468 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1469 #ifdef CONFIG_X86_64
1470 rdmsrl(MSR_FS_BASE, a);
1471 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1472 rdmsrl(MSR_GS_BASE, a);
1473 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1475 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1476 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1479 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1482 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1484 asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
1485 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
1486 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1487 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1488 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
1490 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1491 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1492 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1493 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1494 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1495 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1497 for (i = 0; i < NR_VMX_MSR; ++i) {
1498 u32 index = vmx_msr_index[i];
1499 u32 data_low, data_high;
1503 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1505 if (wrmsr_safe(index, data_low, data_high) < 0)
1507 data = data_low | ((u64)data_high << 32);
1508 vmx->host_msrs[j].index = index;
1509 vmx->host_msrs[j].reserved = 0;
1510 vmx->host_msrs[j].data = data;
1511 vmx->guest_msrs[j] = vmx->host_msrs[j];
1517 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
1519 /* 22.2.1, 20.8.1 */
1520 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1522 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1524 #ifdef CONFIG_X86_64
1525 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0);
1526 vmcs_writel(TPR_THRESHOLD, 0);
1529 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1530 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1532 vcpu->cr0 = 0x60000010;
1533 vmx_set_cr0(vcpu, vcpu->cr0); // enter rmode
1534 vmx_set_cr4(vcpu, 0);
1535 #ifdef CONFIG_X86_64
1536 vmx_set_efer(vcpu, 0);
1538 vmx_fpu_activate(vcpu);
1539 update_exception_bitmap(vcpu);
1547 static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1552 unsigned long flags;
1553 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1554 u16 sp = vmcs_readl(GUEST_RSP);
1555 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1557 if (sp > ss_limit || sp < 6 ) {
1558 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1560 vmcs_readl(GUEST_RSP),
1561 vmcs_readl(GUEST_SS_BASE),
1562 vmcs_read32(GUEST_SS_LIMIT));
1566 if (kvm_read_guest(vcpu, irq * sizeof(ent), sizeof(ent), &ent) !=
1568 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1572 flags = vmcs_readl(GUEST_RFLAGS);
1573 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1574 ip = vmcs_readl(GUEST_RIP);
1577 if (kvm_write_guest(vcpu, ss_base + sp - 2, 2, &flags) != 2 ||
1578 kvm_write_guest(vcpu, ss_base + sp - 4, 2, &cs) != 2 ||
1579 kvm_write_guest(vcpu, ss_base + sp - 6, 2, &ip) != 2) {
1580 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1584 vmcs_writel(GUEST_RFLAGS, flags &
1585 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1586 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1587 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1588 vmcs_writel(GUEST_RIP, ent[0]);
1589 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1592 static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1594 int word_index = __ffs(vcpu->irq_summary);
1595 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1596 int irq = word_index * BITS_PER_LONG + bit_index;
1598 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1599 if (!vcpu->irq_pending[word_index])
1600 clear_bit(word_index, &vcpu->irq_summary);
1602 if (vcpu->rmode.active) {
1603 inject_rmode_irq(vcpu, irq);
1606 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1607 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1611 static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1612 struct kvm_run *kvm_run)
1614 u32 cpu_based_vm_exec_control;
1616 vcpu->interrupt_window_open =
1617 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1618 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1620 if (vcpu->interrupt_window_open &&
1621 vcpu->irq_summary &&
1622 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
1624 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1626 kvm_do_inject_irq(vcpu);
1628 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1629 if (!vcpu->interrupt_window_open &&
1630 (vcpu->irq_summary || kvm_run->request_interrupt_window))
1632 * Interrupts blocked. Wait for unblock.
1634 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1636 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1637 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
1640 static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1642 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1644 set_debugreg(dbg->bp[0], 0);
1645 set_debugreg(dbg->bp[1], 1);
1646 set_debugreg(dbg->bp[2], 2);
1647 set_debugreg(dbg->bp[3], 3);
1649 if (dbg->singlestep) {
1650 unsigned long flags;
1652 flags = vmcs_readl(GUEST_RFLAGS);
1653 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1654 vmcs_writel(GUEST_RFLAGS, flags);
1658 static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1659 int vec, u32 err_code)
1661 if (!vcpu->rmode.active)
1665 * Instruction with address size override prefix opcode 0x67
1666 * Cause the #SS fault with 0 error code in VM86 mode.
1668 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
1669 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1674 static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1676 u32 intr_info, error_code;
1677 unsigned long cr2, rip;
1679 enum emulation_result er;
1682 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1683 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1685 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1686 !is_page_fault(intr_info)) {
1687 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1688 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1691 if (is_external_interrupt(vect_info)) {
1692 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1693 set_bit(irq, vcpu->irq_pending);
1694 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1697 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
1702 if (is_no_device(intr_info)) {
1703 vmx_fpu_activate(vcpu);
1708 rip = vmcs_readl(GUEST_RIP);
1709 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1710 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1711 if (is_page_fault(intr_info)) {
1712 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1714 spin_lock(&vcpu->kvm->lock);
1715 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1717 spin_unlock(&vcpu->kvm->lock);
1721 spin_unlock(&vcpu->kvm->lock);
1725 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
1726 spin_unlock(&vcpu->kvm->lock);
1731 case EMULATE_DO_MMIO:
1732 ++vcpu->stat.mmio_exits;
1735 vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
1742 if (vcpu->rmode.active &&
1743 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
1745 if (vcpu->halt_request) {
1746 vcpu->halt_request = 0;
1747 return kvm_emulate_halt(vcpu);
1752 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1753 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1756 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1757 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1758 kvm_run->ex.error_code = error_code;
1762 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1763 struct kvm_run *kvm_run)
1765 ++vcpu->stat.irq_exits;
1769 static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1771 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1775 static int get_io_count(struct kvm_vcpu *vcpu, unsigned long *count)
1782 if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_VM)) {
1785 u32 cs_ar = vmcs_read32(GUEST_CS_AR_BYTES);
1787 countr_size = (cs_ar & AR_L_MASK) ? 8:
1788 (cs_ar & AR_DB_MASK) ? 4: 2;
1791 rip = vmcs_readl(GUEST_RIP);
1792 if (countr_size != 8)
1793 rip += vmcs_readl(GUEST_CS_BASE);
1795 n = kvm_read_guest(vcpu, rip, sizeof(inst), &inst);
1797 for (i = 0; i < n; i++) {
1798 switch (((u8*)&inst)[i]) {
1811 countr_size = (countr_size == 2) ? 4: (countr_size >> 1);
1819 *count = vcpu->regs[VCPU_REGS_RCX] & (~0ULL >> (64 - countr_size));
1820 //printk("cx: %lx\n", vcpu->regs[VCPU_REGS_RCX]);
1824 static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1826 u64 exit_qualification;
1827 int size, down, in, string, rep;
1829 unsigned long count;
1832 ++vcpu->stat.io_exits;
1833 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1834 in = (exit_qualification & 8) != 0;
1835 size = (exit_qualification & 7) + 1;
1836 string = (exit_qualification & 16) != 0;
1837 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
1839 rep = (exit_qualification & 32) != 0;
1840 port = exit_qualification >> 16;
1843 if (rep && !get_io_count(vcpu, &count))
1845 address = vmcs_readl(GUEST_LINEAR_ADDRESS);
1847 return kvm_setup_pio(vcpu, kvm_run, in, size, count, string, down,
1848 address, rep, port);
1852 vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1855 * Patch in the VMCALL instruction:
1857 hypercall[0] = 0x0f;
1858 hypercall[1] = 0x01;
1859 hypercall[2] = 0xc1;
1860 hypercall[3] = 0xc3;
1863 static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1865 u64 exit_qualification;
1869 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1870 cr = exit_qualification & 15;
1871 reg = (exit_qualification >> 8) & 15;
1872 switch ((exit_qualification >> 4) & 3) {
1873 case 0: /* mov to cr */
1876 vcpu_load_rsp_rip(vcpu);
1877 set_cr0(vcpu, vcpu->regs[reg]);
1878 skip_emulated_instruction(vcpu);
1881 vcpu_load_rsp_rip(vcpu);
1882 set_cr3(vcpu, vcpu->regs[reg]);
1883 skip_emulated_instruction(vcpu);
1886 vcpu_load_rsp_rip(vcpu);
1887 set_cr4(vcpu, vcpu->regs[reg]);
1888 skip_emulated_instruction(vcpu);
1891 vcpu_load_rsp_rip(vcpu);
1892 set_cr8(vcpu, vcpu->regs[reg]);
1893 skip_emulated_instruction(vcpu);
1898 vcpu_load_rsp_rip(vcpu);
1899 vmx_fpu_deactivate(vcpu);
1900 vcpu->cr0 &= ~X86_CR0_TS;
1901 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
1902 vmx_fpu_activate(vcpu);
1903 skip_emulated_instruction(vcpu);
1905 case 1: /*mov from cr*/
1908 vcpu_load_rsp_rip(vcpu);
1909 vcpu->regs[reg] = vcpu->cr3;
1910 vcpu_put_rsp_rip(vcpu);
1911 skip_emulated_instruction(vcpu);
1914 vcpu_load_rsp_rip(vcpu);
1915 vcpu->regs[reg] = vcpu->cr8;
1916 vcpu_put_rsp_rip(vcpu);
1917 skip_emulated_instruction(vcpu);
1922 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1924 skip_emulated_instruction(vcpu);
1929 kvm_run->exit_reason = 0;
1930 printk(KERN_ERR "kvm: unhandled control register: op %d cr %d\n",
1931 (int)(exit_qualification >> 4) & 3, cr);
1935 static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1937 u64 exit_qualification;
1942 * FIXME: this code assumes the host is debugging the guest.
1943 * need to deal with guest debugging itself too.
1945 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1946 dr = exit_qualification & 7;
1947 reg = (exit_qualification >> 8) & 15;
1948 vcpu_load_rsp_rip(vcpu);
1949 if (exit_qualification & 16) {
1961 vcpu->regs[reg] = val;
1965 vcpu_put_rsp_rip(vcpu);
1966 skip_emulated_instruction(vcpu);
1970 static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1972 kvm_emulate_cpuid(vcpu);
1976 static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1978 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1981 if (vmx_get_msr(vcpu, ecx, &data)) {
1982 vmx_inject_gp(vcpu, 0);
1986 /* FIXME: handling of bits 32:63 of rax, rdx */
1987 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
1988 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
1989 skip_emulated_instruction(vcpu);
1993 static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1995 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1996 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
1997 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
1999 if (vmx_set_msr(vcpu, ecx, data) != 0) {
2000 vmx_inject_gp(vcpu, 0);
2004 skip_emulated_instruction(vcpu);
2008 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2009 struct kvm_run *kvm_run)
2011 kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
2012 kvm_run->cr8 = vcpu->cr8;
2013 kvm_run->apic_base = vcpu->apic_base;
2014 kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
2015 vcpu->irq_summary == 0);
2018 static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2019 struct kvm_run *kvm_run)
2022 * If the user space waits to inject interrupts, exit as soon as
2025 if (kvm_run->request_interrupt_window &&
2026 !vcpu->irq_summary) {
2027 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2028 ++vcpu->stat.irq_window_exits;
2034 static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2036 skip_emulated_instruction(vcpu);
2037 return kvm_emulate_halt(vcpu);
2040 static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2042 skip_emulated_instruction(vcpu);
2043 return kvm_hypercall(vcpu, kvm_run);
2047 * The exit handlers return 1 if the exit was handled fully and guest execution
2048 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2049 * to be done to userspace and return 0.
2051 static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2052 struct kvm_run *kvm_run) = {
2053 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2054 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
2055 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
2056 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
2057 [EXIT_REASON_CR_ACCESS] = handle_cr,
2058 [EXIT_REASON_DR_ACCESS] = handle_dr,
2059 [EXIT_REASON_CPUID] = handle_cpuid,
2060 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2061 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2062 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2063 [EXIT_REASON_HLT] = handle_halt,
2064 [EXIT_REASON_VMCALL] = handle_vmcall,
2067 static const int kvm_vmx_max_exit_handlers =
2068 ARRAY_SIZE(kvm_vmx_exit_handlers);
2071 * The guest has exited. See if we can fix it or if we need userspace
2074 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2076 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2077 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
2079 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
2080 exit_reason != EXIT_REASON_EXCEPTION_NMI )
2081 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
2082 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
2083 if (exit_reason < kvm_vmx_max_exit_handlers
2084 && kvm_vmx_exit_handlers[exit_reason])
2085 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2087 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2088 kvm_run->hw.hardware_exit_reason = exit_reason;
2094 * Check if userspace requested an interrupt window, and that the
2095 * interrupt window is open.
2097 * No need to exit to userspace if we already have an interrupt queued.
2099 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2100 struct kvm_run *kvm_run)
2102 return (!vcpu->irq_summary &&
2103 kvm_run->request_interrupt_window &&
2104 vcpu->interrupt_window_open &&
2105 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
2108 static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2112 static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2114 struct vcpu_vmx *vmx = to_vmx(vcpu);
2119 if (vcpu->guest_debug.enabled)
2120 kvm_guest_debug_pre(vcpu);
2123 r = kvm_mmu_reload(vcpu);
2129 if (!vcpu->mmio_read_completed)
2130 do_interrupt_requests(vcpu, kvm_run);
2132 vmx_save_host_state(vcpu);
2133 kvm_load_guest_fpu(vcpu);
2136 * Loading guest fpu may have cleared host cr0.ts
2138 vmcs_writel(HOST_CR0, read_cr0());
2140 local_irq_disable();
2142 vcpu->guest_mode = 1;
2144 if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
2145 vmx_flush_tlb(vcpu);
2148 /* Store host registers */
2149 #ifdef CONFIG_X86_64
2150 "push %%rax; push %%rbx; push %%rdx;"
2151 "push %%rsi; push %%rdi; push %%rbp;"
2152 "push %%r8; push %%r9; push %%r10; push %%r11;"
2153 "push %%r12; push %%r13; push %%r14; push %%r15;"
2155 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2157 "pusha; push %%ecx \n\t"
2158 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
2160 /* Check if vmlaunch of vmresume is needed */
2162 /* Load guest registers. Don't clobber flags. */
2163 #ifdef CONFIG_X86_64
2164 "mov %c[cr2](%3), %%rax \n\t"
2165 "mov %%rax, %%cr2 \n\t"
2166 "mov %c[rax](%3), %%rax \n\t"
2167 "mov %c[rbx](%3), %%rbx \n\t"
2168 "mov %c[rdx](%3), %%rdx \n\t"
2169 "mov %c[rsi](%3), %%rsi \n\t"
2170 "mov %c[rdi](%3), %%rdi \n\t"
2171 "mov %c[rbp](%3), %%rbp \n\t"
2172 "mov %c[r8](%3), %%r8 \n\t"
2173 "mov %c[r9](%3), %%r9 \n\t"
2174 "mov %c[r10](%3), %%r10 \n\t"
2175 "mov %c[r11](%3), %%r11 \n\t"
2176 "mov %c[r12](%3), %%r12 \n\t"
2177 "mov %c[r13](%3), %%r13 \n\t"
2178 "mov %c[r14](%3), %%r14 \n\t"
2179 "mov %c[r15](%3), %%r15 \n\t"
2180 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
2182 "mov %c[cr2](%3), %%eax \n\t"
2183 "mov %%eax, %%cr2 \n\t"
2184 "mov %c[rax](%3), %%eax \n\t"
2185 "mov %c[rbx](%3), %%ebx \n\t"
2186 "mov %c[rdx](%3), %%edx \n\t"
2187 "mov %c[rsi](%3), %%esi \n\t"
2188 "mov %c[rdi](%3), %%edi \n\t"
2189 "mov %c[rbp](%3), %%ebp \n\t"
2190 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
2192 /* Enter guest mode */
2193 "jne .Llaunched \n\t"
2194 ASM_VMX_VMLAUNCH "\n\t"
2195 "jmp .Lkvm_vmx_return \n\t"
2196 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2197 ".Lkvm_vmx_return: "
2198 /* Save guest registers, load host registers, keep flags */
2199 #ifdef CONFIG_X86_64
2200 "xchg %3, (%%rsp) \n\t"
2201 "mov %%rax, %c[rax](%3) \n\t"
2202 "mov %%rbx, %c[rbx](%3) \n\t"
2203 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
2204 "mov %%rdx, %c[rdx](%3) \n\t"
2205 "mov %%rsi, %c[rsi](%3) \n\t"
2206 "mov %%rdi, %c[rdi](%3) \n\t"
2207 "mov %%rbp, %c[rbp](%3) \n\t"
2208 "mov %%r8, %c[r8](%3) \n\t"
2209 "mov %%r9, %c[r9](%3) \n\t"
2210 "mov %%r10, %c[r10](%3) \n\t"
2211 "mov %%r11, %c[r11](%3) \n\t"
2212 "mov %%r12, %c[r12](%3) \n\t"
2213 "mov %%r13, %c[r13](%3) \n\t"
2214 "mov %%r14, %c[r14](%3) \n\t"
2215 "mov %%r15, %c[r15](%3) \n\t"
2216 "mov %%cr2, %%rax \n\t"
2217 "mov %%rax, %c[cr2](%3) \n\t"
2218 "mov (%%rsp), %3 \n\t"
2220 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2221 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2222 "pop %%rbp; pop %%rdi; pop %%rsi;"
2223 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2225 "xchg %3, (%%esp) \n\t"
2226 "mov %%eax, %c[rax](%3) \n\t"
2227 "mov %%ebx, %c[rbx](%3) \n\t"
2228 "pushl (%%esp); popl %c[rcx](%3) \n\t"
2229 "mov %%edx, %c[rdx](%3) \n\t"
2230 "mov %%esi, %c[rsi](%3) \n\t"
2231 "mov %%edi, %c[rdi](%3) \n\t"
2232 "mov %%ebp, %c[rbp](%3) \n\t"
2233 "mov %%cr2, %%eax \n\t"
2234 "mov %%eax, %c[cr2](%3) \n\t"
2235 "mov (%%esp), %3 \n\t"
2237 "pop %%ecx; popa \n\t"
2241 : "r"(vmx->launched), "d"((unsigned long)HOST_RSP),
2243 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2244 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2245 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2246 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2247 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2248 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2249 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
2250 #ifdef CONFIG_X86_64
2251 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2252 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2253 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2254 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2255 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2256 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2257 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2258 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2260 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2263 vcpu->guest_mode = 0;
2268 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
2270 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
2275 if (unlikely(fail)) {
2276 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2277 kvm_run->fail_entry.hardware_entry_failure_reason
2278 = vmcs_read32(VM_INSTRUCTION_ERROR);
2283 * Profile KVM exit RIPs:
2285 if (unlikely(prof_on == KVM_PROFILING))
2286 profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP));
2288 r = kvm_handle_exit(kvm_run, vcpu);
2290 /* Give scheduler a change to reschedule. */
2291 if (signal_pending(current)) {
2293 kvm_run->exit_reason = KVM_EXIT_INTR;
2294 ++vcpu->stat.signal_exits;
2298 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2300 kvm_run->exit_reason = KVM_EXIT_INTR;
2301 ++vcpu->stat.request_irq_exits;
2304 if (!need_resched()) {
2305 ++vcpu->stat.light_exits;
2316 post_kvm_run_save(vcpu, kvm_run);
2320 static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2324 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2326 ++vcpu->stat.pf_guest;
2328 if (is_page_fault(vect_info)) {
2329 printk(KERN_DEBUG "inject_page_fault: "
2330 "double fault 0x%lx @ 0x%lx\n",
2331 addr, vmcs_readl(GUEST_RIP));
2332 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2333 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2335 INTR_TYPE_EXCEPTION |
2336 INTR_INFO_DELIEVER_CODE_MASK |
2337 INTR_INFO_VALID_MASK);
2341 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2342 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2344 INTR_TYPE_EXCEPTION |
2345 INTR_INFO_DELIEVER_CODE_MASK |
2346 INTR_INFO_VALID_MASK);
2350 static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2352 struct vcpu_vmx *vmx = to_vmx(vcpu);
2355 on_each_cpu(__vcpu_clear, vcpu, 0, 1);
2356 free_vmcs(vmx->vmcs);
2361 static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2363 struct vcpu_vmx *vmx = to_vmx(vcpu);
2365 vmx_free_vmcs(vcpu);
2366 kfree(vmx->host_msrs);
2367 kfree(vmx->guest_msrs);
2368 kvm_vcpu_uninit(vcpu);
2372 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
2375 struct vcpu_vmx *vmx = kzalloc(sizeof(*vmx), GFP_KERNEL);
2379 return ERR_PTR(-ENOMEM);
2381 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2385 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2386 if (!vmx->guest_msrs) {
2391 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2392 if (!vmx->host_msrs)
2393 goto free_guest_msrs;
2395 vmx->vmcs = alloc_vmcs();
2399 vmcs_clear(vmx->vmcs);
2402 vmx_vcpu_load(&vmx->vcpu, cpu);
2403 err = vmx_vcpu_setup(&vmx->vcpu);
2404 vmx_vcpu_put(&vmx->vcpu);
2412 free_vmcs(vmx->vmcs);
2414 kfree(vmx->host_msrs);
2416 kfree(vmx->guest_msrs);
2418 kvm_vcpu_uninit(&vmx->vcpu);
2421 return ERR_PTR(err);
2424 static struct kvm_arch_ops vmx_arch_ops = {
2425 .cpu_has_kvm_support = cpu_has_kvm_support,
2426 .disabled_by_bios = vmx_disabled_by_bios,
2427 .hardware_setup = hardware_setup,
2428 .hardware_unsetup = hardware_unsetup,
2429 .hardware_enable = hardware_enable,
2430 .hardware_disable = hardware_disable,
2432 .vcpu_create = vmx_create_vcpu,
2433 .vcpu_free = vmx_free_vcpu,
2435 .vcpu_load = vmx_vcpu_load,
2436 .vcpu_put = vmx_vcpu_put,
2437 .vcpu_decache = vmx_vcpu_decache,
2439 .set_guest_debug = set_guest_debug,
2440 .get_msr = vmx_get_msr,
2441 .set_msr = vmx_set_msr,
2442 .get_segment_base = vmx_get_segment_base,
2443 .get_segment = vmx_get_segment,
2444 .set_segment = vmx_set_segment,
2445 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
2446 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
2447 .set_cr0 = vmx_set_cr0,
2448 .set_cr3 = vmx_set_cr3,
2449 .set_cr4 = vmx_set_cr4,
2450 #ifdef CONFIG_X86_64
2451 .set_efer = vmx_set_efer,
2453 .get_idt = vmx_get_idt,
2454 .set_idt = vmx_set_idt,
2455 .get_gdt = vmx_get_gdt,
2456 .set_gdt = vmx_set_gdt,
2457 .cache_regs = vcpu_load_rsp_rip,
2458 .decache_regs = vcpu_put_rsp_rip,
2459 .get_rflags = vmx_get_rflags,
2460 .set_rflags = vmx_set_rflags,
2462 .tlb_flush = vmx_flush_tlb,
2463 .inject_page_fault = vmx_inject_page_fault,
2465 .inject_gp = vmx_inject_gp,
2467 .run = vmx_vcpu_run,
2468 .skip_emulated_instruction = skip_emulated_instruction,
2469 .patch_hypercall = vmx_patch_hypercall,
2472 static int __init vmx_init(void)
2477 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2478 if (!vmx_io_bitmap_a)
2481 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2482 if (!vmx_io_bitmap_b) {
2488 * Allow direct access to the PC debug port (it is often used for I/O
2489 * delays, but the vmexits simply slow things down).
2491 iova = kmap(vmx_io_bitmap_a);
2492 memset(iova, 0xff, PAGE_SIZE);
2493 clear_bit(0x80, iova);
2494 kunmap(vmx_io_bitmap_a);
2496 iova = kmap(vmx_io_bitmap_b);
2497 memset(iova, 0xff, PAGE_SIZE);
2498 kunmap(vmx_io_bitmap_b);
2500 r = kvm_init_arch(&vmx_arch_ops, THIS_MODULE);
2507 __free_page(vmx_io_bitmap_b);
2509 __free_page(vmx_io_bitmap_a);
2513 static void __exit vmx_exit(void)
2515 __free_page(vmx_io_bitmap_b);
2516 __free_page(vmx_io_bitmap_a);
2521 module_init(vmx_init)
2522 module_exit(vmx_exit)