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