page.h 4.6 KB

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