source: trunk/packages/xen-3.1/xen-3.1/linux-2.6-xen-sparse/include/asm-x86_64/mach-xen/asm/maddr.h @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 4.8 KB
Line 
1#ifndef _X86_64_MADDR_H
2#define _X86_64_MADDR_H
3
4#include <xen/features.h>
5#include <xen/interface/xen.h>
6
7/**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
8#define INVALID_P2M_ENTRY       (~0UL)
9#define FOREIGN_FRAME_BIT       (1UL<<63)
10#define FOREIGN_FRAME(m)        ((m) | FOREIGN_FRAME_BIT)
11
12/* Definitions for machine and pseudophysical addresses. */
13typedef unsigned long paddr_t;
14typedef unsigned long maddr_t;
15
16#ifdef CONFIG_XEN
17
18extern unsigned long *phys_to_machine_mapping;
19
20#undef machine_to_phys_mapping
21extern unsigned long *machine_to_phys_mapping;
22extern unsigned int   machine_to_phys_order;
23
24static inline unsigned long pfn_to_mfn(unsigned long pfn)
25{
26        if (xen_feature(XENFEAT_auto_translated_physmap))
27                return pfn;
28        BUG_ON(end_pfn && pfn >= end_pfn);
29        return phys_to_machine_mapping[pfn] & ~FOREIGN_FRAME_BIT;
30}
31
32static inline int phys_to_machine_mapping_valid(unsigned long pfn)
33{
34        if (xen_feature(XENFEAT_auto_translated_physmap))
35                return 1;
36        BUG_ON(end_pfn && pfn >= end_pfn);
37        return (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY);
38}
39
40static inline unsigned long mfn_to_pfn(unsigned long mfn)
41{
42        unsigned long pfn;
43
44        if (xen_feature(XENFEAT_auto_translated_physmap))
45                return mfn;
46
47        if (unlikely((mfn >> machine_to_phys_order) != 0))
48                return end_pfn;
49
50        /* The array access can fail (e.g., device space beyond end of RAM). */
51        asm (
52                "1:     movq %1,%0\n"
53                "2:\n"
54                ".section .fixup,\"ax\"\n"
55                "3:     movq %2,%0\n"
56                "       jmp  2b\n"
57                ".previous\n"
58                ".section __ex_table,\"a\"\n"
59                "       .align 8\n"
60                "       .quad 1b,3b\n"
61                ".previous"
62                : "=r" (pfn)
63                : "m" (machine_to_phys_mapping[mfn]), "m" (end_pfn) );
64
65        return pfn;
66}
67
68/*
69 * We detect special mappings in one of two ways:
70 *  1. If the MFN is an I/O page then Xen will set the m2p entry
71 *     to be outside our maximum possible pseudophys range.
72 *  2. If the MFN belongs to a different domain then we will certainly
73 *     not have MFN in our p2m table. Conversely, if the page is ours,
74 *     then we'll have p2m(m2p(MFN))==MFN.
75 * If we detect a special mapping then it doesn't have a 'struct page'.
76 * We force !pfn_valid() by returning an out-of-range pointer.
77 *
78 * NB. These checks require that, for any MFN that is not in our reservation,
79 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
80 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
81 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
82 *
83 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
84 *      use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
85 *      require. In all the cases we care about, the FOREIGN_FRAME bit is
86 *      masked (e.g., pfn_to_mfn()) so behaviour there is correct.
87 */
88static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
89{
90        unsigned long pfn = mfn_to_pfn(mfn);
91        if ((pfn < end_pfn)
92            && !xen_feature(XENFEAT_auto_translated_physmap)
93            && (phys_to_machine_mapping[pfn] != mfn))
94                return end_pfn; /* force !pfn_valid() */
95        return pfn;
96}
97
98static inline void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
99{
100        BUG_ON(end_pfn && pfn >= end_pfn);
101        if (xen_feature(XENFEAT_auto_translated_physmap)) {
102                BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
103                return;
104        }
105        phys_to_machine_mapping[pfn] = mfn;
106}
107
108static inline maddr_t phys_to_machine(paddr_t phys)
109{
110        maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
111        machine = (machine << PAGE_SHIFT) | (phys & ~PAGE_MASK);
112        return machine;
113}
114
115static inline paddr_t machine_to_phys(maddr_t machine)
116{
117        paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
118        phys = (phys << PAGE_SHIFT) | (machine & ~PAGE_MASK);
119        return phys;
120}
121
122static inline paddr_t pte_phys_to_machine(paddr_t phys)
123{
124        maddr_t machine;
125        machine = pfn_to_mfn((phys & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT);
126        machine = (machine << PAGE_SHIFT) | (phys & ~PHYSICAL_PAGE_MASK);
127        return machine;
128}
129
130static inline paddr_t pte_machine_to_phys(maddr_t machine)
131{
132        paddr_t phys;
133        phys = mfn_to_pfn((machine & PHYSICAL_PAGE_MASK) >> PAGE_SHIFT);
134        phys = (phys << PAGE_SHIFT) | (machine & ~PHYSICAL_PAGE_MASK);
135        return phys;
136}
137
138#define __pte_ma(x)     ((pte_t) { (x) } )
139#define pfn_pte_ma(pfn, prot)   __pte_ma((((pfn) << PAGE_SHIFT) | pgprot_val(prot)) & __supported_pte_mask)
140
141#else /* !CONFIG_XEN */
142
143#define pfn_to_mfn(pfn) (pfn)
144#define mfn_to_pfn(mfn) (mfn)
145#define mfn_to_local_pfn(mfn) (mfn)
146#define set_phys_to_machine(pfn, mfn) ((void)0)
147#define phys_to_machine_mapping_valid(pfn) (1)
148#define phys_to_machine(phys) ((maddr_t)(phys))
149#define machine_to_phys(mach) ((paddr_t)(mach))
150#define pfn_pte_ma(pfn, prot) pfn_pte(pfn, prot)
151#define __pte_ma(x) __pte(x)
152
153#endif /* !CONFIG_XEN */
154
155/* VIRT <-> MACHINE conversion */
156#define virt_to_machine(v)      (phys_to_machine(__pa(v)))
157#define virt_to_mfn(v)          (pfn_to_mfn(__pa(v) >> PAGE_SHIFT))
158#define mfn_to_virt(m)          (__va(mfn_to_pfn(m) << PAGE_SHIFT))
159
160#endif /* _X86_64_MADDR_H */
161
Note: See TracBrowser for help on using the repository browser.