page.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_XEN_PAGE_H
  3. #define _ASM_X86_XEN_PAGE_H
  4. #include <linux/kernel.h>
  5. #include <linux/types.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/pfn.h>
  8. #include <linux/mm.h>
  9. #include <linux/device.h>
  10. #include <asm/extable.h>
  11. #include <asm/page.h>
  12. #include <xen/interface/xen.h>
  13. #include <xen/interface/grant_table.h>
  14. #include <xen/features.h>
  15. /* Xen machine address */
  16. typedef struct xmaddr {
  17. phys_addr_t maddr;
  18. } xmaddr_t;
  19. /* Xen pseudo-physical address */
  20. typedef struct xpaddr {
  21. phys_addr_t paddr;
  22. } xpaddr_t;
  23. #ifdef CONFIG_X86_64
  24. #define XEN_PHYSICAL_MASK __sme_clr((1UL << 52) - 1)
  25. #else
  26. #define XEN_PHYSICAL_MASK __PHYSICAL_MASK
  27. #endif
  28. #define XEN_PTE_MFN_MASK ((pteval_t)(((signed long)PAGE_MASK) & \
  29. XEN_PHYSICAL_MASK))
  30. #define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
  31. #define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
  32. /**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
  33. #define INVALID_P2M_ENTRY (~0UL)
  34. #define FOREIGN_FRAME_BIT (1UL<<(BITS_PER_LONG-1))
  35. #define IDENTITY_FRAME_BIT (1UL<<(BITS_PER_LONG-2))
  36. #define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
  37. #define IDENTITY_FRAME(m) ((m) | IDENTITY_FRAME_BIT)
  38. #define P2M_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  39. extern unsigned long *machine_to_phys_mapping;
  40. extern unsigned long machine_to_phys_nr;
  41. extern unsigned long *xen_p2m_addr;
  42. extern unsigned long xen_p2m_size;
  43. extern unsigned long xen_max_p2m_pfn;
  44. extern int xen_alloc_p2m_entry(unsigned long pfn);
  45. extern unsigned long get_phys_to_machine(unsigned long pfn);
  46. extern bool set_phys_to_machine(unsigned long pfn, unsigned long mfn);
  47. extern bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn);
  48. extern unsigned long __init set_phys_range_identity(unsigned long pfn_s,
  49. unsigned long pfn_e);
  50. #ifdef CONFIG_XEN_PV
  51. extern int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
  52. struct gnttab_map_grant_ref *kmap_ops,
  53. struct page **pages, unsigned int count);
  54. extern int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  55. struct gnttab_unmap_grant_ref *kunmap_ops,
  56. struct page **pages, unsigned int count);
  57. #else
  58. static inline int
  59. set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
  60. struct gnttab_map_grant_ref *kmap_ops,
  61. struct page **pages, unsigned int count)
  62. {
  63. return 0;
  64. }
  65. static inline int
  66. clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  67. struct gnttab_unmap_grant_ref *kunmap_ops,
  68. struct page **pages, unsigned int count)
  69. {
  70. return 0;
  71. }
  72. #endif
  73. /*
  74. * Helper functions to write or read unsigned long values to/from
  75. * memory, when the access may fault.
  76. */
  77. static inline int xen_safe_write_ulong(unsigned long *addr, unsigned long val)
  78. {
  79. int ret = 0;
  80. asm volatile("1: mov %[val], %[ptr]\n"
  81. "2:\n"
  82. _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG, %[ret])
  83. : [ret] "+r" (ret), [ptr] "=m" (*addr)
  84. : [val] "r" (val));
  85. return ret;
  86. }
  87. static inline int xen_safe_read_ulong(const unsigned long *addr,
  88. unsigned long *val)
  89. {
  90. unsigned long rval = ~0ul;
  91. int ret = 0;
  92. asm volatile("1: mov %[ptr], %[rval]\n"
  93. "2:\n"
  94. _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG, %[ret])
  95. : [ret] "+r" (ret), [rval] "+r" (rval)
  96. : [ptr] "m" (*addr));
  97. *val = rval;
  98. return ret;
  99. }
  100. #ifdef CONFIG_XEN_PV
  101. /*
  102. * When to use pfn_to_mfn(), __pfn_to_mfn() or get_phys_to_machine():
  103. * - pfn_to_mfn() returns either INVALID_P2M_ENTRY or the mfn. No indicator
  104. * bits (identity or foreign) are set.
  105. * - __pfn_to_mfn() returns the found entry of the p2m table. A possibly set
  106. * identity or foreign indicator will be still set. __pfn_to_mfn() is
  107. * encapsulating get_phys_to_machine() which is called in special cases only.
  108. * - get_phys_to_machine() is to be called by __pfn_to_mfn() only in special
  109. * cases needing an extended handling.
  110. */
  111. static inline unsigned long __pfn_to_mfn(unsigned long pfn)
  112. {
  113. unsigned long mfn;
  114. if (pfn < xen_p2m_size)
  115. mfn = xen_p2m_addr[pfn];
  116. else if (unlikely(pfn < xen_max_p2m_pfn))
  117. return get_phys_to_machine(pfn);
  118. else
  119. return IDENTITY_FRAME(pfn);
  120. if (unlikely(mfn == INVALID_P2M_ENTRY))
  121. return get_phys_to_machine(pfn);
  122. return mfn;
  123. }
  124. #else
  125. static inline unsigned long __pfn_to_mfn(unsigned long pfn)
  126. {
  127. return pfn;
  128. }
  129. #endif
  130. static inline unsigned long pfn_to_mfn(unsigned long pfn)
  131. {
  132. unsigned long mfn;
  133. /*
  134. * Some x86 code are still using pfn_to_mfn instead of
  135. * pfn_to_mfn. This will have to be removed when we figured
  136. * out which call.
  137. */
  138. if (xen_feature(XENFEAT_auto_translated_physmap))
  139. return pfn;
  140. mfn = __pfn_to_mfn(pfn);
  141. if (mfn != INVALID_P2M_ENTRY)
  142. mfn &= ~(FOREIGN_FRAME_BIT | IDENTITY_FRAME_BIT);
  143. return mfn;
  144. }
  145. static inline int phys_to_machine_mapping_valid(unsigned long pfn)
  146. {
  147. if (xen_feature(XENFEAT_auto_translated_physmap))
  148. return 1;
  149. return __pfn_to_mfn(pfn) != INVALID_P2M_ENTRY;
  150. }
  151. static inline unsigned long mfn_to_pfn_no_overrides(unsigned long mfn)
  152. {
  153. unsigned long pfn;
  154. int ret;
  155. if (unlikely(mfn >= machine_to_phys_nr))
  156. return ~0;
  157. /*
  158. * The array access can fail (e.g., device space beyond end of RAM).
  159. * In such cases it doesn't matter what we return (we return garbage),
  160. * but we must handle the fault without crashing!
  161. */
  162. ret = xen_safe_read_ulong(&machine_to_phys_mapping[mfn], &pfn);
  163. if (ret < 0)
  164. return ~0;
  165. return pfn;
  166. }
  167. static inline unsigned long mfn_to_pfn(unsigned long mfn)
  168. {
  169. unsigned long pfn;
  170. /*
  171. * Some x86 code are still using mfn_to_pfn instead of
  172. * gfn_to_pfn. This will have to be removed when we figure
  173. * out which call.
  174. */
  175. if (xen_feature(XENFEAT_auto_translated_physmap))
  176. return mfn;
  177. pfn = mfn_to_pfn_no_overrides(mfn);
  178. if (__pfn_to_mfn(pfn) != mfn)
  179. pfn = ~0;
  180. /*
  181. * pfn is ~0 if there are no entries in the m2p for mfn or the
  182. * entry doesn't map back to the mfn.
  183. */
  184. if (pfn == ~0 && __pfn_to_mfn(mfn) == IDENTITY_FRAME(mfn))
  185. pfn = mfn;
  186. return pfn;
  187. }
  188. static inline xmaddr_t phys_to_machine(xpaddr_t phys)
  189. {
  190. unsigned offset = phys.paddr & ~PAGE_MASK;
  191. return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
  192. }
  193. static inline xpaddr_t machine_to_phys(xmaddr_t machine)
  194. {
  195. unsigned offset = machine.maddr & ~PAGE_MASK;
  196. return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
  197. }
  198. /* Pseudo-physical <-> Guest conversion */
  199. static inline unsigned long pfn_to_gfn(unsigned long pfn)
  200. {
  201. if (xen_feature(XENFEAT_auto_translated_physmap))
  202. return pfn;
  203. else
  204. return pfn_to_mfn(pfn);
  205. }
  206. static inline unsigned long gfn_to_pfn(unsigned long gfn)
  207. {
  208. if (xen_feature(XENFEAT_auto_translated_physmap))
  209. return gfn;
  210. else
  211. return mfn_to_pfn(gfn);
  212. }
  213. /* Pseudo-physical <-> Bus conversion */
  214. #define pfn_to_bfn(pfn) pfn_to_gfn(pfn)
  215. #define bfn_to_pfn(bfn) gfn_to_pfn(bfn)
  216. /*
  217. * We detect special mappings in one of two ways:
  218. * 1. If the MFN is an I/O page then Xen will set the m2p entry
  219. * to be outside our maximum possible pseudophys range.
  220. * 2. If the MFN belongs to a different domain then we will certainly
  221. * not have MFN in our p2m table. Conversely, if the page is ours,
  222. * then we'll have p2m(m2p(MFN))==MFN.
  223. * If we detect a special mapping then it doesn't have a 'struct page'.
  224. * We force !pfn_valid() by returning an out-of-range pointer.
  225. *
  226. * NB. These checks require that, for any MFN that is not in our reservation,
  227. * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
  228. * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
  229. * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
  230. *
  231. * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
  232. * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
  233. * require. In all the cases we care about, the FOREIGN_FRAME bit is
  234. * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
  235. */
  236. static inline unsigned long bfn_to_local_pfn(unsigned long mfn)
  237. {
  238. unsigned long pfn;
  239. if (xen_feature(XENFEAT_auto_translated_physmap))
  240. return mfn;
  241. pfn = mfn_to_pfn(mfn);
  242. if (__pfn_to_mfn(pfn) != mfn)
  243. return -1; /* force !pfn_valid() */
  244. return pfn;
  245. }
  246. /* VIRT <-> MACHINE conversion */
  247. #define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
  248. #define virt_to_pfn(v) (PFN_DOWN(__pa(v)))
  249. #define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v)))
  250. #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
  251. /* VIRT <-> GUEST conversion */
  252. #define virt_to_gfn(v) (pfn_to_gfn(virt_to_pfn(v)))
  253. #define gfn_to_virt(g) (__va(gfn_to_pfn(g) << PAGE_SHIFT))
  254. static inline unsigned long pte_mfn(pte_t pte)
  255. {
  256. return (pte.pte & XEN_PTE_MFN_MASK) >> PAGE_SHIFT;
  257. }
  258. static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
  259. {
  260. pte_t pte;
  261. pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
  262. massage_pgprot(pgprot);
  263. return pte;
  264. }
  265. static inline pteval_t pte_val_ma(pte_t pte)
  266. {
  267. return pte.pte;
  268. }
  269. static inline pte_t __pte_ma(pteval_t x)
  270. {
  271. return (pte_t) { .pte = x };
  272. }
  273. #define pmd_val_ma(v) ((v).pmd)
  274. #ifdef __PAGETABLE_PUD_FOLDED
  275. #define pud_val_ma(v) ((v).p4d.pgd.pgd)
  276. #else
  277. #define pud_val_ma(v) ((v).pud)
  278. #endif
  279. #define __pmd_ma(x) ((pmd_t) { (x) } )
  280. #ifdef __PAGETABLE_P4D_FOLDED
  281. #define p4d_val_ma(x) ((x).pgd.pgd)
  282. #else
  283. #define p4d_val_ma(x) ((x).p4d)
  284. #endif
  285. xmaddr_t arbitrary_virt_to_machine(void *address);
  286. unsigned long arbitrary_virt_to_mfn(void *vaddr);
  287. void make_lowmem_page_readonly(void *vaddr);
  288. void make_lowmem_page_readwrite(void *vaddr);
  289. static inline bool xen_arch_need_swiotlb(struct device *dev,
  290. phys_addr_t phys,
  291. dma_addr_t dev_addr)
  292. {
  293. return false;
  294. }
  295. #endif /* _ASM_X86_XEN_PAGE_H */