]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/include/asm/xen/page.h
Linux 2.6.38-rc2
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / xen / page.h
CommitLineData
05e4d316
PA
1#ifndef _ASM_X86_XEN_PAGE_H
2#define _ASM_X86_XEN_PAGE_H
20e71f2e 3
ecbf29cd
JF
4#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/spinlock.h>
20e71f2e 7#include <linux/pfn.h>
7e77506a 8#include <linux/mm.h>
20e71f2e
IY
9
10#include <asm/uaccess.h>
ecbf29cd 11#include <asm/page.h>
20e71f2e
IY
12#include <asm/pgtable.h>
13
ecbf29cd 14#include <xen/interface/xen.h>
20e71f2e
IY
15#include <xen/features.h>
16
17/* Xen machine address */
18typedef struct xmaddr {
19 phys_addr_t maddr;
20} xmaddr_t;
21
22/* Xen pseudo-physical address */
23typedef struct xpaddr {
24 phys_addr_t paddr;
25} xpaddr_t;
26
27#define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
28#define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
29
30/**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
31#define INVALID_P2M_ENTRY (~0UL)
32#define FOREIGN_FRAME_BIT (1UL<<31)
33#define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
34
8006ec3e
JF
35/* Maximum amount of memory we can handle in a domain in pages */
36#define MAX_DOMAIN_PAGES \
37 ((unsigned long)((u64)CONFIG_XEN_MAX_DOMAIN_MEMORY * 1024 * 1024 * 1024 / PAGE_SIZE))
38
7e77506a
IC
39extern unsigned long *machine_to_phys_mapping;
40extern unsigned int machine_to_phys_order;
8006ec3e 41
d451bb7a 42extern unsigned long get_phys_to_machine(unsigned long pfn);
c3798062 43extern bool set_phys_to_machine(unsigned long pfn, unsigned long mfn);
20e71f2e 44
87f1d40a
JF
45extern int m2p_add_override(unsigned long mfn, struct page *page);
46extern int m2p_remove_override(struct page *page);
448f2831
JF
47extern struct page *m2p_find_override(unsigned long mfn);
48extern unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn);
49
20e71f2e
IY
50static inline unsigned long pfn_to_mfn(unsigned long pfn)
51{
cfd8951e
JF
52 unsigned long mfn;
53
20e71f2e
IY
54 if (xen_feature(XENFEAT_auto_translated_physmap))
55 return pfn;
56
cfd8951e
JF
57 mfn = get_phys_to_machine(pfn);
58
59 if (mfn != INVALID_P2M_ENTRY)
60 mfn &= ~FOREIGN_FRAME_BIT;
61
62 return mfn;
20e71f2e
IY
63}
64
65static inline int phys_to_machine_mapping_valid(unsigned long pfn)
66{
67 if (xen_feature(XENFEAT_auto_translated_physmap))
68 return 1;
69
d451bb7a 70 return get_phys_to_machine(pfn) != INVALID_P2M_ENTRY;
20e71f2e
IY
71}
72
73static inline unsigned long mfn_to_pfn(unsigned long mfn)
74{
75 unsigned long pfn;
76
77 if (xen_feature(XENFEAT_auto_translated_physmap))
78 return mfn;
79
20e71f2e
IY
80 pfn = 0;
81 /*
82 * The array access can fail (e.g., device space beyond end of RAM).
83 * In such cases it doesn't matter what we return (we return garbage),
84 * but we must handle the fault without crashing!
85 */
86 __get_user(pfn, &machine_to_phys_mapping[mfn]);
87
448f2831
JF
88 /*
89 * If this appears to be a foreign mfn (because the pfn
90 * doesn't map back to the mfn), then check the local override
91 * table to see if there's a better pfn to use.
92 */
93 if (get_phys_to_machine(pfn) != mfn)
94 pfn = m2p_find_override_pfn(mfn, pfn);
95
20e71f2e
IY
96 return pfn;
97}
98
99static inline xmaddr_t phys_to_machine(xpaddr_t phys)
100{
101 unsigned offset = phys.paddr & ~PAGE_MASK;
947d0496 102 return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
20e71f2e
IY
103}
104
105static inline xpaddr_t machine_to_phys(xmaddr_t machine)
106{
107 unsigned offset = machine.maddr & ~PAGE_MASK;
947d0496 108 return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
20e71f2e
IY
109}
110
111/*
112 * We detect special mappings in one of two ways:
113 * 1. If the MFN is an I/O page then Xen will set the m2p entry
114 * to be outside our maximum possible pseudophys range.
115 * 2. If the MFN belongs to a different domain then we will certainly
116 * not have MFN in our p2m table. Conversely, if the page is ours,
117 * then we'll have p2m(m2p(MFN))==MFN.
118 * If we detect a special mapping then it doesn't have a 'struct page'.
119 * We force !pfn_valid() by returning an out-of-range pointer.
120 *
121 * NB. These checks require that, for any MFN that is not in our reservation,
122 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
123 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
124 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
125 *
126 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
127 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
128 * require. In all the cases we care about, the FOREIGN_FRAME bit is
129 * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
130 */
131static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
132{
20e71f2e 133 unsigned long pfn = mfn_to_pfn(mfn);
c0011dbf
JF
134 if (get_phys_to_machine(pfn) != mfn)
135 return -1; /* force !pfn_valid() */
20e71f2e
IY
136 return pfn;
137}
138
20e71f2e
IY
139/* VIRT <-> MACHINE conversion */
140#define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
b40bf53e
AN
141#define virt_to_pfn(v) (PFN_DOWN(__pa(v)))
142#define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v)))
20e71f2e
IY
143#define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
144
145static inline unsigned long pte_mfn(pte_t pte)
146{
59438c9f 147 return (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
20e71f2e
IY
148}
149
150static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
151{
152 pte_t pte;
153
154 pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
b534816b 155 massage_pgprot(pgprot);
20e71f2e
IY
156
157 return pte;
158}
159
160static inline pteval_t pte_val_ma(pte_t pte)
161{
162 return pte.pte;
163}
164
165static inline pte_t __pte_ma(pteval_t x)
166{
167 return (pte_t) { .pte = x };
168}
169
20e71f2e 170#define pmd_val_ma(v) ((v).pmd)
f6e58732 171#ifdef __PAGETABLE_PUD_FOLDED
20e71f2e 172#define pud_val_ma(v) ((v).pgd.pgd)
f6e58732
JF
173#else
174#define pud_val_ma(v) ((v).pud)
175#endif
20e71f2e 176#define __pmd_ma(x) ((pmd_t) { (x) } )
20e71f2e
IY
177
178#define pgd_val_ma(x) ((x).pgd)
179
eba3ff8b 180void xen_set_domain_pte(pte_t *ptep, pte_t pteval, unsigned domid);
20e71f2e 181
ce803e70 182xmaddr_t arbitrary_virt_to_machine(void *address);
9976b39b 183unsigned long arbitrary_virt_to_mfn(void *vaddr);
20e71f2e
IY
184void make_lowmem_page_readonly(void *vaddr);
185void make_lowmem_page_readwrite(void *vaddr);
186
05e4d316 187#endif /* _ASM_X86_XEN_PAGE_H */