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