Commit | Line | Data |
---|---|---|
edf88417 AK |
1 | #ifndef __KVM_HOST_H |
2 | #define __KVM_HOST_H | |
6aa8b732 | 3 | |
f445f11e KH |
4 | #if IS_ENABLED(CONFIG_KVM) |
5 | ||
6aa8b732 AK |
6 | /* |
7 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
8 | * the COPYING file in the top-level directory. | |
9 | */ | |
10 | ||
11 | #include <linux/types.h> | |
e56a7a28 | 12 | #include <linux/hardirq.h> |
6aa8b732 AK |
13 | #include <linux/list.h> |
14 | #include <linux/mutex.h> | |
15 | #include <linux/spinlock.h> | |
06ff0d37 MR |
16 | #include <linux/signal.h> |
17 | #include <linux/sched.h> | |
187f1882 | 18 | #include <linux/bug.h> |
6aa8b732 | 19 | #include <linux/mm.h> |
b297e672 | 20 | #include <linux/mmu_notifier.h> |
15ad7146 | 21 | #include <linux/preempt.h> |
0937c48d | 22 | #include <linux/msi.h> |
d89f5eff | 23 | #include <linux/slab.h> |
bd2b53b2 | 24 | #include <linux/rcupdate.h> |
bd80158a | 25 | #include <linux/ratelimit.h> |
83f09228 | 26 | #include <linux/err.h> |
c11f11fc | 27 | #include <linux/irqflags.h> |
e8edc6e0 | 28 | #include <asm/signal.h> |
6aa8b732 | 29 | |
6aa8b732 | 30 | #include <linux/kvm.h> |
102d8325 | 31 | #include <linux/kvm_para.h> |
6aa8b732 | 32 | |
edf88417 | 33 | #include <linux/kvm_types.h> |
d77a39d9 | 34 | |
edf88417 | 35 | #include <asm/kvm_host.h> |
d657a98e | 36 | |
cef4dea0 AK |
37 | #ifndef KVM_MMIO_SIZE |
38 | #define KVM_MMIO_SIZE 8 | |
39 | #endif | |
40 | ||
67b29204 XG |
41 | /* |
42 | * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used | |
43 | * in kvm, other bits are visible for userspace which are defined in | |
44 | * include/linux/kvm_h. | |
45 | */ | |
46 | #define KVM_MEMSLOT_INVALID (1UL << 16) | |
47 | ||
87da7e66 XG |
48 | /* Two fragments for cross MMIO pages. */ |
49 | #define KVM_MAX_MMIO_FRAGMENTS 2 | |
f78146b0 | 50 | |
9c5b1172 XG |
51 | /* |
52 | * For the normal pfn, the highest 12 bits should be zero, | |
81c52c56 XG |
53 | * so we can mask bit 62 ~ bit 52 to indicate the error pfn, |
54 | * mask bit 63 to indicate the noslot pfn. | |
9c5b1172 | 55 | */ |
81c52c56 XG |
56 | #define KVM_PFN_ERR_MASK (0x7ffULL << 52) |
57 | #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52) | |
58 | #define KVM_PFN_NOSLOT (0x1ULL << 63) | |
9c5b1172 XG |
59 | |
60 | #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK) | |
61 | #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1) | |
81c52c56 | 62 | #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2) |
6c8ee57b | 63 | |
81c52c56 XG |
64 | /* |
65 | * error pfns indicate that the gfn is in slot but faild to | |
66 | * translate it to pfn on host. | |
67 | */ | |
9c5b1172 | 68 | static inline bool is_error_pfn(pfn_t pfn) |
83f09228 | 69 | { |
9c5b1172 | 70 | return !!(pfn & KVM_PFN_ERR_MASK); |
83f09228 XG |
71 | } |
72 | ||
81c52c56 XG |
73 | /* |
74 | * error_noslot pfns indicate that the gfn can not be | |
75 | * translated to pfn - it is not in slot or failed to | |
76 | * translate it to pfn. | |
77 | */ | |
78 | static inline bool is_error_noslot_pfn(pfn_t pfn) | |
83f09228 | 79 | { |
81c52c56 | 80 | return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK); |
83f09228 XG |
81 | } |
82 | ||
81c52c56 XG |
83 | /* noslot pfn indicates that the gfn is not in slot. */ |
84 | static inline bool is_noslot_pfn(pfn_t pfn) | |
83f09228 | 85 | { |
81c52c56 | 86 | return pfn == KVM_PFN_NOSLOT; |
83f09228 XG |
87 | } |
88 | ||
7068d097 XG |
89 | #define KVM_HVA_ERR_BAD (PAGE_OFFSET) |
90 | #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE) | |
ca3a490c XG |
91 | |
92 | static inline bool kvm_is_error_hva(unsigned long addr) | |
93 | { | |
7068d097 | 94 | return addr >= PAGE_OFFSET; |
ca3a490c XG |
95 | } |
96 | ||
6cede2e6 XG |
97 | #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT)) |
98 | ||
9c5b1172 | 99 | static inline bool is_error_page(struct page *page) |
6cede2e6 XG |
100 | { |
101 | return IS_ERR(page); | |
102 | } | |
103 | ||
d9e368d6 AK |
104 | /* |
105 | * vcpu->requests bit members | |
106 | */ | |
3176bc3e | 107 | #define KVM_REQ_TLB_FLUSH 0 |
2f52d58c | 108 | #define KVM_REQ_MIGRATE_TIMER 1 |
b209749f | 109 | #define KVM_REQ_REPORT_TPR_ACCESS 2 |
2e53d63a | 110 | #define KVM_REQ_MMU_RELOAD 3 |
71c4dfaf | 111 | #define KVM_REQ_TRIPLE_FAULT 4 |
06e05645 | 112 | #define KVM_REQ_PENDING_TIMER 5 |
d7690175 | 113 | #define KVM_REQ_UNHALT 6 |
4731d4c7 | 114 | #define KVM_REQ_MMU_SYNC 7 |
34c238a1 | 115 | #define KVM_REQ_CLOCK_UPDATE 8 |
32f88400 | 116 | #define KVM_REQ_KICK 9 |
02daab21 | 117 | #define KVM_REQ_DEACTIVATE_FPU 10 |
3842d135 | 118 | #define KVM_REQ_EVENT 11 |
af585b92 | 119 | #define KVM_REQ_APF_HALT 12 |
c9aaa895 | 120 | #define KVM_REQ_STEAL_UPDATE 13 |
7460fb4a | 121 | #define KVM_REQ_NMI 14 |
d6185f20 | 122 | #define KVM_REQ_IMMEDIATE_EXIT 15 |
f5132b01 GN |
123 | #define KVM_REQ_PMU 16 |
124 | #define KVM_REQ_PMI 17 | |
f61c94bb | 125 | #define KVM_REQ_WATCHDOG 18 |
d828199e MT |
126 | #define KVM_REQ_MASTERCLOCK_UPDATE 19 |
127 | #define KVM_REQ_MCLOCK_INPROGRESS 20 | |
1c810636 | 128 | #define KVM_REQ_EPR_EXIT 21 |
c7c9c56c | 129 | #define KVM_REQ_EOIBITMAP 22 |
6aa8b732 | 130 | |
7a84428a AW |
131 | #define KVM_USERSPACE_IRQ_SOURCE_ID 0 |
132 | #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 | |
5550af4d | 133 | |
6c474694 | 134 | struct kvm; |
6aa8b732 | 135 | struct kvm_vcpu; |
c16f862d | 136 | extern struct kmem_cache *kvm_vcpu_cache; |
6aa8b732 | 137 | |
fc1b7492 GL |
138 | extern raw_spinlock_t kvm_lock; |
139 | extern struct list_head vm_list; | |
140 | ||
743eeb0b SL |
141 | struct kvm_io_range { |
142 | gpa_t addr; | |
143 | int len; | |
144 | struct kvm_io_device *dev; | |
145 | }; | |
146 | ||
786a9f88 | 147 | #define NR_IOBUS_DEVS 1000 |
a1300716 | 148 | |
2eeb2e94 GH |
149 | struct kvm_io_bus { |
150 | int dev_count; | |
a1300716 | 151 | struct kvm_io_range range[]; |
2eeb2e94 GH |
152 | }; |
153 | ||
e93f8a0f MT |
154 | enum kvm_bus { |
155 | KVM_MMIO_BUS, | |
156 | KVM_PIO_BUS, | |
060f0ce6 | 157 | KVM_VIRTIO_CCW_NOTIFY_BUS, |
e93f8a0f MT |
158 | KVM_NR_BUSES |
159 | }; | |
160 | ||
161 | int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, | |
162 | int len, const void *val); | |
163 | int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len, | |
bda9020e | 164 | void *val); |
743eeb0b SL |
165 | int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, |
166 | int len, struct kvm_io_device *dev); | |
e93f8a0f MT |
167 | int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, |
168 | struct kvm_io_device *dev); | |
2eeb2e94 | 169 | |
af585b92 GN |
170 | #ifdef CONFIG_KVM_ASYNC_PF |
171 | struct kvm_async_pf { | |
172 | struct work_struct work; | |
173 | struct list_head link; | |
174 | struct list_head queue; | |
175 | struct kvm_vcpu *vcpu; | |
176 | struct mm_struct *mm; | |
177 | gva_t gva; | |
178 | unsigned long addr; | |
179 | struct kvm_arch_async_pf arch; | |
180 | struct page *page; | |
181 | bool done; | |
182 | }; | |
183 | ||
184 | void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu); | |
185 | void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu); | |
186 | int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, | |
187 | struct kvm_arch_async_pf *arch); | |
344d9588 | 188 | int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); |
af585b92 GN |
189 | #endif |
190 | ||
6b7e2d09 XG |
191 | enum { |
192 | OUTSIDE_GUEST_MODE, | |
193 | IN_GUEST_MODE, | |
c142786c AK |
194 | EXITING_GUEST_MODE, |
195 | READING_SHADOW_PAGE_TABLES, | |
6b7e2d09 XG |
196 | }; |
197 | ||
f78146b0 AK |
198 | /* |
199 | * Sometimes a large or cross-page mmio needs to be broken up into separate | |
200 | * exits for userspace servicing. | |
201 | */ | |
202 | struct kvm_mmio_fragment { | |
203 | gpa_t gpa; | |
204 | void *data; | |
205 | unsigned len; | |
206 | }; | |
207 | ||
d17fbbf7 ZX |
208 | struct kvm_vcpu { |
209 | struct kvm *kvm; | |
31bb117e | 210 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
d17fbbf7 | 211 | struct preempt_notifier preempt_notifier; |
31bb117e | 212 | #endif |
6b7e2d09 | 213 | int cpu; |
d17fbbf7 | 214 | int vcpu_id; |
6b7e2d09 XG |
215 | int srcu_idx; |
216 | int mode; | |
d17fbbf7 | 217 | unsigned long requests; |
d0bfb940 | 218 | unsigned long guest_debug; |
6b7e2d09 XG |
219 | |
220 | struct mutex mutex; | |
221 | struct kvm_run *run; | |
f656ce01 | 222 | |
d17fbbf7 | 223 | int fpu_active; |
2acf923e | 224 | int guest_fpu_loaded, guest_xcr0_loaded; |
d17fbbf7 | 225 | wait_queue_head_t wq; |
34bb10b7 | 226 | struct pid *pid; |
d17fbbf7 ZX |
227 | int sigset_active; |
228 | sigset_t sigset; | |
229 | struct kvm_vcpu_stat stat; | |
230 | ||
34c16eec | 231 | #ifdef CONFIG_HAS_IOMEM |
d17fbbf7 ZX |
232 | int mmio_needed; |
233 | int mmio_read_completed; | |
234 | int mmio_is_write; | |
f78146b0 AK |
235 | int mmio_cur_fragment; |
236 | int mmio_nr_fragments; | |
237 | struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS]; | |
34c16eec | 238 | #endif |
1165f5fe | 239 | |
af585b92 GN |
240 | #ifdef CONFIG_KVM_ASYNC_PF |
241 | struct { | |
242 | u32 queued; | |
243 | struct list_head queue; | |
244 | struct list_head done; | |
245 | spinlock_t lock; | |
246 | } async_pf; | |
247 | #endif | |
248 | ||
4c088493 R |
249 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
250 | /* | |
251 | * Cpu relax intercept or pause loop exit optimization | |
252 | * in_spin_loop: set when a vcpu does a pause loop exit | |
253 | * or cpu relax intercepted. | |
254 | * dy_eligible: indicates whether vcpu is eligible for directed yield. | |
255 | */ | |
256 | struct { | |
257 | bool in_spin_loop; | |
258 | bool dy_eligible; | |
259 | } spin_loop; | |
260 | #endif | |
3a08a8f9 | 261 | bool preempted; |
d657a98e ZX |
262 | struct kvm_vcpu_arch arch; |
263 | }; | |
264 | ||
6b7e2d09 XG |
265 | static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) |
266 | { | |
267 | return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE); | |
268 | } | |
269 | ||
660c22c4 TY |
270 | /* |
271 | * Some of the bitops functions do not support too long bitmaps. | |
272 | * This number must be determined not to exceed such limits. | |
273 | */ | |
274 | #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) | |
275 | ||
6aa8b732 AK |
276 | struct kvm_memory_slot { |
277 | gfn_t base_gfn; | |
278 | unsigned long npages; | |
6aa8b732 | 279 | unsigned long *dirty_bitmap; |
db3fe4eb | 280 | struct kvm_arch_memory_slot arch; |
8a7ae055 | 281 | unsigned long userspace_addr; |
6104f472 | 282 | u32 flags; |
1e702d9a | 283 | short id; |
6aa8b732 AK |
284 | }; |
285 | ||
87bf6e7d TY |
286 | static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot) |
287 | { | |
288 | return ALIGN(memslot->npages, BITS_PER_LONG) / 8; | |
289 | } | |
290 | ||
399ec807 AK |
291 | struct kvm_kernel_irq_routing_entry { |
292 | u32 gsi; | |
5116d8f6 | 293 | u32 type; |
4925663a | 294 | int (*set)(struct kvm_kernel_irq_routing_entry *e, |
1a6e4a8c | 295 | struct kvm *kvm, int irq_source_id, int level); |
399ec807 AK |
296 | union { |
297 | struct { | |
298 | unsigned irqchip; | |
299 | unsigned pin; | |
300 | } irqchip; | |
79950e10 | 301 | struct msi_msg msi; |
399ec807 | 302 | }; |
46e624b9 GN |
303 | struct hlist_node link; |
304 | }; | |
305 | ||
3e71f88b GN |
306 | #ifdef __KVM_HAVE_IOAPIC |
307 | ||
46e624b9 | 308 | struct kvm_irq_routing_table { |
3e71f88b | 309 | int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS]; |
46e624b9 GN |
310 | struct kvm_kernel_irq_routing_entry *rt_entries; |
311 | u32 nr_rt_entries; | |
312 | /* | |
313 | * Array indexed by gsi. Each entry contains list of irq chips | |
314 | * the gsi is connected to. | |
315 | */ | |
316 | struct hlist_head map[0]; | |
399ec807 AK |
317 | }; |
318 | ||
3e71f88b GN |
319 | #else |
320 | ||
321 | struct kvm_irq_routing_table {}; | |
322 | ||
323 | #endif | |
324 | ||
0743247f AW |
325 | #ifndef KVM_PRIVATE_MEM_SLOTS |
326 | #define KVM_PRIVATE_MEM_SLOTS 0 | |
327 | #endif | |
328 | ||
93a5cef0 | 329 | #ifndef KVM_MEM_SLOTS_NUM |
bbacc0c1 | 330 | #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS) |
93a5cef0 XG |
331 | #endif |
332 | ||
bf3e05bc XG |
333 | /* |
334 | * Note: | |
335 | * memslots are not sorted by id anymore, please use id_to_memslot() | |
336 | * to get the memslot by its id. | |
337 | */ | |
46a26bf5 | 338 | struct kvm_memslots { |
49c7754c | 339 | u64 generation; |
93a5cef0 | 340 | struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM]; |
f85e2cb5 | 341 | /* The mapping table from slot id to the index in memslots[]. */ |
1e702d9a | 342 | short id_to_index[KVM_MEM_SLOTS_NUM]; |
46a26bf5 MT |
343 | }; |
344 | ||
6aa8b732 | 345 | struct kvm { |
aaee2c94 | 346 | spinlock_t mmu_lock; |
79fac95e | 347 | struct mutex slots_lock; |
6d4e4c4f | 348 | struct mm_struct *mm; /* userspace tied to this vm */ |
46a26bf5 | 349 | struct kvm_memslots *memslots; |
bc6678a3 | 350 | struct srcu_struct srcu; |
73880c80 GN |
351 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
352 | u32 bsp_vcpu_id; | |
73880c80 | 353 | #endif |
fb3f0f51 | 354 | struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; |
73880c80 | 355 | atomic_t online_vcpus; |
217ece61 | 356 | int last_boosted_vcpu; |
133de902 | 357 | struct list_head vm_list; |
60eead79 | 358 | struct mutex lock; |
e93f8a0f | 359 | struct kvm_io_bus *buses[KVM_NR_BUSES]; |
721eecbf GH |
360 | #ifdef CONFIG_HAVE_KVM_EVENTFD |
361 | struct { | |
362 | spinlock_t lock; | |
363 | struct list_head items; | |
7a84428a AW |
364 | struct list_head resampler_list; |
365 | struct mutex resampler_lock; | |
721eecbf | 366 | } irqfds; |
d34e6b17 | 367 | struct list_head ioeventfds; |
721eecbf | 368 | #endif |
ba1389b7 | 369 | struct kvm_vm_stat stat; |
d69fb81f | 370 | struct kvm_arch arch; |
d39f13b0 | 371 | atomic_t users_count; |
5f94c174 | 372 | #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET |
5f94c174 | 373 | struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; |
2b3c246a SL |
374 | spinlock_t ring_lock; |
375 | struct list_head coalesced_zones; | |
5f94c174 | 376 | #endif |
e930bffe | 377 | |
60eead79 | 378 | struct mutex irq_lock; |
75858a84 | 379 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
bd2b53b2 MT |
380 | /* |
381 | * Update side is protected by irq_lock and, | |
382 | * if configured, irqfds.lock. | |
383 | */ | |
4b6a2872 | 384 | struct kvm_irq_routing_table __rcu *irq_routing; |
75858a84 | 385 | struct hlist_head mask_notifier_list; |
136bdfee | 386 | struct hlist_head irq_ack_notifier_list; |
75858a84 AK |
387 | #endif |
388 | ||
36c1ed82 | 389 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
e930bffe AA |
390 | struct mmu_notifier mmu_notifier; |
391 | unsigned long mmu_notifier_seq; | |
392 | long mmu_notifier_count; | |
393 | #endif | |
5c663a15 | 394 | long tlbs_dirty; |
6aa8b732 AK |
395 | }; |
396 | ||
a737f256 CD |
397 | #define kvm_err(fmt, ...) \ |
398 | pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) | |
399 | #define kvm_info(fmt, ...) \ | |
400 | pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) | |
401 | #define kvm_debug(fmt, ...) \ | |
402 | pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__) | |
403 | #define kvm_pr_unimpl(fmt, ...) \ | |
404 | pr_err_ratelimited("kvm [%i]: " fmt, \ | |
405 | task_tgid_nr(current), ## __VA_ARGS__) | |
f0242478 | 406 | |
a737f256 CD |
407 | /* The guest did something we don't support. */ |
408 | #define vcpu_unimpl(vcpu, fmt, ...) \ | |
409 | kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__) | |
6aa8b732 | 410 | |
988a2cae GN |
411 | static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i) |
412 | { | |
413 | smp_rmb(); | |
414 | return kvm->vcpus[i]; | |
415 | } | |
416 | ||
417 | #define kvm_for_each_vcpu(idx, vcpup, kvm) \ | |
b42fc3cb JM |
418 | for (idx = 0; \ |
419 | idx < atomic_read(&kvm->online_vcpus) && \ | |
420 | (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \ | |
421 | idx++) | |
988a2cae | 422 | |
be6ba0f0 XG |
423 | #define kvm_for_each_memslot(memslot, slots) \ |
424 | for (memslot = &slots->memslots[0]; \ | |
bf3e05bc XG |
425 | memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\ |
426 | memslot++) | |
be6ba0f0 | 427 | |
fb3f0f51 RR |
428 | int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id); |
429 | void kvm_vcpu_uninit(struct kvm_vcpu *vcpu); | |
430 | ||
9fc77441 | 431 | int __must_check vcpu_load(struct kvm_vcpu *vcpu); |
313a3dc7 CO |
432 | void vcpu_put(struct kvm_vcpu *vcpu); |
433 | ||
a0f155e9 CH |
434 | #ifdef __KVM_HAVE_IOAPIC |
435 | int kvm_irqfd_init(void); | |
436 | void kvm_irqfd_exit(void); | |
437 | #else | |
438 | static inline int kvm_irqfd_init(void) | |
439 | { | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static inline void kvm_irqfd_exit(void) | |
444 | { | |
445 | } | |
446 | #endif | |
0ee75bea | 447 | int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, |
c16f862d | 448 | struct module *module); |
cb498ea2 | 449 | void kvm_exit(void); |
6aa8b732 | 450 | |
d39f13b0 IE |
451 | void kvm_get_kvm(struct kvm *kvm); |
452 | void kvm_put_kvm(struct kvm *kvm); | |
116c14c0 AW |
453 | void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new, |
454 | u64 last_generation); | |
d39f13b0 | 455 | |
90d83dc3 LJ |
456 | static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm) |
457 | { | |
458 | return rcu_dereference_check(kvm->memslots, | |
459 | srcu_read_lock_held(&kvm->srcu) | |
460 | || lockdep_is_held(&kvm->slots_lock)); | |
461 | } | |
462 | ||
28a37544 XG |
463 | static inline struct kvm_memory_slot * |
464 | id_to_memslot(struct kvm_memslots *slots, int id) | |
465 | { | |
f85e2cb5 XG |
466 | int index = slots->id_to_index[id]; |
467 | struct kvm_memory_slot *slot; | |
bf3e05bc | 468 | |
f85e2cb5 | 469 | slot = &slots->memslots[index]; |
bf3e05bc | 470 | |
f85e2cb5 XG |
471 | WARN_ON(slot->id != id); |
472 | return slot; | |
28a37544 XG |
473 | } |
474 | ||
74d0727c TY |
475 | /* |
476 | * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: | |
477 | * - create a new memory slot | |
478 | * - delete an existing memory slot | |
479 | * - modify an existing memory slot | |
480 | * -- move it in the guest physical memory space | |
481 | * -- just change its flags | |
482 | * | |
483 | * Since flags can be changed by some of these operations, the following | |
484 | * differentiation is the best we can do for __kvm_set_memory_region(): | |
485 | */ | |
486 | enum kvm_mr_change { | |
487 | KVM_MR_CREATE, | |
488 | KVM_MR_DELETE, | |
489 | KVM_MR_MOVE, | |
490 | KVM_MR_FLAGS_ONLY, | |
491 | }; | |
492 | ||
210c7c4d | 493 | int kvm_set_memory_region(struct kvm *kvm, |
47ae31e2 | 494 | struct kvm_userspace_memory_region *mem); |
f78e0e2e | 495 | int __kvm_set_memory_region(struct kvm *kvm, |
47ae31e2 | 496 | struct kvm_userspace_memory_region *mem); |
db3fe4eb TY |
497 | void kvm_arch_free_memslot(struct kvm_memory_slot *free, |
498 | struct kvm_memory_slot *dont); | |
499 | int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages); | |
f7784b8e MT |
500 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
501 | struct kvm_memory_slot *memslot, | |
7b6195a9 TY |
502 | struct kvm_userspace_memory_region *mem, |
503 | enum kvm_mr_change change); | |
f7784b8e | 504 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
0de10343 | 505 | struct kvm_userspace_memory_region *mem, |
8482644a TY |
506 | const struct kvm_memory_slot *old, |
507 | enum kvm_mr_change change); | |
db3fe4eb | 508 | bool kvm_largepages_enabled(void); |
54dee993 | 509 | void kvm_disable_largepages(void); |
2df72e9b MT |
510 | /* flush all memory translations */ |
511 | void kvm_arch_flush_shadow_all(struct kvm *kvm); | |
512 | /* flush memory translations pointing to 'slot' */ | |
513 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, | |
514 | struct kvm_memory_slot *slot); | |
a983fb23 | 515 | |
48987781 XG |
516 | int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages, |
517 | int nr_pages); | |
518 | ||
954bbbc2 | 519 | struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn); |
05da4558 | 520 | unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn); |
4d8b81ab | 521 | unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn); |
b4231d61 IE |
522 | void kvm_release_page_clean(struct page *page); |
523 | void kvm_release_page_dirty(struct page *page); | |
35149e21 AL |
524 | void kvm_set_page_dirty(struct page *page); |
525 | void kvm_set_page_accessed(struct page *page); | |
526 | ||
365fb3fd | 527 | pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn); |
612819c3 MT |
528 | pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async, |
529 | bool write_fault, bool *writable); | |
35149e21 | 530 | pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); |
612819c3 MT |
531 | pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, |
532 | bool *writable); | |
d5661048 | 533 | pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn); |
037d92dc XG |
534 | pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn); |
535 | ||
32cad84f | 536 | void kvm_release_pfn_dirty(pfn_t pfn); |
35149e21 AL |
537 | void kvm_release_pfn_clean(pfn_t pfn); |
538 | void kvm_set_pfn_dirty(pfn_t pfn); | |
539 | void kvm_set_pfn_accessed(pfn_t pfn); | |
540 | void kvm_get_pfn(pfn_t pfn); | |
541 | ||
195aefde IE |
542 | int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, |
543 | int len); | |
7ec54588 MT |
544 | int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data, |
545 | unsigned long len); | |
195aefde | 546 | int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); |
e03b644f GN |
547 | int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
548 | void *data, unsigned long len); | |
195aefde IE |
549 | int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, |
550 | int offset, int len); | |
551 | int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data, | |
552 | unsigned long len); | |
49c7754c GN |
553 | int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, |
554 | void *data, unsigned long len); | |
555 | int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, | |
556 | gpa_t gpa); | |
195aefde IE |
557 | int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len); |
558 | int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len); | |
6aa8b732 | 559 | struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); |
e0d62c7f | 560 | int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); |
8f0b1ab6 | 561 | unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn); |
6aa8b732 | 562 | void mark_page_dirty(struct kvm *kvm, gfn_t gfn); |
49c7754c GN |
563 | void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot, |
564 | gfn_t gfn); | |
6aa8b732 | 565 | |
8776e519 | 566 | void kvm_vcpu_block(struct kvm_vcpu *vcpu); |
b6d33834 | 567 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); |
41628d33 | 568 | bool kvm_vcpu_yield_to(struct kvm_vcpu *target); |
d255f4f2 | 569 | void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu); |
6aa8b732 | 570 | void kvm_resched(struct kvm_vcpu *vcpu); |
7702fd1f AK |
571 | void kvm_load_guest_fpu(struct kvm_vcpu *vcpu); |
572 | void kvm_put_guest_fpu(struct kvm_vcpu *vcpu); | |
a4ee1ca4 | 573 | |
d9e368d6 | 574 | void kvm_flush_remote_tlbs(struct kvm *kvm); |
2e53d63a | 575 | void kvm_reload_remote_mmus(struct kvm *kvm); |
d828199e | 576 | void kvm_make_mclock_inprogress_request(struct kvm *kvm); |
c7c9c56c | 577 | void kvm_make_update_eoibitmap_request(struct kvm *kvm); |
6aa8b732 | 578 | |
043405e1 CO |
579 | long kvm_arch_dev_ioctl(struct file *filp, |
580 | unsigned int ioctl, unsigned long arg); | |
313a3dc7 CO |
581 | long kvm_arch_vcpu_ioctl(struct file *filp, |
582 | unsigned int ioctl, unsigned long arg); | |
5b1c1493 | 583 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf); |
018d00d2 ZX |
584 | |
585 | int kvm_dev_ioctl_check_extension(long ext); | |
586 | ||
5bb064dc ZX |
587 | int kvm_get_dirty_log(struct kvm *kvm, |
588 | struct kvm_dirty_log *log, int *is_dirty); | |
589 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
590 | struct kvm_dirty_log *log); | |
591 | ||
1fe779f8 | 592 | int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, |
47ae31e2 | 593 | struct kvm_userspace_memory_region *mem); |
23d43cf9 | 594 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level); |
1fe779f8 CO |
595 | long kvm_arch_vm_ioctl(struct file *filp, |
596 | unsigned int ioctl, unsigned long arg); | |
313a3dc7 | 597 | |
d0752060 HB |
598 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); |
599 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu); | |
600 | ||
8b006791 ZX |
601 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
602 | struct kvm_translation *tr); | |
603 | ||
b6c7a5dc HB |
604 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); |
605 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs); | |
606 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
607 | struct kvm_sregs *sregs); | |
608 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
609 | struct kvm_sregs *sregs); | |
62d9f0db MT |
610 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
611 | struct kvm_mp_state *mp_state); | |
612 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
613 | struct kvm_mp_state *mp_state); | |
d0bfb940 JK |
614 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
615 | struct kvm_guest_debug *dbg); | |
b6c7a5dc HB |
616 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run); |
617 | ||
f8c16bba ZX |
618 | int kvm_arch_init(void *opaque); |
619 | void kvm_arch_exit(void); | |
043405e1 | 620 | |
e9b11c17 ZX |
621 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu); |
622 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu); | |
623 | ||
624 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu); | |
625 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); | |
626 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); | |
627 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id); | |
26e5215f | 628 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu); |
42897d86 | 629 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu); |
d40ccc62 | 630 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); |
e9b11c17 | 631 | |
10474ae8 | 632 | int kvm_arch_hardware_enable(void *garbage); |
e9b11c17 ZX |
633 | void kvm_arch_hardware_disable(void *garbage); |
634 | int kvm_arch_hardware_setup(void); | |
635 | void kvm_arch_hardware_unsetup(void); | |
636 | void kvm_arch_check_processor_compat(void *rtn); | |
1d737c8a | 637 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu); |
b6d33834 | 638 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); |
e9b11c17 | 639 | |
d19a9cd2 ZX |
640 | void kvm_free_physmem(struct kvm *kvm); |
641 | ||
c1a7b32a TY |
642 | void *kvm_kvzalloc(unsigned long size); |
643 | void kvm_kvfree(const void *addr); | |
644 | ||
d89f5eff JK |
645 | #ifndef __KVM_HAVE_ARCH_VM_ALLOC |
646 | static inline struct kvm *kvm_arch_alloc_vm(void) | |
647 | { | |
648 | return kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
649 | } | |
650 | ||
651 | static inline void kvm_arch_free_vm(struct kvm *kvm) | |
652 | { | |
653 | kfree(kvm); | |
654 | } | |
655 | #endif | |
656 | ||
b6d33834 CD |
657 | static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu) |
658 | { | |
2246f8b5 AG |
659 | #ifdef __KVM_HAVE_ARCH_WQP |
660 | return vcpu->arch.wqp; | |
661 | #else | |
b6d33834 | 662 | return &vcpu->wq; |
b6d33834 | 663 | #endif |
2246f8b5 | 664 | } |
b6d33834 | 665 | |
e08b9637 | 666 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type); |
d19a9cd2 | 667 | void kvm_arch_destroy_vm(struct kvm *kvm); |
8a98f664 | 668 | void kvm_free_all_assigned_devices(struct kvm *kvm); |
ad8ba2cd | 669 | void kvm_arch_sync_events(struct kvm *kvm); |
e9b11c17 | 670 | |
3d80840d | 671 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); |
5736199a | 672 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); |
682c59a3 | 673 | |
a2766325 | 674 | bool kvm_is_mmio_pfn(pfn_t pfn); |
c77fb9dc | 675 | |
62c476c7 BAY |
676 | struct kvm_irq_ack_notifier { |
677 | struct hlist_node link; | |
678 | unsigned gsi; | |
679 | void (*irq_acked)(struct kvm_irq_ack_notifier *kian); | |
680 | }; | |
681 | ||
682 | struct kvm_assigned_dev_kernel { | |
683 | struct kvm_irq_ack_notifier ack_notifier; | |
62c476c7 BAY |
684 | struct list_head list; |
685 | int assigned_dev_id; | |
ab9f4ecb | 686 | int host_segnr; |
62c476c7 BAY |
687 | int host_busnr; |
688 | int host_devfn; | |
c1e01514 | 689 | unsigned int entries_nr; |
62c476c7 | 690 | int host_irq; |
defaf158 | 691 | bool host_irq_disabled; |
07700a94 | 692 | bool pci_2_3; |
c1e01514 | 693 | struct msix_entry *host_msix_entries; |
62c476c7 | 694 | int guest_irq; |
0645211c | 695 | struct msix_entry *guest_msix_entries; |
4f906c19 | 696 | unsigned long irq_requested_type; |
5550af4d | 697 | int irq_source_id; |
b653574a | 698 | int flags; |
62c476c7 BAY |
699 | struct pci_dev *dev; |
700 | struct kvm *kvm; | |
0645211c | 701 | spinlock_t intx_lock; |
cf9eeac4 | 702 | spinlock_t intx_mask_lock; |
1e001d49 | 703 | char irq_name[32]; |
f8fcfd77 | 704 | struct pci_saved_state *pci_saved_state; |
62c476c7 | 705 | }; |
75858a84 AK |
706 | |
707 | struct kvm_irq_mask_notifier { | |
708 | void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked); | |
709 | int irq; | |
710 | struct hlist_node link; | |
711 | }; | |
712 | ||
713 | void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, | |
714 | struct kvm_irq_mask_notifier *kimn); | |
715 | void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, | |
716 | struct kvm_irq_mask_notifier *kimn); | |
4a994358 GN |
717 | void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin, |
718 | bool mask); | |
75858a84 | 719 | |
46e624b9 GN |
720 | #ifdef __KVM_HAVE_IOAPIC |
721 | void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic, | |
722 | union kvm_ioapic_redirect_entry *entry, | |
723 | unsigned long *deliver_bitmask); | |
724 | #endif | |
725 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level); | |
01f21880 | 726 | int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level); |
bd2b53b2 MT |
727 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm, |
728 | int irq_source_id, int level); | |
c7c9c56c | 729 | bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin); |
44882eed | 730 | void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin); |
3de42dc0 XZ |
731 | void kvm_register_irq_ack_notifier(struct kvm *kvm, |
732 | struct kvm_irq_ack_notifier *kian); | |
fa40a821 MT |
733 | void kvm_unregister_irq_ack_notifier(struct kvm *kvm, |
734 | struct kvm_irq_ack_notifier *kian); | |
5550af4d SY |
735 | int kvm_request_irq_source_id(struct kvm *kvm); |
736 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id); | |
62c476c7 | 737 | |
522c68c4 SY |
738 | /* For vcpu->arch.iommu_flags */ |
739 | #define KVM_IOMMU_CACHE_COHERENCY 0x1 | |
740 | ||
19de40a8 | 741 | #ifdef CONFIG_IOMMU_API |
3ad26d81 | 742 | int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot); |
32f6daad | 743 | void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot); |
260782bc | 744 | int kvm_iommu_map_guest(struct kvm *kvm); |
62c476c7 | 745 | int kvm_iommu_unmap_guest(struct kvm *kvm); |
260782bc WH |
746 | int kvm_assign_device(struct kvm *kvm, |
747 | struct kvm_assigned_dev_kernel *assigned_dev); | |
0a920356 WH |
748 | int kvm_deassign_device(struct kvm *kvm, |
749 | struct kvm_assigned_dev_kernel *assigned_dev); | |
19de40a8 | 750 | #else /* CONFIG_IOMMU_API */ |
62c476c7 | 751 | static inline int kvm_iommu_map_pages(struct kvm *kvm, |
d7a79b6c | 752 | struct kvm_memory_slot *slot) |
62c476c7 BAY |
753 | { |
754 | return 0; | |
755 | } | |
756 | ||
32f6daad AW |
757 | static inline void kvm_iommu_unmap_pages(struct kvm *kvm, |
758 | struct kvm_memory_slot *slot) | |
759 | { | |
760 | } | |
761 | ||
260782bc | 762 | static inline int kvm_iommu_map_guest(struct kvm *kvm) |
62c476c7 BAY |
763 | { |
764 | return -ENODEV; | |
765 | } | |
766 | ||
767 | static inline int kvm_iommu_unmap_guest(struct kvm *kvm) | |
768 | { | |
769 | return 0; | |
770 | } | |
260782bc WH |
771 | |
772 | static inline int kvm_assign_device(struct kvm *kvm, | |
773 | struct kvm_assigned_dev_kernel *assigned_dev) | |
774 | { | |
775 | return 0; | |
776 | } | |
0a920356 WH |
777 | |
778 | static inline int kvm_deassign_device(struct kvm *kvm, | |
779 | struct kvm_assigned_dev_kernel *assigned_dev) | |
780 | { | |
781 | return 0; | |
782 | } | |
19de40a8 | 783 | #endif /* CONFIG_IOMMU_API */ |
62c476c7 | 784 | |
6a61671b | 785 | static inline void __guest_enter(void) |
d172fcd3 | 786 | { |
b080935c FW |
787 | /* |
788 | * This is running in ioctl context so we can avoid | |
789 | * the call to vtime_account() with its unnecessary idle check. | |
790 | */ | |
c11f11fc | 791 | vtime_account_system(current); |
d172fcd3 | 792 | current->flags |= PF_VCPU; |
c11f11fc FW |
793 | } |
794 | ||
6a61671b | 795 | static inline void __guest_exit(void) |
c11f11fc FW |
796 | { |
797 | /* | |
798 | * This is running in ioctl context so we can avoid | |
799 | * the call to vtime_account() with its unnecessary idle check. | |
800 | */ | |
801 | vtime_account_system(current); | |
802 | current->flags &= ~PF_VCPU; | |
803 | } | |
804 | ||
6a61671b FW |
805 | #ifdef CONFIG_CONTEXT_TRACKING |
806 | extern void guest_enter(void); | |
807 | extern void guest_exit(void); | |
808 | ||
809 | #else /* !CONFIG_CONTEXT_TRACKING */ | |
810 | static inline void guest_enter(void) | |
811 | { | |
812 | __guest_enter(); | |
813 | } | |
814 | ||
815 | static inline void guest_exit(void) | |
816 | { | |
817 | __guest_exit(); | |
818 | } | |
819 | #endif /* !CONFIG_CONTEXT_TRACKING */ | |
820 | ||
c11f11fc FW |
821 | static inline void kvm_guest_enter(void) |
822 | { | |
823 | unsigned long flags; | |
824 | ||
825 | BUG_ON(preemptible()); | |
826 | ||
827 | local_irq_save(flags); | |
828 | guest_enter(); | |
829 | local_irq_restore(flags); | |
830 | ||
8fa22068 GN |
831 | /* KVM does not hold any references to rcu protected data when it |
832 | * switches CPU into a guest mode. In fact switching to a guest mode | |
833 | * is very similar to exiting to userspase from rcu point of view. In | |
834 | * addition CPU may stay in a guest mode for quite a long time (up to | |
835 | * one time slice). Lets treat guest mode as quiescent state, just like | |
836 | * we do with user-mode execution. | |
837 | */ | |
838 | rcu_virt_note_context_switch(smp_processor_id()); | |
d172fcd3 LV |
839 | } |
840 | ||
841 | static inline void kvm_guest_exit(void) | |
842 | { | |
c11f11fc FW |
843 | unsigned long flags; |
844 | ||
845 | local_irq_save(flags); | |
846 | guest_exit(); | |
847 | local_irq_restore(flags); | |
d172fcd3 LV |
848 | } |
849 | ||
9d4cba7f PM |
850 | /* |
851 | * search_memslots() and __gfn_to_memslot() are here because they are | |
852 | * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c. | |
853 | * gfn_to_memslot() itself isn't here as an inline because that would | |
854 | * bloat other code too much. | |
855 | */ | |
856 | static inline struct kvm_memory_slot * | |
857 | search_memslots(struct kvm_memslots *slots, gfn_t gfn) | |
858 | { | |
859 | struct kvm_memory_slot *memslot; | |
860 | ||
861 | kvm_for_each_memslot(memslot, slots) | |
862 | if (gfn >= memslot->base_gfn && | |
863 | gfn < memslot->base_gfn + memslot->npages) | |
864 | return memslot; | |
865 | ||
866 | return NULL; | |
867 | } | |
868 | ||
869 | static inline struct kvm_memory_slot * | |
870 | __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn) | |
871 | { | |
872 | return search_memslots(slots, gfn); | |
873 | } | |
874 | ||
66a03505 GS |
875 | static inline unsigned long |
876 | __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn) | |
877 | { | |
878 | return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE; | |
879 | } | |
880 | ||
0ee8dcb8 XG |
881 | static inline int memslot_id(struct kvm *kvm, gfn_t gfn) |
882 | { | |
883 | return gfn_to_memslot(kvm, gfn)->id; | |
884 | } | |
885 | ||
fb03cb6f TY |
886 | static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level) |
887 | { | |
888 | /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */ | |
889 | return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) - | |
890 | (base_gfn >> KVM_HPAGE_GFN_SHIFT(level)); | |
891 | } | |
892 | ||
d19a748b TY |
893 | static inline gfn_t |
894 | hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) | |
887c08ac | 895 | { |
d19a748b TY |
896 | gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT; |
897 | ||
898 | return slot->base_gfn + gfn_offset; | |
887c08ac XG |
899 | } |
900 | ||
1755fbcc AK |
901 | static inline gpa_t gfn_to_gpa(gfn_t gfn) |
902 | { | |
903 | return (gpa_t)gfn << PAGE_SHIFT; | |
904 | } | |
6aa8b732 | 905 | |
c30a358d JR |
906 | static inline gfn_t gpa_to_gfn(gpa_t gpa) |
907 | { | |
908 | return (gfn_t)(gpa >> PAGE_SHIFT); | |
909 | } | |
910 | ||
62c476c7 BAY |
911 | static inline hpa_t pfn_to_hpa(pfn_t pfn) |
912 | { | |
913 | return (hpa_t)pfn << PAGE_SHIFT; | |
914 | } | |
915 | ||
2f599714 | 916 | static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu) |
2f52d58c AK |
917 | { |
918 | set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests); | |
919 | } | |
920 | ||
ba1389b7 AK |
921 | enum kvm_stat_kind { |
922 | KVM_STAT_VM, | |
923 | KVM_STAT_VCPU, | |
924 | }; | |
925 | ||
417bc304 HB |
926 | struct kvm_stats_debugfs_item { |
927 | const char *name; | |
928 | int offset; | |
ba1389b7 | 929 | enum kvm_stat_kind kind; |
417bc304 HB |
930 | struct dentry *dentry; |
931 | }; | |
932 | extern struct kvm_stats_debugfs_item debugfs_entries[]; | |
76f7c879 | 933 | extern struct dentry *kvm_debugfs_dir; |
d4c9ff2d | 934 | |
36c1ed82 | 935 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
8ca40a70 | 936 | static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq) |
e930bffe | 937 | { |
8ca40a70 | 938 | if (unlikely(kvm->mmu_notifier_count)) |
e930bffe AA |
939 | return 1; |
940 | /* | |
a355aa54 PM |
941 | * Ensure the read of mmu_notifier_count happens before the read |
942 | * of mmu_notifier_seq. This interacts with the smp_wmb() in | |
943 | * mmu_notifier_invalidate_range_end to make sure that the caller | |
944 | * either sees the old (non-zero) value of mmu_notifier_count or | |
945 | * the new (incremented) value of mmu_notifier_seq. | |
946 | * PowerPC Book3s HV KVM calls this under a per-page lock | |
947 | * rather than under kvm->mmu_lock, for scalability, so | |
948 | * can't rely on kvm->mmu_lock to keep things ordered. | |
e930bffe | 949 | */ |
a355aa54 | 950 | smp_rmb(); |
8ca40a70 | 951 | if (kvm->mmu_notifier_seq != mmu_seq) |
e930bffe AA |
952 | return 1; |
953 | return 0; | |
954 | } | |
955 | #endif | |
956 | ||
9900b4b4 | 957 | #ifdef KVM_CAP_IRQ_ROUTING |
399ec807 AK |
958 | |
959 | #define KVM_MAX_IRQ_ROUTES 1024 | |
960 | ||
961 | int kvm_setup_default_irq_routing(struct kvm *kvm); | |
962 | int kvm_set_irq_routing(struct kvm *kvm, | |
963 | const struct kvm_irq_routing_entry *entries, | |
964 | unsigned nr, | |
965 | unsigned flags); | |
966 | void kvm_free_irq_routing(struct kvm *kvm); | |
967 | ||
07975ad3 JK |
968 | int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi); |
969 | ||
399ec807 AK |
970 | #else |
971 | ||
972 | static inline void kvm_free_irq_routing(struct kvm *kvm) {} | |
973 | ||
974 | #endif | |
975 | ||
721eecbf GH |
976 | #ifdef CONFIG_HAVE_KVM_EVENTFD |
977 | ||
d34e6b17 | 978 | void kvm_eventfd_init(struct kvm *kvm); |
914daba8 AG |
979 | int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args); |
980 | ||
981 | #ifdef CONFIG_HAVE_KVM_IRQCHIP | |
d4db2935 | 982 | int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args); |
721eecbf | 983 | void kvm_irqfd_release(struct kvm *kvm); |
bd2b53b2 | 984 | void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *); |
914daba8 AG |
985 | #else |
986 | static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) | |
987 | { | |
988 | return -EINVAL; | |
989 | } | |
990 | ||
991 | static inline void kvm_irqfd_release(struct kvm *kvm) {} | |
992 | #endif | |
721eecbf GH |
993 | |
994 | #else | |
995 | ||
d34e6b17 | 996 | static inline void kvm_eventfd_init(struct kvm *kvm) {} |
bd2b53b2 | 997 | |
d4db2935 | 998 | static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args) |
721eecbf GH |
999 | { |
1000 | return -EINVAL; | |
1001 | } | |
1002 | ||
1003 | static inline void kvm_irqfd_release(struct kvm *kvm) {} | |
bd2b53b2 | 1004 | |
27923eb1 | 1005 | #ifdef CONFIG_HAVE_KVM_IRQCHIP |
bd2b53b2 MT |
1006 | static inline void kvm_irq_routing_update(struct kvm *kvm, |
1007 | struct kvm_irq_routing_table *irq_rt) | |
1008 | { | |
1009 | rcu_assign_pointer(kvm->irq_routing, irq_rt); | |
1010 | } | |
27923eb1 | 1011 | #endif |
bd2b53b2 | 1012 | |
d34e6b17 GH |
1013 | static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) |
1014 | { | |
1015 | return -ENOSYS; | |
1016 | } | |
721eecbf GH |
1017 | |
1018 | #endif /* CONFIG_HAVE_KVM_EVENTFD */ | |
1019 | ||
73880c80 | 1020 | #ifdef CONFIG_KVM_APIC_ARCHITECTURE |
c5af89b6 GN |
1021 | static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) |
1022 | { | |
d3efc8ef | 1023 | return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id; |
c5af89b6 | 1024 | } |
3e515705 AK |
1025 | |
1026 | bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu); | |
1027 | ||
1028 | #else | |
1029 | ||
1030 | static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; } | |
1031 | ||
6aa8b732 | 1032 | #endif |
bfd99ff5 AK |
1033 | |
1034 | #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT | |
1035 | ||
1036 | long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl, | |
1037 | unsigned long arg); | |
1038 | ||
1039 | #else | |
1040 | ||
1041 | static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl, | |
1042 | unsigned long arg) | |
1043 | { | |
1044 | return -ENOTTY; | |
1045 | } | |
1046 | ||
73880c80 | 1047 | #endif |
bfd99ff5 | 1048 | |
a8eeb04a AK |
1049 | static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu) |
1050 | { | |
1051 | set_bit(req, &vcpu->requests); | |
1052 | } | |
1053 | ||
a8eeb04a AK |
1054 | static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) |
1055 | { | |
0719837c AK |
1056 | if (test_bit(req, &vcpu->requests)) { |
1057 | clear_bit(req, &vcpu->requests); | |
1058 | return true; | |
1059 | } else { | |
1060 | return false; | |
1061 | } | |
a8eeb04a AK |
1062 | } |
1063 | ||
8b415dcd GL |
1064 | extern bool kvm_rebooting; |
1065 | ||
4c088493 R |
1066 | #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT |
1067 | ||
1068 | static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) | |
1069 | { | |
1070 | vcpu->spin_loop.in_spin_loop = val; | |
1071 | } | |
1072 | static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) | |
1073 | { | |
1074 | vcpu->spin_loop.dy_eligible = val; | |
1075 | } | |
1076 | ||
1077 | #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ | |
1078 | ||
1079 | static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val) | |
1080 | { | |
1081 | } | |
1082 | ||
1083 | static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val) | |
1084 | { | |
1085 | } | |
1086 | ||
06e48c51 R |
1087 | static inline bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu) |
1088 | { | |
1089 | return true; | |
1090 | } | |
1091 | ||
4c088493 | 1092 | #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */ |
f445f11e KH |
1093 | #else |
1094 | static inline void __guest_enter(void) { return; } | |
1095 | static inline void __guest_exit(void) { return; } | |
1096 | #endif /* IS_ENABLED(CONFIG_KVM) */ | |
bfd99ff5 | 1097 | #endif |