kvm_host.h 35 KB

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