kvm_host.h 18 KB

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