Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.26
      1 /*	$NetBSD: pmap.c,v 1.26 2001/10/18 16:50:30 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.26 2001/10/18 16:50:30 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 		PDEBUG(0,
   1340 		    printf("pmap: Cannot allocate pageable memory for L1\n"));
   1341 #endif	/* DIAGNOSTIC */
   1342 		return(NULL);
   1343 	}
   1344 
   1345 	/* Allocate memory for the l1pt structure */
   1346 	pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
   1347 
   1348 	/*
   1349 	 * Allocate pages from the VM system.
   1350 	 */
   1351 	TAILQ_INIT(&pt->pt_plist);
   1352 	error = uvm_pglistalloc(PD_SIZE, physical_start, physical_end,
   1353 	    PD_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
   1354 	if (error) {
   1355 #ifdef DIAGNOSTIC
   1356 		PDEBUG(0,
   1357 		    printf("pmap: Cannot allocate physical mem for L1 (%d)\n",
   1358 		    error));
   1359 #endif	/* DIAGNOSTIC */
   1360 		/* Release the resources we already have claimed */
   1361 		free(pt, M_VMPMAP);
   1362 		uvm_km_free(kernel_map, va, PD_SIZE);
   1363 		return(NULL);
   1364 	}
   1365 
   1366 	/* Map our physical pages into our virtual space */
   1367 	pt->pt_va = va;
   1368 	m = pt->pt_plist.tqh_first;
   1369 	ptes = pmap_map_ptes(pmap_kernel());
   1370 	while (m && va < (pt->pt_va + PD_SIZE)) {
   1371 		pa = VM_PAGE_TO_PHYS(m);
   1372 
   1373 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
   1374 
   1375 		/* Revoke cacheability and bufferability */
   1376 		/* XXX should be done better than this */
   1377 		ptes[arm_byte_to_page(va)] &= ~(PT_C | PT_B);
   1378 
   1379 		va += NBPG;
   1380 		m = m->pageq.tqe_next;
   1381 	}
   1382 	pmap_unmap_ptes(pmap_kernel());
   1383 	pmap_update(pmap_kernel());
   1384 
   1385 #ifdef DIAGNOSTIC
   1386 	if (m)
   1387 		panic("pmap_alloc_l1pt: pglist not empty\n");
   1388 #endif	/* DIAGNOSTIC */
   1389 
   1390 	pt->pt_flags = 0;
   1391 	return(pt);
   1392 }
   1393 
   1394 /*
   1395  * Free a L1 page table previously allocated with pmap_alloc_l1pt().
   1396  */
   1397 void
   1398 pmap_free_l1pt(pt)
   1399 	struct l1pt *pt;
   1400 {
   1401 	/* Separate the physical memory for the virtual space */
   1402 	pmap_kremove(pt->pt_va, PD_SIZE);
   1403 	pmap_update(pmap_kernel());
   1404 
   1405 	/* Return the physical memory */
   1406 	uvm_pglistfree(&pt->pt_plist);
   1407 
   1408 	/* Free the virtual space */
   1409 	uvm_km_free(kernel_map, pt->pt_va, PD_SIZE);
   1410 
   1411 	/* Free the l1pt structure */
   1412 	free(pt, M_VMPMAP);
   1413 }
   1414 
   1415 /*
   1416  * Allocate a page directory.
   1417  * This routine will either allocate a new page directory from the pool
   1418  * of L1 page tables currently held by the kernel or it will allocate
   1419  * a new one via pmap_alloc_l1pt().
   1420  * It will then initialise the l1 page table for use.
   1421  */
   1422 int
   1423 pmap_allocpagedir(pmap)
   1424 	struct pmap *pmap;
   1425 {
   1426 	paddr_t pa;
   1427 	struct l1pt *pt;
   1428 	pt_entry_t *pte;
   1429 
   1430 	PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
   1431 
   1432 	/* Do we have any spare L1's lying around ? */
   1433 	if (l1pt_static_queue_count) {
   1434 		--l1pt_static_queue_count;
   1435 		pt = l1pt_static_queue.sqh_first;
   1436 		SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
   1437 	} else if (l1pt_queue_count) {
   1438 		--l1pt_queue_count;
   1439 		pt = l1pt_queue.sqh_first;
   1440 		SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
   1441 		++l1pt_reuse_count;
   1442 	} else {
   1443 		pt = pmap_alloc_l1pt();
   1444 		if (!pt)
   1445 			return(ENOMEM);
   1446 		++l1pt_create_count;
   1447 	}
   1448 
   1449 	/* Store the pointer to the l1 descriptor in the pmap. */
   1450 	pmap->pm_l1pt = pt;
   1451 
   1452 	/* Get the physical address of the start of the l1 */
   1453 	pa = VM_PAGE_TO_PHYS(pt->pt_plist.tqh_first);
   1454 
   1455 	/* Store the virtual address of the l1 in the pmap. */
   1456 	pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
   1457 
   1458 	/* Clean the L1 if it is dirty */
   1459 	if (!(pt->pt_flags & PTFLAG_CLEAN))
   1460 		bzero((void *)pmap->pm_pdir, (PD_SIZE - KERNEL_PD_SIZE));
   1461 
   1462 	/* Do we already have the kernel mappings ? */
   1463 	if (!(pt->pt_flags & PTFLAG_KPT)) {
   1464 		/* Duplicate the kernel mapping i.e. all mappings 0xf0000000+ */
   1465 
   1466 		bcopy((char *)pmap_kernel()->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1467 		    (char *)pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1468 		    KERNEL_PD_SIZE);
   1469 		pt->pt_flags |= PTFLAG_KPT;
   1470 	}
   1471 
   1472 	/* Allocate a page table to map all the page tables for this pmap */
   1473 
   1474 #ifdef DIAGNOSTIC
   1475 	if (pmap->pm_vptpt) {
   1476 		/* XXX What if we have one already ? */
   1477 		panic("pmap_allocpagedir: have pt already\n");
   1478 	}
   1479 #endif	/* DIAGNOSTIC */
   1480 	pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
   1481 	if (pmap->pm_vptpt == 0) {
   1482 		pmap_freepagedir(pmap);
   1483 		return(ENOMEM);
   1484 	}
   1485 
   1486 	(void) pmap_extract(pmap_kernel(), pmap->pm_vptpt, &pmap->pm_pptpt);
   1487 	pmap->pm_pptpt &= PG_FRAME;
   1488 	/* Revoke cacheability and bufferability */
   1489 	/* XXX should be done better than this */
   1490 	pte = pmap_pte(pmap_kernel(), pmap->pm_vptpt);
   1491 	*pte = *pte & ~(PT_C | PT_B);
   1492 
   1493 	/* Wire in this page table */
   1494 	pmap_map_in_l1(pmap, PROCESS_PAGE_TBLS_BASE, pmap->pm_pptpt, TRUE);
   1495 
   1496 	pt->pt_flags &= ~PTFLAG_CLEAN;	/* L1 is dirty now */
   1497 
   1498 	/*
   1499 	 * Map the kernel page tables for 0xf0000000 +
   1500 	 * into the page table used to map the
   1501 	 * pmap's page tables
   1502 	 */
   1503 	bcopy((char *)(PROCESS_PAGE_TBLS_BASE
   1504 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2))
   1505 	    + ((PD_SIZE - KERNEL_PD_SIZE) >> 2)),
   1506 	    (char *)pmap->pm_vptpt + ((PD_SIZE - KERNEL_PD_SIZE) >> 2),
   1507 	    (KERNEL_PD_SIZE >> 2));
   1508 
   1509 	return(0);
   1510 }
   1511 
   1512 
   1513 /*
   1514  * Initialize a preallocated and zeroed pmap structure,
   1515  * such as one in a vmspace structure.
   1516  */
   1517 
   1518 void
   1519 pmap_pinit(pmap)
   1520 	struct pmap *pmap;
   1521 {
   1522 	int backoff = 6;
   1523 	int retry = 10;
   1524 
   1525 	PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
   1526 
   1527 	/* Keep looping until we succeed in allocating a page directory */
   1528 	while (pmap_allocpagedir(pmap) != 0) {
   1529 		/*
   1530 		 * Ok we failed to allocate a suitable block of memory for an
   1531 		 * L1 page table. This means that either:
   1532 		 * 1. 16KB of virtual address space could not be allocated
   1533 		 * 2. 16KB of physically contiguous memory on a 16KB boundary
   1534 		 *    could not be allocated.
   1535 		 *
   1536 		 * Since we cannot fail we will sleep for a while and try
   1537 		 * again.
   1538 		 *
   1539 		 * Searching for a suitable L1 PT is expensive:
   1540 		 * to avoid hogging the system when memory is really
   1541 		 * scarce, use an exponential back-off so that
   1542 		 * eventually we won't retry more than once every 8
   1543 		 * seconds.  This should allow other processes to run
   1544 		 * to completion and free up resources.
   1545 		 */
   1546 		(void) ltsleep(&lbolt, PVM, "l1ptwait", (hz << 3) >> backoff,
   1547 		    NULL);
   1548 		if (--retry == 0) {
   1549 			retry = 10;
   1550 			if (backoff)
   1551 				--backoff;
   1552 		}
   1553 	}
   1554 
   1555 	/* Map zero page for the pmap. This will also map the L2 for it */
   1556 	pmap_enter(pmap, 0x00000000, systempage.pv_pa,
   1557 	    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1558 	pmap_update(pmap);
   1559 }
   1560 
   1561 
   1562 void
   1563 pmap_freepagedir(pmap)
   1564 	struct pmap *pmap;
   1565 {
   1566 	/* Free the memory used for the page table mapping */
   1567 	if (pmap->pm_vptpt != 0)
   1568 		uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
   1569 
   1570 	/* junk the L1 page table */
   1571 	if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
   1572 		/* Add the page table to the queue */
   1573 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
   1574 		++l1pt_static_queue_count;
   1575 	} else if (l1pt_queue_count < 8) {
   1576 		/* Add the page table to the queue */
   1577 		SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
   1578 		++l1pt_queue_count;
   1579 	} else
   1580 		pmap_free_l1pt(pmap->pm_l1pt);
   1581 }
   1582 
   1583 
   1584 /*
   1585  * Retire the given physical map from service.
   1586  * Should only be called if the map contains no valid mappings.
   1587  */
   1588 
   1589 void
   1590 pmap_destroy(pmap)
   1591 	struct pmap *pmap;
   1592 {
   1593 	struct vm_page *page;
   1594 	int count;
   1595 
   1596 	if (pmap == NULL)
   1597 		return;
   1598 
   1599 	PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
   1600 
   1601 	/*
   1602 	 * Drop reference count
   1603 	 */
   1604 	simple_lock(&pmap->pm_obj.vmobjlock);
   1605 	count = --pmap->pm_obj.uo_refs;
   1606 	simple_unlock(&pmap->pm_obj.vmobjlock);
   1607 	if (count > 0) {
   1608 		return;
   1609 	}
   1610 
   1611 	/*
   1612 	 * reference count is zero, free pmap resources and then free pmap.
   1613 	 */
   1614 
   1615 	/* Remove the zero page mapping */
   1616 	pmap_remove(pmap, 0x00000000, 0x00000000 + NBPG);
   1617 	pmap_update(pmap);
   1618 
   1619 	/*
   1620 	 * Free any page tables still mapped
   1621 	 * This is only temporay until pmap_enter can count the number
   1622 	 * of mappings made in a page table. Then pmap_remove() can
   1623 	 * reduce the count and free the pagetable when the count
   1624 	 * reaches zero.  Note that entries in this list should match the
   1625 	 * contents of the ptpt, however this is faster than walking a 1024
   1626 	 * entries looking for pt's
   1627 	 * taken from i386 pmap.c
   1628 	 */
   1629 	while (pmap->pm_obj.memq.tqh_first != NULL) {
   1630 		page = pmap->pm_obj.memq.tqh_first;
   1631 #ifdef DIAGNOSTIC
   1632 		if (page->flags & PG_BUSY)
   1633 			panic("pmap_release: busy page table page");
   1634 #endif
   1635 		/* pmap_page_protect?  currently no need for it. */
   1636 
   1637 		page->wire_count = 0;
   1638 		uvm_pagefree(page);
   1639 	}
   1640 
   1641 	/* Free the page dir */
   1642 	pmap_freepagedir(pmap);
   1643 
   1644 	/* return the pmap to the pool */
   1645 	pool_put(&pmap_pmap_pool, pmap);
   1646 }
   1647 
   1648 
   1649 /*
   1650  * void pmap_reference(struct pmap *pmap)
   1651  *
   1652  * Add a reference to the specified pmap.
   1653  */
   1654 
   1655 void
   1656 pmap_reference(pmap)
   1657 	struct pmap *pmap;
   1658 {
   1659 	if (pmap == NULL)
   1660 		return;
   1661 
   1662 	simple_lock(&pmap->pm_lock);
   1663 	pmap->pm_obj.uo_refs++;
   1664 	simple_unlock(&pmap->pm_lock);
   1665 }
   1666 
   1667 /*
   1668  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   1669  *
   1670  * Return the start and end addresses of the kernel's virtual space.
   1671  * These values are setup in pmap_bootstrap and are updated as pages
   1672  * are allocated.
   1673  */
   1674 
   1675 void
   1676 pmap_virtual_space(start, end)
   1677 	vaddr_t *start;
   1678 	vaddr_t *end;
   1679 {
   1680 	*start = virtual_start;
   1681 	*end = virtual_end;
   1682 }
   1683 
   1684 
   1685 /*
   1686  * Activate the address space for the specified process.  If the process
   1687  * is the current process, load the new MMU context.
   1688  */
   1689 void
   1690 pmap_activate(p)
   1691 	struct proc *p;
   1692 {
   1693 	struct pmap *pmap = p->p_vmspace->vm_map.pmap;
   1694 	struct pcb *pcb = &p->p_addr->u_pcb;
   1695 
   1696 	(void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_pdir,
   1697 	    (paddr_t *)&pcb->pcb_pagedir);
   1698 
   1699 	PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
   1700 	    p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
   1701 
   1702 	if (p == curproc) {
   1703 		PDEBUG(0, printf("pmap_activate: setting TTB\n"));
   1704 		setttb((u_int)pcb->pcb_pagedir);
   1705 	}
   1706 #if 0
   1707 	pmap->pm_pdchanged = FALSE;
   1708 #endif
   1709 }
   1710 
   1711 
   1712 /*
   1713  * Deactivate the address space of the specified process.
   1714  */
   1715 void
   1716 pmap_deactivate(p)
   1717 	struct proc *p;
   1718 {
   1719 }
   1720 
   1721 
   1722 /*
   1723  * pmap_clean_page()
   1724  *
   1725  * This is a local function used to work out the best strategy to clean
   1726  * a single page referenced by its entry in the PV table. It's used by
   1727  * pmap_copy_page, pmap_zero page and maybe some others later on.
   1728  *
   1729  * Its policy is effectively:
   1730  *  o If there are no mappings, we don't bother doing anything with the cache.
   1731  *  o If there is one mapping, we clean just that page.
   1732  *  o If there are multiple mappings, we clean the entire cache.
   1733  *
   1734  * So that some functions can be further optimised, it returns 0 if it didn't
   1735  * clean the entire cache, or 1 if it did.
   1736  *
   1737  * XXX One bug in this routine is that if the pv_entry has a single page
   1738  * mapped at 0x00000000 a whole cache clean will be performed rather than
   1739  * just the 1 page. Since this should not occur in everyday use and if it does
   1740  * it will just result in not the most efficient clean for the page.
   1741  */
   1742 static int
   1743 pmap_clean_page(pv, is_src)
   1744 	struct pv_entry *pv;
   1745 	boolean_t is_src;
   1746 {
   1747 	struct pmap *pmap;
   1748 	struct pv_entry *npv;
   1749 	int cache_needs_cleaning = 0;
   1750 	vaddr_t page_to_clean = 0;
   1751 
   1752 	if (pv == NULL)
   1753 		/* nothing mapped in so nothing to flush */
   1754 		return (0);
   1755 
   1756 	/* Since we flush the cache each time we change curproc, we
   1757 	 * only need to flush the page if it is in the current pmap.
   1758 	 */
   1759 	if (curproc)
   1760 		pmap = curproc->p_vmspace->vm_map.pmap;
   1761 	else
   1762 		pmap = pmap_kernel();
   1763 
   1764 	for (npv = pv; npv; npv = npv->pv_next) {
   1765 		if (npv->pv_pmap == pmap) {
   1766 			/* The page is mapped non-cacheable in
   1767 			 * this map.  No need to flush the cache.
   1768 			 */
   1769 			if (npv->pv_flags & PT_NC) {
   1770 #ifdef DIAGNOSTIC
   1771 				if (cache_needs_cleaning)
   1772 					panic("pmap_clean_page: "
   1773 							"cache inconsistency");
   1774 #endif
   1775 				break;
   1776 			}
   1777 #if 0
   1778 			/* This doesn't work, because pmap_protect
   1779 			   doesn't flush changes on pages that it
   1780 			   has write-protected.  */
   1781 
   1782 			/* If the page is not writable and this
   1783 			   is the source, then there is no need
   1784 			   to flush it from the cache.  */
   1785 			else if (is_src && ! (npv->pv_flags & PT_Wr))
   1786 				continue;
   1787 #endif
   1788 			if (cache_needs_cleaning){
   1789 				page_to_clean = 0;
   1790 				break;
   1791 			}
   1792 			else
   1793 				page_to_clean = npv->pv_va;
   1794 			cache_needs_cleaning = 1;
   1795 		}
   1796 	}
   1797 
   1798 	if (page_to_clean)
   1799 		cpu_cache_purgeID_rng(page_to_clean, NBPG);
   1800 	else if (cache_needs_cleaning) {
   1801 		cpu_cache_purgeID();
   1802 		return (1);
   1803 	}
   1804 	return (0);
   1805 }
   1806 
   1807 /*
   1808  * pmap_find_pv()
   1809  *
   1810  * This is a local function that finds a PV head for a given physical page.
   1811  * This is a common op, and this function removes loads of ifdefs in the code.
   1812  */
   1813 static __inline struct pv_head *
   1814 pmap_find_pvh(phys)
   1815 	paddr_t phys;
   1816 {
   1817 	int bank, off;
   1818 	struct pv_head *pvh;
   1819 
   1820 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1)
   1821 		panic("pmap_find_pv: not a real page, phys=%lx\n", phys);
   1822 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   1823 	return (pvh);
   1824 }
   1825 
   1826 /*
   1827  * pmap_zero_page()
   1828  *
   1829  * Zero a given physical page by mapping it at a page hook point.
   1830  * In doing the zero page op, the page we zero is mapped cachable, as with
   1831  * StrongARM accesses to non-cached pages are non-burst making writing
   1832  * _any_ bulk data very slow.
   1833  */
   1834 void
   1835 pmap_zero_page(phys)
   1836 	paddr_t phys;
   1837 {
   1838 	struct pv_head *pvh;
   1839 
   1840 	/* Get an entry for this page, and clean it it. */
   1841 	pvh = pmap_find_pvh(phys);
   1842 	simple_lock(&pvh->pvh_lock);
   1843 	pmap_clean_page(pvh->pvh_list, FALSE);
   1844 	simple_unlock(&pvh->pvh_lock);
   1845 
   1846 	/*
   1847 	 * Hook in the page, zero it, and purge the cache for that
   1848 	 * zeroed page. Invalidate the TLB as needed.
   1849 	 */
   1850 	*page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
   1851 	cpu_tlb_flushD_SE(page_hook0.va);
   1852 	bzero_page(page_hook0.va);
   1853 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1854 }
   1855 
   1856 /* pmap_pageidlezero()
   1857  *
   1858  * The same as above, except that we assume that the page is not
   1859  * mapped.  This means we never have to flush the cache first.  Called
   1860  * from the idle loop.
   1861  */
   1862 boolean_t
   1863 pmap_pageidlezero(phys)
   1864     paddr_t phys;
   1865 {
   1866 	int i, *ptr;
   1867 	boolean_t rv = TRUE;
   1868 
   1869 #ifdef DIAGNOSTIC
   1870 	struct pv_head *pvh;
   1871 
   1872 	pvh = pmap_find_pvh(phys);
   1873 	if (pvh->pvh_list != NULL)
   1874 		panic("pmap_pageidlezero: zeroing mapped page\n");
   1875 #endif
   1876 
   1877 	/*
   1878 	 * Hook in the page, zero it, and purge the cache for that
   1879 	 * zeroed page. Invalidate the TLB as needed.
   1880 	 */
   1881 	*page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
   1882 	cpu_tlb_flushD_SE(page_hook0.va);
   1883 
   1884 	for (i = 0, ptr = (int *)page_hook0.va;
   1885 			i < (NBPG / sizeof(int)); i++) {
   1886 		if (sched_whichqs != 0) {
   1887 			/*
   1888 			 * A process has become ready.  Abort now,
   1889 			 * so we don't keep it waiting while we
   1890 			 * do slow memory access to finish this
   1891 			 * page.
   1892 			 */
   1893 			rv = FALSE;
   1894 			break;
   1895 		}
   1896 		*ptr++ = 0;
   1897 	}
   1898 
   1899 	if (rv)
   1900 		/*
   1901 		 * if we aborted we'll rezero this page again later so don't
   1902 		 * purge it unless we finished it
   1903 		 */
   1904 		cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1905 	return (rv);
   1906 }
   1907 
   1908 /*
   1909  * pmap_copy_page()
   1910  *
   1911  * Copy one physical page into another, by mapping the pages into
   1912  * hook points. The same comment regarding cachability as in
   1913  * pmap_zero_page also applies here.
   1914  */
   1915 void
   1916 pmap_copy_page(src, dest)
   1917 	paddr_t src;
   1918 	paddr_t dest;
   1919 {
   1920 	struct pv_head *src_pvh, *dest_pvh;
   1921 	boolean_t cleanedcache;
   1922 
   1923 	/* Get PV entries for the pages, and clean them if needed. */
   1924 	src_pvh = pmap_find_pvh(src);
   1925 
   1926 	simple_lock(&src_pvh->pvh_lock);
   1927 	cleanedcache = pmap_clean_page(src_pvh->pvh_list, TRUE);
   1928 	simple_unlock(&src_pvh->pvh_lock);
   1929 
   1930 	if (cleanedcache == 0) {
   1931 		dest_pvh = pmap_find_pvh(dest);
   1932 		simple_lock(&dest_pvh->pvh_lock);
   1933 		pmap_clean_page(dest_pvh->pvh_list, FALSE);
   1934 		simple_unlock(&dest_pvh->pvh_lock);
   1935 	}
   1936 	/*
   1937 	 * Map the pages into the page hook points, copy them, and purge
   1938 	 * the cache for the appropriate page. Invalidate the TLB
   1939 	 * as required.
   1940 	 */
   1941 	*page_hook0.pte = L2_PTE(src & PG_FRAME, AP_KRW);
   1942 	*page_hook1.pte = L2_PTE(dest & PG_FRAME, AP_KRW);
   1943 	cpu_tlb_flushD_SE(page_hook0.va);
   1944 	cpu_tlb_flushD_SE(page_hook1.va);
   1945 	bcopy_page(page_hook0.va, page_hook1.va);
   1946 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1947 	cpu_cache_purgeD_rng(page_hook1.va, NBPG);
   1948 }
   1949 
   1950 /*
   1951  * int pmap_next_phys_page(paddr_t *addr)
   1952  *
   1953  * Allocate another physical page returning true or false depending
   1954  * on whether a page could be allocated.
   1955  */
   1956 
   1957 paddr_t
   1958 pmap_next_phys_page(addr)
   1959 	paddr_t addr;
   1960 
   1961 {
   1962 	int loop;
   1963 
   1964 	if (addr < bootconfig.dram[0].address)
   1965 		return(bootconfig.dram[0].address);
   1966 
   1967 	loop = 0;
   1968 
   1969 	while (bootconfig.dram[loop].address != 0
   1970 	    && addr > (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG))
   1971 		++loop;
   1972 
   1973 	if (bootconfig.dram[loop].address == 0)
   1974 		return(0);
   1975 
   1976 	addr += NBPG;
   1977 
   1978 	if (addr >= (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG)) {
   1979 		if (bootconfig.dram[loop + 1].address == 0)
   1980 			return(0);
   1981 		addr = bootconfig.dram[loop + 1].address;
   1982 	}
   1983 
   1984 	return(addr);
   1985 }
   1986 
   1987 #if 0
   1988 void
   1989 pmap_pte_addref(pmap, va)
   1990 	struct pmap *pmap;
   1991 	vaddr_t va;
   1992 {
   1993 	pd_entry_t *pde;
   1994 	paddr_t pa;
   1995 	struct vm_page *m;
   1996 
   1997 	if (pmap == pmap_kernel())
   1998 		return;
   1999 
   2000 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   2001 	pa = pmap_pte_pa(pde);
   2002 	m = PHYS_TO_VM_PAGE(pa);
   2003 	++m->wire_count;
   2004 #ifdef MYCROFT_HACK
   2005 	printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   2006 	    pmap, va, pde, pa, m, m->wire_count);
   2007 #endif
   2008 }
   2009 
   2010 void
   2011 pmap_pte_delref(pmap, va)
   2012 	struct pmap *pmap;
   2013 	vaddr_t va;
   2014 {
   2015 	pd_entry_t *pde;
   2016 	paddr_t pa;
   2017 	struct vm_page *m;
   2018 
   2019 	if (pmap == pmap_kernel())
   2020 		return;
   2021 
   2022 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   2023 	pa = pmap_pte_pa(pde);
   2024 	m = PHYS_TO_VM_PAGE(pa);
   2025 	--m->wire_count;
   2026 #ifdef MYCROFT_HACK
   2027 	printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   2028 	    pmap, va, pde, pa, m, m->wire_count);
   2029 #endif
   2030 	if (m->wire_count == 0) {
   2031 #ifdef MYCROFT_HACK
   2032 		printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
   2033 		    pmap, va, pde, pa, m);
   2034 #endif
   2035 		pmap_unmap_in_l1(pmap, va);
   2036 		uvm_pagefree(m);
   2037 		--pmap->pm_stats.resident_count;
   2038 	}
   2039 }
   2040 #else
   2041 #define	pmap_pte_addref(pmap, va)
   2042 #define	pmap_pte_delref(pmap, va)
   2043 #endif
   2044 
   2045 /*
   2046  * Since we have a virtually indexed cache, we may need to inhibit caching if
   2047  * there is more than one mapping and at least one of them is writable.
   2048  * Since we purge the cache on every context switch, we only need to check for
   2049  * other mappings within the same pmap, or kernel_pmap.
   2050  * This function is also called when a page is unmapped, to possibly reenable
   2051  * caching on any remaining mappings.
   2052  *
   2053  * Note that the pmap must have it's ptes mapped in, and passed with ptes.
   2054  */
   2055 __inline static void
   2056 pmap_vac_me_harder(struct pmap *pmap, struct pv_head *pvh, pt_entry_t *ptes,
   2057 	boolean_t clear_cache)
   2058 {
   2059 	if (pmap == pmap_kernel())
   2060 		pmap_vac_me_kpmap(pmap, pvh, ptes, clear_cache);
   2061 	else
   2062 		pmap_vac_me_user(pmap, pvh, ptes, clear_cache);
   2063 }
   2064 
   2065 static void
   2066 pmap_vac_me_kpmap(struct pmap *pmap, struct pv_head *pvh, pt_entry_t *ptes,
   2067 	boolean_t clear_cache)
   2068 {
   2069 	int user_entries = 0;
   2070 	int user_writable = 0;
   2071 	int user_cacheable = 0;
   2072 	int kernel_entries = 0;
   2073 	int kernel_writable = 0;
   2074 	int kernel_cacheable = 0;
   2075 	struct pv_entry *pv;
   2076 	struct pmap *last_pmap = pmap;
   2077 
   2078 #ifdef DIAGNOSTIC
   2079 	if (pmap != pmap_kernel())
   2080 		panic("pmap_vac_me_kpmap: pmap != pmap_kernel()");
   2081 #endif
   2082 
   2083 	/*
   2084 	 * Pass one, see if there are both kernel and user pmaps for
   2085 	 * this page.  Calculate whether there are user-writable or
   2086 	 * kernel-writable pages.
   2087 	 */
   2088 	for (pv = pvh->pvh_list; pv != NULL; pv = pv->pv_next) {
   2089 		if (pv->pv_pmap != pmap) {
   2090 			user_entries++;
   2091 			if (pv->pv_flags & PT_Wr)
   2092 				user_writable++;
   2093 			if ((pv->pv_flags & PT_NC) == 0)
   2094 				user_cacheable++;
   2095 		} else {
   2096 			kernel_entries++;
   2097 			if (pv->pv_flags & PT_Wr)
   2098 				kernel_writable++;
   2099 			if ((pv->pv_flags & PT_NC) == 0)
   2100 				kernel_cacheable++;
   2101 		}
   2102 	}
   2103 
   2104 	/*
   2105 	 * We know we have just been updating a kernel entry, so if
   2106 	 * all user pages are already cacheable, then there is nothing
   2107 	 * further to do.
   2108 	 */
   2109 	if (kernel_entries == 0 &&
   2110 	    user_cacheable == user_entries)
   2111 		return;
   2112 
   2113 	if (user_entries) {
   2114 		/*
   2115 		 * Scan over the list again, for each entry, if it
   2116 		 * might not be set correctly, call pmap_vac_me_user
   2117 		 * to recalculate the settings.
   2118 		 */
   2119 		for (pv = pvh->pvh_list; pv; pv = pv->pv_next) {
   2120 			/*
   2121 			 * We know kernel mappings will get set
   2122 			 * correctly in other calls.  We also know
   2123 			 * that if the pmap is the same as last_pmap
   2124 			 * then we've just handled this entry.
   2125 			 */
   2126 			if (pv->pv_pmap == pmap || pv->pv_pmap == last_pmap)
   2127 				continue;
   2128 			/*
   2129 			 * If there are kernel entries and this page
   2130 			 * is writable but non-cacheable, then we can
   2131 			 * skip this entry also.
   2132 			 */
   2133 			if (kernel_entries > 0 &&
   2134 			    (pv->pv_flags & (PT_NC | PT_Wr)) ==
   2135 			    (PT_NC | PT_Wr))
   2136 				continue;
   2137 			/*
   2138 			 * Similarly if there are no kernel-writable
   2139 			 * entries and the page is already
   2140 			 * read-only/cacheable.
   2141 			 */
   2142 			if (kernel_writable == 0 &&
   2143 			    (pv->pv_flags & (PT_NC | PT_Wr)) == 0)
   2144 				continue;
   2145 			/*
   2146 			 * For some of the remaining cases, we know
   2147 			 * that we must recalculate, but for others we
   2148 			 * can't tell if they are correct or not, so
   2149 			 * we recalculate anyway.
   2150 			 */
   2151 			pmap_unmap_ptes(last_pmap);
   2152 			last_pmap = pv->pv_pmap;
   2153 			ptes = pmap_map_ptes(last_pmap);
   2154 			pmap_vac_me_user(last_pmap, pvh, ptes,
   2155 			    pmap_is_curpmap(last_pmap));
   2156 		}
   2157 		/* Restore the pte mapping that was passed to us.  */
   2158 		if (last_pmap != pmap) {
   2159 			pmap_unmap_ptes(last_pmap);
   2160 			ptes = pmap_map_ptes(pmap);
   2161 		}
   2162 		if (kernel_entries == 0)
   2163 			return;
   2164 	}
   2165 
   2166 	pmap_vac_me_user(pmap, pvh, ptes, clear_cache);
   2167 	return;
   2168 }
   2169 
   2170 static void
   2171 pmap_vac_me_user(struct pmap *pmap, struct pv_head *pvh, pt_entry_t *ptes,
   2172 	boolean_t clear_cache)
   2173 {
   2174 	struct pmap *kpmap = pmap_kernel();
   2175 	struct pv_entry *pv, *npv;
   2176 	int entries = 0;
   2177 	int writable = 0;
   2178 	int cacheable_entries = 0;
   2179 	int kern_cacheable = 0;
   2180 	int other_writable = 0;
   2181 
   2182 	pv = pvh->pvh_list;
   2183 	KASSERT(ptes != NULL);
   2184 
   2185 	/*
   2186 	 * Count mappings and writable mappings in this pmap.
   2187 	 * Include kernel mappings as part of our own.
   2188 	 * Keep a pointer to the first one.
   2189 	 */
   2190 	for (npv = pv; npv; npv = npv->pv_next) {
   2191 		/* Count mappings in the same pmap */
   2192 		if (pmap == npv->pv_pmap ||
   2193 		    kpmap == npv->pv_pmap) {
   2194 			if (entries++ == 0)
   2195 				pv = npv;
   2196 			/* Cacheable mappings */
   2197 			if ((npv->pv_flags & PT_NC) == 0) {
   2198 				cacheable_entries++;
   2199 				if (kpmap == npv->pv_pmap)
   2200 					kern_cacheable++;
   2201 			}
   2202 			/* Writable mappings */
   2203 			if (npv->pv_flags & PT_Wr)
   2204 				++writable;
   2205 		} else if (npv->pv_flags & PT_Wr)
   2206 			other_writable = 1;
   2207 	}
   2208 
   2209 	PDEBUG(3,printf("pmap_vac_me_harder: pmap %p Entries %d, "
   2210 		"writable %d cacheable %d %s\n", pmap, entries, writable,
   2211 	    	cacheable_entries, clear_cache ? "clean" : "no clean"));
   2212 
   2213 	/*
   2214 	 * Enable or disable caching as necessary.
   2215 	 * Note: the first entry might be part of the kernel pmap,
   2216 	 * so we can't assume this is indicative of the state of the
   2217 	 * other (maybe non-kpmap) entries.
   2218 	 */
   2219 	if ((entries > 1 && writable) ||
   2220 	    (entries > 0 && pmap == kpmap && other_writable)) {
   2221 		if (cacheable_entries == 0)
   2222 		    return;
   2223 		for (npv = pv; npv; npv = npv->pv_next) {
   2224 			if ((pmap == npv->pv_pmap
   2225 			    || kpmap == npv->pv_pmap) &&
   2226 			    (npv->pv_flags & PT_NC) == 0) {
   2227 				ptes[arm_byte_to_page(npv->pv_va)] &=
   2228 				    ~(PT_C | PT_B);
   2229  				npv->pv_flags |= PT_NC;
   2230 				/*
   2231 				 * If this page needs flushing from the
   2232 				 * cache, and we aren't going to do it
   2233 				 * below, do it now.
   2234 				 */
   2235 				if ((cacheable_entries < 4 &&
   2236 				    (clear_cache || npv->pv_pmap == kpmap)) ||
   2237 				    (npv->pv_pmap == kpmap &&
   2238 				    !clear_cache && kern_cacheable < 4)) {
   2239 					cpu_cache_purgeID_rng(npv->pv_va,
   2240 					    NBPG);
   2241 					cpu_tlb_flushID_SE(npv->pv_va);
   2242 				}
   2243 			}
   2244 		}
   2245 		if ((clear_cache && cacheable_entries >= 4) ||
   2246 		    kern_cacheable >= 4) {
   2247 			cpu_cache_purgeID();
   2248 			cpu_tlb_flushID();
   2249 		}
   2250 	} else if (entries > 0) {
   2251 		/*
   2252 		 * Turn cacheing back on for some pages.  If it is a kernel
   2253 		 * page, only do so if there are no other writable pages.
   2254 		 */
   2255 		for (npv = pv; npv; npv = npv->pv_next) {
   2256 			if ((pmap == npv->pv_pmap ||
   2257 			    (kpmap == npv->pv_pmap && other_writable == 0)) &&
   2258 			    (npv->pv_flags & PT_NC)) {
   2259 				ptes[arm_byte_to_page(npv->pv_va)] |=
   2260 				    (PT_C | PT_B);
   2261 				npv->pv_flags &= ~PT_NC;
   2262 			}
   2263 		}
   2264 	}
   2265 }
   2266 
   2267 /*
   2268  * pmap_remove()
   2269  *
   2270  * pmap_remove is responsible for nuking a number of mappings for a range
   2271  * of virtual address space in the current pmap. To do this efficiently
   2272  * is interesting, because in a number of cases a wide virtual address
   2273  * range may be supplied that contains few actual mappings. So, the
   2274  * optimisations are:
   2275  *  1. Try and skip over hunks of address space for which an L1 entry
   2276  *     does not exist.
   2277  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   2278  *     maybe do just a partial cache clean. This path of execution is
   2279  *     complicated by the fact that the cache must be flushed _before_
   2280  *     the PTE is nuked, being a VAC :-)
   2281  *  3. Maybe later fast-case a single page, but I don't think this is
   2282  *     going to make _that_ much difference overall.
   2283  */
   2284 
   2285 #define PMAP_REMOVE_CLEAN_LIST_SIZE	3
   2286 
   2287 void
   2288 pmap_remove(pmap, sva, eva)
   2289 	struct pmap *pmap;
   2290 	vaddr_t sva;
   2291 	vaddr_t eva;
   2292 {
   2293 	int cleanlist_idx = 0;
   2294 	struct pagelist {
   2295 		vaddr_t va;
   2296 		pt_entry_t *pte;
   2297 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   2298 	pt_entry_t *pte = 0, *ptes;
   2299 	paddr_t pa;
   2300 	int pmap_active;
   2301 	struct pv_head *pvh;
   2302 
   2303 	/* Exit quick if there is no pmap */
   2304 	if (!pmap)
   2305 		return;
   2306 
   2307 	PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n", pmap, sva, eva));
   2308 
   2309 	sva &= PG_FRAME;
   2310 	eva &= PG_FRAME;
   2311 
   2312 	/*
   2313 	 * we lock in the pmap => pv_head direction
   2314 	 */
   2315 	PMAP_MAP_TO_HEAD_LOCK();
   2316 
   2317 	ptes = pmap_map_ptes(pmap);
   2318 	/* Get a page table pointer */
   2319 	while (sva < eva) {
   2320 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   2321 			break;
   2322 		sva = (sva & PD_MASK) + NBPD;
   2323 	}
   2324 
   2325 	pte = &ptes[arm_byte_to_page(sva)];
   2326 	/* Note if the pmap is active thus require cache and tlb cleans */
   2327 	if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
   2328 	    || (pmap == pmap_kernel()))
   2329 		pmap_active = 1;
   2330 	else
   2331 		pmap_active = 0;
   2332 
   2333 	/* Now loop along */
   2334 	while (sva < eva) {
   2335 		/* Check if we can move to the next PDE (l1 chunk) */
   2336 		if (!(sva & PT_MASK))
   2337 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   2338 				sva += NBPD;
   2339 				pte += arm_byte_to_page(NBPD);
   2340 				continue;
   2341 			}
   2342 
   2343 		/* We've found a valid PTE, so this page of PTEs has to go. */
   2344 		if (pmap_pte_v(pte)) {
   2345 			int bank, off;
   2346 
   2347 			/* Update statistics */
   2348 			--pmap->pm_stats.resident_count;
   2349 
   2350 			/*
   2351 			 * Add this page to our cache remove list, if we can.
   2352 			 * If, however the cache remove list is totally full,
   2353 			 * then do a complete cache invalidation taking note
   2354 			 * to backtrack the PTE table beforehand, and ignore
   2355 			 * the lists in future because there's no longer any
   2356 			 * point in bothering with them (we've paid the
   2357 			 * penalty, so will carry on unhindered). Otherwise,
   2358 			 * when we fall out, we just clean the list.
   2359 			 */
   2360 			PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
   2361 			pa = pmap_pte_pa(pte);
   2362 
   2363 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2364 				/* Add to the clean list. */
   2365 				cleanlist[cleanlist_idx].pte = pte;
   2366 				cleanlist[cleanlist_idx].va = sva;
   2367 				cleanlist_idx++;
   2368 			} else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2369 				int cnt;
   2370 
   2371 				/* Nuke everything if needed. */
   2372 				if (pmap_active) {
   2373 					cpu_cache_purgeID();
   2374 					cpu_tlb_flushID();
   2375 				}
   2376 
   2377 				/*
   2378 				 * Roll back the previous PTE list,
   2379 				 * and zero out the current PTE.
   2380 				 */
   2381 				for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   2382 					*cleanlist[cnt].pte = 0;
   2383 					pmap_pte_delref(pmap, cleanlist[cnt].va);
   2384 				}
   2385 				*pte = 0;
   2386 				pmap_pte_delref(pmap, sva);
   2387 				cleanlist_idx++;
   2388 			} else {
   2389 				/*
   2390 				 * We've already nuked the cache and
   2391 				 * TLB, so just carry on regardless,
   2392 				 * and we won't need to do it again
   2393 				 */
   2394 				*pte = 0;
   2395 				pmap_pte_delref(pmap, sva);
   2396 			}
   2397 
   2398 			/*
   2399 			 * Update flags. In a number of circumstances,
   2400 			 * we could cluster a lot of these and do a
   2401 			 * number of sequential pages in one go.
   2402 			 */
   2403 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2404 				struct pv_entry *pve;
   2405 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2406 				simple_lock(&pvh->pvh_lock);
   2407 				pve = pmap_remove_pv(pvh, pmap, sva);
   2408 				pmap_free_pv(pmap, pve);
   2409 				pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
   2410 				simple_unlock(&pvh->pvh_lock);
   2411 			}
   2412 		}
   2413 		sva += NBPG;
   2414 		pte++;
   2415 	}
   2416 
   2417 	pmap_unmap_ptes(pmap);
   2418 	/*
   2419 	 * Now, if we've fallen through down to here, chances are that there
   2420 	 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
   2421 	 */
   2422 	if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2423 		u_int cnt;
   2424 
   2425 		for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   2426 			if (pmap_active) {
   2427 				cpu_cache_purgeID_rng(cleanlist[cnt].va, NBPG);
   2428 				*cleanlist[cnt].pte = 0;
   2429 				cpu_tlb_flushID_SE(cleanlist[cnt].va);
   2430 			} else
   2431 				*cleanlist[cnt].pte = 0;
   2432 			pmap_pte_delref(pmap, cleanlist[cnt].va);
   2433 		}
   2434 	}
   2435 	PMAP_MAP_TO_HEAD_UNLOCK();
   2436 }
   2437 
   2438 /*
   2439  * Routine:	pmap_remove_all
   2440  * Function:
   2441  *		Removes this physical page from
   2442  *		all physical maps in which it resides.
   2443  *		Reflects back modify bits to the pager.
   2444  */
   2445 
   2446 void
   2447 pmap_remove_all(pa)
   2448 	paddr_t pa;
   2449 {
   2450 	struct pv_entry *pv, *npv;
   2451 	struct pv_head *pvh;
   2452 	struct pmap *pmap;
   2453 	pt_entry_t *pte, *ptes;
   2454 
   2455 	PDEBUG(0, printf("pmap_remove_all: pa=%lx ", pa));
   2456 
   2457 	/* set pv_head => pmap locking */
   2458 	PMAP_HEAD_TO_MAP_LOCK();
   2459 
   2460 	pvh = pmap_find_pvh(pa);
   2461 	simple_lock(&pvh->pvh_lock);
   2462 
   2463 	pv = pvh->pvh_list;
   2464 	if (pv == NULL)
   2465 	{
   2466 	    PDEBUG(0, printf("free page\n"));
   2467 	    simple_unlock(&pvh->pvh_lock);
   2468 	    PMAP_HEAD_TO_MAP_UNLOCK();
   2469 	    return;
   2470 	}
   2471 	pmap_clean_page(pv, FALSE);
   2472 
   2473 	while (pv) {
   2474 		pmap = pv->pv_pmap;
   2475 		ptes = pmap_map_ptes(pmap);
   2476 		pte = &ptes[arm_byte_to_page(pv->pv_va)];
   2477 
   2478 		PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
   2479 		    pv->pv_va, pv->pv_flags));
   2480 #ifdef DEBUG
   2481 		if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)
   2482 			    || pmap_pte_pa(pte) != pa)
   2483 			panic("pmap_remove_all: bad mapping");
   2484 #endif	/* DEBUG */
   2485 
   2486 		/*
   2487 		 * Update statistics
   2488 		 */
   2489 		--pmap->pm_stats.resident_count;
   2490 
   2491 		/* Wired bit */
   2492 		if (pv->pv_flags & PT_W)
   2493 			--pmap->pm_stats.wired_count;
   2494 
   2495 		/*
   2496 		 * Invalidate the PTEs.
   2497 		 * XXX: should cluster them up and invalidate as many
   2498 		 * as possible at once.
   2499 		 */
   2500 
   2501 #ifdef needednotdone
   2502 reduce wiring count on page table pages as references drop
   2503 #endif
   2504 
   2505 		*pte = 0;
   2506 		pmap_pte_delref(pmap, pv->pv_va);
   2507 
   2508 		npv = pv->pv_next;
   2509 		pmap_free_pv(pmap, pv);
   2510 		pv = npv;
   2511 		pmap_unmap_ptes(pmap);
   2512 	}
   2513 	pvh->pvh_list = NULL;
   2514 	simple_unlock(&pvh->pvh_lock);
   2515 	PMAP_HEAD_TO_MAP_UNLOCK();
   2516 
   2517 	PDEBUG(0, printf("done\n"));
   2518 	cpu_tlb_flushID();
   2519 }
   2520 
   2521 
   2522 /*
   2523  * Set the physical protection on the specified range of this map as requested.
   2524  */
   2525 
   2526 void
   2527 pmap_protect(pmap, sva, eva, prot)
   2528 	struct pmap *pmap;
   2529 	vaddr_t sva;
   2530 	vaddr_t eva;
   2531 	vm_prot_t prot;
   2532 {
   2533 	pt_entry_t *pte = NULL, *ptes;
   2534 	int armprot;
   2535 	int flush = 0;
   2536 	paddr_t pa;
   2537 	int bank, off;
   2538 	struct pv_head *pvh;
   2539 
   2540 	PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
   2541 	    pmap, sva, eva, prot));
   2542 
   2543 	if (~prot & VM_PROT_READ) {
   2544 		/* Just remove the mappings. */
   2545 		pmap_remove(pmap, sva, eva);
   2546 		return;
   2547 	}
   2548 	if (prot & VM_PROT_WRITE) {
   2549 		/*
   2550 		 * If this is a read->write transition, just ignore it and let
   2551 		 * uvm_fault() take care of it later.
   2552 		 */
   2553 		return;
   2554 	}
   2555 
   2556 	sva &= PG_FRAME;
   2557 	eva &= PG_FRAME;
   2558 
   2559 	/* Need to lock map->head */
   2560 	PMAP_MAP_TO_HEAD_LOCK();
   2561 
   2562 	ptes = pmap_map_ptes(pmap);
   2563 	/*
   2564 	 * We need to acquire a pointer to a page table page before entering
   2565 	 * the following loop.
   2566 	 */
   2567 	while (sva < eva) {
   2568 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   2569 			break;
   2570 		sva = (sva & PD_MASK) + NBPD;
   2571 	}
   2572 
   2573 	pte = &ptes[arm_byte_to_page(sva)];
   2574 
   2575 	while (sva < eva) {
   2576 		/* only check once in a while */
   2577 		if ((sva & PT_MASK) == 0) {
   2578 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   2579 				/* We can race ahead here, to the next pde. */
   2580 				sva += NBPD;
   2581 				pte += arm_byte_to_page(NBPD);
   2582 				continue;
   2583 			}
   2584 		}
   2585 
   2586 		if (!pmap_pte_v(pte))
   2587 			goto next;
   2588 
   2589 		flush = 1;
   2590 
   2591 		armprot = 0;
   2592 		if (sva < VM_MAXUSER_ADDRESS)
   2593 			armprot |= PT_AP(AP_U);
   2594 		else if (sva < VM_MAX_ADDRESS)
   2595 			armprot |= PT_AP(AP_W);  /* XXX Ekk what is this ? */
   2596 		*pte = (*pte & 0xfffff00f) | armprot;
   2597 
   2598 		pa = pmap_pte_pa(pte);
   2599 
   2600 		/* Get the physical page index */
   2601 
   2602 		/* Clear write flag */
   2603 		if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2604 			pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2605 			simple_lock(&pvh->pvh_lock);
   2606 			(void) pmap_modify_pv(pmap, sva, pvh, PT_Wr, 0);
   2607 			pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
   2608 			simple_unlock(&pvh->pvh_lock);
   2609 		}
   2610 
   2611 next:
   2612 		sva += NBPG;
   2613 		pte++;
   2614 	}
   2615 	pmap_unmap_ptes(pmap);
   2616 	PMAP_MAP_TO_HEAD_UNLOCK();
   2617 	if (flush)
   2618 		cpu_tlb_flushID();
   2619 }
   2620 
   2621 /*
   2622  * void pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2623  * int flags)
   2624  *
   2625  *      Insert the given physical page (p) at
   2626  *      the specified virtual address (v) in the
   2627  *      target physical map with the protection requested.
   2628  *
   2629  *      If specified, the page will be wired down, meaning
   2630  *      that the related pte can not be reclaimed.
   2631  *
   2632  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2633  *      or lose information.  That is, this routine must actually
   2634  *      insert this page into the given map NOW.
   2635  */
   2636 
   2637 int
   2638 pmap_enter(pmap, va, pa, prot, flags)
   2639 	struct pmap *pmap;
   2640 	vaddr_t va;
   2641 	paddr_t pa;
   2642 	vm_prot_t prot;
   2643 	int flags;
   2644 {
   2645 	pt_entry_t *pte, *ptes;
   2646 	u_int npte;
   2647 	int bank, off;
   2648 	paddr_t opa;
   2649 	int nflags;
   2650 	boolean_t wired = (flags & PMAP_WIRED) != 0;
   2651 	struct pv_entry *pve;
   2652 	struct pv_head	*pvh;
   2653 	int error;
   2654 
   2655 	PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
   2656 	    va, pa, pmap, prot, wired));
   2657 
   2658 #ifdef DIAGNOSTIC
   2659 	/* Valid address ? */
   2660 	if (va >= (KERNEL_VM_BASE + KERNEL_VM_SIZE))
   2661 		panic("pmap_enter: too big");
   2662 	if (pmap != pmap_kernel() && va != 0) {
   2663 		if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
   2664 			panic("pmap_enter: kernel page in user map");
   2665 	} else {
   2666 		if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
   2667 			panic("pmap_enter: user page in kernel map");
   2668 		if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
   2669 			panic("pmap_enter: entering PT page");
   2670 	}
   2671 #endif
   2672 	/* get lock */
   2673 	PMAP_MAP_TO_HEAD_LOCK();
   2674 	/*
   2675 	 * Get a pointer to the pte for this virtual address. If the
   2676 	 * pte pointer is NULL then we are missing the L2 page table
   2677 	 * so we need to create one.
   2678 	 */
   2679 	/* XXX horrible hack to get us working with lockdebug */
   2680 	simple_lock(&pmap->pm_obj.vmobjlock);
   2681 	pte = pmap_pte(pmap, va);
   2682 	if (!pte) {
   2683 		struct vm_page *ptp;
   2684 
   2685 		/* if failure is allowed then don't try too hard */
   2686 		ptp = pmap_get_ptp(pmap, va, flags & PMAP_CANFAIL);
   2687 		if (ptp == NULL) {
   2688 			if (flags & PMAP_CANFAIL) {
   2689 				error = ENOMEM;
   2690 				goto out;
   2691 			}
   2692 			panic("pmap_enter: get ptp failed");
   2693 		}
   2694 
   2695 		pte = pmap_pte(pmap, va);
   2696 #ifdef DIAGNOSTIC
   2697 		if (!pte)
   2698 			panic("pmap_enter: no pte");
   2699 #endif
   2700 	}
   2701 
   2702 	nflags = 0;
   2703 	if (prot & VM_PROT_WRITE)
   2704 		nflags |= PT_Wr;
   2705 	if (wired)
   2706 		nflags |= PT_W;
   2707 
   2708 	/* More debugging info */
   2709 	PDEBUG(5, printf("pmap_enter: pte for V%08lx = V%p (%08x)\n", va, pte,
   2710 	    *pte));
   2711 
   2712 	/* Is the pte valid ? If so then this page is already mapped */
   2713 	if (pmap_pte_v(pte)) {
   2714 		/* Get the physical address of the current page mapped */
   2715 		opa = pmap_pte_pa(pte);
   2716 
   2717 #ifdef MYCROFT_HACK
   2718 		printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx\n", pmap, va, pa, opa);
   2719 #endif
   2720 
   2721 		/* Are we mapping the same page ? */
   2722 		if (opa == pa) {
   2723 			/* All we must be doing is changing the protection */
   2724 			PDEBUG(0, printf("Case 02 in pmap_enter (V%08lx P%08lx)\n",
   2725 			    va, pa));
   2726 
   2727 			/* Has the wiring changed ? */
   2728 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2729 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2730 				simple_lock(&pvh->pvh_lock);
   2731 				(void) pmap_modify_pv(pmap, va, pvh,
   2732 				    PT_Wr | PT_W, nflags);
   2733 				simple_unlock(&pvh->pvh_lock);
   2734  			} else {
   2735 				pvh = NULL;
   2736 			}
   2737 		} else {
   2738 			/* We are replacing the page with a new one. */
   2739 			cpu_cache_purgeID_rng(va, NBPG);
   2740 
   2741 			PDEBUG(0, printf("Case 03 in pmap_enter (V%08lx P%08lx P%08lx)\n",
   2742 			    va, pa, opa));
   2743 
   2744 			/*
   2745 			 * If it is part of our managed memory then we
   2746 			 * must remove it from the PV list
   2747 			 */
   2748 			if ((bank = vm_physseg_find(atop(opa), &off)) != -1) {
   2749 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2750 				simple_lock(&pvh->pvh_lock);
   2751 				pve = pmap_remove_pv(pvh, pmap, va);
   2752 				simple_unlock(&pvh->pvh_lock);
   2753 			} else {
   2754 				pve = NULL;
   2755 			}
   2756 
   2757 			goto enter;
   2758 		}
   2759 	} else {
   2760 		opa = 0;
   2761 		pve = NULL;
   2762 		pmap_pte_addref(pmap, va);
   2763 
   2764 		/* pte is not valid so we must be hooking in a new page */
   2765 		++pmap->pm_stats.resident_count;
   2766 
   2767 	enter:
   2768 		/*
   2769 		 * Enter on the PV list if part of our managed memory
   2770 		 */
   2771 		bank = vm_physseg_find(atop(pa), &off);
   2772 
   2773 		if (pmap_initialized && (bank != -1)) {
   2774 			pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2775 			if (pve == NULL) {
   2776 				pve = pmap_alloc_pv(pmap, ALLOCPV_NEED);
   2777 				if (pve == NULL) {
   2778 					if (flags & PMAP_CANFAIL) {
   2779 						error = ENOMEM;
   2780 						goto out;
   2781 					}
   2782 					panic("pmap_enter: no pv entries available");
   2783 				}
   2784 			}
   2785 			/* enter_pv locks pvh when adding */
   2786 			pmap_enter_pv(pvh, pve, pmap, va, NULL, nflags);
   2787 		} else {
   2788 			pvh = NULL;
   2789 			if (pve != NULL)
   2790 				pmap_free_pv(pmap, pve);
   2791 		}
   2792 	}
   2793 
   2794 #ifdef MYCROFT_HACK
   2795 	if (mycroft_hack)
   2796 		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);
   2797 #endif
   2798 
   2799 	/* Construct the pte, giving the correct access. */
   2800 	npte = (pa & PG_FRAME);
   2801 
   2802 	/* VA 0 is magic. */
   2803 	if (pmap != pmap_kernel() && va != 0)
   2804 		npte |= PT_AP(AP_U);
   2805 
   2806 	if (pmap_initialized && bank != -1) {
   2807 #ifdef DIAGNOSTIC
   2808 		if ((flags & VM_PROT_ALL) & ~prot)
   2809 			panic("pmap_enter: access_type exceeds prot");
   2810 #endif
   2811 		npte |= PT_C | PT_B;
   2812 		if (flags & VM_PROT_WRITE) {
   2813 			npte |= L2_SPAGE | PT_AP(AP_W);
   2814 			vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   2815 		} else if (flags & VM_PROT_ALL) {
   2816 			npte |= L2_SPAGE;
   2817 			vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   2818 		} else
   2819 			npte |= L2_INVAL;
   2820 	} else {
   2821 		if (prot & VM_PROT_WRITE)
   2822 			npte |= L2_SPAGE | PT_AP(AP_W);
   2823 		else if (prot & VM_PROT_ALL)
   2824 			npte |= L2_SPAGE;
   2825 		else
   2826 			npte |= L2_INVAL;
   2827 	}
   2828 
   2829 #ifdef MYCROFT_HACK
   2830 	if (mycroft_hack)
   2831 		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);
   2832 #endif
   2833 
   2834 	*pte = npte;
   2835 
   2836 	if (pmap_initialized && bank != -1)
   2837 	{
   2838 		boolean_t pmap_active = FALSE;
   2839 		/* XXX this will change once the whole of pmap_enter uses
   2840 		 * map_ptes
   2841 		 */
   2842 		ptes = pmap_map_ptes(pmap);
   2843 		if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
   2844 		    || (pmap == pmap_kernel()))
   2845 			pmap_active = TRUE;
   2846 		simple_lock(&pvh->pvh_lock);
   2847  		pmap_vac_me_harder(pmap, pvh, ptes, pmap_active);
   2848 		simple_unlock(&pvh->pvh_lock);
   2849 		pmap_unmap_ptes(pmap);
   2850 	}
   2851 
   2852 	/* Better flush the TLB ... */
   2853 	cpu_tlb_flushID_SE(va);
   2854 	error = 0;
   2855 out:
   2856 	simple_unlock(&pmap->pm_obj.vmobjlock);
   2857 	PMAP_MAP_TO_HEAD_UNLOCK();
   2858 	PDEBUG(5, printf("pmap_enter: pte = V%p %08x\n", pte, *pte));
   2859 
   2860 	return error;
   2861 }
   2862 
   2863 void
   2864 pmap_kenter_pa(va, pa, prot)
   2865 	vaddr_t va;
   2866 	paddr_t pa;
   2867 	vm_prot_t prot;
   2868 {
   2869 	struct pmap *pmap = pmap_kernel();
   2870 	pt_entry_t *pte;
   2871 	struct vm_page *pg;
   2872 
   2873 	if (!pmap_pde_v(pmap_pde(pmap, va))) {
   2874 
   2875 		/*
   2876 		 * For the kernel pmaps it would be better to ensure
   2877 		 * that they are always present, and to grow the
   2878 		 * kernel as required.
   2879 		 */
   2880 
   2881 	    	/* must lock the pmap */
   2882 	    	simple_lock(&(pmap_kernel()->pm_obj.vmobjlock));
   2883 		/* Allocate a page table */
   2884 		pg = uvm_pagealloc(&(pmap_kernel()->pm_obj), 0, NULL,
   2885 		    UVM_PGA_USERESERVE | UVM_PGA_ZERO);
   2886 		if (pg == NULL) {
   2887 			panic("pmap_kenter_pa: no free pages");
   2888 		}
   2889 		pg->flags &= ~PG_BUSY;	/* never busy */
   2890 
   2891 		/* Wire this page table into the L1. */
   2892 		pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(pg), TRUE);
   2893 		simple_unlock(&(pmap_kernel()->pm_obj.vmobjlock));
   2894 	}
   2895 	pte = vtopte(va);
   2896 	KASSERT(!pmap_pte_v(pte));
   2897 	*pte = L2_PTE(pa, AP_KRW);
   2898 }
   2899 
   2900 void
   2901 pmap_kremove(va, len)
   2902 	vaddr_t va;
   2903 	vsize_t len;
   2904 {
   2905 	pt_entry_t *pte;
   2906 
   2907 	for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
   2908 
   2909 		/*
   2910 		 * We assume that we will only be called with small
   2911 		 * regions of memory.
   2912 		 */
   2913 
   2914 		KASSERT(pmap_pde_v(pmap_pde(pmap_kernel(), va)));
   2915 		pte = vtopte(va);
   2916 		cpu_cache_purgeID_rng(va, PAGE_SIZE);
   2917 		*pte = 0;
   2918 		cpu_tlb_flushID_SE(va);
   2919 	}
   2920 }
   2921 
   2922 /*
   2923  * pmap_page_protect:
   2924  *
   2925  * Lower the permission for all mappings to a given page.
   2926  */
   2927 
   2928 void
   2929 pmap_page_protect(pg, prot)
   2930 	struct vm_page *pg;
   2931 	vm_prot_t prot;
   2932 {
   2933 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2934 
   2935 	PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n", pa, prot));
   2936 
   2937 	switch(prot) {
   2938 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   2939 	case VM_PROT_READ|VM_PROT_WRITE:
   2940 		return;
   2941 
   2942 	case VM_PROT_READ:
   2943 	case VM_PROT_READ|VM_PROT_EXECUTE:
   2944 		pmap_copy_on_write(pa);
   2945 		break;
   2946 
   2947 	default:
   2948 		pmap_remove_all(pa);
   2949 		break;
   2950 	}
   2951 }
   2952 
   2953 
   2954 /*
   2955  * Routine:	pmap_unwire
   2956  * Function:	Clear the wired attribute for a map/virtual-address
   2957  *		pair.
   2958  * In/out conditions:
   2959  *		The mapping must already exist in the pmap.
   2960  */
   2961 
   2962 void
   2963 pmap_unwire(pmap, va)
   2964 	struct pmap *pmap;
   2965 	vaddr_t va;
   2966 {
   2967 	pt_entry_t *pte;
   2968 	paddr_t pa;
   2969 	int bank, off;
   2970 	struct pv_head *pvh;
   2971 
   2972 	/*
   2973 	 * Make sure pmap is valid. -dct
   2974 	 */
   2975 	if (pmap == NULL)
   2976 		return;
   2977 
   2978 	/* Get the pte */
   2979 	pte = pmap_pte(pmap, va);
   2980 	if (!pte)
   2981 		return;
   2982 
   2983 	/* Extract the physical address of the page */
   2984 	pa = pmap_pte_pa(pte);
   2985 
   2986 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2987 		return;
   2988 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2989 	simple_lock(&pvh->pvh_lock);
   2990 	/* Update the wired bit in the pv entry for this page. */
   2991 	(void) pmap_modify_pv(pmap, va, pvh, PT_W, 0);
   2992 	simple_unlock(&pvh->pvh_lock);
   2993 }
   2994 
   2995 /*
   2996  * pt_entry_t *pmap_pte(struct pmap *pmap, vaddr_t va)
   2997  *
   2998  * Return the pointer to a page table entry corresponding to the supplied
   2999  * virtual address.
   3000  *
   3001  * The page directory is first checked to make sure that a page table
   3002  * for the address in question exists and if it does a pointer to the
   3003  * entry is returned.
   3004  *
   3005  * The way this works is that that the kernel page tables are mapped
   3006  * into the memory map at ALT_PAGE_TBLS_BASE to ALT_PAGE_TBLS_BASE+4MB.
   3007  * This allows page tables to be located quickly.
   3008  */
   3009 pt_entry_t *
   3010 pmap_pte(pmap, va)
   3011 	struct pmap *pmap;
   3012 	vaddr_t va;
   3013 {
   3014 	pt_entry_t *ptp;
   3015 	pt_entry_t *result;
   3016 
   3017 	/* The pmap must be valid */
   3018 	if (!pmap)
   3019 		return(NULL);
   3020 
   3021 	/* Return the address of the pte */
   3022 	PDEBUG(10, printf("pmap_pte: pmap=%p va=V%08lx pde = V%p (%08X)\n",
   3023 	    pmap, va, pmap_pde(pmap, va), *(pmap_pde(pmap, va))));
   3024 
   3025 	/* Do we have a valid pde ? If not we don't have a page table */
   3026 	if (!pmap_pde_v(pmap_pde(pmap, va))) {
   3027 		PDEBUG(0, printf("pmap_pte: failed - pde = %p\n",
   3028 		    pmap_pde(pmap, va)));
   3029 		return(NULL);
   3030 	}
   3031 
   3032 	PDEBUG(10, printf("pmap pagetable = P%08lx current = P%08x\n",
   3033 	    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   3034 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   3035 	    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)));
   3036 
   3037 	/*
   3038 	 * If the pmap is the kernel pmap or the pmap is the active one
   3039 	 * then we can just return a pointer to entry relative to
   3040 	 * PROCESS_PAGE_TBLS_BASE.
   3041 	 * Otherwise we need to map the page tables to an alternative
   3042 	 * address and reference them there.
   3043 	 */
   3044 	if (pmap == pmap_kernel() || pmap->pm_pptpt
   3045 	    == (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   3046 	    + ((PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) &
   3047 	    ~3) + (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   3048 		ptp = (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   3049 	} else {
   3050 		struct proc *p = curproc;
   3051 
   3052 		/* If we don't have a valid curproc use proc0 */
   3053 		/* Perhaps we should just use kernel_pmap instead */
   3054 		if (p == NULL)
   3055 			p = &proc0;
   3056 #ifdef DIAGNOSTIC
   3057 		/*
   3058 		 * The pmap should always be valid for the process so
   3059 		 * panic if it is not.
   3060 		 */
   3061 		if (!p->p_vmspace || !p->p_vmspace->vm_map.pmap) {
   3062 			printf("pmap_pte: va=%08lx p=%p vm=%p\n",
   3063 			    va, p, p->p_vmspace);
   3064 			console_debugger();
   3065 		}
   3066 		/*
   3067 		 * The pmap for the current process should be mapped. If it
   3068 		 * is not then we have a problem.
   3069 		 */
   3070 		if (p->p_vmspace->vm_map.pmap->pm_pptpt !=
   3071 		    (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   3072 		    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   3073 		    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   3074 			printf("pmap pagetable = P%08lx current = P%08x ",
   3075 			    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   3076 			    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   3077 			    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) &
   3078 			    PG_FRAME));
   3079 			printf("pptpt=%lx\n", p->p_vmspace->vm_map.pmap->pm_pptpt);
   3080 			panic("pmap_pte: current and pmap mismatch\n");
   3081 		}
   3082 #endif
   3083 
   3084 		ptp = (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   3085 		pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   3086 		    pmap->pm_pptpt, FALSE);
   3087 		cpu_tlb_flushD();
   3088 	}
   3089 	PDEBUG(10, printf("page tables base = %p offset=%lx\n", ptp,
   3090 	    ((va >> (PGSHIFT-2)) & ~3)));
   3091 	result = (pt_entry_t *)((char *)ptp + ((va >> (PGSHIFT-2)) & ~3));
   3092 	return(result);
   3093 }
   3094 
   3095 /*
   3096  * Routine:  pmap_extract
   3097  * Function:
   3098  *           Extract the physical page address associated
   3099  *           with the given map/virtual_address pair.
   3100  */
   3101 boolean_t
   3102 pmap_extract(pmap, va, pap)
   3103 	struct pmap *pmap;
   3104 	vaddr_t va;
   3105 	paddr_t *pap;
   3106 {
   3107 	pt_entry_t *pte, *ptes;
   3108 	paddr_t pa;
   3109 
   3110 	PDEBUG(5, printf("pmap_extract: pmap=%p, va=V%08lx\n", pmap, va));
   3111 
   3112 	/*
   3113 	 * Get the pte for this virtual address.
   3114 	 */
   3115 	ptes = pmap_map_ptes(pmap);
   3116 	pte = &ptes[arm_byte_to_page(va)];
   3117 
   3118 	/*
   3119 	 * If there is no pte then there is no page table etc.
   3120 	 * Is the pte valid ? If not then no paged is actually mapped here
   3121 	 */
   3122 	if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)){
   3123 	    pmap_unmap_ptes(pmap);
   3124     	    return (FALSE);
   3125 	}
   3126 
   3127 	/* Return the physical address depending on the PTE type */
   3128 	/* XXX What about L1 section mappings ? */
   3129 	if ((*(pte) & L2_MASK) == L2_LPAGE) {
   3130 		/* Extract the physical address from the pte */
   3131 		pa = (*(pte)) & ~(L2_LPAGE_SIZE - 1);
   3132 
   3133 		PDEBUG(5, printf("pmap_extract: LPAGE pa = P%08lx\n",
   3134 		    (pa | (va & (L2_LPAGE_SIZE - 1)))));
   3135 
   3136 		if (pap != NULL)
   3137 			*pap = pa | (va & (L2_LPAGE_SIZE - 1));
   3138 	} else {
   3139 		/* Extract the physical address from the pte */
   3140 		pa = pmap_pte_pa(pte);
   3141 
   3142 		PDEBUG(5, printf("pmap_extract: SPAGE pa = P%08lx\n",
   3143 		    (pa | (va & ~PG_FRAME))));
   3144 
   3145 		if (pap != NULL)
   3146 			*pap = pa | (va & ~PG_FRAME);
   3147 	}
   3148 	pmap_unmap_ptes(pmap);
   3149 	return (TRUE);
   3150 }
   3151 
   3152 
   3153 /*
   3154  * Copy the range specified by src_addr/len from the source map to the
   3155  * range dst_addr/len in the destination map.
   3156  *
   3157  * This routine is only advisory and need not do anything.
   3158  */
   3159 
   3160 void
   3161 pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
   3162 	struct pmap *dst_pmap;
   3163 	struct pmap *src_pmap;
   3164 	vaddr_t dst_addr;
   3165 	vsize_t len;
   3166 	vaddr_t src_addr;
   3167 {
   3168 	PDEBUG(0, printf("pmap_copy(%p, %p, %lx, %lx, %lx)\n",
   3169 	    dst_pmap, src_pmap, dst_addr, len, src_addr));
   3170 }
   3171 
   3172 #if defined(PMAP_DEBUG)
   3173 void
   3174 pmap_dump_pvlist(phys, m)
   3175 	vaddr_t phys;
   3176 	char *m;
   3177 {
   3178 	struct pv_head *pvh;
   3179 	struct pv_entry *pv;
   3180 	int bank, off;
   3181 
   3182 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1) {
   3183 		printf("INVALID PA\n");
   3184 		return;
   3185 	}
   3186 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3187 	simple_lock(&pvh->pvh_lock);
   3188 	printf("%s %08lx:", m, phys);
   3189 	if (pvh->pvh_list == NULL) {
   3190 		printf(" no mappings\n");
   3191 		return;
   3192 	}
   3193 
   3194 	for (pv = pvh->pvh_list; pv; pv = pv->pv_next)
   3195 		printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
   3196 		    pv->pv_va, pv->pv_flags);
   3197 
   3198 	printf("\n");
   3199 	simple_unlock(&pvh->pvh_lock);
   3200 }
   3201 
   3202 #endif	/* PMAP_DEBUG */
   3203 
   3204 __inline static boolean_t
   3205 pmap_testbit(pa, setbits)
   3206 	paddr_t pa;
   3207 	unsigned int setbits;
   3208 {
   3209 	int bank, off;
   3210 
   3211 	PDEBUG(1, printf("pmap_testbit: pa=%08lx set=%08x\n", pa, setbits));
   3212 
   3213 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3214 		return(FALSE);
   3215 
   3216 	/*
   3217 	 * Check saved info only
   3218 	 */
   3219 	if (vm_physmem[bank].pmseg.attrs[off] & setbits) {
   3220 		PDEBUG(0, printf("pmap_attributes = %02x\n",
   3221 		    vm_physmem[bank].pmseg.attrs[off]));
   3222 		return(TRUE);
   3223 	}
   3224 
   3225 	return(FALSE);
   3226 }
   3227 
   3228 static pt_entry_t *
   3229 pmap_map_ptes(struct pmap *pmap)
   3230 {
   3231     	struct proc *p;
   3232 
   3233     	/* the kernel's pmap is always accessible */
   3234 	if (pmap == pmap_kernel()) {
   3235 		return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE ;
   3236 	}
   3237 
   3238 	if (pmap_is_curpmap(pmap)) {
   3239 		simple_lock(&pmap->pm_obj.vmobjlock);
   3240 		return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   3241 	}
   3242 
   3243 	p = curproc;
   3244 
   3245 	if (p == NULL)
   3246 		p = &proc0;
   3247 
   3248 	/* need to lock both curpmap and pmap: use ordered locking */
   3249 	if ((unsigned) pmap < (unsigned) curproc->p_vmspace->vm_map.pmap) {
   3250 		simple_lock(&pmap->pm_obj.vmobjlock);
   3251 		simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3252 	} else {
   3253 		simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3254 		simple_lock(&pmap->pm_obj.vmobjlock);
   3255 	}
   3256 
   3257 	pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   3258 			pmap->pm_pptpt, FALSE);
   3259 	cpu_tlb_flushD();
   3260 	return (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   3261 }
   3262 
   3263 /*
   3264  * pmap_unmap_ptes: unlock the PTE mapping of "pmap"
   3265  */
   3266 
   3267 static void
   3268 pmap_unmap_ptes(pmap)
   3269 	struct pmap *pmap;
   3270 {
   3271 	if (pmap == pmap_kernel()) {
   3272 		return;
   3273 	}
   3274 	if (pmap_is_curpmap(pmap)) {
   3275 		simple_unlock(&pmap->pm_obj.vmobjlock);
   3276 	} else {
   3277 		simple_unlock(&pmap->pm_obj.vmobjlock);
   3278 		simple_unlock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3279 	}
   3280 }
   3281 
   3282 /*
   3283  * Modify pte bits for all ptes corresponding to the given physical address.
   3284  * We use `maskbits' rather than `clearbits' because we're always passing
   3285  * constants and the latter would require an extra inversion at run-time.
   3286  */
   3287 
   3288 static void
   3289 pmap_clearbit(pa, maskbits)
   3290 	paddr_t pa;
   3291 	unsigned int maskbits;
   3292 {
   3293 	struct pv_entry *pv;
   3294 	struct pv_head *pvh;
   3295 	pt_entry_t *pte;
   3296 	vaddr_t va;
   3297 	int bank, off, tlbentry;
   3298 
   3299 	PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
   3300 	    pa, maskbits));
   3301 
   3302 	tlbentry = 0;
   3303 
   3304 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3305 		return;
   3306 	PMAP_HEAD_TO_MAP_LOCK();
   3307 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3308 	simple_lock(&pvh->pvh_lock);
   3309 
   3310 	/*
   3311 	 * Clear saved attributes (modify, reference)
   3312 	 */
   3313 	vm_physmem[bank].pmseg.attrs[off] &= ~maskbits;
   3314 
   3315 	if (pvh->pvh_list == NULL) {
   3316 		simple_unlock(&pvh->pvh_lock);
   3317 		PMAP_HEAD_TO_MAP_UNLOCK();
   3318 		return;
   3319 	}
   3320 
   3321 	/*
   3322 	 * Loop over all current mappings setting/clearing as appropos
   3323 	 */
   3324 	for (pv = pvh->pvh_list; pv; pv = pv->pv_next) {
   3325 		va = pv->pv_va;
   3326 		pv->pv_flags &= ~maskbits;
   3327 		pte = pmap_pte(pv->pv_pmap, va);
   3328 		KASSERT(pte != NULL);
   3329 		if (maskbits & (PT_Wr|PT_M))
   3330 		{
   3331 		    if ((pv->pv_flags & PT_NC))
   3332 		    {
   3333 			/*
   3334 			 * entry is not cacheable, so reenable the cache,
   3335 			 * nothing to flush
   3336 			 */
   3337 			*pte |= (PT_C | PT_B);
   3338 			pv->pv_flags &= ~PT_NC;
   3339 		    } else {
   3340 			/*
   3341 			 * entry is cacheable check if pmap is current if it
   3342 			 * is flush it, otherwise it won't be in the cache
   3343 			 */
   3344 			if (pmap_is_curpmap(pv->pv_pmap))
   3345 			{
   3346 			    /* entry is in current pmap purge it */
   3347 			    cpu_cache_purgeID_rng(pv->pv_va, NBPG);
   3348 			}
   3349 		    }
   3350 
   3351 		    /* make the pte read only */
   3352 	    	    *pte &= ~PT_AP(AP_W);
   3353 
   3354 	    	    if (pmap_is_curpmap(pv->pv_pmap))
   3355 			/*
   3356 			 * if we had cacheable pte's we'd clean the pte out to
   3357 			 * memory here
   3358 			 */
   3359     			/*
   3360 			 * flush tlb entry as it's in the current pmap
   3361 			 */
   3362 			cpu_tlb_flushID_SE(pv->pv_va);
   3363 
   3364 		}
   3365 		if (maskbits & PT_H)
   3366 			*pte = (*pte & ~L2_MASK) | L2_INVAL;
   3367 	}
   3368 	simple_unlock(&pvh->pvh_lock);
   3369 	PMAP_HEAD_TO_MAP_UNLOCK();
   3370 }
   3371 
   3372 
   3373 boolean_t
   3374 pmap_clear_modify(pg)
   3375 	struct vm_page *pg;
   3376 {
   3377 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3378 	boolean_t rv;
   3379 
   3380 	PDEBUG(0, printf("pmap_clear_modify pa=%08lx\n", pa));
   3381 	rv = pmap_testbit(pa, PT_M);
   3382 	pmap_clearbit(pa, PT_M);
   3383 	return rv;
   3384 }
   3385 
   3386 
   3387 boolean_t
   3388 pmap_clear_reference(pg)
   3389 	struct vm_page *pg;
   3390 {
   3391 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3392 	boolean_t rv;
   3393 
   3394 	PDEBUG(0, printf("pmap_clear_reference pa=%08lx\n", pa));
   3395 	rv = pmap_testbit(pa, PT_H);
   3396 	pmap_clearbit(pa, PT_H);
   3397 	return rv;
   3398 }
   3399 
   3400 
   3401 void
   3402 pmap_copy_on_write(pa)
   3403 	paddr_t pa;
   3404 {
   3405 	PDEBUG(0, printf("pmap_copy_on_write pa=%08lx\n", pa));
   3406 	pmap_clearbit(pa, PT_Wr);
   3407 }
   3408 
   3409 
   3410 boolean_t
   3411 pmap_is_modified(pg)
   3412 	struct vm_page *pg;
   3413 {
   3414 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3415 	boolean_t result;
   3416 
   3417 	result = pmap_testbit(pa, PT_M);
   3418 	PDEBUG(1, printf("pmap_is_modified pa=%08lx %x\n", pa, result));
   3419 	return (result);
   3420 }
   3421 
   3422 
   3423 boolean_t
   3424 pmap_is_referenced(pg)
   3425 	struct vm_page *pg;
   3426 {
   3427 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3428 	boolean_t result;
   3429 
   3430 	result = pmap_testbit(pa, PT_H);
   3431 	PDEBUG(0, printf("pmap_is_referenced pa=%08lx %x\n", pa, result));
   3432 	return (result);
   3433 }
   3434 
   3435 
   3436 int
   3437 pmap_modified_emulation(pmap, va)
   3438 	struct pmap *pmap;
   3439 	vaddr_t va;
   3440 {
   3441 	pt_entry_t *pte;
   3442 	paddr_t pa;
   3443 	int bank, off;
   3444 	struct pv_head *pvh;
   3445 	u_int flags;
   3446 
   3447 	PDEBUG(2, printf("pmap_modified_emulation\n"));
   3448 
   3449 	/* Get the pte */
   3450 	pte = pmap_pte(pmap, va);
   3451 	if (!pte) {
   3452 		PDEBUG(2, printf("no pte\n"));
   3453 		return(0);
   3454 	}
   3455 
   3456 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   3457 
   3458 	/* Check for a zero pte */
   3459 	if (*pte == 0)
   3460 		return(0);
   3461 
   3462 	/* This can happen if user code tries to access kernel memory. */
   3463 	if ((*pte & PT_AP(AP_W)) != 0)
   3464 		return (0);
   3465 
   3466 	/* Extract the physical address of the page */
   3467 	pa = pmap_pte_pa(pte);
   3468 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3469 		return(0);
   3470 
   3471 	PMAP_HEAD_TO_MAP_LOCK();
   3472 	/* Get the current flags for this page. */
   3473 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3474 	/* XXX: needed if we hold head->map lock? */
   3475 	simple_lock(&pvh->pvh_lock);
   3476 
   3477 	flags = pmap_modify_pv(pmap, va, pvh, 0, 0);
   3478 	PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
   3479 
   3480 	/*
   3481 	 * Do the flags say this page is writable ? If not then it is a
   3482 	 * genuine write fault. If yes then the write fault is our fault
   3483 	 * as we did not reflect the write access in the PTE. Now we know
   3484 	 * a write has occurred we can correct this and also set the
   3485 	 * modified bit
   3486 	 */
   3487 	if (~flags & PT_Wr) {
   3488 	    	simple_unlock(&pvh->pvh_lock);
   3489 		PMAP_HEAD_TO_MAP_UNLOCK();
   3490 		return(0);
   3491 	}
   3492 
   3493 	PDEBUG(0, printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %p (%08x)\n",
   3494 	    va, pte, *pte));
   3495 	vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   3496 	*pte = (*pte & ~L2_MASK) | L2_SPAGE | PT_AP(AP_W);
   3497 	PDEBUG(0, printf("->(%08x)\n", *pte));
   3498 
   3499 	simple_unlock(&pvh->pvh_lock);
   3500 	PMAP_HEAD_TO_MAP_UNLOCK();
   3501 	/* Return, indicating the problem has been dealt with */
   3502 	cpu_tlb_flushID_SE(va);
   3503 	return(1);
   3504 }
   3505 
   3506 
   3507 int
   3508 pmap_handled_emulation(pmap, va)
   3509 	struct pmap *pmap;
   3510 	vaddr_t va;
   3511 {
   3512 	pt_entry_t *pte;
   3513 	paddr_t pa;
   3514 	int bank, off;
   3515 
   3516 	PDEBUG(2, printf("pmap_handled_emulation\n"));
   3517 
   3518 	/* Get the pte */
   3519 	pte = pmap_pte(pmap, va);
   3520 	if (!pte) {
   3521 		PDEBUG(2, printf("no pte\n"));
   3522 		return(0);
   3523 	}
   3524 
   3525 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   3526 
   3527 	/* Check for a zero pte */
   3528 	if (*pte == 0)
   3529 		return(0);
   3530 
   3531 	/* This can happen if user code tries to access kernel memory. */
   3532 	if ((*pte & L2_MASK) != L2_INVAL)
   3533 		return (0);
   3534 
   3535 	/* Extract the physical address of the page */
   3536 	pa = pmap_pte_pa(pte);
   3537 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3538 		return(0);
   3539 
   3540 	/*
   3541 	 * Ok we just enable the pte and mark the attibs as handled
   3542 	 */
   3543 	PDEBUG(0, printf("pmap_handled_emulation: Got a hit va=%08lx pte = %p (%08x)\n",
   3544 	    va, pte, *pte));
   3545 	vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   3546 	*pte = (*pte & ~L2_MASK) | L2_SPAGE;
   3547 	PDEBUG(0, printf("->(%08x)\n", *pte));
   3548 
   3549 	/* Return, indicating the problem has been dealt with */
   3550 	cpu_tlb_flushID_SE(va);
   3551 	return(1);
   3552 }
   3553 
   3554 
   3555 
   3556 
   3557 /*
   3558  * pmap_collect: free resources held by a pmap
   3559  *
   3560  * => optional function.
   3561  * => called when a process is swapped out to free memory.
   3562  */
   3563 
   3564 void
   3565 pmap_collect(pmap)
   3566 	struct pmap *pmap;
   3567 {
   3568 }
   3569 
   3570 /*
   3571  * Routine:	pmap_procwr
   3572  *
   3573  * Function:
   3574  *	Synchronize caches corresponding to [addr, addr+len) in p.
   3575  *
   3576  */
   3577 void
   3578 pmap_procwr(p, va, len)
   3579 	struct proc	*p;
   3580 	vaddr_t		va;
   3581 	int		len;
   3582 {
   3583 	/* We only need to do anything if it is the current process. */
   3584 	if (p == curproc)
   3585 		cpu_cache_syncI_rng(va, len);
   3586 }
   3587 /*
   3588  * PTP functions
   3589  */
   3590 
   3591 /*
   3592  * pmap_steal_ptp: Steal a PTP from somewhere else.
   3593  *
   3594  * This is just a placeholder, for now we never steal.
   3595  */
   3596 
   3597 static struct vm_page *
   3598 pmap_steal_ptp(struct pmap *pmap, vaddr_t va)
   3599 {
   3600     return (NULL);
   3601 }
   3602 
   3603 /*
   3604  * pmap_get_ptp: get a PTP (if there isn't one, allocate a new one)
   3605  *
   3606  * => pmap should NOT be pmap_kernel()
   3607  * => pmap should be locked
   3608  */
   3609 
   3610 static struct vm_page *
   3611 pmap_get_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
   3612 {
   3613     struct vm_page *ptp;
   3614 
   3615     if (pmap_pde_v(pmap_pde(pmap, va))) {
   3616 
   3617 	/* valid... check hint (saves us a PA->PG lookup) */
   3618 #if 0
   3619 	if (pmap->pm_ptphint &&
   3620     		((unsigned)pmap_pde(pmap, va) & PG_FRAME) ==
   3621 		VM_PAGE_TO_PHYS(pmap->pm_ptphint))
   3622 	    return (pmap->pm_ptphint);
   3623 #endif
   3624 	ptp = uvm_pagelookup(&pmap->pm_obj, va);
   3625 #ifdef DIAGNOSTIC
   3626 	if (ptp == NULL)
   3627     	    panic("pmap_get_ptp: unmanaged user PTP");
   3628 #endif
   3629 //	pmap->pm_ptphint = ptp;
   3630 	return(ptp);
   3631     }
   3632 
   3633     /* allocate a new PTP (updates ptphint) */
   3634     return(pmap_alloc_ptp(pmap, va, just_try));
   3635 }
   3636 
   3637 /*
   3638  * pmap_alloc_ptp: allocate a PTP for a PMAP
   3639  *
   3640  * => pmap should already be locked by caller
   3641  * => we use the ptp's wire_count to count the number of active mappings
   3642  *	in the PTP (we start it at one to prevent any chance this PTP
   3643  *	will ever leak onto the active/inactive queues)
   3644  */
   3645 
   3646 /*__inline */ static struct vm_page *
   3647 pmap_alloc_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
   3648 {
   3649 	struct vm_page *ptp;
   3650 
   3651 	ptp = uvm_pagealloc(&pmap->pm_obj, va, NULL,
   3652 		UVM_PGA_USERESERVE|UVM_PGA_ZERO);
   3653 	if (ptp == NULL) {
   3654 	    if (just_try)
   3655 		return (NULL);
   3656 
   3657 	    ptp = pmap_steal_ptp(pmap, va);
   3658 
   3659 	    if (ptp == NULL)
   3660 		return (NULL);
   3661 	    /* Stole a page, zero it.  */
   3662 	    pmap_zero_page(VM_PAGE_TO_PHYS(ptp));
   3663 	}
   3664 
   3665 	/* got one! */
   3666 	ptp->flags &= ~PG_BUSY;	/* never busy */
   3667 	ptp->wire_count = 1;	/* no mappings yet */
   3668 	pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(ptp), TRUE);
   3669 	pmap->pm_stats.resident_count++;	/* count PTP as resident */
   3670 //	pmap->pm_ptphint = ptp;
   3671 	return (ptp);
   3672 }
   3673 
   3674 /* End of pmap.c */
   3675