kvm_host.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 <asm/signal.h>
  25. #include <linux/kvm.h>
  26. #include <linux/kvm_para.h>
  27. #include <linux/kvm_types.h>
  28. #include <asm/kvm_host.h>
  29. #ifndef KVM_MMIO_SIZE
  30. #define KVM_MMIO_SIZE 8
  31. #endif
  32. /*
  33. * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
  34. * in kvm, other bits are visible for userspace which are defined in
  35. * include/linux/kvm_h.
  36. */
  37. #define KVM_MEMSLOT_INVALID (1UL << 16)
  38. /* Two fragments for cross MMIO pages. */
  39. #define KVM_MAX_MMIO_FRAGMENTS 2
  40. /*
  41. * For the normal pfn, the highest 12 bits should be zero,
  42. * so we can mask bit 62 ~ bit 52 to indicate the error pfn,
  43. * mask bit 63 to indicate the noslot pfn.
  44. */
  45. #define KVM_PFN_ERR_MASK (0x7ffULL << 52)
  46. #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
  47. #define KVM_PFN_NOSLOT (0x1ULL << 63)
  48. #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK)
  49. #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
  50. #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)
  51. /*
  52. * error pfns indicate that the gfn is in slot but faild to
  53. * translate it to pfn on host.
  54. */
  55. static inline bool is_error_pfn(pfn_t pfn)
  56. {
  57. return !!(pfn & KVM_PFN_ERR_MASK);
  58. }
  59. /*
  60. * error_noslot pfns indicate that the gfn can not be
  61. * translated to pfn - it is not in slot or failed to
  62. * translate it to pfn.
  63. */
  64. static inline bool is_error_noslot_pfn(pfn_t pfn)
  65. {
  66. return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
  67. }
  68. /* noslot pfn indicates that the gfn is not in slot. */
  69. static inline bool is_noslot_pfn(pfn_t pfn)
  70. {
  71. return pfn == KVM_PFN_NOSLOT;
  72. }
  73. #define KVM_HVA_ERR_BAD (PAGE_OFFSET)
  74. #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE)
  75. static inline bool kvm_is_error_hva(unsigned long addr)
  76. {
  77. return addr >= PAGE_OFFSET;
  78. }
  79. #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT))
  80. static inline bool is_error_page(struct page *page)
  81. {
  82. return IS_ERR(page);
  83. }
  84. /*
  85. * vcpu->requests bit members
  86. */
  87. #define KVM_REQ_TLB_FLUSH 0
  88. #define KVM_REQ_MIGRATE_TIMER 1
  89. #define KVM_REQ_REPORT_TPR_ACCESS 2
  90. #define KVM_REQ_MMU_RELOAD 3
  91. #define KVM_REQ_TRIPLE_FAULT 4
  92. #define KVM_REQ_PENDING_TIMER 5
  93. #define KVM_REQ_UNHALT 6
  94. #define KVM_REQ_MMU_SYNC 7
  95. #define KVM_REQ_CLOCK_UPDATE 8
  96. #define KVM_REQ_KICK 9
  97. #define KVM_REQ_DEACTIVATE_FPU 10
  98. #define KVM_REQ_EVENT 11
  99. #define KVM_REQ_APF_HALT 12
  100. #define KVM_REQ_STEAL_UPDATE 13
  101. #define KVM_REQ_NMI 14
  102. #define KVM_REQ_IMMEDIATE_EXIT 15
  103. #define KVM_REQ_PMU 16
  104. #define KVM_REQ_PMI 17
  105. #define KVM_REQ_WATCHDOG 18
  106. #define KVM_REQ_MASTERCLOCK_UPDATE 19
  107. #define KVM_REQ_MCLOCK_INPROGRESS 20
  108. #define KVM_USERSPACE_IRQ_SOURCE_ID 0
  109. #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
  110. struct kvm;
  111. struct kvm_vcpu;
  112. extern struct kmem_cache *kvm_vcpu_cache;
  113. struct kvm_io_range {
  114. gpa_t addr;
  115. int len;
  116. struct kvm_io_device *dev;
  117. };
  118. #define NR_IOBUS_DEVS 1000
  119. struct kvm_io_bus {
  120. int dev_count;
  121. struct kvm_io_range range[];
  122. };
  123. enum kvm_bus {
  124. KVM_MMIO_BUS,
  125. KVM_PIO_BUS,
  126. KVM_NR_BUSES
  127. };
  128. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  129. int len, const void *val);
  130. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len,
  131. void *val);
  132. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  133. int len, struct kvm_io_device *dev);
  134. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  135. struct kvm_io_device *dev);
  136. #ifdef CONFIG_KVM_ASYNC_PF
  137. struct kvm_async_pf {
  138. struct work_struct work;
  139. struct list_head link;
  140. struct list_head queue;
  141. struct kvm_vcpu *vcpu;
  142. struct mm_struct *mm;
  143. gva_t gva;
  144. unsigned long addr;
  145. struct kvm_arch_async_pf arch;
  146. struct page *page;
  147. bool done;
  148. };
  149. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
  150. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
  151. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  152. struct kvm_arch_async_pf *arch);
  153. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
  154. #endif
  155. enum {
  156. OUTSIDE_GUEST_MODE,
  157. IN_GUEST_MODE,
  158. EXITING_GUEST_MODE,
  159. READING_SHADOW_PAGE_TABLES,
  160. };
  161. /*
  162. * Sometimes a large or cross-page mmio needs to be broken up into separate
  163. * exits for userspace servicing.
  164. */
  165. struct kvm_mmio_fragment {
  166. gpa_t gpa;
  167. void *data;
  168. unsigned len;
  169. };
  170. struct kvm_vcpu {
  171. struct kvm *kvm;
  172. #ifdef CONFIG_PREEMPT_NOTIFIERS
  173. struct preempt_notifier preempt_notifier;
  174. #endif
  175. int cpu;
  176. int vcpu_id;
  177. int srcu_idx;
  178. int mode;
  179. unsigned long requests;
  180. unsigned long guest_debug;
  181. struct mutex mutex;
  182. struct kvm_run *run;
  183. int fpu_active;
  184. int guest_fpu_loaded, guest_xcr0_loaded;
  185. wait_queue_head_t wq;
  186. struct pid *pid;
  187. int sigset_active;
  188. sigset_t sigset;
  189. struct kvm_vcpu_stat stat;
  190. #ifdef CONFIG_HAS_IOMEM
  191. int mmio_needed;
  192. int mmio_read_completed;
  193. int mmio_is_write;
  194. int mmio_cur_fragment;
  195. int mmio_nr_fragments;
  196. struct kvm_mmio_fragment mmio_fragments[KVM_MAX_MMIO_FRAGMENTS];
  197. #endif
  198. #ifdef CONFIG_KVM_ASYNC_PF
  199. struct {
  200. u32 queued;
  201. struct list_head queue;
  202. struct list_head done;
  203. spinlock_t lock;
  204. } async_pf;
  205. #endif
  206. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  207. /*
  208. * Cpu relax intercept or pause loop exit optimization
  209. * in_spin_loop: set when a vcpu does a pause loop exit
  210. * or cpu relax intercepted.
  211. * dy_eligible: indicates whether vcpu is eligible for directed yield.
  212. */
  213. struct {
  214. bool in_spin_loop;
  215. bool dy_eligible;
  216. } spin_loop;
  217. #endif
  218. struct kvm_vcpu_arch arch;
  219. };
  220. static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
  221. {
  222. return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
  223. }
  224. /*
  225. * Some of the bitops functions do not support too long bitmaps.
  226. * This number must be determined not to exceed such limits.
  227. */
  228. #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
  229. struct kvm_memory_slot {
  230. gfn_t base_gfn;
  231. unsigned long npages;
  232. unsigned long flags;
  233. unsigned long *dirty_bitmap;
  234. struct kvm_arch_memory_slot arch;
  235. unsigned long userspace_addr;
  236. int user_alloc;
  237. int id;
  238. };
  239. static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
  240. {
  241. return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  242. }
  243. struct kvm_kernel_irq_routing_entry {
  244. u32 gsi;
  245. u32 type;
  246. int (*set)(struct kvm_kernel_irq_routing_entry *e,
  247. struct kvm *kvm, int irq_source_id, int level);
  248. union {
  249. struct {
  250. unsigned irqchip;
  251. unsigned pin;
  252. } irqchip;
  253. struct msi_msg msi;
  254. };
  255. struct hlist_node link;
  256. };
  257. #ifdef __KVM_HAVE_IOAPIC
  258. struct kvm_irq_routing_table {
  259. int chip[KVM_NR_IRQCHIPS][KVM_IOAPIC_NUM_PINS];
  260. struct kvm_kernel_irq_routing_entry *rt_entries;
  261. u32 nr_rt_entries;
  262. /*
  263. * Array indexed by gsi. Each entry contains list of irq chips
  264. * the gsi is connected to.
  265. */
  266. struct hlist_head map[0];
  267. };
  268. #else
  269. struct kvm_irq_routing_table {};
  270. #endif
  271. #ifndef KVM_MEM_SLOTS_NUM
  272. #define KVM_MEM_SLOTS_NUM (KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  273. #endif
  274. /*
  275. * Note:
  276. * memslots are not sorted by id anymore, please use id_to_memslot()
  277. * to get the memslot by its id.
  278. */
  279. struct kvm_memslots {
  280. u64 generation;
  281. struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
  282. /* The mapping table from slot id to the index in memslots[]. */
  283. int id_to_index[KVM_MEM_SLOTS_NUM];
  284. };
  285. struct kvm {
  286. spinlock_t mmu_lock;
  287. struct mutex slots_lock;
  288. struct mm_struct *mm; /* userspace tied to this vm */
  289. struct kvm_memslots *memslots;
  290. struct srcu_struct srcu;
  291. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  292. u32 bsp_vcpu_id;
  293. #endif
  294. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  295. atomic_t online_vcpus;
  296. int last_boosted_vcpu;
  297. struct list_head vm_list;
  298. struct mutex lock;
  299. struct kvm_io_bus *buses[KVM_NR_BUSES];
  300. #ifdef CONFIG_HAVE_KVM_EVENTFD
  301. struct {
  302. spinlock_t lock;
  303. struct list_head items;
  304. struct list_head resampler_list;
  305. struct mutex resampler_lock;
  306. } irqfds;
  307. struct list_head ioeventfds;
  308. #endif
  309. struct kvm_vm_stat stat;
  310. struct kvm_arch arch;
  311. atomic_t users_count;
  312. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  313. struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
  314. spinlock_t ring_lock;
  315. struct list_head coalesced_zones;
  316. #endif
  317. struct mutex irq_lock;
  318. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  319. /*
  320. * Update side is protected by irq_lock and,
  321. * if configured, irqfds.lock.
  322. */
  323. struct kvm_irq_routing_table __rcu *irq_routing;
  324. struct hlist_head mask_notifier_list;
  325. struct hlist_head irq_ack_notifier_list;
  326. #endif
  327. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  328. struct mmu_notifier mmu_notifier;
  329. unsigned long mmu_notifier_seq;
  330. long mmu_notifier_count;
  331. #endif
  332. long tlbs_dirty;
  333. };
  334. #define kvm_err(fmt, ...) \
  335. pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  336. #define kvm_info(fmt, ...) \
  337. pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  338. #define kvm_debug(fmt, ...) \
  339. pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  340. #define kvm_pr_unimpl(fmt, ...) \
  341. pr_err_ratelimited("kvm [%i]: " fmt, \
  342. task_tgid_nr(current), ## __VA_ARGS__)
  343. /* The guest did something we don't support. */
  344. #define vcpu_unimpl(vcpu, fmt, ...) \
  345. kvm_pr_unimpl("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
  346. static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
  347. {
  348. smp_rmb();
  349. return kvm->vcpus[i];
  350. }
  351. #define kvm_for_each_vcpu(idx, vcpup, kvm) \
  352. for (idx = 0; \
  353. idx < atomic_read(&kvm->online_vcpus) && \
  354. (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
  355. idx++)
  356. #define kvm_for_each_memslot(memslot, slots) \
  357. for (memslot = &slots->memslots[0]; \
  358. memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
  359. memslot++)
  360. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  361. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  362. int __must_check vcpu_load(struct kvm_vcpu *vcpu);
  363. void vcpu_put(struct kvm_vcpu *vcpu);
  364. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  365. struct module *module);
  366. void kvm_exit(void);
  367. void kvm_get_kvm(struct kvm *kvm);
  368. void kvm_put_kvm(struct kvm *kvm);
  369. void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new);
  370. static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
  371. {
  372. return rcu_dereference_check(kvm->memslots,
  373. srcu_read_lock_held(&kvm->srcu)
  374. || lockdep_is_held(&kvm->slots_lock));
  375. }
  376. static inline struct kvm_memory_slot *
  377. id_to_memslot(struct kvm_memslots *slots, int id)
  378. {
  379. int index = slots->id_to_index[id];
  380. struct kvm_memory_slot *slot;
  381. slot = &slots->memslots[index];
  382. WARN_ON(slot->id != id);
  383. return slot;
  384. }
  385. int kvm_set_memory_region(struct kvm *kvm,
  386. struct kvm_userspace_memory_region *mem,
  387. int user_alloc);
  388. int __kvm_set_memory_region(struct kvm *kvm,
  389. struct kvm_userspace_memory_region *mem,
  390. int user_alloc);
  391. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  392. struct kvm_memory_slot *dont);
  393. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages);
  394. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  395. struct kvm_memory_slot *memslot,
  396. struct kvm_memory_slot old,
  397. struct kvm_userspace_memory_region *mem,
  398. int user_alloc);
  399. void kvm_arch_commit_memory_region(struct kvm *kvm,
  400. struct kvm_userspace_memory_region *mem,
  401. struct kvm_memory_slot old,
  402. int user_alloc);
  403. bool kvm_largepages_enabled(void);
  404. void kvm_disable_largepages(void);
  405. /* flush all memory translations */
  406. void kvm_arch_flush_shadow_all(struct kvm *kvm);
  407. /* flush memory translations pointing to 'slot' */
  408. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  409. struct kvm_memory_slot *slot);
  410. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  411. int nr_pages);
  412. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  413. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
  414. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  415. void kvm_release_page_clean(struct page *page);
  416. void kvm_release_page_dirty(struct page *page);
  417. void kvm_set_page_dirty(struct page *page);
  418. void kvm_set_page_accessed(struct page *page);
  419. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
  420. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  421. bool write_fault, bool *writable);
  422. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
  423. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  424. bool *writable);
  425. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  426. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
  427. void kvm_release_pfn_dirty(pfn_t pfn);
  428. void kvm_release_pfn_clean(pfn_t pfn);
  429. void kvm_set_pfn_dirty(pfn_t pfn);
  430. void kvm_set_pfn_accessed(pfn_t pfn);
  431. void kvm_get_pfn(pfn_t pfn);
  432. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  433. int len);
  434. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  435. unsigned long len);
  436. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  437. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  438. void *data, unsigned long len);
  439. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  440. int offset, int len);
  441. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  442. unsigned long len);
  443. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  444. void *data, unsigned long len);
  445. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  446. gpa_t gpa);
  447. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  448. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  449. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  450. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
  451. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
  452. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  453. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  454. gfn_t gfn);
  455. void kvm_vcpu_block(struct kvm_vcpu *vcpu);
  456. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  457. bool kvm_vcpu_yield_to(struct kvm_vcpu *target);
  458. void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu);
  459. void kvm_resched(struct kvm_vcpu *vcpu);
  460. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  461. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  462. void kvm_flush_remote_tlbs(struct kvm *kvm);
  463. void kvm_reload_remote_mmus(struct kvm *kvm);
  464. void kvm_make_mclock_inprogress_request(struct kvm *kvm);
  465. long kvm_arch_dev_ioctl(struct file *filp,
  466. unsigned int ioctl, unsigned long arg);
  467. long kvm_arch_vcpu_ioctl(struct file *filp,
  468. unsigned int ioctl, unsigned long arg);
  469. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
  470. int kvm_dev_ioctl_check_extension(long ext);
  471. int kvm_get_dirty_log(struct kvm *kvm,
  472. struct kvm_dirty_log *log, int *is_dirty);
  473. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  474. struct kvm_dirty_log *log);
  475. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  476. struct
  477. kvm_userspace_memory_region *mem,
  478. int user_alloc);
  479. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level);
  480. long kvm_arch_vm_ioctl(struct file *filp,
  481. unsigned int ioctl, unsigned long arg);
  482. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  483. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  484. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  485. struct kvm_translation *tr);
  486. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  487. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  488. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  489. struct kvm_sregs *sregs);
  490. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  491. struct kvm_sregs *sregs);
  492. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  493. struct kvm_mp_state *mp_state);
  494. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  495. struct kvm_mp_state *mp_state);
  496. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  497. struct kvm_guest_debug *dbg);
  498. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  499. int kvm_arch_init(void *opaque);
  500. void kvm_arch_exit(void);
  501. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  502. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  503. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  504. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  505. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  506. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  507. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
  508. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
  509. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
  510. int kvm_arch_hardware_enable(void *garbage);
  511. void kvm_arch_hardware_disable(void *garbage);
  512. int kvm_arch_hardware_setup(void);
  513. void kvm_arch_hardware_unsetup(void);
  514. void kvm_arch_check_processor_compat(void *rtn);
  515. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
  516. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
  517. void kvm_free_physmem(struct kvm *kvm);
  518. void *kvm_kvzalloc(unsigned long size);
  519. void kvm_kvfree(const void *addr);
  520. #ifndef __KVM_HAVE_ARCH_VM_ALLOC
  521. static inline struct kvm *kvm_arch_alloc_vm(void)
  522. {
  523. return kzalloc(sizeof(struct kvm), GFP_KERNEL);
  524. }
  525. static inline void kvm_arch_free_vm(struct kvm *kvm)
  526. {
  527. kfree(kvm);
  528. }
  529. #endif
  530. static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  531. {
  532. #ifdef __KVM_HAVE_ARCH_WQP
  533. return vcpu->arch.wqp;
  534. #else
  535. return &vcpu->wq;
  536. #endif
  537. }
  538. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
  539. void kvm_arch_destroy_vm(struct kvm *kvm);
  540. void kvm_free_all_assigned_devices(struct kvm *kvm);
  541. void kvm_arch_sync_events(struct kvm *kvm);
  542. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
  543. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  544. bool kvm_is_mmio_pfn(pfn_t pfn);
  545. struct kvm_irq_ack_notifier {
  546. struct hlist_node link;
  547. unsigned gsi;
  548. void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
  549. };
  550. struct kvm_assigned_dev_kernel {
  551. struct kvm_irq_ack_notifier ack_notifier;
  552. struct list_head list;
  553. int assigned_dev_id;
  554. int host_segnr;
  555. int host_busnr;
  556. int host_devfn;
  557. unsigned int entries_nr;
  558. int host_irq;
  559. bool host_irq_disabled;
  560. bool pci_2_3;
  561. struct msix_entry *host_msix_entries;
  562. int guest_irq;
  563. struct msix_entry *guest_msix_entries;
  564. unsigned long irq_requested_type;
  565. int irq_source_id;
  566. int flags;
  567. struct pci_dev *dev;
  568. struct kvm *kvm;
  569. spinlock_t intx_lock;
  570. spinlock_t intx_mask_lock;
  571. char irq_name[32];
  572. struct pci_saved_state *pci_saved_state;
  573. };
  574. struct kvm_irq_mask_notifier {
  575. void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
  576. int irq;
  577. struct hlist_node link;
  578. };
  579. void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
  580. struct kvm_irq_mask_notifier *kimn);
  581. void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
  582. struct kvm_irq_mask_notifier *kimn);
  583. void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
  584. bool mask);
  585. #ifdef __KVM_HAVE_IOAPIC
  586. void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
  587. union kvm_ioapic_redirect_entry *entry,
  588. unsigned long *deliver_bitmask);
  589. #endif
  590. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
  591. int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level);
  592. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
  593. int irq_source_id, int level);
  594. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
  595. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  596. struct kvm_irq_ack_notifier *kian);
  597. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  598. struct kvm_irq_ack_notifier *kian);
  599. int kvm_request_irq_source_id(struct kvm *kvm);
  600. void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
  601. /* For vcpu->arch.iommu_flags */
  602. #define KVM_IOMMU_CACHE_COHERENCY 0x1
  603. #ifdef CONFIG_IOMMU_API
  604. int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
  605. void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
  606. int kvm_iommu_map_guest(struct kvm *kvm);
  607. int kvm_iommu_unmap_guest(struct kvm *kvm);
  608. int kvm_assign_device(struct kvm *kvm,
  609. struct kvm_assigned_dev_kernel *assigned_dev);
  610. int kvm_deassign_device(struct kvm *kvm,
  611. struct kvm_assigned_dev_kernel *assigned_dev);
  612. #else /* CONFIG_IOMMU_API */
  613. static inline int kvm_iommu_map_pages(struct kvm *kvm,
  614. struct kvm_memory_slot *slot)
  615. {
  616. return 0;
  617. }
  618. static inline void kvm_iommu_unmap_pages(struct kvm *kvm,
  619. struct kvm_memory_slot *slot)
  620. {
  621. }
  622. static inline int kvm_iommu_map_guest(struct kvm *kvm)
  623. {
  624. return -ENODEV;
  625. }
  626. static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
  627. {
  628. return 0;
  629. }
  630. static inline int kvm_assign_device(struct kvm *kvm,
  631. struct kvm_assigned_dev_kernel *assigned_dev)
  632. {
  633. return 0;
  634. }
  635. static inline int kvm_deassign_device(struct kvm *kvm,
  636. struct kvm_assigned_dev_kernel *assigned_dev)
  637. {
  638. return 0;
  639. }
  640. #endif /* CONFIG_IOMMU_API */
  641. static inline void __guest_enter(void)
  642. {
  643. /*
  644. * This is running in ioctl context so we can avoid
  645. * the call to vtime_account() with its unnecessary idle check.
  646. */
  647. vtime_account_system(current);
  648. current->flags |= PF_VCPU;
  649. }
  650. static inline void __guest_exit(void)
  651. {
  652. /*
  653. * This is running in ioctl context so we can avoid
  654. * the call to vtime_account() with its unnecessary idle check.
  655. */
  656. vtime_account_system(current);
  657. current->flags &= ~PF_VCPU;
  658. }
  659. #ifdef CONFIG_CONTEXT_TRACKING
  660. extern void guest_enter(void);
  661. extern void guest_exit(void);
  662. #else /* !CONFIG_CONTEXT_TRACKING */
  663. static inline void guest_enter(void)
  664. {
  665. __guest_enter();
  666. }
  667. static inline void guest_exit(void)
  668. {
  669. __guest_exit();
  670. }
  671. #endif /* !CONFIG_CONTEXT_TRACKING */
  672. static inline void kvm_guest_enter(void)
  673. {
  674. unsigned long flags;
  675. BUG_ON(preemptible());
  676. local_irq_save(flags);
  677. guest_enter();
  678. local_irq_restore(flags);
  679. /* KVM does not hold any references to rcu protected data when it
  680. * switches CPU into a guest mode. In fact switching to a guest mode
  681. * is very similar to exiting to userspase from rcu point of view. In
  682. * addition CPU may stay in a guest mode for quite a long time (up to
  683. * one time slice). Lets treat guest mode as quiescent state, just like
  684. * we do with user-mode execution.
  685. */
  686. rcu_virt_note_context_switch(smp_processor_id());
  687. }
  688. static inline void kvm_guest_exit(void)
  689. {
  690. unsigned long flags;
  691. local_irq_save(flags);
  692. guest_exit();
  693. local_irq_restore(flags);
  694. }
  695. /*
  696. * search_memslots() and __gfn_to_memslot() are here because they are
  697. * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
  698. * gfn_to_memslot() itself isn't here as an inline because that would
  699. * bloat other code too much.
  700. */
  701. static inline struct kvm_memory_slot *
  702. search_memslots(struct kvm_memslots *slots, gfn_t gfn)
  703. {
  704. struct kvm_memory_slot *memslot;
  705. kvm_for_each_memslot(memslot, slots)
  706. if (gfn >= memslot->base_gfn &&
  707. gfn < memslot->base_gfn + memslot->npages)
  708. return memslot;
  709. return NULL;
  710. }
  711. static inline struct kvm_memory_slot *
  712. __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
  713. {
  714. return search_memslots(slots, gfn);
  715. }
  716. static inline unsigned long
  717. __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  718. {
  719. return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
  720. }
  721. static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
  722. {
  723. return gfn_to_memslot(kvm, gfn)->id;
  724. }
  725. static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
  726. {
  727. /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
  728. return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
  729. (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
  730. }
  731. static inline gfn_t
  732. hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
  733. {
  734. gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
  735. return slot->base_gfn + gfn_offset;
  736. }
  737. static inline gpa_t gfn_to_gpa(gfn_t gfn)
  738. {
  739. return (gpa_t)gfn << PAGE_SHIFT;
  740. }
  741. static inline gfn_t gpa_to_gfn(gpa_t gpa)
  742. {
  743. return (gfn_t)(gpa >> PAGE_SHIFT);
  744. }
  745. static inline hpa_t pfn_to_hpa(pfn_t pfn)
  746. {
  747. return (hpa_t)pfn << PAGE_SHIFT;
  748. }
  749. static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
  750. {
  751. set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
  752. }
  753. enum kvm_stat_kind {
  754. KVM_STAT_VM,
  755. KVM_STAT_VCPU,
  756. };
  757. struct kvm_stats_debugfs_item {
  758. const char *name;
  759. int offset;
  760. enum kvm_stat_kind kind;
  761. struct dentry *dentry;
  762. };
  763. extern struct kvm_stats_debugfs_item debugfs_entries[];
  764. extern struct dentry *kvm_debugfs_dir;
  765. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  766. static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
  767. {
  768. if (unlikely(kvm->mmu_notifier_count))
  769. return 1;
  770. /*
  771. * Ensure the read of mmu_notifier_count happens before the read
  772. * of mmu_notifier_seq. This interacts with the smp_wmb() in
  773. * mmu_notifier_invalidate_range_end to make sure that the caller
  774. * either sees the old (non-zero) value of mmu_notifier_count or
  775. * the new (incremented) value of mmu_notifier_seq.
  776. * PowerPC Book3s HV KVM calls this under a per-page lock
  777. * rather than under kvm->mmu_lock, for scalability, so
  778. * can't rely on kvm->mmu_lock to keep things ordered.
  779. */
  780. smp_rmb();
  781. if (kvm->mmu_notifier_seq != mmu_seq)
  782. return 1;
  783. return 0;
  784. }
  785. #endif
  786. #ifdef KVM_CAP_IRQ_ROUTING
  787. #define KVM_MAX_IRQ_ROUTES 1024
  788. int kvm_setup_default_irq_routing(struct kvm *kvm);
  789. int kvm_set_irq_routing(struct kvm *kvm,
  790. const struct kvm_irq_routing_entry *entries,
  791. unsigned nr,
  792. unsigned flags);
  793. void kvm_free_irq_routing(struct kvm *kvm);
  794. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
  795. #else
  796. static inline void kvm_free_irq_routing(struct kvm *kvm) {}
  797. #endif
  798. #ifdef CONFIG_HAVE_KVM_EVENTFD
  799. void kvm_eventfd_init(struct kvm *kvm);
  800. int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
  801. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  802. int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
  803. void kvm_irqfd_release(struct kvm *kvm);
  804. void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *);
  805. #else
  806. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  807. {
  808. return -EINVAL;
  809. }
  810. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  811. #endif
  812. #else
  813. static inline void kvm_eventfd_init(struct kvm *kvm) {}
  814. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  815. {
  816. return -EINVAL;
  817. }
  818. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  819. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  820. static inline void kvm_irq_routing_update(struct kvm *kvm,
  821. struct kvm_irq_routing_table *irq_rt)
  822. {
  823. rcu_assign_pointer(kvm->irq_routing, irq_rt);
  824. }
  825. #endif
  826. static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  827. {
  828. return -ENOSYS;
  829. }
  830. #endif /* CONFIG_HAVE_KVM_EVENTFD */
  831. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  832. static inline bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
  833. {
  834. return vcpu->kvm->bsp_vcpu_id == vcpu->vcpu_id;
  835. }
  836. bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu);
  837. #else
  838. static inline bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu) { return true; }
  839. #endif
  840. #ifdef __KVM_HAVE_DEVICE_ASSIGNMENT
  841. long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  842. unsigned long arg);
  843. #else
  844. static inline long kvm_vm_ioctl_assigned_device(struct kvm *kvm, unsigned ioctl,
  845. unsigned long arg)
  846. {
  847. return -ENOTTY;
  848. }
  849. #endif
  850. static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
  851. {
  852. set_bit(req, &vcpu->requests);
  853. }
  854. static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
  855. {
  856. if (test_bit(req, &vcpu->requests)) {
  857. clear_bit(req, &vcpu->requests);
  858. return true;
  859. } else {
  860. return false;
  861. }
  862. }
  863. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  864. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  865. {
  866. vcpu->spin_loop.in_spin_loop = val;
  867. }
  868. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  869. {
  870. vcpu->spin_loop.dy_eligible = val;
  871. }
  872. #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  873. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  874. {
  875. }
  876. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  877. {
  878. }
  879. static inline bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  880. {
  881. return true;
  882. }
  883. #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  884. #endif