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