2 * Kernel-based Virtual Machine driver for Linux
4 * derived from drivers/kvm/kvm_main.c
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
17 #include <linux/kvm_host.h>
18 #include "segment_descriptor.h"
22 #include <linux/kvm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/mman.h>
27 #include <linux/highmem.h>
29 #include <asm/uaccess.h>
32 #define MAX_IO_MSRS 256
33 #define CR0_RESERVED_BITS \
34 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
35 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
36 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
37 #define CR4_RESERVED_BITS \
38 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
39 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
40 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
41 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
43 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
44 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
46 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
49 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
50 struct kvm_cpuid_entry2 __user *entries);
52 struct kvm_x86_ops *kvm_x86_ops;
54 struct kvm_stats_debugfs_item debugfs_entries[] = {
55 { "pf_fixed", VCPU_STAT(pf_fixed) },
56 { "pf_guest", VCPU_STAT(pf_guest) },
57 { "tlb_flush", VCPU_STAT(tlb_flush) },
58 { "invlpg", VCPU_STAT(invlpg) },
59 { "exits", VCPU_STAT(exits) },
60 { "io_exits", VCPU_STAT(io_exits) },
61 { "mmio_exits", VCPU_STAT(mmio_exits) },
62 { "signal_exits", VCPU_STAT(signal_exits) },
63 { "irq_window", VCPU_STAT(irq_window_exits) },
64 { "halt_exits", VCPU_STAT(halt_exits) },
65 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
66 { "request_irq", VCPU_STAT(request_irq_exits) },
67 { "irq_exits", VCPU_STAT(irq_exits) },
68 { "host_state_reload", VCPU_STAT(host_state_reload) },
69 { "efer_reload", VCPU_STAT(efer_reload) },
70 { "fpu_reload", VCPU_STAT(fpu_reload) },
71 { "insn_emulation", VCPU_STAT(insn_emulation) },
72 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
73 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
74 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
75 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
76 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
77 { "mmu_flooded", VM_STAT(mmu_flooded) },
78 { "mmu_recycled", VM_STAT(mmu_recycled) },
79 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
80 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
85 unsigned long segment_base(u16 selector)
87 struct descriptor_table gdt;
88 struct segment_descriptor *d;
89 unsigned long table_base;
95 asm("sgdt %0" : "=m"(gdt));
96 table_base = gdt.base;
98 if (selector & 4) { /* from ldt */
101 asm("sldt %0" : "=g"(ldt_selector));
102 table_base = segment_base(ldt_selector);
104 d = (struct segment_descriptor *)(table_base + (selector & ~7));
105 v = d->base_low | ((unsigned long)d->base_mid << 16) |
106 ((unsigned long)d->base_high << 24);
108 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
109 v |= ((unsigned long) \
110 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
114 EXPORT_SYMBOL_GPL(segment_base);
116 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
118 if (irqchip_in_kernel(vcpu->kvm))
119 return vcpu->arch.apic_base;
121 return vcpu->arch.apic_base;
123 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
125 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
127 /* TODO: reserve bits check */
128 if (irqchip_in_kernel(vcpu->kvm))
129 kvm_lapic_set_base(vcpu, data);
131 vcpu->arch.apic_base = data;
133 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
135 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
137 WARN_ON(vcpu->arch.exception.pending);
138 vcpu->arch.exception.pending = true;
139 vcpu->arch.exception.has_error_code = false;
140 vcpu->arch.exception.nr = nr;
142 EXPORT_SYMBOL_GPL(kvm_queue_exception);
144 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
147 ++vcpu->stat.pf_guest;
148 if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
149 printk(KERN_DEBUG "kvm: inject_page_fault:"
150 " double fault 0x%lx\n", addr);
151 vcpu->arch.exception.nr = DF_VECTOR;
152 vcpu->arch.exception.error_code = 0;
155 vcpu->arch.cr2 = addr;
156 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
159 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
161 WARN_ON(vcpu->arch.exception.pending);
162 vcpu->arch.exception.pending = true;
163 vcpu->arch.exception.has_error_code = true;
164 vcpu->arch.exception.nr = nr;
165 vcpu->arch.exception.error_code = error_code;
167 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
169 static void __queue_exception(struct kvm_vcpu *vcpu)
171 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
172 vcpu->arch.exception.has_error_code,
173 vcpu->arch.exception.error_code);
177 * Load the pae pdptrs. Return true is they are all valid.
179 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
181 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
182 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
185 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
187 down_read(¤t->mm->mmap_sem);
188 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
189 offset * sizeof(u64), sizeof(pdpte));
194 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
195 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
202 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
204 up_read(¤t->mm->mmap_sem);
209 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
211 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
215 if (is_long_mode(vcpu) || !is_pae(vcpu))
218 down_read(¤t->mm->mmap_sem);
219 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
222 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
224 up_read(¤t->mm->mmap_sem);
229 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
231 if (cr0 & CR0_RESERVED_BITS) {
232 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
233 cr0, vcpu->arch.cr0);
234 kvm_inject_gp(vcpu, 0);
238 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
239 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
240 kvm_inject_gp(vcpu, 0);
244 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
245 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
246 "and a clear PE flag\n");
247 kvm_inject_gp(vcpu, 0);
251 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
253 if ((vcpu->arch.shadow_efer & EFER_LME)) {
257 printk(KERN_DEBUG "set_cr0: #GP, start paging "
258 "in long mode while PAE is disabled\n");
259 kvm_inject_gp(vcpu, 0);
262 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
264 printk(KERN_DEBUG "set_cr0: #GP, start paging "
265 "in long mode while CS.L == 1\n");
266 kvm_inject_gp(vcpu, 0);
272 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
273 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
275 kvm_inject_gp(vcpu, 0);
281 kvm_x86_ops->set_cr0(vcpu, cr0);
282 vcpu->arch.cr0 = cr0;
284 kvm_mmu_reset_context(vcpu);
287 EXPORT_SYMBOL_GPL(set_cr0);
289 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
291 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
293 EXPORT_SYMBOL_GPL(lmsw);
295 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
297 if (cr4 & CR4_RESERVED_BITS) {
298 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
299 kvm_inject_gp(vcpu, 0);
303 if (is_long_mode(vcpu)) {
304 if (!(cr4 & X86_CR4_PAE)) {
305 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
307 kvm_inject_gp(vcpu, 0);
310 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
311 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
312 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
313 kvm_inject_gp(vcpu, 0);
317 if (cr4 & X86_CR4_VMXE) {
318 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
319 kvm_inject_gp(vcpu, 0);
322 kvm_x86_ops->set_cr4(vcpu, cr4);
323 vcpu->arch.cr4 = cr4;
324 kvm_mmu_reset_context(vcpu);
326 EXPORT_SYMBOL_GPL(set_cr4);
328 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
330 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
331 kvm_mmu_flush_tlb(vcpu);
335 if (is_long_mode(vcpu)) {
336 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
337 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
338 kvm_inject_gp(vcpu, 0);
343 if (cr3 & CR3_PAE_RESERVED_BITS) {
345 "set_cr3: #GP, reserved bits\n");
346 kvm_inject_gp(vcpu, 0);
349 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
350 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
352 kvm_inject_gp(vcpu, 0);
357 * We don't check reserved bits in nonpae mode, because
358 * this isn't enforced, and VMware depends on this.
362 down_read(¤t->mm->mmap_sem);
364 * Does the new cr3 value map to physical memory? (Note, we
365 * catch an invalid cr3 even in real-mode, because it would
366 * cause trouble later on when we turn on paging anyway.)
368 * A real CPU would silently accept an invalid cr3 and would
369 * attempt to use it - with largely undefined (and often hard
370 * to debug) behavior on the guest side.
372 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
373 kvm_inject_gp(vcpu, 0);
375 vcpu->arch.cr3 = cr3;
376 vcpu->arch.mmu.new_cr3(vcpu);
378 up_read(¤t->mm->mmap_sem);
380 EXPORT_SYMBOL_GPL(set_cr3);
382 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
384 if (cr8 & CR8_RESERVED_BITS) {
385 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
386 kvm_inject_gp(vcpu, 0);
389 if (irqchip_in_kernel(vcpu->kvm))
390 kvm_lapic_set_tpr(vcpu, cr8);
392 vcpu->arch.cr8 = cr8;
394 EXPORT_SYMBOL_GPL(set_cr8);
396 unsigned long get_cr8(struct kvm_vcpu *vcpu)
398 if (irqchip_in_kernel(vcpu->kvm))
399 return kvm_lapic_get_cr8(vcpu);
401 return vcpu->arch.cr8;
403 EXPORT_SYMBOL_GPL(get_cr8);
406 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
407 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
409 * This list is modified at module load time to reflect the
410 * capabilities of the host cpu.
412 static u32 msrs_to_save[] = {
413 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
416 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
418 MSR_IA32_TIME_STAMP_COUNTER,
421 static unsigned num_msrs_to_save;
423 static u32 emulated_msrs[] = {
424 MSR_IA32_MISC_ENABLE,
429 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
431 if (efer & EFER_RESERVED_BITS) {
432 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
434 kvm_inject_gp(vcpu, 0);
439 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
440 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
441 kvm_inject_gp(vcpu, 0);
445 kvm_x86_ops->set_efer(vcpu, efer);
448 efer |= vcpu->arch.shadow_efer & EFER_LMA;
450 vcpu->arch.shadow_efer = efer;
456 * Writes msr value into into the appropriate "register".
457 * Returns 0 on success, non-0 otherwise.
458 * Assumes vcpu_load() was already called.
460 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
462 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
466 * Adapt set_msr() to msr_io()'s calling convention
468 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
470 return kvm_set_msr(vcpu, index, *data);
474 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
479 set_efer(vcpu, data);
482 case MSR_IA32_MC0_STATUS:
483 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
486 case MSR_IA32_MCG_STATUS:
487 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
490 case MSR_IA32_MCG_CTL:
491 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
494 case MSR_IA32_UCODE_REV:
495 case MSR_IA32_UCODE_WRITE:
496 case 0x200 ... 0x2ff: /* MTRRs */
498 case MSR_IA32_APICBASE:
499 kvm_set_apic_base(vcpu, data);
501 case MSR_IA32_MISC_ENABLE:
502 vcpu->arch.ia32_misc_enable_msr = data;
505 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
510 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
514 * Reads an msr value (of 'msr_index') into 'pdata'.
515 * Returns 0 on success, non-0 otherwise.
516 * Assumes vcpu_load() was already called.
518 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
520 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
523 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
528 case 0xc0010010: /* SYSCFG */
529 case 0xc0010015: /* HWCR */
530 case MSR_IA32_PLATFORM_ID:
531 case MSR_IA32_P5_MC_ADDR:
532 case MSR_IA32_P5_MC_TYPE:
533 case MSR_IA32_MC0_CTL:
534 case MSR_IA32_MCG_STATUS:
535 case MSR_IA32_MCG_CAP:
536 case MSR_IA32_MCG_CTL:
537 case MSR_IA32_MC0_MISC:
538 case MSR_IA32_MC0_MISC+4:
539 case MSR_IA32_MC0_MISC+8:
540 case MSR_IA32_MC0_MISC+12:
541 case MSR_IA32_MC0_MISC+16:
542 case MSR_IA32_UCODE_REV:
543 case MSR_IA32_PERF_STATUS:
544 case MSR_IA32_EBL_CR_POWERON:
547 case 0x200 ... 0x2ff:
550 case 0xcd: /* fsb frequency */
553 case MSR_IA32_APICBASE:
554 data = kvm_get_apic_base(vcpu);
556 case MSR_IA32_MISC_ENABLE:
557 data = vcpu->arch.ia32_misc_enable_msr;
561 data = vcpu->arch.shadow_efer;
565 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
571 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
574 * Read or write a bunch of msrs. All parameters are kernel addresses.
576 * @return number of msrs set successfully.
578 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
579 struct kvm_msr_entry *entries,
580 int (*do_msr)(struct kvm_vcpu *vcpu,
581 unsigned index, u64 *data))
587 for (i = 0; i < msrs->nmsrs; ++i)
588 if (do_msr(vcpu, entries[i].index, &entries[i].data))
597 * Read or write a bunch of msrs. Parameters are user addresses.
599 * @return number of msrs set successfully.
601 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
602 int (*do_msr)(struct kvm_vcpu *vcpu,
603 unsigned index, u64 *data),
606 struct kvm_msrs msrs;
607 struct kvm_msr_entry *entries;
612 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
616 if (msrs.nmsrs >= MAX_IO_MSRS)
620 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
621 entries = vmalloc(size);
626 if (copy_from_user(entries, user_msrs->entries, size))
629 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
634 if (writeback && copy_to_user(user_msrs->entries, entries, size))
646 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
649 void decache_vcpus_on_cpu(int cpu)
652 struct kvm_vcpu *vcpu;
655 spin_lock(&kvm_lock);
656 list_for_each_entry(vm, &vm_list, vm_list)
657 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
662 * If the vcpu is locked, then it is running on some
663 * other cpu and therefore it is not cached on the
666 * If it's not locked, check the last cpu it executed
669 if (mutex_trylock(&vcpu->mutex)) {
670 if (vcpu->cpu == cpu) {
671 kvm_x86_ops->vcpu_decache(vcpu);
674 mutex_unlock(&vcpu->mutex);
677 spin_unlock(&kvm_lock);
680 int kvm_dev_ioctl_check_extension(long ext)
685 case KVM_CAP_IRQCHIP:
687 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
688 case KVM_CAP_USER_MEMORY:
689 case KVM_CAP_SET_TSS_ADDR:
690 case KVM_CAP_EXT_CPUID:
694 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
704 long kvm_arch_dev_ioctl(struct file *filp,
705 unsigned int ioctl, unsigned long arg)
707 void __user *argp = (void __user *)arg;
711 case KVM_GET_MSR_INDEX_LIST: {
712 struct kvm_msr_list __user *user_msr_list = argp;
713 struct kvm_msr_list msr_list;
717 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
720 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
721 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
724 if (n < num_msrs_to_save)
727 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
728 num_msrs_to_save * sizeof(u32)))
730 if (copy_to_user(user_msr_list->indices
731 + num_msrs_to_save * sizeof(u32),
733 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
738 case KVM_GET_SUPPORTED_CPUID: {
739 struct kvm_cpuid2 __user *cpuid_arg = argp;
740 struct kvm_cpuid2 cpuid;
743 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
745 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
751 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
763 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
765 kvm_x86_ops->vcpu_load(vcpu, cpu);
768 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
770 kvm_x86_ops->vcpu_put(vcpu);
771 kvm_put_guest_fpu(vcpu);
774 static int is_efer_nx(void)
778 rdmsrl(MSR_EFER, efer);
779 return efer & EFER_NX;
782 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
785 struct kvm_cpuid_entry2 *e, *entry;
788 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
789 e = &vcpu->arch.cpuid_entries[i];
790 if (e->function == 0x80000001) {
795 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
796 entry->edx &= ~(1 << 20);
797 printk(KERN_INFO "kvm: guest NX capability removed\n");
801 /* when an old userspace process fills a new kernel module */
802 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
803 struct kvm_cpuid *cpuid,
804 struct kvm_cpuid_entry __user *entries)
807 struct kvm_cpuid_entry *cpuid_entries;
810 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
813 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
817 if (copy_from_user(cpuid_entries, entries,
818 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
820 for (i = 0; i < cpuid->nent; i++) {
821 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
822 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
823 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
824 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
825 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
826 vcpu->arch.cpuid_entries[i].index = 0;
827 vcpu->arch.cpuid_entries[i].flags = 0;
828 vcpu->arch.cpuid_entries[i].padding[0] = 0;
829 vcpu->arch.cpuid_entries[i].padding[1] = 0;
830 vcpu->arch.cpuid_entries[i].padding[2] = 0;
832 vcpu->arch.cpuid_nent = cpuid->nent;
833 cpuid_fix_nx_cap(vcpu);
837 vfree(cpuid_entries);
842 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
843 struct kvm_cpuid2 *cpuid,
844 struct kvm_cpuid_entry2 __user *entries)
849 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
852 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
853 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
855 vcpu->arch.cpuid_nent = cpuid->nent;
862 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
863 struct kvm_cpuid2 *cpuid,
864 struct kvm_cpuid_entry2 __user *entries)
869 if (cpuid->nent < vcpu->arch.cpuid_nent)
872 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
873 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
878 cpuid->nent = vcpu->arch.cpuid_nent;
882 static inline u32 bit(int bitno)
884 return 1 << (bitno & 31);
887 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
890 entry->function = function;
891 entry->index = index;
892 cpuid_count(entry->function, entry->index,
893 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
897 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
898 u32 index, int *nent, int maxnent)
900 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
901 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
902 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
903 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
904 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
905 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
906 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
907 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
908 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
909 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
910 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
911 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
912 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
913 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
914 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
915 bit(X86_FEATURE_PGE) |
916 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
917 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
918 bit(X86_FEATURE_SYSCALL) |
919 (bit(X86_FEATURE_NX) && is_efer_nx()) |
921 bit(X86_FEATURE_LM) |
923 bit(X86_FEATURE_MMXEXT) |
924 bit(X86_FEATURE_3DNOWEXT) |
925 bit(X86_FEATURE_3DNOW);
926 const u32 kvm_supported_word3_x86_features =
927 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
928 const u32 kvm_supported_word6_x86_features =
929 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
931 /* all func 2 cpuid_count() should be called on the same cpu */
933 do_cpuid_1_ent(entry, function, index);
938 entry->eax = min(entry->eax, (u32)0xb);
941 entry->edx &= kvm_supported_word0_x86_features;
942 entry->ecx &= kvm_supported_word3_x86_features;
944 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
945 * may return different values. This forces us to get_cpu() before
946 * issuing the first command, and also to emulate this annoying behavior
947 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
949 int t, times = entry->eax & 0xff;
951 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
952 for (t = 1; t < times && *nent < maxnent; ++t) {
953 do_cpuid_1_ent(&entry[t], function, 0);
954 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
959 /* function 4 and 0xb have additional index. */
961 int index, cache_type;
963 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
964 /* read more entries until cache_type is zero */
965 for (index = 1; *nent < maxnent; ++index) {
966 cache_type = entry[index - 1].eax & 0x1f;
969 do_cpuid_1_ent(&entry[index], function, index);
970 entry[index].flags |=
971 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
977 int index, level_type;
979 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
980 /* read more entries until level_type is zero */
981 for (index = 1; *nent < maxnent; ++index) {
982 level_type = entry[index - 1].ecx & 0xff;
985 do_cpuid_1_ent(&entry[index], function, index);
986 entry[index].flags |=
987 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
993 entry->eax = min(entry->eax, 0x8000001a);
996 entry->edx &= kvm_supported_word1_x86_features;
997 entry->ecx &= kvm_supported_word6_x86_features;
1003 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1004 struct kvm_cpuid_entry2 __user *entries)
1006 struct kvm_cpuid_entry2 *cpuid_entries;
1007 int limit, nent = 0, r = -E2BIG;
1010 if (cpuid->nent < 1)
1013 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1017 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1018 limit = cpuid_entries[0].eax;
1019 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1020 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1021 &nent, cpuid->nent);
1023 if (nent >= cpuid->nent)
1026 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1027 limit = cpuid_entries[nent - 1].eax;
1028 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1029 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1030 &nent, cpuid->nent);
1032 if (copy_to_user(entries, cpuid_entries,
1033 nent * sizeof(struct kvm_cpuid_entry2)))
1039 vfree(cpuid_entries);
1044 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1045 struct kvm_lapic_state *s)
1048 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1054 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1055 struct kvm_lapic_state *s)
1058 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1059 kvm_apic_post_state_restore(vcpu);
1065 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1066 struct kvm_interrupt *irq)
1068 if (irq->irq < 0 || irq->irq >= 256)
1070 if (irqchip_in_kernel(vcpu->kvm))
1074 set_bit(irq->irq, vcpu->arch.irq_pending);
1075 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1082 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1083 struct kvm_tpr_access_ctl *tac)
1087 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1091 long kvm_arch_vcpu_ioctl(struct file *filp,
1092 unsigned int ioctl, unsigned long arg)
1094 struct kvm_vcpu *vcpu = filp->private_data;
1095 void __user *argp = (void __user *)arg;
1099 case KVM_GET_LAPIC: {
1100 struct kvm_lapic_state lapic;
1102 memset(&lapic, 0, sizeof lapic);
1103 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1107 if (copy_to_user(argp, &lapic, sizeof lapic))
1112 case KVM_SET_LAPIC: {
1113 struct kvm_lapic_state lapic;
1116 if (copy_from_user(&lapic, argp, sizeof lapic))
1118 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1124 case KVM_INTERRUPT: {
1125 struct kvm_interrupt irq;
1128 if (copy_from_user(&irq, argp, sizeof irq))
1130 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1136 case KVM_SET_CPUID: {
1137 struct kvm_cpuid __user *cpuid_arg = argp;
1138 struct kvm_cpuid cpuid;
1141 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1143 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1148 case KVM_SET_CPUID2: {
1149 struct kvm_cpuid2 __user *cpuid_arg = argp;
1150 struct kvm_cpuid2 cpuid;
1153 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1155 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1156 cpuid_arg->entries);
1161 case KVM_GET_CPUID2: {
1162 struct kvm_cpuid2 __user *cpuid_arg = argp;
1163 struct kvm_cpuid2 cpuid;
1166 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1168 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1169 cpuid_arg->entries);
1173 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1179 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1182 r = msr_io(vcpu, argp, do_set_msr, 0);
1184 case KVM_TPR_ACCESS_REPORTING: {
1185 struct kvm_tpr_access_ctl tac;
1188 if (copy_from_user(&tac, argp, sizeof tac))
1190 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1194 if (copy_to_user(argp, &tac, sizeof tac))
1199 case KVM_SET_VAPIC_ADDR: {
1200 struct kvm_vapic_addr va;
1203 if (!irqchip_in_kernel(vcpu->kvm))
1206 if (copy_from_user(&va, argp, sizeof va))
1209 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1219 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1223 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1225 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1229 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1230 u32 kvm_nr_mmu_pages)
1232 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1235 down_write(¤t->mm->mmap_sem);
1237 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1238 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1240 up_write(¤t->mm->mmap_sem);
1244 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1246 return kvm->arch.n_alloc_mmu_pages;
1249 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1252 struct kvm_mem_alias *alias;
1254 for (i = 0; i < kvm->arch.naliases; ++i) {
1255 alias = &kvm->arch.aliases[i];
1256 if (gfn >= alias->base_gfn
1257 && gfn < alias->base_gfn + alias->npages)
1258 return alias->target_gfn + gfn - alias->base_gfn;
1264 * Set a new alias region. Aliases map a portion of physical memory into
1265 * another portion. This is useful for memory windows, for example the PC
1268 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1269 struct kvm_memory_alias *alias)
1272 struct kvm_mem_alias *p;
1275 /* General sanity checks */
1276 if (alias->memory_size & (PAGE_SIZE - 1))
1278 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1280 if (alias->slot >= KVM_ALIAS_SLOTS)
1282 if (alias->guest_phys_addr + alias->memory_size
1283 < alias->guest_phys_addr)
1285 if (alias->target_phys_addr + alias->memory_size
1286 < alias->target_phys_addr)
1289 down_write(¤t->mm->mmap_sem);
1291 p = &kvm->arch.aliases[alias->slot];
1292 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1293 p->npages = alias->memory_size >> PAGE_SHIFT;
1294 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1296 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1297 if (kvm->arch.aliases[n - 1].npages)
1299 kvm->arch.naliases = n;
1301 kvm_mmu_zap_all(kvm);
1303 up_write(¤t->mm->mmap_sem);
1311 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1316 switch (chip->chip_id) {
1317 case KVM_IRQCHIP_PIC_MASTER:
1318 memcpy(&chip->chip.pic,
1319 &pic_irqchip(kvm)->pics[0],
1320 sizeof(struct kvm_pic_state));
1322 case KVM_IRQCHIP_PIC_SLAVE:
1323 memcpy(&chip->chip.pic,
1324 &pic_irqchip(kvm)->pics[1],
1325 sizeof(struct kvm_pic_state));
1327 case KVM_IRQCHIP_IOAPIC:
1328 memcpy(&chip->chip.ioapic,
1329 ioapic_irqchip(kvm),
1330 sizeof(struct kvm_ioapic_state));
1339 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1344 switch (chip->chip_id) {
1345 case KVM_IRQCHIP_PIC_MASTER:
1346 memcpy(&pic_irqchip(kvm)->pics[0],
1348 sizeof(struct kvm_pic_state));
1350 case KVM_IRQCHIP_PIC_SLAVE:
1351 memcpy(&pic_irqchip(kvm)->pics[1],
1353 sizeof(struct kvm_pic_state));
1355 case KVM_IRQCHIP_IOAPIC:
1356 memcpy(ioapic_irqchip(kvm),
1358 sizeof(struct kvm_ioapic_state));
1364 kvm_pic_update_irq(pic_irqchip(kvm));
1369 * Get (and clear) the dirty memory log for a memory slot.
1371 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1372 struct kvm_dirty_log *log)
1376 struct kvm_memory_slot *memslot;
1379 down_write(¤t->mm->mmap_sem);
1381 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1385 /* If nothing is dirty, don't bother messing with page tables. */
1387 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1388 kvm_flush_remote_tlbs(kvm);
1389 memslot = &kvm->memslots[log->slot];
1390 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1391 memset(memslot->dirty_bitmap, 0, n);
1395 up_write(¤t->mm->mmap_sem);
1399 long kvm_arch_vm_ioctl(struct file *filp,
1400 unsigned int ioctl, unsigned long arg)
1402 struct kvm *kvm = filp->private_data;
1403 void __user *argp = (void __user *)arg;
1407 case KVM_SET_TSS_ADDR:
1408 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1412 case KVM_SET_MEMORY_REGION: {
1413 struct kvm_memory_region kvm_mem;
1414 struct kvm_userspace_memory_region kvm_userspace_mem;
1417 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1419 kvm_userspace_mem.slot = kvm_mem.slot;
1420 kvm_userspace_mem.flags = kvm_mem.flags;
1421 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1422 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1423 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1428 case KVM_SET_NR_MMU_PAGES:
1429 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1433 case KVM_GET_NR_MMU_PAGES:
1434 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1436 case KVM_SET_MEMORY_ALIAS: {
1437 struct kvm_memory_alias alias;
1440 if (copy_from_user(&alias, argp, sizeof alias))
1442 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1447 case KVM_CREATE_IRQCHIP:
1449 kvm->arch.vpic = kvm_create_pic(kvm);
1450 if (kvm->arch.vpic) {
1451 r = kvm_ioapic_init(kvm);
1453 kfree(kvm->arch.vpic);
1454 kvm->arch.vpic = NULL;
1460 case KVM_IRQ_LINE: {
1461 struct kvm_irq_level irq_event;
1464 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1466 if (irqchip_in_kernel(kvm)) {
1467 mutex_lock(&kvm->lock);
1468 if (irq_event.irq < 16)
1469 kvm_pic_set_irq(pic_irqchip(kvm),
1472 kvm_ioapic_set_irq(kvm->arch.vioapic,
1475 mutex_unlock(&kvm->lock);
1480 case KVM_GET_IRQCHIP: {
1481 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1482 struct kvm_irqchip chip;
1485 if (copy_from_user(&chip, argp, sizeof chip))
1488 if (!irqchip_in_kernel(kvm))
1490 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1494 if (copy_to_user(argp, &chip, sizeof chip))
1499 case KVM_SET_IRQCHIP: {
1500 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1501 struct kvm_irqchip chip;
1504 if (copy_from_user(&chip, argp, sizeof chip))
1507 if (!irqchip_in_kernel(kvm))
1509 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1522 static void kvm_init_msr_list(void)
1527 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1528 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1531 msrs_to_save[j] = msrs_to_save[i];
1534 num_msrs_to_save = j;
1538 * Only apic need an MMIO device hook, so shortcut now..
1540 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1543 struct kvm_io_device *dev;
1545 if (vcpu->arch.apic) {
1546 dev = &vcpu->arch.apic->dev;
1547 if (dev->in_range(dev, addr))
1554 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1557 struct kvm_io_device *dev;
1559 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1561 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1565 int emulator_read_std(unsigned long addr,
1568 struct kvm_vcpu *vcpu)
1571 int r = X86EMUL_CONTINUE;
1573 down_read(¤t->mm->mmap_sem);
1575 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1576 unsigned offset = addr & (PAGE_SIZE-1);
1577 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1580 if (gpa == UNMAPPED_GVA) {
1581 r = X86EMUL_PROPAGATE_FAULT;
1584 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1586 r = X86EMUL_UNHANDLEABLE;
1595 up_read(¤t->mm->mmap_sem);
1598 EXPORT_SYMBOL_GPL(emulator_read_std);
1600 static int emulator_read_emulated(unsigned long addr,
1603 struct kvm_vcpu *vcpu)
1605 struct kvm_io_device *mmio_dev;
1608 if (vcpu->mmio_read_completed) {
1609 memcpy(val, vcpu->mmio_data, bytes);
1610 vcpu->mmio_read_completed = 0;
1611 return X86EMUL_CONTINUE;
1614 down_read(¤t->mm->mmap_sem);
1615 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1616 up_read(¤t->mm->mmap_sem);
1618 /* For APIC access vmexit */
1619 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1622 if (emulator_read_std(addr, val, bytes, vcpu)
1623 == X86EMUL_CONTINUE)
1624 return X86EMUL_CONTINUE;
1625 if (gpa == UNMAPPED_GVA)
1626 return X86EMUL_PROPAGATE_FAULT;
1630 * Is this MMIO handled locally?
1632 mutex_lock(&vcpu->kvm->lock);
1633 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1635 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1636 mutex_unlock(&vcpu->kvm->lock);
1637 return X86EMUL_CONTINUE;
1639 mutex_unlock(&vcpu->kvm->lock);
1641 vcpu->mmio_needed = 1;
1642 vcpu->mmio_phys_addr = gpa;
1643 vcpu->mmio_size = bytes;
1644 vcpu->mmio_is_write = 0;
1646 return X86EMUL_UNHANDLEABLE;
1649 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1650 const void *val, int bytes)
1654 down_read(¤t->mm->mmap_sem);
1655 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1657 up_read(¤t->mm->mmap_sem);
1660 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1661 up_read(¤t->mm->mmap_sem);
1665 static int emulator_write_emulated_onepage(unsigned long addr,
1668 struct kvm_vcpu *vcpu)
1670 struct kvm_io_device *mmio_dev;
1673 down_read(¤t->mm->mmap_sem);
1674 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1675 up_read(¤t->mm->mmap_sem);
1677 if (gpa == UNMAPPED_GVA) {
1678 kvm_inject_page_fault(vcpu, addr, 2);
1679 return X86EMUL_PROPAGATE_FAULT;
1682 /* For APIC access vmexit */
1683 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1686 if (emulator_write_phys(vcpu, gpa, val, bytes))
1687 return X86EMUL_CONTINUE;
1691 * Is this MMIO handled locally?
1693 mutex_lock(&vcpu->kvm->lock);
1694 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1696 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1697 mutex_unlock(&vcpu->kvm->lock);
1698 return X86EMUL_CONTINUE;
1700 mutex_unlock(&vcpu->kvm->lock);
1702 vcpu->mmio_needed = 1;
1703 vcpu->mmio_phys_addr = gpa;
1704 vcpu->mmio_size = bytes;
1705 vcpu->mmio_is_write = 1;
1706 memcpy(vcpu->mmio_data, val, bytes);
1708 return X86EMUL_CONTINUE;
1711 int emulator_write_emulated(unsigned long addr,
1714 struct kvm_vcpu *vcpu)
1716 /* Crossing a page boundary? */
1717 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1720 now = -addr & ~PAGE_MASK;
1721 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1722 if (rc != X86EMUL_CONTINUE)
1728 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1730 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1732 static int emulator_cmpxchg_emulated(unsigned long addr,
1736 struct kvm_vcpu *vcpu)
1738 static int reported;
1742 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1744 #ifndef CONFIG_X86_64
1745 /* guests cmpxchg8b have to be emulated atomically */
1752 down_read(¤t->mm->mmap_sem);
1753 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1755 if (gpa == UNMAPPED_GVA ||
1756 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1759 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1763 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1764 kaddr = kmap_atomic(page, KM_USER0);
1765 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1766 kunmap_atomic(kaddr, KM_USER0);
1767 kvm_release_page_dirty(page);
1769 up_read(¤t->mm->mmap_sem);
1773 return emulator_write_emulated(addr, new, bytes, vcpu);
1776 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1778 return kvm_x86_ops->get_segment_base(vcpu, seg);
1781 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1783 return X86EMUL_CONTINUE;
1786 int emulate_clts(struct kvm_vcpu *vcpu)
1788 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
1789 return X86EMUL_CONTINUE;
1792 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1794 struct kvm_vcpu *vcpu = ctxt->vcpu;
1798 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1799 return X86EMUL_CONTINUE;
1801 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1802 return X86EMUL_UNHANDLEABLE;
1806 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1808 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1811 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1813 /* FIXME: better handling */
1814 return X86EMUL_UNHANDLEABLE;
1816 return X86EMUL_CONTINUE;
1819 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1821 static int reported;
1823 unsigned long rip = vcpu->arch.rip;
1824 unsigned long rip_linear;
1826 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1831 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1833 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1834 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1837 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1839 struct x86_emulate_ops emulate_ops = {
1840 .read_std = emulator_read_std,
1841 .read_emulated = emulator_read_emulated,
1842 .write_emulated = emulator_write_emulated,
1843 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1846 int emulate_instruction(struct kvm_vcpu *vcpu,
1847 struct kvm_run *run,
1853 struct decode_cache *c;
1855 vcpu->arch.mmio_fault_cr2 = cr2;
1856 kvm_x86_ops->cache_regs(vcpu);
1858 vcpu->mmio_is_write = 0;
1859 vcpu->arch.pio.string = 0;
1861 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
1863 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1865 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1866 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1867 vcpu->arch.emulate_ctxt.mode =
1868 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
1869 ? X86EMUL_MODE_REAL : cs_l
1870 ? X86EMUL_MODE_PROT64 : cs_db
1871 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1873 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1874 vcpu->arch.emulate_ctxt.cs_base = 0;
1875 vcpu->arch.emulate_ctxt.ds_base = 0;
1876 vcpu->arch.emulate_ctxt.es_base = 0;
1877 vcpu->arch.emulate_ctxt.ss_base = 0;
1879 vcpu->arch.emulate_ctxt.cs_base =
1880 get_segment_base(vcpu, VCPU_SREG_CS);
1881 vcpu->arch.emulate_ctxt.ds_base =
1882 get_segment_base(vcpu, VCPU_SREG_DS);
1883 vcpu->arch.emulate_ctxt.es_base =
1884 get_segment_base(vcpu, VCPU_SREG_ES);
1885 vcpu->arch.emulate_ctxt.ss_base =
1886 get_segment_base(vcpu, VCPU_SREG_SS);
1889 vcpu->arch.emulate_ctxt.gs_base =
1890 get_segment_base(vcpu, VCPU_SREG_GS);
1891 vcpu->arch.emulate_ctxt.fs_base =
1892 get_segment_base(vcpu, VCPU_SREG_FS);
1894 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1896 /* Reject the instructions other than VMCALL/VMMCALL when
1897 * try to emulate invalid opcode */
1898 c = &vcpu->arch.emulate_ctxt.decode;
1899 if ((emulation_type & EMULTYPE_TRAP_UD) &&
1900 (!(c->twobyte && c->b == 0x01 &&
1901 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
1902 c->modrm_mod == 3 && c->modrm_rm == 1)))
1903 return EMULATE_FAIL;
1905 ++vcpu->stat.insn_emulation;
1907 ++vcpu->stat.insn_emulation_fail;
1908 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1909 return EMULATE_DONE;
1910 return EMULATE_FAIL;
1914 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1916 if (vcpu->arch.pio.string)
1917 return EMULATE_DO_MMIO;
1919 if ((r || vcpu->mmio_is_write) && run) {
1920 run->exit_reason = KVM_EXIT_MMIO;
1921 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1922 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1923 run->mmio.len = vcpu->mmio_size;
1924 run->mmio.is_write = vcpu->mmio_is_write;
1928 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1929 return EMULATE_DONE;
1930 if (!vcpu->mmio_needed) {
1931 kvm_report_emulation_failure(vcpu, "mmio");
1932 return EMULATE_FAIL;
1934 return EMULATE_DO_MMIO;
1937 kvm_x86_ops->decache_regs(vcpu);
1938 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
1940 if (vcpu->mmio_is_write) {
1941 vcpu->mmio_needed = 0;
1942 return EMULATE_DO_MMIO;
1945 return EMULATE_DONE;
1947 EXPORT_SYMBOL_GPL(emulate_instruction);
1949 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1953 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
1954 if (vcpu->arch.pio.guest_pages[i]) {
1955 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
1956 vcpu->arch.pio.guest_pages[i] = NULL;
1960 static int pio_copy_data(struct kvm_vcpu *vcpu)
1962 void *p = vcpu->arch.pio_data;
1965 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
1967 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1970 free_pio_guest_pages(vcpu);
1973 q += vcpu->arch.pio.guest_page_offset;
1974 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
1975 if (vcpu->arch.pio.in)
1976 memcpy(q, p, bytes);
1978 memcpy(p, q, bytes);
1979 q -= vcpu->arch.pio.guest_page_offset;
1981 free_pio_guest_pages(vcpu);
1985 int complete_pio(struct kvm_vcpu *vcpu)
1987 struct kvm_pio_request *io = &vcpu->arch.pio;
1991 kvm_x86_ops->cache_regs(vcpu);
1995 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
1999 r = pio_copy_data(vcpu);
2001 kvm_x86_ops->cache_regs(vcpu);
2008 delta *= io->cur_count;
2010 * The size of the register should really depend on
2011 * current address size.
2013 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2019 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2021 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2024 kvm_x86_ops->decache_regs(vcpu);
2026 io->count -= io->cur_count;
2032 static void kernel_pio(struct kvm_io_device *pio_dev,
2033 struct kvm_vcpu *vcpu,
2036 /* TODO: String I/O for in kernel device */
2038 mutex_lock(&vcpu->kvm->lock);
2039 if (vcpu->arch.pio.in)
2040 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2041 vcpu->arch.pio.size,
2044 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2045 vcpu->arch.pio.size,
2047 mutex_unlock(&vcpu->kvm->lock);
2050 static void pio_string_write(struct kvm_io_device *pio_dev,
2051 struct kvm_vcpu *vcpu)
2053 struct kvm_pio_request *io = &vcpu->arch.pio;
2054 void *pd = vcpu->arch.pio_data;
2057 mutex_lock(&vcpu->kvm->lock);
2058 for (i = 0; i < io->cur_count; i++) {
2059 kvm_iodevice_write(pio_dev, io->port,
2064 mutex_unlock(&vcpu->kvm->lock);
2067 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2070 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2073 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2074 int size, unsigned port)
2076 struct kvm_io_device *pio_dev;
2078 vcpu->run->exit_reason = KVM_EXIT_IO;
2079 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2080 vcpu->run->io.size = vcpu->arch.pio.size = size;
2081 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2082 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2083 vcpu->run->io.port = vcpu->arch.pio.port = port;
2084 vcpu->arch.pio.in = in;
2085 vcpu->arch.pio.string = 0;
2086 vcpu->arch.pio.down = 0;
2087 vcpu->arch.pio.guest_page_offset = 0;
2088 vcpu->arch.pio.rep = 0;
2090 kvm_x86_ops->cache_regs(vcpu);
2091 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2092 kvm_x86_ops->decache_regs(vcpu);
2094 kvm_x86_ops->skip_emulated_instruction(vcpu);
2096 pio_dev = vcpu_find_pio_dev(vcpu, port);
2098 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2104 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2106 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2107 int size, unsigned long count, int down,
2108 gva_t address, int rep, unsigned port)
2110 unsigned now, in_page;
2114 struct kvm_io_device *pio_dev;
2116 vcpu->run->exit_reason = KVM_EXIT_IO;
2117 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2118 vcpu->run->io.size = vcpu->arch.pio.size = size;
2119 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2120 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2121 vcpu->run->io.port = vcpu->arch.pio.port = port;
2122 vcpu->arch.pio.in = in;
2123 vcpu->arch.pio.string = 1;
2124 vcpu->arch.pio.down = down;
2125 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2126 vcpu->arch.pio.rep = rep;
2129 kvm_x86_ops->skip_emulated_instruction(vcpu);
2134 in_page = PAGE_SIZE - offset_in_page(address);
2136 in_page = offset_in_page(address) + size;
2137 now = min(count, (unsigned long)in_page / size);
2140 * String I/O straddles page boundary. Pin two guest pages
2141 * so that we satisfy atomicity constraints. Do just one
2142 * transaction to avoid complexity.
2149 * String I/O in reverse. Yuck. Kill the guest, fix later.
2151 pr_unimpl(vcpu, "guest string pio down\n");
2152 kvm_inject_gp(vcpu, 0);
2155 vcpu->run->io.count = now;
2156 vcpu->arch.pio.cur_count = now;
2158 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2159 kvm_x86_ops->skip_emulated_instruction(vcpu);
2161 for (i = 0; i < nr_pages; ++i) {
2162 down_read(¤t->mm->mmap_sem);
2163 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2164 vcpu->arch.pio.guest_pages[i] = page;
2165 up_read(¤t->mm->mmap_sem);
2167 kvm_inject_gp(vcpu, 0);
2168 free_pio_guest_pages(vcpu);
2173 pio_dev = vcpu_find_pio_dev(vcpu, port);
2174 if (!vcpu->arch.pio.in) {
2175 /* string PIO write */
2176 ret = pio_copy_data(vcpu);
2177 if (ret >= 0 && pio_dev) {
2178 pio_string_write(pio_dev, vcpu);
2180 if (vcpu->arch.pio.count == 0)
2184 pr_unimpl(vcpu, "no string pio read support yet, "
2185 "port %x size %d count %ld\n",
2190 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2192 int kvm_arch_init(void *opaque)
2195 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2198 printk(KERN_ERR "kvm: already loaded the other module\n");
2203 if (!ops->cpu_has_kvm_support()) {
2204 printk(KERN_ERR "kvm: no hardware support\n");
2208 if (ops->disabled_by_bios()) {
2209 printk(KERN_ERR "kvm: disabled by bios\n");
2214 r = kvm_mmu_module_init();
2218 kvm_init_msr_list();
2221 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2228 void kvm_arch_exit(void)
2231 kvm_mmu_module_exit();
2234 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2236 ++vcpu->stat.halt_exits;
2237 if (irqchip_in_kernel(vcpu->kvm)) {
2238 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
2239 kvm_vcpu_block(vcpu);
2240 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
2244 vcpu->run->exit_reason = KVM_EXIT_HLT;
2248 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2250 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2252 unsigned long nr, a0, a1, a2, a3, ret;
2254 kvm_x86_ops->cache_regs(vcpu);
2256 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2257 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2258 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2259 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2260 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2262 if (!is_long_mode(vcpu)) {
2271 case KVM_HC_VAPIC_POLL_IRQ:
2278 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2279 kvm_x86_ops->decache_regs(vcpu);
2282 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2284 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2286 char instruction[3];
2291 * Blow out the MMU to ensure that no other VCPU has an active mapping
2292 * to ensure that the updated hypercall appears atomically across all
2295 kvm_mmu_zap_all(vcpu->kvm);
2297 kvm_x86_ops->cache_regs(vcpu);
2298 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2299 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2300 != X86EMUL_CONTINUE)
2306 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2308 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2311 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2313 struct descriptor_table dt = { limit, base };
2315 kvm_x86_ops->set_gdt(vcpu, &dt);
2318 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2320 struct descriptor_table dt = { limit, base };
2322 kvm_x86_ops->set_idt(vcpu, &dt);
2325 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2326 unsigned long *rflags)
2329 *rflags = kvm_x86_ops->get_rflags(vcpu);
2332 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2334 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2337 return vcpu->arch.cr0;
2339 return vcpu->arch.cr2;
2341 return vcpu->arch.cr3;
2343 return vcpu->arch.cr4;
2345 return get_cr8(vcpu);
2347 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2352 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2353 unsigned long *rflags)
2357 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2358 *rflags = kvm_x86_ops->get_rflags(vcpu);
2361 vcpu->arch.cr2 = val;
2367 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2370 set_cr8(vcpu, val & 0xfUL);
2373 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2377 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2379 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2380 int j, nent = vcpu->arch.cpuid_nent;
2382 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2383 /* when no next entry is found, the current entry[i] is reselected */
2384 for (j = i + 1; j == i; j = (j + 1) % nent) {
2385 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2386 if (ej->function == e->function) {
2387 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2391 return 0; /* silence gcc, even though control never reaches here */
2394 /* find an entry with matching function, matching index (if needed), and that
2395 * should be read next (if it's stateful) */
2396 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2397 u32 function, u32 index)
2399 if (e->function != function)
2401 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2403 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2404 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2409 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2412 u32 function, index;
2413 struct kvm_cpuid_entry2 *e, *best;
2415 kvm_x86_ops->cache_regs(vcpu);
2416 function = vcpu->arch.regs[VCPU_REGS_RAX];
2417 index = vcpu->arch.regs[VCPU_REGS_RCX];
2418 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2419 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2420 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2421 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2423 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2424 e = &vcpu->arch.cpuid_entries[i];
2425 if (is_matching_cpuid_entry(e, function, index)) {
2426 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2427 move_to_next_stateful_cpuid_entry(vcpu, i);
2432 * Both basic or both extended?
2434 if (((e->function ^ function) & 0x80000000) == 0)
2435 if (!best || e->function > best->function)
2439 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2440 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2441 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2442 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2444 kvm_x86_ops->decache_regs(vcpu);
2445 kvm_x86_ops->skip_emulated_instruction(vcpu);
2447 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2450 * Check if userspace requested an interrupt window, and that the
2451 * interrupt window is open.
2453 * No need to exit to userspace if we already have an interrupt queued.
2455 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2456 struct kvm_run *kvm_run)
2458 return (!vcpu->arch.irq_summary &&
2459 kvm_run->request_interrupt_window &&
2460 vcpu->arch.interrupt_window_open &&
2461 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2464 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2465 struct kvm_run *kvm_run)
2467 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2468 kvm_run->cr8 = get_cr8(vcpu);
2469 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2470 if (irqchip_in_kernel(vcpu->kvm))
2471 kvm_run->ready_for_interrupt_injection = 1;
2473 kvm_run->ready_for_interrupt_injection =
2474 (vcpu->arch.interrupt_window_open &&
2475 vcpu->arch.irq_summary == 0);
2478 static void vapic_enter(struct kvm_vcpu *vcpu)
2480 struct kvm_lapic *apic = vcpu->arch.apic;
2483 if (!apic || !apic->vapic_addr)
2486 down_read(¤t->mm->mmap_sem);
2487 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2488 vcpu->arch.apic->vapic_page = page;
2489 up_read(¤t->mm->mmap_sem);
2492 static void vapic_exit(struct kvm_vcpu *vcpu)
2494 struct kvm_lapic *apic = vcpu->arch.apic;
2496 if (!apic || !apic->vapic_addr)
2499 kvm_release_page_dirty(apic->vapic_page);
2500 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2503 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2507 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2508 pr_debug("vcpu %d received sipi with vector # %x\n",
2509 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2510 kvm_lapic_reset(vcpu);
2511 r = kvm_x86_ops->vcpu_reset(vcpu);
2514 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
2520 if (vcpu->guest_debug.enabled)
2521 kvm_x86_ops->guest_debug_pre(vcpu);
2524 r = kvm_mmu_reload(vcpu);
2528 if (vcpu->requests) {
2529 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2530 __kvm_migrate_apic_timer(vcpu);
2531 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2533 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2539 kvm_inject_pending_timer_irqs(vcpu);
2543 kvm_x86_ops->prepare_guest_switch(vcpu);
2544 kvm_load_guest_fpu(vcpu);
2546 local_irq_disable();
2548 if (need_resched()) {
2555 if (signal_pending(current)) {
2559 kvm_run->exit_reason = KVM_EXIT_INTR;
2560 ++vcpu->stat.signal_exits;
2564 if (vcpu->arch.exception.pending)
2565 __queue_exception(vcpu);
2566 else if (irqchip_in_kernel(vcpu->kvm))
2567 kvm_x86_ops->inject_pending_irq(vcpu);
2569 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2571 kvm_lapic_sync_to_vapic(vcpu);
2573 vcpu->guest_mode = 1;
2577 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2578 kvm_x86_ops->tlb_flush(vcpu);
2580 kvm_x86_ops->run(vcpu, kvm_run);
2582 vcpu->guest_mode = 0;
2588 * We must have an instruction between local_irq_enable() and
2589 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2590 * the interrupt shadow. The stat.exits increment will do nicely.
2591 * But we need to prevent reordering, hence this barrier():
2600 * Profile KVM exit RIPs:
2602 if (unlikely(prof_on == KVM_PROFILING)) {
2603 kvm_x86_ops->cache_regs(vcpu);
2604 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2607 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2608 vcpu->arch.exception.pending = false;
2610 kvm_lapic_sync_from_vapic(vcpu);
2612 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2615 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2617 kvm_run->exit_reason = KVM_EXIT_INTR;
2618 ++vcpu->stat.request_irq_exits;
2621 if (!need_resched())
2631 post_kvm_run_save(vcpu, kvm_run);
2638 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2645 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2646 kvm_vcpu_block(vcpu);
2651 if (vcpu->sigset_active)
2652 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2654 /* re-sync apic's tpr */
2655 if (!irqchip_in_kernel(vcpu->kvm))
2656 set_cr8(vcpu, kvm_run->cr8);
2658 if (vcpu->arch.pio.cur_count) {
2659 r = complete_pio(vcpu);
2663 #if CONFIG_HAS_IOMEM
2664 if (vcpu->mmio_needed) {
2665 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2666 vcpu->mmio_read_completed = 1;
2667 vcpu->mmio_needed = 0;
2668 r = emulate_instruction(vcpu, kvm_run,
2669 vcpu->arch.mmio_fault_cr2, 0,
2670 EMULTYPE_NO_DECODE);
2671 if (r == EMULATE_DO_MMIO) {
2673 * Read-modify-write. Back to userspace.
2680 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2681 kvm_x86_ops->cache_regs(vcpu);
2682 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2683 kvm_x86_ops->decache_regs(vcpu);
2686 r = __vcpu_run(vcpu, kvm_run);
2689 if (vcpu->sigset_active)
2690 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2696 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2700 kvm_x86_ops->cache_regs(vcpu);
2702 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2703 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2704 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2705 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2706 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2707 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2708 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2709 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
2710 #ifdef CONFIG_X86_64
2711 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2712 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2713 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2714 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2715 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2716 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2717 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2718 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
2721 regs->rip = vcpu->arch.rip;
2722 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2725 * Don't leak debug flags in case they were set for guest debugging
2727 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2728 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2735 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2739 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2740 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2741 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2742 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2743 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2744 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2745 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2746 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
2747 #ifdef CONFIG_X86_64
2748 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2749 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2750 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2751 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2752 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2753 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2754 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2755 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
2758 vcpu->arch.rip = regs->rip;
2759 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2761 kvm_x86_ops->decache_regs(vcpu);
2768 static void get_segment(struct kvm_vcpu *vcpu,
2769 struct kvm_segment *var, int seg)
2771 return kvm_x86_ops->get_segment(vcpu, var, seg);
2774 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2776 struct kvm_segment cs;
2778 get_segment(vcpu, &cs, VCPU_SREG_CS);
2782 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2784 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2785 struct kvm_sregs *sregs)
2787 struct descriptor_table dt;
2792 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2793 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2794 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2795 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2796 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2797 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2799 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2800 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2802 kvm_x86_ops->get_idt(vcpu, &dt);
2803 sregs->idt.limit = dt.limit;
2804 sregs->idt.base = dt.base;
2805 kvm_x86_ops->get_gdt(vcpu, &dt);
2806 sregs->gdt.limit = dt.limit;
2807 sregs->gdt.base = dt.base;
2809 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2810 sregs->cr0 = vcpu->arch.cr0;
2811 sregs->cr2 = vcpu->arch.cr2;
2812 sregs->cr3 = vcpu->arch.cr3;
2813 sregs->cr4 = vcpu->arch.cr4;
2814 sregs->cr8 = get_cr8(vcpu);
2815 sregs->efer = vcpu->arch.shadow_efer;
2816 sregs->apic_base = kvm_get_apic_base(vcpu);
2818 if (irqchip_in_kernel(vcpu->kvm)) {
2819 memset(sregs->interrupt_bitmap, 0,
2820 sizeof sregs->interrupt_bitmap);
2821 pending_vec = kvm_x86_ops->get_irq(vcpu);
2822 if (pending_vec >= 0)
2823 set_bit(pending_vec,
2824 (unsigned long *)sregs->interrupt_bitmap);
2826 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
2827 sizeof sregs->interrupt_bitmap);
2834 static void set_segment(struct kvm_vcpu *vcpu,
2835 struct kvm_segment *var, int seg)
2837 return kvm_x86_ops->set_segment(vcpu, var, seg);
2840 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2841 struct kvm_sregs *sregs)
2843 int mmu_reset_needed = 0;
2844 int i, pending_vec, max_bits;
2845 struct descriptor_table dt;
2849 dt.limit = sregs->idt.limit;
2850 dt.base = sregs->idt.base;
2851 kvm_x86_ops->set_idt(vcpu, &dt);
2852 dt.limit = sregs->gdt.limit;
2853 dt.base = sregs->gdt.base;
2854 kvm_x86_ops->set_gdt(vcpu, &dt);
2856 vcpu->arch.cr2 = sregs->cr2;
2857 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2858 vcpu->arch.cr3 = sregs->cr3;
2860 set_cr8(vcpu, sregs->cr8);
2862 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
2863 #ifdef CONFIG_X86_64
2864 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2866 kvm_set_apic_base(vcpu, sregs->apic_base);
2868 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2870 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
2871 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2872 vcpu->arch.cr0 = sregs->cr0;
2874 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
2875 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2876 if (!is_long_mode(vcpu) && is_pae(vcpu))
2877 load_pdptrs(vcpu, vcpu->arch.cr3);
2879 if (mmu_reset_needed)
2880 kvm_mmu_reset_context(vcpu);
2882 if (!irqchip_in_kernel(vcpu->kvm)) {
2883 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
2884 sizeof vcpu->arch.irq_pending);
2885 vcpu->arch.irq_summary = 0;
2886 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
2887 if (vcpu->arch.irq_pending[i])
2888 __set_bit(i, &vcpu->arch.irq_summary);
2890 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2891 pending_vec = find_first_bit(
2892 (const unsigned long *)sregs->interrupt_bitmap,
2894 /* Only pending external irq is handled here */
2895 if (pending_vec < max_bits) {
2896 kvm_x86_ops->set_irq(vcpu, pending_vec);
2897 pr_debug("Set back pending irq %d\n",
2902 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2903 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2904 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2905 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2906 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2907 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2909 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2910 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2917 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2918 struct kvm_debug_guest *dbg)
2924 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2932 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2933 * we have asm/x86/processor.h
2944 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2945 #ifdef CONFIG_X86_64
2946 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2948 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2953 * Translate a guest virtual address to a guest physical address.
2955 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2956 struct kvm_translation *tr)
2958 unsigned long vaddr = tr->linear_address;
2962 down_read(¤t->mm->mmap_sem);
2963 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
2964 up_read(¤t->mm->mmap_sem);
2965 tr->physical_address = gpa;
2966 tr->valid = gpa != UNMAPPED_GVA;
2974 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2976 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
2980 memcpy(fpu->fpr, fxsave->st_space, 128);
2981 fpu->fcw = fxsave->cwd;
2982 fpu->fsw = fxsave->swd;
2983 fpu->ftwx = fxsave->twd;
2984 fpu->last_opcode = fxsave->fop;
2985 fpu->last_ip = fxsave->rip;
2986 fpu->last_dp = fxsave->rdp;
2987 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2994 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2996 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3000 memcpy(fxsave->st_space, fpu->fpr, 128);
3001 fxsave->cwd = fpu->fcw;
3002 fxsave->swd = fpu->fsw;
3003 fxsave->twd = fpu->ftwx;
3004 fxsave->fop = fpu->last_opcode;
3005 fxsave->rip = fpu->last_ip;
3006 fxsave->rdp = fpu->last_dp;
3007 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3014 void fx_init(struct kvm_vcpu *vcpu)
3016 unsigned after_mxcsr_mask;
3018 /* Initialize guest FPU by resetting ours and saving into guest's */
3020 fx_save(&vcpu->arch.host_fx_image);
3022 fx_save(&vcpu->arch.guest_fx_image);
3023 fx_restore(&vcpu->arch.host_fx_image);
3026 vcpu->arch.cr0 |= X86_CR0_ET;
3027 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3028 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3029 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3030 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3032 EXPORT_SYMBOL_GPL(fx_init);
3034 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3036 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3039 vcpu->guest_fpu_loaded = 1;
3040 fx_save(&vcpu->arch.host_fx_image);
3041 fx_restore(&vcpu->arch.guest_fx_image);
3043 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3045 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3047 if (!vcpu->guest_fpu_loaded)
3050 vcpu->guest_fpu_loaded = 0;
3051 fx_save(&vcpu->arch.guest_fx_image);
3052 fx_restore(&vcpu->arch.host_fx_image);
3053 ++vcpu->stat.fpu_reload;
3055 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3057 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3059 kvm_x86_ops->vcpu_free(vcpu);
3062 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3065 return kvm_x86_ops->vcpu_create(kvm, id);
3068 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3072 /* We do fxsave: this must be aligned. */
3073 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3076 r = kvm_arch_vcpu_reset(vcpu);
3078 r = kvm_mmu_setup(vcpu);
3085 kvm_x86_ops->vcpu_free(vcpu);
3089 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3092 kvm_mmu_unload(vcpu);
3095 kvm_x86_ops->vcpu_free(vcpu);
3098 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3100 return kvm_x86_ops->vcpu_reset(vcpu);
3103 void kvm_arch_hardware_enable(void *garbage)
3105 kvm_x86_ops->hardware_enable(garbage);
3108 void kvm_arch_hardware_disable(void *garbage)
3110 kvm_x86_ops->hardware_disable(garbage);
3113 int kvm_arch_hardware_setup(void)
3115 return kvm_x86_ops->hardware_setup();
3118 void kvm_arch_hardware_unsetup(void)
3120 kvm_x86_ops->hardware_unsetup();
3123 void kvm_arch_check_processor_compat(void *rtn)
3125 kvm_x86_ops->check_processor_compatibility(rtn);
3128 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3134 BUG_ON(vcpu->kvm == NULL);
3137 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3138 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3139 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
3141 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
3143 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3148 vcpu->arch.pio_data = page_address(page);
3150 r = kvm_mmu_create(vcpu);
3152 goto fail_free_pio_data;
3154 if (irqchip_in_kernel(kvm)) {
3155 r = kvm_create_lapic(vcpu);
3157 goto fail_mmu_destroy;
3163 kvm_mmu_destroy(vcpu);
3165 free_page((unsigned long)vcpu->arch.pio_data);
3170 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3172 kvm_free_lapic(vcpu);
3173 kvm_mmu_destroy(vcpu);
3174 free_page((unsigned long)vcpu->arch.pio_data);
3177 struct kvm *kvm_arch_create_vm(void)
3179 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3182 return ERR_PTR(-ENOMEM);
3184 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3189 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3192 kvm_mmu_unload(vcpu);
3196 static void kvm_free_vcpus(struct kvm *kvm)
3201 * Unpin any mmu pages first.
3203 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3205 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3206 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3207 if (kvm->vcpus[i]) {
3208 kvm_arch_vcpu_free(kvm->vcpus[i]);
3209 kvm->vcpus[i] = NULL;
3215 void kvm_arch_destroy_vm(struct kvm *kvm)
3217 kfree(kvm->arch.vpic);
3218 kfree(kvm->arch.vioapic);
3219 kvm_free_vcpus(kvm);
3220 kvm_free_physmem(kvm);
3224 int kvm_arch_set_memory_region(struct kvm *kvm,
3225 struct kvm_userspace_memory_region *mem,
3226 struct kvm_memory_slot old,
3229 int npages = mem->memory_size >> PAGE_SHIFT;
3230 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3232 /*To keep backward compatibility with older userspace,
3233 *x86 needs to hanlde !user_alloc case.
3236 if (npages && !old.rmap) {
3237 memslot->userspace_addr = do_mmap(NULL, 0,
3239 PROT_READ | PROT_WRITE,
3240 MAP_SHARED | MAP_ANONYMOUS,
3243 if (IS_ERR((void *)memslot->userspace_addr))
3244 return PTR_ERR((void *)memslot->userspace_addr);
3246 if (!old.user_alloc && old.rmap) {
3249 ret = do_munmap(current->mm, old.userspace_addr,
3250 old.npages * PAGE_SIZE);
3253 "kvm_vm_ioctl_set_memory_region: "
3254 "failed to munmap memory\n");
3259 if (!kvm->arch.n_requested_mmu_pages) {
3260 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3261 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3264 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3265 kvm_flush_remote_tlbs(kvm);
3270 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3272 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3273 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3276 static void vcpu_kick_intr(void *info)
3279 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3280 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3284 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3286 int ipi_pcpu = vcpu->cpu;
3288 if (waitqueue_active(&vcpu->wq)) {
3289 wake_up_interruptible(&vcpu->wq);
3290 ++vcpu->stat.halt_wakeup;
3292 if (vcpu->guest_mode)
3293 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);