blob: 875f5a08a6c788e1471ad188dd2cf9e34a2f76af [file] [log] [blame]
H. Peter Anvin05e4d312008-10-23 00:01:39 -07001#ifndef _ASM_X86_XEN_PAGE_H
2#define _ASM_X86_XEN_PAGE_H
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -07003
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -08004#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/spinlock.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -07007#include <linux/pfn.h>
8
9#include <asm/uaccess.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080010#include <asm/page.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070011#include <asm/pgtable.h>
12
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080013#include <xen/interface/xen.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070014#include <xen/features.h>
15
16/* Xen machine address */
17typedef struct xmaddr {
18 phys_addr_t maddr;
19} xmaddr_t;
20
21/* Xen pseudo-physical address */
22typedef struct xpaddr {
23 phys_addr_t paddr;
24} xpaddr_t;
25
26#define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
27#define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
28
29/**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
30#define INVALID_P2M_ENTRY (~0UL)
31#define FOREIGN_FRAME_BIT (1UL<<31)
32#define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
33
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010034/* Maximum amount of memory we can handle in a domain in pages */
35#define MAX_DOMAIN_PAGES \
36 ((unsigned long)((u64)CONFIG_XEN_MAX_DOMAIN_MEMORY * 1024 * 1024 * 1024 / PAGE_SIZE))
37
38
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010039extern unsigned long get_phys_to_machine(unsigned long pfn);
Jeremy Fitzhardingec3798062010-08-27 13:42:04 -070040extern bool set_phys_to_machine(unsigned long pfn, unsigned long mfn);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070041
42static inline unsigned long pfn_to_mfn(unsigned long pfn)
43{
Jeremy Fitzhardingecfd89512010-08-31 14:06:22 -070044 unsigned long mfn;
45
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070046 if (xen_feature(XENFEAT_auto_translated_physmap))
47 return pfn;
48
Jeremy Fitzhardingecfd89512010-08-31 14:06:22 -070049 mfn = get_phys_to_machine(pfn);
50
51 if (mfn != INVALID_P2M_ENTRY)
52 mfn &= ~FOREIGN_FRAME_BIT;
53
54 return mfn;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070055}
56
57static inline int phys_to_machine_mapping_valid(unsigned long pfn)
58{
59 if (xen_feature(XENFEAT_auto_translated_physmap))
60 return 1;
61
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010062 return get_phys_to_machine(pfn) != INVALID_P2M_ENTRY;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070063}
64
65static inline unsigned long mfn_to_pfn(unsigned long mfn)
66{
67 unsigned long pfn;
68
69 if (xen_feature(XENFEAT_auto_translated_physmap))
70 return mfn;
71
72#if 0
73 if (unlikely((mfn >> machine_to_phys_order) != 0))
74 return max_mapnr;
75#endif
76
77 pfn = 0;
78 /*
79 * The array access can fail (e.g., device space beyond end of RAM).
80 * In such cases it doesn't matter what we return (we return garbage),
81 * but we must handle the fault without crashing!
82 */
83 __get_user(pfn, &machine_to_phys_mapping[mfn]);
84
85 return pfn;
86}
87
88static inline xmaddr_t phys_to_machine(xpaddr_t phys)
89{
90 unsigned offset = phys.paddr & ~PAGE_MASK;
Jeremy Fitzhardinge947d0492008-09-11 01:31:48 -070091 return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070092}
93
94static inline xpaddr_t machine_to_phys(xmaddr_t machine)
95{
96 unsigned offset = machine.maddr & ~PAGE_MASK;
Jeremy Fitzhardinge947d0492008-09-11 01:31:48 -070097 return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070098}
99
100/*
101 * We detect special mappings in one of two ways:
102 * 1. If the MFN is an I/O page then Xen will set the m2p entry
103 * to be outside our maximum possible pseudophys range.
104 * 2. If the MFN belongs to a different domain then we will certainly
105 * not have MFN in our p2m table. Conversely, if the page is ours,
106 * then we'll have p2m(m2p(MFN))==MFN.
107 * If we detect a special mapping then it doesn't have a 'struct page'.
108 * We force !pfn_valid() by returning an out-of-range pointer.
109 *
110 * NB. These checks require that, for any MFN that is not in our reservation,
111 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
112 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
113 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
114 *
115 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
116 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
117 * require. In all the cases we care about, the FOREIGN_FRAME bit is
118 * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
119 */
120static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
121{
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700122 unsigned long pfn = mfn_to_pfn(mfn);
Jeremy Fitzhardingec0011db2010-02-04 14:46:34 -0800123 if (get_phys_to_machine(pfn) != mfn)
124 return -1; /* force !pfn_valid() */
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700125 return pfn;
126}
127
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700128/* VIRT <-> MACHINE conversion */
129#define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
Alex Nixonb40bf532009-02-09 12:05:46 -0800130#define virt_to_pfn(v) (PFN_DOWN(__pa(v)))
131#define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v)))
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700132#define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
133
134static inline unsigned long pte_mfn(pte_t pte)
135{
Jeremy Fitzhardinge59438c92008-07-21 22:59:42 -0700136 return (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700137}
138
139static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
140{
141 pte_t pte;
142
143 pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800144 massage_pgprot(pgprot);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700145
146 return pte;
147}
148
149static inline pteval_t pte_val_ma(pte_t pte)
150{
151 return pte.pte;
152}
153
154static inline pte_t __pte_ma(pteval_t x)
155{
156 return (pte_t) { .pte = x };
157}
158
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700159#define pmd_val_ma(v) ((v).pmd)
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700160#ifdef __PAGETABLE_PUD_FOLDED
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700161#define pud_val_ma(v) ((v).pgd.pgd)
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700162#else
163#define pud_val_ma(v) ((v).pud)
164#endif
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700165#define __pmd_ma(x) ((pmd_t) { (x) } )
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700166
167#define pgd_val_ma(x) ((x).pgd)
168
169
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700170xmaddr_t arbitrary_virt_to_machine(void *address);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800171unsigned long arbitrary_virt_to_mfn(void *vaddr);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700172void make_lowmem_page_readonly(void *vaddr);
173void make_lowmem_page_readwrite(void *vaddr);
174
H. Peter Anvin05e4d312008-10-23 00:01:39 -0700175#endif /* _ASM_X86_XEN_PAGE_H */