kvm_host.h 26 KB

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