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