gmap.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * KVM guest address space mapping code
  4. *
  5. * Copyright IBM Corp. 2007, 2016, 2025
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Claudio Imbrenda <imbrenda@linux.ibm.com>
  8. */
  9. #ifndef ARCH_KVM_S390_GMAP_H
  10. #define ARCH_KVM_S390_GMAP_H
  11. #include "dat.h"
  12. /**
  13. * enum gmap_flags - Flags of a gmap.
  14. *
  15. * @GMAP_FLAG_SHADOW: The gmap is a vsie shadow gmap.
  16. * @GMAP_FLAG_OWNS_PAGETABLES: The gmap owns all dat levels; normally 1, is 0
  17. * only for ucontrol per-cpu gmaps, since they
  18. * share the page tables with the main gmap.
  19. * @GMAP_FLAG_IS_UCONTROL: The gmap is ucontrol (main gmap or per-cpu gmap).
  20. * @GMAP_FLAG_ALLOW_HPAGE_1M: 1M hugepages are allowed for this gmap,
  21. * independently of the page size used by userspace.
  22. * @GMAP_FLAG_ALLOW_HPAGE_2G: 2G hugepages are allowed for this gmap,
  23. * independently of the page size used by userspace.
  24. * @GMAP_FLAG_PFAULT_ENABLED: Pfault is enabled for the gmap.
  25. * @GMAP_FLAG_USES_SKEYS: If the guest uses storage keys.
  26. * @GMAP_FLAG_USES_CMM: Whether the guest uses CMMA.
  27. * @GMAP_FLAG_EXPORT_ON_UNMAP: Whether to export guest pages when unmapping.
  28. */
  29. enum gmap_flags {
  30. GMAP_FLAG_SHADOW = 0,
  31. GMAP_FLAG_OWNS_PAGETABLES,
  32. GMAP_FLAG_IS_UCONTROL,
  33. GMAP_FLAG_ALLOW_HPAGE_1M,
  34. GMAP_FLAG_ALLOW_HPAGE_2G,
  35. GMAP_FLAG_PFAULT_ENABLED,
  36. GMAP_FLAG_USES_SKEYS,
  37. GMAP_FLAG_USES_CMM,
  38. GMAP_FLAG_EXPORT_ON_UNMAP,
  39. };
  40. /**
  41. * struct gmap_struct - Guest address space.
  42. *
  43. * @flags: GMAP_FLAG_* flags.
  44. * @edat_level: The edat level of this shadow gmap.
  45. * @kvm: The vm.
  46. * @asce: The ASCE used by this gmap.
  47. * @list: List head used in children gmaps for the children gmap list.
  48. * @children_lock: Protects children and scb_users.
  49. * @children: List of child gmaps of this gmap.
  50. * @scb_users: List of vsie_scb that use this shadow gmap.
  51. * @parent: Parent gmap of a child gmap.
  52. * @guest_asce: Original ASCE of this shadow gmap.
  53. * @host_to_rmap_lock: Protects host_to_rmap.
  54. * @host_to_rmap: Radix tree mapping host addresses to guest addresses.
  55. */
  56. struct gmap {
  57. unsigned long flags;
  58. unsigned char edat_level;
  59. bool invalidated;
  60. struct kvm *kvm;
  61. union asce asce;
  62. struct list_head list;
  63. spinlock_t children_lock; /* Protects: children, scb_users */
  64. struct list_head children;
  65. struct list_head scb_users;
  66. struct gmap *parent;
  67. union asce guest_asce;
  68. spinlock_t host_to_rmap_lock; /* Protects host_to_rmap */
  69. struct radix_tree_root host_to_rmap;
  70. refcount_t refcount;
  71. };
  72. struct gmap_cache {
  73. struct list_head list;
  74. struct gmap *gmap;
  75. };
  76. #define gmap_for_each_rmap_safe(pos, n, head) \
  77. for (pos = (head); n = pos ? pos->next : NULL, pos; pos = n)
  78. int s390_replace_asce(struct gmap *gmap);
  79. bool _gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end, bool hint);
  80. bool gmap_age_gfn(struct gmap *gmap, gfn_t start, gfn_t end);
  81. bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t start, gfn_t end);
  82. int gmap_try_fixup_minor(struct gmap *gmap, struct guest_fault *fault);
  83. struct gmap *gmap_new(struct kvm *kvm, gfn_t limit);
  84. struct gmap *gmap_new_child(struct gmap *parent, gfn_t limit);
  85. void gmap_remove_child(struct gmap *child);
  86. void gmap_dispose(struct gmap *gmap);
  87. int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *fault);
  88. void gmap_sync_dirty_log(struct gmap *gmap, gfn_t start, gfn_t end);
  89. int gmap_set_limit(struct gmap *gmap, gfn_t limit);
  90. int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t *gaddr);
  91. int gmap_ucas_map(struct gmap *gmap, gfn_t p_gfn, gfn_t c_gfn, unsigned long count);
  92. void gmap_ucas_unmap(struct gmap *gmap, gfn_t c_gfn, unsigned long count);
  93. int gmap_enable_skeys(struct gmap *gmap);
  94. int gmap_pv_destroy_range(struct gmap *gmap, gfn_t start, gfn_t end, bool interruptible);
  95. int gmap_insert_rmap(struct gmap *sg, gfn_t p_gfn, gfn_t r_gfn, int level);
  96. int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn, gfn_t r_gfn,
  97. kvm_pfn_t pfn, int level, bool wr);
  98. void gmap_set_cmma_all_dirty(struct gmap *gmap);
  99. void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn);
  100. struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *gmap,
  101. union asce asce, int edat_level);
  102. void gmap_split_huge_pages(struct gmap *gmap);
  103. static inline bool uses_skeys(struct gmap *gmap)
  104. {
  105. return test_bit(GMAP_FLAG_USES_SKEYS, &gmap->flags);
  106. }
  107. static inline bool uses_cmm(struct gmap *gmap)
  108. {
  109. return test_bit(GMAP_FLAG_USES_CMM, &gmap->flags);
  110. }
  111. static inline bool pfault_enabled(struct gmap *gmap)
  112. {
  113. return test_bit(GMAP_FLAG_PFAULT_ENABLED, &gmap->flags);
  114. }
  115. static inline bool is_ucontrol(struct gmap *gmap)
  116. {
  117. return test_bit(GMAP_FLAG_IS_UCONTROL, &gmap->flags);
  118. }
  119. static inline bool is_shadow(struct gmap *gmap)
  120. {
  121. return test_bit(GMAP_FLAG_SHADOW, &gmap->flags);
  122. }
  123. static inline bool owns_page_tables(struct gmap *gmap)
  124. {
  125. return test_bit(GMAP_FLAG_OWNS_PAGETABLES, &gmap->flags);
  126. }
  127. static inline struct gmap *gmap_put(struct gmap *gmap)
  128. {
  129. if (refcount_dec_and_test(&gmap->refcount))
  130. gmap_dispose(gmap);
  131. return NULL;
  132. }
  133. static inline void gmap_get(struct gmap *gmap)
  134. {
  135. WARN_ON_ONCE(unlikely(!refcount_inc_not_zero(&gmap->refcount)));
  136. }
  137. static inline void gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn)
  138. {
  139. scoped_guard(spinlock, &parent->children_lock)
  140. _gmap_handle_vsie_unshadow_event(parent, gfn);
  141. }
  142. static inline bool gmap_mkold_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end)
  143. {
  144. return _gmap_unmap_prefix(gmap, gfn, end, true);
  145. }
  146. static inline bool gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end)
  147. {
  148. return _gmap_unmap_prefix(gmap, gfn, end, false);
  149. }
  150. static inline union pgste _gmap_ptep_xchg(struct gmap *gmap, union pte *ptep, union pte newpte,
  151. union pgste pgste, gfn_t gfn, bool needs_lock)
  152. {
  153. lockdep_assert_held(&gmap->kvm->mmu_lock);
  154. if (!needs_lock)
  155. lockdep_assert_held(&gmap->children_lock);
  156. else
  157. lockdep_assert_not_held(&gmap->children_lock);
  158. if (pgste.prefix_notif && (newpte.h.p || newpte.h.i)) {
  159. pgste.prefix_notif = 0;
  160. gmap_unmap_prefix(gmap, gfn, gfn + 1);
  161. }
  162. if (pgste.vsie_notif && (ptep->h.p != newpte.h.p || newpte.h.i)) {
  163. pgste.vsie_notif = 0;
  164. if (needs_lock)
  165. gmap_handle_vsie_unshadow_event(gmap, gfn);
  166. else
  167. _gmap_handle_vsie_unshadow_event(gmap, gfn);
  168. }
  169. if (!ptep->s.d && newpte.s.d && !newpte.s.s)
  170. SetPageDirty(pfn_to_page(newpte.h.pfra));
  171. return __dat_ptep_xchg(ptep, pgste, newpte, gfn, gmap->asce, uses_skeys(gmap));
  172. }
  173. static inline union pgste gmap_ptep_xchg(struct gmap *gmap, union pte *ptep, union pte newpte,
  174. union pgste pgste, gfn_t gfn)
  175. {
  176. return _gmap_ptep_xchg(gmap, ptep, newpte, pgste, gfn, true);
  177. }
  178. static inline bool __must_check _gmap_crstep_xchg_atomic(struct gmap *gmap, union crste *crstep,
  179. union crste oldcrste, union crste newcrste,
  180. gfn_t gfn, bool needs_lock)
  181. {
  182. unsigned long align = is_pmd(newcrste) ? _PAGE_ENTRIES : _PAGE_ENTRIES * _CRST_ENTRIES;
  183. if (KVM_BUG_ON(crstep->h.tt != oldcrste.h.tt || newcrste.h.tt != oldcrste.h.tt, gmap->kvm))
  184. return true;
  185. lockdep_assert_held(&gmap->kvm->mmu_lock);
  186. if (!needs_lock)
  187. lockdep_assert_held(&gmap->children_lock);
  188. gfn = ALIGN_DOWN(gfn, align);
  189. if (crste_prefix(oldcrste) && (newcrste.h.p || newcrste.h.i || !crste_prefix(newcrste))) {
  190. newcrste.s.fc1.prefix_notif = 0;
  191. gmap_unmap_prefix(gmap, gfn, gfn + align);
  192. }
  193. if (crste_leaf(oldcrste) && oldcrste.s.fc1.vsie_notif &&
  194. (newcrste.h.p || newcrste.h.i || !newcrste.s.fc1.vsie_notif)) {
  195. newcrste.s.fc1.vsie_notif = 0;
  196. if (needs_lock)
  197. gmap_handle_vsie_unshadow_event(gmap, gfn);
  198. else
  199. _gmap_handle_vsie_unshadow_event(gmap, gfn);
  200. }
  201. if (!oldcrste.s.fc1.d && newcrste.s.fc1.d && !newcrste.s.fc1.s)
  202. SetPageDirty(phys_to_page(crste_origin_large(newcrste)));
  203. return dat_crstep_xchg_atomic(crstep, oldcrste, newcrste, gfn, gmap->asce);
  204. }
  205. static inline bool __must_check gmap_crstep_xchg_atomic(struct gmap *gmap, union crste *crstep,
  206. union crste oldcrste, union crste newcrste,
  207. gfn_t gfn)
  208. {
  209. return _gmap_crstep_xchg_atomic(gmap, crstep, oldcrste, newcrste, gfn, true);
  210. }
  211. /**
  212. * gmap_is_shadow_valid() - check if a shadow guest address space matches the
  213. * given properties and is still valid.
  214. * @sg: Pointer to the shadow guest address space structure.
  215. * @asce: ASCE for which the shadow table is requested.
  216. * @edat_level: Edat level to be used for the shadow translation.
  217. *
  218. * Return: true if the gmap shadow is still valid and matches the given
  219. * properties and the caller can continue using it; false otherwise, the
  220. * caller has to request a new shadow gmap in this case.
  221. */
  222. static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int edat_level)
  223. {
  224. return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
  225. }
  226. #endif /* ARCH_KVM_S390_GMAP_H */