kvm_host.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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/bug.h>
  15. #include <linux/mm.h>
  16. #include <linux/mmu_notifier.h>
  17. #include <linux/preempt.h>
  18. #include <linux/msi.h>
  19. #include <linux/slab.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/err.h>
  23. #include <linux/irqflags.h>
  24. #include <linux/context_tracking.h>
  25. #include <linux/irqbypass.h>
  26. #include <linux/swait.h>
  27. #include <asm/signal.h>
  28. #include <linux/kvm.h>
  29. #include <linux/kvm_para.h>
  30. #include <linux/kvm_types.h>
  31. #include <asm/kvm_host.h>
  32. #ifndef KVM_MAX_VCPU_ID
  33. #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
  34. #endif
  35. /*
  36. * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
  37. * in kvm, other bits are visible for userspace which are defined in
  38. * include/linux/kvm_h.
  39. */
  40. #define KVM_MEMSLOT_INVALID (1UL << 16)
  41. #define KVM_MEMSLOT_INCOHERENT (1UL << 17)
  42. /* Two fragments for cross MMIO pages. */
  43. #define KVM_MAX_MMIO_FRAGMENTS 2
  44. #ifndef KVM_ADDRESS_SPACE_NUM
  45. #define KVM_ADDRESS_SPACE_NUM 1
  46. #endif
  47. /*
  48. * For the normal pfn, the highest 12 bits should be zero,
  49. * so we can mask bit 62 ~ bit 52 to indicate the error pfn,
  50. * mask bit 63 to indicate the noslot pfn.
  51. */
  52. #define KVM_PFN_ERR_MASK (0x7ffULL << 52)
  53. #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
  54. #define KVM_PFN_NOSLOT (0x1ULL << 63)
  55. #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK)
  56. #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
  57. #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)
  58. /*
  59. * error pfns indicate that the gfn is in slot but faild to
  60. * translate it to pfn on host.
  61. */
  62. static inline bool is_error_pfn(kvm_pfn_t pfn)
  63. {
  64. return !!(pfn & KVM_PFN_ERR_MASK);
  65. }
  66. /*
  67. * error_noslot pfns indicate that the gfn can not be
  68. * translated to pfn - it is not in slot or failed to
  69. * translate it to pfn.
  70. */
  71. static inline bool is_error_noslot_pfn(kvm_pfn_t pfn)
  72. {
  73. return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
  74. }
  75. /* noslot pfn indicates that the gfn is not in slot. */
  76. static inline bool is_noslot_pfn(kvm_pfn_t pfn)
  77. {
  78. return pfn == KVM_PFN_NOSLOT;
  79. }
  80. /*
  81. * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390)
  82. * provide own defines and kvm_is_error_hva
  83. */
  84. #ifndef KVM_HVA_ERR_BAD
  85. #define KVM_HVA_ERR_BAD (PAGE_OFFSET)
  86. #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE)
  87. static inline bool kvm_is_error_hva(unsigned long addr)
  88. {
  89. return addr >= PAGE_OFFSET;
  90. }
  91. #endif
  92. #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT))
  93. static inline bool is_error_page(struct page *page)
  94. {
  95. return IS_ERR(page);
  96. }
  97. /*
  98. * Architecture-independent vcpu->requests bit members
  99. * Bits 4-7 are reserved for more arch-independent bits.
  100. */
  101. #define KVM_REQ_TLB_FLUSH 0
  102. #define KVM_REQ_MMU_RELOAD 1
  103. #define KVM_REQ_PENDING_TIMER 2
  104. #define KVM_REQ_UNHALT 3
  105. #define KVM_USERSPACE_IRQ_SOURCE_ID 0
  106. #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
  107. extern struct kmem_cache *kvm_vcpu_cache;
  108. extern spinlock_t kvm_lock;
  109. extern struct list_head vm_list;
  110. struct kvm_io_range {
  111. gpa_t addr;
  112. int len;
  113. struct kvm_io_device *dev;
  114. };
  115. #define NR_IOBUS_DEVS 1000
  116. struct kvm_io_bus {
  117. int dev_count;
  118. int ioeventfd_count;
  119. struct kvm_io_range range[];
  120. };
  121. enum kvm_bus {
  122. KVM_MMIO_BUS,
  123. KVM_PIO_BUS,
  124. KVM_VIRTIO_CCW_NOTIFY_BUS,
  125. KVM_FAST_MMIO_BUS,
  126. KVM_NR_BUSES
  127. };
  128. int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  129. int len, const void *val);
  130. int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
  131. gpa_t addr, int len, const void *val, long cookie);
  132. int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  133. int len, void *val);
  134. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  135. int len, struct kvm_io_device *dev);
  136. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  137. struct kvm_io_device *dev);
  138. #ifdef CONFIG_KVM_ASYNC_PF
  139. struct kvm_async_pf {
  140. struct work_struct work;
  141. struct list_head link;
  142. struct list_head queue;
  143. struct kvm_vcpu *vcpu;
  144. struct mm_struct *mm;
  145. gva_t gva;
  146. unsigned long addr;
  147. struct kvm_arch_async_pf arch;
  148. bool wakeup_all;
  149. };
  150. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
  151. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
  152. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,
  153. struct kvm_arch_async_pf *arch);
  154. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
  155. #endif
  156. enum {
  157. OUTSIDE_GUEST_MODE,
  158. IN_GUEST_MODE,
  159. EXITING_GUEST_MODE,
  160. READING_SHADOW_PAGE_TABLES,
  161. };
  162. /*
  163. * Sometimes a large or cross-page mmio needs to be broken up into separate
  164. * exits for userspace servicing.
  165. */
  166. struct kvm_mmio_fragment {
  167. gpa_t gpa;
  168. void *data;
  169. unsigned len;
  170. };
  171. struct kvm_vcpu {
  172. struct kvm *kvm;
  173. #ifdef CONFIG_PREEMPT_NOTIFIERS
  174. struct preempt_notifier preempt_notifier;
  175. #endif
  176. int cpu;
  177. int vcpu_id;
  178. int srcu_idx;
  179. int mode;
  180. unsigned long requests;
  181. unsigned long guest_debug;
  182. int pre_pcpu;
  183. struct list_head blocked_vcpu_list;
  184. struct mutex mutex;
  185. struct kvm_run *run;
  186. int fpu_active;
  187. int guest_fpu_loaded, guest_xcr0_loaded;
  188. unsigned char fpu_counter;
  189. struct swait_queue_head wq;
  190. struct pid *pid;
  191. int sigset_active;
  192. sigset_t sigset;
  193. struct kvm_vcpu_stat stat;
  194. unsigned int halt_poll_ns;
  195. bool valid_wakeup;
  196. #ifdef CONFIG_HAS_IOMEM
  197. int mmio_needed;
  198. int mmio_read_completed;
  199. int mmio_is_write;
  200. int mmio_cur_fragment;
  201. int mmio_nr_fragments;
  202. struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS];
  203. #endif
  204. #ifdef CONFIG_KVM_ASYNC_PF
  205. struct {
  206. u32 queued;
  207. struct list_head queue;
  208. struct list_head done;
  209. spinlock_t lock;
  210. } async_pf;
  211. #endif
  212. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  213. /*
  214. * Cpu relax intercept or pause loop exit optimization
  215. * in_spin_loop: set when a vcpu does a pause loop exit
  216. * or cpu relax intercepted.
  217. * dy_eligible: indicates whether vcpu is eligible for directed yield.
  218. */
  219. struct {
  220. bool in_spin_loop;
  221. bool dy_eligible;
  222. } spin_loop;
  223. #endif
  224. bool preempted;
  225. struct kvm_vcpu_arch arch;
  226. };
  227. static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
  228. {
  229. return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
  230. }
  231. /*
  232. * Some of the bitops functions do not support too long bitmaps.
  233. * This number must be determined not to exceed such limits.
  234. */
  235. #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
  236. struct kvm_memory_slot {
  237. gfn_t base_gfn;
  238. unsigned long npages;
  239. unsigned long *dirty_bitmap;
  240. struct kvm_arch_memory_slot arch;
  241. unsigned long userspace_addr;
  242. u32 flags;
  243. short id;
  244. };
  245. static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
  246. {
  247. return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  248. }
  249. struct kvm_s390_adapter_int {
  250. u64 ind_addr;
  251. u64 summary_addr;
  252. u64 ind_offset;
  253. u32 summary_offset;
  254. u32 adapter_id;
  255. };
  256. struct kvm_hv_sint {
  257. u32 vcpu;
  258. u32 sint;
  259. };
  260. struct kvm_kernel_irq_routing_entry {
  261. u32 gsi;
  262. u32 type;
  263. int (*set)(struct kvm_kernel_irq_routing_entry *e,
  264. struct kvm *kvm, int irq_source_id, int level,
  265. bool line_status);
  266. union {
  267. struct {
  268. unsigned irqchip;
  269. unsigned pin;
  270. } irqchip;
  271. struct msi_msg msi;
  272. struct kvm_s390_adapter_int adapter;
  273. struct kvm_hv_sint hv_sint;
  274. };
  275. struct hlist_node link;
  276. };
  277. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  278. struct kvm_irq_routing_table {
  279. int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
  280. u32 nr_rt_entries;
  281. /*
  282. * Array indexed by gsi. Each entry contains list of irq chips
  283. * the gsi is connected to.
  284. */
  285. struct hlist_head map[0];
  286. };
  287. #endif
  288. #ifndef KVM_PRIVATE_MEM_SLOTS
  289. #define KVM_PRIVATE_MEM_SLOTS 0
  290. #endif
  291. #ifndef KVM_MEM_SLOTS_NUM
  292. #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  293. #endif
  294. #ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
  295. static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
  296. {
  297. return 0;
  298. }
  299. #endif
  300. /*
  301. * Note:
  302. * memslots are not sorted by id anymore, please use id_to_memslot()
  303. * to get the memslot by its id.
  304. */
  305. struct kvm_memslots {
  306. u64 generation;
  307. struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
  308. /* The mapping table from slot id to the index in memslots[]. */
  309. short id_to_index[KVM_MEM_SLOTS_NUM];
  310. atomic_t lru_slot;
  311. int used_slots;
  312. };
  313. struct kvm {
  314. spinlock_t mmu_lock;
  315. struct mutex slots_lock;
  316. struct mm_struct *mm; /* userspace tied to this vm */
  317. struct kvm_memslots *memslots[KVM_ADDRESS_SPACE_NUM];
  318. struct srcu_struct srcu;
  319. struct srcu_struct irq_srcu;
  320. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  321. /*
  322. * created_vcpus is protected by kvm->lock, and is incremented
  323. * at the beginning of KVM_CREATE_VCPU. online_vcpus is only
  324. * incremented after storing the kvm_vcpu pointer in vcpus,
  325. * and is accessed atomically.
  326. */
  327. atomic_t online_vcpus;
  328. int created_vcpus;
  329. int last_boosted_vcpu;
  330. struct list_head vm_list;
  331. struct mutex lock;
  332. struct kvm_io_bus *buses[KVM_NR_BUSES];
  333. #ifdef CONFIG_HAVE_KVM_EVENTFD
  334. struct {
  335. spinlock_t lock;
  336. struct list_head items;
  337. struct list_head resampler_list;
  338. struct mutex resampler_lock;
  339. } irqfds;
  340. struct list_head ioeventfds;
  341. #endif
  342. struct kvm_vm_stat stat;
  343. struct kvm_arch arch;
  344. atomic_t users_count;
  345. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  346. struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
  347. spinlock_t ring_lock;
  348. struct list_head coalesced_zones;
  349. #endif
  350. struct mutex irq_lock;
  351. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  352. /*
  353. * Update side is protected by irq_lock.
  354. */
  355. struct kvm_irq_routing_table __rcu *irq_routing;
  356. #endif
  357. #ifdef CONFIG_HAVE_KVM_IRQFD
  358. struct hlist_head irq_ack_notifier_list;
  359. #endif
  360. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  361. struct mmu_notifier mmu_notifier;
  362. unsigned long mmu_notifier_seq;
  363. long mmu_notifier_count;
  364. #endif
  365. long tlbs_dirty;
  366. struct list_head devices;
  367. struct dentry *debugfs_dentry;
  368. struct kvm_stat_data **debugfs_stat_data;
  369. };
  370. #define kvm_err(fmt, ...) \
  371. pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  372. #define kvm_info(fmt, ...) \
  373. pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  374. #define kvm_debug(fmt, ...) \
  375. pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  376. #define kvm_pr_unimpl(fmt, ...) \
  377. pr_err_ratelimited("kvm [%i]: " fmt, \
  378. task_tgid_nr(current), ## __VA_ARGS__)
  379. /* The guest did something we don't support. */
  380. #define vcpu_unimpl(vcpu, fmt, ...) \
  381. kvm_pr_unimpl("vcpu%i, guest rIP: 0x%lx " fmt, \
  382. (vcpu)->vcpu_id, kvm_rip_read(vcpu), ## __VA_ARGS__)
  383. #define vcpu_debug(vcpu, fmt, ...) \
  384. kvm_debug("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
  385. #define vcpu_err(vcpu, fmt, ...) \
  386. kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
  387. static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
  388. {
  389. /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu, in case
  390. * the caller has read kvm->online_vcpus before (as is the case
  391. * for kvm_for_each_vcpu, for example).
  392. */
  393. smp_rmb();
  394. return kvm->vcpus[i];
  395. }
  396. #define kvm_for_each_vcpu(idx, vcpup, kvm) \
  397. for (idx = 0; \
  398. idx < atomic_read(&kvm->online_vcpus) && \
  399. (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
  400. idx++)
  401. static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
  402. {
  403. struct kvm_vcpu *vcpu = NULL;
  404. int i;
  405. if (id < 0)
  406. return NULL;
  407. if (id < KVM_MAX_VCPUS)
  408. vcpu = kvm_get_vcpu(kvm, id);
  409. if (vcpu && vcpu->vcpu_id == id)
  410. return vcpu;
  411. kvm_for_each_vcpu(i, vcpu, kvm)
  412. if (vcpu->vcpu_id == id)
  413. return vcpu;
  414. return NULL;
  415. }
  416. #define kvm_for_each_memslot(memslot, slots) \
  417. for (memslot = &slots->memslots[0]; \
  418. memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
  419. memslot++)
  420. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  421. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  422. int __must_check vcpu_load(struct kvm_vcpu *vcpu);
  423. void vcpu_put(struct kvm_vcpu *vcpu);
  424. #ifdef __KVM_HAVE_IOAPIC
  425. void kvm_vcpu_request_scan_ioapic(struct kvm *kvm);
  426. void kvm_arch_post_irq_routing_update(struct kvm *kvm);
  427. #else
  428. static inline void kvm_vcpu_request_scan_ioapic(struct kvm *kvm)
  429. {
  430. }
  431. static inline void kvm_arch_post_irq_routing_update(struct kvm *kvm)
  432. {
  433. }
  434. #endif
  435. #ifdef CONFIG_HAVE_KVM_IRQFD
  436. int kvm_irqfd_init(void);
  437. void kvm_irqfd_exit(void);
  438. #else
  439. static inline int kvm_irqfd_init(void)
  440. {
  441. return 0;
  442. }
  443. static inline void kvm_irqfd_exit(void)
  444. {
  445. }
  446. #endif
  447. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  448. struct module *module);
  449. void kvm_exit(void);
  450. void kvm_get_kvm(struct kvm *kvm);
  451. void kvm_put_kvm(struct kvm *kvm);
  452. static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
  453. {
  454. return rcu_dereference_check(kvm->memslots[as_id],
  455. srcu_read_lock_held(&kvm->srcu)
  456. || lockdep_is_held(&kvm->slots_lock));
  457. }
  458. static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
  459. {
  460. return __kvm_memslots(kvm, 0);
  461. }
  462. static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu)
  463. {
  464. int as_id = kvm_arch_vcpu_memslots_id(vcpu);
  465. return __kvm_memslots(vcpu->kvm, as_id);
  466. }
  467. static inline struct kvm_memory_slot *
  468. id_to_memslot(struct kvm_memslots *slots, int id)
  469. {
  470. int index = slots->id_to_index[id];
  471. struct kvm_memory_slot *slot;
  472. slot = &slots->memslots[index];
  473. WARN_ON(slot->id != id);
  474. return slot;
  475. }
  476. /*
  477. * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
  478. * - create a new memory slot
  479. * - delete an existing memory slot
  480. * - modify an existing memory slot
  481. * -- move it in the guest physical memory space
  482. * -- just change its flags
  483. *
  484. * Since flags can be changed by some of these operations, the following
  485. * differentiation is the best we can do for __kvm_set_memory_region():
  486. */
  487. enum kvm_mr_change {
  488. KVM_MR_CREATE,
  489. KVM_MR_DELETE,
  490. KVM_MR_MOVE,
  491. KVM_MR_FLAGS_ONLY,
  492. };
  493. int kvm_set_memory_region(struct kvm *kvm,
  494. const struct kvm_userspace_memory_region *mem);
  495. int __kvm_set_memory_region(struct kvm *kvm,
  496. const struct kvm_userspace_memory_region *mem);
  497. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  498. struct kvm_memory_slot *dont);
  499. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  500. unsigned long npages);
  501. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots);
  502. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  503. struct kvm_memory_slot *memslot,
  504. const struct kvm_userspace_memory_region *mem,
  505. enum kvm_mr_change change);
  506. void kvm_arch_commit_memory_region(struct kvm *kvm,
  507. const struct kvm_userspace_memory_region *mem,
  508. const struct kvm_memory_slot *old,
  509. const struct kvm_memory_slot *new,
  510. enum kvm_mr_change change);
  511. bool kvm_largepages_enabled(void);
  512. void kvm_disable_largepages(void);
  513. /* flush all memory translations */
  514. void kvm_arch_flush_shadow_all(struct kvm *kvm);
  515. /* flush memory translations pointing to 'slot' */
  516. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  517. struct kvm_memory_slot *slot);
  518. int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  519. struct page **pages, int nr_pages);
  520. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  521. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
  522. unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable);
  523. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  524. unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn,
  525. bool *writable);
  526. void kvm_release_page_clean(struct page *page);
  527. void kvm_release_page_dirty(struct page *page);
  528. void kvm_set_page_accessed(struct page *page);
  529. kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
  530. kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
  531. kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  532. bool *writable);
  533. kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  534. kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
  535. kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
  536. bool atomic, bool *async, bool write_fault,
  537. bool *writable);
  538. void kvm_release_pfn_clean(kvm_pfn_t pfn);
  539. void kvm_set_pfn_dirty(kvm_pfn_t pfn);
  540. void kvm_set_pfn_accessed(kvm_pfn_t pfn);
  541. void kvm_get_pfn(kvm_pfn_t pfn);
  542. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  543. int len);
  544. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  545. unsigned long len);
  546. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  547. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  548. void *data, unsigned long len);
  549. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  550. int offset, int len);
  551. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  552. unsigned long len);
  553. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  554. void *data, unsigned long len);
  555. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  556. gpa_t gpa, unsigned long len);
  557. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  558. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  559. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  560. bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
  561. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
  562. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  563. struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
  564. struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn);
  565. kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn);
  566. kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  567. struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn);
  568. unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
  569. unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
  570. int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset,
  571. int len);
  572. int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
  573. unsigned long len);
  574. int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
  575. unsigned long len);
  576. int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data,
  577. int offset, int len);
  578. int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
  579. unsigned long len);
  580. void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn);
  581. void kvm_vcpu_block(struct kvm_vcpu *vcpu);
  582. void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);
  583. void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
  584. void kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
  585. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  586. int kvm_vcpu_yield_to(struct kvm_vcpu *target);
  587. void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
  588. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  589. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  590. void kvm_flush_remote_tlbs(struct kvm *kvm);
  591. void kvm_reload_remote_mmus(struct kvm *kvm);
  592. bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
  593. long kvm_arch_dev_ioctl(struct file *filp,
  594. unsigned int ioctl, unsigned long arg);
  595. long kvm_arch_vcpu_ioctl(struct file *filp,
  596. unsigned int ioctl, unsigned long arg);
  597. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
  598. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext);
  599. int kvm_get_dirty_log(struct kvm *kvm,
  600. struct kvm_dirty_log *log, int *is_dirty);
  601. int kvm_get_dirty_log_protect(struct kvm *kvm,
  602. struct kvm_dirty_log *log, bool *is_dirty);
  603. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  604. struct kvm_memory_slot *slot,
  605. gfn_t gfn_offset,
  606. unsigned long mask);
  607. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  608. struct kvm_dirty_log *log);
  609. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  610. bool line_status);
  611. long kvm_arch_vm_ioctl(struct file *filp,
  612. unsigned int ioctl, unsigned long arg);
  613. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  614. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  615. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  616. struct kvm_translation *tr);
  617. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  618. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  619. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  620. struct kvm_sregs *sregs);
  621. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  622. struct kvm_sregs *sregs);
  623. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  624. struct kvm_mp_state *mp_state);
  625. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  626. struct kvm_mp_state *mp_state);
  627. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  628. struct kvm_guest_debug *dbg);
  629. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  630. int kvm_arch_init(void *opaque);
  631. void kvm_arch_exit(void);
  632. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  633. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  634. void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu);
  635. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  636. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  637. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  638. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  639. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
  640. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
  641. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
  642. int kvm_arch_hardware_enable(void);
  643. void kvm_arch_hardware_disable(void);
  644. int kvm_arch_hardware_setup(void);
  645. void kvm_arch_hardware_unsetup(void);
  646. void kvm_arch_check_processor_compat(void *rtn);
  647. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
  648. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
  649. void *kvm_kvzalloc(unsigned long size);
  650. #ifndef __KVM_HAVE_ARCH_VM_ALLOC
  651. static inline struct kvm *kvm_arch_alloc_vm(void)
  652. {
  653. return kzalloc(sizeof(struct kvm), GFP_KERNEL);
  654. }
  655. static inline void kvm_arch_free_vm(struct kvm *kvm)
  656. {
  657. kfree(kvm);
  658. }
  659. #endif
  660. #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
  661. void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
  662. void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
  663. bool kvm_arch_has_noncoherent_dma(struct kvm *kvm);
  664. #else
  665. static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
  666. {
  667. }
  668. static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
  669. {
  670. }
  671. static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
  672. {
  673. return false;
  674. }
  675. #endif
  676. #ifdef __KVM_HAVE_ARCH_ASSIGNED_DEVICE
  677. void kvm_arch_start_assignment(struct kvm *kvm);
  678. void kvm_arch_end_assignment(struct kvm *kvm);
  679. bool kvm_arch_has_assigned_device(struct kvm *kvm);
  680. #else
  681. static inline void kvm_arch_start_assignment(struct kvm *kvm)
  682. {
  683. }
  684. static inline void kvm_arch_end_assignment(struct kvm *kvm)
  685. {
  686. }
  687. static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
  688. {
  689. return false;
  690. }
  691. #endif
  692. static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  693. {
  694. #ifdef __KVM_HAVE_ARCH_WQP
  695. return vcpu->arch.wqp;
  696. #else
  697. return &vcpu->wq;
  698. #endif
  699. }
  700. #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED
  701. /*
  702. * returns true if the virtual interrupt controller is initialized and
  703. * ready to accept virtual IRQ. On some architectures the virtual interrupt
  704. * controller is dynamically instantiated and this is not always true.
  705. */
  706. bool kvm_arch_intc_initialized(struct kvm *kvm);
  707. #else
  708. static inline bool kvm_arch_intc_initialized(struct kvm *kvm)
  709. {
  710. return true;
  711. }
  712. #endif
  713. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
  714. void kvm_arch_destroy_vm(struct kvm *kvm);
  715. void kvm_arch_sync_events(struct kvm *kvm);
  716. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
  717. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  718. bool kvm_is_reserved_pfn(kvm_pfn_t pfn);
  719. struct kvm_irq_ack_notifier {
  720. struct hlist_node link;
  721. unsigned gsi;
  722. void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
  723. };
  724. int kvm_irq_map_gsi(struct kvm *kvm,
  725. struct kvm_kernel_irq_routing_entry *entries, int gsi);
  726. int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin);
  727. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  728. bool line_status);
  729. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
  730. int irq_source_id, int level, bool line_status);
  731. int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
  732. struct kvm *kvm, int irq_source_id,
  733. int level, bool line_status);
  734. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
  735. void kvm_notify_acked_gsi(struct kvm *kvm, int gsi);
  736. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
  737. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  738. struct kvm_irq_ack_notifier *kian);
  739. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  740. struct kvm_irq_ack_notifier *kian);
  741. int kvm_request_irq_source_id(struct kvm *kvm);
  742. void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
  743. #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
  744. int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
  745. void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
  746. #else
  747. static inline int kvm_iommu_map_pages(struct kvm *kvm,
  748. struct kvm_memory_slot *slot)
  749. {
  750. return 0;
  751. }
  752. static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
  753. struct kvm_memory_slot *slot)
  754. {
  755. }
  756. #endif
  757. /* must be called with irqs disabled */
  758. static inline void __kvm_guest_enter(void)
  759. {
  760. guest_enter_irqoff();
  761. }
  762. /* must be called with irqs disabled */
  763. static inline void __kvm_guest_exit(void)
  764. {
  765. guest_exit_irqoff();
  766. }
  767. static inline void kvm_guest_enter(void)
  768. {
  769. guest_enter();
  770. }
  771. static inline void kvm_guest_exit(void)
  772. {
  773. guest_exit();
  774. }
  775. /*
  776. * search_memslots() and __gfn_to_memslot() are here because they are
  777. * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
  778. * gfn_to_memslot() itself isn't here as an inline because that would
  779. * bloat other code too much.
  780. */
  781. static inline struct kvm_memory_slot *
  782. search_memslots(struct kvm_memslots *slots, gfn_t gfn)
  783. {
  784. int start = 0, end = slots->used_slots;
  785. int slot = atomic_read(&slots->lru_slot);
  786. struct kvm_memory_slot *memslots = slots->memslots;
  787. if (gfn >= memslots[slot].base_gfn &&
  788. gfn < memslots[slot].base_gfn + memslots[slot].npages)
  789. return &memslots[slot];
  790. while (start < end) {
  791. slot = start + (end - start) / 2;
  792. if (gfn >= memslots[slot].base_gfn)
  793. end = slot;
  794. else
  795. start = slot + 1;
  796. }
  797. if (gfn >= memslots[start].base_gfn &&
  798. gfn < memslots[start].base_gfn + memslots[start].npages) {
  799. atomic_set(&slots->lru_slot, start);
  800. return &memslots[start];
  801. }
  802. return NULL;
  803. }
  804. static inline struct kvm_memory_slot *
  805. __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
  806. {
  807. return search_memslots(slots, gfn);
  808. }
  809. static inline unsigned long
  810. __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  811. {
  812. return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
  813. }
  814. static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
  815. {
  816. return gfn_to_memslot(kvm, gfn)->id;
  817. }
  818. static inline gfn_t
  819. hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
  820. {
  821. gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
  822. return slot->base_gfn + gfn_offset;
  823. }
  824. static inline gpa_t gfn_to_gpa(gfn_t gfn)
  825. {
  826. return (gpa_t)gfn << PAGE_SHIFT;
  827. }
  828. static inline gfn_t gpa_to_gfn(gpa_t gpa)
  829. {
  830. return (gfn_t)(gpa >> PAGE_SHIFT);
  831. }
  832. static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn)
  833. {
  834. return (hpa_t)pfn << PAGE_SHIFT;
  835. }
  836. static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa)
  837. {
  838. unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
  839. return kvm_is_error_hva(hva);
  840. }
  841. enum kvm_stat_kind {
  842. KVM_STAT_VM,
  843. KVM_STAT_VCPU,
  844. };
  845. struct kvm_stat_data {
  846. int offset;
  847. struct kvm *kvm;
  848. };
  849. struct kvm_stats_debugfs_item {
  850. const char *name;
  851. int offset;
  852. enum kvm_stat_kind kind;
  853. };
  854. extern struct kvm_stats_debugfs_item debugfs_entries[];
  855. extern struct dentry *kvm_debugfs_dir;
  856. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  857. static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
  858. {
  859. if (unlikely(kvm->mmu_notifier_count))
  860. return 1;
  861. /*
  862. * Ensure the read of mmu_notifier_count happens before the read
  863. * of mmu_notifier_seq. This interacts with the smp_wmb() in
  864. * mmu_notifier_invalidate_range_end to make sure that the caller
  865. * either sees the old (non-zero) value of mmu_notifier_count or
  866. * the new (incremented) value of mmu_notifier_seq.
  867. * PowerPC Book3s HV KVM calls this under a per-page lock
  868. * rather than under kvm->mmu_lock, for scalability, so
  869. * can't rely on kvm->mmu_lock to keep things ordered.
  870. */
  871. smp_rmb();
  872. if (kvm->mmu_notifier_seq != mmu_seq)
  873. return 1;
  874. return 0;
  875. }
  876. #endif
  877. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  878. #ifdef CONFIG_S390
  879. #define KVM_MAX_IRQ_ROUTES 4096 //FIXME: we can have more than that...
  880. #else
  881. #define KVM_MAX_IRQ_ROUTES 1024
  882. #endif
  883. int kvm_setup_default_irq_routing(struct kvm *kvm);
  884. int kvm_setup_empty_irq_routing(struct kvm *kvm);
  885. int kvm_set_irq_routing(struct kvm *kvm,
  886. const struct kvm_irq_routing_entry *entries,
  887. unsigned nr,
  888. unsigned flags);
  889. int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
  890. const struct kvm_irq_routing_entry *ue);
  891. void kvm_free_irq_routing(struct kvm *kvm);
  892. #else
  893. static inline void kvm_free_irq_routing(struct kvm *kvm) {}
  894. #endif
  895. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
  896. #ifdef CONFIG_HAVE_KVM_EVENTFD
  897. void kvm_eventfd_init(struct kvm *kvm);
  898. int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
  899. #ifdef CONFIG_HAVE_KVM_IRQFD
  900. int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
  901. void kvm_irqfd_release(struct kvm *kvm);
  902. void kvm_irq_routing_update(struct kvm *);
  903. #else
  904. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  905. {
  906. return -EINVAL;
  907. }
  908. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  909. #endif
  910. #else
  911. static inline void kvm_eventfd_init(struct kvm *kvm) {}
  912. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  913. {
  914. return -EINVAL;
  915. }
  916. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  917. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  918. static inline void kvm_irq_routing_update(struct kvm *kvm)
  919. {
  920. }
  921. #endif
  922. void kvm_arch_irq_routing_update(struct kvm *kvm);
  923. static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  924. {
  925. return -ENOSYS;
  926. }
  927. #endif /* CONFIG_HAVE_KVM_EVENTFD */
  928. static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
  929. {
  930. /*
  931. * Ensure the rest of the request is published to kvm_check_request's
  932. * caller. Paired with the smp_mb__after_atomic in kvm_check_request.
  933. */
  934. smp_wmb();
  935. set_bit(req, &vcpu->requests);
  936. }
  937. static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
  938. {
  939. if (test_bit(req, &vcpu->requests)) {
  940. clear_bit(req, &vcpu->requests);
  941. /*
  942. * Ensure the rest of the request is visible to kvm_check_request's
  943. * caller. Paired with the smp_wmb in kvm_make_request.
  944. */
  945. smp_mb__after_atomic();
  946. return true;
  947. } else {
  948. return false;
  949. }
  950. }
  951. extern bool kvm_rebooting;
  952. struct kvm_device {
  953. struct kvm_device_ops *ops;
  954. struct kvm *kvm;
  955. void *private;
  956. struct list_head vm_node;
  957. };
  958. /* create, destroy, and name are mandatory */
  959. struct kvm_device_ops {
  960. const char *name;
  961. int (*create)(struct kvm_device *dev, u32 type);
  962. /*
  963. * Destroy is responsible for freeing dev.
  964. *
  965. * Destroy may be called before or after destructors are called
  966. * on emulated I/O regions, depending on whether a reference is
  967. * held by a vcpu or other kvm component that gets destroyed
  968. * after the emulated I/O.
  969. */
  970. void (*destroy)(struct kvm_device *dev);
  971. int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  972. int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  973. int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  974. long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
  975. unsigned long arg);
  976. };
  977. void kvm_device_get(struct kvm_device *dev);
  978. void kvm_device_put(struct kvm_device *dev);
  979. struct kvm_device *kvm_device_from_filp(struct file *filp);
  980. int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
  981. void kvm_unregister_device_ops(u32 type);
  982. extern struct kvm_device_ops kvm_mpic_ops;
  983. extern struct kvm_device_ops kvm_xics_ops;
  984. extern struct kvm_device_ops kvm_arm_vgic_v2_ops;
  985. extern struct kvm_device_ops kvm_arm_vgic_v3_ops;
  986. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  987. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  988. {
  989. vcpu->spin_loop.in_spin_loop = val;
  990. }
  991. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  992. {
  993. vcpu->spin_loop.dy_eligible = val;
  994. }
  995. #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  996. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  997. {
  998. }
  999. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  1000. {
  1001. }
  1002. #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  1003. #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
  1004. bool kvm_arch_has_irq_bypass(void);
  1005. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *,
  1006. struct irq_bypass_producer *);
  1007. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *,
  1008. struct irq_bypass_producer *);
  1009. void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *);
  1010. void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *);
  1011. int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
  1012. uint32_t guest_irq, bool set);
  1013. #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */
  1014. #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS
  1015. /* If we wakeup during the poll time, was it a sucessful poll? */
  1016. static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
  1017. {
  1018. return vcpu->valid_wakeup;
  1019. }
  1020. #else
  1021. static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
  1022. {
  1023. return true;
  1024. }
  1025. #endif /* CONFIG_HAVE_KVM_INVALID_WAKEUPS */
  1026. #endif