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