page.h 4.5 KB

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