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