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