page.h 5.2 KB

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