Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.11
      1 /*	$NetBSD: pmap.c,v 1.11 2001/06/24 23:21:04 chris Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1994-1998 Mark Brinicombe.
     41  * Copyright (c) 1994 Brini.
     42  * All rights reserved.
     43  *
     44  * This code is derived from software written for Brini by Mark Brinicombe
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by Mark Brinicombe.
     57  * 4. The name of the author may not be used to endorse or promote products
     58  *    derived from this software without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     61  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     62  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     63  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     64  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     65  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     66  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     67  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     68  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     69  *
     70  * RiscBSD kernel project
     71  *
     72  * pmap.c
     73  *
     74  * Machine dependant vm stuff
     75  *
     76  * Created      : 20/09/94
     77  */
     78 
     79 /*
     80  * Performance improvements, UVM changes, overhauls and part-rewrites
     81  * were contributed by Neil A. Carson <neil (at) causality.com>.
     82  */
     83 
     84 /*
     85  * The dram block info is currently referenced from the bootconfig.
     86  * This should be placed in a separate structure.
     87  */
     88 
     89 /*
     90  * Special compilation symbols
     91  * PMAP_DEBUG		- Build in pmap_debug_level code
     92  */
     93 
     94 /* Include header files */
     95 
     96 #include "opt_pmap_debug.h"
     97 #include "opt_ddb.h"
     98 
     99 #include <sys/types.h>
    100 #include <sys/param.h>
    101 #include <sys/kernel.h>
    102 #include <sys/systm.h>
    103 #include <sys/proc.h>
    104 #include <sys/malloc.h>
    105 #include <sys/user.h>
    106 #include <sys/pool.h>
    107 
    108 #include <uvm/uvm.h>
    109 
    110 #include <machine/bootconfig.h>
    111 #include <machine/bus.h>
    112 #include <machine/pmap.h>
    113 #include <machine/pcb.h>
    114 #include <machine/param.h>
    115 #include <machine/katelib.h>
    116 
    117 #ifdef PMAP_DEBUG
    118 #define	PDEBUG(_lev_,_stat_) \
    119 	if (pmap_debug_level >= (_lev_)) \
    120         	((_stat_))
    121 int pmap_debug_level = -2;
    122 #else	/* PMAP_DEBUG */
    123 #define	PDEBUG(_lev_,_stat_) /* Nothing */
    124 #endif	/* PMAP_DEBUG */
    125 
    126 struct pmap     kernel_pmap_store;
    127 pmap_t          kernel_pmap;
    128 
    129 /*
    130  * pool that pmap structures are allocated from
    131  */
    132 
    133 struct pool pmap_pmap_pool;
    134 
    135 pagehook_t page_hook0;
    136 pagehook_t page_hook1;
    137 char *memhook;
    138 pt_entry_t msgbufpte;
    139 extern caddr_t msgbufaddr;
    140 
    141 #ifdef DIAGNOSTIC
    142 boolean_t pmap_initialized = FALSE;	/* Has pmap_init completed? */
    143 #endif
    144 
    145 TAILQ_HEAD(pv_page_list, pv_page) pv_page_freelist;
    146 
    147 int pv_nfree = 0;
    148 
    149 vsize_t npages;
    150 
    151 extern paddr_t physical_start;
    152 extern paddr_t physical_freestart;
    153 extern paddr_t physical_end;
    154 extern paddr_t physical_freeend;
    155 extern unsigned int free_pages;
    156 extern int max_processes;
    157 
    158 vaddr_t virtual_start;
    159 vaddr_t virtual_end;
    160 
    161 vaddr_t avail_start;
    162 vaddr_t avail_end;
    163 
    164 extern pv_addr_t systempage;
    165 
    166 #define ALLOC_PAGE_HOOK(x, s) \
    167 	x.va = virtual_start; \
    168 	x.pte = (pt_entry_t *)pmap_pte(kernel_pmap, virtual_start); \
    169 	virtual_start += s;
    170 
    171 /* Variables used by the L1 page table queue code */
    172 SIMPLEQ_HEAD(l1pt_queue, l1pt);
    173 struct l1pt_queue l1pt_static_queue;	/* head of our static l1 queue */
    174 int l1pt_static_queue_count;		/* items in the static l1 queue */
    175 int l1pt_static_create_count;		/* static l1 items created */
    176 struct l1pt_queue l1pt_queue;		/* head of our l1 queue */
    177 int l1pt_queue_count;			/* items in the l1 queue */
    178 int l1pt_create_count;			/* stat - L1's create count */
    179 int l1pt_reuse_count;			/* stat - L1's reused count */
    180 
    181 /* Local function prototypes (not used outside this file) */
    182 pt_entry_t *pmap_pte __P((pmap_t pmap, vaddr_t va));
    183 void map_pagetable __P((vaddr_t pagetable, vaddr_t va,
    184     paddr_t pa, unsigned int flags));
    185 void pmap_copy_on_write __P((paddr_t pa));
    186 void pmap_pinit __P((pmap_t));
    187 void pmap_freepagedir __P((pmap_t));
    188 void pmap_release __P((pmap_t));
    189 
    190 /* Other function prototypes */
    191 extern void bzero_page __P((vaddr_t));
    192 extern void bcopy_page __P((vaddr_t, vaddr_t));
    193 
    194 struct l1pt *pmap_alloc_l1pt __P((void));
    195 static __inline void pmap_map_in_l1 __P((pmap_t pmap, vaddr_t va,
    196      vaddr_t l2pa));
    197 
    198 static pt_entry_t *pmap_map_ptes __P((struct pmap *));
    199 /* eventually this will be a function */
    200 #define pmap_unmap_ptes(a)
    201 
    202 void pmap_vac_me_harder __P((struct pmap *, struct pv_entry *,
    203 	    pt_entry_t *));
    204 
    205 #ifdef MYCROFT_HACK
    206 int mycroft_hack = 0;
    207 #endif
    208 
    209 /* Function to set the debug level of the pmap code */
    210 
    211 #ifdef PMAP_DEBUG
    212 void
    213 pmap_debug(level)
    214 	int level;
    215 {
    216 	pmap_debug_level = level;
    217 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    218 }
    219 #endif	/* PMAP_DEBUG */
    220 
    221 #include "isadma.h"
    222 
    223 #if NISADMA > 0
    224 /*
    225  * Used to protect memory for ISA DMA bounce buffers.  If, when loading
    226  * pages into the system, memory intersects with any of these ranges,
    227  * the intersecting memory will be loaded into a lower-priority free list.
    228  */
    229 bus_dma_segment_t *pmap_isa_dma_ranges;
    230 int pmap_isa_dma_nranges;
    231 
    232 boolean_t pmap_isa_dma_range_intersect __P((paddr_t, psize_t,
    233 	    paddr_t *, psize_t *));
    234 
    235 /*
    236  * Check if a memory range intersects with an ISA DMA range, and
    237  * return the page-rounded intersection if it does.  The intersection
    238  * will be placed on a lower-priority free list.
    239  */
    240 boolean_t
    241 pmap_isa_dma_range_intersect(pa, size, pap, sizep)
    242 	paddr_t pa;
    243 	psize_t size;
    244 	paddr_t *pap;
    245 	psize_t *sizep;
    246 {
    247 	bus_dma_segment_t *ds;
    248 	int i;
    249 
    250 	if (pmap_isa_dma_ranges == NULL)
    251 		return (FALSE);
    252 
    253 	for (i = 0, ds = pmap_isa_dma_ranges;
    254 	     i < pmap_isa_dma_nranges; i++, ds++) {
    255 		if (ds->ds_addr <= pa && pa < (ds->ds_addr + ds->ds_len)) {
    256 			/*
    257 			 * Beginning of region intersects with this range.
    258 			 */
    259 			*pap = trunc_page(pa);
    260 			*sizep = round_page(min(pa + size,
    261 			    ds->ds_addr + ds->ds_len) - pa);
    262 			return (TRUE);
    263 		}
    264 		if (pa < ds->ds_addr && ds->ds_addr < (pa + size)) {
    265 			/*
    266 			 * End of region intersects with this range.
    267 			 */
    268 			*pap = trunc_page(ds->ds_addr);
    269 			*sizep = round_page(min((pa + size) - ds->ds_addr,
    270 			    ds->ds_len));
    271 			return (TRUE);
    272 		}
    273 	}
    274 
    275 	/*
    276 	 * No intersection found.
    277 	 */
    278 	return (FALSE);
    279 }
    280 #endif /* NISADMA > 0 */
    281 
    282 /*
    283  * Functions for manipluation pv_entry structures. These are used to keep a
    284  * record of the mappings of virtual addresses and the associated physical
    285  * pages.
    286  */
    287 
    288 /*
    289  * Allocate a new pv_entry structure from the freelist. If the list is
    290  * empty allocate a new page and fill the freelist.
    291  */
    292 struct pv_entry *
    293 pmap_alloc_pv()
    294 {
    295 	struct pv_page *pvp;
    296 	struct pv_entry *pv;
    297 	int i;
    298 
    299 	/*
    300 	 * Do we have any free pv_entry structures left ?
    301 	 * If not allocate a page of them
    302 	 */
    303 
    304 	if (pv_nfree == 0) {
    305 		/* NOTE: can't lock kernel_map here */
    306 		MALLOC(pvp, struct pv_page *, NBPG, M_VMPVENT, M_WAITOK);
    307 		if (pvp == 0)
    308 			panic("pmap_alloc_pv: kmem_alloc() failed");
    309 		pvp->pvp_pgi.pgi_freelist = pv = &pvp->pvp_pv[1];
    310 		for (i = NPVPPG - 2; i; i--, pv++)
    311 			pv->pv_next = pv + 1;
    312 		pv->pv_next = 0;
    313 		pv_nfree += pvp->pvp_pgi.pgi_nfree = NPVPPG - 1;
    314 		TAILQ_INSERT_HEAD(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
    315 		pv = &pvp->pvp_pv[0];
    316 	} else {
    317 		--pv_nfree;
    318 		pvp = pv_page_freelist.tqh_first;
    319 		if (--pvp->pvp_pgi.pgi_nfree == 0) {
    320 			TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
    321 		}
    322 		pv = pvp->pvp_pgi.pgi_freelist;
    323 #ifdef DIAGNOSTIC
    324 		if (pv == 0)
    325 			panic("pmap_alloc_pv: pgi_nfree inconsistent");
    326 #endif	/* DIAGNOSTIC */
    327 		pvp->pvp_pgi.pgi_freelist = pv->pv_next;
    328 	}
    329 	return pv;
    330 }
    331 
    332 /*
    333  * Release a pv_entry structure putting it back on the freelist.
    334  */
    335 
    336 void
    337 pmap_free_pv(pv)
    338 	struct pv_entry *pv;
    339 {
    340 	struct pv_page *pvp;
    341 
    342 	pvp = (struct pv_page *) trunc_page((vaddr_t)pv);
    343 	switch (++pvp->pvp_pgi.pgi_nfree) {
    344 	case 1:
    345 		TAILQ_INSERT_TAIL(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
    346 	default:
    347 		pv->pv_next = pvp->pvp_pgi.pgi_freelist;
    348 		pvp->pvp_pgi.pgi_freelist = pv;
    349 		++pv_nfree;
    350 		break;
    351 	case NPVPPG:
    352 		pv_nfree -= NPVPPG - 1;
    353 		TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
    354 		FREE((vaddr_t)pvp, M_VMPVENT);
    355 		break;
    356 	}
    357 }
    358 
    359 #if 0
    360 void
    361 pmap_collect_pv()
    362 {
    363 	struct pv_page_list pv_page_collectlist;
    364 	struct pv_page *pvp, *npvp;
    365 	struct pv_entry *ph, *ppv, *pv, *npv;
    366 	int s;
    367 
    368 	TAILQ_INIT(&pv_page_collectlist);
    369 
    370 	for (pvp = pv_page_freelist.tqh_first; pvp; pvp = npvp) {
    371 		if (pv_nfree < NPVPPG)
    372 			break;
    373 		npvp = pvp->pvp_pgi.pgi_list.tqe_next;
    374 		if (pvp->pvp_pgi.pgi_nfree > NPVPPG / 3) {
    375 			TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
    376 			TAILQ_INSERT_TAIL(&pv_page_collectlist, pvp,
    377 			    pvp_pgi.pgi_list);
    378 			pv_nfree -= NPVPPG;
    379 			pvp->pvp_pgi.pgi_nfree = -1;
    380 		}
    381 	}
    382 
    383 	if (pv_page_collectlist.tqh_first == 0)
    384 		return;
    385 
    386 	for (ph = &pv_table[npages - 1]; ph >= &pv_table[0]; ph--) {
    387 		if (ph->pv_pmap == 0)
    388 			continue;
    389 		s = splvm();
    390 		for (ppv = ph; (pv = ppv->pv_next) != 0; ) {
    391 			pvp = (struct pv_page *) trunc_page((vaddr_t)pv);
    392 			if (pvp->pvp_pgi.pgi_nfree == -1) {
    393 				pvp = pv_page_freelist.tqh_first;
    394 				if (--pvp->pvp_pgi.pgi_nfree == 0) {
    395 					TAILQ_REMOVE(&pv_page_freelist,
    396 					    pvp, pvp_pgi.pgi_list);
    397 				}
    398 				npv = pvp->pvp_pgi.pgi_freelist;
    399 #ifdef DIAGNOSTIC
    400 				if (npv == 0)
    401 					panic("pmap_collect_pv: pgi_nfree inconsistent");
    402 #endif	/* DIAGNOSTIC */
    403 				pvp->pvp_pgi.pgi_freelist = npv->pv_next;
    404 				*npv = *pv;
    405 				ppv->pv_next = npv;
    406 				ppv = npv;
    407 			} else
    408 				ppv = pv;
    409 		}
    410 		splx(s);
    411 	}
    412 
    413 	for (pvp = pv_page_collectlist.tqh_first; pvp; pvp = npvp) {
    414 		npvp = pvp->pvp_pgi.pgi_list.tqe_next;
    415 		FREE((vaddr_t)pvp, M_VMPVENT);
    416 	}
    417 }
    418 #endif
    419 
    420 /*
    421  * Enter a new physical-virtual mapping into the pv table
    422  */
    423 
    424 /*__inline*/ void
    425 pmap_enter_pv(pmap, va, pv, flags)
    426 	pmap_t pmap;
    427 	vaddr_t va;
    428 	struct pv_entry *pv;
    429 	u_int flags;
    430 {
    431 	struct pv_entry *npv;
    432 	u_int s;
    433 
    434 #ifdef DIAGNOSTIC
    435 	if (!pmap_initialized)
    436 		panic("pmap_enter_pv: !pmap_initialized");
    437 #endif
    438 
    439 	s = splvm();
    440 
    441 	PDEBUG(5, printf("pmap_enter_pv: pv %p: %08lx/%p/%p\n",
    442 	    pv, pv->pv_va, pv->pv_pmap, pv->pv_next));
    443 
    444 	if (pv->pv_pmap == NULL) {
    445 		/*
    446 		 * No entries yet, use header as the first entry
    447 		 */
    448 		pv->pv_va = va;
    449 		pv->pv_pmap = pmap;
    450 		pv->pv_next = NULL;
    451 		pv->pv_flags = flags;
    452 	} else {
    453 		/*
    454 		 * There is at least one other VA mapping this page.
    455 		 * Place this entry after the header.
    456 		 */
    457 #ifdef PMAP_DEBUG
    458 		for (npv = pv; npv; npv = npv->pv_next)
    459 			if (pmap == npv->pv_pmap && va == npv->pv_va)
    460 				panic("pmap_enter_pv: already in pv_tab pv %p: %08lx/%p/%p",
    461 				    pv, pv->pv_va, pv->pv_pmap, pv->pv_next);
    462 #endif
    463 		npv = pmap_alloc_pv();
    464 		npv->pv_va = va;
    465 		npv->pv_pmap = pmap;
    466 		npv->pv_flags = flags;
    467 		npv->pv_next = pv->pv_next;
    468 		pv->pv_next = npv;
    469 	}
    470 
    471 	if (flags & PT_W)
    472 		++pmap->pm_stats.wired_count;
    473 
    474 	splx(s);
    475 }
    476 
    477 
    478 /*
    479  * Remove a physical-virtual mapping from the pv table
    480  */
    481 
    482 /*__inline*/ void
    483 pmap_remove_pv(pmap, va, pv)
    484 	pmap_t pmap;
    485 	vaddr_t va;
    486 	struct pv_entry *pv;
    487 {
    488 	struct pv_entry *npv;
    489 	u_int s;
    490 	u_int flags = 0;
    491 
    492 #ifdef DIAGNOSTIC
    493 	if (!pmap_initialized)
    494 		panic("pmap_remove_pv: !pmap_initialized");
    495 #endif
    496 
    497 	s = splvm();
    498 
    499 	/*
    500 	 * If it is the first entry on the list, it is actually
    501 	 * in the header and we must copy the following entry up
    502 	 * to the header.  Otherwise we must search the list for
    503 	 * the entry.  In either case we free the now unused entry.
    504 	 */
    505 
    506 	if (pmap == pv->pv_pmap && va == pv->pv_va) {
    507 		npv = pv->pv_next;
    508 		if (npv) {
    509 			*pv = *npv;
    510 			flags = npv->pv_flags;
    511 			pmap_free_pv(npv);
    512 		} else {
    513 			flags = pv->pv_flags;
    514 			pv->pv_pmap = NULL;
    515 		}
    516 	} else {
    517 		for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
    518 			if (pmap == npv->pv_pmap && va == npv->pv_va)
    519 				break;
    520 		}
    521 		if (npv) {
    522 			pv->pv_next = npv->pv_next;
    523 			flags = npv->pv_flags;
    524 			pmap_free_pv(npv);
    525 		} else
    526 			panic("pmap_remove_pv: lost entry");
    527 	}
    528 
    529 	if (flags & PT_W)
    530 		--pmap->pm_stats.wired_count;
    531 
    532 	splx(s);
    533 }
    534 
    535 /*
    536  * Modify a physical-virtual mapping in the pv table
    537  */
    538 
    539 /*__inline */ u_int
    540 pmap_modify_pv(pmap, va, pv, bic_mask, eor_mask)
    541 	pmap_t pmap;
    542 	vaddr_t va;
    543 	struct pv_entry *pv;
    544 	u_int bic_mask;
    545 	u_int eor_mask;
    546 {
    547 	struct pv_entry *npv;
    548 	u_int s;
    549 	u_int flags, oflags;
    550 
    551 	PDEBUG(5, printf("pmap_modify_pv(pmap=%p, va=%08lx, pv=%p, bic_mask=%08x, eor_mask=%08x)\n",
    552 	    pmap, va, pv, bic_mask, eor_mask));
    553 
    554 #ifdef DIAGNOSTIC
    555 	if (!pmap_initialized)
    556 		panic("pmap_modify_pv: !pmap_initialized");
    557 #endif
    558 
    559 	s = splvm();
    560 
    561 	PDEBUG(5, printf("pmap_modify_pv: pv %p: %08lx/%p/%p/%08x ",
    562 	    pv, pv->pv_va, pv->pv_pmap, pv->pv_next, pv->pv_flags));
    563 
    564 	/*
    565 	 * There is at least one VA mapping this page.
    566 	 */
    567 
    568 	for (npv = pv; npv; npv = npv->pv_next) {
    569 		if (pmap == npv->pv_pmap && va == npv->pv_va) {
    570 			oflags = npv->pv_flags;
    571 			npv->pv_flags = flags =
    572 			    ((oflags & ~bic_mask) ^ eor_mask);
    573 			if ((flags ^ oflags) & PT_W) {
    574 				if (flags & PT_W)
    575 					++pmap->pm_stats.wired_count;
    576 				else
    577 					--pmap->pm_stats.wired_count;
    578 			}
    579 			PDEBUG(0, printf("done flags=%08x\n", flags));
    580 			splx(s);
    581 			return (oflags);
    582 		}
    583 	}
    584 
    585 	PDEBUG(0, printf("done.\n"));
    586 	splx(s);
    587 	return (0);
    588 }
    589 
    590 
    591 /*
    592  * Map the specified level 2 pagetable into the level 1 page table for
    593  * the given pmap to cover a chunk of virtual address space starting from the
    594  * address specified.
    595  */
    596 static /*__inline*/ void
    597 pmap_map_in_l1(pmap, va, l2pa)
    598 	pmap_t pmap;
    599 	vaddr_t va, l2pa;
    600 {
    601 	vaddr_t ptva;
    602 
    603 	/* Calculate the index into the L1 page table. */
    604 	ptva = (va >> PDSHIFT) & ~3;
    605 
    606 	PDEBUG(0, printf("wiring %08lx in to pd%p pte0x%lx va0x%lx\n", l2pa,
    607 	    pmap->pm_pdir, L1_PTE(l2pa), ptva));
    608 
    609 	/* Map page table into the L1. */
    610 	pmap->pm_pdir[ptva + 0] = L1_PTE(l2pa + 0x000);
    611 	pmap->pm_pdir[ptva + 1] = L1_PTE(l2pa + 0x400);
    612 	pmap->pm_pdir[ptva + 2] = L1_PTE(l2pa + 0x800);
    613 	pmap->pm_pdir[ptva + 3] = L1_PTE(l2pa + 0xc00);
    614 
    615 	PDEBUG(0, printf("pt self reference %lx in %lx\n",
    616 	    L2_PTE_NC_NB(l2pa, AP_KRW), pmap->pm_vptpt));
    617 
    618 	/* Map the page table into the page table area. */
    619 	*((pt_entry_t *)(pmap->pm_vptpt + ptva)) = L2_PTE_NC_NB(l2pa, AP_KRW);
    620 
    621 	/* XXX should be a purge */
    622 /*	cpu_tlb_flushD();*/
    623 }
    624 
    625 #if 0
    626 static /*__inline*/ void
    627 pmap_unmap_in_l1(pmap, va)
    628 	pmap_t pmap;
    629 	vaddr_t va;
    630 {
    631 	vaddr_t ptva;
    632 
    633 	/* Calculate the index into the L1 page table. */
    634 	ptva = (va >> PDSHIFT) & ~3;
    635 
    636 	/* Unmap page table from the L1. */
    637 	pmap->pm_pdir[ptva + 0] = 0;
    638 	pmap->pm_pdir[ptva + 1] = 0;
    639 	pmap->pm_pdir[ptva + 2] = 0;
    640 	pmap->pm_pdir[ptva + 3] = 0;
    641 
    642 	/* Unmap the page table from the page table area. */
    643 	*((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
    644 
    645 	/* XXX should be a purge */
    646 /*	cpu_tlb_flushD();*/
    647 }
    648 #endif
    649 
    650 
    651 /*
    652  *	Used to map a range of physical addresses into kernel
    653  *	virtual address space.
    654  *
    655  *	For now, VM is already on, we only need to map the
    656  *	specified memory.
    657  */
    658 vaddr_t
    659 pmap_map(va, spa, epa, prot)
    660 	vaddr_t va, spa, epa;
    661 	int prot;
    662 {
    663 	while (spa < epa) {
    664 		pmap_enter(pmap_kernel(), va, spa, prot, 0);
    665 		va += NBPG;
    666 		spa += NBPG;
    667 	}
    668 	pmap_update();
    669 	return(va);
    670 }
    671 
    672 
    673 /*
    674  * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
    675  *
    676  * bootstrap the pmap system. This is called from initarm and allows
    677  * the pmap system to initailise any structures it requires.
    678  *
    679  * Currently this sets up the kernel_pmap that is statically allocated
    680  * and also allocated virtual addresses for certain page hooks.
    681  * Currently the only one page hook is allocated that is used
    682  * to zero physical pages of memory.
    683  * It also initialises the start and end address of the kernel data space.
    684  */
    685 extern paddr_t physical_freestart;
    686 extern paddr_t physical_freeend;
    687 
    688 struct pv_entry *boot_pvent;
    689 char *boot_attrs;
    690 
    691 void
    692 pmap_bootstrap(kernel_l1pt, kernel_ptpt)
    693 	pd_entry_t *kernel_l1pt;
    694 	pv_addr_t kernel_ptpt;
    695 {
    696 	int loop;
    697 	paddr_t start, end;
    698 #if NISADMA > 0
    699 	paddr_t istart;
    700 	psize_t isize;
    701 #endif
    702 	vsize_t size;
    703 
    704 	kernel_pmap = &kernel_pmap_store;
    705 
    706 	kernel_pmap->pm_pdir = kernel_l1pt;
    707 	kernel_pmap->pm_pptpt = kernel_ptpt.pv_pa;
    708 	kernel_pmap->pm_vptpt = kernel_ptpt.pv_va;
    709 	simple_lock_init(&kernel_pmap->pm_lock);
    710 	kernel_pmap->pm_count = 1;
    711 
    712 	/*
    713 	 * Initialize PAGE_SIZE-dependent variables.
    714 	 */
    715 	uvm_setpagesize();
    716 
    717 	npages = 0;
    718 	loop = 0;
    719 	while (loop < bootconfig.dramblocks) {
    720 		start = (paddr_t)bootconfig.dram[loop].address;
    721 		end = start + (bootconfig.dram[loop].pages * NBPG);
    722 		if (start < physical_freestart)
    723 			start = physical_freestart;
    724 		if (end > physical_freeend)
    725 			end = physical_freeend;
    726 #if 0
    727 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
    728 #endif
    729 #if NISADMA > 0
    730 		if (pmap_isa_dma_range_intersect(start, end - start,
    731 		    &istart, &isize)) {
    732 			/*
    733 			 * Place the pages that intersect with the
    734 			 * ISA DMA range onto the ISA DMA free list.
    735 			 */
    736 #if 0
    737 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
    738 			    istart + isize - 1);
    739 #endif
    740 			uvm_page_physload(atop(istart),
    741 			    atop(istart + isize), atop(istart),
    742 			    atop(istart + isize), VM_FREELIST_ISADMA);
    743 			npages += atop(istart + isize) - atop(istart);
    744 
    745 			/*
    746 			 * Load the pieces that come before
    747 			 * the intersection into the default
    748 			 * free list.
    749 			 */
    750 			if (start < istart) {
    751 #if 0
    752 				printf("    BEFORE 0x%lx -> 0x%lx\n",
    753 				    start, istart - 1);
    754 #endif
    755 				uvm_page_physload(atop(start),
    756 				    atop(istart), atop(start),
    757 				    atop(istart), VM_FREELIST_DEFAULT);
    758 				npages += atop(istart) - atop(start);
    759 			}
    760 
    761 			/*
    762 			 * Load the pieces that come after
    763 			 * the intersection into the default
    764 			 * free list.
    765 			 */
    766 			if ((istart + isize) < end) {
    767 #if 0
    768 				printf("     AFTER 0x%lx -> 0x%lx\n",
    769 				    (istart + isize), end - 1);
    770 #endif
    771 				uvm_page_physload(atop(istart + isize),
    772 				    atop(end), atop(istart + isize),
    773 				    atop(end), VM_FREELIST_DEFAULT);
    774 				npages += atop(end) - atop(istart + isize);
    775 			}
    776 		} else {
    777 			uvm_page_physload(atop(start), atop(end),
    778 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
    779 			npages += atop(end) - atop(start);
    780 		}
    781 #else	/* NISADMA > 0 */
    782 		uvm_page_physload(atop(start), atop(end),
    783 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
    784 		npages += atop(end) - atop(start);
    785 #endif /* NISADMA > 0 */
    786 		++loop;
    787 	}
    788 
    789 #ifdef MYCROFT_HACK
    790 	printf("npages = %ld\n", npages);
    791 #endif
    792 
    793 	virtual_start = KERNEL_VM_BASE;
    794 	virtual_end = virtual_start + KERNEL_VM_SIZE - 1;
    795 
    796 	ALLOC_PAGE_HOOK(page_hook0, NBPG);
    797 	ALLOC_PAGE_HOOK(page_hook1, NBPG);
    798 
    799 	/*
    800 	 * The mem special device needs a virtual hook but we don't
    801 	 * need a pte
    802 	 */
    803 	memhook = (char *)virtual_start;
    804 	virtual_start += NBPG;
    805 
    806 	msgbufaddr = (caddr_t)virtual_start;
    807 	msgbufpte = (pt_entry_t)pmap_pte(kernel_pmap, virtual_start);
    808 	virtual_start += round_page(MSGBUFSIZE);
    809 
    810 	size = npages * sizeof(struct pv_entry);
    811 	boot_pvent = (struct pv_entry *)uvm_pageboot_alloc(size);
    812 	bzero(boot_pvent, size);
    813 	size = npages * sizeof(char);
    814 	boot_attrs = (char *)uvm_pageboot_alloc(size);
    815 	bzero(boot_attrs, size);
    816 
    817 	/*
    818 	 * initialize the pmap pool.
    819 	 */
    820 
    821 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
    822 		  0, pool_page_alloc_nointr, pool_page_free_nointr, M_VMPMAP);
    823 
    824 	cpu_cache_cleanD();
    825 }
    826 
    827 /*
    828  * void pmap_init(void)
    829  *
    830  * Initialize the pmap module.
    831  * Called by vm_init() in vm/vm_init.c in order to initialise
    832  * any structures that the pmap system needs to map virtual memory.
    833  */
    834 
    835 extern int physmem;
    836 
    837 void
    838 pmap_init()
    839 {
    840 	int lcv;
    841 
    842 #ifdef MYCROFT_HACK
    843 	printf("physmem = %d\n", physmem);
    844 #endif
    845 
    846 	/*
    847 	 * Set the available memory vars - These do not map to real memory
    848 	 * addresses and cannot as the physical memory is fragmented.
    849 	 * They are used by ps for %mem calculations.
    850 	 * One could argue whether this should be the entire memory or just
    851 	 * the memory that is useable in a user process.
    852 	 */
    853 	avail_start = 0;
    854 	avail_end = physmem * NBPG;
    855 
    856 	/* Set up pmap info for physsegs. */
    857 	for (lcv = 0; lcv < vm_nphysseg; lcv++) {
    858 		vm_physmem[lcv].pmseg.pvent = boot_pvent;
    859 		boot_pvent += vm_physmem[lcv].end - vm_physmem[lcv].start;
    860 		vm_physmem[lcv].pmseg.attrs = boot_attrs;
    861 		boot_attrs += vm_physmem[lcv].end - vm_physmem[lcv].start;
    862 	}
    863 #ifdef MYCROFT_HACK
    864 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    865 		printf("physseg[%d] pvent=%p attrs=%p start=%ld end=%ld\n",
    866 		    lcv,
    867 		    vm_physmem[lcv].pmseg.pvent, vm_physmem[lcv].pmseg.attrs,
    868 		    vm_physmem[lcv].start, vm_physmem[lcv].end);
    869 	}
    870 #endif
    871 	TAILQ_INIT(&pv_page_freelist);
    872 
    873 #ifdef DIAGNOSTIC
    874 	/* Now it is safe to enable pv_entry recording. */
    875 	pmap_initialized = TRUE;
    876 #endif
    877 
    878 	/* Initialise our L1 page table queues and counters */
    879 	SIMPLEQ_INIT(&l1pt_static_queue);
    880 	l1pt_static_queue_count = 0;
    881 	l1pt_static_create_count = 0;
    882 	SIMPLEQ_INIT(&l1pt_queue);
    883 	l1pt_queue_count = 0;
    884 	l1pt_create_count = 0;
    885 	l1pt_reuse_count = 0;
    886 }
    887 
    888 /*
    889  * pmap_postinit()
    890  *
    891  * This routine is called after the vm and kmem subsystems have been
    892  * initialised. This allows the pmap code to perform any initialisation
    893  * that can only be done one the memory allocation is in place.
    894  */
    895 
    896 void
    897 pmap_postinit()
    898 {
    899 	int loop;
    900 	struct l1pt *pt;
    901 
    902 #ifdef PMAP_STATIC_L1S
    903 	for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
    904 #else	/* PMAP_STATIC_L1S */
    905 	for (loop = 0; loop < max_processes; ++loop) {
    906 #endif	/* PMAP_STATIC_L1S */
    907 		/* Allocate a L1 page table */
    908 		pt = pmap_alloc_l1pt();
    909 		if (!pt)
    910 			panic("Cannot allocate static L1 page tables\n");
    911 
    912 		/* Clean it */
    913 		bzero((void *)pt->pt_va, PD_SIZE);
    914 		pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
    915 		/* Add the page table to the queue */
    916 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
    917 		++l1pt_static_queue_count;
    918 		++l1pt_static_create_count;
    919 	}
    920 }
    921 
    922 
    923 /*
    924  * Create and return a physical map.
    925  *
    926  * If the size specified for the map is zero, the map is an actual physical
    927  * map, and may be referenced by the hardware.
    928  *
    929  * If the size specified is non-zero, the map will be used in software only,
    930  * and is bounded by that size.
    931  */
    932 
    933 pmap_t
    934 pmap_create()
    935 {
    936 	pmap_t pmap;
    937 
    938 	/*
    939 	 * Fetch pmap entry from the pool
    940 	 */
    941 
    942 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
    943 	bzero(pmap, sizeof(*pmap));
    944 
    945 	/* Now init the machine part of the pmap */
    946 	pmap_pinit(pmap);
    947 	return(pmap);
    948 }
    949 
    950 /*
    951  * pmap_alloc_l1pt()
    952  *
    953  * This routine allocates physical and virtual memory for a L1 page table
    954  * and wires it.
    955  * A l1pt structure is returned to describe the allocated page table.
    956  *
    957  * This routine is allowed to fail if the required memory cannot be allocated.
    958  * In this case NULL is returned.
    959  */
    960 
    961 struct l1pt *
    962 pmap_alloc_l1pt(void)
    963 {
    964 	paddr_t pa;
    965 	vaddr_t va;
    966 	struct l1pt *pt;
    967 	int error;
    968 	struct vm_page *m;
    969 	pt_entry_t *ptes;
    970 
    971 	/* Allocate virtual address space for the L1 page table */
    972 	va = uvm_km_valloc(kernel_map, PD_SIZE);
    973 	if (va == 0) {
    974 #ifdef DIAGNOSTIC
    975 		printf("pmap: Cannot allocate pageable memory for L1\n");
    976 #endif	/* DIAGNOSTIC */
    977 		return(NULL);
    978 	}
    979 
    980 	/* Allocate memory for the l1pt structure */
    981 	pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
    982 
    983 	/*
    984 	 * Allocate pages from the VM system.
    985 	 */
    986 	TAILQ_INIT(&pt->pt_plist);
    987 	error = uvm_pglistalloc(PD_SIZE, physical_start, physical_end,
    988 	    PD_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
    989 	if (error) {
    990 #ifdef DIAGNOSTIC
    991 		printf("pmap: Cannot allocate physical memory for L1 (%d)\n",
    992 		    error);
    993 #endif	/* DIAGNOSTIC */
    994 		/* Release the resources we already have claimed */
    995 		free(pt, M_VMPMAP);
    996 		uvm_km_free(kernel_map, va, PD_SIZE);
    997 		return(NULL);
    998 	}
    999 
   1000 	/* Map our physical pages into our virtual space */
   1001 	pt->pt_va = va;
   1002 	m = pt->pt_plist.tqh_first;
   1003 	ptes = pmap_map_ptes(pmap_kernel());
   1004 	while (m && va < (pt->pt_va + PD_SIZE)) {
   1005 		pa = VM_PAGE_TO_PHYS(m);
   1006 
   1007 		pmap_enter(pmap_kernel(), va, pa,
   1008 		    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
   1009 
   1010 		/* Revoke cacheability and bufferability */
   1011 		/* XXX should be done better than this */
   1012 		ptes[arm_byte_to_page(va)] &= ~(PT_C | PT_B);
   1013 
   1014 		va += NBPG;
   1015 		m = m->pageq.tqe_next;
   1016 	}
   1017 	pmap_unmap_ptes(pmap_kernel());
   1018 	pmap_update();
   1019 
   1020 #ifdef DIAGNOSTIC
   1021 	if (m)
   1022 		panic("pmap_alloc_l1pt: pglist not empty\n");
   1023 #endif	/* DIAGNOSTIC */
   1024 
   1025 	pt->pt_flags = 0;
   1026 	return(pt);
   1027 }
   1028 
   1029 /*
   1030  * Free a L1 page table previously allocated with pmap_alloc_l1pt().
   1031  */
   1032 void
   1033 pmap_free_l1pt(pt)
   1034 	struct l1pt *pt;
   1035 {
   1036 	/* Separate the physical memory for the virtual space */
   1037 	pmap_remove(kernel_pmap, pt->pt_va, pt->pt_va + PD_SIZE);
   1038 	pmap_update();
   1039 
   1040 	/* Return the physical memory */
   1041 	uvm_pglistfree(&pt->pt_plist);
   1042 
   1043 	/* Free the virtual space */
   1044 	uvm_km_free(kernel_map, pt->pt_va, PD_SIZE);
   1045 
   1046 	/* Free the l1pt structure */
   1047 	free(pt, M_VMPMAP);
   1048 }
   1049 
   1050 /*
   1051  * Allocate a page directory.
   1052  * This routine will either allocate a new page directory from the pool
   1053  * of L1 page tables currently held by the kernel or it will allocate
   1054  * a new one via pmap_alloc_l1pt().
   1055  * It will then initialise the l1 page table for use.
   1056  */
   1057 int
   1058 pmap_allocpagedir(pmap)
   1059 	struct pmap *pmap;
   1060 {
   1061 	paddr_t pa;
   1062 	struct l1pt *pt;
   1063 	pt_entry_t *pte;
   1064 
   1065 	PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
   1066 
   1067 	/* Do we have any spare L1's lying around ? */
   1068 	if (l1pt_static_queue_count) {
   1069 		--l1pt_static_queue_count;
   1070 		pt = l1pt_static_queue.sqh_first;
   1071 		SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
   1072 	} else if (l1pt_queue_count) {
   1073 		--l1pt_queue_count;
   1074 		pt = l1pt_queue.sqh_first;
   1075 		SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
   1076 		++l1pt_reuse_count;
   1077 	} else {
   1078 		pt = pmap_alloc_l1pt();
   1079 		if (!pt)
   1080 			return(ENOMEM);
   1081 		++l1pt_create_count;
   1082 	}
   1083 
   1084 	/* Store the pointer to the l1 descriptor in the pmap. */
   1085 	pmap->pm_l1pt = pt;
   1086 
   1087 	/* Get the physical address of the start of the l1 */
   1088 	pa = VM_PAGE_TO_PHYS(pt->pt_plist.tqh_first);
   1089 
   1090 	/* Store the virtual address of the l1 in the pmap. */
   1091 	pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
   1092 
   1093 	/* Clean the L1 if it is dirty */
   1094 	if (!(pt->pt_flags & PTFLAG_CLEAN))
   1095 		bzero((void *)pmap->pm_pdir, (PD_SIZE - KERNEL_PD_SIZE));
   1096 
   1097 	/* Do we already have the kernel mappings ? */
   1098 	if (!(pt->pt_flags & PTFLAG_KPT)) {
   1099 		/* Duplicate the kernel mapping i.e. all mappings 0xf0000000+ */
   1100 
   1101 		bcopy((char *)kernel_pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1102 		    (char *)pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1103 		    KERNEL_PD_SIZE);
   1104 		pt->pt_flags |= PTFLAG_KPT;
   1105 	}
   1106 
   1107 	/* Allocate a page table to map all the page tables for this pmap */
   1108 
   1109 #ifdef DIAGNOSTIC
   1110 	if (pmap->pm_vptpt) {
   1111 		/* XXX What if we have one already ? */
   1112 		panic("pmap_allocpagedir: have pt already\n");
   1113 	}
   1114 #endif	/* DIAGNOSTIC */
   1115 	pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
   1116 	if (pmap->pm_vptpt == 0) {
   1117 		pmap_freepagedir(pmap);
   1118 		return(ENOMEM);
   1119 	}
   1120 
   1121 	(void) pmap_extract(kernel_pmap, pmap->pm_vptpt, &pmap->pm_pptpt);
   1122 	pmap->pm_pptpt &= PG_FRAME;
   1123 	/* Revoke cacheability and bufferability */
   1124 	/* XXX should be done better than this */
   1125 	pte = pmap_pte(kernel_pmap, pmap->pm_vptpt);
   1126 	*pte = *pte & ~(PT_C | PT_B);
   1127 
   1128 	/* Wire in this page table */
   1129 	pmap_map_in_l1(pmap, PROCESS_PAGE_TBLS_BASE, pmap->pm_pptpt);
   1130 
   1131 	pt->pt_flags &= ~PTFLAG_CLEAN;	/* L1 is dirty now */
   1132 
   1133 	/*
   1134 	 * Map the kernel page tables for 0xf0000000 +
   1135 	 * into the page table used to map the
   1136 	 * pmap's page tables
   1137 	 */
   1138 	bcopy((char *)(PROCESS_PAGE_TBLS_BASE
   1139 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2))
   1140 	    + ((PD_SIZE - KERNEL_PD_SIZE) >> 2)),
   1141 	    (char *)pmap->pm_vptpt + ((PD_SIZE - KERNEL_PD_SIZE) >> 2),
   1142 	    (KERNEL_PD_SIZE >> 2));
   1143 
   1144 	pmap->pm_count = 1;
   1145 	simple_lock_init(&pmap->pm_lock);
   1146 
   1147 	return(0);
   1148 }
   1149 
   1150 
   1151 /*
   1152  * Initialize a preallocated and zeroed pmap structure,
   1153  * such as one in a vmspace structure.
   1154  */
   1155 
   1156 static int pmap_pagedir_ident;	/* tsleep() ident */
   1157 
   1158 void
   1159 pmap_pinit(pmap)
   1160 	struct pmap *pmap;
   1161 {
   1162 	PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
   1163 
   1164 	/* Keep looping until we succeed in allocating a page directory */
   1165 	while (pmap_allocpagedir(pmap) != 0) {
   1166 		/*
   1167 		 * Ok we failed to allocate a suitable block of memory for an
   1168 		 * L1 page table. This means that either:
   1169 		 * 1. 16KB of virtual address space could not be allocated
   1170 		 * 2. 16KB of physically contiguous memory on a 16KB boundary
   1171 		 *    could not be allocated.
   1172 		 *
   1173 		 * Since we cannot fail we will sleep for a while and try
   1174 		 * again. Although we will be wakened when another page table
   1175 		 * is freed other memory releasing and swapping may occur
   1176 		 * that will mean we can succeed so we will keep trying
   1177 		 * regularly just in case.
   1178 		 */
   1179 
   1180 		if (tsleep((caddr_t)&pmap_pagedir_ident, PZERO,
   1181 		   "l1ptwait", 1000) == EWOULDBLOCK)
   1182 			printf("pmap: Cannot allocate L1 page table, sleeping ...\n");
   1183 	}
   1184 
   1185 	/* Map zero page for the pmap. This will also map the L2 for it */
   1186 	pmap_enter(pmap, 0x00000000, systempage.pv_pa,
   1187 	    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1188 	pmap_update();
   1189 }
   1190 
   1191 
   1192 void
   1193 pmap_freepagedir(pmap)
   1194 	pmap_t pmap;
   1195 {
   1196 	/* Free the memory used for the page table mapping */
   1197 	if (pmap->pm_vptpt != 0)
   1198 		uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
   1199 
   1200 	/* junk the L1 page table */
   1201 	if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
   1202 		/* Add the page table to the queue */
   1203 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
   1204 		++l1pt_static_queue_count;
   1205 		/* Wake up any sleeping processes waiting for a l1 page table */
   1206 		wakeup((caddr_t)&pmap_pagedir_ident);
   1207 	} else if (l1pt_queue_count < 8) {
   1208 		/* Add the page table to the queue */
   1209 		SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
   1210 		++l1pt_queue_count;
   1211 		/* Wake up any sleeping processes waiting for a l1 page table */
   1212 		wakeup((caddr_t)&pmap_pagedir_ident);
   1213 	} else
   1214 		pmap_free_l1pt(pmap->pm_l1pt);
   1215 }
   1216 
   1217 
   1218 /*
   1219  * Retire the given physical map from service.
   1220  * Should only be called if the map contains no valid mappings.
   1221  */
   1222 
   1223 void
   1224 pmap_destroy(pmap)
   1225 	pmap_t pmap;
   1226 {
   1227 	int count;
   1228 
   1229 	if (pmap == NULL)
   1230 		return;
   1231 
   1232 	PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
   1233 	simple_lock(&pmap->pm_lock);
   1234 	count = --pmap->pm_count;
   1235 	simple_unlock(&pmap->pm_lock);
   1236 	if (count == 0) {
   1237 		pmap_release(pmap);
   1238 		pool_put(&pmap_pmap_pool, pmap);
   1239 	}
   1240 }
   1241 
   1242 
   1243 /*
   1244  * Release any resources held by the given physical map.
   1245  * Called when a pmap initialized by pmap_pinit is being released.
   1246  * Should only be called if the map contains no valid mappings.
   1247  */
   1248 
   1249 void
   1250 pmap_release(pmap)
   1251 	pmap_t pmap;
   1252 {
   1253 	struct vm_page *page;
   1254 	pt_entry_t *pte;
   1255 	int loop;
   1256 
   1257 	PDEBUG(0, printf("pmap_release(%p)\n", pmap));
   1258 
   1259 #if 0
   1260 	if (pmap->pm_count != 1)		/* XXX: needs sorting */
   1261 		panic("pmap_release count %d", pmap->pm_count);
   1262 #endif
   1263 
   1264 	/* Remove the zero page mapping */
   1265 	pmap_remove(pmap, 0x00000000, 0x00000000 + NBPG);
   1266 	pmap_update();
   1267 
   1268 	/*
   1269 	 * Free any page tables still mapped
   1270 	 * This is only temporay until pmap_enter can count the number
   1271 	 * of mappings made in a page table. Then pmap_remove() can
   1272 	 * reduce the count and free the pagetable when the count
   1273 	 * reaches zero.
   1274 	 */
   1275 	for (loop = 0; loop < (((PD_SIZE - KERNEL_PD_SIZE) >> 4) - 1); ++loop) {
   1276 		pte = (pt_entry_t *)(pmap->pm_vptpt + loop * 4);
   1277 		if (*pte != 0) {
   1278 			PDEBUG(0, printf("%x: pte=%p:%08x\n", loop, pte, *pte));
   1279 			page = PHYS_TO_VM_PAGE(pmap_pte_pa(pte));
   1280 			if (page == NULL)
   1281 				panic("pmap_release: bad address for phys page");
   1282 			uvm_pagefree(page);
   1283 		}
   1284 	}
   1285 	/* Free the page dir */
   1286 	pmap_freepagedir(pmap);
   1287 }
   1288 
   1289 
   1290 /*
   1291  * void pmap_reference(pmap_t pmap)
   1292  *
   1293  * Add a reference to the specified pmap.
   1294  */
   1295 
   1296 void
   1297 pmap_reference(pmap)
   1298 	pmap_t pmap;
   1299 {
   1300 	if (pmap == NULL)
   1301 		return;
   1302 
   1303 	simple_lock(&pmap->pm_lock);
   1304 	pmap->pm_count++;
   1305 	simple_unlock(&pmap->pm_lock);
   1306 }
   1307 
   1308 /*
   1309  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   1310  *
   1311  * Return the start and end addresses of the kernel's virtual space.
   1312  * These values are setup in pmap_bootstrap and are updated as pages
   1313  * are allocated.
   1314  */
   1315 
   1316 void
   1317 pmap_virtual_space(start, end)
   1318 	vaddr_t *start;
   1319 	vaddr_t *end;
   1320 {
   1321 	*start = virtual_start;
   1322 	*end = virtual_end;
   1323 }
   1324 
   1325 
   1326 /*
   1327  * Activate the address space for the specified process.  If the process
   1328  * is the current process, load the new MMU context.
   1329  */
   1330 void
   1331 pmap_activate(p)
   1332 	struct proc *p;
   1333 {
   1334 	pmap_t pmap = p->p_vmspace->vm_map.pmap;
   1335 	struct pcb *pcb = &p->p_addr->u_pcb;
   1336 
   1337 	(void) pmap_extract(kernel_pmap, (vaddr_t)pmap->pm_pdir,
   1338 	    (paddr_t *)&pcb->pcb_pagedir);
   1339 
   1340 	PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
   1341 	    p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
   1342 
   1343 	if (p == curproc) {
   1344 		PDEBUG(0, printf("pmap_activate: setting TTB\n"));
   1345 		setttb((u_int)pcb->pcb_pagedir);
   1346 	}
   1347 #if 0
   1348 	pmap->pm_pdchanged = FALSE;
   1349 #endif
   1350 }
   1351 
   1352 
   1353 /*
   1354  * Deactivate the address space of the specified process.
   1355  */
   1356 void
   1357 pmap_deactivate(p)
   1358 	struct proc *p;
   1359 {
   1360 }
   1361 
   1362 
   1363 /*
   1364  * pmap_clean_page()
   1365  *
   1366  * This is a local function used to work out the best strategy to clean
   1367  * a single page referenced by its entry in the PV table. It's used by
   1368  * pmap_copy_page, pmap_zero page and maybe some others later on.
   1369  *
   1370  * Its policy is effectively:
   1371  *  o If there are no mappings, we don't bother doing anything with the cache.
   1372  *  o If there is one mapping, we clean just that page.
   1373  *  o If there are multiple mappings, we clean the entire cache.
   1374  *
   1375  * So that some functions can be further optimised, it returns 0 if it didn't
   1376  * clean the entire cache, or 1 if it did.
   1377  *
   1378  * XXX One bug in this routine is that if the pv_entry has a single page
   1379  * mapped at 0x00000000 a whole cache clean will be performed rather than
   1380  * just the 1 page. Since this should not occur in everyday use and if it does
   1381  * it will just result in not the most efficient clean for the page.
   1382  */
   1383 static int
   1384 pmap_clean_page(pv)
   1385 	struct pv_entry *pv;
   1386 {
   1387 	int s;
   1388 	int cache_needs_cleaning = 0;
   1389 	vaddr_t page_to_clean = 0;
   1390 
   1391 	/* Go to splvm() so we get exclusive lock for a mo */
   1392 	s = splvm();
   1393 	if (pv->pv_pmap) {
   1394 		cache_needs_cleaning = 1;
   1395 		if (!pv->pv_next)
   1396 			page_to_clean = pv->pv_va;
   1397 	}
   1398 	splx(s);
   1399 
   1400 	/* Do cache ops outside the splvm. */
   1401 	if (page_to_clean)
   1402 		cpu_cache_purgeID_rng(page_to_clean, NBPG);
   1403 	else if (cache_needs_cleaning) {
   1404 		cpu_cache_purgeID();
   1405 		return (1);
   1406 	}
   1407 	return (0);
   1408 }
   1409 
   1410 /*
   1411  * pmap_find_pv()
   1412  *
   1413  * This is a local function that finds a PV entry for a given physical page.
   1414  * This is a common op, and this function removes loads of ifdefs in the code.
   1415  */
   1416 static __inline struct pv_entry *
   1417 pmap_find_pv(phys)
   1418 	paddr_t phys;
   1419 {
   1420 	int bank, off;
   1421 	struct pv_entry *pv;
   1422 
   1423 #ifdef DIAGNOSTIC
   1424 	if (!pmap_initialized)
   1425 		panic("pmap_find_pv: !pmap_initialized");
   1426 #endif
   1427 
   1428 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1)
   1429 		panic("pmap_find_pv: not a real page, phys=%lx\n", phys);
   1430 	pv = &vm_physmem[bank].pmseg.pvent[off];
   1431 	return (pv);
   1432 }
   1433 
   1434 /*
   1435  * pmap_zero_page()
   1436  *
   1437  * Zero a given physical page by mapping it at a page hook point.
   1438  * In doing the zero page op, the page we zero is mapped cachable, as with
   1439  * StrongARM accesses to non-cached pages are non-burst making writing
   1440  * _any_ bulk data very slow.
   1441  */
   1442 void
   1443 pmap_zero_page(phys)
   1444 	paddr_t phys;
   1445 {
   1446 	struct pv_entry *pv;
   1447 
   1448 	/* Get an entry for this page, and clean it it. */
   1449 	pv = pmap_find_pv(phys);
   1450 	pmap_clean_page(pv);
   1451 
   1452 	/*
   1453 	 * Hook in the page, zero it, and purge the cache for that
   1454 	 * zeroed page. Invalidate the TLB as needed.
   1455 	 */
   1456 	*page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
   1457 	cpu_tlb_flushD_SE(page_hook0.va);
   1458 	bzero_page(page_hook0.va);
   1459 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1460 }
   1461 
   1462 /*
   1463  * pmap_copy_page()
   1464  *
   1465  * Copy one physical page into another, by mapping the pages into
   1466  * hook points. The same comment regarding cachability as in
   1467  * pmap_zero_page also applies here.
   1468  */
   1469 void
   1470 pmap_copy_page(src, dest)
   1471 	paddr_t src;
   1472 	paddr_t dest;
   1473 {
   1474 	struct pv_entry *src_pv, *dest_pv;
   1475 
   1476 	/* Get PV entries for the pages, and clean them if needed. */
   1477 	src_pv = pmap_find_pv(src);
   1478 	dest_pv = pmap_find_pv(dest);
   1479 	if (!pmap_clean_page(src_pv))
   1480 		pmap_clean_page(dest_pv);
   1481 
   1482 	/*
   1483 	 * Map the pages into the page hook points, copy them, and purge
   1484 	 * the cache for the appropriate page. Invalidate the TLB
   1485 	 * as required.
   1486 	 */
   1487 	*page_hook0.pte = L2_PTE(src & PG_FRAME, AP_KRW);
   1488 	*page_hook1.pte = L2_PTE(dest & PG_FRAME, AP_KRW);
   1489 	cpu_tlb_flushD_SE(page_hook0.va);
   1490 	cpu_tlb_flushD_SE(page_hook1.va);
   1491 	bcopy_page(page_hook0.va, page_hook1.va);
   1492 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1493 	cpu_cache_purgeD_rng(page_hook1.va, NBPG);
   1494 }
   1495 
   1496 /*
   1497  * int pmap_next_phys_page(paddr_t *addr)
   1498  *
   1499  * Allocate another physical page returning true or false depending
   1500  * on whether a page could be allocated.
   1501  */
   1502 
   1503 paddr_t
   1504 pmap_next_phys_page(addr)
   1505 	paddr_t addr;
   1506 
   1507 {
   1508 	int loop;
   1509 
   1510 	if (addr < bootconfig.dram[0].address)
   1511 		return(bootconfig.dram[0].address);
   1512 
   1513 	loop = 0;
   1514 
   1515 	while (bootconfig.dram[loop].address != 0
   1516 	    && addr > (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG))
   1517 		++loop;
   1518 
   1519 	if (bootconfig.dram[loop].address == 0)
   1520 		return(0);
   1521 
   1522 	addr += NBPG;
   1523 
   1524 	if (addr >= (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG)) {
   1525 		if (bootconfig.dram[loop + 1].address == 0)
   1526 			return(0);
   1527 		addr = bootconfig.dram[loop + 1].address;
   1528 	}
   1529 
   1530 	return(addr);
   1531 }
   1532 
   1533 #if 0
   1534 void
   1535 pmap_pte_addref(pmap, va)
   1536 	pmap_t pmap;
   1537 	vaddr_t va;
   1538 {
   1539 	pd_entry_t *pde;
   1540 	paddr_t pa;
   1541 	struct vm_page *m;
   1542 
   1543 	if (pmap == pmap_kernel())
   1544 		return;
   1545 
   1546 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   1547 	pa = pmap_pte_pa(pde);
   1548 	m = PHYS_TO_VM_PAGE(pa);
   1549 	++m->wire_count;
   1550 #ifdef MYCROFT_HACK
   1551 	printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   1552 	    pmap, va, pde, pa, m, m->wire_count);
   1553 #endif
   1554 }
   1555 
   1556 void
   1557 pmap_pte_delref(pmap, va)
   1558 	pmap_t pmap;
   1559 	vaddr_t va;
   1560 {
   1561 	pd_entry_t *pde;
   1562 	paddr_t pa;
   1563 	struct vm_page *m;
   1564 
   1565 	if (pmap == pmap_kernel())
   1566 		return;
   1567 
   1568 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   1569 	pa = pmap_pte_pa(pde);
   1570 	m = PHYS_TO_VM_PAGE(pa);
   1571 	--m->wire_count;
   1572 #ifdef MYCROFT_HACK
   1573 	printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   1574 	    pmap, va, pde, pa, m, m->wire_count);
   1575 #endif
   1576 	if (m->wire_count == 0) {
   1577 #ifdef MYCROFT_HACK
   1578 		printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
   1579 		    pmap, va, pde, pa, m);
   1580 #endif
   1581 		pmap_unmap_in_l1(pmap, va);
   1582 		uvm_pagefree(m);
   1583 		--pmap->pm_stats.resident_count;
   1584 	}
   1585 }
   1586 #else
   1587 #define	pmap_pte_addref(pmap, va)
   1588 #define	pmap_pte_delref(pmap, va)
   1589 #endif
   1590 
   1591 /*
   1592  * Since we have a virtually indexed cache, we may need to inhibit caching if
   1593  * there is more than one mapping and at least one of them is writable.
   1594  * Since we purge the cache on every context switch, we only need to check for
   1595  * other mappings within the same pmap, or kernel_pmap.
   1596  * This function is also called when a page is unmapped, to possibly reenable
   1597  * caching on any remaining mappings.
   1598  *
   1599  * Note that the pmap must have it's ptes mapped in, and passed with ptes.
   1600  */
   1601 void
   1602 pmap_vac_me_harder(pmap, pv, ptes)
   1603 	pmap_t pmap;
   1604 	struct pv_entry *pv;
   1605 	pt_entry_t *ptes;
   1606 {
   1607 	struct pv_entry *npv;
   1608 	pt_entry_t *pte;
   1609 	int entries = 0;
   1610 	int writeable = 0;
   1611 
   1612 	if (pv->pv_pmap == NULL)
   1613 		return;
   1614 	KASSERT(ptes != NULL);
   1615 
   1616 	/*
   1617 	 * Count mappings and writable mappings in this pmap.
   1618 	 * Keep a pointer to the first one.
   1619 	 */
   1620 	for (npv = pv; npv; npv = npv->pv_next) {
   1621 		/* Count mappings in the same pmap */
   1622 		if (pmap == npv->pv_pmap) {
   1623 			if (entries++ == 0)
   1624 				pv = npv;
   1625 			/* Writeable mappings */
   1626 			if (npv->pv_flags & PT_Wr)
   1627 				++writeable;
   1628 		}
   1629 	}
   1630 
   1631 	/*
   1632 	 * Enable or disable caching as necessary.
   1633 	 * We do a quick check of the first PTE to avoid walking the list if
   1634 	 * we're already in the right state.
   1635 	 */
   1636 	if (entries > 1 && writeable) {
   1637 		pte =  &ptes[arm_byte_to_page(pv->pv_va)];
   1638 		if (~*pte & (PT_C | PT_B))
   1639 		{
   1640 			return;
   1641 		}
   1642 		*pte &= ~(PT_C | PT_B);
   1643 		for (npv = pv->pv_next; npv; npv = npv->pv_next) {
   1644 			if (pmap == npv->pv_pmap) {
   1645 			    ptes[arm_byte_to_page(npv->pv_va)] &=
   1646 				    ~(PT_C | PT_B);
   1647 			}
   1648 		}
   1649 	} else if (entries > 0) {
   1650 		pte = &ptes[arm_byte_to_page(pv->pv_va)];
   1651 		if (*pte & (PT_C | PT_B)) {
   1652 			return;
   1653 		}
   1654 		*pte |= (PT_C | PT_B);
   1655 		for (npv = pv->pv_next; npv; npv = npv->pv_next) {
   1656 			if (pmap == npv->pv_pmap) {
   1657 				ptes[arm_byte_to_page(npv->pv_va)] |=
   1658 				    (PT_C | PT_B);
   1659 			}
   1660 		}
   1661 	}
   1662 }
   1663 
   1664 /*
   1665  * pmap_remove()
   1666  *
   1667  * pmap_remove is responsible for nuking a number of mappings for a range
   1668  * of virtual address space in the current pmap. To do this efficiently
   1669  * is interesting, because in a number of cases a wide virtual address
   1670  * range may be supplied that contains few actual mappings. So, the
   1671  * optimisations are:
   1672  *  1. Try and skip over hunks of address space for which an L1 entry
   1673  *     does not exist.
   1674  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   1675  *     maybe do just a partial cache clean. This path of execution is
   1676  *     complicated by the fact that the cache must be flushed _before_
   1677  *     the PTE is nuked, being a VAC :-)
   1678  *  3. Maybe later fast-case a single page, but I don't think this is
   1679  *     going to make _that_ much difference overall.
   1680  */
   1681 
   1682 #define PMAP_REMOVE_CLEAN_LIST_SIZE	3
   1683 
   1684 void
   1685 pmap_remove(pmap, sva, eva)
   1686 	pmap_t pmap;
   1687 	vaddr_t sva;
   1688 	vaddr_t eva;
   1689 {
   1690 	int cleanlist_idx = 0;
   1691 	struct pagelist {
   1692 		vaddr_t va;
   1693 		pt_entry_t *pte;
   1694 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   1695 	pt_entry_t *pte = 0, *ptes;
   1696 	paddr_t pa;
   1697 	int pmap_active;
   1698 	struct pv_entry *pv;
   1699 
   1700 	/* Exit quick if there is no pmap */
   1701 	if (!pmap)
   1702 		return;
   1703 
   1704 	PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n", pmap, sva, eva));
   1705 
   1706 	sva &= PG_FRAME;
   1707 	eva &= PG_FRAME;
   1708 
   1709 	ptes = pmap_map_ptes(pmap);
   1710 	/* Get a page table pointer */
   1711 	while (sva < eva) {
   1712 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   1713 			break;
   1714 		sva = (sva & PD_MASK) + NBPD;
   1715 	}
   1716 
   1717 	pte = &ptes[arm_byte_to_page(sva)];
   1718 	/* Note if the pmap is active thus require cache and tlb cleans */
   1719 	if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
   1720 	    || (pmap == kernel_pmap))
   1721 		pmap_active = 1;
   1722 	else
   1723 		pmap_active = 0;
   1724 
   1725 	/* Now loop along */
   1726 	while (sva < eva) {
   1727 		/* Check if we can move to the next PDE (l1 chunk) */
   1728 		if (!(sva & PT_MASK))
   1729 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   1730 				sva += NBPD;
   1731 				pte += arm_byte_to_page(NBPD);
   1732 				continue;
   1733 			}
   1734 
   1735 		/* We've found a valid PTE, so this page of PTEs has to go. */
   1736 		if (pmap_pte_v(pte)) {
   1737 			int bank, off;
   1738 
   1739 			/* Update statistics */
   1740 			--pmap->pm_stats.resident_count;
   1741 
   1742 			/*
   1743 			 * Add this page to our cache remove list, if we can.
   1744 			 * If, however the cache remove list is totally full,
   1745 			 * then do a complete cache invalidation taking note
   1746 			 * to backtrack the PTE table beforehand, and ignore
   1747 			 * the lists in future because there's no longer any
   1748 			 * point in bothering with them (we've paid the
   1749 			 * penalty, so will carry on unhindered). Otherwise,
   1750 			 * when we fall out, we just clean the list.
   1751 			 */
   1752 			PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
   1753 			pa = pmap_pte_pa(pte);
   1754 
   1755 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   1756 				/* Add to the clean list. */
   1757 				cleanlist[cleanlist_idx].pte = pte;
   1758 				cleanlist[cleanlist_idx].va = sva;
   1759 				cleanlist_idx++;
   1760 			} else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   1761 				int cnt;
   1762 
   1763 				/* Nuke everything if needed. */
   1764 				if (pmap_active) {
   1765 					cpu_cache_purgeID();
   1766 					cpu_tlb_flushID();
   1767 				}
   1768 
   1769 				/*
   1770 				 * Roll back the previous PTE list,
   1771 				 * and zero out the current PTE.
   1772 				 */
   1773 				for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   1774 					*cleanlist[cnt].pte = 0;
   1775 					pmap_pte_delref(pmap, cleanlist[cnt].va);
   1776 				}
   1777 				*pte = 0;
   1778 				pmap_pte_delref(pmap, sva);
   1779 				cleanlist_idx++;
   1780 			} else {
   1781 				/*
   1782 				 * We've already nuked the cache and
   1783 				 * TLB, so just carry on regardless,
   1784 				 * and we won't need to do it again
   1785 				 */
   1786 				*pte = 0;
   1787 				pmap_pte_delref(pmap, sva);
   1788 			}
   1789 
   1790 			/*
   1791 			 * Update flags. In a number of circumstances,
   1792 			 * we could cluster a lot of these and do a
   1793 			 * number of sequential pages in one go.
   1794 			 */
   1795 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   1796 				pv = &vm_physmem[bank].pmseg.pvent[off];
   1797 				pmap_remove_pv(pmap, sva, pv);
   1798 				pmap_vac_me_harder(pmap, pv, ptes);
   1799 			}
   1800 		}
   1801 		sva += NBPG;
   1802 		pte++;
   1803 	}
   1804 
   1805 	pmap_unmap_ptes(pmap);
   1806 	/*
   1807 	 * Now, if we've fallen through down to here, chances are that there
   1808 	 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
   1809 	 */
   1810 	if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   1811 		u_int cnt;
   1812 
   1813 		for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   1814 			if (pmap_active) {
   1815 				cpu_cache_purgeID_rng(cleanlist[cnt].va, NBPG);
   1816 				*cleanlist[cnt].pte = 0;
   1817 				cpu_tlb_flushID_SE(cleanlist[cnt].va);
   1818 			} else
   1819 				*cleanlist[cnt].pte = 0;
   1820 			pmap_pte_delref(pmap, cleanlist[cnt].va);
   1821 		}
   1822 	}
   1823 }
   1824 
   1825 /*
   1826  * Routine:	pmap_remove_all
   1827  * Function:
   1828  *		Removes this physical page from
   1829  *		all physical maps in which it resides.
   1830  *		Reflects back modify bits to the pager.
   1831  */
   1832 
   1833 void
   1834 pmap_remove_all(pa)
   1835 	paddr_t pa;
   1836 {
   1837 	struct pv_entry *ph, *pv, *npv;
   1838 	pmap_t pmap;
   1839 	pt_entry_t *pte, *ptes;
   1840 	int s;
   1841 
   1842 	PDEBUG(0, printf("pmap_remove_all: pa=%lx ", pa));
   1843 
   1844 	pv = ph = pmap_find_pv(pa);
   1845 	pmap_clean_page(pv);
   1846 
   1847 	s = splvm();
   1848 
   1849 	if (ph->pv_pmap == NULL) {
   1850 		PDEBUG(0, printf("free page\n"));
   1851 		splx(s);
   1852 		return;
   1853 	}
   1854 
   1855 
   1856 
   1857 	while (pv) {
   1858 		pmap = pv->pv_pmap;
   1859 		ptes = pmap_map_ptes(pmap);
   1860 		pte = &ptes[arm_byte_to_page(pv->pv_va)];
   1861 
   1862 		PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
   1863 		    pv->pv_va, pv->pv_flags));
   1864 #ifdef DEBUG
   1865 		if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)
   1866 			    || pmap_pte_pa(pte) != pa)
   1867 			panic("pmap_remove_all: bad mapping");
   1868 #endif	/* DEBUG */
   1869 
   1870 		/*
   1871 		 * Update statistics
   1872 		 */
   1873 		--pmap->pm_stats.resident_count;
   1874 
   1875 		/* Wired bit */
   1876 		if (pv->pv_flags & PT_W)
   1877 			--pmap->pm_stats.wired_count;
   1878 
   1879 		/*
   1880 		 * Invalidate the PTEs.
   1881 		 * XXX: should cluster them up and invalidate as many
   1882 		 * as possible at once.
   1883 		 */
   1884 
   1885 #ifdef needednotdone
   1886 reduce wiring count on page table pages as references drop
   1887 #endif
   1888 
   1889 		*pte = 0;
   1890 		pmap_pte_delref(pmap, pv->pv_va);
   1891 
   1892 		npv = pv->pv_next;
   1893 		if (pv == ph)
   1894 			ph->pv_pmap = NULL;
   1895 		else
   1896 			pmap_free_pv(pv);
   1897 		pv = npv;
   1898 		pmap_unmap_ptes(pmap);
   1899 	}
   1900 
   1901 	splx(s);
   1902 
   1903 	PDEBUG(0, printf("done\n"));
   1904 	cpu_tlb_flushID();
   1905 }
   1906 
   1907 
   1908 /*
   1909  * Set the physical protection on the specified range of this map as requested.
   1910  */
   1911 
   1912 void
   1913 pmap_protect(pmap, sva, eva, prot)
   1914 	pmap_t pmap;
   1915 	vaddr_t sva;
   1916 	vaddr_t eva;
   1917 	vm_prot_t prot;
   1918 {
   1919 	pt_entry_t *pte = NULL, *ptes;
   1920 	int armprot;
   1921 	int flush = 0;
   1922 	paddr_t pa;
   1923 	int bank, off;
   1924 	struct pv_entry *pv;
   1925 
   1926 	/*
   1927 	 * Make sure pmap is valid. -dct
   1928 	 */
   1929 	if (pmap == NULL)
   1930 		return;
   1931 	PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
   1932 	    pmap, sva, eva, prot));
   1933 
   1934 	if (~prot & VM_PROT_READ) {
   1935 		/* Just remove the mappings. */
   1936 		pmap_remove(pmap, sva, eva);
   1937 		return;
   1938 	}
   1939 	if (prot & VM_PROT_WRITE) {
   1940 		/*
   1941 		 * If this is a read->write transition, just ignore it and let
   1942 		 * uvm_fault() take care of it later.
   1943 		 */
   1944 		return;
   1945 	}
   1946 
   1947 	sva &= PG_FRAME;
   1948 	eva &= PG_FRAME;
   1949 
   1950 	ptes = pmap_map_ptes(pmap);
   1951 	/*
   1952 	 * We need to acquire a pointer to a page table page before entering
   1953 	 * the following loop.
   1954 	 */
   1955 	while (sva < eva) {
   1956 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   1957 			break;
   1958 		sva = (sva & PD_MASK) + NBPD;
   1959 	}
   1960 
   1961 	pte = &ptes[arm_byte_to_page(sva)];
   1962 
   1963 	while (sva < eva) {
   1964 		/* only check once in a while */
   1965 		if ((sva & PT_MASK) == 0) {
   1966 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   1967 				/* We can race ahead here, to the next pde. */
   1968 				sva += NBPD;
   1969 				pte += arm_byte_to_page(NBPD);
   1970 				continue;
   1971 			}
   1972 		}
   1973 
   1974 		if (!pmap_pte_v(pte))
   1975 			goto next;
   1976 
   1977 		flush = 1;
   1978 
   1979 		armprot = 0;
   1980 		if (sva < VM_MAXUSER_ADDRESS)
   1981 			armprot |= PT_AP(AP_U);
   1982 		else if (sva < VM_MAX_ADDRESS)
   1983 			armprot |= PT_AP(AP_W);  /* XXX Ekk what is this ? */
   1984 		*pte = (*pte & 0xfffff00f) | armprot;
   1985 
   1986 		pa = pmap_pte_pa(pte);
   1987 
   1988 		/* Get the physical page index */
   1989 
   1990 		/* Clear write flag */
   1991 		if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   1992 			pv = &vm_physmem[bank].pmseg.pvent[off];
   1993 			(void) pmap_modify_pv(pmap, sva, pv, PT_Wr, 0);
   1994 			pmap_vac_me_harder(pmap, pv, ptes);
   1995 		}
   1996 
   1997 next:
   1998 		sva += NBPG;
   1999 		pte++;
   2000 	}
   2001 	pmap_unmap_ptes(pmap);
   2002 	if (flush)
   2003 		cpu_tlb_flushID();
   2004 }
   2005 
   2006 /*
   2007  * void pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2008  * int flags)
   2009  *
   2010  *      Insert the given physical page (p) at
   2011  *      the specified virtual address (v) in the
   2012  *      target physical map with the protection requested.
   2013  *
   2014  *      If specified, the page will be wired down, meaning
   2015  *      that the related pte can not be reclaimed.
   2016  *
   2017  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2018  *      or lose information.  That is, this routine must actually
   2019  *      insert this page into the given map NOW.
   2020  */
   2021 
   2022 int
   2023 pmap_enter(pmap, va, pa, prot, flags)
   2024 	pmap_t pmap;
   2025 	vaddr_t va;
   2026 	paddr_t pa;
   2027 	vm_prot_t prot;
   2028 	int flags;
   2029 {
   2030 	pt_entry_t *pte, *ptes;
   2031 	u_int npte;
   2032 	int bank, off;
   2033 	struct pv_entry *pv = NULL;
   2034 	paddr_t opa;
   2035 	int nflags;
   2036 	boolean_t wired = (flags & PMAP_WIRED) != 0;
   2037 
   2038 	PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
   2039 	    va, pa, pmap, prot, wired));
   2040 
   2041 #ifdef DIAGNOSTIC
   2042 	/* Valid address ? */
   2043 	if (va >= (KERNEL_VM_BASE + KERNEL_VM_SIZE))
   2044 		panic("pmap_enter: too big");
   2045 	if (pmap != pmap_kernel() && va != 0) {
   2046 		if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
   2047 			panic("pmap_enter: kernel page in user map");
   2048 	} else {
   2049 		if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
   2050 			panic("pmap_enter: user page in kernel map");
   2051 		if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
   2052 			panic("pmap_enter: entering PT page");
   2053 	}
   2054 #endif
   2055 
   2056 	/*
   2057 	 * Get a pointer to the pte for this virtual address. If the
   2058 	 * pte pointer is NULL then we are missing the L2 page table
   2059 	 * so we need to create one.
   2060 	 */
   2061 	pte = pmap_pte(pmap, va);
   2062 	if (!pte) {
   2063 		paddr_t l2pa;
   2064 		struct vm_page *m;
   2065 
   2066 		/* Allocate a page table */
   2067 		for (;;) {
   2068 			m = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
   2069 			if (m != NULL)
   2070 				break;
   2071 
   2072 			/*
   2073 			 * No page available.  If we're the kernel
   2074 			 * pmap, we die, since we might not have
   2075 			 * a valid thread context.  For user pmaps,
   2076 			 * we assume that we _do_ have a valid thread
   2077 			 * context, so we wait here for the pagedaemon
   2078 			 * to free up some pages.
   2079 			 *
   2080 			 * XXX THE VM CODE IS PROBABLY HOLDING LOCKS
   2081 			 * XXX RIGHT NOW, BUT ONLY ON OUR PARENT VM_MAP
   2082 			 * XXX SO THIS IS PROBABLY SAFE.  In any case,
   2083 			 * XXX other pmap modules claim it is safe to
   2084 			 * XXX sleep here if it's a user pmap.
   2085 			 */
   2086 			if (pmap == pmap_kernel())
   2087 				panic("pmap_enter: no free pages");
   2088 			else
   2089 				uvm_wait("pmap_enter");
   2090 		}
   2091 
   2092 		/* Wire this page table into the L1. */
   2093 		l2pa = VM_PAGE_TO_PHYS(m);
   2094 		pmap_zero_page(l2pa);
   2095 		pmap_map_in_l1(pmap, va, l2pa);
   2096 		++pmap->pm_stats.resident_count;
   2097 
   2098 		pte = pmap_pte(pmap, va);
   2099 #ifdef DIAGNOSTIC
   2100 		if (!pte)
   2101 			panic("pmap_enter: no pte");
   2102 #endif
   2103 	}
   2104 
   2105 	nflags = 0;
   2106 	if (prot & VM_PROT_WRITE)
   2107 		nflags |= PT_Wr;
   2108 	if (wired)
   2109 		nflags |= PT_W;
   2110 
   2111 	/* More debugging info */
   2112 	PDEBUG(5, printf("pmap_enter: pte for V%08lx = V%p (%08x)\n", va, pte,
   2113 	    *pte));
   2114 
   2115 	/* Is the pte valid ? If so then this page is already mapped */
   2116 	if (pmap_pte_v(pte)) {
   2117 		/* Get the physical address of the current page mapped */
   2118 		opa = pmap_pte_pa(pte);
   2119 
   2120 #ifdef MYCROFT_HACK
   2121 		printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx\n", pmap, va, pa, opa);
   2122 #endif
   2123 
   2124 		/* Are we mapping the same page ? */
   2125 		if (opa == pa) {
   2126 			/* All we must be doing is changing the protection */
   2127 			PDEBUG(0, printf("Case 02 in pmap_enter (V%08lx P%08lx)\n",
   2128 			    va, pa));
   2129 
   2130 			/* Has the wiring changed ? */
   2131 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2132 				pv = &vm_physmem[bank].pmseg.pvent[off];
   2133 				(void) pmap_modify_pv(pmap, va, pv,
   2134 				    PT_Wr | PT_W, nflags);
   2135  			}
   2136 		} else {
   2137 			/* We are replacing the page with a new one. */
   2138 			cpu_cache_purgeID_rng(va, NBPG);
   2139 
   2140 			PDEBUG(0, printf("Case 03 in pmap_enter (V%08lx P%08lx P%08lx)\n",
   2141 			    va, pa, opa));
   2142 
   2143 			/*
   2144 			 * If it is part of our managed memory then we
   2145 			 * must remove it from the PV list
   2146 			 */
   2147 			if ((bank = vm_physseg_find(atop(opa), &off)) != -1) {
   2148 				pv = &vm_physmem[bank].pmseg.pvent[off];
   2149 				pmap_remove_pv(pmap, va, pv);
   2150 			}
   2151 
   2152 			goto enter;
   2153 		}
   2154 	} else {
   2155 		opa = 0;
   2156 		pmap_pte_addref(pmap, va);
   2157 
   2158 		/* pte is not valid so we must be hooking in a new page */
   2159 		++pmap->pm_stats.resident_count;
   2160 
   2161 	enter:
   2162 		/*
   2163 		 * Enter on the PV list if part of our managed memory
   2164 		 */
   2165 		if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2166 			pv = &vm_physmem[bank].pmseg.pvent[off];
   2167 			pmap_enter_pv(pmap, va, pv, nflags);
   2168 		}
   2169 	}
   2170 
   2171 #ifdef MYCROFT_HACK
   2172 	if (mycroft_hack)
   2173 		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);
   2174 #endif
   2175 
   2176 	/* Construct the pte, giving the correct access. */
   2177 	npte = (pa & PG_FRAME);
   2178 
   2179 	/* VA 0 is magic. */
   2180 	if (pmap != pmap_kernel() && va != 0)
   2181 		npte |= PT_AP(AP_U);
   2182 
   2183 	if (bank != -1) {
   2184 #ifdef DIAGNOSTIC
   2185 		if ((flags & VM_PROT_ALL) & ~prot)
   2186 			panic("pmap_enter: access_type exceeds prot");
   2187 #endif
   2188 		npte |= PT_C | PT_B;
   2189 		if (flags & VM_PROT_WRITE) {
   2190 			npte |= L2_SPAGE | PT_AP(AP_W);
   2191 			vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   2192 		} else if (flags & VM_PROT_ALL) {
   2193 			npte |= L2_SPAGE;
   2194 			vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   2195 		} else
   2196 			npte |= L2_INVAL;
   2197 	} else {
   2198 		if (prot & VM_PROT_WRITE)
   2199 			npte |= L2_SPAGE | PT_AP(AP_W);
   2200 		else if (prot & VM_PROT_ALL)
   2201 			npte |= L2_SPAGE;
   2202 		else
   2203 			npte |= L2_INVAL;
   2204 	}
   2205 
   2206 #ifdef MYCROFT_HACK
   2207 	if (mycroft_hack)
   2208 		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);
   2209 #endif
   2210 
   2211 	*pte = npte;
   2212 
   2213 	if (bank != -1)
   2214 	{
   2215 		/* XXX this will change once the whole of pmap_enter uses
   2216 		 * map_ptes
   2217 		 */
   2218 		ptes = pmap_map_ptes(pmap);
   2219 		pmap_vac_me_harder(pmap, pv, ptes);
   2220 		pmap_unmap_ptes(pmap);
   2221 	}
   2222 
   2223 	/* Better flush the TLB ... */
   2224 	cpu_tlb_flushID_SE(va);
   2225 
   2226 	PDEBUG(5, printf("pmap_enter: pte = V%p %08x\n", pte, *pte));
   2227 
   2228 	return 0;
   2229 }
   2230 
   2231 void
   2232 pmap_kenter_pa(va, pa, prot)
   2233 	vaddr_t va;
   2234 	paddr_t pa;
   2235 	vm_prot_t prot;
   2236 {
   2237 	pmap_enter(pmap_kernel(), va, pa, prot, PMAP_WIRED);
   2238 }
   2239 
   2240 void
   2241 pmap_kremove(va, len)
   2242 	vaddr_t va;
   2243 	vsize_t len;
   2244 {
   2245 	for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
   2246 		pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
   2247 	}
   2248 }
   2249 
   2250 /*
   2251  * pmap_page_protect:
   2252  *
   2253  * Lower the permission for all mappings to a given page.
   2254  */
   2255 
   2256 void
   2257 pmap_page_protect(pg, prot)
   2258 	struct vm_page *pg;
   2259 	vm_prot_t prot;
   2260 {
   2261 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2262 
   2263 	PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n", pa, prot));
   2264 
   2265 	switch(prot) {
   2266 	case VM_PROT_READ:
   2267 	case VM_PROT_READ|VM_PROT_EXECUTE:
   2268 		pmap_copy_on_write(pa);
   2269 		break;
   2270 
   2271 	case VM_PROT_ALL:
   2272 		break;
   2273 
   2274 	default:
   2275 		pmap_remove_all(pa);
   2276 		break;
   2277 	}
   2278 }
   2279 
   2280 
   2281 /*
   2282  * Routine:	pmap_unwire
   2283  * Function:	Clear the wired attribute for a map/virtual-address
   2284  *		pair.
   2285  * In/out conditions:
   2286  *		The mapping must already exist in the pmap.
   2287  */
   2288 
   2289 void
   2290 pmap_unwire(pmap, va)
   2291 	pmap_t pmap;
   2292 	vaddr_t va;
   2293 {
   2294 	pt_entry_t *pte;
   2295 	paddr_t pa;
   2296 	int bank, off;
   2297 	struct pv_entry *pv;
   2298 
   2299 	/*
   2300 	 * Make sure pmap is valid. -dct
   2301 	 */
   2302 	if (pmap == NULL)
   2303 		return;
   2304 
   2305 	/* Get the pte */
   2306 	pte = pmap_pte(pmap, va);
   2307 	if (!pte)
   2308 		return;
   2309 
   2310 	/* Extract the physical address of the page */
   2311 	pa = pmap_pte_pa(pte);
   2312 
   2313 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2314 		return;
   2315 	pv = &vm_physmem[bank].pmseg.pvent[off];
   2316 	/* Update the wired bit in the pv entry for this page. */
   2317 	(void) pmap_modify_pv(pmap, va, pv, PT_W, 0);
   2318 }
   2319 
   2320 /*
   2321  * pt_entry_t *pmap_pte(pmap_t pmap, vaddr_t va)
   2322  *
   2323  * Return the pointer to a page table entry corresponding to the supplied
   2324  * virtual address.
   2325  *
   2326  * The page directory is first checked to make sure that a page table
   2327  * for the address in question exists and if it does a pointer to the
   2328  * entry is returned.
   2329  *
   2330  * The way this works is that that the kernel page tables are mapped
   2331  * into the memory map at ALT_PAGE_TBLS_BASE to ALT_PAGE_TBLS_BASE+4MB.
   2332  * This allows page tables to be located quickly.
   2333  */
   2334 pt_entry_t *
   2335 pmap_pte(pmap, va)
   2336 	pmap_t pmap;
   2337 	vaddr_t va;
   2338 {
   2339 	pt_entry_t *ptp;
   2340 	pt_entry_t *result;
   2341 
   2342 	/* The pmap must be valid */
   2343 	if (!pmap)
   2344 		return(NULL);
   2345 
   2346 	/* Return the address of the pte */
   2347 	PDEBUG(10, printf("pmap_pte: pmap=%p va=V%08lx pde = V%p (%08X)\n",
   2348 	    pmap, va, pmap_pde(pmap, va), *(pmap_pde(pmap, va))));
   2349 
   2350 	/* Do we have a valid pde ? If not we don't have a page table */
   2351 	if (!pmap_pde_v(pmap_pde(pmap, va))) {
   2352 		PDEBUG(0, printf("pmap_pte: failed - pde = %p\n",
   2353 		    pmap_pde(pmap, va)));
   2354 		return(NULL);
   2355 	}
   2356 
   2357 	PDEBUG(10, printf("pmap pagetable = P%08lx current = P%08x\n",
   2358 	    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2359 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2360 	    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)));
   2361 
   2362 	/*
   2363 	 * If the pmap is the kernel pmap or the pmap is the active one
   2364 	 * then we can just return a pointer to entry relative to
   2365 	 * PROCESS_PAGE_TBLS_BASE.
   2366 	 * Otherwise we need to map the page tables to an alternative
   2367 	 * address and reference them there.
   2368 	 */
   2369 	if (pmap == kernel_pmap || pmap->pm_pptpt
   2370 	    == (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2371 	    + ((PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) &
   2372 	    ~3) + (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   2373 		ptp = (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   2374 	} else {
   2375 		struct proc *p = curproc;
   2376 
   2377 		/* If we don't have a valid curproc use proc0 */
   2378 		/* Perhaps we should just use kernel_pmap instead */
   2379 		if (p == NULL)
   2380 			p = &proc0;
   2381 #ifdef DIAGNOSTIC
   2382 		/*
   2383 		 * The pmap should always be valid for the process so
   2384 		 * panic if it is not.
   2385 		 */
   2386 		if (!p->p_vmspace || !p->p_vmspace->vm_map.pmap) {
   2387 			printf("pmap_pte: va=%08lx p=%p vm=%p\n",
   2388 			    va, p, p->p_vmspace);
   2389 			console_debugger();
   2390 		}
   2391 		/*
   2392 		 * The pmap for the current process should be mapped. If it
   2393 		 * is not then we have a problem.
   2394 		 */
   2395 		if (p->p_vmspace->vm_map.pmap->pm_pptpt !=
   2396 		    (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2397 		    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2398 		    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   2399 			printf("pmap pagetable = P%08lx current = P%08x ",
   2400 			    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2401 			    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2402 			    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) &
   2403 			    PG_FRAME));
   2404 			printf("pptpt=%lx\n", p->p_vmspace->vm_map.pmap->pm_pptpt);
   2405 			panic("pmap_pte: current and pmap mismatch\n");
   2406 		}
   2407 #endif
   2408 
   2409 		ptp = (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   2410 		pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   2411 		    pmap->pm_pptpt);
   2412 		cpu_tlb_flushD();
   2413 	}
   2414 	PDEBUG(10, printf("page tables base = %p offset=%lx\n", ptp,
   2415 	    ((va >> (PGSHIFT-2)) & ~3)));
   2416 	result = (pt_entry_t *)((char *)ptp + ((va >> (PGSHIFT-2)) & ~3));
   2417 	return(result);
   2418 }
   2419 
   2420 /*
   2421  * Routine:  pmap_extract
   2422  * Function:
   2423  *           Extract the physical page address associated
   2424  *           with the given map/virtual_address pair.
   2425  */
   2426 boolean_t
   2427 pmap_extract(pmap, va, pap)
   2428 	pmap_t pmap;
   2429 	vaddr_t va;
   2430 	paddr_t *pap;
   2431 {
   2432 	pt_entry_t *pte, *ptes;
   2433 	paddr_t pa;
   2434 
   2435 	PDEBUG(5, printf("pmap_extract: pmap=%p, va=V%08lx\n", pmap, va));
   2436 
   2437 	/*
   2438 	 * Get the pte for this virtual address.
   2439 	 */
   2440 	ptes = pmap_map_ptes(pmap);
   2441 	pte = &ptes[arm_byte_to_page(va)];
   2442 
   2443 	/*
   2444 	 * If there is no pte then there is no page table etc.
   2445 	 * Is the pte valid ? If not then no paged is actually mapped here
   2446 	 */
   2447 	if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)){
   2448 	    pmap_unmap_ptes(pmap);
   2449     	    return (FALSE);
   2450 	}
   2451 
   2452 	/* Return the physical address depending on the PTE type */
   2453 	/* XXX What about L1 section mappings ? */
   2454 	if ((*(pte) & L2_MASK) == L2_LPAGE) {
   2455 		/* Extract the physical address from the pte */
   2456 		pa = (*(pte)) & ~(L2_LPAGE_SIZE - 1);
   2457 
   2458 		PDEBUG(5, printf("pmap_extract: LPAGE pa = P%08lx\n",
   2459 		    (pa | (va & (L2_LPAGE_SIZE - 1)))));
   2460 
   2461 		if (pap != NULL)
   2462 			*pap = pa | (va & (L2_LPAGE_SIZE - 1));
   2463 	} else {
   2464 		/* Extract the physical address from the pte */
   2465 		pa = pmap_pte_pa(pte);
   2466 
   2467 		PDEBUG(5, printf("pmap_extract: SPAGE pa = P%08lx\n",
   2468 		    (pa | (va & ~PG_FRAME))));
   2469 
   2470 		if (pap != NULL)
   2471 			*pap = pa | (va & ~PG_FRAME);
   2472 	}
   2473 	pmap_unmap_ptes(pmap);
   2474 	return (TRUE);
   2475 }
   2476 
   2477 
   2478 /*
   2479  * Copy the range specified by src_addr/len from the source map to the
   2480  * range dst_addr/len in the destination map.
   2481  *
   2482  * This routine is only advisory and need not do anything.
   2483  */
   2484 
   2485 void
   2486 pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
   2487 	pmap_t dst_pmap;
   2488 	pmap_t src_pmap;
   2489 	vaddr_t dst_addr;
   2490 	vsize_t len;
   2491 	vaddr_t src_addr;
   2492 {
   2493 	PDEBUG(0, printf("pmap_copy(%p, %p, %lx, %lx, %lx)\n",
   2494 	    dst_pmap, src_pmap, dst_addr, len, src_addr));
   2495 }
   2496 
   2497 #if defined(PMAP_DEBUG)
   2498 void
   2499 pmap_dump_pvlist(phys, m)
   2500 	vaddr_t phys;
   2501 	char *m;
   2502 {
   2503 	struct pv_entry *pv;
   2504 	int bank, off;
   2505 
   2506 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1) {
   2507 		printf("INVALID PA\n");
   2508 		return;
   2509 	}
   2510 	pv = &vm_physmem[bank].pmseg.pvent[off];
   2511 	printf("%s %08lx:", m, phys);
   2512 	if (pv->pv_pmap == NULL) {
   2513 		printf(" no mappings\n");
   2514 		return;
   2515 	}
   2516 
   2517 	for (; pv; pv = pv->pv_next)
   2518 		printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
   2519 		    pv->pv_va, pv->pv_flags);
   2520 
   2521 	printf("\n");
   2522 }
   2523 
   2524 #endif	/* PMAP_DEBUG */
   2525 
   2526 boolean_t
   2527 pmap_testbit(pa, setbits)
   2528 	paddr_t pa;
   2529 	int setbits;
   2530 {
   2531 	int bank, off;
   2532 
   2533 	PDEBUG(1, printf("pmap_testbit: pa=%08lx set=%08x\n", pa, setbits));
   2534 
   2535 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2536 		return(FALSE);
   2537 
   2538 	/*
   2539 	 * Check saved info only
   2540 	 */
   2541 	if (vm_physmem[bank].pmseg.attrs[off] & setbits) {
   2542 		PDEBUG(0, printf("pmap_attributes = %02x\n",
   2543 		    vm_physmem[bank].pmseg.attrs[off]));
   2544 		return(TRUE);
   2545 	}
   2546 
   2547 	return(FALSE);
   2548 }
   2549 
   2550 static pt_entry_t *
   2551 pmap_map_ptes(struct pmap *pmap)
   2552 {
   2553     struct proc *p;
   2554 
   2555     /* the kernel's pmap is always accessible */
   2556     if (pmap == pmap_kernel()) {
   2557 	return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE ;
   2558     }
   2559 
   2560     if (curproc &&
   2561 	    curproc->p_vmspace->vm_map.pmap == pmap)
   2562 	return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   2563 
   2564     p = curproc;
   2565 
   2566     if (p == NULL)
   2567 	p = &proc0;
   2568 
   2569     pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   2570 	    pmap->pm_pptpt);
   2571     cpu_tlb_flushD();
   2572     return (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   2573 }
   2574 
   2575 /*
   2576  * Modify pte bits for all ptes corresponding to the given physical address.
   2577  * We use `maskbits' rather than `clearbits' because we're always passing
   2578  * constants and the latter would require an extra inversion at run-time.
   2579  */
   2580 
   2581 void
   2582 pmap_clearbit(pa, maskbits)
   2583 	paddr_t pa;
   2584 	int maskbits;
   2585 {
   2586 	struct pv_entry *pv;
   2587 	pt_entry_t *pte;
   2588 	vaddr_t va;
   2589 	int bank, off;
   2590 	int s;
   2591 
   2592 	PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
   2593 	    pa, maskbits));
   2594 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2595 		return;
   2596 	pv = &vm_physmem[bank].pmseg.pvent[off];
   2597 	s = splvm();
   2598 
   2599 	/*
   2600 	 * Clear saved attributes (modify, reference)
   2601 	 */
   2602 	vm_physmem[bank].pmseg.attrs[off] &= ~maskbits;
   2603 
   2604 	if (pv->pv_pmap == NULL) {
   2605 		splx(s);
   2606 		return;
   2607 	}
   2608 
   2609 	/*
   2610 	 * Loop over all current mappings setting/clearing as appropos
   2611 	 */
   2612 	for (; pv; pv = pv->pv_next) {
   2613 		va = pv->pv_va;
   2614 
   2615 		/*
   2616 		 * XXX don't write protect pager mappings
   2617 		 */
   2618 		if (va >= uvm.pager_sva && va < uvm.pager_eva) {
   2619 			printf("pmap_clearbit: found page VA on pv_list\n");
   2620 			continue;
   2621 		}
   2622 
   2623 		pv->pv_flags &= ~maskbits;
   2624 		pte = pmap_pte(pv->pv_pmap, va);
   2625 		if (maskbits & (PT_Wr|PT_M))
   2626 			*pte = *pte & ~PT_AP(AP_W);
   2627 		if (maskbits & PT_H)
   2628 			*pte = (*pte & ~L2_MASK) | L2_INVAL;
   2629 	}
   2630 	cpu_tlb_flushID();
   2631 
   2632 	splx(s);
   2633 }
   2634 
   2635 
   2636 boolean_t
   2637 pmap_clear_modify(pg)
   2638 	struct vm_page *pg;
   2639 {
   2640 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2641 	boolean_t rv;
   2642 
   2643 	PDEBUG(0, printf("pmap_clear_modify pa=%08lx\n", pa));
   2644 	rv = pmap_testbit(pa, PT_M);
   2645 	pmap_clearbit(pa, PT_M);
   2646 	return rv;
   2647 }
   2648 
   2649 
   2650 boolean_t
   2651 pmap_clear_reference(pg)
   2652 	struct vm_page *pg;
   2653 {
   2654 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2655 	boolean_t rv;
   2656 
   2657 	PDEBUG(0, printf("pmap_clear_reference pa=%08lx\n", pa));
   2658 	rv = pmap_testbit(pa, PT_H);
   2659 	pmap_clearbit(pa, PT_H);
   2660 	return rv;
   2661 }
   2662 
   2663 
   2664 void
   2665 pmap_copy_on_write(pa)
   2666 	paddr_t pa;
   2667 {
   2668 	PDEBUG(0, printf("pmap_copy_on_write pa=%08lx\n", pa));
   2669 	pmap_clearbit(pa, PT_Wr);
   2670 }
   2671 
   2672 
   2673 boolean_t
   2674 pmap_is_modified(pg)
   2675 	struct vm_page *pg;
   2676 {
   2677 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2678 	boolean_t result;
   2679 
   2680 	result = pmap_testbit(pa, PT_M);
   2681 	PDEBUG(0, printf("pmap_is_modified pa=%08lx %x\n", pa, result));
   2682 	return (result);
   2683 }
   2684 
   2685 
   2686 boolean_t
   2687 pmap_is_referenced(pg)
   2688 	struct vm_page *pg;
   2689 {
   2690 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2691 	boolean_t result;
   2692 
   2693 	result = pmap_testbit(pa, PT_H);
   2694 	PDEBUG(0, printf("pmap_is_referenced pa=%08lx %x\n", pa, result));
   2695 	return (result);
   2696 }
   2697 
   2698 
   2699 int
   2700 pmap_modified_emulation(pmap, va)
   2701 	pmap_t pmap;
   2702 	vaddr_t va;
   2703 {
   2704 	pt_entry_t *pte;
   2705 	paddr_t pa;
   2706 	int bank, off;
   2707 	struct pv_entry *pv;
   2708 	u_int flags;
   2709 
   2710 	PDEBUG(2, printf("pmap_modified_emulation\n"));
   2711 
   2712 	/* Get the pte */
   2713 	pte = pmap_pte(pmap, va);
   2714 	if (!pte) {
   2715 		PDEBUG(2, printf("no pte\n"));
   2716 		return(0);
   2717 	}
   2718 
   2719 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   2720 
   2721 	/* Check for a zero pte */
   2722 	if (*pte == 0)
   2723 		return(0);
   2724 
   2725 	/* This can happen if user code tries to access kernel memory. */
   2726 	if ((*pte & PT_AP(AP_W)) != 0)
   2727 		return (0);
   2728 
   2729 	/* Extract the physical address of the page */
   2730 	pa = pmap_pte_pa(pte);
   2731 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2732 		return(0);
   2733 
   2734 	/* Get the current flags for this page. */
   2735 	pv = &vm_physmem[bank].pmseg.pvent[off];
   2736 	flags = pmap_modify_pv(pmap, va, pv, 0, 0);
   2737 	PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
   2738 
   2739 	/*
   2740 	 * Do the flags say this page is writable ? If not then it is a
   2741 	 * genuine write fault. If yes then the write fault is our fault
   2742 	 * as we did not reflect the write access in the PTE. Now we know
   2743 	 * a write has occurred we can correct this and also set the
   2744 	 * modified bit
   2745 	 */
   2746 	if (~flags & PT_Wr)
   2747 		return(0);
   2748 
   2749 	PDEBUG(0, printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %p (%08x)\n",
   2750 	    va, pte, *pte));
   2751 	vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   2752 	*pte = (*pte & ~L2_MASK) | L2_SPAGE | PT_AP(AP_W);
   2753 	PDEBUG(0, printf("->(%08x)\n", *pte));
   2754 
   2755 	/* Return, indicating the problem has been dealt with */
   2756 	cpu_tlb_flushID_SE(va);
   2757 	return(1);
   2758 }
   2759 
   2760 
   2761 int
   2762 pmap_handled_emulation(pmap, va)
   2763 	pmap_t pmap;
   2764 	vaddr_t va;
   2765 {
   2766 	pt_entry_t *pte;
   2767 	paddr_t pa;
   2768 	int bank, off;
   2769 
   2770 	PDEBUG(2, printf("pmap_handled_emulation\n"));
   2771 
   2772 	/* Get the pte */
   2773 	pte = pmap_pte(pmap, va);
   2774 	if (!pte) {
   2775 		PDEBUG(2, printf("no pte\n"));
   2776 		return(0);
   2777 	}
   2778 
   2779 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   2780 
   2781 	/* Check for a zero pte */
   2782 	if (*pte == 0)
   2783 		return(0);
   2784 
   2785 	/* This can happen if user code tries to access kernel memory. */
   2786 	if ((*pte & L2_MASK) != L2_INVAL)
   2787 		return (0);
   2788 
   2789 	/* Extract the physical address of the page */
   2790 	pa = pmap_pte_pa(pte);
   2791 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2792 		return(0);
   2793 
   2794 	/*
   2795 	 * Ok we just enable the pte and mark the attibs as handled
   2796 	 */
   2797 	PDEBUG(0, printf("pmap_handled_emulation: Got a hit va=%08lx pte = %p (%08x)\n",
   2798 	    va, pte, *pte));
   2799 	vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   2800 	*pte = (*pte & ~L2_MASK) | L2_SPAGE;
   2801 	PDEBUG(0, printf("->(%08x)\n", *pte));
   2802 
   2803 	/* Return, indicating the problem has been dealt with */
   2804 	cpu_tlb_flushID_SE(va);
   2805 	return(1);
   2806 }
   2807 
   2808 /*
   2809  * pmap_collect: free resources held by a pmap
   2810  *
   2811  * => optional function.
   2812  * => called when a process is swapped out to free memory.
   2813  */
   2814 
   2815 void
   2816 pmap_collect(pmap)
   2817 	pmap_t pmap;
   2818 {
   2819 }
   2820 
   2821 /*
   2822  * Routine:	pmap_procwr
   2823  *
   2824  * Function:
   2825  *	Synchronize caches corresponding to [addr, addr+len) in p.
   2826  *
   2827  */
   2828 void
   2829 pmap_procwr(p, va, len)
   2830 	struct proc	*p;
   2831 	vaddr_t		va;
   2832 	int		len;
   2833 {
   2834 	/* We only need to do anything if it is the current process. */
   2835 	if (p == curproc)
   2836 		cpu_cache_syncI_rng(va, len);
   2837 }
   2838 
   2839 /* End of pmap.c */
   2840