kvm_host.h 33 KB

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