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