Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.86
      1 /*	$NetBSD: pmap.c,v 1.86 2002/04/09 22:37:01 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Wasabi Systems, Inc.
      5  * Copyright (c) 2001 Richard Earnshaw
      6  * Copyright (c) 2001 Christopher Gilbert
      7  * All rights reserved.
      8  *
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the company nor the name of the author may be used to
     15  *    endorse or promote products derived from this software without specific
     16  *    prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*-
     32  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     33  * All rights reserved.
     34  *
     35  * This code is derived from software contributed to The NetBSD Foundation
     36  * by Charles M. Hannum.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *        This product includes software developed by the NetBSD
     49  *        Foundation, Inc. and its contributors.
     50  * 4. Neither the name of The NetBSD Foundation nor the names of its
     51  *    contributors may be used to endorse or promote products derived
     52  *    from this software without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     55  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 /*
     68  * Copyright (c) 1994-1998 Mark Brinicombe.
     69  * Copyright (c) 1994 Brini.
     70  * All rights reserved.
     71  *
     72  * This code is derived from software written for Brini by Mark Brinicombe
     73  *
     74  * Redistribution and use in source and binary forms, with or without
     75  * modification, are permitted provided that the following conditions
     76  * are met:
     77  * 1. Redistributions of source code must retain the above copyright
     78  *    notice, this list of conditions and the following disclaimer.
     79  * 2. Redistributions in binary form must reproduce the above copyright
     80  *    notice, this list of conditions and the following disclaimer in the
     81  *    documentation and/or other materials provided with the distribution.
     82  * 3. All advertising materials mentioning features or use of this software
     83  *    must display the following acknowledgement:
     84  *	This product includes software developed by Mark Brinicombe.
     85  * 4. The name of the author may not be used to endorse or promote products
     86  *    derived from this software without specific prior written permission.
     87  *
     88  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     89  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     91  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     92  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     93  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     94  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     95  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     96  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     97  *
     98  * RiscBSD kernel project
     99  *
    100  * pmap.c
    101  *
    102  * Machine dependant vm stuff
    103  *
    104  * Created      : 20/09/94
    105  */
    106 
    107 /*
    108  * Performance improvements, UVM changes, overhauls and part-rewrites
    109  * were contributed by Neil A. Carson <neil (at) causality.com>.
    110  */
    111 
    112 /*
    113  * The dram block info is currently referenced from the bootconfig.
    114  * This should be placed in a separate structure.
    115  */
    116 
    117 /*
    118  * Special compilation symbols
    119  * PMAP_DEBUG		- Build in pmap_debug_level code
    120  */
    121 
    122 /* Include header files */
    123 
    124 #include "opt_pmap_debug.h"
    125 #include "opt_ddb.h"
    126 
    127 #include <sys/types.h>
    128 #include <sys/param.h>
    129 #include <sys/kernel.h>
    130 #include <sys/systm.h>
    131 #include <sys/proc.h>
    132 #include <sys/malloc.h>
    133 #include <sys/user.h>
    134 #include <sys/pool.h>
    135 #include <sys/cdefs.h>
    136 
    137 #include <uvm/uvm.h>
    138 
    139 #include <machine/bootconfig.h>
    140 #include <machine/bus.h>
    141 #include <machine/pmap.h>
    142 #include <machine/pcb.h>
    143 #include <machine/param.h>
    144 #include <arm/arm32/katelib.h>
    145 
    146 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.86 2002/04/09 22:37:01 thorpej Exp $");
    147 #ifdef PMAP_DEBUG
    148 #define	PDEBUG(_lev_,_stat_) \
    149 	if (pmap_debug_level >= (_lev_)) \
    150         	((_stat_))
    151 int pmap_debug_level = -2;
    152 void pmap_dump_pvlist(vaddr_t phys, char *m);
    153 
    154 /*
    155  * for switching to potentially finer grained debugging
    156  */
    157 #define	PDB_FOLLOW	0x0001
    158 #define	PDB_INIT	0x0002
    159 #define	PDB_ENTER	0x0004
    160 #define	PDB_REMOVE	0x0008
    161 #define	PDB_CREATE	0x0010
    162 #define	PDB_PTPAGE	0x0020
    163 #define	PDB_GROWKERN	0x0040
    164 #define	PDB_BITS	0x0080
    165 #define	PDB_COLLECT	0x0100
    166 #define	PDB_PROTECT	0x0200
    167 #define	PDB_MAP_L1	0x0400
    168 #define	PDB_BOOTSTRAP	0x1000
    169 #define	PDB_PARANOIA	0x2000
    170 #define	PDB_WIRING	0x4000
    171 #define	PDB_PVDUMP	0x8000
    172 
    173 int debugmap = 0;
    174 int pmapdebug = PDB_PARANOIA | PDB_FOLLOW;
    175 #define	NPDEBUG(_lev_,_stat_) \
    176 	if (pmapdebug & (_lev_)) \
    177         	((_stat_))
    178 
    179 #else	/* PMAP_DEBUG */
    180 #define	PDEBUG(_lev_,_stat_) /* Nothing */
    181 #define NPDEBUG(_lev_,_stat_) /* Nothing */
    182 #endif	/* PMAP_DEBUG */
    183 
    184 struct pmap     kernel_pmap_store;
    185 
    186 /*
    187  * linked list of all non-kernel pmaps
    188  */
    189 
    190 static LIST_HEAD(, pmap) pmaps;
    191 
    192 /*
    193  * pool that pmap structures are allocated from
    194  */
    195 
    196 struct pool pmap_pmap_pool;
    197 
    198 static pt_entry_t *csrc_pte, *cdst_pte;
    199 static vaddr_t csrcp, cdstp;
    200 
    201 char *memhook;
    202 extern caddr_t msgbufaddr;
    203 
    204 boolean_t pmap_initialized = FALSE;	/* Has pmap_init completed? */
    205 /*
    206  * locking data structures
    207  */
    208 
    209 static struct lock pmap_main_lock;
    210 static struct simplelock pvalloc_lock;
    211 static struct simplelock pmaps_lock;
    212 #ifdef LOCKDEBUG
    213 #define PMAP_MAP_TO_HEAD_LOCK() \
    214      (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
    215 #define PMAP_MAP_TO_HEAD_UNLOCK() \
    216      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    217 
    218 #define PMAP_HEAD_TO_MAP_LOCK() \
    219      (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
    220 #define PMAP_HEAD_TO_MAP_UNLOCK() \
    221      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    222 #else
    223 #define	PMAP_MAP_TO_HEAD_LOCK()		/* nothing */
    224 #define	PMAP_MAP_TO_HEAD_UNLOCK()	/* nothing */
    225 #define	PMAP_HEAD_TO_MAP_LOCK()		/* nothing */
    226 #define	PMAP_HEAD_TO_MAP_UNLOCK()	/* nothing */
    227 #endif /* LOCKDEBUG */
    228 
    229 /*
    230  * pv_page management structures: locked by pvalloc_lock
    231  */
    232 
    233 TAILQ_HEAD(pv_pagelist, pv_page);
    234 static struct pv_pagelist pv_freepages;	/* list of pv_pages with free entrys */
    235 static struct pv_pagelist pv_unusedpgs; /* list of unused pv_pages */
    236 static int pv_nfpvents;			/* # of free pv entries */
    237 static struct pv_page *pv_initpage;	/* bootstrap page from kernel_map */
    238 static vaddr_t pv_cachedva;		/* cached VA for later use */
    239 
    240 #define PVE_LOWAT (PVE_PER_PVPAGE / 2)	/* free pv_entry low water mark */
    241 #define PVE_HIWAT (PVE_LOWAT + (PVE_PER_PVPAGE * 2))
    242 					/* high water mark */
    243 
    244 /*
    245  * local prototypes
    246  */
    247 
    248 static struct pv_entry	*pmap_add_pvpage __P((struct pv_page *, boolean_t));
    249 static struct pv_entry	*pmap_alloc_pv __P((struct pmap *, int)); /* see codes below */
    250 #define ALLOCPV_NEED	0	/* need PV now */
    251 #define ALLOCPV_TRY	1	/* just try to allocate, don't steal */
    252 #define ALLOCPV_NONEED	2	/* don't need PV, just growing cache */
    253 static struct pv_entry	*pmap_alloc_pvpage __P((struct pmap *, int));
    254 static void		 pmap_enter_pv __P((struct vm_page *,
    255 					    struct pv_entry *, struct pmap *,
    256 					    vaddr_t, struct vm_page *, int));
    257 static void		 pmap_free_pv __P((struct pmap *, struct pv_entry *));
    258 static void		 pmap_free_pvs __P((struct pmap *, struct pv_entry *));
    259 static void		 pmap_free_pv_doit __P((struct pv_entry *));
    260 static void		 pmap_free_pvpage __P((void));
    261 static boolean_t	 pmap_is_curpmap __P((struct pmap *));
    262 static struct pv_entry	*pmap_remove_pv __P((struct vm_page *, struct pmap *,
    263 			vaddr_t));
    264 #define PMAP_REMOVE_ALL		0	/* remove all mappings */
    265 #define PMAP_REMOVE_SKIPWIRED	1	/* skip wired mappings */
    266 
    267 static u_int pmap_modify_pv __P((struct pmap *, vaddr_t, struct vm_page *,
    268 	u_int, u_int));
    269 
    270 /*
    271  * Structure that describes and L1 table.
    272  */
    273 struct l1pt {
    274 	SIMPLEQ_ENTRY(l1pt)	pt_queue;	/* Queue pointers */
    275 	struct pglist		pt_plist;	/* Allocated page list */
    276 	vaddr_t			pt_va;		/* Allocated virtual address */
    277 	int			pt_flags;	/* Flags */
    278 };
    279 #define	PTFLAG_STATIC		0x01		/* Statically allocated */
    280 #define	PTFLAG_KPT		0x02		/* Kernel pt's are mapped */
    281 #define	PTFLAG_CLEAN		0x04		/* L1 is clean */
    282 
    283 static void pmap_free_l1pt __P((struct l1pt *));
    284 static int pmap_allocpagedir __P((struct pmap *));
    285 static int pmap_clean_page __P((struct pv_entry *, boolean_t));
    286 static void pmap_remove_all __P((struct vm_page *));
    287 
    288 static struct vm_page	*pmap_alloc_ptp __P((struct pmap *, vaddr_t));
    289 static struct vm_page	*pmap_get_ptp __P((struct pmap *, vaddr_t));
    290 __inline static void pmap_clearbit __P((struct vm_page *, unsigned int));
    291 
    292 extern paddr_t physical_start;
    293 extern paddr_t physical_freestart;
    294 extern paddr_t physical_end;
    295 extern paddr_t physical_freeend;
    296 extern unsigned int free_pages;
    297 extern int max_processes;
    298 
    299 vaddr_t virtual_avail;
    300 vaddr_t virtual_end;
    301 vaddr_t pmap_curmaxkvaddr;
    302 
    303 vaddr_t avail_start;
    304 vaddr_t avail_end;
    305 
    306 extern pv_addr_t systempage;
    307 
    308 /* Variables used by the L1 page table queue code */
    309 SIMPLEQ_HEAD(l1pt_queue, l1pt);
    310 static struct l1pt_queue l1pt_static_queue; /* head of our static l1 queue */
    311 static int l1pt_static_queue_count;	    /* items in the static l1 queue */
    312 static int l1pt_static_create_count;	    /* static l1 items created */
    313 static struct l1pt_queue l1pt_queue;	    /* head of our l1 queue */
    314 static int l1pt_queue_count;		    /* items in the l1 queue */
    315 static int l1pt_create_count;		    /* stat - L1's create count */
    316 static int l1pt_reuse_count;		    /* stat - L1's reused count */
    317 
    318 /* Local function prototypes (not used outside this file) */
    319 void pmap_pinit __P((struct pmap *));
    320 void pmap_freepagedir __P((struct pmap *));
    321 
    322 /* Other function prototypes */
    323 extern void bzero_page __P((vaddr_t));
    324 extern void bcopy_page __P((vaddr_t, vaddr_t));
    325 
    326 struct l1pt *pmap_alloc_l1pt __P((void));
    327 static __inline void pmap_map_in_l1 __P((struct pmap *pmap, vaddr_t va,
    328      vaddr_t l2pa, boolean_t));
    329 
    330 static pt_entry_t *pmap_map_ptes __P((struct pmap *));
    331 static void pmap_unmap_ptes __P((struct pmap *));
    332 
    333 __inline static void pmap_vac_me_harder __P((struct pmap *, struct vm_page *,
    334     pt_entry_t *, boolean_t));
    335 static void pmap_vac_me_kpmap __P((struct pmap *, struct vm_page *,
    336     pt_entry_t *, boolean_t));
    337 static void pmap_vac_me_user __P((struct pmap *, struct vm_page *,
    338     pt_entry_t *, boolean_t));
    339 
    340 /*
    341  * real definition of pv_entry.
    342  */
    343 
    344 struct pv_entry {
    345 	struct pv_entry *pv_next;       /* next pv_entry */
    346 	struct pmap     *pv_pmap;        /* pmap where mapping lies */
    347 	vaddr_t         pv_va;          /* virtual address for mapping */
    348 	int             pv_flags;       /* flags */
    349 	struct vm_page	*pv_ptp;	/* vm_page for the ptp */
    350 };
    351 
    352 /*
    353  * pv_entrys are dynamically allocated in chunks from a single page.
    354  * we keep track of how many pv_entrys are in use for each page and
    355  * we can free pv_entry pages if needed.  there is one lock for the
    356  * entire allocation system.
    357  */
    358 
    359 struct pv_page_info {
    360 	TAILQ_ENTRY(pv_page) pvpi_list;
    361 	struct pv_entry *pvpi_pvfree;
    362 	int pvpi_nfree;
    363 };
    364 
    365 /*
    366  * number of pv_entry's in a pv_page
    367  * (note: won't work on systems where NPBG isn't a constant)
    368  */
    369 
    370 #define PVE_PER_PVPAGE ((NBPG - sizeof(struct pv_page_info)) / \
    371 			sizeof(struct pv_entry))
    372 
    373 /*
    374  * a pv_page: where pv_entrys are allocated from
    375  */
    376 
    377 struct pv_page {
    378 	struct pv_page_info pvinfo;
    379 	struct pv_entry pvents[PVE_PER_PVPAGE];
    380 };
    381 
    382 #ifdef MYCROFT_HACK
    383 int mycroft_hack = 0;
    384 #endif
    385 
    386 /* Function to set the debug level of the pmap code */
    387 
    388 #ifdef PMAP_DEBUG
    389 void
    390 pmap_debug(int level)
    391 {
    392 	pmap_debug_level = level;
    393 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    394 }
    395 #endif	/* PMAP_DEBUG */
    396 
    397 __inline static boolean_t
    398 pmap_is_curpmap(struct pmap *pmap)
    399 {
    400 
    401 	if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap) ||
    402 	    pmap == pmap_kernel())
    403 		return (TRUE);
    404 
    405 	return (FALSE);
    406 }
    407 
    408 #include "isadma.h"
    409 
    410 #if NISADMA > 0
    411 /*
    412  * Used to protect memory for ISA DMA bounce buffers.  If, when loading
    413  * pages into the system, memory intersects with any of these ranges,
    414  * the intersecting memory will be loaded into a lower-priority free list.
    415  */
    416 bus_dma_segment_t *pmap_isa_dma_ranges;
    417 int pmap_isa_dma_nranges;
    418 
    419 /*
    420  * Check if a memory range intersects with an ISA DMA range, and
    421  * return the page-rounded intersection if it does.  The intersection
    422  * will be placed on a lower-priority free list.
    423  */
    424 static boolean_t
    425 pmap_isa_dma_range_intersect(paddr_t pa, psize_t size, paddr_t *pap,
    426     psize_t *sizep)
    427 {
    428 	bus_dma_segment_t *ds;
    429 	int i;
    430 
    431 	if (pmap_isa_dma_ranges == NULL)
    432 		return (FALSE);
    433 
    434 	for (i = 0, ds = pmap_isa_dma_ranges;
    435 	     i < pmap_isa_dma_nranges; i++, ds++) {
    436 		if (ds->ds_addr <= pa && pa < (ds->ds_addr + ds->ds_len)) {
    437 			/*
    438 			 * Beginning of region intersects with this range.
    439 			 */
    440 			*pap = trunc_page(pa);
    441 			*sizep = round_page(min(pa + size,
    442 			    ds->ds_addr + ds->ds_len) - pa);
    443 			return (TRUE);
    444 		}
    445 		if (pa < ds->ds_addr && ds->ds_addr < (pa + size)) {
    446 			/*
    447 			 * End of region intersects with this range.
    448 			 */
    449 			*pap = trunc_page(ds->ds_addr);
    450 			*sizep = round_page(min((pa + size) - ds->ds_addr,
    451 			    ds->ds_len));
    452 			return (TRUE);
    453 		}
    454 	}
    455 
    456 	/*
    457 	 * No intersection found.
    458 	 */
    459 	return (FALSE);
    460 }
    461 #endif /* NISADMA > 0 */
    462 
    463 /*
    464  * p v _ e n t r y   f u n c t i o n s
    465  */
    466 
    467 /*
    468  * pv_entry allocation functions:
    469  *   the main pv_entry allocation functions are:
    470  *     pmap_alloc_pv: allocate a pv_entry structure
    471  *     pmap_free_pv: free one pv_entry
    472  *     pmap_free_pvs: free a list of pv_entrys
    473  *
    474  * the rest are helper functions
    475  */
    476 
    477 /*
    478  * pmap_alloc_pv: inline function to allocate a pv_entry structure
    479  * => we lock pvalloc_lock
    480  * => if we fail, we call out to pmap_alloc_pvpage
    481  * => 3 modes:
    482  *    ALLOCPV_NEED   = we really need a pv_entry, even if we have to steal it
    483  *    ALLOCPV_TRY    = we want a pv_entry, but not enough to steal
    484  *    ALLOCPV_NONEED = we are trying to grow our free list, don't really need
    485  *			one now
    486  *
    487  * "try" is for optional functions like pmap_copy().
    488  */
    489 
    490 __inline static struct pv_entry *
    491 pmap_alloc_pv(struct pmap *pmap, int mode)
    492 {
    493 	struct pv_page *pvpage;
    494 	struct pv_entry *pv;
    495 
    496 	simple_lock(&pvalloc_lock);
    497 
    498 	pvpage = TAILQ_FIRST(&pv_freepages);
    499 
    500 	if (pvpage != NULL) {
    501 		pvpage->pvinfo.pvpi_nfree--;
    502 		if (pvpage->pvinfo.pvpi_nfree == 0) {
    503 			/* nothing left in this one? */
    504 			TAILQ_REMOVE(&pv_freepages, pvpage, pvinfo.pvpi_list);
    505 		}
    506 		pv = pvpage->pvinfo.pvpi_pvfree;
    507 		KASSERT(pv);
    508 		pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
    509 		pv_nfpvents--;  /* took one from pool */
    510 	} else {
    511 		pv = NULL;		/* need more of them */
    512 	}
    513 
    514 	/*
    515 	 * if below low water mark or we didn't get a pv_entry we try and
    516 	 * create more pv_entrys ...
    517 	 */
    518 
    519 	if (pv_nfpvents < PVE_LOWAT || pv == NULL) {
    520 		if (pv == NULL)
    521 			pv = pmap_alloc_pvpage(pmap, (mode == ALLOCPV_TRY) ?
    522 					       mode : ALLOCPV_NEED);
    523 		else
    524 			(void) pmap_alloc_pvpage(pmap, ALLOCPV_NONEED);
    525 	}
    526 
    527 	simple_unlock(&pvalloc_lock);
    528 	return(pv);
    529 }
    530 
    531 /*
    532  * pmap_alloc_pvpage: maybe allocate a new pvpage
    533  *
    534  * if need_entry is false: try and allocate a new pv_page
    535  * if need_entry is true: try and allocate a new pv_page and return a
    536  *	new pv_entry from it.   if we are unable to allocate a pv_page
    537  *	we make a last ditch effort to steal a pv_page from some other
    538  *	mapping.    if that fails, we panic...
    539  *
    540  * => we assume that the caller holds pvalloc_lock
    541  */
    542 
    543 static struct pv_entry *
    544 pmap_alloc_pvpage(struct pmap *pmap, int mode)
    545 {
    546 	struct vm_page *pg;
    547 	struct pv_page *pvpage;
    548 	struct pv_entry *pv;
    549 	int s;
    550 
    551 	/*
    552 	 * if we need_entry and we've got unused pv_pages, allocate from there
    553 	 */
    554 
    555 	pvpage = TAILQ_FIRST(&pv_unusedpgs);
    556 	if (mode != ALLOCPV_NONEED && pvpage != NULL) {
    557 
    558 		/* move it to pv_freepages list */
    559 		TAILQ_REMOVE(&pv_unusedpgs, pvpage, pvinfo.pvpi_list);
    560 		TAILQ_INSERT_HEAD(&pv_freepages, pvpage, pvinfo.pvpi_list);
    561 
    562 		/* allocate a pv_entry */
    563 		pvpage->pvinfo.pvpi_nfree--;	/* can't go to zero */
    564 		pv = pvpage->pvinfo.pvpi_pvfree;
    565 		KASSERT(pv);
    566 		pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
    567 
    568 		pv_nfpvents--;  /* took one from pool */
    569 		return(pv);
    570 	}
    571 
    572 	/*
    573 	 *  see if we've got a cached unmapped VA that we can map a page in.
    574 	 * if not, try to allocate one.
    575 	 */
    576 
    577 
    578 	if (pv_cachedva == 0) {
    579 		s = splvm();
    580 		pv_cachedva = uvm_km_kmemalloc(kmem_map, NULL,
    581 		    PAGE_SIZE, UVM_KMF_TRYLOCK|UVM_KMF_VALLOC);
    582 		splx(s);
    583 		if (pv_cachedva == 0) {
    584 			return (NULL);
    585 		}
    586 	}
    587 
    588 	pg = uvm_pagealloc(NULL, pv_cachedva - vm_map_min(kernel_map), NULL,
    589 	    UVM_PGA_USERESERVE);
    590 
    591 	if (pg == NULL)
    592 		return (NULL);
    593 	pg->flags &= ~PG_BUSY;	/* never busy */
    594 
    595 	/*
    596 	 * add a mapping for our new pv_page and free its entrys (save one!)
    597 	 *
    598 	 * NOTE: If we are allocating a PV page for the kernel pmap, the
    599 	 * pmap is already locked!  (...but entering the mapping is safe...)
    600 	 */
    601 
    602 	pmap_kenter_pa(pv_cachedva, VM_PAGE_TO_PHYS(pg),
    603 		VM_PROT_READ|VM_PROT_WRITE);
    604 	pmap_update(pmap_kernel());
    605 	pvpage = (struct pv_page *) pv_cachedva;
    606 	pv_cachedva = 0;
    607 	return (pmap_add_pvpage(pvpage, mode != ALLOCPV_NONEED));
    608 }
    609 
    610 /*
    611  * pmap_add_pvpage: add a pv_page's pv_entrys to the free list
    612  *
    613  * => caller must hold pvalloc_lock
    614  * => if need_entry is true, we allocate and return one pv_entry
    615  */
    616 
    617 static struct pv_entry *
    618 pmap_add_pvpage(struct pv_page *pvp, boolean_t need_entry)
    619 {
    620 	int tofree, lcv;
    621 
    622 	/* do we need to return one? */
    623 	tofree = (need_entry) ? PVE_PER_PVPAGE - 1 : PVE_PER_PVPAGE;
    624 
    625 	pvp->pvinfo.pvpi_pvfree = NULL;
    626 	pvp->pvinfo.pvpi_nfree = tofree;
    627 	for (lcv = 0 ; lcv < tofree ; lcv++) {
    628 		pvp->pvents[lcv].pv_next = pvp->pvinfo.pvpi_pvfree;
    629 		pvp->pvinfo.pvpi_pvfree = &pvp->pvents[lcv];
    630 	}
    631 	if (need_entry)
    632 		TAILQ_INSERT_TAIL(&pv_freepages, pvp, pvinfo.pvpi_list);
    633 	else
    634 		TAILQ_INSERT_TAIL(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    635 	pv_nfpvents += tofree;
    636 	return((need_entry) ? &pvp->pvents[lcv] : NULL);
    637 }
    638 
    639 /*
    640  * pmap_free_pv_doit: actually free a pv_entry
    641  *
    642  * => do not call this directly!  instead use either
    643  *    1. pmap_free_pv ==> free a single pv_entry
    644  *    2. pmap_free_pvs => free a list of pv_entrys
    645  * => we must be holding pvalloc_lock
    646  */
    647 
    648 __inline static void
    649 pmap_free_pv_doit(struct pv_entry *pv)
    650 {
    651 	struct pv_page *pvp;
    652 
    653 	pvp = (struct pv_page *) arm_trunc_page((vaddr_t)pv);
    654 	pv_nfpvents++;
    655 	pvp->pvinfo.pvpi_nfree++;
    656 
    657 	/* nfree == 1 => fully allocated page just became partly allocated */
    658 	if (pvp->pvinfo.pvpi_nfree == 1) {
    659 		TAILQ_INSERT_HEAD(&pv_freepages, pvp, pvinfo.pvpi_list);
    660 	}
    661 
    662 	/* free it */
    663 	pv->pv_next = pvp->pvinfo.pvpi_pvfree;
    664 	pvp->pvinfo.pvpi_pvfree = pv;
    665 
    666 	/*
    667 	 * are all pv_page's pv_entry's free?  move it to unused queue.
    668 	 */
    669 
    670 	if (pvp->pvinfo.pvpi_nfree == PVE_PER_PVPAGE) {
    671 		TAILQ_REMOVE(&pv_freepages, pvp, pvinfo.pvpi_list);
    672 		TAILQ_INSERT_HEAD(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    673 	}
    674 }
    675 
    676 /*
    677  * pmap_free_pv: free a single pv_entry
    678  *
    679  * => we gain the pvalloc_lock
    680  */
    681 
    682 __inline static void
    683 pmap_free_pv(struct pmap *pmap, struct pv_entry *pv)
    684 {
    685 	simple_lock(&pvalloc_lock);
    686 	pmap_free_pv_doit(pv);
    687 
    688 	/*
    689 	 * Can't free the PV page if the PV entries were associated with
    690 	 * the kernel pmap; the pmap is already locked.
    691 	 */
    692 	if (pv_nfpvents > PVE_HIWAT && TAILQ_FIRST(&pv_unusedpgs) != NULL &&
    693 	    pmap != pmap_kernel())
    694 		pmap_free_pvpage();
    695 
    696 	simple_unlock(&pvalloc_lock);
    697 }
    698 
    699 /*
    700  * pmap_free_pvs: free a list of pv_entrys
    701  *
    702  * => we gain the pvalloc_lock
    703  */
    704 
    705 __inline static void
    706 pmap_free_pvs(struct pmap *pmap, struct pv_entry *pvs)
    707 {
    708 	struct pv_entry *nextpv;
    709 
    710 	simple_lock(&pvalloc_lock);
    711 
    712 	for ( /* null */ ; pvs != NULL ; pvs = nextpv) {
    713 		nextpv = pvs->pv_next;
    714 		pmap_free_pv_doit(pvs);
    715 	}
    716 
    717 	/*
    718 	 * Can't free the PV page if the PV entries were associated with
    719 	 * the kernel pmap; the pmap is already locked.
    720 	 */
    721 	if (pv_nfpvents > PVE_HIWAT && TAILQ_FIRST(&pv_unusedpgs) != NULL &&
    722 	    pmap != pmap_kernel())
    723 		pmap_free_pvpage();
    724 
    725 	simple_unlock(&pvalloc_lock);
    726 }
    727 
    728 
    729 /*
    730  * pmap_free_pvpage: try and free an unused pv_page structure
    731  *
    732  * => assume caller is holding the pvalloc_lock and that
    733  *	there is a page on the pv_unusedpgs list
    734  * => if we can't get a lock on the kmem_map we try again later
    735  */
    736 
    737 static void
    738 pmap_free_pvpage(void)
    739 {
    740 	int s;
    741 	struct vm_map *map;
    742 	struct vm_map_entry *dead_entries;
    743 	struct pv_page *pvp;
    744 
    745 	s = splvm(); /* protect kmem_map */
    746 
    747 	pvp = TAILQ_FIRST(&pv_unusedpgs);
    748 
    749 	/*
    750 	 * note: watch out for pv_initpage which is allocated out of
    751 	 * kernel_map rather than kmem_map.
    752 	 */
    753 	if (pvp == pv_initpage)
    754 		map = kernel_map;
    755 	else
    756 		map = kmem_map;
    757 	if (vm_map_lock_try(map)) {
    758 
    759 		/* remove pvp from pv_unusedpgs */
    760 		TAILQ_REMOVE(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    761 
    762 		/* unmap the page */
    763 		dead_entries = NULL;
    764 		uvm_unmap_remove(map, (vaddr_t)pvp, ((vaddr_t)pvp) + PAGE_SIZE,
    765 		    &dead_entries);
    766 		vm_map_unlock(map);
    767 
    768 		if (dead_entries != NULL)
    769 			uvm_unmap_detach(dead_entries, 0);
    770 
    771 		pv_nfpvents -= PVE_PER_PVPAGE;  /* update free count */
    772 	}
    773 	if (pvp == pv_initpage)
    774 		/* no more initpage, we've freed it */
    775 		pv_initpage = NULL;
    776 
    777 	splx(s);
    778 }
    779 
    780 /*
    781  * main pv_entry manipulation functions:
    782  *   pmap_enter_pv: enter a mapping onto a vm_page list
    783  *   pmap_remove_pv: remove a mappiing from a vm_page list
    784  *
    785  * NOTE: pmap_enter_pv expects to lock the pvh itself
    786  *       pmap_remove_pv expects te caller to lock the pvh before calling
    787  */
    788 
    789 /*
    790  * pmap_enter_pv: enter a mapping onto a vm_page lst
    791  *
    792  * => caller should hold the proper lock on pmap_main_lock
    793  * => caller should have pmap locked
    794  * => we will gain the lock on the vm_page and allocate the new pv_entry
    795  * => caller should adjust ptp's wire_count before calling
    796  * => caller should not adjust pmap's wire_count
    797  */
    798 
    799 __inline static void
    800 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, struct pmap *pmap,
    801     vaddr_t va, struct vm_page *ptp, int flags)
    802 {
    803 	pve->pv_pmap = pmap;
    804 	pve->pv_va = va;
    805 	pve->pv_ptp = ptp;			/* NULL for kernel pmap */
    806 	pve->pv_flags = flags;
    807 	simple_lock(&pg->mdpage.pvh_slock);	/* lock vm_page */
    808 	pve->pv_next = pg->mdpage.pvh_list;	/* add to ... */
    809 	pg->mdpage.pvh_list = pve;		/* ... locked list */
    810 	simple_unlock(&pg->mdpage.pvh_slock);	/* unlock, done! */
    811 	if (pve->pv_flags & PVF_WIRED)
    812 		++pmap->pm_stats.wired_count;
    813 }
    814 
    815 /*
    816  * pmap_remove_pv: try to remove a mapping from a pv_list
    817  *
    818  * => caller should hold proper lock on pmap_main_lock
    819  * => pmap should be locked
    820  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    821  * => caller should adjust ptp's wire_count and free PTP if needed
    822  * => caller should NOT adjust pmap's wire_count
    823  * => we return the removed pve
    824  */
    825 
    826 __inline static struct pv_entry *
    827 pmap_remove_pv(struct vm_page *pg, struct pmap *pmap, vaddr_t va)
    828 {
    829 	struct pv_entry *pve, **prevptr;
    830 
    831 	prevptr = &pg->mdpage.pvh_list;		/* previous pv_entry pointer */
    832 	pve = *prevptr;
    833 	while (pve) {
    834 		if (pve->pv_pmap == pmap && pve->pv_va == va) {	/* match? */
    835 			*prevptr = pve->pv_next;		/* remove it! */
    836 			if (pve->pv_flags & PVF_WIRED)
    837 			    --pmap->pm_stats.wired_count;
    838 			break;
    839 		}
    840 		prevptr = &pve->pv_next;		/* previous pointer */
    841 		pve = pve->pv_next;			/* advance */
    842 	}
    843 	return(pve);				/* return removed pve */
    844 }
    845 
    846 /*
    847  *
    848  * pmap_modify_pv: Update pv flags
    849  *
    850  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    851  * => caller should NOT adjust pmap's wire_count
    852  * => caller must call pmap_vac_me_harder() if writable status of a page
    853  *    may have changed.
    854  * => we return the old flags
    855  *
    856  * Modify a physical-virtual mapping in the pv table
    857  */
    858 
    859 static /* __inline */ u_int
    860 pmap_modify_pv(struct pmap *pmap, vaddr_t va, struct vm_page *pg,
    861     u_int bic_mask, u_int eor_mask)
    862 {
    863 	struct pv_entry *npv;
    864 	u_int flags, oflags;
    865 
    866 	/*
    867 	 * There is at least one VA mapping this page.
    868 	 */
    869 
    870 	for (npv = pg->mdpage.pvh_list; npv; npv = npv->pv_next) {
    871 		if (pmap == npv->pv_pmap && va == npv->pv_va) {
    872 			oflags = npv->pv_flags;
    873 			npv->pv_flags = flags =
    874 			    ((oflags & ~bic_mask) ^ eor_mask);
    875 			if ((flags ^ oflags) & PVF_WIRED) {
    876 				if (flags & PVF_WIRED)
    877 					++pmap->pm_stats.wired_count;
    878 				else
    879 					--pmap->pm_stats.wired_count;
    880 			}
    881 			return (oflags);
    882 		}
    883 	}
    884 	return (0);
    885 }
    886 
    887 /*
    888  * Map the specified level 2 pagetable into the level 1 page table for
    889  * the given pmap to cover a chunk of virtual address space starting from the
    890  * address specified.
    891  */
    892 static __inline void
    893 pmap_map_in_l1(struct pmap *pmap, vaddr_t va, paddr_t l2pa, boolean_t selfref)
    894 {
    895 	vaddr_t ptva;
    896 
    897 	/* Calculate the index into the L1 page table. */
    898 	ptva = (va >> L1_S_SHIFT) & ~3;
    899 
    900 	/* Map page table into the L1. */
    901 	pmap->pm_pdir[ptva + 0] = L1_C_PROTO | (l2pa + 0x000);
    902 	pmap->pm_pdir[ptva + 1] = L1_C_PROTO | (l2pa + 0x400);
    903 	pmap->pm_pdir[ptva + 2] = L1_C_PROTO | (l2pa + 0x800);
    904 	pmap->pm_pdir[ptva + 3] = L1_C_PROTO | (l2pa + 0xc00);
    905 
    906 	/* Map the page table into the page table area. */
    907 	if (selfref)
    908 		*((pt_entry_t *)(pmap->pm_vptpt + ptva)) = L2_S_PROTO | l2pa |
    909 		    L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE);
    910 }
    911 
    912 #if 0
    913 static __inline void
    914 pmap_unmap_in_l1(struct pmap *pmap, vaddr_t va)
    915 {
    916 	vaddr_t ptva;
    917 
    918 	/* Calculate the index into the L1 page table. */
    919 	ptva = (va >> L1_S_SHIFT) & ~3;
    920 
    921 	/* Unmap page table from the L1. */
    922 	pmap->pm_pdir[ptva + 0] = 0;
    923 	pmap->pm_pdir[ptva + 1] = 0;
    924 	pmap->pm_pdir[ptva + 2] = 0;
    925 	pmap->pm_pdir[ptva + 3] = 0;
    926 
    927 	/* Unmap the page table from the page table area. */
    928 	*((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
    929 }
    930 #endif
    931 
    932 /*
    933  *	Used to map a range of physical addresses into kernel
    934  *	virtual address space.
    935  *
    936  *	For now, VM is already on, we only need to map the
    937  *	specified memory.
    938  */
    939 vaddr_t
    940 pmap_map(vaddr_t va, paddr_t spa, paddr_t epa, vm_prot_t prot)
    941 {
    942 	while (spa < epa) {
    943 		pmap_kenter_pa(va, spa, prot);
    944 		va += NBPG;
    945 		spa += NBPG;
    946 	}
    947 	pmap_update(pmap_kernel());
    948 	return(va);
    949 }
    950 
    951 
    952 /*
    953  * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
    954  *
    955  * bootstrap the pmap system. This is called from initarm and allows
    956  * the pmap system to initailise any structures it requires.
    957  *
    958  * Currently this sets up the kernel_pmap that is statically allocated
    959  * and also allocated virtual addresses for certain page hooks.
    960  * Currently the only one page hook is allocated that is used
    961  * to zero physical pages of memory.
    962  * It also initialises the start and end address of the kernel data space.
    963  */
    964 extern paddr_t physical_freestart;
    965 extern paddr_t physical_freeend;
    966 
    967 char *boot_head;
    968 
    969 void
    970 pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
    971 {
    972 	pt_entry_t *pte;
    973 	int loop;
    974 	paddr_t start, end;
    975 #if NISADMA > 0
    976 	paddr_t istart;
    977 	psize_t isize;
    978 #endif
    979 
    980 	pmap_kernel()->pm_pdir = kernel_l1pt;
    981 	pmap_kernel()->pm_pptpt = kernel_ptpt.pv_pa;
    982 	pmap_kernel()->pm_vptpt = kernel_ptpt.pv_va;
    983 	simple_lock_init(&pmap_kernel()->pm_lock);
    984 	pmap_kernel()->pm_obj.pgops = NULL;
    985 	TAILQ_INIT(&(pmap_kernel()->pm_obj.memq));
    986 	pmap_kernel()->pm_obj.uo_npages = 0;
    987 	pmap_kernel()->pm_obj.uo_refs = 1;
    988 
    989 	/*
    990 	 * Initialize PAGE_SIZE-dependent variables.
    991 	 */
    992 	uvm_setpagesize();
    993 
    994 	loop = 0;
    995 	while (loop < bootconfig.dramblocks) {
    996 		start = (paddr_t)bootconfig.dram[loop].address;
    997 		end = start + (bootconfig.dram[loop].pages * NBPG);
    998 		if (start < physical_freestart)
    999 			start = physical_freestart;
   1000 		if (end > physical_freeend)
   1001 			end = physical_freeend;
   1002 #if 0
   1003 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
   1004 #endif
   1005 #if NISADMA > 0
   1006 		if (pmap_isa_dma_range_intersect(start, end - start,
   1007 		    &istart, &isize)) {
   1008 			/*
   1009 			 * Place the pages that intersect with the
   1010 			 * ISA DMA range onto the ISA DMA free list.
   1011 			 */
   1012 #if 0
   1013 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
   1014 			    istart + isize - 1);
   1015 #endif
   1016 			uvm_page_physload(atop(istart),
   1017 			    atop(istart + isize), atop(istart),
   1018 			    atop(istart + isize), VM_FREELIST_ISADMA);
   1019 
   1020 			/*
   1021 			 * Load the pieces that come before
   1022 			 * the intersection into the default
   1023 			 * free list.
   1024 			 */
   1025 			if (start < istart) {
   1026 #if 0
   1027 				printf("    BEFORE 0x%lx -> 0x%lx\n",
   1028 				    start, istart - 1);
   1029 #endif
   1030 				uvm_page_physload(atop(start),
   1031 				    atop(istart), atop(start),
   1032 				    atop(istart), VM_FREELIST_DEFAULT);
   1033 			}
   1034 
   1035 			/*
   1036 			 * Load the pieces that come after
   1037 			 * the intersection into the default
   1038 			 * free list.
   1039 			 */
   1040 			if ((istart + isize) < end) {
   1041 #if 0
   1042 				printf("     AFTER 0x%lx -> 0x%lx\n",
   1043 				    (istart + isize), end - 1);
   1044 #endif
   1045 				uvm_page_physload(atop(istart + isize),
   1046 				    atop(end), atop(istart + isize),
   1047 				    atop(end), VM_FREELIST_DEFAULT);
   1048 			}
   1049 		} else {
   1050 			uvm_page_physload(atop(start), atop(end),
   1051 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
   1052 		}
   1053 #else	/* NISADMA > 0 */
   1054 		uvm_page_physload(atop(start), atop(end),
   1055 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
   1056 #endif /* NISADMA > 0 */
   1057 		++loop;
   1058 	}
   1059 
   1060 	virtual_avail = KERNEL_VM_BASE;
   1061 	virtual_end = KERNEL_VM_BASE + KERNEL_VM_SIZE;
   1062 
   1063 	/*
   1064 	 * now we allocate the "special" VAs which are used for tmp mappings
   1065 	 * by the pmap (and other modules).  we allocate the VAs by advancing
   1066 	 * virtual_avail (note that there are no pages mapped at these VAs).
   1067 	 * we find the PTE that maps the allocated VA via the linear PTE
   1068 	 * mapping.
   1069 	 */
   1070 
   1071 	pte = ((pt_entry_t *) PTE_BASE) + atop(virtual_avail);
   1072 
   1073 	csrcp = virtual_avail; csrc_pte = pte;
   1074 	virtual_avail += PAGE_SIZE; pte++;
   1075 
   1076 	cdstp = virtual_avail; cdst_pte = pte;
   1077 	virtual_avail += PAGE_SIZE; pte++;
   1078 
   1079 	memhook = (char *) virtual_avail;	/* don't need pte */
   1080 	virtual_avail += PAGE_SIZE; pte++;
   1081 
   1082 	msgbufaddr = (caddr_t) virtual_avail;	/* don't need pte */
   1083 	virtual_avail += round_page(MSGBUFSIZE);
   1084 	pte += atop(round_page(MSGBUFSIZE));
   1085 
   1086 	/*
   1087 	 * init the static-global locks and global lists.
   1088 	 */
   1089 	spinlockinit(&pmap_main_lock, "pmaplk", 0);
   1090 	simple_lock_init(&pvalloc_lock);
   1091 	simple_lock_init(&pmaps_lock);
   1092 	LIST_INIT(&pmaps);
   1093 	TAILQ_INIT(&pv_freepages);
   1094 	TAILQ_INIT(&pv_unusedpgs);
   1095 
   1096 	/*
   1097 	 * initialize the pmap pool.
   1098 	 */
   1099 
   1100 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
   1101 		  &pool_allocator_nointr);
   1102 
   1103 	cpu_dcache_wbinv_all();
   1104 }
   1105 
   1106 /*
   1107  * void pmap_init(void)
   1108  *
   1109  * Initialize the pmap module.
   1110  * Called by vm_init() in vm/vm_init.c in order to initialise
   1111  * any structures that the pmap system needs to map virtual memory.
   1112  */
   1113 
   1114 extern int physmem;
   1115 
   1116 void
   1117 pmap_init(void)
   1118 {
   1119 
   1120 	/*
   1121 	 * Set the available memory vars - These do not map to real memory
   1122 	 * addresses and cannot as the physical memory is fragmented.
   1123 	 * They are used by ps for %mem calculations.
   1124 	 * One could argue whether this should be the entire memory or just
   1125 	 * the memory that is useable in a user process.
   1126 	 */
   1127 	avail_start = 0;
   1128 	avail_end = physmem * NBPG;
   1129 
   1130 	/*
   1131 	 * now we need to free enough pv_entry structures to allow us to get
   1132 	 * the kmem_map/kmem_object allocated and inited (done after this
   1133 	 * function is finished).  to do this we allocate one bootstrap page out
   1134 	 * of kernel_map and use it to provide an initial pool of pv_entry
   1135 	 * structures.   we never free this page.
   1136 	 */
   1137 
   1138 	pv_initpage = (struct pv_page *) uvm_km_alloc(kernel_map, PAGE_SIZE);
   1139 	if (pv_initpage == NULL)
   1140 		panic("pmap_init: pv_initpage");
   1141 	pv_cachedva = 0;   /* a VA we have allocated but not used yet */
   1142 	pv_nfpvents = 0;
   1143 	(void) pmap_add_pvpage(pv_initpage, FALSE);
   1144 
   1145 	pmap_initialized = TRUE;
   1146 
   1147 	/* Initialise our L1 page table queues and counters */
   1148 	SIMPLEQ_INIT(&l1pt_static_queue);
   1149 	l1pt_static_queue_count = 0;
   1150 	l1pt_static_create_count = 0;
   1151 	SIMPLEQ_INIT(&l1pt_queue);
   1152 	l1pt_queue_count = 0;
   1153 	l1pt_create_count = 0;
   1154 	l1pt_reuse_count = 0;
   1155 }
   1156 
   1157 /*
   1158  * pmap_postinit()
   1159  *
   1160  * This routine is called after the vm and kmem subsystems have been
   1161  * initialised. This allows the pmap code to perform any initialisation
   1162  * that can only be done one the memory allocation is in place.
   1163  */
   1164 
   1165 void
   1166 pmap_postinit(void)
   1167 {
   1168 	int loop;
   1169 	struct l1pt *pt;
   1170 
   1171 #ifdef PMAP_STATIC_L1S
   1172 	for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
   1173 #else	/* PMAP_STATIC_L1S */
   1174 	for (loop = 0; loop < max_processes; ++loop) {
   1175 #endif	/* PMAP_STATIC_L1S */
   1176 		/* Allocate a L1 page table */
   1177 		pt = pmap_alloc_l1pt();
   1178 		if (!pt)
   1179 			panic("Cannot allocate static L1 page tables\n");
   1180 
   1181 		/* Clean it */
   1182 		bzero((void *)pt->pt_va, L1_TABLE_SIZE);
   1183 		pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
   1184 		/* Add the page table to the queue */
   1185 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
   1186 		++l1pt_static_queue_count;
   1187 		++l1pt_static_create_count;
   1188 	}
   1189 }
   1190 
   1191 
   1192 /*
   1193  * Create and return a physical map.
   1194  *
   1195  * If the size specified for the map is zero, the map is an actual physical
   1196  * map, and may be referenced by the hardware.
   1197  *
   1198  * If the size specified is non-zero, the map will be used in software only,
   1199  * and is bounded by that size.
   1200  */
   1201 
   1202 pmap_t
   1203 pmap_create(void)
   1204 {
   1205 	struct pmap *pmap;
   1206 
   1207 	/*
   1208 	 * Fetch pmap entry from the pool
   1209 	 */
   1210 
   1211 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
   1212 	/* XXX is this really needed! */
   1213 	memset(pmap, 0, sizeof(*pmap));
   1214 
   1215 	simple_lock_init(&pmap->pm_obj.vmobjlock);
   1216 	pmap->pm_obj.pgops = NULL;	/* currently not a mappable object */
   1217 	TAILQ_INIT(&pmap->pm_obj.memq);
   1218 	pmap->pm_obj.uo_npages = 0;
   1219 	pmap->pm_obj.uo_refs = 1;
   1220 	pmap->pm_stats.wired_count = 0;
   1221 	pmap->pm_stats.resident_count = 1;
   1222 	pmap->pm_ptphint = NULL;
   1223 
   1224 	/* Now init the machine part of the pmap */
   1225 	pmap_pinit(pmap);
   1226 	return(pmap);
   1227 }
   1228 
   1229 /*
   1230  * pmap_alloc_l1pt()
   1231  *
   1232  * This routine allocates physical and virtual memory for a L1 page table
   1233  * and wires it.
   1234  * A l1pt structure is returned to describe the allocated page table.
   1235  *
   1236  * This routine is allowed to fail if the required memory cannot be allocated.
   1237  * In this case NULL is returned.
   1238  */
   1239 
   1240 struct l1pt *
   1241 pmap_alloc_l1pt(void)
   1242 {
   1243 	paddr_t pa;
   1244 	vaddr_t va;
   1245 	struct l1pt *pt;
   1246 	int error;
   1247 	struct vm_page *m;
   1248 	pt_entry_t *ptes;
   1249 
   1250 	/* Allocate virtual address space for the L1 page table */
   1251 	va = uvm_km_valloc(kernel_map, L1_TABLE_SIZE);
   1252 	if (va == 0) {
   1253 #ifdef DIAGNOSTIC
   1254 		PDEBUG(0,
   1255 		    printf("pmap: Cannot allocate pageable memory for L1\n"));
   1256 #endif	/* DIAGNOSTIC */
   1257 		return(NULL);
   1258 	}
   1259 
   1260 	/* Allocate memory for the l1pt structure */
   1261 	pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
   1262 
   1263 	/*
   1264 	 * Allocate pages from the VM system.
   1265 	 */
   1266 	TAILQ_INIT(&pt->pt_plist);
   1267 	error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start, physical_end,
   1268 	    L1_TABLE_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
   1269 	if (error) {
   1270 #ifdef DIAGNOSTIC
   1271 		PDEBUG(0,
   1272 		    printf("pmap: Cannot allocate physical mem for L1 (%d)\n",
   1273 		    error));
   1274 #endif	/* DIAGNOSTIC */
   1275 		/* Release the resources we already have claimed */
   1276 		free(pt, M_VMPMAP);
   1277 		uvm_km_free(kernel_map, va, L1_TABLE_SIZE);
   1278 		return(NULL);
   1279 	}
   1280 
   1281 	/* Map our physical pages into our virtual space */
   1282 	pt->pt_va = va;
   1283 	m = TAILQ_FIRST(&pt->pt_plist);
   1284 	ptes = pmap_map_ptes(pmap_kernel());
   1285 	while (m && va < (pt->pt_va + L1_TABLE_SIZE)) {
   1286 		pa = VM_PAGE_TO_PHYS(m);
   1287 
   1288 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
   1289 
   1290 		/* Revoke cacheability and bufferability */
   1291 		/* XXX should be done better than this */
   1292 		ptes[arm_btop(va)] &= ~(L2_C | L2_B);
   1293 
   1294 		va += NBPG;
   1295 		m = m->pageq.tqe_next;
   1296 	}
   1297 	pmap_unmap_ptes(pmap_kernel());
   1298 	pmap_update(pmap_kernel());
   1299 
   1300 #ifdef DIAGNOSTIC
   1301 	if (m)
   1302 		panic("pmap_alloc_l1pt: pglist not empty\n");
   1303 #endif	/* DIAGNOSTIC */
   1304 
   1305 	pt->pt_flags = 0;
   1306 	return(pt);
   1307 }
   1308 
   1309 /*
   1310  * Free a L1 page table previously allocated with pmap_alloc_l1pt().
   1311  */
   1312 static void
   1313 pmap_free_l1pt(struct l1pt *pt)
   1314 {
   1315 	/* Separate the physical memory for the virtual space */
   1316 	pmap_kremove(pt->pt_va, L1_TABLE_SIZE);
   1317 	pmap_update(pmap_kernel());
   1318 
   1319 	/* Return the physical memory */
   1320 	uvm_pglistfree(&pt->pt_plist);
   1321 
   1322 	/* Free the virtual space */
   1323 	uvm_km_free(kernel_map, pt->pt_va, L1_TABLE_SIZE);
   1324 
   1325 	/* Free the l1pt structure */
   1326 	free(pt, M_VMPMAP);
   1327 }
   1328 
   1329 /*
   1330  * Allocate a page directory.
   1331  * This routine will either allocate a new page directory from the pool
   1332  * of L1 page tables currently held by the kernel or it will allocate
   1333  * a new one via pmap_alloc_l1pt().
   1334  * It will then initialise the l1 page table for use.
   1335  *
   1336  * XXX must tidy up and fix this code, not happy about how it does the pmaps_locking
   1337  */
   1338 static int
   1339 pmap_allocpagedir(struct pmap *pmap)
   1340 {
   1341 	paddr_t pa;
   1342 	struct l1pt *pt;
   1343 	pt_entry_t *pte;
   1344 
   1345 	PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
   1346 
   1347 	/* Do we have any spare L1's lying around ? */
   1348 	if (l1pt_static_queue_count) {
   1349 		--l1pt_static_queue_count;
   1350 		pt = l1pt_static_queue.sqh_first;
   1351 		SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
   1352 	} else if (l1pt_queue_count) {
   1353 		--l1pt_queue_count;
   1354 		pt = l1pt_queue.sqh_first;
   1355 		SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
   1356 		++l1pt_reuse_count;
   1357 	} else {
   1358 		pt = pmap_alloc_l1pt();
   1359 		if (!pt)
   1360 			return(ENOMEM);
   1361 		++l1pt_create_count;
   1362 	}
   1363 
   1364 	/* Store the pointer to the l1 descriptor in the pmap. */
   1365 	pmap->pm_l1pt = pt;
   1366 
   1367 	/* Get the physical address of the start of the l1 */
   1368 	pa = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pt->pt_plist));
   1369 
   1370 	/* Store the virtual address of the l1 in the pmap. */
   1371 	pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
   1372 
   1373 	/* Clean the L1 if it is dirty */
   1374 	if (!(pt->pt_flags & PTFLAG_CLEAN))
   1375 		bzero((void *)pmap->pm_pdir, (L1_TABLE_SIZE - KERNEL_PD_SIZE));
   1376 
   1377 	/* Allocate a page table to map all the page tables for this pmap */
   1378 
   1379 #ifdef DIAGNOSTIC
   1380 	if (pmap->pm_vptpt) {
   1381 		/* XXX What if we have one already ? */
   1382 		panic("pmap_allocpagedir: have pt already\n");
   1383 	}
   1384 #endif	/* DIAGNOSTIC */
   1385 	pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
   1386 	if (pmap->pm_vptpt == 0) {
   1387 	    pmap_freepagedir(pmap);
   1388     	    return(ENOMEM);
   1389 	}
   1390 
   1391 	/* need to lock this all up  for growkernel */
   1392 	simple_lock(&pmaps_lock);
   1393 	/* wish we didn't have to keep this locked... */
   1394 
   1395 	/* Duplicate the kernel mappings. */
   1396 	bcopy((char *)pmap_kernel()->pm_pdir + (L1_TABLE_SIZE - KERNEL_PD_SIZE),
   1397 		(char *)pmap->pm_pdir + (L1_TABLE_SIZE - KERNEL_PD_SIZE),
   1398 		KERNEL_PD_SIZE);
   1399 
   1400 	pte = vtopte(pmap->pm_vptpt);
   1401 	pmap->pm_pptpt = l2pte_pa(*pte);
   1402 
   1403 	/* Revoke cacheability and bufferability */
   1404 	/* XXX should be done better than this */
   1405 	*pte &= ~(L2_C | L2_B);
   1406 
   1407 	/* Wire in this page table */
   1408 	pmap_map_in_l1(pmap, PTE_BASE, pmap->pm_pptpt, TRUE);
   1409 
   1410 	pt->pt_flags &= ~PTFLAG_CLEAN;	/* L1 is dirty now */
   1411 
   1412 	/*
   1413 	 * Map the kernel page tables into the new PT map.
   1414 	 */
   1415 	bcopy((char *)(PTE_BASE
   1416 	    + (PTE_BASE >> (PGSHIFT - 2))
   1417 	    + ((L1_TABLE_SIZE - KERNEL_PD_SIZE) >> 2)),
   1418 	    (char *)pmap->pm_vptpt + ((L1_TABLE_SIZE - KERNEL_PD_SIZE) >> 2),
   1419 	    (KERNEL_PD_SIZE >> 2));
   1420 
   1421 	LIST_INSERT_HEAD(&pmaps, pmap, pm_list);
   1422 	simple_unlock(&pmaps_lock);
   1423 
   1424 	return(0);
   1425 }
   1426 
   1427 
   1428 /*
   1429  * Initialize a preallocated and zeroed pmap structure,
   1430  * such as one in a vmspace structure.
   1431  */
   1432 
   1433 void
   1434 pmap_pinit(struct pmap *pmap)
   1435 {
   1436 	int backoff = 6;
   1437 	int retry = 10;
   1438 
   1439 	PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
   1440 
   1441 	/* Keep looping until we succeed in allocating a page directory */
   1442 	while (pmap_allocpagedir(pmap) != 0) {
   1443 		/*
   1444 		 * Ok we failed to allocate a suitable block of memory for an
   1445 		 * L1 page table. This means that either:
   1446 		 * 1. 16KB of virtual address space could not be allocated
   1447 		 * 2. 16KB of physically contiguous memory on a 16KB boundary
   1448 		 *    could not be allocated.
   1449 		 *
   1450 		 * Since we cannot fail we will sleep for a while and try
   1451 		 * again.
   1452 		 *
   1453 		 * Searching for a suitable L1 PT is expensive:
   1454 		 * to avoid hogging the system when memory is really
   1455 		 * scarce, use an exponential back-off so that
   1456 		 * eventually we won't retry more than once every 8
   1457 		 * seconds.  This should allow other processes to run
   1458 		 * to completion and free up resources.
   1459 		 */
   1460 		(void) ltsleep(&lbolt, PVM, "l1ptwait", (hz << 3) >> backoff,
   1461 		    NULL);
   1462 		if (--retry == 0) {
   1463 			retry = 10;
   1464 			if (backoff)
   1465 				--backoff;
   1466 		}
   1467 	}
   1468 
   1469 	if (vector_page < KERNEL_BASE) {
   1470 		/*
   1471 		 * Map the vector page.  This will also allocate and map
   1472 		 * an L2 table for it.
   1473 		 */
   1474 		pmap_enter(pmap, vector_page, systempage.pv_pa,
   1475 		    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1476 		pmap_update(pmap);
   1477 	}
   1478 }
   1479 
   1480 
   1481 void
   1482 pmap_freepagedir(struct pmap *pmap)
   1483 {
   1484 	/* Free the memory used for the page table mapping */
   1485 	if (pmap->pm_vptpt != 0)
   1486 		uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
   1487 
   1488 	/* junk the L1 page table */
   1489 	if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
   1490 		/* Add the page table to the queue */
   1491 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
   1492 		++l1pt_static_queue_count;
   1493 	} else if (l1pt_queue_count < 8) {
   1494 		/* Add the page table to the queue */
   1495 		SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
   1496 		++l1pt_queue_count;
   1497 	} else
   1498 		pmap_free_l1pt(pmap->pm_l1pt);
   1499 }
   1500 
   1501 
   1502 /*
   1503  * Retire the given physical map from service.
   1504  * Should only be called if the map contains no valid mappings.
   1505  */
   1506 
   1507 void
   1508 pmap_destroy(struct pmap *pmap)
   1509 {
   1510 	struct vm_page *page;
   1511 	int count;
   1512 
   1513 	if (pmap == NULL)
   1514 		return;
   1515 
   1516 	PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
   1517 
   1518 	/*
   1519 	 * Drop reference count
   1520 	 */
   1521 	simple_lock(&pmap->pm_obj.vmobjlock);
   1522 	count = --pmap->pm_obj.uo_refs;
   1523 	simple_unlock(&pmap->pm_obj.vmobjlock);
   1524 	if (count > 0) {
   1525 		return;
   1526 	}
   1527 
   1528 	/*
   1529 	 * reference count is zero, free pmap resources and then free pmap.
   1530 	 */
   1531 
   1532 	/*
   1533 	 * remove it from global list of pmaps
   1534 	 */
   1535 
   1536 	simple_lock(&pmaps_lock);
   1537 	LIST_REMOVE(pmap, pm_list);
   1538 	simple_unlock(&pmaps_lock);
   1539 
   1540 	if (vector_page < KERNEL_BASE) {
   1541 		/* Remove the vector page mapping */
   1542 		pmap_remove(pmap, vector_page, vector_page + NBPG);
   1543 		pmap_update(pmap);
   1544 	}
   1545 
   1546 	/*
   1547 	 * Free any page tables still mapped
   1548 	 * This is only temporay until pmap_enter can count the number
   1549 	 * of mappings made in a page table. Then pmap_remove() can
   1550 	 * reduce the count and free the pagetable when the count
   1551 	 * reaches zero.  Note that entries in this list should match the
   1552 	 * contents of the ptpt, however this is faster than walking a 1024
   1553 	 * entries looking for pt's
   1554 	 * taken from i386 pmap.c
   1555 	 */
   1556 	while ((page = TAILQ_FIRST(&pmap->pm_obj.memq)) != NULL) {
   1557 		KASSERT((page->flags & PG_BUSY) == 0);
   1558 		page->wire_count = 0;
   1559 		uvm_pagefree(page);
   1560 	}
   1561 
   1562 	/* Free the page dir */
   1563 	pmap_freepagedir(pmap);
   1564 
   1565 	/* return the pmap to the pool */
   1566 	pool_put(&pmap_pmap_pool, pmap);
   1567 }
   1568 
   1569 
   1570 /*
   1571  * void pmap_reference(struct pmap *pmap)
   1572  *
   1573  * Add a reference to the specified pmap.
   1574  */
   1575 
   1576 void
   1577 pmap_reference(struct pmap *pmap)
   1578 {
   1579 	if (pmap == NULL)
   1580 		return;
   1581 
   1582 	simple_lock(&pmap->pm_lock);
   1583 	pmap->pm_obj.uo_refs++;
   1584 	simple_unlock(&pmap->pm_lock);
   1585 }
   1586 
   1587 /*
   1588  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   1589  *
   1590  * Return the start and end addresses of the kernel's virtual space.
   1591  * These values are setup in pmap_bootstrap and are updated as pages
   1592  * are allocated.
   1593  */
   1594 
   1595 void
   1596 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   1597 {
   1598 	*start = virtual_avail;
   1599 	*end = virtual_end;
   1600 }
   1601 
   1602 /*
   1603  * Activate the address space for the specified process.  If the process
   1604  * is the current process, load the new MMU context.
   1605  */
   1606 void
   1607 pmap_activate(struct proc *p)
   1608 {
   1609 	struct pmap *pmap = p->p_vmspace->vm_map.pmap;
   1610 	struct pcb *pcb = &p->p_addr->u_pcb;
   1611 
   1612 	(void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_pdir,
   1613 	    (paddr_t *)&pcb->pcb_pagedir);
   1614 
   1615 	PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
   1616 	    p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
   1617 
   1618 	if (p == curproc) {
   1619 		PDEBUG(0, printf("pmap_activate: setting TTB\n"));
   1620 		setttb((u_int)pcb->pcb_pagedir);
   1621 	}
   1622 }
   1623 
   1624 /*
   1625  * Deactivate the address space of the specified process.
   1626  */
   1627 void
   1628 pmap_deactivate(struct proc *p)
   1629 {
   1630 }
   1631 
   1632 /*
   1633  * Perform any deferred pmap operations.
   1634  */
   1635 void
   1636 pmap_update(struct pmap *pmap)
   1637 {
   1638 
   1639 	/*
   1640 	 * We haven't deferred any pmap operations, but we do need to
   1641 	 * make sure TLB/cache operations have completed.
   1642 	 */
   1643 	cpu_cpwait();
   1644 }
   1645 
   1646 /*
   1647  * pmap_clean_page()
   1648  *
   1649  * This is a local function used to work out the best strategy to clean
   1650  * a single page referenced by its entry in the PV table. It's used by
   1651  * pmap_copy_page, pmap_zero page and maybe some others later on.
   1652  *
   1653  * Its policy is effectively:
   1654  *  o If there are no mappings, we don't bother doing anything with the cache.
   1655  *  o If there is one mapping, we clean just that page.
   1656  *  o If there are multiple mappings, we clean the entire cache.
   1657  *
   1658  * So that some functions can be further optimised, it returns 0 if it didn't
   1659  * clean the entire cache, or 1 if it did.
   1660  *
   1661  * XXX One bug in this routine is that if the pv_entry has a single page
   1662  * mapped at 0x00000000 a whole cache clean will be performed rather than
   1663  * just the 1 page. Since this should not occur in everyday use and if it does
   1664  * it will just result in not the most efficient clean for the page.
   1665  */
   1666 static int
   1667 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
   1668 {
   1669 	struct pmap *pmap;
   1670 	struct pv_entry *npv;
   1671 	int cache_needs_cleaning = 0;
   1672 	vaddr_t page_to_clean = 0;
   1673 
   1674 	if (pv == NULL)
   1675 		/* nothing mapped in so nothing to flush */
   1676 		return (0);
   1677 
   1678 	/* Since we flush the cache each time we change curproc, we
   1679 	 * only need to flush the page if it is in the current pmap.
   1680 	 */
   1681 	if (curproc)
   1682 		pmap = curproc->p_vmspace->vm_map.pmap;
   1683 	else
   1684 		pmap = pmap_kernel();
   1685 
   1686 	for (npv = pv; npv; npv = npv->pv_next) {
   1687 		if (npv->pv_pmap == pmap) {
   1688 			/* The page is mapped non-cacheable in
   1689 			 * this map.  No need to flush the cache.
   1690 			 */
   1691 			if (npv->pv_flags & PVF_NC) {
   1692 #ifdef DIAGNOSTIC
   1693 				if (cache_needs_cleaning)
   1694 					panic("pmap_clean_page: "
   1695 							"cache inconsistency");
   1696 #endif
   1697 				break;
   1698 			}
   1699 #if 0
   1700 			/* This doesn't work, because pmap_protect
   1701 			   doesn't flush changes on pages that it
   1702 			   has write-protected.  */
   1703 
   1704 			/* If the page is not writable and this
   1705 			   is the source, then there is no need
   1706 			   to flush it from the cache.  */
   1707 			else if (is_src && ! (npv->pv_flags & PVF_WRITE))
   1708 				continue;
   1709 #endif
   1710 			if (cache_needs_cleaning){
   1711 				page_to_clean = 0;
   1712 				break;
   1713 			}
   1714 			else
   1715 				page_to_clean = npv->pv_va;
   1716 			cache_needs_cleaning = 1;
   1717 		}
   1718 	}
   1719 
   1720 	if (page_to_clean)
   1721 		cpu_idcache_wbinv_range(page_to_clean, NBPG);
   1722 	else if (cache_needs_cleaning) {
   1723 		cpu_idcache_wbinv_all();
   1724 		return (1);
   1725 	}
   1726 	return (0);
   1727 }
   1728 
   1729 /*
   1730  * pmap_zero_page()
   1731  *
   1732  * Zero a given physical page by mapping it at a page hook point.
   1733  * In doing the zero page op, the page we zero is mapped cachable, as with
   1734  * StrongARM accesses to non-cached pages are non-burst making writing
   1735  * _any_ bulk data very slow.
   1736  */
   1737 void
   1738 pmap_zero_page(paddr_t phys)
   1739 {
   1740 #ifdef DEBUG
   1741 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   1742 
   1743 	if (pg->mdpage.pvh_list != NULL)
   1744 		panic("pmap_zero_page: page has mappings");
   1745 #endif
   1746 
   1747 	KDASSERT((phys & PGOFSET) == 0);
   1748 
   1749 	/*
   1750 	 * Hook in the page, zero it, and purge the cache for that
   1751 	 * zeroed page. Invalidate the TLB as needed.
   1752 	 */
   1753 	*cdst_pte = L2_S_PROTO | phys |
   1754 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   1755 	cpu_tlb_flushD_SE(cdstp);
   1756 	cpu_cpwait();
   1757 	bzero_page(cdstp);
   1758 	cpu_dcache_wbinv_range(cdstp, NBPG);
   1759 }
   1760 
   1761 /* pmap_pageidlezero()
   1762  *
   1763  * The same as above, except that we assume that the page is not
   1764  * mapped.  This means we never have to flush the cache first.  Called
   1765  * from the idle loop.
   1766  */
   1767 boolean_t
   1768 pmap_pageidlezero(paddr_t phys)
   1769 {
   1770 	int i, *ptr;
   1771 	boolean_t rv = TRUE;
   1772 #ifdef DEBUG
   1773 	struct vm_page *pg;
   1774 
   1775 	pg = PHYS_TO_VM_PAGE(phys);
   1776 	if (pg->mdpage.pvh_list != NULL)
   1777 		panic("pmap_pageidlezero: page has mappings");
   1778 #endif
   1779 
   1780 	KDASSERT((phys & PGOFSET) == 0);
   1781 
   1782 	/*
   1783 	 * Hook in the page, zero it, and purge the cache for that
   1784 	 * zeroed page. Invalidate the TLB as needed.
   1785 	 */
   1786 	*cdst_pte = L2_S_PROTO | phys |
   1787 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   1788 	cpu_tlb_flushD_SE(cdstp);
   1789 	cpu_cpwait();
   1790 
   1791 	for (i = 0, ptr = (int *)cdstp;
   1792 			i < (NBPG / sizeof(int)); i++) {
   1793 		if (sched_whichqs != 0) {
   1794 			/*
   1795 			 * A process has become ready.  Abort now,
   1796 			 * so we don't keep it waiting while we
   1797 			 * do slow memory access to finish this
   1798 			 * page.
   1799 			 */
   1800 			rv = FALSE;
   1801 			break;
   1802 		}
   1803 		*ptr++ = 0;
   1804 	}
   1805 
   1806 	if (rv)
   1807 		/*
   1808 		 * if we aborted we'll rezero this page again later so don't
   1809 		 * purge it unless we finished it
   1810 		 */
   1811 		cpu_dcache_wbinv_range(cdstp, NBPG);
   1812 	return (rv);
   1813 }
   1814 
   1815 /*
   1816  * pmap_copy_page()
   1817  *
   1818  * Copy one physical page into another, by mapping the pages into
   1819  * hook points. The same comment regarding cachability as in
   1820  * pmap_zero_page also applies here.
   1821  */
   1822 void
   1823 pmap_copy_page(paddr_t src, paddr_t dst)
   1824 {
   1825 	struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
   1826 #ifdef DEBUG
   1827 	struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
   1828 
   1829 	if (dst_pg->mdpage.pvh_list != NULL)
   1830 		panic("pmap_copy_page: dst page has mappings");
   1831 #endif
   1832 
   1833 	KDASSERT((src & PGOFSET) == 0);
   1834 	KDASSERT((dst & PGOFSET) == 0);
   1835 
   1836 	/*
   1837 	 * Clean the source page.  Hold the source page's lock for
   1838 	 * the duration of the copy so that no other mappings can
   1839 	 * be created while we have a potentially aliased mapping.
   1840 	 */
   1841 	simple_lock(&src_pg->mdpage.pvh_slock);
   1842 	(void) pmap_clean_page(src_pg->mdpage.pvh_list, TRUE);
   1843 
   1844 	/*
   1845 	 * Map the pages into the page hook points, copy them, and purge
   1846 	 * the cache for the appropriate page. Invalidate the TLB
   1847 	 * as required.
   1848 	 */
   1849 	*csrc_pte = L2_S_PROTO | src |
   1850 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
   1851 	*cdst_pte = L2_S_PROTO | dst |
   1852 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   1853 	cpu_tlb_flushD_SE(csrcp);
   1854 	cpu_tlb_flushD_SE(cdstp);
   1855 	cpu_cpwait();
   1856 	bcopy_page(csrcp, cdstp);
   1857 	cpu_dcache_inv_range(csrcp, NBPG);
   1858 	simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
   1859 	cpu_dcache_wbinv_range(cdstp, NBPG);
   1860 }
   1861 
   1862 #if 0
   1863 void
   1864 pmap_pte_addref(struct pmap *pmap, vaddr_t va)
   1865 {
   1866 	pd_entry_t *pde;
   1867 	paddr_t pa;
   1868 	struct vm_page *m;
   1869 
   1870 	if (pmap == pmap_kernel())
   1871 		return;
   1872 
   1873 	pde = pmap_pde(pmap, va & ~(3 << L1_S_SHIFT));
   1874 	pa = pmap_pte_pa(pde);
   1875 	m = PHYS_TO_VM_PAGE(pa);
   1876 	++m->wire_count;
   1877 #ifdef MYCROFT_HACK
   1878 	printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   1879 	    pmap, va, pde, pa, m, m->wire_count);
   1880 #endif
   1881 }
   1882 
   1883 void
   1884 pmap_pte_delref(struct pmap *pmap, vaddr_t va)
   1885 {
   1886 	pd_entry_t *pde;
   1887 	paddr_t pa;
   1888 	struct vm_page *m;
   1889 
   1890 	if (pmap == pmap_kernel())
   1891 		return;
   1892 
   1893 	pde = pmap_pde(pmap, va & ~(3 << L1_S_SHIFT));
   1894 	pa = pmap_pte_pa(pde);
   1895 	m = PHYS_TO_VM_PAGE(pa);
   1896 	--m->wire_count;
   1897 #ifdef MYCROFT_HACK
   1898 	printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   1899 	    pmap, va, pde, pa, m, m->wire_count);
   1900 #endif
   1901 	if (m->wire_count == 0) {
   1902 #ifdef MYCROFT_HACK
   1903 		printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
   1904 		    pmap, va, pde, pa, m);
   1905 #endif
   1906 		pmap_unmap_in_l1(pmap, va);
   1907 		uvm_pagefree(m);
   1908 		--pmap->pm_stats.resident_count;
   1909 	}
   1910 }
   1911 #else
   1912 #define	pmap_pte_addref(pmap, va)
   1913 #define	pmap_pte_delref(pmap, va)
   1914 #endif
   1915 
   1916 /*
   1917  * Since we have a virtually indexed cache, we may need to inhibit caching if
   1918  * there is more than one mapping and at least one of them is writable.
   1919  * Since we purge the cache on every context switch, we only need to check for
   1920  * other mappings within the same pmap, or kernel_pmap.
   1921  * This function is also called when a page is unmapped, to possibly reenable
   1922  * caching on any remaining mappings.
   1923  *
   1924  * The code implements the following logic, where:
   1925  *
   1926  * KW = # of kernel read/write pages
   1927  * KR = # of kernel read only pages
   1928  * UW = # of user read/write pages
   1929  * UR = # of user read only pages
   1930  * OW = # of user read/write pages in another pmap, then
   1931  *
   1932  * KC = kernel mapping is cacheable
   1933  * UC = user mapping is cacheable
   1934  *
   1935  *                     KW=0,KR=0  KW=0,KR>0  KW=1,KR=0  KW>1,KR>=0
   1936  *                   +---------------------------------------------
   1937  * UW=0,UR=0,OW=0    | ---        KC=1       KC=1       KC=0
   1938  * UW=0,UR>0,OW=0    | UC=1       KC=1,UC=1  KC=0,UC=0  KC=0,UC=0
   1939  * UW=0,UR>0,OW>0    | UC=1       KC=0,UC=1  KC=0,UC=0  KC=0,UC=0
   1940  * UW=1,UR=0,OW=0    | UC=1       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1941  * UW>1,UR>=0,OW>=0  | UC=0       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1942  *
   1943  * Note that the pmap must have it's ptes mapped in, and passed with ptes.
   1944  */
   1945 __inline static void
   1946 pmap_vac_me_harder(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
   1947 	boolean_t clear_cache)
   1948 {
   1949 	if (pmap == pmap_kernel())
   1950 		pmap_vac_me_kpmap(pmap, pg, ptes, clear_cache);
   1951 	else
   1952 		pmap_vac_me_user(pmap, pg, ptes, clear_cache);
   1953 }
   1954 
   1955 static void
   1956 pmap_vac_me_kpmap(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
   1957 	boolean_t clear_cache)
   1958 {
   1959 	int user_entries = 0;
   1960 	int user_writable = 0;
   1961 	int user_cacheable = 0;
   1962 	int kernel_entries = 0;
   1963 	int kernel_writable = 0;
   1964 	int kernel_cacheable = 0;
   1965 	struct pv_entry *pv;
   1966 	struct pmap *last_pmap = pmap;
   1967 
   1968 #ifdef DIAGNOSTIC
   1969 	if (pmap != pmap_kernel())
   1970 		panic("pmap_vac_me_kpmap: pmap != pmap_kernel()");
   1971 #endif
   1972 
   1973 	/*
   1974 	 * Pass one, see if there are both kernel and user pmaps for
   1975 	 * this page.  Calculate whether there are user-writable or
   1976 	 * kernel-writable pages.
   1977 	 */
   1978 	for (pv = pg->mdpage.pvh_list; pv != NULL; pv = pv->pv_next) {
   1979 		if (pv->pv_pmap != pmap) {
   1980 			user_entries++;
   1981 			if (pv->pv_flags & PVF_WRITE)
   1982 				user_writable++;
   1983 			if ((pv->pv_flags & PVF_NC) == 0)
   1984 				user_cacheable++;
   1985 		} else {
   1986 			kernel_entries++;
   1987 			if (pv->pv_flags & PVF_WRITE)
   1988 				kernel_writable++;
   1989 			if ((pv->pv_flags & PVF_NC) == 0)
   1990 				kernel_cacheable++;
   1991 		}
   1992 	}
   1993 
   1994 	/*
   1995 	 * We know we have just been updating a kernel entry, so if
   1996 	 * all user pages are already cacheable, then there is nothing
   1997 	 * further to do.
   1998 	 */
   1999 	if (kernel_entries == 0 &&
   2000 	    user_cacheable == user_entries)
   2001 		return;
   2002 
   2003 	if (user_entries) {
   2004 		/*
   2005 		 * Scan over the list again, for each entry, if it
   2006 		 * might not be set correctly, call pmap_vac_me_user
   2007 		 * to recalculate the settings.
   2008 		 */
   2009 		for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   2010 			/*
   2011 			 * We know kernel mappings will get set
   2012 			 * correctly in other calls.  We also know
   2013 			 * that if the pmap is the same as last_pmap
   2014 			 * then we've just handled this entry.
   2015 			 */
   2016 			if (pv->pv_pmap == pmap || pv->pv_pmap == last_pmap)
   2017 				continue;
   2018 			/*
   2019 			 * If there are kernel entries and this page
   2020 			 * is writable but non-cacheable, then we can
   2021 			 * skip this entry also.
   2022 			 */
   2023 			if (kernel_entries > 0 &&
   2024 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
   2025 			    (PVF_NC | PVF_WRITE))
   2026 				continue;
   2027 			/*
   2028 			 * Similarly if there are no kernel-writable
   2029 			 * entries and the page is already
   2030 			 * read-only/cacheable.
   2031 			 */
   2032 			if (kernel_writable == 0 &&
   2033 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
   2034 				continue;
   2035 			/*
   2036 			 * For some of the remaining cases, we know
   2037 			 * that we must recalculate, but for others we
   2038 			 * can't tell if they are correct or not, so
   2039 			 * we recalculate anyway.
   2040 			 */
   2041 			pmap_unmap_ptes(last_pmap);
   2042 			last_pmap = pv->pv_pmap;
   2043 			ptes = pmap_map_ptes(last_pmap);
   2044 			pmap_vac_me_user(last_pmap, pg, ptes,
   2045 			    pmap_is_curpmap(last_pmap));
   2046 		}
   2047 		/* Restore the pte mapping that was passed to us.  */
   2048 		if (last_pmap != pmap) {
   2049 			pmap_unmap_ptes(last_pmap);
   2050 			ptes = pmap_map_ptes(pmap);
   2051 		}
   2052 		if (kernel_entries == 0)
   2053 			return;
   2054 	}
   2055 
   2056 	pmap_vac_me_user(pmap, pg, ptes, clear_cache);
   2057 	return;
   2058 }
   2059 
   2060 static void
   2061 pmap_vac_me_user(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
   2062 	boolean_t clear_cache)
   2063 {
   2064 	struct pmap *kpmap = pmap_kernel();
   2065 	struct pv_entry *pv, *npv;
   2066 	int entries = 0;
   2067 	int writable = 0;
   2068 	int cacheable_entries = 0;
   2069 	int kern_cacheable = 0;
   2070 	int other_writable = 0;
   2071 
   2072 	pv = pg->mdpage.pvh_list;
   2073 	KASSERT(ptes != NULL);
   2074 
   2075 	/*
   2076 	 * Count mappings and writable mappings in this pmap.
   2077 	 * Include kernel mappings as part of our own.
   2078 	 * Keep a pointer to the first one.
   2079 	 */
   2080 	for (npv = pv; npv; npv = npv->pv_next) {
   2081 		/* Count mappings in the same pmap */
   2082 		if (pmap == npv->pv_pmap ||
   2083 		    kpmap == npv->pv_pmap) {
   2084 			if (entries++ == 0)
   2085 				pv = npv;
   2086 			/* Cacheable mappings */
   2087 			if ((npv->pv_flags & PVF_NC) == 0) {
   2088 				cacheable_entries++;
   2089 				if (kpmap == npv->pv_pmap)
   2090 					kern_cacheable++;
   2091 			}
   2092 			/* Writable mappings */
   2093 			if (npv->pv_flags & PVF_WRITE)
   2094 				++writable;
   2095 		} else if (npv->pv_flags & PVF_WRITE)
   2096 			other_writable = 1;
   2097 	}
   2098 
   2099 	PDEBUG(3,printf("pmap_vac_me_harder: pmap %p Entries %d, "
   2100 		"writable %d cacheable %d %s\n", pmap, entries, writable,
   2101 	    	cacheable_entries, clear_cache ? "clean" : "no clean"));
   2102 
   2103 	/*
   2104 	 * Enable or disable caching as necessary.
   2105 	 * Note: the first entry might be part of the kernel pmap,
   2106 	 * so we can't assume this is indicative of the state of the
   2107 	 * other (maybe non-kpmap) entries.
   2108 	 */
   2109 	if ((entries > 1 && writable) ||
   2110 	    (entries > 0 && pmap == kpmap && other_writable)) {
   2111 		if (cacheable_entries == 0)
   2112 		    return;
   2113 		for (npv = pv; npv; npv = npv->pv_next) {
   2114 			if ((pmap == npv->pv_pmap
   2115 			    || kpmap == npv->pv_pmap) &&
   2116 			    (npv->pv_flags & PVF_NC) == 0) {
   2117 				ptes[arm_btop(npv->pv_va)] &= ~(L2_C | L2_B);
   2118  				npv->pv_flags |= PVF_NC;
   2119 				/*
   2120 				 * If this page needs flushing from the
   2121 				 * cache, and we aren't going to do it
   2122 				 * below, do it now.
   2123 				 */
   2124 				if ((cacheable_entries < 4 &&
   2125 				    (clear_cache || npv->pv_pmap == kpmap)) ||
   2126 				    (npv->pv_pmap == kpmap &&
   2127 				    !clear_cache && kern_cacheable < 4)) {
   2128 					cpu_idcache_wbinv_range(npv->pv_va,
   2129 					    NBPG);
   2130 					cpu_tlb_flushID_SE(npv->pv_va);
   2131 				}
   2132 			}
   2133 		}
   2134 		if ((clear_cache && cacheable_entries >= 4) ||
   2135 		    kern_cacheable >= 4) {
   2136 			cpu_idcache_wbinv_all();
   2137 			cpu_tlb_flushID();
   2138 		}
   2139 		cpu_cpwait();
   2140 	} else if (entries > 0) {
   2141 		/*
   2142 		 * Turn cacheing back on for some pages.  If it is a kernel
   2143 		 * page, only do so if there are no other writable pages.
   2144 		 */
   2145 		for (npv = pv; npv; npv = npv->pv_next) {
   2146 			if ((pmap == npv->pv_pmap ||
   2147 			    (kpmap == npv->pv_pmap && other_writable == 0)) &&
   2148 			    (npv->pv_flags & PVF_NC)) {
   2149 				ptes[arm_btop(npv->pv_va)] |=
   2150 				    pte_l2_s_cache_mode;
   2151 				npv->pv_flags &= ~PVF_NC;
   2152 			}
   2153 		}
   2154 	}
   2155 }
   2156 
   2157 /*
   2158  * pmap_remove()
   2159  *
   2160  * pmap_remove is responsible for nuking a number of mappings for a range
   2161  * of virtual address space in the current pmap. To do this efficiently
   2162  * is interesting, because in a number of cases a wide virtual address
   2163  * range may be supplied that contains few actual mappings. So, the
   2164  * optimisations are:
   2165  *  1. Try and skip over hunks of address space for which an L1 entry
   2166  *     does not exist.
   2167  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   2168  *     maybe do just a partial cache clean. This path of execution is
   2169  *     complicated by the fact that the cache must be flushed _before_
   2170  *     the PTE is nuked, being a VAC :-)
   2171  *  3. Maybe later fast-case a single page, but I don't think this is
   2172  *     going to make _that_ much difference overall.
   2173  */
   2174 
   2175 #define PMAP_REMOVE_CLEAN_LIST_SIZE	3
   2176 
   2177 void
   2178 pmap_remove(struct pmap *pmap, vaddr_t sva, vaddr_t eva)
   2179 {
   2180 	int cleanlist_idx = 0;
   2181 	struct pagelist {
   2182 		vaddr_t va;
   2183 		pt_entry_t *pte;
   2184 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   2185 	pt_entry_t *pte = 0, *ptes;
   2186 	paddr_t pa;
   2187 	int pmap_active;
   2188 	struct vm_page *pg;
   2189 
   2190 	/* Exit quick if there is no pmap */
   2191 	if (!pmap)
   2192 		return;
   2193 
   2194 	PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n",
   2195 	    pmap, sva, eva));
   2196 
   2197 	/*
   2198 	 * we lock in the pmap => vm_page direction
   2199 	 */
   2200 	PMAP_MAP_TO_HEAD_LOCK();
   2201 
   2202 	ptes = pmap_map_ptes(pmap);
   2203 	/* Get a page table pointer */
   2204 	while (sva < eva) {
   2205 		if (pmap_pde_page(pmap_pde(pmap, sva)))
   2206 			break;
   2207 		sva = (sva & L1_S_FRAME) + L1_S_SIZE;
   2208 	}
   2209 
   2210 	pte = &ptes[arm_btop(sva)];
   2211 	/* Note if the pmap is active thus require cache and tlb cleans */
   2212 	pmap_active = pmap_is_curpmap(pmap);
   2213 
   2214 	/* Now loop along */
   2215 	while (sva < eva) {
   2216 		/* Check if we can move to the next PDE (l1 chunk) */
   2217 		if (!(sva & L2_ADDR_BITS))
   2218 			if (!pmap_pde_page(pmap_pde(pmap, sva))) {
   2219 				sva += L1_S_SIZE;
   2220 				pte += arm_btop(L1_S_SIZE);
   2221 				continue;
   2222 			}
   2223 
   2224 		/* We've found a valid PTE, so this page of PTEs has to go. */
   2225 		if (pmap_pte_v(pte)) {
   2226 			/* Update statistics */
   2227 			--pmap->pm_stats.resident_count;
   2228 
   2229 			/*
   2230 			 * Add this page to our cache remove list, if we can.
   2231 			 * If, however the cache remove list is totally full,
   2232 			 * then do a complete cache invalidation taking note
   2233 			 * to backtrack the PTE table beforehand, and ignore
   2234 			 * the lists in future because there's no longer any
   2235 			 * point in bothering with them (we've paid the
   2236 			 * penalty, so will carry on unhindered). Otherwise,
   2237 			 * when we fall out, we just clean the list.
   2238 			 */
   2239 			PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
   2240 			pa = pmap_pte_pa(pte);
   2241 
   2242 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2243 				/* Add to the clean list. */
   2244 				cleanlist[cleanlist_idx].pte = pte;
   2245 				cleanlist[cleanlist_idx].va = sva;
   2246 				cleanlist_idx++;
   2247 			} else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2248 				int cnt;
   2249 
   2250 				/* Nuke everything if needed. */
   2251 				if (pmap_active) {
   2252 					cpu_idcache_wbinv_all();
   2253 					cpu_tlb_flushID();
   2254 				}
   2255 
   2256 				/*
   2257 				 * Roll back the previous PTE list,
   2258 				 * and zero out the current PTE.
   2259 				 */
   2260 				for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   2261 					*cleanlist[cnt].pte = 0;
   2262 					pmap_pte_delref(pmap, cleanlist[cnt].va);
   2263 				}
   2264 				*pte = 0;
   2265 				pmap_pte_delref(pmap, sva);
   2266 				cleanlist_idx++;
   2267 			} else {
   2268 				/*
   2269 				 * We've already nuked the cache and
   2270 				 * TLB, so just carry on regardless,
   2271 				 * and we won't need to do it again
   2272 				 */
   2273 				*pte = 0;
   2274 				pmap_pte_delref(pmap, sva);
   2275 			}
   2276 
   2277 			/*
   2278 			 * Update flags. In a number of circumstances,
   2279 			 * we could cluster a lot of these and do a
   2280 			 * number of sequential pages in one go.
   2281 			 */
   2282 			if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   2283 				struct pv_entry *pve;
   2284 				simple_lock(&pg->mdpage.pvh_slock);
   2285 				pve = pmap_remove_pv(pg, pmap, sva);
   2286 				pmap_free_pv(pmap, pve);
   2287 				pmap_vac_me_harder(pmap, pg, ptes, FALSE);
   2288 				simple_unlock(&pg->mdpage.pvh_slock);
   2289 			}
   2290 		}
   2291 		sva += NBPG;
   2292 		pte++;
   2293 	}
   2294 
   2295 	pmap_unmap_ptes(pmap);
   2296 	/*
   2297 	 * Now, if we've fallen through down to here, chances are that there
   2298 	 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
   2299 	 */
   2300 	if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2301 		u_int cnt;
   2302 
   2303 		for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   2304 			if (pmap_active) {
   2305 				cpu_idcache_wbinv_range(cleanlist[cnt].va,
   2306 				    NBPG);
   2307 				*cleanlist[cnt].pte = 0;
   2308 				cpu_tlb_flushID_SE(cleanlist[cnt].va);
   2309 			} else
   2310 				*cleanlist[cnt].pte = 0;
   2311 			pmap_pte_delref(pmap, cleanlist[cnt].va);
   2312 		}
   2313 	}
   2314 	PMAP_MAP_TO_HEAD_UNLOCK();
   2315 }
   2316 
   2317 /*
   2318  * Routine:	pmap_remove_all
   2319  * Function:
   2320  *		Removes this physical page from
   2321  *		all physical maps in which it resides.
   2322  *		Reflects back modify bits to the pager.
   2323  */
   2324 
   2325 static void
   2326 pmap_remove_all(struct vm_page *pg)
   2327 {
   2328 	struct pv_entry *pv, *npv;
   2329 	struct pmap *pmap;
   2330 	pt_entry_t *pte, *ptes;
   2331 
   2332 	PDEBUG(0, printf("pmap_remove_all: pa=%lx ", VM_PAGE_TO_PHYS(pg)));
   2333 
   2334 	/* set vm_page => pmap locking */
   2335 	PMAP_HEAD_TO_MAP_LOCK();
   2336 
   2337 	simple_lock(&pg->mdpage.pvh_slock);
   2338 
   2339 	pv = pg->mdpage.pvh_list;
   2340 	if (pv == NULL) {
   2341 		PDEBUG(0, printf("free page\n"));
   2342 		simple_unlock(&pg->mdpage.pvh_slock);
   2343 		PMAP_HEAD_TO_MAP_UNLOCK();
   2344 		return;
   2345 	}
   2346 	pmap_clean_page(pv, FALSE);
   2347 
   2348 	while (pv) {
   2349 		pmap = pv->pv_pmap;
   2350 		ptes = pmap_map_ptes(pmap);
   2351 		pte = &ptes[arm_btop(pv->pv_va)];
   2352 
   2353 		PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
   2354 		    pv->pv_va, pv->pv_flags));
   2355 #ifdef DEBUG
   2356 		if (pmap_pde_page(pmap_pde(pmap, pv->pv_va)) == 0 ||
   2357 		    pmap_pte_v(pte) == 0 ||
   2358 		    pmap_pte_pa(pte) != VM_PAGE_TO_PHYS(pg))
   2359 			panic("pmap_remove_all: bad mapping");
   2360 #endif	/* DEBUG */
   2361 
   2362 		/*
   2363 		 * Update statistics
   2364 		 */
   2365 		--pmap->pm_stats.resident_count;
   2366 
   2367 		/* Wired bit */
   2368 		if (pv->pv_flags & PVF_WIRED)
   2369 			--pmap->pm_stats.wired_count;
   2370 
   2371 		/*
   2372 		 * Invalidate the PTEs.
   2373 		 * XXX: should cluster them up and invalidate as many
   2374 		 * as possible at once.
   2375 		 */
   2376 
   2377 #ifdef needednotdone
   2378 reduce wiring count on page table pages as references drop
   2379 #endif
   2380 
   2381 		*pte = 0;
   2382 		pmap_pte_delref(pmap, pv->pv_va);
   2383 
   2384 		npv = pv->pv_next;
   2385 		pmap_free_pv(pmap, pv);
   2386 		pv = npv;
   2387 		pmap_unmap_ptes(pmap);
   2388 	}
   2389 	pg->mdpage.pvh_list = NULL;
   2390 	simple_unlock(&pg->mdpage.pvh_slock);
   2391 	PMAP_HEAD_TO_MAP_UNLOCK();
   2392 
   2393 	PDEBUG(0, printf("done\n"));
   2394 	cpu_tlb_flushID();
   2395 	cpu_cpwait();
   2396 }
   2397 
   2398 
   2399 /*
   2400  * Set the physical protection on the specified range of this map as requested.
   2401  */
   2402 
   2403 void
   2404 pmap_protect(struct pmap *pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   2405 {
   2406 	pt_entry_t *pte = NULL, *ptes;
   2407 	struct vm_page *pg;
   2408 	int armprot;
   2409 	int flush = 0;
   2410 	paddr_t pa;
   2411 
   2412 	PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
   2413 	    pmap, sva, eva, prot));
   2414 
   2415 	if (~prot & VM_PROT_READ) {
   2416 		/* Just remove the mappings. */
   2417 		pmap_remove(pmap, sva, eva);
   2418 		/* pmap_update not needed as it should be called by the caller
   2419 		 * of pmap_protect */
   2420 		return;
   2421 	}
   2422 	if (prot & VM_PROT_WRITE) {
   2423 		/*
   2424 		 * If this is a read->write transition, just ignore it and let
   2425 		 * uvm_fault() take care of it later.
   2426 		 */
   2427 		return;
   2428 	}
   2429 
   2430 	/* Need to lock map->head */
   2431 	PMAP_MAP_TO_HEAD_LOCK();
   2432 
   2433 	ptes = pmap_map_ptes(pmap);
   2434 	/*
   2435 	 * We need to acquire a pointer to a page table page before entering
   2436 	 * the following loop.
   2437 	 */
   2438 	while (sva < eva) {
   2439 		if (pmap_pde_page(pmap_pde(pmap, sva)))
   2440 			break;
   2441 		sva = (sva & L1_S_FRAME) + L1_S_SIZE;
   2442 	}
   2443 
   2444 	pte = &ptes[arm_btop(sva)];
   2445 
   2446 	while (sva < eva) {
   2447 		/* only check once in a while */
   2448 		if ((sva & L2_ADDR_BITS) == 0) {
   2449 			if (!pmap_pde_page(pmap_pde(pmap, sva))) {
   2450 				/* We can race ahead here, to the next pde. */
   2451 				sva += L1_S_SIZE;
   2452 				pte += arm_btop(L1_S_SIZE);
   2453 				continue;
   2454 			}
   2455 		}
   2456 
   2457 		if (!pmap_pte_v(pte))
   2458 			goto next;
   2459 
   2460 		flush = 1;
   2461 
   2462 		armprot = 0;
   2463 		if (sva < VM_MAXUSER_ADDRESS)
   2464 			armprot |= L2_S_PROT_U;
   2465 		else if (sva < VM_MAX_ADDRESS)
   2466 			armprot |= L2_S_PROT_W;  /* XXX Ekk what is this ? */
   2467 		*pte = (*pte & 0xfffff00f) | armprot;
   2468 
   2469 		pa = pmap_pte_pa(pte);
   2470 
   2471 		/* Get the physical page index */
   2472 
   2473 		/* Clear write flag */
   2474 		if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   2475 			simple_lock(&pg->mdpage.pvh_slock);
   2476 			(void) pmap_modify_pv(pmap, sva, pg, PVF_WRITE, 0);
   2477 			pmap_vac_me_harder(pmap, pg, ptes, FALSE);
   2478 			simple_unlock(&pg->mdpage.pvh_slock);
   2479 		}
   2480 
   2481 next:
   2482 		sva += NBPG;
   2483 		pte++;
   2484 	}
   2485 	pmap_unmap_ptes(pmap);
   2486 	PMAP_MAP_TO_HEAD_UNLOCK();
   2487 	if (flush)
   2488 		cpu_tlb_flushID();
   2489 }
   2490 
   2491 /*
   2492  * void pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2493  * int flags)
   2494  *
   2495  *      Insert the given physical page (p) at
   2496  *      the specified virtual address (v) in the
   2497  *      target physical map with the protection requested.
   2498  *
   2499  *      If specified, the page will be wired down, meaning
   2500  *      that the related pte can not be reclaimed.
   2501  *
   2502  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2503  *      or lose information.  That is, this routine must actually
   2504  *      insert this page into the given map NOW.
   2505  */
   2506 
   2507 int
   2508 pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2509     int flags)
   2510 {
   2511 	pt_entry_t *ptes, opte, npte;
   2512 	paddr_t opa;
   2513 	boolean_t wired = (flags & PMAP_WIRED) != 0;
   2514 	struct vm_page *pg;
   2515 	struct pv_entry *pve;
   2516 	int error, nflags;
   2517 
   2518 	PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
   2519 	    va, pa, pmap, prot, wired));
   2520 
   2521 #ifdef DIAGNOSTIC
   2522 	/* Valid address ? */
   2523 	if (va >= (pmap_curmaxkvaddr))
   2524 		panic("pmap_enter: too big");
   2525 	if (pmap != pmap_kernel() && va != 0) {
   2526 		if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
   2527 			panic("pmap_enter: kernel page in user map");
   2528 	} else {
   2529 		if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
   2530 			panic("pmap_enter: user page in kernel map");
   2531 		if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
   2532 			panic("pmap_enter: entering PT page");
   2533 	}
   2534 #endif
   2535 
   2536 	KDASSERT(((va | pa) & PGOFSET) == 0);
   2537 
   2538 	/*
   2539 	 * Get a pointer to the page.  Later on in this function, we
   2540 	 * test for a managed page by checking pg != NULL.
   2541 	 */
   2542 	pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
   2543 
   2544 	/* get lock */
   2545 	PMAP_MAP_TO_HEAD_LOCK();
   2546 
   2547 	/*
   2548 	 * map the ptes.  If there's not already an L2 table for this
   2549 	 * address, allocate one.
   2550 	 */
   2551 	ptes = pmap_map_ptes(pmap);		/* locks pmap */
   2552 	if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
   2553 		struct vm_page *ptp;
   2554 
   2555 		/* kernel should be pre-grown */
   2556 		KASSERT(pmap != pmap_kernel());
   2557 
   2558 		/* if failure is allowed then don't try too hard */
   2559 		ptp = pmap_get_ptp(pmap, va & L1_S_FRAME);
   2560 		if (ptp == NULL) {
   2561 			if (flags & PMAP_CANFAIL) {
   2562 				error = ENOMEM;
   2563 				goto out;
   2564 			}
   2565 			panic("pmap_enter: get ptp failed");
   2566 		}
   2567 	}
   2568 	opte = ptes[arm_btop(va)];
   2569 
   2570 	nflags = 0;
   2571 	if (prot & VM_PROT_WRITE)
   2572 		nflags |= PVF_WRITE;
   2573 	if (wired)
   2574 		nflags |= PVF_WIRED;
   2575 
   2576 	/* Is the pte valid ? If so then this page is already mapped */
   2577 	if (l2pte_valid(opte)) {
   2578 		/* Get the physical address of the current page mapped */
   2579 		opa = l2pte_pa(opte);
   2580 
   2581 		/* Are we mapping the same page ? */
   2582 		if (opa == pa) {
   2583 			/* Has the wiring changed ? */
   2584 			if (pg != NULL) {
   2585 				simple_lock(&pg->mdpage.pvh_slock);
   2586 				(void) pmap_modify_pv(pmap, va, pg,
   2587 				    PVF_WRITE | PVF_WIRED, nflags);
   2588 				simple_unlock(&pg->mdpage.pvh_slock);
   2589  			}
   2590 		} else {
   2591 			struct vm_page *opg;
   2592 
   2593 			/* We are replacing the page with a new one. */
   2594 			cpu_idcache_wbinv_range(va, NBPG);
   2595 
   2596 			/*
   2597 			 * If it is part of our managed memory then we
   2598 			 * must remove it from the PV list
   2599 			 */
   2600 			if ((opg = PHYS_TO_VM_PAGE(opa)) != NULL) {
   2601 				simple_lock(&opg->mdpage.pvh_slock);
   2602 				pve = pmap_remove_pv(opg, pmap, va);
   2603 				simple_unlock(&opg->mdpage.pvh_slock);
   2604 			} else {
   2605 				pve = NULL;
   2606 			}
   2607 
   2608 			goto enter;
   2609 		}
   2610 	} else {
   2611 		opa = 0;
   2612 		pve = NULL;
   2613 		pmap_pte_addref(pmap, va);
   2614 
   2615 		/* pte is not valid so we must be hooking in a new page */
   2616 		++pmap->pm_stats.resident_count;
   2617 
   2618 	enter:
   2619 		/*
   2620 		 * Enter on the PV list if part of our managed memory
   2621 		 */
   2622 		if (pg != NULL) {
   2623 			if (pve == NULL) {
   2624 				pve = pmap_alloc_pv(pmap, ALLOCPV_NEED);
   2625 				if (pve == NULL) {
   2626 					if (flags & PMAP_CANFAIL) {
   2627 						error = ENOMEM;
   2628 						goto out;
   2629 					}
   2630 					panic("pmap_enter: no pv entries "
   2631 					    "available");
   2632 				}
   2633 			}
   2634 			/* enter_pv locks pvh when adding */
   2635 			pmap_enter_pv(pg, pve, pmap, va, NULL, nflags);
   2636 		} else {
   2637 			if (pve != NULL)
   2638 				pmap_free_pv(pmap, pve);
   2639 		}
   2640 	}
   2641 
   2642 	/* Construct the pte, giving the correct access. */
   2643 	npte = pa;
   2644 
   2645 	/* VA 0 is magic. */
   2646 	if (pmap != pmap_kernel() && va != vector_page)
   2647 		npte |= L2_S_PROT_U;
   2648 
   2649 	if (pg != NULL) {
   2650 #ifdef DIAGNOSTIC
   2651 		if ((flags & VM_PROT_ALL) & ~prot)
   2652 			panic("pmap_enter: access_type exceeds prot");
   2653 #endif
   2654 		npte |= pte_l2_s_cache_mode;
   2655 		if (flags & VM_PROT_WRITE) {
   2656 			npte |= L2_S_PROTO | L2_S_PROT_W;
   2657 			pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
   2658 		} else if (flags & VM_PROT_ALL) {
   2659 			npte |= L2_S_PROTO;
   2660 			pg->mdpage.pvh_attrs |= PVF_REF;
   2661 		} else
   2662 			npte |= L2_TYPE_INV;
   2663 	} else {
   2664 		if (prot & VM_PROT_WRITE)
   2665 			npte |= L2_S_PROTO | L2_S_PROT_W;
   2666 		else if (prot & VM_PROT_ALL)
   2667 			npte |= L2_S_PROTO;
   2668 		else
   2669 			npte |= L2_TYPE_INV;
   2670 	}
   2671 
   2672 	ptes[arm_btop(va)] = npte;
   2673 
   2674 	if (pg != NULL) {
   2675 		simple_lock(&pg->mdpage.pvh_slock);
   2676  		pmap_vac_me_harder(pmap, pg, ptes, pmap_is_curpmap(pmap));
   2677 		simple_unlock(&pg->mdpage.pvh_slock);
   2678 	}
   2679 
   2680 	/* Better flush the TLB ... */
   2681 	cpu_tlb_flushID_SE(va);
   2682 	error = 0;
   2683 out:
   2684 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   2685 	PMAP_MAP_TO_HEAD_UNLOCK();
   2686 
   2687 	return error;
   2688 }
   2689 
   2690 /*
   2691  * pmap_kenter_pa: enter a kernel mapping
   2692  *
   2693  * => no need to lock anything assume va is already allocated
   2694  * => should be faster than normal pmap enter function
   2695  */
   2696 void
   2697 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
   2698 {
   2699 	pt_entry_t *pte;
   2700 
   2701 	pte = vtopte(va);
   2702 	KASSERT(!pmap_pte_v(pte));
   2703 
   2704 	/* XXX r/w! */
   2705 	*pte = L2_S_PROTO | pa |
   2706 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE) |
   2707 	    pte_l2_s_cache_mode;
   2708 }
   2709 
   2710 void
   2711 pmap_kremove(vaddr_t va, vsize_t len)
   2712 {
   2713 	pt_entry_t *pte;
   2714 
   2715 	for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
   2716 
   2717 		/*
   2718 		 * We assume that we will only be called with small
   2719 		 * regions of memory.
   2720 		 */
   2721 
   2722 		KASSERT(pmap_pde_page(pmap_pde(pmap_kernel(), va)));
   2723 		pte = vtopte(va);
   2724 		cpu_idcache_wbinv_range(va, PAGE_SIZE);
   2725 		*pte = 0;
   2726 		cpu_tlb_flushID_SE(va);
   2727 	}
   2728 }
   2729 
   2730 /*
   2731  * pmap_page_protect:
   2732  *
   2733  * Lower the permission for all mappings to a given page.
   2734  */
   2735 
   2736 void
   2737 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   2738 {
   2739 
   2740 	PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n",
   2741 	    VM_PAGE_TO_PHYS(pg), prot));
   2742 
   2743 	switch(prot) {
   2744 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   2745 	case VM_PROT_READ|VM_PROT_WRITE:
   2746 		return;
   2747 
   2748 	case VM_PROT_READ:
   2749 	case VM_PROT_READ|VM_PROT_EXECUTE:
   2750 		pmap_clearbit(pg, PVF_WRITE);
   2751 		break;
   2752 
   2753 	default:
   2754 		pmap_remove_all(pg);
   2755 		break;
   2756 	}
   2757 }
   2758 
   2759 
   2760 /*
   2761  * Routine:	pmap_unwire
   2762  * Function:	Clear the wired attribute for a map/virtual-address
   2763  *		pair.
   2764  * In/out conditions:
   2765  *		The mapping must already exist in the pmap.
   2766  */
   2767 
   2768 void
   2769 pmap_unwire(struct pmap *pmap, vaddr_t va)
   2770 {
   2771 	pt_entry_t *ptes;
   2772 	struct vm_page *pg;
   2773 	paddr_t pa;
   2774 
   2775 	PMAP_MAP_TO_HEAD_LOCK();
   2776 	ptes = pmap_map_ptes(pmap);		/* locks pmap */
   2777 
   2778 	if (pmap_pde_v(pmap_pde(pmap, va))) {
   2779 #ifdef DIAGNOSTIC
   2780 		if (l2pte_valid(ptes[arm_btop(va)]) == 0)
   2781 			panic("pmap_unwire: invalid L2 PTE");
   2782 #endif
   2783 		/* Extract the physical address of the page */
   2784 		pa = l2pte_pa(ptes[arm_btop(va)]);
   2785 
   2786 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   2787 			goto out;
   2788 
   2789 		/* Update the wired bit in the pv entry for this page. */
   2790 		simple_lock(&pg->mdpage.pvh_slock);
   2791 		(void) pmap_modify_pv(pmap, va, pg, PVF_WIRED, 0);
   2792 		simple_unlock(&pg->mdpage.pvh_slock);
   2793 	}
   2794 #ifdef DIAGNOSTIC
   2795 	else {
   2796 		panic("pmap_unwire: invalid L1 PTE");
   2797 	}
   2798 #endif
   2799  out:
   2800 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   2801 	PMAP_MAP_TO_HEAD_UNLOCK();
   2802 }
   2803 
   2804 /*
   2805  * Routine:  pmap_extract
   2806  * Function:
   2807  *           Extract the physical page address associated
   2808  *           with the given map/virtual_address pair.
   2809  */
   2810 boolean_t
   2811 pmap_extract(struct pmap *pmap, vaddr_t va, paddr_t *pap)
   2812 {
   2813 	pd_entry_t *pde;
   2814 	pt_entry_t *pte, *ptes;
   2815 	paddr_t pa;
   2816 
   2817 	PDEBUG(5, printf("pmap_extract: pmap=%p, va=0x%08lx -> ", pmap, va));
   2818 
   2819 	ptes = pmap_map_ptes(pmap);		/* locks pmap */
   2820 
   2821 	pde = pmap_pde(pmap, va);
   2822 	pte = &ptes[arm_btop(va)];
   2823 
   2824 	if (pmap_pde_section(pde)) {
   2825 		pa = (*pde & L1_S_FRAME) | (va & L1_S_OFFSET);
   2826 		PDEBUG(5, printf("section pa=0x%08lx\n", pa));
   2827 		goto out;
   2828 	} else if (pmap_pde_page(pde) == 0 || pmap_pte_v(pte) == 0) {
   2829 		PDEBUG(5, printf("no mapping\n"));
   2830 		goto failed;
   2831 	}
   2832 
   2833 	if ((*pte & L2_TYPE_MASK) == L2_TYPE_L) {
   2834 		pa = (*pte & L2_L_FRAME) | (va & L2_L_OFFSET);
   2835 		PDEBUG(5, printf("large page pa=0x%08lx\n", pa));
   2836 		goto out;
   2837 	}
   2838 
   2839 	pa = (*pte & L2_S_FRAME) | (va & L2_S_OFFSET);
   2840 	PDEBUG(5, printf("small page pa=0x%08lx\n", pa));
   2841 
   2842  out:
   2843 	if (pap != NULL)
   2844 		*pap = pa;
   2845 
   2846 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   2847 	return (TRUE);
   2848 
   2849  failed:
   2850 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   2851 	return (FALSE);
   2852 }
   2853 
   2854 
   2855 /*
   2856  * pmap_copy:
   2857  *
   2858  *	Copy the range specified by src_addr/len from the source map to the
   2859  *	range dst_addr/len in the destination map.
   2860  *
   2861  *	This routine is only advisory and need not do anything.
   2862  */
   2863 /* Call deleted in <arm/arm32/pmap.h> */
   2864 
   2865 #if defined(PMAP_DEBUG)
   2866 void
   2867 pmap_dump_pvlist(phys, m)
   2868 	vaddr_t phys;
   2869 	char *m;
   2870 {
   2871 	struct vm_page *pg;
   2872 	struct pv_entry *pv;
   2873 
   2874 	if ((pg = PHYS_TO_VM_PAGE(phys)) == NULL) {
   2875 		printf("INVALID PA\n");
   2876 		return;
   2877 	}
   2878 	simple_lock(&pg->mdpage.pvh_slock);
   2879 	printf("%s %08lx:", m, phys);
   2880 	if (pg->mdpage.pvh_list == NULL) {
   2881 		printf(" no mappings\n");
   2882 		return;
   2883 	}
   2884 
   2885 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next)
   2886 		printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
   2887 		    pv->pv_va, pv->pv_flags);
   2888 
   2889 	printf("\n");
   2890 	simple_unlock(&pg->mdpage.pvh_slock);
   2891 }
   2892 
   2893 #endif	/* PMAP_DEBUG */
   2894 
   2895 static pt_entry_t *
   2896 pmap_map_ptes(struct pmap *pmap)
   2897 {
   2898 	struct proc *p;
   2899 
   2900     	/* the kernel's pmap is always accessible */
   2901 	if (pmap == pmap_kernel()) {
   2902 		return (pt_entry_t *)PTE_BASE;
   2903 	}
   2904 
   2905 	if (pmap_is_curpmap(pmap)) {
   2906 		simple_lock(&pmap->pm_obj.vmobjlock);
   2907 		return (pt_entry_t *)PTE_BASE;
   2908 	}
   2909 
   2910 	p = curproc;
   2911 	KDASSERT(p != NULL);
   2912 
   2913 	/* need to lock both curpmap and pmap: use ordered locking */
   2914 	if ((vaddr_t) pmap < (vaddr_t) p->p_vmspace->vm_map.pmap) {
   2915 		simple_lock(&pmap->pm_obj.vmobjlock);
   2916 		simple_lock(&p->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   2917 	} else {
   2918 		simple_lock(&p->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   2919 		simple_lock(&pmap->pm_obj.vmobjlock);
   2920 	}
   2921 
   2922 	pmap_map_in_l1(p->p_vmspace->vm_map.pmap, APTE_BASE, pmap->pm_pptpt,
   2923 	    FALSE);
   2924 	cpu_tlb_flushD();
   2925 	cpu_cpwait();
   2926 	return (pt_entry_t *)APTE_BASE;
   2927 }
   2928 
   2929 /*
   2930  * pmap_unmap_ptes: unlock the PTE mapping of "pmap"
   2931  */
   2932 
   2933 static void
   2934 pmap_unmap_ptes(struct pmap *pmap)
   2935 {
   2936 
   2937 	if (pmap == pmap_kernel()) {
   2938 		return;
   2939 	}
   2940 	if (pmap_is_curpmap(pmap)) {
   2941 		simple_unlock(&pmap->pm_obj.vmobjlock);
   2942 	} else {
   2943 		KDASSERT(curproc != NULL);
   2944 		simple_unlock(&pmap->pm_obj.vmobjlock);
   2945 		simple_unlock(
   2946 		    &curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   2947 	}
   2948 }
   2949 
   2950 /*
   2951  * Modify pte bits for all ptes corresponding to the given physical address.
   2952  * We use `maskbits' rather than `clearbits' because we're always passing
   2953  * constants and the latter would require an extra inversion at run-time.
   2954  */
   2955 
   2956 static void
   2957 pmap_clearbit(struct vm_page *pg, u_int maskbits)
   2958 {
   2959 	struct pv_entry *pv;
   2960 	pt_entry_t *ptes;
   2961 	vaddr_t va;
   2962 	int tlbentry;
   2963 
   2964 	PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
   2965 	    VM_PAGE_TO_PHYS(pg), maskbits));
   2966 
   2967 	tlbentry = 0;
   2968 
   2969 	PMAP_HEAD_TO_MAP_LOCK();
   2970 	simple_lock(&pg->mdpage.pvh_slock);
   2971 
   2972 	/*
   2973 	 * Clear saved attributes (modify, reference)
   2974 	 */
   2975 	pg->mdpage.pvh_attrs &= ~maskbits;
   2976 
   2977 	if (pg->mdpage.pvh_list == NULL) {
   2978 		simple_unlock(&pg->mdpage.pvh_slock);
   2979 		PMAP_HEAD_TO_MAP_UNLOCK();
   2980 		return;
   2981 	}
   2982 
   2983 	/*
   2984 	 * Loop over all current mappings setting/clearing as appropos
   2985 	 */
   2986 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   2987 		va = pv->pv_va;
   2988 		pv->pv_flags &= ~maskbits;
   2989 		ptes = pmap_map_ptes(pv->pv_pmap);	/* locks pmap */
   2990 		KASSERT(pmap_pde_v(pmap_pde(pv->pv_pmap, va)));
   2991 		if (maskbits & (PVF_WRITE|PVF_MOD)) {
   2992 			if ((pv->pv_flags & PVF_NC)) {
   2993 				/*
   2994 				 * Entry is not cacheable: reenable
   2995 				 * the cache, nothing to flush
   2996 				 *
   2997 				 * Don't turn caching on again if this
   2998 				 * is a modified emulation.  This
   2999 				 * would be inconsitent with the
   3000 				 * settings created by
   3001 				 * pmap_vac_me_harder().
   3002 				 *
   3003 				 * There's no need to call
   3004 				 * pmap_vac_me_harder() here: all
   3005 				 * pages are loosing their write
   3006 				 * permission.
   3007 				 *
   3008 				 */
   3009 				if (maskbits & PVF_WRITE) {
   3010 					ptes[arm_btop(va)] |=
   3011 					    pte_l2_s_cache_mode;
   3012 					pv->pv_flags &= ~PVF_NC;
   3013 				}
   3014 			} else if (pmap_is_curpmap(pv->pv_pmap)) {
   3015 				/*
   3016 				 * Entry is cacheable: check if pmap is
   3017 				 * current if it is flush it,
   3018 				 * otherwise it won't be in the cache
   3019 				 */
   3020 				cpu_idcache_wbinv_range(pv->pv_va, NBPG);
   3021 			}
   3022 
   3023 			/* make the pte read only */
   3024 			ptes[arm_btop(va)] &= ~L2_S_PROT_W;
   3025 		}
   3026 
   3027 		if (maskbits & PVF_REF)
   3028 			ptes[arm_btop(va)] =
   3029 			    (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_TYPE_INV;
   3030 
   3031 		if (pmap_is_curpmap(pv->pv_pmap)) {
   3032 			/*
   3033 			 * if we had cacheable pte's we'd clean the
   3034 			 * pte out to memory here
   3035 			 *
   3036 			 * flush tlb entry as it's in the current pmap
   3037 			 */
   3038 			cpu_tlb_flushID_SE(pv->pv_va);
   3039 		}
   3040 		pmap_unmap_ptes(pv->pv_pmap);		/* unlocks pmap */
   3041 	}
   3042 	cpu_cpwait();
   3043 
   3044 	simple_unlock(&pg->mdpage.pvh_slock);
   3045 	PMAP_HEAD_TO_MAP_UNLOCK();
   3046 }
   3047 
   3048 /*
   3049  * pmap_clear_modify:
   3050  *
   3051  *	Clear the "modified" attribute for a page.
   3052  */
   3053 boolean_t
   3054 pmap_clear_modify(struct vm_page *pg)
   3055 {
   3056 	boolean_t rv;
   3057 
   3058 	if (pg->mdpage.pvh_attrs & PVF_MOD) {
   3059 		rv = TRUE;
   3060 		pmap_clearbit(pg, PVF_MOD);
   3061 	} else
   3062 		rv = FALSE;
   3063 
   3064 	PDEBUG(0, printf("pmap_clear_modify pa=%08lx -> %d\n",
   3065 	    VM_PAGE_TO_PHYS(pg), rv));
   3066 
   3067 	return (rv);
   3068 }
   3069 
   3070 /*
   3071  * pmap_clear_reference:
   3072  *
   3073  *	Clear the "referenced" attribute for a page.
   3074  */
   3075 boolean_t
   3076 pmap_clear_reference(struct vm_page *pg)
   3077 {
   3078 	boolean_t rv;
   3079 
   3080 	if (pg->mdpage.pvh_attrs & PVF_REF) {
   3081 		rv = TRUE;
   3082 		pmap_clearbit(pg, PVF_REF);
   3083 	} else
   3084 		rv = FALSE;
   3085 
   3086 	PDEBUG(0, printf("pmap_clear_reference pa=%08lx -> %d\n",
   3087 	    VM_PAGE_TO_PHYS(pg), rv));
   3088 
   3089 	return (rv);
   3090 }
   3091 
   3092 /*
   3093  * pmap_is_modified:
   3094  *
   3095  *	Test if a page has the "modified" attribute.
   3096  */
   3097 /* See <arm/arm32/pmap.h> */
   3098 
   3099 /*
   3100  * pmap_is_referenced:
   3101  *
   3102  *	Test if a page has the "referenced" attribute.
   3103  */
   3104 /* See <arm/arm32/pmap.h> */
   3105 
   3106 int
   3107 pmap_modified_emulation(struct pmap *pmap, vaddr_t va)
   3108 {
   3109 	pt_entry_t *ptes;
   3110 	struct vm_page *pg;
   3111 	paddr_t pa;
   3112 	u_int flags;
   3113 	int rv = 0;
   3114 
   3115 	PDEBUG(2, printf("pmap_modified_emulation\n"));
   3116 
   3117 	PMAP_MAP_TO_HEAD_LOCK();
   3118 	ptes = pmap_map_ptes(pmap);		/* locks pmap */
   3119 
   3120 	if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
   3121 		PDEBUG(2, printf("L1 PTE invalid\n"));
   3122 		goto out;
   3123 	}
   3124 
   3125 	PDEBUG(1, printf("pte=%08x\n", ptes[arm_btop(va)]));
   3126 
   3127 	/* Check for a invalid pte */
   3128 	if (l2pte_valid(ptes[arm_btop(va)]) == 0)
   3129 		goto out;
   3130 
   3131 	/* This can happen if user code tries to access kernel memory. */
   3132 	if ((ptes[arm_btop(va)] & L2_S_PROT_W) != 0)
   3133 		goto out;
   3134 
   3135 	/* Extract the physical address of the page */
   3136 	pa = l2pte_pa(ptes[arm_btop(va)]);
   3137 	if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3138 		goto out;
   3139 
   3140 	/* Get the current flags for this page. */
   3141 	simple_lock(&pg->mdpage.pvh_slock);
   3142 
   3143 	flags = pmap_modify_pv(pmap, va, pg, 0, 0);
   3144 	PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
   3145 
   3146 	/*
   3147 	 * Do the flags say this page is writable ? If not then it is a
   3148 	 * genuine write fault. If yes then the write fault is our fault
   3149 	 * as we did not reflect the write access in the PTE. Now we know
   3150 	 * a write has occurred we can correct this and also set the
   3151 	 * modified bit
   3152 	 */
   3153 	if (~flags & PVF_WRITE) {
   3154 	    	simple_unlock(&pg->mdpage.pvh_slock);
   3155 		goto out;
   3156 	}
   3157 
   3158 	PDEBUG(0,
   3159 	    printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %08x\n",
   3160 	    va, ptes[arm_btop(va)]));
   3161 	pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
   3162 
   3163 	/*
   3164 	 * Re-enable write permissions for the page.  No need to call
   3165 	 * pmap_vac_me_harder(), since this is just a
   3166 	 * modified-emulation fault, and the PVF_WRITE bit isn't changing.
   3167 	 * We've already set the cacheable bits based on the assumption
   3168 	 * that we can write to this page.
   3169 	 */
   3170 	ptes[arm_btop(va)] =
   3171 	    (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
   3172 	PDEBUG(0, printf("->(%08x)\n", ptes[arm_btop(va)]));
   3173 
   3174 	simple_unlock(&pg->mdpage.pvh_slock);
   3175 
   3176 	cpu_tlb_flushID_SE(va);
   3177 	cpu_cpwait();
   3178 	rv = 1;
   3179  out:
   3180 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   3181 	PMAP_MAP_TO_HEAD_UNLOCK();
   3182 	return (rv);
   3183 }
   3184 
   3185 int
   3186 pmap_handled_emulation(struct pmap *pmap, vaddr_t va)
   3187 {
   3188 	pt_entry_t *ptes;
   3189 	struct vm_page *pg;
   3190 	paddr_t pa;
   3191 	int rv = 0;
   3192 
   3193 	PDEBUG(2, printf("pmap_handled_emulation\n"));
   3194 
   3195 	PMAP_MAP_TO_HEAD_LOCK();
   3196 	ptes = pmap_map_ptes(pmap);		/* locks pmap */
   3197 
   3198 	if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
   3199 		PDEBUG(2, printf("L1 PTE invalid\n"));
   3200 		goto out;
   3201 	}
   3202 
   3203 	PDEBUG(1, printf("pte=%08x\n", ptes[arm_btop(va)]));
   3204 
   3205 	/* Check for invalid pte */
   3206 	if (l2pte_valid(ptes[arm_btop(va)]) == 0)
   3207 		goto out;
   3208 
   3209 	/* This can happen if user code tries to access kernel memory. */
   3210 	if ((ptes[arm_btop(va)] & L2_TYPE_MASK) != L2_TYPE_INV)
   3211 		goto out;
   3212 
   3213 	/* Extract the physical address of the page */
   3214 	pa = l2pte_pa(ptes[arm_btop(va)]);
   3215 	if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3216 		goto out;
   3217 
   3218 	simple_lock(&pg->mdpage.pvh_slock);
   3219 
   3220 	/*
   3221 	 * Ok we just enable the pte and mark the attibs as handled
   3222 	 * XXX Should we traverse the PV list and enable all PTEs?
   3223 	 */
   3224 	PDEBUG(0,
   3225 	    printf("pmap_handled_emulation: Got a hit va=%08lx pte = %08x\n",
   3226 	    va, ptes[arm_btop(va)]));
   3227 	pg->mdpage.pvh_attrs |= PVF_REF;
   3228 
   3229 	ptes[arm_btop(va)] = (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_S_PROTO;
   3230 	PDEBUG(0, printf("->(%08x)\n", ptes[arm_btop(va)]));
   3231 
   3232 	simple_unlock(&pg->mdpage.pvh_slock);
   3233 
   3234 	cpu_tlb_flushID_SE(va);
   3235 	cpu_cpwait();
   3236 	rv = 1;
   3237  out:
   3238 	pmap_unmap_ptes(pmap);			/* unlocks pmap */
   3239 	PMAP_MAP_TO_HEAD_UNLOCK();
   3240 	return (rv);
   3241 }
   3242 
   3243 /*
   3244  * pmap_collect: free resources held by a pmap
   3245  *
   3246  * => optional function.
   3247  * => called when a process is swapped out to free memory.
   3248  */
   3249 
   3250 void
   3251 pmap_collect(struct pmap *pmap)
   3252 {
   3253 }
   3254 
   3255 /*
   3256  * Routine:	pmap_procwr
   3257  *
   3258  * Function:
   3259  *	Synchronize caches corresponding to [addr, addr+len) in p.
   3260  *
   3261  */
   3262 void
   3263 pmap_procwr(struct proc *p, vaddr_t va, int len)
   3264 {
   3265 	/* We only need to do anything if it is the current process. */
   3266 	if (p == curproc)
   3267 		cpu_icache_sync_range(va, len);
   3268 }
   3269 /*
   3270  * PTP functions
   3271  */
   3272 
   3273 /*
   3274  * pmap_get_ptp: get a PTP (if there isn't one, allocate a new one)
   3275  *
   3276  * => pmap should NOT be pmap_kernel()
   3277  * => pmap should be locked
   3278  */
   3279 
   3280 static struct vm_page *
   3281 pmap_get_ptp(struct pmap *pmap, vaddr_t va)
   3282 {
   3283 	struct vm_page *ptp;
   3284 
   3285 	if (pmap_pde_page(pmap_pde(pmap, va))) {
   3286 
   3287 		/* valid... check hint (saves us a PA->PG lookup) */
   3288 		if (pmap->pm_ptphint &&
   3289 		    (pmap->pm_pdir[pmap_pdei(va)] & L2_S_FRAME) ==
   3290 		    VM_PAGE_TO_PHYS(pmap->pm_ptphint))
   3291 			return (pmap->pm_ptphint);
   3292 		ptp = uvm_pagelookup(&pmap->pm_obj, va);
   3293 #ifdef DIAGNOSTIC
   3294 		if (ptp == NULL)
   3295 			panic("pmap_get_ptp: unmanaged user PTP");
   3296 #endif
   3297 		pmap->pm_ptphint = ptp;
   3298 		return(ptp);
   3299 	}
   3300 
   3301 	/* allocate a new PTP (updates ptphint) */
   3302 	return(pmap_alloc_ptp(pmap, va));
   3303 }
   3304 
   3305 /*
   3306  * pmap_alloc_ptp: allocate a PTP for a PMAP
   3307  *
   3308  * => pmap should already be locked by caller
   3309  * => we use the ptp's wire_count to count the number of active mappings
   3310  *	in the PTP (we start it at one to prevent any chance this PTP
   3311  *	will ever leak onto the active/inactive queues)
   3312  */
   3313 
   3314 /*__inline */ static struct vm_page *
   3315 pmap_alloc_ptp(struct pmap *pmap, vaddr_t va)
   3316 {
   3317 	struct vm_page *ptp;
   3318 
   3319 	ptp = uvm_pagealloc(&pmap->pm_obj, va, NULL,
   3320 		UVM_PGA_USERESERVE|UVM_PGA_ZERO);
   3321 	if (ptp == NULL)
   3322 		return (NULL);
   3323 
   3324 	/* got one! */
   3325 	ptp->flags &= ~PG_BUSY;	/* never busy */
   3326 	ptp->wire_count = 1;	/* no mappings yet */
   3327 	pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(ptp), TRUE);
   3328 	pmap->pm_stats.resident_count++;	/* count PTP as resident */
   3329 	pmap->pm_ptphint = ptp;
   3330 	return (ptp);
   3331 }
   3332 
   3333 vaddr_t
   3334 pmap_growkernel(vaddr_t maxkvaddr)
   3335 {
   3336 	struct pmap *kpm = pmap_kernel(), *pm;
   3337 	int s;
   3338 	paddr_t ptaddr;
   3339 	struct vm_page *ptp;
   3340 
   3341 	if (maxkvaddr <= pmap_curmaxkvaddr)
   3342 		goto out;		/* we are OK */
   3343 	NPDEBUG(PDB_GROWKERN, printf("pmap_growkernel: growing kernel from %lx to %lx\n",
   3344 		    pmap_curmaxkvaddr, maxkvaddr));
   3345 
   3346 	/*
   3347 	 * whoops!   we need to add kernel PTPs
   3348 	 */
   3349 
   3350 	s = splhigh();	/* to be safe */
   3351 	simple_lock(&kpm->pm_obj.vmobjlock);
   3352 	/* due to the way the arm pmap works we map 4MB at a time */
   3353 	for (/*null*/ ; pmap_curmaxkvaddr < maxkvaddr;
   3354 	     pmap_curmaxkvaddr += 4 * L1_S_SIZE) {
   3355 
   3356 		if (uvm.page_init_done == FALSE) {
   3357 
   3358 			/*
   3359 			 * we're growing the kernel pmap early (from
   3360 			 * uvm_pageboot_alloc()).  this case must be
   3361 			 * handled a little differently.
   3362 			 */
   3363 
   3364 			if (uvm_page_physget(&ptaddr) == FALSE)
   3365 				panic("pmap_growkernel: out of memory");
   3366 			pmap_zero_page(ptaddr);
   3367 
   3368 			/* map this page in */
   3369 			pmap_map_in_l1(kpm, pmap_curmaxkvaddr, ptaddr, TRUE);
   3370 
   3371 			/* count PTP as resident */
   3372 			kpm->pm_stats.resident_count++;
   3373 			continue;
   3374 		}
   3375 
   3376 		/*
   3377 		 * THIS *MUST* BE CODED SO AS TO WORK IN THE
   3378 		 * pmap_initialized == FALSE CASE!  WE MAY BE
   3379 		 * INVOKED WHILE pmap_init() IS RUNNING!
   3380 		 */
   3381 
   3382 		if ((ptp = pmap_alloc_ptp(kpm, pmap_curmaxkvaddr)) == NULL)
   3383 			panic("pmap_growkernel: alloc ptp failed");
   3384 
   3385 		/* distribute new kernel PTP to all active pmaps */
   3386 		simple_lock(&pmaps_lock);
   3387 		LIST_FOREACH(pm, &pmaps, pm_list) {
   3388 			pmap_map_in_l1(pm, pmap_curmaxkvaddr,
   3389 			    VM_PAGE_TO_PHYS(ptp), TRUE);
   3390 		}
   3391 
   3392 		simple_unlock(&pmaps_lock);
   3393 	}
   3394 
   3395 	/*
   3396 	 * flush out the cache, expensive but growkernel will happen so
   3397 	 * rarely
   3398 	 */
   3399 	cpu_tlb_flushD();
   3400 	cpu_cpwait();
   3401 
   3402 	simple_unlock(&kpm->pm_obj.vmobjlock);
   3403 	splx(s);
   3404 
   3405 out:
   3406 	return (pmap_curmaxkvaddr);
   3407 }
   3408 
   3409 /************************ Utility routines ****************************/
   3410 
   3411 /*
   3412  * vector_page_setprot:
   3413  *
   3414  *	Manipulate the protection of the vector page.
   3415  */
   3416 void
   3417 vector_page_setprot(int prot)
   3418 {
   3419 	pt_entry_t *pte;
   3420 
   3421 	pte = vtopte(vector_page);
   3422 
   3423 	*pte = (*pte & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
   3424 	cpu_tlb_flushD_SE(vector_page);
   3425 	cpu_cpwait();
   3426 }
   3427 
   3428 /************************ Bootstrapping routines ****************************/
   3429 
   3430 /*
   3431  * This list exists for the benefit of pmap_map_chunk().  It keeps track
   3432  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
   3433  * find them as necessary.
   3434  *
   3435  * Note that the data on this list is not valid after initarm() returns.
   3436  */
   3437 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
   3438 
   3439 static vaddr_t
   3440 kernel_pt_lookup(paddr_t pa)
   3441 {
   3442 	pv_addr_t *pv;
   3443 
   3444 	SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
   3445 		if (pv->pv_pa == pa)
   3446 			return (pv->pv_va);
   3447 	}
   3448 	return (0);
   3449 }
   3450 
   3451 /*
   3452  * pmap_map_section:
   3453  *
   3454  *	Create a single section mapping.
   3455  */
   3456 void
   3457 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   3458 {
   3459 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   3460 	pd_entry_t fl = (cache == PTE_CACHE) ? pte_l1_s_cache_mode : 0;
   3461 
   3462 	KASSERT(((va | pa) & L1_S_OFFSET) == 0);
   3463 
   3464 	pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   3465 	    L1_S_PROT(PTE_KERNEL, prot) | fl;
   3466 }
   3467 
   3468 /*
   3469  * pmap_map_entry:
   3470  *
   3471  *	Create a single page mapping.
   3472  */
   3473 void
   3474 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   3475 {
   3476 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   3477 	pt_entry_t fl = (cache == PTE_CACHE) ? pte_l2_s_cache_mode : 0;
   3478 	pt_entry_t *pte;
   3479 
   3480 	KASSERT(((va | pa) & PGOFSET) == 0);
   3481 
   3482 	if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   3483 		panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
   3484 
   3485 	pte = (pt_entry_t *)
   3486 	    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   3487 	if (pte == NULL)
   3488 		panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
   3489 
   3490 	pte[(va >> PGSHIFT) & 0x3ff] = L2_S_PROTO | pa |
   3491 	    L2_S_PROT(PTE_KERNEL, prot) | fl;
   3492 }
   3493 
   3494 /*
   3495  * pmap_link_l2pt:
   3496  *
   3497  *	Link the L2 page table specified by "pa" into the L1
   3498  *	page table at the slot for "va".
   3499  */
   3500 void
   3501 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
   3502 {
   3503 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   3504 	u_int slot = va >> L1_S_SHIFT;
   3505 
   3506 	KASSERT((l2pv->pv_pa & PGOFSET) == 0);
   3507 
   3508 	pde[slot + 0] = L1_C_PROTO | (l2pv->pv_pa + 0x000);
   3509 	pde[slot + 1] = L1_C_PROTO | (l2pv->pv_pa + 0x400);
   3510 	pde[slot + 2] = L1_C_PROTO | (l2pv->pv_pa + 0x800);
   3511 	pde[slot + 3] = L1_C_PROTO | (l2pv->pv_pa + 0xc00);
   3512 
   3513 	SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
   3514 }
   3515 
   3516 /*
   3517  * pmap_map_chunk:
   3518  *
   3519  *	Map a chunk of memory using the most efficient mappings
   3520  *	possible (section, large page, small page) into the
   3521  *	provided L1 and L2 tables at the specified virtual address.
   3522  */
   3523 vsize_t
   3524 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
   3525     int prot, int cache)
   3526 {
   3527 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   3528 	pt_entry_t *pte, fl;
   3529 	vsize_t resid;
   3530 	int i;
   3531 
   3532 	resid = (size + (NBPG - 1)) & ~(NBPG - 1);
   3533 
   3534 	if (l1pt == 0)
   3535 		panic("pmap_map_chunk: no L1 table provided");
   3536 
   3537 #ifdef VERBOSE_INIT_ARM
   3538 	printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
   3539 	    "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
   3540 #endif
   3541 
   3542 	size = resid;
   3543 
   3544 	while (resid > 0) {
   3545 		/* See if we can use a section mapping. */
   3546 		if (((pa | va) & L1_S_OFFSET) == 0 &&
   3547 		    resid >= L1_S_SIZE) {
   3548 			fl = (cache == PTE_CACHE) ? pte_l1_s_cache_mode : 0;
   3549 #ifdef VERBOSE_INIT_ARM
   3550 			printf("S");
   3551 #endif
   3552 			pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   3553 			    L1_S_PROT(PTE_KERNEL, prot) | fl;
   3554 			va += L1_S_SIZE;
   3555 			pa += L1_S_SIZE;
   3556 			resid -= L1_S_SIZE;
   3557 			continue;
   3558 		}
   3559 
   3560 		/*
   3561 		 * Ok, we're going to use an L2 table.  Make sure
   3562 		 * one is actually in the corresponding L1 slot
   3563 		 * for the current VA.
   3564 		 */
   3565 		if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   3566 			panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
   3567 
   3568 		pte = (pt_entry_t *)
   3569 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   3570 		if (pte == NULL)
   3571 			panic("pmap_map_chunk: can't find L2 table for VA"
   3572 			    "0x%08lx", va);
   3573 
   3574 		/* See if we can use a L2 large page mapping. */
   3575 		if (((pa | va) & L2_L_OFFSET) == 0 &&
   3576 		    resid >= L2_L_SIZE) {
   3577 			fl = (cache == PTE_CACHE) ? pte_l2_l_cache_mode : 0;
   3578 #ifdef VERBOSE_INIT_ARM
   3579 			printf("L");
   3580 #endif
   3581 			for (i = 0; i < 16; i++) {
   3582 				pte[((va >> PGSHIFT) & 0x3f0) + i] =
   3583 				    L2_L_PROTO | pa |
   3584 				    L2_L_PROT(PTE_KERNEL, prot) | fl;
   3585 			}
   3586 			va += L2_L_SIZE;
   3587 			pa += L2_L_SIZE;
   3588 			resid -= L2_L_SIZE;
   3589 			continue;
   3590 		}
   3591 
   3592 		/* Use a small page mapping. */
   3593 		fl = (cache == PTE_CACHE) ? pte_l2_s_cache_mode : 0;
   3594 #ifdef VERBOSE_INIT_ARM
   3595 		printf("P");
   3596 #endif
   3597 		pte[(va >> PGSHIFT) & 0x3ff] = L2_S_PROTO | pa |
   3598 		    L2_S_PROT(PTE_KERNEL, prot) | fl;
   3599 		va += NBPG;
   3600 		pa += NBPG;
   3601 		resid -= NBPG;
   3602 	}
   3603 #ifdef VERBOSE_INIT_ARM
   3604 	printf("\n");
   3605 #endif
   3606 	return (size);
   3607 }
   3608 
   3609 /********************** PTE initialization routines **************************/
   3610 
   3611 /*
   3612  * These routines are called when the CPU type is identified to set up
   3613  * the PTE prototypes, cache modes, etc.
   3614  *
   3615  * The variables are always here, just in case LKMs need to reference
   3616  * them (though, they shouldn't).
   3617  */
   3618 
   3619 pt_entry_t	pte_l1_s_cache_mode;
   3620 pt_entry_t	pte_l1_s_cache_mask;
   3621 
   3622 pt_entry_t	pte_l2_l_cache_mode;
   3623 pt_entry_t	pte_l2_l_cache_mask;
   3624 
   3625 pt_entry_t	pte_l2_s_cache_mode;
   3626 pt_entry_t	pte_l2_s_cache_mask;
   3627 
   3628 pt_entry_t	pte_l2_s_prot_u;
   3629 pt_entry_t	pte_l2_s_prot_w;
   3630 pt_entry_t	pte_l2_s_prot_mask;
   3631 
   3632 pt_entry_t	pte_l1_s_proto;
   3633 pt_entry_t	pte_l1_c_proto;
   3634 pt_entry_t	pte_l2_s_proto;
   3635 
   3636 #if ARM_MMU_GENERIC == 1
   3637 void
   3638 pmap_pte_init_generic(void)
   3639 {
   3640 
   3641 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   3642 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
   3643 
   3644 	pte_l2_l_cache_mode = L2_B|L2_C;
   3645 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
   3646 
   3647 	pte_l2_s_cache_mode = L2_B|L2_C;
   3648 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
   3649 
   3650 	pte_l2_s_prot_u = L2_S_PROT_U_generic;
   3651 	pte_l2_s_prot_w = L2_S_PROT_W_generic;
   3652 	pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
   3653 
   3654 	pte_l1_s_proto = L1_S_PROTO_generic;
   3655 	pte_l1_c_proto = L1_C_PROTO_generic;
   3656 	pte_l2_s_proto = L2_S_PROTO_generic;
   3657 }
   3658 
   3659 #if defined(CPU_ARM9)
   3660 void
   3661 pmap_pte_init_arm9(void)
   3662 {
   3663 
   3664 	/*
   3665 	 * ARM9 is compatible with generic, but we want to use
   3666 	 * write-through caching for now.
   3667 	 */
   3668 	pmap_pte_init_generic();
   3669 
   3670 	pte_l1_s_cache_mode = L1_S_C;
   3671 	pte_l2_l_cache_mode = L2_C;
   3672 	pte_l2_s_cache_mode = L2_C;
   3673 }
   3674 #endif /* CPU_ARM9 */
   3675 #endif /* ARM_MMU_GENERIC == 1 */
   3676 
   3677 #if ARM_MMU_XSCALE == 1
   3678 void
   3679 pmap_pte_init_xscale(void)
   3680 {
   3681 
   3682 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   3683 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
   3684 
   3685 	pte_l2_l_cache_mode = L2_B|L2_C;
   3686 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
   3687 
   3688 	pte_l2_s_cache_mode = L2_B|L2_C;
   3689 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
   3690 
   3691 	pte_l2_s_prot_u = L2_S_PROT_U_xscale;
   3692 	pte_l2_s_prot_w = L2_S_PROT_W_xscale;
   3693 	pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
   3694 
   3695 	pte_l1_s_proto = L1_S_PROTO_xscale;
   3696 	pte_l1_c_proto = L1_C_PROTO_xscale;
   3697 	pte_l2_s_proto = L2_S_PROTO_xscale;
   3698 }
   3699 
   3700 #if defined(CPU_XSCALE_80200)
   3701 void
   3702 pmap_pte_init_i80200(void)
   3703 {
   3704 
   3705 	/*
   3706 	 * Use write-through caching on the i80200.
   3707 	 */
   3708 	pmap_pte_init_xscale();
   3709 	pte_l1_s_cache_mode = L1_S_C;
   3710 	pte_l2_l_cache_mode = L2_C;
   3711 	pte_l2_s_cache_mode = L2_C;
   3712 }
   3713 #endif /* CPU_XSCALE_80200 */
   3714 #endif /* ARM_MMU_XSCALE == 1 */
   3715