page.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* Maximum amount of memory we can handle in a domain in pages */
  31. #define MAX_DOMAIN_PAGES \
  32. ((unsigned long)((u64)CONFIG_XEN_MAX_DOMAIN_MEMORY * 1024 * 1024 * 1024 / PAGE_SIZE))
  33. extern unsigned long *machine_to_phys_mapping;
  34. extern unsigned long machine_to_phys_nr;
  35. extern unsigned long get_phys_to_machine(unsigned long pfn);
  36. extern bool set_phys_to_machine(unsigned long pfn, unsigned long mfn);
  37. extern bool __init early_set_phys_to_machine(unsigned long pfn, unsigned long mfn);
  38. extern bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn);
  39. extern unsigned long set_phys_range_identity(unsigned long pfn_s,
  40. unsigned long pfn_e);
  41. extern int m2p_add_override(unsigned long mfn, struct page *page,
  42. struct gnttab_map_grant_ref *kmap_op);
  43. extern int m2p_remove_override(struct page *page,
  44. struct gnttab_map_grant_ref *kmap_op,
  45. unsigned long mfn);
  46. extern struct page *m2p_find_override(unsigned long mfn);
  47. extern unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn);
  48. static inline unsigned long pfn_to_mfn(unsigned long pfn)
  49. {
  50. unsigned long mfn;
  51. if (xen_feature(XENFEAT_auto_translated_physmap))
  52. return pfn;
  53. mfn = get_phys_to_machine(pfn);
  54. if (mfn != INVALID_P2M_ENTRY)
  55. mfn &= ~(FOREIGN_FRAME_BIT | IDENTITY_FRAME_BIT);
  56. return mfn;
  57. }
  58. static inline int phys_to_machine_mapping_valid(unsigned long pfn)
  59. {
  60. if (xen_feature(XENFEAT_auto_translated_physmap))
  61. return 1;
  62. return get_phys_to_machine(pfn) != INVALID_P2M_ENTRY;
  63. }
  64. static inline unsigned long mfn_to_pfn_no_overrides(unsigned long mfn)
  65. {
  66. unsigned long pfn;
  67. int ret;
  68. if (xen_feature(XENFEAT_auto_translated_physmap))
  69. return mfn;
  70. if (unlikely(mfn >= machine_to_phys_nr))
  71. return ~0;
  72. /*
  73. * The array access can fail (e.g., device space beyond end of RAM).
  74. * In such cases it doesn't matter what we return (we return garbage),
  75. * but we must handle the fault without crashing!
  76. */
  77. ret = __get_user(pfn, &machine_to_phys_mapping[mfn]);
  78. if (ret < 0)
  79. return ~0;
  80. return pfn;
  81. }
  82. static inline unsigned long mfn_to_pfn(unsigned long mfn)
  83. {
  84. unsigned long pfn;
  85. if (xen_feature(XENFEAT_auto_translated_physmap))
  86. return mfn;
  87. pfn = mfn_to_pfn_no_overrides(mfn);
  88. if (get_phys_to_machine(pfn) != mfn) {
  89. /*
  90. * If this appears to be a foreign mfn (because the pfn
  91. * doesn't map back to the mfn), then check the local override
  92. * table to see if there's a better pfn to use.
  93. *
  94. * m2p_find_override_pfn returns ~0 if it doesn't find anything.
  95. */
  96. pfn = m2p_find_override_pfn(mfn, ~0);
  97. }
  98. /*
  99. * pfn is ~0 if there are no entries in the m2p for mfn or if the
  100. * entry doesn't map back to the mfn and m2p_override doesn't have a
  101. * valid entry for it.
  102. */
  103. if (pfn == ~0 &&
  104. get_phys_to_machine(mfn) == IDENTITY_FRAME(mfn))
  105. pfn = mfn;
  106. return pfn;
  107. }
  108. static inline xmaddr_t phys_to_machine(xpaddr_t phys)
  109. {
  110. unsigned offset = phys.paddr & ~PAGE_MASK;
  111. return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
  112. }
  113. static inline xpaddr_t machine_to_phys(xmaddr_t machine)
  114. {
  115. unsigned offset = machine.maddr & ~PAGE_MASK;
  116. return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
  117. }
  118. /*
  119. * We detect special mappings in one of two ways:
  120. * 1. If the MFN is an I/O page then Xen will set the m2p entry
  121. * to be outside our maximum possible pseudophys range.
  122. * 2. If the MFN belongs to a different domain then we will certainly
  123. * not have MFN in our p2m table. Conversely, if the page is ours,
  124. * then we'll have p2m(m2p(MFN))==MFN.
  125. * If we detect a special mapping then it doesn't have a 'struct page'.
  126. * We force !pfn_valid() by returning an out-of-range pointer.
  127. *
  128. * NB. These checks require that, for any MFN that is not in our reservation,
  129. * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
  130. * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
  131. * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
  132. *
  133. * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
  134. * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
  135. * require. In all the cases we care about, the FOREIGN_FRAME bit is
  136. * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
  137. */
  138. static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
  139. {
  140. unsigned long pfn;
  141. if (xen_feature(XENFEAT_auto_translated_physmap))
  142. return mfn;
  143. pfn = mfn_to_pfn(mfn);
  144. if (get_phys_to_machine(pfn) != mfn)
  145. return -1; /* force !pfn_valid() */
  146. return pfn;
  147. }
  148. /* VIRT <-> MACHINE conversion */
  149. #define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
  150. #define virt_to_pfn(v) (PFN_DOWN(__pa(v)))
  151. #define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v)))
  152. #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
  153. static inline unsigned long pte_mfn(pte_t pte)
  154. {
  155. return (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
  156. }
  157. static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
  158. {
  159. pte_t pte;
  160. pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
  161. massage_pgprot(pgprot);
  162. return pte;
  163. }
  164. static inline pteval_t pte_val_ma(pte_t pte)
  165. {
  166. return pte.pte;
  167. }
  168. static inline pte_t __pte_ma(pteval_t x)
  169. {
  170. return (pte_t) { .pte = x };
  171. }
  172. #define pmd_val_ma(v) ((v).pmd)
  173. #ifdef __PAGETABLE_PUD_FOLDED
  174. #define pud_val_ma(v) ((v).pgd.pgd)
  175. #else
  176. #define pud_val_ma(v) ((v).pud)
  177. #endif
  178. #define __pmd_ma(x) ((pmd_t) { (x) } )
  179. #define pgd_val_ma(x) ((x).pgd)
  180. void xen_set_domain_pte(pte_t *ptep, pte_t pteval, unsigned domid);
  181. xmaddr_t arbitrary_virt_to_machine(void *address);
  182. unsigned long arbitrary_virt_to_mfn(void *vaddr);
  183. void make_lowmem_page_readonly(void *vaddr);
  184. void make_lowmem_page_readwrite(void *vaddr);
  185. #define xen_remap(cookie, size) ioremap((cookie), (size));
  186. #define xen_unmap(cookie) iounmap((cookie))
  187. #endif /* _ASM_X86_XEN_PAGE_H */