kvm_host.h 33 KB

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