Home | History | Annotate | Line # | Download | only in ibm4xx
pmap.c revision 1.60.2.4
      1 /*	$NetBSD: pmap.c,v 1.60.2.4 2010/08/11 13:46:28 uebayasi Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     40  * Copyright (C) 1995, 1996 TooLs GmbH.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by TooLs GmbH.
     54  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     61  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     62  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     63  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     64  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     65  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     66  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.60.2.4 2010/08/11 13:46:28 uebayasi Exp $");
     71 
     72 #include "opt_xip.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/malloc.h>
     76 #include <sys/proc.h>
     77 #include <sys/queue.h>
     78 #include <sys/systm.h>
     79 #include <sys/pool.h>
     80 #include <sys/device.h>
     81 
     82 #include <uvm/uvm.h>
     83 
     84 #include <machine/cpu.h>
     85 #include <machine/pcb.h>
     86 #include <machine/powerpc.h>
     87 
     88 #include <powerpc/spr.h>
     89 #include <powerpc/ibm4xx/spr.h>
     90 #include <machine/tlb.h>
     91 
     92 /*
     93  * kernmap is an array of PTEs large enough to map in
     94  * 4GB.  At 16KB/page it is 256K entries or 2MB.
     95  */
     96 #define KERNMAP_SIZE	((0xffffffffU/PAGE_SIZE)+1)
     97 void *kernmap;
     98 
     99 #define MINCTX		2
    100 #define NUMCTX		256
    101 
    102 volatile struct pmap *ctxbusy[NUMCTX];
    103 
    104 #define TLBF_USED	0x1
    105 #define	TLBF_REF	0x2
    106 #define	TLBF_LOCKED	0x4
    107 #define	TLB_LOCKED(i)	(tlb_info[(i)].ti_flags & TLBF_LOCKED)
    108 
    109 typedef struct tlb_info_s {
    110 	char	ti_flags;
    111 	char	ti_ctx;		/* TLB_PID assiciated with the entry */
    112 	u_int	ti_va;
    113 } tlb_info_t;
    114 
    115 volatile tlb_info_t tlb_info[NTLB];
    116 /* We'll use a modified FIFO replacement policy cause it's cheap */
    117 volatile int tlbnext;
    118 
    119 static int tlb_nreserved = 0;
    120 static int pmap_bootstrap_done = 0;
    121 
    122 /* Event counters */
    123 struct evcnt tlbmiss_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
    124 	NULL, "cpu", "tlbmiss");
    125 struct evcnt tlbhit_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
    126 	NULL, "cpu", "tlbhit");
    127 struct evcnt tlbflush_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
    128 	NULL, "cpu", "tlbflush");
    129 struct evcnt tlbenter_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
    130 	NULL, "cpu", "tlbenter");
    131 
    132 struct pmap kernel_pmap_;
    133 struct pmap *const kernel_pmap_ptr = &kernel_pmap_;
    134 
    135 static int npgs;
    136 static u_int nextavail;
    137 #ifndef MSGBUFADDR
    138 extern paddr_t msgbuf_paddr;
    139 #endif
    140 
    141 static struct mem_region *mem, *avail;
    142 
    143 /*
    144  * This is a cache of referenced/modified bits.
    145  * Bits herein are shifted by ATTRSHFT.
    146  */
    147 static char *pmap_attrib;
    148 
    149 #define PV_WIRED	0x1
    150 #define PV_WIRE(pv)	((pv)->pv_va |= PV_WIRED)
    151 #define PV_UNWIRE(pv)	((pv)->pv_va &= ~PV_WIRED)
    152 #define PV_ISWIRED(pv)	((pv)->pv_va & PV_WIRED)
    153 #define PV_CMPVA(va,pv)	(!(((pv)->pv_va ^ (va)) & (~PV_WIRED)))
    154 
    155 struct pv_entry {
    156 	struct pv_entry *pv_next;	/* Linked list of mappings */
    157 	vaddr_t pv_va;			/* virtual address of mapping */
    158 	struct pmap *pv_pm;
    159 };
    160 
    161 /* Each index corresponds to TLB_SIZE_* value. */
    162 static size_t tlbsize[] = {
    163 	1024, 		/* TLB_SIZE_1K */
    164 	4096, 		/* TLB_SIZE_4K */
    165 	16384, 		/* TLB_SIZE_16K */
    166 	65536, 		/* TLB_SIZE_64K */
    167 	262144, 	/* TLB_SIZE_256K */
    168 	1048576, 	/* TLB_SIZE_1M */
    169 	4194304, 	/* TLB_SIZE_4M */
    170 	16777216, 	/* TLB_SIZE_16M */
    171 };
    172 
    173 struct pv_entry *pv_table;
    174 static struct pool pv_pool;
    175 
    176 static int pmap_initialized;
    177 
    178 static int ctx_flush(int);
    179 
    180 inline struct pv_entry *pa_to_pv(paddr_t);
    181 static inline char *pa_to_attr(paddr_t);
    182 
    183 static inline volatile u_int *pte_find(struct pmap *, vaddr_t);
    184 static inline int pte_enter(struct pmap *, vaddr_t, u_int);
    185 
    186 static inline int pmap_enter_pv(struct pmap *, vaddr_t, paddr_t, int);
    187 static void pmap_remove_pv(struct pmap *, vaddr_t, paddr_t);
    188 
    189 static int ppc4xx_tlb_size_mask(size_t, int *, int *);
    190 
    191 
    192 inline struct pv_entry *
    193 pa_to_pv(paddr_t pa)
    194 {
    195 	int bank, pg;
    196 
    197 #ifdef XIP
    198 	bank = vm_physseg_find_device(atop(pa), &pg);
    199 	if (bank != -1)
    200 		return &VM_PHYSDEV_PTR(bank)->pmseg.pvent[pg];
    201 #endif
    202 	bank = vm_physseg_find(atop(pa), &pg);
    203 	if (bank != -1)
    204 		return &VM_PHYSMEM_PTR(bank)->pmseg.pvent[pg];
    205 	return NULL;
    206 }
    207 
    208 static inline char *
    209 pa_to_attr(paddr_t pa)
    210 {
    211 	int bank, pg;
    212 
    213 #ifdef XIP
    214 	bank = vm_physseg_find_device(atop(pa), &pg);
    215 	if (bank != -1)
    216 		return &VM_PHYSDEV_PTR(bank)->pmseg.attrs[pg];
    217 #endif
    218 	bank = vm_physseg_find(atop(pa), &pg);
    219 	if (bank != -1)
    220 		return &VM_PHYSMEM_PTR(bank)->pmseg.attrs[pg];
    221 	return NULL;
    222 }
    223 
    224 /*
    225  * Insert PTE into page table.
    226  */
    227 int
    228 pte_enter(struct pmap *pm, vaddr_t va, u_int pte)
    229 {
    230 	int seg = STIDX(va);
    231 	int ptn = PTIDX(va);
    232 	u_int oldpte;
    233 
    234 	if (!pm->pm_ptbl[seg]) {
    235 		/* Don't allocate a page to clear a non-existent mapping. */
    236 		if (!pte)
    237 			return (0);
    238 		/* Allocate a page XXXX this will sleep! */
    239 		pm->pm_ptbl[seg] =
    240 		    (uint *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    241 		    UVM_KMF_WIRED | UVM_KMF_ZERO);
    242 	}
    243 	oldpte = pm->pm_ptbl[seg][ptn];
    244 	pm->pm_ptbl[seg][ptn] = pte;
    245 
    246 	/* Flush entry. */
    247 	ppc4xx_tlb_flush(va, pm->pm_ctx);
    248 	if (oldpte != pte) {
    249 		if (pte == 0)
    250 			pm->pm_stats.resident_count--;
    251 		else
    252 			pm->pm_stats.resident_count++;
    253 	}
    254 	return (1);
    255 }
    256 
    257 /*
    258  * Get a pointer to a PTE in a page table.
    259  */
    260 volatile u_int *
    261 pte_find(struct pmap *pm, vaddr_t va)
    262 {
    263 	int seg = STIDX(va);
    264 	int ptn = PTIDX(va);
    265 
    266 	if (pm->pm_ptbl[seg])
    267 		return (&pm->pm_ptbl[seg][ptn]);
    268 
    269 	return (NULL);
    270 }
    271 
    272 /*
    273  * This is called during initppc, before the system is really initialized.
    274  */
    275 void
    276 pmap_bootstrap(u_int kernelstart, u_int kernelend)
    277 {
    278 	struct mem_region *mp, *mp1;
    279 	int cnt, i;
    280 	u_int s, e, sz;
    281 
    282 	tlbnext = tlb_nreserved;
    283 
    284 	/*
    285 	 * Allocate the kernel page table at the end of
    286 	 * kernel space so it's in the locked TTE.
    287 	 */
    288 	kernmap = (void *)kernelend;
    289 
    290 	/*
    291 	 * Initialize kernel page table.
    292 	 */
    293 	for (i = 0; i < STSZ; i++) {
    294 		pmap_kernel()->pm_ptbl[i] = 0;
    295 	}
    296 	ctxbusy[0] = ctxbusy[1] = pmap_kernel();
    297 
    298 	/*
    299 	 * Announce page-size to the VM-system
    300 	 */
    301 	uvmexp.pagesize = NBPG;
    302 	uvm_setpagesize();
    303 
    304 	/*
    305 	 * Get memory.
    306 	 */
    307 	mem_regions(&mem, &avail);
    308 	for (mp = mem; mp->size; mp++) {
    309 		physmem += btoc(mp->size);
    310 		printf("+%lx,",mp->size);
    311 	}
    312 	printf("\n");
    313 	ppc4xx_tlb_init();
    314 	/*
    315 	 * Count the number of available entries.
    316 	 */
    317 	for (cnt = 0, mp = avail; mp->size; mp++)
    318 		cnt++;
    319 
    320 	/*
    321 	 * Page align all regions.
    322 	 * Non-page aligned memory isn't very interesting to us.
    323 	 * Also, sort the entries for ascending addresses.
    324 	 */
    325 	kernelstart &= ~PGOFSET;
    326 	kernelend = (kernelend + PGOFSET) & ~PGOFSET;
    327 	for (mp = avail; mp->size; mp++) {
    328 		s = mp->start;
    329 		e = mp->start + mp->size;
    330 		printf("%08x-%08x -> ",s,e);
    331 		/*
    332 		 * Check whether this region holds all of the kernel.
    333 		 */
    334 		if (s < kernelstart && e > kernelend) {
    335 			avail[cnt].start = kernelend;
    336 			avail[cnt++].size = e - kernelend;
    337 			e = kernelstart;
    338 		}
    339 		/*
    340 		 * Look whether this regions starts within the kernel.
    341 		 */
    342 		if (s >= kernelstart && s < kernelend) {
    343 			if (e <= kernelend)
    344 				goto empty;
    345 			s = kernelend;
    346 		}
    347 		/*
    348 		 * Now look whether this region ends within the kernel.
    349 		 */
    350 		if (e > kernelstart && e <= kernelend) {
    351 			if (s >= kernelstart)
    352 				goto empty;
    353 			e = kernelstart;
    354 		}
    355 		/*
    356 		 * Now page align the start and size of the region.
    357 		 */
    358 		s = round_page(s);
    359 		e = trunc_page(e);
    360 		if (e < s)
    361 			e = s;
    362 		sz = e - s;
    363 		printf("%08x-%08x = %x\n",s,e,sz);
    364 		/*
    365 		 * Check whether some memory is left here.
    366 		 */
    367 		if (sz == 0) {
    368 		empty:
    369 			memmove(mp, mp + 1,
    370 				(cnt - (mp - avail)) * sizeof *mp);
    371 			cnt--;
    372 			mp--;
    373 			continue;
    374 		}
    375 		/*
    376 		 * Do an insertion sort.
    377 		 */
    378 		npgs += btoc(sz);
    379 		for (mp1 = avail; mp1 < mp; mp1++)
    380 			if (s < mp1->start)
    381 				break;
    382 		if (mp1 < mp) {
    383 			memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
    384 			mp1->start = s;
    385 			mp1->size = sz;
    386 		} else {
    387 			mp->start = s;
    388 			mp->size = sz;
    389 		}
    390 	}
    391 
    392 	/*
    393 	 * We cannot do pmap_steal_memory here,
    394 	 * since we don't run with translation enabled yet.
    395 	 */
    396 #ifndef MSGBUFADDR
    397 	/*
    398 	 * allow for msgbuf
    399 	 */
    400 	sz = round_page(MSGBUFSIZE);
    401 	mp = NULL;
    402 	for (mp1 = avail; mp1->size; mp1++)
    403 		if (mp1->size >= sz)
    404 			mp = mp1;
    405 	if (mp == NULL)
    406 		panic("not enough memory?");
    407 
    408 	npgs -= btoc(sz);
    409 	msgbuf_paddr = mp->start + mp->size - sz;
    410 	mp->size -= sz;
    411 	if (mp->size <= 0)
    412 		memmove(mp, mp + 1, (cnt - (mp - avail)) * sizeof *mp);
    413 #endif
    414 
    415 	for (mp = avail; mp->size; mp++)
    416 		uvm_page_physload(atop(mp->start), atop(mp->start + mp->size),
    417 			atop(mp->start), atop(mp->start + mp->size),
    418 			VM_FREELIST_DEFAULT);
    419 
    420 	/*
    421 	 * Initialize kernel pmap and hardware.
    422 	 */
    423 	/* Setup TLB pid allocator so it knows we alreadu using PID 1 */
    424 	pmap_kernel()->pm_ctx = KERNEL_PID;
    425 	nextavail = avail->start;
    426 
    427 	evcnt_attach_static(&tlbmiss_ev);
    428 	evcnt_attach_static(&tlbhit_ev);
    429 	evcnt_attach_static(&tlbflush_ev);
    430 	evcnt_attach_static(&tlbenter_ev);
    431 
    432 	pmap_bootstrap_done = 1;
    433 }
    434 
    435 /*
    436  * Restrict given range to physical memory
    437  *
    438  * (Used by /dev/mem)
    439  */
    440 void
    441 pmap_real_memory(paddr_t *start, psize_t *size)
    442 {
    443 	struct mem_region *mp;
    444 
    445 	for (mp = mem; mp->size; mp++) {
    446 		if (*start + *size > mp->start &&
    447 		    *start < mp->start + mp->size) {
    448 			if (*start < mp->start) {
    449 				*size -= mp->start - *start;
    450 				*start = mp->start;
    451 			}
    452 			if (*start + *size > mp->start + mp->size)
    453 				*size = mp->start + mp->size - *start;
    454 			return;
    455 		}
    456 	}
    457 	*size = 0;
    458 }
    459 
    460 /*
    461  * Initialize anything else for pmap handling.
    462  * Called during vm_init().
    463  */
    464 void
    465 pmap_init(void)
    466 {
    467 	struct pv_entry *pv;
    468 	vsize_t sz;
    469 	vaddr_t addr;
    470 	int i, s;
    471 	int bank;
    472 	char *attr;
    473 
    474 	sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npgs);
    475 	sz = round_page(sz);
    476 	addr = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED | UVM_KMF_ZERO);
    477 	s = splvm();
    478 	pv = pv_table = (struct pv_entry *)addr;
    479 	for (i = npgs; --i >= 0;)
    480 		pv++->pv_pm = NULL;
    481 	pmap_attrib = (char *)pv;
    482 	memset(pv, 0, npgs);
    483 
    484 	pv = pv_table;
    485 	attr = pmap_attrib;
    486 	for (bank = 0; bank < vm_nphysseg; bank++) {
    487 		sz = VM_PHYSMEM_PTR(bank)->end - VM_PHYSMEM_PTR(bank)->start;
    488 		VM_PHYSMEM_PTR(bank)->pmseg.pvent = pv;
    489 		VM_PHYSMEM_PTR(bank)->pmseg.attrs = attr;
    490 		pv += sz;
    491 		attr += sz;
    492 	}
    493 
    494 	pmap_initialized = 1;
    495 	splx(s);
    496 
    497 	/* Setup a pool for additional pvlist structures */
    498 	pool_init(&pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pv_entry", NULL,
    499 	    IPL_VM);
    500 }
    501 
    502 void
    503 pmap_physseg_init(struct vm_physseg *seg)
    504 {
    505 	size_t npages;
    506 	vsize_t sz;
    507 	struct pv_entry *pv;
    508 	char *attr;
    509 
    510 	npages = seg->end - seg->start + 1;
    511 	sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npages);
    512 	sz = round_page(sz);
    513 	pv = (void *)uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED | UVM_KMF_ZERO);
    514 	attr = (void *)(pv + npages);
    515 
    516 	seg->pmseg.pvent = pv;
    517 	seg->pmseg.attrs = attr;
    518 }
    519 
    520 void
    521 pmap_physseg_fini(struct vm_physseg *seg)
    522 {
    523 	size_t npages;
    524 	vsize_t sz;
    525 
    526 	npages = seg->end - seg->start + 1;
    527 	sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npages);
    528 	sz = round_page(sz);
    529 	uvm_km_free(kernel_map, (vaddr_t)seg->pmseg.pvent, sz, UVM_KMF_WIRED);
    530 }
    531 
    532 /*
    533  * How much virtual space is available to the kernel?
    534  */
    535 void
    536 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
    537 {
    538 
    539 #if 0
    540 	/*
    541 	 * Reserve one segment for kernel virtual memory
    542 	 */
    543 	*start = (vaddr_t)(KERNEL_SR << ADDR_SR_SHFT);
    544 	*end = *start + SEGMENT_LENGTH;
    545 #else
    546 	*start = (vaddr_t) VM_MIN_KERNEL_ADDRESS;
    547 	*end = (vaddr_t) VM_MAX_KERNEL_ADDRESS;
    548 #endif
    549 }
    550 
    551 #ifdef PMAP_GROWKERNEL
    552 /*
    553  * Preallocate kernel page tables to a specified VA.
    554  * This simply loops through the first TTE for each
    555  * page table from the beginning of the kernel pmap,
    556  * reads the entry, and if the result is
    557  * zero (either invalid entry or no page table) it stores
    558  * a zero there, populating page tables in the process.
    559  * This is not the most efficient technique but i don't
    560  * expect it to be called that often.
    561  */
    562 extern struct vm_page *vm_page_alloc1(void);
    563 extern void vm_page_free1(struct vm_page *);
    564 
    565 vaddr_t kbreak = VM_MIN_KERNEL_ADDRESS;
    566 
    567 vaddr_t
    568 pmap_growkernel(vaddr_t maxkvaddr)
    569 {
    570 	int s;
    571 	int seg;
    572 	paddr_t pg;
    573 	struct pmap *pm = pmap_kernel();
    574 
    575 	s = splvm();
    576 
    577 	/* Align with the start of a page table */
    578 	for (kbreak &= ~(PTMAP-1); kbreak < maxkvaddr;
    579 	     kbreak += PTMAP) {
    580 		seg = STIDX(kbreak);
    581 
    582 		if (pte_find(pm, kbreak))
    583 			continue;
    584 
    585 		if (uvm.page_init_done) {
    586 			pg = (paddr_t)VM_PAGE_TO_PHYS(vm_page_alloc1());
    587 		} else {
    588 			if (!uvm_page_physget(&pg))
    589 				panic("pmap_growkernel: no memory");
    590 		}
    591 		if (!pg)
    592 			panic("pmap_growkernel: no pages");
    593 		pmap_zero_page((paddr_t)pg);
    594 
    595 		/* XXX This is based on all phymem being addressable */
    596 		pm->pm_ptbl[seg] = (u_int *)pg;
    597 	}
    598 	splx(s);
    599 	return (kbreak);
    600 }
    601 
    602 /*
    603  *	vm_page_alloc1:
    604  *
    605  *	Allocate and return a memory cell with no associated object.
    606  */
    607 struct vm_page *
    608 vm_page_alloc1(void)
    609 {
    610 	struct vm_page *pg;
    611 
    612 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    613 	if (pg) {
    614 		pg->wire_count = 1;	/* no mappings yet */
    615 		pg->flags &= ~PG_BUSY;	/* never busy */
    616 	}
    617 	return pg;
    618 }
    619 
    620 /*
    621  *	vm_page_free1:
    622  *
    623  *	Returns the given page to the free list,
    624  *	disassociating it with any VM object.
    625  *
    626  *	Object and page must be locked prior to entry.
    627  */
    628 void
    629 vm_page_free1(struct vm_page *pg)
    630 {
    631 #ifdef DIAGNOSTIC
    632 	if (pg->flags != (PG_CLEAN|PG_FAKE)) {
    633 		printf("Freeing invalid page %p\n", pg);
    634 		printf("pa = %llx\n", (unsigned long long)VM_PAGE_TO_PHYS(pg));
    635 #ifdef DDB
    636 		Debugger();
    637 #endif
    638 		return;
    639 	}
    640 #endif
    641 	pg->flags |= PG_BUSY;
    642 	pg->wire_count = 0;
    643 	uvm_pagefree(pg);
    644 }
    645 #endif
    646 
    647 /*
    648  * Create and return a physical map.
    649  */
    650 struct pmap *
    651 pmap_create(void)
    652 {
    653 	struct pmap *pm;
    654 
    655 	pm = malloc(sizeof *pm, M_VMPMAP, M_WAITOK);
    656 	memset(pm, 0, sizeof *pm);
    657 	pm->pm_refs = 1;
    658 	return pm;
    659 }
    660 
    661 /*
    662  * Add a reference to the given pmap.
    663  */
    664 void
    665 pmap_reference(struct pmap *pm)
    666 {
    667 
    668 	pm->pm_refs++;
    669 }
    670 
    671 /*
    672  * Retire the given pmap from service.
    673  * Should only be called if the map contains no valid mappings.
    674  */
    675 void
    676 pmap_destroy(struct pmap *pm)
    677 {
    678 	int i;
    679 
    680 	if (--pm->pm_refs > 0) {
    681 		return;
    682 	}
    683 	KASSERT(pm->pm_stats.resident_count == 0);
    684 	KASSERT(pm->pm_stats.wired_count == 0);
    685 	for (i = 0; i < STSZ; i++)
    686 		if (pm->pm_ptbl[i]) {
    687 			uvm_km_free(kernel_map, (vaddr_t)pm->pm_ptbl[i],
    688 			    PAGE_SIZE, UVM_KMF_WIRED);
    689 			pm->pm_ptbl[i] = NULL;
    690 		}
    691 	if (pm->pm_ctx)
    692 		ctx_free(pm);
    693 	free(pm, M_VMPMAP);
    694 }
    695 
    696 /*
    697  * Copy the range specified by src_addr/len
    698  * from the source map to the range dst_addr/len
    699  * in the destination map.
    700  *
    701  * This routine is only advisory and need not do anything.
    702  */
    703 void
    704 pmap_copy(struct pmap *dst_pmap, struct pmap *src_pmap, vaddr_t dst_addr,
    705 	  vsize_t len, vaddr_t src_addr)
    706 {
    707 }
    708 
    709 /*
    710  * Require that all active physical maps contain no
    711  * incorrect entries NOW.
    712  */
    713 void
    714 pmap_update(struct pmap *pmap)
    715 {
    716 }
    717 
    718 /*
    719  * Fill the given physical page with zeroes.
    720  */
    721 void
    722 pmap_zero_page(paddr_t pa)
    723 {
    724 
    725 #ifdef PPC_4XX_NOCACHE
    726 	memset((void *)pa, 0, PAGE_SIZE);
    727 #else
    728 	int i;
    729 
    730 	for (i = PAGE_SIZE/CACHELINESIZE; i > 0; i--) {
    731 		__asm volatile ("dcbz 0,%0" :: "r"(pa));
    732 		pa += CACHELINESIZE;
    733 	}
    734 #endif
    735 }
    736 
    737 /*
    738  * Copy the given physical source page to its destination.
    739  */
    740 void
    741 pmap_copy_page(paddr_t src, paddr_t dst)
    742 {
    743 
    744 	memcpy((void *)dst, (void *)src, PAGE_SIZE);
    745 	dcache_flush_page(dst);
    746 }
    747 
    748 /*
    749  * This returns != 0 on success.
    750  */
    751 static inline int
    752 pmap_enter_pv(struct pmap *pm, vaddr_t va, paddr_t pa, int flags)
    753 {
    754 	struct pv_entry *pv, *npv = NULL;
    755 	int s;
    756 
    757 	if (!pmap_initialized)
    758 		return 0;
    759 
    760 	s = splvm();
    761 	pv = pa_to_pv(pa);
    762 	if (!pv->pv_pm) {
    763 		/*
    764 		 * No entries yet, use header as the first entry.
    765 		 */
    766 		pv->pv_va = va;
    767 		pv->pv_pm = pm;
    768 		pv->pv_next = NULL;
    769 	} else {
    770 		/*
    771 		 * There is at least one other VA mapping this page.
    772 		 * Place this entry after the header.
    773 		 */
    774 		npv = pool_get(&pv_pool, PR_NOWAIT);
    775 		if (npv == NULL) {
    776 			if ((flags & PMAP_CANFAIL) == 0)
    777 				panic("pmap_enter_pv: failed");
    778 			splx(s);
    779 			return 0;
    780 		}
    781 		npv->pv_va = va;
    782 		npv->pv_pm = pm;
    783 		npv->pv_next = pv->pv_next;
    784 		pv->pv_next = npv;
    785 		pv = npv;
    786 	}
    787 	if (flags & PMAP_WIRED) {
    788 		PV_WIRE(pv);
    789 		pm->pm_stats.wired_count++;
    790 	}
    791 	splx(s);
    792 	return (1);
    793 }
    794 
    795 static void
    796 pmap_remove_pv(struct pmap *pm, vaddr_t va, paddr_t pa)
    797 {
    798 	struct pv_entry *pv, *npv;
    799 
    800 	/*
    801 	 * Remove from the PV table.
    802 	 */
    803 	pv = pa_to_pv(pa);
    804 	if (!pv)
    805 		return;
    806 
    807 	/*
    808 	 * If it is the first entry on the list, it is actually
    809 	 * in the header and we must copy the following entry up
    810 	 * to the header.  Otherwise we must search the list for
    811 	 * the entry.  In either case we free the now unused entry.
    812 	 */
    813 	if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
    814 		if (PV_ISWIRED(pv)) {
    815 			pm->pm_stats.wired_count--;
    816 		}
    817 		if ((npv = pv->pv_next)) {
    818 			*pv = *npv;
    819 			pool_put(&pv_pool, npv);
    820 		} else
    821 			pv->pv_pm = NULL;
    822 	} else {
    823 		for (; (npv = pv->pv_next) != NULL; pv = npv)
    824 			if (pm == npv->pv_pm && PV_CMPVA(va, npv))
    825 				break;
    826 		if (npv) {
    827 			pv->pv_next = npv->pv_next;
    828 			if (PV_ISWIRED(npv)) {
    829 				pm->pm_stats.wired_count--;
    830 			}
    831 			pool_put(&pv_pool, npv);
    832 		}
    833 	}
    834 }
    835 
    836 /*
    837  * Insert physical page at pa into the given pmap at virtual address va.
    838  */
    839 int
    840 pmap_enter(struct pmap *pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    841 {
    842 	int s;
    843 	u_int tte;
    844 	bool managed;
    845 
    846 	/*
    847 	 * Have to remove any existing mapping first.
    848 	 */
    849 	pmap_remove(pm, va, va + PAGE_SIZE);
    850 
    851 	if (flags & PMAP_WIRED)
    852 		flags |= prot;
    853 
    854 	managed = ((flags & PMAP_UNMANAGED) == 0) && uvm_pageismanaged(pa);
    855 
    856 	/*
    857 	 * Generate TTE.
    858 	 */
    859 	tte = TTE_PA(pa);
    860 	/* XXXX -- need to support multiple page sizes. */
    861 	tte |= TTE_SZ_16K;
    862 #ifdef	DIAGNOSTIC
    863 	if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) ==
    864 		(PME_NOCACHE | PME_WRITETHROUG))
    865 		panic("pmap_enter: uncached & writethrough");
    866 #endif
    867 	if (flags & PME_NOCACHE)
    868 		/* Must be I/O mapping */
    869 		tte |= TTE_I | TTE_G;
    870 #ifdef PPC_4XX_NOCACHE
    871 	tte |= TTE_I;
    872 #else
    873 	else if (flags & PME_WRITETHROUG)
    874 		/* Uncached and writethrough are not compatible */
    875 		tte |= TTE_W;
    876 #endif
    877 	if (pm == pmap_kernel())
    878 		tte |= TTE_ZONE(ZONE_PRIV);
    879 	else
    880 		tte |= TTE_ZONE(ZONE_USER);
    881 
    882 	if (flags & VM_PROT_WRITE)
    883 		tte |= TTE_WR;
    884 
    885 	if (flags & VM_PROT_EXECUTE)
    886 		tte |= TTE_EX;
    887 
    888 	/*
    889 	 * Now record mapping for later back-translation.
    890 	 */
    891 	if (pmap_initialized && managed) {
    892 		char *attr;
    893 
    894 		if (!pmap_enter_pv(pm, va, pa, flags)) {
    895 			/* Could not enter pv on a managed page */
    896 			return 1;
    897 		}
    898 
    899 		/* Now set attributes. */
    900 		attr = pa_to_attr(pa);
    901 #ifdef DIAGNOSTIC
    902 		if (!attr)
    903 			panic("managed but no attr");
    904 #endif
    905 		if (flags & VM_PROT_ALL)
    906 			*attr |= PMAP_ATTR_REF;
    907 		if (flags & VM_PROT_WRITE)
    908 			*attr |= PMAP_ATTR_CHG;
    909 	}
    910 
    911 	s = splvm();
    912 
    913 	/* Insert page into page table. */
    914 	pte_enter(pm, va, tte);
    915 
    916 	/* If this is a real fault, enter it in the tlb */
    917 	if (tte && ((flags & PMAP_WIRED) == 0)) {
    918 		ppc4xx_tlb_enter(pm->pm_ctx, va, tte);
    919 	}
    920 	splx(s);
    921 
    922 	/* Flush the real memory from the instruction cache. */
    923 	if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0)
    924 		__syncicache((void *)pa, PAGE_SIZE);
    925 
    926 	return 0;
    927 }
    928 
    929 void
    930 pmap_unwire(struct pmap *pm, vaddr_t va)
    931 {
    932 	struct pv_entry *pv;
    933 	paddr_t pa;
    934 	int s;
    935 
    936 	if (!pmap_extract(pm, va, &pa)) {
    937 		return;
    938 	}
    939 
    940 	pv = pa_to_pv(pa);
    941 	if (!pv)
    942 		return;
    943 
    944 	s = splvm();
    945 	while (pv != NULL) {
    946 		if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
    947 			if (PV_ISWIRED(pv)) {
    948 				PV_UNWIRE(pv);
    949 				pm->pm_stats.wired_count--;
    950 			}
    951 			break;
    952 		}
    953 		pv = pv->pv_next;
    954 	}
    955 	splx(s);
    956 }
    957 
    958 void
    959 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    960 {
    961 	int s;
    962 	u_int tte;
    963 	struct pmap *pm = pmap_kernel();
    964 
    965 	/*
    966 	 * Have to remove any existing mapping first.
    967 	 */
    968 
    969 	/*
    970 	 * Generate TTE.
    971 	 *
    972 	 * XXXX
    973 	 *
    974 	 * Since the kernel does not handle execution privileges properly,
    975 	 * we will handle read and execute permissions together.
    976 	 */
    977 	tte = 0;
    978 	if (prot & VM_PROT_ALL) {
    979 
    980 		tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV);
    981 		/* XXXX -- need to support multiple page sizes. */
    982 		tte |= TTE_SZ_16K;
    983 #ifdef DIAGNOSTIC
    984 		if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) ==
    985 			(PME_NOCACHE | PME_WRITETHROUG))
    986 			panic("pmap_kenter_pa: uncached & writethrough");
    987 #endif
    988 		if (prot & PME_NOCACHE)
    989 			/* Must be I/O mapping */
    990 			tte |= TTE_I | TTE_G;
    991 #ifdef PPC_4XX_NOCACHE
    992 		tte |= TTE_I;
    993 #else
    994 		else if (prot & PME_WRITETHROUG)
    995 			/* Uncached and writethrough are not compatible */
    996 			tte |= TTE_W;
    997 #endif
    998 		if (prot & VM_PROT_WRITE)
    999 			tte |= TTE_WR;
   1000 	}
   1001 
   1002 	s = splvm();
   1003 
   1004 	/* Insert page into page table. */
   1005 	pte_enter(pm, va, tte);
   1006 	splx(s);
   1007 }
   1008 
   1009 void
   1010 pmap_kremove(vaddr_t va, vsize_t len)
   1011 {
   1012 
   1013 	while (len > 0) {
   1014 		pte_enter(pmap_kernel(), va, 0);
   1015 		va += PAGE_SIZE;
   1016 		len -= PAGE_SIZE;
   1017 	}
   1018 }
   1019 
   1020 /*
   1021  * Remove the given range of mapping entries.
   1022  */
   1023 void
   1024 pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva)
   1025 {
   1026 	int s;
   1027 	paddr_t pa;
   1028 	volatile u_int *ptp;
   1029 
   1030 	s = splvm();
   1031 	while (va < endva) {
   1032 
   1033 		if ((ptp = pte_find(pm, va)) && (pa = *ptp)) {
   1034 			pa = TTE_PA(pa);
   1035 			pmap_remove_pv(pm, va, pa);
   1036 			*ptp = 0;
   1037 			ppc4xx_tlb_flush(va, pm->pm_ctx);
   1038 			pm->pm_stats.resident_count--;
   1039 		}
   1040 		va += PAGE_SIZE;
   1041 	}
   1042 
   1043 	splx(s);
   1044 }
   1045 
   1046 /*
   1047  * Get the physical page address for the given pmap/virtual address.
   1048  */
   1049 bool
   1050 pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap)
   1051 {
   1052 	int seg = STIDX(va);
   1053 	int ptn = PTIDX(va);
   1054 	u_int pa = 0;
   1055 	int s;
   1056 
   1057 	s = splvm();
   1058 	if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) {
   1059 		*pap = TTE_PA(pa) | (va & PGOFSET);
   1060 	}
   1061 	splx(s);
   1062 	return (pa != 0);
   1063 }
   1064 
   1065 /*
   1066  * Lower the protection on the specified range of this pmap.
   1067  *
   1068  * There are only two cases: either the protection is going to 0,
   1069  * or it is going to read-only.
   1070  */
   1071 void
   1072 pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   1073 {
   1074 	volatile u_int *ptp;
   1075 	int s, bic;
   1076 
   1077 	if ((prot & VM_PROT_READ) == 0) {
   1078 		pmap_remove(pm, sva, eva);
   1079 		return;
   1080 	}
   1081 	bic = 0;
   1082 	if ((prot & VM_PROT_WRITE) == 0) {
   1083 		bic |= TTE_WR;
   1084 	}
   1085 	if ((prot & VM_PROT_EXECUTE) == 0) {
   1086 		bic |= TTE_EX;
   1087 	}
   1088 	if (bic == 0) {
   1089 		return;
   1090 	}
   1091 	s = splvm();
   1092 	while (sva < eva) {
   1093 		if ((ptp = pte_find(pm, sva)) != NULL) {
   1094 			*ptp &= ~bic;
   1095 			ppc4xx_tlb_flush(sva, pm->pm_ctx);
   1096 		}
   1097 		sva += PAGE_SIZE;
   1098 	}
   1099 	splx(s);
   1100 }
   1101 
   1102 bool
   1103 pmap_check_attr(struct vm_page *pg, u_int mask, int clear)
   1104 {
   1105 	paddr_t pa;
   1106 	char *attr;
   1107 	int s, rv;
   1108 
   1109 	/*
   1110 	 * First modify bits in cache.
   1111 	 */
   1112 	pa = VM_PAGE_TO_PHYS(pg);
   1113 	attr = pa_to_attr(pa);
   1114 	if (attr == NULL)
   1115 		return false;
   1116 
   1117 	s = splvm();
   1118 	rv = ((*attr & mask) != 0);
   1119 	if (clear) {
   1120 		*attr &= ~mask;
   1121 		pmap_page_protect(pg, mask == PMAP_ATTR_CHG ? VM_PROT_READ : 0);
   1122 	}
   1123 	splx(s);
   1124 	return rv;
   1125 }
   1126 
   1127 
   1128 /*
   1129  * Lower the protection on the specified physical page.
   1130  *
   1131  * There are only two cases: either the protection is going to 0,
   1132  * or it is going to read-only.
   1133  */
   1134 void
   1135 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   1136 {
   1137 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1138 	vaddr_t va;
   1139 	struct pv_entry *pvh, *pv, *npv;
   1140 	struct pmap *pm;
   1141 
   1142 	pvh = pa_to_pv(pa);
   1143 	if (pvh == NULL)
   1144 		return;
   1145 
   1146 	/* Handle extra pvs which may be deleted in the operation */
   1147 	for (pv = pvh->pv_next; pv; pv = npv) {
   1148 		npv = pv->pv_next;
   1149 
   1150 		pm = pv->pv_pm;
   1151 		va = pv->pv_va;
   1152 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1153 	}
   1154 	/* Now check the head pv */
   1155 	if (pvh->pv_pm) {
   1156 		pv = pvh;
   1157 		pm = pv->pv_pm;
   1158 		va = pv->pv_va;
   1159 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1160 	}
   1161 }
   1162 
   1163 /*
   1164  * Activate the address space for the specified process.  If the process
   1165  * is the current process, load the new MMU context.
   1166  */
   1167 void
   1168 pmap_activate(struct lwp *l)
   1169 {
   1170 #if 0
   1171 	struct pcb *pcb = &l->l_proc->p_addr->u_pcb;
   1172 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
   1173 
   1174 	/*
   1175 	 * XXX Normally performed in cpu_lwp_fork().
   1176 	 */
   1177 	printf("pmap_activate(%p), pmap=%p\n",l,pmap);
   1178 	pcb->pcb_pm = pmap;
   1179 #endif
   1180 }
   1181 
   1182 /*
   1183  * Deactivate the specified process's address space.
   1184  */
   1185 void
   1186 pmap_deactivate(struct lwp *l)
   1187 {
   1188 }
   1189 
   1190 /*
   1191  * Synchronize caches corresponding to [addr, addr+len) in p.
   1192  */
   1193 void
   1194 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
   1195 {
   1196 	struct pmap *pm = p->p_vmspace->vm_map.pmap;
   1197 	int msr, ctx, opid, step;
   1198 
   1199 	step = CACHELINESIZE;
   1200 
   1201 	/*
   1202 	 * Need to turn off IMMU and switch to user context.
   1203 	 * (icbi uses DMMU).
   1204 	 */
   1205 	if (!(ctx = pm->pm_ctx)) {
   1206 		/* No context -- assign it one */
   1207 		ctx_alloc(pm);
   1208 		ctx = pm->pm_ctx;
   1209 	}
   1210 	__asm volatile("mfmsr %0;"
   1211 		"li %1, %7;"
   1212 		"andc %1,%0,%1;"
   1213 		"mtmsr %1;"
   1214 		"sync;isync;"
   1215 		"mfpid %1;"
   1216 		"mtpid %2;"
   1217 		"sync; isync;"
   1218 		"1:"
   1219 		"dcbf 0,%3;"
   1220 		"icbi 0,%3;"
   1221 		"add %3,%3,%5;"
   1222 		"addc. %4,%4,%6;"
   1223 		"bge 1b;"
   1224 		"mtpid %1;"
   1225 		"mtmsr %0;"
   1226 		"sync; isync"
   1227 		: "=&r" (msr), "=&r" (opid)
   1228 		: "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step),
   1229 		  "K" (PSL_IR | PSL_DR));
   1230 }
   1231 
   1232 
   1233 /* This has to be done in real mode !!! */
   1234 void
   1235 ppc4xx_tlb_flush(vaddr_t va, int pid)
   1236 {
   1237 	u_long i, found;
   1238 	u_long msr;
   1239 
   1240 	/* If there's no context then it can't be mapped. */
   1241 	if (!pid)
   1242 		return;
   1243 
   1244 	__asm( 	"mfpid %1;"		/* Save PID */
   1245 		"mfmsr %2;"		/* Save MSR */
   1246 		"li %0,0;"		/* Now clear MSR */
   1247 		"mtmsr %0;"
   1248 		"mtpid %4;"		/* Set PID */
   1249 		"sync;"
   1250 		"tlbsx. %0,0,%3;"	/* Search TLB */
   1251 		"sync;"
   1252 		"mtpid %1;"		/* Restore PID */
   1253 		"mtmsr %2;"		/* Restore MSR */
   1254 		"sync;isync;"
   1255 		"li %1,1;"
   1256 		"beq 1f;"
   1257 		"li %1,0;"
   1258 		"1:"
   1259 		: "=&r" (i), "=&r" (found), "=&r" (msr)
   1260 		: "r" (va), "r" (pid));
   1261 	if (found && !TLB_LOCKED(i)) {
   1262 
   1263 		/* Now flush translation */
   1264 		__asm volatile(
   1265 			"tlbwe %0,%1,0;"
   1266 			"sync;isync;"
   1267 			: : "r" (0), "r" (i));
   1268 
   1269 		tlb_info[i].ti_ctx = 0;
   1270 		tlb_info[i].ti_flags = 0;
   1271 		tlbnext = i;
   1272 		/* Successful flushes */
   1273 		tlbflush_ev.ev_count++;
   1274 	}
   1275 }
   1276 
   1277 void
   1278 ppc4xx_tlb_flush_all(void)
   1279 {
   1280 	u_long i;
   1281 
   1282 	for (i = 0; i < NTLB; i++)
   1283 		if (!TLB_LOCKED(i)) {
   1284 			__asm volatile(
   1285 				"tlbwe %0,%1,0;"
   1286 				"sync;isync;"
   1287 				: : "r" (0), "r" (i));
   1288 			tlb_info[i].ti_ctx = 0;
   1289 			tlb_info[i].ti_flags = 0;
   1290 		}
   1291 
   1292 	__asm volatile("sync;isync");
   1293 }
   1294 
   1295 /* Find a TLB entry to evict. */
   1296 static int
   1297 ppc4xx_tlb_find_victim(void)
   1298 {
   1299 	int flags;
   1300 
   1301 	for (;;) {
   1302 		if (++tlbnext >= NTLB)
   1303 			tlbnext = tlb_nreserved;
   1304 		flags = tlb_info[tlbnext].ti_flags;
   1305 		if (!(flags & TLBF_USED) ||
   1306 			(flags & (TLBF_LOCKED | TLBF_REF)) == 0) {
   1307 			u_long va, stack = (u_long)&va;
   1308 
   1309 			if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) &&
   1310 			    (tlb_info[tlbnext].ti_ctx == KERNEL_PID) &&
   1311 			     (flags & TLBF_USED)) {
   1312 				/* Kernel stack page */
   1313 				flags |= TLBF_USED;
   1314 				tlb_info[tlbnext].ti_flags = flags;
   1315 			} else {
   1316 				/* Found it! */
   1317 				return (tlbnext);
   1318 			}
   1319 		} else {
   1320 			tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF);
   1321 		}
   1322 	}
   1323 }
   1324 
   1325 void
   1326 ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte)
   1327 {
   1328 	u_long th, tl, idx;
   1329 	tlbpid_t pid;
   1330 	u_short msr;
   1331 	paddr_t pa;
   1332 	int s, sz;
   1333 
   1334 	tlbenter_ev.ev_count++;
   1335 
   1336 	sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT;
   1337 	pa = (pte & TTE_RPN_MASK(sz));
   1338 	th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID;
   1339 	tl = (pte & ~TLB_RPN_MASK) | pa;
   1340 	tl |= ppc4xx_tlbflags(va, pa);
   1341 
   1342 	s = splhigh();
   1343 	idx = ppc4xx_tlb_find_victim();
   1344 
   1345 #ifdef DIAGNOSTIC
   1346 	if ((idx < tlb_nreserved) || (idx >= NTLB)) {
   1347 		panic("ppc4xx_tlb_enter: replacing entry %ld", idx);
   1348 	}
   1349 #endif
   1350 
   1351 	tlb_info[idx].ti_va = (va & TLB_EPN_MASK);
   1352 	tlb_info[idx].ti_ctx = ctx;
   1353 	tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF;
   1354 
   1355 	__asm volatile(
   1356 		"mfmsr %0;"			/* Save MSR */
   1357 		"li %1,0;"
   1358 		"tlbwe %1,%3,0;"		/* Invalidate old entry. */
   1359 		"mtmsr %1;"			/* Clear MSR */
   1360 		"mfpid %1;"			/* Save old PID */
   1361 		"mtpid %2;"			/* Load translation ctx */
   1362 		"sync; isync;"
   1363 #ifdef DEBUG
   1364 		"andi. %3,%3,63;"
   1365 		"tweqi %3,0;" 			/* XXXXX DEBUG trap on index 0 */
   1366 #endif
   1367 		"tlbwe %4,%3,1; tlbwe %5,%3,0;"	/* Set TLB */
   1368 		"sync; isync;"
   1369 		"mtpid %1; mtmsr %0;"		/* Restore PID and MSR */
   1370 		"sync; isync;"
   1371 	: "=&r" (msr), "=&r" (pid)
   1372 	: "r" (ctx), "r" (idx), "r" (tl), "r" (th));
   1373 	splx(s);
   1374 }
   1375 
   1376 void
   1377 ppc4xx_tlb_init(void)
   1378 {
   1379 	int i;
   1380 
   1381 	/* Mark reserved TLB entries */
   1382 	for (i = 0; i < tlb_nreserved; i++) {
   1383 		tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED;
   1384 		tlb_info[i].ti_ctx = KERNEL_PID;
   1385 	}
   1386 
   1387 	/* Setup security zones */
   1388 	/* Z0 - accessible by kernel only if TLB entry permissions allow
   1389 	 * Z1,Z2 - access is controlled by TLB entry permissions
   1390 	 * Z3 - full access regardless of TLB entry permissions
   1391 	 */
   1392 
   1393 	__asm volatile(
   1394 		"mtspr %0,%1;"
   1395 		"sync;"
   1396 		::  "K"(SPR_ZPR), "r" (0x1b000000));
   1397 }
   1398 
   1399 /*
   1400  * ppc4xx_tlb_size_mask:
   1401  *
   1402  * 	Roundup size to supported page size, return TLBHI mask and real size.
   1403  */
   1404 static int
   1405 ppc4xx_tlb_size_mask(size_t size, int *mask, int *rsiz)
   1406 {
   1407 	int 			i;
   1408 
   1409 	for (i = 0; i < __arraycount(tlbsize); i++)
   1410 		if (size <= tlbsize[i]) {
   1411 			*mask = (i << TLB_SIZE_SHFT);
   1412 			*rsiz = tlbsize[i];
   1413 			return (0);
   1414 		}
   1415 	return (EINVAL);
   1416 }
   1417 
   1418 /*
   1419  * ppc4xx_tlb_mapiodev:
   1420  *
   1421  * 	Lookup virtual address of mapping previously entered via
   1422  * 	ppc4xx_tlb_reserve. Search TLB directly so that we don't
   1423  * 	need to waste extra storage for reserved mappings. Note
   1424  * 	that reading TLBHI also sets PID, but all reserved mappings
   1425  * 	use KERNEL_PID, so the side effect is nil.
   1426  */
   1427 void *
   1428 ppc4xx_tlb_mapiodev(paddr_t base, psize_t len)
   1429 {
   1430 	paddr_t 		pa;
   1431 	vaddr_t 		va;
   1432 	u_int 			lo, hi, sz;
   1433 	int 			i;
   1434 
   1435 	/* tlb_nreserved is only allowed to grow, so this is safe. */
   1436 	for (i = 0; i < tlb_nreserved; i++) {
   1437 		__asm volatile (
   1438 		    "	tlbre %0,%2,1 	\n" 	/* TLBLO */
   1439 		    "	tlbre %1,%2,0 	\n" 	/* TLBHI */
   1440 		    : "=&r" (lo), "=&r" (hi)
   1441 		    : "r" (i));
   1442 
   1443 		KASSERT(hi & TLB_VALID);
   1444 		KASSERT(mfspr(SPR_PID) == KERNEL_PID);
   1445 
   1446 		pa = (lo & TLB_RPN_MASK);
   1447 		if (base < pa)
   1448 			continue;
   1449 
   1450 		sz = tlbsize[(hi & TLB_SIZE_MASK) >> TLB_SIZE_SHFT];
   1451 		if ((base + len) > (pa + sz))
   1452 			continue;
   1453 
   1454 		va = (hi & TLB_EPN_MASK) + (base & (sz - 1)); 	/* sz = 2^n */
   1455 		return (void *)(va);
   1456 	}
   1457 
   1458 	return (NULL);
   1459 }
   1460 
   1461 /*
   1462  * ppc4xx_tlb_reserve:
   1463  *
   1464  * 	Map physical range to kernel virtual chunk via reserved TLB entry.
   1465  */
   1466 void
   1467 ppc4xx_tlb_reserve(paddr_t pa, vaddr_t va, size_t size, int flags)
   1468 {
   1469 	u_int 			lo, hi;
   1470 	int 			szmask, rsize;
   1471 
   1472 	/* Called before pmap_bootstrap(), va outside kernel space. */
   1473 	KASSERT(va < VM_MIN_KERNEL_ADDRESS || va >= VM_MAX_KERNEL_ADDRESS);
   1474 	KASSERT(! pmap_bootstrap_done);
   1475 	KASSERT(tlb_nreserved < NTLB);
   1476 
   1477 	/* Resolve size. */
   1478 	if (ppc4xx_tlb_size_mask(size, &szmask, &rsize) != 0)
   1479 		panic("ppc4xx_tlb_reserve: entry %d, %zuB too large",
   1480 		    size, tlb_nreserved);
   1481 
   1482 	/* Real size will be power of two >= 1024, so this is OK. */
   1483 	pa &= ~(rsize - 1); 	/* RPN */
   1484 	va &= ~(rsize - 1); 	/* EPN */
   1485 
   1486 	lo = pa | TLB_WR | flags;
   1487 	hi = va | TLB_VALID | szmask;
   1488 
   1489 #ifdef PPC_4XX_NOCACHE
   1490 	lo |= TLB_I;
   1491 #endif
   1492 
   1493 	__asm volatile(
   1494 	    "	tlbwe %1,%0,1 	\n" 	/* write TLBLO */
   1495 	    "	tlbwe %2,%0,0 	\n" 	/* write TLBHI */
   1496 	    "   sync 		\n"
   1497 	    "	isync 		\n"
   1498 	    : : "r" (tlb_nreserved), "r" (lo), "r" (hi));
   1499 
   1500 	tlb_nreserved++;
   1501 }
   1502 
   1503 /*
   1504  * We should pass the ctx in from trap code.
   1505  */
   1506 int
   1507 pmap_tlbmiss(vaddr_t va, int ctx)
   1508 {
   1509 	volatile u_int *pte;
   1510 	u_long tte;
   1511 
   1512 	tlbmiss_ev.ev_count++;
   1513 
   1514 	/*
   1515 	 * We will reserve 0 upto VM_MIN_KERNEL_ADDRESS for va == pa mappings.
   1516 	 * Physical RAM is expected to live in this range, care must be taken
   1517 	 * to not clobber 0 upto ${physmem} with device mappings in machdep
   1518 	 * code.
   1519 	 */
   1520 	if (ctx != KERNEL_PID || va >= VM_MIN_KERNEL_ADDRESS) {
   1521 		pte = pte_find((struct pmap *)__UNVOLATILE(ctxbusy[ctx]), va);
   1522 		if (pte == NULL) {
   1523 			/* Map unmanaged addresses directly for kernel access */
   1524 			return 1;
   1525 		}
   1526 		tte = *pte;
   1527 		if (tte == 0) {
   1528 			return 1;
   1529 		}
   1530 	} else {
   1531 		/* Create a 16MB writable mapping. */
   1532 #ifdef PPC_4XX_NOCACHE
   1533 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I |TTE_WR;
   1534 #else
   1535 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR;
   1536 #endif
   1537 	}
   1538 	tlbhit_ev.ev_count++;
   1539 	ppc4xx_tlb_enter(ctx, va, tte);
   1540 
   1541 	return 0;
   1542 }
   1543 
   1544 /*
   1545  * Flush all the entries matching a context from the TLB.
   1546  */
   1547 static int
   1548 ctx_flush(int cnum)
   1549 {
   1550 	int i;
   1551 
   1552 	/* We gotta steal this context */
   1553 	for (i = tlb_nreserved; i < NTLB; i++) {
   1554 		if (tlb_info[i].ti_ctx == cnum) {
   1555 			/* Can't steal ctx if it has a locked entry. */
   1556 			if (TLB_LOCKED(i)) {
   1557 #ifdef DIAGNOSTIC
   1558 				printf("ctx_flush: can't invalidate "
   1559 					"locked mapping %d "
   1560 					"for context %d\n", i, cnum);
   1561 #ifdef DDB
   1562 				Debugger();
   1563 #endif
   1564 #endif
   1565 				return (1);
   1566 			}
   1567 #ifdef DIAGNOSTIC
   1568 			if (i < tlb_nreserved)
   1569 				panic("TLB entry %d not locked", i);
   1570 #endif
   1571 			/* Invalidate particular TLB entry regardless of locked status */
   1572 			__asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i));
   1573 			tlb_info[i].ti_flags = 0;
   1574 		}
   1575 	}
   1576 	return (0);
   1577 }
   1578 
   1579 /*
   1580  * Allocate a context.  If necessary, steal one from someone else.
   1581  *
   1582  * The new context is flushed from the TLB before returning.
   1583  */
   1584 int
   1585 ctx_alloc(struct pmap *pm)
   1586 {
   1587 	int s, cnum;
   1588 	static int next = MINCTX;
   1589 
   1590 	if (pm == pmap_kernel()) {
   1591 #ifdef DIAGNOSTIC
   1592 		printf("ctx_alloc: kernel pmap!\n");
   1593 #endif
   1594 		return (0);
   1595 	}
   1596 	s = splvm();
   1597 
   1598 	/* Find a likely context. */
   1599 	cnum = next;
   1600 	do {
   1601 		if ((++cnum) > NUMCTX)
   1602 			cnum = MINCTX;
   1603 	} while (ctxbusy[cnum] != NULL && cnum != next);
   1604 
   1605 	/* Now clean it out */
   1606 oops:
   1607 	if (cnum < MINCTX)
   1608 		cnum = MINCTX; /* Never steal ctx 0 or 1 */
   1609 	if (ctx_flush(cnum)) {
   1610 		/* oops -- something's wired. */
   1611 		if ((++cnum) > NUMCTX)
   1612 			cnum = MINCTX;
   1613 		goto oops;
   1614 	}
   1615 
   1616 	if (ctxbusy[cnum]) {
   1617 #ifdef DEBUG
   1618 		/* We should identify this pmap and clear it */
   1619 		printf("Warning: stealing context %d\n", cnum);
   1620 #endif
   1621 		ctxbusy[cnum]->pm_ctx = 0;
   1622 	}
   1623 	ctxbusy[cnum] = pm;
   1624 	next = cnum;
   1625 	splx(s);
   1626 	pm->pm_ctx = cnum;
   1627 
   1628 	return cnum;
   1629 }
   1630 
   1631 /*
   1632  * Give away a context.
   1633  */
   1634 void
   1635 ctx_free(struct pmap *pm)
   1636 {
   1637 	int oldctx;
   1638 
   1639 	oldctx = pm->pm_ctx;
   1640 
   1641 	if (oldctx == 0)
   1642 		panic("ctx_free: freeing kernel context");
   1643 #ifdef DIAGNOSTIC
   1644 	if (ctxbusy[oldctx] == 0)
   1645 		printf("ctx_free: freeing free context %d\n", oldctx);
   1646 	if (ctxbusy[oldctx] != pm) {
   1647 		printf("ctx_free: freeing someone esle's context\n "
   1648 		       "ctxbusy[%d] = %p, pm->pm_ctx = %p\n",
   1649 		       oldctx, (void *)(u_long)ctxbusy[oldctx], pm);
   1650 #ifdef DDB
   1651 		Debugger();
   1652 #endif
   1653 	}
   1654 #endif
   1655 	/* We should verify it has not been stolen and reallocated... */
   1656 	ctxbusy[oldctx] = NULL;
   1657 	ctx_flush(oldctx);
   1658 }
   1659 
   1660 
   1661 #ifdef DEBUG
   1662 /*
   1663  * Test ref/modify handling.
   1664  */
   1665 void pmap_testout(void);
   1666 void
   1667 pmap_testout(void)
   1668 {
   1669 	vaddr_t va;
   1670 	volatile int *loc;
   1671 	int val = 0;
   1672 	paddr_t pa;
   1673 	struct vm_page *pg;
   1674 	int ref, mod;
   1675 
   1676 	/* Allocate a page */
   1677 	va = (vaddr_t)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   1678 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
   1679 	loc = (int*)va;
   1680 
   1681 	pmap_extract(pmap_kernel(), va, &pa);
   1682 	pg = PHYS_TO_VM_PAGE(pa);
   1683 	pmap_unwire(pmap_kernel(), va);
   1684 
   1685 	pmap_kremove(va, PAGE_SIZE);
   1686 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1687 	pmap_update(pmap_kernel());
   1688 
   1689 	/* Now clear reference and modify */
   1690 	ref = pmap_clear_reference(pg);
   1691 	mod = pmap_clear_modify(pg);
   1692 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1693 	       (void *)(u_long)va, (long)pa,
   1694 	       ref, mod);
   1695 
   1696 	/* Check it's properly cleared */
   1697 	ref = pmap_is_referenced(pg);
   1698 	mod = pmap_is_modified(pg);
   1699 	printf("Checking cleared page: ref %d, mod %d\n",
   1700 	       ref, mod);
   1701 
   1702 	/* Reference page */
   1703 	val = *loc;
   1704 
   1705 	ref = pmap_is_referenced(pg);
   1706 	mod = pmap_is_modified(pg);
   1707 	printf("Referenced page: ref %d, mod %d val %x\n",
   1708 	       ref, mod, val);
   1709 
   1710 	/* Now clear reference and modify */
   1711 	ref = pmap_clear_reference(pg);
   1712 	mod = pmap_clear_modify(pg);
   1713 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1714 	       (void *)(u_long)va, (long)pa,
   1715 	       ref, mod);
   1716 
   1717 	/* Modify page */
   1718 	*loc = 1;
   1719 
   1720 	ref = pmap_is_referenced(pg);
   1721 	mod = pmap_is_modified(pg);
   1722 	printf("Modified page: ref %d, mod %d\n",
   1723 	       ref, mod);
   1724 
   1725 	/* Now clear reference and modify */
   1726 	ref = pmap_clear_reference(pg);
   1727 	mod = pmap_clear_modify(pg);
   1728 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1729 	       (void *)(u_long)va, (long)pa,
   1730 	       ref, mod);
   1731 
   1732 	/* Check it's properly cleared */
   1733 	ref = pmap_is_referenced(pg);
   1734 	mod = pmap_is_modified(pg);
   1735 	printf("Checking cleared page: ref %d, mod %d\n",
   1736 	       ref, mod);
   1737 
   1738 	/* Modify page */
   1739 	*loc = 1;
   1740 
   1741 	ref = pmap_is_referenced(pg);
   1742 	mod = pmap_is_modified(pg);
   1743 	printf("Modified page: ref %d, mod %d\n",
   1744 	       ref, mod);
   1745 
   1746 	/* Check pmap_protect() */
   1747 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ);
   1748 	pmap_update(pmap_kernel());
   1749 	ref = pmap_is_referenced(pg);
   1750 	mod = pmap_is_modified(pg);
   1751 	printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n",
   1752 	       ref, mod);
   1753 
   1754 	/* Now clear reference and modify */
   1755 	ref = pmap_clear_reference(pg);
   1756 	mod = pmap_clear_modify(pg);
   1757 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1758 	       (void *)(u_long)va, (long)pa,
   1759 	       ref, mod);
   1760 
   1761 	/* Reference page */
   1762 	val = *loc;
   1763 
   1764 	ref = pmap_is_referenced(pg);
   1765 	mod = pmap_is_modified(pg);
   1766 	printf("Referenced page: ref %d, mod %d val %x\n",
   1767 	       ref, mod, val);
   1768 
   1769 	/* Now clear reference and modify */
   1770 	ref = pmap_clear_reference(pg);
   1771 	mod = pmap_clear_modify(pg);
   1772 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1773 	       (void *)(u_long)va, (long)pa,
   1774 	       ref, mod);
   1775 
   1776 	/* Modify page */
   1777 #if 0
   1778 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1779 	pmap_update(pmap_kernel());
   1780 #endif
   1781 	*loc = 1;
   1782 
   1783 	ref = pmap_is_referenced(pg);
   1784 	mod = pmap_is_modified(pg);
   1785 	printf("Modified page: ref %d, mod %d\n",
   1786 	       ref, mod);
   1787 
   1788 	/* Check pmap_protect() */
   1789 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE);
   1790 	pmap_update(pmap_kernel());
   1791 	ref = pmap_is_referenced(pg);
   1792 	mod = pmap_is_modified(pg);
   1793 	printf("pmap_protect(): ref %d, mod %d\n",
   1794 	       ref, mod);
   1795 
   1796 	/* Now clear reference and modify */
   1797 	ref = pmap_clear_reference(pg);
   1798 	mod = pmap_clear_modify(pg);
   1799 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1800 	       (void *)(u_long)va, (long)pa,
   1801 	       ref, mod);
   1802 
   1803 	/* Reference page */
   1804 	val = *loc;
   1805 
   1806 	ref = pmap_is_referenced(pg);
   1807 	mod = pmap_is_modified(pg);
   1808 	printf("Referenced page: ref %d, mod %d val %x\n",
   1809 	       ref, mod, val);
   1810 
   1811 	/* Now clear reference and modify */
   1812 	ref = pmap_clear_reference(pg);
   1813 	mod = pmap_clear_modify(pg);
   1814 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1815 	       (void *)(u_long)va, (long)pa,
   1816 	       ref, mod);
   1817 
   1818 	/* Modify page */
   1819 #if 0
   1820 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1821 	pmap_update(pmap_kernel());
   1822 #endif
   1823 	*loc = 1;
   1824 
   1825 	ref = pmap_is_referenced(pg);
   1826 	mod = pmap_is_modified(pg);
   1827 	printf("Modified page: ref %d, mod %d\n",
   1828 	       ref, mod);
   1829 
   1830 	/* Check pmap_pag_protect() */
   1831 	pmap_page_protect(pg, VM_PROT_READ);
   1832 	ref = pmap_is_referenced(pg);
   1833 	mod = pmap_is_modified(pg);
   1834 	printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n",
   1835 	       ref, mod);
   1836 
   1837 	/* Now clear reference and modify */
   1838 	ref = pmap_clear_reference(pg);
   1839 	mod = pmap_clear_modify(pg);
   1840 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1841 	       (void *)(u_long)va, (long)pa,
   1842 	       ref, mod);
   1843 
   1844 	/* Reference page */
   1845 	val = *loc;
   1846 
   1847 	ref = pmap_is_referenced(pg);
   1848 	mod = pmap_is_modified(pg);
   1849 	printf("Referenced page: ref %d, mod %d val %x\n",
   1850 	       ref, mod, val);
   1851 
   1852 	/* Now clear reference and modify */
   1853 	ref = pmap_clear_reference(pg);
   1854 	mod = pmap_clear_modify(pg);
   1855 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1856 	       (void *)(u_long)va, (long)pa,
   1857 	       ref, mod);
   1858 
   1859 	/* Modify page */
   1860 #if 0
   1861 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1862 	pmap_update(pmap_kernel());
   1863 #endif
   1864 	*loc = 1;
   1865 
   1866 	ref = pmap_is_referenced(pg);
   1867 	mod = pmap_is_modified(pg);
   1868 	printf("Modified page: ref %d, mod %d\n",
   1869 	       ref, mod);
   1870 
   1871 	/* Check pmap_pag_protect() */
   1872 	pmap_page_protect(pg, VM_PROT_NONE);
   1873 	ref = pmap_is_referenced(pg);
   1874 	mod = pmap_is_modified(pg);
   1875 	printf("pmap_page_protect(): ref %d, mod %d\n",
   1876 	       ref, mod);
   1877 
   1878 	/* Now clear reference and modify */
   1879 	ref = pmap_clear_reference(pg);
   1880 	mod = pmap_clear_modify(pg);
   1881 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1882 	       (void *)(u_long)va, (long)pa,
   1883 	       ref, mod);
   1884 
   1885 
   1886 	/* Reference page */
   1887 	val = *loc;
   1888 
   1889 	ref = pmap_is_referenced(pg);
   1890 	mod = pmap_is_modified(pg);
   1891 	printf("Referenced page: ref %d, mod %d val %x\n",
   1892 	       ref, mod, val);
   1893 
   1894 	/* Now clear reference and modify */
   1895 	ref = pmap_clear_reference(pg);
   1896 	mod = pmap_clear_modify(pg);
   1897 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1898 	       (void *)(u_long)va, (long)pa,
   1899 	       ref, mod);
   1900 
   1901 	/* Modify page */
   1902 #if 0
   1903 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1904 	pmap_update(pmap_kernel());
   1905 #endif
   1906 	*loc = 1;
   1907 
   1908 	ref = pmap_is_referenced(pg);
   1909 	mod = pmap_is_modified(pg);
   1910 	printf("Modified page: ref %d, mod %d\n",
   1911 	       ref, mod);
   1912 
   1913 	/* Unmap page */
   1914 	pmap_remove(pmap_kernel(), va, va+1);
   1915 	pmap_update(pmap_kernel());
   1916 	ref = pmap_is_referenced(pg);
   1917 	mod = pmap_is_modified(pg);
   1918 	printf("Unmapped page: ref %d, mod %d\n", ref, mod);
   1919 
   1920 	/* Now clear reference and modify */
   1921 	ref = pmap_clear_reference(pg);
   1922 	mod = pmap_clear_modify(pg);
   1923 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1924 	       (void *)(u_long)va, (long)pa, ref, mod);
   1925 
   1926 	/* Check it's properly cleared */
   1927 	ref = pmap_is_referenced(pg);
   1928 	mod = pmap_is_modified(pg);
   1929 	printf("Checking cleared page: ref %d, mod %d\n",
   1930 	       ref, mod);
   1931 
   1932 	pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
   1933 	pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
   1934 	uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE, UVM_KMF_WIRED);
   1935 }
   1936 #endif
   1937