Home | History | Annotate | Line # | Download | only in pmap
pmap.c revision 1.46
      1 /*	$NetBSD: pmap.c,v 1.46 2020/03/11 13:30:31 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Chris G. Demetriou.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This code is derived from software contributed to Berkeley by
     38  * the Systems Programming Group of the University of Utah Computer
     39  * Science Department and Ralph Campbell.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     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. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR 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  *	@(#)pmap.c	8.4 (Berkeley) 1/26/94
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 
     70 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.46 2020/03/11 13:30:31 thorpej Exp $");
     71 
     72 /*
     73  *	Manages physical address maps.
     74  *
     75  *	In addition to hardware address maps, this
     76  *	module is called upon to provide software-use-only
     77  *	maps which may or may not be stored in the same
     78  *	form as hardware maps.  These pseudo-maps are
     79  *	used to store intermediate results from copy
     80  *	operations to and from address spaces.
     81  *
     82  *	Since the information managed by this module is
     83  *	also stored by the logical address mapping module,
     84  *	this module may throw away valid virtual-to-physical
     85  *	mappings at almost any time.  However, invalidations
     86  *	of virtual-to-physical mappings must be done as
     87  *	requested.
     88  *
     89  *	In order to cope with hardware architectures which
     90  *	make virtual-to-physical map invalidates expensive,
     91  *	this module may delay invalidate or reduced protection
     92  *	operations until such time as they are actually
     93  *	necessary.  This module is given full information as
     94  *	to which processors are currently using which maps,
     95  *	and to when physical maps must be made correct.
     96  */
     97 
     98 #include "opt_modular.h"
     99 #include "opt_multiprocessor.h"
    100 #include "opt_sysv.h"
    101 
    102 #define __PMAP_PRIVATE
    103 
    104 #include <sys/param.h>
    105 
    106 #include <sys/atomic.h>
    107 #include <sys/buf.h>
    108 #include <sys/cpu.h>
    109 #include <sys/mutex.h>
    110 #include <sys/pool.h>
    111 
    112 #include <uvm/uvm.h>
    113 #include <uvm/uvm_physseg.h>
    114 
    115 #if defined(MULTIPROCESSOR) && defined(PMAP_VIRTUAL_CACHE_ALIASES) \
    116     && !defined(PMAP_NO_PV_UNCACHED)
    117 #error PMAP_VIRTUAL_CACHE_ALIASES with MULTIPROCESSOR requires \
    118  PMAP_NO_PV_UNCACHED to be defined
    119 #endif
    120 
    121 PMAP_COUNTER(remove_kernel_calls, "remove kernel calls");
    122 PMAP_COUNTER(remove_kernel_pages, "kernel pages unmapped");
    123 PMAP_COUNTER(remove_user_calls, "remove user calls");
    124 PMAP_COUNTER(remove_user_pages, "user pages unmapped");
    125 PMAP_COUNTER(remove_flushes, "remove cache flushes");
    126 PMAP_COUNTER(remove_tlb_ops, "remove tlb ops");
    127 PMAP_COUNTER(remove_pvfirst, "remove pv first");
    128 PMAP_COUNTER(remove_pvsearch, "remove pv search");
    129 
    130 PMAP_COUNTER(prefer_requests, "prefer requests");
    131 PMAP_COUNTER(prefer_adjustments, "prefer adjustments");
    132 
    133 PMAP_COUNTER(idlezeroed_pages, "pages idle zeroed");
    134 
    135 PMAP_COUNTER(kenter_pa, "kernel fast mapped pages");
    136 PMAP_COUNTER(kenter_pa_bad, "kernel fast mapped pages (bad color)");
    137 PMAP_COUNTER(kenter_pa_unmanaged, "kernel fast mapped unmanaged pages");
    138 PMAP_COUNTER(kremove_pages, "kernel fast unmapped pages");
    139 
    140 PMAP_COUNTER(page_cache_evictions, "pages changed to uncacheable");
    141 PMAP_COUNTER(page_cache_restorations, "pages changed to cacheable");
    142 
    143 PMAP_COUNTER(kernel_mappings_bad, "kernel pages mapped (bad color)");
    144 PMAP_COUNTER(user_mappings_bad, "user pages mapped (bad color)");
    145 PMAP_COUNTER(kernel_mappings, "kernel pages mapped");
    146 PMAP_COUNTER(user_mappings, "user pages mapped");
    147 PMAP_COUNTER(user_mappings_changed, "user mapping changed");
    148 PMAP_COUNTER(kernel_mappings_changed, "kernel mapping changed");
    149 PMAP_COUNTER(uncached_mappings, "uncached pages mapped");
    150 PMAP_COUNTER(unmanaged_mappings, "unmanaged pages mapped");
    151 PMAP_COUNTER(managed_mappings, "managed pages mapped");
    152 PMAP_COUNTER(mappings, "pages mapped");
    153 PMAP_COUNTER(remappings, "pages remapped");
    154 PMAP_COUNTER(unmappings, "pages unmapped");
    155 PMAP_COUNTER(primary_mappings, "page initial mappings");
    156 PMAP_COUNTER(primary_unmappings, "page final unmappings");
    157 PMAP_COUNTER(tlb_hit, "page mapping");
    158 
    159 PMAP_COUNTER(exec_mappings, "exec pages mapped");
    160 PMAP_COUNTER(exec_synced_mappings, "exec pages synced");
    161 PMAP_COUNTER(exec_synced_remove, "exec pages synced (PR)");
    162 PMAP_COUNTER(exec_synced_clear_modify, "exec pages synced (CM)");
    163 PMAP_COUNTER(exec_synced_page_protect, "exec pages synced (PP)");
    164 PMAP_COUNTER(exec_synced_protect, "exec pages synced (P)");
    165 PMAP_COUNTER(exec_uncached_page_protect, "exec pages uncached (PP)");
    166 PMAP_COUNTER(exec_uncached_clear_modify, "exec pages uncached (CM)");
    167 PMAP_COUNTER(exec_uncached_zero_page, "exec pages uncached (ZP)");
    168 PMAP_COUNTER(exec_uncached_copy_page, "exec pages uncached (CP)");
    169 PMAP_COUNTER(exec_uncached_remove, "exec pages uncached (PR)");
    170 
    171 PMAP_COUNTER(create, "creates");
    172 PMAP_COUNTER(reference, "references");
    173 PMAP_COUNTER(dereference, "dereferences");
    174 PMAP_COUNTER(destroy, "destroyed");
    175 PMAP_COUNTER(activate, "activations");
    176 PMAP_COUNTER(deactivate, "deactivations");
    177 PMAP_COUNTER(update, "updates");
    178 #ifdef MULTIPROCESSOR
    179 PMAP_COUNTER(shootdown_ipis, "shootdown IPIs");
    180 #endif
    181 PMAP_COUNTER(unwire, "unwires");
    182 PMAP_COUNTER(copy, "copies");
    183 PMAP_COUNTER(clear_modify, "clear_modifies");
    184 PMAP_COUNTER(protect, "protects");
    185 PMAP_COUNTER(page_protect, "page_protects");
    186 
    187 #define PMAP_ASID_RESERVED 0
    188 CTASSERT(PMAP_ASID_RESERVED == 0);
    189 
    190 #ifndef PMAP_SEGTAB_ALIGN
    191 #define PMAP_SEGTAB_ALIGN	/* nothing */
    192 #endif
    193 #ifdef _LP64
    194 pmap_segtab_t	pmap_kstart_segtab PMAP_SEGTAB_ALIGN; /* first mid-level segtab for kernel */
    195 #endif
    196 pmap_segtab_t	pmap_kern_segtab PMAP_SEGTAB_ALIGN = { /* top level segtab for kernel */
    197 #ifdef _LP64
    198 	.seg_seg[(VM_MIN_KERNEL_ADDRESS & XSEGOFSET) >> SEGSHIFT] = &pmap_kstart_segtab,
    199 #endif
    200 };
    201 
    202 struct pmap_kernel kernel_pmap_store = {
    203 	.kernel_pmap = {
    204 		.pm_count = 1,
    205 		.pm_segtab = &pmap_kern_segtab,
    206 		.pm_minaddr = VM_MIN_KERNEL_ADDRESS,
    207 		.pm_maxaddr = VM_MAX_KERNEL_ADDRESS,
    208 	},
    209 };
    210 
    211 struct pmap * const kernel_pmap_ptr = &kernel_pmap_store.kernel_pmap;
    212 
    213 struct pmap_limits pmap_limits = {	/* VA and PA limits */
    214 	.virtual_start = VM_MIN_KERNEL_ADDRESS,
    215 };
    216 
    217 #ifdef UVMHIST
    218 static struct kern_history_ent pmapexechistbuf[10000];
    219 static struct kern_history_ent pmaphistbuf[10000];
    220 UVMHIST_DEFINE(pmapexechist);
    221 UVMHIST_DEFINE(pmaphist);
    222 #endif
    223 
    224 /*
    225  * The pools from which pmap structures and sub-structures are allocated.
    226  */
    227 struct pool pmap_pmap_pool;
    228 struct pool pmap_pv_pool;
    229 
    230 #ifndef PMAP_PV_LOWAT
    231 #define	PMAP_PV_LOWAT	16
    232 #endif
    233 int	pmap_pv_lowat = PMAP_PV_LOWAT;
    234 
    235 bool	pmap_initialized = false;
    236 #define	PMAP_PAGE_COLOROK_P(a, b) \
    237 		((((int)(a) ^ (int)(b)) & pmap_page_colormask) == 0)
    238 u_int	pmap_page_colormask;
    239 
    240 #define PAGE_IS_MANAGED(pa)	(pmap_initialized && uvm_pageismanaged(pa))
    241 
    242 #define PMAP_IS_ACTIVE(pm)						\
    243 	((pm) == pmap_kernel() || 					\
    244 	 (pm) == curlwp->l_proc->p_vmspace->vm_map.pmap)
    245 
    246 /* Forward function declarations */
    247 void pmap_page_remove(struct vm_page *);
    248 static void pmap_pvlist_check(struct vm_page_md *);
    249 void pmap_remove_pv(pmap_t, vaddr_t, struct vm_page *, bool);
    250 void pmap_enter_pv(pmap_t, vaddr_t, struct vm_page *, pt_entry_t *, u_int);
    251 
    252 /*
    253  * PV table management functions.
    254  */
    255 void	*pmap_pv_page_alloc(struct pool *, int);
    256 void	pmap_pv_page_free(struct pool *, void *);
    257 
    258 struct pool_allocator pmap_pv_page_allocator = {
    259 	pmap_pv_page_alloc, pmap_pv_page_free, 0,
    260 };
    261 
    262 #define	pmap_pv_alloc()		pool_get(&pmap_pv_pool, PR_NOWAIT)
    263 #define	pmap_pv_free(pv)	pool_put(&pmap_pv_pool, (pv))
    264 
    265 #ifndef PMAP_NEED_TLB_MISS_LOCK
    266 
    267 #if defined(PMAP_MD_NEED_TLB_MISS_LOCK) || defined(DEBUG)
    268 #define	PMAP_NEED_TLB_MISS_LOCK
    269 #endif /* PMAP_MD_NEED_TLB_MISS_LOCK || DEBUG */
    270 
    271 #endif /* PMAP_NEED_TLB_MISS_LOCK */
    272 
    273 #ifdef PMAP_NEED_TLB_MISS_LOCK
    274 
    275 #ifdef PMAP_MD_NEED_TLB_MISS_LOCK
    276 #define	pmap_tlb_miss_lock_init()	__nothing /* MD code deals with this */
    277 #define	pmap_tlb_miss_lock_enter()	pmap_md_tlb_miss_lock_enter()
    278 #define	pmap_tlb_miss_lock_exit()	pmap_md_tlb_miss_lock_exit()
    279 #else
    280 static kmutex_t pmap_tlb_miss_lock __cacheline_aligned;
    281 
    282 static void
    283 pmap_tlb_miss_lock_init(void)
    284 {
    285 	mutex_init(&pmap_tlb_miss_lock, MUTEX_SPIN, IPL_HIGH);
    286 }
    287 
    288 static inline void
    289 pmap_tlb_miss_lock_enter(void)
    290 {
    291 	mutex_spin_enter(&pmap_tlb_miss_lock);
    292 }
    293 
    294 static inline void
    295 pmap_tlb_miss_lock_exit(void)
    296 {
    297 	mutex_spin_exit(&pmap_tlb_miss_lock);
    298 }
    299 #endif /* PMAP_MD_NEED_TLB_MISS_LOCK */
    300 
    301 #else
    302 
    303 #define	pmap_tlb_miss_lock_init()	__nothing
    304 #define	pmap_tlb_miss_lock_enter()	__nothing
    305 #define	pmap_tlb_miss_lock_exit()	__nothing
    306 
    307 #endif /* PMAP_NEED_TLB_MISS_LOCK */
    308 
    309 #ifndef MULTIPROCESSOR
    310 kmutex_t pmap_pvlist_mutex	__cacheline_aligned;
    311 #endif
    312 
    313 /*
    314  * Debug functions.
    315  */
    316 
    317 #ifdef DEBUG
    318 static inline void
    319 pmap_asid_check(pmap_t pm, const char *func)
    320 {
    321 	if (!PMAP_IS_ACTIVE(pm))
    322 		return;
    323 
    324 	struct pmap_asid_info * const pai = PMAP_PAI(pm, cpu_tlb_info(curcpu()));
    325 	tlb_asid_t asid = tlb_get_asid();
    326 	if (asid != pai->pai_asid)
    327 		panic("%s: inconsistency for active TLB update: %u <-> %u",
    328 		    func, asid, pai->pai_asid);
    329 }
    330 #endif
    331 
    332 static void
    333 pmap_addr_range_check(pmap_t pmap, vaddr_t sva, vaddr_t eva, const char *func)
    334 {
    335 #ifdef DEBUG
    336 	if (pmap == pmap_kernel()) {
    337 		if (sva < VM_MIN_KERNEL_ADDRESS)
    338 			panic("%s: kva %#"PRIxVADDR" not in range",
    339 			    func, sva);
    340 		if (eva >= pmap_limits.virtual_end)
    341 			panic("%s: kva %#"PRIxVADDR" not in range",
    342 			    func, eva);
    343 	} else {
    344 		if (eva > VM_MAXUSER_ADDRESS)
    345 			panic("%s: uva %#"PRIxVADDR" not in range",
    346 			    func, eva);
    347 		pmap_asid_check(pmap, func);
    348 	}
    349 #endif
    350 }
    351 
    352 /*
    353  * Misc. functions.
    354  */
    355 
    356 bool
    357 pmap_page_clear_attributes(struct vm_page_md *mdpg, u_int clear_attributes)
    358 {
    359 	volatile unsigned long * const attrp = &mdpg->mdpg_attrs;
    360 #ifdef MULTIPROCESSOR
    361 	for (;;) {
    362 		u_int old_attr = *attrp;
    363 		if ((old_attr & clear_attributes) == 0)
    364 			return false;
    365 		u_int new_attr = old_attr & ~clear_attributes;
    366 		if (old_attr == atomic_cas_ulong(attrp, old_attr, new_attr))
    367 			return true;
    368 	}
    369 #else
    370 	unsigned long old_attr = *attrp;
    371 	if ((old_attr & clear_attributes) == 0)
    372 		return false;
    373 	*attrp &= ~clear_attributes;
    374 	return true;
    375 #endif
    376 }
    377 
    378 void
    379 pmap_page_set_attributes(struct vm_page_md *mdpg, u_int set_attributes)
    380 {
    381 #ifdef MULTIPROCESSOR
    382 	atomic_or_ulong(&mdpg->mdpg_attrs, set_attributes);
    383 #else
    384 	mdpg->mdpg_attrs |= set_attributes;
    385 #endif
    386 }
    387 
    388 static void
    389 pmap_page_syncicache(struct vm_page *pg)
    390 {
    391 #ifndef MULTIPROCESSOR
    392 	struct pmap * const curpmap = curlwp->l_proc->p_vmspace->vm_map.pmap;
    393 #endif
    394 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    395 	pv_entry_t pv = &mdpg->mdpg_first;
    396 	kcpuset_t *onproc;
    397 #ifdef MULTIPROCESSOR
    398 	kcpuset_create(&onproc, true);
    399 	KASSERT(onproc != NULL);
    400 #else
    401 	onproc = NULL;
    402 #endif
    403 	VM_PAGEMD_PVLIST_READLOCK(mdpg);
    404 	pmap_pvlist_check(mdpg);
    405 
    406 	if (pv->pv_pmap != NULL) {
    407 		for (; pv != NULL; pv = pv->pv_next) {
    408 #ifdef MULTIPROCESSOR
    409 			kcpuset_merge(onproc, pv->pv_pmap->pm_onproc);
    410 			if (kcpuset_match(onproc, kcpuset_running)) {
    411 				break;
    412 			}
    413 #else
    414 			if (pv->pv_pmap == curpmap) {
    415 				onproc = curcpu()->ci_data.cpu_kcpuset;
    416 				break;
    417 			}
    418 #endif
    419 		}
    420 	}
    421 	pmap_pvlist_check(mdpg);
    422 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    423 	kpreempt_disable();
    424 	pmap_md_page_syncicache(pg, onproc);
    425 	kpreempt_enable();
    426 #ifdef MULTIPROCESSOR
    427 	kcpuset_destroy(onproc);
    428 #endif
    429 }
    430 
    431 /*
    432  * Define the initial bounds of the kernel virtual address space.
    433  */
    434 void
    435 pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp)
    436 {
    437 
    438 	*vstartp = pmap_limits.virtual_start;
    439 	*vendp = pmap_limits.virtual_end;
    440 }
    441 
    442 vaddr_t
    443 pmap_growkernel(vaddr_t maxkvaddr)
    444 {
    445 	vaddr_t virtual_end = pmap_limits.virtual_end;
    446 	maxkvaddr = pmap_round_seg(maxkvaddr) - 1;
    447 
    448 	/*
    449 	 * Reserve PTEs for the new KVA space.
    450 	 */
    451 	for (; virtual_end < maxkvaddr; virtual_end += NBSEG) {
    452 		pmap_pte_reserve(pmap_kernel(), virtual_end, 0);
    453 	}
    454 
    455 	/*
    456 	 * Don't exceed VM_MAX_KERNEL_ADDRESS!
    457 	 */
    458 	if (virtual_end == 0 || virtual_end > VM_MAX_KERNEL_ADDRESS)
    459 		virtual_end = VM_MAX_KERNEL_ADDRESS;
    460 
    461 	/*
    462 	 * Update new end.
    463 	 */
    464 	pmap_limits.virtual_end = virtual_end;
    465 	return virtual_end;
    466 }
    467 
    468 /*
    469  * Bootstrap memory allocator (alternative to vm_bootstrap_steal_memory()).
    470  * This function allows for early dynamic memory allocation until the virtual
    471  * memory system has been bootstrapped.  After that point, either kmem_alloc
    472  * or malloc should be used.  This function works by stealing pages from the
    473  * (to be) managed page pool, then implicitly mapping the pages (by using
    474  * their direct mapped addresses) and zeroing them.
    475  *
    476  * It may be used once the physical memory segments have been pre-loaded
    477  * into the vm_physmem[] array.  Early memory allocation MUST use this
    478  * interface!  This cannot be used after vm_page_startup(), and will
    479  * generate a panic if tried.
    480  *
    481  * Note that this memory will never be freed, and in essence it is wired
    482  * down.
    483  *
    484  * We must adjust *vstartp and/or *vendp iff we use address space
    485  * from the kernel virtual address range defined by pmap_virtual_space().
    486  */
    487 vaddr_t
    488 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
    489 {
    490 	size_t npgs;
    491 	paddr_t pa;
    492 	vaddr_t va;
    493 
    494 	uvm_physseg_t maybe_bank = UVM_PHYSSEG_TYPE_INVALID;
    495 
    496 	size = round_page(size);
    497 	npgs = atop(size);
    498 
    499 	aprint_debug("%s: need %zu pages\n", __func__, npgs);
    500 
    501 	for (uvm_physseg_t bank = uvm_physseg_get_first();
    502 	     uvm_physseg_valid_p(bank);
    503 	     bank = uvm_physseg_get_next(bank)) {
    504 
    505 		if (uvm.page_init_done == true)
    506 			panic("pmap_steal_memory: called _after_ bootstrap");
    507 
    508 		aprint_debug("%s: seg %"PRIxPHYSSEG": %#"PRIxPADDR" %#"PRIxPADDR" %#"PRIxPADDR" %#"PRIxPADDR"\n",
    509 		    __func__, bank,
    510 		    uvm_physseg_get_avail_start(bank), uvm_physseg_get_start(bank),
    511 		    uvm_physseg_get_avail_end(bank), uvm_physseg_get_end(bank));
    512 
    513 		if (uvm_physseg_get_avail_start(bank) != uvm_physseg_get_start(bank)
    514 		    || uvm_physseg_get_avail_start(bank) >= uvm_physseg_get_avail_end(bank)) {
    515 			aprint_debug("%s: seg %"PRIxPHYSSEG": bad start\n", __func__, bank);
    516 			continue;
    517 		}
    518 
    519 		if (uvm_physseg_get_avail_end(bank) - uvm_physseg_get_avail_start(bank) < npgs) {
    520 			aprint_debug("%s: seg %"PRIxPHYSSEG": too small for %zu pages\n",
    521 			    __func__, bank, npgs);
    522 			continue;
    523 		}
    524 
    525 		if (!pmap_md_ok_to_steal_p(bank, npgs)) {
    526 			continue;
    527 		}
    528 
    529 		/*
    530 		 * Always try to allocate from the segment with the least
    531 		 * amount of space left.
    532 		 */
    533 #define VM_PHYSMEM_SPACE(b)	((uvm_physseg_get_avail_end(b)) - (uvm_physseg_get_avail_start(b)))
    534 		if (uvm_physseg_valid_p(maybe_bank) == false
    535 		    || VM_PHYSMEM_SPACE(bank) < VM_PHYSMEM_SPACE(maybe_bank)) {
    536 			maybe_bank = bank;
    537 		}
    538 	}
    539 
    540 	if (uvm_physseg_valid_p(maybe_bank)) {
    541 		const uvm_physseg_t bank = maybe_bank;
    542 
    543 		/*
    544 		 * There are enough pages here; steal them!
    545 		 */
    546 		pa = ptoa(uvm_physseg_get_start(bank));
    547 		uvm_physseg_unplug(atop(pa), npgs);
    548 
    549 		aprint_debug("%s: seg %"PRIxPHYSSEG": %zu pages stolen (%#"PRIxPADDR" left)\n",
    550 		    __func__, bank, npgs, VM_PHYSMEM_SPACE(bank));
    551 
    552 		va = pmap_md_map_poolpage(pa, size);
    553 		memset((void *)va, 0, size);
    554 		return va;
    555 	}
    556 
    557 	/*
    558 	 * If we got here, there was no memory left.
    559 	 */
    560 	panic("pmap_steal_memory: no memory to steal %zu pages", npgs);
    561 }
    562 
    563 /*
    564  *	Bootstrap the system enough to run with virtual memory.
    565  *	(Common routine called by machine-dependent bootstrap code.)
    566  */
    567 void
    568 pmap_bootstrap_common(void)
    569 {
    570 	pmap_tlb_miss_lock_init();
    571 }
    572 
    573 /*
    574  *	Initialize the pmap module.
    575  *	Called by vm_init, to initialize any structures that the pmap
    576  *	system needs to map virtual memory.
    577  */
    578 void
    579 pmap_init(void)
    580 {
    581 	UVMHIST_INIT_STATIC(pmapexechist, pmapexechistbuf);
    582 	UVMHIST_INIT_STATIC(pmaphist, pmaphistbuf);
    583 
    584 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    585 
    586 	/*
    587 	 * Initialize the segtab lock.
    588 	 */
    589 	mutex_init(&pmap_segtab_lock, MUTEX_DEFAULT, IPL_HIGH);
    590 
    591 	/*
    592 	 * Set a low water mark on the pv_entry pool, so that we are
    593 	 * more likely to have these around even in extreme memory
    594 	 * starvation.
    595 	 */
    596 	pool_setlowat(&pmap_pv_pool, pmap_pv_lowat);
    597 
    598 	/*
    599 	 * Set the page colormask but allow pmap_md_init to override it.
    600 	 */
    601 	pmap_page_colormask = ptoa(uvmexp.colormask);
    602 
    603 	pmap_md_init();
    604 
    605 	/*
    606 	 * Now it is safe to enable pv entry recording.
    607 	 */
    608 	pmap_initialized = true;
    609 }
    610 
    611 /*
    612  *	Create and return a physical map.
    613  *
    614  *	If the size specified for the map
    615  *	is zero, the map is an actual physical
    616  *	map, and may be referenced by the
    617  *	hardware.
    618  *
    619  *	If the size specified is non-zero,
    620  *	the map will be used in software only, and
    621  *	is bounded by that size.
    622  */
    623 pmap_t
    624 pmap_create(void)
    625 {
    626 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    627 	PMAP_COUNT(create);
    628 
    629 	pmap_t pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
    630 	memset(pmap, 0, PMAP_SIZE);
    631 
    632 	KASSERT(pmap->pm_pai[0].pai_link.le_prev == NULL);
    633 
    634 	pmap->pm_count = 1;
    635 	pmap->pm_minaddr = VM_MIN_ADDRESS;
    636 	pmap->pm_maxaddr = VM_MAXUSER_ADDRESS;
    637 
    638 	pmap_segtab_init(pmap);
    639 
    640 #ifdef MULTIPROCESSOR
    641 	kcpuset_create(&pmap->pm_active, true);
    642 	kcpuset_create(&pmap->pm_onproc, true);
    643 	KASSERT(pmap->pm_active != NULL);
    644 	KASSERT(pmap->pm_onproc != NULL);
    645 #endif
    646 
    647 	UVMHIST_LOG(pmaphist, " <-- done (pmap=%#jx)", (uintptr_t)pmap,
    648 	    0, 0, 0);
    649 
    650 	return pmap;
    651 }
    652 
    653 /*
    654  *	Retire the given physical map from service.
    655  *	Should only be called if the map contains
    656  *	no valid mappings.
    657  */
    658 void
    659 pmap_destroy(pmap_t pmap)
    660 {
    661 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    662 	UVMHIST_LOG(pmaphist, "(pmap=%#jx)", (uintptr_t)pmap, 0, 0, 0);
    663 
    664 	if (atomic_dec_uint_nv(&pmap->pm_count) > 0) {
    665 		PMAP_COUNT(dereference);
    666 		UVMHIST_LOG(pmaphist, " <-- done (deref)", 0, 0, 0, 0);
    667 		return;
    668 	}
    669 
    670 	PMAP_COUNT(destroy);
    671 	KASSERT(pmap->pm_count == 0);
    672 	kpreempt_disable();
    673 	pmap_tlb_miss_lock_enter();
    674 	pmap_tlb_asid_release_all(pmap);
    675 	pmap_segtab_destroy(pmap, NULL, 0);
    676 	pmap_tlb_miss_lock_exit();
    677 
    678 #ifdef MULTIPROCESSOR
    679 	kcpuset_destroy(pmap->pm_active);
    680 	kcpuset_destroy(pmap->pm_onproc);
    681 	pmap->pm_active = NULL;
    682 	pmap->pm_onproc = NULL;
    683 #endif
    684 
    685 	pool_put(&pmap_pmap_pool, pmap);
    686 	kpreempt_enable();
    687 
    688 	UVMHIST_LOG(pmaphist, " <-- done (freed)", 0, 0, 0, 0);
    689 }
    690 
    691 /*
    692  *	Add a reference to the specified pmap.
    693  */
    694 void
    695 pmap_reference(pmap_t pmap)
    696 {
    697 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    698 	UVMHIST_LOG(pmaphist, "(pmap=%#jx)", (uintptr_t)pmap, 0, 0, 0);
    699 	PMAP_COUNT(reference);
    700 
    701 	if (pmap != NULL) {
    702 		atomic_inc_uint(&pmap->pm_count);
    703 	}
    704 
    705 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    706 }
    707 
    708 /*
    709  *	Make a new pmap (vmspace) active for the given process.
    710  */
    711 void
    712 pmap_activate(struct lwp *l)
    713 {
    714 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    715 
    716 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    717 	UVMHIST_LOG(pmaphist, "(l=%#jx pmap=%#jx)", (uintptr_t)l,
    718 	    (uintptr_t)pmap, 0, 0);
    719 	PMAP_COUNT(activate);
    720 
    721 	kpreempt_disable();
    722 	pmap_tlb_miss_lock_enter();
    723 	pmap_tlb_asid_acquire(pmap, l);
    724 	if (l == curlwp) {
    725 		pmap_segtab_activate(pmap, l);
    726 	}
    727 	pmap_tlb_miss_lock_exit();
    728 	kpreempt_enable();
    729 
    730 	UVMHIST_LOG(pmaphist, " <-- done (%ju:%ju)", l->l_proc->p_pid,
    731 	    l->l_lid, 0, 0);
    732 }
    733 
    734 /*
    735  * Remove this page from all physical maps in which it resides.
    736  * Reflects back modify bits to the pager.
    737  */
    738 void
    739 pmap_page_remove(struct vm_page *pg)
    740 {
    741 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    742 
    743 	kpreempt_disable();
    744 	VM_PAGEMD_PVLIST_LOCK(mdpg);
    745 	pmap_pvlist_check(mdpg);
    746 
    747 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    748 
    749 	UVMHIST_LOG(pmapexechist, "pg %#jx (pa %#jx) [page removed]: "
    750 	    "execpage cleared", (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), 0, 0);
    751 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
    752 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE|VM_PAGEMD_UNCACHED);
    753 #else
    754 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
    755 #endif
    756 	PMAP_COUNT(exec_uncached_remove);
    757 
    758 	pv_entry_t pv = &mdpg->mdpg_first;
    759 	if (pv->pv_pmap == NULL) {
    760 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    761 		kpreempt_enable();
    762 		UVMHIST_LOG(pmaphist, " <-- done (empty)", 0, 0, 0, 0);
    763 		return;
    764 	}
    765 
    766 	pv_entry_t npv;
    767 	pv_entry_t pvp = NULL;
    768 
    769 	for (; pv != NULL; pv = npv) {
    770 		npv = pv->pv_next;
    771 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
    772 		if (PV_ISKENTER_P(pv)) {
    773 			UVMHIST_LOG(pmaphist, " pv %#jx pmap %#jx va %jx"
    774 			    " skip", (uintptr_t)pv, (uintptr_t)pv->pv_pmap,
    775 			    pv->pv_va, 0);
    776 
    777 			KASSERT(pv->pv_pmap == pmap_kernel());
    778 
    779 			/* Assume no more - it'll get fixed if there are */
    780 			pv->pv_next = NULL;
    781 
    782 			/*
    783 			 * pvp is non-null when we already have a PV_KENTER
    784 			 * pv in pvh_first; otherwise we haven't seen a
    785 			 * PV_KENTER pv and we need to copy this one to
    786 			 * pvh_first
    787 			 */
    788 			if (pvp) {
    789 				/*
    790 				 * The previous PV_KENTER pv needs to point to
    791 				 * this PV_KENTER pv
    792 				 */
    793 				pvp->pv_next = pv;
    794 			} else {
    795 				pv_entry_t fpv = &mdpg->mdpg_first;
    796 				*fpv = *pv;
    797 				KASSERT(fpv->pv_pmap == pmap_kernel());
    798 			}
    799 			pvp = pv;
    800 			continue;
    801 		}
    802 #endif
    803 		const pmap_t pmap = pv->pv_pmap;
    804 		vaddr_t va = trunc_page(pv->pv_va);
    805 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
    806 		KASSERTMSG(ptep != NULL, "%#"PRIxVADDR " %#"PRIxVADDR, va,
    807 		    pmap_limits.virtual_end);
    808 		pt_entry_t pte = *ptep;
    809 		UVMHIST_LOG(pmaphist, " pv %#jx pmap %#jx va %jx"
    810 		    " pte %jx", (uintptr_t)pv, (uintptr_t)pmap, va,
    811 		    pte_value(pte));
    812 		if (!pte_valid_p(pte))
    813 			continue;
    814 		const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    815 		if (is_kernel_pmap_p) {
    816 			PMAP_COUNT(remove_kernel_pages);
    817 		} else {
    818 			PMAP_COUNT(remove_user_pages);
    819 		}
    820 		if (pte_wired_p(pte))
    821 			pmap->pm_stats.wired_count--;
    822 		pmap->pm_stats.resident_count--;
    823 
    824 		pmap_tlb_miss_lock_enter();
    825 		const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    826 		pte_set(ptep, npte);
    827 		if (__predict_true(!(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE))) {
    828 			/*
    829 			 * Flush the TLB for the given address.
    830 			 */
    831 			pmap_tlb_invalidate_addr(pmap, va);
    832 		}
    833 		pmap_tlb_miss_lock_exit();
    834 
    835 		/*
    836 		 * non-null means this is a non-pvh_first pv, so we should
    837 		 * free it.
    838 		 */
    839 		if (pvp) {
    840 			KASSERT(pvp->pv_pmap == pmap_kernel());
    841 			KASSERT(pvp->pv_next == NULL);
    842 			pmap_pv_free(pv);
    843 		} else {
    844 			pv->pv_pmap = NULL;
    845 			pv->pv_next = NULL;
    846 		}
    847 	}
    848 
    849 	pmap_pvlist_check(mdpg);
    850 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    851 	kpreempt_enable();
    852 
    853 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    854 }
    855 
    856 
    857 /*
    858  *	Make a previously active pmap (vmspace) inactive.
    859  */
    860 void
    861 pmap_deactivate(struct lwp *l)
    862 {
    863 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    864 
    865 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    866 	UVMHIST_LOG(pmaphist, "(l=%#jx pmap=%#jx)", (uintptr_t)l,
    867 	    (uintptr_t)pmap, 0, 0);
    868 	PMAP_COUNT(deactivate);
    869 
    870 	kpreempt_disable();
    871 	KASSERT(l == curlwp || l->l_cpu == curlwp->l_cpu);
    872 	pmap_tlb_miss_lock_enter();
    873 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    874 #ifdef _LP64
    875 	curcpu()->ci_pmap_user_seg0tab = NULL;
    876 #endif
    877 	pmap_tlb_asid_deactivate(pmap);
    878 	pmap_tlb_miss_lock_exit();
    879 	kpreempt_enable();
    880 
    881 	UVMHIST_LOG(pmaphist, " <-- done (%ju:%ju)", l->l_proc->p_pid,
    882 	    l->l_lid, 0, 0);
    883 }
    884 
    885 void
    886 pmap_update(struct pmap *pmap)
    887 {
    888 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    889 	UVMHIST_LOG(pmaphist, "(pmap=%#jx)", (uintptr_t)pmap, 0, 0, 0);
    890 	PMAP_COUNT(update);
    891 
    892 	kpreempt_disable();
    893 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    894 	u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
    895 	if (pending && pmap_tlb_shootdown_bystanders(pmap))
    896 		PMAP_COUNT(shootdown_ipis);
    897 #endif
    898 	pmap_tlb_miss_lock_enter();
    899 #if defined(DEBUG) && !defined(MULTIPROCESSOR)
    900 	pmap_tlb_check(pmap, pmap_md_tlb_check_entry);
    901 #endif /* DEBUG */
    902 
    903 	/*
    904 	 * If pmap_remove_all was called, we deactivated ourselves and nuked
    905 	 * our ASID.  Now we have to reactivate ourselves.
    906 	 */
    907 	if (__predict_false(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE)) {
    908 		pmap->pm_flags ^= PMAP_DEFERRED_ACTIVATE;
    909 		pmap_tlb_asid_acquire(pmap, curlwp);
    910 		pmap_segtab_activate(pmap, curlwp);
    911 	}
    912 	pmap_tlb_miss_lock_exit();
    913 	kpreempt_enable();
    914 
    915 	UVMHIST_LOG(pmaphist, " <-- done (kernel=%#jx)",
    916 		    (pmap == pmap_kernel() ? 1 : 0), 0, 0, 0);
    917 }
    918 
    919 /*
    920  *	Remove the given range of addresses from the specified map.
    921  *
    922  *	It is assumed that the start and end are properly
    923  *	rounded to the page size.
    924  */
    925 
    926 static bool
    927 pmap_pte_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    928 	uintptr_t flags)
    929 {
    930 	const pt_entry_t npte = flags;
    931 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    932 
    933 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    934 	UVMHIST_LOG(pmaphist, "(pmap=%#jx kernel=%c va=%#jx..%#jx)",
    935 	    (uintptr_t)pmap, (is_kernel_pmap_p ? 1 : 0), sva, eva);
    936 	UVMHIST_LOG(pmaphist, "ptep=%#jx, flags(npte)=%#jx",
    937 	    (uintptr_t)ptep, flags, 0, 0);
    938 
    939 	KASSERT(kpreempt_disabled());
    940 
    941 	for (; sva < eva; sva += NBPG, ptep++) {
    942 		const pt_entry_t pte = *ptep;
    943 		if (!pte_valid_p(pte))
    944 			continue;
    945 		if (is_kernel_pmap_p) {
    946 			PMAP_COUNT(remove_kernel_pages);
    947 		} else {
    948 			PMAP_COUNT(remove_user_pages);
    949 		}
    950 		if (pte_wired_p(pte))
    951 			pmap->pm_stats.wired_count--;
    952 		pmap->pm_stats.resident_count--;
    953 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
    954 		if (__predict_true(pg != NULL)) {
    955 			pmap_remove_pv(pmap, sva, pg, pte_modified_p(pte));
    956 		}
    957 		pmap_tlb_miss_lock_enter();
    958 		pte_set(ptep, npte);
    959 		if (__predict_true(!(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE))) {
    960 
    961 			/*
    962 			 * Flush the TLB for the given address.
    963 			 */
    964 			pmap_tlb_invalidate_addr(pmap, sva);
    965 		}
    966 		pmap_tlb_miss_lock_exit();
    967 	}
    968 
    969 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    970 
    971 	return false;
    972 }
    973 
    974 void
    975 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva)
    976 {
    977 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    978 	const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    979 
    980 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    981 	UVMHIST_LOG(pmaphist, "(pmap=%#jx, va=%#jx..%#jx)",
    982 	    (uintptr_t)pmap, sva, eva, 0);
    983 
    984 	if (is_kernel_pmap_p) {
    985 		PMAP_COUNT(remove_kernel_calls);
    986 	} else {
    987 		PMAP_COUNT(remove_user_calls);
    988 	}
    989 #ifdef PMAP_FAULTINFO
    990 	curpcb->pcb_faultinfo.pfi_faultaddr = 0;
    991 	curpcb->pcb_faultinfo.pfi_repeats = 0;
    992 	curpcb->pcb_faultinfo.pfi_faultpte = NULL;
    993 #endif
    994 	kpreempt_disable();
    995 	pmap_addr_range_check(pmap, sva, eva, __func__);
    996 	pmap_pte_process(pmap, sva, eva, pmap_pte_remove, npte);
    997 	kpreempt_enable();
    998 
    999 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1000 }
   1001 
   1002 /*
   1003  *	pmap_page_protect:
   1004  *
   1005  *	Lower the permission for all mappings to a given page.
   1006  */
   1007 void
   1008 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   1009 {
   1010 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1011 	pv_entry_t pv;
   1012 	vaddr_t va;
   1013 
   1014 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1015 	UVMHIST_LOG(pmaphist, "(pg=%#jx (pa %#jx) prot=%#jx)",
   1016 	    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), prot, 0);
   1017 	PMAP_COUNT(page_protect);
   1018 
   1019 	switch (prot) {
   1020 	case VM_PROT_READ|VM_PROT_WRITE:
   1021 	case VM_PROT_ALL:
   1022 		break;
   1023 
   1024 	/* copy_on_write */
   1025 	case VM_PROT_READ:
   1026 	case VM_PROT_READ|VM_PROT_EXECUTE:
   1027 		pv = &mdpg->mdpg_first;
   1028 		kpreempt_disable();
   1029 		VM_PAGEMD_PVLIST_READLOCK(mdpg);
   1030 		pmap_pvlist_check(mdpg);
   1031 		/*
   1032 		 * Loop over all current mappings setting/clearing as
   1033 		 * appropriate.
   1034 		 */
   1035 		if (pv->pv_pmap != NULL) {
   1036 			while (pv != NULL) {
   1037 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1038 				if (PV_ISKENTER_P(pv)) {
   1039 					pv = pv->pv_next;
   1040 					continue;
   1041 				}
   1042 #endif
   1043 				const pmap_t pmap = pv->pv_pmap;
   1044 				va = trunc_page(pv->pv_va);
   1045 				const uintptr_t gen =
   1046 				    VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1047 				pmap_protect(pmap, va, va + PAGE_SIZE, prot);
   1048 				KASSERT(pv->pv_pmap == pmap);
   1049 				pmap_update(pmap);
   1050 				if (gen != VM_PAGEMD_PVLIST_READLOCK(mdpg)) {
   1051 					pv = &mdpg->mdpg_first;
   1052 				} else {
   1053 					pv = pv->pv_next;
   1054 				}
   1055 				pmap_pvlist_check(mdpg);
   1056 			}
   1057 		}
   1058 		pmap_pvlist_check(mdpg);
   1059 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1060 		kpreempt_enable();
   1061 		break;
   1062 
   1063 	/* remove_all */
   1064 	default:
   1065 		pmap_page_remove(pg);
   1066 	}
   1067 
   1068 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1069 }
   1070 
   1071 static bool
   1072 pmap_pte_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1073 	uintptr_t flags)
   1074 {
   1075 	const vm_prot_t prot = (flags & VM_PROT_ALL);
   1076 
   1077 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1078 	UVMHIST_LOG(pmaphist, "(pmap=%#jx kernel=%jx va=%#jx..%#jx)",
   1079 	    (uintptr_t)pmap, (pmap == pmap_kernel() ? 1 : 0), sva, eva);
   1080 	UVMHIST_LOG(pmaphist, "ptep=%#jx, flags(npte)=%#jx)",
   1081 	    (uintptr_t)ptep, flags, 0, 0);
   1082 
   1083 	KASSERT(kpreempt_disabled());
   1084 	/*
   1085 	 * Change protection on every valid mapping within this segment.
   1086 	 */
   1087 	for (; sva < eva; sva += NBPG, ptep++) {
   1088 		pt_entry_t pte = *ptep;
   1089 		if (!pte_valid_p(pte))
   1090 			continue;
   1091 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
   1092 		if (pg != NULL && pte_modified_p(pte)) {
   1093 			struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1094 			if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1095 				KASSERT(!VM_PAGEMD_PVLIST_EMPTY_P(mdpg));
   1096 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1097 				if (VM_PAGEMD_CACHED_P(mdpg)) {
   1098 #endif
   1099 					UVMHIST_LOG(pmapexechist,
   1100 					    "pg %#jx (pa %#jx): "
   1101 					    "syncicached performed",
   1102 					    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg),
   1103 					    0, 0);
   1104 					pmap_page_syncicache(pg);
   1105 					PMAP_COUNT(exec_synced_protect);
   1106 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1107 				}
   1108 #endif
   1109 			}
   1110 		}
   1111 		pte = pte_prot_downgrade(pte, prot);
   1112 		if (*ptep != pte) {
   1113 			pmap_tlb_miss_lock_enter();
   1114 			pte_set(ptep, pte);
   1115 			/*
   1116 			 * Update the TLB if needed.
   1117 			 */
   1118 			pmap_tlb_update_addr(pmap, sva, pte, PMAP_TLB_NEED_IPI);
   1119 			pmap_tlb_miss_lock_exit();
   1120 		}
   1121 	}
   1122 
   1123 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1124 
   1125 	return false;
   1126 }
   1127 
   1128 /*
   1129  *	Set the physical protection on the
   1130  *	specified range of this map as requested.
   1131  */
   1132 void
   1133 pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   1134 {
   1135 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1136 	UVMHIST_LOG(pmaphist, "(pmap=%#jx, va=%#jx..%#jx, prot=%ju)",
   1137 	    (uintptr_t)pmap, sva, eva, prot);
   1138 	PMAP_COUNT(protect);
   1139 
   1140 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
   1141 		pmap_remove(pmap, sva, eva);
   1142 		UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1143 		return;
   1144 	}
   1145 
   1146 	/*
   1147 	 * Change protection on every valid mapping within this segment.
   1148 	 */
   1149 	kpreempt_disable();
   1150 	pmap_addr_range_check(pmap, sva, eva, __func__);
   1151 	pmap_pte_process(pmap, sva, eva, pmap_pte_protect, prot);
   1152 	kpreempt_enable();
   1153 
   1154 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1155 }
   1156 
   1157 #if defined(PMAP_VIRTUAL_CACHE_ALIASES) && !defined(PMAP_NO_PV_UNCACHED)
   1158 /*
   1159  *	pmap_page_cache:
   1160  *
   1161  *	Change all mappings of a managed page to cached/uncached.
   1162  */
   1163 void
   1164 pmap_page_cache(struct vm_page *pg, bool cached)
   1165 {
   1166 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1167 
   1168 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1169 	UVMHIST_LOG(pmaphist, "(pg=%#jx (pa %#jx) cached=%jd)",
   1170 	    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), cached, 0);
   1171 
   1172 	KASSERT(kpreempt_disabled());
   1173 	KASSERT(VM_PAGEMD_PVLIST_LOCKED_P(mdpg));
   1174 
   1175 	if (cached) {
   1176 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1177 		PMAP_COUNT(page_cache_restorations);
   1178 	} else {
   1179 		pmap_page_set_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1180 		PMAP_COUNT(page_cache_evictions);
   1181 	}
   1182 
   1183 	for (pv_entry_t pv = &mdpg->mdpg_first; pv != NULL; pv = pv->pv_next) {
   1184 		pmap_t pmap = pv->pv_pmap;
   1185 		vaddr_t va = trunc_page(pv->pv_va);
   1186 
   1187 		KASSERT(pmap != NULL);
   1188 		KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1189 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1190 		if (ptep == NULL)
   1191 			continue;
   1192 		pt_entry_t pte = *ptep;
   1193 		if (pte_valid_p(pte)) {
   1194 			pte = pte_cached_change(pte, cached);
   1195 			pmap_tlb_miss_lock_enter();
   1196 			pte_set(ptep, pte);
   1197 			pmap_tlb_update_addr(pmap, va, pte, PMAP_TLB_NEED_IPI);
   1198 			pmap_tlb_miss_lock_exit();
   1199 		}
   1200 	}
   1201 
   1202 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1203 }
   1204 #endif	/* PMAP_VIRTUAL_CACHE_ALIASES && !PMAP_NO_PV_UNCACHED */
   1205 
   1206 /*
   1207  *	Insert the given physical page (p) at
   1208  *	the specified virtual address (v) in the
   1209  *	target physical map with the protection requested.
   1210  *
   1211  *	If specified, the page will be wired down, meaning
   1212  *	that the related pte can not be reclaimed.
   1213  *
   1214  *	NB:  This is the only routine which MAY NOT lazy-evaluate
   1215  *	or lose information.  That is, this routine must actually
   1216  *	insert this page into the given map NOW.
   1217  */
   1218 int
   1219 pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1220 {
   1221 	const bool wired = (flags & PMAP_WIRED) != 0;
   1222 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
   1223 	u_int update_flags = (flags & VM_PROT_ALL) != 0 ? PMAP_TLB_INSERT : 0;
   1224 #ifdef UVMHIST
   1225 	struct kern_history * const histp =
   1226 	    ((prot & VM_PROT_EXECUTE) ? &pmapexechist : &pmaphist);
   1227 #endif
   1228 
   1229 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(*histp);
   1230 	UVMHIST_LOG(*histp, "(pmap=%#jx, va=%#jx, pa=%#jx",
   1231 	    (uintptr_t)pmap, va, pa, 0);
   1232 	UVMHIST_LOG(*histp, "prot=%#jx flags=%#jx)", prot, flags, 0, 0);
   1233 
   1234 	const bool good_color = PMAP_PAGE_COLOROK_P(pa, va);
   1235 	if (is_kernel_pmap_p) {
   1236 		PMAP_COUNT(kernel_mappings);
   1237 		if (!good_color)
   1238 			PMAP_COUNT(kernel_mappings_bad);
   1239 	} else {
   1240 		PMAP_COUNT(user_mappings);
   1241 		if (!good_color)
   1242 			PMAP_COUNT(user_mappings_bad);
   1243 	}
   1244 	pmap_addr_range_check(pmap, va, va, __func__);
   1245 
   1246 	KASSERTMSG(prot & VM_PROT_READ, "no READ (%#x) in prot %#x",
   1247 	    VM_PROT_READ, prot);
   1248 
   1249 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1250 	struct vm_page_md * const mdpg = (pg ? VM_PAGE_TO_MD(pg) : NULL);
   1251 
   1252 	if (pg) {
   1253 		/* Set page referenced/modified status based on flags */
   1254 		if (flags & VM_PROT_WRITE) {
   1255 			pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1256 		} else if (flags & VM_PROT_ALL) {
   1257 			pmap_page_set_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1258 		}
   1259 
   1260 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1261 		if (!VM_PAGEMD_CACHED_P(mdpg)) {
   1262 			flags |= PMAP_NOCACHE;
   1263 			PMAP_COUNT(uncached_mappings);
   1264 		}
   1265 #endif
   1266 
   1267 		PMAP_COUNT(managed_mappings);
   1268 	} else {
   1269 		/*
   1270 		 * Assumption: if it is not part of our managed memory
   1271 		 * then it must be device memory which may be volatile.
   1272 		 */
   1273 		if ((flags & PMAP_CACHE_MASK) == 0)
   1274 			flags |= PMAP_NOCACHE;
   1275 		PMAP_COUNT(unmanaged_mappings);
   1276 	}
   1277 
   1278 	pt_entry_t npte = pte_make_enter(pa, mdpg, prot, flags,
   1279 	    is_kernel_pmap_p);
   1280 
   1281 	kpreempt_disable();
   1282 
   1283 	pt_entry_t * const ptep = pmap_pte_reserve(pmap, va, flags);
   1284 	if (__predict_false(ptep == NULL)) {
   1285 		kpreempt_enable();
   1286 		UVMHIST_LOG(*histp, " <-- ENOMEM", 0, 0, 0, 0);
   1287 		return ENOMEM;
   1288 	}
   1289 	const pt_entry_t opte = *ptep;
   1290 	const bool resident = pte_valid_p(opte);
   1291 	bool remap = false;
   1292 	if (resident) {
   1293 		if (pte_to_paddr(opte) != pa) {
   1294 			KASSERT(!is_kernel_pmap_p);
   1295 		    	const pt_entry_t rpte = pte_nv_entry(false);
   1296 
   1297 			pmap_addr_range_check(pmap, va, va + NBPG, __func__);
   1298 			pmap_pte_process(pmap, va, va + NBPG, pmap_pte_remove,
   1299 			    rpte);
   1300 			PMAP_COUNT(user_mappings_changed);
   1301 			remap = true;
   1302 		}
   1303 		update_flags |= PMAP_TLB_NEED_IPI;
   1304 	}
   1305 
   1306 	if (!resident || remap) {
   1307 		pmap->pm_stats.resident_count++;
   1308 	}
   1309 
   1310 	/* Done after case that may sleep/return. */
   1311 	if (pg)
   1312 		pmap_enter_pv(pmap, va, pg, &npte, 0);
   1313 
   1314 	/*
   1315 	 * Now validate mapping with desired protection/wiring.
   1316 	 * Assume uniform modified and referenced status for all
   1317 	 * MIPS pages in a MACH page.
   1318 	 */
   1319 	if (wired) {
   1320 		pmap->pm_stats.wired_count++;
   1321 		npte = pte_wire_entry(npte);
   1322 	}
   1323 
   1324 	UVMHIST_LOG(*histp, "new pte %#jx (pa %#jx)",
   1325 	    pte_value(npte), pa, 0, 0);
   1326 
   1327 	KASSERT(pte_valid_p(npte));
   1328 
   1329 	pmap_tlb_miss_lock_enter();
   1330 	pte_set(ptep, npte);
   1331 	pmap_tlb_update_addr(pmap, va, npte, update_flags);
   1332 	pmap_tlb_miss_lock_exit();
   1333 	kpreempt_enable();
   1334 
   1335 	if (pg != NULL && (prot == (VM_PROT_READ | VM_PROT_EXECUTE))) {
   1336 		KASSERT(mdpg != NULL);
   1337 		PMAP_COUNT(exec_mappings);
   1338 		if (!VM_PAGEMD_EXECPAGE_P(mdpg) && pte_cached_p(npte)) {
   1339 			if (!pte_deferred_exec_p(npte)) {
   1340 				UVMHIST_LOG(*histp, "va=%#jx pg %#jx: "
   1341 				    "immediate syncicache",
   1342 				    va, (uintptr_t)pg, 0, 0);
   1343 				pmap_page_syncicache(pg);
   1344 				pmap_page_set_attributes(mdpg,
   1345 				    VM_PAGEMD_EXECPAGE);
   1346 				PMAP_COUNT(exec_synced_mappings);
   1347 			} else {
   1348 				UVMHIST_LOG(*histp, "va=%#jx pg %#jx: defer "
   1349 				    "syncicache: pte %#jx",
   1350 				    va, (uintptr_t)pg, npte, 0);
   1351 			}
   1352 		} else {
   1353 			UVMHIST_LOG(*histp,
   1354 			    "va=%#jx pg %#jx: no syncicache cached %jd",
   1355 			    va, (uintptr_t)pg, pte_cached_p(npte), 0);
   1356 		}
   1357 	} else if (pg != NULL && (prot & VM_PROT_EXECUTE)) {
   1358 		KASSERT(mdpg != NULL);
   1359 		KASSERT(prot & VM_PROT_WRITE);
   1360 		PMAP_COUNT(exec_mappings);
   1361 		pmap_page_syncicache(pg);
   1362 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1363 		UVMHIST_LOG(*histp,
   1364 		    "va=%#jx pg %#jx: immediate syncicache (writeable)",
   1365 		    va, (uintptr_t)pg, 0, 0);
   1366 	}
   1367 
   1368 	UVMHIST_LOG(*histp, " <-- 0 (OK)", 0, 0, 0, 0);
   1369 	return 0;
   1370 }
   1371 
   1372 void
   1373 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1374 {
   1375 	pmap_t pmap = pmap_kernel();
   1376 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1377 	struct vm_page_md * const mdpg = (pg ? VM_PAGE_TO_MD(pg) : NULL);
   1378 
   1379 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1380 	UVMHIST_LOG(pmaphist, "(va=%#jx pa=%#jx prot=%ju, flags=%#jx)",
   1381 	    va, pa, prot, flags);
   1382 	PMAP_COUNT(kenter_pa);
   1383 
   1384 	if (mdpg == NULL) {
   1385 		PMAP_COUNT(kenter_pa_unmanaged);
   1386 		if ((flags & PMAP_CACHE_MASK) == 0)
   1387 			flags |= PMAP_NOCACHE;
   1388 	} else {
   1389 		if ((flags & PMAP_NOCACHE) == 0 && !PMAP_PAGE_COLOROK_P(pa, va))
   1390 			PMAP_COUNT(kenter_pa_bad);
   1391 	}
   1392 
   1393 	pt_entry_t npte = pte_make_kenter_pa(pa, mdpg, prot, flags);
   1394 	kpreempt_disable();
   1395 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1396 	KASSERTMSG(ptep != NULL, "%#"PRIxVADDR " %#"PRIxVADDR, va,
   1397 	    pmap_limits.virtual_end);
   1398 	KASSERT(!pte_valid_p(*ptep));
   1399 
   1400 	/*
   1401 	 * No need to track non-managed pages or PMAP_KMPAGEs pages for aliases
   1402 	 */
   1403 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1404 	if (pg != NULL && (flags & PMAP_KMPAGE) == 0
   1405 	    && pmap_md_virtual_cache_aliasing_p()) {
   1406 		pmap_enter_pv(pmap, va, pg, &npte, PV_KENTER);
   1407 	}
   1408 #endif
   1409 
   1410 	/*
   1411 	 * We have the option to force this mapping into the TLB but we
   1412 	 * don't.  Instead let the next reference to the page do it.
   1413 	 */
   1414 	pmap_tlb_miss_lock_enter();
   1415 	pte_set(ptep, npte);
   1416 	pmap_tlb_update_addr(pmap_kernel(), va, npte, 0);
   1417 	pmap_tlb_miss_lock_exit();
   1418 	kpreempt_enable();
   1419 #if DEBUG > 1
   1420 	for (u_int i = 0; i < PAGE_SIZE / sizeof(long); i++) {
   1421 		if (((long *)va)[i] != ((long *)pa)[i])
   1422 			panic("%s: contents (%lx) of va %#"PRIxVADDR
   1423 			    " != contents (%lx) of pa %#"PRIxPADDR, __func__,
   1424 			    ((long *)va)[i], va, ((long *)pa)[i], pa);
   1425 	}
   1426 #endif
   1427 
   1428 	UVMHIST_LOG(pmaphist, " <-- done (ptep=%#jx)", (uintptr_t)ptep, 0, 0,
   1429 	    0);
   1430 }
   1431 
   1432 /*
   1433  *	Remove the given range of addresses from the kernel map.
   1434  *
   1435  *	It is assumed that the start and end are properly
   1436  *	rounded to the page size.
   1437  */
   1438 
   1439 static bool
   1440 pmap_pte_kremove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1441 	uintptr_t flags)
   1442 {
   1443 	const pt_entry_t new_pte = pte_nv_entry(true);
   1444 
   1445 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1446 	UVMHIST_LOG(pmaphist,
   1447 	    "(pmap=%#jx, sva=%#jx eva=%#jx ptep=%#jx)",
   1448 	    (uintptr_t)pmap, sva, eva, (uintptr_t)ptep);
   1449 
   1450 	KASSERT(kpreempt_disabled());
   1451 
   1452 	for (; sva < eva; sva += NBPG, ptep++) {
   1453 		pt_entry_t pte = *ptep;
   1454 		if (!pte_valid_p(pte))
   1455 			continue;
   1456 
   1457 		PMAP_COUNT(kremove_pages);
   1458 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1459 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
   1460 		if (pg != NULL && pmap_md_virtual_cache_aliasing_p()) {
   1461 			pmap_remove_pv(pmap, sva, pg, !pte_readonly_p(pte));
   1462 		}
   1463 #endif
   1464 
   1465 		pmap_tlb_miss_lock_enter();
   1466 		pte_set(ptep, new_pte);
   1467 		pmap_tlb_invalidate_addr(pmap, sva);
   1468 		pmap_tlb_miss_lock_exit();
   1469 	}
   1470 
   1471 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1472 
   1473 	return false;
   1474 }
   1475 
   1476 void
   1477 pmap_kremove(vaddr_t va, vsize_t len)
   1478 {
   1479 	const vaddr_t sva = trunc_page(va);
   1480 	const vaddr_t eva = round_page(va + len);
   1481 
   1482 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1483 	UVMHIST_LOG(pmaphist, "(va=%#jx len=%#jx)", va, len, 0, 0);
   1484 
   1485 	kpreempt_disable();
   1486 	pmap_pte_process(pmap_kernel(), sva, eva, pmap_pte_kremove, 0);
   1487 	kpreempt_enable();
   1488 
   1489 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1490 }
   1491 
   1492 void
   1493 pmap_remove_all(struct pmap *pmap)
   1494 {
   1495 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1496 	UVMHIST_LOG(pmaphist, "(pm=%#jx)", (uintptr_t)pmap, 0, 0, 0);
   1497 
   1498 	KASSERT(pmap != pmap_kernel());
   1499 
   1500 	kpreempt_disable();
   1501 	/*
   1502 	 * Free all of our ASIDs which means we can skip doing all the
   1503 	 * tlb_invalidate_addrs().
   1504 	 */
   1505 	pmap_tlb_miss_lock_enter();
   1506 #ifdef MULTIPROCESSOR
   1507 	// This should be the last CPU with this pmap onproc
   1508 	KASSERT(!kcpuset_isotherset(pmap->pm_onproc, cpu_index(curcpu())));
   1509 	if (kcpuset_isset(pmap->pm_onproc, cpu_index(curcpu())))
   1510 #endif
   1511 		pmap_tlb_asid_deactivate(pmap);
   1512 #ifdef MULTIPROCESSOR
   1513 	KASSERT(kcpuset_iszero(pmap->pm_onproc));
   1514 #endif
   1515 	pmap_tlb_asid_release_all(pmap);
   1516 	pmap_tlb_miss_lock_exit();
   1517 	pmap->pm_flags |= PMAP_DEFERRED_ACTIVATE;
   1518 
   1519 #ifdef PMAP_FAULTINFO
   1520 	curpcb->pcb_faultinfo.pfi_faultaddr = 0;
   1521 	curpcb->pcb_faultinfo.pfi_repeats = 0;
   1522 	curpcb->pcb_faultinfo.pfi_faultpte = NULL;
   1523 #endif
   1524 	kpreempt_enable();
   1525 
   1526 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1527 }
   1528 
   1529 /*
   1530  *	Routine:	pmap_unwire
   1531  *	Function:	Clear the wired attribute for a map/virtual-address
   1532  *			pair.
   1533  *	In/out conditions:
   1534  *			The mapping must already exist in the pmap.
   1535  */
   1536 void
   1537 pmap_unwire(pmap_t pmap, vaddr_t va)
   1538 {
   1539 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1540 	UVMHIST_LOG(pmaphist, "(pmap=%#jx, va=%#jx)", (uintptr_t)pmap, va,
   1541 	    0, 0);
   1542 	PMAP_COUNT(unwire);
   1543 
   1544 	/*
   1545 	 * Don't need to flush the TLB since PG_WIRED is only in software.
   1546 	 */
   1547 	kpreempt_disable();
   1548 	pmap_addr_range_check(pmap, va, va, __func__);
   1549 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1550 	KASSERTMSG(ptep != NULL, "pmap %p va %#"PRIxVADDR" invalid STE",
   1551 	    pmap, va);
   1552 	pt_entry_t pte = *ptep;
   1553 	KASSERTMSG(pte_valid_p(pte),
   1554 	    "pmap %p va %#"PRIxVADDR" invalid PTE %#"PRIxPTE" @ %p",
   1555 	    pmap, va, pte_value(pte), ptep);
   1556 
   1557 	if (pte_wired_p(pte)) {
   1558 		pmap_tlb_miss_lock_enter();
   1559 		pte_set(ptep, pte_unwire_entry(pte));
   1560 		pmap_tlb_miss_lock_exit();
   1561 		pmap->pm_stats.wired_count--;
   1562 	}
   1563 #ifdef DIAGNOSTIC
   1564 	else {
   1565 		printf("%s: wiring for pmap %p va %#"PRIxVADDR" unchanged!\n",
   1566 		    __func__, pmap, va);
   1567 	}
   1568 #endif
   1569 	kpreempt_enable();
   1570 
   1571 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1572 }
   1573 
   1574 /*
   1575  *	Routine:	pmap_extract
   1576  *	Function:
   1577  *		Extract the physical page address associated
   1578  *		with the given map/virtual_address pair.
   1579  */
   1580 bool
   1581 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap)
   1582 {
   1583 	paddr_t pa;
   1584 
   1585 	if (pmap == pmap_kernel()) {
   1586 		if (pmap_md_direct_mapped_vaddr_p(va)) {
   1587 			pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1588 			goto done;
   1589 		}
   1590 		if (pmap_md_io_vaddr_p(va))
   1591 			panic("pmap_extract: io address %#"PRIxVADDR"", va);
   1592 
   1593 		if (va >= pmap_limits.virtual_end)
   1594 			panic("%s: illegal kernel mapped address %#"PRIxVADDR,
   1595 			    __func__, va);
   1596 	}
   1597 	kpreempt_disable();
   1598 	const pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1599 	if (ptep == NULL || !pte_valid_p(*ptep)) {
   1600 		kpreempt_enable();
   1601 		return false;
   1602 	}
   1603 	pa = pte_to_paddr(*ptep) | (va & PGOFSET);
   1604 	kpreempt_enable();
   1605 done:
   1606 	if (pap != NULL) {
   1607 		*pap = pa;
   1608 	}
   1609 	return true;
   1610 }
   1611 
   1612 /*
   1613  *	Copy the range specified by src_addr/len
   1614  *	from the source map to the range dst_addr/len
   1615  *	in the destination map.
   1616  *
   1617  *	This routine is only advisory and need not do anything.
   1618  */
   1619 void
   1620 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len,
   1621     vaddr_t src_addr)
   1622 {
   1623 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1624 	PMAP_COUNT(copy);
   1625 }
   1626 
   1627 /*
   1628  *	pmap_clear_reference:
   1629  *
   1630  *	Clear the reference bit on the specified physical page.
   1631  */
   1632 bool
   1633 pmap_clear_reference(struct vm_page *pg)
   1634 {
   1635 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1636 
   1637 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1638 	UVMHIST_LOG(pmaphist, "(pg=%#jx (pa %#jx))",
   1639 	   (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1640 
   1641 	bool rv = pmap_page_clear_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1642 
   1643 	UVMHIST_LOG(pmaphist, " <-- wasref %ju", rv, 0, 0, 0);
   1644 
   1645 	return rv;
   1646 }
   1647 
   1648 /*
   1649  *	pmap_is_referenced:
   1650  *
   1651  *	Return whether or not the specified physical page is referenced
   1652  *	by any physical maps.
   1653  */
   1654 bool
   1655 pmap_is_referenced(struct vm_page *pg)
   1656 {
   1657 	return VM_PAGEMD_REFERENCED_P(VM_PAGE_TO_MD(pg));
   1658 }
   1659 
   1660 /*
   1661  *	Clear the modify bits on the specified physical page.
   1662  */
   1663 bool
   1664 pmap_clear_modify(struct vm_page *pg)
   1665 {
   1666 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1667 	pv_entry_t pv = &mdpg->mdpg_first;
   1668 	pv_entry_t pv_next;
   1669 
   1670 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1671 	UVMHIST_LOG(pmaphist, "(pg=%#jx (%#jx))",
   1672 	    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1673 	PMAP_COUNT(clear_modify);
   1674 
   1675 	if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1676 		if (pv->pv_pmap == NULL) {
   1677 			UVMHIST_LOG(pmapexechist,
   1678 			    "pg %#jx (pa %#jx): execpage cleared",
   1679 			    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), 0, 0);
   1680 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1681 			PMAP_COUNT(exec_uncached_clear_modify);
   1682 		} else {
   1683 			UVMHIST_LOG(pmapexechist,
   1684 			    "pg %#jx (pa %#jx): syncicache performed",
   1685 			    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), 0, 0);
   1686 			pmap_page_syncicache(pg);
   1687 			PMAP_COUNT(exec_synced_clear_modify);
   1688 		}
   1689 	}
   1690 	if (!pmap_page_clear_attributes(mdpg, VM_PAGEMD_MODIFIED)) {
   1691 		UVMHIST_LOG(pmaphist, " <-- false", 0, 0, 0, 0);
   1692 		return false;
   1693 	}
   1694 	if (pv->pv_pmap == NULL) {
   1695 		UVMHIST_LOG(pmaphist, " <-- true (no mappings)", 0, 0, 0, 0);
   1696 		return true;
   1697 	}
   1698 
   1699 	/*
   1700 	 * remove write access from any pages that are dirty
   1701 	 * so we can tell if they are written to again later.
   1702 	 * flush the VAC first if there is one.
   1703 	 */
   1704 	kpreempt_disable();
   1705 	VM_PAGEMD_PVLIST_READLOCK(mdpg);
   1706 	pmap_pvlist_check(mdpg);
   1707 	for (; pv != NULL; pv = pv_next) {
   1708 		pmap_t pmap = pv->pv_pmap;
   1709 		vaddr_t va = trunc_page(pv->pv_va);
   1710 
   1711 		pv_next = pv->pv_next;
   1712 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1713 		if (PV_ISKENTER_P(pv))
   1714 			continue;
   1715 #endif
   1716 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1717 		KASSERT(ptep);
   1718 		pt_entry_t pte = pte_prot_nowrite(*ptep);
   1719 		if (*ptep == pte) {
   1720 			continue;
   1721 		}
   1722 		KASSERT(pte_valid_p(pte));
   1723 		const uintptr_t gen = VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1724 		pmap_tlb_miss_lock_enter();
   1725 		pte_set(ptep, pte);
   1726 		pmap_tlb_invalidate_addr(pmap, va);
   1727 		pmap_tlb_miss_lock_exit();
   1728 		pmap_update(pmap);
   1729 		if (__predict_false(gen != VM_PAGEMD_PVLIST_READLOCK(mdpg))) {
   1730 			/*
   1731 			 * The list changed!  So restart from the beginning.
   1732 			 */
   1733 			pv_next = &mdpg->mdpg_first;
   1734 			pmap_pvlist_check(mdpg);
   1735 		}
   1736 	}
   1737 	pmap_pvlist_check(mdpg);
   1738 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1739 	kpreempt_enable();
   1740 
   1741 	UVMHIST_LOG(pmaphist, " <-- true (mappings changed)", 0, 0, 0, 0);
   1742 	return true;
   1743 }
   1744 
   1745 /*
   1746  *	pmap_is_modified:
   1747  *
   1748  *	Return whether or not the specified physical page is modified
   1749  *	by any physical maps.
   1750  */
   1751 bool
   1752 pmap_is_modified(struct vm_page *pg)
   1753 {
   1754 	return VM_PAGEMD_MODIFIED_P(VM_PAGE_TO_MD(pg));
   1755 }
   1756 
   1757 /*
   1758  *	pmap_set_modified:
   1759  *
   1760  *	Sets the page modified reference bit for the specified page.
   1761  */
   1762 void
   1763 pmap_set_modified(paddr_t pa)
   1764 {
   1765 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1766 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1767 	pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1768 }
   1769 
   1770 /******************** pv_entry management ********************/
   1771 
   1772 static void
   1773 pmap_pvlist_check(struct vm_page_md *mdpg)
   1774 {
   1775 #ifdef DEBUG
   1776 	pv_entry_t pv = &mdpg->mdpg_first;
   1777 	if (pv->pv_pmap != NULL) {
   1778 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1779 		const u_int colormask = uvmexp.colormask;
   1780 		u_int colors = 0;
   1781 #endif
   1782 		for (; pv != NULL; pv = pv->pv_next) {
   1783 			KASSERT(pv->pv_pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(pv->pv_va));
   1784 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1785 			colors |= __BIT(atop(pv->pv_va) & colormask);
   1786 #endif
   1787 		}
   1788 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1789 		// Assert that if there is more than 1 color mapped, that the
   1790 		// page is uncached.
   1791 		KASSERTMSG(!pmap_md_virtual_cache_aliasing_p()
   1792 		    || colors == 0 || (colors & (colors-1)) == 0
   1793 		    || VM_PAGEMD_UNCACHED_P(mdpg), "colors=%#x uncached=%u",
   1794 		    colors, VM_PAGEMD_UNCACHED_P(mdpg));
   1795 #endif
   1796 	} else {
   1797     		KASSERT(pv->pv_next == NULL);
   1798 	}
   1799 #endif /* DEBUG */
   1800 }
   1801 
   1802 /*
   1803  * Enter the pmap and virtual address into the
   1804  * physical to virtual map table.
   1805  */
   1806 void
   1807 pmap_enter_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, pt_entry_t *nptep,
   1808     u_int flags)
   1809 {
   1810 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1811 	pv_entry_t pv, npv, apv;
   1812 #ifdef UVMHIST
   1813 	bool first = false;
   1814 #endif
   1815 
   1816 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1817 	UVMHIST_LOG(pmaphist,
   1818 	    "(pmap=%#jx va=%#jx pg=%#jx (%#jx)",
   1819 	    (uintptr_t)pmap, va, (uintptr_t)pg, VM_PAGE_TO_PHYS(pg));
   1820 	UVMHIST_LOG(pmaphist, "nptep=%#jx (%#jx))",
   1821 	    (uintptr_t)nptep, pte_value(*nptep), 0, 0);
   1822 
   1823 	KASSERT(kpreempt_disabled());
   1824 	KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1825 	KASSERTMSG(pmap != pmap_kernel() || !pmap_md_io_vaddr_p(va),
   1826 	    "va %#"PRIxVADDR, va);
   1827 
   1828 	apv = NULL;
   1829 	VM_PAGEMD_PVLIST_LOCK(mdpg);
   1830 again:
   1831 	pv = &mdpg->mdpg_first;
   1832 	pmap_pvlist_check(mdpg);
   1833 	if (pv->pv_pmap == NULL) {
   1834 		KASSERT(pv->pv_next == NULL);
   1835 		/*
   1836 		 * No entries yet, use header as the first entry
   1837 		 */
   1838 		PMAP_COUNT(primary_mappings);
   1839 		PMAP_COUNT(mappings);
   1840 #ifdef UVMHIST
   1841 		first = true;
   1842 #endif
   1843 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1844 		KASSERT(VM_PAGEMD_CACHED_P(mdpg));
   1845 		// If the new mapping has an incompatible color the last
   1846 		// mapping of this page, clean the page before using it.
   1847 		if (!PMAP_PAGE_COLOROK_P(va, pv->pv_va)) {
   1848 			pmap_md_vca_clean(pg, PMAP_WBINV);
   1849 		}
   1850 #endif
   1851 		pv->pv_pmap = pmap;
   1852 		pv->pv_va = va | flags;
   1853 	} else {
   1854 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1855 		if (pmap_md_vca_add(pg, va, nptep)) {
   1856 			goto again;
   1857 		}
   1858 #endif
   1859 
   1860 		/*
   1861 		 * There is at least one other VA mapping this page.
   1862 		 * Place this entry after the header.
   1863 		 *
   1864 		 * Note: the entry may already be in the table if
   1865 		 * we are only changing the protection bits.
   1866 		 */
   1867 
   1868 #ifdef PARANOIADIAG
   1869 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1870 #endif
   1871 		for (npv = pv; npv; npv = npv->pv_next) {
   1872 			if (pmap == npv->pv_pmap
   1873 			    && va == trunc_page(npv->pv_va)) {
   1874 #ifdef PARANOIADIAG
   1875 				pt_entry_t *ptep = pmap_pte_lookup(pmap, va);
   1876 				pt_entry_t pte = (ptep != NULL) ? *ptep : 0;
   1877 				if (!pte_valid_p(pte) || pte_to_paddr(pte) != pa)
   1878 					printf("%s: found va %#"PRIxVADDR
   1879 					    " pa %#"PRIxPADDR
   1880 					    " in pv_table but != %#"PRIxPTE"\n",
   1881 					    __func__, va, pa, pte_value(pte));
   1882 #endif
   1883 				PMAP_COUNT(remappings);
   1884 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1885 				if (__predict_false(apv != NULL))
   1886 					pmap_pv_free(apv);
   1887 
   1888 				UVMHIST_LOG(pmaphist,
   1889 				    " <-- done pv=%#jx (reused)",
   1890 				    (uintptr_t)pv, 0, 0, 0);
   1891 				return;
   1892 			}
   1893 		}
   1894 		if (__predict_true(apv == NULL)) {
   1895 			/*
   1896 			 * To allocate a PV, we have to release the PVLIST lock
   1897 			 * so get the page generation.  We allocate the PV, and
   1898 			 * then reacquire the lock.
   1899 			 */
   1900 			pmap_pvlist_check(mdpg);
   1901 			const uintptr_t gen = VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1902 
   1903 			apv = (pv_entry_t)pmap_pv_alloc();
   1904 			if (apv == NULL)
   1905 				panic("pmap_enter_pv: pmap_pv_alloc() failed");
   1906 
   1907 			/*
   1908 			 * If the generation has changed, then someone else
   1909 			 * tinkered with this page so we should start over.
   1910 			 */
   1911 			if (gen != VM_PAGEMD_PVLIST_LOCK(mdpg))
   1912 				goto again;
   1913 		}
   1914 		npv = apv;
   1915 		apv = NULL;
   1916 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1917 		/*
   1918 		 * If need to deal with virtual cache aliases, keep mappings
   1919 		 * in the kernel pmap at the head of the list.  This allows
   1920 		 * the VCA code to easily use them for cache operations if
   1921 		 * present.
   1922 		 */
   1923 		pmap_t kpmap = pmap_kernel();
   1924 		if (pmap != kpmap) {
   1925 			while (pv->pv_pmap == kpmap && pv->pv_next != NULL) {
   1926 				pv = pv->pv_next;
   1927 			}
   1928 		}
   1929 #endif
   1930 		npv->pv_va = va | flags;
   1931 		npv->pv_pmap = pmap;
   1932 		npv->pv_next = pv->pv_next;
   1933 		pv->pv_next = npv;
   1934 		PMAP_COUNT(mappings);
   1935 	}
   1936 	pmap_pvlist_check(mdpg);
   1937 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1938 	if (__predict_false(apv != NULL))
   1939 		pmap_pv_free(apv);
   1940 
   1941 	UVMHIST_LOG(pmaphist, " <-- done pv=%#jx (first %ju)", (uintptr_t)pv,
   1942 	    first, 0, 0);
   1943 }
   1944 
   1945 /*
   1946  * Remove a physical to virtual address translation.
   1947  * If cache was inhibited on this page, and there are no more cache
   1948  * conflicts, restore caching.
   1949  * Flush the cache if the last page is removed (should always be cached
   1950  * at this point).
   1951  */
   1952 void
   1953 pmap_remove_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, bool dirty)
   1954 {
   1955 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1956 	pv_entry_t pv, npv;
   1957 	bool last;
   1958 
   1959 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1960 	UVMHIST_LOG(pmaphist,
   1961 	    "(pmap=%#jx, va=%#jx, pg=%#jx (pa %#jx)",
   1962 	    (uintptr_t)pmap, va, (uintptr_t)pg, VM_PAGE_TO_PHYS(pg));
   1963 	UVMHIST_LOG(pmaphist, "dirty=%ju)", dirty, 0, 0, 0);
   1964 
   1965 	KASSERT(kpreempt_disabled());
   1966 	KASSERT((va & PAGE_MASK) == 0);
   1967 	pv = &mdpg->mdpg_first;
   1968 
   1969 	VM_PAGEMD_PVLIST_LOCK(mdpg);
   1970 	pmap_pvlist_check(mdpg);
   1971 
   1972 	/*
   1973 	 * If it is the first entry on the list, it is actually
   1974 	 * in the header and we must copy the following entry up
   1975 	 * to the header.  Otherwise we must search the list for
   1976 	 * the entry.  In either case we free the now unused entry.
   1977 	 */
   1978 
   1979 	last = false;
   1980 	if (pmap == pv->pv_pmap && va == trunc_page(pv->pv_va)) {
   1981 		npv = pv->pv_next;
   1982 		if (npv) {
   1983 			*pv = *npv;
   1984 			KASSERT(pv->pv_pmap != NULL);
   1985 		} else {
   1986 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1987 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1988 #endif
   1989 			pv->pv_pmap = NULL;
   1990 			last = true;	/* Last mapping removed */
   1991 		}
   1992 		PMAP_COUNT(remove_pvfirst);
   1993 	} else {
   1994 		for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
   1995 			PMAP_COUNT(remove_pvsearch);
   1996 			if (pmap == npv->pv_pmap && va == trunc_page(npv->pv_va))
   1997 				break;
   1998 		}
   1999 		if (npv) {
   2000 			pv->pv_next = npv->pv_next;
   2001 		}
   2002 	}
   2003 
   2004 	pmap_pvlist_check(mdpg);
   2005 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   2006 
   2007 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   2008 	pmap_md_vca_remove(pg, va, dirty, last);
   2009 #endif
   2010 
   2011 	/*
   2012 	 * Free the pv_entry if needed.
   2013 	 */
   2014 	if (npv)
   2015 		pmap_pv_free(npv);
   2016 	if (VM_PAGEMD_EXECPAGE_P(mdpg) && dirty) {
   2017 		if (last) {
   2018 			/*
   2019 			 * If this was the page's last mapping, we no longer
   2020 			 * care about its execness.
   2021 			 */
   2022 			UVMHIST_LOG(pmapexechist,
   2023 			    "pg %#jx (pa %#jx)last %ju: execpage cleared",
   2024 			    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), last, 0);
   2025 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   2026 			PMAP_COUNT(exec_uncached_remove);
   2027 		} else {
   2028 			/*
   2029 			 * Someone still has it mapped as an executable page
   2030 			 * so we must sync it.
   2031 			 */
   2032 			UVMHIST_LOG(pmapexechist,
   2033 			    "pg %#jx (pa %#jx) last %ju: performed syncicache",
   2034 			    (uintptr_t)pg, VM_PAGE_TO_PHYS(pg), last, 0);
   2035 			pmap_page_syncicache(pg);
   2036 			PMAP_COUNT(exec_synced_remove);
   2037 		}
   2038 	}
   2039 
   2040 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   2041 }
   2042 
   2043 #if defined(MULTIPROCESSOR)
   2044 struct pmap_pvlist_info {
   2045 	kmutex_t *pli_locks[PAGE_SIZE / 32];
   2046 	volatile u_int pli_lock_refs[PAGE_SIZE / 32];
   2047 	volatile u_int pli_lock_index;
   2048 	u_int pli_lock_mask;
   2049 } pmap_pvlist_info;
   2050 
   2051 void
   2052 pmap_pvlist_lock_init(size_t cache_line_size)
   2053 {
   2054 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   2055 	const vaddr_t lock_page = uvm_pageboot_alloc(PAGE_SIZE);
   2056 	vaddr_t lock_va = lock_page;
   2057 	if (sizeof(kmutex_t) > cache_line_size) {
   2058 		cache_line_size = roundup2(sizeof(kmutex_t), cache_line_size);
   2059 	}
   2060 	const size_t nlocks = PAGE_SIZE / cache_line_size;
   2061 	KASSERT((nlocks & (nlocks - 1)) == 0);
   2062 	/*
   2063 	 * Now divide the page into a number of mutexes, one per cacheline.
   2064 	 */
   2065 	for (size_t i = 0; i < nlocks; lock_va += cache_line_size, i++) {
   2066 		kmutex_t * const lock = (kmutex_t *)lock_va;
   2067 		mutex_init(lock, MUTEX_DEFAULT, IPL_HIGH);
   2068 		pli->pli_locks[i] = lock;
   2069 	}
   2070 	pli->pli_lock_mask = nlocks - 1;
   2071 }
   2072 
   2073 kmutex_t *
   2074 pmap_pvlist_lock_addr(struct vm_page_md *mdpg)
   2075 {
   2076 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   2077 	kmutex_t *lock = mdpg->mdpg_lock;
   2078 
   2079 	/*
   2080 	 * Allocate a lock on an as-needed basis.  This will hopefully give us
   2081 	 * semi-random distribution not based on page color.
   2082 	 */
   2083 	if (__predict_false(lock == NULL)) {
   2084 		size_t locknum = atomic_add_int_nv(&pli->pli_lock_index, 37);
   2085 		size_t lockid = locknum & pli->pli_lock_mask;
   2086 		kmutex_t * const new_lock = pli->pli_locks[lockid];
   2087 		/*
   2088 		 * Set the lock.  If some other thread already did, just use
   2089 		 * the one they assigned.
   2090 		 */
   2091 		lock = atomic_cas_ptr(&mdpg->mdpg_lock, NULL, new_lock);
   2092 		if (lock == NULL) {
   2093 			lock = new_lock;
   2094 			atomic_inc_uint(&pli->pli_lock_refs[lockid]);
   2095 		}
   2096 	}
   2097 
   2098 	/*
   2099 	 * Now finally provide the lock.
   2100 	 */
   2101 	return lock;
   2102 }
   2103 #else /* !MULTIPROCESSOR */
   2104 void
   2105 pmap_pvlist_lock_init(size_t cache_line_size)
   2106 {
   2107 	mutex_init(&pmap_pvlist_mutex, MUTEX_DEFAULT, IPL_HIGH);
   2108 }
   2109 
   2110 #ifdef MODULAR
   2111 kmutex_t *
   2112 pmap_pvlist_lock_addr(struct vm_page_md *mdpg)
   2113 {
   2114 	/*
   2115 	 * We just use a global lock.
   2116 	 */
   2117 	if (__predict_false(mdpg->mdpg_lock == NULL)) {
   2118 		mdpg->mdpg_lock = &pmap_pvlist_mutex;
   2119 	}
   2120 
   2121 	/*
   2122 	 * Now finally provide the lock.
   2123 	 */
   2124 	return mdpg->mdpg_lock;
   2125 }
   2126 #endif /* MODULAR */
   2127 #endif /* !MULTIPROCESSOR */
   2128 
   2129 /*
   2130  * pmap_pv_page_alloc:
   2131  *
   2132  *	Allocate a page for the pv_entry pool.
   2133  */
   2134 void *
   2135 pmap_pv_page_alloc(struct pool *pp, int flags)
   2136 {
   2137 	struct vm_page * const pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_USERESERVE);
   2138 	if (pg == NULL)
   2139 		return NULL;
   2140 
   2141 	return (void *)pmap_map_poolpage(VM_PAGE_TO_PHYS(pg));
   2142 }
   2143 
   2144 /*
   2145  * pmap_pv_page_free:
   2146  *
   2147  *	Free a pv_entry pool page.
   2148  */
   2149 void
   2150 pmap_pv_page_free(struct pool *pp, void *v)
   2151 {
   2152 	vaddr_t va = (vaddr_t)v;
   2153 
   2154 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   2155 	const paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   2156 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2157 	KASSERT(pg != NULL);
   2158 #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   2159 	kpreempt_disable();
   2160 	pmap_md_vca_remove(pg, va, true, true);
   2161 	kpreempt_enable();
   2162 #endif
   2163 	pmap_page_clear_attributes(VM_PAGE_TO_MD(pg), VM_PAGEMD_POOLPAGE);
   2164 	KASSERT(!VM_PAGEMD_EXECPAGE_P(VM_PAGE_TO_MD(pg)));
   2165 	uvm_pagefree(pg);
   2166 }
   2167 
   2168 #ifdef PMAP_PREFER
   2169 /*
   2170  * Find first virtual address >= *vap that doesn't cause
   2171  * a cache alias conflict.
   2172  */
   2173 void
   2174 pmap_prefer(vaddr_t foff, vaddr_t *vap, vsize_t sz, int td)
   2175 {
   2176 	vsize_t prefer_mask = ptoa(uvmexp.colormask);
   2177 
   2178 	PMAP_COUNT(prefer_requests);
   2179 
   2180 	prefer_mask |= pmap_md_cache_prefer_mask();
   2181 
   2182 	if (prefer_mask) {
   2183 		vaddr_t	va = *vap;
   2184 		vsize_t d = (foff - va) & prefer_mask;
   2185 		if (d) {
   2186 			if (td)
   2187 				*vap = trunc_page(va - ((-d) & prefer_mask));
   2188 			else
   2189 				*vap = round_page(va + d);
   2190 			PMAP_COUNT(prefer_adjustments);
   2191 		}
   2192 	}
   2193 }
   2194 #endif /* PMAP_PREFER */
   2195 
   2196 #ifdef PMAP_MAP_POOLPAGE
   2197 vaddr_t
   2198 pmap_map_poolpage(paddr_t pa)
   2199 {
   2200 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2201 	KASSERT(pg);
   2202 
   2203 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   2204 	KASSERT(!VM_PAGEMD_EXECPAGE_P(mdpg));
   2205 
   2206 	pmap_page_set_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   2207 
   2208 	return pmap_md_map_poolpage(pa, NBPG);
   2209 }
   2210 
   2211 paddr_t
   2212 pmap_unmap_poolpage(vaddr_t va)
   2213 {
   2214 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   2215 	paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   2216 
   2217 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2218 	KASSERT(pg != NULL);
   2219 	KASSERT(!VM_PAGEMD_EXECPAGE_P(VM_PAGE_TO_MD(pg)));
   2220 
   2221 	pmap_page_clear_attributes(VM_PAGE_TO_MD(pg), VM_PAGEMD_POOLPAGE);
   2222 	pmap_md_unmap_poolpage(va, NBPG);
   2223 
   2224 	return pa;
   2225 }
   2226 #endif /* PMAP_MAP_POOLPAGE */
   2227