kvm_host.h 37 KB

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