kvm_host.h 26 KB

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