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