page.h 4.8 KB

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