1 /*
2  *
3  * Copyright (c) 2004 Christian Limpach.
4  * Copyright (c) 2004,2005 Kip Macy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christian Limpach.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * $FreeBSD: stable/10/sys/i386/include/xen/xenpmap.h 230916 2012-02-02 17:54:35Z ken $
34  */
35 
36 #ifndef _XEN_XENPMAP_H_
37 #define _XEN_XENPMAP_H_
38 
39 #if defined(XEN)
40 void _xen_queue_pt_update(vm_paddr_t, vm_paddr_t, char *, int);
41 void xen_pt_switch(vm_paddr_t);
42 void xen_set_ldt(vm_paddr_t, unsigned long);
43 void xen_pgdpt_pin(vm_paddr_t);
44 void xen_pgd_pin(vm_paddr_t);
45 void xen_pgd_unpin(vm_paddr_t);
46 void xen_pt_pin(vm_paddr_t);
47 void xen_pt_unpin(vm_paddr_t);
48 void xen_flush_queue(void);
49 void pmap_ref(pt_entry_t *pte, vm_paddr_t ma);
50 void pmap_suspend(void);
51 void pmap_resume(void);
52 void xen_check_queue(void);
53 
54 #ifdef INVARIANTS
55 #define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), __FILE__, __LINE__)
56 #else
57 #define xen_queue_pt_update(a, b) _xen_queue_pt_update((a), (b), NULL, 0)
58 #endif
59 
60 
61 #include <sys/param.h>
62 #include <sys/pcpu.h>
63 
64 #ifdef PMAP_DEBUG
65 #define PMAP_REF pmap_ref
66 #define PMAP_DEC_REF_PAGE pmap_dec_ref_page
67 #define PMAP_MARK_PRIV pmap_mark_privileged
68 #define PMAP_MARK_UNPRIV pmap_mark_unprivileged
69 #else
70 #define PMAP_MARK_PRIV(a)
71 #define PMAP_MARK_UNPRIV(a)
72 #define PMAP_REF(a, b)
73 #define PMAP_DEC_REF_PAGE(a)
74 #endif
75 
76 #define ALWAYS_SYNC 0
77 
78 #ifdef PT_DEBUG
79 #define PT_LOG() printk("WP PT_SET %s:%d\n", __FILE__, __LINE__)
80 #else
81 #define PT_LOG()
82 #endif
83 
84 #define INVALID_P2M_ENTRY	(~0UL)
85 
86 #define pmap_valid_entry(E)           ((E) & PG_V) /* is PDE or PTE valid? */
87 
88 #define SH_PD_SET_VA        1
89 #define SH_PD_SET_VA_MA     2
90 #define SH_PD_SET_VA_CLEAR  3
91 
92 struct pmap;
93 void pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type);
94 #ifdef notyet
95 static vm_paddr_t
vptetomachpte(vm_paddr_t * pte)96 vptetomachpte(vm_paddr_t *pte)
97 {
98 	vm_offset_t offset, ppte;
99 	vm_paddr_t pgoffset, retval, *pdir_shadow_ptr;
100 	int pgindex;
101 
102 	ppte = (vm_offset_t)pte;
103 	pgoffset = (ppte & PAGE_MASK);
104 	offset = ppte - (vm_offset_t)PTmap;
105 	pgindex = ppte >> PDRSHIFT;
106 
107 	pdir_shadow_ptr = (vm_paddr_t *)PCPU_GET(pdir_shadow);
108 	retval = (pdir_shadow_ptr[pgindex] & ~PAGE_MASK) + pgoffset;
109 	return (retval);
110 }
111 #endif
112 #define	PT_GET(_ptp)						\
113 	(pmap_valid_entry(*(_ptp)) ? xpmap_mtop(*(_ptp)) : (0))
114 
115 #ifdef WRITABLE_PAGETABLES
116 
117 #define PT_SET_VA(_ptp,_npte,sync) do {				\
118         PMAP_REF((_ptp), xpmap_ptom(_npte));                    \
119         PT_LOG();                                               \
120         *(_ptp) = xpmap_ptom((_npte));                          \
121 } while (/*CONSTCOND*/0)
122 #define PT_SET_VA_MA(_ptp,_npte,sync) do {		        \
123         PMAP_REF((_ptp), (_npte));                              \
124         PT_LOG();                                               \
125         *(_ptp) = (_npte);                                      \
126 } while (/*CONSTCOND*/0)
127 #define PT_CLEAR_VA(_ptp, sync) do {				\
128         PMAP_REF((pt_entry_t *)(_ptp), 0);                      \
129         PT_LOG();                                               \
130         *(_ptp) = 0;                                            \
131 } while (/*CONSTCOND*/0)
132 
133 #define PD_SET_VA(_pmap, _ptp, _npte, sync) do {		\
134         PMAP_REF((_ptp), xpmap_ptom(_npte));                    \
135         pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA);           \
136 	if (sync || ALWAYS_SYNC) xen_flush_queue();     	\
137 } while (/*CONSTCOND*/0)
138 #define PD_SET_VA_MA(_pmap, _ptp, _npte, sync) do {		\
139         PMAP_REF((_ptp), (_npte));                              \
140         pd_set((_pmap),(_ptp),(_npte), SH_PD_SET_VA_MA);        \
141 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
142 } while (/*CONSTCOND*/0)
143 #define PD_CLEAR_VA(_pmap, _ptp, sync) do {			\
144         PMAP_REF((pt_entry_t *)(_ptp), 0);                      \
145         pd_set((_pmap),(_ptp), 0, SH_PD_SET_VA_CLEAR);  	\
146 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
147 } while (/*CONSTCOND*/0)
148 
149 #else /* !WRITABLE_PAGETABLES */
150 
151 #define PT_SET_VA(_ptp,_npte,sync) do {				\
152         PMAP_REF((_ptp), xpmap_ptom(_npte));                    \
153 	xen_queue_pt_update(vtomach(_ptp), 	        \
154 			    xpmap_ptom(_npte)); 		\
155 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
156 } while (/*CONSTCOND*/0)
157 #define PT_SET_VA_MA(_ptp,_npte,sync) do {		        \
158         PMAP_REF((_ptp), (_npte));                              \
159 	xen_queue_pt_update(vtomach(_ptp), _npte);        \
160 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
161 } while (/*CONSTCOND*/0)
162 #define PT_CLEAR_VA(_ptp, sync) do {				\
163         PMAP_REF((pt_entry_t *)(_ptp), 0);                      \
164 	xen_queue_pt_update(vtomach(_ptp), 0);            \
165 	if (sync || ALWAYS_SYNC)				\
166 		xen_flush_queue();				\
167 } while (/*CONSTCOND*/0)
168 
169 #define PD_SET_VA(_pmap, _ptepindex,_npte,sync) do {		\
170         PMAP_REF((_ptp), xpmap_ptom(_npte));                    \
171         pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA);     \
172 	if (sync || ALWAYS_SYNC) xen_flush_queue();     	\
173 } while (/*CONSTCOND*/0)
174 #define PD_SET_VA_MA(_pmap, _ptepindex,_npte,sync) do {		\
175         PMAP_REF((_ptp), (_npte));                              \
176         pd_set((_pmap),(_ptepindex),(_npte), SH_PD_SET_VA_MA);  \
177 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
178 } while (/*CONSTCOND*/0)
179 #define PD_CLEAR_VA(_pmap, _ptepindex, sync) do {		\
180         PMAP_REF((pt_entry_t *)(_ptp), 0);                      \
181         pd_set((_pmap),(_ptepindex), 0, SH_PD_SET_VA_CLEAR);    \
182 	if (sync || ALWAYS_SYNC) xen_flush_queue();		\
183 } while (/*CONSTCOND*/0)
184 
185 #endif
186 
187 #define PT_SET_MA(_va, _ma) 					\
188 do { 								\
189    PANIC_IF(HYPERVISOR_update_va_mapping(((unsigned long)(_va)),\
190 	   (_ma),						\
191 	   UVMF_INVLPG| UVMF_ALL) < 0);			\
192 } while (/*CONSTCOND*/0)
193 
194 #define	PT_UPDATES_FLUSH() do {				        \
195         xen_flush_queue();                                      \
196 } while (/*CONSTCOND*/0)
197 
198 static __inline vm_paddr_t
xpmap_mtop(vm_paddr_t mpa)199 xpmap_mtop(vm_paddr_t mpa)
200 {
201 	vm_paddr_t tmp = (mpa & PG_FRAME);
202 
203 	return machtophys(tmp) | (mpa & ~PG_FRAME);
204 }
205 
206 static __inline vm_paddr_t
xpmap_ptom(vm_paddr_t ppa)207 xpmap_ptom(vm_paddr_t ppa)
208 {
209 	vm_paddr_t tmp = (ppa & PG_FRAME);
210 
211 	return phystomach(tmp) | (ppa & ~PG_FRAME);
212 }
213 
214 static __inline void
set_phys_to_machine(unsigned long pfn,unsigned long mfn)215 set_phys_to_machine(unsigned long pfn, unsigned long mfn)
216 {
217 #ifdef notyet
218         PANIC_IF(max_mapnr && pfn >= max_mapnr);
219 #endif
220         if (xen_feature(XENFEAT_auto_translated_physmap)) {
221 #ifdef notyet
222                 PANIC_IF((pfn != mfn && mfn != INVALID_P2M_ENTRY));
223 #endif
224                 return;
225         }
226         xen_phys_machine[pfn] = mfn;
227 }
228 
229 static __inline int
phys_to_machine_mapping_valid(unsigned long pfn)230 phys_to_machine_mapping_valid(unsigned long pfn)
231 {
232 	return xen_phys_machine[pfn] != INVALID_P2M_ENTRY;
233 }
234 
235 #endif /* !XEN */
236 
237 #endif /* _XEN_XENPMAP_H_ */
238