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