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