Home | History | Annotate | Line # | Download | only in pmap
pmap_tlb.c revision 1.14
      1 /*	$NetBSD: pmap_tlb.c,v 1.14 2016/07/12 15:30:46 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas at 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: pmap_tlb.c,v 1.14 2016/07/12 15:30:46 skrll Exp $");
     35 
     36 /*
     37  * Manages address spaces in a TLB.
     38  *
     39  * Normally there is a 1:1 mapping between a TLB and a CPU.  However, some
     40  * implementations may share a TLB between multiple CPUs (really CPU thread
     41  * contexts).  This requires the TLB abstraction to be separated from the
     42  * CPU abstraction.  It also requires that the TLB be locked while doing
     43  * TLB activities.
     44  *
     45  * For each TLB, we track the ASIDs in use in a bitmap and a list of pmaps
     46  * that have a valid ASID.
     47  *
     48  * We allocate ASIDs in increasing order until we have exhausted the supply,
     49  * then reinitialize the ASID space, and start allocating again at 1.  When
     50  * allocating from the ASID bitmap, we skip any ASID who has a corresponding
     51  * bit set in the ASID bitmap.  Eventually this causes the ASID bitmap to fill
     52  * and, when completely filled, a reinitialization of the ASID space.
     53  *
     54  * To reinitialize the ASID space, the ASID bitmap is reset and then the ASIDs
     55  * of non-kernel TLB entries get recorded in the ASID bitmap.  If the entries
     56  * in TLB consume more than half of the ASID space, all ASIDs are invalidated,
     57  * the ASID bitmap is recleared, and the list of pmaps is emptied.  Otherwise,
     58  * (the normal case), any ASID present in the TLB (even those which are no
     59  * longer used by a pmap) will remain active (allocated) and all other ASIDs
     60  * will be freed.  If the size of the TLB is much smaller than the ASID space,
     61  * this algorithm completely avoids TLB invalidation.
     62  *
     63  * For multiprocessors, we also have to deal TLB invalidation requests from
     64  * other CPUs, some of which are dealt with the reinitialization of the ASID
     65  * space.  Whereas above we keep the ASIDs of those pmaps which have active
     66  * TLB entries, this type of reinitialization preserves the ASIDs of any
     67  * "onproc" user pmap and all other ASIDs will be freed.  We must do this
     68  * since we can't change the current ASID.
     69  *
     70  * Each pmap has two bitmaps: pm_active and pm_onproc.  Each bit in pm_active
     71  * indicates whether that pmap has an allocated ASID for a CPU.  Each bit in
     72  * pm_onproc indicates that pmap's ASID is active (equal to the ASID in COP 0
     73  * register EntryHi) on a CPU.  The bit number comes from the CPU's cpu_index().
     74  * Even though these bitmaps contain the bits for all CPUs, the bits that
     75  * correspond to the bits belonging to the CPUs sharing a TLB can only be
     76  * manipulated while holding that TLB's lock.  Atomic ops must be used to
     77  * update them since multiple CPUs may be changing different sets of bits at
     78  * same time but these sets never overlap.
     79  *
     80  * When a change to the local TLB may require a change in the TLB's of other
     81  * CPUs, we try to avoid sending an IPI if at all possible.  For instance, if
     82  * we are updating a PTE and that PTE previously was invalid and therefore
     83  * couldn't support an active mapping, there's no need for an IPI since there
     84  * can't be a TLB entry to invalidate.  The other case is when we change a PTE
     85  * to be modified we just update the local TLB.  If another TLB has a stale
     86  * entry, a TLB MOD exception will be raised and that will cause the local TLB
     87  * to be updated.
     88  *
     89  * We never need to update a non-local TLB if the pmap doesn't have a valid
     90  * ASID for that TLB.  If it does have a valid ASID but isn't current "onproc"
     91  * we simply reset its ASID for that TLB and then when it goes "onproc" it
     92  * will allocate a new ASID and any existing TLB entries will be orphaned.
     93  * Only in the case that pmap has an "onproc" ASID do we actually have to send
     94  * an IPI.
     95  *
     96  * Once we determined we must send an IPI to shootdown a TLB, we need to send
     97  * it to one of CPUs that share that TLB.  We choose the lowest numbered CPU
     98  * that has one of the pmap's ASID "onproc".  In reality, any CPU sharing that
     99  * TLB would do, but interrupting an active CPU seems best.
    100  *
    101  * A TLB might have multiple shootdowns active concurrently.  The shootdown
    102  * logic compresses these into a few cases:
    103  *	0) nobody needs to have its TLB entries invalidated
    104  *	1) one ASID needs to have its TLB entries invalidated
    105  *	2) more than one ASID needs to have its TLB entries invalidated
    106  *	3) the kernel needs to have its TLB entries invalidated
    107  *	4) the kernel and one or more ASID need their TLB entries invalidated.
    108  *
    109  * And for each case we do:
    110  *	0) nothing,
    111  *	1) if that ASID is still "onproc", we invalidate the TLB entries for
    112  *	   that single ASID.  If not, just reset the pmap's ASID to invalidate
    113  *	   and let it allocate a new ASID the next time it goes "onproc",
    114  *	2) we reinitialize the ASID space (preserving any "onproc" ASIDs) and
    115  *	   invalidate all non-wired non-global TLB entries,
    116  *	3) we invalidate all of the non-wired global TLB entries,
    117  *	4) we reinitialize the ASID space (again preserving any "onproc" ASIDs)
    118  *	   invalidate all non-wired TLB entries.
    119  *
    120  * As you can see, shootdowns are not concerned with addresses, just address
    121  * spaces.  Since the number of TLB entries is usually quite small, this avoids
    122  * a lot of overhead for not much gain.
    123  */
    124 
    125 #define __PMAP_PRIVATE
    126 
    127 #include "opt_multiprocessor.h"
    128 
    129 #include <sys/param.h>
    130 #include <sys/systm.h>
    131 #include <sys/proc.h>
    132 #include <sys/mutex.h>
    133 #include <sys/atomic.h>
    134 #include <sys/kernel.h>			/* for cold */
    135 #include <sys/cpu.h>
    136 
    137 #include <uvm/uvm.h>
    138 
    139 static kmutex_t pmap_tlb0_lock __cacheline_aligned;
    140 
    141 #define	IFCONSTANT(x)	(__builtin_constant_p((x)) ? (x) : 0)
    142 
    143 struct pmap_tlb_info pmap_tlb0_info = {
    144 	.ti_name = "tlb0",
    145 	.ti_asid_hint = KERNEL_PID + 1,
    146 #ifdef PMAP_TLB_NUM_PIDS
    147 	.ti_asid_max = IFCONSTANT(PMAP_TLB_NUM_PIDS - 1),
    148 	.ti_asids_free = IFCONSTANT(PMAP_TLB_NUM_PIDS - (1 + KERNEL_PID)),
    149 #endif
    150 	.ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1,
    151 #ifdef PMAP_TLB_WIRED_UPAGES
    152 	.ti_wired = PMAP_TLB_WIRED_UPAGES,
    153 #endif
    154 	.ti_lock = &pmap_tlb0_lock,
    155 	.ti_pais = LIST_HEAD_INITIALIZER(pmap_tlb0_info.ti_pais),
    156 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
    157 	.ti_tlbinvop = TLBINV_NOBODY,
    158 #endif
    159 };
    160 
    161 #undef IFCONSTANT
    162 
    163 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
    164 struct pmap_tlb_info *pmap_tlbs[PMAP_TLB_MAX] = {
    165 	[0] = &pmap_tlb0_info,
    166 };
    167 u_int pmap_ntlbs = 1;
    168 #endif
    169 
    170 #define	__BITMAP_SET(bm, n) \
    171 	((bm)[(n) / (8*sizeof(bm[0]))] |= 1LU << ((n) % (8*sizeof(bm[0]))))
    172 #define	__BITMAP_CLR(bm, n) \
    173 	((bm)[(n) / (8*sizeof(bm[0]))] &= ~(1LU << ((n) % (8*sizeof(bm[0])))))
    174 #define	__BITMAP_ISSET_P(bm, n) \
    175 	(((bm)[(n) / (8*sizeof(bm[0]))] & (1LU << ((n) % (8*sizeof(bm[0]))))) != 0)
    176 
    177 #define	TLBINFO_ASID_MARK_UNUSED(ti, asid) \
    178 	__BITMAP_CLR((ti)->ti_asid_bitmap, (asid))
    179 #define	TLBINFO_ASID_MARK_USED(ti, asid) \
    180 	__BITMAP_SET((ti)->ti_asid_bitmap, (asid))
    181 #define	TLBINFO_ASID_INUSE_P(ti, asid) \
    182 	__BITMAP_ISSET_P((ti)->ti_asid_bitmap, (asid))
    183 
    184 #ifdef MULTIPROCESSOR
    185 __unused static inline bool
    186 pmap_tlb_intersecting_active_p(pmap_t pm, struct pmap_tlb_info *ti)
    187 {
    188 #if PMAP_TLB_MAX == 1
    189 	return !kcpuset_iszero(pm->pm_active);
    190 #else
    191 	return kcpuset_intersecting_p(pm->pm_active, ti->ti_kcpuset);
    192 #endif
    193 }
    194 
    195 static inline bool
    196 pmap_tlb_intersecting_onproc_p(pmap_t pm, struct pmap_tlb_info *ti)
    197 {
    198 #if PMAP_TLB_MAX == 1
    199 	return !kcpuset_iszero(pm->pm_onproc);
    200 #else
    201 	return kcpuset_intersecting_p(pm->pm_onproc, ti->ti_kcpuset);
    202 #endif
    203 }
    204 #endif
    205 
    206 static void
    207 pmap_tlb_pai_check(struct pmap_tlb_info *ti)
    208 {
    209 #ifdef DIAGNOSTIC
    210 	struct pmap_asid_info *pai;
    211 	LIST_FOREACH(pai, &ti->ti_pais, pai_link) {
    212 		KASSERT(pai != NULL);
    213 		KASSERT(PAI_PMAP(pai, ti) != pmap_kernel());
    214 		KASSERT(pai->pai_asid > KERNEL_PID);
    215 		KASSERTMSG(pai->pai_asid <= ti->ti_asid_max,
    216 		    "pm %p asid %#x", PAI_PMAP(pai, ti), pai->pai_asid);
    217 		KASSERTMSG(TLBINFO_ASID_INUSE_P(ti, pai->pai_asid),
    218 		    "pm %p asid %u", PAI_PMAP(pai, ti), pai->pai_asid);
    219 #ifdef MULTIPROCESSOR
    220 		KASSERT(pmap_tlb_intersecting_active_p(PAI_PMAP(pai, ti), ti));
    221 #endif
    222 	}
    223 #endif
    224 }
    225 
    226 static void
    227 pmap_tlb_pai_reset(struct pmap_tlb_info *ti, struct pmap_asid_info *pai,
    228 	struct pmap *pm)
    229 {
    230 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    231 	UVMHIST_LOG(maphist, "(ti=%p, pai=%p, pm=%p): asid %u",
    232 	    ti, pai, pm, pai->pai_asid);
    233 
    234 	/*
    235 	 * We must have an ASID but it must not be onproc (on a processor).
    236 	 */
    237 	KASSERT(pai->pai_asid > KERNEL_PID);
    238 	KASSERT(pai->pai_asid <= ti->ti_asid_max);
    239 #if defined(MULTIPROCESSOR)
    240 	KASSERT(pmap_tlb_intersecting_active_p(pm, ti));
    241 	KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
    242 #endif
    243 	LIST_REMOVE(pai, pai_link);
    244 #ifdef DIAGNOSTIC
    245 	pai->pai_link.le_prev = NULL;	/* tagged as unlinked */
    246 #endif
    247 	/*
    248 	 * If the platform has a cheap way to flush ASIDs then free the ASID
    249 	 * back into the pool.  On multiprocessor systems, we will flush the
    250 	 * ASID from the TLB when it's allocated.  That way we know the flush
    251 	 * was always done in the correct TLB space.  On uniprocessor systems,
    252 	 * just do the flush now since we know that it has been used.  This has
    253 	 * a bit less overhead.  Either way, this will mean that we will only
    254 	 * need to flush all ASIDs if all ASIDs are in use and we need to
    255 	 * allocate a new one.
    256 	 */
    257 	if (PMAP_TLB_FLUSH_ASID_ON_RESET) {
    258 #ifndef MULTIPROCESSOR
    259 		tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
    260 #endif
    261 		if (TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
    262 			TLBINFO_ASID_MARK_UNUSED(ti, pai->pai_asid);
    263 			ti->ti_asids_free++;
    264 		}
    265 	}
    266 	/*
    267 	 * Note that we don't mark the ASID as not in use in the TLB's ASID
    268 	 * bitmap (thus it can't be allocated until the ASID space is exhausted
    269 	 * and therefore reinitialized).  We don't want to flush the TLB for
    270 	 * entries belonging to this ASID so we will let natural TLB entry
    271 	 * replacement flush them out of the TLB.  Any new entries for this
    272 	 * pmap will need a new ASID allocated.
    273 	 */
    274 	pai->pai_asid = 0;
    275 
    276 #if defined(MULTIPROCESSOR)
    277 	/*
    278 	 * The bits in pm_active belonging to this TLB can only be changed
    279 	 * while this TLB's lock is held.
    280 	 */
    281 #if PMAP_TLB_MAX == 1
    282 	kcpuset_zero(pm->pm_active);
    283 #else
    284 	kcpuset_remove(pm->pm_active, ti->ti_kcpuset);
    285 #endif
    286 	KASSERT(!pmap_tlb_intersecting_active_p(pm, ti));
    287 #endif /* MULTIPROCESSOR */
    288 
    289 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
    290 }
    291 
    292 void
    293 pmap_tlb_info_evcnt_attach(struct pmap_tlb_info *ti)
    294 {
    295 #if defined(MULTIPROCESSOR)
    296 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_desired,
    297 	    EVCNT_TYPE_MISC, NULL,
    298 	    ti->ti_name, "icache syncs desired");
    299 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_asts,
    300 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
    301 	    ti->ti_name, "icache sync asts");
    302 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_all,
    303 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
    304 	    ti->ti_name, "icache full syncs");
    305 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_pages,
    306 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
    307 	    ti->ti_name, "icache pages synced");
    308 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_duplicate,
    309 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
    310 	    ti->ti_name, "icache dup pages skipped");
    311 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_deferred,
    312 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
    313 	    ti->ti_name, "icache pages deferred");
    314 #endif /* MULTIPROCESSOR */
    315 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_asid_reinits,
    316 	    EVCNT_TYPE_MISC, NULL,
    317 	    ti->ti_name, "asid pool reinit");
    318 }
    319 
    320 void
    321 pmap_tlb_info_init(struct pmap_tlb_info *ti)
    322 {
    323 #if defined(MULTIPROCESSOR)
    324 #if PMAP_TLB_MAX == 1
    325 	KASSERT(ti == &pmap_tlb0_info);
    326 #else
    327 	if (ti != &pmap_tlb0_info) {
    328 		KASSERT(pmap_ntlbs < PMAP_TLB_MAX);
    329 
    330 		KASSERT(pmap_tlbs[pmap_ntlbs] == NULL);
    331 
    332 		ti->ti_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    333 		ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
    334 		ti->ti_asid_hint = KERNEL_PID + 1;
    335 		ti->ti_asid_max = pmap_tlbs[0]->ti_asid_max;
    336 		ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
    337 		ti->ti_tlbinvop = TLBINV_NOBODY,
    338 		ti->ti_victim = NULL;
    339 		kcpuset_create(&ti->ti_kcpuset, true);
    340 		ti->ti_index = pmap_ntlbs++;
    341 		ti->ti_wired = 0;
    342 		pmap_tlbs[ti->ti_index] = ti;
    343 		snprintf(ti->ti_name, sizeof(ti->ti_name), "tlb%u",
    344 		    ti->ti_index);
    345 		pmap_tlb_info_evcnt_attach(ti);
    346 		return;
    347 	}
    348 #endif
    349 #endif /* MULTIPROCESSOR */
    350 	KASSERT(ti == &pmap_tlb0_info);
    351 	KASSERT(ti->ti_lock == &pmap_tlb0_lock);
    352 	//printf("ti_lock %p ", ti->ti_lock);
    353 	mutex_init(ti->ti_lock, MUTEX_DEFAULT, IPL_SCHED);
    354 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
    355 	kcpuset_create(&ti->ti_kcpuset, true);
    356 	kcpuset_set(ti->ti_kcpuset, cpu_index(curcpu()));
    357 #endif
    358 	//printf("asid ");
    359 	if (ti->ti_asid_max == 0) {
    360 		ti->ti_asid_max = pmap_md_tlb_asid_max();
    361 		ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
    362 	}
    363 
    364 	KASSERT(ti->ti_asid_max < sizeof(ti->ti_asid_bitmap)*8);
    365 }
    366 
    367 #if defined(MULTIPROCESSOR)
    368 void
    369 pmap_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
    370 {
    371 	KASSERT(!CPU_IS_PRIMARY(ci));
    372 	KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    373 	KASSERT(cold);
    374 
    375 	TLBINFO_LOCK(ti);
    376 #if PMAP_TLB_MAX > 1
    377 	kcpuset_set(ti->ti_kcpuset, cpu_index(ci));
    378 	cpu_set_tlb_info(ci, ti);
    379 #endif
    380 
    381 	/*
    382 	 * Do any MD tlb info init.
    383 	 */
    384 	pmap_md_tlb_info_attach(ti, ci);
    385 
    386 	/*
    387 	 * The kernel pmap uses the kcpuset_running set so it's always
    388 	 * up-to-date.
    389 	 */
    390 	TLBINFO_UNLOCK(ti);
    391 }
    392 #endif /* MULTIPROCESSOR */
    393 
    394 #ifdef DIAGNOSTIC
    395 static size_t
    396 pmap_tlb_asid_count(struct pmap_tlb_info *ti)
    397 {
    398 	size_t count = 0;
    399 	for (tlb_asid_t asid = 1; asid <= ti->ti_asid_max; asid++) {
    400 		count += TLBINFO_ASID_INUSE_P(ti, asid);
    401 	}
    402 	return count;
    403 }
    404 #endif
    405 
    406 static void
    407 pmap_tlb_asid_reinitialize(struct pmap_tlb_info *ti, enum tlb_invalidate_op op)
    408 {
    409 	const size_t asid_bitmap_words =
    410 	    ti->ti_asid_max / (8 * sizeof(ti->ti_asid_bitmap[0]));
    411 
    412 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    413 	UVMHIST_LOG(maphist, "(ti=%p, op=%u)", ti, op, 0, 0);
    414 
    415 	pmap_tlb_pai_check(ti);
    416 
    417 	ti->ti_evcnt_asid_reinits.ev_count++;
    418 
    419 	/*
    420 	 * First, clear the ASID bitmap (except for ASID 0 which belongs
    421 	 * to the kernel).
    422 	 */
    423 	ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
    424 	ti->ti_asid_hint = KERNEL_PID + 1;
    425 	ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
    426 	for (size_t word = 1; word <= asid_bitmap_words; word++) {
    427 		ti->ti_asid_bitmap[word] = 0;
    428 	}
    429 
    430 	switch (op) {
    431 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    432 	case TLBINV_ALL:
    433 		tlb_invalidate_all();
    434 		break;
    435 	case TLBINV_ALLUSER:
    436 		tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
    437 		break;
    438 #endif /* MULTIPROCESSOR && PMAP_TLB_NEED_SHOOTDOWN */
    439 	case TLBINV_NOBODY: {
    440 		/*
    441 		 * If we are just reclaiming ASIDs in the TLB, let's go find
    442 		 * what ASIDs are in use in the TLB.  Since this is a
    443 		 * semi-expensive operation, we don't want to do it too often.
    444 		 * So if more half of the ASIDs are in use, we don't have
    445 		 * enough free ASIDs so invalidate the TLB entries with ASIDs
    446 		 * and clear the ASID bitmap.  That will force everyone to
    447 		 * allocate a new ASID.
    448 		 */
    449 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
    450 		pmap_tlb_asid_check();
    451 		const u_int asids_found = tlb_record_asids(ti->ti_asid_bitmap,
    452 		    ti->ti_asid_max);
    453 		pmap_tlb_asid_check();
    454 		KASSERT(asids_found == pmap_tlb_asid_count(ti));
    455 		if (__predict_false(asids_found >= ti->ti_asid_max / 2)) {
    456 			tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
    457 #else /* MULTIPROCESSOR && !PMAP_TLB_NEED_SHOOTDOWN */
    458 			/*
    459 			 * For those systems (PowerPC) that don't require
    460 			 * cross cpu TLB shootdowns, we have to invalidate the
    461 			 * entire TLB because we can't record the ASIDs in use
    462 			 * on the other CPUs.  This is hopefully cheaper than
    463 			 * than trying to use an IPI to record all the ASIDs
    464 			 * on all the CPUs (which would be a synchronization
    465 			 * nightmare).
    466 			 */
    467 			tlb_invalidate_all();
    468 #endif /* MULTIPROCESSOR && !PMAP_TLB_NEED_SHOOTDOWN */
    469 			ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
    470 			for (size_t word = 1;
    471 			     word <= asid_bitmap_words;
    472 			     word++) {
    473 				ti->ti_asid_bitmap[word] = 0;
    474 			}
    475 			ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
    476 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
    477 		} else {
    478 			ti->ti_asids_free -= asids_found;
    479 		}
    480 #endif /* !MULTIPROCESSOR || PMAP_TLB_NEED_SHOOTDOWN */
    481 		KASSERTMSG(ti->ti_asids_free <= ti->ti_asid_max, "%u",
    482 		    ti->ti_asids_free);
    483 		break;
    484 	}
    485 	default:
    486 		panic("%s: unexpected op %d", __func__, op);
    487 	}
    488 
    489 	/*
    490 	 * Now go through the active ASIDs.  If the ASID is on a processor or
    491 	 * we aren't invalidating all ASIDs and the TLB has an entry owned by
    492 	 * that ASID, mark it as in use.  Otherwise release the ASID.
    493 	 */
    494 	struct pmap_asid_info *pai, *next;
    495 	for (pai = LIST_FIRST(&ti->ti_pais); pai != NULL; pai = next) {
    496 		struct pmap * const pm = PAI_PMAP(pai, ti);
    497 		next = LIST_NEXT(pai, pai_link);
    498 		KASSERT(pm != pmap_kernel());
    499 		KASSERT(pai->pai_asid > KERNEL_PID);
    500 #if defined(MULTIPROCESSOR)
    501 		if (pmap_tlb_intersecting_onproc_p(pm, ti)) {
    502 			if (!TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
    503 				TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
    504 				ti->ti_asids_free--;
    505 			}
    506 			continue;
    507 		}
    508 #endif /* MULTIPROCESSOR */
    509 		if (TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
    510 			KASSERT(op == TLBINV_NOBODY);
    511 		} else {
    512 			pmap_tlb_pai_reset(ti, pai, pm);
    513 		}
    514 	}
    515 #ifdef DIAGNOSTIC
    516 	size_t free_count __diagused = ti->ti_asid_max - pmap_tlb_asid_count(ti);
    517 	KASSERTMSG(free_count == ti->ti_asids_free,
    518 	    "bitmap error: %zu != %u", free_count, ti->ti_asids_free);
    519 #endif
    520 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
    521 }
    522 
    523 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    524 #if PMAP_TLB_MAX == 1
    525 #error shootdown not required for single TLB systems
    526 #endif
    527 void
    528 pmap_tlb_shootdown_process(void)
    529 {
    530 	struct cpu_info * const ci = curcpu();
    531 	struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
    532 #ifdef DIAGNOSTIC
    533 	struct pmap * const pm = curlwp->l_proc->p_vmspace->vm_map.pmap;
    534 #endif
    535 
    536 	KASSERT(cpu_intr_p());
    537 	KASSERTMSG(ci->ci_cpl >= IPL_SCHED,
    538 	    "%s: cpl (%d) < IPL_SCHED (%d)",
    539 	    __func__, ci->ci_cpl, IPL_SCHED);
    540 
    541 	TLBINFO_LOCK(ti);
    542 
    543 	switch (ti->ti_tlbinvop) {
    544 	case TLBINV_ONE: {
    545 		/*
    546 		 * We only need to invalidate one user ASID.
    547 		 */
    548 		struct pmap_asid_info * const pai = PMAP_PAI(ti->ti_victim, ti);
    549 		KASSERT(ti->ti_victim != pmap_kernel());
    550 		if (!pmap_tlb_intersecting_onproc_p(ti->ti_victim, ti)) {
    551 			/*
    552 			 * The victim is an active pmap so we will just
    553 			 * invalidate its TLB entries.
    554 			 */
    555 			KASSERT(pai->pai_asid > KERNEL_PID);
    556 			pmap_tlb_asid_check();
    557 			tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
    558 			pmap_tlb_asid_check();
    559 		} else if (pai->pai_asid) {
    560 			/*
    561 			 * The victim is no longer an active pmap for this TLB.
    562 			 * So simply clear its ASID and when pmap_activate is
    563 			 * next called for this pmap, it will allocate a new
    564 			 * ASID.
    565 			 */
    566 			KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
    567 			pmap_tlb_pai_reset(ti, pai, PAI_PMAP(pai, ti));
    568 		}
    569 		break;
    570 	}
    571 	case TLBINV_ALLUSER:
    572 		/*
    573 		 * Flush all user TLB entries.
    574 		 */
    575 		pmap_tlb_asid_reinitialize(ti, TLBINV_ALLUSER);
    576 		break;
    577 	case TLBINV_ALLKERNEL:
    578 		/*
    579 		 * We need to invalidate all global TLB entries.
    580 		 */
    581 		pmap_tlb_asid_check();
    582 		tlb_invalidate_globals();
    583 		pmap_tlb_asid_check();
    584 		break;
    585 	case TLBINV_ALL:
    586 		/*
    587 		 * Flush all the TLB entries (user and kernel).
    588 		 */
    589 		pmap_tlb_asid_reinitialize(ti, TLBINV_ALL);
    590 		break;
    591 	case TLBINV_NOBODY:
    592 		/*
    593 		 * Might be spurious or another SMT CPU sharing this TLB
    594 		 * could have already done the work.
    595 		 */
    596 		break;
    597 	}
    598 
    599 	/*
    600 	 * Indicate we are done with shutdown event.
    601 	 */
    602 	ti->ti_victim = NULL;
    603 	ti->ti_tlbinvop = TLBINV_NOBODY;
    604 	TLBINFO_UNLOCK(ti);
    605 }
    606 
    607 /*
    608  * This state machine could be encoded into an array of integers but since all
    609  * the values fit in 3 bits, the 5 entry "table" fits in a 16 bit value which
    610  * can be loaded in a single instruction.
    611  */
    612 #define	TLBINV_MAP(op, nobody, one, alluser, allkernel, all)	\
    613 	((((   (nobody) << 3*TLBINV_NOBODY)			\
    614 	 | (      (one) << 3*TLBINV_ONE)			\
    615 	 | (  (alluser) << 3*TLBINV_ALLUSER)			\
    616 	 | ((allkernel) << 3*TLBINV_ALLKERNEL)			\
    617 	 | (      (all) << 3*TLBINV_ALL)) >> 3*(op)) & 7)
    618 
    619 #define	TLBINV_USER_MAP(op)	\
    620 	TLBINV_MAP(op, TLBINV_ONE, TLBINV_ALLUSER, TLBINV_ALLUSER,	\
    621 	    TLBINV_ALL, TLBINV_ALL)
    622 
    623 #define	TLBINV_KERNEL_MAP(op)	\
    624 	TLBINV_MAP(op, TLBINV_ALLKERNEL, TLBINV_ALL, TLBINV_ALL,	\
    625 	    TLBINV_ALLKERNEL, TLBINV_ALL)
    626 
    627 bool
    628 pmap_tlb_shootdown_bystanders(pmap_t pm)
    629 {
    630 	/*
    631 	 * We don't need to deal our own TLB.
    632 	 */
    633 
    634 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    635 
    636 	kcpuset_t *pm_active;
    637 	kcpuset_clone(&pm_active, pm->pm_active);
    638 	kcpuset_remove(pm_active, cpu_tlb_info(curcpu())->ti_kcpuset);
    639 	const bool kernel_p = (pm == pmap_kernel());
    640 	bool ipi_sent = false;
    641 
    642 	/*
    643 	 * If pm_active gets more bits set, then it's after all our changes
    644 	 * have been made so they will already be cognizant of them.
    645 	 */
    646 
    647 	for (size_t i = 0; !kcpuset_iszero(pm_active); i++) {
    648 		KASSERT(i < pmap_ntlbs);
    649 		struct pmap_tlb_info * const ti = pmap_tlbs[i];
    650 		KASSERT(tlbinfo_index(ti) == i);
    651 		/*
    652 		 * Skip this TLB if there are no active mappings for it.
    653 		 */
    654 		if (!kcpuset_intersecting_p(pm_active, ti->ti_kcpuset))
    655 			continue;
    656 		struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
    657 		kcpuset_remove(pm_active, ti->ti_kcpuset);
    658 		TLBINFO_LOCK(ti);
    659 		cpuid_t j = kcpuset_ffs_intersecting(pm->pm_onproc,
    660 		    ti->ti_kcpuset);
    661 		// post decrement since ffs returns bit + 1 or 0 if no bit
    662 		if (j-- > 0) {
    663 			if (kernel_p) {
    664 				ti->ti_tlbinvop =
    665 				    TLBINV_KERNEL_MAP(ti->ti_tlbinvop);
    666 				ti->ti_victim = NULL;
    667 			} else {
    668 				KASSERT(pai->pai_asid);
    669 				if (__predict_false(ti->ti_victim == pm)) {
    670 					KASSERT(ti->ti_tlbinvop == TLBINV_ONE);
    671 					/*
    672 					 * We still need to invalidate this one
    673 					 * ASID so there's nothing to change.
    674 					 */
    675 				} else {
    676 					ti->ti_tlbinvop =
    677 					    TLBINV_USER_MAP(ti->ti_tlbinvop);
    678 					if (ti->ti_tlbinvop == TLBINV_ONE)
    679 						ti->ti_victim = pm;
    680 					else
    681 						ti->ti_victim = NULL;
    682 				}
    683 			}
    684 			TLBINFO_UNLOCK(ti);
    685 			/*
    686 			 * Now we can send out the shootdown IPIs to a CPU
    687 			 * that shares this TLB and is currently using this
    688 			 * pmap.  That CPU will process the IPI and do the
    689 			 * all the work.  Any other CPUs sharing that TLB
    690 			 * will take advantage of that work.  pm_onproc might
    691 			 * change now that we have released the lock but we
    692 			 * can tolerate spurious shootdowns.
    693 			 */
    694 			cpu_send_ipi(cpu_lookup(j), IPI_SHOOTDOWN);
    695 			ipi_sent = true;
    696 			continue;
    697 		}
    698 		if (!pmap_tlb_intersecting_active_p(pm, ti)) {
    699 			/*
    700 			 * If this pmap has an ASID assigned but it's not
    701 			 * currently running, nuke its ASID.  Next time the
    702 			 * pmap is activated, it will allocate a new ASID.
    703 			 * And best of all, we avoid an IPI.
    704 			 */
    705 			KASSERT(!kernel_p);
    706 			pmap_tlb_pai_reset(ti, pai, pm);
    707 			//ti->ti_evcnt_lazy_shots.ev_count++;
    708 		}
    709 		TLBINFO_UNLOCK(ti);
    710 	}
    711 
    712 	kcpuset_destroy(pm_active);
    713 
    714 	UVMHIST_LOG(maphist, " <-- done (ipi_sent=%d)", ipi_sent, 0, 0, 0);
    715 
    716 	return ipi_sent;
    717 }
    718 #endif /* MULTIPROCESSOR && PMAP_TLB_NEED_SHOOTDOWN */
    719 
    720 #ifndef PMAP_TLB_HWPAGEWALKER
    721 int
    722 pmap_tlb_update_addr(pmap_t pm, vaddr_t va, pt_entry_t pte, u_int flags)
    723 {
    724 	struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
    725 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
    726 	int rv = -1;
    727 
    728 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    729 	UVMHIST_LOG(maphist,
    730 	    " (pm=%p va=%#"PRIxVADDR", pte=%#"PRIxPTE" flags=%#x)",
    731 	    pm, va, pte_value(pte), flags);
    732 
    733 	KASSERT(kpreempt_disabled());
    734 
    735 	KASSERTMSG(pte_valid_p(pte), "va %#"PRIxVADDR" %#"PRIxPTE,
    736 	    va, pte_value(pte));
    737 
    738 	TLBINFO_LOCK(ti);
    739 	if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
    740 		pmap_tlb_asid_check();
    741 		rv = tlb_update_addr(va, pai->pai_asid, pte,
    742 		    (flags & PMAP_TLB_INSERT) != 0);
    743 		pmap_tlb_asid_check();
    744 		UVMHIST_LOG(maphist,
    745 		     "   %d <-- tlb_update_addr(%#"PRIxVADDR", %#x, %#"PRIxPTE", ...)",
    746 		     rv, va, pai->pai_asid, pte_value(pte));
    747 		KASSERTMSG((flags & PMAP_TLB_INSERT) == 0 || rv == 1,
    748 		    "pmap %p (asid %u) va %#"PRIxVADDR" pte %#"PRIxPTE" rv %d",
    749 		    pm, pai->pai_asid, va, pte_value(pte), rv);
    750 	}
    751 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    752 	if (flags & PMAP_TLB_NEED_IPI)
    753 		pm->pm_shootdown_pending = 1;
    754 #endif
    755 	TLBINFO_UNLOCK(ti);
    756 
    757 	UVMHIST_LOG(maphist, "   <-- done (rv=%d)", rv, 0, 0, 0);
    758 
    759 	return rv;
    760 }
    761 #endif /* !PMAP_TLB_HWPAGEWALKER */
    762 
    763 void
    764 pmap_tlb_invalidate_addr(pmap_t pm, vaddr_t va)
    765 {
    766 	struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
    767 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
    768 
    769 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    770 	UVMHIST_LOG(maphist, " (pm=%p va=%#"PRIxVADDR") ti=%p asid=%#x",
    771 	    pm, va, ti, pai->pai_asid);
    772 
    773 	KASSERT(kpreempt_disabled());
    774 
    775 	TLBINFO_LOCK(ti);
    776 	if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
    777 		pmap_tlb_asid_check();
    778 		UVMHIST_LOG(maphist, " invalidating %#"PRIxVADDR" asid %#x",
    779 		    va, pai->pai_asid, 0, 0);
    780 		tlb_invalidate_addr(va, pai->pai_asid);
    781 		pmap_tlb_asid_check();
    782 	}
    783 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    784 	pm->pm_shootdown_pending = 1;
    785 #endif
    786 	TLBINFO_UNLOCK(ti);
    787 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
    788 }
    789 
    790 static inline void
    791 pmap_tlb_asid_alloc(struct pmap_tlb_info *ti, pmap_t pm,
    792 	struct pmap_asid_info *pai)
    793 {
    794 	/*
    795 	 * We shouldn't have an ASID assigned, and thusly must not be onproc
    796 	 * nor active.
    797 	 */
    798 	KASSERT(pm != pmap_kernel());
    799 	KASSERT(pai->pai_asid == 0);
    800 	KASSERT(pai->pai_link.le_prev == NULL);
    801 #if defined(MULTIPROCESSOR)
    802 	KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
    803 	KASSERT(!pmap_tlb_intersecting_active_p(pm, ti));
    804 #endif
    805 	KASSERT(ti->ti_asids_free > 0);
    806 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
    807 
    808 	/*
    809 	 * If the last ASID allocated was the maximum ASID, then the
    810 	 * hint will be out of range.  Reset the hint to first
    811 	 * available ASID.
    812 	 */
    813 	if (PMAP_TLB_FLUSH_ASID_ON_RESET
    814 	    && ti->ti_asid_hint > ti->ti_asid_max) {
    815 		ti->ti_asid_hint = KERNEL_PID + 1;
    816 	}
    817 	KASSERTMSG(ti->ti_asid_hint <= ti->ti_asid_max, "hint %u",
    818 	    ti->ti_asid_hint);
    819 
    820 	/*
    821 	 * Let's see if the hinted ASID is free.  If not search for
    822 	 * a new one.
    823 	 */
    824 	if (__predict_true(TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint))) {
    825 		const size_t nbpw __diagused = 8*sizeof(ti->ti_asid_bitmap[0]);
    826 		size_t i;
    827 		u_long bits;
    828 		for (i = 0; (bits = ~ti->ti_asid_bitmap[i]) == 0; i++) {
    829 			KASSERT(i < __arraycount(ti->ti_asid_bitmap) - 1);
    830 		}
    831 		/*
    832 		 * ffs wants to find the first bit set while we want
    833 		 * to find the first bit cleared.
    834 		 */
    835 		const u_int n = __builtin_ffsl(bits) - 1;
    836 		KASSERTMSG((bits << (nbpw - (n+1))) == (1ul << (nbpw-1)),
    837 		    "n %u bits %#lx", n, bits);
    838 		KASSERT(n < nbpw);
    839 		ti->ti_asid_hint = n + i * nbpw;
    840 	}
    841 
    842 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
    843 	KASSERT(ti->ti_asid_hint <= ti->ti_asid_max);
    844 	KASSERTMSG(PMAP_TLB_FLUSH_ASID_ON_RESET
    845 	    || TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint - 1),
    846 	    "hint %u bitmap %p", ti->ti_asid_hint, ti->ti_asid_bitmap);
    847 	KASSERTMSG(!TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint),
    848 	    "hint %u bitmap %p", ti->ti_asid_hint, ti->ti_asid_bitmap);
    849 
    850 	/*
    851 	 * The hint contains our next ASID so take it and advance the hint.
    852 	 * Mark it as used and insert the pai into the list of active asids.
    853 	 * There is also one less asid free in this TLB.
    854 	 */
    855 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
    856 	pai->pai_asid = ti->ti_asid_hint++;
    857 #ifdef MULTIPROCESSOR
    858 	if (PMAP_TLB_FLUSH_ASID_ON_RESET) {
    859 		/*
    860 		 * Clean the new ASID from the TLB.
    861 		 */
    862 		tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
    863 	}
    864 #endif
    865 	TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
    866 	LIST_INSERT_HEAD(&ti->ti_pais, pai, pai_link);
    867 	ti->ti_asids_free--;
    868 
    869 #if defined(MULTIPROCESSOR)
    870 	/*
    871 	 * Mark that we now have an active ASID for all CPUs sharing this TLB.
    872 	 * The bits in pm_active belonging to this TLB can only be changed
    873 	 * while this TLBs lock is held.
    874 	 */
    875 #if PMAP_TLB_MAX == 1
    876 	kcpuset_copy(pm->pm_active, kcpuset_running);
    877 #else
    878 	kcpuset_merge(pm->pm_active, ti->ti_kcpuset);
    879 #endif
    880 #endif
    881 }
    882 
    883 /*
    884  * Acquire a TLB address space tag (called ASID or TLBPID) and return it.
    885  * ASID might have already been previously acquired.
    886  */
    887 void
    888 pmap_tlb_asid_acquire(pmap_t pm, struct lwp *l)
    889 {
    890 	struct cpu_info * const ci = l->l_cpu;
    891 	struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
    892 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
    893 
    894 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    895 	UVMHIST_LOG(maphist, "(pm=%p, l=%p, ti=%p)", pm, l, ti, 0);
    896 
    897 	KASSERT(kpreempt_disabled());
    898 
    899 	/*
    900 	 * Kernels use a fixed ASID and thus doesn't need to acquire one.
    901 	 */
    902 	if (pm == pmap_kernel()) {
    903 		UVMHIST_LOG(maphist, " <-- done (kernel)", 0, 0, 0, 0);
    904 		return;
    905 	}
    906 
    907 	TLBINFO_LOCK(ti);
    908 	KASSERT(pai->pai_asid <= KERNEL_PID || pai->pai_link.le_prev != NULL);
    909 	KASSERT(pai->pai_asid > KERNEL_PID || pai->pai_link.le_prev == NULL);
    910 	pmap_tlb_pai_check(ti);
    911 	if (__predict_false(!PMAP_PAI_ASIDVALID_P(pai, ti))) {
    912 		/*
    913 		 * If we've run out ASIDs, reinitialize the ASID space.
    914 		 */
    915 		if (__predict_false(tlbinfo_noasids_p(ti))) {
    916 			KASSERT(l == curlwp);
    917 			UVMHIST_LOG(maphist, " asid reinit", 0, 0, 0, 0);
    918 			pmap_tlb_asid_reinitialize(ti, TLBINV_NOBODY);
    919 			KASSERT(!tlbinfo_noasids_p(ti));
    920 		}
    921 
    922 		/*
    923 		 * Get an ASID.
    924 		 */
    925 		pmap_tlb_asid_alloc(ti, pm, pai);
    926 		UVMHIST_LOG(maphist, "allocated asid %#x", pai->pai_asid, 0, 0, 0);
    927 	}
    928 	pmap_tlb_pai_check(ti);
    929 #if defined(MULTIPROCESSOR)
    930 	KASSERT(kcpuset_isset(pm->pm_active, cpu_index(ci)));
    931 #endif
    932 
    933 	if (l == curlwp) {
    934 #if defined(MULTIPROCESSOR)
    935 		/*
    936 		 * The bits in pm_onproc belonging to this TLB can only
    937 		 * be changed while this TLBs lock is held unless atomic
    938 		 * operations are used.
    939 		 */
    940 		KASSERT(pm != pmap_kernel());
    941 		kcpuset_atomic_set(pm->pm_onproc, cpu_index(ci));
    942 #endif
    943 		ci->ci_pmap_asid_cur = pai->pai_asid;
    944 		UVMHIST_LOG(maphist, "setting asid to %#x", pai->pai_asid, 0, 0, 0);
    945 		tlb_set_asid(pai->pai_asid);
    946 		pmap_tlb_asid_check();
    947 	} else {
    948 		printf("%s: l (%p) != curlwp %p\n", __func__, l, curlwp);
    949 	}
    950 	TLBINFO_UNLOCK(ti);
    951 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
    952 }
    953 
    954 void
    955 pmap_tlb_asid_deactivate(pmap_t pm)
    956 {
    957 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    958 
    959 	KASSERT(kpreempt_disabled());
    960 #if defined(MULTIPROCESSOR)
    961 	/*
    962 	 * The kernel pmap is aways onproc and active and must never have
    963 	 * those bits cleared.  If pmap_remove_all was called, it has already
    964 	 * deactivated the pmap and thusly onproc will be 0 so there's nothing
    965 	 * to do.
    966 	 */
    967 	if (pm != pmap_kernel() && !kcpuset_iszero(pm->pm_onproc)) {
    968 		struct cpu_info * const ci = curcpu();
    969 		KASSERT(!cpu_intr_p());
    970 		KASSERTMSG(kcpuset_isset(pm->pm_onproc, cpu_index(ci)),
    971 		    "%s: pmap %p onproc %p doesn't include cpu %d (%p)",
    972 		    __func__, pm, pm->pm_onproc, cpu_index(ci), ci);
    973 		/*
    974 		 * The bits in pm_onproc that belong to this TLB can
    975 		 * be changed while this TLBs lock is not held as long
    976 		 * as we use atomic ops.
    977 		 */
    978 		kcpuset_atomic_clear(pm->pm_onproc, cpu_index(ci));
    979 	}
    980 #endif
    981 	curcpu()->ci_pmap_asid_cur = 0;
    982 	UVMHIST_LOG(maphist, " <-- done (pm=%p)", pm, 0, 0, 0);
    983 	tlb_set_asid(KERNEL_PID);
    984 	pmap_tlb_pai_check(cpu_tlb_info(curcpu()));
    985 #if defined(DEBUG)
    986 	pmap_tlb_asid_check();
    987 #endif
    988 }
    989 
    990 void
    991 pmap_tlb_asid_release_all(struct pmap *pm)
    992 {
    993 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    994 	UVMHIST_LOG(maphist, "(pm=%p)", pm, 0, 0, 0);
    995 
    996 	KASSERT(pm != pmap_kernel());
    997 #if defined(MULTIPROCESSOR)
    998 	//KASSERT(!kcpuset_iszero(pm->pm_onproc)); // XXX
    999 	struct cpu_info * const ci __diagused = curcpu();
   1000 	KASSERT(!kcpuset_isotherset(pm->pm_onproc, cpu_index(ci)));
   1001 #if PMAP_TLB_MAX > 1
   1002 	for (u_int i = 0; !kcpuset_iszero(pm->pm_active); i++) {
   1003 		KASSERT(i < pmap_ntlbs);
   1004 		struct pmap_tlb_info * const ti = pmap_tlbs[i];
   1005 #else
   1006 		struct pmap_tlb_info * const ti = &pmap_tlb0_info;
   1007 #endif
   1008 		struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
   1009 		TLBINFO_LOCK(ti);
   1010 		if (PMAP_PAI_ASIDVALID_P(pai, ti)) {
   1011 			/*
   1012 			 * This pmap should not be in use by any other cpu so
   1013 			 * we can just reset and be happy.
   1014 			 */
   1015 			if (ti->ti_victim == pm)
   1016 				ti->ti_victim = NULL;
   1017 			pmap_tlb_pai_reset(ti, pai, pm);
   1018 		}
   1019 		KASSERT(pai->pai_link.le_prev == NULL);
   1020 		TLBINFO_UNLOCK(ti);
   1021 #if PMAP_TLB_MAX > 1
   1022 	}
   1023 #endif
   1024 #ifdef DIAGNOSTIC
   1025 	for (size_t i = 0; i < (PMAP_TLB_MAX > 1 ? pmap_ntlbs : 1); i++) {
   1026 		KASSERTMSG(pm->pm_pai[i].pai_asid == 0,
   1027 		    "pm %p i %zu asid %u",
   1028 		    pm, i, pm->pm_pai[i].pai_asid);
   1029 	}
   1030 #endif
   1031 #else
   1032 	/*
   1033 	 * Handle the case of an UP kernel which only has, at most, one ASID.
   1034 	 * If the pmap has an ASID allocated, free it.
   1035 	 */
   1036 	struct pmap_tlb_info * const ti = &pmap_tlb0_info;
   1037 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
   1038 	TLBINFO_LOCK(ti);
   1039 	if (pai->pai_asid > KERNEL_PID) {
   1040 		if (curcpu()->ci_pmap_asid_cur == pai->pai_asid) {
   1041 			tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
   1042 		} else {
   1043 			pmap_tlb_pai_reset(ti, pai, pm);
   1044 		}
   1045 	}
   1046 	TLBINFO_UNLOCK(ti);
   1047 #endif /* MULTIPROCESSOR */
   1048 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
   1049 }
   1050 
   1051 void
   1052 pmap_tlb_asid_check(void)
   1053 {
   1054 #ifdef DEBUG
   1055 	kpreempt_disable();
   1056 	const tlb_asid_t asid __debugused = tlb_get_asid();
   1057 	KDASSERTMSG(asid == curcpu()->ci_pmap_asid_cur,
   1058 	   "%s: asid (%#x) != current asid (%#x)",
   1059 	    __func__, asid, curcpu()->ci_pmap_asid_cur);
   1060 	kpreempt_enable();
   1061 #endif
   1062 }
   1063 
   1064 #ifdef DEBUG
   1065 void
   1066 pmap_tlb_check(pmap_t pm, bool (*func)(void *, vaddr_t, tlb_asid_t, pt_entry_t))
   1067 {
   1068         struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
   1069         struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
   1070         TLBINFO_LOCK(ti);
   1071         if (pm == pmap_kernel() || pai->pai_asid > KERNEL_PID)
   1072 		tlb_walk(pm, func);
   1073         TLBINFO_UNLOCK(ti);
   1074 }
   1075 #endif /* DEBUG */
   1076