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