kvm_host.h 20 KB

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