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