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