kvm_host.h 27 KB

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