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