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