1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <linux/types.h>
7 #include <linux/hardirq.h>
8 #include <linux/list.h>
9 #include <linux/mutex.h>
10 #include <linux/spinlock.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/sched/stat.h>
14 #include <linux/bug.h>
15 #include <linux/minmax.h>
17 #include <linux/mmu_notifier.h>
18 #include <linux/ftrace.h>
19 #include <linux/instrumentation.h>
20 #include <linux/preempt.h>
21 #include <linux/msi.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/rcupdate.h>
25 #include <linux/ratelimit.h>
26 #include <linux/err.h>
27 #include <linux/irqflags.h>
28 #include <linux/context_tracking.h>
29 #include <linux/irqbypass.h>
30 #include <linux/rcuwait.h>
31 #include <linux/refcount.h>
32 #include <linux/nospec.h>
33 #include <linux/notifier.h>
34 #include <asm/signal.h>
36 #include <linux/kvm.h>
37 #include <linux/kvm_para.h>
39 #include <linux/kvm_types.h>
41 #include <asm/kvm_host.h>
42 #include <linux/kvm_dirty_ring.h>
44 #ifndef KVM_MAX_VCPU_ID
45 #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
49 * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
50 * in kvm, other bits are visible for userspace which are defined in
51 * include/linux/kvm_h.
53 #define KVM_MEMSLOT_INVALID (1UL << 16)
56 * Bit 63 of the memslot generation number is an "update in-progress flag",
57 * e.g. is temporarily set for the duration of install_new_memslots().
58 * This flag effectively creates a unique generation number that is used to
59 * mark cached memslot data, e.g. MMIO accesses, as potentially being stale,
60 * i.e. may (or may not) have come from the previous memslots generation.
62 * This is necessary because the actual memslots update is not atomic with
63 * respect to the generation number update. Updating the generation number
64 * first would allow a vCPU to cache a spte from the old memslots using the
65 * new generation number, and updating the generation number after switching
66 * to the new memslots would allow cache hits using the old generation number
67 * to reference the defunct memslots.
69 * This mechanism is used to prevent getting hits in KVM's caches while a
70 * memslot update is in-progress, and to prevent cache hits *after* updating
71 * the actual generation number against accesses that were inserted into the
72 * cache *before* the memslots were updated.
74 #define KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS BIT_ULL(63)
76 /* Two fragments for cross MMIO pages. */
77 #define KVM_MAX_MMIO_FRAGMENTS 2
79 #ifndef KVM_ADDRESS_SPACE_NUM
80 #define KVM_ADDRESS_SPACE_NUM 1
84 * For the normal pfn, the highest 12 bits should be zero,
85 * so we can mask bit 62 ~ bit 52 to indicate the error pfn,
86 * mask bit 63 to indicate the noslot pfn.
88 #define KVM_PFN_ERR_MASK (0x7ffULL << 52)
89 #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
90 #define KVM_PFN_NOSLOT (0x1ULL << 63)
92 #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK)
93 #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
94 #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)
97 * error pfns indicate that the gfn is in slot but faild to
98 * translate it to pfn on host.
100 static inline bool is_error_pfn(kvm_pfn_t pfn)
102 return !!(pfn & KVM_PFN_ERR_MASK);
106 * error_noslot pfns indicate that the gfn can not be
107 * translated to pfn - it is not in slot or failed to
108 * translate it to pfn.
110 static inline bool is_error_noslot_pfn(kvm_pfn_t pfn)
112 return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
115 /* noslot pfn indicates that the gfn is not in slot. */
116 static inline bool is_noslot_pfn(kvm_pfn_t pfn)
118 return pfn == KVM_PFN_NOSLOT;
122 * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390)
123 * provide own defines and kvm_is_error_hva
125 #ifndef KVM_HVA_ERR_BAD
127 #define KVM_HVA_ERR_BAD (PAGE_OFFSET)
128 #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE)
130 static inline bool kvm_is_error_hva(unsigned long addr)
132 return addr >= PAGE_OFFSET;
137 #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT))
139 static inline bool is_error_page(struct page *page)
144 #define KVM_REQUEST_MASK GENMASK(7,0)
145 #define KVM_REQUEST_NO_WAKEUP BIT(8)
146 #define KVM_REQUEST_WAIT BIT(9)
148 * Architecture-independent vcpu->requests bit members
149 * Bits 4-7 are reserved for more arch-independent bits.
151 #define KVM_REQ_TLB_FLUSH (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
152 #define KVM_REQ_MMU_RELOAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
153 #define KVM_REQ_UNBLOCK 2
154 #define KVM_REQ_UNHALT 3
155 #define KVM_REQ_VM_BUGGED (4 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
156 #define KVM_REQUEST_ARCH_BASE 8
158 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \
159 BUILD_BUG_ON((unsigned)(nr) >= (sizeof_field(struct kvm_vcpu, requests) * 8) - KVM_REQUEST_ARCH_BASE); \
160 (unsigned)(((nr) + KVM_REQUEST_ARCH_BASE) | (flags)); \
162 #define KVM_ARCH_REQ(nr) KVM_ARCH_REQ_FLAGS(nr, 0)
164 bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
165 struct kvm_vcpu *except,
166 unsigned long *vcpu_bitmap, cpumask_var_t tmp);
167 bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
168 bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
169 struct kvm_vcpu *except);
170 bool kvm_make_cpus_request_mask(struct kvm *kvm, unsigned int req,
171 unsigned long *vcpu_bitmap);
173 #define KVM_USERSPACE_IRQ_SOURCE_ID 0
174 #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
176 extern struct mutex kvm_lock;
177 extern struct list_head vm_list;
179 struct kvm_io_range {
182 struct kvm_io_device *dev;
185 #define NR_IOBUS_DEVS 1000
190 struct kvm_io_range range[];
196 KVM_VIRTIO_CCW_NOTIFY_BUS,
201 int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
202 int len, const void *val);
203 int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
204 gpa_t addr, int len, const void *val, long cookie);
205 int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
207 int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
208 int len, struct kvm_io_device *dev);
209 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
210 struct kvm_io_device *dev);
211 struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
214 #ifdef CONFIG_KVM_ASYNC_PF
215 struct kvm_async_pf {
216 struct work_struct work;
217 struct list_head link;
218 struct list_head queue;
219 struct kvm_vcpu *vcpu;
220 struct mm_struct *mm;
223 struct kvm_arch_async_pf arch;
225 bool notpresent_injected;
228 void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
229 void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
230 bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
231 unsigned long hva, struct kvm_arch_async_pf *arch);
232 int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
235 #ifdef KVM_ARCH_WANT_MMU_NOTIFIER
236 struct kvm_gfn_range {
237 struct kvm_memory_slot *slot;
243 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range);
244 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
245 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
246 bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range);
253 READING_SHADOW_PAGE_TABLES,
256 #define KVM_UNMAPPED_PAGE ((void *) 0x500 + POISON_POINTER_DELTA)
258 struct kvm_host_map {
260 * Only valid if the 'pfn' is managed by the host kernel (i.e. There is
261 * a 'struct page' for it. When using mem= kernel parameter some memory
262 * can be used as guest memory but they are not managed by host
264 * If 'pfn' is not managed by the host kernel, this field is
265 * initialized to KVM_UNMAPPED_PAGE.
274 * Used to check if the mapping is valid or not. Never use 'kvm_host_map'
275 * directly to check for that.
277 static inline bool kvm_vcpu_mapped(struct kvm_host_map *map)
282 static inline bool kvm_vcpu_can_poll(ktime_t cur, ktime_t stop)
284 return single_task_running() && !need_resched() && ktime_before(cur, stop);
288 * Sometimes a large or cross-page mmio needs to be broken up into separate
289 * exits for userspace servicing.
291 struct kvm_mmio_fragment {
299 #ifdef CONFIG_PREEMPT_NOTIFIERS
300 struct preempt_notifier preempt_notifier;
303 int vcpu_id; /* id given by userspace at creation */
304 int vcpu_idx; /* index in kvm->vcpus array */
308 unsigned long guest_debug;
311 struct list_head blocked_vcpu_list;
317 struct pid __rcu *pid;
320 unsigned int halt_poll_ns;
323 #ifdef CONFIG_HAS_IOMEM
325 int mmio_read_completed;
327 int mmio_cur_fragment;
328 int mmio_nr_fragments;
329 struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS];
332 #ifdef CONFIG_KVM_ASYNC_PF
335 struct list_head queue;
336 struct list_head done;
341 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
343 * Cpu relax intercept or pause loop exit optimization
344 * in_spin_loop: set when a vcpu does a pause loop exit
345 * or cpu relax intercepted.
346 * dy_eligible: indicates whether vcpu is eligible for directed yield.
355 struct kvm_vcpu_arch arch;
356 struct kvm_vcpu_stat stat;
357 char stats_id[KVM_STATS_NAME_SIZE];
358 struct kvm_dirty_ring dirty_ring;
361 * The index of the most recently used memslot by this vCPU. It's ok
362 * if this becomes stale due to memslot changes since we always check
363 * it is a valid slot.
369 * Start accounting time towards a guest.
370 * Must be called before entering guest context.
372 static __always_inline void guest_timing_enter_irqoff(void)
375 * This is running in ioctl context so its safe to assume that it's the
376 * stime pending cputime to flush.
378 instrumentation_begin();
379 vtime_account_guest_enter();
380 instrumentation_end();
384 * Enter guest context and enter an RCU extended quiescent state.
386 * Between guest_context_enter_irqoff() and guest_context_exit_irqoff() it is
387 * unsafe to use any code which may directly or indirectly use RCU, tracing
388 * (including IRQ flag tracing), or lockdep. All code in this period must be
389 * non-instrumentable.
391 static __always_inline void guest_context_enter_irqoff(void)
394 * KVM does not hold any references to rcu protected data when it
395 * switches CPU into a guest mode. In fact switching to a guest mode
396 * is very similar to exiting to userspace from rcu point of view. In
397 * addition CPU may stay in a guest mode for quite a long time (up to
398 * one time slice). Lets treat guest mode as quiescent state, just like
399 * we do with user-mode execution.
401 if (!context_tracking_guest_enter()) {
402 instrumentation_begin();
403 rcu_virt_note_context_switch(smp_processor_id());
404 instrumentation_end();
409 * Deprecated. Architectures should move to guest_timing_enter_irqoff() and
410 * guest_state_enter_irqoff().
412 static __always_inline void guest_enter_irqoff(void)
414 guest_timing_enter_irqoff();
415 guest_context_enter_irqoff();
419 * guest_state_enter_irqoff - Fixup state when entering a guest
421 * Entry to a guest will enable interrupts, but the kernel state is interrupts
422 * disabled when this is invoked. Also tell RCU about it.
424 * 1) Trace interrupts on state
425 * 2) Invoke context tracking if enabled to adjust RCU state
426 * 3) Tell lockdep that interrupts are enabled
428 * Invoked from architecture specific code before entering a guest.
429 * Must be called with interrupts disabled and the caller must be
430 * non-instrumentable.
431 * The caller has to invoke guest_timing_enter_irqoff() before this.
433 * Note: this is analogous to exit_to_user_mode().
435 static __always_inline void guest_state_enter_irqoff(void)
437 instrumentation_begin();
438 trace_hardirqs_on_prepare();
439 lockdep_hardirqs_on_prepare();
440 instrumentation_end();
442 guest_context_enter_irqoff();
443 lockdep_hardirqs_on(CALLER_ADDR0);
447 * Exit guest context and exit an RCU extended quiescent state.
449 * Between guest_context_enter_irqoff() and guest_context_exit_irqoff() it is
450 * unsafe to use any code which may directly or indirectly use RCU, tracing
451 * (including IRQ flag tracing), or lockdep. All code in this period must be
452 * non-instrumentable.
454 static __always_inline void guest_context_exit_irqoff(void)
456 context_tracking_guest_exit();
460 * Stop accounting time towards a guest.
461 * Must be called after exiting guest context.
463 static __always_inline void guest_timing_exit_irqoff(void)
465 instrumentation_begin();
466 /* Flush the guest cputime we spent on the guest */
467 vtime_account_guest_exit();
468 instrumentation_end();
472 * Deprecated. Architectures should move to guest_state_exit_irqoff() and
473 * guest_timing_exit_irqoff().
475 static __always_inline void guest_exit_irqoff(void)
477 guest_context_exit_irqoff();
478 guest_timing_exit_irqoff();
481 static inline void guest_exit(void)
485 local_irq_save(flags);
487 local_irq_restore(flags);
491 * guest_state_exit_irqoff - Establish state when returning from guest mode
493 * Entry from a guest disables interrupts, but guest mode is traced as
494 * interrupts enabled. Also with NO_HZ_FULL RCU might be idle.
496 * 1) Tell lockdep that interrupts are disabled
497 * 2) Invoke context tracking if enabled to reactivate RCU
498 * 3) Trace interrupts off state
500 * Invoked from architecture specific code after exiting a guest.
501 * Must be invoked with interrupts disabled and the caller must be
502 * non-instrumentable.
503 * The caller has to invoke guest_timing_exit_irqoff() after this.
505 * Note: this is analogous to enter_from_user_mode().
507 static __always_inline void guest_state_exit_irqoff(void)
509 lockdep_hardirqs_off(CALLER_ADDR0);
510 guest_context_exit_irqoff();
512 instrumentation_begin();
513 trace_hardirqs_off_finish();
514 instrumentation_end();
517 static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
520 * The memory barrier ensures a previous write to vcpu->requests cannot
521 * be reordered with the read of vcpu->mode. It pairs with the general
522 * memory barrier following the write of vcpu->mode in VCPU RUN.
524 smp_mb__before_atomic();
525 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
529 * Some of the bitops functions do not support too long bitmaps.
530 * This number must be determined not to exceed such limits.
532 #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
534 struct kvm_memory_slot {
536 unsigned long npages;
537 unsigned long *dirty_bitmap;
538 struct kvm_arch_memory_slot arch;
539 unsigned long userspace_addr;
545 static inline bool kvm_slot_dirty_track_enabled(struct kvm_memory_slot *slot)
547 return slot->flags & KVM_MEM_LOG_DIRTY_PAGES;
550 static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
552 return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
555 static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *memslot)
557 unsigned long len = kvm_dirty_bitmap_bytes(memslot);
559 return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
562 #ifndef KVM_DIRTY_LOG_MANUAL_CAPS
563 #define KVM_DIRTY_LOG_MANUAL_CAPS KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE
566 struct kvm_s390_adapter_int {
579 struct kvm_kernel_irq_routing_entry {
582 int (*set)(struct kvm_kernel_irq_routing_entry *e,
583 struct kvm *kvm, int irq_source_id, int level,
597 struct kvm_s390_adapter_int adapter;
598 struct kvm_hv_sint hv_sint;
600 struct hlist_node link;
603 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
604 struct kvm_irq_routing_table {
605 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
608 * Array indexed by gsi. Each entry contains list of irq chips
609 * the gsi is connected to.
611 struct hlist_head map[];
615 #ifndef KVM_PRIVATE_MEM_SLOTS
616 #define KVM_PRIVATE_MEM_SLOTS 0
619 #define KVM_MEM_SLOTS_NUM SHRT_MAX
620 #define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_PRIVATE_MEM_SLOTS)
622 #ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
623 static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
631 * memslots are not sorted by id anymore, please use id_to_memslot()
632 * to get the memslot by its id.
634 struct kvm_memslots {
636 /* The mapping table from slot id to the index in memslots[]. */
637 short id_to_index[KVM_MEM_SLOTS_NUM];
638 atomic_t last_used_slot;
640 struct kvm_memory_slot memslots[];
644 #ifdef KVM_HAVE_MMU_RWLOCK
648 #endif /* KVM_HAVE_MMU_RWLOCK */
650 struct mutex slots_lock;
653 * Protects the arch-specific fields of struct kvm_memory_slots in
654 * use by the VM. To be used under the slots_lock (above) or in a
655 * kvm->srcu critical section where acquiring the slots_lock would
656 * lead to deadlock with the synchronize_srcu in
657 * install_new_memslots.
659 struct mutex slots_arch_lock;
660 struct mm_struct *mm; /* userspace tied to this vm */
661 struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM];
662 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
664 /* Used to wait for completion of MMU notifiers. */
665 spinlock_t mn_invalidate_lock;
666 unsigned long mn_active_invalidate_count;
667 struct rcuwait mn_memslots_update_rcuwait;
670 * created_vcpus is protected by kvm->lock, and is incremented
671 * at the beginning of KVM_CREATE_VCPU. online_vcpus is only
672 * incremented after storing the kvm_vcpu pointer in vcpus,
673 * and is accessed atomically.
675 atomic_t online_vcpus;
677 int last_boosted_vcpu;
678 struct list_head vm_list;
680 struct kvm_io_bus __rcu *buses[KVM_NR_BUSES];
681 #ifdef CONFIG_HAVE_KVM_EVENTFD
684 struct list_head items;
685 struct list_head resampler_list;
686 struct mutex resampler_lock;
688 struct list_head ioeventfds;
690 struct kvm_vm_stat stat;
691 struct kvm_arch arch;
692 refcount_t users_count;
693 #ifdef CONFIG_KVM_MMIO
694 struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
695 spinlock_t ring_lock;
696 struct list_head coalesced_zones;
699 struct mutex irq_lock;
700 #ifdef CONFIG_HAVE_KVM_IRQCHIP
702 * Update side is protected by irq_lock.
704 struct kvm_irq_routing_table __rcu *irq_routing;
706 #ifdef CONFIG_HAVE_KVM_IRQFD
707 struct hlist_head irq_ack_notifier_list;
710 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
711 struct mmu_notifier mmu_notifier;
712 unsigned long mmu_notifier_seq;
713 long mmu_notifier_count;
714 unsigned long mmu_notifier_range_start;
715 unsigned long mmu_notifier_range_end;
717 struct list_head devices;
718 u64 manual_dirty_log_protect;
719 struct dentry *debugfs_dentry;
720 struct kvm_stat_data **debugfs_stat_data;
721 struct srcu_struct srcu;
722 struct srcu_struct irq_srcu;
724 unsigned int max_halt_poll_ns;
728 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
729 struct notifier_block pm_notifier;
731 char stats_id[KVM_STATS_NAME_SIZE];
734 #define kvm_err(fmt, ...) \
735 pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
736 #define kvm_info(fmt, ...) \
737 pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
738 #define kvm_debug(fmt, ...) \
739 pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
740 #define kvm_debug_ratelimited(fmt, ...) \
741 pr_debug_ratelimited("kvm [%i]: " fmt, task_pid_nr(current), \
743 #define kvm_pr_unimpl(fmt, ...) \
744 pr_err_ratelimited("kvm [%i]: " fmt, \
745 task_tgid_nr(current), ## __VA_ARGS__)
747 /* The guest did something we don't support. */
748 #define vcpu_unimpl(vcpu, fmt, ...) \
749 kvm_pr_unimpl("vcpu%i, guest rIP: 0x%lx " fmt, \
750 (vcpu)->vcpu_id, kvm_rip_read(vcpu), ## __VA_ARGS__)
752 #define vcpu_debug(vcpu, fmt, ...) \
753 kvm_debug("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
754 #define vcpu_debug_ratelimited(vcpu, fmt, ...) \
755 kvm_debug_ratelimited("vcpu%i " fmt, (vcpu)->vcpu_id, \
757 #define vcpu_err(vcpu, fmt, ...) \
758 kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
760 static inline void kvm_vm_bugged(struct kvm *kvm)
762 kvm->vm_bugged = true;
763 kvm_make_all_cpus_request(kvm, KVM_REQ_VM_BUGGED);
766 #define KVM_BUG(cond, kvm, fmt...) \
768 int __ret = (cond); \
770 if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \
771 kvm_vm_bugged(kvm); \
775 #define KVM_BUG_ON(cond, kvm) \
777 int __ret = (cond); \
779 if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \
780 kvm_vm_bugged(kvm); \
784 static inline bool kvm_dirty_log_manual_protect_and_init_set(struct kvm *kvm)
786 return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET);
789 static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx)
791 return srcu_dereference_check(kvm->buses[idx], &kvm->srcu,
792 lockdep_is_held(&kvm->slots_lock) ||
793 !refcount_read(&kvm->users_count));
796 static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
798 int num_vcpus = atomic_read(&kvm->online_vcpus);
799 i = array_index_nospec(i, num_vcpus);
801 /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu. */
803 return kvm->vcpus[i];
806 #define kvm_for_each_vcpu(idx, vcpup, kvm) \
808 idx < atomic_read(&kvm->online_vcpus) && \
809 (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
812 static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
814 struct kvm_vcpu *vcpu = NULL;
819 if (id < KVM_MAX_VCPUS)
820 vcpu = kvm_get_vcpu(kvm, id);
821 if (vcpu && vcpu->vcpu_id == id)
823 kvm_for_each_vcpu(i, vcpu, kvm)
824 if (vcpu->vcpu_id == id)
829 #define kvm_for_each_memslot(memslot, slots) \
830 for (memslot = &slots->memslots[0]; \
831 memslot < slots->memslots + slots->used_slots; memslot++) \
832 if (WARN_ON_ONCE(!memslot->npages)) { \
835 void kvm_vcpu_destroy(struct kvm_vcpu *vcpu);
837 void vcpu_load(struct kvm_vcpu *vcpu);
838 void vcpu_put(struct kvm_vcpu *vcpu);
840 #ifdef __KVM_HAVE_IOAPIC
841 void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm);
842 void kvm_arch_post_irq_routing_update(struct kvm *kvm);
844 static inline void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm)
847 static inline void kvm_arch_post_irq_routing_update(struct kvm *kvm)
852 #ifdef CONFIG_HAVE_KVM_IRQFD
853 int kvm_irqfd_init(void);
854 void kvm_irqfd_exit(void);
856 static inline int kvm_irqfd_init(void)
861 static inline void kvm_irqfd_exit(void)
865 int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
866 struct module *module);
869 void kvm_get_kvm(struct kvm *kvm);
870 bool kvm_get_kvm_safe(struct kvm *kvm);
871 void kvm_put_kvm(struct kvm *kvm);
872 bool file_is_kvm(struct file *file);
873 void kvm_put_kvm_no_destroy(struct kvm *kvm);
875 static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
877 as_id = array_index_nospec(as_id, KVM_ADDRESS_SPACE_NUM);
878 return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu,
879 lockdep_is_held(&kvm->slots_lock) ||
880 !refcount_read(&kvm->users_count));
883 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
885 return __kvm_memslots(kvm, 0);
888 static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu)
890 int as_id = kvm_arch_vcpu_memslots_id(vcpu);
892 return __kvm_memslots(vcpu->kvm, as_id);
896 struct kvm_memory_slot *id_to_memslot(struct kvm_memslots *slots, int id)
898 int index = slots->id_to_index[id];
899 struct kvm_memory_slot *slot;
904 slot = &slots->memslots[index];
906 WARN_ON(slot->id != id);
911 * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
912 * - create a new memory slot
913 * - delete an existing memory slot
914 * - modify an existing memory slot
915 * -- move it in the guest physical memory space
916 * -- just change its flags
918 * Since flags can be changed by some of these operations, the following
919 * differentiation is the best we can do for __kvm_set_memory_region():
928 int kvm_set_memory_region(struct kvm *kvm,
929 const struct kvm_userspace_memory_region *mem);
930 int __kvm_set_memory_region(struct kvm *kvm,
931 const struct kvm_userspace_memory_region *mem);
932 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot);
933 void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen);
934 int kvm_arch_prepare_memory_region(struct kvm *kvm,
935 struct kvm_memory_slot *memslot,
936 const struct kvm_userspace_memory_region *mem,
937 enum kvm_mr_change change);
938 void kvm_arch_commit_memory_region(struct kvm *kvm,
939 const struct kvm_userspace_memory_region *mem,
940 struct kvm_memory_slot *old,
941 const struct kvm_memory_slot *new,
942 enum kvm_mr_change change);
943 /* flush all memory translations */
944 void kvm_arch_flush_shadow_all(struct kvm *kvm);
945 /* flush memory translations pointing to 'slot' */
946 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
947 struct kvm_memory_slot *slot);
949 int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
950 struct page **pages, int nr_pages);
952 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
953 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
954 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable);
955 unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
956 unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn,
958 void kvm_release_page_clean(struct page *page);
959 void kvm_release_page_dirty(struct page *page);
960 void kvm_set_page_accessed(struct page *page);
962 kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
963 kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
965 kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
966 kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
967 kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
968 bool atomic, bool *async, bool write_fault,
969 bool *writable, hva_t *hva);
971 void kvm_release_pfn_clean(kvm_pfn_t pfn);
972 void kvm_release_pfn_dirty(kvm_pfn_t pfn);
973 void kvm_set_pfn_dirty(kvm_pfn_t pfn);
974 void kvm_set_pfn_accessed(kvm_pfn_t pfn);
976 void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache);
977 int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
979 int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
980 int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
981 void *data, unsigned long len);
982 int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
983 void *data, unsigned int offset,
985 int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
986 int offset, int len);
987 int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
989 int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
990 void *data, unsigned long len);
991 int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
992 void *data, unsigned int offset,
994 int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
995 gpa_t gpa, unsigned long len);
997 #define __kvm_get_guest(kvm, gfn, offset, v) \
999 unsigned long __addr = gfn_to_hva(kvm, gfn); \
1000 typeof(v) __user *__uaddr = (typeof(__uaddr))(__addr + offset); \
1001 int __ret = -EFAULT; \
1003 if (!kvm_is_error_hva(__addr)) \
1004 __ret = get_user(v, __uaddr); \
1008 #define kvm_get_guest(kvm, gpa, v) \
1010 gpa_t __gpa = gpa; \
1011 struct kvm *__kvm = kvm; \
1013 __kvm_get_guest(__kvm, __gpa >> PAGE_SHIFT, \
1014 offset_in_page(__gpa), v); \
1017 #define __kvm_put_guest(kvm, gfn, offset, v) \
1019 unsigned long __addr = gfn_to_hva(kvm, gfn); \
1020 typeof(v) __user *__uaddr = (typeof(__uaddr))(__addr + offset); \
1021 int __ret = -EFAULT; \
1023 if (!kvm_is_error_hva(__addr)) \
1024 __ret = put_user(v, __uaddr); \
1026 mark_page_dirty(kvm, gfn); \
1030 #define kvm_put_guest(kvm, gpa, v) \
1032 gpa_t __gpa = gpa; \
1033 struct kvm *__kvm = kvm; \
1035 __kvm_put_guest(__kvm, __gpa >> PAGE_SHIFT, \
1036 offset_in_page(__gpa), v); \
1039 int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
1040 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
1041 bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
1042 bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
1043 unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn);
1044 void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot, gfn_t gfn);
1045 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
1047 struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
1048 struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn);
1049 kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn);
1050 kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
1051 int kvm_vcpu_map(struct kvm_vcpu *vcpu, gpa_t gpa, struct kvm_host_map *map);
1052 int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
1053 struct gfn_to_pfn_cache *cache, bool atomic);
1054 struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn);
1055 void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty);
1056 int kvm_unmap_gfn(struct kvm_vcpu *vcpu, struct kvm_host_map *map,
1057 struct gfn_to_pfn_cache *cache, bool dirty, bool atomic);
1058 unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
1059 unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
1060 int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset,
1062 int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
1064 int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
1066 int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data,
1067 int offset, int len);
1068 int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
1070 void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn);
1072 void kvm_sigset_activate(struct kvm_vcpu *vcpu);
1073 void kvm_sigset_deactivate(struct kvm_vcpu *vcpu);
1075 void kvm_vcpu_block(struct kvm_vcpu *vcpu);
1076 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);
1077 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
1078 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
1079 void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
1080 int kvm_vcpu_yield_to(struct kvm_vcpu *target);
1081 void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible);
1083 void kvm_flush_remote_tlbs(struct kvm *kvm);
1084 void kvm_reload_remote_mmus(struct kvm *kvm);
1086 #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE
1087 int kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int min);
1088 int kvm_mmu_memory_cache_nr_free_objects(struct kvm_mmu_memory_cache *mc);
1089 void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc);
1090 void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);
1093 void kvm_inc_notifier_count(struct kvm *kvm, unsigned long start,
1095 void kvm_dec_notifier_count(struct kvm *kvm, unsigned long start,
1098 long kvm_arch_dev_ioctl(struct file *filp,
1099 unsigned int ioctl, unsigned long arg);
1100 long kvm_arch_vcpu_ioctl(struct file *filp,
1101 unsigned int ioctl, unsigned long arg);
1102 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
1104 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext);
1106 void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1107 struct kvm_memory_slot *slot,
1109 unsigned long mask);
1110 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot);
1112 #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
1113 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
1114 const struct kvm_memory_slot *memslot);
1115 #else /* !CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
1116 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log);
1117 int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
1118 int *is_dirty, struct kvm_memory_slot **memslot);
1121 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
1123 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1124 struct kvm_enable_cap *cap);
1125 long kvm_arch_vm_ioctl(struct file *filp,
1126 unsigned int ioctl, unsigned long arg);
1128 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
1129 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
1131 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1132 struct kvm_translation *tr);
1134 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
1135 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
1136 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1137 struct kvm_sregs *sregs);
1138 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1139 struct kvm_sregs *sregs);
1140 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1141 struct kvm_mp_state *mp_state);
1142 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1143 struct kvm_mp_state *mp_state);
1144 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1145 struct kvm_guest_debug *dbg);
1146 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu);
1148 int kvm_arch_init(void *opaque);
1149 void kvm_arch_exit(void);
1151 void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu);
1153 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
1154 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
1155 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id);
1156 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu);
1157 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
1158 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
1160 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
1161 int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state);
1164 #ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS
1165 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry);
1168 int kvm_arch_hardware_enable(void);
1169 void kvm_arch_hardware_disable(void);
1170 int kvm_arch_hardware_setup(void *opaque);
1171 void kvm_arch_hardware_unsetup(void);
1172 int kvm_arch_check_processor_compat(void *opaque);
1173 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
1174 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);
1175 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
1176 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu);
1177 bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu);
1178 int kvm_arch_post_init_vm(struct kvm *kvm);
1179 void kvm_arch_pre_destroy_vm(struct kvm *kvm);
1180 int kvm_arch_create_vm_debugfs(struct kvm *kvm);
1182 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
1184 * All architectures that want to use vzalloc currently also
1185 * need their own kvm_arch_alloc_vm implementation.
1187 static inline struct kvm *kvm_arch_alloc_vm(void)
1189 return kzalloc(sizeof(struct kvm), GFP_KERNEL);
1192 static inline void kvm_arch_free_vm(struct kvm *kvm)
1198 #ifndef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
1199 static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
1205 #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
1206 void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
1207 void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
1208 bool kvm_arch_has_noncoherent_dma(struct kvm *kvm);
1210 static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
1214 static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
1218 static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
1223 #ifdef __KVM_HAVE_ARCH_ASSIGNED_DEVICE
1224 void kvm_arch_start_assignment(struct kvm *kvm);
1225 void kvm_arch_end_assignment(struct kvm *kvm);
1226 bool kvm_arch_has_assigned_device(struct kvm *kvm);
1228 static inline void kvm_arch_start_assignment(struct kvm *kvm)
1232 static inline void kvm_arch_end_assignment(struct kvm *kvm)
1236 static __always_inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
1242 static inline struct rcuwait *kvm_arch_vcpu_get_wait(struct kvm_vcpu *vcpu)
1244 #ifdef __KVM_HAVE_ARCH_WQP
1245 return vcpu->arch.waitp;
1251 #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED
1253 * returns true if the virtual interrupt controller is initialized and
1254 * ready to accept virtual IRQ. On some architectures the virtual interrupt
1255 * controller is dynamically instantiated and this is not always true.
1257 bool kvm_arch_intc_initialized(struct kvm *kvm);
1259 static inline bool kvm_arch_intc_initialized(struct kvm *kvm)
1265 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
1266 void kvm_arch_destroy_vm(struct kvm *kvm);
1267 void kvm_arch_sync_events(struct kvm *kvm);
1269 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
1271 bool kvm_is_reserved_pfn(kvm_pfn_t pfn);
1272 bool kvm_is_zone_device_pfn(kvm_pfn_t pfn);
1273 bool kvm_is_transparent_hugepage(kvm_pfn_t pfn);
1275 struct kvm_irq_ack_notifier {
1276 struct hlist_node link;
1278 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
1281 int kvm_irq_map_gsi(struct kvm *kvm,
1282 struct kvm_kernel_irq_routing_entry *entries, int gsi);
1283 int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin);
1285 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1287 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
1288 int irq_source_id, int level, bool line_status);
1289 int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
1290 struct kvm *kvm, int irq_source_id,
1291 int level, bool line_status);
1292 bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
1293 void kvm_notify_acked_gsi(struct kvm *kvm, int gsi);
1294 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
1295 void kvm_register_irq_ack_notifier(struct kvm *kvm,
1296 struct kvm_irq_ack_notifier *kian);
1297 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
1298 struct kvm_irq_ack_notifier *kian);
1299 int kvm_request_irq_source_id(struct kvm *kvm);
1300 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
1301 bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args);
1304 * Returns a pointer to the memslot at slot_index if it contains gfn.
1305 * Otherwise returns NULL.
1307 static inline struct kvm_memory_slot *
1308 try_get_memslot(struct kvm_memslots *slots, int slot_index, gfn_t gfn)
1310 struct kvm_memory_slot *slot;
1312 if (slot_index < 0 || slot_index >= slots->used_slots)
1316 * slot_index can come from vcpu->last_used_slot which is not kept
1317 * in sync with userspace-controllable memslot deletion. So use nospec
1318 * to prevent the CPU from speculating past the end of memslots[].
1320 slot_index = array_index_nospec(slot_index, slots->used_slots);
1321 slot = &slots->memslots[slot_index];
1323 if (gfn >= slot->base_gfn && gfn < slot->base_gfn + slot->npages)
1330 * Returns a pointer to the memslot that contains gfn and records the index of
1331 * the slot in index. Otherwise returns NULL.
1333 * IMPORTANT: Slots are sorted from highest GFN to lowest GFN!
1335 static inline struct kvm_memory_slot *
1336 search_memslots(struct kvm_memslots *slots, gfn_t gfn, int *index)
1338 int start = 0, end = slots->used_slots;
1339 struct kvm_memory_slot *memslots = slots->memslots;
1340 struct kvm_memory_slot *slot;
1342 if (unlikely(!slots->used_slots))
1345 while (start < end) {
1346 int slot = start + (end - start) / 2;
1348 if (gfn >= memslots[slot].base_gfn)
1354 slot = try_get_memslot(slots, start, gfn);
1364 * __gfn_to_memslot() and its descendants are here because it is called from
1365 * non-modular code in arch/powerpc/kvm/book3s_64_vio{,_hv}.c. gfn_to_memslot()
1366 * itself isn't here as an inline because that would bloat other code too much.
1368 static inline struct kvm_memory_slot *
1369 __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
1371 struct kvm_memory_slot *slot;
1372 int slot_index = atomic_read(&slots->last_used_slot);
1374 slot = try_get_memslot(slots, slot_index, gfn);
1378 slot = search_memslots(slots, gfn, &slot_index);
1380 atomic_set(&slots->last_used_slot, slot_index);
1387 static inline unsigned long
1388 __gfn_to_hva_memslot(const struct kvm_memory_slot *slot, gfn_t gfn)
1391 * The index was checked originally in search_memslots. To avoid
1392 * that a malicious guest builds a Spectre gadget out of e.g. page
1393 * table walks, do not let the processor speculate loads outside
1394 * the guest's registered memslots.
1396 unsigned long offset = gfn - slot->base_gfn;
1397 offset = array_index_nospec(offset, slot->npages);
1398 return slot->userspace_addr + offset * PAGE_SIZE;
1401 static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
1403 return gfn_to_memslot(kvm, gfn)->id;
1407 hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
1409 gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
1411 return slot->base_gfn + gfn_offset;
1414 static inline gpa_t gfn_to_gpa(gfn_t gfn)
1416 return (gpa_t)gfn << PAGE_SHIFT;
1419 static inline gfn_t gpa_to_gfn(gpa_t gpa)
1421 return (gfn_t)(gpa >> PAGE_SHIFT);
1424 static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn)
1426 return (hpa_t)pfn << PAGE_SHIFT;
1429 static inline struct page *kvm_vcpu_gpa_to_page(struct kvm_vcpu *vcpu,
1432 return kvm_vcpu_gfn_to_page(vcpu, gpa_to_gfn(gpa));
1435 static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa)
1437 unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
1439 return kvm_is_error_hva(hva);
1442 enum kvm_stat_kind {
1447 struct kvm_stat_data {
1449 const struct _kvm_stats_desc *desc;
1450 enum kvm_stat_kind kind;
1453 struct _kvm_stats_desc {
1454 struct kvm_stats_desc desc;
1455 char name[KVM_STATS_NAME_SIZE];
1458 #define STATS_DESC_COMMON(type, unit, base, exp, sz, bsz) \
1459 .flags = type | unit | base | \
1460 BUILD_BUG_ON_ZERO(type & ~KVM_STATS_TYPE_MASK) | \
1461 BUILD_BUG_ON_ZERO(unit & ~KVM_STATS_UNIT_MASK) | \
1462 BUILD_BUG_ON_ZERO(base & ~KVM_STATS_BASE_MASK), \
1467 #define VM_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \
1470 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \
1471 .offset = offsetof(struct kvm_vm_stat, generic.stat) \
1475 #define VCPU_GENERIC_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \
1478 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \
1479 .offset = offsetof(struct kvm_vcpu_stat, generic.stat) \
1483 #define VM_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \
1486 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \
1487 .offset = offsetof(struct kvm_vm_stat, stat) \
1491 #define VCPU_STATS_DESC(stat, type, unit, base, exp, sz, bsz) \
1494 STATS_DESC_COMMON(type, unit, base, exp, sz, bsz), \
1495 .offset = offsetof(struct kvm_vcpu_stat, stat) \
1499 /* SCOPE: VM, VM_GENERIC, VCPU, VCPU_GENERIC */
1500 #define STATS_DESC(SCOPE, stat, type, unit, base, exp, sz, bsz) \
1501 SCOPE##_STATS_DESC(stat, type, unit, base, exp, sz, bsz)
1503 #define STATS_DESC_CUMULATIVE(SCOPE, name, unit, base, exponent) \
1504 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_CUMULATIVE, \
1505 unit, base, exponent, 1, 0)
1506 #define STATS_DESC_INSTANT(SCOPE, name, unit, base, exponent) \
1507 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_INSTANT, \
1508 unit, base, exponent, 1, 0)
1509 #define STATS_DESC_PEAK(SCOPE, name, unit, base, exponent) \
1510 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_PEAK, \
1511 unit, base, exponent, 1, 0)
1512 #define STATS_DESC_LINEAR_HIST(SCOPE, name, unit, base, exponent, sz, bsz) \
1513 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_LINEAR_HIST, \
1514 unit, base, exponent, sz, bsz)
1515 #define STATS_DESC_LOG_HIST(SCOPE, name, unit, base, exponent, sz) \
1516 STATS_DESC(SCOPE, name, KVM_STATS_TYPE_LOG_HIST, \
1517 unit, base, exponent, sz, 0)
1519 /* Cumulative counter, read/write */
1520 #define STATS_DESC_COUNTER(SCOPE, name) \
1521 STATS_DESC_CUMULATIVE(SCOPE, name, KVM_STATS_UNIT_NONE, \
1522 KVM_STATS_BASE_POW10, 0)
1523 /* Instantaneous counter, read only */
1524 #define STATS_DESC_ICOUNTER(SCOPE, name) \
1525 STATS_DESC_INSTANT(SCOPE, name, KVM_STATS_UNIT_NONE, \
1526 KVM_STATS_BASE_POW10, 0)
1527 /* Peak counter, read/write */
1528 #define STATS_DESC_PCOUNTER(SCOPE, name) \
1529 STATS_DESC_PEAK(SCOPE, name, KVM_STATS_UNIT_NONE, \
1530 KVM_STATS_BASE_POW10, 0)
1532 /* Cumulative time in nanosecond */
1533 #define STATS_DESC_TIME_NSEC(SCOPE, name) \
1534 STATS_DESC_CUMULATIVE(SCOPE, name, KVM_STATS_UNIT_SECONDS, \
1535 KVM_STATS_BASE_POW10, -9)
1536 /* Linear histogram for time in nanosecond */
1537 #define STATS_DESC_LINHIST_TIME_NSEC(SCOPE, name, sz, bsz) \
1538 STATS_DESC_LINEAR_HIST(SCOPE, name, KVM_STATS_UNIT_SECONDS, \
1539 KVM_STATS_BASE_POW10, -9, sz, bsz)
1540 /* Logarithmic histogram for time in nanosecond */
1541 #define STATS_DESC_LOGHIST_TIME_NSEC(SCOPE, name, sz) \
1542 STATS_DESC_LOG_HIST(SCOPE, name, KVM_STATS_UNIT_SECONDS, \
1543 KVM_STATS_BASE_POW10, -9, sz)
1545 #define KVM_GENERIC_VM_STATS() \
1546 STATS_DESC_COUNTER(VM_GENERIC, remote_tlb_flush), \
1547 STATS_DESC_COUNTER(VM_GENERIC, remote_tlb_flush_requests)
1549 #define KVM_GENERIC_VCPU_STATS() \
1550 STATS_DESC_COUNTER(VCPU_GENERIC, halt_successful_poll), \
1551 STATS_DESC_COUNTER(VCPU_GENERIC, halt_attempted_poll), \
1552 STATS_DESC_COUNTER(VCPU_GENERIC, halt_poll_invalid), \
1553 STATS_DESC_COUNTER(VCPU_GENERIC, halt_wakeup), \
1554 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_success_ns), \
1555 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_ns), \
1556 STATS_DESC_TIME_NSEC(VCPU_GENERIC, halt_wait_ns), \
1557 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_success_hist, \
1558 HALT_POLL_HIST_COUNT), \
1559 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_poll_fail_hist, \
1560 HALT_POLL_HIST_COUNT), \
1561 STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_wait_hist, \
1562 HALT_POLL_HIST_COUNT)
1564 extern struct dentry *kvm_debugfs_dir;
1566 ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
1567 const struct _kvm_stats_desc *desc,
1568 void *stats, size_t size_stats,
1569 char __user *user_buffer, size_t size, loff_t *offset);
1572 * kvm_stats_linear_hist_update() - Update bucket value for linear histogram
1575 * @data: start address of the stats data
1576 * @size: the number of bucket of the stats data
1577 * @value: the new value used to update the linear histogram's bucket
1578 * @bucket_size: the size (width) of a bucket
1580 static inline void kvm_stats_linear_hist_update(u64 *data, size_t size,
1581 u64 value, size_t bucket_size)
1583 size_t index = div64_u64(value, bucket_size);
1585 index = min(index, size - 1);
1590 * kvm_stats_log_hist_update() - Update bucket value for logarithmic histogram
1593 * @data: start address of the stats data
1594 * @size: the number of bucket of the stats data
1595 * @value: the new value used to update the logarithmic histogram's bucket
1597 static inline void kvm_stats_log_hist_update(u64 *data, size_t size, u64 value)
1599 size_t index = fls64(value);
1601 index = min(index, size - 1);
1605 #define KVM_STATS_LINEAR_HIST_UPDATE(array, value, bsize) \
1606 kvm_stats_linear_hist_update(array, ARRAY_SIZE(array), value, bsize)
1607 #define KVM_STATS_LOG_HIST_UPDATE(array, value) \
1608 kvm_stats_log_hist_update(array, ARRAY_SIZE(array), value)
1611 extern const struct kvm_stats_header kvm_vm_stats_header;
1612 extern const struct _kvm_stats_desc kvm_vm_stats_desc[];
1613 extern const struct kvm_stats_header kvm_vcpu_stats_header;
1614 extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[];
1616 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
1617 static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
1619 if (unlikely(kvm->mmu_notifier_count))
1622 * Ensure the read of mmu_notifier_count happens before the read
1623 * of mmu_notifier_seq. This interacts with the smp_wmb() in
1624 * mmu_notifier_invalidate_range_end to make sure that the caller
1625 * either sees the old (non-zero) value of mmu_notifier_count or
1626 * the new (incremented) value of mmu_notifier_seq.
1627 * PowerPC Book3s HV KVM calls this under a per-page lock
1628 * rather than under kvm->mmu_lock, for scalability, so
1629 * can't rely on kvm->mmu_lock to keep things ordered.
1632 if (kvm->mmu_notifier_seq != mmu_seq)
1637 static inline int mmu_notifier_retry_hva(struct kvm *kvm,
1638 unsigned long mmu_seq,
1641 lockdep_assert_held(&kvm->mmu_lock);
1643 * If mmu_notifier_count is non-zero, then the range maintained by
1644 * kvm_mmu_notifier_invalidate_range_start contains all addresses that
1645 * might be being invalidated. Note that it may include some false
1646 * positives, due to shortcuts when handing concurrent invalidations.
1648 if (unlikely(kvm->mmu_notifier_count) &&
1649 hva >= kvm->mmu_notifier_range_start &&
1650 hva < kvm->mmu_notifier_range_end)
1652 if (kvm->mmu_notifier_seq != mmu_seq)
1658 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
1660 #define KVM_MAX_IRQ_ROUTES 4096 /* might need extension/rework in the future */
1662 bool kvm_arch_can_set_irq_routing(struct kvm *kvm);
1663 int kvm_set_irq_routing(struct kvm *kvm,
1664 const struct kvm_irq_routing_entry *entries,
1667 int kvm_set_routing_entry(struct kvm *kvm,
1668 struct kvm_kernel_irq_routing_entry *e,
1669 const struct kvm_irq_routing_entry *ue);
1670 void kvm_free_irq_routing(struct kvm *kvm);
1674 static inline void kvm_free_irq_routing(struct kvm *kvm) {}
1678 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
1680 #ifdef CONFIG_HAVE_KVM_EVENTFD
1682 void kvm_eventfd_init(struct kvm *kvm);
1683 int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
1685 #ifdef CONFIG_HAVE_KVM_IRQFD
1686 int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
1687 void kvm_irqfd_release(struct kvm *kvm);
1688 void kvm_irq_routing_update(struct kvm *);
1690 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
1695 static inline void kvm_irqfd_release(struct kvm *kvm) {}
1700 static inline void kvm_eventfd_init(struct kvm *kvm) {}
1702 static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
1707 static inline void kvm_irqfd_release(struct kvm *kvm) {}
1709 #ifdef CONFIG_HAVE_KVM_IRQCHIP
1710 static inline void kvm_irq_routing_update(struct kvm *kvm)
1715 static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
1720 #endif /* CONFIG_HAVE_KVM_EVENTFD */
1722 void kvm_arch_irq_routing_update(struct kvm *kvm);
1724 static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
1727 * Ensure the rest of the request is published to kvm_check_request's
1728 * caller. Paired with the smp_mb__after_atomic in kvm_check_request.
1731 set_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
1734 static inline bool kvm_request_pending(struct kvm_vcpu *vcpu)
1736 return READ_ONCE(vcpu->requests);
1739 static inline bool kvm_test_request(int req, struct kvm_vcpu *vcpu)
1741 return test_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
1744 static inline void kvm_clear_request(int req, struct kvm_vcpu *vcpu)
1746 clear_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
1749 static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
1751 if (kvm_test_request(req, vcpu)) {
1752 kvm_clear_request(req, vcpu);
1755 * Ensure the rest of the request is visible to kvm_check_request's
1756 * caller. Paired with the smp_wmb in kvm_make_request.
1758 smp_mb__after_atomic();
1765 extern bool kvm_rebooting;
1767 extern unsigned int halt_poll_ns;
1768 extern unsigned int halt_poll_ns_grow;
1769 extern unsigned int halt_poll_ns_grow_start;
1770 extern unsigned int halt_poll_ns_shrink;
1773 const struct kvm_device_ops *ops;
1776 struct list_head vm_node;
1779 /* create, destroy, and name are mandatory */
1780 struct kvm_device_ops {
1784 * create is called holding kvm->lock and any operations not suitable
1785 * to do while holding the lock should be deferred to init (see
1788 int (*create)(struct kvm_device *dev, u32 type);
1791 * init is called after create if create is successful and is called
1792 * outside of holding kvm->lock.
1794 void (*init)(struct kvm_device *dev);
1797 * Destroy is responsible for freeing dev.
1799 * Destroy may be called before or after destructors are called
1800 * on emulated I/O regions, depending on whether a reference is
1801 * held by a vcpu or other kvm component that gets destroyed
1802 * after the emulated I/O.
1804 void (*destroy)(struct kvm_device *dev);
1807 * Release is an alternative method to free the device. It is
1808 * called when the device file descriptor is closed. Once
1809 * release is called, the destroy method will not be called
1810 * anymore as the device is removed from the device list of
1811 * the VM. kvm->lock is held.
1813 void (*release)(struct kvm_device *dev);
1815 int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1816 int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1817 int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
1818 long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
1820 int (*mmap)(struct kvm_device *dev, struct vm_area_struct *vma);
1823 void kvm_device_get(struct kvm_device *dev);
1824 void kvm_device_put(struct kvm_device *dev);
1825 struct kvm_device *kvm_device_from_filp(struct file *filp);
1826 int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type);
1827 void kvm_unregister_device_ops(u32 type);
1829 extern struct kvm_device_ops kvm_mpic_ops;
1830 extern struct kvm_device_ops kvm_arm_vgic_v2_ops;
1831 extern struct kvm_device_ops kvm_arm_vgic_v3_ops;
1833 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
1835 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1837 vcpu->spin_loop.in_spin_loop = val;
1839 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1841 vcpu->spin_loop.dy_eligible = val;
1844 #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1846 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
1850 static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
1853 #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
1855 static inline bool kvm_is_visible_memslot(struct kvm_memory_slot *memslot)
1857 return (memslot && memslot->id < KVM_USER_MEM_SLOTS &&
1858 !(memslot->flags & KVM_MEMSLOT_INVALID));
1861 struct kvm_vcpu *kvm_get_running_vcpu(void);
1862 struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void);
1864 #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
1865 bool kvm_arch_has_irq_bypass(void);
1866 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *,
1867 struct irq_bypass_producer *);
1868 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *,
1869 struct irq_bypass_producer *);
1870 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *);
1871 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *);
1872 int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
1873 uint32_t guest_irq, bool set);
1874 #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */
1876 #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS
1877 /* If we wakeup during the poll time, was it a sucessful poll? */
1878 static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
1880 return vcpu->valid_wakeup;
1884 static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
1888 #endif /* CONFIG_HAVE_KVM_INVALID_WAKEUPS */
1890 #ifdef CONFIG_HAVE_KVM_NO_POLL
1891 /* Callback that tells if we must not poll */
1892 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu);
1894 static inline bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
1898 #endif /* CONFIG_HAVE_KVM_NO_POLL */
1900 #ifdef CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL
1901 long kvm_arch_vcpu_async_ioctl(struct file *filp,
1902 unsigned int ioctl, unsigned long arg);
1904 static inline long kvm_arch_vcpu_async_ioctl(struct file *filp,
1908 return -ENOIOCTLCMD;
1910 #endif /* CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL */
1912 void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
1913 unsigned long start, unsigned long end);
1915 #ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE
1916 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu);
1918 static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
1922 #endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */
1924 typedef int (*kvm_vm_thread_fn_t)(struct kvm *kvm, uintptr_t data);
1926 int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
1927 uintptr_t data, const char *name,
1928 struct task_struct **thread_ptr);
1930 #ifdef CONFIG_KVM_XFER_TO_GUEST_WORK
1931 static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu)
1933 vcpu->run->exit_reason = KVM_EXIT_INTR;
1934 vcpu->stat.signal_exits++;
1936 #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */
1939 * This defines how many reserved entries we want to keep before we
1940 * kick the vcpu to the userspace to avoid dirty ring full. This
1941 * value can be tuned to higher if e.g. PML is enabled on the host.
1943 #define KVM_DIRTY_RING_RSVD_ENTRIES 64
1945 /* Max number of entries allowed for each kvm dirty ring */
1946 #define KVM_DIRTY_RING_MAX_ENTRIES 65536