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>
23 #include <linux/clocksource.h>
24 #include <linux/kvm.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/mman.h>
29 #include <linux/highmem.h>
31 #include <asm/uaccess.h>
35 #define MAX_IO_MSRS 256
36 #define CR0_RESERVED_BITS \
37 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
38 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
39 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
40 #define CR4_RESERVED_BITS \
41 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
42 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
43 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
44 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
46 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
48 * - enable syscall per default because its emulated by KVM
49 * - enable LME and LMA per default on 64 bit KVM
52 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
54 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
57 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
58 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
60 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
61 struct kvm_cpuid_entry2 __user *entries);
63 struct kvm_x86_ops *kvm_x86_ops;
65 struct kvm_stats_debugfs_item debugfs_entries[] = {
66 { "pf_fixed", VCPU_STAT(pf_fixed) },
67 { "pf_guest", VCPU_STAT(pf_guest) },
68 { "tlb_flush", VCPU_STAT(tlb_flush) },
69 { "invlpg", VCPU_STAT(invlpg) },
70 { "exits", VCPU_STAT(exits) },
71 { "io_exits", VCPU_STAT(io_exits) },
72 { "mmio_exits", VCPU_STAT(mmio_exits) },
73 { "signal_exits", VCPU_STAT(signal_exits) },
74 { "irq_window", VCPU_STAT(irq_window_exits) },
75 { "halt_exits", VCPU_STAT(halt_exits) },
76 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
77 { "hypercalls", VCPU_STAT(hypercalls) },
78 { "request_irq", VCPU_STAT(request_irq_exits) },
79 { "irq_exits", VCPU_STAT(irq_exits) },
80 { "host_state_reload", VCPU_STAT(host_state_reload) },
81 { "efer_reload", VCPU_STAT(efer_reload) },
82 { "fpu_reload", VCPU_STAT(fpu_reload) },
83 { "insn_emulation", VCPU_STAT(insn_emulation) },
84 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
85 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
86 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
87 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
88 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
89 { "mmu_flooded", VM_STAT(mmu_flooded) },
90 { "mmu_recycled", VM_STAT(mmu_recycled) },
91 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
92 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
93 { "largepages", VM_STAT(lpages) },
98 unsigned long segment_base(u16 selector)
100 struct descriptor_table gdt;
101 struct desc_struct *d;
102 unsigned long table_base;
108 asm("sgdt %0" : "=m"(gdt));
109 table_base = gdt.base;
111 if (selector & 4) { /* from ldt */
114 asm("sldt %0" : "=g"(ldt_selector));
115 table_base = segment_base(ldt_selector);
117 d = (struct desc_struct *)(table_base + (selector & ~7));
118 v = d->base0 | ((unsigned long)d->base1 << 16) |
119 ((unsigned long)d->base2 << 24);
121 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
122 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
126 EXPORT_SYMBOL_GPL(segment_base);
128 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
130 if (irqchip_in_kernel(vcpu->kvm))
131 return vcpu->arch.apic_base;
133 return vcpu->arch.apic_base;
135 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
137 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
139 /* TODO: reserve bits check */
140 if (irqchip_in_kernel(vcpu->kvm))
141 kvm_lapic_set_base(vcpu, data);
143 vcpu->arch.apic_base = data;
145 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
147 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
149 WARN_ON(vcpu->arch.exception.pending);
150 vcpu->arch.exception.pending = true;
151 vcpu->arch.exception.has_error_code = false;
152 vcpu->arch.exception.nr = nr;
154 EXPORT_SYMBOL_GPL(kvm_queue_exception);
156 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
159 ++vcpu->stat.pf_guest;
160 if (vcpu->arch.exception.pending) {
161 if (vcpu->arch.exception.nr == PF_VECTOR) {
162 printk(KERN_DEBUG "kvm: inject_page_fault:"
163 " double fault 0x%lx\n", addr);
164 vcpu->arch.exception.nr = DF_VECTOR;
165 vcpu->arch.exception.error_code = 0;
166 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
167 /* triple fault -> shutdown */
168 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
172 vcpu->arch.cr2 = addr;
173 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
176 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
178 WARN_ON(vcpu->arch.exception.pending);
179 vcpu->arch.exception.pending = true;
180 vcpu->arch.exception.has_error_code = true;
181 vcpu->arch.exception.nr = nr;
182 vcpu->arch.exception.error_code = error_code;
184 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
186 static void __queue_exception(struct kvm_vcpu *vcpu)
188 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
189 vcpu->arch.exception.has_error_code,
190 vcpu->arch.exception.error_code);
194 * Load the pae pdptrs. Return true is they are all valid.
196 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
198 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
199 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
202 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
204 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
205 offset * sizeof(u64), sizeof(pdpte));
210 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
211 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
218 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
223 EXPORT_SYMBOL_GPL(load_pdptrs);
225 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
227 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
231 if (is_long_mode(vcpu) || !is_pae(vcpu))
234 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
237 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
243 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
245 if (cr0 & CR0_RESERVED_BITS) {
246 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
247 cr0, vcpu->arch.cr0);
248 kvm_inject_gp(vcpu, 0);
252 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
253 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
254 kvm_inject_gp(vcpu, 0);
258 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
259 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
260 "and a clear PE flag\n");
261 kvm_inject_gp(vcpu, 0);
265 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
267 if ((vcpu->arch.shadow_efer & EFER_LME)) {
271 printk(KERN_DEBUG "set_cr0: #GP, start paging "
272 "in long mode while PAE is disabled\n");
273 kvm_inject_gp(vcpu, 0);
276 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
278 printk(KERN_DEBUG "set_cr0: #GP, start paging "
279 "in long mode while CS.L == 1\n");
280 kvm_inject_gp(vcpu, 0);
286 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
287 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
289 kvm_inject_gp(vcpu, 0);
295 kvm_x86_ops->set_cr0(vcpu, cr0);
296 vcpu->arch.cr0 = cr0;
298 kvm_mmu_reset_context(vcpu);
301 EXPORT_SYMBOL_GPL(kvm_set_cr0);
303 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
305 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
306 KVMTRACE_1D(LMSW, vcpu,
307 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
310 EXPORT_SYMBOL_GPL(kvm_lmsw);
312 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
314 if (cr4 & CR4_RESERVED_BITS) {
315 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
316 kvm_inject_gp(vcpu, 0);
320 if (is_long_mode(vcpu)) {
321 if (!(cr4 & X86_CR4_PAE)) {
322 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
324 kvm_inject_gp(vcpu, 0);
327 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
328 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
329 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
330 kvm_inject_gp(vcpu, 0);
334 if (cr4 & X86_CR4_VMXE) {
335 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
336 kvm_inject_gp(vcpu, 0);
339 kvm_x86_ops->set_cr4(vcpu, cr4);
340 vcpu->arch.cr4 = cr4;
341 kvm_mmu_reset_context(vcpu);
343 EXPORT_SYMBOL_GPL(kvm_set_cr4);
345 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
347 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
348 kvm_mmu_flush_tlb(vcpu);
352 if (is_long_mode(vcpu)) {
353 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
354 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
355 kvm_inject_gp(vcpu, 0);
360 if (cr3 & CR3_PAE_RESERVED_BITS) {
362 "set_cr3: #GP, reserved bits\n");
363 kvm_inject_gp(vcpu, 0);
366 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
367 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
369 kvm_inject_gp(vcpu, 0);
374 * We don't check reserved bits in nonpae mode, because
375 * this isn't enforced, and VMware depends on this.
380 * Does the new cr3 value map to physical memory? (Note, we
381 * catch an invalid cr3 even in real-mode, because it would
382 * cause trouble later on when we turn on paging anyway.)
384 * A real CPU would silently accept an invalid cr3 and would
385 * attempt to use it - with largely undefined (and often hard
386 * to debug) behavior on the guest side.
388 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
389 kvm_inject_gp(vcpu, 0);
391 vcpu->arch.cr3 = cr3;
392 vcpu->arch.mmu.new_cr3(vcpu);
395 EXPORT_SYMBOL_GPL(kvm_set_cr3);
397 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
399 if (cr8 & CR8_RESERVED_BITS) {
400 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
401 kvm_inject_gp(vcpu, 0);
404 if (irqchip_in_kernel(vcpu->kvm))
405 kvm_lapic_set_tpr(vcpu, cr8);
407 vcpu->arch.cr8 = cr8;
409 EXPORT_SYMBOL_GPL(kvm_set_cr8);
411 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
413 if (irqchip_in_kernel(vcpu->kvm))
414 return kvm_lapic_get_cr8(vcpu);
416 return vcpu->arch.cr8;
418 EXPORT_SYMBOL_GPL(kvm_get_cr8);
421 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
422 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
424 * This list is modified at module load time to reflect the
425 * capabilities of the host cpu.
427 static u32 msrs_to_save[] = {
428 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
431 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
433 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
434 MSR_IA32_PERF_STATUS,
437 static unsigned num_msrs_to_save;
439 static u32 emulated_msrs[] = {
440 MSR_IA32_MISC_ENABLE,
443 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
445 if (efer & efer_reserved_bits) {
446 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
448 kvm_inject_gp(vcpu, 0);
453 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
454 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
455 kvm_inject_gp(vcpu, 0);
459 kvm_x86_ops->set_efer(vcpu, efer);
462 efer |= vcpu->arch.shadow_efer & EFER_LMA;
464 vcpu->arch.shadow_efer = efer;
467 void kvm_enable_efer_bits(u64 mask)
469 efer_reserved_bits &= ~mask;
471 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
475 * Writes msr value into into the appropriate "register".
476 * Returns 0 on success, non-0 otherwise.
477 * Assumes vcpu_load() was already called.
479 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
481 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
485 * Adapt set_msr() to msr_io()'s calling convention
487 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
489 return kvm_set_msr(vcpu, index, *data);
492 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
495 struct pvclock_wall_clock wc;
496 struct timespec now, sys, boot;
503 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
506 * The guest calculates current wall clock time by adding
507 * system time (updated by kvm_write_guest_time below) to the
508 * wall clock specified here. guest system time equals host
509 * system time for us, thus we must fill in host boot time here.
511 now = current_kernel_time();
513 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
515 wc.sec = boot.tv_sec;
516 wc.nsec = boot.tv_nsec;
517 wc.version = version;
519 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
522 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
525 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
527 uint32_t quotient, remainder;
529 /* Don't try to replace with do_div(), this one calculates
530 * "(dividend << 32) / divisor" */
532 : "=a" (quotient), "=d" (remainder)
533 : "0" (0), "1" (dividend), "r" (divisor) );
537 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
539 uint64_t nsecs = 1000000000LL;
544 tps64 = tsc_khz * 1000LL;
545 while (tps64 > nsecs*2) {
550 tps32 = (uint32_t)tps64;
551 while (tps32 <= (uint32_t)nsecs) {
556 hv_clock->tsc_shift = shift;
557 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
559 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
560 __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
561 hv_clock->tsc_to_system_mul);
564 static void kvm_write_guest_time(struct kvm_vcpu *v)
568 struct kvm_vcpu_arch *vcpu = &v->arch;
571 if ((!vcpu->time_page))
574 if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
575 kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
576 vcpu->hv_clock_tsc_khz = tsc_khz;
579 /* Keep irq disabled to prevent changes to the clock */
580 local_irq_save(flags);
581 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
582 &vcpu->hv_clock.tsc_timestamp);
584 local_irq_restore(flags);
586 /* With all the info we got, fill in the values */
588 vcpu->hv_clock.system_time = ts.tv_nsec +
589 (NSEC_PER_SEC * (u64)ts.tv_sec);
591 * The interface expects us to write an even number signaling that the
592 * update is finished. Since the guest won't see the intermediate
593 * state, we just increase by 2 at the end.
595 vcpu->hv_clock.version += 2;
597 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
599 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
600 sizeof(vcpu->hv_clock));
602 kunmap_atomic(shared_kaddr, KM_USER0);
604 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
608 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
612 set_efer(vcpu, data);
614 case MSR_IA32_MC0_STATUS:
615 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
618 case MSR_IA32_MCG_STATUS:
619 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
622 case MSR_IA32_MCG_CTL:
623 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
626 case MSR_IA32_UCODE_REV:
627 case MSR_IA32_UCODE_WRITE:
628 case 0x200 ... 0x2ff: /* MTRRs */
630 case MSR_IA32_APICBASE:
631 kvm_set_apic_base(vcpu, data);
633 case MSR_IA32_MISC_ENABLE:
634 vcpu->arch.ia32_misc_enable_msr = data;
636 case MSR_KVM_WALL_CLOCK:
637 vcpu->kvm->arch.wall_clock = data;
638 kvm_write_wall_clock(vcpu->kvm, data);
640 case MSR_KVM_SYSTEM_TIME: {
641 if (vcpu->arch.time_page) {
642 kvm_release_page_dirty(vcpu->arch.time_page);
643 vcpu->arch.time_page = NULL;
646 vcpu->arch.time = data;
648 /* we verify if the enable bit is set... */
652 /* ...but clean it before doing the actual write */
653 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
655 down_read(¤t->mm->mmap_sem);
656 vcpu->arch.time_page =
657 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
658 up_read(¤t->mm->mmap_sem);
660 if (is_error_page(vcpu->arch.time_page)) {
661 kvm_release_page_clean(vcpu->arch.time_page);
662 vcpu->arch.time_page = NULL;
665 kvm_write_guest_time(vcpu);
669 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
674 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
678 * Reads an msr value (of 'msr_index') into 'pdata'.
679 * Returns 0 on success, non-0 otherwise.
680 * Assumes vcpu_load() was already called.
682 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
684 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
687 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
692 case 0xc0010010: /* SYSCFG */
693 case 0xc0010015: /* HWCR */
694 case MSR_IA32_PLATFORM_ID:
695 case MSR_IA32_P5_MC_ADDR:
696 case MSR_IA32_P5_MC_TYPE:
697 case MSR_IA32_MC0_CTL:
698 case MSR_IA32_MCG_STATUS:
699 case MSR_IA32_MCG_CAP:
700 case MSR_IA32_MCG_CTL:
701 case MSR_IA32_MC0_MISC:
702 case MSR_IA32_MC0_MISC+4:
703 case MSR_IA32_MC0_MISC+8:
704 case MSR_IA32_MC0_MISC+12:
705 case MSR_IA32_MC0_MISC+16:
706 case MSR_IA32_UCODE_REV:
707 case MSR_IA32_EBL_CR_POWERON:
710 case 0x200 ... 0x2ff:
713 case 0xcd: /* fsb frequency */
716 case MSR_IA32_APICBASE:
717 data = kvm_get_apic_base(vcpu);
719 case MSR_IA32_MISC_ENABLE:
720 data = vcpu->arch.ia32_misc_enable_msr;
722 case MSR_IA32_PERF_STATUS:
723 /* TSC increment by tick */
726 data |= (((uint64_t)4ULL) << 40);
729 data = vcpu->arch.shadow_efer;
731 case MSR_KVM_WALL_CLOCK:
732 data = vcpu->kvm->arch.wall_clock;
734 case MSR_KVM_SYSTEM_TIME:
735 data = vcpu->arch.time;
738 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
744 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
747 * Read or write a bunch of msrs. All parameters are kernel addresses.
749 * @return number of msrs set successfully.
751 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
752 struct kvm_msr_entry *entries,
753 int (*do_msr)(struct kvm_vcpu *vcpu,
754 unsigned index, u64 *data))
760 down_read(&vcpu->kvm->slots_lock);
761 for (i = 0; i < msrs->nmsrs; ++i)
762 if (do_msr(vcpu, entries[i].index, &entries[i].data))
764 up_read(&vcpu->kvm->slots_lock);
772 * Read or write a bunch of msrs. Parameters are user addresses.
774 * @return number of msrs set successfully.
776 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
777 int (*do_msr)(struct kvm_vcpu *vcpu,
778 unsigned index, u64 *data),
781 struct kvm_msrs msrs;
782 struct kvm_msr_entry *entries;
787 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
791 if (msrs.nmsrs >= MAX_IO_MSRS)
795 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
796 entries = vmalloc(size);
801 if (copy_from_user(entries, user_msrs->entries, size))
804 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
809 if (writeback && copy_to_user(user_msrs->entries, entries, size))
821 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
824 void decache_vcpus_on_cpu(int cpu)
827 struct kvm_vcpu *vcpu;
830 spin_lock(&kvm_lock);
831 list_for_each_entry(vm, &vm_list, vm_list)
832 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
837 * If the vcpu is locked, then it is running on some
838 * other cpu and therefore it is not cached on the
841 * If it's not locked, check the last cpu it executed
844 if (mutex_trylock(&vcpu->mutex)) {
845 if (vcpu->cpu == cpu) {
846 kvm_x86_ops->vcpu_decache(vcpu);
849 mutex_unlock(&vcpu->mutex);
852 spin_unlock(&kvm_lock);
855 int kvm_dev_ioctl_check_extension(long ext)
860 case KVM_CAP_IRQCHIP:
862 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
863 case KVM_CAP_USER_MEMORY:
864 case KVM_CAP_SET_TSS_ADDR:
865 case KVM_CAP_EXT_CPUID:
866 case KVM_CAP_CLOCKSOURCE:
868 case KVM_CAP_NOP_IO_DELAY:
869 case KVM_CAP_MP_STATE:
873 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
875 case KVM_CAP_NR_VCPUS:
878 case KVM_CAP_NR_MEMSLOTS:
879 r = KVM_MEMORY_SLOTS;
892 long kvm_arch_dev_ioctl(struct file *filp,
893 unsigned int ioctl, unsigned long arg)
895 void __user *argp = (void __user *)arg;
899 case KVM_GET_MSR_INDEX_LIST: {
900 struct kvm_msr_list __user *user_msr_list = argp;
901 struct kvm_msr_list msr_list;
905 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
908 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
909 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
912 if (n < num_msrs_to_save)
915 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
916 num_msrs_to_save * sizeof(u32)))
918 if (copy_to_user(user_msr_list->indices
919 + num_msrs_to_save * sizeof(u32),
921 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
926 case KVM_GET_SUPPORTED_CPUID: {
927 struct kvm_cpuid2 __user *cpuid_arg = argp;
928 struct kvm_cpuid2 cpuid;
931 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
933 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
939 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
951 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
953 kvm_x86_ops->vcpu_load(vcpu, cpu);
954 kvm_write_guest_time(vcpu);
957 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
959 kvm_x86_ops->vcpu_put(vcpu);
960 kvm_put_guest_fpu(vcpu);
963 static int is_efer_nx(void)
967 rdmsrl(MSR_EFER, efer);
968 return efer & EFER_NX;
971 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
974 struct kvm_cpuid_entry2 *e, *entry;
977 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
978 e = &vcpu->arch.cpuid_entries[i];
979 if (e->function == 0x80000001) {
984 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
985 entry->edx &= ~(1 << 20);
986 printk(KERN_INFO "kvm: guest NX capability removed\n");
990 /* when an old userspace process fills a new kernel module */
991 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
992 struct kvm_cpuid *cpuid,
993 struct kvm_cpuid_entry __user *entries)
996 struct kvm_cpuid_entry *cpuid_entries;
999 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1002 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1006 if (copy_from_user(cpuid_entries, entries,
1007 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1009 for (i = 0; i < cpuid->nent; i++) {
1010 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1011 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1012 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1013 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1014 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1015 vcpu->arch.cpuid_entries[i].index = 0;
1016 vcpu->arch.cpuid_entries[i].flags = 0;
1017 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1018 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1019 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1021 vcpu->arch.cpuid_nent = cpuid->nent;
1022 cpuid_fix_nx_cap(vcpu);
1026 vfree(cpuid_entries);
1031 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1032 struct kvm_cpuid2 *cpuid,
1033 struct kvm_cpuid_entry2 __user *entries)
1038 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1041 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1042 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1044 vcpu->arch.cpuid_nent = cpuid->nent;
1051 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1052 struct kvm_cpuid2 *cpuid,
1053 struct kvm_cpuid_entry2 __user *entries)
1058 if (cpuid->nent < vcpu->arch.cpuid_nent)
1061 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1062 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1067 cpuid->nent = vcpu->arch.cpuid_nent;
1071 static inline u32 bit(int bitno)
1073 return 1 << (bitno & 31);
1076 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1079 entry->function = function;
1080 entry->index = index;
1081 cpuid_count(entry->function, entry->index,
1082 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1086 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1087 u32 index, int *nent, int maxnent)
1089 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1090 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1091 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1092 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1093 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1094 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1095 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1096 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1097 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1098 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1099 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1100 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1101 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1102 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1103 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1104 bit(X86_FEATURE_PGE) |
1105 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1106 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1107 bit(X86_FEATURE_SYSCALL) |
1108 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1109 #ifdef CONFIG_X86_64
1110 bit(X86_FEATURE_LM) |
1112 bit(X86_FEATURE_MMXEXT) |
1113 bit(X86_FEATURE_3DNOWEXT) |
1114 bit(X86_FEATURE_3DNOW);
1115 const u32 kvm_supported_word3_x86_features =
1116 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1117 const u32 kvm_supported_word6_x86_features =
1118 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1120 /* all func 2 cpuid_count() should be called on the same cpu */
1122 do_cpuid_1_ent(entry, function, index);
1127 entry->eax = min(entry->eax, (u32)0xb);
1130 entry->edx &= kvm_supported_word0_x86_features;
1131 entry->ecx &= kvm_supported_word3_x86_features;
1133 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1134 * may return different values. This forces us to get_cpu() before
1135 * issuing the first command, and also to emulate this annoying behavior
1136 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1138 int t, times = entry->eax & 0xff;
1140 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1141 for (t = 1; t < times && *nent < maxnent; ++t) {
1142 do_cpuid_1_ent(&entry[t], function, 0);
1143 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1148 /* function 4 and 0xb have additional index. */
1152 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1153 /* read more entries until cache_type is zero */
1154 for (i = 1; *nent < maxnent; ++i) {
1155 cache_type = entry[i - 1].eax & 0x1f;
1158 do_cpuid_1_ent(&entry[i], function, i);
1160 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1168 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1169 /* read more entries until level_type is zero */
1170 for (i = 1; *nent < maxnent; ++i) {
1171 level_type = entry[i - 1].ecx & 0xff;
1174 do_cpuid_1_ent(&entry[i], function, i);
1176 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1182 entry->eax = min(entry->eax, 0x8000001a);
1185 entry->edx &= kvm_supported_word1_x86_features;
1186 entry->ecx &= kvm_supported_word6_x86_features;
1192 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1193 struct kvm_cpuid_entry2 __user *entries)
1195 struct kvm_cpuid_entry2 *cpuid_entries;
1196 int limit, nent = 0, r = -E2BIG;
1199 if (cpuid->nent < 1)
1202 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1206 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1207 limit = cpuid_entries[0].eax;
1208 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1209 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1210 &nent, cpuid->nent);
1212 if (nent >= cpuid->nent)
1215 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1216 limit = cpuid_entries[nent - 1].eax;
1217 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1218 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1219 &nent, cpuid->nent);
1221 if (copy_to_user(entries, cpuid_entries,
1222 nent * sizeof(struct kvm_cpuid_entry2)))
1228 vfree(cpuid_entries);
1233 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1234 struct kvm_lapic_state *s)
1237 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1243 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1244 struct kvm_lapic_state *s)
1247 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1248 kvm_apic_post_state_restore(vcpu);
1254 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1255 struct kvm_interrupt *irq)
1257 if (irq->irq < 0 || irq->irq >= 256)
1259 if (irqchip_in_kernel(vcpu->kvm))
1263 set_bit(irq->irq, vcpu->arch.irq_pending);
1264 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1271 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1272 struct kvm_tpr_access_ctl *tac)
1276 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1280 long kvm_arch_vcpu_ioctl(struct file *filp,
1281 unsigned int ioctl, unsigned long arg)
1283 struct kvm_vcpu *vcpu = filp->private_data;
1284 void __user *argp = (void __user *)arg;
1288 case KVM_GET_LAPIC: {
1289 struct kvm_lapic_state lapic;
1291 memset(&lapic, 0, sizeof lapic);
1292 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1296 if (copy_to_user(argp, &lapic, sizeof lapic))
1301 case KVM_SET_LAPIC: {
1302 struct kvm_lapic_state lapic;
1305 if (copy_from_user(&lapic, argp, sizeof lapic))
1307 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1313 case KVM_INTERRUPT: {
1314 struct kvm_interrupt irq;
1317 if (copy_from_user(&irq, argp, sizeof irq))
1319 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1325 case KVM_SET_CPUID: {
1326 struct kvm_cpuid __user *cpuid_arg = argp;
1327 struct kvm_cpuid cpuid;
1330 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1332 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1337 case KVM_SET_CPUID2: {
1338 struct kvm_cpuid2 __user *cpuid_arg = argp;
1339 struct kvm_cpuid2 cpuid;
1342 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1344 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1345 cpuid_arg->entries);
1350 case KVM_GET_CPUID2: {
1351 struct kvm_cpuid2 __user *cpuid_arg = argp;
1352 struct kvm_cpuid2 cpuid;
1355 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1357 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1358 cpuid_arg->entries);
1362 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1368 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1371 r = msr_io(vcpu, argp, do_set_msr, 0);
1373 case KVM_TPR_ACCESS_REPORTING: {
1374 struct kvm_tpr_access_ctl tac;
1377 if (copy_from_user(&tac, argp, sizeof tac))
1379 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1383 if (copy_to_user(argp, &tac, sizeof tac))
1388 case KVM_SET_VAPIC_ADDR: {
1389 struct kvm_vapic_addr va;
1392 if (!irqchip_in_kernel(vcpu->kvm))
1395 if (copy_from_user(&va, argp, sizeof va))
1398 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1408 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1412 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1414 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1418 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1419 u32 kvm_nr_mmu_pages)
1421 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1424 down_write(&kvm->slots_lock);
1426 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1427 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1429 up_write(&kvm->slots_lock);
1433 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1435 return kvm->arch.n_alloc_mmu_pages;
1438 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1441 struct kvm_mem_alias *alias;
1443 for (i = 0; i < kvm->arch.naliases; ++i) {
1444 alias = &kvm->arch.aliases[i];
1445 if (gfn >= alias->base_gfn
1446 && gfn < alias->base_gfn + alias->npages)
1447 return alias->target_gfn + gfn - alias->base_gfn;
1453 * Set a new alias region. Aliases map a portion of physical memory into
1454 * another portion. This is useful for memory windows, for example the PC
1457 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1458 struct kvm_memory_alias *alias)
1461 struct kvm_mem_alias *p;
1464 /* General sanity checks */
1465 if (alias->memory_size & (PAGE_SIZE - 1))
1467 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1469 if (alias->slot >= KVM_ALIAS_SLOTS)
1471 if (alias->guest_phys_addr + alias->memory_size
1472 < alias->guest_phys_addr)
1474 if (alias->target_phys_addr + alias->memory_size
1475 < alias->target_phys_addr)
1478 down_write(&kvm->slots_lock);
1480 p = &kvm->arch.aliases[alias->slot];
1481 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1482 p->npages = alias->memory_size >> PAGE_SHIFT;
1483 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1485 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1486 if (kvm->arch.aliases[n - 1].npages)
1488 kvm->arch.naliases = n;
1490 kvm_mmu_zap_all(kvm);
1492 up_write(&kvm->slots_lock);
1500 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1505 switch (chip->chip_id) {
1506 case KVM_IRQCHIP_PIC_MASTER:
1507 memcpy(&chip->chip.pic,
1508 &pic_irqchip(kvm)->pics[0],
1509 sizeof(struct kvm_pic_state));
1511 case KVM_IRQCHIP_PIC_SLAVE:
1512 memcpy(&chip->chip.pic,
1513 &pic_irqchip(kvm)->pics[1],
1514 sizeof(struct kvm_pic_state));
1516 case KVM_IRQCHIP_IOAPIC:
1517 memcpy(&chip->chip.ioapic,
1518 ioapic_irqchip(kvm),
1519 sizeof(struct kvm_ioapic_state));
1528 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1533 switch (chip->chip_id) {
1534 case KVM_IRQCHIP_PIC_MASTER:
1535 memcpy(&pic_irqchip(kvm)->pics[0],
1537 sizeof(struct kvm_pic_state));
1539 case KVM_IRQCHIP_PIC_SLAVE:
1540 memcpy(&pic_irqchip(kvm)->pics[1],
1542 sizeof(struct kvm_pic_state));
1544 case KVM_IRQCHIP_IOAPIC:
1545 memcpy(ioapic_irqchip(kvm),
1547 sizeof(struct kvm_ioapic_state));
1553 kvm_pic_update_irq(pic_irqchip(kvm));
1557 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1561 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1565 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1569 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1570 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1575 * Get (and clear) the dirty memory log for a memory slot.
1577 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1578 struct kvm_dirty_log *log)
1582 struct kvm_memory_slot *memslot;
1585 down_write(&kvm->slots_lock);
1587 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1591 /* If nothing is dirty, don't bother messing with page tables. */
1593 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1594 kvm_flush_remote_tlbs(kvm);
1595 memslot = &kvm->memslots[log->slot];
1596 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1597 memset(memslot->dirty_bitmap, 0, n);
1601 up_write(&kvm->slots_lock);
1605 long kvm_arch_vm_ioctl(struct file *filp,
1606 unsigned int ioctl, unsigned long arg)
1608 struct kvm *kvm = filp->private_data;
1609 void __user *argp = (void __user *)arg;
1613 case KVM_SET_TSS_ADDR:
1614 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1618 case KVM_SET_MEMORY_REGION: {
1619 struct kvm_memory_region kvm_mem;
1620 struct kvm_userspace_memory_region kvm_userspace_mem;
1623 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1625 kvm_userspace_mem.slot = kvm_mem.slot;
1626 kvm_userspace_mem.flags = kvm_mem.flags;
1627 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1628 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1629 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1634 case KVM_SET_NR_MMU_PAGES:
1635 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1639 case KVM_GET_NR_MMU_PAGES:
1640 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1642 case KVM_SET_MEMORY_ALIAS: {
1643 struct kvm_memory_alias alias;
1646 if (copy_from_user(&alias, argp, sizeof alias))
1648 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1653 case KVM_CREATE_IRQCHIP:
1655 kvm->arch.vpic = kvm_create_pic(kvm);
1656 if (kvm->arch.vpic) {
1657 r = kvm_ioapic_init(kvm);
1659 kfree(kvm->arch.vpic);
1660 kvm->arch.vpic = NULL;
1666 case KVM_CREATE_PIT:
1668 kvm->arch.vpit = kvm_create_pit(kvm);
1672 case KVM_IRQ_LINE: {
1673 struct kvm_irq_level irq_event;
1676 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1678 if (irqchip_in_kernel(kvm)) {
1679 mutex_lock(&kvm->lock);
1680 if (irq_event.irq < 16)
1681 kvm_pic_set_irq(pic_irqchip(kvm),
1684 kvm_ioapic_set_irq(kvm->arch.vioapic,
1687 mutex_unlock(&kvm->lock);
1692 case KVM_GET_IRQCHIP: {
1693 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1694 struct kvm_irqchip chip;
1697 if (copy_from_user(&chip, argp, sizeof chip))
1700 if (!irqchip_in_kernel(kvm))
1702 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1706 if (copy_to_user(argp, &chip, sizeof chip))
1711 case KVM_SET_IRQCHIP: {
1712 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1713 struct kvm_irqchip chip;
1716 if (copy_from_user(&chip, argp, sizeof chip))
1719 if (!irqchip_in_kernel(kvm))
1721 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1728 struct kvm_pit_state ps;
1730 if (copy_from_user(&ps, argp, sizeof ps))
1733 if (!kvm->arch.vpit)
1735 r = kvm_vm_ioctl_get_pit(kvm, &ps);
1739 if (copy_to_user(argp, &ps, sizeof ps))
1745 struct kvm_pit_state ps;
1747 if (copy_from_user(&ps, argp, sizeof ps))
1750 if (!kvm->arch.vpit)
1752 r = kvm_vm_ioctl_set_pit(kvm, &ps);
1765 static void kvm_init_msr_list(void)
1770 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1771 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1774 msrs_to_save[j] = msrs_to_save[i];
1777 num_msrs_to_save = j;
1781 * Only apic need an MMIO device hook, so shortcut now..
1783 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1786 struct kvm_io_device *dev;
1788 if (vcpu->arch.apic) {
1789 dev = &vcpu->arch.apic->dev;
1790 if (dev->in_range(dev, addr))
1797 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1800 struct kvm_io_device *dev;
1802 dev = vcpu_find_pervcpu_dev(vcpu, addr);
1804 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1808 int emulator_read_std(unsigned long addr,
1811 struct kvm_vcpu *vcpu)
1814 int r = X86EMUL_CONTINUE;
1817 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1818 unsigned offset = addr & (PAGE_SIZE-1);
1819 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1822 if (gpa == UNMAPPED_GVA) {
1823 r = X86EMUL_PROPAGATE_FAULT;
1826 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1828 r = X86EMUL_UNHANDLEABLE;
1839 EXPORT_SYMBOL_GPL(emulator_read_std);
1841 static int emulator_read_emulated(unsigned long addr,
1844 struct kvm_vcpu *vcpu)
1846 struct kvm_io_device *mmio_dev;
1849 if (vcpu->mmio_read_completed) {
1850 memcpy(val, vcpu->mmio_data, bytes);
1851 vcpu->mmio_read_completed = 0;
1852 return X86EMUL_CONTINUE;
1855 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1857 /* For APIC access vmexit */
1858 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1861 if (emulator_read_std(addr, val, bytes, vcpu)
1862 == X86EMUL_CONTINUE)
1863 return X86EMUL_CONTINUE;
1864 if (gpa == UNMAPPED_GVA)
1865 return X86EMUL_PROPAGATE_FAULT;
1869 * Is this MMIO handled locally?
1871 mutex_lock(&vcpu->kvm->lock);
1872 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1874 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1875 mutex_unlock(&vcpu->kvm->lock);
1876 return X86EMUL_CONTINUE;
1878 mutex_unlock(&vcpu->kvm->lock);
1880 vcpu->mmio_needed = 1;
1881 vcpu->mmio_phys_addr = gpa;
1882 vcpu->mmio_size = bytes;
1883 vcpu->mmio_is_write = 0;
1885 return X86EMUL_UNHANDLEABLE;
1888 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1889 const void *val, int bytes)
1893 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1896 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1900 static int emulator_write_emulated_onepage(unsigned long addr,
1903 struct kvm_vcpu *vcpu)
1905 struct kvm_io_device *mmio_dev;
1908 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1910 if (gpa == UNMAPPED_GVA) {
1911 kvm_inject_page_fault(vcpu, addr, 2);
1912 return X86EMUL_PROPAGATE_FAULT;
1915 /* For APIC access vmexit */
1916 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1919 if (emulator_write_phys(vcpu, gpa, val, bytes))
1920 return X86EMUL_CONTINUE;
1924 * Is this MMIO handled locally?
1926 mutex_lock(&vcpu->kvm->lock);
1927 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1929 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1930 mutex_unlock(&vcpu->kvm->lock);
1931 return X86EMUL_CONTINUE;
1933 mutex_unlock(&vcpu->kvm->lock);
1935 vcpu->mmio_needed = 1;
1936 vcpu->mmio_phys_addr = gpa;
1937 vcpu->mmio_size = bytes;
1938 vcpu->mmio_is_write = 1;
1939 memcpy(vcpu->mmio_data, val, bytes);
1941 return X86EMUL_CONTINUE;
1944 int emulator_write_emulated(unsigned long addr,
1947 struct kvm_vcpu *vcpu)
1949 /* Crossing a page boundary? */
1950 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1953 now = -addr & ~PAGE_MASK;
1954 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1955 if (rc != X86EMUL_CONTINUE)
1961 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1963 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1965 static int emulator_cmpxchg_emulated(unsigned long addr,
1969 struct kvm_vcpu *vcpu)
1971 static int reported;
1975 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1977 #ifndef CONFIG_X86_64
1978 /* guests cmpxchg8b have to be emulated atomically */
1985 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1987 if (gpa == UNMAPPED_GVA ||
1988 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1991 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
1996 down_read(¤t->mm->mmap_sem);
1997 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1998 up_read(¤t->mm->mmap_sem);
2000 kaddr = kmap_atomic(page, KM_USER0);
2001 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2002 kunmap_atomic(kaddr, KM_USER0);
2003 kvm_release_page_dirty(page);
2008 return emulator_write_emulated(addr, new, bytes, vcpu);
2011 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2013 return kvm_x86_ops->get_segment_base(vcpu, seg);
2016 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2018 return X86EMUL_CONTINUE;
2021 int emulate_clts(struct kvm_vcpu *vcpu)
2023 KVMTRACE_0D(CLTS, vcpu, handler);
2024 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
2025 return X86EMUL_CONTINUE;
2028 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2030 struct kvm_vcpu *vcpu = ctxt->vcpu;
2034 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2035 return X86EMUL_CONTINUE;
2037 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
2038 return X86EMUL_UNHANDLEABLE;
2042 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2044 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2047 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2049 /* FIXME: better handling */
2050 return X86EMUL_UNHANDLEABLE;
2052 return X86EMUL_CONTINUE;
2055 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2057 static int reported;
2059 unsigned long rip = vcpu->arch.rip;
2060 unsigned long rip_linear;
2062 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2067 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
2069 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2070 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
2073 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2075 static struct x86_emulate_ops emulate_ops = {
2076 .read_std = emulator_read_std,
2077 .read_emulated = emulator_read_emulated,
2078 .write_emulated = emulator_write_emulated,
2079 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2082 int emulate_instruction(struct kvm_vcpu *vcpu,
2083 struct kvm_run *run,
2089 struct decode_cache *c;
2091 vcpu->arch.mmio_fault_cr2 = cr2;
2092 kvm_x86_ops->cache_regs(vcpu);
2094 vcpu->mmio_is_write = 0;
2095 vcpu->arch.pio.string = 0;
2097 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
2099 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2101 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2102 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2103 vcpu->arch.emulate_ctxt.mode =
2104 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
2105 ? X86EMUL_MODE_REAL : cs_l
2106 ? X86EMUL_MODE_PROT64 : cs_db
2107 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2109 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
2110 vcpu->arch.emulate_ctxt.cs_base = 0;
2111 vcpu->arch.emulate_ctxt.ds_base = 0;
2112 vcpu->arch.emulate_ctxt.es_base = 0;
2113 vcpu->arch.emulate_ctxt.ss_base = 0;
2115 vcpu->arch.emulate_ctxt.cs_base =
2116 get_segment_base(vcpu, VCPU_SREG_CS);
2117 vcpu->arch.emulate_ctxt.ds_base =
2118 get_segment_base(vcpu, VCPU_SREG_DS);
2119 vcpu->arch.emulate_ctxt.es_base =
2120 get_segment_base(vcpu, VCPU_SREG_ES);
2121 vcpu->arch.emulate_ctxt.ss_base =
2122 get_segment_base(vcpu, VCPU_SREG_SS);
2125 vcpu->arch.emulate_ctxt.gs_base =
2126 get_segment_base(vcpu, VCPU_SREG_GS);
2127 vcpu->arch.emulate_ctxt.fs_base =
2128 get_segment_base(vcpu, VCPU_SREG_FS);
2130 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2132 /* Reject the instructions other than VMCALL/VMMCALL when
2133 * try to emulate invalid opcode */
2134 c = &vcpu->arch.emulate_ctxt.decode;
2135 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2136 (!(c->twobyte && c->b == 0x01 &&
2137 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2138 c->modrm_mod == 3 && c->modrm_rm == 1)))
2139 return EMULATE_FAIL;
2141 ++vcpu->stat.insn_emulation;
2143 ++vcpu->stat.insn_emulation_fail;
2144 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2145 return EMULATE_DONE;
2146 return EMULATE_FAIL;
2150 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2152 if (vcpu->arch.pio.string)
2153 return EMULATE_DO_MMIO;
2155 if ((r || vcpu->mmio_is_write) && run) {
2156 run->exit_reason = KVM_EXIT_MMIO;
2157 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2158 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2159 run->mmio.len = vcpu->mmio_size;
2160 run->mmio.is_write = vcpu->mmio_is_write;
2164 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2165 return EMULATE_DONE;
2166 if (!vcpu->mmio_needed) {
2167 kvm_report_emulation_failure(vcpu, "mmio");
2168 return EMULATE_FAIL;
2170 return EMULATE_DO_MMIO;
2173 kvm_x86_ops->decache_regs(vcpu);
2174 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
2176 if (vcpu->mmio_is_write) {
2177 vcpu->mmio_needed = 0;
2178 return EMULATE_DO_MMIO;
2181 return EMULATE_DONE;
2183 EXPORT_SYMBOL_GPL(emulate_instruction);
2185 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2189 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2190 if (vcpu->arch.pio.guest_pages[i]) {
2191 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2192 vcpu->arch.pio.guest_pages[i] = NULL;
2196 static int pio_copy_data(struct kvm_vcpu *vcpu)
2198 void *p = vcpu->arch.pio_data;
2201 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
2203 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
2206 free_pio_guest_pages(vcpu);
2209 q += vcpu->arch.pio.guest_page_offset;
2210 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2211 if (vcpu->arch.pio.in)
2212 memcpy(q, p, bytes);
2214 memcpy(p, q, bytes);
2215 q -= vcpu->arch.pio.guest_page_offset;
2217 free_pio_guest_pages(vcpu);
2221 int complete_pio(struct kvm_vcpu *vcpu)
2223 struct kvm_pio_request *io = &vcpu->arch.pio;
2227 kvm_x86_ops->cache_regs(vcpu);
2231 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
2235 r = pio_copy_data(vcpu);
2237 kvm_x86_ops->cache_regs(vcpu);
2244 delta *= io->cur_count;
2246 * The size of the register should really depend on
2247 * current address size.
2249 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2255 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2257 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2260 kvm_x86_ops->decache_regs(vcpu);
2262 io->count -= io->cur_count;
2268 static void kernel_pio(struct kvm_io_device *pio_dev,
2269 struct kvm_vcpu *vcpu,
2272 /* TODO: String I/O for in kernel device */
2274 mutex_lock(&vcpu->kvm->lock);
2275 if (vcpu->arch.pio.in)
2276 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2277 vcpu->arch.pio.size,
2280 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2281 vcpu->arch.pio.size,
2283 mutex_unlock(&vcpu->kvm->lock);
2286 static void pio_string_write(struct kvm_io_device *pio_dev,
2287 struct kvm_vcpu *vcpu)
2289 struct kvm_pio_request *io = &vcpu->arch.pio;
2290 void *pd = vcpu->arch.pio_data;
2293 mutex_lock(&vcpu->kvm->lock);
2294 for (i = 0; i < io->cur_count; i++) {
2295 kvm_iodevice_write(pio_dev, io->port,
2300 mutex_unlock(&vcpu->kvm->lock);
2303 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2306 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
2309 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2310 int size, unsigned port)
2312 struct kvm_io_device *pio_dev;
2314 vcpu->run->exit_reason = KVM_EXIT_IO;
2315 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2316 vcpu->run->io.size = vcpu->arch.pio.size = size;
2317 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2318 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2319 vcpu->run->io.port = vcpu->arch.pio.port = port;
2320 vcpu->arch.pio.in = in;
2321 vcpu->arch.pio.string = 0;
2322 vcpu->arch.pio.down = 0;
2323 vcpu->arch.pio.guest_page_offset = 0;
2324 vcpu->arch.pio.rep = 0;
2326 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2327 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2330 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2333 kvm_x86_ops->cache_regs(vcpu);
2334 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2335 kvm_x86_ops->decache_regs(vcpu);
2337 kvm_x86_ops->skip_emulated_instruction(vcpu);
2339 pio_dev = vcpu_find_pio_dev(vcpu, port);
2341 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2347 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2349 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2350 int size, unsigned long count, int down,
2351 gva_t address, int rep, unsigned port)
2353 unsigned now, in_page;
2357 struct kvm_io_device *pio_dev;
2359 vcpu->run->exit_reason = KVM_EXIT_IO;
2360 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2361 vcpu->run->io.size = vcpu->arch.pio.size = size;
2362 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2363 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2364 vcpu->run->io.port = vcpu->arch.pio.port = port;
2365 vcpu->arch.pio.in = in;
2366 vcpu->arch.pio.string = 1;
2367 vcpu->arch.pio.down = down;
2368 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2369 vcpu->arch.pio.rep = rep;
2371 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2372 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2375 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2379 kvm_x86_ops->skip_emulated_instruction(vcpu);
2384 in_page = PAGE_SIZE - offset_in_page(address);
2386 in_page = offset_in_page(address) + size;
2387 now = min(count, (unsigned long)in_page / size);
2390 * String I/O straddles page boundary. Pin two guest pages
2391 * so that we satisfy atomicity constraints. Do just one
2392 * transaction to avoid complexity.
2399 * String I/O in reverse. Yuck. Kill the guest, fix later.
2401 pr_unimpl(vcpu, "guest string pio down\n");
2402 kvm_inject_gp(vcpu, 0);
2405 vcpu->run->io.count = now;
2406 vcpu->arch.pio.cur_count = now;
2408 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2409 kvm_x86_ops->skip_emulated_instruction(vcpu);
2411 for (i = 0; i < nr_pages; ++i) {
2412 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2413 vcpu->arch.pio.guest_pages[i] = page;
2415 kvm_inject_gp(vcpu, 0);
2416 free_pio_guest_pages(vcpu);
2421 pio_dev = vcpu_find_pio_dev(vcpu, port);
2422 if (!vcpu->arch.pio.in) {
2423 /* string PIO write */
2424 ret = pio_copy_data(vcpu);
2425 if (ret >= 0 && pio_dev) {
2426 pio_string_write(pio_dev, vcpu);
2428 if (vcpu->arch.pio.count == 0)
2432 pr_unimpl(vcpu, "no string pio read support yet, "
2433 "port %x size %d count %ld\n",
2438 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2440 int kvm_arch_init(void *opaque)
2443 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2446 printk(KERN_ERR "kvm: already loaded the other module\n");
2451 if (!ops->cpu_has_kvm_support()) {
2452 printk(KERN_ERR "kvm: no hardware support\n");
2456 if (ops->disabled_by_bios()) {
2457 printk(KERN_ERR "kvm: disabled by bios\n");
2462 r = kvm_mmu_module_init();
2466 kvm_init_msr_list();
2469 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2470 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2471 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
2472 PT_DIRTY_MASK, PT64_NX_MASK, 0);
2479 void kvm_arch_exit(void)
2482 kvm_mmu_module_exit();
2485 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2487 ++vcpu->stat.halt_exits;
2488 KVMTRACE_0D(HLT, vcpu, handler);
2489 if (irqchip_in_kernel(vcpu->kvm)) {
2490 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
2491 up_read(&vcpu->kvm->slots_lock);
2492 kvm_vcpu_block(vcpu);
2493 down_read(&vcpu->kvm->slots_lock);
2494 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
2498 vcpu->run->exit_reason = KVM_EXIT_HLT;
2502 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2504 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2507 if (is_long_mode(vcpu))
2510 return a0 | ((gpa_t)a1 << 32);
2513 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2515 unsigned long nr, a0, a1, a2, a3, ret;
2518 kvm_x86_ops->cache_regs(vcpu);
2520 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2521 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2522 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2523 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2524 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2526 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2528 if (!is_long_mode(vcpu)) {
2537 case KVM_HC_VAPIC_POLL_IRQ:
2541 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2547 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2548 kvm_x86_ops->decache_regs(vcpu);
2549 ++vcpu->stat.hypercalls;
2552 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2554 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2556 char instruction[3];
2561 * Blow out the MMU to ensure that no other VCPU has an active mapping
2562 * to ensure that the updated hypercall appears atomically across all
2565 kvm_mmu_zap_all(vcpu->kvm);
2567 kvm_x86_ops->cache_regs(vcpu);
2568 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2569 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2570 != X86EMUL_CONTINUE)
2576 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2578 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2581 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2583 struct descriptor_table dt = { limit, base };
2585 kvm_x86_ops->set_gdt(vcpu, &dt);
2588 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2590 struct descriptor_table dt = { limit, base };
2592 kvm_x86_ops->set_idt(vcpu, &dt);
2595 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2596 unsigned long *rflags)
2598 kvm_lmsw(vcpu, msw);
2599 *rflags = kvm_x86_ops->get_rflags(vcpu);
2602 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2604 unsigned long value;
2606 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2609 value = vcpu->arch.cr0;
2612 value = vcpu->arch.cr2;
2615 value = vcpu->arch.cr3;
2618 value = vcpu->arch.cr4;
2621 value = kvm_get_cr8(vcpu);
2624 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
2627 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2628 (u32)((u64)value >> 32), handler);
2633 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2634 unsigned long *rflags)
2636 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2637 (u32)((u64)val >> 32), handler);
2641 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2642 *rflags = kvm_x86_ops->get_rflags(vcpu);
2645 vcpu->arch.cr2 = val;
2648 kvm_set_cr3(vcpu, val);
2651 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2654 kvm_set_cr8(vcpu, val & 0xfUL);
2657 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
2661 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2663 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2664 int j, nent = vcpu->arch.cpuid_nent;
2666 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2667 /* when no next entry is found, the current entry[i] is reselected */
2668 for (j = i + 1; j == i; j = (j + 1) % nent) {
2669 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2670 if (ej->function == e->function) {
2671 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2675 return 0; /* silence gcc, even though control never reaches here */
2678 /* find an entry with matching function, matching index (if needed), and that
2679 * should be read next (if it's stateful) */
2680 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2681 u32 function, u32 index)
2683 if (e->function != function)
2685 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2687 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2688 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2693 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2696 u32 function, index;
2697 struct kvm_cpuid_entry2 *e, *best;
2699 kvm_x86_ops->cache_regs(vcpu);
2700 function = vcpu->arch.regs[VCPU_REGS_RAX];
2701 index = vcpu->arch.regs[VCPU_REGS_RCX];
2702 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2703 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2704 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2705 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2707 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2708 e = &vcpu->arch.cpuid_entries[i];
2709 if (is_matching_cpuid_entry(e, function, index)) {
2710 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2711 move_to_next_stateful_cpuid_entry(vcpu, i);
2716 * Both basic or both extended?
2718 if (((e->function ^ function) & 0x80000000) == 0)
2719 if (!best || e->function > best->function)
2723 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2724 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2725 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2726 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2728 kvm_x86_ops->decache_regs(vcpu);
2729 kvm_x86_ops->skip_emulated_instruction(vcpu);
2730 KVMTRACE_5D(CPUID, vcpu, function,
2731 (u32)vcpu->arch.regs[VCPU_REGS_RAX],
2732 (u32)vcpu->arch.regs[VCPU_REGS_RBX],
2733 (u32)vcpu->arch.regs[VCPU_REGS_RCX],
2734 (u32)vcpu->arch.regs[VCPU_REGS_RDX], handler);
2736 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2739 * Check if userspace requested an interrupt window, and that the
2740 * interrupt window is open.
2742 * No need to exit to userspace if we already have an interrupt queued.
2744 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2745 struct kvm_run *kvm_run)
2747 return (!vcpu->arch.irq_summary &&
2748 kvm_run->request_interrupt_window &&
2749 vcpu->arch.interrupt_window_open &&
2750 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2753 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2754 struct kvm_run *kvm_run)
2756 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2757 kvm_run->cr8 = kvm_get_cr8(vcpu);
2758 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2759 if (irqchip_in_kernel(vcpu->kvm))
2760 kvm_run->ready_for_interrupt_injection = 1;
2762 kvm_run->ready_for_interrupt_injection =
2763 (vcpu->arch.interrupt_window_open &&
2764 vcpu->arch.irq_summary == 0);
2767 static void vapic_enter(struct kvm_vcpu *vcpu)
2769 struct kvm_lapic *apic = vcpu->arch.apic;
2772 if (!apic || !apic->vapic_addr)
2775 down_read(¤t->mm->mmap_sem);
2776 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2777 up_read(¤t->mm->mmap_sem);
2779 vcpu->arch.apic->vapic_page = page;
2782 static void vapic_exit(struct kvm_vcpu *vcpu)
2784 struct kvm_lapic *apic = vcpu->arch.apic;
2786 if (!apic || !apic->vapic_addr)
2789 kvm_release_page_dirty(apic->vapic_page);
2790 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2793 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2797 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
2798 pr_debug("vcpu %d received sipi with vector # %x\n",
2799 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2800 kvm_lapic_reset(vcpu);
2801 r = kvm_x86_ops->vcpu_reset(vcpu);
2804 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2807 down_read(&vcpu->kvm->slots_lock);
2811 if (vcpu->guest_debug.enabled)
2812 kvm_x86_ops->guest_debug_pre(vcpu);
2816 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
2817 kvm_mmu_unload(vcpu);
2819 r = kvm_mmu_reload(vcpu);
2823 if (vcpu->requests) {
2824 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2825 __kvm_migrate_timers(vcpu);
2826 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2827 kvm_x86_ops->tlb_flush(vcpu);
2828 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2830 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2834 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
2835 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2841 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
2842 kvm_inject_pending_timer_irqs(vcpu);
2846 kvm_x86_ops->prepare_guest_switch(vcpu);
2847 kvm_load_guest_fpu(vcpu);
2849 local_irq_disable();
2851 if (vcpu->requests || need_resched()) {
2858 if (signal_pending(current)) {
2862 kvm_run->exit_reason = KVM_EXIT_INTR;
2863 ++vcpu->stat.signal_exits;
2867 vcpu->guest_mode = 1;
2869 * Make sure that guest_mode assignment won't happen after
2870 * testing the pending IRQ vector bitmap.
2874 if (vcpu->arch.exception.pending)
2875 __queue_exception(vcpu);
2876 else if (irqchip_in_kernel(vcpu->kvm))
2877 kvm_x86_ops->inject_pending_irq(vcpu);
2879 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2881 kvm_lapic_sync_to_vapic(vcpu);
2883 up_read(&vcpu->kvm->slots_lock);
2888 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
2889 kvm_x86_ops->run(vcpu, kvm_run);
2891 vcpu->guest_mode = 0;
2897 * We must have an instruction between local_irq_enable() and
2898 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2899 * the interrupt shadow. The stat.exits increment will do nicely.
2900 * But we need to prevent reordering, hence this barrier():
2908 down_read(&vcpu->kvm->slots_lock);
2911 * Profile KVM exit RIPs:
2913 if (unlikely(prof_on == KVM_PROFILING)) {
2914 kvm_x86_ops->cache_regs(vcpu);
2915 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2918 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2919 vcpu->arch.exception.pending = false;
2921 kvm_lapic_sync_from_vapic(vcpu);
2923 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2926 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2928 kvm_run->exit_reason = KVM_EXIT_INTR;
2929 ++vcpu->stat.request_irq_exits;
2932 if (!need_resched())
2937 up_read(&vcpu->kvm->slots_lock);
2940 down_read(&vcpu->kvm->slots_lock);
2944 post_kvm_run_save(vcpu, kvm_run);
2946 down_read(&vcpu->kvm->slots_lock);
2948 up_read(&vcpu->kvm->slots_lock);
2953 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2960 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
2961 kvm_vcpu_block(vcpu);
2966 if (vcpu->sigset_active)
2967 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2969 /* re-sync apic's tpr */
2970 if (!irqchip_in_kernel(vcpu->kvm))
2971 kvm_set_cr8(vcpu, kvm_run->cr8);
2973 if (vcpu->arch.pio.cur_count) {
2974 r = complete_pio(vcpu);
2978 #if CONFIG_HAS_IOMEM
2979 if (vcpu->mmio_needed) {
2980 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
2981 vcpu->mmio_read_completed = 1;
2982 vcpu->mmio_needed = 0;
2984 down_read(&vcpu->kvm->slots_lock);
2985 r = emulate_instruction(vcpu, kvm_run,
2986 vcpu->arch.mmio_fault_cr2, 0,
2987 EMULTYPE_NO_DECODE);
2988 up_read(&vcpu->kvm->slots_lock);
2989 if (r == EMULATE_DO_MMIO) {
2991 * Read-modify-write. Back to userspace.
2998 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
2999 kvm_x86_ops->cache_regs(vcpu);
3000 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
3001 kvm_x86_ops->decache_regs(vcpu);
3004 r = __vcpu_run(vcpu, kvm_run);
3007 if (vcpu->sigset_active)
3008 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3014 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3018 kvm_x86_ops->cache_regs(vcpu);
3020 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
3021 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
3022 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
3023 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
3024 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
3025 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
3026 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3027 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
3028 #ifdef CONFIG_X86_64
3029 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
3030 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
3031 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
3032 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
3033 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
3034 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
3035 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
3036 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
3039 regs->rip = vcpu->arch.rip;
3040 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3043 * Don't leak debug flags in case they were set for guest debugging
3045 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
3046 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3053 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3057 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
3058 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
3059 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
3060 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
3061 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
3062 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
3063 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
3064 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
3065 #ifdef CONFIG_X86_64
3066 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
3067 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
3068 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
3069 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
3070 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
3071 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
3072 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
3073 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
3076 vcpu->arch.rip = regs->rip;
3077 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3079 kvm_x86_ops->decache_regs(vcpu);
3081 vcpu->arch.exception.pending = false;
3088 static void get_segment(struct kvm_vcpu *vcpu,
3089 struct kvm_segment *var, int seg)
3091 kvm_x86_ops->get_segment(vcpu, var, seg);
3094 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3096 struct kvm_segment cs;
3098 get_segment(vcpu, &cs, VCPU_SREG_CS);
3102 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3104 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3105 struct kvm_sregs *sregs)
3107 struct descriptor_table dt;
3112 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3113 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3114 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3115 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3116 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3117 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3119 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3120 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3122 kvm_x86_ops->get_idt(vcpu, &dt);
3123 sregs->idt.limit = dt.limit;
3124 sregs->idt.base = dt.base;
3125 kvm_x86_ops->get_gdt(vcpu, &dt);
3126 sregs->gdt.limit = dt.limit;
3127 sregs->gdt.base = dt.base;
3129 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3130 sregs->cr0 = vcpu->arch.cr0;
3131 sregs->cr2 = vcpu->arch.cr2;
3132 sregs->cr3 = vcpu->arch.cr3;
3133 sregs->cr4 = vcpu->arch.cr4;
3134 sregs->cr8 = kvm_get_cr8(vcpu);
3135 sregs->efer = vcpu->arch.shadow_efer;
3136 sregs->apic_base = kvm_get_apic_base(vcpu);
3138 if (irqchip_in_kernel(vcpu->kvm)) {
3139 memset(sregs->interrupt_bitmap, 0,
3140 sizeof sregs->interrupt_bitmap);
3141 pending_vec = kvm_x86_ops->get_irq(vcpu);
3142 if (pending_vec >= 0)
3143 set_bit(pending_vec,
3144 (unsigned long *)sregs->interrupt_bitmap);
3146 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
3147 sizeof sregs->interrupt_bitmap);
3154 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3155 struct kvm_mp_state *mp_state)
3158 mp_state->mp_state = vcpu->arch.mp_state;
3163 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3164 struct kvm_mp_state *mp_state)
3167 vcpu->arch.mp_state = mp_state->mp_state;
3172 static void set_segment(struct kvm_vcpu *vcpu,
3173 struct kvm_segment *var, int seg)
3175 kvm_x86_ops->set_segment(vcpu, var, seg);
3178 static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3179 struct kvm_segment *kvm_desct)
3181 kvm_desct->base = seg_desc->base0;
3182 kvm_desct->base |= seg_desc->base1 << 16;
3183 kvm_desct->base |= seg_desc->base2 << 24;
3184 kvm_desct->limit = seg_desc->limit0;
3185 kvm_desct->limit |= seg_desc->limit << 16;
3186 kvm_desct->selector = selector;
3187 kvm_desct->type = seg_desc->type;
3188 kvm_desct->present = seg_desc->p;
3189 kvm_desct->dpl = seg_desc->dpl;
3190 kvm_desct->db = seg_desc->d;
3191 kvm_desct->s = seg_desc->s;
3192 kvm_desct->l = seg_desc->l;
3193 kvm_desct->g = seg_desc->g;
3194 kvm_desct->avl = seg_desc->avl;
3196 kvm_desct->unusable = 1;
3198 kvm_desct->unusable = 0;
3199 kvm_desct->padding = 0;
3202 static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
3204 struct descriptor_table *dtable)
3206 if (selector & 1 << 2) {
3207 struct kvm_segment kvm_seg;
3209 get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
3211 if (kvm_seg.unusable)
3214 dtable->limit = kvm_seg.limit;
3215 dtable->base = kvm_seg.base;
3218 kvm_x86_ops->get_gdt(vcpu, dtable);
3221 /* allowed just for 8 bytes segments */
3222 static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3223 struct desc_struct *seg_desc)
3225 struct descriptor_table dtable;
3226 u16 index = selector >> 3;
3228 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3230 if (dtable.limit < index * 8 + 7) {
3231 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3234 return kvm_read_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8);
3237 /* allowed just for 8 bytes segments */
3238 static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3239 struct desc_struct *seg_desc)
3241 struct descriptor_table dtable;
3242 u16 index = selector >> 3;
3244 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3246 if (dtable.limit < index * 8 + 7)
3248 return kvm_write_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8);
3251 static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3252 struct desc_struct *seg_desc)
3256 base_addr = seg_desc->base0;
3257 base_addr |= (seg_desc->base1 << 16);
3258 base_addr |= (seg_desc->base2 << 24);
3263 static int load_tss_segment32(struct kvm_vcpu *vcpu,
3264 struct desc_struct *seg_desc,
3265 struct tss_segment_32 *tss)
3269 base_addr = get_tss_base_addr(vcpu, seg_desc);
3271 return kvm_read_guest(vcpu->kvm, base_addr, tss,
3272 sizeof(struct tss_segment_32));
3275 static int save_tss_segment32(struct kvm_vcpu *vcpu,
3276 struct desc_struct *seg_desc,
3277 struct tss_segment_32 *tss)
3281 base_addr = get_tss_base_addr(vcpu, seg_desc);
3283 return kvm_write_guest(vcpu->kvm, base_addr, tss,
3284 sizeof(struct tss_segment_32));
3287 static int load_tss_segment16(struct kvm_vcpu *vcpu,
3288 struct desc_struct *seg_desc,
3289 struct tss_segment_16 *tss)
3293 base_addr = get_tss_base_addr(vcpu, seg_desc);
3295 return kvm_read_guest(vcpu->kvm, base_addr, tss,
3296 sizeof(struct tss_segment_16));
3299 static int save_tss_segment16(struct kvm_vcpu *vcpu,
3300 struct desc_struct *seg_desc,
3301 struct tss_segment_16 *tss)
3305 base_addr = get_tss_base_addr(vcpu, seg_desc);
3307 return kvm_write_guest(vcpu->kvm, base_addr, tss,
3308 sizeof(struct tss_segment_16));
3311 static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3313 struct kvm_segment kvm_seg;
3315 get_segment(vcpu, &kvm_seg, seg);
3316 return kvm_seg.selector;
3319 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3321 struct kvm_segment *kvm_seg)
3323 struct desc_struct seg_desc;
3325 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3327 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3331 static int load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3332 int type_bits, int seg)
3334 struct kvm_segment kvm_seg;
3336 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3338 kvm_seg.type |= type_bits;
3340 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3341 seg != VCPU_SREG_LDTR)
3343 kvm_seg.unusable = 1;
3345 set_segment(vcpu, &kvm_seg, seg);
3349 static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3350 struct tss_segment_32 *tss)
3352 tss->cr3 = vcpu->arch.cr3;
3353 tss->eip = vcpu->arch.rip;
3354 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
3355 tss->eax = vcpu->arch.regs[VCPU_REGS_RAX];
3356 tss->ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3357 tss->edx = vcpu->arch.regs[VCPU_REGS_RDX];
3358 tss->ebx = vcpu->arch.regs[VCPU_REGS_RBX];
3359 tss->esp = vcpu->arch.regs[VCPU_REGS_RSP];
3360 tss->ebp = vcpu->arch.regs[VCPU_REGS_RBP];
3361 tss->esi = vcpu->arch.regs[VCPU_REGS_RSI];
3362 tss->edi = vcpu->arch.regs[VCPU_REGS_RDI];
3364 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3365 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3366 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3367 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3368 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3369 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3370 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3371 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3374 static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3375 struct tss_segment_32 *tss)
3377 kvm_set_cr3(vcpu, tss->cr3);
3379 vcpu->arch.rip = tss->eip;
3380 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3382 vcpu->arch.regs[VCPU_REGS_RAX] = tss->eax;
3383 vcpu->arch.regs[VCPU_REGS_RCX] = tss->ecx;
3384 vcpu->arch.regs[VCPU_REGS_RDX] = tss->edx;
3385 vcpu->arch.regs[VCPU_REGS_RBX] = tss->ebx;
3386 vcpu->arch.regs[VCPU_REGS_RSP] = tss->esp;
3387 vcpu->arch.regs[VCPU_REGS_RBP] = tss->ebp;
3388 vcpu->arch.regs[VCPU_REGS_RSI] = tss->esi;
3389 vcpu->arch.regs[VCPU_REGS_RDI] = tss->edi;
3391 if (load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
3394 if (load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
3397 if (load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
3400 if (load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
3403 if (load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
3406 if (load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
3409 if (load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
3414 static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3415 struct tss_segment_16 *tss)
3417 tss->ip = vcpu->arch.rip;
3418 tss->flag = kvm_x86_ops->get_rflags(vcpu);
3419 tss->ax = vcpu->arch.regs[VCPU_REGS_RAX];
3420 tss->cx = vcpu->arch.regs[VCPU_REGS_RCX];
3421 tss->dx = vcpu->arch.regs[VCPU_REGS_RDX];
3422 tss->bx = vcpu->arch.regs[VCPU_REGS_RBX];
3423 tss->sp = vcpu->arch.regs[VCPU_REGS_RSP];
3424 tss->bp = vcpu->arch.regs[VCPU_REGS_RBP];
3425 tss->si = vcpu->arch.regs[VCPU_REGS_RSI];
3426 tss->di = vcpu->arch.regs[VCPU_REGS_RDI];
3428 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3429 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3430 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3431 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3432 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3433 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3436 static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3437 struct tss_segment_16 *tss)
3439 vcpu->arch.rip = tss->ip;
3440 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
3441 vcpu->arch.regs[VCPU_REGS_RAX] = tss->ax;
3442 vcpu->arch.regs[VCPU_REGS_RCX] = tss->cx;
3443 vcpu->arch.regs[VCPU_REGS_RDX] = tss->dx;
3444 vcpu->arch.regs[VCPU_REGS_RBX] = tss->bx;
3445 vcpu->arch.regs[VCPU_REGS_RSP] = tss->sp;
3446 vcpu->arch.regs[VCPU_REGS_RBP] = tss->bp;
3447 vcpu->arch.regs[VCPU_REGS_RSI] = tss->si;
3448 vcpu->arch.regs[VCPU_REGS_RDI] = tss->di;
3450 if (load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
3453 if (load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
3456 if (load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
3459 if (load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
3462 if (load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
3467 static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
3468 struct desc_struct *cseg_desc,
3469 struct desc_struct *nseg_desc)
3471 struct tss_segment_16 tss_segment_16;
3474 if (load_tss_segment16(vcpu, cseg_desc, &tss_segment_16))
3477 save_state_to_tss16(vcpu, &tss_segment_16);
3478 save_tss_segment16(vcpu, cseg_desc, &tss_segment_16);
3480 if (load_tss_segment16(vcpu, nseg_desc, &tss_segment_16))
3482 if (load_state_from_tss16(vcpu, &tss_segment_16))
3490 static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
3491 struct desc_struct *cseg_desc,
3492 struct desc_struct *nseg_desc)
3494 struct tss_segment_32 tss_segment_32;
3497 if (load_tss_segment32(vcpu, cseg_desc, &tss_segment_32))
3500 save_state_to_tss32(vcpu, &tss_segment_32);
3501 save_tss_segment32(vcpu, cseg_desc, &tss_segment_32);
3503 if (load_tss_segment32(vcpu, nseg_desc, &tss_segment_32))
3505 if (load_state_from_tss32(vcpu, &tss_segment_32))
3513 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3515 struct kvm_segment tr_seg;
3516 struct desc_struct cseg_desc;
3517 struct desc_struct nseg_desc;
3520 get_segment(vcpu, &tr_seg, VCPU_SREG_TR);
3522 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3525 if (load_guest_segment_descriptor(vcpu, tr_seg.selector, &cseg_desc))
3529 if (reason != TASK_SWITCH_IRET) {
3532 cpl = kvm_x86_ops->get_cpl(vcpu);
3533 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3534 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3539 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3540 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3544 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3545 cseg_desc.type &= ~(1 << 1); //clear the B flag
3546 save_guest_segment_descriptor(vcpu, tr_seg.selector,
3550 if (reason == TASK_SWITCH_IRET) {
3551 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3552 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
3555 kvm_x86_ops->skip_emulated_instruction(vcpu);
3556 kvm_x86_ops->cache_regs(vcpu);
3558 if (nseg_desc.type & 8)
3559 ret = kvm_task_switch_32(vcpu, tss_selector, &cseg_desc,
3562 ret = kvm_task_switch_16(vcpu, tss_selector, &cseg_desc,
3565 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
3566 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3567 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
3570 if (reason != TASK_SWITCH_IRET) {
3571 nseg_desc.type |= (1 << 1);
3572 save_guest_segment_descriptor(vcpu, tss_selector,
3576 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
3577 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
3579 set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
3581 kvm_x86_ops->decache_regs(vcpu);
3584 EXPORT_SYMBOL_GPL(kvm_task_switch);
3586 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3587 struct kvm_sregs *sregs)
3589 int mmu_reset_needed = 0;
3590 int i, pending_vec, max_bits;
3591 struct descriptor_table dt;
3595 dt.limit = sregs->idt.limit;
3596 dt.base = sregs->idt.base;
3597 kvm_x86_ops->set_idt(vcpu, &dt);
3598 dt.limit = sregs->gdt.limit;
3599 dt.base = sregs->gdt.base;
3600 kvm_x86_ops->set_gdt(vcpu, &dt);
3602 vcpu->arch.cr2 = sregs->cr2;
3603 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
3604 vcpu->arch.cr3 = sregs->cr3;
3606 kvm_set_cr8(vcpu, sregs->cr8);
3608 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
3609 kvm_x86_ops->set_efer(vcpu, sregs->efer);
3610 kvm_set_apic_base(vcpu, sregs->apic_base);
3612 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3614 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
3615 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
3616 vcpu->arch.cr0 = sregs->cr0;
3618 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
3619 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3620 if (!is_long_mode(vcpu) && is_pae(vcpu))
3621 load_pdptrs(vcpu, vcpu->arch.cr3);
3623 if (mmu_reset_needed)
3624 kvm_mmu_reset_context(vcpu);
3626 if (!irqchip_in_kernel(vcpu->kvm)) {
3627 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3628 sizeof vcpu->arch.irq_pending);
3629 vcpu->arch.irq_summary = 0;
3630 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3631 if (vcpu->arch.irq_pending[i])
3632 __set_bit(i, &vcpu->arch.irq_summary);
3634 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3635 pending_vec = find_first_bit(
3636 (const unsigned long *)sregs->interrupt_bitmap,
3638 /* Only pending external irq is handled here */
3639 if (pending_vec < max_bits) {
3640 kvm_x86_ops->set_irq(vcpu, pending_vec);
3641 pr_debug("Set back pending irq %d\n",
3646 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3647 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3648 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3649 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3650 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3651 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3653 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3654 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3661 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3662 struct kvm_debug_guest *dbg)
3668 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3676 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3677 * we have asm/x86/processor.h
3688 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3689 #ifdef CONFIG_X86_64
3690 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3692 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3697 * Translate a guest virtual address to a guest physical address.
3699 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3700 struct kvm_translation *tr)
3702 unsigned long vaddr = tr->linear_address;
3706 down_read(&vcpu->kvm->slots_lock);
3707 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
3708 up_read(&vcpu->kvm->slots_lock);
3709 tr->physical_address = gpa;
3710 tr->valid = gpa != UNMAPPED_GVA;
3718 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3720 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3724 memcpy(fpu->fpr, fxsave->st_space, 128);
3725 fpu->fcw = fxsave->cwd;
3726 fpu->fsw = fxsave->swd;
3727 fpu->ftwx = fxsave->twd;
3728 fpu->last_opcode = fxsave->fop;
3729 fpu->last_ip = fxsave->rip;
3730 fpu->last_dp = fxsave->rdp;
3731 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3738 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3740 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3744 memcpy(fxsave->st_space, fpu->fpr, 128);
3745 fxsave->cwd = fpu->fcw;
3746 fxsave->swd = fpu->fsw;
3747 fxsave->twd = fpu->ftwx;
3748 fxsave->fop = fpu->last_opcode;
3749 fxsave->rip = fpu->last_ip;
3750 fxsave->rdp = fpu->last_dp;
3751 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3758 void fx_init(struct kvm_vcpu *vcpu)
3760 unsigned after_mxcsr_mask;
3763 * Touch the fpu the first time in non atomic context as if
3764 * this is the first fpu instruction the exception handler
3765 * will fire before the instruction returns and it'll have to
3766 * allocate ram with GFP_KERNEL.
3769 fx_save(&vcpu->arch.host_fx_image);
3771 /* Initialize guest FPU by resetting ours and saving into guest's */
3773 fx_save(&vcpu->arch.host_fx_image);
3775 fx_save(&vcpu->arch.guest_fx_image);
3776 fx_restore(&vcpu->arch.host_fx_image);
3779 vcpu->arch.cr0 |= X86_CR0_ET;
3780 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3781 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3782 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3783 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3785 EXPORT_SYMBOL_GPL(fx_init);
3787 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3789 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3792 vcpu->guest_fpu_loaded = 1;
3793 fx_save(&vcpu->arch.host_fx_image);
3794 fx_restore(&vcpu->arch.guest_fx_image);
3796 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3798 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3800 if (!vcpu->guest_fpu_loaded)
3803 vcpu->guest_fpu_loaded = 0;
3804 fx_save(&vcpu->arch.guest_fx_image);
3805 fx_restore(&vcpu->arch.host_fx_image);
3806 ++vcpu->stat.fpu_reload;
3808 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3810 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3812 kvm_x86_ops->vcpu_free(vcpu);
3815 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3818 return kvm_x86_ops->vcpu_create(kvm, id);
3821 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3825 /* We do fxsave: this must be aligned. */
3826 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3829 r = kvm_arch_vcpu_reset(vcpu);
3831 r = kvm_mmu_setup(vcpu);
3838 kvm_x86_ops->vcpu_free(vcpu);
3842 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3845 kvm_mmu_unload(vcpu);
3848 kvm_x86_ops->vcpu_free(vcpu);
3851 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3853 return kvm_x86_ops->vcpu_reset(vcpu);
3856 void kvm_arch_hardware_enable(void *garbage)
3858 kvm_x86_ops->hardware_enable(garbage);
3861 void kvm_arch_hardware_disable(void *garbage)
3863 kvm_x86_ops->hardware_disable(garbage);
3866 int kvm_arch_hardware_setup(void)
3868 return kvm_x86_ops->hardware_setup();
3871 void kvm_arch_hardware_unsetup(void)
3873 kvm_x86_ops->hardware_unsetup();
3876 void kvm_arch_check_processor_compat(void *rtn)
3878 kvm_x86_ops->check_processor_compatibility(rtn);
3881 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3887 BUG_ON(vcpu->kvm == NULL);
3890 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3891 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3892 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3894 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
3896 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3901 vcpu->arch.pio_data = page_address(page);
3903 r = kvm_mmu_create(vcpu);
3905 goto fail_free_pio_data;
3907 if (irqchip_in_kernel(kvm)) {
3908 r = kvm_create_lapic(vcpu);
3910 goto fail_mmu_destroy;
3916 kvm_mmu_destroy(vcpu);
3918 free_page((unsigned long)vcpu->arch.pio_data);
3923 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3925 kvm_free_lapic(vcpu);
3926 down_read(&vcpu->kvm->slots_lock);
3927 kvm_mmu_destroy(vcpu);
3928 up_read(&vcpu->kvm->slots_lock);
3929 free_page((unsigned long)vcpu->arch.pio_data);
3932 struct kvm *kvm_arch_create_vm(void)
3934 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3937 return ERR_PTR(-ENOMEM);
3939 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3944 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3947 kvm_mmu_unload(vcpu);
3951 static void kvm_free_vcpus(struct kvm *kvm)
3956 * Unpin any mmu pages first.
3958 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3960 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3961 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3962 if (kvm->vcpus[i]) {
3963 kvm_arch_vcpu_free(kvm->vcpus[i]);
3964 kvm->vcpus[i] = NULL;
3970 void kvm_arch_destroy_vm(struct kvm *kvm)
3973 kfree(kvm->arch.vpic);
3974 kfree(kvm->arch.vioapic);
3975 kvm_free_vcpus(kvm);
3976 kvm_free_physmem(kvm);
3977 if (kvm->arch.apic_access_page)
3978 put_page(kvm->arch.apic_access_page);
3979 if (kvm->arch.ept_identity_pagetable)
3980 put_page(kvm->arch.ept_identity_pagetable);
3984 int kvm_arch_set_memory_region(struct kvm *kvm,
3985 struct kvm_userspace_memory_region *mem,
3986 struct kvm_memory_slot old,
3989 int npages = mem->memory_size >> PAGE_SHIFT;
3990 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
3992 /*To keep backward compatibility with older userspace,
3993 *x86 needs to hanlde !user_alloc case.
3996 if (npages && !old.rmap) {
3997 down_write(¤t->mm->mmap_sem);
3998 memslot->userspace_addr = do_mmap(NULL, 0,
4000 PROT_READ | PROT_WRITE,
4001 MAP_SHARED | MAP_ANONYMOUS,
4003 up_write(¤t->mm->mmap_sem);
4005 if (IS_ERR((void *)memslot->userspace_addr))
4006 return PTR_ERR((void *)memslot->userspace_addr);
4008 if (!old.user_alloc && old.rmap) {
4011 down_write(¤t->mm->mmap_sem);
4012 ret = do_munmap(current->mm, old.userspace_addr,
4013 old.npages * PAGE_SIZE);
4014 up_write(¤t->mm->mmap_sem);
4017 "kvm_vm_ioctl_set_memory_region: "
4018 "failed to munmap memory\n");
4023 if (!kvm->arch.n_requested_mmu_pages) {
4024 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4025 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4028 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
4029 kvm_flush_remote_tlbs(kvm);
4034 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4036 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
4037 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
4040 static void vcpu_kick_intr(void *info)
4043 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
4044 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
4048 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4050 int ipi_pcpu = vcpu->cpu;
4051 int cpu = get_cpu();
4053 if (waitqueue_active(&vcpu->wq)) {
4054 wake_up_interruptible(&vcpu->wq);
4055 ++vcpu->stat.halt_wakeup;
4058 * We may be called synchronously with irqs disabled in guest mode,
4059 * So need not to call smp_call_function_single() in that case.
4061 if (vcpu->guest_mode && vcpu->cpu != cpu)
4062 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);