kvm_host.h 29 KB

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