kvm_host.h 29 KB

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