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