kvm_host.h 21 KB

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