Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.24
      1  1.24   chris /*	$NetBSD: pmap.c,v 1.24 2001/09/29 09:39:12 chris Exp $	*/
      2  1.12   chris 
      3  1.12   chris /*
      4  1.12   chris  * Copyright (c) 2001 Richard Earnshaw
      5  1.12   chris  * Copyright (c) 2001 Christopher Gilbert
      6  1.12   chris  * All rights reserved.
      7  1.12   chris  *
      8  1.12   chris  * 1. Redistributions of source code must retain the above copyright
      9  1.12   chris  *    notice, this list of conditions and the following disclaimer.
     10  1.12   chris  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.12   chris  *    notice, this list of conditions and the following disclaimer in the
     12  1.12   chris  *    documentation and/or other materials provided with the distribution.
     13  1.12   chris  * 3. The name of the company nor the name of the author may be used to
     14  1.12   chris  *    endorse or promote products derived from this software without specific
     15  1.12   chris  *    prior written permission.
     16  1.12   chris  *
     17  1.12   chris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     18  1.12   chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.12   chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.12   chris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     21  1.12   chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  1.12   chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  1.12   chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.12   chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.12   chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.12   chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.12   chris  * SUCH DAMAGE.
     28  1.12   chris  */
     29   1.1    matt 
     30   1.1    matt /*-
     31   1.1    matt  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     32   1.1    matt  * All rights reserved.
     33   1.1    matt  *
     34   1.1    matt  * This code is derived from software contributed to The NetBSD Foundation
     35   1.1    matt  * by Charles M. Hannum.
     36   1.1    matt  *
     37   1.1    matt  * Redistribution and use in source and binary forms, with or without
     38   1.1    matt  * modification, are permitted provided that the following conditions
     39   1.1    matt  * are met:
     40   1.1    matt  * 1. Redistributions of source code must retain the above copyright
     41   1.1    matt  *    notice, this list of conditions and the following disclaimer.
     42   1.1    matt  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1    matt  *    notice, this list of conditions and the following disclaimer in the
     44   1.1    matt  *    documentation and/or other materials provided with the distribution.
     45   1.1    matt  * 3. All advertising materials mentioning features or use of this software
     46   1.1    matt  *    must display the following acknowledgement:
     47   1.1    matt  *        This product includes software developed by the NetBSD
     48   1.1    matt  *        Foundation, Inc. and its contributors.
     49   1.1    matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     50   1.1    matt  *    contributors may be used to endorse or promote products derived
     51   1.1    matt  *    from this software without specific prior written permission.
     52   1.1    matt  *
     53   1.1    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54   1.1    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55   1.1    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56   1.1    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57   1.1    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58   1.1    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59   1.1    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60   1.1    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61   1.1    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62   1.1    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63   1.1    matt  * POSSIBILITY OF SUCH DAMAGE.
     64   1.1    matt  */
     65   1.1    matt 
     66   1.1    matt /*
     67   1.1    matt  * Copyright (c) 1994-1998 Mark Brinicombe.
     68   1.1    matt  * Copyright (c) 1994 Brini.
     69   1.1    matt  * All rights reserved.
     70   1.1    matt  *
     71   1.1    matt  * This code is derived from software written for Brini by Mark Brinicombe
     72   1.1    matt  *
     73   1.1    matt  * Redistribution and use in source and binary forms, with or without
     74   1.1    matt  * modification, are permitted provided that the following conditions
     75   1.1    matt  * are met:
     76   1.1    matt  * 1. Redistributions of source code must retain the above copyright
     77   1.1    matt  *    notice, this list of conditions and the following disclaimer.
     78   1.1    matt  * 2. Redistributions in binary form must reproduce the above copyright
     79   1.1    matt  *    notice, this list of conditions and the following disclaimer in the
     80   1.1    matt  *    documentation and/or other materials provided with the distribution.
     81   1.1    matt  * 3. All advertising materials mentioning features or use of this software
     82   1.1    matt  *    must display the following acknowledgement:
     83   1.1    matt  *	This product includes software developed by Mark Brinicombe.
     84   1.1    matt  * 4. The name of the author may not be used to endorse or promote products
     85   1.1    matt  *    derived from this software without specific prior written permission.
     86   1.1    matt  *
     87   1.1    matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     88   1.1    matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     89   1.1    matt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     90   1.1    matt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     91   1.1    matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     92   1.1    matt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     93   1.1    matt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     94   1.1    matt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     95   1.1    matt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     96   1.1    matt  *
     97   1.1    matt  * RiscBSD kernel project
     98   1.1    matt  *
     99   1.1    matt  * pmap.c
    100   1.1    matt  *
    101   1.1    matt  * Machine dependant vm stuff
    102   1.1    matt  *
    103   1.1    matt  * Created      : 20/09/94
    104   1.1    matt  */
    105   1.1    matt 
    106   1.1    matt /*
    107   1.1    matt  * Performance improvements, UVM changes, overhauls and part-rewrites
    108   1.1    matt  * were contributed by Neil A. Carson <neil (at) causality.com>.
    109   1.1    matt  */
    110   1.1    matt 
    111   1.1    matt /*
    112   1.1    matt  * The dram block info is currently referenced from the bootconfig.
    113   1.1    matt  * This should be placed in a separate structure.
    114   1.1    matt  */
    115   1.1    matt 
    116   1.1    matt /*
    117   1.1    matt  * Special compilation symbols
    118   1.1    matt  * PMAP_DEBUG		- Build in pmap_debug_level code
    119   1.1    matt  */
    120   1.1    matt 
    121   1.1    matt /* Include header files */
    122   1.1    matt 
    123   1.1    matt #include "opt_pmap_debug.h"
    124   1.1    matt #include "opt_ddb.h"
    125   1.1    matt 
    126   1.1    matt #include <sys/types.h>
    127   1.1    matt #include <sys/param.h>
    128   1.1    matt #include <sys/kernel.h>
    129   1.1    matt #include <sys/systm.h>
    130   1.1    matt #include <sys/proc.h>
    131   1.1    matt #include <sys/malloc.h>
    132   1.1    matt #include <sys/user.h>
    133  1.10   chris #include <sys/pool.h>
    134  1.16   chris #include <sys/cdefs.h>
    135  1.16   chris 
    136   1.1    matt #include <uvm/uvm.h>
    137   1.1    matt 
    138   1.1    matt #include <machine/bootconfig.h>
    139   1.1    matt #include <machine/bus.h>
    140   1.1    matt #include <machine/pmap.h>
    141   1.1    matt #include <machine/pcb.h>
    142   1.1    matt #include <machine/param.h>
    143   1.1    matt #include <machine/katelib.h>
    144  1.16   chris 
    145  1.24   chris __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.24 2001/09/29 09:39:12 chris Exp $");
    146   1.1    matt #ifdef PMAP_DEBUG
    147   1.1    matt #define	PDEBUG(_lev_,_stat_) \
    148   1.1    matt 	if (pmap_debug_level >= (_lev_)) \
    149   1.1    matt         	((_stat_))
    150   1.1    matt int pmap_debug_level = -2;
    151  1.17   chris 
    152  1.17   chris /*
    153  1.17   chris  * for switching to potentially finer grained debugging
    154  1.17   chris  */
    155  1.17   chris #define	PDB_FOLLOW	0x0001
    156  1.17   chris #define	PDB_INIT	0x0002
    157  1.17   chris #define	PDB_ENTER	0x0004
    158  1.17   chris #define	PDB_REMOVE	0x0008
    159  1.17   chris #define	PDB_CREATE	0x0010
    160  1.17   chris #define	PDB_PTPAGE	0x0020
    161  1.17   chris #define	PDB_ASN		0x0040
    162  1.17   chris #define	PDB_BITS	0x0080
    163  1.17   chris #define	PDB_COLLECT	0x0100
    164  1.17   chris #define	PDB_PROTECT	0x0200
    165  1.17   chris #define	PDB_BOOTSTRAP	0x1000
    166  1.17   chris #define	PDB_PARANOIA	0x2000
    167  1.17   chris #define	PDB_WIRING	0x4000
    168  1.17   chris #define	PDB_PVDUMP	0x8000
    169  1.17   chris 
    170  1.17   chris int debugmap = 0;
    171  1.17   chris int pmapdebug = PDB_PARANOIA | PDB_FOLLOW;
    172  1.17   chris #define	NPDEBUG(_lev_,_stat_) \
    173  1.17   chris 	if (pmapdebug & (_lev_)) \
    174  1.17   chris         	((_stat_))
    175  1.17   chris 
    176   1.1    matt #else	/* PMAP_DEBUG */
    177   1.1    matt #define	PDEBUG(_lev_,_stat_) /* Nothing */
    178  1.17   chris #define PDEBUG(_lev_,_stat_) /* Nothing */
    179   1.1    matt #endif	/* PMAP_DEBUG */
    180   1.1    matt 
    181   1.1    matt struct pmap     kernel_pmap_store;
    182   1.1    matt 
    183  1.10   chris /*
    184  1.10   chris  * pool that pmap structures are allocated from
    185  1.10   chris  */
    186  1.10   chris 
    187  1.10   chris struct pool pmap_pmap_pool;
    188  1.10   chris 
    189   1.1    matt pagehook_t page_hook0;
    190   1.1    matt pagehook_t page_hook1;
    191   1.1    matt char *memhook;
    192   1.1    matt pt_entry_t msgbufpte;
    193   1.1    matt extern caddr_t msgbufaddr;
    194   1.1    matt 
    195   1.1    matt boolean_t pmap_initialized = FALSE;	/* Has pmap_init completed? */
    196  1.17   chris /*
    197  1.17   chris  * locking data structures
    198  1.17   chris  */
    199   1.1    matt 
    200  1.17   chris static struct lock pmap_main_lock;
    201  1.17   chris static struct simplelock pvalloc_lock;
    202  1.17   chris #ifdef LOCKDEBUG
    203  1.17   chris #define PMAP_MAP_TO_HEAD_LOCK() \
    204  1.17   chris      (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
    205  1.17   chris #define PMAP_MAP_TO_HEAD_UNLOCK() \
    206  1.17   chris      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    207  1.17   chris 
    208  1.17   chris #define PMAP_HEAD_TO_MAP_LOCK() \
    209  1.17   chris      (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
    210  1.17   chris #define PMAP_HEAD_TO_MAP_UNLOCK() \
    211  1.17   chris      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    212  1.17   chris #else
    213  1.17   chris #define	PMAP_MAP_TO_HEAD_LOCK()		/* nothing */
    214  1.17   chris #define	PMAP_MAP_TO_HEAD_UNLOCK()	/* nothing */
    215  1.17   chris #define	PMAP_HEAD_TO_MAP_LOCK()		/* nothing */
    216  1.17   chris #define	PMAP_HEAD_TO_MAP_UNLOCK()	/* nothing */
    217  1.17   chris #endif /* LOCKDEBUG */
    218  1.17   chris 
    219  1.17   chris /*
    220  1.17   chris  * pv_page management structures: locked by pvalloc_lock
    221  1.17   chris  */
    222   1.1    matt 
    223  1.17   chris TAILQ_HEAD(pv_pagelist, pv_page);
    224  1.17   chris static struct pv_pagelist pv_freepages;	/* list of pv_pages with free entrys */
    225  1.17   chris static struct pv_pagelist pv_unusedpgs; /* list of unused pv_pages */
    226  1.17   chris static int pv_nfpvents;			/* # of free pv entries */
    227  1.17   chris static struct pv_page *pv_initpage;	/* bootstrap page from kernel_map */
    228  1.17   chris static vaddr_t pv_cachedva;		/* cached VA for later use */
    229  1.17   chris 
    230  1.17   chris #define PVE_LOWAT (PVE_PER_PVPAGE / 2)	/* free pv_entry low water mark */
    231  1.17   chris #define PVE_HIWAT (PVE_LOWAT + (PVE_PER_PVPAGE * 2))
    232  1.17   chris 					/* high water mark */
    233  1.17   chris 
    234  1.17   chris /*
    235  1.17   chris  * local prototypes
    236  1.17   chris  */
    237  1.17   chris 
    238  1.17   chris static struct pv_entry	*pmap_add_pvpage __P((struct pv_page *, boolean_t));
    239  1.17   chris static struct pv_entry	*pmap_alloc_pv __P((struct pmap *, int)); /* see codes below */
    240  1.17   chris #define ALLOCPV_NEED	0	/* need PV now */
    241  1.17   chris #define ALLOCPV_TRY	1	/* just try to allocate, don't steal */
    242  1.17   chris #define ALLOCPV_NONEED	2	/* don't need PV, just growing cache */
    243  1.17   chris static struct pv_entry	*pmap_alloc_pvpage __P((struct pmap *, int));
    244  1.17   chris static void		 pmap_enter_pv __P((struct pv_head *,
    245  1.17   chris 					    struct pv_entry *, struct pmap *,
    246  1.17   chris 					    vaddr_t, struct vm_page *, int));
    247  1.17   chris static void		 pmap_free_pv __P((struct pmap *, struct pv_entry *));
    248  1.17   chris static void		 pmap_free_pvs __P((struct pmap *, struct pv_entry *));
    249  1.17   chris static void		 pmap_free_pv_doit __P((struct pv_entry *));
    250  1.17   chris static void		 pmap_free_pvpage __P((void));
    251  1.17   chris static boolean_t	 pmap_is_curpmap __P((struct pmap *));
    252  1.17   chris static struct pv_entry	*pmap_remove_pv __P((struct pv_head *, struct pmap *,
    253  1.17   chris 			vaddr_t));
    254  1.17   chris #define PMAP_REMOVE_ALL		0	/* remove all mappings */
    255  1.17   chris #define PMAP_REMOVE_SKIPWIRED	1	/* skip wired mappings */
    256   1.1    matt 
    257   1.2    matt vsize_t npages;
    258   1.1    matt 
    259  1.17   chris static struct vm_page	*pmap_alloc_ptp __P((struct pmap *, vaddr_t, boolean_t));
    260  1.17   chris static struct vm_page	*pmap_get_ptp __P((struct pmap *, vaddr_t, boolean_t));
    261  1.22   chris __inline static void pmap_clearbit __P((paddr_t, unsigned int));
    262  1.22   chris __inline static boolean_t pmap_testbit __P((paddr_t, unsigned int));
    263  1.17   chris 
    264   1.2    matt extern paddr_t physical_start;
    265   1.2    matt extern paddr_t physical_freestart;
    266   1.2    matt extern paddr_t physical_end;
    267   1.2    matt extern paddr_t physical_freeend;
    268   1.1    matt extern unsigned int free_pages;
    269   1.1    matt extern int max_processes;
    270   1.1    matt 
    271   1.1    matt vaddr_t virtual_start;
    272   1.1    matt vaddr_t virtual_end;
    273   1.1    matt 
    274   1.1    matt vaddr_t avail_start;
    275   1.1    matt vaddr_t avail_end;
    276   1.1    matt 
    277   1.1    matt extern pv_addr_t systempage;
    278   1.1    matt 
    279   1.1    matt #define ALLOC_PAGE_HOOK(x, s) \
    280   1.1    matt 	x.va = virtual_start; \
    281  1.15   chris 	x.pte = (pt_entry_t *)pmap_pte(pmap_kernel(), virtual_start); \
    282   1.1    matt 	virtual_start += s;
    283   1.1    matt 
    284   1.1    matt /* Variables used by the L1 page table queue code */
    285   1.1    matt SIMPLEQ_HEAD(l1pt_queue, l1pt);
    286   1.1    matt struct l1pt_queue l1pt_static_queue;	/* head of our static l1 queue */
    287   1.1    matt int l1pt_static_queue_count;		/* items in the static l1 queue */
    288   1.1    matt int l1pt_static_create_count;		/* static l1 items created */
    289   1.1    matt struct l1pt_queue l1pt_queue;		/* head of our l1 queue */
    290   1.1    matt int l1pt_queue_count;			/* items in the l1 queue */
    291   1.1    matt int l1pt_create_count;			/* stat - L1's create count */
    292   1.1    matt int l1pt_reuse_count;			/* stat - L1's reused count */
    293   1.1    matt 
    294   1.1    matt /* Local function prototypes (not used outside this file) */
    295  1.15   chris pt_entry_t *pmap_pte __P((struct pmap *pmap, vaddr_t va));
    296   1.1    matt void map_pagetable __P((vaddr_t pagetable, vaddr_t va,
    297   1.2    matt     paddr_t pa, unsigned int flags));
    298   1.2    matt void pmap_copy_on_write __P((paddr_t pa));
    299  1.15   chris void pmap_pinit __P((struct pmap *));
    300  1.15   chris void pmap_freepagedir __P((struct pmap *));
    301   1.1    matt 
    302   1.1    matt /* Other function prototypes */
    303   1.1    matt extern void bzero_page __P((vaddr_t));
    304   1.1    matt extern void bcopy_page __P((vaddr_t, vaddr_t));
    305   1.1    matt 
    306   1.1    matt struct l1pt *pmap_alloc_l1pt __P((void));
    307  1.15   chris static __inline void pmap_map_in_l1 __P((struct pmap *pmap, vaddr_t va,
    308  1.17   chris      vaddr_t l2pa, boolean_t));
    309   1.1    matt 
    310  1.11   chris static pt_entry_t *pmap_map_ptes __P((struct pmap *));
    311  1.17   chris static void pmap_unmap_ptes __P((struct pmap *));
    312  1.11   chris 
    313  1.17   chris void pmap_vac_me_harder __P((struct pmap *, struct pv_head *,
    314  1.12   chris 	    pt_entry_t *, boolean_t));
    315  1.11   chris 
    316  1.17   chris /*
    317  1.17   chris  * real definition of pv_entry.
    318  1.17   chris  */
    319  1.17   chris 
    320  1.17   chris struct pv_entry {
    321  1.17   chris 	struct pv_entry *pv_next;       /* next pv_entry */
    322  1.17   chris 	struct pmap     *pv_pmap;        /* pmap where mapping lies */
    323  1.17   chris 	vaddr_t         pv_va;          /* virtual address for mapping */
    324  1.17   chris 	int             pv_flags;       /* flags */
    325  1.17   chris 	struct vm_page	*pv_ptp;	/* vm_page for the ptp */
    326  1.17   chris };
    327  1.17   chris 
    328  1.17   chris /*
    329  1.17   chris  * pv_entrys are dynamically allocated in chunks from a single page.
    330  1.17   chris  * we keep track of how many pv_entrys are in use for each page and
    331  1.17   chris  * we can free pv_entry pages if needed.  there is one lock for the
    332  1.17   chris  * entire allocation system.
    333  1.17   chris  */
    334  1.17   chris 
    335  1.17   chris struct pv_page_info {
    336  1.17   chris 	TAILQ_ENTRY(pv_page) pvpi_list;
    337  1.17   chris 	struct pv_entry *pvpi_pvfree;
    338  1.17   chris 	int pvpi_nfree;
    339  1.17   chris };
    340  1.17   chris 
    341  1.17   chris /*
    342  1.17   chris  * number of pv_entry's in a pv_page
    343  1.17   chris  * (note: won't work on systems where NPBG isn't a constant)
    344  1.17   chris  */
    345  1.17   chris 
    346  1.17   chris #define PVE_PER_PVPAGE ((NBPG - sizeof(struct pv_page_info)) / \
    347  1.17   chris 			sizeof(struct pv_entry))
    348  1.17   chris 
    349  1.17   chris /*
    350  1.17   chris  * a pv_page: where pv_entrys are allocated from
    351  1.17   chris  */
    352  1.17   chris 
    353  1.17   chris struct pv_page {
    354  1.17   chris 	struct pv_page_info pvinfo;
    355  1.17   chris 	struct pv_entry pvents[PVE_PER_PVPAGE];
    356  1.17   chris };
    357  1.17   chris 
    358   1.1    matt #ifdef MYCROFT_HACK
    359   1.1    matt int mycroft_hack = 0;
    360   1.1    matt #endif
    361   1.1    matt 
    362   1.1    matt /* Function to set the debug level of the pmap code */
    363   1.1    matt 
    364   1.1    matt #ifdef PMAP_DEBUG
    365   1.1    matt void
    366   1.1    matt pmap_debug(level)
    367   1.1    matt 	int level;
    368   1.1    matt {
    369   1.1    matt 	pmap_debug_level = level;
    370   1.1    matt 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    371   1.1    matt }
    372   1.1    matt #endif	/* PMAP_DEBUG */
    373   1.1    matt 
    374  1.22   chris __inline static boolean_t
    375  1.17   chris pmap_is_curpmap(struct pmap *pmap)
    376  1.17   chris {
    377  1.17   chris     if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
    378  1.17   chris 	    || (pmap == pmap_kernel()))
    379  1.17   chris 	return (TRUE);
    380  1.17   chris     return (FALSE);
    381  1.17   chris }
    382   1.1    matt #include "isadma.h"
    383   1.1    matt 
    384   1.1    matt #if NISADMA > 0
    385   1.1    matt /*
    386   1.1    matt  * Used to protect memory for ISA DMA bounce buffers.  If, when loading
    387   1.1    matt  * pages into the system, memory intersects with any of these ranges,
    388   1.1    matt  * the intersecting memory will be loaded into a lower-priority free list.
    389   1.1    matt  */
    390   1.1    matt bus_dma_segment_t *pmap_isa_dma_ranges;
    391   1.1    matt int pmap_isa_dma_nranges;
    392   1.1    matt 
    393   1.2    matt boolean_t pmap_isa_dma_range_intersect __P((paddr_t, psize_t,
    394   1.2    matt 	    paddr_t *, psize_t *));
    395   1.1    matt 
    396   1.1    matt /*
    397   1.1    matt  * Check if a memory range intersects with an ISA DMA range, and
    398   1.1    matt  * return the page-rounded intersection if it does.  The intersection
    399   1.1    matt  * will be placed on a lower-priority free list.
    400   1.1    matt  */
    401   1.1    matt boolean_t
    402   1.1    matt pmap_isa_dma_range_intersect(pa, size, pap, sizep)
    403   1.2    matt 	paddr_t pa;
    404   1.2    matt 	psize_t size;
    405   1.2    matt 	paddr_t *pap;
    406   1.2    matt 	psize_t *sizep;
    407   1.1    matt {
    408   1.1    matt 	bus_dma_segment_t *ds;
    409   1.1    matt 	int i;
    410   1.1    matt 
    411   1.1    matt 	if (pmap_isa_dma_ranges == NULL)
    412   1.1    matt 		return (FALSE);
    413   1.1    matt 
    414   1.1    matt 	for (i = 0, ds = pmap_isa_dma_ranges;
    415   1.1    matt 	     i < pmap_isa_dma_nranges; i++, ds++) {
    416   1.1    matt 		if (ds->ds_addr <= pa && pa < (ds->ds_addr + ds->ds_len)) {
    417   1.1    matt 			/*
    418   1.1    matt 			 * Beginning of region intersects with this range.
    419   1.1    matt 			 */
    420   1.1    matt 			*pap = trunc_page(pa);
    421   1.1    matt 			*sizep = round_page(min(pa + size,
    422   1.1    matt 			    ds->ds_addr + ds->ds_len) - pa);
    423   1.1    matt 			return (TRUE);
    424   1.1    matt 		}
    425   1.1    matt 		if (pa < ds->ds_addr && ds->ds_addr < (pa + size)) {
    426   1.1    matt 			/*
    427   1.1    matt 			 * End of region intersects with this range.
    428   1.1    matt 			 */
    429   1.1    matt 			*pap = trunc_page(ds->ds_addr);
    430   1.1    matt 			*sizep = round_page(min((pa + size) - ds->ds_addr,
    431   1.1    matt 			    ds->ds_len));
    432   1.1    matt 			return (TRUE);
    433   1.1    matt 		}
    434   1.1    matt 	}
    435   1.1    matt 
    436   1.1    matt 	/*
    437   1.1    matt 	 * No intersection found.
    438   1.1    matt 	 */
    439   1.1    matt 	return (FALSE);
    440   1.1    matt }
    441   1.1    matt #endif /* NISADMA > 0 */
    442   1.1    matt 
    443   1.1    matt /*
    444  1.17   chris  * p v _ e n t r y   f u n c t i o n s
    445  1.17   chris  */
    446  1.17   chris 
    447  1.17   chris /*
    448  1.17   chris  * pv_entry allocation functions:
    449  1.17   chris  *   the main pv_entry allocation functions are:
    450  1.17   chris  *     pmap_alloc_pv: allocate a pv_entry structure
    451  1.17   chris  *     pmap_free_pv: free one pv_entry
    452  1.17   chris  *     pmap_free_pvs: free a list of pv_entrys
    453  1.17   chris  *
    454  1.17   chris  * the rest are helper functions
    455   1.1    matt  */
    456   1.1    matt 
    457   1.1    matt /*
    458  1.17   chris  * pmap_alloc_pv: inline function to allocate a pv_entry structure
    459  1.17   chris  * => we lock pvalloc_lock
    460  1.17   chris  * => if we fail, we call out to pmap_alloc_pvpage
    461  1.17   chris  * => 3 modes:
    462  1.17   chris  *    ALLOCPV_NEED   = we really need a pv_entry, even if we have to steal it
    463  1.17   chris  *    ALLOCPV_TRY    = we want a pv_entry, but not enough to steal
    464  1.17   chris  *    ALLOCPV_NONEED = we are trying to grow our free list, don't really need
    465  1.17   chris  *			one now
    466  1.17   chris  *
    467  1.17   chris  * "try" is for optional functions like pmap_copy().
    468   1.1    matt  */
    469  1.17   chris 
    470  1.17   chris __inline static struct pv_entry *
    471  1.17   chris pmap_alloc_pv(pmap, mode)
    472  1.17   chris 	struct pmap *pmap;
    473  1.17   chris 	int mode;
    474   1.1    matt {
    475  1.17   chris 	struct pv_page *pvpage;
    476  1.17   chris 	struct pv_entry *pv;
    477  1.17   chris 
    478  1.17   chris 	simple_lock(&pvalloc_lock);
    479  1.17   chris 
    480  1.17   chris 	if (pv_freepages.tqh_first != NULL) {
    481  1.17   chris 		pvpage = pv_freepages.tqh_first;
    482  1.17   chris 		pvpage->pvinfo.pvpi_nfree--;
    483  1.17   chris 		if (pvpage->pvinfo.pvpi_nfree == 0) {
    484  1.17   chris 			/* nothing left in this one? */
    485  1.17   chris 			TAILQ_REMOVE(&pv_freepages, pvpage, pvinfo.pvpi_list);
    486  1.17   chris 		}
    487  1.17   chris 		pv = pvpage->pvinfo.pvpi_pvfree;
    488  1.17   chris #ifdef DIAGNOSTIC
    489  1.17   chris 		if (pv == NULL)
    490  1.17   chris 			panic("pmap_alloc_pv: pvpi_nfree off");
    491  1.17   chris #endif
    492  1.17   chris 		pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
    493  1.17   chris 		pv_nfpvents--;  /* took one from pool */
    494  1.17   chris 	} else {
    495  1.17   chris 		pv = NULL;		/* need more of them */
    496  1.17   chris 	}
    497  1.17   chris 
    498  1.17   chris 	/*
    499  1.17   chris 	 * if below low water mark or we didn't get a pv_entry we try and
    500  1.17   chris 	 * create more pv_entrys ...
    501  1.17   chris 	 */
    502  1.17   chris 
    503  1.17   chris 	if (pv_nfpvents < PVE_LOWAT || pv == NULL) {
    504  1.17   chris 		if (pv == NULL)
    505  1.17   chris 			pv = pmap_alloc_pvpage(pmap, (mode == ALLOCPV_TRY) ?
    506  1.17   chris 					       mode : ALLOCPV_NEED);
    507  1.17   chris 		else
    508  1.17   chris 			(void) pmap_alloc_pvpage(pmap, ALLOCPV_NONEED);
    509  1.17   chris 	}
    510  1.17   chris 
    511  1.17   chris 	simple_unlock(&pvalloc_lock);
    512  1.17   chris 	return(pv);
    513  1.17   chris }
    514  1.17   chris 
    515  1.17   chris /*
    516  1.17   chris  * pmap_alloc_pvpage: maybe allocate a new pvpage
    517  1.17   chris  *
    518  1.17   chris  * if need_entry is false: try and allocate a new pv_page
    519  1.17   chris  * if need_entry is true: try and allocate a new pv_page and return a
    520  1.17   chris  *	new pv_entry from it.   if we are unable to allocate a pv_page
    521  1.17   chris  *	we make a last ditch effort to steal a pv_page from some other
    522  1.17   chris  *	mapping.    if that fails, we panic...
    523  1.17   chris  *
    524  1.17   chris  * => we assume that the caller holds pvalloc_lock
    525  1.17   chris  */
    526  1.17   chris 
    527  1.17   chris static struct pv_entry *
    528  1.17   chris pmap_alloc_pvpage(pmap, mode)
    529  1.17   chris 	struct pmap *pmap;
    530  1.17   chris 	int mode;
    531  1.17   chris {
    532  1.17   chris 	struct vm_page *pg;
    533  1.17   chris 	struct pv_page *pvpage;
    534   1.1    matt 	struct pv_entry *pv;
    535  1.17   chris 	int s;
    536  1.17   chris 
    537  1.17   chris 	/*
    538  1.17   chris 	 * if we need_entry and we've got unused pv_pages, allocate from there
    539  1.17   chris 	 */
    540  1.17   chris 
    541  1.17   chris 	if (mode != ALLOCPV_NONEED && pv_unusedpgs.tqh_first != NULL) {
    542  1.17   chris 
    543  1.17   chris 		/* move it to pv_freepages list */
    544  1.17   chris 		pvpage = pv_unusedpgs.tqh_first;
    545  1.17   chris 		TAILQ_REMOVE(&pv_unusedpgs, pvpage, pvinfo.pvpi_list);
    546  1.17   chris 		TAILQ_INSERT_HEAD(&pv_freepages, pvpage, pvinfo.pvpi_list);
    547  1.17   chris 
    548  1.17   chris 		/* allocate a pv_entry */
    549  1.17   chris 		pvpage->pvinfo.pvpi_nfree--;	/* can't go to zero */
    550  1.17   chris 		pv = pvpage->pvinfo.pvpi_pvfree;
    551  1.17   chris #ifdef DIAGNOSTIC
    552  1.17   chris 		if (pv == NULL)
    553  1.17   chris 			panic("pmap_alloc_pvpage: pvpi_nfree off");
    554  1.17   chris #endif
    555  1.17   chris 		pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
    556  1.17   chris 
    557  1.17   chris 		pv_nfpvents--;  /* took one from pool */
    558  1.17   chris 		return(pv);
    559  1.17   chris 	}
    560   1.1    matt 
    561   1.1    matt 	/*
    562  1.17   chris 	 *  see if we've got a cached unmapped VA that we can map a page in.
    563  1.17   chris 	 * if not, try to allocate one.
    564   1.1    matt 	 */
    565   1.1    matt 
    566  1.23     chs 
    567  1.17   chris 	if (pv_cachedva == 0) {
    568  1.23     chs 		s = splvm();
    569  1.23     chs 		pv_cachedva = uvm_km_kmemalloc(kmem_map, NULL,
    570  1.17   chris 		    PAGE_SIZE, UVM_KMF_TRYLOCK|UVM_KMF_VALLOC);
    571  1.23     chs 		splx(s);
    572  1.17   chris 		if (pv_cachedva == 0) {
    573  1.17   chris 			return (NULL);
    574   1.1    matt 		}
    575   1.1    matt 	}
    576  1.17   chris 
    577  1.23     chs 	pg = uvm_pagealloc(NULL, pv_cachedva - vm_map_min(kernel_map), NULL,
    578  1.23     chs 	    UVM_PGA_USERESERVE);
    579  1.17   chris 	if (pg)
    580  1.17   chris 		pg->flags &= ~PG_BUSY;	/* never busy */
    581  1.17   chris 
    582  1.17   chris 	if (pg == NULL)
    583  1.17   chris 		return (NULL);
    584  1.17   chris 
    585  1.17   chris 	/*
    586  1.17   chris 	 * add a mapping for our new pv_page and free its entrys (save one!)
    587  1.17   chris 	 *
    588  1.17   chris 	 * NOTE: If we are allocating a PV page for the kernel pmap, the
    589  1.17   chris 	 * pmap is already locked!  (...but entering the mapping is safe...)
    590  1.17   chris 	 */
    591  1.17   chris 
    592  1.17   chris 	pmap_kenter_pa(pv_cachedva, VM_PAGE_TO_PHYS(pg), VM_PROT_ALL);
    593  1.19   chris 	pmap_update(pmap_kernel());
    594  1.17   chris 	pvpage = (struct pv_page *) pv_cachedva;
    595  1.17   chris 	pv_cachedva = 0;
    596  1.17   chris 	return (pmap_add_pvpage(pvpage, mode != ALLOCPV_NONEED));
    597   1.1    matt }
    598   1.1    matt 
    599   1.1    matt /*
    600  1.17   chris  * pmap_add_pvpage: add a pv_page's pv_entrys to the free list
    601  1.17   chris  *
    602  1.17   chris  * => caller must hold pvalloc_lock
    603  1.17   chris  * => if need_entry is true, we allocate and return one pv_entry
    604   1.1    matt  */
    605   1.1    matt 
    606  1.17   chris static struct pv_entry *
    607  1.17   chris pmap_add_pvpage(pvp, need_entry)
    608  1.17   chris 	struct pv_page *pvp;
    609  1.17   chris 	boolean_t need_entry;
    610   1.1    matt {
    611  1.17   chris 	int tofree, lcv;
    612  1.17   chris 
    613  1.17   chris 	/* do we need to return one? */
    614  1.17   chris 	tofree = (need_entry) ? PVE_PER_PVPAGE - 1 : PVE_PER_PVPAGE;
    615   1.1    matt 
    616  1.17   chris 	pvp->pvinfo.pvpi_pvfree = NULL;
    617  1.17   chris 	pvp->pvinfo.pvpi_nfree = tofree;
    618  1.17   chris 	for (lcv = 0 ; lcv < tofree ; lcv++) {
    619  1.17   chris 		pvp->pvents[lcv].pv_next = pvp->pvinfo.pvpi_pvfree;
    620  1.17   chris 		pvp->pvinfo.pvpi_pvfree = &pvp->pvents[lcv];
    621   1.1    matt 	}
    622  1.17   chris 	if (need_entry)
    623  1.17   chris 		TAILQ_INSERT_TAIL(&pv_freepages, pvp, pvinfo.pvpi_list);
    624  1.17   chris 	else
    625  1.17   chris 		TAILQ_INSERT_TAIL(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    626  1.17   chris 	pv_nfpvents += tofree;
    627  1.17   chris 	return((need_entry) ? &pvp->pvents[lcv] : NULL);
    628   1.1    matt }
    629   1.1    matt 
    630  1.17   chris /*
    631  1.17   chris  * pmap_free_pv_doit: actually free a pv_entry
    632  1.17   chris  *
    633  1.17   chris  * => do not call this directly!  instead use either
    634  1.17   chris  *    1. pmap_free_pv ==> free a single pv_entry
    635  1.17   chris  *    2. pmap_free_pvs => free a list of pv_entrys
    636  1.17   chris  * => we must be holding pvalloc_lock
    637  1.17   chris  */
    638  1.17   chris 
    639  1.17   chris __inline static void
    640  1.17   chris pmap_free_pv_doit(pv)
    641  1.17   chris 	struct pv_entry *pv;
    642   1.1    matt {
    643  1.17   chris 	struct pv_page *pvp;
    644   1.1    matt 
    645  1.17   chris 	pvp = (struct pv_page *) arm_trunc_page((vaddr_t)pv);
    646  1.17   chris 	pv_nfpvents++;
    647  1.17   chris 	pvp->pvinfo.pvpi_nfree++;
    648   1.1    matt 
    649  1.17   chris 	/* nfree == 1 => fully allocated page just became partly allocated */
    650  1.17   chris 	if (pvp->pvinfo.pvpi_nfree == 1) {
    651  1.17   chris 		TAILQ_INSERT_HEAD(&pv_freepages, pvp, pvinfo.pvpi_list);
    652   1.1    matt 	}
    653   1.1    matt 
    654  1.17   chris 	/* free it */
    655  1.17   chris 	pv->pv_next = pvp->pvinfo.pvpi_pvfree;
    656  1.17   chris 	pvp->pvinfo.pvpi_pvfree = pv;
    657   1.1    matt 
    658  1.17   chris 	/*
    659  1.17   chris 	 * are all pv_page's pv_entry's free?  move it to unused queue.
    660  1.17   chris 	 */
    661   1.1    matt 
    662  1.17   chris 	if (pvp->pvinfo.pvpi_nfree == PVE_PER_PVPAGE) {
    663  1.17   chris 		TAILQ_REMOVE(&pv_freepages, pvp, pvinfo.pvpi_list);
    664  1.17   chris 		TAILQ_INSERT_HEAD(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    665   1.1    matt 	}
    666   1.1    matt }
    667   1.1    matt 
    668   1.1    matt /*
    669  1.17   chris  * pmap_free_pv: free a single pv_entry
    670  1.17   chris  *
    671  1.17   chris  * => we gain the pvalloc_lock
    672   1.1    matt  */
    673   1.1    matt 
    674  1.17   chris __inline static void
    675  1.17   chris pmap_free_pv(pmap, pv)
    676  1.15   chris 	struct pmap *pmap;
    677   1.1    matt 	struct pv_entry *pv;
    678   1.1    matt {
    679  1.17   chris 	simple_lock(&pvalloc_lock);
    680  1.17   chris 	pmap_free_pv_doit(pv);
    681  1.17   chris 
    682  1.17   chris 	/*
    683  1.17   chris 	 * Can't free the PV page if the PV entries were associated with
    684  1.17   chris 	 * the kernel pmap; the pmap is already locked.
    685  1.17   chris 	 */
    686  1.17   chris 	if (pv_nfpvents > PVE_HIWAT && pv_unusedpgs.tqh_first != NULL &&
    687  1.17   chris 	    pmap != pmap_kernel())
    688  1.17   chris 		pmap_free_pvpage();
    689  1.17   chris 
    690  1.17   chris 	simple_unlock(&pvalloc_lock);
    691  1.17   chris }
    692   1.1    matt 
    693  1.17   chris /*
    694  1.17   chris  * pmap_free_pvs: free a list of pv_entrys
    695  1.17   chris  *
    696  1.17   chris  * => we gain the pvalloc_lock
    697  1.17   chris  */
    698   1.1    matt 
    699  1.17   chris __inline static void
    700  1.17   chris pmap_free_pvs(pmap, pvs)
    701  1.17   chris 	struct pmap *pmap;
    702  1.17   chris 	struct pv_entry *pvs;
    703  1.17   chris {
    704  1.17   chris 	struct pv_entry *nextpv;
    705   1.1    matt 
    706  1.17   chris 	simple_lock(&pvalloc_lock);
    707   1.1    matt 
    708  1.17   chris 	for ( /* null */ ; pvs != NULL ; pvs = nextpv) {
    709  1.17   chris 		nextpv = pvs->pv_next;
    710  1.17   chris 		pmap_free_pv_doit(pvs);
    711   1.1    matt 	}
    712   1.1    matt 
    713  1.17   chris 	/*
    714  1.17   chris 	 * Can't free the PV page if the PV entries were associated with
    715  1.17   chris 	 * the kernel pmap; the pmap is already locked.
    716  1.17   chris 	 */
    717  1.17   chris 	if (pv_nfpvents > PVE_HIWAT && pv_unusedpgs.tqh_first != NULL &&
    718  1.17   chris 	    pmap != pmap_kernel())
    719  1.17   chris 		pmap_free_pvpage();
    720   1.1    matt 
    721  1.17   chris 	simple_unlock(&pvalloc_lock);
    722   1.1    matt }
    723   1.1    matt 
    724   1.1    matt 
    725   1.1    matt /*
    726  1.17   chris  * pmap_free_pvpage: try and free an unused pv_page structure
    727  1.17   chris  *
    728  1.17   chris  * => assume caller is holding the pvalloc_lock and that
    729  1.17   chris  *	there is a page on the pv_unusedpgs list
    730  1.17   chris  * => if we can't get a lock on the kmem_map we try again later
    731  1.17   chris  * => note: analysis of MI kmem_map usage [i.e. malloc/free] shows
    732  1.17   chris  *	that if we can lock the kmem_map then we are not already
    733  1.17   chris  *	holding kmem_object's lock.
    734   1.1    matt  */
    735   1.1    matt 
    736  1.17   chris static void
    737  1.17   chris pmap_free_pvpage()
    738   1.1    matt {
    739  1.17   chris 	int s;
    740  1.17   chris 	struct vm_map *map;
    741  1.17   chris 	struct vm_map_entry *dead_entries;
    742  1.17   chris 	struct pv_page *pvp;
    743  1.17   chris 
    744  1.17   chris 	s = splvm(); /* protect kmem_map */
    745   1.1    matt 
    746  1.17   chris 	pvp = pv_unusedpgs.tqh_first;
    747   1.1    matt 
    748   1.1    matt 	/*
    749  1.17   chris 	 * note: watch out for pv_initpage which is allocated out of
    750  1.17   chris 	 * kernel_map rather than kmem_map.
    751   1.1    matt 	 */
    752  1.17   chris 	if (pvp == pv_initpage)
    753  1.17   chris 		map = kernel_map;
    754  1.17   chris 	else
    755  1.17   chris 		map = kmem_map;
    756  1.17   chris 
    757  1.17   chris 	if (vm_map_lock_try(map)) {
    758  1.17   chris 
    759  1.17   chris 		/* remove pvp from pv_unusedpgs */
    760  1.17   chris 		TAILQ_REMOVE(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
    761  1.17   chris 
    762  1.17   chris 		/* unmap the page */
    763  1.17   chris 		dead_entries = NULL;
    764  1.17   chris 		uvm_unmap_remove(map, (vaddr_t)pvp, ((vaddr_t)pvp) + PAGE_SIZE,
    765  1.17   chris 		    &dead_entries);
    766  1.17   chris 		vm_map_unlock(map);
    767  1.17   chris 
    768  1.17   chris 		if (dead_entries != NULL)
    769  1.17   chris 			uvm_unmap_detach(dead_entries, 0);
    770   1.1    matt 
    771  1.17   chris 		pv_nfpvents -= PVE_PER_PVPAGE;  /* update free count */
    772   1.1    matt 	}
    773   1.1    matt 
    774  1.17   chris 	if (pvp == pv_initpage)
    775  1.17   chris 		/* no more initpage, we've freed it */
    776  1.17   chris 		pv_initpage = NULL;
    777   1.1    matt 
    778   1.1    matt 	splx(s);
    779   1.1    matt }
    780   1.1    matt 
    781   1.1    matt /*
    782  1.17   chris  * main pv_entry manipulation functions:
    783  1.17   chris  *   pmap_enter_pv: enter a mapping onto a pv_head list
    784  1.17   chris  *   pmap_remove_pv: remove a mappiing from a pv_head list
    785  1.17   chris  *
    786  1.17   chris  * NOTE: pmap_enter_pv expects to lock the pvh itself
    787  1.17   chris  *       pmap_remove_pv expects te caller to lock the pvh before calling
    788  1.17   chris  */
    789  1.17   chris 
    790  1.17   chris /*
    791  1.17   chris  * pmap_enter_pv: enter a mapping onto a pv_head lst
    792  1.17   chris  *
    793  1.17   chris  * => caller should hold the proper lock on pmap_main_lock
    794  1.17   chris  * => caller should have pmap locked
    795  1.17   chris  * => we will gain the lock on the pv_head and allocate the new pv_entry
    796  1.17   chris  * => caller should adjust ptp's wire_count before calling
    797  1.17   chris  * => caller should not adjust pmap's wire_count
    798  1.17   chris  */
    799  1.17   chris 
    800  1.17   chris __inline static void
    801  1.17   chris pmap_enter_pv(pvh, pve, pmap, va, ptp, flags)
    802  1.17   chris 	struct pv_head *pvh;
    803  1.17   chris 	struct pv_entry *pve;	/* preallocated pve for us to use */
    804  1.17   chris 	struct pmap *pmap;
    805  1.17   chris 	vaddr_t va;
    806  1.17   chris 	struct vm_page *ptp;	/* PTP in pmap that maps this VA */
    807  1.17   chris 	int flags;
    808  1.17   chris {
    809  1.17   chris 	pve->pv_pmap = pmap;
    810  1.17   chris 	pve->pv_va = va;
    811  1.17   chris 	pve->pv_ptp = ptp;			/* NULL for kernel pmap */
    812  1.17   chris 	pve->pv_flags = flags;
    813  1.17   chris 	simple_lock(&pvh->pvh_lock);		/* lock pv_head */
    814  1.17   chris 	pve->pv_next = pvh->pvh_list;		/* add to ... */
    815  1.17   chris 	pvh->pvh_list = pve;			/* ... locked list */
    816  1.17   chris 	simple_unlock(&pvh->pvh_lock);		/* unlock, done! */
    817  1.17   chris 	if (pve->pv_flags & PT_W)
    818  1.17   chris 		++pmap->pm_stats.wired_count;
    819  1.17   chris }
    820  1.17   chris 
    821  1.17   chris /*
    822  1.17   chris  * pmap_remove_pv: try to remove a mapping from a pv_list
    823  1.17   chris  *
    824  1.17   chris  * => caller should hold proper lock on pmap_main_lock
    825  1.17   chris  * => pmap should be locked
    826  1.17   chris  * => caller should hold lock on pv_head [so that attrs can be adjusted]
    827  1.17   chris  * => caller should adjust ptp's wire_count and free PTP if needed
    828  1.17   chris  * => caller should NOT adjust pmap's wire_count
    829  1.17   chris  * => we return the removed pve
    830  1.17   chris  */
    831  1.17   chris 
    832  1.17   chris __inline static struct pv_entry *
    833  1.17   chris pmap_remove_pv(pvh, pmap, va)
    834  1.17   chris 	struct pv_head *pvh;
    835  1.17   chris 	struct pmap *pmap;
    836  1.17   chris 	vaddr_t va;
    837  1.17   chris {
    838  1.17   chris 	struct pv_entry *pve, **prevptr;
    839  1.17   chris 
    840  1.17   chris 	prevptr = &pvh->pvh_list;		/* previous pv_entry pointer */
    841  1.17   chris 	pve = *prevptr;
    842  1.17   chris 	while (pve) {
    843  1.17   chris 		if (pve->pv_pmap == pmap && pve->pv_va == va) {	/* match? */
    844  1.17   chris 			*prevptr = pve->pv_next;		/* remove it! */
    845  1.17   chris 			if (pve->pv_flags & PT_W)
    846  1.17   chris 			    --pmap->pm_stats.wired_count;
    847  1.17   chris 			break;
    848  1.17   chris 		}
    849  1.17   chris 		prevptr = &pve->pv_next;		/* previous pointer */
    850  1.17   chris 		pve = pve->pv_next;			/* advance */
    851  1.17   chris 	}
    852  1.17   chris 	return(pve);				/* return removed pve */
    853  1.17   chris }
    854  1.17   chris 
    855  1.17   chris /*
    856  1.17   chris  *
    857  1.17   chris  * pmap_modify_pv: Update pv flags
    858  1.17   chris  *
    859  1.17   chris  * => caller should hold lock on pv_head [so that attrs can be adjusted]
    860  1.17   chris  * => caller should NOT adjust pmap's wire_count
    861  1.17   chris  * => we return the old flags
    862  1.17   chris  *
    863   1.1    matt  * Modify a physical-virtual mapping in the pv table
    864   1.1    matt  */
    865   1.1    matt 
    866   1.1    matt /*__inline */ u_int
    867  1.17   chris pmap_modify_pv(pmap, va, pvh, bic_mask, eor_mask)
    868  1.15   chris 	struct pmap *pmap;
    869   1.1    matt 	vaddr_t va;
    870  1.17   chris 	struct pv_head *pvh;
    871   1.1    matt 	u_int bic_mask;
    872   1.1    matt 	u_int eor_mask;
    873   1.1    matt {
    874   1.1    matt 	struct pv_entry *npv;
    875   1.1    matt 	u_int flags, oflags;
    876   1.1    matt 
    877   1.1    matt 	/*
    878   1.1    matt 	 * There is at least one VA mapping this page.
    879   1.1    matt 	 */
    880   1.1    matt 
    881  1.17   chris 	for (npv = pvh->pvh_list; npv; npv = npv->pv_next) {
    882   1.1    matt 		if (pmap == npv->pv_pmap && va == npv->pv_va) {
    883   1.1    matt 			oflags = npv->pv_flags;
    884   1.1    matt 			npv->pv_flags = flags =
    885   1.1    matt 			    ((oflags & ~bic_mask) ^ eor_mask);
    886   1.1    matt 			if ((flags ^ oflags) & PT_W) {
    887   1.1    matt 				if (flags & PT_W)
    888   1.1    matt 					++pmap->pm_stats.wired_count;
    889   1.1    matt 				else
    890   1.1    matt 					--pmap->pm_stats.wired_count;
    891   1.1    matt 			}
    892   1.1    matt 			return (oflags);
    893   1.1    matt 		}
    894   1.1    matt 	}
    895   1.1    matt 	return (0);
    896   1.1    matt }
    897   1.1    matt 
    898   1.1    matt 
    899   1.1    matt /*
    900   1.1    matt  * Map the specified level 2 pagetable into the level 1 page table for
    901   1.1    matt  * the given pmap to cover a chunk of virtual address space starting from the
    902   1.1    matt  * address specified.
    903   1.1    matt  */
    904   1.1    matt static /*__inline*/ void
    905  1.17   chris pmap_map_in_l1(pmap, va, l2pa, selfref)
    906  1.15   chris 	struct pmap *pmap;
    907   1.1    matt 	vaddr_t va, l2pa;
    908  1.17   chris 	boolean_t selfref;
    909   1.1    matt {
    910   1.1    matt 	vaddr_t ptva;
    911   1.1    matt 
    912   1.1    matt 	/* Calculate the index into the L1 page table. */
    913   1.1    matt 	ptva = (va >> PDSHIFT) & ~3;
    914   1.1    matt 
    915   1.1    matt 	PDEBUG(0, printf("wiring %08lx in to pd%p pte0x%lx va0x%lx\n", l2pa,
    916   1.1    matt 	    pmap->pm_pdir, L1_PTE(l2pa), ptva));
    917   1.1    matt 
    918   1.1    matt 	/* Map page table into the L1. */
    919   1.1    matt 	pmap->pm_pdir[ptva + 0] = L1_PTE(l2pa + 0x000);
    920   1.1    matt 	pmap->pm_pdir[ptva + 1] = L1_PTE(l2pa + 0x400);
    921   1.1    matt 	pmap->pm_pdir[ptva + 2] = L1_PTE(l2pa + 0x800);
    922   1.1    matt 	pmap->pm_pdir[ptva + 3] = L1_PTE(l2pa + 0xc00);
    923   1.1    matt 
    924   1.1    matt 	PDEBUG(0, printf("pt self reference %lx in %lx\n",
    925   1.1    matt 	    L2_PTE_NC_NB(l2pa, AP_KRW), pmap->pm_vptpt));
    926   1.1    matt 
    927   1.1    matt 	/* Map the page table into the page table area. */
    928  1.17   chris 	if (selfref) {
    929  1.17   chris 		*((pt_entry_t *)(pmap->pm_vptpt + ptva)) =
    930  1.17   chris 			L2_PTE_NC_NB(l2pa, AP_KRW);
    931  1.17   chris 	}
    932   1.1    matt 	/* XXX should be a purge */
    933   1.1    matt /*	cpu_tlb_flushD();*/
    934   1.1    matt }
    935   1.1    matt 
    936   1.1    matt #if 0
    937   1.1    matt static /*__inline*/ void
    938   1.1    matt pmap_unmap_in_l1(pmap, va)
    939  1.15   chris 	struct pmap *pmap;
    940   1.1    matt 	vaddr_t va;
    941   1.1    matt {
    942   1.1    matt 	vaddr_t ptva;
    943   1.1    matt 
    944   1.1    matt 	/* Calculate the index into the L1 page table. */
    945   1.1    matt 	ptva = (va >> PDSHIFT) & ~3;
    946   1.1    matt 
    947   1.1    matt 	/* Unmap page table from the L1. */
    948   1.1    matt 	pmap->pm_pdir[ptva + 0] = 0;
    949   1.1    matt 	pmap->pm_pdir[ptva + 1] = 0;
    950   1.1    matt 	pmap->pm_pdir[ptva + 2] = 0;
    951   1.1    matt 	pmap->pm_pdir[ptva + 3] = 0;
    952   1.1    matt 
    953   1.1    matt 	/* Unmap the page table from the page table area. */
    954   1.1    matt 	*((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
    955   1.1    matt 
    956   1.1    matt 	/* XXX should be a purge */
    957   1.1    matt /*	cpu_tlb_flushD();*/
    958   1.1    matt }
    959   1.1    matt #endif
    960   1.1    matt 
    961   1.1    matt 
    962   1.1    matt /*
    963   1.1    matt  *	Used to map a range of physical addresses into kernel
    964   1.1    matt  *	virtual address space.
    965   1.1    matt  *
    966   1.1    matt  *	For now, VM is already on, we only need to map the
    967   1.1    matt  *	specified memory.
    968   1.1    matt  */
    969   1.1    matt vaddr_t
    970   1.1    matt pmap_map(va, spa, epa, prot)
    971   1.1    matt 	vaddr_t va, spa, epa;
    972   1.1    matt 	int prot;
    973   1.1    matt {
    974   1.1    matt 	while (spa < epa) {
    975  1.20   chris 		pmap_kenter_pa(va, spa, prot);
    976   1.1    matt 		va += NBPG;
    977   1.1    matt 		spa += NBPG;
    978   1.1    matt 	}
    979  1.19   chris 	pmap_update(pmap_kernel());
    980   1.1    matt 	return(va);
    981   1.1    matt }
    982   1.1    matt 
    983   1.1    matt 
    984   1.1    matt /*
    985   1.3    matt  * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
    986   1.1    matt  *
    987   1.1    matt  * bootstrap the pmap system. This is called from initarm and allows
    988   1.1    matt  * the pmap system to initailise any structures it requires.
    989   1.1    matt  *
    990   1.1    matt  * Currently this sets up the kernel_pmap that is statically allocated
    991   1.1    matt  * and also allocated virtual addresses for certain page hooks.
    992   1.1    matt  * Currently the only one page hook is allocated that is used
    993   1.1    matt  * to zero physical pages of memory.
    994   1.1    matt  * It also initialises the start and end address of the kernel data space.
    995   1.1    matt  */
    996   1.2    matt extern paddr_t physical_freestart;
    997   1.2    matt extern paddr_t physical_freeend;
    998   1.1    matt 
    999  1.17   chris char *boot_head;
   1000   1.1    matt 
   1001   1.1    matt void
   1002   1.1    matt pmap_bootstrap(kernel_l1pt, kernel_ptpt)
   1003   1.1    matt 	pd_entry_t *kernel_l1pt;
   1004   1.1    matt 	pv_addr_t kernel_ptpt;
   1005   1.1    matt {
   1006   1.1    matt 	int loop;
   1007   1.2    matt 	paddr_t start, end;
   1008   1.1    matt #if NISADMA > 0
   1009   1.2    matt 	paddr_t istart;
   1010   1.2    matt 	psize_t isize;
   1011   1.1    matt #endif
   1012   1.1    matt 
   1013  1.15   chris 	pmap_kernel()->pm_pdir = kernel_l1pt;
   1014  1.15   chris 	pmap_kernel()->pm_pptpt = kernel_ptpt.pv_pa;
   1015  1.15   chris 	pmap_kernel()->pm_vptpt = kernel_ptpt.pv_va;
   1016  1.15   chris 	simple_lock_init(&pmap_kernel()->pm_lock);
   1017  1.16   chris 	pmap_kernel()->pm_obj.pgops = NULL;
   1018  1.16   chris 	TAILQ_INIT(&(pmap_kernel()->pm_obj.memq));
   1019  1.16   chris 	pmap_kernel()->pm_obj.uo_npages = 0;
   1020  1.16   chris 	pmap_kernel()->pm_obj.uo_refs = 1;
   1021  1.16   chris 
   1022   1.1    matt 	/*
   1023   1.1    matt 	 * Initialize PAGE_SIZE-dependent variables.
   1024   1.1    matt 	 */
   1025   1.1    matt 	uvm_setpagesize();
   1026   1.1    matt 
   1027   1.1    matt 	npages = 0;
   1028   1.1    matt 	loop = 0;
   1029   1.1    matt 	while (loop < bootconfig.dramblocks) {
   1030   1.2    matt 		start = (paddr_t)bootconfig.dram[loop].address;
   1031   1.1    matt 		end = start + (bootconfig.dram[loop].pages * NBPG);
   1032   1.1    matt 		if (start < physical_freestart)
   1033   1.1    matt 			start = physical_freestart;
   1034   1.1    matt 		if (end > physical_freeend)
   1035   1.1    matt 			end = physical_freeend;
   1036   1.1    matt #if 0
   1037   1.1    matt 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
   1038   1.1    matt #endif
   1039   1.1    matt #if NISADMA > 0
   1040   1.1    matt 		if (pmap_isa_dma_range_intersect(start, end - start,
   1041   1.1    matt 		    &istart, &isize)) {
   1042   1.1    matt 			/*
   1043   1.1    matt 			 * Place the pages that intersect with the
   1044   1.1    matt 			 * ISA DMA range onto the ISA DMA free list.
   1045   1.1    matt 			 */
   1046   1.1    matt #if 0
   1047   1.1    matt 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
   1048   1.1    matt 			    istart + isize - 1);
   1049   1.1    matt #endif
   1050   1.1    matt 			uvm_page_physload(atop(istart),
   1051   1.1    matt 			    atop(istart + isize), atop(istart),
   1052   1.1    matt 			    atop(istart + isize), VM_FREELIST_ISADMA);
   1053   1.1    matt 			npages += atop(istart + isize) - atop(istart);
   1054   1.1    matt 
   1055   1.1    matt 			/*
   1056   1.1    matt 			 * Load the pieces that come before
   1057   1.1    matt 			 * the intersection into the default
   1058   1.1    matt 			 * free list.
   1059   1.1    matt 			 */
   1060   1.1    matt 			if (start < istart) {
   1061   1.1    matt #if 0
   1062   1.1    matt 				printf("    BEFORE 0x%lx -> 0x%lx\n",
   1063   1.1    matt 				    start, istart - 1);
   1064   1.1    matt #endif
   1065   1.1    matt 				uvm_page_physload(atop(start),
   1066   1.1    matt 				    atop(istart), atop(start),
   1067   1.1    matt 				    atop(istart), VM_FREELIST_DEFAULT);
   1068   1.1    matt 				npages += atop(istart) - atop(start);
   1069   1.1    matt 			}
   1070   1.1    matt 
   1071   1.1    matt 			/*
   1072   1.1    matt 			 * Load the pieces that come after
   1073   1.1    matt 			 * the intersection into the default
   1074   1.1    matt 			 * free list.
   1075   1.1    matt 			 */
   1076   1.1    matt 			if ((istart + isize) < end) {
   1077   1.1    matt #if 0
   1078   1.1    matt 				printf("     AFTER 0x%lx -> 0x%lx\n",
   1079   1.1    matt 				    (istart + isize), end - 1);
   1080   1.1    matt #endif
   1081   1.1    matt 				uvm_page_physload(atop(istart + isize),
   1082   1.1    matt 				    atop(end), atop(istart + isize),
   1083   1.1    matt 				    atop(end), VM_FREELIST_DEFAULT);
   1084   1.1    matt 				npages += atop(end) - atop(istart + isize);
   1085   1.1    matt 			}
   1086   1.1    matt 		} else {
   1087   1.1    matt 			uvm_page_physload(atop(start), atop(end),
   1088   1.1    matt 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
   1089   1.1    matt 			npages += atop(end) - atop(start);
   1090   1.1    matt 		}
   1091   1.1    matt #else	/* NISADMA > 0 */
   1092   1.1    matt 		uvm_page_physload(atop(start), atop(end),
   1093   1.1    matt 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
   1094   1.1    matt 		npages += atop(end) - atop(start);
   1095   1.1    matt #endif /* NISADMA > 0 */
   1096   1.1    matt 		++loop;
   1097   1.1    matt 	}
   1098   1.1    matt 
   1099   1.1    matt #ifdef MYCROFT_HACK
   1100   1.1    matt 	printf("npages = %ld\n", npages);
   1101   1.1    matt #endif
   1102   1.1    matt 
   1103   1.1    matt 	virtual_start = KERNEL_VM_BASE;
   1104   1.1    matt 	virtual_end = virtual_start + KERNEL_VM_SIZE - 1;
   1105   1.1    matt 
   1106   1.1    matt 	ALLOC_PAGE_HOOK(page_hook0, NBPG);
   1107   1.1    matt 	ALLOC_PAGE_HOOK(page_hook1, NBPG);
   1108   1.1    matt 
   1109   1.1    matt 	/*
   1110   1.1    matt 	 * The mem special device needs a virtual hook but we don't
   1111   1.1    matt 	 * need a pte
   1112   1.1    matt 	 */
   1113   1.1    matt 	memhook = (char *)virtual_start;
   1114   1.1    matt 	virtual_start += NBPG;
   1115   1.1    matt 
   1116   1.1    matt 	msgbufaddr = (caddr_t)virtual_start;
   1117  1.15   chris 	msgbufpte = (pt_entry_t)pmap_pte(pmap_kernel(), virtual_start);
   1118   1.1    matt 	virtual_start += round_page(MSGBUFSIZE);
   1119   1.1    matt 
   1120  1.17   chris 	/*
   1121  1.17   chris 	 * init the static-global locks and global lists.
   1122  1.17   chris 	 */
   1123  1.17   chris 	spinlockinit(&pmap_main_lock, "pmaplk", 0);
   1124  1.17   chris 	simple_lock_init(&pvalloc_lock);
   1125  1.17   chris 	TAILQ_INIT(&pv_freepages);
   1126  1.17   chris 	TAILQ_INIT(&pv_unusedpgs);
   1127   1.1    matt 
   1128  1.10   chris 	/*
   1129  1.17   chris 	 * compute the number of pages we have and then allocate RAM
   1130  1.17   chris 	 * for each pages' pv_head and saved attributes.
   1131  1.17   chris 	 */
   1132  1.17   chris 	{
   1133  1.17   chris 	       	int npages, lcv;
   1134  1.17   chris 		vsize_t s;
   1135  1.17   chris 
   1136  1.17   chris 		npages = 0;
   1137  1.17   chris 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
   1138  1.17   chris 			npages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
   1139  1.17   chris 		s = (vsize_t) (sizeof(struct pv_head) * npages +
   1140  1.17   chris 				sizeof(char) * npages);
   1141  1.17   chris 		s = round_page(s); /* round up */
   1142  1.17   chris 		boot_head = (char *)uvm_pageboot_alloc(s);
   1143  1.17   chris 		bzero((char *)boot_head, s);
   1144  1.17   chris 		if (boot_head == 0)
   1145  1.17   chris 			panic("pmap_init: unable to allocate pv_heads");
   1146  1.17   chris 	}
   1147  1.17   chris 
   1148  1.17   chris 	/*
   1149  1.10   chris 	 * initialize the pmap pool.
   1150  1.10   chris 	 */
   1151  1.10   chris 
   1152  1.10   chris 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
   1153  1.10   chris 		  0, pool_page_alloc_nointr, pool_page_free_nointr, M_VMPMAP);
   1154  1.10   chris 
   1155   1.1    matt 	cpu_cache_cleanD();
   1156   1.1    matt }
   1157   1.1    matt 
   1158   1.1    matt /*
   1159   1.1    matt  * void pmap_init(void)
   1160   1.1    matt  *
   1161   1.1    matt  * Initialize the pmap module.
   1162   1.1    matt  * Called by vm_init() in vm/vm_init.c in order to initialise
   1163   1.1    matt  * any structures that the pmap system needs to map virtual memory.
   1164   1.1    matt  */
   1165   1.1    matt 
   1166   1.1    matt extern int physmem;
   1167   1.1    matt 
   1168   1.1    matt void
   1169   1.1    matt pmap_init()
   1170   1.1    matt {
   1171  1.17   chris 	int lcv, i;
   1172   1.1    matt 
   1173   1.1    matt #ifdef MYCROFT_HACK
   1174   1.1    matt 	printf("physmem = %d\n", physmem);
   1175   1.1    matt #endif
   1176   1.1    matt 
   1177   1.1    matt 	/*
   1178   1.1    matt 	 * Set the available memory vars - These do not map to real memory
   1179   1.1    matt 	 * addresses and cannot as the physical memory is fragmented.
   1180   1.1    matt 	 * They are used by ps for %mem calculations.
   1181   1.1    matt 	 * One could argue whether this should be the entire memory or just
   1182   1.1    matt 	 * the memory that is useable in a user process.
   1183   1.1    matt 	 */
   1184   1.1    matt 	avail_start = 0;
   1185   1.1    matt 	avail_end = physmem * NBPG;
   1186   1.1    matt 
   1187  1.17   chris 	/* allocate pv_head stuff first */
   1188  1.17   chris 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
   1189  1.17   chris 		vm_physmem[lcv].pmseg.pvhead = (struct pv_head *)boot_head;
   1190  1.17   chris 		boot_head = (char *)(vaddr_t)(vm_physmem[lcv].pmseg.pvhead +
   1191  1.17   chris 				 (vm_physmem[lcv].end - vm_physmem[lcv].start));
   1192  1.17   chris 		for (i = 0;
   1193  1.17   chris 		     i < (vm_physmem[lcv].end - vm_physmem[lcv].start); i++) {
   1194  1.17   chris 			simple_lock_init(
   1195  1.17   chris 			    &vm_physmem[lcv].pmseg.pvhead[i].pvh_lock);
   1196  1.17   chris 		}
   1197   1.1    matt 	}
   1198  1.17   chris 
   1199  1.17   chris 	/* now allocate attrs */
   1200  1.17   chris 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
   1201  1.17   chris 		vm_physmem[lcv].pmseg.attrs = (char *) boot_head;
   1202  1.17   chris 		boot_head = (char *)(vaddr_t)(vm_physmem[lcv].pmseg.attrs +
   1203  1.17   chris 				 (vm_physmem[lcv].end - vm_physmem[lcv].start));
   1204  1.17   chris 	}
   1205  1.17   chris 
   1206  1.17   chris 	/*
   1207  1.17   chris 	 * now we need to free enough pv_entry structures to allow us to get
   1208  1.17   chris 	 * the kmem_map/kmem_object allocated and inited (done after this
   1209  1.17   chris 	 * function is finished).  to do this we allocate one bootstrap page out
   1210  1.17   chris 	 * of kernel_map and use it to provide an initial pool of pv_entry
   1211  1.17   chris 	 * structures.   we never free this page.
   1212  1.17   chris 	 */
   1213  1.17   chris 
   1214  1.17   chris 	pv_initpage = (struct pv_page *) uvm_km_alloc(kernel_map, PAGE_SIZE);
   1215  1.17   chris 	if (pv_initpage == NULL)
   1216  1.17   chris 		panic("pmap_init: pv_initpage");
   1217  1.17   chris 	pv_cachedva = 0;   /* a VA we have allocated but not used yet */
   1218  1.17   chris 	pv_nfpvents = 0;
   1219  1.17   chris 	(void) pmap_add_pvpage(pv_initpage, FALSE);
   1220  1.17   chris 
   1221   1.1    matt #ifdef MYCROFT_HACK
   1222   1.1    matt 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
   1223   1.1    matt 		printf("physseg[%d] pvent=%p attrs=%p start=%ld end=%ld\n",
   1224   1.1    matt 		    lcv,
   1225   1.1    matt 		    vm_physmem[lcv].pmseg.pvent, vm_physmem[lcv].pmseg.attrs,
   1226   1.1    matt 		    vm_physmem[lcv].start, vm_physmem[lcv].end);
   1227   1.1    matt 	}
   1228   1.1    matt #endif
   1229   1.1    matt 	pmap_initialized = TRUE;
   1230   1.1    matt 
   1231   1.1    matt 	/* Initialise our L1 page table queues and counters */
   1232   1.1    matt 	SIMPLEQ_INIT(&l1pt_static_queue);
   1233   1.1    matt 	l1pt_static_queue_count = 0;
   1234   1.1    matt 	l1pt_static_create_count = 0;
   1235   1.1    matt 	SIMPLEQ_INIT(&l1pt_queue);
   1236   1.1    matt 	l1pt_queue_count = 0;
   1237   1.1    matt 	l1pt_create_count = 0;
   1238   1.1    matt 	l1pt_reuse_count = 0;
   1239   1.1    matt }
   1240   1.1    matt 
   1241   1.1    matt /*
   1242   1.1    matt  * pmap_postinit()
   1243   1.1    matt  *
   1244   1.1    matt  * This routine is called after the vm and kmem subsystems have been
   1245   1.1    matt  * initialised. This allows the pmap code to perform any initialisation
   1246   1.1    matt  * that can only be done one the memory allocation is in place.
   1247   1.1    matt  */
   1248   1.1    matt 
   1249   1.1    matt void
   1250   1.1    matt pmap_postinit()
   1251   1.1    matt {
   1252   1.1    matt 	int loop;
   1253   1.1    matt 	struct l1pt *pt;
   1254   1.1    matt 
   1255   1.1    matt #ifdef PMAP_STATIC_L1S
   1256   1.1    matt 	for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
   1257   1.1    matt #else	/* PMAP_STATIC_L1S */
   1258   1.1    matt 	for (loop = 0; loop < max_processes; ++loop) {
   1259   1.1    matt #endif	/* PMAP_STATIC_L1S */
   1260   1.1    matt 		/* Allocate a L1 page table */
   1261   1.1    matt 		pt = pmap_alloc_l1pt();
   1262   1.1    matt 		if (!pt)
   1263   1.1    matt 			panic("Cannot allocate static L1 page tables\n");
   1264   1.1    matt 
   1265   1.1    matt 		/* Clean it */
   1266   1.1    matt 		bzero((void *)pt->pt_va, PD_SIZE);
   1267   1.1    matt 		pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
   1268   1.1    matt 		/* Add the page table to the queue */
   1269   1.1    matt 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
   1270   1.1    matt 		++l1pt_static_queue_count;
   1271   1.1    matt 		++l1pt_static_create_count;
   1272   1.1    matt 	}
   1273   1.1    matt }
   1274   1.1    matt 
   1275   1.1    matt 
   1276   1.1    matt /*
   1277   1.1    matt  * Create and return a physical map.
   1278   1.1    matt  *
   1279   1.1    matt  * If the size specified for the map is zero, the map is an actual physical
   1280   1.1    matt  * map, and may be referenced by the hardware.
   1281   1.1    matt  *
   1282   1.1    matt  * If the size specified is non-zero, the map will be used in software only,
   1283   1.1    matt  * and is bounded by that size.
   1284   1.1    matt  */
   1285   1.1    matt 
   1286   1.1    matt pmap_t
   1287   1.1    matt pmap_create()
   1288   1.1    matt {
   1289  1.15   chris 	struct pmap *pmap;
   1290   1.1    matt 
   1291  1.10   chris 	/*
   1292  1.10   chris 	 * Fetch pmap entry from the pool
   1293  1.10   chris 	 */
   1294  1.10   chris 
   1295  1.10   chris 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
   1296  1.17   chris 	/* XXX is this really needed! */
   1297  1.17   chris 	memset(pmap, 0, sizeof(*pmap));
   1298   1.1    matt 
   1299  1.16   chris 	simple_lock_init(&pmap->pm_obj.vmobjlock);
   1300  1.16   chris 	pmap->pm_obj.pgops = NULL;	/* currently not a mappable object */
   1301  1.16   chris 	TAILQ_INIT(&pmap->pm_obj.memq);
   1302  1.16   chris 	pmap->pm_obj.uo_npages = 0;
   1303  1.16   chris 	pmap->pm_obj.uo_refs = 1;
   1304  1.16   chris 	pmap->pm_stats.wired_count = 0;
   1305  1.16   chris 	pmap->pm_stats.resident_count = 1;
   1306  1.16   chris 
   1307   1.1    matt 	/* Now init the machine part of the pmap */
   1308   1.1    matt 	pmap_pinit(pmap);
   1309   1.1    matt 	return(pmap);
   1310   1.1    matt }
   1311   1.1    matt 
   1312   1.1    matt /*
   1313   1.1    matt  * pmap_alloc_l1pt()
   1314   1.1    matt  *
   1315   1.1    matt  * This routine allocates physical and virtual memory for a L1 page table
   1316   1.1    matt  * and wires it.
   1317   1.1    matt  * A l1pt structure is returned to describe the allocated page table.
   1318   1.1    matt  *
   1319   1.1    matt  * This routine is allowed to fail if the required memory cannot be allocated.
   1320   1.1    matt  * In this case NULL is returned.
   1321   1.1    matt  */
   1322   1.1    matt 
   1323   1.1    matt struct l1pt *
   1324   1.1    matt pmap_alloc_l1pt(void)
   1325   1.1    matt {
   1326   1.2    matt 	paddr_t pa;
   1327   1.2    matt 	vaddr_t va;
   1328   1.1    matt 	struct l1pt *pt;
   1329   1.1    matt 	int error;
   1330   1.9     chs 	struct vm_page *m;
   1331  1.11   chris 	pt_entry_t *ptes;
   1332   1.1    matt 
   1333   1.1    matt 	/* Allocate virtual address space for the L1 page table */
   1334   1.1    matt 	va = uvm_km_valloc(kernel_map, PD_SIZE);
   1335   1.1    matt 	if (va == 0) {
   1336   1.1    matt #ifdef DIAGNOSTIC
   1337   1.1    matt 		printf("pmap: Cannot allocate pageable memory for L1\n");
   1338   1.1    matt #endif	/* DIAGNOSTIC */
   1339   1.1    matt 		return(NULL);
   1340   1.1    matt 	}
   1341   1.1    matt 
   1342   1.1    matt 	/* Allocate memory for the l1pt structure */
   1343   1.1    matt 	pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
   1344   1.1    matt 
   1345   1.1    matt 	/*
   1346   1.1    matt 	 * Allocate pages from the VM system.
   1347   1.1    matt 	 */
   1348   1.1    matt 	TAILQ_INIT(&pt->pt_plist);
   1349   1.1    matt 	error = uvm_pglistalloc(PD_SIZE, physical_start, physical_end,
   1350   1.1    matt 	    PD_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
   1351   1.1    matt 	if (error) {
   1352   1.1    matt #ifdef DIAGNOSTIC
   1353   1.1    matt 		printf("pmap: Cannot allocate physical memory for L1 (%d)\n",
   1354   1.1    matt 		    error);
   1355   1.1    matt #endif	/* DIAGNOSTIC */
   1356   1.1    matt 		/* Release the resources we already have claimed */
   1357   1.1    matt 		free(pt, M_VMPMAP);
   1358   1.1    matt 		uvm_km_free(kernel_map, va, PD_SIZE);
   1359   1.1    matt 		return(NULL);
   1360   1.1    matt 	}
   1361   1.1    matt 
   1362   1.1    matt 	/* Map our physical pages into our virtual space */
   1363   1.1    matt 	pt->pt_va = va;
   1364   1.1    matt 	m = pt->pt_plist.tqh_first;
   1365  1.11   chris 	ptes = pmap_map_ptes(pmap_kernel());
   1366   1.1    matt 	while (m && va < (pt->pt_va + PD_SIZE)) {
   1367   1.1    matt 		pa = VM_PAGE_TO_PHYS(m);
   1368   1.1    matt 
   1369  1.20   chris 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
   1370   1.1    matt 
   1371   1.1    matt 		/* Revoke cacheability and bufferability */
   1372   1.1    matt 		/* XXX should be done better than this */
   1373  1.11   chris 		ptes[arm_byte_to_page(va)] &= ~(PT_C | PT_B);
   1374   1.1    matt 
   1375   1.1    matt 		va += NBPG;
   1376   1.1    matt 		m = m->pageq.tqe_next;
   1377   1.1    matt 	}
   1378  1.11   chris 	pmap_unmap_ptes(pmap_kernel());
   1379  1.19   chris 	pmap_update(pmap_kernel());
   1380   1.1    matt 
   1381   1.1    matt #ifdef DIAGNOSTIC
   1382   1.1    matt 	if (m)
   1383   1.1    matt 		panic("pmap_alloc_l1pt: pglist not empty\n");
   1384   1.1    matt #endif	/* DIAGNOSTIC */
   1385   1.1    matt 
   1386   1.1    matt 	pt->pt_flags = 0;
   1387   1.1    matt 	return(pt);
   1388   1.1    matt }
   1389   1.1    matt 
   1390   1.1    matt /*
   1391   1.1    matt  * Free a L1 page table previously allocated with pmap_alloc_l1pt().
   1392   1.1    matt  */
   1393   1.1    matt void
   1394   1.1    matt pmap_free_l1pt(pt)
   1395   1.1    matt 	struct l1pt *pt;
   1396   1.1    matt {
   1397   1.1    matt 	/* Separate the physical memory for the virtual space */
   1398  1.20   chris 	pmap_kremove(pt->pt_va, PD_SIZE);
   1399  1.19   chris 	pmap_update(pmap_kernel());
   1400   1.1    matt 
   1401   1.1    matt 	/* Return the physical memory */
   1402   1.1    matt 	uvm_pglistfree(&pt->pt_plist);
   1403   1.1    matt 
   1404   1.1    matt 	/* Free the virtual space */
   1405   1.1    matt 	uvm_km_free(kernel_map, pt->pt_va, PD_SIZE);
   1406   1.1    matt 
   1407   1.1    matt 	/* Free the l1pt structure */
   1408   1.1    matt 	free(pt, M_VMPMAP);
   1409   1.1    matt }
   1410   1.1    matt 
   1411   1.1    matt /*
   1412   1.1    matt  * Allocate a page directory.
   1413   1.1    matt  * This routine will either allocate a new page directory from the pool
   1414   1.1    matt  * of L1 page tables currently held by the kernel or it will allocate
   1415   1.1    matt  * a new one via pmap_alloc_l1pt().
   1416   1.1    matt  * It will then initialise the l1 page table for use.
   1417   1.1    matt  */
   1418   1.1    matt int
   1419   1.1    matt pmap_allocpagedir(pmap)
   1420   1.1    matt 	struct pmap *pmap;
   1421   1.1    matt {
   1422   1.2    matt 	paddr_t pa;
   1423   1.1    matt 	struct l1pt *pt;
   1424   1.1    matt 	pt_entry_t *pte;
   1425   1.1    matt 
   1426   1.1    matt 	PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
   1427   1.1    matt 
   1428   1.1    matt 	/* Do we have any spare L1's lying around ? */
   1429   1.1    matt 	if (l1pt_static_queue_count) {
   1430   1.1    matt 		--l1pt_static_queue_count;
   1431   1.1    matt 		pt = l1pt_static_queue.sqh_first;
   1432   1.1    matt 		SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
   1433   1.1    matt 	} else if (l1pt_queue_count) {
   1434   1.1    matt 		--l1pt_queue_count;
   1435   1.1    matt 		pt = l1pt_queue.sqh_first;
   1436   1.1    matt 		SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
   1437   1.1    matt 		++l1pt_reuse_count;
   1438   1.1    matt 	} else {
   1439   1.1    matt 		pt = pmap_alloc_l1pt();
   1440   1.1    matt 		if (!pt)
   1441   1.1    matt 			return(ENOMEM);
   1442   1.1    matt 		++l1pt_create_count;
   1443   1.1    matt 	}
   1444   1.1    matt 
   1445   1.1    matt 	/* Store the pointer to the l1 descriptor in the pmap. */
   1446   1.1    matt 	pmap->pm_l1pt = pt;
   1447   1.1    matt 
   1448   1.1    matt 	/* Get the physical address of the start of the l1 */
   1449   1.1    matt 	pa = VM_PAGE_TO_PHYS(pt->pt_plist.tqh_first);
   1450   1.1    matt 
   1451   1.1    matt 	/* Store the virtual address of the l1 in the pmap. */
   1452   1.1    matt 	pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
   1453   1.1    matt 
   1454   1.1    matt 	/* Clean the L1 if it is dirty */
   1455   1.1    matt 	if (!(pt->pt_flags & PTFLAG_CLEAN))
   1456   1.1    matt 		bzero((void *)pmap->pm_pdir, (PD_SIZE - KERNEL_PD_SIZE));
   1457   1.1    matt 
   1458   1.1    matt 	/* Do we already have the kernel mappings ? */
   1459   1.1    matt 	if (!(pt->pt_flags & PTFLAG_KPT)) {
   1460   1.1    matt 		/* Duplicate the kernel mapping i.e. all mappings 0xf0000000+ */
   1461   1.1    matt 
   1462  1.15   chris 		bcopy((char *)pmap_kernel()->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1463   1.1    matt 		    (char *)pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
   1464   1.1    matt 		    KERNEL_PD_SIZE);
   1465   1.1    matt 		pt->pt_flags |= PTFLAG_KPT;
   1466   1.1    matt 	}
   1467   1.1    matt 
   1468   1.1    matt 	/* Allocate a page table to map all the page tables for this pmap */
   1469   1.1    matt 
   1470   1.1    matt #ifdef DIAGNOSTIC
   1471   1.1    matt 	if (pmap->pm_vptpt) {
   1472   1.1    matt 		/* XXX What if we have one already ? */
   1473   1.1    matt 		panic("pmap_allocpagedir: have pt already\n");
   1474   1.1    matt 	}
   1475   1.1    matt #endif	/* DIAGNOSTIC */
   1476   1.1    matt 	pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
   1477   1.5  toshii 	if (pmap->pm_vptpt == 0) {
   1478   1.5  toshii 		pmap_freepagedir(pmap);
   1479   1.5  toshii 		return(ENOMEM);
   1480   1.5  toshii 	}
   1481   1.5  toshii 
   1482  1.15   chris 	(void) pmap_extract(pmap_kernel(), pmap->pm_vptpt, &pmap->pm_pptpt);
   1483   1.1    matt 	pmap->pm_pptpt &= PG_FRAME;
   1484   1.1    matt 	/* Revoke cacheability and bufferability */
   1485   1.1    matt 	/* XXX should be done better than this */
   1486  1.15   chris 	pte = pmap_pte(pmap_kernel(), pmap->pm_vptpt);
   1487   1.1    matt 	*pte = *pte & ~(PT_C | PT_B);
   1488   1.1    matt 
   1489   1.1    matt 	/* Wire in this page table */
   1490  1.17   chris 	pmap_map_in_l1(pmap, PROCESS_PAGE_TBLS_BASE, pmap->pm_pptpt, TRUE);
   1491   1.1    matt 
   1492   1.1    matt 	pt->pt_flags &= ~PTFLAG_CLEAN;	/* L1 is dirty now */
   1493   1.1    matt 
   1494   1.1    matt 	/*
   1495   1.1    matt 	 * Map the kernel page tables for 0xf0000000 +
   1496   1.1    matt 	 * into the page table used to map the
   1497   1.1    matt 	 * pmap's page tables
   1498   1.1    matt 	 */
   1499   1.1    matt 	bcopy((char *)(PROCESS_PAGE_TBLS_BASE
   1500   1.1    matt 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2))
   1501   1.1    matt 	    + ((PD_SIZE - KERNEL_PD_SIZE) >> 2)),
   1502   1.1    matt 	    (char *)pmap->pm_vptpt + ((PD_SIZE - KERNEL_PD_SIZE) >> 2),
   1503   1.1    matt 	    (KERNEL_PD_SIZE >> 2));
   1504   1.1    matt 
   1505   1.1    matt 	return(0);
   1506   1.1    matt }
   1507   1.1    matt 
   1508   1.1    matt 
   1509   1.1    matt /*
   1510   1.1    matt  * Initialize a preallocated and zeroed pmap structure,
   1511   1.1    matt  * such as one in a vmspace structure.
   1512   1.1    matt  */
   1513   1.1    matt 
   1514   1.1    matt void
   1515   1.1    matt pmap_pinit(pmap)
   1516   1.1    matt 	struct pmap *pmap;
   1517   1.1    matt {
   1518   1.1    matt 	PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
   1519   1.1    matt 
   1520   1.1    matt 	/* Keep looping until we succeed in allocating a page directory */
   1521   1.1    matt 	while (pmap_allocpagedir(pmap) != 0) {
   1522   1.1    matt 		/*
   1523   1.1    matt 		 * Ok we failed to allocate a suitable block of memory for an
   1524   1.1    matt 		 * L1 page table. This means that either:
   1525   1.1    matt 		 * 1. 16KB of virtual address space could not be allocated
   1526   1.1    matt 		 * 2. 16KB of physically contiguous memory on a 16KB boundary
   1527   1.1    matt 		 *    could not be allocated.
   1528   1.1    matt 		 *
   1529   1.1    matt 		 * Since we cannot fail we will sleep for a while and try
   1530  1.17   chris 		 * again.
   1531   1.1    matt 		 */
   1532  1.17   chris 		(void) ltsleep(&lbolt, PVM, "l1ptwait", hz >> 3, NULL);
   1533   1.1    matt 	}
   1534   1.1    matt 
   1535   1.1    matt 	/* Map zero page for the pmap. This will also map the L2 for it */
   1536   1.1    matt 	pmap_enter(pmap, 0x00000000, systempage.pv_pa,
   1537   1.1    matt 	    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1538  1.19   chris 	pmap_update(pmap);
   1539   1.1    matt }
   1540   1.1    matt 
   1541   1.1    matt 
   1542   1.1    matt void
   1543   1.1    matt pmap_freepagedir(pmap)
   1544  1.15   chris 	struct pmap *pmap;
   1545   1.1    matt {
   1546   1.1    matt 	/* Free the memory used for the page table mapping */
   1547   1.5  toshii 	if (pmap->pm_vptpt != 0)
   1548   1.5  toshii 		uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
   1549   1.1    matt 
   1550   1.1    matt 	/* junk the L1 page table */
   1551   1.1    matt 	if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
   1552   1.1    matt 		/* Add the page table to the queue */
   1553   1.1    matt 		SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
   1554   1.1    matt 		++l1pt_static_queue_count;
   1555   1.1    matt 	} else if (l1pt_queue_count < 8) {
   1556   1.1    matt 		/* Add the page table to the queue */
   1557   1.1    matt 		SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
   1558   1.1    matt 		++l1pt_queue_count;
   1559   1.1    matt 	} else
   1560   1.1    matt 		pmap_free_l1pt(pmap->pm_l1pt);
   1561   1.1    matt }
   1562   1.1    matt 
   1563   1.1    matt 
   1564   1.1    matt /*
   1565   1.1    matt  * Retire the given physical map from service.
   1566   1.1    matt  * Should only be called if the map contains no valid mappings.
   1567   1.1    matt  */
   1568   1.1    matt 
   1569   1.1    matt void
   1570   1.1    matt pmap_destroy(pmap)
   1571  1.15   chris 	struct pmap *pmap;
   1572   1.1    matt {
   1573  1.17   chris 	struct vm_page *page;
   1574   1.1    matt 	int count;
   1575   1.1    matt 
   1576   1.1    matt 	if (pmap == NULL)
   1577   1.1    matt 		return;
   1578   1.1    matt 
   1579   1.1    matt 	PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
   1580  1.17   chris 
   1581  1.17   chris 	/*
   1582  1.17   chris 	 * Drop reference count
   1583  1.17   chris 	 */
   1584  1.17   chris 	simple_lock(&pmap->pm_obj.vmobjlock);
   1585  1.16   chris 	count = --pmap->pm_obj.uo_refs;
   1586  1.17   chris 	simple_unlock(&pmap->pm_obj.vmobjlock);
   1587  1.17   chris 	if (count > 0) {
   1588  1.17   chris 		return;
   1589   1.1    matt 	}
   1590   1.1    matt 
   1591  1.17   chris 	/*
   1592  1.17   chris 	 * reference count is zero, free pmap resources and then free pmap.
   1593  1.17   chris 	 */
   1594  1.17   chris 
   1595   1.1    matt 	/* Remove the zero page mapping */
   1596   1.1    matt 	pmap_remove(pmap, 0x00000000, 0x00000000 + NBPG);
   1597  1.19   chris 	pmap_update(pmap);
   1598   1.1    matt 
   1599   1.1    matt 	/*
   1600   1.1    matt 	 * Free any page tables still mapped
   1601   1.1    matt 	 * This is only temporay until pmap_enter can count the number
   1602   1.1    matt 	 * of mappings made in a page table. Then pmap_remove() can
   1603   1.1    matt 	 * reduce the count and free the pagetable when the count
   1604  1.16   chris 	 * reaches zero.  Note that entries in this list should match the
   1605  1.16   chris 	 * contents of the ptpt, however this is faster than walking a 1024
   1606  1.16   chris 	 * entries looking for pt's
   1607  1.16   chris 	 * taken from i386 pmap.c
   1608   1.1    matt 	 */
   1609  1.16   chris 	while (pmap->pm_obj.memq.tqh_first != NULL) {
   1610  1.16   chris 		page = pmap->pm_obj.memq.tqh_first;
   1611  1.16   chris #ifdef DIAGNOSTIC
   1612  1.16   chris 		if (page->flags & PG_BUSY)
   1613  1.16   chris 			panic("pmap_release: busy page table page");
   1614  1.16   chris #endif
   1615  1.16   chris 		/* pmap_page_protect?  currently no need for it. */
   1616  1.16   chris 
   1617  1.16   chris 		page->wire_count = 0;
   1618  1.16   chris 		uvm_pagefree(page);
   1619   1.1    matt 	}
   1620  1.16   chris 
   1621   1.1    matt 	/* Free the page dir */
   1622   1.1    matt 	pmap_freepagedir(pmap);
   1623  1.17   chris 
   1624  1.17   chris 	/* return the pmap to the pool */
   1625  1.17   chris 	pool_put(&pmap_pmap_pool, pmap);
   1626   1.1    matt }
   1627   1.1    matt 
   1628   1.1    matt 
   1629   1.1    matt /*
   1630  1.15   chris  * void pmap_reference(struct pmap *pmap)
   1631   1.1    matt  *
   1632   1.1    matt  * Add a reference to the specified pmap.
   1633   1.1    matt  */
   1634   1.1    matt 
   1635   1.1    matt void
   1636   1.1    matt pmap_reference(pmap)
   1637  1.15   chris 	struct pmap *pmap;
   1638   1.1    matt {
   1639   1.1    matt 	if (pmap == NULL)
   1640   1.1    matt 		return;
   1641   1.1    matt 
   1642   1.1    matt 	simple_lock(&pmap->pm_lock);
   1643  1.16   chris 	pmap->pm_obj.uo_refs++;
   1644   1.1    matt 	simple_unlock(&pmap->pm_lock);
   1645   1.1    matt }
   1646   1.1    matt 
   1647   1.1    matt /*
   1648   1.1    matt  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   1649   1.1    matt  *
   1650   1.1    matt  * Return the start and end addresses of the kernel's virtual space.
   1651   1.1    matt  * These values are setup in pmap_bootstrap and are updated as pages
   1652   1.1    matt  * are allocated.
   1653   1.1    matt  */
   1654   1.1    matt 
   1655   1.1    matt void
   1656   1.1    matt pmap_virtual_space(start, end)
   1657   1.1    matt 	vaddr_t *start;
   1658   1.1    matt 	vaddr_t *end;
   1659   1.1    matt {
   1660   1.1    matt 	*start = virtual_start;
   1661   1.1    matt 	*end = virtual_end;
   1662   1.1    matt }
   1663   1.1    matt 
   1664   1.1    matt 
   1665   1.1    matt /*
   1666   1.1    matt  * Activate the address space for the specified process.  If the process
   1667   1.1    matt  * is the current process, load the new MMU context.
   1668   1.1    matt  */
   1669   1.1    matt void
   1670   1.1    matt pmap_activate(p)
   1671   1.1    matt 	struct proc *p;
   1672   1.1    matt {
   1673  1.15   chris 	struct pmap *pmap = p->p_vmspace->vm_map.pmap;
   1674   1.1    matt 	struct pcb *pcb = &p->p_addr->u_pcb;
   1675   1.1    matt 
   1676  1.15   chris 	(void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_pdir,
   1677   1.1    matt 	    (paddr_t *)&pcb->pcb_pagedir);
   1678   1.1    matt 
   1679   1.1    matt 	PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
   1680   1.1    matt 	    p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
   1681   1.1    matt 
   1682   1.1    matt 	if (p == curproc) {
   1683   1.1    matt 		PDEBUG(0, printf("pmap_activate: setting TTB\n"));
   1684   1.1    matt 		setttb((u_int)pcb->pcb_pagedir);
   1685   1.1    matt 	}
   1686   1.1    matt #if 0
   1687   1.1    matt 	pmap->pm_pdchanged = FALSE;
   1688   1.1    matt #endif
   1689   1.1    matt }
   1690   1.1    matt 
   1691   1.1    matt 
   1692   1.1    matt /*
   1693   1.1    matt  * Deactivate the address space of the specified process.
   1694   1.1    matt  */
   1695   1.1    matt void
   1696   1.1    matt pmap_deactivate(p)
   1697   1.1    matt 	struct proc *p;
   1698   1.1    matt {
   1699   1.1    matt }
   1700   1.1    matt 
   1701   1.1    matt 
   1702   1.1    matt /*
   1703   1.1    matt  * pmap_clean_page()
   1704   1.1    matt  *
   1705   1.1    matt  * This is a local function used to work out the best strategy to clean
   1706   1.1    matt  * a single page referenced by its entry in the PV table. It's used by
   1707   1.1    matt  * pmap_copy_page, pmap_zero page and maybe some others later on.
   1708   1.1    matt  *
   1709   1.1    matt  * Its policy is effectively:
   1710   1.1    matt  *  o If there are no mappings, we don't bother doing anything with the cache.
   1711   1.1    matt  *  o If there is one mapping, we clean just that page.
   1712   1.1    matt  *  o If there are multiple mappings, we clean the entire cache.
   1713   1.1    matt  *
   1714   1.1    matt  * So that some functions can be further optimised, it returns 0 if it didn't
   1715   1.1    matt  * clean the entire cache, or 1 if it did.
   1716   1.1    matt  *
   1717   1.1    matt  * XXX One bug in this routine is that if the pv_entry has a single page
   1718   1.1    matt  * mapped at 0x00000000 a whole cache clean will be performed rather than
   1719   1.1    matt  * just the 1 page. Since this should not occur in everyday use and if it does
   1720   1.1    matt  * it will just result in not the most efficient clean for the page.
   1721   1.1    matt  */
   1722   1.1    matt static int
   1723  1.17   chris pmap_clean_page(pv, is_src)
   1724   1.1    matt 	struct pv_entry *pv;
   1725  1.17   chris 	boolean_t is_src;
   1726   1.1    matt {
   1727  1.17   chris 	struct pmap *pmap;
   1728  1.17   chris 	struct pv_entry *npv;
   1729   1.1    matt 	int cache_needs_cleaning = 0;
   1730   1.1    matt 	vaddr_t page_to_clean = 0;
   1731   1.1    matt 
   1732  1.17   chris 	if (pv == NULL)
   1733  1.17   chris 		/* nothing mapped in so nothing to flush */
   1734  1.17   chris 		return (0);
   1735  1.17   chris 
   1736  1.17   chris 	/* Since we flush the cache each time we change curproc, we
   1737  1.17   chris 	 * only need to flush the page if it is in the current pmap.
   1738  1.17   chris 	 */
   1739  1.17   chris 	if (curproc)
   1740  1.17   chris 		pmap = curproc->p_vmspace->vm_map.pmap;
   1741  1.17   chris 	else
   1742  1.17   chris 		pmap = pmap_kernel();
   1743  1.17   chris 
   1744  1.17   chris 	for (npv = pv; npv; npv = npv->pv_next) {
   1745  1.17   chris 		if (npv->pv_pmap == pmap) {
   1746  1.17   chris 			/* The page is mapped non-cacheable in
   1747  1.17   chris 			 * this map.  No need to flush the cache.
   1748  1.17   chris 			 */
   1749  1.17   chris 			if (npv->pv_flags & PT_NC) {
   1750  1.17   chris #ifdef DIAGNOSTIC
   1751  1.17   chris 				if (cache_needs_cleaning)
   1752  1.17   chris 					panic("pmap_clean_page: "
   1753  1.17   chris 							"cache inconsistency");
   1754  1.17   chris #endif
   1755  1.17   chris 				break;
   1756  1.17   chris 			}
   1757  1.17   chris #if 0
   1758  1.17   chris 			/* This doesn't work, because pmap_protect
   1759  1.17   chris 			   doesn't flush changes on pages that it
   1760  1.17   chris 			   has write-protected.  */
   1761  1.21   chris 
   1762  1.17   chris 			/* If the page is not writeable and this
   1763  1.17   chris 			   is the source, then there is no need
   1764  1.17   chris 			   to flush it from the cache.  */
   1765  1.17   chris 			else if (is_src && ! (npv->pv_flags & PT_Wr))
   1766  1.17   chris 				continue;
   1767  1.17   chris #endif
   1768  1.17   chris 			if (cache_needs_cleaning){
   1769  1.17   chris 				page_to_clean = 0;
   1770  1.17   chris 				break;
   1771  1.17   chris 			}
   1772  1.17   chris 			else
   1773  1.17   chris 				page_to_clean = npv->pv_va;
   1774  1.17   chris 			cache_needs_cleaning = 1;
   1775  1.17   chris 		}
   1776   1.1    matt 	}
   1777   1.1    matt 
   1778   1.1    matt 	if (page_to_clean)
   1779   1.1    matt 		cpu_cache_purgeID_rng(page_to_clean, NBPG);
   1780   1.1    matt 	else if (cache_needs_cleaning) {
   1781   1.1    matt 		cpu_cache_purgeID();
   1782   1.1    matt 		return (1);
   1783   1.1    matt 	}
   1784   1.1    matt 	return (0);
   1785   1.1    matt }
   1786   1.1    matt 
   1787   1.1    matt /*
   1788   1.1    matt  * pmap_find_pv()
   1789   1.1    matt  *
   1790  1.17   chris  * This is a local function that finds a PV head for a given physical page.
   1791   1.1    matt  * This is a common op, and this function removes loads of ifdefs in the code.
   1792   1.1    matt  */
   1793  1.17   chris static __inline struct pv_head *
   1794  1.17   chris pmap_find_pvh(phys)
   1795   1.2    matt 	paddr_t phys;
   1796   1.1    matt {
   1797   1.1    matt 	int bank, off;
   1798  1.17   chris 	struct pv_head *pvh;
   1799   1.1    matt 
   1800   1.1    matt 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1)
   1801   1.1    matt 		panic("pmap_find_pv: not a real page, phys=%lx\n", phys);
   1802  1.17   chris 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   1803  1.17   chris 	return (pvh);
   1804   1.1    matt }
   1805   1.1    matt 
   1806   1.1    matt /*
   1807   1.1    matt  * pmap_zero_page()
   1808   1.1    matt  *
   1809   1.1    matt  * Zero a given physical page by mapping it at a page hook point.
   1810   1.1    matt  * In doing the zero page op, the page we zero is mapped cachable, as with
   1811   1.1    matt  * StrongARM accesses to non-cached pages are non-burst making writing
   1812   1.1    matt  * _any_ bulk data very slow.
   1813   1.1    matt  */
   1814   1.1    matt void
   1815   1.1    matt pmap_zero_page(phys)
   1816   1.2    matt 	paddr_t phys;
   1817   1.1    matt {
   1818  1.17   chris 	struct pv_head *pvh;
   1819   1.1    matt 
   1820   1.1    matt 	/* Get an entry for this page, and clean it it. */
   1821  1.17   chris 	pvh = pmap_find_pvh(phys);
   1822  1.17   chris 	simple_lock(&pvh->pvh_lock);
   1823  1.17   chris 	pmap_clean_page(pvh->pvh_list, FALSE);
   1824  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   1825  1.17   chris 
   1826   1.1    matt 	/*
   1827   1.1    matt 	 * Hook in the page, zero it, and purge the cache for that
   1828   1.1    matt 	 * zeroed page. Invalidate the TLB as needed.
   1829   1.1    matt 	 */
   1830   1.1    matt 	*page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
   1831   1.1    matt 	cpu_tlb_flushD_SE(page_hook0.va);
   1832   1.1    matt 	bzero_page(page_hook0.va);
   1833   1.1    matt 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1834   1.1    matt }
   1835   1.1    matt 
   1836  1.17   chris /* pmap_pageidlezero()
   1837  1.17   chris  *
   1838  1.17   chris  * The same as above, except that we assume that the page is not
   1839  1.17   chris  * mapped.  This means we never have to flush the cache first.  Called
   1840  1.17   chris  * from the idle loop.
   1841  1.17   chris  */
   1842  1.17   chris boolean_t
   1843  1.17   chris pmap_pageidlezero(phys)
   1844  1.17   chris     paddr_t phys;
   1845  1.17   chris {
   1846  1.17   chris 	int i, *ptr;
   1847  1.17   chris 	boolean_t rv = TRUE;
   1848  1.17   chris 
   1849  1.17   chris #ifdef DIAGNOSTIC
   1850  1.17   chris 	struct pv_head *pvh;
   1851  1.17   chris 
   1852  1.17   chris 	pvh = pmap_find_pvh(phys);
   1853  1.17   chris 	if (pvh->pvh_list != NULL)
   1854  1.17   chris 		panic("pmap_pageidlezero: zeroing mapped page\n");
   1855  1.17   chris #endif
   1856  1.17   chris 
   1857  1.17   chris 	/*
   1858  1.17   chris 	 * Hook in the page, zero it, and purge the cache for that
   1859  1.17   chris 	 * zeroed page. Invalidate the TLB as needed.
   1860  1.17   chris 	 */
   1861  1.17   chris 	*page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
   1862  1.17   chris 	cpu_tlb_flushD_SE(page_hook0.va);
   1863  1.17   chris 
   1864  1.17   chris 	for (i = 0, ptr = (int *)page_hook0.va;
   1865  1.17   chris 			i < (NBPG / sizeof(int)); i++) {
   1866  1.17   chris 		if (sched_whichqs != 0) {
   1867  1.17   chris 			/*
   1868  1.17   chris 			 * A process has become ready.  Abort now,
   1869  1.17   chris 			 * so we don't keep it waiting while we
   1870  1.17   chris 			 * do slow memory access to finish this
   1871  1.17   chris 			 * page.
   1872  1.17   chris 			 */
   1873  1.17   chris 			rv = FALSE;
   1874  1.17   chris 			break;
   1875  1.17   chris 		}
   1876  1.17   chris 		*ptr++ = 0;
   1877  1.17   chris 	}
   1878  1.17   chris 
   1879  1.17   chris 	if (rv)
   1880  1.17   chris 		/*
   1881  1.17   chris 		 * if we aborted we'll rezero this page again later so don't
   1882  1.17   chris 		 * purge it unless we finished it
   1883  1.17   chris 		 */
   1884  1.17   chris 		cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1885  1.17   chris 	return (rv);
   1886  1.17   chris }
   1887  1.17   chris 
   1888   1.1    matt /*
   1889   1.1    matt  * pmap_copy_page()
   1890   1.1    matt  *
   1891   1.1    matt  * Copy one physical page into another, by mapping the pages into
   1892   1.1    matt  * hook points. The same comment regarding cachability as in
   1893   1.1    matt  * pmap_zero_page also applies here.
   1894   1.1    matt  */
   1895   1.1    matt void
   1896   1.1    matt pmap_copy_page(src, dest)
   1897   1.2    matt 	paddr_t src;
   1898   1.2    matt 	paddr_t dest;
   1899   1.1    matt {
   1900  1.17   chris 	struct pv_head *src_pvh, *dest_pvh;
   1901  1.20   chris 	boolean_t cleanedcache;
   1902   1.1    matt 
   1903   1.1    matt 	/* Get PV entries for the pages, and clean them if needed. */
   1904  1.17   chris 	src_pvh = pmap_find_pvh(src);
   1905  1.20   chris 
   1906  1.17   chris 	simple_lock(&src_pvh->pvh_lock);
   1907  1.20   chris 	cleanedcache = pmap_clean_page(src_pvh->pvh_list, TRUE);
   1908  1.17   chris 	simple_unlock(&src_pvh->pvh_lock);
   1909   1.1    matt 
   1910  1.20   chris 	if (cleanedcache == 0) {
   1911  1.20   chris 		dest_pvh = pmap_find_pvh(dest);
   1912  1.20   chris 		simple_lock(&dest_pvh->pvh_lock);
   1913  1.20   chris 		pmap_clean_page(dest_pvh->pvh_list, FALSE);
   1914  1.20   chris 		simple_unlock(&dest_pvh->pvh_lock);
   1915  1.20   chris 	}
   1916   1.1    matt 	/*
   1917   1.1    matt 	 * Map the pages into the page hook points, copy them, and purge
   1918   1.1    matt 	 * the cache for the appropriate page. Invalidate the TLB
   1919   1.1    matt 	 * as required.
   1920   1.1    matt 	 */
   1921   1.1    matt 	*page_hook0.pte = L2_PTE(src & PG_FRAME, AP_KRW);
   1922   1.1    matt 	*page_hook1.pte = L2_PTE(dest & PG_FRAME, AP_KRW);
   1923   1.1    matt 	cpu_tlb_flushD_SE(page_hook0.va);
   1924   1.1    matt 	cpu_tlb_flushD_SE(page_hook1.va);
   1925   1.1    matt 	bcopy_page(page_hook0.va, page_hook1.va);
   1926   1.1    matt 	cpu_cache_purgeD_rng(page_hook0.va, NBPG);
   1927   1.1    matt 	cpu_cache_purgeD_rng(page_hook1.va, NBPG);
   1928   1.1    matt }
   1929   1.1    matt 
   1930   1.1    matt /*
   1931   1.2    matt  * int pmap_next_phys_page(paddr_t *addr)
   1932   1.1    matt  *
   1933   1.1    matt  * Allocate another physical page returning true or false depending
   1934   1.1    matt  * on whether a page could be allocated.
   1935   1.1    matt  */
   1936   1.1    matt 
   1937   1.2    matt paddr_t
   1938   1.1    matt pmap_next_phys_page(addr)
   1939   1.2    matt 	paddr_t addr;
   1940   1.1    matt 
   1941   1.1    matt {
   1942   1.1    matt 	int loop;
   1943   1.1    matt 
   1944   1.1    matt 	if (addr < bootconfig.dram[0].address)
   1945   1.1    matt 		return(bootconfig.dram[0].address);
   1946   1.1    matt 
   1947   1.1    matt 	loop = 0;
   1948   1.1    matt 
   1949   1.1    matt 	while (bootconfig.dram[loop].address != 0
   1950   1.1    matt 	    && addr > (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG))
   1951   1.1    matt 		++loop;
   1952   1.1    matt 
   1953   1.1    matt 	if (bootconfig.dram[loop].address == 0)
   1954   1.1    matt 		return(0);
   1955   1.1    matt 
   1956   1.1    matt 	addr += NBPG;
   1957   1.1    matt 
   1958   1.1    matt 	if (addr >= (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG)) {
   1959   1.1    matt 		if (bootconfig.dram[loop + 1].address == 0)
   1960   1.1    matt 			return(0);
   1961   1.1    matt 		addr = bootconfig.dram[loop + 1].address;
   1962   1.1    matt 	}
   1963   1.1    matt 
   1964   1.1    matt 	return(addr);
   1965   1.1    matt }
   1966   1.1    matt 
   1967   1.1    matt #if 0
   1968   1.1    matt void
   1969   1.1    matt pmap_pte_addref(pmap, va)
   1970  1.15   chris 	struct pmap *pmap;
   1971   1.1    matt 	vaddr_t va;
   1972   1.1    matt {
   1973   1.1    matt 	pd_entry_t *pde;
   1974   1.2    matt 	paddr_t pa;
   1975   1.1    matt 	struct vm_page *m;
   1976   1.1    matt 
   1977   1.1    matt 	if (pmap == pmap_kernel())
   1978   1.1    matt 		return;
   1979   1.1    matt 
   1980   1.1    matt 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   1981   1.1    matt 	pa = pmap_pte_pa(pde);
   1982   1.1    matt 	m = PHYS_TO_VM_PAGE(pa);
   1983   1.1    matt 	++m->wire_count;
   1984   1.1    matt #ifdef MYCROFT_HACK
   1985   1.1    matt 	printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   1986   1.1    matt 	    pmap, va, pde, pa, m, m->wire_count);
   1987   1.1    matt #endif
   1988   1.1    matt }
   1989   1.1    matt 
   1990   1.1    matt void
   1991   1.1    matt pmap_pte_delref(pmap, va)
   1992  1.15   chris 	struct pmap *pmap;
   1993   1.1    matt 	vaddr_t va;
   1994   1.1    matt {
   1995   1.1    matt 	pd_entry_t *pde;
   1996   1.2    matt 	paddr_t pa;
   1997   1.1    matt 	struct vm_page *m;
   1998   1.1    matt 
   1999   1.1    matt 	if (pmap == pmap_kernel())
   2000   1.1    matt 		return;
   2001   1.1    matt 
   2002   1.1    matt 	pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
   2003   1.1    matt 	pa = pmap_pte_pa(pde);
   2004   1.1    matt 	m = PHYS_TO_VM_PAGE(pa);
   2005   1.1    matt 	--m->wire_count;
   2006   1.1    matt #ifdef MYCROFT_HACK
   2007   1.1    matt 	printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
   2008   1.1    matt 	    pmap, va, pde, pa, m, m->wire_count);
   2009   1.1    matt #endif
   2010   1.1    matt 	if (m->wire_count == 0) {
   2011   1.1    matt #ifdef MYCROFT_HACK
   2012   1.1    matt 		printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
   2013   1.1    matt 		    pmap, va, pde, pa, m);
   2014   1.1    matt #endif
   2015   1.1    matt 		pmap_unmap_in_l1(pmap, va);
   2016   1.1    matt 		uvm_pagefree(m);
   2017   1.1    matt 		--pmap->pm_stats.resident_count;
   2018   1.1    matt 	}
   2019   1.1    matt }
   2020   1.1    matt #else
   2021   1.1    matt #define	pmap_pte_addref(pmap, va)
   2022   1.1    matt #define	pmap_pte_delref(pmap, va)
   2023   1.1    matt #endif
   2024   1.1    matt 
   2025   1.1    matt /*
   2026   1.1    matt  * Since we have a virtually indexed cache, we may need to inhibit caching if
   2027   1.1    matt  * there is more than one mapping and at least one of them is writable.
   2028   1.1    matt  * Since we purge the cache on every context switch, we only need to check for
   2029   1.1    matt  * other mappings within the same pmap, or kernel_pmap.
   2030   1.1    matt  * This function is also called when a page is unmapped, to possibly reenable
   2031   1.1    matt  * caching on any remaining mappings.
   2032  1.11   chris  *
   2033  1.11   chris  * Note that the pmap must have it's ptes mapped in, and passed with ptes.
   2034   1.1    matt  */
   2035   1.1    matt void
   2036  1.17   chris pmap_vac_me_harder(struct pmap *pmap, struct pv_head *pvh, pt_entry_t *ptes,
   2037  1.12   chris 	boolean_t clear_cache)
   2038   1.1    matt {
   2039  1.17   chris 	struct pv_entry *pv, *npv;
   2040   1.1    matt 	pt_entry_t *pte;
   2041   1.1    matt 	int entries = 0;
   2042   1.1    matt 	int writeable = 0;
   2043  1.12   chris 	int cacheable_entries = 0;
   2044   1.1    matt 
   2045  1.17   chris 	pv = pvh->pvh_list;
   2046  1.11   chris 	KASSERT(ptes != NULL);
   2047   1.1    matt 
   2048   1.1    matt 	/*
   2049   1.1    matt 	 * Count mappings and writable mappings in this pmap.
   2050   1.1    matt 	 * Keep a pointer to the first one.
   2051   1.1    matt 	 */
   2052   1.1    matt 	for (npv = pv; npv; npv = npv->pv_next) {
   2053   1.1    matt 		/* Count mappings in the same pmap */
   2054   1.1    matt 		if (pmap == npv->pv_pmap) {
   2055   1.1    matt 			if (entries++ == 0)
   2056   1.1    matt 				pv = npv;
   2057  1.12   chris 			/* Cacheable mappings */
   2058  1.12   chris 			if ((npv->pv_flags & PT_NC) == 0)
   2059  1.12   chris 				cacheable_entries++;
   2060   1.1    matt 			/* Writeable mappings */
   2061   1.1    matt 			if (npv->pv_flags & PT_Wr)
   2062   1.1    matt 				++writeable;
   2063   1.1    matt 		}
   2064   1.1    matt 	}
   2065   1.1    matt 
   2066  1.12   chris 	PDEBUG(3,printf("pmap_vac_me_harder: pmap %p Entries %d, "
   2067  1.12   chris 		"writeable %d cacheable %d %s\n", pmap, entries, writeable,
   2068  1.12   chris 	    	cacheable_entries, clear_cache ? "clean" : "no clean"));
   2069  1.12   chris 
   2070   1.1    matt 	/*
   2071   1.1    matt 	 * Enable or disable caching as necessary.
   2072   1.1    matt 	 * We do a quick check of the first PTE to avoid walking the list if
   2073   1.1    matt 	 * we're already in the right state.
   2074   1.1    matt 	 */
   2075   1.1    matt 	if (entries > 1 && writeable) {
   2076  1.12   chris 		if (cacheable_entries == 0)
   2077  1.12   chris 		    return;
   2078  1.12   chris 		if (pv->pv_flags & PT_NC) {
   2079  1.12   chris #ifdef DIAGNOSTIC
   2080  1.12   chris     			/* We have cacheable entries, but the first one
   2081  1.12   chris  			isn't among them. Something is wrong.  */
   2082  1.12   chris     			if (cacheable_entries)
   2083  1.12   chris 				panic("pmap_vac_me_harder: "
   2084  1.12   chris 	    				"cacheable inconsistent");
   2085  1.12   chris #endif
   2086   1.1    matt 			return;
   2087  1.11   chris 		}
   2088  1.12   chris 		pte =  &ptes[arm_byte_to_page(pv->pv_va)];
   2089  1.11   chris 		*pte &= ~(PT_C | PT_B);
   2090  1.12   chris 		pv->pv_flags |= PT_NC;
   2091  1.12   chris 		if (clear_cache && cacheable_entries < 4) {
   2092  1.12   chris 			cpu_cache_purgeID_rng(pv->pv_va, NBPG);
   2093  1.12   chris 			cpu_tlb_flushID_SE(pv->pv_va);
   2094  1.12   chris 		}
   2095   1.1    matt 		for (npv = pv->pv_next; npv; npv = npv->pv_next) {
   2096  1.12   chris 			if (pmap == npv->pv_pmap &&
   2097  1.12   chris 			    (npv->pv_flags & PT_NC) == 0) {
   2098  1.12   chris 				ptes[arm_byte_to_page(npv->pv_va)] &=
   2099  1.11   chris 				    ~(PT_C | PT_B);
   2100  1.12   chris  				npv->pv_flags |= PT_NC;
   2101  1.12   chris 				if (clear_cache && cacheable_entries < 4) {
   2102  1.12   chris 					cpu_cache_purgeID_rng(npv->pv_va,
   2103  1.12   chris 					    NBPG);
   2104  1.12   chris 					cpu_tlb_flushID_SE(npv->pv_va);
   2105  1.12   chris 				}
   2106   1.1    matt 			}
   2107   1.1    matt 		}
   2108  1.12   chris 		if (clear_cache && cacheable_entries >= 4) {
   2109  1.12   chris 			cpu_cache_purgeID();
   2110  1.12   chris 			cpu_tlb_flushID();
   2111  1.12   chris 		}
   2112   1.1    matt 	} else if (entries > 0) {
   2113  1.12   chris 		if ((pv->pv_flags & PT_NC) == 0)
   2114  1.12   chris 			return;
   2115  1.11   chris 		pte = &ptes[arm_byte_to_page(pv->pv_va)];
   2116  1.11   chris 		*pte |= (PT_C | PT_B);
   2117  1.12   chris 		pv->pv_flags &= ~PT_NC;
   2118   1.1    matt 		for (npv = pv->pv_next; npv; npv = npv->pv_next) {
   2119  1.12   chris 			if (pmap == npv->pv_pmap &&
   2120  1.12   chris 				(npv->pv_flags & PT_NC)) {
   2121  1.11   chris 				ptes[arm_byte_to_page(npv->pv_va)] |=
   2122  1.12   chris 				    (PT_C | PT_B);
   2123  1.12   chris 				npv->pv_flags &= ~PT_NC;
   2124   1.1    matt 			}
   2125   1.1    matt 		}
   2126   1.1    matt 	}
   2127   1.1    matt }
   2128   1.1    matt 
   2129   1.1    matt /*
   2130   1.1    matt  * pmap_remove()
   2131   1.1    matt  *
   2132   1.1    matt  * pmap_remove is responsible for nuking a number of mappings for a range
   2133   1.1    matt  * of virtual address space in the current pmap. To do this efficiently
   2134   1.1    matt  * is interesting, because in a number of cases a wide virtual address
   2135   1.1    matt  * range may be supplied that contains few actual mappings. So, the
   2136   1.1    matt  * optimisations are:
   2137   1.1    matt  *  1. Try and skip over hunks of address space for which an L1 entry
   2138   1.1    matt  *     does not exist.
   2139   1.1    matt  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   2140   1.1    matt  *     maybe do just a partial cache clean. This path of execution is
   2141   1.1    matt  *     complicated by the fact that the cache must be flushed _before_
   2142   1.1    matt  *     the PTE is nuked, being a VAC :-)
   2143   1.1    matt  *  3. Maybe later fast-case a single page, but I don't think this is
   2144   1.1    matt  *     going to make _that_ much difference overall.
   2145   1.1    matt  */
   2146   1.1    matt 
   2147   1.1    matt #define PMAP_REMOVE_CLEAN_LIST_SIZE	3
   2148   1.1    matt 
   2149   1.1    matt void
   2150   1.1    matt pmap_remove(pmap, sva, eva)
   2151  1.15   chris 	struct pmap *pmap;
   2152   1.1    matt 	vaddr_t sva;
   2153   1.1    matt 	vaddr_t eva;
   2154   1.1    matt {
   2155   1.1    matt 	int cleanlist_idx = 0;
   2156   1.1    matt 	struct pagelist {
   2157   1.1    matt 		vaddr_t va;
   2158   1.1    matt 		pt_entry_t *pte;
   2159   1.1    matt 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   2160  1.11   chris 	pt_entry_t *pte = 0, *ptes;
   2161   1.2    matt 	paddr_t pa;
   2162   1.1    matt 	int pmap_active;
   2163  1.17   chris 	struct pv_head *pvh;
   2164   1.1    matt 
   2165   1.1    matt 	/* Exit quick if there is no pmap */
   2166   1.1    matt 	if (!pmap)
   2167   1.1    matt 		return;
   2168   1.1    matt 
   2169   1.1    matt 	PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n", pmap, sva, eva));
   2170   1.1    matt 
   2171   1.1    matt 	sva &= PG_FRAME;
   2172   1.1    matt 	eva &= PG_FRAME;
   2173   1.1    matt 
   2174  1.17   chris 	/*
   2175  1.17   chris 	 * we lock in the pmap => pv_head direction
   2176  1.17   chris 	 */
   2177  1.17   chris 	PMAP_MAP_TO_HEAD_LOCK();
   2178  1.17   chris 
   2179  1.11   chris 	ptes = pmap_map_ptes(pmap);
   2180   1.1    matt 	/* Get a page table pointer */
   2181   1.1    matt 	while (sva < eva) {
   2182  1.11   chris 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   2183   1.1    matt 			break;
   2184   1.1    matt 		sva = (sva & PD_MASK) + NBPD;
   2185   1.1    matt 	}
   2186  1.11   chris 
   2187  1.11   chris 	pte = &ptes[arm_byte_to_page(sva)];
   2188   1.1    matt 	/* Note if the pmap is active thus require cache and tlb cleans */
   2189   1.1    matt 	if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
   2190  1.15   chris 	    || (pmap == pmap_kernel()))
   2191   1.1    matt 		pmap_active = 1;
   2192   1.1    matt 	else
   2193   1.1    matt 		pmap_active = 0;
   2194   1.1    matt 
   2195   1.1    matt 	/* Now loop along */
   2196   1.1    matt 	while (sva < eva) {
   2197   1.1    matt 		/* Check if we can move to the next PDE (l1 chunk) */
   2198   1.1    matt 		if (!(sva & PT_MASK))
   2199   1.1    matt 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   2200   1.1    matt 				sva += NBPD;
   2201   1.1    matt 				pte += arm_byte_to_page(NBPD);
   2202   1.1    matt 				continue;
   2203   1.1    matt 			}
   2204   1.1    matt 
   2205   1.1    matt 		/* We've found a valid PTE, so this page of PTEs has to go. */
   2206   1.1    matt 		if (pmap_pte_v(pte)) {
   2207   1.1    matt 			int bank, off;
   2208   1.1    matt 
   2209   1.1    matt 			/* Update statistics */
   2210   1.1    matt 			--pmap->pm_stats.resident_count;
   2211   1.1    matt 
   2212   1.1    matt 			/*
   2213   1.1    matt 			 * Add this page to our cache remove list, if we can.
   2214   1.1    matt 			 * If, however the cache remove list is totally full,
   2215   1.1    matt 			 * then do a complete cache invalidation taking note
   2216   1.1    matt 			 * to backtrack the PTE table beforehand, and ignore
   2217   1.1    matt 			 * the lists in future because there's no longer any
   2218   1.1    matt 			 * point in bothering with them (we've paid the
   2219   1.1    matt 			 * penalty, so will carry on unhindered). Otherwise,
   2220   1.1    matt 			 * when we fall out, we just clean the list.
   2221   1.1    matt 			 */
   2222   1.1    matt 			PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
   2223   1.1    matt 			pa = pmap_pte_pa(pte);
   2224   1.1    matt 
   2225   1.1    matt 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2226   1.1    matt 				/* Add to the clean list. */
   2227   1.1    matt 				cleanlist[cleanlist_idx].pte = pte;
   2228   1.1    matt 				cleanlist[cleanlist_idx].va = sva;
   2229   1.1    matt 				cleanlist_idx++;
   2230   1.1    matt 			} else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2231   1.1    matt 				int cnt;
   2232   1.1    matt 
   2233   1.1    matt 				/* Nuke everything if needed. */
   2234   1.1    matt 				if (pmap_active) {
   2235   1.1    matt 					cpu_cache_purgeID();
   2236   1.1    matt 					cpu_tlb_flushID();
   2237   1.1    matt 				}
   2238   1.1    matt 
   2239   1.1    matt 				/*
   2240   1.1    matt 				 * Roll back the previous PTE list,
   2241   1.1    matt 				 * and zero out the current PTE.
   2242   1.1    matt 				 */
   2243   1.1    matt 				for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   2244   1.1    matt 					*cleanlist[cnt].pte = 0;
   2245   1.1    matt 					pmap_pte_delref(pmap, cleanlist[cnt].va);
   2246   1.1    matt 				}
   2247   1.1    matt 				*pte = 0;
   2248   1.1    matt 				pmap_pte_delref(pmap, sva);
   2249   1.1    matt 				cleanlist_idx++;
   2250   1.1    matt 			} else {
   2251   1.1    matt 				/*
   2252   1.1    matt 				 * We've already nuked the cache and
   2253   1.1    matt 				 * TLB, so just carry on regardless,
   2254   1.1    matt 				 * and we won't need to do it again
   2255   1.1    matt 				 */
   2256   1.1    matt 				*pte = 0;
   2257   1.1    matt 				pmap_pte_delref(pmap, sva);
   2258   1.1    matt 			}
   2259   1.1    matt 
   2260   1.1    matt 			/*
   2261   1.1    matt 			 * Update flags. In a number of circumstances,
   2262   1.1    matt 			 * we could cluster a lot of these and do a
   2263   1.1    matt 			 * number of sequential pages in one go.
   2264   1.1    matt 			 */
   2265   1.1    matt 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2266  1.17   chris 				struct pv_entry *pve;
   2267  1.17   chris 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2268  1.17   chris 				simple_lock(&pvh->pvh_lock);
   2269  1.17   chris 				pve = pmap_remove_pv(pvh, pmap, sva);
   2270  1.17   chris 				pmap_free_pv(pmap, pve);
   2271  1.17   chris 				pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
   2272  1.17   chris 				simple_unlock(&pvh->pvh_lock);
   2273   1.1    matt 			}
   2274   1.1    matt 		}
   2275   1.1    matt 		sva += NBPG;
   2276   1.1    matt 		pte++;
   2277   1.1    matt 	}
   2278   1.1    matt 
   2279  1.11   chris 	pmap_unmap_ptes(pmap);
   2280   1.1    matt 	/*
   2281   1.1    matt 	 * Now, if we've fallen through down to here, chances are that there
   2282   1.1    matt 	 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
   2283   1.1    matt 	 */
   2284   1.1    matt 	if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2285   1.1    matt 		u_int cnt;
   2286   1.1    matt 
   2287   1.1    matt 		for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   2288   1.1    matt 			if (pmap_active) {
   2289   1.1    matt 				cpu_cache_purgeID_rng(cleanlist[cnt].va, NBPG);
   2290   1.1    matt 				*cleanlist[cnt].pte = 0;
   2291   1.1    matt 				cpu_tlb_flushID_SE(cleanlist[cnt].va);
   2292   1.1    matt 			} else
   2293   1.1    matt 				*cleanlist[cnt].pte = 0;
   2294   1.1    matt 			pmap_pte_delref(pmap, cleanlist[cnt].va);
   2295   1.1    matt 		}
   2296   1.1    matt 	}
   2297  1.17   chris 	PMAP_MAP_TO_HEAD_UNLOCK();
   2298   1.1    matt }
   2299   1.1    matt 
   2300   1.1    matt /*
   2301   1.1    matt  * Routine:	pmap_remove_all
   2302   1.1    matt  * Function:
   2303   1.1    matt  *		Removes this physical page from
   2304   1.1    matt  *		all physical maps in which it resides.
   2305   1.1    matt  *		Reflects back modify bits to the pager.
   2306   1.1    matt  */
   2307   1.1    matt 
   2308   1.1    matt void
   2309   1.1    matt pmap_remove_all(pa)
   2310   1.2    matt 	paddr_t pa;
   2311   1.1    matt {
   2312  1.17   chris 	struct pv_entry *pv, *npv;
   2313  1.17   chris 	struct pv_head *pvh;
   2314  1.15   chris 	struct pmap *pmap;
   2315  1.11   chris 	pt_entry_t *pte, *ptes;
   2316   1.1    matt 
   2317   1.1    matt 	PDEBUG(0, printf("pmap_remove_all: pa=%lx ", pa));
   2318   1.1    matt 
   2319  1.17   chris 	/* set pv_head => pmap locking */
   2320  1.17   chris 	PMAP_HEAD_TO_MAP_LOCK();
   2321   1.1    matt 
   2322  1.17   chris 	pvh = pmap_find_pvh(pa);
   2323  1.17   chris 	simple_lock(&pvh->pvh_lock);
   2324  1.17   chris 
   2325  1.17   chris 	pv = pvh->pvh_list;
   2326  1.17   chris 	if (pv == NULL)
   2327  1.17   chris 	{
   2328  1.17   chris 	    PDEBUG(0, printf("free page\n"));
   2329  1.17   chris 	    simple_unlock(&pvh->pvh_lock);
   2330  1.17   chris 	    PMAP_HEAD_TO_MAP_UNLOCK();
   2331  1.17   chris 	    return;
   2332   1.1    matt 	}
   2333  1.17   chris 	pmap_clean_page(pv, FALSE);
   2334   1.1    matt 
   2335   1.1    matt 	while (pv) {
   2336   1.1    matt 		pmap = pv->pv_pmap;
   2337  1.11   chris 		ptes = pmap_map_ptes(pmap);
   2338  1.11   chris 		pte = &ptes[arm_byte_to_page(pv->pv_va)];
   2339   1.1    matt 
   2340   1.1    matt 		PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
   2341   1.1    matt 		    pv->pv_va, pv->pv_flags));
   2342   1.1    matt #ifdef DEBUG
   2343  1.11   chris 		if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)
   2344  1.11   chris 			    || pmap_pte_pa(pte) != pa)
   2345   1.1    matt 			panic("pmap_remove_all: bad mapping");
   2346   1.1    matt #endif	/* DEBUG */
   2347   1.1    matt 
   2348   1.1    matt 		/*
   2349   1.1    matt 		 * Update statistics
   2350   1.1    matt 		 */
   2351   1.1    matt 		--pmap->pm_stats.resident_count;
   2352   1.1    matt 
   2353   1.1    matt 		/* Wired bit */
   2354   1.1    matt 		if (pv->pv_flags & PT_W)
   2355   1.1    matt 			--pmap->pm_stats.wired_count;
   2356   1.1    matt 
   2357   1.1    matt 		/*
   2358   1.1    matt 		 * Invalidate the PTEs.
   2359   1.1    matt 		 * XXX: should cluster them up and invalidate as many
   2360   1.1    matt 		 * as possible at once.
   2361   1.1    matt 		 */
   2362   1.1    matt 
   2363   1.1    matt #ifdef needednotdone
   2364   1.1    matt reduce wiring count on page table pages as references drop
   2365   1.1    matt #endif
   2366   1.1    matt 
   2367   1.1    matt 		*pte = 0;
   2368   1.1    matt 		pmap_pte_delref(pmap, pv->pv_va);
   2369   1.1    matt 
   2370   1.1    matt 		npv = pv->pv_next;
   2371  1.17   chris 		pmap_free_pv(pmap, pv);
   2372   1.1    matt 		pv = npv;
   2373  1.11   chris 		pmap_unmap_ptes(pmap);
   2374   1.1    matt 	}
   2375  1.17   chris 	pvh->pvh_list = NULL;
   2376  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   2377  1.17   chris 	PMAP_HEAD_TO_MAP_UNLOCK();
   2378   1.1    matt 
   2379   1.1    matt 	PDEBUG(0, printf("done\n"));
   2380   1.1    matt 	cpu_tlb_flushID();
   2381   1.1    matt }
   2382   1.1    matt 
   2383   1.1    matt 
   2384   1.1    matt /*
   2385   1.1    matt  * Set the physical protection on the specified range of this map as requested.
   2386   1.1    matt  */
   2387   1.1    matt 
   2388   1.1    matt void
   2389   1.1    matt pmap_protect(pmap, sva, eva, prot)
   2390  1.15   chris 	struct pmap *pmap;
   2391   1.1    matt 	vaddr_t sva;
   2392   1.1    matt 	vaddr_t eva;
   2393   1.1    matt 	vm_prot_t prot;
   2394   1.1    matt {
   2395  1.11   chris 	pt_entry_t *pte = NULL, *ptes;
   2396   1.1    matt 	int armprot;
   2397   1.1    matt 	int flush = 0;
   2398   1.2    matt 	paddr_t pa;
   2399   1.1    matt 	int bank, off;
   2400  1.17   chris 	struct pv_head *pvh;
   2401   1.1    matt 
   2402   1.1    matt 	PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
   2403   1.1    matt 	    pmap, sva, eva, prot));
   2404   1.1    matt 
   2405   1.1    matt 	if (~prot & VM_PROT_READ) {
   2406   1.1    matt 		/* Just remove the mappings. */
   2407   1.1    matt 		pmap_remove(pmap, sva, eva);
   2408   1.1    matt 		return;
   2409   1.1    matt 	}
   2410   1.1    matt 	if (prot & VM_PROT_WRITE) {
   2411   1.1    matt 		/*
   2412   1.1    matt 		 * If this is a read->write transition, just ignore it and let
   2413   1.1    matt 		 * uvm_fault() take care of it later.
   2414   1.1    matt 		 */
   2415   1.1    matt 		return;
   2416   1.1    matt 	}
   2417   1.1    matt 
   2418   1.1    matt 	sva &= PG_FRAME;
   2419   1.1    matt 	eva &= PG_FRAME;
   2420   1.1    matt 
   2421  1.17   chris 	/* Need to lock map->head */
   2422  1.17   chris 	PMAP_MAP_TO_HEAD_LOCK();
   2423  1.17   chris 
   2424  1.11   chris 	ptes = pmap_map_ptes(pmap);
   2425   1.1    matt 	/*
   2426   1.1    matt 	 * We need to acquire a pointer to a page table page before entering
   2427   1.1    matt 	 * the following loop.
   2428   1.1    matt 	 */
   2429   1.1    matt 	while (sva < eva) {
   2430  1.11   chris 		if (pmap_pde_v(pmap_pde(pmap, sva)))
   2431   1.1    matt 			break;
   2432   1.1    matt 		sva = (sva & PD_MASK) + NBPD;
   2433   1.1    matt 	}
   2434  1.11   chris 
   2435  1.11   chris 	pte = &ptes[arm_byte_to_page(sva)];
   2436  1.17   chris 
   2437   1.1    matt 	while (sva < eva) {
   2438   1.1    matt 		/* only check once in a while */
   2439   1.1    matt 		if ((sva & PT_MASK) == 0) {
   2440   1.1    matt 			if (!pmap_pde_v(pmap_pde(pmap, sva))) {
   2441   1.1    matt 				/* We can race ahead here, to the next pde. */
   2442   1.1    matt 				sva += NBPD;
   2443   1.1    matt 				pte += arm_byte_to_page(NBPD);
   2444   1.1    matt 				continue;
   2445   1.1    matt 			}
   2446   1.1    matt 		}
   2447   1.1    matt 
   2448   1.1    matt 		if (!pmap_pte_v(pte))
   2449   1.1    matt 			goto next;
   2450   1.1    matt 
   2451   1.1    matt 		flush = 1;
   2452   1.1    matt 
   2453   1.1    matt 		armprot = 0;
   2454   1.1    matt 		if (sva < VM_MAXUSER_ADDRESS)
   2455   1.1    matt 			armprot |= PT_AP(AP_U);
   2456   1.1    matt 		else if (sva < VM_MAX_ADDRESS)
   2457   1.1    matt 			armprot |= PT_AP(AP_W);  /* XXX Ekk what is this ? */
   2458   1.1    matt 		*pte = (*pte & 0xfffff00f) | armprot;
   2459   1.1    matt 
   2460   1.1    matt 		pa = pmap_pte_pa(pte);
   2461   1.1    matt 
   2462   1.1    matt 		/* Get the physical page index */
   2463   1.1    matt 
   2464   1.1    matt 		/* Clear write flag */
   2465   1.1    matt 		if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2466  1.17   chris 			pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2467  1.17   chris 			simple_lock(&pvh->pvh_lock);
   2468  1.17   chris 			(void) pmap_modify_pv(pmap, sva, pvh, PT_Wr, 0);
   2469  1.17   chris 			pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
   2470  1.17   chris 			simple_unlock(&pvh->pvh_lock);
   2471   1.1    matt 		}
   2472   1.1    matt 
   2473   1.1    matt next:
   2474   1.1    matt 		sva += NBPG;
   2475   1.1    matt 		pte++;
   2476   1.1    matt 	}
   2477  1.11   chris 	pmap_unmap_ptes(pmap);
   2478  1.17   chris 	PMAP_MAP_TO_HEAD_UNLOCK();
   2479   1.1    matt 	if (flush)
   2480   1.1    matt 		cpu_tlb_flushID();
   2481   1.1    matt }
   2482   1.1    matt 
   2483   1.1    matt /*
   2484  1.15   chris  * void pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2485   1.1    matt  * int flags)
   2486   1.1    matt  *
   2487   1.1    matt  *      Insert the given physical page (p) at
   2488   1.1    matt  *      the specified virtual address (v) in the
   2489   1.1    matt  *      target physical map with the protection requested.
   2490   1.1    matt  *
   2491   1.1    matt  *      If specified, the page will be wired down, meaning
   2492   1.1    matt  *      that the related pte can not be reclaimed.
   2493   1.1    matt  *
   2494   1.1    matt  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2495   1.1    matt  *      or lose information.  That is, this routine must actually
   2496   1.1    matt  *      insert this page into the given map NOW.
   2497   1.1    matt  */
   2498   1.1    matt 
   2499   1.1    matt int
   2500   1.1    matt pmap_enter(pmap, va, pa, prot, flags)
   2501  1.15   chris 	struct pmap *pmap;
   2502   1.1    matt 	vaddr_t va;
   2503   1.2    matt 	paddr_t pa;
   2504   1.1    matt 	vm_prot_t prot;
   2505   1.1    matt 	int flags;
   2506   1.1    matt {
   2507  1.11   chris 	pt_entry_t *pte, *ptes;
   2508   1.1    matt 	u_int npte;
   2509   1.1    matt 	int bank, off;
   2510   1.2    matt 	paddr_t opa;
   2511   1.1    matt 	int nflags;
   2512   1.1    matt 	boolean_t wired = (flags & PMAP_WIRED) != 0;
   2513  1.17   chris 	struct pv_entry *pve;
   2514  1.17   chris 	struct pv_head	*pvh;
   2515  1.17   chris 	int error;
   2516   1.1    matt 
   2517   1.1    matt 	PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
   2518   1.1    matt 	    va, pa, pmap, prot, wired));
   2519   1.1    matt 
   2520   1.1    matt #ifdef DIAGNOSTIC
   2521   1.1    matt 	/* Valid address ? */
   2522   1.1    matt 	if (va >= (KERNEL_VM_BASE + KERNEL_VM_SIZE))
   2523   1.1    matt 		panic("pmap_enter: too big");
   2524   1.1    matt 	if (pmap != pmap_kernel() && va != 0) {
   2525   1.1    matt 		if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
   2526   1.1    matt 			panic("pmap_enter: kernel page in user map");
   2527   1.1    matt 	} else {
   2528   1.1    matt 		if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
   2529   1.1    matt 			panic("pmap_enter: user page in kernel map");
   2530   1.1    matt 		if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
   2531   1.1    matt 			panic("pmap_enter: entering PT page");
   2532   1.1    matt 	}
   2533   1.1    matt #endif
   2534  1.17   chris 	/* get lock */
   2535  1.17   chris 	PMAP_MAP_TO_HEAD_LOCK();
   2536   1.1    matt 	/*
   2537   1.1    matt 	 * Get a pointer to the pte for this virtual address. If the
   2538   1.1    matt 	 * pte pointer is NULL then we are missing the L2 page table
   2539   1.1    matt 	 * so we need to create one.
   2540   1.1    matt 	 */
   2541  1.24   chris 	/* XXX horrible hack to get us working with lockdebug */
   2542  1.24   chris 	simple_lock(&pmap->pm_obj.vmobjlock);
   2543   1.1    matt 	pte = pmap_pte(pmap, va);
   2544   1.1    matt 	if (!pte) {
   2545  1.17   chris 		struct vm_page *ptp;
   2546  1.17   chris 
   2547  1.17   chris 		/* if failure is allowed then don't try too hard */
   2548  1.17   chris 		ptp = pmap_get_ptp(pmap, va, flags & PMAP_CANFAIL);
   2549  1.17   chris 		if (ptp == NULL) {
   2550  1.17   chris 			if (flags & PMAP_CANFAIL) {
   2551  1.17   chris 				error = ENOMEM;
   2552  1.17   chris 				goto out;
   2553  1.17   chris 			}
   2554  1.17   chris 			panic("pmap_enter: get ptp failed");
   2555   1.1    matt 		}
   2556  1.16   chris 
   2557   1.1    matt 		pte = pmap_pte(pmap, va);
   2558   1.1    matt #ifdef DIAGNOSTIC
   2559   1.1    matt 		if (!pte)
   2560   1.1    matt 			panic("pmap_enter: no pte");
   2561   1.1    matt #endif
   2562   1.1    matt 	}
   2563   1.1    matt 
   2564   1.1    matt 	nflags = 0;
   2565   1.1    matt 	if (prot & VM_PROT_WRITE)
   2566   1.1    matt 		nflags |= PT_Wr;
   2567   1.1    matt 	if (wired)
   2568   1.1    matt 		nflags |= PT_W;
   2569   1.1    matt 
   2570   1.1    matt 	/* More debugging info */
   2571   1.1    matt 	PDEBUG(5, printf("pmap_enter: pte for V%08lx = V%p (%08x)\n", va, pte,
   2572   1.1    matt 	    *pte));
   2573   1.1    matt 
   2574   1.1    matt 	/* Is the pte valid ? If so then this page is already mapped */
   2575   1.1    matt 	if (pmap_pte_v(pte)) {
   2576   1.1    matt 		/* Get the physical address of the current page mapped */
   2577   1.1    matt 		opa = pmap_pte_pa(pte);
   2578   1.1    matt 
   2579   1.1    matt #ifdef MYCROFT_HACK
   2580   1.1    matt 		printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx\n", pmap, va, pa, opa);
   2581   1.1    matt #endif
   2582   1.1    matt 
   2583   1.1    matt 		/* Are we mapping the same page ? */
   2584   1.1    matt 		if (opa == pa) {
   2585   1.1    matt 			/* All we must be doing is changing the protection */
   2586   1.1    matt 			PDEBUG(0, printf("Case 02 in pmap_enter (V%08lx P%08lx)\n",
   2587   1.1    matt 			    va, pa));
   2588   1.1    matt 
   2589   1.1    matt 			/* Has the wiring changed ? */
   2590   1.1    matt 			if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
   2591  1.17   chris 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2592  1.17   chris 				simple_lock(&pvh->pvh_lock);
   2593  1.17   chris 				(void) pmap_modify_pv(pmap, va, pvh,
   2594   1.1    matt 				    PT_Wr | PT_W, nflags);
   2595  1.17   chris 				simple_unlock(&pvh->pvh_lock);
   2596  1.17   chris  			} else {
   2597  1.17   chris 				pvh = NULL;
   2598  1.17   chris 			}
   2599   1.1    matt 		} else {
   2600   1.1    matt 			/* We are replacing the page with a new one. */
   2601   1.1    matt 			cpu_cache_purgeID_rng(va, NBPG);
   2602   1.1    matt 
   2603   1.1    matt 			PDEBUG(0, printf("Case 03 in pmap_enter (V%08lx P%08lx P%08lx)\n",
   2604   1.1    matt 			    va, pa, opa));
   2605   1.1    matt 
   2606   1.1    matt 			/*
   2607   1.1    matt 			 * If it is part of our managed memory then we
   2608   1.1    matt 			 * must remove it from the PV list
   2609   1.1    matt 			 */
   2610   1.1    matt 			if ((bank = vm_physseg_find(atop(opa), &off)) != -1) {
   2611  1.17   chris 				pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2612  1.17   chris 				simple_lock(&pvh->pvh_lock);
   2613  1.17   chris 				pve = pmap_remove_pv(pvh, pmap, va);
   2614  1.17   chris 				simple_unlock(&pvh->pvh_lock);
   2615  1.17   chris 			} else {
   2616  1.17   chris 				pve = NULL;
   2617   1.1    matt 			}
   2618   1.1    matt 
   2619   1.1    matt 			goto enter;
   2620   1.1    matt 		}
   2621   1.1    matt 	} else {
   2622   1.1    matt 		opa = 0;
   2623  1.17   chris 		pve = NULL;
   2624   1.1    matt 		pmap_pte_addref(pmap, va);
   2625   1.1    matt 
   2626   1.1    matt 		/* pte is not valid so we must be hooking in a new page */
   2627   1.1    matt 		++pmap->pm_stats.resident_count;
   2628   1.1    matt 
   2629   1.1    matt 	enter:
   2630   1.1    matt 		/*
   2631   1.1    matt 		 * Enter on the PV list if part of our managed memory
   2632   1.1    matt 		 */
   2633  1.17   chris 		bank = vm_physseg_find(atop(pa), &off);
   2634  1.17   chris 
   2635  1.17   chris 		if (pmap_initialized && (bank != -1)) {
   2636  1.17   chris 			pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2637  1.17   chris 			if (pve == NULL) {
   2638  1.17   chris 				pve = pmap_alloc_pv(pmap, ALLOCPV_NEED);
   2639  1.17   chris 				if (pve == NULL) {
   2640  1.17   chris 					if (flags & PMAP_CANFAIL) {
   2641  1.17   chris 						error = ENOMEM;
   2642  1.17   chris 						goto out;
   2643  1.17   chris 					}
   2644  1.17   chris 					panic("pmap_enter: no pv entries available");
   2645  1.17   chris 				}
   2646  1.17   chris 			}
   2647  1.17   chris 			/* enter_pv locks pvh when adding */
   2648  1.17   chris 			pmap_enter_pv(pvh, pve, pmap, va, NULL, nflags);
   2649  1.17   chris 		} else {
   2650  1.17   chris 			pvh = NULL;
   2651  1.17   chris 			if (pve != NULL)
   2652  1.17   chris 				pmap_free_pv(pmap, pve);
   2653   1.1    matt 		}
   2654   1.1    matt 	}
   2655   1.1    matt 
   2656   1.1    matt #ifdef MYCROFT_HACK
   2657   1.1    matt 	if (mycroft_hack)
   2658   1.1    matt 		printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx bank=%d off=%d pv=%p\n", pmap, va, pa, opa, bank, off, pv);
   2659   1.1    matt #endif
   2660   1.1    matt 
   2661   1.1    matt 	/* Construct the pte, giving the correct access. */
   2662   1.1    matt 	npte = (pa & PG_FRAME);
   2663   1.1    matt 
   2664   1.1    matt 	/* VA 0 is magic. */
   2665   1.1    matt 	if (pmap != pmap_kernel() && va != 0)
   2666   1.1    matt 		npte |= PT_AP(AP_U);
   2667   1.1    matt 
   2668  1.17   chris 	if (pmap_initialized && bank != -1) {
   2669   1.1    matt #ifdef DIAGNOSTIC
   2670   1.1    matt 		if ((flags & VM_PROT_ALL) & ~prot)
   2671   1.1    matt 			panic("pmap_enter: access_type exceeds prot");
   2672   1.1    matt #endif
   2673   1.1    matt 		npte |= PT_C | PT_B;
   2674   1.1    matt 		if (flags & VM_PROT_WRITE) {
   2675   1.1    matt 			npte |= L2_SPAGE | PT_AP(AP_W);
   2676   1.1    matt 			vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   2677   1.1    matt 		} else if (flags & VM_PROT_ALL) {
   2678   1.1    matt 			npte |= L2_SPAGE;
   2679   1.1    matt 			vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   2680   1.1    matt 		} else
   2681   1.1    matt 			npte |= L2_INVAL;
   2682   1.1    matt 	} else {
   2683   1.1    matt 		if (prot & VM_PROT_WRITE)
   2684   1.1    matt 			npte |= L2_SPAGE | PT_AP(AP_W);
   2685   1.1    matt 		else if (prot & VM_PROT_ALL)
   2686   1.1    matt 			npte |= L2_SPAGE;
   2687   1.1    matt 		else
   2688   1.1    matt 			npte |= L2_INVAL;
   2689   1.1    matt 	}
   2690   1.1    matt 
   2691   1.1    matt #ifdef MYCROFT_HACK
   2692   1.1    matt 	if (mycroft_hack)
   2693   1.1    matt 		printf("pmap_enter: pmap=%p va=%lx pa=%lx prot=%x wired=%d access_type=%x npte=%08x\n", pmap, va, pa, prot, wired, flags & VM_PROT_ALL, npte);
   2694   1.1    matt #endif
   2695   1.1    matt 
   2696   1.1    matt 	*pte = npte;
   2697   1.1    matt 
   2698  1.17   chris 	if (pmap_initialized && bank != -1)
   2699  1.11   chris 	{
   2700  1.12   chris 		boolean_t pmap_active = FALSE;
   2701  1.11   chris 		/* XXX this will change once the whole of pmap_enter uses
   2702  1.11   chris 		 * map_ptes
   2703  1.11   chris 		 */
   2704  1.11   chris 		ptes = pmap_map_ptes(pmap);
   2705  1.12   chris 		if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
   2706  1.15   chris 		    || (pmap == pmap_kernel()))
   2707  1.12   chris 			pmap_active = TRUE;
   2708  1.17   chris 		simple_lock(&pvh->pvh_lock);
   2709  1.17   chris  		pmap_vac_me_harder(pmap, pvh, ptes, pmap_active);
   2710  1.17   chris 		simple_unlock(&pvh->pvh_lock);
   2711  1.11   chris 		pmap_unmap_ptes(pmap);
   2712  1.11   chris 	}
   2713   1.1    matt 
   2714   1.1    matt 	/* Better flush the TLB ... */
   2715   1.1    matt 	cpu_tlb_flushID_SE(va);
   2716  1.17   chris 	error = 0;
   2717  1.17   chris out:
   2718  1.24   chris 	simple_unlock(&pmap->pm_obj.vmobjlock);
   2719  1.17   chris 	PMAP_MAP_TO_HEAD_UNLOCK();
   2720   1.1    matt 	PDEBUG(5, printf("pmap_enter: pte = V%p %08x\n", pte, *pte));
   2721   1.1    matt 
   2722  1.17   chris 	return error;
   2723   1.1    matt }
   2724   1.1    matt 
   2725   1.1    matt void
   2726   1.1    matt pmap_kenter_pa(va, pa, prot)
   2727   1.1    matt 	vaddr_t va;
   2728   1.1    matt 	paddr_t pa;
   2729   1.1    matt 	vm_prot_t prot;
   2730   1.1    matt {
   2731  1.14     chs 	struct pmap *pmap = pmap_kernel();
   2732  1.13   chris 	pt_entry_t *pte;
   2733  1.14     chs 	struct vm_page *pg;
   2734  1.13   chris 
   2735  1.14     chs 	if (!pmap_pde_v(pmap_pde(pmap, va))) {
   2736  1.14     chs 
   2737  1.13   chris 		/*
   2738  1.13   chris 		 * For the kernel pmaps it would be better to ensure
   2739  1.13   chris 		 * that they are always present, and to grow the
   2740  1.13   chris 		 * kernel as required.
   2741  1.13   chris 		 */
   2742  1.13   chris 
   2743  1.24   chris 	    	/* must lock the pmap */
   2744  1.24   chris 	    	simple_lock(&(pmap_kernel()->pm_obj.vmobjlock));
   2745  1.13   chris 		/* Allocate a page table */
   2746  1.16   chris 		pg = uvm_pagealloc(&(pmap_kernel()->pm_obj), 0, NULL,
   2747  1.14     chs 		    UVM_PGA_USERESERVE | UVM_PGA_ZERO);
   2748  1.14     chs 		if (pg == NULL) {
   2749  1.13   chris 			panic("pmap_kenter_pa: no free pages");
   2750  1.13   chris 		}
   2751  1.16   chris 		pg->flags &= ~PG_BUSY;	/* never busy */
   2752  1.13   chris 
   2753  1.13   chris 		/* Wire this page table into the L1. */
   2754  1.17   chris 		pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(pg), TRUE);
   2755  1.24   chris 		simple_unlock(&(pmap_kernel()->pm_obj.vmobjlock));
   2756  1.13   chris 	}
   2757  1.13   chris 	pte = vtopte(va);
   2758  1.14     chs 	KASSERT(!pmap_pte_v(pte));
   2759  1.13   chris 	*pte = L2_PTE(pa, AP_KRW);
   2760   1.1    matt }
   2761   1.1    matt 
   2762   1.1    matt void
   2763   1.1    matt pmap_kremove(va, len)
   2764   1.1    matt 	vaddr_t va;
   2765   1.1    matt 	vsize_t len;
   2766   1.1    matt {
   2767  1.14     chs 	pt_entry_t *pte;
   2768  1.14     chs 
   2769   1.1    matt 	for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
   2770  1.13   chris 
   2771  1.14     chs 		/*
   2772  1.14     chs 		 * We assume that we will only be called with small
   2773  1.14     chs 		 * regions of memory.
   2774  1.14     chs 		 */
   2775  1.14     chs 
   2776  1.14     chs 		KASSERT(pmap_pde_v(pmap_pde(pmap_kernel(), va)));
   2777  1.13   chris 		pte = vtopte(va);
   2778  1.13   chris 		cpu_cache_purgeID_rng(va, PAGE_SIZE);
   2779  1.13   chris 		*pte = 0;
   2780  1.13   chris 		cpu_tlb_flushID_SE(va);
   2781   1.1    matt 	}
   2782   1.1    matt }
   2783   1.1    matt 
   2784   1.1    matt /*
   2785   1.1    matt  * pmap_page_protect:
   2786   1.1    matt  *
   2787   1.1    matt  * Lower the permission for all mappings to a given page.
   2788   1.1    matt  */
   2789   1.1    matt 
   2790   1.1    matt void
   2791   1.1    matt pmap_page_protect(pg, prot)
   2792   1.1    matt 	struct vm_page *pg;
   2793   1.1    matt 	vm_prot_t prot;
   2794   1.1    matt {
   2795   1.1    matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2796   1.1    matt 
   2797   1.1    matt 	PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n", pa, prot));
   2798   1.1    matt 
   2799   1.1    matt 	switch(prot) {
   2800  1.17   chris 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   2801  1.17   chris 	case VM_PROT_READ|VM_PROT_WRITE:
   2802  1.17   chris 		return;
   2803  1.17   chris 
   2804   1.1    matt 	case VM_PROT_READ:
   2805   1.1    matt 	case VM_PROT_READ|VM_PROT_EXECUTE:
   2806   1.1    matt 		pmap_copy_on_write(pa);
   2807   1.1    matt 		break;
   2808   1.1    matt 
   2809   1.1    matt 	default:
   2810   1.1    matt 		pmap_remove_all(pa);
   2811   1.1    matt 		break;
   2812   1.1    matt 	}
   2813   1.1    matt }
   2814   1.1    matt 
   2815   1.1    matt 
   2816   1.1    matt /*
   2817   1.1    matt  * Routine:	pmap_unwire
   2818   1.1    matt  * Function:	Clear the wired attribute for a map/virtual-address
   2819   1.1    matt  *		pair.
   2820   1.1    matt  * In/out conditions:
   2821   1.1    matt  *		The mapping must already exist in the pmap.
   2822   1.1    matt  */
   2823   1.1    matt 
   2824   1.1    matt void
   2825   1.1    matt pmap_unwire(pmap, va)
   2826  1.15   chris 	struct pmap *pmap;
   2827   1.1    matt 	vaddr_t va;
   2828   1.1    matt {
   2829   1.1    matt 	pt_entry_t *pte;
   2830   1.2    matt 	paddr_t pa;
   2831   1.1    matt 	int bank, off;
   2832  1.17   chris 	struct pv_head *pvh;
   2833   1.1    matt 
   2834   1.1    matt 	/*
   2835   1.1    matt 	 * Make sure pmap is valid. -dct
   2836   1.1    matt 	 */
   2837   1.1    matt 	if (pmap == NULL)
   2838   1.1    matt 		return;
   2839   1.1    matt 
   2840   1.1    matt 	/* Get the pte */
   2841   1.1    matt 	pte = pmap_pte(pmap, va);
   2842   1.1    matt 	if (!pte)
   2843   1.1    matt 		return;
   2844   1.1    matt 
   2845   1.1    matt 	/* Extract the physical address of the page */
   2846   1.1    matt 	pa = pmap_pte_pa(pte);
   2847   1.1    matt 
   2848   1.1    matt 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   2849   1.1    matt 		return;
   2850  1.17   chris 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   2851  1.17   chris 	simple_lock(&pvh->pvh_lock);
   2852   1.1    matt 	/* Update the wired bit in the pv entry for this page. */
   2853  1.17   chris 	(void) pmap_modify_pv(pmap, va, pvh, PT_W, 0);
   2854  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   2855   1.1    matt }
   2856   1.1    matt 
   2857   1.1    matt /*
   2858  1.15   chris  * pt_entry_t *pmap_pte(struct pmap *pmap, vaddr_t va)
   2859   1.1    matt  *
   2860   1.1    matt  * Return the pointer to a page table entry corresponding to the supplied
   2861   1.1    matt  * virtual address.
   2862   1.1    matt  *
   2863   1.1    matt  * The page directory is first checked to make sure that a page table
   2864   1.1    matt  * for the address in question exists and if it does a pointer to the
   2865   1.1    matt  * entry is returned.
   2866   1.1    matt  *
   2867   1.1    matt  * The way this works is that that the kernel page tables are mapped
   2868   1.1    matt  * into the memory map at ALT_PAGE_TBLS_BASE to ALT_PAGE_TBLS_BASE+4MB.
   2869   1.1    matt  * This allows page tables to be located quickly.
   2870   1.1    matt  */
   2871   1.1    matt pt_entry_t *
   2872   1.1    matt pmap_pte(pmap, va)
   2873  1.15   chris 	struct pmap *pmap;
   2874   1.1    matt 	vaddr_t va;
   2875   1.1    matt {
   2876   1.1    matt 	pt_entry_t *ptp;
   2877   1.1    matt 	pt_entry_t *result;
   2878   1.1    matt 
   2879   1.1    matt 	/* The pmap must be valid */
   2880   1.1    matt 	if (!pmap)
   2881   1.1    matt 		return(NULL);
   2882   1.1    matt 
   2883   1.1    matt 	/* Return the address of the pte */
   2884   1.1    matt 	PDEBUG(10, printf("pmap_pte: pmap=%p va=V%08lx pde = V%p (%08X)\n",
   2885   1.1    matt 	    pmap, va, pmap_pde(pmap, va), *(pmap_pde(pmap, va))));
   2886   1.1    matt 
   2887   1.1    matt 	/* Do we have a valid pde ? If not we don't have a page table */
   2888   1.1    matt 	if (!pmap_pde_v(pmap_pde(pmap, va))) {
   2889   1.1    matt 		PDEBUG(0, printf("pmap_pte: failed - pde = %p\n",
   2890   1.1    matt 		    pmap_pde(pmap, va)));
   2891   1.1    matt 		return(NULL);
   2892   1.1    matt 	}
   2893   1.1    matt 
   2894   1.1    matt 	PDEBUG(10, printf("pmap pagetable = P%08lx current = P%08x\n",
   2895   1.1    matt 	    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2896   1.1    matt 	    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2897   1.1    matt 	    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)));
   2898   1.1    matt 
   2899   1.1    matt 	/*
   2900   1.1    matt 	 * If the pmap is the kernel pmap or the pmap is the active one
   2901   1.1    matt 	 * then we can just return a pointer to entry relative to
   2902   1.1    matt 	 * PROCESS_PAGE_TBLS_BASE.
   2903   1.1    matt 	 * Otherwise we need to map the page tables to an alternative
   2904   1.1    matt 	 * address and reference them there.
   2905   1.1    matt 	 */
   2906  1.15   chris 	if (pmap == pmap_kernel() || pmap->pm_pptpt
   2907   1.1    matt 	    == (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2908   1.1    matt 	    + ((PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) &
   2909   1.1    matt 	    ~3) + (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   2910   1.1    matt 		ptp = (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   2911   1.1    matt 	} else {
   2912   1.1    matt 		struct proc *p = curproc;
   2913   1.1    matt 
   2914   1.1    matt 		/* If we don't have a valid curproc use proc0 */
   2915   1.1    matt 		/* Perhaps we should just use kernel_pmap instead */
   2916   1.1    matt 		if (p == NULL)
   2917   1.1    matt 			p = &proc0;
   2918   1.1    matt #ifdef DIAGNOSTIC
   2919   1.1    matt 		/*
   2920   1.1    matt 		 * The pmap should always be valid for the process so
   2921   1.1    matt 		 * panic if it is not.
   2922   1.1    matt 		 */
   2923   1.1    matt 		if (!p->p_vmspace || !p->p_vmspace->vm_map.pmap) {
   2924   1.1    matt 			printf("pmap_pte: va=%08lx p=%p vm=%p\n",
   2925   1.1    matt 			    va, p, p->p_vmspace);
   2926   1.1    matt 			console_debugger();
   2927   1.1    matt 		}
   2928   1.1    matt 		/*
   2929   1.1    matt 		 * The pmap for the current process should be mapped. If it
   2930   1.1    matt 		 * is not then we have a problem.
   2931   1.1    matt 		 */
   2932   1.1    matt 		if (p->p_vmspace->vm_map.pmap->pm_pptpt !=
   2933   1.1    matt 		    (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2934   1.1    matt 		    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2935   1.1    matt 		    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
   2936   1.1    matt 			printf("pmap pagetable = P%08lx current = P%08x ",
   2937   1.1    matt 			    pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
   2938   1.1    matt 			    + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
   2939   1.1    matt 			    (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) &
   2940   1.1    matt 			    PG_FRAME));
   2941   1.1    matt 			printf("pptpt=%lx\n", p->p_vmspace->vm_map.pmap->pm_pptpt);
   2942   1.1    matt 			panic("pmap_pte: current and pmap mismatch\n");
   2943   1.1    matt 		}
   2944   1.1    matt #endif
   2945   1.1    matt 
   2946   1.1    matt 		ptp = (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   2947   1.1    matt 		pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   2948  1.17   chris 		    pmap->pm_pptpt, FALSE);
   2949   1.1    matt 		cpu_tlb_flushD();
   2950   1.1    matt 	}
   2951   1.1    matt 	PDEBUG(10, printf("page tables base = %p offset=%lx\n", ptp,
   2952   1.1    matt 	    ((va >> (PGSHIFT-2)) & ~3)));
   2953   1.1    matt 	result = (pt_entry_t *)((char *)ptp + ((va >> (PGSHIFT-2)) & ~3));
   2954   1.1    matt 	return(result);
   2955   1.1    matt }
   2956   1.1    matt 
   2957   1.1    matt /*
   2958   1.1    matt  * Routine:  pmap_extract
   2959   1.1    matt  * Function:
   2960   1.1    matt  *           Extract the physical page address associated
   2961   1.1    matt  *           with the given map/virtual_address pair.
   2962   1.1    matt  */
   2963   1.1    matt boolean_t
   2964   1.1    matt pmap_extract(pmap, va, pap)
   2965  1.15   chris 	struct pmap *pmap;
   2966   1.1    matt 	vaddr_t va;
   2967   1.1    matt 	paddr_t *pap;
   2968   1.1    matt {
   2969  1.11   chris 	pt_entry_t *pte, *ptes;
   2970   1.1    matt 	paddr_t pa;
   2971   1.1    matt 
   2972   1.1    matt 	PDEBUG(5, printf("pmap_extract: pmap=%p, va=V%08lx\n", pmap, va));
   2973   1.1    matt 
   2974   1.1    matt 	/*
   2975  1.11   chris 	 * Get the pte for this virtual address.
   2976   1.1    matt 	 */
   2977  1.11   chris 	ptes = pmap_map_ptes(pmap);
   2978  1.11   chris 	pte = &ptes[arm_byte_to_page(va)];
   2979   1.1    matt 
   2980  1.11   chris 	/*
   2981  1.11   chris 	 * If there is no pte then there is no page table etc.
   2982  1.11   chris 	 * Is the pte valid ? If not then no paged is actually mapped here
   2983  1.11   chris 	 */
   2984  1.11   chris 	if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)){
   2985  1.11   chris 	    pmap_unmap_ptes(pmap);
   2986  1.11   chris     	    return (FALSE);
   2987  1.11   chris 	}
   2988   1.1    matt 
   2989   1.1    matt 	/* Return the physical address depending on the PTE type */
   2990   1.1    matt 	/* XXX What about L1 section mappings ? */
   2991   1.1    matt 	if ((*(pte) & L2_MASK) == L2_LPAGE) {
   2992   1.1    matt 		/* Extract the physical address from the pte */
   2993   1.1    matt 		pa = (*(pte)) & ~(L2_LPAGE_SIZE - 1);
   2994   1.1    matt 
   2995   1.1    matt 		PDEBUG(5, printf("pmap_extract: LPAGE pa = P%08lx\n",
   2996   1.1    matt 		    (pa | (va & (L2_LPAGE_SIZE - 1)))));
   2997   1.1    matt 
   2998   1.1    matt 		if (pap != NULL)
   2999   1.1    matt 			*pap = pa | (va & (L2_LPAGE_SIZE - 1));
   3000   1.1    matt 	} else {
   3001   1.1    matt 		/* Extract the physical address from the pte */
   3002   1.1    matt 		pa = pmap_pte_pa(pte);
   3003   1.1    matt 
   3004   1.1    matt 		PDEBUG(5, printf("pmap_extract: SPAGE pa = P%08lx\n",
   3005   1.1    matt 		    (pa | (va & ~PG_FRAME))));
   3006   1.1    matt 
   3007   1.1    matt 		if (pap != NULL)
   3008   1.1    matt 			*pap = pa | (va & ~PG_FRAME);
   3009   1.1    matt 	}
   3010  1.11   chris 	pmap_unmap_ptes(pmap);
   3011  1.11   chris 	return (TRUE);
   3012   1.1    matt }
   3013   1.1    matt 
   3014   1.1    matt 
   3015   1.1    matt /*
   3016   1.1    matt  * Copy the range specified by src_addr/len from the source map to the
   3017   1.1    matt  * range dst_addr/len in the destination map.
   3018   1.1    matt  *
   3019   1.1    matt  * This routine is only advisory and need not do anything.
   3020   1.1    matt  */
   3021   1.1    matt 
   3022   1.1    matt void
   3023   1.1    matt pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
   3024  1.15   chris 	struct pmap *dst_pmap;
   3025  1.15   chris 	struct pmap *src_pmap;
   3026   1.1    matt 	vaddr_t dst_addr;
   3027   1.2    matt 	vsize_t len;
   3028   1.1    matt 	vaddr_t src_addr;
   3029   1.1    matt {
   3030   1.1    matt 	PDEBUG(0, printf("pmap_copy(%p, %p, %lx, %lx, %lx)\n",
   3031   1.1    matt 	    dst_pmap, src_pmap, dst_addr, len, src_addr));
   3032   1.1    matt }
   3033   1.1    matt 
   3034   1.1    matt #if defined(PMAP_DEBUG)
   3035   1.1    matt void
   3036   1.1    matt pmap_dump_pvlist(phys, m)
   3037   1.1    matt 	vaddr_t phys;
   3038   1.1    matt 	char *m;
   3039   1.1    matt {
   3040  1.17   chris 	struct pv_head *pvh;
   3041   1.1    matt 	struct pv_entry *pv;
   3042   1.1    matt 	int bank, off;
   3043   1.1    matt 
   3044   1.1    matt 	if ((bank = vm_physseg_find(atop(phys), &off)) == -1) {
   3045   1.1    matt 		printf("INVALID PA\n");
   3046   1.1    matt 		return;
   3047   1.1    matt 	}
   3048  1.17   chris 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3049  1.17   chris 	simple_lock(&pvh->pvh_lock);
   3050   1.1    matt 	printf("%s %08lx:", m, phys);
   3051  1.17   chris 	if (pvh->pvh_list == NULL) {
   3052   1.1    matt 		printf(" no mappings\n");
   3053   1.1    matt 		return;
   3054   1.1    matt 	}
   3055   1.1    matt 
   3056  1.17   chris 	for (pv = pvh->pvh_list; pv; pv = pv->pv_next)
   3057   1.1    matt 		printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
   3058   1.1    matt 		    pv->pv_va, pv->pv_flags);
   3059   1.1    matt 
   3060   1.1    matt 	printf("\n");
   3061  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   3062   1.1    matt }
   3063   1.1    matt 
   3064   1.1    matt #endif	/* PMAP_DEBUG */
   3065   1.1    matt 
   3066  1.22   chris __inline static boolean_t
   3067   1.1    matt pmap_testbit(pa, setbits)
   3068   1.2    matt 	paddr_t pa;
   3069  1.22   chris 	unsigned int setbits;
   3070   1.1    matt {
   3071   1.1    matt 	int bank, off;
   3072   1.1    matt 
   3073   1.1    matt 	PDEBUG(1, printf("pmap_testbit: pa=%08lx set=%08x\n", pa, setbits));
   3074   1.1    matt 
   3075   1.1    matt 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3076   1.1    matt 		return(FALSE);
   3077   1.1    matt 
   3078   1.1    matt 	/*
   3079   1.1    matt 	 * Check saved info only
   3080   1.1    matt 	 */
   3081   1.1    matt 	if (vm_physmem[bank].pmseg.attrs[off] & setbits) {
   3082   1.1    matt 		PDEBUG(0, printf("pmap_attributes = %02x\n",
   3083   1.1    matt 		    vm_physmem[bank].pmseg.attrs[off]));
   3084   1.1    matt 		return(TRUE);
   3085   1.1    matt 	}
   3086   1.1    matt 
   3087   1.1    matt 	return(FALSE);
   3088   1.1    matt }
   3089   1.1    matt 
   3090  1.11   chris static pt_entry_t *
   3091  1.11   chris pmap_map_ptes(struct pmap *pmap)
   3092  1.11   chris {
   3093  1.17   chris     	struct proc *p;
   3094  1.17   chris 
   3095  1.17   chris     	/* the kernel's pmap is always accessible */
   3096  1.17   chris 	if (pmap == pmap_kernel()) {
   3097  1.17   chris 		return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE ;
   3098  1.17   chris 	}
   3099  1.17   chris 
   3100  1.17   chris 	if (pmap_is_curpmap(pmap)) {
   3101  1.17   chris 		simple_lock(&pmap->pm_obj.vmobjlock);
   3102  1.17   chris 		return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
   3103  1.17   chris 	}
   3104  1.17   chris 
   3105  1.17   chris 	p = curproc;
   3106  1.17   chris 
   3107  1.17   chris 	if (p == NULL)
   3108  1.17   chris 		p = &proc0;
   3109  1.17   chris 
   3110  1.17   chris 	/* need to lock both curpmap and pmap: use ordered locking */
   3111  1.17   chris 	if ((unsigned) pmap < (unsigned) curproc->p_vmspace->vm_map.pmap) {
   3112  1.17   chris 		simple_lock(&pmap->pm_obj.vmobjlock);
   3113  1.17   chris 		simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3114  1.17   chris 	} else {
   3115  1.17   chris 		simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3116  1.17   chris 		simple_lock(&pmap->pm_obj.vmobjlock);
   3117  1.17   chris 	}
   3118  1.11   chris 
   3119  1.17   chris 	pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
   3120  1.17   chris 			pmap->pm_pptpt, FALSE);
   3121  1.17   chris 	cpu_tlb_flushD();
   3122  1.17   chris 	return (pt_entry_t *)ALT_PAGE_TBLS_BASE;
   3123  1.17   chris }
   3124  1.17   chris 
   3125  1.17   chris /*
   3126  1.17   chris  * pmap_unmap_ptes: unlock the PTE mapping of "pmap"
   3127  1.17   chris  */
   3128  1.17   chris 
   3129  1.17   chris static void
   3130  1.17   chris pmap_unmap_ptes(pmap)
   3131  1.17   chris 	struct pmap *pmap;
   3132  1.17   chris {
   3133  1.17   chris 	if (pmap == pmap_kernel()) {
   3134  1.17   chris 		return;
   3135  1.17   chris 	}
   3136  1.17   chris 	if (pmap_is_curpmap(pmap)) {
   3137  1.17   chris 		simple_unlock(&pmap->pm_obj.vmobjlock);
   3138  1.17   chris 	} else {
   3139  1.17   chris 		simple_unlock(&pmap->pm_obj.vmobjlock);
   3140  1.17   chris 		simple_unlock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
   3141  1.17   chris 	}
   3142  1.11   chris }
   3143   1.1    matt 
   3144   1.1    matt /*
   3145   1.1    matt  * Modify pte bits for all ptes corresponding to the given physical address.
   3146   1.1    matt  * We use `maskbits' rather than `clearbits' because we're always passing
   3147   1.1    matt  * constants and the latter would require an extra inversion at run-time.
   3148   1.1    matt  */
   3149   1.1    matt 
   3150  1.22   chris static void
   3151   1.1    matt pmap_clearbit(pa, maskbits)
   3152   1.2    matt 	paddr_t pa;
   3153  1.22   chris 	unsigned int maskbits;
   3154   1.1    matt {
   3155   1.1    matt 	struct pv_entry *pv;
   3156  1.17   chris 	struct pv_head *pvh;
   3157   1.1    matt 	pt_entry_t *pte;
   3158   1.1    matt 	vaddr_t va;
   3159  1.21   chris 	int bank, off, tlbentry;
   3160   1.1    matt 
   3161   1.1    matt 	PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
   3162   1.1    matt 	    pa, maskbits));
   3163  1.21   chris 
   3164  1.21   chris 	tlbentry = 0;
   3165  1.21   chris 
   3166   1.1    matt 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3167   1.1    matt 		return;
   3168  1.17   chris 	PMAP_HEAD_TO_MAP_LOCK();
   3169  1.17   chris 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3170  1.17   chris 	simple_lock(&pvh->pvh_lock);
   3171  1.17   chris 
   3172   1.1    matt 	/*
   3173   1.1    matt 	 * Clear saved attributes (modify, reference)
   3174   1.1    matt 	 */
   3175   1.1    matt 	vm_physmem[bank].pmseg.attrs[off] &= ~maskbits;
   3176   1.1    matt 
   3177  1.17   chris 	if (pvh->pvh_list == NULL) {
   3178  1.17   chris 		simple_unlock(&pvh->pvh_lock);
   3179  1.17   chris 		PMAP_HEAD_TO_MAP_UNLOCK();
   3180   1.1    matt 		return;
   3181   1.1    matt 	}
   3182   1.1    matt 
   3183   1.1    matt 	/*
   3184   1.1    matt 	 * Loop over all current mappings setting/clearing as appropos
   3185   1.1    matt 	 */
   3186  1.17   chris 	for (pv = pvh->pvh_list; pv; pv = pv->pv_next) {
   3187   1.1    matt 		va = pv->pv_va;
   3188   1.1    matt 		pv->pv_flags &= ~maskbits;
   3189   1.1    matt 		pte = pmap_pte(pv->pv_pmap, va);
   3190  1.17   chris 		KASSERT(pte != NULL);
   3191   1.1    matt 		if (maskbits & (PT_Wr|PT_M))
   3192  1.21   chris 		{
   3193  1.21   chris 		    if ((pv->pv_flags & PT_NC))
   3194  1.21   chris 		    {
   3195  1.21   chris 			/*
   3196  1.21   chris 			 * entry is not cacheable, so reenable the cache,
   3197  1.21   chris 			 * nothing to flush
   3198  1.21   chris 			 */
   3199  1.21   chris 			*pte |= (PT_C | PT_B);
   3200  1.21   chris 			pv->pv_flags &= ~PT_NC;
   3201  1.21   chris 		    } else {
   3202  1.21   chris 			/*
   3203  1.21   chris 			 * entry is cacheable check if pmap is current if it
   3204  1.21   chris 			 * is flush it, otherwise it won't be in the cache
   3205  1.21   chris 			 */
   3206  1.21   chris 			if (pmap_is_curpmap(pv->pv_pmap))
   3207  1.21   chris 			{
   3208  1.21   chris 			    /* entry is in current pmap purge it */
   3209  1.21   chris 			    cpu_cache_purgeID_rng(pv->pv_va, NBPG);
   3210  1.21   chris 			}
   3211  1.21   chris 		    }
   3212  1.21   chris 
   3213  1.21   chris 		    /* make the pte read only */
   3214  1.21   chris 	    	    *pte &= ~PT_AP(AP_W);
   3215  1.21   chris 
   3216  1.21   chris 	    	    if (pmap_is_curpmap(pv->pv_pmap))
   3217  1.21   chris 			/*
   3218  1.21   chris 			 * if we had cacheable pte's we'd clean the pte out to
   3219  1.21   chris 			 * memory here
   3220  1.21   chris 			 */
   3221  1.21   chris     			/*
   3222  1.21   chris 			 * flush tlb entry as it's in the current pmap
   3223  1.21   chris 			 */
   3224  1.21   chris 			cpu_tlb_flushID_SE(pv->pv_va);
   3225  1.21   chris 
   3226  1.21   chris 		}
   3227   1.1    matt 		if (maskbits & PT_H)
   3228   1.1    matt 			*pte = (*pte & ~L2_MASK) | L2_INVAL;
   3229   1.1    matt 	}
   3230  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   3231  1.17   chris 	PMAP_HEAD_TO_MAP_UNLOCK();
   3232   1.1    matt }
   3233   1.1    matt 
   3234   1.1    matt 
   3235   1.1    matt boolean_t
   3236   1.1    matt pmap_clear_modify(pg)
   3237   1.1    matt 	struct vm_page *pg;
   3238   1.1    matt {
   3239   1.1    matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3240   1.1    matt 	boolean_t rv;
   3241   1.1    matt 
   3242   1.1    matt 	PDEBUG(0, printf("pmap_clear_modify pa=%08lx\n", pa));
   3243   1.1    matt 	rv = pmap_testbit(pa, PT_M);
   3244   1.1    matt 	pmap_clearbit(pa, PT_M);
   3245   1.1    matt 	return rv;
   3246   1.1    matt }
   3247   1.1    matt 
   3248   1.1    matt 
   3249   1.1    matt boolean_t
   3250   1.1    matt pmap_clear_reference(pg)
   3251   1.1    matt 	struct vm_page *pg;
   3252   1.1    matt {
   3253   1.1    matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3254   1.1    matt 	boolean_t rv;
   3255   1.1    matt 
   3256   1.1    matt 	PDEBUG(0, printf("pmap_clear_reference pa=%08lx\n", pa));
   3257   1.1    matt 	rv = pmap_testbit(pa, PT_H);
   3258   1.1    matt 	pmap_clearbit(pa, PT_H);
   3259   1.1    matt 	return rv;
   3260   1.1    matt }
   3261   1.1    matt 
   3262   1.1    matt 
   3263   1.1    matt void
   3264   1.1    matt pmap_copy_on_write(pa)
   3265   1.2    matt 	paddr_t pa;
   3266   1.1    matt {
   3267   1.1    matt 	PDEBUG(0, printf("pmap_copy_on_write pa=%08lx\n", pa));
   3268   1.1    matt 	pmap_clearbit(pa, PT_Wr);
   3269   1.1    matt }
   3270   1.1    matt 
   3271   1.1    matt 
   3272   1.1    matt boolean_t
   3273   1.1    matt pmap_is_modified(pg)
   3274   1.1    matt 	struct vm_page *pg;
   3275   1.1    matt {
   3276   1.1    matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3277   1.1    matt 	boolean_t result;
   3278   1.1    matt 
   3279   1.1    matt 	result = pmap_testbit(pa, PT_M);
   3280  1.17   chris 	PDEBUG(1, printf("pmap_is_modified pa=%08lx %x\n", pa, result));
   3281   1.1    matt 	return (result);
   3282   1.1    matt }
   3283   1.1    matt 
   3284   1.1    matt 
   3285   1.1    matt boolean_t
   3286   1.1    matt pmap_is_referenced(pg)
   3287   1.1    matt 	struct vm_page *pg;
   3288   1.1    matt {
   3289   1.1    matt 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3290   1.1    matt 	boolean_t result;
   3291   1.1    matt 
   3292   1.1    matt 	result = pmap_testbit(pa, PT_H);
   3293   1.1    matt 	PDEBUG(0, printf("pmap_is_referenced pa=%08lx %x\n", pa, result));
   3294   1.1    matt 	return (result);
   3295   1.1    matt }
   3296   1.1    matt 
   3297   1.1    matt 
   3298   1.1    matt int
   3299   1.1    matt pmap_modified_emulation(pmap, va)
   3300  1.15   chris 	struct pmap *pmap;
   3301   1.1    matt 	vaddr_t va;
   3302   1.1    matt {
   3303   1.1    matt 	pt_entry_t *pte;
   3304   1.2    matt 	paddr_t pa;
   3305   1.1    matt 	int bank, off;
   3306  1.17   chris 	struct pv_head *pvh;
   3307   1.1    matt 	u_int flags;
   3308   1.1    matt 
   3309   1.1    matt 	PDEBUG(2, printf("pmap_modified_emulation\n"));
   3310   1.1    matt 
   3311   1.1    matt 	/* Get the pte */
   3312   1.1    matt 	pte = pmap_pte(pmap, va);
   3313   1.1    matt 	if (!pte) {
   3314   1.1    matt 		PDEBUG(2, printf("no pte\n"));
   3315   1.1    matt 		return(0);
   3316   1.1    matt 	}
   3317   1.1    matt 
   3318   1.1    matt 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   3319   1.1    matt 
   3320   1.1    matt 	/* Check for a zero pte */
   3321   1.1    matt 	if (*pte == 0)
   3322   1.1    matt 		return(0);
   3323   1.1    matt 
   3324   1.1    matt 	/* This can happen if user code tries to access kernel memory. */
   3325   1.1    matt 	if ((*pte & PT_AP(AP_W)) != 0)
   3326   1.1    matt 		return (0);
   3327   1.1    matt 
   3328   1.1    matt 	/* Extract the physical address of the page */
   3329   1.1    matt 	pa = pmap_pte_pa(pte);
   3330   1.1    matt 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3331   1.1    matt 		return(0);
   3332   1.1    matt 
   3333  1.17   chris 	PMAP_HEAD_TO_MAP_LOCK();
   3334   1.1    matt 	/* Get the current flags for this page. */
   3335  1.17   chris 	pvh = &vm_physmem[bank].pmseg.pvhead[off];
   3336  1.17   chris 	/* XXX: needed if we hold head->map lock? */
   3337  1.17   chris 	simple_lock(&pvh->pvh_lock);
   3338  1.17   chris 
   3339  1.17   chris 	flags = pmap_modify_pv(pmap, va, pvh, 0, 0);
   3340   1.1    matt 	PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
   3341   1.1    matt 
   3342   1.1    matt 	/*
   3343   1.1    matt 	 * Do the flags say this page is writable ? If not then it is a
   3344   1.1    matt 	 * genuine write fault. If yes then the write fault is our fault
   3345   1.1    matt 	 * as we did not reflect the write access in the PTE. Now we know
   3346   1.1    matt 	 * a write has occurred we can correct this and also set the
   3347   1.1    matt 	 * modified bit
   3348   1.1    matt 	 */
   3349  1.17   chris 	if (~flags & PT_Wr) {
   3350  1.17   chris 	    	simple_unlock(&pvh->pvh_lock);
   3351  1.17   chris 		PMAP_HEAD_TO_MAP_UNLOCK();
   3352   1.1    matt 		return(0);
   3353  1.17   chris 	}
   3354   1.1    matt 
   3355   1.1    matt 	PDEBUG(0, printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %p (%08x)\n",
   3356   1.1    matt 	    va, pte, *pte));
   3357   1.1    matt 	vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
   3358   1.1    matt 	*pte = (*pte & ~L2_MASK) | L2_SPAGE | PT_AP(AP_W);
   3359   1.1    matt 	PDEBUG(0, printf("->(%08x)\n", *pte));
   3360   1.1    matt 
   3361  1.17   chris 	simple_unlock(&pvh->pvh_lock);
   3362  1.17   chris 	PMAP_HEAD_TO_MAP_UNLOCK();
   3363   1.1    matt 	/* Return, indicating the problem has been dealt with */
   3364   1.1    matt 	cpu_tlb_flushID_SE(va);
   3365   1.1    matt 	return(1);
   3366   1.1    matt }
   3367   1.1    matt 
   3368   1.1    matt 
   3369   1.1    matt int
   3370   1.1    matt pmap_handled_emulation(pmap, va)
   3371  1.15   chris 	struct pmap *pmap;
   3372   1.1    matt 	vaddr_t va;
   3373   1.1    matt {
   3374   1.1    matt 	pt_entry_t *pte;
   3375   1.2    matt 	paddr_t pa;
   3376   1.1    matt 	int bank, off;
   3377   1.1    matt 
   3378   1.1    matt 	PDEBUG(2, printf("pmap_handled_emulation\n"));
   3379   1.1    matt 
   3380   1.1    matt 	/* Get the pte */
   3381   1.1    matt 	pte = pmap_pte(pmap, va);
   3382   1.1    matt 	if (!pte) {
   3383   1.1    matt 		PDEBUG(2, printf("no pte\n"));
   3384   1.1    matt 		return(0);
   3385   1.1    matt 	}
   3386   1.1    matt 
   3387   1.1    matt 	PDEBUG(1, printf("*pte=%08x\n", *pte));
   3388   1.1    matt 
   3389   1.1    matt 	/* Check for a zero pte */
   3390   1.1    matt 	if (*pte == 0)
   3391   1.1    matt 		return(0);
   3392   1.1    matt 
   3393   1.1    matt 	/* This can happen if user code tries to access kernel memory. */
   3394   1.1    matt 	if ((*pte & L2_MASK) != L2_INVAL)
   3395   1.1    matt 		return (0);
   3396   1.1    matt 
   3397   1.1    matt 	/* Extract the physical address of the page */
   3398   1.1    matt 	pa = pmap_pte_pa(pte);
   3399   1.1    matt 	if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
   3400   1.1    matt 		return(0);
   3401   1.1    matt 
   3402   1.1    matt 	/*
   3403   1.1    matt 	 * Ok we just enable the pte and mark the attibs as handled
   3404   1.1    matt 	 */
   3405   1.1    matt 	PDEBUG(0, printf("pmap_handled_emulation: Got a hit va=%08lx pte = %p (%08x)\n",
   3406   1.1    matt 	    va, pte, *pte));
   3407   1.1    matt 	vm_physmem[bank].pmseg.attrs[off] |= PT_H;
   3408   1.1    matt 	*pte = (*pte & ~L2_MASK) | L2_SPAGE;
   3409   1.1    matt 	PDEBUG(0, printf("->(%08x)\n", *pte));
   3410   1.1    matt 
   3411   1.1    matt 	/* Return, indicating the problem has been dealt with */
   3412   1.1    matt 	cpu_tlb_flushID_SE(va);
   3413   1.1    matt 	return(1);
   3414   1.1    matt }
   3415   1.1    matt 
   3416  1.17   chris 
   3417  1.17   chris 
   3418  1.17   chris 
   3419   1.1    matt /*
   3420   1.1    matt  * pmap_collect: free resources held by a pmap
   3421   1.1    matt  *
   3422   1.1    matt  * => optional function.
   3423   1.1    matt  * => called when a process is swapped out to free memory.
   3424   1.1    matt  */
   3425   1.1    matt 
   3426   1.1    matt void
   3427   1.1    matt pmap_collect(pmap)
   3428  1.15   chris 	struct pmap *pmap;
   3429   1.1    matt {
   3430   1.1    matt }
   3431   1.1    matt 
   3432   1.1    matt /*
   3433   1.1    matt  * Routine:	pmap_procwr
   3434   1.1    matt  *
   3435   1.1    matt  * Function:
   3436   1.1    matt  *	Synchronize caches corresponding to [addr, addr+len) in p.
   3437   1.1    matt  *
   3438   1.1    matt  */
   3439   1.1    matt void
   3440   1.1    matt pmap_procwr(p, va, len)
   3441   1.1    matt 	struct proc	*p;
   3442   1.1    matt 	vaddr_t		va;
   3443   1.3    matt 	int		len;
   3444   1.1    matt {
   3445   1.1    matt 	/* We only need to do anything if it is the current process. */
   3446   1.1    matt 	if (p == curproc)
   3447   1.1    matt 		cpu_cache_syncI_rng(va, len);
   3448  1.17   chris }
   3449  1.17   chris /*
   3450  1.17   chris  * PTP functions
   3451  1.17   chris  */
   3452  1.17   chris 
   3453  1.17   chris /*
   3454  1.17   chris  * pmap_steal_ptp: Steal a PTP from somewhere else.
   3455  1.17   chris  *
   3456  1.17   chris  * This is just a placeholder, for now we never steal.
   3457  1.17   chris  */
   3458  1.17   chris 
   3459  1.17   chris static struct vm_page *
   3460  1.17   chris pmap_steal_ptp(struct pmap *pmap, vaddr_t va)
   3461  1.17   chris {
   3462  1.17   chris     return (NULL);
   3463  1.17   chris }
   3464  1.17   chris 
   3465  1.17   chris /*
   3466  1.17   chris  * pmap_get_ptp: get a PTP (if there isn't one, allocate a new one)
   3467  1.17   chris  *
   3468  1.17   chris  * => pmap should NOT be pmap_kernel()
   3469  1.17   chris  * => pmap should be locked
   3470  1.17   chris  */
   3471  1.17   chris 
   3472  1.17   chris static struct vm_page *
   3473  1.17   chris pmap_get_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
   3474  1.17   chris {
   3475  1.17   chris     struct vm_page *ptp;
   3476  1.17   chris 
   3477  1.17   chris     if (pmap_pde_v(pmap_pde(pmap, va))) {
   3478  1.17   chris 
   3479  1.17   chris 	/* valid... check hint (saves us a PA->PG lookup) */
   3480  1.17   chris #if 0
   3481  1.17   chris 	if (pmap->pm_ptphint &&
   3482  1.17   chris     		((unsigned)pmap_pde(pmap, va) & PG_FRAME) ==
   3483  1.17   chris 		VM_PAGE_TO_PHYS(pmap->pm_ptphint))
   3484  1.17   chris 	    return (pmap->pm_ptphint);
   3485  1.17   chris #endif
   3486  1.17   chris 	ptp = uvm_pagelookup(&pmap->pm_obj, va);
   3487  1.17   chris #ifdef DIAGNOSTIC
   3488  1.17   chris 	if (ptp == NULL)
   3489  1.17   chris     	    panic("pmap_get_ptp: unmanaged user PTP");
   3490  1.17   chris #endif
   3491  1.17   chris //	pmap->pm_ptphint = ptp;
   3492  1.17   chris 	return(ptp);
   3493  1.17   chris     }
   3494  1.17   chris 
   3495  1.17   chris     /* allocate a new PTP (updates ptphint) */
   3496  1.17   chris     return(pmap_alloc_ptp(pmap, va, just_try));
   3497  1.17   chris }
   3498  1.17   chris 
   3499  1.17   chris /*
   3500  1.17   chris  * pmap_alloc_ptp: allocate a PTP for a PMAP
   3501  1.17   chris  *
   3502  1.17   chris  * => pmap should already be locked by caller
   3503  1.17   chris  * => we use the ptp's wire_count to count the number of active mappings
   3504  1.17   chris  *	in the PTP (we start it at one to prevent any chance this PTP
   3505  1.17   chris  *	will ever leak onto the active/inactive queues)
   3506  1.17   chris  */
   3507  1.17   chris 
   3508  1.17   chris /*__inline */ static struct vm_page *
   3509  1.17   chris pmap_alloc_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
   3510  1.17   chris {
   3511  1.17   chris 	struct vm_page *ptp;
   3512  1.17   chris 
   3513  1.17   chris 	ptp = uvm_pagealloc(&pmap->pm_obj, va, NULL,
   3514  1.17   chris 		UVM_PGA_USERESERVE|UVM_PGA_ZERO);
   3515  1.17   chris 	if (ptp == NULL) {
   3516  1.17   chris 	    if (just_try)
   3517  1.17   chris 		return (NULL);
   3518  1.17   chris 
   3519  1.17   chris 	    ptp = pmap_steal_ptp(pmap, va);
   3520  1.17   chris 
   3521  1.17   chris 	    if (ptp == NULL)
   3522  1.17   chris 		return (NULL);
   3523  1.17   chris 	    /* Stole a page, zero it.  */
   3524  1.17   chris 	    pmap_zero_page(VM_PAGE_TO_PHYS(ptp));
   3525  1.17   chris 	}
   3526  1.17   chris 
   3527  1.17   chris 	/* got one! */
   3528  1.17   chris 	ptp->flags &= ~PG_BUSY;	/* never busy */
   3529  1.17   chris 	ptp->wire_count = 1;	/* no mappings yet */
   3530  1.17   chris 	pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(ptp), TRUE);
   3531  1.17   chris 	pmap->pm_stats.resident_count++;	/* count PTP as resident */
   3532  1.17   chris //	pmap->pm_ptphint = ptp;
   3533  1.17   chris 	return (ptp);
   3534   1.1    matt }
   3535   1.1    matt 
   3536   1.1    matt /* End of pmap.c */
   3537