page.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef __XEN_PAGE_H
  2. #define __XEN_PAGE_H
  3. #include <linux/pfn.h>
  4. #include <asm/uaccess.h>
  5. #include <xen/features.h>
  6. #ifdef CONFIG_X86_PAE
  7. /* Xen machine address */
  8. typedef struct xmaddr {
  9. unsigned long long maddr;
  10. } xmaddr_t;
  11. /* Xen pseudo-physical address */
  12. typedef struct xpaddr {
  13. unsigned long long paddr;
  14. } xpaddr_t;
  15. #else
  16. /* Xen machine address */
  17. typedef struct xmaddr {
  18. unsigned long maddr;
  19. } xmaddr_t;
  20. /* Xen pseudo-physical address */
  21. typedef struct xpaddr {
  22. unsigned long paddr;
  23. } xpaddr_t;
  24. #endif
  25. #define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
  26. #define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
  27. /**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
  28. #define INVALID_P2M_ENTRY (~0UL)
  29. #define FOREIGN_FRAME_BIT (1UL<<31)
  30. #define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
  31. extern unsigned long *phys_to_machine_mapping;
  32. static inline unsigned long pfn_to_mfn(unsigned long pfn)
  33. {
  34. if (xen_feature(XENFEAT_auto_translated_physmap))
  35. return pfn;
  36. return phys_to_machine_mapping[(unsigned int)(pfn)] &
  37. ~FOREIGN_FRAME_BIT;
  38. }
  39. static inline int phys_to_machine_mapping_valid(unsigned long pfn)
  40. {
  41. if (xen_feature(XENFEAT_auto_translated_physmap))
  42. return 1;
  43. return (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY);
  44. }
  45. static inline unsigned long mfn_to_pfn(unsigned long mfn)
  46. {
  47. unsigned long pfn;
  48. if (xen_feature(XENFEAT_auto_translated_physmap))
  49. return mfn;
  50. #if 0
  51. if (unlikely((mfn >> machine_to_phys_order) != 0))
  52. return max_mapnr;
  53. #endif
  54. pfn = 0;
  55. /*
  56. * The array access can fail (e.g., device space beyond end of RAM).
  57. * In such cases it doesn't matter what we return (we return garbage),
  58. * but we must handle the fault without crashing!
  59. */
  60. __get_user(pfn, &machine_to_phys_mapping[mfn]);
  61. return pfn;
  62. }
  63. static inline xmaddr_t phys_to_machine(xpaddr_t phys)
  64. {
  65. unsigned offset = phys.paddr & ~PAGE_MASK;
  66. return XMADDR(PFN_PHYS((u64)pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
  67. }
  68. static inline xpaddr_t machine_to_phys(xmaddr_t machine)
  69. {
  70. unsigned offset = machine.maddr & ~PAGE_MASK;
  71. return XPADDR(PFN_PHYS((u64)mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
  72. }
  73. /*
  74. * We detect special mappings in one of two ways:
  75. * 1. If the MFN is an I/O page then Xen will set the m2p entry
  76. * to be outside our maximum possible pseudophys range.
  77. * 2. If the MFN belongs to a different domain then we will certainly
  78. * not have MFN in our p2m table. Conversely, if the page is ours,
  79. * then we'll have p2m(m2p(MFN))==MFN.
  80. * If we detect a special mapping then it doesn't have a 'struct page'.
  81. * We force !pfn_valid() by returning an out-of-range pointer.
  82. *
  83. * NB. These checks require that, for any MFN that is not in our reservation,
  84. * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
  85. * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
  86. * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
  87. *
  88. * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
  89. * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
  90. * require. In all the cases we care about, the FOREIGN_FRAME bit is
  91. * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
  92. */
  93. static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
  94. {
  95. extern unsigned long max_mapnr;
  96. unsigned long pfn = mfn_to_pfn(mfn);
  97. if ((pfn < max_mapnr)
  98. && !xen_feature(XENFEAT_auto_translated_physmap)
  99. && (phys_to_machine_mapping[pfn] != mfn))
  100. return max_mapnr; /* force !pfn_valid() */
  101. return pfn;
  102. }
  103. static inline void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  104. {
  105. if (xen_feature(XENFEAT_auto_translated_physmap)) {
  106. BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
  107. return;
  108. }
  109. phys_to_machine_mapping[pfn] = mfn;
  110. }
  111. /* VIRT <-> MACHINE conversion */
  112. #define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
  113. #define virt_to_mfn(v) (pfn_to_mfn(PFN_DOWN(__pa(v))))
  114. #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
  115. #ifdef CONFIG_X86_PAE
  116. #define pte_mfn(_pte) (((_pte).pte_low >> PAGE_SHIFT) | \
  117. (((_pte).pte_high & 0xfff) << (32-PAGE_SHIFT)))
  118. static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
  119. {
  120. pte_t pte;
  121. pte.pte_high = (page_nr >> (32 - PAGE_SHIFT)) |
  122. (pgprot_val(pgprot) >> 32);
  123. pte.pte_high &= (__supported_pte_mask >> 32);
  124. pte.pte_low = ((page_nr << PAGE_SHIFT) | pgprot_val(pgprot));
  125. pte.pte_low &= __supported_pte_mask;
  126. return pte;
  127. }
  128. static inline unsigned long long pte_val_ma(pte_t x)
  129. {
  130. return ((unsigned long long)x.pte_high << 32) | x.pte_low;
  131. }
  132. #define pmd_val_ma(v) ((v).pmd)
  133. #define pud_val_ma(v) ((v).pgd.pgd)
  134. #define __pte_ma(x) ((pte_t) { .pte_low = (x), .pte_high = (x)>>32 } )
  135. #define __pmd_ma(x) ((pmd_t) { (x) } )
  136. #else /* !X86_PAE */
  137. #define pte_mfn(_pte) ((_pte).pte_low >> PAGE_SHIFT)
  138. #define mfn_pte(pfn, prot) __pte_ma(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
  139. #define pte_val_ma(x) ((x).pte_low)
  140. #define pmd_val_ma(v) ((v).pud.pgd.pgd)
  141. #define __pte_ma(x) ((pte_t) { (x) } )
  142. #endif /* CONFIG_X86_PAE */
  143. #define pgd_val_ma(x) ((x).pgd)
  144. xmaddr_t arbitrary_virt_to_machine(unsigned long address);
  145. void make_lowmem_page_readonly(void *vaddr);
  146. void make_lowmem_page_readwrite(void *vaddr);
  147. #endif /* __XEN_PAGE_H */