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