kvm_host.h 27 KB

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