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