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