Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.136
      1 /*	$NetBSD: pmap.c,v 1.136 2003/06/15 18:18:16 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 2002-2003 Wasabi Systems, Inc.
     40  * Copyright (c) 2001 Richard Earnshaw
     41  * Copyright (c) 2001-2002 Christopher Gilbert
     42  * All rights reserved.
     43  *
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. The name of the company nor the name of the author may be used to
     50  *    endorse or promote products derived from this software without specific
     51  *    prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  */
     65 
     66 /*-
     67  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     68  * All rights reserved.
     69  *
     70  * This code is derived from software contributed to The NetBSD Foundation
     71  * by Charles M. Hannum.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *        This product includes software developed by the NetBSD
     84  *        Foundation, Inc. and its contributors.
     85  * 4. Neither the name of The NetBSD Foundation nor the names of its
     86  *    contributors may be used to endorse or promote products derived
     87  *    from this software without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     90  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     91  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     92  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     93  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     94  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     95  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     96  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     97  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     98  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     99  * POSSIBILITY OF SUCH DAMAGE.
    100  */
    101 
    102 /*
    103  * Copyright (c) 1994-1998 Mark Brinicombe.
    104  * Copyright (c) 1994 Brini.
    105  * All rights reserved.
    106  *
    107  * This code is derived from software written for Brini by Mark Brinicombe
    108  *
    109  * Redistribution and use in source and binary forms, with or without
    110  * modification, are permitted provided that the following conditions
    111  * are met:
    112  * 1. Redistributions of source code must retain the above copyright
    113  *    notice, this list of conditions and the following disclaimer.
    114  * 2. Redistributions in binary form must reproduce the above copyright
    115  *    notice, this list of conditions and the following disclaimer in the
    116  *    documentation and/or other materials provided with the distribution.
    117  * 3. All advertising materials mentioning features or use of this software
    118  *    must display the following acknowledgement:
    119  *	This product includes software developed by Mark Brinicombe.
    120  * 4. The name of the author may not be used to endorse or promote products
    121  *    derived from this software without specific prior written permission.
    122  *
    123  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    124  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    125  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    126  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    127  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    128  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    129  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    130  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    131  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    132  *
    133  * RiscBSD kernel project
    134  *
    135  * pmap.c
    136  *
    137  * Machine dependant vm stuff
    138  *
    139  * Created      : 20/09/94
    140  */
    141 
    142 /*
    143  * Performance improvements, UVM changes, overhauls and part-rewrites
    144  * were contributed by Neil A. Carson <neil (at) causality.com>.
    145  */
    146 
    147 /*
    148  * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables
    149  * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
    150  * Systems, Inc.
    151  *
    152  * There are still a few things outstanding at this time:
    153  *
    154  *   - There are some unresolved issues for MP systems:
    155  *
    156  *     o The L1 metadata needs a lock, or more specifically, some places
    157  *       need to acquire an exclusive lock when modifying L1 translation
    158  *       table entries.
    159  *
    160  *     o When one cpu modifies an L1 entry, and that L1 table is also
    161  *       being used by another cpu, then the latter will need to be told
    162  *       that a tlb invalidation may be necessary. (But only if the old
    163  *       domain number in the L1 entry being over-written is currently
    164  *       the active domain on that cpu). I guess there are lots more tlb
    165  *       shootdown issues too...
    166  *
    167  *     o If the vector_page is at 0x00000000 instead of 0xffff0000, then
    168  *       MP systems will lose big-time because of the MMU domain hack.
    169  *       The only way this can be solved (apart from moving the vector
    170  *       page to 0xffff0000) is to reserve the first 1MB of user address
    171  *       space for kernel use only. This would require re-linking all
    172  *       applications so that the text section starts above this 1MB
    173  *       boundary.
    174  *
    175  *     o Tracking which VM space is resident in the cache/tlb has not yet
    176  *       been implemented for MP systems.
    177  *
    178  *     o Finally, there is a pathological condition where two cpus running
    179  *       two separate processes (not lwps) which happen to share an L1
    180  *       can get into a fight over one or more L1 entries. This will result
    181  *       in a significant slow-down if both processes are in tight loops.
    182  */
    183 
    184 /*
    185  * Special compilation symbols
    186  * PMAP_DEBUG		- Build in pmap_debug_level code
    187  */
    188 
    189 /* Include header files */
    190 
    191 #include "opt_cpuoptions.h"
    192 #include "opt_pmap_debug.h"
    193 #include "opt_ddb.h"
    194 
    195 #include <sys/types.h>
    196 #include <sys/param.h>
    197 #include <sys/kernel.h>
    198 #include <sys/systm.h>
    199 #include <sys/proc.h>
    200 #include <sys/malloc.h>
    201 #include <sys/user.h>
    202 #include <sys/pool.h>
    203 #include <sys/cdefs.h>
    204 
    205 #include <uvm/uvm.h>
    206 
    207 #include <machine/bus.h>
    208 #include <machine/pmap.h>
    209 #include <machine/pcb.h>
    210 #include <machine/param.h>
    211 #include <arm/arm32/katelib.h>
    212 
    213 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.136 2003/06/15 18:18:16 thorpej Exp $");
    214 
    215 #ifdef PMAP_DEBUG
    216 #define	PDEBUG(_lev_,_stat_) \
    217 	if (pmap_debug_level >= (_lev_)) \
    218         	((_stat_))
    219 int pmap_debug_level = 0;
    220 
    221 /*
    222  * for switching to potentially finer grained debugging
    223  */
    224 #define	PDB_FOLLOW	0x0001
    225 #define	PDB_INIT	0x0002
    226 #define	PDB_ENTER	0x0004
    227 #define	PDB_REMOVE	0x0008
    228 #define	PDB_CREATE	0x0010
    229 #define	PDB_PTPAGE	0x0020
    230 #define	PDB_GROWKERN	0x0040
    231 #define	PDB_BITS	0x0080
    232 #define	PDB_COLLECT	0x0100
    233 #define	PDB_PROTECT	0x0200
    234 #define	PDB_MAP_L1	0x0400
    235 #define	PDB_BOOTSTRAP	0x1000
    236 #define	PDB_PARANOIA	0x2000
    237 #define	PDB_WIRING	0x4000
    238 #define	PDB_PVDUMP	0x8000
    239 #define	PDB_VAC		0x10000
    240 #define	PDB_KENTER	0x20000
    241 #define	PDB_KREMOVE	0x40000
    242 
    243 int debugmap = 1;
    244 int pmapdebug = 0;
    245 #define	NPDEBUG(_lev_,_stat_) \
    246 	if (pmapdebug & (_lev_)) \
    247         	((_stat_))
    248 
    249 #else	/* PMAP_DEBUG */
    250 #define	PDEBUG(_lev_,_stat_) /* Nothing */
    251 #define NPDEBUG(_lev_,_stat_) /* Nothing */
    252 #endif	/* PMAP_DEBUG */
    253 
    254 /*
    255  * pmap_kernel() points here
    256  */
    257 struct pmap     kernel_pmap_store;
    258 
    259 /*
    260  * Which pmap is currently 'live' in the cache
    261  *
    262  * XXXSCW: Fix for SMP ...
    263  */
    264 union pmap_cache_state *pmap_cache_state;
    265 
    266 /*
    267  * Pool and cache that pmap structures are allocated from.
    268  * We use a cache to avoid clearing the pm_l2[] array (1KB)
    269  * in pmap_create().
    270  */
    271 static struct pool pmap_pmap_pool;
    272 static struct pool_cache pmap_pmap_cache;
    273 static LIST_HEAD(, pmap) pmap_pmaps;
    274 
    275 /*
    276  * Pool of PV structures
    277  */
    278 static struct pool pmap_pv_pool;
    279 static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
    280 static void pmap_bootstrap_pv_page_free(struct pool *, void *);
    281 static struct pool_allocator pmap_bootstrap_pv_allocator = {
    282 	pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
    283 };
    284 
    285 /*
    286  * Pool and cache of l2_dtable structures.
    287  * We use a cache to avoid clearing the structures when they're
    288  * allocated. (196 bytes)
    289  */
    290 static struct pool pmap_l2dtable_pool;
    291 static struct pool_cache pmap_l2dtable_cache;
    292 static vaddr_t pmap_kernel_l2dtable_kva;
    293 
    294 /*
    295  * Pool and cache of L2 page descriptors.
    296  * We use a cache to avoid clearing the descriptor table
    297  * when they're allocated. (1KB)
    298  */
    299 static struct pool pmap_l2ptp_pool;
    300 static struct pool_cache pmap_l2ptp_cache;
    301 static vaddr_t pmap_kernel_l2ptp_kva;
    302 static paddr_t pmap_kernel_l2ptp_phys;
    303 
    304 /*
    305  * pmap copy/zero page, and mem(5) hook point
    306  */
    307 static pt_entry_t *csrc_pte, *cdst_pte;
    308 static vaddr_t csrcp, cdstp;
    309 char *memhook;
    310 extern caddr_t msgbufaddr;
    311 
    312 /*
    313  * Flag to indicate if pmap_init() has done its thing
    314  */
    315 boolean_t pmap_initialized;
    316 
    317 /*
    318  * Misc. locking data structures
    319  */
    320 
    321 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    322 static struct lock pmap_main_lock;
    323 
    324 #define PMAP_MAP_TO_HEAD_LOCK() \
    325      (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
    326 #define PMAP_MAP_TO_HEAD_UNLOCK() \
    327      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    328 #define PMAP_HEAD_TO_MAP_LOCK() \
    329      (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
    330 #define PMAP_HEAD_TO_MAP_UNLOCK() \
    331      spinlockmgr(&pmap_main_lock, LK_RELEASE, (void *) 0)
    332 #else
    333 #define PMAP_MAP_TO_HEAD_LOCK()		/* null */
    334 #define PMAP_MAP_TO_HEAD_UNLOCK()	/* null */
    335 #define PMAP_HEAD_TO_MAP_LOCK()		/* null */
    336 #define PMAP_HEAD_TO_MAP_UNLOCK()	/* null */
    337 #endif
    338 
    339 #define	pmap_acquire_pmap_lock(pm)			\
    340 	do {						\
    341 		if ((pm) != pmap_kernel())		\
    342 			simple_lock(&(pm)->pm_lock);	\
    343 	} while (/*CONSTCOND*/0)
    344 
    345 #define	pmap_release_pmap_lock(pm)			\
    346 	do {						\
    347 		if ((pm) != pmap_kernel())		\
    348 			simple_unlock(&(pm)->pm_lock);	\
    349 	} while (/*CONSTCOND*/0)
    350 
    351 
    352 /*
    353  * Metadata for L1 translation tables.
    354  */
    355 struct l1_ttable {
    356 	/* Entry on the L1 Table list */
    357 	SLIST_ENTRY(l1_ttable) l1_link;
    358 
    359 	/* Entry on the L1 Least Recently Used list */
    360 	TAILQ_ENTRY(l1_ttable) l1_lru;
    361 
    362 	/* Track how many domains are allocated from this L1 */
    363 	volatile u_int l1_domain_use_count;
    364 
    365 	/*
    366 	 * A free-list of domain numbers for this L1.
    367 	 * We avoid using ffs() and a bitmap to track domains since ffs()
    368 	 * is slow on ARM.
    369 	 */
    370 	u_int8_t l1_domain_first;
    371 	u_int8_t l1_domain_free[PMAP_DOMAINS];
    372 
    373 	/* Physical address of this L1 page table */
    374 	paddr_t l1_physaddr;
    375 
    376 	/* KVA of this L1 page table */
    377 	pd_entry_t *l1_kva;
    378 };
    379 
    380 /*
    381  * Convert a virtual address into its L1 table index. That is, the
    382  * index used to locate the L2 descriptor table pointer in an L1 table.
    383  * This is basically used to index l1->l1_kva[].
    384  *
    385  * Each L2 descriptor table represents 1MB of VA space.
    386  */
    387 #define	L1_IDX(va)		(((vaddr_t)(va)) >> L1_S_SHIFT)
    388 
    389 /*
    390  * L1 Page Tables are tracked using a Least Recently Used list.
    391  *  - New L1s are allocated from the HEAD.
    392  *  - Freed L1s are added to the TAIl.
    393  *  - Recently accessed L1s (where an 'access' is some change to one of
    394  *    the userland pmaps which owns this L1) are moved to the TAIL.
    395  */
    396 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
    397 static struct simplelock l1_lru_lock;
    398 
    399 /*
    400  * A list of all L1 tables
    401  */
    402 static SLIST_HEAD(, l1_ttable) l1_list;
    403 
    404 /*
    405  * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
    406  *
    407  * This is normally 16MB worth L2 page descriptors for any given pmap.
    408  * Reference counts are maintained for L2 descriptors so they can be
    409  * freed when empty.
    410  */
    411 struct l2_dtable {
    412 	/* The number of L2 page descriptors allocated to this l2_dtable */
    413 	u_int l2_occupancy;
    414 
    415 	/* List of L2 page descriptors */
    416 	struct l2_bucket {
    417 		pt_entry_t *l2b_kva;	/* KVA of L2 Descriptor Table */
    418 		paddr_t l2b_phys;	/* Physical address of same */
    419 		u_short l2b_l1idx;	/* This L2 table's L1 index */
    420 		u_short l2b_occupancy;	/* How many active descriptors */
    421 	} l2_bucket[L2_BUCKET_SIZE];
    422 };
    423 
    424 /*
    425  * Given an L1 table index, calculate the corresponding l2_dtable index
    426  * and bucket index within the l2_dtable.
    427  */
    428 #define	L2_IDX(l1idx)		(((l1idx) >> L2_BUCKET_LOG2) & \
    429 				 (L2_SIZE - 1))
    430 #define	L2_BUCKET(l1idx)	((l1idx) & (L2_BUCKET_SIZE - 1))
    431 
    432 /*
    433  * Given a virtual address, this macro returns the
    434  * virtual address required to drop into the next L2 bucket.
    435  */
    436 #define	L2_NEXT_BUCKET(va)	(((va) & L1_S_FRAME) + L1_S_SIZE)
    437 
    438 /*
    439  * L2 allocation.
    440  */
    441 #define	pmap_alloc_l2_dtable()		\
    442 	    pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
    443 #define	pmap_free_l2_dtable(l2)		\
    444 	    pool_cache_put(&pmap_l2dtable_cache, (l2))
    445 #define pmap_alloc_l2_ptp(pap)		\
    446 	    ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
    447 	    PR_NOWAIT, (pap)))
    448 
    449 /*
    450  * We try to map the page tables write-through, if possible.  However, not
    451  * all CPUs have a write-through cache mode, so on those we have to sync
    452  * the cache when we frob page tables.
    453  *
    454  * We try to evaluate this at compile time, if possible.  However, it's
    455  * not always possible to do that, hence this run-time var.
    456  */
    457 int	pmap_needs_pte_sync;
    458 
    459 /*
    460  * Real definition of pv_entry.
    461  */
    462 struct pv_entry {
    463 	struct pv_entry *pv_next;       /* next pv_entry */
    464 	pmap_t		pv_pmap;        /* pmap where mapping lies */
    465 	vaddr_t		pv_va;          /* virtual address for mapping */
    466 	u_int		pv_flags;       /* flags */
    467 };
    468 
    469 /*
    470  * Macro to determine if a mapping might be resident in the
    471  * instruction cache and/or TLB
    472  */
    473 #define	PV_BEEN_EXECD(f)  (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
    474 
    475 /*
    476  * Macro to determine if a mapping might be resident in the
    477  * data cache and/or TLB
    478  */
    479 #define	PV_BEEN_REFD(f)   (((f) & PVF_REF) != 0)
    480 
    481 /*
    482  * Local prototypes
    483  */
    484 static int		pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t);
    485 static void		pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
    486 			    pt_entry_t **);
    487 static boolean_t	pmap_is_current(pmap_t);
    488 static boolean_t	pmap_is_cached(pmap_t);
    489 static void		pmap_enter_pv(struct vm_page *, struct pv_entry *,
    490 			    pmap_t, vaddr_t, u_int);
    491 static struct pv_entry *pmap_find_pv(struct vm_page *, pmap_t, vaddr_t);
    492 static struct pv_entry *pmap_remove_pv(struct vm_page *, pmap_t, vaddr_t);
    493 static u_int		pmap_modify_pv(struct vm_page *, pmap_t, vaddr_t,
    494 			    u_int, u_int);
    495 
    496 static void		pmap_pinit(pmap_t);
    497 static int		pmap_pmap_ctor(void *, void *, int);
    498 
    499 static void		pmap_alloc_l1(pmap_t);
    500 static void		pmap_free_l1(pmap_t);
    501 static void		pmap_use_l1(pmap_t);
    502 
    503 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
    504 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
    505 static void		pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
    506 static int		pmap_l2ptp_ctor(void *, void *, int);
    507 static int		pmap_l2dtable_ctor(void *, void *, int);
    508 
    509 static void		pmap_vac_me_harder(struct vm_page *, pmap_t, vaddr_t);
    510 static void		pmap_vac_me_kpmap(struct vm_page *, pmap_t, vaddr_t);
    511 static void		pmap_vac_me_user(struct vm_page *, pmap_t, vaddr_t);
    512 
    513 static void		pmap_clearbit(struct vm_page *, u_int);
    514 static int		pmap_clean_page(struct pv_entry *, boolean_t);
    515 static void		pmap_page_remove(struct vm_page *);
    516 
    517 static void		pmap_init_l1(struct l1_ttable *, pd_entry_t *);
    518 static vaddr_t		kernel_pt_lookup(paddr_t);
    519 
    520 
    521 /*
    522  * External function prototypes
    523  */
    524 extern void bzero_page(vaddr_t);
    525 extern void bcopy_page(vaddr_t, vaddr_t);
    526 
    527 /*
    528  * Misc variables
    529  */
    530 vaddr_t virtual_avail;
    531 vaddr_t virtual_end;
    532 vaddr_t pmap_curmaxkvaddr;
    533 
    534 vaddr_t avail_start;
    535 vaddr_t avail_end;
    536 
    537 extern pv_addr_t systempage;
    538 
    539 /* Function to set the debug level of the pmap code */
    540 
    541 #ifdef PMAP_DEBUG
    542 void
    543 pmap_debug(int level)
    544 {
    545 	pmap_debug_level = level;
    546 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    547 }
    548 #endif	/* PMAP_DEBUG */
    549 
    550 /*
    551  * A bunch of routines to conditionally flush the caches/TLB depending
    552  * on whether the specified pmap actually needs to be flushed at any
    553  * given time.
    554  */
    555 static __inline void
    556 pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va)
    557 {
    558 
    559 	if (pm->pm_cstate.cs_tlb_id)
    560 		cpu_tlb_flushID_SE(va);
    561 }
    562 
    563 static __inline void
    564 pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va)
    565 {
    566 
    567 	if (pm->pm_cstate.cs_tlb_d)
    568 		cpu_tlb_flushD_SE(va);
    569 }
    570 
    571 static __inline void
    572 pmap_tlb_flushID(pmap_t pm)
    573 {
    574 
    575 	if (pm->pm_cstate.cs_tlb_id) {
    576 		cpu_tlb_flushID();
    577 		pm->pm_cstate.cs_tlb = 0;
    578 	}
    579 }
    580 
    581 static __inline void
    582 pmap_tlb_flushD(pmap_t pm)
    583 {
    584 
    585 	if (pm->pm_cstate.cs_tlb_d) {
    586 		cpu_tlb_flushD();
    587 		pm->pm_cstate.cs_tlb_d = 0;
    588 	}
    589 }
    590 
    591 static __inline void
    592 pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len)
    593 {
    594 
    595 	if (pm->pm_cstate.cs_cache_id)
    596 		cpu_idcache_wbinv_range(va, len);
    597 }
    598 
    599 static __inline void
    600 pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len,
    601     boolean_t do_inv, boolean_t rd_only)
    602 {
    603 
    604 	if (pm->pm_cstate.cs_cache_d) {
    605 		if (do_inv) {
    606 			if (rd_only)
    607 				cpu_dcache_inv_range(va, len);
    608 			else
    609 				cpu_dcache_wbinv_range(va, len);
    610 		} else
    611 		if (!rd_only)
    612 			cpu_dcache_wb_range(va, len);
    613 	}
    614 }
    615 
    616 static __inline void
    617 pmap_idcache_wbinv_all(pmap_t pm)
    618 {
    619 
    620 	if (pm->pm_cstate.cs_cache_id) {
    621 		cpu_idcache_wbinv_all();
    622 		pm->pm_cstate.cs_cache = 0;
    623 	}
    624 }
    625 
    626 static __inline void
    627 pmap_dcache_wbinv_all(pmap_t pm)
    628 {
    629 
    630 	if (pm->pm_cstate.cs_cache_d) {
    631 		cpu_dcache_wbinv_all();
    632 		pm->pm_cstate.cs_cache_d = 0;
    633 	}
    634 }
    635 
    636 static __inline boolean_t
    637 pmap_is_current(pmap_t pm)
    638 {
    639 
    640 	if (pm == pmap_kernel() ||
    641 	    (curproc && curproc->p_vmspace->vm_map.pmap == pm))
    642 		return (TRUE);
    643 
    644 	return (FALSE);
    645 }
    646 
    647 static __inline boolean_t
    648 pmap_is_cached(pmap_t pm)
    649 {
    650 
    651 	if (pm == pmap_kernel() || pmap_cache_state == NULL ||
    652 	   pmap_cache_state == &pm->pm_cstate)
    653 		return (TRUE);
    654 
    655 	return (FALSE);
    656 }
    657 
    658 /*
    659  * PTE_SYNC_CURRENT:
    660  *
    661  *     Make sure the pte is written out to RAM.
    662  *     We need to do this for one of two cases:
    663  *       - We're dealing with the kernel pmap
    664  *       - There is no pmap active in the cache/tlb.
    665  *       - The specified pmap is 'active' in the cache/tlb.
    666  */
    667 #ifdef PMAP_INCLUDE_PTE_SYNC
    668 #define	PTE_SYNC_CURRENT(pm, ptep)	\
    669 do {					\
    670 	if (PMAP_NEEDS_PTE_SYNC && 	\
    671 	    pmap_is_cached(pm))		\
    672 		PTE_SYNC(ptep);		\
    673 } while (/*CONSTCOND*/0)
    674 #else
    675 #define	PTE_SYNC_CURRENT(pm, ptep)	/* nothing */
    676 #endif
    677 
    678 /*
    679  * main pv_entry manipulation functions:
    680  *   pmap_enter_pv: enter a mapping onto a vm_page list
    681  *   pmap_remove_pv: remove a mappiing from a vm_page list
    682  *
    683  * NOTE: pmap_enter_pv expects to lock the pvh itself
    684  *       pmap_remove_pv expects te caller to lock the pvh before calling
    685  */
    686 
    687 /*
    688  * pmap_enter_pv: enter a mapping onto a vm_page lst
    689  *
    690  * => caller should hold the proper lock on pmap_main_lock
    691  * => caller should have pmap locked
    692  * => we will gain the lock on the vm_page and allocate the new pv_entry
    693  * => caller should adjust ptp's wire_count before calling
    694  * => caller should not adjust pmap's wire_count
    695  */
    696 static void
    697 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
    698     vaddr_t va, u_int flags)
    699 {
    700 
    701 	NPDEBUG(PDB_PVDUMP,
    702 	    printf("pmap_enter_pv: pm %p, pg %p, flags 0x%x\n", pm, pg, flags));
    703 
    704 	pve->pv_pmap = pm;
    705 	pve->pv_va = va;
    706 	pve->pv_flags = flags;
    707 
    708 	simple_lock(&pg->mdpage.pvh_slock);	/* lock vm_page */
    709 	pve->pv_next = pg->mdpage.pvh_list;	/* add to ... */
    710 	pg->mdpage.pvh_list = pve;		/* ... locked list */
    711 	pg->mdpage.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
    712 	if (pm == pmap_kernel()) {
    713 		if (flags & PVF_WRITE)
    714 			pg->mdpage.krw_mappings++;
    715 		else
    716 			pg->mdpage.kro_mappings++;
    717 	} else
    718 	if (flags & PVF_WRITE)
    719 		pg->mdpage.urw_mappings++;
    720 	else
    721 		pg->mdpage.uro_mappings++;
    722 	simple_unlock(&pg->mdpage.pvh_slock);	/* unlock, done! */
    723 
    724 	if (pve->pv_flags & PVF_WIRED)
    725 		++pm->pm_stats.wired_count;
    726 }
    727 
    728 /*
    729  *
    730  * pmap_find_pv: Find a pv entry
    731  *
    732  * => caller should hold lock on vm_page
    733  */
    734 static __inline struct pv_entry *
    735 pmap_find_pv(struct vm_page *pg, pmap_t pm, vaddr_t va)
    736 {
    737 	struct pv_entry *pv;
    738 
    739 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
    740 		if (pm == pv->pv_pmap && va == pv->pv_va)
    741 			break;
    742 	}
    743 
    744 	return (pv);
    745 }
    746 
    747 /*
    748  * pmap_remove_pv: try to remove a mapping from a pv_list
    749  *
    750  * => caller should hold proper lock on pmap_main_lock
    751  * => pmap should be locked
    752  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    753  * => caller should adjust ptp's wire_count and free PTP if needed
    754  * => caller should NOT adjust pmap's wire_count
    755  * => we return the removed pve
    756  */
    757 static struct pv_entry *
    758 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vaddr_t va)
    759 {
    760 	struct pv_entry *pve, **prevptr;
    761 
    762 	NPDEBUG(PDB_PVDUMP,
    763 	    printf("pmap_remove_pv: pm %p, pg %p, va 0x%08lx\n", pm, pg, va));
    764 
    765 	prevptr = &pg->mdpage.pvh_list;		/* previous pv_entry pointer */
    766 	pve = *prevptr;
    767 
    768 	while (pve) {
    769 		if (pve->pv_pmap == pm && pve->pv_va == va) {	/* match? */
    770 			NPDEBUG(PDB_PVDUMP,
    771 			    printf("pmap_remove_pv: pm %p, pg %p, flags 0x%x\n", pm, pg, pve->pv_flags));
    772 			*prevptr = pve->pv_next;		/* remove it! */
    773 			if (pve->pv_flags & PVF_WIRED)
    774 			    --pm->pm_stats.wired_count;
    775 			if (pm == pmap_kernel()) {
    776 				if (pve->pv_flags & PVF_WRITE)
    777 					pg->mdpage.krw_mappings--;
    778 				else
    779 					pg->mdpage.kro_mappings--;
    780 			} else
    781 			if (pve->pv_flags & PVF_WRITE)
    782 				pg->mdpage.urw_mappings--;
    783 			else
    784 				pg->mdpage.uro_mappings--;
    785 			break;
    786 		}
    787 		prevptr = &pve->pv_next;		/* previous pointer */
    788 		pve = pve->pv_next;			/* advance */
    789 	}
    790 
    791 	return(pve);				/* return removed pve */
    792 }
    793 
    794 /*
    795  *
    796  * pmap_modify_pv: Update pv flags
    797  *
    798  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    799  * => caller should NOT adjust pmap's wire_count
    800  * => caller must call pmap_vac_me_harder() if writable status of a page
    801  *    may have changed.
    802  * => we return the old flags
    803  *
    804  * Modify a physical-virtual mapping in the pv table
    805  */
    806 static u_int
    807 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vaddr_t va,
    808     u_int clr_mask, u_int set_mask)
    809 {
    810 	struct pv_entry *npv;
    811 	u_int flags, oflags;
    812 
    813 	if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
    814 		return (0);
    815 
    816 	NPDEBUG(PDB_PVDUMP,
    817 	    printf("pmap_modify_pv: pm %p, pg %p, clr 0x%x, set 0x%x, flags 0x%x\n", pm, pg, clr_mask, set_mask, npv->pv_flags));
    818 
    819 	/*
    820 	 * There is at least one VA mapping this page.
    821 	 */
    822 
    823 	if (clr_mask & (PVF_REF | PVF_MOD))
    824 		pg->mdpage.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
    825 
    826 	oflags = npv->pv_flags;
    827 	npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
    828 
    829 	if ((flags ^ oflags) & PVF_WIRED) {
    830 		if (flags & PVF_WIRED)
    831 			++pm->pm_stats.wired_count;
    832 		else
    833 			--pm->pm_stats.wired_count;
    834 	}
    835 
    836 	if ((flags ^ oflags) & PVF_WRITE) {
    837 		if (pm == pmap_kernel()) {
    838 			if (flags & PVF_WRITE) {
    839 				pg->mdpage.krw_mappings++;
    840 				pg->mdpage.kro_mappings--;
    841 			} else {
    842 				pg->mdpage.kro_mappings++;
    843 				pg->mdpage.krw_mappings--;
    844 			}
    845 		} else
    846 		if (flags & PVF_WRITE) {
    847 			pg->mdpage.urw_mappings++;
    848 			pg->mdpage.uro_mappings--;
    849 		} else {
    850 			pg->mdpage.uro_mappings++;
    851 			pg->mdpage.urw_mappings--;
    852 		}
    853 	}
    854 
    855 	return (oflags);
    856 }
    857 
    858 static void
    859 pmap_pinit(pmap_t pm)
    860 {
    861 
    862 	if (vector_page < KERNEL_BASE) {
    863 		/*
    864 		 * Map the vector page.
    865 		 */
    866 		pmap_enter(pm, vector_page, systempage.pv_pa,
    867 		    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
    868 		pmap_update(pm);
    869 	}
    870 }
    871 
    872 /*
    873  * Allocate an L1 translation table for the specified pmap.
    874  * This is called at pmap creation time.
    875  */
    876 static void
    877 pmap_alloc_l1(pmap_t pm)
    878 {
    879 	struct l1_ttable *l1;
    880 	u_int8_t domain;
    881 
    882 	/*
    883 	 * Remove the L1 at the head of the LRU list
    884 	 */
    885 	simple_lock(&l1_lru_lock);
    886 	l1 = TAILQ_FIRST(&l1_lru_list);
    887 	KDASSERT(l1 != NULL);
    888 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
    889 
    890 	/*
    891 	 * Pick the first available domain number, and update
    892 	 * the link to the next number.
    893 	 */
    894 	domain = l1->l1_domain_first;
    895 	l1->l1_domain_first = l1->l1_domain_free[domain];
    896 
    897 	/*
    898 	 * If there are still free domain numbers in this L1,
    899 	 * put it back on the TAIL of the LRU list.
    900 	 */
    901 	if (++l1->l1_domain_use_count < PMAP_DOMAINS)
    902 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
    903 
    904 	simple_unlock(&l1_lru_lock);
    905 
    906 	/*
    907 	 * Fix up the relevant bits in the pmap structure
    908 	 */
    909 	pm->pm_l1 = l1;
    910 	pm->pm_domain = domain;
    911 }
    912 
    913 /*
    914  * Free an L1 translation table.
    915  * This is called at pmap destruction time.
    916  */
    917 static void
    918 pmap_free_l1(pmap_t pm)
    919 {
    920 	struct l1_ttable *l1 = pm->pm_l1;
    921 
    922 	simple_lock(&l1_lru_lock);
    923 
    924 	/*
    925 	 * If this L1 is currently on the LRU list, remove it.
    926 	 */
    927 	if (l1->l1_domain_use_count < PMAP_DOMAINS)
    928 		TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
    929 
    930 	/*
    931 	 * Free up the domain number which was allocated to the pmap
    932 	 */
    933 	l1->l1_domain_free[pm->pm_domain] = l1->l1_domain_first;
    934 	l1->l1_domain_first = pm->pm_domain;
    935 	l1->l1_domain_use_count--;
    936 
    937 	/*
    938 	 * The L1 now must have at least 1 free domain, so add
    939 	 * it back to the LRU list. If the use count is zero,
    940 	 * put it at the head of the list, otherwise it goes
    941 	 * to the tail.
    942 	 */
    943 	if (l1->l1_domain_use_count == 0)
    944 		TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
    945 	else
    946 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
    947 
    948 	simple_unlock(&l1_lru_lock);
    949 }
    950 
    951 static __inline void
    952 pmap_use_l1(pmap_t pm)
    953 {
    954 	struct l1_ttable *l1;
    955 
    956 	/*
    957 	 * Do nothing if we're in interrupt context.
    958 	 * Access to an L1 by the kernel pmap must not affect
    959 	 * the LRU list.
    960 	 */
    961 	if (current_intr_depth || pm == pmap_kernel())
    962 		return;
    963 
    964 	l1 = pm->pm_l1;
    965 
    966 	/*
    967 	 * If the L1 is not currently on the LRU list, just return
    968 	 */
    969 	if (l1->l1_domain_use_count == PMAP_DOMAINS)
    970 		return;
    971 
    972 	simple_lock(&l1_lru_lock);
    973 
    974 	/*
    975 	 * Check the use count again, now that we've acquired the lock
    976 	 */
    977 	if (l1->l1_domain_use_count == PMAP_DOMAINS) {
    978 		simple_unlock(&l1_lru_lock);
    979 		return;
    980 	}
    981 
    982 	/*
    983 	 * Move the L1 to the back of the LRU list
    984 	 */
    985 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
    986 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
    987 
    988 	simple_unlock(&l1_lru_lock);
    989 }
    990 
    991 /*
    992  * void pmap_free_l2_ptp(pt_entry_t *, paddr_t *)
    993  *
    994  * Free an L2 descriptor table.
    995  */
    996 static __inline void
    997 #ifndef PMAP_INCLUDE_PTE_SYNC
    998 pmap_free_l2_ptp(pt_entry_t *l2, paddr_t pa)
    999 #else
   1000 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2, paddr_t pa)
   1001 #endif
   1002 {
   1003 #ifdef PMAP_INCLUDE_PTE_SYNC
   1004 	/*
   1005 	 * Note: With a write-back cache, we may need to sync this
   1006 	 * L2 table before re-using it.
   1007 	 * This is because it may have belonged to a non-current
   1008 	 * pmap, in which case the cache syncs would have been
   1009 	 * skipped when the pages were being unmapped. If the
   1010 	 * L2 table were then to be immediately re-allocated to
   1011 	 * the *current* pmap, it may well contain stale mappings
   1012 	 * which have not yet been cleared by a cache write-back
   1013 	 * and so would still be visible to the mmu.
   1014 	 */
   1015 	if (need_sync)
   1016 		PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1017 #endif
   1018 	pool_cache_put_paddr(&pmap_l2ptp_cache, (void *)l2, pa);
   1019 }
   1020 
   1021 /*
   1022  * Returns a pointer to the L2 bucket associated with the specified pmap
   1023  * and VA, or NULL if no L2 bucket exists for the address.
   1024  */
   1025 static __inline struct l2_bucket *
   1026 pmap_get_l2_bucket(pmap_t pm, vaddr_t va)
   1027 {
   1028 	struct l2_dtable *l2;
   1029 	struct l2_bucket *l2b;
   1030 	u_short l1idx;
   1031 
   1032 	l1idx = L1_IDX(va);
   1033 
   1034 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
   1035 	    (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
   1036 		return (NULL);
   1037 
   1038 	return (l2b);
   1039 }
   1040 
   1041 /*
   1042  * Returns a pointer to the L2 bucket associated with the specified pmap
   1043  * and VA.
   1044  *
   1045  * If no L2 bucket exists, perform the necessary allocations to put an L2
   1046  * bucket/page table in place.
   1047  *
   1048  * Note that if a new L2 bucket/page was allocated, the caller *must*
   1049  * increment the bucket occupancy counter appropriately *before*
   1050  * releasing the pmap's lock to ensure no other thread or cpu deallocates
   1051  * the bucket/page in the meantime.
   1052  */
   1053 static struct l2_bucket *
   1054 pmap_alloc_l2_bucket(pmap_t pm, vaddr_t va)
   1055 {
   1056 	struct l2_dtable *l2;
   1057 	struct l2_bucket *l2b;
   1058 	u_short l1idx;
   1059 
   1060 	l1idx = L1_IDX(va);
   1061 
   1062 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   1063 		/*
   1064 		 * No mapping at this address, as there is
   1065 		 * no entry in the L1 table.
   1066 		 * Need to allocate a new l2_dtable.
   1067 		 */
   1068 		if ((l2 = pmap_alloc_l2_dtable()) == NULL)
   1069 			return (NULL);
   1070 
   1071 		/*
   1072 		 * Link it into the parent pmap
   1073 		 */
   1074 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   1075 	}
   1076 
   1077 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   1078 
   1079 	/*
   1080 	 * Fetch pointer to the L2 page table associated with the address.
   1081 	 */
   1082 	if (l2b->l2b_kva == NULL) {
   1083 		pt_entry_t *ptep;
   1084 
   1085 		/*
   1086 		 * No L2 page table has been allocated. Chances are, this
   1087 		 * is because we just allocated the l2_dtable, above.
   1088 		 */
   1089 		if ((ptep = pmap_alloc_l2_ptp(&l2b->l2b_phys)) == NULL) {
   1090 			/*
   1091 			 * Oops, no more L2 page tables available at this
   1092 			 * time. We may need to deallocate the l2_dtable
   1093 			 * if we allocated a new one above.
   1094 			 */
   1095 			if (l2->l2_occupancy == 0) {
   1096 				pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1097 				pmap_free_l2_dtable(l2);
   1098 			}
   1099 			return (NULL);
   1100 		}
   1101 
   1102 		l2->l2_occupancy++;
   1103 		l2b->l2b_kva = ptep;
   1104 		l2b->l2b_l1idx = l1idx;
   1105 	}
   1106 
   1107 	return (l2b);
   1108 }
   1109 
   1110 /*
   1111  * One or more mappings in the specified L2 descriptor table have just been
   1112  * invalidated.
   1113  *
   1114  * Garbage collect the metadata and descriptor table itself if necessary.
   1115  *
   1116  * The pmap lock must be acquired when this is called (not necessary
   1117  * for the kernel pmap).
   1118  */
   1119 static void
   1120 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
   1121 {
   1122 	struct l2_dtable *l2;
   1123 	pd_entry_t *pl1pd, l1pd;
   1124 	pt_entry_t *ptep;
   1125 	u_short l1idx;
   1126 
   1127 	KDASSERT(count <= l2b->l2b_occupancy);
   1128 
   1129 	/*
   1130 	 * Update the bucket's reference count according to how many
   1131 	 * PTEs the caller has just invalidated.
   1132 	 */
   1133 	l2b->l2b_occupancy -= count;
   1134 
   1135 	/*
   1136 	 * Note:
   1137 	 *
   1138 	 * Level 2 page tables allocated to the kernel pmap are never freed
   1139 	 * as that would require checking all Level 1 page tables and
   1140 	 * removing any references to the Level 2 page table. See also the
   1141 	 * comment elsewhere about never freeing bootstrap L2 descriptors.
   1142 	 *
   1143 	 * We make do with just invalidating the mapping in the L2 table.
   1144 	 *
   1145 	 * This isn't really a big deal in practice and, in fact, leads
   1146 	 * to a performance win over time as we don't need to continually
   1147 	 * alloc/free.
   1148 	 */
   1149 	if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
   1150 		return;
   1151 
   1152 	/*
   1153 	 * There are no more valid mappings in this level 2 page table.
   1154 	 * Go ahead and NULL-out the pointer in the bucket, then
   1155 	 * free the page table.
   1156 	 */
   1157 	l1idx = l2b->l2b_l1idx;
   1158 	ptep = l2b->l2b_kva;
   1159 	l2b->l2b_kva = NULL;
   1160 
   1161 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   1162 
   1163 	/*
   1164 	 * If the L1 slot matches the pmap's domain
   1165 	 * number, then invalidate it.
   1166 	 */
   1167 	l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
   1168 	if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
   1169 		*pl1pd = 0;
   1170 		PTE_SYNC(pl1pd);
   1171 	}
   1172 
   1173 	/*
   1174 	 * Release the L2 descriptor table back to the pool cache.
   1175 	 */
   1176 #ifndef PMAP_INCLUDE_PTE_SYNC
   1177 	pmap_free_l2_ptp(ptep, l2b->l2b_phys);
   1178 #else
   1179 	pmap_free_l2_ptp(!pmap_is_cached(pm), ptep, l2b->l2b_phys);
   1180 #endif
   1181 
   1182 	/*
   1183 	 * Update the reference count in the associated l2_dtable
   1184 	 */
   1185 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   1186 	if (--l2->l2_occupancy > 0)
   1187 		return;
   1188 
   1189 	/*
   1190 	 * There are no more valid mappings in any of the Level 1
   1191 	 * slots managed by this l2_dtable. Go ahead and NULL-out
   1192 	 * the pointer in the parent pmap and free the l2_dtable.
   1193 	 */
   1194 	pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1195 	pmap_free_l2_dtable(l2);
   1196 }
   1197 
   1198 /*
   1199  * Pool cache constructors for L2 descriptor tables, metadata and pmap
   1200  * structures.
   1201  */
   1202 static int
   1203 pmap_l2ptp_ctor(void *arg, void *v, int flags)
   1204 {
   1205 #ifndef PMAP_INCLUDE_PTE_SYNC
   1206 	struct l2_bucket *l2b;
   1207 	pt_entry_t *ptep, pte;
   1208 	vaddr_t va = (vaddr_t)v & ~PGOFSET;
   1209 
   1210 	/*
   1211 	 * The mappings for these page tables were initially made using
   1212 	 * pmap_kenter_pa() by the pool subsystem. Therefore, the cache-
   1213 	 * mode will not be right for page table mappings. To avoid
   1214 	 * polluting the pmap_kenter_pa() code with a special case for
   1215 	 * page tables, we simply fix up the cache-mode here if it's not
   1216 	 * correct.
   1217 	 */
   1218 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   1219 	KDASSERT(l2b != NULL);
   1220 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   1221 	pte = *ptep;
   1222 
   1223 	if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   1224 		/*
   1225 		 * Page tables must have the cache-mode set to Write-Thru.
   1226 		 */
   1227 		*ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   1228 		PTE_SYNC(ptep);
   1229 		cpu_tlb_flushD_SE(va);
   1230 		cpu_cpwait();
   1231 	}
   1232 #endif
   1233 
   1234 	memset(v, 0, L2_TABLE_SIZE_REAL);
   1235 	PTE_SYNC_RANGE(v, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1236 	return (0);
   1237 }
   1238 
   1239 static int
   1240 pmap_l2dtable_ctor(void *arg, void *v, int flags)
   1241 {
   1242 
   1243 	memset(v, 0, sizeof(struct l2_dtable));
   1244 	return (0);
   1245 }
   1246 
   1247 static int
   1248 pmap_pmap_ctor(void *arg, void *v, int flags)
   1249 {
   1250 
   1251 	memset(v, 0, sizeof(struct pmap));
   1252 	return (0);
   1253 }
   1254 
   1255 /*
   1256  * Since we have a virtually indexed cache, we may need to inhibit caching if
   1257  * there is more than one mapping and at least one of them is writable.
   1258  * Since we purge the cache on every context switch, we only need to check for
   1259  * other mappings within the same pmap, or kernel_pmap.
   1260  * This function is also called when a page is unmapped, to possibly reenable
   1261  * caching on any remaining mappings.
   1262  *
   1263  * The code implements the following logic, where:
   1264  *
   1265  * KW = # of kernel read/write pages
   1266  * KR = # of kernel read only pages
   1267  * UW = # of user read/write pages
   1268  * UR = # of user read only pages
   1269  *
   1270  * KC = kernel mapping is cacheable
   1271  * UC = user mapping is cacheable
   1272  *
   1273  *               KW=0,KR=0  KW=0,KR>0  KW=1,KR=0  KW>1,KR>=0
   1274  *             +---------------------------------------------
   1275  * UW=0,UR=0   | ---        KC=1       KC=1       KC=0
   1276  * UW=0,UR>0   | UC=1       KC=1,UC=1  KC=0,UC=0  KC=0,UC=0
   1277  * UW=1,UR=0   | UC=1       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1278  * UW>1,UR>=0  | UC=0       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1279  */
   1280 
   1281 static const int pmap_vac_flags[4][4] = {
   1282 	{-1,		0,		0,		PVF_KNC},
   1283 	{0,		0,		PVF_NC,		PVF_NC},
   1284 	{0,		PVF_NC,		PVF_NC,		PVF_NC},
   1285 	{PVF_UNC,	PVF_NC,		PVF_NC,		PVF_NC}
   1286 };
   1287 
   1288 static __inline int
   1289 pmap_get_vac_flags(const struct vm_page *pg)
   1290 {
   1291 	int kidx, uidx;
   1292 
   1293 	kidx = 0;
   1294 	if (pg->mdpage.kro_mappings || pg->mdpage.krw_mappings > 1)
   1295 		kidx |= 1;
   1296 	if (pg->mdpage.krw_mappings)
   1297 		kidx |= 2;
   1298 
   1299 	uidx = 0;
   1300 	if (pg->mdpage.uro_mappings || pg->mdpage.urw_mappings > 1)
   1301 		uidx |= 1;
   1302 	if (pg->mdpage.urw_mappings)
   1303 		uidx |= 2;
   1304 
   1305 	return (pmap_vac_flags[uidx][kidx]);
   1306 }
   1307 
   1308 static __inline void
   1309 pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1310 {
   1311 	int nattr;
   1312 
   1313 	nattr = pmap_get_vac_flags(pg);
   1314 
   1315 	if (nattr < 0) {
   1316 		pg->mdpage.pvh_attrs &= ~PVF_NC;
   1317 		return;
   1318 	}
   1319 
   1320 	if (nattr == 0 && (pg->mdpage.pvh_attrs & PVF_NC) == 0)
   1321 		return;
   1322 
   1323 	if (pm == pmap_kernel())
   1324 		pmap_vac_me_kpmap(pg, pm, va);
   1325 	else
   1326 		pmap_vac_me_user(pg, pm, va);
   1327 
   1328 	pg->mdpage.pvh_attrs = (pg->mdpage.pvh_attrs & ~PVF_NC) | nattr;
   1329 }
   1330 
   1331 static void
   1332 pmap_vac_me_kpmap(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1333 {
   1334 	u_int u_cacheable, u_entries;
   1335 	struct pv_entry *pv;
   1336 	pmap_t last_pmap = pm;
   1337 
   1338 	/*
   1339 	 * Pass one, see if there are both kernel and user pmaps for
   1340 	 * this page.  Calculate whether there are user-writable or
   1341 	 * kernel-writable pages.
   1342 	 */
   1343 	u_cacheable = 0;
   1344 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1345 		if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
   1346 			u_cacheable++;
   1347 	}
   1348 
   1349 	u_entries = pg->mdpage.urw_mappings + pg->mdpage.uro_mappings;
   1350 
   1351 	/*
   1352 	 * We know we have just been updating a kernel entry, so if
   1353 	 * all user pages are already cacheable, then there is nothing
   1354 	 * further to do.
   1355 	 */
   1356 	if (pg->mdpage.k_mappings == 0 && u_cacheable == u_entries)
   1357 		return;
   1358 
   1359 	if (u_entries) {
   1360 		/*
   1361 		 * Scan over the list again, for each entry, if it
   1362 		 * might not be set correctly, call pmap_vac_me_user
   1363 		 * to recalculate the settings.
   1364 		 */
   1365 		for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1366 			/*
   1367 			 * We know kernel mappings will get set
   1368 			 * correctly in other calls.  We also know
   1369 			 * that if the pmap is the same as last_pmap
   1370 			 * then we've just handled this entry.
   1371 			 */
   1372 			if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
   1373 				continue;
   1374 
   1375 			/*
   1376 			 * If there are kernel entries and this page
   1377 			 * is writable but non-cacheable, then we can
   1378 			 * skip this entry also.
   1379 			 */
   1380 			if (pg->mdpage.k_mappings &&
   1381 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
   1382 			    (PVF_NC | PVF_WRITE))
   1383 				continue;
   1384 
   1385 			/*
   1386 			 * Similarly if there are no kernel-writable
   1387 			 * entries and the page is already
   1388 			 * read-only/cacheable.
   1389 			 */
   1390 			if (pg->mdpage.krw_mappings == 0 &&
   1391 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
   1392 				continue;
   1393 
   1394 			/*
   1395 			 * For some of the remaining cases, we know
   1396 			 * that we must recalculate, but for others we
   1397 			 * can't tell if they are correct or not, so
   1398 			 * we recalculate anyway.
   1399 			 */
   1400 			pmap_vac_me_user(pg, (last_pmap = pv->pv_pmap), 0);
   1401 		}
   1402 
   1403 		if (pg->mdpage.k_mappings == 0)
   1404 			return;
   1405 	}
   1406 
   1407 	pmap_vac_me_user(pg, pm, va);
   1408 }
   1409 
   1410 static void
   1411 pmap_vac_me_user(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1412 {
   1413 	pmap_t kpmap = pmap_kernel();
   1414 	struct pv_entry *pv, *npv;
   1415 	struct l2_bucket *l2b;
   1416 	pt_entry_t *ptep, pte;
   1417 	u_int entries = 0;
   1418 	u_int writable = 0;
   1419 	u_int cacheable_entries = 0;
   1420 	u_int kern_cacheable = 0;
   1421 	u_int other_writable = 0;
   1422 
   1423 	/*
   1424 	 * Count mappings and writable mappings in this pmap.
   1425 	 * Include kernel mappings as part of our own.
   1426 	 * Keep a pointer to the first one.
   1427 	 */
   1428 	for (pv = npv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1429 		/* Count mappings in the same pmap */
   1430 		if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
   1431 			if (entries++ == 0)
   1432 				npv = pv;
   1433 
   1434 			/* Cacheable mappings */
   1435 			if ((pv->pv_flags & PVF_NC) == 0) {
   1436 				cacheable_entries++;
   1437 				if (kpmap == pv->pv_pmap)
   1438 					kern_cacheable++;
   1439 			}
   1440 
   1441 			/* Writable mappings */
   1442 			if (pv->pv_flags & PVF_WRITE)
   1443 				++writable;
   1444 		} else
   1445 		if (pv->pv_flags & PVF_WRITE)
   1446 			other_writable = 1;
   1447 	}
   1448 
   1449 	/*
   1450 	 * Enable or disable caching as necessary.
   1451 	 * Note: the first entry might be part of the kernel pmap,
   1452 	 * so we can't assume this is indicative of the state of the
   1453 	 * other (maybe non-kpmap) entries.
   1454 	 */
   1455 	if ((entries > 1 && writable) ||
   1456 	    (entries > 0 && pm == kpmap && other_writable)) {
   1457 		if (cacheable_entries == 0)
   1458 			return;
   1459 
   1460 		for (pv = npv; pv; pv = pv->pv_next) {
   1461 			if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
   1462 			    (pv->pv_flags & PVF_NC))
   1463 				continue;
   1464 
   1465 			pv->pv_flags |= PVF_NC;
   1466 
   1467 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1468 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1469 			pte = *ptep & ~L2_S_CACHE_MASK;
   1470 
   1471 			if ((va != pv->pv_va || pm != pv->pv_pmap) &&
   1472 			    l2pte_valid(pte)) {
   1473 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1474 					pmap_idcache_wbinv_range(pv->pv_pmap,
   1475 					    pv->pv_va, PAGE_SIZE);
   1476 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1477 					    pv->pv_va);
   1478 				} else
   1479 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1480 					pmap_dcache_wb_range(pv->pv_pmap,
   1481 					    pv->pv_va, PAGE_SIZE, TRUE,
   1482 					    (pv->pv_flags & PVF_WRITE) == 0);
   1483 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1484 					    pv->pv_va);
   1485 				}
   1486 			}
   1487 
   1488 			*ptep = pte;
   1489 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1490 		}
   1491 		cpu_cpwait();
   1492 	} else
   1493 	if (entries > cacheable_entries) {
   1494 		/*
   1495 		 * Turn cacheing back on for some pages.  If it is a kernel
   1496 		 * page, only do so if there are no other writable pages.
   1497 		 */
   1498 		for (pv = npv; pv; pv = pv->pv_next) {
   1499 			if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
   1500 			    (kpmap != pv->pv_pmap || other_writable)))
   1501 				continue;
   1502 
   1503 			pv->pv_flags &= ~PVF_NC;
   1504 
   1505 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1506 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1507 			pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
   1508 
   1509 			if (l2pte_valid(pte)) {
   1510 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1511 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1512 					    pv->pv_va);
   1513 				} else
   1514 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1515 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1516 					    pv->pv_va);
   1517 				}
   1518 			}
   1519 
   1520 			*ptep = pte;
   1521 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1522 		}
   1523 	}
   1524 }
   1525 
   1526 /*
   1527  * Modify pte bits for all ptes corresponding to the given physical address.
   1528  * We use `maskbits' rather than `clearbits' because we're always passing
   1529  * constants and the latter would require an extra inversion at run-time.
   1530  */
   1531 static void
   1532 pmap_clearbit(struct vm_page *pg, u_int maskbits)
   1533 {
   1534 	struct l2_bucket *l2b;
   1535 	struct pv_entry *pv;
   1536 	pt_entry_t *ptep, npte, opte;
   1537 	pmap_t pm;
   1538 	vaddr_t va;
   1539 	u_int oflags;
   1540 
   1541 	NPDEBUG(PDB_BITS,
   1542 	    printf("pmap_clearbit: pg %p (0x%08lx) mask 0x%x\n",
   1543 	    pg, pg->phys_addr, maskbits));
   1544 
   1545 	PMAP_HEAD_TO_MAP_LOCK();
   1546 	simple_lock(&pg->mdpage.pvh_slock);
   1547 
   1548 	/*
   1549 	 * Clear saved attributes (modify, reference)
   1550 	 */
   1551 	pg->mdpage.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
   1552 
   1553 	if (pg->mdpage.pvh_list == NULL) {
   1554 		simple_unlock(&pg->mdpage.pvh_slock);
   1555 		PMAP_HEAD_TO_MAP_UNLOCK();
   1556 		return;
   1557 	}
   1558 
   1559 	/*
   1560 	 * Loop over all current mappings setting/clearing as appropos
   1561 	 */
   1562 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1563 		va = pv->pv_va;
   1564 		pm = pv->pv_pmap;
   1565 		oflags = pv->pv_flags;
   1566 		pv->pv_flags &= ~maskbits;
   1567 
   1568 		pmap_acquire_pmap_lock(pm);
   1569 
   1570 		l2b = pmap_get_l2_bucket(pm, va);
   1571 		KDASSERT(l2b != NULL);
   1572 
   1573 		ptep = &l2b->l2b_kva[l2pte_index(va)];
   1574 		npte = opte = *ptep;
   1575 
   1576 		NPDEBUG(PDB_BITS,
   1577 		    printf(
   1578 		    "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
   1579 		    pv, pv->pv_pmap, pv->pv_va, oflags));
   1580 
   1581 		if (maskbits & (PVF_WRITE|PVF_MOD)) {
   1582 			if ((pv->pv_flags & PVF_NC)) {
   1583 				/*
   1584 				 * Entry is not cacheable:
   1585 				 *
   1586 				 * Don't turn caching on again if this is a
   1587 				 * modified emulation. This would be
   1588 				 * inconsitent with the settings created by
   1589 				 * pmap_vac_me_harder(). Otherwise, it's safe
   1590 				 * to re-enable cacheing.
   1591 				 *
   1592 				 * There's no need to call pmap_vac_me_harder()
   1593 				 * here: all pages are losing their write
   1594 				 * permission.
   1595 				 */
   1596 				if (maskbits & PVF_WRITE) {
   1597 					npte |= pte_l2_s_cache_mode;
   1598 					pv->pv_flags &= ~PVF_NC;
   1599 				}
   1600 			} else
   1601 			if (opte & L2_S_PROT_W) {
   1602 				/*
   1603 				 * Entry is writable/cacheable: check if pmap
   1604 				 * is current if it is flush it, otherwise it
   1605 				 * won't be in the cache
   1606 				 */
   1607 				if (PV_BEEN_EXECD(oflags))
   1608 					pmap_idcache_wbinv_range(pm, pv->pv_va,
   1609 					    PAGE_SIZE);
   1610 				else
   1611 				if (PV_BEEN_REFD(oflags))
   1612 					pmap_dcache_wb_range(pm, pv->pv_va,
   1613 					    PAGE_SIZE,
   1614 					    (maskbits & PVF_REF) ? TRUE : FALSE,
   1615 					    FALSE);
   1616 			}
   1617 
   1618 			/* make the pte read only */
   1619 			npte &= ~L2_S_PROT_W;
   1620 
   1621 			if (maskbits & PVF_WRITE) {
   1622 				/*
   1623 				 * Keep alias accounting up to date
   1624 				 */
   1625 				if (pv->pv_pmap == pmap_kernel()) {
   1626 					if (oflags & PVF_WRITE) {
   1627 						pg->mdpage.krw_mappings--;
   1628 						pg->mdpage.kro_mappings++;
   1629 					}
   1630 				} else
   1631 				if (oflags & PVF_WRITE) {
   1632 					pg->mdpage.urw_mappings--;
   1633 					pg->mdpage.uro_mappings++;
   1634 				}
   1635 			}
   1636 		}
   1637 
   1638 		if (maskbits & PVF_REF) {
   1639 			if ((pv->pv_flags & PVF_NC) == 0 &&
   1640 			    (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
   1641 				/*
   1642 				 * Check npte here; we may have already
   1643 				 * done the wbinv above, and the validity
   1644 				 * of the PTE is the same for opte and
   1645 				 * npte.
   1646 				 */
   1647 				if (npte & L2_S_PROT_W) {
   1648 					if (PV_BEEN_EXECD(oflags))
   1649 						pmap_idcache_wbinv_range(pm,
   1650 						    pv->pv_va, PAGE_SIZE);
   1651 					else
   1652 					if (PV_BEEN_REFD(oflags))
   1653 						pmap_dcache_wb_range(pm,
   1654 						    pv->pv_va, PAGE_SIZE,
   1655 						    TRUE, FALSE);
   1656 				} else
   1657 				if ((npte & L2_TYPE_MASK) != L2_TYPE_INV) {
   1658 					/* XXXJRT need idcache_inv_range */
   1659 					if (PV_BEEN_EXECD(oflags))
   1660 						pmap_idcache_wbinv_range(pm,
   1661 						    pv->pv_va, PAGE_SIZE);
   1662 					else
   1663 					if (PV_BEEN_REFD(oflags))
   1664 						pmap_dcache_wb_range(pm,
   1665 						    pv->pv_va, PAGE_SIZE,
   1666 						    TRUE, TRUE);
   1667 				}
   1668 			}
   1669 
   1670 			/*
   1671 			 * Make the PTE invalid so that we will take a
   1672 			 * page fault the next time the mapping is
   1673 			 * referenced.
   1674 			 */
   1675 			npte &= ~L2_TYPE_MASK;
   1676 			npte |= L2_TYPE_INV;
   1677 		}
   1678 
   1679 		if (npte != opte) {
   1680 			*ptep = npte;
   1681 			PTE_SYNC(ptep);
   1682 			/* Flush the TLB entry if a current pmap. */
   1683 			if (PV_BEEN_EXECD(oflags))
   1684 				pmap_tlb_flushID_SE(pm, pv->pv_va);
   1685 			else
   1686 			if (PV_BEEN_REFD(oflags))
   1687 				pmap_tlb_flushD_SE(pm, pv->pv_va);
   1688 		}
   1689 
   1690 		pmap_release_pmap_lock(pm);
   1691 
   1692 		NPDEBUG(PDB_BITS,
   1693 		    printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
   1694 		    pm, va, opte, npte));
   1695 	}
   1696 
   1697 	simple_unlock(&pg->mdpage.pvh_slock);
   1698 	PMAP_HEAD_TO_MAP_UNLOCK();
   1699 }
   1700 
   1701 /*
   1702  * pmap_clean_page()
   1703  *
   1704  * This is a local function used to work out the best strategy to clean
   1705  * a single page referenced by its entry in the PV table. It's used by
   1706  * pmap_copy_page, pmap_zero page and maybe some others later on.
   1707  *
   1708  * Its policy is effectively:
   1709  *  o If there are no mappings, we don't bother doing anything with the cache.
   1710  *  o If there is one mapping, we clean just that page.
   1711  *  o If there are multiple mappings, we clean the entire cache.
   1712  *
   1713  * So that some functions can be further optimised, it returns 0 if it didn't
   1714  * clean the entire cache, or 1 if it did.
   1715  *
   1716  * XXX One bug in this routine is that if the pv_entry has a single page
   1717  * mapped at 0x00000000 a whole cache clean will be performed rather than
   1718  * just the 1 page. Since this should not occur in everyday use and if it does
   1719  * it will just result in not the most efficient clean for the page.
   1720  */
   1721 static int
   1722 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
   1723 {
   1724 	pmap_t pm, pm_to_clean = NULL;
   1725 	struct pv_entry *npv;
   1726 	u_int cache_needs_cleaning = 0;
   1727 	u_int flags = 0;
   1728 	vaddr_t page_to_clean = 0;
   1729 
   1730 	if (pv == NULL) {
   1731 		/* nothing mapped in so nothing to flush */
   1732 		return (0);
   1733 	}
   1734 
   1735 	/*
   1736 	 * Since we flush the cache each time we change to a different
   1737 	 * user vmspace, we only need to flush the page if it is in the
   1738 	 * current pmap.
   1739 	 */
   1740 	if (curproc)
   1741 		pm = curproc->p_vmspace->vm_map.pmap;
   1742 	else
   1743 		pm = pmap_kernel();
   1744 
   1745 	for (npv = pv; npv; npv = npv->pv_next) {
   1746 		if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
   1747 			flags |= npv->pv_flags;
   1748 			/*
   1749 			 * The page is mapped non-cacheable in
   1750 			 * this map.  No need to flush the cache.
   1751 			 */
   1752 			if (npv->pv_flags & PVF_NC) {
   1753 #ifdef DIAGNOSTIC
   1754 				if (cache_needs_cleaning)
   1755 					panic("pmap_clean_page: "
   1756 					    "cache inconsistency");
   1757 #endif
   1758 				break;
   1759 			} else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
   1760 				continue;
   1761 			if (cache_needs_cleaning) {
   1762 				page_to_clean = 0;
   1763 				break;
   1764 			} else {
   1765 				page_to_clean = npv->pv_va;
   1766 				pm_to_clean = npv->pv_pmap;
   1767 			}
   1768 			cache_needs_cleaning = 1;
   1769 		}
   1770 	}
   1771 
   1772 	if (page_to_clean) {
   1773 		if (PV_BEEN_EXECD(flags))
   1774 			pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
   1775 			    PAGE_SIZE);
   1776 		else
   1777 			pmap_dcache_wb_range(pm_to_clean, page_to_clean,
   1778 			    PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
   1779 	} else if (cache_needs_cleaning) {
   1780 		if (PV_BEEN_EXECD(flags))
   1781 			pmap_idcache_wbinv_all(pm);
   1782 		else
   1783 			pmap_dcache_wbinv_all(pm);
   1784 		return (1);
   1785 	}
   1786 	return (0);
   1787 }
   1788 
   1789 /*
   1790  * Routine:	pmap_page_remove
   1791  * Function:
   1792  *		Removes this physical page from
   1793  *		all physical maps in which it resides.
   1794  *		Reflects back modify bits to the pager.
   1795  */
   1796 static void
   1797 pmap_page_remove(struct vm_page *pg)
   1798 {
   1799 	struct l2_bucket *l2b;
   1800 	struct pv_entry *pv, *npv;
   1801 	pmap_t pm, curpm;
   1802 	pt_entry_t *ptep, pte;
   1803 	boolean_t flush;
   1804 	u_int flags;
   1805 
   1806 	NPDEBUG(PDB_FOLLOW,
   1807 	    printf("pmap_page_remove: pg %p (0x%08lx)\n", pg, pg->phys_addr));
   1808 
   1809 	PMAP_HEAD_TO_MAP_LOCK();
   1810 	simple_lock(&pg->mdpage.pvh_slock);
   1811 
   1812 	pv = pg->mdpage.pvh_list;
   1813 	if (pv == NULL) {
   1814 		simple_unlock(&pg->mdpage.pvh_slock);
   1815 		PMAP_HEAD_TO_MAP_UNLOCK();
   1816 		return;
   1817 	}
   1818 
   1819 	/*
   1820 	 * Clear alias counts
   1821 	 */
   1822 	pg->mdpage.k_mappings = 0;
   1823 	pg->mdpage.urw_mappings = pg->mdpage.uro_mappings = 0;
   1824 
   1825 	flush = FALSE;
   1826 	flags = 0;
   1827 	if (curproc)
   1828 		curpm = curproc->p_vmspace->vm_map.pmap;
   1829 	else
   1830 		curpm = pmap_kernel();
   1831 
   1832 	pmap_clean_page(pv, FALSE);
   1833 
   1834 	while (pv) {
   1835 		pm = pv->pv_pmap;
   1836 		if (flush == FALSE && (pm == curpm || pm == pmap_kernel()))
   1837 			flush = TRUE;
   1838 
   1839 		pmap_acquire_pmap_lock(pm);
   1840 
   1841 		l2b = pmap_get_l2_bucket(pm, pv->pv_va);
   1842 		KDASSERT(l2b != NULL);
   1843 
   1844 		ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1845 		pte = *ptep;
   1846 
   1847 		/*
   1848 		 * Update statistics
   1849 		 */
   1850 		--pm->pm_stats.resident_count;
   1851 
   1852 		/* Wired bit */
   1853 		if (pv->pv_flags & PVF_WIRED)
   1854 			--pm->pm_stats.wired_count;
   1855 
   1856 		flags |= pv->pv_flags;
   1857 
   1858 		/*
   1859 		 * Invalidate the PTEs.
   1860 		 */
   1861 		*ptep = 0;
   1862 		PTE_SYNC_CURRENT(pm, ptep);
   1863 		pmap_free_l2_bucket(pm, l2b, 1);
   1864 
   1865 		npv = pv->pv_next;
   1866 		pool_put(&pmap_pv_pool, pv);
   1867 		pv = npv;
   1868 		pmap_release_pmap_lock(pm);
   1869 	}
   1870 	pg->mdpage.pvh_list = NULL;
   1871 	simple_unlock(&pg->mdpage.pvh_slock);
   1872 	PMAP_HEAD_TO_MAP_UNLOCK();
   1873 
   1874 	if (flush) {
   1875 		if (PV_BEEN_EXECD(flags))
   1876 			pmap_tlb_flushID(curpm);
   1877 		else
   1878 			pmap_tlb_flushD(curpm);
   1879 	}
   1880 	cpu_cpwait();
   1881 }
   1882 
   1883 /*
   1884  * pmap_t pmap_create(void)
   1885  *
   1886  *      Create a new pmap structure from scratch.
   1887  */
   1888 pmap_t
   1889 pmap_create(void)
   1890 {
   1891 	pmap_t pm;
   1892 
   1893 	pm = pool_cache_get(&pmap_pmap_cache, PR_WAITOK);
   1894 
   1895 	simple_lock_init(&pm->pm_lock);
   1896 	pm->pm_obj.pgops = NULL;	/* currently not a mappable object */
   1897 	TAILQ_INIT(&pm->pm_obj.memq);
   1898 	pm->pm_obj.uo_npages = 0;
   1899 	pm->pm_obj.uo_refs = 1;
   1900 	pm->pm_stats.wired_count = 0;
   1901 	pm->pm_stats.resident_count = 1;
   1902 	pm->pm_cstate.cs_all = 0;
   1903 	pmap_alloc_l1(pm);
   1904 
   1905 	/*
   1906 	 * Note: The pool cache ensures that the pm_l2[] array is already
   1907 	 * initialised to zero.
   1908 	 */
   1909 
   1910 	pmap_pinit(pm);
   1911 
   1912 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   1913 
   1914 	return (pm);
   1915 }
   1916 
   1917 /*
   1918  * void pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
   1919  *     int flags)
   1920  *
   1921  *      Insert the given physical page (p) at
   1922  *      the specified virtual address (v) in the
   1923  *      target physical map with the protection requested.
   1924  *
   1925  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   1926  *      or lose information.  That is, this routine must actually
   1927  *      insert this page into the given map NOW.
   1928  */
   1929 int
   1930 pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags)
   1931 {
   1932 	struct l2_bucket *l2b;
   1933 	struct vm_page *pg, *opg;
   1934 	struct pv_entry *pve;
   1935 	pt_entry_t *ptep, npte, opte;
   1936 	u_int nflags;
   1937 	u_int oflags;
   1938 
   1939 	NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags));
   1940 
   1941 	KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
   1942 	KDASSERT(((va | pa) & PGOFSET) == 0);
   1943 
   1944 	/*
   1945 	 * Get a pointer to the page.  Later on in this function, we
   1946 	 * test for a managed page by checking pg != NULL.
   1947 	 */
   1948 	pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
   1949 
   1950 	nflags = 0;
   1951 	if (prot & VM_PROT_WRITE)
   1952 		nflags |= PVF_WRITE;
   1953 	if (prot & VM_PROT_EXECUTE)
   1954 		nflags |= PVF_EXEC;
   1955 	if (flags & PMAP_WIRED)
   1956 		nflags |= PVF_WIRED;
   1957 
   1958 	PMAP_MAP_TO_HEAD_LOCK();
   1959 	pmap_acquire_pmap_lock(pm);
   1960 
   1961 	/*
   1962 	 * Fetch the L2 bucket which maps this page, allocating one if
   1963 	 * necessary for user pmaps.
   1964 	 */
   1965 	if (pm == pmap_kernel())
   1966 		l2b = pmap_get_l2_bucket(pm, va);
   1967 	else
   1968 		l2b = pmap_alloc_l2_bucket(pm, va);
   1969 	if (l2b == NULL) {
   1970 		if (flags & PMAP_CANFAIL) {
   1971 			pmap_release_pmap_lock(pm);
   1972 			PMAP_MAP_TO_HEAD_UNLOCK();
   1973 			return (ENOMEM);
   1974 		}
   1975 		panic("pmap_enter: failed to allocate L2 bucket");
   1976 	}
   1977 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   1978 	opte = *ptep;
   1979 	npte = pa;
   1980 	oflags = 0;
   1981 
   1982 	if (opte) {
   1983 		/*
   1984 		 * There is already a mapping at this address.
   1985 		 * If the physical address is different, lookup the
   1986 		 * vm_page.
   1987 		 */
   1988 		if (l2pte_pa(opte) != pa)
   1989 			opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   1990 		else
   1991 			opg = pg;
   1992 	} else
   1993 		opg = NULL;
   1994 
   1995 	if (pg) {
   1996 		/*
   1997 		 * This is to be a managed mapping.
   1998 		 */
   1999 		if ((flags & VM_PROT_ALL) ||
   2000 		    (pg->mdpage.pvh_attrs & PVF_REF)) {
   2001 			/*
   2002 			 * - The access type indicates that we don't need
   2003 			 *   to do referenced emulation.
   2004 			 * OR
   2005 			 * - The physical page has already been referenced
   2006 			 *   so no need to re-do referenced emulation here.
   2007 			 */
   2008 			npte |= L2_S_PROTO;
   2009 
   2010 			nflags |= PVF_REF;
   2011 
   2012 			if ((prot & VM_PROT_WRITE) != 0 &&
   2013 			    ((flags & VM_PROT_WRITE) != 0 ||
   2014 			     (pg->mdpage.pvh_attrs & PVF_MOD) != 0)) {
   2015 				/*
   2016 				 * This is a writable mapping, and the
   2017 				 * page's mod state indicates it has
   2018 				 * already been modified. Make it
   2019 				 * writable from the outset.
   2020 				 */
   2021 				npte |= L2_S_PROT_W;
   2022 				nflags |= PVF_MOD;
   2023 			}
   2024 		} else {
   2025 			/*
   2026 			 * Need to do page referenced emulation.
   2027 			 */
   2028 			npte |= L2_TYPE_INV;
   2029 		}
   2030 
   2031 		npte |= pte_l2_s_cache_mode;
   2032 
   2033 		if (pg == opg) {
   2034 			/*
   2035 			 * We're changing the attrs of an existing mapping.
   2036 			 */
   2037 			simple_lock(&pg->mdpage.pvh_slock);
   2038 			oflags = pmap_modify_pv(pg, pm, va,
   2039 			    PVF_WRITE | PVF_EXEC | PVF_WIRED |
   2040 			    PVF_MOD | PVF_REF, nflags);
   2041 			simple_unlock(&pg->mdpage.pvh_slock);
   2042 
   2043 			/*
   2044 			 * We may need to flush the cache if we're
   2045 			 * doing rw-ro...
   2046 			 */
   2047 			if (pm->pm_cstate.cs_cache_d &&
   2048 			    (oflags & PVF_NC) == 0 &&
   2049 			    (opte & L2_S_PROT_W) != 0 &&
   2050 			    (prot & VM_PROT_WRITE) == 0)
   2051 				cpu_dcache_wb_range(va, PAGE_SIZE);
   2052 		} else {
   2053 			/*
   2054 			 * New mapping, or changing the backing page
   2055 			 * of an existing mapping.
   2056 			 */
   2057 			if (opg) {
   2058 				/*
   2059 				 * Replacing an existing mapping with a new one.
   2060 				 * It is part of our managed memory so we
   2061 				 * must remove it from the PV list
   2062 				 */
   2063 				simple_lock(&opg->mdpage.pvh_slock);
   2064 				pve = pmap_remove_pv(opg, pm, va);
   2065 				pmap_vac_me_harder(opg, pm, 0);
   2066 				simple_unlock(&opg->mdpage.pvh_slock);
   2067 				oflags = pve->pv_flags;
   2068 
   2069 				/*
   2070 				 * If the old mapping was valid (ref/mod
   2071 				 * emulation creates 'invalid' mappings
   2072 				 * initially) then make sure to frob
   2073 				 * the cache.
   2074 				 */
   2075 				if ((oflags & PVF_NC) == 0 &&
   2076 				    l2pte_valid(opte)) {
   2077 					if (PV_BEEN_EXECD(oflags)) {
   2078 						pmap_idcache_wbinv_range(pm, va,
   2079 						    PAGE_SIZE);
   2080 					} else
   2081 					if (PV_BEEN_REFD(oflags)) {
   2082 						pmap_dcache_wb_range(pm, va,
   2083 						    PAGE_SIZE, TRUE,
   2084 						    (oflags & PVF_WRITE) == 0);
   2085 					}
   2086 				}
   2087 			} else
   2088 			if ((pve = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){
   2089 				if ((flags & PMAP_CANFAIL) == 0)
   2090 					panic("pmap_enter: no pv entries");
   2091 
   2092 				if (pm != pmap_kernel())
   2093 					pmap_free_l2_bucket(pm, l2b, 0);
   2094 				pmap_release_pmap_lock(pm);
   2095 				PMAP_MAP_TO_HEAD_UNLOCK();
   2096 				NPDEBUG(PDB_ENTER,
   2097 				    printf("pmap_enter: ENOMEM\n"));
   2098 				return (ENOMEM);
   2099 			}
   2100 
   2101 			pmap_enter_pv(pg, pve, pm, va, nflags);
   2102 		}
   2103 	} else {
   2104 		/*
   2105 		 * We're mapping an unmanaged page.
   2106 		 * These are always readable, and possibly writable, from
   2107 		 * the get go as we don't need to track ref/mod status.
   2108 		 */
   2109 		npte |= L2_S_PROTO;
   2110 		if (prot & VM_PROT_WRITE)
   2111 			npte |= L2_S_PROT_W;
   2112 
   2113 		/*
   2114 		 * Make sure the vector table is mapped cacheable
   2115 		 */
   2116 		if (pm != pmap_kernel() && va == vector_page)
   2117 			npte |= pte_l2_s_cache_mode;
   2118 
   2119 		if (opg) {
   2120 			/*
   2121 			 * Looks like there's an existing 'managed' mapping
   2122 			 * at this address.
   2123 			 */
   2124 			simple_lock(&opg->mdpage.pvh_slock);
   2125 			pve = pmap_remove_pv(opg, pm, va);
   2126 			pmap_vac_me_harder(opg, pm, 0);
   2127 			simple_unlock(&opg->mdpage.pvh_slock);
   2128 			oflags = pve->pv_flags;
   2129 
   2130 			if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
   2131 				if (PV_BEEN_EXECD(oflags))
   2132 					pmap_idcache_wbinv_range(pm, va,
   2133 					    PAGE_SIZE);
   2134 				else
   2135 				if (PV_BEEN_REFD(oflags))
   2136 					pmap_dcache_wb_range(pm, va, PAGE_SIZE,
   2137 					    TRUE, (oflags & PVF_WRITE) == 0);
   2138 			}
   2139 			pool_put(&pmap_pv_pool, pve);
   2140 		}
   2141 	}
   2142 
   2143 	/*
   2144 	 * Make sure userland mappings get the right permissions
   2145 	 */
   2146 	if (pm != pmap_kernel() && va != vector_page)
   2147 		npte |= L2_S_PROT_U;
   2148 
   2149 	/*
   2150 	 * Keep the stats up to date
   2151 	 */
   2152 	if (opte == 0) {
   2153 		l2b->l2b_occupancy++;
   2154 		pm->pm_stats.resident_count++;
   2155 	}
   2156 
   2157 	NPDEBUG(PDB_ENTER,
   2158 	    printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte));
   2159 
   2160 	/*
   2161 	 * If this is just a wiring change, the two PTEs will be
   2162 	 * identical, so there's no need to update the page table.
   2163 	 */
   2164 	if (npte != opte) {
   2165 		boolean_t is_cached = pmap_is_cached(pm);
   2166 
   2167 		*ptep = npte;
   2168 		if (is_cached) {
   2169 			/*
   2170 			 * We only need to frob the cache/tlb if this pmap
   2171 			 * is current
   2172 			 */
   2173 			PTE_SYNC(ptep);
   2174 			if (va != vector_page && l2pte_valid(npte)) {
   2175 				/*
   2176 				 * This mapping is likely to be accessed as
   2177 				 * soon as we return to userland. Fix up the
   2178 				 * L1 entry to avoid taking another
   2179 				 * page/domain fault.
   2180 				 */
   2181 				pd_entry_t *pl1pd, l1pd;
   2182 
   2183 				pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)];
   2184 				l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) |
   2185 				    L1_C_PROTO;
   2186 				if (*pl1pd != l1pd) {
   2187 					*pl1pd = l1pd;
   2188 					PTE_SYNC(pl1pd);
   2189 				}
   2190 			}
   2191 		}
   2192 
   2193 		if (PV_BEEN_EXECD(oflags))
   2194 			pmap_tlb_flushID_SE(pm, va);
   2195 		else
   2196 		if (PV_BEEN_REFD(oflags))
   2197 			pmap_tlb_flushD_SE(pm, va);
   2198 
   2199 		NPDEBUG(PDB_ENTER,
   2200 		    printf("pmap_enter: is_cached %d cs 0x%08x\n",
   2201 		    is_cached, pm->pm_cstate.cs_all));
   2202 
   2203 		if (pg != NULL) {
   2204 			simple_lock(&pg->mdpage.pvh_slock);
   2205 			pmap_vac_me_harder(pg, pm, va);
   2206 			simple_unlock(&pg->mdpage.pvh_slock);
   2207 		}
   2208 	}
   2209 
   2210 	pmap_release_pmap_lock(pm);
   2211 	PMAP_MAP_TO_HEAD_UNLOCK();
   2212 
   2213 	return (0);
   2214 }
   2215 
   2216 /*
   2217  * pmap_remove()
   2218  *
   2219  * pmap_remove is responsible for nuking a number of mappings for a range
   2220  * of virtual address space in the current pmap. To do this efficiently
   2221  * is interesting, because in a number of cases a wide virtual address
   2222  * range may be supplied that contains few actual mappings. So, the
   2223  * optimisations are:
   2224  *  1. Skip over hunks of address space for which no L1 or L2 entry exists.
   2225  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   2226  *     maybe do just a partial cache clean. This path of execution is
   2227  *     complicated by the fact that the cache must be flushed _before_
   2228  *     the PTE is nuked, being a VAC :-)
   2229  *  3. If we're called after UVM calls pmap_remove_all(), we can defer
   2230  *     all invalidations until pmap_update(), since pmap_remove_all() has
   2231  *     already flushed the cache.
   2232  *  4. Maybe later fast-case a single page, but I don't think this is
   2233  *     going to make _that_ much difference overall.
   2234  */
   2235 
   2236 #define	PMAP_REMOVE_CLEAN_LIST_SIZE	3
   2237 
   2238 void
   2239 pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva)
   2240 {
   2241 	struct l2_bucket *l2b;
   2242 	vaddr_t next_bucket;
   2243 	pt_entry_t *ptep;
   2244 	u_int cleanlist_idx, total, cnt;
   2245 	struct {
   2246 		vaddr_t va;
   2247 		pt_entry_t *pte;
   2248 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   2249 	u_int mappings, is_exec, is_refd;
   2250 
   2251 	NPDEBUG(PDB_REMOVE, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n",
   2252 	    pm, sva, eva));
   2253 
   2254 	/*
   2255 	 * we lock in the pmap => pv_head direction
   2256 	 */
   2257 	PMAP_MAP_TO_HEAD_LOCK();
   2258 	pmap_acquire_pmap_lock(pm);
   2259 
   2260 	if (pm->pm_remove_all || !pmap_is_cached(pm)) {
   2261 		cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   2262 		if (pm->pm_cstate.cs_tlb == 0)
   2263 			pm->pm_remove_all = TRUE;
   2264 	} else
   2265 		cleanlist_idx = 0;
   2266 
   2267 	total = 0;
   2268 
   2269 	while (sva < eva) {
   2270 		/*
   2271 		 * Do one L2 bucket's worth at a time.
   2272 		 */
   2273 		next_bucket = L2_NEXT_BUCKET(sva);
   2274 		if (next_bucket > eva)
   2275 			next_bucket = eva;
   2276 
   2277 		l2b = pmap_get_l2_bucket(pm, sva);
   2278 		if (l2b == NULL) {
   2279 			sva = next_bucket;
   2280 			continue;
   2281 		}
   2282 
   2283 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   2284 		mappings = 0;
   2285 
   2286 		while (sva < next_bucket) {
   2287 			struct vm_page *pg;
   2288 			pt_entry_t pte;
   2289 			paddr_t pa;
   2290 
   2291 			pte = *ptep;
   2292 
   2293 			if (pte == 0) {
   2294 				/*
   2295 				 * Nothing here, move along
   2296 				 */
   2297 				sva += PAGE_SIZE;
   2298 				ptep++;
   2299 				continue;
   2300 			}
   2301 
   2302 			pm->pm_stats.resident_count--;
   2303 			pa = l2pte_pa(pte);
   2304 			is_exec = 0;
   2305 			is_refd = 1;
   2306 
   2307 			/*
   2308 			 * Update flags. In a number of circumstances,
   2309 			 * we could cluster a lot of these and do a
   2310 			 * number of sequential pages in one go.
   2311 			 */
   2312 			if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   2313 				struct pv_entry *pve;
   2314 				simple_lock(&pg->mdpage.pvh_slock);
   2315 				pve = pmap_remove_pv(pg, pm, sva);
   2316 				pmap_vac_me_harder(pg, pm, 0);
   2317 				simple_unlock(&pg->mdpage.pvh_slock);
   2318 				if (pve != NULL) {
   2319 					if (pm->pm_remove_all == FALSE) {
   2320 						is_exec =
   2321 						   PV_BEEN_EXECD(pve->pv_flags);
   2322 						is_refd =
   2323 						   PV_BEEN_REFD(pve->pv_flags);
   2324 					}
   2325 					pool_put(&pmap_pv_pool, pve);
   2326 				}
   2327 			}
   2328 
   2329 			if (!l2pte_valid(pte)) {
   2330 				*ptep = 0;
   2331 				PTE_SYNC_CURRENT(pm, ptep);
   2332 				sva += PAGE_SIZE;
   2333 				ptep++;
   2334 				mappings++;
   2335 				continue;
   2336 			}
   2337 
   2338 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2339 				/* Add to the clean list. */
   2340 				cleanlist[cleanlist_idx].pte = ptep;
   2341 				cleanlist[cleanlist_idx].va =
   2342 				    sva | (is_exec & 1);
   2343 				cleanlist_idx++;
   2344 			} else
   2345 			if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2346 				/* Nuke everything if needed. */
   2347 				pmap_idcache_wbinv_all(pm);
   2348 				pmap_tlb_flushID(pm);
   2349 
   2350 				/*
   2351 				 * Roll back the previous PTE list,
   2352 				 * and zero out the current PTE.
   2353 				 */
   2354 				for (cnt = 0;
   2355 				     cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   2356 					*cleanlist[cnt].pte = 0;
   2357 				}
   2358 				*ptep = 0;
   2359 				PTE_SYNC(ptep);
   2360 				cleanlist_idx++;
   2361 				pm->pm_remove_all = TRUE;
   2362 			} else {
   2363 				*ptep = 0;
   2364 				PTE_SYNC(ptep);
   2365 				if (pm->pm_remove_all == FALSE) {
   2366 					if (is_exec)
   2367 						pmap_tlb_flushID_SE(pm, sva);
   2368 					else
   2369 					if (is_refd)
   2370 						pmap_tlb_flushD_SE(pm, sva);
   2371 				}
   2372 			}
   2373 
   2374 			sva += PAGE_SIZE;
   2375 			ptep++;
   2376 			mappings++;
   2377 		}
   2378 
   2379 		/*
   2380 		 * Deal with any left overs
   2381 		 */
   2382 		if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2383 			total += cleanlist_idx;
   2384 			for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   2385 				if (pm->pm_cstate.cs_all != 0) {
   2386 					vaddr_t clva = cleanlist[cnt].va & ~1;
   2387 					if (cleanlist[cnt].va & 1) {
   2388 						pmap_idcache_wbinv_range(pm,
   2389 						    clva, PAGE_SIZE);
   2390 						pmap_tlb_flushID_SE(pm, clva);
   2391 					} else {
   2392 						pmap_dcache_wb_range(pm,
   2393 						    clva, PAGE_SIZE, TRUE,
   2394 						    FALSE);
   2395 						pmap_tlb_flushD_SE(pm, clva);
   2396 					}
   2397 				}
   2398 				*cleanlist[cnt].pte = 0;
   2399 				PTE_SYNC_CURRENT(pm, cleanlist[cnt].pte);
   2400 			}
   2401 
   2402 			/*
   2403 			 * If it looks like we're removing a whole bunch
   2404 			 * of mappings, it's faster to just write-back
   2405 			 * the whole cache now and defer TLB flushes until
   2406 			 * pmap_update() is called.
   2407 			 */
   2408 			if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
   2409 				cleanlist_idx = 0;
   2410 			else {
   2411 				cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   2412 				pmap_idcache_wbinv_all(pm);
   2413 				pm->pm_remove_all = TRUE;
   2414 			}
   2415 		}
   2416 
   2417 		pmap_free_l2_bucket(pm, l2b, mappings);
   2418 	}
   2419 
   2420 	pmap_release_pmap_lock(pm);
   2421 	PMAP_MAP_TO_HEAD_UNLOCK();
   2422 }
   2423 
   2424 /*
   2425  * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
   2426  *
   2427  * We assume there is already sufficient KVM space available
   2428  * to do this, as we can't allocate L2 descriptor tables/metadata
   2429  * from here.
   2430  */
   2431 void
   2432 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
   2433 {
   2434 	struct l2_bucket *l2b;
   2435 	pt_entry_t *ptep, opte;
   2436 
   2437 	NPDEBUG(PDB_KENTER,
   2438 	    printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n",
   2439 	    va, pa, prot));
   2440 
   2441 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   2442 	KDASSERT(l2b != NULL);
   2443 
   2444 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   2445 	opte = *ptep;
   2446 
   2447 	if (l2pte_valid(opte)) {
   2448 		cpu_dcache_wbinv_range(va, PAGE_SIZE);
   2449 		cpu_tlb_flushD_SE(va);
   2450 		cpu_cpwait();
   2451 	} else
   2452 	if (opte == 0)
   2453 		l2b->l2b_occupancy++;
   2454 
   2455 	*ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) |
   2456 	    pte_l2_s_cache_mode;
   2457 	PTE_SYNC(ptep);
   2458 }
   2459 
   2460 void
   2461 pmap_kremove(vaddr_t va, vsize_t len)
   2462 {
   2463 	struct l2_bucket *l2b;
   2464 	pt_entry_t *ptep, *sptep, opte;
   2465 	vaddr_t next_bucket, eva;
   2466 	u_int mappings;
   2467 
   2468 	NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n",
   2469 	    va, len));
   2470 
   2471 	eva = va + len;
   2472 
   2473 	while (va < eva) {
   2474 		next_bucket = L2_NEXT_BUCKET(va);
   2475 		if (next_bucket > eva)
   2476 			next_bucket = eva;
   2477 
   2478 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   2479 		KDASSERT(l2b != NULL);
   2480 
   2481 		sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
   2482 		mappings = 0;
   2483 
   2484 		while (va < next_bucket) {
   2485 			opte = *ptep;
   2486 			if (l2pte_valid(opte)) {
   2487 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   2488 				cpu_tlb_flushD_SE(va);
   2489 			}
   2490 			if (opte) {
   2491 				*ptep = 0;
   2492 				mappings++;
   2493 			}
   2494 			va += PAGE_SIZE;
   2495 			ptep++;
   2496 		}
   2497 		KDASSERT(mappings <= l2b->l2b_occupancy);
   2498 		l2b->l2b_occupancy -= mappings;
   2499 		PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
   2500 	}
   2501 	cpu_cpwait();
   2502 }
   2503 
   2504 boolean_t
   2505 pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
   2506 {
   2507 	struct l2_dtable *l2;
   2508 	pd_entry_t *pl1pd, l1pd;
   2509 	pt_entry_t *ptep, pte;
   2510 	paddr_t pa;
   2511 	u_int l1idx;
   2512 
   2513 	pmap_acquire_pmap_lock(pm);
   2514 
   2515 	l1idx = L1_IDX(va);
   2516 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   2517 	l1pd = *pl1pd;
   2518 
   2519 	if (l1pte_section_p(l1pd)) {
   2520 		/*
   2521 		 * These should only happen for pmap_kernel()
   2522 		 */
   2523 		KDASSERT(pm == pmap_kernel());
   2524 		pmap_release_pmap_lock(pm);
   2525 		pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
   2526 	} else {
   2527 		/*
   2528 		 * Note that we can't rely on the validity of the L1
   2529 		 * descriptor as an indication that a mapping exists.
   2530 		 * We have to look it up in the L2 dtable.
   2531 		 */
   2532 		l2 = pm->pm_l2[L2_IDX(l1idx)];
   2533 
   2534 		if (l2 == NULL ||
   2535 		    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   2536 			pmap_release_pmap_lock(pm);
   2537 			return (FALSE);
   2538 		}
   2539 
   2540 		ptep = &ptep[l2pte_index(va)];
   2541 		pte = *ptep;
   2542 		pmap_release_pmap_lock(pm);
   2543 
   2544 		if (pte == 0)
   2545 			return (FALSE);
   2546 
   2547 		switch (pte & L2_TYPE_MASK) {
   2548 		case L2_TYPE_L:
   2549 			pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
   2550 			break;
   2551 
   2552 		default:
   2553 			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
   2554 			break;
   2555 		}
   2556 	}
   2557 
   2558 	if (pap != NULL)
   2559 		*pap = pa;
   2560 
   2561 	return (TRUE);
   2562 }
   2563 
   2564 void
   2565 pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   2566 {
   2567 	struct l2_bucket *l2b;
   2568 	pt_entry_t *ptep, pte;
   2569 	vaddr_t next_bucket;
   2570 	u_int flags;
   2571 	int flush;
   2572 
   2573 	NPDEBUG(PDB_PROTECT,
   2574 	    printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
   2575 	    pm, sva, eva, prot));
   2576 
   2577 	if ((prot & VM_PROT_READ) == 0) {
   2578 		pmap_remove(pm, sva, eva);
   2579 		return;
   2580 	}
   2581 
   2582 	if (prot & VM_PROT_WRITE) {
   2583 		/*
   2584 		 * If this is a read->write transition, just ignore it and let
   2585 		 * uvm_fault() take care of it later.
   2586 		 */
   2587 		return;
   2588 	}
   2589 
   2590 	PMAP_MAP_TO_HEAD_LOCK();
   2591 	pmap_acquire_pmap_lock(pm);
   2592 
   2593 	/*
   2594 	 * OK, at this point, we know we're doing write-protect operation.
   2595 	 * If the pmap is active, write-back the range.
   2596 	 */
   2597 	pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
   2598 
   2599 	flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
   2600 	flags = 0;
   2601 
   2602 	while (sva < eva) {
   2603 		next_bucket = L2_NEXT_BUCKET(sva);
   2604 		if (next_bucket > eva)
   2605 			next_bucket = eva;
   2606 
   2607 		l2b = pmap_get_l2_bucket(pm, sva);
   2608 		if (l2b == NULL) {
   2609 			sva = next_bucket;
   2610 			continue;
   2611 		}
   2612 
   2613 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   2614 
   2615 		while (sva < next_bucket) {
   2616 			if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
   2617 				struct vm_page *pg;
   2618 				u_int f;
   2619 
   2620 				pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
   2621 				pte &= ~L2_S_PROT_W;
   2622 				*ptep = pte;
   2623 				PTE_SYNC(ptep);
   2624 
   2625 				if (pg != NULL) {
   2626 					simple_lock(&pg->mdpage.pvh_slock);
   2627 					f = pmap_modify_pv(pg, pm, sva,
   2628 					    PVF_WRITE, 0);
   2629 					pmap_vac_me_harder(pg, pm, sva);
   2630 					simple_unlock(&pg->mdpage.pvh_slock);
   2631 				} else
   2632 					f = PVF_REF | PVF_EXEC;
   2633 
   2634 				if (flush >= 0) {
   2635 					flush++;
   2636 					flags |= f;
   2637 				} else
   2638 				if (PV_BEEN_EXECD(f))
   2639 					pmap_tlb_flushID_SE(pm, sva);
   2640 				else
   2641 				if (PV_BEEN_REFD(f))
   2642 					pmap_tlb_flushD_SE(pm, sva);
   2643 			}
   2644 
   2645 			sva += PAGE_SIZE;
   2646 			ptep++;
   2647 		}
   2648 	}
   2649 
   2650 	pmap_release_pmap_lock(pm);
   2651 	PMAP_MAP_TO_HEAD_UNLOCK();
   2652 
   2653 	if (flush) {
   2654 		if (PV_BEEN_EXECD(flags))
   2655 			pmap_tlb_flushID(pm);
   2656 		else
   2657 		if (PV_BEEN_REFD(flags))
   2658 			pmap_tlb_flushD(pm);
   2659 	}
   2660 }
   2661 
   2662 void
   2663 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   2664 {
   2665 
   2666 	NPDEBUG(PDB_PROTECT,
   2667 	    printf("pmap_page_protect: pg %p (0x%08lx), prot 0x%x\n",
   2668 	    pg, pg->phys_addr, prot));
   2669 
   2670 	switch(prot) {
   2671 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   2672 	case VM_PROT_READ|VM_PROT_WRITE:
   2673 		return;
   2674 
   2675 	case VM_PROT_READ:
   2676 	case VM_PROT_READ|VM_PROT_EXECUTE:
   2677 		pmap_clearbit(pg, PVF_WRITE);
   2678 		break;
   2679 
   2680 	default:
   2681 		pmap_page_remove(pg);
   2682 		break;
   2683 	}
   2684 }
   2685 
   2686 /*
   2687  * pmap_clear_modify:
   2688  *
   2689  *	Clear the "modified" attribute for a page.
   2690  */
   2691 boolean_t
   2692 pmap_clear_modify(struct vm_page *pg)
   2693 {
   2694 	boolean_t rv;
   2695 
   2696 	if (pg->mdpage.pvh_attrs & PVF_MOD) {
   2697 		rv = TRUE;
   2698 		pmap_clearbit(pg, PVF_MOD);
   2699 	} else
   2700 		rv = FALSE;
   2701 
   2702 	return (rv);
   2703 }
   2704 
   2705 /*
   2706  * pmap_clear_reference:
   2707  *
   2708  *	Clear the "referenced" attribute for a page.
   2709  */
   2710 boolean_t
   2711 pmap_clear_reference(struct vm_page *pg)
   2712 {
   2713 	boolean_t rv;
   2714 
   2715 	if (pg->mdpage.pvh_attrs & PVF_REF) {
   2716 		rv = TRUE;
   2717 		pmap_clearbit(pg, PVF_REF);
   2718 	} else
   2719 		rv = FALSE;
   2720 
   2721 	return (rv);
   2722 }
   2723 
   2724 /*
   2725  * pmap_is_modified:
   2726  *
   2727  *	Test if a page has the "modified" attribute.
   2728  */
   2729 /* See <arm/arm32/pmap.h> */
   2730 
   2731 /*
   2732  * pmap_is_referenced:
   2733  *
   2734  *	Test if a page has the "referenced" attribute.
   2735  */
   2736 /* See <arm/arm32/pmap.h> */
   2737 
   2738 int
   2739 pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
   2740 {
   2741 	struct l2_dtable *l2;
   2742 	struct l2_bucket *l2b;
   2743 	pd_entry_t *pl1pd, l1pd;
   2744 	pt_entry_t *ptep, pte;
   2745 	paddr_t pa;
   2746 	u_int l1idx;
   2747 	int rv = 0;
   2748 
   2749 	PMAP_MAP_TO_HEAD_LOCK();
   2750 	pmap_acquire_pmap_lock(pm);
   2751 
   2752 	l1idx = L1_IDX(va);
   2753 
   2754 	/*
   2755 	 * If there is no l2_dtable for this address, then the process
   2756 	 * has no business accessing it.
   2757 	 *
   2758 	 * Note: This will catch userland processes trying to access
   2759 	 * kernel addresses.
   2760 	 */
   2761 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   2762 	if (l2 == NULL)
   2763 		goto out;
   2764 
   2765 	/*
   2766 	 * Likewise if there is no L2 descriptor table
   2767 	 */
   2768 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   2769 	if (l2b->l2b_kva == NULL)
   2770 		goto out;
   2771 
   2772 	/*
   2773 	 * Check the PTE itself.
   2774 	 */
   2775 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   2776 	pte = *ptep;
   2777 	if (pte == 0)
   2778 		goto out;
   2779 
   2780 	/*
   2781 	 * Catch a userland access to the vector page mapped at 0x0
   2782 	 */
   2783 	if (user && (pte & L2_S_PROT_U) == 0)
   2784 		goto out;
   2785 
   2786 	pa = l2pte_pa(pte);
   2787 
   2788 	if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
   2789 		/*
   2790 		 * This looks like a good candidate for "page modified"
   2791 		 * emulation...
   2792 		 */
   2793 		struct pv_entry *pv;
   2794 		struct vm_page *pg;
   2795 
   2796 		/* Extract the physical address of the page */
   2797 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   2798 			goto out;
   2799 
   2800 		/* Get the current flags for this page. */
   2801 		simple_lock(&pg->mdpage.pvh_slock);
   2802 
   2803 		pv = pmap_find_pv(pg, pm, va);
   2804 		if (pv == NULL) {
   2805 	    		simple_unlock(&pg->mdpage.pvh_slock);
   2806 			goto out;
   2807 		}
   2808 
   2809 		/*
   2810 		 * Do the flags say this page is writable? If not then it
   2811 		 * is a genuine write fault. If yes then the write fault is
   2812 		 * our fault as we did not reflect the write access in the
   2813 		 * PTE. Now we know a write has occurred we can correct this
   2814 		 * and also set the modified bit
   2815 		 */
   2816 		if ((pv->pv_flags & PVF_WRITE) == 0) {
   2817 		    	simple_unlock(&pg->mdpage.pvh_slock);
   2818 			goto out;
   2819 		}
   2820 
   2821 		NPDEBUG(PDB_FOLLOW,
   2822 		    printf("pmap_fault_fixup: mod emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   2823 		    pm, va, pg->phys_addr));
   2824 
   2825 		pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
   2826 		pv->pv_flags |= PVF_REF | PVF_MOD;
   2827 		simple_unlock(&pg->mdpage.pvh_slock);
   2828 
   2829 		/*
   2830 		 * Re-enable write permissions for the page.  No need to call
   2831 		 * pmap_vac_me_harder(), since this is just a
   2832 		 * modified-emulation fault, and the PVF_WRITE bit isn't
   2833 		 * changing. We've already set the cacheable bits based on
   2834 		 * the assumption that we can write to this page.
   2835 		 */
   2836 		*ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
   2837 		PTE_SYNC(ptep);
   2838 		rv = 1;
   2839 	} else
   2840 	if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
   2841 		/*
   2842 		 * This looks like a good candidate for "page referenced"
   2843 		 * emulation.
   2844 		 */
   2845 		struct pv_entry *pv;
   2846 		struct vm_page *pg;
   2847 
   2848 		/* Extract the physical address of the page */
   2849 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   2850 			goto out;
   2851 
   2852 		/* Get the current flags for this page. */
   2853 		simple_lock(&pg->mdpage.pvh_slock);
   2854 
   2855 		pv = pmap_find_pv(pg, pm, va);
   2856 		if (pv == NULL) {
   2857 	    		simple_unlock(&pg->mdpage.pvh_slock);
   2858 			goto out;
   2859 		}
   2860 
   2861 		pg->mdpage.pvh_attrs |= PVF_REF;
   2862 		pv->pv_flags |= PVF_REF;
   2863 		simple_unlock(&pg->mdpage.pvh_slock);
   2864 
   2865 		NPDEBUG(PDB_FOLLOW,
   2866 		    printf("pmap_fault_fixup: ref emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   2867 		    pm, va, pg->phys_addr));
   2868 
   2869 		*ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
   2870 		PTE_SYNC(ptep);
   2871 		rv = 1;
   2872 	}
   2873 
   2874 	/*
   2875 	 * We know there is a valid mapping here, so simply
   2876 	 * fix up the L1 if necessary.
   2877 	 */
   2878 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   2879 	l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
   2880 	if (*pl1pd != l1pd) {
   2881 		*pl1pd = l1pd;
   2882 		PTE_SYNC(pl1pd);
   2883 		rv = 1;
   2884 	}
   2885 
   2886 #ifdef CPU_SA110
   2887 	/*
   2888 	 * There are bugs in the rev K SA110.  This is a check for one
   2889 	 * of them.
   2890 	 */
   2891 	if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
   2892 	    curcpu()->ci_arm_cpurev < 3) {
   2893 		/* Always current pmap */
   2894 		if (l2pte_valid(pte)) {
   2895 			extern int kernel_debug;
   2896 			if (kernel_debug & 1) {
   2897 				struct proc *p = curlwp->l_proc;
   2898 				printf("prefetch_abort: page is already "
   2899 				    "mapped - pte=%p *pte=%08x\n", ptep, pte);
   2900 				printf("prefetch_abort: pc=%08lx proc=%p "
   2901 				    "process=%s\n", va, p, p->p_comm);
   2902 				printf("prefetch_abort: far=%08x fs=%x\n",
   2903 				    cpu_faultaddress(), cpu_faultstatus());
   2904 			}
   2905 #ifdef DDB
   2906 			if (kernel_debug & 2)
   2907 				Debugger();
   2908 #endif
   2909 			rv = 1;
   2910 		}
   2911 	}
   2912 #endif /* CPU_SA110 */
   2913 
   2914 #ifdef DEBUG
   2915 	/*
   2916 	 * If 'rv == 0' at this point, it generally indicates that there is a
   2917 	 * stale TLB entry for the faulting address. This happens when two or
   2918 	 * more processes are sharing an L1. Since we don't flush the TLB on
   2919 	 * a context switch between such processes, we can take domain faults
   2920 	 * for mappings which exist at the same VA in both processes. EVEN IF
   2921 	 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
   2922 	 * example.
   2923 	 *
   2924 	 * This is extremely likely to happen if pmap_enter() updated the L1
   2925 	 * entry for a recently entered mapping. In this case, the TLB is
   2926 	 * flushed for the new mapping, but there may still be TLB entries for
   2927 	 * other mappings belonging to other processes in the 1MB range
   2928 	 * covered by the L1 entry.
   2929 	 *
   2930 	 * Since 'rv == 0', we know that the L1 already contains the correct
   2931 	 * value, so the fault must be due to a stale TLB entry.
   2932 	 *
   2933 	 * Since we always need to flush the TLB anyway in the case where we
   2934 	 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
   2935 	 * stale TLB entries dynamically.
   2936 	 *
   2937 	 * However, the above condition can ONLY happen if the current L1 is
   2938 	 * being shared. If it happens when the L1 is unshared, it indicates
   2939 	 * that other parts of the pmap are not doing their job WRT managing
   2940 	 * the TLB.
   2941 	 */
   2942 	if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
   2943 		extern int last_fault_code;
   2944 		printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
   2945 		    pm, va, ftype);
   2946 		printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
   2947 		    l2, l2b, ptep, pl1pd);
   2948 		printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
   2949 		    pte, l1pd, last_fault_code);
   2950 #ifdef DDB
   2951 		Debugger();
   2952 #endif
   2953 	}
   2954 #endif
   2955 
   2956 	cpu_tlb_flushID_SE(va);
   2957 	cpu_cpwait();
   2958 
   2959 	rv = 1;
   2960 
   2961 out:
   2962 	pmap_release_pmap_lock(pm);
   2963 	PMAP_MAP_TO_HEAD_UNLOCK();
   2964 
   2965 	return (rv);
   2966 }
   2967 
   2968 /*
   2969  * pmap_collect: free resources held by a pmap
   2970  *
   2971  * => optional function.
   2972  * => called when a process is swapped out to free memory.
   2973  */
   2974 void
   2975 pmap_collect(pmap_t pm)
   2976 {
   2977 	/*
   2978 	 * Nothing to do.
   2979 	 * We don't even need to free-up the process' L1.
   2980 	 */
   2981 }
   2982 
   2983 /*
   2984  * Routine:	pmap_procwr
   2985  *
   2986  * Function:
   2987  *	Synchronize caches corresponding to [addr, addr+len) in p.
   2988  *
   2989  */
   2990 void
   2991 pmap_procwr(struct proc *p, vaddr_t va, int len)
   2992 {
   2993 	/* We only need to do anything if it is the current process. */
   2994 	if (p == curproc)
   2995 		cpu_icache_sync_range(va, len);
   2996 }
   2997 
   2998 /*
   2999  * Routine:	pmap_unwire
   3000  * Function:	Clear the wired attribute for a map/virtual-address pair.
   3001  *
   3002  * In/out conditions:
   3003  *		The mapping must already exist in the pmap.
   3004  */
   3005 void
   3006 pmap_unwire(pmap_t pm, vaddr_t va)
   3007 {
   3008 	struct l2_bucket *l2b;
   3009 	pt_entry_t *ptep, pte;
   3010 	struct vm_page *pg;
   3011 	paddr_t pa;
   3012 
   3013 	NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
   3014 
   3015 	PMAP_MAP_TO_HEAD_LOCK();
   3016 	pmap_acquire_pmap_lock(pm);
   3017 
   3018 	l2b = pmap_get_l2_bucket(pm, va);
   3019 	KDASSERT(l2b != NULL);
   3020 
   3021 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3022 	pte = *ptep;
   3023 
   3024 	/* Extract the physical address of the page */
   3025 	pa = l2pte_pa(pte);
   3026 
   3027 	if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   3028 		/* Update the wired bit in the pv entry for this page. */
   3029 		simple_lock(&pg->mdpage.pvh_slock);
   3030 		(void) pmap_modify_pv(pg, pm, va, PVF_WIRED, 0);
   3031 		simple_unlock(&pg->mdpage.pvh_slock);
   3032 	}
   3033 
   3034 	pmap_release_pmap_lock(pm);
   3035 	PMAP_MAP_TO_HEAD_UNLOCK();
   3036 }
   3037 
   3038 void
   3039 pmap_activate(struct lwp *l)
   3040 {
   3041 	pmap_t pm;
   3042 	struct pcb *pcb;
   3043 	int s;
   3044 
   3045 	pm = l->l_proc->p_vmspace->vm_map.pmap;
   3046 	pcb = &l->l_addr->u_pcb;
   3047 
   3048 	pmap_set_pcb_pagedir(pm, pcb);
   3049 
   3050 	if (l == curlwp) {
   3051 		u_int cur_dacr, cur_ttb;
   3052 
   3053 		__asm __volatile("mrc p15, 0, %0, c2, c0, 0" : "=r"(cur_ttb));
   3054 		__asm __volatile("mrc p15, 0, %0, c3, c0, 0" : "=r"(cur_dacr));
   3055 
   3056 		cur_ttb &= ~(L1_TABLE_SIZE - 1);
   3057 
   3058 		if (cur_ttb == (u_int)pcb->pcb_pagedir &&
   3059 		    cur_dacr == pcb->pcb_dacr) {
   3060 			/*
   3061 			 * No need to switch address spaces.
   3062 			 */
   3063 			return;
   3064 		}
   3065 
   3066 		s = splhigh();
   3067 		pmap_acquire_pmap_lock(pm);
   3068 		disable_interrupts(I32_bit | F32_bit);
   3069 
   3070 		/*
   3071 		 * We MUST, I repeat, MUST fix up the L1 entry corresponding
   3072 		 * to 'vector_page' in the incoming L1 table before switching
   3073 		 * to it otherwise subsequent interrupts/exceptions (including
   3074 		 * domain faults!) will jump into hyperspace.
   3075 		 */
   3076 		if (pcb->pcb_pl1vec) {
   3077 			*pcb->pcb_pl1vec = pcb->pcb_l1vec;
   3078 			/*
   3079 			 * Don't need to PTE_SYNC() at this point since
   3080 			 * cpu_setttb() is about to flush both the cache
   3081 			 * and the TLB.
   3082 			 */
   3083 		}
   3084 
   3085 		cpu_domains(pcb->pcb_dacr);
   3086 		cpu_setttb(pcb->pcb_pagedir);
   3087 
   3088 		enable_interrupts(I32_bit | F32_bit);
   3089 
   3090 		/*
   3091 		 * Flag any previous userland pmap as being NOT
   3092 		 * resident in the cache/tlb.
   3093 		 */
   3094 		if (pmap_cache_state && pmap_cache_state != &pm->pm_cstate)
   3095 			pmap_cache_state->cs_all = 0;
   3096 
   3097 		/*
   3098 		 * The new pmap, however, IS resident.
   3099 		 */
   3100 		pmap_cache_state = &pm->pm_cstate;
   3101 		pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3102 		pmap_release_pmap_lock(pm);
   3103 		splx(s);
   3104 	}
   3105 }
   3106 
   3107 void
   3108 pmap_deactivate(struct lwp *l)
   3109 {
   3110 }
   3111 
   3112 void
   3113 pmap_update(pmap_t pm)
   3114 {
   3115 
   3116 	if (pm->pm_remove_all) {
   3117 		/*
   3118 		 * Finish up the pmap_remove_all() optimisation by flushing
   3119 		 * the TLB.
   3120 		 */
   3121 		pmap_tlb_flushID(pm);
   3122 		pm->pm_remove_all = FALSE;
   3123 	}
   3124 
   3125 	if (pmap_is_current(pm)) {
   3126 		/*
   3127 		 * If we're dealing with a current userland pmap, move its L1
   3128 		 * to the end of the LRU.
   3129 		 */
   3130 		if (pm != pmap_kernel())
   3131 			pmap_use_l1(pm);
   3132 
   3133 		/*
   3134 		 * We can assume we're done with frobbing the cache/tlb for
   3135 		 * now. Make sure any future pmap ops don't skip cache/tlb
   3136 		 * flushes.
   3137 		 */
   3138 		pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3139 	}
   3140 
   3141 	/*
   3142 	 * make sure TLB/cache operations have completed.
   3143 	 */
   3144 	cpu_cpwait();
   3145 }
   3146 
   3147 void
   3148 pmap_remove_all(pmap_t pm)
   3149 {
   3150 
   3151 	/*
   3152 	 * The vmspace described by this pmap is about to be torn down.
   3153 	 * Until pmap_update() is called, UVM will only make calls
   3154 	 * to pmap_remove(). We can make life much simpler by flushing
   3155 	 * the cache now, and deferring TLB invalidation to pmap_update().
   3156 	 */
   3157 	pmap_idcache_wbinv_all(pm);
   3158 	pm->pm_remove_all = TRUE;
   3159 }
   3160 
   3161 /*
   3162  * Retire the given physical map from service.
   3163  * Should only be called if the map contains no valid mappings.
   3164  */
   3165 void
   3166 pmap_destroy(pmap_t pm)
   3167 {
   3168 	u_int count;
   3169 
   3170 	if (pm == NULL)
   3171 		return;
   3172 
   3173 	if (pm->pm_remove_all) {
   3174 		pmap_tlb_flushID(pm);
   3175 		pm->pm_remove_all = FALSE;
   3176 	}
   3177 
   3178 	/*
   3179 	 * Drop reference count
   3180 	 */
   3181 	simple_lock(&pm->pm_lock);
   3182 	count = --pm->pm_obj.uo_refs;
   3183 	simple_unlock(&pm->pm_lock);
   3184 	if (count > 0) {
   3185 		if (pmap_is_current(pm)) {
   3186 			if (pm != pmap_kernel())
   3187 				pmap_use_l1(pm);
   3188 			pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3189 		}
   3190 		return;
   3191 	}
   3192 
   3193 	/*
   3194 	 * reference count is zero, free pmap resources and then free pmap.
   3195 	 */
   3196 
   3197 	if (vector_page < KERNEL_BASE) {
   3198 		/* Remove the vector page mapping */
   3199 		pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
   3200 		pmap_update(pm);
   3201 	}
   3202 
   3203 	LIST_REMOVE(pm, pm_list);
   3204 
   3205 	pmap_free_l1(pm);
   3206 
   3207 	/* return the pmap to the pool */
   3208 	pool_cache_put(&pmap_pmap_cache, pm);
   3209 }
   3210 
   3211 
   3212 /*
   3213  * void pmap_reference(pmap_t pm)
   3214  *
   3215  * Add a reference to the specified pmap.
   3216  */
   3217 void
   3218 pmap_reference(pmap_t pm)
   3219 {
   3220 
   3221 	if (pm == NULL)
   3222 		return;
   3223 
   3224 	pmap_use_l1(pm);
   3225 
   3226 	simple_lock(&pm->pm_lock);
   3227 	pm->pm_obj.uo_refs++;
   3228 	simple_unlock(&pm->pm_lock);
   3229 }
   3230 
   3231 /*
   3232  * pmap_zero_page()
   3233  *
   3234  * Zero a given physical page by mapping it at a page hook point.
   3235  * In doing the zero page op, the page we zero is mapped cachable, as with
   3236  * StrongARM accesses to non-cached pages are non-burst making writing
   3237  * _any_ bulk data very slow.
   3238  */
   3239 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
   3240 void
   3241 pmap_zero_page_generic(paddr_t phys)
   3242 {
   3243 #ifdef DEBUG
   3244 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   3245 
   3246 	if (pg->mdpage.pvh_list != NULL)
   3247 		panic("pmap_zero_page: page has mappings");
   3248 #endif
   3249 
   3250 	KDASSERT((phys & PGOFSET) == 0);
   3251 
   3252 	/*
   3253 	 * Hook in the page, zero it, and purge the cache for that
   3254 	 * zeroed page. Invalidate the TLB as needed.
   3255 	 */
   3256 	*cdst_pte = L2_S_PROTO | phys |
   3257 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   3258 	PTE_SYNC(cdst_pte);
   3259 	cpu_tlb_flushD_SE(cdstp);
   3260 	cpu_cpwait();
   3261 	bzero_page(cdstp);
   3262 	cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
   3263 }
   3264 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
   3265 
   3266 #if ARM_MMU_XSCALE == 1
   3267 void
   3268 pmap_zero_page_xscale(paddr_t phys)
   3269 {
   3270 #ifdef DEBUG
   3271 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   3272 
   3273 	if (pg->mdpage.pvh_list != NULL)
   3274 		panic("pmap_zero_page: page has mappings");
   3275 #endif
   3276 
   3277 	KDASSERT((phys & PGOFSET) == 0);
   3278 
   3279 	/*
   3280 	 * Hook in the page, zero it, and purge the cache for that
   3281 	 * zeroed page. Invalidate the TLB as needed.
   3282 	 */
   3283 	*cdst_pte = L2_S_PROTO | phys |
   3284 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   3285 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   3286 	PTE_SYNC(cdst_pte);
   3287 	cpu_tlb_flushD_SE(cdstp);
   3288 	cpu_cpwait();
   3289 	bzero_page(cdstp);
   3290 	xscale_cache_clean_minidata();
   3291 }
   3292 #endif /* ARM_MMU_XSCALE == 1 */
   3293 
   3294 /* pmap_pageidlezero()
   3295  *
   3296  * The same as above, except that we assume that the page is not
   3297  * mapped.  This means we never have to flush the cache first.  Called
   3298  * from the idle loop.
   3299  */
   3300 boolean_t
   3301 pmap_pageidlezero(paddr_t phys)
   3302 {
   3303 	unsigned int i;
   3304 	int *ptr;
   3305 	boolean_t rv = TRUE;
   3306 #ifdef DEBUG
   3307 	struct vm_page *pg;
   3308 
   3309 	pg = PHYS_TO_VM_PAGE(phys);
   3310 	if (pg->mdpage.pvh_list != NULL)
   3311 		panic("pmap_pageidlezero: page has mappings");
   3312 #endif
   3313 
   3314 	KDASSERT((phys & PGOFSET) == 0);
   3315 
   3316 	/*
   3317 	 * Hook in the page, zero it, and purge the cache for that
   3318 	 * zeroed page. Invalidate the TLB as needed.
   3319 	 */
   3320 	*cdst_pte = L2_S_PROTO | phys |
   3321 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   3322 	PTE_SYNC(cdst_pte);
   3323 	cpu_tlb_flushD_SE(cdstp);
   3324 	cpu_cpwait();
   3325 
   3326 	for (i = 0, ptr = (int *)cdstp;
   3327 			i < (PAGE_SIZE / sizeof(int)); i++) {
   3328 		if (sched_whichqs != 0) {
   3329 			/*
   3330 			 * A process has become ready.  Abort now,
   3331 			 * so we don't keep it waiting while we
   3332 			 * do slow memory access to finish this
   3333 			 * page.
   3334 			 */
   3335 			rv = FALSE;
   3336 			break;
   3337 		}
   3338 		*ptr++ = 0;
   3339 	}
   3340 
   3341 	if (rv)
   3342 		/*
   3343 		 * if we aborted we'll rezero this page again later so don't
   3344 		 * purge it unless we finished it
   3345 		 */
   3346 		cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
   3347 
   3348 	return (rv);
   3349 }
   3350 
   3351 /*
   3352  * pmap_copy_page()
   3353  *
   3354  * Copy one physical page into another, by mapping the pages into
   3355  * hook points. The same comment regarding cachability as in
   3356  * pmap_zero_page also applies here.
   3357  */
   3358 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
   3359 void
   3360 pmap_copy_page_generic(paddr_t src, paddr_t dst)
   3361 {
   3362 	struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
   3363 #ifdef DEBUG
   3364 	struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
   3365 
   3366 	if (dst_pg->mdpage.pvh_list != NULL)
   3367 		panic("pmap_copy_page: dst page has mappings");
   3368 #endif
   3369 
   3370 	KDASSERT((src & PGOFSET) == 0);
   3371 	KDASSERT((dst & PGOFSET) == 0);
   3372 
   3373 	/*
   3374 	 * Clean the source page.  Hold the source page's lock for
   3375 	 * the duration of the copy so that no other mappings can
   3376 	 * be created while we have a potentially aliased mapping.
   3377 	 */
   3378 	simple_lock(&src_pg->mdpage.pvh_slock);
   3379 	(void) pmap_clean_page(src_pg->mdpage.pvh_list, TRUE);
   3380 
   3381 	/*
   3382 	 * Map the pages into the page hook points, copy them, and purge
   3383 	 * the cache for the appropriate page. Invalidate the TLB
   3384 	 * as required.
   3385 	 */
   3386 	*csrc_pte = L2_S_PROTO | src |
   3387 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
   3388 	PTE_SYNC(csrc_pte);
   3389 	*cdst_pte = L2_S_PROTO | dst |
   3390 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   3391 	PTE_SYNC(cdst_pte);
   3392 	cpu_tlb_flushD_SE(csrcp);
   3393 	cpu_tlb_flushD_SE(cdstp);
   3394 	cpu_cpwait();
   3395 	bcopy_page(csrcp, cdstp);
   3396 	cpu_dcache_inv_range(csrcp, PAGE_SIZE);
   3397 	simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
   3398 	cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
   3399 }
   3400 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
   3401 
   3402 #if ARM_MMU_XSCALE == 1
   3403 void
   3404 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
   3405 {
   3406 	struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
   3407 #ifdef DEBUG
   3408 	struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
   3409 
   3410 	if (dst_pg->mdpage.pvh_list != NULL)
   3411 		panic("pmap_copy_page: dst page has mappings");
   3412 #endif
   3413 
   3414 	KDASSERT((src & PGOFSET) == 0);
   3415 	KDASSERT((dst & PGOFSET) == 0);
   3416 
   3417 	/*
   3418 	 * Clean the source page.  Hold the source page's lock for
   3419 	 * the duration of the copy so that no other mappings can
   3420 	 * be created while we have a potentially aliased mapping.
   3421 	 */
   3422 	simple_lock(&src_pg->mdpage.pvh_slock);
   3423 	(void) pmap_clean_page(src_pg->mdpage.pvh_list, TRUE);
   3424 
   3425 	/*
   3426 	 * Map the pages into the page hook points, copy them, and purge
   3427 	 * the cache for the appropriate page. Invalidate the TLB
   3428 	 * as required.
   3429 	 */
   3430 	*csrc_pte = L2_S_PROTO | src |
   3431 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   3432 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   3433 	PTE_SYNC(csrc_pte);
   3434 	*cdst_pte = L2_S_PROTO | dst |
   3435 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   3436 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   3437 	PTE_SYNC(cdst_pte);
   3438 	cpu_tlb_flushD_SE(csrcp);
   3439 	cpu_tlb_flushD_SE(cdstp);
   3440 	cpu_cpwait();
   3441 	bcopy_page(csrcp, cdstp);
   3442 	simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
   3443 	xscale_cache_clean_minidata();
   3444 }
   3445 #endif /* ARM_MMU_XSCALE == 1 */
   3446 
   3447 /*
   3448  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   3449  *
   3450  * Return the start and end addresses of the kernel's virtual space.
   3451  * These values are setup in pmap_bootstrap and are updated as pages
   3452  * are allocated.
   3453  */
   3454 void
   3455 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   3456 {
   3457 	*start = virtual_avail;
   3458 	*end = virtual_end;
   3459 }
   3460 
   3461 /*
   3462  * Helper function for pmap_grow_l2_bucket()
   3463  */
   3464 static __inline int
   3465 pmap_grow_map(vaddr_t va, pt_entry_t cache_mode, paddr_t *pap)
   3466 {
   3467 	struct l2_bucket *l2b;
   3468 	pt_entry_t *ptep;
   3469 	paddr_t pa;
   3470 
   3471 	if (uvm.page_init_done == FALSE) {
   3472 		if (uvm_page_physget(&pa) == FALSE)
   3473 			return (1);
   3474 	} else {
   3475 		struct vm_page *pg;
   3476 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
   3477 		if (pg == NULL)
   3478 			return (1);
   3479 		pa = VM_PAGE_TO_PHYS(pg);
   3480 	}
   3481 
   3482 	if (pap)
   3483 		*pap = pa;
   3484 
   3485 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   3486 	KDASSERT(l2b != NULL);
   3487 
   3488 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3489 	*ptep = L2_S_PROTO | pa | cache_mode |
   3490 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
   3491 	PTE_SYNC(ptep);
   3492 	memset((void *)va, 0, PAGE_SIZE);
   3493 	return (0);
   3494 }
   3495 
   3496 /*
   3497  * This is the same as pmap_alloc_l2_bucket(), except that it is only
   3498  * used by pmap_growkernel().
   3499  */
   3500 static __inline struct l2_bucket *
   3501 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
   3502 {
   3503 	struct l2_dtable *l2;
   3504 	struct l2_bucket *l2b;
   3505 	u_short l1idx;
   3506 	vaddr_t nva;
   3507 
   3508 	l1idx = L1_IDX(va);
   3509 
   3510 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   3511 		/*
   3512 		 * No mapping at this address, as there is
   3513 		 * no entry in the L1 table.
   3514 		 * Need to allocate a new l2_dtable.
   3515 		 */
   3516 		nva = pmap_kernel_l2dtable_kva;
   3517 		if ((nva & PGOFSET) == 0) {
   3518 			/*
   3519 			 * Need to allocate a backing page
   3520 			 */
   3521 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   3522 				return (NULL);
   3523 		}
   3524 
   3525 		l2 = (struct l2_dtable *)nva;
   3526 		nva += sizeof(struct l2_dtable);
   3527 
   3528 		if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
   3529 			/*
   3530 			 * The new l2_dtable straddles a page boundary.
   3531 			 * Map in another page to cover it.
   3532 			 */
   3533 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   3534 				return (NULL);
   3535 		}
   3536 
   3537 		pmap_kernel_l2dtable_kva = nva;
   3538 
   3539 		/*
   3540 		 * Link it into the parent pmap
   3541 		 */
   3542 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   3543 	}
   3544 
   3545 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   3546 
   3547 	/*
   3548 	 * Fetch pointer to the L2 page table associated with the address.
   3549 	 */
   3550 	if (l2b->l2b_kva == NULL) {
   3551 		pt_entry_t *ptep;
   3552 
   3553 		/*
   3554 		 * No L2 page table has been allocated. Chances are, this
   3555 		 * is because we just allocated the l2_dtable, above.
   3556 		 */
   3557 		nva = pmap_kernel_l2ptp_kva;
   3558 		ptep = (pt_entry_t *)nva;
   3559 		if ((nva & PGOFSET) == 0) {
   3560 			/*
   3561 			 * Need to allocate a backing page
   3562 			 */
   3563 			if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
   3564 			    &pmap_kernel_l2ptp_phys))
   3565 				return (NULL);
   3566 			PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
   3567 		}
   3568 
   3569 		l2->l2_occupancy++;
   3570 		l2b->l2b_kva = ptep;
   3571 		l2b->l2b_l1idx = l1idx;
   3572 		l2b->l2b_phys = pmap_kernel_l2ptp_phys;
   3573 
   3574 		pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
   3575 		pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
   3576 	}
   3577 
   3578 	return (l2b);
   3579 }
   3580 
   3581 vaddr_t
   3582 pmap_growkernel(vaddr_t maxkvaddr)
   3583 {
   3584 	pmap_t kpm = pmap_kernel();
   3585 	struct l1_ttable *l1;
   3586 	struct l2_bucket *l2b;
   3587 	pd_entry_t *pl1pd;
   3588 	int s;
   3589 
   3590 	if (maxkvaddr <= pmap_curmaxkvaddr)
   3591 		goto out;		/* we are OK */
   3592 
   3593 	NPDEBUG(PDB_GROWKERN,
   3594 	    printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
   3595 	    pmap_curmaxkvaddr, maxkvaddr));
   3596 
   3597 	KDASSERT(maxkvaddr <= virtual_end);
   3598 
   3599 	/*
   3600 	 * whoops!   we need to add kernel PTPs
   3601 	 */
   3602 
   3603 	s = splhigh();	/* to be safe */
   3604 	simple_lock(&kpm->pm_lock);
   3605 
   3606 	/* Map 1MB at a time */
   3607 	for (; pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE) {
   3608 
   3609 		l2b = pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
   3610 		KDASSERT(l2b != NULL);
   3611 
   3612 		/* Distribute new L1 entry to all other L1s */
   3613 		SLIST_FOREACH(l1, &l1_list, l1_link) {
   3614 			pl1pd = &l1->l1_kva[L1_IDX(pmap_curmaxkvaddr)];
   3615 			*pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
   3616 			    L1_C_PROTO;
   3617 			PTE_SYNC(pl1pd);
   3618 		}
   3619 	}
   3620 
   3621 	/*
   3622 	 * flush out the cache, expensive but growkernel will happen so
   3623 	 * rarely
   3624 	 */
   3625 	cpu_dcache_wbinv_all();
   3626 	cpu_tlb_flushD();
   3627 	cpu_cpwait();
   3628 
   3629 	simple_unlock(&kpm->pm_lock);
   3630 	splx(s);
   3631 
   3632 out:
   3633 	return (pmap_curmaxkvaddr);
   3634 }
   3635 
   3636 /************************ Utility routines ****************************/
   3637 
   3638 /*
   3639  * vector_page_setprot:
   3640  *
   3641  *	Manipulate the protection of the vector page.
   3642  */
   3643 void
   3644 vector_page_setprot(int prot)
   3645 {
   3646 	struct l2_bucket *l2b;
   3647 	pt_entry_t *ptep;
   3648 
   3649 	l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
   3650 	KDASSERT(l2b != NULL);
   3651 
   3652 	ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
   3653 
   3654 	*ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
   3655 	PTE_SYNC(ptep);
   3656 	cpu_tlb_flushD_SE(vector_page);
   3657 	cpu_cpwait();
   3658 }
   3659 
   3660 /*
   3661  * This is used to stuff certain critical values into the PCB where they
   3662  * can be accessed quickly from cpu_switch() et al.
   3663  */
   3664 void
   3665 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
   3666 {
   3667 	struct l2_bucket *l2b;
   3668 
   3669 	KDASSERT(pm->pm_l1);
   3670 
   3671 	pcb->pcb_pagedir = pm->pm_l1->l1_physaddr;
   3672 	pcb->pcb_dacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
   3673 	    (DOMAIN_CLIENT << (pm->pm_domain * 2));
   3674 	pcb->pcb_cstate = (void *)&pm->pm_cstate;
   3675 
   3676 	if (vector_page < KERNEL_BASE) {
   3677 		pcb->pcb_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
   3678 		l2b = pmap_get_l2_bucket(pm, vector_page);
   3679 		pcb->pcb_l1vec = l2b->l2b_phys | L1_C_PROTO |
   3680 		    L1_C_DOM(pm->pm_domain);
   3681 	} else
   3682 		pcb->pcb_pl1vec = NULL;
   3683 }
   3684 
   3685 /*
   3686  * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
   3687  * Returns TRUE if the mapping exists, else FALSE.
   3688  *
   3689  * NOTE: This function is only used by a couple of arm-specific modules.
   3690  * It is not safe to take any pmap locks here, since we could be right
   3691  * in the middle of debugging the pmap anyway...
   3692  *
   3693  * It is possible for this routine to return FALSE even though a valid
   3694  * mapping does exist. This is because we don't lock, so the metadata
   3695  * state may be inconsistent.
   3696  *
   3697  * NOTE: We can return a NULL *ptp in the case where the L1 pde is
   3698  * a "section" mapping.
   3699  */
   3700 boolean_t
   3701 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
   3702 {
   3703 	struct l2_dtable *l2;
   3704 	pd_entry_t *pl1pd, l1pd;
   3705 	pt_entry_t *ptep;
   3706 	u_short l1idx;
   3707 
   3708 	if (pm->pm_l1 == NULL)
   3709 		return (FALSE);
   3710 
   3711 	l1idx = L1_IDX(va);
   3712 	*pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
   3713 	l1pd = *pl1pd;
   3714 
   3715 	if (l1pte_section_p(l1pd)) {
   3716 		*ptp = NULL;
   3717 		return (TRUE);
   3718 	}
   3719 
   3720 	if (pm->pm_l2 == NULL)
   3721 		return (FALSE);
   3722 
   3723 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   3724 
   3725 	if (l2 == NULL ||
   3726 	    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   3727 		return (FALSE);
   3728 	}
   3729 
   3730 	*ptp = &ptep[l2pte_index(va)];
   3731 	return (TRUE);
   3732 }
   3733 
   3734 boolean_t
   3735 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
   3736 {
   3737 	u_short l1idx;
   3738 
   3739 	if (pm->pm_l1 == NULL)
   3740 		return (FALSE);
   3741 
   3742 	l1idx = L1_IDX(va);
   3743 	*pdp = &pm->pm_l1->l1_kva[l1idx];
   3744 
   3745 	return (TRUE);
   3746 }
   3747 
   3748 /************************ Bootstrapping routines ****************************/
   3749 
   3750 static void
   3751 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
   3752 {
   3753 	int i;
   3754 
   3755 	l1->l1_kva = l1pt;
   3756 	l1->l1_domain_use_count = 0;
   3757 	l1->l1_domain_first = 0;
   3758 
   3759 	for (i = 0; i < PMAP_DOMAINS; i++)
   3760 		l1->l1_domain_free[i] = i + 1;
   3761 
   3762 	/*
   3763 	 * Copy the kernel's L1 entries to each new L1.
   3764 	 */
   3765 	if (pmap_initialized)
   3766 		memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
   3767 
   3768 	if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
   3769 	    &l1->l1_physaddr) == FALSE)
   3770 		panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
   3771 
   3772 	SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
   3773 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   3774 }
   3775 
   3776 /*
   3777  * pmap_bootstrap() is called from the board-specific initarm() routine
   3778  * once the kernel L1/L2 descriptors tables have been set up.
   3779  *
   3780  * This is a somewhat convoluted process since pmap bootstrap is, effectively,
   3781  * spread over a number of disparate files/functions.
   3782  *
   3783  * We are passed the following parameters
   3784  *  - kernel_l1pt
   3785  *    This is a pointer to the base of the kernel's L1 translation table.
   3786  *  - vstart
   3787  *    1MB-aligned start of managed kernel virtual memory.
   3788  *  - vend
   3789  *    1MB-aligned end of managed kernel virtual memory.
   3790  *
   3791  * We use the first parameter to build the metadata (struct l1_ttable and
   3792  * struct l2_dtable) necessary to track kernel mappings.
   3793  */
   3794 #define	PMAP_STATIC_L2_SIZE 16
   3795 void
   3796 pmap_bootstrap(pd_entry_t *kernel_l1pt, vaddr_t vstart, vaddr_t vend)
   3797 {
   3798 	static struct l1_ttable static_l1;
   3799 	static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
   3800 	struct l1_ttable *l1 = &static_l1;
   3801 	struct l2_dtable *l2;
   3802 	struct l2_bucket *l2b;
   3803 	pmap_t pm = pmap_kernel();
   3804 	pd_entry_t pde;
   3805 	pt_entry_t *ptep;
   3806 	paddr_t pa;
   3807 	vaddr_t va;
   3808 	vsize_t size;
   3809 	int l1idx, l2idx, l2next = 0;
   3810 
   3811 	/*
   3812 	 * Initialise the kernel pmap object
   3813 	 */
   3814 	pm->pm_l1 = l1;
   3815 	pm->pm_domain = PMAP_DOMAIN_KERNEL;
   3816 	pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3817 	simple_lock_init(&pm->pm_lock);
   3818 	pm->pm_obj.pgops = NULL;
   3819 	TAILQ_INIT(&pm->pm_obj.memq);
   3820 	pm->pm_obj.uo_npages = 0;
   3821 	pm->pm_obj.uo_refs = 1;
   3822 
   3823 	/*
   3824 	 * Scan the L1 translation table created by initarm() and create
   3825 	 * the required metadata for all valid mappings found in it.
   3826 	 */
   3827 	for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
   3828 		pde = kernel_l1pt[l1idx];
   3829 
   3830 		/*
   3831 		 * We're only interested in Coarse mappings.
   3832 		 * pmap_extract() can deal with section mappings without
   3833 		 * recourse to checking L2 metadata.
   3834 		 */
   3835 		if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
   3836 			continue;
   3837 
   3838 		/*
   3839 		 * Lookup the KVA of this L2 descriptor table
   3840 		 */
   3841 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   3842 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   3843 		if (ptep == NULL) {
   3844 			panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
   3845 			    (u_int)l1idx << L1_S_SHIFT, pa);
   3846 		}
   3847 
   3848 		/*
   3849 		 * Fetch the associated L2 metadata structure.
   3850 		 * Allocate a new one if necessary.
   3851 		 */
   3852 		if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   3853 			if (l2next == PMAP_STATIC_L2_SIZE)
   3854 				panic("pmap_bootstrap: out of static L2s");
   3855 			pm->pm_l2[L2_IDX(l1idx)] = l2 = &static_l2[l2next++];
   3856 		}
   3857 
   3858 		/*
   3859 		 * One more L1 slot tracked...
   3860 		 */
   3861 		l2->l2_occupancy++;
   3862 
   3863 		/*
   3864 		 * Fill in the details of the L2 descriptor in the
   3865 		 * appropriate bucket.
   3866 		 */
   3867 		l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   3868 		l2b->l2b_kva = ptep;
   3869 		l2b->l2b_phys = pa;
   3870 		l2b->l2b_l1idx = l1idx;
   3871 
   3872 		/*
   3873 		 * Establish an initial occupancy count for this descriptor
   3874 		 */
   3875 		for (l2idx = 0;
   3876 		    l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   3877 		    l2idx++) {
   3878 			if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
   3879 				l2b->l2b_occupancy++;
   3880 			}
   3881 		}
   3882 
   3883 		/*
   3884 		 * Make sure the descriptor itself has the correct cache mode.
   3885 		 * If not, fix it, but bitch about the problem. Port-meisters
   3886 		 * should consider this a clue to fix up their initarm()
   3887 		 * function. :)
   3888 		 */
   3889 		if (pmap_set_pt_cache_mode(kernel_l1pt, (vaddr_t)ptep)) {
   3890 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   3891 			    "L2 pte @ %p\n", ptep);
   3892 		}
   3893 	}
   3894 
   3895 	/*
   3896 	 * Ensure the primary (kernel) L1 has the correct cache mode for
   3897 	 * a page table. Bitch if it is not correctly set.
   3898 	 */
   3899 	for (va = (vaddr_t)kernel_l1pt;
   3900 	    va < ((vaddr_t)kernel_l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
   3901 		if (pmap_set_pt_cache_mode(kernel_l1pt, va))
   3902 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   3903 			    "primary L1 @ 0x%lx\n", va);
   3904 	}
   3905 
   3906 	cpu_dcache_wbinv_all();
   3907 	cpu_tlb_flushID();
   3908 	cpu_cpwait();
   3909 
   3910 	/*
   3911 	 * now we allocate the "special" VAs which are used for tmp mappings
   3912 	 * by the pmap (and other modules).  we allocate the VAs by advancing
   3913 	 * virtual_avail (note that there are no pages mapped at these VAs).
   3914 	 *
   3915 	 * Managed KVM space start from wherever initarm() tells us.
   3916 	 */
   3917 	virtual_avail = vstart;
   3918 	virtual_end = vend;
   3919 
   3920 	pmap_alloc_specials(&virtual_avail, 1, &csrcp, &csrc_pte);
   3921 	pmap_set_pt_cache_mode(kernel_l1pt, (vaddr_t)csrc_pte);
   3922 	pmap_alloc_specials(&virtual_avail, 1, &cdstp, &cdst_pte);
   3923 	pmap_set_pt_cache_mode(kernel_l1pt, (vaddr_t)cdst_pte);
   3924 	pmap_alloc_specials(&virtual_avail, 1, (vaddr_t *)&memhook, NULL);
   3925 	pmap_alloc_specials(&virtual_avail, round_page(MSGBUFSIZE) / PAGE_SIZE,
   3926 	    (vaddr_t *)&msgbufaddr, NULL);
   3927 
   3928 	/*
   3929 	 * Allocate a range of kernel virtual address space to be used
   3930 	 * for L2 descriptor tables and metadata allocation in
   3931 	 * pmap_growkernel().
   3932 	 */
   3933 	size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
   3934 	pmap_alloc_specials(&virtual_avail,
   3935 	    round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
   3936 	    &pmap_kernel_l2ptp_kva, NULL);
   3937 
   3938 	size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
   3939 	pmap_alloc_specials(&virtual_avail,
   3940 	    round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
   3941 	    &pmap_kernel_l2dtable_kva, NULL);
   3942 
   3943 	/*
   3944 	 * init the static-global locks and global pmap list.
   3945 	 */
   3946 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   3947 	spinlockinit(&pmap_main_lock, "pmaplk", 0);
   3948 #endif
   3949 
   3950 	/*
   3951 	 * We can now initialise the first L1's metadata.
   3952 	 */
   3953 	SLIST_INIT(&l1_list);
   3954 	TAILQ_INIT(&l1_lru_list);
   3955 	simple_lock_init(&l1_lru_lock);
   3956 	pmap_init_l1(l1, kernel_l1pt);
   3957 
   3958 	/*
   3959 	 * Initialize the pmap pool and cache
   3960 	 */
   3961 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
   3962 	    &pool_allocator_nointr);
   3963 	pool_cache_init(&pmap_pmap_cache, &pmap_pmap_pool,
   3964 	    pmap_pmap_ctor, NULL, NULL);
   3965 	LIST_INIT(&pmap_pmaps);
   3966 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   3967 
   3968 	/*
   3969 	 * Initialize the pv pool.
   3970 	 */
   3971 	pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
   3972 	    &pmap_bootstrap_pv_allocator);
   3973 
   3974 	/*
   3975 	 * Initialize the L2 dtable pool and cache.
   3976 	 */
   3977 	pool_init(&pmap_l2dtable_pool, sizeof(struct l2_dtable), 0, 0, 0,
   3978 	    "l2dtblpl", NULL);
   3979 	pool_cache_init(&pmap_l2dtable_cache, &pmap_l2dtable_pool,
   3980 	    pmap_l2dtable_ctor, NULL, NULL);
   3981 
   3982 	/*
   3983 	 * Initialise the L2 descriptor table pool and cache
   3984 	 */
   3985 	pool_init(&pmap_l2ptp_pool, L2_TABLE_SIZE_REAL, 0, L2_TABLE_SIZE_REAL,
   3986 	    0, "l2ptppl", NULL);
   3987 	pool_cache_init(&pmap_l2ptp_cache, &pmap_l2ptp_pool,
   3988 	    pmap_l2ptp_ctor, NULL, NULL);
   3989 
   3990 	cpu_dcache_wbinv_all();
   3991 }
   3992 
   3993 static int
   3994 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va)
   3995 {
   3996 	pd_entry_t *pdep, pde;
   3997 	pt_entry_t *ptep, pte;
   3998 	vaddr_t pa;
   3999 	int rv = 0;
   4000 
   4001 	/*
   4002 	 * Make sure the descriptor itself has the correct cache mode
   4003 	 */
   4004 	pdep = &kl1[L1_IDX(va)];
   4005 	pde = *pdep;
   4006 
   4007 	if (l1pte_section_p(pde)) {
   4008 		if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
   4009 			*pdep = (pde & ~L1_S_CACHE_MASK) |
   4010 			    pte_l1_s_cache_mode_pt;
   4011 			PTE_SYNC(pdep);
   4012 			cpu_dcache_wbinv_range((vaddr_t)pdep, sizeof(*pdep));
   4013 			rv = 1;
   4014 		}
   4015 	} else {
   4016 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   4017 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   4018 		if (ptep == NULL)
   4019 			panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
   4020 
   4021 		ptep = &ptep[l2pte_index(va)];
   4022 		pte = *ptep;
   4023 		if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   4024 			*ptep = (pte & ~L2_S_CACHE_MASK) |
   4025 			    pte_l2_s_cache_mode_pt;
   4026 			PTE_SYNC(ptep);
   4027 			cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
   4028 			rv = 1;
   4029 		}
   4030 	}
   4031 
   4032 	return (rv);
   4033 }
   4034 
   4035 static void
   4036 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
   4037 {
   4038 	vaddr_t va = *availp;
   4039 	struct l2_bucket *l2b;
   4040 
   4041 	if (ptep) {
   4042 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   4043 		if (l2b == NULL)
   4044 			panic("pmap_alloc_specials: no l2b for 0x%lx", va);
   4045 
   4046 		if (ptep)
   4047 			*ptep = &l2b->l2b_kva[l2pte_index(va)];
   4048 	}
   4049 
   4050 	*vap = va;
   4051 	*availp = va + (PAGE_SIZE * pages);
   4052 }
   4053 
   4054 void
   4055 pmap_init(void)
   4056 {
   4057 	extern int physmem;
   4058 
   4059 	/*
   4060 	 * Set the available memory vars - These do not map to real memory
   4061 	 * addresses and cannot as the physical memory is fragmented.
   4062 	 * They are used by ps for %mem calculations.
   4063 	 * One could argue whether this should be the entire memory or just
   4064 	 * the memory that is useable in a user process.
   4065 	 */
   4066 	avail_start = 0;
   4067 	avail_end = physmem * PAGE_SIZE;
   4068 
   4069 	/*
   4070 	 * Now we need to free enough pv_entry structures to allow us to get
   4071 	 * the kmem_map/kmem_object allocated and inited (done after this
   4072 	 * function is finished).  to do this we allocate one bootstrap page out
   4073 	 * of kernel_map and use it to provide an initial pool of pv_entry
   4074 	 * structures.   we never free this page.
   4075 	 */
   4076 	pool_setlowat(&pmap_pv_pool,
   4077 	    (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
   4078 
   4079 	pmap_initialized = TRUE;
   4080 }
   4081 
   4082 static vaddr_t last_bootstrap_page = 0;
   4083 static void *free_bootstrap_pages = NULL;
   4084 
   4085 static void *
   4086 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
   4087 {
   4088 	extern void *pool_page_alloc(struct pool *, int);
   4089 	vaddr_t new_page;
   4090 	void *rv;
   4091 
   4092 	if (pmap_initialized)
   4093 		return (pool_page_alloc(pp, flags));
   4094 
   4095 	if (free_bootstrap_pages) {
   4096 		rv = free_bootstrap_pages;
   4097 		free_bootstrap_pages = *((void **)rv);
   4098 		return (rv);
   4099 	}
   4100 
   4101 	new_page = uvm_km_kmemalloc(kernel_map, NULL, PAGE_SIZE,
   4102 	    (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT);
   4103 
   4104 	KASSERT(new_page > last_bootstrap_page);
   4105 	last_bootstrap_page = new_page;
   4106 	return ((void *)new_page);
   4107 }
   4108 
   4109 static void
   4110 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
   4111 {
   4112 	extern void pool_page_free(struct pool *, void *);
   4113 
   4114 	if (pmap_initialized) {
   4115 		pool_page_free(pp, v);
   4116 		return;
   4117 	}
   4118 
   4119 	if ((vaddr_t)v < last_bootstrap_page) {
   4120 		*((void **)v) = free_bootstrap_pages;
   4121 		free_bootstrap_pages = v;
   4122 		return;
   4123 	}
   4124 }
   4125 
   4126 /*
   4127  * pmap_postinit()
   4128  *
   4129  * This routine is called after the vm and kmem subsystems have been
   4130  * initialised. This allows the pmap code to perform any initialisation
   4131  * that can only be done one the memory allocation is in place.
   4132  */
   4133 void
   4134 pmap_postinit(void)
   4135 {
   4136 	extern paddr_t physical_start, physical_end;
   4137 	struct l2_bucket *l2b;
   4138 	struct l1_ttable *l1;
   4139 	struct pglist plist;
   4140 	struct vm_page *m;
   4141 	pd_entry_t *pl1pt;
   4142 	pt_entry_t *ptep, pte;
   4143 	vaddr_t va, eva;
   4144 	u_int loop, needed;
   4145 	int error;
   4146 
   4147 	pool_setlowat(&pmap_l2ptp_pool,
   4148 	    (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
   4149 	pool_setlowat(&pmap_l2dtable_pool,
   4150 	    (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
   4151 
   4152 	needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
   4153 	needed -= 1;
   4154 
   4155 	l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
   4156 
   4157 	for (loop = 0; loop < needed; loop++, l1++) {
   4158 		/* Allocate a L1 page table */
   4159 		va = uvm_km_valloc(kernel_map, L1_TABLE_SIZE);
   4160 		if (va == 0)
   4161 			panic("Cannot allocate L1 KVM");
   4162 
   4163 		error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
   4164 		    physical_end, L1_TABLE_SIZE, 0, &plist, 1, M_WAITOK);
   4165 		if (error)
   4166 			panic("Cannot allocate L1 physical pages");
   4167 
   4168 		m = TAILQ_FIRST(&plist);
   4169 		eva = va + L1_TABLE_SIZE;
   4170 		pl1pt = (pd_entry_t *)va;
   4171 
   4172 		while (m && va < eva) {
   4173 			paddr_t pa = VM_PAGE_TO_PHYS(m);
   4174 
   4175 			pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
   4176 
   4177 			/*
   4178 			 * Make sure the L1 descriptor table is mapped
   4179 			 * with the cache-mode set to write-through.
   4180 			 */
   4181 			l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   4182 			ptep = &l2b->l2b_kva[l2pte_index(va)];
   4183 			pte = *ptep;
   4184 			pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   4185 			*ptep = pte;
   4186 			PTE_SYNC(ptep);
   4187 			cpu_tlb_flushD_SE(va);
   4188 
   4189 			va += PAGE_SIZE;
   4190 			m = m->pageq.tqe_next;
   4191 		}
   4192 
   4193 #ifdef DIAGNOSTIC
   4194 		if (m)
   4195 			panic("pmap_alloc_l1pt: pglist not empty");
   4196 #endif	/* DIAGNOSTIC */
   4197 
   4198 		pmap_init_l1(l1, pl1pt);
   4199 	}
   4200 
   4201 #ifdef DEBUG
   4202 	printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
   4203 	    needed);
   4204 #endif
   4205 }
   4206 
   4207 /*
   4208  * Note that the following routines are used by board-specific initialisation
   4209  * code to configure the initial kernel page tables.
   4210  *
   4211  * If ARM32_NEW_VM_LAYOUT is *not* defined, they operate on the assumption that
   4212  * L2 page-table pages are 4KB in size and use 4 L1 slots. This mimics the
   4213  * behaviour of the old pmap, and provides an easy migration path for
   4214  * initial bring-up of the new pmap on existing ports. Fortunately,
   4215  * pmap_bootstrap() compensates for this hackery. This is only a stop-gap and
   4216  * will be deprecated.
   4217  *
   4218  * If ARM32_NEW_VM_LAYOUT *is* defined, these functions deal with 1KB L2 page
   4219  * tables.
   4220  */
   4221 
   4222 /*
   4223  * This list exists for the benefit of pmap_map_chunk().  It keeps track
   4224  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
   4225  * find them as necessary.
   4226  *
   4227  * Note that the data on this list MUST remain valid after initarm() returns,
   4228  * as pmap_bootstrap() uses it to contruct L2 table metadata.
   4229  */
   4230 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
   4231 
   4232 static vaddr_t
   4233 kernel_pt_lookup(paddr_t pa)
   4234 {
   4235 	pv_addr_t *pv;
   4236 
   4237 	SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
   4238 #ifndef ARM32_NEW_VM_LAYOUT
   4239 		if (pv->pv_pa == (pa & ~PGOFSET))
   4240 			return (pv->pv_va | (pa & PGOFSET));
   4241 #else
   4242 		if (pv->pv_pa == pa)
   4243 			return (pv->pv_va);
   4244 #endif
   4245 	}
   4246 	return (0);
   4247 }
   4248 
   4249 /*
   4250  * pmap_map_section:
   4251  *
   4252  *	Create a single section mapping.
   4253  */
   4254 void
   4255 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   4256 {
   4257 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   4258 	pd_entry_t fl;
   4259 
   4260 	KASSERT(((va | pa) & L1_S_OFFSET) == 0);
   4261 
   4262 	switch (cache) {
   4263 	case PTE_NOCACHE:
   4264 	default:
   4265 		fl = 0;
   4266 		break;
   4267 
   4268 	case PTE_CACHE:
   4269 		fl = pte_l1_s_cache_mode;
   4270 		break;
   4271 
   4272 	case PTE_PAGETABLE:
   4273 		fl = pte_l1_s_cache_mode_pt;
   4274 		break;
   4275 	}
   4276 
   4277 	pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   4278 	    L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
   4279 	PTE_SYNC(&pde[va >> L1_S_SHIFT]);
   4280 }
   4281 
   4282 /*
   4283  * pmap_map_entry:
   4284  *
   4285  *	Create a single page mapping.
   4286  */
   4287 void
   4288 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   4289 {
   4290 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   4291 	pt_entry_t fl;
   4292 	pt_entry_t *pte;
   4293 
   4294 	KASSERT(((va | pa) & PGOFSET) == 0);
   4295 
   4296 	switch (cache) {
   4297 	case PTE_NOCACHE:
   4298 	default:
   4299 		fl = 0;
   4300 		break;
   4301 
   4302 	case PTE_CACHE:
   4303 		fl = pte_l2_s_cache_mode;
   4304 		break;
   4305 
   4306 	case PTE_PAGETABLE:
   4307 		fl = pte_l2_s_cache_mode_pt;
   4308 		break;
   4309 	}
   4310 
   4311 	if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   4312 		panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
   4313 
   4314 #ifndef ARM32_NEW_VM_LAYOUT
   4315 	pte = (pt_entry_t *)
   4316 	    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   4317 #else
   4318 	pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   4319 #endif
   4320 	if (pte == NULL)
   4321 		panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
   4322 
   4323 #ifndef ARM32_NEW_VM_LAYOUT
   4324 	pte[(va >> PGSHIFT) & 0x3ff] =
   4325 	    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
   4326 	PTE_SYNC(&pte[(va >> PGSHIFT) & 0x3ff]);
   4327 #else
   4328 	pte[l2pte_index(va)] =
   4329 	    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
   4330 	PTE_SYNC(&pte[l2pte_index(va)]);
   4331 #endif
   4332 }
   4333 
   4334 /*
   4335  * pmap_link_l2pt:
   4336  *
   4337  *	Link the L2 page table specified by "l2pv" into the L1
   4338  *	page table at the slot for "va".
   4339  */
   4340 void
   4341 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
   4342 {
   4343 	pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
   4344 	u_int slot = va >> L1_S_SHIFT;
   4345 
   4346 #ifndef ARM32_NEW_VM_LAYOUT
   4347 	KASSERT((va & ((L1_S_SIZE * 4) - 1)) == 0);
   4348 	KASSERT((l2pv->pv_pa & PGOFSET) == 0);
   4349 #endif
   4350 
   4351 	proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
   4352 
   4353 	pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
   4354 #ifdef ARM32_NEW_VM_LAYOUT
   4355 	PTE_SYNC(&pde[slot]);
   4356 #else
   4357 	pde[slot + 1] = proto | (l2pv->pv_pa + 0x400);
   4358 	pde[slot + 2] = proto | (l2pv->pv_pa + 0x800);
   4359 	pde[slot + 3] = proto | (l2pv->pv_pa + 0xc00);
   4360 	PTE_SYNC_RANGE(&pde[slot + 0], 4);
   4361 #endif
   4362 
   4363 	SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
   4364 }
   4365 
   4366 /*
   4367  * pmap_map_chunk:
   4368  *
   4369  *	Map a chunk of memory using the most efficient mappings
   4370  *	possible (section, large page, small page) into the
   4371  *	provided L1 and L2 tables at the specified virtual address.
   4372  */
   4373 vsize_t
   4374 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
   4375     int prot, int cache)
   4376 {
   4377 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   4378 	pt_entry_t *pte, f1, f2s, f2l;
   4379 	vsize_t resid;
   4380 	int i;
   4381 
   4382 	resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
   4383 
   4384 	if (l1pt == 0)
   4385 		panic("pmap_map_chunk: no L1 table provided");
   4386 
   4387 #ifdef VERBOSE_INIT_ARM
   4388 	printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
   4389 	    "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
   4390 #endif
   4391 
   4392 	switch (cache) {
   4393 	case PTE_NOCACHE:
   4394 	default:
   4395 		f1 = 0;
   4396 		f2l = 0;
   4397 		f2s = 0;
   4398 		break;
   4399 
   4400 	case PTE_CACHE:
   4401 		f1 = pte_l1_s_cache_mode;
   4402 		f2l = pte_l2_l_cache_mode;
   4403 		f2s = pte_l2_s_cache_mode;
   4404 		break;
   4405 
   4406 	case PTE_PAGETABLE:
   4407 		f1 = pte_l1_s_cache_mode_pt;
   4408 		f2l = pte_l2_l_cache_mode_pt;
   4409 		f2s = pte_l2_s_cache_mode_pt;
   4410 		break;
   4411 	}
   4412 
   4413 	size = resid;
   4414 
   4415 	while (resid > 0) {
   4416 		/* See if we can use a section mapping. */
   4417 		if (L1_S_MAPPABLE_P(va, pa, resid)) {
   4418 #ifdef VERBOSE_INIT_ARM
   4419 			printf("S");
   4420 #endif
   4421 			pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   4422 			    L1_S_PROT(PTE_KERNEL, prot) | f1 |
   4423 			    L1_S_DOM(PMAP_DOMAIN_KERNEL);
   4424 			PTE_SYNC(&pde[va >> L1_S_SHIFT]);
   4425 			va += L1_S_SIZE;
   4426 			pa += L1_S_SIZE;
   4427 			resid -= L1_S_SIZE;
   4428 			continue;
   4429 		}
   4430 
   4431 		/*
   4432 		 * Ok, we're going to use an L2 table.  Make sure
   4433 		 * one is actually in the corresponding L1 slot
   4434 		 * for the current VA.
   4435 		 */
   4436 		if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   4437 			panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
   4438 
   4439 #ifndef ARM32_NEW_VM_LAYOUT
   4440 		pte = (pt_entry_t *)
   4441 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   4442 #else
   4443 		pte = (pt_entry_t *) kernel_pt_lookup(
   4444 		    pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   4445 #endif
   4446 		if (pte == NULL)
   4447 			panic("pmap_map_chunk: can't find L2 table for VA"
   4448 			    "0x%08lx", va);
   4449 
   4450 		/* See if we can use a L2 large page mapping. */
   4451 		if (L2_L_MAPPABLE_P(va, pa, resid)) {
   4452 #ifdef VERBOSE_INIT_ARM
   4453 			printf("L");
   4454 #endif
   4455 			for (i = 0; i < 16; i++) {
   4456 #ifndef ARM32_NEW_VM_LAYOUT
   4457 				pte[((va >> PGSHIFT) & 0x3f0) + i] =
   4458 				    L2_L_PROTO | pa |
   4459 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   4460 				PTE_SYNC(&pte[((va >> PGSHIFT) & 0x3f0) + i]);
   4461 #else
   4462 				pte[l2pte_index(va) + i] =
   4463 				    L2_L_PROTO | pa |
   4464 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   4465 				PTE_SYNC(&pte[l2pte_index(va) + i]);
   4466 #endif
   4467 			}
   4468 			va += L2_L_SIZE;
   4469 			pa += L2_L_SIZE;
   4470 			resid -= L2_L_SIZE;
   4471 			continue;
   4472 		}
   4473 
   4474 		/* Use a small page mapping. */
   4475 #ifdef VERBOSE_INIT_ARM
   4476 		printf("P");
   4477 #endif
   4478 #ifndef ARM32_NEW_VM_LAYOUT
   4479 		pte[(va >> PGSHIFT) & 0x3ff] =
   4480 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   4481 		PTE_SYNC(&pte[(va >> PGSHIFT) & 0x3ff]);
   4482 #else
   4483 		pte[l2pte_index(va)] =
   4484 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   4485 		PTE_SYNC(&pte[l2pte_index(va)]);
   4486 #endif
   4487 		va += PAGE_SIZE;
   4488 		pa += PAGE_SIZE;
   4489 		resid -= PAGE_SIZE;
   4490 	}
   4491 #ifdef VERBOSE_INIT_ARM
   4492 	printf("\n");
   4493 #endif
   4494 	return (size);
   4495 }
   4496 
   4497 /********************** Static device map routines ***************************/
   4498 
   4499 static const struct pmap_devmap *pmap_devmap_table;
   4500 
   4501 /*
   4502  * Register the devmap table.  This is provided in case early console
   4503  * initialization needs to register mappings created by bootstrap code
   4504  * before pmap_devmap_bootstrap() is called.
   4505  */
   4506 void
   4507 pmap_devmap_register(const struct pmap_devmap *table)
   4508 {
   4509 
   4510 	pmap_devmap_table = table;
   4511 }
   4512 
   4513 /*
   4514  * Map all of the static regions in the devmap table, and remember
   4515  * the devmap table so other parts of the kernel can look up entries
   4516  * later.
   4517  */
   4518 void
   4519 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
   4520 {
   4521 	int i;
   4522 
   4523 	pmap_devmap_table = table;
   4524 
   4525 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   4526 #ifdef VERBOSE_INIT_ARM
   4527 		printf("devmap: %08lx -> %08lx @ %08lx\n",
   4528 		    pmap_devmap_table[i].pd_pa,
   4529 		    pmap_devmap_table[i].pd_pa +
   4530 			pmap_devmap_table[i].pd_size - 1,
   4531 		    pmap_devmap_table[i].pd_va);
   4532 #endif
   4533 		pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
   4534 		    pmap_devmap_table[i].pd_pa,
   4535 		    pmap_devmap_table[i].pd_size,
   4536 		    pmap_devmap_table[i].pd_prot,
   4537 		    pmap_devmap_table[i].pd_cache);
   4538 	}
   4539 }
   4540 
   4541 const struct pmap_devmap *
   4542 pmap_devmap_find_pa(paddr_t pa, psize_t size)
   4543 {
   4544 	int i;
   4545 
   4546 	if (pmap_devmap_table == NULL)
   4547 		return (NULL);
   4548 
   4549 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   4550 		if (pa >= pmap_devmap_table[i].pd_pa &&
   4551 		    pa + size <= pmap_devmap_table[i].pd_pa +
   4552 				 pmap_devmap_table[i].pd_size)
   4553 			return (&pmap_devmap_table[i]);
   4554 	}
   4555 
   4556 	return (NULL);
   4557 }
   4558 
   4559 const struct pmap_devmap *
   4560 pmap_devmap_find_va(vaddr_t va, vsize_t size)
   4561 {
   4562 	int i;
   4563 
   4564 	if (pmap_devmap_table == NULL)
   4565 		return (NULL);
   4566 
   4567 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   4568 		if (va >= pmap_devmap_table[i].pd_va &&
   4569 		    va + size <= pmap_devmap_table[i].pd_va +
   4570 				 pmap_devmap_table[i].pd_size)
   4571 			return (&pmap_devmap_table[i]);
   4572 	}
   4573 
   4574 	return (NULL);
   4575 }
   4576 
   4577 /********************** PTE initialization routines **************************/
   4578 
   4579 /*
   4580  * These routines are called when the CPU type is identified to set up
   4581  * the PTE prototypes, cache modes, etc.
   4582  *
   4583  * The variables are always here, just in case LKMs need to reference
   4584  * them (though, they shouldn't).
   4585  */
   4586 
   4587 pt_entry_t	pte_l1_s_cache_mode;
   4588 pt_entry_t	pte_l1_s_cache_mode_pt;
   4589 pt_entry_t	pte_l1_s_cache_mask;
   4590 
   4591 pt_entry_t	pte_l2_l_cache_mode;
   4592 pt_entry_t	pte_l2_l_cache_mode_pt;
   4593 pt_entry_t	pte_l2_l_cache_mask;
   4594 
   4595 pt_entry_t	pte_l2_s_cache_mode;
   4596 pt_entry_t	pte_l2_s_cache_mode_pt;
   4597 pt_entry_t	pte_l2_s_cache_mask;
   4598 
   4599 pt_entry_t	pte_l2_s_prot_u;
   4600 pt_entry_t	pte_l2_s_prot_w;
   4601 pt_entry_t	pte_l2_s_prot_mask;
   4602 
   4603 pt_entry_t	pte_l1_s_proto;
   4604 pt_entry_t	pte_l1_c_proto;
   4605 pt_entry_t	pte_l2_s_proto;
   4606 
   4607 void		(*pmap_copy_page_func)(paddr_t, paddr_t);
   4608 void		(*pmap_zero_page_func)(paddr_t);
   4609 
   4610 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
   4611 void
   4612 pmap_pte_init_generic(void)
   4613 {
   4614 
   4615 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   4616 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
   4617 
   4618 	pte_l2_l_cache_mode = L2_B|L2_C;
   4619 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
   4620 
   4621 	pte_l2_s_cache_mode = L2_B|L2_C;
   4622 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
   4623 
   4624 	/*
   4625 	 * If we have a write-through cache, set B and C.  If
   4626 	 * we have a write-back cache, then we assume setting
   4627 	 * only C will make those pages write-through.
   4628 	 */
   4629 	if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
   4630 		pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   4631 		pte_l2_l_cache_mode_pt = L2_B|L2_C;
   4632 		pte_l2_s_cache_mode_pt = L2_B|L2_C;
   4633 	} else {
   4634 		pte_l1_s_cache_mode_pt = L1_S_C;
   4635 		pte_l2_l_cache_mode_pt = L2_C;
   4636 		pte_l2_s_cache_mode_pt = L2_C;
   4637 	}
   4638 
   4639 	pte_l2_s_prot_u = L2_S_PROT_U_generic;
   4640 	pte_l2_s_prot_w = L2_S_PROT_W_generic;
   4641 	pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
   4642 
   4643 	pte_l1_s_proto = L1_S_PROTO_generic;
   4644 	pte_l1_c_proto = L1_C_PROTO_generic;
   4645 	pte_l2_s_proto = L2_S_PROTO_generic;
   4646 
   4647 	pmap_copy_page_func = pmap_copy_page_generic;
   4648 	pmap_zero_page_func = pmap_zero_page_generic;
   4649 }
   4650 
   4651 #if defined(CPU_ARM8)
   4652 void
   4653 pmap_pte_init_arm8(void)
   4654 {
   4655 
   4656 	/*
   4657 	 * ARM8 is compatible with generic, but we need to use
   4658 	 * the page tables uncached.
   4659 	 */
   4660 	pmap_pte_init_generic();
   4661 
   4662 	pte_l1_s_cache_mode_pt = 0;
   4663 	pte_l2_l_cache_mode_pt = 0;
   4664 	pte_l2_s_cache_mode_pt = 0;
   4665 }
   4666 #endif /* CPU_ARM8 */
   4667 
   4668 #if defined(CPU_ARM9)
   4669 void
   4670 pmap_pte_init_arm9(void)
   4671 {
   4672 
   4673 	/*
   4674 	 * ARM9 is compatible with generic, but we want to use
   4675 	 * write-through caching for now.
   4676 	 */
   4677 	pmap_pte_init_generic();
   4678 
   4679 	pte_l1_s_cache_mode = L1_S_C;
   4680 	pte_l2_l_cache_mode = L2_C;
   4681 	pte_l2_s_cache_mode = L2_C;
   4682 
   4683 	pte_l1_s_cache_mode_pt = L1_S_C;
   4684 	pte_l2_l_cache_mode_pt = L2_C;
   4685 	pte_l2_s_cache_mode_pt = L2_C;
   4686 }
   4687 #endif /* CPU_ARM9 */
   4688 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
   4689 
   4690 #if ARM_MMU_SA1 == 1
   4691 void
   4692 pmap_pte_init_sa1(void)
   4693 {
   4694 
   4695 	/*
   4696 	 * The StrongARM SA-1 cache does not have a write-through
   4697 	 * mode.  So, do the generic initialization, then reset
   4698 	 * the page table cache mode to B=1,C=1, and note that
   4699 	 * the PTEs need to be sync'd.
   4700 	 */
   4701 	pmap_pte_init_generic();
   4702 
   4703 	pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   4704 	pte_l2_l_cache_mode_pt = L2_B|L2_C;
   4705 	pte_l2_s_cache_mode_pt = L2_B|L2_C;
   4706 
   4707 	pmap_needs_pte_sync = 1;
   4708 }
   4709 #endif /* ARM_MMU_SA1 == 1*/
   4710 
   4711 #if ARM_MMU_XSCALE == 1
   4712 void
   4713 pmap_pte_init_xscale(void)
   4714 {
   4715 	uint32_t auxctl;
   4716 	int write_through = 0;
   4717 
   4718 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   4719 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
   4720 
   4721 	pte_l2_l_cache_mode = L2_B|L2_C;
   4722 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
   4723 
   4724 	pte_l2_s_cache_mode = L2_B|L2_C;
   4725 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
   4726 
   4727 	pte_l1_s_cache_mode_pt = L1_S_C;
   4728 	pte_l2_l_cache_mode_pt = L2_C;
   4729 	pte_l2_s_cache_mode_pt = L2_C;
   4730 
   4731 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
   4732 	/*
   4733 	 * The XScale core has an enhanced mode where writes that
   4734 	 * miss the cache cause a cache line to be allocated.  This
   4735 	 * is significantly faster than the traditional, write-through
   4736 	 * behavior of this case.
   4737 	 */
   4738 	pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
   4739 	pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
   4740 	pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
   4741 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
   4742 
   4743 #ifdef XSCALE_CACHE_WRITE_THROUGH
   4744 	/*
   4745 	 * Some versions of the XScale core have various bugs in
   4746 	 * their cache units, the work-around for which is to run
   4747 	 * the cache in write-through mode.  Unfortunately, this
   4748 	 * has a major (negative) impact on performance.  So, we
   4749 	 * go ahead and run fast-and-loose, in the hopes that we
   4750 	 * don't line up the planets in a way that will trip the
   4751 	 * bugs.
   4752 	 *
   4753 	 * However, we give you the option to be slow-but-correct.
   4754 	 */
   4755 	write_through = 1;
   4756 #elif defined(XSCALE_CACHE_WRITE_BACK)
   4757 	/* force write back cache mode */
   4758 	write_through = 0;
   4759 #elif defined(CPU_XSCALE_PXA2X0)
   4760 	/*
   4761 	 * Intel PXA2[15]0 processors are known to have a bug in
   4762 	 * write-back cache on revision 4 and earlier (stepping
   4763 	 * A[01] and B[012]).  Fixed for C0 and later.
   4764 	 */
   4765 	{
   4766 		uint32_t id, type;
   4767 
   4768 		id = cpufunc_id();
   4769 		type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
   4770 
   4771 		if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
   4772 			if ((id & CPU_ID_REVISION_MASK) < 5) {
   4773 				/* write through for stepping A0-1 and B0-2 */
   4774 				write_through = 1;
   4775 			}
   4776 		}
   4777 	}
   4778 #endif /* XSCALE_CACHE_WRITE_THROUGH */
   4779 
   4780 	if (write_through) {
   4781 		pte_l1_s_cache_mode = L1_S_C;
   4782 		pte_l2_l_cache_mode = L2_C;
   4783 		pte_l2_s_cache_mode = L2_C;
   4784 	}
   4785 
   4786 	pte_l2_s_prot_u = L2_S_PROT_U_xscale;
   4787 	pte_l2_s_prot_w = L2_S_PROT_W_xscale;
   4788 	pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
   4789 
   4790 	pte_l1_s_proto = L1_S_PROTO_xscale;
   4791 	pte_l1_c_proto = L1_C_PROTO_xscale;
   4792 	pte_l2_s_proto = L2_S_PROTO_xscale;
   4793 
   4794 	pmap_copy_page_func = pmap_copy_page_xscale;
   4795 	pmap_zero_page_func = pmap_zero_page_xscale;
   4796 
   4797 	/*
   4798 	 * Disable ECC protection of page table access, for now.
   4799 	 */
   4800 	__asm __volatile("mrc p15, 0, %0, c1, c0, 1"
   4801 		: "=r" (auxctl));
   4802 	auxctl &= ~XSCALE_AUXCTL_P;
   4803 	__asm __volatile("mcr p15, 0, %0, c1, c0, 1"
   4804 		:
   4805 		: "r" (auxctl));
   4806 }
   4807 
   4808 /*
   4809  * xscale_setup_minidata:
   4810  *
   4811  *	Set up the mini-data cache clean area.  We require the
   4812  *	caller to allocate the right amount of physically and
   4813  *	virtually contiguous space.
   4814  */
   4815 void
   4816 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
   4817 {
   4818 	extern vaddr_t xscale_minidata_clean_addr;
   4819 	extern vsize_t xscale_minidata_clean_size; /* already initialized */
   4820 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   4821 	pt_entry_t *pte;
   4822 	vsize_t size;
   4823 	uint32_t auxctl;
   4824 
   4825 	xscale_minidata_clean_addr = va;
   4826 
   4827 	/* Round it to page size. */
   4828 	size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
   4829 
   4830 	for (; size != 0;
   4831 	     va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
   4832 #ifndef ARM32_NEW_VM_LAYOUT
   4833 		pte = (pt_entry_t *)
   4834 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   4835 #else
   4836 		pte = (pt_entry_t *) kernel_pt_lookup(
   4837 		    pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   4838 #endif
   4839 		if (pte == NULL)
   4840 			panic("xscale_setup_minidata: can't find L2 table for "
   4841 			    "VA 0x%08lx", va);
   4842 #ifndef ARM32_NEW_VM_LAYOUT
   4843 		pte[(va >> PGSHIFT) & 0x3ff] =
   4844 #else
   4845 		pte[l2pte_index(va)] =
   4846 #endif
   4847 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   4848 		    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
   4849 	}
   4850 
   4851 	/*
   4852 	 * Configure the mini-data cache for write-back with
   4853 	 * read/write-allocate.
   4854 	 *
   4855 	 * NOTE: In order to reconfigure the mini-data cache, we must
   4856 	 * make sure it contains no valid data!  In order to do that,
   4857 	 * we must issue a global data cache invalidate command!
   4858 	 *
   4859 	 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
   4860 	 * THIS IS VERY IMPORTANT!
   4861 	 */
   4862 
   4863 	/* Invalidate data and mini-data. */
   4864 	__asm __volatile("mcr p15, 0, %0, c7, c6, 0"
   4865 		:
   4866 		: "r" (auxctl));
   4867 
   4868 
   4869 	__asm __volatile("mrc p15, 0, %0, c1, c0, 1"
   4870 		: "=r" (auxctl));
   4871 	auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
   4872 	__asm __volatile("mcr p15, 0, %0, c1, c0, 1"
   4873 		:
   4874 		: "r" (auxctl));
   4875 }
   4876 #endif /* ARM_MMU_XSCALE == 1 */
   4877 
   4878 #if defined(DDB)
   4879 /*
   4880  * A couple of ddb-callable functions for dumping pmaps
   4881  */
   4882 void pmap_dump_all(void);
   4883 void pmap_dump(pmap_t);
   4884 
   4885 void
   4886 pmap_dump_all(void)
   4887 {
   4888 	pmap_t pm;
   4889 
   4890 	LIST_FOREACH(pm, &pmap_pmaps, pm_list) {
   4891 		if (pm == pmap_kernel())
   4892 			continue;
   4893 		pmap_dump(pm);
   4894 		printf("\n");
   4895 	}
   4896 }
   4897 
   4898 static pt_entry_t ncptes[64];
   4899 static void pmap_dump_ncpg(pmap_t);
   4900 
   4901 void
   4902 pmap_dump(pmap_t pm)
   4903 {
   4904 	struct l2_dtable *l2;
   4905 	struct l2_bucket *l2b;
   4906 	pt_entry_t *ptep, pte;
   4907 	vaddr_t l2_va, l2b_va, va;
   4908 	int i, j, k, occ, rows = 0;
   4909 	char ch;
   4910 
   4911 	if (pm == pmap_kernel())
   4912 		printf("pmap_kernel (%p): ", pm);
   4913 	else
   4914 		printf("user pmap (%p): ", pm);
   4915 
   4916 	printf("domain %d, l1 at %p\n", pm->pm_domain, pm->pm_l1->l1_kva);
   4917 
   4918 	l2_va = 0;
   4919 	for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
   4920 		l2 = pm->pm_l2[i];
   4921 
   4922 		if (l2 == NULL || l2->l2_occupancy == 0)
   4923 			continue;
   4924 
   4925 		l2b_va = l2_va;
   4926 		for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
   4927 			l2b = &l2->l2_bucket[j];
   4928 
   4929 			if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
   4930 				continue;
   4931 
   4932 			ptep = l2b->l2b_kva;
   4933 
   4934 			for (k = 0; k < 256 && ptep[k] == 0; k++)
   4935 				;
   4936 
   4937 			k &= ~63;
   4938 			occ = l2b->l2b_occupancy;
   4939 			va = l2b_va + (k * 4096);
   4940 			for (; k < 256; k++, va += 0x1000) {
   4941 				if ((k % 64) == 0) {
   4942 					if ((rows % 8) == 0) {
   4943 						printf(
   4944 "          |0000   |8000   |10000  |18000  |20000  |28000  |30000  |38000\n");
   4945 					}
   4946 					printf("%08lx: ", va);
   4947 				}
   4948 
   4949 				ncptes[k & 63] = 0;
   4950 				pte = ptep[k];
   4951 				if (pte == 0) {
   4952 					ch = '.';
   4953 				} else {
   4954 					occ--;
   4955 					switch (pte & 0x0c) {
   4956 					case 0x00:
   4957 						ch = 'D'; /* No cache No buff */
   4958 						break;
   4959 					case 0x04:
   4960 						ch = 'B'; /* No cache buff */
   4961 						break;
   4962 					case 0x08:
   4963 						ch = 'C'; /* Cache No buff */
   4964 						break;
   4965 					case 0x0c:
   4966 						ch = 'F'; /* Cache Buff */
   4967 						break;
   4968 					}
   4969 
   4970 					if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
   4971 						ch += 0x20;
   4972 
   4973 					if ((pte & 0xc) == 0)
   4974 						ncptes[k & 63] = pte;
   4975 				}
   4976 
   4977 				if ((k % 64) == 63) {
   4978 					rows++;
   4979 					printf("%c\n", ch);
   4980 					pmap_dump_ncpg(pm);
   4981 					if (occ == 0)
   4982 						break;
   4983 				} else
   4984 					printf("%c", ch);
   4985 			}
   4986 		}
   4987 	}
   4988 }
   4989 
   4990 static void
   4991 pmap_dump_ncpg(pmap_t pm)
   4992 {
   4993 	struct vm_page *pg;
   4994 	struct pv_entry *pv;
   4995 	int i;
   4996 
   4997 	for (i = 0; i < 63; i++) {
   4998 		if (ncptes[i] == 0)
   4999 			continue;
   5000 
   5001 		pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
   5002 		if (pg == NULL)
   5003 			continue;
   5004 
   5005 		printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
   5006 		    pg->phys_addr,
   5007 		    pg->mdpage.krw_mappings, pg->mdpage.kro_mappings,
   5008 		    pg->mdpage.urw_mappings, pg->mdpage.uro_mappings);
   5009 
   5010 		for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   5011 			printf("   %c va 0x%08lx, flags 0x%x\n",
   5012 			    (pm == pv->pv_pmap) ? '*' : ' ',
   5013 			    pv->pv_va, pv->pv_flags);
   5014 		}
   5015 	}
   5016 }
   5017 #endif
   5018