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