kvm: release: merge from trunk
[kvm-userspace.git] / kernel / kvm_main.c
bloba8b3691afda0ec17f2c436aecbfdf4adedf58886
1 /*
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.
9 * Authors:
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.
18 #include "kvm.h"
20 #include <linux/kvm.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <asm/processor.h>
24 #include <linux/percpu.h>
25 #include <linux/gfp.h>
26 #include <asm/msr.h>
27 #include <linux/mm.h>
28 #include <linux/miscdevice.h>
29 #include <linux/vmalloc.h>
30 #include <asm/uaccess.h>
31 #include <linux/reboot.h>
32 #include <asm/io.h>
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/file.h>
36 #include <asm/desc.h>
37 #include <linux/sysdev.h>
38 #include <linux/cpu.h>
40 #include "x86_emulate.h"
41 #include "segment_descriptor.h"
43 MODULE_AUTHOR("Qumranet");
44 MODULE_LICENSE("GPL");
46 static spinlock_t kvm_lock = SPIN_LOCK_UNLOCKED;
47 static struct list_head vm_list = LIST_HEAD_INIT(vm_list);
49 struct kvm_arch_ops *kvm_arch_ops;
50 struct kvm_stat kvm_stat;
51 EXPORT_SYMBOL_GPL(kvm_stat);
53 static struct kvm_stats_debugfs_item {
54 const char *name;
55 u32 *data;
56 struct dentry *dentry;
57 } debugfs_entries[] = {
58 { "pf_fixed", &kvm_stat.pf_fixed },
59 { "pf_guest", &kvm_stat.pf_guest },
60 { "tlb_flush", &kvm_stat.tlb_flush },
61 { "invlpg", &kvm_stat.invlpg },
62 { "exits", &kvm_stat.exits },
63 { "io_exits", &kvm_stat.io_exits },
64 { "mmio_exits", &kvm_stat.mmio_exits },
65 { "signal_exits", &kvm_stat.signal_exits },
66 { "irq_window", &kvm_stat.irq_window_exits },
67 { "halt_exits", &kvm_stat.halt_exits },
68 { "request_irq", &kvm_stat.request_irq_exits },
69 { "irq_exits", &kvm_stat.irq_exits },
70 { 0, 0 }
73 static struct dentry *debugfs_dir;
75 #define MAX_IO_MSRS 256
77 #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
78 #define LMSW_GUEST_MASK 0x0eULL
79 #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
80 #define CR8_RESEVED_BITS (~0x0fULL)
81 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
83 #ifdef CONFIG_X86_64
84 // LDT or TSS descriptor in the GDT. 16 bytes.
85 struct segment_descriptor_64 {
86 struct segment_descriptor s;
87 u32 base_higher;
88 u32 pad_zero;
91 #endif
93 unsigned long segment_base(u16 selector)
95 struct descriptor_table gdt;
96 struct segment_descriptor *d;
97 unsigned long table_base;
98 typedef unsigned long ul;
99 unsigned long v;
101 if (selector == 0)
102 return 0;
104 asm ("sgdt %0" : "=m"(gdt));
105 table_base = gdt.base;
107 if (selector & 4) { /* from ldt */
108 u16 ldt_selector;
110 asm ("sldt %0" : "=g"(ldt_selector));
111 table_base = segment_base(ldt_selector);
113 d = (struct segment_descriptor *)(table_base + (selector & ~7));
114 v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
115 #ifdef CONFIG_X86_64
116 if (d->system == 0
117 && (d->type == 2 || d->type == 9 || d->type == 11))
118 v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
119 #endif
120 return v;
122 EXPORT_SYMBOL_GPL(segment_base);
124 static inline int valid_vcpu(int n)
126 return likely(n >= 0 && n < KVM_MAX_VCPUS);
129 int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
130 void *dest)
132 unsigned char *host_buf = dest;
133 unsigned long req_size = size;
135 while (size) {
136 hpa_t paddr;
137 unsigned now;
138 unsigned offset;
139 hva_t guest_buf;
141 paddr = gva_to_hpa(vcpu, addr);
143 if (is_error_hpa(paddr))
144 break;
146 guest_buf = (hva_t)kmap_atomic(
147 pfn_to_page(paddr >> PAGE_SHIFT),
148 KM_USER0);
149 offset = addr & ~PAGE_MASK;
150 guest_buf |= offset;
151 now = min(size, PAGE_SIZE - offset);
152 memcpy(host_buf, (void*)guest_buf, now);
153 host_buf += now;
154 addr += now;
155 size -= now;
156 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
158 return req_size - size;
160 EXPORT_SYMBOL_GPL(kvm_read_guest);
162 int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
163 void *data)
165 unsigned char *host_buf = data;
166 unsigned long req_size = size;
168 while (size) {
169 hpa_t paddr;
170 unsigned now;
171 unsigned offset;
172 hva_t guest_buf;
174 paddr = gva_to_hpa(vcpu, addr);
176 if (is_error_hpa(paddr))
177 break;
179 guest_buf = (hva_t)kmap_atomic(
180 pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
181 offset = addr & ~PAGE_MASK;
182 guest_buf |= offset;
183 now = min(size, PAGE_SIZE - offset);
184 memcpy((void*)guest_buf, host_buf, now);
185 host_buf += now;
186 addr += now;
187 size -= now;
188 kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
190 return req_size - size;
192 EXPORT_SYMBOL_GPL(kvm_write_guest);
194 static int vcpu_slot(struct kvm_vcpu *vcpu)
196 return vcpu - vcpu->kvm->vcpus;
200 * Switches to specified vcpu, until a matching vcpu_put()
202 static struct kvm_vcpu *vcpu_load(struct kvm *kvm, int vcpu_slot)
204 struct kvm_vcpu *vcpu = &kvm->vcpus[vcpu_slot];
206 mutex_lock(&vcpu->mutex);
207 if (unlikely(!vcpu->vmcs)) {
208 mutex_unlock(&vcpu->mutex);
209 return 0;
211 return kvm_arch_ops->vcpu_load(vcpu);
214 static void vcpu_put(struct kvm_vcpu *vcpu)
216 kvm_arch_ops->vcpu_put(vcpu);
217 mutex_unlock(&vcpu->mutex);
220 static int kvm_dev_open(struct inode *inode, struct file *filp)
222 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
223 int i;
225 if (!kvm)
226 return -ENOMEM;
228 spin_lock_init(&kvm->lock);
229 INIT_LIST_HEAD(&kvm->active_mmu_pages);
230 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
231 struct kvm_vcpu *vcpu = &kvm->vcpus[i];
233 mutex_init(&vcpu->mutex);
234 vcpu->cpu = -1;
235 vcpu->kvm = kvm;
236 vcpu->mmu.root_hpa = INVALID_PAGE;
237 INIT_LIST_HEAD(&vcpu->free_pages);
238 spin_lock(&kvm_lock);
239 list_add(&kvm->vm_list, &vm_list);
240 spin_unlock(&kvm_lock);
242 filp->private_data = kvm;
243 return 0;
247 * Free any memory in @free but not in @dont.
249 static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
250 struct kvm_memory_slot *dont)
252 int i;
254 if (!dont || free->phys_mem != dont->phys_mem)
255 if (free->phys_mem) {
256 for (i = 0; i < free->npages; ++i)
257 if (free->phys_mem[i])
258 __free_page(free->phys_mem[i]);
259 vfree(free->phys_mem);
262 if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
263 vfree(free->dirty_bitmap);
265 free->phys_mem = 0;
266 free->npages = 0;
267 free->dirty_bitmap = 0;
270 static void kvm_free_physmem(struct kvm *kvm)
272 int i;
274 for (i = 0; i < kvm->nmemslots; ++i)
275 kvm_free_physmem_slot(&kvm->memslots[i], 0);
278 static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
280 if (!vcpu_load(vcpu->kvm, vcpu_slot(vcpu)))
281 return;
283 kvm_mmu_destroy(vcpu);
284 vcpu_put(vcpu);
285 kvm_arch_ops->vcpu_free(vcpu);
288 static void kvm_free_vcpus(struct kvm *kvm)
290 unsigned int i;
292 for (i = 0; i < KVM_MAX_VCPUS; ++i)
293 kvm_free_vcpu(&kvm->vcpus[i]);
296 static int kvm_dev_release(struct inode *inode, struct file *filp)
298 struct kvm *kvm = filp->private_data;
300 spin_lock(&kvm_lock);
301 list_del(&kvm->vm_list);
302 spin_unlock(&kvm_lock);
303 kvm_free_vcpus(kvm);
304 kvm_free_physmem(kvm);
305 kfree(kvm);
306 return 0;
309 static void inject_gp(struct kvm_vcpu *vcpu)
311 kvm_arch_ops->inject_gp(vcpu, 0);
315 * Load the pae pdptrs. Return true is they are all valid.
317 static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
319 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
320 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
321 int i;
322 u64 pdpte;
323 u64 *pdpt;
324 int ret;
325 struct kvm_memory_slot *memslot;
327 spin_lock(&vcpu->kvm->lock);
328 memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
329 /* FIXME: !memslot - emulate? 0xff? */
330 pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
332 ret = 1;
333 for (i = 0; i < 4; ++i) {
334 pdpte = pdpt[offset + i];
335 if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
336 ret = 0;
337 goto out;
341 for (i = 0; i < 4; ++i)
342 vcpu->pdptrs[i] = pdpt[offset + i];
344 out:
345 kunmap_atomic(pdpt, KM_USER0);
346 spin_unlock(&vcpu->kvm->lock);
348 return ret;
351 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
353 if (cr0 & CR0_RESEVED_BITS) {
354 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
355 cr0, vcpu->cr0);
356 inject_gp(vcpu);
357 return;
360 if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
361 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
362 inject_gp(vcpu);
363 return;
366 if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
367 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
368 "and a clear PE flag\n");
369 inject_gp(vcpu);
370 return;
373 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
374 #ifdef CONFIG_X86_64
375 if ((vcpu->shadow_efer & EFER_LME)) {
376 int cs_db, cs_l;
378 if (!is_pae(vcpu)) {
379 printk(KERN_DEBUG "set_cr0: #GP, start paging "
380 "in long mode while PAE is disabled\n");
381 inject_gp(vcpu);
382 return;
384 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
385 if (cs_l) {
386 printk(KERN_DEBUG "set_cr0: #GP, start paging "
387 "in long mode while CS.L == 1\n");
388 inject_gp(vcpu);
389 return;
392 } else
393 #endif
394 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
395 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
396 "reserved bits\n");
397 inject_gp(vcpu);
398 return;
403 kvm_arch_ops->set_cr0(vcpu, cr0);
404 vcpu->cr0 = cr0;
406 spin_lock(&vcpu->kvm->lock);
407 kvm_mmu_reset_context(vcpu);
408 spin_unlock(&vcpu->kvm->lock);
409 return;
411 EXPORT_SYMBOL_GPL(set_cr0);
413 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
415 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
416 set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
418 EXPORT_SYMBOL_GPL(lmsw);
420 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
422 if (cr4 & CR4_RESEVED_BITS) {
423 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
424 inject_gp(vcpu);
425 return;
428 if (is_long_mode(vcpu)) {
429 if (!(cr4 & CR4_PAE_MASK)) {
430 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
431 "in long mode\n");
432 inject_gp(vcpu);
433 return;
435 } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
436 && !load_pdptrs(vcpu, vcpu->cr3)) {
437 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
438 inject_gp(vcpu);
441 if (cr4 & CR4_VMXE_MASK) {
442 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
443 inject_gp(vcpu);
444 return;
446 kvm_arch_ops->set_cr4(vcpu, cr4);
447 spin_lock(&vcpu->kvm->lock);
448 kvm_mmu_reset_context(vcpu);
449 spin_unlock(&vcpu->kvm->lock);
451 EXPORT_SYMBOL_GPL(set_cr4);
453 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
455 if (is_long_mode(vcpu)) {
456 if (cr3 & CR3_L_MODE_RESEVED_BITS) {
457 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
458 inject_gp(vcpu);
459 return;
461 } else {
462 if (cr3 & CR3_RESEVED_BITS) {
463 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
464 inject_gp(vcpu);
465 return;
467 if (is_paging(vcpu) && is_pae(vcpu) &&
468 !load_pdptrs(vcpu, cr3)) {
469 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
470 "reserved bits\n");
471 inject_gp(vcpu);
472 return;
476 vcpu->cr3 = cr3;
477 spin_lock(&vcpu->kvm->lock);
479 * Does the new cr3 value map to physical memory? (Note, we
480 * catch an invalid cr3 even in real-mode, because it would
481 * cause trouble later on when we turn on paging anyway.)
483 * A real CPU would silently accept an invalid cr3 and would
484 * attempt to use it - with largely undefined (and often hard
485 * to debug) behavior on the guest side.
487 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
488 inject_gp(vcpu);
489 else
490 vcpu->mmu.new_cr3(vcpu);
491 spin_unlock(&vcpu->kvm->lock);
493 EXPORT_SYMBOL_GPL(set_cr3);
495 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
497 if ( cr8 & CR8_RESEVED_BITS) {
498 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
499 inject_gp(vcpu);
500 return;
502 vcpu->cr8 = cr8;
504 EXPORT_SYMBOL_GPL(set_cr8);
506 void fx_init(struct kvm_vcpu *vcpu)
508 struct __attribute__ ((__packed__)) fx_image_s {
509 u16 control; //fcw
510 u16 status; //fsw
511 u16 tag; // ftw
512 u16 opcode; //fop
513 u64 ip; // fpu ip
514 u64 operand;// fpu dp
515 u32 mxcsr;
516 u32 mxcsr_mask;
518 } *fx_image;
520 fx_save(vcpu->host_fx_image);
521 fpu_init();
522 fx_save(vcpu->guest_fx_image);
523 fx_restore(vcpu->host_fx_image);
525 fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
526 fx_image->mxcsr = 0x1f80;
527 memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
528 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
530 EXPORT_SYMBOL_GPL(fx_init);
533 * Creates some virtual cpus. Good luck creating more than one.
535 static int kvm_dev_ioctl_create_vcpu(struct kvm *kvm, int n)
537 int r;
538 struct kvm_vcpu *vcpu;
540 r = -EINVAL;
541 if (!valid_vcpu(n))
542 goto out;
544 vcpu = &kvm->vcpus[n];
546 mutex_lock(&vcpu->mutex);
548 if (vcpu->vmcs) {
549 mutex_unlock(&vcpu->mutex);
550 return -EEXIST;
553 vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
554 FX_IMAGE_ALIGN);
555 vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
557 r = kvm_arch_ops->vcpu_create(vcpu);
558 if (r < 0)
559 goto out_free_vcpus;
561 r = kvm_mmu_create(vcpu);
562 if (r < 0)
563 goto out_free_vcpus;
565 kvm_arch_ops->vcpu_load(vcpu);
566 r = kvm_mmu_setup(vcpu);
567 if (r >= 0)
568 r = kvm_arch_ops->vcpu_setup(vcpu);
569 vcpu_put(vcpu);
571 if (r < 0)
572 goto out_free_vcpus;
574 return 0;
576 out_free_vcpus:
577 kvm_free_vcpu(vcpu);
578 mutex_unlock(&vcpu->mutex);
579 out:
580 return r;
584 * Allocate some memory and give it an address in the guest physical address
585 * space.
587 * Discontiguous memory is allowed, mostly for framebuffers.
589 static int kvm_dev_ioctl_set_memory_region(struct kvm *kvm,
590 struct kvm_memory_region *mem)
592 int r;
593 gfn_t base_gfn;
594 unsigned long npages;
595 unsigned long i;
596 struct kvm_memory_slot *memslot;
597 struct kvm_memory_slot old, new;
598 int memory_config_version;
600 r = -EINVAL;
601 /* General sanity checks */
602 if (mem->memory_size & (PAGE_SIZE - 1))
603 goto out;
604 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
605 goto out;
606 if (mem->slot >= KVM_MEMORY_SLOTS)
607 goto out;
608 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
609 goto out;
611 memslot = &kvm->memslots[mem->slot];
612 base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
613 npages = mem->memory_size >> PAGE_SHIFT;
615 if (!npages)
616 mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
618 raced:
619 spin_lock(&kvm->lock);
621 memory_config_version = kvm->memory_config_version;
622 new = old = *memslot;
624 new.base_gfn = base_gfn;
625 new.npages = npages;
626 new.flags = mem->flags;
628 /* Disallow changing a memory slot's size. */
629 r = -EINVAL;
630 if (npages && old.npages && npages != old.npages)
631 goto out_unlock;
633 /* Check for overlaps */
634 r = -EEXIST;
635 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
636 struct kvm_memory_slot *s = &kvm->memslots[i];
638 if (s == memslot)
639 continue;
640 if (!((base_gfn + npages <= s->base_gfn) ||
641 (base_gfn >= s->base_gfn + s->npages)))
642 goto out_unlock;
645 * Do memory allocations outside lock. memory_config_version will
646 * detect any races.
648 spin_unlock(&kvm->lock);
650 /* Deallocate if slot is being removed */
651 if (!npages)
652 new.phys_mem = 0;
654 /* Free page dirty bitmap if unneeded */
655 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
656 new.dirty_bitmap = 0;
658 r = -ENOMEM;
660 /* Allocate if a slot is being created */
661 if (npages && !new.phys_mem) {
662 new.phys_mem = vmalloc(npages * sizeof(struct page *));
664 if (!new.phys_mem)
665 goto out_free;
667 memset(new.phys_mem, 0, npages * sizeof(struct page *));
668 for (i = 0; i < npages; ++i) {
669 new.phys_mem[i] = alloc_page(GFP_HIGHUSER
670 | __GFP_ZERO);
671 if (!new.phys_mem[i])
672 goto out_free;
673 set_page_private(new.phys_mem[i],0);
677 /* Allocate page dirty bitmap if needed */
678 if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
679 unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
681 new.dirty_bitmap = vmalloc(dirty_bytes);
682 if (!new.dirty_bitmap)
683 goto out_free;
684 memset(new.dirty_bitmap, 0, dirty_bytes);
687 spin_lock(&kvm->lock);
689 if (memory_config_version != kvm->memory_config_version) {
690 spin_unlock(&kvm->lock);
691 kvm_free_physmem_slot(&new, &old);
692 goto raced;
695 r = -EAGAIN;
696 if (kvm->busy)
697 goto out_unlock;
699 if (mem->slot >= kvm->nmemslots)
700 kvm->nmemslots = mem->slot + 1;
702 *memslot = new;
703 ++kvm->memory_config_version;
705 spin_unlock(&kvm->lock);
707 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
708 struct kvm_vcpu *vcpu;
710 vcpu = vcpu_load(kvm, i);
711 if (!vcpu)
712 continue;
713 kvm_mmu_reset_context(vcpu);
714 vcpu_put(vcpu);
717 kvm_free_physmem_slot(&old, &new);
718 return 0;
720 out_unlock:
721 spin_unlock(&kvm->lock);
722 out_free:
723 kvm_free_physmem_slot(&new, &old);
724 out:
725 return r;
728 static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
730 spin_lock(&vcpu->kvm->lock);
731 kvm_mmu_slot_remove_write_access(vcpu, slot);
732 spin_unlock(&vcpu->kvm->lock);
736 * Get (and clear) the dirty memory log for a memory slot.
738 static int kvm_dev_ioctl_get_dirty_log(struct kvm *kvm,
739 struct kvm_dirty_log *log)
741 struct kvm_memory_slot *memslot;
742 int r, i;
743 int n;
744 int cleared;
745 unsigned long any = 0;
747 spin_lock(&kvm->lock);
750 * Prevent changes to guest memory configuration even while the lock
751 * is not taken.
753 ++kvm->busy;
754 spin_unlock(&kvm->lock);
755 r = -EINVAL;
756 if (log->slot >= KVM_MEMORY_SLOTS)
757 goto out;
759 memslot = &kvm->memslots[log->slot];
760 r = -ENOENT;
761 if (!memslot->dirty_bitmap)
762 goto out;
764 n = ALIGN(memslot->npages, 8) / 8;
766 for (i = 0; !any && i < n; ++i)
767 any = memslot->dirty_bitmap[i];
769 r = -EFAULT;
770 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
771 goto out;
773 if (any) {
774 cleared = 0;
775 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
776 struct kvm_vcpu *vcpu = vcpu_load(kvm, i);
778 if (!vcpu)
779 continue;
780 if (!cleared) {
781 do_remove_write_access(vcpu, log->slot);
782 memset(memslot->dirty_bitmap, 0, n);
783 cleared = 1;
785 kvm_arch_ops->tlb_flush(vcpu);
786 vcpu_put(vcpu);
790 r = 0;
792 out:
793 spin_lock(&kvm->lock);
794 --kvm->busy;
795 spin_unlock(&kvm->lock);
796 return r;
799 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
801 int i;
803 for (i = 0; i < kvm->nmemslots; ++i) {
804 struct kvm_memory_slot *memslot = &kvm->memslots[i];
806 if (gfn >= memslot->base_gfn
807 && gfn < memslot->base_gfn + memslot->npages)
808 return memslot;
810 return 0;
812 EXPORT_SYMBOL_GPL(gfn_to_memslot);
814 void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
816 int i;
817 struct kvm_memory_slot *memslot = 0;
818 unsigned long rel_gfn;
820 for (i = 0; i < kvm->nmemslots; ++i) {
821 memslot = &kvm->memslots[i];
823 if (gfn >= memslot->base_gfn
824 && gfn < memslot->base_gfn + memslot->npages) {
826 if (!memslot || !memslot->dirty_bitmap)
827 return;
829 rel_gfn = gfn - memslot->base_gfn;
831 /* avoid RMW */
832 if (!test_bit(rel_gfn, memslot->dirty_bitmap))
833 set_bit(rel_gfn, memslot->dirty_bitmap);
834 return;
839 static int emulator_read_std(unsigned long addr,
840 unsigned long *val,
841 unsigned int bytes,
842 struct x86_emulate_ctxt *ctxt)
844 struct kvm_vcpu *vcpu = ctxt->vcpu;
845 void *data = val;
847 while (bytes) {
848 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
849 unsigned offset = addr & (PAGE_SIZE-1);
850 unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
851 unsigned long pfn;
852 struct kvm_memory_slot *memslot;
853 void *page;
855 if (gpa == UNMAPPED_GVA)
856 return X86EMUL_PROPAGATE_FAULT;
857 pfn = gpa >> PAGE_SHIFT;
858 memslot = gfn_to_memslot(vcpu->kvm, pfn);
859 if (!memslot)
860 return X86EMUL_UNHANDLEABLE;
861 page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
863 memcpy(data, page + offset, tocopy);
865 kunmap_atomic(page, KM_USER0);
867 bytes -= tocopy;
868 data += tocopy;
869 addr += tocopy;
872 return X86EMUL_CONTINUE;
875 static int emulator_write_std(unsigned long addr,
876 unsigned long val,
877 unsigned int bytes,
878 struct x86_emulate_ctxt *ctxt)
880 printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
881 addr, bytes);
882 return X86EMUL_UNHANDLEABLE;
885 static int emulator_read_emulated(unsigned long addr,
886 unsigned long *val,
887 unsigned int bytes,
888 struct x86_emulate_ctxt *ctxt)
890 struct kvm_vcpu *vcpu = ctxt->vcpu;
892 if (vcpu->mmio_read_completed) {
893 memcpy(val, vcpu->mmio_data, bytes);
894 vcpu->mmio_read_completed = 0;
895 return X86EMUL_CONTINUE;
896 } else if (emulator_read_std(addr, val, bytes, ctxt)
897 == X86EMUL_CONTINUE)
898 return X86EMUL_CONTINUE;
899 else {
900 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
902 if (gpa == UNMAPPED_GVA)
903 return X86EMUL_PROPAGATE_FAULT;
904 vcpu->mmio_needed = 1;
905 vcpu->mmio_phys_addr = gpa;
906 vcpu->mmio_size = bytes;
907 vcpu->mmio_is_write = 0;
909 return X86EMUL_UNHANDLEABLE;
913 static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
914 unsigned long val, int bytes)
916 struct kvm_memory_slot *m;
917 struct page *page;
918 void *virt;
920 if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
921 return 0;
922 m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
923 if (!m)
924 return 0;
925 page = gfn_to_page(m, gpa >> PAGE_SHIFT);
926 kvm_mmu_pre_write(vcpu, gpa, bytes);
927 virt = kmap_atomic(page, KM_USER0);
928 memcpy(virt + offset_in_page(gpa), &val, bytes);
929 kunmap_atomic(virt, KM_USER0);
930 kvm_mmu_post_write(vcpu, gpa, bytes);
931 return 1;
934 static int emulator_write_emulated(unsigned long addr,
935 unsigned long val,
936 unsigned int bytes,
937 struct x86_emulate_ctxt *ctxt)
939 struct kvm_vcpu *vcpu = ctxt->vcpu;
940 gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
942 if (gpa == UNMAPPED_GVA)
943 return X86EMUL_PROPAGATE_FAULT;
945 if (emulator_write_phys(vcpu, gpa, val, bytes))
946 return X86EMUL_CONTINUE;
948 vcpu->mmio_needed = 1;
949 vcpu->mmio_phys_addr = gpa;
950 vcpu->mmio_size = bytes;
951 vcpu->mmio_is_write = 1;
952 memcpy(vcpu->mmio_data, &val, bytes);
954 return X86EMUL_CONTINUE;
957 static int emulator_cmpxchg_emulated(unsigned long addr,
958 unsigned long old,
959 unsigned long new,
960 unsigned int bytes,
961 struct x86_emulate_ctxt *ctxt)
963 static int reported;
965 if (!reported) {
966 reported = 1;
967 printk(KERN_WARNING "kvm: emulating exchange as write\n");
969 return emulator_write_emulated(addr, new, bytes, ctxt);
972 #ifdef CONFIG_X86_32
974 static int emulator_cmpxchg8b_emulated(unsigned long addr,
975 unsigned long old_lo,
976 unsigned long old_hi,
977 unsigned long new_lo,
978 unsigned long new_hi,
979 struct x86_emulate_ctxt *ctxt)
981 static int reported;
982 int r;
984 if (!reported) {
985 reported = 1;
986 printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
988 r = emulator_write_emulated(addr, new_lo, 4, ctxt);
989 if (r != X86EMUL_CONTINUE)
990 return r;
991 return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
994 #endif
996 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
998 return kvm_arch_ops->get_segment_base(vcpu, seg);
1001 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
1003 return X86EMUL_CONTINUE;
1006 int emulate_clts(struct kvm_vcpu *vcpu)
1008 unsigned long cr0;
1010 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1011 cr0 = vcpu->cr0 & ~CR0_TS_MASK;
1012 kvm_arch_ops->set_cr0(vcpu, cr0);
1013 return X86EMUL_CONTINUE;
1016 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
1018 struct kvm_vcpu *vcpu = ctxt->vcpu;
1020 switch (dr) {
1021 case 0 ... 3:
1022 *dest = kvm_arch_ops->get_dr(vcpu, dr);
1023 return X86EMUL_CONTINUE;
1024 default:
1025 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1026 __FUNCTION__, dr);
1027 return X86EMUL_UNHANDLEABLE;
1031 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
1033 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
1034 int exception;
1036 kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
1037 if (exception) {
1038 /* FIXME: better handling */
1039 return X86EMUL_UNHANDLEABLE;
1041 return X86EMUL_CONTINUE;
1044 static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
1046 static int reported;
1047 u8 opcodes[4];
1048 unsigned long rip = ctxt->vcpu->rip;
1049 unsigned long rip_linear;
1051 rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
1053 if (reported)
1054 return;
1056 emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
1058 printk(KERN_ERR "emulation failed but !mmio_needed?"
1059 " rip %lx %02x %02x %02x %02x\n",
1060 rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
1061 reported = 1;
1064 struct x86_emulate_ops emulate_ops = {
1065 .read_std = emulator_read_std,
1066 .write_std = emulator_write_std,
1067 .read_emulated = emulator_read_emulated,
1068 .write_emulated = emulator_write_emulated,
1069 .cmpxchg_emulated = emulator_cmpxchg_emulated,
1070 #ifdef CONFIG_X86_32
1071 .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
1072 #endif
1075 int emulate_instruction(struct kvm_vcpu *vcpu,
1076 struct kvm_run *run,
1077 unsigned long cr2,
1078 u16 error_code)
1080 struct x86_emulate_ctxt emulate_ctxt;
1081 int r;
1082 int cs_db, cs_l;
1084 kvm_arch_ops->cache_regs(vcpu);
1086 kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
1088 emulate_ctxt.vcpu = vcpu;
1089 emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
1090 emulate_ctxt.cr2 = cr2;
1091 emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
1092 ? X86EMUL_MODE_REAL : cs_l
1093 ? X86EMUL_MODE_PROT64 : cs_db
1094 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
1096 if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
1097 emulate_ctxt.cs_base = 0;
1098 emulate_ctxt.ds_base = 0;
1099 emulate_ctxt.es_base = 0;
1100 emulate_ctxt.ss_base = 0;
1101 } else {
1102 emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
1103 emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
1104 emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
1105 emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
1108 emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
1109 emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
1111 vcpu->mmio_is_write = 0;
1112 r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
1114 if ((r || vcpu->mmio_is_write) && run) {
1115 run->mmio.phys_addr = vcpu->mmio_phys_addr;
1116 memcpy(run->mmio.data, vcpu->mmio_data, 8);
1117 run->mmio.len = vcpu->mmio_size;
1118 run->mmio.is_write = vcpu->mmio_is_write;
1121 if (r) {
1122 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
1123 return EMULATE_DONE;
1124 if (!vcpu->mmio_needed) {
1125 report_emulation_failure(&emulate_ctxt);
1126 return EMULATE_FAIL;
1128 return EMULATE_DO_MMIO;
1131 kvm_arch_ops->decache_regs(vcpu);
1132 kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
1134 if (vcpu->mmio_is_write)
1135 return EMULATE_DO_MMIO;
1137 return EMULATE_DONE;
1139 EXPORT_SYMBOL_GPL(emulate_instruction);
1141 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
1143 unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
1145 kvm_arch_ops->decache_regs(vcpu);
1146 ret = -KVM_EINVAL;
1147 #ifdef CONFIG_X86_64
1148 if (is_long_mode(vcpu)) {
1149 nr = vcpu->regs[VCPU_REGS_RAX];
1150 a0 = vcpu->regs[VCPU_REGS_RDI];
1151 a1 = vcpu->regs[VCPU_REGS_RSI];
1152 a2 = vcpu->regs[VCPU_REGS_RDX];
1153 a3 = vcpu->regs[VCPU_REGS_RCX];
1154 a4 = vcpu->regs[VCPU_REGS_R8];
1155 a5 = vcpu->regs[VCPU_REGS_R9];
1156 } else
1157 #endif
1159 nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
1160 a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
1161 a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
1162 a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
1163 a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
1164 a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
1165 a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
1167 switch (nr) {
1168 default:
1171 vcpu->regs[VCPU_REGS_RAX] = ret;
1172 kvm_arch_ops->cache_regs(vcpu);
1173 return 1;
1175 EXPORT_SYMBOL_GPL(kvm_hypercall);
1177 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
1179 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
1182 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1184 struct descriptor_table dt = { limit, base };
1186 kvm_arch_ops->set_gdt(vcpu, &dt);
1189 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
1191 struct descriptor_table dt = { limit, base };
1193 kvm_arch_ops->set_idt(vcpu, &dt);
1196 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
1197 unsigned long *rflags)
1199 lmsw(vcpu, msw);
1200 *rflags = kvm_arch_ops->get_rflags(vcpu);
1203 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
1205 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1206 switch (cr) {
1207 case 0:
1208 return vcpu->cr0;
1209 case 2:
1210 return vcpu->cr2;
1211 case 3:
1212 return vcpu->cr3;
1213 case 4:
1214 return vcpu->cr4;
1215 default:
1216 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1217 return 0;
1221 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
1222 unsigned long *rflags)
1224 switch (cr) {
1225 case 0:
1226 set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
1227 *rflags = kvm_arch_ops->get_rflags(vcpu);
1228 break;
1229 case 2:
1230 vcpu->cr2 = val;
1231 break;
1232 case 3:
1233 set_cr3(vcpu, val);
1234 break;
1235 case 4:
1236 set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
1237 break;
1238 default:
1239 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
1244 * Register the para guest with the host:
1246 static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
1248 struct kvm_vcpu_para_state *para_state;
1249 hpa_t para_state_hpa, hypercall_hpa;
1250 struct page *para_state_page;
1251 unsigned char *hypercall;
1252 gpa_t hypercall_gpa;
1254 printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
1255 printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
1258 * Needs to be page aligned:
1260 if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
1261 goto err_gp;
1263 para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
1264 printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
1265 if (is_error_hpa(para_state_hpa))
1266 goto err_gp;
1268 para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
1269 para_state = kmap_atomic(para_state_page, KM_USER0);
1271 printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
1272 printk(KERN_DEBUG ".... size: %d\n", para_state->size);
1274 para_state->host_version = KVM_PARA_API_VERSION;
1276 * We cannot support guests that try to register themselves
1277 * with a newer API version than the host supports:
1279 if (para_state->guest_version > KVM_PARA_API_VERSION) {
1280 para_state->ret = -KVM_EINVAL;
1281 goto err_kunmap_skip;
1284 hypercall_gpa = para_state->hypercall_gpa;
1285 hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
1286 printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
1287 if (is_error_hpa(hypercall_hpa)) {
1288 para_state->ret = -KVM_EINVAL;
1289 goto err_kunmap_skip;
1292 printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
1293 vcpu->para_state_page = para_state_page;
1294 vcpu->para_state_gpa = para_state_gpa;
1295 vcpu->hypercall_gpa = hypercall_gpa;
1297 hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
1298 KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
1299 kvm_arch_ops->patch_hypercall(vcpu, hypercall);
1300 kunmap_atomic(hypercall, KM_USER1);
1302 para_state->ret = 0;
1303 err_kunmap_skip:
1304 kunmap_atomic(para_state, KM_USER0);
1305 return 0;
1306 err_gp:
1307 return 1;
1310 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1312 u64 data;
1314 switch (msr) {
1315 case 0xc0010010: /* SYSCFG */
1316 case 0xc0010015: /* HWCR */
1317 case MSR_IA32_PLATFORM_ID:
1318 case MSR_IA32_P5_MC_ADDR:
1319 case MSR_IA32_P5_MC_TYPE:
1320 case MSR_IA32_MC0_CTL:
1321 case MSR_IA32_MCG_STATUS:
1322 case MSR_IA32_MCG_CAP:
1323 case MSR_IA32_MC0_MISC:
1324 case MSR_IA32_MC0_MISC+4:
1325 case MSR_IA32_MC0_MISC+8:
1326 case MSR_IA32_MC0_MISC+12:
1327 case MSR_IA32_MC0_MISC+16:
1328 case MSR_IA32_UCODE_REV:
1329 case MSR_IA32_PERF_STATUS:
1330 /* MTRR registers */
1331 case 0xfe:
1332 case 0x200 ... 0x2ff:
1333 data = 0;
1334 break;
1335 case 0xcd: /* fsb frequency */
1336 data = 3;
1337 break;
1338 case MSR_IA32_APICBASE:
1339 data = vcpu->apic_base;
1340 break;
1341 case MSR_IA32_MISC_ENABLE:
1342 data = vcpu->ia32_misc_enable_msr;
1343 break;
1344 #ifdef CONFIG_X86_64
1345 case MSR_EFER:
1346 data = vcpu->shadow_efer;
1347 break;
1348 #endif
1349 default:
1350 printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
1351 return 1;
1353 *pdata = data;
1354 return 0;
1356 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1359 * Reads an msr value (of 'msr_index') into 'pdata'.
1360 * Returns 0 on success, non-0 otherwise.
1361 * Assumes vcpu_load() was already called.
1363 static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1365 return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
1368 #ifdef CONFIG_X86_64
1370 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
1372 if (efer & EFER_RESERVED_BITS) {
1373 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
1374 efer);
1375 inject_gp(vcpu);
1376 return;
1379 if (is_paging(vcpu)
1380 && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
1381 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
1382 inject_gp(vcpu);
1383 return;
1386 kvm_arch_ops->set_efer(vcpu, efer);
1388 efer &= ~EFER_LMA;
1389 efer |= vcpu->shadow_efer & EFER_LMA;
1391 vcpu->shadow_efer = efer;
1394 #endif
1396 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1398 switch (msr) {
1399 #ifdef CONFIG_X86_64
1400 case MSR_EFER:
1401 set_efer(vcpu, data);
1402 break;
1403 #endif
1404 case MSR_IA32_MC0_STATUS:
1405 printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
1406 __FUNCTION__, data);
1407 break;
1408 case MSR_IA32_UCODE_REV:
1409 case MSR_IA32_UCODE_WRITE:
1410 case 0x200 ... 0x2ff: /* MTRRs */
1411 break;
1412 case MSR_IA32_APICBASE:
1413 vcpu->apic_base = data;
1414 break;
1415 case MSR_IA32_MISC_ENABLE:
1416 vcpu->ia32_misc_enable_msr = data;
1417 break;
1419 * This is the 'probe whether the host is KVM' logic:
1421 case MSR_KVM_API_MAGIC:
1422 return vcpu_register_para(vcpu, data);
1424 default:
1425 printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
1426 return 1;
1428 return 0;
1430 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1433 * Writes msr value into into the appropriate "register".
1434 * Returns 0 on success, non-0 otherwise.
1435 * Assumes vcpu_load() was already called.
1437 static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1439 return kvm_arch_ops->set_msr(vcpu, msr_index, data);
1442 void kvm_resched(struct kvm_vcpu *vcpu)
1444 vcpu_put(vcpu);
1445 cond_resched();
1446 /* Cannot fail - no vcpu unplug yet. */
1447 vcpu_load(vcpu->kvm, vcpu_slot(vcpu));
1449 EXPORT_SYMBOL_GPL(kvm_resched);
1451 void load_msrs(struct vmx_msr_entry *e, int n)
1453 int i;
1455 for (i = 0; i < n; ++i)
1456 wrmsrl(e[i].index, e[i].data);
1458 EXPORT_SYMBOL_GPL(load_msrs);
1460 void save_msrs(struct vmx_msr_entry *e, int n)
1462 int i;
1464 for (i = 0; i < n; ++i)
1465 rdmsrl(e[i].index, e[i].data);
1467 EXPORT_SYMBOL_GPL(save_msrs);
1469 static int kvm_dev_ioctl_run(struct kvm *kvm, struct kvm_run *kvm_run)
1471 struct kvm_vcpu *vcpu;
1472 int r;
1474 if (!valid_vcpu(kvm_run->vcpu))
1475 return -EINVAL;
1477 vcpu = vcpu_load(kvm, kvm_run->vcpu);
1478 if (!vcpu)
1479 return -ENOENT;
1481 /* re-sync apic's tpr */
1482 vcpu->cr8 = kvm_run->cr8;
1484 if (kvm_run->emulated) {
1485 kvm_arch_ops->skip_emulated_instruction(vcpu);
1486 kvm_run->emulated = 0;
1489 if (kvm_run->mmio_completed) {
1490 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
1491 vcpu->mmio_read_completed = 1;
1494 vcpu->mmio_needed = 0;
1496 r = kvm_arch_ops->run(vcpu, kvm_run);
1498 vcpu_put(vcpu);
1499 return r;
1502 static int kvm_dev_ioctl_get_regs(struct kvm *kvm, struct kvm_regs *regs)
1504 struct kvm_vcpu *vcpu;
1506 if (!valid_vcpu(regs->vcpu))
1507 return -EINVAL;
1509 vcpu = vcpu_load(kvm, regs->vcpu);
1510 if (!vcpu)
1511 return -ENOENT;
1513 kvm_arch_ops->cache_regs(vcpu);
1515 regs->rax = vcpu->regs[VCPU_REGS_RAX];
1516 regs->rbx = vcpu->regs[VCPU_REGS_RBX];
1517 regs->rcx = vcpu->regs[VCPU_REGS_RCX];
1518 regs->rdx = vcpu->regs[VCPU_REGS_RDX];
1519 regs->rsi = vcpu->regs[VCPU_REGS_RSI];
1520 regs->rdi = vcpu->regs[VCPU_REGS_RDI];
1521 regs->rsp = vcpu->regs[VCPU_REGS_RSP];
1522 regs->rbp = vcpu->regs[VCPU_REGS_RBP];
1523 #ifdef CONFIG_X86_64
1524 regs->r8 = vcpu->regs[VCPU_REGS_R8];
1525 regs->r9 = vcpu->regs[VCPU_REGS_R9];
1526 regs->r10 = vcpu->regs[VCPU_REGS_R10];
1527 regs->r11 = vcpu->regs[VCPU_REGS_R11];
1528 regs->r12 = vcpu->regs[VCPU_REGS_R12];
1529 regs->r13 = vcpu->regs[VCPU_REGS_R13];
1530 regs->r14 = vcpu->regs[VCPU_REGS_R14];
1531 regs->r15 = vcpu->regs[VCPU_REGS_R15];
1532 #endif
1534 regs->rip = vcpu->rip;
1535 regs->rflags = kvm_arch_ops->get_rflags(vcpu);
1538 * Don't leak debug flags in case they were set for guest debugging
1540 if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
1541 regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1543 vcpu_put(vcpu);
1545 return 0;
1548 static int kvm_dev_ioctl_set_regs(struct kvm *kvm, struct kvm_regs *regs)
1550 struct kvm_vcpu *vcpu;
1552 if (!valid_vcpu(regs->vcpu))
1553 return -EINVAL;
1555 vcpu = vcpu_load(kvm, regs->vcpu);
1556 if (!vcpu)
1557 return -ENOENT;
1559 vcpu->regs[VCPU_REGS_RAX] = regs->rax;
1560 vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
1561 vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
1562 vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
1563 vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
1564 vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
1565 vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
1566 vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
1567 #ifdef CONFIG_X86_64
1568 vcpu->regs[VCPU_REGS_R8] = regs->r8;
1569 vcpu->regs[VCPU_REGS_R9] = regs->r9;
1570 vcpu->regs[VCPU_REGS_R10] = regs->r10;
1571 vcpu->regs[VCPU_REGS_R11] = regs->r11;
1572 vcpu->regs[VCPU_REGS_R12] = regs->r12;
1573 vcpu->regs[VCPU_REGS_R13] = regs->r13;
1574 vcpu->regs[VCPU_REGS_R14] = regs->r14;
1575 vcpu->regs[VCPU_REGS_R15] = regs->r15;
1576 #endif
1578 vcpu->rip = regs->rip;
1579 kvm_arch_ops->set_rflags(vcpu, regs->rflags);
1581 kvm_arch_ops->decache_regs(vcpu);
1583 vcpu_put(vcpu);
1585 return 0;
1588 static void get_segment(struct kvm_vcpu *vcpu,
1589 struct kvm_segment *var, int seg)
1591 return kvm_arch_ops->get_segment(vcpu, var, seg);
1594 static int kvm_dev_ioctl_get_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1596 struct kvm_vcpu *vcpu;
1597 struct descriptor_table dt;
1599 if (!valid_vcpu(sregs->vcpu))
1600 return -EINVAL;
1601 vcpu = vcpu_load(kvm, sregs->vcpu);
1602 if (!vcpu)
1603 return -ENOENT;
1605 get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1606 get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1607 get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1608 get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1609 get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1610 get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1612 get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1613 get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1615 kvm_arch_ops->get_idt(vcpu, &dt);
1616 sregs->idt.limit = dt.limit;
1617 sregs->idt.base = dt.base;
1618 kvm_arch_ops->get_gdt(vcpu, &dt);
1619 sregs->gdt.limit = dt.limit;
1620 sregs->gdt.base = dt.base;
1622 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1623 sregs->cr0 = vcpu->cr0;
1624 sregs->cr2 = vcpu->cr2;
1625 sregs->cr3 = vcpu->cr3;
1626 sregs->cr4 = vcpu->cr4;
1627 sregs->cr8 = vcpu->cr8;
1628 sregs->efer = vcpu->shadow_efer;
1629 sregs->apic_base = vcpu->apic_base;
1631 memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
1632 sizeof sregs->interrupt_bitmap);
1634 vcpu_put(vcpu);
1636 return 0;
1639 static void set_segment(struct kvm_vcpu *vcpu,
1640 struct kvm_segment *var, int seg)
1642 return kvm_arch_ops->set_segment(vcpu, var, seg);
1645 static int kvm_dev_ioctl_set_sregs(struct kvm *kvm, struct kvm_sregs *sregs)
1647 struct kvm_vcpu *vcpu;
1648 int mmu_reset_needed = 0;
1649 int i;
1650 struct descriptor_table dt;
1652 if (!valid_vcpu(sregs->vcpu))
1653 return -EINVAL;
1654 vcpu = vcpu_load(kvm, sregs->vcpu);
1655 if (!vcpu)
1656 return -ENOENT;
1658 set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
1659 set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
1660 set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
1661 set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
1662 set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
1663 set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
1665 set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
1666 set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
1668 dt.limit = sregs->idt.limit;
1669 dt.base = sregs->idt.base;
1670 kvm_arch_ops->set_idt(vcpu, &dt);
1671 dt.limit = sregs->gdt.limit;
1672 dt.base = sregs->gdt.base;
1673 kvm_arch_ops->set_gdt(vcpu, &dt);
1675 vcpu->cr2 = sregs->cr2;
1676 mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
1677 vcpu->cr3 = sregs->cr3;
1679 vcpu->cr8 = sregs->cr8;
1681 mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
1682 #ifdef CONFIG_X86_64
1683 kvm_arch_ops->set_efer(vcpu, sregs->efer);
1684 #endif
1685 vcpu->apic_base = sregs->apic_base;
1687 kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
1689 mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
1690 kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
1692 mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
1693 kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
1694 if (!is_long_mode(vcpu) && is_pae(vcpu))
1695 load_pdptrs(vcpu, vcpu->cr3);
1697 if (mmu_reset_needed)
1698 kvm_mmu_reset_context(vcpu);
1700 memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
1701 sizeof vcpu->irq_pending);
1702 vcpu->irq_summary = 0;
1703 for (i = 0; i < NR_IRQ_WORDS; ++i)
1704 if (vcpu->irq_pending[i])
1705 __set_bit(i, &vcpu->irq_summary);
1707 vcpu_put(vcpu);
1709 return 0;
1713 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1714 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1716 * This list is modified at module load time to reflect the
1717 * capabilities of the host cpu.
1719 static u32 msrs_to_save[] = {
1720 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
1721 MSR_K6_STAR,
1722 #ifdef CONFIG_X86_64
1723 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1724 #endif
1725 MSR_IA32_TIME_STAMP_COUNTER,
1728 static unsigned num_msrs_to_save;
1730 static u32 emulated_msrs[] = {
1731 MSR_IA32_MISC_ENABLE,
1734 static __init void kvm_init_msr_list(void)
1736 u32 dummy[2];
1737 unsigned i, j;
1739 for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
1740 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
1741 continue;
1742 if (j < i)
1743 msrs_to_save[j] = msrs_to_save[i];
1744 j++;
1746 num_msrs_to_save = j;
1750 * Adapt set_msr() to msr_io()'s calling convention
1752 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1754 return set_msr(vcpu, index, *data);
1758 * Read or write a bunch of msrs. All parameters are kernel addresses.
1760 * @return number of msrs set successfully.
1762 static int __msr_io(struct kvm *kvm, struct kvm_msrs *msrs,
1763 struct kvm_msr_entry *entries,
1764 int (*do_msr)(struct kvm_vcpu *vcpu,
1765 unsigned index, u64 *data))
1767 struct kvm_vcpu *vcpu;
1768 int i;
1770 if (!valid_vcpu(msrs->vcpu))
1771 return -EINVAL;
1773 vcpu = vcpu_load(kvm, msrs->vcpu);
1774 if (!vcpu)
1775 return -ENOENT;
1777 for (i = 0; i < msrs->nmsrs; ++i)
1778 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1779 break;
1781 vcpu_put(vcpu);
1783 return i;
1787 * Read or write a bunch of msrs. Parameters are user addresses.
1789 * @return number of msrs set successfully.
1791 static int msr_io(struct kvm *kvm, struct kvm_msrs __user *user_msrs,
1792 int (*do_msr)(struct kvm_vcpu *vcpu,
1793 unsigned index, u64 *data),
1794 int writeback)
1796 struct kvm_msrs msrs;
1797 struct kvm_msr_entry *entries;
1798 int r, n;
1799 unsigned size;
1801 r = -EFAULT;
1802 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1803 goto out;
1805 r = -E2BIG;
1806 if (msrs.nmsrs >= MAX_IO_MSRS)
1807 goto out;
1809 r = -ENOMEM;
1810 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1811 entries = vmalloc(size);
1812 if (!entries)
1813 goto out;
1815 r = -EFAULT;
1816 if (copy_from_user(entries, user_msrs->entries, size))
1817 goto out_free;
1819 r = n = __msr_io(kvm, &msrs, entries, do_msr);
1820 if (r < 0)
1821 goto out_free;
1823 r = -EFAULT;
1824 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1825 goto out_free;
1827 r = n;
1829 out_free:
1830 vfree(entries);
1831 out:
1832 return r;
1836 * Translate a guest virtual address to a guest physical address.
1838 static int kvm_dev_ioctl_translate(struct kvm *kvm, struct kvm_translation *tr)
1840 unsigned long vaddr = tr->linear_address;
1841 struct kvm_vcpu *vcpu;
1842 gpa_t gpa;
1844 vcpu = vcpu_load(kvm, tr->vcpu);
1845 if (!vcpu)
1846 return -ENOENT;
1847 spin_lock(&kvm->lock);
1848 gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
1849 tr->physical_address = gpa;
1850 tr->valid = gpa != UNMAPPED_GVA;
1851 tr->writeable = 1;
1852 tr->usermode = 0;
1853 spin_unlock(&kvm->lock);
1854 vcpu_put(vcpu);
1856 return 0;
1859 static int kvm_dev_ioctl_interrupt(struct kvm *kvm, struct kvm_interrupt *irq)
1861 struct kvm_vcpu *vcpu;
1863 if (!valid_vcpu(irq->vcpu))
1864 return -EINVAL;
1865 if (irq->irq < 0 || irq->irq >= 256)
1866 return -EINVAL;
1867 vcpu = vcpu_load(kvm, irq->vcpu);
1868 if (!vcpu)
1869 return -ENOENT;
1871 set_bit(irq->irq, vcpu->irq_pending);
1872 set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
1874 vcpu_put(vcpu);
1876 return 0;
1879 static int kvm_dev_ioctl_debug_guest(struct kvm *kvm,
1880 struct kvm_debug_guest *dbg)
1882 struct kvm_vcpu *vcpu;
1883 int r;
1885 if (!valid_vcpu(dbg->vcpu))
1886 return -EINVAL;
1887 vcpu = vcpu_load(kvm, dbg->vcpu);
1888 if (!vcpu)
1889 return -ENOENT;
1891 r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
1893 vcpu_put(vcpu);
1895 return r;
1898 static long kvm_dev_ioctl(struct file *filp,
1899 unsigned int ioctl, unsigned long arg)
1901 struct kvm *kvm = filp->private_data;
1902 int r = -EINVAL;
1904 switch (ioctl) {
1905 case KVM_GET_API_VERSION:
1906 r = KVM_API_VERSION;
1907 break;
1908 case KVM_CREATE_VCPU:
1909 r = kvm_dev_ioctl_create_vcpu(kvm, arg);
1910 if (r)
1911 goto out;
1912 break;
1913 case KVM_RUN: {
1914 struct kvm_run kvm_run;
1916 r = -EFAULT;
1917 if (copy_from_user(&kvm_run, (void *)arg, sizeof kvm_run))
1918 goto out;
1919 r = kvm_dev_ioctl_run(kvm, &kvm_run);
1920 if (r < 0 && r != -EINTR)
1921 goto out;
1922 if (copy_to_user((void *)arg, &kvm_run, sizeof kvm_run)) {
1923 r = -EFAULT;
1924 goto out;
1926 break;
1928 case KVM_GET_REGS: {
1929 struct kvm_regs kvm_regs;
1931 r = -EFAULT;
1932 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1933 goto out;
1934 r = kvm_dev_ioctl_get_regs(kvm, &kvm_regs);
1935 if (r)
1936 goto out;
1937 r = -EFAULT;
1938 if (copy_to_user((void *)arg, &kvm_regs, sizeof kvm_regs))
1939 goto out;
1940 r = 0;
1941 break;
1943 case KVM_SET_REGS: {
1944 struct kvm_regs kvm_regs;
1946 r = -EFAULT;
1947 if (copy_from_user(&kvm_regs, (void *)arg, sizeof kvm_regs))
1948 goto out;
1949 r = kvm_dev_ioctl_set_regs(kvm, &kvm_regs);
1950 if (r)
1951 goto out;
1952 r = 0;
1953 break;
1955 case KVM_GET_SREGS: {
1956 struct kvm_sregs kvm_sregs;
1958 r = -EFAULT;
1959 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1960 goto out;
1961 r = kvm_dev_ioctl_get_sregs(kvm, &kvm_sregs);
1962 if (r)
1963 goto out;
1964 r = -EFAULT;
1965 if (copy_to_user((void *)arg, &kvm_sregs, sizeof kvm_sregs))
1966 goto out;
1967 r = 0;
1968 break;
1970 case KVM_SET_SREGS: {
1971 struct kvm_sregs kvm_sregs;
1973 r = -EFAULT;
1974 if (copy_from_user(&kvm_sregs, (void *)arg, sizeof kvm_sregs))
1975 goto out;
1976 r = kvm_dev_ioctl_set_sregs(kvm, &kvm_sregs);
1977 if (r)
1978 goto out;
1979 r = 0;
1980 break;
1982 case KVM_TRANSLATE: {
1983 struct kvm_translation tr;
1985 r = -EFAULT;
1986 if (copy_from_user(&tr, (void *)arg, sizeof tr))
1987 goto out;
1988 r = kvm_dev_ioctl_translate(kvm, &tr);
1989 if (r)
1990 goto out;
1991 r = -EFAULT;
1992 if (copy_to_user((void *)arg, &tr, sizeof tr))
1993 goto out;
1994 r = 0;
1995 break;
1997 case KVM_INTERRUPT: {
1998 struct kvm_interrupt irq;
2000 r = -EFAULT;
2001 if (copy_from_user(&irq, (void *)arg, sizeof irq))
2002 goto out;
2003 r = kvm_dev_ioctl_interrupt(kvm, &irq);
2004 if (r)
2005 goto out;
2006 r = 0;
2007 break;
2009 case KVM_DEBUG_GUEST: {
2010 struct kvm_debug_guest dbg;
2012 r = -EFAULT;
2013 if (copy_from_user(&dbg, (void *)arg, sizeof dbg))
2014 goto out;
2015 r = kvm_dev_ioctl_debug_guest(kvm, &dbg);
2016 if (r)
2017 goto out;
2018 r = 0;
2019 break;
2021 case KVM_SET_MEMORY_REGION: {
2022 struct kvm_memory_region kvm_mem;
2024 r = -EFAULT;
2025 if (copy_from_user(&kvm_mem, (void *)arg, sizeof kvm_mem))
2026 goto out;
2027 r = kvm_dev_ioctl_set_memory_region(kvm, &kvm_mem);
2028 if (r)
2029 goto out;
2030 break;
2032 case KVM_GET_DIRTY_LOG: {
2033 struct kvm_dirty_log log;
2035 r = -EFAULT;
2036 if (copy_from_user(&log, (void *)arg, sizeof log))
2037 goto out;
2038 r = kvm_dev_ioctl_get_dirty_log(kvm, &log);
2039 if (r)
2040 goto out;
2041 break;
2043 case KVM_GET_MSRS:
2044 r = msr_io(kvm, (void __user *)arg, get_msr, 1);
2045 break;
2046 case KVM_SET_MSRS:
2047 r = msr_io(kvm, (void __user *)arg, do_set_msr, 0);
2048 break;
2049 case KVM_GET_MSR_INDEX_LIST: {
2050 struct kvm_msr_list __user *user_msr_list = (void __user *)arg;
2051 struct kvm_msr_list msr_list;
2052 unsigned n;
2054 r = -EFAULT;
2055 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
2056 goto out;
2057 n = msr_list.nmsrs;
2058 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
2059 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
2060 goto out;
2061 r = -E2BIG;
2062 if (n < num_msrs_to_save)
2063 goto out;
2064 r = -EFAULT;
2065 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
2066 num_msrs_to_save * sizeof(u32)))
2067 goto out;
2068 if (copy_to_user(user_msr_list->indices
2069 + num_msrs_to_save * sizeof(u32),
2070 &emulated_msrs,
2071 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
2072 goto out;
2073 r = 0;
2074 break;
2076 default:
2079 out:
2080 return r;
2083 static struct page *kvm_dev_nopage(struct vm_area_struct *vma,
2084 unsigned long address,
2085 int *type)
2087 struct kvm *kvm = vma->vm_file->private_data;
2088 unsigned long pgoff;
2089 struct kvm_memory_slot *slot;
2090 struct page *page;
2092 *type = VM_FAULT_MINOR;
2093 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2094 slot = gfn_to_memslot(kvm, pgoff);
2095 if (!slot)
2096 return NOPAGE_SIGBUS;
2097 page = gfn_to_page(slot, pgoff);
2098 if (!page)
2099 return NOPAGE_SIGBUS;
2100 get_page(page);
2101 return page;
2104 static struct vm_operations_struct kvm_dev_vm_ops = {
2105 .nopage = kvm_dev_nopage,
2108 static int kvm_dev_mmap(struct file *file, struct vm_area_struct *vma)
2110 vma->vm_ops = &kvm_dev_vm_ops;
2111 return 0;
2114 static struct file_operations kvm_chardev_ops = {
2115 .open = kvm_dev_open,
2116 .release = kvm_dev_release,
2117 .unlocked_ioctl = kvm_dev_ioctl,
2118 .compat_ioctl = kvm_dev_ioctl,
2119 .mmap = kvm_dev_mmap,
2122 static struct miscdevice kvm_dev = {
2123 MISC_DYNAMIC_MINOR,
2124 "kvm",
2125 &kvm_chardev_ops,
2128 static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
2129 void *v)
2131 if (val == SYS_RESTART) {
2133 * Some (well, at least mine) BIOSes hang on reboot if
2134 * in vmx root mode.
2136 printk(KERN_INFO "kvm: exiting hardware virtualization\n");
2137 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2139 return NOTIFY_OK;
2142 static struct notifier_block kvm_reboot_notifier = {
2143 .notifier_call = kvm_reboot,
2144 .priority = 0,
2148 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
2149 * cached on it.
2151 static void decache_vcpus_on_cpu(int cpu)
2153 struct kvm *vm;
2154 struct kvm_vcpu *vcpu;
2155 int i;
2157 spin_lock(&kvm_lock);
2158 list_for_each_entry(vm, &vm_list, vm_list)
2159 for (i = 0; i < KVM_MAX_VCPUS; ++i) {
2160 vcpu = &vm->vcpus[i];
2162 * If the vcpu is locked, then it is running on some
2163 * other cpu and therefore it is not cached on the
2164 * cpu in question.
2166 * If it's not locked, check the last cpu it executed
2167 * on.
2169 if (mutex_trylock(&vcpu->mutex)) {
2170 if (vcpu->cpu == cpu) {
2171 kvm_arch_ops->vcpu_decache(vcpu);
2172 vcpu->cpu = -1;
2174 mutex_unlock(&vcpu->mutex);
2177 spin_unlock(&kvm_lock);
2180 static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
2181 void *v)
2183 int cpu = (long)v;
2185 switch (val) {
2186 case CPU_DOWN_PREPARE:
2187 case CPU_UP_CANCELED:
2188 printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
2189 cpu);
2190 decache_vcpus_on_cpu(cpu);
2191 smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
2192 NULL, 0, 1);
2193 break;
2194 case CPU_ONLINE:
2195 printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
2196 cpu);
2197 smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
2198 NULL, 0, 1);
2199 break;
2201 return NOTIFY_OK;
2204 static struct notifier_block kvm_cpu_notifier = {
2205 .notifier_call = kvm_cpu_hotplug,
2206 .priority = 20, /* must be > scheduler priority */
2209 static __init void kvm_init_debug(void)
2211 struct kvm_stats_debugfs_item *p;
2213 debugfs_dir = debugfs_create_dir("kvm", 0);
2214 for (p = debugfs_entries; p->name; ++p)
2215 p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
2216 p->data);
2219 static void kvm_exit_debug(void)
2221 struct kvm_stats_debugfs_item *p;
2223 for (p = debugfs_entries; p->name; ++p)
2224 debugfs_remove(p->dentry);
2225 debugfs_remove(debugfs_dir);
2228 static int kvm_suspend(struct sys_device *dev, pm_message_t state)
2230 decache_vcpus_on_cpu(raw_smp_processor_id());
2231 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2232 return 0;
2235 static int kvm_resume(struct sys_device *dev)
2237 on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1);
2238 return 0;
2241 static struct sysdev_class kvm_sysdev_class = {
2242 set_kset_name("kvm"),
2243 .suspend = kvm_suspend,
2244 .resume = kvm_resume,
2247 static struct sys_device kvm_sysdev = {
2248 .id = 0,
2249 .cls = &kvm_sysdev_class,
2252 hpa_t bad_page_address;
2254 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
2256 int r;
2258 if (kvm_arch_ops) {
2259 printk(KERN_ERR "kvm: already loaded the other module\n");
2260 return -EEXIST;
2263 if (!ops->cpu_has_kvm_support()) {
2264 printk(KERN_ERR "kvm: no hardware support\n");
2265 return -EOPNOTSUPP;
2267 if (ops->disabled_by_bios()) {
2268 printk(KERN_ERR "kvm: disabled by bios\n");
2269 return -EOPNOTSUPP;
2272 kvm_arch_ops = ops;
2274 r = kvm_arch_ops->hardware_setup();
2275 if (r < 0)
2276 return r;
2278 on_each_cpu(kvm_arch_ops->hardware_enable, 0, 0, 1);
2279 r = register_cpu_notifier(&kvm_cpu_notifier);
2280 if (r)
2281 goto out_free_1;
2282 register_reboot_notifier(&kvm_reboot_notifier);
2284 r = sysdev_class_register(&kvm_sysdev_class);
2285 if (r)
2286 goto out_free_2;
2288 r = sysdev_register(&kvm_sysdev);
2289 if (r)
2290 goto out_free_3;
2292 kvm_chardev_ops.owner = module;
2294 r = misc_register(&kvm_dev);
2295 if (r) {
2296 printk (KERN_ERR "kvm: misc device register failed\n");
2297 goto out_free;
2300 return r;
2302 out_free:
2303 sysdev_unregister(&kvm_sysdev);
2304 out_free_3:
2305 sysdev_class_unregister(&kvm_sysdev_class);
2306 out_free_2:
2307 unregister_reboot_notifier(&kvm_reboot_notifier);
2308 unregister_cpu_notifier(&kvm_cpu_notifier);
2309 out_free_1:
2310 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2311 kvm_arch_ops->hardware_unsetup();
2312 return r;
2315 void kvm_exit_arch(void)
2317 misc_deregister(&kvm_dev);
2318 sysdev_unregister(&kvm_sysdev);
2319 sysdev_class_unregister(&kvm_sysdev_class);
2320 unregister_reboot_notifier(&kvm_reboot_notifier);
2321 unregister_cpu_notifier(&kvm_cpu_notifier);
2322 on_each_cpu(kvm_arch_ops->hardware_disable, 0, 0, 1);
2323 kvm_arch_ops->hardware_unsetup();
2324 kvm_arch_ops = NULL;
2327 static __init int kvm_init(void)
2329 static struct page *bad_page;
2330 int r = 0;
2332 kvm_init_debug();
2334 kvm_init_msr_list();
2336 if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
2337 r = -ENOMEM;
2338 goto out;
2341 bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
2342 memset(__va(bad_page_address), 0, PAGE_SIZE);
2344 return r;
2346 out:
2347 kvm_exit_debug();
2348 return r;
2351 static __exit void kvm_exit(void)
2353 kvm_exit_debug();
2354 __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
2357 module_init(kvm_init)
2358 module_exit(kvm_exit)
2360 EXPORT_SYMBOL_GPL(kvm_init_arch);
2361 EXPORT_SYMBOL_GPL(kvm_exit_arch);