kvm_host.h 30 KB

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