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 | ||
8a98f664 XZ |
54 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
55 | #include <linux/pci.h> | |
56 | #include <linux/interrupt.h> | |
57 | #include "irq.h" | |
58 | #endif | |
59 | ||
6aa8b732 AK |
60 | MODULE_AUTHOR("Qumranet"); |
61 | MODULE_LICENSE("GPL"); | |
62 | ||
e9b11c17 ZX |
63 | DEFINE_SPINLOCK(kvm_lock); |
64 | LIST_HEAD(vm_list); | |
133de902 | 65 | |
1b6c0168 AK |
66 | static cpumask_t cpus_hardware_enabled; |
67 | ||
c16f862d RR |
68 | struct kmem_cache *kvm_vcpu_cache; |
69 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 70 | |
15ad7146 AK |
71 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
72 | ||
76f7c879 | 73 | struct dentry *kvm_debugfs_dir; |
6aa8b732 | 74 | |
bccf2150 AK |
75 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
76 | unsigned long arg); | |
77 | ||
4ecac3fd AK |
78 | bool kvm_rebooting; |
79 | ||
8a98f664 XZ |
80 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT |
81 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, | |
82 | int assigned_dev_id) | |
83 | { | |
84 | struct list_head *ptr; | |
85 | struct kvm_assigned_dev_kernel *match; | |
86 | ||
87 | list_for_each(ptr, head) { | |
88 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); | |
89 | if (match->assigned_dev_id == assigned_dev_id) | |
90 | return match; | |
91 | } | |
92 | return NULL; | |
93 | } | |
94 | ||
95 | static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work) | |
96 | { | |
97 | struct kvm_assigned_dev_kernel *assigned_dev; | |
98 | ||
99 | assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, | |
100 | interrupt_work); | |
101 | ||
102 | /* This is taken to safely inject irq inside the guest. When | |
103 | * the interrupt injection (or the ioapic code) uses a | |
104 | * finer-grained lock, update this | |
105 | */ | |
106 | mutex_lock(&assigned_dev->kvm->lock); | |
107 | kvm_set_irq(assigned_dev->kvm, | |
5550af4d | 108 | assigned_dev->irq_source_id, |
8a98f664 XZ |
109 | assigned_dev->guest_irq, 1); |
110 | mutex_unlock(&assigned_dev->kvm->lock); | |
111 | kvm_put_kvm(assigned_dev->kvm); | |
112 | } | |
113 | ||
8a98f664 XZ |
114 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) |
115 | { | |
116 | struct kvm_assigned_dev_kernel *assigned_dev = | |
117 | (struct kvm_assigned_dev_kernel *) dev_id; | |
118 | ||
119 | kvm_get_kvm(assigned_dev->kvm); | |
120 | schedule_work(&assigned_dev->interrupt_work); | |
121 | disable_irq_nosync(irq); | |
122 | return IRQ_HANDLED; | |
123 | } | |
124 | ||
125 | /* Ack the irq line for an assigned device */ | |
126 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) | |
127 | { | |
128 | struct kvm_assigned_dev_kernel *dev; | |
129 | ||
130 | if (kian->gsi == -1) | |
131 | return; | |
132 | ||
133 | dev = container_of(kian, struct kvm_assigned_dev_kernel, | |
134 | ack_notifier); | |
5550af4d | 135 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); |
8a98f664 XZ |
136 | enable_irq(dev->host_irq); |
137 | } | |
138 | ||
139 | static void kvm_free_assigned_device(struct kvm *kvm, | |
140 | struct kvm_assigned_dev_kernel | |
141 | *assigned_dev) | |
142 | { | |
143 | if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested) | |
144 | free_irq(assigned_dev->host_irq, (void *)assigned_dev); | |
145 | ||
e19e30ef | 146 | kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier); |
5550af4d | 147 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); |
8a98f664 XZ |
148 | |
149 | if (cancel_work_sync(&assigned_dev->interrupt_work)) | |
150 | /* We had pending work. That means we will have to take | |
151 | * care of kvm_put_kvm. | |
152 | */ | |
153 | kvm_put_kvm(kvm); | |
154 | ||
6eb55818 SY |
155 | pci_reset_function(assigned_dev->dev); |
156 | ||
8a98f664 XZ |
157 | pci_release_regions(assigned_dev->dev); |
158 | pci_disable_device(assigned_dev->dev); | |
159 | pci_dev_put(assigned_dev->dev); | |
160 | ||
161 | list_del(&assigned_dev->list); | |
162 | kfree(assigned_dev); | |
163 | } | |
164 | ||
165 | void kvm_free_all_assigned_devices(struct kvm *kvm) | |
166 | { | |
167 | struct list_head *ptr, *ptr2; | |
168 | struct kvm_assigned_dev_kernel *assigned_dev; | |
169 | ||
170 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { | |
171 | assigned_dev = list_entry(ptr, | |
172 | struct kvm_assigned_dev_kernel, | |
173 | list); | |
174 | ||
175 | kvm_free_assigned_device(kvm, assigned_dev); | |
176 | } | |
177 | } | |
178 | ||
179 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, | |
180 | struct kvm_assigned_irq | |
181 | *assigned_irq) | |
182 | { | |
183 | int r = 0; | |
184 | struct kvm_assigned_dev_kernel *match; | |
185 | ||
186 | mutex_lock(&kvm->lock); | |
187 | ||
188 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | |
189 | assigned_irq->assigned_dev_id); | |
190 | if (!match) { | |
191 | mutex_unlock(&kvm->lock); | |
192 | return -EINVAL; | |
193 | } | |
194 | ||
195 | if (match->irq_requested) { | |
196 | match->guest_irq = assigned_irq->guest_irq; | |
197 | match->ack_notifier.gsi = assigned_irq->guest_irq; | |
198 | mutex_unlock(&kvm->lock); | |
199 | return 0; | |
200 | } | |
201 | ||
202 | INIT_WORK(&match->interrupt_work, | |
203 | kvm_assigned_dev_interrupt_work_handler); | |
204 | ||
205 | if (irqchip_in_kernel(kvm)) { | |
206 | if (!capable(CAP_SYS_RAWIO)) { | |
207 | r = -EPERM; | |
208 | goto out_release; | |
209 | } | |
210 | ||
211 | if (assigned_irq->host_irq) | |
212 | match->host_irq = assigned_irq->host_irq; | |
213 | else | |
214 | match->host_irq = match->dev->irq; | |
215 | match->guest_irq = assigned_irq->guest_irq; | |
216 | match->ack_notifier.gsi = assigned_irq->guest_irq; | |
217 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; | |
218 | kvm_register_irq_ack_notifier(kvm, &match->ack_notifier); | |
5550af4d SY |
219 | r = kvm_request_irq_source_id(kvm); |
220 | if (r < 0) | |
221 | goto out_release; | |
222 | else | |
223 | match->irq_source_id = r; | |
8a98f664 XZ |
224 | |
225 | /* Even though this is PCI, we don't want to use shared | |
226 | * interrupts. Sharing host devices with guest-assigned devices | |
227 | * on the same interrupt line is not a happy situation: there | |
228 | * are going to be long delays in accepting, acking, etc. | |
229 | */ | |
230 | if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0, | |
231 | "kvm_assigned_device", (void *)match)) { | |
232 | r = -EIO; | |
233 | goto out_release; | |
234 | } | |
235 | } | |
236 | ||
237 | match->irq_requested = true; | |
238 | mutex_unlock(&kvm->lock); | |
239 | return r; | |
240 | out_release: | |
241 | mutex_unlock(&kvm->lock); | |
242 | kvm_free_assigned_device(kvm, match); | |
243 | return r; | |
244 | } | |
245 | ||
246 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, | |
247 | struct kvm_assigned_pci_dev *assigned_dev) | |
248 | { | |
249 | int r = 0; | |
250 | struct kvm_assigned_dev_kernel *match; | |
251 | struct pci_dev *dev; | |
252 | ||
253 | mutex_lock(&kvm->lock); | |
254 | ||
255 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | |
256 | assigned_dev->assigned_dev_id); | |
257 | if (match) { | |
258 | /* device already assigned */ | |
259 | r = -EINVAL; | |
260 | goto out; | |
261 | } | |
262 | ||
263 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); | |
264 | if (match == NULL) { | |
265 | printk(KERN_INFO "%s: Couldn't allocate memory\n", | |
266 | __func__); | |
267 | r = -ENOMEM; | |
268 | goto out; | |
269 | } | |
270 | dev = pci_get_bus_and_slot(assigned_dev->busnr, | |
271 | assigned_dev->devfn); | |
272 | if (!dev) { | |
273 | printk(KERN_INFO "%s: host device not found\n", __func__); | |
274 | r = -EINVAL; | |
275 | goto out_free; | |
276 | } | |
277 | if (pci_enable_device(dev)) { | |
278 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); | |
279 | r = -EBUSY; | |
280 | goto out_put; | |
281 | } | |
282 | r = pci_request_regions(dev, "kvm_assigned_device"); | |
283 | if (r) { | |
284 | printk(KERN_INFO "%s: Could not get access to device regions\n", | |
285 | __func__); | |
286 | goto out_disable; | |
287 | } | |
6eb55818 SY |
288 | |
289 | pci_reset_function(dev); | |
290 | ||
8a98f664 XZ |
291 | match->assigned_dev_id = assigned_dev->assigned_dev_id; |
292 | match->host_busnr = assigned_dev->busnr; | |
293 | match->host_devfn = assigned_dev->devfn; | |
294 | match->dev = dev; | |
295 | ||
296 | match->kvm = kvm; | |
297 | ||
298 | list_add(&match->list, &kvm->arch.assigned_dev_head); | |
299 | ||
300 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { | |
301 | r = kvm_iommu_map_guest(kvm, match); | |
302 | if (r) | |
303 | goto out_list_del; | |
304 | } | |
305 | ||
306 | out: | |
307 | mutex_unlock(&kvm->lock); | |
308 | return r; | |
309 | out_list_del: | |
310 | list_del(&match->list); | |
311 | pci_release_regions(dev); | |
312 | out_disable: | |
313 | pci_disable_device(dev); | |
314 | out_put: | |
315 | pci_dev_put(dev); | |
316 | out_free: | |
317 | kfree(match); | |
318 | mutex_unlock(&kvm->lock); | |
319 | return r; | |
320 | } | |
321 | #endif | |
322 | ||
5aacf0ca JM |
323 | static inline int valid_vcpu(int n) |
324 | { | |
325 | return likely(n >= 0 && n < KVM_MAX_VCPUS); | |
326 | } | |
327 | ||
c77fb9dc | 328 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
cbff90a7 BAY |
329 | { |
330 | if (pfn_valid(pfn)) | |
331 | return PageReserved(pfn_to_page(pfn)); | |
332 | ||
333 | return true; | |
334 | } | |
335 | ||
bccf2150 AK |
336 | /* |
337 | * Switches to specified vcpu, until a matching vcpu_put() | |
338 | */ | |
313a3dc7 | 339 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 340 | { |
15ad7146 AK |
341 | int cpu; |
342 | ||
bccf2150 | 343 | mutex_lock(&vcpu->mutex); |
15ad7146 AK |
344 | cpu = get_cpu(); |
345 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 346 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 347 | put_cpu(); |
6aa8b732 AK |
348 | } |
349 | ||
313a3dc7 | 350 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 351 | { |
15ad7146 | 352 | preempt_disable(); |
313a3dc7 | 353 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
354 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
355 | preempt_enable(); | |
6aa8b732 AK |
356 | mutex_unlock(&vcpu->mutex); |
357 | } | |
358 | ||
d9e368d6 AK |
359 | static void ack_flush(void *_completed) |
360 | { | |
d9e368d6 AK |
361 | } |
362 | ||
363 | void kvm_flush_remote_tlbs(struct kvm *kvm) | |
364 | { | |
597a5f55 | 365 | int i, cpu, me; |
d9e368d6 AK |
366 | cpumask_t cpus; |
367 | struct kvm_vcpu *vcpu; | |
d9e368d6 | 368 | |
597a5f55 | 369 | me = get_cpu(); |
d9e368d6 | 370 | cpus_clear(cpus); |
fb3f0f51 RR |
371 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
372 | vcpu = kvm->vcpus[i]; | |
373 | if (!vcpu) | |
374 | continue; | |
3176bc3e | 375 | if (test_and_set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests)) |
d9e368d6 AK |
376 | continue; |
377 | cpu = vcpu->cpu; | |
597a5f55 | 378 | if (cpu != -1 && cpu != me) |
49d3bd7e | 379 | cpu_set(cpu, cpus); |
d9e368d6 | 380 | } |
0f74a24c | 381 | if (cpus_empty(cpus)) |
597a5f55 | 382 | goto out; |
0f74a24c | 383 | ++kvm->stat.remote_tlb_flush; |
49d3bd7e | 384 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
597a5f55 AK |
385 | out: |
386 | put_cpu(); | |
d9e368d6 AK |
387 | } |
388 | ||
2e53d63a MT |
389 | void kvm_reload_remote_mmus(struct kvm *kvm) |
390 | { | |
597a5f55 | 391 | int i, cpu, me; |
2e53d63a MT |
392 | cpumask_t cpus; |
393 | struct kvm_vcpu *vcpu; | |
394 | ||
597a5f55 | 395 | me = get_cpu(); |
2e53d63a MT |
396 | cpus_clear(cpus); |
397 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
398 | vcpu = kvm->vcpus[i]; | |
399 | if (!vcpu) | |
400 | continue; | |
401 | if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests)) | |
402 | continue; | |
403 | cpu = vcpu->cpu; | |
597a5f55 | 404 | if (cpu != -1 && cpu != me) |
2e53d63a MT |
405 | cpu_set(cpu, cpus); |
406 | } | |
407 | if (cpus_empty(cpus)) | |
597a5f55 | 408 | goto out; |
2e53d63a | 409 | smp_call_function_mask(cpus, ack_flush, NULL, 1); |
597a5f55 AK |
410 | out: |
411 | put_cpu(); | |
2e53d63a MT |
412 | } |
413 | ||
414 | ||
fb3f0f51 RR |
415 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
416 | { | |
417 | struct page *page; | |
418 | int r; | |
419 | ||
420 | mutex_init(&vcpu->mutex); | |
421 | vcpu->cpu = -1; | |
fb3f0f51 RR |
422 | vcpu->kvm = kvm; |
423 | vcpu->vcpu_id = id; | |
b6958ce4 | 424 | init_waitqueue_head(&vcpu->wq); |
fb3f0f51 RR |
425 | |
426 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
427 | if (!page) { | |
428 | r = -ENOMEM; | |
429 | goto fail; | |
430 | } | |
431 | vcpu->run = page_address(page); | |
432 | ||
e9b11c17 | 433 | r = kvm_arch_vcpu_init(vcpu); |
fb3f0f51 | 434 | if (r < 0) |
e9b11c17 | 435 | goto fail_free_run; |
fb3f0f51 RR |
436 | return 0; |
437 | ||
fb3f0f51 RR |
438 | fail_free_run: |
439 | free_page((unsigned long)vcpu->run); | |
440 | fail: | |
76fafa5e | 441 | return r; |
fb3f0f51 RR |
442 | } |
443 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
444 | ||
445 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
446 | { | |
e9b11c17 | 447 | kvm_arch_vcpu_uninit(vcpu); |
fb3f0f51 RR |
448 | free_page((unsigned long)vcpu->run); |
449 | } | |
450 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
451 | ||
e930bffe AA |
452 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
453 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) | |
454 | { | |
455 | return container_of(mn, struct kvm, mmu_notifier); | |
456 | } | |
457 | ||
458 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, | |
459 | struct mm_struct *mm, | |
460 | unsigned long address) | |
461 | { | |
462 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
463 | int need_tlb_flush; | |
464 | ||
465 | /* | |
466 | * When ->invalidate_page runs, the linux pte has been zapped | |
467 | * already but the page is still allocated until | |
468 | * ->invalidate_page returns. So if we increase the sequence | |
469 | * here the kvm page fault will notice if the spte can't be | |
470 | * established because the page is going to be freed. If | |
471 | * instead the kvm page fault establishes the spte before | |
472 | * ->invalidate_page runs, kvm_unmap_hva will release it | |
473 | * before returning. | |
474 | * | |
475 | * The sequence increase only need to be seen at spin_unlock | |
476 | * time, and not at spin_lock time. | |
477 | * | |
478 | * Increasing the sequence after the spin_unlock would be | |
479 | * unsafe because the kvm page fault could then establish the | |
480 | * pte after kvm_unmap_hva returned, without noticing the page | |
481 | * is going to be freed. | |
482 | */ | |
483 | spin_lock(&kvm->mmu_lock); | |
484 | kvm->mmu_notifier_seq++; | |
485 | need_tlb_flush = kvm_unmap_hva(kvm, address); | |
486 | spin_unlock(&kvm->mmu_lock); | |
487 | ||
488 | /* we've to flush the tlb before the pages can be freed */ | |
489 | if (need_tlb_flush) | |
490 | kvm_flush_remote_tlbs(kvm); | |
491 | ||
492 | } | |
493 | ||
494 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, | |
495 | struct mm_struct *mm, | |
496 | unsigned long start, | |
497 | unsigned long end) | |
498 | { | |
499 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
500 | int need_tlb_flush = 0; | |
501 | ||
502 | spin_lock(&kvm->mmu_lock); | |
503 | /* | |
504 | * The count increase must become visible at unlock time as no | |
505 | * spte can be established without taking the mmu_lock and | |
506 | * count is also read inside the mmu_lock critical section. | |
507 | */ | |
508 | kvm->mmu_notifier_count++; | |
509 | for (; start < end; start += PAGE_SIZE) | |
510 | need_tlb_flush |= kvm_unmap_hva(kvm, start); | |
511 | spin_unlock(&kvm->mmu_lock); | |
512 | ||
513 | /* we've to flush the tlb before the pages can be freed */ | |
514 | if (need_tlb_flush) | |
515 | kvm_flush_remote_tlbs(kvm); | |
516 | } | |
517 | ||
518 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, | |
519 | struct mm_struct *mm, | |
520 | unsigned long start, | |
521 | unsigned long end) | |
522 | { | |
523 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
524 | ||
525 | spin_lock(&kvm->mmu_lock); | |
526 | /* | |
527 | * This sequence increase will notify the kvm page fault that | |
528 | * the page that is going to be mapped in the spte could have | |
529 | * been freed. | |
530 | */ | |
531 | kvm->mmu_notifier_seq++; | |
532 | /* | |
533 | * The above sequence increase must be visible before the | |
534 | * below count decrease but both values are read by the kvm | |
535 | * page fault under mmu_lock spinlock so we don't need to add | |
536 | * a smb_wmb() here in between the two. | |
537 | */ | |
538 | kvm->mmu_notifier_count--; | |
539 | spin_unlock(&kvm->mmu_lock); | |
540 | ||
541 | BUG_ON(kvm->mmu_notifier_count < 0); | |
542 | } | |
543 | ||
544 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, | |
545 | struct mm_struct *mm, | |
546 | unsigned long address) | |
547 | { | |
548 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
549 | int young; | |
550 | ||
551 | spin_lock(&kvm->mmu_lock); | |
552 | young = kvm_age_hva(kvm, address); | |
553 | spin_unlock(&kvm->mmu_lock); | |
554 | ||
555 | if (young) | |
556 | kvm_flush_remote_tlbs(kvm); | |
557 | ||
558 | return young; | |
559 | } | |
560 | ||
561 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { | |
562 | .invalidate_page = kvm_mmu_notifier_invalidate_page, | |
563 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, | |
564 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, | |
565 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, | |
566 | }; | |
567 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ | |
568 | ||
f17abe9a | 569 | static struct kvm *kvm_create_vm(void) |
6aa8b732 | 570 | { |
d19a9cd2 | 571 | struct kvm *kvm = kvm_arch_create_vm(); |
5f94c174 LV |
572 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
573 | struct page *page; | |
574 | #endif | |
6aa8b732 | 575 | |
d19a9cd2 ZX |
576 | if (IS_ERR(kvm)) |
577 | goto out; | |
6aa8b732 | 578 | |
5f94c174 LV |
579 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
580 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
581 | if (!page) { | |
582 | kfree(kvm); | |
583 | return ERR_PTR(-ENOMEM); | |
584 | } | |
585 | kvm->coalesced_mmio_ring = | |
586 | (struct kvm_coalesced_mmio_ring *)page_address(page); | |
587 | #endif | |
588 | ||
e930bffe AA |
589 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
590 | { | |
591 | int err; | |
592 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; | |
593 | err = mmu_notifier_register(&kvm->mmu_notifier, current->mm); | |
594 | if (err) { | |
595 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
596 | put_page(page); | |
597 | #endif | |
598 | kfree(kvm); | |
599 | return ERR_PTR(err); | |
600 | } | |
601 | } | |
602 | #endif | |
603 | ||
6d4e4c4f AK |
604 | kvm->mm = current->mm; |
605 | atomic_inc(&kvm->mm->mm_count); | |
aaee2c94 | 606 | spin_lock_init(&kvm->mmu_lock); |
74906345 | 607 | kvm_io_bus_init(&kvm->pio_bus); |
11ec2804 | 608 | mutex_init(&kvm->lock); |
2eeb2e94 | 609 | kvm_io_bus_init(&kvm->mmio_bus); |
72dc67a6 | 610 | init_rwsem(&kvm->slots_lock); |
d39f13b0 | 611 | atomic_set(&kvm->users_count, 1); |
5e58cfe4 RR |
612 | spin_lock(&kvm_lock); |
613 | list_add(&kvm->vm_list, &vm_list); | |
614 | spin_unlock(&kvm_lock); | |
5f94c174 LV |
615 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
616 | kvm_coalesced_mmio_init(kvm); | |
617 | #endif | |
d19a9cd2 | 618 | out: |
f17abe9a AK |
619 | return kvm; |
620 | } | |
621 | ||
6aa8b732 AK |
622 | /* |
623 | * Free any memory in @free but not in @dont. | |
624 | */ | |
625 | static void kvm_free_physmem_slot(struct kvm_memory_slot *free, | |
626 | struct kvm_memory_slot *dont) | |
627 | { | |
290fc38d IE |
628 | if (!dont || free->rmap != dont->rmap) |
629 | vfree(free->rmap); | |
6aa8b732 AK |
630 | |
631 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
632 | vfree(free->dirty_bitmap); | |
633 | ||
05da4558 MT |
634 | if (!dont || free->lpage_info != dont->lpage_info) |
635 | vfree(free->lpage_info); | |
636 | ||
6aa8b732 | 637 | free->npages = 0; |
8b6d44c7 | 638 | free->dirty_bitmap = NULL; |
8d4e1288 | 639 | free->rmap = NULL; |
05da4558 | 640 | free->lpage_info = NULL; |
6aa8b732 AK |
641 | } |
642 | ||
d19a9cd2 | 643 | void kvm_free_physmem(struct kvm *kvm) |
6aa8b732 AK |
644 | { |
645 | int i; | |
646 | ||
647 | for (i = 0; i < kvm->nmemslots; ++i) | |
8b6d44c7 | 648 | kvm_free_physmem_slot(&kvm->memslots[i], NULL); |
6aa8b732 AK |
649 | } |
650 | ||
f17abe9a AK |
651 | static void kvm_destroy_vm(struct kvm *kvm) |
652 | { | |
6d4e4c4f AK |
653 | struct mm_struct *mm = kvm->mm; |
654 | ||
133de902 AK |
655 | spin_lock(&kvm_lock); |
656 | list_del(&kvm->vm_list); | |
657 | spin_unlock(&kvm_lock); | |
74906345 | 658 | kvm_io_bus_destroy(&kvm->pio_bus); |
2eeb2e94 | 659 | kvm_io_bus_destroy(&kvm->mmio_bus); |
5f94c174 LV |
660 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
661 | if (kvm->coalesced_mmio_ring != NULL) | |
662 | free_page((unsigned long)kvm->coalesced_mmio_ring); | |
e930bffe AA |
663 | #endif |
664 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) | |
665 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); | |
5f94c174 | 666 | #endif |
d19a9cd2 | 667 | kvm_arch_destroy_vm(kvm); |
6d4e4c4f | 668 | mmdrop(mm); |
f17abe9a AK |
669 | } |
670 | ||
d39f13b0 IE |
671 | void kvm_get_kvm(struct kvm *kvm) |
672 | { | |
673 | atomic_inc(&kvm->users_count); | |
674 | } | |
675 | EXPORT_SYMBOL_GPL(kvm_get_kvm); | |
676 | ||
677 | void kvm_put_kvm(struct kvm *kvm) | |
678 | { | |
679 | if (atomic_dec_and_test(&kvm->users_count)) | |
680 | kvm_destroy_vm(kvm); | |
681 | } | |
682 | EXPORT_SYMBOL_GPL(kvm_put_kvm); | |
683 | ||
684 | ||
f17abe9a AK |
685 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
686 | { | |
687 | struct kvm *kvm = filp->private_data; | |
688 | ||
d39f13b0 | 689 | kvm_put_kvm(kvm); |
6aa8b732 AK |
690 | return 0; |
691 | } | |
692 | ||
6aa8b732 AK |
693 | /* |
694 | * Allocate some memory and give it an address in the guest physical address | |
695 | * space. | |
696 | * | |
697 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e | 698 | * |
10589a46 | 699 | * Must be called holding mmap_sem for write. |
6aa8b732 | 700 | */ |
f78e0e2e SY |
701 | int __kvm_set_memory_region(struct kvm *kvm, |
702 | struct kvm_userspace_memory_region *mem, | |
703 | int user_alloc) | |
6aa8b732 AK |
704 | { |
705 | int r; | |
706 | gfn_t base_gfn; | |
707 | unsigned long npages; | |
708 | unsigned long i; | |
709 | struct kvm_memory_slot *memslot; | |
710 | struct kvm_memory_slot old, new; | |
6aa8b732 AK |
711 | |
712 | r = -EINVAL; | |
713 | /* General sanity checks */ | |
714 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
715 | goto out; | |
716 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
717 | goto out; | |
e0d62c7f | 718 | if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
6aa8b732 AK |
719 | goto out; |
720 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
721 | goto out; | |
722 | ||
723 | memslot = &kvm->memslots[mem->slot]; | |
724 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; | |
725 | npages = mem->memory_size >> PAGE_SHIFT; | |
726 | ||
727 | if (!npages) | |
728 | mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES; | |
729 | ||
6aa8b732 AK |
730 | new = old = *memslot; |
731 | ||
732 | new.base_gfn = base_gfn; | |
733 | new.npages = npages; | |
734 | new.flags = mem->flags; | |
735 | ||
736 | /* Disallow changing a memory slot's size. */ | |
737 | r = -EINVAL; | |
738 | if (npages && old.npages && npages != old.npages) | |
f78e0e2e | 739 | goto out_free; |
6aa8b732 AK |
740 | |
741 | /* Check for overlaps */ | |
742 | r = -EEXIST; | |
743 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
744 | struct kvm_memory_slot *s = &kvm->memslots[i]; | |
745 | ||
746 | if (s == memslot) | |
747 | continue; | |
748 | if (!((base_gfn + npages <= s->base_gfn) || | |
749 | (base_gfn >= s->base_gfn + s->npages))) | |
f78e0e2e | 750 | goto out_free; |
6aa8b732 | 751 | } |
6aa8b732 | 752 | |
6aa8b732 AK |
753 | /* Free page dirty bitmap if unneeded */ |
754 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 755 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
756 | |
757 | r = -ENOMEM; | |
758 | ||
759 | /* Allocate if a slot is being created */ | |
eff0114a | 760 | #ifndef CONFIG_S390 |
8d4e1288 | 761 | if (npages && !new.rmap) { |
d77c26fc | 762 | new.rmap = vmalloc(npages * sizeof(struct page *)); |
290fc38d IE |
763 | |
764 | if (!new.rmap) | |
f78e0e2e | 765 | goto out_free; |
290fc38d | 766 | |
290fc38d | 767 | memset(new.rmap, 0, npages * sizeof(*new.rmap)); |
8d4e1288 | 768 | |
80b14b5b | 769 | new.user_alloc = user_alloc; |
604b38ac AA |
770 | /* |
771 | * hva_to_rmmap() serialzies with the mmu_lock and to be | |
772 | * safe it has to ignore memslots with !user_alloc && | |
773 | * !userspace_addr. | |
774 | */ | |
775 | if (user_alloc) | |
776 | new.userspace_addr = mem->userspace_addr; | |
777 | else | |
778 | new.userspace_addr = 0; | |
6aa8b732 | 779 | } |
05da4558 MT |
780 | if (npages && !new.lpage_info) { |
781 | int largepages = npages / KVM_PAGES_PER_HPAGE; | |
782 | if (npages % KVM_PAGES_PER_HPAGE) | |
783 | largepages++; | |
784 | if (base_gfn % KVM_PAGES_PER_HPAGE) | |
785 | largepages++; | |
786 | ||
787 | new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info)); | |
788 | ||
789 | if (!new.lpage_info) | |
790 | goto out_free; | |
791 | ||
792 | memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info)); | |
793 | ||
794 | if (base_gfn % KVM_PAGES_PER_HPAGE) | |
795 | new.lpage_info[0].write_count = 1; | |
796 | if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE) | |
797 | new.lpage_info[largepages-1].write_count = 1; | |
798 | } | |
6aa8b732 AK |
799 | |
800 | /* Allocate page dirty bitmap if needed */ | |
801 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
802 | unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8; | |
803 | ||
804 | new.dirty_bitmap = vmalloc(dirty_bytes); | |
805 | if (!new.dirty_bitmap) | |
f78e0e2e | 806 | goto out_free; |
6aa8b732 AK |
807 | memset(new.dirty_bitmap, 0, dirty_bytes); |
808 | } | |
eff0114a | 809 | #endif /* not defined CONFIG_S390 */ |
6aa8b732 | 810 | |
34d4cb8f MT |
811 | if (!npages) |
812 | kvm_arch_flush_shadow(kvm); | |
813 | ||
604b38ac AA |
814 | spin_lock(&kvm->mmu_lock); |
815 | if (mem->slot >= kvm->nmemslots) | |
816 | kvm->nmemslots = mem->slot + 1; | |
817 | ||
3ad82a7e | 818 | *memslot = new; |
604b38ac | 819 | spin_unlock(&kvm->mmu_lock); |
3ad82a7e | 820 | |
0de10343 ZX |
821 | r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc); |
822 | if (r) { | |
604b38ac | 823 | spin_lock(&kvm->mmu_lock); |
0de10343 | 824 | *memslot = old; |
604b38ac | 825 | spin_unlock(&kvm->mmu_lock); |
0de10343 | 826 | goto out_free; |
82ce2c96 IE |
827 | } |
828 | ||
6aa8b732 | 829 | kvm_free_physmem_slot(&old, &new); |
8a98f664 | 830 | #ifdef CONFIG_DMAR |
62c476c7 BAY |
831 | /* map the pages in iommu page table */ |
832 | r = kvm_iommu_map_pages(kvm, base_gfn, npages); | |
833 | if (r) | |
834 | goto out; | |
8a98f664 | 835 | #endif |
6aa8b732 AK |
836 | return 0; |
837 | ||
f78e0e2e | 838 | out_free: |
6aa8b732 AK |
839 | kvm_free_physmem_slot(&new, &old); |
840 | out: | |
841 | return r; | |
210c7c4d IE |
842 | |
843 | } | |
f78e0e2e SY |
844 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
845 | ||
846 | int kvm_set_memory_region(struct kvm *kvm, | |
847 | struct kvm_userspace_memory_region *mem, | |
848 | int user_alloc) | |
849 | { | |
850 | int r; | |
851 | ||
72dc67a6 | 852 | down_write(&kvm->slots_lock); |
f78e0e2e | 853 | r = __kvm_set_memory_region(kvm, mem, user_alloc); |
72dc67a6 | 854 | up_write(&kvm->slots_lock); |
f78e0e2e SY |
855 | return r; |
856 | } | |
210c7c4d IE |
857 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
858 | ||
1fe779f8 CO |
859 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
860 | struct | |
861 | kvm_userspace_memory_region *mem, | |
862 | int user_alloc) | |
210c7c4d | 863 | { |
e0d62c7f IE |
864 | if (mem->slot >= KVM_MEMORY_SLOTS) |
865 | return -EINVAL; | |
210c7c4d | 866 | return kvm_set_memory_region(kvm, mem, user_alloc); |
6aa8b732 AK |
867 | } |
868 | ||
5bb064dc ZX |
869 | int kvm_get_dirty_log(struct kvm *kvm, |
870 | struct kvm_dirty_log *log, int *is_dirty) | |
6aa8b732 AK |
871 | { |
872 | struct kvm_memory_slot *memslot; | |
873 | int r, i; | |
874 | int n; | |
875 | unsigned long any = 0; | |
876 | ||
6aa8b732 AK |
877 | r = -EINVAL; |
878 | if (log->slot >= KVM_MEMORY_SLOTS) | |
879 | goto out; | |
880 | ||
881 | memslot = &kvm->memslots[log->slot]; | |
882 | r = -ENOENT; | |
883 | if (!memslot->dirty_bitmap) | |
884 | goto out; | |
885 | ||
cd1a4a98 | 886 | n = ALIGN(memslot->npages, BITS_PER_LONG) / 8; |
6aa8b732 | 887 | |
cd1a4a98 | 888 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
889 | any = memslot->dirty_bitmap[i]; |
890 | ||
891 | r = -EFAULT; | |
892 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
893 | goto out; | |
894 | ||
5bb064dc ZX |
895 | if (any) |
896 | *is_dirty = 1; | |
6aa8b732 AK |
897 | |
898 | r = 0; | |
6aa8b732 | 899 | out: |
6aa8b732 AK |
900 | return r; |
901 | } | |
902 | ||
cea7bb21 IE |
903 | int is_error_page(struct page *page) |
904 | { | |
905 | return page == bad_page; | |
906 | } | |
907 | EXPORT_SYMBOL_GPL(is_error_page); | |
908 | ||
35149e21 AL |
909 | int is_error_pfn(pfn_t pfn) |
910 | { | |
911 | return pfn == bad_pfn; | |
912 | } | |
913 | EXPORT_SYMBOL_GPL(is_error_pfn); | |
914 | ||
f9d46eb0 IE |
915 | static inline unsigned long bad_hva(void) |
916 | { | |
917 | return PAGE_OFFSET; | |
918 | } | |
919 | ||
920 | int kvm_is_error_hva(unsigned long addr) | |
921 | { | |
922 | return addr == bad_hva(); | |
923 | } | |
924 | EXPORT_SYMBOL_GPL(kvm_is_error_hva); | |
925 | ||
2843099f | 926 | struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn) |
6aa8b732 AK |
927 | { |
928 | int i; | |
929 | ||
930 | for (i = 0; i < kvm->nmemslots; ++i) { | |
931 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
932 | ||
933 | if (gfn >= memslot->base_gfn | |
934 | && gfn < memslot->base_gfn + memslot->npages) | |
935 | return memslot; | |
936 | } | |
8b6d44c7 | 937 | return NULL; |
6aa8b732 | 938 | } |
2843099f | 939 | EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased); |
e8207547 AK |
940 | |
941 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) | |
942 | { | |
943 | gfn = unalias_gfn(kvm, gfn); | |
2843099f | 944 | return gfn_to_memslot_unaliased(kvm, gfn); |
e8207547 | 945 | } |
6aa8b732 | 946 | |
e0d62c7f IE |
947 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
948 | { | |
949 | int i; | |
950 | ||
951 | gfn = unalias_gfn(kvm, gfn); | |
952 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { | |
953 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; | |
954 | ||
955 | if (gfn >= memslot->base_gfn | |
956 | && gfn < memslot->base_gfn + memslot->npages) | |
957 | return 1; | |
958 | } | |
959 | return 0; | |
960 | } | |
961 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
962 | ||
05da4558 | 963 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
539cb660 IE |
964 | { |
965 | struct kvm_memory_slot *slot; | |
966 | ||
967 | gfn = unalias_gfn(kvm, gfn); | |
2843099f | 968 | slot = gfn_to_memslot_unaliased(kvm, gfn); |
539cb660 IE |
969 | if (!slot) |
970 | return bad_hva(); | |
971 | return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE); | |
972 | } | |
0d150298 | 973 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
539cb660 | 974 | |
35149e21 | 975 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
954bbbc2 | 976 | { |
8d4e1288 | 977 | struct page *page[1]; |
539cb660 | 978 | unsigned long addr; |
8d4e1288 | 979 | int npages; |
2e2e3738 | 980 | pfn_t pfn; |
954bbbc2 | 981 | |
60395224 AK |
982 | might_sleep(); |
983 | ||
539cb660 IE |
984 | addr = gfn_to_hva(kvm, gfn); |
985 | if (kvm_is_error_hva(addr)) { | |
8a7ae055 | 986 | get_page(bad_page); |
35149e21 | 987 | return page_to_pfn(bad_page); |
8a7ae055 | 988 | } |
8d4e1288 | 989 | |
4c2155ce | 990 | npages = get_user_pages_fast(addr, 1, 1, page); |
539cb660 | 991 | |
2e2e3738 AL |
992 | if (unlikely(npages != 1)) { |
993 | struct vm_area_struct *vma; | |
994 | ||
4c2155ce | 995 | down_read(¤t->mm->mmap_sem); |
2e2e3738 | 996 | vma = find_vma(current->mm, addr); |
4c2155ce | 997 | |
2e2e3738 AL |
998 | if (vma == NULL || addr < vma->vm_start || |
999 | !(vma->vm_flags & VM_PFNMAP)) { | |
4c2155ce | 1000 | up_read(¤t->mm->mmap_sem); |
2e2e3738 AL |
1001 | get_page(bad_page); |
1002 | return page_to_pfn(bad_page); | |
1003 | } | |
1004 | ||
1005 | pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | |
4c2155ce | 1006 | up_read(¤t->mm->mmap_sem); |
c77fb9dc | 1007 | BUG_ON(!kvm_is_mmio_pfn(pfn)); |
2e2e3738 AL |
1008 | } else |
1009 | pfn = page_to_pfn(page[0]); | |
8d4e1288 | 1010 | |
2e2e3738 | 1011 | return pfn; |
35149e21 AL |
1012 | } |
1013 | ||
1014 | EXPORT_SYMBOL_GPL(gfn_to_pfn); | |
1015 | ||
1016 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) | |
1017 | { | |
2e2e3738 AL |
1018 | pfn_t pfn; |
1019 | ||
1020 | pfn = gfn_to_pfn(kvm, gfn); | |
c77fb9dc | 1021 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 AL |
1022 | return pfn_to_page(pfn); |
1023 | ||
c77fb9dc | 1024 | WARN_ON(kvm_is_mmio_pfn(pfn)); |
2e2e3738 AL |
1025 | |
1026 | get_page(bad_page); | |
1027 | return bad_page; | |
954bbbc2 | 1028 | } |
aab61cc0 | 1029 | |
954bbbc2 AK |
1030 | EXPORT_SYMBOL_GPL(gfn_to_page); |
1031 | ||
b4231d61 IE |
1032 | void kvm_release_page_clean(struct page *page) |
1033 | { | |
35149e21 | 1034 | kvm_release_pfn_clean(page_to_pfn(page)); |
b4231d61 IE |
1035 | } |
1036 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); | |
1037 | ||
35149e21 AL |
1038 | void kvm_release_pfn_clean(pfn_t pfn) |
1039 | { | |
c77fb9dc | 1040 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1041 | put_page(pfn_to_page(pfn)); |
35149e21 AL |
1042 | } |
1043 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); | |
1044 | ||
b4231d61 | 1045 | void kvm_release_page_dirty(struct page *page) |
8a7ae055 | 1046 | { |
35149e21 AL |
1047 | kvm_release_pfn_dirty(page_to_pfn(page)); |
1048 | } | |
1049 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); | |
1050 | ||
1051 | void kvm_release_pfn_dirty(pfn_t pfn) | |
1052 | { | |
1053 | kvm_set_pfn_dirty(pfn); | |
1054 | kvm_release_pfn_clean(pfn); | |
1055 | } | |
1056 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); | |
1057 | ||
1058 | void kvm_set_page_dirty(struct page *page) | |
1059 | { | |
1060 | kvm_set_pfn_dirty(page_to_pfn(page)); | |
1061 | } | |
1062 | EXPORT_SYMBOL_GPL(kvm_set_page_dirty); | |
1063 | ||
1064 | void kvm_set_pfn_dirty(pfn_t pfn) | |
1065 | { | |
c77fb9dc | 1066 | if (!kvm_is_mmio_pfn(pfn)) { |
2e2e3738 AL |
1067 | struct page *page = pfn_to_page(pfn); |
1068 | if (!PageReserved(page)) | |
1069 | SetPageDirty(page); | |
1070 | } | |
8a7ae055 | 1071 | } |
35149e21 AL |
1072 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
1073 | ||
1074 | void kvm_set_pfn_accessed(pfn_t pfn) | |
1075 | { | |
c77fb9dc | 1076 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1077 | mark_page_accessed(pfn_to_page(pfn)); |
35149e21 AL |
1078 | } |
1079 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); | |
1080 | ||
1081 | void kvm_get_pfn(pfn_t pfn) | |
1082 | { | |
c77fb9dc | 1083 | if (!kvm_is_mmio_pfn(pfn)) |
2e2e3738 | 1084 | get_page(pfn_to_page(pfn)); |
35149e21 AL |
1085 | } |
1086 | EXPORT_SYMBOL_GPL(kvm_get_pfn); | |
8a7ae055 | 1087 | |
195aefde IE |
1088 | static int next_segment(unsigned long len, int offset) |
1089 | { | |
1090 | if (len > PAGE_SIZE - offset) | |
1091 | return PAGE_SIZE - offset; | |
1092 | else | |
1093 | return len; | |
1094 | } | |
1095 | ||
1096 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
1097 | int len) | |
1098 | { | |
e0506bcb IE |
1099 | int r; |
1100 | unsigned long addr; | |
195aefde | 1101 | |
e0506bcb IE |
1102 | addr = gfn_to_hva(kvm, gfn); |
1103 | if (kvm_is_error_hva(addr)) | |
1104 | return -EFAULT; | |
1105 | r = copy_from_user(data, (void __user *)addr + offset, len); | |
1106 | if (r) | |
195aefde | 1107 | return -EFAULT; |
195aefde IE |
1108 | return 0; |
1109 | } | |
1110 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); | |
1111 | ||
1112 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) | |
1113 | { | |
1114 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1115 | int seg; | |
1116 | int offset = offset_in_page(gpa); | |
1117 | int ret; | |
1118 | ||
1119 | while ((seg = next_segment(len, offset)) != 0) { | |
1120 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
1121 | if (ret < 0) | |
1122 | return ret; | |
1123 | offset = 0; | |
1124 | len -= seg; | |
1125 | data += seg; | |
1126 | ++gfn; | |
1127 | } | |
1128 | return 0; | |
1129 | } | |
1130 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
1131 | ||
7ec54588 MT |
1132 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
1133 | unsigned long len) | |
1134 | { | |
1135 | int r; | |
1136 | unsigned long addr; | |
1137 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1138 | int offset = offset_in_page(gpa); | |
1139 | ||
1140 | addr = gfn_to_hva(kvm, gfn); | |
1141 | if (kvm_is_error_hva(addr)) | |
1142 | return -EFAULT; | |
0aac03f0 | 1143 | pagefault_disable(); |
7ec54588 | 1144 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
0aac03f0 | 1145 | pagefault_enable(); |
7ec54588 MT |
1146 | if (r) |
1147 | return -EFAULT; | |
1148 | return 0; | |
1149 | } | |
1150 | EXPORT_SYMBOL(kvm_read_guest_atomic); | |
1151 | ||
195aefde IE |
1152 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
1153 | int offset, int len) | |
1154 | { | |
e0506bcb IE |
1155 | int r; |
1156 | unsigned long addr; | |
195aefde | 1157 | |
e0506bcb IE |
1158 | addr = gfn_to_hva(kvm, gfn); |
1159 | if (kvm_is_error_hva(addr)) | |
1160 | return -EFAULT; | |
1161 | r = copy_to_user((void __user *)addr + offset, data, len); | |
1162 | if (r) | |
195aefde | 1163 | return -EFAULT; |
195aefde IE |
1164 | mark_page_dirty(kvm, gfn); |
1165 | return 0; | |
1166 | } | |
1167 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); | |
1168 | ||
1169 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
1170 | unsigned long len) | |
1171 | { | |
1172 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1173 | int seg; | |
1174 | int offset = offset_in_page(gpa); | |
1175 | int ret; | |
1176 | ||
1177 | while ((seg = next_segment(len, offset)) != 0) { | |
1178 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
1179 | if (ret < 0) | |
1180 | return ret; | |
1181 | offset = 0; | |
1182 | len -= seg; | |
1183 | data += seg; | |
1184 | ++gfn; | |
1185 | } | |
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) | |
1190 | { | |
3e021bf5 | 1191 | return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len); |
195aefde IE |
1192 | } |
1193 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
1194 | ||
1195 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
1196 | { | |
1197 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1198 | int seg; | |
1199 | int offset = offset_in_page(gpa); | |
1200 | int ret; | |
1201 | ||
1202 | while ((seg = next_segment(len, offset)) != 0) { | |
1203 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); | |
1204 | if (ret < 0) | |
1205 | return ret; | |
1206 | offset = 0; | |
1207 | len -= seg; | |
1208 | ++gfn; | |
1209 | } | |
1210 | return 0; | |
1211 | } | |
1212 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
1213 | ||
6aa8b732 AK |
1214 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
1215 | { | |
31389947 | 1216 | struct kvm_memory_slot *memslot; |
6aa8b732 | 1217 | |
3b6fff19 | 1218 | gfn = unalias_gfn(kvm, gfn); |
2843099f | 1219 | memslot = gfn_to_memslot_unaliased(kvm, gfn); |
7e9d619d RR |
1220 | if (memslot && memslot->dirty_bitmap) { |
1221 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 1222 | |
7e9d619d RR |
1223 | /* avoid RMW */ |
1224 | if (!test_bit(rel_gfn, memslot->dirty_bitmap)) | |
1225 | set_bit(rel_gfn, memslot->dirty_bitmap); | |
6aa8b732 AK |
1226 | } |
1227 | } | |
1228 | ||
b6958ce4 ED |
1229 | /* |
1230 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
1231 | */ | |
8776e519 | 1232 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 1233 | { |
e5c239cf MT |
1234 | DEFINE_WAIT(wait); |
1235 | ||
1236 | for (;;) { | |
1237 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); | |
1238 | ||
d7690175 MT |
1239 | if (kvm_cpu_has_interrupt(vcpu) || |
1240 | kvm_cpu_has_pending_timer(vcpu) || | |
1241 | kvm_arch_vcpu_runnable(vcpu)) { | |
1242 | set_bit(KVM_REQ_UNHALT, &vcpu->requests); | |
e5c239cf | 1243 | break; |
d7690175 | 1244 | } |
e5c239cf MT |
1245 | if (signal_pending(current)) |
1246 | break; | |
1247 | ||
b6958ce4 ED |
1248 | vcpu_put(vcpu); |
1249 | schedule(); | |
1250 | vcpu_load(vcpu); | |
1251 | } | |
d3bef15f | 1252 | |
e5c239cf | 1253 | finish_wait(&vcpu->wq, &wait); |
b6958ce4 ED |
1254 | } |
1255 | ||
6aa8b732 AK |
1256 | void kvm_resched(struct kvm_vcpu *vcpu) |
1257 | { | |
3fca0365 YD |
1258 | if (!need_resched()) |
1259 | return; | |
6aa8b732 | 1260 | cond_resched(); |
6aa8b732 AK |
1261 | } |
1262 | EXPORT_SYMBOL_GPL(kvm_resched); | |
1263 | ||
e4a533a4 | 1264 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
9a2bb7f4 AK |
1265 | { |
1266 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
9a2bb7f4 AK |
1267 | struct page *page; |
1268 | ||
e4a533a4 | 1269 | if (vmf->pgoff == 0) |
039576c0 | 1270 | page = virt_to_page(vcpu->run); |
09566765 | 1271 | #ifdef CONFIG_X86 |
e4a533a4 | 1272 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
ad312c7c | 1273 | page = virt_to_page(vcpu->arch.pio_data); |
5f94c174 LV |
1274 | #endif |
1275 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
1276 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) | |
1277 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); | |
09566765 | 1278 | #endif |
039576c0 | 1279 | else |
e4a533a4 | 1280 | return VM_FAULT_SIGBUS; |
9a2bb7f4 | 1281 | get_page(page); |
e4a533a4 | 1282 | vmf->page = page; |
1283 | return 0; | |
9a2bb7f4 AK |
1284 | } |
1285 | ||
1286 | static struct vm_operations_struct kvm_vcpu_vm_ops = { | |
e4a533a4 | 1287 | .fault = kvm_vcpu_fault, |
9a2bb7f4 AK |
1288 | }; |
1289 | ||
1290 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
1291 | { | |
1292 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
1293 | return 0; | |
1294 | } | |
1295 | ||
bccf2150 AK |
1296 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
1297 | { | |
1298 | struct kvm_vcpu *vcpu = filp->private_data; | |
1299 | ||
66c0b394 | 1300 | kvm_put_kvm(vcpu->kvm); |
bccf2150 AK |
1301 | return 0; |
1302 | } | |
1303 | ||
5c502742 | 1304 | static const struct file_operations kvm_vcpu_fops = { |
bccf2150 AK |
1305 | .release = kvm_vcpu_release, |
1306 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
1307 | .compat_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 1308 | .mmap = kvm_vcpu_mmap, |
bccf2150 AK |
1309 | }; |
1310 | ||
1311 | /* | |
1312 | * Allocates an inode for the vcpu. | |
1313 | */ | |
1314 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
1315 | { | |
7d9dbca3 | 1316 | int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); |
2030a42c | 1317 | if (fd < 0) |
66c0b394 | 1318 | kvm_put_kvm(vcpu->kvm); |
bccf2150 | 1319 | return fd; |
bccf2150 AK |
1320 | } |
1321 | ||
c5ea7660 AK |
1322 | /* |
1323 | * Creates some virtual cpus. Good luck creating more than one. | |
1324 | */ | |
1325 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n) | |
1326 | { | |
1327 | int r; | |
1328 | struct kvm_vcpu *vcpu; | |
1329 | ||
c5ea7660 | 1330 | if (!valid_vcpu(n)) |
fb3f0f51 | 1331 | return -EINVAL; |
c5ea7660 | 1332 | |
e9b11c17 | 1333 | vcpu = kvm_arch_vcpu_create(kvm, n); |
fb3f0f51 RR |
1334 | if (IS_ERR(vcpu)) |
1335 | return PTR_ERR(vcpu); | |
c5ea7660 | 1336 | |
15ad7146 AK |
1337 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
1338 | ||
26e5215f AK |
1339 | r = kvm_arch_vcpu_setup(vcpu); |
1340 | if (r) | |
7d8fece6 | 1341 | return r; |
26e5215f | 1342 | |
11ec2804 | 1343 | mutex_lock(&kvm->lock); |
fb3f0f51 RR |
1344 | if (kvm->vcpus[n]) { |
1345 | r = -EEXIST; | |
e9b11c17 | 1346 | goto vcpu_destroy; |
fb3f0f51 RR |
1347 | } |
1348 | kvm->vcpus[n] = vcpu; | |
11ec2804 | 1349 | mutex_unlock(&kvm->lock); |
c5ea7660 | 1350 | |
fb3f0f51 | 1351 | /* Now it's all set up, let userspace reach it */ |
66c0b394 | 1352 | kvm_get_kvm(kvm); |
bccf2150 AK |
1353 | r = create_vcpu_fd(vcpu); |
1354 | if (r < 0) | |
fb3f0f51 RR |
1355 | goto unlink; |
1356 | return r; | |
39c3b86e | 1357 | |
fb3f0f51 | 1358 | unlink: |
11ec2804 | 1359 | mutex_lock(&kvm->lock); |
fb3f0f51 | 1360 | kvm->vcpus[n] = NULL; |
e9b11c17 | 1361 | vcpu_destroy: |
7d8fece6 | 1362 | mutex_unlock(&kvm->lock); |
d40ccc62 | 1363 | kvm_arch_vcpu_destroy(vcpu); |
c5ea7660 AK |
1364 | return r; |
1365 | } | |
1366 | ||
1961d276 AK |
1367 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
1368 | { | |
1369 | if (sigset) { | |
1370 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
1371 | vcpu->sigset_active = 1; | |
1372 | vcpu->sigset = *sigset; | |
1373 | } else | |
1374 | vcpu->sigset_active = 0; | |
1375 | return 0; | |
1376 | } | |
1377 | ||
bccf2150 AK |
1378 | static long kvm_vcpu_ioctl(struct file *filp, |
1379 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 1380 | { |
bccf2150 | 1381 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 1382 | void __user *argp = (void __user *)arg; |
313a3dc7 | 1383 | int r; |
fa3795a7 DH |
1384 | struct kvm_fpu *fpu = NULL; |
1385 | struct kvm_sregs *kvm_sregs = NULL; | |
6aa8b732 | 1386 | |
6d4e4c4f AK |
1387 | if (vcpu->kvm->mm != current->mm) |
1388 | return -EIO; | |
6aa8b732 | 1389 | switch (ioctl) { |
9a2bb7f4 | 1390 | case KVM_RUN: |
f0fe5108 AK |
1391 | r = -EINVAL; |
1392 | if (arg) | |
1393 | goto out; | |
b6c7a5dc | 1394 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
6aa8b732 | 1395 | break; |
6aa8b732 | 1396 | case KVM_GET_REGS: { |
3e4bb3ac | 1397 | struct kvm_regs *kvm_regs; |
6aa8b732 | 1398 | |
3e4bb3ac XZ |
1399 | r = -ENOMEM; |
1400 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); | |
1401 | if (!kvm_regs) | |
6aa8b732 | 1402 | goto out; |
3e4bb3ac XZ |
1403 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
1404 | if (r) | |
1405 | goto out_free1; | |
6aa8b732 | 1406 | r = -EFAULT; |
3e4bb3ac XZ |
1407 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
1408 | goto out_free1; | |
6aa8b732 | 1409 | r = 0; |
3e4bb3ac XZ |
1410 | out_free1: |
1411 | kfree(kvm_regs); | |
6aa8b732 AK |
1412 | break; |
1413 | } | |
1414 | case KVM_SET_REGS: { | |
3e4bb3ac | 1415 | struct kvm_regs *kvm_regs; |
6aa8b732 | 1416 | |
3e4bb3ac XZ |
1417 | r = -ENOMEM; |
1418 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); | |
1419 | if (!kvm_regs) | |
6aa8b732 | 1420 | goto out; |
3e4bb3ac XZ |
1421 | r = -EFAULT; |
1422 | if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs))) | |
1423 | goto out_free2; | |
1424 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); | |
6aa8b732 | 1425 | if (r) |
3e4bb3ac | 1426 | goto out_free2; |
6aa8b732 | 1427 | r = 0; |
3e4bb3ac XZ |
1428 | out_free2: |
1429 | kfree(kvm_regs); | |
6aa8b732 AK |
1430 | break; |
1431 | } | |
1432 | case KVM_GET_SREGS: { | |
fa3795a7 DH |
1433 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
1434 | r = -ENOMEM; | |
1435 | if (!kvm_sregs) | |
1436 | goto out; | |
1437 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); | |
6aa8b732 AK |
1438 | if (r) |
1439 | goto out; | |
1440 | r = -EFAULT; | |
fa3795a7 | 1441 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
6aa8b732 AK |
1442 | goto out; |
1443 | r = 0; | |
1444 | break; | |
1445 | } | |
1446 | case KVM_SET_SREGS: { | |
fa3795a7 DH |
1447 | kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
1448 | r = -ENOMEM; | |
1449 | if (!kvm_sregs) | |
1450 | goto out; | |
6aa8b732 | 1451 | r = -EFAULT; |
fa3795a7 | 1452 | if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs))) |
6aa8b732 | 1453 | goto out; |
fa3795a7 | 1454 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
6aa8b732 AK |
1455 | if (r) |
1456 | goto out; | |
1457 | r = 0; | |
1458 | break; | |
1459 | } | |
62d9f0db MT |
1460 | case KVM_GET_MP_STATE: { |
1461 | struct kvm_mp_state mp_state; | |
1462 | ||
1463 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); | |
1464 | if (r) | |
1465 | goto out; | |
1466 | r = -EFAULT; | |
1467 | if (copy_to_user(argp, &mp_state, sizeof mp_state)) | |
1468 | goto out; | |
1469 | r = 0; | |
1470 | break; | |
1471 | } | |
1472 | case KVM_SET_MP_STATE: { | |
1473 | struct kvm_mp_state mp_state; | |
1474 | ||
1475 | r = -EFAULT; | |
1476 | if (copy_from_user(&mp_state, argp, sizeof mp_state)) | |
1477 | goto out; | |
1478 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); | |
1479 | if (r) | |
1480 | goto out; | |
1481 | r = 0; | |
1482 | break; | |
1483 | } | |
6aa8b732 AK |
1484 | case KVM_TRANSLATE: { |
1485 | struct kvm_translation tr; | |
1486 | ||
1487 | r = -EFAULT; | |
2f366987 | 1488 | if (copy_from_user(&tr, argp, sizeof tr)) |
6aa8b732 | 1489 | goto out; |
8b006791 | 1490 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
1491 | if (r) |
1492 | goto out; | |
1493 | r = -EFAULT; | |
2f366987 | 1494 | if (copy_to_user(argp, &tr, sizeof tr)) |
6aa8b732 AK |
1495 | goto out; |
1496 | r = 0; | |
1497 | break; | |
1498 | } | |
6aa8b732 AK |
1499 | case KVM_DEBUG_GUEST: { |
1500 | struct kvm_debug_guest dbg; | |
1501 | ||
1502 | r = -EFAULT; | |
2f366987 | 1503 | if (copy_from_user(&dbg, argp, sizeof dbg)) |
6aa8b732 | 1504 | goto out; |
b6c7a5dc | 1505 | r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg); |
6aa8b732 AK |
1506 | if (r) |
1507 | goto out; | |
1508 | r = 0; | |
1509 | break; | |
1510 | } | |
1961d276 AK |
1511 | case KVM_SET_SIGNAL_MASK: { |
1512 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
1513 | struct kvm_signal_mask kvm_sigmask; | |
1514 | sigset_t sigset, *p; | |
1515 | ||
1516 | p = NULL; | |
1517 | if (argp) { | |
1518 | r = -EFAULT; | |
1519 | if (copy_from_user(&kvm_sigmask, argp, | |
1520 | sizeof kvm_sigmask)) | |
1521 | goto out; | |
1522 | r = -EINVAL; | |
1523 | if (kvm_sigmask.len != sizeof sigset) | |
1524 | goto out; | |
1525 | r = -EFAULT; | |
1526 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
1527 | sizeof sigset)) | |
1528 | goto out; | |
1529 | p = &sigset; | |
1530 | } | |
1531 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
1532 | break; | |
1533 | } | |
b8836737 | 1534 | case KVM_GET_FPU: { |
fa3795a7 DH |
1535 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
1536 | r = -ENOMEM; | |
1537 | if (!fpu) | |
1538 | goto out; | |
1539 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); | |
b8836737 AK |
1540 | if (r) |
1541 | goto out; | |
1542 | r = -EFAULT; | |
fa3795a7 | 1543 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
b8836737 AK |
1544 | goto out; |
1545 | r = 0; | |
1546 | break; | |
1547 | } | |
1548 | case KVM_SET_FPU: { | |
fa3795a7 DH |
1549 | fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
1550 | r = -ENOMEM; | |
1551 | if (!fpu) | |
1552 | goto out; | |
b8836737 | 1553 | r = -EFAULT; |
fa3795a7 | 1554 | if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu))) |
b8836737 | 1555 | goto out; |
fa3795a7 | 1556 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
b8836737 AK |
1557 | if (r) |
1558 | goto out; | |
1559 | r = 0; | |
1560 | break; | |
1561 | } | |
bccf2150 | 1562 | default: |
313a3dc7 | 1563 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
1564 | } |
1565 | out: | |
fa3795a7 DH |
1566 | kfree(fpu); |
1567 | kfree(kvm_sregs); | |
bccf2150 AK |
1568 | return r; |
1569 | } | |
1570 | ||
1571 | static long kvm_vm_ioctl(struct file *filp, | |
1572 | unsigned int ioctl, unsigned long arg) | |
1573 | { | |
1574 | struct kvm *kvm = filp->private_data; | |
1575 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 1576 | int r; |
bccf2150 | 1577 | |
6d4e4c4f AK |
1578 | if (kvm->mm != current->mm) |
1579 | return -EIO; | |
bccf2150 AK |
1580 | switch (ioctl) { |
1581 | case KVM_CREATE_VCPU: | |
1582 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
1583 | if (r < 0) | |
1584 | goto out; | |
1585 | break; | |
6fc138d2 IE |
1586 | case KVM_SET_USER_MEMORY_REGION: { |
1587 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
1588 | ||
1589 | r = -EFAULT; | |
1590 | if (copy_from_user(&kvm_userspace_mem, argp, | |
1591 | sizeof kvm_userspace_mem)) | |
1592 | goto out; | |
1593 | ||
1594 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1); | |
6aa8b732 AK |
1595 | if (r) |
1596 | goto out; | |
1597 | break; | |
1598 | } | |
1599 | case KVM_GET_DIRTY_LOG: { | |
1600 | struct kvm_dirty_log log; | |
1601 | ||
1602 | r = -EFAULT; | |
2f366987 | 1603 | if (copy_from_user(&log, argp, sizeof log)) |
6aa8b732 | 1604 | goto out; |
2c6f5df9 | 1605 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
1606 | if (r) |
1607 | goto out; | |
1608 | break; | |
1609 | } | |
5f94c174 LV |
1610 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
1611 | case KVM_REGISTER_COALESCED_MMIO: { | |
1612 | struct kvm_coalesced_mmio_zone zone; | |
1613 | r = -EFAULT; | |
1614 | if (copy_from_user(&zone, argp, sizeof zone)) | |
1615 | goto out; | |
1616 | r = -ENXIO; | |
1617 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); | |
1618 | if (r) | |
1619 | goto out; | |
1620 | r = 0; | |
1621 | break; | |
1622 | } | |
1623 | case KVM_UNREGISTER_COALESCED_MMIO: { | |
1624 | struct kvm_coalesced_mmio_zone zone; | |
1625 | r = -EFAULT; | |
1626 | if (copy_from_user(&zone, argp, sizeof zone)) | |
1627 | goto out; | |
1628 | r = -ENXIO; | |
1629 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); | |
1630 | if (r) | |
1631 | goto out; | |
1632 | r = 0; | |
1633 | break; | |
1634 | } | |
8a98f664 XZ |
1635 | #endif |
1636 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT | |
1637 | case KVM_ASSIGN_PCI_DEVICE: { | |
1638 | struct kvm_assigned_pci_dev assigned_dev; | |
1639 | ||
1640 | r = -EFAULT; | |
1641 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) | |
1642 | goto out; | |
1643 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); | |
1644 | if (r) | |
1645 | goto out; | |
1646 | break; | |
1647 | } | |
1648 | case KVM_ASSIGN_IRQ: { | |
1649 | struct kvm_assigned_irq assigned_irq; | |
1650 | ||
1651 | r = -EFAULT; | |
1652 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) | |
1653 | goto out; | |
1654 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); | |
1655 | if (r) | |
1656 | goto out; | |
1657 | break; | |
1658 | } | |
5f94c174 | 1659 | #endif |
f17abe9a | 1660 | default: |
1fe779f8 | 1661 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
1662 | } |
1663 | out: | |
1664 | return r; | |
1665 | } | |
1666 | ||
e4a533a4 | 1667 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
f17abe9a | 1668 | { |
777b3f49 MT |
1669 | struct page *page[1]; |
1670 | unsigned long addr; | |
1671 | int npages; | |
1672 | gfn_t gfn = vmf->pgoff; | |
f17abe9a | 1673 | struct kvm *kvm = vma->vm_file->private_data; |
f17abe9a | 1674 | |
777b3f49 MT |
1675 | addr = gfn_to_hva(kvm, gfn); |
1676 | if (kvm_is_error_hva(addr)) | |
e4a533a4 | 1677 | return VM_FAULT_SIGBUS; |
777b3f49 MT |
1678 | |
1679 | npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page, | |
1680 | NULL); | |
1681 | if (unlikely(npages != 1)) | |
e4a533a4 | 1682 | return VM_FAULT_SIGBUS; |
777b3f49 MT |
1683 | |
1684 | vmf->page = page[0]; | |
e4a533a4 | 1685 | return 0; |
f17abe9a AK |
1686 | } |
1687 | ||
1688 | static struct vm_operations_struct kvm_vm_vm_ops = { | |
e4a533a4 | 1689 | .fault = kvm_vm_fault, |
f17abe9a AK |
1690 | }; |
1691 | ||
1692 | static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |
1693 | { | |
1694 | vma->vm_ops = &kvm_vm_vm_ops; | |
1695 | return 0; | |
1696 | } | |
1697 | ||
5c502742 | 1698 | static const struct file_operations kvm_vm_fops = { |
f17abe9a AK |
1699 | .release = kvm_vm_release, |
1700 | .unlocked_ioctl = kvm_vm_ioctl, | |
1701 | .compat_ioctl = kvm_vm_ioctl, | |
1702 | .mmap = kvm_vm_mmap, | |
1703 | }; | |
1704 | ||
1705 | static int kvm_dev_ioctl_create_vm(void) | |
1706 | { | |
2030a42c | 1707 | int fd; |
f17abe9a AK |
1708 | struct kvm *kvm; |
1709 | ||
f17abe9a | 1710 | kvm = kvm_create_vm(); |
d6d28168 AK |
1711 | if (IS_ERR(kvm)) |
1712 | return PTR_ERR(kvm); | |
7d9dbca3 | 1713 | fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0); |
2030a42c | 1714 | if (fd < 0) |
66c0b394 | 1715 | kvm_put_kvm(kvm); |
f17abe9a | 1716 | |
f17abe9a | 1717 | return fd; |
f17abe9a AK |
1718 | } |
1719 | ||
1720 | static long kvm_dev_ioctl(struct file *filp, | |
1721 | unsigned int ioctl, unsigned long arg) | |
1722 | { | |
07c45a36 | 1723 | long r = -EINVAL; |
f17abe9a AK |
1724 | |
1725 | switch (ioctl) { | |
1726 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
1727 | r = -EINVAL; |
1728 | if (arg) | |
1729 | goto out; | |
f17abe9a AK |
1730 | r = KVM_API_VERSION; |
1731 | break; | |
1732 | case KVM_CREATE_VM: | |
f0fe5108 AK |
1733 | r = -EINVAL; |
1734 | if (arg) | |
1735 | goto out; | |
f17abe9a AK |
1736 | r = kvm_dev_ioctl_create_vm(); |
1737 | break; | |
018d00d2 | 1738 | case KVM_CHECK_EXTENSION: |
1e1c65e0 | 1739 | r = kvm_dev_ioctl_check_extension(arg); |
5d308f45 | 1740 | break; |
07c45a36 AK |
1741 | case KVM_GET_VCPU_MMAP_SIZE: |
1742 | r = -EINVAL; | |
1743 | if (arg) | |
1744 | goto out; | |
adb1ff46 AK |
1745 | r = PAGE_SIZE; /* struct kvm_run */ |
1746 | #ifdef CONFIG_X86 | |
1747 | r += PAGE_SIZE; /* pio data page */ | |
5f94c174 LV |
1748 | #endif |
1749 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
1750 | r += PAGE_SIZE; /* coalesced mmio ring page */ | |
adb1ff46 | 1751 | #endif |
07c45a36 | 1752 | break; |
d4c9ff2d FEL |
1753 | case KVM_TRACE_ENABLE: |
1754 | case KVM_TRACE_PAUSE: | |
1755 | case KVM_TRACE_DISABLE: | |
1756 | r = kvm_trace_ioctl(ioctl, arg); | |
1757 | break; | |
6aa8b732 | 1758 | default: |
043405e1 | 1759 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
1760 | } |
1761 | out: | |
1762 | return r; | |
1763 | } | |
1764 | ||
6aa8b732 | 1765 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
1766 | .unlocked_ioctl = kvm_dev_ioctl, |
1767 | .compat_ioctl = kvm_dev_ioctl, | |
6aa8b732 AK |
1768 | }; |
1769 | ||
1770 | static struct miscdevice kvm_dev = { | |
bbe4432e | 1771 | KVM_MINOR, |
6aa8b732 AK |
1772 | "kvm", |
1773 | &kvm_chardev_ops, | |
1774 | }; | |
1775 | ||
1b6c0168 AK |
1776 | static void hardware_enable(void *junk) |
1777 | { | |
1778 | int cpu = raw_smp_processor_id(); | |
1779 | ||
1780 | if (cpu_isset(cpu, cpus_hardware_enabled)) | |
1781 | return; | |
1782 | cpu_set(cpu, cpus_hardware_enabled); | |
e9b11c17 | 1783 | kvm_arch_hardware_enable(NULL); |
1b6c0168 AK |
1784 | } |
1785 | ||
1786 | static void hardware_disable(void *junk) | |
1787 | { | |
1788 | int cpu = raw_smp_processor_id(); | |
1789 | ||
1790 | if (!cpu_isset(cpu, cpus_hardware_enabled)) | |
1791 | return; | |
1792 | cpu_clear(cpu, cpus_hardware_enabled); | |
e9b11c17 | 1793 | kvm_arch_hardware_disable(NULL); |
1b6c0168 AK |
1794 | } |
1795 | ||
774c47f1 AK |
1796 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
1797 | void *v) | |
1798 | { | |
1799 | int cpu = (long)v; | |
1800 | ||
1a6f4d7f | 1801 | val &= ~CPU_TASKS_FROZEN; |
774c47f1 | 1802 | switch (val) { |
cec9ad27 | 1803 | case CPU_DYING: |
6ec8a856 AK |
1804 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1805 | cpu); | |
1806 | hardware_disable(NULL); | |
1807 | break; | |
774c47f1 | 1808 | case CPU_UP_CANCELED: |
43934a38 JK |
1809 | printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n", |
1810 | cpu); | |
8691e5a8 | 1811 | smp_call_function_single(cpu, hardware_disable, NULL, 1); |
774c47f1 | 1812 | break; |
43934a38 JK |
1813 | case CPU_ONLINE: |
1814 | printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n", | |
1815 | cpu); | |
8691e5a8 | 1816 | smp_call_function_single(cpu, hardware_enable, NULL, 1); |
774c47f1 AK |
1817 | break; |
1818 | } | |
1819 | return NOTIFY_OK; | |
1820 | } | |
1821 | ||
4ecac3fd AK |
1822 | |
1823 | asmlinkage void kvm_handle_fault_on_reboot(void) | |
1824 | { | |
1825 | if (kvm_rebooting) | |
1826 | /* spin while reset goes on */ | |
1827 | while (true) | |
1828 | ; | |
1829 | /* Fault while not rebooting. We want the trace. */ | |
1830 | BUG(); | |
1831 | } | |
1832 | EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot); | |
1833 | ||
9a2b85c6 | 1834 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 1835 | void *v) |
9a2b85c6 RR |
1836 | { |
1837 | if (val == SYS_RESTART) { | |
1838 | /* | |
1839 | * Some (well, at least mine) BIOSes hang on reboot if | |
1840 | * in vmx root mode. | |
1841 | */ | |
1842 | printk(KERN_INFO "kvm: exiting hardware virtualization\n"); | |
4ecac3fd | 1843 | kvm_rebooting = true; |
15c8b6c1 | 1844 | on_each_cpu(hardware_disable, NULL, 1); |
9a2b85c6 RR |
1845 | } |
1846 | return NOTIFY_OK; | |
1847 | } | |
1848 | ||
1849 | static struct notifier_block kvm_reboot_notifier = { | |
1850 | .notifier_call = kvm_reboot, | |
1851 | .priority = 0, | |
1852 | }; | |
1853 | ||
2eeb2e94 GH |
1854 | void kvm_io_bus_init(struct kvm_io_bus *bus) |
1855 | { | |
1856 | memset(bus, 0, sizeof(*bus)); | |
1857 | } | |
1858 | ||
1859 | void kvm_io_bus_destroy(struct kvm_io_bus *bus) | |
1860 | { | |
1861 | int i; | |
1862 | ||
1863 | for (i = 0; i < bus->dev_count; i++) { | |
1864 | struct kvm_io_device *pos = bus->devs[i]; | |
1865 | ||
1866 | kvm_iodevice_destructor(pos); | |
1867 | } | |
1868 | } | |
1869 | ||
92760499 LV |
1870 | struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, |
1871 | gpa_t addr, int len, int is_write) | |
2eeb2e94 GH |
1872 | { |
1873 | int i; | |
1874 | ||
1875 | for (i = 0; i < bus->dev_count; i++) { | |
1876 | struct kvm_io_device *pos = bus->devs[i]; | |
1877 | ||
92760499 | 1878 | if (pos->in_range(pos, addr, len, is_write)) |
2eeb2e94 GH |
1879 | return pos; |
1880 | } | |
1881 | ||
1882 | return NULL; | |
1883 | } | |
1884 | ||
1885 | void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev) | |
1886 | { | |
1887 | BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1)); | |
1888 | ||
1889 | bus->devs[bus->dev_count++] = dev; | |
1890 | } | |
1891 | ||
774c47f1 AK |
1892 | static struct notifier_block kvm_cpu_notifier = { |
1893 | .notifier_call = kvm_cpu_hotplug, | |
1894 | .priority = 20, /* must be > scheduler priority */ | |
1895 | }; | |
1896 | ||
8b88b099 | 1897 | static int vm_stat_get(void *_offset, u64 *val) |
ba1389b7 AK |
1898 | { |
1899 | unsigned offset = (long)_offset; | |
ba1389b7 AK |
1900 | struct kvm *kvm; |
1901 | ||
8b88b099 | 1902 | *val = 0; |
ba1389b7 AK |
1903 | spin_lock(&kvm_lock); |
1904 | list_for_each_entry(kvm, &vm_list, vm_list) | |
8b88b099 | 1905 | *val += *(u32 *)((void *)kvm + offset); |
ba1389b7 | 1906 | spin_unlock(&kvm_lock); |
8b88b099 | 1907 | return 0; |
ba1389b7 AK |
1908 | } |
1909 | ||
1910 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); | |
1911 | ||
8b88b099 | 1912 | static int vcpu_stat_get(void *_offset, u64 *val) |
1165f5fe AK |
1913 | { |
1914 | unsigned offset = (long)_offset; | |
1165f5fe AK |
1915 | struct kvm *kvm; |
1916 | struct kvm_vcpu *vcpu; | |
1917 | int i; | |
1918 | ||
8b88b099 | 1919 | *val = 0; |
1165f5fe AK |
1920 | spin_lock(&kvm_lock); |
1921 | list_for_each_entry(kvm, &vm_list, vm_list) | |
1922 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { | |
fb3f0f51 RR |
1923 | vcpu = kvm->vcpus[i]; |
1924 | if (vcpu) | |
8b88b099 | 1925 | *val += *(u32 *)((void *)vcpu + offset); |
1165f5fe AK |
1926 | } |
1927 | spin_unlock(&kvm_lock); | |
8b88b099 | 1928 | return 0; |
1165f5fe AK |
1929 | } |
1930 | ||
ba1389b7 AK |
1931 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
1932 | ||
1933 | static struct file_operations *stat_fops[] = { | |
1934 | [KVM_STAT_VCPU] = &vcpu_stat_fops, | |
1935 | [KVM_STAT_VM] = &vm_stat_fops, | |
1936 | }; | |
1165f5fe | 1937 | |
a16b043c | 1938 | static void kvm_init_debug(void) |
6aa8b732 AK |
1939 | { |
1940 | struct kvm_stats_debugfs_item *p; | |
1941 | ||
76f7c879 | 1942 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
6aa8b732 | 1943 | for (p = debugfs_entries; p->name; ++p) |
76f7c879 | 1944 | p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
1165f5fe | 1945 | (void *)(long)p->offset, |
ba1389b7 | 1946 | stat_fops[p->kind]); |
6aa8b732 AK |
1947 | } |
1948 | ||
1949 | static void kvm_exit_debug(void) | |
1950 | { | |
1951 | struct kvm_stats_debugfs_item *p; | |
1952 | ||
1953 | for (p = debugfs_entries; p->name; ++p) | |
1954 | debugfs_remove(p->dentry); | |
76f7c879 | 1955 | debugfs_remove(kvm_debugfs_dir); |
6aa8b732 AK |
1956 | } |
1957 | ||
59ae6c6b AK |
1958 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
1959 | { | |
4267c41a | 1960 | hardware_disable(NULL); |
59ae6c6b AK |
1961 | return 0; |
1962 | } | |
1963 | ||
1964 | static int kvm_resume(struct sys_device *dev) | |
1965 | { | |
4267c41a | 1966 | hardware_enable(NULL); |
59ae6c6b AK |
1967 | return 0; |
1968 | } | |
1969 | ||
1970 | static struct sysdev_class kvm_sysdev_class = { | |
af5ca3f4 | 1971 | .name = "kvm", |
59ae6c6b AK |
1972 | .suspend = kvm_suspend, |
1973 | .resume = kvm_resume, | |
1974 | }; | |
1975 | ||
1976 | static struct sys_device kvm_sysdev = { | |
1977 | .id = 0, | |
1978 | .cls = &kvm_sysdev_class, | |
1979 | }; | |
1980 | ||
cea7bb21 | 1981 | struct page *bad_page; |
35149e21 | 1982 | pfn_t bad_pfn; |
6aa8b732 | 1983 | |
15ad7146 AK |
1984 | static inline |
1985 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
1986 | { | |
1987 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
1988 | } | |
1989 | ||
1990 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
1991 | { | |
1992 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
1993 | ||
e9b11c17 | 1994 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 AK |
1995 | } |
1996 | ||
1997 | static void kvm_sched_out(struct preempt_notifier *pn, | |
1998 | struct task_struct *next) | |
1999 | { | |
2000 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
2001 | ||
e9b11c17 | 2002 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
2003 | } |
2004 | ||
f8c16bba | 2005 | int kvm_init(void *opaque, unsigned int vcpu_size, |
c16f862d | 2006 | struct module *module) |
6aa8b732 AK |
2007 | { |
2008 | int r; | |
002c7f7c | 2009 | int cpu; |
6aa8b732 | 2010 | |
cb498ea2 ZX |
2011 | kvm_init_debug(); |
2012 | ||
f8c16bba ZX |
2013 | r = kvm_arch_init(opaque); |
2014 | if (r) | |
d2308784 | 2015 | goto out_fail; |
cb498ea2 ZX |
2016 | |
2017 | bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
2018 | ||
2019 | if (bad_page == NULL) { | |
2020 | r = -ENOMEM; | |
2021 | goto out; | |
2022 | } | |
2023 | ||
35149e21 AL |
2024 | bad_pfn = page_to_pfn(bad_page); |
2025 | ||
e9b11c17 | 2026 | r = kvm_arch_hardware_setup(); |
6aa8b732 | 2027 | if (r < 0) |
d2308784 | 2028 | goto out_free_0; |
6aa8b732 | 2029 | |
002c7f7c YS |
2030 | for_each_online_cpu(cpu) { |
2031 | smp_call_function_single(cpu, | |
e9b11c17 | 2032 | kvm_arch_check_processor_compat, |
8691e5a8 | 2033 | &r, 1); |
002c7f7c | 2034 | if (r < 0) |
d2308784 | 2035 | goto out_free_1; |
002c7f7c YS |
2036 | } |
2037 | ||
15c8b6c1 | 2038 | on_each_cpu(hardware_enable, NULL, 1); |
774c47f1 AK |
2039 | r = register_cpu_notifier(&kvm_cpu_notifier); |
2040 | if (r) | |
d2308784 | 2041 | goto out_free_2; |
6aa8b732 AK |
2042 | register_reboot_notifier(&kvm_reboot_notifier); |
2043 | ||
59ae6c6b AK |
2044 | r = sysdev_class_register(&kvm_sysdev_class); |
2045 | if (r) | |
d2308784 | 2046 | goto out_free_3; |
59ae6c6b AK |
2047 | |
2048 | r = sysdev_register(&kvm_sysdev); | |
2049 | if (r) | |
d2308784 | 2050 | goto out_free_4; |
59ae6c6b | 2051 | |
c16f862d RR |
2052 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
2053 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, | |
56919c5c JP |
2054 | __alignof__(struct kvm_vcpu), |
2055 | 0, NULL); | |
c16f862d RR |
2056 | if (!kvm_vcpu_cache) { |
2057 | r = -ENOMEM; | |
d2308784 | 2058 | goto out_free_5; |
c16f862d RR |
2059 | } |
2060 | ||
6aa8b732 AK |
2061 | kvm_chardev_ops.owner = module; |
2062 | ||
2063 | r = misc_register(&kvm_dev); | |
2064 | if (r) { | |
d77c26fc | 2065 | printk(KERN_ERR "kvm: misc device register failed\n"); |
6aa8b732 AK |
2066 | goto out_free; |
2067 | } | |
2068 | ||
15ad7146 AK |
2069 | kvm_preempt_ops.sched_in = kvm_sched_in; |
2070 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
2071 | ||
c7addb90 | 2072 | return 0; |
6aa8b732 AK |
2073 | |
2074 | out_free: | |
c16f862d | 2075 | kmem_cache_destroy(kvm_vcpu_cache); |
d2308784 | 2076 | out_free_5: |
59ae6c6b | 2077 | sysdev_unregister(&kvm_sysdev); |
d2308784 | 2078 | out_free_4: |
59ae6c6b | 2079 | sysdev_class_unregister(&kvm_sysdev_class); |
d2308784 | 2080 | out_free_3: |
6aa8b732 | 2081 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 | 2082 | unregister_cpu_notifier(&kvm_cpu_notifier); |
d2308784 | 2083 | out_free_2: |
15c8b6c1 | 2084 | on_each_cpu(hardware_disable, NULL, 1); |
d2308784 | 2085 | out_free_1: |
e9b11c17 | 2086 | kvm_arch_hardware_unsetup(); |
d2308784 ZX |
2087 | out_free_0: |
2088 | __free_page(bad_page); | |
ca45aaae | 2089 | out: |
f8c16bba | 2090 | kvm_arch_exit(); |
cb498ea2 | 2091 | kvm_exit_debug(); |
d2308784 | 2092 | out_fail: |
6aa8b732 AK |
2093 | return r; |
2094 | } | |
cb498ea2 | 2095 | EXPORT_SYMBOL_GPL(kvm_init); |
6aa8b732 | 2096 | |
cb498ea2 | 2097 | void kvm_exit(void) |
6aa8b732 | 2098 | { |
d4c9ff2d | 2099 | kvm_trace_cleanup(); |
6aa8b732 | 2100 | misc_deregister(&kvm_dev); |
c16f862d | 2101 | kmem_cache_destroy(kvm_vcpu_cache); |
59ae6c6b AK |
2102 | sysdev_unregister(&kvm_sysdev); |
2103 | sysdev_class_unregister(&kvm_sysdev_class); | |
6aa8b732 | 2104 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 2105 | unregister_cpu_notifier(&kvm_cpu_notifier); |
15c8b6c1 | 2106 | on_each_cpu(hardware_disable, NULL, 1); |
e9b11c17 | 2107 | kvm_arch_hardware_unsetup(); |
f8c16bba | 2108 | kvm_arch_exit(); |
6aa8b732 | 2109 | kvm_exit_debug(); |
cea7bb21 | 2110 | __free_page(bad_page); |
6aa8b732 | 2111 | } |
cb498ea2 | 2112 | EXPORT_SYMBOL_GPL(kvm_exit); |