]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - include/linux/kvm_host.h
Revert "KVM: allow host header to be included even for !CONFIG_KVM"
[karo-tx-linux.git] / include / linux / kvm_host.h
1 #ifndef __KVM_HOST_H
2 #define __KVM_HOST_H
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>
10 #include <linux/hardirq.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/spinlock.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/bug.h>
17 #include <linux/mm.h>
18 #include <linux/mmu_notifier.h>
19 #include <linux/preempt.h>
20 #include <linux/msi.h>
21 #include <linux/slab.h>
22 #include <linux/rcupdate.h>
23 #include <linux/ratelimit.h>
24 #include <linux/err.h>
25 #include <linux/irqflags.h>
26 #include <asm/signal.h>
27
28 #include <linux/kvm.h>
29 #include <linux/kvm_para.h>
30
31 #include <linux/kvm_types.h>
32
33 #include <asm/kvm_host.h>
34
35 #ifndef KVM_MMIO_SIZE
36 #define KVM_MMIO_SIZE 8
37 #endif
38
39 /*
40  * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
41  * in kvm, other bits are visible for userspace which are defined in
42  * include/linux/kvm_h.
43  */
44 #define KVM_MEMSLOT_INVALID     (1UL << 16)
45
46 /* Two fragments for cross MMIO pages. */
47 #define KVM_MAX_MMIO_FRAGMENTS  2
48
49 /*
50  * For the normal pfn, the highest 12 bits should be zero,
51  * so we can mask bit 62 ~ bit 52  to indicate the error pfn,
52  * mask bit 63 to indicate the noslot pfn.
53  */
54 #define KVM_PFN_ERR_MASK        (0x7ffULL << 52)
55 #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
56 #define KVM_PFN_NOSLOT          (0x1ULL << 63)
57
58 #define KVM_PFN_ERR_FAULT       (KVM_PFN_ERR_MASK)
59 #define KVM_PFN_ERR_HWPOISON    (KVM_PFN_ERR_MASK + 1)
60 #define KVM_PFN_ERR_RO_FAULT    (KVM_PFN_ERR_MASK + 2)
61
62 /*
63  * error pfns indicate that the gfn is in slot but faild to
64  * translate it to pfn on host.
65  */
66 static inline bool is_error_pfn(pfn_t pfn)
67 {
68         return !!(pfn & KVM_PFN_ERR_MASK);
69 }
70
71 /*
72  * error_noslot pfns indicate that the gfn can not be
73  * translated to pfn - it is not in slot or failed to
74  * translate it to pfn.
75  */
76 static inline bool is_error_noslot_pfn(pfn_t pfn)
77 {
78         return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
79 }
80
81 /* noslot pfn indicates that the gfn is not in slot. */
82 static inline bool is_noslot_pfn(pfn_t pfn)
83 {
84         return pfn == KVM_PFN_NOSLOT;
85 }
86
87 #define KVM_HVA_ERR_BAD         (PAGE_OFFSET)
88 #define KVM_HVA_ERR_RO_BAD      (PAGE_OFFSET + PAGE_SIZE)
89
90 static inline bool kvm_is_error_hva(unsigned long addr)
91 {
92         return addr >= PAGE_OFFSET;
93 }
94
95 #define KVM_ERR_PTR_BAD_PAGE    (ERR_PTR(-ENOENT))
96
97 static inline bool is_error_page(struct page *page)
98 {
99         return IS_ERR(page);
100 }
101
102 /*
103  * vcpu->requests bit members
104  */
105 #define KVM_REQ_TLB_FLUSH          0
106 #define KVM_REQ_MIGRATE_TIMER      1
107 #define KVM_REQ_REPORT_TPR_ACCESS  2
108 #define KVM_REQ_MMU_RELOAD         3
109 #define KVM_REQ_TRIPLE_FAULT       4
110 #define KVM_REQ_PENDING_TIMER      5
111 #define KVM_REQ_UNHALT             6
112 #define KVM_REQ_MMU_SYNC           7
113 #define KVM_REQ_CLOCK_UPDATE       8
114 #define KVM_REQ_KICK               9
115 #define KVM_REQ_DEACTIVATE_FPU    10
116 #define KVM_REQ_EVENT             11
117 #define KVM_REQ_APF_HALT          12
118 #define KVM_REQ_STEAL_UPDATE      13
119 #define KVM_REQ_NMI               14
120 #define KVM_REQ_IMMEDIATE_EXIT    15
121 #define KVM_REQ_PMU               16
122 #define KVM_REQ_PMI               17
123 #define KVM_REQ_WATCHDOG          18
124 #define KVM_REQ_MASTERCLOCK_UPDATE 19
125 #define KVM_REQ_MCLOCK_INPROGRESS 20
126 #define KVM_REQ_EPR_EXIT          21
127 #define KVM_REQ_EOIBITMAP         22
128
129 #define KVM_USERSPACE_IRQ_SOURCE_ID             0
130 #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID        1
131
132 struct kvm;
133 struct kvm_vcpu;
134 extern struct kmem_cache *kvm_vcpu_cache;
135
136 struct kvm_io_range {
137         gpa_t addr;
138         int len;
139         struct kvm_io_device *dev;
140 };
141
142 #define NR_IOBUS_DEVS 1000
143
144 struct kvm_io_bus {
145         int                   dev_count;
146         struct kvm_io_range range[];
147 };
148
149 enum kvm_bus {
150         KVM_MMIO_BUS,
151         KVM_PIO_BUS,
152         KVM_VIRTIO_CCW_NOTIFY_BUS,
153         KVM_NR_BUSES
154 };
155
156 int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
157                      int len, const void *val);
158 int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
159                     void *val);
160 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
161                             int len, struct kvm_io_device *dev);
162 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
163                               struct kvm_io_device *dev);
164
165 #ifdef CONFIG_KVM_ASYNC_PF
166 struct kvm_async_pf {
167         struct work_struct work;
168         struct list_head link;
169         struct list_head queue;
170         struct kvm_vcpu *vcpu;
171         struct mm_struct *mm;
172         gva_t gva;
173         unsigned long addr;
174         struct kvm_arch_async_pf arch;
175         struct page *page;
176         bool done;
177 };
178
179 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
180 void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
181 int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
182                        struct kvm_arch_async_pf *arch);
183 int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
184 #endif
185
186 enum {
187         OUTSIDE_GUEST_MODE,
188         IN_GUEST_MODE,
189         EXITING_GUEST_MODE,
190         READING_SHADOW_PAGE_TABLES,
191 };
192
193 /*
194  * Sometimes a large or cross-page mmio needs to be broken up into separate
195  * exits for userspace servicing.
196  */
197 struct kvm_mmio_fragment {
198         gpa_t gpa;
199         void *data;
200         unsigned len;
201 };
202
203 struct kvm_vcpu {
204         struct kvm *kvm;
205 #ifdef CONFIG_PREEMPT_NOTIFIERS
206         struct preempt_notifier preempt_notifier;
207 #endif
208         int cpu;
209         int vcpu_id;
210         int srcu_idx;
211         int mode;
212         unsigned long requests;
213         unsigned long guest_debug;
214
215         struct mutex mutex;
216         struct kvm_run *run;
217
218         int fpu_active;
219         int guest_fpu_loaded, guest_xcr0_loaded;
220         wait_queue_head_t wq;
221         struct pid *pid;
222         int sigset_active;
223         sigset_t sigset;
224         struct kvm_vcpu_stat stat;
225
226 #ifdef CONFIG_HAS_IOMEM
227         int mmio_needed;
228         int mmio_read_completed;
229         int mmio_is_write;
230         int mmio_cur_fragment;
231         int mmio_nr_fragments;
232         struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS];
233 #endif
234
235 #ifdef CONFIG_KVM_ASYNC_PF
236         struct {
237                 u32 queued;
238                 struct list_head queue;
239                 struct list_head done;
240                 spinlock_t lock;
241         } async_pf;
242 #endif
243
244 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
245         /*
246          * Cpu relax intercept or pause loop exit optimization
247          * in_spin_loop: set when a vcpu does a pause loop exit
248          *  or cpu relax intercepted.
249          * dy_eligible: indicates whether vcpu is eligible for directed yield.
250          */
251         struct {
252                 bool in_spin_loop;
253                 bool dy_eligible;
254         } spin_loop;
255 #endif
256         bool preempted;
257         struct kvm_vcpu_arch arch;
258 };
259
260 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
261 {
262         return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
263 }
264
265 /*
266  * Some of the bitops functions do not support too long bitmaps.
267  * This number must be determined not to exceed such limits.
268  */
269 #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
270
271 struct kvm_memory_slot {
272         gfn_t base_gfn;
273         unsigned long npages;
274         unsigned long *dirty_bitmap;
275         struct kvm_arch_memory_slot arch;
276         unsigned long userspace_addr;
277         u32 flags;
278         short id;
279 };
280
281 static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
282 {
283         return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
284 }
285
286 struct kvm_kernel_irq_routing_entry {
287         u32 gsi;
288         u32 type;
289         int (*set)(struct kvm_kernel_irq_routing_entry *e,
290                    struct kvm *kvm, int irq_source_id, int level);
291         union {
292                 struct {
293                         unsigned irqchip;
294                         unsigned pin;
295                 } irqchip;
296                 struct msi_msg msi;
297         };
298         struct hlist_node link;
299 };
300
301 #ifdef __KVM_HAVE_IOAPIC
302
303 struct kvm_irq_routing_table {
304         int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS];
305         struct kvm_kernel_irq_routing_entry *rt_entries;
306         u32 nr_rt_entries;
307         /*
308          * Array indexed by gsi. Each entry contains list of irq chips
309          * the gsi is connected to.
310          */
311         struct hlist_head map[0];
312 };
313
314 #else
315
316 struct kvm_irq_routing_table {};
317
318 #endif
319
320 #ifndef KVM_PRIVATE_MEM_SLOTS
321 #define KVM_PRIVATE_MEM_SLOTS 0
322 #endif
323
324 #ifndef KVM_MEM_SLOTS_NUM
325 #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
326 #endif
327
328 /*
329  * Note:
330  * memslots are not sorted by id anymore, please use id_to_memslot()
331  * to get the memslot by its id.
332  */
333 struct kvm_memslots {
334         u64 generation;
335         struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
336         /* The mapping table from slot id to the index in memslots[]. */
337         short id_to_index[KVM_MEM_SLOTS_NUM];
338 };
339
340 struct kvm {
341         spinlock_t mmu_lock;
342         struct mutex slots_lock;
343         struct mm_struct *mm; /* userspace tied to this vm */
344         struct kvm_memslots *memslots;
345         struct srcu_struct srcu;
346 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
347         u32 bsp_vcpu_id;
348 #endif
349         struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
350         atomic_t online_vcpus;
351         int last_boosted_vcpu;
352         struct list_head vm_list;
353         struct mutex lock;
354         struct kvm_io_bus *buses[KVM_NR_BUSES];
355 #ifdef CONFIG_HAVE_KVM_EVENTFD
356         struct {
357                 spinlock_t        lock;
358                 struct list_head  items;
359                 struct list_head  resampler_list;
360                 struct mutex      resampler_lock;
361         } irqfds;
362         struct list_head ioeventfds;
363 #endif
364         struct kvm_vm_stat stat;
365         struct kvm_arch arch;
366         atomic_t users_count;
367 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
368         struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
369         spinlock_t ring_lock;
370         struct list_head coalesced_zones;
371 #endif
372
373         struct mutex irq_lock;
374 #ifdef CONFIG_HAVE_KVM_IRQCHIP
375         /*
376          * Update side is protected by irq_lock and,
377          * if configured, irqfds.lock.
378          */
379         struct kvm_irq_routing_table __rcu *irq_routing;
380         struct hlist_head mask_notifier_list;
381         struct hlist_head irq_ack_notifier_list;
382 #endif
383
384 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
385         struct mmu_notifier mmu_notifier;
386         unsigned long mmu_notifier_seq;
387         long mmu_notifier_count;
388 #endif
389         long tlbs_dirty;
390 };
391
392 #define kvm_err(fmt, ...) \
393         pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
394 #define kvm_info(fmt, ...) \
395         pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
396 #define kvm_debug(fmt, ...) \
397         pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
398 #define kvm_pr_unimpl(fmt, ...) \
399         pr_err_ratelimited("kvm [%i]: " fmt, \
400                            task_tgid_nr(current), ## __VA_ARGS__)
401
402 /* The guest did something we don't support. */
403 #define vcpu_unimpl(vcpu, fmt, ...)                                     \
404         kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
405
406 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
407 {
408         smp_rmb();
409         return kvm->vcpus[i];
410 }
411
412 #define kvm_for_each_vcpu(idx, vcpup, kvm) \
413         for (idx = 0; \
414              idx < atomic_read(&kvm->online_vcpus) && \
415              (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
416              idx++)
417
418 #define kvm_for_each_memslot(memslot, slots)    \
419         for (memslot = &slots->memslots[0];     \
420               memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
421                 memslot++)
422
423 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
424 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
425
426 int __must_check vcpu_load(struct kvm_vcpu *vcpu);
427 void vcpu_put(struct kvm_vcpu *vcpu);
428
429 #ifdef __KVM_HAVE_IOAPIC
430 int kvm_irqfd_init(void);
431 void kvm_irqfd_exit(void);
432 #else
433 static inline int kvm_irqfd_init(void)
434 {
435         return 0;
436 }
437
438 static inline void kvm_irqfd_exit(void)
439 {
440 }
441 #endif
442 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
443                   struct module *module);
444 void kvm_exit(void);
445
446 void kvm_get_kvm(struct kvm *kvm);
447 void kvm_put_kvm(struct kvm *kvm);
448 void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
449                      u64 last_generation);
450
451 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
452 {
453         return rcu_dereference_check(kvm->memslots,
454                         srcu_read_lock_held(&kvm->srcu)
455                         || lockdep_is_held(&kvm->slots_lock));
456 }
457
458 static inline struct kvm_memory_slot *
459 id_to_memslot(struct kvm_memslots *slots, int id)
460 {
461         int index = slots->id_to_index[id];
462         struct kvm_memory_slot *slot;
463
464         slot = &slots->memslots[index];
465
466         WARN_ON(slot->id != id);
467         return slot;
468 }
469
470 /*
471  * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
472  * - create a new memory slot
473  * - delete an existing memory slot
474  * - modify an existing memory slot
475  *   -- move it in the guest physical memory space
476  *   -- just change its flags
477  *
478  * Since flags can be changed by some of these operations, the following
479  * differentiation is the best we can do for __kvm_set_memory_region():
480  */
481 enum kvm_mr_change {
482         KVM_MR_CREATE,
483         KVM_MR_DELETE,
484         KVM_MR_MOVE,
485         KVM_MR_FLAGS_ONLY,
486 };
487
488 int kvm_set_memory_region(struct kvm *kvm,
489                           struct kvm_userspace_memory_region *mem);
490 int __kvm_set_memory_region(struct kvm *kvm,
491                             struct kvm_userspace_memory_region *mem);
492 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
493                            struct kvm_memory_slot *dont);
494 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages);
495 int kvm_arch_prepare_memory_region(struct kvm *kvm,
496                                 struct kvm_memory_slot *memslot,
497                                 struct kvm_userspace_memory_region *mem,
498                                 enum kvm_mr_change change);
499 void kvm_arch_commit_memory_region(struct kvm *kvm,
500                                 struct kvm_userspace_memory_region *mem,
501                                 const struct kvm_memory_slot *old,
502                                 enum kvm_mr_change change);
503 bool kvm_largepages_enabled(void);
504 void kvm_disable_largepages(void);
505 /* flush all memory translations */
506 void kvm_arch_flush_shadow_all(struct kvm *kvm);
507 /* flush memory translations pointing to 'slot' */
508 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
509                                    struct kvm_memory_slot *slot);
510
511 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
512                             int nr_pages);
513
514 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
515 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
516 unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
517 void kvm_release_page_clean(struct page *page);
518 void kvm_release_page_dirty(struct page *page);
519 void kvm_set_page_dirty(struct page *page);
520 void kvm_set_page_accessed(struct page *page);
521
522 pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
523 pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
524                        bool write_fault, bool *writable);
525 pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
526 pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
527                       bool *writable);
528 pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
529 pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
530
531 void kvm_release_pfn_dirty(pfn_t pfn);
532 void kvm_release_pfn_clean(pfn_t pfn);
533 void kvm_set_pfn_dirty(pfn_t pfn);
534 void kvm_set_pfn_accessed(pfn_t pfn);
535 void kvm_get_pfn(pfn_t pfn);
536
537 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
538                         int len);
539 int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
540                           unsigned long len);
541 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
542 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
543                            void *data, unsigned long len);
544 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
545                          int offset, int len);
546 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
547                     unsigned long len);
548 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
549                            void *data, unsigned long len);
550 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
551                               gpa_t gpa);
552 int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
553 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
554 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
555 int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
556 unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
557 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
558 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
559                              gfn_t gfn);
560
561 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
562 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
563 bool kvm_vcpu_yield_to(struct kvm_vcpu *target);
564 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
565 void kvm_resched(struct kvm_vcpu *vcpu);
566 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
567 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
568
569 void kvm_flush_remote_tlbs(struct kvm *kvm);
570 void kvm_reload_remote_mmus(struct kvm *kvm);
571 void kvm_make_mclock_inprogress_request(struct kvm *kvm);
572 void kvm_make_update_eoibitmap_request(struct kvm *kvm);
573
574 long kvm_arch_dev_ioctl(struct file *filp,
575                         unsigned int ioctl, unsigned long arg);
576 long kvm_arch_vcpu_ioctl(struct file *filp,
577                          unsigned int ioctl, unsigned long arg);
578 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
579
580 int kvm_dev_ioctl_check_extension(long ext);
581
582 int kvm_get_dirty_log(struct kvm *kvm,
583                         struct kvm_dirty_log *log, int *is_dirty);
584 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
585                                 struct kvm_dirty_log *log);
586
587 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
588                                    struct kvm_userspace_memory_region *mem);
589 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level);
590 long kvm_arch_vm_ioctl(struct file *filp,
591                        unsigned int ioctl, unsigned long arg);
592
593 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
594 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
595
596 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
597                                     struct kvm_translation *tr);
598
599 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
600 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
601 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
602                                   struct kvm_sregs *sregs);
603 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
604                                   struct kvm_sregs *sregs);
605 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
606                                     struct kvm_mp_state *mp_state);
607 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
608                                     struct kvm_mp_state *mp_state);
609 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
610                                         struct kvm_guest_debug *dbg);
611 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
612
613 int kvm_arch_init(void *opaque);
614 void kvm_arch_exit(void);
615
616 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
617 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
618
619 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
620 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
621 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
622 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
623 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
624 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
625 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
626
627 int kvm_arch_hardware_enable(void *garbage);
628 void kvm_arch_hardware_disable(void *garbage);
629 int kvm_arch_hardware_setup(void);
630 void kvm_arch_hardware_unsetup(void);
631 void kvm_arch_check_processor_compat(void *rtn);
632 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
633 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
634
635 void kvm_free_physmem(struct kvm *kvm);
636
637 void *kvm_kvzalloc(unsigned long size);
638 void kvm_kvfree(const void *addr);
639
640 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
641 static inline struct kvm *kvm_arch_alloc_vm(void)
642 {
643         return kzalloc(sizeof(struct kvm), GFP_KERNEL);
644 }
645
646 static inline void kvm_arch_free_vm(struct kvm *kvm)
647 {
648         kfree(kvm);
649 }
650 #endif
651
652 static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
653 {
654 #ifdef __KVM_HAVE_ARCH_WQP
655         return vcpu->arch.wqp;
656 #else
657         return &vcpu->wq;
658 #endif
659 }
660
661 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
662 void kvm_arch_destroy_vm(struct kvm *kvm);
663 void kvm_free_all_assigned_devices(struct kvm *kvm);
664 void kvm_arch_sync_events(struct kvm *kvm);
665
666 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
667 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
668
669 bool kvm_is_mmio_pfn(pfn_t pfn);
670
671 struct kvm_irq_ack_notifier {
672         struct hlist_node link;
673         unsigned gsi;
674         void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
675 };
676
677 struct kvm_assigned_dev_kernel {
678         struct kvm_irq_ack_notifier ack_notifier;
679         struct list_head list;
680         int assigned_dev_id;
681         int host_segnr;
682         int host_busnr;
683         int host_devfn;
684         unsigned int entries_nr;
685         int host_irq;
686         bool host_irq_disabled;
687         bool pci_2_3;
688         struct msix_entry *host_msix_entries;
689         int guest_irq;
690         struct msix_entry *guest_msix_entries;
691         unsigned long irq_requested_type;
692         int irq_source_id;
693         int flags;
694         struct pci_dev *dev;
695         struct kvm *kvm;
696         spinlock_t intx_lock;
697         spinlock_t intx_mask_lock;
698         char irq_name[32];
699         struct pci_saved_state *pci_saved_state;
700 };
701
702 struct kvm_irq_mask_notifier {
703         void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
704         int irq;
705         struct hlist_node link;
706 };
707
708 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
709                                     struct kvm_irq_mask_notifier *kimn);
710 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
711                                       struct kvm_irq_mask_notifier *kimn);
712 void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
713                              bool mask);
714
715 #ifdef __KVM_HAVE_IOAPIC
716 void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
717                                    union kvm_ioapic_redirect_entry *entry,
718                                    unsigned long *deliver_bitmask);
719 #endif
720 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
721 int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level);
722 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
723                 int irq_source_id, int level);
724 bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
725 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
726 void kvm_register_irq_ack_notifier(struct kvm *kvm,
727                                    struct kvm_irq_ack_notifier *kian);
728 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
729                                    struct kvm_irq_ack_notifier *kian);
730 int kvm_request_irq_source_id(struct kvm *kvm);
731 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
732
733 /* For vcpu->arch.iommu_flags */
734 #define KVM_IOMMU_CACHE_COHERENCY       0x1
735
736 #ifdef CONFIG_IOMMU_API
737 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
738 void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
739 int kvm_iommu_map_guest(struct kvm *kvm);
740 int kvm_iommu_unmap_guest(struct kvm *kvm);
741 int kvm_assign_device(struct kvm *kvm,
742                       struct kvm_assigned_dev_kernel *assigned_dev);
743 int kvm_deassign_device(struct kvm *kvm,
744                         struct kvm_assigned_dev_kernel *assigned_dev);
745 #else /* CONFIG_IOMMU_API */
746 static inline int kvm_iommu_map_pages(struct kvm *kvm,
747                                       struct kvm_memory_slot *slot)
748 {
749         return 0;
750 }
751
752 static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
753                                          struct kvm_memory_slot *slot)
754 {
755 }
756
757 static inline int kvm_iommu_map_guest(struct kvm *kvm)
758 {
759         return -ENODEV;
760 }
761
762 static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
763 {
764         return 0;
765 }
766
767 static inline int kvm_assign_device(struct kvm *kvm,
768                 struct kvm_assigned_dev_kernel *assigned_dev)
769 {
770         return 0;
771 }
772
773 static inline int kvm_deassign_device(struct kvm *kvm,
774                 struct kvm_assigned_dev_kernel *assigned_dev)
775 {
776         return 0;
777 }
778 #endif /* CONFIG_IOMMU_API */
779
780 static inline void __guest_enter(void)
781 {
782         /*
783          * This is running in ioctl context so we can avoid
784          * the call to vtime_account() with its unnecessary idle check.
785          */
786         vtime_account_system(current);
787         current->flags |= PF_VCPU;
788 }
789
790 static inline void __guest_exit(void)
791 {
792         /*
793          * This is running in ioctl context so we can avoid
794          * the call to vtime_account() with its unnecessary idle check.
795          */
796         vtime_account_system(current);
797         current->flags &= ~PF_VCPU;
798 }
799
800 #ifdef CONFIG_CONTEXT_TRACKING
801 extern void guest_enter(void);
802 extern void guest_exit(void);
803
804 #else /* !CONFIG_CONTEXT_TRACKING */
805 static inline void guest_enter(void)
806 {
807         __guest_enter();
808 }
809
810 static inline void guest_exit(void)
811 {
812         __guest_exit();
813 }
814 #endif /* !CONFIG_CONTEXT_TRACKING */
815
816 static inline void kvm_guest_enter(void)
817 {
818         unsigned long flags;
819
820         BUG_ON(preemptible());
821
822         local_irq_save(flags);
823         guest_enter();
824         local_irq_restore(flags);
825
826         /* KVM does not hold any references to rcu protected data when it
827          * switches CPU into a guest mode. In fact switching to a guest mode
828          * is very similar to exiting to userspase from rcu point of view. In
829          * addition CPU may stay in a guest mode for quite a long time (up to
830          * one time slice). Lets treat guest mode as quiescent state, just like
831          * we do with user-mode execution.
832          */
833         rcu_virt_note_context_switch(smp_processor_id());
834 }
835
836 static inline void kvm_guest_exit(void)
837 {
838         unsigned long flags;
839
840         local_irq_save(flags);
841         guest_exit();
842         local_irq_restore(flags);
843 }
844
845 /*
846  * search_memslots() and __gfn_to_memslot() are here because they are
847  * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
848  * gfn_to_memslot() itself isn't here as an inline because that would
849  * bloat other code too much.
850  */
851 static inline struct kvm_memory_slot *
852 search_memslots(struct kvm_memslots *slots, gfn_t gfn)
853 {
854         struct kvm_memory_slot *memslot;
855
856         kvm_for_each_memslot(memslot, slots)
857                 if (gfn >= memslot->base_gfn &&
858                       gfn < memslot->base_gfn + memslot->npages)
859                         return memslot;
860
861         return NULL;
862 }
863
864 static inline struct kvm_memory_slot *
865 __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
866 {
867         return search_memslots(slots, gfn);
868 }
869
870 static inline unsigned long
871 __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
872 {
873         return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
874 }
875
876 static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
877 {
878         return gfn_to_memslot(kvm, gfn)->id;
879 }
880
881 static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
882 {
883         /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
884         return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
885                 (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
886 }
887
888 static inline gfn_t
889 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
890 {
891         gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
892
893         return slot->base_gfn + gfn_offset;
894 }
895
896 static inline gpa_t gfn_to_gpa(gfn_t gfn)
897 {
898         return (gpa_t)gfn << PAGE_SHIFT;
899 }
900
901 static inline gfn_t gpa_to_gfn(gpa_t gpa)
902 {
903         return (gfn_t)(gpa >> PAGE_SHIFT);
904 }
905
906 static inline hpa_t pfn_to_hpa(pfn_t pfn)
907 {
908         return (hpa_t)pfn << PAGE_SHIFT;
909 }
910
911 static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
912 {
913         set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
914 }
915
916 enum kvm_stat_kind {
917         KVM_STAT_VM,
918         KVM_STAT_VCPU,
919 };
920
921 struct kvm_stats_debugfs_item {
922         const char *name;
923         int offset;
924         enum kvm_stat_kind kind;
925         struct dentry *dentry;
926 };
927 extern struct kvm_stats_debugfs_item debugfs_entries[];
928 extern struct dentry *kvm_debugfs_dir;
929
930 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
931 static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
932 {
933         if (unlikely(kvm->mmu_notifier_count))
934                 return 1;
935         /*
936          * Ensure the read of mmu_notifier_count happens before the read
937          * of mmu_notifier_seq.  This interacts with the smp_wmb() in
938          * mmu_notifier_invalidate_range_end to make sure that the caller
939          * either sees the old (non-zero) value of mmu_notifier_count or
940          * the new (incremented) value of mmu_notifier_seq.
941          * PowerPC Book3s HV KVM calls this under a per-page lock
942          * rather than under kvm->mmu_lock, for scalability, so
943          * can't rely on kvm->mmu_lock to keep things ordered.
944          */
945         smp_rmb();
946         if (kvm->mmu_notifier_seq != mmu_seq)
947                 return 1;
948         return 0;
949 }
950 #endif
951
952 #ifdef KVM_CAP_IRQ_ROUTING
953
954 #define KVM_MAX_IRQ_ROUTES 1024
955
956 int kvm_setup_default_irq_routing(struct kvm *kvm);
957 int kvm_set_irq_routing(struct kvm *kvm,
958                         const struct kvm_irq_routing_entry *entries,
959                         unsigned nr,
960                         unsigned flags);
961 void kvm_free_irq_routing(struct kvm *kvm);
962
963 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
964
965 #else
966
967 static inline void kvm_free_irq_routing(struct kvm *kvm) {}
968
969 #endif
970
971 #ifdef CONFIG_HAVE_KVM_EVENTFD
972
973 void kvm_eventfd_init(struct kvm *kvm);
974 int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
975
976 #ifdef CONFIG_HAVE_KVM_IRQCHIP
977 int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
978 void kvm_irqfd_release(struct kvm *kvm);
979 void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *);
980 #else
981 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
982 {
983         return -EINVAL;
984 }
985
986 static inline void kvm_irqfd_release(struct kvm *kvm) {}
987 #endif
988
989 #else
990
991 static inline void kvm_eventfd_init(struct kvm *kvm) {}
992
993 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
994 {
995         return -EINVAL;
996 }
997
998 static inline void kvm_irqfd_release(struct kvm *kvm) {}
999
1000 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1001 static inline void kvm_irq_routing_update(struct kvm *kvm,
1002                                           struct kvm_irq_routing_table *irq_rt)
1003 {
1004         rcu_assign_pointer(kvm->irq_routing, irq_rt);
1005 }
1006 #endif
1007
1008 static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
1009 {
1010         return -ENOSYS;
1011 }
1012
1013 #endif /* CONFIG_HAVE_KVM_EVENTFD */
1014
1015 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
1016 static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
1017 {
1018         return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
1019 }
1020
1021 bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
1022
1023 #else
1024
1025 static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
1026
1027 #endif
1028
1029 #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
1030
1031 long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
1032                                   unsigned long arg);
1033
1034 #else
1035
1036 static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
1037                                                 unsigned long arg)
1038 {
1039         return -ENOTTY;
1040 }
1041
1042 #endif
1043
1044 static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
1045 {
1046         set_bit(req, &vcpu->requests);
1047 }
1048
1049 static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
1050 {
1051         if (test_bit(req, &vcpu->requests)) {
1052                 clear_bit(req, &vcpu->requests);
1053                 return true;
1054         } else {
1055                 return false;
1056         }
1057 }
1058
1059 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1060
1061 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1062 {
1063         vcpu->spin_loop.in_spin_loop = val;
1064 }
1065 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1066 {
1067         vcpu->spin_loop.dy_eligible = val;
1068 }
1069
1070 #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1071
1072 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1073 {
1074 }
1075
1076 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1077 {
1078 }
1079
1080 static inline bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
1081 {
1082         return true;
1083 }
1084
1085 #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1086 #endif
1087