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