page.h 8.6 KB

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