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