Home | History | Annotate | Line # | Download | only in ibm4xx
pmap.c revision 1.60.2.8
      1 /*	$NetBSD: pmap.c,v 1.60.2.8 2010/11/04 08:47:37 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.8 2010/11/04 08:47:37 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_NOCACHE) == 0) &&
    855 	    (uvm_pageismanaged(pa) || uvm_pageismanaged_device(pa));
    856 
    857 	/*
    858 	 * Generate TTE.
    859 	 */
    860 	tte = TTE_PA(pa);
    861 	/* XXXX -- need to support multiple page sizes. */
    862 	tte |= TTE_SZ_16K;
    863 #ifdef	DIAGNOSTIC
    864 	if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) ==
    865 		(PME_NOCACHE | PME_WRITETHROUG))
    866 		panic("pmap_enter: uncached & writethrough");
    867 #endif
    868 	if (flags & PME_NOCACHE)
    869 		/* Must be I/O mapping */
    870 		tte |= TTE_I | TTE_G;
    871 #ifdef PPC_4XX_NOCACHE
    872 	tte |= TTE_I;
    873 #else
    874 	else if (flags & PME_WRITETHROUG)
    875 		/* Uncached and writethrough are not compatible */
    876 		tte |= TTE_W;
    877 #endif
    878 	if (pm == pmap_kernel())
    879 		tte |= TTE_ZONE(ZONE_PRIV);
    880 	else
    881 		tte |= TTE_ZONE(ZONE_USER);
    882 
    883 	if (flags & VM_PROT_WRITE)
    884 		tte |= TTE_WR;
    885 
    886 	if (flags & VM_PROT_EXECUTE)
    887 		tte |= TTE_EX;
    888 
    889 	/*
    890 	 * Now record mapping for later back-translation.
    891 	 */
    892 	if (pmap_initialized && managed) {
    893 		char *attr;
    894 
    895 		if (!pmap_enter_pv(pm, va, pa, flags)) {
    896 			/* Could not enter pv on a managed page */
    897 			return 1;
    898 		}
    899 
    900 		/* Now set attributes. */
    901 		attr = pa_to_attr(pa);
    902 #ifdef DIAGNOSTIC
    903 		if (!attr)
    904 			panic("managed but no attr");
    905 #endif
    906 		if (flags & VM_PROT_ALL)
    907 			*attr |= PMAP_ATTR_REF;
    908 		if (flags & VM_PROT_WRITE)
    909 			*attr |= PMAP_ATTR_CHG;
    910 	}
    911 
    912 	s = splvm();
    913 
    914 	/* Insert page into page table. */
    915 	pte_enter(pm, va, tte);
    916 
    917 	/* If this is a real fault, enter it in the tlb */
    918 	if (tte && ((flags & PMAP_WIRED) == 0)) {
    919 		ppc4xx_tlb_enter(pm->pm_ctx, va, tte);
    920 	}
    921 	splx(s);
    922 
    923 	/* Flush the real memory from the instruction cache. */
    924 	if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0)
    925 		__syncicache((void *)pa, PAGE_SIZE);
    926 
    927 	return 0;
    928 }
    929 
    930 void
    931 pmap_unwire(struct pmap *pm, vaddr_t va)
    932 {
    933 	struct pv_entry *pv;
    934 	paddr_t pa;
    935 	int s;
    936 
    937 	if (!pmap_extract(pm, va, &pa)) {
    938 		return;
    939 	}
    940 
    941 	pv = pa_to_pv(pa);
    942 	if (!pv)
    943 		return;
    944 
    945 	s = splvm();
    946 	while (pv != NULL) {
    947 		if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
    948 			if (PV_ISWIRED(pv)) {
    949 				PV_UNWIRE(pv);
    950 				pm->pm_stats.wired_count--;
    951 			}
    952 			break;
    953 		}
    954 		pv = pv->pv_next;
    955 	}
    956 	splx(s);
    957 }
    958 
    959 void
    960 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    961 {
    962 	int s;
    963 	u_int tte;
    964 	struct pmap *pm = pmap_kernel();
    965 
    966 	/*
    967 	 * Have to remove any existing mapping first.
    968 	 */
    969 
    970 	/*
    971 	 * Generate TTE.
    972 	 *
    973 	 * XXXX
    974 	 *
    975 	 * Since the kernel does not handle execution privileges properly,
    976 	 * we will handle read and execute permissions together.
    977 	 */
    978 	tte = 0;
    979 	if (prot & VM_PROT_ALL) {
    980 
    981 		tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV);
    982 		/* XXXX -- need to support multiple page sizes. */
    983 		tte |= TTE_SZ_16K;
    984 #ifdef DIAGNOSTIC
    985 		if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) ==
    986 			(PME_NOCACHE | PME_WRITETHROUG))
    987 			panic("pmap_kenter_pa: uncached & writethrough");
    988 #endif
    989 		if (prot & PME_NOCACHE)
    990 			/* Must be I/O mapping */
    991 			tte |= TTE_I | TTE_G;
    992 #ifdef PPC_4XX_NOCACHE
    993 		tte |= TTE_I;
    994 #else
    995 		else if (prot & PME_WRITETHROUG)
    996 			/* Uncached and writethrough are not compatible */
    997 			tte |= TTE_W;
    998 #endif
    999 		if (prot & VM_PROT_WRITE)
   1000 			tte |= TTE_WR;
   1001 	}
   1002 
   1003 	s = splvm();
   1004 
   1005 	/* Insert page into page table. */
   1006 	pte_enter(pm, va, tte);
   1007 	splx(s);
   1008 }
   1009 
   1010 void
   1011 pmap_kremove(vaddr_t va, vsize_t len)
   1012 {
   1013 
   1014 	while (len > 0) {
   1015 		pte_enter(pmap_kernel(), va, 0);
   1016 		va += PAGE_SIZE;
   1017 		len -= PAGE_SIZE;
   1018 	}
   1019 }
   1020 
   1021 /*
   1022  * Remove the given range of mapping entries.
   1023  */
   1024 void
   1025 pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva)
   1026 {
   1027 	int s;
   1028 	paddr_t pa;
   1029 	volatile u_int *ptp;
   1030 
   1031 	s = splvm();
   1032 	while (va < endva) {
   1033 
   1034 		if ((ptp = pte_find(pm, va)) && (pa = *ptp)) {
   1035 			pa = TTE_PA(pa);
   1036 			pmap_remove_pv(pm, va, pa);
   1037 			*ptp = 0;
   1038 			ppc4xx_tlb_flush(va, pm->pm_ctx);
   1039 			pm->pm_stats.resident_count--;
   1040 		}
   1041 		va += PAGE_SIZE;
   1042 	}
   1043 
   1044 	splx(s);
   1045 }
   1046 
   1047 /*
   1048  * Convert the given kernel virtual address to the page frame
   1049  * number (mmap cookie).
   1050  */
   1051 paddr_t
   1052 pmap_mmap(vaddr_t addr, off_t off)
   1053 {
   1054 
   1055 	return trunc_page((paddr_t)addr + off);
   1056 }
   1057 
   1058 /*
   1059  * Get the physical page address for the given pmap/virtual address.
   1060  */
   1061 bool
   1062 pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap)
   1063 {
   1064 	int seg = STIDX(va);
   1065 	int ptn = PTIDX(va);
   1066 	u_int pa = 0;
   1067 	int s;
   1068 
   1069 	s = splvm();
   1070 	if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) {
   1071 		*pap = TTE_PA(pa) | (va & PGOFSET);
   1072 	}
   1073 	splx(s);
   1074 	return (pa != 0);
   1075 }
   1076 
   1077 /*
   1078  * Lower the protection on the specified range of this pmap.
   1079  *
   1080  * There are only two cases: either the protection is going to 0,
   1081  * or it is going to read-only.
   1082  */
   1083 void
   1084 pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   1085 {
   1086 	volatile u_int *ptp;
   1087 	int s, bic;
   1088 
   1089 	if ((prot & VM_PROT_READ) == 0) {
   1090 		pmap_remove(pm, sva, eva);
   1091 		return;
   1092 	}
   1093 	bic = 0;
   1094 	if ((prot & VM_PROT_WRITE) == 0) {
   1095 		bic |= TTE_WR;
   1096 	}
   1097 	if ((prot & VM_PROT_EXECUTE) == 0) {
   1098 		bic |= TTE_EX;
   1099 	}
   1100 	if (bic == 0) {
   1101 		return;
   1102 	}
   1103 	s = splvm();
   1104 	while (sva < eva) {
   1105 		if ((ptp = pte_find(pm, sva)) != NULL) {
   1106 			*ptp &= ~bic;
   1107 			ppc4xx_tlb_flush(sva, pm->pm_ctx);
   1108 		}
   1109 		sva += PAGE_SIZE;
   1110 	}
   1111 	splx(s);
   1112 }
   1113 
   1114 bool
   1115 pmap_check_attr(struct vm_page *pg, u_int mask, int clear)
   1116 {
   1117 	paddr_t pa;
   1118 	char *attr;
   1119 	int s, rv;
   1120 
   1121 	/*
   1122 	 * First modify bits in cache.
   1123 	 */
   1124 	pa = VM_PAGE_TO_PHYS(pg);
   1125 	attr = pa_to_attr(pa);
   1126 	if (attr == NULL)
   1127 		return false;
   1128 
   1129 	s = splvm();
   1130 	rv = ((*attr & mask) != 0);
   1131 	if (clear) {
   1132 		*attr &= ~mask;
   1133 		pmap_page_protect(pg, mask == PMAP_ATTR_CHG ? VM_PROT_READ : 0);
   1134 	}
   1135 	splx(s);
   1136 	return rv;
   1137 }
   1138 
   1139 
   1140 /*
   1141  * Lower the protection on the specified physical page.
   1142  *
   1143  * There are only two cases: either the protection is going to 0,
   1144  * or it is going to read-only.
   1145  */
   1146 void
   1147 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   1148 {
   1149 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1150 	vaddr_t va;
   1151 	struct pv_entry *pvh, *pv, *npv;
   1152 	struct pmap *pm;
   1153 
   1154 	pvh = pa_to_pv(pa);
   1155 	if (pvh == NULL)
   1156 		return;
   1157 
   1158 	/* Handle extra pvs which may be deleted in the operation */
   1159 	for (pv = pvh->pv_next; pv; pv = npv) {
   1160 		npv = pv->pv_next;
   1161 
   1162 		pm = pv->pv_pm;
   1163 		va = pv->pv_va;
   1164 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1165 	}
   1166 	/* Now check the head pv */
   1167 	if (pvh->pv_pm) {
   1168 		pv = pvh;
   1169 		pm = pv->pv_pm;
   1170 		va = pv->pv_va;
   1171 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1172 	}
   1173 }
   1174 
   1175 /*
   1176  * Activate the address space for the specified process.  If the process
   1177  * is the current process, load the new MMU context.
   1178  */
   1179 void
   1180 pmap_activate(struct lwp *l)
   1181 {
   1182 #if 0
   1183 	struct pcb *pcb = &l->l_proc->p_addr->u_pcb;
   1184 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
   1185 
   1186 	/*
   1187 	 * XXX Normally performed in cpu_lwp_fork().
   1188 	 */
   1189 	printf("pmap_activate(%p), pmap=%p\n",l,pmap);
   1190 	pcb->pcb_pm = pmap;
   1191 #endif
   1192 }
   1193 
   1194 /*
   1195  * Deactivate the specified process's address space.
   1196  */
   1197 void
   1198 pmap_deactivate(struct lwp *l)
   1199 {
   1200 }
   1201 
   1202 /*
   1203  * Synchronize caches corresponding to [addr, addr+len) in p.
   1204  */
   1205 void
   1206 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
   1207 {
   1208 	struct pmap *pm = p->p_vmspace->vm_map.pmap;
   1209 	int msr, ctx, opid, step;
   1210 
   1211 	step = CACHELINESIZE;
   1212 
   1213 	/*
   1214 	 * Need to turn off IMMU and switch to user context.
   1215 	 * (icbi uses DMMU).
   1216 	 */
   1217 	if (!(ctx = pm->pm_ctx)) {
   1218 		/* No context -- assign it one */
   1219 		ctx_alloc(pm);
   1220 		ctx = pm->pm_ctx;
   1221 	}
   1222 	__asm volatile("mfmsr %0;"
   1223 		"li %1, %7;"
   1224 		"andc %1,%0,%1;"
   1225 		"mtmsr %1;"
   1226 		"sync;isync;"
   1227 		"mfpid %1;"
   1228 		"mtpid %2;"
   1229 		"sync; isync;"
   1230 		"1:"
   1231 		"dcbf 0,%3;"
   1232 		"icbi 0,%3;"
   1233 		"add %3,%3,%5;"
   1234 		"addc. %4,%4,%6;"
   1235 		"bge 1b;"
   1236 		"mtpid %1;"
   1237 		"mtmsr %0;"
   1238 		"sync; isync"
   1239 		: "=&r" (msr), "=&r" (opid)
   1240 		: "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step),
   1241 		  "K" (PSL_IR | PSL_DR));
   1242 }
   1243 
   1244 
   1245 /* This has to be done in real mode !!! */
   1246 void
   1247 ppc4xx_tlb_flush(vaddr_t va, int pid)
   1248 {
   1249 	u_long i, found;
   1250 	u_long msr;
   1251 
   1252 	/* If there's no context then it can't be mapped. */
   1253 	if (!pid)
   1254 		return;
   1255 
   1256 	__asm( 	"mfpid %1;"		/* Save PID */
   1257 		"mfmsr %2;"		/* Save MSR */
   1258 		"li %0,0;"		/* Now clear MSR */
   1259 		"mtmsr %0;"
   1260 		"mtpid %4;"		/* Set PID */
   1261 		"sync;"
   1262 		"tlbsx. %0,0,%3;"	/* Search TLB */
   1263 		"sync;"
   1264 		"mtpid %1;"		/* Restore PID */
   1265 		"mtmsr %2;"		/* Restore MSR */
   1266 		"sync;isync;"
   1267 		"li %1,1;"
   1268 		"beq 1f;"
   1269 		"li %1,0;"
   1270 		"1:"
   1271 		: "=&r" (i), "=&r" (found), "=&r" (msr)
   1272 		: "r" (va), "r" (pid));
   1273 	if (found && !TLB_LOCKED(i)) {
   1274 
   1275 		/* Now flush translation */
   1276 		__asm volatile(
   1277 			"tlbwe %0,%1,0;"
   1278 			"sync;isync;"
   1279 			: : "r" (0), "r" (i));
   1280 
   1281 		tlb_info[i].ti_ctx = 0;
   1282 		tlb_info[i].ti_flags = 0;
   1283 		tlbnext = i;
   1284 		/* Successful flushes */
   1285 		tlbflush_ev.ev_count++;
   1286 	}
   1287 }
   1288 
   1289 void
   1290 ppc4xx_tlb_flush_all(void)
   1291 {
   1292 	u_long i;
   1293 
   1294 	for (i = 0; i < NTLB; i++)
   1295 		if (!TLB_LOCKED(i)) {
   1296 			__asm volatile(
   1297 				"tlbwe %0,%1,0;"
   1298 				"sync;isync;"
   1299 				: : "r" (0), "r" (i));
   1300 			tlb_info[i].ti_ctx = 0;
   1301 			tlb_info[i].ti_flags = 0;
   1302 		}
   1303 
   1304 	__asm volatile("sync;isync");
   1305 }
   1306 
   1307 /* Find a TLB entry to evict. */
   1308 static int
   1309 ppc4xx_tlb_find_victim(void)
   1310 {
   1311 	int flags;
   1312 
   1313 	for (;;) {
   1314 		if (++tlbnext >= NTLB)
   1315 			tlbnext = tlb_nreserved;
   1316 		flags = tlb_info[tlbnext].ti_flags;
   1317 		if (!(flags & TLBF_USED) ||
   1318 			(flags & (TLBF_LOCKED | TLBF_REF)) == 0) {
   1319 			u_long va, stack = (u_long)&va;
   1320 
   1321 			if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) &&
   1322 			    (tlb_info[tlbnext].ti_ctx == KERNEL_PID) &&
   1323 			     (flags & TLBF_USED)) {
   1324 				/* Kernel stack page */
   1325 				flags |= TLBF_USED;
   1326 				tlb_info[tlbnext].ti_flags = flags;
   1327 			} else {
   1328 				/* Found it! */
   1329 				return (tlbnext);
   1330 			}
   1331 		} else {
   1332 			tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF);
   1333 		}
   1334 	}
   1335 }
   1336 
   1337 void
   1338 ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte)
   1339 {
   1340 	u_long th, tl, idx;
   1341 	tlbpid_t pid;
   1342 	u_short msr;
   1343 	paddr_t pa;
   1344 	int s, sz;
   1345 
   1346 	tlbenter_ev.ev_count++;
   1347 
   1348 	sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT;
   1349 	pa = (pte & TTE_RPN_MASK(sz));
   1350 	th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID;
   1351 	tl = (pte & ~TLB_RPN_MASK) | pa;
   1352 	tl |= ppc4xx_tlbflags(va, pa);
   1353 
   1354 	s = splhigh();
   1355 	idx = ppc4xx_tlb_find_victim();
   1356 
   1357 #ifdef DIAGNOSTIC
   1358 	if ((idx < tlb_nreserved) || (idx >= NTLB)) {
   1359 		panic("ppc4xx_tlb_enter: replacing entry %ld", idx);
   1360 	}
   1361 #endif
   1362 
   1363 	tlb_info[idx].ti_va = (va & TLB_EPN_MASK);
   1364 	tlb_info[idx].ti_ctx = ctx;
   1365 	tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF;
   1366 
   1367 	__asm volatile(
   1368 		"mfmsr %0;"			/* Save MSR */
   1369 		"li %1,0;"
   1370 		"tlbwe %1,%3,0;"		/* Invalidate old entry. */
   1371 		"mtmsr %1;"			/* Clear MSR */
   1372 		"mfpid %1;"			/* Save old PID */
   1373 		"mtpid %2;"			/* Load translation ctx */
   1374 		"sync; isync;"
   1375 #ifdef DEBUG
   1376 		"andi. %3,%3,63;"
   1377 		"tweqi %3,0;" 			/* XXXXX DEBUG trap on index 0 */
   1378 #endif
   1379 		"tlbwe %4,%3,1; tlbwe %5,%3,0;"	/* Set TLB */
   1380 		"sync; isync;"
   1381 		"mtpid %1; mtmsr %0;"		/* Restore PID and MSR */
   1382 		"sync; isync;"
   1383 	: "=&r" (msr), "=&r" (pid)
   1384 	: "r" (ctx), "r" (idx), "r" (tl), "r" (th));
   1385 	splx(s);
   1386 }
   1387 
   1388 void
   1389 ppc4xx_tlb_init(void)
   1390 {
   1391 	int i;
   1392 
   1393 	/* Mark reserved TLB entries */
   1394 	for (i = 0; i < tlb_nreserved; i++) {
   1395 		tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED;
   1396 		tlb_info[i].ti_ctx = KERNEL_PID;
   1397 	}
   1398 
   1399 	/* Setup security zones */
   1400 	/* Z0 - accessible by kernel only if TLB entry permissions allow
   1401 	 * Z1,Z2 - access is controlled by TLB entry permissions
   1402 	 * Z3 - full access regardless of TLB entry permissions
   1403 	 */
   1404 
   1405 	__asm volatile(
   1406 		"mtspr %0,%1;"
   1407 		"sync;"
   1408 		::  "K"(SPR_ZPR), "r" (0x1b000000));
   1409 }
   1410 
   1411 /*
   1412  * ppc4xx_tlb_size_mask:
   1413  *
   1414  * 	Roundup size to supported page size, return TLBHI mask and real size.
   1415  */
   1416 static int
   1417 ppc4xx_tlb_size_mask(size_t size, int *mask, int *rsiz)
   1418 {
   1419 	int 			i;
   1420 
   1421 	for (i = 0; i < __arraycount(tlbsize); i++)
   1422 		if (size <= tlbsize[i]) {
   1423 			*mask = (i << TLB_SIZE_SHFT);
   1424 			*rsiz = tlbsize[i];
   1425 			return (0);
   1426 		}
   1427 	return (EINVAL);
   1428 }
   1429 
   1430 /*
   1431  * ppc4xx_tlb_mapiodev:
   1432  *
   1433  * 	Lookup virtual address of mapping previously entered via
   1434  * 	ppc4xx_tlb_reserve. Search TLB directly so that we don't
   1435  * 	need to waste extra storage for reserved mappings. Note
   1436  * 	that reading TLBHI also sets PID, but all reserved mappings
   1437  * 	use KERNEL_PID, so the side effect is nil.
   1438  */
   1439 void *
   1440 ppc4xx_tlb_mapiodev(paddr_t base, psize_t len)
   1441 {
   1442 	paddr_t 		pa;
   1443 	vaddr_t 		va;
   1444 	u_int 			lo, hi, sz;
   1445 	int 			i;
   1446 
   1447 	/* tlb_nreserved is only allowed to grow, so this is safe. */
   1448 	for (i = 0; i < tlb_nreserved; i++) {
   1449 		__asm volatile (
   1450 		    "	tlbre %0,%2,1 	\n" 	/* TLBLO */
   1451 		    "	tlbre %1,%2,0 	\n" 	/* TLBHI */
   1452 		    : "=&r" (lo), "=&r" (hi)
   1453 		    : "r" (i));
   1454 
   1455 		KASSERT(hi & TLB_VALID);
   1456 		KASSERT(mfspr(SPR_PID) == KERNEL_PID);
   1457 
   1458 		pa = (lo & TLB_RPN_MASK);
   1459 		if (base < pa)
   1460 			continue;
   1461 
   1462 		sz = tlbsize[(hi & TLB_SIZE_MASK) >> TLB_SIZE_SHFT];
   1463 		if ((base + len) > (pa + sz))
   1464 			continue;
   1465 
   1466 		va = (hi & TLB_EPN_MASK) + (base & (sz - 1)); 	/* sz = 2^n */
   1467 		return (void *)(va);
   1468 	}
   1469 
   1470 	return (NULL);
   1471 }
   1472 
   1473 /*
   1474  * ppc4xx_tlb_reserve:
   1475  *
   1476  * 	Map physical range to kernel virtual chunk via reserved TLB entry.
   1477  */
   1478 void
   1479 ppc4xx_tlb_reserve(paddr_t pa, vaddr_t va, size_t size, int flags)
   1480 {
   1481 	u_int 			lo, hi;
   1482 	int 			szmask, rsize;
   1483 
   1484 	/* Called before pmap_bootstrap(), va outside kernel space. */
   1485 	KASSERT(va < VM_MIN_KERNEL_ADDRESS || va >= VM_MAX_KERNEL_ADDRESS);
   1486 	KASSERT(! pmap_bootstrap_done);
   1487 	KASSERT(tlb_nreserved < NTLB);
   1488 
   1489 	/* Resolve size. */
   1490 	if (ppc4xx_tlb_size_mask(size, &szmask, &rsize) != 0)
   1491 		panic("ppc4xx_tlb_reserve: entry %d, %zuB too large",
   1492 		    size, tlb_nreserved);
   1493 
   1494 	/* Real size will be power of two >= 1024, so this is OK. */
   1495 	pa &= ~(rsize - 1); 	/* RPN */
   1496 	va &= ~(rsize - 1); 	/* EPN */
   1497 
   1498 	lo = pa | TLB_WR | flags;
   1499 	hi = va | TLB_VALID | szmask;
   1500 
   1501 #ifdef PPC_4XX_NOCACHE
   1502 	lo |= TLB_I;
   1503 #endif
   1504 
   1505 	__asm volatile(
   1506 	    "	tlbwe %1,%0,1 	\n" 	/* write TLBLO */
   1507 	    "	tlbwe %2,%0,0 	\n" 	/* write TLBHI */
   1508 	    "   sync 		\n"
   1509 	    "	isync 		\n"
   1510 	    : : "r" (tlb_nreserved), "r" (lo), "r" (hi));
   1511 
   1512 	tlb_nreserved++;
   1513 }
   1514 
   1515 /*
   1516  * We should pass the ctx in from trap code.
   1517  */
   1518 int
   1519 pmap_tlbmiss(vaddr_t va, int ctx)
   1520 {
   1521 	volatile u_int *pte;
   1522 	u_long tte;
   1523 
   1524 	tlbmiss_ev.ev_count++;
   1525 
   1526 	/*
   1527 	 * We will reserve 0 upto VM_MIN_KERNEL_ADDRESS for va == pa mappings.
   1528 	 * Physical RAM is expected to live in this range, care must be taken
   1529 	 * to not clobber 0 upto ${physmem} with device mappings in machdep
   1530 	 * code.
   1531 	 */
   1532 	if (ctx != KERNEL_PID ||
   1533 	    (va >= VM_MIN_KERNEL_ADDRESS && va < VM_MAX_KERNEL_ADDRESS)) {
   1534 		pte = pte_find((struct pmap *)__UNVOLATILE(ctxbusy[ctx]), va);
   1535 		if (pte == NULL) {
   1536 			/* Map unmanaged addresses directly for kernel access */
   1537 			return 1;
   1538 		}
   1539 		tte = *pte;
   1540 		if (tte == 0) {
   1541 			return 1;
   1542 		}
   1543 	} else {
   1544 		/* Create a 16MB writable mapping. */
   1545 #ifdef PPC_4XX_NOCACHE
   1546 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I |TTE_WR;
   1547 #else
   1548 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR;
   1549 #endif
   1550 	}
   1551 	tlbhit_ev.ev_count++;
   1552 	ppc4xx_tlb_enter(ctx, va, tte);
   1553 
   1554 	return 0;
   1555 }
   1556 
   1557 /*
   1558  * Flush all the entries matching a context from the TLB.
   1559  */
   1560 static int
   1561 ctx_flush(int cnum)
   1562 {
   1563 	int i;
   1564 
   1565 	/* We gotta steal this context */
   1566 	for (i = tlb_nreserved; i < NTLB; i++) {
   1567 		if (tlb_info[i].ti_ctx == cnum) {
   1568 			/* Can't steal ctx if it has a locked entry. */
   1569 			if (TLB_LOCKED(i)) {
   1570 #ifdef DIAGNOSTIC
   1571 				printf("ctx_flush: can't invalidate "
   1572 					"locked mapping %d "
   1573 					"for context %d\n", i, cnum);
   1574 #ifdef DDB
   1575 				Debugger();
   1576 #endif
   1577 #endif
   1578 				return (1);
   1579 			}
   1580 #ifdef DIAGNOSTIC
   1581 			if (i < tlb_nreserved)
   1582 				panic("TLB entry %d not locked", i);
   1583 #endif
   1584 			/* Invalidate particular TLB entry regardless of locked status */
   1585 			__asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i));
   1586 			tlb_info[i].ti_flags = 0;
   1587 		}
   1588 	}
   1589 	return (0);
   1590 }
   1591 
   1592 /*
   1593  * Allocate a context.  If necessary, steal one from someone else.
   1594  *
   1595  * The new context is flushed from the TLB before returning.
   1596  */
   1597 int
   1598 ctx_alloc(struct pmap *pm)
   1599 {
   1600 	int s, cnum;
   1601 	static int next = MINCTX;
   1602 
   1603 	if (pm == pmap_kernel()) {
   1604 #ifdef DIAGNOSTIC
   1605 		printf("ctx_alloc: kernel pmap!\n");
   1606 #endif
   1607 		return (0);
   1608 	}
   1609 	s = splvm();
   1610 
   1611 	/* Find a likely context. */
   1612 	cnum = next;
   1613 	do {
   1614 		if ((++cnum) > NUMCTX)
   1615 			cnum = MINCTX;
   1616 	} while (ctxbusy[cnum] != NULL && cnum != next);
   1617 
   1618 	/* Now clean it out */
   1619 oops:
   1620 	if (cnum < MINCTX)
   1621 		cnum = MINCTX; /* Never steal ctx 0 or 1 */
   1622 	if (ctx_flush(cnum)) {
   1623 		/* oops -- something's wired. */
   1624 		if ((++cnum) > NUMCTX)
   1625 			cnum = MINCTX;
   1626 		goto oops;
   1627 	}
   1628 
   1629 	if (ctxbusy[cnum]) {
   1630 #ifdef DEBUG
   1631 		/* We should identify this pmap and clear it */
   1632 		printf("Warning: stealing context %d\n", cnum);
   1633 #endif
   1634 		ctxbusy[cnum]->pm_ctx = 0;
   1635 	}
   1636 	ctxbusy[cnum] = pm;
   1637 	next = cnum;
   1638 	splx(s);
   1639 	pm->pm_ctx = cnum;
   1640 
   1641 	return cnum;
   1642 }
   1643 
   1644 /*
   1645  * Give away a context.
   1646  */
   1647 void
   1648 ctx_free(struct pmap *pm)
   1649 {
   1650 	int oldctx;
   1651 
   1652 	oldctx = pm->pm_ctx;
   1653 
   1654 	if (oldctx == 0)
   1655 		panic("ctx_free: freeing kernel context");
   1656 #ifdef DIAGNOSTIC
   1657 	if (ctxbusy[oldctx] == 0)
   1658 		printf("ctx_free: freeing free context %d\n", oldctx);
   1659 	if (ctxbusy[oldctx] != pm) {
   1660 		printf("ctx_free: freeing someone esle's context\n "
   1661 		       "ctxbusy[%d] = %p, pm->pm_ctx = %p\n",
   1662 		       oldctx, (void *)(u_long)ctxbusy[oldctx], pm);
   1663 #ifdef DDB
   1664 		Debugger();
   1665 #endif
   1666 	}
   1667 #endif
   1668 	/* We should verify it has not been stolen and reallocated... */
   1669 	ctxbusy[oldctx] = NULL;
   1670 	ctx_flush(oldctx);
   1671 }
   1672 
   1673 
   1674 #ifdef DEBUG
   1675 /*
   1676  * Test ref/modify handling.
   1677  */
   1678 void pmap_testout(void);
   1679 void
   1680 pmap_testout(void)
   1681 {
   1682 	vaddr_t va;
   1683 	volatile int *loc;
   1684 	int val = 0;
   1685 	paddr_t pa;
   1686 	struct vm_page *pg;
   1687 	int ref, mod;
   1688 
   1689 	/* Allocate a page */
   1690 	va = (vaddr_t)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   1691 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
   1692 	loc = (int*)va;
   1693 
   1694 	pmap_extract(pmap_kernel(), va, &pa);
   1695 	pg = PHYS_TO_VM_PAGE(pa);
   1696 	pmap_unwire(pmap_kernel(), va);
   1697 
   1698 	pmap_kremove(va, PAGE_SIZE);
   1699 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1700 	pmap_update(pmap_kernel());
   1701 
   1702 	/* Now clear reference and modify */
   1703 	ref = pmap_clear_reference(pg);
   1704 	mod = pmap_clear_modify(pg);
   1705 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1706 	       (void *)(u_long)va, (long)pa,
   1707 	       ref, mod);
   1708 
   1709 	/* Check it's properly cleared */
   1710 	ref = pmap_is_referenced(pg);
   1711 	mod = pmap_is_modified(pg);
   1712 	printf("Checking cleared page: ref %d, mod %d\n",
   1713 	       ref, mod);
   1714 
   1715 	/* Reference page */
   1716 	val = *loc;
   1717 
   1718 	ref = pmap_is_referenced(pg);
   1719 	mod = pmap_is_modified(pg);
   1720 	printf("Referenced page: ref %d, mod %d val %x\n",
   1721 	       ref, mod, val);
   1722 
   1723 	/* Now clear reference and modify */
   1724 	ref = pmap_clear_reference(pg);
   1725 	mod = pmap_clear_modify(pg);
   1726 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1727 	       (void *)(u_long)va, (long)pa,
   1728 	       ref, mod);
   1729 
   1730 	/* Modify page */
   1731 	*loc = 1;
   1732 
   1733 	ref = pmap_is_referenced(pg);
   1734 	mod = pmap_is_modified(pg);
   1735 	printf("Modified page: ref %d, mod %d\n",
   1736 	       ref, mod);
   1737 
   1738 	/* Now clear reference and modify */
   1739 	ref = pmap_clear_reference(pg);
   1740 	mod = pmap_clear_modify(pg);
   1741 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1742 	       (void *)(u_long)va, (long)pa,
   1743 	       ref, mod);
   1744 
   1745 	/* Check it's properly cleared */
   1746 	ref = pmap_is_referenced(pg);
   1747 	mod = pmap_is_modified(pg);
   1748 	printf("Checking cleared page: ref %d, mod %d\n",
   1749 	       ref, mod);
   1750 
   1751 	/* Modify page */
   1752 	*loc = 1;
   1753 
   1754 	ref = pmap_is_referenced(pg);
   1755 	mod = pmap_is_modified(pg);
   1756 	printf("Modified page: ref %d, mod %d\n",
   1757 	       ref, mod);
   1758 
   1759 	/* Check pmap_protect() */
   1760 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ);
   1761 	pmap_update(pmap_kernel());
   1762 	ref = pmap_is_referenced(pg);
   1763 	mod = pmap_is_modified(pg);
   1764 	printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n",
   1765 	       ref, mod);
   1766 
   1767 	/* Now clear reference and modify */
   1768 	ref = pmap_clear_reference(pg);
   1769 	mod = pmap_clear_modify(pg);
   1770 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1771 	       (void *)(u_long)va, (long)pa,
   1772 	       ref, mod);
   1773 
   1774 	/* Reference page */
   1775 	val = *loc;
   1776 
   1777 	ref = pmap_is_referenced(pg);
   1778 	mod = pmap_is_modified(pg);
   1779 	printf("Referenced page: ref %d, mod %d val %x\n",
   1780 	       ref, mod, val);
   1781 
   1782 	/* Now clear reference and modify */
   1783 	ref = pmap_clear_reference(pg);
   1784 	mod = pmap_clear_modify(pg);
   1785 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1786 	       (void *)(u_long)va, (long)pa,
   1787 	       ref, mod);
   1788 
   1789 	/* Modify page */
   1790 #if 0
   1791 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1792 	pmap_update(pmap_kernel());
   1793 #endif
   1794 	*loc = 1;
   1795 
   1796 	ref = pmap_is_referenced(pg);
   1797 	mod = pmap_is_modified(pg);
   1798 	printf("Modified page: ref %d, mod %d\n",
   1799 	       ref, mod);
   1800 
   1801 	/* Check pmap_protect() */
   1802 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE);
   1803 	pmap_update(pmap_kernel());
   1804 	ref = pmap_is_referenced(pg);
   1805 	mod = pmap_is_modified(pg);
   1806 	printf("pmap_protect(): ref %d, mod %d\n",
   1807 	       ref, mod);
   1808 
   1809 	/* Now clear reference and modify */
   1810 	ref = pmap_clear_reference(pg);
   1811 	mod = pmap_clear_modify(pg);
   1812 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1813 	       (void *)(u_long)va, (long)pa,
   1814 	       ref, mod);
   1815 
   1816 	/* Reference page */
   1817 	val = *loc;
   1818 
   1819 	ref = pmap_is_referenced(pg);
   1820 	mod = pmap_is_modified(pg);
   1821 	printf("Referenced page: ref %d, mod %d val %x\n",
   1822 	       ref, mod, val);
   1823 
   1824 	/* Now clear reference and modify */
   1825 	ref = pmap_clear_reference(pg);
   1826 	mod = pmap_clear_modify(pg);
   1827 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1828 	       (void *)(u_long)va, (long)pa,
   1829 	       ref, mod);
   1830 
   1831 	/* Modify page */
   1832 #if 0
   1833 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1834 	pmap_update(pmap_kernel());
   1835 #endif
   1836 	*loc = 1;
   1837 
   1838 	ref = pmap_is_referenced(pg);
   1839 	mod = pmap_is_modified(pg);
   1840 	printf("Modified page: ref %d, mod %d\n",
   1841 	       ref, mod);
   1842 
   1843 	/* Check pmap_pag_protect() */
   1844 	pmap_page_protect(pg, VM_PROT_READ);
   1845 	ref = pmap_is_referenced(pg);
   1846 	mod = pmap_is_modified(pg);
   1847 	printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n",
   1848 	       ref, mod);
   1849 
   1850 	/* Now clear reference and modify */
   1851 	ref = pmap_clear_reference(pg);
   1852 	mod = pmap_clear_modify(pg);
   1853 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1854 	       (void *)(u_long)va, (long)pa,
   1855 	       ref, mod);
   1856 
   1857 	/* Reference page */
   1858 	val = *loc;
   1859 
   1860 	ref = pmap_is_referenced(pg);
   1861 	mod = pmap_is_modified(pg);
   1862 	printf("Referenced page: ref %d, mod %d val %x\n",
   1863 	       ref, mod, val);
   1864 
   1865 	/* Now clear reference and modify */
   1866 	ref = pmap_clear_reference(pg);
   1867 	mod = pmap_clear_modify(pg);
   1868 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1869 	       (void *)(u_long)va, (long)pa,
   1870 	       ref, mod);
   1871 
   1872 	/* Modify page */
   1873 #if 0
   1874 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1875 	pmap_update(pmap_kernel());
   1876 #endif
   1877 	*loc = 1;
   1878 
   1879 	ref = pmap_is_referenced(pg);
   1880 	mod = pmap_is_modified(pg);
   1881 	printf("Modified page: ref %d, mod %d\n",
   1882 	       ref, mod);
   1883 
   1884 	/* Check pmap_pag_protect() */
   1885 	pmap_page_protect(pg, VM_PROT_NONE);
   1886 	ref = pmap_is_referenced(pg);
   1887 	mod = pmap_is_modified(pg);
   1888 	printf("pmap_page_protect(): ref %d, mod %d\n",
   1889 	       ref, mod);
   1890 
   1891 	/* Now clear reference and modify */
   1892 	ref = pmap_clear_reference(pg);
   1893 	mod = pmap_clear_modify(pg);
   1894 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1895 	       (void *)(u_long)va, (long)pa,
   1896 	       ref, mod);
   1897 
   1898 
   1899 	/* Reference page */
   1900 	val = *loc;
   1901 
   1902 	ref = pmap_is_referenced(pg);
   1903 	mod = pmap_is_modified(pg);
   1904 	printf("Referenced page: ref %d, mod %d val %x\n",
   1905 	       ref, mod, val);
   1906 
   1907 	/* Now clear reference and modify */
   1908 	ref = pmap_clear_reference(pg);
   1909 	mod = pmap_clear_modify(pg);
   1910 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1911 	       (void *)(u_long)va, (long)pa,
   1912 	       ref, mod);
   1913 
   1914 	/* Modify page */
   1915 #if 0
   1916 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1917 	pmap_update(pmap_kernel());
   1918 #endif
   1919 	*loc = 1;
   1920 
   1921 	ref = pmap_is_referenced(pg);
   1922 	mod = pmap_is_modified(pg);
   1923 	printf("Modified page: ref %d, mod %d\n",
   1924 	       ref, mod);
   1925 
   1926 	/* Unmap page */
   1927 	pmap_remove(pmap_kernel(), va, va+1);
   1928 	pmap_update(pmap_kernel());
   1929 	ref = pmap_is_referenced(pg);
   1930 	mod = pmap_is_modified(pg);
   1931 	printf("Unmapped page: ref %d, mod %d\n", ref, mod);
   1932 
   1933 	/* Now clear reference and modify */
   1934 	ref = pmap_clear_reference(pg);
   1935 	mod = pmap_clear_modify(pg);
   1936 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1937 	       (void *)(u_long)va, (long)pa, ref, mod);
   1938 
   1939 	/* Check it's properly cleared */
   1940 	ref = pmap_is_referenced(pg);
   1941 	mod = pmap_is_modified(pg);
   1942 	printf("Checking cleared page: ref %d, mod %d\n",
   1943 	       ref, mod);
   1944 
   1945 	pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
   1946 	pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
   1947 	uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE, UVM_KMF_WIRED);
   1948 }
   1949 #endif
   1950