kvm_host.h 30 KB

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