kvm_host.h 30 KB

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