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