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