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