Home | History | Annotate | Line # | Download | only in pmap
pmap.c revision 1.10
      1 /*	$NetBSD: pmap.c,v 1.10 2015/01/26 04:47:53 nonaka 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.10 2015/01/26 04:47:53 nonaka 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 #include <sys/systm.h>
    106 #include <sys/proc.h>
    107 #include <sys/buf.h>
    108 #include <sys/pool.h>
    109 #include <sys/atomic.h>
    110 #include <sys/mutex.h>
    111 #include <sys/atomic.h>
    112 #ifdef SYSVSHM
    113 #include <sys/shm.h>
    114 #endif
    115 #include <sys/socketvar.h>	/* XXX: for sock_loan_thresh */
    116 
    117 #include <uvm/uvm.h>
    118 
    119 #define	PMAP_COUNT(name)	(pmap_evcnt_##name.ev_count++ + 0)
    120 #define PMAP_COUNTER(name, desc) \
    121 static struct evcnt pmap_evcnt_##name = \
    122 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", desc); \
    123 EVCNT_ATTACH_STATIC(pmap_evcnt_##name)
    124 
    125 PMAP_COUNTER(remove_kernel_calls, "remove kernel calls");
    126 PMAP_COUNTER(remove_kernel_pages, "kernel pages unmapped");
    127 PMAP_COUNTER(remove_user_calls, "remove user calls");
    128 PMAP_COUNTER(remove_user_pages, "user pages unmapped");
    129 PMAP_COUNTER(remove_flushes, "remove cache flushes");
    130 PMAP_COUNTER(remove_tlb_ops, "remove tlb ops");
    131 PMAP_COUNTER(remove_pvfirst, "remove pv first");
    132 PMAP_COUNTER(remove_pvsearch, "remove pv search");
    133 
    134 PMAP_COUNTER(prefer_requests, "prefer requests");
    135 PMAP_COUNTER(prefer_adjustments, "prefer adjustments");
    136 
    137 PMAP_COUNTER(idlezeroed_pages, "pages idle zeroed");
    138 PMAP_COUNTER(zeroed_pages, "pages zeroed");
    139 PMAP_COUNTER(copied_pages, "pages copied");
    140 
    141 PMAP_COUNTER(kenter_pa, "kernel fast mapped pages");
    142 PMAP_COUNTER(kenter_pa_bad, "kernel fast mapped pages (bad color)");
    143 PMAP_COUNTER(kenter_pa_unmanaged, "kernel fast mapped unmanaged pages");
    144 PMAP_COUNTER(kremove_pages, "kernel fast unmapped pages");
    145 
    146 PMAP_COUNTER(page_cache_evictions, "pages changed to uncacheable");
    147 PMAP_COUNTER(page_cache_restorations, "pages changed to cacheable");
    148 
    149 PMAP_COUNTER(kernel_mappings_bad, "kernel pages mapped (bad color)");
    150 PMAP_COUNTER(user_mappings_bad, "user pages mapped (bad color)");
    151 PMAP_COUNTER(kernel_mappings, "kernel pages mapped");
    152 PMAP_COUNTER(user_mappings, "user pages mapped");
    153 PMAP_COUNTER(user_mappings_changed, "user mapping changed");
    154 PMAP_COUNTER(kernel_mappings_changed, "kernel mapping changed");
    155 PMAP_COUNTER(uncached_mappings, "uncached pages mapped");
    156 PMAP_COUNTER(unmanaged_mappings, "unmanaged pages mapped");
    157 PMAP_COUNTER(managed_mappings, "managed pages mapped");
    158 PMAP_COUNTER(mappings, "pages mapped");
    159 PMAP_COUNTER(remappings, "pages remapped");
    160 PMAP_COUNTER(unmappings, "pages unmapped");
    161 PMAP_COUNTER(primary_mappings, "page initial mappings");
    162 PMAP_COUNTER(primary_unmappings, "page final unmappings");
    163 PMAP_COUNTER(tlb_hit, "page mapping");
    164 
    165 PMAP_COUNTER(exec_mappings, "exec pages mapped");
    166 PMAP_COUNTER(exec_synced_mappings, "exec pages synced");
    167 PMAP_COUNTER(exec_synced_remove, "exec pages synced (PR)");
    168 PMAP_COUNTER(exec_synced_clear_modify, "exec pages synced (CM)");
    169 PMAP_COUNTER(exec_synced_page_protect, "exec pages synced (PP)");
    170 PMAP_COUNTER(exec_synced_protect, "exec pages synced (P)");
    171 PMAP_COUNTER(exec_uncached_page_protect, "exec pages uncached (PP)");
    172 PMAP_COUNTER(exec_uncached_clear_modify, "exec pages uncached (CM)");
    173 PMAP_COUNTER(exec_uncached_zero_page, "exec pages uncached (ZP)");
    174 PMAP_COUNTER(exec_uncached_copy_page, "exec pages uncached (CP)");
    175 PMAP_COUNTER(exec_uncached_remove, "exec pages uncached (PR)");
    176 
    177 PMAP_COUNTER(create, "creates");
    178 PMAP_COUNTER(reference, "references");
    179 PMAP_COUNTER(dereference, "dereferences");
    180 PMAP_COUNTER(destroy, "destroyed");
    181 PMAP_COUNTER(activate, "activations");
    182 PMAP_COUNTER(deactivate, "deactivations");
    183 PMAP_COUNTER(update, "updates");
    184 #ifdef MULTIPROCESSOR
    185 PMAP_COUNTER(shootdown_ipis, "shootdown IPIs");
    186 #endif
    187 PMAP_COUNTER(unwire, "unwires");
    188 PMAP_COUNTER(copy, "copies");
    189 PMAP_COUNTER(clear_modify, "clear_modifies");
    190 PMAP_COUNTER(protect, "protects");
    191 PMAP_COUNTER(page_protect, "page_protects");
    192 
    193 #define PMAP_ASID_RESERVED 0
    194 CTASSERT(PMAP_ASID_RESERVED == 0);
    195 
    196 /*
    197  * Initialize the kernel pmap.
    198  */
    199 #ifdef MULTIPROCESSOR
    200 #define	PMAP_SIZE	offsetof(struct pmap, pm_pai[PMAP_TLB_MAX])
    201 #else
    202 #define	PMAP_SIZE	sizeof(struct pmap)
    203 kmutex_t pmap_pvlist_mutex __aligned(COHERENCY_UNIT);
    204 #endif
    205 
    206 struct pmap_kernel kernel_pmap_store = {
    207 	.kernel_pmap = {
    208 		.pm_count = 1,
    209 		.pm_segtab = PMAP_INVALID_SEGTAB_ADDRESS,
    210 		.pm_minaddr = VM_MIN_KERNEL_ADDRESS,
    211 		.pm_maxaddr = VM_MAX_KERNEL_ADDRESS,
    212 	},
    213 };
    214 
    215 struct pmap * const kernel_pmap_ptr = &kernel_pmap_store.kernel_pmap;
    216 
    217 struct pmap_limits pmap_limits;
    218 
    219 #ifdef UVMHIST
    220 static struct kern_history_ent pmapexechistbuf[10000];
    221 static struct kern_history_ent pmaphistbuf[10000];
    222 UVMHIST_DEFINE(pmapexechist);
    223 UVMHIST_DEFINE(pmaphist);
    224 #endif
    225 
    226 /*
    227  * The pools from which pmap structures and sub-structures are allocated.
    228  */
    229 struct pool pmap_pmap_pool;
    230 struct pool pmap_pv_pool;
    231 
    232 #ifndef PMAP_PV_LOWAT
    233 #define	PMAP_PV_LOWAT	16
    234 #endif
    235 int		pmap_pv_lowat = PMAP_PV_LOWAT;
    236 
    237 bool		pmap_initialized = false;
    238 #define	PMAP_PAGE_COLOROK_P(a, b) \
    239 		((((int)(a) ^ (int)(b)) & pmap_page_colormask) == 0)
    240 u_int		pmap_page_colormask;
    241 
    242 #define PAGE_IS_MANAGED(pa)	\
    243 	(pmap_initialized == true && vm_physseg_find(atop(pa), NULL) != -1)
    244 
    245 #define PMAP_IS_ACTIVE(pm)						\
    246 	((pm) == pmap_kernel() || 					\
    247 	 (pm) == curlwp->l_proc->p_vmspace->vm_map.pmap)
    248 
    249 /* Forward function declarations */
    250 void pmap_remove_pv(pmap_t, vaddr_t, struct vm_page *, bool);
    251 void pmap_enter_pv(pmap_t, vaddr_t, struct vm_page *, u_int *);
    252 
    253 /*
    254  * PV table management functions.
    255  */
    256 void	*pmap_pv_page_alloc(struct pool *, int);
    257 void	pmap_pv_page_free(struct pool *, void *);
    258 
    259 struct pool_allocator pmap_pv_page_allocator = {
    260 	pmap_pv_page_alloc, pmap_pv_page_free, 0,
    261 };
    262 
    263 #define	pmap_pv_alloc()		pool_get(&pmap_pv_pool, PR_NOWAIT)
    264 #define	pmap_pv_free(pv)	pool_put(&pmap_pv_pool, (pv))
    265 
    266 #if !defined(MULTIPROCESSOR) || !defined(PMAP_MD_NEED_TLB_MISS_LOCK)
    267 #define	pmap_md_tlb_miss_lock_enter()	do { } while(/*CONSTCOND*/0)
    268 #define	pmap_md_tlb_miss_lock_exit()	do { } while(/*CONSTCOND*/0)
    269 #endif	/* !MULTIPROCESSOR || !PMAP_MD_NEED_TLB_MISS_LOCK */
    270 
    271 /*
    272  * Misc. functions.
    273  */
    274 
    275 bool
    276 pmap_page_clear_attributes(struct vm_page_md *mdpg, u_int clear_attributes)
    277 {
    278 	volatile u_int * const attrp = &mdpg->mdpg_attrs;
    279 #ifdef MULTIPROCESSOR
    280 	for (;;) {
    281 		u_int old_attr = *attrp;
    282 		if ((old_attr & clear_attributes) == 0)
    283 			return false;
    284 		u_int new_attr = old_attr & ~clear_attributes;
    285 		if (old_attr == atomic_cas_uint(attrp, old_attr, new_attr))
    286 			return true;
    287 	}
    288 #else
    289 	u_int old_attr = *attrp;
    290 	if ((old_attr & clear_attributes) == 0)
    291 		return false;
    292 	*attrp &= ~clear_attributes;
    293 	return true;
    294 #endif
    295 }
    296 
    297 void
    298 pmap_page_set_attributes(struct vm_page_md *mdpg, u_int set_attributes)
    299 {
    300 #ifdef MULTIPROCESSOR
    301 	atomic_or_uint(&mdpg->mdpg_attrs, set_attributes);
    302 #else
    303 	mdpg->mdpg_attrs |= set_attributes;
    304 #endif
    305 }
    306 
    307 static void
    308 pmap_page_syncicache(struct vm_page *pg)
    309 {
    310 #ifndef MULTIPROCESSOR
    311 	struct pmap * const curpmap = curcpu()->ci_curpm;
    312 #endif
    313 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    314 	pv_entry_t pv = &mdpg->mdpg_first;
    315 	kcpuset_t *onproc;
    316 #ifdef MULTIPROCESSOR
    317 	kcpuset_create(&onproc, true);
    318 #else
    319 	onproc = NULL;
    320 #endif
    321 	(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    322 
    323 	if (pv->pv_pmap != NULL) {
    324 		for (; pv != NULL; pv = pv->pv_next) {
    325 #ifdef MULTIPROCESSOR
    326 			kcpuset_merge(onproc, pv->pv_pmap->pm_onproc);
    327 			if (kcpuset_match(onproc, kcpuset_running)) {
    328 				break;
    329 			}
    330 #else
    331 			if (pv->pv_pmap == curpmap) {
    332 				onproc = curcpu()->ci_data.cpu_kcpuset;
    333 				break;
    334 			}
    335 #endif
    336 		}
    337 	}
    338 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    339 	kpreempt_disable();
    340 	pmap_md_page_syncicache(pg, onproc);
    341 #ifdef MULTIPROCESSOR
    342 	kcpuset_destroy(onproc);
    343 #endif
    344 	kpreempt_enable();
    345 }
    346 
    347 /*
    348  * Define the initial bounds of the kernel virtual address space.
    349  */
    350 void
    351 pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp)
    352 {
    353 
    354 	*vstartp = VM_MIN_KERNEL_ADDRESS;
    355 	*vendp = VM_MAX_KERNEL_ADDRESS;
    356 }
    357 
    358 vaddr_t
    359 pmap_growkernel(vaddr_t maxkvaddr)
    360 {
    361 	vaddr_t virtual_end = pmap_limits.virtual_end;
    362 	maxkvaddr = pmap_round_seg(maxkvaddr) - 1;
    363 
    364 	/*
    365 	 * Reserve PTEs for the new KVA space.
    366 	 */
    367 	for (; virtual_end < maxkvaddr; virtual_end += NBSEG) {
    368 		pmap_pte_reserve(pmap_kernel(), virtual_end, 0);
    369 	}
    370 
    371 	/*
    372 	 * Don't exceed VM_MAX_KERNEL_ADDRESS!
    373 	 */
    374 	if (virtual_end == 0 || virtual_end > VM_MAX_KERNEL_ADDRESS)
    375 		virtual_end = VM_MAX_KERNEL_ADDRESS;
    376 
    377 	/*
    378 	 * Update new end.
    379 	 */
    380 	pmap_limits.virtual_end = virtual_end;
    381 	return virtual_end;
    382 }
    383 
    384 /*
    385  * Bootstrap memory allocator (alternative to vm_bootstrap_steal_memory()).
    386  * This function allows for early dynamic memory allocation until the virtual
    387  * memory system has been bootstrapped.  After that point, either kmem_alloc
    388  * or malloc should be used.  This function works by stealing pages from the
    389  * (to be) managed page pool, then implicitly mapping the pages (by using
    390  * their k0seg addresses) and zeroing them.
    391  *
    392  * It may be used once the physical memory segments have been pre-loaded
    393  * into the vm_physmem[] array.  Early memory allocation MUST use this
    394  * interface!  This cannot be used after vm_page_startup(), and will
    395  * generate a panic if tried.
    396  *
    397  * Note that this memory will never be freed, and in essence it is wired
    398  * down.
    399  *
    400  * We must adjust *vstartp and/or *vendp iff we use address space
    401  * from the kernel virtual address range defined by pmap_virtual_space().
    402  */
    403 vaddr_t
    404 pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
    405 {
    406 	u_int npgs;
    407 	paddr_t pa;
    408 	vaddr_t va;
    409 
    410 	size = round_page(size);
    411 	npgs = atop(size);
    412 
    413 	for (u_int bank = 0; bank < vm_nphysseg; bank++) {
    414 		struct vm_physseg * const seg = VM_PHYSMEM_PTR(bank);
    415 		if (uvm.page_init_done == true)
    416 			panic("pmap_steal_memory: called _after_ bootstrap");
    417 
    418 		if (seg->avail_start != seg->start ||
    419 		    seg->avail_start >= seg->avail_end)
    420 			continue;
    421 
    422 		if ((seg->avail_end - seg->avail_start) < npgs)
    423 			continue;
    424 
    425 		/*
    426 		 * There are enough pages here; steal them!
    427 		 */
    428 		pa = ptoa(seg->avail_start);
    429 		seg->avail_start += npgs;
    430 		seg->start += npgs;
    431 
    432 		/*
    433 		 * Have we used up this segment?
    434 		 */
    435 		if (seg->avail_start == seg->end) {
    436 			if (vm_nphysseg == 1)
    437 				panic("pmap_steal_memory: out of memory!");
    438 
    439 			/* Remove this segment from the list. */
    440 			vm_nphysseg--;
    441 			if (bank < vm_nphysseg)
    442 				memmove(seg, seg+1,
    443 				    sizeof(*seg) * (vm_nphysseg - bank));
    444 		}
    445 
    446 		va = pmap_md_map_poolpage(pa, size);
    447 		memset((void *)va, 0, size);
    448 		return va;
    449 	}
    450 
    451 	/*
    452 	 * If we got here, there was no memory left.
    453 	 */
    454 	panic("pmap_steal_memory: no memory to steal");
    455 }
    456 
    457 /*
    458  *	Initialize the pmap module.
    459  *	Called by vm_init, to initialize any structures that the pmap
    460  *	system needs to map virtual memory.
    461  */
    462 void
    463 pmap_init(void)
    464 {
    465 	UVMHIST_INIT_STATIC(pmapexechist, pmapexechistbuf);
    466 	UVMHIST_INIT_STATIC(pmaphist, pmaphistbuf);
    467 
    468 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    469 
    470 	/*
    471 	 * Initialize the segtab lock.
    472 	 */
    473 	mutex_init(&pmap_segtab_lock, MUTEX_DEFAULT, IPL_HIGH);
    474 
    475 	/*
    476 	 * Set a low water mark on the pv_entry pool, so that we are
    477 	 * more likely to have these around even in extreme memory
    478 	 * starvation.
    479 	 */
    480 	pool_setlowat(&pmap_pv_pool, pmap_pv_lowat);
    481 
    482 	pmap_md_init();
    483 
    484 	/*
    485 	 * Now it is safe to enable pv entry recording.
    486 	 */
    487 	pmap_initialized = true;
    488 }
    489 
    490 /*
    491  *	Create and return a physical map.
    492  *
    493  *	If the size specified for the map
    494  *	is zero, the map is an actual physical
    495  *	map, and may be referenced by the
    496  *	hardware.
    497  *
    498  *	If the size specified is non-zero,
    499  *	the map will be used in software only, and
    500  *	is bounded by that size.
    501  */
    502 pmap_t
    503 pmap_create(void)
    504 {
    505 	pmap_t pmap;
    506 
    507 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    508 	PMAP_COUNT(create);
    509 
    510 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
    511 	memset(pmap, 0, PMAP_SIZE);
    512 
    513 	KASSERT(pmap->pm_pai[0].pai_link.le_prev == NULL);
    514 
    515 	pmap->pm_count = 1;
    516 	pmap->pm_minaddr = VM_MIN_ADDRESS;
    517 	pmap->pm_maxaddr = VM_MAXUSER_ADDRESS;
    518 
    519 	pmap_segtab_init(pmap);
    520 
    521 #ifdef MULTIPROCESSOR
    522 	kcpuset_create(&pmap->pm_active, true);
    523 	kcpuset_create(&pmap->pm_onproc, true);
    524 #endif
    525 
    526 	UVMHIST_LOG(pmaphist, "<- pmap %p", pmap,0,0,0);
    527 	return pmap;
    528 }
    529 
    530 /*
    531  *	Retire the given physical map from service.
    532  *	Should only be called if the map contains
    533  *	no valid mappings.
    534  */
    535 void
    536 pmap_destroy(pmap_t pmap)
    537 {
    538 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    539 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    540 
    541 	if (atomic_dec_uint_nv(&pmap->pm_count) > 0) {
    542 		PMAP_COUNT(dereference);
    543 		return;
    544 	}
    545 
    546 	KASSERT(pmap->pm_count == 0);
    547 	PMAP_COUNT(destroy);
    548 	kpreempt_disable();
    549 	pmap_md_tlb_miss_lock_enter();
    550 	pmap_tlb_asid_release_all(pmap);
    551 	pmap_segtab_destroy(pmap, NULL, 0);
    552 	pmap_md_tlb_miss_lock_exit();
    553 
    554 #ifdef MULTIPROCESSOR
    555 	kcpuset_destroy(pmap->pm_active);
    556 	kcpuset_destroy(pmap->pm_onproc);
    557 #endif
    558 
    559 	pool_put(&pmap_pmap_pool, pmap);
    560 	kpreempt_enable();
    561 
    562 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    563 }
    564 
    565 /*
    566  *	Add a reference to the specified pmap.
    567  */
    568 void
    569 pmap_reference(pmap_t pmap)
    570 {
    571 
    572 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    573 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    574 	PMAP_COUNT(reference);
    575 
    576 	if (pmap != NULL) {
    577 		atomic_inc_uint(&pmap->pm_count);
    578 	}
    579 
    580 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    581 }
    582 
    583 /*
    584  *	Make a new pmap (vmspace) active for the given process.
    585  */
    586 void
    587 pmap_activate(struct lwp *l)
    588 {
    589 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    590 
    591 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    592 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0,0);
    593 	PMAP_COUNT(activate);
    594 
    595 	kpreempt_disable();
    596 	pmap_md_tlb_miss_lock_enter();
    597 	pmap_tlb_asid_acquire(pmap, l);
    598 	if (l == curlwp) {
    599 		pmap_segtab_activate(pmap, l);
    600 	}
    601 	pmap_md_tlb_miss_lock_exit();
    602 	kpreempt_enable();
    603 
    604 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    605 }
    606 
    607 /*
    608  *	Make a previously active pmap (vmspace) inactive.
    609  */
    610 void
    611 pmap_deactivate(struct lwp *l)
    612 {
    613 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    614 
    615 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    616 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0,0);
    617 	PMAP_COUNT(deactivate);
    618 
    619 	kpreempt_disable();
    620 	pmap_md_tlb_miss_lock_enter();
    621 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    622 	pmap_tlb_asid_deactivate(pmap);
    623 	pmap_md_tlb_miss_lock_exit();
    624 	kpreempt_enable();
    625 
    626 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    627 }
    628 
    629 void
    630 pmap_update(struct pmap *pmap)
    631 {
    632 
    633 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    634 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    635 	PMAP_COUNT(update);
    636 
    637 	kpreempt_disable();
    638 #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
    639 	u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
    640 	if (pending && pmap_tlb_shootdown_bystanders(pmap))
    641 		PMAP_COUNT(shootdown_ipis);
    642 #endif
    643 	pmap_md_tlb_miss_lock_enter();
    644 #ifdef DEBUG
    645 	pmap_tlb_check(pmap, pmap_md_tlb_check_entry);
    646 #endif /* DEBUG */
    647 
    648 	/*
    649 	 * If pmap_remove_all was called, we deactivated ourselves and nuked
    650 	 * our ASID.  Now we have to reactivate ourselves.
    651 	 */
    652 	if (__predict_false(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE)) {
    653 		pmap->pm_flags ^= PMAP_DEFERRED_ACTIVATE;
    654 		pmap_tlb_asid_acquire(pmap, curlwp);
    655 		pmap_segtab_activate(pmap, curlwp);
    656 	}
    657 	pmap_md_tlb_miss_lock_exit();
    658 	kpreempt_enable();
    659 
    660 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    661 }
    662 
    663 /*
    664  *	Remove the given range of addresses from the specified map.
    665  *
    666  *	It is assumed that the start and end are properly
    667  *	rounded to the page size.
    668  */
    669 
    670 static bool
    671 pmap_pte_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    672 	uintptr_t flags)
    673 {
    674 	const pt_entry_t npte = flags;
    675 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    676 
    677 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    678 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%"PRIxVADDR"..%"PRIxVADDR,
    679 	    pmap, (is_kernel_pmap_p ? "(kernel) " : ""), sva, eva);
    680 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
    681 	    ptep, flags, 0, 0);
    682 
    683 	KASSERT(kpreempt_disabled());
    684 
    685 	for (; sva < eva; sva += NBPG, ptep++) {
    686 		pt_entry_t pt_entry = *ptep;
    687 		if (!pte_valid_p(pt_entry))
    688 			continue;
    689 		if (is_kernel_pmap_p)
    690 			PMAP_COUNT(remove_kernel_calls);
    691 		else
    692 			PMAP_COUNT(remove_user_pages);
    693 		if (pte_wired_p(pt_entry))
    694 			pmap->pm_stats.wired_count--;
    695 		pmap->pm_stats.resident_count--;
    696 		struct vm_page *pg = PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
    697 		if (__predict_true(pg != NULL)) {
    698 			pmap_remove_pv(pmap, sva, pg,
    699 			   pte_modified_p(pt_entry));
    700 		}
    701 		pmap_md_tlb_miss_lock_enter();
    702 		*ptep = npte;
    703 		/*
    704 		 * Flush the TLB for the given address.
    705 		 */
    706 		pmap_tlb_invalidate_addr(pmap, sva);
    707 		pmap_md_tlb_miss_lock_exit();
    708 	}
    709 	return false;
    710 }
    711 
    712 void
    713 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva)
    714 {
    715 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    716 	const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    717 
    718 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    719 	UVMHIST_LOG(pmaphist, "(pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR")",
    720 	    pmap, sva, eva, 0);
    721 
    722 	if (is_kernel_pmap_p)
    723 		PMAP_COUNT(remove_kernel_calls);
    724 	else
    725 		PMAP_COUNT(remove_user_calls);
    726 #ifdef PARANOIADIAG
    727 	if (sva < pm->pm_minaddr || eva > pm->pm_maxaddr)
    728 		panic("%s: va range %#"PRIxVADDR"-%#"PRIxVADDR" not in range",
    729 		    __func__, sva, eva - 1);
    730 	if (PMAP_IS_ACTIVE(pmap)) {
    731 		struct pmap_asid_info * const pai = PMAP_PAI(pmap, curcpu());
    732 		uint32_t asid = tlb_get_asid();
    733 		if (asid != pai->pai_asid) {
    734 			panic("%s: inconsistency for active TLB flush"
    735 			    ": %d <-> %d", __func__, asid, pai->pai_asid);
    736 		}
    737 	}
    738 #endif
    739 	kpreempt_disable();
    740 	pmap_pte_process(pmap, sva, eva, pmap_pte_remove, npte);
    741 	kpreempt_enable();
    742 
    743 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    744 }
    745 
    746 /*
    747  *	pmap_page_protect:
    748  *
    749  *	Lower the permission for all mappings to a given page.
    750  */
    751 void
    752 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
    753 {
    754 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    755 	pv_entry_t pv;
    756 	vaddr_t va;
    757 
    758 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    759 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") prot=%#x)",
    760 	    pg, VM_PAGE_TO_PHYS(pg), prot, 0);
    761 	PMAP_COUNT(page_protect);
    762 
    763 	switch (prot) {
    764 	case VM_PROT_READ|VM_PROT_WRITE:
    765 	case VM_PROT_ALL:
    766 		break;
    767 
    768 	/* copy_on_write */
    769 	case VM_PROT_READ:
    770 	case VM_PROT_READ|VM_PROT_EXECUTE:
    771 		(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    772 		pv = &mdpg->mdpg_first;
    773 		/*
    774 		 * Loop over all current mappings setting/clearing as appropriate.
    775 		 */
    776 		if (pv->pv_pmap != NULL) {
    777 			while (pv != NULL) {
    778 				const pmap_t pmap = pv->pv_pmap;
    779 				const uint16_t gen = VM_PAGEMD_PVLIST_GEN(mdpg);
    780 				va = pv->pv_va;
    781 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    782 				pmap_protect(pmap, va, va + PAGE_SIZE, prot);
    783 				KASSERT(pv->pv_pmap == pmap);
    784 				pmap_update(pmap);
    785 				if (gen != VM_PAGEMD_PVLIST_LOCK(mdpg, false)) {
    786 					pv = &mdpg->mdpg_first;
    787 				} else {
    788 					pv = pv->pv_next;
    789 				}
    790 			}
    791 		}
    792 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    793 		break;
    794 
    795 	/* remove_all */
    796 	default:
    797 		/*
    798 		 * Do this first so that for each unmapping, pmap_remove_pv
    799 		 * won't try to sync the icache.
    800 		 */
    801 		if (pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE)) {
    802 			UVMHIST_LOG(pmapexechist, "pg %p (pa %#"PRIxPADDR
    803 			    "): execpage cleared", pg, VM_PAGE_TO_PHYS(pg),0,0);
    804 			PMAP_COUNT(exec_uncached_page_protect);
    805 		}
    806 		(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    807 		pv = &mdpg->mdpg_first;
    808 		while (pv->pv_pmap != NULL) {
    809 			const pmap_t pmap = pv->pv_pmap;
    810 			va = pv->pv_va;
    811 			VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    812 			pmap_remove(pmap, va, va + PAGE_SIZE);
    813 			pmap_update(pmap);
    814 			(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    815 		}
    816 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    817 	}
    818 
    819 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    820 }
    821 
    822 static bool
    823 pmap_pte_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    824 	uintptr_t flags)
    825 {
    826 	const vm_prot_t prot = (flags & VM_PROT_ALL);
    827 
    828 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    829 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%"PRIxVADDR"..%"PRIxVADDR,
    830 	    pmap, (pmap == pmap_kernel() ? "(kernel) " : ""), sva, eva);
    831 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
    832 	    ptep, flags, 0, 0);
    833 
    834 	KASSERT(kpreempt_disabled());
    835 	/*
    836 	 * Change protection on every valid mapping within this segment.
    837 	 */
    838 	for (; sva < eva; sva += NBPG, ptep++) {
    839 		pt_entry_t pt_entry = *ptep;
    840 		if (!pte_valid_p(pt_entry))
    841 			continue;
    842 		struct vm_page * const pg =
    843 		    PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
    844 		if (pg != NULL && pte_modified_p(pt_entry)) {
    845 			struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    846 			pmap_md_vca_clean(pg, sva, PMAP_WBINV);
    847 			if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
    848 				KASSERT(mdpg->mdpg_first.pv_pmap != NULL);
    849 				if (pte_cached_p(pt_entry)) {
    850 					UVMHIST_LOG(pmapexechist,
    851 					    "pg %p (pa %#"PRIxPADDR"): %s",
    852 					    pg, VM_PAGE_TO_PHYS(pg),
    853 					    "syncicached performed", 0);
    854 					pmap_page_syncicache(pg);
    855 					PMAP_COUNT(exec_synced_protect);
    856 				}
    857 			}
    858 		}
    859 		pt_entry = pte_prot_downgrade(pt_entry, prot);
    860 		if (*ptep != pt_entry) {
    861 			pmap_md_tlb_miss_lock_enter();
    862 			*ptep = pt_entry;
    863 			/*
    864 			 * Update the TLB if needed.
    865 			 */
    866 			pmap_tlb_update_addr(pmap, sva, pt_entry,
    867 			    PMAP_TLB_NEED_IPI);
    868 			pmap_md_tlb_miss_lock_exit();
    869 		}
    870 	}
    871 	return false;
    872 }
    873 
    874 /*
    875  *	Set the physical protection on the
    876  *	specified range of this map as requested.
    877  */
    878 void
    879 pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
    880 {
    881 
    882 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    883 	UVMHIST_LOG(pmaphist,
    884 	    "  pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR" port=%#x)",
    885 	    pmap, sva, eva, prot);
    886 	PMAP_COUNT(protect);
    887 
    888 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
    889 		pmap_remove(pmap, sva, eva);
    890 		UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    891 		return;
    892 	}
    893 
    894 #ifdef PARANOIADIAG
    895 	if (sva < pm->pm_minaddr || eva > pm->pm_maxaddr)
    896 		panic("%s: va range %#"PRIxVADDR"-%#"PRIxVADDR" not in range",
    897 		    __func__, sva, eva - 1);
    898 	if (PMAP_IS_ACTIVE(pmap)) {
    899 		struct pmap_asid_info * const pai = PMAP_PAI(pmap, curcpu());
    900 		uint32_t asid = tlb_get_asid();
    901 		if (asid != pai->pai_asid) {
    902 			panic("%s: inconsistency for active TLB update"
    903 			    ": %d <-> %d", __func__, asid, pai->pai_asid);
    904 		}
    905 	}
    906 #endif
    907 
    908 	/*
    909 	 * Change protection on every valid mapping within this segment.
    910 	 */
    911 	kpreempt_disable();
    912 	pmap_pte_process(pmap, sva, eva, pmap_pte_protect, prot);
    913 	kpreempt_enable();
    914 
    915 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    916 }
    917 
    918 #if defined(__PMAP_VIRTUAL_CACHE_ALIASES)
    919 /*
    920  *	pmap_page_cache:
    921  *
    922  *	Change all mappings of a managed page to cached/uncached.
    923  */
    924 static void
    925 pmap_page_cache(struct vm_page *pg, bool cached)
    926 {
    927 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    928 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    929 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") cached=%s)",
    930 	    pg, VM_PAGE_TO_PHYS(pg), cached ? "true" : "false", 0);
    931 	KASSERT(kpreempt_disabled());
    932 
    933 	if (cached) {
    934 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
    935 		PMAP_COUNT(page_cache_restorations);
    936 	} else {
    937 		pmap_page_set_attributes(mdpg, VM_PAGEMD_UNCACHED);
    938 		PMAP_COUNT(page_cache_evictions);
    939 	}
    940 
    941 	KASSERT(VM_PAGEMD_PVLIST_LOCKED_P(mdpg));
    942 	KASSERT(kpreempt_disabled());
    943 	for (pv_entry_t pv = &mdpg->mdpg_first;
    944 	     pv != NULL;
    945 	     pv = pv->pv_next) {
    946 		pmap_t pmap = pv->pv_pmap;
    947 		vaddr_t va = pv->pv_va;
    948 
    949 		KASSERT(pmap != NULL);
    950 		KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
    951 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
    952 		if (ptep == NULL)
    953 			continue;
    954 		pt_entry_t pt_entry = *ptep;
    955 		if (pte_valid_p(pt_entry)) {
    956 			pt_entry = pte_cached_change(pt_entry, cached);
    957 			pmap_md_tlb_miss_lock_enter();
    958 			*ptep = pt_entry;
    959 			pmap_tlb_update_addr(pmap, va, pt_entry,
    960 			    PMAP_TLB_NEED_IPI);
    961 			pmap_md_tlb_miss_lock_exit();
    962 		}
    963 	}
    964 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    965 }
    966 #endif	/* __PMAP_VIRTUAL_CACHE_ALIASES */
    967 
    968 /*
    969  *	Insert the given physical page (p) at
    970  *	the specified virtual address (v) in the
    971  *	target physical map with the protection requested.
    972  *
    973  *	If specified, the page will be wired down, meaning
    974  *	that the related pte can not be reclaimed.
    975  *
    976  *	NB:  This is the only routine which MAY NOT lazy-evaluate
    977  *	or lose information.  That is, this routine must actually
    978  *	insert this page into the given map NOW.
    979  */
    980 int
    981 pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    982 {
    983 	pt_entry_t npte;
    984 	const bool wired = (flags & PMAP_WIRED) != 0;
    985 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    986 #ifdef UVMHIST
    987 	struct kern_history * const histp =
    988 	    ((prot & VM_PROT_EXECUTE) ? &pmapexechist : &pmaphist);
    989 #endif
    990 
    991 	UVMHIST_FUNC(__func__);
    992 #define VM_PROT_STRING(prot) \
    993 	&"\0    (R)\0  (W)\0  (RW)\0 (X)\0  (RX)\0 (WX)\0 (RWX)\0"[UVM_PROTECTION(prot)*6]
    994 	UVMHIST_CALLED(*histp);
    995 	UVMHIST_LOG(*histp, "(pmap=%p, va=%#"PRIxVADDR", pa=%#"PRIxPADDR,
    996 	    pmap, va, pa, 0);
    997 	UVMHIST_LOG(*histp, "prot=%#x%s flags=%#x%s)",
    998 	    prot, VM_PROT_STRING(prot), flags, VM_PROT_STRING(flags));
    999 
   1000 	const bool good_color = PMAP_PAGE_COLOROK_P(pa, va);
   1001 	if (is_kernel_pmap_p) {
   1002 		PMAP_COUNT(kernel_mappings);
   1003 		if (!good_color)
   1004 			PMAP_COUNT(kernel_mappings_bad);
   1005 	} else {
   1006 		PMAP_COUNT(user_mappings);
   1007 		if (!good_color)
   1008 			PMAP_COUNT(user_mappings_bad);
   1009 	}
   1010 #if defined(DEBUG) || defined(DIAGNOSTIC) || defined(PARANOIADIAG)
   1011 	if (va < pmap->pm_minaddr || va >= pmap->pm_maxaddr)
   1012 		panic("%s: %s %#"PRIxVADDR" too big",
   1013 		    __func__, is_kernel_pmap_p ? "kva" : "uva", va);
   1014 #endif
   1015 
   1016 	KASSERTMSG(prot & VM_PROT_READ,
   1017 	    "%s: no READ (%#x) in prot %#x", __func__, VM_PROT_READ, prot);
   1018 
   1019 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1020 	struct vm_page_md *mdpg;
   1021 
   1022 	if (pg) {
   1023 		mdpg = VM_PAGE_TO_MD(pg);
   1024 		/* Set page referenced/modified status based on flags */
   1025 		if (flags & VM_PROT_WRITE)
   1026 			pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1027 		else if (flags & VM_PROT_ALL)
   1028 			pmap_page_set_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1029 
   1030 #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1031 		if (!VM_PAGEMD_CACHED(pg))
   1032 			flags |= PMAP_NOCACHE;
   1033 #endif
   1034 
   1035 		PMAP_COUNT(managed_mappings);
   1036 	} else {
   1037 		/*
   1038 		 * Assumption: if it is not part of our managed memory
   1039 		 * then it must be device memory which may be volatile.
   1040 		 */
   1041 		mdpg = NULL;
   1042 		flags |= PMAP_NOCACHE;
   1043 		PMAP_COUNT(unmanaged_mappings);
   1044 	}
   1045 
   1046 	npte = pte_make_enter(pa, mdpg, prot, flags, is_kernel_pmap_p);
   1047 
   1048 	kpreempt_disable();
   1049 	pt_entry_t * const ptep = pmap_pte_reserve(pmap, va, flags);
   1050 	if (__predict_false(ptep == NULL)) {
   1051 		kpreempt_enable();
   1052 		UVMHIST_LOG(*histp, "<- ENOMEM", 0,0,0,0);
   1053 		return ENOMEM;
   1054 	}
   1055 	pt_entry_t opte = *ptep;
   1056 
   1057 	/* Done after case that may sleep/return. */
   1058 	if (pg)
   1059 		pmap_enter_pv(pmap, va, pg, &npte);
   1060 
   1061 	/*
   1062 	 * Now validate mapping with desired protection/wiring.
   1063 	 * Assume uniform modified and referenced status for all
   1064 	 * MIPS pages in a MACH page.
   1065 	 */
   1066 	if (wired) {
   1067 		pmap->pm_stats.wired_count++;
   1068 		npte = pte_wire_entry(npte);
   1069 	}
   1070 
   1071 	UVMHIST_LOG(*histp, "new pte %#x (pa %#"PRIxPADDR")", npte, pa, 0,0);
   1072 
   1073 	if (pte_valid_p(opte) && pte_to_paddr(opte) != pa) {
   1074 		pmap_remove(pmap, va, va + NBPG);
   1075 		PMAP_COUNT(user_mappings_changed);
   1076 	}
   1077 
   1078 	KASSERT(pte_valid_p(npte));
   1079 	bool resident = pte_valid_p(opte);
   1080 	if (!resident)
   1081 		pmap->pm_stats.resident_count++;
   1082 	pmap_md_tlb_miss_lock_enter();
   1083 	*ptep = npte;
   1084 
   1085 	pmap_tlb_update_addr(pmap, va, npte,
   1086 	    ((flags & VM_PROT_ALL) ? PMAP_TLB_INSERT : 0)
   1087 	    | (resident ? PMAP_TLB_NEED_IPI : 0));
   1088 	pmap_md_tlb_miss_lock_exit();
   1089 	kpreempt_enable();
   1090 
   1091 	if (pg != NULL && (prot == (VM_PROT_READ | VM_PROT_EXECUTE))) {
   1092 		KASSERT(mdpg != NULL);
   1093 		PMAP_COUNT(exec_mappings);
   1094 		if (!VM_PAGEMD_EXECPAGE_P(mdpg) && pte_cached_p(npte)) {
   1095 			if (!pte_deferred_exec_p(npte)) {
   1096 				UVMHIST_LOG(*histp,
   1097 				    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1098 				    va, pg, "immediate", "");
   1099 				pmap_page_syncicache(pg);
   1100 				pmap_page_set_attributes(mdpg,
   1101 				    VM_PAGEMD_EXECPAGE);
   1102 				PMAP_COUNT(exec_synced_mappings);
   1103 			} else {
   1104 				UVMHIST_LOG(*histp, "va=%#"PRIxVADDR
   1105 				    " pg %p: %s syncicache: pte %#x",
   1106 				    va, pg, "defer", npte);
   1107 			}
   1108 		} else {
   1109 			UVMHIST_LOG(*histp,
   1110 			    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1111 			    va, pg, "no",
   1112 			    (pte_cached_p(npte)
   1113 				? " (already exec)"
   1114 				: " (uncached)"));
   1115 		}
   1116 	} else if (pg != NULL && (prot & VM_PROT_EXECUTE)) {
   1117 		KASSERT(mdpg != NULL);
   1118 		KASSERT(prot & VM_PROT_WRITE);
   1119 		PMAP_COUNT(exec_mappings);
   1120 		pmap_page_syncicache(pg);
   1121 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1122 		UVMHIST_LOG(pmapexechist,
   1123 		    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1124 		    va, pg, "immediate", " (writeable)");
   1125 	}
   1126 
   1127 	if (prot & VM_PROT_EXECUTE) {
   1128 		UVMHIST_LOG(pmapexechist, "<- 0 (OK)", 0,0,0,0);
   1129 	} else {
   1130 		UVMHIST_LOG(pmaphist, "<- 0 (OK)", 0,0,0,0);
   1131 	}
   1132 	return 0;
   1133 }
   1134 
   1135 void
   1136 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1137 {
   1138 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1139 	struct vm_page_md *mdpg;
   1140 
   1141 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1142 	UVMHIST_LOG(pmaphist, "(va=%#"PRIxVADDR" pa=%#"PRIxPADDR
   1143 	    ", prot=%#x, flags=%#x)", va, pa, prot, flags);
   1144 	PMAP_COUNT(kenter_pa);
   1145 
   1146 	if (pg == NULL) {
   1147 		mdpg = NULL;
   1148 		PMAP_COUNT(kenter_pa_unmanaged);
   1149 		flags |= PMAP_NOCACHE;
   1150 	} else {
   1151 		mdpg = VM_PAGE_TO_MD(pg);
   1152 	}
   1153 
   1154 	if ((flags & PMAP_NOCACHE) == 0 && !PMAP_PAGE_COLOROK_P(pa, va))
   1155 		PMAP_COUNT(kenter_pa_bad);
   1156 
   1157 	const pt_entry_t npte = pte_make_kenter_pa(pa, mdpg, prot, flags);
   1158 	kpreempt_disable();
   1159 	pt_entry_t * const ptep = pmap_pte_reserve(pmap_kernel(), va, 0);
   1160 	KASSERT(ptep != NULL);
   1161 	KASSERT(!pte_valid_p(*ptep));
   1162 	pmap_md_tlb_miss_lock_enter();
   1163 	*ptep = npte;
   1164 	/*
   1165 	 * We have the option to force this mapping into the TLB but we
   1166 	 * don't.  Instead let the next reference to the page do it.
   1167 	 */
   1168 	pmap_tlb_update_addr(pmap_kernel(), va, npte, 0);
   1169 	pmap_md_tlb_miss_lock_exit();
   1170 	kpreempt_enable();
   1171 #if DEBUG > 1
   1172 	for (u_int i = 0; i < PAGE_SIZE / sizeof(long); i++) {
   1173 		if (((long *)va)[i] != ((long *)pa)[i])
   1174 			panic("%s: contents (%lx) of va %#"PRIxVADDR
   1175 			    " != contents (%lx) of pa %#"PRIxPADDR, __func__,
   1176 			    ((long *)va)[i], va, ((long *)pa)[i], pa);
   1177 	}
   1178 #endif
   1179 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1180 }
   1181 
   1182 static bool
   1183 pmap_pte_kremove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1184 	uintptr_t flags)
   1185 {
   1186 	const pt_entry_t new_pt_entry = pte_nv_entry(true);
   1187 
   1188 	KASSERT(kpreempt_disabled());
   1189 
   1190 	/*
   1191 	 * Set every pt on every valid mapping within this segment.
   1192 	 */
   1193 	for (; sva < eva; sva += NBPG, ptep++) {
   1194 		pt_entry_t pt_entry = *ptep;
   1195 		if (!pte_valid_p(pt_entry)) {
   1196 			continue;
   1197 		}
   1198 
   1199 		PMAP_COUNT(kremove_pages);
   1200 		struct vm_page * const pg =
   1201 		    PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
   1202 		if (pg != NULL)
   1203 			pmap_md_vca_clean(pg, sva, PMAP_WBINV);
   1204 
   1205 		pmap_md_tlb_miss_lock_enter();
   1206 		*ptep = new_pt_entry;
   1207 		pmap_tlb_invalidate_addr(pmap_kernel(), sva);
   1208 		pmap_md_tlb_miss_lock_exit();
   1209 	}
   1210 
   1211 	return false;
   1212 }
   1213 
   1214 void
   1215 pmap_kremove(vaddr_t va, vsize_t len)
   1216 {
   1217 	const vaddr_t sva = trunc_page(va);
   1218 	const vaddr_t eva = round_page(va + len);
   1219 
   1220 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1221 	UVMHIST_LOG(pmaphist, "(va=%#"PRIxVADDR" len=%#"PRIxVSIZE")",
   1222 	    va, len, 0,0);
   1223 
   1224 	kpreempt_disable();
   1225 	pmap_pte_process(pmap_kernel(), sva, eva, pmap_pte_kremove, 0);
   1226 	kpreempt_enable();
   1227 
   1228 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1229 }
   1230 
   1231 void
   1232 pmap_remove_all(struct pmap *pmap)
   1233 {
   1234 	KASSERT(pmap != pmap_kernel());
   1235 
   1236 	kpreempt_disable();
   1237 	/*
   1238 	 * Free all of our ASIDs which means we can skip doing all the
   1239 	 * tlb_invalidate_addrs().
   1240 	 */
   1241 	pmap_md_tlb_miss_lock_enter();
   1242 	pmap_tlb_asid_deactivate(pmap);
   1243 	pmap_tlb_asid_release_all(pmap);
   1244 	pmap_md_tlb_miss_lock_exit();
   1245 	pmap->pm_flags |= PMAP_DEFERRED_ACTIVATE;
   1246 
   1247 	kpreempt_enable();
   1248 }
   1249 
   1250 /*
   1251  *	Routine:	pmap_unwire
   1252  *	Function:	Clear the wired attribute for a map/virtual-address
   1253  *			pair.
   1254  *	In/out conditions:
   1255  *			The mapping must already exist in the pmap.
   1256  */
   1257 void
   1258 pmap_unwire(pmap_t pmap, vaddr_t va)
   1259 {
   1260 
   1261 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1262 	UVMHIST_LOG(pmaphist, "(pmap=%p va=%#"PRIxVADDR")", pmap, va, 0,0);
   1263 	PMAP_COUNT(unwire);
   1264 
   1265 	/*
   1266 	 * Don't need to flush the TLB since PG_WIRED is only in software.
   1267 	 */
   1268 #ifdef PARANOIADIAG
   1269 	if (va < pmap->pm_minaddr || pmap->pm_maxaddr <= va)
   1270 		panic("pmap_unwire");
   1271 #endif
   1272 	kpreempt_disable();
   1273 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1274 	pt_entry_t pt_entry = *ptep;
   1275 #ifdef DIAGNOSTIC
   1276 	if (ptep == NULL)
   1277 		panic("%s: pmap %p va %#"PRIxVADDR" invalid STE",
   1278 		    __func__, pmap, va);
   1279 #endif
   1280 
   1281 #ifdef DIAGNOSTIC
   1282 	if (!pte_valid_p(pt_entry))
   1283 		panic("pmap_unwire: pmap %p va %#"PRIxVADDR" invalid PTE",
   1284 		    pmap, va);
   1285 #endif
   1286 
   1287 	if (pte_wired_p(pt_entry)) {
   1288 		pmap_md_tlb_miss_lock_enter();
   1289 		*ptep = pte_unwire_entry(*ptep);
   1290 		pmap_md_tlb_miss_lock_exit();
   1291 		pmap->pm_stats.wired_count--;
   1292 	}
   1293 #ifdef DIAGNOSTIC
   1294 	else {
   1295 		printf("%s: wiring for pmap %p va %#"PRIxVADDR" unchanged!\n",
   1296 		    __func__, pmap, va);
   1297 	}
   1298 #endif
   1299 	kpreempt_enable();
   1300 }
   1301 
   1302 /*
   1303  *	Routine:	pmap_extract
   1304  *	Function:
   1305  *		Extract the physical page address associated
   1306  *		with the given map/virtual_address pair.
   1307  */
   1308 bool
   1309 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap)
   1310 {
   1311 	paddr_t pa;
   1312 
   1313 	//UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1314 	//UVMHIST_LOG(pmaphist, "(pmap=%p va=%#"PRIxVADDR")", pmap, va, 0,0);
   1315 	if (pmap == pmap_kernel()) {
   1316 		if (pmap_md_direct_mapped_vaddr_p(va)) {
   1317 			pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1318 			goto done;
   1319 		}
   1320 		if (pmap_md_io_vaddr_p(va))
   1321 			panic("pmap_extract: io address %#"PRIxVADDR"", va);
   1322 	}
   1323 	kpreempt_disable();
   1324 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1325 	if (ptep == NULL) {
   1326 		//UVMHIST_LOG(pmaphist, "<- false (not in segmap)", 0,0,0,0);
   1327 		kpreempt_enable();
   1328 		return false;
   1329 	}
   1330 	if (!pte_valid_p(*ptep)) {
   1331 		//UVMHIST_LOG(pmaphist, "<- false (PTE not valid)", 0,0,0,0);
   1332 		kpreempt_enable();
   1333 		return false;
   1334 	}
   1335 	pa = pte_to_paddr(*ptep) | (va & PGOFSET);
   1336 	kpreempt_enable();
   1337 done:
   1338 	if (pap != NULL) {
   1339 		*pap = pa;
   1340 	}
   1341 	//UVMHIST_LOG(pmaphist, "<- true (pa %#"PRIxPADDR")", pa, 0,0,0);
   1342 	return true;
   1343 }
   1344 
   1345 /*
   1346  *	Copy the range specified by src_addr/len
   1347  *	from the source map to the range dst_addr/len
   1348  *	in the destination map.
   1349  *
   1350  *	This routine is only advisory and need not do anything.
   1351  */
   1352 void
   1353 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len,
   1354     vaddr_t src_addr)
   1355 {
   1356 
   1357 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1358 	PMAP_COUNT(copy);
   1359 }
   1360 
   1361 /*
   1362  *	pmap_clear_reference:
   1363  *
   1364  *	Clear the reference bit on the specified physical page.
   1365  */
   1366 bool
   1367 pmap_clear_reference(struct vm_page *pg)
   1368 {
   1369 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1370 
   1371 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1372 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR"))",
   1373 	   pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1374 
   1375 	bool rv = pmap_page_clear_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1376 
   1377 	UVMHIST_LOG(pmaphist, "<- %s", rv ? "true" : "false", 0,0,0);
   1378 
   1379 	return rv;
   1380 }
   1381 
   1382 /*
   1383  *	pmap_is_referenced:
   1384  *
   1385  *	Return whether or not the specified physical page is referenced
   1386  *	by any physical maps.
   1387  */
   1388 bool
   1389 pmap_is_referenced(struct vm_page *pg)
   1390 {
   1391 
   1392 	return VM_PAGEMD_REFERENCED_P(VM_PAGE_TO_MD(pg));
   1393 }
   1394 
   1395 /*
   1396  *	Clear the modify bits on the specified physical page.
   1397  */
   1398 bool
   1399 pmap_clear_modify(struct vm_page *pg)
   1400 {
   1401 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1402 	pv_entry_t pv = &mdpg->mdpg_first;
   1403 	pv_entry_t pv_next;
   1404 	uint16_t gen;
   1405 
   1406 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1407 	UVMHIST_LOG(pmaphist, "(pg=%p (%#"PRIxPADDR"))",
   1408 	    pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1409 	PMAP_COUNT(clear_modify);
   1410 
   1411 	if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1412 		if (pv->pv_pmap == NULL) {
   1413 			UVMHIST_LOG(pmapexechist,
   1414 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1415 			    pg, VM_PAGE_TO_PHYS(pg), "execpage cleared", 0);
   1416 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1417 			PMAP_COUNT(exec_uncached_clear_modify);
   1418 		} else {
   1419 			UVMHIST_LOG(pmapexechist,
   1420 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1421 			    pg, VM_PAGE_TO_PHYS(pg), "syncicache performed", 0);
   1422 			pmap_page_syncicache(pg);
   1423 			PMAP_COUNT(exec_synced_clear_modify);
   1424 		}
   1425 	}
   1426 	if (!pmap_page_clear_attributes(mdpg, VM_PAGEMD_MODIFIED)) {
   1427 		UVMHIST_LOG(pmaphist, "<- false", 0,0,0,0);
   1428 		return false;
   1429 	}
   1430 	if (pv->pv_pmap == NULL) {
   1431 		UVMHIST_LOG(pmaphist, "<- true (no mappings)", 0,0,0,0);
   1432 		return true;
   1433 	}
   1434 
   1435 	/*
   1436 	 * remove write access from any pages that are dirty
   1437 	 * so we can tell if they are written to again later.
   1438 	 * flush the VAC first if there is one.
   1439 	 */
   1440 	kpreempt_disable();
   1441 	gen = VM_PAGEMD_PVLIST_LOCK(mdpg, false);
   1442 	for (; pv != NULL; pv = pv_next) {
   1443 		pmap_t pmap = pv->pv_pmap;
   1444 		vaddr_t va = pv->pv_va;
   1445 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1446 		KASSERT(ptep);
   1447 		pv_next = pv->pv_next;
   1448 		pt_entry_t pt_entry = pte_prot_nowrite(*ptep);
   1449 		if (*ptep == pt_entry) {
   1450 			continue;
   1451 		}
   1452 		pmap_md_vca_clean(pg, va, PMAP_WBINV);
   1453 		pmap_md_tlb_miss_lock_enter();
   1454 		*ptep = pt_entry;
   1455 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1456 		pmap_tlb_invalidate_addr(pmap, va);
   1457 		pmap_md_tlb_miss_lock_exit();
   1458 		pmap_update(pmap);
   1459 		if (__predict_false(gen != VM_PAGEMD_PVLIST_LOCK(mdpg, false))) {
   1460 			/*
   1461 			 * The list changed!  So restart from the beginning.
   1462 			 */
   1463 			pv_next = &mdpg->mdpg_first;
   1464 		}
   1465 	}
   1466 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1467 	kpreempt_enable();
   1468 
   1469 	UVMHIST_LOG(pmaphist, "<- true (mappings changed)", 0,0,0,0);
   1470 	return true;
   1471 }
   1472 
   1473 /*
   1474  *	pmap_is_modified:
   1475  *
   1476  *	Return whether or not the specified physical page is modified
   1477  *	by any physical maps.
   1478  */
   1479 bool
   1480 pmap_is_modified(struct vm_page *pg)
   1481 {
   1482 
   1483 	return VM_PAGEMD_MODIFIED_P(VM_PAGE_TO_MD(pg));
   1484 }
   1485 
   1486 /*
   1487  *	pmap_set_modified:
   1488  *
   1489  *	Sets the page modified reference bit for the specified page.
   1490  */
   1491 void
   1492 pmap_set_modified(paddr_t pa)
   1493 {
   1494 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1495 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1496 	pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1497 }
   1498 
   1499 /******************** pv_entry management ********************/
   1500 
   1501 static void
   1502 pmap_check_pvlist(struct vm_page *pg)
   1503 {
   1504 #ifdef PARANOIADIAG
   1505 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1506 	pt_entry_t pv = &mdpg->mdpg_first;
   1507 	if (pv->pv_pmap != NULL) {
   1508 		for (; pv != NULL; pv = pv->pv_next) {
   1509 			KASSERT(!pmap_md_direct_mapped_vaddr_p(pv->pv_va));
   1510 		}
   1511 	}
   1512 #endif /* PARANOIADIAG */
   1513 }
   1514 
   1515 /*
   1516  * Enter the pmap and virtual address into the
   1517  * physical to virtual map table.
   1518  */
   1519 void
   1520 pmap_enter_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, u_int *npte)
   1521 {
   1522 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1523 	pv_entry_t pv, npv, apv;
   1524 	int16_t gen;
   1525 	bool first __unused = false;
   1526 
   1527 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1528 	UVMHIST_LOG(pmaphist,
   1529 	    "(pmap=%p va=%#"PRIxVADDR" pg=%p (%#"PRIxPADDR")",
   1530 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1531 	UVMHIST_LOG(pmaphist, "nptep=%p (%#x))", npte, *npte, 0, 0);
   1532 
   1533 	KASSERT(kpreempt_disabled());
   1534 	KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1535 
   1536 	apv = NULL;
   1537 	pv = &mdpg->mdpg_first;
   1538 	gen = VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1539 	pmap_check_pvlist(pg);
   1540 again:
   1541 	if (pv->pv_pmap == NULL) {
   1542 		KASSERT(pv->pv_next == NULL);
   1543 		/*
   1544 		 * No entries yet, use header as the first entry
   1545 		 */
   1546 		PMAP_COUNT(primary_mappings);
   1547 		PMAP_COUNT(mappings);
   1548 		first = true;
   1549 #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1550 		pmap_page_clear_attributes(pg, VM_PAGEMD_UNCACHED);
   1551 #endif
   1552 		pv->pv_pmap = pmap;
   1553 		pv->pv_va = va;
   1554 	} else {
   1555 		if (pmap_md_vca_add(pg, va, npte))
   1556 			goto again;
   1557 
   1558 		/*
   1559 		 * There is at least one other VA mapping this page.
   1560 		 * Place this entry after the header.
   1561 		 *
   1562 		 * Note: the entry may already be in the table if
   1563 		 * we are only changing the protection bits.
   1564 		 */
   1565 
   1566 #ifdef PARANOIADIAG
   1567 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1568 #endif
   1569 		for (npv = pv; npv; npv = npv->pv_next) {
   1570 			if (pmap == npv->pv_pmap && va == npv->pv_va) {
   1571 #ifdef PARANOIADIAG
   1572 				pt_entry_t *ptep = pmap_pte_lookup(pmap, va);
   1573 				pt_entry_t pt_entry = (ptep ? *ptep : 0);
   1574 				if (!pte_valid_p(pt_entry)
   1575 				    || pte_to_paddr(pt_entry) != pa)
   1576 					printf(
   1577 		"pmap_enter_pv: found va %#"PRIxVADDR" pa %#"PRIxPADDR" in pv_table but != %x\n",
   1578 					    va, pa, pt_entry);
   1579 #endif
   1580 				PMAP_COUNT(remappings);
   1581 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1582 				if (__predict_false(apv != NULL))
   1583 					pmap_pv_free(apv);
   1584 				return;
   1585 			}
   1586 		}
   1587 		if (__predict_true(apv == NULL)) {
   1588 			/*
   1589 			 * To allocate a PV, we have to release the PVLIST lock
   1590 			 * so get the page generation.  We allocate the PV, and
   1591 			 * then reacquire the lock.
   1592 			 */
   1593 			VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1594 
   1595 			apv = (pv_entry_t)pmap_pv_alloc();
   1596 			if (apv == NULL)
   1597 				panic("pmap_enter_pv: pmap_pv_alloc() failed");
   1598 
   1599 			/*
   1600 			 * If the generation has changed, then someone else
   1601 			 * tinkered with this page so we should
   1602 			 * start over.
   1603 			 */
   1604 			uint16_t oldgen = gen;
   1605 			gen = VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1606 			if (gen != oldgen)
   1607 				goto again;
   1608 		}
   1609 		npv = apv;
   1610 		apv = NULL;
   1611 		npv->pv_va = va;
   1612 		npv->pv_pmap = pmap;
   1613 		npv->pv_next = pv->pv_next;
   1614 		pv->pv_next = npv;
   1615 		PMAP_COUNT(mappings);
   1616 	}
   1617 	pmap_check_pvlist(pg);
   1618 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1619 	if (__predict_false(apv != NULL))
   1620 		pmap_pv_free(apv);
   1621 
   1622 	UVMHIST_LOG(pmaphist, "<- done pv=%p%s",
   1623 	    pv, first ? " (first pv)" : "",0,0);
   1624 }
   1625 
   1626 /*
   1627  * Remove a physical to virtual address translation.
   1628  * If cache was inhibited on this page, and there are no more cache
   1629  * conflicts, restore caching.
   1630  * Flush the cache if the last page is removed (should always be cached
   1631  * at this point).
   1632  */
   1633 void
   1634 pmap_remove_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, bool dirty)
   1635 {
   1636 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1637 	pv_entry_t pv, npv;
   1638 	bool last;
   1639 
   1640 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1641 	UVMHIST_LOG(pmaphist,
   1642 	    "(pmap=%p va=%#"PRIxVADDR" pg=%p (pa %#"PRIxPADDR")\n",
   1643 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1644 	UVMHIST_LOG(pmaphist, "dirty=%s)", dirty ? "true" : "false", 0,0,0);
   1645 
   1646 	KASSERT(kpreempt_disabled());
   1647 	pv = &mdpg->mdpg_first;
   1648 
   1649 	(void)VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1650 	pmap_check_pvlist(pg);
   1651 
   1652 	/*
   1653 	 * If it is the first entry on the list, it is actually
   1654 	 * in the header and we must copy the following entry up
   1655 	 * to the header.  Otherwise we must search the list for
   1656 	 * the entry.  In either case we free the now unused entry.
   1657 	 */
   1658 
   1659 	last = false;
   1660 	if (pmap == pv->pv_pmap && va == pv->pv_va) {
   1661 		npv = pv->pv_next;
   1662 		if (npv) {
   1663 			*pv = *npv;
   1664 			KASSERT(pv->pv_pmap != NULL);
   1665 		} else {
   1666 #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1667 			pmap_page_clear_attributes(pg, VM_PAGEMD_UNCACHED);
   1668 #endif
   1669 			pv->pv_pmap = NULL;
   1670 			last = true;	/* Last mapping removed */
   1671 		}
   1672 		PMAP_COUNT(remove_pvfirst);
   1673 	} else {
   1674 		for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
   1675 			PMAP_COUNT(remove_pvsearch);
   1676 			if (pmap == npv->pv_pmap && va == npv->pv_va)
   1677 				break;
   1678 		}
   1679 		if (npv) {
   1680 			pv->pv_next = npv->pv_next;
   1681 		}
   1682 	}
   1683 	pmap_md_vca_remove(pg, va);
   1684 
   1685 	pmap_check_pvlist(pg);
   1686 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1687 
   1688 	/*
   1689 	 * Free the pv_entry if needed.
   1690 	 */
   1691 	if (npv)
   1692 		pmap_pv_free(npv);
   1693 	if (VM_PAGEMD_EXECPAGE_P(mdpg) && dirty) {
   1694 		if (last) {
   1695 			/*
   1696 			 * If this was the page's last mapping, we no longer
   1697 			 * care about its execness.
   1698 			 */
   1699 			UVMHIST_LOG(pmapexechist,
   1700 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1701 			    pg, VM_PAGE_TO_PHYS(pg),
   1702 			    last ? " [last mapping]" : "",
   1703 			    "execpage cleared");
   1704 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1705 			PMAP_COUNT(exec_uncached_remove);
   1706 		} else {
   1707 			/*
   1708 			 * Someone still has it mapped as an executable page
   1709 			 * so we must sync it.
   1710 			 */
   1711 			UVMHIST_LOG(pmapexechist,
   1712 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1713 			    pg, VM_PAGE_TO_PHYS(pg),
   1714 			    last ? " [last mapping]" : "",
   1715 			    "performed syncicache");
   1716 			pmap_page_syncicache(pg);
   1717 			PMAP_COUNT(exec_synced_remove);
   1718 		}
   1719 	}
   1720 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1721 }
   1722 
   1723 #if defined(MULTIPROCESSOR)
   1724 struct pmap_pvlist_info {
   1725 	kmutex_t *pli_locks[PAGE_SIZE / 32];
   1726 	volatile u_int pli_lock_refs[PAGE_SIZE / 32];
   1727 	volatile u_int pli_lock_index;
   1728 	u_int pli_lock_mask;
   1729 } pmap_pvlist_info;
   1730 
   1731 void
   1732 pmap_pvlist_lock_init(size_t cache_line_size)
   1733 {
   1734 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   1735 	const vaddr_t lock_page = uvm_pageboot_alloc(PAGE_SIZE);
   1736 	vaddr_t lock_va = lock_page;
   1737 	if (sizeof(kmutex_t) > cache_line_size) {
   1738 		cache_line_size = roundup2(sizeof(kmutex_t), cache_line_size);
   1739 	}
   1740 	const size_t nlocks = PAGE_SIZE / cache_line_size;
   1741 	KASSERT((nlocks & (nlocks - 1)) == 0);
   1742 	/*
   1743 	 * Now divide the page into a number of mutexes, one per cacheline.
   1744 	 */
   1745 	for (size_t i = 0; i < nlocks; lock_va += cache_line_size, i++) {
   1746 		kmutex_t * const lock = (kmutex_t *)lock_va;
   1747 		mutex_init(lock, MUTEX_DEFAULT, IPL_VM);
   1748 		pli->pli_locks[i] = lock;
   1749 	}
   1750 	pli->pli_lock_mask = nlocks - 1;
   1751 }
   1752 
   1753 uint16_t
   1754 pmap_pvlist_lock(struct vm_page_md *mdpg, bool list_change)
   1755 {
   1756 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   1757 	kmutex_t *lock = mdpg->mdpg_lock;
   1758 	int16_t gen;
   1759 
   1760 	/*
   1761 	 * Allocate a lock on an as-needed basis.  This will hopefully give us
   1762 	 * semi-random distribution not based on page color.
   1763 	 */
   1764 	if (__predict_false(lock == NULL)) {
   1765 		size_t locknum = atomic_add_int_nv(&pli->pli_lock_index, 37);
   1766 		size_t lockid = locknum & pli->pli_lock_mask;
   1767 		kmutex_t * const new_lock = pli->pli_locks[lockid];
   1768 		/*
   1769 		 * Set the lock.  If some other thread already did, just use
   1770 		 * the one they assigned.
   1771 		 */
   1772 		lock = atomic_cas_ptr(&mdpg->mdpg_lock, NULL, new_lock);
   1773 		if (lock == NULL) {
   1774 			lock = new_lock;
   1775 			atomic_inc_uint(&pli->pli_lock_refs[lockid]);
   1776 		}
   1777 	}
   1778 
   1779 	/*
   1780 	 * Now finally lock the pvlists.
   1781 	 */
   1782 	mutex_spin_enter(lock);
   1783 
   1784 	/*
   1785 	 * If the locker will be changing the list, increment the high 16 bits
   1786 	 * of attrs so we use that as a generation number.
   1787 	 */
   1788 	gen = VM_PAGEMD_PVLIST_GEN(mdpg);		/* get old value */
   1789 	if (list_change)
   1790 		atomic_add_int(&mdpg->mdpg_attrs, 0x10000);
   1791 
   1792 	/*
   1793 	 * Return the generation number.
   1794 	 */
   1795 	return gen;
   1796 }
   1797 #else /* !MULTIPROCESSOR */
   1798 void
   1799 pmap_pvlist_lock_init(size_t cache_line_size)
   1800 {
   1801 	mutex_init(&pmap_pvlist_mutex, MUTEX_DEFAULT, IPL_VM);
   1802 }
   1803 
   1804 #ifdef MODULAR
   1805 uint16_t
   1806 pmap_pvlist_lock(struct vm_page_md *mdpg, bool list_change)
   1807 {
   1808 	/*
   1809 	 * We just use a global lock.
   1810 	 */
   1811 	if (__predict_false(mdpg->mdpg_lock == NULL)) {
   1812 		mdpg->mdpg_lock = &pmap_pvlist_mutex;
   1813 	}
   1814 
   1815 	/*
   1816 	 * Now finally lock the pvlists.
   1817 	 */
   1818 	mutex_spin_enter(mdpg->mdpg_lock);
   1819 
   1820 	return 0;
   1821 }
   1822 #endif /* MODULAR */
   1823 #endif /* !MULTIPROCESSOR */
   1824 
   1825 /*
   1826  * pmap_pv_page_alloc:
   1827  *
   1828  *	Allocate a page for the pv_entry pool.
   1829  */
   1830 void *
   1831 pmap_pv_page_alloc(struct pool *pp, int flags)
   1832 {
   1833 	struct vm_page *pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_USERESERVE);
   1834 	if (pg == NULL)
   1835 		return NULL;
   1836 
   1837 	return (void *)pmap_map_poolpage(VM_PAGE_TO_PHYS(pg));
   1838 }
   1839 
   1840 /*
   1841  * pmap_pv_page_free:
   1842  *
   1843  *	Free a pv_entry pool page.
   1844  */
   1845 void
   1846 pmap_pv_page_free(struct pool *pp, void *v)
   1847 {
   1848 	vaddr_t va = (vaddr_t)v;
   1849 
   1850 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   1851 	const paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1852 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1853 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1854 	pmap_md_vca_remove(pg, va);
   1855 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1856 	uvm_pagefree(pg);
   1857 }
   1858 
   1859 #ifdef PMAP_PREFER
   1860 /*
   1861  * Find first virtual address >= *vap that doesn't cause
   1862  * a cache alias conflict.
   1863  */
   1864 void
   1865 pmap_prefer(vaddr_t foff, vaddr_t *vap, vsize_t sz, int td)
   1866 {
   1867 	vaddr_t	va;
   1868 	vsize_t d;
   1869 	vsize_t prefer_mask = ptoa(uvmexp.colormask);
   1870 
   1871 	PMAP_COUNT(prefer_requests);
   1872 
   1873 	prefer_mask |= pmap_md_cache_prefer_mask();
   1874 
   1875 	if (prefer_mask) {
   1876 		va = *vap;
   1877 
   1878 		d = foff - va;
   1879 		d &= prefer_mask;
   1880 		if (d) {
   1881 			if (td)
   1882 				*vap = trunc_page(va -((-d) & prefer_mask));
   1883 			else
   1884 				*vap = round_page(va + d);
   1885 			PMAP_COUNT(prefer_adjustments);
   1886 		}
   1887 	}
   1888 }
   1889 #endif /* PMAP_PREFER */
   1890 
   1891 #ifdef PMAP_MAP_POOLPAGE
   1892 vaddr_t
   1893 pmap_map_poolpage(paddr_t pa)
   1894 {
   1895 
   1896 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1897 	KASSERT(pg);
   1898 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1899 	pmap_page_set_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1900 
   1901 	const vaddr_t va = pmap_md_map_poolpage(pa, NBPG);
   1902 	pmap_md_vca_add(pg, va, NULL);
   1903 	return va;
   1904 }
   1905 
   1906 paddr_t
   1907 pmap_unmap_poolpage(vaddr_t va)
   1908 {
   1909 
   1910 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   1911 	paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1912 
   1913 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1914 	KASSERT(pg);
   1915 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1916 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1917 	pmap_md_unmap_poolpage(va, NBPG);
   1918 	pmap_md_vca_remove(pg, va);
   1919 
   1920 	return pa;
   1921 }
   1922 #endif /* PMAP_MAP_POOLPAGE */
   1923