page.h 5.3 KB

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