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