kvm_host.h 26 KB

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