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