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 | |
| 18 | #include "kvm.h" |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 19 | #include "x86.h" |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 20 | #include "irq.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | |
| 22 | #include <linux/kvm.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/percpu.h> |
| 26 | #include <linux/gfp.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/vmalloc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <linux/reboot.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | #include <linux/debugfs.h> |
| 32 | #include <linux/highmem.h> |
| 33 | #include <linux/file.h> |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 34 | #include <linux/sysdev.h> |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 35 | #include <linux/cpu.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 36 | #include <linux/sched.h> |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 37 | #include <linux/cpumask.h> |
| 38 | #include <linux/smp.h> |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 39 | #include <linux/anon_inodes.h> |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 40 | #include <linux/profile.h> |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 41 | #include <linux/kvm_para.h> |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 42 | #include <linux/pagemap.h> |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 43 | #include <linux/mman.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 45 | #include <asm/processor.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 46 | #include <asm/io.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | |
| 50 | MODULE_AUTHOR("Qumranet"); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 53 | static DEFINE_SPINLOCK(kvm_lock); |
| 54 | static LIST_HEAD(vm_list); |
| 55 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 56 | static cpumask_t cpus_hardware_enabled; |
| 57 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 58 | struct kmem_cache *kvm_vcpu_cache; |
| 59 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 60 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 61 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
| 62 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 63 | static struct dentry *debugfs_dir; |
| 64 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 65 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
| 66 | unsigned long arg); |
| 67 | |
James Morris | 5aacf0c | 2006-12-22 01:04:55 -0800 | [diff] [blame] | 68 | static inline int valid_vcpu(int n) |
| 69 | { |
| 70 | return likely(n >= 0 && n < KVM_MAX_VCPUS); |
| 71 | } |
| 72 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Switches to specified vcpu, until a matching vcpu_put() |
| 75 | */ |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 76 | void vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 77 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 78 | int cpu; |
| 79 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 80 | mutex_lock(&vcpu->mutex); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 81 | cpu = get_cpu(); |
| 82 | preempt_notifier_register(&vcpu->preempt_notifier); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 83 | kvm_arch_vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 84 | put_cpu(); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 87 | void vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 88 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 89 | preempt_disable(); |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 90 | kvm_arch_vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 91 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
| 92 | preempt_enable(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 93 | mutex_unlock(&vcpu->mutex); |
| 94 | } |
| 95 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 96 | static void ack_flush(void *_completed) |
| 97 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
| 101 | { |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 102 | int i, cpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 103 | cpumask_t cpus; |
| 104 | struct kvm_vcpu *vcpu; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 105 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 106 | cpus_clear(cpus); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 107 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 108 | vcpu = kvm->vcpus[i]; |
| 109 | if (!vcpu) |
| 110 | continue; |
Avi Kivity | 3176bc3 | 2007-10-16 17:22:08 +0200 | [diff] [blame] | 111 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 112 | continue; |
| 113 | cpu = vcpu->cpu; |
| 114 | if (cpu != -1 && cpu != raw_smp_processor_id()) |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 115 | cpu_set(cpu, cpus); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 116 | } |
Laurent Vivier | 49d3bd7 | 2007-10-22 16:33:07 +0200 | [diff] [blame] | 117 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 118 | } |
| 119 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 120 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
| 121 | { |
| 122 | struct page *page; |
| 123 | int r; |
| 124 | |
| 125 | mutex_init(&vcpu->mutex); |
| 126 | vcpu->cpu = -1; |
| 127 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 128 | vcpu->kvm = kvm; |
| 129 | vcpu->vcpu_id = id; |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 130 | if (!irqchip_in_kernel(kvm) || id == 0) |
| 131 | vcpu->mp_state = VCPU_MP_STATE_RUNNABLE; |
| 132 | else |
| 133 | vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED; |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 134 | init_waitqueue_head(&vcpu->wq); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 135 | |
| 136 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 137 | if (!page) { |
| 138 | r = -ENOMEM; |
| 139 | goto fail; |
| 140 | } |
| 141 | vcpu->run = page_address(page); |
| 142 | |
| 143 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 144 | if (!page) { |
| 145 | r = -ENOMEM; |
| 146 | goto fail_free_run; |
| 147 | } |
| 148 | vcpu->pio_data = page_address(page); |
| 149 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 150 | r = kvm_mmu_create(vcpu); |
| 151 | if (r < 0) |
| 152 | goto fail_free_pio_data; |
| 153 | |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 154 | if (irqchip_in_kernel(kvm)) { |
| 155 | r = kvm_create_lapic(vcpu); |
| 156 | if (r < 0) |
| 157 | goto fail_mmu_destroy; |
| 158 | } |
| 159 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 160 | return 0; |
| 161 | |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 162 | fail_mmu_destroy: |
| 163 | kvm_mmu_destroy(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 164 | fail_free_pio_data: |
| 165 | free_page((unsigned long)vcpu->pio_data); |
| 166 | fail_free_run: |
| 167 | free_page((unsigned long)vcpu->run); |
| 168 | fail: |
Rusty Russell | 76fafa5 | 2007-10-08 10:50:48 +1000 | [diff] [blame] | 169 | return r; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 170 | } |
| 171 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); |
| 172 | |
| 173 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 174 | { |
Rusty Russell | d589444 | 2007-10-08 10:48:30 +1000 | [diff] [blame] | 175 | kvm_free_lapic(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 176 | kvm_mmu_destroy(vcpu); |
| 177 | free_page((unsigned long)vcpu->pio_data); |
| 178 | free_page((unsigned long)vcpu->run); |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); |
| 181 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 182 | static struct kvm *kvm_create_vm(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 183 | { |
| 184 | struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 185 | |
| 186 | if (!kvm) |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 187 | return ERR_PTR(-ENOMEM); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 188 | |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 189 | kvm_io_bus_init(&kvm->pio_bus); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 190 | mutex_init(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 191 | INIT_LIST_HEAD(&kvm->active_mmu_pages); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 192 | kvm_io_bus_init(&kvm->mmio_bus); |
Rusty Russell | 5e58cfe | 2007-07-23 17:08:21 +1000 | [diff] [blame] | 193 | spin_lock(&kvm_lock); |
| 194 | list_add(&kvm->vm_list, &vm_list); |
| 195 | spin_unlock(&kvm_lock); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 196 | return kvm; |
| 197 | } |
| 198 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 199 | /* |
| 200 | * Free any memory in @free but not in @dont. |
| 201 | */ |
| 202 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, |
| 203 | struct kvm_memory_slot *dont) |
| 204 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 205 | if (!dont || free->rmap != dont->rmap) |
| 206 | vfree(free->rmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 207 | |
| 208 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) |
| 209 | vfree(free->dirty_bitmap); |
| 210 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 211 | free->npages = 0; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 212 | free->dirty_bitmap = NULL; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 213 | free->rmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void kvm_free_physmem(struct kvm *kvm) |
| 217 | { |
| 218 | int i; |
| 219 | |
| 220 | for (i = 0; i < kvm->nmemslots; ++i) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 221 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 224 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
| 225 | { |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 226 | vcpu_load(vcpu); |
| 227 | kvm_mmu_unload(vcpu); |
| 228 | vcpu_put(vcpu); |
| 229 | } |
| 230 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 231 | static void kvm_free_vcpus(struct kvm *kvm) |
| 232 | { |
| 233 | unsigned int i; |
| 234 | |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 235 | /* |
| 236 | * Unpin any mmu pages first. |
| 237 | */ |
| 238 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 239 | if (kvm->vcpus[i]) |
| 240 | kvm_unload_vcpu_mmu(kvm->vcpus[i]); |
| 241 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
| 242 | if (kvm->vcpus[i]) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 243 | kvm_x86_ops->vcpu_free(kvm->vcpus[i]); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 244 | kvm->vcpus[i] = NULL; |
| 245 | } |
| 246 | } |
| 247 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 250 | static void kvm_destroy_vm(struct kvm *kvm) |
| 251 | { |
Avi Kivity | 133de90 | 2007-02-12 00:54:44 -0800 | [diff] [blame] | 252 | spin_lock(&kvm_lock); |
| 253 | list_del(&kvm->vm_list); |
| 254 | spin_unlock(&kvm_lock); |
Eddie Dong | 7490634 | 2007-06-19 18:05:03 +0300 | [diff] [blame] | 255 | kvm_io_bus_destroy(&kvm->pio_bus); |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 256 | kvm_io_bus_destroy(&kvm->mmio_bus); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 257 | kfree(kvm->vpic); |
Eddie Dong | 1fd4f2a | 2007-07-18 12:03:39 +0300 | [diff] [blame] | 258 | kfree(kvm->vioapic); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 259 | kvm_free_vcpus(kvm); |
| 260 | kvm_free_physmem(kvm); |
| 261 | kfree(kvm); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
| 265 | { |
| 266 | struct kvm *kvm = filp->private_data; |
| 267 | |
| 268 | kvm_destroy_vm(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 272 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 273 | * Allocate some memory and give it an address in the guest physical address |
| 274 | * space. |
| 275 | * |
| 276 | * Discontiguous memory is allowed, mostly for framebuffers. |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 277 | * |
| 278 | * Must be called holding kvm->lock. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 279 | */ |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 280 | int __kvm_set_memory_region(struct kvm *kvm, |
| 281 | struct kvm_userspace_memory_region *mem, |
| 282 | int user_alloc) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 283 | { |
| 284 | int r; |
| 285 | gfn_t base_gfn; |
| 286 | unsigned long npages; |
| 287 | unsigned long i; |
| 288 | struct kvm_memory_slot *memslot; |
| 289 | struct kvm_memory_slot old, new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 290 | |
| 291 | r = -EINVAL; |
| 292 | /* General sanity checks */ |
| 293 | if (mem->memory_size & (PAGE_SIZE - 1)) |
| 294 | goto out; |
| 295 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) |
| 296 | goto out; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 297 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 298 | goto out; |
| 299 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) |
| 300 | goto out; |
| 301 | |
| 302 | memslot = &kvm->memslots[mem->slot]; |
| 303 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
| 304 | npages = mem->memory_size >> PAGE_SHIFT; |
| 305 | |
| 306 | if (!npages) |
| 307 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; |
| 308 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 309 | new = old = *memslot; |
| 310 | |
| 311 | new.base_gfn = base_gfn; |
| 312 | new.npages = npages; |
| 313 | new.flags = mem->flags; |
| 314 | |
| 315 | /* Disallow changing a memory slot's size. */ |
| 316 | r = -EINVAL; |
| 317 | if (npages && old.npages && npages != old.npages) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 318 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 319 | |
| 320 | /* Check for overlaps */ |
| 321 | r = -EEXIST; |
| 322 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 323 | struct kvm_memory_slot *s = &kvm->memslots[i]; |
| 324 | |
| 325 | if (s == memslot) |
| 326 | continue; |
| 327 | if (!((base_gfn + npages <= s->base_gfn) || |
| 328 | (base_gfn >= s->base_gfn + s->npages))) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 329 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 330 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 331 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 332 | /* Free page dirty bitmap if unneeded */ |
| 333 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 334 | new.dirty_bitmap = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 335 | |
| 336 | r = -ENOMEM; |
| 337 | |
| 338 | /* Allocate if a slot is being created */ |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 339 | if (npages && !new.rmap) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 340 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 341 | |
| 342 | if (!new.rmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 343 | goto out_free; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 344 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 345 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 346 | |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 347 | new.user_alloc = user_alloc; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 348 | if (user_alloc) |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 349 | new.userspace_addr = mem->userspace_addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 350 | else { |
| 351 | down_write(¤t->mm->mmap_sem); |
| 352 | new.userspace_addr = do_mmap(NULL, 0, |
| 353 | npages * PAGE_SIZE, |
| 354 | PROT_READ | PROT_WRITE, |
| 355 | MAP_SHARED | MAP_ANONYMOUS, |
| 356 | 0); |
| 357 | up_write(¤t->mm->mmap_sem); |
| 358 | |
| 359 | if (IS_ERR((void *)new.userspace_addr)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 360 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 361 | } |
Izik Eidus | 80b14b5 | 2007-10-25 11:54:04 +0200 | [diff] [blame] | 362 | } else { |
| 363 | if (!old.user_alloc && old.rmap) { |
| 364 | int ret; |
| 365 | |
| 366 | down_write(¤t->mm->mmap_sem); |
| 367 | ret = do_munmap(current->mm, old.userspace_addr, |
| 368 | old.npages * PAGE_SIZE); |
| 369 | up_write(¤t->mm->mmap_sem); |
| 370 | if (ret < 0) |
| 371 | printk(KERN_WARNING |
| 372 | "kvm_vm_ioctl_set_memory_region: " |
| 373 | "failed to munmap memory\n"); |
| 374 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* Allocate page dirty bitmap if needed */ |
| 378 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { |
| 379 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; |
| 380 | |
| 381 | new.dirty_bitmap = vmalloc(dirty_bytes); |
| 382 | if (!new.dirty_bitmap) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 383 | goto out_free; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 384 | memset(new.dirty_bitmap, 0, dirty_bytes); |
| 385 | } |
| 386 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 387 | if (mem->slot >= kvm->nmemslots) |
| 388 | kvm->nmemslots = mem->slot + 1; |
| 389 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 390 | if (!kvm->n_requested_mmu_pages) { |
| 391 | unsigned int n_pages; |
| 392 | |
| 393 | if (npages) { |
| 394 | n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 395 | kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages + |
| 396 | n_pages); |
| 397 | } else { |
| 398 | unsigned int nr_mmu_pages; |
| 399 | |
| 400 | n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 401 | nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages; |
| 402 | nr_mmu_pages = max(nr_mmu_pages, |
| 403 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 404 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); |
| 405 | } |
| 406 | } |
| 407 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 408 | *memslot = new; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 409 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 410 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
| 411 | kvm_flush_remote_tlbs(kvm); |
| 412 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 413 | kvm_free_physmem_slot(&old, &new); |
| 414 | return 0; |
| 415 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 416 | out_free: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 417 | kvm_free_physmem_slot(&new, &old); |
| 418 | out: |
| 419 | return r; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 420 | |
| 421 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 422 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
| 423 | |
| 424 | int kvm_set_memory_region(struct kvm *kvm, |
| 425 | struct kvm_userspace_memory_region *mem, |
| 426 | int user_alloc) |
| 427 | { |
| 428 | int r; |
| 429 | |
| 430 | mutex_lock(&kvm->lock); |
| 431 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
| 432 | mutex_unlock(&kvm->lock); |
| 433 | return r; |
| 434 | } |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 435 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
| 436 | |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 437 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
| 438 | struct |
| 439 | kvm_userspace_memory_region *mem, |
| 440 | int user_alloc) |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 441 | { |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 442 | if (mem->slot >= KVM_MEMORY_SLOTS) |
| 443 | return -EINVAL; |
Izik Eidus | 210c7c4 | 2007-10-24 23:52:57 +0200 | [diff] [blame] | 444 | return kvm_set_memory_region(kvm, mem, user_alloc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Get (and clear) the dirty memory log for a memory slot. |
| 449 | */ |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 450 | static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 451 | struct kvm_dirty_log *log) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 452 | { |
| 453 | struct kvm_memory_slot *memslot; |
| 454 | int r, i; |
| 455 | int n; |
| 456 | unsigned long any = 0; |
| 457 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 458 | mutex_lock(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 459 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 460 | r = -EINVAL; |
| 461 | if (log->slot >= KVM_MEMORY_SLOTS) |
| 462 | goto out; |
| 463 | |
| 464 | memslot = &kvm->memslots[log->slot]; |
| 465 | r = -ENOENT; |
| 466 | if (!memslot->dirty_bitmap) |
| 467 | goto out; |
| 468 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 469 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 470 | |
Uri Lublin | cd1a4a9 | 2007-02-22 16:43:09 +0200 | [diff] [blame] | 471 | for (i = 0; !any && i < n/sizeof(long); ++i) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 472 | any = memslot->dirty_bitmap[i]; |
| 473 | |
| 474 | r = -EFAULT; |
| 475 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 476 | goto out; |
| 477 | |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 478 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 479 | if (any) { |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 480 | kvm_mmu_slot_remove_write_access(kvm, log->slot); |
| 481 | kvm_flush_remote_tlbs(kvm); |
| 482 | memset(memslot->dirty_bitmap, 0, n); |
Rusty Russell | 3921491 | 2007-07-31 19:57:47 +1000 | [diff] [blame] | 483 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 484 | |
| 485 | r = 0; |
| 486 | |
| 487 | out: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 488 | mutex_unlock(&kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 489 | return r; |
| 490 | } |
| 491 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 492 | int is_error_page(struct page *page) |
| 493 | { |
| 494 | return page == bad_page; |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(is_error_page); |
| 497 | |
Izik Eidus | f9d46eb | 2007-11-11 22:02:22 +0200 | [diff] [blame] | 498 | static inline unsigned long bad_hva(void) |
| 499 | { |
| 500 | return PAGE_OFFSET; |
| 501 | } |
| 502 | |
| 503 | int kvm_is_error_hva(unsigned long addr) |
| 504 | { |
| 505 | return addr == bad_hva(); |
| 506 | } |
| 507 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); |
| 508 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 509 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 510 | { |
| 511 | int i; |
| 512 | struct kvm_mem_alias *alias; |
| 513 | |
| 514 | for (i = 0; i < kvm->naliases; ++i) { |
| 515 | alias = &kvm->aliases[i]; |
| 516 | if (gfn >= alias->base_gfn |
| 517 | && gfn < alias->base_gfn + alias->npages) |
| 518 | return alias->target_gfn + gfn - alias->base_gfn; |
| 519 | } |
| 520 | return gfn; |
| 521 | } |
| 522 | |
| 523 | 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] | 524 | { |
| 525 | int i; |
| 526 | |
| 527 | for (i = 0; i < kvm->nmemslots; ++i) { |
| 528 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 529 | |
| 530 | if (gfn >= memslot->base_gfn |
| 531 | && gfn < memslot->base_gfn + memslot->npages) |
| 532 | return memslot; |
| 533 | } |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 534 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 535 | } |
Avi Kivity | e820754 | 2007-03-30 16:54:30 +0300 | [diff] [blame] | 536 | |
| 537 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
| 538 | { |
| 539 | gfn = unalias_gfn(kvm, gfn); |
| 540 | return __gfn_to_memslot(kvm, gfn); |
| 541 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 542 | |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 543 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
| 544 | { |
| 545 | int i; |
| 546 | |
| 547 | gfn = unalias_gfn(kvm, gfn); |
| 548 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 549 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 550 | |
| 551 | if (gfn >= memslot->base_gfn |
| 552 | && gfn < memslot->base_gfn + memslot->npages) |
| 553 | return 1; |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); |
| 558 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 559 | static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
| 560 | { |
| 561 | struct kvm_memory_slot *slot; |
| 562 | |
| 563 | gfn = unalias_gfn(kvm, gfn); |
| 564 | slot = __gfn_to_memslot(kvm, gfn); |
| 565 | if (!slot) |
| 566 | return bad_hva(); |
| 567 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); |
| 568 | } |
| 569 | |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 570 | /* |
| 571 | * Requires current->mm->mmap_sem to be held |
| 572 | */ |
| 573 | static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 574 | { |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 575 | struct page *page[1]; |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 576 | unsigned long addr; |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 577 | int npages; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 578 | |
Avi Kivity | 6039522 | 2007-10-21 11:03:36 +0200 | [diff] [blame] | 579 | might_sleep(); |
| 580 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 581 | addr = gfn_to_hva(kvm, gfn); |
| 582 | if (kvm_is_error_hva(addr)) { |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 583 | get_page(bad_page); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 584 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 585 | } |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 586 | |
Izik Eidus | 539cb66 | 2007-11-11 22:05:04 +0200 | [diff] [blame] | 587 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
| 588 | NULL); |
| 589 | |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 590 | if (npages != 1) { |
| 591 | get_page(bad_page); |
| 592 | return bad_page; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 593 | } |
Anthony Liguori | 8d4e128 | 2007-10-18 09:59:34 -0500 | [diff] [blame] | 594 | |
| 595 | return page[0]; |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 596 | } |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 597 | |
| 598 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
| 599 | { |
| 600 | struct page *page; |
| 601 | |
| 602 | down_read(¤t->mm->mmap_sem); |
| 603 | page = __gfn_to_page(kvm, gfn); |
| 604 | up_read(¤t->mm->mmap_sem); |
| 605 | |
| 606 | return page; |
| 607 | } |
| 608 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 609 | EXPORT_SYMBOL_GPL(gfn_to_page); |
| 610 | |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 611 | void kvm_release_page(struct page *page) |
| 612 | { |
| 613 | if (!PageReserved(page)) |
| 614 | SetPageDirty(page); |
| 615 | put_page(page); |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(kvm_release_page); |
| 618 | |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 619 | static int next_segment(unsigned long len, int offset) |
| 620 | { |
| 621 | if (len > PAGE_SIZE - offset) |
| 622 | return PAGE_SIZE - offset; |
| 623 | else |
| 624 | return len; |
| 625 | } |
| 626 | |
| 627 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
| 628 | int len) |
| 629 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 630 | int r; |
| 631 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 632 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 633 | addr = gfn_to_hva(kvm, gfn); |
| 634 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 635 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 636 | r = copy_from_user(data, (void __user *)addr + offset, len); |
| 637 | if (r) |
| 638 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
| 642 | |
| 643 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
| 644 | { |
| 645 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 646 | int seg; |
| 647 | int offset = offset_in_page(gpa); |
| 648 | int ret; |
| 649 | |
| 650 | while ((seg = next_segment(len, offset)) != 0) { |
| 651 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); |
| 652 | if (ret < 0) |
| 653 | return ret; |
| 654 | offset = 0; |
| 655 | len -= seg; |
| 656 | data += seg; |
| 657 | ++gfn; |
| 658 | } |
| 659 | return 0; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(kvm_read_guest); |
| 662 | |
| 663 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
| 664 | int offset, int len) |
| 665 | { |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 666 | int r; |
| 667 | unsigned long addr; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 668 | |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 669 | addr = gfn_to_hva(kvm, gfn); |
| 670 | if (kvm_is_error_hva(addr)) |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 671 | return -EFAULT; |
Izik Eidus | e0506bc | 2007-11-11 22:10:22 +0200 | [diff] [blame] | 672 | r = copy_to_user((void __user *)addr + offset, data, len); |
| 673 | if (r) |
| 674 | return -EFAULT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 675 | mark_page_dirty(kvm, gfn); |
| 676 | return 0; |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
| 679 | |
| 680 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
| 681 | unsigned long len) |
| 682 | { |
| 683 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 684 | int seg; |
| 685 | int offset = offset_in_page(gpa); |
| 686 | int ret; |
| 687 | |
| 688 | while ((seg = next_segment(len, offset)) != 0) { |
| 689 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); |
| 690 | if (ret < 0) |
| 691 | return ret; |
| 692 | offset = 0; |
| 693 | len -= seg; |
| 694 | data += seg; |
| 695 | ++gfn; |
| 696 | } |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
| 701 | { |
| 702 | void *page_virt; |
| 703 | struct page *page; |
| 704 | |
| 705 | page = gfn_to_page(kvm, gfn); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 706 | if (is_error_page(page)) { |
| 707 | kvm_release_page(page); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 708 | return -EFAULT; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 709 | } |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 710 | page_virt = kmap_atomic(page, KM_USER0); |
| 711 | |
| 712 | memset(page_virt + offset, 0, len); |
| 713 | |
| 714 | kunmap_atomic(page_virt, KM_USER0); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 715 | kvm_release_page(page); |
Dor Laor | 1226476 | 2007-11-07 16:20:06 +0200 | [diff] [blame] | 716 | mark_page_dirty(kvm, gfn); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); |
| 720 | |
| 721 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) |
| 722 | { |
| 723 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 724 | int seg; |
| 725 | int offset = offset_in_page(gpa); |
| 726 | int ret; |
| 727 | |
| 728 | while ((seg = next_segment(len, offset)) != 0) { |
| 729 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
| 730 | if (ret < 0) |
| 731 | return ret; |
| 732 | offset = 0; |
| 733 | len -= seg; |
| 734 | ++gfn; |
| 735 | } |
| 736 | return 0; |
| 737 | } |
| 738 | EXPORT_SYMBOL_GPL(kvm_clear_guest); |
| 739 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 740 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
| 741 | { |
Nguyen Anh Quynh | 3138994 | 2007-06-05 10:35:19 +0300 | [diff] [blame] | 742 | struct kvm_memory_slot *memslot; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 743 | |
Uri Lublin | 3b6fff1 | 2007-10-30 10:42:09 +0200 | [diff] [blame] | 744 | gfn = unalias_gfn(kvm, gfn); |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 745 | memslot = __gfn_to_memslot(kvm, gfn); |
| 746 | if (memslot && memslot->dirty_bitmap) { |
| 747 | unsigned long rel_gfn = gfn - memslot->base_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 748 | |
Rusty Russell | 7e9d619 | 2007-07-31 20:41:14 +1000 | [diff] [blame] | 749 | /* avoid RMW */ |
| 750 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) |
| 751 | set_bit(rel_gfn, memslot->dirty_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 755 | /* |
| 756 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. |
| 757 | */ |
Hollis Blanchard | 8776e51 | 2007-10-31 17:24:24 -0500 | [diff] [blame] | 758 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 759 | { |
| 760 | DECLARE_WAITQUEUE(wait, current); |
| 761 | |
| 762 | add_wait_queue(&vcpu->wq, &wait); |
| 763 | |
| 764 | /* |
| 765 | * We will block until either an interrupt or a signal wakes us up |
| 766 | */ |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 767 | while (!kvm_cpu_has_interrupt(vcpu) |
| 768 | && !signal_pending(current) |
| 769 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE |
| 770 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 771 | set_current_state(TASK_INTERRUPTIBLE); |
| 772 | vcpu_put(vcpu); |
| 773 | schedule(); |
| 774 | vcpu_load(vcpu); |
| 775 | } |
| 776 | |
He, Qing | c5ec153 | 2007-09-03 17:07:41 +0300 | [diff] [blame] | 777 | __set_current_state(TASK_RUNNING); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 778 | remove_wait_queue(&vcpu->wq, &wait); |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 779 | } |
| 780 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 781 | void kvm_resched(struct kvm_vcpu *vcpu) |
| 782 | { |
Yaozu Dong | 3fca036 | 2007-04-25 16:49:19 +0300 | [diff] [blame] | 783 | if (!need_resched()) |
| 784 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 785 | cond_resched(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 786 | } |
| 787 | EXPORT_SYMBOL_GPL(kvm_resched); |
| 788 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 789 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 790 | * Translate a guest virtual address to a guest physical address. |
| 791 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 792 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 793 | struct kvm_translation *tr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 794 | { |
| 795 | unsigned long vaddr = tr->linear_address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 796 | gpa_t gpa; |
| 797 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 798 | vcpu_load(vcpu); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 799 | mutex_lock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
| 801 | tr->physical_address = gpa; |
| 802 | tr->valid = gpa != UNMAPPED_GVA; |
| 803 | tr->writeable = 1; |
| 804 | tr->usermode = 0; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 805 | mutex_unlock(&vcpu->kvm->lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 806 | vcpu_put(vcpu); |
| 807 | |
| 808 | return 0; |
| 809 | } |
| 810 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 811 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
| 812 | struct kvm_interrupt *irq) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 813 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 814 | if (irq->irq < 0 || irq->irq >= 256) |
| 815 | return -EINVAL; |
Eddie Dong | 97222cc | 2007-09-12 10:58:04 +0300 | [diff] [blame] | 816 | if (irqchip_in_kernel(vcpu->kvm)) |
| 817 | return -ENXIO; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 818 | vcpu_load(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 819 | |
| 820 | set_bit(irq->irq, vcpu->irq_pending); |
| 821 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 822 | |
| 823 | vcpu_put(vcpu); |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 828 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
| 829 | unsigned long address, |
| 830 | int *type) |
| 831 | { |
| 832 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
| 833 | unsigned long pgoff; |
| 834 | struct page *page; |
| 835 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 836 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 837 | if (pgoff == 0) |
| 838 | page = virt_to_page(vcpu->run); |
| 839 | else if (pgoff == KVM_PIO_PAGE_OFFSET) |
| 840 | page = virt_to_page(vcpu->pio_data); |
| 841 | else |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 842 | return NOPAGE_SIGBUS; |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 843 | get_page(page); |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 844 | if (type != NULL) |
| 845 | *type = VM_FAULT_MINOR; |
| 846 | |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 847 | return page; |
| 848 | } |
| 849 | |
| 850 | static struct vm_operations_struct kvm_vcpu_vm_ops = { |
| 851 | .nopage = kvm_vcpu_nopage, |
| 852 | }; |
| 853 | |
| 854 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) |
| 855 | { |
| 856 | vma->vm_ops = &kvm_vcpu_vm_ops; |
| 857 | return 0; |
| 858 | } |
| 859 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 860 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
| 861 | { |
| 862 | struct kvm_vcpu *vcpu = filp->private_data; |
| 863 | |
| 864 | fput(vcpu->kvm->filp); |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static struct file_operations kvm_vcpu_fops = { |
| 869 | .release = kvm_vcpu_release, |
| 870 | .unlocked_ioctl = kvm_vcpu_ioctl, |
| 871 | .compat_ioctl = kvm_vcpu_ioctl, |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 872 | .mmap = kvm_vcpu_mmap, |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 873 | }; |
| 874 | |
| 875 | /* |
| 876 | * Allocates an inode for the vcpu. |
| 877 | */ |
| 878 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) |
| 879 | { |
| 880 | int fd, r; |
| 881 | struct inode *inode; |
| 882 | struct file *file; |
| 883 | |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 884 | r = anon_inode_getfd(&fd, &inode, &file, |
| 885 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); |
| 886 | if (r) |
| 887 | return r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 888 | atomic_inc(&vcpu->kvm->filp->f_count); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 889 | return fd; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 892 | /* |
| 893 | * Creates some virtual cpus. Good luck creating more than one. |
| 894 | */ |
| 895 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) |
| 896 | { |
| 897 | int r; |
| 898 | struct kvm_vcpu *vcpu; |
| 899 | |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 900 | if (!valid_vcpu(n)) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 901 | return -EINVAL; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 902 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 903 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 904 | if (IS_ERR(vcpu)) |
| 905 | return PTR_ERR(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 906 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 907 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 908 | |
Rusty Russell | b114b08 | 2007-07-30 21:13:43 +1000 | [diff] [blame] | 909 | /* We do fxsave: this must be aligned. */ |
| 910 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); |
| 911 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 912 | vcpu_load(vcpu); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 913 | r = kvm_x86_ops->vcpu_reset(vcpu); |
| 914 | if (r == 0) |
| 915 | r = kvm_mmu_setup(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 916 | vcpu_put(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 917 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 918 | goto free_vcpu; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 919 | |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 920 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 921 | if (kvm->vcpus[n]) { |
| 922 | r = -EEXIST; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 923 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 924 | goto mmu_unload; |
| 925 | } |
| 926 | kvm->vcpus[n] = vcpu; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 927 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 928 | |
| 929 | /* Now it's all set up, let userspace reach it */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 930 | r = create_vcpu_fd(vcpu); |
| 931 | if (r < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 932 | goto unlink; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 933 | return r; |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 934 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 935 | unlink: |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 936 | mutex_lock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 937 | kvm->vcpus[n] = NULL; |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 938 | mutex_unlock(&kvm->lock); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 939 | |
| 940 | mmu_unload: |
| 941 | vcpu_load(vcpu); |
| 942 | kvm_mmu_unload(vcpu); |
| 943 | vcpu_put(vcpu); |
| 944 | |
| 945 | free_vcpu: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 946 | kvm_x86_ops->vcpu_free(vcpu); |
Avi Kivity | c5ea766 | 2007-02-20 18:41:05 +0200 | [diff] [blame] | 947 | return r; |
| 948 | } |
| 949 | |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 950 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
| 951 | { |
| 952 | if (sigset) { |
| 953 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 954 | vcpu->sigset_active = 1; |
| 955 | vcpu->sigset = *sigset; |
| 956 | } else |
| 957 | vcpu->sigset_active = 0; |
| 958 | return 0; |
| 959 | } |
| 960 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 961 | static long kvm_vcpu_ioctl(struct file *filp, |
| 962 | unsigned int ioctl, unsigned long arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 963 | { |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 964 | struct kvm_vcpu *vcpu = filp->private_data; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 965 | void __user *argp = (void __user *)arg; |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 966 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 967 | |
| 968 | switch (ioctl) { |
Avi Kivity | 9a2bb7f | 2007-02-22 12:58:31 +0200 | [diff] [blame] | 969 | case KVM_RUN: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 970 | r = -EINVAL; |
| 971 | if (arg) |
| 972 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 973 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 974 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | case KVM_GET_REGS: { |
| 976 | struct kvm_regs kvm_regs; |
| 977 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 978 | memset(&kvm_regs, 0, sizeof kvm_regs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 979 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 980 | if (r) |
| 981 | goto out; |
| 982 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 983 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 984 | goto out; |
| 985 | r = 0; |
| 986 | break; |
| 987 | } |
| 988 | case KVM_SET_REGS: { |
| 989 | struct kvm_regs kvm_regs; |
| 990 | |
| 991 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 992 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 993 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 994 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | if (r) |
| 996 | goto out; |
| 997 | r = 0; |
| 998 | break; |
| 999 | } |
| 1000 | case KVM_GET_SREGS: { |
| 1001 | struct kvm_sregs kvm_sregs; |
| 1002 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1003 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1004 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1005 | if (r) |
| 1006 | goto out; |
| 1007 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1008 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1009 | goto out; |
| 1010 | r = 0; |
| 1011 | break; |
| 1012 | } |
| 1013 | case KVM_SET_SREGS: { |
| 1014 | struct kvm_sregs kvm_sregs; |
| 1015 | |
| 1016 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1017 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1018 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1019 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1020 | if (r) |
| 1021 | goto out; |
| 1022 | r = 0; |
| 1023 | break; |
| 1024 | } |
| 1025 | case KVM_TRANSLATE: { |
| 1026 | struct kvm_translation tr; |
| 1027 | |
| 1028 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1029 | if (copy_from_user(&tr, argp, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1030 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1031 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1032 | if (r) |
| 1033 | goto out; |
| 1034 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1035 | if (copy_to_user(argp, &tr, sizeof tr)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1036 | goto out; |
| 1037 | r = 0; |
| 1038 | break; |
| 1039 | } |
| 1040 | case KVM_INTERRUPT: { |
| 1041 | struct kvm_interrupt irq; |
| 1042 | |
| 1043 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1044 | if (copy_from_user(&irq, argp, sizeof irq)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1045 | goto out; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1046 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1047 | if (r) |
| 1048 | goto out; |
| 1049 | r = 0; |
| 1050 | break; |
| 1051 | } |
| 1052 | case KVM_DEBUG_GUEST: { |
| 1053 | struct kvm_debug_guest dbg; |
| 1054 | |
| 1055 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1056 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1057 | goto out; |
Hollis Blanchard | b6c7a5d | 2007-11-01 14:16:10 -0500 | [diff] [blame] | 1058 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1059 | if (r) |
| 1060 | goto out; |
| 1061 | r = 0; |
| 1062 | break; |
| 1063 | } |
Avi Kivity | 1961d27 | 2007-03-05 19:46:05 +0200 | [diff] [blame] | 1064 | case KVM_SET_SIGNAL_MASK: { |
| 1065 | struct kvm_signal_mask __user *sigmask_arg = argp; |
| 1066 | struct kvm_signal_mask kvm_sigmask; |
| 1067 | sigset_t sigset, *p; |
| 1068 | |
| 1069 | p = NULL; |
| 1070 | if (argp) { |
| 1071 | r = -EFAULT; |
| 1072 | if (copy_from_user(&kvm_sigmask, argp, |
| 1073 | sizeof kvm_sigmask)) |
| 1074 | goto out; |
| 1075 | r = -EINVAL; |
| 1076 | if (kvm_sigmask.len != sizeof sigset) |
| 1077 | goto out; |
| 1078 | r = -EFAULT; |
| 1079 | if (copy_from_user(&sigset, sigmask_arg->sigset, |
| 1080 | sizeof sigset)) |
| 1081 | goto out; |
| 1082 | p = &sigset; |
| 1083 | } |
| 1084 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
| 1085 | break; |
| 1086 | } |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1087 | case KVM_GET_FPU: { |
| 1088 | struct kvm_fpu fpu; |
| 1089 | |
| 1090 | memset(&fpu, 0, sizeof fpu); |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1091 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1092 | if (r) |
| 1093 | goto out; |
| 1094 | r = -EFAULT; |
| 1095 | if (copy_to_user(argp, &fpu, sizeof fpu)) |
| 1096 | goto out; |
| 1097 | r = 0; |
| 1098 | break; |
| 1099 | } |
| 1100 | case KVM_SET_FPU: { |
| 1101 | struct kvm_fpu fpu; |
| 1102 | |
| 1103 | r = -EFAULT; |
| 1104 | if (copy_from_user(&fpu, argp, sizeof fpu)) |
| 1105 | goto out; |
Hollis Blanchard | d075206 | 2007-10-31 17:24:25 -0500 | [diff] [blame] | 1106 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
Avi Kivity | b883673 | 2007-04-01 16:34:31 +0300 | [diff] [blame] | 1107 | if (r) |
| 1108 | goto out; |
| 1109 | r = 0; |
| 1110 | break; |
| 1111 | } |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1112 | default: |
Carsten Otte | 313a3dc | 2007-10-11 19:16:52 +0200 | [diff] [blame] | 1113 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1114 | } |
| 1115 | out: |
| 1116 | return r; |
| 1117 | } |
| 1118 | |
| 1119 | static long kvm_vm_ioctl(struct file *filp, |
| 1120 | unsigned int ioctl, unsigned long arg) |
| 1121 | { |
| 1122 | struct kvm *kvm = filp->private_data; |
| 1123 | void __user *argp = (void __user *)arg; |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1124 | int r; |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1125 | |
| 1126 | switch (ioctl) { |
| 1127 | case KVM_CREATE_VCPU: |
| 1128 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); |
| 1129 | if (r < 0) |
| 1130 | goto out; |
| 1131 | break; |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1132 | case KVM_SET_USER_MEMORY_REGION: { |
| 1133 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1134 | |
| 1135 | r = -EFAULT; |
| 1136 | if (copy_from_user(&kvm_userspace_mem, argp, |
| 1137 | sizeof kvm_userspace_mem)) |
| 1138 | goto out; |
| 1139 | |
| 1140 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1141 | if (r) |
| 1142 | goto out; |
| 1143 | break; |
| 1144 | } |
| 1145 | case KVM_GET_DIRTY_LOG: { |
| 1146 | struct kvm_dirty_log log; |
| 1147 | |
| 1148 | r = -EFAULT; |
Al Viro | 2f36698 | 2007-02-09 16:38:35 +0000 | [diff] [blame] | 1149 | if (copy_from_user(&log, argp, sizeof log)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1150 | goto out; |
Avi Kivity | 2c6f5df | 2007-02-20 18:27:58 +0200 | [diff] [blame] | 1151 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1152 | if (r) |
| 1153 | goto out; |
| 1154 | break; |
| 1155 | } |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1156 | default: |
Carsten Otte | 1fe779f | 2007-10-29 16:08:35 +0100 | [diff] [blame] | 1157 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1158 | } |
| 1159 | out: |
| 1160 | return r; |
| 1161 | } |
| 1162 | |
| 1163 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, |
| 1164 | unsigned long address, |
| 1165 | int *type) |
| 1166 | { |
| 1167 | struct kvm *kvm = vma->vm_file->private_data; |
| 1168 | unsigned long pgoff; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1169 | struct page *page; |
| 1170 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1171 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
Izik Eidus | e0d62c7 | 2007-10-24 23:57:46 +0200 | [diff] [blame] | 1172 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
| 1173 | return NOPAGE_SIGBUS; |
Anthony Liguori | aab61cc | 2007-10-29 15:15:20 -0500 | [diff] [blame] | 1174 | /* current->mm->mmap_sem is already held so call lockless version */ |
| 1175 | page = __gfn_to_page(kvm, pgoff); |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1176 | if (is_error_page(page)) { |
| 1177 | kvm_release_page(page); |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1178 | return NOPAGE_SIGBUS; |
Izik Eidus | 8a7ae05 | 2007-10-18 11:09:33 +0200 | [diff] [blame] | 1179 | } |
Nguyen Anh Quynh | cd0d913 | 2007-07-11 14:30:54 +0300 | [diff] [blame] | 1180 | if (type != NULL) |
| 1181 | *type = VM_FAULT_MINOR; |
| 1182 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1183 | return page; |
| 1184 | } |
| 1185 | |
| 1186 | static struct vm_operations_struct kvm_vm_vm_ops = { |
| 1187 | .nopage = kvm_vm_nopage, |
| 1188 | }; |
| 1189 | |
| 1190 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) |
| 1191 | { |
| 1192 | vma->vm_ops = &kvm_vm_vm_ops; |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static struct file_operations kvm_vm_fops = { |
| 1197 | .release = kvm_vm_release, |
| 1198 | .unlocked_ioctl = kvm_vm_ioctl, |
| 1199 | .compat_ioctl = kvm_vm_ioctl, |
| 1200 | .mmap = kvm_vm_mmap, |
| 1201 | }; |
| 1202 | |
| 1203 | static int kvm_dev_ioctl_create_vm(void) |
| 1204 | { |
| 1205 | int fd, r; |
| 1206 | struct inode *inode; |
| 1207 | struct file *file; |
| 1208 | struct kvm *kvm; |
| 1209 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1210 | kvm = kvm_create_vm(); |
Avi Kivity | d6d2816 | 2007-06-28 08:38:16 -0400 | [diff] [blame] | 1211 | if (IS_ERR(kvm)) |
| 1212 | return PTR_ERR(kvm); |
| 1213 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); |
| 1214 | if (r) { |
| 1215 | kvm_destroy_vm(kvm); |
| 1216 | return r; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 1219 | kvm->filp = file; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1220 | |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1221 | return fd; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static long kvm_dev_ioctl(struct file *filp, |
| 1225 | unsigned int ioctl, unsigned long arg) |
| 1226 | { |
| 1227 | void __user *argp = (void __user *)arg; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1228 | long r = -EINVAL; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1229 | |
| 1230 | switch (ioctl) { |
| 1231 | case KVM_GET_API_VERSION: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1232 | r = -EINVAL; |
| 1233 | if (arg) |
| 1234 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1235 | r = KVM_API_VERSION; |
| 1236 | break; |
| 1237 | case KVM_CREATE_VM: |
Avi Kivity | f0fe510 | 2007-03-07 13:11:17 +0200 | [diff] [blame] | 1238 | r = -EINVAL; |
| 1239 | if (arg) |
| 1240 | goto out; |
Avi Kivity | f17abe9 | 2007-02-21 19:28:04 +0200 | [diff] [blame] | 1241 | r = kvm_dev_ioctl_create_vm(); |
| 1242 | break; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1243 | case KVM_CHECK_EXTENSION: { |
| 1244 | int ext = (long)argp; |
| 1245 | |
| 1246 | switch (ext) { |
| 1247 | case KVM_CAP_IRQCHIP: |
Eddie Dong | b6958ce | 2007-07-18 12:15:21 +0300 | [diff] [blame] | 1248 | case KVM_CAP_HLT: |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1249 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
Izik Eidus | 6fc138d | 2007-10-09 19:20:39 +0200 | [diff] [blame] | 1250 | case KVM_CAP_USER_MEMORY: |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1251 | case KVM_CAP_SET_TSS_ADDR: |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1252 | r = 1; |
| 1253 | break; |
| 1254 | default: |
| 1255 | r = 0; |
| 1256 | break; |
| 1257 | } |
Avi Kivity | 5d308f4 | 2007-03-01 17:56:20 +0200 | [diff] [blame] | 1258 | break; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1259 | } |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1260 | case KVM_GET_VCPU_MMAP_SIZE: |
| 1261 | r = -EINVAL; |
| 1262 | if (arg) |
| 1263 | goto out; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1264 | r = 2 * PAGE_SIZE; |
Avi Kivity | 07c45a3 | 2007-03-07 13:05:38 +0200 | [diff] [blame] | 1265 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1266 | default: |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1267 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1268 | } |
| 1269 | out: |
| 1270 | return r; |
| 1271 | } |
| 1272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1273 | static struct file_operations kvm_chardev_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1274 | .unlocked_ioctl = kvm_dev_ioctl, |
| 1275 | .compat_ioctl = kvm_dev_ioctl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1276 | }; |
| 1277 | |
| 1278 | static struct miscdevice kvm_dev = { |
Avi Kivity | bbe4432 | 2007-03-04 13:27:36 +0200 | [diff] [blame] | 1279 | KVM_MINOR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1280 | "kvm", |
| 1281 | &kvm_chardev_ops, |
| 1282 | }; |
| 1283 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1284 | /* |
| 1285 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus |
| 1286 | * cached on it. |
| 1287 | */ |
| 1288 | static void decache_vcpus_on_cpu(int cpu) |
| 1289 | { |
| 1290 | struct kvm *vm; |
| 1291 | struct kvm_vcpu *vcpu; |
| 1292 | int i; |
| 1293 | |
| 1294 | spin_lock(&kvm_lock); |
Shaohua Li | 11ec280 | 2007-07-23 14:51:37 +0800 | [diff] [blame] | 1295 | list_for_each_entry(vm, &vm_list, vm_list) |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1296 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1297 | vcpu = vm->vcpus[i]; |
| 1298 | if (!vcpu) |
| 1299 | continue; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1300 | /* |
| 1301 | * If the vcpu is locked, then it is running on some |
| 1302 | * other cpu and therefore it is not cached on the |
| 1303 | * cpu in question. |
| 1304 | * |
| 1305 | * If it's not locked, check the last cpu it executed |
| 1306 | * on. |
| 1307 | */ |
| 1308 | if (mutex_trylock(&vcpu->mutex)) { |
| 1309 | if (vcpu->cpu == cpu) { |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1310 | kvm_x86_ops->vcpu_decache(vcpu); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1311 | vcpu->cpu = -1; |
| 1312 | } |
| 1313 | mutex_unlock(&vcpu->mutex); |
| 1314 | } |
| 1315 | } |
| 1316 | spin_unlock(&kvm_lock); |
| 1317 | } |
| 1318 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1319 | static void hardware_enable(void *junk) |
| 1320 | { |
| 1321 | int cpu = raw_smp_processor_id(); |
| 1322 | |
| 1323 | if (cpu_isset(cpu, cpus_hardware_enabled)) |
| 1324 | return; |
| 1325 | cpu_set(cpu, cpus_hardware_enabled); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1326 | kvm_x86_ops->hardware_enable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | static void hardware_disable(void *junk) |
| 1330 | { |
| 1331 | int cpu = raw_smp_processor_id(); |
| 1332 | |
| 1333 | if (!cpu_isset(cpu, cpus_hardware_enabled)) |
| 1334 | return; |
| 1335 | cpu_clear(cpu, cpus_hardware_enabled); |
| 1336 | decache_vcpus_on_cpu(cpu); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1337 | kvm_x86_ops->hardware_disable(NULL); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1338 | } |
| 1339 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1340 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
| 1341 | void *v) |
| 1342 | { |
| 1343 | int cpu = (long)v; |
| 1344 | |
Avi Kivity | 1a6f4d7 | 2007-11-11 18:37:32 +0200 | [diff] [blame] | 1345 | val &= ~CPU_TASKS_FROZEN; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1346 | switch (val) { |
Avi Kivity | cec9ad2 | 2007-05-24 13:11:41 +0300 | [diff] [blame] | 1347 | case CPU_DYING: |
Avi Kivity | 6ec8a85 | 2007-08-19 15:57:26 +0300 | [diff] [blame] | 1348 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1349 | cpu); |
| 1350 | hardware_disable(NULL); |
| 1351 | break; |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1352 | case CPU_UP_CANCELED: |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1353 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
| 1354 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1355 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1356 | break; |
Jeremy Katz | 43934a3 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 1357 | case CPU_ONLINE: |
| 1358 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", |
| 1359 | cpu); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1360 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1361 | break; |
| 1362 | } |
| 1363 | return NOTIFY_OK; |
| 1364 | } |
| 1365 | |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1366 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1367 | void *v) |
Rusty Russell | 9a2b85c | 2007-07-17 23:17:55 +1000 | [diff] [blame] | 1368 | { |
| 1369 | if (val == SYS_RESTART) { |
| 1370 | /* |
| 1371 | * Some (well, at least mine) BIOSes hang on reboot if |
| 1372 | * in vmx root mode. |
| 1373 | */ |
| 1374 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); |
| 1375 | on_each_cpu(hardware_disable, NULL, 0, 1); |
| 1376 | } |
| 1377 | return NOTIFY_OK; |
| 1378 | } |
| 1379 | |
| 1380 | static struct notifier_block kvm_reboot_notifier = { |
| 1381 | .notifier_call = kvm_reboot, |
| 1382 | .priority = 0, |
| 1383 | }; |
| 1384 | |
Gregory Haskins | 2eeb2e9 | 2007-05-31 14:08:53 -0400 | [diff] [blame] | 1385 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
| 1386 | { |
| 1387 | memset(bus, 0, sizeof(*bus)); |
| 1388 | } |
| 1389 | |
| 1390 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
| 1391 | { |
| 1392 | int i; |
| 1393 | |
| 1394 | for (i = 0; i < bus->dev_count; i++) { |
| 1395 | struct kvm_io_device *pos = bus->devs[i]; |
| 1396 | |
| 1397 | kvm_iodevice_destructor(pos); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) |
| 1402 | { |
| 1403 | int i; |
| 1404 | |
| 1405 | for (i = 0; i < bus->dev_count; i++) { |
| 1406 | struct kvm_io_device *pos = bus->devs[i]; |
| 1407 | |
| 1408 | if (pos->in_range(pos, addr)) |
| 1409 | return pos; |
| 1410 | } |
| 1411 | |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | |
| 1415 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) |
| 1416 | { |
| 1417 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); |
| 1418 | |
| 1419 | bus->devs[bus->dev_count++] = dev; |
| 1420 | } |
| 1421 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1422 | static struct notifier_block kvm_cpu_notifier = { |
| 1423 | .notifier_call = kvm_cpu_hotplug, |
| 1424 | .priority = 20, /* must be > scheduler priority */ |
| 1425 | }; |
| 1426 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1427 | static u64 stat_get(void *_offset) |
| 1428 | { |
| 1429 | unsigned offset = (long)_offset; |
| 1430 | u64 total = 0; |
| 1431 | struct kvm *kvm; |
| 1432 | struct kvm_vcpu *vcpu; |
| 1433 | int i; |
| 1434 | |
| 1435 | spin_lock(&kvm_lock); |
| 1436 | list_for_each_entry(kvm, &vm_list, vm_list) |
| 1437 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1438 | vcpu = kvm->vcpus[i]; |
| 1439 | if (vcpu) |
| 1440 | total += *(u32 *)((void *)vcpu + offset); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1441 | } |
| 1442 | spin_unlock(&kvm_lock); |
| 1443 | return total; |
| 1444 | } |
| 1445 | |
Rusty Russell | 3dea7ca | 2007-08-01 10:12:22 +1000 | [diff] [blame] | 1446 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1447 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1448 | static __init void kvm_init_debug(void) |
| 1449 | { |
| 1450 | struct kvm_stats_debugfs_item *p; |
| 1451 | |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1452 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1453 | for (p = debugfs_entries; p->name; ++p) |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1454 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
| 1455 | (void *)(long)p->offset, |
| 1456 | &stat_fops); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static void kvm_exit_debug(void) |
| 1460 | { |
| 1461 | struct kvm_stats_debugfs_item *p; |
| 1462 | |
| 1463 | for (p = debugfs_entries; p->name; ++p) |
| 1464 | debugfs_remove(p->dentry); |
| 1465 | debugfs_remove(debugfs_dir); |
| 1466 | } |
| 1467 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1468 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
| 1469 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1470 | hardware_disable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | static int kvm_resume(struct sys_device *dev) |
| 1475 | { |
Avi Kivity | 4267c41 | 2007-05-24 13:09:41 +0300 | [diff] [blame] | 1476 | hardware_enable(NULL); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | static struct sysdev_class kvm_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1481 | .name = "kvm", |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1482 | .suspend = kvm_suspend, |
| 1483 | .resume = kvm_resume, |
| 1484 | }; |
| 1485 | |
| 1486 | static struct sys_device kvm_sysdev = { |
| 1487 | .id = 0, |
| 1488 | .cls = &kvm_sysdev_class, |
| 1489 | }; |
| 1490 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1491 | struct page *bad_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1492 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1493 | static inline |
| 1494 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) |
| 1495 | { |
| 1496 | return container_of(pn, struct kvm_vcpu, preempt_notifier); |
| 1497 | } |
| 1498 | |
| 1499 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) |
| 1500 | { |
| 1501 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1502 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1503 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static void kvm_sched_out(struct preempt_notifier *pn, |
| 1507 | struct task_struct *next) |
| 1508 | { |
| 1509 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); |
| 1510 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1511 | kvm_x86_ops->vcpu_put(vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1512 | } |
| 1513 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1514 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1515 | struct module *module) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1516 | { |
| 1517 | int r; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1518 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1519 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1520 | if (kvm_x86_ops) { |
Yoshimi Ichiyanagi | 09db28b | 2006-12-29 16:49:41 -0800 | [diff] [blame] | 1521 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
| 1522 | return -EEXIST; |
| 1523 | } |
| 1524 | |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1525 | if (!ops->cpu_has_kvm_support()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1526 | printk(KERN_ERR "kvm: no hardware support\n"); |
| 1527 | return -EOPNOTSUPP; |
| 1528 | } |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1529 | if (ops->disabled_by_bios()) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1530 | printk(KERN_ERR "kvm: disabled by bios\n"); |
| 1531 | return -EOPNOTSUPP; |
| 1532 | } |
| 1533 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1534 | kvm_x86_ops = ops; |
Yoshimi Ichiyanagi | e097f35 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1535 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1536 | r = kvm_x86_ops->hardware_setup(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1537 | if (r < 0) |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1538 | goto out; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1539 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1540 | for_each_online_cpu(cpu) { |
| 1541 | smp_call_function_single(cpu, |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1542 | kvm_x86_ops->check_processor_compatibility, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1543 | &r, 0, 1); |
| 1544 | if (r < 0) |
| 1545 | goto out_free_0; |
| 1546 | } |
| 1547 | |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1548 | on_each_cpu(hardware_enable, NULL, 0, 1); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1549 | r = register_cpu_notifier(&kvm_cpu_notifier); |
| 1550 | if (r) |
| 1551 | goto out_free_1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1552 | register_reboot_notifier(&kvm_reboot_notifier); |
| 1553 | |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1554 | r = sysdev_class_register(&kvm_sysdev_class); |
| 1555 | if (r) |
| 1556 | goto out_free_2; |
| 1557 | |
| 1558 | r = sysdev_register(&kvm_sysdev); |
| 1559 | if (r) |
| 1560 | goto out_free_3; |
| 1561 | |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1562 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
| 1563 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, |
| 1564 | __alignof__(struct kvm_vcpu), 0, 0); |
| 1565 | if (!kvm_vcpu_cache) { |
| 1566 | r = -ENOMEM; |
| 1567 | goto out_free_4; |
| 1568 | } |
| 1569 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1570 | kvm_chardev_ops.owner = module; |
| 1571 | |
| 1572 | r = misc_register(&kvm_dev); |
| 1573 | if (r) { |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1574 | printk(KERN_ERR "kvm: misc device register failed\n"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1575 | goto out_free; |
| 1576 | } |
| 1577 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1578 | kvm_preempt_ops.sched_in = kvm_sched_in; |
| 1579 | kvm_preempt_ops.sched_out = kvm_sched_out; |
| 1580 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1581 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
| 1582 | |
| 1583 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1584 | |
| 1585 | out_free: |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1586 | kmem_cache_destroy(kvm_vcpu_cache); |
| 1587 | out_free_4: |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1588 | sysdev_unregister(&kvm_sysdev); |
| 1589 | out_free_3: |
| 1590 | sysdev_class_unregister(&kvm_sysdev_class); |
| 1591 | out_free_2: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1592 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 1593 | unregister_cpu_notifier(&kvm_cpu_notifier); |
| 1594 | out_free_1: |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1595 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1596 | out_free_0: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1597 | kvm_x86_ops->hardware_unsetup(); |
Avi Kivity | ca45aaa | 2007-03-01 19:21:03 +0200 | [diff] [blame] | 1598 | out: |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1599 | kvm_x86_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1600 | return r; |
| 1601 | } |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1602 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1603 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1604 | void kvm_exit_x86(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1605 | { |
| 1606 | misc_deregister(&kvm_dev); |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 1607 | kmem_cache_destroy(kvm_vcpu_cache); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1608 | sysdev_unregister(&kvm_sysdev); |
| 1609 | sysdev_class_unregister(&kvm_sysdev_class); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | unregister_reboot_notifier(&kvm_reboot_notifier); |
Avi Kivity | 59ae6c6 | 2007-02-12 00:54:48 -0800 | [diff] [blame] | 1611 | unregister_cpu_notifier(&kvm_cpu_notifier); |
Avi Kivity | 1b6c016 | 2007-05-24 13:03:52 +0300 | [diff] [blame] | 1612 | on_each_cpu(hardware_disable, NULL, 0, 1); |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1613 | kvm_x86_ops->hardware_unsetup(); |
| 1614 | kvm_x86_ops = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1615 | } |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1616 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1617 | |
| 1618 | static __init int kvm_init(void) |
| 1619 | { |
Avi Kivity | 37e29d9 | 2007-02-20 14:07:37 +0200 | [diff] [blame] | 1620 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1622 | r = kvm_mmu_module_init(); |
| 1623 | if (r) |
| 1624 | goto out4; |
| 1625 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1626 | kvm_init_debug(); |
| 1627 | |
Carsten Otte | 043405e | 2007-10-10 17:16:19 +0200 | [diff] [blame] | 1628 | kvm_arch_init(); |
Michael Riepe | bf591b2 | 2006-12-22 01:05:36 -0800 | [diff] [blame] | 1629 | |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1630 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1631 | |
| 1632 | if (bad_page == NULL) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1633 | r = -ENOMEM; |
| 1634 | goto out; |
| 1635 | } |
| 1636 | |
Avi Kivity | 58e690e | 2007-02-26 16:29:43 +0200 | [diff] [blame] | 1637 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1638 | |
| 1639 | out: |
| 1640 | kvm_exit_debug(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1641 | kvm_mmu_module_exit(); |
| 1642 | out4: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1643 | return r; |
| 1644 | } |
| 1645 | |
| 1646 | static __exit void kvm_exit(void) |
| 1647 | { |
| 1648 | kvm_exit_debug(); |
Izik Eidus | cea7bb2 | 2007-10-17 19:17:48 +0200 | [diff] [blame] | 1649 | __free_page(bad_page); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1650 | kvm_mmu_module_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | module_init(kvm_init) |
| 1654 | module_exit(kvm_exit) |