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>
21 #include <linux/clocksource.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>
33 #define MAX_IO_MSRS 256
34 #define CR0_RESERVED_BITS \
35 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
36 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
37 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
38 #define CR4_RESERVED_BITS \
39 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
40 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
41 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
42 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
44 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
46 * - enable syscall per default because its emulated by KVM
47 * - enable LME and LMA per default on 64 bit KVM
50 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
52 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
55 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
56 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
58 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
59 struct kvm_cpuid_entry2 __user *entries);
61 struct kvm_x86_ops *kvm_x86_ops;
63 struct kvm_stats_debugfs_item debugfs_entries[] = {
64 { "pf_fixed", VCPU_STAT(pf_fixed) },
65 { "pf_guest", VCPU_STAT(pf_guest) },
66 { "tlb_flush", VCPU_STAT(tlb_flush) },
67 { "invlpg", VCPU_STAT(invlpg) },
68 { "exits", VCPU_STAT(exits) },
69 { "io_exits", VCPU_STAT(io_exits) },
70 { "mmio_exits", VCPU_STAT(mmio_exits) },
71 { "signal_exits", VCPU_STAT(signal_exits) },
72 { "irq_window", VCPU_STAT(irq_window_exits) },
73 { "halt_exits", VCPU_STAT(halt_exits) },
74 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
75 { "hypercalls", VCPU_STAT(hypercalls) },
76 { "request_irq", VCPU_STAT(request_irq_exits) },
77 { "irq_exits", VCPU_STAT(irq_exits) },
78 { "host_state_reload", VCPU_STAT(host_state_reload) },
79 { "efer_reload", VCPU_STAT(efer_reload) },
80 { "fpu_reload", VCPU_STAT(fpu_reload) },
81 { "insn_emulation", VCPU_STAT(insn_emulation) },
82 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
83 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
84 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
85 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
86 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
87 { "mmu_flooded", VM_STAT(mmu_flooded) },
88 { "mmu_recycled", VM_STAT(mmu_recycled) },
89 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
90 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
95 unsigned long segment_base(u16 selector)
97 struct descriptor_table gdt;
98 struct desc_struct *d;
99 unsigned long table_base;
105 asm("sgdt %0" : "=m"(gdt));
106 table_base = gdt.base;
108 if (selector & 4) { /* from ldt */
111 asm("sldt %0" : "=g"(ldt_selector));
112 table_base = segment_base(ldt_selector);
114 d = (struct desc_struct *)(table_base + (selector & ~7));
115 v = d->base0 | ((unsigned long)d->base1 << 16) |
116 ((unsigned long)d->base2 << 24);
118 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
119 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
123 EXPORT_SYMBOL_GPL(segment_base);
125 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
127 if (irqchip_in_kernel(vcpu->kvm))
128 return vcpu->arch.apic_base;
130 return vcpu->arch.apic_base;
132 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
134 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
136 /* TODO: reserve bits check */
137 if (irqchip_in_kernel(vcpu->kvm))
138 kvm_lapic_set_base(vcpu, data);
140 vcpu->arch.apic_base = data;
142 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
144 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
146 WARN_ON(vcpu->arch.exception.pending);
147 vcpu->arch.exception.pending = true;
148 vcpu->arch.exception.has_error_code = false;
149 vcpu->arch.exception.nr = nr;
151 EXPORT_SYMBOL_GPL(kvm_queue_exception);
153 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
156 ++vcpu->stat.pf_guest;
157 if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
158 printk(KERN_DEBUG "kvm: inject_page_fault:"
159 " double fault 0x%lx\n", addr);
160 vcpu->arch.exception.nr = DF_VECTOR;
161 vcpu->arch.exception.error_code = 0;
164 vcpu->arch.cr2 = addr;
165 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
168 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
170 WARN_ON(vcpu->arch.exception.pending);
171 vcpu->arch.exception.pending = true;
172 vcpu->arch.exception.has_error_code = true;
173 vcpu->arch.exception.nr = nr;
174 vcpu->arch.exception.error_code = error_code;
176 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
178 static void __queue_exception(struct kvm_vcpu *vcpu)
180 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
181 vcpu->arch.exception.has_error_code,
182 vcpu->arch.exception.error_code);
186 * Load the pae pdptrs. Return true is they are all valid.
188 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
190 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
191 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
194 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
196 down_read(&vcpu->kvm->slots_lock);
197 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
198 offset * sizeof(u64), sizeof(pdpte));
203 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
204 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
211 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
213 up_read(&vcpu->kvm->slots_lock);
217 EXPORT_SYMBOL_GPL(load_pdptrs);
219 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
221 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
225 if (is_long_mode(vcpu) || !is_pae(vcpu))
228 down_read(&vcpu->kvm->slots_lock);
229 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
232 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
234 up_read(&vcpu->kvm->slots_lock);
239 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
241 if (cr0 & CR0_RESERVED_BITS) {
242 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
243 cr0, vcpu->arch.cr0);
244 kvm_inject_gp(vcpu, 0);
248 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
249 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
250 kvm_inject_gp(vcpu, 0);
254 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
255 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
256 "and a clear PE flag\n");
257 kvm_inject_gp(vcpu, 0);
261 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
263 if ((vcpu->arch.shadow_efer & EFER_LME)) {
267 printk(KERN_DEBUG "set_cr0: #GP, start paging "
268 "in long mode while PAE is disabled\n");
269 kvm_inject_gp(vcpu, 0);
272 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
274 printk(KERN_DEBUG "set_cr0: #GP, start paging "
275 "in long mode while CS.L == 1\n");
276 kvm_inject_gp(vcpu, 0);
282 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
283 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
285 kvm_inject_gp(vcpu, 0);
291 kvm_x86_ops->set_cr0(vcpu, cr0);
292 vcpu->arch.cr0 = cr0;
294 kvm_mmu_reset_context(vcpu);
297 EXPORT_SYMBOL_GPL(set_cr0);
299 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
301 set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
303 EXPORT_SYMBOL_GPL(lmsw);
305 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
307 if (cr4 & CR4_RESERVED_BITS) {
308 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
309 kvm_inject_gp(vcpu, 0);
313 if (is_long_mode(vcpu)) {
314 if (!(cr4 & X86_CR4_PAE)) {
315 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
317 kvm_inject_gp(vcpu, 0);
320 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
321 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
322 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
323 kvm_inject_gp(vcpu, 0);
327 if (cr4 & X86_CR4_VMXE) {
328 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
329 kvm_inject_gp(vcpu, 0);
332 kvm_x86_ops->set_cr4(vcpu, cr4);
333 vcpu->arch.cr4 = cr4;
334 kvm_mmu_reset_context(vcpu);
336 EXPORT_SYMBOL_GPL(set_cr4);
338 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
340 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
341 kvm_mmu_flush_tlb(vcpu);
345 if (is_long_mode(vcpu)) {
346 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
347 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
348 kvm_inject_gp(vcpu, 0);
353 if (cr3 & CR3_PAE_RESERVED_BITS) {
355 "set_cr3: #GP, reserved bits\n");
356 kvm_inject_gp(vcpu, 0);
359 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
360 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
362 kvm_inject_gp(vcpu, 0);
367 * We don't check reserved bits in nonpae mode, because
368 * this isn't enforced, and VMware depends on this.
372 down_read(&vcpu->kvm->slots_lock);
374 * Does the new cr3 value map to physical memory? (Note, we
375 * catch an invalid cr3 even in real-mode, because it would
376 * cause trouble later on when we turn on paging anyway.)
378 * A real CPU would silently accept an invalid cr3 and would
379 * attempt to use it - with largely undefined (and often hard
380 * to debug) behavior on the guest side.
382 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
383 kvm_inject_gp(vcpu, 0);
385 vcpu->arch.cr3 = cr3;
386 vcpu->arch.mmu.new_cr3(vcpu);
388 up_read(&vcpu->kvm->slots_lock);
390 EXPORT_SYMBOL_GPL(set_cr3);
392 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
394 if (cr8 & CR8_RESERVED_BITS) {
395 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
396 kvm_inject_gp(vcpu, 0);
399 if (irqchip_in_kernel(vcpu->kvm))
400 kvm_lapic_set_tpr(vcpu, cr8);
402 vcpu->arch.cr8 = cr8;
404 EXPORT_SYMBOL_GPL(set_cr8);
406 unsigned long get_cr8(struct kvm_vcpu *vcpu)
408 if (irqchip_in_kernel(vcpu->kvm))
409 return kvm_lapic_get_cr8(vcpu);
411 return vcpu->arch.cr8;
413 EXPORT_SYMBOL_GPL(get_cr8);
416 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
417 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
419 * This list is modified at module load time to reflect the
420 * capabilities of the host cpu.
422 static u32 msrs_to_save[] = {
423 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
426 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
428 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
429 MSR_IA32_PERF_STATUS,
432 static unsigned num_msrs_to_save;
434 static u32 emulated_msrs[] = {
435 MSR_IA32_MISC_ENABLE,
438 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
440 if (efer & efer_reserved_bits) {
441 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
443 kvm_inject_gp(vcpu, 0);
448 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
449 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
450 kvm_inject_gp(vcpu, 0);
454 kvm_x86_ops->set_efer(vcpu, efer);
457 efer |= vcpu->arch.shadow_efer & EFER_LMA;
459 vcpu->arch.shadow_efer = efer;
462 void kvm_enable_efer_bits(u64 mask)
464 efer_reserved_bits &= ~mask;
466 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
470 * Writes msr value into into the appropriate "register".
471 * Returns 0 on success, non-0 otherwise.
472 * Assumes vcpu_load() was already called.
474 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
476 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
480 * Adapt set_msr() to msr_io()'s calling convention
482 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
484 return kvm_set_msr(vcpu, index, *data);
487 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
490 struct kvm_wall_clock wc;
491 struct timespec wc_ts;
498 down_read(&kvm->slots_lock);
499 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
501 wc_ts = current_kernel_time();
502 wc.wc_sec = wc_ts.tv_sec;
503 wc.wc_nsec = wc_ts.tv_nsec;
504 wc.wc_version = version;
506 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
509 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
510 up_read(&kvm->slots_lock);
513 static void kvm_write_guest_time(struct kvm_vcpu *v)
517 struct kvm_vcpu_arch *vcpu = &v->arch;
520 if ((!vcpu->time_page))
523 /* Keep irq disabled to prevent changes to the clock */
524 local_irq_save(flags);
525 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
526 &vcpu->hv_clock.tsc_timestamp);
528 local_irq_restore(flags);
530 /* With all the info we got, fill in the values */
532 vcpu->hv_clock.system_time = ts.tv_nsec +
533 (NSEC_PER_SEC * (u64)ts.tv_sec);
535 * The interface expects us to write an even number signaling that the
536 * update is finished. Since the guest won't see the intermediate
537 * state, we just write "2" at the end
539 vcpu->hv_clock.version = 2;
541 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
543 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
544 sizeof(vcpu->hv_clock));
546 kunmap_atomic(shared_kaddr, KM_USER0);
548 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
552 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
556 set_efer(vcpu, data);
558 case MSR_IA32_MC0_STATUS:
559 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
562 case MSR_IA32_MCG_STATUS:
563 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
566 case MSR_IA32_MCG_CTL:
567 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
570 case MSR_IA32_UCODE_REV:
571 case MSR_IA32_UCODE_WRITE:
572 case 0x200 ... 0x2ff: /* MTRRs */
574 case MSR_IA32_APICBASE:
575 kvm_set_apic_base(vcpu, data);
577 case MSR_IA32_MISC_ENABLE:
578 vcpu->arch.ia32_misc_enable_msr = data;
580 case MSR_KVM_WALL_CLOCK:
581 vcpu->kvm->arch.wall_clock = data;
582 kvm_write_wall_clock(vcpu->kvm, data);
584 case MSR_KVM_SYSTEM_TIME: {
585 if (vcpu->arch.time_page) {
586 kvm_release_page_dirty(vcpu->arch.time_page);
587 vcpu->arch.time_page = NULL;
590 vcpu->arch.time = data;
592 /* we verify if the enable bit is set... */
596 /* ...but clean it before doing the actual write */
597 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
599 vcpu->arch.hv_clock.tsc_to_system_mul =
600 clocksource_khz2mult(tsc_khz, 22);
601 vcpu->arch.hv_clock.tsc_shift = 22;
603 down_read(¤t->mm->mmap_sem);
604 down_read(&vcpu->kvm->slots_lock);
605 vcpu->arch.time_page =
606 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
607 up_read(&vcpu->kvm->slots_lock);
608 up_read(¤t->mm->mmap_sem);
610 if (is_error_page(vcpu->arch.time_page)) {
611 kvm_release_page_clean(vcpu->arch.time_page);
612 vcpu->arch.time_page = NULL;
615 kvm_write_guest_time(vcpu);
619 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
624 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
628 * Reads an msr value (of 'msr_index') into 'pdata'.
629 * Returns 0 on success, non-0 otherwise.
630 * Assumes vcpu_load() was already called.
632 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
634 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
637 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
642 case 0xc0010010: /* SYSCFG */
643 case 0xc0010015: /* HWCR */
644 case MSR_IA32_PLATFORM_ID:
645 case MSR_IA32_P5_MC_ADDR:
646 case MSR_IA32_P5_MC_TYPE:
647 case MSR_IA32_MC0_CTL:
648 case MSR_IA32_MCG_STATUS:
649 case MSR_IA32_MCG_CAP:
650 case MSR_IA32_MCG_CTL:
651 case MSR_IA32_MC0_MISC:
652 case MSR_IA32_MC0_MISC+4:
653 case MSR_IA32_MC0_MISC+8:
654 case MSR_IA32_MC0_MISC+12:
655 case MSR_IA32_MC0_MISC+16:
656 case MSR_IA32_UCODE_REV:
657 case MSR_IA32_EBL_CR_POWERON:
660 case 0x200 ... 0x2ff:
663 case 0xcd: /* fsb frequency */
666 case MSR_IA32_APICBASE:
667 data = kvm_get_apic_base(vcpu);
669 case MSR_IA32_MISC_ENABLE:
670 data = vcpu->arch.ia32_misc_enable_msr;
672 case MSR_IA32_PERF_STATUS:
673 /* TSC increment by tick */
676 data |= (((uint64_t)4ULL) << 40);
679 data = vcpu->arch.shadow_efer;
681 case MSR_KVM_WALL_CLOCK:
682 data = vcpu->kvm->arch.wall_clock;
684 case MSR_KVM_SYSTEM_TIME:
685 data = vcpu->arch.time;
688 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
694 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
697 * Read or write a bunch of msrs. All parameters are kernel addresses.
699 * @return number of msrs set successfully.
701 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
702 struct kvm_msr_entry *entries,
703 int (*do_msr)(struct kvm_vcpu *vcpu,
704 unsigned index, u64 *data))
710 for (i = 0; i < msrs->nmsrs; ++i)
711 if (do_msr(vcpu, entries[i].index, &entries[i].data))
720 * Read or write a bunch of msrs. Parameters are user addresses.
722 * @return number of msrs set successfully.
724 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
725 int (*do_msr)(struct kvm_vcpu *vcpu,
726 unsigned index, u64 *data),
729 struct kvm_msrs msrs;
730 struct kvm_msr_entry *entries;
735 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
739 if (msrs.nmsrs >= MAX_IO_MSRS)
743 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
744 entries = vmalloc(size);
749 if (copy_from_user(entries, user_msrs->entries, size))
752 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
757 if (writeback && copy_to_user(user_msrs->entries, entries, size))
769 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
772 void decache_vcpus_on_cpu(int cpu)
775 struct kvm_vcpu *vcpu;
778 spin_lock(&kvm_lock);
779 list_for_each_entry(vm, &vm_list, vm_list)
780 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
785 * If the vcpu is locked, then it is running on some
786 * other cpu and therefore it is not cached on the
789 * If it's not locked, check the last cpu it executed
792 if (mutex_trylock(&vcpu->mutex)) {
793 if (vcpu->cpu == cpu) {
794 kvm_x86_ops->vcpu_decache(vcpu);
797 mutex_unlock(&vcpu->mutex);
800 spin_unlock(&kvm_lock);
803 int kvm_dev_ioctl_check_extension(long ext)
808 case KVM_CAP_IRQCHIP:
810 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
811 case KVM_CAP_USER_MEMORY:
812 case KVM_CAP_SET_TSS_ADDR:
813 case KVM_CAP_EXT_CPUID:
814 case KVM_CAP_CLOCKSOURCE:
818 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
820 case KVM_CAP_NR_VCPUS:
823 case KVM_CAP_NR_MEMSLOTS:
824 r = KVM_MEMORY_SLOTS;
834 long kvm_arch_dev_ioctl(struct file *filp,
835 unsigned int ioctl, unsigned long arg)
837 void __user *argp = (void __user *)arg;
841 case KVM_GET_MSR_INDEX_LIST: {
842 struct kvm_msr_list __user *user_msr_list = argp;
843 struct kvm_msr_list msr_list;
847 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
850 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
851 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
854 if (n < num_msrs_to_save)
857 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
858 num_msrs_to_save * sizeof(u32)))
860 if (copy_to_user(user_msr_list->indices
861 + num_msrs_to_save * sizeof(u32),
863 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
868 case KVM_GET_SUPPORTED_CPUID: {
869 struct kvm_cpuid2 __user *cpuid_arg = argp;
870 struct kvm_cpuid2 cpuid;
873 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
875 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
881 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
893 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
895 kvm_x86_ops->vcpu_load(vcpu, cpu);
896 kvm_write_guest_time(vcpu);
899 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
901 kvm_x86_ops->vcpu_put(vcpu);
902 kvm_put_guest_fpu(vcpu);
905 static int is_efer_nx(void)
909 rdmsrl(MSR_EFER, efer);
910 return efer & EFER_NX;
913 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
916 struct kvm_cpuid_entry2 *e, *entry;
919 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
920 e = &vcpu->arch.cpuid_entries[i];
921 if (e->function == 0x80000001) {
926 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
927 entry->edx &= ~(1 << 20);
928 printk(KERN_INFO "kvm: guest NX capability removed\n");
932 /* when an old userspace process fills a new kernel module */
933 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
934 struct kvm_cpuid *cpuid,
935 struct kvm_cpuid_entry __user *entries)
938 struct kvm_cpuid_entry *cpuid_entries;
941 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
944 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
948 if (copy_from_user(cpuid_entries, entries,
949 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
951 for (i = 0; i < cpuid->nent; i++) {
952 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
953 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
954 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
955 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
956 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
957 vcpu->arch.cpuid_entries[i].index = 0;
958 vcpu->arch.cpuid_entries[i].flags = 0;
959 vcpu->arch.cpuid_entries[i].padding[0] = 0;
960 vcpu->arch.cpuid_entries[i].padding[1] = 0;
961 vcpu->arch.cpuid_entries[i].padding[2] = 0;
963 vcpu->arch.cpuid_nent = cpuid->nent;
964 cpuid_fix_nx_cap(vcpu);
968 vfree(cpuid_entries);
973 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
974 struct kvm_cpuid2 *cpuid,
975 struct kvm_cpuid_entry2 __user *entries)
980 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
983 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
984 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
986 vcpu->arch.cpuid_nent = cpuid->nent;
993 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
994 struct kvm_cpuid2 *cpuid,
995 struct kvm_cpuid_entry2 __user *entries)
1000 if (cpuid->nent < vcpu->arch.cpuid_nent)
1003 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1004 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1009 cpuid->nent = vcpu->arch.cpuid_nent;
1013 static inline u32 bit(int bitno)
1015 return 1 << (bitno & 31);
1018 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1021 entry->function = function;
1022 entry->index = index;
1023 cpuid_count(entry->function, entry->index,
1024 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1028 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1029 u32 index, int *nent, int maxnent)
1031 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1032 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1033 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1034 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1035 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1036 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1037 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1038 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1039 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1040 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1041 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1042 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1043 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1044 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1045 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1046 bit(X86_FEATURE_PGE) |
1047 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1048 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1049 bit(X86_FEATURE_SYSCALL) |
1050 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1051 #ifdef CONFIG_X86_64
1052 bit(X86_FEATURE_LM) |
1054 bit(X86_FEATURE_MMXEXT) |
1055 bit(X86_FEATURE_3DNOWEXT) |
1056 bit(X86_FEATURE_3DNOW);
1057 const u32 kvm_supported_word3_x86_features =
1058 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1059 const u32 kvm_supported_word6_x86_features =
1060 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1062 /* all func 2 cpuid_count() should be called on the same cpu */
1064 do_cpuid_1_ent(entry, function, index);
1069 entry->eax = min(entry->eax, (u32)0xb);
1072 entry->edx &= kvm_supported_word0_x86_features;
1073 entry->ecx &= kvm_supported_word3_x86_features;
1075 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1076 * may return different values. This forces us to get_cpu() before
1077 * issuing the first command, and also to emulate this annoying behavior
1078 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1080 int t, times = entry->eax & 0xff;
1082 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1083 for (t = 1; t < times && *nent < maxnent; ++t) {
1084 do_cpuid_1_ent(&entry[t], function, 0);
1085 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1090 /* function 4 and 0xb have additional index. */
1094 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1095 /* read more entries until cache_type is zero */
1096 for (i = 1; *nent < maxnent; ++i) {
1097 cache_type = entry[i - 1].eax & 0x1f;
1100 do_cpuid_1_ent(&entry[i], function, i);
1102 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1110 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1111 /* read more entries until level_type is zero */
1112 for (i = 1; *nent < maxnent; ++i) {
1113 level_type = entry[i - 1].ecx & 0xff;
1116 do_cpuid_1_ent(&entry[i], function, i);
1118 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1124 entry->eax = min(entry->eax, 0x8000001a);
1127 entry->edx &= kvm_supported_word1_x86_features;
1128 entry->ecx &= kvm_supported_word6_x86_features;
1134 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1135 struct kvm_cpuid_entry2 __user *entries)
1137 struct kvm_cpuid_entry2 *cpuid_entries;
1138 int limit, nent = 0, r = -E2BIG;
1141 if (cpuid->nent < 1)
1144 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1148 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1149 limit = cpuid_entries[0].eax;
1150 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1151 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1152 &nent, cpuid->nent);
1154 if (nent >= cpuid->nent)
1157 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1158 limit = cpuid_entries[nent - 1].eax;
1159 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1160 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1161 &nent, cpuid->nent);
1163 if (copy_to_user(entries, cpuid_entries,
1164 nent * sizeof(struct kvm_cpuid_entry2)))
1170 vfree(cpuid_entries);
1175 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1176 struct kvm_lapic_state *s)
1179 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1185 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1186 struct kvm_lapic_state *s)
1189 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1190 kvm_apic_post_state_restore(vcpu);
1196 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1197 struct kvm_interrupt *irq)
1199 if (irq->irq < 0 || irq->irq >= 256)
1201 if (irqchip_in_kernel(vcpu->kvm))
1205 set_bit(irq->irq, vcpu->arch.irq_pending);
1206 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1213 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1214 struct kvm_tpr_access_ctl *tac)
1218 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1222 long kvm_arch_vcpu_ioctl(struct file *filp,
1223 unsigned int ioctl, unsigned long arg)
1225 struct kvm_vcpu *vcpu = filp->private_data;
1226 void __user *argp = (void __user *)arg;
1230 case KVM_GET_LAPIC: {
1231 struct kvm_lapic_state lapic;
1233 memset(&lapic, 0, sizeof lapic);
1234 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1238 if (copy_to_user(argp, &lapic, sizeof lapic))
1243 case KVM_SET_LAPIC: {
1244 struct kvm_lapic_state lapic;
1247 if (copy_from_user(&lapic, argp, sizeof lapic))
1249 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1255 case KVM_INTERRUPT: {
1256 struct kvm_interrupt irq;
1259 if (copy_from_user(&irq, argp, sizeof irq))
1261 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1267 case KVM_SET_CPUID: {
1268 struct kvm_cpuid __user *cpuid_arg = argp;
1269 struct kvm_cpuid cpuid;
1272 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1274 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1279 case KVM_SET_CPUID2: {
1280 struct kvm_cpuid2 __user *cpuid_arg = argp;
1281 struct kvm_cpuid2 cpuid;
1284 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1286 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1287 cpuid_arg->entries);
1292 case KVM_GET_CPUID2: {
1293 struct kvm_cpuid2 __user *cpuid_arg = argp;
1294 struct kvm_cpuid2 cpuid;
1297 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1299 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1300 cpuid_arg->entries);
1304 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1310 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1313 r = msr_io(vcpu, argp, do_set_msr, 0);
1315 case KVM_TPR_ACCESS_REPORTING: {
1316 struct kvm_tpr_access_ctl tac;
1319 if (copy_from_user(&tac, argp, sizeof tac))
1321 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1325 if (copy_to_user(argp, &tac, sizeof tac))
1330 case KVM_SET_VAPIC_ADDR: {
1331 struct kvm_vapic_addr va;
1334 if (!irqchip_in_kernel(vcpu->kvm))
1337 if (copy_from_user(&va, argp, sizeof va))
1340 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1350 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1354 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1356 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1360 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1361 u32 kvm_nr_mmu_pages)
1363 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1366 down_write(&kvm->slots_lock);
1368 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1369 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1371 up_write(&kvm->slots_lock);
1375 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1377 return kvm->arch.n_alloc_mmu_pages;
1380 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1383 struct kvm_mem_alias *alias;
1385 for (i = 0; i < kvm->arch.naliases; ++i) {
1386 alias = &kvm->arch.aliases[i];
1387 if (gfn >= alias->base_gfn
1388 && gfn < alias->base_gfn + alias->npages)
1389 return alias->target_gfn + gfn - alias->base_gfn;
1395 * Set a new alias region. Aliases map a portion of physical memory into
1396 * another portion. This is useful for memory windows, for example the PC
1399 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1400 struct kvm_memory_alias *alias)
1403 struct kvm_mem_alias *p;
1406 /* General sanity checks */
1407 if (alias->memory_size & (PAGE_SIZE - 1))
1409 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1411 if (alias->slot >= KVM_ALIAS_SLOTS)
1413 if (alias->guest_phys_addr + alias->memory_size
1414 < alias->guest_phys_addr)
1416 if (alias->target_phys_addr + alias->memory_size
1417 < alias->target_phys_addr)
1420 down_write(&kvm->slots_lock);
1422 p = &kvm->arch.aliases[alias->slot];
1423 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1424 p->npages = alias->memory_size >> PAGE_SHIFT;
1425 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1427 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1428 if (kvm->arch.aliases[n - 1].npages)
1430 kvm->arch.naliases = n;
1432 kvm_mmu_zap_all(kvm);
1434 up_write(&kvm->slots_lock);
1442 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1447 switch (chip->chip_id) {
1448 case KVM_IRQCHIP_PIC_MASTER:
1449 memcpy(&chip->chip.pic,
1450 &pic_irqchip(kvm)->pics[0],
1451 sizeof(struct kvm_pic_state));
1453 case KVM_IRQCHIP_PIC_SLAVE:
1454 memcpy(&chip->chip.pic,
1455 &pic_irqchip(kvm)->pics[1],
1456 sizeof(struct kvm_pic_state));
1458 case KVM_IRQCHIP_IOAPIC:
1459 memcpy(&chip->chip.ioapic,
1460 ioapic_irqchip(kvm),
1461 sizeof(struct kvm_ioapic_state));
1470 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1475 switch (chip->chip_id) {
1476 case KVM_IRQCHIP_PIC_MASTER:
1477 memcpy(&pic_irqchip(kvm)->pics[0],
1479 sizeof(struct kvm_pic_state));
1481 case KVM_IRQCHIP_PIC_SLAVE:
1482 memcpy(&pic_irqchip(kvm)->pics[1],
1484 sizeof(struct kvm_pic_state));
1486 case KVM_IRQCHIP_IOAPIC:
1487 memcpy(ioapic_irqchip(kvm),
1489 sizeof(struct kvm_ioapic_state));
1495 kvm_pic_update_irq(pic_irqchip(kvm));
1500 * Get (and clear) the dirty memory log for a memory slot.
1502 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1503 struct kvm_dirty_log *log)
1507 struct kvm_memory_slot *memslot;
1510 down_write(&kvm->slots_lock);
1512 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1516 /* If nothing is dirty, don't bother messing with page tables. */
1518 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1519 kvm_flush_remote_tlbs(kvm);
1520 memslot = &kvm->memslots[log->slot];
1521 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1522 memset(memslot->dirty_bitmap, 0, n);
1526 up_write(&kvm->slots_lock);
1530 long kvm_arch_vm_ioctl(struct file *filp,
1531 unsigned int ioctl, unsigned long arg)
1533 struct kvm *kvm = filp->private_data;
1534 void __user *argp = (void __user *)arg;
1538 case KVM_SET_TSS_ADDR:
1539 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1543 case KVM_SET_MEMORY_REGION: {
1544 struct kvm_memory_region kvm_mem;
1545 struct kvm_userspace_memory_region kvm_userspace_mem;
1548 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1550 kvm_userspace_mem.slot = kvm_mem.slot;
1551 kvm_userspace_mem.flags = kvm_mem.flags;
1552 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1553 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1554 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1559 case KVM_SET_NR_MMU_PAGES:
1560 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1564 case KVM_GET_NR_MMU_PAGES:
1565 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1567 case KVM_SET_MEMORY_ALIAS: {
1568 struct kvm_memory_alias alias;
1571 if (copy_from_user(&alias, argp, sizeof alias))
1573 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1578 case KVM_CREATE_IRQCHIP:
1580 kvm->arch.vpic = kvm_create_pic(kvm);
1581 if (kvm->arch.vpic) {
1582 r = kvm_ioapic_init(kvm);
1584 kfree(kvm->arch.vpic);
1585 kvm->arch.vpic = NULL;
1591 case KVM_IRQ_LINE: {
1592 struct kvm_irq_level irq_event;
1595 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1597 if (irqchip_in_kernel(kvm)) {
1598 mutex_lock(&kvm->lock);
1599 if (irq_event.irq < 16)
1600 kvm_pic_set_irq(pic_irqchip(kvm),
1603 kvm_ioapic_set_irq(kvm->arch.vioapic,
1606 mutex_unlock(&kvm->lock);
1611 case KVM_GET_IRQCHIP: {
1612 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1613 struct kvm_irqchip chip;
1616 if (copy_from_user(&chip, argp, sizeof chip))
1619 if (!irqchip_in_kernel(kvm))
1621 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1625 if (copy_to_user(argp, &chip, sizeof chip))
1630 case KVM_SET_IRQCHIP: {
1631 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1632 struct kvm_irqchip chip;
1635 if (copy_from_user(&chip, argp, sizeof chip))
1638 if (!irqchip_in_kernel(kvm))
1640 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1653 static void kvm_init_msr_list(void)
1658 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1659 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1662 msrs_to_save[j] = msrs_to_save[i];
1665 num_msrs_to_save = j;
1669 * Only apic need an MMIO device hook, so shortcut now..
1671 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1674 struct kvm_io_device *dev;
1676 if (vcpu->arch.apic) {
1677 dev = &vcpu->arch.apic->dev;
1678 if (dev->in_range(dev, addr))
1685 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1688 struct kvm_io_device *dev;
1690 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1692 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1696 int emulator_read_std(unsigned long addr,
1699 struct kvm_vcpu *vcpu)
1702 int r = X86EMUL_CONTINUE;
1704 down_read(&vcpu->kvm->slots_lock);
1706 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1707 unsigned offset = addr & (PAGE_SIZE-1);
1708 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1711 if (gpa == UNMAPPED_GVA) {
1712 r = X86EMUL_PROPAGATE_FAULT;
1715 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1717 r = X86EMUL_UNHANDLEABLE;
1726 up_read(&vcpu->kvm->slots_lock);
1729 EXPORT_SYMBOL_GPL(emulator_read_std);
1731 static int emulator_read_emulated(unsigned long addr,
1734 struct kvm_vcpu *vcpu)
1736 struct kvm_io_device *mmio_dev;
1739 if (vcpu->mmio_read_completed) {
1740 memcpy(val, vcpu->mmio_data, bytes);
1741 vcpu->mmio_read_completed = 0;
1742 return X86EMUL_CONTINUE;
1745 down_read(&vcpu->kvm->slots_lock);
1746 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1747 up_read(&vcpu->kvm->slots_lock);
1749 /* For APIC access vmexit */
1750 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1753 if (emulator_read_std(addr, val, bytes, vcpu)
1754 == X86EMUL_CONTINUE)
1755 return X86EMUL_CONTINUE;
1756 if (gpa == UNMAPPED_GVA)
1757 return X86EMUL_PROPAGATE_FAULT;
1761 * Is this MMIO handled locally?
1763 mutex_lock(&vcpu->kvm->lock);
1764 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1766 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1767 mutex_unlock(&vcpu->kvm->lock);
1768 return X86EMUL_CONTINUE;
1770 mutex_unlock(&vcpu->kvm->lock);
1772 vcpu->mmio_needed = 1;
1773 vcpu->mmio_phys_addr = gpa;
1774 vcpu->mmio_size = bytes;
1775 vcpu->mmio_is_write = 0;
1777 return X86EMUL_UNHANDLEABLE;
1780 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1781 const void *val, int bytes)
1785 down_read(&vcpu->kvm->slots_lock);
1786 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1788 up_read(&vcpu->kvm->slots_lock);
1791 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1792 up_read(&vcpu->kvm->slots_lock);
1796 static int emulator_write_emulated_onepage(unsigned long addr,
1799 struct kvm_vcpu *vcpu)
1801 struct kvm_io_device *mmio_dev;
1804 down_read(&vcpu->kvm->slots_lock);
1805 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1806 up_read(&vcpu->kvm->slots_lock);
1808 if (gpa == UNMAPPED_GVA) {
1809 kvm_inject_page_fault(vcpu, addr, 2);
1810 return X86EMUL_PROPAGATE_FAULT;
1813 /* For APIC access vmexit */
1814 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1817 if (emulator_write_phys(vcpu, gpa, val, bytes))
1818 return X86EMUL_CONTINUE;
1822 * Is this MMIO handled locally?
1824 mutex_lock(&vcpu->kvm->lock);
1825 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1827 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1828 mutex_unlock(&vcpu->kvm->lock);
1829 return X86EMUL_CONTINUE;
1831 mutex_unlock(&vcpu->kvm->lock);
1833 vcpu->mmio_needed = 1;
1834 vcpu->mmio_phys_addr = gpa;
1835 vcpu->mmio_size = bytes;
1836 vcpu->mmio_is_write = 1;
1837 memcpy(vcpu->mmio_data, val, bytes);
1839 return X86EMUL_CONTINUE;
1842 int emulator_write_emulated(unsigned long addr,
1845 struct kvm_vcpu *vcpu)
1847 /* Crossing a page boundary? */
1848 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1851 now = -addr & ~PAGE_MASK;
1852 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1853 if (rc != X86EMUL_CONTINUE)
1859 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1861 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1863 static int emulator_cmpxchg_emulated(unsigned long addr,
1867 struct kvm_vcpu *vcpu)
1869 static int reported;
1873 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1875 #ifndef CONFIG_X86_64
1876 /* guests cmpxchg8b have to be emulated atomically */
1883 down_read(&vcpu->kvm->slots_lock);
1884 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1886 if (gpa == UNMAPPED_GVA ||
1887 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1890 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1895 down_read(¤t->mm->mmap_sem);
1896 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1897 up_read(¤t->mm->mmap_sem);
1899 kaddr = kmap_atomic(page, KM_USER0);
1900 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
1901 kunmap_atomic(kaddr, KM_USER0);
1902 kvm_release_page_dirty(page);
1904 up_read(&vcpu->kvm->slots_lock);
1908 return emulator_write_emulated(addr, new, bytes, vcpu);
1911 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1913 return kvm_x86_ops->get_segment_base(vcpu, seg);
1916 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1918 return X86EMUL_CONTINUE;
1921 int emulate_clts(struct kvm_vcpu *vcpu)
1923 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
1924 return X86EMUL_CONTINUE;
1927 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
1929 struct kvm_vcpu *vcpu = ctxt->vcpu;
1933 *dest = kvm_x86_ops->get_dr(vcpu, dr);
1934 return X86EMUL_CONTINUE;
1936 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
1937 return X86EMUL_UNHANDLEABLE;
1941 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1943 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1946 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1948 /* FIXME: better handling */
1949 return X86EMUL_UNHANDLEABLE;
1951 return X86EMUL_CONTINUE;
1954 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
1956 static int reported;
1958 unsigned long rip = vcpu->arch.rip;
1959 unsigned long rip_linear;
1961 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
1966 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
1968 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1969 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1972 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
1974 static struct x86_emulate_ops emulate_ops = {
1975 .read_std = emulator_read_std,
1976 .read_emulated = emulator_read_emulated,
1977 .write_emulated = emulator_write_emulated,
1978 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1981 int emulate_instruction(struct kvm_vcpu *vcpu,
1982 struct kvm_run *run,
1988 struct decode_cache *c;
1990 vcpu->arch.mmio_fault_cr2 = cr2;
1991 kvm_x86_ops->cache_regs(vcpu);
1993 vcpu->mmio_is_write = 0;
1994 vcpu->arch.pio.string = 0;
1996 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
1998 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2000 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2001 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2002 vcpu->arch.emulate_ctxt.mode =
2003 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
2004 ? X86EMUL_MODE_REAL : cs_l
2005 ? X86EMUL_MODE_PROT64 : cs_db
2006 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2008 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
2009 vcpu->arch.emulate_ctxt.cs_base = 0;
2010 vcpu->arch.emulate_ctxt.ds_base = 0;
2011 vcpu->arch.emulate_ctxt.es_base = 0;
2012 vcpu->arch.emulate_ctxt.ss_base = 0;
2014 vcpu->arch.emulate_ctxt.cs_base =
2015 get_segment_base(vcpu, VCPU_SREG_CS);
2016 vcpu->arch.emulate_ctxt.ds_base =
2017 get_segment_base(vcpu, VCPU_SREG_DS);
2018 vcpu->arch.emulate_ctxt.es_base =
2019 get_segment_base(vcpu, VCPU_SREG_ES);
2020 vcpu->arch.emulate_ctxt.ss_base =
2021 get_segment_base(vcpu, VCPU_SREG_SS);
2024 vcpu->arch.emulate_ctxt.gs_base =
2025 get_segment_base(vcpu, VCPU_SREG_GS);
2026 vcpu->arch.emulate_ctxt.fs_base =
2027 get_segment_base(vcpu, VCPU_SREG_FS);
2029 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2031 /* Reject the instructions other than VMCALL/VMMCALL when
2032 * try to emulate invalid opcode */
2033 c = &vcpu->arch.emulate_ctxt.decode;
2034 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2035 (!(c->twobyte && c->b == 0x01 &&
2036 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2037 c->modrm_mod == 3 && c->modrm_rm == 1)))
2038 return EMULATE_FAIL;
2040 ++vcpu->stat.insn_emulation;
2042 ++vcpu->stat.insn_emulation_fail;
2043 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2044 return EMULATE_DONE;
2045 return EMULATE_FAIL;
2049 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2051 if (vcpu->arch.pio.string)
2052 return EMULATE_DO_MMIO;
2054 if ((r || vcpu->mmio_is_write) && run) {
2055 run->exit_reason = KVM_EXIT_MMIO;
2056 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2057 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2058 run->mmio.len = vcpu->mmio_size;
2059 run->mmio.is_write = vcpu->mmio_is_write;
2063 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2064 return EMULATE_DONE;
2065 if (!vcpu->mmio_needed) {
2066 kvm_report_emulation_failure(vcpu, "mmio");
2067 return EMULATE_FAIL;
2069 return EMULATE_DO_MMIO;
2072 kvm_x86_ops->decache_regs(vcpu);
2073 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
2075 if (vcpu->mmio_is_write) {
2076 vcpu->mmio_needed = 0;
2077 return EMULATE_DO_MMIO;
2080 return EMULATE_DONE;
2082 EXPORT_SYMBOL_GPL(emulate_instruction);
2084 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2088 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2089 if (vcpu->arch.pio.guest_pages[i]) {
2090 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2091 vcpu->arch.pio.guest_pages[i] = NULL;
2095 static int pio_copy_data(struct kvm_vcpu *vcpu)
2097 void *p = vcpu->arch.pio_data;
2100 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
2102 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
2105 free_pio_guest_pages(vcpu);
2108 q += vcpu->arch.pio.guest_page_offset;
2109 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2110 if (vcpu->arch.pio.in)
2111 memcpy(q, p, bytes);
2113 memcpy(p, q, bytes);
2114 q -= vcpu->arch.pio.guest_page_offset;
2116 free_pio_guest_pages(vcpu);
2120 int complete_pio(struct kvm_vcpu *vcpu)
2122 struct kvm_pio_request *io = &vcpu->arch.pio;
2126 kvm_x86_ops->cache_regs(vcpu);
2130 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
2134 r = pio_copy_data(vcpu);
2136 kvm_x86_ops->cache_regs(vcpu);
2143 delta *= io->cur_count;
2145 * The size of the register should really depend on
2146 * current address size.
2148 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2154 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2156 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2159 kvm_x86_ops->decache_regs(vcpu);
2161 io->count -= io->cur_count;
2167 static void kernel_pio(struct kvm_io_device *pio_dev,
2168 struct kvm_vcpu *vcpu,
2171 /* TODO: String I/O for in kernel device */
2173 mutex_lock(&vcpu->kvm->lock);
2174 if (vcpu->arch.pio.in)
2175 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2176 vcpu->arch.pio.size,
2179 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2180 vcpu->arch.pio.size,
2182 mutex_unlock(&vcpu->kvm->lock);
2185 static void pio_string_write(struct kvm_io_device *pio_dev,
2186 struct kvm_vcpu *vcpu)
2188 struct kvm_pio_request *io = &vcpu->arch.pio;
2189 void *pd = vcpu->arch.pio_data;
2192 mutex_lock(&vcpu->kvm->lock);
2193 for (i = 0; i < io->cur_count; i++) {
2194 kvm_iodevice_write(pio_dev, io->port,
2199 mutex_unlock(&vcpu->kvm->lock);
2202 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2205 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2208 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2209 int size, unsigned port)
2211 struct kvm_io_device *pio_dev;
2213 vcpu->run->exit_reason = KVM_EXIT_IO;
2214 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2215 vcpu->run->io.size = vcpu->arch.pio.size = size;
2216 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2217 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2218 vcpu->run->io.port = vcpu->arch.pio.port = port;
2219 vcpu->arch.pio.in = in;
2220 vcpu->arch.pio.string = 0;
2221 vcpu->arch.pio.down = 0;
2222 vcpu->arch.pio.guest_page_offset = 0;
2223 vcpu->arch.pio.rep = 0;
2225 kvm_x86_ops->cache_regs(vcpu);
2226 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2227 kvm_x86_ops->decache_regs(vcpu);
2229 kvm_x86_ops->skip_emulated_instruction(vcpu);
2231 pio_dev = vcpu_find_pio_dev(vcpu, port);
2233 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2239 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2241 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2242 int size, unsigned long count, int down,
2243 gva_t address, int rep, unsigned port)
2245 unsigned now, in_page;
2249 struct kvm_io_device *pio_dev;
2251 vcpu->run->exit_reason = KVM_EXIT_IO;
2252 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2253 vcpu->run->io.size = vcpu->arch.pio.size = size;
2254 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2255 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2256 vcpu->run->io.port = vcpu->arch.pio.port = port;
2257 vcpu->arch.pio.in = in;
2258 vcpu->arch.pio.string = 1;
2259 vcpu->arch.pio.down = down;
2260 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2261 vcpu->arch.pio.rep = rep;
2264 kvm_x86_ops->skip_emulated_instruction(vcpu);
2269 in_page = PAGE_SIZE - offset_in_page(address);
2271 in_page = offset_in_page(address) + size;
2272 now = min(count, (unsigned long)in_page / size);
2275 * String I/O straddles page boundary. Pin two guest pages
2276 * so that we satisfy atomicity constraints. Do just one
2277 * transaction to avoid complexity.
2284 * String I/O in reverse. Yuck. Kill the guest, fix later.
2286 pr_unimpl(vcpu, "guest string pio down\n");
2287 kvm_inject_gp(vcpu, 0);
2290 vcpu->run->io.count = now;
2291 vcpu->arch.pio.cur_count = now;
2293 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2294 kvm_x86_ops->skip_emulated_instruction(vcpu);
2296 for (i = 0; i < nr_pages; ++i) {
2297 down_read(&vcpu->kvm->slots_lock);
2298 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2299 vcpu->arch.pio.guest_pages[i] = page;
2300 up_read(&vcpu->kvm->slots_lock);
2302 kvm_inject_gp(vcpu, 0);
2303 free_pio_guest_pages(vcpu);
2308 pio_dev = vcpu_find_pio_dev(vcpu, port);
2309 if (!vcpu->arch.pio.in) {
2310 /* string PIO write */
2311 ret = pio_copy_data(vcpu);
2312 if (ret >= 0 && pio_dev) {
2313 pio_string_write(pio_dev, vcpu);
2315 if (vcpu->arch.pio.count == 0)
2319 pr_unimpl(vcpu, "no string pio read support yet, "
2320 "port %x size %d count %ld\n",
2325 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2327 int kvm_arch_init(void *opaque)
2330 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2333 printk(KERN_ERR "kvm: already loaded the other module\n");
2338 if (!ops->cpu_has_kvm_support()) {
2339 printk(KERN_ERR "kvm: no hardware support\n");
2343 if (ops->disabled_by_bios()) {
2344 printk(KERN_ERR "kvm: disabled by bios\n");
2349 r = kvm_mmu_module_init();
2353 kvm_init_msr_list();
2356 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2363 void kvm_arch_exit(void)
2366 kvm_mmu_module_exit();
2369 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2371 ++vcpu->stat.halt_exits;
2372 if (irqchip_in_kernel(vcpu->kvm)) {
2373 vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
2374 kvm_vcpu_block(vcpu);
2375 if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
2379 vcpu->run->exit_reason = KVM_EXIT_HLT;
2383 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2385 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2387 unsigned long nr, a0, a1, a2, a3, ret;
2389 kvm_x86_ops->cache_regs(vcpu);
2391 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2392 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2393 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2394 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2395 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2397 if (!is_long_mode(vcpu)) {
2406 case KVM_HC_VAPIC_POLL_IRQ:
2413 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2414 kvm_x86_ops->decache_regs(vcpu);
2415 ++vcpu->stat.hypercalls;
2418 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2420 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2422 char instruction[3];
2427 * Blow out the MMU to ensure that no other VCPU has an active mapping
2428 * to ensure that the updated hypercall appears atomically across all
2431 kvm_mmu_zap_all(vcpu->kvm);
2433 kvm_x86_ops->cache_regs(vcpu);
2434 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2435 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2436 != X86EMUL_CONTINUE)
2442 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2444 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2447 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2449 struct descriptor_table dt = { limit, base };
2451 kvm_x86_ops->set_gdt(vcpu, &dt);
2454 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2456 struct descriptor_table dt = { limit, base };
2458 kvm_x86_ops->set_idt(vcpu, &dt);
2461 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2462 unsigned long *rflags)
2465 *rflags = kvm_x86_ops->get_rflags(vcpu);
2468 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2470 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2473 return vcpu->arch.cr0;
2475 return vcpu->arch.cr2;
2477 return vcpu->arch.cr3;
2479 return vcpu->arch.cr4;
2481 return get_cr8(vcpu);
2483 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2488 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2489 unsigned long *rflags)
2493 set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2494 *rflags = kvm_x86_ops->get_rflags(vcpu);
2497 vcpu->arch.cr2 = val;
2503 set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2506 set_cr8(vcpu, val & 0xfUL);
2509 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
2513 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2515 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2516 int j, nent = vcpu->arch.cpuid_nent;
2518 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2519 /* when no next entry is found, the current entry[i] is reselected */
2520 for (j = i + 1; j == i; j = (j + 1) % nent) {
2521 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2522 if (ej->function == e->function) {
2523 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2527 return 0; /* silence gcc, even though control never reaches here */
2530 /* find an entry with matching function, matching index (if needed), and that
2531 * should be read next (if it's stateful) */
2532 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2533 u32 function, u32 index)
2535 if (e->function != function)
2537 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2539 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2540 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2545 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2548 u32 function, index;
2549 struct kvm_cpuid_entry2 *e, *best;
2551 kvm_x86_ops->cache_regs(vcpu);
2552 function = vcpu->arch.regs[VCPU_REGS_RAX];
2553 index = vcpu->arch.regs[VCPU_REGS_RCX];
2554 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2555 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2556 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2557 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2559 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2560 e = &vcpu->arch.cpuid_entries[i];
2561 if (is_matching_cpuid_entry(e, function, index)) {
2562 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2563 move_to_next_stateful_cpuid_entry(vcpu, i);
2568 * Both basic or both extended?
2570 if (((e->function ^ function) & 0x80000000) == 0)
2571 if (!best || e->function > best->function)
2575 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2576 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2577 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2578 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2580 kvm_x86_ops->decache_regs(vcpu);
2581 kvm_x86_ops->skip_emulated_instruction(vcpu);
2583 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2586 * Check if userspace requested an interrupt window, and that the
2587 * interrupt window is open.
2589 * No need to exit to userspace if we already have an interrupt queued.
2591 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2592 struct kvm_run *kvm_run)
2594 return (!vcpu->arch.irq_summary &&
2595 kvm_run->request_interrupt_window &&
2596 vcpu->arch.interrupt_window_open &&
2597 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2600 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2601 struct kvm_run *kvm_run)
2603 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2604 kvm_run->cr8 = get_cr8(vcpu);
2605 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2606 if (irqchip_in_kernel(vcpu->kvm))
2607 kvm_run->ready_for_interrupt_injection = 1;
2609 kvm_run->ready_for_interrupt_injection =
2610 (vcpu->arch.interrupt_window_open &&
2611 vcpu->arch.irq_summary == 0);
2614 static void vapic_enter(struct kvm_vcpu *vcpu)
2616 struct kvm_lapic *apic = vcpu->arch.apic;
2619 if (!apic || !apic->vapic_addr)
2622 down_read(¤t->mm->mmap_sem);
2623 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2624 up_read(¤t->mm->mmap_sem);
2626 vcpu->arch.apic->vapic_page = page;
2629 static void vapic_exit(struct kvm_vcpu *vcpu)
2631 struct kvm_lapic *apic = vcpu->arch.apic;
2633 if (!apic || !apic->vapic_addr)
2636 kvm_release_page_dirty(apic->vapic_page);
2637 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2640 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2644 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
2645 pr_debug("vcpu %d received sipi with vector # %x\n",
2646 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2647 kvm_lapic_reset(vcpu);
2648 r = kvm_x86_ops->vcpu_reset(vcpu);
2651 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
2657 if (vcpu->guest_debug.enabled)
2658 kvm_x86_ops->guest_debug_pre(vcpu);
2661 r = kvm_mmu_reload(vcpu);
2665 if (vcpu->requests) {
2666 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2667 __kvm_migrate_apic_timer(vcpu);
2668 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2670 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2676 kvm_inject_pending_timer_irqs(vcpu);
2680 kvm_x86_ops->prepare_guest_switch(vcpu);
2681 kvm_load_guest_fpu(vcpu);
2683 local_irq_disable();
2685 if (need_resched()) {
2692 if (signal_pending(current)) {
2696 kvm_run->exit_reason = KVM_EXIT_INTR;
2697 ++vcpu->stat.signal_exits;
2701 if (vcpu->arch.exception.pending)
2702 __queue_exception(vcpu);
2703 else if (irqchip_in_kernel(vcpu->kvm))
2704 kvm_x86_ops->inject_pending_irq(vcpu);
2706 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2708 kvm_lapic_sync_to_vapic(vcpu);
2710 vcpu->guest_mode = 1;
2714 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2715 kvm_x86_ops->tlb_flush(vcpu);
2717 kvm_x86_ops->run(vcpu, kvm_run);
2719 vcpu->guest_mode = 0;
2725 * We must have an instruction between local_irq_enable() and
2726 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2727 * the interrupt shadow. The stat.exits increment will do nicely.
2728 * But we need to prevent reordering, hence this barrier():
2737 * Profile KVM exit RIPs:
2739 if (unlikely(prof_on == KVM_PROFILING)) {
2740 kvm_x86_ops->cache_regs(vcpu);
2741 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2744 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2745 vcpu->arch.exception.pending = false;
2747 kvm_lapic_sync_from_vapic(vcpu);
2749 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2752 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2754 kvm_run->exit_reason = KVM_EXIT_INTR;
2755 ++vcpu->stat.request_irq_exits;
2758 if (!need_resched())
2768 post_kvm_run_save(vcpu, kvm_run);
2775 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2782 if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
2783 kvm_vcpu_block(vcpu);
2788 if (vcpu->sigset_active)
2789 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2791 /* re-sync apic's tpr */
2792 if (!irqchip_in_kernel(vcpu->kvm))
2793 set_cr8(vcpu, kvm_run->cr8);
2795 if (vcpu->arch.pio.cur_count) {
2796 r = complete_pio(vcpu);
2800 #if CONFIG_HAS_IOMEM
2801 if (vcpu->mmio_needed) {
2802 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2803 vcpu->mmio_read_completed = 1;
2804 vcpu->mmio_needed = 0;
2805 r = emulate_instruction(vcpu, kvm_run,
2806 vcpu->arch.mmio_fault_cr2, 0,
2807 EMULTYPE_NO_DECODE);
2808 if (r == EMULATE_DO_MMIO) {
2810 * Read-modify-write. Back to userspace.
2817 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2818 kvm_x86_ops->cache_regs(vcpu);
2819 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
2820 kvm_x86_ops->decache_regs(vcpu);
2823 r = __vcpu_run(vcpu, kvm_run);
2826 if (vcpu->sigset_active)
2827 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2833 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2837 kvm_x86_ops->cache_regs(vcpu);
2839 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
2840 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
2841 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
2842 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
2843 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
2844 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
2845 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2846 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
2847 #ifdef CONFIG_X86_64
2848 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
2849 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
2850 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
2851 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
2852 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
2853 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
2854 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
2855 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
2858 regs->rip = vcpu->arch.rip;
2859 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
2862 * Don't leak debug flags in case they were set for guest debugging
2864 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2865 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2872 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2876 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
2877 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
2878 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
2879 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
2880 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
2881 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
2882 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
2883 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
2884 #ifdef CONFIG_X86_64
2885 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
2886 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
2887 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
2888 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
2889 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
2890 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
2891 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
2892 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
2895 vcpu->arch.rip = regs->rip;
2896 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
2898 kvm_x86_ops->decache_regs(vcpu);
2905 static void get_segment(struct kvm_vcpu *vcpu,
2906 struct kvm_segment *var, int seg)
2908 kvm_x86_ops->get_segment(vcpu, var, seg);
2911 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2913 struct kvm_segment cs;
2915 get_segment(vcpu, &cs, VCPU_SREG_CS);
2919 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
2921 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2922 struct kvm_sregs *sregs)
2924 struct descriptor_table dt;
2929 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2930 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2931 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2932 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2933 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2934 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2936 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2937 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2939 kvm_x86_ops->get_idt(vcpu, &dt);
2940 sregs->idt.limit = dt.limit;
2941 sregs->idt.base = dt.base;
2942 kvm_x86_ops->get_gdt(vcpu, &dt);
2943 sregs->gdt.limit = dt.limit;
2944 sregs->gdt.base = dt.base;
2946 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2947 sregs->cr0 = vcpu->arch.cr0;
2948 sregs->cr2 = vcpu->arch.cr2;
2949 sregs->cr3 = vcpu->arch.cr3;
2950 sregs->cr4 = vcpu->arch.cr4;
2951 sregs->cr8 = get_cr8(vcpu);
2952 sregs->efer = vcpu->arch.shadow_efer;
2953 sregs->apic_base = kvm_get_apic_base(vcpu);
2955 if (irqchip_in_kernel(vcpu->kvm)) {
2956 memset(sregs->interrupt_bitmap, 0,
2957 sizeof sregs->interrupt_bitmap);
2958 pending_vec = kvm_x86_ops->get_irq(vcpu);
2959 if (pending_vec >= 0)
2960 set_bit(pending_vec,
2961 (unsigned long *)sregs->interrupt_bitmap);
2963 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
2964 sizeof sregs->interrupt_bitmap);
2971 static void set_segment(struct kvm_vcpu *vcpu,
2972 struct kvm_segment *var, int seg)
2974 kvm_x86_ops->set_segment(vcpu, var, seg);
2977 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2978 struct kvm_sregs *sregs)
2980 int mmu_reset_needed = 0;
2981 int i, pending_vec, max_bits;
2982 struct descriptor_table dt;
2986 dt.limit = sregs->idt.limit;
2987 dt.base = sregs->idt.base;
2988 kvm_x86_ops->set_idt(vcpu, &dt);
2989 dt.limit = sregs->gdt.limit;
2990 dt.base = sregs->gdt.base;
2991 kvm_x86_ops->set_gdt(vcpu, &dt);
2993 vcpu->arch.cr2 = sregs->cr2;
2994 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
2995 vcpu->arch.cr3 = sregs->cr3;
2997 set_cr8(vcpu, sregs->cr8);
2999 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
3000 kvm_x86_ops->set_efer(vcpu, sregs->efer);
3001 kvm_set_apic_base(vcpu, sregs->apic_base);
3003 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3005 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
3006 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
3007 vcpu->arch.cr0 = sregs->cr0;
3009 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
3010 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3011 if (!is_long_mode(vcpu) && is_pae(vcpu))
3012 load_pdptrs(vcpu, vcpu->arch.cr3);
3014 if (mmu_reset_needed)
3015 kvm_mmu_reset_context(vcpu);
3017 if (!irqchip_in_kernel(vcpu->kvm)) {
3018 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3019 sizeof vcpu->arch.irq_pending);
3020 vcpu->arch.irq_summary = 0;
3021 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3022 if (vcpu->arch.irq_pending[i])
3023 __set_bit(i, &vcpu->arch.irq_summary);
3025 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3026 pending_vec = find_first_bit(
3027 (const unsigned long *)sregs->interrupt_bitmap,
3029 /* Only pending external irq is handled here */
3030 if (pending_vec < max_bits) {
3031 kvm_x86_ops->set_irq(vcpu, pending_vec);
3032 pr_debug("Set back pending irq %d\n",
3037 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3038 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3039 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3040 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3041 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3042 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3044 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3045 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3052 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3053 struct kvm_debug_guest *dbg)
3059 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3067 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3068 * we have asm/x86/processor.h
3079 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3080 #ifdef CONFIG_X86_64
3081 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3083 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3088 * Translate a guest virtual address to a guest physical address.
3090 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3091 struct kvm_translation *tr)
3093 unsigned long vaddr = tr->linear_address;
3097 down_read(&vcpu->kvm->slots_lock);
3098 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
3099 up_read(&vcpu->kvm->slots_lock);
3100 tr->physical_address = gpa;
3101 tr->valid = gpa != UNMAPPED_GVA;
3109 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3111 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3115 memcpy(fpu->fpr, fxsave->st_space, 128);
3116 fpu->fcw = fxsave->cwd;
3117 fpu->fsw = fxsave->swd;
3118 fpu->ftwx = fxsave->twd;
3119 fpu->last_opcode = fxsave->fop;
3120 fpu->last_ip = fxsave->rip;
3121 fpu->last_dp = fxsave->rdp;
3122 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3129 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3131 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3135 memcpy(fxsave->st_space, fpu->fpr, 128);
3136 fxsave->cwd = fpu->fcw;
3137 fxsave->swd = fpu->fsw;
3138 fxsave->twd = fpu->ftwx;
3139 fxsave->fop = fpu->last_opcode;
3140 fxsave->rip = fpu->last_ip;
3141 fxsave->rdp = fpu->last_dp;
3142 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3149 void fx_init(struct kvm_vcpu *vcpu)
3151 unsigned after_mxcsr_mask;
3153 /* Initialize guest FPU by resetting ours and saving into guest's */
3155 fx_save(&vcpu->arch.host_fx_image);
3157 fx_save(&vcpu->arch.guest_fx_image);
3158 fx_restore(&vcpu->arch.host_fx_image);
3161 vcpu->arch.cr0 |= X86_CR0_ET;
3162 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3163 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3164 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3165 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3167 EXPORT_SYMBOL_GPL(fx_init);
3169 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3171 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3174 vcpu->guest_fpu_loaded = 1;
3175 fx_save(&vcpu->arch.host_fx_image);
3176 fx_restore(&vcpu->arch.guest_fx_image);
3178 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3180 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3182 if (!vcpu->guest_fpu_loaded)
3185 vcpu->guest_fpu_loaded = 0;
3186 fx_save(&vcpu->arch.guest_fx_image);
3187 fx_restore(&vcpu->arch.host_fx_image);
3188 ++vcpu->stat.fpu_reload;
3190 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3192 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3194 kvm_x86_ops->vcpu_free(vcpu);
3197 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3200 return kvm_x86_ops->vcpu_create(kvm, id);
3203 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3207 /* We do fxsave: this must be aligned. */
3208 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3211 r = kvm_arch_vcpu_reset(vcpu);
3213 r = kvm_mmu_setup(vcpu);
3220 kvm_x86_ops->vcpu_free(vcpu);
3224 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3227 kvm_mmu_unload(vcpu);
3230 kvm_x86_ops->vcpu_free(vcpu);
3233 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3235 return kvm_x86_ops->vcpu_reset(vcpu);
3238 void kvm_arch_hardware_enable(void *garbage)
3240 kvm_x86_ops->hardware_enable(garbage);
3243 void kvm_arch_hardware_disable(void *garbage)
3245 kvm_x86_ops->hardware_disable(garbage);
3248 int kvm_arch_hardware_setup(void)
3250 return kvm_x86_ops->hardware_setup();
3253 void kvm_arch_hardware_unsetup(void)
3255 kvm_x86_ops->hardware_unsetup();
3258 void kvm_arch_check_processor_compat(void *rtn)
3260 kvm_x86_ops->check_processor_compatibility(rtn);
3263 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3269 BUG_ON(vcpu->kvm == NULL);
3272 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3273 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3274 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
3276 vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
3278 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3283 vcpu->arch.pio_data = page_address(page);
3285 r = kvm_mmu_create(vcpu);
3287 goto fail_free_pio_data;
3289 if (irqchip_in_kernel(kvm)) {
3290 r = kvm_create_lapic(vcpu);
3292 goto fail_mmu_destroy;
3298 kvm_mmu_destroy(vcpu);
3300 free_page((unsigned long)vcpu->arch.pio_data);
3305 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3307 kvm_free_lapic(vcpu);
3308 kvm_mmu_destroy(vcpu);
3309 free_page((unsigned long)vcpu->arch.pio_data);
3312 struct kvm *kvm_arch_create_vm(void)
3314 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3317 return ERR_PTR(-ENOMEM);
3319 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3324 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3327 kvm_mmu_unload(vcpu);
3331 static void kvm_free_vcpus(struct kvm *kvm)
3336 * Unpin any mmu pages first.
3338 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3340 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3341 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3342 if (kvm->vcpus[i]) {
3343 kvm_arch_vcpu_free(kvm->vcpus[i]);
3344 kvm->vcpus[i] = NULL;
3350 void kvm_arch_destroy_vm(struct kvm *kvm)
3352 kfree(kvm->arch.vpic);
3353 kfree(kvm->arch.vioapic);
3354 kvm_free_vcpus(kvm);
3355 kvm_free_physmem(kvm);
3359 int kvm_arch_set_memory_region(struct kvm *kvm,
3360 struct kvm_userspace_memory_region *mem,
3361 struct kvm_memory_slot old,
3364 int npages = mem->memory_size >> PAGE_SHIFT;
3365 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3367 /*To keep backward compatibility with older userspace,
3368 *x86 needs to hanlde !user_alloc case.
3371 if (npages && !old.rmap) {
3372 down_write(¤t->mm->mmap_sem);
3373 memslot->userspace_addr = do_mmap(NULL, 0,
3375 PROT_READ | PROT_WRITE,
3376 MAP_SHARED | MAP_ANONYMOUS,
3378 up_write(¤t->mm->mmap_sem);
3380 if (IS_ERR((void *)memslot->userspace_addr))
3381 return PTR_ERR((void *)memslot->userspace_addr);
3383 if (!old.user_alloc && old.rmap) {
3386 down_write(¤t->mm->mmap_sem);
3387 ret = do_munmap(current->mm, old.userspace_addr,
3388 old.npages * PAGE_SIZE);
3389 up_write(¤t->mm->mmap_sem);
3392 "kvm_vm_ioctl_set_memory_region: "
3393 "failed to munmap memory\n");
3398 if (!kvm->arch.n_requested_mmu_pages) {
3399 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
3400 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
3403 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
3404 kvm_flush_remote_tlbs(kvm);
3409 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3411 return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
3412 || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
3415 static void vcpu_kick_intr(void *info)
3418 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
3419 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
3423 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
3425 int ipi_pcpu = vcpu->cpu;
3427 if (waitqueue_active(&vcpu->wq)) {
3428 wake_up_interruptible(&vcpu->wq);
3429 ++vcpu->stat.halt_wakeup;
3431 if (vcpu->guest_mode)
3432 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0);