xref: /netbsd-src/sys/uvm/pmap/pmap_tlb.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /*	$NetBSD: pmap_tlb.c,v 1.30 2019/12/18 11:27:56 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.30 2019/12/18 11:27:56 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 
131 #include <sys/atomic.h>
132 #include <sys/cpu.h>
133 #include <sys/kernel.h>			/* for cold */
134 #include <sys/mutex.h>
135 #include <sys/proc.h>
136 #include <sys/systm.h>
137 
138 #include <uvm/uvm.h>
139 
140 static kmutex_t pmap_tlb0_lock __cacheline_aligned;
141 
142 #define	IFCONSTANT(x)	(__builtin_constant_p((x)) ? (x) : 0)
143 
144 #if KERNEL_PID > 31
145 #error "KERNEL_PID expected in range 0-31"
146 #endif
147 
148 #define	TLBINFO_ASID_MARK_UNUSED(ti, asid) \
149 	__BITMAP_CLR((asid), &(ti)->ti_asid_bitmap)
150 #define	TLBINFO_ASID_MARK_USED(ti, asid) \
151 	__BITMAP_SET((asid), &(ti)->ti_asid_bitmap)
152 #define	TLBINFO_ASID_INUSE_P(ti, asid) \
153 	__BITMAP_ISSET((asid), &(ti)->ti_asid_bitmap)
154 #define	TLBINFO_ASID_RESET(ti) \
155 	do {								\
156 		__BITMAP_ZERO(&ti->ti_asid_bitmap);			\
157 		for (tlb_asid_t asid = 0; asid <= KERNEL_PID; asid++) 	\
158 			TLBINFO_ASID_MARK_USED(ti, asid);	 	\
159 	} while (0)
160 #define	TLBINFO_ASID_INITIAL_FREE(asid_max) \
161 	(asid_max + 1 /* 0 */ - (1 + KERNEL_PID))
162 
163 struct pmap_tlb_info pmap_tlb0_info = {
164 	.ti_name = "tlb0",
165 	.ti_asid_hint = KERNEL_PID + 1,
166 #ifdef PMAP_TLB_NUM_PIDS
167 	.ti_asid_max = IFCONSTANT(PMAP_TLB_NUM_PIDS - 1),
168 	.ti_asids_free = IFCONSTANT(
169 		TLBINFO_ASID_INITIAL_FREE(PMAP_TLB_NUM_PIDS - 1)),
170 #endif
171 	.ti_asid_bitmap._b[0] = __BITS(0, KERNEL_PID),
172 #ifdef PMAP_TLB_WIRED_UPAGES
173 	.ti_wired = PMAP_TLB_WIRED_UPAGES,
174 #endif
175 	.ti_lock = &pmap_tlb0_lock,
176 	.ti_pais = LIST_HEAD_INITIALIZER(pmap_tlb0_info.ti_pais),
177 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
178 	.ti_tlbinvop = TLBINV_NOBODY,
179 #endif
180 };
181 
182 #undef IFCONSTANT
183 
184 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
185 struct pmap_tlb_info *pmap_tlbs[PMAP_TLB_MAX] = {
186 	[0] = &pmap_tlb0_info,
187 };
188 u_int pmap_ntlbs = 1;
189 #endif
190 
191 #ifdef MULTIPROCESSOR
192 __unused static inline bool
193 pmap_tlb_intersecting_active_p(pmap_t pm, struct pmap_tlb_info *ti)
194 {
195 #if PMAP_TLB_MAX == 1
196 	return !kcpuset_iszero(pm->pm_active);
197 #else
198 	return kcpuset_intersecting_p(pm->pm_active, ti->ti_kcpuset);
199 #endif
200 }
201 
202 static inline bool
203 pmap_tlb_intersecting_onproc_p(pmap_t pm, struct pmap_tlb_info *ti)
204 {
205 #if PMAP_TLB_MAX == 1
206 	return !kcpuset_iszero(pm->pm_onproc);
207 #else
208 	return kcpuset_intersecting_p(pm->pm_onproc, ti->ti_kcpuset);
209 #endif
210 }
211 #endif
212 
213 static void
214 pmap_tlb_pai_check(struct pmap_tlb_info *ti, bool locked_p)
215 {
216 #ifdef DIAGNOSTIC
217 	struct pmap_asid_info *pai;
218 	if (!locked_p)
219 		TLBINFO_LOCK(ti);
220 	LIST_FOREACH(pai, &ti->ti_pais, pai_link) {
221 		KASSERT(pai != NULL);
222 		KASSERT(PAI_PMAP(pai, ti) != pmap_kernel());
223 		KASSERT(pai->pai_asid > KERNEL_PID);
224 		KASSERTMSG(pai->pai_asid <= ti->ti_asid_max,
225 		    "pm %p asid %#x", PAI_PMAP(pai, ti), pai->pai_asid);
226 		KASSERTMSG(TLBINFO_ASID_INUSE_P(ti, pai->pai_asid),
227 		    "pm %p asid %u", PAI_PMAP(pai, ti), pai->pai_asid);
228 #ifdef MULTIPROCESSOR
229 		KASSERT(pmap_tlb_intersecting_active_p(PAI_PMAP(pai, ti), ti));
230 #endif
231 	}
232 	if (!locked_p)
233 		TLBINFO_UNLOCK(ti);
234 #endif
235 }
236 
237 static void
238 pmap_tlb_pai_reset(struct pmap_tlb_info *ti, struct pmap_asid_info *pai,
239 	struct pmap *pm)
240 {
241 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
242 	UVMHIST_LOG(maphist, "(ti=%#jx, pai=%#jx, pm=%#jx): asid %u",
243 	    (uintptr_t)ti, (uintptr_t)pai, (uintptr_t)pm, pai->pai_asid);
244 
245 	/*
246 	 * We must have an ASID but it must not be onproc (on a processor).
247 	 */
248 	KASSERT(pai->pai_asid > KERNEL_PID);
249 	KASSERT(pai->pai_asid <= ti->ti_asid_max);
250 #if defined(MULTIPROCESSOR)
251 	KASSERT(pmap_tlb_intersecting_active_p(pm, ti));
252 	KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
253 #endif
254 	LIST_REMOVE(pai, pai_link);
255 #ifdef DIAGNOSTIC
256 	pai->pai_link.le_prev = NULL;	/* tagged as unlinked */
257 #endif
258 	/*
259 	 * If the platform has a cheap way to flush ASIDs then free the ASID
260 	 * back into the pool.  On multiprocessor systems, we will flush the
261 	 * ASID from the TLB when it's allocated.  That way we know the flush
262 	 * was always done in the correct TLB space.  On uniprocessor systems,
263 	 * just do the flush now since we know that it has been used.  This has
264 	 * a bit less overhead.  Either way, this will mean that we will only
265 	 * need to flush all ASIDs if all ASIDs are in use and we need to
266 	 * allocate a new one.
267 	 */
268 	if (PMAP_TLB_FLUSH_ASID_ON_RESET) {
269 #ifndef MULTIPROCESSOR
270 		tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
271 #endif
272 		if (TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
273 			TLBINFO_ASID_MARK_UNUSED(ti, pai->pai_asid);
274 			ti->ti_asids_free++;
275 		}
276 	}
277 	/*
278 	 * Note that we don't mark the ASID as not in use in the TLB's ASID
279 	 * bitmap (thus it can't be allocated until the ASID space is exhausted
280 	 * and therefore reinitialized).  We don't want to flush the TLB for
281 	 * entries belonging to this ASID so we will let natural TLB entry
282 	 * replacement flush them out of the TLB.  Any new entries for this
283 	 * pmap will need a new ASID allocated.
284 	 */
285 	pai->pai_asid = 0;
286 
287 #if defined(MULTIPROCESSOR)
288 	/*
289 	 * The bits in pm_active belonging to this TLB can only be changed
290 	 * while this TLB's lock is held.
291 	 */
292 #if PMAP_TLB_MAX == 1
293 	kcpuset_zero(pm->pm_active);
294 #else
295 	kcpuset_remove(pm->pm_active, ti->ti_kcpuset);
296 #endif
297 	KASSERT(!pmap_tlb_intersecting_active_p(pm, ti));
298 #endif /* MULTIPROCESSOR */
299 
300 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
301 }
302 
303 void
304 pmap_tlb_info_evcnt_attach(struct pmap_tlb_info *ti)
305 {
306 #if defined(MULTIPROCESSOR) && !defined(PMAP_TLB_NO_SYNCI_EVCNT)
307 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_desired,
308 	    EVCNT_TYPE_MISC, NULL,
309 	    ti->ti_name, "icache syncs desired");
310 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_asts,
311 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
312 	    ti->ti_name, "icache sync asts");
313 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_all,
314 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
315 	    ti->ti_name, "icache full syncs");
316 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_pages,
317 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
318 	    ti->ti_name, "icache pages synced");
319 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_duplicate,
320 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
321 	    ti->ti_name, "icache dup pages skipped");
322 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_deferred,
323 	    EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
324 	    ti->ti_name, "icache pages deferred");
325 #endif /* MULTIPROCESSOR && !PMAP_TLB_NO_SYNCI_EVCNT */
326 	evcnt_attach_dynamic_nozero(&ti->ti_evcnt_asid_reinits,
327 	    EVCNT_TYPE_MISC, NULL,
328 	    ti->ti_name, "asid pool reinit");
329 }
330 
331 void
332 pmap_tlb_info_init(struct pmap_tlb_info *ti)
333 {
334 #if defined(MULTIPROCESSOR)
335 #if PMAP_TLB_MAX == 1
336 	KASSERT(ti == &pmap_tlb0_info);
337 #else
338 	if (ti != &pmap_tlb0_info) {
339 		KASSERT(pmap_ntlbs < PMAP_TLB_MAX);
340 
341 		KASSERT(pmap_tlbs[pmap_ntlbs] == NULL);
342 
343 		ti->ti_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
344 		TLBINFO_ASID_RESET(ti);
345 		ti->ti_asid_hint = KERNEL_PID + 1;
346 		ti->ti_asid_max = pmap_tlbs[0]->ti_asid_max;
347 		ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti->ti_asid_max);
348 		ti->ti_tlbinvop = TLBINV_NOBODY;
349 		ti->ti_victim = NULL;
350 		kcpuset_create(&ti->ti_kcpuset, true);
351 		ti->ti_index = pmap_ntlbs++;
352 		ti->ti_wired = 0;
353 		pmap_tlbs[ti->ti_index] = ti;
354 		snprintf(ti->ti_name, sizeof(ti->ti_name), "tlb%u",
355 		    ti->ti_index);
356 		pmap_tlb_info_evcnt_attach(ti);
357 
358 		KASSERT(ti->ti_asid_max < PMAP_TLB_BITMAP_LENGTH);
359 		return;
360 	}
361 #endif
362 #endif /* MULTIPROCESSOR */
363 	KASSERT(ti == &pmap_tlb0_info);
364 	KASSERT(ti->ti_lock == &pmap_tlb0_lock);
365 	//printf("ti_lock %p ", ti->ti_lock);
366 	mutex_init(ti->ti_lock, MUTEX_DEFAULT, IPL_SCHED);
367 #if defined(MULTIPROCESSOR) && PMAP_TLB_MAX > 1
368 	kcpuset_create(&ti->ti_kcpuset, true);
369 	kcpuset_set(ti->ti_kcpuset, cpu_index(curcpu()));
370 #endif
371 	//printf("asid ");
372 	if (ti->ti_asid_max == 0) {
373 		ti->ti_asid_max = pmap_md_tlb_asid_max();
374 		ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti->ti_asid_max);
375 	}
376 
377 	KASSERT(ti->ti_asid_max < PMAP_TLB_BITMAP_LENGTH);
378 }
379 
380 #if defined(MULTIPROCESSOR)
381 void
382 pmap_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
383 {
384 	KASSERT(!CPU_IS_PRIMARY(ci));
385 	KASSERT(ci->ci_data.cpu_idlelwp != NULL);
386 	KASSERT(cold);
387 
388 	TLBINFO_LOCK(ti);
389 #if PMAP_TLB_MAX > 1
390 	kcpuset_set(ti->ti_kcpuset, cpu_index(ci));
391 	cpu_set_tlb_info(ci, ti);
392 #endif
393 
394 	/*
395 	 * Do any MD tlb info init.
396 	 */
397 	pmap_md_tlb_info_attach(ti, ci);
398 
399 	/*
400 	 * The kernel pmap uses the kcpuset_running set so it's always
401 	 * up-to-date.
402 	 */
403 	TLBINFO_UNLOCK(ti);
404 }
405 #endif /* MULTIPROCESSOR */
406 
407 #ifdef DIAGNOSTIC
408 static size_t
409 pmap_tlb_asid_count(struct pmap_tlb_info *ti)
410 {
411 	size_t count = 0;
412 	for (tlb_asid_t asid = 1; asid <= ti->ti_asid_max; asid++) {
413 		if (TLBINFO_ASID_INUSE_P(ti, asid))
414 			count++;
415 	}
416 	return count;
417 }
418 #endif
419 
420 static void
421 pmap_tlb_asid_reinitialize(struct pmap_tlb_info *ti, enum tlb_invalidate_op op)
422 {
423 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
424 	UVMHIST_LOG(maphist, "(ti=%#jx, op=%ju)", (uintptr_t)ti, op, 0, 0);
425 
426 	pmap_tlb_pai_check(ti, true);
427 
428 	ti->ti_evcnt_asid_reinits.ev_count++;
429 
430 	/*
431 	 * First, clear the ASID bitmap (except for ASID 0 which belongs
432 	 * to the kernel).
433 	 */
434 	ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(ti->ti_asid_max);
435 	ti->ti_asid_hint = KERNEL_PID + 1;
436 	TLBINFO_ASID_RESET(ti);
437 
438 	switch (op) {
439 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
440 	case TLBINV_ALL:
441 		tlb_invalidate_all();
442 		break;
443 	case TLBINV_ALLUSER:
444 		tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
445 		break;
446 #endif /* MULTIPROCESSOR && PMAP_TLB_NEED_SHOOTDOWN */
447 	case TLBINV_NOBODY: {
448 		/*
449 		 * If we are just reclaiming ASIDs in the TLB, let's go find
450 		 * what ASIDs are in use in the TLB.  Since this is a
451 		 * semi-expensive operation, we don't want to do it too often.
452 		 * So if more half of the ASIDs are in use, we don't have
453 		 * enough free ASIDs so invalidate the TLB entries with ASIDs
454 		 * and clear the ASID bitmap.  That will force everyone to
455 		 * allocate a new ASID.
456 		 */
457 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
458 		pmap_tlb_asid_check();
459 		const u_int asids_found = tlb_record_asids(
460 		    ti->ti_asid_bitmap._b, ti->ti_asid_max);
461 		pmap_tlb_asid_check();
462 #ifdef DIAGNOSTIC
463 		const u_int asids_count = pmap_tlb_asid_count(ti);
464 #endif
465 		KASSERTMSG(asids_found == asids_count,
466 		    "found %u != count %u", asids_found, asids_count);
467 		if (__predict_false(asids_found >= ti->ti_asid_max / 2)) {
468 			tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
469 #else /* MULTIPROCESSOR && !PMAP_TLB_NEED_SHOOTDOWN */
470 			/*
471 			 * For those systems (PowerPC) that don't require
472 			 * cross cpu TLB shootdowns, we have to invalidate the
473 			 * entire TLB because we can't record the ASIDs in use
474 			 * on the other CPUs.  This is hopefully cheaper than
475 			 * than trying to use an IPI to record all the ASIDs
476 			 * on all the CPUs (which would be a synchronization
477 			 * nightmare).
478 			 */
479 			tlb_invalidate_all();
480 #endif /* MULTIPROCESSOR && !PMAP_TLB_NEED_SHOOTDOWN */
481 			TLBINFO_ASID_RESET(ti);
482 			ti->ti_asids_free = TLBINFO_ASID_INITIAL_FREE(
483 				ti->ti_asid_max);
484 #if !defined(MULTIPROCESSOR) || defined(PMAP_TLB_NEED_SHOOTDOWN)
485 		} else {
486 			ti->ti_asids_free -= asids_found;
487 		}
488 #endif /* !MULTIPROCESSOR || PMAP_TLB_NEED_SHOOTDOWN */
489 		KASSERTMSG(ti->ti_asids_free <= ti->ti_asid_max, "%u",
490 		    ti->ti_asids_free);
491 		break;
492 	}
493 	default:
494 		panic("%s: unexpected op %d", __func__, op);
495 	}
496 
497 	/*
498 	 * Now go through the active ASIDs.  If the ASID is on a processor or
499 	 * we aren't invalidating all ASIDs and the TLB has an entry owned by
500 	 * that ASID, mark it as in use.  Otherwise release the ASID.
501 	 */
502 	struct pmap_asid_info *pai, *next;
503 	for (pai = LIST_FIRST(&ti->ti_pais); pai != NULL; pai = next) {
504 		struct pmap * const pm = PAI_PMAP(pai, ti);
505 		next = LIST_NEXT(pai, pai_link);
506 		KASSERT(pm != pmap_kernel());
507 		KASSERT(pai->pai_asid > KERNEL_PID);
508 #if defined(MULTIPROCESSOR)
509 		if (pmap_tlb_intersecting_onproc_p(pm, ti)) {
510 			if (!TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
511 				TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
512 				ti->ti_asids_free--;
513 			}
514 			continue;
515 		}
516 #endif /* MULTIPROCESSOR */
517 		if (TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
518 			KASSERT(op == TLBINV_NOBODY);
519 		} else {
520 			pmap_tlb_pai_reset(ti, pai, pm);
521 		}
522 	}
523 #ifdef DIAGNOSTIC
524 	size_t free_count __diagused = ti->ti_asid_max - pmap_tlb_asid_count(ti);
525 	KASSERTMSG(free_count == ti->ti_asids_free,
526 	    "bitmap error: %zu != %u", free_count, ti->ti_asids_free);
527 #endif
528 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
529 }
530 
531 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
532 #if PMAP_TLB_MAX == 1
533 #error shootdown not required for single TLB systems
534 #endif
535 void
536 pmap_tlb_shootdown_process(void)
537 {
538 	struct cpu_info * const ci = curcpu();
539 	struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
540 #ifdef DIAGNOSTIC
541 	struct pmap * const pm = curlwp->l_proc->p_vmspace->vm_map.pmap;
542 #endif
543 
544 	KASSERT(cpu_intr_p());
545 	KASSERTMSG(ci->ci_cpl >= IPL_SCHED,
546 	    "%s: cpl (%d) < IPL_SCHED (%d)",
547 	    __func__, ci->ci_cpl, IPL_SCHED);
548 
549 	TLBINFO_LOCK(ti);
550 
551 	switch (ti->ti_tlbinvop) {
552 	case TLBINV_ONE: {
553 		/*
554 		 * We only need to invalidate one user ASID.
555 		 */
556 		struct pmap_asid_info * const pai = PMAP_PAI(ti->ti_victim, ti);
557 		KASSERT(ti->ti_victim != pmap_kernel());
558 		if (!pmap_tlb_intersecting_onproc_p(ti->ti_victim, ti)) {
559 			/*
560 			 * The victim is an active pmap so we will just
561 			 * invalidate its TLB entries.
562 			 */
563 			KASSERT(pai->pai_asid > KERNEL_PID);
564 			pmap_tlb_asid_check();
565 			tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
566 			pmap_tlb_asid_check();
567 		} else if (pai->pai_asid) {
568 			/*
569 			 * The victim is no longer an active pmap for this TLB.
570 			 * So simply clear its ASID and when pmap_activate is
571 			 * next called for this pmap, it will allocate a new
572 			 * ASID.
573 			 */
574 			KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
575 			pmap_tlb_pai_reset(ti, pai, PAI_PMAP(pai, ti));
576 		}
577 		break;
578 	}
579 	case TLBINV_ALLUSER:
580 		/*
581 		 * Flush all user TLB entries.
582 		 */
583 		pmap_tlb_asid_reinitialize(ti, TLBINV_ALLUSER);
584 		break;
585 	case TLBINV_ALLKERNEL:
586 		/*
587 		 * We need to invalidate all global TLB entries.
588 		 */
589 		pmap_tlb_asid_check();
590 		tlb_invalidate_globals();
591 		pmap_tlb_asid_check();
592 		break;
593 	case TLBINV_ALL:
594 		/*
595 		 * Flush all the TLB entries (user and kernel).
596 		 */
597 		pmap_tlb_asid_reinitialize(ti, TLBINV_ALL);
598 		break;
599 	case TLBINV_NOBODY:
600 		/*
601 		 * Might be spurious or another SMT CPU sharing this TLB
602 		 * could have already done the work.
603 		 */
604 		break;
605 	}
606 
607 	/*
608 	 * Indicate we are done with shutdown event.
609 	 */
610 	ti->ti_victim = NULL;
611 	ti->ti_tlbinvop = TLBINV_NOBODY;
612 	TLBINFO_UNLOCK(ti);
613 }
614 
615 /*
616  * This state machine could be encoded into an array of integers but since all
617  * the values fit in 3 bits, the 5 entry "table" fits in a 16 bit value which
618  * can be loaded in a single instruction.
619  */
620 #define	TLBINV_MAP(op, nobody, one, alluser, allkernel, all)	\
621 	((((   (nobody) << 3*TLBINV_NOBODY)			\
622 	 | (      (one) << 3*TLBINV_ONE)			\
623 	 | (  (alluser) << 3*TLBINV_ALLUSER)			\
624 	 | ((allkernel) << 3*TLBINV_ALLKERNEL)			\
625 	 | (      (all) << 3*TLBINV_ALL)) >> 3*(op)) & 7)
626 
627 #define	TLBINV_USER_MAP(op)	\
628 	TLBINV_MAP(op, TLBINV_ONE, TLBINV_ALLUSER, TLBINV_ALLUSER,	\
629 	    TLBINV_ALL, TLBINV_ALL)
630 
631 #define	TLBINV_KERNEL_MAP(op)	\
632 	TLBINV_MAP(op, TLBINV_ALLKERNEL, TLBINV_ALL, TLBINV_ALL,	\
633 	    TLBINV_ALLKERNEL, TLBINV_ALL)
634 
635 bool
636 pmap_tlb_shootdown_bystanders(pmap_t pm)
637 {
638 	/*
639 	 * We don't need to deal with our own TLB.
640 	 */
641 
642 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
643 
644 	kcpuset_t *pm_active;
645 	kcpuset_clone(&pm_active, pm->pm_active);
646 	kcpuset_remove(pm_active, cpu_tlb_info(curcpu())->ti_kcpuset);
647 	const bool kernel_p = (pm == pmap_kernel());
648 	bool ipi_sent = false;
649 
650 	/*
651 	 * If pm_active gets more bits set, then it's after all our changes
652 	 * have been made so they will already be cognizant of them.
653 	 */
654 
655 	for (size_t i = 0; !kcpuset_iszero(pm_active); i++) {
656 		KASSERT(i < pmap_ntlbs);
657 		struct pmap_tlb_info * const ti = pmap_tlbs[i];
658 		KASSERT(tlbinfo_index(ti) == i);
659 		/*
660 		 * Skip this TLB if there are no active mappings for it.
661 		 */
662 		if (!kcpuset_intersecting_p(pm_active, ti->ti_kcpuset))
663 			continue;
664 		struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
665 		kcpuset_remove(pm_active, ti->ti_kcpuset);
666 		TLBINFO_LOCK(ti);
667 		cpuid_t j = kcpuset_ffs_intersecting(pm->pm_onproc,
668 		    ti->ti_kcpuset);
669 		// post decrement since ffs returns bit + 1 or 0 if no bit
670 		if (j-- > 0) {
671 			if (kernel_p) {
672 				ti->ti_tlbinvop =
673 				    TLBINV_KERNEL_MAP(ti->ti_tlbinvop);
674 				ti->ti_victim = NULL;
675 			} else {
676 				KASSERT(pai->pai_asid);
677 				if (__predict_false(ti->ti_victim == pm)) {
678 					KASSERT(ti->ti_tlbinvop == TLBINV_ONE);
679 					/*
680 					 * We still need to invalidate this one
681 					 * ASID so there's nothing to change.
682 					 */
683 				} else {
684 					ti->ti_tlbinvop =
685 					    TLBINV_USER_MAP(ti->ti_tlbinvop);
686 					if (ti->ti_tlbinvop == TLBINV_ONE)
687 						ti->ti_victim = pm;
688 					else
689 						ti->ti_victim = NULL;
690 				}
691 			}
692 			TLBINFO_UNLOCK(ti);
693 			/*
694 			 * Now we can send out the shootdown IPIs to a CPU
695 			 * that shares this TLB and is currently using this
696 			 * pmap.  That CPU will process the IPI and do the
697 			 * all the work.  Any other CPUs sharing that TLB
698 			 * will take advantage of that work.  pm_onproc might
699 			 * change now that we have released the lock but we
700 			 * can tolerate spurious shootdowns.
701 			 */
702 			cpu_send_ipi(cpu_lookup(j), IPI_SHOOTDOWN);
703 			ipi_sent = true;
704 			continue;
705 		}
706 		if (!pmap_tlb_intersecting_active_p(pm, ti)) {
707 			/*
708 			 * If this pmap has an ASID assigned but it's not
709 			 * currently running, nuke its ASID.  Next time the
710 			 * pmap is activated, it will allocate a new ASID.
711 			 * And best of all, we avoid an IPI.
712 			 */
713 			KASSERT(!kernel_p);
714 			pmap_tlb_pai_reset(ti, pai, pm);
715 			//ti->ti_evcnt_lazy_shots.ev_count++;
716 		}
717 		TLBINFO_UNLOCK(ti);
718 	}
719 
720 	kcpuset_destroy(pm_active);
721 
722 	UVMHIST_LOG(maphist, " <-- done (ipi_sent=%jd)", ipi_sent, 0, 0, 0);
723 
724 	return ipi_sent;
725 }
726 #endif /* MULTIPROCESSOR && PMAP_TLB_NEED_SHOOTDOWN */
727 
728 #ifndef PMAP_HWPAGEWALKER
729 int
730 pmap_tlb_update_addr(pmap_t pm, vaddr_t va, pt_entry_t pte, u_int flags)
731 {
732 	struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
733 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
734 	int rv = -1;
735 
736 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
737 	UVMHIST_LOG(maphist,
738 	    " (pm=%#jx va=%#jx, pte=%#jx flags=%#jx)",
739 	    (uintptr_t)pm, va, pte_value(pte), flags);
740 
741 	KASSERT(kpreempt_disabled());
742 
743 	KASSERTMSG(pte_valid_p(pte), "va %#"PRIxVADDR" %#"PRIxPTE,
744 	    va, pte_value(pte));
745 
746 	TLBINFO_LOCK(ti);
747 	if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
748 		pmap_tlb_asid_check();
749 		rv = tlb_update_addr(va, pai->pai_asid, pte,
750 		    (flags & PMAP_TLB_INSERT) != 0);
751 		pmap_tlb_asid_check();
752 		UVMHIST_LOG(maphist,
753 		     "   %jd <-- tlb_update_addr(%#jx, %#jx, %#jx, ...)",
754 		     rv, va, pai->pai_asid, pte_value(pte));
755 		KASSERTMSG((flags & PMAP_TLB_INSERT) == 0 || rv == 1,
756 		    "pmap %p (asid %u) va %#"PRIxVADDR" pte %#"PRIxPTE" rv %d",
757 		    pm, pai->pai_asid, va, pte_value(pte), rv);
758 	}
759 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
760 	if (flags & PMAP_TLB_NEED_IPI)
761 		pm->pm_shootdown_pending = 1;
762 #endif
763 	TLBINFO_UNLOCK(ti);
764 
765 	UVMHIST_LOG(maphist, "   <-- done (rv=%jd)", rv, 0, 0, 0);
766 
767 	return rv;
768 }
769 #endif /* !PMAP_HWPAGEWALKER */
770 
771 void
772 pmap_tlb_invalidate_addr(pmap_t pm, vaddr_t va)
773 {
774 	struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
775 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
776 
777 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
778 	UVMHIST_LOG(maphist, " (pm=%#jx va=%#jx) ti=%#jx asid=%#jx",
779 	    (uintptr_t)pm, va, (uintptr_t)ti, pai->pai_asid);
780 
781 	KASSERT(kpreempt_disabled());
782 
783 	TLBINFO_LOCK(ti);
784 	if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
785 		pmap_tlb_asid_check();
786 		UVMHIST_LOG(maphist, " invalidating %#jx asid %#jx",
787 		    va, pai->pai_asid, 0, 0);
788 		tlb_invalidate_addr(va, pai->pai_asid);
789 		pmap_tlb_asid_check();
790 	}
791 #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
792 	pm->pm_shootdown_pending = 1;
793 #endif
794 	TLBINFO_UNLOCK(ti);
795 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
796 }
797 
798 static inline void
799 pmap_tlb_asid_alloc(struct pmap_tlb_info *ti, pmap_t pm,
800 	struct pmap_asid_info *pai)
801 {
802 	/*
803 	 * We shouldn't have an ASID assigned, and thusly must not be onproc
804 	 * nor active.
805 	 */
806 	KASSERT(pm != pmap_kernel());
807 	KASSERT(pai->pai_asid == 0);
808 	KASSERT(pai->pai_link.le_prev == NULL);
809 #if defined(MULTIPROCESSOR)
810 	KASSERT(!pmap_tlb_intersecting_onproc_p(pm, ti));
811 	KASSERT(!pmap_tlb_intersecting_active_p(pm, ti));
812 #endif
813 	KASSERT(ti->ti_asids_free > 0);
814 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
815 
816 	/*
817 	 * If the last ASID allocated was the maximum ASID, then the
818 	 * hint will be out of range.  Reset the hint to first
819 	 * available ASID.
820 	 */
821 	if (PMAP_TLB_FLUSH_ASID_ON_RESET
822 	    && ti->ti_asid_hint > ti->ti_asid_max) {
823 		ti->ti_asid_hint = KERNEL_PID + 1;
824 	}
825 	KASSERTMSG(ti->ti_asid_hint <= ti->ti_asid_max, "hint %u",
826 	    ti->ti_asid_hint);
827 
828 	/*
829 	 * Let's see if the hinted ASID is free.  If not search for
830 	 * a new one.
831 	 */
832 	if (__predict_true(TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint))) {
833 		const size_t nbpw = NBBY * sizeof(ti->ti_asid_bitmap._b[0]);
834 		size_t i;
835 		u_long bits;
836 		for (i = 0; (bits = ~ti->ti_asid_bitmap._b[i]) == 0; i++) {
837 			KASSERT(i < __arraycount(ti->ti_asid_bitmap._b) - 1);
838 		}
839 		/*
840 		 * ffs wants to find the first bit set while we want
841 		 * to find the first bit cleared.
842 		 */
843 		const u_int n = __builtin_ffsl(bits) - 1;
844 		KASSERTMSG((bits << (nbpw - (n+1))) == (1ul << (nbpw-1)),
845 		    "n %u bits %#lx", n, bits);
846 		KASSERT(n < nbpw);
847 		ti->ti_asid_hint = n + i * nbpw;
848 	}
849 
850 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
851 	KASSERT(ti->ti_asid_hint <= ti->ti_asid_max);
852 	KASSERTMSG(PMAP_TLB_FLUSH_ASID_ON_RESET
853 	    || TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint - 1),
854 	    "hint %u bitmap %p", ti->ti_asid_hint, &ti->ti_asid_bitmap);
855 	KASSERTMSG(!TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint),
856 	    "hint %u bitmap %p", ti->ti_asid_hint, &ti->ti_asid_bitmap);
857 
858 	/*
859 	 * The hint contains our next ASID so take it and advance the hint.
860 	 * Mark it as used and insert the pai into the list of active asids.
861 	 * There is also one less asid free in this TLB.
862 	 */
863 	KASSERT(ti->ti_asid_hint > KERNEL_PID);
864 	pai->pai_asid = ti->ti_asid_hint++;
865 #ifdef MULTIPROCESSOR
866 	if (PMAP_TLB_FLUSH_ASID_ON_RESET) {
867 		/*
868 		 * Clean the new ASID from the TLB.
869 		 */
870 		tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
871 	}
872 #endif
873 	TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
874 	LIST_INSERT_HEAD(&ti->ti_pais, pai, pai_link);
875 	ti->ti_asids_free--;
876 
877 #if defined(MULTIPROCESSOR)
878 	/*
879 	 * Mark that we now have an active ASID for all CPUs sharing this TLB.
880 	 * The bits in pm_active belonging to this TLB can only be changed
881 	 * while this TLBs lock is held.
882 	 */
883 #if PMAP_TLB_MAX == 1
884 	kcpuset_copy(pm->pm_active, kcpuset_running);
885 #else
886 	kcpuset_merge(pm->pm_active, ti->ti_kcpuset);
887 #endif
888 #endif
889 }
890 
891 /*
892  * Acquire a TLB address space tag (called ASID or TLBPID) and return it.
893  * ASID might have already been previously acquired.
894  */
895 void
896 pmap_tlb_asid_acquire(pmap_t pm, struct lwp *l)
897 {
898 	struct cpu_info * const ci = l->l_cpu;
899 	struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
900 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
901 
902 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
903 	UVMHIST_LOG(maphist, "(pm=%#jx, l=%#jx, ti=%#jx)", (uintptr_t)pm,
904 	    (uintptr_t)l, (uintptr_t)ti, 0);
905 
906 	KASSERT(kpreempt_disabled());
907 
908 	/*
909 	 * Kernels use a fixed ASID and thus doesn't need to acquire one.
910 	 */
911 	if (pm == pmap_kernel()) {
912 		UVMHIST_LOG(maphist, " <-- done (kernel)", 0, 0, 0, 0);
913 		return;
914 	}
915 
916 	TLBINFO_LOCK(ti);
917 	KASSERT(pai->pai_asid <= KERNEL_PID || pai->pai_link.le_prev != NULL);
918 	KASSERT(pai->pai_asid > KERNEL_PID || pai->pai_link.le_prev == NULL);
919 	pmap_tlb_pai_check(ti, true);
920 	if (__predict_false(!PMAP_PAI_ASIDVALID_P(pai, ti))) {
921 		/*
922 		 * If we've run out ASIDs, reinitialize the ASID space.
923 		 */
924 		if (__predict_false(tlbinfo_noasids_p(ti))) {
925 			KASSERT(l == curlwp);
926 			UVMHIST_LOG(maphist, " asid reinit", 0, 0, 0, 0);
927 			pmap_tlb_asid_reinitialize(ti, TLBINV_NOBODY);
928 			KASSERT(!tlbinfo_noasids_p(ti));
929 		}
930 
931 		/*
932 		 * Get an ASID.
933 		 */
934 		pmap_tlb_asid_alloc(ti, pm, pai);
935 		UVMHIST_LOG(maphist, "allocated asid %#jx", pai->pai_asid,
936 		    0, 0, 0);
937 	}
938 	pmap_tlb_pai_check(ti, true);
939 #if defined(MULTIPROCESSOR)
940 	KASSERT(kcpuset_isset(pm->pm_active, cpu_index(ci)));
941 #endif
942 
943 	if (l == curlwp) {
944 #if defined(MULTIPROCESSOR)
945 		/*
946 		 * The bits in pm_onproc belonging to this TLB can only
947 		 * be changed while this TLBs lock is held unless atomic
948 		 * operations are used.
949 		 */
950 		KASSERT(pm != pmap_kernel());
951 		kcpuset_atomic_set(pm->pm_onproc, cpu_index(ci));
952 #endif
953 		ci->ci_pmap_asid_cur = pai->pai_asid;
954 		UVMHIST_LOG(maphist, "setting asid to %#jx", pai->pai_asid,
955 		    0, 0, 0);
956 		tlb_set_asid(pai->pai_asid);
957 		pmap_tlb_asid_check();
958 	} else {
959 		printf("%s: l (%p) != curlwp %p\n", __func__, l, curlwp);
960 	}
961 	TLBINFO_UNLOCK(ti);
962 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
963 }
964 
965 void
966 pmap_tlb_asid_deactivate(pmap_t pm)
967 {
968 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
969 
970 	KASSERT(kpreempt_disabled());
971 #if defined(MULTIPROCESSOR)
972 	/*
973 	 * The kernel pmap is aways onproc and active and must never have
974 	 * those bits cleared.  If pmap_remove_all was called, it has already
975 	 * deactivated the pmap and thusly onproc will be 0 so there's nothing
976 	 * to do.
977 	 */
978 	if (pm != pmap_kernel() && !kcpuset_iszero(pm->pm_onproc)) {
979 		struct cpu_info * const ci = curcpu();
980 		KASSERT(!cpu_intr_p());
981 		KASSERTMSG(kcpuset_isset(pm->pm_onproc, cpu_index(ci)),
982 		    "%s: pmap %p onproc %p doesn't include cpu %d (%p)",
983 		    __func__, pm, pm->pm_onproc, cpu_index(ci), ci);
984 		/*
985 		 * The bits in pm_onproc that belong to this TLB can
986 		 * be changed while this TLBs lock is not held as long
987 		 * as we use atomic ops.
988 		 */
989 		kcpuset_atomic_clear(pm->pm_onproc, cpu_index(ci));
990 	}
991 #endif
992 	curcpu()->ci_pmap_asid_cur = KERNEL_PID;
993 	UVMHIST_LOG(maphist, " <-- done (pm=%#jx)", (uintptr_t)pm, 0, 0, 0);
994 	tlb_set_asid(KERNEL_PID);
995 	pmap_tlb_pai_check(cpu_tlb_info(curcpu()), false);
996 #if defined(DEBUG)
997 	pmap_tlb_asid_check();
998 #endif
999 }
1000 
1001 void
1002 pmap_tlb_asid_release_all(struct pmap *pm)
1003 {
1004 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
1005 	UVMHIST_LOG(maphist, "(pm=%#jx)", (uintptr_t)pm, 0, 0, 0);
1006 
1007 	KASSERT(pm != pmap_kernel());
1008 #if defined(MULTIPROCESSOR)
1009 	//KASSERT(!kcpuset_iszero(pm->pm_onproc)); // XXX
1010 	struct cpu_info * const ci __diagused = curcpu();
1011 	KASSERT(!kcpuset_isotherset(pm->pm_onproc, cpu_index(ci)));
1012 #if PMAP_TLB_MAX > 1
1013 	for (u_int i = 0; !kcpuset_iszero(pm->pm_active); i++) {
1014 		KASSERT(i < pmap_ntlbs);
1015 		struct pmap_tlb_info * const ti = pmap_tlbs[i];
1016 #else
1017 		struct pmap_tlb_info * const ti = &pmap_tlb0_info;
1018 #endif
1019 		struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
1020 		TLBINFO_LOCK(ti);
1021 		if (PMAP_PAI_ASIDVALID_P(pai, ti)) {
1022 			/*
1023 			 * This pmap should not be in use by any other cpu so
1024 			 * we can just reset and be happy.
1025 			 */
1026 			if (ti->ti_victim == pm)
1027 				ti->ti_victim = NULL;
1028 			pmap_tlb_pai_reset(ti, pai, pm);
1029 		}
1030 		KASSERT(pai->pai_link.le_prev == NULL);
1031 		TLBINFO_UNLOCK(ti);
1032 #if PMAP_TLB_MAX > 1
1033 	}
1034 #endif
1035 #ifdef DIAGNOSTIC
1036 	for (size_t i = 0; i < (PMAP_TLB_MAX > 1 ? pmap_ntlbs : 1); i++) {
1037 		KASSERTMSG(pm->pm_pai[i].pai_asid == 0,
1038 		    "pm %p i %zu asid %u",
1039 		    pm, i, pm->pm_pai[i].pai_asid);
1040 	}
1041 #endif
1042 #else
1043 	/*
1044 	 * Handle the case of an UP kernel which only has, at most, one TLB.
1045 	 * If the pmap has an ASID allocated, free it.
1046 	 */
1047 	struct pmap_tlb_info * const ti = &pmap_tlb0_info;
1048 	struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
1049 	TLBINFO_LOCK(ti);
1050 	if (pai->pai_asid > KERNEL_PID) {
1051 		if (curcpu()->ci_pmap_asid_cur == pai->pai_asid) {
1052 			tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
1053 		} else {
1054 			pmap_tlb_pai_reset(ti, pai, pm);
1055 		}
1056 	}
1057 	TLBINFO_UNLOCK(ti);
1058 #endif /* MULTIPROCESSOR */
1059 	UVMHIST_LOG(maphist, " <-- done", 0, 0, 0, 0);
1060 }
1061 
1062 void
1063 pmap_tlb_asid_check(void)
1064 {
1065 #ifdef DEBUG
1066 	kpreempt_disable();
1067 	const tlb_asid_t asid __debugused = tlb_get_asid();
1068 	KDASSERTMSG(asid == curcpu()->ci_pmap_asid_cur,
1069 	   "%s: asid (%#x) != current asid (%#x)",
1070 	    __func__, asid, curcpu()->ci_pmap_asid_cur);
1071 	kpreempt_enable();
1072 #endif
1073 }
1074 
1075 #ifdef DEBUG
1076 void
1077 pmap_tlb_check(pmap_t pm, bool (*func)(void *, vaddr_t, tlb_asid_t, pt_entry_t))
1078 {
1079         struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
1080         struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
1081         TLBINFO_LOCK(ti);
1082         if (pm == pmap_kernel() || pai->pai_asid > KERNEL_PID)
1083 		tlb_walk(pm, func);
1084         TLBINFO_UNLOCK(ti);
1085 }
1086 #endif /* DEBUG */
1087