Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.251
      1 /*	$NetBSD: pmap.c,v 1.251 2013/02/01 15:02:31 matt 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  *
     82  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     83  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     84  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     85  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     86  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     87  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     88  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     89  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     90  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     91  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     92  * POSSIBILITY OF SUCH DAMAGE.
     93  */
     94 
     95 /*
     96  * Copyright (c) 1994-1998 Mark Brinicombe.
     97  * Copyright (c) 1994 Brini.
     98  * All rights reserved.
     99  *
    100  * This code is derived from software written for Brini by Mark Brinicombe
    101  *
    102  * Redistribution and use in source and binary forms, with or without
    103  * modification, are permitted provided that the following conditions
    104  * are met:
    105  * 1. Redistributions of source code must retain the above copyright
    106  *    notice, this list of conditions and the following disclaimer.
    107  * 2. Redistributions in binary form must reproduce the above copyright
    108  *    notice, this list of conditions and the following disclaimer in the
    109  *    documentation and/or other materials provided with the distribution.
    110  * 3. All advertising materials mentioning features or use of this software
    111  *    must display the following acknowledgement:
    112  *	This product includes software developed by Mark Brinicombe.
    113  * 4. The name of the author may not be used to endorse or promote products
    114  *    derived from this software without specific prior written permission.
    115  *
    116  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    117  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    118  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    119  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    120  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    121  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    122  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    123  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    124  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    125  *
    126  * RiscBSD kernel project
    127  *
    128  * pmap.c
    129  *
    130  * Machine dependent vm stuff
    131  *
    132  * Created      : 20/09/94
    133  */
    134 
    135 /*
    136  * armv6 and VIPT cache support by 3am Software Foundry,
    137  * Copyright (c) 2007 Microsoft
    138  */
    139 
    140 /*
    141  * Performance improvements, UVM changes, overhauls and part-rewrites
    142  * were contributed by Neil A. Carson <neil (at) causality.com>.
    143  */
    144 
    145 /*
    146  * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables
    147  * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
    148  * Systems, Inc.
    149  *
    150  * There are still a few things outstanding at this time:
    151  *
    152  *   - There are some unresolved issues for MP systems:
    153  *
    154  *     o The L1 metadata needs a lock, or more specifically, some places
    155  *       need to acquire an exclusive lock when modifying L1 translation
    156  *       table entries.
    157  *
    158  *     o When one cpu modifies an L1 entry, and that L1 table is also
    159  *       being used by another cpu, then the latter will need to be told
    160  *       that a tlb invalidation may be necessary. (But only if the old
    161  *       domain number in the L1 entry being over-written is currently
    162  *       the active domain on that cpu). I guess there are lots more tlb
    163  *       shootdown issues too...
    164  *
    165  *     o If the vector_page is at 0x00000000 instead of 0xffff0000, then
    166  *       MP systems will lose big-time because of the MMU domain hack.
    167  *       The only way this can be solved (apart from moving the vector
    168  *       page to 0xffff0000) is to reserve the first 1MB of user address
    169  *       space for kernel use only. This would require re-linking all
    170  *       applications so that the text section starts above this 1MB
    171  *       boundary.
    172  *
    173  *     o Tracking which VM space is resident in the cache/tlb has not yet
    174  *       been implemented for MP systems.
    175  *
    176  *     o Finally, there is a pathological condition where two cpus running
    177  *       two separate processes (not lwps) which happen to share an L1
    178  *       can get into a fight over one or more L1 entries. This will result
    179  *       in a significant slow-down if both processes are in tight loops.
    180  */
    181 
    182 /*
    183  * Special compilation symbols
    184  * PMAP_DEBUG		- Build in pmap_debug_level code
    185  */
    186 
    187 /* Include header files */
    188 
    189 #include "opt_cpuoptions.h"
    190 #include "opt_pmap_debug.h"
    191 #include "opt_ddb.h"
    192 #include "opt_lockdebug.h"
    193 #include "opt_multiprocessor.h"
    194 
    195 #include <sys/param.h>
    196 #include <sys/types.h>
    197 #include <sys/kernel.h>
    198 #include <sys/systm.h>
    199 #include <sys/proc.h>
    200 #include <sys/pool.h>
    201 #include <sys/kmem.h>
    202 #include <sys/cdefs.h>
    203 #include <sys/cpu.h>
    204 #include <sys/sysctl.h>
    205 
    206 #include <uvm/uvm.h>
    207 
    208 #include <sys/bus.h>
    209 #include <machine/pmap.h>
    210 #include <machine/pcb.h>
    211 #include <machine/param.h>
    212 #include <arm/cpuconf.h>
    213 #include <arm/arm32/katelib.h>
    214 
    215 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.251 2013/02/01 15:02:31 matt Exp $");
    216 
    217 #ifdef PMAP_DEBUG
    218 
    219 /* XXX need to get rid of all refs to this */
    220 int pmap_debug_level = 0;
    221 
    222 /*
    223  * for switching to potentially finer grained debugging
    224  */
    225 #define	PDB_FOLLOW	0x0001
    226 #define	PDB_INIT	0x0002
    227 #define	PDB_ENTER	0x0004
    228 #define	PDB_REMOVE	0x0008
    229 #define	PDB_CREATE	0x0010
    230 #define	PDB_PTPAGE	0x0020
    231 #define	PDB_GROWKERN	0x0040
    232 #define	PDB_BITS	0x0080
    233 #define	PDB_COLLECT	0x0100
    234 #define	PDB_PROTECT	0x0200
    235 #define	PDB_MAP_L1	0x0400
    236 #define	PDB_BOOTSTRAP	0x1000
    237 #define	PDB_PARANOIA	0x2000
    238 #define	PDB_WIRING	0x4000
    239 #define	PDB_PVDUMP	0x8000
    240 #define	PDB_VAC		0x10000
    241 #define	PDB_KENTER	0x20000
    242 #define	PDB_KREMOVE	0x40000
    243 #define	PDB_EXEC	0x80000
    244 
    245 int debugmap = 1;
    246 int pmapdebug = 0;
    247 #define	NPDEBUG(_lev_,_stat_) \
    248 	if (pmapdebug & (_lev_)) \
    249         	((_stat_))
    250 
    251 #else	/* PMAP_DEBUG */
    252 #define NPDEBUG(_lev_,_stat_) /* Nothing */
    253 #endif	/* PMAP_DEBUG */
    254 
    255 /*
    256  * pmap_kernel() points here
    257  */
    258 static struct pmap	kernel_pmap_store;
    259 struct pmap		*const kernel_pmap_ptr = &kernel_pmap_store;
    260 #ifdef PMAP_NEED_ALLOC_POOLPAGE
    261 int			arm_poolpage_vmfreelist = VM_FREELIST_DEFAULT;
    262 #endif
    263 
    264 /*
    265  * Which pmap is currently 'live' in the cache
    266  *
    267  * XXXSCW: Fix for SMP ...
    268  */
    269 static pmap_t pmap_recent_user;
    270 
    271 /*
    272  * Pointer to last active lwp, or NULL if it exited.
    273  */
    274 struct lwp *pmap_previous_active_lwp;
    275 
    276 /*
    277  * Pool and cache that pmap structures are allocated from.
    278  * We use a cache to avoid clearing the pm_l2[] array (1KB)
    279  * in pmap_create().
    280  */
    281 static struct pool_cache pmap_cache;
    282 static LIST_HEAD(, pmap) pmap_pmaps;
    283 
    284 /*
    285  * Pool of PV structures
    286  */
    287 static struct pool pmap_pv_pool;
    288 static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
    289 static void pmap_bootstrap_pv_page_free(struct pool *, void *);
    290 static struct pool_allocator pmap_bootstrap_pv_allocator = {
    291 	pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
    292 };
    293 
    294 /*
    295  * Pool and cache of l2_dtable structures.
    296  * We use a cache to avoid clearing the structures when they're
    297  * allocated. (196 bytes)
    298  */
    299 static struct pool_cache pmap_l2dtable_cache;
    300 static vaddr_t pmap_kernel_l2dtable_kva;
    301 
    302 /*
    303  * Pool and cache of L2 page descriptors.
    304  * We use a cache to avoid clearing the descriptor table
    305  * when they're allocated. (1KB)
    306  */
    307 static struct pool_cache pmap_l2ptp_cache;
    308 static vaddr_t pmap_kernel_l2ptp_kva;
    309 static paddr_t pmap_kernel_l2ptp_phys;
    310 
    311 #ifdef PMAPCOUNTERS
    312 #define	PMAP_EVCNT_INITIALIZER(name) \
    313 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name)
    314 
    315 #ifdef PMAP_CACHE_VIPT
    316 static struct evcnt pmap_ev_vac_clean_one =
    317    PMAP_EVCNT_INITIALIZER("clean page (1 color)");
    318 static struct evcnt pmap_ev_vac_flush_one =
    319    PMAP_EVCNT_INITIALIZER("flush page (1 color)");
    320 static struct evcnt pmap_ev_vac_flush_lots =
    321    PMAP_EVCNT_INITIALIZER("flush page (2+ colors)");
    322 static struct evcnt pmap_ev_vac_flush_lots2 =
    323    PMAP_EVCNT_INITIALIZER("flush page (2+ colors, kmpage)");
    324 EVCNT_ATTACH_STATIC(pmap_ev_vac_clean_one);
    325 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_one);
    326 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots);
    327 EVCNT_ATTACH_STATIC(pmap_ev_vac_flush_lots2);
    328 
    329 static struct evcnt pmap_ev_vac_color_new =
    330    PMAP_EVCNT_INITIALIZER("new page color");
    331 static struct evcnt pmap_ev_vac_color_reuse =
    332    PMAP_EVCNT_INITIALIZER("ok first page color");
    333 static struct evcnt pmap_ev_vac_color_ok =
    334    PMAP_EVCNT_INITIALIZER("ok page color");
    335 static struct evcnt pmap_ev_vac_color_blind =
    336    PMAP_EVCNT_INITIALIZER("blind page color");
    337 static struct evcnt pmap_ev_vac_color_change =
    338    PMAP_EVCNT_INITIALIZER("change page color");
    339 static struct evcnt pmap_ev_vac_color_erase =
    340    PMAP_EVCNT_INITIALIZER("erase page color");
    341 static struct evcnt pmap_ev_vac_color_none =
    342    PMAP_EVCNT_INITIALIZER("no page color");
    343 static struct evcnt pmap_ev_vac_color_restore =
    344    PMAP_EVCNT_INITIALIZER("restore page color");
    345 
    346 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new);
    347 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse);
    348 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok);
    349 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_blind);
    350 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change);
    351 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase);
    352 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none);
    353 EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore);
    354 #endif
    355 
    356 static struct evcnt pmap_ev_mappings =
    357    PMAP_EVCNT_INITIALIZER("pages mapped");
    358 static struct evcnt pmap_ev_unmappings =
    359    PMAP_EVCNT_INITIALIZER("pages unmapped");
    360 static struct evcnt pmap_ev_remappings =
    361    PMAP_EVCNT_INITIALIZER("pages remapped");
    362 
    363 EVCNT_ATTACH_STATIC(pmap_ev_mappings);
    364 EVCNT_ATTACH_STATIC(pmap_ev_unmappings);
    365 EVCNT_ATTACH_STATIC(pmap_ev_remappings);
    366 
    367 static struct evcnt pmap_ev_kernel_mappings =
    368    PMAP_EVCNT_INITIALIZER("kernel pages mapped");
    369 static struct evcnt pmap_ev_kernel_unmappings =
    370    PMAP_EVCNT_INITIALIZER("kernel pages unmapped");
    371 static struct evcnt pmap_ev_kernel_remappings =
    372    PMAP_EVCNT_INITIALIZER("kernel pages remapped");
    373 
    374 EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings);
    375 EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings);
    376 EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings);
    377 
    378 static struct evcnt pmap_ev_kenter_mappings =
    379    PMAP_EVCNT_INITIALIZER("kenter pages mapped");
    380 static struct evcnt pmap_ev_kenter_unmappings =
    381    PMAP_EVCNT_INITIALIZER("kenter pages unmapped");
    382 static struct evcnt pmap_ev_kenter_remappings =
    383    PMAP_EVCNT_INITIALIZER("kenter pages remapped");
    384 static struct evcnt pmap_ev_pt_mappings =
    385    PMAP_EVCNT_INITIALIZER("page table pages mapped");
    386 
    387 EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings);
    388 EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings);
    389 EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings);
    390 EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings);
    391 
    392 #ifdef PMAP_CACHE_VIPT
    393 static struct evcnt pmap_ev_exec_mappings =
    394    PMAP_EVCNT_INITIALIZER("exec pages mapped");
    395 static struct evcnt pmap_ev_exec_cached =
    396    PMAP_EVCNT_INITIALIZER("exec pages cached");
    397 
    398 EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings);
    399 EVCNT_ATTACH_STATIC(pmap_ev_exec_cached);
    400 
    401 static struct evcnt pmap_ev_exec_synced =
    402    PMAP_EVCNT_INITIALIZER("exec pages synced");
    403 static struct evcnt pmap_ev_exec_synced_map =
    404    PMAP_EVCNT_INITIALIZER("exec pages synced (MP)");
    405 static struct evcnt pmap_ev_exec_synced_unmap =
    406    PMAP_EVCNT_INITIALIZER("exec pages synced (UM)");
    407 static struct evcnt pmap_ev_exec_synced_remap =
    408    PMAP_EVCNT_INITIALIZER("exec pages synced (RM)");
    409 static struct evcnt pmap_ev_exec_synced_clearbit =
    410    PMAP_EVCNT_INITIALIZER("exec pages synced (DG)");
    411 static struct evcnt pmap_ev_exec_synced_kremove =
    412    PMAP_EVCNT_INITIALIZER("exec pages synced (KU)");
    413 
    414 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced);
    415 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map);
    416 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap);
    417 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap);
    418 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit);
    419 EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove);
    420 
    421 static struct evcnt pmap_ev_exec_discarded_unmap =
    422    PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)");
    423 static struct evcnt pmap_ev_exec_discarded_zero =
    424    PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)");
    425 static struct evcnt pmap_ev_exec_discarded_copy =
    426    PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)");
    427 static struct evcnt pmap_ev_exec_discarded_page_protect =
    428    PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)");
    429 static struct evcnt pmap_ev_exec_discarded_clearbit =
    430    PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)");
    431 static struct evcnt pmap_ev_exec_discarded_kremove =
    432    PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)");
    433 
    434 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap);
    435 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero);
    436 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy);
    437 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect);
    438 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit);
    439 EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove);
    440 #endif /* PMAP_CACHE_VIPT */
    441 
    442 static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates");
    443 static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects");
    444 static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations");
    445 
    446 EVCNT_ATTACH_STATIC(pmap_ev_updates);
    447 EVCNT_ATTACH_STATIC(pmap_ev_collects);
    448 EVCNT_ATTACH_STATIC(pmap_ev_activations);
    449 
    450 #define	PMAPCOUNT(x)	((void)(pmap_ev_##x.ev_count++))
    451 #else
    452 #define	PMAPCOUNT(x)	((void)0)
    453 #endif
    454 
    455 /*
    456  * pmap copy/zero page, and mem(5) hook point
    457  */
    458 static pt_entry_t *csrc_pte, *cdst_pte;
    459 static vaddr_t csrcp, cdstp;
    460 vaddr_t memhook;			/* used by mem.c */
    461 kmutex_t memlock;			/* used by mem.c */
    462 void *zeropage;				/* used by mem.c */
    463 extern void *msgbufaddr;
    464 int pmap_kmpages;
    465 /*
    466  * Flag to indicate if pmap_init() has done its thing
    467  */
    468 bool pmap_initialized;
    469 
    470 /*
    471  * Misc. locking data structures
    472  */
    473 
    474 #define	pmap_acquire_pmap_lock(pm)			\
    475 	do {						\
    476 		if ((pm) != pmap_kernel())		\
    477 			mutex_enter((pm)->pm_lock);	\
    478 	} while (/*CONSTCOND*/0)
    479 
    480 #define	pmap_release_pmap_lock(pm)			\
    481 	do {						\
    482 		if ((pm) != pmap_kernel())		\
    483 			mutex_exit((pm)->pm_lock);	\
    484 	} while (/*CONSTCOND*/0)
    485 
    486 
    487 /*
    488  * Metadata for L1 translation tables.
    489  */
    490 struct l1_ttable {
    491 	/* Entry on the L1 Table list */
    492 	SLIST_ENTRY(l1_ttable) l1_link;
    493 
    494 	/* Entry on the L1 Least Recently Used list */
    495 	TAILQ_ENTRY(l1_ttable) l1_lru;
    496 
    497 	/* Track how many domains are allocated from this L1 */
    498 	volatile u_int l1_domain_use_count;
    499 
    500 	/*
    501 	 * A free-list of domain numbers for this L1.
    502 	 * We avoid using ffs() and a bitmap to track domains since ffs()
    503 	 * is slow on ARM.
    504 	 */
    505 	uint8_t l1_domain_first;
    506 	uint8_t l1_domain_free[PMAP_DOMAINS];
    507 
    508 	/* Physical address of this L1 page table */
    509 	paddr_t l1_physaddr;
    510 
    511 	/* KVA of this L1 page table */
    512 	pd_entry_t *l1_kva;
    513 };
    514 
    515 /*
    516  * Convert a virtual address into its L1 table index. That is, the
    517  * index used to locate the L2 descriptor table pointer in an L1 table.
    518  * This is basically used to index l1->l1_kva[].
    519  *
    520  * Each L2 descriptor table represents 1MB of VA space.
    521  */
    522 #define	L1_IDX(va)		(((vaddr_t)(va)) >> L1_S_SHIFT)
    523 
    524 /*
    525  * L1 Page Tables are tracked using a Least Recently Used list.
    526  *  - New L1s are allocated from the HEAD.
    527  *  - Freed L1s are added to the TAIl.
    528  *  - Recently accessed L1s (where an 'access' is some change to one of
    529  *    the userland pmaps which owns this L1) are moved to the TAIL.
    530  */
    531 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
    532 static kmutex_t l1_lru_lock __cacheline_aligned;
    533 
    534 /*
    535  * A list of all L1 tables
    536  */
    537 static SLIST_HEAD(, l1_ttable) l1_list;
    538 
    539 /*
    540  * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
    541  *
    542  * This is normally 16MB worth L2 page descriptors for any given pmap.
    543  * Reference counts are maintained for L2 descriptors so they can be
    544  * freed when empty.
    545  */
    546 struct l2_dtable {
    547 	/* The number of L2 page descriptors allocated to this l2_dtable */
    548 	u_int l2_occupancy;
    549 
    550 	/* List of L2 page descriptors */
    551 	struct l2_bucket {
    552 		pt_entry_t *l2b_kva;	/* KVA of L2 Descriptor Table */
    553 		paddr_t l2b_phys;	/* Physical address of same */
    554 		u_short l2b_l1idx;	/* This L2 table's L1 index */
    555 		u_short l2b_occupancy;	/* How many active descriptors */
    556 	} l2_bucket[L2_BUCKET_SIZE];
    557 };
    558 
    559 /*
    560  * Given an L1 table index, calculate the corresponding l2_dtable index
    561  * and bucket index within the l2_dtable.
    562  */
    563 #define	L2_IDX(l1idx)		(((l1idx) >> L2_BUCKET_LOG2) & \
    564 				 (L2_SIZE - 1))
    565 #define	L2_BUCKET(l1idx)	((l1idx) & (L2_BUCKET_SIZE - 1))
    566 
    567 /*
    568  * Given a virtual address, this macro returns the
    569  * virtual address required to drop into the next L2 bucket.
    570  */
    571 #define	L2_NEXT_BUCKET(va)	(((va) & L1_S_FRAME) + L1_S_SIZE)
    572 
    573 /*
    574  * L2 allocation.
    575  */
    576 #define	pmap_alloc_l2_dtable()		\
    577 	    pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
    578 #define	pmap_free_l2_dtable(l2)		\
    579 	    pool_cache_put(&pmap_l2dtable_cache, (l2))
    580 #define pmap_alloc_l2_ptp(pap)		\
    581 	    ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
    582 	    PR_NOWAIT, (pap)))
    583 
    584 /*
    585  * We try to map the page tables write-through, if possible.  However, not
    586  * all CPUs have a write-through cache mode, so on those we have to sync
    587  * the cache when we frob page tables.
    588  *
    589  * We try to evaluate this at compile time, if possible.  However, it's
    590  * not always possible to do that, hence this run-time var.
    591  */
    592 int	pmap_needs_pte_sync;
    593 
    594 /*
    595  * Real definition of pv_entry.
    596  */
    597 struct pv_entry {
    598 	SLIST_ENTRY(pv_entry) pv_link;	/* next pv_entry */
    599 	pmap_t		pv_pmap;        /* pmap where mapping lies */
    600 	vaddr_t		pv_va;          /* virtual address for mapping */
    601 	u_int		pv_flags;       /* flags */
    602 };
    603 
    604 /*
    605  * Macro to determine if a mapping might be resident in the
    606  * instruction cache and/or TLB
    607  */
    608 #define	PV_BEEN_EXECD(f)  (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
    609 #define	PV_IS_EXEC_P(f)   (((f) & PVF_EXEC) != 0)
    610 
    611 /*
    612  * Macro to determine if a mapping might be resident in the
    613  * data cache and/or TLB
    614  */
    615 #define	PV_BEEN_REFD(f)   (((f) & PVF_REF) != 0)
    616 
    617 /*
    618  * Local prototypes
    619  */
    620 static int		pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t);
    621 static void		pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
    622 			    pt_entry_t **);
    623 static bool		pmap_is_current(pmap_t);
    624 static bool		pmap_is_cached(pmap_t);
    625 static void		pmap_enter_pv(struct vm_page_md *, paddr_t, struct pv_entry *,
    626 			    pmap_t, vaddr_t, u_int);
    627 static struct pv_entry *pmap_find_pv(struct vm_page_md *, pmap_t, vaddr_t);
    628 static struct pv_entry *pmap_remove_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
    629 static u_int		pmap_modify_pv(struct vm_page_md *, paddr_t, pmap_t, vaddr_t,
    630 			    u_int, u_int);
    631 
    632 static void		pmap_pinit(pmap_t);
    633 static int		pmap_pmap_ctor(void *, void *, int);
    634 
    635 static void		pmap_alloc_l1(pmap_t);
    636 static void		pmap_free_l1(pmap_t);
    637 static void		pmap_use_l1(pmap_t);
    638 
    639 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
    640 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
    641 static void		pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
    642 static int		pmap_l2ptp_ctor(void *, void *, int);
    643 static int		pmap_l2dtable_ctor(void *, void *, int);
    644 
    645 static void		pmap_vac_me_harder(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
    646 #ifdef PMAP_CACHE_VIVT
    647 static void		pmap_vac_me_kpmap(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
    648 static void		pmap_vac_me_user(struct vm_page_md *, paddr_t, pmap_t, vaddr_t);
    649 #endif
    650 
    651 static void		pmap_clearbit(struct vm_page_md *, paddr_t, u_int);
    652 #ifdef PMAP_CACHE_VIVT
    653 static int		pmap_clean_page(struct pv_entry *, bool);
    654 #endif
    655 #ifdef PMAP_CACHE_VIPT
    656 static void		pmap_syncicache_page(struct vm_page_md *, paddr_t);
    657 enum pmap_flush_op {
    658 	PMAP_FLUSH_PRIMARY,
    659 	PMAP_FLUSH_SECONDARY,
    660 	PMAP_CLEAN_PRIMARY
    661 };
    662 static void		pmap_flush_page(struct vm_page_md *, paddr_t, enum pmap_flush_op);
    663 #endif
    664 static void		pmap_page_remove(struct vm_page_md *, paddr_t);
    665 
    666 static void		pmap_init_l1(struct l1_ttable *, pd_entry_t *);
    667 static vaddr_t		kernel_pt_lookup(paddr_t);
    668 
    669 
    670 /*
    671  * Misc variables
    672  */
    673 vaddr_t virtual_avail;
    674 vaddr_t virtual_end;
    675 vaddr_t pmap_curmaxkvaddr;
    676 
    677 paddr_t avail_start;
    678 paddr_t avail_end;
    679 
    680 pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq);
    681 pv_addr_t kernelpages;
    682 pv_addr_t kernel_l1pt;
    683 pv_addr_t systempage;
    684 
    685 /* Function to set the debug level of the pmap code */
    686 
    687 #ifdef PMAP_DEBUG
    688 void
    689 pmap_debug(int level)
    690 {
    691 	pmap_debug_level = level;
    692 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    693 }
    694 #endif	/* PMAP_DEBUG */
    695 
    696 #ifdef PMAP_CACHE_VIPT
    697 #define PMAP_VALIDATE_MD_PAGE(md)	\
    698 	KASSERTMSG(arm_cache_prefer_mask == 0 || (((md)->pvh_attrs & PVF_WRITE) == 0) == ((md)->urw_mappings + (md)->krw_mappings == 0), \
    699 	    "(md) %p: attrs=%#x urw=%u krw=%u", (md), \
    700 	    (md)->pvh_attrs, (md)->urw_mappings, (md)->krw_mappings);
    701 #endif /* PMAP_CACHE_VIPT */
    702 /*
    703  * A bunch of routines to conditionally flush the caches/TLB depending
    704  * on whether the specified pmap actually needs to be flushed at any
    705  * given time.
    706  */
    707 static inline void
    708 pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va)
    709 {
    710 
    711 	if (pm->pm_cstate.cs_tlb_id)
    712 		cpu_tlb_flushID_SE(va);
    713 }
    714 
    715 static inline void
    716 pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va)
    717 {
    718 
    719 	if (pm->pm_cstate.cs_tlb_d)
    720 		cpu_tlb_flushD_SE(va);
    721 }
    722 
    723 static inline void
    724 pmap_tlb_flushID(pmap_t pm)
    725 {
    726 
    727 	if (pm->pm_cstate.cs_tlb_id) {
    728 		cpu_tlb_flushID();
    729 		pm->pm_cstate.cs_tlb = 0;
    730 	}
    731 }
    732 
    733 static inline void
    734 pmap_tlb_flushD(pmap_t pm)
    735 {
    736 
    737 	if (pm->pm_cstate.cs_tlb_d) {
    738 		cpu_tlb_flushD();
    739 		pm->pm_cstate.cs_tlb_d = 0;
    740 	}
    741 }
    742 
    743 #ifdef PMAP_CACHE_VIVT
    744 static inline void
    745 pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len)
    746 {
    747 	if (pm->pm_cstate.cs_cache_id) {
    748 		cpu_idcache_wbinv_range(va, len);
    749 	}
    750 }
    751 
    752 static inline void
    753 pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len,
    754     bool do_inv, bool rd_only)
    755 {
    756 
    757 	if (pm->pm_cstate.cs_cache_d) {
    758 		if (do_inv) {
    759 			if (rd_only)
    760 				cpu_dcache_inv_range(va, len);
    761 			else
    762 				cpu_dcache_wbinv_range(va, len);
    763 		} else
    764 		if (!rd_only)
    765 			cpu_dcache_wb_range(va, len);
    766 	}
    767 }
    768 
    769 static inline void
    770 pmap_idcache_wbinv_all(pmap_t pm)
    771 {
    772 	if (pm->pm_cstate.cs_cache_id) {
    773 		cpu_idcache_wbinv_all();
    774 		pm->pm_cstate.cs_cache = 0;
    775 	}
    776 }
    777 
    778 static inline void
    779 pmap_dcache_wbinv_all(pmap_t pm)
    780 {
    781 	if (pm->pm_cstate.cs_cache_d) {
    782 		cpu_dcache_wbinv_all();
    783 		pm->pm_cstate.cs_cache_d = 0;
    784 	}
    785 }
    786 #endif /* PMAP_CACHE_VIVT */
    787 
    788 static inline bool
    789 pmap_is_current(pmap_t pm)
    790 {
    791 
    792 	if (pm == pmap_kernel() || curproc->p_vmspace->vm_map.pmap == pm)
    793 		return true;
    794 
    795 	return false;
    796 }
    797 
    798 static inline bool
    799 pmap_is_cached(pmap_t pm)
    800 {
    801 
    802 	if (pm == pmap_kernel() || pmap_recent_user == NULL ||
    803 	    pmap_recent_user == pm)
    804 		return (true);
    805 
    806 	return false;
    807 }
    808 
    809 /*
    810  * PTE_SYNC_CURRENT:
    811  *
    812  *     Make sure the pte is written out to RAM.
    813  *     We need to do this for one of two cases:
    814  *       - We're dealing with the kernel pmap
    815  *       - There is no pmap active in the cache/tlb.
    816  *       - The specified pmap is 'active' in the cache/tlb.
    817  */
    818 #ifdef PMAP_INCLUDE_PTE_SYNC
    819 #define	PTE_SYNC_CURRENT(pm, ptep)	\
    820 do {					\
    821 	if (PMAP_NEEDS_PTE_SYNC && 	\
    822 	    pmap_is_cached(pm))		\
    823 		PTE_SYNC(ptep);		\
    824 } while (/*CONSTCOND*/0)
    825 #else
    826 #define	PTE_SYNC_CURRENT(pm, ptep)	/* nothing */
    827 #endif
    828 
    829 /*
    830  * main pv_entry manipulation functions:
    831  *   pmap_enter_pv: enter a mapping onto a vm_page list
    832  *   pmap_remove_pv: remove a mapping from a vm_page list
    833  *
    834  * NOTE: pmap_enter_pv expects to lock the pvh itself
    835  *       pmap_remove_pv expects the caller to lock the pvh before calling
    836  */
    837 
    838 /*
    839  * pmap_enter_pv: enter a mapping onto a vm_page lst
    840  *
    841  * => caller should hold the proper lock on pmap_main_lock
    842  * => caller should have pmap locked
    843  * => we will gain the lock on the vm_page and allocate the new pv_entry
    844  * => caller should adjust ptp's wire_count before calling
    845  * => caller should not adjust pmap's wire_count
    846  */
    847 static void
    848 pmap_enter_pv(struct vm_page_md *md, paddr_t pa, struct pv_entry *pv, pmap_t pm,
    849     vaddr_t va, u_int flags)
    850 {
    851 	struct pv_entry **pvp;
    852 
    853 	NPDEBUG(PDB_PVDUMP,
    854 	    printf("pmap_enter_pv: pm %p, md %p, flags 0x%x\n", pm, md, flags));
    855 
    856 	pv->pv_pmap = pm;
    857 	pv->pv_va = va;
    858 	pv->pv_flags = flags;
    859 
    860 	pvp = &SLIST_FIRST(&md->pvh_list);
    861 #ifdef PMAP_CACHE_VIPT
    862 	/*
    863 	 * Insert unmanaged entries, writeable first, at the head of
    864 	 * the pv list.
    865 	 */
    866 	if (__predict_true((flags & PVF_KENTRY) == 0)) {
    867 		while (*pvp != NULL && (*pvp)->pv_flags & PVF_KENTRY)
    868 			pvp = &SLIST_NEXT(*pvp, pv_link);
    869 	} else if ((flags & PVF_WRITE) == 0) {
    870 		while (*pvp != NULL && (*pvp)->pv_flags & PVF_WRITE)
    871 			pvp = &SLIST_NEXT(*pvp, pv_link);
    872 	}
    873 #endif
    874 	SLIST_NEXT(pv, pv_link) = *pvp;		/* add to ... */
    875 	*pvp = pv;				/* ... locked list */
    876 	md->pvh_attrs |= flags & (PVF_REF | PVF_MOD);
    877 #ifdef PMAP_CACHE_VIPT
    878 	if ((pv->pv_flags & PVF_KWRITE) == PVF_KWRITE)
    879 		md->pvh_attrs |= PVF_KMOD;
    880 	if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
    881 		md->pvh_attrs |= PVF_DIRTY;
    882 	KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
    883 #endif
    884 	if (pm == pmap_kernel()) {
    885 		PMAPCOUNT(kernel_mappings);
    886 		if (flags & PVF_WRITE)
    887 			md->krw_mappings++;
    888 		else
    889 			md->kro_mappings++;
    890 	} else {
    891 		if (flags & PVF_WRITE)
    892 			md->urw_mappings++;
    893 		else
    894 			md->uro_mappings++;
    895 	}
    896 
    897 #ifdef PMAP_CACHE_VIPT
    898 	/*
    899 	 * Even though pmap_vac_me_harder will set PVF_WRITE for us,
    900 	 * do it here as well to keep the mappings & KVF_WRITE consistent.
    901 	 */
    902 	if (arm_cache_prefer_mask != 0 && (flags & PVF_WRITE) != 0) {
    903 		md->pvh_attrs |= PVF_WRITE;
    904 	}
    905 	/*
    906 	 * If this is an exec mapping and its the first exec mapping
    907 	 * for this page, make sure to sync the I-cache.
    908 	 */
    909 	if (PV_IS_EXEC_P(flags)) {
    910 		if (!PV_IS_EXEC_P(md->pvh_attrs)) {
    911 			pmap_syncicache_page(md, pa);
    912 			PMAPCOUNT(exec_synced_map);
    913 		}
    914 		PMAPCOUNT(exec_mappings);
    915 	}
    916 #endif
    917 
    918 	PMAPCOUNT(mappings);
    919 
    920 	if (pv->pv_flags & PVF_WIRED)
    921 		++pm->pm_stats.wired_count;
    922 }
    923 
    924 /*
    925  *
    926  * pmap_find_pv: Find a pv entry
    927  *
    928  * => caller should hold lock on vm_page
    929  */
    930 static inline struct pv_entry *
    931 pmap_find_pv(struct vm_page_md *md, pmap_t pm, vaddr_t va)
    932 {
    933 	struct pv_entry *pv;
    934 
    935 	SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
    936 		if (pm == pv->pv_pmap && va == pv->pv_va)
    937 			break;
    938 	}
    939 
    940 	return (pv);
    941 }
    942 
    943 /*
    944  * pmap_remove_pv: try to remove a mapping from a pv_list
    945  *
    946  * => caller should hold proper lock on pmap_main_lock
    947  * => pmap should be locked
    948  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    949  * => caller should adjust ptp's wire_count and free PTP if needed
    950  * => caller should NOT adjust pmap's wire_count
    951  * => we return the removed pv
    952  */
    953 static struct pv_entry *
    954 pmap_remove_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
    955 {
    956 	struct pv_entry *pv, **prevptr;
    957 
    958 	NPDEBUG(PDB_PVDUMP,
    959 	    printf("pmap_remove_pv: pm %p, md %p, va 0x%08lx\n", pm, md, va));
    960 
    961 	prevptr = &SLIST_FIRST(&md->pvh_list); /* prev pv_entry ptr */
    962 	pv = *prevptr;
    963 
    964 	while (pv) {
    965 		if (pv->pv_pmap == pm && pv->pv_va == va) {	/* match? */
    966 			NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, md "
    967 			    "%p, flags 0x%x\n", pm, md, pv->pv_flags));
    968 			if (pv->pv_flags & PVF_WIRED) {
    969 				--pm->pm_stats.wired_count;
    970 			}
    971 			*prevptr = SLIST_NEXT(pv, pv_link);	/* remove it! */
    972 			if (pm == pmap_kernel()) {
    973 				PMAPCOUNT(kernel_unmappings);
    974 				if (pv->pv_flags & PVF_WRITE)
    975 					md->krw_mappings--;
    976 				else
    977 					md->kro_mappings--;
    978 			} else {
    979 				if (pv->pv_flags & PVF_WRITE)
    980 					md->urw_mappings--;
    981 				else
    982 					md->uro_mappings--;
    983 			}
    984 
    985 			PMAPCOUNT(unmappings);
    986 #ifdef PMAP_CACHE_VIPT
    987 			if (!(pv->pv_flags & PVF_WRITE))
    988 				break;
    989 			/*
    990 			 * If this page has had an exec mapping, then if
    991 			 * this was the last mapping, discard the contents,
    992 			 * otherwise sync the i-cache for this page.
    993 			 */
    994 			if (PV_IS_EXEC_P(md->pvh_attrs)) {
    995 				if (SLIST_EMPTY(&md->pvh_list)) {
    996 					md->pvh_attrs &= ~PVF_EXEC;
    997 					PMAPCOUNT(exec_discarded_unmap);
    998 				} else {
    999 					pmap_syncicache_page(md, pa);
   1000 					PMAPCOUNT(exec_synced_unmap);
   1001 				}
   1002 			}
   1003 #endif /* PMAP_CACHE_VIPT */
   1004 			break;
   1005 		}
   1006 		prevptr = &SLIST_NEXT(pv, pv_link);	/* previous pointer */
   1007 		pv = *prevptr;				/* advance */
   1008 	}
   1009 
   1010 #ifdef PMAP_CACHE_VIPT
   1011 	/*
   1012 	 * If we no longer have a WRITEABLE KENTRY at the head of list,
   1013 	 * clear the KMOD attribute from the page.
   1014 	 */
   1015 	if (SLIST_FIRST(&md->pvh_list) == NULL
   1016 	    || (SLIST_FIRST(&md->pvh_list)->pv_flags & PVF_KWRITE) != PVF_KWRITE)
   1017 		md->pvh_attrs &= ~PVF_KMOD;
   1018 
   1019 	/*
   1020 	 * If this was a writeable page and there are no more writeable
   1021 	 * mappings (ignoring KMPAGE), clear the WRITE flag and writeback
   1022 	 * the contents to memory.
   1023 	 */
   1024 	if (arm_cache_prefer_mask != 0) {
   1025 		if (md->krw_mappings + md->urw_mappings == 0)
   1026 			md->pvh_attrs &= ~PVF_WRITE;
   1027 		PMAP_VALIDATE_MD_PAGE(md);
   1028 	}
   1029 	KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1030 #endif /* PMAP_CACHE_VIPT */
   1031 
   1032 	return(pv);				/* return removed pv */
   1033 }
   1034 
   1035 /*
   1036  *
   1037  * pmap_modify_pv: Update pv flags
   1038  *
   1039  * => caller should hold lock on vm_page [so that attrs can be adjusted]
   1040  * => caller should NOT adjust pmap's wire_count
   1041  * => caller must call pmap_vac_me_harder() if writable status of a page
   1042  *    may have changed.
   1043  * => we return the old flags
   1044  *
   1045  * Modify a physical-virtual mapping in the pv table
   1046  */
   1047 static u_int
   1048 pmap_modify_pv(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va,
   1049     u_int clr_mask, u_int set_mask)
   1050 {
   1051 	struct pv_entry *npv;
   1052 	u_int flags, oflags;
   1053 
   1054 	KASSERT((clr_mask & PVF_KENTRY) == 0);
   1055 	KASSERT((set_mask & PVF_KENTRY) == 0);
   1056 
   1057 	if ((npv = pmap_find_pv(md, pm, va)) == NULL)
   1058 		return (0);
   1059 
   1060 	NPDEBUG(PDB_PVDUMP,
   1061 	    printf("pmap_modify_pv: pm %p, md %p, clr 0x%x, set 0x%x, flags 0x%x\n", pm, md, clr_mask, set_mask, npv->pv_flags));
   1062 
   1063 	/*
   1064 	 * There is at least one VA mapping this page.
   1065 	 */
   1066 
   1067 	if (clr_mask & (PVF_REF | PVF_MOD)) {
   1068 		md->pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
   1069 #ifdef PMAP_CACHE_VIPT
   1070 		if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) != PVF_NC)
   1071 			md->pvh_attrs |= PVF_DIRTY;
   1072 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1073 #endif
   1074 	}
   1075 
   1076 	oflags = npv->pv_flags;
   1077 	npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
   1078 
   1079 	if ((flags ^ oflags) & PVF_WIRED) {
   1080 		if (flags & PVF_WIRED)
   1081 			++pm->pm_stats.wired_count;
   1082 		else
   1083 			--pm->pm_stats.wired_count;
   1084 	}
   1085 
   1086 	if ((flags ^ oflags) & PVF_WRITE) {
   1087 		if (pm == pmap_kernel()) {
   1088 			if (flags & PVF_WRITE) {
   1089 				md->krw_mappings++;
   1090 				md->kro_mappings--;
   1091 			} else {
   1092 				md->kro_mappings++;
   1093 				md->krw_mappings--;
   1094 			}
   1095 		} else {
   1096 			if (flags & PVF_WRITE) {
   1097 				md->urw_mappings++;
   1098 				md->uro_mappings--;
   1099 			} else {
   1100 				md->uro_mappings++;
   1101 				md->urw_mappings--;
   1102 			}
   1103 		}
   1104 	}
   1105 #ifdef PMAP_CACHE_VIPT
   1106 	if (arm_cache_prefer_mask != 0) {
   1107 		if (md->urw_mappings + md->krw_mappings == 0) {
   1108 			md->pvh_attrs &= ~PVF_WRITE;
   1109 		} else {
   1110 			md->pvh_attrs |= PVF_WRITE;
   1111 		}
   1112 	}
   1113 	/*
   1114 	 * We have two cases here: the first is from enter_pv (new exec
   1115 	 * page), the second is a combined pmap_remove_pv/pmap_enter_pv.
   1116 	 * Since in latter, pmap_enter_pv won't do anything, we just have
   1117 	 * to do what pmap_remove_pv would do.
   1118 	 */
   1119 	if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(md->pvh_attrs))
   1120 	    || (PV_IS_EXEC_P(md->pvh_attrs)
   1121 		|| (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) {
   1122 		pmap_syncicache_page(md, pa);
   1123 		PMAPCOUNT(exec_synced_remap);
   1124 	}
   1125 	KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1126 #endif
   1127 
   1128 	PMAPCOUNT(remappings);
   1129 
   1130 	return (oflags);
   1131 }
   1132 
   1133 /*
   1134  * Allocate an L1 translation table for the specified pmap.
   1135  * This is called at pmap creation time.
   1136  */
   1137 static void
   1138 pmap_alloc_l1(pmap_t pm)
   1139 {
   1140 	struct l1_ttable *l1;
   1141 	uint8_t domain;
   1142 
   1143 	/*
   1144 	 * Remove the L1 at the head of the LRU list
   1145 	 */
   1146 	mutex_spin_enter(&l1_lru_lock);
   1147 	l1 = TAILQ_FIRST(&l1_lru_list);
   1148 	KDASSERT(l1 != NULL);
   1149 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1150 
   1151 	/*
   1152 	 * Pick the first available domain number, and update
   1153 	 * the link to the next number.
   1154 	 */
   1155 	domain = l1->l1_domain_first;
   1156 	l1->l1_domain_first = l1->l1_domain_free[domain];
   1157 
   1158 	/*
   1159 	 * If there are still free domain numbers in this L1,
   1160 	 * put it back on the TAIL of the LRU list.
   1161 	 */
   1162 	if (++l1->l1_domain_use_count < PMAP_DOMAINS)
   1163 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1164 
   1165 	mutex_spin_exit(&l1_lru_lock);
   1166 
   1167 	/*
   1168 	 * Fix up the relevant bits in the pmap structure
   1169 	 */
   1170 	pm->pm_l1 = l1;
   1171 	pm->pm_domain = domain + 1;
   1172 }
   1173 
   1174 /*
   1175  * Free an L1 translation table.
   1176  * This is called at pmap destruction time.
   1177  */
   1178 static void
   1179 pmap_free_l1(pmap_t pm)
   1180 {
   1181 	struct l1_ttable *l1 = pm->pm_l1;
   1182 
   1183 	mutex_spin_enter(&l1_lru_lock);
   1184 
   1185 	/*
   1186 	 * If this L1 is currently on the LRU list, remove it.
   1187 	 */
   1188 	if (l1->l1_domain_use_count < PMAP_DOMAINS)
   1189 		TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1190 
   1191 	/*
   1192 	 * Free up the domain number which was allocated to the pmap
   1193 	 */
   1194 	l1->l1_domain_free[pm->pm_domain - 1] = l1->l1_domain_first;
   1195 	l1->l1_domain_first = pm->pm_domain - 1;
   1196 	l1->l1_domain_use_count--;
   1197 
   1198 	/*
   1199 	 * The L1 now must have at least 1 free domain, so add
   1200 	 * it back to the LRU list. If the use count is zero,
   1201 	 * put it at the head of the list, otherwise it goes
   1202 	 * to the tail.
   1203 	 */
   1204 	if (l1->l1_domain_use_count == 0)
   1205 		TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
   1206 	else
   1207 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1208 
   1209 	mutex_spin_exit(&l1_lru_lock);
   1210 }
   1211 
   1212 static inline void
   1213 pmap_use_l1(pmap_t pm)
   1214 {
   1215 	struct l1_ttable *l1;
   1216 
   1217 	/*
   1218 	 * Do nothing if we're in interrupt context.
   1219 	 * Access to an L1 by the kernel pmap must not affect
   1220 	 * the LRU list.
   1221 	 */
   1222 	if (cpu_intr_p() || pm == pmap_kernel())
   1223 		return;
   1224 
   1225 	l1 = pm->pm_l1;
   1226 
   1227 	/*
   1228 	 * If the L1 is not currently on the LRU list, just return
   1229 	 */
   1230 	if (l1->l1_domain_use_count == PMAP_DOMAINS)
   1231 		return;
   1232 
   1233 	mutex_spin_enter(&l1_lru_lock);
   1234 
   1235 	/*
   1236 	 * Check the use count again, now that we've acquired the lock
   1237 	 */
   1238 	if (l1->l1_domain_use_count == PMAP_DOMAINS) {
   1239 		mutex_spin_exit(&l1_lru_lock);
   1240 		return;
   1241 	}
   1242 
   1243 	/*
   1244 	 * Move the L1 to the back of the LRU list
   1245 	 */
   1246 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1247 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1248 
   1249 	mutex_spin_exit(&l1_lru_lock);
   1250 }
   1251 
   1252 /*
   1253  * void pmap_free_l2_ptp(pt_entry_t *, paddr_t *)
   1254  *
   1255  * Free an L2 descriptor table.
   1256  */
   1257 static inline void
   1258 #ifndef PMAP_INCLUDE_PTE_SYNC
   1259 pmap_free_l2_ptp(pt_entry_t *l2, paddr_t pa)
   1260 #else
   1261 pmap_free_l2_ptp(bool need_sync, pt_entry_t *l2, paddr_t pa)
   1262 #endif
   1263 {
   1264 #ifdef PMAP_INCLUDE_PTE_SYNC
   1265 #ifdef PMAP_CACHE_VIVT
   1266 	/*
   1267 	 * Note: With a write-back cache, we may need to sync this
   1268 	 * L2 table before re-using it.
   1269 	 * This is because it may have belonged to a non-current
   1270 	 * pmap, in which case the cache syncs would have been
   1271 	 * skipped for the pages that were being unmapped. If the
   1272 	 * L2 table were then to be immediately re-allocated to
   1273 	 * the *current* pmap, it may well contain stale mappings
   1274 	 * which have not yet been cleared by a cache write-back
   1275 	 * and so would still be visible to the mmu.
   1276 	 */
   1277 	if (need_sync)
   1278 		PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1279 #endif /* PMAP_CACHE_VIVT */
   1280 #endif /* PMAP_INCLUDE_PTE_SYNC */
   1281 	pool_cache_put_paddr(&pmap_l2ptp_cache, (void *)l2, pa);
   1282 }
   1283 
   1284 /*
   1285  * Returns a pointer to the L2 bucket associated with the specified pmap
   1286  * and VA, or NULL if no L2 bucket exists for the address.
   1287  */
   1288 static inline struct l2_bucket *
   1289 pmap_get_l2_bucket(pmap_t pm, vaddr_t va)
   1290 {
   1291 	struct l2_dtable *l2;
   1292 	struct l2_bucket *l2b;
   1293 	u_short l1idx;
   1294 
   1295 	l1idx = L1_IDX(va);
   1296 
   1297 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
   1298 	    (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
   1299 		return (NULL);
   1300 
   1301 	return (l2b);
   1302 }
   1303 
   1304 /*
   1305  * Returns a pointer to the L2 bucket associated with the specified pmap
   1306  * and VA.
   1307  *
   1308  * If no L2 bucket exists, perform the necessary allocations to put an L2
   1309  * bucket/page table in place.
   1310  *
   1311  * Note that if a new L2 bucket/page was allocated, the caller *must*
   1312  * increment the bucket occupancy counter appropriately *before*
   1313  * releasing the pmap's lock to ensure no other thread or cpu deallocates
   1314  * the bucket/page in the meantime.
   1315  */
   1316 static struct l2_bucket *
   1317 pmap_alloc_l2_bucket(pmap_t pm, vaddr_t va)
   1318 {
   1319 	struct l2_dtable *l2;
   1320 	struct l2_bucket *l2b;
   1321 	u_short l1idx;
   1322 
   1323 	l1idx = L1_IDX(va);
   1324 
   1325 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   1326 		/*
   1327 		 * No mapping at this address, as there is
   1328 		 * no entry in the L1 table.
   1329 		 * Need to allocate a new l2_dtable.
   1330 		 */
   1331 		if ((l2 = pmap_alloc_l2_dtable()) == NULL)
   1332 			return (NULL);
   1333 
   1334 		/*
   1335 		 * Link it into the parent pmap
   1336 		 */
   1337 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   1338 	}
   1339 
   1340 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   1341 
   1342 	/*
   1343 	 * Fetch pointer to the L2 page table associated with the address.
   1344 	 */
   1345 	if (l2b->l2b_kva == NULL) {
   1346 		pt_entry_t *ptep;
   1347 
   1348 		/*
   1349 		 * No L2 page table has been allocated. Chances are, this
   1350 		 * is because we just allocated the l2_dtable, above.
   1351 		 */
   1352 		if ((ptep = pmap_alloc_l2_ptp(&l2b->l2b_phys)) == NULL) {
   1353 			/*
   1354 			 * Oops, no more L2 page tables available at this
   1355 			 * time. We may need to deallocate the l2_dtable
   1356 			 * if we allocated a new one above.
   1357 			 */
   1358 			if (l2->l2_occupancy == 0) {
   1359 				pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1360 				pmap_free_l2_dtable(l2);
   1361 			}
   1362 			return (NULL);
   1363 		}
   1364 
   1365 		l2->l2_occupancy++;
   1366 		l2b->l2b_kva = ptep;
   1367 		l2b->l2b_l1idx = l1idx;
   1368 	}
   1369 
   1370 	return (l2b);
   1371 }
   1372 
   1373 /*
   1374  * One or more mappings in the specified L2 descriptor table have just been
   1375  * invalidated.
   1376  *
   1377  * Garbage collect the metadata and descriptor table itself if necessary.
   1378  *
   1379  * The pmap lock must be acquired when this is called (not necessary
   1380  * for the kernel pmap).
   1381  */
   1382 static void
   1383 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
   1384 {
   1385 	struct l2_dtable *l2;
   1386 	pd_entry_t *pl1pd, l1pd;
   1387 	pt_entry_t *ptep;
   1388 	u_short l1idx;
   1389 
   1390 	KDASSERT(count <= l2b->l2b_occupancy);
   1391 
   1392 	/*
   1393 	 * Update the bucket's reference count according to how many
   1394 	 * PTEs the caller has just invalidated.
   1395 	 */
   1396 	l2b->l2b_occupancy -= count;
   1397 
   1398 	/*
   1399 	 * Note:
   1400 	 *
   1401 	 * Level 2 page tables allocated to the kernel pmap are never freed
   1402 	 * as that would require checking all Level 1 page tables and
   1403 	 * removing any references to the Level 2 page table. See also the
   1404 	 * comment elsewhere about never freeing bootstrap L2 descriptors.
   1405 	 *
   1406 	 * We make do with just invalidating the mapping in the L2 table.
   1407 	 *
   1408 	 * This isn't really a big deal in practice and, in fact, leads
   1409 	 * to a performance win over time as we don't need to continually
   1410 	 * alloc/free.
   1411 	 */
   1412 	if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
   1413 		return;
   1414 
   1415 	/*
   1416 	 * There are no more valid mappings in this level 2 page table.
   1417 	 * Go ahead and NULL-out the pointer in the bucket, then
   1418 	 * free the page table.
   1419 	 */
   1420 	l1idx = l2b->l2b_l1idx;
   1421 	ptep = l2b->l2b_kva;
   1422 	l2b->l2b_kva = NULL;
   1423 
   1424 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   1425 
   1426 	/*
   1427 	 * If the L1 slot matches the pmap's domain
   1428 	 * number, then invalidate it.
   1429 	 */
   1430 	l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
   1431 	if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
   1432 		*pl1pd = 0;
   1433 		PTE_SYNC(pl1pd);
   1434 	}
   1435 
   1436 	/*
   1437 	 * Release the L2 descriptor table back to the pool cache.
   1438 	 */
   1439 #ifndef PMAP_INCLUDE_PTE_SYNC
   1440 	pmap_free_l2_ptp(ptep, l2b->l2b_phys);
   1441 #else
   1442 	pmap_free_l2_ptp(!pmap_is_cached(pm), ptep, l2b->l2b_phys);
   1443 #endif
   1444 
   1445 	/*
   1446 	 * Update the reference count in the associated l2_dtable
   1447 	 */
   1448 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   1449 	if (--l2->l2_occupancy > 0)
   1450 		return;
   1451 
   1452 	/*
   1453 	 * There are no more valid mappings in any of the Level 1
   1454 	 * slots managed by this l2_dtable. Go ahead and NULL-out
   1455 	 * the pointer in the parent pmap and free the l2_dtable.
   1456 	 */
   1457 	pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1458 	pmap_free_l2_dtable(l2);
   1459 }
   1460 
   1461 /*
   1462  * Pool cache constructors for L2 descriptor tables, metadata and pmap
   1463  * structures.
   1464  */
   1465 static int
   1466 pmap_l2ptp_ctor(void *arg, void *v, int flags)
   1467 {
   1468 #ifndef PMAP_INCLUDE_PTE_SYNC
   1469 	struct l2_bucket *l2b;
   1470 	pt_entry_t *ptep, pte;
   1471 	vaddr_t va = (vaddr_t)v & ~PGOFSET;
   1472 
   1473 	/*
   1474 	 * The mappings for these page tables were initially made using
   1475 	 * pmap_kenter_pa() by the pool subsystem. Therefore, the cache-
   1476 	 * mode will not be right for page table mappings. To avoid
   1477 	 * polluting the pmap_kenter_pa() code with a special case for
   1478 	 * page tables, we simply fix up the cache-mode here if it's not
   1479 	 * correct.
   1480 	 */
   1481 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   1482 	KDASSERT(l2b != NULL);
   1483 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   1484 	pte = *ptep;
   1485 
   1486 	if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   1487 		/*
   1488 		 * Page tables must have the cache-mode set to Write-Thru.
   1489 		 */
   1490 		*ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   1491 		PTE_SYNC(ptep);
   1492 		cpu_tlb_flushD_SE(va);
   1493 		cpu_cpwait();
   1494 	}
   1495 #endif
   1496 
   1497 	memset(v, 0, L2_TABLE_SIZE_REAL);
   1498 	PTE_SYNC_RANGE(v, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1499 	return (0);
   1500 }
   1501 
   1502 static int
   1503 pmap_l2dtable_ctor(void *arg, void *v, int flags)
   1504 {
   1505 
   1506 	memset(v, 0, sizeof(struct l2_dtable));
   1507 	return (0);
   1508 }
   1509 
   1510 static int
   1511 pmap_pmap_ctor(void *arg, void *v, int flags)
   1512 {
   1513 
   1514 	memset(v, 0, sizeof(struct pmap));
   1515 	return (0);
   1516 }
   1517 
   1518 static void
   1519 pmap_pinit(pmap_t pm)
   1520 {
   1521 	struct l2_bucket *l2b;
   1522 
   1523 	if (vector_page < KERNEL_BASE) {
   1524 		/*
   1525 		 * Map the vector page.
   1526 		 */
   1527 		pmap_enter(pm, vector_page, systempage.pv_pa,
   1528 		    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1529 		pmap_update(pm);
   1530 
   1531 		pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
   1532 		l2b = pmap_get_l2_bucket(pm, vector_page);
   1533 		KDASSERT(l2b != NULL);
   1534 		pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
   1535 		    L1_C_DOM(pm->pm_domain);
   1536 	} else
   1537 		pm->pm_pl1vec = NULL;
   1538 }
   1539 
   1540 #ifdef PMAP_CACHE_VIVT
   1541 /*
   1542  * Since we have a virtually indexed cache, we may need to inhibit caching if
   1543  * there is more than one mapping and at least one of them is writable.
   1544  * Since we purge the cache on every context switch, we only need to check for
   1545  * other mappings within the same pmap, or kernel_pmap.
   1546  * This function is also called when a page is unmapped, to possibly reenable
   1547  * caching on any remaining mappings.
   1548  *
   1549  * The code implements the following logic, where:
   1550  *
   1551  * KW = # of kernel read/write pages
   1552  * KR = # of kernel read only pages
   1553  * UW = # of user read/write pages
   1554  * UR = # of user read only pages
   1555  *
   1556  * KC = kernel mapping is cacheable
   1557  * UC = user mapping is cacheable
   1558  *
   1559  *               KW=0,KR=0  KW=0,KR>0  KW=1,KR=0  KW>1,KR>=0
   1560  *             +---------------------------------------------
   1561  * UW=0,UR=0   | ---        KC=1       KC=1       KC=0
   1562  * UW=0,UR>0   | UC=1       KC=1,UC=1  KC=0,UC=0  KC=0,UC=0
   1563  * UW=1,UR=0   | UC=1       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1564  * UW>1,UR>=0  | UC=0       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1565  */
   1566 
   1567 static const int pmap_vac_flags[4][4] = {
   1568 	{-1,		0,		0,		PVF_KNC},
   1569 	{0,		0,		PVF_NC,		PVF_NC},
   1570 	{0,		PVF_NC,		PVF_NC,		PVF_NC},
   1571 	{PVF_UNC,	PVF_NC,		PVF_NC,		PVF_NC}
   1572 };
   1573 
   1574 static inline int
   1575 pmap_get_vac_flags(const struct vm_page_md *md)
   1576 {
   1577 	int kidx, uidx;
   1578 
   1579 	kidx = 0;
   1580 	if (md->kro_mappings || md->krw_mappings > 1)
   1581 		kidx |= 1;
   1582 	if (md->krw_mappings)
   1583 		kidx |= 2;
   1584 
   1585 	uidx = 0;
   1586 	if (md->uro_mappings || md->urw_mappings > 1)
   1587 		uidx |= 1;
   1588 	if (md->urw_mappings)
   1589 		uidx |= 2;
   1590 
   1591 	return (pmap_vac_flags[uidx][kidx]);
   1592 }
   1593 
   1594 static inline void
   1595 pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
   1596 {
   1597 	int nattr;
   1598 
   1599 	nattr = pmap_get_vac_flags(md);
   1600 
   1601 	if (nattr < 0) {
   1602 		md->pvh_attrs &= ~PVF_NC;
   1603 		return;
   1604 	}
   1605 
   1606 	if (nattr == 0 && (md->pvh_attrs & PVF_NC) == 0)
   1607 		return;
   1608 
   1609 	if (pm == pmap_kernel())
   1610 		pmap_vac_me_kpmap(md, pa, pm, va);
   1611 	else
   1612 		pmap_vac_me_user(md, pa, pm, va);
   1613 
   1614 	md->pvh_attrs = (md->pvh_attrs & ~PVF_NC) | nattr;
   1615 }
   1616 
   1617 static void
   1618 pmap_vac_me_kpmap(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
   1619 {
   1620 	u_int u_cacheable, u_entries;
   1621 	struct pv_entry *pv;
   1622 	pmap_t last_pmap = pm;
   1623 
   1624 	/*
   1625 	 * Pass one, see if there are both kernel and user pmaps for
   1626 	 * this page.  Calculate whether there are user-writable or
   1627 	 * kernel-writable pages.
   1628 	 */
   1629 	u_cacheable = 0;
   1630 	SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   1631 		if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
   1632 			u_cacheable++;
   1633 	}
   1634 
   1635 	u_entries = md->urw_mappings + md->uro_mappings;
   1636 
   1637 	/*
   1638 	 * We know we have just been updating a kernel entry, so if
   1639 	 * all user pages are already cacheable, then there is nothing
   1640 	 * further to do.
   1641 	 */
   1642 	if (md->k_mappings == 0 && u_cacheable == u_entries)
   1643 		return;
   1644 
   1645 	if (u_entries) {
   1646 		/*
   1647 		 * Scan over the list again, for each entry, if it
   1648 		 * might not be set correctly, call pmap_vac_me_user
   1649 		 * to recalculate the settings.
   1650 		 */
   1651 		SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   1652 			/*
   1653 			 * We know kernel mappings will get set
   1654 			 * correctly in other calls.  We also know
   1655 			 * that if the pmap is the same as last_pmap
   1656 			 * then we've just handled this entry.
   1657 			 */
   1658 			if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
   1659 				continue;
   1660 
   1661 			/*
   1662 			 * If there are kernel entries and this page
   1663 			 * is writable but non-cacheable, then we can
   1664 			 * skip this entry also.
   1665 			 */
   1666 			if (md->k_mappings &&
   1667 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
   1668 			    (PVF_NC | PVF_WRITE))
   1669 				continue;
   1670 
   1671 			/*
   1672 			 * Similarly if there are no kernel-writable
   1673 			 * entries and the page is already
   1674 			 * read-only/cacheable.
   1675 			 */
   1676 			if (md->krw_mappings == 0 &&
   1677 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
   1678 				continue;
   1679 
   1680 			/*
   1681 			 * For some of the remaining cases, we know
   1682 			 * that we must recalculate, but for others we
   1683 			 * can't tell if they are correct or not, so
   1684 			 * we recalculate anyway.
   1685 			 */
   1686 			pmap_vac_me_user(md, pa, (last_pmap = pv->pv_pmap), 0);
   1687 		}
   1688 
   1689 		if (md->k_mappings == 0)
   1690 			return;
   1691 	}
   1692 
   1693 	pmap_vac_me_user(md, pa, pm, va);
   1694 }
   1695 
   1696 static void
   1697 pmap_vac_me_user(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
   1698 {
   1699 	pmap_t kpmap = pmap_kernel();
   1700 	struct pv_entry *pv, *npv = NULL;
   1701 	struct l2_bucket *l2b;
   1702 	pt_entry_t *ptep, pte;
   1703 	u_int entries = 0;
   1704 	u_int writable = 0;
   1705 	u_int cacheable_entries = 0;
   1706 	u_int kern_cacheable = 0;
   1707 	u_int other_writable = 0;
   1708 
   1709 	/*
   1710 	 * Count mappings and writable mappings in this pmap.
   1711 	 * Include kernel mappings as part of our own.
   1712 	 * Keep a pointer to the first one.
   1713 	 */
   1714 	npv = NULL;
   1715 	SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   1716 		/* Count mappings in the same pmap */
   1717 		if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
   1718 			if (entries++ == 0)
   1719 				npv = pv;
   1720 
   1721 			/* Cacheable mappings */
   1722 			if ((pv->pv_flags & PVF_NC) == 0) {
   1723 				cacheable_entries++;
   1724 				if (kpmap == pv->pv_pmap)
   1725 					kern_cacheable++;
   1726 			}
   1727 
   1728 			/* Writable mappings */
   1729 			if (pv->pv_flags & PVF_WRITE)
   1730 				++writable;
   1731 		} else
   1732 		if (pv->pv_flags & PVF_WRITE)
   1733 			other_writable = 1;
   1734 	}
   1735 
   1736 	/*
   1737 	 * Enable or disable caching as necessary.
   1738 	 * Note: the first entry might be part of the kernel pmap,
   1739 	 * so we can't assume this is indicative of the state of the
   1740 	 * other (maybe non-kpmap) entries.
   1741 	 */
   1742 	if ((entries > 1 && writable) ||
   1743 	    (entries > 0 && pm == kpmap && other_writable)) {
   1744 		if (cacheable_entries == 0)
   1745 			return;
   1746 
   1747 		for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
   1748 			if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
   1749 			    (pv->pv_flags & PVF_NC))
   1750 				continue;
   1751 
   1752 			pv->pv_flags |= PVF_NC;
   1753 
   1754 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1755 			KDASSERT(l2b != NULL);
   1756 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1757 			pte = *ptep & ~L2_S_CACHE_MASK;
   1758 
   1759 			if ((va != pv->pv_va || pm != pv->pv_pmap) &&
   1760 			    l2pte_valid(pte)) {
   1761 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1762 #ifdef PMAP_CACHE_VIVT
   1763 					pmap_idcache_wbinv_range(pv->pv_pmap,
   1764 					    pv->pv_va, PAGE_SIZE);
   1765 #endif
   1766 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1767 					    pv->pv_va);
   1768 				} else
   1769 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1770 #ifdef PMAP_CACHE_VIVT
   1771 					pmap_dcache_wb_range(pv->pv_pmap,
   1772 					    pv->pv_va, PAGE_SIZE, true,
   1773 					    (pv->pv_flags & PVF_WRITE) == 0);
   1774 #endif
   1775 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1776 					    pv->pv_va);
   1777 				}
   1778 			}
   1779 
   1780 			*ptep = pte;
   1781 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1782 		}
   1783 		cpu_cpwait();
   1784 	} else
   1785 	if (entries > cacheable_entries) {
   1786 		/*
   1787 		 * Turn cacheing back on for some pages.  If it is a kernel
   1788 		 * page, only do so if there are no other writable pages.
   1789 		 */
   1790 		for (pv = npv; pv; pv = SLIST_NEXT(pv, pv_link)) {
   1791 			if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
   1792 			    (kpmap != pv->pv_pmap || other_writable)))
   1793 				continue;
   1794 
   1795 			pv->pv_flags &= ~PVF_NC;
   1796 
   1797 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1798 			KDASSERT(l2b != NULL);
   1799 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1800 			pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
   1801 
   1802 			if (l2pte_valid(pte)) {
   1803 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1804 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1805 					    pv->pv_va);
   1806 				} else
   1807 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1808 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1809 					    pv->pv_va);
   1810 				}
   1811 			}
   1812 
   1813 			*ptep = pte;
   1814 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1815 		}
   1816 	}
   1817 }
   1818 #endif
   1819 
   1820 #ifdef PMAP_CACHE_VIPT
   1821 static void
   1822 pmap_vac_me_harder(struct vm_page_md *md, paddr_t pa, pmap_t pm, vaddr_t va)
   1823 {
   1824 	struct pv_entry *pv;
   1825 	vaddr_t tst_mask;
   1826 	bool bad_alias;
   1827 	struct l2_bucket *l2b;
   1828 	pt_entry_t *ptep, pte, opte;
   1829 	const u_int
   1830 	    rw_mappings = md->urw_mappings + md->krw_mappings,
   1831 	    ro_mappings = md->uro_mappings + md->kro_mappings;
   1832 
   1833 	/* do we need to do anything? */
   1834 	if (arm_cache_prefer_mask == 0)
   1835 		return;
   1836 
   1837 	NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: md=%p, pmap=%p va=%08lx\n",
   1838 	    md, pm, va));
   1839 
   1840 	KASSERT(!va || pm);
   1841 	KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1842 
   1843 	/* Already a conflict? */
   1844 	if (__predict_false(md->pvh_attrs & PVF_NC)) {
   1845 		/* just an add, things are already non-cached */
   1846 		KASSERT(!(md->pvh_attrs & PVF_DIRTY));
   1847 		KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
   1848 		bad_alias = false;
   1849 		if (va) {
   1850 			PMAPCOUNT(vac_color_none);
   1851 			bad_alias = true;
   1852 			KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   1853 			goto fixup;
   1854 		}
   1855 		pv = SLIST_FIRST(&md->pvh_list);
   1856 		/* the list can't be empty because it would be cachable */
   1857 		if (md->pvh_attrs & PVF_KMPAGE) {
   1858 			tst_mask = md->pvh_attrs;
   1859 		} else {
   1860 			KASSERT(pv);
   1861 			tst_mask = pv->pv_va;
   1862 			pv = SLIST_NEXT(pv, pv_link);
   1863 		}
   1864 		/*
   1865 		 * Only check for a bad alias if we have writable mappings.
   1866 		 */
   1867 		tst_mask &= arm_cache_prefer_mask;
   1868 		if (rw_mappings > 0) {
   1869 			for (; pv && !bad_alias; pv = SLIST_NEXT(pv, pv_link)) {
   1870 				/* if there's a bad alias, stop checking. */
   1871 				if (tst_mask != (pv->pv_va & arm_cache_prefer_mask))
   1872 					bad_alias = true;
   1873 			}
   1874 			md->pvh_attrs |= PVF_WRITE;
   1875 			if (!bad_alias)
   1876 				md->pvh_attrs |= PVF_DIRTY;
   1877 		} else {
   1878 			/*
   1879 			 * We have only read-only mappings.  Let's see if there
   1880 			 * are multiple colors in use or if we mapped a KMPAGE.
   1881 			 * If the latter, we have a bad alias.  If the former,
   1882 			 * we need to remember that.
   1883 			 */
   1884 			for (; pv; pv = SLIST_NEXT(pv, pv_link)) {
   1885 				if (tst_mask != (pv->pv_va & arm_cache_prefer_mask)) {
   1886 					if (md->pvh_attrs & PVF_KMPAGE)
   1887 						bad_alias = true;
   1888 					break;
   1889 				}
   1890 			}
   1891 			md->pvh_attrs &= ~PVF_WRITE;
   1892 			/*
   1893 			 * No KMPAGE and we exited early, so we must have
   1894 			 * multiple color mappings.
   1895 			 */
   1896 			if (!bad_alias && pv != NULL)
   1897 				md->pvh_attrs |= PVF_MULTCLR;
   1898 		}
   1899 
   1900 		/* If no conflicting colors, set everything back to cached */
   1901 		if (!bad_alias) {
   1902 #ifdef DEBUG
   1903 			if ((md->pvh_attrs & PVF_WRITE)
   1904 			    || ro_mappings < 2) {
   1905 				SLIST_FOREACH(pv, &md->pvh_list, pv_link)
   1906 					KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
   1907 			}
   1908 #endif
   1909 			md->pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_NC;
   1910 			md->pvh_attrs |= tst_mask | PVF_COLORED;
   1911 			/*
   1912 			 * Restore DIRTY bit if page is modified
   1913 			 */
   1914 			if (md->pvh_attrs & PVF_DMOD)
   1915 				md->pvh_attrs |= PVF_DIRTY;
   1916 			PMAPCOUNT(vac_color_restore);
   1917 		} else {
   1918 			KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
   1919 			KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
   1920 		}
   1921 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1922 		KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   1923 	} else if (!va) {
   1924 		KASSERT(pmap_is_page_colored_p(md));
   1925 		KASSERT(!(md->pvh_attrs & PVF_WRITE)
   1926 		    || (md->pvh_attrs & PVF_DIRTY));
   1927 		if (rw_mappings == 0) {
   1928 			md->pvh_attrs &= ~PVF_WRITE;
   1929 			if (ro_mappings == 1
   1930 			    && (md->pvh_attrs & PVF_MULTCLR)) {
   1931 				/*
   1932 				 * If this is the last readonly mapping
   1933 				 * but it doesn't match the current color
   1934 				 * for the page, change the current color
   1935 				 * to match this last readonly mapping.
   1936 				 */
   1937 				pv = SLIST_FIRST(&md->pvh_list);
   1938 				tst_mask = (md->pvh_attrs ^ pv->pv_va)
   1939 				    & arm_cache_prefer_mask;
   1940 				if (tst_mask) {
   1941 					md->pvh_attrs ^= tst_mask;
   1942 					PMAPCOUNT(vac_color_change);
   1943 				}
   1944 			}
   1945 		}
   1946 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1947 		KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   1948 		return;
   1949 	} else if (!pmap_is_page_colored_p(md)) {
   1950 		/* not colored so we just use its color */
   1951 		KASSERT(md->pvh_attrs & (PVF_WRITE|PVF_DIRTY));
   1952 		KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
   1953 		PMAPCOUNT(vac_color_new);
   1954 		md->pvh_attrs &= PAGE_SIZE - 1;
   1955 		md->pvh_attrs |= PVF_COLORED
   1956 		    | (va & arm_cache_prefer_mask)
   1957 		    | (rw_mappings > 0 ? PVF_WRITE : 0);
   1958 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   1959 		KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   1960 		return;
   1961 	} else if (((md->pvh_attrs ^ va) & arm_cache_prefer_mask) == 0) {
   1962 		bad_alias = false;
   1963 		if (rw_mappings > 0) {
   1964 			/*
   1965 			 * We now have writeable mappings and if we have
   1966 			 * readonly mappings in more than once color, we have
   1967 			 * an aliasing problem.  Regardless mark the page as
   1968 			 * writeable.
   1969 			 */
   1970 			if (md->pvh_attrs & PVF_MULTCLR) {
   1971 				if (ro_mappings < 2) {
   1972 					/*
   1973 					 * If we only have less than two
   1974 					 * read-only mappings, just flush the
   1975 					 * non-primary colors from the cache.
   1976 					 */
   1977 					pmap_flush_page(md, pa,
   1978 					    PMAP_FLUSH_SECONDARY);
   1979 				} else {
   1980 					bad_alias = true;
   1981 				}
   1982 			}
   1983 			md->pvh_attrs |= PVF_WRITE;
   1984 		}
   1985 		/* If no conflicting colors, set everything back to cached */
   1986 		if (!bad_alias) {
   1987 #ifdef DEBUG
   1988 			if (rw_mappings > 0
   1989 			    || (md->pvh_attrs & PMAP_KMPAGE)) {
   1990 				tst_mask = md->pvh_attrs & arm_cache_prefer_mask;
   1991 				SLIST_FOREACH(pv, &md->pvh_list, pv_link)
   1992 					KDASSERT(((tst_mask ^ pv->pv_va) & arm_cache_prefer_mask) == 0);
   1993 			}
   1994 #endif
   1995 			if (SLIST_EMPTY(&md->pvh_list))
   1996 				PMAPCOUNT(vac_color_reuse);
   1997 			else
   1998 				PMAPCOUNT(vac_color_ok);
   1999 
   2000 			/* matching color, just return */
   2001 			KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   2002 			KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   2003 			return;
   2004 		}
   2005 		KASSERT(SLIST_FIRST(&md->pvh_list) != NULL);
   2006 		KASSERT(SLIST_NEXT(SLIST_FIRST(&md->pvh_list), pv_link) != NULL);
   2007 
   2008 		/* color conflict.  evict from cache. */
   2009 
   2010 		pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
   2011 		md->pvh_attrs &= ~PVF_COLORED;
   2012 		md->pvh_attrs |= PVF_NC;
   2013 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   2014 		KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
   2015 		PMAPCOUNT(vac_color_erase);
   2016 	} else if (rw_mappings == 0
   2017 		   && (md->pvh_attrs & PVF_KMPAGE) == 0) {
   2018 		KASSERT((md->pvh_attrs & PVF_WRITE) == 0);
   2019 
   2020 		/*
   2021 		 * If the page has dirty cache lines, clean it.
   2022 		 */
   2023 		if (md->pvh_attrs & PVF_DIRTY)
   2024 			pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
   2025 
   2026 		/*
   2027 		 * If this is the first remapping (we know that there are no
   2028 		 * writeable mappings), then this is a simple color change.
   2029 		 * Otherwise this is a seconary r/o mapping, which means
   2030 		 * we don't have to do anything.
   2031 		 */
   2032 		if (ro_mappings == 1) {
   2033 			KASSERT(((md->pvh_attrs ^ va) & arm_cache_prefer_mask) != 0);
   2034 			md->pvh_attrs &= PAGE_SIZE - 1;
   2035 			md->pvh_attrs |= (va & arm_cache_prefer_mask);
   2036 			PMAPCOUNT(vac_color_change);
   2037 		} else {
   2038 			PMAPCOUNT(vac_color_blind);
   2039 		}
   2040 		md->pvh_attrs |= PVF_MULTCLR;
   2041 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   2042 		KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   2043 		return;
   2044 	} else {
   2045 		if (rw_mappings > 0)
   2046 			md->pvh_attrs |= PVF_WRITE;
   2047 
   2048 		/* color conflict.  evict from cache. */
   2049 		pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
   2050 
   2051 		/* the list can't be empty because this was a enter/modify */
   2052 		pv = SLIST_FIRST(&md->pvh_list);
   2053 		if ((md->pvh_attrs & PVF_KMPAGE) == 0) {
   2054 			KASSERT(pv);
   2055 			/*
   2056 			 * If there's only one mapped page, change color to the
   2057 			 * page's new color and return.  Restore the DIRTY bit
   2058 			 * that was erased by pmap_flush_page.
   2059 			 */
   2060 			if (SLIST_NEXT(pv, pv_link) == NULL) {
   2061 				md->pvh_attrs &= PAGE_SIZE - 1;
   2062 				md->pvh_attrs |= (va & arm_cache_prefer_mask);
   2063 				if (md->pvh_attrs & PVF_DMOD)
   2064 					md->pvh_attrs |= PVF_DIRTY;
   2065 				PMAPCOUNT(vac_color_change);
   2066 				KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   2067 				KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   2068 				KASSERT(!(md->pvh_attrs & PVF_MULTCLR));
   2069 				return;
   2070 			}
   2071 		}
   2072 		bad_alias = true;
   2073 		md->pvh_attrs &= ~PVF_COLORED;
   2074 		md->pvh_attrs |= PVF_NC;
   2075 		PMAPCOUNT(vac_color_erase);
   2076 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   2077 	}
   2078 
   2079   fixup:
   2080 	KASSERT((rw_mappings == 0) == !(md->pvh_attrs & PVF_WRITE));
   2081 
   2082 	/*
   2083 	 * Turn cacheing on/off for all pages.
   2084 	 */
   2085 	SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   2086 		l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   2087 		KDASSERT(l2b != NULL);
   2088 		ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   2089 		opte = *ptep;
   2090 		pte = opte & ~L2_S_CACHE_MASK;
   2091 		if (bad_alias) {
   2092 			pv->pv_flags |= PVF_NC;
   2093 		} else {
   2094 			pv->pv_flags &= ~PVF_NC;
   2095 			pte |= pte_l2_s_cache_mode;
   2096 		}
   2097 
   2098 		if (opte == pte)	/* only update is there's a change */
   2099 			continue;
   2100 
   2101 		if (l2pte_valid(pte)) {
   2102 			if (PV_BEEN_EXECD(pv->pv_flags)) {
   2103 				pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
   2104 			} else if (PV_BEEN_REFD(pv->pv_flags)) {
   2105 				pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
   2106 			}
   2107 		}
   2108 
   2109 		*ptep = pte;
   2110 		PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   2111 	}
   2112 }
   2113 #endif	/* PMAP_CACHE_VIPT */
   2114 
   2115 
   2116 /*
   2117  * Modify pte bits for all ptes corresponding to the given physical address.
   2118  * We use `maskbits' rather than `clearbits' because we're always passing
   2119  * constants and the latter would require an extra inversion at run-time.
   2120  */
   2121 static void
   2122 pmap_clearbit(struct vm_page_md *md, paddr_t pa, u_int maskbits)
   2123 {
   2124 	struct l2_bucket *l2b;
   2125 	struct pv_entry *pv;
   2126 	pt_entry_t *ptep, npte, opte;
   2127 	pmap_t pm;
   2128 	vaddr_t va;
   2129 	u_int oflags;
   2130 #ifdef PMAP_CACHE_VIPT
   2131 	const bool want_syncicache = PV_IS_EXEC_P(md->pvh_attrs);
   2132 	bool need_syncicache = false;
   2133 	bool did_syncicache = false;
   2134 	bool need_vac_me_harder = false;
   2135 #endif
   2136 
   2137 	NPDEBUG(PDB_BITS,
   2138 	    printf("pmap_clearbit: md %p mask 0x%x\n",
   2139 	    md, maskbits));
   2140 
   2141 #ifdef PMAP_CACHE_VIPT
   2142 	/*
   2143 	 * If we might want to sync the I-cache and we've modified it,
   2144 	 * then we know we definitely need to sync or discard it.
   2145 	 */
   2146 	if (want_syncicache)
   2147 		need_syncicache = md->pvh_attrs & PVF_MOD;
   2148 #endif
   2149 	/*
   2150 	 * Clear saved attributes (modify, reference)
   2151 	 */
   2152 	md->pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
   2153 
   2154 	if (SLIST_EMPTY(&md->pvh_list)) {
   2155 #ifdef PMAP_CACHE_VIPT
   2156 		if (need_syncicache) {
   2157 			/*
   2158 			 * No one has it mapped, so just discard it.  The next
   2159 			 * exec remapping will cause it to be synced.
   2160 			 */
   2161 			md->pvh_attrs &= ~PVF_EXEC;
   2162 			PMAPCOUNT(exec_discarded_clearbit);
   2163 		}
   2164 #endif
   2165 		return;
   2166 	}
   2167 
   2168 	/*
   2169 	 * Loop over all current mappings setting/clearing as appropos
   2170 	 */
   2171 	SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   2172 		va = pv->pv_va;
   2173 		pm = pv->pv_pmap;
   2174 		oflags = pv->pv_flags;
   2175 		/*
   2176 		 * Kernel entries are unmanaged and as such not to be changed.
   2177 		 */
   2178 		if (oflags & PVF_KENTRY)
   2179 			continue;
   2180 		pv->pv_flags &= ~maskbits;
   2181 
   2182 		pmap_acquire_pmap_lock(pm);
   2183 
   2184 		l2b = pmap_get_l2_bucket(pm, va);
   2185 		KDASSERT(l2b != NULL);
   2186 
   2187 		ptep = &l2b->l2b_kva[l2pte_index(va)];
   2188 		npte = opte = *ptep;
   2189 
   2190 		NPDEBUG(PDB_BITS,
   2191 		    printf(
   2192 		    "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
   2193 		    pv, pv->pv_pmap, pv->pv_va, oflags));
   2194 
   2195 		if (maskbits & (PVF_WRITE|PVF_MOD)) {
   2196 #ifdef PMAP_CACHE_VIVT
   2197 			if ((pv->pv_flags & PVF_NC)) {
   2198 				/*
   2199 				 * Entry is not cacheable:
   2200 				 *
   2201 				 * Don't turn caching on again if this is a
   2202 				 * modified emulation. This would be
   2203 				 * inconsitent with the settings created by
   2204 				 * pmap_vac_me_harder(). Otherwise, it's safe
   2205 				 * to re-enable cacheing.
   2206 				 *
   2207 				 * There's no need to call pmap_vac_me_harder()
   2208 				 * here: all pages are losing their write
   2209 				 * permission.
   2210 				 */
   2211 				if (maskbits & PVF_WRITE) {
   2212 					npte |= pte_l2_s_cache_mode;
   2213 					pv->pv_flags &= ~PVF_NC;
   2214 				}
   2215 			} else
   2216 			if (l2pte_writable_p(opte)) {
   2217 				/*
   2218 				 * Entry is writable/cacheable: check if pmap
   2219 				 * is current if it is flush it, otherwise it
   2220 				 * won't be in the cache
   2221 				 */
   2222 				if (PV_BEEN_EXECD(oflags))
   2223 					pmap_idcache_wbinv_range(pm, pv->pv_va,
   2224 					    PAGE_SIZE);
   2225 				else
   2226 				if (PV_BEEN_REFD(oflags))
   2227 					pmap_dcache_wb_range(pm, pv->pv_va,
   2228 					    PAGE_SIZE,
   2229 					    (maskbits & PVF_REF) != 0, false);
   2230 			}
   2231 #endif
   2232 
   2233 			/* make the pte read only */
   2234 			npte = l2pte_set_readonly(npte);
   2235 
   2236 			if (maskbits & oflags & PVF_WRITE) {
   2237 				/*
   2238 				 * Keep alias accounting up to date
   2239 				 */
   2240 				if (pv->pv_pmap == pmap_kernel()) {
   2241 					md->krw_mappings--;
   2242 					md->kro_mappings++;
   2243 				} else {
   2244 					md->urw_mappings--;
   2245 					md->uro_mappings++;
   2246 				}
   2247 #ifdef PMAP_CACHE_VIPT
   2248 				if (arm_cache_prefer_mask != 0) {
   2249 					if (md->urw_mappings + md->krw_mappings == 0) {
   2250 						md->pvh_attrs &= ~PVF_WRITE;
   2251 					} else {
   2252 						PMAP_VALIDATE_MD_PAGE(md);
   2253 					}
   2254 				}
   2255 				if (want_syncicache)
   2256 					need_syncicache = true;
   2257 				need_vac_me_harder = true;
   2258 #endif
   2259 			}
   2260 		}
   2261 
   2262 		if (maskbits & PVF_REF) {
   2263 			if ((pv->pv_flags & PVF_NC) == 0 &&
   2264 			    (maskbits & (PVF_WRITE|PVF_MOD)) == 0 &&
   2265 			    l2pte_valid(npte)) {
   2266 #ifdef PMAP_CACHE_VIVT
   2267 				/*
   2268 				 * Check npte here; we may have already
   2269 				 * done the wbinv above, and the validity
   2270 				 * of the PTE is the same for opte and
   2271 				 * npte.
   2272 				 */
   2273 				/* XXXJRT need idcache_inv_range */
   2274 				if (PV_BEEN_EXECD(oflags))
   2275 					pmap_idcache_wbinv_range(pm,
   2276 					    pv->pv_va, PAGE_SIZE);
   2277 				else
   2278 				if (PV_BEEN_REFD(oflags))
   2279 					pmap_dcache_wb_range(pm,
   2280 					    pv->pv_va, PAGE_SIZE,
   2281 					    true, true);
   2282 #endif
   2283 			}
   2284 
   2285 			/*
   2286 			 * Make the PTE invalid so that we will take a
   2287 			 * page fault the next time the mapping is
   2288 			 * referenced.
   2289 			 */
   2290 			npte &= ~L2_TYPE_MASK;
   2291 			npte |= L2_TYPE_INV;
   2292 		}
   2293 
   2294 		if (npte != opte) {
   2295 			*ptep = npte;
   2296 			PTE_SYNC(ptep);
   2297 			/* Flush the TLB entry if a current pmap. */
   2298 			if (PV_BEEN_EXECD(oflags))
   2299 				pmap_tlb_flushID_SE(pm, pv->pv_va);
   2300 			else
   2301 			if (PV_BEEN_REFD(oflags))
   2302 				pmap_tlb_flushD_SE(pm, pv->pv_va);
   2303 		}
   2304 
   2305 		pmap_release_pmap_lock(pm);
   2306 
   2307 		NPDEBUG(PDB_BITS,
   2308 		    printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
   2309 		    pm, va, opte, npte));
   2310 	}
   2311 
   2312 #ifdef PMAP_CACHE_VIPT
   2313 	/*
   2314 	 * If we need to sync the I-cache and we haven't done it yet, do it.
   2315 	 */
   2316 	if (need_syncicache && !did_syncicache) {
   2317 		pmap_syncicache_page(md, pa);
   2318 		PMAPCOUNT(exec_synced_clearbit);
   2319 	}
   2320 	/*
   2321 	 * If we are changing this to read-only, we need to call vac_me_harder
   2322 	 * so we can change all the read-only pages to cacheable.  We pretend
   2323 	 * this as a page deletion.
   2324 	 */
   2325 	if (need_vac_me_harder) {
   2326 		if (md->pvh_attrs & PVF_NC)
   2327 			pmap_vac_me_harder(md, pa, NULL, 0);
   2328 	}
   2329 #endif
   2330 }
   2331 
   2332 /*
   2333  * pmap_clean_page()
   2334  *
   2335  * This is a local function used to work out the best strategy to clean
   2336  * a single page referenced by its entry in the PV table. It's used by
   2337  * pmap_copy_page, pmap_zero page and maybe some others later on.
   2338  *
   2339  * Its policy is effectively:
   2340  *  o If there are no mappings, we don't bother doing anything with the cache.
   2341  *  o If there is one mapping, we clean just that page.
   2342  *  o If there are multiple mappings, we clean the entire cache.
   2343  *
   2344  * So that some functions can be further optimised, it returns 0 if it didn't
   2345  * clean the entire cache, or 1 if it did.
   2346  *
   2347  * XXX One bug in this routine is that if the pv_entry has a single page
   2348  * mapped at 0x00000000 a whole cache clean will be performed rather than
   2349  * just the 1 page. Since this should not occur in everyday use and if it does
   2350  * it will just result in not the most efficient clean for the page.
   2351  */
   2352 #ifdef PMAP_CACHE_VIVT
   2353 static int
   2354 pmap_clean_page(struct pv_entry *pv, bool is_src)
   2355 {
   2356 	pmap_t pm_to_clean = NULL;
   2357 	struct pv_entry *npv;
   2358 	u_int cache_needs_cleaning = 0;
   2359 	u_int flags = 0;
   2360 	vaddr_t page_to_clean = 0;
   2361 
   2362 	if (pv == NULL) {
   2363 		/* nothing mapped in so nothing to flush */
   2364 		return (0);
   2365 	}
   2366 
   2367 	/*
   2368 	 * Since we flush the cache each time we change to a different
   2369 	 * user vmspace, we only need to flush the page if it is in the
   2370 	 * current pmap.
   2371 	 */
   2372 
   2373 	for (npv = pv; npv; npv = SLIST_NEXT(npv, pv_link)) {
   2374 		if (pmap_is_current(npv->pv_pmap)) {
   2375 			flags |= npv->pv_flags;
   2376 			/*
   2377 			 * The page is mapped non-cacheable in
   2378 			 * this map.  No need to flush the cache.
   2379 			 */
   2380 			if (npv->pv_flags & PVF_NC) {
   2381 #ifdef DIAGNOSTIC
   2382 				if (cache_needs_cleaning)
   2383 					panic("pmap_clean_page: "
   2384 					    "cache inconsistency");
   2385 #endif
   2386 				break;
   2387 			} else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
   2388 				continue;
   2389 			if (cache_needs_cleaning) {
   2390 				page_to_clean = 0;
   2391 				break;
   2392 			} else {
   2393 				page_to_clean = npv->pv_va;
   2394 				pm_to_clean = npv->pv_pmap;
   2395 			}
   2396 			cache_needs_cleaning = 1;
   2397 		}
   2398 	}
   2399 
   2400 	if (page_to_clean) {
   2401 		if (PV_BEEN_EXECD(flags))
   2402 			pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
   2403 			    PAGE_SIZE);
   2404 		else
   2405 			pmap_dcache_wb_range(pm_to_clean, page_to_clean,
   2406 			    PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
   2407 	} else if (cache_needs_cleaning) {
   2408 		pmap_t const pm = curproc->p_vmspace->vm_map.pmap;
   2409 
   2410 		if (PV_BEEN_EXECD(flags))
   2411 			pmap_idcache_wbinv_all(pm);
   2412 		else
   2413 			pmap_dcache_wbinv_all(pm);
   2414 		return (1);
   2415 	}
   2416 	return (0);
   2417 }
   2418 #endif
   2419 
   2420 #ifdef PMAP_CACHE_VIPT
   2421 /*
   2422  * Sync a page with the I-cache.  Since this is a VIPT, we must pick the
   2423  * right cache alias to make sure we flush the right stuff.
   2424  */
   2425 void
   2426 pmap_syncicache_page(struct vm_page_md *md, paddr_t pa)
   2427 {
   2428 	const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
   2429 	pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
   2430 
   2431 	NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: md=%p (attrs=%#x)\n",
   2432 	    md, md->pvh_attrs));
   2433 	/*
   2434 	 * No need to clean the page if it's non-cached.
   2435 	 */
   2436 	if (md->pvh_attrs & PVF_NC)
   2437 		return;
   2438 	KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & PVF_COLORED);
   2439 
   2440 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2441 	/*
   2442 	 * Set up a PTE with the right coloring to flush existing cache lines.
   2443 	 */
   2444 	*ptep = L2_S_PROTO |
   2445 	    pa
   2446 	    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
   2447 	    | pte_l2_s_cache_mode;
   2448 	PTE_SYNC(ptep);
   2449 
   2450 	/*
   2451 	 * Flush it.
   2452 	 */
   2453 	cpu_icache_sync_range(cdstp + va_offset, PAGE_SIZE);
   2454 	/*
   2455 	 * Unmap the page.
   2456 	 */
   2457 	*ptep = 0;
   2458 	PTE_SYNC(ptep);
   2459 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2460 
   2461 	md->pvh_attrs |= PVF_EXEC;
   2462 	PMAPCOUNT(exec_synced);
   2463 }
   2464 
   2465 void
   2466 pmap_flush_page(struct vm_page_md *md, paddr_t pa, enum pmap_flush_op flush)
   2467 {
   2468 	vsize_t va_offset, end_va;
   2469 	void (*cf)(vaddr_t, vsize_t);
   2470 
   2471 	if (arm_cache_prefer_mask == 0)
   2472 		return;
   2473 
   2474 	switch (flush) {
   2475 	case PMAP_FLUSH_PRIMARY:
   2476 		if (md->pvh_attrs & PVF_MULTCLR) {
   2477 			va_offset = 0;
   2478 			end_va = arm_cache_prefer_mask;
   2479 			md->pvh_attrs &= ~PVF_MULTCLR;
   2480 			PMAPCOUNT(vac_flush_lots);
   2481 		} else {
   2482 			va_offset = md->pvh_attrs & arm_cache_prefer_mask;
   2483 			end_va = va_offset;
   2484 			PMAPCOUNT(vac_flush_one);
   2485 		}
   2486 		/*
   2487 		 * Mark that the page is no longer dirty.
   2488 		 */
   2489 		md->pvh_attrs &= ~PVF_DIRTY;
   2490 		cf = cpufuncs.cf_idcache_wbinv_range;
   2491 		break;
   2492 	case PMAP_FLUSH_SECONDARY:
   2493 		va_offset = 0;
   2494 		end_va = arm_cache_prefer_mask;
   2495 		cf = cpufuncs.cf_idcache_wbinv_range;
   2496 		md->pvh_attrs &= ~PVF_MULTCLR;
   2497 		PMAPCOUNT(vac_flush_lots);
   2498 		break;
   2499 	case PMAP_CLEAN_PRIMARY:
   2500 		va_offset = md->pvh_attrs & arm_cache_prefer_mask;
   2501 		end_va = va_offset;
   2502 		cf = cpufuncs.cf_dcache_wb_range;
   2503 		/*
   2504 		 * Mark that the page is no longer dirty.
   2505 		 */
   2506 		if ((md->pvh_attrs & PVF_DMOD) == 0)
   2507 			md->pvh_attrs &= ~PVF_DIRTY;
   2508 		PMAPCOUNT(vac_clean_one);
   2509 		break;
   2510 	default:
   2511 		return;
   2512 	}
   2513 
   2514 	KASSERT(!(md->pvh_attrs & PVF_NC));
   2515 
   2516 	NPDEBUG(PDB_VAC, printf("pmap_flush_page: md=%p (attrs=%#x)\n",
   2517 	    md, md->pvh_attrs));
   2518 
   2519 	for (; va_offset <= end_va; va_offset += PAGE_SIZE) {
   2520 		const size_t pte_offset = va_offset >> PGSHIFT;
   2521 		pt_entry_t * const ptep = &cdst_pte[pte_offset];
   2522 		const pt_entry_t oldpte = *ptep;
   2523 
   2524 		if (flush == PMAP_FLUSH_SECONDARY
   2525 		    && va_offset == (md->pvh_attrs & arm_cache_prefer_mask))
   2526 			continue;
   2527 
   2528 		pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2529 		/*
   2530 		 * Set up a PTE with the right coloring to flush
   2531 		 * existing cache entries.
   2532 		 */
   2533 		*ptep = L2_S_PROTO
   2534 		    | pa
   2535 		    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
   2536 		    | pte_l2_s_cache_mode;
   2537 		PTE_SYNC(ptep);
   2538 
   2539 		/*
   2540 		 * Flush it.
   2541 		 */
   2542 		(*cf)(cdstp + va_offset, PAGE_SIZE);
   2543 
   2544 		/*
   2545 		 * Restore the page table entry since we might have interrupted
   2546 		 * pmap_zero_page or pmap_copy_page which was already using
   2547 		 * this pte.
   2548 		 */
   2549 		*ptep = oldpte;
   2550 		PTE_SYNC(ptep);
   2551 		pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2552 	}
   2553 }
   2554 #endif /* PMAP_CACHE_VIPT */
   2555 
   2556 /*
   2557  * Routine:	pmap_page_remove
   2558  * Function:
   2559  *		Removes this physical page from
   2560  *		all physical maps in which it resides.
   2561  *		Reflects back modify bits to the pager.
   2562  */
   2563 static void
   2564 pmap_page_remove(struct vm_page_md *md, paddr_t pa)
   2565 {
   2566 	struct l2_bucket *l2b;
   2567 	struct pv_entry *pv, *npv, **pvp;
   2568 	pmap_t pm;
   2569 	pt_entry_t *ptep;
   2570 	bool flush;
   2571 	u_int flags;
   2572 
   2573 	NPDEBUG(PDB_FOLLOW,
   2574 	    printf("pmap_page_remove: md %p (0x%08lx)\n", md,
   2575 	    pa));
   2576 
   2577 	pv = SLIST_FIRST(&md->pvh_list);
   2578 	if (pv == NULL) {
   2579 #ifdef PMAP_CACHE_VIPT
   2580 		/*
   2581 		 * We *know* the page contents are about to be replaced.
   2582 		 * Discard the exec contents
   2583 		 */
   2584 		if (PV_IS_EXEC_P(md->pvh_attrs))
   2585 			PMAPCOUNT(exec_discarded_page_protect);
   2586 		md->pvh_attrs &= ~PVF_EXEC;
   2587 		PMAP_VALIDATE_MD_PAGE(md);
   2588 #endif
   2589 		return;
   2590 	}
   2591 #ifdef PMAP_CACHE_VIPT
   2592 	KASSERT(arm_cache_prefer_mask == 0 || pmap_is_page_colored_p(md));
   2593 #endif
   2594 
   2595 	/*
   2596 	 * Clear alias counts
   2597 	 */
   2598 #ifdef PMAP_CACHE_VIVT
   2599 	md->k_mappings = 0;
   2600 #endif
   2601 	md->urw_mappings = md->uro_mappings = 0;
   2602 
   2603 	flush = false;
   2604 	flags = 0;
   2605 
   2606 #ifdef PMAP_CACHE_VIVT
   2607 	pmap_clean_page(pv, false);
   2608 #endif
   2609 
   2610 	pvp = &SLIST_FIRST(&md->pvh_list);
   2611 	while (pv) {
   2612 		pm = pv->pv_pmap;
   2613 		npv = SLIST_NEXT(pv, pv_link);
   2614 		if (flush == false && pmap_is_current(pm))
   2615 			flush = true;
   2616 
   2617 		if (pm == pmap_kernel()) {
   2618 #ifdef PMAP_CACHE_VIPT
   2619 			/*
   2620 			 * If this was unmanaged mapping, it must be preserved.
   2621 			 * Move it back on the list and advance the end-of-list
   2622 			 * pointer.
   2623 			 */
   2624 			if (pv->pv_flags & PVF_KENTRY) {
   2625 				*pvp = pv;
   2626 				pvp = &SLIST_NEXT(pv, pv_link);
   2627 				pv = npv;
   2628 				continue;
   2629 			}
   2630 			if (pv->pv_flags & PVF_WRITE)
   2631 				md->krw_mappings--;
   2632 			else
   2633 				md->kro_mappings--;
   2634 #endif
   2635 			PMAPCOUNT(kernel_unmappings);
   2636 		}
   2637 		PMAPCOUNT(unmappings);
   2638 
   2639 		pmap_acquire_pmap_lock(pm);
   2640 
   2641 		l2b = pmap_get_l2_bucket(pm, pv->pv_va);
   2642 		KDASSERT(l2b != NULL);
   2643 
   2644 		ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   2645 
   2646 		/*
   2647 		 * Update statistics
   2648 		 */
   2649 		--pm->pm_stats.resident_count;
   2650 
   2651 		/* Wired bit */
   2652 		if (pv->pv_flags & PVF_WIRED)
   2653 			--pm->pm_stats.wired_count;
   2654 
   2655 		flags |= pv->pv_flags;
   2656 
   2657 		/*
   2658 		 * Invalidate the PTEs.
   2659 		 */
   2660 		*ptep = 0;
   2661 		PTE_SYNC_CURRENT(pm, ptep);
   2662 		pmap_free_l2_bucket(pm, l2b, 1);
   2663 
   2664 		pool_put(&pmap_pv_pool, pv);
   2665 		pv = npv;
   2666 		/*
   2667 		 * if we reach the end of the list and there are still
   2668 		 * mappings, they might be able to be cached now.
   2669 		 */
   2670 		if (pv == NULL) {
   2671 			*pvp = NULL;
   2672 			if (!SLIST_EMPTY(&md->pvh_list))
   2673 				pmap_vac_me_harder(md, pa, pm, 0);
   2674 		}
   2675 		pmap_release_pmap_lock(pm);
   2676 	}
   2677 #ifdef PMAP_CACHE_VIPT
   2678 	/*
   2679 	 * Its EXEC cache is now gone.
   2680 	 */
   2681 	if (PV_IS_EXEC_P(md->pvh_attrs))
   2682 		PMAPCOUNT(exec_discarded_page_protect);
   2683 	md->pvh_attrs &= ~PVF_EXEC;
   2684 	KASSERT(md->urw_mappings == 0);
   2685 	KASSERT(md->uro_mappings == 0);
   2686 	if (arm_cache_prefer_mask != 0) {
   2687 		if (md->krw_mappings == 0)
   2688 			md->pvh_attrs &= ~PVF_WRITE;
   2689 		PMAP_VALIDATE_MD_PAGE(md);
   2690 	}
   2691 #endif
   2692 
   2693 	if (flush) {
   2694 		/*
   2695 		 * Note: We can't use pmap_tlb_flush{I,D}() here since that
   2696 		 * would need a subsequent call to pmap_update() to ensure
   2697 		 * curpm->pm_cstate.cs_all is reset. Our callers are not
   2698 		 * required to do that (see pmap(9)), so we can't modify
   2699 		 * the current pmap's state.
   2700 		 */
   2701 		if (PV_BEEN_EXECD(flags))
   2702 			cpu_tlb_flushID();
   2703 		else
   2704 			cpu_tlb_flushD();
   2705 	}
   2706 	cpu_cpwait();
   2707 }
   2708 
   2709 /*
   2710  * pmap_t pmap_create(void)
   2711  *
   2712  *      Create a new pmap structure from scratch.
   2713  */
   2714 pmap_t
   2715 pmap_create(void)
   2716 {
   2717 	pmap_t pm;
   2718 
   2719 	pm = pool_cache_get(&pmap_cache, PR_WAITOK);
   2720 
   2721 	mutex_init(&pm->pm_obj_lock, MUTEX_DEFAULT, IPL_NONE);
   2722 	uvm_obj_init(&pm->pm_obj, NULL, false, 1);
   2723 	uvm_obj_setlock(&pm->pm_obj, &pm->pm_obj_lock);
   2724 
   2725 	pm->pm_stats.wired_count = 0;
   2726 	pm->pm_stats.resident_count = 1;
   2727 	pm->pm_cstate.cs_all = 0;
   2728 	pmap_alloc_l1(pm);
   2729 
   2730 	/*
   2731 	 * Note: The pool cache ensures that the pm_l2[] array is already
   2732 	 * initialised to zero.
   2733 	 */
   2734 
   2735 	pmap_pinit(pm);
   2736 
   2737 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   2738 
   2739 	return (pm);
   2740 }
   2741 
   2742 u_int
   2743 arm32_mmap_flags(paddr_t pa)
   2744 {
   2745 	/*
   2746 	 * the upper 8 bits in pmap_enter()'s flags are reserved for MD stuff
   2747 	 * and we're using the upper bits in page numbers to pass flags around
   2748 	 * so we might as well use the same bits
   2749 	 */
   2750 	return (u_int)pa & PMAP_MD_MASK;
   2751 }
   2752 /*
   2753  * int pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2754  *      u_int flags)
   2755  *
   2756  *      Insert the given physical page (p) at
   2757  *      the specified virtual address (v) in the
   2758  *      target physical map with the protection requested.
   2759  *
   2760  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2761  *      or lose information.  That is, this routine must actually
   2762  *      insert this page into the given map NOW.
   2763  */
   2764 int
   2765 pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   2766 {
   2767 	struct l2_bucket *l2b;
   2768 	struct vm_page *pg, *opg;
   2769 	struct pv_entry *pv;
   2770 	pt_entry_t *ptep, npte, opte;
   2771 	u_int nflags;
   2772 	u_int oflags;
   2773 
   2774 	NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags));
   2775 
   2776 	KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
   2777 	KDASSERT(((va | pa) & PGOFSET) == 0);
   2778 
   2779 	/*
   2780 	 * Get a pointer to the page.  Later on in this function, we
   2781 	 * test for a managed page by checking pg != NULL.
   2782 	 */
   2783 	pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
   2784 
   2785 	nflags = 0;
   2786 	if (prot & VM_PROT_WRITE)
   2787 		nflags |= PVF_WRITE;
   2788 	if (prot & VM_PROT_EXECUTE)
   2789 		nflags |= PVF_EXEC;
   2790 	if (flags & PMAP_WIRED)
   2791 		nflags |= PVF_WIRED;
   2792 
   2793 	pmap_acquire_pmap_lock(pm);
   2794 
   2795 	/*
   2796 	 * Fetch the L2 bucket which maps this page, allocating one if
   2797 	 * necessary for user pmaps.
   2798 	 */
   2799 	if (pm == pmap_kernel())
   2800 		l2b = pmap_get_l2_bucket(pm, va);
   2801 	else
   2802 		l2b = pmap_alloc_l2_bucket(pm, va);
   2803 	if (l2b == NULL) {
   2804 		if (flags & PMAP_CANFAIL) {
   2805 			pmap_release_pmap_lock(pm);
   2806 			return (ENOMEM);
   2807 		}
   2808 		panic("pmap_enter: failed to allocate L2 bucket");
   2809 	}
   2810 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   2811 	opte = *ptep;
   2812 	npte = pa;
   2813 	oflags = 0;
   2814 
   2815 	if (opte) {
   2816 		/*
   2817 		 * There is already a mapping at this address.
   2818 		 * If the physical address is different, lookup the
   2819 		 * vm_page.
   2820 		 */
   2821 		if (l2pte_pa(opte) != pa)
   2822 			opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   2823 		else
   2824 			opg = pg;
   2825 	} else
   2826 		opg = NULL;
   2827 
   2828 	if (pg) {
   2829 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   2830 
   2831 		/*
   2832 		 * This is to be a managed mapping.
   2833 		 */
   2834 		if ((flags & VM_PROT_ALL) || (md->pvh_attrs & PVF_REF)) {
   2835 			/*
   2836 			 * - The access type indicates that we don't need
   2837 			 *   to do referenced emulation.
   2838 			 * OR
   2839 			 * - The physical page has already been referenced
   2840 			 *   so no need to re-do referenced emulation here.
   2841 			 */
   2842 			npte |= l2pte_set_readonly(L2_S_PROTO);
   2843 
   2844 			nflags |= PVF_REF;
   2845 
   2846 			if ((prot & VM_PROT_WRITE) != 0 &&
   2847 			    ((flags & VM_PROT_WRITE) != 0 ||
   2848 			     (md->pvh_attrs & PVF_MOD) != 0)) {
   2849 				/*
   2850 				 * This is a writable mapping, and the
   2851 				 * page's mod state indicates it has
   2852 				 * already been modified. Make it
   2853 				 * writable from the outset.
   2854 				 */
   2855 				npte = l2pte_set_writable(npte);
   2856 				nflags |= PVF_MOD;
   2857 			}
   2858 		} else {
   2859 			/*
   2860 			 * Need to do page referenced emulation.
   2861 			 */
   2862 			npte |= L2_TYPE_INV;
   2863 		}
   2864 
   2865 		npte |= pte_l2_s_cache_mode;
   2866 
   2867 		if (pg == opg) {
   2868 			/*
   2869 			 * We're changing the attrs of an existing mapping.
   2870 			 */
   2871 #ifdef MULTIPROCESSOR
   2872 			KASSERT(uvm_page_locked_p(pg));
   2873 #endif
   2874 			oflags = pmap_modify_pv(md, pa, pm, va,
   2875 			    PVF_WRITE | PVF_EXEC | PVF_WIRED |
   2876 			    PVF_MOD | PVF_REF, nflags);
   2877 
   2878 #ifdef PMAP_CACHE_VIVT
   2879 			/*
   2880 			 * We may need to flush the cache if we're
   2881 			 * doing rw-ro...
   2882 			 */
   2883 			if (pm->pm_cstate.cs_cache_d &&
   2884 			    (oflags & PVF_NC) == 0 &&
   2885 			    l2pte_writable_p(opte) &&
   2886 			    (prot & VM_PROT_WRITE) == 0)
   2887 				cpu_dcache_wb_range(va, PAGE_SIZE);
   2888 #endif
   2889 		} else {
   2890 			/*
   2891 			 * New mapping, or changing the backing page
   2892 			 * of an existing mapping.
   2893 			 */
   2894 			if (opg) {
   2895 				struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
   2896 				paddr_t opa = VM_PAGE_TO_PHYS(opg);
   2897 
   2898 				/*
   2899 				 * Replacing an existing mapping with a new one.
   2900 				 * It is part of our managed memory so we
   2901 				 * must remove it from the PV list
   2902 				 */
   2903 #ifdef MULTIPROCESSOR
   2904 				KASSERT(uvm_page_locked_p(opg));
   2905 #endif
   2906 				pv = pmap_remove_pv(omd, opa, pm, va);
   2907 				pmap_vac_me_harder(omd, opa, pm, 0);
   2908 				oflags = pv->pv_flags;
   2909 
   2910 #ifdef PMAP_CACHE_VIVT
   2911 				/*
   2912 				 * If the old mapping was valid (ref/mod
   2913 				 * emulation creates 'invalid' mappings
   2914 				 * initially) then make sure to frob
   2915 				 * the cache.
   2916 				 */
   2917 				if ((oflags & PVF_NC) == 0 &&
   2918 				    l2pte_valid(opte)) {
   2919 					if (PV_BEEN_EXECD(oflags)) {
   2920 						pmap_idcache_wbinv_range(pm, va,
   2921 						    PAGE_SIZE);
   2922 					} else
   2923 					if (PV_BEEN_REFD(oflags)) {
   2924 						pmap_dcache_wb_range(pm, va,
   2925 						    PAGE_SIZE, true,
   2926 						    (oflags & PVF_WRITE) == 0);
   2927 					}
   2928 				}
   2929 #endif
   2930 			} else
   2931 			if ((pv = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){
   2932 				if ((flags & PMAP_CANFAIL) == 0)
   2933 					panic("pmap_enter: no pv entries");
   2934 
   2935 				if (pm != pmap_kernel())
   2936 					pmap_free_l2_bucket(pm, l2b, 0);
   2937 				pmap_release_pmap_lock(pm);
   2938 				NPDEBUG(PDB_ENTER,
   2939 				    printf("pmap_enter: ENOMEM\n"));
   2940 				return (ENOMEM);
   2941 			}
   2942 
   2943 #ifdef MULTIPROCESSOR
   2944 			KASSERT(uvm_page_locked_p(pg));
   2945 #endif
   2946 			pmap_enter_pv(md, pa, pv, pm, va, nflags);
   2947 		}
   2948 	} else {
   2949 		/*
   2950 		 * We're mapping an unmanaged page.
   2951 		 * These are always readable, and possibly writable, from
   2952 		 * the get go as we don't need to track ref/mod status.
   2953 		 */
   2954 		npte |= l2pte_set_readonly(L2_S_PROTO);
   2955 		if (prot & VM_PROT_WRITE)
   2956 			npte = l2pte_set_writable(npte);
   2957 
   2958 		/*
   2959 		 * Make sure the vector table is mapped cacheable
   2960 		 */
   2961 		if ((pm != pmap_kernel() && va == vector_page) ||
   2962 		    (flags & ARM32_MMAP_CACHEABLE)) {
   2963 			npte |= pte_l2_s_cache_mode;
   2964 		} else if (flags & ARM32_MMAP_WRITECOMBINE) {
   2965 			npte |= pte_l2_s_wc_mode;
   2966 		}
   2967 		if (opg) {
   2968 			/*
   2969 			 * Looks like there's an existing 'managed' mapping
   2970 			 * at this address.
   2971 			 */
   2972 			struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
   2973 			paddr_t opa = VM_PAGE_TO_PHYS(opg);
   2974 
   2975 #ifdef MULTIPROCESSOR
   2976 			KASSERT(uvm_page_locked_p(opg));
   2977 #endif
   2978 			pv = pmap_remove_pv(omd, opa, pm, va);
   2979 			pmap_vac_me_harder(omd, opa, pm, 0);
   2980 			oflags = pv->pv_flags;
   2981 
   2982 #ifdef PMAP_CACHE_VIVT
   2983 			if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
   2984 				if (PV_BEEN_EXECD(oflags))
   2985 					pmap_idcache_wbinv_range(pm, va,
   2986 					    PAGE_SIZE);
   2987 				else
   2988 				if (PV_BEEN_REFD(oflags))
   2989 					pmap_dcache_wb_range(pm, va, PAGE_SIZE,
   2990 					    true, (oflags & PVF_WRITE) == 0);
   2991 			}
   2992 #endif
   2993 			pool_put(&pmap_pv_pool, pv);
   2994 		}
   2995 	}
   2996 
   2997 	/*
   2998 	 * Make sure userland mappings get the right permissions
   2999 	 */
   3000 	if (pm != pmap_kernel() && va != vector_page)
   3001 		npte |= L2_S_PROT_U;
   3002 
   3003 	/*
   3004 	 * Keep the stats up to date
   3005 	 */
   3006 	if (opte == 0) {
   3007 		l2b->l2b_occupancy++;
   3008 		pm->pm_stats.resident_count++;
   3009 	}
   3010 
   3011 	NPDEBUG(PDB_ENTER,
   3012 	    printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte));
   3013 
   3014 	/*
   3015 	 * If this is just a wiring change, the two PTEs will be
   3016 	 * identical, so there's no need to update the page table.
   3017 	 */
   3018 	if (npte != opte) {
   3019 		bool is_cached = pmap_is_cached(pm);
   3020 
   3021 		*ptep = npte;
   3022 		PTE_SYNC(ptep);
   3023 		if (is_cached) {
   3024 			/*
   3025 			 * We only need to frob the cache/tlb if this pmap
   3026 			 * is current
   3027 			 */
   3028 			if (va != vector_page && l2pte_valid(npte)) {
   3029 				/*
   3030 				 * This mapping is likely to be accessed as
   3031 				 * soon as we return to userland. Fix up the
   3032 				 * L1 entry to avoid taking another
   3033 				 * page/domain fault.
   3034 				 */
   3035 				pd_entry_t *pl1pd, l1pd;
   3036 
   3037 				pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)];
   3038 				l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) |
   3039 				    L1_C_PROTO;
   3040 				if (*pl1pd != l1pd) {
   3041 					*pl1pd = l1pd;
   3042 					PTE_SYNC(pl1pd);
   3043 				}
   3044 			}
   3045 		}
   3046 
   3047 		if (PV_BEEN_EXECD(oflags))
   3048 			pmap_tlb_flushID_SE(pm, va);
   3049 		else
   3050 		if (PV_BEEN_REFD(oflags))
   3051 			pmap_tlb_flushD_SE(pm, va);
   3052 
   3053 		NPDEBUG(PDB_ENTER,
   3054 		    printf("pmap_enter: is_cached %d cs 0x%08x\n",
   3055 		    is_cached, pm->pm_cstate.cs_all));
   3056 
   3057 		if (pg != NULL) {
   3058 			struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3059 
   3060 #ifdef MULTIPROCESSOR
   3061 			KASSERT(uvm_page_locked_p(pg));
   3062 #endif
   3063 			pmap_vac_me_harder(md, pa, pm, va);
   3064 		}
   3065 	}
   3066 #if defined(PMAP_CACHE_VIPT) && defined(DIAGNOSTIC)
   3067 	if (pg) {
   3068 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3069 
   3070 #ifdef MULTIPROCESSOR
   3071 		KASSERT(uvm_page_locked_p(pg));
   3072 #endif
   3073 		KASSERT((md->pvh_attrs & PVF_DMOD) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   3074 		PMAP_VALIDATE_MD_PAGE(md);
   3075 	}
   3076 #endif
   3077 
   3078 	pmap_release_pmap_lock(pm);
   3079 
   3080 	return (0);
   3081 }
   3082 
   3083 /*
   3084  * pmap_remove()
   3085  *
   3086  * pmap_remove is responsible for nuking a number of mappings for a range
   3087  * of virtual address space in the current pmap. To do this efficiently
   3088  * is interesting, because in a number of cases a wide virtual address
   3089  * range may be supplied that contains few actual mappings. So, the
   3090  * optimisations are:
   3091  *  1. Skip over hunks of address space for which no L1 or L2 entry exists.
   3092  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   3093  *     maybe do just a partial cache clean. This path of execution is
   3094  *     complicated by the fact that the cache must be flushed _before_
   3095  *     the PTE is nuked, being a VAC :-)
   3096  *  3. If we're called after UVM calls pmap_remove_all(), we can defer
   3097  *     all invalidations until pmap_update(), since pmap_remove_all() has
   3098  *     already flushed the cache.
   3099  *  4. Maybe later fast-case a single page, but I don't think this is
   3100  *     going to make _that_ much difference overall.
   3101  */
   3102 
   3103 #define	PMAP_REMOVE_CLEAN_LIST_SIZE	3
   3104 
   3105 void
   3106 pmap_remove(pmap_t pm, vaddr_t sva, vaddr_t eva)
   3107 {
   3108 	struct l2_bucket *l2b;
   3109 	vaddr_t next_bucket;
   3110 	pt_entry_t *ptep;
   3111 	u_int cleanlist_idx, total, cnt;
   3112 	struct {
   3113 		vaddr_t va;
   3114 		pt_entry_t *ptep;
   3115 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   3116 	u_int mappings, is_exec, is_refd;
   3117 
   3118 	NPDEBUG(PDB_REMOVE, printf("pmap_do_remove: pmap=%p sva=%08lx "
   3119 	    "eva=%08lx\n", pm, sva, eva));
   3120 
   3121 	/*
   3122 	 * we lock in the pmap => pv_head direction
   3123 	 */
   3124 	pmap_acquire_pmap_lock(pm);
   3125 
   3126 	if (pm->pm_remove_all || !pmap_is_cached(pm)) {
   3127 		cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   3128 		if (pm->pm_cstate.cs_tlb == 0)
   3129 			pm->pm_remove_all = true;
   3130 	} else
   3131 		cleanlist_idx = 0;
   3132 
   3133 	total = 0;
   3134 
   3135 	while (sva < eva) {
   3136 		/*
   3137 		 * Do one L2 bucket's worth at a time.
   3138 		 */
   3139 		next_bucket = L2_NEXT_BUCKET(sva);
   3140 		if (next_bucket > eva)
   3141 			next_bucket = eva;
   3142 
   3143 		l2b = pmap_get_l2_bucket(pm, sva);
   3144 		if (l2b == NULL) {
   3145 			sva = next_bucket;
   3146 			continue;
   3147 		}
   3148 
   3149 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   3150 
   3151 		for (mappings = 0; sva < next_bucket; sva += PAGE_SIZE, ptep++){
   3152 			struct vm_page *pg;
   3153 			pt_entry_t pte;
   3154 			paddr_t pa;
   3155 
   3156 			pte = *ptep;
   3157 
   3158 			if (pte == 0) {
   3159 				/* Nothing here, move along */
   3160 				continue;
   3161 			}
   3162 
   3163 			pa = l2pte_pa(pte);
   3164 			is_exec = 0;
   3165 			is_refd = 1;
   3166 
   3167 			/*
   3168 			 * Update flags. In a number of circumstances,
   3169 			 * we could cluster a lot of these and do a
   3170 			 * number of sequential pages in one go.
   3171 			 */
   3172 			if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   3173 				struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3174 				struct pv_entry *pv;
   3175 
   3176 #ifdef MULTIPROCESSOR
   3177 				KASSERT(uvm_page_locked_p(pg));
   3178 #endif
   3179 				pv = pmap_remove_pv(md, pa, pm, sva);
   3180 				pmap_vac_me_harder(md, pa, pm, 0);
   3181 				if (pv != NULL) {
   3182 					if (pm->pm_remove_all == false) {
   3183 						is_exec =
   3184 						   PV_BEEN_EXECD(pv->pv_flags);
   3185 						is_refd =
   3186 						   PV_BEEN_REFD(pv->pv_flags);
   3187 					}
   3188 					pool_put(&pmap_pv_pool, pv);
   3189 				}
   3190 			}
   3191 			mappings++;
   3192 
   3193 			if (!l2pte_valid(pte)) {
   3194 				/*
   3195 				 * Ref/Mod emulation is still active for this
   3196 				 * mapping, therefore it is has not yet been
   3197 				 * accessed. No need to frob the cache/tlb.
   3198 				 */
   3199 				*ptep = 0;
   3200 				PTE_SYNC_CURRENT(pm, ptep);
   3201 				continue;
   3202 			}
   3203 
   3204 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   3205 				/* Add to the clean list. */
   3206 				cleanlist[cleanlist_idx].ptep = ptep;
   3207 				cleanlist[cleanlist_idx].va =
   3208 				    sva | (is_exec & 1);
   3209 				cleanlist_idx++;
   3210 			} else
   3211 			if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   3212 				/* Nuke everything if needed. */
   3213 #ifdef PMAP_CACHE_VIVT
   3214 				pmap_idcache_wbinv_all(pm);
   3215 #endif
   3216 				pmap_tlb_flushID(pm);
   3217 
   3218 				/*
   3219 				 * Roll back the previous PTE list,
   3220 				 * and zero out the current PTE.
   3221 				 */
   3222 				for (cnt = 0;
   3223 				     cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   3224 					*cleanlist[cnt].ptep = 0;
   3225 					PTE_SYNC(cleanlist[cnt].ptep);
   3226 				}
   3227 				*ptep = 0;
   3228 				PTE_SYNC(ptep);
   3229 				cleanlist_idx++;
   3230 				pm->pm_remove_all = true;
   3231 			} else {
   3232 				*ptep = 0;
   3233 				PTE_SYNC(ptep);
   3234 				if (pm->pm_remove_all == false) {
   3235 					if (is_exec)
   3236 						pmap_tlb_flushID_SE(pm, sva);
   3237 					else
   3238 					if (is_refd)
   3239 						pmap_tlb_flushD_SE(pm, sva);
   3240 				}
   3241 			}
   3242 		}
   3243 
   3244 		/*
   3245 		 * Deal with any left overs
   3246 		 */
   3247 		if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   3248 			total += cleanlist_idx;
   3249 			for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   3250 				if (pm->pm_cstate.cs_all != 0) {
   3251 					vaddr_t clva = cleanlist[cnt].va & ~1;
   3252 					if (cleanlist[cnt].va & 1) {
   3253 #ifdef PMAP_CACHE_VIVT
   3254 						pmap_idcache_wbinv_range(pm,
   3255 						    clva, PAGE_SIZE);
   3256 #endif
   3257 						pmap_tlb_flushID_SE(pm, clva);
   3258 					} else {
   3259 #ifdef PMAP_CACHE_VIVT
   3260 						pmap_dcache_wb_range(pm,
   3261 						    clva, PAGE_SIZE, true,
   3262 						    false);
   3263 #endif
   3264 						pmap_tlb_flushD_SE(pm, clva);
   3265 					}
   3266 				}
   3267 				*cleanlist[cnt].ptep = 0;
   3268 				PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
   3269 			}
   3270 
   3271 			/*
   3272 			 * If it looks like we're removing a whole bunch
   3273 			 * of mappings, it's faster to just write-back
   3274 			 * the whole cache now and defer TLB flushes until
   3275 			 * pmap_update() is called.
   3276 			 */
   3277 			if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
   3278 				cleanlist_idx = 0;
   3279 			else {
   3280 				cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   3281 #ifdef PMAP_CACHE_VIVT
   3282 				pmap_idcache_wbinv_all(pm);
   3283 #endif
   3284 				pm->pm_remove_all = true;
   3285 			}
   3286 		}
   3287 
   3288 		pmap_free_l2_bucket(pm, l2b, mappings);
   3289 		pm->pm_stats.resident_count -= mappings;
   3290 	}
   3291 
   3292 	pmap_release_pmap_lock(pm);
   3293 }
   3294 
   3295 #ifdef PMAP_CACHE_VIPT
   3296 static struct pv_entry *
   3297 pmap_kremove_pg(struct vm_page *pg, vaddr_t va)
   3298 {
   3299 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3300 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3301 	struct pv_entry *pv;
   3302 
   3303 	KASSERT(arm_cache_prefer_mask == 0 || md->pvh_attrs & (PVF_COLORED|PVF_NC));
   3304 	KASSERT((md->pvh_attrs & PVF_KMPAGE) == 0);
   3305 
   3306 	pv = pmap_remove_pv(md, pa, pmap_kernel(), va);
   3307 	KASSERT(pv);
   3308 	KASSERT(pv->pv_flags & PVF_KENTRY);
   3309 
   3310 	/*
   3311 	 * If we are removing a writeable mapping to a cached exec page,
   3312 	 * if it's the last mapping then clear it execness other sync
   3313 	 * the page to the icache.
   3314 	 */
   3315 	if ((md->pvh_attrs & (PVF_NC|PVF_EXEC)) == PVF_EXEC
   3316 	    && (pv->pv_flags & PVF_WRITE) != 0) {
   3317 		if (SLIST_EMPTY(&md->pvh_list)) {
   3318 			md->pvh_attrs &= ~PVF_EXEC;
   3319 			PMAPCOUNT(exec_discarded_kremove);
   3320 		} else {
   3321 			pmap_syncicache_page(md, pa);
   3322 			PMAPCOUNT(exec_synced_kremove);
   3323 		}
   3324 	}
   3325 	pmap_vac_me_harder(md, pa, pmap_kernel(), 0);
   3326 
   3327 	return pv;
   3328 }
   3329 #endif /* PMAP_CACHE_VIPT */
   3330 
   3331 /*
   3332  * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
   3333  *
   3334  * We assume there is already sufficient KVM space available
   3335  * to do this, as we can't allocate L2 descriptor tables/metadata
   3336  * from here.
   3337  */
   3338 void
   3339 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   3340 {
   3341 	struct l2_bucket *l2b;
   3342 	pt_entry_t *ptep, opte;
   3343 #ifdef PMAP_CACHE_VIVT
   3344 	struct vm_page *pg = (flags & PMAP_KMPAGE) ? PHYS_TO_VM_PAGE(pa) : NULL;
   3345 #endif
   3346 #ifdef PMAP_CACHE_VIPT
   3347 	struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
   3348 	struct vm_page *opg;
   3349 	struct pv_entry *pv = NULL;
   3350 #endif
   3351 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3352 
   3353 	NPDEBUG(PDB_KENTER,
   3354 	    printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n",
   3355 	    va, pa, prot));
   3356 
   3357 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   3358 	KDASSERT(l2b != NULL);
   3359 
   3360 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3361 	opte = *ptep;
   3362 
   3363 	if (opte == 0) {
   3364 		PMAPCOUNT(kenter_mappings);
   3365 		l2b->l2b_occupancy++;
   3366 	} else {
   3367 		PMAPCOUNT(kenter_remappings);
   3368 #ifdef PMAP_CACHE_VIPT
   3369 		opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   3370 #ifdef DIAGNOSTIC
   3371 		struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
   3372 #endif
   3373 		if (opg) {
   3374 			KASSERT(opg != pg);
   3375 			KASSERT((omd->pvh_attrs & PVF_KMPAGE) == 0);
   3376 			KASSERT((flags & PMAP_KMPAGE) == 0);
   3377 			pv = pmap_kremove_pg(opg, va);
   3378 		}
   3379 #endif
   3380 		if (l2pte_valid(opte)) {
   3381 #ifdef PMAP_CACHE_VIVT
   3382 			cpu_dcache_wbinv_range(va, PAGE_SIZE);
   3383 #endif
   3384 			cpu_tlb_flushD_SE(va);
   3385 			cpu_cpwait();
   3386 		}
   3387 	}
   3388 
   3389 	*ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot)
   3390 	    | ((flags & PMAP_NOCACHE) ? 0 : pte_l2_s_cache_mode);
   3391 	PTE_SYNC(ptep);
   3392 
   3393 	if (pg) {
   3394 #ifdef MULTIPROCESSOR
   3395 		KASSERT(uvm_page_locked_p(pg));
   3396 #endif
   3397 		if (flags & PMAP_KMPAGE) {
   3398 			KASSERT(md->urw_mappings == 0);
   3399 			KASSERT(md->uro_mappings == 0);
   3400 			KASSERT(md->krw_mappings == 0);
   3401 			KASSERT(md->kro_mappings == 0);
   3402 #ifdef PMAP_CACHE_VIPT
   3403 			KASSERT(pv == NULL);
   3404 			KASSERT(arm_cache_prefer_mask == 0 || (va & PVF_COLORED) == 0);
   3405 			KASSERT((md->pvh_attrs & PVF_NC) == 0);
   3406 			/* if there is a color conflict, evict from cache. */
   3407 			if (pmap_is_page_colored_p(md)
   3408 			    && ((va ^ md->pvh_attrs) & arm_cache_prefer_mask)) {
   3409 				PMAPCOUNT(vac_color_change);
   3410 				pmap_flush_page(md, pa, PMAP_FLUSH_PRIMARY);
   3411 			} else if (md->pvh_attrs & PVF_MULTCLR) {
   3412 				/*
   3413 				 * If this page has multiple colors, expunge
   3414 				 * them.
   3415 				 */
   3416 				PMAPCOUNT(vac_flush_lots2);
   3417 				pmap_flush_page(md, pa, PMAP_FLUSH_SECONDARY);
   3418 			}
   3419 			md->pvh_attrs &= PAGE_SIZE - 1;
   3420 			md->pvh_attrs |= PVF_KMPAGE
   3421 			    | PVF_COLORED | PVF_DIRTY
   3422 			    | (va & arm_cache_prefer_mask);
   3423 #endif
   3424 #ifdef PMAP_CACHE_VIVT
   3425 			md->pvh_attrs |= PVF_KMPAGE;
   3426 #endif
   3427 			pmap_kmpages++;
   3428 #ifdef PMAP_CACHE_VIPT
   3429 		} else {
   3430 			if (pv == NULL) {
   3431 				pv = pool_get(&pmap_pv_pool, PR_NOWAIT);
   3432 				KASSERT(pv != NULL);
   3433 			}
   3434 			pmap_enter_pv(md, pa, pv, pmap_kernel(), va,
   3435 			    PVF_WIRED | PVF_KENTRY
   3436 			    | (prot & VM_PROT_WRITE ? PVF_WRITE : 0));
   3437 			if ((prot & VM_PROT_WRITE)
   3438 			    && !(md->pvh_attrs & PVF_NC))
   3439 				md->pvh_attrs |= PVF_DIRTY;
   3440 			KASSERT((prot & VM_PROT_WRITE) == 0 || (md->pvh_attrs & (PVF_DIRTY|PVF_NC)));
   3441 			pmap_vac_me_harder(md, pa, pmap_kernel(), va);
   3442 #endif
   3443 		}
   3444 #ifdef PMAP_CACHE_VIPT
   3445 	} else {
   3446 		if (pv != NULL)
   3447 			pool_put(&pmap_pv_pool, pv);
   3448 #endif
   3449 	}
   3450 }
   3451 
   3452 void
   3453 pmap_kremove(vaddr_t va, vsize_t len)
   3454 {
   3455 	struct l2_bucket *l2b;
   3456 	pt_entry_t *ptep, *sptep, opte;
   3457 	vaddr_t next_bucket, eva;
   3458 	u_int mappings;
   3459 	struct vm_page *opg;
   3460 
   3461 	PMAPCOUNT(kenter_unmappings);
   3462 
   3463 	NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n",
   3464 	    va, len));
   3465 
   3466 	eva = va + len;
   3467 
   3468 	while (va < eva) {
   3469 		next_bucket = L2_NEXT_BUCKET(va);
   3470 		if (next_bucket > eva)
   3471 			next_bucket = eva;
   3472 
   3473 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   3474 		KDASSERT(l2b != NULL);
   3475 
   3476 		sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
   3477 		mappings = 0;
   3478 
   3479 		while (va < next_bucket) {
   3480 			opte = *ptep;
   3481 			opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   3482 			if (opg) {
   3483 				struct vm_page_md *omd = VM_PAGE_TO_MD(opg);
   3484 
   3485 				if (omd->pvh_attrs & PVF_KMPAGE) {
   3486 					KASSERT(omd->urw_mappings == 0);
   3487 					KASSERT(omd->uro_mappings == 0);
   3488 					KASSERT(omd->krw_mappings == 0);
   3489 					KASSERT(omd->kro_mappings == 0);
   3490 					omd->pvh_attrs &= ~PVF_KMPAGE;
   3491 #ifdef PMAP_CACHE_VIPT
   3492 					if (arm_cache_prefer_mask != 0) {
   3493 						omd->pvh_attrs &= ~PVF_WRITE;
   3494 					}
   3495 #endif
   3496 					pmap_kmpages--;
   3497 #ifdef PMAP_CACHE_VIPT
   3498 				} else {
   3499 					pool_put(&pmap_pv_pool,
   3500 					    pmap_kremove_pg(opg, va));
   3501 #endif
   3502 				}
   3503 			}
   3504 			if (l2pte_valid(opte)) {
   3505 #ifdef PMAP_CACHE_VIVT
   3506 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   3507 #endif
   3508 				cpu_tlb_flushD_SE(va);
   3509 			}
   3510 			if (opte) {
   3511 				*ptep = 0;
   3512 				mappings++;
   3513 			}
   3514 			va += PAGE_SIZE;
   3515 			ptep++;
   3516 		}
   3517 		KDASSERT(mappings <= l2b->l2b_occupancy);
   3518 		l2b->l2b_occupancy -= mappings;
   3519 		PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
   3520 	}
   3521 	cpu_cpwait();
   3522 }
   3523 
   3524 bool
   3525 pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
   3526 {
   3527 	struct l2_dtable *l2;
   3528 	pd_entry_t *pl1pd, l1pd;
   3529 	pt_entry_t *ptep, pte;
   3530 	paddr_t pa;
   3531 	u_int l1idx;
   3532 
   3533 	pmap_acquire_pmap_lock(pm);
   3534 
   3535 	l1idx = L1_IDX(va);
   3536 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   3537 	l1pd = *pl1pd;
   3538 
   3539 	if (l1pte_section_p(l1pd)) {
   3540 		/*
   3541 		 * These should only happen for pmap_kernel()
   3542 		 */
   3543 		KDASSERT(pm == pmap_kernel());
   3544 		pmap_release_pmap_lock(pm);
   3545 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   3546 		if (l1pte_supersection_p(l1pd)) {
   3547 			pa = (l1pd & L1_SS_FRAME) | (va & L1_SS_OFFSET);
   3548 		} else
   3549 #endif
   3550 			pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
   3551 	} else {
   3552 		/*
   3553 		 * Note that we can't rely on the validity of the L1
   3554 		 * descriptor as an indication that a mapping exists.
   3555 		 * We have to look it up in the L2 dtable.
   3556 		 */
   3557 		l2 = pm->pm_l2[L2_IDX(l1idx)];
   3558 
   3559 		if (l2 == NULL ||
   3560 		    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   3561 			pmap_release_pmap_lock(pm);
   3562 			return false;
   3563 		}
   3564 
   3565 		ptep = &ptep[l2pte_index(va)];
   3566 		pte = *ptep;
   3567 		pmap_release_pmap_lock(pm);
   3568 
   3569 		if (pte == 0)
   3570 			return false;
   3571 
   3572 		switch (pte & L2_TYPE_MASK) {
   3573 		case L2_TYPE_L:
   3574 			pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
   3575 			break;
   3576 
   3577 		default:
   3578 			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
   3579 			break;
   3580 		}
   3581 	}
   3582 
   3583 	if (pap != NULL)
   3584 		*pap = pa;
   3585 
   3586 	return true;
   3587 }
   3588 
   3589 void
   3590 pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   3591 {
   3592 	struct l2_bucket *l2b;
   3593 	pt_entry_t *ptep, pte;
   3594 	vaddr_t next_bucket;
   3595 	u_int flags;
   3596 	u_int clr_mask;
   3597 	int flush;
   3598 
   3599 	NPDEBUG(PDB_PROTECT,
   3600 	    printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
   3601 	    pm, sva, eva, prot));
   3602 
   3603 	if ((prot & VM_PROT_READ) == 0) {
   3604 		pmap_remove(pm, sva, eva);
   3605 		return;
   3606 	}
   3607 
   3608 	if (prot & VM_PROT_WRITE) {
   3609 		/*
   3610 		 * If this is a read->write transition, just ignore it and let
   3611 		 * uvm_fault() take care of it later.
   3612 		 */
   3613 		return;
   3614 	}
   3615 
   3616 	pmap_acquire_pmap_lock(pm);
   3617 
   3618 	flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
   3619 	flags = 0;
   3620 	clr_mask = PVF_WRITE | ((prot & VM_PROT_EXECUTE) ? 0 : PVF_EXEC);
   3621 
   3622 	while (sva < eva) {
   3623 		next_bucket = L2_NEXT_BUCKET(sva);
   3624 		if (next_bucket > eva)
   3625 			next_bucket = eva;
   3626 
   3627 		l2b = pmap_get_l2_bucket(pm, sva);
   3628 		if (l2b == NULL) {
   3629 			sva = next_bucket;
   3630 			continue;
   3631 		}
   3632 
   3633 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   3634 
   3635 		while (sva < next_bucket) {
   3636 			pte = *ptep;
   3637 			if (l2pte_valid(pte) != 0 && l2pte_writable_p(pte)) {
   3638 				struct vm_page *pg;
   3639 				u_int f;
   3640 
   3641 #ifdef PMAP_CACHE_VIVT
   3642 				/*
   3643 				 * OK, at this point, we know we're doing
   3644 				 * write-protect operation.  If the pmap is
   3645 				 * active, write-back the page.
   3646 				 */
   3647 				pmap_dcache_wb_range(pm, sva, PAGE_SIZE,
   3648 				    false, false);
   3649 #endif
   3650 
   3651 				pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
   3652 				pte = l2pte_set_readonly(pte);
   3653 				*ptep = pte;
   3654 				PTE_SYNC(ptep);
   3655 
   3656 				if (pg != NULL) {
   3657 					struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3658 					paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3659 
   3660 #ifdef MULTIPROCESSOR
   3661 					KASSERT(uvm_page_locked_p(pg));
   3662 #endif
   3663 					f = pmap_modify_pv(md, pa, pm, sva,
   3664 					    clr_mask, 0);
   3665 					pmap_vac_me_harder(md, pa, pm, sva);
   3666 				} else {
   3667 					f = PVF_REF | PVF_EXEC;
   3668 				}
   3669 
   3670 				if (flush >= 0) {
   3671 					flush++;
   3672 					flags |= f;
   3673 				} else
   3674 				if (PV_BEEN_EXECD(f))
   3675 					pmap_tlb_flushID_SE(pm, sva);
   3676 				else
   3677 				if (PV_BEEN_REFD(f))
   3678 					pmap_tlb_flushD_SE(pm, sva);
   3679 			}
   3680 
   3681 			sva += PAGE_SIZE;
   3682 			ptep++;
   3683 		}
   3684 	}
   3685 
   3686 	pmap_release_pmap_lock(pm);
   3687 
   3688 	if (flush) {
   3689 		if (PV_BEEN_EXECD(flags))
   3690 			pmap_tlb_flushID(pm);
   3691 		else
   3692 		if (PV_BEEN_REFD(flags))
   3693 			pmap_tlb_flushD(pm);
   3694 	}
   3695 }
   3696 
   3697 void
   3698 pmap_icache_sync_range(pmap_t pm, vaddr_t sva, vaddr_t eva)
   3699 {
   3700 	struct l2_bucket *l2b;
   3701 	pt_entry_t *ptep;
   3702 	vaddr_t next_bucket;
   3703 	vsize_t page_size = trunc_page(sva) + PAGE_SIZE - sva;
   3704 
   3705 	NPDEBUG(PDB_EXEC,
   3706 	    printf("pmap_icache_sync_range: pm %p sva 0x%lx eva 0x%lx\n",
   3707 	    pm, sva, eva));
   3708 
   3709 	pmap_acquire_pmap_lock(pm);
   3710 
   3711 	while (sva < eva) {
   3712 		next_bucket = L2_NEXT_BUCKET(sva);
   3713 		if (next_bucket > eva)
   3714 			next_bucket = eva;
   3715 
   3716 		l2b = pmap_get_l2_bucket(pm, sva);
   3717 		if (l2b == NULL) {
   3718 			sva = next_bucket;
   3719 			continue;
   3720 		}
   3721 
   3722 		for (ptep = &l2b->l2b_kva[l2pte_index(sva)];
   3723 		     sva < next_bucket;
   3724 		     sva += page_size, ptep++, page_size = PAGE_SIZE) {
   3725 			if (l2pte_valid(*ptep)) {
   3726 				cpu_icache_sync_range(sva,
   3727 				    min(page_size, eva - sva));
   3728 			}
   3729 		}
   3730 	}
   3731 
   3732 	pmap_release_pmap_lock(pm);
   3733 }
   3734 
   3735 void
   3736 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   3737 {
   3738 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3739 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3740 
   3741 	NPDEBUG(PDB_PROTECT,
   3742 	    printf("pmap_page_protect: md %p (0x%08lx), prot 0x%x\n",
   3743 	    md, pa, prot));
   3744 
   3745 #ifdef MULTIPROCESSOR
   3746 	KASSERT(uvm_page_locked_p(pg));
   3747 #endif
   3748 
   3749 	switch(prot) {
   3750 	case VM_PROT_READ|VM_PROT_WRITE:
   3751 #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
   3752 		pmap_clearbit(md, pa, PVF_EXEC);
   3753 		break;
   3754 #endif
   3755 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   3756 		break;
   3757 
   3758 	case VM_PROT_READ:
   3759 #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
   3760 		pmap_clearbit(md, pa, PVF_WRITE|PVF_EXEC);
   3761 		break;
   3762 #endif
   3763 	case VM_PROT_READ|VM_PROT_EXECUTE:
   3764 		pmap_clearbit(md, pa, PVF_WRITE);
   3765 		break;
   3766 
   3767 	default:
   3768 		pmap_page_remove(md, pa);
   3769 		break;
   3770 	}
   3771 }
   3772 
   3773 /*
   3774  * pmap_clear_modify:
   3775  *
   3776  *	Clear the "modified" attribute for a page.
   3777  */
   3778 bool
   3779 pmap_clear_modify(struct vm_page *pg)
   3780 {
   3781 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3782 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3783 	bool rv;
   3784 
   3785 #ifdef MULTIPROCESSOR
   3786 	KASSERT(uvm_page_locked_p(pg));
   3787 #endif
   3788 
   3789 	if (md->pvh_attrs & PVF_MOD) {
   3790 		rv = true;
   3791 #ifdef PMAP_CACHE_VIPT
   3792 		/*
   3793 		 * If we are going to clear the modified bit and there are
   3794 		 * no other modified bits set, flush the page to memory and
   3795 		 * mark it clean.
   3796 		 */
   3797 		if ((md->pvh_attrs & (PVF_DMOD|PVF_NC)) == PVF_MOD)
   3798 			pmap_flush_page(md, pa, PMAP_CLEAN_PRIMARY);
   3799 #endif
   3800 		pmap_clearbit(md, pa, PVF_MOD);
   3801 	} else
   3802 		rv = false;
   3803 
   3804 	return (rv);
   3805 }
   3806 
   3807 /*
   3808  * pmap_clear_reference:
   3809  *
   3810  *	Clear the "referenced" attribute for a page.
   3811  */
   3812 bool
   3813 pmap_clear_reference(struct vm_page *pg)
   3814 {
   3815 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3816 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3817 	bool rv;
   3818 
   3819 #ifdef MULTIPROCESSOR
   3820 	KASSERT(uvm_page_locked_p(pg));
   3821 #endif
   3822 
   3823 	if (md->pvh_attrs & PVF_REF) {
   3824 		rv = true;
   3825 		pmap_clearbit(md, pa, PVF_REF);
   3826 	} else
   3827 		rv = false;
   3828 
   3829 	return (rv);
   3830 }
   3831 
   3832 /*
   3833  * pmap_is_modified:
   3834  *
   3835  *	Test if a page has the "modified" attribute.
   3836  */
   3837 /* See <arm/arm32/pmap.h> */
   3838 
   3839 /*
   3840  * pmap_is_referenced:
   3841  *
   3842  *	Test if a page has the "referenced" attribute.
   3843  */
   3844 /* See <arm/arm32/pmap.h> */
   3845 
   3846 int
   3847 pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
   3848 {
   3849 	struct l2_dtable *l2;
   3850 	struct l2_bucket *l2b;
   3851 	pd_entry_t *pl1pd, l1pd;
   3852 	pt_entry_t *ptep, pte;
   3853 	paddr_t pa;
   3854 	u_int l1idx;
   3855 	int rv = 0;
   3856 
   3857 	pmap_acquire_pmap_lock(pm);
   3858 
   3859 	l1idx = L1_IDX(va);
   3860 
   3861 	/*
   3862 	 * If there is no l2_dtable for this address, then the process
   3863 	 * has no business accessing it.
   3864 	 *
   3865 	 * Note: This will catch userland processes trying to access
   3866 	 * kernel addresses.
   3867 	 */
   3868 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   3869 	if (l2 == NULL)
   3870 		goto out;
   3871 
   3872 	/*
   3873 	 * Likewise if there is no L2 descriptor table
   3874 	 */
   3875 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   3876 	if (l2b->l2b_kva == NULL)
   3877 		goto out;
   3878 
   3879 	/*
   3880 	 * Check the PTE itself.
   3881 	 */
   3882 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3883 	pte = *ptep;
   3884 	if (pte == 0)
   3885 		goto out;
   3886 
   3887 	/*
   3888 	 * Catch a userland access to the vector page mapped at 0x0
   3889 	 */
   3890 	if (user && (pte & L2_S_PROT_U) == 0)
   3891 		goto out;
   3892 
   3893 	pa = l2pte_pa(pte);
   3894 
   3895 	if ((ftype & VM_PROT_WRITE) && !l2pte_writable_p(pte)) {
   3896 		/*
   3897 		 * This looks like a good candidate for "page modified"
   3898 		 * emulation...
   3899 		 */
   3900 		struct pv_entry *pv;
   3901 		struct vm_page *pg;
   3902 
   3903 		/* Extract the physical address of the page */
   3904 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3905 			goto out;
   3906 
   3907 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3908 
   3909 		/* Get the current flags for this page. */
   3910 #ifdef MULTIPROCESSOR
   3911 		KASSERT(uvm_page_locked_p(pg));
   3912 #endif
   3913 
   3914 		pv = pmap_find_pv(md, pm, va);
   3915 		if (pv == NULL) {
   3916 			goto out;
   3917 		}
   3918 
   3919 		/*
   3920 		 * Do the flags say this page is writable? If not then it
   3921 		 * is a genuine write fault. If yes then the write fault is
   3922 		 * our fault as we did not reflect the write access in the
   3923 		 * PTE. Now we know a write has occurred we can correct this
   3924 		 * and also set the modified bit
   3925 		 */
   3926 		if ((pv->pv_flags & PVF_WRITE) == 0) {
   3927 			goto out;
   3928 		}
   3929 
   3930 		NPDEBUG(PDB_FOLLOW,
   3931 		    printf("pmap_fault_fixup: mod emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   3932 		    pm, va, pa));
   3933 
   3934 		md->pvh_attrs |= PVF_REF | PVF_MOD;
   3935 		pv->pv_flags |= PVF_REF | PVF_MOD;
   3936 #ifdef PMAP_CACHE_VIPT
   3937 		/*
   3938 		 * If there are cacheable mappings for this page, mark it dirty.
   3939 		 */
   3940 		if ((md->pvh_attrs & PVF_NC) == 0)
   3941 			md->pvh_attrs |= PVF_DIRTY;
   3942 #endif
   3943 
   3944 		/*
   3945 		 * Re-enable write permissions for the page.  No need to call
   3946 		 * pmap_vac_me_harder(), since this is just a
   3947 		 * modified-emulation fault, and the PVF_WRITE bit isn't
   3948 		 * changing. We've already set the cacheable bits based on
   3949 		 * the assumption that we can write to this page.
   3950 		 */
   3951 		*ptep = l2pte_set_writable((pte & ~L2_TYPE_MASK) | L2_S_PROTO);
   3952 		PTE_SYNC(ptep);
   3953 		rv = 1;
   3954 	} else
   3955 	if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
   3956 		/*
   3957 		 * This looks like a good candidate for "page referenced"
   3958 		 * emulation.
   3959 		 */
   3960 		struct pv_entry *pv;
   3961 		struct vm_page *pg;
   3962 
   3963 		/* Extract the physical address of the page */
   3964 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3965 			goto out;
   3966 
   3967 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   3968 
   3969 		/* Get the current flags for this page. */
   3970 #ifdef MULTIPROCESSOR
   3971 		KASSERT(uvm_page_locked_p(pg));
   3972 #endif
   3973 
   3974 		pv = pmap_find_pv(md, pm, va);
   3975 		if (pv == NULL) {
   3976 			goto out;
   3977 		}
   3978 
   3979 		md->pvh_attrs |= PVF_REF;
   3980 		pv->pv_flags |= PVF_REF;
   3981 
   3982 		NPDEBUG(PDB_FOLLOW,
   3983 		    printf("pmap_fault_fixup: ref emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   3984 		    pm, va, pa));
   3985 
   3986 		*ptep = l2pte_set_readonly((pte & ~L2_TYPE_MASK) | L2_S_PROTO);
   3987 		PTE_SYNC(ptep);
   3988 		rv = 1;
   3989 	}
   3990 
   3991 	/*
   3992 	 * We know there is a valid mapping here, so simply
   3993 	 * fix up the L1 if necessary.
   3994 	 */
   3995 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   3996 	l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
   3997 	if (*pl1pd != l1pd) {
   3998 		*pl1pd = l1pd;
   3999 		PTE_SYNC(pl1pd);
   4000 		rv = 1;
   4001 	}
   4002 
   4003 #ifdef CPU_SA110
   4004 	/*
   4005 	 * There are bugs in the rev K SA110.  This is a check for one
   4006 	 * of them.
   4007 	 */
   4008 	if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
   4009 	    curcpu()->ci_arm_cpurev < 3) {
   4010 		/* Always current pmap */
   4011 		if (l2pte_valid(pte)) {
   4012 			extern int kernel_debug;
   4013 			if (kernel_debug & 1) {
   4014 				struct proc *p = curlwp->l_proc;
   4015 				printf("prefetch_abort: page is already "
   4016 				    "mapped - pte=%p *pte=%08x\n", ptep, pte);
   4017 				printf("prefetch_abort: pc=%08lx proc=%p "
   4018 				    "process=%s\n", va, p, p->p_comm);
   4019 				printf("prefetch_abort: far=%08x fs=%x\n",
   4020 				    cpu_faultaddress(), cpu_faultstatus());
   4021 			}
   4022 #ifdef DDB
   4023 			if (kernel_debug & 2)
   4024 				Debugger();
   4025 #endif
   4026 			rv = 1;
   4027 		}
   4028 	}
   4029 #endif /* CPU_SA110 */
   4030 
   4031 	/*
   4032 	 * If 'rv == 0' at this point, it generally indicates that there is a
   4033 	 * stale TLB entry for the faulting address.  That might be due to a
   4034 	 * wrong setting of pmap_needs_pte_sync.  So set it and retry.
   4035 	 */
   4036 	if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1
   4037 	    && pmap_needs_pte_sync == 0) {
   4038 		pmap_needs_pte_sync = 1;
   4039 		PTE_SYNC(ptep);
   4040 		rv = 1;
   4041 	}
   4042 
   4043 #ifdef DEBUG
   4044 	/*
   4045 	 * If 'rv == 0' at this point, it generally indicates that there is a
   4046 	 * stale TLB entry for the faulting address. This happens when two or
   4047 	 * more processes are sharing an L1. Since we don't flush the TLB on
   4048 	 * a context switch between such processes, we can take domain faults
   4049 	 * for mappings which exist at the same VA in both processes. EVEN IF
   4050 	 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
   4051 	 * example.
   4052 	 *
   4053 	 * This is extremely likely to happen if pmap_enter() updated the L1
   4054 	 * entry for a recently entered mapping. In this case, the TLB is
   4055 	 * flushed for the new mapping, but there may still be TLB entries for
   4056 	 * other mappings belonging to other processes in the 1MB range
   4057 	 * covered by the L1 entry.
   4058 	 *
   4059 	 * Since 'rv == 0', we know that the L1 already contains the correct
   4060 	 * value, so the fault must be due to a stale TLB entry.
   4061 	 *
   4062 	 * Since we always need to flush the TLB anyway in the case where we
   4063 	 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
   4064 	 * stale TLB entries dynamically.
   4065 	 *
   4066 	 * However, the above condition can ONLY happen if the current L1 is
   4067 	 * being shared. If it happens when the L1 is unshared, it indicates
   4068 	 * that other parts of the pmap are not doing their job WRT managing
   4069 	 * the TLB.
   4070 	 */
   4071 	if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
   4072 		extern int last_fault_code;
   4073 		extern int kernel_debug;
   4074 		printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
   4075 		    pm, va, ftype);
   4076 		printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
   4077 		    l2, l2b, ptep, pl1pd);
   4078 		printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
   4079 		    pte, l1pd, last_fault_code);
   4080 #ifdef DDB
   4081 		if (kernel_debug & 2)
   4082 			Debugger();
   4083 #endif
   4084 	}
   4085 #endif
   4086 
   4087 	cpu_tlb_flushID_SE(va);
   4088 	cpu_cpwait();
   4089 
   4090 	rv = 1;
   4091 
   4092 out:
   4093 	pmap_release_pmap_lock(pm);
   4094 
   4095 	return (rv);
   4096 }
   4097 
   4098 /*
   4099  * Routine:	pmap_procwr
   4100  *
   4101  * Function:
   4102  *	Synchronize caches corresponding to [addr, addr+len) in p.
   4103  *
   4104  */
   4105 void
   4106 pmap_procwr(struct proc *p, vaddr_t va, int len)
   4107 {
   4108 	/* We only need to do anything if it is the current process. */
   4109 	if (p == curproc)
   4110 		cpu_icache_sync_range(va, len);
   4111 }
   4112 
   4113 /*
   4114  * Routine:	pmap_unwire
   4115  * Function:	Clear the wired attribute for a map/virtual-address pair.
   4116  *
   4117  * In/out conditions:
   4118  *		The mapping must already exist in the pmap.
   4119  */
   4120 void
   4121 pmap_unwire(pmap_t pm, vaddr_t va)
   4122 {
   4123 	struct l2_bucket *l2b;
   4124 	pt_entry_t *ptep, pte;
   4125 	struct vm_page *pg;
   4126 	paddr_t pa;
   4127 
   4128 	NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
   4129 
   4130 	pmap_acquire_pmap_lock(pm);
   4131 
   4132 	l2b = pmap_get_l2_bucket(pm, va);
   4133 	KDASSERT(l2b != NULL);
   4134 
   4135 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   4136 	pte = *ptep;
   4137 
   4138 	/* Extract the physical address of the page */
   4139 	pa = l2pte_pa(pte);
   4140 
   4141 	if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   4142 		/* Update the wired bit in the pv entry for this page. */
   4143 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   4144 
   4145 #ifdef MULTIPROCESSOR
   4146 		KASSERT(uvm_page_locked_p(pg));
   4147 #endif
   4148 		(void) pmap_modify_pv(md, pa, pm, va, PVF_WIRED, 0);
   4149 	}
   4150 
   4151 	pmap_release_pmap_lock(pm);
   4152 }
   4153 
   4154 void
   4155 pmap_activate(struct lwp *l)
   4156 {
   4157 	extern int block_userspace_access;
   4158 	pmap_t opm, npm, rpm;
   4159 	uint32_t odacr, ndacr;
   4160 	int oldirqstate;
   4161 
   4162 	/*
   4163 	 * If activating a non-current lwp or the current lwp is
   4164 	 * already active, just return.
   4165 	 */
   4166 	if (l != curlwp ||
   4167 	    l->l_proc->p_vmspace->vm_map.pmap->pm_activated == true)
   4168 		return;
   4169 
   4170 	npm = l->l_proc->p_vmspace->vm_map.pmap;
   4171 	ndacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
   4172 	    (DOMAIN_CLIENT << (npm->pm_domain * 2));
   4173 
   4174 	/*
   4175 	 * If TTB and DACR are unchanged, short-circuit all the
   4176 	 * TLB/cache management stuff.
   4177 	 */
   4178 	if (pmap_previous_active_lwp != NULL) {
   4179 		opm = pmap_previous_active_lwp->l_proc->p_vmspace->vm_map.pmap;
   4180 		odacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
   4181 		    (DOMAIN_CLIENT << (opm->pm_domain * 2));
   4182 
   4183 		if (opm->pm_l1 == npm->pm_l1 && odacr == ndacr)
   4184 			goto all_done;
   4185 	} else
   4186 		opm = NULL;
   4187 
   4188 	PMAPCOUNT(activations);
   4189 	block_userspace_access = 1;
   4190 
   4191 	/*
   4192 	 * If switching to a user vmspace which is different to the
   4193 	 * most recent one, and the most recent one is potentially
   4194 	 * live in the cache, we must write-back and invalidate the
   4195 	 * entire cache.
   4196 	 */
   4197 	rpm = pmap_recent_user;
   4198 
   4199 /*
   4200  * XXXSCW: There's a corner case here which can leave turds in the cache as
   4201  * reported in kern/41058. They're probably left over during tear-down and
   4202  * switching away from an exiting process. Until the root cause is identified
   4203  * and fixed, zap the cache when switching pmaps. This will result in a few
   4204  * unnecessary cache flushes, but that's better than silently corrupting data.
   4205  */
   4206 #if 0
   4207 	if (npm != pmap_kernel() && rpm && npm != rpm &&
   4208 	    rpm->pm_cstate.cs_cache) {
   4209 		rpm->pm_cstate.cs_cache = 0;
   4210 #ifdef PMAP_CACHE_VIVT
   4211 		cpu_idcache_wbinv_all();
   4212 #endif
   4213 	}
   4214 #else
   4215 	if (rpm) {
   4216 		rpm->pm_cstate.cs_cache = 0;
   4217 		if (npm == pmap_kernel())
   4218 			pmap_recent_user = NULL;
   4219 #ifdef PMAP_CACHE_VIVT
   4220 		cpu_idcache_wbinv_all();
   4221 #endif
   4222 	}
   4223 #endif
   4224 
   4225 	/* No interrupts while we frob the TTB/DACR */
   4226 	oldirqstate = disable_interrupts(IF32_bits);
   4227 
   4228 	/*
   4229 	 * For ARM_VECTORS_LOW, we MUST, I repeat, MUST fix up the L1
   4230 	 * entry corresponding to 'vector_page' in the incoming L1 table
   4231 	 * before switching to it otherwise subsequent interrupts/exceptions
   4232 	 * (including domain faults!) will jump into hyperspace.
   4233 	 */
   4234 	if (npm->pm_pl1vec != NULL) {
   4235 		cpu_tlb_flushID_SE((u_int)vector_page);
   4236 		cpu_cpwait();
   4237 		*npm->pm_pl1vec = npm->pm_l1vec;
   4238 		PTE_SYNC(npm->pm_pl1vec);
   4239 	}
   4240 
   4241 	cpu_domains(ndacr);
   4242 
   4243 	if (npm == pmap_kernel() || npm == rpm) {
   4244 		/*
   4245 		 * Switching to a kernel thread, or back to the
   4246 		 * same user vmspace as before... Simply update
   4247 		 * the TTB (no TLB flush required)
   4248 		 */
   4249 		cpu_setttb(npm->pm_l1->l1_physaddr, false);
   4250 		cpu_cpwait();
   4251 	} else {
   4252 		/*
   4253 		 * Otherwise, update TTB and flush TLB
   4254 		 */
   4255 		cpu_context_switch(npm->pm_l1->l1_physaddr);
   4256 		if (rpm != NULL)
   4257 			rpm->pm_cstate.cs_tlb = 0;
   4258 	}
   4259 
   4260 	restore_interrupts(oldirqstate);
   4261 
   4262 	block_userspace_access = 0;
   4263 
   4264  all_done:
   4265 	/*
   4266 	 * The new pmap is resident. Make sure it's marked
   4267 	 * as resident in the cache/TLB.
   4268 	 */
   4269 	npm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   4270 	if (npm != pmap_kernel())
   4271 		pmap_recent_user = npm;
   4272 
   4273 	/* The old pmap is not longer active */
   4274 	if (opm != NULL)
   4275 		opm->pm_activated = false;
   4276 
   4277 	/* But the new one is */
   4278 	npm->pm_activated = true;
   4279 }
   4280 
   4281 void
   4282 pmap_deactivate(struct lwp *l)
   4283 {
   4284 
   4285 	/*
   4286 	 * If the process is exiting, make sure pmap_activate() does
   4287 	 * a full MMU context-switch and cache flush, which we might
   4288 	 * otherwise skip. See PR port-arm/38950.
   4289 	 */
   4290 	if (l->l_proc->p_sflag & PS_WEXIT)
   4291 		pmap_previous_active_lwp = NULL;
   4292 
   4293 	l->l_proc->p_vmspace->vm_map.pmap->pm_activated = false;
   4294 }
   4295 
   4296 void
   4297 pmap_update(pmap_t pm)
   4298 {
   4299 
   4300 	if (pm->pm_remove_all) {
   4301 		/*
   4302 		 * Finish up the pmap_remove_all() optimisation by flushing
   4303 		 * the TLB.
   4304 		 */
   4305 		pmap_tlb_flushID(pm);
   4306 		pm->pm_remove_all = false;
   4307 	}
   4308 
   4309 	if (pmap_is_current(pm)) {
   4310 		/*
   4311 		 * If we're dealing with a current userland pmap, move its L1
   4312 		 * to the end of the LRU.
   4313 		 */
   4314 		if (pm != pmap_kernel())
   4315 			pmap_use_l1(pm);
   4316 
   4317 		/*
   4318 		 * We can assume we're done with frobbing the cache/tlb for
   4319 		 * now. Make sure any future pmap ops don't skip cache/tlb
   4320 		 * flushes.
   4321 		 */
   4322 		pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   4323 	}
   4324 
   4325 	PMAPCOUNT(updates);
   4326 
   4327 	/*
   4328 	 * make sure TLB/cache operations have completed.
   4329 	 */
   4330 	cpu_cpwait();
   4331 }
   4332 
   4333 void
   4334 pmap_remove_all(pmap_t pm)
   4335 {
   4336 
   4337 	/*
   4338 	 * The vmspace described by this pmap is about to be torn down.
   4339 	 * Until pmap_update() is called, UVM will only make calls
   4340 	 * to pmap_remove(). We can make life much simpler by flushing
   4341 	 * the cache now, and deferring TLB invalidation to pmap_update().
   4342 	 */
   4343 #ifdef PMAP_CACHE_VIVT
   4344 	pmap_idcache_wbinv_all(pm);
   4345 #endif
   4346 	pm->pm_remove_all = true;
   4347 }
   4348 
   4349 /*
   4350  * Retire the given physical map from service.
   4351  * Should only be called if the map contains no valid mappings.
   4352  */
   4353 void
   4354 pmap_destroy(pmap_t pm)
   4355 {
   4356 	u_int count;
   4357 
   4358 	if (pm == NULL)
   4359 		return;
   4360 
   4361 	if (pm->pm_remove_all) {
   4362 		pmap_tlb_flushID(pm);
   4363 		pm->pm_remove_all = false;
   4364 	}
   4365 
   4366 	/*
   4367 	 * Drop reference count
   4368 	 */
   4369 	mutex_enter(pm->pm_lock);
   4370 	count = --pm->pm_obj.uo_refs;
   4371 	mutex_exit(pm->pm_lock);
   4372 	if (count > 0) {
   4373 		if (pmap_is_current(pm)) {
   4374 			if (pm != pmap_kernel())
   4375 				pmap_use_l1(pm);
   4376 			pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   4377 		}
   4378 		return;
   4379 	}
   4380 
   4381 	/*
   4382 	 * reference count is zero, free pmap resources and then free pmap.
   4383 	 */
   4384 
   4385 	if (vector_page < KERNEL_BASE) {
   4386 		KDASSERT(!pmap_is_current(pm));
   4387 
   4388 		/* Remove the vector page mapping */
   4389 		pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
   4390 		pmap_update(pm);
   4391 	}
   4392 
   4393 	LIST_REMOVE(pm, pm_list);
   4394 
   4395 	pmap_free_l1(pm);
   4396 
   4397 	if (pmap_recent_user == pm)
   4398 		pmap_recent_user = NULL;
   4399 
   4400 	uvm_obj_destroy(&pm->pm_obj, false);
   4401 	mutex_destroy(&pm->pm_obj_lock);
   4402 	pool_cache_put(&pmap_cache, pm);
   4403 }
   4404 
   4405 
   4406 /*
   4407  * void pmap_reference(pmap_t pm)
   4408  *
   4409  * Add a reference to the specified pmap.
   4410  */
   4411 void
   4412 pmap_reference(pmap_t pm)
   4413 {
   4414 
   4415 	if (pm == NULL)
   4416 		return;
   4417 
   4418 	pmap_use_l1(pm);
   4419 
   4420 	mutex_enter(pm->pm_lock);
   4421 	pm->pm_obj.uo_refs++;
   4422 	mutex_exit(pm->pm_lock);
   4423 }
   4424 
   4425 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   4426 
   4427 static struct evcnt pmap_prefer_nochange_ev =
   4428     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange");
   4429 static struct evcnt pmap_prefer_change_ev =
   4430     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change");
   4431 
   4432 EVCNT_ATTACH_STATIC(pmap_prefer_change_ev);
   4433 EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev);
   4434 
   4435 void
   4436 pmap_prefer(vaddr_t hint, vaddr_t *vap, int td)
   4437 {
   4438 	vsize_t mask = arm_cache_prefer_mask | (PAGE_SIZE - 1);
   4439 	vaddr_t va = *vap;
   4440 	vaddr_t diff = (hint - va) & mask;
   4441 	if (diff == 0) {
   4442 		pmap_prefer_nochange_ev.ev_count++;
   4443 	} else {
   4444 		pmap_prefer_change_ev.ev_count++;
   4445 		if (__predict_false(td))
   4446 			va -= mask + 1;
   4447 		*vap = va + diff;
   4448 	}
   4449 }
   4450 #endif /* ARM_MMU_V6 | ARM_MMU_V7 */
   4451 
   4452 /*
   4453  * pmap_zero_page()
   4454  *
   4455  * Zero a given physical page by mapping it at a page hook point.
   4456  * In doing the zero page op, the page we zero is mapped cachable, as with
   4457  * StrongARM accesses to non-cached pages are non-burst making writing
   4458  * _any_ bulk data very slow.
   4459  */
   4460 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
   4461 void
   4462 pmap_zero_page_generic(paddr_t phys)
   4463 {
   4464 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4465 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   4466 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   4467 #endif
   4468 #if defined(PMAP_CACHE_VIPT)
   4469 	/* Choose the last page color it had, if any */
   4470 	const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
   4471 #else
   4472 	const vsize_t va_offset = 0;
   4473 #endif
   4474 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
   4475 	/*
   4476 	 * Is this page mapped at its natural color?
   4477 	 * If we have all of memory mapped, then just convert PA to VA.
   4478 	 */
   4479 	const bool okcolor = va_offset == (phys & arm_cache_prefer_mask);
   4480 	const vaddr_t vdstp = KERNEL_BASE + (phys - physical_start);
   4481 #else
   4482 	const bool okcolor = false;
   4483 	const vaddr_t vdstp = cdstp + va_offset;
   4484 #endif
   4485 	pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
   4486 
   4487 
   4488 #ifdef DEBUG
   4489 	if (!SLIST_EMPTY(&md->pvh_list))
   4490 		panic("pmap_zero_page: page has mappings");
   4491 #endif
   4492 
   4493 	KDASSERT((phys & PGOFSET) == 0);
   4494 
   4495 	if (!okcolor) {
   4496 		/*
   4497 		 * Hook in the page, zero it, and purge the cache for that
   4498 		 * zeroed page. Invalidate the TLB as needed.
   4499 		 */
   4500 		*ptep = L2_S_PROTO | phys |
   4501 		    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4502 		PTE_SYNC(ptep);
   4503 		cpu_tlb_flushD_SE(cdstp + va_offset);
   4504 		cpu_cpwait();
   4505 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT)
   4506 		/*
   4507 		 * If we are direct-mapped and our color isn't ok, then before
   4508 		 * we bzero the page invalidate its contents from the cache and
   4509 		 * reset the color to its natural color.
   4510 		 */
   4511 		cpu_dcache_inv_range(cdstp + va_offset, PAGE_SIZE);
   4512 		md->pvh_attrs &= ~arm_cache_prefer_mask;
   4513 		md->pvh_attrs |= (phys & arm_cache_prefer_mask);
   4514 #endif
   4515 	}
   4516 	bzero_page(vdstp);
   4517 	if (!okcolor) {
   4518 		/*
   4519 		 * Unmap the page.
   4520 		 */
   4521 		*ptep = 0;
   4522 		PTE_SYNC(ptep);
   4523 		cpu_tlb_flushD_SE(cdstp + va_offset);
   4524 #ifdef PMAP_CACHE_VIVT
   4525 		cpu_dcache_wbinv_range(cdstp + va_offset, PAGE_SIZE);
   4526 #endif
   4527 	}
   4528 #ifdef PMAP_CACHE_VIPT
   4529 	/*
   4530 	 * This page is now cache resident so it now has a page color.
   4531 	 * Any contents have been obliterated so clear the EXEC flag.
   4532 	 */
   4533 	if (!pmap_is_page_colored_p(md)) {
   4534 		PMAPCOUNT(vac_color_new);
   4535 		md->pvh_attrs |= PVF_COLORED;
   4536 	}
   4537 	if (PV_IS_EXEC_P(md->pvh_attrs)) {
   4538 		md->pvh_attrs &= ~PVF_EXEC;
   4539 		PMAPCOUNT(exec_discarded_zero);
   4540 	}
   4541 	md->pvh_attrs |= PVF_DIRTY;
   4542 #endif
   4543 }
   4544 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   4545 
   4546 #if ARM_MMU_XSCALE == 1
   4547 void
   4548 pmap_zero_page_xscale(paddr_t phys)
   4549 {
   4550 #ifdef DEBUG
   4551 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   4552 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   4553 
   4554 	if (!SLIST_EMPTY(&md->pvh_list))
   4555 		panic("pmap_zero_page: page has mappings");
   4556 #endif
   4557 
   4558 	KDASSERT((phys & PGOFSET) == 0);
   4559 
   4560 	/*
   4561 	 * Hook in the page, zero it, and purge the cache for that
   4562 	 * zeroed page. Invalidate the TLB as needed.
   4563 	 */
   4564 	*cdst_pte = L2_S_PROTO | phys |
   4565 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   4566 	    L2_C | L2_XS_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4567 	PTE_SYNC(cdst_pte);
   4568 	cpu_tlb_flushD_SE(cdstp);
   4569 	cpu_cpwait();
   4570 	bzero_page(cdstp);
   4571 	xscale_cache_clean_minidata();
   4572 }
   4573 #endif /* ARM_MMU_XSCALE == 1 */
   4574 
   4575 /* pmap_pageidlezero()
   4576  *
   4577  * The same as above, except that we assume that the page is not
   4578  * mapped.  This means we never have to flush the cache first.  Called
   4579  * from the idle loop.
   4580  */
   4581 bool
   4582 pmap_pageidlezero(paddr_t phys)
   4583 {
   4584 	unsigned int i;
   4585 	int *ptr;
   4586 	bool rv = true;
   4587 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4588 	struct vm_page * const pg = PHYS_TO_VM_PAGE(phys);
   4589 	struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   4590 #endif
   4591 #ifdef PMAP_CACHE_VIPT
   4592 	/* Choose the last page color it had, if any */
   4593 	const vsize_t va_offset = md->pvh_attrs & arm_cache_prefer_mask;
   4594 #else
   4595 	const vsize_t va_offset = 0;
   4596 #endif
   4597 	pt_entry_t * const ptep = &csrc_pte[va_offset >> PGSHIFT];
   4598 
   4599 
   4600 #ifdef DEBUG
   4601 	if (!SLIST_EMPTY(&md->pvh_list))
   4602 		panic("pmap_pageidlezero: page has mappings");
   4603 #endif
   4604 
   4605 	KDASSERT((phys & PGOFSET) == 0);
   4606 
   4607 	/*
   4608 	 * Hook in the page, zero it, and purge the cache for that
   4609 	 * zeroed page. Invalidate the TLB as needed.
   4610 	 */
   4611 	*ptep = L2_S_PROTO | phys |
   4612 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4613 	PTE_SYNC(ptep);
   4614 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4615 	cpu_cpwait();
   4616 
   4617 	for (i = 0, ptr = (int *)(cdstp + va_offset);
   4618 			i < (PAGE_SIZE / sizeof(int)); i++) {
   4619 		if (sched_curcpu_runnable_p() != 0) {
   4620 			/*
   4621 			 * A process has become ready.  Abort now,
   4622 			 * so we don't keep it waiting while we
   4623 			 * do slow memory access to finish this
   4624 			 * page.
   4625 			 */
   4626 			rv = false;
   4627 			break;
   4628 		}
   4629 		*ptr++ = 0;
   4630 	}
   4631 
   4632 #ifdef PMAP_CACHE_VIVT
   4633 	if (rv)
   4634 		/*
   4635 		 * if we aborted we'll rezero this page again later so don't
   4636 		 * purge it unless we finished it
   4637 		 */
   4638 		cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
   4639 #elif defined(PMAP_CACHE_VIPT)
   4640 	/*
   4641 	 * This page is now cache resident so it now has a page color.
   4642 	 * Any contents have been obliterated so clear the EXEC flag.
   4643 	 */
   4644 	if (!pmap_is_page_colored_p(md)) {
   4645 		PMAPCOUNT(vac_color_new);
   4646 		md->pvh_attrs |= PVF_COLORED;
   4647 	}
   4648 	if (PV_IS_EXEC_P(md->pvh_attrs)) {
   4649 		md->pvh_attrs &= ~PVF_EXEC;
   4650 		PMAPCOUNT(exec_discarded_zero);
   4651 	}
   4652 #endif
   4653 	/*
   4654 	 * Unmap the page.
   4655 	 */
   4656 	*ptep = 0;
   4657 	PTE_SYNC(ptep);
   4658 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4659 
   4660 	return (rv);
   4661 }
   4662 
   4663 /*
   4664  * pmap_copy_page()
   4665  *
   4666  * Copy one physical page into another, by mapping the pages into
   4667  * hook points. The same comment regarding cachability as in
   4668  * pmap_zero_page also applies here.
   4669  */
   4670 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
   4671 void
   4672 pmap_copy_page_generic(paddr_t src, paddr_t dst)
   4673 {
   4674 	struct vm_page * const src_pg = PHYS_TO_VM_PAGE(src);
   4675 	struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
   4676 #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4677 	struct vm_page * const dst_pg = PHYS_TO_VM_PAGE(dst);
   4678 	struct vm_page_md *dst_md = VM_PAGE_TO_MD(dst_pg);
   4679 #endif
   4680 #ifdef PMAP_CACHE_VIPT
   4681 	const vsize_t src_va_offset = src_md->pvh_attrs & arm_cache_prefer_mask;
   4682 	const vsize_t dst_va_offset = dst_md->pvh_attrs & arm_cache_prefer_mask;
   4683 #else
   4684 	const vsize_t src_va_offset = 0;
   4685 	const vsize_t dst_va_offset = 0;
   4686 #endif
   4687 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS)
   4688 	/*
   4689 	 * Is this page mapped at its natural color?
   4690 	 * If we have all of memory mapped, then just convert PA to VA.
   4691 	 */
   4692 	const bool src_okcolor = src_va_offset == (src & arm_cache_prefer_mask);
   4693 	const bool dst_okcolor = dst_va_offset == (dst & arm_cache_prefer_mask);
   4694 	const vaddr_t vsrcp = src_okcolor
   4695 	    ? KERNEL_BASE + (src - physical_start)
   4696 	    : csrcp + src_va_offset;
   4697 	const vaddr_t vdstp = KERNEL_BASE + (dst - physical_start);
   4698 #else
   4699 	const bool src_okcolor = false;
   4700 	const bool dst_okcolor = false;
   4701 	const vaddr_t vsrcp = csrcp + src_va_offset;
   4702 	const vaddr_t vdstp = cdstp + dst_va_offset;
   4703 #endif
   4704 	pt_entry_t * const src_ptep = &csrc_pte[src_va_offset >> PGSHIFT];
   4705 	pt_entry_t * const dst_ptep = &cdst_pte[dst_va_offset >> PGSHIFT];
   4706 
   4707 #ifdef DEBUG
   4708 	if (!SLIST_EMPTY(&dst_md->pvh_list))
   4709 		panic("pmap_copy_page: dst page has mappings");
   4710 #endif
   4711 
   4712 #ifdef PMAP_CACHE_VIPT
   4713 	KASSERT(arm_cache_prefer_mask == 0 || src_md->pvh_attrs & (PVF_COLORED|PVF_NC));
   4714 #endif
   4715 	KDASSERT((src & PGOFSET) == 0);
   4716 	KDASSERT((dst & PGOFSET) == 0);
   4717 
   4718 	/*
   4719 	 * Clean the source page.  Hold the source page's lock for
   4720 	 * the duration of the copy so that no other mappings can
   4721 	 * be created while we have a potentially aliased mapping.
   4722 	 */
   4723 #ifdef MULTIPROCESSOR
   4724 	KASSERT(uvm_page_locked_p(src_pg));
   4725 #endif
   4726 #ifdef PMAP_CACHE_VIVT
   4727 	(void) pmap_clean_page(SLIST_FIRST(&src_md->pvh_list), true);
   4728 #endif
   4729 
   4730 	/*
   4731 	 * Map the pages into the page hook points, copy them, and purge
   4732 	 * the cache for the appropriate page. Invalidate the TLB
   4733 	 * as required.
   4734 	 */
   4735 	if (!src_okcolor) {
   4736 		*src_ptep = L2_S_PROTO
   4737 		    | src
   4738 #ifdef PMAP_CACHE_VIPT
   4739 		    | ((src_md->pvh_attrs & PVF_NC) ? 0 : pte_l2_s_cache_mode)
   4740 #endif
   4741 #ifdef PMAP_CACHE_VIVT
   4742 		    | pte_l2_s_cache_mode
   4743 #endif
   4744 		    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ);
   4745 		PTE_SYNC(src_ptep);
   4746 		cpu_tlb_flushD_SE(csrcp + src_va_offset);
   4747 		cpu_cpwait();
   4748 	}
   4749 	if (!dst_okcolor) {
   4750 		*dst_ptep = L2_S_PROTO | dst |
   4751 		    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4752 		PTE_SYNC(dst_ptep);
   4753 		cpu_tlb_flushD_SE(cdstp + dst_va_offset);
   4754 		cpu_cpwait();
   4755 #if defined(__HAVE_MM_MD_DIRECT_MAPPED_PHYS) && defined(PMAP_CACHE_VIPT)
   4756 		/*
   4757 		 * If we are direct-mapped and our color isn't ok, then before
   4758 		 * we bcopy to the new page invalidate its contents from the
   4759 		 * cache and reset its color to its natural color.
   4760 		 */
   4761 		cpu_dcache_inv_range(cdstp + dst_va_offset, PAGE_SIZE);
   4762 		dst_md->pvh_attrs &= ~arm_cache_prefer_mask;
   4763 		dst_md->pvh_attrs |= (dst & arm_cache_prefer_mask);
   4764 #endif
   4765 	}
   4766 	bcopy_page(vsrcp, vdstp);
   4767 #ifdef PMAP_CACHE_VIVT
   4768 	cpu_dcache_inv_range(vsrcp, PAGE_SIZE);
   4769 	cpu_dcache_wbinv_range(vdstp, PAGE_SIZE);
   4770 #endif
   4771 	/*
   4772 	 * Unmap the pages.
   4773 	 */
   4774 	if (!src_okcolor) {
   4775 		*src_ptep = 0;
   4776 		PTE_SYNC(src_ptep);
   4777 		cpu_tlb_flushD_SE(csrcp + src_va_offset);
   4778 		cpu_cpwait();
   4779 	}
   4780 	if (!dst_okcolor) {
   4781 		*dst_ptep = 0;
   4782 		PTE_SYNC(dst_ptep);
   4783 		cpu_tlb_flushD_SE(cdstp + dst_va_offset);
   4784 		cpu_cpwait();
   4785 	}
   4786 #ifdef PMAP_CACHE_VIPT
   4787 	/*
   4788 	 * Now that the destination page is in the cache, mark it as colored.
   4789 	 * If this was an exec page, discard it.
   4790 	 */
   4791 	if (!pmap_is_page_colored_p(dst_md)) {
   4792 		PMAPCOUNT(vac_color_new);
   4793 		dst_md->pvh_attrs |= PVF_COLORED;
   4794 	}
   4795 	if (PV_IS_EXEC_P(dst_md->pvh_attrs)) {
   4796 		dst_md->pvh_attrs &= ~PVF_EXEC;
   4797 		PMAPCOUNT(exec_discarded_copy);
   4798 	}
   4799 	dst_md->pvh_attrs |= PVF_DIRTY;
   4800 #endif
   4801 }
   4802 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   4803 
   4804 #if ARM_MMU_XSCALE == 1
   4805 void
   4806 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
   4807 {
   4808 	struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
   4809 	struct vm_page_md *src_md = VM_PAGE_TO_MD(src_pg);
   4810 #ifdef DEBUG
   4811 	struct vm_page_md *dst_md = VM_PAGE_TO_MD(PHYS_TO_VM_PAGE(dst));
   4812 
   4813 	if (!SLIST_EMPTY(&dst_md->pvh_list))
   4814 		panic("pmap_copy_page: dst page has mappings");
   4815 #endif
   4816 
   4817 	KDASSERT((src & PGOFSET) == 0);
   4818 	KDASSERT((dst & PGOFSET) == 0);
   4819 
   4820 	/*
   4821 	 * Clean the source page.  Hold the source page's lock for
   4822 	 * the duration of the copy so that no other mappings can
   4823 	 * be created while we have a potentially aliased mapping.
   4824 	 */
   4825 #ifdef MULTIPROCESSOR
   4826 	KASSERT(uvm_page_locked_p(src_pg));
   4827 #endif
   4828 #ifdef PMAP_CACHE_VIVT
   4829 	(void) pmap_clean_page(SLIST_FIRST(&src_md->pvh_list), true);
   4830 #endif
   4831 
   4832 	/*
   4833 	 * Map the pages into the page hook points, copy them, and purge
   4834 	 * the cache for the appropriate page. Invalidate the TLB
   4835 	 * as required.
   4836 	 */
   4837 	*csrc_pte = L2_S_PROTO | src |
   4838 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   4839 	    L2_C | L2_XS_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4840 	PTE_SYNC(csrc_pte);
   4841 	*cdst_pte = L2_S_PROTO | dst |
   4842 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   4843 	    L2_C | L2_XS_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4844 	PTE_SYNC(cdst_pte);
   4845 	cpu_tlb_flushD_SE(csrcp);
   4846 	cpu_tlb_flushD_SE(cdstp);
   4847 	cpu_cpwait();
   4848 	bcopy_page(csrcp, cdstp);
   4849 	xscale_cache_clean_minidata();
   4850 }
   4851 #endif /* ARM_MMU_XSCALE == 1 */
   4852 
   4853 /*
   4854  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   4855  *
   4856  * Return the start and end addresses of the kernel's virtual space.
   4857  * These values are setup in pmap_bootstrap and are updated as pages
   4858  * are allocated.
   4859  */
   4860 void
   4861 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   4862 {
   4863 	*start = virtual_avail;
   4864 	*end = virtual_end;
   4865 }
   4866 
   4867 /*
   4868  * Helper function for pmap_grow_l2_bucket()
   4869  */
   4870 static inline int
   4871 pmap_grow_map(vaddr_t va, pt_entry_t cache_mode, paddr_t *pap)
   4872 {
   4873 	struct l2_bucket *l2b;
   4874 	pt_entry_t *ptep;
   4875 	paddr_t pa;
   4876 
   4877 	if (uvm.page_init_done == false) {
   4878 #ifdef PMAP_STEAL_MEMORY
   4879 		pv_addr_t pv;
   4880 		pmap_boot_pagealloc(PAGE_SIZE,
   4881 #ifdef PMAP_CACHE_VIPT
   4882 		    arm_cache_prefer_mask,
   4883 		    va & arm_cache_prefer_mask,
   4884 #else
   4885 		    0, 0,
   4886 #endif
   4887 		    &pv);
   4888 		pa = pv.pv_pa;
   4889 #else
   4890 		if (uvm_page_physget(&pa) == false)
   4891 			return (1);
   4892 #endif	/* PMAP_STEAL_MEMORY */
   4893 	} else {
   4894 		struct vm_page *pg;
   4895 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
   4896 		if (pg == NULL)
   4897 			return (1);
   4898 		pa = VM_PAGE_TO_PHYS(pg);
   4899 #ifdef PMAP_CACHE_VIPT
   4900 #ifdef DIAGNOSTIC
   4901 		struct vm_page_md *md = VM_PAGE_TO_MD(pg);
   4902 #endif
   4903 		/*
   4904 		 * This new page must not have any mappings.  Enter it via
   4905 		 * pmap_kenter_pa and let that routine do the hard work.
   4906 		 */
   4907 		KASSERT(SLIST_EMPTY(&md->pvh_list));
   4908 		pmap_kenter_pa(va, pa,
   4909 		    VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
   4910 #endif
   4911 	}
   4912 
   4913 	if (pap)
   4914 		*pap = pa;
   4915 
   4916 	PMAPCOUNT(pt_mappings);
   4917 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   4918 	KDASSERT(l2b != NULL);
   4919 
   4920 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   4921 	*ptep = L2_S_PROTO | pa | cache_mode |
   4922 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
   4923 	PTE_SYNC(ptep);
   4924 	memset((void *)va, 0, PAGE_SIZE);
   4925 	return (0);
   4926 }
   4927 
   4928 /*
   4929  * This is the same as pmap_alloc_l2_bucket(), except that it is only
   4930  * used by pmap_growkernel().
   4931  */
   4932 static inline struct l2_bucket *
   4933 pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
   4934 {
   4935 	struct l2_dtable *l2;
   4936 	struct l2_bucket *l2b;
   4937 	u_short l1idx;
   4938 	vaddr_t nva;
   4939 
   4940 	l1idx = L1_IDX(va);
   4941 
   4942 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   4943 		/*
   4944 		 * No mapping at this address, as there is
   4945 		 * no entry in the L1 table.
   4946 		 * Need to allocate a new l2_dtable.
   4947 		 */
   4948 		nva = pmap_kernel_l2dtable_kva;
   4949 		if ((nva & PGOFSET) == 0) {
   4950 			/*
   4951 			 * Need to allocate a backing page
   4952 			 */
   4953 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   4954 				return (NULL);
   4955 		}
   4956 
   4957 		l2 = (struct l2_dtable *)nva;
   4958 		nva += sizeof(struct l2_dtable);
   4959 
   4960 		if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
   4961 			/*
   4962 			 * The new l2_dtable straddles a page boundary.
   4963 			 * Map in another page to cover it.
   4964 			 */
   4965 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   4966 				return (NULL);
   4967 		}
   4968 
   4969 		pmap_kernel_l2dtable_kva = nva;
   4970 
   4971 		/*
   4972 		 * Link it into the parent pmap
   4973 		 */
   4974 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   4975 	}
   4976 
   4977 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   4978 
   4979 	/*
   4980 	 * Fetch pointer to the L2 page table associated with the address.
   4981 	 */
   4982 	if (l2b->l2b_kva == NULL) {
   4983 		pt_entry_t *ptep;
   4984 
   4985 		/*
   4986 		 * No L2 page table has been allocated. Chances are, this
   4987 		 * is because we just allocated the l2_dtable, above.
   4988 		 */
   4989 		nva = pmap_kernel_l2ptp_kva;
   4990 		ptep = (pt_entry_t *)nva;
   4991 		if ((nva & PGOFSET) == 0) {
   4992 			/*
   4993 			 * Need to allocate a backing page
   4994 			 */
   4995 			if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
   4996 			    &pmap_kernel_l2ptp_phys))
   4997 				return (NULL);
   4998 			PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
   4999 		}
   5000 
   5001 		l2->l2_occupancy++;
   5002 		l2b->l2b_kva = ptep;
   5003 		l2b->l2b_l1idx = l1idx;
   5004 		l2b->l2b_phys = pmap_kernel_l2ptp_phys;
   5005 
   5006 		pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
   5007 		pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
   5008 	}
   5009 
   5010 	return (l2b);
   5011 }
   5012 
   5013 vaddr_t
   5014 pmap_growkernel(vaddr_t maxkvaddr)
   5015 {
   5016 	pmap_t kpm = pmap_kernel();
   5017 	struct l1_ttable *l1;
   5018 	struct l2_bucket *l2b;
   5019 	pd_entry_t *pl1pd;
   5020 	int s;
   5021 
   5022 	if (maxkvaddr <= pmap_curmaxkvaddr)
   5023 		goto out;		/* we are OK */
   5024 
   5025 	NPDEBUG(PDB_GROWKERN,
   5026 	    printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
   5027 	    pmap_curmaxkvaddr, maxkvaddr));
   5028 
   5029 	KDASSERT(maxkvaddr <= virtual_end);
   5030 
   5031 	/*
   5032 	 * whoops!   we need to add kernel PTPs
   5033 	 */
   5034 
   5035 	s = splhigh();	/* to be safe */
   5036 	mutex_enter(kpm->pm_lock);
   5037 
   5038 	/* Map 1MB at a time */
   5039 	for (; pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE) {
   5040 
   5041 		l2b = pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
   5042 		KDASSERT(l2b != NULL);
   5043 
   5044 		/* Distribute new L1 entry to all other L1s */
   5045 		SLIST_FOREACH(l1, &l1_list, l1_link) {
   5046 			pl1pd = &l1->l1_kva[L1_IDX(pmap_curmaxkvaddr)];
   5047 			*pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
   5048 			    L1_C_PROTO;
   5049 			PTE_SYNC(pl1pd);
   5050 		}
   5051 	}
   5052 
   5053 	/*
   5054 	 * flush out the cache, expensive but growkernel will happen so
   5055 	 * rarely
   5056 	 */
   5057 	cpu_dcache_wbinv_all();
   5058 	cpu_tlb_flushD();
   5059 	cpu_cpwait();
   5060 
   5061 	mutex_exit(kpm->pm_lock);
   5062 	splx(s);
   5063 
   5064 out:
   5065 	return (pmap_curmaxkvaddr);
   5066 }
   5067 
   5068 /************************ Utility routines ****************************/
   5069 
   5070 /*
   5071  * vector_page_setprot:
   5072  *
   5073  *	Manipulate the protection of the vector page.
   5074  */
   5075 void
   5076 vector_page_setprot(int prot)
   5077 {
   5078 	struct l2_bucket *l2b;
   5079 	pt_entry_t *ptep;
   5080 
   5081 	l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
   5082 	KDASSERT(l2b != NULL);
   5083 
   5084 	ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
   5085 
   5086 	*ptep = (*ptep & ~L2_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
   5087 	PTE_SYNC(ptep);
   5088 	cpu_tlb_flushD_SE(vector_page);
   5089 	cpu_cpwait();
   5090 }
   5091 
   5092 /*
   5093  * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
   5094  * Returns true if the mapping exists, else false.
   5095  *
   5096  * NOTE: This function is only used by a couple of arm-specific modules.
   5097  * It is not safe to take any pmap locks here, since we could be right
   5098  * in the middle of debugging the pmap anyway...
   5099  *
   5100  * It is possible for this routine to return false even though a valid
   5101  * mapping does exist. This is because we don't lock, so the metadata
   5102  * state may be inconsistent.
   5103  *
   5104  * NOTE: We can return a NULL *ptp in the case where the L1 pde is
   5105  * a "section" mapping.
   5106  */
   5107 bool
   5108 pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
   5109 {
   5110 	struct l2_dtable *l2;
   5111 	pd_entry_t *pl1pd, l1pd;
   5112 	pt_entry_t *ptep;
   5113 	u_short l1idx;
   5114 
   5115 	if (pm->pm_l1 == NULL)
   5116 		return false;
   5117 
   5118 	l1idx = L1_IDX(va);
   5119 	*pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
   5120 	l1pd = *pl1pd;
   5121 
   5122 	if (l1pte_section_p(l1pd)) {
   5123 		*ptp = NULL;
   5124 		return true;
   5125 	}
   5126 
   5127 	if (pm->pm_l2 == NULL)
   5128 		return false;
   5129 
   5130 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   5131 
   5132 	if (l2 == NULL ||
   5133 	    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   5134 		return false;
   5135 	}
   5136 
   5137 	*ptp = &ptep[l2pte_index(va)];
   5138 	return true;
   5139 }
   5140 
   5141 bool
   5142 pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
   5143 {
   5144 	u_short l1idx;
   5145 
   5146 	if (pm->pm_l1 == NULL)
   5147 		return false;
   5148 
   5149 	l1idx = L1_IDX(va);
   5150 	*pdp = &pm->pm_l1->l1_kva[l1idx];
   5151 
   5152 	return true;
   5153 }
   5154 
   5155 /************************ Bootstrapping routines ****************************/
   5156 
   5157 static void
   5158 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
   5159 {
   5160 	int i;
   5161 
   5162 	l1->l1_kva = l1pt;
   5163 	l1->l1_domain_use_count = 0;
   5164 	l1->l1_domain_first = 0;
   5165 
   5166 	for (i = 0; i < PMAP_DOMAINS; i++)
   5167 		l1->l1_domain_free[i] = i + 1;
   5168 
   5169 	/*
   5170 	 * Copy the kernel's L1 entries to each new L1.
   5171 	 */
   5172 	if (pmap_initialized)
   5173 		memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
   5174 
   5175 	if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
   5176 	    &l1->l1_physaddr) == false)
   5177 		panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
   5178 
   5179 	SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
   5180 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   5181 }
   5182 
   5183 /*
   5184  * pmap_bootstrap() is called from the board-specific initarm() routine
   5185  * once the kernel L1/L2 descriptors tables have been set up.
   5186  *
   5187  * This is a somewhat convoluted process since pmap bootstrap is, effectively,
   5188  * spread over a number of disparate files/functions.
   5189  *
   5190  * We are passed the following parameters
   5191  *  - kernel_l1pt
   5192  *    This is a pointer to the base of the kernel's L1 translation table.
   5193  *  - vstart
   5194  *    1MB-aligned start of managed kernel virtual memory.
   5195  *  - vend
   5196  *    1MB-aligned end of managed kernel virtual memory.
   5197  *
   5198  * We use the first parameter to build the metadata (struct l1_ttable and
   5199  * struct l2_dtable) necessary to track kernel mappings.
   5200  */
   5201 #define	PMAP_STATIC_L2_SIZE 16
   5202 void
   5203 pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
   5204 {
   5205 	static struct l1_ttable static_l1;
   5206 	static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
   5207 	struct l1_ttable *l1 = &static_l1;
   5208 	struct l2_dtable *l2;
   5209 	struct l2_bucket *l2b;
   5210 	pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
   5211 	pmap_t pm = pmap_kernel();
   5212 	pd_entry_t pde;
   5213 	pt_entry_t *ptep;
   5214 	paddr_t pa;
   5215 	vaddr_t va;
   5216 	vsize_t size;
   5217 	int nptes, l1idx, l2idx, l2next = 0;
   5218 
   5219 	/*
   5220 	 * Initialise the kernel pmap object
   5221 	 */
   5222 	pm->pm_l1 = l1;
   5223 	pm->pm_domain = PMAP_DOMAIN_KERNEL;
   5224 	pm->pm_activated = true;
   5225 	pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   5226 
   5227 	mutex_init(&pm->pm_obj_lock, MUTEX_DEFAULT, IPL_NONE);
   5228 	uvm_obj_init(&pm->pm_obj, NULL, false, 1);
   5229 	uvm_obj_setlock(&pm->pm_obj, &pm->pm_obj_lock);
   5230 
   5231 	/*
   5232 	 * Scan the L1 translation table created by initarm() and create
   5233 	 * the required metadata for all valid mappings found in it.
   5234 	 */
   5235 	for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
   5236 		pde = l1pt[l1idx];
   5237 
   5238 		/*
   5239 		 * We're only interested in Coarse mappings.
   5240 		 * pmap_extract() can deal with section mappings without
   5241 		 * recourse to checking L2 metadata.
   5242 		 */
   5243 		if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
   5244 			continue;
   5245 
   5246 		/*
   5247 		 * Lookup the KVA of this L2 descriptor table
   5248 		 */
   5249 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   5250 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   5251 		if (ptep == NULL) {
   5252 			panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
   5253 			    (u_int)l1idx << L1_S_SHIFT, pa);
   5254 		}
   5255 
   5256 		/*
   5257 		 * Fetch the associated L2 metadata structure.
   5258 		 * Allocate a new one if necessary.
   5259 		 */
   5260 		if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   5261 			if (l2next == PMAP_STATIC_L2_SIZE)
   5262 				panic("pmap_bootstrap: out of static L2s");
   5263 			pm->pm_l2[L2_IDX(l1idx)] = l2 = &static_l2[l2next++];
   5264 		}
   5265 
   5266 		/*
   5267 		 * One more L1 slot tracked...
   5268 		 */
   5269 		l2->l2_occupancy++;
   5270 
   5271 		/*
   5272 		 * Fill in the details of the L2 descriptor in the
   5273 		 * appropriate bucket.
   5274 		 */
   5275 		l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   5276 		l2b->l2b_kva = ptep;
   5277 		l2b->l2b_phys = pa;
   5278 		l2b->l2b_l1idx = l1idx;
   5279 
   5280 		/*
   5281 		 * Establish an initial occupancy count for this descriptor
   5282 		 */
   5283 		for (l2idx = 0;
   5284 		    l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   5285 		    l2idx++) {
   5286 			if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
   5287 				l2b->l2b_occupancy++;
   5288 			}
   5289 		}
   5290 
   5291 		/*
   5292 		 * Make sure the descriptor itself has the correct cache mode.
   5293 		 * If not, fix it, but whine about the problem. Port-meisters
   5294 		 * should consider this a clue to fix up their initarm()
   5295 		 * function. :)
   5296 		 */
   5297 		if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep)) {
   5298 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   5299 			    "L2 pte @ %p\n", ptep);
   5300 		}
   5301 	}
   5302 
   5303 	/*
   5304 	 * Ensure the primary (kernel) L1 has the correct cache mode for
   5305 	 * a page table. Bitch if it is not correctly set.
   5306 	 */
   5307 	for (va = (vaddr_t)l1pt;
   5308 	    va < ((vaddr_t)l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
   5309 		if (pmap_set_pt_cache_mode(l1pt, va))
   5310 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   5311 			    "primary L1 @ 0x%lx\n", va);
   5312 	}
   5313 
   5314 	cpu_dcache_wbinv_all();
   5315 	cpu_tlb_flushID();
   5316 	cpu_cpwait();
   5317 
   5318 	/*
   5319 	 * now we allocate the "special" VAs which are used for tmp mappings
   5320 	 * by the pmap (and other modules).  we allocate the VAs by advancing
   5321 	 * virtual_avail (note that there are no pages mapped at these VAs).
   5322 	 *
   5323 	 * Managed KVM space start from wherever initarm() tells us.
   5324 	 */
   5325 	virtual_avail = vstart;
   5326 	virtual_end = vend;
   5327 
   5328 #ifdef PMAP_CACHE_VIPT
   5329 	/*
   5330 	 * If we have a VIPT cache, we need one page/pte per possible alias
   5331 	 * page so we won't violate cache aliasing rules.
   5332 	 */
   5333 	virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
   5334 	nptes = (arm_cache_prefer_mask >> PGSHIFT) + 1;
   5335 #else
   5336 	nptes = 1;
   5337 #endif
   5338 	pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
   5339 	pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte);
   5340 	pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
   5341 	pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte);
   5342 	pmap_alloc_specials(&virtual_avail, nptes, &memhook, NULL);
   5343 	pmap_alloc_specials(&virtual_avail, round_page(MSGBUFSIZE) / PAGE_SIZE,
   5344 	    (void *)&msgbufaddr, NULL);
   5345 
   5346 	/*
   5347 	 * Allocate a range of kernel virtual address space to be used
   5348 	 * for L2 descriptor tables and metadata allocation in
   5349 	 * pmap_growkernel().
   5350 	 */
   5351 	size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
   5352 	pmap_alloc_specials(&virtual_avail,
   5353 	    round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
   5354 	    &pmap_kernel_l2ptp_kva, NULL);
   5355 
   5356 	size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
   5357 	pmap_alloc_specials(&virtual_avail,
   5358 	    round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
   5359 	    &pmap_kernel_l2dtable_kva, NULL);
   5360 
   5361 	/*
   5362 	 * init the static-global locks and global pmap list.
   5363 	 */
   5364 	mutex_init(&l1_lru_lock, MUTEX_DEFAULT, IPL_VM);
   5365 
   5366 	/*
   5367 	 * We can now initialise the first L1's metadata.
   5368 	 */
   5369 	SLIST_INIT(&l1_list);
   5370 	TAILQ_INIT(&l1_lru_list);
   5371 	pmap_init_l1(l1, l1pt);
   5372 
   5373 	/* Set up vector page L1 details, if necessary */
   5374 	if (vector_page < KERNEL_BASE) {
   5375 		pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
   5376 		l2b = pmap_get_l2_bucket(pm, vector_page);
   5377 		KDASSERT(l2b != NULL);
   5378 		pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
   5379 		    L1_C_DOM(pm->pm_domain);
   5380 	} else
   5381 		pm->pm_pl1vec = NULL;
   5382 
   5383 	/*
   5384 	 * Initialize the pmap cache
   5385 	 */
   5386 	pool_cache_bootstrap(&pmap_cache, sizeof(struct pmap), 0, 0, 0,
   5387 	    "pmappl", NULL, IPL_NONE, pmap_pmap_ctor, NULL, NULL);
   5388 	LIST_INIT(&pmap_pmaps);
   5389 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   5390 
   5391 	/*
   5392 	 * Initialize the pv pool.
   5393 	 */
   5394 	pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
   5395 	    &pmap_bootstrap_pv_allocator, IPL_NONE);
   5396 
   5397 	/*
   5398 	 * Initialize the L2 dtable pool and cache.
   5399 	 */
   5400 	pool_cache_bootstrap(&pmap_l2dtable_cache, sizeof(struct l2_dtable), 0,
   5401 	    0, 0, "l2dtblpl", NULL, IPL_NONE, pmap_l2dtable_ctor, NULL, NULL);
   5402 
   5403 	/*
   5404 	 * Initialise the L2 descriptor table pool and cache
   5405 	 */
   5406 	pool_cache_bootstrap(&pmap_l2ptp_cache, L2_TABLE_SIZE_REAL, 0,
   5407 	    L2_TABLE_SIZE_REAL, 0, "l2ptppl", NULL, IPL_NONE,
   5408 	    pmap_l2ptp_ctor, NULL, NULL);
   5409 
   5410 	cpu_dcache_wbinv_all();
   5411 }
   5412 
   5413 static int
   5414 pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va)
   5415 {
   5416 	pd_entry_t *pdep, pde;
   5417 	pt_entry_t *ptep, pte;
   5418 	vaddr_t pa;
   5419 	int rv = 0;
   5420 
   5421 	/*
   5422 	 * Make sure the descriptor itself has the correct cache mode
   5423 	 */
   5424 	pdep = &kl1[L1_IDX(va)];
   5425 	pde = *pdep;
   5426 
   5427 	if (l1pte_section_p(pde)) {
   5428 		__CTASSERT((L1_S_CACHE_MASK & L1_S_V6_SUPER) == 0);
   5429 		if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
   5430 			*pdep = (pde & ~L1_S_CACHE_MASK) |
   5431 			    pte_l1_s_cache_mode_pt;
   5432 			PTE_SYNC(pdep);
   5433 			cpu_dcache_wbinv_range((vaddr_t)pdep, sizeof(*pdep));
   5434 			rv = 1;
   5435 		}
   5436 	} else {
   5437 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   5438 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   5439 		if (ptep == NULL)
   5440 			panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
   5441 
   5442 		ptep = &ptep[l2pte_index(va)];
   5443 		pte = *ptep;
   5444 		if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   5445 			*ptep = (pte & ~L2_S_CACHE_MASK) |
   5446 			    pte_l2_s_cache_mode_pt;
   5447 			PTE_SYNC(ptep);
   5448 			cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
   5449 			rv = 1;
   5450 		}
   5451 	}
   5452 
   5453 	return (rv);
   5454 }
   5455 
   5456 static void
   5457 pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
   5458 {
   5459 	vaddr_t va = *availp;
   5460 	struct l2_bucket *l2b;
   5461 
   5462 	if (ptep) {
   5463 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   5464 		if (l2b == NULL)
   5465 			panic("pmap_alloc_specials: no l2b for 0x%lx", va);
   5466 
   5467 		if (ptep)
   5468 			*ptep = &l2b->l2b_kva[l2pte_index(va)];
   5469 	}
   5470 
   5471 	*vap = va;
   5472 	*availp = va + (PAGE_SIZE * pages);
   5473 }
   5474 
   5475 void
   5476 pmap_init(void)
   5477 {
   5478 
   5479 	/*
   5480 	 * Set the available memory vars - These do not map to real memory
   5481 	 * addresses and cannot as the physical memory is fragmented.
   5482 	 * They are used by ps for %mem calculations.
   5483 	 * One could argue whether this should be the entire memory or just
   5484 	 * the memory that is useable in a user process.
   5485 	 */
   5486 	avail_start = ptoa(VM_PHYSMEM_PTR(0)->start);
   5487 	avail_end = ptoa(VM_PHYSMEM_PTR(vm_nphysseg - 1)->end);
   5488 
   5489 	/*
   5490 	 * Now we need to free enough pv_entry structures to allow us to get
   5491 	 * the kmem_map/kmem_object allocated and inited (done after this
   5492 	 * function is finished).  to do this we allocate one bootstrap page out
   5493 	 * of kernel_map and use it to provide an initial pool of pv_entry
   5494 	 * structures.   we never free this page.
   5495 	 */
   5496 	pool_setlowat(&pmap_pv_pool,
   5497 	    (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
   5498 
   5499 	mutex_init(&memlock, MUTEX_DEFAULT, IPL_NONE);
   5500 	zeropage = (void *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   5501 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
   5502 
   5503 	pmap_initialized = true;
   5504 }
   5505 
   5506 static vaddr_t last_bootstrap_page = 0;
   5507 static void *free_bootstrap_pages = NULL;
   5508 
   5509 static void *
   5510 pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
   5511 {
   5512 	extern void *pool_page_alloc(struct pool *, int);
   5513 	vaddr_t new_page;
   5514 	void *rv;
   5515 
   5516 	if (pmap_initialized)
   5517 		return (pool_page_alloc(pp, flags));
   5518 
   5519 	if (free_bootstrap_pages) {
   5520 		rv = free_bootstrap_pages;
   5521 		free_bootstrap_pages = *((void **)rv);
   5522 		return (rv);
   5523 	}
   5524 
   5525 	new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   5526 	    UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
   5527 
   5528 	KASSERT(new_page > last_bootstrap_page);
   5529 	last_bootstrap_page = new_page;
   5530 	return ((void *)new_page);
   5531 }
   5532 
   5533 static void
   5534 pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
   5535 {
   5536 	extern void pool_page_free(struct pool *, void *);
   5537 
   5538 	if ((vaddr_t)v <= last_bootstrap_page) {
   5539 		*((void **)v) = free_bootstrap_pages;
   5540 		free_bootstrap_pages = v;
   5541 		return;
   5542 	}
   5543 
   5544 	if (pmap_initialized) {
   5545 		pool_page_free(pp, v);
   5546 		return;
   5547 	}
   5548 }
   5549 
   5550 /*
   5551  * pmap_postinit()
   5552  *
   5553  * This routine is called after the vm and kmem subsystems have been
   5554  * initialised. This allows the pmap code to perform any initialisation
   5555  * that can only be done one the memory allocation is in place.
   5556  */
   5557 void
   5558 pmap_postinit(void)
   5559 {
   5560 	extern paddr_t physical_start, physical_end;
   5561 	struct l2_bucket *l2b;
   5562 	struct l1_ttable *l1;
   5563 	struct pglist plist;
   5564 	struct vm_page *m;
   5565 	pd_entry_t *pl1pt;
   5566 	pt_entry_t *ptep, pte;
   5567 	vaddr_t va, eva;
   5568 	u_int loop, needed;
   5569 	int error;
   5570 
   5571 	pool_cache_setlowat(&pmap_l2ptp_cache,
   5572 	    (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
   5573 	pool_cache_setlowat(&pmap_l2dtable_cache,
   5574 	    (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
   5575 
   5576 	needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
   5577 	needed -= 1;
   5578 
   5579 	l1 = kmem_alloc(sizeof(*l1) * needed, KM_SLEEP);
   5580 
   5581 	for (loop = 0; loop < needed; loop++, l1++) {
   5582 		/* Allocate a L1 page table */
   5583 		va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
   5584 		if (va == 0)
   5585 			panic("Cannot allocate L1 KVM");
   5586 
   5587 		error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
   5588 		    physical_end, L1_TABLE_SIZE, 0, &plist, 1, 1);
   5589 		if (error)
   5590 			panic("Cannot allocate L1 physical pages");
   5591 
   5592 		m = TAILQ_FIRST(&plist);
   5593 		eva = va + L1_TABLE_SIZE;
   5594 		pl1pt = (pd_entry_t *)va;
   5595 
   5596 		while (m && va < eva) {
   5597 			paddr_t pa = VM_PAGE_TO_PHYS(m);
   5598 
   5599 			pmap_kenter_pa(va, pa,
   5600 			    VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
   5601 
   5602 			/*
   5603 			 * Make sure the L1 descriptor table is mapped
   5604 			 * with the cache-mode set to write-through.
   5605 			 */
   5606 			l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   5607 			KDASSERT(l2b != NULL);
   5608 			ptep = &l2b->l2b_kva[l2pte_index(va)];
   5609 			pte = *ptep;
   5610 			pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   5611 			*ptep = pte;
   5612 			PTE_SYNC(ptep);
   5613 			cpu_tlb_flushD_SE(va);
   5614 
   5615 			va += PAGE_SIZE;
   5616 			m = TAILQ_NEXT(m, pageq.queue);
   5617 		}
   5618 
   5619 #ifdef DIAGNOSTIC
   5620 		if (m)
   5621 			panic("pmap_alloc_l1pt: pglist not empty");
   5622 #endif	/* DIAGNOSTIC */
   5623 
   5624 		pmap_init_l1(l1, pl1pt);
   5625 	}
   5626 
   5627 #ifdef DEBUG
   5628 	printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
   5629 	    needed);
   5630 #endif
   5631 }
   5632 
   5633 /*
   5634  * Note that the following routines are used by board-specific initialisation
   5635  * code to configure the initial kernel page tables.
   5636  *
   5637  * If ARM32_NEW_VM_LAYOUT is *not* defined, they operate on the assumption that
   5638  * L2 page-table pages are 4KB in size and use 4 L1 slots. This mimics the
   5639  * behaviour of the old pmap, and provides an easy migration path for
   5640  * initial bring-up of the new pmap on existing ports. Fortunately,
   5641  * pmap_bootstrap() compensates for this hackery. This is only a stop-gap and
   5642  * will be deprecated.
   5643  *
   5644  * If ARM32_NEW_VM_LAYOUT *is* defined, these functions deal with 1KB L2 page
   5645  * tables.
   5646  */
   5647 
   5648 /*
   5649  * This list exists for the benefit of pmap_map_chunk().  It keeps track
   5650  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
   5651  * find them as necessary.
   5652  *
   5653  * Note that the data on this list MUST remain valid after initarm() returns,
   5654  * as pmap_bootstrap() uses it to contruct L2 table metadata.
   5655  */
   5656 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
   5657 
   5658 static vaddr_t
   5659 kernel_pt_lookup(paddr_t pa)
   5660 {
   5661 	pv_addr_t *pv;
   5662 
   5663 	SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
   5664 #ifndef ARM32_NEW_VM_LAYOUT
   5665 		if (pv->pv_pa == (pa & ~PGOFSET))
   5666 			return (pv->pv_va | (pa & PGOFSET));
   5667 #else
   5668 		if (pv->pv_pa == pa)
   5669 			return (pv->pv_va);
   5670 #endif
   5671 	}
   5672 	return (0);
   5673 }
   5674 
   5675 /*
   5676  * pmap_map_section:
   5677  *
   5678  *	Create a single section mapping.
   5679  */
   5680 void
   5681 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   5682 {
   5683 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5684 	pd_entry_t fl;
   5685 
   5686 	KASSERT(((va | pa) & L1_S_OFFSET) == 0);
   5687 
   5688 	switch (cache) {
   5689 	case PTE_NOCACHE:
   5690 	default:
   5691 		fl = 0;
   5692 		break;
   5693 
   5694 	case PTE_CACHE:
   5695 		fl = pte_l1_s_cache_mode;
   5696 		break;
   5697 
   5698 	case PTE_PAGETABLE:
   5699 		fl = pte_l1_s_cache_mode_pt;
   5700 		break;
   5701 	}
   5702 
   5703 	pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   5704 	    L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
   5705 	PTE_SYNC(&pde[va >> L1_S_SHIFT]);
   5706 }
   5707 
   5708 /*
   5709  * pmap_map_entry:
   5710  *
   5711  *	Create a single page mapping.
   5712  */
   5713 void
   5714 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   5715 {
   5716 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5717 	pt_entry_t fl;
   5718 	pt_entry_t *pte;
   5719 
   5720 	KASSERT(((va | pa) & PGOFSET) == 0);
   5721 
   5722 	switch (cache) {
   5723 	case PTE_NOCACHE:
   5724 	default:
   5725 		fl = 0;
   5726 		break;
   5727 
   5728 	case PTE_CACHE:
   5729 		fl = pte_l2_s_cache_mode;
   5730 		break;
   5731 
   5732 	case PTE_PAGETABLE:
   5733 		fl = pte_l2_s_cache_mode_pt;
   5734 		break;
   5735 	}
   5736 
   5737 	if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   5738 		panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
   5739 
   5740 #ifndef ARM32_NEW_VM_LAYOUT
   5741 	pte = (pt_entry_t *)
   5742 	    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   5743 #else
   5744 	pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   5745 #endif
   5746 	if (pte == NULL)
   5747 		panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
   5748 
   5749 	fl |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
   5750 #ifndef ARM32_NEW_VM_LAYOUT
   5751 	pte += (va >> PGSHIFT) & 0x3ff;
   5752 #else
   5753 	pte += l2pte_index(va);
   5754 	    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
   5755 #endif
   5756 	*pte = fl;
   5757 	PTE_SYNC(pte);
   5758 }
   5759 
   5760 /*
   5761  * pmap_link_l2pt:
   5762  *
   5763  *	Link the L2 page table specified by "l2pv" into the L1
   5764  *	page table at the slot for "va".
   5765  */
   5766 void
   5767 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
   5768 {
   5769 	pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
   5770 	u_int slot = va >> L1_S_SHIFT;
   5771 
   5772 #ifndef ARM32_NEW_VM_LAYOUT
   5773 	KASSERT((va & ((L1_S_SIZE * 4) - 1)) == 0);
   5774 	KASSERT((l2pv->pv_pa & PGOFSET) == 0);
   5775 #endif
   5776 
   5777 	proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
   5778 
   5779 	pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
   5780 #ifdef ARM32_NEW_VM_LAYOUT
   5781 	PTE_SYNC(&pde[slot]);
   5782 #else
   5783 	pde[slot + 1] = proto | (l2pv->pv_pa + 0x400);
   5784 	pde[slot + 2] = proto | (l2pv->pv_pa + 0x800);
   5785 	pde[slot + 3] = proto | (l2pv->pv_pa + 0xc00);
   5786 	PTE_SYNC_RANGE(&pde[slot + 0], 4);
   5787 #endif
   5788 
   5789 	SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
   5790 }
   5791 
   5792 /*
   5793  * pmap_map_chunk:
   5794  *
   5795  *	Map a chunk of memory using the most efficient mappings
   5796  *	possible (section, large page, small page) into the
   5797  *	provided L1 and L2 tables at the specified virtual address.
   5798  */
   5799 vsize_t
   5800 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
   5801     int prot, int cache)
   5802 {
   5803 	pd_entry_t *pdep = (pd_entry_t *) l1pt;
   5804 	pt_entry_t *pte, f1, f2s, f2l;
   5805 	vsize_t resid;
   5806 	int i;
   5807 
   5808 	resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
   5809 
   5810 	if (l1pt == 0)
   5811 		panic("pmap_map_chunk: no L1 table provided");
   5812 
   5813 #ifdef VERBOSE_INIT_ARM
   5814 	printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
   5815 	    "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
   5816 #endif
   5817 
   5818 	switch (cache) {
   5819 	case PTE_NOCACHE:
   5820 	default:
   5821 		f1 = 0;
   5822 		f2l = 0;
   5823 		f2s = 0;
   5824 		break;
   5825 
   5826 	case PTE_CACHE:
   5827 		f1 = pte_l1_s_cache_mode;
   5828 		f2l = pte_l2_l_cache_mode;
   5829 		f2s = pte_l2_s_cache_mode;
   5830 		break;
   5831 
   5832 	case PTE_PAGETABLE:
   5833 		f1 = pte_l1_s_cache_mode_pt;
   5834 		f2l = pte_l2_l_cache_mode_pt;
   5835 		f2s = pte_l2_s_cache_mode_pt;
   5836 		break;
   5837 	}
   5838 
   5839 	size = resid;
   5840 
   5841 	while (resid > 0) {
   5842 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   5843 		/* See if we can use a supersection mapping. */
   5844 		if (L1_SS_PROTO && L1_SS_MAPPABLE_P(va, pa, resid)) {
   5845 			/* Supersection are always domain 0 */
   5846 			pd_entry_t pde = L1_SS_PROTO | pa |
   5847 			    L1_S_PROT(PTE_KERNEL, prot) | f1;
   5848 #ifdef VERBOSE_INIT_ARM
   5849 			printf("sS");
   5850 #endif
   5851 			for (size_t s = va >> L1_S_SHIFT,
   5852 			     e = s + L1_SS_SIZE / L1_S_SIZE;
   5853 			     s < e;
   5854 			     s++) {
   5855 				pdep[s] = pde;
   5856 				PTE_SYNC(&pdep[s]);
   5857 			}
   5858 			va += L1_SS_SIZE;
   5859 			pa += L1_SS_SIZE;
   5860 			resid -= L1_SS_SIZE;
   5861 			continue;
   5862 		}
   5863 #endif
   5864 		/* See if we can use a section mapping. */
   5865 		if (L1_S_MAPPABLE_P(va, pa, resid)) {
   5866 #ifdef VERBOSE_INIT_ARM
   5867 			printf("S");
   5868 #endif
   5869 			pdep[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   5870 			    L1_S_PROT(PTE_KERNEL, prot) | f1 |
   5871 			    L1_S_DOM(PMAP_DOMAIN_KERNEL);
   5872 			PTE_SYNC(&pdep[va >> L1_S_SHIFT]);
   5873 			va += L1_S_SIZE;
   5874 			pa += L1_S_SIZE;
   5875 			resid -= L1_S_SIZE;
   5876 			continue;
   5877 		}
   5878 
   5879 		/*
   5880 		 * Ok, we're going to use an L2 table.  Make sure
   5881 		 * one is actually in the corresponding L1 slot
   5882 		 * for the current VA.
   5883 		 */
   5884 		if ((pdep[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   5885 			panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
   5886 
   5887 #ifndef ARM32_NEW_VM_LAYOUT
   5888 		pte = (pt_entry_t *)
   5889 		    kernel_pt_lookup(pdep[va >> L1_S_SHIFT] & L2_S_FRAME);
   5890 #else
   5891 		pte = (pt_entry_t *) kernel_pt_lookup(
   5892 		    pdep[L1_IDX(va)] & L1_C_ADDR_MASK);
   5893 #endif
   5894 		if (pte == NULL)
   5895 			panic("pmap_map_chunk: can't find L2 table for VA"
   5896 			    "0x%08lx", va);
   5897 
   5898 		/* See if we can use a L2 large page mapping. */
   5899 		if (L2_L_MAPPABLE_P(va, pa, resid)) {
   5900 #ifdef VERBOSE_INIT_ARM
   5901 			printf("L");
   5902 #endif
   5903 			for (i = 0; i < 16; i++) {
   5904 #ifndef ARM32_NEW_VM_LAYOUT
   5905 				pte[((va >> PGSHIFT) & 0x3f0) + i] =
   5906 				    L2_L_PROTO | pa |
   5907 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   5908 				PTE_SYNC(&pte[((va >> PGSHIFT) & 0x3f0) + i]);
   5909 #else
   5910 				pte[l2pte_index(va) + i] =
   5911 				    L2_L_PROTO | pa |
   5912 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   5913 				PTE_SYNC(&pte[l2pte_index(va) + i]);
   5914 #endif
   5915 			}
   5916 			va += L2_L_SIZE;
   5917 			pa += L2_L_SIZE;
   5918 			resid -= L2_L_SIZE;
   5919 			continue;
   5920 		}
   5921 
   5922 		/* Use a small page mapping. */
   5923 #ifdef VERBOSE_INIT_ARM
   5924 		printf("P");
   5925 #endif
   5926 #ifndef ARM32_NEW_VM_LAYOUT
   5927 		pte[(va >> PGSHIFT) & 0x3ff] =
   5928 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   5929 		PTE_SYNC(&pte[(va >> PGSHIFT) & 0x3ff]);
   5930 #else
   5931 		pte[l2pte_index(va)] =
   5932 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   5933 		PTE_SYNC(&pte[l2pte_index(va)]);
   5934 #endif
   5935 		va += PAGE_SIZE;
   5936 		pa += PAGE_SIZE;
   5937 		resid -= PAGE_SIZE;
   5938 	}
   5939 #ifdef VERBOSE_INIT_ARM
   5940 	printf("\n");
   5941 #endif
   5942 	return (size);
   5943 }
   5944 
   5945 /********************** Static device map routines ***************************/
   5946 
   5947 static const struct pmap_devmap *pmap_devmap_table;
   5948 
   5949 /*
   5950  * Register the devmap table.  This is provided in case early console
   5951  * initialization needs to register mappings created by bootstrap code
   5952  * before pmap_devmap_bootstrap() is called.
   5953  */
   5954 void
   5955 pmap_devmap_register(const struct pmap_devmap *table)
   5956 {
   5957 
   5958 	pmap_devmap_table = table;
   5959 }
   5960 
   5961 /*
   5962  * Map all of the static regions in the devmap table, and remember
   5963  * the devmap table so other parts of the kernel can look up entries
   5964  * later.
   5965  */
   5966 void
   5967 pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
   5968 {
   5969 	int i;
   5970 
   5971 	pmap_devmap_table = table;
   5972 
   5973 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   5974 #ifdef VERBOSE_INIT_ARM
   5975 		printf("devmap: %08lx -> %08lx @ %08lx\n",
   5976 		    pmap_devmap_table[i].pd_pa,
   5977 		    pmap_devmap_table[i].pd_pa +
   5978 			pmap_devmap_table[i].pd_size - 1,
   5979 		    pmap_devmap_table[i].pd_va);
   5980 #endif
   5981 		pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
   5982 		    pmap_devmap_table[i].pd_pa,
   5983 		    pmap_devmap_table[i].pd_size,
   5984 		    pmap_devmap_table[i].pd_prot,
   5985 		    pmap_devmap_table[i].pd_cache);
   5986 	}
   5987 }
   5988 
   5989 const struct pmap_devmap *
   5990 pmap_devmap_find_pa(paddr_t pa, psize_t size)
   5991 {
   5992 	uint64_t endpa;
   5993 	int i;
   5994 
   5995 	if (pmap_devmap_table == NULL)
   5996 		return (NULL);
   5997 
   5998 	endpa = (uint64_t)pa + (uint64_t)(size - 1);
   5999 
   6000 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   6001 		if (pa >= pmap_devmap_table[i].pd_pa &&
   6002 		    endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
   6003 			     (uint64_t)(pmap_devmap_table[i].pd_size - 1))
   6004 			return (&pmap_devmap_table[i]);
   6005 	}
   6006 
   6007 	return (NULL);
   6008 }
   6009 
   6010 const struct pmap_devmap *
   6011 pmap_devmap_find_va(vaddr_t va, vsize_t size)
   6012 {
   6013 	int i;
   6014 
   6015 	if (pmap_devmap_table == NULL)
   6016 		return (NULL);
   6017 
   6018 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   6019 		if (va >= pmap_devmap_table[i].pd_va &&
   6020 		    va + size - 1 <= pmap_devmap_table[i].pd_va +
   6021 				     pmap_devmap_table[i].pd_size - 1)
   6022 			return (&pmap_devmap_table[i]);
   6023 	}
   6024 
   6025 	return (NULL);
   6026 }
   6027 
   6028 /********************** PTE initialization routines **************************/
   6029 
   6030 /*
   6031  * These routines are called when the CPU type is identified to set up
   6032  * the PTE prototypes, cache modes, etc.
   6033  *
   6034  * The variables are always here, just in case modules need to reference
   6035  * them (though, they shouldn't).
   6036  */
   6037 
   6038 pt_entry_t	pte_l1_s_cache_mode;
   6039 pt_entry_t	pte_l1_s_wc_mode;
   6040 pt_entry_t	pte_l1_s_cache_mode_pt;
   6041 pt_entry_t	pte_l1_s_cache_mask;
   6042 
   6043 pt_entry_t	pte_l2_l_cache_mode;
   6044 pt_entry_t	pte_l2_l_wc_mode;
   6045 pt_entry_t	pte_l2_l_cache_mode_pt;
   6046 pt_entry_t	pte_l2_l_cache_mask;
   6047 
   6048 pt_entry_t	pte_l2_s_cache_mode;
   6049 pt_entry_t	pte_l2_s_wc_mode;
   6050 pt_entry_t	pte_l2_s_cache_mode_pt;
   6051 pt_entry_t	pte_l2_s_cache_mask;
   6052 
   6053 pt_entry_t	pte_l1_s_prot_u;
   6054 pt_entry_t	pte_l1_s_prot_w;
   6055 pt_entry_t	pte_l1_s_prot_ro;
   6056 pt_entry_t	pte_l1_s_prot_mask;
   6057 
   6058 pt_entry_t	pte_l2_s_prot_u;
   6059 pt_entry_t	pte_l2_s_prot_w;
   6060 pt_entry_t	pte_l2_s_prot_ro;
   6061 pt_entry_t	pte_l2_s_prot_mask;
   6062 
   6063 pt_entry_t	pte_l2_l_prot_u;
   6064 pt_entry_t	pte_l2_l_prot_w;
   6065 pt_entry_t	pte_l2_l_prot_ro;
   6066 pt_entry_t	pte_l2_l_prot_mask;
   6067 
   6068 pt_entry_t	pte_l1_ss_proto;
   6069 pt_entry_t	pte_l1_s_proto;
   6070 pt_entry_t	pte_l1_c_proto;
   6071 pt_entry_t	pte_l2_s_proto;
   6072 
   6073 void		(*pmap_copy_page_func)(paddr_t, paddr_t);
   6074 void		(*pmap_zero_page_func)(paddr_t);
   6075 
   6076 #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6 + ARM_MMU_V7) != 0
   6077 void
   6078 pmap_pte_init_generic(void)
   6079 {
   6080 
   6081 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   6082 	pte_l1_s_wc_mode = L1_S_B;
   6083 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
   6084 
   6085 	pte_l2_l_cache_mode = L2_B|L2_C;
   6086 	pte_l2_l_wc_mode = L2_B;
   6087 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
   6088 
   6089 	pte_l2_s_cache_mode = L2_B|L2_C;
   6090 	pte_l2_s_wc_mode = L2_B;
   6091 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
   6092 
   6093 	/*
   6094 	 * If we have a write-through cache, set B and C.  If
   6095 	 * we have a write-back cache, then we assume setting
   6096 	 * only C will make those pages write-through (except for those
   6097 	 * Cortex CPUs which can read the L1 caches).
   6098 	 */
   6099 	if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop
   6100 #if ARM_MMU_V7 > 0
   6101 	    || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)
   6102 #endif
   6103 #if ARM_MMU_V6 > 0
   6104 	    || CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid) /* arm116 errata 399234 */
   6105 #endif
   6106 	    || false) {
   6107 		pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   6108 		pte_l2_l_cache_mode_pt = L2_B|L2_C;
   6109 		pte_l2_s_cache_mode_pt = L2_B|L2_C;
   6110 	} else {
   6111 		pte_l1_s_cache_mode_pt = L1_S_C;	/* write through */
   6112 		pte_l2_l_cache_mode_pt = L2_C;		/* write through */
   6113 		pte_l2_s_cache_mode_pt = L2_C;		/* write through */
   6114 	}
   6115 
   6116 	pte_l1_s_prot_u = L1_S_PROT_U_generic;
   6117 	pte_l1_s_prot_w = L1_S_PROT_W_generic;
   6118 	pte_l1_s_prot_ro = L1_S_PROT_RO_generic;
   6119 	pte_l1_s_prot_mask = L1_S_PROT_MASK_generic;
   6120 
   6121 	pte_l2_s_prot_u = L2_S_PROT_U_generic;
   6122 	pte_l2_s_prot_w = L2_S_PROT_W_generic;
   6123 	pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
   6124 	pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
   6125 
   6126 	pte_l2_l_prot_u = L2_L_PROT_U_generic;
   6127 	pte_l2_l_prot_w = L2_L_PROT_W_generic;
   6128 	pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
   6129 	pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
   6130 
   6131 	pte_l1_ss_proto = L1_SS_PROTO_generic;
   6132 	pte_l1_s_proto = L1_S_PROTO_generic;
   6133 	pte_l1_c_proto = L1_C_PROTO_generic;
   6134 	pte_l2_s_proto = L2_S_PROTO_generic;
   6135 
   6136 	pmap_copy_page_func = pmap_copy_page_generic;
   6137 	pmap_zero_page_func = pmap_zero_page_generic;
   6138 }
   6139 
   6140 #if defined(CPU_ARM8)
   6141 void
   6142 pmap_pte_init_arm8(void)
   6143 {
   6144 
   6145 	/*
   6146 	 * ARM8 is compatible with generic, but we need to use
   6147 	 * the page tables uncached.
   6148 	 */
   6149 	pmap_pte_init_generic();
   6150 
   6151 	pte_l1_s_cache_mode_pt = 0;
   6152 	pte_l2_l_cache_mode_pt = 0;
   6153 	pte_l2_s_cache_mode_pt = 0;
   6154 }
   6155 #endif /* CPU_ARM8 */
   6156 
   6157 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
   6158 void
   6159 pmap_pte_init_arm9(void)
   6160 {
   6161 
   6162 	/*
   6163 	 * ARM9 is compatible with generic, but we want to use
   6164 	 * write-through caching for now.
   6165 	 */
   6166 	pmap_pte_init_generic();
   6167 
   6168 	pte_l1_s_cache_mode = L1_S_C;
   6169 	pte_l2_l_cache_mode = L2_C;
   6170 	pte_l2_s_cache_mode = L2_C;
   6171 
   6172 	pte_l1_s_wc_mode = L1_S_B;
   6173 	pte_l2_l_wc_mode = L2_B;
   6174 	pte_l2_s_wc_mode = L2_B;
   6175 
   6176 	pte_l1_s_cache_mode_pt = L1_S_C;
   6177 	pte_l2_l_cache_mode_pt = L2_C;
   6178 	pte_l2_s_cache_mode_pt = L2_C;
   6179 }
   6180 #endif /* CPU_ARM9 && ARM9_CACHE_WRITE_THROUGH */
   6181 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   6182 
   6183 #if defined(CPU_ARM10)
   6184 void
   6185 pmap_pte_init_arm10(void)
   6186 {
   6187 
   6188 	/*
   6189 	 * ARM10 is compatible with generic, but we want to use
   6190 	 * write-through caching for now.
   6191 	 */
   6192 	pmap_pte_init_generic();
   6193 
   6194 	pte_l1_s_cache_mode = L1_S_B | L1_S_C;
   6195 	pte_l2_l_cache_mode = L2_B | L2_C;
   6196 	pte_l2_s_cache_mode = L2_B | L2_C;
   6197 
   6198 	pte_l1_s_cache_mode = L1_S_B;
   6199 	pte_l2_l_cache_mode = L2_B;
   6200 	pte_l2_s_cache_mode = L2_B;
   6201 
   6202 	pte_l1_s_cache_mode_pt = L1_S_C;
   6203 	pte_l2_l_cache_mode_pt = L2_C;
   6204 	pte_l2_s_cache_mode_pt = L2_C;
   6205 
   6206 }
   6207 #endif /* CPU_ARM10 */
   6208 
   6209 #if defined(CPU_ARM11) && defined(ARM11_CACHE_WRITE_THROUGH)
   6210 void
   6211 pmap_pte_init_arm11(void)
   6212 {
   6213 
   6214 	/*
   6215 	 * ARM11 is compatible with generic, but we want to use
   6216 	 * write-through caching for now.
   6217 	 */
   6218 	pmap_pte_init_generic();
   6219 
   6220 	pte_l1_s_cache_mode = L1_S_C;
   6221 	pte_l2_l_cache_mode = L2_C;
   6222 	pte_l2_s_cache_mode = L2_C;
   6223 
   6224 	pte_l1_s_wc_mode = L1_S_B;
   6225 	pte_l2_l_wc_mode = L2_B;
   6226 	pte_l2_s_wc_mode = L2_B;
   6227 
   6228 	pte_l1_s_cache_mode_pt = L1_S_C;
   6229 	pte_l2_l_cache_mode_pt = L2_C;
   6230 	pte_l2_s_cache_mode_pt = L2_C;
   6231 }
   6232 #endif /* CPU_ARM11 && ARM11_CACHE_WRITE_THROUGH */
   6233 
   6234 #if ARM_MMU_SA1 == 1
   6235 void
   6236 pmap_pte_init_sa1(void)
   6237 {
   6238 
   6239 	/*
   6240 	 * The StrongARM SA-1 cache does not have a write-through
   6241 	 * mode.  So, do the generic initialization, then reset
   6242 	 * the page table cache mode to B=1,C=1, and note that
   6243 	 * the PTEs need to be sync'd.
   6244 	 */
   6245 	pmap_pte_init_generic();
   6246 
   6247 	pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   6248 	pte_l2_l_cache_mode_pt = L2_B|L2_C;
   6249 	pte_l2_s_cache_mode_pt = L2_B|L2_C;
   6250 
   6251 	pmap_needs_pte_sync = 1;
   6252 }
   6253 #endif /* ARM_MMU_SA1 == 1*/
   6254 
   6255 #if ARM_MMU_XSCALE == 1
   6256 #if (ARM_NMMUS > 1)
   6257 static u_int xscale_use_minidata;
   6258 #endif
   6259 
   6260 void
   6261 pmap_pte_init_xscale(void)
   6262 {
   6263 	uint32_t auxctl;
   6264 	int write_through = 0;
   6265 
   6266 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   6267 	pte_l1_s_wc_mode = L1_S_B;
   6268 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
   6269 
   6270 	pte_l2_l_cache_mode = L2_B|L2_C;
   6271 	pte_l2_l_wc_mode = L2_B;
   6272 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
   6273 
   6274 	pte_l2_s_cache_mode = L2_B|L2_C;
   6275 	pte_l2_s_wc_mode = L2_B;
   6276 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
   6277 
   6278 	pte_l1_s_cache_mode_pt = L1_S_C;
   6279 	pte_l2_l_cache_mode_pt = L2_C;
   6280 	pte_l2_s_cache_mode_pt = L2_C;
   6281 
   6282 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
   6283 	/*
   6284 	 * The XScale core has an enhanced mode where writes that
   6285 	 * miss the cache cause a cache line to be allocated.  This
   6286 	 * is significantly faster than the traditional, write-through
   6287 	 * behavior of this case.
   6288 	 */
   6289 	pte_l1_s_cache_mode |= L1_S_XS_TEX(TEX_XSCALE_X);
   6290 	pte_l2_l_cache_mode |= L2_XS_L_TEX(TEX_XSCALE_X);
   6291 	pte_l2_s_cache_mode |= L2_XS_T_TEX(TEX_XSCALE_X);
   6292 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
   6293 
   6294 #ifdef XSCALE_CACHE_WRITE_THROUGH
   6295 	/*
   6296 	 * Some versions of the XScale core have various bugs in
   6297 	 * their cache units, the work-around for which is to run
   6298 	 * the cache in write-through mode.  Unfortunately, this
   6299 	 * has a major (negative) impact on performance.  So, we
   6300 	 * go ahead and run fast-and-loose, in the hopes that we
   6301 	 * don't line up the planets in a way that will trip the
   6302 	 * bugs.
   6303 	 *
   6304 	 * However, we give you the option to be slow-but-correct.
   6305 	 */
   6306 	write_through = 1;
   6307 #elif defined(XSCALE_CACHE_WRITE_BACK)
   6308 	/* force write back cache mode */
   6309 	write_through = 0;
   6310 #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
   6311 	/*
   6312 	 * Intel PXA2[15]0 processors are known to have a bug in
   6313 	 * write-back cache on revision 4 and earlier (stepping
   6314 	 * A[01] and B[012]).  Fixed for C0 and later.
   6315 	 */
   6316 	{
   6317 		uint32_t id, type;
   6318 
   6319 		id = cpufunc_id();
   6320 		type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
   6321 
   6322 		if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
   6323 			if ((id & CPU_ID_REVISION_MASK) < 5) {
   6324 				/* write through for stepping A0-1 and B0-2 */
   6325 				write_through = 1;
   6326 			}
   6327 		}
   6328 	}
   6329 #endif /* XSCALE_CACHE_WRITE_THROUGH */
   6330 
   6331 	if (write_through) {
   6332 		pte_l1_s_cache_mode = L1_S_C;
   6333 		pte_l2_l_cache_mode = L2_C;
   6334 		pte_l2_s_cache_mode = L2_C;
   6335 	}
   6336 
   6337 #if (ARM_NMMUS > 1)
   6338 	xscale_use_minidata = 1;
   6339 #endif
   6340 
   6341 	pte_l1_s_prot_u = L1_S_PROT_U_xscale;
   6342 	pte_l1_s_prot_w = L1_S_PROT_W_xscale;
   6343 	pte_l1_s_prot_ro = L1_S_PROT_RO_xscale;
   6344 	pte_l1_s_prot_mask = L1_S_PROT_MASK_xscale;
   6345 
   6346 	pte_l2_s_prot_u = L2_S_PROT_U_xscale;
   6347 	pte_l2_s_prot_w = L2_S_PROT_W_xscale;
   6348 	pte_l2_s_prot_ro = L2_S_PROT_RO_xscale;
   6349 	pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
   6350 
   6351 	pte_l2_l_prot_u = L2_L_PROT_U_xscale;
   6352 	pte_l2_l_prot_w = L2_L_PROT_W_xscale;
   6353 	pte_l2_l_prot_ro = L2_L_PROT_RO_xscale;
   6354 	pte_l2_l_prot_mask = L2_L_PROT_MASK_xscale;
   6355 
   6356 	pte_l1_ss_proto = L1_SS_PROTO_xscale;
   6357 	pte_l1_s_proto = L1_S_PROTO_xscale;
   6358 	pte_l1_c_proto = L1_C_PROTO_xscale;
   6359 	pte_l2_s_proto = L2_S_PROTO_xscale;
   6360 
   6361 	pmap_copy_page_func = pmap_copy_page_xscale;
   6362 	pmap_zero_page_func = pmap_zero_page_xscale;
   6363 
   6364 	/*
   6365 	 * Disable ECC protection of page table access, for now.
   6366 	 */
   6367 	__asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
   6368 	auxctl &= ~XSCALE_AUXCTL_P;
   6369 	__asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
   6370 }
   6371 
   6372 /*
   6373  * xscale_setup_minidata:
   6374  *
   6375  *	Set up the mini-data cache clean area.  We require the
   6376  *	caller to allocate the right amount of physically and
   6377  *	virtually contiguous space.
   6378  */
   6379 void
   6380 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
   6381 {
   6382 	extern vaddr_t xscale_minidata_clean_addr;
   6383 	extern vsize_t xscale_minidata_clean_size; /* already initialized */
   6384 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   6385 	pt_entry_t *pte;
   6386 	vsize_t size;
   6387 	uint32_t auxctl;
   6388 
   6389 	xscale_minidata_clean_addr = va;
   6390 
   6391 	/* Round it to page size. */
   6392 	size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
   6393 
   6394 	for (; size != 0;
   6395 	     va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
   6396 #ifndef ARM32_NEW_VM_LAYOUT
   6397 		pte = (pt_entry_t *)
   6398 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   6399 #else
   6400 		pte = (pt_entry_t *) kernel_pt_lookup(
   6401 		    pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   6402 #endif
   6403 		if (pte == NULL)
   6404 			panic("xscale_setup_minidata: can't find L2 table for "
   6405 			    "VA 0x%08lx", va);
   6406 #ifndef ARM32_NEW_VM_LAYOUT
   6407 		pte[(va >> PGSHIFT) & 0x3ff] =
   6408 #else
   6409 		pte[l2pte_index(va)] =
   6410 #endif
   6411 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   6412 		    L2_C | L2_XS_T_TEX(TEX_XSCALE_X);
   6413 	}
   6414 
   6415 	/*
   6416 	 * Configure the mini-data cache for write-back with
   6417 	 * read/write-allocate.
   6418 	 *
   6419 	 * NOTE: In order to reconfigure the mini-data cache, we must
   6420 	 * make sure it contains no valid data!  In order to do that,
   6421 	 * we must issue a global data cache invalidate command!
   6422 	 *
   6423 	 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
   6424 	 * THIS IS VERY IMPORTANT!
   6425 	 */
   6426 
   6427 	/* Invalidate data and mini-data. */
   6428 	__asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
   6429 	__asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
   6430 	auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
   6431 	__asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
   6432 }
   6433 
   6434 /*
   6435  * Change the PTEs for the specified kernel mappings such that they
   6436  * will use the mini data cache instead of the main data cache.
   6437  */
   6438 void
   6439 pmap_uarea(vaddr_t va)
   6440 {
   6441 	struct l2_bucket *l2b;
   6442 	pt_entry_t *ptep, *sptep, pte;
   6443 	vaddr_t next_bucket, eva;
   6444 
   6445 #if (ARM_NMMUS > 1)
   6446 	if (xscale_use_minidata == 0)
   6447 		return;
   6448 #endif
   6449 
   6450 	eva = va + USPACE;
   6451 
   6452 	while (va < eva) {
   6453 		next_bucket = L2_NEXT_BUCKET(va);
   6454 		if (next_bucket > eva)
   6455 			next_bucket = eva;
   6456 
   6457 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   6458 		KDASSERT(l2b != NULL);
   6459 
   6460 		sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
   6461 
   6462 		while (va < next_bucket) {
   6463 			pte = *ptep;
   6464 			if (!l2pte_minidata(pte)) {
   6465 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   6466 				cpu_tlb_flushD_SE(va);
   6467 				*ptep = pte & ~L2_B;
   6468 			}
   6469 			ptep++;
   6470 			va += PAGE_SIZE;
   6471 		}
   6472 		PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
   6473 	}
   6474 	cpu_cpwait();
   6475 }
   6476 #endif /* ARM_MMU_XSCALE == 1 */
   6477 
   6478 
   6479 #if defined(CPU_ARM11MPCORE)
   6480 
   6481 void
   6482 pmap_pte_init_arm11mpcore(void)
   6483 {
   6484 
   6485 	/* cache mode is controlled by 5 bits (B, C, TEX) */
   6486 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv6;
   6487 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv6;
   6488 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
   6489 	/* use extended small page (without APn, with TEX) */
   6490 	pte_l2_s_cache_mask = L2_XS_CACHE_MASK_armv6;
   6491 #else
   6492 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv6c;
   6493 #endif
   6494 
   6495 	/* write-back, write-allocate */
   6496 	pte_l1_s_cache_mode = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
   6497 	pte_l2_l_cache_mode = L2_C | L2_B | L2_V6_L_TEX(0x01);
   6498 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
   6499 	pte_l2_s_cache_mode = L2_C | L2_B | L2_V6_XS_TEX(0x01);
   6500 #else
   6501 	/* no TEX. read-allocate */
   6502 	pte_l2_s_cache_mode = L2_C | L2_B;
   6503 #endif
   6504 	/*
   6505 	 * write-back, write-allocate for page tables.
   6506 	 */
   6507 	pte_l1_s_cache_mode_pt = L1_S_C | L1_S_B | L1_S_V6_TEX(0x01);
   6508 	pte_l2_l_cache_mode_pt = L2_C | L2_B | L2_V6_L_TEX(0x01);
   6509 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
   6510 	pte_l2_s_cache_mode_pt = L2_C | L2_B | L2_V6_XS_TEX(0x01);
   6511 #else
   6512 	pte_l2_s_cache_mode_pt = L2_C | L2_B;
   6513 #endif
   6514 
   6515 	pte_l1_s_prot_u = L1_S_PROT_U_armv6;
   6516 	pte_l1_s_prot_w = L1_S_PROT_W_armv6;
   6517 	pte_l1_s_prot_ro = L1_S_PROT_RO_armv6;
   6518 	pte_l1_s_prot_mask = L1_S_PROT_MASK_armv6;
   6519 
   6520 #if defined(ARM11MPCORE_COMPAT_MMU) || defined(ARMV6_EXTENDED_SMALL_PAGE)
   6521 	pte_l2_s_prot_u = L2_S_PROT_U_armv6n;
   6522 	pte_l2_s_prot_w = L2_S_PROT_W_armv6n;
   6523 	pte_l2_s_prot_ro = L2_S_PROT_RO_armv6n;
   6524 	pte_l2_s_prot_mask = L2_S_PROT_MASK_armv6n;
   6525 
   6526 #else
   6527 	/* with AP[0..3] */
   6528 	pte_l2_s_prot_u = L2_S_PROT_U_generic;
   6529 	pte_l2_s_prot_w = L2_S_PROT_W_generic;
   6530 	pte_l2_s_prot_ro = L2_S_PROT_RO_generic;
   6531 	pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
   6532 #endif
   6533 
   6534 #ifdef	ARM11MPCORE_COMPAT_MMU
   6535 	/* with AP[0..3] */
   6536 	pte_l2_l_prot_u = L2_L_PROT_U_generic;
   6537 	pte_l2_l_prot_w = L2_L_PROT_W_generic;
   6538 	pte_l2_l_prot_ro = L2_L_PROT_RO_generic;
   6539 	pte_l2_l_prot_mask = L2_L_PROT_MASK_generic;
   6540 
   6541 	pte_l1_ss_proto = L1_SS_PROTO_armv6;
   6542 	pte_l1_s_proto = L1_S_PROTO_armv6;
   6543 	pte_l1_c_proto = L1_C_PROTO_armv6;
   6544 	pte_l2_s_proto = L2_S_PROTO_armv6c;
   6545 #else
   6546 	pte_l2_l_prot_u = L2_L_PROT_U_armv6n;
   6547 	pte_l2_l_prot_w = L2_L_PROT_W_armv6n;
   6548 	pte_l2_l_prot_ro = L2_L_PROT_RO_armv6n;
   6549 	pte_l2_l_prot_mask = L2_L_PROT_MASK_armv6n;
   6550 
   6551 	pte_l1_ss_proto = L1_SS_PROTO_armv6;
   6552 	pte_l1_s_proto = L1_S_PROTO_armv6;
   6553 	pte_l1_c_proto = L1_C_PROTO_armv6;
   6554 	pte_l2_s_proto = L2_S_PROTO_armv6n;
   6555 #endif
   6556 
   6557 	pmap_copy_page_func = pmap_copy_page_generic;
   6558 	pmap_zero_page_func = pmap_zero_page_generic;
   6559 	pmap_needs_pte_sync = 1;
   6560 }
   6561 #endif	/* CPU_ARM11MPCORE */
   6562 
   6563 
   6564 #if ARM_MMU_V7 == 1
   6565 void
   6566 pmap_pte_init_armv7(void)
   6567 {
   6568 	/*
   6569 	 * The ARMv7-A MMU is mostly compatible with generic. If the
   6570 	 * AP field is zero, that now means "no access" rather than
   6571 	 * read-only. The prototypes are a little different because of
   6572 	 * the XN bit.
   6573 	 */
   6574 	pmap_pte_init_generic();
   6575 
   6576 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_armv7;
   6577 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_armv7;
   6578 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_armv7;
   6579 
   6580 	if (CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid)) {
   6581 		/*
   6582 		 * write-back, no write-allocate, shareable for normal pages.
   6583 		 */
   6584 		pte_l1_s_cache_mode = L1_S_C | L1_S_B | L1_S_V6_S;
   6585 		pte_l2_l_cache_mode = L2_C | L2_B | L2_XS_S;
   6586 		pte_l2_s_cache_mode = L2_C | L2_B | L2_XS_S;
   6587 
   6588 		/*
   6589 		 * write-back, no write-allocate, shareable for page tables.
   6590 		 */
   6591 		pte_l1_s_cache_mode_pt = L1_S_C | L1_S_B | L1_S_V6_S;
   6592 		pte_l2_l_cache_mode_pt = L2_C | L2_B | L2_XS_S;
   6593 		pte_l2_s_cache_mode_pt = L2_C | L2_B | L2_XS_S;
   6594 	}
   6595 
   6596 	pte_l1_s_prot_u = L1_S_PROT_U_armv7;
   6597 	pte_l1_s_prot_w = L1_S_PROT_W_armv7;
   6598 	pte_l1_s_prot_ro = L1_S_PROT_RO_armv7;
   6599 	pte_l1_s_prot_mask = L1_S_PROT_MASK_armv7;
   6600 
   6601 	pte_l2_s_prot_u = L2_S_PROT_U_armv7;
   6602 	pte_l2_s_prot_w = L2_S_PROT_W_armv7;
   6603 	pte_l2_s_prot_ro = L2_S_PROT_RO_armv7;
   6604 	pte_l2_s_prot_mask = L2_S_PROT_MASK_armv7;
   6605 
   6606 	pte_l2_l_prot_u = L2_L_PROT_U_armv7;
   6607 	pte_l2_l_prot_w = L2_L_PROT_W_armv7;
   6608 	pte_l2_l_prot_ro = L2_L_PROT_RO_armv7;
   6609 	pte_l2_l_prot_mask = L2_L_PROT_MASK_armv7;
   6610 
   6611 	pte_l1_ss_proto = L1_SS_PROTO_armv7;
   6612 	pte_l1_s_proto = L1_S_PROTO_armv7;
   6613 	pte_l1_c_proto = L1_C_PROTO_armv7;
   6614 	pte_l2_s_proto = L2_S_PROTO_armv7;
   6615 
   6616 	pmap_needs_pte_sync = 1;
   6617 }
   6618 #endif /* ARM_MMU_V7 */
   6619 
   6620 /*
   6621  * return the PA of the current L1 table, for use when handling a crash dump
   6622  */
   6623 uint32_t pmap_kernel_L1_addr(void)
   6624 {
   6625 	return pmap_kernel()->pm_l1->l1_physaddr;
   6626 }
   6627 
   6628 #if defined(DDB)
   6629 /*
   6630  * A couple of ddb-callable functions for dumping pmaps
   6631  */
   6632 void pmap_dump_all(void);
   6633 void pmap_dump(pmap_t);
   6634 
   6635 void
   6636 pmap_dump_all(void)
   6637 {
   6638 	pmap_t pm;
   6639 
   6640 	LIST_FOREACH(pm, &pmap_pmaps, pm_list) {
   6641 		if (pm == pmap_kernel())
   6642 			continue;
   6643 		pmap_dump(pm);
   6644 		printf("\n");
   6645 	}
   6646 }
   6647 
   6648 static pt_entry_t ncptes[64];
   6649 static void pmap_dump_ncpg(pmap_t);
   6650 
   6651 void
   6652 pmap_dump(pmap_t pm)
   6653 {
   6654 	struct l2_dtable *l2;
   6655 	struct l2_bucket *l2b;
   6656 	pt_entry_t *ptep, pte;
   6657 	vaddr_t l2_va, l2b_va, va;
   6658 	int i, j, k, occ, rows = 0;
   6659 
   6660 	if (pm == pmap_kernel())
   6661 		printf("pmap_kernel (%p): ", pm);
   6662 	else
   6663 		printf("user pmap (%p): ", pm);
   6664 
   6665 	printf("domain %d, l1 at %p\n", pm->pm_domain, pm->pm_l1->l1_kva);
   6666 
   6667 	l2_va = 0;
   6668 	for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
   6669 		l2 = pm->pm_l2[i];
   6670 
   6671 		if (l2 == NULL || l2->l2_occupancy == 0)
   6672 			continue;
   6673 
   6674 		l2b_va = l2_va;
   6675 		for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
   6676 			l2b = &l2->l2_bucket[j];
   6677 
   6678 			if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
   6679 				continue;
   6680 
   6681 			ptep = l2b->l2b_kva;
   6682 
   6683 			for (k = 0; k < 256 && ptep[k] == 0; k++)
   6684 				;
   6685 
   6686 			k &= ~63;
   6687 			occ = l2b->l2b_occupancy;
   6688 			va = l2b_va + (k * 4096);
   6689 			for (; k < 256; k++, va += 0x1000) {
   6690 				char ch = ' ';
   6691 				if ((k % 64) == 0) {
   6692 					if ((rows % 8) == 0) {
   6693 						printf(
   6694 "          |0000   |8000   |10000  |18000  |20000  |28000  |30000  |38000\n");
   6695 					}
   6696 					printf("%08lx: ", va);
   6697 				}
   6698 
   6699 				ncptes[k & 63] = 0;
   6700 				pte = ptep[k];
   6701 				if (pte == 0) {
   6702 					ch = '.';
   6703 				} else {
   6704 					occ--;
   6705 					switch (pte & 0x0c) {
   6706 					case 0x00:
   6707 						ch = 'D'; /* No cache No buff */
   6708 						break;
   6709 					case 0x04:
   6710 						ch = 'B'; /* No cache buff */
   6711 						break;
   6712 					case 0x08:
   6713 						if (pte & 0x40)
   6714 							ch = 'm';
   6715 						else
   6716 						   ch = 'C'; /* Cache No buff */
   6717 						break;
   6718 					case 0x0c:
   6719 						ch = 'F'; /* Cache Buff */
   6720 						break;
   6721 					}
   6722 
   6723 					if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
   6724 						ch += 0x20;
   6725 
   6726 					if ((pte & 0xc) == 0)
   6727 						ncptes[k & 63] = pte;
   6728 				}
   6729 
   6730 				if ((k % 64) == 63) {
   6731 					rows++;
   6732 					printf("%c\n", ch);
   6733 					pmap_dump_ncpg(pm);
   6734 					if (occ == 0)
   6735 						break;
   6736 				} else
   6737 					printf("%c", ch);
   6738 			}
   6739 		}
   6740 	}
   6741 }
   6742 
   6743 static void
   6744 pmap_dump_ncpg(pmap_t pm)
   6745 {
   6746 	struct vm_page *pg;
   6747 	struct vm_page_md *md;
   6748 	struct pv_entry *pv;
   6749 	int i;
   6750 
   6751 	for (i = 0; i < 63; i++) {
   6752 		if (ncptes[i] == 0)
   6753 			continue;
   6754 
   6755 		pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
   6756 		if (pg == NULL)
   6757 			continue;
   6758 		md = VM_PAGE_TO_MD(pg);
   6759 
   6760 		printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
   6761 		    VM_PAGE_TO_PHYS(pg),
   6762 		    md->krw_mappings, md->kro_mappings,
   6763 		    md->urw_mappings, md->uro_mappings);
   6764 
   6765 		SLIST_FOREACH(pv, &md->pvh_list, pv_link) {
   6766 			printf("   %c va 0x%08lx, flags 0x%x\n",
   6767 			    (pm == pv->pv_pmap) ? '*' : ' ',
   6768 			    pv->pv_va, pv->pv_flags);
   6769 		}
   6770 	}
   6771 }
   6772 #endif
   6773 
   6774 #ifdef PMAP_STEAL_MEMORY
   6775 void
   6776 pmap_boot_pageadd(pv_addr_t *newpv)
   6777 {
   6778 	pv_addr_t *pv, *npv;
   6779 
   6780 	if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
   6781 		if (newpv->pv_pa < pv->pv_va) {
   6782 			KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
   6783 			if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
   6784 				newpv->pv_size += pv->pv_size;
   6785 				SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
   6786 			}
   6787 			pv = NULL;
   6788 		} else {
   6789 			for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
   6790 			     pv = npv) {
   6791 				KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
   6792 				KASSERT(pv->pv_pa < newpv->pv_pa);
   6793 				if (newpv->pv_pa > npv->pv_pa)
   6794 					continue;
   6795 				if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
   6796 					pv->pv_size += newpv->pv_size;
   6797 					return;
   6798 				}
   6799 				if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
   6800 					break;
   6801 				newpv->pv_size += npv->pv_size;
   6802 				SLIST_INSERT_AFTER(pv, newpv, pv_list);
   6803 				SLIST_REMOVE_AFTER(newpv, pv_list);
   6804 				return;
   6805 			}
   6806 		}
   6807 	}
   6808 
   6809 	if (pv) {
   6810 		SLIST_INSERT_AFTER(pv, newpv, pv_list);
   6811 	} else {
   6812 		SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
   6813 	}
   6814 }
   6815 
   6816 void
   6817 pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
   6818 	pv_addr_t *rpv)
   6819 {
   6820 	pv_addr_t *pv, **pvp;
   6821 	struct vm_physseg *ps;
   6822 	size_t i;
   6823 
   6824 	KASSERT(amount & PGOFSET);
   6825 	KASSERT((mask & PGOFSET) == 0);
   6826 	KASSERT((match & PGOFSET) == 0);
   6827 	KASSERT(amount != 0);
   6828 
   6829 	for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
   6830 	     (pv = *pvp) != NULL;
   6831 	     pvp = &SLIST_NEXT(pv, pv_list)) {
   6832 		pv_addr_t *newpv;
   6833 		psize_t off;
   6834 		/*
   6835 		 * If this entry is too small to satify the request...
   6836 		 */
   6837 		KASSERT(pv->pv_size > 0);
   6838 		if (pv->pv_size < amount)
   6839 			continue;
   6840 
   6841 		for (off = 0; off <= mask; off += PAGE_SIZE) {
   6842 			if (((pv->pv_pa + off) & mask) == match
   6843 			    && off + amount <= pv->pv_size)
   6844 				break;
   6845 		}
   6846 		if (off > mask)
   6847 			continue;
   6848 
   6849 		rpv->pv_va = pv->pv_va + off;
   6850 		rpv->pv_pa = pv->pv_pa + off;
   6851 		rpv->pv_size = amount;
   6852 		pv->pv_size -= amount;
   6853 		if (pv->pv_size == 0) {
   6854 			KASSERT(off == 0);
   6855 			KASSERT((vaddr_t) pv == rpv->pv_va);
   6856 			*pvp = SLIST_NEXT(pv, pv_list);
   6857 		} else if (off == 0) {
   6858 			KASSERT((vaddr_t) pv == rpv->pv_va);
   6859 			newpv = (pv_addr_t *) (rpv->pv_va + amount);
   6860 			*newpv = *pv;
   6861 			newpv->pv_pa += amount;
   6862 			newpv->pv_va += amount;
   6863 			*pvp = newpv;
   6864 		} else if (off < pv->pv_size) {
   6865 			newpv = (pv_addr_t *) (rpv->pv_va + amount);
   6866 			*newpv = *pv;
   6867 			newpv->pv_size -= off;
   6868 			newpv->pv_pa += off + amount;
   6869 			newpv->pv_va += off + amount;
   6870 
   6871 			SLIST_NEXT(pv, pv_list) = newpv;
   6872 			pv->pv_size = off;
   6873 		} else {
   6874 			KASSERT((vaddr_t) pv != rpv->pv_va);
   6875 		}
   6876 		memset((void *)rpv->pv_va, 0, amount);
   6877 		return;
   6878 	}
   6879 
   6880 	if (vm_nphysseg == 0)
   6881 		panic("pmap_boot_pagealloc: couldn't allocate memory");
   6882 
   6883 	for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
   6884 	     (pv = *pvp) != NULL;
   6885 	     pvp = &SLIST_NEXT(pv, pv_list)) {
   6886 		if (SLIST_NEXT(pv, pv_list) == NULL)
   6887 			break;
   6888 	}
   6889 	KASSERT(mask == 0);
   6890 	for (i = 0; i < vm_nphysseg; i++) {
   6891 		ps = VM_PHYSMEM_PTR(i);
   6892 		if (ps->avail_start == atop(pv->pv_pa + pv->pv_size)
   6893 		    && pv->pv_va + pv->pv_size <= ptoa(ps->avail_end)) {
   6894 			rpv->pv_va = pv->pv_va;
   6895 			rpv->pv_pa = pv->pv_pa;
   6896 			rpv->pv_size = amount;
   6897 			*pvp = NULL;
   6898 			pmap_map_chunk(kernel_l1pt.pv_va,
   6899 			     ptoa(ps->avail_start) + (pv->pv_va - pv->pv_pa),
   6900 			     ptoa(ps->avail_start),
   6901 			     amount - pv->pv_size,
   6902 			     VM_PROT_READ|VM_PROT_WRITE,
   6903 			     PTE_CACHE);
   6904 			ps->avail_start += atop(amount - pv->pv_size);
   6905 			/*
   6906 			 * If we consumed the entire physseg, remove it.
   6907 			 */
   6908 			if (ps->avail_start == ps->avail_end) {
   6909 				for (--vm_nphysseg; i < vm_nphysseg; i++)
   6910 					VM_PHYSMEM_PTR_SWAP(i, i + 1);
   6911 			}
   6912 			memset((void *)rpv->pv_va, 0, rpv->pv_size);
   6913 			return;
   6914 		}
   6915 	}
   6916 
   6917 	panic("pmap_boot_pagealloc: couldn't allocate memory");
   6918 }
   6919 
   6920 vaddr_t
   6921 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
   6922 {
   6923 	pv_addr_t pv;
   6924 
   6925 	pmap_boot_pagealloc(size, 0, 0, &pv);
   6926 
   6927 	return pv.pv_va;
   6928 }
   6929 #endif /* PMAP_STEAL_MEMORY */
   6930 
   6931 SYSCTL_SETUP(sysctl_machdep_pmap_setup, "sysctl machdep.kmpages setup")
   6932 {
   6933 	sysctl_createv(clog, 0, NULL, NULL,
   6934 			CTLFLAG_PERMANENT,
   6935 			CTLTYPE_NODE, "machdep", NULL,
   6936 			NULL, 0, NULL, 0,
   6937 			CTL_MACHDEP, CTL_EOL);
   6938 
   6939 	sysctl_createv(clog, 0, NULL, NULL,
   6940 			CTLFLAG_PERMANENT,
   6941 			CTLTYPE_INT, "kmpages",
   6942 			SYSCTL_DESCR("count of pages allocated to kernel memory allocators"),
   6943 			NULL, 0, &pmap_kmpages, 0,
   6944 			CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   6945 }
   6946 
   6947 #ifdef PMAP_NEED_ALLOC_POOLPAGE
   6948 struct vm_page *
   6949 arm_pmap_alloc_poolpage(int flags)
   6950 {
   6951 	/*
   6952 	 * On some systems, only some pages may be "coherent" for dma and we
   6953 	 * want to prefer those for pool pages (think mbufs) but fallback to
   6954 	 * any page if none is available.
   6955 	 */
   6956 	if (arm_poolpage_vmfreelist != VM_FREELIST_DEFAULT) {
   6957 		return uvm_pagealloc_strat(NULL, 0, NULL, flags,
   6958 		    UVM_PGA_STRAT_FALLBACK, arm_poolpage_vmfreelist);
   6959 	}
   6960 
   6961 	return uvm_pagealloc(NULL, 0, NULL, flags);
   6962 }
   6963 #endif
   6964