kvm_host.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #ifndef __KVM_HOST_H
  2. #define __KVM_HOST_H
  3. /*
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/hardirq.h>
  9. #include <linux/list.h>
  10. #include <linux/mutex.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/preempt.h>
  16. #include <linux/msi.h>
  17. #include <linux/slab.h>
  18. #include <asm/signal.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_para.h>
  21. #include <linux/kvm_types.h>
  22. #include <asm/kvm_host.h>
  23. /*
  24. * vcpu->requests bit members
  25. */
  26. #define KVM_REQ_TLB_FLUSH 0
  27. #define KVM_REQ_MIGRATE_TIMER 1
  28. #define KVM_REQ_REPORT_TPR_ACCESS 2
  29. #define KVM_REQ_MMU_RELOAD 3
  30. #define KVM_REQ_TRIPLE_FAULT 4
  31. #define KVM_REQ_PENDING_TIMER 5
  32. #define KVM_REQ_UNHALT 6
  33. #define KVM_REQ_MMU_SYNC 7
  34. #define KVM_REQ_CLOCK_UPDATE 8
  35. #define KVM_REQ_KICK 9
  36. #define KVM_REQ_DEACTIVATE_FPU 10
  37. #define KVM_REQ_EVENT 11
  38. #define KVM_REQ_APF_HALT 12
  39. #define KVM_USERSPACE_IRQ_SOURCE_ID 0
  40. struct kvm;
  41. struct kvm_vcpu;
  42. extern struct kmem_cache *kvm_vcpu_cache;
  43. /*
  44. * It would be nice to use something smarter than a linear search, TBD...
  45. * Thankfully we dont expect many devices to register (famous last words :),
  46. * so until then it will suffice. At least its abstracted so we can change
  47. * in one place.
  48. */
  49. struct kvm_io_bus {
  50. int dev_count;
  51. #define NR_IOBUS_DEVS 200
  52. struct kvm_io_device *devs[NR_IOBUS_DEVS];
  53. };
  54. enum kvm_bus {
  55. KVM_MMIO_BUS,
  56. KVM_PIO_BUS,
  57. KVM_NR_BUSES
  58. };
  59. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  60. int len, const void *val);
  61. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
  62. void *val);
  63. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  64. struct kvm_io_device *dev);
  65. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  66. struct kvm_io_device *dev);
  67. #ifdef CONFIG_KVM_ASYNC_PF
  68. struct kvm_async_pf {
  69. struct work_struct work;
  70. struct list_head link;
  71. struct list_head queue;
  72. struct kvm_vcpu *vcpu;
  73. struct mm_struct *mm;
  74. gva_t gva;
  75. unsigned long addr;
  76. struct kvm_arch_async_pf arch;
  77. struct page *page;
  78. bool done;
  79. };
  80. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
  81. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
  82. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  83. struct kvm_arch_async_pf *arch);
  84. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
  85. #endif
  86. struct kvm_vcpu {
  87. struct kvm *kvm;
  88. #ifdef CONFIG_PREEMPT_NOTIFIERS
  89. struct preempt_notifier preempt_notifier;
  90. #endif
  91. int vcpu_id;
  92. struct mutex mutex;
  93. int cpu;
  94. atomic_t guest_mode;
  95. struct kvm_run *run;
  96. unsigned long requests;
  97. unsigned long guest_debug;
  98. int srcu_idx;
  99. int fpu_active;
  100. int guest_fpu_loaded, guest_xcr0_loaded;
  101. wait_queue_head_t wq;
  102. int sigset_active;
  103. sigset_t sigset;
  104. struct kvm_vcpu_stat stat;
  105. #ifdef CONFIG_HAS_IOMEM
  106. int mmio_needed;
  107. int mmio_read_completed;
  108. int mmio_is_write;
  109. int mmio_size;
  110. unsigned char mmio_data[8];
  111. gpa_t mmio_phys_addr;
  112. #endif
  113. #ifdef CONFIG_KVM_ASYNC_PF
  114. struct {
  115. u32 queued;
  116. struct list_head queue;
  117. struct list_head done;
  118. spinlock_t lock;
  119. } async_pf;
  120. #endif
  121. struct kvm_vcpu_arch arch;
  122. };
  123. /*
  124. * Some of the bitops functions do not support too long bitmaps.
  125. * This number must be determined not to exceed such limits.
  126. */
  127. #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
  128. struct kvm_memory_slot {
  129. gfn_t base_gfn;
  130. unsigned long npages;
  131. unsigned long flags;
  132. unsigned long *rmap;
  133. unsigned long *dirty_bitmap;
  134. unsigned long *dirty_bitmap_head;
  135. struct {
  136. unsigned long rmap_pde;
  137. int write_count;
  138. } *lpage_info[KVM_NR_PAGE_SIZES - 1];
  139. unsigned long userspace_addr;
  140. int user_alloc;
  141. int id;
  142. };
  143. static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
  144. {
  145. return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  146. }
  147. struct kvm_kernel_irq_routing_entry {
  148. u32 gsi;
  149. u32 type;
  150. int (*set)(struct kvm_kernel_irq_routing_entry *e,
  151. struct kvm *kvm, int irq_source_id, int level);
  152. union {
  153. struct {
  154. unsigned irqchip;
  155. unsigned pin;
  156. } irqchip;
  157. struct msi_msg msi;
  158. };
  159. struct hlist_node link;
  160. };
  161. #ifdef __KVM_HAVE_IOAPIC
  162. struct kvm_irq_routing_table {
  163. int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS];
  164. struct kvm_kernel_irq_routing_entry *rt_entries;
  165. u32 nr_rt_entries;
  166. /*
  167. * Array indexed by gsi. Each entry contains list of irq chips
  168. * the gsi is connected to.
  169. */
  170. struct hlist_head map[0];
  171. };
  172. #else
  173. struct kvm_irq_routing_table {};
  174. #endif
  175. struct kvm_memslots {
  176. int nmemslots;
  177. u64 generation;
  178. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
  179. KVM_PRIVATE_MEM_SLOTS];
  180. };
  181. struct kvm {
  182. spinlock_t mmu_lock;
  183. raw_spinlock_t requests_lock;
  184. struct mutex slots_lock;
  185. struct mm_struct *mm; /* userspace tied to this vm */
  186. struct kvm_memslots *memslots;
  187. struct srcu_struct srcu;
  188. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  189. u32 bsp_vcpu_id;
  190. struct kvm_vcpu *bsp_vcpu;
  191. #endif
  192. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  193. atomic_t online_vcpus;
  194. struct list_head vm_list;
  195. struct mutex lock;
  196. struct kvm_io_bus *buses[KVM_NR_BUSES];
  197. #ifdef CONFIG_HAVE_KVM_EVENTFD
  198. struct {
  199. spinlock_t lock;
  200. struct list_head items;
  201. } irqfds;
  202. struct list_head ioeventfds;
  203. #endif
  204. struct kvm_vm_stat stat;
  205. struct kvm_arch arch;
  206. atomic_t users_count;
  207. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  208. struct kvm_coalesced_mmio_dev *coalesced_mmio_dev;
  209. struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
  210. #endif
  211. struct mutex irq_lock;
  212. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  213. struct kvm_irq_routing_table __rcu *irq_routing;
  214. struct hlist_head mask_notifier_list;
  215. struct hlist_head irq_ack_notifier_list;
  216. #endif
  217. #ifdef KVM_ARCH_WANT_MMU_NOTIFIER
  218. struct mmu_notifier mmu_notifier;
  219. unsigned long mmu_notifier_seq;
  220. long mmu_notifier_count;
  221. #endif
  222. };
  223. /* The guest did something we don't support. */
  224. #define pr_unimpl(vcpu, fmt, ...) \
  225. do { \
  226. if (printk_ratelimit()) \
  227. printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
  228. current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
  229. } while (0)
  230. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  231. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  232. static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
  233. {
  234. smp_rmb();
  235. return kvm->vcpus[i];
  236. }
  237. #define kvm_for_each_vcpu(idx, vcpup, kvm) \
  238. for (idx = 0, vcpup = kvm_get_vcpu(kvm, idx); \
  239. idx < atomic_read(&kvm->online_vcpus) && vcpup; \
  240. vcpup = kvm_get_vcpu(kvm, ++idx))
  241. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  242. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  243. void vcpu_load(struct kvm_vcpu *vcpu);
  244. void vcpu_put(struct kvm_vcpu *vcpu);
  245. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  246. struct module *module);
  247. void kvm_exit(void);
  248. void kvm_get_kvm(struct kvm *kvm);
  249. void kvm_put_kvm(struct kvm *kvm);
  250. static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
  251. {
  252. return rcu_dereference_check(kvm->memslots,
  253. srcu_read_lock_held(&kvm->srcu)
  254. || lockdep_is_held(&kvm->slots_lock));
  255. }
  256. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  257. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  258. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  259. extern struct page *bad_page;
  260. extern pfn_t bad_pfn;
  261. int is_error_page(struct page *page);
  262. int is_error_pfn(pfn_t pfn);
  263. int is_hwpoison_pfn(pfn_t pfn);
  264. int is_fault_pfn(pfn_t pfn);
  265. int kvm_is_error_hva(unsigned long addr);
  266. int kvm_set_memory_region(struct kvm *kvm,
  267. struct kvm_userspace_memory_region *mem,
  268. int user_alloc);
  269. int __kvm_set_memory_region(struct kvm *kvm,
  270. struct kvm_userspace_memory_region *mem,
  271. int user_alloc);
  272. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  273. struct kvm_memory_slot *memslot,
  274. struct kvm_memory_slot old,
  275. struct kvm_userspace_memory_region *mem,
  276. int user_alloc);
  277. void kvm_arch_commit_memory_region(struct kvm *kvm,
  278. struct kvm_userspace_memory_region *mem,
  279. struct kvm_memory_slot old,
  280. int user_alloc);
  281. void kvm_disable_largepages(void);
  282. void kvm_arch_flush_shadow(struct kvm *kvm);
  283. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  284. int nr_pages);
  285. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  286. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
  287. void kvm_release_page_clean(struct page *page);
  288. void kvm_release_page_dirty(struct page *page);
  289. void kvm_set_page_dirty(struct page *page);
  290. void kvm_set_page_accessed(struct page *page);
  291. pfn_t hva_to_pfn_atomic(struct kvm *kvm, unsigned long addr);
  292. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
  293. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  294. bool write_fault, bool *writable);
  295. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
  296. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  297. bool *writable);
  298. pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
  299. struct kvm_memory_slot *slot, gfn_t gfn);
  300. int memslot_id(struct kvm *kvm, gfn_t gfn);
  301. void kvm_release_pfn_dirty(pfn_t);
  302. void kvm_release_pfn_clean(pfn_t pfn);
  303. void kvm_set_pfn_dirty(pfn_t pfn);
  304. void kvm_set_pfn_accessed(pfn_t pfn);
  305. void kvm_get_pfn(pfn_t pfn);
  306. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  307. int len);
  308. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  309. unsigned long len);
  310. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  311. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  312. int offset, int len);
  313. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  314. unsigned long len);
  315. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  316. void *data, unsigned long len);
  317. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  318. gpa_t gpa);
  319. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  320. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  321. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  322. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
  323. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
  324. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  325. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  326. gfn_t gfn);
  327. void kvm_vcpu_block(struct kvm_vcpu *vcpu);
  328. void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
  329. void kvm_resched(struct kvm_vcpu *vcpu);
  330. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  331. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  332. void kvm_flush_remote_tlbs(struct kvm *kvm);
  333. void kvm_reload_remote_mmus(struct kvm *kvm);
  334. long kvm_arch_dev_ioctl(struct file *filp,
  335. unsigned int ioctl, unsigned long arg);
  336. long kvm_arch_vcpu_ioctl(struct file *filp,
  337. unsigned int ioctl, unsigned long arg);
  338. int kvm_dev_ioctl_check_extension(long ext);
  339. int kvm_get_dirty_log(struct kvm *kvm,
  340. struct kvm_dirty_log *log, int *is_dirty);
  341. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  342. struct kvm_dirty_log *log);
  343. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  344. struct
  345. kvm_userspace_memory_region *mem,
  346. int user_alloc);
  347. long kvm_arch_vm_ioctl(struct file *filp,
  348. unsigned int ioctl, unsigned long arg);
  349. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  350. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  351. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  352. struct kvm_translation *tr);
  353. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  354. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  355. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  356. struct kvm_sregs *sregs);
  357. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  358. struct kvm_sregs *sregs);
  359. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  360. struct kvm_mp_state *mp_state);
  361. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  362. struct kvm_mp_state *mp_state);
  363. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  364. struct kvm_guest_debug *dbg);
  365. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  366. int kvm_arch_init(void *opaque);
  367. void kvm_arch_exit(void);
  368. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  369. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  370. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  371. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  372. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  373. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  374. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
  375. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
  376. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
  377. int kvm_arch_hardware_enable(void *garbage);
  378. void kvm_arch_hardware_disable(void *garbage);
  379. int kvm_arch_hardware_setup(void);
  380. void kvm_arch_hardware_unsetup(void);
  381. void kvm_arch_check_processor_compat(void *rtn);
  382. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
  383. void kvm_free_physmem(struct kvm *kvm);
  384. #ifndef __KVM_HAVE_ARCH_VM_ALLOC
  385. static inline struct kvm *kvm_arch_alloc_vm(void)
  386. {
  387. return kzalloc(sizeof(struct kvm), GFP_KERNEL);
  388. }
  389. static inline void kvm_arch_free_vm(struct kvm *kvm)
  390. {
  391. kfree(kvm);
  392. }
  393. #endif
  394. int kvm_arch_init_vm(struct kvm *kvm);
  395. void kvm_arch_destroy_vm(struct kvm *kvm);
  396. void kvm_free_all_assigned_devices(struct kvm *kvm);
  397. void kvm_arch_sync_events(struct kvm *kvm);
  398. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
  399. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  400. int kvm_is_mmio_pfn(pfn_t pfn);
  401. struct kvm_irq_ack_notifier {
  402. struct hlist_node link;
  403. unsigned gsi;
  404. void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
  405. };
  406. #define KVM_ASSIGNED_MSIX_PENDING 0x1
  407. struct kvm_guest_msix_entry {
  408. u32 vector;
  409. u16 entry;
  410. u16 flags;
  411. };
  412. struct kvm_assigned_dev_kernel {
  413. struct kvm_irq_ack_notifier ack_notifier;
  414. struct work_struct interrupt_work;
  415. struct list_head list;
  416. int assigned_dev_id;
  417. int host_segnr;
  418. int host_busnr;
  419. int host_devfn;
  420. unsigned int entries_nr;
  421. int host_irq;
  422. bool host_irq_disabled;
  423. struct msix_entry *host_msix_entries;
  424. int guest_irq;
  425. struct kvm_guest_msix_entry *guest_msix_entries;
  426. unsigned long irq_requested_type;
  427. int irq_source_id;
  428. int flags;
  429. struct pci_dev *dev;
  430. struct kvm *kvm;
  431. spinlock_t assigned_dev_lock;
  432. };
  433. struct kvm_irq_mask_notifier {
  434. void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
  435. int irq;
  436. struct hlist_node link;
  437. };
  438. void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
  439. struct kvm_irq_mask_notifier *kimn);
  440. void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
  441. struct kvm_irq_mask_notifier *kimn);
  442. void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
  443. bool mask);
  444. #ifdef __KVM_HAVE_IOAPIC
  445. void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
  446. union kvm_ioapic_redirect_entry *entry,
  447. unsigned long *deliver_bitmask);
  448. #endif
  449. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
  450. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
  451. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  452. struct kvm_irq_ack_notifier *kian);
  453. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  454. struct kvm_irq_ack_notifier *kian);
  455. int kvm_request_irq_source_id(struct kvm *kvm);
  456. void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
  457. /* For vcpu->arch.iommu_flags */
  458. #define KVM_IOMMU_CACHE_COHERENCY 0x1
  459. #ifdef CONFIG_IOMMU_API
  460. int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
  461. int kvm_iommu_map_guest(struct kvm *kvm);
  462. int kvm_iommu_unmap_guest(struct kvm *kvm);
  463. int kvm_assign_device(struct kvm *kvm,
  464. struct kvm_assigned_dev_kernel *assigned_dev);
  465. int kvm_deassign_device(struct kvm *kvm,
  466. struct kvm_assigned_dev_kernel *assigned_dev);
  467. #else /* CONFIG_IOMMU_API */
  468. static inline int kvm_iommu_map_pages(struct kvm *kvm,
  469. struct kvm_memory_slot *slot)
  470. {
  471. return 0;
  472. }
  473. static inline int kvm_iommu_map_guest(struct kvm *kvm)
  474. {
  475. return -ENODEV;
  476. }
  477. static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
  478. {
  479. return 0;
  480. }
  481. static inline int kvm_assign_device(struct kvm *kvm,
  482. struct kvm_assigned_dev_kernel *assigned_dev)
  483. {
  484. return 0;
  485. }
  486. static inline int kvm_deassign_device(struct kvm *kvm,
  487. struct kvm_assigned_dev_kernel *assigned_dev)
  488. {
  489. return 0;
  490. }
  491. #endif /* CONFIG_IOMMU_API */
  492. static inline void kvm_guest_enter(void)
  493. {
  494. account_system_vtime(current);
  495. current->flags |= PF_VCPU;
  496. }
  497. static inline void kvm_guest_exit(void)
  498. {
  499. account_system_vtime(current);
  500. current->flags &= ~PF_VCPU;
  501. }
  502. static inline unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  503. gfn_t gfn)
  504. {
  505. return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
  506. }
  507. static inline gpa_t gfn_to_gpa(gfn_t gfn)
  508. {
  509. return (gpa_t)gfn << PAGE_SHIFT;
  510. }
  511. static inline gfn_t gpa_to_gfn(gpa_t gpa)
  512. {
  513. return (gfn_t)(gpa >> PAGE_SHIFT);
  514. }
  515. static inline hpa_t pfn_to_hpa(pfn_t pfn)
  516. {
  517. return (hpa_t)pfn << PAGE_SHIFT;
  518. }
  519. static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
  520. {
  521. set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
  522. }
  523. enum kvm_stat_kind {
  524. KVM_STAT_VM,
  525. KVM_STAT_VCPU,
  526. };
  527. struct kvm_stats_debugfs_item {
  528. const char *name;
  529. int offset;
  530. enum kvm_stat_kind kind;
  531. struct dentry *dentry;
  532. };
  533. extern struct kvm_stats_debugfs_item debugfs_entries[];
  534. extern struct dentry *kvm_debugfs_dir;
  535. #ifdef KVM_ARCH_WANT_MMU_NOTIFIER
  536. static inline int mmu_notifier_retry(struct kvm_vcpu *vcpu, unsigned long mmu_seq)
  537. {
  538. if (unlikely(vcpu->kvm->mmu_notifier_count))
  539. return 1;
  540. /*
  541. * Both reads happen under the mmu_lock and both values are
  542. * modified under mmu_lock, so there's no need of smb_rmb()
  543. * here in between, otherwise mmu_notifier_count should be
  544. * read before mmu_notifier_seq, see
  545. * mmu_notifier_invalidate_range_end write side.
  546. */
  547. if (vcpu->kvm->mmu_notifier_seq != mmu_seq)
  548. return 1;
  549. return 0;
  550. }
  551. #endif
  552. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  553. #define KVM_MAX_IRQ_ROUTES 1024
  554. int kvm_setup_default_irq_routing(struct kvm *kvm);
  555. int kvm_set_irq_routing(struct kvm *kvm,
  556. const struct kvm_irq_routing_entry *entries,
  557. unsigned nr,
  558. unsigned flags);
  559. void kvm_free_irq_routing(struct kvm *kvm);
  560. #else
  561. static inline void kvm_free_irq_routing(struct kvm *kvm) {}
  562. #endif
  563. #ifdef CONFIG_HAVE_KVM_EVENTFD
  564. void kvm_eventfd_init(struct kvm *kvm);
  565. int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags);
  566. void kvm_irqfd_release(struct kvm *kvm);
  567. int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
  568. #else
  569. static inline void kvm_eventfd_init(struct kvm *kvm) {}
  570. static inline int kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
  571. {
  572. return -EINVAL;
  573. }
  574. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  575. static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  576. {
  577. return -ENOSYS;
  578. }
  579. #endif /* CONFIG_HAVE_KVM_EVENTFD */
  580. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  581. static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
  582. {
  583. return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
  584. }
  585. #endif
  586. #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
  587. long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  588. unsigned long arg);
  589. #else
  590. static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  591. unsigned long arg)
  592. {
  593. return -ENOTTY;
  594. }
  595. #endif
  596. static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
  597. {
  598. set_bit(req, &vcpu->requests);
  599. }
  600. static inline bool kvm_make_check_request(int req, struct kvm_vcpu *vcpu)
  601. {
  602. return test_and_set_bit(req, &vcpu->requests);
  603. }
  604. static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
  605. {
  606. if (test_bit(req, &vcpu->requests)) {
  607. clear_bit(req, &vcpu->requests);
  608. return true;
  609. } else {
  610. return false;
  611. }
  612. }
  613. #endif