kvm_host.h 20 KB

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