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