Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 18 | #include "iodev.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 20 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | #include <linux/kvm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/percpu.h> |
| 25 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 33 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 34 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 36 | #include <linux/cpumask.h> |
| 37 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 38 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 39 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 40 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 41 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 42 | #include <linux/mman.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 43 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 44 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 45 | #include <asm/io.h> |
| 46 | #include <asm/uaccess.h> |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 47 | #include <asm/pgtable.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 48 | |
| 49 | MODULE_AUTHOR("Qumranet"); |
| 50 | MODULE_LICENSE("GPL"); |
| 51 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 52 | DEFINE_SPINLOCK(kvm_lock); |
| 53 | LIST_HEAD(vm_list); |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 54 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 55 | static cpumask_t cpus_hardware_enabled; |
| 56 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 57 | struct kmem_cache *kvm_vcpu_cache; |
| 58 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 59 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 60 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 61 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 62 | static struct dentry *debugfs_dir; |
| 63 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 64 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 65 | unsigned long arg); |
| 66 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 67 | static inline int valid_vcpu(int n) |
| 68 | { |
| 69 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 70 | } |
| 71 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | /* |
| 73 | * Switches to specified vcpu, until a matching vcpu_put() |
| 74 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 75 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 76 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 77 | int cpu; |
| 78 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 79 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 80 | cpu = get_cpu(); |
| 81 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 82 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 83 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 86 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 87 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 88 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 89 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 90 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 91 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 92 | mutex_unlock(&vcpu->mutex); |
| 93 | } |
| 94 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 95 | static void ack_flush(void *_completed) |
| 96 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 100 | { |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 101 | int i, cpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 102 | cpumask_t cpus; |
| 103 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 104 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 105 | cpus_clear(cpus); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 106 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 107 | vcpu = kvm->vcpus[i]; |
| 108 | if (!vcpu) |
| 109 | continue; |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 110 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 111 | continue; |
| 112 | cpu = vcpu->cpu; |
| 113 | if (cpu != -1 && cpu != raw_smp_processor_id()) |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 114 | cpu_set(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 115 | } |
Avi Kivity | 0f74a24 | 2007-11-20 23:01:14 +0200 | [diff] [blame] | 116 | if (cpus_empty(cpus)) |
| 117 | return; |
| 118 | ++kvm->stat.remote_tlb_flush; |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 119 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 120 | } |
| 121 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 122 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 123 | { |
| 124 | struct page *page; |
| 125 | int r; |
| 126 | |
| 127 | mutex_init(&vcpu->mutex); |
| 128 | vcpu->cpu = -1; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 129 | vcpu->kvm = kvm; |
| 130 | vcpu->vcpu_id = id; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 131 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 132 | |
| 133 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 134 | if (!page) { |
| 135 | r = -ENOMEM; |
| 136 | goto fail; |
| 137 | } |
| 138 | vcpu->run = page_address(page); |
| 139 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 140 | r = kvm_arch_vcpu_init(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 141 | if (r < 0) |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 142 | goto fail_free_run; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 143 | return 0; |
| 144 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 145 | fail_free_run: |
| 146 | free_page((unsigned long)vcpu->run); |
| 147 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 148 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 149 | } |
| 150 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 151 | |
| 152 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 153 | { |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 154 | kvm_arch_vcpu_uninit(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 155 | free_page((unsigned long)vcpu->run); |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 158 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 159 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 160 | { |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 161 | struct kvm *kvm = kvm_arch_create_vm(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 162 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 163 | if (IS_ERR(kvm)) |
| 164 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 165 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 166 | kvm->mm = current->mm; |
| 167 | atomic_inc(&kvm->mm->mm_count); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 168 | spin_lock_init(&kvm->mmu_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 169 | kvm_io_bus_init(&kvm->pio_bus); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 170 | mutex_init(&kvm->lock); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 171 | kvm_io_bus_init(&kvm->mmio_bus); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 172 | init_rwsem(&kvm->slots_lock); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 173 | spin_lock(&kvm_lock); |
| 174 | list_add(&kvm->vm_list, &vm_list); |
| 175 | spin_unlock(&kvm_lock); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 176 | out: |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 177 | return kvm; |
| 178 | } |
| 179 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 180 | /* |
| 181 | * Free any memory in @free but not in @dont. |
| 182 | */ |
| 183 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 184 | struct kvm_memory_slot *dont) |
| 185 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 186 | if (!dont || free->rmap != dont->rmap) |
| 187 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 188 | |
| 189 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 190 | vfree(free->dirty_bitmap); |
| 191 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 192 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 193 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 194 | free->rmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 197 | void kvm_free_physmem(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 198 | { |
| 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 202 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 205 | static void kvm_destroy_vm(struct kvm *kvm) |
| 206 | { |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 207 | struct mm_struct *mm = kvm->mm; |
| 208 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 209 | spin_lock(&kvm_lock); |
| 210 | list_del(&kvm->vm_list); |
| 211 | spin_unlock(&kvm_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 212 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 213 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Zhang Xiantao | d19a9cd | 2007-11-18 18:43:45 +0800 | [diff] [blame] | 214 | kvm_arch_destroy_vm(kvm); |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 215 | mmdrop(mm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 219 | { |
| 220 | struct kvm *kvm = filp->private_data; |
| 221 | |
| 222 | kvm_destroy_vm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 226 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 227 | * Allocate some memory and give it an address in the guest physical address |
| 228 | * space. |
| 229 | * |
| 230 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 231 | * |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 232 | * Must be called holding mmap_sem for write. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 233 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 234 | int __kvm_set_memory_region(struct kvm *kvm, |
| 235 | struct kvm_userspace_memory_region *mem, |
| 236 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 237 | { |
| 238 | int r; |
| 239 | gfn_t base_gfn; |
| 240 | unsigned long npages; |
| 241 | unsigned long i; |
| 242 | struct kvm_memory_slot *memslot; |
| 243 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | |
| 245 | r = -EINVAL; |
| 246 | /* General sanity checks */ |
| 247 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 248 | goto out; |
| 249 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 250 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 251 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 252 | goto out; |
| 253 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 254 | goto out; |
| 255 | |
| 256 | memslot = &kvm->memslots[mem->slot]; |
| 257 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 258 | npages = mem->memory_size >> PAGE_SHIFT; |
| 259 | |
| 260 | if (!npages) |
| 261 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 262 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 263 | new = old = *memslot; |
| 264 | |
| 265 | new.base_gfn = base_gfn; |
| 266 | new.npages = npages; |
| 267 | new.flags = mem->flags; |
| 268 | |
| 269 | /* Disallow changing a memory slot's size. */ |
| 270 | r = -EINVAL; |
| 271 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 272 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 273 | |
| 274 | /* Check for overlaps */ |
| 275 | r = -EEXIST; |
| 276 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 277 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 278 | |
| 279 | if (s == memslot) |
| 280 | continue; |
| 281 | if (!((base_gfn + npages <= s->base_gfn) || |
| 282 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 283 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 284 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 285 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 286 | /* Free page dirty bitmap if unneeded */ |
| 287 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 288 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 289 | |
| 290 | r = -ENOMEM; |
| 291 | |
| 292 | /* Allocate if a slot is being created */ |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 293 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 294 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 295 | |
| 296 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 297 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 298 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 299 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 300 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 301 | new.user_alloc = user_alloc; |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 302 | new.userspace_addr = mem->userspace_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Allocate page dirty bitmap if needed */ |
| 306 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 307 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 308 | |
| 309 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 310 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 311 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 312 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 313 | } |
| 314 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 315 | if (mem->slot >= kvm->nmemslots) |
| 316 | kvm->nmemslots = mem->slot + 1; |
| 317 | |
| 318 | *memslot = new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 319 | |
Zhang Xiantao | 0de1034 | 2007-11-20 16:25:04 +0800 | [diff] [blame] | 320 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
| 321 | if (r) { |
| 322 | *memslot = old; |
| 323 | goto out_free; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 324 | } |
| 325 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | kvm_free_physmem_slot(&old, &new); |
| 327 | return 0; |
| 328 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 329 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 330 | kvm_free_physmem_slot(&new, &old); |
| 331 | out: |
| 332 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 333 | |
| 334 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 335 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 336 | |
| 337 | int kvm_set_memory_region(struct kvm *kvm, |
| 338 | struct kvm_userspace_memory_region *mem, |
| 339 | int user_alloc) |
| 340 | { |
| 341 | int r; |
| 342 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 343 | down_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 344 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 345 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 346 | return r; |
| 347 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 349 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 350 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 351 | struct |
| 352 | kvm_userspace_memory_region *mem, |
| 353 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 354 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 355 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 356 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 357 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 360 | int kvm_get_dirty_log(struct kvm *kvm, |
| 361 | struct kvm_dirty_log *log, int *is_dirty) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 362 | { |
| 363 | struct kvm_memory_slot *memslot; |
| 364 | int r, i; |
| 365 | int n; |
| 366 | unsigned long any = 0; |
| 367 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 368 | r = -EINVAL; |
| 369 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 370 | goto out; |
| 371 | |
| 372 | memslot = &kvm->memslots[log->slot]; |
| 373 | r = -ENOENT; |
| 374 | if (!memslot->dirty_bitmap) |
| 375 | goto out; |
| 376 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 377 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 378 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 379 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 380 | any = memslot->dirty_bitmap[i]; |
| 381 | |
| 382 | r = -EFAULT; |
| 383 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 384 | goto out; |
| 385 | |
Zhang Xiantao | 5bb064d | 2007-11-18 20:29:43 +0800 | [diff] [blame] | 386 | if (any) |
| 387 | *is_dirty = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 388 | |
| 389 | r = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 390 | out: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 391 | return r; |
| 392 | } |
| 393 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 394 | int is_error_page(struct page *page) |
| 395 | { |
| 396 | return page == bad_page; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(is_error_page); |
| 399 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 400 | static inline unsigned long bad_hva(void) |
| 401 | { |
| 402 | return PAGE_OFFSET; |
| 403 | } |
| 404 | |
| 405 | int kvm_is_error_hva(unsigned long addr) |
| 406 | { |
| 407 | return addr == bad_hva(); |
| 408 | } |
| 409 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 410 | |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 411 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 412 | { |
| 413 | int i; |
| 414 | |
| 415 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 416 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 417 | |
| 418 | if (gfn >= memslot->base_gfn |
| 419 | && gfn < memslot->base_gfn + memslot->npages) |
| 420 | return memslot; |
| 421 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 422 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 423 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 424 | |
| 425 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 426 | { |
| 427 | gfn = unalias_gfn(kvm, gfn); |
| 428 | return __gfn_to_memslot(kvm, gfn); |
| 429 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 430 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 431 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 432 | { |
| 433 | int i; |
| 434 | |
| 435 | gfn = unalias_gfn(kvm, gfn); |
| 436 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 437 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 438 | |
| 439 | if (gfn >= memslot->base_gfn |
| 440 | && gfn < memslot->base_gfn + memslot->npages) |
| 441 | return 1; |
| 442 | } |
| 443 | return 0; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 446 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 447 | static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
| 448 | { |
| 449 | struct kvm_memory_slot *slot; |
| 450 | |
| 451 | gfn = unalias_gfn(kvm, gfn); |
| 452 | slot = __gfn_to_memslot(kvm, gfn); |
| 453 | if (!slot) |
| 454 | return bad_hva(); |
| 455 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); |
| 456 | } |
| 457 | |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 458 | /* |
| 459 | * Requires current->mm->mmap_sem to be held |
| 460 | */ |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 461 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 462 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 463 | struct page *page[1]; |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 464 | unsigned long addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 465 | int npages; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 466 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 467 | might_sleep(); |
| 468 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 469 | addr = gfn_to_hva(kvm, gfn); |
| 470 | if (kvm_is_error_hva(addr)) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 471 | get_page(bad_page); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 472 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 473 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 474 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 475 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
| 476 | NULL); |
| 477 | |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 478 | if (npages != 1) { |
| 479 | get_page(bad_page); |
| 480 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 481 | } |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 482 | |
| 483 | return page[0]; |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 484 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 485 | |
Avi Kivity | 954bbbc | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 486 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 487 | |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 488 | void kvm_release_page_clean(struct page *page) |
| 489 | { |
| 490 | put_page(page); |
| 491 | } |
| 492 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); |
| 493 | |
| 494 | void kvm_release_page_dirty(struct page *page) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 495 | { |
| 496 | if (!PageReserved(page)) |
| 497 | SetPageDirty(page); |
| 498 | put_page(page); |
| 499 | } |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 501 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 502 | static int next_segment(unsigned long len, int offset) |
| 503 | { |
| 504 | if (len > PAGE_SIZE - offset) |
| 505 | return PAGE_SIZE - offset; |
| 506 | else |
| 507 | return len; |
| 508 | } |
| 509 | |
| 510 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 511 | int len) |
| 512 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 513 | int r; |
| 514 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 515 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 516 | addr = gfn_to_hva(kvm, gfn); |
| 517 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 518 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 519 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 520 | if (r) |
| 521 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 525 | |
| 526 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 527 | { |
| 528 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 529 | int seg; |
| 530 | int offset = offset_in_page(gpa); |
| 531 | int ret; |
| 532 | |
| 533 | while ((seg = next_segment(len, offset)) != 0) { |
| 534 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 535 | if (ret < 0) |
| 536 | return ret; |
| 537 | offset = 0; |
| 538 | len -= seg; |
| 539 | data += seg; |
| 540 | ++gfn; |
| 541 | } |
| 542 | return 0; |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 545 | |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 546 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
| 547 | unsigned long len) |
| 548 | { |
| 549 | int r; |
| 550 | unsigned long addr; |
| 551 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 552 | int offset = offset_in_page(gpa); |
| 553 | |
| 554 | addr = gfn_to_hva(kvm, gfn); |
| 555 | if (kvm_is_error_hva(addr)) |
| 556 | return -EFAULT; |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 557 | pagefault_disable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 558 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
Andrea Arcangeli | 0aac03f | 2008-01-30 19:57:35 +0100 | [diff] [blame] | 559 | pagefault_enable(); |
Marcelo Tosatti | 7ec5458 | 2007-12-20 19:18:23 -0500 | [diff] [blame] | 560 | if (r) |
| 561 | return -EFAULT; |
| 562 | return 0; |
| 563 | } |
| 564 | EXPORT_SYMBOL(kvm_read_guest_atomic); |
| 565 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 566 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 567 | int offset, int len) |
| 568 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 569 | int r; |
| 570 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 571 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 572 | addr = gfn_to_hva(kvm, gfn); |
| 573 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 574 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 575 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 576 | if (r) |
| 577 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 578 | mark_page_dirty(kvm, gfn); |
| 579 | return 0; |
| 580 | } |
| 581 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 582 | |
| 583 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 584 | unsigned long len) |
| 585 | { |
| 586 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 587 | int seg; |
| 588 | int offset = offset_in_page(gpa); |
| 589 | int ret; |
| 590 | |
| 591 | while ((seg = next_segment(len, offset)) != 0) { |
| 592 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | offset = 0; |
| 596 | len -= seg; |
| 597 | data += seg; |
| 598 | ++gfn; |
| 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 604 | { |
Izik Eidus | 3e021bf | 2007-11-19 11:16:57 +0200 | [diff] [blame] | 605 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 606 | } |
| 607 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 608 | |
| 609 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 610 | { |
| 611 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 612 | int seg; |
| 613 | int offset = offset_in_page(gpa); |
| 614 | int ret; |
| 615 | |
| 616 | while ((seg = next_segment(len, offset)) != 0) { |
| 617 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 618 | if (ret < 0) |
| 619 | return ret; |
| 620 | offset = 0; |
| 621 | len -= seg; |
| 622 | ++gfn; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 629 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 630 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 631 | |
Uri Lublin | 3b6fff1 | 2007-10-30 10:42:09 +0200 | [diff] [blame] | 632 | gfn = unalias_gfn(kvm, gfn); |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 633 | memslot = __gfn_to_memslot(kvm, gfn); |
| 634 | if (memslot && memslot->dirty_bitmap) { |
| 635 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 636 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 637 | /* avoid RMW */ |
| 638 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 639 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 643 | /* |
| 644 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 645 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 646 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 647 | { |
| 648 | DECLARE_WAITQUEUE(wait, current); |
| 649 | |
| 650 | add_wait_queue(&vcpu->wq, &wait); |
| 651 | |
| 652 | /* |
| 653 | * We will block until either an interrupt or a signal wakes us up |
| 654 | */ |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 655 | while (!kvm_cpu_has_interrupt(vcpu) |
| 656 | && !signal_pending(current) |
Hollis Blanchard | 53e0aa7 | 2007-12-03 16:15:26 -0600 | [diff] [blame] | 657 | && !kvm_arch_vcpu_runnable(vcpu)) { |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 658 | set_current_state(TASK_INTERRUPTIBLE); |
| 659 | vcpu_put(vcpu); |
| 660 | schedule(); |
| 661 | vcpu_load(vcpu); |
| 662 | } |
| 663 | |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 664 | __set_current_state(TASK_RUNNING); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 665 | remove_wait_queue(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 666 | } |
| 667 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 668 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 669 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 670 | if (!need_resched()) |
| 671 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 672 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 673 | } |
| 674 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 675 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 676 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 677 | { |
| 678 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 679 | struct page *page; |
| 680 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 681 | if (vmf->pgoff == 0) |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 682 | page = virt_to_page(vcpu->run); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 683 | #ifdef CONFIG_X86 |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 684 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 685 | page = virt_to_page(vcpu->arch.pio_data); |
Avi Kivity | 0956676 | 2008-01-23 18:14:23 +0200 | [diff] [blame] | 686 | #endif |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 687 | else |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 688 | return VM_FAULT_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 689 | get_page(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 690 | vmf->page = page; |
| 691 | return 0; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 695 | .fault = kvm_vcpu_fault, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 699 | { |
| 700 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 701 | return 0; |
| 702 | } |
| 703 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 704 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 705 | { |
| 706 | struct kvm_vcpu *vcpu = filp->private_data; |
| 707 | |
| 708 | fput(vcpu->kvm->filp); |
| 709 | return 0; |
| 710 | } |
| 711 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 712 | static const struct file_operations kvm_vcpu_fops = { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 713 | .release = kvm_vcpu_release, |
| 714 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 715 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 716 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | /* |
| 720 | * Allocates an inode for the vcpu. |
| 721 | */ |
| 722 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 723 | { |
| 724 | int fd, r; |
| 725 | struct inode *inode; |
| 726 | struct file *file; |
| 727 | |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 728 | r = anon_inode_getfd(&fd, &inode, &file, |
| 729 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); |
| 730 | if (r) |
| 731 | return r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 732 | atomic_inc(&vcpu->kvm->filp->f_count); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 733 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 736 | /* |
| 737 | * Creates some virtual cpus. Good luck creating more than one. |
| 738 | */ |
| 739 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 740 | { |
| 741 | int r; |
| 742 | struct kvm_vcpu *vcpu; |
| 743 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 744 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 745 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 746 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 747 | vcpu = kvm_arch_vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 748 | if (IS_ERR(vcpu)) |
| 749 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 750 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 751 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 752 | |
Avi Kivity | 26e5215 | 2007-11-20 15:30:24 +0200 | [diff] [blame] | 753 | r = kvm_arch_vcpu_setup(vcpu); |
| 754 | if (r) |
| 755 | goto vcpu_destroy; |
| 756 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 757 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 758 | if (kvm->vcpus[n]) { |
| 759 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 760 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 761 | goto vcpu_destroy; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 762 | } |
| 763 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 764 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 765 | |
| 766 | /* Now it's all set up, let userspace reach it */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 767 | r = create_vcpu_fd(vcpu); |
| 768 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 769 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 770 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 771 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 772 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 773 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 774 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 775 | mutex_unlock(&kvm->lock); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 776 | vcpu_destroy: |
Hollis Blanchard | d40ccc6 | 2007-11-19 14:04:43 -0600 | [diff] [blame] | 777 | kvm_arch_vcpu_destroy(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 778 | return r; |
| 779 | } |
| 780 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 781 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 782 | { |
| 783 | if (sigset) { |
| 784 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 785 | vcpu->sigset_active = 1; |
| 786 | vcpu->sigset = *sigset; |
| 787 | } else |
| 788 | vcpu->sigset_active = 0; |
| 789 | return 0; |
| 790 | } |
| 791 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 792 | static long kvm_vcpu_ioctl(struct file *filp, |
| 793 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 794 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 795 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 796 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 797 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 798 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 799 | if (vcpu->kvm->mm != current->mm) |
| 800 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 801 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 802 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 803 | r = -EINVAL; |
| 804 | if (arg) |
| 805 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 806 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 807 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 808 | case KVM_GET_REGS: { |
| 809 | struct kvm_regs kvm_regs; |
| 810 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 811 | memset(&kvm_regs, 0, sizeof kvm_regs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 812 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 813 | if (r) |
| 814 | goto out; |
| 815 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 816 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 817 | goto out; |
| 818 | r = 0; |
| 819 | break; |
| 820 | } |
| 821 | case KVM_SET_REGS: { |
| 822 | struct kvm_regs kvm_regs; |
| 823 | |
| 824 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 825 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 826 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 827 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 828 | if (r) |
| 829 | goto out; |
| 830 | r = 0; |
| 831 | break; |
| 832 | } |
| 833 | case KVM_GET_SREGS: { |
| 834 | struct kvm_sregs kvm_sregs; |
| 835 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 836 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 837 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 838 | if (r) |
| 839 | goto out; |
| 840 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 841 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 842 | goto out; |
| 843 | r = 0; |
| 844 | break; |
| 845 | } |
| 846 | case KVM_SET_SREGS: { |
| 847 | struct kvm_sregs kvm_sregs; |
| 848 | |
| 849 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 850 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 851 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 852 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 853 | if (r) |
| 854 | goto out; |
| 855 | r = 0; |
| 856 | break; |
| 857 | } |
| 858 | case KVM_TRANSLATE: { |
| 859 | struct kvm_translation tr; |
| 860 | |
| 861 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 862 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 863 | goto out; |
Zhang Xiantao | 8b00679 | 2007-11-16 13:05:55 +0800 | [diff] [blame] | 864 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 865 | if (r) |
| 866 | goto out; |
| 867 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 868 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 869 | goto out; |
| 870 | r = 0; |
| 871 | break; |
| 872 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 873 | case KVM_DEBUG_GUEST: { |
| 874 | struct kvm_debug_guest dbg; |
| 875 | |
| 876 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 877 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 878 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 879 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 880 | if (r) |
| 881 | goto out; |
| 882 | r = 0; |
| 883 | break; |
| 884 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 885 | case KVM_SET_SIGNAL_MASK: { |
| 886 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 887 | struct kvm_signal_mask kvm_sigmask; |
| 888 | sigset_t sigset, *p; |
| 889 | |
| 890 | p = NULL; |
| 891 | if (argp) { |
| 892 | r = -EFAULT; |
| 893 | if (copy_from_user(&kvm_sigmask, argp, |
| 894 | sizeof kvm_sigmask)) |
| 895 | goto out; |
| 896 | r = -EINVAL; |
| 897 | if (kvm_sigmask.len != sizeof sigset) |
| 898 | goto out; |
| 899 | r = -EFAULT; |
| 900 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 901 | sizeof sigset)) |
| 902 | goto out; |
| 903 | p = &sigset; |
| 904 | } |
| 905 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 906 | break; |
| 907 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 908 | case KVM_GET_FPU: { |
| 909 | struct kvm_fpu fpu; |
| 910 | |
| 911 | memset(&fpu, 0, sizeof fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 912 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 913 | if (r) |
| 914 | goto out; |
| 915 | r = -EFAULT; |
| 916 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 917 | goto out; |
| 918 | r = 0; |
| 919 | break; |
| 920 | } |
| 921 | case KVM_SET_FPU: { |
| 922 | struct kvm_fpu fpu; |
| 923 | |
| 924 | r = -EFAULT; |
| 925 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 926 | goto out; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 927 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 928 | if (r) |
| 929 | goto out; |
| 930 | r = 0; |
| 931 | break; |
| 932 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 933 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 934 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 935 | } |
| 936 | out: |
| 937 | return r; |
| 938 | } |
| 939 | |
| 940 | static long kvm_vm_ioctl(struct file *filp, |
| 941 | unsigned int ioctl, unsigned long arg) |
| 942 | { |
| 943 | struct kvm *kvm = filp->private_data; |
| 944 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 945 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 946 | |
Avi Kivity | 6d4e4c4 | 2007-11-21 16:41:05 +0200 | [diff] [blame] | 947 | if (kvm->mm != current->mm) |
| 948 | return -EIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 949 | switch (ioctl) { |
| 950 | case KVM_CREATE_VCPU: |
| 951 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 952 | if (r < 0) |
| 953 | goto out; |
| 954 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 955 | case KVM_SET_USER_MEMORY_REGION: { |
| 956 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 957 | |
| 958 | r = -EFAULT; |
| 959 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 960 | sizeof kvm_userspace_mem)) |
| 961 | goto out; |
| 962 | |
| 963 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | if (r) |
| 965 | goto out; |
| 966 | break; |
| 967 | } |
| 968 | case KVM_GET_DIRTY_LOG: { |
| 969 | struct kvm_dirty_log log; |
| 970 | |
| 971 | r = -EFAULT; |
Al Viro | 2f366987 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 972 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 973 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 974 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | if (r) |
| 976 | goto out; |
| 977 | break; |
| 978 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 979 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 980 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 981 | } |
| 982 | out: |
| 983 | return r; |
| 984 | } |
| 985 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 986 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 987 | { |
| 988 | struct kvm *kvm = vma->vm_file->private_data; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 989 | struct page *page; |
| 990 | |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 991 | if (!kvm_is_visible_gfn(kvm, vmf->pgoff)) |
| 992 | return VM_FAULT_SIGBUS; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 993 | page = gfn_to_page(kvm, vmf->pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 994 | if (is_error_page(page)) { |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 995 | kvm_release_page_clean(page); |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 996 | return VM_FAULT_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 997 | } |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 998 | vmf->page = page; |
| 999 | return 0; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static struct vm_operations_struct kvm_vm_vm_ops = { |
npiggin@suse.de | e4a533a | 2007-12-05 18:15:52 +1100 | [diff] [blame] | 1003 | .fault = kvm_vm_fault, |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1004 | }; |
| 1005 | |
| 1006 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1007 | { |
| 1008 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Jan Engelhardt | 5c50274 | 2008-01-22 20:46:14 +0100 | [diff] [blame] | 1012 | static const struct file_operations kvm_vm_fops = { |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1013 | .release = kvm_vm_release, |
| 1014 | .unlocked_ioctl = kvm_vm_ioctl, |
| 1015 | .compat_ioctl = kvm_vm_ioctl, |
| 1016 | .mmap = kvm_vm_mmap, |
| 1017 | }; |
| 1018 | |
| 1019 | static int kvm_dev_ioctl_create_vm(void) |
| 1020 | { |
| 1021 | int fd, r; |
| 1022 | struct inode *inode; |
| 1023 | struct file *file; |
| 1024 | struct kvm *kvm; |
| 1025 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1026 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1027 | if (IS_ERR(kvm)) |
| 1028 | return PTR_ERR(kvm); |
| 1029 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); |
| 1030 | if (r) { |
| 1031 | kvm_destroy_vm(kvm); |
| 1032 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1035 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1036 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1037 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | static long kvm_dev_ioctl(struct file *filp, |
| 1041 | unsigned int ioctl, unsigned long arg) |
| 1042 | { |
| 1043 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1044 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1045 | |
| 1046 | switch (ioctl) { |
| 1047 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1048 | r = -EINVAL; |
| 1049 | if (arg) |
| 1050 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1051 | r = KVM_API_VERSION; |
| 1052 | break; |
| 1053 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1054 | r = -EINVAL; |
| 1055 | if (arg) |
| 1056 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1057 | r = kvm_dev_ioctl_create_vm(); |
| 1058 | break; |
Zhang Xiantao | 018d00d | 2007-11-15 23:07:47 +0800 | [diff] [blame] | 1059 | case KVM_CHECK_EXTENSION: |
| 1060 | r = kvm_dev_ioctl_check_extension((long)argp); |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 1061 | break; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1062 | case KVM_GET_VCPU_MMAP_SIZE: |
| 1063 | r = -EINVAL; |
| 1064 | if (arg) |
| 1065 | goto out; |
Avi Kivity | adb1ff4 | 2008-01-24 15:13:08 +0200 | [diff] [blame] | 1066 | r = PAGE_SIZE; /* struct kvm_run */ |
| 1067 | #ifdef CONFIG_X86 |
| 1068 | r += PAGE_SIZE; /* pio data page */ |
| 1069 | #endif |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1070 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1071 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1072 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1073 | } |
| 1074 | out: |
| 1075 | return r; |
| 1076 | } |
| 1077 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1079 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1080 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1081 | }; |
| 1082 | |
| 1083 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 1084 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1085 | "kvm", |
| 1086 | &kvm_chardev_ops, |
| 1087 | }; |
| 1088 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1089 | static void hardware_enable(void *junk) |
| 1090 | { |
| 1091 | int cpu = raw_smp_processor_id(); |
| 1092 | |
| 1093 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 1094 | return; |
| 1095 | cpu_set(cpu, cpus_hardware_enabled); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1096 | kvm_arch_hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static void hardware_disable(void *junk) |
| 1100 | { |
| 1101 | int cpu = raw_smp_processor_id(); |
| 1102 | |
| 1103 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 1104 | return; |
| 1105 | cpu_clear(cpu, cpus_hardware_enabled); |
| 1106 | decache_vcpus_on_cpu(cpu); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1107 | kvm_arch_hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1108 | } |
| 1109 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1110 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 1111 | void *v) |
| 1112 | { |
| 1113 | int cpu = (long)v; |
| 1114 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 1115 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1116 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 1117 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 1118 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1119 | cpu); |
| 1120 | hardware_disable(NULL); |
| 1121 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1122 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1123 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1124 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1125 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1126 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1127 | case CPU_ONLINE: |
| 1128 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 1129 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1130 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1131 | break; |
| 1132 | } |
| 1133 | return NOTIFY_OK; |
| 1134 | } |
| 1135 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1136 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1137 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1138 | { |
| 1139 | if (val == SYS_RESTART) { |
| 1140 | /* |
| 1141 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1142 | * in vmx root mode. |
| 1143 | */ |
| 1144 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 1145 | on_each_cpu(hardware_disable, NULL, 0, 1); |
| 1146 | } |
| 1147 | return NOTIFY_OK; |
| 1148 | } |
| 1149 | |
| 1150 | static struct notifier_block kvm_reboot_notifier = { |
| 1151 | .notifier_call = kvm_reboot, |
| 1152 | .priority = 0, |
| 1153 | }; |
| 1154 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1155 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 1156 | { |
| 1157 | memset(bus, 0, sizeof(*bus)); |
| 1158 | } |
| 1159 | |
| 1160 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 1161 | { |
| 1162 | int i; |
| 1163 | |
| 1164 | for (i = 0; i < bus->dev_count; i++) { |
| 1165 | struct kvm_io_device *pos = bus->devs[i]; |
| 1166 | |
| 1167 | kvm_iodevice_destructor(pos); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) |
| 1172 | { |
| 1173 | int i; |
| 1174 | |
| 1175 | for (i = 0; i < bus->dev_count; i++) { |
| 1176 | struct kvm_io_device *pos = bus->devs[i]; |
| 1177 | |
| 1178 | if (pos->in_range(pos, addr)) |
| 1179 | return pos; |
| 1180 | } |
| 1181 | |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | |
| 1185 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 1186 | { |
| 1187 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 1188 | |
| 1189 | bus->devs[bus->dev_count++] = dev; |
| 1190 | } |
| 1191 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1192 | static struct notifier_block kvm_cpu_notifier = { |
| 1193 | .notifier_call = kvm_cpu_hotplug, |
| 1194 | .priority = 20, /* must be > scheduler priority */ |
| 1195 | }; |
| 1196 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1197 | static int vm_stat_get(void *_offset, u64 *val) |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1198 | { |
| 1199 | unsigned offset = (long)_offset; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1200 | struct kvm *kvm; |
| 1201 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1202 | *val = 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1203 | spin_lock(&kvm_lock); |
| 1204 | list_for_each_entry(kvm, &vm_list, vm_list) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1205 | *val += *(u32 *)((void *)kvm + offset); |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1206 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1207 | return 0; |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); |
| 1211 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1212 | static int vcpu_stat_get(void *_offset, u64 *val) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1213 | { |
| 1214 | unsigned offset = (long)_offset; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1215 | struct kvm *kvm; |
| 1216 | struct kvm_vcpu *vcpu; |
| 1217 | int i; |
| 1218 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1219 | *val = 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1220 | spin_lock(&kvm_lock); |
| 1221 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 1222 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1223 | vcpu = kvm->vcpus[i]; |
| 1224 | if (vcpu) |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1225 | *val += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1226 | } |
| 1227 | spin_unlock(&kvm_lock); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 1228 | return 0; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1229 | } |
| 1230 | |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1231 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
| 1232 | |
| 1233 | static struct file_operations *stat_fops[] = { |
| 1234 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
| 1235 | [KVM_STAT_VM] = &vm_stat_fops, |
| 1236 | }; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1237 | |
Zhang Xiantao | a16b043 | 2007-11-16 14:38:21 +0800 | [diff] [blame] | 1238 | static void kvm_init_debug(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1239 | { |
| 1240 | struct kvm_stats_debugfs_item *p; |
| 1241 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1242 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1243 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1244 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 1245 | (void *)(long)p->offset, |
Avi Kivity | ba1389b | 2007-11-18 16:24:12 +0200 | [diff] [blame] | 1246 | stat_fops[p->kind]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | static void kvm_exit_debug(void) |
| 1250 | { |
| 1251 | struct kvm_stats_debugfs_item *p; |
| 1252 | |
| 1253 | for (p = debugfs_entries; p->name; ++p) |
| 1254 | debugfs_remove(p->dentry); |
| 1255 | debugfs_remove(debugfs_dir); |
| 1256 | } |
| 1257 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1258 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 1259 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1260 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int kvm_resume(struct sys_device *dev) |
| 1265 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1266 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1271 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1272 | .suspend = kvm_suspend, |
| 1273 | .resume = kvm_resume, |
| 1274 | }; |
| 1275 | |
| 1276 | static struct sys_device kvm_sysdev = { |
| 1277 | .id = 0, |
| 1278 | .cls = &kvm_sysdev_class, |
| 1279 | }; |
| 1280 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1281 | struct page *bad_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1282 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1283 | static inline |
| 1284 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 1285 | { |
| 1286 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 1287 | } |
| 1288 | |
| 1289 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 1290 | { |
| 1291 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1292 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1293 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 1297 | struct task_struct *next) |
| 1298 | { |
| 1299 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1300 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1301 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1302 | } |
| 1303 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1304 | int kvm_init(void *opaque, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1305 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1306 | { |
| 1307 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1308 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1309 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1310 | kvm_init_debug(); |
| 1311 | |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1312 | r = kvm_arch_init(opaque); |
| 1313 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1314 | goto out_fail; |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1315 | |
| 1316 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 1317 | |
| 1318 | if (bad_page == NULL) { |
| 1319 | r = -ENOMEM; |
| 1320 | goto out; |
| 1321 | } |
| 1322 | |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1323 | r = kvm_arch_hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1324 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1325 | goto out_free_0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1326 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1327 | for_each_online_cpu(cpu) { |
| 1328 | smp_call_function_single(cpu, |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1329 | kvm_arch_check_processor_compat, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1330 | &r, 0, 1); |
| 1331 | if (r < 0) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1332 | goto out_free_1; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1333 | } |
| 1334 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1335 | on_each_cpu(hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1336 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 1337 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1338 | goto out_free_2; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1339 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1340 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1341 | r = sysdev_class_register(&kvm_sysdev_class); |
| 1342 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1343 | goto out_free_3; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1344 | |
| 1345 | r = sysdev_register(&kvm_sysdev); |
| 1346 | if (r) |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1347 | goto out_free_4; |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1348 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1349 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 1350 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
Joe Perches | 56919c5 | 2007-11-12 20:06:51 -0800 | [diff] [blame] | 1351 | __alignof__(struct kvm_vcpu), |
| 1352 | 0, NULL); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1353 | if (!kvm_vcpu_cache) { |
| 1354 | r = -ENOMEM; |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1355 | goto out_free_5; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1356 | } |
| 1357 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1358 | kvm_chardev_ops.owner = module; |
| 1359 | |
| 1360 | r = misc_register(&kvm_dev); |
| 1361 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1362 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | goto out_free; |
| 1364 | } |
| 1365 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1366 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 1367 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 1368 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1369 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1370 | |
| 1371 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1372 | kmem_cache_destroy(kvm_vcpu_cache); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1373 | out_free_5: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1374 | sysdev_unregister(&kvm_sysdev); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1375 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1376 | sysdev_class_unregister(&kvm_sysdev_class); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1377 | out_free_3: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1378 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1379 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1380 | out_free_2: |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1381 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1382 | out_free_1: |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1383 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1384 | out_free_0: |
| 1385 | __free_page(bad_page); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1386 | out: |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1387 | kvm_arch_exit(); |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1388 | kvm_exit_debug(); |
Zhang Xiantao | d2308784 | 2007-11-29 15:35:39 +0800 | [diff] [blame] | 1389 | out_fail: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1390 | return r; |
| 1391 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1392 | EXPORT_SYMBOL_GPL(kvm_init); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1394 | void kvm_exit(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1395 | { |
| 1396 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1397 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1398 | sysdev_unregister(&kvm_sysdev); |
| 1399 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1400 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1401 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1402 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Zhang Xiantao | e9b11c1 | 2007-11-14 20:38:21 +0800 | [diff] [blame] | 1403 | kvm_arch_hardware_unsetup(); |
Zhang Xiantao | f8c16bb | 2007-11-14 20:40:21 +0800 | [diff] [blame] | 1404 | kvm_arch_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1405 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1406 | __free_page(bad_page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1407 | } |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 1408 | EXPORT_SYMBOL_GPL(kvm_exit); |