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