kvm_host.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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/vmalloc.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/ratelimit.h>
  23. #include <linux/err.h>
  24. #include <linux/irqflags.h>
  25. #include <linux/context_tracking.h>
  26. #include <linux/irqbypass.h>
  27. #include <linux/swait.h>
  28. #include <linux/refcount.h>
  29. #include <asm/signal.h>
  30. #include <linux/kvm.h>
  31. #include <linux/kvm_para.h>
  32. #include <linux/kvm_types.h>
  33. #include <asm/kvm_host.h>
  34. #ifndef KVM_MAX_VCPU_ID
  35. #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
  36. #endif
  37. /*
  38. * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used
  39. * in kvm, other bits are visible for userspace which are defined in
  40. * include/linux/kvm_h.
  41. */
  42. #define KVM_MEMSLOT_INVALID (1UL << 16)
  43. /* Two fragments for cross MMIO pages. */
  44. #define KVM_MAX_MMIO_FRAGMENTS 2
  45. #ifndef KVM_ADDRESS_SPACE_NUM
  46. #define KVM_ADDRESS_SPACE_NUM 1
  47. #endif
  48. /*
  49. * For the normal pfn, the highest 12 bits should be zero,
  50. * so we can mask bit 62 ~ bit 52 to indicate the error pfn,
  51. * mask bit 63 to indicate the noslot pfn.
  52. */
  53. #define KVM_PFN_ERR_MASK (0x7ffULL << 52)
  54. #define KVM_PFN_ERR_NOSLOT_MASK (0xfffULL << 52)
  55. #define KVM_PFN_NOSLOT (0x1ULL << 63)
  56. #define KVM_PFN_ERR_FAULT (KVM_PFN_ERR_MASK)
  57. #define KVM_PFN_ERR_HWPOISON (KVM_PFN_ERR_MASK + 1)
  58. #define KVM_PFN_ERR_RO_FAULT (KVM_PFN_ERR_MASK + 2)
  59. /*
  60. * error pfns indicate that the gfn is in slot but faild to
  61. * translate it to pfn on host.
  62. */
  63. static inline bool is_error_pfn(kvm_pfn_t pfn)
  64. {
  65. return !!(pfn & KVM_PFN_ERR_MASK);
  66. }
  67. /*
  68. * error_noslot pfns indicate that the gfn can not be
  69. * translated to pfn - it is not in slot or failed to
  70. * translate it to pfn.
  71. */
  72. static inline bool is_error_noslot_pfn(kvm_pfn_t pfn)
  73. {
  74. return !!(pfn & KVM_PFN_ERR_NOSLOT_MASK);
  75. }
  76. /* noslot pfn indicates that the gfn is not in slot. */
  77. static inline bool is_noslot_pfn(kvm_pfn_t pfn)
  78. {
  79. return pfn == KVM_PFN_NOSLOT;
  80. }
  81. /*
  82. * architectures with KVM_HVA_ERR_BAD other than PAGE_OFFSET (e.g. s390)
  83. * provide own defines and kvm_is_error_hva
  84. */
  85. #ifndef KVM_HVA_ERR_BAD
  86. #define KVM_HVA_ERR_BAD (PAGE_OFFSET)
  87. #define KVM_HVA_ERR_RO_BAD (PAGE_OFFSET + PAGE_SIZE)
  88. static inline bool kvm_is_error_hva(unsigned long addr)
  89. {
  90. return addr >= PAGE_OFFSET;
  91. }
  92. #endif
  93. #define KVM_ERR_PTR_BAD_PAGE (ERR_PTR(-ENOENT))
  94. static inline bool is_error_page(struct page *page)
  95. {
  96. return IS_ERR(page);
  97. }
  98. #define KVM_REQUEST_MASK GENMASK(7,0)
  99. #define KVM_REQUEST_NO_WAKEUP BIT(8)
  100. #define KVM_REQUEST_WAIT BIT(9)
  101. /*
  102. * Architecture-independent vcpu->requests bit members
  103. * Bits 4-7 are reserved for more arch-independent bits.
  104. */
  105. #define KVM_REQ_TLB_FLUSH (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  106. #define KVM_REQ_MMU_RELOAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  107. #define KVM_REQ_PENDING_TIMER 2
  108. #define KVM_REQ_UNHALT 3
  109. #define KVM_REQUEST_ARCH_BASE 8
  110. #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \
  111. BUILD_BUG_ON((unsigned)(nr) >= (FIELD_SIZEOF(struct kvm_vcpu, requests) * 8) - KVM_REQUEST_ARCH_BASE); \
  112. (unsigned)(((nr) + KVM_REQUEST_ARCH_BASE) | (flags)); \
  113. })
  114. #define KVM_ARCH_REQ(nr) KVM_ARCH_REQ_FLAGS(nr, 0)
  115. #define KVM_USERSPACE_IRQ_SOURCE_ID 0
  116. #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
  117. extern struct kmem_cache *kvm_vcpu_cache;
  118. extern spinlock_t kvm_lock;
  119. extern struct list_head vm_list;
  120. struct kvm_io_range {
  121. gpa_t addr;
  122. int len;
  123. struct kvm_io_device *dev;
  124. };
  125. #define NR_IOBUS_DEVS 1000
  126. struct kvm_io_bus {
  127. int dev_count;
  128. int ioeventfd_count;
  129. struct kvm_io_range range[];
  130. };
  131. enum kvm_bus {
  132. KVM_MMIO_BUS,
  133. KVM_PIO_BUS,
  134. KVM_VIRTIO_CCW_NOTIFY_BUS,
  135. KVM_FAST_MMIO_BUS,
  136. KVM_NR_BUSES
  137. };
  138. int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  139. int len, const void *val);
  140. int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
  141. gpa_t addr, int len, const void *val, long cookie);
  142. int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  143. int len, void *val);
  144. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  145. int len, struct kvm_io_device *dev);
  146. void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  147. struct kvm_io_device *dev);
  148. struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  149. gpa_t addr);
  150. #ifdef CONFIG_KVM_ASYNC_PF
  151. struct kvm_async_pf {
  152. struct work_struct work;
  153. struct list_head link;
  154. struct list_head queue;
  155. struct kvm_vcpu *vcpu;
  156. struct mm_struct *mm;
  157. gva_t gva;
  158. unsigned long addr;
  159. struct kvm_arch_async_pf arch;
  160. bool wakeup_all;
  161. };
  162. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu);
  163. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu);
  164. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,
  165. struct kvm_arch_async_pf *arch);
  166. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);
  167. #endif
  168. enum {
  169. OUTSIDE_GUEST_MODE,
  170. IN_GUEST_MODE,
  171. EXITING_GUEST_MODE,
  172. READING_SHADOW_PAGE_TABLES,
  173. };
  174. /*
  175. * Sometimes a large or cross-page mmio needs to be broken up into separate
  176. * exits for userspace servicing.
  177. */
  178. struct kvm_mmio_fragment {
  179. gpa_t gpa;
  180. void *data;
  181. unsigned len;
  182. };
  183. struct kvm_vcpu {
  184. struct kvm *kvm;
  185. #ifdef CONFIG_PREEMPT_NOTIFIERS
  186. struct preempt_notifier preempt_notifier;
  187. #endif
  188. int cpu;
  189. int vcpu_id;
  190. int srcu_idx;
  191. int mode;
  192. u64 requests;
  193. unsigned long guest_debug;
  194. int pre_pcpu;
  195. struct list_head blocked_vcpu_list;
  196. struct mutex mutex;
  197. struct kvm_run *run;
  198. int guest_xcr0_loaded;
  199. struct swait_queue_head wq;
  200. struct pid __rcu *pid;
  201. int sigset_active;
  202. sigset_t sigset;
  203. struct kvm_vcpu_stat stat;
  204. unsigned int halt_poll_ns;
  205. bool valid_wakeup;
  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. struct dentry *debugfs_dentry;
  237. };
  238. static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
  239. {
  240. /*
  241. * The memory barrier ensures a previous write to vcpu->requests cannot
  242. * be reordered with the read of vcpu->mode. It pairs with the general
  243. * memory barrier following the write of vcpu->mode in VCPU RUN.
  244. */
  245. smp_mb__before_atomic();
  246. return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
  247. }
  248. /*
  249. * Some of the bitops functions do not support too long bitmaps.
  250. * This number must be determined not to exceed such limits.
  251. */
  252. #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1)
  253. struct kvm_memory_slot {
  254. gfn_t base_gfn;
  255. unsigned long npages;
  256. unsigned long *dirty_bitmap;
  257. struct kvm_arch_memory_slot arch;
  258. unsigned long userspace_addr;
  259. u32 flags;
  260. short id;
  261. };
  262. static inline unsigned long kvm_dirty_bitmap_bytes(struct kvm_memory_slot *memslot)
  263. {
  264. return ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  265. }
  266. static inline unsigned long *kvm_second_dirty_bitmap(struct kvm_memory_slot *memslot)
  267. {
  268. unsigned long len = kvm_dirty_bitmap_bytes(memslot);
  269. return memslot->dirty_bitmap + len / sizeof(*memslot->dirty_bitmap);
  270. }
  271. struct kvm_s390_adapter_int {
  272. u64 ind_addr;
  273. u64 summary_addr;
  274. u64 ind_offset;
  275. u32 summary_offset;
  276. u32 adapter_id;
  277. };
  278. struct kvm_hv_sint {
  279. u32 vcpu;
  280. u32 sint;
  281. };
  282. struct kvm_kernel_irq_routing_entry {
  283. u32 gsi;
  284. u32 type;
  285. int (*set)(struct kvm_kernel_irq_routing_entry *e,
  286. struct kvm *kvm, int irq_source_id, int level,
  287. bool line_status);
  288. union {
  289. struct {
  290. unsigned irqchip;
  291. unsigned pin;
  292. } irqchip;
  293. struct {
  294. u32 address_lo;
  295. u32 address_hi;
  296. u32 data;
  297. u32 flags;
  298. u32 devid;
  299. } msi;
  300. struct kvm_s390_adapter_int adapter;
  301. struct kvm_hv_sint hv_sint;
  302. };
  303. struct hlist_node link;
  304. };
  305. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  306. struct kvm_irq_routing_table {
  307. int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
  308. u32 nr_rt_entries;
  309. /*
  310. * Array indexed by gsi. Each entry contains list of irq chips
  311. * the gsi is connected to.
  312. */
  313. struct hlist_head map[0];
  314. };
  315. #endif
  316. #ifndef KVM_PRIVATE_MEM_SLOTS
  317. #define KVM_PRIVATE_MEM_SLOTS 0
  318. #endif
  319. #ifndef KVM_MEM_SLOTS_NUM
  320. #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  321. #endif
  322. #ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
  323. static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu)
  324. {
  325. return 0;
  326. }
  327. #endif
  328. /*
  329. * Note:
  330. * memslots are not sorted by id anymore, please use id_to_memslot()
  331. * to get the memslot by its id.
  332. */
  333. struct kvm_memslots {
  334. u64 generation;
  335. struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM];
  336. /* The mapping table from slot id to the index in memslots[]. */
  337. short id_to_index[KVM_MEM_SLOTS_NUM];
  338. atomic_t lru_slot;
  339. int used_slots;
  340. };
  341. struct kvm {
  342. spinlock_t mmu_lock;
  343. struct mutex slots_lock;
  344. struct mm_struct *mm; /* userspace tied to this vm */
  345. struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM];
  346. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  347. /*
  348. * created_vcpus is protected by kvm->lock, and is incremented
  349. * at the beginning of KVM_CREATE_VCPU. online_vcpus is only
  350. * incremented after storing the kvm_vcpu pointer in vcpus,
  351. * and is accessed atomically.
  352. */
  353. atomic_t online_vcpus;
  354. int created_vcpus;
  355. int last_boosted_vcpu;
  356. struct list_head vm_list;
  357. struct mutex lock;
  358. struct kvm_io_bus __rcu *buses[KVM_NR_BUSES];
  359. #ifdef CONFIG_HAVE_KVM_EVENTFD
  360. struct {
  361. spinlock_t lock;
  362. struct list_head items;
  363. struct list_head resampler_list;
  364. struct mutex resampler_lock;
  365. } irqfds;
  366. struct list_head ioeventfds;
  367. #endif
  368. struct kvm_vm_stat stat;
  369. struct kvm_arch arch;
  370. refcount_t users_count;
  371. #ifdef CONFIG_KVM_MMIO
  372. struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
  373. spinlock_t ring_lock;
  374. struct list_head coalesced_zones;
  375. #endif
  376. struct mutex irq_lock;
  377. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  378. /*
  379. * Update side is protected by irq_lock.
  380. */
  381. struct kvm_irq_routing_table __rcu *irq_routing;
  382. #endif
  383. #ifdef CONFIG_HAVE_KVM_IRQFD
  384. struct hlist_head irq_ack_notifier_list;
  385. #endif
  386. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  387. struct mmu_notifier mmu_notifier;
  388. unsigned long mmu_notifier_seq;
  389. long mmu_notifier_count;
  390. #endif
  391. long tlbs_dirty;
  392. struct list_head devices;
  393. struct dentry *debugfs_dentry;
  394. struct kvm_stat_data **debugfs_stat_data;
  395. struct srcu_struct srcu;
  396. struct srcu_struct irq_srcu;
  397. pid_t userspace_pid;
  398. };
  399. #define kvm_err(fmt, ...) \
  400. pr_err("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  401. #define kvm_info(fmt, ...) \
  402. pr_info("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  403. #define kvm_debug(fmt, ...) \
  404. pr_debug("kvm [%i]: " fmt, task_pid_nr(current), ## __VA_ARGS__)
  405. #define kvm_debug_ratelimited(fmt, ...) \
  406. pr_debug_ratelimited("kvm [%i]: " fmt, task_pid_nr(current), \
  407. ## __VA_ARGS__)
  408. #define kvm_pr_unimpl(fmt, ...) \
  409. pr_err_ratelimited("kvm [%i]: " fmt, \
  410. task_tgid_nr(current), ## __VA_ARGS__)
  411. /* The guest did something we don't support. */
  412. #define vcpu_unimpl(vcpu, fmt, ...) \
  413. kvm_pr_unimpl("vcpu%i, guest rIP: 0x%lx " fmt, \
  414. (vcpu)->vcpu_id, kvm_rip_read(vcpu), ## __VA_ARGS__)
  415. #define vcpu_debug(vcpu, fmt, ...) \
  416. kvm_debug("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
  417. #define vcpu_debug_ratelimited(vcpu, fmt, ...) \
  418. kvm_debug_ratelimited("vcpu%i " fmt, (vcpu)->vcpu_id, \
  419. ## __VA_ARGS__)
  420. #define vcpu_err(vcpu, fmt, ...) \
  421. kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__)
  422. static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx)
  423. {
  424. return srcu_dereference_check(kvm->buses[idx], &kvm->srcu,
  425. lockdep_is_held(&kvm->slots_lock) ||
  426. !refcount_read(&kvm->users_count));
  427. }
  428. static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)
  429. {
  430. /* Pairs with smp_wmb() in kvm_vm_ioctl_create_vcpu, in case
  431. * the caller has read kvm->online_vcpus before (as is the case
  432. * for kvm_for_each_vcpu, for example).
  433. */
  434. smp_rmb();
  435. return kvm->vcpus[i];
  436. }
  437. #define kvm_for_each_vcpu(idx, vcpup, kvm) \
  438. for (idx = 0; \
  439. idx < atomic_read(&kvm->online_vcpus) && \
  440. (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
  441. idx++)
  442. static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
  443. {
  444. struct kvm_vcpu *vcpu = NULL;
  445. int i;
  446. if (id < 0)
  447. return NULL;
  448. if (id < KVM_MAX_VCPUS)
  449. vcpu = kvm_get_vcpu(kvm, id);
  450. if (vcpu && vcpu->vcpu_id == id)
  451. return vcpu;
  452. kvm_for_each_vcpu(i, vcpu, kvm)
  453. if (vcpu->vcpu_id == id)
  454. return vcpu;
  455. return NULL;
  456. }
  457. static inline int kvm_vcpu_get_idx(struct kvm_vcpu *vcpu)
  458. {
  459. struct kvm_vcpu *tmp;
  460. int idx;
  461. kvm_for_each_vcpu(idx, tmp, vcpu->kvm)
  462. if (tmp == vcpu)
  463. return idx;
  464. BUG();
  465. }
  466. #define kvm_for_each_memslot(memslot, slots) \
  467. for (memslot = &slots->memslots[0]; \
  468. memslot < slots->memslots + KVM_MEM_SLOTS_NUM && memslot->npages;\
  469. memslot++)
  470. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  471. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  472. void vcpu_load(struct kvm_vcpu *vcpu);
  473. void vcpu_put(struct kvm_vcpu *vcpu);
  474. #ifdef __KVM_HAVE_IOAPIC
  475. void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm);
  476. void kvm_arch_post_irq_routing_update(struct kvm *kvm);
  477. #else
  478. static inline void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm)
  479. {
  480. }
  481. static inline void kvm_arch_post_irq_routing_update(struct kvm *kvm)
  482. {
  483. }
  484. #endif
  485. #ifdef CONFIG_HAVE_KVM_IRQFD
  486. int kvm_irqfd_init(void);
  487. void kvm_irqfd_exit(void);
  488. #else
  489. static inline int kvm_irqfd_init(void)
  490. {
  491. return 0;
  492. }
  493. static inline void kvm_irqfd_exit(void)
  494. {
  495. }
  496. #endif
  497. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  498. struct module *module);
  499. void kvm_exit(void);
  500. void kvm_get_kvm(struct kvm *kvm);
  501. void kvm_put_kvm(struct kvm *kvm);
  502. static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)
  503. {
  504. return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu,
  505. lockdep_is_held(&kvm->slots_lock) ||
  506. !refcount_read(&kvm->users_count));
  507. }
  508. static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
  509. {
  510. return __kvm_memslots(kvm, 0);
  511. }
  512. static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu)
  513. {
  514. int as_id = kvm_arch_vcpu_memslots_id(vcpu);
  515. return __kvm_memslots(vcpu->kvm, as_id);
  516. }
  517. static inline struct kvm_memory_slot *
  518. id_to_memslot(struct kvm_memslots *slots, int id)
  519. {
  520. int index = slots->id_to_index[id];
  521. struct kvm_memory_slot *slot;
  522. slot = &slots->memslots[index];
  523. WARN_ON(slot->id != id);
  524. return slot;
  525. }
  526. /*
  527. * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
  528. * - create a new memory slot
  529. * - delete an existing memory slot
  530. * - modify an existing memory slot
  531. * -- move it in the guest physical memory space
  532. * -- just change its flags
  533. *
  534. * Since flags can be changed by some of these operations, the following
  535. * differentiation is the best we can do for __kvm_set_memory_region():
  536. */
  537. enum kvm_mr_change {
  538. KVM_MR_CREATE,
  539. KVM_MR_DELETE,
  540. KVM_MR_MOVE,
  541. KVM_MR_FLAGS_ONLY,
  542. };
  543. int kvm_set_memory_region(struct kvm *kvm,
  544. const struct kvm_userspace_memory_region *mem);
  545. int __kvm_set_memory_region(struct kvm *kvm,
  546. const struct kvm_userspace_memory_region *mem);
  547. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  548. struct kvm_memory_slot *dont);
  549. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  550. unsigned long npages);
  551. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots);
  552. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  553. struct kvm_memory_slot *memslot,
  554. const struct kvm_userspace_memory_region *mem,
  555. enum kvm_mr_change change);
  556. void kvm_arch_commit_memory_region(struct kvm *kvm,
  557. const struct kvm_userspace_memory_region *mem,
  558. const struct kvm_memory_slot *old,
  559. const struct kvm_memory_slot *new,
  560. enum kvm_mr_change change);
  561. bool kvm_largepages_enabled(void);
  562. void kvm_disable_largepages(void);
  563. /* flush all memory translations */
  564. void kvm_arch_flush_shadow_all(struct kvm *kvm);
  565. /* flush memory translations pointing to 'slot' */
  566. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  567. struct kvm_memory_slot *slot);
  568. int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  569. struct page **pages, int nr_pages);
  570. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  571. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
  572. unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable);
  573. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  574. unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn,
  575. bool *writable);
  576. void kvm_release_page_clean(struct page *page);
  577. void kvm_release_page_dirty(struct page *page);
  578. void kvm_set_page_accessed(struct page *page);
  579. kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn);
  580. kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
  581. kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  582. bool *writable);
  583. kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn);
  584. kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn);
  585. kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
  586. bool atomic, bool *async, bool write_fault,
  587. bool *writable);
  588. void kvm_release_pfn_clean(kvm_pfn_t pfn);
  589. void kvm_release_pfn_dirty(kvm_pfn_t pfn);
  590. void kvm_set_pfn_dirty(kvm_pfn_t pfn);
  591. void kvm_set_pfn_accessed(kvm_pfn_t pfn);
  592. void kvm_get_pfn(kvm_pfn_t pfn);
  593. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  594. int len);
  595. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  596. unsigned long len);
  597. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  598. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  599. void *data, unsigned long len);
  600. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  601. int offset, int len);
  602. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  603. unsigned long len);
  604. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  605. void *data, unsigned long len);
  606. int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  607. void *data, int offset, unsigned long len);
  608. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  609. gpa_t gpa, unsigned long len);
  610. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  611. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  612. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  613. bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
  614. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn);
  615. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  616. struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu);
  617. struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn);
  618. kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn);
  619. kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  620. struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn);
  621. unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
  622. unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
  623. int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset,
  624. int len);
  625. int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
  626. unsigned long len);
  627. int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
  628. unsigned long len);
  629. int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, const void *data,
  630. int offset, int len);
  631. int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
  632. unsigned long len);
  633. void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn);
  634. void kvm_sigset_activate(struct kvm_vcpu *vcpu);
  635. void kvm_sigset_deactivate(struct kvm_vcpu *vcpu);
  636. void kvm_vcpu_block(struct kvm_vcpu *vcpu);
  637. void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);
  638. void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
  639. bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
  640. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  641. int kvm_vcpu_yield_to(struct kvm_vcpu *target);
  642. void kvm_vcpu_on_spin(struct kvm_vcpu *vcpu, bool usermode_vcpu_not_eligible);
  643. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  644. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  645. void kvm_flush_remote_tlbs(struct kvm *kvm);
  646. void kvm_reload_remote_mmus(struct kvm *kvm);
  647. bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
  648. unsigned long *vcpu_bitmap, cpumask_var_t tmp);
  649. bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req);
  650. long kvm_arch_dev_ioctl(struct file *filp,
  651. unsigned int ioctl, unsigned long arg);
  652. long kvm_arch_vcpu_ioctl(struct file *filp,
  653. unsigned int ioctl, unsigned long arg);
  654. vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf);
  655. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext);
  656. int kvm_get_dirty_log(struct kvm *kvm,
  657. struct kvm_dirty_log *log, int *is_dirty);
  658. int kvm_get_dirty_log_protect(struct kvm *kvm,
  659. struct kvm_dirty_log *log, bool *is_dirty);
  660. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  661. struct kvm_memory_slot *slot,
  662. gfn_t gfn_offset,
  663. unsigned long mask);
  664. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  665. struct kvm_dirty_log *log);
  666. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
  667. bool line_status);
  668. long kvm_arch_vm_ioctl(struct file *filp,
  669. unsigned int ioctl, unsigned long arg);
  670. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  671. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  672. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  673. struct kvm_translation *tr);
  674. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  675. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  676. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  677. struct kvm_sregs *sregs);
  678. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  679. struct kvm_sregs *sregs);
  680. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  681. struct kvm_mp_state *mp_state);
  682. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  683. struct kvm_mp_state *mp_state);
  684. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  685. struct kvm_guest_debug *dbg);
  686. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  687. int kvm_arch_init(void *opaque);
  688. void kvm_arch_exit(void);
  689. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  690. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  691. void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu);
  692. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  693. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  694. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  695. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  696. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
  697. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
  698. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
  699. bool kvm_arch_has_vcpu_debugfs(void);
  700. int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu);
  701. int kvm_arch_hardware_enable(void);
  702. void kvm_arch_hardware_disable(void);
  703. int kvm_arch_hardware_setup(void);
  704. void kvm_arch_hardware_unsetup(void);
  705. void kvm_arch_check_processor_compat(void *rtn);
  706. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
  707. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);
  708. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
  709. #ifndef __KVM_HAVE_ARCH_VM_ALLOC
  710. /*
  711. * All architectures that want to use vzalloc currently also
  712. * need their own kvm_arch_alloc_vm implementation.
  713. */
  714. static inline struct kvm *kvm_arch_alloc_vm(void)
  715. {
  716. return kzalloc(sizeof(struct kvm), GFP_KERNEL);
  717. }
  718. static inline void kvm_arch_free_vm(struct kvm *kvm)
  719. {
  720. kfree(kvm);
  721. }
  722. #endif
  723. #ifndef __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
  724. static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
  725. {
  726. return -ENOTSUPP;
  727. }
  728. #endif
  729. #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA
  730. void kvm_arch_register_noncoherent_dma(struct kvm *kvm);
  731. void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm);
  732. bool kvm_arch_has_noncoherent_dma(struct kvm *kvm);
  733. #else
  734. static inline void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
  735. {
  736. }
  737. static inline void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
  738. {
  739. }
  740. static inline bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
  741. {
  742. return false;
  743. }
  744. #endif
  745. #ifdef __KVM_HAVE_ARCH_ASSIGNED_DEVICE
  746. void kvm_arch_start_assignment(struct kvm *kvm);
  747. void kvm_arch_end_assignment(struct kvm *kvm);
  748. bool kvm_arch_has_assigned_device(struct kvm *kvm);
  749. #else
  750. static inline void kvm_arch_start_assignment(struct kvm *kvm)
  751. {
  752. }
  753. static inline void kvm_arch_end_assignment(struct kvm *kvm)
  754. {
  755. }
  756. static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
  757. {
  758. return false;
  759. }
  760. #endif
  761. static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  762. {
  763. #ifdef __KVM_HAVE_ARCH_WQP
  764. return vcpu->arch.wqp;
  765. #else
  766. return &vcpu->wq;
  767. #endif
  768. }
  769. #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED
  770. /*
  771. * returns true if the virtual interrupt controller is initialized and
  772. * ready to accept virtual IRQ. On some architectures the virtual interrupt
  773. * controller is dynamically instantiated and this is not always true.
  774. */
  775. bool kvm_arch_intc_initialized(struct kvm *kvm);
  776. #else
  777. static inline bool kvm_arch_intc_initialized(struct kvm *kvm)
  778. {
  779. return true;
  780. }
  781. #endif
  782. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type);
  783. void kvm_arch_destroy_vm(struct kvm *kvm);
  784. void kvm_arch_sync_events(struct kvm *kvm);
  785. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
  786. void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
  787. bool kvm_is_reserved_pfn(kvm_pfn_t pfn);
  788. struct kvm_irq_ack_notifier {
  789. struct hlist_node link;
  790. unsigned gsi;
  791. void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
  792. };
  793. int kvm_irq_map_gsi(struct kvm *kvm,
  794. struct kvm_kernel_irq_routing_entry *entries, int gsi);
  795. int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin);
  796. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  797. bool line_status);
  798. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
  799. int irq_source_id, int level, bool line_status);
  800. int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
  801. struct kvm *kvm, int irq_source_id,
  802. int level, bool line_status);
  803. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
  804. void kvm_notify_acked_gsi(struct kvm *kvm, int gsi);
  805. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
  806. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  807. struct kvm_irq_ack_notifier *kian);
  808. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  809. struct kvm_irq_ack_notifier *kian);
  810. int kvm_request_irq_source_id(struct kvm *kvm);
  811. void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
  812. /*
  813. * search_memslots() and __gfn_to_memslot() are here because they are
  814. * used in non-modular code in arch/powerpc/kvm/book3s_hv_rm_mmu.c.
  815. * gfn_to_memslot() itself isn't here as an inline because that would
  816. * bloat other code too much.
  817. */
  818. static inline struct kvm_memory_slot *
  819. search_memslots(struct kvm_memslots *slots, gfn_t gfn)
  820. {
  821. int start = 0, end = slots->used_slots;
  822. int slot = atomic_read(&slots->lru_slot);
  823. struct kvm_memory_slot *memslots = slots->memslots;
  824. if (gfn >= memslots[slot].base_gfn &&
  825. gfn < memslots[slot].base_gfn + memslots[slot].npages)
  826. return &memslots[slot];
  827. while (start < end) {
  828. slot = start + (end - start) / 2;
  829. if (gfn >= memslots[slot].base_gfn)
  830. end = slot;
  831. else
  832. start = slot + 1;
  833. }
  834. if (gfn >= memslots[start].base_gfn &&
  835. gfn < memslots[start].base_gfn + memslots[start].npages) {
  836. atomic_set(&slots->lru_slot, start);
  837. return &memslots[start];
  838. }
  839. return NULL;
  840. }
  841. static inline struct kvm_memory_slot *
  842. __gfn_to_memslot(struct kvm_memslots *slots, gfn_t gfn)
  843. {
  844. return search_memslots(slots, gfn);
  845. }
  846. static inline unsigned long
  847. __gfn_to_hva_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  848. {
  849. return slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE;
  850. }
  851. static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
  852. {
  853. return gfn_to_memslot(kvm, gfn)->id;
  854. }
  855. static inline gfn_t
  856. hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot)
  857. {
  858. gfn_t gfn_offset = (hva - slot->userspace_addr) >> PAGE_SHIFT;
  859. return slot->base_gfn + gfn_offset;
  860. }
  861. static inline gpa_t gfn_to_gpa(gfn_t gfn)
  862. {
  863. return (gpa_t)gfn << PAGE_SHIFT;
  864. }
  865. static inline gfn_t gpa_to_gfn(gpa_t gpa)
  866. {
  867. return (gfn_t)(gpa >> PAGE_SHIFT);
  868. }
  869. static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn)
  870. {
  871. return (hpa_t)pfn << PAGE_SHIFT;
  872. }
  873. static inline struct page *kvm_vcpu_gpa_to_page(struct kvm_vcpu *vcpu,
  874. gpa_t gpa)
  875. {
  876. return kvm_vcpu_gfn_to_page(vcpu, gpa_to_gfn(gpa));
  877. }
  878. static inline bool kvm_is_error_gpa(struct kvm *kvm, gpa_t gpa)
  879. {
  880. unsigned long hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
  881. return kvm_is_error_hva(hva);
  882. }
  883. enum kvm_stat_kind {
  884. KVM_STAT_VM,
  885. KVM_STAT_VCPU,
  886. };
  887. struct kvm_stat_data {
  888. int offset;
  889. struct kvm *kvm;
  890. };
  891. struct kvm_stats_debugfs_item {
  892. const char *name;
  893. int offset;
  894. enum kvm_stat_kind kind;
  895. };
  896. extern struct kvm_stats_debugfs_item debugfs_entries[];
  897. extern struct dentry *kvm_debugfs_dir;
  898. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  899. static inline int mmu_notifier_retry(struct kvm *kvm, unsigned long mmu_seq)
  900. {
  901. if (unlikely(kvm->mmu_notifier_count))
  902. return 1;
  903. /*
  904. * Ensure the read of mmu_notifier_count happens before the read
  905. * of mmu_notifier_seq. This interacts with the smp_wmb() in
  906. * mmu_notifier_invalidate_range_end to make sure that the caller
  907. * either sees the old (non-zero) value of mmu_notifier_count or
  908. * the new (incremented) value of mmu_notifier_seq.
  909. * PowerPC Book3s HV KVM calls this under a per-page lock
  910. * rather than under kvm->mmu_lock, for scalability, so
  911. * can't rely on kvm->mmu_lock to keep things ordered.
  912. */
  913. smp_rmb();
  914. if (kvm->mmu_notifier_seq != mmu_seq)
  915. return 1;
  916. return 0;
  917. }
  918. #endif
  919. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  920. #define KVM_MAX_IRQ_ROUTES 4096 /* might need extension/rework in the future */
  921. bool kvm_arch_can_set_irq_routing(struct kvm *kvm);
  922. int kvm_set_irq_routing(struct kvm *kvm,
  923. const struct kvm_irq_routing_entry *entries,
  924. unsigned nr,
  925. unsigned flags);
  926. int kvm_set_routing_entry(struct kvm *kvm,
  927. struct kvm_kernel_irq_routing_entry *e,
  928. const struct kvm_irq_routing_entry *ue);
  929. void kvm_free_irq_routing(struct kvm *kvm);
  930. #else
  931. static inline void kvm_free_irq_routing(struct kvm *kvm) {}
  932. #endif
  933. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
  934. #ifdef CONFIG_HAVE_KVM_EVENTFD
  935. void kvm_eventfd_init(struct kvm *kvm);
  936. int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
  937. #ifdef CONFIG_HAVE_KVM_IRQFD
  938. int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
  939. void kvm_irqfd_release(struct kvm *kvm);
  940. void kvm_irq_routing_update(struct kvm *);
  941. #else
  942. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  943. {
  944. return -EINVAL;
  945. }
  946. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  947. #endif
  948. #else
  949. static inline void kvm_eventfd_init(struct kvm *kvm) {}
  950. static inline int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  951. {
  952. return -EINVAL;
  953. }
  954. static inline void kvm_irqfd_release(struct kvm *kvm) {}
  955. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  956. static inline void kvm_irq_routing_update(struct kvm *kvm)
  957. {
  958. }
  959. #endif
  960. static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  961. {
  962. return -ENOSYS;
  963. }
  964. #endif /* CONFIG_HAVE_KVM_EVENTFD */
  965. void kvm_arch_irq_routing_update(struct kvm *kvm);
  966. static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
  967. {
  968. /*
  969. * Ensure the rest of the request is published to kvm_check_request's
  970. * caller. Paired with the smp_mb__after_atomic in kvm_check_request.
  971. */
  972. smp_wmb();
  973. set_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
  974. }
  975. static inline bool kvm_request_pending(struct kvm_vcpu *vcpu)
  976. {
  977. return READ_ONCE(vcpu->requests);
  978. }
  979. static inline bool kvm_test_request(int req, struct kvm_vcpu *vcpu)
  980. {
  981. return test_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
  982. }
  983. static inline void kvm_clear_request(int req, struct kvm_vcpu *vcpu)
  984. {
  985. clear_bit(req & KVM_REQUEST_MASK, (void *)&vcpu->requests);
  986. }
  987. static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
  988. {
  989. if (kvm_test_request(req, vcpu)) {
  990. kvm_clear_request(req, vcpu);
  991. /*
  992. * Ensure the rest of the request is visible to kvm_check_request's
  993. * caller. Paired with the smp_wmb in kvm_make_request.
  994. */
  995. smp_mb__after_atomic();
  996. return true;
  997. } else {
  998. return false;
  999. }
  1000. }
  1001. extern bool kvm_rebooting;
  1002. extern unsigned int halt_poll_ns;
  1003. extern unsigned int halt_poll_ns_grow;
  1004. extern unsigned int halt_poll_ns_shrink;
  1005. struct kvm_device {
  1006. struct kvm_device_ops *ops;
  1007. struct kvm *kvm;
  1008. void *private;
  1009. struct list_head vm_node;
  1010. };
  1011. /* create, destroy, and name are mandatory */
  1012. struct kvm_device_ops {
  1013. const char *name;
  1014. /*
  1015. * create is called holding kvm->lock and any operations not suitable
  1016. * to do while holding the lock should be deferred to init (see
  1017. * below).
  1018. */
  1019. int (*create)(struct kvm_device *dev, u32 type);
  1020. /*
  1021. * init is called after create if create is successful and is called
  1022. * outside of holding kvm->lock.
  1023. */
  1024. void (*init)(struct kvm_device *dev);
  1025. /*
  1026. * Destroy is responsible for freeing dev.
  1027. *
  1028. * Destroy may be called before or after destructors are called
  1029. * on emulated I/O regions, depending on whether a reference is
  1030. * held by a vcpu or other kvm component that gets destroyed
  1031. * after the emulated I/O.
  1032. */
  1033. void (*destroy)(struct kvm_device *dev);
  1034. int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  1035. int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  1036. int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
  1037. long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
  1038. unsigned long arg);
  1039. };
  1040. void kvm_device_get(struct kvm_device *dev);
  1041. void kvm_device_put(struct kvm_device *dev);
  1042. struct kvm_device *kvm_device_from_filp(struct file *filp);
  1043. int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type);
  1044. void kvm_unregister_device_ops(u32 type);
  1045. extern struct kvm_device_ops kvm_mpic_ops;
  1046. extern struct kvm_device_ops kvm_arm_vgic_v2_ops;
  1047. extern struct kvm_device_ops kvm_arm_vgic_v3_ops;
  1048. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1049. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  1050. {
  1051. vcpu->spin_loop.in_spin_loop = val;
  1052. }
  1053. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  1054. {
  1055. vcpu->spin_loop.dy_eligible = val;
  1056. }
  1057. #else /* !CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  1058. static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
  1059. {
  1060. }
  1061. static inline void kvm_vcpu_set_dy_eligible(struct kvm_vcpu *vcpu, bool val)
  1062. {
  1063. }
  1064. #endif /* CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT */
  1065. #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
  1066. bool kvm_arch_has_irq_bypass(void);
  1067. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *,
  1068. struct irq_bypass_producer *);
  1069. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *,
  1070. struct irq_bypass_producer *);
  1071. void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *);
  1072. void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *);
  1073. int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
  1074. uint32_t guest_irq, bool set);
  1075. #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */
  1076. #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS
  1077. /* If we wakeup during the poll time, was it a sucessful poll? */
  1078. static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
  1079. {
  1080. return vcpu->valid_wakeup;
  1081. }
  1082. #else
  1083. static inline bool vcpu_valid_wakeup(struct kvm_vcpu *vcpu)
  1084. {
  1085. return true;
  1086. }
  1087. #endif /* CONFIG_HAVE_KVM_INVALID_WAKEUPS */
  1088. #ifdef CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL
  1089. long kvm_arch_vcpu_async_ioctl(struct file *filp,
  1090. unsigned int ioctl, unsigned long arg);
  1091. #else
  1092. static inline long kvm_arch_vcpu_async_ioctl(struct file *filp,
  1093. unsigned int ioctl,
  1094. unsigned long arg)
  1095. {
  1096. return -ENOIOCTLCMD;
  1097. }
  1098. #endif /* CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL */
  1099. int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
  1100. unsigned long start, unsigned long end, bool blockable);
  1101. #ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE
  1102. int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu);
  1103. #else
  1104. static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
  1105. {
  1106. return 0;
  1107. }
  1108. #endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */
  1109. #endif