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