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