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