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 | ||
af669ac6 | 19 | #include <kvm/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> | |
fb3600cc | 33 | #include <linux/syscore_ops.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> |
743eeb0b SL |
50 | #include <linux/sort.h> |
51 | #include <linux/bsearch.h> | |
6aa8b732 | 52 | |
e495606d | 53 | #include <asm/processor.h> |
e495606d | 54 | #include <asm/io.h> |
2ea75be3 | 55 | #include <asm/ioctl.h> |
e495606d | 56 | #include <asm/uaccess.h> |
3e021bf5 | 57 | #include <asm/pgtable.h> |
6aa8b732 | 58 | |
5f94c174 | 59 | #include "coalesced_mmio.h" |
af585b92 | 60 | #include "async_pf.h" |
3c3c29fd | 61 | #include "vfio.h" |
5f94c174 | 62 | |
229456fc MT |
63 | #define CREATE_TRACE_POINTS |
64 | #include <trace/events/kvm.h> | |
65 | ||
536a6f88 JF |
66 | /* Worst case buffer size needed for holding an integer. */ |
67 | #define ITOA_MAX_LEN 12 | |
68 | ||
6aa8b732 AK |
69 | MODULE_AUTHOR("Qumranet"); |
70 | MODULE_LICENSE("GPL"); | |
71 | ||
920552b2 DH |
72 | /* Architectures should define their poll value according to the halt latency */ |
73 | static unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT; | |
f7819512 PB |
74 | module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR); |
75 | ||
aca6ff29 WL |
76 | /* Default doubles per-vcpu halt_poll_ns. */ |
77 | static unsigned int halt_poll_ns_grow = 2; | |
6b6de68c | 78 | module_param(halt_poll_ns_grow, uint, S_IRUGO | S_IWUSR); |
aca6ff29 WL |
79 | |
80 | /* Default resets per-vcpu halt_poll_ns . */ | |
81 | static unsigned int halt_poll_ns_shrink; | |
6b6de68c | 82 | module_param(halt_poll_ns_shrink, uint, S_IRUGO | S_IWUSR); |
aca6ff29 | 83 | |
fa40a821 MT |
84 | /* |
85 | * Ordering of locks: | |
86 | * | |
b7d409de | 87 | * kvm->lock --> kvm->slots_lock --> kvm->irq_lock |
fa40a821 MT |
88 | */ |
89 | ||
2f303b74 | 90 | DEFINE_SPINLOCK(kvm_lock); |
4a937f96 | 91 | static DEFINE_RAW_SPINLOCK(kvm_count_lock); |
e9b11c17 | 92 | LIST_HEAD(vm_list); |
133de902 | 93 | |
7f59f492 | 94 | static cpumask_var_t cpus_hardware_enabled; |
f4fee932 | 95 | static int kvm_usage_count; |
10474ae8 | 96 | static atomic_t hardware_enable_failed; |
1b6c0168 | 97 | |
c16f862d RR |
98 | struct kmem_cache *kvm_vcpu_cache; |
99 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 100 | |
15ad7146 AK |
101 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
102 | ||
76f7c879 | 103 | struct dentry *kvm_debugfs_dir; |
e23a808b | 104 | EXPORT_SYMBOL_GPL(kvm_debugfs_dir); |
6aa8b732 | 105 | |
536a6f88 JF |
106 | static int kvm_debugfs_num_entries; |
107 | static const struct file_operations *stat_fops_per_vm[]; | |
108 | ||
bccf2150 AK |
109 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
110 | unsigned long arg); | |
de8e5d74 | 111 | #ifdef CONFIG_KVM_COMPAT |
1dda606c AG |
112 | static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl, |
113 | unsigned long arg); | |
114 | #endif | |
10474ae8 AG |
115 | static int hardware_enable_all(void); |
116 | static void hardware_disable_all(void); | |
bccf2150 | 117 | |
e93f8a0f | 118 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus); |
7940876e | 119 | |
ba049e93 | 120 | static void kvm_release_pfn_dirty(kvm_pfn_t pfn); |
bc009e43 | 121 | static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn); |
e93f8a0f | 122 | |
52480137 | 123 | __visible bool kvm_rebooting; |
b7c4145b | 124 | EXPORT_SYMBOL_GPL(kvm_rebooting); |
4ecac3fd | 125 | |
54dee993 MT |
126 | static bool largepages_enabled = true; |
127 | ||
ba049e93 | 128 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn) |
cbff90a7 | 129 | { |
11feeb49 | 130 | if (pfn_valid(pfn)) |
bf4bea8e | 131 | return PageReserved(pfn_to_page(pfn)); |
cbff90a7 BAY |
132 | |
133 | return true; | |
134 | } | |
135 | ||
bccf2150 AK |
136 | /* |
137 | * Switches to specified vcpu, until a matching vcpu_put() | |
138 | */ | |
9fc77441 | 139 | int vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 140 | { |
15ad7146 AK |
141 | int cpu; |
142 | ||
9fc77441 MT |
143 | if (mutex_lock_killable(&vcpu->mutex)) |
144 | return -EINTR; | |
15ad7146 AK |
145 | cpu = get_cpu(); |
146 | preempt_notifier_register(&vcpu->preempt_notifier); | |
313a3dc7 | 147 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 148 | put_cpu(); |
9fc77441 | 149 | return 0; |
6aa8b732 AK |
150 | } |
151 | ||
313a3dc7 | 152 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 153 | { |
15ad7146 | 154 | preempt_disable(); |
313a3dc7 | 155 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
156 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
157 | preempt_enable(); | |
6aa8b732 AK |
158 | mutex_unlock(&vcpu->mutex); |
159 | } | |
160 | ||
d9e368d6 AK |
161 | static void ack_flush(void *_completed) |
162 | { | |
d9e368d6 AK |
163 | } |
164 | ||
445b8236 | 165 | bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req) |
d9e368d6 | 166 | { |
597a5f55 | 167 | int i, cpu, me; |
6ef7a1bc RR |
168 | cpumask_var_t cpus; |
169 | bool called = true; | |
d9e368d6 | 170 | struct kvm_vcpu *vcpu; |
d9e368d6 | 171 | |
79f55997 | 172 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); |
6ef7a1bc | 173 | |
3cba4130 | 174 | me = get_cpu(); |
988a2cae | 175 | kvm_for_each_vcpu(i, vcpu, kvm) { |
3cba4130 | 176 | kvm_make_request(req, vcpu); |
d9e368d6 | 177 | cpu = vcpu->cpu; |
6b7e2d09 | 178 | |
a30a0509 LT |
179 | /* Set ->requests bit before we read ->mode. */ |
180 | smp_mb__after_atomic(); | |
6b7e2d09 XG |
181 | |
182 | if (cpus != NULL && cpu != -1 && cpu != me && | |
183 | kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE) | |
6ef7a1bc | 184 | cpumask_set_cpu(cpu, cpus); |
49846896 | 185 | } |
6ef7a1bc RR |
186 | if (unlikely(cpus == NULL)) |
187 | smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1); | |
188 | else if (!cpumask_empty(cpus)) | |
189 | smp_call_function_many(cpus, ack_flush, NULL, 1); | |
190 | else | |
191 | called = false; | |
3cba4130 | 192 | put_cpu(); |
6ef7a1bc | 193 | free_cpumask_var(cpus); |
49846896 | 194 | return called; |
d9e368d6 AK |
195 | } |
196 | ||
a6d51016 | 197 | #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL |
49846896 | 198 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
2e53d63a | 199 | { |
4ae3cb3a LT |
200 | /* |
201 | * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in | |
202 | * kvm_make_all_cpus_request. | |
203 | */ | |
204 | long dirty_count = smp_load_acquire(&kvm->tlbs_dirty); | |
205 | ||
206 | /* | |
207 | * We want to publish modifications to the page tables before reading | |
208 | * mode. Pairs with a memory barrier in arch-specific code. | |
209 | * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest | |
210 | * and smp_mb in walk_shadow_page_lockless_begin/end. | |
211 | * - powerpc: smp_mb in kvmppc_prepare_to_enter. | |
212 | * | |
213 | * There is already an smp_mb__after_atomic() before | |
214 | * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that | |
215 | * barrier here. | |
216 | */ | |
445b8236 | 217 | if (kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) |
49846896 | 218 | ++kvm->stat.remote_tlb_flush; |
a086f6a1 | 219 | cmpxchg(&kvm->tlbs_dirty, dirty_count, 0); |
2e53d63a | 220 | } |
2ba9f0d8 | 221 | EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs); |
a6d51016 | 222 | #endif |
2e53d63a | 223 | |
49846896 RR |
224 | void kvm_reload_remote_mmus(struct kvm *kvm) |
225 | { | |
445b8236 | 226 | kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD); |
49846896 | 227 | } |
2e53d63a | 228 | |
fb3f0f51 RR |
229 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
230 | { | |
231 | struct page *page; | |
232 | int r; | |
233 | ||
234 | mutex_init(&vcpu->mutex); | |
235 | vcpu->cpu = -1; | |
fb3f0f51 RR |
236 | vcpu->kvm = kvm; |
237 | vcpu->vcpu_id = id; | |
34bb10b7 | 238 | vcpu->pid = NULL; |
8577370f | 239 | init_swait_queue_head(&vcpu->wq); |
af585b92 | 240 | kvm_async_pf_vcpu_init(vcpu); |
fb3f0f51 | 241 | |
bf9f6ac8 FW |
242 | vcpu->pre_pcpu = -1; |
243 | INIT_LIST_HEAD(&vcpu->blocked_vcpu_list); | |
244 | ||
fb3f0f51 RR |
245 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
246 | if (!page) { | |
247 | r = -ENOMEM; | |
248 | goto fail; | |
249 | } | |
250 | vcpu->run = page_address(page); | |
251 | ||
4c088493 R |
252 | kvm_vcpu_set_in_spin_loop(vcpu, false); |
253 | kvm_vcpu_set_dy_eligible(vcpu, false); | |
3a08a8f9 | 254 | vcpu->preempted = false; |
4c088493 | 255 | |
e9b11c17 | 256 | r = kvm_arch_vcpu_init(vcpu); |
fb3f0f51 | 257 | if (r < 0) |
e9b11c17 | 258 | goto fail_free_run; |
fb3f0f51 RR |
259 | return 0; |
260 | ||
fb3f0f51 RR |
261 | fail_free_run: |
262 | free_page((unsigned long)vcpu->run); | |
263 | fail: | |
76fafa5e | 264 | return r; |
fb3f0f51 RR |
265 | } |
266 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
267 | ||
268 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
269 | { | |
34bb10b7 | 270 | put_pid(vcpu->pid); |
e9b11c17 | 271 | kvm_arch_vcpu_uninit(vcpu); |
fb3f0f51 RR |
272 | free_page((unsigned long)vcpu->run); |
273 | } | |
274 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
275 | ||
e930bffe AA |
276 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
277 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) | |
278 | { | |
279 | return container_of(mn, struct kvm, mmu_notifier); | |
280 | } | |
281 | ||
282 | static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, | |
283 | struct mm_struct *mm, | |
284 | unsigned long address) | |
285 | { | |
286 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 287 | int need_tlb_flush, idx; |
e930bffe AA |
288 | |
289 | /* | |
290 | * When ->invalidate_page runs, the linux pte has been zapped | |
291 | * already but the page is still allocated until | |
292 | * ->invalidate_page returns. So if we increase the sequence | |
293 | * here the kvm page fault will notice if the spte can't be | |
294 | * established because the page is going to be freed. If | |
295 | * instead the kvm page fault establishes the spte before | |
296 | * ->invalidate_page runs, kvm_unmap_hva will release it | |
297 | * before returning. | |
298 | * | |
299 | * The sequence increase only need to be seen at spin_unlock | |
300 | * time, and not at spin_lock time. | |
301 | * | |
302 | * Increasing the sequence after the spin_unlock would be | |
303 | * unsafe because the kvm page fault could then establish the | |
304 | * pte after kvm_unmap_hva returned, without noticing the page | |
305 | * is going to be freed. | |
306 | */ | |
bc6678a3 | 307 | idx = srcu_read_lock(&kvm->srcu); |
e930bffe | 308 | spin_lock(&kvm->mmu_lock); |
565f3be2 | 309 | |
e930bffe | 310 | kvm->mmu_notifier_seq++; |
a4ee1ca4 | 311 | need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty; |
e930bffe AA |
312 | /* we've to flush the tlb before the pages can be freed */ |
313 | if (need_tlb_flush) | |
314 | kvm_flush_remote_tlbs(kvm); | |
315 | ||
565f3be2 | 316 | spin_unlock(&kvm->mmu_lock); |
fe71557a TC |
317 | |
318 | kvm_arch_mmu_notifier_invalidate_page(kvm, address); | |
319 | ||
565f3be2 | 320 | srcu_read_unlock(&kvm->srcu, idx); |
e930bffe AA |
321 | } |
322 | ||
3da0dd43 IE |
323 | static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, |
324 | struct mm_struct *mm, | |
325 | unsigned long address, | |
326 | pte_t pte) | |
327 | { | |
328 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 329 | int idx; |
3da0dd43 | 330 | |
bc6678a3 | 331 | idx = srcu_read_lock(&kvm->srcu); |
3da0dd43 IE |
332 | spin_lock(&kvm->mmu_lock); |
333 | kvm->mmu_notifier_seq++; | |
334 | kvm_set_spte_hva(kvm, address, pte); | |
335 | spin_unlock(&kvm->mmu_lock); | |
bc6678a3 | 336 | srcu_read_unlock(&kvm->srcu, idx); |
3da0dd43 IE |
337 | } |
338 | ||
e930bffe AA |
339 | static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
340 | struct mm_struct *mm, | |
341 | unsigned long start, | |
342 | unsigned long end) | |
343 | { | |
344 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 345 | int need_tlb_flush = 0, idx; |
e930bffe | 346 | |
bc6678a3 | 347 | idx = srcu_read_lock(&kvm->srcu); |
e930bffe AA |
348 | spin_lock(&kvm->mmu_lock); |
349 | /* | |
350 | * The count increase must become visible at unlock time as no | |
351 | * spte can be established without taking the mmu_lock and | |
352 | * count is also read inside the mmu_lock critical section. | |
353 | */ | |
354 | kvm->mmu_notifier_count++; | |
b3ae2096 | 355 | need_tlb_flush = kvm_unmap_hva_range(kvm, start, end); |
a4ee1ca4 | 356 | need_tlb_flush |= kvm->tlbs_dirty; |
e930bffe AA |
357 | /* we've to flush the tlb before the pages can be freed */ |
358 | if (need_tlb_flush) | |
359 | kvm_flush_remote_tlbs(kvm); | |
565f3be2 TY |
360 | |
361 | spin_unlock(&kvm->mmu_lock); | |
362 | srcu_read_unlock(&kvm->srcu, idx); | |
e930bffe AA |
363 | } |
364 | ||
365 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, | |
366 | struct mm_struct *mm, | |
367 | unsigned long start, | |
368 | unsigned long end) | |
369 | { | |
370 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
371 | ||
372 | spin_lock(&kvm->mmu_lock); | |
373 | /* | |
374 | * This sequence increase will notify the kvm page fault that | |
375 | * the page that is going to be mapped in the spte could have | |
376 | * been freed. | |
377 | */ | |
378 | kvm->mmu_notifier_seq++; | |
a355aa54 | 379 | smp_wmb(); |
e930bffe AA |
380 | /* |
381 | * The above sequence increase must be visible before the | |
a355aa54 PM |
382 | * below count decrease, which is ensured by the smp_wmb above |
383 | * in conjunction with the smp_rmb in mmu_notifier_retry(). | |
e930bffe AA |
384 | */ |
385 | kvm->mmu_notifier_count--; | |
386 | spin_unlock(&kvm->mmu_lock); | |
387 | ||
388 | BUG_ON(kvm->mmu_notifier_count < 0); | |
389 | } | |
390 | ||
391 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, | |
392 | struct mm_struct *mm, | |
57128468 ALC |
393 | unsigned long start, |
394 | unsigned long end) | |
e930bffe AA |
395 | { |
396 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 397 | int young, idx; |
e930bffe | 398 | |
bc6678a3 | 399 | idx = srcu_read_lock(&kvm->srcu); |
e930bffe | 400 | spin_lock(&kvm->mmu_lock); |
e930bffe | 401 | |
57128468 | 402 | young = kvm_age_hva(kvm, start, end); |
e930bffe AA |
403 | if (young) |
404 | kvm_flush_remote_tlbs(kvm); | |
405 | ||
565f3be2 TY |
406 | spin_unlock(&kvm->mmu_lock); |
407 | srcu_read_unlock(&kvm->srcu, idx); | |
408 | ||
e930bffe AA |
409 | return young; |
410 | } | |
411 | ||
1d7715c6 VD |
412 | static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, |
413 | struct mm_struct *mm, | |
414 | unsigned long start, | |
415 | unsigned long end) | |
416 | { | |
417 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
418 | int young, idx; | |
419 | ||
420 | idx = srcu_read_lock(&kvm->srcu); | |
421 | spin_lock(&kvm->mmu_lock); | |
422 | /* | |
423 | * Even though we do not flush TLB, this will still adversely | |
424 | * affect performance on pre-Haswell Intel EPT, where there is | |
425 | * no EPT Access Bit to clear so that we have to tear down EPT | |
426 | * tables instead. If we find this unacceptable, we can always | |
427 | * add a parameter to kvm_age_hva so that it effectively doesn't | |
428 | * do anything on clear_young. | |
429 | * | |
430 | * Also note that currently we never issue secondary TLB flushes | |
431 | * from clear_young, leaving this job up to the regular system | |
432 | * cadence. If we find this inaccurate, we might come up with a | |
433 | * more sophisticated heuristic later. | |
434 | */ | |
435 | young = kvm_age_hva(kvm, start, end); | |
436 | spin_unlock(&kvm->mmu_lock); | |
437 | srcu_read_unlock(&kvm->srcu, idx); | |
438 | ||
439 | return young; | |
440 | } | |
441 | ||
8ee53820 AA |
442 | static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, |
443 | struct mm_struct *mm, | |
444 | unsigned long address) | |
445 | { | |
446 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
447 | int young, idx; | |
448 | ||
449 | idx = srcu_read_lock(&kvm->srcu); | |
450 | spin_lock(&kvm->mmu_lock); | |
451 | young = kvm_test_age_hva(kvm, address); | |
452 | spin_unlock(&kvm->mmu_lock); | |
453 | srcu_read_unlock(&kvm->srcu, idx); | |
454 | ||
455 | return young; | |
456 | } | |
457 | ||
85db06e5 MT |
458 | static void kvm_mmu_notifier_release(struct mmu_notifier *mn, |
459 | struct mm_struct *mm) | |
460 | { | |
461 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
eda2beda LJ |
462 | int idx; |
463 | ||
464 | idx = srcu_read_lock(&kvm->srcu); | |
2df72e9b | 465 | kvm_arch_flush_shadow_all(kvm); |
eda2beda | 466 | srcu_read_unlock(&kvm->srcu, idx); |
85db06e5 MT |
467 | } |
468 | ||
e930bffe AA |
469 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
470 | .invalidate_page = kvm_mmu_notifier_invalidate_page, | |
471 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, | |
472 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, | |
473 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, | |
1d7715c6 | 474 | .clear_young = kvm_mmu_notifier_clear_young, |
8ee53820 | 475 | .test_young = kvm_mmu_notifier_test_young, |
3da0dd43 | 476 | .change_pte = kvm_mmu_notifier_change_pte, |
85db06e5 | 477 | .release = kvm_mmu_notifier_release, |
e930bffe | 478 | }; |
4c07b0a4 AK |
479 | |
480 | static int kvm_init_mmu_notifier(struct kvm *kvm) | |
481 | { | |
482 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; | |
483 | return mmu_notifier_register(&kvm->mmu_notifier, current->mm); | |
484 | } | |
485 | ||
486 | #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ | |
487 | ||
488 | static int kvm_init_mmu_notifier(struct kvm *kvm) | |
489 | { | |
490 | return 0; | |
491 | } | |
492 | ||
e930bffe AA |
493 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
494 | ||
a47d2b07 | 495 | static struct kvm_memslots *kvm_alloc_memslots(void) |
bf3e05bc XG |
496 | { |
497 | int i; | |
a47d2b07 | 498 | struct kvm_memslots *slots; |
bf3e05bc | 499 | |
a47d2b07 PB |
500 | slots = kvm_kvzalloc(sizeof(struct kvm_memslots)); |
501 | if (!slots) | |
502 | return NULL; | |
503 | ||
504 | /* | |
505 | * Init kvm generation close to the maximum to easily test the | |
506 | * code of handling generation number wrap-around. | |
507 | */ | |
508 | slots->generation = -150; | |
bf3e05bc | 509 | for (i = 0; i < KVM_MEM_SLOTS_NUM; i++) |
f85e2cb5 | 510 | slots->id_to_index[i] = slots->memslots[i].id = i; |
a47d2b07 PB |
511 | |
512 | return slots; | |
513 | } | |
514 | ||
515 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) | |
516 | { | |
517 | if (!memslot->dirty_bitmap) | |
518 | return; | |
519 | ||
520 | kvfree(memslot->dirty_bitmap); | |
521 | memslot->dirty_bitmap = NULL; | |
522 | } | |
523 | ||
524 | /* | |
525 | * Free any memory in @free but not in @dont. | |
526 | */ | |
527 | static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, | |
528 | struct kvm_memory_slot *dont) | |
529 | { | |
530 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
531 | kvm_destroy_dirty_bitmap(free); | |
532 | ||
533 | kvm_arch_free_memslot(kvm, free, dont); | |
534 | ||
535 | free->npages = 0; | |
536 | } | |
537 | ||
538 | static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots) | |
539 | { | |
540 | struct kvm_memory_slot *memslot; | |
541 | ||
542 | if (!slots) | |
543 | return; | |
544 | ||
545 | kvm_for_each_memslot(memslot, slots) | |
546 | kvm_free_memslot(kvm, memslot, NULL); | |
547 | ||
548 | kvfree(slots); | |
bf3e05bc XG |
549 | } |
550 | ||
536a6f88 JF |
551 | static void kvm_destroy_vm_debugfs(struct kvm *kvm) |
552 | { | |
553 | int i; | |
554 | ||
555 | if (!kvm->debugfs_dentry) | |
556 | return; | |
557 | ||
558 | debugfs_remove_recursive(kvm->debugfs_dentry); | |
559 | ||
560 | for (i = 0; i < kvm_debugfs_num_entries; i++) | |
561 | kfree(kvm->debugfs_stat_data[i]); | |
562 | kfree(kvm->debugfs_stat_data); | |
563 | } | |
564 | ||
565 | static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) | |
566 | { | |
567 | char dir_name[ITOA_MAX_LEN * 2]; | |
568 | struct kvm_stat_data *stat_data; | |
569 | struct kvm_stats_debugfs_item *p; | |
570 | ||
571 | if (!debugfs_initialized()) | |
572 | return 0; | |
573 | ||
574 | snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd); | |
575 | kvm->debugfs_dentry = debugfs_create_dir(dir_name, | |
576 | kvm_debugfs_dir); | |
577 | if (!kvm->debugfs_dentry) | |
578 | return -ENOMEM; | |
579 | ||
580 | kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries, | |
581 | sizeof(*kvm->debugfs_stat_data), | |
582 | GFP_KERNEL); | |
583 | if (!kvm->debugfs_stat_data) | |
584 | return -ENOMEM; | |
585 | ||
586 | for (p = debugfs_entries; p->name; p++) { | |
587 | stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL); | |
588 | if (!stat_data) | |
589 | return -ENOMEM; | |
590 | ||
591 | stat_data->kvm = kvm; | |
592 | stat_data->offset = p->offset; | |
593 | kvm->debugfs_stat_data[p - debugfs_entries] = stat_data; | |
594 | if (!debugfs_create_file(p->name, 0444, | |
595 | kvm->debugfs_dentry, | |
596 | stat_data, | |
597 | stat_fops_per_vm[p->kind])) | |
598 | return -ENOMEM; | |
599 | } | |
600 | return 0; | |
601 | } | |
602 | ||
e08b9637 | 603 | static struct kvm *kvm_create_vm(unsigned long type) |
6aa8b732 | 604 | { |
d89f5eff JK |
605 | int r, i; |
606 | struct kvm *kvm = kvm_arch_alloc_vm(); | |
6aa8b732 | 607 | |
d89f5eff JK |
608 | if (!kvm) |
609 | return ERR_PTR(-ENOMEM); | |
610 | ||
e9ad4ec8 PB |
611 | spin_lock_init(&kvm->mmu_lock); |
612 | atomic_inc(¤t->mm->mm_count); | |
613 | kvm->mm = current->mm; | |
614 | kvm_eventfd_init(kvm); | |
615 | mutex_init(&kvm->lock); | |
616 | mutex_init(&kvm->irq_lock); | |
617 | mutex_init(&kvm->slots_lock); | |
618 | atomic_set(&kvm->users_count, 1); | |
619 | INIT_LIST_HEAD(&kvm->devices); | |
620 | ||
e08b9637 | 621 | r = kvm_arch_init_vm(kvm, type); |
d89f5eff | 622 | if (r) |
719d93cd | 623 | goto out_err_no_disable; |
10474ae8 AG |
624 | |
625 | r = hardware_enable_all(); | |
626 | if (r) | |
719d93cd | 627 | goto out_err_no_disable; |
10474ae8 | 628 | |
c77dcacb | 629 | #ifdef CONFIG_HAVE_KVM_IRQFD |
136bdfee | 630 | INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
75858a84 | 631 | #endif |
6aa8b732 | 632 | |
1e702d9a AW |
633 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); |
634 | ||
46a26bf5 | 635 | r = -ENOMEM; |
f481b069 PB |
636 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
637 | kvm->memslots[i] = kvm_alloc_memslots(); | |
638 | if (!kvm->memslots[i]) | |
639 | goto out_err_no_srcu; | |
640 | } | |
00f034a1 | 641 | |
bc6678a3 | 642 | if (init_srcu_struct(&kvm->srcu)) |
719d93cd CB |
643 | goto out_err_no_srcu; |
644 | if (init_srcu_struct(&kvm->irq_srcu)) | |
645 | goto out_err_no_irq_srcu; | |
e93f8a0f MT |
646 | for (i = 0; i < KVM_NR_BUSES; i++) { |
647 | kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus), | |
648 | GFP_KERNEL); | |
57e7fbee | 649 | if (!kvm->buses[i]) |
e93f8a0f | 650 | goto out_err; |
e93f8a0f | 651 | } |
e930bffe | 652 | |
74b5c5bf MW |
653 | r = kvm_init_mmu_notifier(kvm); |
654 | if (r) | |
655 | goto out_err; | |
656 | ||
2f303b74 | 657 | spin_lock(&kvm_lock); |
5e58cfe4 | 658 | list_add(&kvm->vm_list, &vm_list); |
2f303b74 | 659 | spin_unlock(&kvm_lock); |
d89f5eff | 660 | |
2ecd9d29 PZ |
661 | preempt_notifier_inc(); |
662 | ||
f17abe9a | 663 | return kvm; |
10474ae8 AG |
664 | |
665 | out_err: | |
719d93cd CB |
666 | cleanup_srcu_struct(&kvm->irq_srcu); |
667 | out_err_no_irq_srcu: | |
57e7fbee | 668 | cleanup_srcu_struct(&kvm->srcu); |
719d93cd | 669 | out_err_no_srcu: |
10474ae8 | 670 | hardware_disable_all(); |
719d93cd | 671 | out_err_no_disable: |
e93f8a0f MT |
672 | for (i = 0; i < KVM_NR_BUSES; i++) |
673 | kfree(kvm->buses[i]); | |
f481b069 PB |
674 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
675 | kvm_free_memslots(kvm, kvm->memslots[i]); | |
d89f5eff | 676 | kvm_arch_free_vm(kvm); |
e9ad4ec8 | 677 | mmdrop(current->mm); |
10474ae8 | 678 | return ERR_PTR(r); |
f17abe9a AK |
679 | } |
680 | ||
92eca8fa TY |
681 | /* |
682 | * Avoid using vmalloc for a small buffer. | |
683 | * Should not be used when the size is statically known. | |
684 | */ | |
c1a7b32a | 685 | void *kvm_kvzalloc(unsigned long size) |
92eca8fa TY |
686 | { |
687 | if (size > PAGE_SIZE) | |
688 | return vzalloc(size); | |
689 | else | |
690 | return kzalloc(size, GFP_KERNEL); | |
691 | } | |
692 | ||
07f0a7bd SW |
693 | static void kvm_destroy_devices(struct kvm *kvm) |
694 | { | |
e6e3b5a6 | 695 | struct kvm_device *dev, *tmp; |
07f0a7bd | 696 | |
e6e3b5a6 GT |
697 | list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) { |
698 | list_del(&dev->vm_node); | |
07f0a7bd SW |
699 | dev->ops->destroy(dev); |
700 | } | |
701 | } | |
702 | ||
f17abe9a AK |
703 | static void kvm_destroy_vm(struct kvm *kvm) |
704 | { | |
e93f8a0f | 705 | int i; |
6d4e4c4f AK |
706 | struct mm_struct *mm = kvm->mm; |
707 | ||
536a6f88 | 708 | kvm_destroy_vm_debugfs(kvm); |
ad8ba2cd | 709 | kvm_arch_sync_events(kvm); |
2f303b74 | 710 | spin_lock(&kvm_lock); |
133de902 | 711 | list_del(&kvm->vm_list); |
2f303b74 | 712 | spin_unlock(&kvm_lock); |
399ec807 | 713 | kvm_free_irq_routing(kvm); |
e93f8a0f MT |
714 | for (i = 0; i < KVM_NR_BUSES; i++) |
715 | kvm_io_bus_destroy(kvm->buses[i]); | |
980da6ce | 716 | kvm_coalesced_mmio_free(kvm); |
e930bffe AA |
717 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
718 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); | |
f00be0ca | 719 | #else |
2df72e9b | 720 | kvm_arch_flush_shadow_all(kvm); |
5f94c174 | 721 | #endif |
d19a9cd2 | 722 | kvm_arch_destroy_vm(kvm); |
07f0a7bd | 723 | kvm_destroy_devices(kvm); |
f481b069 PB |
724 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
725 | kvm_free_memslots(kvm, kvm->memslots[i]); | |
820b3fcd | 726 | cleanup_srcu_struct(&kvm->irq_srcu); |
d89f5eff JK |
727 | cleanup_srcu_struct(&kvm->srcu); |
728 | kvm_arch_free_vm(kvm); | |
2ecd9d29 | 729 | preempt_notifier_dec(); |
10474ae8 | 730 | hardware_disable_all(); |
6d4e4c4f | 731 | mmdrop(mm); |
f17abe9a AK |
732 | } |
733 | ||
d39f13b0 IE |
734 | void kvm_get_kvm(struct kvm *kvm) |
735 | { | |
736 | atomic_inc(&kvm->users_count); | |
737 | } | |
738 | EXPORT_SYMBOL_GPL(kvm_get_kvm); | |
739 | ||
740 | void kvm_put_kvm(struct kvm *kvm) | |
741 | { | |
742 | if (atomic_dec_and_test(&kvm->users_count)) | |
743 | kvm_destroy_vm(kvm); | |
744 | } | |
745 | EXPORT_SYMBOL_GPL(kvm_put_kvm); | |
746 | ||
747 | ||
f17abe9a AK |
748 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
749 | { | |
750 | struct kvm *kvm = filp->private_data; | |
751 | ||
721eecbf GH |
752 | kvm_irqfd_release(kvm); |
753 | ||
d39f13b0 | 754 | kvm_put_kvm(kvm); |
6aa8b732 AK |
755 | return 0; |
756 | } | |
757 | ||
515a0127 TY |
758 | /* |
759 | * Allocation size is twice as large as the actual dirty bitmap size. | |
93474b25 | 760 | * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed. |
515a0127 | 761 | */ |
a36a57b1 TY |
762 | static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) |
763 | { | |
515a0127 | 764 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); |
a36a57b1 | 765 | |
92eca8fa | 766 | memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes); |
a36a57b1 TY |
767 | if (!memslot->dirty_bitmap) |
768 | return -ENOMEM; | |
769 | ||
a36a57b1 TY |
770 | return 0; |
771 | } | |
772 | ||
bf3e05bc | 773 | /* |
0e60b079 IM |
774 | * Insert memslot and re-sort memslots based on their GFN, |
775 | * so binary search could be used to lookup GFN. | |
776 | * Sorting algorithm takes advantage of having initially | |
777 | * sorted array and known changed memslot position. | |
bf3e05bc | 778 | */ |
5cc15027 PB |
779 | static void update_memslots(struct kvm_memslots *slots, |
780 | struct kvm_memory_slot *new) | |
bf3e05bc | 781 | { |
8593176c PB |
782 | int id = new->id; |
783 | int i = slots->id_to_index[id]; | |
063584d4 | 784 | struct kvm_memory_slot *mslots = slots->memslots; |
f85e2cb5 | 785 | |
8593176c | 786 | WARN_ON(mslots[i].id != id); |
9c1a5d38 | 787 | if (!new->npages) { |
dbaff309 | 788 | WARN_ON(!mslots[i].npages); |
9c1a5d38 IM |
789 | if (mslots[i].npages) |
790 | slots->used_slots--; | |
791 | } else { | |
792 | if (!mslots[i].npages) | |
793 | slots->used_slots++; | |
794 | } | |
0e60b079 | 795 | |
7f379cff | 796 | while (i < KVM_MEM_SLOTS_NUM - 1 && |
0e60b079 IM |
797 | new->base_gfn <= mslots[i + 1].base_gfn) { |
798 | if (!mslots[i + 1].npages) | |
799 | break; | |
7f379cff IM |
800 | mslots[i] = mslots[i + 1]; |
801 | slots->id_to_index[mslots[i].id] = i; | |
802 | i++; | |
803 | } | |
efbeec70 PB |
804 | |
805 | /* | |
806 | * The ">=" is needed when creating a slot with base_gfn == 0, | |
807 | * so that it moves before all those with base_gfn == npages == 0. | |
808 | * | |
809 | * On the other hand, if new->npages is zero, the above loop has | |
810 | * already left i pointing to the beginning of the empty part of | |
811 | * mslots, and the ">=" would move the hole backwards in this | |
812 | * case---which is wrong. So skip the loop when deleting a slot. | |
813 | */ | |
814 | if (new->npages) { | |
815 | while (i > 0 && | |
816 | new->base_gfn >= mslots[i - 1].base_gfn) { | |
817 | mslots[i] = mslots[i - 1]; | |
818 | slots->id_to_index[mslots[i].id] = i; | |
819 | i--; | |
820 | } | |
dbaff309 PB |
821 | } else |
822 | WARN_ON_ONCE(i != slots->used_slots); | |
f85e2cb5 | 823 | |
8593176c PB |
824 | mslots[i] = *new; |
825 | slots->id_to_index[mslots[i].id] = i; | |
bf3e05bc XG |
826 | } |
827 | ||
09170a49 | 828 | static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem) |
a50d64d6 | 829 | { |
4d8b81ab XG |
830 | u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; |
831 | ||
0f8a4de3 | 832 | #ifdef __KVM_HAVE_READONLY_MEM |
4d8b81ab XG |
833 | valid_flags |= KVM_MEM_READONLY; |
834 | #endif | |
835 | ||
836 | if (mem->flags & ~valid_flags) | |
a50d64d6 XG |
837 | return -EINVAL; |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
7ec4fb44 | 842 | static struct kvm_memslots *install_new_memslots(struct kvm *kvm, |
f481b069 | 843 | int as_id, struct kvm_memslots *slots) |
7ec4fb44 | 844 | { |
f481b069 | 845 | struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id); |
7ec4fb44 | 846 | |
ee3d1570 DM |
847 | /* |
848 | * Set the low bit in the generation, which disables SPTE caching | |
849 | * until the end of synchronize_srcu_expedited. | |
850 | */ | |
851 | WARN_ON(old_memslots->generation & 1); | |
852 | slots->generation = old_memslots->generation + 1; | |
853 | ||
f481b069 | 854 | rcu_assign_pointer(kvm->memslots[as_id], slots); |
7ec4fb44 | 855 | synchronize_srcu_expedited(&kvm->srcu); |
e59dbe09 | 856 | |
ee3d1570 DM |
857 | /* |
858 | * Increment the new memslot generation a second time. This prevents | |
859 | * vm exits that race with memslot updates from caching a memslot | |
860 | * generation that will (potentially) be valid forever. | |
861 | */ | |
862 | slots->generation++; | |
863 | ||
15f46015 | 864 | kvm_arch_memslots_updated(kvm, slots); |
e59dbe09 TY |
865 | |
866 | return old_memslots; | |
7ec4fb44 GN |
867 | } |
868 | ||
6aa8b732 AK |
869 | /* |
870 | * Allocate some memory and give it an address in the guest physical address | |
871 | * space. | |
872 | * | |
873 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e | 874 | * |
02d5d55b | 875 | * Must be called holding kvm->slots_lock for write. |
6aa8b732 | 876 | */ |
f78e0e2e | 877 | int __kvm_set_memory_region(struct kvm *kvm, |
09170a49 | 878 | const struct kvm_userspace_memory_region *mem) |
6aa8b732 | 879 | { |
8234b22e | 880 | int r; |
6aa8b732 | 881 | gfn_t base_gfn; |
28bcb112 | 882 | unsigned long npages; |
a843fac2 | 883 | struct kvm_memory_slot *slot; |
6aa8b732 | 884 | struct kvm_memory_slot old, new; |
b7f69c55 | 885 | struct kvm_memslots *slots = NULL, *old_memslots; |
f481b069 | 886 | int as_id, id; |
f64c0398 | 887 | enum kvm_mr_change change; |
6aa8b732 | 888 | |
a50d64d6 XG |
889 | r = check_memory_region_flags(mem); |
890 | if (r) | |
891 | goto out; | |
892 | ||
6aa8b732 | 893 | r = -EINVAL; |
f481b069 PB |
894 | as_id = mem->slot >> 16; |
895 | id = (u16)mem->slot; | |
896 | ||
6aa8b732 AK |
897 | /* General sanity checks */ |
898 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
899 | goto out; | |
900 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
901 | goto out; | |
fa3d315a | 902 | /* We can read the guest memory with __xxx_user() later on. */ |
f481b069 | 903 | if ((id < KVM_USER_MEM_SLOTS) && |
fa3d315a | 904 | ((mem->userspace_addr & (PAGE_SIZE - 1)) || |
9e3bb6b6 HC |
905 | !access_ok(VERIFY_WRITE, |
906 | (void __user *)(unsigned long)mem->userspace_addr, | |
907 | mem->memory_size))) | |
78749809 | 908 | goto out; |
f481b069 | 909 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) |
6aa8b732 AK |
910 | goto out; |
911 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
912 | goto out; | |
913 | ||
f481b069 | 914 | slot = id_to_memslot(__kvm_memslots(kvm, as_id), id); |
6aa8b732 AK |
915 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
916 | npages = mem->memory_size >> PAGE_SHIFT; | |
917 | ||
660c22c4 TY |
918 | if (npages > KVM_MEM_MAX_NR_PAGES) |
919 | goto out; | |
920 | ||
a843fac2 | 921 | new = old = *slot; |
6aa8b732 | 922 | |
f481b069 | 923 | new.id = id; |
6aa8b732 AK |
924 | new.base_gfn = base_gfn; |
925 | new.npages = npages; | |
926 | new.flags = mem->flags; | |
927 | ||
f64c0398 TY |
928 | if (npages) { |
929 | if (!old.npages) | |
930 | change = KVM_MR_CREATE; | |
931 | else { /* Modify an existing slot. */ | |
932 | if ((mem->userspace_addr != old.userspace_addr) || | |
75d61fbc TY |
933 | (npages != old.npages) || |
934 | ((new.flags ^ old.flags) & KVM_MEM_READONLY)) | |
f64c0398 TY |
935 | goto out; |
936 | ||
937 | if (base_gfn != old.base_gfn) | |
938 | change = KVM_MR_MOVE; | |
939 | else if (new.flags != old.flags) | |
940 | change = KVM_MR_FLAGS_ONLY; | |
941 | else { /* Nothing to change. */ | |
942 | r = 0; | |
943 | goto out; | |
944 | } | |
945 | } | |
09170a49 PB |
946 | } else { |
947 | if (!old.npages) | |
948 | goto out; | |
949 | ||
f64c0398 | 950 | change = KVM_MR_DELETE; |
09170a49 PB |
951 | new.base_gfn = 0; |
952 | new.flags = 0; | |
953 | } | |
6aa8b732 | 954 | |
f64c0398 | 955 | if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { |
0a706bee TY |
956 | /* Check for overlaps */ |
957 | r = -EEXIST; | |
f481b069 | 958 | kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) { |
a843fac2 | 959 | if ((slot->id >= KVM_USER_MEM_SLOTS) || |
f481b069 | 960 | (slot->id == id)) |
0a706bee TY |
961 | continue; |
962 | if (!((base_gfn + npages <= slot->base_gfn) || | |
963 | (base_gfn >= slot->base_gfn + slot->npages))) | |
964 | goto out; | |
965 | } | |
6aa8b732 | 966 | } |
6aa8b732 | 967 | |
6aa8b732 AK |
968 | /* Free page dirty bitmap if unneeded */ |
969 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 970 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
971 | |
972 | r = -ENOMEM; | |
f64c0398 | 973 | if (change == KVM_MR_CREATE) { |
189a2f7b | 974 | new.userspace_addr = mem->userspace_addr; |
d89cc617 | 975 | |
5587027c | 976 | if (kvm_arch_create_memslot(kvm, &new, npages)) |
db3fe4eb | 977 | goto out_free; |
6aa8b732 | 978 | } |
ec04b260 | 979 | |
6aa8b732 AK |
980 | /* Allocate page dirty bitmap if needed */ |
981 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
a36a57b1 | 982 | if (kvm_create_dirty_bitmap(&new) < 0) |
f78e0e2e | 983 | goto out_free; |
6aa8b732 AK |
984 | } |
985 | ||
74496134 | 986 | slots = kvm_kvzalloc(sizeof(struct kvm_memslots)); |
f2a81036 PB |
987 | if (!slots) |
988 | goto out_free; | |
f481b069 | 989 | memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots)); |
f2a81036 | 990 | |
f64c0398 | 991 | if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) { |
f481b069 | 992 | slot = id_to_memslot(slots, id); |
28a37544 XG |
993 | slot->flags |= KVM_MEMSLOT_INVALID; |
994 | ||
f481b069 | 995 | old_memslots = install_new_memslots(kvm, as_id, slots); |
bc6678a3 | 996 | |
e40f193f AW |
997 | /* slot was deleted or moved, clear iommu mapping */ |
998 | kvm_iommu_unmap_pages(kvm, &old); | |
12d6e753 MT |
999 | /* From this point no new shadow pages pointing to a deleted, |
1000 | * or moved, memslot will be created. | |
bc6678a3 MT |
1001 | * |
1002 | * validation of sp->gfn happens in: | |
b7d409de XL |
1003 | * - gfn_to_hva (kvm_read_guest, gfn_to_pfn) |
1004 | * - kvm_is_visible_gfn (mmu_check_roots) | |
bc6678a3 | 1005 | */ |
2df72e9b | 1006 | kvm_arch_flush_shadow_memslot(kvm, slot); |
f2a81036 PB |
1007 | |
1008 | /* | |
1009 | * We can re-use the old_memslots from above, the only difference | |
1010 | * from the currently installed memslots is the invalid flag. This | |
1011 | * will get overwritten by update_memslots anyway. | |
1012 | */ | |
b7f69c55 | 1013 | slots = old_memslots; |
bc6678a3 | 1014 | } |
34d4cb8f | 1015 | |
7b6195a9 | 1016 | r = kvm_arch_prepare_memory_region(kvm, &new, mem, change); |
f7784b8e | 1017 | if (r) |
b7f69c55 | 1018 | goto out_slots; |
f7784b8e | 1019 | |
a47d2b07 | 1020 | /* actual memory is freed via old in kvm_free_memslot below */ |
f64c0398 | 1021 | if (change == KVM_MR_DELETE) { |
bc6678a3 | 1022 | new.dirty_bitmap = NULL; |
db3fe4eb | 1023 | memset(&new.arch, 0, sizeof(new.arch)); |
bc6678a3 MT |
1024 | } |
1025 | ||
5cc15027 | 1026 | update_memslots(slots, &new); |
f481b069 | 1027 | old_memslots = install_new_memslots(kvm, as_id, slots); |
3ad82a7e | 1028 | |
f36f3f28 | 1029 | kvm_arch_commit_memory_region(kvm, mem, &old, &new, change); |
82ce2c96 | 1030 | |
a47d2b07 | 1031 | kvm_free_memslot(kvm, &old, &new); |
74496134 | 1032 | kvfree(old_memslots); |
bc6678a3 | 1033 | |
261874b0 AW |
1034 | /* |
1035 | * IOMMU mapping: New slots need to be mapped. Old slots need to be | |
75d61fbc TY |
1036 | * un-mapped and re-mapped if their base changes. Since base change |
1037 | * unmapping is handled above with slot deletion, mapping alone is | |
1038 | * needed here. Anything else the iommu might care about for existing | |
1039 | * slots (size changes, userspace addr changes and read-only flag | |
1040 | * changes) is disallowed above, so any other attribute changes getting | |
1041 | * here can be skipped. | |
261874b0 | 1042 | */ |
75d61fbc TY |
1043 | if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { |
1044 | r = kvm_iommu_map_pages(kvm, &new); | |
e0230e13 | 1045 | return r; |
bc6678a3 MT |
1046 | } |
1047 | ||
6aa8b732 AK |
1048 | return 0; |
1049 | ||
e40f193f | 1050 | out_slots: |
74496134 | 1051 | kvfree(slots); |
f78e0e2e | 1052 | out_free: |
a47d2b07 | 1053 | kvm_free_memslot(kvm, &new, &old); |
6aa8b732 AK |
1054 | out: |
1055 | return r; | |
210c7c4d | 1056 | } |
f78e0e2e SY |
1057 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
1058 | ||
1059 | int kvm_set_memory_region(struct kvm *kvm, | |
09170a49 | 1060 | const struct kvm_userspace_memory_region *mem) |
f78e0e2e SY |
1061 | { |
1062 | int r; | |
1063 | ||
79fac95e | 1064 | mutex_lock(&kvm->slots_lock); |
47ae31e2 | 1065 | r = __kvm_set_memory_region(kvm, mem); |
79fac95e | 1066 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
1067 | return r; |
1068 | } | |
210c7c4d IE |
1069 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
1070 | ||
7940876e SH |
1071 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
1072 | struct kvm_userspace_memory_region *mem) | |
210c7c4d | 1073 | { |
f481b069 | 1074 | if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) |
e0d62c7f | 1075 | return -EINVAL; |
09170a49 | 1076 | |
47ae31e2 | 1077 | return kvm_set_memory_region(kvm, mem); |
6aa8b732 AK |
1078 | } |
1079 | ||
5bb064dc ZX |
1080 | int kvm_get_dirty_log(struct kvm *kvm, |
1081 | struct kvm_dirty_log *log, int *is_dirty) | |
6aa8b732 | 1082 | { |
9f6b8029 | 1083 | struct kvm_memslots *slots; |
6aa8b732 | 1084 | struct kvm_memory_slot *memslot; |
f481b069 | 1085 | int r, i, as_id, id; |
87bf6e7d | 1086 | unsigned long n; |
6aa8b732 AK |
1087 | unsigned long any = 0; |
1088 | ||
6aa8b732 | 1089 | r = -EINVAL; |
f481b069 PB |
1090 | as_id = log->slot >> 16; |
1091 | id = (u16)log->slot; | |
1092 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) | |
6aa8b732 AK |
1093 | goto out; |
1094 | ||
f481b069 PB |
1095 | slots = __kvm_memslots(kvm, as_id); |
1096 | memslot = id_to_memslot(slots, id); | |
6aa8b732 AK |
1097 | r = -ENOENT; |
1098 | if (!memslot->dirty_bitmap) | |
1099 | goto out; | |
1100 | ||
87bf6e7d | 1101 | n = kvm_dirty_bitmap_bytes(memslot); |
6aa8b732 | 1102 | |
cd1a4a98 | 1103 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
1104 | any = memslot->dirty_bitmap[i]; |
1105 | ||
1106 | r = -EFAULT; | |
1107 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
1108 | goto out; | |
1109 | ||
5bb064dc ZX |
1110 | if (any) |
1111 | *is_dirty = 1; | |
6aa8b732 AK |
1112 | |
1113 | r = 0; | |
6aa8b732 | 1114 | out: |
6aa8b732 AK |
1115 | return r; |
1116 | } | |
2ba9f0d8 | 1117 | EXPORT_SYMBOL_GPL(kvm_get_dirty_log); |
6aa8b732 | 1118 | |
ba0513b5 MS |
1119 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
1120 | /** | |
1121 | * kvm_get_dirty_log_protect - get a snapshot of dirty pages, and if any pages | |
1122 | * are dirty write protect them for next write. | |
1123 | * @kvm: pointer to kvm instance | |
1124 | * @log: slot id and address to which we copy the log | |
1125 | * @is_dirty: flag set if any page is dirty | |
1126 | * | |
1127 | * We need to keep it in mind that VCPU threads can write to the bitmap | |
1128 | * concurrently. So, to avoid losing track of dirty pages we keep the | |
1129 | * following order: | |
1130 | * | |
1131 | * 1. Take a snapshot of the bit and clear it if needed. | |
1132 | * 2. Write protect the corresponding page. | |
1133 | * 3. Copy the snapshot to the userspace. | |
1134 | * 4. Upon return caller flushes TLB's if needed. | |
1135 | * | |
1136 | * Between 2 and 4, the guest may write to the page using the remaining TLB | |
1137 | * entry. This is not a problem because the page is reported dirty using | |
1138 | * the snapshot taken before and step 4 ensures that writes done after | |
1139 | * exiting to userspace will be logged for the next call. | |
1140 | * | |
1141 | */ | |
1142 | int kvm_get_dirty_log_protect(struct kvm *kvm, | |
1143 | struct kvm_dirty_log *log, bool *is_dirty) | |
1144 | { | |
9f6b8029 | 1145 | struct kvm_memslots *slots; |
ba0513b5 | 1146 | struct kvm_memory_slot *memslot; |
f481b069 | 1147 | int r, i, as_id, id; |
ba0513b5 MS |
1148 | unsigned long n; |
1149 | unsigned long *dirty_bitmap; | |
1150 | unsigned long *dirty_bitmap_buffer; | |
1151 | ||
1152 | r = -EINVAL; | |
f481b069 PB |
1153 | as_id = log->slot >> 16; |
1154 | id = (u16)log->slot; | |
1155 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) | |
ba0513b5 MS |
1156 | goto out; |
1157 | ||
f481b069 PB |
1158 | slots = __kvm_memslots(kvm, as_id); |
1159 | memslot = id_to_memslot(slots, id); | |
ba0513b5 MS |
1160 | |
1161 | dirty_bitmap = memslot->dirty_bitmap; | |
1162 | r = -ENOENT; | |
1163 | if (!dirty_bitmap) | |
1164 | goto out; | |
1165 | ||
1166 | n = kvm_dirty_bitmap_bytes(memslot); | |
1167 | ||
1168 | dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long); | |
1169 | memset(dirty_bitmap_buffer, 0, n); | |
1170 | ||
1171 | spin_lock(&kvm->mmu_lock); | |
1172 | *is_dirty = false; | |
1173 | for (i = 0; i < n / sizeof(long); i++) { | |
1174 | unsigned long mask; | |
1175 | gfn_t offset; | |
1176 | ||
1177 | if (!dirty_bitmap[i]) | |
1178 | continue; | |
1179 | ||
1180 | *is_dirty = true; | |
1181 | ||
1182 | mask = xchg(&dirty_bitmap[i], 0); | |
1183 | dirty_bitmap_buffer[i] = mask; | |
1184 | ||
58d2930f TY |
1185 | if (mask) { |
1186 | offset = i * BITS_PER_LONG; | |
1187 | kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, | |
1188 | offset, mask); | |
1189 | } | |
ba0513b5 MS |
1190 | } |
1191 | ||
1192 | spin_unlock(&kvm->mmu_lock); | |
1193 | ||
1194 | r = -EFAULT; | |
1195 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n)) | |
1196 | goto out; | |
1197 | ||
1198 | r = 0; | |
1199 | out: | |
1200 | return r; | |
1201 | } | |
1202 | EXPORT_SYMBOL_GPL(kvm_get_dirty_log_protect); | |
1203 | #endif | |
1204 | ||
db3fe4eb TY |
1205 | bool kvm_largepages_enabled(void) |
1206 | { | |
1207 | return largepages_enabled; | |
1208 | } | |
1209 | ||
54dee993 MT |
1210 | void kvm_disable_largepages(void) |
1211 | { | |
1212 | largepages_enabled = false; | |
1213 | } | |
1214 | EXPORT_SYMBOL_GPL(kvm_disable_largepages); | |
1215 | ||
49c7754c GN |
1216 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
1217 | { | |
1218 | return __gfn_to_memslot(kvm_memslots(kvm), gfn); | |
1219 | } | |
a1f4d395 | 1220 | EXPORT_SYMBOL_GPL(gfn_to_memslot); |
6aa8b732 | 1221 | |
8e73485c PB |
1222 | struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn) |
1223 | { | |
1224 | return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn); | |
1225 | } | |
1226 | ||
33e94154 | 1227 | bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
e0d62c7f | 1228 | { |
bf3e05bc | 1229 | struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); |
e0d62c7f | 1230 | |
bbacc0c1 | 1231 | if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS || |
bf3e05bc | 1232 | memslot->flags & KVM_MEMSLOT_INVALID) |
33e94154 | 1233 | return false; |
e0d62c7f | 1234 | |
33e94154 | 1235 | return true; |
e0d62c7f IE |
1236 | } |
1237 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
1238 | ||
8f0b1ab6 JR |
1239 | unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn) |
1240 | { | |
1241 | struct vm_area_struct *vma; | |
1242 | unsigned long addr, size; | |
1243 | ||
1244 | size = PAGE_SIZE; | |
1245 | ||
1246 | addr = gfn_to_hva(kvm, gfn); | |
1247 | if (kvm_is_error_hva(addr)) | |
1248 | return PAGE_SIZE; | |
1249 | ||
1250 | down_read(¤t->mm->mmap_sem); | |
1251 | vma = find_vma(current->mm, addr); | |
1252 | if (!vma) | |
1253 | goto out; | |
1254 | ||
1255 | size = vma_kernel_pagesize(vma); | |
1256 | ||
1257 | out: | |
1258 | up_read(¤t->mm->mmap_sem); | |
1259 | ||
1260 | return size; | |
1261 | } | |
1262 | ||
4d8b81ab XG |
1263 | static bool memslot_is_readonly(struct kvm_memory_slot *slot) |
1264 | { | |
1265 | return slot->flags & KVM_MEM_READONLY; | |
1266 | } | |
1267 | ||
4d8b81ab XG |
1268 | static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
1269 | gfn_t *nr_pages, bool write) | |
539cb660 | 1270 | { |
bc6678a3 | 1271 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
ca3a490c | 1272 | return KVM_HVA_ERR_BAD; |
48987781 | 1273 | |
4d8b81ab XG |
1274 | if (memslot_is_readonly(slot) && write) |
1275 | return KVM_HVA_ERR_RO_BAD; | |
48987781 XG |
1276 | |
1277 | if (nr_pages) | |
1278 | *nr_pages = slot->npages - (gfn - slot->base_gfn); | |
1279 | ||
4d8b81ab | 1280 | return __gfn_to_hva_memslot(slot, gfn); |
539cb660 | 1281 | } |
48987781 | 1282 | |
4d8b81ab XG |
1283 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
1284 | gfn_t *nr_pages) | |
1285 | { | |
1286 | return __gfn_to_hva_many(slot, gfn, nr_pages, true); | |
539cb660 | 1287 | } |
48987781 | 1288 | |
4d8b81ab | 1289 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, |
7940876e | 1290 | gfn_t gfn) |
4d8b81ab XG |
1291 | { |
1292 | return gfn_to_hva_many(slot, gfn, NULL); | |
1293 | } | |
1294 | EXPORT_SYMBOL_GPL(gfn_to_hva_memslot); | |
1295 | ||
48987781 XG |
1296 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
1297 | { | |
49c7754c | 1298 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); |
48987781 | 1299 | } |
0d150298 | 1300 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
539cb660 | 1301 | |
8e73485c PB |
1302 | unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn) |
1303 | { | |
1304 | return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL); | |
1305 | } | |
1306 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva); | |
1307 | ||
86ab8cff | 1308 | /* |
ba6a3541 PB |
1309 | * If writable is set to false, the hva returned by this function is only |
1310 | * allowed to be read. | |
86ab8cff | 1311 | */ |
64d83126 CD |
1312 | unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, |
1313 | gfn_t gfn, bool *writable) | |
86ab8cff | 1314 | { |
a2ac07fe GN |
1315 | unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false); |
1316 | ||
1317 | if (!kvm_is_error_hva(hva) && writable) | |
ba6a3541 PB |
1318 | *writable = !memslot_is_readonly(slot); |
1319 | ||
a2ac07fe | 1320 | return hva; |
86ab8cff XG |
1321 | } |
1322 | ||
64d83126 CD |
1323 | unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable) |
1324 | { | |
1325 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1326 | ||
1327 | return gfn_to_hva_memslot_prot(slot, gfn, writable); | |
1328 | } | |
1329 | ||
8e73485c PB |
1330 | unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable) |
1331 | { | |
1332 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1333 | ||
1334 | return gfn_to_hva_memslot_prot(slot, gfn, writable); | |
1335 | } | |
1336 | ||
d4edcf0d DH |
1337 | static int get_user_page_nowait(unsigned long start, int write, |
1338 | struct page **page) | |
0857b9e9 GN |
1339 | { |
1340 | int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET; | |
1341 | ||
1342 | if (write) | |
1343 | flags |= FOLL_WRITE; | |
1344 | ||
d4edcf0d DH |
1345 | return __get_user_pages(current, current->mm, start, 1, flags, page, |
1346 | NULL, NULL); | |
0857b9e9 GN |
1347 | } |
1348 | ||
fafc3dba HY |
1349 | static inline int check_user_page_hwpoison(unsigned long addr) |
1350 | { | |
1351 | int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE; | |
1352 | ||
1353 | rc = __get_user_pages(current, current->mm, addr, 1, | |
1354 | flags, NULL, NULL, NULL); | |
1355 | return rc == -EHWPOISON; | |
1356 | } | |
1357 | ||
2fc84311 XG |
1358 | /* |
1359 | * The atomic path to get the writable pfn which will be stored in @pfn, | |
1360 | * true indicates success, otherwise false is returned. | |
1361 | */ | |
1362 | static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async, | |
ba049e93 | 1363 | bool write_fault, bool *writable, kvm_pfn_t *pfn) |
954bbbc2 | 1364 | { |
8d4e1288 | 1365 | struct page *page[1]; |
2fc84311 | 1366 | int npages; |
954bbbc2 | 1367 | |
2fc84311 XG |
1368 | if (!(async || atomic)) |
1369 | return false; | |
af585b92 | 1370 | |
12ce13fe XG |
1371 | /* |
1372 | * Fast pin a writable pfn only if it is a write fault request | |
1373 | * or the caller allows to map a writable pfn for a read fault | |
1374 | * request. | |
1375 | */ | |
1376 | if (!(write_fault || writable)) | |
1377 | return false; | |
612819c3 | 1378 | |
2fc84311 XG |
1379 | npages = __get_user_pages_fast(addr, 1, 1, page); |
1380 | if (npages == 1) { | |
1381 | *pfn = page_to_pfn(page[0]); | |
612819c3 | 1382 | |
2fc84311 XG |
1383 | if (writable) |
1384 | *writable = true; | |
1385 | return true; | |
1386 | } | |
af585b92 | 1387 | |
2fc84311 XG |
1388 | return false; |
1389 | } | |
612819c3 | 1390 | |
2fc84311 XG |
1391 | /* |
1392 | * The slow path to get the pfn of the specified host virtual address, | |
1393 | * 1 indicates success, -errno is returned if error is detected. | |
1394 | */ | |
1395 | static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, | |
ba049e93 | 1396 | bool *writable, kvm_pfn_t *pfn) |
2fc84311 XG |
1397 | { |
1398 | struct page *page[1]; | |
1399 | int npages = 0; | |
612819c3 | 1400 | |
2fc84311 XG |
1401 | might_sleep(); |
1402 | ||
1403 | if (writable) | |
1404 | *writable = write_fault; | |
1405 | ||
1406 | if (async) { | |
1407 | down_read(¤t->mm->mmap_sem); | |
d4edcf0d | 1408 | npages = get_user_page_nowait(addr, write_fault, page); |
2fc84311 | 1409 | up_read(¤t->mm->mmap_sem); |
0664e57f AA |
1410 | } else |
1411 | npages = __get_user_pages_unlocked(current, current->mm, addr, 1, | |
1412 | write_fault, 0, page, | |
1413 | FOLL_TOUCH|FOLL_HWPOISON); | |
2fc84311 XG |
1414 | if (npages != 1) |
1415 | return npages; | |
1416 | ||
1417 | /* map read fault as writable if possible */ | |
12ce13fe | 1418 | if (unlikely(!write_fault) && writable) { |
2fc84311 XG |
1419 | struct page *wpage[1]; |
1420 | ||
1421 | npages = __get_user_pages_fast(addr, 1, 1, wpage); | |
1422 | if (npages == 1) { | |
1423 | *writable = true; | |
1424 | put_page(page[0]); | |
1425 | page[0] = wpage[0]; | |
612819c3 | 1426 | } |
2fc84311 XG |
1427 | |
1428 | npages = 1; | |
887c08ac | 1429 | } |
2fc84311 XG |
1430 | *pfn = page_to_pfn(page[0]); |
1431 | return npages; | |
1432 | } | |
539cb660 | 1433 | |
4d8b81ab XG |
1434 | static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) |
1435 | { | |
1436 | if (unlikely(!(vma->vm_flags & VM_READ))) | |
1437 | return false; | |
2e2e3738 | 1438 | |
4d8b81ab XG |
1439 | if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE)))) |
1440 | return false; | |
887c08ac | 1441 | |
4d8b81ab XG |
1442 | return true; |
1443 | } | |
bf998156 | 1444 | |
92176a8e PB |
1445 | static int hva_to_pfn_remapped(struct vm_area_struct *vma, |
1446 | unsigned long addr, bool *async, | |
1447 | bool write_fault, kvm_pfn_t *p_pfn) | |
1448 | { | |
1449 | *p_pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + | |
1450 | vma->vm_pgoff; | |
1451 | BUG_ON(!kvm_is_reserved_pfn(*p_pfn)); | |
1452 | return 0; | |
1453 | } | |
1454 | ||
12ce13fe XG |
1455 | /* |
1456 | * Pin guest page in memory and return its pfn. | |
1457 | * @addr: host virtual address which maps memory to the guest | |
1458 | * @atomic: whether this function can sleep | |
1459 | * @async: whether this function need to wait IO complete if the | |
1460 | * host page is not in the memory | |
1461 | * @write_fault: whether we should get a writable host page | |
1462 | * @writable: whether it allows to map a writable host page for !@write_fault | |
1463 | * | |
1464 | * The function will map a writable host page for these two cases: | |
1465 | * 1): @write_fault = true | |
1466 | * 2): @write_fault = false && @writable, @writable will tell the caller | |
1467 | * whether the mapping is writable. | |
1468 | */ | |
ba049e93 | 1469 | static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async, |
2fc84311 XG |
1470 | bool write_fault, bool *writable) |
1471 | { | |
1472 | struct vm_area_struct *vma; | |
ba049e93 | 1473 | kvm_pfn_t pfn = 0; |
92176a8e | 1474 | int npages, r; |
2e2e3738 | 1475 | |
2fc84311 XG |
1476 | /* we can do it either atomically or asynchronously, not both */ |
1477 | BUG_ON(atomic && async); | |
8d4e1288 | 1478 | |
2fc84311 XG |
1479 | if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn)) |
1480 | return pfn; | |
1481 | ||
1482 | if (atomic) | |
1483 | return KVM_PFN_ERR_FAULT; | |
1484 | ||
1485 | npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn); | |
1486 | if (npages == 1) | |
1487 | return pfn; | |
8d4e1288 | 1488 | |
2fc84311 XG |
1489 | down_read(¤t->mm->mmap_sem); |
1490 | if (npages == -EHWPOISON || | |
1491 | (!async && check_user_page_hwpoison(addr))) { | |
1492 | pfn = KVM_PFN_ERR_HWPOISON; | |
1493 | goto exit; | |
1494 | } | |
1495 | ||
1496 | vma = find_vma_intersection(current->mm, addr, addr + 1); | |
1497 | ||
1498 | if (vma == NULL) | |
1499 | pfn = KVM_PFN_ERR_FAULT; | |
92176a8e PB |
1500 | else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { |
1501 | r = hva_to_pfn_remapped(vma, addr, async, write_fault, &pfn); | |
1502 | if (r < 0) | |
1503 | pfn = KVM_PFN_ERR_FAULT; | |
2fc84311 | 1504 | } else { |
4d8b81ab | 1505 | if (async && vma_is_valid(vma, write_fault)) |
2fc84311 XG |
1506 | *async = true; |
1507 | pfn = KVM_PFN_ERR_FAULT; | |
1508 | } | |
1509 | exit: | |
1510 | up_read(¤t->mm->mmap_sem); | |
2e2e3738 | 1511 | return pfn; |
35149e21 AL |
1512 | } |
1513 | ||
ba049e93 DW |
1514 | kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, |
1515 | bool atomic, bool *async, bool write_fault, | |
1516 | bool *writable) | |
887c08ac | 1517 | { |
4d8b81ab XG |
1518 | unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); |
1519 | ||
b2740d35 PB |
1520 | if (addr == KVM_HVA_ERR_RO_BAD) { |
1521 | if (writable) | |
1522 | *writable = false; | |
4d8b81ab | 1523 | return KVM_PFN_ERR_RO_FAULT; |
b2740d35 | 1524 | } |
4d8b81ab | 1525 | |
b2740d35 PB |
1526 | if (kvm_is_error_hva(addr)) { |
1527 | if (writable) | |
1528 | *writable = false; | |
81c52c56 | 1529 | return KVM_PFN_NOSLOT; |
b2740d35 | 1530 | } |
4d8b81ab XG |
1531 | |
1532 | /* Do not map writable pfn in the readonly memslot. */ | |
1533 | if (writable && memslot_is_readonly(slot)) { | |
1534 | *writable = false; | |
1535 | writable = NULL; | |
1536 | } | |
1537 | ||
1538 | return hva_to_pfn(addr, atomic, async, write_fault, | |
1539 | writable); | |
887c08ac | 1540 | } |
3520469d | 1541 | EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); |
887c08ac | 1542 | |
ba049e93 | 1543 | kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
612819c3 MT |
1544 | bool *writable) |
1545 | { | |
e37afc6e PB |
1546 | return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL, |
1547 | write_fault, writable); | |
612819c3 MT |
1548 | } |
1549 | EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); | |
1550 | ||
ba049e93 | 1551 | kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn) |
506f0d6f | 1552 | { |
4d8b81ab | 1553 | return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL); |
506f0d6f | 1554 | } |
e37afc6e | 1555 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); |
506f0d6f | 1556 | |
ba049e93 | 1557 | kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn) |
506f0d6f | 1558 | { |
4d8b81ab | 1559 | return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL); |
506f0d6f | 1560 | } |
037d92dc | 1561 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); |
506f0d6f | 1562 | |
ba049e93 | 1563 | kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn) |
e37afc6e PB |
1564 | { |
1565 | return gfn_to_pfn_memslot_atomic(gfn_to_memslot(kvm, gfn), gfn); | |
1566 | } | |
1567 | EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic); | |
1568 | ||
ba049e93 | 1569 | kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn) |
8e73485c PB |
1570 | { |
1571 | return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); | |
1572 | } | |
1573 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic); | |
1574 | ||
ba049e93 | 1575 | kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
e37afc6e PB |
1576 | { |
1577 | return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); | |
1578 | } | |
1579 | EXPORT_SYMBOL_GPL(gfn_to_pfn); | |
1580 | ||
ba049e93 | 1581 | kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
8e73485c PB |
1582 | { |
1583 | return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); | |
1584 | } | |
1585 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); | |
1586 | ||
d9ef13c2 PB |
1587 | int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
1588 | struct page **pages, int nr_pages) | |
48987781 XG |
1589 | { |
1590 | unsigned long addr; | |
1591 | gfn_t entry; | |
1592 | ||
d9ef13c2 | 1593 | addr = gfn_to_hva_many(slot, gfn, &entry); |
48987781 XG |
1594 | if (kvm_is_error_hva(addr)) |
1595 | return -1; | |
1596 | ||
1597 | if (entry < nr_pages) | |
1598 | return 0; | |
1599 | ||
1600 | return __get_user_pages_fast(addr, nr_pages, 1, pages); | |
1601 | } | |
1602 | EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); | |
1603 | ||
ba049e93 | 1604 | static struct page *kvm_pfn_to_page(kvm_pfn_t pfn) |
a2766325 | 1605 | { |
81c52c56 | 1606 | if (is_error_noslot_pfn(pfn)) |
cb9aaa30 | 1607 | return KVM_ERR_PTR_BAD_PAGE; |
a2766325 | 1608 | |
bf4bea8e | 1609 | if (kvm_is_reserved_pfn(pfn)) { |
cb9aaa30 | 1610 | WARN_ON(1); |
6cede2e6 | 1611 | return KVM_ERR_PTR_BAD_PAGE; |
cb9aaa30 | 1612 | } |
a2766325 XG |
1613 | |
1614 | return pfn_to_page(pfn); | |
1615 | } | |
1616 | ||
35149e21 AL |
1617 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
1618 | { | |
ba049e93 | 1619 | kvm_pfn_t pfn; |
2e2e3738 AL |
1620 | |
1621 | pfn = gfn_to_pfn(kvm, gfn); | |
2e2e3738 | 1622 | |
a2766325 | 1623 | return kvm_pfn_to_page(pfn); |
954bbbc2 AK |
1624 | } |
1625 | EXPORT_SYMBOL_GPL(gfn_to_page); | |
1626 | ||
8e73485c PB |
1627 | struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
1628 | { | |
ba049e93 | 1629 | kvm_pfn_t pfn; |
8e73485c PB |
1630 | |
1631 | pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn); | |
1632 | ||
1633 | return kvm_pfn_to_page(pfn); | |
1634 | } | |
1635 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page); | |
1636 | ||
b4231d61 IE |
1637 | void kvm_release_page_clean(struct page *page) |
1638 | { | |
32cad84f XG |
1639 | WARN_ON(is_error_page(page)); |
1640 | ||
35149e21 | 1641 | kvm_release_pfn_clean(page_to_pfn(page)); |
b4231d61 IE |
1642 | } |
1643 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); | |
1644 | ||
ba049e93 | 1645 | void kvm_release_pfn_clean(kvm_pfn_t pfn) |
35149e21 | 1646 | { |
bf4bea8e | 1647 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1648 | put_page(pfn_to_page(pfn)); |
35149e21 AL |
1649 | } |
1650 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); | |
1651 | ||
b4231d61 | 1652 | void kvm_release_page_dirty(struct page *page) |
8a7ae055 | 1653 | { |
a2766325 XG |
1654 | WARN_ON(is_error_page(page)); |
1655 | ||
35149e21 AL |
1656 | kvm_release_pfn_dirty(page_to_pfn(page)); |
1657 | } | |
1658 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); | |
1659 | ||
ba049e93 | 1660 | static void kvm_release_pfn_dirty(kvm_pfn_t pfn) |
35149e21 AL |
1661 | { |
1662 | kvm_set_pfn_dirty(pfn); | |
1663 | kvm_release_pfn_clean(pfn); | |
1664 | } | |
35149e21 | 1665 | |
ba049e93 | 1666 | void kvm_set_pfn_dirty(kvm_pfn_t pfn) |
35149e21 | 1667 | { |
bf4bea8e | 1668 | if (!kvm_is_reserved_pfn(pfn)) { |
2e2e3738 | 1669 | struct page *page = pfn_to_page(pfn); |
f95ef0cd | 1670 | |
2e2e3738 AL |
1671 | if (!PageReserved(page)) |
1672 | SetPageDirty(page); | |
1673 | } | |
8a7ae055 | 1674 | } |
35149e21 AL |
1675 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
1676 | ||
ba049e93 | 1677 | void kvm_set_pfn_accessed(kvm_pfn_t pfn) |
35149e21 | 1678 | { |
bf4bea8e | 1679 | if (!kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1680 | mark_page_accessed(pfn_to_page(pfn)); |
35149e21 AL |
1681 | } |
1682 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); | |
1683 | ||
ba049e93 | 1684 | void kvm_get_pfn(kvm_pfn_t pfn) |
35149e21 | 1685 | { |
bf4bea8e | 1686 | if (!kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1687 | get_page(pfn_to_page(pfn)); |
35149e21 AL |
1688 | } |
1689 | EXPORT_SYMBOL_GPL(kvm_get_pfn); | |
8a7ae055 | 1690 | |
195aefde IE |
1691 | static int next_segment(unsigned long len, int offset) |
1692 | { | |
1693 | if (len > PAGE_SIZE - offset) | |
1694 | return PAGE_SIZE - offset; | |
1695 | else | |
1696 | return len; | |
1697 | } | |
1698 | ||
8e73485c PB |
1699 | static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, |
1700 | void *data, int offset, int len) | |
195aefde | 1701 | { |
e0506bcb IE |
1702 | int r; |
1703 | unsigned long addr; | |
195aefde | 1704 | |
8e73485c | 1705 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); |
e0506bcb IE |
1706 | if (kvm_is_error_hva(addr)) |
1707 | return -EFAULT; | |
3180a7fc | 1708 | r = __copy_from_user(data, (void __user *)addr + offset, len); |
e0506bcb | 1709 | if (r) |
195aefde | 1710 | return -EFAULT; |
195aefde IE |
1711 | return 0; |
1712 | } | |
8e73485c PB |
1713 | |
1714 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
1715 | int len) | |
1716 | { | |
1717 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1718 | ||
1719 | return __kvm_read_guest_page(slot, gfn, data, offset, len); | |
1720 | } | |
195aefde IE |
1721 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
1722 | ||
8e73485c PB |
1723 | int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, |
1724 | int offset, int len) | |
1725 | { | |
1726 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1727 | ||
1728 | return __kvm_read_guest_page(slot, gfn, data, offset, len); | |
1729 | } | |
1730 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); | |
1731 | ||
195aefde IE |
1732 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
1733 | { | |
1734 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1735 | int seg; | |
1736 | int offset = offset_in_page(gpa); | |
1737 | int ret; | |
1738 | ||
1739 | while ((seg = next_segment(len, offset)) != 0) { | |
1740 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
1741 | if (ret < 0) | |
1742 | return ret; | |
1743 | offset = 0; | |
1744 | len -= seg; | |
1745 | data += seg; | |
1746 | ++gfn; | |
1747 | } | |
1748 | return 0; | |
1749 | } | |
1750 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
1751 | ||
8e73485c | 1752 | int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len) |
7ec54588 | 1753 | { |
7ec54588 | 1754 | gfn_t gfn = gpa >> PAGE_SHIFT; |
8e73485c | 1755 | int seg; |
7ec54588 | 1756 | int offset = offset_in_page(gpa); |
8e73485c PB |
1757 | int ret; |
1758 | ||
1759 | while ((seg = next_segment(len, offset)) != 0) { | |
1760 | ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg); | |
1761 | if (ret < 0) | |
1762 | return ret; | |
1763 | offset = 0; | |
1764 | len -= seg; | |
1765 | data += seg; | |
1766 | ++gfn; | |
1767 | } | |
1768 | return 0; | |
1769 | } | |
1770 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest); | |
7ec54588 | 1771 | |
8e73485c PB |
1772 | static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
1773 | void *data, int offset, unsigned long len) | |
1774 | { | |
1775 | int r; | |
1776 | unsigned long addr; | |
1777 | ||
1778 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); | |
7ec54588 MT |
1779 | if (kvm_is_error_hva(addr)) |
1780 | return -EFAULT; | |
0aac03f0 | 1781 | pagefault_disable(); |
3180a7fc | 1782 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
0aac03f0 | 1783 | pagefault_enable(); |
7ec54588 MT |
1784 | if (r) |
1785 | return -EFAULT; | |
1786 | return 0; | |
1787 | } | |
7ec54588 | 1788 | |
8e73485c PB |
1789 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
1790 | unsigned long len) | |
1791 | { | |
1792 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1793 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1794 | int offset = offset_in_page(gpa); | |
1795 | ||
1796 | return __kvm_read_guest_atomic(slot, gfn, data, offset, len); | |
1797 | } | |
1798 | EXPORT_SYMBOL_GPL(kvm_read_guest_atomic); | |
1799 | ||
1800 | int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, | |
1801 | void *data, unsigned long len) | |
1802 | { | |
1803 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1804 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1805 | int offset = offset_in_page(gpa); | |
1806 | ||
1807 | return __kvm_read_guest_atomic(slot, gfn, data, offset, len); | |
1808 | } | |
1809 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); | |
1810 | ||
1811 | static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn, | |
1812 | const void *data, int offset, int len) | |
195aefde | 1813 | { |
e0506bcb IE |
1814 | int r; |
1815 | unsigned long addr; | |
195aefde | 1816 | |
251eb841 | 1817 | addr = gfn_to_hva_memslot(memslot, gfn); |
e0506bcb IE |
1818 | if (kvm_is_error_hva(addr)) |
1819 | return -EFAULT; | |
8b0cedff | 1820 | r = __copy_to_user((void __user *)addr + offset, data, len); |
e0506bcb | 1821 | if (r) |
195aefde | 1822 | return -EFAULT; |
bc009e43 | 1823 | mark_page_dirty_in_slot(memslot, gfn); |
195aefde IE |
1824 | return 0; |
1825 | } | |
8e73485c PB |
1826 | |
1827 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, | |
1828 | const void *data, int offset, int len) | |
1829 | { | |
1830 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1831 | ||
1832 | return __kvm_write_guest_page(slot, gfn, data, offset, len); | |
1833 | } | |
195aefde IE |
1834 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
1835 | ||
8e73485c PB |
1836 | int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
1837 | const void *data, int offset, int len) | |
1838 | { | |
1839 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1840 | ||
1841 | return __kvm_write_guest_page(slot, gfn, data, offset, len); | |
1842 | } | |
1843 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page); | |
1844 | ||
195aefde IE |
1845 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
1846 | unsigned long len) | |
1847 | { | |
1848 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1849 | int seg; | |
1850 | int offset = offset_in_page(gpa); | |
1851 | int ret; | |
1852 | ||
1853 | while ((seg = next_segment(len, offset)) != 0) { | |
1854 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
1855 | if (ret < 0) | |
1856 | return ret; | |
1857 | offset = 0; | |
1858 | len -= seg; | |
1859 | data += seg; | |
1860 | ++gfn; | |
1861 | } | |
1862 | return 0; | |
1863 | } | |
ff651cb6 | 1864 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
195aefde | 1865 | |
8e73485c PB |
1866 | int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, |
1867 | unsigned long len) | |
1868 | { | |
1869 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1870 | int seg; | |
1871 | int offset = offset_in_page(gpa); | |
1872 | int ret; | |
1873 | ||
1874 | while ((seg = next_segment(len, offset)) != 0) { | |
1875 | ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg); | |
1876 | if (ret < 0) | |
1877 | return ret; | |
1878 | offset = 0; | |
1879 | len -= seg; | |
1880 | data += seg; | |
1881 | ++gfn; | |
1882 | } | |
1883 | return 0; | |
1884 | } | |
1885 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest); | |
1886 | ||
49c7754c | 1887 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
8f964525 | 1888 | gpa_t gpa, unsigned long len) |
49c7754c GN |
1889 | { |
1890 | struct kvm_memslots *slots = kvm_memslots(kvm); | |
1891 | int offset = offset_in_page(gpa); | |
8f964525 AH |
1892 | gfn_t start_gfn = gpa >> PAGE_SHIFT; |
1893 | gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT; | |
1894 | gfn_t nr_pages_needed = end_gfn - start_gfn + 1; | |
1895 | gfn_t nr_pages_avail; | |
49c7754c GN |
1896 | |
1897 | ghc->gpa = gpa; | |
1898 | ghc->generation = slots->generation; | |
8f964525 AH |
1899 | ghc->len = len; |
1900 | ghc->memslot = gfn_to_memslot(kvm, start_gfn); | |
ca3f0874 RK |
1901 | ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, NULL); |
1902 | if (!kvm_is_error_hva(ghc->hva) && nr_pages_needed <= 1) { | |
49c7754c | 1903 | ghc->hva += offset; |
8f964525 AH |
1904 | } else { |
1905 | /* | |
1906 | * If the requested region crosses two memslots, we still | |
1907 | * verify that the entire region is valid here. | |
1908 | */ | |
1909 | while (start_gfn <= end_gfn) { | |
1910 | ghc->memslot = gfn_to_memslot(kvm, start_gfn); | |
1911 | ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, | |
1912 | &nr_pages_avail); | |
1913 | if (kvm_is_error_hva(ghc->hva)) | |
1914 | return -EFAULT; | |
1915 | start_gfn += nr_pages_avail; | |
1916 | } | |
1917 | /* Use the slow path for cross page reads and writes. */ | |
1918 | ghc->memslot = NULL; | |
1919 | } | |
49c7754c GN |
1920 | return 0; |
1921 | } | |
1922 | EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init); | |
1923 | ||
1924 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, | |
1925 | void *data, unsigned long len) | |
1926 | { | |
1927 | struct kvm_memslots *slots = kvm_memslots(kvm); | |
1928 | int r; | |
1929 | ||
8f964525 AH |
1930 | BUG_ON(len > ghc->len); |
1931 | ||
49c7754c | 1932 | if (slots->generation != ghc->generation) |
8f964525 AH |
1933 | kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len); |
1934 | ||
1935 | if (unlikely(!ghc->memslot)) | |
1936 | return kvm_write_guest(kvm, ghc->gpa, data, len); | |
49c7754c GN |
1937 | |
1938 | if (kvm_is_error_hva(ghc->hva)) | |
1939 | return -EFAULT; | |
1940 | ||
8b0cedff | 1941 | r = __copy_to_user((void __user *)ghc->hva, data, len); |
49c7754c GN |
1942 | if (r) |
1943 | return -EFAULT; | |
bc009e43 | 1944 | mark_page_dirty_in_slot(ghc->memslot, ghc->gpa >> PAGE_SHIFT); |
49c7754c GN |
1945 | |
1946 | return 0; | |
1947 | } | |
1948 | EXPORT_SYMBOL_GPL(kvm_write_guest_cached); | |
1949 | ||
e03b644f GN |
1950 | int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
1951 | void *data, unsigned long len) | |
1952 | { | |
1953 | struct kvm_memslots *slots = kvm_memslots(kvm); | |
1954 | int r; | |
1955 | ||
8f964525 AH |
1956 | BUG_ON(len > ghc->len); |
1957 | ||
e03b644f | 1958 | if (slots->generation != ghc->generation) |
8f964525 AH |
1959 | kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len); |
1960 | ||
1961 | if (unlikely(!ghc->memslot)) | |
1962 | return kvm_read_guest(kvm, ghc->gpa, data, len); | |
e03b644f GN |
1963 | |
1964 | if (kvm_is_error_hva(ghc->hva)) | |
1965 | return -EFAULT; | |
1966 | ||
1967 | r = __copy_from_user(data, (void __user *)ghc->hva, len); | |
1968 | if (r) | |
1969 | return -EFAULT; | |
1970 | ||
1971 | return 0; | |
1972 | } | |
1973 | EXPORT_SYMBOL_GPL(kvm_read_guest_cached); | |
1974 | ||
195aefde IE |
1975 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
1976 | { | |
8a3caa6d HC |
1977 | const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); |
1978 | ||
1979 | return kvm_write_guest_page(kvm, gfn, zero_page, offset, len); | |
195aefde IE |
1980 | } |
1981 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
1982 | ||
1983 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
1984 | { | |
1985 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1986 | int seg; | |
1987 | int offset = offset_in_page(gpa); | |
1988 | int ret; | |
1989 | ||
bfda0e84 | 1990 | while ((seg = next_segment(len, offset)) != 0) { |
195aefde IE |
1991 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
1992 | if (ret < 0) | |
1993 | return ret; | |
1994 | offset = 0; | |
1995 | len -= seg; | |
1996 | ++gfn; | |
1997 | } | |
1998 | return 0; | |
1999 | } | |
2000 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
2001 | ||
bc009e43 | 2002 | static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, |
7940876e | 2003 | gfn_t gfn) |
6aa8b732 | 2004 | { |
7e9d619d RR |
2005 | if (memslot && memslot->dirty_bitmap) { |
2006 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 2007 | |
b74ca3b3 | 2008 | set_bit_le(rel_gfn, memslot->dirty_bitmap); |
6aa8b732 AK |
2009 | } |
2010 | } | |
2011 | ||
49c7754c GN |
2012 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
2013 | { | |
2014 | struct kvm_memory_slot *memslot; | |
2015 | ||
2016 | memslot = gfn_to_memslot(kvm, gfn); | |
bc009e43 | 2017 | mark_page_dirty_in_slot(memslot, gfn); |
49c7754c | 2018 | } |
2ba9f0d8 | 2019 | EXPORT_SYMBOL_GPL(mark_page_dirty); |
49c7754c | 2020 | |
8e73485c PB |
2021 | void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) |
2022 | { | |
2023 | struct kvm_memory_slot *memslot; | |
2024 | ||
2025 | memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
2026 | mark_page_dirty_in_slot(memslot, gfn); | |
2027 | } | |
2028 | EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); | |
2029 | ||
aca6ff29 WL |
2030 | static void grow_halt_poll_ns(struct kvm_vcpu *vcpu) |
2031 | { | |
6b6de68c | 2032 | unsigned int old, val, grow; |
aca6ff29 | 2033 | |
2cbd7824 | 2034 | old = val = vcpu->halt_poll_ns; |
6b6de68c | 2035 | grow = READ_ONCE(halt_poll_ns_grow); |
aca6ff29 | 2036 | /* 10us base */ |
6b6de68c | 2037 | if (val == 0 && grow) |
aca6ff29 WL |
2038 | val = 10000; |
2039 | else | |
6b6de68c | 2040 | val *= grow; |
aca6ff29 | 2041 | |
313f636d DM |
2042 | if (val > halt_poll_ns) |
2043 | val = halt_poll_ns; | |
2044 | ||
aca6ff29 | 2045 | vcpu->halt_poll_ns = val; |
2cbd7824 | 2046 | trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old); |
aca6ff29 WL |
2047 | } |
2048 | ||
2049 | static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu) | |
2050 | { | |
6b6de68c | 2051 | unsigned int old, val, shrink; |
aca6ff29 | 2052 | |
2cbd7824 | 2053 | old = val = vcpu->halt_poll_ns; |
6b6de68c CB |
2054 | shrink = READ_ONCE(halt_poll_ns_shrink); |
2055 | if (shrink == 0) | |
aca6ff29 WL |
2056 | val = 0; |
2057 | else | |
6b6de68c | 2058 | val /= shrink; |
aca6ff29 WL |
2059 | |
2060 | vcpu->halt_poll_ns = val; | |
2cbd7824 | 2061 | trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old); |
aca6ff29 WL |
2062 | } |
2063 | ||
f7819512 PB |
2064 | static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) |
2065 | { | |
2066 | if (kvm_arch_vcpu_runnable(vcpu)) { | |
2067 | kvm_make_request(KVM_REQ_UNHALT, vcpu); | |
2068 | return -EINTR; | |
2069 | } | |
2070 | if (kvm_cpu_has_pending_timer(vcpu)) | |
2071 | return -EINTR; | |
2072 | if (signal_pending(current)) | |
2073 | return -EINTR; | |
2074 | ||
2075 | return 0; | |
2076 | } | |
2077 | ||
b6958ce4 ED |
2078 | /* |
2079 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
2080 | */ | |
8776e519 | 2081 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 2082 | { |
f7819512 | 2083 | ktime_t start, cur; |
8577370f | 2084 | DECLARE_SWAITQUEUE(wait); |
f7819512 | 2085 | bool waited = false; |
aca6ff29 | 2086 | u64 block_ns; |
f7819512 PB |
2087 | |
2088 | start = cur = ktime_get(); | |
19020f8a WL |
2089 | if (vcpu->halt_poll_ns) { |
2090 | ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns); | |
f95ef0cd | 2091 | |
62bea5bf | 2092 | ++vcpu->stat.halt_attempted_poll; |
f7819512 PB |
2093 | do { |
2094 | /* | |
2095 | * This sets KVM_REQ_UNHALT if an interrupt | |
2096 | * arrives. | |
2097 | */ | |
2098 | if (kvm_vcpu_check_block(vcpu) < 0) { | |
2099 | ++vcpu->stat.halt_successful_poll; | |
3491caf2 CB |
2100 | if (!vcpu_valid_wakeup(vcpu)) |
2101 | ++vcpu->stat.halt_poll_invalid; | |
f7819512 PB |
2102 | goto out; |
2103 | } | |
2104 | cur = ktime_get(); | |
2105 | } while (single_task_running() && ktime_before(cur, stop)); | |
2106 | } | |
e5c239cf | 2107 | |
3217f7c2 CD |
2108 | kvm_arch_vcpu_blocking(vcpu); |
2109 | ||
e5c239cf | 2110 | for (;;) { |
8577370f | 2111 | prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
e5c239cf | 2112 | |
f7819512 | 2113 | if (kvm_vcpu_check_block(vcpu) < 0) |
e5c239cf MT |
2114 | break; |
2115 | ||
f7819512 | 2116 | waited = true; |
b6958ce4 | 2117 | schedule(); |
b6958ce4 | 2118 | } |
d3bef15f | 2119 | |
8577370f | 2120 | finish_swait(&vcpu->wq, &wait); |
f7819512 PB |
2121 | cur = ktime_get(); |
2122 | ||
3217f7c2 | 2123 | kvm_arch_vcpu_unblocking(vcpu); |
f7819512 | 2124 | out: |
aca6ff29 WL |
2125 | block_ns = ktime_to_ns(cur) - ktime_to_ns(start); |
2126 | ||
2086d320 CB |
2127 | if (!vcpu_valid_wakeup(vcpu)) |
2128 | shrink_halt_poll_ns(vcpu); | |
2129 | else if (halt_poll_ns) { | |
aca6ff29 WL |
2130 | if (block_ns <= vcpu->halt_poll_ns) |
2131 | ; | |
2132 | /* we had a long block, shrink polling */ | |
2086d320 | 2133 | else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns) |
aca6ff29 WL |
2134 | shrink_halt_poll_ns(vcpu); |
2135 | /* we had a short halt and our poll time is too small */ | |
2136 | else if (vcpu->halt_poll_ns < halt_poll_ns && | |
2137 | block_ns < halt_poll_ns) | |
2138 | grow_halt_poll_ns(vcpu); | |
edb9272f WL |
2139 | } else |
2140 | vcpu->halt_poll_ns = 0; | |
aca6ff29 | 2141 | |
3491caf2 CB |
2142 | trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu)); |
2143 | kvm_arch_vcpu_block_finish(vcpu); | |
b6958ce4 | 2144 | } |
2ba9f0d8 | 2145 | EXPORT_SYMBOL_GPL(kvm_vcpu_block); |
b6958ce4 | 2146 | |
8c84780d | 2147 | #ifndef CONFIG_S390 |
dd1a4cc1 | 2148 | void kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) |
b6d33834 | 2149 | { |
8577370f | 2150 | struct swait_queue_head *wqp; |
b6d33834 CD |
2151 | |
2152 | wqp = kvm_arch_vcpu_wq(vcpu); | |
8577370f MT |
2153 | if (swait_active(wqp)) { |
2154 | swake_up(wqp); | |
b6d33834 CD |
2155 | ++vcpu->stat.halt_wakeup; |
2156 | } | |
2157 | ||
dd1a4cc1 RK |
2158 | } |
2159 | EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up); | |
2160 | ||
2161 | /* | |
2162 | * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. | |
2163 | */ | |
2164 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) | |
2165 | { | |
2166 | int me; | |
2167 | int cpu = vcpu->cpu; | |
2168 | ||
2169 | kvm_vcpu_wake_up(vcpu); | |
b6d33834 CD |
2170 | me = get_cpu(); |
2171 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) | |
2172 | if (kvm_arch_vcpu_should_kick(vcpu)) | |
2173 | smp_send_reschedule(cpu); | |
2174 | put_cpu(); | |
2175 | } | |
a20ed54d | 2176 | EXPORT_SYMBOL_GPL(kvm_vcpu_kick); |
8c84780d | 2177 | #endif /* !CONFIG_S390 */ |
b6d33834 | 2178 | |
fa93384f | 2179 | int kvm_vcpu_yield_to(struct kvm_vcpu *target) |
41628d33 KW |
2180 | { |
2181 | struct pid *pid; | |
2182 | struct task_struct *task = NULL; | |
fa93384f | 2183 | int ret = 0; |
41628d33 KW |
2184 | |
2185 | rcu_read_lock(); | |
2186 | pid = rcu_dereference(target->pid); | |
2187 | if (pid) | |
27fbe64b | 2188 | task = get_pid_task(pid, PIDTYPE_PID); |
41628d33 KW |
2189 | rcu_read_unlock(); |
2190 | if (!task) | |
c45c528e | 2191 | return ret; |
c45c528e | 2192 | ret = yield_to(task, 1); |
41628d33 | 2193 | put_task_struct(task); |
c45c528e R |
2194 | |
2195 | return ret; | |
41628d33 KW |
2196 | } |
2197 | EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to); | |
2198 | ||
06e48c51 R |
2199 | /* |
2200 | * Helper that checks whether a VCPU is eligible for directed yield. | |
2201 | * Most eligible candidate to yield is decided by following heuristics: | |
2202 | * | |
2203 | * (a) VCPU which has not done pl-exit or cpu relax intercepted recently | |
2204 | * (preempted lock holder), indicated by @in_spin_loop. | |
2205 | * Set at the beiginning and cleared at the end of interception/PLE handler. | |
2206 | * | |
2207 | * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get | |
2208 | * chance last time (mostly it has become eligible now since we have probably | |
2209 | * yielded to lockholder in last iteration. This is done by toggling | |
2210 | * @dy_eligible each time a VCPU checked for eligibility.) | |
2211 | * | |
2212 | * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding | |
2213 | * to preempted lock-holder could result in wrong VCPU selection and CPU | |
2214 | * burning. Giving priority for a potential lock-holder increases lock | |
2215 | * progress. | |
2216 | * | |
2217 | * Since algorithm is based on heuristics, accessing another VCPU data without | |
2218 | * locking does not harm. It may result in trying to yield to same VCPU, fail | |
2219 | * and continue with next VCPU and so on. | |
2220 | */ | |
7940876e | 2221 | static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu) |
06e48c51 | 2222 | { |
4a55dd72 | 2223 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
06e48c51 R |
2224 | bool eligible; |
2225 | ||
2226 | eligible = !vcpu->spin_loop.in_spin_loop || | |
34656113 | 2227 | vcpu->spin_loop.dy_eligible; |
06e48c51 R |
2228 | |
2229 | if (vcpu->spin_loop.in_spin_loop) | |
2230 | kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible); | |
2231 | ||
2232 | return eligible; | |
4a55dd72 SW |
2233 | #else |
2234 | return true; | |
06e48c51 | 2235 | #endif |
4a55dd72 | 2236 | } |
c45c528e | 2237 | |
217ece61 | 2238 | void kvm_vcpu_on_spin(struct kvm_vcpu *me) |
d255f4f2 | 2239 | { |
217ece61 RR |
2240 | struct kvm *kvm = me->kvm; |
2241 | struct kvm_vcpu *vcpu; | |
2242 | int last_boosted_vcpu = me->kvm->last_boosted_vcpu; | |
2243 | int yielded = 0; | |
c45c528e | 2244 | int try = 3; |
217ece61 RR |
2245 | int pass; |
2246 | int i; | |
d255f4f2 | 2247 | |
4c088493 | 2248 | kvm_vcpu_set_in_spin_loop(me, true); |
217ece61 RR |
2249 | /* |
2250 | * We boost the priority of a VCPU that is runnable but not | |
2251 | * currently running, because it got preempted by something | |
2252 | * else and called schedule in __vcpu_run. Hopefully that | |
2253 | * VCPU is holding the lock that we need and will release it. | |
2254 | * We approximate round-robin by starting at the last boosted VCPU. | |
2255 | */ | |
c45c528e | 2256 | for (pass = 0; pass < 2 && !yielded && try; pass++) { |
217ece61 | 2257 | kvm_for_each_vcpu(i, vcpu, kvm) { |
5cfc2aab | 2258 | if (!pass && i <= last_boosted_vcpu) { |
217ece61 RR |
2259 | i = last_boosted_vcpu; |
2260 | continue; | |
2261 | } else if (pass && i > last_boosted_vcpu) | |
2262 | break; | |
7bc7ae25 R |
2263 | if (!ACCESS_ONCE(vcpu->preempted)) |
2264 | continue; | |
217ece61 RR |
2265 | if (vcpu == me) |
2266 | continue; | |
8577370f | 2267 | if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu)) |
217ece61 | 2268 | continue; |
06e48c51 R |
2269 | if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) |
2270 | continue; | |
c45c528e R |
2271 | |
2272 | yielded = kvm_vcpu_yield_to(vcpu); | |
2273 | if (yielded > 0) { | |
217ece61 | 2274 | kvm->last_boosted_vcpu = i; |
217ece61 | 2275 | break; |
c45c528e R |
2276 | } else if (yielded < 0) { |
2277 | try--; | |
2278 | if (!try) | |
2279 | break; | |
217ece61 | 2280 | } |
217ece61 RR |
2281 | } |
2282 | } | |
4c088493 | 2283 | kvm_vcpu_set_in_spin_loop(me, false); |
06e48c51 R |
2284 | |
2285 | /* Ensure vcpu is not eligible during next spinloop */ | |
2286 | kvm_vcpu_set_dy_eligible(me, false); | |
d255f4f2 ZE |
2287 | } |
2288 | EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin); | |
2289 | ||
e4a533a4 | 2290 | static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
9a2bb7f4 AK |
2291 | { |
2292 | struct kvm_vcpu *vcpu = vma->vm_file->private_data; | |
9a2bb7f4 AK |
2293 | struct page *page; |
2294 | ||
e4a533a4 | 2295 | if (vmf->pgoff == 0) |
039576c0 | 2296 | page = virt_to_page(vcpu->run); |
09566765 | 2297 | #ifdef CONFIG_X86 |
e4a533a4 | 2298 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
ad312c7c | 2299 | page = virt_to_page(vcpu->arch.pio_data); |
5f94c174 LV |
2300 | #endif |
2301 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
2302 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) | |
2303 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); | |
09566765 | 2304 | #endif |
039576c0 | 2305 | else |
5b1c1493 | 2306 | return kvm_arch_vcpu_fault(vcpu, vmf); |
9a2bb7f4 | 2307 | get_page(page); |
e4a533a4 | 2308 | vmf->page = page; |
2309 | return 0; | |
9a2bb7f4 AK |
2310 | } |
2311 | ||
f0f37e2f | 2312 | static const struct vm_operations_struct kvm_vcpu_vm_ops = { |
e4a533a4 | 2313 | .fault = kvm_vcpu_fault, |
9a2bb7f4 AK |
2314 | }; |
2315 | ||
2316 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
2317 | { | |
2318 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
2319 | return 0; | |
2320 | } | |
2321 | ||
bccf2150 AK |
2322 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
2323 | { | |
2324 | struct kvm_vcpu *vcpu = filp->private_data; | |
2325 | ||
66c0b394 | 2326 | kvm_put_kvm(vcpu->kvm); |
bccf2150 AK |
2327 | return 0; |
2328 | } | |
2329 | ||
3d3aab1b | 2330 | static struct file_operations kvm_vcpu_fops = { |
bccf2150 AK |
2331 | .release = kvm_vcpu_release, |
2332 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
de8e5d74 | 2333 | #ifdef CONFIG_KVM_COMPAT |
1dda606c AG |
2334 | .compat_ioctl = kvm_vcpu_compat_ioctl, |
2335 | #endif | |
9a2bb7f4 | 2336 | .mmap = kvm_vcpu_mmap, |
6038f373 | 2337 | .llseek = noop_llseek, |
bccf2150 AK |
2338 | }; |
2339 | ||
2340 | /* | |
2341 | * Allocates an inode for the vcpu. | |
2342 | */ | |
2343 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
2344 | { | |
24009b05 | 2345 | return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC); |
bccf2150 AK |
2346 | } |
2347 | ||
c5ea7660 AK |
2348 | /* |
2349 | * Creates some virtual cpus. Good luck creating more than one. | |
2350 | */ | |
73880c80 | 2351 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) |
c5ea7660 AK |
2352 | { |
2353 | int r; | |
e09fefde | 2354 | struct kvm_vcpu *vcpu; |
c5ea7660 | 2355 | |
0b1b1dfd | 2356 | if (id >= KVM_MAX_VCPU_ID) |
338c7dba AH |
2357 | return -EINVAL; |
2358 | ||
6c7caebc PB |
2359 | mutex_lock(&kvm->lock); |
2360 | if (kvm->created_vcpus == KVM_MAX_VCPUS) { | |
2361 | mutex_unlock(&kvm->lock); | |
2362 | return -EINVAL; | |
2363 | } | |
2364 | ||
2365 | kvm->created_vcpus++; | |
2366 | mutex_unlock(&kvm->lock); | |
2367 | ||
73880c80 | 2368 | vcpu = kvm_arch_vcpu_create(kvm, id); |
6c7caebc PB |
2369 | if (IS_ERR(vcpu)) { |
2370 | r = PTR_ERR(vcpu); | |
2371 | goto vcpu_decrement; | |
2372 | } | |
c5ea7660 | 2373 | |
15ad7146 AK |
2374 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
2375 | ||
26e5215f AK |
2376 | r = kvm_arch_vcpu_setup(vcpu); |
2377 | if (r) | |
d780592b | 2378 | goto vcpu_destroy; |
26e5215f | 2379 | |
11ec2804 | 2380 | mutex_lock(&kvm->lock); |
e09fefde DH |
2381 | if (kvm_get_vcpu_by_id(kvm, id)) { |
2382 | r = -EEXIST; | |
2383 | goto unlock_vcpu_destroy; | |
2384 | } | |
73880c80 GN |
2385 | |
2386 | BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); | |
c5ea7660 | 2387 | |
fb3f0f51 | 2388 | /* Now it's all set up, let userspace reach it */ |
66c0b394 | 2389 | kvm_get_kvm(kvm); |
bccf2150 | 2390 | r = create_vcpu_fd(vcpu); |
73880c80 GN |
2391 | if (r < 0) { |
2392 | kvm_put_kvm(kvm); | |
d780592b | 2393 | goto unlock_vcpu_destroy; |
73880c80 GN |
2394 | } |
2395 | ||
2396 | kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; | |
dd489240 PB |
2397 | |
2398 | /* | |
2399 | * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus | |
2400 | * before kvm->online_vcpu's incremented value. | |
2401 | */ | |
73880c80 GN |
2402 | smp_wmb(); |
2403 | atomic_inc(&kvm->online_vcpus); | |
2404 | ||
73880c80 | 2405 | mutex_unlock(&kvm->lock); |
42897d86 | 2406 | kvm_arch_vcpu_postcreate(vcpu); |
fb3f0f51 | 2407 | return r; |
39c3b86e | 2408 | |
d780592b | 2409 | unlock_vcpu_destroy: |
7d8fece6 | 2410 | mutex_unlock(&kvm->lock); |
d780592b | 2411 | vcpu_destroy: |
d40ccc62 | 2412 | kvm_arch_vcpu_destroy(vcpu); |
6c7caebc PB |
2413 | vcpu_decrement: |
2414 | mutex_lock(&kvm->lock); | |
2415 | kvm->created_vcpus--; | |
2416 | mutex_unlock(&kvm->lock); | |
c5ea7660 AK |
2417 | return r; |
2418 | } | |
2419 | ||
1961d276 AK |
2420 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
2421 | { | |
2422 | if (sigset) { | |
2423 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
2424 | vcpu->sigset_active = 1; | |
2425 | vcpu->sigset = *sigset; | |
2426 | } else | |
2427 | vcpu->sigset_active = 0; | |
2428 | return 0; | |
2429 | } | |
2430 | ||
bccf2150 AK |
2431 | static long kvm_vcpu_ioctl(struct file *filp, |
2432 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 2433 | { |
bccf2150 | 2434 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 2435 | void __user *argp = (void __user *)arg; |
313a3dc7 | 2436 | int r; |
fa3795a7 DH |
2437 | struct kvm_fpu *fpu = NULL; |
2438 | struct kvm_sregs *kvm_sregs = NULL; | |
6aa8b732 | 2439 | |
6d4e4c4f AK |
2440 | if (vcpu->kvm->mm != current->mm) |
2441 | return -EIO; | |
2122ff5e | 2442 | |
2ea75be3 DM |
2443 | if (unlikely(_IOC_TYPE(ioctl) != KVMIO)) |
2444 | return -EINVAL; | |
2445 | ||
2f4d9b54 | 2446 | #if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS) |
2122ff5e AK |
2447 | /* |
2448 | * Special cases: vcpu ioctls that are asynchronous to vcpu execution, | |
2449 | * so vcpu_load() would break it. | |
2450 | */ | |
47b43c52 | 2451 | if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_S390_IRQ || ioctl == KVM_INTERRUPT) |
2122ff5e AK |
2452 | return kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
2453 | #endif | |
2454 | ||
2455 | ||
9fc77441 MT |
2456 | r = vcpu_load(vcpu); |
2457 | if (r) | |
2458 | return r; | |
6aa8b732 | 2459 | switch (ioctl) { |
9a2bb7f4 | 2460 | case KVM_RUN: |
f0fe5108 AK |
2461 | r = -EINVAL; |
2462 | if (arg) | |
2463 | goto out; | |
7a72f7a1 CB |
2464 | if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) { |
2465 | /* The thread running this VCPU changed. */ | |
2466 | struct pid *oldpid = vcpu->pid; | |
2467 | struct pid *newpid = get_task_pid(current, PIDTYPE_PID); | |
f95ef0cd | 2468 | |
7a72f7a1 CB |
2469 | rcu_assign_pointer(vcpu->pid, newpid); |
2470 | if (oldpid) | |
2471 | synchronize_rcu(); | |
2472 | put_pid(oldpid); | |
2473 | } | |
b6c7a5dc | 2474 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
64be5007 | 2475 | trace_kvm_userspace_exit(vcpu->run->exit_reason, r); |
6aa8b732 | 2476 | break; |
6aa8b732 | 2477 | case KVM_GET_REGS: { |
3e4bb3ac | 2478 | struct kvm_regs *kvm_regs; |
6aa8b732 | 2479 | |
3e4bb3ac XZ |
2480 | r = -ENOMEM; |
2481 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL); | |
2482 | if (!kvm_regs) | |
6aa8b732 | 2483 | goto out; |
3e4bb3ac XZ |
2484 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
2485 | if (r) | |
2486 | goto out_free1; | |
6aa8b732 | 2487 | r = -EFAULT; |
3e4bb3ac XZ |
2488 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
2489 | goto out_free1; | |
6aa8b732 | 2490 | r = 0; |
3e4bb3ac XZ |
2491 | out_free1: |
2492 | kfree(kvm_regs); | |
6aa8b732 AK |
2493 | break; |
2494 | } | |
2495 | case KVM_SET_REGS: { | |
3e4bb3ac | 2496 | struct kvm_regs *kvm_regs; |
6aa8b732 | 2497 | |
3e4bb3ac | 2498 | r = -ENOMEM; |
ff5c2c03 SL |
2499 | kvm_regs = memdup_user(argp, sizeof(*kvm_regs)); |
2500 | if (IS_ERR(kvm_regs)) { | |
2501 | r = PTR_ERR(kvm_regs); | |
6aa8b732 | 2502 | goto out; |
ff5c2c03 | 2503 | } |
3e4bb3ac | 2504 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
3e4bb3ac | 2505 | kfree(kvm_regs); |
6aa8b732 AK |
2506 | break; |
2507 | } | |
2508 | case KVM_GET_SREGS: { | |
fa3795a7 DH |
2509 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL); |
2510 | r = -ENOMEM; | |
2511 | if (!kvm_sregs) | |
2512 | goto out; | |
2513 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); | |
6aa8b732 AK |
2514 | if (r) |
2515 | goto out; | |
2516 | r = -EFAULT; | |
fa3795a7 | 2517 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
6aa8b732 AK |
2518 | goto out; |
2519 | r = 0; | |
2520 | break; | |
2521 | } | |
2522 | case KVM_SET_SREGS: { | |
ff5c2c03 SL |
2523 | kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs)); |
2524 | if (IS_ERR(kvm_sregs)) { | |
2525 | r = PTR_ERR(kvm_sregs); | |
18595411 | 2526 | kvm_sregs = NULL; |
6aa8b732 | 2527 | goto out; |
ff5c2c03 | 2528 | } |
fa3795a7 | 2529 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
6aa8b732 AK |
2530 | break; |
2531 | } | |
62d9f0db MT |
2532 | case KVM_GET_MP_STATE: { |
2533 | struct kvm_mp_state mp_state; | |
2534 | ||
2535 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); | |
2536 | if (r) | |
2537 | goto out; | |
2538 | r = -EFAULT; | |
893bdbf1 | 2539 | if (copy_to_user(argp, &mp_state, sizeof(mp_state))) |
62d9f0db MT |
2540 | goto out; |
2541 | r = 0; | |
2542 | break; | |
2543 | } | |
2544 | case KVM_SET_MP_STATE: { | |
2545 | struct kvm_mp_state mp_state; | |
2546 | ||
2547 | r = -EFAULT; | |
893bdbf1 | 2548 | if (copy_from_user(&mp_state, argp, sizeof(mp_state))) |
62d9f0db MT |
2549 | goto out; |
2550 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); | |
62d9f0db MT |
2551 | break; |
2552 | } | |
6aa8b732 AK |
2553 | case KVM_TRANSLATE: { |
2554 | struct kvm_translation tr; | |
2555 | ||
2556 | r = -EFAULT; | |
893bdbf1 | 2557 | if (copy_from_user(&tr, argp, sizeof(tr))) |
6aa8b732 | 2558 | goto out; |
8b006791 | 2559 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
2560 | if (r) |
2561 | goto out; | |
2562 | r = -EFAULT; | |
893bdbf1 | 2563 | if (copy_to_user(argp, &tr, sizeof(tr))) |
6aa8b732 AK |
2564 | goto out; |
2565 | r = 0; | |
2566 | break; | |
2567 | } | |
d0bfb940 JK |
2568 | case KVM_SET_GUEST_DEBUG: { |
2569 | struct kvm_guest_debug dbg; | |
6aa8b732 AK |
2570 | |
2571 | r = -EFAULT; | |
893bdbf1 | 2572 | if (copy_from_user(&dbg, argp, sizeof(dbg))) |
6aa8b732 | 2573 | goto out; |
d0bfb940 | 2574 | r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg); |
6aa8b732 AK |
2575 | break; |
2576 | } | |
1961d276 AK |
2577 | case KVM_SET_SIGNAL_MASK: { |
2578 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2579 | struct kvm_signal_mask kvm_sigmask; | |
2580 | sigset_t sigset, *p; | |
2581 | ||
2582 | p = NULL; | |
2583 | if (argp) { | |
2584 | r = -EFAULT; | |
2585 | if (copy_from_user(&kvm_sigmask, argp, | |
893bdbf1 | 2586 | sizeof(kvm_sigmask))) |
1961d276 AK |
2587 | goto out; |
2588 | r = -EINVAL; | |
893bdbf1 | 2589 | if (kvm_sigmask.len != sizeof(sigset)) |
1961d276 AK |
2590 | goto out; |
2591 | r = -EFAULT; | |
2592 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
893bdbf1 | 2593 | sizeof(sigset))) |
1961d276 AK |
2594 | goto out; |
2595 | p = &sigset; | |
2596 | } | |
376d41ff | 2597 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, p); |
1961d276 AK |
2598 | break; |
2599 | } | |
b8836737 | 2600 | case KVM_GET_FPU: { |
fa3795a7 DH |
2601 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL); |
2602 | r = -ENOMEM; | |
2603 | if (!fpu) | |
2604 | goto out; | |
2605 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); | |
b8836737 AK |
2606 | if (r) |
2607 | goto out; | |
2608 | r = -EFAULT; | |
fa3795a7 | 2609 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
b8836737 AK |
2610 | goto out; |
2611 | r = 0; | |
2612 | break; | |
2613 | } | |
2614 | case KVM_SET_FPU: { | |
ff5c2c03 SL |
2615 | fpu = memdup_user(argp, sizeof(*fpu)); |
2616 | if (IS_ERR(fpu)) { | |
2617 | r = PTR_ERR(fpu); | |
18595411 | 2618 | fpu = NULL; |
b8836737 | 2619 | goto out; |
ff5c2c03 | 2620 | } |
fa3795a7 | 2621 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
b8836737 AK |
2622 | break; |
2623 | } | |
bccf2150 | 2624 | default: |
313a3dc7 | 2625 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
2626 | } |
2627 | out: | |
2122ff5e | 2628 | vcpu_put(vcpu); |
fa3795a7 DH |
2629 | kfree(fpu); |
2630 | kfree(kvm_sregs); | |
bccf2150 AK |
2631 | return r; |
2632 | } | |
2633 | ||
de8e5d74 | 2634 | #ifdef CONFIG_KVM_COMPAT |
1dda606c AG |
2635 | static long kvm_vcpu_compat_ioctl(struct file *filp, |
2636 | unsigned int ioctl, unsigned long arg) | |
2637 | { | |
2638 | struct kvm_vcpu *vcpu = filp->private_data; | |
2639 | void __user *argp = compat_ptr(arg); | |
2640 | int r; | |
2641 | ||
2642 | if (vcpu->kvm->mm != current->mm) | |
2643 | return -EIO; | |
2644 | ||
2645 | switch (ioctl) { | |
2646 | case KVM_SET_SIGNAL_MASK: { | |
2647 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2648 | struct kvm_signal_mask kvm_sigmask; | |
2649 | compat_sigset_t csigset; | |
2650 | sigset_t sigset; | |
2651 | ||
2652 | if (argp) { | |
2653 | r = -EFAULT; | |
2654 | if (copy_from_user(&kvm_sigmask, argp, | |
893bdbf1 | 2655 | sizeof(kvm_sigmask))) |
1dda606c AG |
2656 | goto out; |
2657 | r = -EINVAL; | |
893bdbf1 | 2658 | if (kvm_sigmask.len != sizeof(csigset)) |
1dda606c AG |
2659 | goto out; |
2660 | r = -EFAULT; | |
2661 | if (copy_from_user(&csigset, sigmask_arg->sigset, | |
893bdbf1 | 2662 | sizeof(csigset))) |
1dda606c | 2663 | goto out; |
760a9a30 AC |
2664 | sigset_from_compat(&sigset, &csigset); |
2665 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); | |
2666 | } else | |
2667 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL); | |
1dda606c AG |
2668 | break; |
2669 | } | |
2670 | default: | |
2671 | r = kvm_vcpu_ioctl(filp, ioctl, arg); | |
2672 | } | |
2673 | ||
2674 | out: | |
2675 | return r; | |
2676 | } | |
2677 | #endif | |
2678 | ||
852b6d57 SW |
2679 | static int kvm_device_ioctl_attr(struct kvm_device *dev, |
2680 | int (*accessor)(struct kvm_device *dev, | |
2681 | struct kvm_device_attr *attr), | |
2682 | unsigned long arg) | |
2683 | { | |
2684 | struct kvm_device_attr attr; | |
2685 | ||
2686 | if (!accessor) | |
2687 | return -EPERM; | |
2688 | ||
2689 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
2690 | return -EFAULT; | |
2691 | ||
2692 | return accessor(dev, &attr); | |
2693 | } | |
2694 | ||
2695 | static long kvm_device_ioctl(struct file *filp, unsigned int ioctl, | |
2696 | unsigned long arg) | |
2697 | { | |
2698 | struct kvm_device *dev = filp->private_data; | |
2699 | ||
2700 | switch (ioctl) { | |
2701 | case KVM_SET_DEVICE_ATTR: | |
2702 | return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg); | |
2703 | case KVM_GET_DEVICE_ATTR: | |
2704 | return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg); | |
2705 | case KVM_HAS_DEVICE_ATTR: | |
2706 | return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg); | |
2707 | default: | |
2708 | if (dev->ops->ioctl) | |
2709 | return dev->ops->ioctl(dev, ioctl, arg); | |
2710 | ||
2711 | return -ENOTTY; | |
2712 | } | |
2713 | } | |
2714 | ||
852b6d57 SW |
2715 | static int kvm_device_release(struct inode *inode, struct file *filp) |
2716 | { | |
2717 | struct kvm_device *dev = filp->private_data; | |
2718 | struct kvm *kvm = dev->kvm; | |
2719 | ||
852b6d57 SW |
2720 | kvm_put_kvm(kvm); |
2721 | return 0; | |
2722 | } | |
2723 | ||
2724 | static const struct file_operations kvm_device_fops = { | |
2725 | .unlocked_ioctl = kvm_device_ioctl, | |
de8e5d74 | 2726 | #ifdef CONFIG_KVM_COMPAT |
db6ae615 SW |
2727 | .compat_ioctl = kvm_device_ioctl, |
2728 | #endif | |
852b6d57 SW |
2729 | .release = kvm_device_release, |
2730 | }; | |
2731 | ||
2732 | struct kvm_device *kvm_device_from_filp(struct file *filp) | |
2733 | { | |
2734 | if (filp->f_op != &kvm_device_fops) | |
2735 | return NULL; | |
2736 | ||
2737 | return filp->private_data; | |
2738 | } | |
2739 | ||
d60eacb0 | 2740 | static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = { |
5df554ad | 2741 | #ifdef CONFIG_KVM_MPIC |
d60eacb0 WD |
2742 | [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops, |
2743 | [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops, | |
5975a2e0 | 2744 | #endif |
d60eacb0 | 2745 | |
5975a2e0 | 2746 | #ifdef CONFIG_KVM_XICS |
d60eacb0 | 2747 | [KVM_DEV_TYPE_XICS] = &kvm_xics_ops, |
ec53500f | 2748 | #endif |
d60eacb0 WD |
2749 | }; |
2750 | ||
2751 | int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type) | |
2752 | { | |
2753 | if (type >= ARRAY_SIZE(kvm_device_ops_table)) | |
2754 | return -ENOSPC; | |
2755 | ||
2756 | if (kvm_device_ops_table[type] != NULL) | |
2757 | return -EEXIST; | |
2758 | ||
2759 | kvm_device_ops_table[type] = ops; | |
2760 | return 0; | |
2761 | } | |
2762 | ||
571ee1b6 WL |
2763 | void kvm_unregister_device_ops(u32 type) |
2764 | { | |
2765 | if (kvm_device_ops_table[type] != NULL) | |
2766 | kvm_device_ops_table[type] = NULL; | |
2767 | } | |
2768 | ||
852b6d57 SW |
2769 | static int kvm_ioctl_create_device(struct kvm *kvm, |
2770 | struct kvm_create_device *cd) | |
2771 | { | |
2772 | struct kvm_device_ops *ops = NULL; | |
2773 | struct kvm_device *dev; | |
2774 | bool test = cd->flags & KVM_CREATE_DEVICE_TEST; | |
2775 | int ret; | |
2776 | ||
d60eacb0 WD |
2777 | if (cd->type >= ARRAY_SIZE(kvm_device_ops_table)) |
2778 | return -ENODEV; | |
2779 | ||
2780 | ops = kvm_device_ops_table[cd->type]; | |
2781 | if (ops == NULL) | |
852b6d57 | 2782 | return -ENODEV; |
852b6d57 SW |
2783 | |
2784 | if (test) | |
2785 | return 0; | |
2786 | ||
2787 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
2788 | if (!dev) | |
2789 | return -ENOMEM; | |
2790 | ||
2791 | dev->ops = ops; | |
2792 | dev->kvm = kvm; | |
852b6d57 SW |
2793 | |
2794 | ret = ops->create(dev, cd->type); | |
2795 | if (ret < 0) { | |
2796 | kfree(dev); | |
2797 | return ret; | |
2798 | } | |
2799 | ||
24009b05 | 2800 | ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); |
852b6d57 SW |
2801 | if (ret < 0) { |
2802 | ops->destroy(dev); | |
2803 | return ret; | |
2804 | } | |
2805 | ||
07f0a7bd | 2806 | list_add(&dev->vm_node, &kvm->devices); |
852b6d57 SW |
2807 | kvm_get_kvm(kvm); |
2808 | cd->fd = ret; | |
2809 | return 0; | |
2810 | } | |
2811 | ||
92b591a4 AG |
2812 | static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) |
2813 | { | |
2814 | switch (arg) { | |
2815 | case KVM_CAP_USER_MEMORY: | |
2816 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: | |
2817 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: | |
92b591a4 AG |
2818 | case KVM_CAP_INTERNAL_ERROR_DATA: |
2819 | #ifdef CONFIG_HAVE_KVM_MSI | |
2820 | case KVM_CAP_SIGNAL_MSI: | |
2821 | #endif | |
297e2105 | 2822 | #ifdef CONFIG_HAVE_KVM_IRQFD |
dc9be0fa | 2823 | case KVM_CAP_IRQFD: |
92b591a4 AG |
2824 | case KVM_CAP_IRQFD_RESAMPLE: |
2825 | #endif | |
e9ea5069 | 2826 | case KVM_CAP_IOEVENTFD_ANY_LENGTH: |
92b591a4 AG |
2827 | case KVM_CAP_CHECK_EXTENSION_VM: |
2828 | return 1; | |
2829 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING | |
2830 | case KVM_CAP_IRQ_ROUTING: | |
2831 | return KVM_MAX_IRQ_ROUTES; | |
f481b069 PB |
2832 | #endif |
2833 | #if KVM_ADDRESS_SPACE_NUM > 1 | |
2834 | case KVM_CAP_MULTI_ADDRESS_SPACE: | |
2835 | return KVM_ADDRESS_SPACE_NUM; | |
92b591a4 | 2836 | #endif |
0b1b1dfd GK |
2837 | case KVM_CAP_MAX_VCPU_ID: |
2838 | return KVM_MAX_VCPU_ID; | |
92b591a4 AG |
2839 | default: |
2840 | break; | |
2841 | } | |
2842 | return kvm_vm_ioctl_check_extension(kvm, arg); | |
2843 | } | |
2844 | ||
bccf2150 AK |
2845 | static long kvm_vm_ioctl(struct file *filp, |
2846 | unsigned int ioctl, unsigned long arg) | |
2847 | { | |
2848 | struct kvm *kvm = filp->private_data; | |
2849 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 2850 | int r; |
bccf2150 | 2851 | |
6d4e4c4f AK |
2852 | if (kvm->mm != current->mm) |
2853 | return -EIO; | |
bccf2150 AK |
2854 | switch (ioctl) { |
2855 | case KVM_CREATE_VCPU: | |
2856 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
bccf2150 | 2857 | break; |
6fc138d2 IE |
2858 | case KVM_SET_USER_MEMORY_REGION: { |
2859 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
2860 | ||
2861 | r = -EFAULT; | |
2862 | if (copy_from_user(&kvm_userspace_mem, argp, | |
893bdbf1 | 2863 | sizeof(kvm_userspace_mem))) |
6fc138d2 IE |
2864 | goto out; |
2865 | ||
47ae31e2 | 2866 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem); |
6aa8b732 AK |
2867 | break; |
2868 | } | |
2869 | case KVM_GET_DIRTY_LOG: { | |
2870 | struct kvm_dirty_log log; | |
2871 | ||
2872 | r = -EFAULT; | |
893bdbf1 | 2873 | if (copy_from_user(&log, argp, sizeof(log))) |
6aa8b732 | 2874 | goto out; |
2c6f5df9 | 2875 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
2876 | break; |
2877 | } | |
5f94c174 LV |
2878 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
2879 | case KVM_REGISTER_COALESCED_MMIO: { | |
2880 | struct kvm_coalesced_mmio_zone zone; | |
f95ef0cd | 2881 | |
5f94c174 | 2882 | r = -EFAULT; |
893bdbf1 | 2883 | if (copy_from_user(&zone, argp, sizeof(zone))) |
5f94c174 | 2884 | goto out; |
5f94c174 | 2885 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
5f94c174 LV |
2886 | break; |
2887 | } | |
2888 | case KVM_UNREGISTER_COALESCED_MMIO: { | |
2889 | struct kvm_coalesced_mmio_zone zone; | |
f95ef0cd | 2890 | |
5f94c174 | 2891 | r = -EFAULT; |
893bdbf1 | 2892 | if (copy_from_user(&zone, argp, sizeof(zone))) |
5f94c174 | 2893 | goto out; |
5f94c174 | 2894 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
5f94c174 LV |
2895 | break; |
2896 | } | |
2897 | #endif | |
721eecbf GH |
2898 | case KVM_IRQFD: { |
2899 | struct kvm_irqfd data; | |
2900 | ||
2901 | r = -EFAULT; | |
893bdbf1 | 2902 | if (copy_from_user(&data, argp, sizeof(data))) |
721eecbf | 2903 | goto out; |
d4db2935 | 2904 | r = kvm_irqfd(kvm, &data); |
721eecbf GH |
2905 | break; |
2906 | } | |
d34e6b17 GH |
2907 | case KVM_IOEVENTFD: { |
2908 | struct kvm_ioeventfd data; | |
2909 | ||
2910 | r = -EFAULT; | |
893bdbf1 | 2911 | if (copy_from_user(&data, argp, sizeof(data))) |
d34e6b17 GH |
2912 | goto out; |
2913 | r = kvm_ioeventfd(kvm, &data); | |
2914 | break; | |
2915 | } | |
07975ad3 JK |
2916 | #ifdef CONFIG_HAVE_KVM_MSI |
2917 | case KVM_SIGNAL_MSI: { | |
2918 | struct kvm_msi msi; | |
2919 | ||
2920 | r = -EFAULT; | |
893bdbf1 | 2921 | if (copy_from_user(&msi, argp, sizeof(msi))) |
07975ad3 JK |
2922 | goto out; |
2923 | r = kvm_send_userspace_msi(kvm, &msi); | |
2924 | break; | |
2925 | } | |
23d43cf9 CD |
2926 | #endif |
2927 | #ifdef __KVM_HAVE_IRQ_LINE | |
2928 | case KVM_IRQ_LINE_STATUS: | |
2929 | case KVM_IRQ_LINE: { | |
2930 | struct kvm_irq_level irq_event; | |
2931 | ||
2932 | r = -EFAULT; | |
893bdbf1 | 2933 | if (copy_from_user(&irq_event, argp, sizeof(irq_event))) |
23d43cf9 CD |
2934 | goto out; |
2935 | ||
aa2fbe6d YZ |
2936 | r = kvm_vm_ioctl_irq_line(kvm, &irq_event, |
2937 | ioctl == KVM_IRQ_LINE_STATUS); | |
23d43cf9 CD |
2938 | if (r) |
2939 | goto out; | |
2940 | ||
2941 | r = -EFAULT; | |
2942 | if (ioctl == KVM_IRQ_LINE_STATUS) { | |
893bdbf1 | 2943 | if (copy_to_user(argp, &irq_event, sizeof(irq_event))) |
23d43cf9 CD |
2944 | goto out; |
2945 | } | |
2946 | ||
2947 | r = 0; | |
2948 | break; | |
2949 | } | |
73880c80 | 2950 | #endif |
aa8d5944 AG |
2951 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
2952 | case KVM_SET_GSI_ROUTING: { | |
2953 | struct kvm_irq_routing routing; | |
2954 | struct kvm_irq_routing __user *urouting; | |
f8c1b85b | 2955 | struct kvm_irq_routing_entry *entries = NULL; |
aa8d5944 AG |
2956 | |
2957 | r = -EFAULT; | |
2958 | if (copy_from_user(&routing, argp, sizeof(routing))) | |
2959 | goto out; | |
2960 | r = -EINVAL; | |
2961 | if (routing.nr >= KVM_MAX_IRQ_ROUTES) | |
2962 | goto out; | |
2963 | if (routing.flags) | |
2964 | goto out; | |
f8c1b85b PB |
2965 | if (routing.nr) { |
2966 | r = -ENOMEM; | |
2967 | entries = vmalloc(routing.nr * sizeof(*entries)); | |
2968 | if (!entries) | |
2969 | goto out; | |
2970 | r = -EFAULT; | |
2971 | urouting = argp; | |
2972 | if (copy_from_user(entries, urouting->entries, | |
2973 | routing.nr * sizeof(*entries))) | |
2974 | goto out_free_irq_routing; | |
2975 | } | |
aa8d5944 AG |
2976 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
2977 | routing.flags); | |
a642a175 | 2978 | out_free_irq_routing: |
aa8d5944 AG |
2979 | vfree(entries); |
2980 | break; | |
2981 | } | |
2982 | #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ | |
852b6d57 SW |
2983 | case KVM_CREATE_DEVICE: { |
2984 | struct kvm_create_device cd; | |
2985 | ||
2986 | r = -EFAULT; | |
2987 | if (copy_from_user(&cd, argp, sizeof(cd))) | |
2988 | goto out; | |
2989 | ||
2990 | r = kvm_ioctl_create_device(kvm, &cd); | |
2991 | if (r) | |
2992 | goto out; | |
2993 | ||
2994 | r = -EFAULT; | |
2995 | if (copy_to_user(argp, &cd, sizeof(cd))) | |
2996 | goto out; | |
2997 | ||
2998 | r = 0; | |
2999 | break; | |
3000 | } | |
92b591a4 AG |
3001 | case KVM_CHECK_EXTENSION: |
3002 | r = kvm_vm_ioctl_check_extension_generic(kvm, arg); | |
3003 | break; | |
f17abe9a | 3004 | default: |
1fe779f8 | 3005 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
3006 | } |
3007 | out: | |
3008 | return r; | |
3009 | } | |
3010 | ||
de8e5d74 | 3011 | #ifdef CONFIG_KVM_COMPAT |
6ff5894c AB |
3012 | struct compat_kvm_dirty_log { |
3013 | __u32 slot; | |
3014 | __u32 padding1; | |
3015 | union { | |
3016 | compat_uptr_t dirty_bitmap; /* one bit per page */ | |
3017 | __u64 padding2; | |
3018 | }; | |
3019 | }; | |
3020 | ||
3021 | static long kvm_vm_compat_ioctl(struct file *filp, | |
3022 | unsigned int ioctl, unsigned long arg) | |
3023 | { | |
3024 | struct kvm *kvm = filp->private_data; | |
3025 | int r; | |
3026 | ||
3027 | if (kvm->mm != current->mm) | |
3028 | return -EIO; | |
3029 | switch (ioctl) { | |
3030 | case KVM_GET_DIRTY_LOG: { | |
3031 | struct compat_kvm_dirty_log compat_log; | |
3032 | struct kvm_dirty_log log; | |
3033 | ||
3034 | r = -EFAULT; | |
3035 | if (copy_from_user(&compat_log, (void __user *)arg, | |
3036 | sizeof(compat_log))) | |
3037 | goto out; | |
3038 | log.slot = compat_log.slot; | |
3039 | log.padding1 = compat_log.padding1; | |
3040 | log.padding2 = compat_log.padding2; | |
3041 | log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap); | |
3042 | ||
3043 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); | |
6ff5894c AB |
3044 | break; |
3045 | } | |
3046 | default: | |
3047 | r = kvm_vm_ioctl(filp, ioctl, arg); | |
3048 | } | |
3049 | ||
3050 | out: | |
3051 | return r; | |
3052 | } | |
3053 | #endif | |
3054 | ||
3d3aab1b | 3055 | static struct file_operations kvm_vm_fops = { |
f17abe9a AK |
3056 | .release = kvm_vm_release, |
3057 | .unlocked_ioctl = kvm_vm_ioctl, | |
de8e5d74 | 3058 | #ifdef CONFIG_KVM_COMPAT |
6ff5894c AB |
3059 | .compat_ioctl = kvm_vm_compat_ioctl, |
3060 | #endif | |
6038f373 | 3061 | .llseek = noop_llseek, |
f17abe9a AK |
3062 | }; |
3063 | ||
e08b9637 | 3064 | static int kvm_dev_ioctl_create_vm(unsigned long type) |
f17abe9a | 3065 | { |
aac87636 | 3066 | int r; |
f17abe9a AK |
3067 | struct kvm *kvm; |
3068 | ||
e08b9637 | 3069 | kvm = kvm_create_vm(type); |
d6d28168 AK |
3070 | if (IS_ERR(kvm)) |
3071 | return PTR_ERR(kvm); | |
6ce5a090 TY |
3072 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
3073 | r = kvm_coalesced_mmio_init(kvm); | |
3074 | if (r < 0) { | |
3075 | kvm_put_kvm(kvm); | |
3076 | return r; | |
3077 | } | |
3078 | #endif | |
24009b05 | 3079 | r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR | O_CLOEXEC); |
536a6f88 | 3080 | if (r < 0) { |
66c0b394 | 3081 | kvm_put_kvm(kvm); |
536a6f88 JF |
3082 | return r; |
3083 | } | |
3084 | ||
3085 | if (kvm_create_vm_debugfs(kvm, r) < 0) { | |
3086 | kvm_put_kvm(kvm); | |
3087 | return -ENOMEM; | |
3088 | } | |
f17abe9a | 3089 | |
aac87636 | 3090 | return r; |
f17abe9a AK |
3091 | } |
3092 | ||
3093 | static long kvm_dev_ioctl(struct file *filp, | |
3094 | unsigned int ioctl, unsigned long arg) | |
3095 | { | |
07c45a36 | 3096 | long r = -EINVAL; |
f17abe9a AK |
3097 | |
3098 | switch (ioctl) { | |
3099 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
3100 | if (arg) |
3101 | goto out; | |
f17abe9a AK |
3102 | r = KVM_API_VERSION; |
3103 | break; | |
3104 | case KVM_CREATE_VM: | |
e08b9637 | 3105 | r = kvm_dev_ioctl_create_vm(arg); |
f17abe9a | 3106 | break; |
018d00d2 | 3107 | case KVM_CHECK_EXTENSION: |
784aa3d7 | 3108 | r = kvm_vm_ioctl_check_extension_generic(NULL, arg); |
5d308f45 | 3109 | break; |
07c45a36 | 3110 | case KVM_GET_VCPU_MMAP_SIZE: |
07c45a36 AK |
3111 | if (arg) |
3112 | goto out; | |
adb1ff46 AK |
3113 | r = PAGE_SIZE; /* struct kvm_run */ |
3114 | #ifdef CONFIG_X86 | |
3115 | r += PAGE_SIZE; /* pio data page */ | |
5f94c174 LV |
3116 | #endif |
3117 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
3118 | r += PAGE_SIZE; /* coalesced mmio ring page */ | |
adb1ff46 | 3119 | #endif |
07c45a36 | 3120 | break; |
d4c9ff2d FEL |
3121 | case KVM_TRACE_ENABLE: |
3122 | case KVM_TRACE_PAUSE: | |
3123 | case KVM_TRACE_DISABLE: | |
2023a29c | 3124 | r = -EOPNOTSUPP; |
d4c9ff2d | 3125 | break; |
6aa8b732 | 3126 | default: |
043405e1 | 3127 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
3128 | } |
3129 | out: | |
3130 | return r; | |
3131 | } | |
3132 | ||
6aa8b732 | 3133 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 AK |
3134 | .unlocked_ioctl = kvm_dev_ioctl, |
3135 | .compat_ioctl = kvm_dev_ioctl, | |
6038f373 | 3136 | .llseek = noop_llseek, |
6aa8b732 AK |
3137 | }; |
3138 | ||
3139 | static struct miscdevice kvm_dev = { | |
bbe4432e | 3140 | KVM_MINOR, |
6aa8b732 AK |
3141 | "kvm", |
3142 | &kvm_chardev_ops, | |
3143 | }; | |
3144 | ||
75b7127c | 3145 | static void hardware_enable_nolock(void *junk) |
1b6c0168 AK |
3146 | { |
3147 | int cpu = raw_smp_processor_id(); | |
10474ae8 | 3148 | int r; |
1b6c0168 | 3149 | |
7f59f492 | 3150 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
1b6c0168 | 3151 | return; |
10474ae8 | 3152 | |
7f59f492 | 3153 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
10474ae8 | 3154 | |
13a34e06 | 3155 | r = kvm_arch_hardware_enable(); |
10474ae8 AG |
3156 | |
3157 | if (r) { | |
3158 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); | |
3159 | atomic_inc(&hardware_enable_failed); | |
1170adc6 | 3160 | pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); |
10474ae8 | 3161 | } |
1b6c0168 AK |
3162 | } |
3163 | ||
4fa92fb2 | 3164 | static void hardware_enable(void) |
75b7127c | 3165 | { |
4a937f96 | 3166 | raw_spin_lock(&kvm_count_lock); |
4fa92fb2 PB |
3167 | if (kvm_usage_count) |
3168 | hardware_enable_nolock(NULL); | |
4a937f96 | 3169 | raw_spin_unlock(&kvm_count_lock); |
75b7127c TY |
3170 | } |
3171 | ||
3172 | static void hardware_disable_nolock(void *junk) | |
1b6c0168 AK |
3173 | { |
3174 | int cpu = raw_smp_processor_id(); | |
3175 | ||
7f59f492 | 3176 | if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
1b6c0168 | 3177 | return; |
7f59f492 | 3178 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
13a34e06 | 3179 | kvm_arch_hardware_disable(); |
1b6c0168 AK |
3180 | } |
3181 | ||
4fa92fb2 | 3182 | static void hardware_disable(void) |
75b7127c | 3183 | { |
4a937f96 | 3184 | raw_spin_lock(&kvm_count_lock); |
4fa92fb2 PB |
3185 | if (kvm_usage_count) |
3186 | hardware_disable_nolock(NULL); | |
4a937f96 | 3187 | raw_spin_unlock(&kvm_count_lock); |
75b7127c TY |
3188 | } |
3189 | ||
10474ae8 AG |
3190 | static void hardware_disable_all_nolock(void) |
3191 | { | |
3192 | BUG_ON(!kvm_usage_count); | |
3193 | ||
3194 | kvm_usage_count--; | |
3195 | if (!kvm_usage_count) | |
75b7127c | 3196 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
10474ae8 AG |
3197 | } |
3198 | ||
3199 | static void hardware_disable_all(void) | |
3200 | { | |
4a937f96 | 3201 | raw_spin_lock(&kvm_count_lock); |
10474ae8 | 3202 | hardware_disable_all_nolock(); |
4a937f96 | 3203 | raw_spin_unlock(&kvm_count_lock); |
10474ae8 AG |
3204 | } |
3205 | ||
3206 | static int hardware_enable_all(void) | |
3207 | { | |
3208 | int r = 0; | |
3209 | ||
4a937f96 | 3210 | raw_spin_lock(&kvm_count_lock); |
10474ae8 AG |
3211 | |
3212 | kvm_usage_count++; | |
3213 | if (kvm_usage_count == 1) { | |
3214 | atomic_set(&hardware_enable_failed, 0); | |
75b7127c | 3215 | on_each_cpu(hardware_enable_nolock, NULL, 1); |
10474ae8 AG |
3216 | |
3217 | if (atomic_read(&hardware_enable_failed)) { | |
3218 | hardware_disable_all_nolock(); | |
3219 | r = -EBUSY; | |
3220 | } | |
3221 | } | |
3222 | ||
4a937f96 | 3223 | raw_spin_unlock(&kvm_count_lock); |
10474ae8 AG |
3224 | |
3225 | return r; | |
3226 | } | |
3227 | ||
774c47f1 AK |
3228 | static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, |
3229 | void *v) | |
3230 | { | |
1a6f4d7f | 3231 | val &= ~CPU_TASKS_FROZEN; |
774c47f1 | 3232 | switch (val) { |
cec9ad27 | 3233 | case CPU_DYING: |
4fa92fb2 | 3234 | hardware_disable(); |
6ec8a856 | 3235 | break; |
da908f2f | 3236 | case CPU_STARTING: |
4fa92fb2 | 3237 | hardware_enable(); |
774c47f1 AK |
3238 | break; |
3239 | } | |
3240 | return NOTIFY_OK; | |
3241 | } | |
3242 | ||
9a2b85c6 | 3243 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 3244 | void *v) |
9a2b85c6 | 3245 | { |
8e1c1815 SY |
3246 | /* |
3247 | * Some (well, at least mine) BIOSes hang on reboot if | |
3248 | * in vmx root mode. | |
3249 | * | |
3250 | * And Intel TXT required VMX off for all cpu when system shutdown. | |
3251 | */ | |
1170adc6 | 3252 | pr_info("kvm: exiting hardware virtualization\n"); |
8e1c1815 | 3253 | kvm_rebooting = true; |
75b7127c | 3254 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
9a2b85c6 RR |
3255 | return NOTIFY_OK; |
3256 | } | |
3257 | ||
3258 | static struct notifier_block kvm_reboot_notifier = { | |
3259 | .notifier_call = kvm_reboot, | |
3260 | .priority = 0, | |
3261 | }; | |
3262 | ||
e93f8a0f | 3263 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
2eeb2e94 GH |
3264 | { |
3265 | int i; | |
3266 | ||
3267 | for (i = 0; i < bus->dev_count; i++) { | |
743eeb0b | 3268 | struct kvm_io_device *pos = bus->range[i].dev; |
2eeb2e94 GH |
3269 | |
3270 | kvm_iodevice_destructor(pos); | |
3271 | } | |
e93f8a0f | 3272 | kfree(bus); |
2eeb2e94 GH |
3273 | } |
3274 | ||
c21fbff1 | 3275 | static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1, |
20e87b72 | 3276 | const struct kvm_io_range *r2) |
743eeb0b | 3277 | { |
8f4216c7 JW |
3278 | gpa_t addr1 = r1->addr; |
3279 | gpa_t addr2 = r2->addr; | |
3280 | ||
3281 | if (addr1 < addr2) | |
743eeb0b | 3282 | return -1; |
8f4216c7 JW |
3283 | |
3284 | /* If r2->len == 0, match the exact address. If r2->len != 0, | |
3285 | * accept any overlapping write. Any order is acceptable for | |
3286 | * overlapping ranges, because kvm_io_bus_get_first_dev ensures | |
3287 | * we process all of them. | |
3288 | */ | |
3289 | if (r2->len) { | |
3290 | addr1 += r1->len; | |
3291 | addr2 += r2->len; | |
3292 | } | |
3293 | ||
3294 | if (addr1 > addr2) | |
743eeb0b | 3295 | return 1; |
8f4216c7 | 3296 | |
743eeb0b SL |
3297 | return 0; |
3298 | } | |
3299 | ||
a343c9b7 PB |
3300 | static int kvm_io_bus_sort_cmp(const void *p1, const void *p2) |
3301 | { | |
c21fbff1 | 3302 | return kvm_io_bus_cmp(p1, p2); |
a343c9b7 PB |
3303 | } |
3304 | ||
39369f7a | 3305 | static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev, |
743eeb0b SL |
3306 | gpa_t addr, int len) |
3307 | { | |
743eeb0b SL |
3308 | bus->range[bus->dev_count++] = (struct kvm_io_range) { |
3309 | .addr = addr, | |
3310 | .len = len, | |
3311 | .dev = dev, | |
3312 | }; | |
3313 | ||
3314 | sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range), | |
3315 | kvm_io_bus_sort_cmp, NULL); | |
3316 | ||
3317 | return 0; | |
3318 | } | |
3319 | ||
39369f7a | 3320 | static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus, |
743eeb0b SL |
3321 | gpa_t addr, int len) |
3322 | { | |
3323 | struct kvm_io_range *range, key; | |
3324 | int off; | |
3325 | ||
3326 | key = (struct kvm_io_range) { | |
3327 | .addr = addr, | |
3328 | .len = len, | |
3329 | }; | |
3330 | ||
3331 | range = bsearch(&key, bus->range, bus->dev_count, | |
3332 | sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp); | |
3333 | if (range == NULL) | |
3334 | return -ENOENT; | |
3335 | ||
3336 | off = range - bus->range; | |
3337 | ||
c21fbff1 | 3338 | while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0) |
743eeb0b SL |
3339 | off--; |
3340 | ||
3341 | return off; | |
3342 | } | |
3343 | ||
e32edf4f | 3344 | static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
126a5af5 CH |
3345 | struct kvm_io_range *range, const void *val) |
3346 | { | |
3347 | int idx; | |
3348 | ||
3349 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); | |
3350 | if (idx < 0) | |
3351 | return -EOPNOTSUPP; | |
3352 | ||
3353 | while (idx < bus->dev_count && | |
c21fbff1 | 3354 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
e32edf4f | 3355 | if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr, |
126a5af5 CH |
3356 | range->len, val)) |
3357 | return idx; | |
3358 | idx++; | |
3359 | } | |
3360 | ||
3361 | return -EOPNOTSUPP; | |
3362 | } | |
3363 | ||
bda9020e | 3364 | /* kvm_io_bus_write - called under kvm->slots_lock */ |
e32edf4f | 3365 | int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
bda9020e | 3366 | int len, const void *val) |
2eeb2e94 | 3367 | { |
90d83dc3 | 3368 | struct kvm_io_bus *bus; |
743eeb0b | 3369 | struct kvm_io_range range; |
126a5af5 | 3370 | int r; |
743eeb0b SL |
3371 | |
3372 | range = (struct kvm_io_range) { | |
3373 | .addr = addr, | |
3374 | .len = len, | |
3375 | }; | |
90d83dc3 | 3376 | |
e32edf4f NN |
3377 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
3378 | r = __kvm_io_bus_write(vcpu, bus, &range, val); | |
126a5af5 CH |
3379 | return r < 0 ? r : 0; |
3380 | } | |
3381 | ||
3382 | /* kvm_io_bus_write_cookie - called under kvm->slots_lock */ | |
e32edf4f NN |
3383 | int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, |
3384 | gpa_t addr, int len, const void *val, long cookie) | |
126a5af5 CH |
3385 | { |
3386 | struct kvm_io_bus *bus; | |
3387 | struct kvm_io_range range; | |
3388 | ||
3389 | range = (struct kvm_io_range) { | |
3390 | .addr = addr, | |
3391 | .len = len, | |
3392 | }; | |
3393 | ||
e32edf4f | 3394 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
126a5af5 CH |
3395 | |
3396 | /* First try the device referenced by cookie. */ | |
3397 | if ((cookie >= 0) && (cookie < bus->dev_count) && | |
c21fbff1 | 3398 | (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0)) |
e32edf4f | 3399 | if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len, |
126a5af5 CH |
3400 | val)) |
3401 | return cookie; | |
3402 | ||
3403 | /* | |
3404 | * cookie contained garbage; fall back to search and return the | |
3405 | * correct cookie value. | |
3406 | */ | |
e32edf4f | 3407 | return __kvm_io_bus_write(vcpu, bus, &range, val); |
126a5af5 CH |
3408 | } |
3409 | ||
e32edf4f NN |
3410 | static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
3411 | struct kvm_io_range *range, void *val) | |
126a5af5 CH |
3412 | { |
3413 | int idx; | |
3414 | ||
3415 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); | |
743eeb0b SL |
3416 | if (idx < 0) |
3417 | return -EOPNOTSUPP; | |
3418 | ||
3419 | while (idx < bus->dev_count && | |
c21fbff1 | 3420 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
e32edf4f | 3421 | if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr, |
126a5af5 CH |
3422 | range->len, val)) |
3423 | return idx; | |
743eeb0b SL |
3424 | idx++; |
3425 | } | |
3426 | ||
bda9020e MT |
3427 | return -EOPNOTSUPP; |
3428 | } | |
68c3b4d1 | 3429 | EXPORT_SYMBOL_GPL(kvm_io_bus_write); |
2eeb2e94 | 3430 | |
bda9020e | 3431 | /* kvm_io_bus_read - called under kvm->slots_lock */ |
e32edf4f | 3432 | int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
e93f8a0f | 3433 | int len, void *val) |
bda9020e | 3434 | { |
90d83dc3 | 3435 | struct kvm_io_bus *bus; |
743eeb0b | 3436 | struct kvm_io_range range; |
126a5af5 | 3437 | int r; |
743eeb0b SL |
3438 | |
3439 | range = (struct kvm_io_range) { | |
3440 | .addr = addr, | |
3441 | .len = len, | |
3442 | }; | |
e93f8a0f | 3443 | |
e32edf4f NN |
3444 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
3445 | r = __kvm_io_bus_read(vcpu, bus, &range, val); | |
126a5af5 CH |
3446 | return r < 0 ? r : 0; |
3447 | } | |
743eeb0b | 3448 | |
2eeb2e94 | 3449 | |
79fac95e | 3450 | /* Caller must hold slots_lock. */ |
743eeb0b SL |
3451 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
3452 | int len, struct kvm_io_device *dev) | |
6c474694 | 3453 | { |
e93f8a0f | 3454 | struct kvm_io_bus *new_bus, *bus; |
090b7aff | 3455 | |
e93f8a0f | 3456 | bus = kvm->buses[bus_idx]; |
6ea34c9b AK |
3457 | /* exclude ioeventfd which is limited by maximum fd */ |
3458 | if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) | |
090b7aff | 3459 | return -ENOSPC; |
2eeb2e94 | 3460 | |
d3febddd | 3461 | new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count + 1) * |
a1300716 | 3462 | sizeof(struct kvm_io_range)), GFP_KERNEL); |
e93f8a0f MT |
3463 | if (!new_bus) |
3464 | return -ENOMEM; | |
a1300716 AK |
3465 | memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count * |
3466 | sizeof(struct kvm_io_range))); | |
743eeb0b | 3467 | kvm_io_bus_insert_dev(new_bus, dev, addr, len); |
e93f8a0f MT |
3468 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
3469 | synchronize_srcu_expedited(&kvm->srcu); | |
3470 | kfree(bus); | |
090b7aff GH |
3471 | |
3472 | return 0; | |
3473 | } | |
3474 | ||
79fac95e | 3475 | /* Caller must hold slots_lock. */ |
e93f8a0f MT |
3476 | int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
3477 | struct kvm_io_device *dev) | |
090b7aff | 3478 | { |
e93f8a0f MT |
3479 | int i, r; |
3480 | struct kvm_io_bus *new_bus, *bus; | |
090b7aff | 3481 | |
cdfca7b3 | 3482 | bus = kvm->buses[bus_idx]; |
e93f8a0f | 3483 | r = -ENOENT; |
a1300716 AK |
3484 | for (i = 0; i < bus->dev_count; i++) |
3485 | if (bus->range[i].dev == dev) { | |
e93f8a0f | 3486 | r = 0; |
090b7aff GH |
3487 | break; |
3488 | } | |
e93f8a0f | 3489 | |
a1300716 | 3490 | if (r) |
e93f8a0f | 3491 | return r; |
a1300716 | 3492 | |
d3febddd | 3493 | new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count - 1) * |
a1300716 AK |
3494 | sizeof(struct kvm_io_range)), GFP_KERNEL); |
3495 | if (!new_bus) | |
3496 | return -ENOMEM; | |
3497 | ||
3498 | memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); | |
3499 | new_bus->dev_count--; | |
3500 | memcpy(new_bus->range + i, bus->range + i + 1, | |
3501 | (new_bus->dev_count - i) * sizeof(struct kvm_io_range)); | |
e93f8a0f MT |
3502 | |
3503 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); | |
3504 | synchronize_srcu_expedited(&kvm->srcu); | |
3505 | kfree(bus); | |
3506 | return r; | |
2eeb2e94 GH |
3507 | } |
3508 | ||
774c47f1 AK |
3509 | static struct notifier_block kvm_cpu_notifier = { |
3510 | .notifier_call = kvm_cpu_hotplug, | |
774c47f1 AK |
3511 | }; |
3512 | ||
536a6f88 JF |
3513 | static int kvm_debugfs_open(struct inode *inode, struct file *file, |
3514 | int (*get)(void *, u64 *), int (*set)(void *, u64), | |
3515 | const char *fmt) | |
3516 | { | |
3517 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) | |
3518 | inode->i_private; | |
3519 | ||
3520 | /* The debugfs files are a reference to the kvm struct which | |
3521 | * is still valid when kvm_destroy_vm is called. | |
3522 | * To avoid the race between open and the removal of the debugfs | |
3523 | * directory we test against the users count. | |
3524 | */ | |
3525 | if (!atomic_add_unless(&stat_data->kvm->users_count, 1, 0)) | |
3526 | return -ENOENT; | |
3527 | ||
3528 | if (simple_attr_open(inode, file, get, set, fmt)) { | |
3529 | kvm_put_kvm(stat_data->kvm); | |
3530 | return -ENOMEM; | |
3531 | } | |
3532 | ||
3533 | return 0; | |
3534 | } | |
3535 | ||
3536 | static int kvm_debugfs_release(struct inode *inode, struct file *file) | |
3537 | { | |
3538 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) | |
3539 | inode->i_private; | |
3540 | ||
3541 | simple_attr_release(inode, file); | |
3542 | kvm_put_kvm(stat_data->kvm); | |
3543 | ||
3544 | return 0; | |
3545 | } | |
3546 | ||
3547 | static int vm_stat_get_per_vm(void *data, u64 *val) | |
3548 | { | |
3549 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3550 | ||
3551 | *val = *(u32 *)((void *)stat_data->kvm + stat_data->offset); | |
3552 | ||
3553 | return 0; | |
3554 | } | |
3555 | ||
3556 | static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file) | |
3557 | { | |
3558 | __simple_attr_check_format("%llu\n", 0ull); | |
3559 | return kvm_debugfs_open(inode, file, vm_stat_get_per_vm, | |
3560 | NULL, "%llu\n"); | |
3561 | } | |
3562 | ||
3563 | static const struct file_operations vm_stat_get_per_vm_fops = { | |
3564 | .owner = THIS_MODULE, | |
3565 | .open = vm_stat_get_per_vm_open, | |
3566 | .release = kvm_debugfs_release, | |
3567 | .read = simple_attr_read, | |
3568 | .write = simple_attr_write, | |
3569 | .llseek = generic_file_llseek, | |
3570 | }; | |
3571 | ||
3572 | static int vcpu_stat_get_per_vm(void *data, u64 *val) | |
3573 | { | |
3574 | int i; | |
3575 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3576 | struct kvm_vcpu *vcpu; | |
3577 | ||
3578 | *val = 0; | |
3579 | ||
3580 | kvm_for_each_vcpu(i, vcpu, stat_data->kvm) | |
3581 | *val += *(u32 *)((void *)vcpu + stat_data->offset); | |
3582 | ||
3583 | return 0; | |
3584 | } | |
3585 | ||
3586 | static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file) | |
3587 | { | |
3588 | __simple_attr_check_format("%llu\n", 0ull); | |
3589 | return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm, | |
3590 | NULL, "%llu\n"); | |
3591 | } | |
3592 | ||
3593 | static const struct file_operations vcpu_stat_get_per_vm_fops = { | |
3594 | .owner = THIS_MODULE, | |
3595 | .open = vcpu_stat_get_per_vm_open, | |
3596 | .release = kvm_debugfs_release, | |
3597 | .read = simple_attr_read, | |
3598 | .write = simple_attr_write, | |
3599 | .llseek = generic_file_llseek, | |
3600 | }; | |
3601 | ||
3602 | static const struct file_operations *stat_fops_per_vm[] = { | |
3603 | [KVM_STAT_VCPU] = &vcpu_stat_get_per_vm_fops, | |
3604 | [KVM_STAT_VM] = &vm_stat_get_per_vm_fops, | |
3605 | }; | |
3606 | ||
8b88b099 | 3607 | static int vm_stat_get(void *_offset, u64 *val) |
ba1389b7 AK |
3608 | { |
3609 | unsigned offset = (long)_offset; | |
ba1389b7 | 3610 | struct kvm *kvm; |
536a6f88 JF |
3611 | struct kvm_stat_data stat_tmp = {.offset = offset}; |
3612 | u64 tmp_val; | |
ba1389b7 | 3613 | |
8b88b099 | 3614 | *val = 0; |
2f303b74 | 3615 | spin_lock(&kvm_lock); |
536a6f88 JF |
3616 | list_for_each_entry(kvm, &vm_list, vm_list) { |
3617 | stat_tmp.kvm = kvm; | |
3618 | vm_stat_get_per_vm((void *)&stat_tmp, &tmp_val); | |
3619 | *val += tmp_val; | |
3620 | } | |
2f303b74 | 3621 | spin_unlock(&kvm_lock); |
8b88b099 | 3622 | return 0; |
ba1389b7 AK |
3623 | } |
3624 | ||
3625 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n"); | |
3626 | ||
8b88b099 | 3627 | static int vcpu_stat_get(void *_offset, u64 *val) |
1165f5fe AK |
3628 | { |
3629 | unsigned offset = (long)_offset; | |
1165f5fe | 3630 | struct kvm *kvm; |
536a6f88 JF |
3631 | struct kvm_stat_data stat_tmp = {.offset = offset}; |
3632 | u64 tmp_val; | |
1165f5fe | 3633 | |
8b88b099 | 3634 | *val = 0; |
2f303b74 | 3635 | spin_lock(&kvm_lock); |
536a6f88 JF |
3636 | list_for_each_entry(kvm, &vm_list, vm_list) { |
3637 | stat_tmp.kvm = kvm; | |
3638 | vcpu_stat_get_per_vm((void *)&stat_tmp, &tmp_val); | |
3639 | *val += tmp_val; | |
3640 | } | |
2f303b74 | 3641 | spin_unlock(&kvm_lock); |
8b88b099 | 3642 | return 0; |
1165f5fe AK |
3643 | } |
3644 | ||
ba1389b7 AK |
3645 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n"); |
3646 | ||
828c0950 | 3647 | static const struct file_operations *stat_fops[] = { |
ba1389b7 AK |
3648 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
3649 | [KVM_STAT_VM] = &vm_stat_fops, | |
3650 | }; | |
1165f5fe | 3651 | |
4f69b680 | 3652 | static int kvm_init_debug(void) |
6aa8b732 | 3653 | { |
0c8eb04a | 3654 | int r = -EEXIST; |
6aa8b732 AK |
3655 | struct kvm_stats_debugfs_item *p; |
3656 | ||
76f7c879 | 3657 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
4f69b680 H |
3658 | if (kvm_debugfs_dir == NULL) |
3659 | goto out; | |
3660 | ||
536a6f88 JF |
3661 | kvm_debugfs_num_entries = 0; |
3662 | for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) { | |
4bd33b56 JF |
3663 | if (!debugfs_create_file(p->name, 0444, kvm_debugfs_dir, |
3664 | (void *)(long)p->offset, | |
3665 | stat_fops[p->kind])) | |
4f69b680 H |
3666 | goto out_dir; |
3667 | } | |
3668 | ||
3669 | return 0; | |
3670 | ||
3671 | out_dir: | |
3672 | debugfs_remove_recursive(kvm_debugfs_dir); | |
3673 | out: | |
3674 | return r; | |
6aa8b732 AK |
3675 | } |
3676 | ||
fb3600cc | 3677 | static int kvm_suspend(void) |
59ae6c6b | 3678 | { |
10474ae8 | 3679 | if (kvm_usage_count) |
75b7127c | 3680 | hardware_disable_nolock(NULL); |
59ae6c6b AK |
3681 | return 0; |
3682 | } | |
3683 | ||
fb3600cc | 3684 | static void kvm_resume(void) |
59ae6c6b | 3685 | { |
ca84d1a2 | 3686 | if (kvm_usage_count) { |
4a937f96 | 3687 | WARN_ON(raw_spin_is_locked(&kvm_count_lock)); |
75b7127c | 3688 | hardware_enable_nolock(NULL); |
ca84d1a2 | 3689 | } |
59ae6c6b AK |
3690 | } |
3691 | ||
fb3600cc | 3692 | static struct syscore_ops kvm_syscore_ops = { |
59ae6c6b AK |
3693 | .suspend = kvm_suspend, |
3694 | .resume = kvm_resume, | |
3695 | }; | |
3696 | ||
15ad7146 AK |
3697 | static inline |
3698 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
3699 | { | |
3700 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
3701 | } | |
3702 | ||
3703 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
3704 | { | |
3705 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
f95ef0cd | 3706 | |
3a08a8f9 R |
3707 | if (vcpu->preempted) |
3708 | vcpu->preempted = false; | |
15ad7146 | 3709 | |
e790d9ef RK |
3710 | kvm_arch_sched_in(vcpu, cpu); |
3711 | ||
e9b11c17 | 3712 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 AK |
3713 | } |
3714 | ||
3715 | static void kvm_sched_out(struct preempt_notifier *pn, | |
3716 | struct task_struct *next) | |
3717 | { | |
3718 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
3719 | ||
3a08a8f9 R |
3720 | if (current->state == TASK_RUNNING) |
3721 | vcpu->preempted = true; | |
e9b11c17 | 3722 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
3723 | } |
3724 | ||
0ee75bea | 3725 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
c16f862d | 3726 | struct module *module) |
6aa8b732 AK |
3727 | { |
3728 | int r; | |
002c7f7c | 3729 | int cpu; |
6aa8b732 | 3730 | |
f8c16bba ZX |
3731 | r = kvm_arch_init(opaque); |
3732 | if (r) | |
d2308784 | 3733 | goto out_fail; |
cb498ea2 | 3734 | |
7dac16c3 AH |
3735 | /* |
3736 | * kvm_arch_init makes sure there's at most one caller | |
3737 | * for architectures that support multiple implementations, | |
3738 | * like intel and amd on x86. | |
3739 | * kvm_arch_init must be called before kvm_irqfd_init to avoid creating | |
3740 | * conflicts in case kvm is already setup for another implementation. | |
3741 | */ | |
3742 | r = kvm_irqfd_init(); | |
3743 | if (r) | |
3744 | goto out_irqfd; | |
3745 | ||
8437a617 | 3746 | if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { |
7f59f492 RR |
3747 | r = -ENOMEM; |
3748 | goto out_free_0; | |
3749 | } | |
3750 | ||
e9b11c17 | 3751 | r = kvm_arch_hardware_setup(); |
6aa8b732 | 3752 | if (r < 0) |
7f59f492 | 3753 | goto out_free_0a; |
6aa8b732 | 3754 | |
002c7f7c YS |
3755 | for_each_online_cpu(cpu) { |
3756 | smp_call_function_single(cpu, | |
e9b11c17 | 3757 | kvm_arch_check_processor_compat, |
8691e5a8 | 3758 | &r, 1); |
002c7f7c | 3759 | if (r < 0) |
d2308784 | 3760 | goto out_free_1; |
002c7f7c YS |
3761 | } |
3762 | ||
774c47f1 AK |
3763 | r = register_cpu_notifier(&kvm_cpu_notifier); |
3764 | if (r) | |
d2308784 | 3765 | goto out_free_2; |
6aa8b732 AK |
3766 | register_reboot_notifier(&kvm_reboot_notifier); |
3767 | ||
c16f862d | 3768 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
0ee75bea AK |
3769 | if (!vcpu_align) |
3770 | vcpu_align = __alignof__(struct kvm_vcpu); | |
3771 | kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align, | |
56919c5c | 3772 | 0, NULL); |
c16f862d RR |
3773 | if (!kvm_vcpu_cache) { |
3774 | r = -ENOMEM; | |
fb3600cc | 3775 | goto out_free_3; |
c16f862d RR |
3776 | } |
3777 | ||
af585b92 GN |
3778 | r = kvm_async_pf_init(); |
3779 | if (r) | |
3780 | goto out_free; | |
3781 | ||
6aa8b732 | 3782 | kvm_chardev_ops.owner = module; |
3d3aab1b CB |
3783 | kvm_vm_fops.owner = module; |
3784 | kvm_vcpu_fops.owner = module; | |
6aa8b732 AK |
3785 | |
3786 | r = misc_register(&kvm_dev); | |
3787 | if (r) { | |
1170adc6 | 3788 | pr_err("kvm: misc device register failed\n"); |
af585b92 | 3789 | goto out_unreg; |
6aa8b732 AK |
3790 | } |
3791 | ||
fb3600cc RW |
3792 | register_syscore_ops(&kvm_syscore_ops); |
3793 | ||
15ad7146 AK |
3794 | kvm_preempt_ops.sched_in = kvm_sched_in; |
3795 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
3796 | ||
4f69b680 H |
3797 | r = kvm_init_debug(); |
3798 | if (r) { | |
1170adc6 | 3799 | pr_err("kvm: create debugfs files failed\n"); |
4f69b680 H |
3800 | goto out_undebugfs; |
3801 | } | |
0ea4ed8e | 3802 | |
3c3c29fd PB |
3803 | r = kvm_vfio_ops_init(); |
3804 | WARN_ON(r); | |
3805 | ||
c7addb90 | 3806 | return 0; |
6aa8b732 | 3807 | |
4f69b680 H |
3808 | out_undebugfs: |
3809 | unregister_syscore_ops(&kvm_syscore_ops); | |
afc2f792 | 3810 | misc_deregister(&kvm_dev); |
af585b92 GN |
3811 | out_unreg: |
3812 | kvm_async_pf_deinit(); | |
6aa8b732 | 3813 | out_free: |
c16f862d | 3814 | kmem_cache_destroy(kvm_vcpu_cache); |
d2308784 | 3815 | out_free_3: |
6aa8b732 | 3816 | unregister_reboot_notifier(&kvm_reboot_notifier); |
774c47f1 | 3817 | unregister_cpu_notifier(&kvm_cpu_notifier); |
d2308784 | 3818 | out_free_2: |
d2308784 | 3819 | out_free_1: |
e9b11c17 | 3820 | kvm_arch_hardware_unsetup(); |
7f59f492 RR |
3821 | out_free_0a: |
3822 | free_cpumask_var(cpus_hardware_enabled); | |
d2308784 | 3823 | out_free_0: |
a0f155e9 CH |
3824 | kvm_irqfd_exit(); |
3825 | out_irqfd: | |
7dac16c3 AH |
3826 | kvm_arch_exit(); |
3827 | out_fail: | |
6aa8b732 AK |
3828 | return r; |
3829 | } | |
cb498ea2 | 3830 | EXPORT_SYMBOL_GPL(kvm_init); |
6aa8b732 | 3831 | |
cb498ea2 | 3832 | void kvm_exit(void) |
6aa8b732 | 3833 | { |
4bd33b56 | 3834 | debugfs_remove_recursive(kvm_debugfs_dir); |
6aa8b732 | 3835 | misc_deregister(&kvm_dev); |
c16f862d | 3836 | kmem_cache_destroy(kvm_vcpu_cache); |
af585b92 | 3837 | kvm_async_pf_deinit(); |
fb3600cc | 3838 | unregister_syscore_ops(&kvm_syscore_ops); |
6aa8b732 | 3839 | unregister_reboot_notifier(&kvm_reboot_notifier); |
59ae6c6b | 3840 | unregister_cpu_notifier(&kvm_cpu_notifier); |
75b7127c | 3841 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
e9b11c17 | 3842 | kvm_arch_hardware_unsetup(); |
f8c16bba | 3843 | kvm_arch_exit(); |
a0f155e9 | 3844 | kvm_irqfd_exit(); |
7f59f492 | 3845 | free_cpumask_var(cpus_hardware_enabled); |
571ee1b6 | 3846 | kvm_vfio_ops_exit(); |
6aa8b732 | 3847 | } |
cb498ea2 | 3848 | EXPORT_SYMBOL_GPL(kvm_exit); |