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