pmap_tlb.c revision 1.2 1 /* $NetBSD: pmap_tlb.c,v 1.2 2013/07/02 09:35:48 matt 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.2 2013/07/02 09:35:48 matt 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_mutex __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 - (KERNEL_PID + 1)),
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_mutex,
155 .ti_pais = LIST_HEAD_INITIALIZER(pmap_tlb0_info.ti_pais),
156 #if defined(MULTIPROCESSOR)
157 .ti_cpu_mask = 1,
158 .ti_tlbinvop = TLBINV_NOBODY,
159 #endif
160 };
161
162 #undef IFCONSTANT
163
164 #if defined(MULTIPROCESSOR)
165 struct pmap_tlb_info *pmap_tlbs[MAXCPUS] = {
166 [0] = &pmap_tlb0_info,
167 };
168 u_int pmap_ntlbs = 1;
169 #endif
170
171 #define __BITMAP_SET(bm, n) \
172 ((bm)[(n) / (8*sizeof(bm[0]))] |= 1LU << ((n) % (8*sizeof(bm[0]))))
173 #define __BITMAP_CLR(bm, n) \
174 ((bm)[(n) / (8*sizeof(bm[0]))] &= ~(1LU << ((n) % (8*sizeof(bm[0])))))
175 #define __BITMAP_ISSET_P(bm, n) \
176 (((bm)[(n) / (8*sizeof(bm[0]))] & (1LU << ((n) % (8*sizeof(bm[0]))))) != 0)
177
178 #define TLBINFO_ASID_MARK_USED(ti, asid) \
179 __BITMAP_SET((ti)->ti_asid_bitmap, (asid))
180 #define TLBINFO_ASID_INUSE_P(ti, asid) \
181 __BITMAP_ISSET_P((ti)->ti_asid_bitmap, (asid))
182
183 static void
184 pmap_pai_check(struct pmap_tlb_info *ti)
185 {
186 #ifdef DIAGNOSTIC
187 struct pmap_asid_info *pai;
188 LIST_FOREACH(pai, &ti->ti_pais, pai_link) {
189 KASSERT(pai != NULL);
190 KASSERT(PAI_PMAP(pai, ti) != pmap_kernel());
191 KASSERT(pai->pai_asid > KERNEL_PID);
192 KASSERT(TLBINFO_ASID_INUSE_P(ti, pai->pai_asid));
193 }
194 #endif
195 }
196
197 static inline void
198 pmap_pai_reset(struct pmap_tlb_info *ti, struct pmap_asid_info *pai,
199 struct pmap *pm)
200 {
201 /*
202 * We must have an ASID but it must not be onproc (on a processor).
203 */
204 KASSERT(pai->pai_asid > KERNEL_PID);
205 #if defined(MULTIPROCESSOR)
206 KASSERT((pm->pm_onproc & ti->ti_cpu_mask) == 0);
207 #endif
208 LIST_REMOVE(pai, pai_link);
209 #ifdef DIAGNOSTIC
210 pai->pai_link.le_prev = NULL; /* tagged as unlinked */
211 #endif
212 /*
213 * Note that we don't mark the ASID as not in use in the TLB's ASID
214 * bitmap (thus it can't be allocated until the ASID space is exhausted
215 * and therefore reinitialized). We don't want to flush the TLB for
216 * entries belonging to this ASID so we will let natural TLB entry
217 * replacement flush them out of the TLB. Any new entries for this
218 * pmap will need a new ASID allocated.
219 */
220 pai->pai_asid = 0;
221
222 #if defined(MULTIPROCESSOR)
223 /*
224 * The bits in pm_active belonging to this TLB can only be changed
225 * while this TLB's lock is held.
226 */
227 CPUSET_DELSET(pm->pm_active, ti->ti_cpu_mask);
228 #endif /* MULTIPROCESSOR */
229 }
230
231 void
232 pmap_tlb_info_evcnt_attach(struct pmap_tlb_info *ti)
233 {
234 #if defined(MULTIPROCESSOR)
235 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_desired,
236 EVCNT_TYPE_MISC, NULL,
237 ti->ti_name, "icache syncs desired");
238 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_asts,
239 EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
240 ti->ti_name, "icache sync asts");
241 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_all,
242 EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
243 ti->ti_name, "icache full syncs");
244 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_pages,
245 EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_asts,
246 ti->ti_name, "icache pages synced");
247 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_duplicate,
248 EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
249 ti->ti_name, "icache dup pages skipped");
250 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_synci_deferred,
251 EVCNT_TYPE_MISC, &ti->ti_evcnt_synci_desired,
252 ti->ti_name, "icache pages deferred");
253 #endif /* MULTIPROCESSOR */
254 evcnt_attach_dynamic_nozero(&ti->ti_evcnt_asid_reinits,
255 EVCNT_TYPE_MISC, NULL,
256 ti->ti_name, "asid pool reinit");
257 }
258
259 void
260 pmap_tlb_info_init(struct pmap_tlb_info *ti)
261 {
262 #if defined(MULTIPROCESSOR)
263 if (ti != &pmap_tlb0_info) {
264
265 KASSERT(pmap_tlbs[pmap_ntlbs] == NULL);
266
267 ti->ti_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
268 ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
269 ti->ti_asid_hint = KERNEL_PID + 1;
270 ti->ti_asid_max = pmap_tlbs[0]->ti_asid_max;
271 ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
272 ti->ti_tlbinvop = TLBINV_NOBODY,
273 ti->ti_victim = NULL;
274 ti->ti_cpu_mask = 0;
275 ti->ti_index = pmap_ntlbs++;
276 ti->ti_wired = 0;
277 pmap_tlbs[ti->ti_index] = ti;
278 snprintf(ti->ti_name, sizeof(ti->ti_name), "tlb%u",
279 ti->ti_index);
280 pmap_tlb_info_evcnt_attach(ti);
281 return;
282 }
283 #endif /* MULTIPROCESSOR */
284 KASSERT(ti == &pmap_tlb0_info);
285 mutex_init(ti->ti_lock, MUTEX_DEFAULT, IPL_SCHED);
286 if (ti->ti_asid_max == 0) {
287 ti->ti_asid_max = pmap_md_tlb_asid_max();
288 ti->ti_asids_free = ti->ti_asid_max - (KERNEL_PID + 1);
289 }
290
291 KASSERT(ti->ti_asid_max < sizeof(ti->ti_asid_bitmap)*8);
292 }
293
294 #if defined(MULTIPROCESSOR)
295 void
296 pmap_tlb_info_attach(struct pmap_tlb_info *ti, struct cpu_info *ci)
297 {
298 KASSERT(!CPU_IS_PRIMARY(ci));
299 KASSERT(ci->ci_data.cpu_idlelwp != NULL);
300 KASSERT(cold);
301
302 TLBINFO_LOCK(ti);
303 const __cpuset_t cpu_mask = CPUSET_SINGLE(cpu_index(ci));
304 CPUSET_ADDSET(ti->ti_cpu_mask, cpu_mask);
305 cpu_set_tlb_info(ci, ti);
306
307 /*
308 * Do any MD tlb info init.
309 */
310 pmap_md_tlb_info_attach(ti, ci);
311
312 /*
313 * Mark the kernel as active and "onproc" for this cpu. We assume
314 * we are the only CPU running so atomic ops are not needed.
315 */
316 CPUSET_ADDSET(pmap_kernel()->pm_active, cpu_mask);
317 CPUSET_ADDSET(pmap_kernel()->pm_onproc, cpu_mask);
318 TLBINFO_UNLOCK(ti);
319 }
320 #endif /* MULTIPROCESSOR */
321
322 #ifdef DIAGNOSTIC
323 static size_t
324 pmap_tlb_asid_count(struct pmap_tlb_info *ti)
325 {
326 size_t count = 0;
327 for (tlb_asid_t asid = 1; asid <= ti->ti_asid_max; asid++) {
328 count += TLBINFO_ASID_INUSE_P(ti, asid);
329 }
330 return count;
331 }
332 #endif
333
334 static void
335 pmap_tlb_asid_reinitialize(struct pmap_tlb_info *ti, enum tlb_invalidate_op op)
336 {
337 const size_t asid_bitmap_words =
338 ti->ti_asid_max / (8 * sizeof(ti->ti_asid_bitmap[0]));
339
340 pmap_pai_check(ti);
341
342 /*
343 * First, clear the ASID bitmap (except for ASID 0 which belongs
344 * to the kernel).
345 */
346 ti->ti_asids_free = ti->ti_asid_max - KERNEL_PID;
347 ti->ti_asid_hint = KERNEL_PID + 1;
348 ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
349 for (size_t word = 1; word <= asid_bitmap_words; word++) {
350 ti->ti_asid_bitmap[word] = 0;
351 }
352
353 switch (op) {
354 #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
355 case TLBINV_ALL:
356 tlb_invalidate_all();
357 break;
358 case TLBINV_ALLUSER:
359 tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
360 break;
361 #endif /* MULTIPROCESSOR && PMAP_NEED_TLB_SHOOTDOWN */
362 case TLBINV_NOBODY: {
363 /*
364 * If we are just reclaiming ASIDs in the TLB, let's go find
365 * what ASIDs are in use in the TLB. Since this is a
366 * semi-expensive operation, we don't want to do it too often.
367 * So if more half of the ASIDs are in use, we don't have
368 * enough free ASIDs so invalidate the TLB entries with ASIDs
369 * and clear the ASID bitmap. That will force everyone to
370 * allocate a new ASID.
371 */
372 #if !defined(MULTIPROCESSOR) || defined(PMAP_NEED_TLB_SHOOTDOWN)
373 pmap_tlb_asid_check();
374 const u_int asids_found = tlb_record_asids(ti->ti_asid_bitmap);
375 pmap_tlb_asid_check();
376 KASSERT(asids_found == pmap_tlb_asid_count(ti));
377 if (__predict_false(asids_found >= ti->ti_asid_max / 2)) {
378 tlb_invalidate_asids(KERNEL_PID + 1, ti->ti_asid_max);
379 #else /* MULTIPROCESSOR && !PMAP_NEED_TLB_SHOOTDOWN */
380 /*
381 * For those systems (PowerPC) that don't need require
382 * cross cpu TLB shootdowns, we have to invalidate the
383 * entire TLB because we can't record the ASIDs in use
384 * on the other CPUs. This is hopefully cheaper than
385 * than trying to use an IPI to record all the ASIDs
386 * on all the CPUs (which would be a synchronization
387 * nightmare).
388 */
389 tlb_invalidate_all();
390 #endif /* MULTIPROCESSOR && !PMAP_NEED_TLB_SHOOTDOWN */
391 ti->ti_asid_bitmap[0] = (2 << KERNEL_PID) - 1;
392 for (size_t word = 1;
393 word <= asid_bitmap_words;
394 word++) {
395 ti->ti_asid_bitmap[word] = 0;
396 }
397 #if !defined(MULTIPROCESSOR) || defined(PMAP_NEED_TLB_SHOOTDOWN)
398 } else {
399 ti->ti_asids_free -= asids_found;
400 }
401 #endif /* !MULTIPROCESSOR || PMAP_NEED_TLB_SHOOTDOWN */
402 break;
403 }
404 default:
405 panic("%s: unexpected op %d", __func__, op);
406 }
407
408 /*
409 * Now go through the active ASIDs. If the ASID is on a processor or
410 * we aren't invalidating all ASIDs and the TLB has an entry owned by
411 * that ASID, mark it as in use. Otherwise release the ASID.
412 */
413 struct pmap_asid_info *pai, *next;
414 for (pai = LIST_FIRST(&ti->ti_pais); pai != NULL; pai = next) {
415 struct pmap * const pm = PAI_PMAP(pai, ti);
416 next = LIST_NEXT(pai, pai_link);
417 KASSERT(pm != pmap_kernel());
418 KASSERT(pai->pai_asid > KERNEL_PID);
419 #if defined(MULTIPROCESSOR)
420 if (!CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_onproc, ti->ti_cpu_mask))) {
421 if (!TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
422 TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
423 ti->ti_asids_free--;
424 }
425 continue;
426 }
427 #endif /* MULTIPROCESSOR */
428 if (TLBINFO_ASID_INUSE_P(ti, pai->pai_asid)) {
429 KASSERT(op == TLBINV_NOBODY);
430 } else {
431 pmap_pai_reset(ti, pai, pm);
432 }
433 }
434 #ifdef DIAGNOSTIC
435 size_t free_count = ti->ti_asid_max - pmap_tlb_asid_count(ti);
436 if (free_count != ti->ti_asids_free)
437 panic("%s: bitmap error: %zu != %u",
438 __func__, free_count, ti->ti_asids_free);
439 #endif
440 }
441
442 #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
443 void
444 pmap_tlb_shootdown_process(void)
445 {
446 struct cpu_info * const ci = curcpu();
447 struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
448 #ifdef DIAGNOSTIC
449 struct pmap * const pm = curlwp->l_proc->p_vmspace->vm_map.pmap;
450 #endif
451
452 KASSERT(cpu_intr_p());
453 KASSERTMSG(ci->ci_cpl >= IPL_SCHED,
454 "%s: cpl (%d) < IPL_SCHED (%d)",
455 __func__, ci->ci_cpl, IPL_SCHED);
456
457 TLBINFO_LOCK(ti);
458
459 switch (ti->ti_tlbinvop) {
460 case TLBINV_ONE: {
461 /*
462 * We only need to invalidate one user ASID.
463 */
464 struct pmap_asid_info * const pai = PMAP_PAI(ti->ti_victim, ti);
465 KASSERT(ti->ti_victim != pmap_kernel());
466 if (!CPUSET_EMPTY_P(CPUSET_SUBSET(ti->ti_victim->pm_onproc, ti->ti_cpu_mask))) {
467 /*
468 * The victim is an active pmap so we will just
469 * invalidate its TLB entries.
470 */
471 KASSERT(pai->pai_asid > KERNEL_PID);
472 pmap_tlb_asid_check();
473 tlb_invalidate_asids(pai->pai_asid, pai->pai_asid);
474 pmap_tlb_asid_check();
475 } else if (pai->pai_asid) {
476 /*
477 * The victim is no longer an active pmap for this TLB.
478 * So simply clear its ASID and when pmap_activate is
479 * next called for this pmap, it will allocate a new
480 * ASID.
481 */
482 KASSERT(!CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_onproc, ti->ti_cpu_mask)));
483 pmap_pai_reset(ti, pai, PAI_PMAP(pai, ti));
484 }
485 break;
486 }
487 case TLBINV_ALLUSER:
488 /*
489 * Flush all user TLB entries.
490 */
491 pmap_tlb_asid_reinitialize(ti, TLBINV_ALLUSER);
492 break;
493 case TLBINV_ALLKERNEL:
494 /*
495 * We need to invalidate all global TLB entries.
496 */
497 pmap_tlb_asid_check();
498 tlb_invalidate_globals();
499 pmap_tlb_asid_check();
500 break;
501 case TLBINV_ALL:
502 /*
503 * Flush all the TLB entries (user and kernel).
504 */
505 pmap_tlb_asid_reinitialize(ti, TLBINV_ALL);
506 break;
507 case TLBINV_NOBODY:
508 /*
509 * Might be spurious or another SMT CPU sharing this TLB
510 * could have already done the work.
511 */
512 break;
513 }
514
515 /*
516 * Indicate we are done with shutdown event.
517 */
518 ti->ti_victim = NULL;
519 ti->ti_tlbinvop = TLBINV_NOBODY;
520 TLBINFO_UNLOCK(ti);
521 }
522
523 /*
524 * This state machine could be encoded into an array of integers but since all
525 * the values fit in 3 bits, the 5 entry "table" fits in a 16 bit value which
526 * can be loaded in a single instruction.
527 */
528 #define TLBINV_MAP(op, nobody, one, alluser, allkernel, all) \
529 (((( (nobody) << 3*TLBINV_NOBODY) \
530 | ( (one) << 3*TLBINV_ONE) \
531 | ( (alluser) << 3*TLBINV_ALLUSER) \
532 | ((allkernel) << 3*TLBINV_ALLKERNEL) \
533 | ( (all) << 3*TLBINV_ALL)) >> 3*(op)) & 7)
534
535 #define TLBINV_USER_MAP(op) \
536 TLBINV_MAP(op, TLBINV_ONE, TLBINV_ALLUSER, TLBINV_ALLUSER, \
537 TLBINV_ALL, TLBINV_ALL)
538
539 #define TLBINV_KERNEL_MAP(op) \
540 TLBINV_MAP(op, TLBINV_ALLKERNEL, TLBINV_ALL, TLBINV_ALL, \
541 TLBINV_ALLKERNEL, TLBINV_ALL)
542
543 bool
544 pmap_tlb_shootdown_bystanders(pmap_t pm)
545 {
546 /*
547 * We don't need to deal our own TLB.
548 */
549 __cpuset_t pm_active =
550 CPUSET_EXCLUDE(pm->pm_active, cpu_tlb_info(curcpu())->ti_cpu_mask);
551 const bool kernel_p = (pm == pmap_kernel());
552 bool ipi_sent = false;
553
554 /*
555 * If pm_active gets more bits set, then it's after all our changes
556 * have been made so they will already be cognizant of them.
557 */
558
559 for (size_t i = 0; !CPUSET_EMPTY_P(pm_active); i++) {
560 KASSERT(i < pmap_ntlbs);
561 struct pmap_tlb_info * const ti = pmap_tlbs[i];
562 KASSERT(tlbinfo_index(ti) == i);
563 /*
564 * Skip this TLB if there are no active mappings for it.
565 */
566 if (CPUSET_EMPTY_P(CPUSET_SUBSET(pm_active, ti->ti_cpu_mask)))
567 continue;
568 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
569 CPUSET_DELSET(pm_active, ti->ti_cpu_mask);
570 TLBINFO_LOCK(ti);
571 const __cpuset onproc = CPUSET_SUBSET(pm->pm_onproc,
572 ti->ti_cpu_mask);
573 if (onproc != 0) {
574 if (kernel_p) {
575 ti->ti_tlbinvop =
576 TLBINV_KERNEL_MAP(ti->ti_tlbinvop);
577 ti->ti_victim = NULL;
578 } else {
579 KASSERT(pai->pai_asid);
580 if (__predict_false(ti->ti_victim == pm)) {
581 KASSERT(ti->ti_tlbinvop == TLBINV_ONE);
582 /*
583 * We still need to invalidate this one
584 * ASID so there's nothing to change.
585 */
586 } else {
587 ti->ti_tlbinvop =
588 TLBINV_USER_MAP(ti->ti_tlbinvop);
589 if (ti->ti_tlbinvop == TLBINV_ONE)
590 ti->ti_victim = pm;
591 else
592 ti->ti_victim = NULL;
593 }
594 }
595 TLBINFO_UNLOCK(ti);
596 /*
597 * Now we can send out the shootdown IPIs to a CPU
598 * that shares this TLB and is currently using this
599 * pmap. That CPU will process the IPI and do the
600 * all the work. Any other CPUs sharing that TLB
601 * will take advantage of that work. pm_onproc might
602 * change now that we have released the lock but we
603 * can tolerate spurious shootdowns.
604 */
605 KASSERT(!CPUSET_EMPTY_P(onproc));
606 u_int j = CPUSET_NEXT(onproc);
607 cpu_send_ipi(cpu_lookup(j), IPI_SHOOTDOWN);
608 ipi_sent = true;
609 continue;
610 }
611 if (!CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_active, ti->ti_cpu_mask) {
612 /*
613 * If this pmap has an ASID assigned but it's not
614 * currently running, nuke its ASID. Next time the
615 * pmap is activated, it will allocate a new ASID.
616 * And best of all, we avoid an IPI.
617 */
618 KASSERT(!kernel_p);
619 pmap_pai_reset(ti, pai, pm);
620 //ti->ti_evcnt_lazy_shots.ev_count++;
621 }
622 TLBINFO_UNLOCK(ti);
623 }
624
625 return ipi_sent;
626 }
627 #endif /* MULTIPROCESSOR && PMAP_NEED_TLB_SHOOTDOWN */
628
629 #ifndef PMAP_TLB_HWPAGEWALKER
630 int
631 pmap_tlb_update_addr(pmap_t pm, vaddr_t va, pt_entry_t pt_entry, u_int flags)
632 {
633 struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
634 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
635 int rv = -1;
636
637 KASSERT(kpreempt_disabled());
638
639 TLBINFO_LOCK(ti);
640 if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
641 pmap_tlb_asid_check();
642 rv = tlb_update_addr(va, pai->pai_asid, pt_entry,
643 (flags & PMAP_TLB_INSERT) != 0);
644 pmap_tlb_asid_check();
645 }
646 #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
647 pm->pm_shootdown_pending = (flags & PMAP_TLB_NEED_IPI) != 0;
648 #endif
649 TLBINFO_UNLOCK(ti);
650
651 return rv;
652 }
653 #endif /* !PMAP_TLB_HWPAGEWALKER */
654
655 void
656 pmap_tlb_invalidate_addr(pmap_t pm, vaddr_t va)
657 {
658 struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
659 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
660
661 KASSERT(kpreempt_disabled());
662
663 TLBINFO_LOCK(ti);
664 if (pm == pmap_kernel() || PMAP_PAI_ASIDVALID_P(pai, ti)) {
665 pmap_tlb_asid_check();
666 tlb_invalidate_addr(va, pai->pai_asid);
667 pmap_tlb_asid_check();
668 }
669 #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
670 pm->pm_shootdown_pending = 1;
671 #endif
672 TLBINFO_UNLOCK(ti);
673 }
674
675 static inline void
676 pmap_tlb_asid_alloc(struct pmap_tlb_info *ti, pmap_t pm,
677 struct pmap_asid_info *pai)
678 {
679 /*
680 * We shouldn't have an ASID assigned, and thusly must not be onproc
681 * nor active.
682 */
683 KASSERT(pm != pmap_kernel());
684 KASSERT(pai->pai_asid == 0);
685 KASSERT(pai->pai_link.le_prev == NULL);
686 #if defined(MULTIPROCESSOR)
687 KASSERT(CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_onproc, ti->ti_cpu_mask)));
688 KASSERT(CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_active, ti->ti_cpu_mask)));
689 #endif
690 KASSERT(ti->ti_asids_free > 0);
691 KASSERT(ti->ti_asid_hint <= ti->ti_asid_max);
692
693 /*
694 * Let's see if the hinted ASID is free. If not search for
695 * a new one.
696 */
697 if (__predict_false(TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint))) {
698 #ifdef DIAGNOSTIC
699 const size_t words = __arraycount(ti->ti_asid_bitmap);
700 #endif
701 const size_t nbpw = 8 * sizeof(ti->ti_asid_bitmap[0]);
702 for (size_t i = 0; i < ti->ti_asid_hint / nbpw; i++) {
703 KASSERT(~ti->ti_asid_bitmap[i] == 0);
704 }
705 for (size_t i = ti->ti_asid_hint / nbpw;; i++) {
706 KASSERT(i < words);
707 /*
708 * ffs wants to find the first bit set while we want
709 * to find the first bit cleared.
710 */
711 u_long bits = ~ti->ti_asid_bitmap[i];
712 if (__predict_true(bits)) {
713 u_int n = 0;
714 if ((bits & 0xffffffff) == 0) {
715 bits = (bits >> 31) >> 1;
716 KASSERT(bits);
717 n += 32;
718 }
719 n += ffs(bits) - 1;
720 KASSERT(n < nbpw);
721 ti->ti_asid_hint = n + i * nbpw;
722 break;
723 }
724 }
725 KASSERT(ti->ti_asid_hint > KERNEL_PID);
726 KASSERT(TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint-1));
727 KASSERT(!TLBINFO_ASID_INUSE_P(ti, ti->ti_asid_hint));
728 }
729
730 /*
731 * The hint contains our next ASID so take it and advance the hint.
732 * Mark it as used and insert the pai into the list of active asids.
733 * There is also one less asid free in this TLB.
734 */
735 pai->pai_asid = ti->ti_asid_hint++;
736 TLBINFO_ASID_MARK_USED(ti, pai->pai_asid);
737 LIST_INSERT_HEAD(&ti->ti_pais, pai, pai_link);
738 ti->ti_asids_free--;
739
740 #if defined(MULTIPROCESSOR)
741 /*
742 * Mark that we now have an active ASID for all CPUs sharing this TLB.
743 * The bits in pm_active belonging to this TLB can only be changed
744 * while this TLBs lock is held.
745 */
746 atomic_or_32(&pm->pm_active, ti->ti_cpu_mask);
747 #endif
748 }
749
750 /*
751 * Acquire a TLB address space tag (called ASID or TLBPID) and return it.
752 * ASID might have already been previously acquired.
753 */
754 void
755 pmap_tlb_asid_acquire(pmap_t pm, struct lwp *l)
756 {
757 struct cpu_info * const ci = l->l_cpu;
758 struct pmap_tlb_info * const ti = cpu_tlb_info(ci);
759 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
760
761 KASSERT(kpreempt_disabled());
762
763 /*
764 * Kernels use a fixed ASID and thus doesn't need to acquire one.
765 */
766 if (pm == pmap_kernel())
767 return;
768
769 TLBINFO_LOCK(ti);
770 KASSERT(pai->pai_asid <= KERNEL_PID || pai->pai_link.le_prev != NULL);
771 KASSERT(pai->pai_asid > KERNEL_PID || pai->pai_link.le_prev == NULL);
772 pmap_pai_check(ti);
773 if (__predict_false(!PMAP_PAI_ASIDVALID_P(pai, ti))) {
774 /*
775 * If we've run out ASIDs, reinitialize the ASID space.
776 */
777 if (__predict_false(tlbinfo_noasids_p(ti))) {
778 KASSERT(l == curlwp);
779 pmap_tlb_asid_reinitialize(ti, TLBINV_NOBODY);
780 }
781
782 /*
783 * Get an ASID.
784 */
785 pmap_tlb_asid_alloc(ti, pm, pai);
786 }
787
788 if (l == curlwp) {
789 #if defined(MULTIPROCESSOR)
790 /*
791 * The bits in pm_onproc belonging to this TLB can only
792 * be changed while this TLBs lock is held unless atomic
793 * operations are used.
794 */
795 CPUSET_ADD(pm->pm_onproc, cpu_index(ci));
796 #endif
797 ci->ci_pmap_asid_cur = pai->pai_asid;
798 tlb_set_asid(pai->pai_asid);
799 pmap_tlb_asid_check();
800 } else {
801 printf("%s: l (%p) != curlwp %p\n", __func__, l, curlwp);
802 }
803 TLBINFO_UNLOCK(ti);
804 }
805
806 void
807 pmap_tlb_asid_deactivate(pmap_t pm)
808 {
809 KASSERT(kpreempt_disabled());
810 #if defined(MULTIPROCESSOR)
811 /*
812 * The kernel pmap is aways onproc and active and must never have
813 * those bits cleared. If pmap_remove_all was called, it has already
814 * deactivated the pmap and thusly onproc will be 0 so there's nothing
815 * to do.
816 */
817 if (pm != pmap_kernel() && pm->pm_onproc != 0) {
818 struct cpu_info * const ci = curcpu();
819 KASSERT(!cpu_intr_p());
820 KASSERTMSG(pm->pm_onproc & CPUSET_SINGLE(cpu_index(ci)),
821 "%s: pmap %p onproc %#x doesn't include cpu %d (%p)",
822 __func__, pm, pm->pm_onproc, cpu_index(ci), ci);
823 /*
824 * The bits in pm_onproc that belong to this TLB can
825 * be changed while this TLBs lock is not held as long
826 * as we use atomic ops.
827 */
828 CPUSET_DEL(pm->pm_onproc, cpu_index(ci));
829 }
830 #elif defined(DEBUG)
831 curcpu()->ci_pmap_asid_cur = 0;
832 tlb_set_asid(0);
833 pmap_tlb_asid_check();
834 #endif
835 }
836
837 void
838 pmap_tlb_asid_release_all(struct pmap *pm)
839 {
840 KASSERT(pm != pmap_kernel());
841 #if defined(MULTIPROCESSOR)
842 KASSERT(CPUSET_EMPTY_P(pm->pm_onproc));
843 for (u_int i = 0; !CPUSET_EMPTY_P(pm->pm_active); i++) {
844 KASSERT(i < pmap_ntlbs);
845 struct pmap_tlb_info * const ti = pmap_tlbs[i];
846 if (!CPUSET_EMPTY_P(CPUSET_SUBSET(pm->pm_active, ti->ti_cpu_mask))) {
847 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
848 TLBINFO_LOCK(ti);
849 KASSERT(ti->ti_victim != pm);
850 pmap_pai_reset(ti, pai, pm);
851 TLBINFO_UNLOCK(ti);
852 }
853 }
854 #else
855 /*
856 * Handle the case of an UP kernel which only has, at most, one ASID.
857 * If the pmap has an ASID allocated, free it.
858 */
859 struct pmap_tlb_info * const ti = &pmap_tlb0_info;
860 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
861 TLBINFO_LOCK(ti);
862 if (pai->pai_asid > KERNEL_PID) {
863 pmap_pai_reset(ti, pai, pm);
864 }
865 TLBINFO_UNLOCK(ti);
866 #endif /* MULTIPROCESSOR */
867 }
868
869 void
870 pmap_tlb_asid_check(void)
871 {
872 #ifdef DEBUG
873 kpreempt_disable();
874 const tlb_asid_t asid = tlb_get_asid();
875 KDASSERTMSG(asid == curcpu()->ci_pmap_asid_cur,
876 "%s: asid (%#x) != current asid (%#x)",
877 __func__, asid, curcpu()->ci_pmap_asid_cur);
878 kpreempt_enable();
879 #endif
880 }
881
882 #ifdef DEBUG
883 void
884 pmap_tlb_check(pmap_t pm, bool (*func)(void *, vaddr_t, tlb_asid_t, pt_entry_t))
885 {
886 struct pmap_tlb_info * const ti = cpu_tlb_info(curcpu());
887 struct pmap_asid_info * const pai = PMAP_PAI(pm, ti);
888 TLBINFO_LOCK(ti);
889 if (pm == pmap_kernel() || pai->pai_asid > KERNEL_PID)
890 tlb_walk(pm, func);
891 TLBINFO_UNLOCK(ti);
892 }
893 #endif /* DEBUG */
894