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