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