6 #include <asm/uaccess.h>
7 #include <asm/pgtable.h>
9 #include <xen/features.h>
11 /* Xen machine address */
12 typedef struct xmaddr {
16 /* Xen pseudo-physical address */
17 typedef struct xpaddr {
21 #define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
22 #define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
24 /**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
25 #define INVALID_P2M_ENTRY (~0UL)
26 #define FOREIGN_FRAME_BIT (1UL<<31)
27 #define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
29 extern unsigned long *phys_to_machine_mapping;
31 static inline unsigned long pfn_to_mfn(unsigned long pfn)
33 if (xen_feature(XENFEAT_auto_translated_physmap))
36 return phys_to_machine_mapping[(unsigned int)(pfn)] &
40 static inline int phys_to_machine_mapping_valid(unsigned long pfn)
42 if (xen_feature(XENFEAT_auto_translated_physmap))
45 return (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY);
48 static inline unsigned long mfn_to_pfn(unsigned long mfn)
52 if (xen_feature(XENFEAT_auto_translated_physmap))
56 if (unlikely((mfn >> machine_to_phys_order) != 0))
62 * The array access can fail (e.g., device space beyond end of RAM).
63 * In such cases it doesn't matter what we return (we return garbage),
64 * but we must handle the fault without crashing!
66 __get_user(pfn, &machine_to_phys_mapping[mfn]);
71 static inline xmaddr_t phys_to_machine(xpaddr_t phys)
73 unsigned offset = phys.paddr & ~PAGE_MASK;
74 return XMADDR(PFN_PHYS((u64)pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
77 static inline xpaddr_t machine_to_phys(xmaddr_t machine)
79 unsigned offset = machine.maddr & ~PAGE_MASK;
80 return XPADDR(PFN_PHYS((u64)mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
84 * We detect special mappings in one of two ways:
85 * 1. If the MFN is an I/O page then Xen will set the m2p entry
86 * to be outside our maximum possible pseudophys range.
87 * 2. If the MFN belongs to a different domain then we will certainly
88 * not have MFN in our p2m table. Conversely, if the page is ours,
89 * then we'll have p2m(m2p(MFN))==MFN.
90 * If we detect a special mapping then it doesn't have a 'struct page'.
91 * We force !pfn_valid() by returning an out-of-range pointer.
93 * NB. These checks require that, for any MFN that is not in our reservation,
94 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
95 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
96 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
98 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
99 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
100 * require. In all the cases we care about, the FOREIGN_FRAME bit is
101 * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
103 static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
105 extern unsigned long max_mapnr;
106 unsigned long pfn = mfn_to_pfn(mfn);
107 if ((pfn < max_mapnr)
108 && !xen_feature(XENFEAT_auto_translated_physmap)
109 && (phys_to_machine_mapping[pfn] != mfn))
110 return max_mapnr; /* force !pfn_valid() */
114 static inline void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
116 if (xen_feature(XENFEAT_auto_translated_physmap)) {
117 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
120 phys_to_machine_mapping[pfn] = mfn;
123 /* VIRT <-> MACHINE conversion */
124 #define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
125 #define virt_to_mfn(v) (pfn_to_mfn(PFN_DOWN(__pa(v))))
126 #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
128 static inline unsigned long pte_mfn(pte_t pte)
130 return (pte.pte & PTE_MASK) >> PAGE_SHIFT;
133 static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
137 pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
138 (pgprot_val(pgprot) & __supported_pte_mask);
143 static inline pteval_t pte_val_ma(pte_t pte)
148 static inline pte_t __pte_ma(pteval_t x)
150 return (pte_t) { .pte = x };
153 #ifdef CONFIG_X86_PAE
154 #define pmd_val_ma(v) ((v).pmd)
155 #define pud_val_ma(v) ((v).pgd.pgd)
156 #define __pmd_ma(x) ((pmd_t) { (x) } )
158 #define pmd_val_ma(v) ((v).pud.pgd.pgd)
159 #endif /* CONFIG_X86_PAE */
161 #define pgd_val_ma(x) ((x).pgd)
164 xmaddr_t arbitrary_virt_to_machine(unsigned long address);
165 void make_lowmem_page_readonly(void *vaddr);
166 void make_lowmem_page_readwrite(void *vaddr);
168 #endif /* __XEN_PAGE_H */