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