5 #include <xen/features.h>
6 #include <xen/interface/xen.h>
8 /**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
9 #define INVALID_P2M_ENTRY (~0UL)
10 #define FOREIGN_FRAME_BIT (1UL<<31)
11 #define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
13 /* Definitions for machine and pseudophysical addresses. */
15 typedef unsigned long long paddr_t;
16 typedef unsigned long long maddr_t;
18 typedef unsigned long paddr_t;
19 typedef unsigned long maddr_t;
24 extern unsigned long *phys_to_machine_mapping;
25 extern unsigned long max_mapnr;
27 #undef machine_to_phys_mapping
28 extern unsigned long *machine_to_phys_mapping;
29 extern unsigned int machine_to_phys_order;
31 static inline unsigned long pfn_to_mfn(unsigned long pfn)
33 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
35 if (likely(max_mapnr))
36 BUG_ON(pfn >= max_mapnr);
37 return phys_to_machine_mapping[pfn] & ~FOREIGN_FRAME_BIT;
40 static inline int phys_to_machine_mapping_valid(unsigned long pfn)
42 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
44 if (likely(max_mapnr))
45 BUG_ON(pfn >= max_mapnr);
46 return (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY);
49 static inline unsigned long mfn_to_pfn(unsigned long mfn)
53 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
56 if (unlikely((mfn >> machine_to_phys_order) != 0))
59 /* The array access can fail (e.g., device space beyond end of RAM). */
63 ".section .fixup,\"ax\"\n"
67 ".section __ex_table,\"a\"\n"
72 : "m" (machine_to_phys_mapping[mfn]), "m" (max_mapnr) );
78 * We detect special mappings in one of two ways:
79 * 1. If the MFN is an I/O page then Xen will set the m2p entry
80 * to be outside our maximum possible pseudophys range.
81 * 2. If the MFN belongs to a different domain then we will certainly
82 * not have MFN in our p2m table. Conversely, if the page is ours,
83 * then we'll have p2m(m2p(MFN))==MFN.
84 * If we detect a special mapping then it doesn't have a 'struct page'.
85 * We force !pfn_valid() by returning an out-of-range pointer.
87 * NB. These checks require that, for any MFN that is not in our reservation,
88 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
89 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
90 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
92 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
93 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
94 * require. In all the cases we care about, the FOREIGN_FRAME bit is
95 * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
97 static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
99 unsigned long pfn = mfn_to_pfn(mfn);
100 if (likely(pfn < max_mapnr)
101 && likely(!xen_feature(XENFEAT_auto_translated_physmap))
102 && unlikely(phys_to_machine_mapping[pfn] != mfn))
103 return max_mapnr; /* force !pfn_valid() */
107 static inline void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
109 if (likely(max_mapnr))
110 BUG_ON(pfn >= max_mapnr);
111 if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
112 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
115 phys_to_machine_mapping[pfn] = mfn;
118 static inline maddr_t phys_to_machine(paddr_t phys)
120 maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
121 machine = (machine << PAGE_SHIFT) | (phys & ~PAGE_MASK);
125 static inline paddr_t machine_to_phys(maddr_t machine)
127 paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
128 phys = (phys << PAGE_SHIFT) | (machine & ~PAGE_MASK);
132 #ifdef CONFIG_X86_PAE
133 static inline paddr_t pte_phys_to_machine(paddr_t phys)
136 * In PAE mode, the NX bit needs to be dealt with in the value
137 * passed to pfn_to_mfn(). On x86_64, we need to mask it off,
138 * but for i386 the conversion to ulong for the argument will
141 maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
142 machine = (machine << PAGE_SHIFT) | (phys & ~PHYSICAL_PAGE_MASK);
146 static inline paddr_t pte_machine_to_phys(maddr_t machine)
149 * In PAE mode, the NX bit needs to be dealt with in the value
150 * passed to mfn_to_pfn(). On x86_64, we need to mask it off,
151 * but for i386 the conversion to ulong for the argument will
154 paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
155 phys = (phys << PAGE_SHIFT) | (machine & ~PHYSICAL_PAGE_MASK);
159 #define pte_phys_to_machine phys_to_machine
160 #define pte_machine_to_phys machine_to_phys
163 #else /* !CONFIG_XEN */
165 #define pfn_to_mfn(pfn) (pfn)
166 #define mfn_to_pfn(mfn) (mfn)
167 #define mfn_to_local_pfn(mfn) (mfn)
168 #define set_phys_to_machine(pfn, mfn) ((void)0)
169 #define phys_to_machine_mapping_valid(pfn) (1)
170 #define phys_to_machine(phys) ((maddr_t)(phys))
171 #define machine_to_phys(mach) ((paddr_t)(mach))
172 #define pfn_pte_ma(pfn, prot) pfn_pte(pfn, prot)
173 #define __pte_ma(x) __pte(x)
175 #endif /* !CONFIG_XEN */
177 /* VIRT <-> MACHINE conversion */
178 #define virt_to_machine(v) (phys_to_machine(__pa(v)))
179 #define virt_to_mfn(v) (pfn_to_mfn(__pa(v) >> PAGE_SHIFT))
180 #define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
182 #endif /* _I386_MADDR_H */