kvm_host.h 21 KB

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