]>
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" |
85f455f7 | 20 | #include "irq.h" |
6aa8b732 AK |
21 | |
22 | #include <linux/kvm.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/errno.h> | |
6aa8b732 AK |
25 | #include <linux/percpu.h> |
26 | #include <linux/gfp.h> | |
6aa8b732 AK |
27 | #include <linux/mm.h> |
28 | #include <linux/miscdevice.h> | |
29 | #include <linux/vmalloc.h> | |
6aa8b732 | 30 | #include <linux/reboot.h> |
6aa8b732 AK |
31 | #include <linux/debugfs.h> |
32 | #include <linux/highmem.h> | |
33 | #include <linux/file.h> | |
59ae6c6b | 34 | #include <linux/sysdev.h> |
774c47f1 | 35 | #include <linux/cpu.h> |
e8edc6e0 | 36 | #include <linux/sched.h> |
d9e368d6 AK |
37 | #include <linux/cpumask.h> |
38 | #include <linux/smp.h> | |
d6d28168 | 39 | #include <linux/anon_inodes.h> |
04d2cc77 | 40 | #include <linux/profile.h> |
7aa81cc0 | 41 | #include <linux/kvm_para.h> |
6fc138d2 | 42 | #include <linux/pagemap.h> |
8d4e1288 | 43 | #include <linux/mman.h> |
6aa8b732 | 44 | |
e495606d | 45 | #include <asm/processor.h> |
e495606d AK |
46 | #include <asm/io.h> |
47 | #include <asm/uaccess.h> | |
48 | #include <asm/desc.h> | |
6aa8b732 AK |
49 | |
50 | MODULE_AUTHOR("Qumranet"); | |
51 | MODULE_LICENSE("GPL"); | |
52 | ||
e9b11c17 ZX |
53 | DEFINE_SPINLOCK(kvm_lock); |
54 | LIST_HEAD(vm_list); | |
133de902 | 55 | |
1b6c0168 AK |
56 | static cpumask_t cpus_hardware_enabled; |
57 | ||
c16f862d RR |
58 | struct kmem_cache *kvm_vcpu_cache; |
59 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 60 | |
15ad7146 AK |
61 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
62 | ||
6aa8b732 AK |
63 | static struct dentry *debugfs_dir; |
64 | ||
bccf2150 AK |
65 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
66 | unsigned long arg); | |
67 | ||
5aacf0ca JM |
68 | static inline int valid_vcpu(int n) |
69 | { | |
70 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
71 | } | |
72 | ||
bccf2150 AK |
73 | /* |
74 | * Switches to specified vcpu, until a matching vcpu_put() | |
75 | */ | |
313a3dc7 | 76 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 77 | { |
15ad7146 AK |
78 | int cpu; |
79 | ||
bccf2150 | 80 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
81 | cpu = get_cpu(); |
82 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 83 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 84 | put_cpu(); |
6aa8b732 AK |
85 | } |
86 | ||
313a3dc7 | 87 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 88 | { |
15ad7146 | 89 | preempt_disable(); |
313a3dc7 | 90 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
91 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
92 | preempt_enable(); | |
6aa8b732 AK |
93 | mutex_unlock(&vcpu->mutex); |
94 | } | |
95 | ||
d9e368d6 AK |
96 | static void ack_flush(void *_completed) |
97 | { | |
d9e368d6 AK |
98 | } |
99 | ||
100 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
101 | { | |
49d3bd7e | 102 | int i, cpu; |
d9e368d6 AK |
103 | cpumask_t cpus; |
104 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 105 | |
d9e368d6 | 106 | cpus_clear(cpus); |
fb3f0f51 RR |
107 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
108 | vcpu = kvm->vcpus[i]; | |
109 | if (!vcpu) | |
110 | continue; | |
3176bc3e | 111 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
d9e368d6 AK |
112 | continue; |
113 | cpu = vcpu->cpu; | |
114 | if (cpu != -1 && cpu != raw_smp_processor_id()) | |
49d3bd7e | 115 | cpu_set(cpu, cpus); |
d9e368d6 | 116 | } |
49d3bd7e | 117 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
d9e368d6 AK |
118 | } |
119 | ||
fb3f0f51 RR |
120 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
121 | { | |
122 | struct page *page; | |
123 | int r; | |
124 | ||
125 | mutex_init(&vcpu->mutex); | |
126 | vcpu->cpu = -1; | |
fb3f0f51 RR |
127 | vcpu->kvm = kvm; |
128 | vcpu->vcpu_id = id; | |
b6958ce4 | 129 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
130 | |
131 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
132 | if (!page) { | |
133 | r = -ENOMEM; | |
134 | goto fail; | |
135 | } | |
136 | vcpu->run = page_address(page); | |
137 | ||
e9b11c17 | 138 | r = kvm_arch_vcpu_init(vcpu); |
fb3f0f51 | 139 | if (r < 0) |
e9b11c17 | 140 | goto fail_free_run; |
fb3f0f51 RR |
141 | return 0; |
142 | ||
fb3f0f51 RR |
143 | fail_free_run: |
144 | free_page((unsigned long)vcpu->run); | |
145 | fail: | |
76fafa5e | 146 | return r; |
fb3f0f51 RR |
147 | } |
148 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
149 | ||
150 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
151 | { | |
e9b11c17 | 152 | kvm_arch_vcpu_uninit(vcpu); |
fb3f0f51 RR |
153 | free_page((unsigned long)vcpu->run); |
154 | } | |
155 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
156 | ||
f17abe9a | 157 | static struct kvm *kvm_create_vm(void) |
6aa8b732 | 158 | { |
d19a9cd2 | 159 | struct kvm *kvm = kvm_arch_create_vm(); |
6aa8b732 | 160 | |
d19a9cd2 ZX |
161 | if (IS_ERR(kvm)) |
162 | goto out; | |
6aa8b732 | 163 | |
74906345 | 164 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 165 | mutex_init(&kvm->lock); |
2eeb2e94 | 166 | kvm_io_bus_init(&kvm->mmio_bus); |
5e58cfe4 RR |
167 | spin_lock(&kvm_lock); |
168 | list_add(&kvm->vm_list, &vm_list); | |
169 | spin_unlock(&kvm_lock); | |
d19a9cd2 | 170 | out: |
f17abe9a AK |
171 | return kvm; |
172 | } | |
173 | ||
6aa8b732 AK |
174 | /* |
175 | * Free any memory in @free but not in @dont. | |
176 | */ | |
177 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
178 | struct kvm_memory_slot *dont) | |
179 | { | |
290fc38d IE |
180 | if (!dont || free->rmap != dont->rmap) |
181 | vfree(free->rmap); | |
6aa8b732 AK |
182 | |
183 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
184 | vfree(free->dirty_bitmap); | |
185 | ||
6aa8b732 | 186 | free->npages = 0; |
8b6d44c7 | 187 | free->dirty_bitmap = NULL; |
8d4e1288 | 188 | free->rmap = NULL; |
6aa8b732 AK |
189 | } |
190 | ||
d19a9cd2 | 191 | void kvm_free_physmem(struct kvm *kvm) |
6aa8b732 AK |
192 | { |
193 | int i; | |
194 | ||
195 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 196 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
197 | } |
198 | ||
f17abe9a AK |
199 | static void kvm_destroy_vm(struct kvm *kvm) |
200 | { | |
133de902 AK |
201 | spin_lock(&kvm_lock); |
202 | list_del(&kvm->vm_list); | |
203 | spin_unlock(&kvm_lock); | |
74906345 | 204 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 205 | kvm_io_bus_destroy(&kvm->mmio_bus); |
d19a9cd2 | 206 | kvm_arch_destroy_vm(kvm); |
f17abe9a AK |
207 | } |
208 | ||
209 | static int kvm_vm_release(struct inode *inode, struct file *filp) | |
210 | { | |
211 | struct kvm *kvm = filp->private_data; | |
212 | ||
213 | kvm_destroy_vm(kvm); | |
6aa8b732 AK |
214 | return 0; |
215 | } | |
216 | ||
6aa8b732 AK |
217 | /* |
218 | * Allocate some memory and give it an address in the guest physical address | |
219 | * space. | |
220 | * | |
221 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e SY |
222 | * |
223 | * Must be called holding kvm->lock. | |
6aa8b732 | 224 | */ |
f78e0e2e SY |
225 | int __kvm_set_memory_region(struct kvm *kvm, |
226 | struct kvm_userspace_memory_region *mem, | |
227 | int user_alloc) | |
6aa8b732 AK |
228 | { |
229 | int r; | |
230 | gfn_t base_gfn; | |
231 | unsigned long npages; | |
232 | unsigned long i; | |
233 | struct kvm_memory_slot *memslot; | |
234 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
235 | |
236 | r = -EINVAL; | |
237 | /* General sanity checks */ | |
238 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
239 | goto out; | |
240 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
241 | goto out; | |
e0d62c7f | 242 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
6aa8b732 AK |
243 | goto out; |
244 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
245 | goto out; | |
246 | ||
247 | memslot = &kvm->memslots[mem->slot]; | |
248 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
249 | npages = mem->memory_size >> PAGE_SHIFT; | |
250 | ||
251 | if (!npages) | |
252 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
253 | ||
6aa8b732 AK |
254 | new = old = *memslot; |
255 | ||
256 | new.base_gfn = base_gfn; | |
257 | new.npages = npages; | |
258 | new.flags = mem->flags; | |
259 | ||
260 | /* Disallow changing a memory slot's size. */ | |
261 | r = -EINVAL; | |
262 | if (npages && old.npages && npages != old.npages) | |
f78e0e2e | 263 | goto out_free; |
6aa8b732 AK |
264 | |
265 | /* Check for overlaps */ | |
266 | r = -EEXIST; | |
267 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
268 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
269 | ||
270 | if (s == memslot) | |
271 | continue; | |
272 | if (!((base_gfn + npages <= s->base_gfn) || | |
273 | (base_gfn >= s->base_gfn + s->npages))) | |
f78e0e2e | 274 | goto out_free; |
6aa8b732 | 275 | } |
6aa8b732 | 276 | |
6aa8b732 AK |
277 | /* Free page dirty bitmap if unneeded */ |
278 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 279 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
280 | |
281 | r = -ENOMEM; | |
282 | ||
283 | /* Allocate if a slot is being created */ | |
8d4e1288 | 284 | if (npages && !new.rmap) { |
d77c26fc | 285 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
290fc38d IE |
286 | |
287 | if (!new.rmap) | |
f78e0e2e | 288 | goto out_free; |
290fc38d | 289 | |
290fc38d | 290 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
8d4e1288 | 291 | |
80b14b5b | 292 | new.user_alloc = user_alloc; |
8d4e1288 | 293 | if (user_alloc) |
8a7ae055 | 294 | new.userspace_addr = mem->userspace_addr; |
8d4e1288 AL |
295 | else { |
296 | down_write(¤t->mm->mmap_sem); | |
297 | new.userspace_addr = do_mmap(NULL, 0, | |
298 | npages * PAGE_SIZE, | |
299 | PROT_READ | PROT_WRITE, | |
300 | MAP_SHARED | MAP_ANONYMOUS, | |
301 | 0); | |
302 | up_write(¤t->mm->mmap_sem); | |
303 | ||
304 | if (IS_ERR((void *)new.userspace_addr)) | |
f78e0e2e | 305 | goto out_free; |
6aa8b732 | 306 | } |
80b14b5b IE |
307 | } else { |
308 | if (!old.user_alloc && old.rmap) { | |
309 | int ret; | |
310 | ||
311 | down_write(¤t->mm->mmap_sem); | |
312 | ret = do_munmap(current->mm, old.userspace_addr, | |
313 | old.npages * PAGE_SIZE); | |
314 | up_write(¤t->mm->mmap_sem); | |
315 | if (ret < 0) | |
316 | printk(KERN_WARNING | |
317 | "kvm_vm_ioctl_set_memory_region: " | |
318 | "failed to munmap memory\n"); | |
319 | } | |
6aa8b732 AK |
320 | } |
321 | ||
322 | /* Allocate page dirty bitmap if needed */ | |
323 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
324 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
325 | ||
326 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
327 | if (!new.dirty_bitmap) | |
f78e0e2e | 328 | goto out_free; |
6aa8b732 AK |
329 | memset(new.dirty_bitmap, 0, dirty_bytes); |
330 | } | |
331 | ||
6aa8b732 AK |
332 | if (mem->slot >= kvm->nmemslots) |
333 | kvm->nmemslots = mem->slot + 1; | |
334 | ||
82ce2c96 IE |
335 | if (!kvm->n_requested_mmu_pages) { |
336 | unsigned int n_pages; | |
337 | ||
338 | if (npages) { | |
339 | n_pages = npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
340 | kvm_mmu_change_mmu_pages(kvm, kvm->n_alloc_mmu_pages + | |
341 | n_pages); | |
342 | } else { | |
343 | unsigned int nr_mmu_pages; | |
344 | ||
345 | n_pages = old.npages * KVM_PERMILLE_MMU_PAGES / 1000; | |
346 | nr_mmu_pages = kvm->n_alloc_mmu_pages - n_pages; | |
347 | nr_mmu_pages = max(nr_mmu_pages, | |
348 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); | |
349 | kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages); | |
350 | } | |
351 | } | |
352 | ||
6aa8b732 | 353 | *memslot = new; |
6aa8b732 | 354 | |
90cb0529 AK |
355 | kvm_mmu_slot_remove_write_access(kvm, mem->slot); |
356 | kvm_flush_remote_tlbs(kvm); | |
6aa8b732 | 357 | |
6aa8b732 AK |
358 | kvm_free_physmem_slot(&old, &new); |
359 | return 0; | |
360 | ||
f78e0e2e | 361 | out_free: |
6aa8b732 AK |
362 | kvm_free_physmem_slot(&new, &old); |
363 | out: | |
364 | return r; | |
210c7c4d IE |
365 | |
366 | } | |
f78e0e2e SY |
367 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
368 | ||
369 | int kvm_set_memory_region(struct kvm *kvm, | |
370 | struct kvm_userspace_memory_region *mem, | |
371 | int user_alloc) | |
372 | { | |
373 | int r; | |
374 | ||
375 | mutex_lock(&kvm->lock); | |
376 | r = __kvm_set_memory_region(kvm, mem, user_alloc); | |
377 | mutex_unlock(&kvm->lock); | |
378 | return r; | |
379 | } | |
210c7c4d IE |
380 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
381 | ||
1fe779f8 CO |
382 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
383 | struct | |
384 | kvm_userspace_memory_region *mem, | |
385 | int user_alloc) | |
210c7c4d | 386 | { |
e0d62c7f IE |
387 | if (mem->slot >= KVM_MEMORY_SLOTS) |
388 | return -EINVAL; | |
210c7c4d | 389 | return kvm_set_memory_region(kvm, mem, user_alloc); |
6aa8b732 AK |
390 | } |
391 | ||
5bb064dc ZX |
392 | int kvm_get_dirty_log(struct kvm *kvm, |
393 | struct kvm_dirty_log *log, int *is_dirty) | |
6aa8b732 AK |
394 | { |
395 | struct kvm_memory_slot *memslot; | |
396 | int r, i; | |
397 | int n; | |
398 | unsigned long any = 0; | |
399 | ||
6aa8b732 AK |
400 | r = -EINVAL; |
401 | if (log->slot >= KVM_MEMORY_SLOTS) | |
402 | goto out; | |
403 | ||
404 | memslot = &kvm->memslots[log->slot]; | |
405 | r = -ENOENT; | |
406 | if (!memslot->dirty_bitmap) | |
407 | goto out; | |
408 | ||
cd1a4a98 | 409 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 410 | |
cd1a4a98 | 411 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
412 | any = memslot->dirty_bitmap[i]; |
413 | ||
414 | r = -EFAULT; | |
415 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
416 | goto out; | |
417 | ||
5bb064dc ZX |
418 | if (any) |
419 | *is_dirty = 1; | |
6aa8b732 AK |
420 | |
421 | r = 0; | |
6aa8b732 | 422 | out: |
6aa8b732 AK |
423 | return r; |
424 | } | |
425 | ||
cea7bb21 IE |
426 | int is_error_page(struct page *page) |
427 | { | |
428 | return page == bad_page; | |
429 | } | |
430 | EXPORT_SYMBOL_GPL(is_error_page); | |
431 | ||
f9d46eb0 IE |
432 | static inline unsigned long bad_hva(void) |
433 | { | |
434 | return PAGE_OFFSET; | |
435 | } | |
436 | ||
437 | int kvm_is_error_hva(unsigned long addr) | |
438 | { | |
439 | return addr == bad_hva(); | |
440 | } | |
441 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); | |
442 | ||
290fc38d | 443 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
e8207547 AK |
444 | { |
445 | int i; | |
446 | struct kvm_mem_alias *alias; | |
447 | ||
448 | for (i = 0; i < kvm->naliases; ++i) { | |
449 | alias = &kvm->aliases[i]; | |
450 | if (gfn >= alias->base_gfn | |
451 | && gfn < alias->base_gfn + alias->npages) | |
452 | return alias->target_gfn + gfn - alias->base_gfn; | |
453 | } | |
454 | return gfn; | |
455 | } | |
456 | ||
457 | static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
6aa8b732 AK |
458 | { |
459 | int i; | |
460 | ||
461 | for (i = 0; i < kvm->nmemslots; ++i) { | |
462 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
463 | ||
464 | if (gfn >= memslot->base_gfn | |
465 | && gfn < memslot->base_gfn + memslot->npages) | |
466 | return memslot; | |
467 | } | |
8b6d44c7 | 468 | return NULL; |
6aa8b732 | 469 | } |
e8207547 AK |
470 | |
471 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
472 | { | |
473 | gfn = unalias_gfn(kvm, gfn); | |
474 | return __gfn_to_memslot(kvm, gfn); | |
475 | } | |
6aa8b732 | 476 | |
e0d62c7f IE |
477 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
478 | { | |
479 | int i; | |
480 | ||
481 | gfn = unalias_gfn(kvm, gfn); | |
482 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
483 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
484 | ||
485 | if (gfn >= memslot->base_gfn | |
486 | && gfn < memslot->base_gfn + memslot->npages) | |
487 | return 1; | |
488 | } | |
489 | return 0; | |
490 | } | |
491 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
492 | ||
539cb660 IE |
493 | static unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
494 | { | |
495 | struct kvm_memory_slot *slot; | |
496 | ||
497 | gfn = unalias_gfn(kvm, gfn); | |
498 | slot = __gfn_to_memslot(kvm, gfn); | |
499 | if (!slot) | |
500 | return bad_hva(); | |
501 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); | |
502 | } | |
503 | ||
aab61cc0 AL |
504 | /* |
505 | * Requires current->mm->mmap_sem to be held | |
506 | */ | |
507 | static struct page *__gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
954bbbc2 | 508 | { |
8d4e1288 | 509 | struct page *page[1]; |
539cb660 | 510 | unsigned long addr; |
8d4e1288 | 511 | int npages; |
954bbbc2 | 512 | |
60395224 AK |
513 | might_sleep(); |
514 | ||
539cb660 IE |
515 | addr = gfn_to_hva(kvm, gfn); |
516 | if (kvm_is_error_hva(addr)) { | |
8a7ae055 | 517 | get_page(bad_page); |
cea7bb21 | 518 | return bad_page; |
8a7ae055 | 519 | } |
8d4e1288 | 520 | |
539cb660 IE |
521 | npages = get_user_pages(current, current->mm, addr, 1, 1, 1, page, |
522 | NULL); | |
523 | ||
8d4e1288 AL |
524 | if (npages != 1) { |
525 | get_page(bad_page); | |
526 | return bad_page; | |
8a7ae055 | 527 | } |
8d4e1288 AL |
528 | |
529 | return page[0]; | |
954bbbc2 | 530 | } |
aab61cc0 AL |
531 | |
532 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
533 | { | |
534 | struct page *page; | |
535 | ||
536 | down_read(¤t->mm->mmap_sem); | |
537 | page = __gfn_to_page(kvm, gfn); | |
538 | up_read(¤t->mm->mmap_sem); | |
539 | ||
540 | return page; | |
541 | } | |
542 | ||
954bbbc2 AK |
543 | EXPORT_SYMBOL_GPL(gfn_to_page); |
544 | ||
8a7ae055 IE |
545 | void kvm_release_page(struct page *page) |
546 | { | |
547 | if (!PageReserved(page)) | |
548 | SetPageDirty(page); | |
549 | put_page(page); | |
550 | } | |
551 | EXPORT_SYMBOL_GPL(kvm_release_page); | |
552 | ||
195aefde IE |
553 | static int next_segment(unsigned long len, int offset) |
554 | { | |
555 | if (len > PAGE_SIZE - offset) | |
556 | return PAGE_SIZE - offset; | |
557 | else | |
558 | return len; | |
559 | } | |
560 | ||
561 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
562 | int len) | |
563 | { | |
e0506bcb IE |
564 | int r; |
565 | unsigned long addr; | |
195aefde | 566 | |
e0506bcb IE |
567 | addr = gfn_to_hva(kvm, gfn); |
568 | if (kvm_is_error_hva(addr)) | |
569 | return -EFAULT; | |
570 | r = copy_from_user(data, (void __user *)addr + offset, len); | |
571 | if (r) | |
195aefde | 572 | return -EFAULT; |
195aefde IE |
573 | return 0; |
574 | } | |
575 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
576 | ||
577 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
578 | { | |
579 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
580 | int seg; | |
581 | int offset = offset_in_page(gpa); | |
582 | int ret; | |
583 | ||
584 | while ((seg = next_segment(len, offset)) != 0) { | |
585 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
586 | if (ret < 0) | |
587 | return ret; | |
588 | offset = 0; | |
589 | len -= seg; | |
590 | data += seg; | |
591 | ++gfn; | |
592 | } | |
593 | return 0; | |
594 | } | |
595 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
596 | ||
597 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, | |
598 | int offset, int len) | |
599 | { | |
e0506bcb IE |
600 | int r; |
601 | unsigned long addr; | |
195aefde | 602 | |
e0506bcb IE |
603 | addr = gfn_to_hva(kvm, gfn); |
604 | if (kvm_is_error_hva(addr)) | |
605 | return -EFAULT; | |
606 | r = copy_to_user((void __user *)addr + offset, data, len); | |
607 | if (r) | |
195aefde | 608 | return -EFAULT; |
195aefde IE |
609 | mark_page_dirty(kvm, gfn); |
610 | return 0; | |
611 | } | |
612 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
613 | ||
614 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
615 | unsigned long len) | |
616 | { | |
617 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
618 | int seg; | |
619 | int offset = offset_in_page(gpa); | |
620 | int ret; | |
621 | ||
622 | while ((seg = next_segment(len, offset)) != 0) { | |
623 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
624 | if (ret < 0) | |
625 | return ret; | |
626 | offset = 0; | |
627 | len -= seg; | |
628 | data += seg; | |
629 | ++gfn; | |
630 | } | |
631 | return 0; | |
632 | } | |
633 | ||
634 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
635 | { | |
636 | void *page_virt; | |
637 | struct page *page; | |
638 | ||
639 | page = gfn_to_page(kvm, gfn); | |
8a7ae055 IE |
640 | if (is_error_page(page)) { |
641 | kvm_release_page(page); | |
195aefde | 642 | return -EFAULT; |
8a7ae055 | 643 | } |
195aefde IE |
644 | page_virt = kmap_atomic(page, KM_USER0); |
645 | ||
646 | memset(page_virt + offset, 0, len); | |
647 | ||
648 | kunmap_atomic(page_virt, KM_USER0); | |
8a7ae055 | 649 | kvm_release_page(page); |
12264760 | 650 | mark_page_dirty(kvm, gfn); |
195aefde IE |
651 | return 0; |
652 | } | |
653 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
654 | ||
655 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
656 | { | |
657 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
658 | int seg; | |
659 | int offset = offset_in_page(gpa); | |
660 | int ret; | |
661 | ||
662 | while ((seg = next_segment(len, offset)) != 0) { | |
663 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
664 | if (ret < 0) | |
665 | return ret; | |
666 | offset = 0; | |
667 | len -= seg; | |
668 | ++gfn; | |
669 | } | |
670 | return 0; | |
671 | } | |
672 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
673 | ||
6aa8b732 AK |
674 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
675 | { | |
31389947 | 676 | struct kvm_memory_slot *memslot; |
6aa8b732 | 677 | |
3b6fff19 | 678 | gfn = unalias_gfn(kvm, gfn); |
7e9d619d RR |
679 | memslot = __gfn_to_memslot(kvm, gfn); |
680 | if (memslot && memslot->dirty_bitmap) { | |
681 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 682 | |
7e9d619d RR |
683 | /* avoid RMW */ |
684 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
685 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
686 | } |
687 | } | |
688 | ||
b6958ce4 ED |
689 | /* |
690 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
691 | */ | |
8776e519 | 692 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 693 | { |
b6958ce4 ED |
694 | DECLARE_WAITQUEUE(wait, current); |
695 | ||
696 | add_wait_queue(&vcpu->wq, &wait); | |
697 | ||
698 | /* | |
699 | * We will block until either an interrupt or a signal wakes us up | |
700 | */ | |
c5ec1534 HQ |
701 | while (!kvm_cpu_has_interrupt(vcpu) |
702 | && !signal_pending(current) | |
703 | && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE | |
704 | && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) { | |
b6958ce4 ED |
705 | set_current_state(TASK_INTERRUPTIBLE); |
706 | vcpu_put(vcpu); | |
707 | schedule(); | |
708 | vcpu_load(vcpu); | |
709 | } | |
d3bef15f | 710 | |
c5ec1534 | 711 | __set_current_state(TASK_RUNNING); |
b6958ce4 | 712 | remove_wait_queue(&vcpu->wq, &wait); |
b6958ce4 ED |
713 | } |
714 | ||
6aa8b732 AK |
715 | void kvm_resched(struct kvm_vcpu *vcpu) |
716 | { | |
3fca0365 YD |
717 | if (!need_resched()) |
718 | return; | |
6aa8b732 | 719 | cond_resched(); |
6aa8b732 AK |
720 | } |
721 | EXPORT_SYMBOL_GPL(kvm_resched); | |
722 | ||
bccf2150 AK |
723 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
724 | struct kvm_interrupt *irq) | |
6aa8b732 | 725 | { |
6aa8b732 AK |
726 | if (irq->irq < 0 || irq->irq >= 256) |
727 | return -EINVAL; | |
97222cc8 ED |
728 | if (irqchip_in_kernel(vcpu->kvm)) |
729 | return -ENXIO; | |
bccf2150 | 730 | vcpu_load(vcpu); |
6aa8b732 AK |
731 | |
732 | set_bit(irq->irq, vcpu->irq_pending); | |
733 | set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary); | |
734 | ||
735 | vcpu_put(vcpu); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
9a2bb7f4 AK |
740 | static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma, |
741 | unsigned long address, | |
742 | int *type) | |
743 | { | |
744 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
745 | unsigned long pgoff; | |
746 | struct page *page; | |
747 | ||
9a2bb7f4 | 748 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
039576c0 AK |
749 | if (pgoff == 0) |
750 | page = virt_to_page(vcpu->run); | |
751 | else if (pgoff == KVM_PIO_PAGE_OFFSET) | |
752 | page = virt_to_page(vcpu->pio_data); | |
753 | else | |
9a2bb7f4 | 754 | return NOPAGE_SIGBUS; |
9a2bb7f4 | 755 | get_page(page); |
cd0d9137 NAQ |
756 | if (type != NULL) |
757 | *type = VM_FAULT_MINOR; | |
758 | ||
9a2bb7f4 AK |
759 | return page; |
760 | } | |
761 | ||
762 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
763 | .nopage = kvm_vcpu_nopage, | |
764 | }; | |
765 | ||
766 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
767 | { | |
768 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
769 | return 0; | |
770 | } | |
771 | ||
bccf2150 AK |
772 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
773 | { | |
774 | struct kvm_vcpu *vcpu = filp->private_data; | |
775 | ||
776 | fput(vcpu->kvm->filp); | |
777 | return 0; | |
778 | } | |
779 | ||
780 | static struct file_operations kvm_vcpu_fops = { | |
781 | .release = kvm_vcpu_release, | |
782 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
783 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 784 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
785 | }; |
786 | ||
787 | /* | |
788 | * Allocates an inode for the vcpu. | |
789 | */ | |
790 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
791 | { | |
792 | int fd, r; | |
793 | struct inode *inode; | |
794 | struct file *file; | |
795 | ||
d6d28168 AK |
796 | r = anon_inode_getfd(&fd, &inode, &file, |
797 | "kvm-vcpu", &kvm_vcpu_fops, vcpu); | |
798 | if (r) | |
799 | return r; | |
bccf2150 | 800 | atomic_inc(&vcpu->kvm->filp->f_count); |
bccf2150 | 801 | return fd; |
bccf2150 AK |
802 | } |
803 | ||
c5ea7660 AK |
804 | /* |
805 | * Creates some virtual cpus. Good luck creating more than one. | |
806 | */ | |
807 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
808 | { | |
809 | int r; | |
810 | struct kvm_vcpu *vcpu; | |
811 | ||
c5ea7660 | 812 | if (!valid_vcpu(n)) |
fb3f0f51 | 813 | return -EINVAL; |
c5ea7660 | 814 | |
e9b11c17 | 815 | vcpu = kvm_arch_vcpu_create(kvm, n); |
fb3f0f51 RR |
816 | if (IS_ERR(vcpu)) |
817 | return PTR_ERR(vcpu); | |
c5ea7660 | 818 | |
15ad7146 AK |
819 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
820 | ||
11ec2804 | 821 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
822 | if (kvm->vcpus[n]) { |
823 | r = -EEXIST; | |
11ec2804 | 824 | mutex_unlock(&kvm->lock); |
e9b11c17 | 825 | goto vcpu_destroy; |
fb3f0f51 RR |
826 | } |
827 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 828 | mutex_unlock(&kvm->lock); |
c5ea7660 | 829 | |
fb3f0f51 | 830 | /* Now it's all set up, let userspace reach it */ |
bccf2150 AK |
831 | r = create_vcpu_fd(vcpu); |
832 | if (r < 0) | |
fb3f0f51 RR |
833 | goto unlink; |
834 | return r; | |
39c3b86e | 835 | |
fb3f0f51 | 836 | unlink: |
11ec2804 | 837 | mutex_lock(&kvm->lock); |
fb3f0f51 | 838 | kvm->vcpus[n] = NULL; |
11ec2804 | 839 | mutex_unlock(&kvm->lock); |
e9b11c17 ZX |
840 | vcpu_destroy: |
841 | kvm_arch_vcpu_destory(vcpu); | |
c5ea7660 AK |
842 | return r; |
843 | } | |
844 | ||
1961d276 AK |
845 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
846 | { | |
847 | if (sigset) { | |
848 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
849 | vcpu->sigset_active = 1; | |
850 | vcpu->sigset = *sigset; | |
851 | } else | |
852 | vcpu->sigset_active = 0; | |
853 | return 0; | |
854 | } | |
855 | ||
bccf2150 AK |
856 | static long kvm_vcpu_ioctl(struct file *filp, |
857 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 858 | { |
bccf2150 | 859 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 860 | void __user *argp = (void __user *)arg; |
313a3dc7 | 861 | int r; |
6aa8b732 AK |
862 | |
863 | switch (ioctl) { | |
9a2bb7f4 | 864 | case KVM_RUN: |
f0fe5108 AK |
865 | r = -EINVAL; |
866 | if (arg) | |
867 | goto out; | |
b6c7a5dc | 868 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 869 | break; |
6aa8b732 AK |
870 | case KVM_GET_REGS: { |
871 | struct kvm_regs kvm_regs; | |
872 | ||
bccf2150 | 873 | memset(&kvm_regs, 0, sizeof kvm_regs); |
b6c7a5dc | 874 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
875 | if (r) |
876 | goto out; | |
877 | r = -EFAULT; | |
2f366987 | 878 | if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs)) |
6aa8b732 AK |
879 | goto out; |
880 | r = 0; | |
881 | break; | |
882 | } | |
883 | case KVM_SET_REGS: { | |
884 | struct kvm_regs kvm_regs; | |
885 | ||
886 | r = -EFAULT; | |
2f366987 | 887 | if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs)) |
6aa8b732 | 888 | goto out; |
b6c7a5dc | 889 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, &kvm_regs); |
6aa8b732 AK |
890 | if (r) |
891 | goto out; | |
892 | r = 0; | |
893 | break; | |
894 | } | |
895 | case KVM_GET_SREGS: { | |
896 | struct kvm_sregs kvm_sregs; | |
897 | ||
bccf2150 | 898 | memset(&kvm_sregs, 0, sizeof kvm_sregs); |
b6c7a5dc | 899 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
900 | if (r) |
901 | goto out; | |
902 | r = -EFAULT; | |
2f366987 | 903 | if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs)) |
6aa8b732 AK |
904 | goto out; |
905 | r = 0; | |
906 | break; | |
907 | } | |
908 | case KVM_SET_SREGS: { | |
909 | struct kvm_sregs kvm_sregs; | |
910 | ||
911 | r = -EFAULT; | |
2f366987 | 912 | if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs)) |
6aa8b732 | 913 | goto out; |
b6c7a5dc | 914 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs); |
6aa8b732 AK |
915 | if (r) |
916 | goto out; | |
917 | r = 0; | |
918 | break; | |
919 | } | |
920 | case KVM_TRANSLATE: { | |
921 | struct kvm_translation tr; | |
922 | ||
923 | r = -EFAULT; | |
2f366987 | 924 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 925 | goto out; |
8b006791 | 926 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
927 | if (r) |
928 | goto out; | |
929 | r = -EFAULT; | |
2f366987 | 930 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
931 | goto out; |
932 | r = 0; | |
933 | break; | |
934 | } | |
935 | case KVM_INTERRUPT: { | |
936 | struct kvm_interrupt irq; | |
937 | ||
938 | r = -EFAULT; | |
2f366987 | 939 | if (copy_from_user(&irq, argp, sizeof irq)) |
6aa8b732 | 940 | goto out; |
bccf2150 | 941 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
6aa8b732 AK |
942 | if (r) |
943 | goto out; | |
944 | r = 0; | |
945 | break; | |
946 | } | |
947 | case KVM_DEBUG_GUEST: { | |
948 | struct kvm_debug_guest dbg; | |
949 | ||
950 | r = -EFAULT; | |
2f366987 | 951 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 952 | goto out; |
b6c7a5dc | 953 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
954 | if (r) |
955 | goto out; | |
956 | r = 0; | |
957 | break; | |
958 | } | |
1961d276 AK |
959 | case KVM_SET_SIGNAL_MASK: { |
960 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
961 | struct kvm_signal_mask kvm_sigmask; | |
962 | sigset_t sigset, *p; | |
963 | ||
964 | p = NULL; | |
965 | if (argp) { | |
966 | r = -EFAULT; | |
967 | if (copy_from_user(&kvm_sigmask, argp, | |
968 | sizeof kvm_sigmask)) | |
969 | goto out; | |
970 | r = -EINVAL; | |
971 | if (kvm_sigmask.len != sizeof sigset) | |
972 | goto out; | |
973 | r = -EFAULT; | |
974 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
975 | sizeof sigset)) | |
976 | goto out; | |
977 | p = &sigset; | |
978 | } | |
979 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
980 | break; | |
981 | } | |
b8836737 AK |
982 | case KVM_GET_FPU: { |
983 | struct kvm_fpu fpu; | |
984 | ||
985 | memset(&fpu, 0, sizeof fpu); | |
d0752060 | 986 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, &fpu); |
b8836737 AK |
987 | if (r) |
988 | goto out; | |
989 | r = -EFAULT; | |
990 | if (copy_to_user(argp, &fpu, sizeof fpu)) | |
991 | goto out; | |
992 | r = 0; | |
993 | break; | |
994 | } | |
995 | case KVM_SET_FPU: { | |
996 | struct kvm_fpu fpu; | |
997 | ||
998 | r = -EFAULT; | |
999 | if (copy_from_user(&fpu, argp, sizeof fpu)) | |
1000 | goto out; | |
d0752060 | 1001 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, &fpu); |
b8836737 AK |
1002 | if (r) |
1003 | goto out; | |
1004 | r = 0; | |
1005 | break; | |
1006 | } | |
bccf2150 | 1007 | default: |
313a3dc7 | 1008 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
1009 | } |
1010 | out: | |
1011 | return r; | |
1012 | } | |
1013 | ||
1014 | static long kvm_vm_ioctl(struct file *filp, | |
1015 | unsigned int ioctl, unsigned long arg) | |
1016 | { | |
1017 | struct kvm *kvm = filp->private_data; | |
1018 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 1019 | int r; |
bccf2150 AK |
1020 | |
1021 | switch (ioctl) { | |
1022 | case KVM_CREATE_VCPU: | |
1023 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
1024 | if (r < 0) | |
1025 | goto out; | |
1026 | break; | |
6fc138d2 IE |
1027 | case KVM_SET_USER_MEMORY_REGION: { |
1028 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1029 | ||
1030 | r = -EFAULT; | |
1031 | if (copy_from_user(&kvm_userspace_mem, argp, | |
1032 | sizeof kvm_userspace_mem)) | |
1033 | goto out; | |
1034 | ||
1035 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
1036 | if (r) |
1037 | goto out; | |
1038 | break; | |
1039 | } | |
1040 | case KVM_GET_DIRTY_LOG: { | |
1041 | struct kvm_dirty_log log; | |
1042 | ||
1043 | r = -EFAULT; | |
2f366987 | 1044 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 1045 | goto out; |
2c6f5df9 | 1046 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
1047 | if (r) |
1048 | goto out; | |
1049 | break; | |
1050 | } | |
f17abe9a | 1051 | default: |
1fe779f8 | 1052 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
1053 | } |
1054 | out: | |
1055 | return r; | |
1056 | } | |
1057 | ||
1058 | static struct page *kvm_vm_nopage(struct vm_area_struct *vma, | |
1059 | unsigned long address, | |
1060 | int *type) | |
1061 | { | |
1062 | struct kvm *kvm = vma->vm_file->private_data; | |
1063 | unsigned long pgoff; | |
f17abe9a AK |
1064 | struct page *page; |
1065 | ||
f17abe9a | 1066 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
e0d62c7f IE |
1067 | if (!kvm_is_visible_gfn(kvm, pgoff)) |
1068 | return NOPAGE_SIGBUS; | |
aab61cc0 AL |
1069 | /* current->mm->mmap_sem is already held so call lockless version */ |
1070 | page = __gfn_to_page(kvm, pgoff); | |
8a7ae055 IE |
1071 | if (is_error_page(page)) { |
1072 | kvm_release_page(page); | |
f17abe9a | 1073 | return NOPAGE_SIGBUS; |
8a7ae055 | 1074 | } |
cd0d9137 NAQ |
1075 | if (type != NULL) |
1076 | *type = VM_FAULT_MINOR; | |
1077 | ||
f17abe9a AK |
1078 | return page; |
1079 | } | |
1080 | ||
1081 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
1082 | .nopage = kvm_vm_nopage, | |
1083 | }; | |
1084 | ||
1085 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
1086 | { | |
1087 | vma->vm_ops = &kvm_vm_vm_ops; | |
1088 | return 0; | |
1089 | } | |
1090 | ||
1091 | static struct file_operations kvm_vm_fops = { | |
1092 | .release = kvm_vm_release, | |
1093 | .unlocked_ioctl = kvm_vm_ioctl, | |
1094 | .compat_ioctl = kvm_vm_ioctl, | |
1095 | .mmap = kvm_vm_mmap, | |
1096 | }; | |
1097 | ||
1098 | static int kvm_dev_ioctl_create_vm(void) | |
1099 | { | |
1100 | int fd, r; | |
1101 | struct inode *inode; | |
1102 | struct file *file; | |
1103 | struct kvm *kvm; | |
1104 | ||
f17abe9a | 1105 | kvm = kvm_create_vm(); |
d6d28168 AK |
1106 | if (IS_ERR(kvm)) |
1107 | return PTR_ERR(kvm); | |
1108 | r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm); | |
1109 | if (r) { | |
1110 | kvm_destroy_vm(kvm); | |
1111 | return r; | |
f17abe9a AK |
1112 | } |
1113 | ||
bccf2150 | 1114 | kvm->filp = file; |
f17abe9a | 1115 | |
f17abe9a | 1116 | return fd; |
f17abe9a AK |
1117 | } |
1118 | ||
1119 | static long kvm_dev_ioctl(struct file *filp, | |
1120 | unsigned int ioctl, unsigned long arg) | |
1121 | { | |
1122 | void __user *argp = (void __user *)arg; | |
07c45a36 | 1123 | long r = -EINVAL; |
f17abe9a AK |
1124 | |
1125 | switch (ioctl) { | |
1126 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
1127 | r = -EINVAL; |
1128 | if (arg) | |
1129 | goto out; | |
f17abe9a AK |
1130 | r = KVM_API_VERSION; |
1131 | break; | |
1132 | case KVM_CREATE_VM: | |
f0fe5108 AK |
1133 | r = -EINVAL; |
1134 | if (arg) | |
1135 | goto out; | |
f17abe9a AK |
1136 | r = kvm_dev_ioctl_create_vm(); |
1137 | break; | |
018d00d2 ZX |
1138 | case KVM_CHECK_EXTENSION: |
1139 | r = kvm_dev_ioctl_check_extension((long)argp); | |
5d308f45 | 1140 | break; |
07c45a36 AK |
1141 | case KVM_GET_VCPU_MMAP_SIZE: |
1142 | r = -EINVAL; | |
1143 | if (arg) | |
1144 | goto out; | |
039576c0 | 1145 | r = 2 * PAGE_SIZE; |
07c45a36 | 1146 | break; |
6aa8b732 | 1147 | default: |
043405e1 | 1148 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
1149 | } |
1150 | out: | |
1151 | return r; | |
1152 | } | |
1153 | ||
6aa8b732 | 1154 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
1155 | .unlocked_ioctl = kvm_dev_ioctl, |
1156 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
1157 | }; |
1158 | ||
1159 | static struct miscdevice kvm_dev = { | |
bbe4432e | 1160 | KVM_MINOR, |
6aa8b732 AK |
1161 | "kvm", |
1162 | &kvm_chardev_ops, | |
1163 | }; | |
1164 | ||
1b6c0168 AK |
1165 | static void hardware_enable(void *junk) |
1166 | { | |
1167 | int cpu = raw_smp_processor_id(); | |
1168 | ||
1169 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
1170 | return; | |
1171 | cpu_set(cpu, cpus_hardware_enabled); | |
e9b11c17 | 1172 | kvm_arch_hardware_enable(NULL); |
1b6c0168 AK |
1173 | } |
1174 | ||
1175 | static void hardware_disable(void *junk) | |
1176 | { | |
1177 | int cpu = raw_smp_processor_id(); | |
1178 | ||
1179 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
1180 | return; | |
1181 | cpu_clear(cpu, cpus_hardware_enabled); | |
1182 | decache_vcpus_on_cpu(cpu); | |
e9b11c17 | 1183 | kvm_arch_hardware_disable(NULL); |
1b6c0168 AK |
1184 | } |
1185 | ||
774c47f1 AK |
1186 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
1187 | void *v) | |
1188 | { | |
1189 | int cpu = (long)v; | |
1190 | ||
1a6f4d7f | 1191 | val &= ~CPU_TASKS_FROZEN; |
774c47f1 | 1192 | switch (val) { |
cec9ad27 | 1193 | case CPU_DYING: |
6ec8a856 AK |
1194 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1195 | cpu); | |
1196 | hardware_disable(NULL); | |
1197 | break; | |
774c47f1 | 1198 | case CPU_UP_CANCELED: |
43934a38 JK |
1199 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1200 | cpu); | |
1b6c0168 | 1201 | smp_call_function_single(cpu, hardware_disable, NULL, 0, 1); |
774c47f1 | 1202 | break; |
43934a38 JK |
1203 | case CPU_ONLINE: |
1204 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", | |
1205 | cpu); | |
1b6c0168 | 1206 | smp_call_function_single(cpu, hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1207 | break; |
1208 | } | |
1209 | return NOTIFY_OK; | |
1210 | } | |
1211 | ||
9a2b85c6 | 1212 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 1213 | void *v) |
9a2b85c6 RR |
1214 | { |
1215 | if (val == SYS_RESTART) { | |
1216 | /* | |
1217 | * Some (well, at least mine) BIOSes hang on reboot if | |
1218 | * in vmx root mode. | |
1219 | */ | |
1220 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
1221 | on_each_cpu(hardware_disable, NULL, 0, 1); | |
1222 | } | |
1223 | return NOTIFY_OK; | |
1224 | } | |
1225 | ||
1226 | static struct notifier_block kvm_reboot_notifier = { | |
1227 | .notifier_call = kvm_reboot, | |
1228 | .priority = 0, | |
1229 | }; | |
1230 | ||
2eeb2e94 GH |
1231 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
1232 | { | |
1233 | memset(bus, 0, sizeof(*bus)); | |
1234 | } | |
1235 | ||
1236 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
1237 | { | |
1238 | int i; | |
1239 | ||
1240 | for (i = 0; i < bus->dev_count; i++) { | |
1241 | struct kvm_io_device *pos = bus->devs[i]; | |
1242 | ||
1243 | kvm_iodevice_destructor(pos); | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr) | |
1248 | { | |
1249 | int i; | |
1250 | ||
1251 | for (i = 0; i < bus->dev_count; i++) { | |
1252 | struct kvm_io_device *pos = bus->devs[i]; | |
1253 | ||
1254 | if (pos->in_range(pos, addr)) | |
1255 | return pos; | |
1256 | } | |
1257 | ||
1258 | return NULL; | |
1259 | } | |
1260 | ||
1261 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
1262 | { | |
1263 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
1264 | ||
1265 | bus->devs[bus->dev_count++] = dev; | |
1266 | } | |
1267 | ||
774c47f1 AK |
1268 | static struct notifier_block kvm_cpu_notifier = { |
1269 | .notifier_call = kvm_cpu_hotplug, | |
1270 | .priority = 20, /* must be > scheduler priority */ | |
1271 | }; | |
1272 | ||
ba1389b7 AK |
1273 | static u64 vm_stat_get(void *_offset) |
1274 | { | |
1275 | unsigned offset = (long)_offset; | |
1276 | u64 total = 0; | |
1277 | struct kvm *kvm; | |
1278 | ||
1279 | spin_lock(&kvm_lock); | |
1280 | list_for_each_entry(kvm, &vm_list, vm_list) | |
1281 | total += *(u32 *)((void *)kvm + offset); | |
1282 | spin_unlock(&kvm_lock); | |
1283 | return total; | |
1284 | } | |
1285 | ||
1286 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); | |
1287 | ||
1288 | static u64 vcpu_stat_get(void *_offset) | |
1165f5fe AK |
1289 | { |
1290 | unsigned offset = (long)_offset; | |
1291 | u64 total = 0; | |
1292 | struct kvm *kvm; | |
1293 | struct kvm_vcpu *vcpu; | |
1294 | int i; | |
1295 | ||
1296 | spin_lock(&kvm_lock); | |
1297 | list_for_each_entry(kvm, &vm_list, vm_list) | |
1298 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
1299 | vcpu = kvm->vcpus[i]; |
1300 | if (vcpu) | |
1301 | total += *(u32 *)((void *)vcpu + offset); | |
1165f5fe AK |
1302 | } |
1303 | spin_unlock(&kvm_lock); | |
1304 | return total; | |
1305 | } | |
1306 | ||
ba1389b7 AK |
1307 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
1308 | ||
1309 | static struct file_operations *stat_fops[] = { | |
1310 | [KVM_STAT_VCPU] = &vcpu_stat_fops, | |
1311 | [KVM_STAT_VM] = &vm_stat_fops, | |
1312 | }; | |
1165f5fe | 1313 | |
a16b043c | 1314 | static void kvm_init_debug(void) |
6aa8b732 AK |
1315 | { |
1316 | struct kvm_stats_debugfs_item *p; | |
1317 | ||
8b6d44c7 | 1318 | debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 1319 | for (p = debugfs_entries; p->name; ++p) |
1165f5fe AK |
1320 | p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir, |
1321 | (void *)(long)p->offset, | |
ba1389b7 | 1322 | stat_fops[p->kind]); |
6aa8b732 AK |
1323 | } |
1324 | ||
1325 | static void kvm_exit_debug(void) | |
1326 | { | |
1327 | struct kvm_stats_debugfs_item *p; | |
1328 | ||
1329 | for (p = debugfs_entries; p->name; ++p) | |
1330 | debugfs_remove(p->dentry); | |
1331 | debugfs_remove(debugfs_dir); | |
1332 | } | |
1333 | ||
59ae6c6b AK |
1334 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
1335 | { | |
4267c41a | 1336 | hardware_disable(NULL); |
59ae6c6b AK |
1337 | return 0; |
1338 | } | |
1339 | ||
1340 | static int kvm_resume(struct sys_device *dev) | |
1341 | { | |
4267c41a | 1342 | hardware_enable(NULL); |
59ae6c6b AK |
1343 | return 0; |
1344 | } | |
1345 | ||
1346 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 1347 | .name = "kvm", |
59ae6c6b AK |
1348 | .suspend = kvm_suspend, |
1349 | .resume = kvm_resume, | |
1350 | }; | |
1351 | ||
1352 | static struct sys_device kvm_sysdev = { | |
1353 | .id = 0, | |
1354 | .cls = &kvm_sysdev_class, | |
1355 | }; | |
1356 | ||
cea7bb21 | 1357 | struct page *bad_page; |
6aa8b732 | 1358 | |
15ad7146 AK |
1359 | static inline |
1360 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
1361 | { | |
1362 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
1363 | } | |
1364 | ||
1365 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
1366 | { | |
1367 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1368 | ||
e9b11c17 | 1369 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 AK |
1370 | } |
1371 | ||
1372 | static void kvm_sched_out(struct preempt_notifier *pn, | |
1373 | struct task_struct *next) | |
1374 | { | |
1375 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1376 | ||
e9b11c17 | 1377 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
1378 | } |
1379 | ||
f8c16bba | 1380 | int kvm_init(void *opaque, unsigned int vcpu_size, |
c16f862d | 1381 | struct module *module) |
6aa8b732 AK |
1382 | { |
1383 | int r; | |
002c7f7c | 1384 | int cpu; |
6aa8b732 | 1385 | |
cb498ea2 ZX |
1386 | kvm_init_debug(); |
1387 | ||
f8c16bba ZX |
1388 | r = kvm_arch_init(opaque); |
1389 | if (r) | |
1390 | goto out4; | |
cb498ea2 ZX |
1391 | |
1392 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
1393 | ||
1394 | if (bad_page == NULL) { | |
1395 | r = -ENOMEM; | |
1396 | goto out; | |
1397 | } | |
1398 | ||
e9b11c17 | 1399 | r = kvm_arch_hardware_setup(); |
6aa8b732 | 1400 | if (r < 0) |
ca45aaae | 1401 | goto out; |
6aa8b732 | 1402 | |
002c7f7c YS |
1403 | for_each_online_cpu(cpu) { |
1404 | smp_call_function_single(cpu, | |
e9b11c17 | 1405 | kvm_arch_check_processor_compat, |
002c7f7c YS |
1406 | &r, 0, 1); |
1407 | if (r < 0) | |
1408 | goto out_free_0; | |
1409 | } | |
1410 | ||
1b6c0168 | 1411 | on_each_cpu(hardware_enable, NULL, 0, 1); |
774c47f1 AK |
1412 | r = register_cpu_notifier(&kvm_cpu_notifier); |
1413 | if (r) | |
1414 | goto out_free_1; | |
6aa8b732 AK |
1415 | register_reboot_notifier(&kvm_reboot_notifier); |
1416 | ||
59ae6c6b AK |
1417 | r = sysdev_class_register(&kvm_sysdev_class); |
1418 | if (r) | |
1419 | goto out_free_2; | |
1420 | ||
1421 | r = sysdev_register(&kvm_sysdev); | |
1422 | if (r) | |
1423 | goto out_free_3; | |
1424 | ||
c16f862d RR |
1425 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
1426 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
56919c5c JP |
1427 | __alignof__(struct kvm_vcpu), |
1428 | 0, NULL); | |
c16f862d RR |
1429 | if (!kvm_vcpu_cache) { |
1430 | r = -ENOMEM; | |
1431 | goto out_free_4; | |
1432 | } | |
1433 | ||
6aa8b732 AK |
1434 | kvm_chardev_ops.owner = module; |
1435 | ||
1436 | r = misc_register(&kvm_dev); | |
1437 | if (r) { | |
d77c26fc | 1438 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
1439 | goto out_free; |
1440 | } | |
1441 | ||
15ad7146 AK |
1442 | kvm_preempt_ops.sched_in = kvm_sched_in; |
1443 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
1444 | ||
c7addb90 | 1445 | return 0; |
6aa8b732 AK |
1446 | |
1447 | out_free: | |
c16f862d RR |
1448 | kmem_cache_destroy(kvm_vcpu_cache); |
1449 | out_free_4: | |
59ae6c6b AK |
1450 | sysdev_unregister(&kvm_sysdev); |
1451 | out_free_3: | |
1452 | sysdev_class_unregister(&kvm_sysdev_class); | |
1453 | out_free_2: | |
6aa8b732 | 1454 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 AK |
1455 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1456 | out_free_1: | |
1b6c0168 | 1457 | on_each_cpu(hardware_disable, NULL, 0, 1); |
002c7f7c | 1458 | out_free_0: |
e9b11c17 | 1459 | kvm_arch_hardware_unsetup(); |
ca45aaae | 1460 | out: |
f8c16bba | 1461 | kvm_arch_exit(); |
cb498ea2 | 1462 | kvm_exit_debug(); |
cb498ea2 | 1463 | out4: |
6aa8b732 AK |
1464 | return r; |
1465 | } | |
cb498ea2 | 1466 | EXPORT_SYMBOL_GPL(kvm_init); |
6aa8b732 | 1467 | |
cb498ea2 | 1468 | void kvm_exit(void) |
6aa8b732 AK |
1469 | { |
1470 | misc_deregister(&kvm_dev); | |
c16f862d | 1471 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
1472 | sysdev_unregister(&kvm_sysdev); |
1473 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 1474 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 1475 | unregister_cpu_notifier(&kvm_cpu_notifier); |
1b6c0168 | 1476 | on_each_cpu(hardware_disable, NULL, 0, 1); |
e9b11c17 | 1477 | kvm_arch_hardware_unsetup(); |
f8c16bba | 1478 | kvm_arch_exit(); |
6aa8b732 | 1479 | kvm_exit_debug(); |
cea7bb21 | 1480 | __free_page(bad_page); |
6aa8b732 | 1481 | } |
cb498ea2 | 1482 | EXPORT_SYMBOL_GPL(kvm_exit); |