x86_xpmap.c revision 1.29 1 /* $NetBSD: x86_xpmap.c,v 1.29 2011/08/10 09:50:37 cherry 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.29 2011/08/10 09:50:37 cherry 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 #include <sys/simplelock.h>
81
82 #include <uvm/uvm.h>
83
84 #include <machine/pmap.h>
85 #include <machine/gdt.h>
86 #include <xen/xenfunc.h>
87
88 #include <dev/isa/isareg.h>
89 #include <machine/isa_machdep.h>
90
91 #undef XENDEBUG
92 /* #define XENDEBUG_SYNC */
93 /* #define XENDEBUG_LOW */
94
95 #ifdef XENDEBUG
96 #define XENPRINTF(x) printf x
97 #define XENPRINTK(x) printk x
98 #define XENPRINTK2(x) /* printk x */
99
100 static char XBUF[256];
101 #else
102 #define XENPRINTF(x)
103 #define XENPRINTK(x)
104 #define XENPRINTK2(x)
105 #endif
106 #define PRINTF(x) printf x
107 #define PRINTK(x) printk x
108
109 /* on x86_64 kernel runs in ring 3 */
110 #ifdef __x86_64__
111 #define PG_k PG_u
112 #else
113 #define PG_k 0
114 #endif
115
116 volatile shared_info_t *HYPERVISOR_shared_info;
117 /* Xen requires the start_info struct to be page aligned */
118 union start_info_union start_info_union __aligned(PAGE_SIZE);
119 unsigned long *xpmap_phys_to_machine_mapping;
120
121 void xen_failsafe_handler(void);
122
123 #define HYPERVISOR_mmu_update_self(req, count, success_count) \
124 HYPERVISOR_mmu_update((req), (count), (success_count), DOMID_SELF)
125
126 void
127 xen_failsafe_handler(void)
128 {
129
130 panic("xen_failsafe_handler called!\n");
131 }
132
133
134 void
135 xen_set_ldt(vaddr_t base, uint32_t entries)
136 {
137 vaddr_t va;
138 vaddr_t end;
139 pt_entry_t *ptp;
140 int s;
141
142 #ifdef __x86_64__
143 end = base + (entries << 3);
144 #else
145 end = base + entries * sizeof(union descriptor);
146 #endif
147
148 for (va = base; va < end; va += PAGE_SIZE) {
149 KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
150 ptp = kvtopte(va);
151 XENPRINTF(("xen_set_ldt %#" PRIxVADDR " %d %p\n",
152 base, entries, ptp));
153 pmap_pte_clearbits(ptp, PG_RW);
154 }
155 s = splvm();
156 xpq_queue_lock();
157 xpq_queue_set_ldt(base, entries);
158 xpq_queue_unlock();
159 splx(s);
160 }
161
162 #ifdef XENDEBUG
163 void xpq_debug_dump(void);
164 #endif
165
166 #define XPQUEUE_SIZE 2048
167 static mmu_update_t xpq_queue[XPQUEUE_SIZE];
168 static int xpq_idx = 0;
169 static struct simplelock xpq_lock = SIMPLELOCK_INITIALIZER;
170
171 void
172 xpq_queue_lock(void)
173 {
174 simple_lock(&xpq_lock);
175 }
176
177 void
178 xpq_queue_unlock(void)
179 {
180 simple_unlock(&xpq_lock);
181 }
182
183 /* Must be called with xpq_lock held */
184 void
185 xpq_flush_queue(void)
186 {
187 int i, ok, ret;
188
189 KASSERT(simple_lock_held(&xpq_lock));
190 XENPRINTK2(("flush queue %p entries %d\n", xpq_queue, xpq_idx));
191 for (i = 0; i < xpq_idx; i++)
192 XENPRINTK2(("%d: 0x%08" PRIx64 " 0x%08" PRIx64 "\n", i,
193 xpq_queue[i].ptr, xpq_queue[i].val));
194
195 ret = HYPERVISOR_mmu_update_self(xpq_queue, xpq_idx, &ok);
196
197 if (xpq_idx != 0 && ret < 0) {
198 printf("xpq_flush_queue: %d entries (%d successful)\n",
199 xpq_idx, ok);
200 for (i = 0; i < xpq_idx; i++)
201 printf("0x%016" PRIx64 ": 0x%016" PRIx64 "\n",
202 xpq_queue[i].ptr, xpq_queue[i].val);
203 panic("HYPERVISOR_mmu_update failed, ret: %d\n", ret);
204 }
205 xpq_idx = 0;
206 }
207
208 /* Must be called with xpq_lock held */
209 static inline void
210 xpq_increment_idx(void)
211 {
212
213 KASSERT(simple_lock_held(&xpq_lock));
214 xpq_idx++;
215 if (__predict_false(xpq_idx == XPQUEUE_SIZE))
216 xpq_flush_queue();
217 }
218
219 void
220 xpq_queue_machphys_update(paddr_t ma, paddr_t pa)
221 {
222 XENPRINTK2(("xpq_queue_machphys_update ma=0x%" PRIx64 " pa=0x%" PRIx64
223 "\n", (int64_t)ma, (int64_t)pa));
224 KASSERT(simple_lock_held(&xpq_lock));
225 xpq_queue[xpq_idx].ptr = ma | MMU_MACHPHYS_UPDATE;
226 xpq_queue[xpq_idx].val = (pa - XPMAP_OFFSET) >> PAGE_SHIFT;
227 xpq_increment_idx();
228 #ifdef XENDEBUG_SYNC
229 xpq_flush_queue();
230 #endif
231 }
232
233 void
234 xpq_queue_pte_update(paddr_t ptr, pt_entry_t val)
235 {
236
237 KASSERT((ptr & 3) == 0);
238 KASSERT(simple_lock_held(&xpq_lock));
239 xpq_queue[xpq_idx].ptr = (paddr_t)ptr | MMU_NORMAL_PT_UPDATE;
240 xpq_queue[xpq_idx].val = val;
241 xpq_increment_idx();
242 #ifdef XENDEBUG_SYNC
243 xpq_flush_queue();
244 #endif
245 }
246
247 void
248 xpq_queue_pt_switch(paddr_t pa)
249 {
250 struct mmuext_op op;
251 KASSERT(simple_lock_held(&xpq_lock));
252 xpq_flush_queue();
253
254 XENPRINTK2(("xpq_queue_pt_switch: 0x%" PRIx64 " 0x%" PRIx64 "\n",
255 (int64_t)pa, (int64_t)pa));
256 op.cmd = MMUEXT_NEW_BASEPTR;
257 op.arg1.mfn = pa >> PAGE_SHIFT;
258 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
259 panic("xpq_queue_pt_switch");
260 }
261
262 void
263 xpq_queue_pin_table(paddr_t pa, int lvl)
264 {
265 struct mmuext_op op;
266
267 KASSERT(simple_lock_held(&xpq_lock));
268 xpq_flush_queue();
269
270 XENPRINTK2(("xpq_queue_pin_l%d_table: %#" PRIxPADDR "\n",
271 lvl + 1, pa));
272
273 op.arg1.mfn = pa >> PAGE_SHIFT;
274 op.cmd = lvl;
275
276 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
277 panic("xpq_queue_pin_table");
278 }
279
280 void
281 xpq_queue_unpin_table(paddr_t pa)
282 {
283 struct mmuext_op op;
284
285 KASSERT(simple_lock_held(&xpq_lock));
286 xpq_flush_queue();
287
288 XENPRINTK2(("xpq_queue_unpin_table: %#" PRIxPADDR "\n", pa));
289 op.arg1.mfn = pa >> PAGE_SHIFT;
290 op.cmd = MMUEXT_UNPIN_TABLE;
291 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
292 panic("xpq_queue_unpin_table");
293 }
294
295 void
296 xpq_queue_set_ldt(vaddr_t va, uint32_t entries)
297 {
298 struct mmuext_op op;
299
300 KASSERT(simple_lock_held(&xpq_lock));
301 xpq_flush_queue();
302
303 XENPRINTK2(("xpq_queue_set_ldt\n"));
304 KASSERT(va == (va & ~PAGE_MASK));
305 op.cmd = MMUEXT_SET_LDT;
306 op.arg1.linear_addr = va;
307 op.arg2.nr_ents = entries;
308 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
309 panic("xpq_queue_set_ldt");
310 }
311
312 void
313 xpq_queue_tlb_flush(void)
314 {
315 struct mmuext_op op;
316
317 KASSERT(simple_lock_held(&xpq_lock));
318 xpq_flush_queue();
319
320 XENPRINTK2(("xpq_queue_tlb_flush\n"));
321 op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
322 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
323 panic("xpq_queue_tlb_flush");
324 }
325
326 void
327 xpq_flush_cache(void)
328 {
329 struct mmuext_op op;
330 int s = splvm(), err;
331
332 xpq_queue_lock();
333 xpq_flush_queue();
334
335 XENPRINTK2(("xpq_queue_flush_cache\n"));
336 op.cmd = MMUEXT_FLUSH_CACHE;
337 if ((err = HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF)) < 0)
338 printf("errno == %d\n", err);
339 panic("xpq_flush_cache");
340 xpq_queue_unlock();
341 splx(s); /* XXX: removeme */
342 }
343
344 void
345 xpq_queue_invlpg(vaddr_t va)
346 {
347 struct mmuext_op op;
348 KASSERT(simple_lock_held(&xpq_lock));
349 xpq_flush_queue();
350
351 XENPRINTK2(("xpq_queue_invlpg %#" PRIxVADDR "\n", va));
352 op.cmd = MMUEXT_INVLPG_LOCAL;
353 op.arg1.linear_addr = (va & ~PAGE_MASK);
354 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
355 panic("xpq_queue_invlpg");
356 }
357
358 void
359 xen_mcast_invlpg(vaddr_t va, uint32_t cpumask)
360 {
361 mmuext_op_t op;
362
363 KASSERT(simple_lock_held(&xpq_lock));
364
365 /* Flush pending page updates */
366 xpq_flush_queue();
367
368 op.cmd = MMUEXT_INVLPG_MULTI;
369 op.arg1.linear_addr = va;
370 op.arg2.vcpumask = &cpumask;
371
372 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
373 panic("xpq_queue_invlpg_all");
374 }
375
376 return;
377 }
378
379 void
380 xen_bcast_invlpg(vaddr_t va)
381 {
382 mmuext_op_t op;
383
384 /* Flush pending page updates */
385 KASSERT(simple_lock_held(&xpq_lock));
386 xpq_flush_queue();
387
388 op.cmd = MMUEXT_INVLPG_ALL;
389 op.arg1.linear_addr = va;
390
391 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
392 panic("xpq_queue_invlpg_all");
393 }
394
395 return;
396 }
397
398 /* This is a synchronous call. */
399 void
400 xen_mcast_tlbflush(uint32_t cpumask)
401 {
402 mmuext_op_t op;
403
404 /* Flush pending page updates */
405 KASSERT(simple_lock_held(&xpq_lock));
406 xpq_flush_queue();
407
408 op.cmd = MMUEXT_TLB_FLUSH_MULTI;
409 op.arg2.vcpumask = &cpumask;
410
411 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
412 panic("xpq_queue_invlpg_all");
413 }
414
415 return;
416 }
417
418 /* This is a synchronous call. */
419 void
420 xen_bcast_tlbflush(void)
421 {
422 mmuext_op_t op;
423
424 /* Flush pending page updates */
425 KASSERT(simple_lock_held(&xpq_lock));
426 xpq_flush_queue();
427
428 op.cmd = MMUEXT_TLB_FLUSH_ALL;
429
430 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
431 panic("xpq_queue_invlpg_all");
432 }
433
434 return;
435 }
436
437 /* This is a synchronous call. */
438 void
439 xen_vcpu_mcast_invlpg(vaddr_t sva, vaddr_t eva, uint32_t cpumask)
440 {
441 KASSERT(eva > sva);
442
443 /* Flush pending page updates */
444 KASSERT(simple_lock_held(&xpq_lock));
445 xpq_flush_queue();
446
447 /* Align to nearest page boundary */
448 sva &= ~PAGE_MASK;
449 eva &= ~PAGE_MASK;
450
451 for ( ; sva <= eva; sva += PAGE_SIZE) {
452 xen_mcast_invlpg(sva, cpumask);
453 }
454
455 return;
456 }
457
458 /* This is a synchronous call. */
459 void
460 xen_vcpu_bcast_invlpg(vaddr_t sva, vaddr_t eva)
461 {
462 KASSERT(eva > sva);
463
464 /* Flush pending page updates */
465 KASSERT(simple_lock_held(&xpq_lock));
466 xpq_flush_queue();
467
468 /* Align to nearest page boundary */
469 sva &= ~PAGE_MASK;
470 eva &= ~PAGE_MASK;
471
472 for ( ; sva <= eva; sva += PAGE_SIZE) {
473 xen_bcast_invlpg(sva);
474 }
475
476 return;
477 }
478
479 int
480 xpq_update_foreign(paddr_t ptr, pt_entry_t val, int dom)
481 {
482 mmu_update_t op;
483 int ok;
484
485 KASSERT(simple_lock_held(&xpq_lock));
486 xpq_flush_queue();
487
488 op.ptr = ptr;
489 op.val = val;
490 if (HYPERVISOR_mmu_update(&op, 1, &ok, dom) < 0)
491 return EFAULT;
492 return (0);
493 }
494
495 #ifdef XENDEBUG
496 void
497 xpq_debug_dump(void)
498 {
499 int i;
500
501 XENPRINTK2(("idx: %d\n", xpq_idx));
502 for (i = 0; i < xpq_idx; i++) {
503 snprintf(XBUF, sizeof(XBUF), "%" PRIx64 " %08" PRIx64,
504 xpq_queue[i].ptr, xpq_queue[i].val);
505 if (++i < xpq_idx)
506 snprintf(XBUF + strlen(XBUF),
507 sizeof(XBUF) - strlen(XBUF),
508 "%" PRIx64 " %08" PRIx64,
509 xpq_queue[i].ptr, xpq_queue[i].val);
510 if (++i < xpq_idx)
511 snprintf(XBUF + strlen(XBUF),
512 sizeof(XBUF) - strlen(XBUF),
513 "%" PRIx64 " %08" PRIx64,
514 xpq_queue[i].ptr, xpq_queue[i].val);
515 if (++i < xpq_idx)
516 snprintf(XBUF + strlen(XBUF),
517 sizeof(XBUF) - strlen(XBUF),
518 "%" PRIx64 " %08" PRIx64,
519 xpq_queue[i].ptr, xpq_queue[i].val);
520 XENPRINTK2(("%d: %s\n", xpq_idx, XBUF));
521 }
522 }
523 #endif
524
525
526 extern volatile struct xencons_interface *xencons_interface; /* XXX */
527 extern struct xenstore_domain_interface *xenstore_interface; /* XXX */
528
529 static void xen_bt_set_readonly (vaddr_t);
530 static void xen_bootstrap_tables (vaddr_t, vaddr_t, int, int, int);
531
532 /* How many PDEs ? */
533 #if L2_SLOT_KERNBASE > 0
534 #define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
535 #else
536 #define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1)
537 #endif
538
539 /*
540 * Construct and switch to new pagetables
541 * first_avail is the first vaddr we can use after
542 * we get rid of Xen pagetables
543 */
544
545 vaddr_t xen_pmap_bootstrap (void);
546
547 /*
548 * Function to get rid of Xen bootstrap tables
549 */
550
551 /* How many PDP do we need: */
552 #ifdef PAE
553 /*
554 * For PAE, we consider a single contigous L2 "superpage" of 4 pages,
555 * all of them mapped by the L3 page. We also need a shadow page
556 * for L3[3].
557 */
558 static const int l2_4_count = 6;
559 #else
560 static const int l2_4_count = PTP_LEVELS - 1;
561 #endif
562
563 vaddr_t
564 xen_pmap_bootstrap(void)
565 {
566 int count, oldcount;
567 long mapsize;
568 vaddr_t bootstrap_tables, init_tables;
569
570 xpmap_phys_to_machine_mapping =
571 (unsigned long *)xen_start_info.mfn_list;
572 init_tables = xen_start_info.pt_base;
573 __PRINTK(("xen_arch_pmap_bootstrap init_tables=0x%lx\n", init_tables));
574
575 /* Space after Xen boostrap tables should be free */
576 bootstrap_tables = xen_start_info.pt_base +
577 (xen_start_info.nr_pt_frames * PAGE_SIZE);
578
579 /*
580 * Calculate how many space we need
581 * first everything mapped before the Xen bootstrap tables
582 */
583 mapsize = init_tables - KERNTEXTOFF;
584 /* after the tables we'll have:
585 * - UAREA
586 * - dummy user PGD (x86_64)
587 * - HYPERVISOR_shared_info
588 * - ISA I/O mem (if needed)
589 */
590 mapsize += UPAGES * NBPG;
591 #ifdef __x86_64__
592 mapsize += NBPG;
593 #endif
594 mapsize += NBPG;
595
596 #ifdef DOM0OPS
597 if (xendomain_is_dom0()) {
598 /* space for ISA I/O mem */
599 mapsize += IOM_SIZE;
600 }
601 #endif
602 /* at this point mapsize doens't include the table size */
603
604 #ifdef __x86_64__
605 count = TABLE_L2_ENTRIES;
606 #else
607 count = (mapsize + (NBPD_L2 -1)) >> L2_SHIFT;
608 #endif /* __x86_64__ */
609
610 /* now compute how many L2 pages we need exactly */
611 XENPRINTK(("bootstrap_final mapsize 0x%lx count %d\n", mapsize, count));
612 while (mapsize + (count + l2_4_count) * PAGE_SIZE + KERNTEXTOFF >
613 ((long)count << L2_SHIFT) + KERNBASE) {
614 count++;
615 }
616 #ifndef __x86_64__
617 /*
618 * one more L2 page: we'll alocate several pages after kva_start
619 * in pmap_bootstrap() before pmap_growkernel(), which have not been
620 * counted here. It's not a big issue to allocate one more L2 as
621 * pmap_growkernel() will be called anyway.
622 */
623 count++;
624 nkptp[1] = count;
625 #endif
626
627 /*
628 * install bootstrap pages. We may need more L2 pages than will
629 * have the final table here, as it's installed after the final table
630 */
631 oldcount = count;
632
633 bootstrap_again:
634 XENPRINTK(("bootstrap_again oldcount %d\n", oldcount));
635 /*
636 * Xen space we'll reclaim may not be enough for our new page tables,
637 * move bootstrap tables if necessary
638 */
639 if (bootstrap_tables < init_tables + ((count + l2_4_count) * PAGE_SIZE))
640 bootstrap_tables = init_tables +
641 ((count + l2_4_count) * PAGE_SIZE);
642 /* make sure we have enough to map the bootstrap_tables */
643 if (bootstrap_tables + ((oldcount + l2_4_count) * PAGE_SIZE) >
644 ((long)oldcount << L2_SHIFT) + KERNBASE) {
645 oldcount++;
646 goto bootstrap_again;
647 }
648
649 /* Create temporary tables */
650 xen_bootstrap_tables(xen_start_info.pt_base, bootstrap_tables,
651 xen_start_info.nr_pt_frames, oldcount, 0);
652
653 /* Create final tables */
654 xen_bootstrap_tables(bootstrap_tables, init_tables,
655 oldcount + l2_4_count, count, 1);
656
657 /* zero out free space after tables */
658 memset((void *)(init_tables + ((count + l2_4_count) * PAGE_SIZE)), 0,
659 (UPAGES + 1) * NBPG);
660
661 /* Finally, flush TLB. */
662 xpq_queue_tlb_flush();
663
664 return (init_tables + ((count + l2_4_count) * PAGE_SIZE));
665 }
666
667
668 /*
669 * Build a new table and switch to it
670 * old_count is # of old tables (including PGD, PDTPE and PDE)
671 * new_count is # of new tables (PTE only)
672 * we assume areas don't overlap
673 */
674 static void
675 xen_bootstrap_tables (vaddr_t old_pgd, vaddr_t new_pgd,
676 int old_count, int new_count, int final)
677 {
678 pd_entry_t *pdtpe, *pde, *pte;
679 pd_entry_t *cur_pgd, *bt_pgd;
680 paddr_t addr;
681 vaddr_t page, avail, text_end, map_end;
682 int i;
683 extern char __data_start;
684
685 __PRINTK(("xen_bootstrap_tables(%#" PRIxVADDR ", %#" PRIxVADDR ","
686 " %d, %d)\n",
687 old_pgd, new_pgd, old_count, new_count));
688 text_end = ((vaddr_t)&__data_start) & ~PAGE_MASK;
689 /*
690 * size of R/W area after kernel text:
691 * xencons_interface (if present)
692 * xenstore_interface (if present)
693 * table pages (new_count + l2_4_count entries)
694 * extra mappings (only when final is true):
695 * UAREA
696 * dummy user PGD (x86_64 only)/gdt page (i386 only)
697 * HYPERVISOR_shared_info
698 * ISA I/O mem (if needed)
699 */
700 map_end = new_pgd + ((new_count + l2_4_count) * NBPG);
701 if (final) {
702 map_end += (UPAGES + 1) * NBPG;
703 HYPERVISOR_shared_info = (shared_info_t *)map_end;
704 map_end += NBPG;
705 }
706 /*
707 * we always set atdevbase, as it's used by init386 to find the first
708 * available VA. map_end is updated only if we are dom0, so
709 * atdevbase -> atdevbase + IOM_SIZE will be mapped only in
710 * this case.
711 */
712 if (final)
713 atdevbase = map_end;
714 #ifdef DOM0OPS
715 if (final && xendomain_is_dom0()) {
716 /* ISA I/O mem */
717 map_end += IOM_SIZE;
718 }
719 #endif /* DOM0OPS */
720
721 __PRINTK(("xen_bootstrap_tables text_end 0x%lx map_end 0x%lx\n",
722 text_end, map_end));
723 __PRINTK(("console %#lx ", xen_start_info.console_mfn));
724 __PRINTK(("xenstore %#" PRIx32 "\n", xen_start_info.store_mfn));
725
726 /*
727 * Create bootstrap page tables
728 * What we need:
729 * - a PGD (level 4)
730 * - a PDTPE (level 3)
731 * - a PDE (level2)
732 * - some PTEs (level 1)
733 */
734
735 cur_pgd = (pd_entry_t *) old_pgd;
736 bt_pgd = (pd_entry_t *) new_pgd;
737 memset (bt_pgd, 0, PAGE_SIZE);
738 avail = new_pgd + PAGE_SIZE;
739 #if PTP_LEVELS > 3
740 /* Install level 3 */
741 pdtpe = (pd_entry_t *) avail;
742 memset (pdtpe, 0, PAGE_SIZE);
743 avail += PAGE_SIZE;
744
745 addr = ((u_long) pdtpe) - KERNBASE;
746 bt_pgd[pl4_pi(KERNTEXTOFF)] =
747 xpmap_ptom_masked(addr) | PG_k | PG_RW | PG_V;
748
749 __PRINTK(("L3 va %#lx pa %#" PRIxPADDR " entry %#" PRIxPADDR
750 " -> L4[%#x]\n",
751 pdtpe, addr, bt_pgd[pl4_pi(KERNTEXTOFF)], pl4_pi(KERNTEXTOFF)));
752 #else
753 pdtpe = bt_pgd;
754 #endif /* PTP_LEVELS > 3 */
755
756 #if PTP_LEVELS > 2
757 /* Level 2 */
758 pde = (pd_entry_t *) avail;
759 memset(pde, 0, PAGE_SIZE);
760 avail += PAGE_SIZE;
761
762 addr = ((u_long) pde) - KERNBASE;
763 pdtpe[pl3_pi(KERNTEXTOFF)] =
764 xpmap_ptom_masked(addr) | PG_k | PG_V | PG_RW;
765 __PRINTK(("L2 va %#lx pa %#" PRIxPADDR " entry %#" PRIxPADDR
766 " -> L3[%#x]\n",
767 pde, addr, pdtpe[pl3_pi(KERNTEXTOFF)], pl3_pi(KERNTEXTOFF)));
768 #elif defined(PAE)
769 /* our PAE-style level 2: 5 contigous pages (4 L2 + 1 shadow) */
770 pde = (pd_entry_t *) avail;
771 memset(pde, 0, PAGE_SIZE * 5);
772 avail += PAGE_SIZE * 5;
773 addr = ((u_long) pde) - KERNBASE;
774 /*
775 * enter L2 pages in the L3.
776 * The real L2 kernel PD will be the last one (so that
777 * pde[L2_SLOT_KERN] always point to the shadow).
778 */
779 for (i = 0; i < 3; i++, addr += PAGE_SIZE) {
780 /*
781 * Xen doesn't want R/W mappings in L3 entries, it'll add it
782 * itself.
783 */
784 pdtpe[i] = xpmap_ptom_masked(addr) | PG_k | PG_V;
785 __PRINTK(("L2 va %#lx pa %#" PRIxPADDR " entry %#" PRIxPADDR
786 " -> L3[%#x]\n",
787 (vaddr_t)pde + PAGE_SIZE * i, addr, pdtpe[i], i));
788 }
789 addr += PAGE_SIZE;
790 pdtpe[3] = xpmap_ptom_masked(addr) | PG_k | PG_V;
791 __PRINTK(("L2 va %#lx pa %#" PRIxPADDR " entry %#" PRIxPADDR
792 " -> L3[%#x]\n",
793 (vaddr_t)pde + PAGE_SIZE * 4, addr, pdtpe[3], 3));
794
795 #else /* PAE */
796 pde = bt_pgd;
797 #endif /* PTP_LEVELS > 2 */
798
799 /* Level 1 */
800 page = KERNTEXTOFF;
801 for (i = 0; i < new_count; i ++) {
802 vaddr_t cur_page = page;
803
804 pte = (pd_entry_t *) avail;
805 avail += PAGE_SIZE;
806
807 memset(pte, 0, PAGE_SIZE);
808 while (pl2_pi(page) == pl2_pi (cur_page)) {
809 if (page >= map_end) {
810 /* not mapped at all */
811 pte[pl1_pi(page)] = 0;
812 page += PAGE_SIZE;
813 continue;
814 }
815 pte[pl1_pi(page)] = xpmap_ptom_masked(page - KERNBASE);
816 if (page == (vaddr_t)HYPERVISOR_shared_info) {
817 pte[pl1_pi(page)] = xen_start_info.shared_info;
818 __PRINTK(("HYPERVISOR_shared_info "
819 "va %#lx pte %#" PRIxPADDR "\n",
820 HYPERVISOR_shared_info, pte[pl1_pi(page)]));
821 }
822 if ((xpmap_ptom_masked(page - KERNBASE) >> PAGE_SHIFT)
823 == xen_start_info.console.domU.mfn) {
824 xencons_interface = (void *)page;
825 pte[pl1_pi(page)] = xen_start_info.console_mfn;
826 pte[pl1_pi(page)] <<= PAGE_SHIFT;
827 __PRINTK(("xencons_interface "
828 "va %#lx pte %#" PRIxPADDR "\n",
829 xencons_interface, pte[pl1_pi(page)]));
830 }
831 if ((xpmap_ptom_masked(page - KERNBASE) >> PAGE_SHIFT)
832 == xen_start_info.store_mfn) {
833 xenstore_interface = (void *)page;
834 pte[pl1_pi(page)] = xen_start_info.store_mfn;
835 pte[pl1_pi(page)] <<= PAGE_SHIFT;
836 __PRINTK(("xenstore_interface "
837 "va %#lx pte %#" PRIxPADDR "\n",
838 xenstore_interface, pte[pl1_pi(page)]));
839 }
840 #ifdef DOM0OPS
841 if (page >= (vaddr_t)atdevbase &&
842 page < (vaddr_t)atdevbase + IOM_SIZE) {
843 pte[pl1_pi(page)] =
844 IOM_BEGIN + (page - (vaddr_t)atdevbase);
845 }
846 #endif
847 pte[pl1_pi(page)] |= PG_k | PG_V;
848 if (page < text_end) {
849 /* map kernel text RO */
850 pte[pl1_pi(page)] |= 0;
851 } else if (page >= old_pgd
852 && page < old_pgd + (old_count * PAGE_SIZE)) {
853 /* map old page tables RO */
854 pte[pl1_pi(page)] |= 0;
855 } else if (page >= new_pgd &&
856 page < new_pgd + ((new_count + l2_4_count) * PAGE_SIZE)) {
857 /* map new page tables RO */
858 pte[pl1_pi(page)] |= 0;
859 } else {
860 /* map page RW */
861 pte[pl1_pi(page)] |= PG_RW;
862 }
863
864 if ((page >= old_pgd && page < old_pgd + (old_count * PAGE_SIZE))
865 || page >= new_pgd) {
866 __PRINTK(("va %#lx pa %#lx "
867 "entry 0x%" PRIxPADDR " -> L1[%#x]\n",
868 page, page - KERNBASE,
869 pte[pl1_pi(page)], pl1_pi(page)));
870 }
871 page += PAGE_SIZE;
872 }
873
874 addr = ((u_long) pte) - KERNBASE;
875 pde[pl2_pi(cur_page)] =
876 xpmap_ptom_masked(addr) | PG_k | PG_RW | PG_V;
877 __PRINTK(("L1 va %#lx pa %#" PRIxPADDR " entry %#" PRIxPADDR
878 " -> L2[%#x]\n",
879 pte, addr, pde[pl2_pi(cur_page)], pl2_pi(cur_page)));
880 /* Mark readonly */
881 xen_bt_set_readonly((vaddr_t) pte);
882 }
883
884 /* Install recursive page tables mapping */
885 #ifdef PAE
886 /*
887 * we need a shadow page for the kernel's L2 page
888 * The real L2 kernel PD will be the last one (so that
889 * pde[L2_SLOT_KERN] always point to the shadow.
890 */
891 memcpy(&pde[L2_SLOT_KERN + NPDPG], &pde[L2_SLOT_KERN], PAGE_SIZE);
892 pmap_kl2pd = &pde[L2_SLOT_KERN + NPDPG];
893 pmap_kl2paddr = (u_long)pmap_kl2pd - KERNBASE;
894
895 /*
896 * We don't enter a recursive entry from the L3 PD. Instead,
897 * we enter the first 4 L2 pages, which includes the kernel's L2
898 * shadow. But we have to entrer the shadow after switching
899 * %cr3, or Xen will refcount some PTE with the wrong type.
900 */
901 addr = (u_long)pde - KERNBASE;
902 for (i = 0; i < 3; i++, addr += PAGE_SIZE) {
903 pde[PDIR_SLOT_PTE + i] = xpmap_ptom_masked(addr) | PG_k | PG_V;
904 __PRINTK(("pde[%d] va %#" PRIxVADDR " pa %#" PRIxPADDR
905 " entry %#" PRIxPADDR "\n",
906 (int)(PDIR_SLOT_PTE + i), pde + PAGE_SIZE * i,
907 addr, pde[PDIR_SLOT_PTE + i]));
908 }
909 #if 0
910 addr += PAGE_SIZE; /* point to shadow L2 */
911 pde[PDIR_SLOT_PTE + 3] = xpmap_ptom_masked(addr) | PG_k | PG_V;
912 __PRINTK(("pde[%d] va 0x%lx pa 0x%lx entry 0x%" PRIx64 "\n",
913 (int)(PDIR_SLOT_PTE + 3), pde + PAGE_SIZE * 4, (long)addr,
914 (int64_t)pde[PDIR_SLOT_PTE + 3]));
915 #endif
916 /* Mark tables RO, and pin the kernel's shadow as L2 */
917 addr = (u_long)pde - KERNBASE;
918 for (i = 0; i < 5; i++, addr += PAGE_SIZE) {
919 xen_bt_set_readonly(((vaddr_t)pde) + PAGE_SIZE * i);
920 if (i == 2 || i == 3)
921 continue;
922 #if 0
923 __PRINTK(("pin L2 %d addr 0x%" PRIx64 "\n", i, (int64_t)addr));
924 xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
925 #endif
926 }
927 if (final) {
928 addr = (u_long)pde - KERNBASE + 3 * PAGE_SIZE;
929 __PRINTK(("pin L2 %d addr %#" PRIxPADDR "\n", 2, addr));
930 xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
931 }
932 #if 0
933 addr = (u_long)pde - KERNBASE + 2 * PAGE_SIZE;
934 __PRINTK(("pin L2 %d addr 0x%" PRIx64 "\n", 2, (int64_t)addr));
935 xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
936 #endif
937 #else /* PAE */
938 /* recursive entry in higher-level PD */
939 bt_pgd[PDIR_SLOT_PTE] =
940 xpmap_ptom_masked(new_pgd - KERNBASE) | PG_k | PG_V;
941 __PRINTK(("bt_pgd[PDIR_SLOT_PTE] va %#" PRIxVADDR " pa %#" PRIxPADDR
942 " entry %#" PRIxPADDR "\n", new_pgd, (paddr_t)new_pgd - KERNBASE,
943 bt_pgd[PDIR_SLOT_PTE]));
944 /* Mark tables RO */
945 xen_bt_set_readonly((vaddr_t) pde);
946 #endif
947 #if PTP_LEVELS > 2 || defined(PAE)
948 xen_bt_set_readonly((vaddr_t) pdtpe);
949 #endif
950 #if PTP_LEVELS > 3
951 xen_bt_set_readonly(new_pgd);
952 #endif
953 /* Pin the PGD */
954 __PRINTK(("pin PGD: %"PRIxVADDR"\n", new_pgd - KERNBASE));
955 #ifdef __x86_64__
956 xpq_queue_pin_l4_table(xpmap_ptom_masked(new_pgd - KERNBASE));
957 #elif PAE
958 xpq_queue_pin_l3_table(xpmap_ptom_masked(new_pgd - KERNBASE));
959 #else
960 xpq_queue_pin_l2_table(xpmap_ptom_masked(new_pgd - KERNBASE));
961 #endif
962
963 /* Save phys. addr of PDP, for libkvm. */
964 #ifdef PAE
965 PDPpaddr = (u_long)pde - KERNBASE; /* PDP is the L2 with PAE */
966 #else
967 PDPpaddr = (u_long)new_pgd - KERNBASE;
968 #endif
969
970 /* Switch to new tables */
971 __PRINTK(("switch to PGD\n"));
972 xpq_queue_pt_switch(xpmap_ptom_masked(new_pgd - KERNBASE));
973 __PRINTK(("bt_pgd[PDIR_SLOT_PTE] now entry %#" PRIxPADDR "\n",
974 bt_pgd[PDIR_SLOT_PTE]));
975
976 #ifdef PAE
977 if (final) {
978 /* save the address of the L3 page */
979 cpu_info_primary.ci_pae_l3_pdir = pdtpe;
980 cpu_info_primary.ci_pae_l3_pdirpa = (new_pgd - KERNBASE);
981
982 /* now enter kernel's PTE mappings */
983 addr = (u_long)pde - KERNBASE + PAGE_SIZE * 3;
984 xpq_queue_pte_update(
985 xpmap_ptom(((vaddr_t)&pde[PDIR_SLOT_PTE + 3]) - KERNBASE),
986 xpmap_ptom_masked(addr) | PG_k | PG_V);
987 xpq_flush_queue();
988 }
989 #endif
990
991 /* Now we can safely reclaim space taken by old tables */
992
993 __PRINTK(("unpin old PGD\n"));
994 /* Unpin old PGD */
995 xpq_queue_unpin_table(xpmap_ptom_masked(old_pgd - KERNBASE));
996 /* Mark old tables RW */
997 page = old_pgd;
998 addr = (paddr_t) pde[pl2_pi(page)] & PG_FRAME;
999 addr = xpmap_mtop(addr);
1000 pte = (pd_entry_t *) ((u_long)addr + KERNBASE);
1001 pte += pl1_pi(page);
1002 __PRINTK(("*pde %#" PRIxPADDR " addr %#" PRIxPADDR " pte %#lx\n",
1003 pde[pl2_pi(page)], addr, (long)pte));
1004 while (page < old_pgd + (old_count * PAGE_SIZE) && page < map_end) {
1005 addr = xpmap_ptom(((u_long) pte) - KERNBASE);
1006 XENPRINTK(("addr %#" PRIxPADDR " pte %#lx "
1007 "*pte %#" PRIxPADDR "\n",
1008 addr, (long)pte, *pte));
1009 xpq_queue_pte_update(addr, *pte | PG_RW);
1010 page += PAGE_SIZE;
1011 /*
1012 * Our ptes are contiguous
1013 * so it's safe to just "++" here
1014 */
1015 pte++;
1016 }
1017 xpq_flush_queue();
1018 }
1019
1020
1021 /*
1022 * Bootstrap helper functions
1023 */
1024
1025 /*
1026 * Mark a page readonly
1027 * XXX: assuming vaddr = paddr + KERNBASE
1028 */
1029
1030 static void
1031 xen_bt_set_readonly (vaddr_t page)
1032 {
1033 pt_entry_t entry;
1034
1035 entry = xpmap_ptom_masked(page - KERNBASE);
1036 entry |= PG_k | PG_V;
1037
1038 HYPERVISOR_update_va_mapping (page, entry, UVMF_INVLPG);
1039 }
1040
1041 #ifdef __x86_64__
1042 void
1043 xen_set_user_pgd(paddr_t page)
1044 {
1045 struct mmuext_op op;
1046 int s = splvm();
1047
1048 xpq_flush_queue();
1049 op.cmd = MMUEXT_NEW_USER_BASEPTR;
1050 op.arg1.mfn = xpmap_phys_to_machine_mapping[page >> PAGE_SHIFT];
1051 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
1052 panic("xen_set_user_pgd: failed to install new user page"
1053 " directory %#" PRIxPADDR, page);
1054 splx(s);
1055 }
1056 #endif /* __x86_64__ */
1057