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_UCODE_REV:
491 case MSR_IA32_UCODE_WRITE:
492 case 0x200 ... 0x2ff: /* MTRRs */
494 case MSR_IA32_APICBASE:
495 kvm_set_apic_base(vcpu, data);
497 case MSR_IA32_MISC_ENABLE:
498 vcpu->arch.ia32_misc_enable_msr = data;
501 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
506 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
510 * Reads an msr value (of 'msr_index') into 'pdata'.
511 * Returns 0 on success, non-0 otherwise.
512 * Assumes vcpu_load() was already called.
514 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
516 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
519 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
524 case 0xc0010010: /* SYSCFG */
525 case 0xc0010015: /* HWCR */
526 case MSR_IA32_PLATFORM_ID:
527 case MSR_IA32_P5_MC_ADDR:
528 case MSR_IA32_P5_MC_TYPE:
529 case MSR_IA32_MC0_CTL:
530 case MSR_IA32_MCG_STATUS:
531 case MSR_IA32_MCG_CAP:
532 case MSR_IA32_MC0_MISC:
533 case MSR_IA32_MC0_MISC+4:
534 case MSR_IA32_MC0_MISC+8:
535 case MSR_IA32_MC0_MISC+12:
536 case MSR_IA32_MC0_MISC+16:
537 case MSR_IA32_UCODE_REV:
538 case MSR_IA32_PERF_STATUS:
539 case MSR_IA32_EBL_CR_POWERON:
542 case 0x200 ... 0x2ff:
545 case 0xcd: /* fsb frequency */
548 case MSR_IA32_APICBASE:
549 data = kvm_get_apic_base(vcpu);
551 case MSR_IA32_MISC_ENABLE:
552 data = vcpu->arch.ia32_misc_enable_msr;
556 data = vcpu->arch.shadow_efer;
560 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
566 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
569 * Read or write a bunch of msrs. All parameters are kernel addresses.
571 * @return number of msrs set successfully.
573 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
574 struct kvm_msr_entry *entries,
575 int (*do_msr)(struct kvm_vcpu *vcpu,
576 unsigned index, u64 *data))
582 for (i = 0; i < msrs->nmsrs; ++i)
583 if (do_msr(vcpu, entries[i].index, &entries[i].data))
592 * Read or write a bunch of msrs. Parameters are user addresses.
594 * @return number of msrs set successfully.
596 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
597 int (*do_msr)(struct kvm_vcpu *vcpu,
598 unsigned index, u64 *data),
601 struct kvm_msrs msrs;
602 struct kvm_msr_entry *entries;
607 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
611 if (msrs.nmsrs >= MAX_IO_MSRS)
615 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
616 entries = vmalloc(size);
621 if (copy_from_user(entries, user_msrs->entries, size))
624 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
629 if (writeback && copy_to_user(user_msrs->entries, entries, size))
641 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
644 void decache_vcpus_on_cpu(int cpu)
647 struct kvm_vcpu *vcpu;
650 spin_lock(&kvm_lock);
651 list_for_each_entry(vm, &vm_list, vm_list)
652 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
657 * If the vcpu is locked, then it is running on some
658 * other cpu and therefore it is not cached on the
661 * If it's not locked, check the last cpu it executed
664 if (mutex_trylock(&vcpu->mutex)) {
665 if (vcpu->cpu == cpu) {
666 kvm_x86_ops->vcpu_decache(vcpu);
669 mutex_unlock(&vcpu->mutex);
672 spin_unlock(&kvm_lock);
675 int kvm_dev_ioctl_check_extension(long ext)
680 case KVM_CAP_IRQCHIP:
682 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
683 case KVM_CAP_USER_MEMORY:
684 case KVM_CAP_SET_TSS_ADDR:
685 case KVM_CAP_EXT_CPUID:
689 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
699 long kvm_arch_dev_ioctl(struct file *filp,
700 unsigned int ioctl, unsigned long arg)
702 void __user *argp = (void __user *)arg;
706 case KVM_GET_MSR_INDEX_LIST: {
707 struct kvm_msr_list __user *user_msr_list = argp;
708 struct kvm_msr_list msr_list;
712 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
715 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
716 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
719 if (n < num_msrs_to_save)
722 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
723 num_msrs_to_save * sizeof(u32)))
725 if (copy_to_user(user_msr_list->indices
726 + num_msrs_to_save * sizeof(u32),
728 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
733 case KVM_GET_SUPPORTED_CPUID: {
734 struct kvm_cpuid2 __user *cpuid_arg = argp;
735 struct kvm_cpuid2 cpuid;
738 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
740 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
746 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
758 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
760 kvm_x86_ops->vcpu_load(vcpu, cpu);
763 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
765 kvm_x86_ops->vcpu_put(vcpu);
766 kvm_put_guest_fpu(vcpu);
769 static int is_efer_nx(void)
773 rdmsrl(MSR_EFER, efer);
774 return efer & EFER_NX;
777 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
780 struct kvm_cpuid_entry2 *e, *entry;
783 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
784 e = &vcpu->arch.cpuid_entries[i];
785 if (e->function == 0x80000001) {
790 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
791 entry->edx &= ~(1 << 20);
792 printk(KERN_INFO "kvm: guest NX capability removed\n");
796 /* when an old userspace process fills a new kernel module */
797 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
798 struct kvm_cpuid *cpuid,
799 struct kvm_cpuid_entry __user *entries)
802 struct kvm_cpuid_entry *cpuid_entries;
805 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
808 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
812 if (copy_from_user(cpuid_entries, entries,
813 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
815 for (i = 0; i < cpuid->nent; i++) {
816 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
817 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
818 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
819 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
820 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
821 vcpu->arch.cpuid_entries[i].index = 0;
822 vcpu->arch.cpuid_entries[i].flags = 0;
823 vcpu->arch.cpuid_entries[i].padding[0] = 0;
824 vcpu->arch.cpuid_entries[i].padding[1] = 0;
825 vcpu->arch.cpuid_entries[i].padding[2] = 0;
827 vcpu->arch.cpuid_nent = cpuid->nent;
828 cpuid_fix_nx_cap(vcpu);
832 vfree(cpuid_entries);
837 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
838 struct kvm_cpuid2 *cpuid,
839 struct kvm_cpuid_entry2 __user *entries)
844 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
847 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
848 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
850 vcpu->arch.cpuid_nent = cpuid->nent;
857 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
858 struct kvm_cpuid2 *cpuid,
859 struct kvm_cpuid_entry2 __user *entries)
864 if (cpuid->nent < vcpu->arch.cpuid_nent)
867 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
868 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
873 cpuid->nent = vcpu->arch.cpuid_nent;
877 static inline u32 bit(int bitno)
879 return 1 << (bitno & 31);
882 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
885 entry->function = function;
886 entry->index = index;
887 cpuid_count(entry->function, entry->index,
888 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
892 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
893 u32 index, int *nent, int maxnent)
895 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
896 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
897 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
898 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
899 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
900 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
901 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
902 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
903 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
904 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
905 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
906 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
907 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
908 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
909 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
910 bit(X86_FEATURE_PGE) |
911 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
912 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
913 bit(X86_FEATURE_SYSCALL) |
914 (bit(X86_FEATURE_NX) && is_efer_nx()) |
916 bit(X86_FEATURE_LM) |
918 bit(X86_FEATURE_MMXEXT) |
919 bit(X86_FEATURE_3DNOWEXT) |
920 bit(X86_FEATURE_3DNOW);
921 const u32 kvm_supported_word3_x86_features =
922 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
923 const u32 kvm_supported_word6_x86_features =
924 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
926 /* all func 2 cpuid_count() should be called on the same cpu */
928 do_cpuid_1_ent(entry, function, index);
933 entry->eax = min(entry->eax, (u32)0xb);
936 entry->edx &= kvm_supported_word0_x86_features;
937 entry->ecx &= kvm_supported_word3_x86_features;
939 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
940 * may return different values. This forces us to get_cpu() before
941 * issuing the first command, and also to emulate this annoying behavior
942 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
944 int t, times = entry->eax & 0xff;
946 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
947 for (t = 1; t < times && *nent < maxnent; ++t) {
948 do_cpuid_1_ent(&entry[t], function, 0);
949 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
954 /* function 4 and 0xb have additional index. */
956 int index, cache_type;
958 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
959 /* read more entries until cache_type is zero */
960 for (index = 1; *nent < maxnent; ++index) {
961 cache_type = entry[index - 1].eax & 0x1f;
964 do_cpuid_1_ent(&entry[index], function, index);
965 entry[index].flags |=
966 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
972 int index, level_type;
974 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
975 /* read more entries until level_type is zero */
976 for (index = 1; *nent < maxnent; ++index) {
977 level_type = entry[index - 1].ecx & 0xff;
980 do_cpuid_1_ent(&entry[index], function, index);
981 entry[index].flags |=
982 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
988 entry->eax = min(entry->eax, 0x8000001a);
991 entry->edx &= kvm_supported_word1_x86_features;
992 entry->ecx &= kvm_supported_word6_x86_features;
998 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
999 struct kvm_cpuid_entry2 __user *entries)
1001 struct kvm_cpuid_entry2 *cpuid_entries;
1002 int limit, nent = 0, r = -E2BIG;
1005 if (cpuid->nent < 1)
1008 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1012 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1013 limit = cpuid_entries[0].eax;
1014 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1015 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1016 &nent, cpuid->nent);
1018 if (nent >= cpuid->nent)
1021 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1022 limit = cpuid_entries[nent - 1].eax;
1023 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1024 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1025 &nent, cpuid->nent);
1027 if (copy_to_user(entries, cpuid_entries,
1028 nent * sizeof(struct kvm_cpuid_entry2)))
1034 vfree(cpuid_entries);
1039 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1040 struct kvm_lapic_state *s)
1043 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1049 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1050 struct kvm_lapic_state *s)
1053 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1054 kvm_apic_post_state_restore(vcpu);
1060 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1061 struct kvm_interrupt *irq)
1063 if (irq->irq < 0 || irq->irq >= 256)
1065 if (irqchip_in_kernel(vcpu->kvm))
1069 set_bit(irq->irq, vcpu->arch.irq_pending);
1070 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1077 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1078 struct kvm_tpr_access_ctl *tac)
1082 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1086 long kvm_arch_vcpu_ioctl(struct file *filp,
1087 unsigned int ioctl, unsigned long arg)
1089 struct kvm_vcpu *vcpu = filp->private_data;
1090 void __user *argp = (void __user *)arg;
1094 case KVM_GET_LAPIC: {
1095 struct kvm_lapic_state lapic;
1097 memset(&lapic, 0, sizeof lapic);
1098 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1102 if (copy_to_user(argp, &lapic, sizeof lapic))
1107 case KVM_SET_LAPIC: {
1108 struct kvm_lapic_state lapic;
1111 if (copy_from_user(&lapic, argp, sizeof lapic))
1113 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1119 case KVM_INTERRUPT: {
1120 struct kvm_interrupt irq;
1123 if (copy_from_user(&irq, argp, sizeof irq))
1125 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1131 case KVM_SET_CPUID: {
1132 struct kvm_cpuid __user *cpuid_arg = argp;
1133 struct kvm_cpuid cpuid;
1136 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1138 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1143 case KVM_SET_CPUID2: {
1144 struct kvm_cpuid2 __user *cpuid_arg = argp;
1145 struct kvm_cpuid2 cpuid;
1148 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1150 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1151 cpuid_arg->entries);
1156 case KVM_GET_CPUID2: {
1157 struct kvm_cpuid2 __user *cpuid_arg = argp;
1158 struct kvm_cpuid2 cpuid;
1161 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1163 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1164 cpuid_arg->entries);
1168 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1174 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1177 r = msr_io(vcpu, argp, do_set_msr, 0);
1179 case KVM_TPR_ACCESS_REPORTING: {
1180 struct kvm_tpr_access_ctl tac;
1183 if (copy_from_user(&tac, argp, sizeof tac))
1185 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1189 if (copy_to_user(argp, &tac, sizeof tac))
1194 case KVM_SET_VAPIC_ADDR: {
1195 struct kvm_vapic_addr va;
1198 if (!irqchip_in_kernel(vcpu->kvm))
1201 if (copy_from_user(&va, argp, sizeof va))
1204 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1214 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1218 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1220 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1224 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1225 u32 kvm_nr_mmu_pages)
1227 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1230 down_write(¤t->mm->mmap_sem);
1232 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1233 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1235 up_write(¤t->mm->mmap_sem);
1239 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1241 return kvm->arch.n_alloc_mmu_pages;
1244 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1247 struct kvm_mem_alias *alias;
1249 for (i = 0; i < kvm->arch.naliases; ++i) {
1250 alias = &kvm->arch.aliases[i];
1251 if (gfn >= alias->base_gfn
1252 && gfn < alias->base_gfn + alias->npages)
1253 return alias->target_gfn + gfn - alias->base_gfn;
1259 * Set a new alias region. Aliases map a portion of physical memory into
1260 * another portion. This is useful for memory windows, for example the PC
1263 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1264 struct kvm_memory_alias *alias)
1267 struct kvm_mem_alias *p;
1270 /* General sanity checks */
1271 if (alias->memory_size & (PAGE_SIZE - 1))
1273 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1275 if (alias->slot >= KVM_ALIAS_SLOTS)
1277 if (alias->guest_phys_addr + alias->memory_size
1278 < alias->guest_phys_addr)
1280 if (alias->target_phys_addr + alias->memory_size
1281 < alias->target_phys_addr)
1284 down_write(¤t->mm->mmap_sem);
1286 p = &kvm->arch.aliases[alias->slot];
1287 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1288 p->npages = alias->memory_size >> PAGE_SHIFT;
1289 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1291 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1292 if (kvm->arch.aliases[n - 1].npages)
1294 kvm->arch.naliases = n;
1296 kvm_mmu_zap_all(kvm);
1298 up_write(¤t->mm->mmap_sem);
1306 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1311 switch (chip->chip_id) {
1312 case KVM_IRQCHIP_PIC_MASTER:
1313 memcpy(&chip->chip.pic,
1314 &pic_irqchip(kvm)->pics[0],
1315 sizeof(struct kvm_pic_state));
1317 case KVM_IRQCHIP_PIC_SLAVE:
1318 memcpy(&chip->chip.pic,
1319 &pic_irqchip(kvm)->pics[1],
1320 sizeof(struct kvm_pic_state));
1322 case KVM_IRQCHIP_IOAPIC:
1323 memcpy(&chip->chip.ioapic,
1324 ioapic_irqchip(kvm),
1325 sizeof(struct kvm_ioapic_state));
1334 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1339 switch (chip->chip_id) {
1340 case KVM_IRQCHIP_PIC_MASTER:
1341 memcpy(&pic_irqchip(kvm)->pics[0],
1343 sizeof(struct kvm_pic_state));
1345 case KVM_IRQCHIP_PIC_SLAVE:
1346 memcpy(&pic_irqchip(kvm)->pics[1],
1348 sizeof(struct kvm_pic_state));
1350 case KVM_IRQCHIP_IOAPIC:
1351 memcpy(ioapic_irqchip(kvm),
1353 sizeof(struct kvm_ioapic_state));
1359 kvm_pic_update_irq(pic_irqchip(kvm));
1364 * Get (and clear) the dirty memory log for a memory slot.
1366 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1367 struct kvm_dirty_log *log)
1371 struct kvm_memory_slot *memslot;
1374 down_write(¤t->mm->mmap_sem);
1376 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1380 /* If nothing is dirty, don't bother messing with page tables. */
1382 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1383 kvm_flush_remote_tlbs(kvm);
1384 memslot = &kvm->memslots[log->slot];
1385 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1386 memset(memslot->dirty_bitmap, 0, n);
1390 up_write(¤t->mm->mmap_sem);
1394 long kvm_arch_vm_ioctl(struct file *filp,
1395 unsigned int ioctl, unsigned long arg)
1397 struct kvm *kvm = filp->private_data;
1398 void __user *argp = (void __user *)arg;
1402 case KVM_SET_TSS_ADDR:
1403 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1407 case KVM_SET_MEMORY_REGION: {
1408 struct kvm_memory_region kvm_mem;
1409 struct kvm_userspace_memory_region kvm_userspace_mem;
1412 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1414 kvm_userspace_mem.slot = kvm_mem.slot;
1415 kvm_userspace_mem.flags = kvm_mem.flags;
1416 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1417 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1418 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1423 case KVM_SET_NR_MMU_PAGES:
1424 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1428 case KVM_GET_NR_MMU_PAGES:
1429 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1431 case KVM_SET_MEMORY_ALIAS: {
1432 struct kvm_memory_alias alias;
1435 if (copy_from_user(&alias, argp, sizeof alias))
1437 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1442 case KVM_CREATE_IRQCHIP:
1444 kvm->arch.vpic = kvm_create_pic(kvm);
1445 if (kvm->arch.vpic) {
1446 r = kvm_ioapic_init(kvm);
1448 kfree(kvm->arch.vpic);
1449 kvm->arch.vpic = NULL;
1455 case KVM_IRQ_LINE: {
1456 struct kvm_irq_level irq_event;
1459 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1461 if (irqchip_in_kernel(kvm)) {
1462 mutex_lock(&kvm->lock);
1463 if (irq_event.irq < 16)
1464 kvm_pic_set_irq(pic_irqchip(kvm),
1467 kvm_ioapic_set_irq(kvm->arch.vioapic,
1470 mutex_unlock(&kvm->lock);
1475 case KVM_GET_IRQCHIP: {
1476 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1477 struct kvm_irqchip chip;
1480 if (copy_from_user(&chip, argp, sizeof chip))
1483 if (!irqchip_in_kernel(kvm))
1485 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1489 if (copy_to_user(argp, &chip, sizeof chip))
1494 case KVM_SET_IRQCHIP: {
1495 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1496 struct kvm_irqchip chip;
1499 if (copy_from_user(&chip, argp, sizeof chip))
1502 if (!irqchip_in_kernel(kvm))
1504 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1517 static void kvm_init_msr_list(void)
1522 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1523 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1526 msrs_to_save[j] = msrs_to_save[i];
1529 num_msrs_to_save = j;
1533 * Only apic need an MMIO device hook, so shortcut now..
1535 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1538 struct kvm_io_device *dev;
1540 if (vcpu->arch.apic) {
1541 dev = &vcpu->arch.apic->dev;
1542 if (dev->in_range(dev, addr))
1549 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1552 struct kvm_io_device *dev;
1554 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1556 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1560 int emulator_read_std(unsigned long addr,
1563 struct kvm_vcpu *vcpu)
1566 int r = X86EMUL_CONTINUE;
1568 down_read(¤t->mm->mmap_sem);
1570 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1571 unsigned offset = addr & (PAGE_SIZE-1);
1572 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1575 if (gpa == UNMAPPED_GVA) {
1576 r = X86EMUL_PROPAGATE_FAULT;
1579 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1581 r = X86EMUL_UNHANDLEABLE;
1590 up_read(¤t->mm->mmap_sem);
1593 EXPORT_SYMBOL_GPL(emulator_read_std);
1595 static int emulator_read_emulated(unsigned long addr,
1598 struct kvm_vcpu *vcpu)
1600 struct kvm_io_device *mmio_dev;
1603 if (vcpu->mmio_read_completed) {
1604 memcpy(val, vcpu->mmio_data, bytes);
1605 vcpu->mmio_read_completed = 0;
1606 return X86EMUL_CONTINUE;
1609 down_read(¤t->mm->mmap_sem);
1610 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1611 up_read(¤t->mm->mmap_sem);
1613 /* For APIC access vmexit */
1614 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1617 if (emulator_read_std(addr, val, bytes, vcpu)
1618 == X86EMUL_CONTINUE)
1619 return X86EMUL_CONTINUE;
1620 if (gpa == UNMAPPED_GVA)
1621 return X86EMUL_PROPAGATE_FAULT;
1625 * Is this MMIO handled locally?
1627 mutex_lock(&vcpu->kvm->lock);
1628 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1630 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1631 mutex_unlock(&vcpu->kvm->lock);
1632 return X86EMUL_CONTINUE;
1634 mutex_unlock(&vcpu->kvm->lock);
1636 vcpu->mmio_needed = 1;
1637 vcpu->mmio_phys_addr = gpa;
1638 vcpu->mmio_size = bytes;
1639 vcpu->mmio_is_write = 0;
1641 return X86EMUL_UNHANDLEABLE;
1644 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1645 const void *val, int bytes)
1649 down_read(¤t->mm->mmap_sem);
1650 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1652 up_read(¤t->mm->mmap_sem);
1655 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1656 up_read(¤t->mm->mmap_sem);
1660 static int emulator_write_emulated_onepage(unsigned long addr,
1663 struct kvm_vcpu *vcpu)
1665 struct kvm_io_device *mmio_dev;
1668 down_read(¤t->mm->mmap_sem);
1669 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1670 up_read(¤t->mm->mmap_sem);
1672 if (gpa == UNMAPPED_GVA) {
1673 kvm_inject_page_fault(vcpu, addr, 2);
1674 return X86EMUL_PROPAGATE_FAULT;
1677 /* For APIC access vmexit */
1678 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1681 if (emulator_write_phys(vcpu, gpa, val, bytes))
1682 return X86EMUL_CONTINUE;
1686 * Is this MMIO handled locally?
1688 mutex_lock(&vcpu->kvm->lock);
1689 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1691 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1692 mutex_unlock(&vcpu->kvm->lock);
1693 return X86EMUL_CONTINUE;
1695 mutex_unlock(&vcpu->kvm->lock);
1697 vcpu->mmio_needed = 1;
1698 vcpu->mmio_phys_addr = gpa;
1699 vcpu->mmio_size = bytes;
1700 vcpu->mmio_is_write = 1;
1701 memcpy(vcpu->mmio_data, val, bytes);
1703 return X86EMUL_CONTINUE;
1706 int emulator_write_emulated(unsigned long addr,
1709 struct kvm_vcpu *vcpu)
1711 /* Crossing a page boundary? */
1712 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1715 now = -addr & ~PAGE_MASK;
1716 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1717 if (rc != X86EMUL_CONTINUE)
1723 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1725 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1727 static int emulator_cmpxchg_emulated(unsigned long addr,
1731 struct kvm_vcpu *vcpu)
1733 static int reported;
1737 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1739 #ifndef CONFIG_X86_64
1740 /* guests cmpxchg8b have to be emulated atomically */
1747 down_read(¤t->mm->mmap_sem);
1748 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1750 if (gpa == UNMAPPED_GVA ||
1751 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1754 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1758 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1759 kaddr = kmap_atomic(page, KM_USER0);
1760 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1761 kunmap_atomic(kaddr, KM_USER0);
1762 kvm_release_page_dirty(page);
1764 up_read(¤t->mm->mmap_sem);
1768 return emulator_write_emulated(addr, new, bytes, vcpu);
1771 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1773 return kvm_x86_ops->get_segment_base(vcpu, seg);
1776 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1778 return X86EMUL_CONTINUE;
1781 int emulate_clts(struct kvm_vcpu *vcpu)
1783 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
1784 return X86EMUL_CONTINUE;
1787 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1789 struct kvm_vcpu *vcpu = ctxt->vcpu;
1793 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1794 return X86EMUL_CONTINUE;
1796 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1797 return X86EMUL_UNHANDLEABLE;
1801 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1803 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1806 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1808 /* FIXME: better handling */
1809 return X86EMUL_UNHANDLEABLE;
1811 return X86EMUL_CONTINUE;
1814 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1816 static int reported;
1818 unsigned long rip = vcpu->arch.rip;
1819 unsigned long rip_linear;
1821 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1826 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1828 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1829 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1832 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1834 struct x86_emulate_ops emulate_ops = {
1835 .read_std = emulator_read_std,
1836 .read_emulated = emulator_read_emulated,
1837 .write_emulated = emulator_write_emulated,
1838 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1841 int emulate_instruction(struct kvm_vcpu *vcpu,
1842 struct kvm_run *run,
1848 struct decode_cache *c;
1850 vcpu->arch.mmio_fault_cr2 = cr2;
1851 kvm_x86_ops->cache_regs(vcpu);
1853 vcpu->mmio_is_write = 0;
1854 vcpu->arch.pio.string = 0;
1856 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
1858 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1860 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1861 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1862 vcpu->arch.emulate_ctxt.mode =
1863 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
1864 ? X86EMUL_MODE_REAL : cs_l
1865 ? X86EMUL_MODE_PROT64 : cs_db
1866 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1868 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1869 vcpu->arch.emulate_ctxt.cs_base = 0;
1870 vcpu->arch.emulate_ctxt.ds_base = 0;
1871 vcpu->arch.emulate_ctxt.es_base = 0;
1872 vcpu->arch.emulate_ctxt.ss_base = 0;
1874 vcpu->arch.emulate_ctxt.cs_base =
1875 get_segment_base(vcpu, VCPU_SREG_CS);
1876 vcpu->arch.emulate_ctxt.ds_base =
1877 get_segment_base(vcpu, VCPU_SREG_DS);
1878 vcpu->arch.emulate_ctxt.es_base =
1879 get_segment_base(vcpu, VCPU_SREG_ES);
1880 vcpu->arch.emulate_ctxt.ss_base =
1881 get_segment_base(vcpu, VCPU_SREG_SS);
1884 vcpu->arch.emulate_ctxt.gs_base =
1885 get_segment_base(vcpu, VCPU_SREG_GS);
1886 vcpu->arch.emulate_ctxt.fs_base =
1887 get_segment_base(vcpu, VCPU_SREG_FS);
1889 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1891 /* Reject the instructions other than VMCALL/VMMCALL when
1892 * try to emulate invalid opcode */
1893 c = &vcpu->arch.emulate_ctxt.decode;
1894 if ((emulation_type & EMULTYPE_TRAP_UD) &&
1895 (!(c->twobyte && c->b == 0x01 &&
1896 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
1897 c->modrm_mod == 3 && c->modrm_rm == 1)))
1898 return EMULATE_FAIL;
1900 ++vcpu->stat.insn_emulation;
1902 ++vcpu->stat.insn_emulation_fail;
1903 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1904 return EMULATE_DONE;
1905 return EMULATE_FAIL;
1909 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1911 if (vcpu->arch.pio.string)
1912 return EMULATE_DO_MMIO;
1914 if ((r || vcpu->mmio_is_write) && run) {
1915 run->exit_reason = KVM_EXIT_MMIO;
1916 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1917 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1918 run->mmio.len = vcpu->mmio_size;
1919 run->mmio.is_write = vcpu->mmio_is_write;
1923 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1924 return EMULATE_DONE;
1925 if (!vcpu->mmio_needed) {
1926 kvm_report_emulation_failure(vcpu, "mmio");
1927 return EMULATE_FAIL;
1929 return EMULATE_DO_MMIO;
1932 kvm_x86_ops->decache_regs(vcpu);
1933 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
1935 if (vcpu->mmio_is_write) {
1936 vcpu->mmio_needed = 0;
1937 return EMULATE_DO_MMIO;
1940 return EMULATE_DONE;
1942 EXPORT_SYMBOL_GPL(emulate_instruction);
1944 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1948 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
1949 if (vcpu->arch.pio.guest_pages[i]) {
1950 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
1951 vcpu->arch.pio.guest_pages[i] = NULL;
1955 static int pio_copy_data(struct kvm_vcpu *vcpu)
1957 void *p = vcpu->arch.pio_data;
1960 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
1962 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1965 free_pio_guest_pages(vcpu);
1968 q += vcpu->arch.pio.guest_page_offset;
1969 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
1970 if (vcpu->arch.pio.in)
1971 memcpy(q, p, bytes);
1973 memcpy(p, q, bytes);
1974 q -= vcpu->arch.pio.guest_page_offset;
1976 free_pio_guest_pages(vcpu);
1980 int complete_pio(struct kvm_vcpu *vcpu)
1982 struct kvm_pio_request *io = &vcpu->arch.pio;
1986 kvm_x86_ops->cache_regs(vcpu);
1990 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
1994 r = pio_copy_data(vcpu);
1996 kvm_x86_ops->cache_regs(vcpu);
2003 delta *= io->cur_count;
2005 * The size of the register should really depend on
2006 * current address size.
2008 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2014 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2016 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2019 kvm_x86_ops->decache_regs(vcpu);
2021 io->count -= io->cur_count;
2027 static void kernel_pio(struct kvm_io_device *pio_dev,
2028 struct kvm_vcpu *vcpu,
2031 /* TODO: String I/O for in kernel device */
2033 mutex_lock(&vcpu->kvm->lock);
2034 if (vcpu->arch.pio.in)
2035 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2036 vcpu->arch.pio.size,
2039 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2040 vcpu->arch.pio.size,
2042 mutex_unlock(&vcpu->kvm->lock);
2045 static void pio_string_write(struct kvm_io_device *pio_dev,
2046 struct kvm_vcpu *vcpu)
2048 struct kvm_pio_request *io = &vcpu->arch.pio;
2049 void *pd = vcpu->arch.pio_data;
2052 mutex_lock(&vcpu->kvm->lock);
2053 for (i = 0; i < io->cur_count; i++) {
2054 kvm_iodevice_write(pio_dev, io->port,
2059 mutex_unlock(&vcpu->kvm->lock);
2062 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2065 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2068 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2069 int size, unsigned port)
2071 struct kvm_io_device *pio_dev;
2073 vcpu->run->exit_reason = KVM_EXIT_IO;
2074 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2075 vcpu->run->io.size = vcpu->arch.pio.size = size;
2076 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2077 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2078 vcpu->run->io.port = vcpu->arch.pio.port = port;
2079 vcpu->arch.pio.in = in;
2080 vcpu->arch.pio.string = 0;
2081 vcpu->arch.pio.down = 0;
2082 vcpu->arch.pio.guest_page_offset = 0;
2083 vcpu->arch.pio.rep = 0;
2085 kvm_x86_ops->cache_regs(vcpu);
2086 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2087 kvm_x86_ops->decache_regs(vcpu);
2089 kvm_x86_ops->skip_emulated_instruction(vcpu);
2091 pio_dev = vcpu_find_pio_dev(vcpu, port);
2093 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2099 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2101 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2102 int size, unsigned long count, int down,
2103 gva_t address, int rep, unsigned port)
2105 unsigned now, in_page;
2109 struct kvm_io_device *pio_dev;
2111 vcpu->run->exit_reason = KVM_EXIT_IO;
2112 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2113 vcpu->run->io.size = vcpu->arch.pio.size = size;
2114 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2115 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2116 vcpu->run->io.port = vcpu->arch.pio.port = port;
2117 vcpu->arch.pio.in = in;
2118 vcpu->arch.pio.string = 1;
2119 vcpu->arch.pio.down = down;
2120 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2121 vcpu->arch.pio.rep = rep;
2124 kvm_x86_ops->skip_emulated_instruction(vcpu);
2129 in_page = PAGE_SIZE - offset_in_page(address);
2131 in_page = offset_in_page(address) + size;
2132 now = min(count, (unsigned long)in_page / size);
2135 * String I/O straddles page boundary. Pin two guest pages
2136 * so that we satisfy atomicity constraints. Do just one
2137 * transaction to avoid complexity.
2144 * String I/O in reverse. Yuck. Kill the guest, fix later.
2146 pr_unimpl(vcpu, "guest string pio down\n");
2147 kvm_inject_gp(vcpu, 0);
2150 vcpu->run->io.count = now;
2151 vcpu->arch.pio.cur_count = now;
2153 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2154 kvm_x86_ops->skip_emulated_instruction(vcpu);
2156 for (i = 0; i < nr_pages; ++i) {
2157 down_read(¤t->mm->mmap_sem);
2158 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2159 vcpu->arch.pio.guest_pages[i] = page;
2160 up_read(¤t->mm->mmap_sem);
2162 kvm_inject_gp(vcpu, 0);
2163 free_pio_guest_pages(vcpu);
2168 pio_dev = vcpu_find_pio_dev(vcpu, port);
2169 if (!vcpu->arch.pio.in) {
2170 /* string PIO write */
2171 ret = pio_copy_data(vcpu);
2172 if (ret >= 0 && pio_dev) {
2173 pio_string_write(pio_dev, vcpu);
2175 if (vcpu->arch.pio.count == 0)
2179 pr_unimpl(vcpu, "no string pio read support yet, "
2180 "port %x size %d count %ld\n",
2185 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2187 int kvm_arch_init(void *opaque)
2190 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2193 printk(KERN_ERR "kvm: already loaded the other module\n");
2198 if (!ops->cpu_has_kvm_support()) {
2199 printk(KERN_ERR "kvm: no hardware support\n");
2203 if (ops->disabled_by_bios()) {
2204 printk(KERN_ERR "kvm: disabled by bios\n");
2209 r = kvm_mmu_module_init();
2213 kvm_init_msr_list();
2216 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2223 void kvm_arch_exit(void)
2226 kvm_mmu_module_exit();
2229 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2231 ++vcpu->stat.halt_exits;
2232 if (irqchip_in_kernel(vcpu->kvm)) {
2233 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
2234 kvm_vcpu_block(vcpu);
2235 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
2239 vcpu->run->exit_reason = KVM_EXIT_HLT;
2243 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2245 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2247 unsigned long nr, a0, a1, a2, a3, ret;
2249 kvm_x86_ops->cache_regs(vcpu);
2251 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2252 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2253 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2254 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2255 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2257 if (!is_long_mode(vcpu)) {
2266 case KVM_HC_VAPIC_POLL_IRQ:
2273 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2274 kvm_x86_ops->decache_regs(vcpu);
2277 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2279 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2281 char instruction[3];
2286 * Blow out the MMU to ensure that no other VCPU has an active mapping
2287 * to ensure that the updated hypercall appears atomically across all
2290 kvm_mmu_zap_all(vcpu->kvm);
2292 kvm_x86_ops->cache_regs(vcpu);
2293 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2294 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2295 != X86EMUL_CONTINUE)
2301 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2303 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2306 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2308 struct descriptor_table dt = { limit, base };
2310 kvm_x86_ops->set_gdt(vcpu, &dt);
2313 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2315 struct descriptor_table dt = { limit, base };
2317 kvm_x86_ops->set_idt(vcpu, &dt);
2320 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2321 unsigned long *rflags)
2324 *rflags = kvm_x86_ops->get_rflags(vcpu);
2327 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2329 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2332 return vcpu->arch.cr0;
2334 return vcpu->arch.cr2;
2336 return vcpu->arch.cr3;
2338 return vcpu->arch.cr4;
2340 return get_cr8(vcpu);
2342 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2347 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2348 unsigned long *rflags)
2352 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2353 *rflags = kvm_x86_ops->get_rflags(vcpu);
2356 vcpu->arch.cr2 = val;
2362 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2365 set_cr8(vcpu, val & 0xfUL);
2368 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2372 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2374 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2375 int j, nent = vcpu->arch.cpuid_nent;
2377 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2378 /* when no next entry is found, the current entry[i] is reselected */
2379 for (j = i + 1; j == i; j = (j + 1) % nent) {
2380 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2381 if (ej->function == e->function) {
2382 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2386 return 0; /* silence gcc, even though control never reaches here */
2389 /* find an entry with matching function, matching index (if needed), and that
2390 * should be read next (if it's stateful) */
2391 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2392 u32 function, u32 index)
2394 if (e->function != function)
2396 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2398 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2399 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2404 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2407 u32 function, index;
2408 struct kvm_cpuid_entry2 *e, *best;
2410 kvm_x86_ops->cache_regs(vcpu);
2411 function = vcpu->arch.regs[VCPU_REGS_RAX];
2412 index = vcpu->arch.regs[VCPU_REGS_RCX];
2413 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2414 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2415 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2416 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2418 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2419 e = &vcpu->arch.cpuid_entries[i];
2420 if (is_matching_cpuid_entry(e, function, index)) {
2421 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2422 move_to_next_stateful_cpuid_entry(vcpu, i);
2427 * Both basic or both extended?
2429 if (((e->function ^ function) & 0x80000000) == 0)
2430 if (!best || e->function > best->function)
2434 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2435 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2436 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2437 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2439 kvm_x86_ops->decache_regs(vcpu);
2440 kvm_x86_ops->skip_emulated_instruction(vcpu);
2442 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2445 * Check if userspace requested an interrupt window, and that the
2446 * interrupt window is open.
2448 * No need to exit to userspace if we already have an interrupt queued.
2450 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2451 struct kvm_run *kvm_run)
2453 return (!vcpu->arch.irq_summary &&
2454 kvm_run->request_interrupt_window &&
2455 vcpu->arch.interrupt_window_open &&
2456 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2459 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2460 struct kvm_run *kvm_run)
2462 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2463 kvm_run->cr8 = get_cr8(vcpu);
2464 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2465 if (irqchip_in_kernel(vcpu->kvm))
2466 kvm_run->ready_for_interrupt_injection = 1;
2468 kvm_run->ready_for_interrupt_injection =
2469 (vcpu->arch.interrupt_window_open &&
2470 vcpu->arch.irq_summary == 0);
2473 static void vapic_enter(struct kvm_vcpu *vcpu)
2475 struct kvm_lapic *apic = vcpu->arch.apic;
2478 if (!apic || !apic->vapic_addr)
2481 down_read(¤t->mm->mmap_sem);
2482 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2483 vcpu->arch.apic->vapic_page = page;
2484 up_read(¤t->mm->mmap_sem);
2487 static void vapic_exit(struct kvm_vcpu *vcpu)
2489 struct kvm_lapic *apic = vcpu->arch.apic;
2491 if (!apic || !apic->vapic_addr)
2494 kvm_release_page_dirty(apic->vapic_page);
2495 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2498 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2502 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2503 pr_debug("vcpu %d received sipi with vector # %x\n",
2504 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2505 kvm_lapic_reset(vcpu);
2506 r = kvm_x86_ops->vcpu_reset(vcpu);
2509 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
2515 if (vcpu->guest_debug.enabled)
2516 kvm_x86_ops->guest_debug_pre(vcpu);
2519 r = kvm_mmu_reload(vcpu);
2523 if (vcpu->requests) {
2524 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2525 __kvm_migrate_apic_timer(vcpu);
2526 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2528 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2534 kvm_inject_pending_timer_irqs(vcpu);
2538 kvm_x86_ops->prepare_guest_switch(vcpu);
2539 kvm_load_guest_fpu(vcpu);
2541 local_irq_disable();
2543 if (need_resched()) {
2550 if (signal_pending(current)) {
2554 kvm_run->exit_reason = KVM_EXIT_INTR;
2555 ++vcpu->stat.signal_exits;
2559 if (vcpu->arch.exception.pending)
2560 __queue_exception(vcpu);
2561 else if (irqchip_in_kernel(vcpu->kvm))
2562 kvm_x86_ops->inject_pending_irq(vcpu);
2564 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2566 kvm_lapic_sync_to_vapic(vcpu);
2568 vcpu->guest_mode = 1;
2572 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2573 kvm_x86_ops->tlb_flush(vcpu);
2575 kvm_x86_ops->run(vcpu, kvm_run);
2577 vcpu->guest_mode = 0;
2583 * We must have an instruction between local_irq_enable() and
2584 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2585 * the interrupt shadow. The stat.exits increment will do nicely.
2586 * But we need to prevent reordering, hence this barrier():
2595 * Profile KVM exit RIPs:
2597 if (unlikely(prof_on == KVM_PROFILING)) {
2598 kvm_x86_ops->cache_regs(vcpu);
2599 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2602 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2603 vcpu->arch.exception.pending = false;
2605 kvm_lapic_sync_from_vapic(vcpu);
2607 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2610 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2612 kvm_run->exit_reason = KVM_EXIT_INTR;
2613 ++vcpu->stat.request_irq_exits;
2616 if (!need_resched())
2626 post_kvm_run_save(vcpu, kvm_run);
2633 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2640 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2641 kvm_vcpu_block(vcpu);
2646 if (vcpu->sigset_active)
2647 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2649 /* re-sync apic's tpr */
2650 if (!irqchip_in_kernel(vcpu->kvm))
2651 set_cr8(vcpu, kvm_run->cr8);
2653 if (vcpu->arch.pio.cur_count) {
2654 r = complete_pio(vcpu);
2658 #if CONFIG_HAS_IOMEM
2659 if (vcpu->mmio_needed) {
2660 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2661 vcpu->mmio_read_completed = 1;
2662 vcpu->mmio_needed = 0;
2663 r = emulate_instruction(vcpu, kvm_run,
2664 vcpu->arch.mmio_fault_cr2, 0,
2665 EMULTYPE_NO_DECODE);
2666 if (r == EMULATE_DO_MMIO) {
2668 * Read-modify-write. Back to userspace.
2675 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2676 kvm_x86_ops->cache_regs(vcpu);
2677 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2678 kvm_x86_ops->decache_regs(vcpu);
2681 r = __vcpu_run(vcpu, kvm_run);
2684 if (vcpu->sigset_active)
2685 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2691 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2695 kvm_x86_ops->cache_regs(vcpu);
2697 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2698 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2699 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2700 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2701 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2702 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2703 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2704 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
2705 #ifdef CONFIG_X86_64
2706 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2707 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2708 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2709 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2710 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2711 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2712 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2713 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
2716 regs->rip = vcpu->arch.rip;
2717 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2720 * Don't leak debug flags in case they were set for guest debugging
2722 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2723 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2730 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2734 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2735 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2736 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2737 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2738 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2739 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2740 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2741 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
2742 #ifdef CONFIG_X86_64
2743 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2744 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2745 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2746 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2747 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2748 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2749 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2750 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
2753 vcpu->arch.rip = regs->rip;
2754 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2756 kvm_x86_ops->decache_regs(vcpu);
2763 static void get_segment(struct kvm_vcpu *vcpu,
2764 struct kvm_segment *var, int seg)
2766 return kvm_x86_ops->get_segment(vcpu, var, seg);
2769 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2771 struct kvm_segment cs;
2773 get_segment(vcpu, &cs, VCPU_SREG_CS);
2777 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2779 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2780 struct kvm_sregs *sregs)
2782 struct descriptor_table dt;
2787 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2788 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2789 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2790 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2791 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2792 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2794 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2795 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2797 kvm_x86_ops->get_idt(vcpu, &dt);
2798 sregs->idt.limit = dt.limit;
2799 sregs->idt.base = dt.base;
2800 kvm_x86_ops->get_gdt(vcpu, &dt);
2801 sregs->gdt.limit = dt.limit;
2802 sregs->gdt.base = dt.base;
2804 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2805 sregs->cr0 = vcpu->arch.cr0;
2806 sregs->cr2 = vcpu->arch.cr2;
2807 sregs->cr3 = vcpu->arch.cr3;
2808 sregs->cr4 = vcpu->arch.cr4;
2809 sregs->cr8 = get_cr8(vcpu);
2810 sregs->efer = vcpu->arch.shadow_efer;
2811 sregs->apic_base = kvm_get_apic_base(vcpu);
2813 if (irqchip_in_kernel(vcpu->kvm)) {
2814 memset(sregs->interrupt_bitmap, 0,
2815 sizeof sregs->interrupt_bitmap);
2816 pending_vec = kvm_x86_ops->get_irq(vcpu);
2817 if (pending_vec >= 0)
2818 set_bit(pending_vec,
2819 (unsigned long *)sregs->interrupt_bitmap);
2821 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
2822 sizeof sregs->interrupt_bitmap);
2829 static void set_segment(struct kvm_vcpu *vcpu,
2830 struct kvm_segment *var, int seg)
2832 return kvm_x86_ops->set_segment(vcpu, var, seg);
2835 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2836 struct kvm_sregs *sregs)
2838 int mmu_reset_needed = 0;
2839 int i, pending_vec, max_bits;
2840 struct descriptor_table dt;
2844 dt.limit = sregs->idt.limit;
2845 dt.base = sregs->idt.base;
2846 kvm_x86_ops->set_idt(vcpu, &dt);
2847 dt.limit = sregs->gdt.limit;
2848 dt.base = sregs->gdt.base;
2849 kvm_x86_ops->set_gdt(vcpu, &dt);
2851 vcpu->arch.cr2 = sregs->cr2;
2852 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2853 vcpu->arch.cr3 = sregs->cr3;
2855 set_cr8(vcpu, sregs->cr8);
2857 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
2858 #ifdef CONFIG_X86_64
2859 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2861 kvm_set_apic_base(vcpu, sregs->apic_base);
2863 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2865 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
2866 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2867 vcpu->arch.cr0 = sregs->cr0;
2869 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
2870 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2871 if (!is_long_mode(vcpu) && is_pae(vcpu))
2872 load_pdptrs(vcpu, vcpu->arch.cr3);
2874 if (mmu_reset_needed)
2875 kvm_mmu_reset_context(vcpu);
2877 if (!irqchip_in_kernel(vcpu->kvm)) {
2878 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
2879 sizeof vcpu->arch.irq_pending);
2880 vcpu->arch.irq_summary = 0;
2881 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
2882 if (vcpu->arch.irq_pending[i])
2883 __set_bit(i, &vcpu->arch.irq_summary);
2885 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2886 pending_vec = find_first_bit(
2887 (const unsigned long *)sregs->interrupt_bitmap,
2889 /* Only pending external irq is handled here */
2890 if (pending_vec < max_bits) {
2891 kvm_x86_ops->set_irq(vcpu, pending_vec);
2892 pr_debug("Set back pending irq %d\n",
2897 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2898 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2899 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2900 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2901 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2902 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2904 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2905 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2912 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2913 struct kvm_debug_guest *dbg)
2919 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2927 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2928 * we have asm/x86/processor.h
2939 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2940 #ifdef CONFIG_X86_64
2941 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2943 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2948 * Translate a guest virtual address to a guest physical address.
2950 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2951 struct kvm_translation *tr)
2953 unsigned long vaddr = tr->linear_address;
2957 down_read(¤t->mm->mmap_sem);
2958 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
2959 up_read(¤t->mm->mmap_sem);
2960 tr->physical_address = gpa;
2961 tr->valid = gpa != UNMAPPED_GVA;
2969 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2971 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
2975 memcpy(fpu->fpr, fxsave->st_space, 128);
2976 fpu->fcw = fxsave->cwd;
2977 fpu->fsw = fxsave->swd;
2978 fpu->ftwx = fxsave->twd;
2979 fpu->last_opcode = fxsave->fop;
2980 fpu->last_ip = fxsave->rip;
2981 fpu->last_dp = fxsave->rdp;
2982 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2989 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2991 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
2995 memcpy(fxsave->st_space, fpu->fpr, 128);
2996 fxsave->cwd = fpu->fcw;
2997 fxsave->swd = fpu->fsw;
2998 fxsave->twd = fpu->ftwx;
2999 fxsave->fop = fpu->last_opcode;
3000 fxsave->rip = fpu->last_ip;
3001 fxsave->rdp = fpu->last_dp;
3002 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3009 void fx_init(struct kvm_vcpu *vcpu)
3011 unsigned after_mxcsr_mask;
3013 /* Initialize guest FPU by resetting ours and saving into guest's */
3015 fx_save(&vcpu->arch.host_fx_image);
3017 fx_save(&vcpu->arch.guest_fx_image);
3018 fx_restore(&vcpu->arch.host_fx_image);
3021 vcpu->arch.cr0 |= X86_CR0_ET;
3022 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3023 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3024 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3025 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3027 EXPORT_SYMBOL_GPL(fx_init);
3029 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3031 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3034 vcpu->guest_fpu_loaded = 1;
3035 fx_save(&vcpu->arch.host_fx_image);
3036 fx_restore(&vcpu->arch.guest_fx_image);
3038 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3040 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3042 if (!vcpu->guest_fpu_loaded)
3045 vcpu->guest_fpu_loaded = 0;
3046 fx_save(&vcpu->arch.guest_fx_image);
3047 fx_restore(&vcpu->arch.host_fx_image);
3048 ++vcpu->stat.fpu_reload;
3050 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3052 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3054 kvm_x86_ops->vcpu_free(vcpu);
3057 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3060 return kvm_x86_ops->vcpu_create(kvm, id);
3063 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3067 /* We do fxsave: this must be aligned. */
3068 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3071 r = kvm_arch_vcpu_reset(vcpu);
3073 r = kvm_mmu_setup(vcpu);
3080 kvm_x86_ops->vcpu_free(vcpu);
3084 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3087 kvm_mmu_unload(vcpu);
3090 kvm_x86_ops->vcpu_free(vcpu);
3093 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3095 return kvm_x86_ops->vcpu_reset(vcpu);
3098 void kvm_arch_hardware_enable(void *garbage)
3100 kvm_x86_ops->hardware_enable(garbage);
3103 void kvm_arch_hardware_disable(void *garbage)
3105 kvm_x86_ops->hardware_disable(garbage);
3108 int kvm_arch_hardware_setup(void)
3110 return kvm_x86_ops->hardware_setup();
3113 void kvm_arch_hardware_unsetup(void)
3115 kvm_x86_ops->hardware_unsetup();
3118 void kvm_arch_check_processor_compat(void *rtn)
3120 kvm_x86_ops->check_processor_compatibility(rtn);
3123 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3129 BUG_ON(vcpu->kvm == NULL);
3132 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3133 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3134 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
3136 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
3138 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3143 vcpu->arch.pio_data = page_address(page);
3145 r = kvm_mmu_create(vcpu);
3147 goto fail_free_pio_data;
3149 if (irqchip_in_kernel(kvm)) {
3150 r = kvm_create_lapic(vcpu);
3152 goto fail_mmu_destroy;
3158 kvm_mmu_destroy(vcpu);
3160 free_page((unsigned long)vcpu->arch.pio_data);
3165 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3167 kvm_free_lapic(vcpu);
3168 kvm_mmu_destroy(vcpu);
3169 free_page((unsigned long)vcpu->arch.pio_data);
3172 struct kvm *kvm_arch_create_vm(void)
3174 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3177 return ERR_PTR(-ENOMEM);
3179 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3184 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3187 kvm_mmu_unload(vcpu);
3191 static void kvm_free_vcpus(struct kvm *kvm)
3196 * Unpin any mmu pages first.
3198 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3200 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3201 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3202 if (kvm->vcpus[i]) {
3203 kvm_arch_vcpu_free(kvm->vcpus[i]);
3204 kvm->vcpus[i] = NULL;
3210 void kvm_arch_destroy_vm(struct kvm *kvm)
3212 kfree(kvm->arch.vpic);
3213 kfree(kvm->arch.vioapic);
3214 kvm_free_vcpus(kvm);
3215 kvm_free_physmem(kvm);
3219 int kvm_arch_set_memory_region(struct kvm *kvm,
3220 struct kvm_userspace_memory_region *mem,
3221 struct kvm_memory_slot old,
3224 int npages = mem->memory_size >> PAGE_SHIFT;
3225 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3227 /*To keep backward compatibility with older userspace,
3228 *x86 needs to hanlde !user_alloc case.
3231 if (npages && !old.rmap) {
3232 memslot->userspace_addr = do_mmap(NULL, 0,
3234 PROT_READ | PROT_WRITE,
3235 MAP_SHARED | MAP_ANONYMOUS,
3238 if (IS_ERR((void *)memslot->userspace_addr))
3239 return PTR_ERR((void *)memslot->userspace_addr);
3241 if (!old.user_alloc && old.rmap) {
3244 ret = do_munmap(current->mm, old.userspace_addr,
3245 old.npages * PAGE_SIZE);
3248 "kvm_vm_ioctl_set_memory_region: "
3249 "failed to munmap memory\n");
3254 if (!kvm->arch.n_requested_mmu_pages) {
3255 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3256 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3259 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3260 kvm_flush_remote_tlbs(kvm);
3265 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3267 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3268 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3271 static void vcpu_kick_intr(void *info)
3274 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3275 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3279 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3281 int ipi_pcpu = vcpu->cpu;
3283 if (waitqueue_active(&vcpu->wq)) {
3284 wake_up_interruptible(&vcpu->wq);
3285 ++vcpu->stat.halt_wakeup;
3287 if (vcpu->guest_mode)
3288 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);