kvm_host.h 25 KB

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