2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
20 #include <linux/kvm.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/magic.h>
24 #include <asm/processor.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
29 #include <linux/miscdevice.h>
30 #include <linux/vmalloc.h>
31 #include <asm/uaccess.h>
32 #include <linux/reboot.h>
34 #include <linux/debugfs.h>
35 #include <linux/highmem.h>
36 #include <linux/file.h>
38 #include <linux/sysdev.h>
39 #include <linux/cpu.h>
40 #include <linux/file.h>
42 #include <linux/mount.h>
43 #include <linux/sched.h>
44 #include <linux/cpumask.h>
45 #include <linux/smp.h>
47 #include "x86_emulate.h"
48 #include "segment_descriptor.h"
50 MODULE_AUTHOR("Qumranet");
51 MODULE_LICENSE("GPL");
53 static DEFINE_SPINLOCK(kvm_lock);
54 static LIST_HEAD(vm_list);
56 struct kvm_arch_ops *kvm_arch_ops;
58 #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
60 static struct kvm_stats_debugfs_item {
63 struct dentry *dentry;
64 } debugfs_entries[] = {
65 { "pf_fixed", STAT_OFFSET(pf_fixed) },
66 { "pf_guest", STAT_OFFSET(pf_guest) },
67 { "tlb_flush", STAT_OFFSET(tlb_flush) },
68 { "invlpg", STAT_OFFSET(invlpg) },
69 { "exits", STAT_OFFSET(exits) },
70 { "io_exits", STAT_OFFSET(io_exits) },
71 { "mmio_exits", STAT_OFFSET(mmio_exits) },
72 { "signal_exits", STAT_OFFSET(signal_exits) },
73 { "irq_window", STAT_OFFSET(irq_window_exits) },
74 { "halt_exits", STAT_OFFSET(halt_exits) },
75 { "request_irq", STAT_OFFSET(request_irq_exits) },
76 { "irq_exits", STAT_OFFSET(irq_exits) },
77 { "light_exits", STAT_OFFSET(light_exits) },
78 { "efer_reload", STAT_OFFSET(efer_reload) },
82 static struct dentry *debugfs_dir;
84 struct vfsmount *kvmfs_mnt;
86 #define MAX_IO_MSRS 256
88 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
89 #define LMSW_GUEST_MASK 0x0eULL
90 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
91 #define CR8_RESEVED_BITS (~0x0fULL)
92 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
95 // LDT or TSS descriptor in the GDT. 16 bytes.
96 struct segment_descriptor_64 {
97 struct segment_descriptor s;
104 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
107 static struct inode *kvmfs_inode(struct file_operations *fops)
110 struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
118 * Mark the inode dirty from the very beginning,
119 * that way it will never be moved to the dirty
120 * list because mark_inode_dirty() will think
121 * that it already _is_ on the dirty list.
123 inode->i_state = I_DIRTY;
124 inode->i_mode = S_IRUSR | S_IWUSR;
125 inode->i_uid = current->fsuid;
126 inode->i_gid = current->fsgid;
127 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
131 return ERR_PTR(error);
134 static struct file *kvmfs_file(struct inode *inode, void *private_data)
136 struct file *file = get_empty_filp();
139 return ERR_PTR(-ENFILE);
141 file->f_path.mnt = mntget(kvmfs_mnt);
142 file->f_path.dentry = d_alloc_anon(inode);
143 if (!file->f_path.dentry)
144 return ERR_PTR(-ENOMEM);
145 file->f_mapping = inode->i_mapping;
148 file->f_flags = O_RDWR;
149 file->f_op = inode->i_fop;
150 file->f_mode = FMODE_READ | FMODE_WRITE;
152 file->private_data = private_data;
156 unsigned long segment_base(u16 selector)
158 struct descriptor_table gdt;
159 struct segment_descriptor *d;
160 unsigned long table_base;
161 typedef unsigned long ul;
167 asm ("sgdt %0" : "=m"(gdt));
168 table_base = gdt.base;
170 if (selector & 4) { /* from ldt */
173 asm ("sldt %0" : "=g"(ldt_selector));
174 table_base = segment_base(ldt_selector);
176 d = (struct segment_descriptor *)(table_base + (selector & ~7));
177 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
180 && (d->type == 2 || d->type == 9 || d->type == 11))
181 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
185 EXPORT_SYMBOL_GPL(segment_base);
187 static inline int valid_vcpu(int n)
189 return likely(n >= 0 && n < KVM_MAX_VCPUS);
192 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
195 unsigned char *host_buf = dest;
196 unsigned long req_size = size;
204 paddr = gva_to_hpa(vcpu, addr);
206 if (is_error_hpa(paddr))
209 guest_buf = (hva_t)kmap_atomic(
210 pfn_to_page(paddr >> PAGE_SHIFT),
212 offset = addr & ~PAGE_MASK;
214 now = min(size, PAGE_SIZE - offset);
215 memcpy(host_buf, (void*)guest_buf, now);
219 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
221 return req_size - size;
223 EXPORT_SYMBOL_GPL(kvm_read_guest);
225 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
228 unsigned char *host_buf = data;
229 unsigned long req_size = size;
238 paddr = gva_to_hpa(vcpu, addr);
240 if (is_error_hpa(paddr))
243 gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
244 mark_page_dirty(vcpu->kvm, gfn);
245 guest_buf = (hva_t)kmap_atomic(
246 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
247 offset = addr & ~PAGE_MASK;
249 now = min(size, PAGE_SIZE - offset);
250 memcpy((void*)guest_buf, host_buf, now);
254 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
256 return req_size - size;
258 EXPORT_SYMBOL_GPL(kvm_write_guest);
260 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
262 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
265 vcpu->guest_fpu_loaded = 1;
266 fx_save(vcpu->host_fx_image);
267 fx_restore(vcpu->guest_fx_image);
269 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
271 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
273 if (!vcpu->guest_fpu_loaded)
276 vcpu->guest_fpu_loaded = 0;
277 fx_save(vcpu->guest_fx_image);
278 fx_restore(vcpu->host_fx_image);
280 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
283 * Switches to specified vcpu, until a matching vcpu_put()
285 static void vcpu_load(struct kvm_vcpu *vcpu)
287 mutex_lock(&vcpu->mutex);
288 kvm_arch_ops->vcpu_load(vcpu);
292 * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
293 * if the slot is not populated.
295 static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
297 struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
299 mutex_lock(&vcpu->mutex);
301 mutex_unlock(&vcpu->mutex);
304 kvm_arch_ops->vcpu_load(vcpu);
308 static void vcpu_put(struct kvm_vcpu *vcpu)
310 kvm_arch_ops->vcpu_put(vcpu);
311 mutex_unlock(&vcpu->mutex);
314 static void ack_flush(void *_completed)
316 atomic_t *completed = _completed;
318 atomic_inc(completed);
321 void kvm_flush_remote_tlbs(struct kvm *kvm)
325 struct kvm_vcpu *vcpu;
328 atomic_set(&completed, 0);
331 for (i = 0; i < kvm->nvcpus; ++i) {
332 vcpu = &kvm->vcpus[i];
333 if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
336 if (cpu != -1 && cpu != raw_smp_processor_id())
337 if (!cpu_isset(cpu, cpus)) {
344 * We really want smp_call_function_mask() here. But that's not
345 * available, so ipi all cpus in parallel and wait for them
348 for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
349 smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
350 while (atomic_read(&completed) != needed) {
356 static struct kvm *kvm_create_vm(void)
358 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
362 return ERR_PTR(-ENOMEM);
364 spin_lock_init(&kvm->lock);
365 INIT_LIST_HEAD(&kvm->active_mmu_pages);
366 spin_lock(&kvm_lock);
367 list_add(&kvm->vm_list, &vm_list);
368 spin_unlock(&kvm_lock);
369 kvm_io_bus_init(&kvm->mmio_bus);
370 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
371 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
373 mutex_init(&vcpu->mutex);
376 vcpu->mmu.root_hpa = INVALID_PAGE;
381 static int kvm_dev_open(struct inode *inode, struct file *filp)
387 * Free any memory in @free but not in @dont.
389 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
390 struct kvm_memory_slot *dont)
394 if (!dont || free->phys_mem != dont->phys_mem)
395 if (free->phys_mem) {
396 for (i = 0; i < free->npages; ++i)
397 if (free->phys_mem[i])
398 __free_page(free->phys_mem[i]);
399 vfree(free->phys_mem);
402 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
403 vfree(free->dirty_bitmap);
405 free->phys_mem = NULL;
407 free->dirty_bitmap = NULL;
410 static void kvm_free_physmem(struct kvm *kvm)
414 for (i = 0; i < kvm->nmemslots; ++i)
415 kvm_free_physmem_slot(&kvm->memslots[i], NULL);
418 static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
422 for (i = 0; i < 2; ++i)
423 if (vcpu->pio.guest_pages[i]) {
424 __free_page(vcpu->pio.guest_pages[i]);
425 vcpu->pio.guest_pages[i] = NULL;
429 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
435 kvm_mmu_unload(vcpu);
439 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
445 kvm_mmu_destroy(vcpu);
447 kvm_arch_ops->vcpu_free(vcpu);
448 free_page((unsigned long)vcpu->run);
450 free_page((unsigned long)vcpu->pio_data);
451 vcpu->pio_data = NULL;
452 free_pio_guest_pages(vcpu);
455 static void kvm_free_vcpus(struct kvm *kvm)
460 * Unpin any mmu pages first.
462 for (i = 0; i < KVM_MAX_VCPUS; ++i)
463 kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
464 for (i = 0; i < KVM_MAX_VCPUS; ++i)
465 kvm_free_vcpu(&kvm->vcpus[i]);
468 static int kvm_dev_release(struct inode *inode, struct file *filp)
473 static void kvm_destroy_vm(struct kvm *kvm)
475 spin_lock(&kvm_lock);
476 list_del(&kvm->vm_list);
477 spin_unlock(&kvm_lock);
478 kvm_io_bus_destroy(&kvm->mmio_bus);
480 kvm_free_physmem(kvm);
484 static int kvm_vm_release(struct inode *inode, struct file *filp)
486 struct kvm *kvm = filp->private_data;
492 static void inject_gp(struct kvm_vcpu *vcpu)
494 kvm_arch_ops->inject_gp(vcpu, 0);
498 * Load the pae pdptrs. Return true is they are all valid.
500 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
502 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
503 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
510 spin_lock(&vcpu->kvm->lock);
511 page = gfn_to_page(vcpu->kvm, pdpt_gfn);
512 /* FIXME: !page - emulate? 0xff? */
513 pdpt = kmap_atomic(page, KM_USER0);
516 for (i = 0; i < 4; ++i) {
517 pdpte = pdpt[offset + i];
518 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
524 for (i = 0; i < 4; ++i)
525 vcpu->pdptrs[i] = pdpt[offset + i];
528 kunmap_atomic(pdpt, KM_USER0);
529 spin_unlock(&vcpu->kvm->lock);
534 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
536 if (cr0 & CR0_RESEVED_BITS) {
537 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
543 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
544 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
549 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
550 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
551 "and a clear PE flag\n");
556 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
558 if ((vcpu->shadow_efer & EFER_LME)) {
562 printk(KERN_DEBUG "set_cr0: #GP, start paging "
563 "in long mode while PAE is disabled\n");
567 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
569 printk(KERN_DEBUG "set_cr0: #GP, start paging "
570 "in long mode while CS.L == 1\n");
577 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
578 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
586 kvm_arch_ops->set_cr0(vcpu, cr0);
589 spin_lock(&vcpu->kvm->lock);
590 kvm_mmu_reset_context(vcpu);
591 spin_unlock(&vcpu->kvm->lock);
594 EXPORT_SYMBOL_GPL(set_cr0);
596 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
598 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
600 EXPORT_SYMBOL_GPL(lmsw);
602 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
604 if (cr4 & CR4_RESEVED_BITS) {
605 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
610 if (is_long_mode(vcpu)) {
611 if (!(cr4 & CR4_PAE_MASK)) {
612 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
617 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
618 && !load_pdptrs(vcpu, vcpu->cr3)) {
619 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
623 if (cr4 & CR4_VMXE_MASK) {
624 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
628 kvm_arch_ops->set_cr4(vcpu, cr4);
629 spin_lock(&vcpu->kvm->lock);
630 kvm_mmu_reset_context(vcpu);
631 spin_unlock(&vcpu->kvm->lock);
633 EXPORT_SYMBOL_GPL(set_cr4);
635 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
637 if (is_long_mode(vcpu)) {
638 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
639 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
644 if (cr3 & CR3_RESEVED_BITS) {
645 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
649 if (is_paging(vcpu) && is_pae(vcpu) &&
650 !load_pdptrs(vcpu, cr3)) {
651 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
659 spin_lock(&vcpu->kvm->lock);
661 * Does the new cr3 value map to physical memory? (Note, we
662 * catch an invalid cr3 even in real-mode, because it would
663 * cause trouble later on when we turn on paging anyway.)
665 * A real CPU would silently accept an invalid cr3 and would
666 * attempt to use it - with largely undefined (and often hard
667 * to debug) behavior on the guest side.
669 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
672 vcpu->mmu.new_cr3(vcpu);
673 spin_unlock(&vcpu->kvm->lock);
675 EXPORT_SYMBOL_GPL(set_cr3);
677 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
679 if ( cr8 & CR8_RESEVED_BITS) {
680 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
686 EXPORT_SYMBOL_GPL(set_cr8);
688 void fx_init(struct kvm_vcpu *vcpu)
690 struct __attribute__ ((__packed__)) fx_image_s {
696 u64 operand;// fpu dp
702 fx_save(vcpu->host_fx_image);
704 fx_save(vcpu->guest_fx_image);
705 fx_restore(vcpu->host_fx_image);
707 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
708 fx_image->mxcsr = 0x1f80;
709 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
710 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
712 EXPORT_SYMBOL_GPL(fx_init);
714 static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
716 spin_lock(&vcpu->kvm->lock);
717 kvm_mmu_slot_remove_write_access(vcpu, slot);
718 spin_unlock(&vcpu->kvm->lock);
722 * Allocate some memory and give it an address in the guest physical address
725 * Discontiguous memory is allowed, mostly for framebuffers.
727 static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
728 struct kvm_memory_region *mem)
732 unsigned long npages;
734 struct kvm_memory_slot *memslot;
735 struct kvm_memory_slot old, new;
736 int memory_config_version;
739 /* General sanity checks */
740 if (mem->memory_size & (PAGE_SIZE - 1))
742 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
744 if (mem->slot >= KVM_MEMORY_SLOTS)
746 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
749 memslot = &kvm->memslots[mem->slot];
750 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
751 npages = mem->memory_size >> PAGE_SHIFT;
754 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
757 spin_lock(&kvm->lock);
759 memory_config_version = kvm->memory_config_version;
760 new = old = *memslot;
762 new.base_gfn = base_gfn;
764 new.flags = mem->flags;
766 /* Disallow changing a memory slot's size. */
768 if (npages && old.npages && npages != old.npages)
771 /* Check for overlaps */
773 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
774 struct kvm_memory_slot *s = &kvm->memslots[i];
778 if (!((base_gfn + npages <= s->base_gfn) ||
779 (base_gfn >= s->base_gfn + s->npages)))
783 * Do memory allocations outside lock. memory_config_version will
786 spin_unlock(&kvm->lock);
788 /* Deallocate if slot is being removed */
792 /* Free page dirty bitmap if unneeded */
793 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
794 new.dirty_bitmap = NULL;
798 /* Allocate if a slot is being created */
799 if (npages && !new.phys_mem) {
800 new.phys_mem = vmalloc(npages * sizeof(struct page *));
805 memset(new.phys_mem, 0, npages * sizeof(struct page *));
806 for (i = 0; i < npages; ++i) {
807 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
809 if (!new.phys_mem[i])
811 set_page_private(new.phys_mem[i],0);
815 /* Allocate page dirty bitmap if needed */
816 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
817 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
819 new.dirty_bitmap = vmalloc(dirty_bytes);
820 if (!new.dirty_bitmap)
822 memset(new.dirty_bitmap, 0, dirty_bytes);
825 spin_lock(&kvm->lock);
827 if (memory_config_version != kvm->memory_config_version) {
828 spin_unlock(&kvm->lock);
829 kvm_free_physmem_slot(&new, &old);
837 if (mem->slot >= kvm->nmemslots)
838 kvm->nmemslots = mem->slot + 1;
841 ++kvm->memory_config_version;
843 spin_unlock(&kvm->lock);
845 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
846 struct kvm_vcpu *vcpu;
848 vcpu = vcpu_load_slot(kvm, i);
851 if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
852 do_remove_write_access(vcpu, mem->slot);
853 kvm_mmu_reset_context(vcpu);
857 kvm_free_physmem_slot(&old, &new);
861 spin_unlock(&kvm->lock);
863 kvm_free_physmem_slot(&new, &old);
869 * Get (and clear) the dirty memory log for a memory slot.
871 static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
872 struct kvm_dirty_log *log)
874 struct kvm_memory_slot *memslot;
878 unsigned long any = 0;
880 spin_lock(&kvm->lock);
883 * Prevent changes to guest memory configuration even while the lock
887 spin_unlock(&kvm->lock);
889 if (log->slot >= KVM_MEMORY_SLOTS)
892 memslot = &kvm->memslots[log->slot];
894 if (!memslot->dirty_bitmap)
897 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
899 for (i = 0; !any && i < n/sizeof(long); ++i)
900 any = memslot->dirty_bitmap[i];
903 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
908 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
909 struct kvm_vcpu *vcpu;
911 vcpu = vcpu_load_slot(kvm, i);
915 do_remove_write_access(vcpu, log->slot);
916 memset(memslot->dirty_bitmap, 0, n);
919 kvm_arch_ops->tlb_flush(vcpu);
927 spin_lock(&kvm->lock);
929 spin_unlock(&kvm->lock);
934 * Set a new alias region. Aliases map a portion of physical memory into
935 * another portion. This is useful for memory windows, for example the PC
938 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
939 struct kvm_memory_alias *alias)
942 struct kvm_mem_alias *p;
945 /* General sanity checks */
946 if (alias->memory_size & (PAGE_SIZE - 1))
948 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
950 if (alias->slot >= KVM_ALIAS_SLOTS)
952 if (alias->guest_phys_addr + alias->memory_size
953 < alias->guest_phys_addr)
955 if (alias->target_phys_addr + alias->memory_size
956 < alias->target_phys_addr)
959 spin_lock(&kvm->lock);
961 p = &kvm->aliases[alias->slot];
962 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
963 p->npages = alias->memory_size >> PAGE_SHIFT;
964 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
966 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
967 if (kvm->aliases[n - 1].npages)
971 spin_unlock(&kvm->lock);
973 vcpu_load(&kvm->vcpus[0]);
974 spin_lock(&kvm->lock);
975 kvm_mmu_zap_all(&kvm->vcpus[0]);
976 spin_unlock(&kvm->lock);
977 vcpu_put(&kvm->vcpus[0]);
985 static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
988 struct kvm_mem_alias *alias;
990 for (i = 0; i < kvm->naliases; ++i) {
991 alias = &kvm->aliases[i];
992 if (gfn >= alias->base_gfn
993 && gfn < alias->base_gfn + alias->npages)
994 return alias->target_gfn + gfn - alias->base_gfn;
999 static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1003 for (i = 0; i < kvm->nmemslots; ++i) {
1004 struct kvm_memory_slot *memslot = &kvm->memslots[i];
1006 if (gfn >= memslot->base_gfn
1007 && gfn < memslot->base_gfn + memslot->npages)
1013 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1015 gfn = unalias_gfn(kvm, gfn);
1016 return __gfn_to_memslot(kvm, gfn);
1019 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
1021 struct kvm_memory_slot *slot;
1023 gfn = unalias_gfn(kvm, gfn);
1024 slot = __gfn_to_memslot(kvm, gfn);
1027 return slot->phys_mem[gfn - slot->base_gfn];
1029 EXPORT_SYMBOL_GPL(gfn_to_page);
1031 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
1034 struct kvm_memory_slot *memslot;
1035 unsigned long rel_gfn;
1037 for (i = 0; i < kvm->nmemslots; ++i) {
1038 memslot = &kvm->memslots[i];
1040 if (gfn >= memslot->base_gfn
1041 && gfn < memslot->base_gfn + memslot->npages) {
1043 if (!memslot->dirty_bitmap)
1046 rel_gfn = gfn - memslot->base_gfn;
1049 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
1050 set_bit(rel_gfn, memslot->dirty_bitmap);
1056 static int emulator_read_std(unsigned long addr,
1059 struct x86_emulate_ctxt *ctxt)
1061 struct kvm_vcpu *vcpu = ctxt->vcpu;
1065 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1066 unsigned offset = addr & (PAGE_SIZE-1);
1067 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
1072 if (gpa == UNMAPPED_GVA)
1073 return X86EMUL_PROPAGATE_FAULT;
1074 pfn = gpa >> PAGE_SHIFT;
1075 page = gfn_to_page(vcpu->kvm, pfn);
1077 return X86EMUL_UNHANDLEABLE;
1078 page_virt = kmap_atomic(page, KM_USER0);
1080 memcpy(data, page_virt + offset, tocopy);
1082 kunmap_atomic(page_virt, KM_USER0);
1089 return X86EMUL_CONTINUE;
1092 static int emulator_write_std(unsigned long addr,
1095 struct x86_emulate_ctxt *ctxt)
1097 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
1099 return X86EMUL_UNHANDLEABLE;
1102 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
1106 * Note that its important to have this wrapper function because
1107 * in the very near future we will be checking for MMIOs against
1108 * the LAPIC as well as the general MMIO bus
1110 return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
1113 static int emulator_read_emulated(unsigned long addr,
1116 struct x86_emulate_ctxt *ctxt)
1118 struct kvm_vcpu *vcpu = ctxt->vcpu;
1119 struct kvm_io_device *mmio_dev;
1122 if (vcpu->mmio_read_completed) {
1123 memcpy(val, vcpu->mmio_data, bytes);
1124 vcpu->mmio_read_completed = 0;
1125 return X86EMUL_CONTINUE;
1126 } else if (emulator_read_std(addr, val, bytes, ctxt)
1127 == X86EMUL_CONTINUE)
1128 return X86EMUL_CONTINUE;
1130 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1131 if (gpa == UNMAPPED_GVA)
1132 return X86EMUL_PROPAGATE_FAULT;
1135 * Is this MMIO handled locally?
1137 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1139 kvm_iodevice_read(mmio_dev, gpa, bytes, val);
1140 return X86EMUL_CONTINUE;
1143 vcpu->mmio_needed = 1;
1144 vcpu->mmio_phys_addr = gpa;
1145 vcpu->mmio_size = bytes;
1146 vcpu->mmio_is_write = 0;
1148 return X86EMUL_UNHANDLEABLE;
1151 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
1152 const void *val, int bytes)
1156 unsigned offset = offset_in_page(gpa);
1158 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
1160 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1163 mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
1164 virt = kmap_atomic(page, KM_USER0);
1165 kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
1166 memcpy(virt + offset_in_page(gpa), val, bytes);
1167 kunmap_atomic(virt, KM_USER0);
1171 static int emulator_write_emulated(unsigned long addr,
1174 struct x86_emulate_ctxt *ctxt)
1176 struct kvm_vcpu *vcpu = ctxt->vcpu;
1177 struct kvm_io_device *mmio_dev;
1178 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
1180 if (gpa == UNMAPPED_GVA) {
1181 kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
1182 return X86EMUL_PROPAGATE_FAULT;
1185 if (emulator_write_phys(vcpu, gpa, val, bytes))
1186 return X86EMUL_CONTINUE;
1189 * Is this MMIO handled locally?
1191 mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
1193 kvm_iodevice_write(mmio_dev, gpa, bytes, val);
1194 return X86EMUL_CONTINUE;
1197 vcpu->mmio_needed = 1;
1198 vcpu->mmio_phys_addr = gpa;
1199 vcpu->mmio_size = bytes;
1200 vcpu->mmio_is_write = 1;
1201 memcpy(vcpu->mmio_data, val, bytes);
1203 return X86EMUL_CONTINUE;
1206 static int emulator_cmpxchg_emulated(unsigned long addr,
1210 struct x86_emulate_ctxt *ctxt)
1212 static int reported;
1216 printk(KERN_WARNING "kvm: emulating exchange as write\n");
1218 return emulator_write_emulated(addr, new, bytes, ctxt);
1221 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
1223 return kvm_arch_ops->get_segment_base(vcpu, seg);
1226 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1228 return X86EMUL_CONTINUE;
1231 int emulate_clts(struct kvm_vcpu *vcpu)
1235 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1236 kvm_arch_ops->set_cr0(vcpu, cr0);
1237 return X86EMUL_CONTINUE;
1240 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1242 struct kvm_vcpu *vcpu = ctxt->vcpu;
1246 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1247 return X86EMUL_CONTINUE;
1249 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1251 return X86EMUL_UNHANDLEABLE;
1255 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1257 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1260 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1262 /* FIXME: better handling */
1263 return X86EMUL_UNHANDLEABLE;
1265 return X86EMUL_CONTINUE;
1268 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1270 static int reported;
1272 unsigned long rip = ctxt->vcpu->rip;
1273 unsigned long rip_linear;
1275 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1280 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1282 printk(KERN_ERR "emulation failed but !mmio_needed?"
1283 " rip %lx %02x %02x %02x %02x\n",
1284 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1288 struct x86_emulate_ops emulate_ops = {
1289 .read_std = emulator_read_std,
1290 .write_std = emulator_write_std,
1291 .read_emulated = emulator_read_emulated,
1292 .write_emulated = emulator_write_emulated,
1293 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1296 int emulate_instruction(struct kvm_vcpu *vcpu,
1297 struct kvm_run *run,
1301 struct x86_emulate_ctxt emulate_ctxt;
1305 vcpu->mmio_fault_cr2 = cr2;
1306 kvm_arch_ops->cache_regs(vcpu);
1308 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1310 emulate_ctxt.vcpu = vcpu;
1311 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1312 emulate_ctxt.cr2 = cr2;
1313 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1314 ? X86EMUL_MODE_REAL : cs_l
1315 ? X86EMUL_MODE_PROT64 : cs_db
1316 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1318 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1319 emulate_ctxt.cs_base = 0;
1320 emulate_ctxt.ds_base = 0;
1321 emulate_ctxt.es_base = 0;
1322 emulate_ctxt.ss_base = 0;
1324 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1325 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1326 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1327 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1330 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1331 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1333 vcpu->mmio_is_write = 0;
1334 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1336 if ((r || vcpu->mmio_is_write) && run) {
1337 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1338 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1339 run->mmio.len = vcpu->mmio_size;
1340 run->mmio.is_write = vcpu->mmio_is_write;
1344 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1345 return EMULATE_DONE;
1346 if (!vcpu->mmio_needed) {
1347 report_emulation_failure(&emulate_ctxt);
1348 return EMULATE_FAIL;
1350 return EMULATE_DO_MMIO;
1353 kvm_arch_ops->decache_regs(vcpu);
1354 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1356 if (vcpu->mmio_is_write) {
1357 vcpu->mmio_needed = 0;
1358 return EMULATE_DO_MMIO;
1361 return EMULATE_DONE;
1363 EXPORT_SYMBOL_GPL(emulate_instruction);
1365 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
1367 if (vcpu->irq_summary)
1370 vcpu->run->exit_reason = KVM_EXIT_HLT;
1371 ++vcpu->stat.halt_exits;
1374 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
1376 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1378 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1380 kvm_arch_ops->cache_regs(vcpu);
1382 #ifdef CONFIG_X86_64
1383 if (is_long_mode(vcpu)) {
1384 nr = vcpu->regs[VCPU_REGS_RAX];
1385 a0 = vcpu->regs[VCPU_REGS_RDI];
1386 a1 = vcpu->regs[VCPU_REGS_RSI];
1387 a2 = vcpu->regs[VCPU_REGS_RDX];
1388 a3 = vcpu->regs[VCPU_REGS_RCX];
1389 a4 = vcpu->regs[VCPU_REGS_R8];
1390 a5 = vcpu->regs[VCPU_REGS_R9];
1394 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1395 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1396 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1397 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1398 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1399 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1400 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1404 run->hypercall.args[0] = a0;
1405 run->hypercall.args[1] = a1;
1406 run->hypercall.args[2] = a2;
1407 run->hypercall.args[3] = a3;
1408 run->hypercall.args[4] = a4;
1409 run->hypercall.args[5] = a5;
1410 run->hypercall.ret = ret;
1411 run->hypercall.longmode = is_long_mode(vcpu);
1412 kvm_arch_ops->decache_regs(vcpu);
1415 vcpu->regs[VCPU_REGS_RAX] = ret;
1416 kvm_arch_ops->decache_regs(vcpu);
1419 EXPORT_SYMBOL_GPL(kvm_hypercall);
1421 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1423 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1426 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1428 struct descriptor_table dt = { limit, base };
1430 kvm_arch_ops->set_gdt(vcpu, &dt);
1433 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1435 struct descriptor_table dt = { limit, base };
1437 kvm_arch_ops->set_idt(vcpu, &dt);
1440 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1441 unsigned long *rflags)
1444 *rflags = kvm_arch_ops->get_rflags(vcpu);
1447 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1449 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
1460 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1465 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1466 unsigned long *rflags)
1470 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1471 *rflags = kvm_arch_ops->get_rflags(vcpu);
1480 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1483 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1488 * Register the para guest with the host:
1490 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1492 struct kvm_vcpu_para_state *para_state;
1493 hpa_t para_state_hpa, hypercall_hpa;
1494 struct page *para_state_page;
1495 unsigned char *hypercall;
1496 gpa_t hypercall_gpa;
1498 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1499 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1502 * Needs to be page aligned:
1504 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1507 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1508 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1509 if (is_error_hpa(para_state_hpa))
1512 mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
1513 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1514 para_state = kmap_atomic(para_state_page, KM_USER0);
1516 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1517 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1519 para_state->host_version = KVM_PARA_API_VERSION;
1521 * We cannot support guests that try to register themselves
1522 * with a newer API version than the host supports:
1524 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1525 para_state->ret = -KVM_EINVAL;
1526 goto err_kunmap_skip;
1529 hypercall_gpa = para_state->hypercall_gpa;
1530 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1531 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1532 if (is_error_hpa(hypercall_hpa)) {
1533 para_state->ret = -KVM_EINVAL;
1534 goto err_kunmap_skip;
1537 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1538 vcpu->para_state_page = para_state_page;
1539 vcpu->para_state_gpa = para_state_gpa;
1540 vcpu->hypercall_gpa = hypercall_gpa;
1542 mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
1543 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1544 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1545 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1546 kunmap_atomic(hypercall, KM_USER1);
1548 para_state->ret = 0;
1550 kunmap_atomic(para_state, KM_USER0);
1556 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1561 case 0xc0010010: /* SYSCFG */
1562 case 0xc0010015: /* HWCR */
1563 case MSR_IA32_PLATFORM_ID:
1564 case MSR_IA32_P5_MC_ADDR:
1565 case MSR_IA32_P5_MC_TYPE:
1566 case MSR_IA32_MC0_CTL:
1567 case MSR_IA32_MCG_STATUS:
1568 case MSR_IA32_MCG_CAP:
1569 case MSR_IA32_MC0_MISC:
1570 case MSR_IA32_MC0_MISC+4:
1571 case MSR_IA32_MC0_MISC+8:
1572 case MSR_IA32_MC0_MISC+12:
1573 case MSR_IA32_MC0_MISC+16:
1574 case MSR_IA32_UCODE_REV:
1575 case MSR_IA32_PERF_STATUS:
1576 case MSR_IA32_EBL_CR_POWERON:
1577 /* MTRR registers */
1579 case 0x200 ... 0x2ff:
1582 case 0xcd: /* fsb frequency */
1585 case MSR_IA32_APICBASE:
1586 data = vcpu->apic_base;
1588 case MSR_IA32_MISC_ENABLE:
1589 data = vcpu->ia32_misc_enable_msr;
1591 #ifdef CONFIG_X86_64
1593 data = vcpu->shadow_efer;
1597 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1603 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1606 * Reads an msr value (of 'msr_index') into 'pdata'.
1607 * Returns 0 on success, non-0 otherwise.
1608 * Assumes vcpu_load() was already called.
1610 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1612 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1615 #ifdef CONFIG_X86_64
1617 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1619 if (efer & EFER_RESERVED_BITS) {
1620 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1627 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1628 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1633 kvm_arch_ops->set_efer(vcpu, efer);
1636 efer |= vcpu->shadow_efer & EFER_LMA;
1638 vcpu->shadow_efer = efer;
1643 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1646 #ifdef CONFIG_X86_64
1648 set_efer(vcpu, data);
1651 case MSR_IA32_MC0_STATUS:
1652 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1653 __FUNCTION__, data);
1655 case MSR_IA32_MCG_STATUS:
1656 printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
1657 __FUNCTION__, data);
1659 case MSR_IA32_UCODE_REV:
1660 case MSR_IA32_UCODE_WRITE:
1661 case 0x200 ... 0x2ff: /* MTRRs */
1663 case MSR_IA32_APICBASE:
1664 vcpu->apic_base = data;
1666 case MSR_IA32_MISC_ENABLE:
1667 vcpu->ia32_misc_enable_msr = data;
1670 * This is the 'probe whether the host is KVM' logic:
1672 case MSR_KVM_API_MAGIC:
1673 return vcpu_register_para(vcpu, data);
1676 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1681 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1684 * Writes msr value into into the appropriate "register".
1685 * Returns 0 on success, non-0 otherwise.
1686 * Assumes vcpu_load() was already called.
1688 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1690 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1693 void kvm_resched(struct kvm_vcpu *vcpu)
1695 if (!need_resched())
1701 EXPORT_SYMBOL_GPL(kvm_resched);
1703 void load_msrs(struct vmx_msr_entry *e, int n)
1707 for (i = 0; i < n; ++i)
1708 wrmsrl(e[i].index, e[i].data);
1710 EXPORT_SYMBOL_GPL(load_msrs);
1712 void save_msrs(struct vmx_msr_entry *e, int n)
1716 for (i = 0; i < n; ++i)
1717 rdmsrl(e[i].index, e[i].data);
1719 EXPORT_SYMBOL_GPL(save_msrs);
1721 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1725 struct kvm_cpuid_entry *e, *best;
1727 kvm_arch_ops->cache_regs(vcpu);
1728 function = vcpu->regs[VCPU_REGS_RAX];
1729 vcpu->regs[VCPU_REGS_RAX] = 0;
1730 vcpu->regs[VCPU_REGS_RBX] = 0;
1731 vcpu->regs[VCPU_REGS_RCX] = 0;
1732 vcpu->regs[VCPU_REGS_RDX] = 0;
1734 for (i = 0; i < vcpu->cpuid_nent; ++i) {
1735 e = &vcpu->cpuid_entries[i];
1736 if (e->function == function) {
1741 * Both basic or both extended?
1743 if (((e->function ^ function) & 0x80000000) == 0)
1744 if (!best || e->function > best->function)
1748 vcpu->regs[VCPU_REGS_RAX] = best->eax;
1749 vcpu->regs[VCPU_REGS_RBX] = best->ebx;
1750 vcpu->regs[VCPU_REGS_RCX] = best->ecx;
1751 vcpu->regs[VCPU_REGS_RDX] = best->edx;
1753 kvm_arch_ops->decache_regs(vcpu);
1754 kvm_arch_ops->skip_emulated_instruction(vcpu);
1756 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1758 static int pio_copy_data(struct kvm_vcpu *vcpu)
1760 void *p = vcpu->pio_data;
1763 int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
1765 kvm_arch_ops->vcpu_put(vcpu);
1766 q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
1769 kvm_arch_ops->vcpu_load(vcpu);
1770 free_pio_guest_pages(vcpu);
1773 q += vcpu->pio.guest_page_offset;
1774 bytes = vcpu->pio.size * vcpu->pio.cur_count;
1776 memcpy(q, p, bytes);
1778 memcpy(p, q, bytes);
1779 q -= vcpu->pio.guest_page_offset;
1781 kvm_arch_ops->vcpu_load(vcpu);
1782 free_pio_guest_pages(vcpu);
1786 static int complete_pio(struct kvm_vcpu *vcpu)
1788 struct kvm_pio_request *io = &vcpu->pio;
1792 kvm_arch_ops->cache_regs(vcpu);
1796 memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
1800 r = pio_copy_data(vcpu);
1802 kvm_arch_ops->cache_regs(vcpu);
1809 delta *= io->cur_count;
1811 * The size of the register should really depend on
1812 * current address size.
1814 vcpu->regs[VCPU_REGS_RCX] -= delta;
1820 vcpu->regs[VCPU_REGS_RDI] += delta;
1822 vcpu->regs[VCPU_REGS_RSI] += delta;
1825 kvm_arch_ops->decache_regs(vcpu);
1827 io->count -= io->cur_count;
1831 kvm_arch_ops->skip_emulated_instruction(vcpu);
1835 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
1836 int size, unsigned long count, int string, int down,
1837 gva_t address, int rep, unsigned port)
1839 unsigned now, in_page;
1844 vcpu->run->exit_reason = KVM_EXIT_IO;
1845 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
1846 vcpu->run->io.size = size;
1847 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
1848 vcpu->run->io.count = count;
1849 vcpu->run->io.port = port;
1850 vcpu->pio.count = count;
1851 vcpu->pio.cur_count = count;
1852 vcpu->pio.size = size;
1854 vcpu->pio.string = string;
1855 vcpu->pio.down = down;
1856 vcpu->pio.guest_page_offset = offset_in_page(address);
1857 vcpu->pio.rep = rep;
1860 kvm_arch_ops->cache_regs(vcpu);
1861 memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
1862 kvm_arch_ops->decache_regs(vcpu);
1867 kvm_arch_ops->skip_emulated_instruction(vcpu);
1871 now = min(count, PAGE_SIZE / size);
1874 in_page = PAGE_SIZE - offset_in_page(address);
1876 in_page = offset_in_page(address) + size;
1877 now = min(count, (unsigned long)in_page / size);
1880 * String I/O straddles page boundary. Pin two guest pages
1881 * so that we satisfy atomicity constraints. Do just one
1882 * transaction to avoid complexity.
1889 * String I/O in reverse. Yuck. Kill the guest, fix later.
1891 printk(KERN_ERR "kvm: guest string pio down\n");
1895 vcpu->run->io.count = now;
1896 vcpu->pio.cur_count = now;
1898 for (i = 0; i < nr_pages; ++i) {
1899 spin_lock(&vcpu->kvm->lock);
1900 page = gva_to_page(vcpu, address + i * PAGE_SIZE);
1903 vcpu->pio.guest_pages[i] = page;
1904 spin_unlock(&vcpu->kvm->lock);
1907 free_pio_guest_pages(vcpu);
1913 return pio_copy_data(vcpu);
1916 EXPORT_SYMBOL_GPL(kvm_setup_pio);
1918 static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1925 if (vcpu->sigset_active)
1926 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1928 /* re-sync apic's tpr */
1929 vcpu->cr8 = kvm_run->cr8;
1931 if (vcpu->pio.cur_count) {
1932 r = complete_pio(vcpu);
1937 if (vcpu->mmio_needed) {
1938 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1939 vcpu->mmio_read_completed = 1;
1940 vcpu->mmio_needed = 0;
1941 r = emulate_instruction(vcpu, kvm_run,
1942 vcpu->mmio_fault_cr2, 0);
1943 if (r == EMULATE_DO_MMIO) {
1945 * Read-modify-write. Back to userspace.
1947 kvm_run->exit_reason = KVM_EXIT_MMIO;
1953 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
1954 kvm_arch_ops->cache_regs(vcpu);
1955 vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
1956 kvm_arch_ops->decache_regs(vcpu);
1959 r = kvm_arch_ops->run(vcpu, kvm_run);
1962 if (vcpu->sigset_active)
1963 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1969 static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
1970 struct kvm_regs *regs)
1974 kvm_arch_ops->cache_regs(vcpu);
1976 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1977 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1978 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1979 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1980 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1981 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1982 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1983 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1984 #ifdef CONFIG_X86_64
1985 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1986 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1987 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1988 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1989 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1990 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1991 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1992 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1995 regs->rip = vcpu->rip;
1996 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1999 * Don't leak debug flags in case they were set for guest debugging
2001 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
2002 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
2009 static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
2010 struct kvm_regs *regs)
2014 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
2015 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
2016 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
2017 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
2018 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
2019 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
2020 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
2021 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
2022 #ifdef CONFIG_X86_64
2023 vcpu->regs[VCPU_REGS_R8] = regs->r8;
2024 vcpu->regs[VCPU_REGS_R9] = regs->r9;
2025 vcpu->regs[VCPU_REGS_R10] = regs->r10;
2026 vcpu->regs[VCPU_REGS_R11] = regs->r11;
2027 vcpu->regs[VCPU_REGS_R12] = regs->r12;
2028 vcpu->regs[VCPU_REGS_R13] = regs->r13;
2029 vcpu->regs[VCPU_REGS_R14] = regs->r14;
2030 vcpu->regs[VCPU_REGS_R15] = regs->r15;
2033 vcpu->rip = regs->rip;
2034 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
2036 kvm_arch_ops->decache_regs(vcpu);
2043 static void get_segment(struct kvm_vcpu *vcpu,
2044 struct kvm_segment *var, int seg)
2046 return kvm_arch_ops->get_segment(vcpu, var, seg);
2049 static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2050 struct kvm_sregs *sregs)
2052 struct descriptor_table dt;
2056 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2057 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2058 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2059 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2060 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2061 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2063 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2064 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2066 kvm_arch_ops->get_idt(vcpu, &dt);
2067 sregs->idt.limit = dt.limit;
2068 sregs->idt.base = dt.base;
2069 kvm_arch_ops->get_gdt(vcpu, &dt);
2070 sregs->gdt.limit = dt.limit;
2071 sregs->gdt.base = dt.base;
2073 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2074 sregs->cr0 = vcpu->cr0;
2075 sregs->cr2 = vcpu->cr2;
2076 sregs->cr3 = vcpu->cr3;
2077 sregs->cr4 = vcpu->cr4;
2078 sregs->cr8 = vcpu->cr8;
2079 sregs->efer = vcpu->shadow_efer;
2080 sregs->apic_base = vcpu->apic_base;
2082 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
2083 sizeof sregs->interrupt_bitmap);
2090 static void set_segment(struct kvm_vcpu *vcpu,
2091 struct kvm_segment *var, int seg)
2093 return kvm_arch_ops->set_segment(vcpu, var, seg);
2096 static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2097 struct kvm_sregs *sregs)
2099 int mmu_reset_needed = 0;
2101 struct descriptor_table dt;
2105 dt.limit = sregs->idt.limit;
2106 dt.base = sregs->idt.base;
2107 kvm_arch_ops->set_idt(vcpu, &dt);
2108 dt.limit = sregs->gdt.limit;
2109 dt.base = sregs->gdt.base;
2110 kvm_arch_ops->set_gdt(vcpu, &dt);
2112 vcpu->cr2 = sregs->cr2;
2113 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
2114 vcpu->cr3 = sregs->cr3;
2116 vcpu->cr8 = sregs->cr8;
2118 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
2119 #ifdef CONFIG_X86_64
2120 kvm_arch_ops->set_efer(vcpu, sregs->efer);
2122 vcpu->apic_base = sregs->apic_base;
2124 kvm_arch_ops->decache_cr4_guest_bits(vcpu);
2126 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
2127 kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
2129 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
2130 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
2131 if (!is_long_mode(vcpu) && is_pae(vcpu))
2132 load_pdptrs(vcpu, vcpu->cr3);
2134 if (mmu_reset_needed)
2135 kvm_mmu_reset_context(vcpu);
2137 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
2138 sizeof vcpu->irq_pending);
2139 vcpu->irq_summary = 0;
2140 for (i = 0; i < NR_IRQ_WORDS; ++i)
2141 if (vcpu->irq_pending[i])
2142 __set_bit(i, &vcpu->irq_summary);
2144 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
2145 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
2146 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
2147 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
2148 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
2149 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
2151 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
2152 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
2160 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
2161 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
2163 * This list is modified at module load time to reflect the
2164 * capabilities of the host cpu.
2166 static u32 msrs_to_save[] = {
2167 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
2169 #ifdef CONFIG_X86_64
2170 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
2172 MSR_IA32_TIME_STAMP_COUNTER,
2175 static unsigned num_msrs_to_save;
2177 static u32 emulated_msrs[] = {
2178 MSR_IA32_MISC_ENABLE,
2181 static __init void kvm_init_msr_list(void)
2186 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
2187 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2190 msrs_to_save[j] = msrs_to_save[i];
2193 num_msrs_to_save = j;
2197 * Adapt set_msr() to msr_io()'s calling convention
2199 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
2201 return set_msr(vcpu, index, *data);
2205 * Read or write a bunch of msrs. All parameters are kernel addresses.
2207 * @return number of msrs set successfully.
2209 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
2210 struct kvm_msr_entry *entries,
2211 int (*do_msr)(struct kvm_vcpu *vcpu,
2212 unsigned index, u64 *data))
2218 for (i = 0; i < msrs->nmsrs; ++i)
2219 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2228 * Read or write a bunch of msrs. Parameters are user addresses.
2230 * @return number of msrs set successfully.
2232 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
2233 int (*do_msr)(struct kvm_vcpu *vcpu,
2234 unsigned index, u64 *data),
2237 struct kvm_msrs msrs;
2238 struct kvm_msr_entry *entries;
2243 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
2247 if (msrs.nmsrs >= MAX_IO_MSRS)
2251 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
2252 entries = vmalloc(size);
2257 if (copy_from_user(entries, user_msrs->entries, size))
2260 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2265 if (writeback && copy_to_user(user_msrs->entries, entries, size))
2277 * Translate a guest virtual address to a guest physical address.
2279 static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2280 struct kvm_translation *tr)
2282 unsigned long vaddr = tr->linear_address;
2286 spin_lock(&vcpu->kvm->lock);
2287 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
2288 tr->physical_address = gpa;
2289 tr->valid = gpa != UNMAPPED_GVA;
2292 spin_unlock(&vcpu->kvm->lock);
2298 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
2299 struct kvm_interrupt *irq)
2301 if (irq->irq < 0 || irq->irq >= 256)
2305 set_bit(irq->irq, vcpu->irq_pending);
2306 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
2313 static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
2314 struct kvm_debug_guest *dbg)
2320 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
2327 static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
2328 unsigned long address,
2331 struct kvm_vcpu *vcpu = vma->vm_file->private_data;
2332 unsigned long pgoff;
2335 *type = VM_FAULT_MINOR;
2336 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2338 page = virt_to_page(vcpu->run);
2339 else if (pgoff == KVM_PIO_PAGE_OFFSET)
2340 page = virt_to_page(vcpu->pio_data);
2342 return NOPAGE_SIGBUS;
2347 static struct vm_operations_struct kvm_vcpu_vm_ops = {
2348 .nopage = kvm_vcpu_nopage,
2351 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2353 vma->vm_ops = &kvm_vcpu_vm_ops;
2357 static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2359 struct kvm_vcpu *vcpu = filp->private_data;
2361 fput(vcpu->kvm->filp);
2365 static struct file_operations kvm_vcpu_fops = {
2366 .release = kvm_vcpu_release,
2367 .unlocked_ioctl = kvm_vcpu_ioctl,
2368 .compat_ioctl = kvm_vcpu_ioctl,
2369 .mmap = kvm_vcpu_mmap,
2373 * Allocates an inode for the vcpu.
2375 static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2378 struct inode *inode;
2381 atomic_inc(&vcpu->kvm->filp->f_count);
2382 inode = kvmfs_inode(&kvm_vcpu_fops);
2383 if (IS_ERR(inode)) {
2388 file = kvmfs_file(inode, vcpu);
2394 r = get_unused_fd();
2398 fd_install(fd, file);
2407 fput(vcpu->kvm->filp);
2412 * Creates some virtual cpus. Good luck creating more than one.
2414 static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
2417 struct kvm_vcpu *vcpu;
2424 vcpu = &kvm->vcpus[n];
2426 mutex_lock(&vcpu->mutex);
2429 mutex_unlock(&vcpu->mutex);
2433 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2437 vcpu->run = page_address(page);
2439 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2443 vcpu->pio_data = page_address(page);
2445 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
2447 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
2450 r = kvm_arch_ops->vcpu_create(vcpu);
2452 goto out_free_vcpus;
2454 r = kvm_mmu_create(vcpu);
2456 goto out_free_vcpus;
2458 kvm_arch_ops->vcpu_load(vcpu);
2459 r = kvm_mmu_setup(vcpu);
2461 r = kvm_arch_ops->vcpu_setup(vcpu);
2465 goto out_free_vcpus;
2467 r = create_vcpu_fd(vcpu);
2469 goto out_free_vcpus;
2471 spin_lock(&kvm_lock);
2472 if (n >= kvm->nvcpus)
2473 kvm->nvcpus = n + 1;
2474 spin_unlock(&kvm_lock);
2479 kvm_free_vcpu(vcpu);
2481 free_page((unsigned long)vcpu->run);
2484 mutex_unlock(&vcpu->mutex);
2489 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
2493 struct kvm_cpuid_entry *e, *entry;
2495 rdmsrl(MSR_EFER, efer);
2497 for (i = 0; i < vcpu->cpuid_nent; ++i) {
2498 e = &vcpu->cpuid_entries[i];
2499 if (e->function == 0x80000001) {
2504 if (entry && (entry->edx & EFER_NX) && !(efer & EFER_NX)) {
2505 entry->edx &= ~(1 << 20);
2506 printk(KERN_INFO ": guest NX capability removed\n");
2510 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
2511 struct kvm_cpuid *cpuid,
2512 struct kvm_cpuid_entry __user *entries)
2517 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
2520 if (copy_from_user(&vcpu->cpuid_entries, entries,
2521 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
2523 vcpu->cpuid_nent = cpuid->nent;
2524 cpuid_fix_nx_cap(vcpu);
2531 static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
2534 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2535 vcpu->sigset_active = 1;
2536 vcpu->sigset = *sigset;
2538 vcpu->sigset_active = 0;
2543 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
2544 * we have asm/x86/processor.h
2555 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
2556 #ifdef CONFIG_X86_64
2557 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
2559 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
2563 static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2565 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2569 memcpy(fpu->fpr, fxsave->st_space, 128);
2570 fpu->fcw = fxsave->cwd;
2571 fpu->fsw = fxsave->swd;
2572 fpu->ftwx = fxsave->twd;
2573 fpu->last_opcode = fxsave->fop;
2574 fpu->last_ip = fxsave->rip;
2575 fpu->last_dp = fxsave->rdp;
2576 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
2583 static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2585 struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
2589 memcpy(fxsave->st_space, fpu->fpr, 128);
2590 fxsave->cwd = fpu->fcw;
2591 fxsave->swd = fpu->fsw;
2592 fxsave->twd = fpu->ftwx;
2593 fxsave->fop = fpu->last_opcode;
2594 fxsave->rip = fpu->last_ip;
2595 fxsave->rdp = fpu->last_dp;
2596 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
2603 static long kvm_vcpu_ioctl(struct file *filp,
2604 unsigned int ioctl, unsigned long arg)
2606 struct kvm_vcpu *vcpu = filp->private_data;
2607 void __user *argp = (void __user *)arg;
2615 r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
2617 case KVM_GET_REGS: {
2618 struct kvm_regs kvm_regs;
2620 memset(&kvm_regs, 0, sizeof kvm_regs);
2621 r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
2625 if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
2630 case KVM_SET_REGS: {
2631 struct kvm_regs kvm_regs;
2634 if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
2636 r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
2642 case KVM_GET_SREGS: {
2643 struct kvm_sregs kvm_sregs;
2645 memset(&kvm_sregs, 0, sizeof kvm_sregs);
2646 r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
2650 if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
2655 case KVM_SET_SREGS: {
2656 struct kvm_sregs kvm_sregs;
2659 if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
2661 r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
2667 case KVM_TRANSLATE: {
2668 struct kvm_translation tr;
2671 if (copy_from_user(&tr, argp, sizeof tr))
2673 r = kvm_vcpu_ioctl_translate(vcpu, &tr);
2677 if (copy_to_user(argp, &tr, sizeof tr))
2682 case KVM_INTERRUPT: {
2683 struct kvm_interrupt irq;
2686 if (copy_from_user(&irq, argp, sizeof irq))
2688 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2694 case KVM_DEBUG_GUEST: {
2695 struct kvm_debug_guest dbg;
2698 if (copy_from_user(&dbg, argp, sizeof dbg))
2700 r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
2707 r = msr_io(vcpu, argp, get_msr, 1);
2710 r = msr_io(vcpu, argp, do_set_msr, 0);
2712 case KVM_SET_CPUID: {
2713 struct kvm_cpuid __user *cpuid_arg = argp;
2714 struct kvm_cpuid cpuid;
2717 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2719 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2724 case KVM_SET_SIGNAL_MASK: {
2725 struct kvm_signal_mask __user *sigmask_arg = argp;
2726 struct kvm_signal_mask kvm_sigmask;
2727 sigset_t sigset, *p;
2732 if (copy_from_user(&kvm_sigmask, argp,
2733 sizeof kvm_sigmask))
2736 if (kvm_sigmask.len != sizeof sigset)
2739 if (copy_from_user(&sigset, sigmask_arg->sigset,
2744 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
2750 memset(&fpu, 0, sizeof fpu);
2751 r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
2755 if (copy_to_user(argp, &fpu, sizeof fpu))
2764 if (copy_from_user(&fpu, argp, sizeof fpu))
2766 r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
2779 static long kvm_vm_ioctl(struct file *filp,
2780 unsigned int ioctl, unsigned long arg)
2782 struct kvm *kvm = filp->private_data;
2783 void __user *argp = (void __user *)arg;
2787 case KVM_CREATE_VCPU:
2788 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
2792 case KVM_SET_MEMORY_REGION: {
2793 struct kvm_memory_region kvm_mem;
2796 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2798 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
2803 case KVM_GET_DIRTY_LOG: {
2804 struct kvm_dirty_log log;
2807 if (copy_from_user(&log, argp, sizeof log))
2809 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
2814 case KVM_SET_MEMORY_ALIAS: {
2815 struct kvm_memory_alias alias;
2818 if (copy_from_user(&alias, argp, sizeof alias))
2820 r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
2832 static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
2833 unsigned long address,
2836 struct kvm *kvm = vma->vm_file->private_data;
2837 unsigned long pgoff;
2840 *type = VM_FAULT_MINOR;
2841 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2842 page = gfn_to_page(kvm, pgoff);
2844 return NOPAGE_SIGBUS;
2849 static struct vm_operations_struct kvm_vm_vm_ops = {
2850 .nopage = kvm_vm_nopage,
2853 static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
2855 vma->vm_ops = &kvm_vm_vm_ops;
2859 static struct file_operations kvm_vm_fops = {
2860 .release = kvm_vm_release,
2861 .unlocked_ioctl = kvm_vm_ioctl,
2862 .compat_ioctl = kvm_vm_ioctl,
2863 .mmap = kvm_vm_mmap,
2866 static int kvm_dev_ioctl_create_vm(void)
2869 struct inode *inode;
2873 inode = kvmfs_inode(&kvm_vm_fops);
2874 if (IS_ERR(inode)) {
2879 kvm = kvm_create_vm();
2885 file = kvmfs_file(inode, kvm);
2892 r = get_unused_fd();
2896 fd_install(fd, file);
2903 kvm_destroy_vm(kvm);
2910 static long kvm_dev_ioctl(struct file *filp,
2911 unsigned int ioctl, unsigned long arg)
2913 void __user *argp = (void __user *)arg;
2917 case KVM_GET_API_VERSION:
2921 r = KVM_API_VERSION;
2927 r = kvm_dev_ioctl_create_vm();
2929 case KVM_GET_MSR_INDEX_LIST: {
2930 struct kvm_msr_list __user *user_msr_list = argp;
2931 struct kvm_msr_list msr_list;
2935 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2938 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2939 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2942 if (n < num_msrs_to_save)
2945 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2946 num_msrs_to_save * sizeof(u32)))
2948 if (copy_to_user(user_msr_list->indices
2949 + num_msrs_to_save * sizeof(u32),
2951 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2956 case KVM_CHECK_EXTENSION:
2958 * No extensions defined at present.
2962 case KVM_GET_VCPU_MMAP_SIZE:
2975 static struct file_operations kvm_chardev_ops = {
2976 .open = kvm_dev_open,
2977 .release = kvm_dev_release,
2978 .unlocked_ioctl = kvm_dev_ioctl,
2979 .compat_ioctl = kvm_dev_ioctl,
2982 static struct miscdevice kvm_dev = {
2988 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2991 if (val == SYS_RESTART) {
2993 * Some (well, at least mine) BIOSes hang on reboot if
2996 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2997 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3002 static struct notifier_block kvm_reboot_notifier = {
3003 .notifier_call = kvm_reboot,
3008 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
3011 static void decache_vcpus_on_cpu(int cpu)
3014 struct kvm_vcpu *vcpu;
3017 spin_lock(&kvm_lock);
3018 list_for_each_entry(vm, &vm_list, vm_list)
3019 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3020 vcpu = &vm->vcpus[i];
3022 * If the vcpu is locked, then it is running on some
3023 * other cpu and therefore it is not cached on the
3026 * If it's not locked, check the last cpu it executed
3029 if (mutex_trylock(&vcpu->mutex)) {
3030 if (vcpu->cpu == cpu) {
3031 kvm_arch_ops->vcpu_decache(vcpu);
3034 mutex_unlock(&vcpu->mutex);
3037 spin_unlock(&kvm_lock);
3040 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
3046 case CPU_DOWN_PREPARE:
3047 case CPU_DOWN_PREPARE_FROZEN:
3048 case CPU_UP_CANCELED:
3049 case CPU_UP_CANCELED_FROZEN:
3050 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
3052 decache_vcpus_on_cpu(cpu);
3053 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
3057 case CPU_ONLINE_FROZEN:
3058 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
3060 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
3067 void kvm_io_bus_init(struct kvm_io_bus *bus)
3069 memset(bus, 0, sizeof(*bus));
3072 void kvm_io_bus_destroy(struct kvm_io_bus *bus)
3076 for (i = 0; i < bus->dev_count; i++) {
3077 struct kvm_io_device *pos = bus->devs[i];
3079 kvm_iodevice_destructor(pos);
3083 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
3087 for (i = 0; i < bus->dev_count; i++) {
3088 struct kvm_io_device *pos = bus->devs[i];
3090 if (pos->in_range(pos, addr))
3097 void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
3099 BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
3101 bus->devs[bus->dev_count++] = dev;
3104 static struct notifier_block kvm_cpu_notifier = {
3105 .notifier_call = kvm_cpu_hotplug,
3106 .priority = 20, /* must be > scheduler priority */
3109 static u64 stat_get(void *_offset)
3111 unsigned offset = (long)_offset;
3114 struct kvm_vcpu *vcpu;
3117 spin_lock(&kvm_lock);
3118 list_for_each_entry(kvm, &vm_list, vm_list)
3119 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
3120 vcpu = &kvm->vcpus[i];
3121 total += *(u32 *)((void *)vcpu + offset);
3123 spin_unlock(&kvm_lock);
3127 static void stat_set(void *offset, u64 val)
3131 DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
3133 static __init void kvm_init_debug(void)
3135 struct kvm_stats_debugfs_item *p;
3137 debugfs_dir = debugfs_create_dir("kvm", NULL);
3138 for (p = debugfs_entries; p->name; ++p)
3139 p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
3140 (void *)(long)p->offset,
3144 static void kvm_exit_debug(void)
3146 struct kvm_stats_debugfs_item *p;
3148 for (p = debugfs_entries; p->name; ++p)
3149 debugfs_remove(p->dentry);
3150 debugfs_remove(debugfs_dir);
3153 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
3155 decache_vcpus_on_cpu(raw_smp_processor_id());
3156 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3160 static int kvm_resume(struct sys_device *dev)
3162 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
3166 static struct sysdev_class kvm_sysdev_class = {
3167 set_kset_name("kvm"),
3168 .suspend = kvm_suspend,
3169 .resume = kvm_resume,
3172 static struct sys_device kvm_sysdev = {
3174 .cls = &kvm_sysdev_class,
3177 hpa_t bad_page_address;
3179 static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
3180 const char *dev_name, void *data, struct vfsmount *mnt)
3182 return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
3185 static struct file_system_type kvm_fs_type = {
3187 .get_sb = kvmfs_get_sb,
3188 .kill_sb = kill_anon_super,
3191 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
3196 printk(KERN_ERR "kvm: already loaded the other module\n");
3200 if (!ops->cpu_has_kvm_support()) {
3201 printk(KERN_ERR "kvm: no hardware support\n");
3204 if (ops->disabled_by_bios()) {
3205 printk(KERN_ERR "kvm: disabled by bios\n");
3211 r = kvm_arch_ops->hardware_setup();
3215 on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
3216 r = register_cpu_notifier(&kvm_cpu_notifier);
3219 register_reboot_notifier(&kvm_reboot_notifier);
3221 r = sysdev_class_register(&kvm_sysdev_class);
3225 r = sysdev_register(&kvm_sysdev);
3229 kvm_chardev_ops.owner = module;
3231 r = misc_register(&kvm_dev);
3233 printk (KERN_ERR "kvm: misc device register failed\n");
3240 sysdev_unregister(&kvm_sysdev);
3242 sysdev_class_unregister(&kvm_sysdev_class);
3244 unregister_reboot_notifier(&kvm_reboot_notifier);
3245 unregister_cpu_notifier(&kvm_cpu_notifier);
3247 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3248 kvm_arch_ops->hardware_unsetup();
3250 kvm_arch_ops = NULL;
3254 void kvm_exit_arch(void)
3256 misc_deregister(&kvm_dev);
3257 sysdev_unregister(&kvm_sysdev);
3258 sysdev_class_unregister(&kvm_sysdev_class);
3259 unregister_reboot_notifier(&kvm_reboot_notifier);
3260 unregister_cpu_notifier(&kvm_cpu_notifier);
3261 on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
3262 kvm_arch_ops->hardware_unsetup();
3263 kvm_arch_ops = NULL;
3266 static __init int kvm_init(void)
3268 static struct page *bad_page;
3271 r = kvm_mmu_module_init();
3275 r = register_filesystem(&kvm_fs_type);
3279 kvmfs_mnt = kern_mount(&kvm_fs_type);
3280 r = PTR_ERR(kvmfs_mnt);
3281 if (IS_ERR(kvmfs_mnt))
3285 kvm_init_msr_list();
3287 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
3292 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
3293 memset(__va(bad_page_address), 0, PAGE_SIZE);
3301 unregister_filesystem(&kvm_fs_type);
3303 kvm_mmu_module_exit();
3308 static __exit void kvm_exit(void)
3311 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
3313 unregister_filesystem(&kvm_fs_type);
3314 kvm_mmu_module_exit();
3317 module_init(kvm_init)
3318 module_exit(kvm_exit)
3320 EXPORT_SYMBOL_GPL(kvm_init_arch);
3321 EXPORT_SYMBOL_GPL(kvm_exit_arch);