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