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