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 | ||
539cb660 IE |
562 | static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
563 | { | |
564 | struct kvm_memory_slot *slot; | |
565 | ||
566 | gfn = unalias_gfn(kvm, gfn); | |
567 | slot = __gfn_to_memslot(kvm, gfn); | |
568 | if (!slot) | |
569 | return bad_hva(); | |
570 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); | |
571 | } | |
572 | ||
aab61cc0 AL |
573 | /* |
574 | * Requires current->mm->mmap_sem to be held | |
575 | */ | |
576 | static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
954bbbc2 | 577 | { |
8d4e1288 | 578 | struct page *page[1]; |
539cb660 | 579 | unsigned long addr; |
8d4e1288 | 580 | int npages; |
954bbbc2 | 581 | |
60395224 AK |
582 | might_sleep(); |
583 | ||
539cb660 IE |
584 | addr = gfn_to_hva(kvm, gfn); |
585 | if (kvm_is_error_hva(addr)) { | |
8a7ae055 | 586 | get_page(bad_page); |
cea7bb21 | 587 | return bad_page; |
8a7ae055 | 588 | } |
8d4e1288 | 589 | |
539cb660 IE |
590 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
591 | NULL); | |
592 | ||
8d4e1288 AL |
593 | if (npages != 1) { |
594 | get_page(bad_page); | |
595 | return bad_page; | |
8a7ae055 | 596 | } |
8d4e1288 AL |
597 | |
598 | return page[0]; | |
954bbbc2 | 599 | } |
aab61cc0 AL |
600 | |
601 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
602 | { | |
603 | struct page *page; | |
604 | ||
605 | down_read(¤t->mm->mmap_sem); | |
606 | page = __gfn_to_page(kvm, gfn); | |
607 | up_read(¤t->mm->mmap_sem); | |
608 | ||
609 | return page; | |
610 | } | |
611 | ||
954bbbc2 AK |
612 | EXPORT_SYMBOL_GPL(gfn_to_page); |
613 | ||
8a7ae055 IE |
614 | void kvm_release_page(struct page *page) |
615 | { | |
616 | if (!PageReserved(page)) | |
617 | SetPageDirty(page); | |
618 | put_page(page); | |
619 | } | |
620 | EXPORT_SYMBOL_GPL(kvm_release_page); | |
621 | ||
195aefde IE |
622 | static int next_segment(unsigned long len, int offset) |
623 | { | |
624 | if (len > PAGE_SIZE - offset) | |
625 | return PAGE_SIZE - offset; | |
626 | else | |
627 | return len; | |
628 | } | |
629 | ||
630 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
631 | int len) | |
632 | { | |
633 | void *page_virt; | |
634 | struct page *page; | |
635 | ||
636 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
637 | if (is_error_page(page)) { |
638 | kvm_release_page(page); | |
195aefde | 639 | return -EFAULT; |
8a7ae055 | 640 | } |
195aefde IE |
641 | page_virt = kmap_atomic(page, KM_USER0); |
642 | ||
643 | memcpy(data, page_virt + offset, len); | |
644 | ||
645 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 646 | kvm_release_page(page); |
195aefde IE |
647 | return 0; |
648 | } | |
649 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
650 | ||
651 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
652 | { | |
653 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
654 | int seg; | |
655 | int offset = offset_in_page(gpa); | |
656 | int ret; | |
657 | ||
658 | while ((seg = next_segment(len, offset)) != 0) { | |
659 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
660 | if (ret < 0) | |
661 | return ret; | |
662 | offset = 0; | |
663 | len -= seg; | |
664 | data += seg; | |
665 | ++gfn; | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
670 | ||
671 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, | |
672 | int offset, int len) | |
673 | { | |
674 | void *page_virt; | |
675 | struct page *page; | |
676 | ||
677 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
678 | if (is_error_page(page)) { |
679 | kvm_release_page(page); | |
195aefde | 680 | return -EFAULT; |
8a7ae055 | 681 | } |
195aefde IE |
682 | page_virt = kmap_atomic(page, KM_USER0); |
683 | ||
684 | memcpy(page_virt + offset, data, len); | |
685 | ||
686 | kunmap_atomic(page_virt, KM_USER0); | |
687 | mark_page_dirty(kvm, gfn); | |
8a7ae055 | 688 | kvm_release_page(page); |
195aefde IE |
689 | return 0; |
690 | } | |
691 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
692 | ||
693 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
694 | unsigned long len) | |
695 | { | |
696 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
697 | int seg; | |
698 | int offset = offset_in_page(gpa); | |
699 | int ret; | |
700 | ||
701 | while ((seg = next_segment(len, offset)) != 0) { | |
702 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
703 | if (ret < 0) | |
704 | return ret; | |
705 | offset = 0; | |
706 | len -= seg; | |
707 | data += seg; | |
708 | ++gfn; | |
709 | } | |
710 | return 0; | |
711 | } | |
712 | ||
713 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
714 | { | |
715 | void *page_virt; | |
716 | struct page *page; | |
717 | ||
718 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
719 | if (is_error_page(page)) { |
720 | kvm_release_page(page); | |
195aefde | 721 | return -EFAULT; |
8a7ae055 | 722 | } |
195aefde IE |
723 | page_virt = kmap_atomic(page, KM_USER0); |
724 | ||
725 | memset(page_virt + offset, 0, len); | |
726 | ||
727 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 728 | kvm_release_page(page); |
12264760 | 729 | mark_page_dirty(kvm, gfn); |
195aefde IE |
730 | return 0; |
731 | } | |
732 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
733 | ||
734 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
735 | { | |
736 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
737 | int seg; | |
738 | int offset = offset_in_page(gpa); | |
739 | int ret; | |
740 | ||
741 | while ((seg = next_segment(len, offset)) != 0) { | |
742 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
743 | if (ret < 0) | |
744 | return ret; | |
745 | offset = 0; | |
746 | len -= seg; | |
747 | ++gfn; | |
748 | } | |
749 | return 0; | |
750 | } | |
751 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
752 | ||
6aa8b732 AK |
753 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
754 | { | |
31389947 | 755 | struct kvm_memory_slot *memslot; |
6aa8b732 | 756 | |
3b6fff19 | 757 | gfn = unalias_gfn(kvm, gfn); |
7e9d619d RR |
758 | memslot = __gfn_to_memslot(kvm, gfn); |
759 | if (memslot && memslot->dirty_bitmap) { | |
760 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 761 | |
7e9d619d RR |
762 | /* avoid RMW */ |
763 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
764 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
765 | } |
766 | } | |
767 | ||
b6958ce4 ED |
768 | /* |
769 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
770 | */ | |
8776e519 | 771 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 772 | { |
b6958ce4 ED |
773 | DECLARE_WAITQUEUE(wait, current); |
774 | ||
775 | add_wait_queue(&vcpu->wq, &wait); | |
776 | ||
777 | /* | |
778 | * We will block until either an interrupt or a signal wakes us up | |
779 | */ | |
c5ec1534 HQ |
780 | while (!kvm_cpu_has_interrupt(vcpu) |
781 | && !signal_pending(current) | |
782 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
783 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
784 | set_current_state(TASK_INTERRUPTIBLE); |
785 | vcpu_put(vcpu); | |
786 | schedule(); | |
787 | vcpu_load(vcpu); | |
788 | } | |
d3bef15f | 789 | |
c5ec1534 | 790 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 791 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
792 | } |
793 | ||
6aa8b732 AK |
794 | void kvm_resched(struct kvm_vcpu *vcpu) |
795 | { | |
3fca0365 YD |
796 | if (!need_resched()) |
797 | return; | |
6aa8b732 | 798 | cond_resched(); |
6aa8b732 AK |
799 | } |
800 | EXPORT_SYMBOL_GPL(kvm_resched); | |
801 | ||
6aa8b732 AK |
802 | /* |
803 | * Translate a guest virtual address to a guest physical address. | |
804 | */ | |
bccf2150 AK |
805 | static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
806 | struct kvm_translation *tr) | |
6aa8b732 AK |
807 | { |
808 | unsigned long vaddr = tr->linear_address; | |
6aa8b732 AK |
809 | gpa_t gpa; |
810 | ||
bccf2150 | 811 | vcpu_load(vcpu); |
11ec2804 | 812 | mutex_lock(&vcpu->kvm->lock); |
6aa8b732 AK |
813 | gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr); |
814 | tr->physical_address = gpa; | |
815 | tr->valid = gpa != UNMAPPED_GVA; | |
816 | tr->writeable = 1; | |
817 | tr->usermode = 0; | |
11ec2804 | 818 | mutex_unlock(&vcpu->kvm->lock); |
6aa8b732 AK |
819 | vcpu_put(vcpu); |
820 | ||
821 | return 0; | |
822 | } | |
823 | ||
bccf2150 AK |
824 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
825 | struct kvm_interrupt *irq) | |
6aa8b732 | 826 | { |
6aa8b732 AK |
827 | if (irq->irq < 0 || irq->irq >= 256) |
828 | return -EINVAL; | |
97222cc8 ED |
829 | if (irqchip_in_kernel(vcpu->kvm)) |
830 | return -ENXIO; | |
bccf2150 | 831 | vcpu_load(vcpu); |
6aa8b732 AK |
832 | |
833 | set_bit(irq->irq, vcpu->irq_pending); | |
834 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
835 | ||
836 | vcpu_put(vcpu); | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
9a2bb7f4 AK |
841 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
842 | unsigned long address, | |
843 | int *type) | |
844 | { | |
845 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
846 | unsigned long pgoff; | |
847 | struct page *page; | |
848 | ||
9a2bb7f4 | 849 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
850 | if (pgoff == 0) |
851 | page = virt_to_page(vcpu->run); | |
852 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
853 | page = virt_to_page(vcpu->pio_data); | |
854 | else | |
9a2bb7f4 | 855 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 856 | get_page(page); |
cd0d9137 NAQ |
857 | if (type != NULL) |
858 | *type = VM_FAULT_MINOR; | |
859 | ||
9a2bb7f4 AK |
860 | return page; |
861 | } | |
862 | ||
863 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
864 | .nopage = kvm_vcpu_nopage, | |
865 | }; | |
866 | ||
867 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
868 | { | |
869 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
870 | return 0; | |
871 | } | |
872 | ||
bccf2150 AK |
873 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
874 | { | |
875 | struct kvm_vcpu *vcpu = filp->private_data; | |
876 | ||
877 | fput(vcpu->kvm->filp); | |
878 | return 0; | |
879 | } | |
880 | ||
881 | static struct file_operations kvm_vcpu_fops = { | |
882 | .release = kvm_vcpu_release, | |
883 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
884 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 885 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
886 | }; |
887 | ||
888 | /* | |
889 | * Allocates an inode for the vcpu. | |
890 | */ | |
891 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
892 | { | |
893 | int fd, r; | |
894 | struct inode *inode; | |
895 | struct file *file; | |
896 | ||
d6d28168 AK |
897 | r = anon_inode_getfd(&fd, &inode, &file, |
898 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
899 | if (r) | |
900 | return r; | |
bccf2150 | 901 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 902 | return fd; |
bccf2150 AK |
903 | } |
904 | ||
c5ea7660 AK |
905 | /* |
906 | * Creates some virtual cpus. Good luck creating more than one. | |
907 | */ | |
908 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
909 | { | |
910 | int r; | |
911 | struct kvm_vcpu *vcpu; | |
912 | ||
c5ea7660 | 913 | if (!valid_vcpu(n)) |
fb3f0f51 | 914 | return -EINVAL; |
c5ea7660 | 915 | |
cbdd1bea | 916 | vcpu = kvm_x86_ops->vcpu_create(kvm, n); |
fb3f0f51 RR |
917 | if (IS_ERR(vcpu)) |
918 | return PTR_ERR(vcpu); | |
c5ea7660 | 919 | |
15ad7146 AK |
920 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
921 | ||
b114b080 RR |
922 | /* We do fxsave: this must be aligned. */ |
923 | BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF); | |
924 | ||
fb3f0f51 | 925 | vcpu_load(vcpu); |
e00c8cf2 AK |
926 | r = kvm_x86_ops->vcpu_reset(vcpu); |
927 | if (r == 0) | |
928 | r = kvm_mmu_setup(vcpu); | |
c5ea7660 | 929 | vcpu_put(vcpu); |
c5ea7660 | 930 | if (r < 0) |
fb3f0f51 RR |
931 | goto free_vcpu; |
932 | ||
11ec2804 | 933 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
934 | if (kvm->vcpus[n]) { |
935 | r = -EEXIST; | |
11ec2804 | 936 | mutex_unlock(&kvm->lock); |
fb3f0f51 RR |
937 | goto mmu_unload; |
938 | } | |
939 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 940 | mutex_unlock(&kvm->lock); |
c5ea7660 | 941 | |
fb3f0f51 | 942 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
943 | r = create_vcpu_fd(vcpu); |
944 | if (r < 0) | |
fb3f0f51 RR |
945 | goto unlink; |
946 | return r; | |
39c3b86e | 947 | |
fb3f0f51 | 948 | unlink: |
11ec2804 | 949 | mutex_lock(&kvm->lock); |
fb3f0f51 | 950 | kvm->vcpus[n] = NULL; |
11ec2804 | 951 | mutex_unlock(&kvm->lock); |
a2fa3e9f | 952 | |
fb3f0f51 RR |
953 | mmu_unload: |
954 | vcpu_load(vcpu); | |
955 | kvm_mmu_unload(vcpu); | |
956 | vcpu_put(vcpu); | |
c5ea7660 | 957 | |
fb3f0f51 | 958 | free_vcpu: |
cbdd1bea | 959 | kvm_x86_ops->vcpu_free(vcpu); |
c5ea7660 AK |
960 | return r; |
961 | } | |
962 | ||
1961d276 AK |
963 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
964 | { | |
965 | if (sigset) { | |
966 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
967 | vcpu->sigset_active = 1; | |
968 | vcpu->sigset = *sigset; | |
969 | } else | |
970 | vcpu->sigset_active = 0; | |
971 | return 0; | |
972 | } | |
973 | ||
bccf2150 AK |
974 | static long kvm_vcpu_ioctl(struct file *filp, |
975 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 976 | { |
bccf2150 | 977 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 978 | void __user *argp = (void __user *)arg; |
313a3dc7 | 979 | int r; |
6aa8b732 AK |
980 | |
981 | switch (ioctl) { | |
9a2bb7f4 | 982 | case KVM_RUN: |
f0fe5108 AK |
983 | r = -EINVAL; |
984 | if (arg) | |
985 | goto out; | |
b6c7a5dc | 986 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 987 | break; |
6aa8b732 AK |
988 | case KVM_GET_REGS: { |
989 | struct kvm_regs kvm_regs; | |
990 | ||
bccf2150 | 991 | memset(&kvm_regs, 0, sizeof kvm_regs); |
b6c7a5dc | 992 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
993 | if (r) |
994 | goto out; | |
995 | r = -EFAULT; | |
2f366987 | 996 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
997 | goto out; |
998 | r = 0; | |
999 | break; | |
1000 | } | |
1001 | case KVM_SET_REGS: { | |
1002 | struct kvm_regs kvm_regs; | |
1003 | ||
1004 | r = -EFAULT; | |
2f366987 | 1005 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 1006 | goto out; |
b6c7a5dc | 1007 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
1008 | if (r) |
1009 | goto out; | |
1010 | r = 0; | |
1011 | break; | |
1012 | } | |
1013 | case KVM_GET_SREGS: { | |
1014 | struct kvm_sregs kvm_sregs; | |
1015 | ||
bccf2150 | 1016 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
b6c7a5dc | 1017 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
1018 | if (r) |
1019 | goto out; | |
1020 | r = -EFAULT; | |
2f366987 | 1021 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
1022 | goto out; |
1023 | r = 0; | |
1024 | break; | |
1025 | } | |
1026 | case KVM_SET_SREGS: { | |
1027 | struct kvm_sregs kvm_sregs; | |
1028 | ||
1029 | r = -EFAULT; | |
2f366987 | 1030 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 1031 | goto out; |
b6c7a5dc | 1032 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
1033 | if (r) |
1034 | goto out; | |
1035 | r = 0; | |
1036 | break; | |
1037 | } | |
1038 | case KVM_TRANSLATE: { | |
1039 | struct kvm_translation tr; | |
1040 | ||
1041 | r = -EFAULT; | |
2f366987 | 1042 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 1043 | goto out; |
bccf2150 | 1044 | r = kvm_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
1045 | if (r) |
1046 | goto out; | |
1047 | r = -EFAULT; | |
2f366987 | 1048 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
1049 | goto out; |
1050 | r = 0; | |
1051 | break; | |
1052 | } | |
1053 | case KVM_INTERRUPT: { | |
1054 | struct kvm_interrupt irq; | |
1055 | ||
1056 | r = -EFAULT; | |
2f366987 | 1057 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 1058 | goto out; |
bccf2150 | 1059 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
1060 | if (r) |
1061 | goto out; | |
1062 | r = 0; | |
1063 | break; | |
1064 | } | |
1065 | case KVM_DEBUG_GUEST: { | |
1066 | struct kvm_debug_guest dbg; | |
1067 | ||
1068 | r = -EFAULT; | |
2f366987 | 1069 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 1070 | goto out; |
b6c7a5dc | 1071 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
1072 | if (r) |
1073 | goto out; | |
1074 | r = 0; | |
1075 | break; | |
1076 | } | |
1961d276 AK |
1077 | case KVM_SET_SIGNAL_MASK: { |
1078 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
1079 | struct kvm_signal_mask kvm_sigmask; | |
1080 | sigset_t sigset, *p; | |
1081 | ||
1082 | p = NULL; | |
1083 | if (argp) { | |
1084 | r = -EFAULT; | |
1085 | if (copy_from_user(&kvm_sigmask, argp, | |
1086 | sizeof kvm_sigmask)) | |
1087 | goto out; | |
1088 | r = -EINVAL; | |
1089 | if (kvm_sigmask.len != sizeof sigset) | |
1090 | goto out; | |
1091 | r = -EFAULT; | |
1092 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
1093 | sizeof sigset)) | |
1094 | goto out; | |
1095 | p = &sigset; | |
1096 | } | |
1097 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
1098 | break; | |
1099 | } | |
b8836737 AK |
1100 | case KVM_GET_FPU: { |
1101 | struct kvm_fpu fpu; | |
1102 | ||
1103 | memset(&fpu, 0, sizeof fpu); | |
d0752060 | 1104 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
b8836737 AK |
1105 | if (r) |
1106 | goto out; | |
1107 | r = -EFAULT; | |
1108 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
1109 | goto out; | |
1110 | r = 0; | |
1111 | break; | |
1112 | } | |
1113 | case KVM_SET_FPU: { | |
1114 | struct kvm_fpu fpu; | |
1115 | ||
1116 | r = -EFAULT; | |
1117 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
1118 | goto out; | |
d0752060 | 1119 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
b8836737 AK |
1120 | if (r) |
1121 | goto out; | |
1122 | r = 0; | |
1123 | break; | |
1124 | } | |
bccf2150 | 1125 | default: |
313a3dc7 | 1126 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
1127 | } |
1128 | out: | |
1129 | return r; | |
1130 | } | |
1131 | ||
1132 | static long kvm_vm_ioctl(struct file *filp, | |
1133 | unsigned int ioctl, unsigned long arg) | |
1134 | { | |
1135 | struct kvm *kvm = filp->private_data; | |
1136 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 1137 | int r; |
bccf2150 AK |
1138 | |
1139 | switch (ioctl) { | |
1140 | case KVM_CREATE_VCPU: | |
1141 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
1142 | if (r < 0) | |
1143 | goto out; | |
1144 | break; | |
6fc138d2 IE |
1145 | case KVM_SET_USER_MEMORY_REGION: { |
1146 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1147 | ||
1148 | r = -EFAULT; | |
1149 | if (copy_from_user(&kvm_userspace_mem, argp, | |
1150 | sizeof kvm_userspace_mem)) | |
1151 | goto out; | |
1152 | ||
1153 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
1154 | if (r) |
1155 | goto out; | |
1156 | break; | |
1157 | } | |
1158 | case KVM_GET_DIRTY_LOG: { | |
1159 | struct kvm_dirty_log log; | |
1160 | ||
1161 | r = -EFAULT; | |
2f366987 | 1162 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 1163 | goto out; |
2c6f5df9 | 1164 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
1165 | if (r) |
1166 | goto out; | |
1167 | break; | |
1168 | } | |
f17abe9a | 1169 | default: |
1fe779f8 | 1170 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
1171 | } |
1172 | out: | |
1173 | return r; | |
1174 | } | |
1175 | ||
1176 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
1177 | unsigned long address, | |
1178 | int *type) | |
1179 | { | |
1180 | struct kvm *kvm = vma->vm_file->private_data; | |
1181 | unsigned long pgoff; | |
f17abe9a AK |
1182 | struct page *page; |
1183 | ||
f17abe9a | 1184 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
e0d62c7f IE |
1185 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
1186 | return NOPAGE_SIGBUS; | |
aab61cc0 AL |
1187 | /* current->mm->mmap_sem is already held so call lockless version */ |
1188 | page = __gfn_to_page(kvm, pgoff); | |
8a7ae055 IE |
1189 | if (is_error_page(page)) { |
1190 | kvm_release_page(page); | |
f17abe9a | 1191 | return NOPAGE_SIGBUS; |
8a7ae055 | 1192 | } |
cd0d9137 NAQ |
1193 | if (type != NULL) |
1194 | *type = VM_FAULT_MINOR; | |
1195 | ||
f17abe9a AK |
1196 | return page; |
1197 | } | |
1198 | ||
1199 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
1200 | .nopage = kvm_vm_nopage, | |
1201 | }; | |
1202 | ||
1203 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
1204 | { | |
1205 | vma->vm_ops = &kvm_vm_vm_ops; | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | static struct file_operations kvm_vm_fops = { | |
1210 | .release = kvm_vm_release, | |
1211 | .unlocked_ioctl = kvm_vm_ioctl, | |
1212 | .compat_ioctl = kvm_vm_ioctl, | |
1213 | .mmap = kvm_vm_mmap, | |
1214 | }; | |
1215 | ||
1216 | static int kvm_dev_ioctl_create_vm(void) | |
1217 | { | |
1218 | int fd, r; | |
1219 | struct inode *inode; | |
1220 | struct file *file; | |
1221 | struct kvm *kvm; | |
1222 | ||
f17abe9a | 1223 | kvm = kvm_create_vm(); |
d6d28168 AK |
1224 | if (IS_ERR(kvm)) |
1225 | return PTR_ERR(kvm); | |
1226 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
1227 | if (r) { | |
1228 | kvm_destroy_vm(kvm); | |
1229 | return r; | |
f17abe9a AK |
1230 | } |
1231 | ||
bccf2150 | 1232 | kvm->filp = file; |
f17abe9a | 1233 | |
f17abe9a | 1234 | return fd; |
f17abe9a AK |
1235 | } |
1236 | ||
1237 | static long kvm_dev_ioctl(struct file *filp, | |
1238 | unsigned int ioctl, unsigned long arg) | |
1239 | { | |
1240 | void __user *argp = (void __user *)arg; | |
07c45a36 | 1241 | long r = -EINVAL; |
f17abe9a AK |
1242 | |
1243 | switch (ioctl) { | |
1244 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
1245 | r = -EINVAL; |
1246 | if (arg) | |
1247 | goto out; | |
f17abe9a AK |
1248 | r = KVM_API_VERSION; |
1249 | break; | |
1250 | case KVM_CREATE_VM: | |
f0fe5108 AK |
1251 | r = -EINVAL; |
1252 | if (arg) | |
1253 | goto out; | |
f17abe9a AK |
1254 | r = kvm_dev_ioctl_create_vm(); |
1255 | break; | |
85f455f7 ED |
1256 | case KVM_CHECK_EXTENSION: { |
1257 | int ext = (long)argp; | |
1258 | ||
1259 | switch (ext) { | |
1260 | case KVM_CAP_IRQCHIP: | |
b6958ce4 | 1261 | case KVM_CAP_HLT: |
82ce2c96 | 1262 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: |
6fc138d2 | 1263 | case KVM_CAP_USER_MEMORY: |
cbc94022 | 1264 | case KVM_CAP_SET_TSS_ADDR: |
85f455f7 ED |
1265 | r = 1; |
1266 | break; | |
1267 | default: | |
1268 | r = 0; | |
1269 | break; | |
1270 | } | |
5d308f45 | 1271 | break; |
85f455f7 | 1272 | } |
07c45a36 AK |
1273 | case KVM_GET_VCPU_MMAP_SIZE: |
1274 | r = -EINVAL; | |
1275 | if (arg) | |
1276 | goto out; | |
039576c0 | 1277 | r = 2 * PAGE_SIZE; |
07c45a36 | 1278 | break; |
6aa8b732 | 1279 | default: |
043405e1 | 1280 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
1281 | } |
1282 | out: | |
1283 | return r; | |
1284 | } | |
1285 | ||
6aa8b732 | 1286 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
1287 | .unlocked_ioctl = kvm_dev_ioctl, |
1288 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
1289 | }; |
1290 | ||
1291 | static struct miscdevice kvm_dev = { | |
bbe4432e | 1292 | KVM_MINOR, |
6aa8b732 AK |
1293 | "kvm", |
1294 | &kvm_chardev_ops, | |
1295 | }; | |
1296 | ||
774c47f1 AK |
1297 | /* |
1298 | * Make sure that a cpu that is being hot-unplugged does not have any vcpus | |
1299 | * cached on it. | |
1300 | */ | |
1301 | static void decache_vcpus_on_cpu(int cpu) | |
1302 | { | |
1303 | struct kvm *vm; | |
1304 | struct kvm_vcpu *vcpu; | |
1305 | int i; | |
1306 | ||
1307 | spin_lock(&kvm_lock); | |
11ec2804 | 1308 | list_for_each_entry(vm, &vm_list, vm_list) |
774c47f1 | 1309 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
fb3f0f51 RR |
1310 | vcpu = vm->vcpus[i]; |
1311 | if (!vcpu) | |
1312 | continue; | |
774c47f1 AK |
1313 | /* |
1314 | * If the vcpu is locked, then it is running on some | |
1315 | * other cpu and therefore it is not cached on the | |
1316 | * cpu in question. | |
1317 | * | |
1318 | * If it's not locked, check the last cpu it executed | |
1319 | * on. | |
1320 | */ | |
1321 | if (mutex_trylock(&vcpu->mutex)) { | |
1322 | if (vcpu->cpu == cpu) { | |
cbdd1bea | 1323 | kvm_x86_ops->vcpu_decache(vcpu); |
774c47f1 AK |
1324 | vcpu->cpu = -1; |
1325 | } | |
1326 | mutex_unlock(&vcpu->mutex); | |
1327 | } | |
1328 | } | |
1329 | spin_unlock(&kvm_lock); | |
1330 | } | |
1331 | ||
1b6c0168 AK |
1332 | static void hardware_enable(void *junk) |
1333 | { | |
1334 | int cpu = raw_smp_processor_id(); | |
1335 | ||
1336 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
1337 | return; | |
1338 | cpu_set(cpu, cpus_hardware_enabled); | |
cbdd1bea | 1339 | kvm_x86_ops->hardware_enable(NULL); |
1b6c0168 AK |
1340 | } |
1341 | ||
1342 | static void hardware_disable(void *junk) | |
1343 | { | |
1344 | int cpu = raw_smp_processor_id(); | |
1345 | ||
1346 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
1347 | return; | |
1348 | cpu_clear(cpu, cpus_hardware_enabled); | |
1349 | decache_vcpus_on_cpu(cpu); | |
cbdd1bea | 1350 | kvm_x86_ops->hardware_disable(NULL); |
1b6c0168 AK |
1351 | } |
1352 | ||
774c47f1 AK |
1353 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
1354 | void *v) | |
1355 | { | |
1356 | int cpu = (long)v; | |
1357 | ||
1a6f4d7f | 1358 | val &= ~CPU_TASKS_FROZEN; |
774c47f1 | 1359 | switch (val) { |
cec9ad27 | 1360 | case CPU_DYING: |
6ec8a856 AK |
1361 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1362 | cpu); | |
1363 | hardware_disable(NULL); | |
1364 | break; | |
774c47f1 | 1365 | case CPU_UP_CANCELED: |
43934a38 JK |
1366 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1367 | cpu); | |
1b6c0168 | 1368 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 1369 | break; |
43934a38 JK |
1370 | case CPU_ONLINE: |
1371 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", | |
1372 | cpu); | |
1b6c0168 | 1373 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1374 | break; |
1375 | } | |
1376 | return NOTIFY_OK; | |
1377 | } | |
1378 | ||
9a2b85c6 | 1379 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 1380 | void *v) |
9a2b85c6 RR |
1381 | { |
1382 | if (val == SYS_RESTART) { | |
1383 | /* | |
1384 | * Some (well, at least mine) BIOSes hang on reboot if | |
1385 | * in vmx root mode. | |
1386 | */ | |
1387 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
1388 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
1389 | } | |
1390 | return NOTIFY_OK; | |
1391 | } | |
1392 | ||
1393 | static struct notifier_block kvm_reboot_notifier = { | |
1394 | .notifier_call = kvm_reboot, | |
1395 | .priority = 0, | |
1396 | }; | |
1397 | ||
2eeb2e94 GH |
1398 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
1399 | { | |
1400 | memset(bus, 0, sizeof(*bus)); | |
1401 | } | |
1402 | ||
1403 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
1404 | { | |
1405 | int i; | |
1406 | ||
1407 | for (i = 0; i < bus->dev_count; i++) { | |
1408 | struct kvm_io_device *pos = bus->devs[i]; | |
1409 | ||
1410 | kvm_iodevice_destructor(pos); | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
1415 | { | |
1416 | int i; | |
1417 | ||
1418 | for (i = 0; i < bus->dev_count; i++) { | |
1419 | struct kvm_io_device *pos = bus->devs[i]; | |
1420 | ||
1421 | if (pos->in_range(pos, addr)) | |
1422 | return pos; | |
1423 | } | |
1424 | ||
1425 | return NULL; | |
1426 | } | |
1427 | ||
1428 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
1429 | { | |
1430 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
1431 | ||
1432 | bus->devs[bus->dev_count++] = dev; | |
1433 | } | |
1434 | ||
774c47f1 AK |
1435 | static struct notifier_block kvm_cpu_notifier = { |
1436 | .notifier_call = kvm_cpu_hotplug, | |
1437 | .priority = 20, /* must be > scheduler priority */ | |
1438 | }; | |
1439 | ||
1165f5fe AK |
1440 | static u64 stat_get(void *_offset) |
1441 | { | |
1442 | unsigned offset = (long)_offset; | |
1443 | u64 total = 0; | |
1444 | struct kvm *kvm; | |
1445 | struct kvm_vcpu *vcpu; | |
1446 | int i; | |
1447 | ||
1448 | spin_lock(&kvm_lock); | |
1449 | list_for_each_entry(kvm, &vm_list, vm_list) | |
1450 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
1451 | vcpu = kvm->vcpus[i]; |
1452 | if (vcpu) | |
1453 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
1454 | } |
1455 | spin_unlock(&kvm_lock); | |
1456 | return total; | |
1457 | } | |
1458 | ||
3dea7ca7 | 1459 | DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n"); |
1165f5fe | 1460 | |
6aa8b732 AK |
1461 | static __init void kvm_init_debug(void) |
1462 | { | |
1463 | struct kvm_stats_debugfs_item *p; | |
1464 | ||
8b6d44c7 | 1465 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 1466 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
1467 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
1468 | (void *)(long)p->offset, | |
1469 | &stat_fops); | |
6aa8b732 AK |
1470 | } |
1471 | ||
1472 | static void kvm_exit_debug(void) | |
1473 | { | |
1474 | struct kvm_stats_debugfs_item *p; | |
1475 | ||
1476 | for (p = debugfs_entries; p->name; ++p) | |
1477 | debugfs_remove(p->dentry); | |
1478 | debugfs_remove(debugfs_dir); | |
1479 | } | |
1480 | ||
59ae6c6b AK |
1481 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
1482 | { | |
4267c41a | 1483 | hardware_disable(NULL); |
59ae6c6b AK |
1484 | return 0; |
1485 | } | |
1486 | ||
1487 | static int kvm_resume(struct sys_device *dev) | |
1488 | { | |
4267c41a | 1489 | hardware_enable(NULL); |
59ae6c6b AK |
1490 | return 0; |
1491 | } | |
1492 | ||
1493 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 1494 | .name = "kvm", |
59ae6c6b AK |
1495 | .suspend = kvm_suspend, |
1496 | .resume = kvm_resume, | |
1497 | }; | |
1498 | ||
1499 | static struct sys_device kvm_sysdev = { | |
1500 | .id = 0, | |
1501 | .cls = &kvm_sysdev_class, | |
1502 | }; | |
1503 | ||
cea7bb21 | 1504 | struct page *bad_page; |
6aa8b732 | 1505 | |
15ad7146 AK |
1506 | static inline |
1507 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
1508 | { | |
1509 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
1510 | } | |
1511 | ||
1512 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
1513 | { | |
1514 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1515 | ||
cbdd1bea | 1516 | kvm_x86_ops->vcpu_load(vcpu, cpu); |
15ad7146 AK |
1517 | } |
1518 | ||
1519 | static void kvm_sched_out(struct preempt_notifier *pn, | |
1520 | struct task_struct *next) | |
1521 | { | |
1522 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1523 | ||
cbdd1bea | 1524 | kvm_x86_ops->vcpu_put(vcpu); |
15ad7146 AK |
1525 | } |
1526 | ||
cbdd1bea | 1527 | int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size, |
c16f862d | 1528 | struct module *module) |
6aa8b732 AK |
1529 | { |
1530 | int r; | |
002c7f7c | 1531 | int cpu; |
6aa8b732 | 1532 | |
cbdd1bea | 1533 | if (kvm_x86_ops) { |
09db28b8 YI |
1534 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
1535 | return -EEXIST; | |
1536 | } | |
1537 | ||
e097f35c | 1538 | if (!ops->cpu_has_kvm_support()) { |
6aa8b732 AK |
1539 | printk(KERN_ERR "kvm: no hardware support\n"); |
1540 | return -EOPNOTSUPP; | |
1541 | } | |
e097f35c | 1542 | if (ops->disabled_by_bios()) { |
6aa8b732 AK |
1543 | printk(KERN_ERR "kvm: disabled by bios\n"); |
1544 | return -EOPNOTSUPP; | |
1545 | } | |
1546 | ||
cbdd1bea | 1547 | kvm_x86_ops = ops; |
e097f35c | 1548 | |
cbdd1bea | 1549 | r = kvm_x86_ops->hardware_setup(); |
6aa8b732 | 1550 | if (r < 0) |
ca45aaae | 1551 | goto out; |
6aa8b732 | 1552 | |
002c7f7c YS |
1553 | for_each_online_cpu(cpu) { |
1554 | smp_call_function_single(cpu, | |
cbdd1bea | 1555 | kvm_x86_ops->check_processor_compatibility, |
002c7f7c YS |
1556 | &r, 0, 1); |
1557 | if (r < 0) | |
1558 | goto out_free_0; | |
1559 | } | |
1560 | ||
1b6c0168 | 1561 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1562 | r = register_cpu_notifier(&kvm_cpu_notifier); |
1563 | if (r) | |
1564 | goto out_free_1; | |
6aa8b732 AK |
1565 | register_reboot_notifier(&kvm_reboot_notifier); |
1566 | ||
59ae6c6b AK |
1567 | r = sysdev_class_register(&kvm_sysdev_class); |
1568 | if (r) | |
1569 | goto out_free_2; | |
1570 | ||
1571 | r = sysdev_register(&kvm_sysdev); | |
1572 | if (r) | |
1573 | goto out_free_3; | |
1574 | ||
c16f862d RR |
1575 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
1576 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
1577 | __alignof__(struct kvm_vcpu), 0, 0); | |
1578 | if (!kvm_vcpu_cache) { | |
1579 | r = -ENOMEM; | |
1580 | goto out_free_4; | |
1581 | } | |
1582 | ||
6aa8b732 AK |
1583 | kvm_chardev_ops.owner = module; |
1584 | ||
1585 | r = misc_register(&kvm_dev); | |
1586 | if (r) { | |
d77c26fc | 1587 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
1588 | goto out_free; |
1589 | } | |
1590 | ||
15ad7146 AK |
1591 | kvm_preempt_ops.sched_in = kvm_sched_in; |
1592 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
1593 | ||
c7addb90 AK |
1594 | kvm_mmu_set_nonpresent_ptes(0ull, 0ull); |
1595 | ||
1596 | return 0; | |
6aa8b732 AK |
1597 | |
1598 | out_free: | |
c16f862d RR |
1599 | kmem_cache_destroy(kvm_vcpu_cache); |
1600 | out_free_4: | |
59ae6c6b AK |
1601 | sysdev_unregister(&kvm_sysdev); |
1602 | out_free_3: | |
1603 | sysdev_class_unregister(&kvm_sysdev_class); | |
1604 | out_free_2: | |
6aa8b732 | 1605 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
1606 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1607 | out_free_1: | |
1b6c0168 | 1608 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 1609 | out_free_0: |
cbdd1bea | 1610 | kvm_x86_ops->hardware_unsetup(); |
ca45aaae | 1611 | out: |
cbdd1bea | 1612 | kvm_x86_ops = NULL; |
6aa8b732 AK |
1613 | return r; |
1614 | } | |
d77c26fc | 1615 | EXPORT_SYMBOL_GPL(kvm_init_x86); |
6aa8b732 | 1616 | |
cbdd1bea | 1617 | void kvm_exit_x86(void) |
6aa8b732 AK |
1618 | { |
1619 | misc_deregister(&kvm_dev); | |
c16f862d | 1620 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
1621 | sysdev_unregister(&kvm_sysdev); |
1622 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 1623 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 1624 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 1625 | on_each_cpu(hardware_disable, NULL, 0, 1); |
cbdd1bea CE |
1626 | kvm_x86_ops->hardware_unsetup(); |
1627 | kvm_x86_ops = NULL; | |
6aa8b732 | 1628 | } |
d77c26fc | 1629 | EXPORT_SYMBOL_GPL(kvm_exit_x86); |
6aa8b732 AK |
1630 | |
1631 | static __init int kvm_init(void) | |
1632 | { | |
37e29d90 AK |
1633 | int r; |
1634 | ||
b5a33a75 AK |
1635 | r = kvm_mmu_module_init(); |
1636 | if (r) | |
1637 | goto out4; | |
1638 | ||
6aa8b732 AK |
1639 | kvm_init_debug(); |
1640 | ||
043405e1 | 1641 | kvm_arch_init(); |
bf591b24 | 1642 | |
cea7bb21 | 1643 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
d77c26fc MD |
1644 | |
1645 | if (bad_page == NULL) { | |
6aa8b732 AK |
1646 | r = -ENOMEM; |
1647 | goto out; | |
1648 | } | |
1649 | ||
58e690e6 | 1650 | return 0; |
6aa8b732 AK |
1651 | |
1652 | out: | |
1653 | kvm_exit_debug(); | |
b5a33a75 AK |
1654 | kvm_mmu_module_exit(); |
1655 | out4: | |
6aa8b732 AK |
1656 | return r; |
1657 | } | |
1658 | ||
1659 | static __exit void kvm_exit(void) | |
1660 | { | |
1661 | kvm_exit_debug(); | |
cea7bb21 | 1662 | __free_page(bad_page); |
b5a33a75 | 1663 | kvm_mmu_module_exit(); |
6aa8b732 AK |
1664 | } |
1665 | ||
1666 | module_init(kvm_init) | |
1667 | module_exit(kvm_exit) |