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