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)
45 * - enable syscall per default because its emulated by KVM
46 * - enable LME and LMA per default on 64 bit KVM
49 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
51 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
54 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
55 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
57 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
58 struct kvm_cpuid_entry2 __user *entries);
60 struct kvm_x86_ops *kvm_x86_ops;
62 struct kvm_stats_debugfs_item debugfs_entries[] = {
63 { "pf_fixed", VCPU_STAT(pf_fixed) },
64 { "pf_guest", VCPU_STAT(pf_guest) },
65 { "tlb_flush", VCPU_STAT(tlb_flush) },
66 { "invlpg", VCPU_STAT(invlpg) },
67 { "exits", VCPU_STAT(exits) },
68 { "io_exits", VCPU_STAT(io_exits) },
69 { "mmio_exits", VCPU_STAT(mmio_exits) },
70 { "signal_exits", VCPU_STAT(signal_exits) },
71 { "irq_window", VCPU_STAT(irq_window_exits) },
72 { "halt_exits", VCPU_STAT(halt_exits) },
73 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
74 { "request_irq", VCPU_STAT(request_irq_exits) },
75 { "irq_exits", VCPU_STAT(irq_exits) },
76 { "host_state_reload", VCPU_STAT(host_state_reload) },
77 { "efer_reload", VCPU_STAT(efer_reload) },
78 { "fpu_reload", VCPU_STAT(fpu_reload) },
79 { "insn_emulation", VCPU_STAT(insn_emulation) },
80 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
81 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
82 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
83 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
84 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
85 { "mmu_flooded", VM_STAT(mmu_flooded) },
86 { "mmu_recycled", VM_STAT(mmu_recycled) },
87 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
88 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
93 unsigned long segment_base(u16 selector)
95 struct descriptor_table gdt;
96 struct segment_descriptor *d;
97 unsigned long table_base;
103 asm("sgdt %0" : "=m"(gdt));
104 table_base = gdt.base;
106 if (selector & 4) { /* from ldt */
109 asm("sldt %0" : "=g"(ldt_selector));
110 table_base = segment_base(ldt_selector);
112 d = (struct segment_descriptor *)(table_base + (selector & ~7));
113 v = d->base_low | ((unsigned long)d->base_mid << 16) |
114 ((unsigned long)d->base_high << 24);
116 if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
117 v |= ((unsigned long) \
118 ((struct segment_descriptor_64 *)d)->base_higher) << 32;
122 EXPORT_SYMBOL_GPL(segment_base);
124 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
126 if (irqchip_in_kernel(vcpu->kvm))
127 return vcpu->arch.apic_base;
129 return vcpu->arch.apic_base;
131 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
133 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
135 /* TODO: reserve bits check */
136 if (irqchip_in_kernel(vcpu->kvm))
137 kvm_lapic_set_base(vcpu, data);
139 vcpu->arch.apic_base = data;
141 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
143 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
145 WARN_ON(vcpu->arch.exception.pending);
146 vcpu->arch.exception.pending = true;
147 vcpu->arch.exception.has_error_code = false;
148 vcpu->arch.exception.nr = nr;
150 EXPORT_SYMBOL_GPL(kvm_queue_exception);
152 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
155 ++vcpu->stat.pf_guest;
156 if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
157 printk(KERN_DEBUG "kvm: inject_page_fault:"
158 " double fault 0x%lx\n", addr);
159 vcpu->arch.exception.nr = DF_VECTOR;
160 vcpu->arch.exception.error_code = 0;
163 vcpu->arch.cr2 = addr;
164 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
167 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
169 WARN_ON(vcpu->arch.exception.pending);
170 vcpu->arch.exception.pending = true;
171 vcpu->arch.exception.has_error_code = true;
172 vcpu->arch.exception.nr = nr;
173 vcpu->arch.exception.error_code = error_code;
175 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
177 static void __queue_exception(struct kvm_vcpu *vcpu)
179 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
180 vcpu->arch.exception.has_error_code,
181 vcpu->arch.exception.error_code);
185 * Load the pae pdptrs. Return true is they are all valid.
187 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
189 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
190 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
193 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
195 down_read(&vcpu->kvm->slots_lock);
196 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
197 offset * sizeof(u64), sizeof(pdpte));
202 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
203 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
210 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
212 up_read(&vcpu->kvm->slots_lock);
217 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
219 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
223 if (is_long_mode(vcpu) || !is_pae(vcpu))
226 down_read(&vcpu->kvm->slots_lock);
227 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
230 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
232 up_read(&vcpu->kvm->slots_lock);
237 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
239 if (cr0 & CR0_RESERVED_BITS) {
240 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
241 cr0, vcpu->arch.cr0);
242 kvm_inject_gp(vcpu, 0);
246 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
247 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
248 kvm_inject_gp(vcpu, 0);
252 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
253 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
254 "and a clear PE flag\n");
255 kvm_inject_gp(vcpu, 0);
259 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
261 if ((vcpu->arch.shadow_efer & EFER_LME)) {
265 printk(KERN_DEBUG "set_cr0: #GP, start paging "
266 "in long mode while PAE is disabled\n");
267 kvm_inject_gp(vcpu, 0);
270 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
272 printk(KERN_DEBUG "set_cr0: #GP, start paging "
273 "in long mode while CS.L == 1\n");
274 kvm_inject_gp(vcpu, 0);
280 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
281 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
283 kvm_inject_gp(vcpu, 0);
289 kvm_x86_ops->set_cr0(vcpu, cr0);
290 vcpu->arch.cr0 = cr0;
292 kvm_mmu_reset_context(vcpu);
295 EXPORT_SYMBOL_GPL(set_cr0);
297 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
299 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
301 EXPORT_SYMBOL_GPL(lmsw);
303 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
305 if (cr4 & CR4_RESERVED_BITS) {
306 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
307 kvm_inject_gp(vcpu, 0);
311 if (is_long_mode(vcpu)) {
312 if (!(cr4 & X86_CR4_PAE)) {
313 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
315 kvm_inject_gp(vcpu, 0);
318 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
319 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
320 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
321 kvm_inject_gp(vcpu, 0);
325 if (cr4 & X86_CR4_VMXE) {
326 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
327 kvm_inject_gp(vcpu, 0);
330 kvm_x86_ops->set_cr4(vcpu, cr4);
331 vcpu->arch.cr4 = cr4;
332 kvm_mmu_reset_context(vcpu);
334 EXPORT_SYMBOL_GPL(set_cr4);
336 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
338 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
339 kvm_mmu_flush_tlb(vcpu);
343 if (is_long_mode(vcpu)) {
344 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
345 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
346 kvm_inject_gp(vcpu, 0);
351 if (cr3 & CR3_PAE_RESERVED_BITS) {
353 "set_cr3: #GP, reserved bits\n");
354 kvm_inject_gp(vcpu, 0);
357 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
358 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
360 kvm_inject_gp(vcpu, 0);
365 * We don't check reserved bits in nonpae mode, because
366 * this isn't enforced, and VMware depends on this.
370 down_read(&vcpu->kvm->slots_lock);
372 * Does the new cr3 value map to physical memory? (Note, we
373 * catch an invalid cr3 even in real-mode, because it would
374 * cause trouble later on when we turn on paging anyway.)
376 * A real CPU would silently accept an invalid cr3 and would
377 * attempt to use it - with largely undefined (and often hard
378 * to debug) behavior on the guest side.
380 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
381 kvm_inject_gp(vcpu, 0);
383 vcpu->arch.cr3 = cr3;
384 vcpu->arch.mmu.new_cr3(vcpu);
386 up_read(&vcpu->kvm->slots_lock);
388 EXPORT_SYMBOL_GPL(set_cr3);
390 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
392 if (cr8 & CR8_RESERVED_BITS) {
393 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
394 kvm_inject_gp(vcpu, 0);
397 if (irqchip_in_kernel(vcpu->kvm))
398 kvm_lapic_set_tpr(vcpu, cr8);
400 vcpu->arch.cr8 = cr8;
402 EXPORT_SYMBOL_GPL(set_cr8);
404 unsigned long get_cr8(struct kvm_vcpu *vcpu)
406 if (irqchip_in_kernel(vcpu->kvm))
407 return kvm_lapic_get_cr8(vcpu);
409 return vcpu->arch.cr8;
411 EXPORT_SYMBOL_GPL(get_cr8);
414 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
415 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
417 * This list is modified at module load time to reflect the
418 * capabilities of the host cpu.
420 static u32 msrs_to_save[] = {
421 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
424 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
426 MSR_IA32_TIME_STAMP_COUNTER,
429 static unsigned num_msrs_to_save;
431 static u32 emulated_msrs[] = {
432 MSR_IA32_MISC_ENABLE,
435 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
437 if (efer & efer_reserved_bits) {
438 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
440 kvm_inject_gp(vcpu, 0);
445 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
446 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
447 kvm_inject_gp(vcpu, 0);
451 kvm_x86_ops->set_efer(vcpu, efer);
454 efer |= vcpu->arch.shadow_efer & EFER_LMA;
456 vcpu->arch.shadow_efer = efer;
459 void kvm_enable_efer_bits(u64 mask)
461 efer_reserved_bits &= ~mask;
463 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
467 * Writes msr value into into the appropriate "register".
468 * Returns 0 on success, non-0 otherwise.
469 * Assumes vcpu_load() was already called.
471 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
473 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
477 * Adapt set_msr() to msr_io()'s calling convention
479 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
481 return kvm_set_msr(vcpu, index, *data);
485 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
489 set_efer(vcpu, data);
491 case MSR_IA32_MC0_STATUS:
492 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
495 case MSR_IA32_MCG_STATUS:
496 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
499 case MSR_IA32_MCG_CTL:
500 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
503 case MSR_IA32_UCODE_REV:
504 case MSR_IA32_UCODE_WRITE:
505 case 0x200 ... 0x2ff: /* MTRRs */
507 case MSR_IA32_APICBASE:
508 kvm_set_apic_base(vcpu, data);
510 case MSR_IA32_MISC_ENABLE:
511 vcpu->arch.ia32_misc_enable_msr = data;
514 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
519 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
523 * Reads an msr value (of 'msr_index') into 'pdata'.
524 * Returns 0 on success, non-0 otherwise.
525 * Assumes vcpu_load() was already called.
527 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
529 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
532 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
537 case 0xc0010010: /* SYSCFG */
538 case 0xc0010015: /* HWCR */
539 case MSR_IA32_PLATFORM_ID:
540 case MSR_IA32_P5_MC_ADDR:
541 case MSR_IA32_P5_MC_TYPE:
542 case MSR_IA32_MC0_CTL:
543 case MSR_IA32_MCG_STATUS:
544 case MSR_IA32_MCG_CAP:
545 case MSR_IA32_MCG_CTL:
546 case MSR_IA32_MC0_MISC:
547 case MSR_IA32_MC0_MISC+4:
548 case MSR_IA32_MC0_MISC+8:
549 case MSR_IA32_MC0_MISC+12:
550 case MSR_IA32_MC0_MISC+16:
551 case MSR_IA32_UCODE_REV:
552 case MSR_IA32_PERF_STATUS:
553 case MSR_IA32_EBL_CR_POWERON:
556 case 0x200 ... 0x2ff:
559 case 0xcd: /* fsb frequency */
562 case MSR_IA32_APICBASE:
563 data = kvm_get_apic_base(vcpu);
565 case MSR_IA32_MISC_ENABLE:
566 data = vcpu->arch.ia32_misc_enable_msr;
569 data = vcpu->arch.shadow_efer;
572 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
578 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
581 * Read or write a bunch of msrs. All parameters are kernel addresses.
583 * @return number of msrs set successfully.
585 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
586 struct kvm_msr_entry *entries,
587 int (*do_msr)(struct kvm_vcpu *vcpu,
588 unsigned index, u64 *data))
594 for (i = 0; i < msrs->nmsrs; ++i)
595 if (do_msr(vcpu, entries[i].index, &entries[i].data))
604 * Read or write a bunch of msrs. Parameters are user addresses.
606 * @return number of msrs set successfully.
608 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
609 int (*do_msr)(struct kvm_vcpu *vcpu,
610 unsigned index, u64 *data),
613 struct kvm_msrs msrs;
614 struct kvm_msr_entry *entries;
619 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
623 if (msrs.nmsrs >= MAX_IO_MSRS)
627 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
628 entries = vmalloc(size);
633 if (copy_from_user(entries, user_msrs->entries, size))
636 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
641 if (writeback && copy_to_user(user_msrs->entries, entries, size))
653 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
656 void decache_vcpus_on_cpu(int cpu)
659 struct kvm_vcpu *vcpu;
662 spin_lock(&kvm_lock);
663 list_for_each_entry(vm, &vm_list, vm_list)
664 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
669 * If the vcpu is locked, then it is running on some
670 * other cpu and therefore it is not cached on the
673 * If it's not locked, check the last cpu it executed
676 if (mutex_trylock(&vcpu->mutex)) {
677 if (vcpu->cpu == cpu) {
678 kvm_x86_ops->vcpu_decache(vcpu);
681 mutex_unlock(&vcpu->mutex);
684 spin_unlock(&kvm_lock);
687 int kvm_dev_ioctl_check_extension(long ext)
692 case KVM_CAP_IRQCHIP:
694 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
695 case KVM_CAP_USER_MEMORY:
696 case KVM_CAP_SET_TSS_ADDR:
697 case KVM_CAP_EXT_CPUID:
701 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
711 long kvm_arch_dev_ioctl(struct file *filp,
712 unsigned int ioctl, unsigned long arg)
714 void __user *argp = (void __user *)arg;
718 case KVM_GET_MSR_INDEX_LIST: {
719 struct kvm_msr_list __user *user_msr_list = argp;
720 struct kvm_msr_list msr_list;
724 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
727 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
728 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
731 if (n < num_msrs_to_save)
734 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
735 num_msrs_to_save * sizeof(u32)))
737 if (copy_to_user(user_msr_list->indices
738 + num_msrs_to_save * sizeof(u32),
740 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
745 case KVM_GET_SUPPORTED_CPUID: {
746 struct kvm_cpuid2 __user *cpuid_arg = argp;
747 struct kvm_cpuid2 cpuid;
750 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
752 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
758 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
770 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
772 kvm_x86_ops->vcpu_load(vcpu, cpu);
775 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
777 kvm_x86_ops->vcpu_put(vcpu);
778 kvm_put_guest_fpu(vcpu);
781 static int is_efer_nx(void)
785 rdmsrl(MSR_EFER, efer);
786 return efer & EFER_NX;
789 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
792 struct kvm_cpuid_entry2 *e, *entry;
795 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
796 e = &vcpu->arch.cpuid_entries[i];
797 if (e->function == 0x80000001) {
802 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
803 entry->edx &= ~(1 << 20);
804 printk(KERN_INFO "kvm: guest NX capability removed\n");
808 /* when an old userspace process fills a new kernel module */
809 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
810 struct kvm_cpuid *cpuid,
811 struct kvm_cpuid_entry __user *entries)
814 struct kvm_cpuid_entry *cpuid_entries;
817 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
820 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
824 if (copy_from_user(cpuid_entries, entries,
825 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
827 for (i = 0; i < cpuid->nent; i++) {
828 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
829 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
830 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
831 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
832 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
833 vcpu->arch.cpuid_entries[i].index = 0;
834 vcpu->arch.cpuid_entries[i].flags = 0;
835 vcpu->arch.cpuid_entries[i].padding[0] = 0;
836 vcpu->arch.cpuid_entries[i].padding[1] = 0;
837 vcpu->arch.cpuid_entries[i].padding[2] = 0;
839 vcpu->arch.cpuid_nent = cpuid->nent;
840 cpuid_fix_nx_cap(vcpu);
844 vfree(cpuid_entries);
849 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
850 struct kvm_cpuid2 *cpuid,
851 struct kvm_cpuid_entry2 __user *entries)
856 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
859 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
860 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
862 vcpu->arch.cpuid_nent = cpuid->nent;
869 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
870 struct kvm_cpuid2 *cpuid,
871 struct kvm_cpuid_entry2 __user *entries)
876 if (cpuid->nent < vcpu->arch.cpuid_nent)
879 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
880 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
885 cpuid->nent = vcpu->arch.cpuid_nent;
889 static inline u32 bit(int bitno)
891 return 1 << (bitno & 31);
894 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
897 entry->function = function;
898 entry->index = index;
899 cpuid_count(entry->function, entry->index,
900 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
904 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
905 u32 index, int *nent, int maxnent)
907 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
908 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
909 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
910 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
911 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
912 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
913 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
914 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
915 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
916 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
917 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
918 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
919 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
920 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
921 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
922 bit(X86_FEATURE_PGE) |
923 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
924 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
925 bit(X86_FEATURE_SYSCALL) |
926 (bit(X86_FEATURE_NX) && is_efer_nx()) |
928 bit(X86_FEATURE_LM) |
930 bit(X86_FEATURE_MMXEXT) |
931 bit(X86_FEATURE_3DNOWEXT) |
932 bit(X86_FEATURE_3DNOW);
933 const u32 kvm_supported_word3_x86_features =
934 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
935 const u32 kvm_supported_word6_x86_features =
936 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
938 /* all func 2 cpuid_count() should be called on the same cpu */
940 do_cpuid_1_ent(entry, function, index);
945 entry->eax = min(entry->eax, (u32)0xb);
948 entry->edx &= kvm_supported_word0_x86_features;
949 entry->ecx &= kvm_supported_word3_x86_features;
951 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
952 * may return different values. This forces us to get_cpu() before
953 * issuing the first command, and also to emulate this annoying behavior
954 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
956 int t, times = entry->eax & 0xff;
958 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
959 for (t = 1; t < times && *nent < maxnent; ++t) {
960 do_cpuid_1_ent(&entry[t], function, 0);
961 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
966 /* function 4 and 0xb have additional index. */
968 int index, cache_type;
970 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
971 /* read more entries until cache_type is zero */
972 for (index = 1; *nent < maxnent; ++index) {
973 cache_type = entry[index - 1].eax & 0x1f;
976 do_cpuid_1_ent(&entry[index], function, index);
977 entry[index].flags |=
978 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
984 int index, level_type;
986 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
987 /* read more entries until level_type is zero */
988 for (index = 1; *nent < maxnent; ++index) {
989 level_type = entry[index - 1].ecx & 0xff;
992 do_cpuid_1_ent(&entry[index], function, index);
993 entry[index].flags |=
994 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1000 entry->eax = min(entry->eax, 0x8000001a);
1003 entry->edx &= kvm_supported_word1_x86_features;
1004 entry->ecx &= kvm_supported_word6_x86_features;
1010 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1011 struct kvm_cpuid_entry2 __user *entries)
1013 struct kvm_cpuid_entry2 *cpuid_entries;
1014 int limit, nent = 0, r = -E2BIG;
1017 if (cpuid->nent < 1)
1020 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1024 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1025 limit = cpuid_entries[0].eax;
1026 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1027 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1028 &nent, cpuid->nent);
1030 if (nent >= cpuid->nent)
1033 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1034 limit = cpuid_entries[nent - 1].eax;
1035 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1036 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1037 &nent, cpuid->nent);
1039 if (copy_to_user(entries, cpuid_entries,
1040 nent * sizeof(struct kvm_cpuid_entry2)))
1046 vfree(cpuid_entries);
1051 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1052 struct kvm_lapic_state *s)
1055 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1061 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1062 struct kvm_lapic_state *s)
1065 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1066 kvm_apic_post_state_restore(vcpu);
1072 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1073 struct kvm_interrupt *irq)
1075 if (irq->irq < 0 || irq->irq >= 256)
1077 if (irqchip_in_kernel(vcpu->kvm))
1081 set_bit(irq->irq, vcpu->arch.irq_pending);
1082 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1089 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1090 struct kvm_tpr_access_ctl *tac)
1094 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1098 long kvm_arch_vcpu_ioctl(struct file *filp,
1099 unsigned int ioctl, unsigned long arg)
1101 struct kvm_vcpu *vcpu = filp->private_data;
1102 void __user *argp = (void __user *)arg;
1106 case KVM_GET_LAPIC: {
1107 struct kvm_lapic_state lapic;
1109 memset(&lapic, 0, sizeof lapic);
1110 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1114 if (copy_to_user(argp, &lapic, sizeof lapic))
1119 case KVM_SET_LAPIC: {
1120 struct kvm_lapic_state lapic;
1123 if (copy_from_user(&lapic, argp, sizeof lapic))
1125 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1131 case KVM_INTERRUPT: {
1132 struct kvm_interrupt irq;
1135 if (copy_from_user(&irq, argp, sizeof irq))
1137 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1143 case KVM_SET_CPUID: {
1144 struct kvm_cpuid __user *cpuid_arg = argp;
1145 struct kvm_cpuid cpuid;
1148 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1150 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1155 case KVM_SET_CPUID2: {
1156 struct kvm_cpuid2 __user *cpuid_arg = argp;
1157 struct kvm_cpuid2 cpuid;
1160 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1162 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1163 cpuid_arg->entries);
1168 case KVM_GET_CPUID2: {
1169 struct kvm_cpuid2 __user *cpuid_arg = argp;
1170 struct kvm_cpuid2 cpuid;
1173 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1175 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1176 cpuid_arg->entries);
1180 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1186 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1189 r = msr_io(vcpu, argp, do_set_msr, 0);
1191 case KVM_TPR_ACCESS_REPORTING: {
1192 struct kvm_tpr_access_ctl tac;
1195 if (copy_from_user(&tac, argp, sizeof tac))
1197 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1201 if (copy_to_user(argp, &tac, sizeof tac))
1206 case KVM_SET_VAPIC_ADDR: {
1207 struct kvm_vapic_addr va;
1210 if (!irqchip_in_kernel(vcpu->kvm))
1213 if (copy_from_user(&va, argp, sizeof va))
1216 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1226 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1230 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1232 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1236 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1237 u32 kvm_nr_mmu_pages)
1239 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1242 down_write(&kvm->slots_lock);
1244 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1245 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1247 up_write(&kvm->slots_lock);
1251 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1253 return kvm->arch.n_alloc_mmu_pages;
1256 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1259 struct kvm_mem_alias *alias;
1261 for (i = 0; i < kvm->arch.naliases; ++i) {
1262 alias = &kvm->arch.aliases[i];
1263 if (gfn >= alias->base_gfn
1264 && gfn < alias->base_gfn + alias->npages)
1265 return alias->target_gfn + gfn - alias->base_gfn;
1271 * Set a new alias region. Aliases map a portion of physical memory into
1272 * another portion. This is useful for memory windows, for example the PC
1275 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1276 struct kvm_memory_alias *alias)
1279 struct kvm_mem_alias *p;
1282 /* General sanity checks */
1283 if (alias->memory_size & (PAGE_SIZE - 1))
1285 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1287 if (alias->slot >= KVM_ALIAS_SLOTS)
1289 if (alias->guest_phys_addr + alias->memory_size
1290 < alias->guest_phys_addr)
1292 if (alias->target_phys_addr + alias->memory_size
1293 < alias->target_phys_addr)
1296 down_write(&kvm->slots_lock);
1298 p = &kvm->arch.aliases[alias->slot];
1299 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1300 p->npages = alias->memory_size >> PAGE_SHIFT;
1301 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1303 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1304 if (kvm->arch.aliases[n - 1].npages)
1306 kvm->arch.naliases = n;
1308 kvm_mmu_zap_all(kvm);
1310 up_write(&kvm->slots_lock);
1318 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1323 switch (chip->chip_id) {
1324 case KVM_IRQCHIP_PIC_MASTER:
1325 memcpy(&chip->chip.pic,
1326 &pic_irqchip(kvm)->pics[0],
1327 sizeof(struct kvm_pic_state));
1329 case KVM_IRQCHIP_PIC_SLAVE:
1330 memcpy(&chip->chip.pic,
1331 &pic_irqchip(kvm)->pics[1],
1332 sizeof(struct kvm_pic_state));
1334 case KVM_IRQCHIP_IOAPIC:
1335 memcpy(&chip->chip.ioapic,
1336 ioapic_irqchip(kvm),
1337 sizeof(struct kvm_ioapic_state));
1346 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1351 switch (chip->chip_id) {
1352 case KVM_IRQCHIP_PIC_MASTER:
1353 memcpy(&pic_irqchip(kvm)->pics[0],
1355 sizeof(struct kvm_pic_state));
1357 case KVM_IRQCHIP_PIC_SLAVE:
1358 memcpy(&pic_irqchip(kvm)->pics[1],
1360 sizeof(struct kvm_pic_state));
1362 case KVM_IRQCHIP_IOAPIC:
1363 memcpy(ioapic_irqchip(kvm),
1365 sizeof(struct kvm_ioapic_state));
1371 kvm_pic_update_irq(pic_irqchip(kvm));
1376 * Get (and clear) the dirty memory log for a memory slot.
1378 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1379 struct kvm_dirty_log *log)
1383 struct kvm_memory_slot *memslot;
1386 down_write(&kvm->slots_lock);
1388 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1392 /* If nothing is dirty, don't bother messing with page tables. */
1394 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1395 kvm_flush_remote_tlbs(kvm);
1396 memslot = &kvm->memslots[log->slot];
1397 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1398 memset(memslot->dirty_bitmap, 0, n);
1402 up_write(&kvm->slots_lock);
1406 long kvm_arch_vm_ioctl(struct file *filp,
1407 unsigned int ioctl, unsigned long arg)
1409 struct kvm *kvm = filp->private_data;
1410 void __user *argp = (void __user *)arg;
1414 case KVM_SET_TSS_ADDR:
1415 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1419 case KVM_SET_MEMORY_REGION: {
1420 struct kvm_memory_region kvm_mem;
1421 struct kvm_userspace_memory_region kvm_userspace_mem;
1424 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1426 kvm_userspace_mem.slot = kvm_mem.slot;
1427 kvm_userspace_mem.flags = kvm_mem.flags;
1428 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1429 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1430 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1435 case KVM_SET_NR_MMU_PAGES:
1436 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1440 case KVM_GET_NR_MMU_PAGES:
1441 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1443 case KVM_SET_MEMORY_ALIAS: {
1444 struct kvm_memory_alias alias;
1447 if (copy_from_user(&alias, argp, sizeof alias))
1449 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1454 case KVM_CREATE_IRQCHIP:
1456 kvm->arch.vpic = kvm_create_pic(kvm);
1457 if (kvm->arch.vpic) {
1458 r = kvm_ioapic_init(kvm);
1460 kfree(kvm->arch.vpic);
1461 kvm->arch.vpic = NULL;
1467 case KVM_IRQ_LINE: {
1468 struct kvm_irq_level irq_event;
1471 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1473 if (irqchip_in_kernel(kvm)) {
1474 mutex_lock(&kvm->lock);
1475 if (irq_event.irq < 16)
1476 kvm_pic_set_irq(pic_irqchip(kvm),
1479 kvm_ioapic_set_irq(kvm->arch.vioapic,
1482 mutex_unlock(&kvm->lock);
1487 case KVM_GET_IRQCHIP: {
1488 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1489 struct kvm_irqchip chip;
1492 if (copy_from_user(&chip, argp, sizeof chip))
1495 if (!irqchip_in_kernel(kvm))
1497 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1501 if (copy_to_user(argp, &chip, sizeof chip))
1506 case KVM_SET_IRQCHIP: {
1507 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1508 struct kvm_irqchip chip;
1511 if (copy_from_user(&chip, argp, sizeof chip))
1514 if (!irqchip_in_kernel(kvm))
1516 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1529 static void kvm_init_msr_list(void)
1534 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1535 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1538 msrs_to_save[j] = msrs_to_save[i];
1541 num_msrs_to_save = j;
1545 * Only apic need an MMIO device hook, so shortcut now..
1547 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1550 struct kvm_io_device *dev;
1552 if (vcpu->arch.apic) {
1553 dev = &vcpu->arch.apic->dev;
1554 if (dev->in_range(dev, addr))
1561 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1564 struct kvm_io_device *dev;
1566 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1568 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1572 int emulator_read_std(unsigned long addr,
1575 struct kvm_vcpu *vcpu)
1578 int r = X86EMUL_CONTINUE;
1580 down_read(&vcpu->kvm->slots_lock);
1582 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1583 unsigned offset = addr & (PAGE_SIZE-1);
1584 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1587 if (gpa == UNMAPPED_GVA) {
1588 r = X86EMUL_PROPAGATE_FAULT;
1591 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1593 r = X86EMUL_UNHANDLEABLE;
1602 up_read(&vcpu->kvm->slots_lock);
1605 EXPORT_SYMBOL_GPL(emulator_read_std);
1607 static int emulator_read_emulated(unsigned long addr,
1610 struct kvm_vcpu *vcpu)
1612 struct kvm_io_device *mmio_dev;
1615 if (vcpu->mmio_read_completed) {
1616 memcpy(val, vcpu->mmio_data, bytes);
1617 vcpu->mmio_read_completed = 0;
1618 return X86EMUL_CONTINUE;
1621 down_read(&vcpu->kvm->slots_lock);
1622 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1623 up_read(&vcpu->kvm->slots_lock);
1625 /* For APIC access vmexit */
1626 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1629 if (emulator_read_std(addr, val, bytes, vcpu)
1630 == X86EMUL_CONTINUE)
1631 return X86EMUL_CONTINUE;
1632 if (gpa == UNMAPPED_GVA)
1633 return X86EMUL_PROPAGATE_FAULT;
1637 * Is this MMIO handled locally?
1639 mutex_lock(&vcpu->kvm->lock);
1640 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1642 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1643 mutex_unlock(&vcpu->kvm->lock);
1644 return X86EMUL_CONTINUE;
1646 mutex_unlock(&vcpu->kvm->lock);
1648 vcpu->mmio_needed = 1;
1649 vcpu->mmio_phys_addr = gpa;
1650 vcpu->mmio_size = bytes;
1651 vcpu->mmio_is_write = 0;
1653 return X86EMUL_UNHANDLEABLE;
1656 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1657 const void *val, int bytes)
1661 down_read(&vcpu->kvm->slots_lock);
1662 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1664 up_read(&vcpu->kvm->slots_lock);
1667 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1668 up_read(&vcpu->kvm->slots_lock);
1672 static int emulator_write_emulated_onepage(unsigned long addr,
1675 struct kvm_vcpu *vcpu)
1677 struct kvm_io_device *mmio_dev;
1680 down_read(&vcpu->kvm->slots_lock);
1681 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1682 up_read(&vcpu->kvm->slots_lock);
1684 if (gpa == UNMAPPED_GVA) {
1685 kvm_inject_page_fault(vcpu, addr, 2);
1686 return X86EMUL_PROPAGATE_FAULT;
1689 /* For APIC access vmexit */
1690 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1693 if (emulator_write_phys(vcpu, gpa, val, bytes))
1694 return X86EMUL_CONTINUE;
1698 * Is this MMIO handled locally?
1700 mutex_lock(&vcpu->kvm->lock);
1701 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1703 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1704 mutex_unlock(&vcpu->kvm->lock);
1705 return X86EMUL_CONTINUE;
1707 mutex_unlock(&vcpu->kvm->lock);
1709 vcpu->mmio_needed = 1;
1710 vcpu->mmio_phys_addr = gpa;
1711 vcpu->mmio_size = bytes;
1712 vcpu->mmio_is_write = 1;
1713 memcpy(vcpu->mmio_data, val, bytes);
1715 return X86EMUL_CONTINUE;
1718 int emulator_write_emulated(unsigned long addr,
1721 struct kvm_vcpu *vcpu)
1723 /* Crossing a page boundary? */
1724 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1727 now = -addr & ~PAGE_MASK;
1728 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1729 if (rc != X86EMUL_CONTINUE)
1735 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1737 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1739 static int emulator_cmpxchg_emulated(unsigned long addr,
1743 struct kvm_vcpu *vcpu)
1745 static int reported;
1749 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1751 #ifndef CONFIG_X86_64
1752 /* guests cmpxchg8b have to be emulated atomically */
1759 down_read(&vcpu->kvm->slots_lock);
1760 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1762 if (gpa == UNMAPPED_GVA ||
1763 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1766 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1771 down_read(¤t->mm->mmap_sem);
1772 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1773 up_read(¤t->mm->mmap_sem);
1775 kaddr = kmap_atomic(page, KM_USER0);
1776 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1777 kunmap_atomic(kaddr, KM_USER0);
1778 kvm_release_page_dirty(page);
1780 up_read(&vcpu->kvm->slots_lock);
1784 return emulator_write_emulated(addr, new, bytes, vcpu);
1787 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1789 return kvm_x86_ops->get_segment_base(vcpu, seg);
1792 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1794 return X86EMUL_CONTINUE;
1797 int emulate_clts(struct kvm_vcpu *vcpu)
1799 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
1800 return X86EMUL_CONTINUE;
1803 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1805 struct kvm_vcpu *vcpu = ctxt->vcpu;
1809 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1810 return X86EMUL_CONTINUE;
1812 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1813 return X86EMUL_UNHANDLEABLE;
1817 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1819 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1822 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1824 /* FIXME: better handling */
1825 return X86EMUL_UNHANDLEABLE;
1827 return X86EMUL_CONTINUE;
1830 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1832 static int reported;
1834 unsigned long rip = vcpu->arch.rip;
1835 unsigned long rip_linear;
1837 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1842 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1844 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1845 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1848 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1850 struct x86_emulate_ops emulate_ops = {
1851 .read_std = emulator_read_std,
1852 .read_emulated = emulator_read_emulated,
1853 .write_emulated = emulator_write_emulated,
1854 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1857 int emulate_instruction(struct kvm_vcpu *vcpu,
1858 struct kvm_run *run,
1864 struct decode_cache *c;
1866 vcpu->arch.mmio_fault_cr2 = cr2;
1867 kvm_x86_ops->cache_regs(vcpu);
1869 vcpu->mmio_is_write = 0;
1870 vcpu->arch.pio.string = 0;
1872 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
1874 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1876 vcpu->arch.emulate_ctxt.vcpu = vcpu;
1877 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
1878 vcpu->arch.emulate_ctxt.mode =
1879 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
1880 ? X86EMUL_MODE_REAL : cs_l
1881 ? X86EMUL_MODE_PROT64 : cs_db
1882 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1884 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1885 vcpu->arch.emulate_ctxt.cs_base = 0;
1886 vcpu->arch.emulate_ctxt.ds_base = 0;
1887 vcpu->arch.emulate_ctxt.es_base = 0;
1888 vcpu->arch.emulate_ctxt.ss_base = 0;
1890 vcpu->arch.emulate_ctxt.cs_base =
1891 get_segment_base(vcpu, VCPU_SREG_CS);
1892 vcpu->arch.emulate_ctxt.ds_base =
1893 get_segment_base(vcpu, VCPU_SREG_DS);
1894 vcpu->arch.emulate_ctxt.es_base =
1895 get_segment_base(vcpu, VCPU_SREG_ES);
1896 vcpu->arch.emulate_ctxt.ss_base =
1897 get_segment_base(vcpu, VCPU_SREG_SS);
1900 vcpu->arch.emulate_ctxt.gs_base =
1901 get_segment_base(vcpu, VCPU_SREG_GS);
1902 vcpu->arch.emulate_ctxt.fs_base =
1903 get_segment_base(vcpu, VCPU_SREG_FS);
1905 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1907 /* Reject the instructions other than VMCALL/VMMCALL when
1908 * try to emulate invalid opcode */
1909 c = &vcpu->arch.emulate_ctxt.decode;
1910 if ((emulation_type & EMULTYPE_TRAP_UD) &&
1911 (!(c->twobyte && c->b == 0x01 &&
1912 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
1913 c->modrm_mod == 3 && c->modrm_rm == 1)))
1914 return EMULATE_FAIL;
1916 ++vcpu->stat.insn_emulation;
1918 ++vcpu->stat.insn_emulation_fail;
1919 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1920 return EMULATE_DONE;
1921 return EMULATE_FAIL;
1925 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
1927 if (vcpu->arch.pio.string)
1928 return EMULATE_DO_MMIO;
1930 if ((r || vcpu->mmio_is_write) && run) {
1931 run->exit_reason = KVM_EXIT_MMIO;
1932 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1933 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1934 run->mmio.len = vcpu->mmio_size;
1935 run->mmio.is_write = vcpu->mmio_is_write;
1939 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1940 return EMULATE_DONE;
1941 if (!vcpu->mmio_needed) {
1942 kvm_report_emulation_failure(vcpu, "mmio");
1943 return EMULATE_FAIL;
1945 return EMULATE_DO_MMIO;
1948 kvm_x86_ops->decache_regs(vcpu);
1949 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
1951 if (vcpu->mmio_is_write) {
1952 vcpu->mmio_needed = 0;
1953 return EMULATE_DO_MMIO;
1956 return EMULATE_DONE;
1958 EXPORT_SYMBOL_GPL(emulate_instruction);
1960 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
1964 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
1965 if (vcpu->arch.pio.guest_pages[i]) {
1966 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
1967 vcpu->arch.pio.guest_pages[i] = NULL;
1971 static int pio_copy_data(struct kvm_vcpu *vcpu)
1973 void *p = vcpu->arch.pio_data;
1976 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
1978 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1981 free_pio_guest_pages(vcpu);
1984 q += vcpu->arch.pio.guest_page_offset;
1985 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
1986 if (vcpu->arch.pio.in)
1987 memcpy(q, p, bytes);
1989 memcpy(p, q, bytes);
1990 q -= vcpu->arch.pio.guest_page_offset;
1992 free_pio_guest_pages(vcpu);
1996 int complete_pio(struct kvm_vcpu *vcpu)
1998 struct kvm_pio_request *io = &vcpu->arch.pio;
2002 kvm_x86_ops->cache_regs(vcpu);
2006 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
2010 r = pio_copy_data(vcpu);
2012 kvm_x86_ops->cache_regs(vcpu);
2019 delta *= io->cur_count;
2021 * The size of the register should really depend on
2022 * current address size.
2024 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2030 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2032 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2035 kvm_x86_ops->decache_regs(vcpu);
2037 io->count -= io->cur_count;
2043 static void kernel_pio(struct kvm_io_device *pio_dev,
2044 struct kvm_vcpu *vcpu,
2047 /* TODO: String I/O for in kernel device */
2049 mutex_lock(&vcpu->kvm->lock);
2050 if (vcpu->arch.pio.in)
2051 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2052 vcpu->arch.pio.size,
2055 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2056 vcpu->arch.pio.size,
2058 mutex_unlock(&vcpu->kvm->lock);
2061 static void pio_string_write(struct kvm_io_device *pio_dev,
2062 struct kvm_vcpu *vcpu)
2064 struct kvm_pio_request *io = &vcpu->arch.pio;
2065 void *pd = vcpu->arch.pio_data;
2068 mutex_lock(&vcpu->kvm->lock);
2069 for (i = 0; i < io->cur_count; i++) {
2070 kvm_iodevice_write(pio_dev, io->port,
2075 mutex_unlock(&vcpu->kvm->lock);
2078 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2081 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2084 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2085 int size, unsigned port)
2087 struct kvm_io_device *pio_dev;
2089 vcpu->run->exit_reason = KVM_EXIT_IO;
2090 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2091 vcpu->run->io.size = vcpu->arch.pio.size = size;
2092 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2093 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2094 vcpu->run->io.port = vcpu->arch.pio.port = port;
2095 vcpu->arch.pio.in = in;
2096 vcpu->arch.pio.string = 0;
2097 vcpu->arch.pio.down = 0;
2098 vcpu->arch.pio.guest_page_offset = 0;
2099 vcpu->arch.pio.rep = 0;
2101 kvm_x86_ops->cache_regs(vcpu);
2102 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2103 kvm_x86_ops->decache_regs(vcpu);
2105 kvm_x86_ops->skip_emulated_instruction(vcpu);
2107 pio_dev = vcpu_find_pio_dev(vcpu, port);
2109 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2115 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2117 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2118 int size, unsigned long count, int down,
2119 gva_t address, int rep, unsigned port)
2121 unsigned now, in_page;
2125 struct kvm_io_device *pio_dev;
2127 vcpu->run->exit_reason = KVM_EXIT_IO;
2128 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2129 vcpu->run->io.size = vcpu->arch.pio.size = size;
2130 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2131 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2132 vcpu->run->io.port = vcpu->arch.pio.port = port;
2133 vcpu->arch.pio.in = in;
2134 vcpu->arch.pio.string = 1;
2135 vcpu->arch.pio.down = down;
2136 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2137 vcpu->arch.pio.rep = rep;
2140 kvm_x86_ops->skip_emulated_instruction(vcpu);
2145 in_page = PAGE_SIZE - offset_in_page(address);
2147 in_page = offset_in_page(address) + size;
2148 now = min(count, (unsigned long)in_page / size);
2151 * String I/O straddles page boundary. Pin two guest pages
2152 * so that we satisfy atomicity constraints. Do just one
2153 * transaction to avoid complexity.
2160 * String I/O in reverse. Yuck. Kill the guest, fix later.
2162 pr_unimpl(vcpu, "guest string pio down\n");
2163 kvm_inject_gp(vcpu, 0);
2166 vcpu->run->io.count = now;
2167 vcpu->arch.pio.cur_count = now;
2169 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2170 kvm_x86_ops->skip_emulated_instruction(vcpu);
2172 for (i = 0; i < nr_pages; ++i) {
2173 down_read(&vcpu->kvm->slots_lock);
2174 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2175 vcpu->arch.pio.guest_pages[i] = page;
2176 up_read(&vcpu->kvm->slots_lock);
2178 kvm_inject_gp(vcpu, 0);
2179 free_pio_guest_pages(vcpu);
2184 pio_dev = vcpu_find_pio_dev(vcpu, port);
2185 if (!vcpu->arch.pio.in) {
2186 /* string PIO write */
2187 ret = pio_copy_data(vcpu);
2188 if (ret >= 0 && pio_dev) {
2189 pio_string_write(pio_dev, vcpu);
2191 if (vcpu->arch.pio.count == 0)
2195 pr_unimpl(vcpu, "no string pio read support yet, "
2196 "port %x size %d count %ld\n",
2201 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2203 int kvm_arch_init(void *opaque)
2206 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2209 printk(KERN_ERR "kvm: already loaded the other module\n");
2214 if (!ops->cpu_has_kvm_support()) {
2215 printk(KERN_ERR "kvm: no hardware support\n");
2219 if (ops->disabled_by_bios()) {
2220 printk(KERN_ERR "kvm: disabled by bios\n");
2225 r = kvm_mmu_module_init();
2229 kvm_init_msr_list();
2232 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2239 void kvm_arch_exit(void)
2242 kvm_mmu_module_exit();
2245 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2247 ++vcpu->stat.halt_exits;
2248 if (irqchip_in_kernel(vcpu->kvm)) {
2249 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
2250 kvm_vcpu_block(vcpu);
2251 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
2255 vcpu->run->exit_reason = KVM_EXIT_HLT;
2259 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2261 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2263 unsigned long nr, a0, a1, a2, a3, ret;
2265 kvm_x86_ops->cache_regs(vcpu);
2267 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2268 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2269 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2270 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2271 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2273 if (!is_long_mode(vcpu)) {
2282 case KVM_HC_VAPIC_POLL_IRQ:
2289 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2290 kvm_x86_ops->decache_regs(vcpu);
2293 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2295 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2297 char instruction[3];
2302 * Blow out the MMU to ensure that no other VCPU has an active mapping
2303 * to ensure that the updated hypercall appears atomically across all
2306 kvm_mmu_zap_all(vcpu->kvm);
2308 kvm_x86_ops->cache_regs(vcpu);
2309 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2310 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2311 != X86EMUL_CONTINUE)
2317 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2319 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2322 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2324 struct descriptor_table dt = { limit, base };
2326 kvm_x86_ops->set_gdt(vcpu, &dt);
2329 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2331 struct descriptor_table dt = { limit, base };
2333 kvm_x86_ops->set_idt(vcpu, &dt);
2336 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2337 unsigned long *rflags)
2340 *rflags = kvm_x86_ops->get_rflags(vcpu);
2343 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2345 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2348 return vcpu->arch.cr0;
2350 return vcpu->arch.cr2;
2352 return vcpu->arch.cr3;
2354 return vcpu->arch.cr4;
2356 return get_cr8(vcpu);
2358 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2363 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2364 unsigned long *rflags)
2368 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2369 *rflags = kvm_x86_ops->get_rflags(vcpu);
2372 vcpu->arch.cr2 = val;
2378 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2381 set_cr8(vcpu, val & 0xfUL);
2384 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2388 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2390 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2391 int j, nent = vcpu->arch.cpuid_nent;
2393 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2394 /* when no next entry is found, the current entry[i] is reselected */
2395 for (j = i + 1; j == i; j = (j + 1) % nent) {
2396 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2397 if (ej->function == e->function) {
2398 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2402 return 0; /* silence gcc, even though control never reaches here */
2405 /* find an entry with matching function, matching index (if needed), and that
2406 * should be read next (if it's stateful) */
2407 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2408 u32 function, u32 index)
2410 if (e->function != function)
2412 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2414 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2415 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2420 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2423 u32 function, index;
2424 struct kvm_cpuid_entry2 *e, *best;
2426 kvm_x86_ops->cache_regs(vcpu);
2427 function = vcpu->arch.regs[VCPU_REGS_RAX];
2428 index = vcpu->arch.regs[VCPU_REGS_RCX];
2429 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2430 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2431 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2432 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2434 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2435 e = &vcpu->arch.cpuid_entries[i];
2436 if (is_matching_cpuid_entry(e, function, index)) {
2437 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2438 move_to_next_stateful_cpuid_entry(vcpu, i);
2443 * Both basic or both extended?
2445 if (((e->function ^ function) & 0x80000000) == 0)
2446 if (!best || e->function > best->function)
2450 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2451 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2452 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2453 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2455 kvm_x86_ops->decache_regs(vcpu);
2456 kvm_x86_ops->skip_emulated_instruction(vcpu);
2458 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2461 * Check if userspace requested an interrupt window, and that the
2462 * interrupt window is open.
2464 * No need to exit to userspace if we already have an interrupt queued.
2466 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2467 struct kvm_run *kvm_run)
2469 return (!vcpu->arch.irq_summary &&
2470 kvm_run->request_interrupt_window &&
2471 vcpu->arch.interrupt_window_open &&
2472 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2475 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2476 struct kvm_run *kvm_run)
2478 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2479 kvm_run->cr8 = get_cr8(vcpu);
2480 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2481 if (irqchip_in_kernel(vcpu->kvm))
2482 kvm_run->ready_for_interrupt_injection = 1;
2484 kvm_run->ready_for_interrupt_injection =
2485 (vcpu->arch.interrupt_window_open &&
2486 vcpu->arch.irq_summary == 0);
2489 static void vapic_enter(struct kvm_vcpu *vcpu)
2491 struct kvm_lapic *apic = vcpu->arch.apic;
2494 if (!apic || !apic->vapic_addr)
2497 down_read(¤t->mm->mmap_sem);
2498 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2499 up_read(¤t->mm->mmap_sem);
2501 vcpu->arch.apic->vapic_page = page;
2504 static void vapic_exit(struct kvm_vcpu *vcpu)
2506 struct kvm_lapic *apic = vcpu->arch.apic;
2508 if (!apic || !apic->vapic_addr)
2511 kvm_release_page_dirty(apic->vapic_page);
2512 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2515 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2519 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2520 pr_debug("vcpu %d received sipi with vector # %x\n",
2521 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2522 kvm_lapic_reset(vcpu);
2523 r = kvm_x86_ops->vcpu_reset(vcpu);
2526 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
2532 if (vcpu->guest_debug.enabled)
2533 kvm_x86_ops->guest_debug_pre(vcpu);
2536 r = kvm_mmu_reload(vcpu);
2540 if (vcpu->requests) {
2541 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2542 __kvm_migrate_apic_timer(vcpu);
2543 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2545 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2551 kvm_inject_pending_timer_irqs(vcpu);
2555 kvm_x86_ops->prepare_guest_switch(vcpu);
2556 kvm_load_guest_fpu(vcpu);
2558 local_irq_disable();
2560 if (need_resched()) {
2567 if (signal_pending(current)) {
2571 kvm_run->exit_reason = KVM_EXIT_INTR;
2572 ++vcpu->stat.signal_exits;
2576 if (vcpu->arch.exception.pending)
2577 __queue_exception(vcpu);
2578 else if (irqchip_in_kernel(vcpu->kvm))
2579 kvm_x86_ops->inject_pending_irq(vcpu);
2581 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2583 kvm_lapic_sync_to_vapic(vcpu);
2585 vcpu->guest_mode = 1;
2589 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2590 kvm_x86_ops->tlb_flush(vcpu);
2592 kvm_x86_ops->run(vcpu, kvm_run);
2594 vcpu->guest_mode = 0;
2600 * We must have an instruction between local_irq_enable() and
2601 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2602 * the interrupt shadow. The stat.exits increment will do nicely.
2603 * But we need to prevent reordering, hence this barrier():
2612 * Profile KVM exit RIPs:
2614 if (unlikely(prof_on == KVM_PROFILING)) {
2615 kvm_x86_ops->cache_regs(vcpu);
2616 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2619 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2620 vcpu->arch.exception.pending = false;
2622 kvm_lapic_sync_from_vapic(vcpu);
2624 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2627 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2629 kvm_run->exit_reason = KVM_EXIT_INTR;
2630 ++vcpu->stat.request_irq_exits;
2633 if (!need_resched())
2643 post_kvm_run_save(vcpu, kvm_run);
2650 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2657 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2658 kvm_vcpu_block(vcpu);
2663 if (vcpu->sigset_active)
2664 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2666 /* re-sync apic's tpr */
2667 if (!irqchip_in_kernel(vcpu->kvm))
2668 set_cr8(vcpu, kvm_run->cr8);
2670 if (vcpu->arch.pio.cur_count) {
2671 r = complete_pio(vcpu);
2675 #if CONFIG_HAS_IOMEM
2676 if (vcpu->mmio_needed) {
2677 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2678 vcpu->mmio_read_completed = 1;
2679 vcpu->mmio_needed = 0;
2680 r = emulate_instruction(vcpu, kvm_run,
2681 vcpu->arch.mmio_fault_cr2, 0,
2682 EMULTYPE_NO_DECODE);
2683 if (r == EMULATE_DO_MMIO) {
2685 * Read-modify-write. Back to userspace.
2692 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2693 kvm_x86_ops->cache_regs(vcpu);
2694 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2695 kvm_x86_ops->decache_regs(vcpu);
2698 r = __vcpu_run(vcpu, kvm_run);
2701 if (vcpu->sigset_active)
2702 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2708 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2712 kvm_x86_ops->cache_regs(vcpu);
2714 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2715 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2716 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2717 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2718 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2719 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2720 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2721 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
2722 #ifdef CONFIG_X86_64
2723 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2724 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2725 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2726 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2727 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2728 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2729 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2730 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
2733 regs->rip = vcpu->arch.rip;
2734 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2737 * Don't leak debug flags in case they were set for guest debugging
2739 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2740 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2747 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2751 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2752 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2753 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2754 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2755 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2756 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2757 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2758 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
2759 #ifdef CONFIG_X86_64
2760 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2761 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2762 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2763 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2764 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2765 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2766 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2767 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
2770 vcpu->arch.rip = regs->rip;
2771 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2773 kvm_x86_ops->decache_regs(vcpu);
2780 static void get_segment(struct kvm_vcpu *vcpu,
2781 struct kvm_segment *var, int seg)
2783 return kvm_x86_ops->get_segment(vcpu, var, seg);
2786 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2788 struct kvm_segment cs;
2790 get_segment(vcpu, &cs, VCPU_SREG_CS);
2794 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2796 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2797 struct kvm_sregs *sregs)
2799 struct descriptor_table dt;
2804 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2805 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2806 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2807 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2808 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2809 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2811 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2812 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2814 kvm_x86_ops->get_idt(vcpu, &dt);
2815 sregs->idt.limit = dt.limit;
2816 sregs->idt.base = dt.base;
2817 kvm_x86_ops->get_gdt(vcpu, &dt);
2818 sregs->gdt.limit = dt.limit;
2819 sregs->gdt.base = dt.base;
2821 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2822 sregs->cr0 = vcpu->arch.cr0;
2823 sregs->cr2 = vcpu->arch.cr2;
2824 sregs->cr3 = vcpu->arch.cr3;
2825 sregs->cr4 = vcpu->arch.cr4;
2826 sregs->cr8 = get_cr8(vcpu);
2827 sregs->efer = vcpu->arch.shadow_efer;
2828 sregs->apic_base = kvm_get_apic_base(vcpu);
2830 if (irqchip_in_kernel(vcpu->kvm)) {
2831 memset(sregs->interrupt_bitmap, 0,
2832 sizeof sregs->interrupt_bitmap);
2833 pending_vec = kvm_x86_ops->get_irq(vcpu);
2834 if (pending_vec >= 0)
2835 set_bit(pending_vec,
2836 (unsigned long *)sregs->interrupt_bitmap);
2838 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
2839 sizeof sregs->interrupt_bitmap);
2846 static void set_segment(struct kvm_vcpu *vcpu,
2847 struct kvm_segment *var, int seg)
2849 return kvm_x86_ops->set_segment(vcpu, var, seg);
2852 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2853 struct kvm_sregs *sregs)
2855 int mmu_reset_needed = 0;
2856 int i, pending_vec, max_bits;
2857 struct descriptor_table dt;
2861 dt.limit = sregs->idt.limit;
2862 dt.base = sregs->idt.base;
2863 kvm_x86_ops->set_idt(vcpu, &dt);
2864 dt.limit = sregs->gdt.limit;
2865 dt.base = sregs->gdt.base;
2866 kvm_x86_ops->set_gdt(vcpu, &dt);
2868 vcpu->arch.cr2 = sregs->cr2;
2869 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2870 vcpu->arch.cr3 = sregs->cr3;
2872 set_cr8(vcpu, sregs->cr8);
2874 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
2875 kvm_x86_ops->set_efer(vcpu, sregs->efer);
2876 kvm_set_apic_base(vcpu, sregs->apic_base);
2878 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2880 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
2881 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
2882 vcpu->arch.cr0 = sregs->cr0;
2884 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
2885 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
2886 if (!is_long_mode(vcpu) && is_pae(vcpu))
2887 load_pdptrs(vcpu, vcpu->arch.cr3);
2889 if (mmu_reset_needed)
2890 kvm_mmu_reset_context(vcpu);
2892 if (!irqchip_in_kernel(vcpu->kvm)) {
2893 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
2894 sizeof vcpu->arch.irq_pending);
2895 vcpu->arch.irq_summary = 0;
2896 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
2897 if (vcpu->arch.irq_pending[i])
2898 __set_bit(i, &vcpu->arch.irq_summary);
2900 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
2901 pending_vec = find_first_bit(
2902 (const unsigned long *)sregs->interrupt_bitmap,
2904 /* Only pending external irq is handled here */
2905 if (pending_vec < max_bits) {
2906 kvm_x86_ops->set_irq(vcpu, pending_vec);
2907 pr_debug("Set back pending irq %d\n",
2912 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2913 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2914 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2915 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2916 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2917 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2919 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2920 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2927 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2928 struct kvm_debug_guest *dbg)
2934 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
2942 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2943 * we have asm/x86/processor.h
2954 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2955 #ifdef CONFIG_X86_64
2956 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2958 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2963 * Translate a guest virtual address to a guest physical address.
2965 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2966 struct kvm_translation *tr)
2968 unsigned long vaddr = tr->linear_address;
2972 down_read(&vcpu->kvm->slots_lock);
2973 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
2974 up_read(&vcpu->kvm->slots_lock);
2975 tr->physical_address = gpa;
2976 tr->valid = gpa != UNMAPPED_GVA;
2984 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2986 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
2990 memcpy(fpu->fpr, fxsave->st_space, 128);
2991 fpu->fcw = fxsave->cwd;
2992 fpu->fsw = fxsave->swd;
2993 fpu->ftwx = fxsave->twd;
2994 fpu->last_opcode = fxsave->fop;
2995 fpu->last_ip = fxsave->rip;
2996 fpu->last_dp = fxsave->rdp;
2997 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3004 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3006 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3010 memcpy(fxsave->st_space, fpu->fpr, 128);
3011 fxsave->cwd = fpu->fcw;
3012 fxsave->swd = fpu->fsw;
3013 fxsave->twd = fpu->ftwx;
3014 fxsave->fop = fpu->last_opcode;
3015 fxsave->rip = fpu->last_ip;
3016 fxsave->rdp = fpu->last_dp;
3017 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3024 void fx_init(struct kvm_vcpu *vcpu)
3026 unsigned after_mxcsr_mask;
3028 /* Initialize guest FPU by resetting ours and saving into guest's */
3030 fx_save(&vcpu->arch.host_fx_image);
3032 fx_save(&vcpu->arch.guest_fx_image);
3033 fx_restore(&vcpu->arch.host_fx_image);
3036 vcpu->arch.cr0 |= X86_CR0_ET;
3037 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3038 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3039 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3040 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3042 EXPORT_SYMBOL_GPL(fx_init);
3044 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3046 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3049 vcpu->guest_fpu_loaded = 1;
3050 fx_save(&vcpu->arch.host_fx_image);
3051 fx_restore(&vcpu->arch.guest_fx_image);
3053 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3055 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3057 if (!vcpu->guest_fpu_loaded)
3060 vcpu->guest_fpu_loaded = 0;
3061 fx_save(&vcpu->arch.guest_fx_image);
3062 fx_restore(&vcpu->arch.host_fx_image);
3063 ++vcpu->stat.fpu_reload;
3065 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3067 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3069 kvm_x86_ops->vcpu_free(vcpu);
3072 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3075 return kvm_x86_ops->vcpu_create(kvm, id);
3078 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3082 /* We do fxsave: this must be aligned. */
3083 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3086 r = kvm_arch_vcpu_reset(vcpu);
3088 r = kvm_mmu_setup(vcpu);
3095 kvm_x86_ops->vcpu_free(vcpu);
3099 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3102 kvm_mmu_unload(vcpu);
3105 kvm_x86_ops->vcpu_free(vcpu);
3108 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3110 return kvm_x86_ops->vcpu_reset(vcpu);
3113 void kvm_arch_hardware_enable(void *garbage)
3115 kvm_x86_ops->hardware_enable(garbage);
3118 void kvm_arch_hardware_disable(void *garbage)
3120 kvm_x86_ops->hardware_disable(garbage);
3123 int kvm_arch_hardware_setup(void)
3125 return kvm_x86_ops->hardware_setup();
3128 void kvm_arch_hardware_unsetup(void)
3130 kvm_x86_ops->hardware_unsetup();
3133 void kvm_arch_check_processor_compat(void *rtn)
3135 kvm_x86_ops->check_processor_compatibility(rtn);
3138 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3144 BUG_ON(vcpu->kvm == NULL);
3147 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3148 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3149 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
3151 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
3153 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3158 vcpu->arch.pio_data = page_address(page);
3160 r = kvm_mmu_create(vcpu);
3162 goto fail_free_pio_data;
3164 if (irqchip_in_kernel(kvm)) {
3165 r = kvm_create_lapic(vcpu);
3167 goto fail_mmu_destroy;
3173 kvm_mmu_destroy(vcpu);
3175 free_page((unsigned long)vcpu->arch.pio_data);
3180 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3182 kvm_free_lapic(vcpu);
3183 kvm_mmu_destroy(vcpu);
3184 free_page((unsigned long)vcpu->arch.pio_data);
3187 struct kvm *kvm_arch_create_vm(void)
3189 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3192 return ERR_PTR(-ENOMEM);
3194 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3199 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3202 kvm_mmu_unload(vcpu);
3206 static void kvm_free_vcpus(struct kvm *kvm)
3211 * Unpin any mmu pages first.
3213 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3215 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3216 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3217 if (kvm->vcpus[i]) {
3218 kvm_arch_vcpu_free(kvm->vcpus[i]);
3219 kvm->vcpus[i] = NULL;
3225 void kvm_arch_destroy_vm(struct kvm *kvm)
3227 kfree(kvm->arch.vpic);
3228 kfree(kvm->arch.vioapic);
3229 kvm_free_vcpus(kvm);
3230 kvm_free_physmem(kvm);
3234 int kvm_arch_set_memory_region(struct kvm *kvm,
3235 struct kvm_userspace_memory_region *mem,
3236 struct kvm_memory_slot old,
3239 int npages = mem->memory_size >> PAGE_SHIFT;
3240 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3242 /*To keep backward compatibility with older userspace,
3243 *x86 needs to hanlde !user_alloc case.
3246 if (npages && !old.rmap) {
3247 down_write(¤t->mm->mmap_sem);
3248 memslot->userspace_addr = do_mmap(NULL, 0,
3250 PROT_READ | PROT_WRITE,
3251 MAP_SHARED | MAP_ANONYMOUS,
3253 up_write(¤t->mm->mmap_sem);
3255 if (IS_ERR((void *)memslot->userspace_addr))
3256 return PTR_ERR((void *)memslot->userspace_addr);
3258 if (!old.user_alloc && old.rmap) {
3261 down_write(¤t->mm->mmap_sem);
3262 ret = do_munmap(current->mm, old.userspace_addr,
3263 old.npages * PAGE_SIZE);
3264 up_write(¤t->mm->mmap_sem);
3267 "kvm_vm_ioctl_set_memory_region: "
3268 "failed to munmap memory\n");
3273 if (!kvm->arch.n_requested_mmu_pages) {
3274 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3275 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3278 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3279 kvm_flush_remote_tlbs(kvm);
3284 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3286 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3287 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3290 static void vcpu_kick_intr(void *info)
3293 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3294 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3298 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3300 int ipi_pcpu = vcpu->cpu;
3302 if (waitqueue_active(&vcpu->wq)) {
3303 wake_up_interruptible(&vcpu->wq);
3304 ++vcpu->stat.halt_wakeup;
3306 if (vcpu->guest_mode)
3307 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);