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