Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * This module enables machines with Intel VT-x extensions to run virtual | |
5 | * machines without emulation or binary translation. | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
9 | * |
10 | * Authors: | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * Yaniv Kamay <yaniv@qumranet.com> | |
13 | * | |
14 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
15 | * the COPYING file in the top-level directory. | |
16 | * | |
17 | */ | |
18 | ||
af669ac6 | 19 | #include <kvm/iodev.h> |
6aa8b732 | 20 | |
edf88417 | 21 | #include <linux/kvm_host.h> |
6aa8b732 AK |
22 | #include <linux/kvm.h> |
23 | #include <linux/module.h> | |
24 | #include <linux/errno.h> | |
6aa8b732 | 25 | #include <linux/percpu.h> |
6aa8b732 AK |
26 | #include <linux/mm.h> |
27 | #include <linux/miscdevice.h> | |
28 | #include <linux/vmalloc.h> | |
6aa8b732 | 29 | #include <linux/reboot.h> |
6aa8b732 AK |
30 | #include <linux/debugfs.h> |
31 | #include <linux/highmem.h> | |
32 | #include <linux/file.h> | |
fb3600cc | 33 | #include <linux/syscore_ops.h> |
774c47f1 | 34 | #include <linux/cpu.h> |
174cd4b1 | 35 | #include <linux/sched/signal.h> |
6e84f315 | 36 | #include <linux/sched/mm.h> |
03441a34 | 37 | #include <linux/sched/stat.h> |
d9e368d6 AK |
38 | #include <linux/cpumask.h> |
39 | #include <linux/smp.h> | |
d6d28168 | 40 | #include <linux/anon_inodes.h> |
04d2cc77 | 41 | #include <linux/profile.h> |
7aa81cc0 | 42 | #include <linux/kvm_para.h> |
6fc138d2 | 43 | #include <linux/pagemap.h> |
8d4e1288 | 44 | #include <linux/mman.h> |
35149e21 | 45 | #include <linux/swap.h> |
e56d532f | 46 | #include <linux/bitops.h> |
547de29e | 47 | #include <linux/spinlock.h> |
6ff5894c | 48 | #include <linux/compat.h> |
bc6678a3 | 49 | #include <linux/srcu.h> |
8f0b1ab6 | 50 | #include <linux/hugetlb.h> |
5a0e3ad6 | 51 | #include <linux/slab.h> |
743eeb0b SL |
52 | #include <linux/sort.h> |
53 | #include <linux/bsearch.h> | |
6aa8b732 | 54 | |
e495606d | 55 | #include <asm/processor.h> |
e495606d | 56 | #include <asm/io.h> |
2ea75be3 | 57 | #include <asm/ioctl.h> |
7c0f6ba6 | 58 | #include <linux/uaccess.h> |
3e021bf5 | 59 | #include <asm/pgtable.h> |
6aa8b732 | 60 | |
5f94c174 | 61 | #include "coalesced_mmio.h" |
af585b92 | 62 | #include "async_pf.h" |
3c3c29fd | 63 | #include "vfio.h" |
5f94c174 | 64 | |
229456fc MT |
65 | #define CREATE_TRACE_POINTS |
66 | #include <trace/events/kvm.h> | |
67 | ||
536a6f88 JF |
68 | /* Worst case buffer size needed for holding an integer. */ |
69 | #define ITOA_MAX_LEN 12 | |
70 | ||
6aa8b732 AK |
71 | MODULE_AUTHOR("Qumranet"); |
72 | MODULE_LICENSE("GPL"); | |
73 | ||
920552b2 | 74 | /* Architectures should define their poll value according to the halt latency */ |
ec76d819 | 75 | unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT; |
039c5d1b | 76 | module_param(halt_poll_ns, uint, 0644); |
ec76d819 | 77 | EXPORT_SYMBOL_GPL(halt_poll_ns); |
f7819512 | 78 | |
aca6ff29 | 79 | /* Default doubles per-vcpu halt_poll_ns. */ |
ec76d819 | 80 | unsigned int halt_poll_ns_grow = 2; |
039c5d1b | 81 | module_param(halt_poll_ns_grow, uint, 0644); |
ec76d819 | 82 | EXPORT_SYMBOL_GPL(halt_poll_ns_grow); |
aca6ff29 | 83 | |
49113d36 NW |
84 | /* The start value to grow halt_poll_ns from */ |
85 | unsigned int halt_poll_ns_grow_start = 10000; /* 10us */ | |
86 | module_param(halt_poll_ns_grow_start, uint, 0644); | |
87 | EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start); | |
88 | ||
aca6ff29 | 89 | /* Default resets per-vcpu halt_poll_ns . */ |
ec76d819 | 90 | unsigned int halt_poll_ns_shrink; |
039c5d1b | 91 | module_param(halt_poll_ns_shrink, uint, 0644); |
ec76d819 | 92 | EXPORT_SYMBOL_GPL(halt_poll_ns_shrink); |
aca6ff29 | 93 | |
fa40a821 MT |
94 | /* |
95 | * Ordering of locks: | |
96 | * | |
b7d409de | 97 | * kvm->lock --> kvm->slots_lock --> kvm->irq_lock |
fa40a821 MT |
98 | */ |
99 | ||
2f303b74 | 100 | DEFINE_SPINLOCK(kvm_lock); |
4a937f96 | 101 | static DEFINE_RAW_SPINLOCK(kvm_count_lock); |
e9b11c17 | 102 | LIST_HEAD(vm_list); |
133de902 | 103 | |
7f59f492 | 104 | static cpumask_var_t cpus_hardware_enabled; |
f4fee932 | 105 | static int kvm_usage_count; |
10474ae8 | 106 | static atomic_t hardware_enable_failed; |
1b6c0168 | 107 | |
c16f862d RR |
108 | struct kmem_cache *kvm_vcpu_cache; |
109 | EXPORT_SYMBOL_GPL(kvm_vcpu_cache); | |
1165f5fe | 110 | |
15ad7146 AK |
111 | static __read_mostly struct preempt_ops kvm_preempt_ops; |
112 | ||
76f7c879 | 113 | struct dentry *kvm_debugfs_dir; |
e23a808b | 114 | EXPORT_SYMBOL_GPL(kvm_debugfs_dir); |
6aa8b732 | 115 | |
536a6f88 JF |
116 | static int kvm_debugfs_num_entries; |
117 | static const struct file_operations *stat_fops_per_vm[]; | |
118 | ||
bccf2150 AK |
119 | static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, |
120 | unsigned long arg); | |
de8e5d74 | 121 | #ifdef CONFIG_KVM_COMPAT |
1dda606c AG |
122 | static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl, |
123 | unsigned long arg); | |
7ddfd3e0 MZ |
124 | #define KVM_COMPAT(c) .compat_ioctl = (c) |
125 | #else | |
126 | static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl, | |
127 | unsigned long arg) { return -EINVAL; } | |
128 | #define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl | |
1dda606c | 129 | #endif |
10474ae8 AG |
130 | static int hardware_enable_all(void); |
131 | static void hardware_disable_all(void); | |
bccf2150 | 132 | |
e93f8a0f | 133 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus); |
7940876e | 134 | |
bc009e43 | 135 | static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn); |
e93f8a0f | 136 | |
52480137 | 137 | __visible bool kvm_rebooting; |
b7c4145b | 138 | EXPORT_SYMBOL_GPL(kvm_rebooting); |
4ecac3fd | 139 | |
54dee993 MT |
140 | static bool largepages_enabled = true; |
141 | ||
286de8f6 CI |
142 | #define KVM_EVENT_CREATE_VM 0 |
143 | #define KVM_EVENT_DESTROY_VM 1 | |
144 | static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm); | |
145 | static unsigned long long kvm_createvm_count; | |
146 | static unsigned long long kvm_active_vms; | |
147 | ||
93065ac7 MH |
148 | __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
149 | unsigned long start, unsigned long end, bool blockable) | |
b1394e74 | 150 | { |
93065ac7 | 151 | return 0; |
b1394e74 RK |
152 | } |
153 | ||
ba049e93 | 154 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn) |
cbff90a7 | 155 | { |
11feeb49 | 156 | if (pfn_valid(pfn)) |
bf4bea8e | 157 | return PageReserved(pfn_to_page(pfn)); |
cbff90a7 BAY |
158 | |
159 | return true; | |
160 | } | |
161 | ||
bccf2150 AK |
162 | /* |
163 | * Switches to specified vcpu, until a matching vcpu_put() | |
164 | */ | |
ec7660cc | 165 | void vcpu_load(struct kvm_vcpu *vcpu) |
6aa8b732 | 166 | { |
ec7660cc | 167 | int cpu = get_cpu(); |
15ad7146 | 168 | preempt_notifier_register(&vcpu->preempt_notifier); |
313a3dc7 | 169 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 | 170 | put_cpu(); |
6aa8b732 | 171 | } |
2f1fe811 | 172 | EXPORT_SYMBOL_GPL(vcpu_load); |
6aa8b732 | 173 | |
313a3dc7 | 174 | void vcpu_put(struct kvm_vcpu *vcpu) |
6aa8b732 | 175 | { |
15ad7146 | 176 | preempt_disable(); |
313a3dc7 | 177 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
178 | preempt_notifier_unregister(&vcpu->preempt_notifier); |
179 | preempt_enable(); | |
6aa8b732 | 180 | } |
2f1fe811 | 181 | EXPORT_SYMBOL_GPL(vcpu_put); |
6aa8b732 | 182 | |
7a97cec2 PB |
183 | /* TODO: merge with kvm_arch_vcpu_should_kick */ |
184 | static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req) | |
185 | { | |
186 | int mode = kvm_vcpu_exiting_guest_mode(vcpu); | |
187 | ||
188 | /* | |
189 | * We need to wait for the VCPU to reenable interrupts and get out of | |
190 | * READING_SHADOW_PAGE_TABLES mode. | |
191 | */ | |
192 | if (req & KVM_REQUEST_WAIT) | |
193 | return mode != OUTSIDE_GUEST_MODE; | |
194 | ||
195 | /* | |
196 | * Need to kick a running VCPU, but otherwise there is nothing to do. | |
197 | */ | |
198 | return mode == IN_GUEST_MODE; | |
199 | } | |
200 | ||
d9e368d6 AK |
201 | static void ack_flush(void *_completed) |
202 | { | |
d9e368d6 AK |
203 | } |
204 | ||
b49defe8 PB |
205 | static inline bool kvm_kick_many_cpus(const struct cpumask *cpus, bool wait) |
206 | { | |
207 | if (unlikely(!cpus)) | |
208 | cpus = cpu_online_mask; | |
209 | ||
210 | if (cpumask_empty(cpus)) | |
211 | return false; | |
212 | ||
213 | smp_call_function_many(cpus, ack_flush, NULL, wait); | |
214 | return true; | |
215 | } | |
216 | ||
7053df4e VK |
217 | bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req, |
218 | unsigned long *vcpu_bitmap, cpumask_var_t tmp) | |
d9e368d6 | 219 | { |
597a5f55 | 220 | int i, cpu, me; |
d9e368d6 | 221 | struct kvm_vcpu *vcpu; |
7053df4e | 222 | bool called; |
6ef7a1bc | 223 | |
3cba4130 | 224 | me = get_cpu(); |
7053df4e | 225 | |
988a2cae | 226 | kvm_for_each_vcpu(i, vcpu, kvm) { |
a812297c | 227 | if (vcpu_bitmap && !test_bit(i, vcpu_bitmap)) |
7053df4e VK |
228 | continue; |
229 | ||
3cba4130 | 230 | kvm_make_request(req, vcpu); |
d9e368d6 | 231 | cpu = vcpu->cpu; |
6b7e2d09 | 232 | |
178f02ff RK |
233 | if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu)) |
234 | continue; | |
6c6e8360 | 235 | |
7053df4e | 236 | if (tmp != NULL && cpu != -1 && cpu != me && |
7a97cec2 | 237 | kvm_request_needs_ipi(vcpu, req)) |
7053df4e | 238 | __cpumask_set_cpu(cpu, tmp); |
49846896 | 239 | } |
7053df4e VK |
240 | |
241 | called = kvm_kick_many_cpus(tmp, !!(req & KVM_REQUEST_WAIT)); | |
3cba4130 | 242 | put_cpu(); |
7053df4e VK |
243 | |
244 | return called; | |
245 | } | |
246 | ||
247 | bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req) | |
248 | { | |
249 | cpumask_var_t cpus; | |
250 | bool called; | |
7053df4e VK |
251 | |
252 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); | |
253 | ||
a812297c | 254 | called = kvm_make_vcpus_request_mask(kvm, req, NULL, cpus); |
7053df4e | 255 | |
6ef7a1bc | 256 | free_cpumask_var(cpus); |
49846896 | 257 | return called; |
d9e368d6 AK |
258 | } |
259 | ||
a6d51016 | 260 | #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL |
49846896 | 261 | void kvm_flush_remote_tlbs(struct kvm *kvm) |
2e53d63a | 262 | { |
4ae3cb3a LT |
263 | /* |
264 | * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in | |
265 | * kvm_make_all_cpus_request. | |
266 | */ | |
267 | long dirty_count = smp_load_acquire(&kvm->tlbs_dirty); | |
268 | ||
269 | /* | |
270 | * We want to publish modifications to the page tables before reading | |
271 | * mode. Pairs with a memory barrier in arch-specific code. | |
272 | * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest | |
273 | * and smp_mb in walk_shadow_page_lockless_begin/end. | |
274 | * - powerpc: smp_mb in kvmppc_prepare_to_enter. | |
275 | * | |
276 | * There is already an smp_mb__after_atomic() before | |
277 | * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that | |
278 | * barrier here. | |
279 | */ | |
b08660e5 TL |
280 | if (!kvm_arch_flush_remote_tlb(kvm) |
281 | || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)) | |
49846896 | 282 | ++kvm->stat.remote_tlb_flush; |
a086f6a1 | 283 | cmpxchg(&kvm->tlbs_dirty, dirty_count, 0); |
2e53d63a | 284 | } |
2ba9f0d8 | 285 | EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs); |
a6d51016 | 286 | #endif |
2e53d63a | 287 | |
49846896 RR |
288 | void kvm_reload_remote_mmus(struct kvm *kvm) |
289 | { | |
445b8236 | 290 | kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD); |
49846896 | 291 | } |
2e53d63a | 292 | |
fb3f0f51 RR |
293 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) |
294 | { | |
295 | struct page *page; | |
296 | int r; | |
297 | ||
298 | mutex_init(&vcpu->mutex); | |
299 | vcpu->cpu = -1; | |
fb3f0f51 RR |
300 | vcpu->kvm = kvm; |
301 | vcpu->vcpu_id = id; | |
34bb10b7 | 302 | vcpu->pid = NULL; |
8577370f | 303 | init_swait_queue_head(&vcpu->wq); |
af585b92 | 304 | kvm_async_pf_vcpu_init(vcpu); |
fb3f0f51 | 305 | |
bf9f6ac8 FW |
306 | vcpu->pre_pcpu = -1; |
307 | INIT_LIST_HEAD(&vcpu->blocked_vcpu_list); | |
308 | ||
fb3f0f51 RR |
309 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
310 | if (!page) { | |
311 | r = -ENOMEM; | |
312 | goto fail; | |
313 | } | |
314 | vcpu->run = page_address(page); | |
315 | ||
4c088493 R |
316 | kvm_vcpu_set_in_spin_loop(vcpu, false); |
317 | kvm_vcpu_set_dy_eligible(vcpu, false); | |
3a08a8f9 | 318 | vcpu->preempted = false; |
4c088493 | 319 | |
e9b11c17 | 320 | r = kvm_arch_vcpu_init(vcpu); |
fb3f0f51 | 321 | if (r < 0) |
e9b11c17 | 322 | goto fail_free_run; |
fb3f0f51 RR |
323 | return 0; |
324 | ||
fb3f0f51 RR |
325 | fail_free_run: |
326 | free_page((unsigned long)vcpu->run); | |
327 | fail: | |
76fafa5e | 328 | return r; |
fb3f0f51 RR |
329 | } |
330 | EXPORT_SYMBOL_GPL(kvm_vcpu_init); | |
331 | ||
332 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu) | |
333 | { | |
0e4524a5 CB |
334 | /* |
335 | * no need for rcu_read_lock as VCPU_RUN is the only place that | |
336 | * will change the vcpu->pid pointer and on uninit all file | |
337 | * descriptors are already gone. | |
338 | */ | |
339 | put_pid(rcu_dereference_protected(vcpu->pid, 1)); | |
e9b11c17 | 340 | kvm_arch_vcpu_uninit(vcpu); |
fb3f0f51 RR |
341 | free_page((unsigned long)vcpu->run); |
342 | } | |
343 | EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); | |
344 | ||
e930bffe AA |
345 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
346 | static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) | |
347 | { | |
348 | return container_of(mn, struct kvm, mmu_notifier); | |
349 | } | |
350 | ||
3da0dd43 IE |
351 | static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, |
352 | struct mm_struct *mm, | |
353 | unsigned long address, | |
354 | pte_t pte) | |
355 | { | |
356 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 357 | int idx; |
3da0dd43 | 358 | |
bc6678a3 | 359 | idx = srcu_read_lock(&kvm->srcu); |
3da0dd43 IE |
360 | spin_lock(&kvm->mmu_lock); |
361 | kvm->mmu_notifier_seq++; | |
0cf853c5 LT |
362 | |
363 | if (kvm_set_spte_hva(kvm, address, pte)) | |
364 | kvm_flush_remote_tlbs(kvm); | |
365 | ||
3da0dd43 | 366 | spin_unlock(&kvm->mmu_lock); |
bc6678a3 | 367 | srcu_read_unlock(&kvm->srcu, idx); |
3da0dd43 IE |
368 | } |
369 | ||
93065ac7 | 370 | static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, |
5d6527a7 | 371 | const struct mmu_notifier_range *range) |
e930bffe AA |
372 | { |
373 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 374 | int need_tlb_flush = 0, idx; |
93065ac7 | 375 | int ret; |
e930bffe | 376 | |
bc6678a3 | 377 | idx = srcu_read_lock(&kvm->srcu); |
e930bffe AA |
378 | spin_lock(&kvm->mmu_lock); |
379 | /* | |
380 | * The count increase must become visible at unlock time as no | |
381 | * spte can be established without taking the mmu_lock and | |
382 | * count is also read inside the mmu_lock critical section. | |
383 | */ | |
384 | kvm->mmu_notifier_count++; | |
5d6527a7 | 385 | need_tlb_flush = kvm_unmap_hva_range(kvm, range->start, range->end); |
a4ee1ca4 | 386 | need_tlb_flush |= kvm->tlbs_dirty; |
e930bffe AA |
387 | /* we've to flush the tlb before the pages can be freed */ |
388 | if (need_tlb_flush) | |
389 | kvm_flush_remote_tlbs(kvm); | |
565f3be2 TY |
390 | |
391 | spin_unlock(&kvm->mmu_lock); | |
b1394e74 | 392 | |
5d6527a7 JG |
393 | ret = kvm_arch_mmu_notifier_invalidate_range(kvm, range->start, |
394 | range->end, range->blockable); | |
b1394e74 | 395 | |
565f3be2 | 396 | srcu_read_unlock(&kvm->srcu, idx); |
93065ac7 MH |
397 | |
398 | return ret; | |
e930bffe AA |
399 | } |
400 | ||
401 | static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, | |
5d6527a7 | 402 | const struct mmu_notifier_range *range) |
e930bffe AA |
403 | { |
404 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
405 | ||
406 | spin_lock(&kvm->mmu_lock); | |
407 | /* | |
408 | * This sequence increase will notify the kvm page fault that | |
409 | * the page that is going to be mapped in the spte could have | |
410 | * been freed. | |
411 | */ | |
412 | kvm->mmu_notifier_seq++; | |
a355aa54 | 413 | smp_wmb(); |
e930bffe AA |
414 | /* |
415 | * The above sequence increase must be visible before the | |
a355aa54 PM |
416 | * below count decrease, which is ensured by the smp_wmb above |
417 | * in conjunction with the smp_rmb in mmu_notifier_retry(). | |
e930bffe AA |
418 | */ |
419 | kvm->mmu_notifier_count--; | |
420 | spin_unlock(&kvm->mmu_lock); | |
421 | ||
422 | BUG_ON(kvm->mmu_notifier_count < 0); | |
423 | } | |
424 | ||
425 | static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, | |
426 | struct mm_struct *mm, | |
57128468 ALC |
427 | unsigned long start, |
428 | unsigned long end) | |
e930bffe AA |
429 | { |
430 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
bc6678a3 | 431 | int young, idx; |
e930bffe | 432 | |
bc6678a3 | 433 | idx = srcu_read_lock(&kvm->srcu); |
e930bffe | 434 | spin_lock(&kvm->mmu_lock); |
e930bffe | 435 | |
57128468 | 436 | young = kvm_age_hva(kvm, start, end); |
e930bffe AA |
437 | if (young) |
438 | kvm_flush_remote_tlbs(kvm); | |
439 | ||
565f3be2 TY |
440 | spin_unlock(&kvm->mmu_lock); |
441 | srcu_read_unlock(&kvm->srcu, idx); | |
442 | ||
e930bffe AA |
443 | return young; |
444 | } | |
445 | ||
1d7715c6 VD |
446 | static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, |
447 | struct mm_struct *mm, | |
448 | unsigned long start, | |
449 | unsigned long end) | |
450 | { | |
451 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
452 | int young, idx; | |
453 | ||
454 | idx = srcu_read_lock(&kvm->srcu); | |
455 | spin_lock(&kvm->mmu_lock); | |
456 | /* | |
457 | * Even though we do not flush TLB, this will still adversely | |
458 | * affect performance on pre-Haswell Intel EPT, where there is | |
459 | * no EPT Access Bit to clear so that we have to tear down EPT | |
460 | * tables instead. If we find this unacceptable, we can always | |
461 | * add a parameter to kvm_age_hva so that it effectively doesn't | |
462 | * do anything on clear_young. | |
463 | * | |
464 | * Also note that currently we never issue secondary TLB flushes | |
465 | * from clear_young, leaving this job up to the regular system | |
466 | * cadence. If we find this inaccurate, we might come up with a | |
467 | * more sophisticated heuristic later. | |
468 | */ | |
469 | young = kvm_age_hva(kvm, start, end); | |
470 | spin_unlock(&kvm->mmu_lock); | |
471 | srcu_read_unlock(&kvm->srcu, idx); | |
472 | ||
473 | return young; | |
474 | } | |
475 | ||
8ee53820 AA |
476 | static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, |
477 | struct mm_struct *mm, | |
478 | unsigned long address) | |
479 | { | |
480 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
481 | int young, idx; | |
482 | ||
483 | idx = srcu_read_lock(&kvm->srcu); | |
484 | spin_lock(&kvm->mmu_lock); | |
485 | young = kvm_test_age_hva(kvm, address); | |
486 | spin_unlock(&kvm->mmu_lock); | |
487 | srcu_read_unlock(&kvm->srcu, idx); | |
488 | ||
489 | return young; | |
490 | } | |
491 | ||
85db06e5 MT |
492 | static void kvm_mmu_notifier_release(struct mmu_notifier *mn, |
493 | struct mm_struct *mm) | |
494 | { | |
495 | struct kvm *kvm = mmu_notifier_to_kvm(mn); | |
eda2beda LJ |
496 | int idx; |
497 | ||
498 | idx = srcu_read_lock(&kvm->srcu); | |
2df72e9b | 499 | kvm_arch_flush_shadow_all(kvm); |
eda2beda | 500 | srcu_read_unlock(&kvm->srcu, idx); |
85db06e5 MT |
501 | } |
502 | ||
e930bffe | 503 | static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { |
e930bffe AA |
504 | .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, |
505 | .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, | |
506 | .clear_flush_young = kvm_mmu_notifier_clear_flush_young, | |
1d7715c6 | 507 | .clear_young = kvm_mmu_notifier_clear_young, |
8ee53820 | 508 | .test_young = kvm_mmu_notifier_test_young, |
3da0dd43 | 509 | .change_pte = kvm_mmu_notifier_change_pte, |
85db06e5 | 510 | .release = kvm_mmu_notifier_release, |
e930bffe | 511 | }; |
4c07b0a4 AK |
512 | |
513 | static int kvm_init_mmu_notifier(struct kvm *kvm) | |
514 | { | |
515 | kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops; | |
516 | return mmu_notifier_register(&kvm->mmu_notifier, current->mm); | |
517 | } | |
518 | ||
519 | #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ | |
520 | ||
521 | static int kvm_init_mmu_notifier(struct kvm *kvm) | |
522 | { | |
523 | return 0; | |
524 | } | |
525 | ||
e930bffe AA |
526 | #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ |
527 | ||
a47d2b07 | 528 | static struct kvm_memslots *kvm_alloc_memslots(void) |
bf3e05bc XG |
529 | { |
530 | int i; | |
a47d2b07 | 531 | struct kvm_memslots *slots; |
bf3e05bc | 532 | |
b12ce36a | 533 | slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT); |
a47d2b07 PB |
534 | if (!slots) |
535 | return NULL; | |
536 | ||
bf3e05bc | 537 | for (i = 0; i < KVM_MEM_SLOTS_NUM; i++) |
f85e2cb5 | 538 | slots->id_to_index[i] = slots->memslots[i].id = i; |
a47d2b07 PB |
539 | |
540 | return slots; | |
541 | } | |
542 | ||
543 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) | |
544 | { | |
545 | if (!memslot->dirty_bitmap) | |
546 | return; | |
547 | ||
548 | kvfree(memslot->dirty_bitmap); | |
549 | memslot->dirty_bitmap = NULL; | |
550 | } | |
551 | ||
552 | /* | |
553 | * Free any memory in @free but not in @dont. | |
554 | */ | |
555 | static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, | |
556 | struct kvm_memory_slot *dont) | |
557 | { | |
558 | if (!dont || free->dirty_bitmap != dont->dirty_bitmap) | |
559 | kvm_destroy_dirty_bitmap(free); | |
560 | ||
561 | kvm_arch_free_memslot(kvm, free, dont); | |
562 | ||
563 | free->npages = 0; | |
564 | } | |
565 | ||
566 | static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots) | |
567 | { | |
568 | struct kvm_memory_slot *memslot; | |
569 | ||
570 | if (!slots) | |
571 | return; | |
572 | ||
573 | kvm_for_each_memslot(memslot, slots) | |
574 | kvm_free_memslot(kvm, memslot, NULL); | |
575 | ||
576 | kvfree(slots); | |
bf3e05bc XG |
577 | } |
578 | ||
536a6f88 JF |
579 | static void kvm_destroy_vm_debugfs(struct kvm *kvm) |
580 | { | |
581 | int i; | |
582 | ||
583 | if (!kvm->debugfs_dentry) | |
584 | return; | |
585 | ||
586 | debugfs_remove_recursive(kvm->debugfs_dentry); | |
587 | ||
9d5a1dce LC |
588 | if (kvm->debugfs_stat_data) { |
589 | for (i = 0; i < kvm_debugfs_num_entries; i++) | |
590 | kfree(kvm->debugfs_stat_data[i]); | |
591 | kfree(kvm->debugfs_stat_data); | |
592 | } | |
536a6f88 JF |
593 | } |
594 | ||
595 | static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) | |
596 | { | |
597 | char dir_name[ITOA_MAX_LEN * 2]; | |
598 | struct kvm_stat_data *stat_data; | |
599 | struct kvm_stats_debugfs_item *p; | |
600 | ||
601 | if (!debugfs_initialized()) | |
602 | return 0; | |
603 | ||
604 | snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd); | |
929f45e3 | 605 | kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir); |
536a6f88 JF |
606 | |
607 | kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries, | |
608 | sizeof(*kvm->debugfs_stat_data), | |
b12ce36a | 609 | GFP_KERNEL_ACCOUNT); |
536a6f88 JF |
610 | if (!kvm->debugfs_stat_data) |
611 | return -ENOMEM; | |
612 | ||
613 | for (p = debugfs_entries; p->name; p++) { | |
b12ce36a | 614 | stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT); |
536a6f88 JF |
615 | if (!stat_data) |
616 | return -ENOMEM; | |
617 | ||
618 | stat_data->kvm = kvm; | |
619 | stat_data->offset = p->offset; | |
620 | kvm->debugfs_stat_data[p - debugfs_entries] = stat_data; | |
929f45e3 GKH |
621 | debugfs_create_file(p->name, 0644, kvm->debugfs_dentry, |
622 | stat_data, stat_fops_per_vm[p->kind]); | |
536a6f88 JF |
623 | } |
624 | return 0; | |
625 | } | |
626 | ||
e08b9637 | 627 | static struct kvm *kvm_create_vm(unsigned long type) |
6aa8b732 | 628 | { |
d89f5eff JK |
629 | int r, i; |
630 | struct kvm *kvm = kvm_arch_alloc_vm(); | |
6aa8b732 | 631 | |
d89f5eff JK |
632 | if (!kvm) |
633 | return ERR_PTR(-ENOMEM); | |
634 | ||
e9ad4ec8 | 635 | spin_lock_init(&kvm->mmu_lock); |
f1f10076 | 636 | mmgrab(current->mm); |
e9ad4ec8 PB |
637 | kvm->mm = current->mm; |
638 | kvm_eventfd_init(kvm); | |
639 | mutex_init(&kvm->lock); | |
640 | mutex_init(&kvm->irq_lock); | |
641 | mutex_init(&kvm->slots_lock); | |
e3736c3e | 642 | refcount_set(&kvm->users_count, 1); |
e9ad4ec8 PB |
643 | INIT_LIST_HEAD(&kvm->devices); |
644 | ||
e08b9637 | 645 | r = kvm_arch_init_vm(kvm, type); |
d89f5eff | 646 | if (r) |
719d93cd | 647 | goto out_err_no_disable; |
10474ae8 AG |
648 | |
649 | r = hardware_enable_all(); | |
650 | if (r) | |
719d93cd | 651 | goto out_err_no_disable; |
10474ae8 | 652 | |
c77dcacb | 653 | #ifdef CONFIG_HAVE_KVM_IRQFD |
136bdfee | 654 | INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
75858a84 | 655 | #endif |
6aa8b732 | 656 | |
1e702d9a AW |
657 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); |
658 | ||
46a26bf5 | 659 | r = -ENOMEM; |
f481b069 | 660 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
4bd518f1 PB |
661 | struct kvm_memslots *slots = kvm_alloc_memslots(); |
662 | if (!slots) | |
f481b069 | 663 | goto out_err_no_srcu; |
0e32958e | 664 | /* Generations must be different for each address space. */ |
164bf7e5 | 665 | slots->generation = i; |
4bd518f1 | 666 | rcu_assign_pointer(kvm->memslots[i], slots); |
f481b069 | 667 | } |
00f034a1 | 668 | |
bc6678a3 | 669 | if (init_srcu_struct(&kvm->srcu)) |
719d93cd CB |
670 | goto out_err_no_srcu; |
671 | if (init_srcu_struct(&kvm->irq_srcu)) | |
672 | goto out_err_no_irq_srcu; | |
e93f8a0f | 673 | for (i = 0; i < KVM_NR_BUSES; i++) { |
4a12f951 | 674 | rcu_assign_pointer(kvm->buses[i], |
b12ce36a | 675 | kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); |
57e7fbee | 676 | if (!kvm->buses[i]) |
e93f8a0f | 677 | goto out_err; |
e93f8a0f | 678 | } |
e930bffe | 679 | |
74b5c5bf MW |
680 | r = kvm_init_mmu_notifier(kvm); |
681 | if (r) | |
682 | goto out_err; | |
683 | ||
2f303b74 | 684 | spin_lock(&kvm_lock); |
5e58cfe4 | 685 | list_add(&kvm->vm_list, &vm_list); |
2f303b74 | 686 | spin_unlock(&kvm_lock); |
d89f5eff | 687 | |
2ecd9d29 PZ |
688 | preempt_notifier_inc(); |
689 | ||
f17abe9a | 690 | return kvm; |
10474ae8 AG |
691 | |
692 | out_err: | |
719d93cd CB |
693 | cleanup_srcu_struct(&kvm->irq_srcu); |
694 | out_err_no_irq_srcu: | |
57e7fbee | 695 | cleanup_srcu_struct(&kvm->srcu); |
719d93cd | 696 | out_err_no_srcu: |
10474ae8 | 697 | hardware_disable_all(); |
719d93cd | 698 | out_err_no_disable: |
021086e3 | 699 | refcount_set(&kvm->users_count, 0); |
e93f8a0f | 700 | for (i = 0; i < KVM_NR_BUSES; i++) |
3898da94 | 701 | kfree(kvm_get_bus(kvm, i)); |
f481b069 | 702 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
3898da94 | 703 | kvm_free_memslots(kvm, __kvm_memslots(kvm, i)); |
d89f5eff | 704 | kvm_arch_free_vm(kvm); |
e9ad4ec8 | 705 | mmdrop(current->mm); |
10474ae8 | 706 | return ERR_PTR(r); |
f17abe9a AK |
707 | } |
708 | ||
07f0a7bd SW |
709 | static void kvm_destroy_devices(struct kvm *kvm) |
710 | { | |
e6e3b5a6 | 711 | struct kvm_device *dev, *tmp; |
07f0a7bd | 712 | |
a28ebea2 CD |
713 | /* |
714 | * We do not need to take the kvm->lock here, because nobody else | |
715 | * has a reference to the struct kvm at this point and therefore | |
716 | * cannot access the devices list anyhow. | |
717 | */ | |
e6e3b5a6 GT |
718 | list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) { |
719 | list_del(&dev->vm_node); | |
07f0a7bd SW |
720 | dev->ops->destroy(dev); |
721 | } | |
722 | } | |
723 | ||
f17abe9a AK |
724 | static void kvm_destroy_vm(struct kvm *kvm) |
725 | { | |
e93f8a0f | 726 | int i; |
6d4e4c4f AK |
727 | struct mm_struct *mm = kvm->mm; |
728 | ||
286de8f6 | 729 | kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm); |
536a6f88 | 730 | kvm_destroy_vm_debugfs(kvm); |
ad8ba2cd | 731 | kvm_arch_sync_events(kvm); |
2f303b74 | 732 | spin_lock(&kvm_lock); |
133de902 | 733 | list_del(&kvm->vm_list); |
2f303b74 | 734 | spin_unlock(&kvm_lock); |
399ec807 | 735 | kvm_free_irq_routing(kvm); |
df630b8c | 736 | for (i = 0; i < KVM_NR_BUSES; i++) { |
3898da94 | 737 | struct kvm_io_bus *bus = kvm_get_bus(kvm, i); |
4a12f951 | 738 | |
4a12f951 CB |
739 | if (bus) |
740 | kvm_io_bus_destroy(bus); | |
df630b8c PX |
741 | kvm->buses[i] = NULL; |
742 | } | |
980da6ce | 743 | kvm_coalesced_mmio_free(kvm); |
e930bffe AA |
744 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
745 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); | |
f00be0ca | 746 | #else |
2df72e9b | 747 | kvm_arch_flush_shadow_all(kvm); |
5f94c174 | 748 | #endif |
d19a9cd2 | 749 | kvm_arch_destroy_vm(kvm); |
07f0a7bd | 750 | kvm_destroy_devices(kvm); |
f481b069 | 751 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |
3898da94 | 752 | kvm_free_memslots(kvm, __kvm_memslots(kvm, i)); |
820b3fcd | 753 | cleanup_srcu_struct(&kvm->irq_srcu); |
d89f5eff JK |
754 | cleanup_srcu_struct(&kvm->srcu); |
755 | kvm_arch_free_vm(kvm); | |
2ecd9d29 | 756 | preempt_notifier_dec(); |
10474ae8 | 757 | hardware_disable_all(); |
6d4e4c4f | 758 | mmdrop(mm); |
f17abe9a AK |
759 | } |
760 | ||
d39f13b0 IE |
761 | void kvm_get_kvm(struct kvm *kvm) |
762 | { | |
e3736c3e | 763 | refcount_inc(&kvm->users_count); |
d39f13b0 IE |
764 | } |
765 | EXPORT_SYMBOL_GPL(kvm_get_kvm); | |
766 | ||
767 | void kvm_put_kvm(struct kvm *kvm) | |
768 | { | |
e3736c3e | 769 | if (refcount_dec_and_test(&kvm->users_count)) |
d39f13b0 IE |
770 | kvm_destroy_vm(kvm); |
771 | } | |
772 | EXPORT_SYMBOL_GPL(kvm_put_kvm); | |
773 | ||
774 | ||
f17abe9a AK |
775 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
776 | { | |
777 | struct kvm *kvm = filp->private_data; | |
778 | ||
721eecbf GH |
779 | kvm_irqfd_release(kvm); |
780 | ||
d39f13b0 | 781 | kvm_put_kvm(kvm); |
6aa8b732 AK |
782 | return 0; |
783 | } | |
784 | ||
515a0127 TY |
785 | /* |
786 | * Allocation size is twice as large as the actual dirty bitmap size. | |
93474b25 | 787 | * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed. |
515a0127 | 788 | */ |
a36a57b1 TY |
789 | static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) |
790 | { | |
515a0127 | 791 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); |
a36a57b1 | 792 | |
b12ce36a | 793 | memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT); |
a36a57b1 TY |
794 | if (!memslot->dirty_bitmap) |
795 | return -ENOMEM; | |
796 | ||
a36a57b1 TY |
797 | return 0; |
798 | } | |
799 | ||
bf3e05bc | 800 | /* |
0e60b079 IM |
801 | * Insert memslot and re-sort memslots based on their GFN, |
802 | * so binary search could be used to lookup GFN. | |
803 | * Sorting algorithm takes advantage of having initially | |
804 | * sorted array and known changed memslot position. | |
bf3e05bc | 805 | */ |
5cc15027 | 806 | static void update_memslots(struct kvm_memslots *slots, |
31fc4f95 WY |
807 | struct kvm_memory_slot *new, |
808 | enum kvm_mr_change change) | |
bf3e05bc | 809 | { |
8593176c PB |
810 | int id = new->id; |
811 | int i = slots->id_to_index[id]; | |
063584d4 | 812 | struct kvm_memory_slot *mslots = slots->memslots; |
f85e2cb5 | 813 | |
8593176c | 814 | WARN_ON(mslots[i].id != id); |
31fc4f95 WY |
815 | switch (change) { |
816 | case KVM_MR_CREATE: | |
817 | slots->used_slots++; | |
818 | WARN_ON(mslots[i].npages || !new->npages); | |
819 | break; | |
820 | case KVM_MR_DELETE: | |
821 | slots->used_slots--; | |
822 | WARN_ON(new->npages || !mslots[i].npages); | |
823 | break; | |
824 | default: | |
825 | break; | |
9c1a5d38 | 826 | } |
0e60b079 | 827 | |
7f379cff | 828 | while (i < KVM_MEM_SLOTS_NUM - 1 && |
0e60b079 IM |
829 | new->base_gfn <= mslots[i + 1].base_gfn) { |
830 | if (!mslots[i + 1].npages) | |
831 | break; | |
7f379cff IM |
832 | mslots[i] = mslots[i + 1]; |
833 | slots->id_to_index[mslots[i].id] = i; | |
834 | i++; | |
835 | } | |
efbeec70 PB |
836 | |
837 | /* | |
838 | * The ">=" is needed when creating a slot with base_gfn == 0, | |
839 | * so that it moves before all those with base_gfn == npages == 0. | |
840 | * | |
841 | * On the other hand, if new->npages is zero, the above loop has | |
842 | * already left i pointing to the beginning of the empty part of | |
843 | * mslots, and the ">=" would move the hole backwards in this | |
844 | * case---which is wrong. So skip the loop when deleting a slot. | |
845 | */ | |
846 | if (new->npages) { | |
847 | while (i > 0 && | |
848 | new->base_gfn >= mslots[i - 1].base_gfn) { | |
849 | mslots[i] = mslots[i - 1]; | |
850 | slots->id_to_index[mslots[i].id] = i; | |
851 | i--; | |
852 | } | |
dbaff309 PB |
853 | } else |
854 | WARN_ON_ONCE(i != slots->used_slots); | |
f85e2cb5 | 855 | |
8593176c PB |
856 | mslots[i] = *new; |
857 | slots->id_to_index[mslots[i].id] = i; | |
bf3e05bc XG |
858 | } |
859 | ||
09170a49 | 860 | static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem) |
a50d64d6 | 861 | { |
4d8b81ab XG |
862 | u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; |
863 | ||
0f8a4de3 | 864 | #ifdef __KVM_HAVE_READONLY_MEM |
4d8b81ab XG |
865 | valid_flags |= KVM_MEM_READONLY; |
866 | #endif | |
867 | ||
868 | if (mem->flags & ~valid_flags) | |
a50d64d6 XG |
869 | return -EINVAL; |
870 | ||
871 | return 0; | |
872 | } | |
873 | ||
7ec4fb44 | 874 | static struct kvm_memslots *install_new_memslots(struct kvm *kvm, |
f481b069 | 875 | int as_id, struct kvm_memslots *slots) |
7ec4fb44 | 876 | { |
f481b069 | 877 | struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id); |
361209e0 | 878 | u64 gen = old_memslots->generation; |
7ec4fb44 | 879 | |
361209e0 SC |
880 | WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS); |
881 | slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; | |
ee3d1570 | 882 | |
f481b069 | 883 | rcu_assign_pointer(kvm->memslots[as_id], slots); |
7ec4fb44 | 884 | synchronize_srcu_expedited(&kvm->srcu); |
e59dbe09 | 885 | |
ee3d1570 | 886 | /* |
361209e0 SC |
887 | * Increment the new memslot generation a second time, dropping the |
888 | * update in-progress flag and incrementing then generation based on | |
889 | * the number of address spaces. This provides a unique and easily | |
890 | * identifiable generation number while the memslots are in flux. | |
891 | */ | |
892 | gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS; | |
893 | ||
894 | /* | |
4bd518f1 PB |
895 | * Generations must be unique even across address spaces. We do not need |
896 | * a global counter for that, instead the generation space is evenly split | |
897 | * across address spaces. For example, with two address spaces, address | |
164bf7e5 SC |
898 | * space 0 will use generations 0, 2, 4, ... while address space 1 will |
899 | * use generations 1, 3, 5, ... | |
ee3d1570 | 900 | */ |
164bf7e5 | 901 | gen += KVM_ADDRESS_SPACE_NUM; |
ee3d1570 | 902 | |
15248258 | 903 | kvm_arch_memslots_updated(kvm, gen); |
ee3d1570 | 904 | |
15248258 | 905 | slots->generation = gen; |
e59dbe09 TY |
906 | |
907 | return old_memslots; | |
7ec4fb44 GN |
908 | } |
909 | ||
6aa8b732 AK |
910 | /* |
911 | * Allocate some memory and give it an address in the guest physical address | |
912 | * space. | |
913 | * | |
914 | * Discontiguous memory is allowed, mostly for framebuffers. | |
f78e0e2e | 915 | * |
02d5d55b | 916 | * Must be called holding kvm->slots_lock for write. |
6aa8b732 | 917 | */ |
f78e0e2e | 918 | int __kvm_set_memory_region(struct kvm *kvm, |
09170a49 | 919 | const struct kvm_userspace_memory_region *mem) |
6aa8b732 | 920 | { |
8234b22e | 921 | int r; |
6aa8b732 | 922 | gfn_t base_gfn; |
28bcb112 | 923 | unsigned long npages; |
a843fac2 | 924 | struct kvm_memory_slot *slot; |
6aa8b732 | 925 | struct kvm_memory_slot old, new; |
b7f69c55 | 926 | struct kvm_memslots *slots = NULL, *old_memslots; |
f481b069 | 927 | int as_id, id; |
f64c0398 | 928 | enum kvm_mr_change change; |
6aa8b732 | 929 | |
a50d64d6 XG |
930 | r = check_memory_region_flags(mem); |
931 | if (r) | |
932 | goto out; | |
933 | ||
6aa8b732 | 934 | r = -EINVAL; |
f481b069 PB |
935 | as_id = mem->slot >> 16; |
936 | id = (u16)mem->slot; | |
937 | ||
6aa8b732 AK |
938 | /* General sanity checks */ |
939 | if (mem->memory_size & (PAGE_SIZE - 1)) | |
940 | goto out; | |
941 | if (mem->guest_phys_addr & (PAGE_SIZE - 1)) | |
942 | goto out; | |
fa3d315a | 943 | /* We can read the guest memory with __xxx_user() later on. */ |
f481b069 | 944 | if ((id < KVM_USER_MEM_SLOTS) && |
fa3d315a | 945 | ((mem->userspace_addr & (PAGE_SIZE - 1)) || |
96d4f267 | 946 | !access_ok((void __user *)(unsigned long)mem->userspace_addr, |
9e3bb6b6 | 947 | mem->memory_size))) |
78749809 | 948 | goto out; |
f481b069 | 949 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) |
6aa8b732 AK |
950 | goto out; |
951 | if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) | |
952 | goto out; | |
953 | ||
f481b069 | 954 | slot = id_to_memslot(__kvm_memslots(kvm, as_id), id); |
6aa8b732 AK |
955 | base_gfn = mem->guest_phys_addr >> PAGE_SHIFT; |
956 | npages = mem->memory_size >> PAGE_SHIFT; | |
957 | ||
660c22c4 TY |
958 | if (npages > KVM_MEM_MAX_NR_PAGES) |
959 | goto out; | |
960 | ||
a843fac2 | 961 | new = old = *slot; |
6aa8b732 | 962 | |
f481b069 | 963 | new.id = id; |
6aa8b732 AK |
964 | new.base_gfn = base_gfn; |
965 | new.npages = npages; | |
966 | new.flags = mem->flags; | |
967 | ||
f64c0398 TY |
968 | if (npages) { |
969 | if (!old.npages) | |
970 | change = KVM_MR_CREATE; | |
971 | else { /* Modify an existing slot. */ | |
972 | if ((mem->userspace_addr != old.userspace_addr) || | |
75d61fbc TY |
973 | (npages != old.npages) || |
974 | ((new.flags ^ old.flags) & KVM_MEM_READONLY)) | |
f64c0398 TY |
975 | goto out; |
976 | ||
977 | if (base_gfn != old.base_gfn) | |
978 | change = KVM_MR_MOVE; | |
979 | else if (new.flags != old.flags) | |
980 | change = KVM_MR_FLAGS_ONLY; | |
981 | else { /* Nothing to change. */ | |
982 | r = 0; | |
983 | goto out; | |
984 | } | |
985 | } | |
09170a49 PB |
986 | } else { |
987 | if (!old.npages) | |
988 | goto out; | |
989 | ||
f64c0398 | 990 | change = KVM_MR_DELETE; |
09170a49 PB |
991 | new.base_gfn = 0; |
992 | new.flags = 0; | |
993 | } | |
6aa8b732 | 994 | |
f64c0398 | 995 | if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { |
0a706bee TY |
996 | /* Check for overlaps */ |
997 | r = -EEXIST; | |
f481b069 | 998 | kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) { |
b28676bb | 999 | if (slot->id == id) |
0a706bee TY |
1000 | continue; |
1001 | if (!((base_gfn + npages <= slot->base_gfn) || | |
1002 | (base_gfn >= slot->base_gfn + slot->npages))) | |
1003 | goto out; | |
1004 | } | |
6aa8b732 | 1005 | } |
6aa8b732 | 1006 | |
6aa8b732 AK |
1007 | /* Free page dirty bitmap if unneeded */ |
1008 | if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
8b6d44c7 | 1009 | new.dirty_bitmap = NULL; |
6aa8b732 AK |
1010 | |
1011 | r = -ENOMEM; | |
f64c0398 | 1012 | if (change == KVM_MR_CREATE) { |
189a2f7b | 1013 | new.userspace_addr = mem->userspace_addr; |
d89cc617 | 1014 | |
5587027c | 1015 | if (kvm_arch_create_memslot(kvm, &new, npages)) |
db3fe4eb | 1016 | goto out_free; |
6aa8b732 | 1017 | } |
ec04b260 | 1018 | |
6aa8b732 AK |
1019 | /* Allocate page dirty bitmap if needed */ |
1020 | if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) { | |
a36a57b1 | 1021 | if (kvm_create_dirty_bitmap(&new) < 0) |
f78e0e2e | 1022 | goto out_free; |
6aa8b732 AK |
1023 | } |
1024 | ||
b12ce36a | 1025 | slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT); |
f2a81036 PB |
1026 | if (!slots) |
1027 | goto out_free; | |
f481b069 | 1028 | memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots)); |
f2a81036 | 1029 | |
f64c0398 | 1030 | if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) { |
f481b069 | 1031 | slot = id_to_memslot(slots, id); |
28a37544 XG |
1032 | slot->flags |= KVM_MEMSLOT_INVALID; |
1033 | ||
f481b069 | 1034 | old_memslots = install_new_memslots(kvm, as_id, slots); |
bc6678a3 | 1035 | |
12d6e753 MT |
1036 | /* From this point no new shadow pages pointing to a deleted, |
1037 | * or moved, memslot will be created. | |
bc6678a3 MT |
1038 | * |
1039 | * validation of sp->gfn happens in: | |
b7d409de XL |
1040 | * - gfn_to_hva (kvm_read_guest, gfn_to_pfn) |
1041 | * - kvm_is_visible_gfn (mmu_check_roots) | |
bc6678a3 | 1042 | */ |
2df72e9b | 1043 | kvm_arch_flush_shadow_memslot(kvm, slot); |
f2a81036 PB |
1044 | |
1045 | /* | |
1046 | * We can re-use the old_memslots from above, the only difference | |
1047 | * from the currently installed memslots is the invalid flag. This | |
1048 | * will get overwritten by update_memslots anyway. | |
1049 | */ | |
b7f69c55 | 1050 | slots = old_memslots; |
bc6678a3 | 1051 | } |
34d4cb8f | 1052 | |
7b6195a9 | 1053 | r = kvm_arch_prepare_memory_region(kvm, &new, mem, change); |
f7784b8e | 1054 | if (r) |
b7f69c55 | 1055 | goto out_slots; |
f7784b8e | 1056 | |
a47d2b07 | 1057 | /* actual memory is freed via old in kvm_free_memslot below */ |
f64c0398 | 1058 | if (change == KVM_MR_DELETE) { |
bc6678a3 | 1059 | new.dirty_bitmap = NULL; |
db3fe4eb | 1060 | memset(&new.arch, 0, sizeof(new.arch)); |
bc6678a3 MT |
1061 | } |
1062 | ||
31fc4f95 | 1063 | update_memslots(slots, &new, change); |
f481b069 | 1064 | old_memslots = install_new_memslots(kvm, as_id, slots); |
3ad82a7e | 1065 | |
f36f3f28 | 1066 | kvm_arch_commit_memory_region(kvm, mem, &old, &new, change); |
82ce2c96 | 1067 | |
a47d2b07 | 1068 | kvm_free_memslot(kvm, &old, &new); |
74496134 | 1069 | kvfree(old_memslots); |
6aa8b732 AK |
1070 | return 0; |
1071 | ||
e40f193f | 1072 | out_slots: |
74496134 | 1073 | kvfree(slots); |
f78e0e2e | 1074 | out_free: |
a47d2b07 | 1075 | kvm_free_memslot(kvm, &new, &old); |
6aa8b732 AK |
1076 | out: |
1077 | return r; | |
210c7c4d | 1078 | } |
f78e0e2e SY |
1079 | EXPORT_SYMBOL_GPL(__kvm_set_memory_region); |
1080 | ||
1081 | int kvm_set_memory_region(struct kvm *kvm, | |
09170a49 | 1082 | const struct kvm_userspace_memory_region *mem) |
f78e0e2e SY |
1083 | { |
1084 | int r; | |
1085 | ||
79fac95e | 1086 | mutex_lock(&kvm->slots_lock); |
47ae31e2 | 1087 | r = __kvm_set_memory_region(kvm, mem); |
79fac95e | 1088 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
1089 | return r; |
1090 | } | |
210c7c4d IE |
1091 | EXPORT_SYMBOL_GPL(kvm_set_memory_region); |
1092 | ||
7940876e SH |
1093 | static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
1094 | struct kvm_userspace_memory_region *mem) | |
210c7c4d | 1095 | { |
f481b069 | 1096 | if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) |
e0d62c7f | 1097 | return -EINVAL; |
09170a49 | 1098 | |
47ae31e2 | 1099 | return kvm_set_memory_region(kvm, mem); |
6aa8b732 AK |
1100 | } |
1101 | ||
5bb064dc ZX |
1102 | int kvm_get_dirty_log(struct kvm *kvm, |
1103 | struct kvm_dirty_log *log, int *is_dirty) | |
6aa8b732 | 1104 | { |
9f6b8029 | 1105 | struct kvm_memslots *slots; |
6aa8b732 | 1106 | struct kvm_memory_slot *memslot; |
843574a3 | 1107 | int i, as_id, id; |
87bf6e7d | 1108 | unsigned long n; |
6aa8b732 AK |
1109 | unsigned long any = 0; |
1110 | ||
f481b069 PB |
1111 | as_id = log->slot >> 16; |
1112 | id = (u16)log->slot; | |
1113 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) | |
843574a3 | 1114 | return -EINVAL; |
6aa8b732 | 1115 | |
f481b069 PB |
1116 | slots = __kvm_memslots(kvm, as_id); |
1117 | memslot = id_to_memslot(slots, id); | |
6aa8b732 | 1118 | if (!memslot->dirty_bitmap) |
843574a3 | 1119 | return -ENOENT; |
6aa8b732 | 1120 | |
87bf6e7d | 1121 | n = kvm_dirty_bitmap_bytes(memslot); |
6aa8b732 | 1122 | |
cd1a4a98 | 1123 | for (i = 0; !any && i < n/sizeof(long); ++i) |
6aa8b732 AK |
1124 | any = memslot->dirty_bitmap[i]; |
1125 | ||
6aa8b732 | 1126 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
843574a3 | 1127 | return -EFAULT; |
6aa8b732 | 1128 | |
5bb064dc ZX |
1129 | if (any) |
1130 | *is_dirty = 1; | |
843574a3 | 1131 | return 0; |
6aa8b732 | 1132 | } |
2ba9f0d8 | 1133 | EXPORT_SYMBOL_GPL(kvm_get_dirty_log); |
6aa8b732 | 1134 | |
ba0513b5 MS |
1135 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
1136 | /** | |
b8b00220 | 1137 | * kvm_get_dirty_log_protect - get a snapshot of dirty pages |
2a31b9db | 1138 | * and reenable dirty page tracking for the corresponding pages. |
ba0513b5 MS |
1139 | * @kvm: pointer to kvm instance |
1140 | * @log: slot id and address to which we copy the log | |
b8b00220 | 1141 | * @flush: true if TLB flush is needed by caller |
ba0513b5 MS |
1142 | * |
1143 | * We need to keep it in mind that VCPU threads can write to the bitmap | |
1144 | * concurrently. So, to avoid losing track of dirty pages we keep the | |
1145 | * following order: | |
1146 | * | |
1147 | * 1. Take a snapshot of the bit and clear it if needed. | |
1148 | * 2. Write protect the corresponding page. | |
1149 | * 3. Copy the snapshot to the userspace. | |
1150 | * 4. Upon return caller flushes TLB's if needed. | |
1151 | * | |
1152 | * Between 2 and 4, the guest may write to the page using the remaining TLB | |
1153 | * entry. This is not a problem because the page is reported dirty using | |
1154 | * the snapshot taken before and step 4 ensures that writes done after | |
1155 | * exiting to userspace will be logged for the next call. | |
1156 | * | |
1157 | */ | |
1158 | int kvm_get_dirty_log_protect(struct kvm *kvm, | |
8fe65a82 | 1159 | struct kvm_dirty_log *log, bool *flush) |
ba0513b5 | 1160 | { |
9f6b8029 | 1161 | struct kvm_memslots *slots; |
ba0513b5 | 1162 | struct kvm_memory_slot *memslot; |
58d6db34 | 1163 | int i, as_id, id; |
ba0513b5 MS |
1164 | unsigned long n; |
1165 | unsigned long *dirty_bitmap; | |
1166 | unsigned long *dirty_bitmap_buffer; | |
1167 | ||
f481b069 PB |
1168 | as_id = log->slot >> 16; |
1169 | id = (u16)log->slot; | |
1170 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) | |
58d6db34 | 1171 | return -EINVAL; |
ba0513b5 | 1172 | |
f481b069 PB |
1173 | slots = __kvm_memslots(kvm, as_id); |
1174 | memslot = id_to_memslot(slots, id); | |
ba0513b5 MS |
1175 | |
1176 | dirty_bitmap = memslot->dirty_bitmap; | |
ba0513b5 | 1177 | if (!dirty_bitmap) |
58d6db34 | 1178 | return -ENOENT; |
ba0513b5 MS |
1179 | |
1180 | n = kvm_dirty_bitmap_bytes(memslot); | |
2a31b9db PB |
1181 | *flush = false; |
1182 | if (kvm->manual_dirty_log_protect) { | |
1183 | /* | |
1184 | * Unlike kvm_get_dirty_log, we always return false in *flush, | |
1185 | * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There | |
1186 | * is some code duplication between this function and | |
1187 | * kvm_get_dirty_log, but hopefully all architecture | |
1188 | * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log | |
1189 | * can be eliminated. | |
1190 | */ | |
1191 | dirty_bitmap_buffer = dirty_bitmap; | |
1192 | } else { | |
1193 | dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot); | |
1194 | memset(dirty_bitmap_buffer, 0, n); | |
ba0513b5 | 1195 | |
2a31b9db PB |
1196 | spin_lock(&kvm->mmu_lock); |
1197 | for (i = 0; i < n / sizeof(long); i++) { | |
1198 | unsigned long mask; | |
1199 | gfn_t offset; | |
ba0513b5 | 1200 | |
2a31b9db PB |
1201 | if (!dirty_bitmap[i]) |
1202 | continue; | |
1203 | ||
1204 | *flush = true; | |
1205 | mask = xchg(&dirty_bitmap[i], 0); | |
1206 | dirty_bitmap_buffer[i] = mask; | |
1207 | ||
a67794ca LT |
1208 | offset = i * BITS_PER_LONG; |
1209 | kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, | |
1210 | offset, mask); | |
2a31b9db PB |
1211 | } |
1212 | spin_unlock(&kvm->mmu_lock); | |
1213 | } | |
1214 | ||
1215 | if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n)) | |
1216 | return -EFAULT; | |
1217 | return 0; | |
1218 | } | |
1219 | EXPORT_SYMBOL_GPL(kvm_get_dirty_log_protect); | |
1220 | ||
1221 | /** | |
1222 | * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap | |
1223 | * and reenable dirty page tracking for the corresponding pages. | |
1224 | * @kvm: pointer to kvm instance | |
1225 | * @log: slot id and address from which to fetch the bitmap of dirty pages | |
b8b00220 | 1226 | * @flush: true if TLB flush is needed by caller |
2a31b9db PB |
1227 | */ |
1228 | int kvm_clear_dirty_log_protect(struct kvm *kvm, | |
1229 | struct kvm_clear_dirty_log *log, bool *flush) | |
1230 | { | |
1231 | struct kvm_memslots *slots; | |
1232 | struct kvm_memory_slot *memslot; | |
98938aa8 | 1233 | int as_id, id; |
2a31b9db | 1234 | gfn_t offset; |
98938aa8 | 1235 | unsigned long i, n; |
2a31b9db PB |
1236 | unsigned long *dirty_bitmap; |
1237 | unsigned long *dirty_bitmap_buffer; | |
1238 | ||
1239 | as_id = log->slot >> 16; | |
1240 | id = (u16)log->slot; | |
1241 | if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) | |
1242 | return -EINVAL; | |
1243 | ||
65c4189d | 1244 | if (log->first_page & 63) |
2a31b9db PB |
1245 | return -EINVAL; |
1246 | ||
1247 | slots = __kvm_memslots(kvm, as_id); | |
1248 | memslot = id_to_memslot(slots, id); | |
1249 | ||
1250 | dirty_bitmap = memslot->dirty_bitmap; | |
1251 | if (!dirty_bitmap) | |
1252 | return -ENOENT; | |
1253 | ||
4ddc9204 | 1254 | n = ALIGN(log->num_pages, BITS_PER_LONG) / 8; |
98938aa8 TB |
1255 | |
1256 | if (log->first_page > memslot->npages || | |
65c4189d PB |
1257 | log->num_pages > memslot->npages - log->first_page || |
1258 | (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63))) | |
1259 | return -EINVAL; | |
98938aa8 | 1260 | |
8fe65a82 | 1261 | *flush = false; |
2a31b9db PB |
1262 | dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot); |
1263 | if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n)) | |
1264 | return -EFAULT; | |
ba0513b5 | 1265 | |
2a31b9db | 1266 | spin_lock(&kvm->mmu_lock); |
53eac7a8 PX |
1267 | for (offset = log->first_page, i = offset / BITS_PER_LONG, |
1268 | n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--; | |
2a31b9db PB |
1269 | i++, offset += BITS_PER_LONG) { |
1270 | unsigned long mask = *dirty_bitmap_buffer++; | |
1271 | atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i]; | |
1272 | if (!mask) | |
ba0513b5 MS |
1273 | continue; |
1274 | ||
2a31b9db | 1275 | mask &= atomic_long_fetch_andnot(mask, p); |
ba0513b5 | 1276 | |
2a31b9db PB |
1277 | /* |
1278 | * mask contains the bits that really have been cleared. This | |
1279 | * never includes any bits beyond the length of the memslot (if | |
1280 | * the length is not aligned to 64 pages), therefore it is not | |
1281 | * a problem if userspace sets them in log->dirty_bitmap. | |
1282 | */ | |
58d2930f | 1283 | if (mask) { |
2a31b9db | 1284 | *flush = true; |
58d2930f TY |
1285 | kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, |
1286 | offset, mask); | |
1287 | } | |
ba0513b5 | 1288 | } |
ba0513b5 | 1289 | spin_unlock(&kvm->mmu_lock); |
2a31b9db | 1290 | |
58d6db34 | 1291 | return 0; |
ba0513b5 | 1292 | } |
2a31b9db | 1293 | EXPORT_SYMBOL_GPL(kvm_clear_dirty_log_protect); |
ba0513b5 MS |
1294 | #endif |
1295 | ||
db3fe4eb TY |
1296 | bool kvm_largepages_enabled(void) |
1297 | { | |
1298 | return largepages_enabled; | |
1299 | } | |
1300 | ||
54dee993 MT |
1301 | void kvm_disable_largepages(void) |
1302 | { | |
1303 | largepages_enabled = false; | |
1304 | } | |
1305 | EXPORT_SYMBOL_GPL(kvm_disable_largepages); | |
1306 | ||
49c7754c GN |
1307 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) |
1308 | { | |
1309 | return __gfn_to_memslot(kvm_memslots(kvm), gfn); | |
1310 | } | |
a1f4d395 | 1311 | EXPORT_SYMBOL_GPL(gfn_to_memslot); |
6aa8b732 | 1312 | |
8e73485c PB |
1313 | struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn) |
1314 | { | |
1315 | return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn); | |
1316 | } | |
1317 | ||
33e94154 | 1318 | bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn) |
e0d62c7f | 1319 | { |
bf3e05bc | 1320 | struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn); |
e0d62c7f | 1321 | |
bbacc0c1 | 1322 | if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS || |
bf3e05bc | 1323 | memslot->flags & KVM_MEMSLOT_INVALID) |
33e94154 | 1324 | return false; |
e0d62c7f | 1325 | |
33e94154 | 1326 | return true; |
e0d62c7f IE |
1327 | } |
1328 | EXPORT_SYMBOL_GPL(kvm_is_visible_gfn); | |
1329 | ||
8f0b1ab6 JR |
1330 | unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn) |
1331 | { | |
1332 | struct vm_area_struct *vma; | |
1333 | unsigned long addr, size; | |
1334 | ||
1335 | size = PAGE_SIZE; | |
1336 | ||
1337 | addr = gfn_to_hva(kvm, gfn); | |
1338 | if (kvm_is_error_hva(addr)) | |
1339 | return PAGE_SIZE; | |
1340 | ||
1341 | down_read(¤t->mm->mmap_sem); | |
1342 | vma = find_vma(current->mm, addr); | |
1343 | if (!vma) | |
1344 | goto out; | |
1345 | ||
1346 | size = vma_kernel_pagesize(vma); | |
1347 | ||
1348 | out: | |
1349 | up_read(¤t->mm->mmap_sem); | |
1350 | ||
1351 | return size; | |
1352 | } | |
1353 | ||
4d8b81ab XG |
1354 | static bool memslot_is_readonly(struct kvm_memory_slot *slot) |
1355 | { | |
1356 | return slot->flags & KVM_MEM_READONLY; | |
1357 | } | |
1358 | ||
4d8b81ab XG |
1359 | static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
1360 | gfn_t *nr_pages, bool write) | |
539cb660 | 1361 | { |
bc6678a3 | 1362 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID) |
ca3a490c | 1363 | return KVM_HVA_ERR_BAD; |
48987781 | 1364 | |
4d8b81ab XG |
1365 | if (memslot_is_readonly(slot) && write) |
1366 | return KVM_HVA_ERR_RO_BAD; | |
48987781 XG |
1367 | |
1368 | if (nr_pages) | |
1369 | *nr_pages = slot->npages - (gfn - slot->base_gfn); | |
1370 | ||
4d8b81ab | 1371 | return __gfn_to_hva_memslot(slot, gfn); |
539cb660 | 1372 | } |
48987781 | 1373 | |
4d8b81ab XG |
1374 | static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn, |
1375 | gfn_t *nr_pages) | |
1376 | { | |
1377 | return __gfn_to_hva_many(slot, gfn, nr_pages, true); | |
539cb660 | 1378 | } |
48987781 | 1379 | |
4d8b81ab | 1380 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, |
7940876e | 1381 | gfn_t gfn) |
4d8b81ab XG |
1382 | { |
1383 | return gfn_to_hva_many(slot, gfn, NULL); | |
1384 | } | |
1385 | EXPORT_SYMBOL_GPL(gfn_to_hva_memslot); | |
1386 | ||
48987781 XG |
1387 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn) |
1388 | { | |
49c7754c | 1389 | return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL); |
48987781 | 1390 | } |
0d150298 | 1391 | EXPORT_SYMBOL_GPL(gfn_to_hva); |
539cb660 | 1392 | |
8e73485c PB |
1393 | unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn) |
1394 | { | |
1395 | return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL); | |
1396 | } | |
1397 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva); | |
1398 | ||
86ab8cff | 1399 | /* |
970c0d4b WY |
1400 | * Return the hva of a @gfn and the R/W attribute if possible. |
1401 | * | |
1402 | * @slot: the kvm_memory_slot which contains @gfn | |
1403 | * @gfn: the gfn to be translated | |
1404 | * @writable: used to return the read/write attribute of the @slot if the hva | |
1405 | * is valid and @writable is not NULL | |
86ab8cff | 1406 | */ |
64d83126 CD |
1407 | unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, |
1408 | gfn_t gfn, bool *writable) | |
86ab8cff | 1409 | { |
a2ac07fe GN |
1410 | unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false); |
1411 | ||
1412 | if (!kvm_is_error_hva(hva) && writable) | |
ba6a3541 PB |
1413 | *writable = !memslot_is_readonly(slot); |
1414 | ||
a2ac07fe | 1415 | return hva; |
86ab8cff XG |
1416 | } |
1417 | ||
64d83126 CD |
1418 | unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable) |
1419 | { | |
1420 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1421 | ||
1422 | return gfn_to_hva_memslot_prot(slot, gfn, writable); | |
1423 | } | |
1424 | ||
8e73485c PB |
1425 | unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable) |
1426 | { | |
1427 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1428 | ||
1429 | return gfn_to_hva_memslot_prot(slot, gfn, writable); | |
1430 | } | |
1431 | ||
fafc3dba HY |
1432 | static inline int check_user_page_hwpoison(unsigned long addr) |
1433 | { | |
0d731759 | 1434 | int rc, flags = FOLL_HWPOISON | FOLL_WRITE; |
fafc3dba | 1435 | |
0d731759 | 1436 | rc = get_user_pages(addr, 1, flags, NULL, NULL); |
fafc3dba HY |
1437 | return rc == -EHWPOISON; |
1438 | } | |
1439 | ||
2fc84311 | 1440 | /* |
b9b33da2 PB |
1441 | * The fast path to get the writable pfn which will be stored in @pfn, |
1442 | * true indicates success, otherwise false is returned. It's also the | |
1443 | * only part that runs if we can are in atomic context. | |
2fc84311 | 1444 | */ |
b9b33da2 PB |
1445 | static bool hva_to_pfn_fast(unsigned long addr, bool write_fault, |
1446 | bool *writable, kvm_pfn_t *pfn) | |
954bbbc2 | 1447 | { |
8d4e1288 | 1448 | struct page *page[1]; |
2fc84311 | 1449 | int npages; |
954bbbc2 | 1450 | |
12ce13fe XG |
1451 | /* |
1452 | * Fast pin a writable pfn only if it is a write fault request | |
1453 | * or the caller allows to map a writable pfn for a read fault | |
1454 | * request. | |
1455 | */ | |
1456 | if (!(write_fault || writable)) | |
1457 | return false; | |
612819c3 | 1458 | |
2fc84311 XG |
1459 | npages = __get_user_pages_fast(addr, 1, 1, page); |
1460 | if (npages == 1) { | |
1461 | *pfn = page_to_pfn(page[0]); | |
612819c3 | 1462 | |
2fc84311 XG |
1463 | if (writable) |
1464 | *writable = true; | |
1465 | return true; | |
1466 | } | |
af585b92 | 1467 | |
2fc84311 XG |
1468 | return false; |
1469 | } | |
612819c3 | 1470 | |
2fc84311 XG |
1471 | /* |
1472 | * The slow path to get the pfn of the specified host virtual address, | |
1473 | * 1 indicates success, -errno is returned if error is detected. | |
1474 | */ | |
1475 | static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault, | |
ba049e93 | 1476 | bool *writable, kvm_pfn_t *pfn) |
2fc84311 | 1477 | { |
ce53053c AV |
1478 | unsigned int flags = FOLL_HWPOISON; |
1479 | struct page *page; | |
2fc84311 | 1480 | int npages = 0; |
612819c3 | 1481 | |
2fc84311 XG |
1482 | might_sleep(); |
1483 | ||
1484 | if (writable) | |
1485 | *writable = write_fault; | |
1486 | ||
ce53053c AV |
1487 | if (write_fault) |
1488 | flags |= FOLL_WRITE; | |
1489 | if (async) | |
1490 | flags |= FOLL_NOWAIT; | |
d4944b0e | 1491 | |
ce53053c | 1492 | npages = get_user_pages_unlocked(addr, 1, &page, flags); |
2fc84311 XG |
1493 | if (npages != 1) |
1494 | return npages; | |
1495 | ||
1496 | /* map read fault as writable if possible */ | |
12ce13fe | 1497 | if (unlikely(!write_fault) && writable) { |
ce53053c | 1498 | struct page *wpage; |
2fc84311 | 1499 | |
ce53053c | 1500 | if (__get_user_pages_fast(addr, 1, 1, &wpage) == 1) { |
2fc84311 | 1501 | *writable = true; |
ce53053c AV |
1502 | put_page(page); |
1503 | page = wpage; | |
612819c3 | 1504 | } |
887c08ac | 1505 | } |
ce53053c | 1506 | *pfn = page_to_pfn(page); |
2fc84311 XG |
1507 | return npages; |
1508 | } | |
539cb660 | 1509 | |
4d8b81ab XG |
1510 | static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault) |
1511 | { | |
1512 | if (unlikely(!(vma->vm_flags & VM_READ))) | |
1513 | return false; | |
2e2e3738 | 1514 | |
4d8b81ab XG |
1515 | if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE)))) |
1516 | return false; | |
887c08ac | 1517 | |
4d8b81ab XG |
1518 | return true; |
1519 | } | |
bf998156 | 1520 | |
92176a8e PB |
1521 | static int hva_to_pfn_remapped(struct vm_area_struct *vma, |
1522 | unsigned long addr, bool *async, | |
a340b3e2 KA |
1523 | bool write_fault, bool *writable, |
1524 | kvm_pfn_t *p_pfn) | |
92176a8e | 1525 | { |
add6a0cd PB |
1526 | unsigned long pfn; |
1527 | int r; | |
1528 | ||
1529 | r = follow_pfn(vma, addr, &pfn); | |
1530 | if (r) { | |
1531 | /* | |
1532 | * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does | |
1533 | * not call the fault handler, so do it here. | |
1534 | */ | |
1535 | bool unlocked = false; | |
1536 | r = fixup_user_fault(current, current->mm, addr, | |
1537 | (write_fault ? FAULT_FLAG_WRITE : 0), | |
1538 | &unlocked); | |
1539 | if (unlocked) | |
1540 | return -EAGAIN; | |
1541 | if (r) | |
1542 | return r; | |
1543 | ||
1544 | r = follow_pfn(vma, addr, &pfn); | |
1545 | if (r) | |
1546 | return r; | |
1547 | ||
1548 | } | |
1549 | ||
a340b3e2 KA |
1550 | if (writable) |
1551 | *writable = true; | |
add6a0cd PB |
1552 | |
1553 | /* | |
1554 | * Get a reference here because callers of *hva_to_pfn* and | |
1555 | * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the | |
1556 | * returned pfn. This is only needed if the VMA has VM_MIXEDMAP | |
1557 | * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will | |
1558 | * simply do nothing for reserved pfns. | |
1559 | * | |
1560 | * Whoever called remap_pfn_range is also going to call e.g. | |
1561 | * unmap_mapping_range before the underlying pages are freed, | |
1562 | * causing a call to our MMU notifier. | |
1563 | */ | |
1564 | kvm_get_pfn(pfn); | |
1565 | ||
1566 | *p_pfn = pfn; | |
92176a8e PB |
1567 | return 0; |
1568 | } | |
1569 | ||
12ce13fe XG |
1570 | /* |
1571 | * Pin guest page in memory and return its pfn. | |
1572 | * @addr: host virtual address which maps memory to the guest | |
1573 | * @atomic: whether this function can sleep | |
1574 | * @async: whether this function need to wait IO complete if the | |
1575 | * host page is not in the memory | |
1576 | * @write_fault: whether we should get a writable host page | |
1577 | * @writable: whether it allows to map a writable host page for !@write_fault | |
1578 | * | |
1579 | * The function will map a writable host page for these two cases: | |
1580 | * 1): @write_fault = true | |
1581 | * 2): @write_fault = false && @writable, @writable will tell the caller | |
1582 | * whether the mapping is writable. | |
1583 | */ | |
ba049e93 | 1584 | static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async, |
2fc84311 XG |
1585 | bool write_fault, bool *writable) |
1586 | { | |
1587 | struct vm_area_struct *vma; | |
ba049e93 | 1588 | kvm_pfn_t pfn = 0; |
92176a8e | 1589 | int npages, r; |
2e2e3738 | 1590 | |
2fc84311 XG |
1591 | /* we can do it either atomically or asynchronously, not both */ |
1592 | BUG_ON(atomic && async); | |
8d4e1288 | 1593 | |
b9b33da2 | 1594 | if (hva_to_pfn_fast(addr, write_fault, writable, &pfn)) |
2fc84311 XG |
1595 | return pfn; |
1596 | ||
1597 | if (atomic) | |
1598 | return KVM_PFN_ERR_FAULT; | |
1599 | ||
1600 | npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn); | |
1601 | if (npages == 1) | |
1602 | return pfn; | |
8d4e1288 | 1603 | |
2fc84311 XG |
1604 | down_read(¤t->mm->mmap_sem); |
1605 | if (npages == -EHWPOISON || | |
1606 | (!async && check_user_page_hwpoison(addr))) { | |
1607 | pfn = KVM_PFN_ERR_HWPOISON; | |
1608 | goto exit; | |
1609 | } | |
1610 | ||
add6a0cd | 1611 | retry: |
2fc84311 XG |
1612 | vma = find_vma_intersection(current->mm, addr, addr + 1); |
1613 | ||
1614 | if (vma == NULL) | |
1615 | pfn = KVM_PFN_ERR_FAULT; | |
92176a8e | 1616 | else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { |
a340b3e2 | 1617 | r = hva_to_pfn_remapped(vma, addr, async, write_fault, writable, &pfn); |
add6a0cd PB |
1618 | if (r == -EAGAIN) |
1619 | goto retry; | |
92176a8e PB |
1620 | if (r < 0) |
1621 | pfn = KVM_PFN_ERR_FAULT; | |
2fc84311 | 1622 | } else { |
4d8b81ab | 1623 | if (async && vma_is_valid(vma, write_fault)) |
2fc84311 XG |
1624 | *async = true; |
1625 | pfn = KVM_PFN_ERR_FAULT; | |
1626 | } | |
1627 | exit: | |
1628 | up_read(¤t->mm->mmap_sem); | |
2e2e3738 | 1629 | return pfn; |
35149e21 AL |
1630 | } |
1631 | ||
ba049e93 DW |
1632 | kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, |
1633 | bool atomic, bool *async, bool write_fault, | |
1634 | bool *writable) | |
887c08ac | 1635 | { |
4d8b81ab XG |
1636 | unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault); |
1637 | ||
b2740d35 PB |
1638 | if (addr == KVM_HVA_ERR_RO_BAD) { |
1639 | if (writable) | |
1640 | *writable = false; | |
4d8b81ab | 1641 | return KVM_PFN_ERR_RO_FAULT; |
b2740d35 | 1642 | } |
4d8b81ab | 1643 | |
b2740d35 PB |
1644 | if (kvm_is_error_hva(addr)) { |
1645 | if (writable) | |
1646 | *writable = false; | |
81c52c56 | 1647 | return KVM_PFN_NOSLOT; |
b2740d35 | 1648 | } |
4d8b81ab XG |
1649 | |
1650 | /* Do not map writable pfn in the readonly memslot. */ | |
1651 | if (writable && memslot_is_readonly(slot)) { | |
1652 | *writable = false; | |
1653 | writable = NULL; | |
1654 | } | |
1655 | ||
1656 | return hva_to_pfn(addr, atomic, async, write_fault, | |
1657 | writable); | |
887c08ac | 1658 | } |
3520469d | 1659 | EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot); |
887c08ac | 1660 | |
ba049e93 | 1661 | kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
612819c3 MT |
1662 | bool *writable) |
1663 | { | |
e37afc6e PB |
1664 | return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL, |
1665 | write_fault, writable); | |
612819c3 MT |
1666 | } |
1667 | EXPORT_SYMBOL_GPL(gfn_to_pfn_prot); | |
1668 | ||
ba049e93 | 1669 | kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn) |
506f0d6f | 1670 | { |
4d8b81ab | 1671 | return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL); |
506f0d6f | 1672 | } |
e37afc6e | 1673 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot); |
506f0d6f | 1674 | |
ba049e93 | 1675 | kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn) |
506f0d6f | 1676 | { |
4d8b81ab | 1677 | return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL); |
506f0d6f | 1678 | } |
037d92dc | 1679 | EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic); |
506f0d6f | 1680 | |
ba049e93 | 1681 | kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn) |
e37afc6e PB |
1682 | { |
1683 | return gfn_to_pfn_memslot_atomic(gfn_to_memslot(kvm, gfn), gfn); | |
1684 | } | |
1685 | EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic); | |
1686 | ||
ba049e93 | 1687 | kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn) |
8e73485c PB |
1688 | { |
1689 | return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); | |
1690 | } | |
1691 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic); | |
1692 | ||
ba049e93 | 1693 | kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn) |
e37afc6e PB |
1694 | { |
1695 | return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn); | |
1696 | } | |
1697 | EXPORT_SYMBOL_GPL(gfn_to_pfn); | |
1698 | ||
ba049e93 | 1699 | kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
8e73485c PB |
1700 | { |
1701 | return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn); | |
1702 | } | |
1703 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn); | |
1704 | ||
d9ef13c2 PB |
1705 | int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
1706 | struct page **pages, int nr_pages) | |
48987781 XG |
1707 | { |
1708 | unsigned long addr; | |
076b925d | 1709 | gfn_t entry = 0; |
48987781 | 1710 | |
d9ef13c2 | 1711 | addr = gfn_to_hva_many(slot, gfn, &entry); |
48987781 XG |
1712 | if (kvm_is_error_hva(addr)) |
1713 | return -1; | |
1714 | ||
1715 | if (entry < nr_pages) | |
1716 | return 0; | |
1717 | ||
1718 | return __get_user_pages_fast(addr, nr_pages, 1, pages); | |
1719 | } | |
1720 | EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic); | |
1721 | ||
ba049e93 | 1722 | static struct page *kvm_pfn_to_page(kvm_pfn_t pfn) |
a2766325 | 1723 | { |
81c52c56 | 1724 | if (is_error_noslot_pfn(pfn)) |
cb9aaa30 | 1725 | return KVM_ERR_PTR_BAD_PAGE; |
a2766325 | 1726 | |
bf4bea8e | 1727 | if (kvm_is_reserved_pfn(pfn)) { |
cb9aaa30 | 1728 | WARN_ON(1); |
6cede2e6 | 1729 | return KVM_ERR_PTR_BAD_PAGE; |
cb9aaa30 | 1730 | } |
a2766325 XG |
1731 | |
1732 | return pfn_to_page(pfn); | |
1733 | } | |
1734 | ||
35149e21 AL |
1735 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) |
1736 | { | |
ba049e93 | 1737 | kvm_pfn_t pfn; |
2e2e3738 AL |
1738 | |
1739 | pfn = gfn_to_pfn(kvm, gfn); | |
2e2e3738 | 1740 | |
a2766325 | 1741 | return kvm_pfn_to_page(pfn); |
954bbbc2 AK |
1742 | } |
1743 | EXPORT_SYMBOL_GPL(gfn_to_page); | |
1744 | ||
e45adf66 KA |
1745 | static int __kvm_map_gfn(struct kvm_memory_slot *slot, gfn_t gfn, |
1746 | struct kvm_host_map *map) | |
1747 | { | |
1748 | kvm_pfn_t pfn; | |
1749 | void *hva = NULL; | |
1750 | struct page *page = KVM_UNMAPPED_PAGE; | |
1751 | ||
1752 | if (!map) | |
1753 | return -EINVAL; | |
1754 | ||
1755 | pfn = gfn_to_pfn_memslot(slot, gfn); | |
1756 | if (is_error_noslot_pfn(pfn)) | |
1757 | return -EINVAL; | |
1758 | ||
1759 | if (pfn_valid(pfn)) { | |
1760 | page = pfn_to_page(pfn); | |
1761 | hva = kmap(page); | |
1762 | } else { | |
1763 | hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); | |
1764 | } | |
1765 | ||
1766 | if (!hva) | |
1767 | return -EFAULT; | |
1768 | ||
1769 | map->page = page; | |
1770 | map->hva = hva; | |
1771 | map->pfn = pfn; | |
1772 | map->gfn = gfn; | |
1773 | ||
1774 | return 0; | |
1775 | } | |
1776 | ||
1777 | int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) | |
1778 | { | |
1779 | return __kvm_map_gfn(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, map); | |
1780 | } | |
1781 | EXPORT_SYMBOL_GPL(kvm_vcpu_map); | |
1782 | ||
1783 | void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, | |
1784 | bool dirty) | |
1785 | { | |
1786 | if (!map) | |
1787 | return; | |
1788 | ||
1789 | if (!map->hva) | |
1790 | return; | |
1791 | ||
1792 | if (map->page) | |
1793 | kunmap(map->page); | |
1794 | else | |
1795 | memunmap(map->hva); | |
1796 | ||
1797 | if (dirty) { | |
1798 | kvm_vcpu_mark_page_dirty(vcpu, map->gfn); | |
1799 | kvm_release_pfn_dirty(map->pfn); | |
1800 | } else { | |
1801 | kvm_release_pfn_clean(map->pfn); | |
1802 | } | |
1803 | ||
1804 | map->hva = NULL; | |
1805 | map->page = NULL; | |
1806 | } | |
1807 | EXPORT_SYMBOL_GPL(kvm_vcpu_unmap); | |
1808 | ||
8e73485c PB |
1809 | struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
1810 | { | |
ba049e93 | 1811 | kvm_pfn_t pfn; |
8e73485c PB |
1812 | |
1813 | pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn); | |
1814 | ||
1815 | return kvm_pfn_to_page(pfn); | |
1816 | } | |
1817 | EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page); | |
1818 | ||
b4231d61 IE |
1819 | void kvm_release_page_clean(struct page *page) |
1820 | { | |
32cad84f XG |
1821 | WARN_ON(is_error_page(page)); |
1822 | ||
35149e21 | 1823 | kvm_release_pfn_clean(page_to_pfn(page)); |
b4231d61 IE |
1824 | } |
1825 | EXPORT_SYMBOL_GPL(kvm_release_page_clean); | |
1826 | ||
ba049e93 | 1827 | void kvm_release_pfn_clean(kvm_pfn_t pfn) |
35149e21 | 1828 | { |
bf4bea8e | 1829 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1830 | put_page(pfn_to_page(pfn)); |
35149e21 AL |
1831 | } |
1832 | EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); | |
1833 | ||
b4231d61 | 1834 | void kvm_release_page_dirty(struct page *page) |
8a7ae055 | 1835 | { |
a2766325 XG |
1836 | WARN_ON(is_error_page(page)); |
1837 | ||
35149e21 AL |
1838 | kvm_release_pfn_dirty(page_to_pfn(page)); |
1839 | } | |
1840 | EXPORT_SYMBOL_GPL(kvm_release_page_dirty); | |
1841 | ||
f7a6509f | 1842 | void kvm_release_pfn_dirty(kvm_pfn_t pfn) |
35149e21 AL |
1843 | { |
1844 | kvm_set_pfn_dirty(pfn); | |
1845 | kvm_release_pfn_clean(pfn); | |
1846 | } | |
f7a6509f | 1847 | EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); |
35149e21 | 1848 | |
ba049e93 | 1849 | void kvm_set_pfn_dirty(kvm_pfn_t pfn) |
35149e21 | 1850 | { |
bf4bea8e | 1851 | if (!kvm_is_reserved_pfn(pfn)) { |
2e2e3738 | 1852 | struct page *page = pfn_to_page(pfn); |
f95ef0cd | 1853 | |
2e2e3738 AL |
1854 | if (!PageReserved(page)) |
1855 | SetPageDirty(page); | |
1856 | } | |
8a7ae055 | 1857 | } |
35149e21 AL |
1858 | EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); |
1859 | ||
ba049e93 | 1860 | void kvm_set_pfn_accessed(kvm_pfn_t pfn) |
35149e21 | 1861 | { |
bf4bea8e | 1862 | if (!kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1863 | mark_page_accessed(pfn_to_page(pfn)); |
35149e21 AL |
1864 | } |
1865 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); | |
1866 | ||
ba049e93 | 1867 | void kvm_get_pfn(kvm_pfn_t pfn) |
35149e21 | 1868 | { |
bf4bea8e | 1869 | if (!kvm_is_reserved_pfn(pfn)) |
2e2e3738 | 1870 | get_page(pfn_to_page(pfn)); |
35149e21 AL |
1871 | } |
1872 | EXPORT_SYMBOL_GPL(kvm_get_pfn); | |
8a7ae055 | 1873 | |
195aefde IE |
1874 | static int next_segment(unsigned long len, int offset) |
1875 | { | |
1876 | if (len > PAGE_SIZE - offset) | |
1877 | return PAGE_SIZE - offset; | |
1878 | else | |
1879 | return len; | |
1880 | } | |
1881 | ||
8e73485c PB |
1882 | static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, |
1883 | void *data, int offset, int len) | |
195aefde | 1884 | { |
e0506bcb IE |
1885 | int r; |
1886 | unsigned long addr; | |
195aefde | 1887 | |
8e73485c | 1888 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); |
e0506bcb IE |
1889 | if (kvm_is_error_hva(addr)) |
1890 | return -EFAULT; | |
3180a7fc | 1891 | r = __copy_from_user(data, (void __user *)addr + offset, len); |
e0506bcb | 1892 | if (r) |
195aefde | 1893 | return -EFAULT; |
195aefde IE |
1894 | return 0; |
1895 | } | |
8e73485c PB |
1896 | |
1897 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, | |
1898 | int len) | |
1899 | { | |
1900 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1901 | ||
1902 | return __kvm_read_guest_page(slot, gfn, data, offset, len); | |
1903 | } | |
195aefde IE |
1904 | EXPORT_SYMBOL_GPL(kvm_read_guest_page); |
1905 | ||
8e73485c PB |
1906 | int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, |
1907 | int offset, int len) | |
1908 | { | |
1909 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1910 | ||
1911 | return __kvm_read_guest_page(slot, gfn, data, offset, len); | |
1912 | } | |
1913 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); | |
1914 | ||
195aefde IE |
1915 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len) |
1916 | { | |
1917 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1918 | int seg; | |
1919 | int offset = offset_in_page(gpa); | |
1920 | int ret; | |
1921 | ||
1922 | while ((seg = next_segment(len, offset)) != 0) { | |
1923 | ret = kvm_read_guest_page(kvm, gfn, data, offset, seg); | |
1924 | if (ret < 0) | |
1925 | return ret; | |
1926 | offset = 0; | |
1927 | len -= seg; | |
1928 | data += seg; | |
1929 | ++gfn; | |
1930 | } | |
1931 | return 0; | |
1932 | } | |
1933 | EXPORT_SYMBOL_GPL(kvm_read_guest); | |
1934 | ||
8e73485c | 1935 | int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len) |
7ec54588 | 1936 | { |
7ec54588 | 1937 | gfn_t gfn = gpa >> PAGE_SHIFT; |
8e73485c | 1938 | int seg; |
7ec54588 | 1939 | int offset = offset_in_page(gpa); |
8e73485c PB |
1940 | int ret; |
1941 | ||
1942 | while ((seg = next_segment(len, offset)) != 0) { | |
1943 | ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg); | |
1944 | if (ret < 0) | |
1945 | return ret; | |
1946 | offset = 0; | |
1947 | len -= seg; | |
1948 | data += seg; | |
1949 | ++gfn; | |
1950 | } | |
1951 | return 0; | |
1952 | } | |
1953 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest); | |
7ec54588 | 1954 | |
8e73485c PB |
1955 | static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn, |
1956 | void *data, int offset, unsigned long len) | |
1957 | { | |
1958 | int r; | |
1959 | unsigned long addr; | |
1960 | ||
1961 | addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); | |
7ec54588 MT |
1962 | if (kvm_is_error_hva(addr)) |
1963 | return -EFAULT; | |
0aac03f0 | 1964 | pagefault_disable(); |
3180a7fc | 1965 | r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len); |
0aac03f0 | 1966 | pagefault_enable(); |
7ec54588 MT |
1967 | if (r) |
1968 | return -EFAULT; | |
1969 | return 0; | |
1970 | } | |
7ec54588 | 1971 | |
8e73485c PB |
1972 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
1973 | unsigned long len) | |
1974 | { | |
1975 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1976 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
1977 | int offset = offset_in_page(gpa); | |
1978 | ||
1979 | return __kvm_read_guest_atomic(slot, gfn, data, offset, len); | |
1980 | } | |
1981 | EXPORT_SYMBOL_GPL(kvm_read_guest_atomic); | |
1982 | ||
1983 | int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, | |
1984 | void *data, unsigned long len) | |
1985 | { | |
1986 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
1987 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
1988 | int offset = offset_in_page(gpa); | |
1989 | ||
1990 | return __kvm_read_guest_atomic(slot, gfn, data, offset, len); | |
1991 | } | |
1992 | EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); | |
1993 | ||
1994 | static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn, | |
1995 | const void *data, int offset, int len) | |
195aefde | 1996 | { |
e0506bcb IE |
1997 | int r; |
1998 | unsigned long addr; | |
195aefde | 1999 | |
251eb841 | 2000 | addr = gfn_to_hva_memslot(memslot, gfn); |
e0506bcb IE |
2001 | if (kvm_is_error_hva(addr)) |
2002 | return -EFAULT; | |
8b0cedff | 2003 | r = __copy_to_user((void __user *)addr + offset, data, len); |
e0506bcb | 2004 | if (r) |
195aefde | 2005 | return -EFAULT; |
bc009e43 | 2006 | mark_page_dirty_in_slot(memslot, gfn); |
195aefde IE |
2007 | return 0; |
2008 | } | |
8e73485c PB |
2009 | |
2010 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, | |
2011 | const void *data, int offset, int len) | |
2012 | { | |
2013 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); | |
2014 | ||
2015 | return __kvm_write_guest_page(slot, gfn, data, offset, len); | |
2016 | } | |
195aefde IE |
2017 | EXPORT_SYMBOL_GPL(kvm_write_guest_page); |
2018 | ||
8e73485c PB |
2019 | int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
2020 | const void *data, int offset, int len) | |
2021 | { | |
2022 | struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
2023 | ||
2024 | return __kvm_write_guest_page(slot, gfn, data, offset, len); | |
2025 | } | |
2026 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page); | |
2027 | ||
195aefde IE |
2028 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, |
2029 | unsigned long len) | |
2030 | { | |
2031 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
2032 | int seg; | |
2033 | int offset = offset_in_page(gpa); | |
2034 | int ret; | |
2035 | ||
2036 | while ((seg = next_segment(len, offset)) != 0) { | |
2037 | ret = kvm_write_guest_page(kvm, gfn, data, offset, seg); | |
2038 | if (ret < 0) | |
2039 | return ret; | |
2040 | offset = 0; | |
2041 | len -= seg; | |
2042 | data += seg; | |
2043 | ++gfn; | |
2044 | } | |
2045 | return 0; | |
2046 | } | |
ff651cb6 | 2047 | EXPORT_SYMBOL_GPL(kvm_write_guest); |
195aefde | 2048 | |
8e73485c PB |
2049 | int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data, |
2050 | unsigned long len) | |
2051 | { | |
2052 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
2053 | int seg; | |
2054 | int offset = offset_in_page(gpa); | |
2055 | int ret; | |
2056 | ||
2057 | while ((seg = next_segment(len, offset)) != 0) { | |
2058 | ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg); | |
2059 | if (ret < 0) | |
2060 | return ret; | |
2061 | offset = 0; | |
2062 | len -= seg; | |
2063 | data += seg; | |
2064 | ++gfn; | |
2065 | } | |
2066 | return 0; | |
2067 | } | |
2068 | EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest); | |
2069 | ||
5a2d4365 PB |
2070 | static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots, |
2071 | struct gfn_to_hva_cache *ghc, | |
2072 | gpa_t gpa, unsigned long len) | |
49c7754c | 2073 | { |
49c7754c | 2074 | int offset = offset_in_page(gpa); |
8f964525 AH |
2075 | gfn_t start_gfn = gpa >> PAGE_SHIFT; |
2076 | gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT; | |
2077 | gfn_t nr_pages_needed = end_gfn - start_gfn + 1; | |
2078 | gfn_t nr_pages_avail; | |
f1b9dd5e | 2079 | int r = start_gfn <= end_gfn ? 0 : -EINVAL; |
49c7754c GN |
2080 | |
2081 | ghc->gpa = gpa; | |
2082 | ghc->generation = slots->generation; | |
8f964525 | 2083 | ghc->len = len; |
f1b9dd5e JM |
2084 | ghc->hva = KVM_HVA_ERR_BAD; |
2085 | ||
2086 | /* | |
2087 | * If the requested region crosses two memslots, we still | |
2088 | * verify that the entire region is valid here. | |
2089 | */ | |
2090 | while (!r && start_gfn <= end_gfn) { | |
2091 | ghc->memslot = __gfn_to_memslot(slots, start_gfn); | |
2092 | ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, | |
2093 | &nr_pages_avail); | |
2094 | if (kvm_is_error_hva(ghc->hva)) | |
2095 | r = -EFAULT; | |
2096 | start_gfn += nr_pages_avail; | |
2097 | } | |
2098 | ||
2099 | /* Use the slow path for cross page reads and writes. */ | |
2100 | if (!r && nr_pages_needed == 1) | |
49c7754c | 2101 | ghc->hva += offset; |
f1b9dd5e | 2102 | else |
8f964525 | 2103 | ghc->memslot = NULL; |
f1b9dd5e JM |
2104 | |
2105 | return r; | |
49c7754c | 2106 | } |
5a2d4365 | 2107 | |
4e335d9e | 2108 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
5a2d4365 PB |
2109 | gpa_t gpa, unsigned long len) |
2110 | { | |
4e335d9e | 2111 | struct kvm_memslots *slots = kvm_memslots(kvm); |
5a2d4365 PB |
2112 | return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len); |
2113 | } | |
4e335d9e | 2114 | EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init); |
49c7754c | 2115 | |
4e335d9e | 2116 | int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
7a86dab8 JM |
2117 | void *data, unsigned int offset, |
2118 | unsigned long len) | |
49c7754c | 2119 | { |
4e335d9e | 2120 | struct kvm_memslots *slots = kvm_memslots(kvm); |
49c7754c | 2121 | int r; |
4ec6e863 | 2122 | gpa_t gpa = ghc->gpa + offset; |
49c7754c | 2123 | |
4ec6e863 | 2124 | BUG_ON(len + offset > ghc->len); |
8f964525 | 2125 | |
49c7754c | 2126 | if (slots->generation != ghc->generation) |
5a2d4365 | 2127 | __kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len); |
8f964525 AH |
2128 | |
2129 | if (unlikely(!ghc->memslot)) | |
4e335d9e | 2130 | return kvm_write_guest(kvm, gpa, data, len); |
49c7754c GN |
2131 | |
2132 | if (kvm_is_error_hva(ghc->hva)) | |
2133 | return -EFAULT; | |
2134 | ||
4ec6e863 | 2135 | r = __copy_to_user((void __user *)ghc->hva + offset, data, len); |
49c7754c GN |
2136 | if (r) |
2137 | return -EFAULT; | |
4ec6e863 | 2138 | mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT); |
49c7754c GN |
2139 | |
2140 | return 0; | |
2141 | } | |
4e335d9e | 2142 | EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached); |
4ec6e863 | 2143 | |
4e335d9e PB |
2144 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
2145 | void *data, unsigned long len) | |
4ec6e863 | 2146 | { |
4e335d9e | 2147 | return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len); |
4ec6e863 | 2148 | } |
4e335d9e | 2149 | EXPORT_SYMBOL_GPL(kvm_write_guest_cached); |
49c7754c | 2150 | |
4e335d9e PB |
2151 | int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
2152 | void *data, unsigned long len) | |
e03b644f | 2153 | { |
4e335d9e | 2154 | struct kvm_memslots *slots = kvm_memslots(kvm); |
e03b644f GN |
2155 | int r; |
2156 | ||
8f964525 AH |
2157 | BUG_ON(len > ghc->len); |
2158 | ||
e03b644f | 2159 | if (slots->generation != ghc->generation) |
5a2d4365 | 2160 | __kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len); |
8f964525 AH |
2161 | |
2162 | if (unlikely(!ghc->memslot)) | |
4e335d9e | 2163 | return kvm_read_guest(kvm, ghc->gpa, data, len); |
e03b644f GN |
2164 | |
2165 | if (kvm_is_error_hva(ghc->hva)) | |
2166 | return -EFAULT; | |
2167 | ||
2168 | r = __copy_from_user(data, (void __user *)ghc->hva, len); | |
2169 | if (r) | |
2170 | return -EFAULT; | |
2171 | ||
2172 | return 0; | |
2173 | } | |
4e335d9e | 2174 | EXPORT_SYMBOL_GPL(kvm_read_guest_cached); |
e03b644f | 2175 | |
195aefde IE |
2176 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len) |
2177 | { | |
8a3caa6d HC |
2178 | const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0))); |
2179 | ||
2180 | return kvm_write_guest_page(kvm, gfn, zero_page, offset, len); | |
195aefde IE |
2181 | } |
2182 | EXPORT_SYMBOL_GPL(kvm_clear_guest_page); | |
2183 | ||
2184 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) | |
2185 | { | |
2186 | gfn_t gfn = gpa >> PAGE_SHIFT; | |
2187 | int seg; | |
2188 | int offset = offset_in_page(gpa); | |
2189 | int ret; | |
2190 | ||
bfda0e84 | 2191 | while ((seg = next_segment(len, offset)) != 0) { |
195aefde IE |
2192 | ret = kvm_clear_guest_page(kvm, gfn, offset, seg); |
2193 | if (ret < 0) | |
2194 | return ret; | |
2195 | offset = 0; | |
2196 | len -= seg; | |
2197 | ++gfn; | |
2198 | } | |
2199 | return 0; | |
2200 | } | |
2201 | EXPORT_SYMBOL_GPL(kvm_clear_guest); | |
2202 | ||
bc009e43 | 2203 | static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, |
7940876e | 2204 | gfn_t gfn) |
6aa8b732 | 2205 | { |
7e9d619d RR |
2206 | if (memslot && memslot->dirty_bitmap) { |
2207 | unsigned long rel_gfn = gfn - memslot->base_gfn; | |
6aa8b732 | 2208 | |
b74ca3b3 | 2209 | set_bit_le(rel_gfn, memslot->dirty_bitmap); |
6aa8b732 AK |
2210 | } |
2211 | } | |
2212 | ||
49c7754c GN |
2213 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn) |
2214 | { | |
2215 | struct kvm_memory_slot *memslot; | |
2216 | ||
2217 | memslot = gfn_to_memslot(kvm, gfn); | |
bc009e43 | 2218 | mark_page_dirty_in_slot(memslot, gfn); |
49c7754c | 2219 | } |
2ba9f0d8 | 2220 | EXPORT_SYMBOL_GPL(mark_page_dirty); |
49c7754c | 2221 | |
8e73485c PB |
2222 | void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) |
2223 | { | |
2224 | struct kvm_memory_slot *memslot; | |
2225 | ||
2226 | memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); | |
2227 | mark_page_dirty_in_slot(memslot, gfn); | |
2228 | } | |
2229 | EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); | |
2230 | ||
20b7035c JS |
2231 | void kvm_sigset_activate(struct kvm_vcpu *vcpu) |
2232 | { | |
2233 | if (!vcpu->sigset_active) | |
2234 | return; | |
2235 | ||
2236 | /* | |
2237 | * This does a lockless modification of ->real_blocked, which is fine | |
2238 | * because, only current can change ->real_blocked and all readers of | |
2239 | * ->real_blocked don't care as long ->real_blocked is always a subset | |
2240 | * of ->blocked. | |
2241 | */ | |
2242 | sigprocmask(SIG_SETMASK, &vcpu->sigset, ¤t->real_blocked); | |
2243 | } | |
2244 | ||
2245 | void kvm_sigset_deactivate(struct kvm_vcpu *vcpu) | |
2246 | { | |
2247 | if (!vcpu->sigset_active) | |
2248 | return; | |
2249 | ||
2250 | sigprocmask(SIG_SETMASK, ¤t->real_blocked, NULL); | |
2251 | sigemptyset(¤t->real_blocked); | |
2252 | } | |
2253 | ||
aca6ff29 WL |
2254 | static void grow_halt_poll_ns(struct kvm_vcpu *vcpu) |
2255 | { | |
dee339b5 | 2256 | unsigned int old, val, grow, grow_start; |
aca6ff29 | 2257 | |
2cbd7824 | 2258 | old = val = vcpu->halt_poll_ns; |
dee339b5 | 2259 | grow_start = READ_ONCE(halt_poll_ns_grow_start); |
6b6de68c | 2260 | grow = READ_ONCE(halt_poll_ns_grow); |
7fa08e71 NW |
2261 | if (!grow) |
2262 | goto out; | |
2263 | ||
dee339b5 NW |
2264 | val *= grow; |
2265 | if (val < grow_start) | |
2266 | val = grow_start; | |
aca6ff29 | 2267 | |
313f636d DM |
2268 | if (val > halt_poll_ns) |
2269 | val = halt_poll_ns; | |
2270 | ||
aca6ff29 | 2271 | vcpu->halt_poll_ns = val; |
7fa08e71 | 2272 | out: |
2cbd7824 | 2273 | trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old); |
aca6ff29 WL |
2274 | } |
2275 | ||
2276 | static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu) | |
2277 | { | |
6b6de68c | 2278 | unsigned int old, val, shrink; |
aca6ff29 | 2279 | |
2cbd7824 | 2280 | old = val = vcpu->halt_poll_ns; |
6b6de68c CB |
2281 | shrink = READ_ONCE(halt_poll_ns_shrink); |
2282 | if (shrink == 0) | |
aca6ff29 WL |
2283 | val = 0; |
2284 | else | |
6b6de68c | 2285 | val /= shrink; |
aca6ff29 WL |
2286 | |
2287 | vcpu->halt_poll_ns = val; | |
2cbd7824 | 2288 | trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old); |
aca6ff29 WL |
2289 | } |
2290 | ||
f7819512 PB |
2291 | static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) |
2292 | { | |
50c28f21 JS |
2293 | int ret = -EINTR; |
2294 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
2295 | ||
f7819512 PB |
2296 | if (kvm_arch_vcpu_runnable(vcpu)) { |
2297 | kvm_make_request(KVM_REQ_UNHALT, vcpu); | |
50c28f21 | 2298 | goto out; |
f7819512 PB |
2299 | } |
2300 | if (kvm_cpu_has_pending_timer(vcpu)) | |
50c28f21 | 2301 | goto out; |
f7819512 | 2302 | if (signal_pending(current)) |
50c28f21 | 2303 | goto out; |
f7819512 | 2304 | |
50c28f21 JS |
2305 | ret = 0; |
2306 | out: | |
2307 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
2308 | return ret; | |
f7819512 PB |
2309 | } |
2310 | ||
b6958ce4 ED |
2311 | /* |
2312 | * The vCPU has executed a HLT instruction with in-kernel mode enabled. | |
2313 | */ | |
8776e519 | 2314 | void kvm_vcpu_block(struct kvm_vcpu *vcpu) |
d3bef15f | 2315 | { |
f7819512 | 2316 | ktime_t start, cur; |
8577370f | 2317 | DECLARE_SWAITQUEUE(wait); |
f7819512 | 2318 | bool waited = false; |
aca6ff29 | 2319 | u64 block_ns; |
f7819512 PB |
2320 | |
2321 | start = cur = ktime_get(); | |
cdd6ad3a | 2322 | if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) { |
19020f8a | 2323 | ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns); |
f95ef0cd | 2324 | |
62bea5bf | 2325 | ++vcpu->stat.halt_attempted_poll; |
f7819512 PB |
2326 | do { |
2327 | /* | |
2328 | * This sets KVM_REQ_UNHALT if an interrupt | |
2329 | * arrives. | |
2330 | */ | |
2331 | if (kvm_vcpu_check_block(vcpu) < 0) { | |
2332 | ++vcpu->stat.halt_successful_poll; | |
3491caf2 CB |
2333 | if (!vcpu_valid_wakeup(vcpu)) |
2334 | ++vcpu->stat.halt_poll_invalid; | |
f7819512 PB |
2335 | goto out; |
2336 | } | |
2337 | cur = ktime_get(); | |
2338 | } while (single_task_running() && ktime_before(cur, stop)); | |
2339 | } | |
e5c239cf | 2340 | |
3217f7c2 CD |
2341 | kvm_arch_vcpu_blocking(vcpu); |
2342 | ||
e5c239cf | 2343 | for (;;) { |
b3dae109 | 2344 | prepare_to_swait_exclusive(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); |
e5c239cf | 2345 | |
f7819512 | 2346 | if (kvm_vcpu_check_block(vcpu) < 0) |
e5c239cf MT |
2347 | break; |
2348 | ||
f7819512 | 2349 | waited = true; |
b6958ce4 | 2350 | schedule(); |
b6958ce4 | 2351 | } |
d3bef15f | 2352 | |
8577370f | 2353 | finish_swait(&vcpu->wq, &wait); |
f7819512 PB |
2354 | cur = ktime_get(); |
2355 | ||
3217f7c2 | 2356 | kvm_arch_vcpu_unblocking(vcpu); |
f7819512 | 2357 | out: |
aca6ff29 WL |
2358 | block_ns = ktime_to_ns(cur) - ktime_to_ns(start); |
2359 | ||
2086d320 CB |
2360 | if (!vcpu_valid_wakeup(vcpu)) |
2361 | shrink_halt_poll_ns(vcpu); | |
2362 | else if (halt_poll_ns) { | |
aca6ff29 WL |
2363 | if (block_ns <= vcpu->halt_poll_ns) |
2364 | ; | |
2365 | /* we had a long block, shrink polling */ | |
2086d320 | 2366 | else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns) |
aca6ff29 WL |
2367 | shrink_halt_poll_ns(vcpu); |
2368 | /* we had a short halt and our poll time is too small */ | |
2369 | else if (vcpu->halt_poll_ns < halt_poll_ns && | |
2370 | block_ns < halt_poll_ns) | |
2371 | grow_halt_poll_ns(vcpu); | |
edb9272f WL |
2372 | } else |
2373 | vcpu->halt_poll_ns = 0; | |
aca6ff29 | 2374 | |
3491caf2 CB |
2375 | trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu)); |
2376 | kvm_arch_vcpu_block_finish(vcpu); | |
b6958ce4 | 2377 | } |
2ba9f0d8 | 2378 | EXPORT_SYMBOL_GPL(kvm_vcpu_block); |
b6958ce4 | 2379 | |
178f02ff | 2380 | bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) |
b6d33834 | 2381 | { |
8577370f | 2382 | struct swait_queue_head *wqp; |
b6d33834 CD |
2383 | |
2384 | wqp = kvm_arch_vcpu_wq(vcpu); | |
5e0018b3 | 2385 | if (swq_has_sleeper(wqp)) { |
b3dae109 | 2386 | swake_up_one(wqp); |
b6d33834 | 2387 | ++vcpu->stat.halt_wakeup; |
178f02ff | 2388 | return true; |
b6d33834 CD |
2389 | } |
2390 | ||
178f02ff | 2391 | return false; |
dd1a4cc1 RK |
2392 | } |
2393 | EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up); | |
2394 | ||
0266c894 | 2395 | #ifndef CONFIG_S390 |
dd1a4cc1 RK |
2396 | /* |
2397 | * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. | |
2398 | */ | |
2399 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu) | |
2400 | { | |
2401 | int me; | |
2402 | int cpu = vcpu->cpu; | |
2403 | ||
178f02ff RK |
2404 | if (kvm_vcpu_wake_up(vcpu)) |
2405 | return; | |
2406 | ||
b6d33834 CD |
2407 | me = get_cpu(); |
2408 | if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) | |
2409 | if (kvm_arch_vcpu_should_kick(vcpu)) | |
2410 | smp_send_reschedule(cpu); | |
2411 | put_cpu(); | |
2412 | } | |
a20ed54d | 2413 | EXPORT_SYMBOL_GPL(kvm_vcpu_kick); |
0266c894 | 2414 | #endif /* !CONFIG_S390 */ |
b6d33834 | 2415 | |
fa93384f | 2416 | int kvm_vcpu_yield_to(struct kvm_vcpu *target) |
41628d33 KW |
2417 | { |
2418 | struct pid *pid; | |
2419 | struct task_struct *task = NULL; | |
fa93384f | 2420 | int ret = 0; |
41628d33 KW |
2421 | |
2422 | rcu_read_lock(); | |
2423 | pid = rcu_dereference(target->pid); | |
2424 | if (pid) | |
27fbe64b | 2425 | task = get_pid_task(pid, PIDTYPE_PID); |
41628d33 KW |
2426 | rcu_read_unlock(); |
2427 | if (!task) | |
c45c528e | 2428 | return ret; |
c45c528e | 2429 | ret = yield_to(task, 1); |
41628d33 | 2430 | put_task_struct(task); |
c45c528e R |
2431 | |
2432 | return ret; | |
41628d33 KW |
2433 | } |
2434 | EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to); | |
2435 | ||
06e48c51 R |
2436 | /* |
2437 | * Helper that checks whether a VCPU is eligible for directed yield. | |
2438 | * Most eligible candidate to yield is decided by following heuristics: | |
2439 | * | |
2440 | * (a) VCPU which has not done pl-exit or cpu relax intercepted recently | |
2441 | * (preempted lock holder), indicated by @in_spin_loop. | |
2442 | * Set at the beiginning and cleared at the end of interception/PLE handler. | |
2443 | * | |
2444 | * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get | |
2445 | * chance last time (mostly it has become eligible now since we have probably | |
2446 | * yielded to lockholder in last iteration. This is done by toggling | |
2447 | * @dy_eligible each time a VCPU checked for eligibility.) | |
2448 | * | |
2449 | * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding | |
2450 | * to preempted lock-holder could result in wrong VCPU selection and CPU | |
2451 | * burning. Giving priority for a potential lock-holder increases lock | |
2452 | * progress. | |
2453 | * | |
2454 | * Since algorithm is based on heuristics, accessing another VCPU data without | |
2455 | * locking does not harm. It may result in trying to yield to same VCPU, fail | |
2456 | * and continue with next VCPU and so on. | |
2457 | */ | |
7940876e | 2458 | static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu) |
06e48c51 | 2459 | { |
4a55dd72 | 2460 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
06e48c51 R |
2461 | bool eligible; |
2462 | ||
2463 | eligible = !vcpu->spin_loop.in_spin_loop || | |
34656113 | 2464 | vcpu->spin_loop.dy_eligible; |
06e48c51 R |
2465 | |
2466 | if (vcpu->spin_loop.in_spin_loop) | |
2467 | kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible); | |
2468 | ||
2469 | return eligible; | |
4a55dd72 SW |
2470 | #else |
2471 | return true; | |
06e48c51 | 2472 | #endif |
4a55dd72 | 2473 | } |
c45c528e | 2474 | |
199b5763 | 2475 | void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) |
d255f4f2 | 2476 | { |
217ece61 RR |
2477 | struct kvm *kvm = me->kvm; |
2478 | struct kvm_vcpu *vcpu; | |
2479 | int last_boosted_vcpu = me->kvm->last_boosted_vcpu; | |
2480 | int yielded = 0; | |
c45c528e | 2481 | int try = 3; |
217ece61 RR |
2482 | int pass; |
2483 | int i; | |
d255f4f2 | 2484 | |
4c088493 | 2485 | kvm_vcpu_set_in_spin_loop(me, true); |
217ece61 RR |
2486 | /* |
2487 | * We boost the priority of a VCPU that is runnable but not | |
2488 | * currently running, because it got preempted by something | |
2489 | * else and called schedule in __vcpu_run. Hopefully that | |
2490 | * VCPU is holding the lock that we need and will release it. | |
2491 | * We approximate round-robin by starting at the last boosted VCPU. | |
2492 | */ | |
c45c528e | 2493 | for (pass = 0; pass < 2 && !yielded && try; pass++) { |
217ece61 | 2494 | kvm_for_each_vcpu(i, vcpu, kvm) { |
5cfc2aab | 2495 | if (!pass && i <= last_boosted_vcpu) { |
217ece61 RR |
2496 | i = last_boosted_vcpu; |
2497 | continue; | |
2498 | } else if (pass && i > last_boosted_vcpu) | |
2499 | break; | |
6aa7de05 | 2500 | if (!READ_ONCE(vcpu->preempted)) |
7bc7ae25 | 2501 | continue; |
217ece61 RR |
2502 | if (vcpu == me) |
2503 | continue; | |
8577370f | 2504 | if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu)) |
217ece61 | 2505 | continue; |
199b5763 LM |
2506 | if (yield_to_kernel_mode && !kvm_arch_vcpu_in_kernel(vcpu)) |
2507 | continue; | |
06e48c51 R |
2508 | if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) |
2509 | continue; | |
c45c528e R |
2510 | |
2511 | yielded = kvm_vcpu_yield_to(vcpu); | |
2512 | if (yielded > 0) { | |
217ece61 | 2513 | kvm->last_boosted_vcpu = i; |
217ece61 | 2514 | break; |
c45c528e R |
2515 | } else if (yielded < 0) { |
2516 | try--; | |
2517 | if (!try) | |
2518 | break; | |
217ece61 | 2519 | } |
217ece61 RR |
2520 | } |
2521 | } | |
4c088493 | 2522 | kvm_vcpu_set_in_spin_loop(me, false); |
06e48c51 R |
2523 | |
2524 | /* Ensure vcpu is not eligible during next spinloop */ | |
2525 | kvm_vcpu_set_dy_eligible(me, false); | |
d255f4f2 ZE |
2526 | } |
2527 | EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin); | |
2528 | ||
1499fa80 | 2529 | static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf) |
9a2bb7f4 | 2530 | { |
11bac800 | 2531 | struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data; |
9a2bb7f4 AK |
2532 | struct page *page; |
2533 | ||
e4a533a4 | 2534 | if (vmf->pgoff == 0) |
039576c0 | 2535 | page = virt_to_page(vcpu->run); |
09566765 | 2536 | #ifdef CONFIG_X86 |
e4a533a4 | 2537 | else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET) |
ad312c7c | 2538 | page = virt_to_page(vcpu->arch.pio_data); |
5f94c174 | 2539 | #endif |
4b4357e0 | 2540 | #ifdef CONFIG_KVM_MMIO |
5f94c174 LV |
2541 | else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET) |
2542 | page = virt_to_page(vcpu->kvm->coalesced_mmio_ring); | |
09566765 | 2543 | #endif |
039576c0 | 2544 | else |
5b1c1493 | 2545 | return kvm_arch_vcpu_fault(vcpu, vmf); |
9a2bb7f4 | 2546 | get_page(page); |
e4a533a4 | 2547 | vmf->page = page; |
2548 | return 0; | |
9a2bb7f4 AK |
2549 | } |
2550 | ||
f0f37e2f | 2551 | static const struct vm_operations_struct kvm_vcpu_vm_ops = { |
e4a533a4 | 2552 | .fault = kvm_vcpu_fault, |
9a2bb7f4 AK |
2553 | }; |
2554 | ||
2555 | static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) | |
2556 | { | |
2557 | vma->vm_ops = &kvm_vcpu_vm_ops; | |
2558 | return 0; | |
2559 | } | |
2560 | ||
bccf2150 AK |
2561 | static int kvm_vcpu_release(struct inode *inode, struct file *filp) |
2562 | { | |
2563 | struct kvm_vcpu *vcpu = filp->private_data; | |
2564 | ||
45b5939e | 2565 | debugfs_remove_recursive(vcpu->debugfs_dentry); |
66c0b394 | 2566 | kvm_put_kvm(vcpu->kvm); |
bccf2150 AK |
2567 | return 0; |
2568 | } | |
2569 | ||
3d3aab1b | 2570 | static struct file_operations kvm_vcpu_fops = { |
bccf2150 AK |
2571 | .release = kvm_vcpu_release, |
2572 | .unlocked_ioctl = kvm_vcpu_ioctl, | |
9a2bb7f4 | 2573 | .mmap = kvm_vcpu_mmap, |
6038f373 | 2574 | .llseek = noop_llseek, |
7ddfd3e0 | 2575 | KVM_COMPAT(kvm_vcpu_compat_ioctl), |
bccf2150 AK |
2576 | }; |
2577 | ||
2578 | /* | |
2579 | * Allocates an inode for the vcpu. | |
2580 | */ | |
2581 | static int create_vcpu_fd(struct kvm_vcpu *vcpu) | |
2582 | { | |
e46b4692 MY |
2583 | char name[8 + 1 + ITOA_MAX_LEN + 1]; |
2584 | ||
2585 | snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id); | |
2586 | return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC); | |
bccf2150 AK |
2587 | } |
2588 | ||
45b5939e LC |
2589 | static int kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) |
2590 | { | |
2591 | char dir_name[ITOA_MAX_LEN * 2]; | |
2592 | int ret; | |
2593 | ||
2594 | if (!kvm_arch_has_vcpu_debugfs()) | |
2595 | return 0; | |
2596 | ||
2597 | if (!debugfs_initialized()) | |
2598 | return 0; | |
2599 | ||
2600 | snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id); | |
2601 | vcpu->debugfs_dentry = debugfs_create_dir(dir_name, | |
2602 | vcpu->kvm->debugfs_dentry); | |
2603 | if (!vcpu->debugfs_dentry) | |
2604 | return -ENOMEM; | |
2605 | ||
2606 | ret = kvm_arch_create_vcpu_debugfs(vcpu); | |
2607 | if (ret < 0) { | |
2608 | debugfs_remove_recursive(vcpu->debugfs_dentry); | |
2609 | return ret; | |
2610 | } | |
2611 | ||
2612 | return 0; | |
2613 | } | |
2614 | ||
c5ea7660 AK |
2615 | /* |
2616 | * Creates some virtual cpus. Good luck creating more than one. | |
2617 | */ | |
73880c80 | 2618 | static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) |
c5ea7660 AK |
2619 | { |
2620 | int r; | |
e09fefde | 2621 | struct kvm_vcpu *vcpu; |
c5ea7660 | 2622 | |
0b1b1dfd | 2623 | if (id >= KVM_MAX_VCPU_ID) |
338c7dba AH |
2624 | return -EINVAL; |
2625 | ||
6c7caebc PB |
2626 | mutex_lock(&kvm->lock); |
2627 | if (kvm->created_vcpus == KVM_MAX_VCPUS) { | |
2628 | mutex_unlock(&kvm->lock); | |
2629 | return -EINVAL; | |
2630 | } | |
2631 | ||
2632 | kvm->created_vcpus++; | |
2633 | mutex_unlock(&kvm->lock); | |
2634 | ||
73880c80 | 2635 | vcpu = kvm_arch_vcpu_create(kvm, id); |
6c7caebc PB |
2636 | if (IS_ERR(vcpu)) { |
2637 | r = PTR_ERR(vcpu); | |
2638 | goto vcpu_decrement; | |
2639 | } | |
c5ea7660 | 2640 | |
15ad7146 AK |
2641 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
2642 | ||
26e5215f AK |
2643 | r = kvm_arch_vcpu_setup(vcpu); |
2644 | if (r) | |
d780592b | 2645 | goto vcpu_destroy; |
26e5215f | 2646 | |
45b5939e LC |
2647 | r = kvm_create_vcpu_debugfs(vcpu); |
2648 | if (r) | |
2649 | goto vcpu_destroy; | |
2650 | ||
11ec2804 | 2651 | mutex_lock(&kvm->lock); |
e09fefde DH |
2652 | if (kvm_get_vcpu_by_id(kvm, id)) { |
2653 | r = -EEXIST; | |
2654 | goto unlock_vcpu_destroy; | |
2655 | } | |
73880c80 GN |
2656 | |
2657 | BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); | |
c5ea7660 | 2658 | |
fb3f0f51 | 2659 | /* Now it's all set up, let userspace reach it */ |
66c0b394 | 2660 | kvm_get_kvm(kvm); |
bccf2150 | 2661 | r = create_vcpu_fd(vcpu); |
73880c80 GN |
2662 | if (r < 0) { |
2663 | kvm_put_kvm(kvm); | |
d780592b | 2664 | goto unlock_vcpu_destroy; |
73880c80 GN |
2665 | } |
2666 | ||
2667 | kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; | |
dd489240 PB |
2668 | |
2669 | /* | |
2670 | * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus | |
2671 | * before kvm->online_vcpu's incremented value. | |
2672 | */ | |
73880c80 GN |
2673 | smp_wmb(); |
2674 | atomic_inc(&kvm->online_vcpus); | |
2675 | ||
73880c80 | 2676 | mutex_unlock(&kvm->lock); |
42897d86 | 2677 | kvm_arch_vcpu_postcreate(vcpu); |
fb3f0f51 | 2678 | return r; |
39c3b86e | 2679 | |
d780592b | 2680 | unlock_vcpu_destroy: |
7d8fece6 | 2681 | mutex_unlock(&kvm->lock); |
45b5939e | 2682 | debugfs_remove_recursive(vcpu->debugfs_dentry); |
d780592b | 2683 | vcpu_destroy: |
d40ccc62 | 2684 | kvm_arch_vcpu_destroy(vcpu); |
6c7caebc PB |
2685 | vcpu_decrement: |
2686 | mutex_lock(&kvm->lock); | |
2687 | kvm->created_vcpus--; | |
2688 | mutex_unlock(&kvm->lock); | |
c5ea7660 AK |
2689 | return r; |
2690 | } | |
2691 | ||
1961d276 AK |
2692 | static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) |
2693 | { | |
2694 | if (sigset) { | |
2695 | sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP)); | |
2696 | vcpu->sigset_active = 1; | |
2697 | vcpu->sigset = *sigset; | |
2698 | } else | |
2699 | vcpu->sigset_active = 0; | |
2700 | return 0; | |
2701 | } | |
2702 | ||
bccf2150 AK |
2703 | static long kvm_vcpu_ioctl(struct file *filp, |
2704 | unsigned int ioctl, unsigned long arg) | |
6aa8b732 | 2705 | { |
bccf2150 | 2706 | struct kvm_vcpu *vcpu = filp->private_data; |
2f366987 | 2707 | void __user *argp = (void __user *)arg; |
313a3dc7 | 2708 | int r; |
fa3795a7 DH |
2709 | struct kvm_fpu *fpu = NULL; |
2710 | struct kvm_sregs *kvm_sregs = NULL; | |
6aa8b732 | 2711 | |
6d4e4c4f AK |
2712 | if (vcpu->kvm->mm != current->mm) |
2713 | return -EIO; | |
2122ff5e | 2714 | |
2ea75be3 DM |
2715 | if (unlikely(_IOC_TYPE(ioctl) != KVMIO)) |
2716 | return -EINVAL; | |
2717 | ||
2122ff5e | 2718 | /* |
5cb0944c PB |
2719 | * Some architectures have vcpu ioctls that are asynchronous to vcpu |
2720 | * execution; mutex_lock() would break them. | |
2122ff5e | 2721 | */ |
5cb0944c PB |
2722 | r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg); |
2723 | if (r != -ENOIOCTLCMD) | |
9fc77441 | 2724 | return r; |
2122ff5e | 2725 | |
ec7660cc CD |
2726 | if (mutex_lock_killable(&vcpu->mutex)) |
2727 | return -EINTR; | |
6aa8b732 | 2728 | switch (ioctl) { |
0e4524a5 CB |
2729 | case KVM_RUN: { |
2730 | struct pid *oldpid; | |
f0fe5108 AK |
2731 | r = -EINVAL; |
2732 | if (arg) | |
2733 | goto out; | |
0e4524a5 | 2734 | oldpid = rcu_access_pointer(vcpu->pid); |
71dbc8a9 | 2735 | if (unlikely(oldpid != task_pid(current))) { |
7a72f7a1 | 2736 | /* The thread running this VCPU changed. */ |
bd2a6394 | 2737 | struct pid *newpid; |
f95ef0cd | 2738 | |
bd2a6394 CD |
2739 | r = kvm_arch_vcpu_run_pid_change(vcpu); |
2740 | if (r) | |
2741 | break; | |
2742 | ||
2743 | newpid = get_task_pid(current, PIDTYPE_PID); | |
7a72f7a1 CB |
2744 | rcu_assign_pointer(vcpu->pid, newpid); |
2745 | if (oldpid) | |
2746 | synchronize_rcu(); | |
2747 | put_pid(oldpid); | |
2748 | } | |
b6c7a5dc | 2749 | r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run); |
64be5007 | 2750 | trace_kvm_userspace_exit(vcpu->run->exit_reason, r); |
6aa8b732 | 2751 | break; |
0e4524a5 | 2752 | } |
6aa8b732 | 2753 | case KVM_GET_REGS: { |
3e4bb3ac | 2754 | struct kvm_regs *kvm_regs; |
6aa8b732 | 2755 | |
3e4bb3ac | 2756 | r = -ENOMEM; |
b12ce36a | 2757 | kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT); |
3e4bb3ac | 2758 | if (!kvm_regs) |
6aa8b732 | 2759 | goto out; |
3e4bb3ac XZ |
2760 | r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs); |
2761 | if (r) | |
2762 | goto out_free1; | |
6aa8b732 | 2763 | r = -EFAULT; |
3e4bb3ac XZ |
2764 | if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs))) |
2765 | goto out_free1; | |
6aa8b732 | 2766 | r = 0; |
3e4bb3ac XZ |
2767 | out_free1: |
2768 | kfree(kvm_regs); | |
6aa8b732 AK |
2769 | break; |
2770 | } | |
2771 | case KVM_SET_REGS: { | |
3e4bb3ac | 2772 | struct kvm_regs *kvm_regs; |
6aa8b732 | 2773 | |
3e4bb3ac | 2774 | r = -ENOMEM; |
ff5c2c03 SL |
2775 | kvm_regs = memdup_user(argp, sizeof(*kvm_regs)); |
2776 | if (IS_ERR(kvm_regs)) { | |
2777 | r = PTR_ERR(kvm_regs); | |
6aa8b732 | 2778 | goto out; |
ff5c2c03 | 2779 | } |
3e4bb3ac | 2780 | r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs); |
3e4bb3ac | 2781 | kfree(kvm_regs); |
6aa8b732 AK |
2782 | break; |
2783 | } | |
2784 | case KVM_GET_SREGS: { | |
b12ce36a BG |
2785 | kvm_sregs = kzalloc(sizeof(struct kvm_sregs), |
2786 | GFP_KERNEL_ACCOUNT); | |
fa3795a7 DH |
2787 | r = -ENOMEM; |
2788 | if (!kvm_sregs) | |
2789 | goto out; | |
2790 | r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs); | |
6aa8b732 AK |
2791 | if (r) |
2792 | goto out; | |
2793 | r = -EFAULT; | |
fa3795a7 | 2794 | if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs))) |
6aa8b732 AK |
2795 | goto out; |
2796 | r = 0; | |
2797 | break; | |
2798 | } | |
2799 | case KVM_SET_SREGS: { | |
ff5c2c03 SL |
2800 | kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs)); |
2801 | if (IS_ERR(kvm_sregs)) { | |
2802 | r = PTR_ERR(kvm_sregs); | |
18595411 | 2803 | kvm_sregs = NULL; |
6aa8b732 | 2804 | goto out; |
ff5c2c03 | 2805 | } |
fa3795a7 | 2806 | r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); |
6aa8b732 AK |
2807 | break; |
2808 | } | |
62d9f0db MT |
2809 | case KVM_GET_MP_STATE: { |
2810 | struct kvm_mp_state mp_state; | |
2811 | ||
2812 | r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state); | |
2813 | if (r) | |
2814 | goto out; | |
2815 | r = -EFAULT; | |
893bdbf1 | 2816 | if (copy_to_user(argp, &mp_state, sizeof(mp_state))) |
62d9f0db MT |
2817 | goto out; |
2818 | r = 0; | |
2819 | break; | |
2820 | } | |
2821 | case KVM_SET_MP_STATE: { | |
2822 | struct kvm_mp_state mp_state; | |
2823 | ||
2824 | r = -EFAULT; | |
893bdbf1 | 2825 | if (copy_from_user(&mp_state, argp, sizeof(mp_state))) |
62d9f0db MT |
2826 | goto out; |
2827 | r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state); | |
62d9f0db MT |
2828 | break; |
2829 | } | |
6aa8b732 AK |
2830 | case KVM_TRANSLATE: { |
2831 | struct kvm_translation tr; | |
2832 | ||
2833 | r = -EFAULT; | |
893bdbf1 | 2834 | if (copy_from_user(&tr, argp, sizeof(tr))) |
6aa8b732 | 2835 | goto out; |
8b006791 | 2836 | r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr); |
6aa8b732 AK |
2837 | if (r) |
2838 | goto out; | |
2839 | r = -EFAULT; | |
893bdbf1 | 2840 | if (copy_to_user(argp, &tr, sizeof(tr))) |
6aa8b732 AK |
2841 | goto out; |
2842 | r = 0; | |
2843 | break; | |
2844 | } | |
d0bfb940 JK |
2845 | case KVM_SET_GUEST_DEBUG: { |
2846 | struct kvm_guest_debug dbg; | |
6aa8b732 AK |
2847 | |
2848 | r = -EFAULT; | |
893bdbf1 | 2849 | if (copy_from_user(&dbg, argp, sizeof(dbg))) |
6aa8b732 | 2850 | goto out; |
d0bfb940 | 2851 | r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg); |
6aa8b732 AK |
2852 | break; |
2853 | } | |
1961d276 AK |
2854 | case KVM_SET_SIGNAL_MASK: { |
2855 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2856 | struct kvm_signal_mask kvm_sigmask; | |
2857 | sigset_t sigset, *p; | |
2858 | ||
2859 | p = NULL; | |
2860 | if (argp) { | |
2861 | r = -EFAULT; | |
2862 | if (copy_from_user(&kvm_sigmask, argp, | |
893bdbf1 | 2863 | sizeof(kvm_sigmask))) |
1961d276 AK |
2864 | goto out; |
2865 | r = -EINVAL; | |
893bdbf1 | 2866 | if (kvm_sigmask.len != sizeof(sigset)) |
1961d276 AK |
2867 | goto out; |
2868 | r = -EFAULT; | |
2869 | if (copy_from_user(&sigset, sigmask_arg->sigset, | |
893bdbf1 | 2870 | sizeof(sigset))) |
1961d276 AK |
2871 | goto out; |
2872 | p = &sigset; | |
2873 | } | |
376d41ff | 2874 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, p); |
1961d276 AK |
2875 | break; |
2876 | } | |
b8836737 | 2877 | case KVM_GET_FPU: { |
b12ce36a | 2878 | fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT); |
fa3795a7 DH |
2879 | r = -ENOMEM; |
2880 | if (!fpu) | |
2881 | goto out; | |
2882 | r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu); | |
b8836737 AK |
2883 | if (r) |
2884 | goto out; | |
2885 | r = -EFAULT; | |
fa3795a7 | 2886 | if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu))) |
b8836737 AK |
2887 | goto out; |
2888 | r = 0; | |
2889 | break; | |
2890 | } | |
2891 | case KVM_SET_FPU: { | |
ff5c2c03 SL |
2892 | fpu = memdup_user(argp, sizeof(*fpu)); |
2893 | if (IS_ERR(fpu)) { | |
2894 | r = PTR_ERR(fpu); | |
18595411 | 2895 | fpu = NULL; |
b8836737 | 2896 | goto out; |
ff5c2c03 | 2897 | } |
fa3795a7 | 2898 | r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu); |
b8836737 AK |
2899 | break; |
2900 | } | |
bccf2150 | 2901 | default: |
313a3dc7 | 2902 | r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); |
bccf2150 AK |
2903 | } |
2904 | out: | |
ec7660cc | 2905 | mutex_unlock(&vcpu->mutex); |
fa3795a7 DH |
2906 | kfree(fpu); |
2907 | kfree(kvm_sregs); | |
bccf2150 AK |
2908 | return r; |
2909 | } | |
2910 | ||
de8e5d74 | 2911 | #ifdef CONFIG_KVM_COMPAT |
1dda606c AG |
2912 | static long kvm_vcpu_compat_ioctl(struct file *filp, |
2913 | unsigned int ioctl, unsigned long arg) | |
2914 | { | |
2915 | struct kvm_vcpu *vcpu = filp->private_data; | |
2916 | void __user *argp = compat_ptr(arg); | |
2917 | int r; | |
2918 | ||
2919 | if (vcpu->kvm->mm != current->mm) | |
2920 | return -EIO; | |
2921 | ||
2922 | switch (ioctl) { | |
2923 | case KVM_SET_SIGNAL_MASK: { | |
2924 | struct kvm_signal_mask __user *sigmask_arg = argp; | |
2925 | struct kvm_signal_mask kvm_sigmask; | |
1dda606c AG |
2926 | sigset_t sigset; |
2927 | ||
2928 | if (argp) { | |
2929 | r = -EFAULT; | |
2930 | if (copy_from_user(&kvm_sigmask, argp, | |
893bdbf1 | 2931 | sizeof(kvm_sigmask))) |
1dda606c AG |
2932 | goto out; |
2933 | r = -EINVAL; | |
3968cf62 | 2934 | if (kvm_sigmask.len != sizeof(compat_sigset_t)) |
1dda606c AG |
2935 | goto out; |
2936 | r = -EFAULT; | |
3968cf62 | 2937 | if (get_compat_sigset(&sigset, (void *)sigmask_arg->sigset)) |
1dda606c | 2938 | goto out; |
760a9a30 AC |
2939 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset); |
2940 | } else | |
2941 | r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL); | |
1dda606c AG |
2942 | break; |
2943 | } | |
2944 | default: | |
2945 | r = kvm_vcpu_ioctl(filp, ioctl, arg); | |
2946 | } | |
2947 | ||
2948 | out: | |
2949 | return r; | |
2950 | } | |
2951 | #endif | |
2952 | ||
a1cd3f08 CLG |
2953 | static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma) |
2954 | { | |
2955 | struct kvm_device *dev = filp->private_data; | |
2956 | ||
2957 | if (dev->ops->mmap) | |
2958 | return dev->ops->mmap(dev, vma); | |
2959 | ||
2960 | return -ENODEV; | |
2961 | } | |
2962 | ||
852b6d57 SW |
2963 | static int kvm_device_ioctl_attr(struct kvm_device *dev, |
2964 | int (*accessor)(struct kvm_device *dev, | |
2965 | struct kvm_device_attr *attr), | |
2966 | unsigned long arg) | |
2967 | { | |
2968 | struct kvm_device_attr attr; | |
2969 | ||
2970 | if (!accessor) | |
2971 | return -EPERM; | |
2972 | ||
2973 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
2974 | return -EFAULT; | |
2975 | ||
2976 | return accessor(dev, &attr); | |
2977 | } | |
2978 | ||
2979 | static long kvm_device_ioctl(struct file *filp, unsigned int ioctl, | |
2980 | unsigned long arg) | |
2981 | { | |
2982 | struct kvm_device *dev = filp->private_data; | |
2983 | ||
ddba9180 SC |
2984 | if (dev->kvm->mm != current->mm) |
2985 | return -EIO; | |
2986 | ||
852b6d57 SW |
2987 | switch (ioctl) { |
2988 | case KVM_SET_DEVICE_ATTR: | |
2989 | return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg); | |
2990 | case KVM_GET_DEVICE_ATTR: | |
2991 | return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg); | |
2992 | case KVM_HAS_DEVICE_ATTR: | |
2993 | return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg); | |
2994 | default: | |
2995 | if (dev->ops->ioctl) | |
2996 | return dev->ops->ioctl(dev, ioctl, arg); | |
2997 | ||
2998 | return -ENOTTY; | |
2999 | } | |
3000 | } | |
3001 | ||
852b6d57 SW |
3002 | static int kvm_device_release(struct inode *inode, struct file *filp) |
3003 | { | |
3004 | struct kvm_device *dev = filp->private_data; | |
3005 | struct kvm *kvm = dev->kvm; | |
3006 | ||
2bde9b3e CLG |
3007 | if (dev->ops->release) { |
3008 | mutex_lock(&kvm->lock); | |
3009 | list_del(&dev->vm_node); | |
3010 | dev->ops->release(dev); | |
3011 | mutex_unlock(&kvm->lock); | |
3012 | } | |
3013 | ||
852b6d57 SW |
3014 | kvm_put_kvm(kvm); |
3015 | return 0; | |
3016 | } | |
3017 | ||
3018 | static const struct file_operations kvm_device_fops = { | |
3019 | .unlocked_ioctl = kvm_device_ioctl, | |
3020 | .release = kvm_device_release, | |
7ddfd3e0 | 3021 | KVM_COMPAT(kvm_device_ioctl), |
a1cd3f08 | 3022 | .mmap = kvm_device_mmap, |
852b6d57 SW |
3023 | }; |
3024 | ||
3025 | struct kvm_device *kvm_device_from_filp(struct file *filp) | |
3026 | { | |
3027 | if (filp->f_op != &kvm_device_fops) | |
3028 | return NULL; | |
3029 | ||
3030 | return filp->private_data; | |
3031 | } | |
3032 | ||
d60eacb0 | 3033 | static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = { |
5df554ad | 3034 | #ifdef CONFIG_KVM_MPIC |
d60eacb0 WD |
3035 | [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops, |
3036 | [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops, | |
5975a2e0 | 3037 | #endif |
d60eacb0 WD |
3038 | }; |
3039 | ||
3040 | int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type) | |
3041 | { | |
3042 | if (type >= ARRAY_SIZE(kvm_device_ops_table)) | |
3043 | return -ENOSPC; | |
3044 | ||
3045 | if (kvm_device_ops_table[type] != NULL) | |
3046 | return -EEXIST; | |
3047 | ||
3048 | kvm_device_ops_table[type] = ops; | |
3049 | return 0; | |
3050 | } | |
3051 | ||
571ee1b6 WL |
3052 | void kvm_unregister_device_ops(u32 type) |
3053 | { | |
3054 | if (kvm_device_ops_table[type] != NULL) | |
3055 | kvm_device_ops_table[type] = NULL; | |
3056 | } | |
3057 | ||
852b6d57 SW |
3058 | static int kvm_ioctl_create_device(struct kvm *kvm, |
3059 | struct kvm_create_device *cd) | |
3060 | { | |
3061 | struct kvm_device_ops *ops = NULL; | |
3062 | struct kvm_device *dev; | |
3063 | bool test = cd->flags & KVM_CREATE_DEVICE_TEST; | |
1d487e9b | 3064 | int type; |
852b6d57 SW |
3065 | int ret; |
3066 | ||
d60eacb0 WD |
3067 | if (cd->type >= ARRAY_SIZE(kvm_device_ops_table)) |
3068 | return -ENODEV; | |
3069 | ||
1d487e9b PB |
3070 | type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table)); |
3071 | ops = kvm_device_ops_table[type]; | |
d60eacb0 | 3072 | if (ops == NULL) |
852b6d57 | 3073 | return -ENODEV; |
852b6d57 SW |
3074 | |
3075 | if (test) | |
3076 | return 0; | |
3077 | ||
b12ce36a | 3078 | dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT); |
852b6d57 SW |
3079 | if (!dev) |
3080 | return -ENOMEM; | |
3081 | ||
3082 | dev->ops = ops; | |
3083 | dev->kvm = kvm; | |
852b6d57 | 3084 | |
a28ebea2 | 3085 | mutex_lock(&kvm->lock); |
1d487e9b | 3086 | ret = ops->create(dev, type); |
852b6d57 | 3087 | if (ret < 0) { |
a28ebea2 | 3088 | mutex_unlock(&kvm->lock); |
852b6d57 SW |
3089 | kfree(dev); |
3090 | return ret; | |
3091 | } | |
a28ebea2 CD |
3092 | list_add(&dev->vm_node, &kvm->devices); |
3093 | mutex_unlock(&kvm->lock); | |
852b6d57 | 3094 | |
023e9fdd CD |
3095 | if (ops->init) |
3096 | ops->init(dev); | |
3097 | ||
cfa39381 | 3098 | kvm_get_kvm(kvm); |
24009b05 | 3099 | ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); |
852b6d57 | 3100 | if (ret < 0) { |
cfa39381 | 3101 | kvm_put_kvm(kvm); |
a28ebea2 CD |
3102 | mutex_lock(&kvm->lock); |
3103 | list_del(&dev->vm_node); | |
3104 | mutex_unlock(&kvm->lock); | |
a0f1d21c | 3105 | ops->destroy(dev); |
852b6d57 SW |
3106 | return ret; |
3107 | } | |
3108 | ||
852b6d57 SW |
3109 | cd->fd = ret; |
3110 | return 0; | |
3111 | } | |
3112 | ||
92b591a4 AG |
3113 | static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) |
3114 | { | |
3115 | switch (arg) { | |
3116 | case KVM_CAP_USER_MEMORY: | |
3117 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: | |
3118 | case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: | |
92b591a4 AG |
3119 | case KVM_CAP_INTERNAL_ERROR_DATA: |
3120 | #ifdef CONFIG_HAVE_KVM_MSI | |
3121 | case KVM_CAP_SIGNAL_MSI: | |
3122 | #endif | |
297e2105 | 3123 | #ifdef CONFIG_HAVE_KVM_IRQFD |
dc9be0fa | 3124 | case KVM_CAP_IRQFD: |
92b591a4 AG |
3125 | case KVM_CAP_IRQFD_RESAMPLE: |
3126 | #endif | |
e9ea5069 | 3127 | case KVM_CAP_IOEVENTFD_ANY_LENGTH: |
92b591a4 | 3128 | case KVM_CAP_CHECK_EXTENSION_VM: |
e5d83c74 | 3129 | case KVM_CAP_ENABLE_CAP_VM: |
2a31b9db | 3130 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
d7547c55 | 3131 | case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: |
2a31b9db | 3132 | #endif |
92b591a4 | 3133 | return 1; |
4b4357e0 | 3134 | #ifdef CONFIG_KVM_MMIO |
30422558 PB |
3135 | case KVM_CAP_COALESCED_MMIO: |
3136 | return KVM_COALESCED_MMIO_PAGE_OFFSET; | |
0804c849 PH |
3137 | case KVM_CAP_COALESCED_PIO: |
3138 | return 1; | |
30422558 | 3139 | #endif |
92b591a4 AG |
3140 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
3141 | case KVM_CAP_IRQ_ROUTING: | |
3142 | return KVM_MAX_IRQ_ROUTES; | |
f481b069 PB |
3143 | #endif |
3144 | #if KVM_ADDRESS_SPACE_NUM > 1 | |
3145 | case KVM_CAP_MULTI_ADDRESS_SPACE: | |
3146 | return KVM_ADDRESS_SPACE_NUM; | |
92b591a4 | 3147 | #endif |
0b1b1dfd GK |
3148 | case KVM_CAP_MAX_VCPU_ID: |
3149 | return KVM_MAX_VCPU_ID; | |
c110ae57 PB |
3150 | case KVM_CAP_NR_MEMSLOTS: |
3151 | return KVM_USER_MEM_SLOTS; | |
92b591a4 AG |
3152 | default: |
3153 | break; | |
3154 | } | |
3155 | return kvm_vm_ioctl_check_extension(kvm, arg); | |
3156 | } | |
3157 | ||
e5d83c74 PB |
3158 | int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
3159 | struct kvm_enable_cap *cap) | |
3160 | { | |
3161 | return -EINVAL; | |
3162 | } | |
3163 | ||
3164 | static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, | |
3165 | struct kvm_enable_cap *cap) | |
3166 | { | |
3167 | switch (cap->cap) { | |
2a31b9db | 3168 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
d7547c55 | 3169 | case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: |
2a31b9db PB |
3170 | if (cap->flags || (cap->args[0] & ~1)) |
3171 | return -EINVAL; | |
3172 | kvm->manual_dirty_log_protect = cap->args[0]; | |
3173 | return 0; | |
3174 | #endif | |
e5d83c74 PB |
3175 | default: |
3176 | return kvm_vm_ioctl_enable_cap(kvm, cap); | |
3177 | } | |
3178 | } | |
3179 | ||
bccf2150 AK |
3180 | static long kvm_vm_ioctl(struct file *filp, |
3181 | unsigned int ioctl, unsigned long arg) | |
3182 | { | |
3183 | struct kvm *kvm = filp->private_data; | |
3184 | void __user *argp = (void __user *)arg; | |
1fe779f8 | 3185 | int r; |
bccf2150 | 3186 | |
6d4e4c4f AK |
3187 | if (kvm->mm != current->mm) |
3188 | return -EIO; | |
bccf2150 AK |
3189 | switch (ioctl) { |
3190 | case KVM_CREATE_VCPU: | |
3191 | r = kvm_vm_ioctl_create_vcpu(kvm, arg); | |
bccf2150 | 3192 | break; |
e5d83c74 PB |
3193 | case KVM_ENABLE_CAP: { |
3194 | struct kvm_enable_cap cap; | |
3195 | ||
3196 | r = -EFAULT; | |
3197 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
3198 | goto out; | |
3199 | r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap); | |
3200 | break; | |
3201 | } | |
6fc138d2 IE |
3202 | case KVM_SET_USER_MEMORY_REGION: { |
3203 | struct kvm_userspace_memory_region kvm_userspace_mem; | |
3204 | ||
3205 | r = -EFAULT; | |
3206 | if (copy_from_user(&kvm_userspace_mem, argp, | |
893bdbf1 | 3207 | sizeof(kvm_userspace_mem))) |
6fc138d2 IE |
3208 | goto out; |
3209 | ||
47ae31e2 | 3210 | r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem); |
6aa8b732 AK |
3211 | break; |
3212 | } | |
3213 | case KVM_GET_DIRTY_LOG: { | |
3214 | struct kvm_dirty_log log; | |
3215 | ||
3216 | r = -EFAULT; | |
893bdbf1 | 3217 | if (copy_from_user(&log, argp, sizeof(log))) |
6aa8b732 | 3218 | goto out; |
2c6f5df9 | 3219 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); |
6aa8b732 AK |
3220 | break; |
3221 | } | |
2a31b9db PB |
3222 | #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT |
3223 | case KVM_CLEAR_DIRTY_LOG: { | |
3224 | struct kvm_clear_dirty_log log; | |
3225 | ||
3226 | r = -EFAULT; | |
3227 | if (copy_from_user(&log, argp, sizeof(log))) | |
3228 | goto out; | |
3229 | r = kvm_vm_ioctl_clear_dirty_log(kvm, &log); | |
3230 | break; | |
3231 | } | |
3232 | #endif | |
4b4357e0 | 3233 | #ifdef CONFIG_KVM_MMIO |
5f94c174 LV |
3234 | case KVM_REGISTER_COALESCED_MMIO: { |
3235 | struct kvm_coalesced_mmio_zone zone; | |
f95ef0cd | 3236 | |
5f94c174 | 3237 | r = -EFAULT; |
893bdbf1 | 3238 | if (copy_from_user(&zone, argp, sizeof(zone))) |
5f94c174 | 3239 | goto out; |
5f94c174 | 3240 | r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone); |
5f94c174 LV |
3241 | break; |
3242 | } | |
3243 | case KVM_UNREGISTER_COALESCED_MMIO: { | |
3244 | struct kvm_coalesced_mmio_zone zone; | |
f95ef0cd | 3245 | |
5f94c174 | 3246 | r = -EFAULT; |
893bdbf1 | 3247 | if (copy_from_user(&zone, argp, sizeof(zone))) |
5f94c174 | 3248 | goto out; |
5f94c174 | 3249 | r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone); |
5f94c174 LV |
3250 | break; |
3251 | } | |
3252 | #endif | |
721eecbf GH |
3253 | case KVM_IRQFD: { |
3254 | struct kvm_irqfd data; | |
3255 | ||
3256 | r = -EFAULT; | |
893bdbf1 | 3257 | if (copy_from_user(&data, argp, sizeof(data))) |
721eecbf | 3258 | goto out; |
d4db2935 | 3259 | r = kvm_irqfd(kvm, &data); |
721eecbf GH |
3260 | break; |
3261 | } | |
d34e6b17 GH |
3262 | case KVM_IOEVENTFD: { |
3263 | struct kvm_ioeventfd data; | |
3264 | ||
3265 | r = -EFAULT; | |
893bdbf1 | 3266 | if (copy_from_user(&data, argp, sizeof(data))) |
d34e6b17 GH |
3267 | goto out; |
3268 | r = kvm_ioeventfd(kvm, &data); | |
3269 | break; | |
3270 | } | |
07975ad3 JK |
3271 | #ifdef CONFIG_HAVE_KVM_MSI |
3272 | case KVM_SIGNAL_MSI: { | |
3273 | struct kvm_msi msi; | |
3274 | ||
3275 | r = -EFAULT; | |
893bdbf1 | 3276 | if (copy_from_user(&msi, argp, sizeof(msi))) |
07975ad3 JK |
3277 | goto out; |
3278 | r = kvm_send_userspace_msi(kvm, &msi); | |
3279 | break; | |
3280 | } | |
23d43cf9 CD |
3281 | #endif |
3282 | #ifdef __KVM_HAVE_IRQ_LINE | |
3283 | case KVM_IRQ_LINE_STATUS: | |
3284 | case KVM_IRQ_LINE: { | |
3285 | struct kvm_irq_level irq_event; | |
3286 | ||
3287 | r = -EFAULT; | |
893bdbf1 | 3288 | if (copy_from_user(&irq_event, argp, sizeof(irq_event))) |
23d43cf9 CD |
3289 | goto out; |
3290 | ||
aa2fbe6d YZ |
3291 | r = kvm_vm_ioctl_irq_line(kvm, &irq_event, |
3292 | ioctl == KVM_IRQ_LINE_STATUS); | |
23d43cf9 CD |
3293 | if (r) |
3294 | goto out; | |
3295 | ||
3296 | r = -EFAULT; | |
3297 | if (ioctl == KVM_IRQ_LINE_STATUS) { | |
893bdbf1 | 3298 | if (copy_to_user(argp, &irq_event, sizeof(irq_event))) |
23d43cf9 CD |
3299 | goto out; |
3300 | } | |
3301 | ||
3302 | r = 0; | |
3303 | break; | |
3304 | } | |
73880c80 | 3305 | #endif |
aa8d5944 AG |
3306 | #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING |
3307 | case KVM_SET_GSI_ROUTING: { | |
3308 | struct kvm_irq_routing routing; | |
3309 | struct kvm_irq_routing __user *urouting; | |
f8c1b85b | 3310 | struct kvm_irq_routing_entry *entries = NULL; |
aa8d5944 AG |
3311 | |
3312 | r = -EFAULT; | |
3313 | if (copy_from_user(&routing, argp, sizeof(routing))) | |
3314 | goto out; | |
3315 | r = -EINVAL; | |
5c0aea0e DH |
3316 | if (!kvm_arch_can_set_irq_routing(kvm)) |
3317 | goto out; | |
caf1ff26 | 3318 | if (routing.nr > KVM_MAX_IRQ_ROUTES) |
aa8d5944 AG |
3319 | goto out; |
3320 | if (routing.flags) | |
3321 | goto out; | |
f8c1b85b PB |
3322 | if (routing.nr) { |
3323 | r = -ENOMEM; | |
42bc47b3 KC |
3324 | entries = vmalloc(array_size(sizeof(*entries), |
3325 | routing.nr)); | |
f8c1b85b PB |
3326 | if (!entries) |
3327 | goto out; | |
3328 | r = -EFAULT; | |
3329 | urouting = argp; | |
3330 | if (copy_from_user(entries, urouting->entries, | |
3331 | routing.nr * sizeof(*entries))) | |
3332 | goto out_free_irq_routing; | |
3333 | } | |
aa8d5944 AG |
3334 | r = kvm_set_irq_routing(kvm, entries, routing.nr, |
3335 | routing.flags); | |
a642a175 | 3336 | out_free_irq_routing: |
aa8d5944 AG |
3337 | vfree(entries); |
3338 | break; | |
3339 | } | |
3340 | #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ | |
852b6d57 SW |
3341 | case KVM_CREATE_DEVICE: { |
3342 | struct kvm_create_device cd; | |
3343 | ||
3344 | r = -EFAULT; | |
3345 | if (copy_from_user(&cd, argp, sizeof(cd))) | |
3346 | goto out; | |
3347 | ||
3348 | r = kvm_ioctl_create_device(kvm, &cd); | |
3349 | if (r) | |
3350 | goto out; | |
3351 | ||
3352 | r = -EFAULT; | |
3353 | if (copy_to_user(argp, &cd, sizeof(cd))) | |
3354 | goto out; | |
3355 | ||
3356 | r = 0; | |
3357 | break; | |
3358 | } | |
92b591a4 AG |
3359 | case KVM_CHECK_EXTENSION: |
3360 | r = kvm_vm_ioctl_check_extension_generic(kvm, arg); | |
3361 | break; | |
f17abe9a | 3362 | default: |
1fe779f8 | 3363 | r = kvm_arch_vm_ioctl(filp, ioctl, arg); |
f17abe9a AK |
3364 | } |
3365 | out: | |
3366 | return r; | |
3367 | } | |
3368 | ||
de8e5d74 | 3369 | #ifdef CONFIG_KVM_COMPAT |
6ff5894c AB |
3370 | struct compat_kvm_dirty_log { |
3371 | __u32 slot; | |
3372 | __u32 padding1; | |
3373 | union { | |
3374 | compat_uptr_t dirty_bitmap; /* one bit per page */ | |
3375 | __u64 padding2; | |
3376 | }; | |
3377 | }; | |
3378 | ||
3379 | static long kvm_vm_compat_ioctl(struct file *filp, | |
3380 | unsigned int ioctl, unsigned long arg) | |
3381 | { | |
3382 | struct kvm *kvm = filp->private_data; | |
3383 | int r; | |
3384 | ||
3385 | if (kvm->mm != current->mm) | |
3386 | return -EIO; | |
3387 | switch (ioctl) { | |
3388 | case KVM_GET_DIRTY_LOG: { | |
3389 | struct compat_kvm_dirty_log compat_log; | |
3390 | struct kvm_dirty_log log; | |
3391 | ||
6ff5894c AB |
3392 | if (copy_from_user(&compat_log, (void __user *)arg, |
3393 | sizeof(compat_log))) | |
f6a3b168 | 3394 | return -EFAULT; |
6ff5894c AB |
3395 | log.slot = compat_log.slot; |
3396 | log.padding1 = compat_log.padding1; | |
3397 | log.padding2 = compat_log.padding2; | |
3398 | log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap); | |
3399 | ||
3400 | r = kvm_vm_ioctl_get_dirty_log(kvm, &log); | |
6ff5894c AB |
3401 | break; |
3402 | } | |
3403 | default: | |
3404 | r = kvm_vm_ioctl(filp, ioctl, arg); | |
3405 | } | |
6ff5894c AB |
3406 | return r; |
3407 | } | |
3408 | #endif | |
3409 | ||
3d3aab1b | 3410 | static struct file_operations kvm_vm_fops = { |
f17abe9a AK |
3411 | .release = kvm_vm_release, |
3412 | .unlocked_ioctl = kvm_vm_ioctl, | |
6038f373 | 3413 | .llseek = noop_llseek, |
7ddfd3e0 | 3414 | KVM_COMPAT(kvm_vm_compat_ioctl), |
f17abe9a AK |
3415 | }; |
3416 | ||
e08b9637 | 3417 | static int kvm_dev_ioctl_create_vm(unsigned long type) |
f17abe9a | 3418 | { |
aac87636 | 3419 | int r; |
f17abe9a | 3420 | struct kvm *kvm; |
506cfba9 | 3421 | struct file *file; |
f17abe9a | 3422 | |
e08b9637 | 3423 | kvm = kvm_create_vm(type); |
d6d28168 AK |
3424 | if (IS_ERR(kvm)) |
3425 | return PTR_ERR(kvm); | |
4b4357e0 | 3426 | #ifdef CONFIG_KVM_MMIO |
6ce5a090 | 3427 | r = kvm_coalesced_mmio_init(kvm); |
78588335 ME |
3428 | if (r < 0) |
3429 | goto put_kvm; | |
6ce5a090 | 3430 | #endif |
506cfba9 | 3431 | r = get_unused_fd_flags(O_CLOEXEC); |
78588335 ME |
3432 | if (r < 0) |
3433 | goto put_kvm; | |
3434 | ||
506cfba9 AV |
3435 | file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR); |
3436 | if (IS_ERR(file)) { | |
3437 | put_unused_fd(r); | |
78588335 ME |
3438 | r = PTR_ERR(file); |
3439 | goto put_kvm; | |
506cfba9 | 3440 | } |
536a6f88 | 3441 | |
525df861 PB |
3442 | /* |
3443 | * Don't call kvm_put_kvm anymore at this point; file->f_op is | |
3444 | * already set, with ->release() being kvm_vm_release(). In error | |
3445 | * cases it will be called by the final fput(file) and will take | |
3446 | * care of doing kvm_put_kvm(kvm). | |
3447 | */ | |
536a6f88 | 3448 | if (kvm_create_vm_debugfs(kvm, r) < 0) { |
506cfba9 AV |
3449 | put_unused_fd(r); |
3450 | fput(file); | |
536a6f88 JF |
3451 | return -ENOMEM; |
3452 | } | |
286de8f6 | 3453 | kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm); |
f17abe9a | 3454 | |
506cfba9 | 3455 | fd_install(r, file); |
aac87636 | 3456 | return r; |
78588335 ME |
3457 | |
3458 | put_kvm: | |
3459 | kvm_put_kvm(kvm); | |
3460 | return r; | |
f17abe9a AK |
3461 | } |
3462 | ||
3463 | static long kvm_dev_ioctl(struct file *filp, | |
3464 | unsigned int ioctl, unsigned long arg) | |
3465 | { | |
07c45a36 | 3466 | long r = -EINVAL; |
f17abe9a AK |
3467 | |
3468 | switch (ioctl) { | |
3469 | case KVM_GET_API_VERSION: | |
f0fe5108 AK |
3470 | if (arg) |
3471 | goto out; | |
f17abe9a AK |
3472 | r = KVM_API_VERSION; |
3473 | break; | |
3474 | case KVM_CREATE_VM: | |
e08b9637 | 3475 | r = kvm_dev_ioctl_create_vm(arg); |
f17abe9a | 3476 | break; |
018d00d2 | 3477 | case KVM_CHECK_EXTENSION: |
784aa3d7 | 3478 | r = kvm_vm_ioctl_check_extension_generic(NULL, arg); |
5d308f45 | 3479 | break; |
07c45a36 | 3480 | case KVM_GET_VCPU_MMAP_SIZE: |
07c45a36 AK |
3481 | if (arg) |
3482 | goto out; | |
adb1ff46 AK |
3483 | r = PAGE_SIZE; /* struct kvm_run */ |
3484 | #ifdef CONFIG_X86 | |
3485 | r += PAGE_SIZE; /* pio data page */ | |
5f94c174 | 3486 | #endif |
4b4357e0 | 3487 | #ifdef CONFIG_KVM_MMIO |
5f94c174 | 3488 | r += PAGE_SIZE; /* coalesced mmio ring page */ |
adb1ff46 | 3489 | #endif |
07c45a36 | 3490 | break; |
d4c9ff2d FEL |
3491 | case KVM_TRACE_ENABLE: |
3492 | case KVM_TRACE_PAUSE: | |
3493 | case KVM_TRACE_DISABLE: | |
2023a29c | 3494 | r = -EOPNOTSUPP; |
d4c9ff2d | 3495 | break; |
6aa8b732 | 3496 | default: |
043405e1 | 3497 | return kvm_arch_dev_ioctl(filp, ioctl, arg); |
6aa8b732 AK |
3498 | } |
3499 | out: | |
3500 | return r; | |
3501 | } | |
3502 | ||
6aa8b732 | 3503 | static struct file_operations kvm_chardev_ops = { |
6aa8b732 | 3504 | .unlocked_ioctl = kvm_dev_ioctl, |
6038f373 | 3505 | .llseek = noop_llseek, |
7ddfd3e0 | 3506 | KVM_COMPAT(kvm_dev_ioctl), |
6aa8b732 AK |
3507 | }; |
3508 | ||
3509 | static struct miscdevice kvm_dev = { | |
bbe4432e | 3510 | KVM_MINOR, |
6aa8b732 AK |
3511 | "kvm", |
3512 | &kvm_chardev_ops, | |
3513 | }; | |
3514 | ||
75b7127c | 3515 | static void hardware_enable_nolock(void *junk) |
1b6c0168 AK |
3516 | { |
3517 | int cpu = raw_smp_processor_id(); | |
10474ae8 | 3518 | int r; |
1b6c0168 | 3519 | |
7f59f492 | 3520 | if (cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
1b6c0168 | 3521 | return; |
10474ae8 | 3522 | |
7f59f492 | 3523 | cpumask_set_cpu(cpu, cpus_hardware_enabled); |
10474ae8 | 3524 | |
13a34e06 | 3525 | r = kvm_arch_hardware_enable(); |
10474ae8 AG |
3526 | |
3527 | if (r) { | |
3528 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); | |
3529 | atomic_inc(&hardware_enable_failed); | |
1170adc6 | 3530 | pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu); |
10474ae8 | 3531 | } |
1b6c0168 AK |
3532 | } |
3533 | ||
8c18b2d2 | 3534 | static int kvm_starting_cpu(unsigned int cpu) |
75b7127c | 3535 | { |
4a937f96 | 3536 | raw_spin_lock(&kvm_count_lock); |
4fa92fb2 PB |
3537 | if (kvm_usage_count) |
3538 | hardware_enable_nolock(NULL); | |
4a937f96 | 3539 | raw_spin_unlock(&kvm_count_lock); |
8c18b2d2 | 3540 | return 0; |
75b7127c TY |
3541 | } |
3542 | ||
3543 | static void hardware_disable_nolock(void *junk) | |
1b6c0168 AK |
3544 | { |
3545 | int cpu = raw_smp_processor_id(); | |
3546 | ||
7f59f492 | 3547 | if (!cpumask_test_cpu(cpu, cpus_hardware_enabled)) |
1b6c0168 | 3548 | return; |
7f59f492 | 3549 | cpumask_clear_cpu(cpu, cpus_hardware_enabled); |
13a34e06 | 3550 | kvm_arch_hardware_disable(); |
1b6c0168 AK |
3551 | } |
3552 | ||
8c18b2d2 | 3553 | static int kvm_dying_cpu(unsigned int cpu) |
75b7127c | 3554 | { |
4a937f96 | 3555 | raw_spin_lock(&kvm_count_lock); |
4fa92fb2 PB |
3556 | if (kvm_usage_count) |
3557 | hardware_disable_nolock(NULL); | |
4a937f96 | 3558 | raw_spin_unlock(&kvm_count_lock); |
8c18b2d2 | 3559 | return 0; |
75b7127c TY |
3560 | } |
3561 | ||
10474ae8 AG |
3562 | static void hardware_disable_all_nolock(void) |
3563 | { | |
3564 | BUG_ON(!kvm_usage_count); | |
3565 | ||
3566 | kvm_usage_count--; | |
3567 | if (!kvm_usage_count) | |
75b7127c | 3568 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
10474ae8 AG |
3569 | } |
3570 | ||
3571 | static void hardware_disable_all(void) | |
3572 | { | |
4a937f96 | 3573 | raw_spin_lock(&kvm_count_lock); |
10474ae8 | 3574 | hardware_disable_all_nolock(); |
4a937f96 | 3575 | raw_spin_unlock(&kvm_count_lock); |
10474ae8 AG |
3576 | } |
3577 | ||
3578 | static int hardware_enable_all(void) | |
3579 | { | |
3580 | int r = 0; | |
3581 | ||
4a937f96 | 3582 | raw_spin_lock(&kvm_count_lock); |
10474ae8 AG |
3583 | |
3584 | kvm_usage_count++; | |
3585 | if (kvm_usage_count == 1) { | |
3586 | atomic_set(&hardware_enable_failed, 0); | |
75b7127c | 3587 | on_each_cpu(hardware_enable_nolock, NULL, 1); |
10474ae8 AG |
3588 | |
3589 | if (atomic_read(&hardware_enable_failed)) { | |
3590 | hardware_disable_all_nolock(); | |
3591 | r = -EBUSY; | |
3592 | } | |
3593 | } | |
3594 | ||
4a937f96 | 3595 | raw_spin_unlock(&kvm_count_lock); |
10474ae8 AG |
3596 | |
3597 | return r; | |
3598 | } | |
3599 | ||
9a2b85c6 | 3600 | static int kvm_reboot(struct notifier_block *notifier, unsigned long val, |
d77c26fc | 3601 | void *v) |
9a2b85c6 | 3602 | { |
8e1c1815 SY |
3603 | /* |
3604 | * Some (well, at least mine) BIOSes hang on reboot if | |
3605 | * in vmx root mode. | |
3606 | * | |
3607 | * And Intel TXT required VMX off for all cpu when system shutdown. | |
3608 | */ | |
1170adc6 | 3609 | pr_info("kvm: exiting hardware virtualization\n"); |
8e1c1815 | 3610 | kvm_rebooting = true; |
75b7127c | 3611 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
9a2b85c6 RR |
3612 | return NOTIFY_OK; |
3613 | } | |
3614 | ||
3615 | static struct notifier_block kvm_reboot_notifier = { | |
3616 | .notifier_call = kvm_reboot, | |
3617 | .priority = 0, | |
3618 | }; | |
3619 | ||
e93f8a0f | 3620 | static void kvm_io_bus_destroy(struct kvm_io_bus *bus) |
2eeb2e94 GH |
3621 | { |
3622 | int i; | |
3623 | ||
3624 | for (i = 0; i < bus->dev_count; i++) { | |
743eeb0b | 3625 | struct kvm_io_device *pos = bus->range[i].dev; |
2eeb2e94 GH |
3626 | |
3627 | kvm_iodevice_destructor(pos); | |
3628 | } | |
e93f8a0f | 3629 | kfree(bus); |
2eeb2e94 GH |
3630 | } |
3631 | ||
c21fbff1 | 3632 | static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1, |
20e87b72 | 3633 | const struct kvm_io_range *r2) |
743eeb0b | 3634 | { |
8f4216c7 JW |
3635 | gpa_t addr1 = r1->addr; |
3636 | gpa_t addr2 = r2->addr; | |
3637 | ||
3638 | if (addr1 < addr2) | |
743eeb0b | 3639 | return -1; |
8f4216c7 JW |
3640 | |
3641 | /* If r2->len == 0, match the exact address. If r2->len != 0, | |
3642 | * accept any overlapping write. Any order is acceptable for | |
3643 | * overlapping ranges, because kvm_io_bus_get_first_dev ensures | |
3644 | * we process all of them. | |
3645 | */ | |
3646 | if (r2->len) { | |
3647 | addr1 += r1->len; | |
3648 | addr2 += r2->len; | |
3649 | } | |
3650 | ||
3651 | if (addr1 > addr2) | |
743eeb0b | 3652 | return 1; |
8f4216c7 | 3653 | |
743eeb0b SL |
3654 | return 0; |
3655 | } | |
3656 | ||
a343c9b7 PB |
3657 | static int kvm_io_bus_sort_cmp(const void *p1, const void *p2) |
3658 | { | |
c21fbff1 | 3659 | return kvm_io_bus_cmp(p1, p2); |
a343c9b7 PB |
3660 | } |
3661 | ||
39369f7a | 3662 | static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus, |
743eeb0b SL |
3663 | gpa_t addr, int len) |
3664 | { | |
3665 | struct kvm_io_range *range, key; | |
3666 | int off; | |
3667 | ||
3668 | key = (struct kvm_io_range) { | |
3669 | .addr = addr, | |
3670 | .len = len, | |
3671 | }; | |
3672 | ||
3673 | range = bsearch(&key, bus->range, bus->dev_count, | |
3674 | sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp); | |
3675 | if (range == NULL) | |
3676 | return -ENOENT; | |
3677 | ||
3678 | off = range - bus->range; | |
3679 | ||
c21fbff1 | 3680 | while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0) |
743eeb0b SL |
3681 | off--; |
3682 | ||
3683 | return off; | |
3684 | } | |
3685 | ||
e32edf4f | 3686 | static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
126a5af5 CH |
3687 | struct kvm_io_range *range, const void *val) |
3688 | { | |
3689 | int idx; | |
3690 | ||
3691 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); | |
3692 | if (idx < 0) | |
3693 | return -EOPNOTSUPP; | |
3694 | ||
3695 | while (idx < bus->dev_count && | |
c21fbff1 | 3696 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
e32edf4f | 3697 | if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr, |
126a5af5 CH |
3698 | range->len, val)) |
3699 | return idx; | |
3700 | idx++; | |
3701 | } | |
3702 | ||
3703 | return -EOPNOTSUPP; | |
3704 | } | |
3705 | ||
bda9020e | 3706 | /* kvm_io_bus_write - called under kvm->slots_lock */ |
e32edf4f | 3707 | int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
bda9020e | 3708 | int len, const void *val) |
2eeb2e94 | 3709 | { |
90d83dc3 | 3710 | struct kvm_io_bus *bus; |
743eeb0b | 3711 | struct kvm_io_range range; |
126a5af5 | 3712 | int r; |
743eeb0b SL |
3713 | |
3714 | range = (struct kvm_io_range) { | |
3715 | .addr = addr, | |
3716 | .len = len, | |
3717 | }; | |
90d83dc3 | 3718 | |
e32edf4f | 3719 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
90db1043 DH |
3720 | if (!bus) |
3721 | return -ENOMEM; | |
e32edf4f | 3722 | r = __kvm_io_bus_write(vcpu, bus, &range, val); |
126a5af5 CH |
3723 | return r < 0 ? r : 0; |
3724 | } | |
a2420107 | 3725 | EXPORT_SYMBOL_GPL(kvm_io_bus_write); |
126a5af5 CH |
3726 | |
3727 | /* kvm_io_bus_write_cookie - called under kvm->slots_lock */ | |
e32edf4f NN |
3728 | int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, |
3729 | gpa_t addr, int len, const void *val, long cookie) | |
126a5af5 CH |
3730 | { |
3731 | struct kvm_io_bus *bus; | |
3732 | struct kvm_io_range range; | |
3733 | ||
3734 | range = (struct kvm_io_range) { | |
3735 | .addr = addr, | |
3736 | .len = len, | |
3737 | }; | |
3738 | ||
e32edf4f | 3739 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
90db1043 DH |
3740 | if (!bus) |
3741 | return -ENOMEM; | |
126a5af5 CH |
3742 | |
3743 | /* First try the device referenced by cookie. */ | |
3744 | if ((cookie >= 0) && (cookie < bus->dev_count) && | |
c21fbff1 | 3745 | (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0)) |
e32edf4f | 3746 | if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len, |
126a5af5 CH |
3747 | val)) |
3748 | return cookie; | |
3749 | ||
3750 | /* | |
3751 | * cookie contained garbage; fall back to search and return the | |
3752 | * correct cookie value. | |
3753 | */ | |
e32edf4f | 3754 | return __kvm_io_bus_write(vcpu, bus, &range, val); |
126a5af5 CH |
3755 | } |
3756 | ||
e32edf4f NN |
3757 | static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus, |
3758 | struct kvm_io_range *range, void *val) | |
126a5af5 CH |
3759 | { |
3760 | int idx; | |
3761 | ||
3762 | idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len); | |
743eeb0b SL |
3763 | if (idx < 0) |
3764 | return -EOPNOTSUPP; | |
3765 | ||
3766 | while (idx < bus->dev_count && | |
c21fbff1 | 3767 | kvm_io_bus_cmp(range, &bus->range[idx]) == 0) { |
e32edf4f | 3768 | if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr, |
126a5af5 CH |
3769 | range->len, val)) |
3770 | return idx; | |
743eeb0b SL |
3771 | idx++; |
3772 | } | |
3773 | ||
bda9020e MT |
3774 | return -EOPNOTSUPP; |
3775 | } | |
2eeb2e94 | 3776 | |
bda9020e | 3777 | /* kvm_io_bus_read - called under kvm->slots_lock */ |
e32edf4f | 3778 | int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr, |
e93f8a0f | 3779 | int len, void *val) |
bda9020e | 3780 | { |
90d83dc3 | 3781 | struct kvm_io_bus *bus; |
743eeb0b | 3782 | struct kvm_io_range range; |
126a5af5 | 3783 | int r; |
743eeb0b SL |
3784 | |
3785 | range = (struct kvm_io_range) { | |
3786 | .addr = addr, | |
3787 | .len = len, | |
3788 | }; | |
e93f8a0f | 3789 | |
e32edf4f | 3790 | bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu); |
90db1043 DH |
3791 | if (!bus) |
3792 | return -ENOMEM; | |
e32edf4f | 3793 | r = __kvm_io_bus_read(vcpu, bus, &range, val); |
126a5af5 CH |
3794 | return r < 0 ? r : 0; |
3795 | } | |
743eeb0b | 3796 | |
79fac95e | 3797 | /* Caller must hold slots_lock. */ |
743eeb0b SL |
3798 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
3799 | int len, struct kvm_io_device *dev) | |
6c474694 | 3800 | { |
d4c67a7a | 3801 | int i; |
e93f8a0f | 3802 | struct kvm_io_bus *new_bus, *bus; |
d4c67a7a | 3803 | struct kvm_io_range range; |
090b7aff | 3804 | |
4a12f951 | 3805 | bus = kvm_get_bus(kvm, bus_idx); |
90db1043 DH |
3806 | if (!bus) |
3807 | return -ENOMEM; | |
3808 | ||
6ea34c9b AK |
3809 | /* exclude ioeventfd which is limited by maximum fd */ |
3810 | if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) | |
090b7aff | 3811 | return -ENOSPC; |
2eeb2e94 | 3812 | |
90952cd3 | 3813 | new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1), |
b12ce36a | 3814 | GFP_KERNEL_ACCOUNT); |
e93f8a0f MT |
3815 | if (!new_bus) |
3816 | return -ENOMEM; | |
d4c67a7a GH |
3817 | |
3818 | range = (struct kvm_io_range) { | |
3819 | .addr = addr, | |
3820 | .len = len, | |
3821 | .dev = dev, | |
3822 | }; | |
3823 | ||
3824 | for (i = 0; i < bus->dev_count; i++) | |
3825 | if (kvm_io_bus_cmp(&bus->range[i], &range) > 0) | |
3826 | break; | |
3827 | ||
3828 | memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); | |
3829 | new_bus->dev_count++; | |
3830 | new_bus->range[i] = range; | |
3831 | memcpy(new_bus->range + i + 1, bus->range + i, | |
3832 | (bus->dev_count - i) * sizeof(struct kvm_io_range)); | |
e93f8a0f MT |
3833 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
3834 | synchronize_srcu_expedited(&kvm->srcu); | |
3835 | kfree(bus); | |
090b7aff GH |
3836 | |
3837 | return 0; | |
3838 | } | |
3839 | ||
79fac95e | 3840 | /* Caller must hold slots_lock. */ |
90db1043 DH |
3841 | void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
3842 | struct kvm_io_device *dev) | |
090b7aff | 3843 | { |
90db1043 | 3844 | int i; |
e93f8a0f | 3845 | struct kvm_io_bus *new_bus, *bus; |
090b7aff | 3846 | |
4a12f951 | 3847 | bus = kvm_get_bus(kvm, bus_idx); |
df630b8c | 3848 | if (!bus) |
90db1043 | 3849 | return; |
df630b8c | 3850 | |
a1300716 AK |
3851 | for (i = 0; i < bus->dev_count; i++) |
3852 | if (bus->range[i].dev == dev) { | |
090b7aff GH |
3853 | break; |
3854 | } | |
e93f8a0f | 3855 | |
90db1043 DH |
3856 | if (i == bus->dev_count) |
3857 | return; | |
a1300716 | 3858 | |
90952cd3 | 3859 | new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1), |
b12ce36a | 3860 | GFP_KERNEL_ACCOUNT); |
90db1043 DH |
3861 | if (!new_bus) { |
3862 | pr_err("kvm: failed to shrink bus, removing it completely\n"); | |
3863 | goto broken; | |
3864 | } | |
a1300716 AK |
3865 | |
3866 | memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); | |
3867 | new_bus->dev_count--; | |
3868 | memcpy(new_bus->range + i, bus->range + i + 1, | |
3869 | (new_bus->dev_count - i) * sizeof(struct kvm_io_range)); | |
e93f8a0f | 3870 | |
90db1043 | 3871 | broken: |
e93f8a0f MT |
3872 | rcu_assign_pointer(kvm->buses[bus_idx], new_bus); |
3873 | synchronize_srcu_expedited(&kvm->srcu); | |
3874 | kfree(bus); | |
90db1043 | 3875 | return; |
2eeb2e94 GH |
3876 | } |
3877 | ||
8a39d006 AP |
3878 | struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
3879 | gpa_t addr) | |
3880 | { | |
3881 | struct kvm_io_bus *bus; | |
3882 | int dev_idx, srcu_idx; | |
3883 | struct kvm_io_device *iodev = NULL; | |
3884 | ||
3885 | srcu_idx = srcu_read_lock(&kvm->srcu); | |
3886 | ||
3887 | bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu); | |
90db1043 DH |
3888 | if (!bus) |
3889 | goto out_unlock; | |
8a39d006 AP |
3890 | |
3891 | dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1); | |
3892 | if (dev_idx < 0) | |
3893 | goto out_unlock; | |
3894 | ||
3895 | iodev = bus->range[dev_idx].dev; | |
3896 | ||
3897 | out_unlock: | |
3898 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
3899 | ||
3900 | return iodev; | |
3901 | } | |
3902 | EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev); | |
3903 | ||
536a6f88 JF |
3904 | static int kvm_debugfs_open(struct inode *inode, struct file *file, |
3905 | int (*get)(void *, u64 *), int (*set)(void *, u64), | |
3906 | const char *fmt) | |
3907 | { | |
3908 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) | |
3909 | inode->i_private; | |
3910 | ||
3911 | /* The debugfs files are a reference to the kvm struct which | |
3912 | * is still valid when kvm_destroy_vm is called. | |
3913 | * To avoid the race between open and the removal of the debugfs | |
3914 | * directory we test against the users count. | |
3915 | */ | |
e3736c3e | 3916 | if (!refcount_inc_not_zero(&stat_data->kvm->users_count)) |
536a6f88 JF |
3917 | return -ENOENT; |
3918 | ||
3919 | if (simple_attr_open(inode, file, get, set, fmt)) { | |
3920 | kvm_put_kvm(stat_data->kvm); | |
3921 | return -ENOMEM; | |
3922 | } | |
3923 | ||
3924 | return 0; | |
3925 | } | |
3926 | ||
3927 | static int kvm_debugfs_release(struct inode *inode, struct file *file) | |
3928 | { | |
3929 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *) | |
3930 | inode->i_private; | |
3931 | ||
3932 | simple_attr_release(inode, file); | |
3933 | kvm_put_kvm(stat_data->kvm); | |
3934 | ||
3935 | return 0; | |
3936 | } | |
3937 | ||
3938 | static int vm_stat_get_per_vm(void *data, u64 *val) | |
3939 | { | |
3940 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3941 | ||
8a7e75d4 | 3942 | *val = *(ulong *)((void *)stat_data->kvm + stat_data->offset); |
536a6f88 JF |
3943 | |
3944 | return 0; | |
3945 | } | |
3946 | ||
ce35ef27 SJS |
3947 | static int vm_stat_clear_per_vm(void *data, u64 val) |
3948 | { | |
3949 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3950 | ||
3951 | if (val) | |
3952 | return -EINVAL; | |
3953 | ||
3954 | *(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0; | |
3955 | ||
3956 | return 0; | |
3957 | } | |
3958 | ||
536a6f88 JF |
3959 | static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file) |
3960 | { | |
3961 | __simple_attr_check_format("%llu\n", 0ull); | |
3962 | return kvm_debugfs_open(inode, file, vm_stat_get_per_vm, | |
ce35ef27 | 3963 | vm_stat_clear_per_vm, "%llu\n"); |
536a6f88 JF |
3964 | } |
3965 | ||
3966 | static const struct file_operations vm_stat_get_per_vm_fops = { | |
3967 | .owner = THIS_MODULE, | |
3968 | .open = vm_stat_get_per_vm_open, | |
3969 | .release = kvm_debugfs_release, | |
3970 | .read = simple_attr_read, | |
3971 | .write = simple_attr_write, | |
3bed8888 | 3972 | .llseek = no_llseek, |
536a6f88 JF |
3973 | }; |
3974 | ||
3975 | static int vcpu_stat_get_per_vm(void *data, u64 *val) | |
3976 | { | |
3977 | int i; | |
3978 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3979 | struct kvm_vcpu *vcpu; | |
3980 | ||
3981 | *val = 0; | |
3982 | ||
3983 | kvm_for_each_vcpu(i, vcpu, stat_data->kvm) | |
8a7e75d4 | 3984 | *val += *(u64 *)((void *)vcpu + stat_data->offset); |
536a6f88 JF |
3985 | |
3986 | return 0; | |
3987 | } | |
3988 | ||
ce35ef27 SJS |
3989 | static int vcpu_stat_clear_per_vm(void *data, u64 val) |
3990 | { | |
3991 | int i; | |
3992 | struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data; | |
3993 | struct kvm_vcpu *vcpu; | |
3994 | ||
3995 | if (val) | |
3996 | return -EINVAL; | |
3997 | ||
3998 | kvm_for_each_vcpu(i, vcpu, stat_data->kvm) | |
3999 | *(u64 *)((void *)vcpu + stat_data->offset) = 0; | |
4000 | ||
4001 | return 0; | |
4002 | } | |
4003 | ||
536a6f88 JF |
4004 | static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file) |
4005 | { | |
4006 | __simple_attr_check_format("%llu\n", 0ull); | |
4007 | return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm, | |
ce35ef27 | 4008 | vcpu_stat_clear_per_vm, "%llu\n"); |
536a6f88 JF |
4009 | } |
4010 | ||
4011 | static const struct file_operations vcpu_stat_get_per_vm_fops = { | |
4012 | .owner = THIS_MODULE, | |
4013 | .open = vcpu_stat_get_per_vm_open, | |
4014 | .release = kvm_debugfs_release, | |
4015 | .read = simple_attr_read, | |
4016 | .write = simple_attr_write, | |
3bed8888 | 4017 | .llseek = no_llseek, |
536a6f88 JF |
4018 | }; |
4019 | ||
4020 | static const struct file_operations *stat_fops_per_vm[] = { | |
4021 | [KVM_STAT_VCPU] = &vcpu_stat_get_per_vm_fops, | |
4022 | [KVM_STAT_VM] = &vm_stat_get_per_vm_fops, | |
4023 | }; | |
4024 | ||
8b88b099 | 4025 | static int vm_stat_get(void *_offset, u64 *val) |
ba1389b7 AK |
4026 | { |
4027 | unsigned offset = (long)_offset; | |
ba1389b7 | 4028 | struct kvm *kvm; |
536a6f88 JF |
4029 | struct kvm_stat_data stat_tmp = {.offset = offset}; |
4030 | u64 tmp_val; | |
ba1389b7 | 4031 | |
8b88b099 | 4032 | *val = 0; |
2f303b74 | 4033 | spin_lock(&kvm_lock); |
536a6f88 JF |
4034 | list_for_each_entry(kvm, &vm_list, vm_list) { |
4035 | stat_tmp.kvm = kvm; | |
4036 | vm_stat_get_per_vm((void *)&stat_tmp, &tmp_val); | |
4037 | *val += tmp_val; | |
4038 | } | |
2f303b74 | 4039 | spin_unlock(&kvm_lock); |
8b88b099 | 4040 | return 0; |
ba1389b7 AK |
4041 | } |
4042 | ||
ce35ef27 SJS |
4043 | static int vm_stat_clear(void *_offset, u64 val) |
4044 | { | |
4045 | unsigned offset = (long)_offset; | |
4046 | struct kvm *kvm; | |
4047 | struct kvm_stat_data stat_tmp = {.offset = offset}; | |
4048 | ||
4049 | if (val) | |
4050 | return -EINVAL; | |
4051 | ||
4052 | spin_lock(&kvm_lock); | |
4053 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
4054 | stat_tmp.kvm = kvm; | |
4055 | vm_stat_clear_per_vm((void *)&stat_tmp, 0); | |
4056 | } | |
4057 | spin_unlock(&kvm_lock); | |
4058 | ||
4059 | return 0; | |
4060 | } | |
4061 | ||
4062 | DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n"); | |
ba1389b7 | 4063 | |
8b88b099 | 4064 | static int vcpu_stat_get(void *_offset, u64 *val) |
1165f5fe AK |
4065 | { |
4066 | unsigned offset = (long)_offset; | |
1165f5fe | 4067 | struct kvm *kvm; |
536a6f88 JF |
4068 | struct kvm_stat_data stat_tmp = {.offset = offset}; |
4069 | u64 tmp_val; | |
1165f5fe | 4070 | |
8b88b099 | 4071 | *val = 0; |
2f303b74 | 4072 | spin_lock(&kvm_lock); |
536a6f88 JF |
4073 | list_for_each_entry(kvm, &vm_list, vm_list) { |
4074 | stat_tmp.kvm = kvm; | |
4075 | vcpu_stat_get_per_vm((void *)&stat_tmp, &tmp_val); | |
4076 | *val += tmp_val; | |
4077 | } | |
2f303b74 | 4078 | spin_unlock(&kvm_lock); |
8b88b099 | 4079 | return 0; |
1165f5fe AK |
4080 | } |
4081 | ||
ce35ef27 SJS |
4082 | static int vcpu_stat_clear(void *_offset, u64 val) |
4083 | { | |
4084 | unsigned offset = (long)_offset; | |
4085 | struct kvm *kvm; | |
4086 | struct kvm_stat_data stat_tmp = {.offset = offset}; | |
4087 | ||
4088 | if (val) | |
4089 | return -EINVAL; | |
4090 | ||
4091 | spin_lock(&kvm_lock); | |
4092 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
4093 | stat_tmp.kvm = kvm; | |
4094 | vcpu_stat_clear_per_vm((void *)&stat_tmp, 0); | |
4095 | } | |
4096 | spin_unlock(&kvm_lock); | |
4097 | ||
4098 | return 0; | |
4099 | } | |
4100 | ||
4101 | DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear, | |
4102 | "%llu\n"); | |
ba1389b7 | 4103 | |
828c0950 | 4104 | static const struct file_operations *stat_fops[] = { |
ba1389b7 AK |
4105 | [KVM_STAT_VCPU] = &vcpu_stat_fops, |
4106 | [KVM_STAT_VM] = &vm_stat_fops, | |
4107 | }; | |
1165f5fe | 4108 | |
286de8f6 CI |
4109 | static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm) |
4110 | { | |
4111 | struct kobj_uevent_env *env; | |
286de8f6 CI |
4112 | unsigned long long created, active; |
4113 | ||
4114 | if (!kvm_dev.this_device || !kvm) | |
4115 | return; | |
4116 | ||
4117 | spin_lock(&kvm_lock); | |
4118 | if (type == KVM_EVENT_CREATE_VM) { | |
4119 | kvm_createvm_count++; | |
4120 | kvm_active_vms++; | |
4121 | } else if (type == KVM_EVENT_DESTROY_VM) { | |
4122 | kvm_active_vms--; | |
4123 | } | |
4124 | created = kvm_createvm_count; | |
4125 | active = kvm_active_vms; | |
4126 | spin_unlock(&kvm_lock); | |
4127 | ||
b12ce36a | 4128 | env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT); |
286de8f6 CI |
4129 | if (!env) |
4130 | return; | |
4131 | ||
4132 | add_uevent_var(env, "CREATED=%llu", created); | |
4133 | add_uevent_var(env, "COUNT=%llu", active); | |
4134 | ||
fdeaf7e3 | 4135 | if (type == KVM_EVENT_CREATE_VM) { |
286de8f6 | 4136 | add_uevent_var(env, "EVENT=create"); |
fdeaf7e3 CI |
4137 | kvm->userspace_pid = task_pid_nr(current); |
4138 | } else if (type == KVM_EVENT_DESTROY_VM) { | |
286de8f6 | 4139 | add_uevent_var(env, "EVENT=destroy"); |
fdeaf7e3 CI |
4140 | } |
4141 | add_uevent_var(env, "PID=%d", kvm->userspace_pid); | |
286de8f6 | 4142 | |
8ed0579c | 4143 | if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) { |
b12ce36a | 4144 | char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT); |
fdeaf7e3 CI |
4145 | |
4146 | if (p) { | |
4147 | tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX); | |
4148 | if (!IS_ERR(tmp)) | |
4149 | add_uevent_var(env, "STATS_PATH=%s", tmp); | |
4150 | kfree(p); | |
286de8f6 CI |
4151 | } |
4152 | } | |
4153 | /* no need for checks, since we are adding at most only 5 keys */ | |
4154 | env->envp[env->envp_idx++] = NULL; | |
4155 | kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp); | |
4156 | kfree(env); | |
286de8f6 CI |
4157 | } |
4158 | ||
929f45e3 | 4159 | static void kvm_init_debug(void) |
6aa8b732 AK |
4160 | { |
4161 | struct kvm_stats_debugfs_item *p; | |
4162 | ||
76f7c879 | 4163 | kvm_debugfs_dir = debugfs_create_dir("kvm", NULL); |
4f69b680 | 4164 | |
536a6f88 JF |
4165 | kvm_debugfs_num_entries = 0; |
4166 | for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) { | |
929f45e3 GKH |
4167 | debugfs_create_file(p->name, 0644, kvm_debugfs_dir, |
4168 | (void *)(long)p->offset, | |
4169 | stat_fops[p->kind]); | |
4f69b680 | 4170 | } |
6aa8b732 AK |
4171 | } |
4172 | ||
fb3600cc | 4173 | static int kvm_suspend(void) |
59ae6c6b | 4174 | { |
10474ae8 | 4175 | if (kvm_usage_count) |
75b7127c | 4176 | hardware_disable_nolock(NULL); |
59ae6c6b AK |
4177 | return 0; |
4178 | } | |
4179 | ||
fb3600cc | 4180 | static void kvm_resume(void) |
59ae6c6b | 4181 | { |
ca84d1a2 | 4182 | if (kvm_usage_count) { |
6706dae9 | 4183 | lockdep_assert_held(&kvm_count_lock); |
75b7127c | 4184 | hardware_enable_nolock(NULL); |
ca84d1a2 | 4185 | } |
59ae6c6b AK |
4186 | } |
4187 | ||
fb3600cc | 4188 | static struct syscore_ops kvm_syscore_ops = { |
59ae6c6b AK |
4189 | .suspend = kvm_suspend, |
4190 | .resume = kvm_resume, | |
4191 | }; | |
4192 | ||
15ad7146 AK |
4193 | static inline |
4194 | struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn) | |
4195 | { | |
4196 | return container_of(pn, struct kvm_vcpu, preempt_notifier); | |
4197 | } | |
4198 | ||
4199 | static void kvm_sched_in(struct preempt_notifier *pn, int cpu) | |
4200 | { | |
4201 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
f95ef0cd | 4202 | |
3a08a8f9 R |
4203 | if (vcpu->preempted) |
4204 | vcpu->preempted = false; | |
15ad7146 | 4205 | |
e790d9ef RK |
4206 | kvm_arch_sched_in(vcpu, cpu); |
4207 | ||
e9b11c17 | 4208 | kvm_arch_vcpu_load(vcpu, cpu); |
15ad7146 AK |
4209 | } |
4210 | ||
4211 | static void kvm_sched_out(struct preempt_notifier *pn, | |
4212 | struct task_struct *next) | |
4213 | { | |
4214 | struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); | |
4215 | ||
3a08a8f9 R |
4216 | if (current->state == TASK_RUNNING) |
4217 | vcpu->preempted = true; | |
e9b11c17 | 4218 | kvm_arch_vcpu_put(vcpu); |
15ad7146 AK |
4219 | } |
4220 | ||
0ee75bea | 4221 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
c16f862d | 4222 | struct module *module) |
6aa8b732 AK |
4223 | { |
4224 | int r; | |
002c7f7c | 4225 | int cpu; |
6aa8b732 | 4226 | |
f8c16bba ZX |
4227 | r = kvm_arch_init(opaque); |
4228 | if (r) | |
d2308784 | 4229 | goto out_fail; |
cb498ea2 | 4230 | |
7dac16c3 AH |
4231 | /* |
4232 | * kvm_arch_init makes sure there's at most one caller | |
4233 | * for architectures that support multiple implementations, | |
4234 | * like intel and amd on x86. | |
36343f6e PB |
4235 | * kvm_arch_init must be called before kvm_irqfd_init to avoid creating |
4236 | * conflicts in case kvm is already setup for another implementation. | |
7dac16c3 | 4237 | */ |
36343f6e PB |
4238 | r = kvm_irqfd_init(); |
4239 | if (r) | |
4240 | goto out_irqfd; | |
7dac16c3 | 4241 | |
8437a617 | 4242 | if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) { |
7f59f492 RR |
4243 | r = -ENOMEM; |
4244 | goto out_free_0; | |
4245 | } | |
4246 | ||
e9b11c17 | 4247 | r = kvm_arch_hardware_setup(); |
6aa8b732 | 4248 | if (r < 0) |
7f59f492 | 4249 | goto out_free_0a; |
6aa8b732 | 4250 | |
002c7f7c YS |
4251 | for_each_online_cpu(cpu) { |
4252 | smp_call_function_single(cpu, | |
e9b11c17 | 4253 | kvm_arch_check_processor_compat, |
8691e5a8 | 4254 | &r, 1); |
002c7f7c | 4255 | if (r < 0) |
d2308784 | 4256 | goto out_free_1; |
002c7f7c YS |
4257 | } |
4258 | ||
73c1b41e | 4259 | r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting", |
8c18b2d2 | 4260 | kvm_starting_cpu, kvm_dying_cpu); |
774c47f1 | 4261 | if (r) |
d2308784 | 4262 | goto out_free_2; |
6aa8b732 AK |
4263 | register_reboot_notifier(&kvm_reboot_notifier); |
4264 | ||
c16f862d | 4265 | /* A kmem cache lets us meet the alignment requirements of fx_save. */ |
0ee75bea AK |
4266 | if (!vcpu_align) |
4267 | vcpu_align = __alignof__(struct kvm_vcpu); | |
46515736 PB |
4268 | kvm_vcpu_cache = |
4269 | kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, | |
4270 | SLAB_ACCOUNT, | |
4271 | offsetof(struct kvm_vcpu, arch), | |
4272 | sizeof_field(struct kvm_vcpu, arch), | |
4273 | NULL); | |
c16f862d RR |
4274 | if (!kvm_vcpu_cache) { |
4275 | r = -ENOMEM; | |
fb3600cc | 4276 | goto out_free_3; |
c16f862d RR |
4277 | } |
4278 | ||
af585b92 GN |
4279 | r = kvm_async_pf_init(); |
4280 | if (r) | |
4281 | goto out_free; | |
4282 | ||
6aa8b732 | 4283 | kvm_chardev_ops.owner = module; |
3d3aab1b CB |
4284 | kvm_vm_fops.owner = module; |
4285 | kvm_vcpu_fops.owner = module; | |
6aa8b732 AK |
4286 | |
4287 | r = misc_register(&kvm_dev); | |
4288 | if (r) { | |
1170adc6 | 4289 | pr_err("kvm: misc device register failed\n"); |
af585b92 | 4290 | goto out_unreg; |
6aa8b732 AK |
4291 | } |
4292 | ||
fb3600cc RW |
4293 | register_syscore_ops(&kvm_syscore_ops); |
4294 | ||
15ad7146 AK |
4295 | kvm_preempt_ops.sched_in = kvm_sched_in; |
4296 | kvm_preempt_ops.sched_out = kvm_sched_out; | |
4297 | ||
929f45e3 | 4298 | kvm_init_debug(); |
0ea4ed8e | 4299 | |
3c3c29fd PB |
4300 | r = kvm_vfio_ops_init(); |
4301 | WARN_ON(r); | |
4302 | ||
c7addb90 | 4303 | return 0; |
6aa8b732 | 4304 | |
af585b92 GN |
4305 | out_unreg: |
4306 | kvm_async_pf_deinit(); | |
6aa8b732 | 4307 | out_free: |
c16f862d | 4308 | kmem_cache_destroy(kvm_vcpu_cache); |
d2308784 | 4309 | out_free_3: |
6aa8b732 | 4310 | unregister_reboot_notifier(&kvm_reboot_notifier); |
8c18b2d2 | 4311 | cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); |
d2308784 | 4312 | out_free_2: |
d2308784 | 4313 | out_free_1: |
e9b11c17 | 4314 | kvm_arch_hardware_unsetup(); |
7f59f492 RR |
4315 | out_free_0a: |
4316 | free_cpumask_var(cpus_hardware_enabled); | |
d2308784 | 4317 | out_free_0: |
a0f155e9 | 4318 | kvm_irqfd_exit(); |
36343f6e | 4319 | out_irqfd: |
7dac16c3 AH |
4320 | kvm_arch_exit(); |
4321 | out_fail: | |
6aa8b732 AK |
4322 | return r; |
4323 | } | |
cb498ea2 | 4324 | EXPORT_SYMBOL_GPL(kvm_init); |
6aa8b732 | 4325 | |
cb498ea2 | 4326 | void kvm_exit(void) |
6aa8b732 | 4327 | { |
4bd33b56 | 4328 | debugfs_remove_recursive(kvm_debugfs_dir); |
6aa8b732 | 4329 | misc_deregister(&kvm_dev); |
c16f862d | 4330 | kmem_cache_destroy(kvm_vcpu_cache); |
af585b92 | 4331 | kvm_async_pf_deinit(); |
fb3600cc | 4332 | unregister_syscore_ops(&kvm_syscore_ops); |
6aa8b732 | 4333 | unregister_reboot_notifier(&kvm_reboot_notifier); |
8c18b2d2 | 4334 | cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING); |
75b7127c | 4335 | on_each_cpu(hardware_disable_nolock, NULL, 1); |
e9b11c17 | 4336 | kvm_arch_hardware_unsetup(); |
f8c16bba | 4337 | kvm_arch_exit(); |
a0f155e9 | 4338 | kvm_irqfd_exit(); |
7f59f492 | 4339 | free_cpumask_var(cpus_hardware_enabled); |
571ee1b6 | 4340 | kvm_vfio_ops_exit(); |
6aa8b732 | 4341 | } |
cb498ea2 | 4342 | EXPORT_SYMBOL_GPL(kvm_exit); |