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