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 { "nmi_window", VCPU_STAT(nmi_window_exits) },
76 { "halt_exits", VCPU_STAT(halt_exits) },
77 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
78 { "hypercalls", VCPU_STAT(hypercalls) },
79 { "request_irq", VCPU_STAT(request_irq_exits) },
80 { "irq_exits", VCPU_STAT(irq_exits) },
81 { "host_state_reload", VCPU_STAT(host_state_reload) },
82 { "efer_reload", VCPU_STAT(efer_reload) },
83 { "fpu_reload", VCPU_STAT(fpu_reload) },
84 { "insn_emulation", VCPU_STAT(insn_emulation) },
85 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
86 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
87 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
88 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
89 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
90 { "mmu_flooded", VM_STAT(mmu_flooded) },
91 { "mmu_recycled", VM_STAT(mmu_recycled) },
92 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
93 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
94 { "largepages", VM_STAT(lpages) },
99 unsigned long segment_base(u16 selector)
101 struct descriptor_table gdt;
102 struct desc_struct *d;
103 unsigned long table_base;
109 asm("sgdt %0" : "=m"(gdt));
110 table_base = gdt.base;
112 if (selector & 4) { /* from ldt */
115 asm("sldt %0" : "=g"(ldt_selector));
116 table_base = segment_base(ldt_selector);
118 d = (struct desc_struct *)(table_base + (selector & ~7));
119 v = d->base0 | ((unsigned long)d->base1 << 16) |
120 ((unsigned long)d->base2 << 24);
122 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
123 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
127 EXPORT_SYMBOL_GPL(segment_base);
129 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
131 if (irqchip_in_kernel(vcpu->kvm))
132 return vcpu->arch.apic_base;
134 return vcpu->arch.apic_base;
136 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
138 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
140 /* TODO: reserve bits check */
141 if (irqchip_in_kernel(vcpu->kvm))
142 kvm_lapic_set_base(vcpu, data);
144 vcpu->arch.apic_base = data;
146 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
148 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
150 WARN_ON(vcpu->arch.exception.pending);
151 vcpu->arch.exception.pending = true;
152 vcpu->arch.exception.has_error_code = false;
153 vcpu->arch.exception.nr = nr;
155 EXPORT_SYMBOL_GPL(kvm_queue_exception);
157 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
160 ++vcpu->stat.pf_guest;
161 if (vcpu->arch.exception.pending) {
162 if (vcpu->arch.exception.nr == PF_VECTOR) {
163 printk(KERN_DEBUG "kvm: inject_page_fault:"
164 " double fault 0x%lx\n", addr);
165 vcpu->arch.exception.nr = DF_VECTOR;
166 vcpu->arch.exception.error_code = 0;
167 } else if (vcpu->arch.exception.nr == DF_VECTOR) {
168 /* triple fault -> shutdown */
169 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
173 vcpu->arch.cr2 = addr;
174 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
177 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
179 vcpu->arch.nmi_pending = 1;
181 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
183 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
185 WARN_ON(vcpu->arch.exception.pending);
186 vcpu->arch.exception.pending = true;
187 vcpu->arch.exception.has_error_code = true;
188 vcpu->arch.exception.nr = nr;
189 vcpu->arch.exception.error_code = error_code;
191 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
193 static void __queue_exception(struct kvm_vcpu *vcpu)
195 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
196 vcpu->arch.exception.has_error_code,
197 vcpu->arch.exception.error_code);
201 * Load the pae pdptrs. Return true is they are all valid.
203 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
205 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
206 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
209 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
211 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
212 offset * sizeof(u64), sizeof(pdpte));
217 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
218 if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
225 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
230 EXPORT_SYMBOL_GPL(load_pdptrs);
232 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
234 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
238 if (is_long_mode(vcpu) || !is_pae(vcpu))
241 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
244 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
250 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
252 if (cr0 & CR0_RESERVED_BITS) {
253 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
254 cr0, vcpu->arch.cr0);
255 kvm_inject_gp(vcpu, 0);
259 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
260 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
261 kvm_inject_gp(vcpu, 0);
265 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
266 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
267 "and a clear PE flag\n");
268 kvm_inject_gp(vcpu, 0);
272 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
274 if ((vcpu->arch.shadow_efer & EFER_LME)) {
278 printk(KERN_DEBUG "set_cr0: #GP, start paging "
279 "in long mode while PAE is disabled\n");
280 kvm_inject_gp(vcpu, 0);
283 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
285 printk(KERN_DEBUG "set_cr0: #GP, start paging "
286 "in long mode while CS.L == 1\n");
287 kvm_inject_gp(vcpu, 0);
293 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
294 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
296 kvm_inject_gp(vcpu, 0);
302 kvm_x86_ops->set_cr0(vcpu, cr0);
303 vcpu->arch.cr0 = cr0;
305 kvm_mmu_reset_context(vcpu);
308 EXPORT_SYMBOL_GPL(kvm_set_cr0);
310 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
312 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
313 KVMTRACE_1D(LMSW, vcpu,
314 (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
317 EXPORT_SYMBOL_GPL(kvm_lmsw);
319 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
321 if (cr4 & CR4_RESERVED_BITS) {
322 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
323 kvm_inject_gp(vcpu, 0);
327 if (is_long_mode(vcpu)) {
328 if (!(cr4 & X86_CR4_PAE)) {
329 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
331 kvm_inject_gp(vcpu, 0);
334 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
335 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
336 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
337 kvm_inject_gp(vcpu, 0);
341 if (cr4 & X86_CR4_VMXE) {
342 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
343 kvm_inject_gp(vcpu, 0);
346 kvm_x86_ops->set_cr4(vcpu, cr4);
347 vcpu->arch.cr4 = cr4;
348 kvm_mmu_reset_context(vcpu);
350 EXPORT_SYMBOL_GPL(kvm_set_cr4);
352 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
354 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
355 kvm_mmu_flush_tlb(vcpu);
359 if (is_long_mode(vcpu)) {
360 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
361 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
362 kvm_inject_gp(vcpu, 0);
367 if (cr3 & CR3_PAE_RESERVED_BITS) {
369 "set_cr3: #GP, reserved bits\n");
370 kvm_inject_gp(vcpu, 0);
373 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
374 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
376 kvm_inject_gp(vcpu, 0);
381 * We don't check reserved bits in nonpae mode, because
382 * this isn't enforced, and VMware depends on this.
387 * Does the new cr3 value map to physical memory? (Note, we
388 * catch an invalid cr3 even in real-mode, because it would
389 * cause trouble later on when we turn on paging anyway.)
391 * A real CPU would silently accept an invalid cr3 and would
392 * attempt to use it - with largely undefined (and often hard
393 * to debug) behavior on the guest side.
395 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
396 kvm_inject_gp(vcpu, 0);
398 vcpu->arch.cr3 = cr3;
399 vcpu->arch.mmu.new_cr3(vcpu);
402 EXPORT_SYMBOL_GPL(kvm_set_cr3);
404 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
406 if (cr8 & CR8_RESERVED_BITS) {
407 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
408 kvm_inject_gp(vcpu, 0);
411 if (irqchip_in_kernel(vcpu->kvm))
412 kvm_lapic_set_tpr(vcpu, cr8);
414 vcpu->arch.cr8 = cr8;
416 EXPORT_SYMBOL_GPL(kvm_set_cr8);
418 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
420 if (irqchip_in_kernel(vcpu->kvm))
421 return kvm_lapic_get_cr8(vcpu);
423 return vcpu->arch.cr8;
425 EXPORT_SYMBOL_GPL(kvm_get_cr8);
428 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
429 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
431 * This list is modified at module load time to reflect the
432 * capabilities of the host cpu.
434 static u32 msrs_to_save[] = {
435 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
438 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
440 MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
441 MSR_IA32_PERF_STATUS,
444 static unsigned num_msrs_to_save;
446 static u32 emulated_msrs[] = {
447 MSR_IA32_MISC_ENABLE,
450 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
452 if (efer & efer_reserved_bits) {
453 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
455 kvm_inject_gp(vcpu, 0);
460 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
461 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
462 kvm_inject_gp(vcpu, 0);
466 kvm_x86_ops->set_efer(vcpu, efer);
469 efer |= vcpu->arch.shadow_efer & EFER_LMA;
471 vcpu->arch.shadow_efer = efer;
474 void kvm_enable_efer_bits(u64 mask)
476 efer_reserved_bits &= ~mask;
478 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
482 * Writes msr value into into the appropriate "register".
483 * Returns 0 on success, non-0 otherwise.
484 * Assumes vcpu_load() was already called.
486 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
488 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
492 * Adapt set_msr() to msr_io()'s calling convention
494 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
496 return kvm_set_msr(vcpu, index, *data);
499 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
502 struct pvclock_wall_clock wc;
503 struct timespec now, sys, boot;
510 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
513 * The guest calculates current wall clock time by adding
514 * system time (updated by kvm_write_guest_time below) to the
515 * wall clock specified here. guest system time equals host
516 * system time for us, thus we must fill in host boot time here.
518 now = current_kernel_time();
520 boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
522 wc.sec = boot.tv_sec;
523 wc.nsec = boot.tv_nsec;
524 wc.version = version;
526 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
529 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
532 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
534 uint32_t quotient, remainder;
536 /* Don't try to replace with do_div(), this one calculates
537 * "(dividend << 32) / divisor" */
539 : "=a" (quotient), "=d" (remainder)
540 : "0" (0), "1" (dividend), "r" (divisor) );
544 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
546 uint64_t nsecs = 1000000000LL;
551 tps64 = tsc_khz * 1000LL;
552 while (tps64 > nsecs*2) {
557 tps32 = (uint32_t)tps64;
558 while (tps32 <= (uint32_t)nsecs) {
563 hv_clock->tsc_shift = shift;
564 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
566 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
567 __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
568 hv_clock->tsc_to_system_mul);
571 static void kvm_write_guest_time(struct kvm_vcpu *v)
575 struct kvm_vcpu_arch *vcpu = &v->arch;
578 if ((!vcpu->time_page))
581 if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
582 kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
583 vcpu->hv_clock_tsc_khz = tsc_khz;
586 /* Keep irq disabled to prevent changes to the clock */
587 local_irq_save(flags);
588 kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
589 &vcpu->hv_clock.tsc_timestamp);
591 local_irq_restore(flags);
593 /* With all the info we got, fill in the values */
595 vcpu->hv_clock.system_time = ts.tv_nsec +
596 (NSEC_PER_SEC * (u64)ts.tv_sec);
598 * The interface expects us to write an even number signaling that the
599 * update is finished. Since the guest won't see the intermediate
600 * state, we just increase by 2 at the end.
602 vcpu->hv_clock.version += 2;
604 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
606 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
607 sizeof(vcpu->hv_clock));
609 kunmap_atomic(shared_kaddr, KM_USER0);
611 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
614 static bool msr_mtrr_valid(unsigned msr)
617 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
618 case MSR_MTRRfix64K_00000:
619 case MSR_MTRRfix16K_80000:
620 case MSR_MTRRfix16K_A0000:
621 case MSR_MTRRfix4K_C0000:
622 case MSR_MTRRfix4K_C8000:
623 case MSR_MTRRfix4K_D0000:
624 case MSR_MTRRfix4K_D8000:
625 case MSR_MTRRfix4K_E0000:
626 case MSR_MTRRfix4K_E8000:
627 case MSR_MTRRfix4K_F0000:
628 case MSR_MTRRfix4K_F8000:
629 case MSR_MTRRdefType:
630 case MSR_IA32_CR_PAT:
638 static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
640 if (!msr_mtrr_valid(msr))
643 vcpu->arch.mtrr[msr - 0x200] = data;
647 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
651 set_efer(vcpu, data);
653 case MSR_IA32_MC0_STATUS:
654 pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
657 case MSR_IA32_MCG_STATUS:
658 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
661 case MSR_IA32_MCG_CTL:
662 pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
665 case MSR_IA32_UCODE_REV:
666 case MSR_IA32_UCODE_WRITE:
668 case 0x200 ... 0x2ff:
669 return set_msr_mtrr(vcpu, msr, data);
670 case MSR_IA32_APICBASE:
671 kvm_set_apic_base(vcpu, data);
673 case MSR_IA32_MISC_ENABLE:
674 vcpu->arch.ia32_misc_enable_msr = data;
676 case MSR_KVM_WALL_CLOCK:
677 vcpu->kvm->arch.wall_clock = data;
678 kvm_write_wall_clock(vcpu->kvm, data);
680 case MSR_KVM_SYSTEM_TIME: {
681 if (vcpu->arch.time_page) {
682 kvm_release_page_dirty(vcpu->arch.time_page);
683 vcpu->arch.time_page = NULL;
686 vcpu->arch.time = data;
688 /* we verify if the enable bit is set... */
692 /* ...but clean it before doing the actual write */
693 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
695 down_read(¤t->mm->mmap_sem);
696 vcpu->arch.time_page =
697 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
698 up_read(¤t->mm->mmap_sem);
700 if (is_error_page(vcpu->arch.time_page)) {
701 kvm_release_page_clean(vcpu->arch.time_page);
702 vcpu->arch.time_page = NULL;
705 kvm_write_guest_time(vcpu);
709 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
714 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
718 * Reads an msr value (of 'msr_index') into 'pdata'.
719 * Returns 0 on success, non-0 otherwise.
720 * Assumes vcpu_load() was already called.
722 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
724 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
727 static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
729 if (!msr_mtrr_valid(msr))
732 *pdata = vcpu->arch.mtrr[msr - 0x200];
736 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
741 case 0xc0010010: /* SYSCFG */
742 case 0xc0010015: /* HWCR */
743 case MSR_IA32_PLATFORM_ID:
744 case MSR_IA32_P5_MC_ADDR:
745 case MSR_IA32_P5_MC_TYPE:
746 case MSR_IA32_MC0_CTL:
747 case MSR_IA32_MCG_STATUS:
748 case MSR_IA32_MCG_CAP:
749 case MSR_IA32_MCG_CTL:
750 case MSR_IA32_MC0_MISC:
751 case MSR_IA32_MC0_MISC+4:
752 case MSR_IA32_MC0_MISC+8:
753 case MSR_IA32_MC0_MISC+12:
754 case MSR_IA32_MC0_MISC+16:
755 case MSR_IA32_UCODE_REV:
756 case MSR_IA32_EBL_CR_POWERON:
760 data = 0x500 | KVM_NR_VAR_MTRR;
762 case 0x200 ... 0x2ff:
763 return get_msr_mtrr(vcpu, msr, pdata);
764 case 0xcd: /* fsb frequency */
767 case MSR_IA32_APICBASE:
768 data = kvm_get_apic_base(vcpu);
770 case MSR_IA32_MISC_ENABLE:
771 data = vcpu->arch.ia32_misc_enable_msr;
773 case MSR_IA32_PERF_STATUS:
774 /* TSC increment by tick */
777 data |= (((uint64_t)4ULL) << 40);
780 data = vcpu->arch.shadow_efer;
782 case MSR_KVM_WALL_CLOCK:
783 data = vcpu->kvm->arch.wall_clock;
785 case MSR_KVM_SYSTEM_TIME:
786 data = vcpu->arch.time;
789 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
795 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
798 * Read or write a bunch of msrs. All parameters are kernel addresses.
800 * @return number of msrs set successfully.
802 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
803 struct kvm_msr_entry *entries,
804 int (*do_msr)(struct kvm_vcpu *vcpu,
805 unsigned index, u64 *data))
811 down_read(&vcpu->kvm->slots_lock);
812 for (i = 0; i < msrs->nmsrs; ++i)
813 if (do_msr(vcpu, entries[i].index, &entries[i].data))
815 up_read(&vcpu->kvm->slots_lock);
823 * Read or write a bunch of msrs. Parameters are user addresses.
825 * @return number of msrs set successfully.
827 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
828 int (*do_msr)(struct kvm_vcpu *vcpu,
829 unsigned index, u64 *data),
832 struct kvm_msrs msrs;
833 struct kvm_msr_entry *entries;
838 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
842 if (msrs.nmsrs >= MAX_IO_MSRS)
846 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
847 entries = vmalloc(size);
852 if (copy_from_user(entries, user_msrs->entries, size))
855 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
860 if (writeback && copy_to_user(user_msrs->entries, entries, size))
871 int kvm_dev_ioctl_check_extension(long ext)
876 case KVM_CAP_IRQCHIP:
878 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
879 case KVM_CAP_USER_MEMORY:
880 case KVM_CAP_SET_TSS_ADDR:
881 case KVM_CAP_EXT_CPUID:
882 case KVM_CAP_CLOCKSOURCE:
884 case KVM_CAP_NOP_IO_DELAY:
885 case KVM_CAP_MP_STATE:
889 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
891 case KVM_CAP_NR_VCPUS:
894 case KVM_CAP_NR_MEMSLOTS:
895 r = KVM_MEMORY_SLOTS;
908 long kvm_arch_dev_ioctl(struct file *filp,
909 unsigned int ioctl, unsigned long arg)
911 void __user *argp = (void __user *)arg;
915 case KVM_GET_MSR_INDEX_LIST: {
916 struct kvm_msr_list __user *user_msr_list = argp;
917 struct kvm_msr_list msr_list;
921 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
924 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
925 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
928 if (n < num_msrs_to_save)
931 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
932 num_msrs_to_save * sizeof(u32)))
934 if (copy_to_user(user_msr_list->indices
935 + num_msrs_to_save * sizeof(u32),
937 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
942 case KVM_GET_SUPPORTED_CPUID: {
943 struct kvm_cpuid2 __user *cpuid_arg = argp;
944 struct kvm_cpuid2 cpuid;
947 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
949 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
955 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
967 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
969 kvm_x86_ops->vcpu_load(vcpu, cpu);
970 kvm_write_guest_time(vcpu);
973 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
975 kvm_x86_ops->vcpu_put(vcpu);
976 kvm_put_guest_fpu(vcpu);
979 static int is_efer_nx(void)
983 rdmsrl(MSR_EFER, efer);
984 return efer & EFER_NX;
987 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
990 struct kvm_cpuid_entry2 *e, *entry;
993 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
994 e = &vcpu->arch.cpuid_entries[i];
995 if (e->function == 0x80000001) {
1000 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
1001 entry->edx &= ~(1 << 20);
1002 printk(KERN_INFO "kvm: guest NX capability removed\n");
1006 /* when an old userspace process fills a new kernel module */
1007 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1008 struct kvm_cpuid *cpuid,
1009 struct kvm_cpuid_entry __user *entries)
1012 struct kvm_cpuid_entry *cpuid_entries;
1015 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1018 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1022 if (copy_from_user(cpuid_entries, entries,
1023 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1025 for (i = 0; i < cpuid->nent; i++) {
1026 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1027 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1028 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1029 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1030 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1031 vcpu->arch.cpuid_entries[i].index = 0;
1032 vcpu->arch.cpuid_entries[i].flags = 0;
1033 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1034 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1035 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1037 vcpu->arch.cpuid_nent = cpuid->nent;
1038 cpuid_fix_nx_cap(vcpu);
1042 vfree(cpuid_entries);
1047 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1048 struct kvm_cpuid2 *cpuid,
1049 struct kvm_cpuid_entry2 __user *entries)
1054 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1057 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1058 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1060 vcpu->arch.cpuid_nent = cpuid->nent;
1067 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1068 struct kvm_cpuid2 *cpuid,
1069 struct kvm_cpuid_entry2 __user *entries)
1074 if (cpuid->nent < vcpu->arch.cpuid_nent)
1077 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1078 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1083 cpuid->nent = vcpu->arch.cpuid_nent;
1087 static inline u32 bit(int bitno)
1089 return 1 << (bitno & 31);
1092 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1095 entry->function = function;
1096 entry->index = index;
1097 cpuid_count(entry->function, entry->index,
1098 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1102 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1103 u32 index, int *nent, int maxnent)
1105 const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
1106 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1107 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1108 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1109 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1110 bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
1111 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1112 bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
1113 bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
1114 bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
1115 const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
1116 bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
1117 bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
1118 bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
1119 bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
1120 bit(X86_FEATURE_PGE) |
1121 bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
1122 bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
1123 bit(X86_FEATURE_SYSCALL) |
1124 (bit(X86_FEATURE_NX) && is_efer_nx()) |
1125 #ifdef CONFIG_X86_64
1126 bit(X86_FEATURE_LM) |
1128 bit(X86_FEATURE_MMXEXT) |
1129 bit(X86_FEATURE_3DNOWEXT) |
1130 bit(X86_FEATURE_3DNOW);
1131 const u32 kvm_supported_word3_x86_features =
1132 bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
1133 const u32 kvm_supported_word6_x86_features =
1134 bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
1136 /* all func 2 cpuid_count() should be called on the same cpu */
1138 do_cpuid_1_ent(entry, function, index);
1143 entry->eax = min(entry->eax, (u32)0xb);
1146 entry->edx &= kvm_supported_word0_x86_features;
1147 entry->ecx &= kvm_supported_word3_x86_features;
1149 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1150 * may return different values. This forces us to get_cpu() before
1151 * issuing the first command, and also to emulate this annoying behavior
1152 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1154 int t, times = entry->eax & 0xff;
1156 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1157 for (t = 1; t < times && *nent < maxnent; ++t) {
1158 do_cpuid_1_ent(&entry[t], function, 0);
1159 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1164 /* function 4 and 0xb have additional index. */
1168 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1169 /* read more entries until cache_type is zero */
1170 for (i = 1; *nent < maxnent; ++i) {
1171 cache_type = entry[i - 1].eax & 0x1f;
1174 do_cpuid_1_ent(&entry[i], function, i);
1176 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1184 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1185 /* read more entries until level_type is zero */
1186 for (i = 1; *nent < maxnent; ++i) {
1187 level_type = entry[i - 1].ecx & 0xff;
1190 do_cpuid_1_ent(&entry[i], function, i);
1192 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1198 entry->eax = min(entry->eax, 0x8000001a);
1201 entry->edx &= kvm_supported_word1_x86_features;
1202 entry->ecx &= kvm_supported_word6_x86_features;
1208 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1209 struct kvm_cpuid_entry2 __user *entries)
1211 struct kvm_cpuid_entry2 *cpuid_entries;
1212 int limit, nent = 0, r = -E2BIG;
1215 if (cpuid->nent < 1)
1218 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1222 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1223 limit = cpuid_entries[0].eax;
1224 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1225 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1226 &nent, cpuid->nent);
1228 if (nent >= cpuid->nent)
1231 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1232 limit = cpuid_entries[nent - 1].eax;
1233 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1234 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1235 &nent, cpuid->nent);
1237 if (copy_to_user(entries, cpuid_entries,
1238 nent * sizeof(struct kvm_cpuid_entry2)))
1244 vfree(cpuid_entries);
1249 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1250 struct kvm_lapic_state *s)
1253 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1259 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1260 struct kvm_lapic_state *s)
1263 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1264 kvm_apic_post_state_restore(vcpu);
1270 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1271 struct kvm_interrupt *irq)
1273 if (irq->irq < 0 || irq->irq >= 256)
1275 if (irqchip_in_kernel(vcpu->kvm))
1279 set_bit(irq->irq, vcpu->arch.irq_pending);
1280 set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
1287 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1288 struct kvm_tpr_access_ctl *tac)
1292 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1296 long kvm_arch_vcpu_ioctl(struct file *filp,
1297 unsigned int ioctl, unsigned long arg)
1299 struct kvm_vcpu *vcpu = filp->private_data;
1300 void __user *argp = (void __user *)arg;
1304 case KVM_GET_LAPIC: {
1305 struct kvm_lapic_state lapic;
1307 memset(&lapic, 0, sizeof lapic);
1308 r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
1312 if (copy_to_user(argp, &lapic, sizeof lapic))
1317 case KVM_SET_LAPIC: {
1318 struct kvm_lapic_state lapic;
1321 if (copy_from_user(&lapic, argp, sizeof lapic))
1323 r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
1329 case KVM_INTERRUPT: {
1330 struct kvm_interrupt irq;
1333 if (copy_from_user(&irq, argp, sizeof irq))
1335 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1341 case KVM_SET_CPUID: {
1342 struct kvm_cpuid __user *cpuid_arg = argp;
1343 struct kvm_cpuid cpuid;
1346 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1348 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
1353 case KVM_SET_CPUID2: {
1354 struct kvm_cpuid2 __user *cpuid_arg = argp;
1355 struct kvm_cpuid2 cpuid;
1358 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1360 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
1361 cpuid_arg->entries);
1366 case KVM_GET_CPUID2: {
1367 struct kvm_cpuid2 __user *cpuid_arg = argp;
1368 struct kvm_cpuid2 cpuid;
1371 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1373 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
1374 cpuid_arg->entries);
1378 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1384 r = msr_io(vcpu, argp, kvm_get_msr, 1);
1387 r = msr_io(vcpu, argp, do_set_msr, 0);
1389 case KVM_TPR_ACCESS_REPORTING: {
1390 struct kvm_tpr_access_ctl tac;
1393 if (copy_from_user(&tac, argp, sizeof tac))
1395 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
1399 if (copy_to_user(argp, &tac, sizeof tac))
1404 case KVM_SET_VAPIC_ADDR: {
1405 struct kvm_vapic_addr va;
1408 if (!irqchip_in_kernel(vcpu->kvm))
1411 if (copy_from_user(&va, argp, sizeof va))
1414 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
1424 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
1428 if (addr > (unsigned int)(-3 * PAGE_SIZE))
1430 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
1434 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
1435 u32 kvm_nr_mmu_pages)
1437 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
1440 down_write(&kvm->slots_lock);
1442 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
1443 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1445 up_write(&kvm->slots_lock);
1449 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1451 return kvm->arch.n_alloc_mmu_pages;
1454 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1457 struct kvm_mem_alias *alias;
1459 for (i = 0; i < kvm->arch.naliases; ++i) {
1460 alias = &kvm->arch.aliases[i];
1461 if (gfn >= alias->base_gfn
1462 && gfn < alias->base_gfn + alias->npages)
1463 return alias->target_gfn + gfn - alias->base_gfn;
1469 * Set a new alias region. Aliases map a portion of physical memory into
1470 * another portion. This is useful for memory windows, for example the PC
1473 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
1474 struct kvm_memory_alias *alias)
1477 struct kvm_mem_alias *p;
1480 /* General sanity checks */
1481 if (alias->memory_size & (PAGE_SIZE - 1))
1483 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
1485 if (alias->slot >= KVM_ALIAS_SLOTS)
1487 if (alias->guest_phys_addr + alias->memory_size
1488 < alias->guest_phys_addr)
1490 if (alias->target_phys_addr + alias->memory_size
1491 < alias->target_phys_addr)
1494 down_write(&kvm->slots_lock);
1496 p = &kvm->arch.aliases[alias->slot];
1497 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
1498 p->npages = alias->memory_size >> PAGE_SHIFT;
1499 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
1501 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
1502 if (kvm->arch.aliases[n - 1].npages)
1504 kvm->arch.naliases = n;
1506 kvm_mmu_zap_all(kvm);
1508 up_write(&kvm->slots_lock);
1516 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1521 switch (chip->chip_id) {
1522 case KVM_IRQCHIP_PIC_MASTER:
1523 memcpy(&chip->chip.pic,
1524 &pic_irqchip(kvm)->pics[0],
1525 sizeof(struct kvm_pic_state));
1527 case KVM_IRQCHIP_PIC_SLAVE:
1528 memcpy(&chip->chip.pic,
1529 &pic_irqchip(kvm)->pics[1],
1530 sizeof(struct kvm_pic_state));
1532 case KVM_IRQCHIP_IOAPIC:
1533 memcpy(&chip->chip.ioapic,
1534 ioapic_irqchip(kvm),
1535 sizeof(struct kvm_ioapic_state));
1544 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
1549 switch (chip->chip_id) {
1550 case KVM_IRQCHIP_PIC_MASTER:
1551 memcpy(&pic_irqchip(kvm)->pics[0],
1553 sizeof(struct kvm_pic_state));
1555 case KVM_IRQCHIP_PIC_SLAVE:
1556 memcpy(&pic_irqchip(kvm)->pics[1],
1558 sizeof(struct kvm_pic_state));
1560 case KVM_IRQCHIP_IOAPIC:
1561 memcpy(ioapic_irqchip(kvm),
1563 sizeof(struct kvm_ioapic_state));
1569 kvm_pic_update_irq(pic_irqchip(kvm));
1573 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1577 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
1581 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
1585 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
1586 kvm_pit_load_count(kvm, 0, ps->channels[0].count);
1591 * Get (and clear) the dirty memory log for a memory slot.
1593 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1594 struct kvm_dirty_log *log)
1598 struct kvm_memory_slot *memslot;
1601 down_write(&kvm->slots_lock);
1603 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1607 /* If nothing is dirty, don't bother messing with page tables. */
1609 kvm_mmu_slot_remove_write_access(kvm, log->slot);
1610 kvm_flush_remote_tlbs(kvm);
1611 memslot = &kvm->memslots[log->slot];
1612 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1613 memset(memslot->dirty_bitmap, 0, n);
1617 up_write(&kvm->slots_lock);
1621 long kvm_arch_vm_ioctl(struct file *filp,
1622 unsigned int ioctl, unsigned long arg)
1624 struct kvm *kvm = filp->private_data;
1625 void __user *argp = (void __user *)arg;
1629 case KVM_SET_TSS_ADDR:
1630 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1634 case KVM_SET_MEMORY_REGION: {
1635 struct kvm_memory_region kvm_mem;
1636 struct kvm_userspace_memory_region kvm_userspace_mem;
1639 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
1641 kvm_userspace_mem.slot = kvm_mem.slot;
1642 kvm_userspace_mem.flags = kvm_mem.flags;
1643 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
1644 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
1645 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
1650 case KVM_SET_NR_MMU_PAGES:
1651 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1655 case KVM_GET_NR_MMU_PAGES:
1656 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
1658 case KVM_SET_MEMORY_ALIAS: {
1659 struct kvm_memory_alias alias;
1662 if (copy_from_user(&alias, argp, sizeof alias))
1664 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
1669 case KVM_CREATE_IRQCHIP:
1671 kvm->arch.vpic = kvm_create_pic(kvm);
1672 if (kvm->arch.vpic) {
1673 r = kvm_ioapic_init(kvm);
1675 kfree(kvm->arch.vpic);
1676 kvm->arch.vpic = NULL;
1682 case KVM_CREATE_PIT:
1684 kvm->arch.vpit = kvm_create_pit(kvm);
1688 case KVM_IRQ_LINE: {
1689 struct kvm_irq_level irq_event;
1692 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1694 if (irqchip_in_kernel(kvm)) {
1695 mutex_lock(&kvm->lock);
1696 if (irq_event.irq < 16)
1697 kvm_pic_set_irq(pic_irqchip(kvm),
1700 kvm_ioapic_set_irq(kvm->arch.vioapic,
1703 mutex_unlock(&kvm->lock);
1708 case KVM_GET_IRQCHIP: {
1709 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1710 struct kvm_irqchip chip;
1713 if (copy_from_user(&chip, argp, sizeof chip))
1716 if (!irqchip_in_kernel(kvm))
1718 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1722 if (copy_to_user(argp, &chip, sizeof chip))
1727 case KVM_SET_IRQCHIP: {
1728 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1729 struct kvm_irqchip chip;
1732 if (copy_from_user(&chip, argp, sizeof chip))
1735 if (!irqchip_in_kernel(kvm))
1737 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1744 struct kvm_pit_state ps;
1746 if (copy_from_user(&ps, argp, sizeof ps))
1749 if (!kvm->arch.vpit)
1751 r = kvm_vm_ioctl_get_pit(kvm, &ps);
1755 if (copy_to_user(argp, &ps, sizeof ps))
1761 struct kvm_pit_state ps;
1763 if (copy_from_user(&ps, argp, sizeof ps))
1766 if (!kvm->arch.vpit)
1768 r = kvm_vm_ioctl_set_pit(kvm, &ps);
1781 static void kvm_init_msr_list(void)
1786 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1787 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1790 msrs_to_save[j] = msrs_to_save[i];
1793 num_msrs_to_save = j;
1797 * Only apic need an MMIO device hook, so shortcut now..
1799 static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
1800 gpa_t addr, int len,
1803 struct kvm_io_device *dev;
1805 if (vcpu->arch.apic) {
1806 dev = &vcpu->arch.apic->dev;
1807 if (dev->in_range(dev, addr, len, is_write))
1814 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1815 gpa_t addr, int len,
1818 struct kvm_io_device *dev;
1820 dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
1822 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
1827 int emulator_read_std(unsigned long addr,
1830 struct kvm_vcpu *vcpu)
1833 int r = X86EMUL_CONTINUE;
1836 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1837 unsigned offset = addr & (PAGE_SIZE-1);
1838 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1841 if (gpa == UNMAPPED_GVA) {
1842 r = X86EMUL_PROPAGATE_FAULT;
1845 ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
1847 r = X86EMUL_UNHANDLEABLE;
1858 EXPORT_SYMBOL_GPL(emulator_read_std);
1860 static int emulator_read_emulated(unsigned long addr,
1863 struct kvm_vcpu *vcpu)
1865 struct kvm_io_device *mmio_dev;
1868 if (vcpu->mmio_read_completed) {
1869 memcpy(val, vcpu->mmio_data, bytes);
1870 vcpu->mmio_read_completed = 0;
1871 return X86EMUL_CONTINUE;
1874 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1876 /* For APIC access vmexit */
1877 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1880 if (emulator_read_std(addr, val, bytes, vcpu)
1881 == X86EMUL_CONTINUE)
1882 return X86EMUL_CONTINUE;
1883 if (gpa == UNMAPPED_GVA)
1884 return X86EMUL_PROPAGATE_FAULT;
1888 * Is this MMIO handled locally?
1890 mutex_lock(&vcpu->kvm->lock);
1891 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
1893 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1894 mutex_unlock(&vcpu->kvm->lock);
1895 return X86EMUL_CONTINUE;
1897 mutex_unlock(&vcpu->kvm->lock);
1899 vcpu->mmio_needed = 1;
1900 vcpu->mmio_phys_addr = gpa;
1901 vcpu->mmio_size = bytes;
1902 vcpu->mmio_is_write = 0;
1904 return X86EMUL_UNHANDLEABLE;
1907 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1908 const void *val, int bytes)
1912 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
1915 kvm_mmu_pte_write(vcpu, gpa, val, bytes);
1919 static int emulator_write_emulated_onepage(unsigned long addr,
1922 struct kvm_vcpu *vcpu)
1924 struct kvm_io_device *mmio_dev;
1927 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
1929 if (gpa == UNMAPPED_GVA) {
1930 kvm_inject_page_fault(vcpu, addr, 2);
1931 return X86EMUL_PROPAGATE_FAULT;
1934 /* For APIC access vmexit */
1935 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
1938 if (emulator_write_phys(vcpu, gpa, val, bytes))
1939 return X86EMUL_CONTINUE;
1943 * Is this MMIO handled locally?
1945 mutex_lock(&vcpu->kvm->lock);
1946 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
1948 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1949 mutex_unlock(&vcpu->kvm->lock);
1950 return X86EMUL_CONTINUE;
1952 mutex_unlock(&vcpu->kvm->lock);
1954 vcpu->mmio_needed = 1;
1955 vcpu->mmio_phys_addr = gpa;
1956 vcpu->mmio_size = bytes;
1957 vcpu->mmio_is_write = 1;
1958 memcpy(vcpu->mmio_data, val, bytes);
1960 return X86EMUL_CONTINUE;
1963 int emulator_write_emulated(unsigned long addr,
1966 struct kvm_vcpu *vcpu)
1968 /* Crossing a page boundary? */
1969 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
1972 now = -addr & ~PAGE_MASK;
1973 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
1974 if (rc != X86EMUL_CONTINUE)
1980 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
1982 EXPORT_SYMBOL_GPL(emulator_write_emulated);
1984 static int emulator_cmpxchg_emulated(unsigned long addr,
1988 struct kvm_vcpu *vcpu)
1990 static int reported;
1994 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1996 #ifndef CONFIG_X86_64
1997 /* guests cmpxchg8b have to be emulated atomically */
2004 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2006 if (gpa == UNMAPPED_GVA ||
2007 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2010 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2015 down_read(¤t->mm->mmap_sem);
2016 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2017 up_read(¤t->mm->mmap_sem);
2019 kaddr = kmap_atomic(page, KM_USER0);
2020 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2021 kunmap_atomic(kaddr, KM_USER0);
2022 kvm_release_page_dirty(page);
2027 return emulator_write_emulated(addr, new, bytes, vcpu);
2030 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
2032 return kvm_x86_ops->get_segment_base(vcpu, seg);
2035 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
2037 return X86EMUL_CONTINUE;
2040 int emulate_clts(struct kvm_vcpu *vcpu)
2042 KVMTRACE_0D(CLTS, vcpu, handler);
2043 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
2044 return X86EMUL_CONTINUE;
2047 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
2049 struct kvm_vcpu *vcpu = ctxt->vcpu;
2053 *dest = kvm_x86_ops->get_dr(vcpu, dr);
2054 return X86EMUL_CONTINUE;
2056 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
2057 return X86EMUL_UNHANDLEABLE;
2061 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
2063 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
2066 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
2068 /* FIXME: better handling */
2069 return X86EMUL_UNHANDLEABLE;
2071 return X86EMUL_CONTINUE;
2074 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
2076 static int reported;
2078 unsigned long rip = vcpu->arch.rip;
2079 unsigned long rip_linear;
2081 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
2086 emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
2088 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2089 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
2092 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
2094 static struct x86_emulate_ops emulate_ops = {
2095 .read_std = emulator_read_std,
2096 .read_emulated = emulator_read_emulated,
2097 .write_emulated = emulator_write_emulated,
2098 .cmpxchg_emulated = emulator_cmpxchg_emulated,
2101 int emulate_instruction(struct kvm_vcpu *vcpu,
2102 struct kvm_run *run,
2108 struct decode_cache *c;
2110 vcpu->arch.mmio_fault_cr2 = cr2;
2111 kvm_x86_ops->cache_regs(vcpu);
2113 vcpu->mmio_is_write = 0;
2114 vcpu->arch.pio.string = 0;
2116 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
2118 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
2120 vcpu->arch.emulate_ctxt.vcpu = vcpu;
2121 vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
2122 vcpu->arch.emulate_ctxt.mode =
2123 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
2124 ? X86EMUL_MODE_REAL : cs_l
2125 ? X86EMUL_MODE_PROT64 : cs_db
2126 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
2128 if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
2129 vcpu->arch.emulate_ctxt.cs_base = 0;
2130 vcpu->arch.emulate_ctxt.ds_base = 0;
2131 vcpu->arch.emulate_ctxt.es_base = 0;
2132 vcpu->arch.emulate_ctxt.ss_base = 0;
2134 vcpu->arch.emulate_ctxt.cs_base =
2135 get_segment_base(vcpu, VCPU_SREG_CS);
2136 vcpu->arch.emulate_ctxt.ds_base =
2137 get_segment_base(vcpu, VCPU_SREG_DS);
2138 vcpu->arch.emulate_ctxt.es_base =
2139 get_segment_base(vcpu, VCPU_SREG_ES);
2140 vcpu->arch.emulate_ctxt.ss_base =
2141 get_segment_base(vcpu, VCPU_SREG_SS);
2144 vcpu->arch.emulate_ctxt.gs_base =
2145 get_segment_base(vcpu, VCPU_SREG_GS);
2146 vcpu->arch.emulate_ctxt.fs_base =
2147 get_segment_base(vcpu, VCPU_SREG_FS);
2149 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2151 /* Reject the instructions other than VMCALL/VMMCALL when
2152 * try to emulate invalid opcode */
2153 c = &vcpu->arch.emulate_ctxt.decode;
2154 if ((emulation_type & EMULTYPE_TRAP_UD) &&
2155 (!(c->twobyte && c->b == 0x01 &&
2156 (c->modrm_reg == 0 || c->modrm_reg == 3) &&
2157 c->modrm_mod == 3 && c->modrm_rm == 1)))
2158 return EMULATE_FAIL;
2160 ++vcpu->stat.insn_emulation;
2162 ++vcpu->stat.insn_emulation_fail;
2163 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2164 return EMULATE_DONE;
2165 return EMULATE_FAIL;
2169 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
2171 if (vcpu->arch.pio.string)
2172 return EMULATE_DO_MMIO;
2174 if ((r || vcpu->mmio_is_write) && run) {
2175 run->exit_reason = KVM_EXIT_MMIO;
2176 run->mmio.phys_addr = vcpu->mmio_phys_addr;
2177 memcpy(run->mmio.data, vcpu->mmio_data, 8);
2178 run->mmio.len = vcpu->mmio_size;
2179 run->mmio.is_write = vcpu->mmio_is_write;
2183 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
2184 return EMULATE_DONE;
2185 if (!vcpu->mmio_needed) {
2186 kvm_report_emulation_failure(vcpu, "mmio");
2187 return EMULATE_FAIL;
2189 return EMULATE_DO_MMIO;
2192 kvm_x86_ops->decache_regs(vcpu);
2193 kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
2195 if (vcpu->mmio_is_write) {
2196 vcpu->mmio_needed = 0;
2197 return EMULATE_DO_MMIO;
2200 return EMULATE_DONE;
2202 EXPORT_SYMBOL_GPL(emulate_instruction);
2204 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
2208 for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
2209 if (vcpu->arch.pio.guest_pages[i]) {
2210 kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
2211 vcpu->arch.pio.guest_pages[i] = NULL;
2215 static int pio_copy_data(struct kvm_vcpu *vcpu)
2217 void *p = vcpu->arch.pio_data;
2220 int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
2222 q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
2225 free_pio_guest_pages(vcpu);
2228 q += vcpu->arch.pio.guest_page_offset;
2229 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
2230 if (vcpu->arch.pio.in)
2231 memcpy(q, p, bytes);
2233 memcpy(p, q, bytes);
2234 q -= vcpu->arch.pio.guest_page_offset;
2236 free_pio_guest_pages(vcpu);
2240 int complete_pio(struct kvm_vcpu *vcpu)
2242 struct kvm_pio_request *io = &vcpu->arch.pio;
2246 kvm_x86_ops->cache_regs(vcpu);
2250 memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
2254 r = pio_copy_data(vcpu);
2256 kvm_x86_ops->cache_regs(vcpu);
2263 delta *= io->cur_count;
2265 * The size of the register should really depend on
2266 * current address size.
2268 vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
2274 vcpu->arch.regs[VCPU_REGS_RDI] += delta;
2276 vcpu->arch.regs[VCPU_REGS_RSI] += delta;
2279 kvm_x86_ops->decache_regs(vcpu);
2281 io->count -= io->cur_count;
2287 static void kernel_pio(struct kvm_io_device *pio_dev,
2288 struct kvm_vcpu *vcpu,
2291 /* TODO: String I/O for in kernel device */
2293 mutex_lock(&vcpu->kvm->lock);
2294 if (vcpu->arch.pio.in)
2295 kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
2296 vcpu->arch.pio.size,
2299 kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
2300 vcpu->arch.pio.size,
2302 mutex_unlock(&vcpu->kvm->lock);
2305 static void pio_string_write(struct kvm_io_device *pio_dev,
2306 struct kvm_vcpu *vcpu)
2308 struct kvm_pio_request *io = &vcpu->arch.pio;
2309 void *pd = vcpu->arch.pio_data;
2312 mutex_lock(&vcpu->kvm->lock);
2313 for (i = 0; i < io->cur_count; i++) {
2314 kvm_iodevice_write(pio_dev, io->port,
2319 mutex_unlock(&vcpu->kvm->lock);
2322 static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
2323 gpa_t addr, int len,
2326 return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
2329 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2330 int size, unsigned port)
2332 struct kvm_io_device *pio_dev;
2334 vcpu->run->exit_reason = KVM_EXIT_IO;
2335 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2336 vcpu->run->io.size = vcpu->arch.pio.size = size;
2337 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2338 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
2339 vcpu->run->io.port = vcpu->arch.pio.port = port;
2340 vcpu->arch.pio.in = in;
2341 vcpu->arch.pio.string = 0;
2342 vcpu->arch.pio.down = 0;
2343 vcpu->arch.pio.guest_page_offset = 0;
2344 vcpu->arch.pio.rep = 0;
2346 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2347 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2350 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2353 kvm_x86_ops->cache_regs(vcpu);
2354 memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
2356 kvm_x86_ops->skip_emulated_instruction(vcpu);
2358 pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
2360 kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
2366 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
2368 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
2369 int size, unsigned long count, int down,
2370 gva_t address, int rep, unsigned port)
2372 unsigned now, in_page;
2376 struct kvm_io_device *pio_dev;
2378 vcpu->run->exit_reason = KVM_EXIT_IO;
2379 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
2380 vcpu->run->io.size = vcpu->arch.pio.size = size;
2381 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
2382 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
2383 vcpu->run->io.port = vcpu->arch.pio.port = port;
2384 vcpu->arch.pio.in = in;
2385 vcpu->arch.pio.string = 1;
2386 vcpu->arch.pio.down = down;
2387 vcpu->arch.pio.guest_page_offset = offset_in_page(address);
2388 vcpu->arch.pio.rep = rep;
2390 if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
2391 KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
2394 KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
2398 kvm_x86_ops->skip_emulated_instruction(vcpu);
2403 in_page = PAGE_SIZE - offset_in_page(address);
2405 in_page = offset_in_page(address) + size;
2406 now = min(count, (unsigned long)in_page / size);
2409 * String I/O straddles page boundary. Pin two guest pages
2410 * so that we satisfy atomicity constraints. Do just one
2411 * transaction to avoid complexity.
2418 * String I/O in reverse. Yuck. Kill the guest, fix later.
2420 pr_unimpl(vcpu, "guest string pio down\n");
2421 kvm_inject_gp(vcpu, 0);
2424 vcpu->run->io.count = now;
2425 vcpu->arch.pio.cur_count = now;
2427 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
2428 kvm_x86_ops->skip_emulated_instruction(vcpu);
2430 for (i = 0; i < nr_pages; ++i) {
2431 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
2432 vcpu->arch.pio.guest_pages[i] = page;
2434 kvm_inject_gp(vcpu, 0);
2435 free_pio_guest_pages(vcpu);
2440 pio_dev = vcpu_find_pio_dev(vcpu, port,
2441 vcpu->arch.pio.cur_count,
2442 !vcpu->arch.pio.in);
2443 if (!vcpu->arch.pio.in) {
2444 /* string PIO write */
2445 ret = pio_copy_data(vcpu);
2446 if (ret >= 0 && pio_dev) {
2447 pio_string_write(pio_dev, vcpu);
2449 if (vcpu->arch.pio.count == 0)
2453 pr_unimpl(vcpu, "no string pio read support yet, "
2454 "port %x size %d count %ld\n",
2459 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
2461 int kvm_arch_init(void *opaque)
2464 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
2467 printk(KERN_ERR "kvm: already loaded the other module\n");
2472 if (!ops->cpu_has_kvm_support()) {
2473 printk(KERN_ERR "kvm: no hardware support\n");
2477 if (ops->disabled_by_bios()) {
2478 printk(KERN_ERR "kvm: disabled by bios\n");
2483 r = kvm_mmu_module_init();
2487 kvm_init_msr_list();
2490 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2491 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
2492 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
2493 PT_DIRTY_MASK, PT64_NX_MASK, 0);
2500 void kvm_arch_exit(void)
2503 kvm_mmu_module_exit();
2506 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
2508 ++vcpu->stat.halt_exits;
2509 KVMTRACE_0D(HLT, vcpu, handler);
2510 if (irqchip_in_kernel(vcpu->kvm)) {
2511 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
2512 up_read(&vcpu->kvm->slots_lock);
2513 kvm_vcpu_block(vcpu);
2514 down_read(&vcpu->kvm->slots_lock);
2515 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
2519 vcpu->run->exit_reason = KVM_EXIT_HLT;
2523 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
2525 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
2528 if (is_long_mode(vcpu))
2531 return a0 | ((gpa_t)a1 << 32);
2534 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
2536 unsigned long nr, a0, a1, a2, a3, ret;
2539 kvm_x86_ops->cache_regs(vcpu);
2541 nr = vcpu->arch.regs[VCPU_REGS_RAX];
2542 a0 = vcpu->arch.regs[VCPU_REGS_RBX];
2543 a1 = vcpu->arch.regs[VCPU_REGS_RCX];
2544 a2 = vcpu->arch.regs[VCPU_REGS_RDX];
2545 a3 = vcpu->arch.regs[VCPU_REGS_RSI];
2547 KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
2549 if (!is_long_mode(vcpu)) {
2558 case KVM_HC_VAPIC_POLL_IRQ:
2562 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
2568 vcpu->arch.regs[VCPU_REGS_RAX] = ret;
2569 kvm_x86_ops->decache_regs(vcpu);
2570 ++vcpu->stat.hypercalls;
2573 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
2575 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
2577 char instruction[3];
2582 * Blow out the MMU to ensure that no other VCPU has an active mapping
2583 * to ensure that the updated hypercall appears atomically across all
2586 kvm_mmu_zap_all(vcpu->kvm);
2588 kvm_x86_ops->cache_regs(vcpu);
2589 kvm_x86_ops->patch_hypercall(vcpu, instruction);
2590 if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
2591 != X86EMUL_CONTINUE)
2597 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
2599 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
2602 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2604 struct descriptor_table dt = { limit, base };
2606 kvm_x86_ops->set_gdt(vcpu, &dt);
2609 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
2611 struct descriptor_table dt = { limit, base };
2613 kvm_x86_ops->set_idt(vcpu, &dt);
2616 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
2617 unsigned long *rflags)
2619 kvm_lmsw(vcpu, msw);
2620 *rflags = kvm_x86_ops->get_rflags(vcpu);
2623 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
2625 unsigned long value;
2627 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
2630 value = vcpu->arch.cr0;
2633 value = vcpu->arch.cr2;
2636 value = vcpu->arch.cr3;
2639 value = vcpu->arch.cr4;
2642 value = kvm_get_cr8(vcpu);
2645 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
2648 KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
2649 (u32)((u64)value >> 32), handler);
2654 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
2655 unsigned long *rflags)
2657 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
2658 (u32)((u64)val >> 32), handler);
2662 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
2663 *rflags = kvm_x86_ops->get_rflags(vcpu);
2666 vcpu->arch.cr2 = val;
2669 kvm_set_cr3(vcpu, val);
2672 kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
2675 kvm_set_cr8(vcpu, val & 0xfUL);
2678 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
2682 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
2684 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
2685 int j, nent = vcpu->arch.cpuid_nent;
2687 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
2688 /* when no next entry is found, the current entry[i] is reselected */
2689 for (j = i + 1; j == i; j = (j + 1) % nent) {
2690 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
2691 if (ej->function == e->function) {
2692 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
2696 return 0; /* silence gcc, even though control never reaches here */
2699 /* find an entry with matching function, matching index (if needed), and that
2700 * should be read next (if it's stateful) */
2701 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
2702 u32 function, u32 index)
2704 if (e->function != function)
2706 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
2708 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
2709 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
2714 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
2717 u32 function, index;
2718 struct kvm_cpuid_entry2 *e, *best;
2720 kvm_x86_ops->cache_regs(vcpu);
2721 function = vcpu->arch.regs[VCPU_REGS_RAX];
2722 index = vcpu->arch.regs[VCPU_REGS_RCX];
2723 vcpu->arch.regs[VCPU_REGS_RAX] = 0;
2724 vcpu->arch.regs[VCPU_REGS_RBX] = 0;
2725 vcpu->arch.regs[VCPU_REGS_RCX] = 0;
2726 vcpu->arch.regs[VCPU_REGS_RDX] = 0;
2728 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
2729 e = &vcpu->arch.cpuid_entries[i];
2730 if (is_matching_cpuid_entry(e, function, index)) {
2731 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
2732 move_to_next_stateful_cpuid_entry(vcpu, i);
2737 * Both basic or both extended?
2739 if (((e->function ^ function) & 0x80000000) == 0)
2740 if (!best || e->function > best->function)
2744 vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
2745 vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
2746 vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
2747 vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
2749 kvm_x86_ops->decache_regs(vcpu);
2750 kvm_x86_ops->skip_emulated_instruction(vcpu);
2751 KVMTRACE_5D(CPUID, vcpu, function,
2752 (u32)vcpu->arch.regs[VCPU_REGS_RAX],
2753 (u32)vcpu->arch.regs[VCPU_REGS_RBX],
2754 (u32)vcpu->arch.regs[VCPU_REGS_RCX],
2755 (u32)vcpu->arch.regs[VCPU_REGS_RDX], handler);
2757 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
2760 * Check if userspace requested an interrupt window, and that the
2761 * interrupt window is open.
2763 * No need to exit to userspace if we already have an interrupt queued.
2765 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
2766 struct kvm_run *kvm_run)
2768 return (!vcpu->arch.irq_summary &&
2769 kvm_run->request_interrupt_window &&
2770 vcpu->arch.interrupt_window_open &&
2771 (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
2774 static void post_kvm_run_save(struct kvm_vcpu *vcpu,
2775 struct kvm_run *kvm_run)
2777 kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
2778 kvm_run->cr8 = kvm_get_cr8(vcpu);
2779 kvm_run->apic_base = kvm_get_apic_base(vcpu);
2780 if (irqchip_in_kernel(vcpu->kvm))
2781 kvm_run->ready_for_interrupt_injection = 1;
2783 kvm_run->ready_for_interrupt_injection =
2784 (vcpu->arch.interrupt_window_open &&
2785 vcpu->arch.irq_summary == 0);
2788 static void vapic_enter(struct kvm_vcpu *vcpu)
2790 struct kvm_lapic *apic = vcpu->arch.apic;
2793 if (!apic || !apic->vapic_addr)
2796 down_read(¤t->mm->mmap_sem);
2797 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2798 up_read(¤t->mm->mmap_sem);
2800 vcpu->arch.apic->vapic_page = page;
2803 static void vapic_exit(struct kvm_vcpu *vcpu)
2805 struct kvm_lapic *apic = vcpu->arch.apic;
2807 if (!apic || !apic->vapic_addr)
2810 kvm_release_page_dirty(apic->vapic_page);
2811 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
2814 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2818 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
2819 pr_debug("vcpu %d received sipi with vector # %x\n",
2820 vcpu->vcpu_id, vcpu->arch.sipi_vector);
2821 kvm_lapic_reset(vcpu);
2822 r = kvm_x86_ops->vcpu_reset(vcpu);
2825 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
2828 down_read(&vcpu->kvm->slots_lock);
2832 if (vcpu->guest_debug.enabled)
2833 kvm_x86_ops->guest_debug_pre(vcpu);
2837 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
2838 kvm_mmu_unload(vcpu);
2840 r = kvm_mmu_reload(vcpu);
2844 if (vcpu->requests) {
2845 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
2846 __kvm_migrate_timers(vcpu);
2847 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
2848 kvm_x86_ops->tlb_flush(vcpu);
2849 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
2851 kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
2855 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
2856 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2862 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
2863 kvm_inject_pending_timer_irqs(vcpu);
2867 kvm_x86_ops->prepare_guest_switch(vcpu);
2868 kvm_load_guest_fpu(vcpu);
2870 local_irq_disable();
2872 if (vcpu->requests || need_resched()) {
2879 if (signal_pending(current)) {
2883 kvm_run->exit_reason = KVM_EXIT_INTR;
2884 ++vcpu->stat.signal_exits;
2888 vcpu->guest_mode = 1;
2890 * Make sure that guest_mode assignment won't happen after
2891 * testing the pending IRQ vector bitmap.
2895 if (vcpu->arch.exception.pending)
2896 __queue_exception(vcpu);
2897 else if (irqchip_in_kernel(vcpu->kvm))
2898 kvm_x86_ops->inject_pending_irq(vcpu);
2900 kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
2902 kvm_lapic_sync_to_vapic(vcpu);
2904 up_read(&vcpu->kvm->slots_lock);
2909 KVMTRACE_0D(VMENTRY, vcpu, entryexit);
2910 kvm_x86_ops->run(vcpu, kvm_run);
2912 vcpu->guest_mode = 0;
2918 * We must have an instruction between local_irq_enable() and
2919 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2920 * the interrupt shadow. The stat.exits increment will do nicely.
2921 * But we need to prevent reordering, hence this barrier():
2929 down_read(&vcpu->kvm->slots_lock);
2932 * Profile KVM exit RIPs:
2934 if (unlikely(prof_on == KVM_PROFILING)) {
2935 kvm_x86_ops->cache_regs(vcpu);
2936 profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
2939 if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
2940 vcpu->arch.exception.pending = false;
2942 kvm_lapic_sync_from_vapic(vcpu);
2944 r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
2947 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2949 kvm_run->exit_reason = KVM_EXIT_INTR;
2950 ++vcpu->stat.request_irq_exits;
2953 if (!need_resched())
2958 up_read(&vcpu->kvm->slots_lock);
2961 down_read(&vcpu->kvm->slots_lock);
2965 post_kvm_run_save(vcpu, kvm_run);
2967 down_read(&vcpu->kvm->slots_lock);
2969 up_read(&vcpu->kvm->slots_lock);
2974 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2981 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
2982 kvm_vcpu_block(vcpu);
2987 if (vcpu->sigset_active)
2988 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2990 /* re-sync apic's tpr */
2991 if (!irqchip_in_kernel(vcpu->kvm))
2992 kvm_set_cr8(vcpu, kvm_run->cr8);
2994 if (vcpu->arch.pio.cur_count) {
2995 r = complete_pio(vcpu);
2999 #if CONFIG_HAS_IOMEM
3000 if (vcpu->mmio_needed) {
3001 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
3002 vcpu->mmio_read_completed = 1;
3003 vcpu->mmio_needed = 0;
3005 down_read(&vcpu->kvm->slots_lock);
3006 r = emulate_instruction(vcpu, kvm_run,
3007 vcpu->arch.mmio_fault_cr2, 0,
3008 EMULTYPE_NO_DECODE);
3009 up_read(&vcpu->kvm->slots_lock);
3010 if (r == EMULATE_DO_MMIO) {
3012 * Read-modify-write. Back to userspace.
3019 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
3020 kvm_x86_ops->cache_regs(vcpu);
3021 vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
3022 kvm_x86_ops->decache_regs(vcpu);
3025 r = __vcpu_run(vcpu, kvm_run);
3028 if (vcpu->sigset_active)
3029 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3035 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3039 kvm_x86_ops->cache_regs(vcpu);
3041 regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
3042 regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
3043 regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
3044 regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
3045 regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
3046 regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
3047 regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3048 regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
3049 #ifdef CONFIG_X86_64
3050 regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
3051 regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
3052 regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
3053 regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
3054 regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
3055 regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
3056 regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
3057 regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
3060 regs->rip = vcpu->arch.rip;
3061 regs->rflags = kvm_x86_ops->get_rflags(vcpu);
3064 * Don't leak debug flags in case they were set for guest debugging
3066 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
3067 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
3074 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3078 vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
3079 vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
3080 vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
3081 vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
3082 vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
3083 vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
3084 vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
3085 vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
3086 #ifdef CONFIG_X86_64
3087 vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
3088 vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
3089 vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
3090 vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
3091 vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
3092 vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
3093 vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
3094 vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
3097 vcpu->arch.rip = regs->rip;
3098 kvm_x86_ops->set_rflags(vcpu, regs->rflags);
3100 kvm_x86_ops->decache_regs(vcpu);
3102 vcpu->arch.exception.pending = false;
3109 void kvm_get_segment(struct kvm_vcpu *vcpu,
3110 struct kvm_segment *var, int seg)
3112 kvm_x86_ops->get_segment(vcpu, var, seg);
3115 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3117 struct kvm_segment cs;
3119 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
3123 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
3125 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3126 struct kvm_sregs *sregs)
3128 struct descriptor_table dt;
3133 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3134 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3135 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3136 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3137 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3138 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3140 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3141 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3143 kvm_x86_ops->get_idt(vcpu, &dt);
3144 sregs->idt.limit = dt.limit;
3145 sregs->idt.base = dt.base;
3146 kvm_x86_ops->get_gdt(vcpu, &dt);
3147 sregs->gdt.limit = dt.limit;
3148 sregs->gdt.base = dt.base;
3150 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3151 sregs->cr0 = vcpu->arch.cr0;
3152 sregs->cr2 = vcpu->arch.cr2;
3153 sregs->cr3 = vcpu->arch.cr3;
3154 sregs->cr4 = vcpu->arch.cr4;
3155 sregs->cr8 = kvm_get_cr8(vcpu);
3156 sregs->efer = vcpu->arch.shadow_efer;
3157 sregs->apic_base = kvm_get_apic_base(vcpu);
3159 if (irqchip_in_kernel(vcpu->kvm)) {
3160 memset(sregs->interrupt_bitmap, 0,
3161 sizeof sregs->interrupt_bitmap);
3162 pending_vec = kvm_x86_ops->get_irq(vcpu);
3163 if (pending_vec >= 0)
3164 set_bit(pending_vec,
3165 (unsigned long *)sregs->interrupt_bitmap);
3167 memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
3168 sizeof sregs->interrupt_bitmap);
3175 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3176 struct kvm_mp_state *mp_state)
3179 mp_state->mp_state = vcpu->arch.mp_state;
3184 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3185 struct kvm_mp_state *mp_state)
3188 vcpu->arch.mp_state = mp_state->mp_state;
3193 static void kvm_set_segment(struct kvm_vcpu *vcpu,
3194 struct kvm_segment *var, int seg)
3196 kvm_x86_ops->set_segment(vcpu, var, seg);
3199 static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
3200 struct kvm_segment *kvm_desct)
3202 kvm_desct->base = seg_desc->base0;
3203 kvm_desct->base |= seg_desc->base1 << 16;
3204 kvm_desct->base |= seg_desc->base2 << 24;
3205 kvm_desct->limit = seg_desc->limit0;
3206 kvm_desct->limit |= seg_desc->limit << 16;
3207 kvm_desct->selector = selector;
3208 kvm_desct->type = seg_desc->type;
3209 kvm_desct->present = seg_desc->p;
3210 kvm_desct->dpl = seg_desc->dpl;
3211 kvm_desct->db = seg_desc->d;
3212 kvm_desct->s = seg_desc->s;
3213 kvm_desct->l = seg_desc->l;
3214 kvm_desct->g = seg_desc->g;
3215 kvm_desct->avl = seg_desc->avl;
3217 kvm_desct->unusable = 1;
3219 kvm_desct->unusable = 0;
3220 kvm_desct->padding = 0;
3223 static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
3225 struct descriptor_table *dtable)
3227 if (selector & 1 << 2) {
3228 struct kvm_segment kvm_seg;
3230 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
3232 if (kvm_seg.unusable)
3235 dtable->limit = kvm_seg.limit;
3236 dtable->base = kvm_seg.base;
3239 kvm_x86_ops->get_gdt(vcpu, dtable);
3242 /* allowed just for 8 bytes segments */
3243 static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3244 struct desc_struct *seg_desc)
3246 struct descriptor_table dtable;
3247 u16 index = selector >> 3;
3249 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3251 if (dtable.limit < index * 8 + 7) {
3252 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
3255 return kvm_read_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8);
3258 /* allowed just for 8 bytes segments */
3259 static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3260 struct desc_struct *seg_desc)
3262 struct descriptor_table dtable;
3263 u16 index = selector >> 3;
3265 get_segment_descritptor_dtable(vcpu, selector, &dtable);
3267 if (dtable.limit < index * 8 + 7)
3269 return kvm_write_guest(vcpu->kvm, dtable.base + index * 8, seg_desc, 8);
3272 static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
3273 struct desc_struct *seg_desc)
3277 base_addr = seg_desc->base0;
3278 base_addr |= (seg_desc->base1 << 16);
3279 base_addr |= (seg_desc->base2 << 24);
3284 static int load_tss_segment32(struct kvm_vcpu *vcpu,
3285 struct desc_struct *seg_desc,
3286 struct tss_segment_32 *tss)
3290 base_addr = get_tss_base_addr(vcpu, seg_desc);
3292 return kvm_read_guest(vcpu->kvm, base_addr, tss,
3293 sizeof(struct tss_segment_32));
3296 static int save_tss_segment32(struct kvm_vcpu *vcpu,
3297 struct desc_struct *seg_desc,
3298 struct tss_segment_32 *tss)
3302 base_addr = get_tss_base_addr(vcpu, seg_desc);
3304 return kvm_write_guest(vcpu->kvm, base_addr, tss,
3305 sizeof(struct tss_segment_32));
3308 static int load_tss_segment16(struct kvm_vcpu *vcpu,
3309 struct desc_struct *seg_desc,
3310 struct tss_segment_16 *tss)
3314 base_addr = get_tss_base_addr(vcpu, seg_desc);
3316 return kvm_read_guest(vcpu->kvm, base_addr, tss,
3317 sizeof(struct tss_segment_16));
3320 static int save_tss_segment16(struct kvm_vcpu *vcpu,
3321 struct desc_struct *seg_desc,
3322 struct tss_segment_16 *tss)
3326 base_addr = get_tss_base_addr(vcpu, seg_desc);
3328 return kvm_write_guest(vcpu->kvm, base_addr, tss,
3329 sizeof(struct tss_segment_16));
3332 static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
3334 struct kvm_segment kvm_seg;
3336 kvm_get_segment(vcpu, &kvm_seg, seg);
3337 return kvm_seg.selector;
3340 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
3342 struct kvm_segment *kvm_seg)
3344 struct desc_struct seg_desc;
3346 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
3348 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
3352 int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
3353 int type_bits, int seg)
3355 struct kvm_segment kvm_seg;
3357 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
3359 kvm_seg.type |= type_bits;
3361 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
3362 seg != VCPU_SREG_LDTR)
3364 kvm_seg.unusable = 1;
3366 kvm_set_segment(vcpu, &kvm_seg, seg);
3370 static void save_state_to_tss32(struct kvm_vcpu *vcpu,
3371 struct tss_segment_32 *tss)
3373 tss->cr3 = vcpu->arch.cr3;
3374 tss->eip = vcpu->arch.rip;
3375 tss->eflags = kvm_x86_ops->get_rflags(vcpu);
3376 tss->eax = vcpu->arch.regs[VCPU_REGS_RAX];
3377 tss->ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3378 tss->edx = vcpu->arch.regs[VCPU_REGS_RDX];
3379 tss->ebx = vcpu->arch.regs[VCPU_REGS_RBX];
3380 tss->esp = vcpu->arch.regs[VCPU_REGS_RSP];
3381 tss->ebp = vcpu->arch.regs[VCPU_REGS_RBP];
3382 tss->esi = vcpu->arch.regs[VCPU_REGS_RSI];
3383 tss->edi = vcpu->arch.regs[VCPU_REGS_RDI];
3385 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3386 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3387 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3388 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3389 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
3390 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
3391 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3392 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3395 static int load_state_from_tss32(struct kvm_vcpu *vcpu,
3396 struct tss_segment_32 *tss)
3398 kvm_set_cr3(vcpu, tss->cr3);
3400 vcpu->arch.rip = tss->eip;
3401 kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
3403 vcpu->arch.regs[VCPU_REGS_RAX] = tss->eax;
3404 vcpu->arch.regs[VCPU_REGS_RCX] = tss->ecx;
3405 vcpu->arch.regs[VCPU_REGS_RDX] = tss->edx;
3406 vcpu->arch.regs[VCPU_REGS_RBX] = tss->ebx;
3407 vcpu->arch.regs[VCPU_REGS_RSP] = tss->esp;
3408 vcpu->arch.regs[VCPU_REGS_RBP] = tss->ebp;
3409 vcpu->arch.regs[VCPU_REGS_RSI] = tss->esi;
3410 vcpu->arch.regs[VCPU_REGS_RDI] = tss->edi;
3412 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
3415 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
3418 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
3421 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
3424 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
3427 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
3430 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
3435 static void save_state_to_tss16(struct kvm_vcpu *vcpu,
3436 struct tss_segment_16 *tss)
3438 tss->ip = vcpu->arch.rip;
3439 tss->flag = kvm_x86_ops->get_rflags(vcpu);
3440 tss->ax = vcpu->arch.regs[VCPU_REGS_RAX];
3441 tss->cx = vcpu->arch.regs[VCPU_REGS_RCX];
3442 tss->dx = vcpu->arch.regs[VCPU_REGS_RDX];
3443 tss->bx = vcpu->arch.regs[VCPU_REGS_RBX];
3444 tss->sp = vcpu->arch.regs[VCPU_REGS_RSP];
3445 tss->bp = vcpu->arch.regs[VCPU_REGS_RBP];
3446 tss->si = vcpu->arch.regs[VCPU_REGS_RSI];
3447 tss->di = vcpu->arch.regs[VCPU_REGS_RDI];
3449 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
3450 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
3451 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
3452 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
3453 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
3454 tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
3457 static int load_state_from_tss16(struct kvm_vcpu *vcpu,
3458 struct tss_segment_16 *tss)
3460 vcpu->arch.rip = tss->ip;
3461 kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
3462 vcpu->arch.regs[VCPU_REGS_RAX] = tss->ax;
3463 vcpu->arch.regs[VCPU_REGS_RCX] = tss->cx;
3464 vcpu->arch.regs[VCPU_REGS_RDX] = tss->dx;
3465 vcpu->arch.regs[VCPU_REGS_RBX] = tss->bx;
3466 vcpu->arch.regs[VCPU_REGS_RSP] = tss->sp;
3467 vcpu->arch.regs[VCPU_REGS_RBP] = tss->bp;
3468 vcpu->arch.regs[VCPU_REGS_RSI] = tss->si;
3469 vcpu->arch.regs[VCPU_REGS_RDI] = tss->di;
3471 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
3474 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
3477 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
3480 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
3483 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
3488 static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
3489 struct desc_struct *cseg_desc,
3490 struct desc_struct *nseg_desc)
3492 struct tss_segment_16 tss_segment_16;
3495 if (load_tss_segment16(vcpu, cseg_desc, &tss_segment_16))
3498 save_state_to_tss16(vcpu, &tss_segment_16);
3499 save_tss_segment16(vcpu, cseg_desc, &tss_segment_16);
3501 if (load_tss_segment16(vcpu, nseg_desc, &tss_segment_16))
3503 if (load_state_from_tss16(vcpu, &tss_segment_16))
3511 static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
3512 struct desc_struct *cseg_desc,
3513 struct desc_struct *nseg_desc)
3515 struct tss_segment_32 tss_segment_32;
3518 if (load_tss_segment32(vcpu, cseg_desc, &tss_segment_32))
3521 save_state_to_tss32(vcpu, &tss_segment_32);
3522 save_tss_segment32(vcpu, cseg_desc, &tss_segment_32);
3524 if (load_tss_segment32(vcpu, nseg_desc, &tss_segment_32))
3526 if (load_state_from_tss32(vcpu, &tss_segment_32))
3534 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
3536 struct kvm_segment tr_seg;
3537 struct desc_struct cseg_desc;
3538 struct desc_struct nseg_desc;
3541 kvm_get_segment(vcpu, &tr_seg, VCPU_SREG_TR);
3543 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
3546 if (load_guest_segment_descriptor(vcpu, tr_seg.selector, &cseg_desc))
3550 if (reason != TASK_SWITCH_IRET) {
3553 cpl = kvm_x86_ops->get_cpl(vcpu);
3554 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
3555 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
3560 if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
3561 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
3565 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
3566 cseg_desc.type &= ~(1 << 1); //clear the B flag
3567 save_guest_segment_descriptor(vcpu, tr_seg.selector,
3571 if (reason == TASK_SWITCH_IRET) {
3572 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3573 kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
3576 kvm_x86_ops->skip_emulated_instruction(vcpu);
3577 kvm_x86_ops->cache_regs(vcpu);
3579 if (nseg_desc.type & 8)
3580 ret = kvm_task_switch_32(vcpu, tss_selector, &cseg_desc,
3583 ret = kvm_task_switch_16(vcpu, tss_selector, &cseg_desc,
3586 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
3587 u32 eflags = kvm_x86_ops->get_rflags(vcpu);
3588 kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
3591 if (reason != TASK_SWITCH_IRET) {
3592 nseg_desc.type |= (1 << 1);
3593 save_guest_segment_descriptor(vcpu, tss_selector,
3597 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
3598 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
3600 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
3602 kvm_x86_ops->decache_regs(vcpu);
3605 EXPORT_SYMBOL_GPL(kvm_task_switch);
3607 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3608 struct kvm_sregs *sregs)
3610 int mmu_reset_needed = 0;
3611 int i, pending_vec, max_bits;
3612 struct descriptor_table dt;
3616 dt.limit = sregs->idt.limit;
3617 dt.base = sregs->idt.base;
3618 kvm_x86_ops->set_idt(vcpu, &dt);
3619 dt.limit = sregs->gdt.limit;
3620 dt.base = sregs->gdt.base;
3621 kvm_x86_ops->set_gdt(vcpu, &dt);
3623 vcpu->arch.cr2 = sregs->cr2;
3624 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
3625 vcpu->arch.cr3 = sregs->cr3;
3627 kvm_set_cr8(vcpu, sregs->cr8);
3629 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
3630 kvm_x86_ops->set_efer(vcpu, sregs->efer);
3631 kvm_set_apic_base(vcpu, sregs->apic_base);
3633 kvm_x86_ops->decache_cr4_guest_bits(vcpu);
3635 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
3636 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
3637 vcpu->arch.cr0 = sregs->cr0;
3639 mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
3640 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
3641 if (!is_long_mode(vcpu) && is_pae(vcpu))
3642 load_pdptrs(vcpu, vcpu->arch.cr3);
3644 if (mmu_reset_needed)
3645 kvm_mmu_reset_context(vcpu);
3647 if (!irqchip_in_kernel(vcpu->kvm)) {
3648 memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
3649 sizeof vcpu->arch.irq_pending);
3650 vcpu->arch.irq_summary = 0;
3651 for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
3652 if (vcpu->arch.irq_pending[i])
3653 __set_bit(i, &vcpu->arch.irq_summary);
3655 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
3656 pending_vec = find_first_bit(
3657 (const unsigned long *)sregs->interrupt_bitmap,
3659 /* Only pending external irq is handled here */
3660 if (pending_vec < max_bits) {
3661 kvm_x86_ops->set_irq(vcpu, pending_vec);
3662 pr_debug("Set back pending irq %d\n",
3667 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
3668 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
3669 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
3670 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
3671 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
3672 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
3674 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
3675 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
3682 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
3683 struct kvm_debug_guest *dbg)
3689 r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
3697 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3698 * we have asm/x86/processor.h
3709 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3710 #ifdef CONFIG_X86_64
3711 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3713 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3718 * Translate a guest virtual address to a guest physical address.
3720 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3721 struct kvm_translation *tr)
3723 unsigned long vaddr = tr->linear_address;
3727 down_read(&vcpu->kvm->slots_lock);
3728 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
3729 up_read(&vcpu->kvm->slots_lock);
3730 tr->physical_address = gpa;
3731 tr->valid = gpa != UNMAPPED_GVA;
3739 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3741 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3745 memcpy(fpu->fpr, fxsave->st_space, 128);
3746 fpu->fcw = fxsave->cwd;
3747 fpu->fsw = fxsave->swd;
3748 fpu->ftwx = fxsave->twd;
3749 fpu->last_opcode = fxsave->fop;
3750 fpu->last_ip = fxsave->rip;
3751 fpu->last_dp = fxsave->rdp;
3752 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
3759 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3761 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
3765 memcpy(fxsave->st_space, fpu->fpr, 128);
3766 fxsave->cwd = fpu->fcw;
3767 fxsave->swd = fpu->fsw;
3768 fxsave->twd = fpu->ftwx;
3769 fxsave->fop = fpu->last_opcode;
3770 fxsave->rip = fpu->last_ip;
3771 fxsave->rdp = fpu->last_dp;
3772 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
3779 void fx_init(struct kvm_vcpu *vcpu)
3781 unsigned after_mxcsr_mask;
3784 * Touch the fpu the first time in non atomic context as if
3785 * this is the first fpu instruction the exception handler
3786 * will fire before the instruction returns and it'll have to
3787 * allocate ram with GFP_KERNEL.
3790 fx_save(&vcpu->arch.host_fx_image);
3792 /* Initialize guest FPU by resetting ours and saving into guest's */
3794 fx_save(&vcpu->arch.host_fx_image);
3796 fx_save(&vcpu->arch.guest_fx_image);
3797 fx_restore(&vcpu->arch.host_fx_image);
3800 vcpu->arch.cr0 |= X86_CR0_ET;
3801 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
3802 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
3803 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
3804 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
3806 EXPORT_SYMBOL_GPL(fx_init);
3808 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
3810 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
3813 vcpu->guest_fpu_loaded = 1;
3814 fx_save(&vcpu->arch.host_fx_image);
3815 fx_restore(&vcpu->arch.guest_fx_image);
3817 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
3819 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
3821 if (!vcpu->guest_fpu_loaded)
3824 vcpu->guest_fpu_loaded = 0;
3825 fx_save(&vcpu->arch.guest_fx_image);
3826 fx_restore(&vcpu->arch.host_fx_image);
3827 ++vcpu->stat.fpu_reload;
3829 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
3831 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
3833 kvm_x86_ops->vcpu_free(vcpu);
3836 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3839 return kvm_x86_ops->vcpu_create(kvm, id);
3842 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
3846 /* We do fxsave: this must be aligned. */
3847 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
3850 r = kvm_arch_vcpu_reset(vcpu);
3852 r = kvm_mmu_setup(vcpu);
3859 kvm_x86_ops->vcpu_free(vcpu);
3863 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3866 kvm_mmu_unload(vcpu);
3869 kvm_x86_ops->vcpu_free(vcpu);
3872 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
3874 return kvm_x86_ops->vcpu_reset(vcpu);
3877 void kvm_arch_hardware_enable(void *garbage)
3879 kvm_x86_ops->hardware_enable(garbage);
3882 void kvm_arch_hardware_disable(void *garbage)
3884 kvm_x86_ops->hardware_disable(garbage);
3887 int kvm_arch_hardware_setup(void)
3889 return kvm_x86_ops->hardware_setup();
3892 void kvm_arch_hardware_unsetup(void)
3894 kvm_x86_ops->hardware_unsetup();
3897 void kvm_arch_check_processor_compat(void *rtn)
3899 kvm_x86_ops->check_processor_compatibility(rtn);
3902 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
3908 BUG_ON(vcpu->kvm == NULL);
3911 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
3912 if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
3913 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3915 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
3917 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3922 vcpu->arch.pio_data = page_address(page);
3924 r = kvm_mmu_create(vcpu);
3926 goto fail_free_pio_data;
3928 if (irqchip_in_kernel(kvm)) {
3929 r = kvm_create_lapic(vcpu);
3931 goto fail_mmu_destroy;
3937 kvm_mmu_destroy(vcpu);
3939 free_page((unsigned long)vcpu->arch.pio_data);
3944 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
3946 kvm_free_lapic(vcpu);
3947 down_read(&vcpu->kvm->slots_lock);
3948 kvm_mmu_destroy(vcpu);
3949 up_read(&vcpu->kvm->slots_lock);
3950 free_page((unsigned long)vcpu->arch.pio_data);
3953 struct kvm *kvm_arch_create_vm(void)
3955 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
3958 return ERR_PTR(-ENOMEM);
3960 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
3965 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
3968 kvm_mmu_unload(vcpu);
3972 static void kvm_free_vcpus(struct kvm *kvm)
3977 * Unpin any mmu pages first.
3979 for (i = 0; i < KVM_MAX_VCPUS; ++i)
3981 kvm_unload_vcpu_mmu(kvm->vcpus[i]);
3982 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3983 if (kvm->vcpus[i]) {
3984 kvm_arch_vcpu_free(kvm->vcpus[i]);
3985 kvm->vcpus[i] = NULL;
3991 void kvm_arch_destroy_vm(struct kvm *kvm)
3994 kfree(kvm->arch.vpic);
3995 kfree(kvm->arch.vioapic);
3996 kvm_free_vcpus(kvm);
3997 kvm_free_physmem(kvm);
3998 if (kvm->arch.apic_access_page)
3999 put_page(kvm->arch.apic_access_page);
4000 if (kvm->arch.ept_identity_pagetable)
4001 put_page(kvm->arch.ept_identity_pagetable);
4005 int kvm_arch_set_memory_region(struct kvm *kvm,
4006 struct kvm_userspace_memory_region *mem,
4007 struct kvm_memory_slot old,
4010 int npages = mem->memory_size >> PAGE_SHIFT;
4011 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
4013 /*To keep backward compatibility with older userspace,
4014 *x86 needs to hanlde !user_alloc case.
4017 if (npages && !old.rmap) {
4018 down_write(¤t->mm->mmap_sem);
4019 memslot->userspace_addr = do_mmap(NULL, 0,
4021 PROT_READ | PROT_WRITE,
4022 MAP_SHARED | MAP_ANONYMOUS,
4024 up_write(¤t->mm->mmap_sem);
4026 if (IS_ERR((void *)memslot->userspace_addr))
4027 return PTR_ERR((void *)memslot->userspace_addr);
4029 if (!old.user_alloc && old.rmap) {
4032 down_write(¤t->mm->mmap_sem);
4033 ret = do_munmap(current->mm, old.userspace_addr,
4034 old.npages * PAGE_SIZE);
4035 up_write(¤t->mm->mmap_sem);
4038 "kvm_vm_ioctl_set_memory_region: "
4039 "failed to munmap memory\n");
4044 if (!kvm->arch.n_requested_mmu_pages) {
4045 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
4046 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
4049 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
4050 kvm_flush_remote_tlbs(kvm);
4055 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
4057 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
4058 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
4061 static void vcpu_kick_intr(void *info)
4064 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
4065 printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
4069 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
4071 int ipi_pcpu = vcpu->cpu;
4072 int cpu = get_cpu();
4074 if (waitqueue_active(&vcpu->wq)) {
4075 wake_up_interruptible(&vcpu->wq);
4076 ++vcpu->stat.halt_wakeup;
4079 * We may be called synchronously with irqs disabled in guest mode,
4080 * So need not to call smp_call_function_single() in that case.
4082 if (vcpu->guest_mode && vcpu->cpu != cpu)
4083 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);