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