kvm/ppc/mpic: adapt to kernel style and environment
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / linux / kvm_host.h
CommitLineData
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 68static 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 */
78static 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. */
84static 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
92static 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 99static 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
3d81bc7e 129#define KVM_REQ_SCAN_IOAPIC 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 134struct kvm;
6aa8b732 135struct kvm_vcpu;
c16f862d 136extern struct kmem_cache *kvm_vcpu_cache;
6aa8b732 137
fc1b7492
GL
138extern raw_spinlock_t kvm_lock;
139extern struct list_head vm_list;
140
743eeb0b
SL
141struct 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
149struct kvm_io_bus {
150 int dev_count;
a1300716 151 struct kvm_io_range range[];
2eeb2e94
GH
152};
153
e93f8a0f
MT
154enum 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
161int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
162 int len, const void *val);
163int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
bda9020e 164 void *val);
743eeb0b
SL
165int 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
167int 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
171struct 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
184void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
185void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
186int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
187 struct kvm_arch_async_pf *arch);
344d9588 188int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
af585b92
GN
189#endif
190
6b7e2d09
XG
191enum {
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 */
202struct kvm_mmio_fragment {
203 gpa_t gpa;
204 void *data;
205 unsigned len;
206};
207
d17fbbf7
ZX
208struct 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
265static 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
276struct 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
286static 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
291struct kvm_kernel_irq_routing_entry {
292 u32 gsi;
5116d8f6 293 u32 type;
4925663a 294 int (*set)(struct kvm_kernel_irq_routing_entry *e,
aa2fbe6d
YZ
295 struct kvm *kvm, int irq_source_id, int level,
296 bool line_status);
399ec807
AK
297 union {
298 struct {
299 unsigned irqchip;
300 unsigned pin;
301 } irqchip;
79950e10 302 struct msi_msg msi;
399ec807 303 };
46e624b9
GN
304 struct hlist_node link;
305};
306
a725d56a 307#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3e71f88b 308
46e624b9 309struct kvm_irq_routing_table {
8175e5b7 310 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
46e624b9
GN
311 struct kvm_kernel_irq_routing_entry *rt_entries;
312 u32 nr_rt_entries;
313 /*
314 * Array indexed by gsi. Each entry contains list of irq chips
315 * the gsi is connected to.
316 */
317 struct hlist_head map[0];
399ec807
AK
318};
319
3e71f88b
GN
320#else
321
322struct kvm_irq_routing_table {};
323
324#endif
325
0743247f
AW
326#ifndef KVM_PRIVATE_MEM_SLOTS
327#define KVM_PRIVATE_MEM_SLOTS 0
328#endif
329
93a5cef0 330#ifndef KVM_MEM_SLOTS_NUM
bbacc0c1 331#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
93a5cef0
XG
332#endif
333
bf3e05bc
XG
334/*
335 * Note:
336 * memslots are not sorted by id anymore, please use id_to_memslot()
337 * to get the memslot by its id.
338 */
46a26bf5 339struct kvm_memslots {
49c7754c 340 u64 generation;
93a5cef0 341 struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
f85e2cb5 342 /* The mapping table from slot id to the index in memslots[]. */
1e702d9a 343 short id_to_index[KVM_MEM_SLOTS_NUM];
46a26bf5
MT
344};
345
6aa8b732 346struct kvm {
aaee2c94 347 spinlock_t mmu_lock;
79fac95e 348 struct mutex slots_lock;
6d4e4c4f 349 struct mm_struct *mm; /* userspace tied to this vm */
46a26bf5 350 struct kvm_memslots *memslots;
bc6678a3 351 struct srcu_struct srcu;
73880c80
GN
352#ifdef CONFIG_KVM_APIC_ARCHITECTURE
353 u32 bsp_vcpu_id;
73880c80 354#endif
fb3f0f51 355 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
73880c80 356 atomic_t online_vcpus;
217ece61 357 int last_boosted_vcpu;
133de902 358 struct list_head vm_list;
60eead79 359 struct mutex lock;
e93f8a0f 360 struct kvm_io_bus *buses[KVM_NR_BUSES];
721eecbf
GH
361#ifdef CONFIG_HAVE_KVM_EVENTFD
362 struct {
363 spinlock_t lock;
364 struct list_head items;
7a84428a
AW
365 struct list_head resampler_list;
366 struct mutex resampler_lock;
721eecbf 367 } irqfds;
d34e6b17 368 struct list_head ioeventfds;
721eecbf 369#endif
ba1389b7 370 struct kvm_vm_stat stat;
d69fb81f 371 struct kvm_arch arch;
d39f13b0 372 atomic_t users_count;
5f94c174 373#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
5f94c174 374 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
2b3c246a
SL
375 spinlock_t ring_lock;
376 struct list_head coalesced_zones;
5f94c174 377#endif
e930bffe 378
60eead79 379 struct mutex irq_lock;
75858a84 380#ifdef CONFIG_HAVE_KVM_IRQCHIP
bd2b53b2
MT
381 /*
382 * Update side is protected by irq_lock and,
383 * if configured, irqfds.lock.
384 */
4b6a2872 385 struct kvm_irq_routing_table __rcu *irq_routing;
75858a84 386 struct hlist_head mask_notifier_list;
136bdfee 387 struct hlist_head irq_ack_notifier_list;
75858a84
AK
388#endif
389
36c1ed82 390#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
e930bffe
AA
391 struct mmu_notifier mmu_notifier;
392 unsigned long mmu_notifier_seq;
393 long mmu_notifier_count;
394#endif
5c663a15 395 long tlbs_dirty;
6aa8b732
AK
396};
397
a737f256
CD
398#define kvm_err(fmt, ...) \
399 pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
400#define kvm_info(fmt, ...) \
401 pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
402#define kvm_debug(fmt, ...) \
403 pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
404#define kvm_pr_unimpl(fmt, ...) \
405 pr_err_ratelimited("kvm [%i]: " fmt, \
406 task_tgid_nr(current), ## __VA_ARGS__)
f0242478 407
a737f256
CD
408/* The guest did something we don't support. */
409#define vcpu_unimpl(vcpu, fmt, ...) \
410 kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
6aa8b732 411
988a2cae
GN
412static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
413{
414 smp_rmb();
415 return kvm->vcpus[i];
416}
417
418#define kvm_for_each_vcpu(idx, vcpup, kvm) \
b42fc3cb
JM
419 for (idx = 0; \
420 idx < atomic_read(&kvm->online_vcpus) && \
421 (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
422 idx++)
988a2cae 423
be6ba0f0
XG
424#define kvm_for_each_memslot(memslot, slots) \
425 for (memslot = &slots->memslots[0]; \
bf3e05bc
XG
426 memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
427 memslot++)
be6ba0f0 428
fb3f0f51
RR
429int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
430void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
431
9fc77441 432int __must_check vcpu_load(struct kvm_vcpu *vcpu);
313a3dc7
CO
433void vcpu_put(struct kvm_vcpu *vcpu);
434
a725d56a 435#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
a0f155e9
CH
436int kvm_irqfd_init(void);
437void kvm_irqfd_exit(void);
438#else
439static inline int kvm_irqfd_init(void)
440{
441 return 0;
442}
443
444static inline void kvm_irqfd_exit(void)
445{
446}
447#endif
0ee75bea 448int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
c16f862d 449 struct module *module);
cb498ea2 450void kvm_exit(void);
6aa8b732 451
d39f13b0
IE
452void kvm_get_kvm(struct kvm *kvm);
453void kvm_put_kvm(struct kvm *kvm);
116c14c0
AW
454void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
455 u64 last_generation);
d39f13b0 456
90d83dc3
LJ
457static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
458{
459 return rcu_dereference_check(kvm->memslots,
460 srcu_read_lock_held(&kvm->srcu)
461 || lockdep_is_held(&kvm->slots_lock));
462}
463
28a37544
XG
464static inline struct kvm_memory_slot *
465id_to_memslot(struct kvm_memslots *slots, int id)
466{
f85e2cb5
XG
467 int index = slots->id_to_index[id];
468 struct kvm_memory_slot *slot;
bf3e05bc 469
f85e2cb5 470 slot = &slots->memslots[index];
bf3e05bc 471
f85e2cb5
XG
472 WARN_ON(slot->id != id);
473 return slot;
28a37544
XG
474}
475
74d0727c
TY
476/*
477 * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
478 * - create a new memory slot
479 * - delete an existing memory slot
480 * - modify an existing memory slot
481 * -- move it in the guest physical memory space
482 * -- just change its flags
483 *
484 * Since flags can be changed by some of these operations, the following
485 * differentiation is the best we can do for __kvm_set_memory_region():
486 */
487enum kvm_mr_change {
488 KVM_MR_CREATE,
489 KVM_MR_DELETE,
490 KVM_MR_MOVE,
491 KVM_MR_FLAGS_ONLY,
492};
493
210c7c4d 494int kvm_set_memory_region(struct kvm *kvm,
47ae31e2 495 struct kvm_userspace_memory_region *mem);
f78e0e2e 496int __kvm_set_memory_region(struct kvm *kvm,
47ae31e2 497 struct kvm_userspace_memory_region *mem);
db3fe4eb
TY
498void kvm_arch_free_memslot(struct kvm_memory_slot *free,
499 struct kvm_memory_slot *dont);
500int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages);
f7784b8e
MT
501int kvm_arch_prepare_memory_region(struct kvm *kvm,
502 struct kvm_memory_slot *memslot,
7b6195a9
TY
503 struct kvm_userspace_memory_region *mem,
504 enum kvm_mr_change change);
f7784b8e 505void kvm_arch_commit_memory_region(struct kvm *kvm,
0de10343 506 struct kvm_userspace_memory_region *mem,
8482644a
TY
507 const struct kvm_memory_slot *old,
508 enum kvm_mr_change change);
db3fe4eb 509bool kvm_largepages_enabled(void);
54dee993 510void kvm_disable_largepages(void);
2df72e9b
MT
511/* flush all memory translations */
512void kvm_arch_flush_shadow_all(struct kvm *kvm);
513/* flush memory translations pointing to 'slot' */
514void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
515 struct kvm_memory_slot *slot);
a983fb23 516
48987781
XG
517int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
518 int nr_pages);
519
954bbbc2 520struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
05da4558 521unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
4d8b81ab 522unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
b4231d61
IE
523void kvm_release_page_clean(struct page *page);
524void kvm_release_page_dirty(struct page *page);
35149e21
AL
525void kvm_set_page_dirty(struct page *page);
526void kvm_set_page_accessed(struct page *page);
527
365fb3fd 528pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
612819c3
MT
529pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
530 bool write_fault, bool *writable);
35149e21 531pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
612819c3
MT
532pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
533 bool *writable);
d5661048 534pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
037d92dc
XG
535pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
536
32cad84f 537void kvm_release_pfn_dirty(pfn_t pfn);
35149e21
AL
538void kvm_release_pfn_clean(pfn_t pfn);
539void kvm_set_pfn_dirty(pfn_t pfn);
540void kvm_set_pfn_accessed(pfn_t pfn);
541void kvm_get_pfn(pfn_t pfn);
542
195aefde
IE
543int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
544 int len);
7ec54588
MT
545int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
546 unsigned long len);
195aefde 547int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
e03b644f
GN
548int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
549 void *data, unsigned long len);
195aefde
IE
550int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
551 int offset, int len);
552int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
553 unsigned long len);
49c7754c
GN
554int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
555 void *data, unsigned long len);
556int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
557 gpa_t gpa);
195aefde
IE
558int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
559int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
6aa8b732 560struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
e0d62c7f 561int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
8f0b1ab6 562unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
6aa8b732 563void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
49c7754c
GN
564void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
565 gfn_t gfn);
6aa8b732 566
8776e519 567void kvm_vcpu_block(struct kvm_vcpu *vcpu);
b6d33834 568void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
41628d33 569bool kvm_vcpu_yield_to(struct kvm_vcpu *target);
d255f4f2 570void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
6aa8b732 571void kvm_resched(struct kvm_vcpu *vcpu);
7702fd1f
AK
572void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
573void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
a4ee1ca4 574
d9e368d6 575void kvm_flush_remote_tlbs(struct kvm *kvm);
2e53d63a 576void kvm_reload_remote_mmus(struct kvm *kvm);
d828199e 577void kvm_make_mclock_inprogress_request(struct kvm *kvm);
3d81bc7e 578void kvm_make_scan_ioapic_request(struct kvm *kvm);
6aa8b732 579
043405e1
CO
580long kvm_arch_dev_ioctl(struct file *filp,
581 unsigned int ioctl, unsigned long arg);
313a3dc7
CO
582long kvm_arch_vcpu_ioctl(struct file *filp,
583 unsigned int ioctl, unsigned long arg);
5b1c1493 584int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
018d00d2
ZX
585
586int kvm_dev_ioctl_check_extension(long ext);
587
5bb064dc
ZX
588int kvm_get_dirty_log(struct kvm *kvm,
589 struct kvm_dirty_log *log, int *is_dirty);
590int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
591 struct kvm_dirty_log *log);
592
1fe779f8 593int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
47ae31e2 594 struct kvm_userspace_memory_region *mem);
aa2fbe6d
YZ
595int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
596 bool line_status);
1fe779f8
CO
597long kvm_arch_vm_ioctl(struct file *filp,
598 unsigned int ioctl, unsigned long arg);
313a3dc7 599
d0752060
HB
600int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
601int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
602
8b006791
ZX
603int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
604 struct kvm_translation *tr);
605
b6c7a5dc
HB
606int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
607int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
608int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
609 struct kvm_sregs *sregs);
610int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
611 struct kvm_sregs *sregs);
62d9f0db
MT
612int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
613 struct kvm_mp_state *mp_state);
614int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
615 struct kvm_mp_state *mp_state);
d0bfb940
JK
616int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
617 struct kvm_guest_debug *dbg);
b6c7a5dc
HB
618int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
619
f8c16bba
ZX
620int kvm_arch_init(void *opaque);
621void kvm_arch_exit(void);
043405e1 622
e9b11c17
ZX
623int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
624void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
625
626void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
627void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
628void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
629struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
26e5215f 630int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
42897d86 631int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
d40ccc62 632void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
e9b11c17 633
10474ae8 634int kvm_arch_hardware_enable(void *garbage);
e9b11c17
ZX
635void kvm_arch_hardware_disable(void *garbage);
636int kvm_arch_hardware_setup(void);
637void kvm_arch_hardware_unsetup(void);
638void kvm_arch_check_processor_compat(void *rtn);
1d737c8a 639int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
b6d33834 640int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
e9b11c17 641
d19a9cd2
ZX
642void kvm_free_physmem(struct kvm *kvm);
643
c1a7b32a
TY
644void *kvm_kvzalloc(unsigned long size);
645void kvm_kvfree(const void *addr);
646
d89f5eff
JK
647#ifndef __KVM_HAVE_ARCH_VM_ALLOC
648static inline struct kvm *kvm_arch_alloc_vm(void)
649{
650 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
651}
652
653static inline void kvm_arch_free_vm(struct kvm *kvm)
654{
655 kfree(kvm);
656}
657#endif
658
b6d33834
CD
659static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
660{
2246f8b5
AG
661#ifdef __KVM_HAVE_ARCH_WQP
662 return vcpu->arch.wqp;
663#else
b6d33834 664 return &vcpu->wq;
b6d33834 665#endif
2246f8b5 666}
b6d33834 667
e08b9637 668int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
d19a9cd2 669void kvm_arch_destroy_vm(struct kvm *kvm);
8a98f664 670void kvm_free_all_assigned_devices(struct kvm *kvm);
ad8ba2cd 671void kvm_arch_sync_events(struct kvm *kvm);
e9b11c17 672
3d80840d 673int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
5736199a 674void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
682c59a3 675
a2766325 676bool kvm_is_mmio_pfn(pfn_t pfn);
c77fb9dc 677
62c476c7
BAY
678struct kvm_irq_ack_notifier {
679 struct hlist_node link;
680 unsigned gsi;
681 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
682};
683
684struct kvm_assigned_dev_kernel {
685 struct kvm_irq_ack_notifier ack_notifier;
62c476c7
BAY
686 struct list_head list;
687 int assigned_dev_id;
ab9f4ecb 688 int host_segnr;
62c476c7
BAY
689 int host_busnr;
690 int host_devfn;
c1e01514 691 unsigned int entries_nr;
62c476c7 692 int host_irq;
defaf158 693 bool host_irq_disabled;
07700a94 694 bool pci_2_3;
c1e01514 695 struct msix_entry *host_msix_entries;
62c476c7 696 int guest_irq;
0645211c 697 struct msix_entry *guest_msix_entries;
4f906c19 698 unsigned long irq_requested_type;
5550af4d 699 int irq_source_id;
b653574a 700 int flags;
62c476c7
BAY
701 struct pci_dev *dev;
702 struct kvm *kvm;
0645211c 703 spinlock_t intx_lock;
cf9eeac4 704 spinlock_t intx_mask_lock;
1e001d49 705 char irq_name[32];
f8fcfd77 706 struct pci_saved_state *pci_saved_state;
62c476c7 707};
75858a84
AK
708
709struct kvm_irq_mask_notifier {
710 void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
711 int irq;
712 struct hlist_node link;
713};
714
715void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
716 struct kvm_irq_mask_notifier *kimn);
717void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
718 struct kvm_irq_mask_notifier *kimn);
4a994358
GN
719void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
720 bool mask);
75858a84 721
aa2fbe6d
YZ
722int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
723 bool line_status);
01f21880 724int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level);
bd2b53b2 725int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
aa2fbe6d 726 int irq_source_id, int level, bool line_status);
c7c9c56c 727bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
44882eed 728void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
3de42dc0
XZ
729void kvm_register_irq_ack_notifier(struct kvm *kvm,
730 struct kvm_irq_ack_notifier *kian);
fa40a821
MT
731void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
732 struct kvm_irq_ack_notifier *kian);
5550af4d
SY
733int kvm_request_irq_source_id(struct kvm *kvm);
734void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
62c476c7 735
522c68c4
SY
736/* For vcpu->arch.iommu_flags */
737#define KVM_IOMMU_CACHE_COHERENCY 0x1
738
19de40a8 739#ifdef CONFIG_IOMMU_API
3ad26d81 740int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
32f6daad 741void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
260782bc 742int kvm_iommu_map_guest(struct kvm *kvm);
62c476c7 743int kvm_iommu_unmap_guest(struct kvm *kvm);
260782bc
WH
744int kvm_assign_device(struct kvm *kvm,
745 struct kvm_assigned_dev_kernel *assigned_dev);
0a920356
WH
746int kvm_deassign_device(struct kvm *kvm,
747 struct kvm_assigned_dev_kernel *assigned_dev);
19de40a8 748#else /* CONFIG_IOMMU_API */
62c476c7 749static inline int kvm_iommu_map_pages(struct kvm *kvm,
d7a79b6c 750 struct kvm_memory_slot *slot)
62c476c7
BAY
751{
752 return 0;
753}
754
32f6daad
AW
755static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
756 struct kvm_memory_slot *slot)
757{
758}
759
260782bc 760static inline int kvm_iommu_map_guest(struct kvm *kvm)
62c476c7
BAY
761{
762 return -ENODEV;
763}
764
765static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
766{
767 return 0;
768}
260782bc
WH
769
770static inline int kvm_assign_device(struct kvm *kvm,
771 struct kvm_assigned_dev_kernel *assigned_dev)
772{
773 return 0;
774}
0a920356
WH
775
776static inline int kvm_deassign_device(struct kvm *kvm,
777 struct kvm_assigned_dev_kernel *assigned_dev)
778{
779 return 0;
780}
19de40a8 781#endif /* CONFIG_IOMMU_API */
62c476c7 782
6a61671b 783static inline void __guest_enter(void)
d172fcd3 784{
b080935c
FW
785 /*
786 * This is running in ioctl context so we can avoid
787 * the call to vtime_account() with its unnecessary idle check.
788 */
c11f11fc 789 vtime_account_system(current);
d172fcd3 790 current->flags |= PF_VCPU;
c11f11fc
FW
791}
792
6a61671b 793static inline void __guest_exit(void)
c11f11fc
FW
794{
795 /*
796 * This is running in ioctl context so we can avoid
797 * the call to vtime_account() with its unnecessary idle check.
798 */
799 vtime_account_system(current);
800 current->flags &= ~PF_VCPU;
801}
802
6a61671b
FW
803#ifdef CONFIG_CONTEXT_TRACKING
804extern void guest_enter(void);
805extern void guest_exit(void);
806
807#else /* !CONFIG_CONTEXT_TRACKING */
808static inline void guest_enter(void)
809{
810 __guest_enter();
811}
812
813static inline void guest_exit(void)
814{
815 __guest_exit();
816}
817#endif /* !CONFIG_CONTEXT_TRACKING */
818
c11f11fc
FW
819static inline void kvm_guest_enter(void)
820{
821 unsigned long flags;
822
823 BUG_ON(preemptible());
824
825 local_irq_save(flags);
826 guest_enter();
827 local_irq_restore(flags);
828
8fa22068
GN
829 /* KVM does not hold any references to rcu protected data when it
830 * switches CPU into a guest mode. In fact switching to a guest mode
831 * is very similar to exiting to userspase from rcu point of view. In
832 * addition CPU may stay in a guest mode for quite a long time (up to
833 * one time slice). Lets treat guest mode as quiescent state, just like
834 * we do with user-mode execution.
835 */
836 rcu_virt_note_context_switch(smp_processor_id());
d172fcd3
LV
837}
838
839static inline void kvm_guest_exit(void)
840{
c11f11fc
FW
841 unsigned long flags;
842
843 local_irq_save(flags);
844 guest_exit();
845 local_irq_restore(flags);
d172fcd3
LV
846}
847
9d4cba7f
PM
848/*
849 * search_memslots() and __gfn_to_memslot() are here because they are
850 * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
851 * gfn_to_memslot() itself isn't here as an inline because that would
852 * bloat other code too much.
853 */
854static inline struct kvm_memory_slot *
855search_memslots(struct kvm_memslots *slots, gfn_t gfn)
856{
857 struct kvm_memory_slot *memslot;
858
859 kvm_for_each_memslot(memslot, slots)
860 if (gfn >= memslot->base_gfn &&
861 gfn < memslot->base_gfn + memslot->npages)
862 return memslot;
863
864 return NULL;
865}
866
867static inline struct kvm_memory_slot *
868__gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
869{
870 return search_memslots(slots, gfn);
871}
872
66a03505
GS
873static inline unsigned long
874__gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
875{
876 return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
877}
878
0ee8dcb8
XG
879static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
880{
881 return gfn_to_memslot(kvm, gfn)->id;
882}
883
fb03cb6f
TY
884static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
885{
886 /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
887 return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
888 (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
889}
890
d19a748b
TY
891static inline gfn_t
892hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
887c08ac 893{
d19a748b
TY
894 gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
895
896 return slot->base_gfn + gfn_offset;
887c08ac
XG
897}
898
1755fbcc
AK
899static inline gpa_t gfn_to_gpa(gfn_t gfn)
900{
901 return (gpa_t)gfn << PAGE_SHIFT;
902}
6aa8b732 903
c30a358d
JR
904static inline gfn_t gpa_to_gfn(gpa_t gpa)
905{
906 return (gfn_t)(gpa >> PAGE_SHIFT);
907}
908
62c476c7
BAY
909static inline hpa_t pfn_to_hpa(pfn_t pfn)
910{
911 return (hpa_t)pfn << PAGE_SHIFT;
912}
913
2f599714 914static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
2f52d58c
AK
915{
916 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
917}
918
ba1389b7
AK
919enum kvm_stat_kind {
920 KVM_STAT_VM,
921 KVM_STAT_VCPU,
922};
923
417bc304
HB
924struct kvm_stats_debugfs_item {
925 const char *name;
926 int offset;
ba1389b7 927 enum kvm_stat_kind kind;
417bc304
HB
928 struct dentry *dentry;
929};
930extern struct kvm_stats_debugfs_item debugfs_entries[];
76f7c879 931extern struct dentry *kvm_debugfs_dir;
d4c9ff2d 932
36c1ed82 933#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
8ca40a70 934static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
e930bffe 935{
8ca40a70 936 if (unlikely(kvm->mmu_notifier_count))
e930bffe
AA
937 return 1;
938 /*
a355aa54
PM
939 * Ensure the read of mmu_notifier_count happens before the read
940 * of mmu_notifier_seq. This interacts with the smp_wmb() in
941 * mmu_notifier_invalidate_range_end to make sure that the caller
942 * either sees the old (non-zero) value of mmu_notifier_count or
943 * the new (incremented) value of mmu_notifier_seq.
944 * PowerPC Book3s HV KVM calls this under a per-page lock
945 * rather than under kvm->mmu_lock, for scalability, so
946 * can't rely on kvm->mmu_lock to keep things ordered.
e930bffe 947 */
a355aa54 948 smp_rmb();
8ca40a70 949 if (kvm->mmu_notifier_seq != mmu_seq)
e930bffe
AA
950 return 1;
951 return 0;
952}
953#endif
954
a725d56a 955#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
399ec807
AK
956
957#define KVM_MAX_IRQ_ROUTES 1024
958
959int kvm_setup_default_irq_routing(struct kvm *kvm);
960int kvm_set_irq_routing(struct kvm *kvm,
961 const struct kvm_irq_routing_entry *entries,
962 unsigned nr,
963 unsigned flags);
e8cde093
AG
964int kvm_set_routing_entry(struct kvm_irq_routing_table *rt,
965 struct kvm_kernel_irq_routing_entry *e,
966 const struct kvm_irq_routing_entry *ue);
399ec807
AK
967void kvm_free_irq_routing(struct kvm *kvm);
968
07975ad3
JK
969int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
970
399ec807
AK
971#else
972
973static inline void kvm_free_irq_routing(struct kvm *kvm) {}
974
975#endif
976
721eecbf
GH
977#ifdef CONFIG_HAVE_KVM_EVENTFD
978
d34e6b17 979void kvm_eventfd_init(struct kvm *kvm);
914daba8
AG
980int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
981
982#ifdef CONFIG_HAVE_KVM_IRQCHIP
d4db2935 983int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
721eecbf 984void kvm_irqfd_release(struct kvm *kvm);
bd2b53b2 985void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *);
914daba8
AG
986#else
987static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
988{
989 return -EINVAL;
990}
991
992static inline void kvm_irqfd_release(struct kvm *kvm) {}
993#endif
721eecbf
GH
994
995#else
996
d34e6b17 997static inline void kvm_eventfd_init(struct kvm *kvm) {}
bd2b53b2 998
d4db2935 999static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
721eecbf
GH
1000{
1001 return -EINVAL;
1002}
1003
1004static inline void kvm_irqfd_release(struct kvm *kvm) {}
bd2b53b2 1005
27923eb1 1006#ifdef CONFIG_HAVE_KVM_IRQCHIP
bd2b53b2
MT
1007static inline void kvm_irq_routing_update(struct kvm *kvm,
1008 struct kvm_irq_routing_table *irq_rt)
1009{
1010 rcu_assign_pointer(kvm->irq_routing, irq_rt);
1011}
27923eb1 1012#endif
bd2b53b2 1013
d34e6b17
GH
1014static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
1015{
1016 return -ENOSYS;
1017}
721eecbf
GH
1018
1019#endif /* CONFIG_HAVE_KVM_EVENTFD */
1020
73880c80 1021#ifdef CONFIG_KVM_APIC_ARCHITECTURE
c5af89b6
GN
1022static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
1023{
d3efc8ef 1024 return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
c5af89b6 1025}
3e515705
AK
1026
1027bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
1028
1029#else
1030
1031static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
1032
6aa8b732 1033#endif
bfd99ff5
AK
1034
1035#ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
1036
1037long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
1038 unsigned long arg);
1039
1040#else
1041
1042static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
1043 unsigned long arg)
1044{
1045 return -ENOTTY;
1046}
1047
73880c80 1048#endif
bfd99ff5 1049
a8eeb04a
AK
1050static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
1051{
1052 set_bit(req, &vcpu->requests);
1053}
1054
a8eeb04a
AK
1055static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
1056{
0719837c
AK
1057 if (test_bit(req, &vcpu->requests)) {
1058 clear_bit(req, &vcpu->requests);
1059 return true;
1060 } else {
1061 return false;
1062 }
a8eeb04a
AK
1063}
1064
8b415dcd
GL
1065extern bool kvm_rebooting;
1066
852b6d57
SW
1067struct kvm_device_ops;
1068
1069struct kvm_device {
1070 struct kvm_device_ops *ops;
1071 struct kvm *kvm;
1072 atomic_t users;
1073 void *private;
1074};
1075
1076/* create, destroy, and name are mandatory */
1077struct kvm_device_ops {
1078 const char *name;
1079 int (*create)(struct kvm_device *dev, u32 type);
1080
1081 /*
1082 * Destroy is responsible for freeing dev.
1083 *
1084 * Destroy may be called before or after destructors are called
1085 * on emulated I/O regions, depending on whether a reference is
1086 * held by a vcpu or other kvm component that gets destroyed
1087 * after the emulated I/O.
1088 */
1089 void (*destroy)(struct kvm_device *dev);
1090
1091 int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1092 int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1093 int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1094 long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
1095 unsigned long arg);
1096};
1097
1098void kvm_device_get(struct kvm_device *dev);
1099void kvm_device_put(struct kvm_device *dev);
1100struct kvm_device *kvm_device_from_filp(struct file *filp);
1101
4c088493
R
1102#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1103
1104static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1105{
1106 vcpu->spin_loop.in_spin_loop = val;
1107}
1108static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1109{
1110 vcpu->spin_loop.dy_eligible = val;
1111}
1112
1113#else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1114
1115static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1116{
1117}
1118
1119static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1120{
1121}
1122
06e48c51
R
1123static inline bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
1124{
1125 return true;
1126}
1127
4c088493 1128#endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
f445f11e
KH
1129#else
1130static inline void __guest_enter(void) { return; }
1131static inline void __guest_exit(void) { return; }
1132#endif /* IS_ENABLED(CONFIG_KVM) */
bfd99ff5 1133#endif