Home | History | Annotate | Line # | Download | only in ibm4xx
pmap.c revision 1.35
      1  1.34     yamt /*	$NetBSD: pmap.c,v 1.35 2005/04/07 12:01:08 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.34     yamt __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.35 2005/04/07 12:01:08 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.34     yamt 		    (uint *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    215  1.34     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.34     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.32   simonb 		if (!pg)
    532  1.32   simonb 			panic("pmap_growkernel: no pages");
    533   1.5      eeh 		pmap_zero_page((paddr_t)pg);
    534   1.5      eeh 
    535   1.5      eeh 		/* XXX This is based on all phymem being addressable */
    536   1.5      eeh 		pm->pm_ptbl[seg] = (u_int *)pg;
    537   1.5      eeh 	}
    538   1.5      eeh 	splx(s);
    539   1.5      eeh 	return (kbreak);
    540   1.5      eeh }
    541   1.5      eeh 
    542   1.5      eeh /*
    543   1.5      eeh  *	vm_page_alloc1:
    544   1.5      eeh  *
    545   1.5      eeh  *	Allocate and return a memory cell with no associated object.
    546   1.5      eeh  */
    547   1.5      eeh struct vm_page *
    548  1.30      chs vm_page_alloc1(void)
    549   1.5      eeh {
    550  1.30      chs 	struct vm_page *pg;
    551  1.30      chs 
    552  1.30      chs 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    553   1.5      eeh 	if (pg) {
    554   1.5      eeh 		pg->wire_count = 1;	/* no mappings yet */
    555   1.5      eeh 		pg->flags &= ~PG_BUSY;	/* never busy */
    556   1.5      eeh 	}
    557   1.5      eeh 	return pg;
    558   1.5      eeh }
    559   1.5      eeh 
    560   1.5      eeh /*
    561   1.5      eeh  *	vm_page_free1:
    562   1.5      eeh  *
    563   1.5      eeh  *	Returns the given page to the free list,
    564   1.5      eeh  *	disassociating it with any VM object.
    565   1.5      eeh  *
    566   1.5      eeh  *	Object and page must be locked prior to entry.
    567   1.5      eeh  */
    568   1.5      eeh void
    569  1.30      chs vm_page_free1(struct vm_page *mem)
    570   1.5      eeh {
    571  1.10      eeh #ifdef DIAGNOSTIC
    572   1.5      eeh 	if (mem->flags != (PG_CLEAN|PG_FAKE)) {
    573   1.5      eeh 		printf("Freeing invalid page %p\n", mem);
    574   1.5      eeh 		printf("pa = %llx\n", (unsigned long long)VM_PAGE_TO_PHYS(mem));
    575  1.10      eeh #ifdef DDB
    576   1.5      eeh 		Debugger();
    577  1.10      eeh #endif
    578   1.5      eeh 		return;
    579   1.5      eeh 	}
    580  1.10      eeh #endif
    581   1.5      eeh 	mem->flags |= PG_BUSY;
    582   1.5      eeh 	mem->wire_count = 0;
    583   1.5      eeh 	uvm_pagefree(mem);
    584   1.5      eeh }
    585   1.5      eeh #endif
    586   1.5      eeh 
    587   1.1   simonb /*
    588   1.1   simonb  * Create and return a physical map.
    589   1.1   simonb  */
    590   1.1   simonb struct pmap *
    591   1.1   simonb pmap_create(void)
    592   1.1   simonb {
    593   1.1   simonb 	struct pmap *pm;
    594   1.1   simonb 
    595  1.30      chs 	pm = malloc(sizeof *pm, M_VMPMAP, M_WAITOK);
    596  1.30      chs 	memset(pm, 0, sizeof *pm);
    597  1.30      chs 	pm->pm_refs = 1;
    598   1.1   simonb 	return pm;
    599   1.1   simonb }
    600   1.1   simonb 
    601   1.1   simonb /*
    602   1.1   simonb  * Add a reference to the given pmap.
    603   1.1   simonb  */
    604   1.1   simonb void
    605   1.1   simonb pmap_reference(struct pmap *pm)
    606   1.1   simonb {
    607   1.1   simonb 
    608   1.1   simonb 	pm->pm_refs++;
    609   1.1   simonb }
    610   1.1   simonb 
    611   1.1   simonb /*
    612   1.1   simonb  * Retire the given pmap from service.
    613   1.1   simonb  * Should only be called if the map contains no valid mappings.
    614   1.1   simonb  */
    615   1.1   simonb void
    616   1.1   simonb pmap_destroy(struct pmap *pm)
    617   1.1   simonb {
    618  1.30      chs 	int i;
    619   1.1   simonb 
    620  1.30      chs 	if (--pm->pm_refs > 0) {
    621  1.30      chs 		return;
    622   1.1   simonb 	}
    623  1.30      chs 	KASSERT(pm->pm_stats.resident_count == 0);
    624  1.30      chs 	KASSERT(pm->pm_stats.wired_count == 0);
    625   1.1   simonb 	for (i = 0; i < STSZ; i++)
    626   1.1   simonb 		if (pm->pm_ptbl[i]) {
    627  1.19  thorpej 			uvm_km_free(kernel_map, (vaddr_t)pm->pm_ptbl[i],
    628  1.34     yamt 			    PAGE_SIZE, UVM_KMF_WIRED);
    629   1.1   simonb 			pm->pm_ptbl[i] = NULL;
    630   1.1   simonb 		}
    631  1.30      chs 	if (pm->pm_ctx)
    632  1.30      chs 		ctx_free(pm);
    633  1.30      chs 	free(pm, M_VMPMAP);
    634   1.1   simonb }
    635   1.1   simonb 
    636   1.1   simonb /*
    637   1.1   simonb  * Copy the range specified by src_addr/len
    638   1.1   simonb  * from the source map to the range dst_addr/len
    639   1.1   simonb  * in the destination map.
    640   1.1   simonb  *
    641   1.1   simonb  * This routine is only advisory and need not do anything.
    642   1.1   simonb  */
    643   1.1   simonb void
    644   1.1   simonb pmap_copy(struct pmap *dst_pmap, struct pmap *src_pmap, vaddr_t dst_addr,
    645   1.1   simonb 	  vsize_t len, vaddr_t src_addr)
    646   1.1   simonb {
    647   1.1   simonb }
    648   1.1   simonb 
    649   1.1   simonb /*
    650   1.1   simonb  * Require that all active physical maps contain no
    651   1.1   simonb  * incorrect entries NOW.
    652   1.1   simonb  */
    653   1.1   simonb void
    654   1.4    chris pmap_update(struct pmap *pmap)
    655   1.1   simonb {
    656   1.1   simonb }
    657   1.1   simonb 
    658   1.1   simonb /*
    659   1.1   simonb  * Garbage collects the physical map system for
    660   1.1   simonb  * pages which are no longer used.
    661   1.1   simonb  * Success need not be guaranteed -- that is, there
    662   1.1   simonb  * may well be pages which are not referenced, but
    663   1.1   simonb  * others may be collected.
    664   1.1   simonb  * Called by the pageout daemon when pages are scarce.
    665   1.1   simonb  */
    666   1.1   simonb void
    667   1.1   simonb pmap_collect(struct pmap *pm)
    668   1.1   simonb {
    669   1.1   simonb }
    670   1.1   simonb 
    671   1.1   simonb /*
    672   1.1   simonb  * Fill the given physical page with zeroes.
    673   1.1   simonb  */
    674   1.1   simonb void
    675   1.1   simonb pmap_zero_page(paddr_t pa)
    676   1.1   simonb {
    677   1.1   simonb 
    678   1.8  thorpej #ifdef PPC_4XX_NOCACHE
    679  1.19  thorpej 	memset((caddr_t)pa, 0, PAGE_SIZE);
    680   1.1   simonb #else
    681   1.1   simonb 	int i;
    682   1.1   simonb 
    683  1.19  thorpej 	for (i = PAGE_SIZE/CACHELINESIZE; i > 0; i--) {
    684   1.1   simonb 		__asm __volatile ("dcbz 0,%0" :: "r"(pa));
    685   1.1   simonb 		pa += CACHELINESIZE;
    686   1.1   simonb 	}
    687   1.1   simonb #endif
    688   1.1   simonb }
    689   1.1   simonb 
    690   1.1   simonb /*
    691   1.1   simonb  * Copy the given physical source page to its destination.
    692   1.1   simonb  */
    693   1.1   simonb void
    694   1.1   simonb pmap_copy_page(paddr_t src, paddr_t dst)
    695   1.1   simonb {
    696   1.1   simonb 
    697  1.19  thorpej 	memcpy((caddr_t)dst, (caddr_t)src, PAGE_SIZE);
    698   1.1   simonb 	dcache_flush_page(dst);
    699   1.1   simonb }
    700   1.1   simonb 
    701   1.1   simonb /*
    702   1.1   simonb  * This returns whether this is the first mapping of a page.
    703   1.1   simonb  */
    704   1.1   simonb static inline int
    705  1.30      chs pmap_enter_pv(struct pmap *pm, vaddr_t va, paddr_t pa, boolean_t wired)
    706   1.1   simonb {
    707   1.1   simonb 	struct pv_entry *pv, *npv = NULL;
    708   1.1   simonb 	int s;
    709   1.1   simonb 
    710   1.1   simonb 	if (!pmap_initialized)
    711   1.1   simonb 		return 0;
    712   1.1   simonb 
    713   1.1   simonb 	s = splvm();
    714   1.1   simonb 	pv = pa_to_pv(pa);
    715   1.1   simonb 	if (!pv->pv_pm) {
    716   1.1   simonb 		/*
    717   1.1   simonb 		 * No entries yet, use header as the first entry.
    718   1.1   simonb 		 */
    719   1.1   simonb 		pv->pv_va = va;
    720   1.1   simonb 		pv->pv_pm = pm;
    721   1.1   simonb 		pv->pv_next = NULL;
    722   1.1   simonb 	} else {
    723   1.1   simonb 		/*
    724   1.1   simonb 		 * There is at least one other VA mapping this page.
    725   1.1   simonb 		 * Place this entry after the header.
    726   1.1   simonb 		 */
    727   1.1   simonb 		npv = pool_get(&pv_pool, PR_WAITOK);
    728   1.1   simonb 		npv->pv_va = va;
    729   1.1   simonb 		npv->pv_pm = pm;
    730   1.1   simonb 		npv->pv_next = pv->pv_next;
    731   1.1   simonb 		pv->pv_next = npv;
    732  1.33      chs 		pv = npv;
    733   1.1   simonb 	}
    734  1.30      chs 	if (wired) {
    735  1.30      chs 		PV_WIRE(pv);
    736  1.33      chs 		pm->pm_stats.wired_count++;
    737  1.30      chs 	}
    738   1.1   simonb 	splx(s);
    739   1.1   simonb 	return (1);
    740   1.1   simonb }
    741   1.1   simonb 
    742   1.1   simonb static void
    743   1.1   simonb pmap_remove_pv(struct pmap *pm, vaddr_t va, paddr_t pa)
    744   1.1   simonb {
    745   1.1   simonb 	struct pv_entry *pv, *npv;
    746   1.1   simonb 
    747   1.1   simonb 	/*
    748   1.1   simonb 	 * Remove from the PV table.
    749   1.1   simonb 	 */
    750   1.1   simonb 	pv = pa_to_pv(pa);
    751  1.30      chs 	if (!pv)
    752  1.30      chs 		return;
    753   1.1   simonb 
    754   1.1   simonb 	/*
    755   1.1   simonb 	 * If it is the first entry on the list, it is actually
    756   1.1   simonb 	 * in the header and we must copy the following entry up
    757   1.1   simonb 	 * to the header.  Otherwise we must search the list for
    758   1.1   simonb 	 * the entry.  In either case we free the now unused entry.
    759   1.1   simonb 	 */
    760   1.1   simonb 	if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
    761  1.30      chs 		if (PV_ISWIRED(pv)) {
    762  1.30      chs 			pm->pm_stats.wired_count--;
    763  1.30      chs 		}
    764   1.1   simonb 		if ((npv = pv->pv_next)) {
    765   1.1   simonb 			*pv = *npv;
    766   1.1   simonb 			pool_put(&pv_pool, npv);
    767   1.1   simonb 		} else
    768   1.1   simonb 			pv->pv_pm = NULL;
    769   1.1   simonb 	} else {
    770   1.1   simonb 		for (; (npv = pv->pv_next) != NULL; pv = npv)
    771   1.1   simonb 			if (pm == npv->pv_pm && PV_CMPVA(va, npv))
    772   1.1   simonb 				break;
    773   1.1   simonb 		if (npv) {
    774   1.1   simonb 			pv->pv_next = npv->pv_next;
    775  1.30      chs 			if (PV_ISWIRED(npv)) {
    776  1.30      chs 				pm->pm_stats.wired_count--;
    777  1.30      chs 			}
    778   1.1   simonb 			pool_put(&pv_pool, npv);
    779   1.1   simonb 		}
    780   1.1   simonb 	}
    781   1.1   simonb }
    782   1.1   simonb 
    783   1.1   simonb /*
    784   1.1   simonb  * Insert physical page at pa into the given pmap at virtual address va.
    785   1.1   simonb  */
    786   1.1   simonb int
    787   1.1   simonb pmap_enter(struct pmap *pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags)
    788   1.1   simonb {
    789   1.1   simonb 	int s;
    790   1.1   simonb 	u_int tte;
    791   1.1   simonb 	int managed;
    792   1.1   simonb 
    793   1.1   simonb 	/*
    794   1.1   simonb 	 * Have to remove any existing mapping first.
    795   1.1   simonb 	 */
    796  1.19  thorpej 	pmap_remove(pm, va, va + PAGE_SIZE);
    797   1.1   simonb 
    798  1.30      chs 	if (flags & PMAP_WIRED)
    799  1.30      chs 		flags |= prot;
    800   1.1   simonb 
    801   1.1   simonb 	managed = 0;
    802   1.1   simonb 	if (vm_physseg_find(atop(pa), NULL) != -1)
    803   1.1   simonb 		managed = 1;
    804   1.1   simonb 
    805   1.1   simonb 	/*
    806   1.1   simonb 	 * Generate TTE.
    807   1.1   simonb 	 */
    808  1.26      chs 	tte = TTE_PA(pa);
    809   1.1   simonb 	/* XXXX -- need to support multiple page sizes. */
    810   1.1   simonb 	tte |= TTE_SZ_16K;
    811   1.1   simonb #ifdef	DIAGNOSTIC
    812   1.1   simonb 	if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) ==
    813   1.1   simonb 		(PME_NOCACHE | PME_WRITETHROUG))
    814  1.13   provos 		panic("pmap_enter: uncached & writethrough");
    815   1.1   simonb #endif
    816   1.1   simonb 	if (flags & PME_NOCACHE)
    817   1.1   simonb 		/* Must be I/O mapping */
    818   1.1   simonb 		tte |= TTE_I | TTE_G;
    819   1.8  thorpej #ifdef PPC_4XX_NOCACHE
    820   1.1   simonb 	tte |= TTE_I;
    821   1.1   simonb #else
    822   1.1   simonb 	else if (flags & PME_WRITETHROUG)
    823   1.1   simonb 		/* Uncached and writethrough are not compatible */
    824   1.1   simonb 		tte |= TTE_W;
    825   1.1   simonb #endif
    826   1.1   simonb 	if (pm == pmap_kernel())
    827   1.1   simonb 		tte |= TTE_ZONE(ZONE_PRIV);
    828   1.1   simonb 	else
    829   1.1   simonb 		tte |= TTE_ZONE(ZONE_USER);
    830   1.1   simonb 
    831   1.1   simonb 	if (flags & VM_PROT_WRITE)
    832   1.1   simonb 		tte |= TTE_WR;
    833   1.1   simonb 
    834  1.26      chs 	if (flags & VM_PROT_EXECUTE)
    835  1.26      chs 		tte |= TTE_EX;
    836  1.26      chs 
    837   1.1   simonb 	/*
    838   1.1   simonb 	 * Now record mapping for later back-translation.
    839   1.1   simonb 	 */
    840   1.1   simonb 	if (pmap_initialized && managed) {
    841   1.1   simonb 		char *attr;
    842   1.1   simonb 
    843  1.30      chs 		if (!pmap_enter_pv(pm, va, pa, flags & PMAP_WIRED)) {
    844   1.1   simonb 			/* Could not enter pv on a managed page */
    845   1.1   simonb 			return 1;
    846   1.1   simonb 		}
    847   1.1   simonb 
    848   1.1   simonb 		/* Now set attributes. */
    849   1.1   simonb 		attr = pa_to_attr(pa);
    850   1.1   simonb #ifdef DIAGNOSTIC
    851   1.1   simonb 		if (!attr)
    852  1.13   provos 			panic("managed but no attr");
    853   1.1   simonb #endif
    854   1.1   simonb 		if (flags & VM_PROT_ALL)
    855  1.30      chs 			*attr |= PMAP_ATTR_REF;
    856   1.1   simonb 		if (flags & VM_PROT_WRITE)
    857  1.30      chs 			*attr |= PMAP_ATTR_CHG;
    858   1.1   simonb 	}
    859   1.1   simonb 
    860   1.1   simonb 	s = splvm();
    861   1.1   simonb 
    862   1.1   simonb 	/* Insert page into page table. */
    863   1.1   simonb 	pte_enter(pm, va, tte);
    864   1.1   simonb 
    865   1.1   simonb 	/* If this is a real fault, enter it in the tlb */
    866   1.1   simonb 	if (tte && ((flags & PMAP_WIRED) == 0)) {
    867   1.1   simonb 		ppc4xx_tlb_enter(pm->pm_ctx, va, tte);
    868   1.1   simonb 	}
    869   1.1   simonb 	splx(s);
    870   1.6   simonb 
    871   1.6   simonb 	/* Flush the real memory from the instruction cache. */
    872   1.6   simonb 	if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0)
    873   1.6   simonb 		__syncicache((void *)pa, PAGE_SIZE);
    874   1.6   simonb 
    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.33      chs 	struct pv_entry *pv;
    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.30      chs 	s = splvm();
    894  1.33      chs 	while (pv != NULL) {
    895  1.33      chs 		if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
    896  1.33      chs 			if (PV_ISWIRED(pv)) {
    897  1.33      chs 				PV_UNWIRE(pv);
    898  1.30      chs 				pm->pm_stats.wired_count--;
    899  1.30      chs 			}
    900   1.1   simonb 			break;
    901   1.1   simonb 		}
    902  1.33      chs 		pv = pv->pv_next;
    903   1.1   simonb 	}
    904   1.1   simonb 	splx(s);
    905   1.1   simonb }
    906   1.1   simonb 
    907   1.1   simonb void
    908   1.1   simonb pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
    909   1.1   simonb {
    910   1.1   simonb 	int s;
    911   1.1   simonb 	u_int tte;
    912   1.1   simonb 	struct pmap *pm = pmap_kernel();
    913   1.1   simonb 
    914   1.1   simonb 	/*
    915   1.1   simonb 	 * Have to remove any existing mapping first.
    916   1.1   simonb 	 */
    917   1.1   simonb 
    918   1.1   simonb 	/*
    919   1.1   simonb 	 * Generate TTE.
    920   1.1   simonb 	 *
    921   1.1   simonb 	 * XXXX
    922   1.1   simonb 	 *
    923   1.1   simonb 	 * Since the kernel does not handle execution privileges properly,
    924   1.1   simonb 	 * we will handle read and execute permissions together.
    925   1.1   simonb 	 */
    926   1.1   simonb 	tte = 0;
    927   1.1   simonb 	if (prot & VM_PROT_ALL) {
    928   1.1   simonb 
    929   1.1   simonb 		tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV);
    930   1.1   simonb 		/* XXXX -- need to support multiple page sizes. */
    931   1.1   simonb 		tte |= TTE_SZ_16K;
    932   1.1   simonb #ifdef DIAGNOSTIC
    933   1.1   simonb 		if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) ==
    934   1.1   simonb 			(PME_NOCACHE | PME_WRITETHROUG))
    935  1.13   provos 			panic("pmap_kenter_pa: uncached & writethrough");
    936   1.1   simonb #endif
    937   1.1   simonb 		if (prot & PME_NOCACHE)
    938   1.1   simonb 			/* Must be I/O mapping */
    939   1.1   simonb 			tte |= TTE_I | TTE_G;
    940   1.8  thorpej #ifdef PPC_4XX_NOCACHE
    941   1.1   simonb 		tte |= TTE_I;
    942   1.1   simonb #else
    943   1.1   simonb 		else if (prot & PME_WRITETHROUG)
    944   1.1   simonb 			/* Uncached and writethrough are not compatible */
    945   1.1   simonb 			tte |= TTE_W;
    946   1.1   simonb #endif
    947   1.1   simonb 		if (prot & VM_PROT_WRITE)
    948   1.1   simonb 			tte |= TTE_WR;
    949   1.1   simonb 	}
    950   1.1   simonb 
    951   1.1   simonb 	s = splvm();
    952   1.1   simonb 
    953   1.1   simonb 	/* Insert page into page table. */
    954   1.1   simonb 	pte_enter(pm, va, tte);
    955   1.1   simonb 	splx(s);
    956   1.1   simonb }
    957   1.1   simonb 
    958   1.1   simonb void
    959   1.1   simonb pmap_kremove(vaddr_t va, vsize_t len)
    960   1.1   simonb {
    961   1.1   simonb 
    962   1.1   simonb 	while (len > 0) {
    963   1.1   simonb 		pte_enter(pmap_kernel(), va, 0);
    964   1.1   simonb 		va += PAGE_SIZE;
    965   1.1   simonb 		len -= PAGE_SIZE;
    966   1.1   simonb 	}
    967   1.1   simonb }
    968   1.1   simonb 
    969   1.1   simonb /*
    970   1.1   simonb  * Remove the given range of mapping entries.
    971   1.1   simonb  */
    972   1.1   simonb void
    973   1.1   simonb pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva)
    974   1.1   simonb {
    975   1.1   simonb 	int s;
    976   1.1   simonb 	paddr_t pa;
    977   1.1   simonb 	volatile u_int *ptp;
    978   1.1   simonb 
    979   1.1   simonb 	s = splvm();
    980   1.1   simonb 	while (va < endva) {
    981   1.1   simonb 
    982   1.1   simonb 		if ((ptp = pte_find(pm, va)) && (pa = *ptp)) {
    983   1.1   simonb 			pa = TTE_PA(pa);
    984   1.1   simonb 			pmap_remove_pv(pm, va, pa);
    985   1.1   simonb 			*ptp = 0;
    986   1.1   simonb 			ppc4xx_tlb_flush(va, pm->pm_ctx);
    987   1.1   simonb 			pm->pm_stats.resident_count--;
    988   1.1   simonb 		}
    989  1.19  thorpej 		va += PAGE_SIZE;
    990   1.1   simonb 	}
    991   1.1   simonb 
    992   1.1   simonb 	splx(s);
    993   1.1   simonb }
    994   1.1   simonb 
    995   1.1   simonb /*
    996   1.1   simonb  * Get the physical page address for the given pmap/virtual address.
    997   1.1   simonb  */
    998   1.1   simonb boolean_t
    999   1.1   simonb pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap)
   1000   1.1   simonb {
   1001   1.1   simonb 	int seg = STIDX(va);
   1002   1.1   simonb 	int ptn = PTIDX(va);
   1003   1.1   simonb 	u_int pa = 0;
   1004  1.30      chs 	int s;
   1005   1.1   simonb 
   1006  1.30      chs 	s = splvm();
   1007   1.1   simonb 	if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) {
   1008   1.1   simonb 		*pap = TTE_PA(pa) | (va & PGOFSET);
   1009   1.1   simonb 	}
   1010   1.1   simonb 	splx(s);
   1011   1.1   simonb 	return (pa != 0);
   1012   1.1   simonb }
   1013   1.1   simonb 
   1014   1.1   simonb /*
   1015   1.1   simonb  * Lower the protection on the specified range of this pmap.
   1016   1.1   simonb  *
   1017   1.1   simonb  * There are only two cases: either the protection is going to 0,
   1018   1.1   simonb  * or it is going to read-only.
   1019   1.1   simonb  */
   1020   1.1   simonb void
   1021   1.1   simonb pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   1022   1.1   simonb {
   1023   1.1   simonb 	volatile u_int *ptp;
   1024  1.26      chs 	int s, bic;
   1025   1.1   simonb 
   1026  1.26      chs 	if ((prot & VM_PROT_READ) == 0) {
   1027  1.26      chs 		pmap_remove(pm, sva, eva);
   1028  1.26      chs 		return;
   1029  1.26      chs 	}
   1030  1.26      chs 	bic = 0;
   1031  1.26      chs 	if ((prot & VM_PROT_WRITE) == 0) {
   1032  1.26      chs 		bic |= TTE_WR;
   1033  1.26      chs 	}
   1034  1.26      chs 	if ((prot & VM_PROT_EXECUTE) == 0) {
   1035  1.26      chs 		bic |= TTE_EX;
   1036  1.26      chs 	}
   1037  1.26      chs 	if (bic == 0) {
   1038  1.26      chs 		return;
   1039  1.26      chs 	}
   1040  1.26      chs 	s = splvm();
   1041  1.26      chs 	while (sva < eva) {
   1042  1.26      chs 		if ((ptp = pte_find(pm, sva)) != NULL) {
   1043  1.26      chs 			*ptp &= ~bic;
   1044  1.26      chs 			ppc4xx_tlb_flush(sva, pm->pm_ctx);
   1045   1.1   simonb 		}
   1046  1.26      chs 		sva += PAGE_SIZE;
   1047   1.1   simonb 	}
   1048  1.26      chs 	splx(s);
   1049   1.1   simonb }
   1050   1.1   simonb 
   1051   1.1   simonb boolean_t
   1052  1.30      chs pmap_check_attr(struct vm_page *pg, u_int mask, int clear)
   1053   1.1   simonb {
   1054  1.30      chs 	paddr_t pa;
   1055   1.1   simonb 	char *attr;
   1056  1.30      chs 	int s, rv;
   1057   1.1   simonb 
   1058   1.1   simonb 	/*
   1059   1.1   simonb 	 * First modify bits in cache.
   1060   1.1   simonb 	 */
   1061  1.30      chs 	pa = VM_PAGE_TO_PHYS(pg);
   1062   1.1   simonb 	attr = pa_to_attr(pa);
   1063   1.1   simonb 	if (attr == NULL)
   1064   1.1   simonb 		return FALSE;
   1065   1.1   simonb 
   1066  1.30      chs 	s = splvm();
   1067   1.1   simonb 	rv = ((*attr & mask) != 0);
   1068  1.11      eeh 	if (clear) {
   1069   1.1   simonb 		*attr &= ~mask;
   1070  1.30      chs 		pmap_page_protect(pg, mask == PMAP_ATTR_CHG ? VM_PROT_READ : 0);
   1071  1.11      eeh 	}
   1072   1.1   simonb 	splx(s);
   1073   1.1   simonb 	return rv;
   1074   1.1   simonb }
   1075   1.1   simonb 
   1076   1.1   simonb 
   1077   1.1   simonb /*
   1078   1.1   simonb  * Lower the protection on the specified physical page.
   1079   1.1   simonb  *
   1080   1.1   simonb  * There are only two cases: either the protection is going to 0,
   1081   1.1   simonb  * or it is going to read-only.
   1082   1.1   simonb  */
   1083   1.1   simonb void
   1084   1.1   simonb pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   1085   1.1   simonb {
   1086   1.1   simonb 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1087   1.1   simonb 	vaddr_t va;
   1088   1.1   simonb 	struct pv_entry *pvh, *pv, *npv;
   1089   1.1   simonb 	struct pmap *pm;
   1090   1.1   simonb 
   1091   1.1   simonb 	pvh = pa_to_pv(pa);
   1092   1.1   simonb 	if (pvh == NULL)
   1093   1.1   simonb 		return;
   1094   1.1   simonb 
   1095   1.1   simonb 	/* Handle extra pvs which may be deleted in the operation */
   1096   1.1   simonb 	for (pv = pvh->pv_next; pv; pv = npv) {
   1097   1.1   simonb 		npv = pv->pv_next;
   1098   1.1   simonb 
   1099   1.1   simonb 		pm = pv->pv_pm;
   1100   1.1   simonb 		va = pv->pv_va;
   1101  1.26      chs 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1102   1.1   simonb 	}
   1103   1.1   simonb 	/* Now check the head pv */
   1104   1.1   simonb 	if (pvh->pv_pm) {
   1105   1.1   simonb 		pv = pvh;
   1106   1.1   simonb 		pm = pv->pv_pm;
   1107   1.1   simonb 		va = pv->pv_va;
   1108  1.26      chs 		pmap_protect(pm, va, va + PAGE_SIZE, prot);
   1109   1.1   simonb 	}
   1110   1.1   simonb }
   1111   1.1   simonb 
   1112   1.1   simonb /*
   1113   1.1   simonb  * Activate the address space for the specified process.  If the process
   1114   1.1   simonb  * is the current process, load the new MMU context.
   1115   1.1   simonb  */
   1116   1.1   simonb void
   1117  1.17  thorpej pmap_activate(struct lwp *l)
   1118   1.1   simonb {
   1119   1.1   simonb #if 0
   1120  1.17  thorpej 	struct pcb *pcb = &l->l_proc->p_addr->u_pcb;
   1121  1.17  thorpej 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
   1122   1.1   simonb 
   1123   1.1   simonb 	/*
   1124   1.1   simonb 	 * XXX Normally performed in cpu_fork().
   1125   1.1   simonb 	 */
   1126  1.17  thorpej 	printf("pmap_activate(%p), pmap=%p\n",l,pmap);
   1127  1.25     matt 	pcb->pcb_pm = pmap;
   1128   1.1   simonb #endif
   1129   1.1   simonb }
   1130   1.1   simonb 
   1131   1.1   simonb /*
   1132   1.1   simonb  * Deactivate the specified process's address space.
   1133   1.1   simonb  */
   1134   1.1   simonb void
   1135  1.17  thorpej pmap_deactivate(struct lwp *l)
   1136   1.1   simonb {
   1137   1.1   simonb }
   1138   1.1   simonb 
   1139   1.1   simonb /*
   1140   1.1   simonb  * Synchronize caches corresponding to [addr, addr+len) in p.
   1141   1.1   simonb  */
   1142   1.1   simonb void
   1143   1.1   simonb pmap_procwr(struct proc *p, vaddr_t va, size_t len)
   1144   1.1   simonb {
   1145   1.1   simonb 	struct pmap *pm = p->p_vmspace->vm_map.pmap;
   1146  1.18  hannken 	int msr, ctx, opid, step;
   1147  1.18  hannken 
   1148  1.18  hannken 	step = CACHELINESIZE;
   1149   1.1   simonb 
   1150   1.1   simonb 	/*
   1151   1.1   simonb 	 * Need to turn off IMMU and switch to user context.
   1152   1.1   simonb 	 * (icbi uses DMMU).
   1153   1.1   simonb 	 */
   1154   1.1   simonb 	if (!(ctx = pm->pm_ctx)) {
   1155   1.1   simonb 		/* No context -- assign it one */
   1156   1.1   simonb 		ctx_alloc(pm);
   1157   1.1   simonb 		ctx = pm->pm_ctx;
   1158   1.1   simonb 	}
   1159   1.1   simonb 	__asm __volatile("mfmsr %0;"
   1160  1.27   simonb 		"li %1, %7;"
   1161   1.1   simonb 		"andc %1,%0,%1;"
   1162   1.1   simonb 		"mtmsr %1;"
   1163   1.1   simonb 		"sync;isync;"
   1164   1.1   simonb 		"mfpid %1;"
   1165   1.1   simonb 		"mtpid %2;"
   1166   1.1   simonb 		"sync; isync;"
   1167  1.12   simonb 		"1:"
   1168   1.1   simonb 		"dcbf 0,%3;"
   1169   1.1   simonb 		"icbi 0,%3;"
   1170  1.18  hannken 		"add %3,%3,%5;"
   1171  1.18  hannken 		"addc. %4,%4,%6;"
   1172   1.1   simonb 		"bge 1b;"
   1173   1.1   simonb 		"mtpid %1;"
   1174   1.1   simonb 		"mtmsr %0;"
   1175   1.1   simonb 		"sync; isync"
   1176   1.1   simonb 		: "=&r" (msr), "=&r" (opid)
   1177  1.27   simonb 		: "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step),
   1178  1.27   simonb 		  "K" (PSL_IR | PSL_DR));
   1179   1.1   simonb }
   1180   1.1   simonb 
   1181   1.1   simonb 
   1182   1.1   simonb /* This has to be done in real mode !!! */
   1183   1.1   simonb void
   1184   1.1   simonb ppc4xx_tlb_flush(vaddr_t va, int pid)
   1185   1.1   simonb {
   1186   1.1   simonb 	u_long i, found;
   1187   1.1   simonb 	u_long msr;
   1188   1.1   simonb 
   1189   1.1   simonb 	/* If there's no context then it can't be mapped. */
   1190  1.26      chs 	if (!pid)
   1191  1.26      chs 		return;
   1192   1.1   simonb 
   1193   1.1   simonb 	asm("mfpid %1;"			/* Save PID */
   1194   1.1   simonb 		"mfmsr %2;"		/* Save MSR */
   1195   1.1   simonb 		"li %0,0;"		/* Now clear MSR */
   1196   1.1   simonb 		"mtmsr %0;"
   1197   1.1   simonb 		"mtpid %4;"		/* Set PID */
   1198   1.1   simonb 		"sync;"
   1199   1.1   simonb 		"tlbsx. %0,0,%3;"	/* Search TLB */
   1200   1.1   simonb 		"sync;"
   1201   1.1   simonb 		"mtpid %1;"		/* Restore PID */
   1202   1.1   simonb 		"mtmsr %2;"		/* Restore MSR */
   1203   1.1   simonb 		"sync;isync;"
   1204   1.1   simonb 		"li %1,1;"
   1205   1.1   simonb 		"beq 1f;"
   1206   1.1   simonb 		"li %1,0;"
   1207   1.1   simonb 		"1:"
   1208   1.1   simonb 		: "=&r" (i), "=&r" (found), "=&r" (msr)
   1209   1.1   simonb 		: "r" (va), "r" (pid));
   1210   1.1   simonb 	if (found && !TLB_LOCKED(i)) {
   1211   1.1   simonb 
   1212   1.1   simonb 		/* Now flush translation */
   1213   1.1   simonb 		asm volatile(
   1214   1.1   simonb 			"tlbwe %0,%1,0;"
   1215   1.1   simonb 			"sync;isync;"
   1216   1.1   simonb 			: : "r" (0), "r" (i));
   1217   1.1   simonb 
   1218   1.1   simonb 		tlb_info[i].ti_ctx = 0;
   1219   1.1   simonb 		tlb_info[i].ti_flags = 0;
   1220   1.1   simonb 		tlbnext = i;
   1221   1.1   simonb 		/* Successful flushes */
   1222   1.1   simonb 		tlbflush_ev.ev_count++;
   1223   1.1   simonb 	}
   1224   1.1   simonb }
   1225   1.1   simonb 
   1226   1.1   simonb void
   1227   1.1   simonb ppc4xx_tlb_flush_all(void)
   1228   1.1   simonb {
   1229   1.1   simonb 	u_long i;
   1230   1.1   simonb 
   1231   1.1   simonb 	for (i = 0; i < NTLB; i++)
   1232   1.1   simonb 		if (!TLB_LOCKED(i)) {
   1233   1.1   simonb 			asm volatile(
   1234   1.1   simonb 				"tlbwe %0,%1,0;"
   1235   1.1   simonb 				"sync;isync;"
   1236   1.1   simonb 				: : "r" (0), "r" (i));
   1237   1.1   simonb 			tlb_info[i].ti_ctx = 0;
   1238   1.1   simonb 			tlb_info[i].ti_flags = 0;
   1239   1.1   simonb 		}
   1240   1.1   simonb 
   1241   1.1   simonb 	asm volatile("sync;isync");
   1242   1.1   simonb }
   1243   1.1   simonb 
   1244   1.1   simonb /* Find a TLB entry to evict. */
   1245   1.1   simonb static int
   1246   1.1   simonb ppc4xx_tlb_find_victim(void)
   1247   1.1   simonb {
   1248   1.1   simonb 	int flags;
   1249   1.1   simonb 
   1250   1.1   simonb 	for (;;) {
   1251   1.1   simonb 		if (++tlbnext >= NTLB)
   1252   1.1   simonb 			tlbnext = TLB_NRESERVED;
   1253   1.1   simonb 		flags = tlb_info[tlbnext].ti_flags;
   1254  1.12   simonb 		if (!(flags & TLBF_USED) ||
   1255   1.1   simonb 			(flags & (TLBF_LOCKED | TLBF_REF)) == 0) {
   1256   1.1   simonb 			u_long va, stack = (u_long)&va;
   1257   1.1   simonb 
   1258   1.1   simonb 			if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) &&
   1259   1.1   simonb 			    (tlb_info[tlbnext].ti_ctx == KERNEL_PID) &&
   1260   1.1   simonb 			     (flags & TLBF_USED)) {
   1261   1.1   simonb 				/* Kernel stack page */
   1262   1.1   simonb 				flags |= TLBF_USED;
   1263   1.1   simonb 				tlb_info[tlbnext].ti_flags = flags;
   1264   1.1   simonb 			} else {
   1265   1.1   simonb 				/* Found it! */
   1266   1.1   simonb 				return (tlbnext);
   1267   1.1   simonb 			}
   1268   1.1   simonb 		} else {
   1269   1.1   simonb 			tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF);
   1270   1.1   simonb 		}
   1271   1.1   simonb 	}
   1272   1.1   simonb }
   1273   1.1   simonb 
   1274   1.1   simonb void
   1275   1.1   simonb ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte)
   1276   1.1   simonb {
   1277   1.1   simonb 	u_long th, tl, idx;
   1278   1.1   simonb 	tlbpid_t pid;
   1279   1.1   simonb 	u_short msr;
   1280  1.10      eeh 	paddr_t pa;
   1281  1.10      eeh 	int s, sz;
   1282  1.10      eeh 
   1283   1.1   simonb 	tlbenter_ev.ev_count++;
   1284   1.1   simonb 
   1285  1.10      eeh 	sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT;
   1286  1.10      eeh 	pa = (pte & TTE_RPN_MASK(sz));
   1287  1.10      eeh 	th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID;
   1288  1.10      eeh 	tl = (pte & ~TLB_RPN_MASK) | pa;
   1289  1.10      eeh 	tl |= ppc4xx_tlbflags(va, pa);
   1290   1.1   simonb 
   1291   1.1   simonb 	s = splhigh();
   1292   1.1   simonb 	idx = ppc4xx_tlb_find_victim();
   1293   1.1   simonb 
   1294   1.1   simonb #ifdef DIAGNOSTIC
   1295   1.1   simonb 	if ((idx < TLB_NRESERVED) || (idx >= NTLB)) {
   1296  1.31   simonb 		panic("ppc4xx_tlb_enter: replacing entry %ld", idx);
   1297   1.1   simonb 	}
   1298   1.1   simonb #endif
   1299  1.12   simonb 
   1300   1.1   simonb 	tlb_info[idx].ti_va = (va & TLB_EPN_MASK);
   1301   1.1   simonb 	tlb_info[idx].ti_ctx = ctx;
   1302   1.1   simonb 	tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF;
   1303   1.1   simonb 
   1304   1.1   simonb 	asm volatile(
   1305   1.1   simonb 		"mfmsr %0;"			/* Save MSR */
   1306   1.1   simonb 		"li %1,0;"
   1307   1.1   simonb 		"tlbwe %1,%3,0;"		/* Invalidate old entry. */
   1308   1.1   simonb 		"mtmsr %1;"			/* Clear MSR */
   1309   1.1   simonb 		"mfpid %1;"			/* Save old PID */
   1310   1.1   simonb 		"mtpid %2;"			/* Load translation ctx */
   1311   1.1   simonb 		"sync; isync;"
   1312   1.1   simonb #ifdef DEBUG
   1313   1.1   simonb 		"andi. %3,%3,63;"
   1314   1.1   simonb 		"tweqi %3,0;" 			/* XXXXX DEBUG trap on index 0 */
   1315   1.1   simonb #endif
   1316   1.1   simonb 		"tlbwe %4,%3,1; tlbwe %5,%3,0;"	/* Set TLB */
   1317   1.1   simonb 		"sync; isync;"
   1318   1.1   simonb 		"mtpid %1; mtmsr %0;"		/* Restore PID and MSR */
   1319   1.1   simonb 		"sync; isync;"
   1320   1.1   simonb 	: "=&r" (msr), "=&r" (pid)
   1321   1.1   simonb 	: "r" (ctx), "r" (idx), "r" (tl), "r" (th));
   1322   1.1   simonb 	splx(s);
   1323   1.1   simonb }
   1324   1.1   simonb 
   1325   1.1   simonb void
   1326   1.1   simonb ppc4xx_tlb_unpin(int i)
   1327   1.1   simonb {
   1328   1.1   simonb 
   1329   1.1   simonb 	if (i == -1)
   1330   1.1   simonb 		for (i = 0; i < TLB_NRESERVED; i++)
   1331   1.1   simonb 			tlb_info[i].ti_flags &= ~TLBF_LOCKED;
   1332   1.1   simonb 	else
   1333   1.1   simonb 		tlb_info[i].ti_flags &= ~TLBF_LOCKED;
   1334   1.1   simonb }
   1335   1.1   simonb 
   1336   1.1   simonb void
   1337   1.1   simonb ppc4xx_tlb_init(void)
   1338   1.1   simonb {
   1339   1.1   simonb 	int i;
   1340   1.1   simonb 
   1341   1.1   simonb 	/* Mark reserved TLB entries */
   1342   1.1   simonb 	for (i = 0; i < TLB_NRESERVED; i++) {
   1343   1.1   simonb 		tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED;
   1344   1.1   simonb 		tlb_info[i].ti_ctx = KERNEL_PID;
   1345   1.1   simonb 	}
   1346   1.1   simonb 
   1347   1.1   simonb 	/* Setup security zones */
   1348   1.1   simonb 	/* Z0 - accessible by kernel only if TLB entry permissions allow
   1349   1.1   simonb 	 * Z1,Z2 - access is controlled by TLB entry permissions
   1350   1.1   simonb 	 * Z3 - full access regardless of TLB entry permissions
   1351   1.1   simonb 	 */
   1352   1.1   simonb 
   1353   1.1   simonb 	asm volatile(
   1354   1.1   simonb 		"mtspr %0,%1;"
   1355   1.1   simonb 		"sync;"
   1356   1.1   simonb 		::  "K"(SPR_ZPR), "r" (0x1b000000));
   1357   1.1   simonb }
   1358   1.1   simonb 
   1359   1.1   simonb 
   1360   1.1   simonb /*
   1361   1.1   simonb  * We should pass the ctx in from trap code.
   1362   1.1   simonb  */
   1363   1.1   simonb int
   1364   1.1   simonb pmap_tlbmiss(vaddr_t va, int ctx)
   1365   1.1   simonb {
   1366   1.1   simonb 	volatile u_int *pte;
   1367   1.1   simonb 	u_long tte;
   1368   1.1   simonb 
   1369   1.1   simonb 	tlbmiss_ev.ev_count++;
   1370   1.1   simonb 
   1371   1.1   simonb 	/*
   1372   1.1   simonb 	 * XXXX We will reserve 0-0x80000000 for va==pa mappings.
   1373   1.1   simonb 	 */
   1374   1.1   simonb 	if (ctx != KERNEL_PID || (va & 0x80000000)) {
   1375   1.1   simonb 		pte = pte_find((struct pmap *)ctxbusy[ctx], va);
   1376   1.1   simonb 		if (pte == NULL) {
   1377   1.1   simonb 			/* Map unmanaged addresses directly for kernel access */
   1378   1.1   simonb 			return 1;
   1379   1.1   simonb 		}
   1380   1.1   simonb 		tte = *pte;
   1381   1.1   simonb 		if (tte == 0) {
   1382   1.1   simonb 			return 1;
   1383   1.1   simonb 		}
   1384   1.1   simonb 	} else {
   1385  1.16      wiz 		/* Create a 16MB writable mapping. */
   1386   1.8  thorpej #ifdef PPC_4XX_NOCACHE
   1387   1.1   simonb 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I | TTE_WR;
   1388   1.1   simonb #else
   1389   1.1   simonb 		tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR;
   1390   1.1   simonb #endif
   1391   1.1   simonb 	}
   1392   1.1   simonb 	tlbhit_ev.ev_count++;
   1393   1.1   simonb 	ppc4xx_tlb_enter(ctx, va, tte);
   1394   1.1   simonb 
   1395   1.1   simonb 	return 0;
   1396   1.1   simonb }
   1397   1.1   simonb 
   1398   1.1   simonb /*
   1399   1.1   simonb  * Flush all the entries matching a context from the TLB.
   1400   1.1   simonb  */
   1401   1.1   simonb static int
   1402   1.1   simonb ctx_flush(int cnum)
   1403   1.1   simonb {
   1404   1.1   simonb 	int i;
   1405   1.1   simonb 
   1406   1.1   simonb 	/* We gotta steal this context */
   1407   1.1   simonb 	for (i = TLB_NRESERVED; i < NTLB; i++) {
   1408   1.1   simonb 		if (tlb_info[i].ti_ctx == cnum) {
   1409   1.1   simonb 			/* Can't steal ctx if it has a locked entry. */
   1410   1.1   simonb 			if (TLB_LOCKED(i)) {
   1411   1.1   simonb #ifdef DIAGNOSTIC
   1412   1.1   simonb 				printf("ctx_flush: can't invalidate "
   1413   1.1   simonb 					"locked mapping %d "
   1414   1.1   simonb 					"for context %d\n", i, cnum);
   1415  1.10      eeh #ifdef DDB
   1416   1.1   simonb 				Debugger();
   1417   1.1   simonb #endif
   1418  1.10      eeh #endif
   1419   1.1   simonb 				return (1);
   1420   1.1   simonb 			}
   1421   1.1   simonb #ifdef DIAGNOSTIC
   1422   1.1   simonb 			if (i < TLB_NRESERVED)
   1423  1.13   provos 				panic("TLB entry %d not locked", i);
   1424   1.1   simonb #endif
   1425   1.1   simonb 			/* Invalidate particular TLB entry regardless of locked status */
   1426   1.1   simonb 			asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i));
   1427   1.1   simonb 			tlb_info[i].ti_flags = 0;
   1428   1.1   simonb 		}
   1429   1.1   simonb 	}
   1430   1.1   simonb 	return (0);
   1431   1.1   simonb }
   1432   1.1   simonb 
   1433   1.1   simonb /*
   1434   1.1   simonb  * Allocate a context.  If necessary, steal one from someone else.
   1435   1.1   simonb  *
   1436   1.1   simonb  * The new context is flushed from the TLB before returning.
   1437   1.1   simonb  */
   1438   1.1   simonb int
   1439   1.1   simonb ctx_alloc(struct pmap *pm)
   1440   1.1   simonb {
   1441   1.1   simonb 	int s, cnum;
   1442   1.1   simonb 	static int next = MINCTX;
   1443   1.1   simonb 
   1444   1.1   simonb 	if (pm == pmap_kernel()) {
   1445   1.1   simonb #ifdef DIAGNOSTIC
   1446   1.1   simonb 		printf("ctx_alloc: kernel pmap!\n");
   1447   1.1   simonb #endif
   1448   1.1   simonb 		return (0);
   1449   1.1   simonb 	}
   1450   1.1   simonb 	s = splvm();
   1451   1.1   simonb 
   1452   1.1   simonb 	/* Find a likely context. */
   1453   1.1   simonb 	cnum = next;
   1454   1.1   simonb 	do {
   1455   1.1   simonb 		if ((++cnum) > NUMCTX)
   1456   1.1   simonb 			cnum = MINCTX;
   1457   1.1   simonb 	} while (ctxbusy[cnum] != NULL && cnum != next);
   1458   1.1   simonb 
   1459   1.1   simonb 	/* Now clean it out */
   1460   1.1   simonb oops:
   1461   1.1   simonb 	if (cnum < MINCTX)
   1462   1.1   simonb 		cnum = MINCTX; /* Never steal ctx 0 or 1 */
   1463   1.1   simonb 	if (ctx_flush(cnum)) {
   1464   1.1   simonb 		/* oops -- something's wired. */
   1465   1.1   simonb 		if ((++cnum) > NUMCTX)
   1466   1.1   simonb 			cnum = MINCTX;
   1467   1.1   simonb 		goto oops;
   1468   1.1   simonb 	}
   1469   1.1   simonb 
   1470   1.1   simonb 	if (ctxbusy[cnum]) {
   1471   1.1   simonb #ifdef DEBUG
   1472   1.1   simonb 		/* We should identify this pmap and clear it */
   1473   1.1   simonb 		printf("Warning: stealing context %d\n", cnum);
   1474   1.1   simonb #endif
   1475   1.1   simonb 		ctxbusy[cnum]->pm_ctx = 0;
   1476   1.1   simonb 	}
   1477   1.1   simonb 	ctxbusy[cnum] = pm;
   1478   1.1   simonb 	next = cnum;
   1479   1.1   simonb 	splx(s);
   1480   1.1   simonb 	pm->pm_ctx = cnum;
   1481   1.1   simonb 
   1482   1.1   simonb 	return cnum;
   1483   1.1   simonb }
   1484   1.1   simonb 
   1485   1.1   simonb /*
   1486   1.1   simonb  * Give away a context.
   1487   1.1   simonb  */
   1488   1.1   simonb void
   1489   1.1   simonb ctx_free(struct pmap *pm)
   1490   1.1   simonb {
   1491   1.1   simonb 	int oldctx;
   1492   1.1   simonb 
   1493   1.1   simonb 	oldctx = pm->pm_ctx;
   1494   1.1   simonb 
   1495   1.1   simonb 	if (oldctx == 0)
   1496   1.1   simonb 		panic("ctx_free: freeing kernel context");
   1497   1.1   simonb #ifdef DIAGNOSTIC
   1498   1.1   simonb 	if (ctxbusy[oldctx] == 0)
   1499   1.1   simonb 		printf("ctx_free: freeing free context %d\n", oldctx);
   1500   1.1   simonb 	if (ctxbusy[oldctx] != pm) {
   1501   1.1   simonb 		printf("ctx_free: freeing someone esle's context\n "
   1502   1.1   simonb 		       "ctxbusy[%d] = %p, pm->pm_ctx = %p\n",
   1503   1.1   simonb 		       oldctx, (void *)(u_long)ctxbusy[oldctx], pm);
   1504  1.10      eeh #ifdef DDB
   1505   1.1   simonb 		Debugger();
   1506  1.10      eeh #endif
   1507   1.1   simonb 	}
   1508   1.1   simonb #endif
   1509   1.1   simonb 	/* We should verify it has not been stolen and reallocated... */
   1510   1.1   simonb 	ctxbusy[oldctx] = NULL;
   1511   1.1   simonb 	ctx_flush(oldctx);
   1512   1.1   simonb }
   1513   1.5      eeh 
   1514   1.1   simonb 
   1515   1.1   simonb #ifdef DEBUG
   1516   1.1   simonb /*
   1517   1.1   simonb  * Test ref/modify handling.
   1518   1.1   simonb  */
   1519   1.1   simonb void pmap_testout __P((void));
   1520   1.1   simonb void
   1521   1.1   simonb pmap_testout()
   1522   1.1   simonb {
   1523   1.1   simonb 	vaddr_t va;
   1524   1.1   simonb 	volatile int *loc;
   1525   1.1   simonb 	int val = 0;
   1526   1.1   simonb 	paddr_t pa;
   1527   1.1   simonb 	struct vm_page *pg;
   1528   1.1   simonb 	int ref, mod;
   1529   1.1   simonb 
   1530   1.1   simonb 	/* Allocate a page */
   1531  1.34     yamt 	va = (vaddr_t)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   1532  1.34     yamt 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
   1533   1.1   simonb 	loc = (int*)va;
   1534   1.1   simonb 
   1535   1.1   simonb 	pmap_extract(pmap_kernel(), va, &pa);
   1536   1.1   simonb 	pg = PHYS_TO_VM_PAGE(pa);
   1537   1.1   simonb 	pmap_unwire(pmap_kernel(), va);
   1538   1.1   simonb 
   1539  1.34     yamt 	pmap_kremove(va, PAGE_SIZE);
   1540   1.1   simonb 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1541   1.4    chris 	pmap_update(pmap_kernel());
   1542   1.1   simonb 
   1543   1.1   simonb 	/* Now clear reference and modify */
   1544   1.1   simonb 	ref = pmap_clear_reference(pg);
   1545   1.1   simonb 	mod = pmap_clear_modify(pg);
   1546   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1547   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1548   1.1   simonb 	       ref, mod);
   1549   1.1   simonb 
   1550   1.1   simonb 	/* Check it's properly cleared */
   1551   1.1   simonb 	ref = pmap_is_referenced(pg);
   1552   1.1   simonb 	mod = pmap_is_modified(pg);
   1553   1.1   simonb 	printf("Checking cleared page: ref %d, mod %d\n",
   1554   1.1   simonb 	       ref, mod);
   1555   1.1   simonb 
   1556   1.1   simonb 	/* Reference page */
   1557   1.1   simonb 	val = *loc;
   1558   1.1   simonb 
   1559   1.1   simonb 	ref = pmap_is_referenced(pg);
   1560   1.1   simonb 	mod = pmap_is_modified(pg);
   1561   1.1   simonb 	printf("Referenced page: ref %d, mod %d val %x\n",
   1562   1.1   simonb 	       ref, mod, val);
   1563   1.1   simonb 
   1564   1.1   simonb 	/* Now clear reference and modify */
   1565   1.1   simonb 	ref = pmap_clear_reference(pg);
   1566   1.1   simonb 	mod = pmap_clear_modify(pg);
   1567   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1568   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1569   1.1   simonb 	       ref, mod);
   1570  1.12   simonb 
   1571   1.1   simonb 	/* Modify page */
   1572   1.1   simonb 	*loc = 1;
   1573   1.1   simonb 
   1574   1.1   simonb 	ref = pmap_is_referenced(pg);
   1575   1.1   simonb 	mod = pmap_is_modified(pg);
   1576   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1577   1.1   simonb 	       ref, mod);
   1578   1.1   simonb 
   1579   1.1   simonb 	/* Now clear reference and modify */
   1580   1.1   simonb 	ref = pmap_clear_reference(pg);
   1581   1.1   simonb 	mod = pmap_clear_modify(pg);
   1582   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1583   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1584   1.1   simonb 	       ref, mod);
   1585   1.1   simonb 
   1586   1.1   simonb 	/* Check it's properly cleared */
   1587   1.1   simonb 	ref = pmap_is_referenced(pg);
   1588   1.1   simonb 	mod = pmap_is_modified(pg);
   1589   1.1   simonb 	printf("Checking cleared page: ref %d, mod %d\n",
   1590   1.1   simonb 	       ref, mod);
   1591   1.1   simonb 
   1592   1.1   simonb 	/* Modify page */
   1593   1.1   simonb 	*loc = 1;
   1594   1.1   simonb 
   1595   1.1   simonb 	ref = pmap_is_referenced(pg);
   1596   1.1   simonb 	mod = pmap_is_modified(pg);
   1597   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1598   1.1   simonb 	       ref, mod);
   1599   1.1   simonb 
   1600   1.1   simonb 	/* Check pmap_protect() */
   1601   1.1   simonb 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ);
   1602   1.4    chris 	pmap_update(pmap_kernel());
   1603   1.1   simonb 	ref = pmap_is_referenced(pg);
   1604   1.1   simonb 	mod = pmap_is_modified(pg);
   1605   1.1   simonb 	printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n",
   1606   1.1   simonb 	       ref, mod);
   1607   1.1   simonb 
   1608   1.1   simonb 	/* Now clear reference and modify */
   1609   1.1   simonb 	ref = pmap_clear_reference(pg);
   1610   1.1   simonb 	mod = pmap_clear_modify(pg);
   1611   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1612   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1613   1.1   simonb 	       ref, mod);
   1614   1.1   simonb 
   1615   1.1   simonb 	/* Reference page */
   1616   1.1   simonb 	val = *loc;
   1617   1.1   simonb 
   1618   1.1   simonb 	ref = pmap_is_referenced(pg);
   1619   1.1   simonb 	mod = pmap_is_modified(pg);
   1620   1.1   simonb 	printf("Referenced page: ref %d, mod %d val %x\n",
   1621   1.1   simonb 	       ref, mod, val);
   1622   1.1   simonb 
   1623   1.1   simonb 	/* Now clear reference and modify */
   1624   1.1   simonb 	ref = pmap_clear_reference(pg);
   1625   1.1   simonb 	mod = pmap_clear_modify(pg);
   1626   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1627   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1628   1.1   simonb 	       ref, mod);
   1629  1.12   simonb 
   1630   1.1   simonb 	/* Modify page */
   1631   1.1   simonb #if 0
   1632   1.1   simonb 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1633   1.4    chris 	pmap_update(pmap_kernel());
   1634   1.1   simonb #endif
   1635   1.1   simonb 	*loc = 1;
   1636   1.1   simonb 
   1637   1.1   simonb 	ref = pmap_is_referenced(pg);
   1638   1.1   simonb 	mod = pmap_is_modified(pg);
   1639   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1640   1.1   simonb 	       ref, mod);
   1641   1.1   simonb 
   1642   1.1   simonb 	/* Check pmap_protect() */
   1643   1.1   simonb 	pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE);
   1644   1.4    chris 	pmap_update(pmap_kernel());
   1645   1.1   simonb 	ref = pmap_is_referenced(pg);
   1646   1.1   simonb 	mod = pmap_is_modified(pg);
   1647   1.1   simonb 	printf("pmap_protect(): ref %d, mod %d\n",
   1648   1.1   simonb 	       ref, mod);
   1649   1.1   simonb 
   1650   1.1   simonb 	/* Now clear reference and modify */
   1651   1.1   simonb 	ref = pmap_clear_reference(pg);
   1652   1.1   simonb 	mod = pmap_clear_modify(pg);
   1653   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1654   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1655   1.1   simonb 	       ref, mod);
   1656   1.1   simonb 
   1657   1.1   simonb 	/* Reference page */
   1658   1.1   simonb 	val = *loc;
   1659   1.1   simonb 
   1660   1.1   simonb 	ref = pmap_is_referenced(pg);
   1661   1.1   simonb 	mod = pmap_is_modified(pg);
   1662   1.1   simonb 	printf("Referenced page: ref %d, mod %d val %x\n",
   1663   1.1   simonb 	       ref, mod, val);
   1664   1.1   simonb 
   1665   1.1   simonb 	/* Now clear reference and modify */
   1666   1.1   simonb 	ref = pmap_clear_reference(pg);
   1667   1.1   simonb 	mod = pmap_clear_modify(pg);
   1668   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1669   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1670   1.1   simonb 	       ref, mod);
   1671  1.12   simonb 
   1672   1.1   simonb 	/* Modify page */
   1673   1.1   simonb #if 0
   1674   1.1   simonb 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1675   1.4    chris 	pmap_update(pmap_kernel());
   1676   1.1   simonb #endif
   1677   1.1   simonb 	*loc = 1;
   1678   1.1   simonb 
   1679   1.1   simonb 	ref = pmap_is_referenced(pg);
   1680   1.1   simonb 	mod = pmap_is_modified(pg);
   1681   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1682   1.1   simonb 	       ref, mod);
   1683   1.1   simonb 
   1684   1.1   simonb 	/* Check pmap_pag_protect() */
   1685   1.1   simonb 	pmap_page_protect(pg, VM_PROT_READ);
   1686   1.1   simonb 	ref = pmap_is_referenced(pg);
   1687   1.1   simonb 	mod = pmap_is_modified(pg);
   1688   1.1   simonb 	printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n",
   1689   1.1   simonb 	       ref, mod);
   1690   1.1   simonb 
   1691   1.1   simonb 	/* Now clear reference and modify */
   1692   1.1   simonb 	ref = pmap_clear_reference(pg);
   1693   1.1   simonb 	mod = pmap_clear_modify(pg);
   1694   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1695   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1696   1.1   simonb 	       ref, mod);
   1697   1.1   simonb 
   1698   1.1   simonb 	/* Reference page */
   1699   1.1   simonb 	val = *loc;
   1700   1.1   simonb 
   1701   1.1   simonb 	ref = pmap_is_referenced(pg);
   1702   1.1   simonb 	mod = pmap_is_modified(pg);
   1703   1.1   simonb 	printf("Referenced page: ref %d, mod %d val %x\n",
   1704   1.1   simonb 	       ref, mod, val);
   1705   1.1   simonb 
   1706   1.1   simonb 	/* Now clear reference and modify */
   1707   1.1   simonb 	ref = pmap_clear_reference(pg);
   1708   1.1   simonb 	mod = pmap_clear_modify(pg);
   1709   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1710   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1711   1.1   simonb 	       ref, mod);
   1712  1.12   simonb 
   1713   1.1   simonb 	/* Modify page */
   1714   1.1   simonb #if 0
   1715   1.1   simonb 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1716   1.4    chris 	pmap_update(pmap_kernel());
   1717   1.1   simonb #endif
   1718   1.1   simonb 	*loc = 1;
   1719   1.1   simonb 
   1720   1.1   simonb 	ref = pmap_is_referenced(pg);
   1721   1.1   simonb 	mod = pmap_is_modified(pg);
   1722   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1723   1.1   simonb 	       ref, mod);
   1724   1.1   simonb 
   1725   1.1   simonb 	/* Check pmap_pag_protect() */
   1726   1.1   simonb 	pmap_page_protect(pg, VM_PROT_NONE);
   1727   1.1   simonb 	ref = pmap_is_referenced(pg);
   1728   1.1   simonb 	mod = pmap_is_modified(pg);
   1729   1.1   simonb 	printf("pmap_page_protect(): ref %d, mod %d\n",
   1730   1.1   simonb 	       ref, mod);
   1731   1.1   simonb 
   1732   1.1   simonb 	/* Now clear reference and modify */
   1733   1.1   simonb 	ref = pmap_clear_reference(pg);
   1734   1.1   simonb 	mod = pmap_clear_modify(pg);
   1735   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1736   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1737   1.1   simonb 	       ref, mod);
   1738   1.1   simonb 
   1739   1.1   simonb 
   1740   1.1   simonb 	/* Reference page */
   1741   1.1   simonb 	val = *loc;
   1742   1.1   simonb 
   1743   1.1   simonb 	ref = pmap_is_referenced(pg);
   1744   1.1   simonb 	mod = pmap_is_modified(pg);
   1745   1.1   simonb 	printf("Referenced page: ref %d, mod %d val %x\n",
   1746   1.1   simonb 	       ref, mod, val);
   1747   1.1   simonb 
   1748   1.1   simonb 	/* Now clear reference and modify */
   1749   1.1   simonb 	ref = pmap_clear_reference(pg);
   1750   1.1   simonb 	mod = pmap_clear_modify(pg);
   1751   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1752   1.1   simonb 	       (void *)(u_long)va, (long)pa,
   1753   1.1   simonb 	       ref, mod);
   1754  1.12   simonb 
   1755   1.1   simonb 	/* Modify page */
   1756   1.1   simonb #if 0
   1757   1.1   simonb 	pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
   1758   1.4    chris 	pmap_update(pmap_kernel());
   1759   1.1   simonb #endif
   1760   1.1   simonb 	*loc = 1;
   1761   1.1   simonb 
   1762   1.1   simonb 	ref = pmap_is_referenced(pg);
   1763   1.1   simonb 	mod = pmap_is_modified(pg);
   1764   1.1   simonb 	printf("Modified page: ref %d, mod %d\n",
   1765   1.1   simonb 	       ref, mod);
   1766   1.1   simonb 
   1767   1.1   simonb 	/* Unmap page */
   1768   1.1   simonb 	pmap_remove(pmap_kernel(), va, va+1);
   1769   1.4    chris 	pmap_update(pmap_kernel());
   1770   1.1   simonb 	ref = pmap_is_referenced(pg);
   1771   1.1   simonb 	mod = pmap_is_modified(pg);
   1772   1.1   simonb 	printf("Unmapped page: ref %d, mod %d\n", ref, mod);
   1773   1.1   simonb 
   1774   1.1   simonb 	/* Now clear reference and modify */
   1775   1.1   simonb 	ref = pmap_clear_reference(pg);
   1776   1.1   simonb 	mod = pmap_clear_modify(pg);
   1777   1.1   simonb 	printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
   1778   1.1   simonb 	       (void *)(u_long)va, (long)pa, ref, mod);
   1779   1.1   simonb 
   1780   1.1   simonb 	/* Check it's properly cleared */
   1781   1.1   simonb 	ref = pmap_is_referenced(pg);
   1782   1.1   simonb 	mod = pmap_is_modified(pg);
   1783   1.1   simonb 	printf("Checking cleared page: ref %d, mod %d\n",
   1784   1.1   simonb 	       ref, mod);
   1785   1.1   simonb 
   1786  1.34     yamt 	pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
   1787  1.35     yamt 	pmap_kenter_pa(va, pa, VM_PROT_ALL);
   1788  1.34     yamt 	uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE, UVM_KMF_WIRED);
   1789   1.1   simonb }
   1790   1.1   simonb #endif
   1791