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