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