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