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