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