diff options
Diffstat (limited to 'virt/kvm/kvm_main.c')
-rw-r--r-- | virt/kvm/kvm_main.c | 961 |
1 files changed, 147 insertions, 814 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7495ce347344..f92ba138007a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -43,6 +43,7 @@ | |||
43 | #include <linux/swap.h> | 43 | #include <linux/swap.h> |
44 | #include <linux/bitops.h> | 44 | #include <linux/bitops.h> |
45 | #include <linux/spinlock.h> | 45 | #include <linux/spinlock.h> |
46 | #include <linux/compat.h> | ||
46 | 47 | ||
47 | #include <asm/processor.h> | 48 | #include <asm/processor.h> |
48 | #include <asm/io.h> | 49 | #include <asm/io.h> |
@@ -53,12 +54,6 @@ | |||
53 | #include "coalesced_mmio.h" | 54 | #include "coalesced_mmio.h" |
54 | #endif | 55 | #endif |
55 | 56 | ||
56 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT | ||
57 | #include <linux/pci.h> | ||
58 | #include <linux/interrupt.h> | ||
59 | #include "irq.h" | ||
60 | #endif | ||
61 | |||
62 | #define CREATE_TRACE_POINTS | 57 | #define CREATE_TRACE_POINTS |
63 | #include <trace/events/kvm.h> | 58 | #include <trace/events/kvm.h> |
64 | 59 | ||
@@ -75,6 +70,8 @@ DEFINE_SPINLOCK(kvm_lock); | |||
75 | LIST_HEAD(vm_list); | 70 | LIST_HEAD(vm_list); |
76 | 71 | ||
77 | static cpumask_var_t cpus_hardware_enabled; | 72 | static cpumask_var_t cpus_hardware_enabled; |
73 | static int kvm_usage_count = 0; | ||
74 | static atomic_t hardware_enable_failed; | ||
78 | 75 | ||
79 | struct kmem_cache *kvm_vcpu_cache; | 76 | struct kmem_cache *kvm_vcpu_cache; |
80 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | 77 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); |
@@ -85,615 +82,13 @@ struct dentry *kvm_debugfs_dir; | |||
85 | 82 | ||
86 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, | 83 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
87 | unsigned long arg); | 84 | unsigned long arg); |
85 | static int hardware_enable_all(void); | ||
86 | static void hardware_disable_all(void); | ||
88 | 87 | ||
89 | static bool kvm_rebooting; | 88 | static bool kvm_rebooting; |
90 | 89 | ||
91 | static bool largepages_enabled = true; | 90 | static bool largepages_enabled = true; |
92 | 91 | ||
93 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT | ||
94 | static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head, | ||
95 | int assigned_dev_id) | ||
96 | { | ||
97 | struct list_head *ptr; | ||
98 | struct kvm_assigned_dev_kernel *match; | ||
99 | |||
100 | list_for_each(ptr, head) { | ||
101 | match = list_entry(ptr, struct kvm_assigned_dev_kernel, list); | ||
102 | if (match->assigned_dev_id == assigned_dev_id) | ||
103 | return match; | ||
104 | } | ||
105 | return NULL; | ||
106 | } | ||
107 | |||
108 | static int find_index_from_host_irq(struct kvm_assigned_dev_kernel | ||
109 | *assigned_dev, int irq) | ||
110 | { | ||
111 | int i, index; | ||
112 | struct msix_entry *host_msix_entries; | ||
113 | |||
114 | host_msix_entries = assigned_dev->host_msix_entries; | ||
115 | |||
116 | index = -1; | ||
117 | for (i = 0; i < assigned_dev->entries_nr; i++) | ||
118 | if (irq == host_msix_entries[i].vector) { | ||
119 | index = i; | ||
120 | break; | ||
121 | } | ||
122 | if (index < 0) { | ||
123 | printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n"); | ||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | return index; | ||
128 | } | ||
129 | |||
130 | static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work) | ||
131 | { | ||
132 | struct kvm_assigned_dev_kernel *assigned_dev; | ||
133 | struct kvm *kvm; | ||
134 | int i; | ||
135 | |||
136 | assigned_dev = container_of(work, struct kvm_assigned_dev_kernel, | ||
137 | interrupt_work); | ||
138 | kvm = assigned_dev->kvm; | ||
139 | |||
140 | mutex_lock(&kvm->irq_lock); | ||
141 | spin_lock_irq(&assigned_dev->assigned_dev_lock); | ||
142 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { | ||
143 | struct kvm_guest_msix_entry *guest_entries = | ||
144 | assigned_dev->guest_msix_entries; | ||
145 | for (i = 0; i < assigned_dev->entries_nr; i++) { | ||
146 | if (!(guest_entries[i].flags & | ||
147 | KVM_ASSIGNED_MSIX_PENDING)) | ||
148 | continue; | ||
149 | guest_entries[i].flags &= ~KVM_ASSIGNED_MSIX_PENDING; | ||
150 | kvm_set_irq(assigned_dev->kvm, | ||
151 | assigned_dev->irq_source_id, | ||
152 | guest_entries[i].vector, 1); | ||
153 | } | ||
154 | } else | ||
155 | kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, | ||
156 | assigned_dev->guest_irq, 1); | ||
157 | |||
158 | spin_unlock_irq(&assigned_dev->assigned_dev_lock); | ||
159 | mutex_unlock(&assigned_dev->kvm->irq_lock); | ||
160 | } | ||
161 | |||
162 | static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id) | ||
163 | { | ||
164 | unsigned long flags; | ||
165 | struct kvm_assigned_dev_kernel *assigned_dev = | ||
166 | (struct kvm_assigned_dev_kernel *) dev_id; | ||
167 | |||
168 | spin_lock_irqsave(&assigned_dev->assigned_dev_lock, flags); | ||
169 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { | ||
170 | int index = find_index_from_host_irq(assigned_dev, irq); | ||
171 | if (index < 0) | ||
172 | goto out; | ||
173 | assigned_dev->guest_msix_entries[index].flags |= | ||
174 | KVM_ASSIGNED_MSIX_PENDING; | ||
175 | } | ||
176 | |||
177 | schedule_work(&assigned_dev->interrupt_work); | ||
178 | |||
179 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) { | ||
180 | disable_irq_nosync(irq); | ||
181 | assigned_dev->host_irq_disabled = true; | ||
182 | } | ||
183 | |||
184 | out: | ||
185 | spin_unlock_irqrestore(&assigned_dev->assigned_dev_lock, flags); | ||
186 | return IRQ_HANDLED; | ||
187 | } | ||
188 | |||
189 | /* Ack the irq line for an assigned device */ | ||
190 | static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian) | ||
191 | { | ||
192 | struct kvm_assigned_dev_kernel *dev; | ||
193 | unsigned long flags; | ||
194 | |||
195 | if (kian->gsi == -1) | ||
196 | return; | ||
197 | |||
198 | dev = container_of(kian, struct kvm_assigned_dev_kernel, | ||
199 | ack_notifier); | ||
200 | |||
201 | kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0); | ||
202 | |||
203 | /* The guest irq may be shared so this ack may be | ||
204 | * from another device. | ||
205 | */ | ||
206 | spin_lock_irqsave(&dev->assigned_dev_lock, flags); | ||
207 | if (dev->host_irq_disabled) { | ||
208 | enable_irq(dev->host_irq); | ||
209 | dev->host_irq_disabled = false; | ||
210 | } | ||
211 | spin_unlock_irqrestore(&dev->assigned_dev_lock, flags); | ||
212 | } | ||
213 | |||
214 | static void deassign_guest_irq(struct kvm *kvm, | ||
215 | struct kvm_assigned_dev_kernel *assigned_dev) | ||
216 | { | ||
217 | kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier); | ||
218 | assigned_dev->ack_notifier.gsi = -1; | ||
219 | |||
220 | if (assigned_dev->irq_source_id != -1) | ||
221 | kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id); | ||
222 | assigned_dev->irq_source_id = -1; | ||
223 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_GUEST_MASK); | ||
224 | } | ||
225 | |||
226 | /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */ | ||
227 | static void deassign_host_irq(struct kvm *kvm, | ||
228 | struct kvm_assigned_dev_kernel *assigned_dev) | ||
229 | { | ||
230 | /* | ||
231 | * In kvm_free_device_irq, cancel_work_sync return true if: | ||
232 | * 1. work is scheduled, and then cancelled. | ||
233 | * 2. work callback is executed. | ||
234 | * | ||
235 | * The first one ensured that the irq is disabled and no more events | ||
236 | * would happen. But for the second one, the irq may be enabled (e.g. | ||
237 | * for MSI). So we disable irq here to prevent further events. | ||
238 | * | ||
239 | * Notice this maybe result in nested disable if the interrupt type is | ||
240 | * INTx, but it's OK for we are going to free it. | ||
241 | * | ||
242 | * If this function is a part of VM destroy, please ensure that till | ||
243 | * now, the kvm state is still legal for probably we also have to wait | ||
244 | * interrupt_work done. | ||
245 | */ | ||
246 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX) { | ||
247 | int i; | ||
248 | for (i = 0; i < assigned_dev->entries_nr; i++) | ||
249 | disable_irq_nosync(assigned_dev-> | ||
250 | host_msix_entries[i].vector); | ||
251 | |||
252 | cancel_work_sync(&assigned_dev->interrupt_work); | ||
253 | |||
254 | for (i = 0; i < assigned_dev->entries_nr; i++) | ||
255 | free_irq(assigned_dev->host_msix_entries[i].vector, | ||
256 | (void *)assigned_dev); | ||
257 | |||
258 | assigned_dev->entries_nr = 0; | ||
259 | kfree(assigned_dev->host_msix_entries); | ||
260 | kfree(assigned_dev->guest_msix_entries); | ||
261 | pci_disable_msix(assigned_dev->dev); | ||
262 | } else { | ||
263 | /* Deal with MSI and INTx */ | ||
264 | disable_irq_nosync(assigned_dev->host_irq); | ||
265 | cancel_work_sync(&assigned_dev->interrupt_work); | ||
266 | |||
267 | free_irq(assigned_dev->host_irq, (void *)assigned_dev); | ||
268 | |||
269 | if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI) | ||
270 | pci_disable_msi(assigned_dev->dev); | ||
271 | } | ||
272 | |||
273 | assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK); | ||
274 | } | ||
275 | |||
276 | static int kvm_deassign_irq(struct kvm *kvm, | ||
277 | struct kvm_assigned_dev_kernel *assigned_dev, | ||
278 | unsigned long irq_requested_type) | ||
279 | { | ||
280 | unsigned long guest_irq_type, host_irq_type; | ||
281 | |||
282 | if (!irqchip_in_kernel(kvm)) | ||
283 | return -EINVAL; | ||
284 | /* no irq assignment to deassign */ | ||
285 | if (!assigned_dev->irq_requested_type) | ||
286 | return -ENXIO; | ||
287 | |||
288 | host_irq_type = irq_requested_type & KVM_DEV_IRQ_HOST_MASK; | ||
289 | guest_irq_type = irq_requested_type & KVM_DEV_IRQ_GUEST_MASK; | ||
290 | |||
291 | if (host_irq_type) | ||
292 | deassign_host_irq(kvm, assigned_dev); | ||
293 | if (guest_irq_type) | ||
294 | deassign_guest_irq(kvm, assigned_dev); | ||
295 | |||
296 | return 0; | ||
297 | } | ||
298 | |||
299 | static void kvm_free_assigned_irq(struct kvm *kvm, | ||
300 | struct kvm_assigned_dev_kernel *assigned_dev) | ||
301 | { | ||
302 | kvm_deassign_irq(kvm, assigned_dev, assigned_dev->irq_requested_type); | ||
303 | } | ||
304 | |||
305 | static void kvm_free_assigned_device(struct kvm *kvm, | ||
306 | struct kvm_assigned_dev_kernel | ||
307 | *assigned_dev) | ||
308 | { | ||
309 | kvm_free_assigned_irq(kvm, assigned_dev); | ||
310 | |||
311 | pci_reset_function(assigned_dev->dev); | ||
312 | |||
313 | pci_release_regions(assigned_dev->dev); | ||
314 | pci_disable_device(assigned_dev->dev); | ||
315 | pci_dev_put(assigned_dev->dev); | ||
316 | |||
317 | list_del(&assigned_dev->list); | ||
318 | kfree(assigned_dev); | ||
319 | } | ||
320 | |||
321 | void kvm_free_all_assigned_devices(struct kvm *kvm) | ||
322 | { | ||
323 | struct list_head *ptr, *ptr2; | ||
324 | struct kvm_assigned_dev_kernel *assigned_dev; | ||
325 | |||
326 | list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) { | ||
327 | assigned_dev = list_entry(ptr, | ||
328 | struct kvm_assigned_dev_kernel, | ||
329 | list); | ||
330 | |||
331 | kvm_free_assigned_device(kvm, assigned_dev); | ||
332 | } | ||
333 | } | ||
334 | |||
335 | static int assigned_device_enable_host_intx(struct kvm *kvm, | ||
336 | struct kvm_assigned_dev_kernel *dev) | ||
337 | { | ||
338 | dev->host_irq = dev->dev->irq; | ||
339 | /* Even though this is PCI, we don't want to use shared | ||
340 | * interrupts. Sharing host devices with guest-assigned devices | ||
341 | * on the same interrupt line is not a happy situation: there | ||
342 | * are going to be long delays in accepting, acking, etc. | ||
343 | */ | ||
344 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, | ||
345 | 0, "kvm_assigned_intx_device", (void *)dev)) | ||
346 | return -EIO; | ||
347 | return 0; | ||
348 | } | ||
349 | |||
350 | #ifdef __KVM_HAVE_MSI | ||
351 | static int assigned_device_enable_host_msi(struct kvm *kvm, | ||
352 | struct kvm_assigned_dev_kernel *dev) | ||
353 | { | ||
354 | int r; | ||
355 | |||
356 | if (!dev->dev->msi_enabled) { | ||
357 | r = pci_enable_msi(dev->dev); | ||
358 | if (r) | ||
359 | return r; | ||
360 | } | ||
361 | |||
362 | dev->host_irq = dev->dev->irq; | ||
363 | if (request_irq(dev->host_irq, kvm_assigned_dev_intr, 0, | ||
364 | "kvm_assigned_msi_device", (void *)dev)) { | ||
365 | pci_disable_msi(dev->dev); | ||
366 | return -EIO; | ||
367 | } | ||
368 | |||
369 | return 0; | ||
370 | } | ||
371 | #endif | ||
372 | |||
373 | #ifdef __KVM_HAVE_MSIX | ||
374 | static int assigned_device_enable_host_msix(struct kvm *kvm, | ||
375 | struct kvm_assigned_dev_kernel *dev) | ||
376 | { | ||
377 | int i, r = -EINVAL; | ||
378 | |||
379 | /* host_msix_entries and guest_msix_entries should have been | ||
380 | * initialized */ | ||
381 | if (dev->entries_nr == 0) | ||
382 | return r; | ||
383 | |||
384 | r = pci_enable_msix(dev->dev, dev->host_msix_entries, dev->entries_nr); | ||
385 | if (r) | ||
386 | return r; | ||
387 | |||
388 | for (i = 0; i < dev->entries_nr; i++) { | ||
389 | r = request_irq(dev->host_msix_entries[i].vector, | ||
390 | kvm_assigned_dev_intr, 0, | ||
391 | "kvm_assigned_msix_device", | ||
392 | (void *)dev); | ||
393 | /* FIXME: free requested_irq's on failure */ | ||
394 | if (r) | ||
395 | return r; | ||
396 | } | ||
397 | |||
398 | return 0; | ||
399 | } | ||
400 | |||
401 | #endif | ||
402 | |||
403 | static int assigned_device_enable_guest_intx(struct kvm *kvm, | ||
404 | struct kvm_assigned_dev_kernel *dev, | ||
405 | struct kvm_assigned_irq *irq) | ||
406 | { | ||
407 | dev->guest_irq = irq->guest_irq; | ||
408 | dev->ack_notifier.gsi = irq->guest_irq; | ||
409 | return 0; | ||
410 | } | ||
411 | |||
412 | #ifdef __KVM_HAVE_MSI | ||
413 | static int assigned_device_enable_guest_msi(struct kvm *kvm, | ||
414 | struct kvm_assigned_dev_kernel *dev, | ||
415 | struct kvm_assigned_irq *irq) | ||
416 | { | ||
417 | dev->guest_irq = irq->guest_irq; | ||
418 | dev->ack_notifier.gsi = -1; | ||
419 | dev->host_irq_disabled = false; | ||
420 | return 0; | ||
421 | } | ||
422 | #endif | ||
423 | #ifdef __KVM_HAVE_MSIX | ||
424 | static int assigned_device_enable_guest_msix(struct kvm *kvm, | ||
425 | struct kvm_assigned_dev_kernel *dev, | ||
426 | struct kvm_assigned_irq *irq) | ||
427 | { | ||
428 | dev->guest_irq = irq->guest_irq; | ||
429 | dev->ack_notifier.gsi = -1; | ||
430 | dev->host_irq_disabled = false; | ||
431 | return 0; | ||
432 | } | ||
433 | #endif | ||
434 | |||
435 | static int assign_host_irq(struct kvm *kvm, | ||
436 | struct kvm_assigned_dev_kernel *dev, | ||
437 | __u32 host_irq_type) | ||
438 | { | ||
439 | int r = -EEXIST; | ||
440 | |||
441 | if (dev->irq_requested_type & KVM_DEV_IRQ_HOST_MASK) | ||
442 | return r; | ||
443 | |||
444 | switch (host_irq_type) { | ||
445 | case KVM_DEV_IRQ_HOST_INTX: | ||
446 | r = assigned_device_enable_host_intx(kvm, dev); | ||
447 | break; | ||
448 | #ifdef __KVM_HAVE_MSI | ||
449 | case KVM_DEV_IRQ_HOST_MSI: | ||
450 | r = assigned_device_enable_host_msi(kvm, dev); | ||
451 | break; | ||
452 | #endif | ||
453 | #ifdef __KVM_HAVE_MSIX | ||
454 | case KVM_DEV_IRQ_HOST_MSIX: | ||
455 | r = assigned_device_enable_host_msix(kvm, dev); | ||
456 | break; | ||
457 | #endif | ||
458 | default: | ||
459 | r = -EINVAL; | ||
460 | } | ||
461 | |||
462 | if (!r) | ||
463 | dev->irq_requested_type |= host_irq_type; | ||
464 | |||
465 | return r; | ||
466 | } | ||
467 | |||
468 | static int assign_guest_irq(struct kvm *kvm, | ||
469 | struct kvm_assigned_dev_kernel *dev, | ||
470 | struct kvm_assigned_irq *irq, | ||
471 | unsigned long guest_irq_type) | ||
472 | { | ||
473 | int id; | ||
474 | int r = -EEXIST; | ||
475 | |||
476 | if (dev->irq_requested_type & KVM_DEV_IRQ_GUEST_MASK) | ||
477 | return r; | ||
478 | |||
479 | id = kvm_request_irq_source_id(kvm); | ||
480 | if (id < 0) | ||
481 | return id; | ||
482 | |||
483 | dev->irq_source_id = id; | ||
484 | |||
485 | switch (guest_irq_type) { | ||
486 | case KVM_DEV_IRQ_GUEST_INTX: | ||
487 | r = assigned_device_enable_guest_intx(kvm, dev, irq); | ||
488 | break; | ||
489 | #ifdef __KVM_HAVE_MSI | ||
490 | case KVM_DEV_IRQ_GUEST_MSI: | ||
491 | r = assigned_device_enable_guest_msi(kvm, dev, irq); | ||
492 | break; | ||
493 | #endif | ||
494 | #ifdef __KVM_HAVE_MSIX | ||
495 | case KVM_DEV_IRQ_GUEST_MSIX: | ||
496 | r = assigned_device_enable_guest_msix(kvm, dev, irq); | ||
497 | break; | ||
498 | #endif | ||
499 | default: | ||
500 | r = -EINVAL; | ||
501 | } | ||
502 | |||
503 | if (!r) { | ||
504 | dev->irq_requested_type |= guest_irq_type; | ||
505 | kvm_register_irq_ack_notifier(kvm, &dev->ack_notifier); | ||
506 | } else | ||
507 | kvm_free_irq_source_id(kvm, dev->irq_source_id); | ||
508 | |||
509 | return r; | ||
510 | } | ||
511 | |||
512 | /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */ | ||
513 | static int kvm_vm_ioctl_assign_irq(struct kvm *kvm, | ||
514 | struct kvm_assigned_irq *assigned_irq) | ||
515 | { | ||
516 | int r = -EINVAL; | ||
517 | struct kvm_assigned_dev_kernel *match; | ||
518 | unsigned long host_irq_type, guest_irq_type; | ||
519 | |||
520 | if (!capable(CAP_SYS_RAWIO)) | ||
521 | return -EPERM; | ||
522 | |||
523 | if (!irqchip_in_kernel(kvm)) | ||
524 | return r; | ||
525 | |||
526 | mutex_lock(&kvm->lock); | ||
527 | r = -ENODEV; | ||
528 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
529 | assigned_irq->assigned_dev_id); | ||
530 | if (!match) | ||
531 | goto out; | ||
532 | |||
533 | host_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_HOST_MASK); | ||
534 | guest_irq_type = (assigned_irq->flags & KVM_DEV_IRQ_GUEST_MASK); | ||
535 | |||
536 | r = -EINVAL; | ||
537 | /* can only assign one type at a time */ | ||
538 | if (hweight_long(host_irq_type) > 1) | ||
539 | goto out; | ||
540 | if (hweight_long(guest_irq_type) > 1) | ||
541 | goto out; | ||
542 | if (host_irq_type == 0 && guest_irq_type == 0) | ||
543 | goto out; | ||
544 | |||
545 | r = 0; | ||
546 | if (host_irq_type) | ||
547 | r = assign_host_irq(kvm, match, host_irq_type); | ||
548 | if (r) | ||
549 | goto out; | ||
550 | |||
551 | if (guest_irq_type) | ||
552 | r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type); | ||
553 | out: | ||
554 | mutex_unlock(&kvm->lock); | ||
555 | return r; | ||
556 | } | ||
557 | |||
558 | static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm, | ||
559 | struct kvm_assigned_irq | ||
560 | *assigned_irq) | ||
561 | { | ||
562 | int r = -ENODEV; | ||
563 | struct kvm_assigned_dev_kernel *match; | ||
564 | |||
565 | mutex_lock(&kvm->lock); | ||
566 | |||
567 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
568 | assigned_irq->assigned_dev_id); | ||
569 | if (!match) | ||
570 | goto out; | ||
571 | |||
572 | r = kvm_deassign_irq(kvm, match, assigned_irq->flags); | ||
573 | out: | ||
574 | mutex_unlock(&kvm->lock); | ||
575 | return r; | ||
576 | } | ||
577 | |||
578 | static int kvm_vm_ioctl_assign_device(struct kvm *kvm, | ||
579 | struct kvm_assigned_pci_dev *assigned_dev) | ||
580 | { | ||
581 | int r = 0; | ||
582 | struct kvm_assigned_dev_kernel *match; | ||
583 | struct pci_dev *dev; | ||
584 | |||
585 | down_read(&kvm->slots_lock); | ||
586 | mutex_lock(&kvm->lock); | ||
587 | |||
588 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
589 | assigned_dev->assigned_dev_id); | ||
590 | if (match) { | ||
591 | /* device already assigned */ | ||
592 | r = -EEXIST; | ||
593 | goto out; | ||
594 | } | ||
595 | |||
596 | match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL); | ||
597 | if (match == NULL) { | ||
598 | printk(KERN_INFO "%s: Couldn't allocate memory\n", | ||
599 | __func__); | ||
600 | r = -ENOMEM; | ||
601 | goto out; | ||
602 | } | ||
603 | dev = pci_get_bus_and_slot(assigned_dev->busnr, | ||
604 | assigned_dev->devfn); | ||
605 | if (!dev) { | ||
606 | printk(KERN_INFO "%s: host device not found\n", __func__); | ||
607 | r = -EINVAL; | ||
608 | goto out_free; | ||
609 | } | ||
610 | if (pci_enable_device(dev)) { | ||
611 | printk(KERN_INFO "%s: Could not enable PCI device\n", __func__); | ||
612 | r = -EBUSY; | ||
613 | goto out_put; | ||
614 | } | ||
615 | r = pci_request_regions(dev, "kvm_assigned_device"); | ||
616 | if (r) { | ||
617 | printk(KERN_INFO "%s: Could not get access to device regions\n", | ||
618 | __func__); | ||
619 | goto out_disable; | ||
620 | } | ||
621 | |||
622 | pci_reset_function(dev); | ||
623 | |||
624 | match->assigned_dev_id = assigned_dev->assigned_dev_id; | ||
625 | match->host_busnr = assigned_dev->busnr; | ||
626 | match->host_devfn = assigned_dev->devfn; | ||
627 | match->flags = assigned_dev->flags; | ||
628 | match->dev = dev; | ||
629 | spin_lock_init(&match->assigned_dev_lock); | ||
630 | match->irq_source_id = -1; | ||
631 | match->kvm = kvm; | ||
632 | match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq; | ||
633 | INIT_WORK(&match->interrupt_work, | ||
634 | kvm_assigned_dev_interrupt_work_handler); | ||
635 | |||
636 | list_add(&match->list, &kvm->arch.assigned_dev_head); | ||
637 | |||
638 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { | ||
639 | if (!kvm->arch.iommu_domain) { | ||
640 | r = kvm_iommu_map_guest(kvm); | ||
641 | if (r) | ||
642 | goto out_list_del; | ||
643 | } | ||
644 | r = kvm_assign_device(kvm, match); | ||
645 | if (r) | ||
646 | goto out_list_del; | ||
647 | } | ||
648 | |||
649 | out: | ||
650 | mutex_unlock(&kvm->lock); | ||
651 | up_read(&kvm->slots_lock); | ||
652 | return r; | ||
653 | out_list_del: | ||
654 | list_del(&match->list); | ||
655 | pci_release_regions(dev); | ||
656 | out_disable: | ||
657 | pci_disable_device(dev); | ||
658 | out_put: | ||
659 | pci_dev_put(dev); | ||
660 | out_free: | ||
661 | kfree(match); | ||
662 | mutex_unlock(&kvm->lock); | ||
663 | up_read(&kvm->slots_lock); | ||
664 | return r; | ||
665 | } | ||
666 | #endif | ||
667 | |||
668 | #ifdef KVM_CAP_DEVICE_DEASSIGNMENT | ||
669 | static int kvm_vm_ioctl_deassign_device(struct kvm *kvm, | ||
670 | struct kvm_assigned_pci_dev *assigned_dev) | ||
671 | { | ||
672 | int r = 0; | ||
673 | struct kvm_assigned_dev_kernel *match; | ||
674 | |||
675 | mutex_lock(&kvm->lock); | ||
676 | |||
677 | match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
678 | assigned_dev->assigned_dev_id); | ||
679 | if (!match) { | ||
680 | printk(KERN_INFO "%s: device hasn't been assigned before, " | ||
681 | "so cannot be deassigned\n", __func__); | ||
682 | r = -EINVAL; | ||
683 | goto out; | ||
684 | } | ||
685 | |||
686 | if (match->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) | ||
687 | kvm_deassign_device(kvm, match); | ||
688 | |||
689 | kvm_free_assigned_device(kvm, match); | ||
690 | |||
691 | out: | ||
692 | mutex_unlock(&kvm->lock); | ||
693 | return r; | ||
694 | } | ||
695 | #endif | ||
696 | |||
697 | inline int kvm_is_mmio_pfn(pfn_t pfn) | 92 | inline int kvm_is_mmio_pfn(pfn_t pfn) |
698 | { | 93 | { |
699 | if (pfn_valid(pfn)) { | 94 | if (pfn_valid(pfn)) { |
@@ -949,6 +344,7 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { | |||
949 | 344 | ||
950 | static struct kvm *kvm_create_vm(void) | 345 | static struct kvm *kvm_create_vm(void) |
951 | { | 346 | { |
347 | int r = 0; | ||
952 | struct kvm *kvm = kvm_arch_create_vm(); | 348 | struct kvm *kvm = kvm_arch_create_vm(); |
953 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | 349 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
954 | struct page *page; | 350 | struct page *page; |
@@ -956,16 +352,21 @@ static struct kvm *kvm_create_vm(void) | |||
956 | 352 | ||
957 | if (IS_ERR(kvm)) | 353 | if (IS_ERR(kvm)) |
958 | goto out; | 354 | goto out; |
355 | |||
356 | r = hardware_enable_all(); | ||
357 | if (r) | ||
358 | goto out_err_nodisable; | ||
359 | |||
959 | #ifdef CONFIG_HAVE_KVM_IRQCHIP | 360 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
960 | INIT_LIST_HEAD(&kvm->irq_routing); | ||
961 | INIT_HLIST_HEAD(&kvm->mask_notifier_list); | 361 | INIT_HLIST_HEAD(&kvm->mask_notifier_list); |
362 | INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); | ||
962 | #endif | 363 | #endif |
963 | 364 | ||
964 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | 365 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
965 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | 366 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
966 | if (!page) { | 367 | if (!page) { |
967 | kfree(kvm); | 368 | r = -ENOMEM; |
968 | return ERR_PTR(-ENOMEM); | 369 | goto out_err; |
969 | } | 370 | } |
970 | kvm->coalesced_mmio_ring = | 371 | kvm->coalesced_mmio_ring = |
971 | (struct kvm_coalesced_mmio_ring *)page_address(page); | 372 | (struct kvm_coalesced_mmio_ring *)page_address(page); |
@@ -973,15 +374,13 @@ static struct kvm *kvm_create_vm(void) | |||
973 | 374 | ||
974 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) | 375 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
975 | { | 376 | { |
976 | int err; | ||
977 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; | 377 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; |
978 | err = mmu_notifier_register(&kvm->mmu_notifier, current->mm); | 378 | r = mmu_notifier_register(&kvm->mmu_notifier, current->mm); |
979 | if (err) { | 379 | if (r) { |
980 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | 380 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
981 | put_page(page); | 381 | put_page(page); |
982 | #endif | 382 | #endif |
983 | kfree(kvm); | 383 | goto out_err; |
984 | return ERR_PTR(err); | ||
985 | } | 384 | } |
986 | } | 385 | } |
987 | #endif | 386 | #endif |
@@ -1005,6 +404,12 @@ static struct kvm *kvm_create_vm(void) | |||
1005 | #endif | 404 | #endif |
1006 | out: | 405 | out: |
1007 | return kvm; | 406 | return kvm; |
407 | |||
408 | out_err: | ||
409 | hardware_disable_all(); | ||
410 | out_err_nodisable: | ||
411 | kfree(kvm); | ||
412 | return ERR_PTR(r); | ||
1008 | } | 413 | } |
1009 | 414 | ||
1010 | /* | 415 | /* |
@@ -1063,6 +468,7 @@ static void kvm_destroy_vm(struct kvm *kvm) | |||
1063 | kvm_arch_flush_shadow(kvm); | 468 | kvm_arch_flush_shadow(kvm); |
1064 | #endif | 469 | #endif |
1065 | kvm_arch_destroy_vm(kvm); | 470 | kvm_arch_destroy_vm(kvm); |
471 | hardware_disable_all(); | ||
1066 | mmdrop(mm); | 472 | mmdrop(mm); |
1067 | } | 473 | } |
1068 | 474 | ||
@@ -1689,9 +1095,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) | |||
1689 | if (signal_pending(current)) | 1095 | if (signal_pending(current)) |
1690 | break; | 1096 | break; |
1691 | 1097 | ||
1692 | vcpu_put(vcpu); | ||
1693 | schedule(); | 1098 | schedule(); |
1694 | vcpu_load(vcpu); | ||
1695 | } | 1099 | } |
1696 | 1100 | ||
1697 | finish_wait(&vcpu->wq, &wait); | 1101 | finish_wait(&vcpu->wq, &wait); |
@@ -1705,6 +1109,21 @@ void kvm_resched(struct kvm_vcpu *vcpu) | |||
1705 | } | 1109 | } |
1706 | EXPORT_SYMBOL_GPL(kvm_resched); | 1110 | EXPORT_SYMBOL_GPL(kvm_resched); |
1707 | 1111 | ||
1112 | void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu) | ||
1113 | { | ||
1114 | ktime_t expires; | ||
1115 | DEFINE_WAIT(wait); | ||
1116 | |||
1117 | prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); | ||
1118 | |||
1119 | /* Sleep for 100 us, and hope lock-holder got scheduled */ | ||
1120 | expires = ktime_add_ns(ktime_get(), 100000UL); | ||
1121 | schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); | ||
1122 | |||
1123 | finish_wait(&vcpu->wq, &wait); | ||
1124 | } | ||
1125 | EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin); | ||
1126 | |||
1708 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | 1127 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1709 | { | 1128 | { |
1710 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | 1129 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; |
@@ -1828,88 +1247,6 @@ static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) | |||
1828 | return 0; | 1247 | return 0; |
1829 | } | 1248 | } |
1830 | 1249 | ||
1831 | #ifdef __KVM_HAVE_MSIX | ||
1832 | static int kvm_vm_ioctl_set_msix_nr(struct kvm *kvm, | ||
1833 | struct kvm_assigned_msix_nr *entry_nr) | ||
1834 | { | ||
1835 | int r = 0; | ||
1836 | struct kvm_assigned_dev_kernel *adev; | ||
1837 | |||
1838 | mutex_lock(&kvm->lock); | ||
1839 | |||
1840 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
1841 | entry_nr->assigned_dev_id); | ||
1842 | if (!adev) { | ||
1843 | r = -EINVAL; | ||
1844 | goto msix_nr_out; | ||
1845 | } | ||
1846 | |||
1847 | if (adev->entries_nr == 0) { | ||
1848 | adev->entries_nr = entry_nr->entry_nr; | ||
1849 | if (adev->entries_nr == 0 || | ||
1850 | adev->entries_nr >= KVM_MAX_MSIX_PER_DEV) { | ||
1851 | r = -EINVAL; | ||
1852 | goto msix_nr_out; | ||
1853 | } | ||
1854 | |||
1855 | adev->host_msix_entries = kzalloc(sizeof(struct msix_entry) * | ||
1856 | entry_nr->entry_nr, | ||
1857 | GFP_KERNEL); | ||
1858 | if (!adev->host_msix_entries) { | ||
1859 | r = -ENOMEM; | ||
1860 | goto msix_nr_out; | ||
1861 | } | ||
1862 | adev->guest_msix_entries = kzalloc( | ||
1863 | sizeof(struct kvm_guest_msix_entry) * | ||
1864 | entry_nr->entry_nr, GFP_KERNEL); | ||
1865 | if (!adev->guest_msix_entries) { | ||
1866 | kfree(adev->host_msix_entries); | ||
1867 | r = -ENOMEM; | ||
1868 | goto msix_nr_out; | ||
1869 | } | ||
1870 | } else /* Not allowed set MSI-X number twice */ | ||
1871 | r = -EINVAL; | ||
1872 | msix_nr_out: | ||
1873 | mutex_unlock(&kvm->lock); | ||
1874 | return r; | ||
1875 | } | ||
1876 | |||
1877 | static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, | ||
1878 | struct kvm_assigned_msix_entry *entry) | ||
1879 | { | ||
1880 | int r = 0, i; | ||
1881 | struct kvm_assigned_dev_kernel *adev; | ||
1882 | |||
1883 | mutex_lock(&kvm->lock); | ||
1884 | |||
1885 | adev = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head, | ||
1886 | entry->assigned_dev_id); | ||
1887 | |||
1888 | if (!adev) { | ||
1889 | r = -EINVAL; | ||
1890 | goto msix_entry_out; | ||
1891 | } | ||
1892 | |||
1893 | for (i = 0; i < adev->entries_nr; i++) | ||
1894 | if (adev->guest_msix_entries[i].vector == 0 || | ||
1895 | adev->guest_msix_entries[i].entry == entry->entry) { | ||
1896 | adev->guest_msix_entries[i].entry = entry->entry; | ||
1897 | adev->guest_msix_entries[i].vector = entry->gsi; | ||
1898 | adev->host_msix_entries[i].entry = entry->entry; | ||
1899 | break; | ||
1900 | } | ||
1901 | if (i == adev->entries_nr) { | ||
1902 | r = -ENOSPC; | ||
1903 | goto msix_entry_out; | ||
1904 | } | ||
1905 | |||
1906 | msix_entry_out: | ||
1907 | mutex_unlock(&kvm->lock); | ||
1908 | |||
1909 | return r; | ||
1910 | } | ||
1911 | #endif | ||
1912 | |||
1913 | static long kvm_vcpu_ioctl(struct file *filp, | 1250 | static long kvm_vcpu_ioctl(struct file *filp, |
1914 | unsigned int ioctl, unsigned long arg) | 1251 | unsigned int ioctl, unsigned long arg) |
1915 | { | 1252 | { |
@@ -2168,112 +1505,6 @@ static long kvm_vm_ioctl(struct file *filp, | |||
2168 | break; | 1505 | break; |
2169 | } | 1506 | } |
2170 | #endif | 1507 | #endif |
2171 | #ifdef KVM_CAP_DEVICE_ASSIGNMENT | ||
2172 | case KVM_ASSIGN_PCI_DEVICE: { | ||
2173 | struct kvm_assigned_pci_dev assigned_dev; | ||
2174 | |||
2175 | r = -EFAULT; | ||
2176 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) | ||
2177 | goto out; | ||
2178 | r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev); | ||
2179 | if (r) | ||
2180 | goto out; | ||
2181 | break; | ||
2182 | } | ||
2183 | case KVM_ASSIGN_IRQ: { | ||
2184 | r = -EOPNOTSUPP; | ||
2185 | break; | ||
2186 | } | ||
2187 | #ifdef KVM_CAP_ASSIGN_DEV_IRQ | ||
2188 | case KVM_ASSIGN_DEV_IRQ: { | ||
2189 | struct kvm_assigned_irq assigned_irq; | ||
2190 | |||
2191 | r = -EFAULT; | ||
2192 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) | ||
2193 | goto out; | ||
2194 | r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq); | ||
2195 | if (r) | ||
2196 | goto out; | ||
2197 | break; | ||
2198 | } | ||
2199 | case KVM_DEASSIGN_DEV_IRQ: { | ||
2200 | struct kvm_assigned_irq assigned_irq; | ||
2201 | |||
2202 | r = -EFAULT; | ||
2203 | if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq)) | ||
2204 | goto out; | ||
2205 | r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq); | ||
2206 | if (r) | ||
2207 | goto out; | ||
2208 | break; | ||
2209 | } | ||
2210 | #endif | ||
2211 | #endif | ||
2212 | #ifdef KVM_CAP_DEVICE_DEASSIGNMENT | ||
2213 | case KVM_DEASSIGN_PCI_DEVICE: { | ||
2214 | struct kvm_assigned_pci_dev assigned_dev; | ||
2215 | |||
2216 | r = -EFAULT; | ||
2217 | if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev)) | ||
2218 | goto out; | ||
2219 | r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev); | ||
2220 | if (r) | ||
2221 | goto out; | ||
2222 | break; | ||
2223 | } | ||
2224 | #endif | ||
2225 | #ifdef KVM_CAP_IRQ_ROUTING | ||
2226 | case KVM_SET_GSI_ROUTING: { | ||
2227 | struct kvm_irq_routing routing; | ||
2228 | struct kvm_irq_routing __user *urouting; | ||
2229 | struct kvm_irq_routing_entry *entries; | ||
2230 | |||
2231 | r = -EFAULT; | ||
2232 | if (copy_from_user(&routing, argp, sizeof(routing))) | ||
2233 | goto out; | ||
2234 | r = -EINVAL; | ||
2235 | if (routing.nr >= KVM_MAX_IRQ_ROUTES) | ||
2236 | goto out; | ||
2237 | if (routing.flags) | ||
2238 | goto out; | ||
2239 | r = -ENOMEM; | ||
2240 | entries = vmalloc(routing.nr * sizeof(*entries)); | ||
2241 | if (!entries) | ||
2242 | goto out; | ||
2243 | r = -EFAULT; | ||
2244 | urouting = argp; | ||
2245 | if (copy_from_user(entries, urouting->entries, | ||
2246 | routing.nr * sizeof(*entries))) | ||
2247 | goto out_free_irq_routing; | ||
2248 | r = kvm_set_irq_routing(kvm, entries, routing.nr, | ||
2249 | routing.flags); | ||
2250 | out_free_irq_routing: | ||
2251 | vfree(entries); | ||
2252 | break; | ||
2253 | } | ||
2254 | #endif /* KVM_CAP_IRQ_ROUTING */ | ||
2255 | #ifdef __KVM_HAVE_MSIX | ||
2256 | case KVM_ASSIGN_SET_MSIX_NR: { | ||
2257 | struct kvm_assigned_msix_nr entry_nr; | ||
2258 | r = -EFAULT; | ||
2259 | if (copy_from_user(&entry_nr, argp, sizeof entry_nr)) | ||
2260 | goto out; | ||
2261 | r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr); | ||
2262 | if (r) | ||
2263 | goto out; | ||
2264 | break; | ||
2265 | } | ||
2266 | case KVM_ASSIGN_SET_MSIX_ENTRY: { | ||
2267 | struct kvm_assigned_msix_entry entry; | ||
2268 | r = -EFAULT; | ||
2269 | if (copy_from_user(&entry, argp, sizeof entry)) | ||
2270 | goto out; | ||
2271 | r = kvm_vm_ioctl_set_msix_entry(kvm, &entry); | ||
2272 | if (r) | ||
2273 | goto out; | ||
2274 | break; | ||
2275 | } | ||
2276 | #endif | ||
2277 | case KVM_IRQFD: { | 1508 | case KVM_IRQFD: { |
2278 | struct kvm_irqfd data; | 1509 | struct kvm_irqfd data; |
2279 | 1510 | ||
@@ -2305,11 +1536,59 @@ static long kvm_vm_ioctl(struct file *filp, | |||
2305 | #endif | 1536 | #endif |
2306 | default: | 1537 | default: |
2307 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); | 1538 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
1539 | if (r == -ENOTTY) | ||
1540 | r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg); | ||
2308 | } | 1541 | } |
2309 | out: | 1542 | out: |
2310 | return r; | 1543 | return r; |
2311 | } | 1544 | } |
2312 | 1545 | ||
1546 | #ifdef CONFIG_COMPAT | ||
1547 | struct compat_kvm_dirty_log { | ||
1548 | __u32 slot; | ||
1549 | __u32 padding1; | ||
1550 | union { | ||
1551 | compat_uptr_t dirty_bitmap; /* one bit per page */ | ||
1552 | __u64 padding2; | ||
1553 | }; | ||
1554 | }; | ||
1555 | |||
1556 | static long kvm_vm_compat_ioctl(struct file *filp, | ||
1557 | unsigned int ioctl, unsigned long arg) | ||
1558 | { | ||
1559 | struct kvm *kvm = filp->private_data; | ||
1560 | int r; | ||
1561 | |||
1562 | if (kvm->mm != current->mm) | ||
1563 | return -EIO; | ||
1564 | switch (ioctl) { | ||
1565 | case KVM_GET_DIRTY_LOG: { | ||
1566 | struct compat_kvm_dirty_log compat_log; | ||
1567 | struct kvm_dirty_log log; | ||
1568 | |||
1569 | r = -EFAULT; | ||
1570 | if (copy_from_user(&compat_log, (void __user *)arg, | ||
1571 | sizeof(compat_log))) | ||
1572 | goto out; | ||
1573 | log.slot = compat_log.slot; | ||
1574 | log.padding1 = compat_log.padding1; | ||
1575 | log.padding2 = compat_log.padding2; | ||
1576 | log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap); | ||
1577 | |||
1578 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); | ||
1579 | if (r) | ||
1580 | goto out; | ||
1581 | break; | ||
1582 | } | ||
1583 | default: | ||
1584 | r = kvm_vm_ioctl(filp, ioctl, arg); | ||
1585 | } | ||
1586 | |||
1587 | out: | ||
1588 | return r; | ||
1589 | } | ||
1590 | #endif | ||
1591 | |||
2313 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | 1592 | static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
2314 | { | 1593 | { |
2315 | struct page *page[1]; | 1594 | struct page *page[1]; |
@@ -2344,7 +1623,9 @@ static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma) | |||
2344 | static struct file_operations kvm_vm_fops = { | 1623 | static struct file_operations kvm_vm_fops = { |
2345 | .release = kvm_vm_release, | 1624 | .release = kvm_vm_release, |
2346 | .unlocked_ioctl = kvm_vm_ioctl, | 1625 | .unlocked_ioctl = kvm_vm_ioctl, |
2347 | .compat_ioctl = kvm_vm_ioctl, | 1626 | #ifdef CONFIG_COMPAT |
1627 | .compat_ioctl = kvm_vm_compat_ioctl, | ||
1628 | #endif | ||
2348 | .mmap = kvm_vm_mmap, | 1629 | .mmap = kvm_vm_mmap, |
2349 | }; | 1630 | }; |
2350 | 1631 | ||
@@ -2372,6 +1653,7 @@ static long kvm_dev_ioctl_check_extension_generic(long arg) | |||
2372 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE | 1653 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
2373 | case KVM_CAP_SET_BOOT_CPU_ID: | 1654 | case KVM_CAP_SET_BOOT_CPU_ID: |
2374 | #endif | 1655 | #endif |
1656 | case KVM_CAP_INTERNAL_ERROR_DATA: | ||
2375 | return 1; | 1657 | return 1; |
2376 | #ifdef CONFIG_HAVE_KVM_IRQCHIP | 1658 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
2377 | case KVM_CAP_IRQ_ROUTING: | 1659 | case KVM_CAP_IRQ_ROUTING: |
@@ -2442,11 +1724,21 @@ static struct miscdevice kvm_dev = { | |||
2442 | static void hardware_enable(void *junk) | 1724 | static void hardware_enable(void *junk) |
2443 | { | 1725 | { |
2444 | int cpu = raw_smp_processor_id(); | 1726 | int cpu = raw_smp_processor_id(); |
1727 | int r; | ||
2445 | 1728 | ||
2446 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) | 1729 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
2447 | return; | 1730 | return; |
1731 | |||
2448 | cpumask_set_cpu(cpu, cpus_hardware_enabled); | 1732 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
2449 | kvm_arch_hardware_enable(NULL); | 1733 | |
1734 | r = kvm_arch_hardware_enable(NULL); | ||
1735 | |||
1736 | if (r) { | ||
1737 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); | ||
1738 | atomic_inc(&hardware_enable_failed); | ||
1739 | printk(KERN_INFO "kvm: enabling virtualization on " | ||
1740 | "CPU%d failed\n", cpu); | ||
1741 | } | ||
2450 | } | 1742 | } |
2451 | 1743 | ||
2452 | static void hardware_disable(void *junk) | 1744 | static void hardware_disable(void *junk) |
@@ -2459,11 +1751,52 @@ static void hardware_disable(void *junk) | |||
2459 | kvm_arch_hardware_disable(NULL); | 1751 | kvm_arch_hardware_disable(NULL); |
2460 | } | 1752 | } |
2461 | 1753 | ||
1754 | static void hardware_disable_all_nolock(void) | ||
1755 | { | ||
1756 | BUG_ON(!kvm_usage_count); | ||
1757 | |||
1758 | kvm_usage_count--; | ||
1759 | if (!kvm_usage_count) | ||
1760 | on_each_cpu(hardware_disable, NULL, 1); | ||
1761 | } | ||
1762 | |||
1763 | static void hardware_disable_all(void) | ||
1764 | { | ||
1765 | spin_lock(&kvm_lock); | ||
1766 | hardware_disable_all_nolock(); | ||
1767 | spin_unlock(&kvm_lock); | ||
1768 | } | ||
1769 | |||
1770 | static int hardware_enable_all(void) | ||
1771 | { | ||
1772 | int r = 0; | ||
1773 | |||
1774 | spin_lock(&kvm_lock); | ||
1775 | |||
1776 | kvm_usage_count++; | ||
1777 | if (kvm_usage_count == 1) { | ||
1778 | atomic_set(&hardware_enable_failed, 0); | ||
1779 | on_each_cpu(hardware_enable, NULL, 1); | ||
1780 | |||
1781 | if (atomic_read(&hardware_enable_failed)) { | ||
1782 | hardware_disable_all_nolock(); | ||
1783 | r = -EBUSY; | ||
1784 | } | ||
1785 | } | ||
1786 | |||
1787 | spin_unlock(&kvm_lock); | ||
1788 | |||
1789 | return r; | ||
1790 | } | ||
1791 | |||
2462 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, | 1792 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
2463 | void *v) | 1793 | void *v) |
2464 | { | 1794 | { |
2465 | int cpu = (long)v; | 1795 | int cpu = (long)v; |
2466 | 1796 | ||
1797 | if (!kvm_usage_count) | ||
1798 | return NOTIFY_OK; | ||
1799 | |||
2467 | val &= ~CPU_TASKS_FROZEN; | 1800 | val &= ~CPU_TASKS_FROZEN; |
2468 | switch (val) { | 1801 | switch (val) { |
2469 | case CPU_DYING: | 1802 | case CPU_DYING: |
@@ -2666,13 +1999,15 @@ static void kvm_exit_debug(void) | |||
2666 | 1999 | ||
2667 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) | 2000 | static int kvm_suspend(struct sys_device *dev, pm_message_t state) |
2668 | { | 2001 | { |
2669 | hardware_disable(NULL); | 2002 | if (kvm_usage_count) |
2003 | hardware_disable(NULL); | ||
2670 | return 0; | 2004 | return 0; |
2671 | } | 2005 | } |
2672 | 2006 | ||
2673 | static int kvm_resume(struct sys_device *dev) | 2007 | static int kvm_resume(struct sys_device *dev) |
2674 | { | 2008 | { |
2675 | hardware_enable(NULL); | 2009 | if (kvm_usage_count) |
2010 | hardware_enable(NULL); | ||
2676 | return 0; | 2011 | return 0; |
2677 | } | 2012 | } |
2678 | 2013 | ||
@@ -2747,7 +2082,6 @@ int kvm_init(void *opaque, unsigned int vcpu_size, | |||
2747 | goto out_free_1; | 2082 | goto out_free_1; |
2748 | } | 2083 | } |
2749 | 2084 | ||
2750 | on_each_cpu(hardware_enable, NULL, 1); | ||
2751 | r = register_cpu_notifier(&kvm_cpu_notifier); | 2085 | r = register_cpu_notifier(&kvm_cpu_notifier); |
2752 | if (r) | 2086 | if (r) |
2753 | goto out_free_2; | 2087 | goto out_free_2; |
@@ -2797,7 +2131,6 @@ out_free_3: | |||
2797 | unregister_reboot_notifier(&kvm_reboot_notifier); | 2131 | unregister_reboot_notifier(&kvm_reboot_notifier); |
2798 | unregister_cpu_notifier(&kvm_cpu_notifier); | 2132 | unregister_cpu_notifier(&kvm_cpu_notifier); |
2799 | out_free_2: | 2133 | out_free_2: |
2800 | on_each_cpu(hardware_disable, NULL, 1); | ||
2801 | out_free_1: | 2134 | out_free_1: |
2802 | kvm_arch_hardware_unsetup(); | 2135 | kvm_arch_hardware_unsetup(); |
2803 | out_free_0a: | 2136 | out_free_0a: |