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