x86_xpmap.c revision 1.3.2.2 1 1.3.2.2 joerg /* $NetBSD: x86_xpmap.c,v 1.3.2.2 2007/11/27 19:36:22 joerg Exp $ */
2 1.3.2.2 joerg
3 1.3.2.2 joerg /*
4 1.3.2.2 joerg * Copyright (c) 2006 Mathieu Ropert <mro (at) adviseo.fr>
5 1.3.2.2 joerg *
6 1.3.2.2 joerg * Permission to use, copy, modify, and distribute this software for any
7 1.3.2.2 joerg * purpose with or without fee is hereby granted, provided that the above
8 1.3.2.2 joerg * copyright notice and this permission notice appear in all copies.
9 1.3.2.2 joerg *
10 1.3.2.2 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.3.2.2 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.3.2.2 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.3.2.2 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.3.2.2 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.3.2.2 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.3.2.2 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.3.2.2 joerg */
18 1.3.2.2 joerg
19 1.3.2.2 joerg /*
20 1.3.2.2 joerg * Copyright (c) 2006, 2007 Manuel Bouyer.
21 1.3.2.2 joerg *
22 1.3.2.2 joerg * Redistribution and use in source and binary forms, with or without
23 1.3.2.2 joerg * modification, are permitted provided that the following conditions
24 1.3.2.2 joerg * are met:
25 1.3.2.2 joerg * 1. Redistributions of source code must retain the above copyright
26 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer.
27 1.3.2.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
28 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer in the
29 1.3.2.2 joerg * documentation and/or other materials provided with the distribution.
30 1.3.2.2 joerg * 3. All advertising materials mentioning features or use of this software
31 1.3.2.2 joerg * must display the following acknowledgement:
32 1.3.2.2 joerg * This product includes software developed by Manuel Bouyer.
33 1.3.2.2 joerg * 4. The name of the author may not be used to endorse or promote products
34 1.3.2.2 joerg * derived from this software without specific prior written permission.
35 1.3.2.2 joerg *
36 1.3.2.2 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
37 1.3.2.2 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 1.3.2.2 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
39 1.3.2.2 joerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
40 1.3.2.2 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 1.3.2.2 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 1.3.2.2 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 1.3.2.2 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 1.3.2.2 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45 1.3.2.2 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 1.3.2.2 joerg *
47 1.3.2.2 joerg */
48 1.3.2.2 joerg
49 1.3.2.2 joerg /*
50 1.3.2.2 joerg *
51 1.3.2.2 joerg * Copyright (c) 2004 Christian Limpach.
52 1.3.2.2 joerg * All rights reserved.
53 1.3.2.2 joerg *
54 1.3.2.2 joerg * Redistribution and use in source and binary forms, with or without
55 1.3.2.2 joerg * modification, are permitted provided that the following conditions
56 1.3.2.2 joerg * are met:
57 1.3.2.2 joerg * 1. Redistributions of source code must retain the above copyright
58 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer.
59 1.3.2.2 joerg * 2. Redistributions in binary form must reproduce the above copyright
60 1.3.2.2 joerg * notice, this list of conditions and the following disclaimer in the
61 1.3.2.2 joerg * documentation and/or other materials provided with the distribution.
62 1.3.2.2 joerg * 3. All advertising materials mentioning features or use of this software
63 1.3.2.2 joerg * must display the following acknowledgement:
64 1.3.2.2 joerg * This product includes software developed by Christian Limpach.
65 1.3.2.2 joerg * 4. The name of the author may not be used to endorse or promote products
66 1.3.2.2 joerg * derived from this software without specific prior written permission.
67 1.3.2.2 joerg *
68 1.3.2.2 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
69 1.3.2.2 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
70 1.3.2.2 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
71 1.3.2.2 joerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
72 1.3.2.2 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
73 1.3.2.2 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
74 1.3.2.2 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
75 1.3.2.2 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
76 1.3.2.2 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
77 1.3.2.2 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78 1.3.2.2 joerg */
79 1.3.2.2 joerg
80 1.3.2.2 joerg
81 1.3.2.2 joerg #include <sys/cdefs.h>
82 1.3.2.2 joerg __KERNEL_RCSID(0, "$NetBSD: x86_xpmap.c,v 1.3.2.2 2007/11/27 19:36:22 joerg Exp $");
83 1.3.2.2 joerg
84 1.3.2.2 joerg #include "opt_xen.h"
85 1.3.2.2 joerg
86 1.3.2.2 joerg #include <sys/param.h>
87 1.3.2.2 joerg #include <sys/systm.h>
88 1.3.2.2 joerg
89 1.3.2.2 joerg #include <uvm/uvm.h>
90 1.3.2.2 joerg
91 1.3.2.2 joerg #include <machine/pmap.h>
92 1.3.2.2 joerg #include <machine/gdt.h>
93 1.3.2.2 joerg #include <xen/xenfunc.h>
94 1.3.2.2 joerg
95 1.3.2.2 joerg #include <dev/isa/isareg.h>
96 1.3.2.2 joerg #include <machine/isa_machdep.h>
97 1.3.2.2 joerg
98 1.3.2.2 joerg #undef XENDEBUG
99 1.3.2.2 joerg /* #define XENDEBUG_SYNC */
100 1.3.2.2 joerg /* #define XENDEBUG_LOW */
101 1.3.2.2 joerg
102 1.3.2.2 joerg #ifdef XENDEBUG
103 1.3.2.2 joerg #define XENPRINTF(x) printf x
104 1.3.2.2 joerg #define XENPRINTK(x) printk x
105 1.3.2.2 joerg #define XENPRINTK2(x) /* printk x */
106 1.3.2.2 joerg
107 1.3.2.2 joerg static char XBUF[256];
108 1.3.2.2 joerg #else
109 1.3.2.2 joerg #define XENPRINTF(x)
110 1.3.2.2 joerg #define XENPRINTK(x)
111 1.3.2.2 joerg #define XENPRINTK2(x)
112 1.3.2.2 joerg #endif
113 1.3.2.2 joerg #define PRINTF(x) printf x
114 1.3.2.2 joerg #define PRINTK(x) printk x
115 1.3.2.2 joerg
116 1.3.2.2 joerg volatile shared_info_t *HYPERVISOR_shared_info;
117 1.3.2.2 joerg union start_info_union start_info_union;
118 1.3.2.2 joerg
119 1.3.2.2 joerg void xen_failsafe_handler(void);
120 1.3.2.2 joerg
121 1.3.2.2 joerg #ifdef XEN3
122 1.3.2.2 joerg #define HYPERVISOR_mmu_update_self(req, count, success_count) \
123 1.3.2.2 joerg HYPERVISOR_mmu_update((req), (count), (success_count), DOMID_SELF)
124 1.3.2.2 joerg #else
125 1.3.2.2 joerg #define HYPERVISOR_mmu_update_self(req, count, success_count) \
126 1.3.2.2 joerg HYPERVISOR_mmu_update((req), (count), (success_count))
127 1.3.2.2 joerg #endif
128 1.3.2.2 joerg
129 1.3.2.2 joerg void
130 1.3.2.2 joerg xen_failsafe_handler(void)
131 1.3.2.2 joerg {
132 1.3.2.2 joerg
133 1.3.2.2 joerg panic("xen_failsafe_handler called!\n");
134 1.3.2.2 joerg }
135 1.3.2.2 joerg
136 1.3.2.2 joerg
137 1.3.2.2 joerg #ifndef __x86_64__
138 1.3.2.2 joerg void
139 1.3.2.2 joerg xen_update_descriptor(union descriptor *table, union descriptor *entry)
140 1.3.2.2 joerg {
141 1.3.2.2 joerg paddr_t pa;
142 1.3.2.2 joerg pt_entry_t *ptp;
143 1.3.2.2 joerg
144 1.3.2.2 joerg ptp = kvtopte((vaddr_t)table);
145 1.3.2.2 joerg pa = (*ptp & PG_FRAME) | ((vaddr_t)table & ~PG_FRAME);
146 1.3.2.2 joerg if (HYPERVISOR_update_descriptor(pa, entry->raw[0], entry->raw[1]))
147 1.3.2.2 joerg panic("HYPERVISOR_update_descriptor failed\n");
148 1.3.2.2 joerg }
149 1.3.2.2 joerg #endif
150 1.3.2.2 joerg
151 1.3.2.2 joerg void
152 1.3.2.2 joerg xen_set_ldt(vaddr_t base, uint32_t entries)
153 1.3.2.2 joerg {
154 1.3.2.2 joerg vaddr_t va;
155 1.3.2.2 joerg vaddr_t end;
156 1.3.2.2 joerg pt_entry_t *ptp, *maptp;
157 1.3.2.2 joerg int s;
158 1.3.2.2 joerg
159 1.3.2.2 joerg #ifdef __x86_64__
160 1.3.2.2 joerg end = base + (entries << 3);
161 1.3.2.2 joerg #else
162 1.3.2.2 joerg end = base + entries * sizeof(union descriptor);
163 1.3.2.2 joerg #endif
164 1.3.2.2 joerg
165 1.3.2.2 joerg for (va = base; va < end; va += PAGE_SIZE) {
166 1.3.2.2 joerg KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
167 1.3.2.2 joerg ptp = kvtopte(va);
168 1.3.2.2 joerg maptp = (pt_entry_t *)vtomach((vaddr_t)ptp);
169 1.3.2.2 joerg XENPRINTF(("xen_set_ldt %p %d %p %p\n", (void *)base,
170 1.3.2.2 joerg entries, ptp, maptp));
171 1.3.2.2 joerg PTE_CLEARBITS(ptp, maptp, PG_RW);
172 1.3.2.2 joerg }
173 1.3.2.2 joerg s = splvm();
174 1.3.2.2 joerg PTE_UPDATES_FLUSH();
175 1.3.2.2 joerg
176 1.3.2.2 joerg xpq_queue_set_ldt(base, entries);
177 1.3.2.2 joerg xpq_flush_queue();
178 1.3.2.2 joerg splx(s);
179 1.3.2.2 joerg }
180 1.3.2.2 joerg
181 1.3.2.2 joerg #ifdef XENDEBUG
182 1.3.2.2 joerg void xpq_debug_dump(void);
183 1.3.2.2 joerg #endif
184 1.3.2.2 joerg
185 1.3.2.2 joerg #define XPQUEUE_SIZE 2048
186 1.3.2.2 joerg static mmu_update_t xpq_queue[XPQUEUE_SIZE];
187 1.3.2.2 joerg static int xpq_idx = 0;
188 1.3.2.2 joerg
189 1.3.2.2 joerg void
190 1.3.2.2 joerg xpq_flush_queue()
191 1.3.2.2 joerg {
192 1.3.2.2 joerg int i, ok;
193 1.3.2.2 joerg
194 1.3.2.2 joerg XENPRINTK2(("flush queue %p entries %d\n", xpq_queue, xpq_idx));
195 1.3.2.2 joerg for (i = 0; i < xpq_idx; i++)
196 1.3.2.2 joerg XENPRINTK2(("%d: %p %08x\n", i, (u_int)xpq_queue[i].ptr,
197 1.3.2.2 joerg (u_int)xpq_queue[i].val));
198 1.3.2.2 joerg if (xpq_idx != 0 &&
199 1.3.2.2 joerg HYPERVISOR_mmu_update_self(xpq_queue, xpq_idx, &ok) < 0) {
200 1.3.2.2 joerg printf("xpq_flush_queue: %d entries \n", xpq_idx);
201 1.3.2.2 joerg for (i = 0; i < xpq_idx; i++)
202 1.3.2.2 joerg printf("0x%016" PRIx64 ": 0x%016" PRIx64 "\n",
203 1.3.2.2 joerg (u_int64_t)xpq_queue[i].ptr,
204 1.3.2.2 joerg (u_int64_t)xpq_queue[i].val);
205 1.3.2.2 joerg panic("HYPERVISOR_mmu_update failed\n");
206 1.3.2.2 joerg }
207 1.3.2.2 joerg xpq_idx = 0;
208 1.3.2.2 joerg }
209 1.3.2.2 joerg
210 1.3.2.2 joerg static inline void
211 1.3.2.2 joerg xpq_increment_idx(void)
212 1.3.2.2 joerg {
213 1.3.2.2 joerg
214 1.3.2.2 joerg xpq_idx++;
215 1.3.2.2 joerg if (__predict_false(xpq_idx == XPQUEUE_SIZE))
216 1.3.2.2 joerg xpq_flush_queue();
217 1.3.2.2 joerg }
218 1.3.2.2 joerg
219 1.3.2.2 joerg void
220 1.3.2.2 joerg xpq_queue_machphys_update(paddr_t ma, paddr_t pa)
221 1.3.2.2 joerg {
222 1.3.2.2 joerg XENPRINTK2(("xpq_queue_machphys_update ma=%p pa=%p\n", (void *)ma, (void *)pa));
223 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = ma | MMU_MACHPHYS_UPDATE;
224 1.3.2.2 joerg xpq_queue[xpq_idx].val = (pa - XPMAP_OFFSET) >> PAGE_SHIFT;
225 1.3.2.2 joerg xpq_increment_idx();
226 1.3.2.2 joerg #ifdef XENDEBUG_SYNC
227 1.3.2.2 joerg xpq_flush_queue();
228 1.3.2.2 joerg #endif
229 1.3.2.2 joerg }
230 1.3.2.2 joerg
231 1.3.2.2 joerg void
232 1.3.2.2 joerg xpq_queue_pde_update(pd_entry_t *ptr, pd_entry_t val)
233 1.3.2.2 joerg {
234 1.3.2.2 joerg
235 1.3.2.2 joerg KASSERT(((paddr_t)ptr & 3) == 0);
236 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = (paddr_t)ptr | MMU_NORMAL_PT_UPDATE;
237 1.3.2.2 joerg xpq_queue[xpq_idx].val = val;
238 1.3.2.2 joerg xpq_increment_idx();
239 1.3.2.2 joerg #ifdef XENDEBUG_SYNC
240 1.3.2.2 joerg xpq_flush_queue();
241 1.3.2.2 joerg #endif
242 1.3.2.2 joerg }
243 1.3.2.2 joerg
244 1.3.2.2 joerg void
245 1.3.2.2 joerg xpq_queue_pte_update(pt_entry_t *ptr, pt_entry_t val)
246 1.3.2.2 joerg {
247 1.3.2.2 joerg
248 1.3.2.2 joerg KASSERT(((paddr_t)ptr & 3) == 0);
249 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = (paddr_t)ptr | MMU_NORMAL_PT_UPDATE;
250 1.3.2.2 joerg xpq_queue[xpq_idx].val = val;
251 1.3.2.2 joerg xpq_increment_idx();
252 1.3.2.2 joerg #ifdef XENDEBUG_SYNC
253 1.3.2.2 joerg xpq_flush_queue();
254 1.3.2.2 joerg #endif
255 1.3.2.2 joerg }
256 1.3.2.2 joerg
257 1.3.2.2 joerg #ifdef XEN3
258 1.3.2.2 joerg void
259 1.3.2.2 joerg xpq_queue_pt_switch(paddr_t pa)
260 1.3.2.2 joerg {
261 1.3.2.2 joerg struct mmuext_op op;
262 1.3.2.2 joerg xpq_flush_queue();
263 1.3.2.2 joerg
264 1.3.2.2 joerg XENPRINTK2(("xpq_queue_pt_switch: %p %p\n", (void *)pa, (void *)pa));
265 1.3.2.2 joerg op.cmd = MMUEXT_NEW_BASEPTR;
266 1.3.2.2 joerg op.arg1.mfn = pa >> PAGE_SHIFT;
267 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
268 1.3.2.2 joerg panic("xpq_queue_pt_switch");
269 1.3.2.2 joerg }
270 1.3.2.2 joerg
271 1.3.2.2 joerg void
272 1.3.2.2 joerg xpq_queue_pin_table(paddr_t pa)
273 1.3.2.2 joerg {
274 1.3.2.2 joerg struct mmuext_op op;
275 1.3.2.2 joerg xpq_flush_queue();
276 1.3.2.2 joerg
277 1.3.2.2 joerg XENPRINTK2(("xpq_queue_pin_table: %p %p\n", (void *)pa, (void *)pa));
278 1.3.2.2 joerg op.arg1.mfn = pa >> PAGE_SHIFT;
279 1.3.2.2 joerg
280 1.3.2.2 joerg #ifdef __x86_64__
281 1.3.2.2 joerg op.cmd = MMUEXT_PIN_L4_TABLE;
282 1.3.2.2 joerg #else
283 1.3.2.2 joerg op.cmd = MMUEXT_PIN_L2_TABLE;
284 1.3.2.2 joerg #endif
285 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
286 1.3.2.2 joerg panic("xpq_queue_pin_table");
287 1.3.2.2 joerg }
288 1.3.2.2 joerg
289 1.3.2.2 joerg void
290 1.3.2.2 joerg xpq_queue_unpin_table(paddr_t pa)
291 1.3.2.2 joerg {
292 1.3.2.2 joerg struct mmuext_op op;
293 1.3.2.2 joerg xpq_flush_queue();
294 1.3.2.2 joerg
295 1.3.2.2 joerg XENPRINTK2(("xpq_queue_unpin_table: %p %p\n", (void *)pa, (void *)pa));
296 1.3.2.2 joerg op.arg1.mfn = pa >> PAGE_SHIFT;
297 1.3.2.2 joerg op.cmd = MMUEXT_UNPIN_TABLE;
298 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
299 1.3.2.2 joerg panic("xpq_queue_unpin_table");
300 1.3.2.2 joerg }
301 1.3.2.2 joerg
302 1.3.2.2 joerg void
303 1.3.2.2 joerg xpq_queue_set_ldt(vaddr_t va, uint32_t entries)
304 1.3.2.2 joerg {
305 1.3.2.2 joerg struct mmuext_op op;
306 1.3.2.2 joerg xpq_flush_queue();
307 1.3.2.2 joerg
308 1.3.2.2 joerg XENPRINTK2(("xpq_queue_set_ldt\n"));
309 1.3.2.2 joerg KASSERT(va == (va & ~PAGE_MASK));
310 1.3.2.2 joerg op.cmd = MMUEXT_SET_LDT;
311 1.3.2.2 joerg op.arg1.linear_addr = va;
312 1.3.2.2 joerg op.arg2.nr_ents = entries;
313 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
314 1.3.2.2 joerg panic("xpq_queue_set_ldt");
315 1.3.2.2 joerg }
316 1.3.2.2 joerg
317 1.3.2.2 joerg void
318 1.3.2.2 joerg xpq_queue_tlb_flush()
319 1.3.2.2 joerg {
320 1.3.2.2 joerg struct mmuext_op op;
321 1.3.2.2 joerg xpq_flush_queue();
322 1.3.2.2 joerg
323 1.3.2.2 joerg XENPRINTK2(("xpq_queue_tlb_flush\n"));
324 1.3.2.2 joerg op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
325 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
326 1.3.2.2 joerg panic("xpq_queue_tlb_flush");
327 1.3.2.2 joerg }
328 1.3.2.2 joerg
329 1.3.2.2 joerg void
330 1.3.2.2 joerg xpq_flush_cache()
331 1.3.2.2 joerg {
332 1.3.2.2 joerg struct mmuext_op op;
333 1.3.2.2 joerg int s = splvm();
334 1.3.2.2 joerg xpq_flush_queue();
335 1.3.2.2 joerg
336 1.3.2.2 joerg XENPRINTK2(("xpq_queue_flush_cache\n"));
337 1.3.2.2 joerg op.cmd = MMUEXT_FLUSH_CACHE;
338 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
339 1.3.2.2 joerg panic("xpq_flush_cache");
340 1.3.2.2 joerg splx(s);
341 1.3.2.2 joerg }
342 1.3.2.2 joerg
343 1.3.2.2 joerg void
344 1.3.2.2 joerg xpq_queue_invlpg(vaddr_t va)
345 1.3.2.2 joerg {
346 1.3.2.2 joerg struct mmuext_op op;
347 1.3.2.2 joerg xpq_flush_queue();
348 1.3.2.2 joerg
349 1.3.2.2 joerg XENPRINTK2(("xpq_queue_invlpg %p\n", (void *)va));
350 1.3.2.2 joerg op.cmd = MMUEXT_INVLPG_LOCAL;
351 1.3.2.2 joerg op.arg1.linear_addr = (va & ~PAGE_MASK);
352 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
353 1.3.2.2 joerg panic("xpq_queue_invlpg");
354 1.3.2.2 joerg }
355 1.3.2.2 joerg
356 1.3.2.2 joerg int
357 1.3.2.2 joerg xpq_update_foreign(pt_entry_t *ptr, pt_entry_t val, int dom)
358 1.3.2.2 joerg {
359 1.3.2.2 joerg mmu_update_t op;
360 1.3.2.2 joerg int ok;
361 1.3.2.2 joerg xpq_flush_queue();
362 1.3.2.2 joerg
363 1.3.2.2 joerg op.ptr = (paddr_t)ptr;
364 1.3.2.2 joerg op.val = val;
365 1.3.2.2 joerg if (HYPERVISOR_mmu_update(&op, 1, &ok, dom) < 0)
366 1.3.2.2 joerg return EFAULT;
367 1.3.2.2 joerg return (0);
368 1.3.2.2 joerg }
369 1.3.2.2 joerg #else /* XEN3 */
370 1.3.2.2 joerg void
371 1.3.2.2 joerg xpq_queue_pt_switch(paddr_t pa)
372 1.3.2.2 joerg {
373 1.3.2.2 joerg
374 1.3.2.2 joerg XENPRINTK2(("xpq_queue_pt_switch: %p %p\n", (void *)pa, (void *)pa));
375 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = pa | MMU_EXTENDED_COMMAND;
376 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_NEW_BASEPTR;
377 1.3.2.2 joerg xpq_increment_idx();
378 1.3.2.2 joerg }
379 1.3.2.2 joerg
380 1.3.2.2 joerg void
381 1.3.2.2 joerg xpq_queue_pin_table(paddr_t pa)
382 1.3.2.2 joerg {
383 1.3.2.2 joerg
384 1.3.2.2 joerg XENPRINTK2(("xpq_queue_pin_table: %p %p\n", (void *)pa, (void *)pa));
385 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = pa | MMU_EXTENDED_COMMAND;
386 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_PIN_L2_TABLE;
387 1.3.2.2 joerg xpq_increment_idx();
388 1.3.2.2 joerg }
389 1.3.2.2 joerg
390 1.3.2.2 joerg void
391 1.3.2.2 joerg xpq_queue_unpin_table(paddr_t pa)
392 1.3.2.2 joerg {
393 1.3.2.2 joerg
394 1.3.2.2 joerg XENPRINTK2(("xpq_queue_unpin_table: %p %p\n", (void *)pa, (void *)pa));
395 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = pa | MMU_EXTENDED_COMMAND;
396 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_UNPIN_TABLE;
397 1.3.2.2 joerg xpq_increment_idx();
398 1.3.2.2 joerg }
399 1.3.2.2 joerg
400 1.3.2.2 joerg void
401 1.3.2.2 joerg xpq_queue_set_ldt(vaddr_t va, uint32_t entries)
402 1.3.2.2 joerg {
403 1.3.2.2 joerg
404 1.3.2.2 joerg XENPRINTK2(("xpq_queue_set_ldt\n"));
405 1.3.2.2 joerg KASSERT(va == (va & ~PAGE_MASK));
406 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = MMU_EXTENDED_COMMAND | va;
407 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_SET_LDT | (entries << MMUEXT_CMD_SHIFT);
408 1.3.2.2 joerg xpq_increment_idx();
409 1.3.2.2 joerg }
410 1.3.2.2 joerg
411 1.3.2.2 joerg void
412 1.3.2.2 joerg xpq_queue_tlb_flush()
413 1.3.2.2 joerg {
414 1.3.2.2 joerg
415 1.3.2.2 joerg XENPRINTK2(("xpq_queue_tlb_flush\n"));
416 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = MMU_EXTENDED_COMMAND;
417 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_TLB_FLUSH;
418 1.3.2.2 joerg xpq_increment_idx();
419 1.3.2.2 joerg }
420 1.3.2.2 joerg
421 1.3.2.2 joerg void
422 1.3.2.2 joerg xpq_flush_cache()
423 1.3.2.2 joerg {
424 1.3.2.2 joerg int s = splvm();
425 1.3.2.2 joerg
426 1.3.2.2 joerg XENPRINTK2(("xpq_queue_flush_cache\n"));
427 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = MMU_EXTENDED_COMMAND;
428 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_FLUSH_CACHE;
429 1.3.2.2 joerg xpq_increment_idx();
430 1.3.2.2 joerg xpq_flush_queue();
431 1.3.2.2 joerg splx(s);
432 1.3.2.2 joerg }
433 1.3.2.2 joerg
434 1.3.2.2 joerg void
435 1.3.2.2 joerg xpq_queue_invlpg(vaddr_t va)
436 1.3.2.2 joerg {
437 1.3.2.2 joerg
438 1.3.2.2 joerg XENPRINTK2(("xpq_queue_invlpg %p\n", (void *)va));
439 1.3.2.2 joerg xpq_queue[xpq_idx].ptr = (va & ~PAGE_MASK) | MMU_EXTENDED_COMMAND;
440 1.3.2.2 joerg xpq_queue[xpq_idx].val = MMUEXT_INVLPG;
441 1.3.2.2 joerg xpq_increment_idx();
442 1.3.2.2 joerg }
443 1.3.2.2 joerg
444 1.3.2.2 joerg int
445 1.3.2.2 joerg xpq_update_foreign(pt_entry_t *ptr, pt_entry_t val, int dom)
446 1.3.2.2 joerg {
447 1.3.2.2 joerg mmu_update_t xpq_up[3];
448 1.3.2.2 joerg
449 1.3.2.2 joerg xpq_up[0].ptr = MMU_EXTENDED_COMMAND;
450 1.3.2.2 joerg xpq_up[0].val = MMUEXT_SET_FOREIGNDOM | (dom << 16);
451 1.3.2.2 joerg xpq_up[1].ptr = (paddr_t)ptr;
452 1.3.2.2 joerg xpq_up[1].val = val;
453 1.3.2.2 joerg if (HYPERVISOR_mmu_update_self(xpq_up, 2, NULL) < 0)
454 1.3.2.2 joerg return EFAULT;
455 1.3.2.2 joerg return (0);
456 1.3.2.2 joerg }
457 1.3.2.2 joerg #endif /* XEN3 */
458 1.3.2.2 joerg
459 1.3.2.2 joerg #ifdef XENDEBUG
460 1.3.2.2 joerg void
461 1.3.2.2 joerg xpq_debug_dump()
462 1.3.2.2 joerg {
463 1.3.2.2 joerg int i;
464 1.3.2.2 joerg
465 1.3.2.2 joerg XENPRINTK2(("idx: %d\n", xpq_idx));
466 1.3.2.2 joerg for (i = 0; i < xpq_idx; i++) {
467 1.3.2.2 joerg sprintf(XBUF, "%x %08x ", (u_int)xpq_queue[i].ptr,
468 1.3.2.2 joerg (u_int)xpq_queue[i].val);
469 1.3.2.2 joerg if (++i < xpq_idx)
470 1.3.2.2 joerg sprintf(XBUF + strlen(XBUF), "%x %08x ",
471 1.3.2.2 joerg (u_int)xpq_queue[i].ptr, (u_int)xpq_queue[i].val);
472 1.3.2.2 joerg if (++i < xpq_idx)
473 1.3.2.2 joerg sprintf(XBUF + strlen(XBUF), "%x %08x ",
474 1.3.2.2 joerg (u_int)xpq_queue[i].ptr, (u_int)xpq_queue[i].val);
475 1.3.2.2 joerg if (++i < xpq_idx)
476 1.3.2.2 joerg sprintf(XBUF + strlen(XBUF), "%x %08x ",
477 1.3.2.2 joerg (u_int)xpq_queue[i].ptr, (u_int)xpq_queue[i].val);
478 1.3.2.2 joerg XENPRINTK2(("%d: %s\n", xpq_idx, XBUF));
479 1.3.2.2 joerg }
480 1.3.2.2 joerg }
481 1.3.2.2 joerg #endif
482 1.3.2.2 joerg
483 1.3.2.2 joerg
484 1.3.2.2 joerg #ifdef __x86_64__
485 1.3.2.2 joerg extern volatile struct xencons_interface *xencons_interface; /* XXX */
486 1.3.2.2 joerg extern struct xenstore_domain_interface *xenstore_interface; /* XXX */
487 1.3.2.2 joerg
488 1.3.2.2 joerg static void xen_bt_set_readonly (vaddr_t);
489 1.3.2.2 joerg static void xen_bootstrap_tables (vaddr_t, vaddr_t, int, int, int);
490 1.3.2.2 joerg
491 1.3.2.2 joerg /* How many PDEs ? */
492 1.3.2.2 joerg #if L2_SLOT_KERNBASE > 0
493 1.3.2.2 joerg #define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
494 1.3.2.2 joerg #else
495 1.3.2.2 joerg #define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1)
496 1.3.2.2 joerg #endif
497 1.3.2.2 joerg
498 1.3.2.2 joerg /*
499 1.3.2.2 joerg * Construct and switch to new pagetables
500 1.3.2.2 joerg * first_avail is the first vaddr we can use after
501 1.3.2.2 joerg * we get rid of Xen pagetables
502 1.3.2.2 joerg */
503 1.3.2.2 joerg
504 1.3.2.2 joerg vaddr_t xen_pmap_bootstrap (void);
505 1.3.2.2 joerg
506 1.3.2.2 joerg /*
507 1.3.2.2 joerg * Function to get rid of Xen bootstrap tables
508 1.3.2.2 joerg */
509 1.3.2.2 joerg
510 1.3.2.2 joerg vaddr_t
511 1.3.2.2 joerg xen_pmap_bootstrap()
512 1.3.2.2 joerg {
513 1.3.2.2 joerg int count, iocount = 0;
514 1.3.2.2 joerg vaddr_t bootstrap_tables, init_tables;
515 1.3.2.2 joerg
516 1.3.2.2 joerg xpmap_phys_to_machine_mapping = (paddr_t *) xen_start_info.mfn_list;
517 1.3.2.2 joerg init_tables = xen_start_info.pt_base;
518 1.3.2.2 joerg __PRINTK(("xen_arch_pmap_bootstrap init_tables=0x%lx\n", init_tables));
519 1.3.2.2 joerg
520 1.3.2.2 joerg /* Space after Xen boostrap tables should be free */
521 1.3.2.2 joerg bootstrap_tables = xen_start_info.pt_base +
522 1.3.2.2 joerg (xen_start_info.nr_pt_frames * PAGE_SIZE);
523 1.3.2.2 joerg
524 1.3.2.2 joerg /* Calculate how many tables we need */
525 1.3.2.2 joerg count = TABLE_L2_ENTRIES;
526 1.3.2.2 joerg
527 1.3.2.2 joerg #ifdef DOM0OPS
528 1.3.2.2 joerg if (xen_start_info.flags & SIF_INITDOMAIN) {
529 1.3.2.2 joerg /* space for ISA I/O mem */
530 1.3.2.2 joerg iocount = IOM_SIZE / PAGE_SIZE;
531 1.3.2.2 joerg }
532 1.3.2.2 joerg #endif
533 1.3.2.2 joerg
534 1.3.2.2 joerg /*
535 1.3.2.2 joerg * Xen space we'll reclaim may not be enough for our new page tables,
536 1.3.2.2 joerg * move bootstrap tables if necessary
537 1.3.2.2 joerg */
538 1.3.2.2 joerg
539 1.3.2.2 joerg if (bootstrap_tables < init_tables + ((count+3+iocount) * PAGE_SIZE))
540 1.3.2.2 joerg bootstrap_tables = init_tables +
541 1.3.2.2 joerg ((count+3+iocount) * PAGE_SIZE);
542 1.3.2.2 joerg
543 1.3.2.2 joerg /* Create temporary tables */
544 1.3.2.2 joerg xen_bootstrap_tables(xen_start_info.pt_base, bootstrap_tables,
545 1.3.2.2 joerg xen_start_info.nr_pt_frames, count, 0);
546 1.3.2.2 joerg
547 1.3.2.2 joerg /* get vaddr space for the shared info and the console pages */
548 1.3.2.2 joerg
549 1.3.2.2 joerg /* Create final tables */
550 1.3.2.2 joerg xen_bootstrap_tables(bootstrap_tables, init_tables,
551 1.3.2.2 joerg count + 3, count, 1);
552 1.3.2.2 joerg
553 1.3.2.2 joerg return (init_tables + ((count + 3) * PAGE_SIZE));
554 1.3.2.2 joerg }
555 1.3.2.2 joerg
556 1.3.2.2 joerg
557 1.3.2.2 joerg /*
558 1.3.2.2 joerg * Build a new table and switch to it
559 1.3.2.2 joerg * old_count is # of old tables (including PGD, PDTPE and PDE)
560 1.3.2.2 joerg * new_count is # of new tables (PTE only)
561 1.3.2.2 joerg * we assume areas don't overlap
562 1.3.2.2 joerg */
563 1.3.2.2 joerg
564 1.3.2.2 joerg
565 1.3.2.2 joerg static void
566 1.3.2.2 joerg xen_bootstrap_tables (vaddr_t old_pgd, vaddr_t new_pgd,
567 1.3.2.2 joerg int old_count, int new_count, int final)
568 1.3.2.2 joerg {
569 1.3.2.2 joerg pd_entry_t *pdtpe, *pde, *pte;
570 1.3.2.2 joerg pd_entry_t *cur_pgd, *bt_pgd;
571 1.3.2.2 joerg paddr_t addr, page;
572 1.3.2.2 joerg vaddr_t avail, text_end, map_end;
573 1.3.2.2 joerg int i;
574 1.3.2.2 joerg extern char __data_start;
575 1.3.2.2 joerg
576 1.3.2.2 joerg __PRINTK(("xen_bootstrap_tables(0x%lx, 0x%lx, %d, %d)\n",
577 1.3.2.2 joerg old_pgd, new_pgd, old_count, new_count));
578 1.3.2.2 joerg text_end = ((vaddr_t)&__data_start) & ~PAGE_MASK;
579 1.3.2.2 joerg /*
580 1.3.2.2 joerg * size of R/W area after kernel text:
581 1.3.2.2 joerg * xencons_interface (if present)
582 1.3.2.2 joerg * xenstore_interface (if present)
583 1.3.2.2 joerg * table pages (new_count + 3 entries)
584 1.3.2.2 joerg * UAREA
585 1.3.2.2 joerg * dummy user PGD
586 1.3.2.2 joerg * extra mappings (only when final is true):
587 1.3.2.2 joerg * HYPERVISOR_shared_info
588 1.3.2.2 joerg * ISA I/O mem (if needed)
589 1.3.2.2 joerg */
590 1.3.2.2 joerg map_end = new_pgd + ((new_count + 3 + UPAGES + 1) * NBPG);
591 1.3.2.2 joerg if (final) {
592 1.3.2.2 joerg HYPERVISOR_shared_info = (struct shared_info *)map_end;
593 1.3.2.2 joerg map_end += NBPG;
594 1.3.2.2 joerg }
595 1.3.2.2 joerg #ifdef DOM0OPS
596 1.3.2.2 joerg if (final && (xen_start_info.flags & SIF_INITDOMAIN)) {
597 1.3.2.2 joerg /* ISA I/O mem */
598 1.3.2.2 joerg atdevbase = map_end;
599 1.3.2.2 joerg map_end += IOM_SIZE;
600 1.3.2.2 joerg }
601 1.3.2.2 joerg #endif /* DOM0OPS */
602 1.3.2.2 joerg
603 1.3.2.2 joerg __PRINTK(("xen_bootstrap_tables text_end 0x%lx map_end 0x%lx\n",
604 1.3.2.2 joerg text_end, map_end));
605 1.3.2.2 joerg
606 1.3.2.2 joerg /*
607 1.3.2.2 joerg * Create bootstrap page tables
608 1.3.2.2 joerg * What we need:
609 1.3.2.2 joerg * - a PGD (level 4)
610 1.3.2.2 joerg * - a PDTPE (level 3)
611 1.3.2.2 joerg * - a PDE (level2)
612 1.3.2.2 joerg * - some PTEs (level 1)
613 1.3.2.2 joerg */
614 1.3.2.2 joerg
615 1.3.2.2 joerg cur_pgd = (pd_entry_t *) old_pgd;
616 1.3.2.2 joerg bt_pgd = (pd_entry_t *) new_pgd;
617 1.3.2.2 joerg memset (bt_pgd, 0, PAGE_SIZE);
618 1.3.2.2 joerg avail = new_pgd + PAGE_SIZE;
619 1.3.2.2 joerg
620 1.3.2.2 joerg /* Install level 3 */
621 1.3.2.2 joerg pdtpe = (pd_entry_t *) avail;
622 1.3.2.2 joerg memset (pdtpe, 0, PAGE_SIZE);
623 1.3.2.2 joerg avail += PAGE_SIZE;
624 1.3.2.2 joerg
625 1.3.2.2 joerg addr = ((paddr_t) pdtpe) - KERNBASE;
626 1.3.2.2 joerg bt_pgd[pl4_pi(KERNTEXTOFF)] =
627 1.3.2.2 joerg xpmap_ptom_masked(addr) | PG_u | PG_RW | PG_V;
628 1.3.2.2 joerg
629 1.3.2.2 joerg __PRINTK(("L3 va 0x%lx pa 0x%lx entry 0x%lx -> L4[0x%x]\n",
630 1.3.2.2 joerg pdtpe, addr, bt_pgd[pl4_pi(KERNTEXTOFF)], pl4_pi(KERNTEXTOFF)));
631 1.3.2.2 joerg
632 1.3.2.2 joerg /* Level 2 */
633 1.3.2.2 joerg pde = (pd_entry_t *) avail;
634 1.3.2.2 joerg memset(pde, 0, PAGE_SIZE);
635 1.3.2.2 joerg avail += PAGE_SIZE;
636 1.3.2.2 joerg
637 1.3.2.2 joerg addr = ((paddr_t) pde) - KERNBASE;
638 1.3.2.2 joerg pdtpe[pl3_pi(KERNTEXTOFF)] =
639 1.3.2.2 joerg xpmap_ptom_masked(addr) | PG_u | PG_RW | PG_V;
640 1.3.2.2 joerg __PRINTK(("L2 va 0x%lx pa 0x%lx entry 0x%lx -> L3[0x%x]\n",
641 1.3.2.2 joerg pde, addr, pdtpe[pl3_pi(KERNTEXTOFF)], pl3_pi(KERNTEXTOFF)));
642 1.3.2.2 joerg
643 1.3.2.2 joerg /* Level 1 */
644 1.3.2.2 joerg page = KERNTEXTOFF;
645 1.3.2.2 joerg for (i = 0; i < new_count; i ++) {
646 1.3.2.2 joerg paddr_t cur_page = page;
647 1.3.2.2 joerg
648 1.3.2.2 joerg pte = (pd_entry_t *) avail;
649 1.3.2.2 joerg avail += PAGE_SIZE;
650 1.3.2.2 joerg
651 1.3.2.2 joerg memset(pte, 0, PAGE_SIZE);
652 1.3.2.2 joerg while (pl2_pi(page) == pl2_pi (cur_page)) {
653 1.3.2.2 joerg if (page >= map_end) {
654 1.3.2.2 joerg /* not mapped at all */
655 1.3.2.2 joerg pte[pl1_pi(page)] = 0;
656 1.3.2.2 joerg page += PAGE_SIZE;
657 1.3.2.2 joerg continue;
658 1.3.2.2 joerg }
659 1.3.2.2 joerg pte[pl1_pi(page)] = xpmap_ptom_masked(page - KERNBASE);
660 1.3.2.2 joerg if (page == (vaddr_t)HYPERVISOR_shared_info) {
661 1.3.2.2 joerg pte[pl1_pi(page)] = xen_start_info.shared_info;
662 1.3.2.2 joerg __PRINTK(("HYPERVISOR_shared_info "
663 1.3.2.2 joerg "va 0x%lx pte 0x%lx\n",
664 1.3.2.2 joerg HYPERVISOR_shared_info, pte[pl1_pi(page)]));
665 1.3.2.2 joerg }
666 1.3.2.2 joerg if (xpmap_ptom_masked(page - KERNBASE) ==
667 1.3.2.2 joerg (xen_start_info.console_mfn << PAGE_SHIFT)) {
668 1.3.2.2 joerg xencons_interface = (void *)page;
669 1.3.2.2 joerg pte[pl1_pi(page)] =
670 1.3.2.2 joerg (xen_start_info.console_mfn << PAGE_SHIFT);
671 1.3.2.2 joerg __PRINTK(("xencons_interface "
672 1.3.2.2 joerg va 0x%lx pte 0x%lx\n",
673 1.3.2.2 joerg xencons_interface, pte[pl1_pi(page)]));
674 1.3.2.2 joerg }
675 1.3.2.2 joerg if (xpmap_ptom_masked(page - KERNBASE) ==
676 1.3.2.2 joerg (xen_start_info.store_mfn << PAGE_SHIFT)) {
677 1.3.2.2 joerg xenstore_interface = (void *)page;
678 1.3.2.2 joerg pte[pl1_pi(page)] =
679 1.3.2.2 joerg (xen_start_info.store_mfn << PAGE_SHIFT);
680 1.3.2.2 joerg __PRINTK(("xenstore_interface "
681 1.3.2.2 joerg "va 0x%lx pte 0x%lx\n",
682 1.3.2.2 joerg xenstore_interface, pte[pl1_pi(page)]));
683 1.3.2.2 joerg }
684 1.3.2.2 joerg #ifdef DOM0OPS
685 1.3.2.2 joerg if (page >= (vaddr_t)atdevbase &&
686 1.3.2.2 joerg page < (vaddr_t)atdevbase + IOM_SIZE) {
687 1.3.2.2 joerg pte[pl1_pi(page)] =
688 1.3.2.2 joerg IOM_BEGIN + (page - (vaddr_t)atdevbase);
689 1.3.2.2 joerg }
690 1.3.2.2 joerg #endif
691 1.3.2.2 joerg pte[pl1_pi(page)] |= PG_u | PG_V;
692 1.3.2.2 joerg if (page < text_end) {
693 1.3.2.2 joerg /* map kernel text RO */
694 1.3.2.2 joerg pte[pl1_pi(page)] |= 0;
695 1.3.2.2 joerg } else if (page >= old_pgd
696 1.3.2.2 joerg && page < old_pgd + (old_count * PAGE_SIZE)) {
697 1.3.2.2 joerg /* map old page tables RO */
698 1.3.2.2 joerg pte[pl1_pi(page)] |= 0;
699 1.3.2.2 joerg } else if (page >= new_pgd &&
700 1.3.2.2 joerg page < new_pgd + ((new_count + 3) * PAGE_SIZE)) {
701 1.3.2.2 joerg /* map new page tables RO */
702 1.3.2.2 joerg pte[pl1_pi(page)] |= 0;
703 1.3.2.2 joerg } else {
704 1.3.2.2 joerg /* map page RW */
705 1.3.2.2 joerg pte[pl1_pi(page)] |= PG_RW;
706 1.3.2.2 joerg }
707 1.3.2.2 joerg if (page == old_pgd)
708 1.3.2.2 joerg __PRINTK(("va 0x%lx pa 0x%lx
709 1.3.2.2 joerg "entry 0x%lx -> L1[0x%x]\n",
710 1.3.2.2 joerg page, page - KERNBASE,
711 1.3.2.2 joerg pte[pl1_pi(page)], pl1_pi(page)));
712 1.3.2.2 joerg page += PAGE_SIZE;
713 1.3.2.2 joerg }
714 1.3.2.2 joerg
715 1.3.2.2 joerg addr = ((paddr_t) pte) - KERNBASE;
716 1.3.2.2 joerg pde[pl2_pi(cur_page)] =
717 1.3.2.2 joerg xpmap_ptom_masked(addr) | PG_u | PG_RW | PG_V;
718 1.3.2.2 joerg __PRINTK(("L1 va 0x%lx pa 0x%lx entry 0x%lx -> L2[0x%x]\n",
719 1.3.2.2 joerg pte, addr, pde[pl2_pi(cur_page)], pl2_pi(cur_page)));
720 1.3.2.2 joerg /* Mark readonly */
721 1.3.2.2 joerg xen_bt_set_readonly((vaddr_t) pte);
722 1.3.2.2 joerg }
723 1.3.2.2 joerg
724 1.3.2.2 joerg /* Install recursive page tables mapping */
725 1.3.2.2 joerg bt_pgd[PDIR_SLOT_PTE] =
726 1.3.2.2 joerg xpmap_ptom_masked(new_pgd - KERNBASE) | PG_u | PG_V;
727 1.3.2.2 joerg __PRINTK(("bt_pgd[PDIR_SLOT_PTE] va 0x%lx pa 0x%lx entry 0x%lx\n",
728 1.3.2.2 joerg new_pgd, new_pgd - KERNBASE, bt_pgd[PDIR_SLOT_PTE]));
729 1.3.2.2 joerg
730 1.3.2.2 joerg /* Mark tables RO */
731 1.3.2.2 joerg xen_bt_set_readonly((vaddr_t) pde);
732 1.3.2.2 joerg xen_bt_set_readonly((vaddr_t) pdtpe);
733 1.3.2.2 joerg xen_bt_set_readonly(new_pgd);
734 1.3.2.2 joerg /* Pin the PGD */
735 1.3.2.2 joerg __PRINTK(("pin PDG\n"));
736 1.3.2.2 joerg xpq_queue_pin_table(xpmap_ptom_masked(new_pgd - KERNBASE));
737 1.3.2.2 joerg /* Switch to new tables */
738 1.3.2.2 joerg __PRINTK(("switch to PDG\n"));
739 1.3.2.2 joerg xpq_queue_pt_switch(xpmap_ptom_masked(new_pgd - KERNBASE));
740 1.3.2.2 joerg __PRINTK(("bt_pgd[PDIR_SLOT_PTE] now entry 0x%lx\n",
741 1.3.2.2 joerg bt_pgd[PDIR_SLOT_PTE]));
742 1.3.2.2 joerg __PRINTK(("L4_BASE va 0x%lx\n", (long)L4_BASE));
743 1.3.2.2 joerg __PRINTK(("value 0x%lx\n", *L4_BASE));
744 1.3.2.2 joerg __PRINTK(("[PDIR_SLOT_PTE] 0x%lx\n", L4_BASE[PDIR_SLOT_PTE]));
745 1.3.2.2 joerg
746 1.3.2.2 joerg /* Now we can safely reclaim space taken by old tables */
747 1.3.2.2 joerg
748 1.3.2.2 joerg __PRINTK(("unpin old PDG\n"));
749 1.3.2.2 joerg /* Unpin old PGD */
750 1.3.2.2 joerg xpq_queue_unpin_table(xpmap_ptom_masked(old_pgd - KERNBASE));
751 1.3.2.2 joerg /* Mark old tables RW */
752 1.3.2.2 joerg page = old_pgd;
753 1.3.2.2 joerg addr = (paddr_t) pde[pl2_pi(page)] & PG_FRAME;
754 1.3.2.2 joerg addr = xpmap_mtop(addr);
755 1.3.2.2 joerg pte = (pd_entry_t *) (addr + KERNBASE);
756 1.3.2.2 joerg pte += pl1_pi(page);
757 1.3.2.2 joerg __PRINTK(("*pde 0x%lx addr 0x%lx pte 0x%lx\n",
758 1.3.2.2 joerg pde[pl2_pi(page)], addr, pte));
759 1.3.2.2 joerg while (page < old_pgd + (old_count * PAGE_SIZE) && page < map_end) {
760 1.3.2.2 joerg addr = xpmap_ptom(((paddr_t) pte) - KERNBASE);
761 1.3.2.2 joerg xpq_queue_pte_update((pt_entry_t *) addr, *pte | PG_RW);
762 1.3.2.2 joerg page += PAGE_SIZE;
763 1.3.2.2 joerg /*
764 1.3.2.2 joerg * Our ptes are contiguous
765 1.3.2.2 joerg * so it's safe to just "++" here
766 1.3.2.2 joerg */
767 1.3.2.2 joerg pte++;
768 1.3.2.2 joerg }
769 1.3.2.2 joerg xpq_flush_queue();
770 1.3.2.2 joerg }
771 1.3.2.2 joerg
772 1.3.2.2 joerg
773 1.3.2.2 joerg void
774 1.3.2.2 joerg xen_set_user_pgd(paddr_t page)
775 1.3.2.2 joerg {
776 1.3.2.2 joerg struct mmuext_op op;
777 1.3.2.2 joerg int s = splvm();
778 1.3.2.2 joerg
779 1.3.2.2 joerg xpq_flush_queue();
780 1.3.2.2 joerg op.cmd = MMUEXT_NEW_USER_BASEPTR;
781 1.3.2.2 joerg op.arg1.mfn = xpmap_phys_to_machine_mapping[page >> PAGE_SHIFT];
782 1.3.2.2 joerg if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
783 1.3.2.2 joerg panic("xen_set_user_pgd: failed to install new user page"
784 1.3.2.2 joerg " directory %lx", page);
785 1.3.2.2 joerg splx(s);
786 1.3.2.2 joerg }
787 1.3.2.2 joerg
788 1.3.2.2 joerg /*
789 1.3.2.2 joerg * Bootstrap helper functions
790 1.3.2.2 joerg */
791 1.3.2.2 joerg
792 1.3.2.2 joerg /*
793 1.3.2.2 joerg * Mark a page readonly
794 1.3.2.2 joerg * XXX: assuming vaddr = paddr + KERNBASE
795 1.3.2.2 joerg */
796 1.3.2.2 joerg
797 1.3.2.2 joerg static void
798 1.3.2.2 joerg xen_bt_set_readonly (vaddr_t page)
799 1.3.2.2 joerg {
800 1.3.2.2 joerg pt_entry_t entry;
801 1.3.2.2 joerg
802 1.3.2.2 joerg entry = xpmap_ptom_masked(page - KERNBASE);
803 1.3.2.2 joerg entry |= PG_u | PG_V;
804 1.3.2.2 joerg
805 1.3.2.2 joerg HYPERVISOR_update_va_mapping (page, entry, UVMF_INVLPG);
806 1.3.2.2 joerg }
807 1.3.2.2 joerg #endif /* x86_64 */
808