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