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