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