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