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