kvm_host.h 43 KB

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