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