Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.34.18.2
      1  1.34.18.2    martin /*	$NetBSD: subr_xcall.c,v 1.34.18.2 2024/09/21 12:32:39 martin Exp $	*/
      2        1.2        ad 
      3        1.2        ad /*-
      4       1.29        ad  * Copyright (c) 2007-2010, 2019 The NetBSD Foundation, Inc.
      5        1.2        ad  * All rights reserved.
      6        1.2        ad  *
      7        1.2        ad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.12     rmind  * by Andrew Doran and Mindaugas Rasiukevicius.
      9        1.2        ad  *
     10        1.2        ad  * Redistribution and use in source and binary forms, with or without
     11        1.2        ad  * modification, are permitted provided that the following conditions
     12        1.2        ad  * are met:
     13        1.2        ad  * 1. Redistributions of source code must retain the above copyright
     14        1.2        ad  *    notice, this list of conditions and the following disclaimer.
     15        1.2        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.2        ad  *    notice, this list of conditions and the following disclaimer in the
     17        1.2        ad  *    documentation and/or other materials provided with the distribution.
     18        1.2        ad  *
     19        1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.2        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.2        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.2        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.2        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.2        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.2        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.2        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.2        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.2        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.2        ad  * POSSIBILITY OF SUCH DAMAGE.
     30        1.2        ad  */
     31        1.2        ad 
     32        1.2        ad /*
     33        1.2        ad  * Cross call support
     34        1.2        ad  *
     35        1.2        ad  * Background
     36        1.2        ad  *
     37        1.2        ad  *	Sometimes it is necessary to modify hardware state that is tied
     38        1.2        ad  *	directly to individual CPUs (such as a CPU's local timer), and
     39        1.2        ad  *	these updates can not be done remotely by another CPU.  The LWP
     40        1.2        ad  *	requesting the update may be unable to guarantee that it will be
     41        1.2        ad  *	running on the CPU where the update must occur, when the update
     42        1.2        ad  *	occurs.
     43        1.2        ad  *
     44        1.2        ad  *	Additionally, it's sometimes necessary to modify per-CPU software
     45        1.2        ad  *	state from a remote CPU.  Where these update operations are so
     46        1.2        ad  *	rare or the access to the per-CPU data so frequent that the cost
     47        1.2        ad  *	of using locking or atomic operations to provide coherency is
     48        1.4        ad  *	prohibitive, another way must be found.
     49        1.2        ad  *
     50        1.2        ad  *	Cross calls help to solve these types of problem by allowing
     51       1.34        ad  *	any LWP in the system to request that an arbitrary function be
     52       1.34        ad  *	executed on a specific CPU.
     53        1.2        ad  *
     54        1.2        ad  * Implementation
     55        1.2        ad  *
     56       1.34        ad  *	A slow mechanism for making low priority cross calls is
     57        1.2        ad  *	provided.  The function to be executed runs on the remote CPU
     58        1.2        ad  *	within a bound kthread.  No queueing is provided, and the
     59        1.2        ad  *	implementation uses global state.  The function being called may
     60        1.2        ad  *	block briefly on locks, but in doing so must be careful to not
     61        1.2        ad  *	interfere with other cross calls in the system.  The function is
     62        1.2        ad  *	called with thread context and not from a soft interrupt, so it
     63        1.2        ad  *	can ensure that it is not interrupting other code running on the
     64        1.2        ad  *	CPU, and so has exclusive access to the CPU.  Since this facility
     65        1.2        ad  *	is heavyweight, it's expected that it will not be used often.
     66        1.2        ad  *
     67       1.34        ad  *	Cross calls must not allocate memory, as the pagedaemon uses cross
     68       1.34        ad  *	calls (and memory allocation may need to wait on the pagedaemon).
     69        1.4        ad  *
     70       1.12     rmind  *	A low-overhead mechanism for high priority calls (XC_HIGHPRI) is
     71       1.34        ad  *	also provided.  The function to be executed runs in software
     72       1.34        ad  *	interrupt context at IPL_SOFTSERIAL level, and is expected to
     73       1.17     rmind  *	be very lightweight, e.g. avoid blocking.
     74        1.2        ad  */
     75       1.17     rmind 
     76        1.2        ad #include <sys/cdefs.h>
     77  1.34.18.2    martin __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.34.18.2 2024/09/21 12:32:39 martin Exp $");
     78        1.2        ad 
     79        1.2        ad #include <sys/types.h>
     80        1.2        ad #include <sys/param.h>
     81        1.2        ad #include <sys/xcall.h>
     82        1.2        ad #include <sys/mutex.h>
     83        1.2        ad #include <sys/condvar.h>
     84        1.2        ad #include <sys/evcnt.h>
     85        1.2        ad #include <sys/kthread.h>
     86        1.3        ad #include <sys/cpu.h>
     87       1.29        ad #include <sys/atomic.h>
     88        1.2        ad 
     89       1.14    martin #ifdef _RUMPKERNEL
     90       1.14    martin #include "rump_private.h"
     91       1.14    martin #endif
     92       1.14    martin 
     93       1.12     rmind /* Cross-call state box. */
     94       1.12     rmind typedef struct {
     95       1.12     rmind 	kmutex_t	xc_lock;
     96       1.12     rmind 	kcondvar_t	xc_busy;
     97       1.12     rmind 	xcfunc_t	xc_func;
     98       1.12     rmind 	void *		xc_arg1;
     99       1.12     rmind 	void *		xc_arg2;
    100       1.12     rmind 	uint64_t	xc_headp;
    101       1.12     rmind 	uint64_t	xc_donep;
    102       1.21     ozaki 	unsigned int	xc_ipl;
    103       1.12     rmind } xc_state_t;
    104       1.12     rmind 
    105       1.12     rmind /* Bit indicating high (1) or low (0) priority. */
    106       1.12     rmind #define	XC_PRI_BIT	(1ULL << 63)
    107       1.12     rmind 
    108       1.12     rmind /* Low priority xcall structures. */
    109       1.13     rmind static xc_state_t	xc_low_pri	__cacheline_aligned;
    110       1.12     rmind 
    111       1.12     rmind /* High priority xcall structures. */
    112       1.13     rmind static xc_state_t	xc_high_pri	__cacheline_aligned;
    113       1.21     ozaki static void *		xc_sihs[4]	__cacheline_aligned;
    114        1.2        ad 
    115       1.12     rmind /* Event counters. */
    116       1.13     rmind static struct evcnt	xc_unicast_ev	__cacheline_aligned;
    117       1.13     rmind static struct evcnt	xc_broadcast_ev	__cacheline_aligned;
    118       1.12     rmind 
    119       1.12     rmind static void		xc_init(void);
    120       1.12     rmind static void		xc_thread(void *);
    121       1.12     rmind 
    122       1.21     ozaki static inline uint64_t	xc_highpri(xcfunc_t, void *, void *, struct cpu_info *,
    123       1.21     ozaki 			    unsigned int);
    124       1.12     rmind static inline uint64_t	xc_lowpri(xcfunc_t, void *, void *, struct cpu_info *);
    125       1.12     rmind 
    126       1.21     ozaki /* The internal form of IPL */
    127       1.21     ozaki #define XC_IPL_MASK		0xff00
    128       1.21     ozaki /*
    129       1.21     ozaki  * Assign 0 to XC_IPL_SOFTSERIAL to treat IPL_SOFTSERIAL as the default value
    130       1.21     ozaki  * (just XC_HIGHPRI).
    131       1.21     ozaki  */
    132       1.21     ozaki #define XC_IPL_SOFTSERIAL	0
    133       1.25     ozaki #define XC_IPL_SOFTNET		1
    134       1.25     ozaki #define XC_IPL_SOFTBIO		2
    135       1.25     ozaki #define XC_IPL_SOFTCLOCK	3
    136       1.25     ozaki #define XC_IPL_MAX		XC_IPL_SOFTCLOCK
    137       1.21     ozaki 
    138       1.21     ozaki CTASSERT(XC_IPL_MAX <= __arraycount(xc_sihs));
    139       1.21     ozaki 
    140       1.12     rmind /*
    141       1.12     rmind  * xc_init:
    142       1.12     rmind  *
    143       1.12     rmind  *	Initialize low and high priority cross-call structures.
    144       1.12     rmind  */
    145       1.12     rmind static void
    146       1.12     rmind xc_init(void)
    147       1.12     rmind {
    148       1.12     rmind 	xc_state_t *xclo = &xc_low_pri, *xchi = &xc_high_pri;
    149       1.12     rmind 
    150       1.12     rmind 	memset(xclo, 0, sizeof(xc_state_t));
    151       1.12     rmind 	mutex_init(&xclo->xc_lock, MUTEX_DEFAULT, IPL_NONE);
    152       1.12     rmind 	cv_init(&xclo->xc_busy, "xclocv");
    153       1.12     rmind 
    154       1.12     rmind 	memset(xchi, 0, sizeof(xc_state_t));
    155       1.17     rmind 	mutex_init(&xchi->xc_lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
    156       1.12     rmind 	cv_init(&xchi->xc_busy, "xchicv");
    157       1.21     ozaki 
    158       1.24     ozaki 	/* Set up a softint for each IPL_SOFT*. */
    159       1.21     ozaki #define SETUP_SOFTINT(xipl, sipl) do {					\
    160       1.21     ozaki 		xc_sihs[(xipl)] = softint_establish( (sipl) | SOFTINT_MPSAFE,\
    161       1.21     ozaki 		    xc__highpri_intr, NULL);				\
    162       1.21     ozaki 		KASSERT(xc_sihs[(xipl)] != NULL);			\
    163       1.21     ozaki 	} while (0)
    164       1.21     ozaki 
    165       1.21     ozaki 	SETUP_SOFTINT(XC_IPL_SOFTSERIAL, SOFTINT_SERIAL);
    166       1.24     ozaki 	/*
    167       1.24     ozaki 	 * If a IPL_SOFTXXX have the same value of the previous, we don't use
    168       1.24     ozaki 	 * the IPL (see xc_encode_ipl).  So we don't need to allocate a softint
    169       1.24     ozaki 	 * for it.
    170       1.24     ozaki 	 */
    171       1.24     ozaki #if IPL_SOFTNET != IPL_SOFTSERIAL
    172       1.24     ozaki 	SETUP_SOFTINT(XC_IPL_SOFTNET, SOFTINT_NET);
    173       1.24     ozaki #endif
    174       1.24     ozaki #if IPL_SOFTBIO != IPL_SOFTNET
    175       1.21     ozaki 	SETUP_SOFTINT(XC_IPL_SOFTBIO, SOFTINT_BIO);
    176       1.24     ozaki #endif
    177       1.24     ozaki #if IPL_SOFTCLOCK != IPL_SOFTBIO
    178       1.21     ozaki 	SETUP_SOFTINT(XC_IPL_SOFTCLOCK, SOFTINT_CLOCK);
    179       1.24     ozaki #endif
    180       1.21     ozaki 
    181       1.21     ozaki #undef SETUP_SOFTINT
    182       1.12     rmind 
    183       1.12     rmind 	evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    184       1.12     rmind 	   "crosscall", "unicast");
    185       1.12     rmind 	evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    186       1.12     rmind 	   "crosscall", "broadcast");
    187       1.12     rmind }
    188        1.2        ad 
    189        1.2        ad /*
    190       1.21     ozaki  * Encode an IPL to a form that can be embedded into flags of xc_broadcast
    191       1.21     ozaki  * or xc_unicast.
    192       1.21     ozaki  */
    193       1.21     ozaki unsigned int
    194       1.21     ozaki xc_encode_ipl(int ipl)
    195       1.21     ozaki {
    196       1.21     ozaki 
    197       1.21     ozaki 	switch (ipl) {
    198       1.21     ozaki 	case IPL_SOFTSERIAL:
    199       1.21     ozaki 		return __SHIFTIN(XC_IPL_SOFTSERIAL, XC_IPL_MASK);
    200       1.23     ozaki 	/* IPL_SOFT* can be the same value (e.g., on sparc or mips). */
    201       1.23     ozaki #if IPL_SOFTNET != IPL_SOFTSERIAL
    202       1.23     ozaki 	case IPL_SOFTNET:
    203       1.23     ozaki 		return __SHIFTIN(XC_IPL_SOFTNET, XC_IPL_MASK);
    204       1.23     ozaki #endif
    205       1.23     ozaki #if IPL_SOFTBIO != IPL_SOFTNET
    206       1.21     ozaki 	case IPL_SOFTBIO:
    207       1.21     ozaki 		return __SHIFTIN(XC_IPL_SOFTBIO, XC_IPL_MASK);
    208       1.23     ozaki #endif
    209       1.22    martin #if IPL_SOFTCLOCK != IPL_SOFTBIO
    210       1.21     ozaki 	case IPL_SOFTCLOCK:
    211       1.21     ozaki 		return __SHIFTIN(XC_IPL_SOFTCLOCK, XC_IPL_MASK);
    212       1.22    martin #endif
    213       1.21     ozaki 	}
    214       1.21     ozaki 
    215       1.21     ozaki 	panic("Invalid IPL: %d", ipl);
    216       1.21     ozaki }
    217       1.21     ozaki 
    218       1.21     ozaki /*
    219       1.21     ozaki  * Extract an XC_IPL from flags of xc_broadcast or xc_unicast.
    220       1.21     ozaki  */
    221       1.21     ozaki static inline unsigned int
    222       1.21     ozaki xc_extract_ipl(unsigned int flags)
    223       1.21     ozaki {
    224       1.21     ozaki 
    225       1.21     ozaki 	return __SHIFTOUT(flags, XC_IPL_MASK);
    226       1.21     ozaki }
    227       1.21     ozaki 
    228       1.21     ozaki /*
    229        1.2        ad  * xc_init_cpu:
    230        1.2        ad  *
    231        1.2        ad  *	Initialize the cross-call subsystem.  Called once for each CPU
    232        1.2        ad  *	in the system as they are attached.
    233        1.2        ad  */
    234        1.2        ad void
    235        1.2        ad xc_init_cpu(struct cpu_info *ci)
    236        1.2        ad {
    237       1.11     pooka 	static bool again = false;
    238       1.16    martin 	int error __diagused;
    239        1.2        ad 
    240        1.2        ad 	if (!again) {
    241        1.2        ad 		/* Autoconfiguration will prevent re-entry. */
    242       1.12     rmind 		xc_init();
    243        1.2        ad 		again = true;
    244        1.2        ad 	}
    245        1.2        ad 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    246        1.2        ad 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    247        1.6    martin 	    NULL, NULL, "xcall/%u", ci->ci_index);
    248       1.12     rmind 	KASSERT(error == 0);
    249        1.2        ad }
    250        1.2        ad 
    251        1.2        ad /*
    252        1.7        ad  * xc_broadcast:
    253        1.2        ad  *
    254        1.2        ad  *	Trigger a call on all CPUs in the system.
    255        1.2        ad  */
    256        1.2        ad uint64_t
    257       1.21     ozaki xc_broadcast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2)
    258        1.2        ad {
    259        1.2        ad 
    260       1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    261       1.26     ozaki 	ASSERT_SLEEPABLE();
    262       1.12     rmind 
    263       1.29        ad 	if (__predict_false(!mp_online)) {
    264  1.34.18.2    martin 		int s, bound;
    265  1.34.18.2    martin 
    266  1.34.18.2    martin 		if (flags & XC_HIGHPRI)
    267  1.34.18.2    martin 			s = splsoftserial();
    268  1.34.18.2    martin 		else
    269  1.34.18.2    martin 			bound = curlwp_bind();
    270       1.29        ad 		(*func)(arg1, arg2);
    271  1.34.18.2    martin 		if (flags & XC_HIGHPRI)
    272  1.34.18.2    martin 			splx(s);
    273  1.34.18.2    martin 		else
    274  1.34.18.2    martin 			curlwp_bindx(bound);
    275       1.29        ad 		return 0;
    276       1.29        ad 	}
    277       1.29        ad 
    278        1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    279       1.21     ozaki 		int ipl = xc_extract_ipl(flags);
    280       1.21     ozaki 		return xc_highpri(func, arg1, arg2, NULL, ipl);
    281        1.2        ad 	} else {
    282       1.12     rmind 		return xc_lowpri(func, arg1, arg2, NULL);
    283        1.2        ad 	}
    284        1.2        ad }
    285        1.2        ad 
    286       1.27       uwe static void
    287       1.27       uwe xc_nop(void *arg1, void *arg2)
    288       1.27       uwe {
    289       1.27       uwe 
    290       1.33   thorpej 	return;
    291       1.27       uwe }
    292       1.27       uwe 
    293       1.27       uwe /*
    294       1.27       uwe  * xc_barrier:
    295       1.27       uwe  *
    296       1.27       uwe  *	Broadcast a nop to all CPUs in the system.
    297       1.27       uwe  */
    298       1.27       uwe void
    299       1.27       uwe xc_barrier(unsigned int flags)
    300       1.27       uwe {
    301       1.27       uwe 	uint64_t where;
    302       1.27       uwe 
    303       1.27       uwe 	where = xc_broadcast(flags, xc_nop, NULL, NULL);
    304       1.27       uwe 	xc_wait(where);
    305       1.27       uwe }
    306       1.27       uwe 
    307        1.2        ad /*
    308        1.2        ad  * xc_unicast:
    309        1.2        ad  *
    310        1.2        ad  *	Trigger a call on one CPU.
    311        1.2        ad  */
    312        1.2        ad uint64_t
    313       1.21     ozaki xc_unicast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2,
    314  1.34.18.2    martin     struct cpu_info *ci)
    315        1.2        ad {
    316        1.2        ad 
    317       1.12     rmind 	KASSERT(ci != NULL);
    318       1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    319       1.26     ozaki 	ASSERT_SLEEPABLE();
    320       1.12     rmind 
    321       1.29        ad 	if (__predict_false(!mp_online)) {
    322  1.34.18.2    martin 		int s, bound;
    323  1.34.18.2    martin 
    324       1.29        ad 		KASSERT(ci == curcpu());
    325  1.34.18.2    martin 
    326  1.34.18.2    martin 		if (flags & XC_HIGHPRI)
    327  1.34.18.2    martin 			s = splsoftserial();
    328  1.34.18.2    martin 		else
    329  1.34.18.2    martin 			bound = curlwp_bind();
    330       1.29        ad 		(*func)(arg1, arg2);
    331  1.34.18.2    martin 		if (flags & XC_HIGHPRI)
    332  1.34.18.2    martin 			splx(s);
    333  1.34.18.2    martin 		else
    334  1.34.18.2    martin 			curlwp_bindx(bound);
    335  1.34.18.2    martin 
    336       1.29        ad 		return 0;
    337       1.29        ad 	}
    338       1.29        ad 
    339        1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    340       1.21     ozaki 		int ipl = xc_extract_ipl(flags);
    341       1.21     ozaki 		return xc_highpri(func, arg1, arg2, ci, ipl);
    342       1.12     rmind 	} else {
    343       1.12     rmind 		return xc_lowpri(func, arg1, arg2, ci);
    344       1.12     rmind 	}
    345       1.12     rmind }
    346       1.12     rmind 
    347       1.12     rmind /*
    348       1.12     rmind  * xc_wait:
    349       1.12     rmind  *
    350       1.12     rmind  *	Wait for a cross call to complete.
    351       1.12     rmind  */
    352       1.12     rmind void
    353       1.12     rmind xc_wait(uint64_t where)
    354       1.12     rmind {
    355       1.12     rmind 	xc_state_t *xc;
    356       1.12     rmind 
    357       1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    358       1.26     ozaki 	ASSERT_SLEEPABLE();
    359       1.12     rmind 
    360       1.29        ad 	if (__predict_false(!mp_online)) {
    361       1.29        ad 		return;
    362       1.29        ad 	}
    363       1.29        ad 
    364       1.12     rmind 	/* Determine whether it is high or low priority cross-call. */
    365       1.12     rmind 	if ((where & XC_PRI_BIT) != 0) {
    366       1.12     rmind 		xc = &xc_high_pri;
    367       1.12     rmind 		where &= ~XC_PRI_BIT;
    368        1.2        ad 	} else {
    369       1.12     rmind 		xc = &xc_low_pri;
    370       1.12     rmind 	}
    371       1.12     rmind 
    372       1.32  riastrad #ifdef __HAVE_ATOMIC64_LOADSTORE
    373       1.32  riastrad 	/* Fast path, if already done. */
    374       1.32  riastrad 	if (atomic_load_acquire(&xc->xc_donep) >= where) {
    375       1.32  riastrad 		return;
    376       1.32  riastrad 	}
    377       1.32  riastrad #endif
    378       1.32  riastrad 
    379       1.32  riastrad 	/* Slow path: block until awoken. */
    380       1.31        ad 	mutex_enter(&xc->xc_lock);
    381       1.31        ad 	while (xc->xc_donep < where) {
    382       1.31        ad 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    383        1.2        ad 	}
    384       1.31        ad 	mutex_exit(&xc->xc_lock);
    385        1.2        ad }
    386        1.2        ad 
    387        1.2        ad /*
    388        1.2        ad  * xc_lowpri:
    389        1.2        ad  *
    390        1.2        ad  *	Trigger a low priority call on one or more CPUs.
    391        1.2        ad  */
    392       1.12     rmind static inline uint64_t
    393       1.12     rmind xc_lowpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
    394        1.2        ad {
    395       1.12     rmind 	xc_state_t *xc = &xc_low_pri;
    396        1.2        ad 	CPU_INFO_ITERATOR cii;
    397       1.10  uebayasi 	uint64_t where;
    398        1.2        ad 
    399       1.12     rmind 	mutex_enter(&xc->xc_lock);
    400       1.19     ozaki 	while (xc->xc_headp != xc->xc_donep) {
    401       1.12     rmind 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    402       1.12     rmind 	}
    403       1.12     rmind 	xc->xc_arg1 = arg1;
    404       1.12     rmind 	xc->xc_arg2 = arg2;
    405       1.12     rmind 	xc->xc_func = func;
    406        1.2        ad 	if (ci == NULL) {
    407        1.2        ad 		xc_broadcast_ev.ev_count++;
    408        1.2        ad 		for (CPU_INFO_FOREACH(cii, ci)) {
    409        1.8        ad 			if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
    410        1.8        ad 				continue;
    411       1.12     rmind 			xc->xc_headp += 1;
    412        1.2        ad 			ci->ci_data.cpu_xcall_pending = true;
    413        1.2        ad 			cv_signal(&ci->ci_data.cpu_xcall);
    414        1.2        ad 		}
    415        1.2        ad 	} else {
    416        1.2        ad 		xc_unicast_ev.ev_count++;
    417       1.12     rmind 		xc->xc_headp += 1;
    418        1.2        ad 		ci->ci_data.cpu_xcall_pending = true;
    419        1.2        ad 		cv_signal(&ci->ci_data.cpu_xcall);
    420        1.2        ad 	}
    421       1.19     ozaki 	KASSERT(xc->xc_donep < xc->xc_headp);
    422       1.12     rmind 	where = xc->xc_headp;
    423       1.12     rmind 	mutex_exit(&xc->xc_lock);
    424        1.2        ad 
    425       1.12     rmind 	/* Return a low priority ticket. */
    426       1.12     rmind 	KASSERT((where & XC_PRI_BIT) == 0);
    427        1.2        ad 	return where;
    428        1.2        ad }
    429        1.2        ad 
    430        1.2        ad /*
    431        1.2        ad  * xc_thread:
    432        1.2        ad  *
    433        1.2        ad  *	One thread per-CPU to dispatch low priority calls.
    434        1.2        ad  */
    435        1.2        ad static void
    436        1.2        ad xc_thread(void *cookie)
    437        1.2        ad {
    438       1.12     rmind 	struct cpu_info *ci = curcpu();
    439       1.12     rmind 	xc_state_t *xc = &xc_low_pri;
    440        1.2        ad 	void *arg1, *arg2;
    441        1.2        ad 	xcfunc_t func;
    442  1.34.18.1    martin 	struct lwp *l = curlwp;
    443  1.34.18.1    martin 
    444  1.34.18.1    martin 	KASSERTMSG(l->l_nopreempt == 0, "lwp %p nopreempt %d",
    445  1.34.18.1    martin 	    l, l->l_nopreempt);
    446        1.2        ad 
    447       1.12     rmind 	mutex_enter(&xc->xc_lock);
    448        1.2        ad 	for (;;) {
    449        1.2        ad 		while (!ci->ci_data.cpu_xcall_pending) {
    450       1.19     ozaki 			if (xc->xc_headp == xc->xc_donep) {
    451       1.12     rmind 				cv_broadcast(&xc->xc_busy);
    452       1.12     rmind 			}
    453       1.12     rmind 			cv_wait(&ci->ci_data.cpu_xcall, &xc->xc_lock);
    454        1.2        ad 			KASSERT(ci == curcpu());
    455        1.2        ad 		}
    456        1.2        ad 		ci->ci_data.cpu_xcall_pending = false;
    457       1.12     rmind 		func = xc->xc_func;
    458       1.12     rmind 		arg1 = xc->xc_arg1;
    459       1.12     rmind 		arg2 = xc->xc_arg2;
    460       1.12     rmind 		mutex_exit(&xc->xc_lock);
    461        1.2        ad 
    462       1.12     rmind 		KASSERT(func != NULL);
    463        1.2        ad 		(*func)(arg1, arg2);
    464        1.2        ad 
    465  1.34.18.1    martin 		KASSERTMSG(l->l_nopreempt == 0, "lwp %p nopreempt %d func %p",
    466  1.34.18.1    martin 		    l, l->l_nopreempt, func);
    467  1.34.18.1    martin 
    468       1.12     rmind 		mutex_enter(&xc->xc_lock);
    469       1.32  riastrad #ifdef __HAVE_ATOMIC64_LOADSTORE
    470       1.32  riastrad 		atomic_store_release(&xc->xc_donep, xc->xc_donep + 1);
    471       1.32  riastrad #else
    472       1.12     rmind 		xc->xc_donep++;
    473       1.32  riastrad #endif
    474        1.2        ad 	}
    475        1.2        ad 	/* NOTREACHED */
    476        1.2        ad }
    477       1.12     rmind 
    478       1.12     rmind /*
    479       1.12     rmind  * xc_ipi_handler:
    480       1.12     rmind  *
    481       1.12     rmind  *	Handler of cross-call IPI.
    482       1.12     rmind  */
    483       1.12     rmind void
    484       1.12     rmind xc_ipi_handler(void)
    485       1.12     rmind {
    486       1.21     ozaki 	xc_state_t *xc = & xc_high_pri;
    487       1.21     ozaki 
    488       1.21     ozaki 	KASSERT(xc->xc_ipl < __arraycount(xc_sihs));
    489       1.24     ozaki 	KASSERT(xc_sihs[xc->xc_ipl] != NULL);
    490       1.21     ozaki 
    491       1.14    martin 	/* Executes xc__highpri_intr() via software interrupt. */
    492       1.21     ozaki 	softint_schedule(xc_sihs[xc->xc_ipl]);
    493       1.12     rmind }
    494       1.12     rmind 
    495       1.12     rmind /*
    496       1.14    martin  * xc__highpri_intr:
    497       1.12     rmind  *
    498       1.12     rmind  *	A software interrupt handler for high priority calls.
    499       1.12     rmind  */
    500       1.14    martin void
    501       1.14    martin xc__highpri_intr(void *dummy)
    502       1.12     rmind {
    503       1.12     rmind 	xc_state_t *xc = &xc_high_pri;
    504       1.12     rmind 	void *arg1, *arg2;
    505       1.12     rmind 	xcfunc_t func;
    506       1.12     rmind 
    507       1.20    martin 	KASSERTMSG(!cpu_intr_p(), "high priority xcall for function %p",
    508       1.20    martin 	    xc->xc_func);
    509       1.12     rmind 	/*
    510       1.12     rmind 	 * Lock-less fetch of function and its arguments.
    511       1.12     rmind 	 * Safe since it cannot change at this point.
    512       1.12     rmind 	 */
    513       1.12     rmind 	func = xc->xc_func;
    514       1.12     rmind 	arg1 = xc->xc_arg1;
    515       1.12     rmind 	arg2 = xc->xc_arg2;
    516       1.12     rmind 
    517       1.12     rmind 	KASSERT(func != NULL);
    518       1.12     rmind 	(*func)(arg1, arg2);
    519       1.12     rmind 
    520       1.12     rmind 	/*
    521       1.12     rmind 	 * Note the request as done, and if we have reached the head,
    522       1.12     rmind 	 * cross-call has been processed - notify waiters, if any.
    523       1.12     rmind 	 */
    524       1.12     rmind 	mutex_enter(&xc->xc_lock);
    525       1.28      maxv 	KASSERT(xc->xc_donep < xc->xc_headp);
    526       1.32  riastrad #ifdef __HAVE_ATOMIC64_LOADSTORE
    527       1.32  riastrad 	atomic_store_release(&xc->xc_donep, xc->xc_donep + 1);
    528       1.32  riastrad #else
    529       1.32  riastrad 	xc->xc_donep++;
    530       1.32  riastrad #endif
    531       1.32  riastrad 	if (xc->xc_donep == xc->xc_headp) {
    532       1.12     rmind 		cv_broadcast(&xc->xc_busy);
    533       1.12     rmind 	}
    534       1.12     rmind 	mutex_exit(&xc->xc_lock);
    535       1.12     rmind }
    536       1.12     rmind 
    537       1.12     rmind /*
    538       1.12     rmind  * xc_highpri:
    539       1.12     rmind  *
    540       1.12     rmind  *	Trigger a high priority call on one or more CPUs.
    541       1.12     rmind  */
    542       1.12     rmind static inline uint64_t
    543       1.21     ozaki xc_highpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci,
    544       1.21     ozaki     unsigned int ipl)
    545       1.12     rmind {
    546       1.12     rmind 	xc_state_t *xc = &xc_high_pri;
    547       1.12     rmind 	uint64_t where;
    548       1.12     rmind 
    549       1.12     rmind 	mutex_enter(&xc->xc_lock);
    550       1.12     rmind 	while (xc->xc_headp != xc->xc_donep) {
    551       1.12     rmind 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    552       1.12     rmind 	}
    553       1.12     rmind 	xc->xc_func = func;
    554       1.12     rmind 	xc->xc_arg1 = arg1;
    555       1.12     rmind 	xc->xc_arg2 = arg2;
    556       1.12     rmind 	xc->xc_headp += (ci ? 1 : ncpu);
    557       1.21     ozaki 	xc->xc_ipl = ipl;
    558       1.12     rmind 	where = xc->xc_headp;
    559       1.12     rmind 	mutex_exit(&xc->xc_lock);
    560       1.12     rmind 
    561       1.12     rmind 	/*
    562       1.12     rmind 	 * Send the IPI once lock is released.
    563       1.12     rmind 	 * Note: it will handle the local CPU case.
    564       1.12     rmind 	 */
    565       1.12     rmind 
    566       1.14    martin #ifdef _RUMPKERNEL
    567       1.14    martin 	rump_xc_highpri(ci);
    568       1.14    martin #else
    569       1.12     rmind #ifdef MULTIPROCESSOR
    570       1.12     rmind 	kpreempt_disable();
    571       1.12     rmind 	if (curcpu() == ci) {
    572       1.12     rmind 		/* Unicast: local CPU. */
    573       1.12     rmind 		xc_ipi_handler();
    574       1.12     rmind 	} else if (ci) {
    575       1.12     rmind 		/* Unicast: remote CPU. */
    576       1.12     rmind 		xc_send_ipi(ci);
    577       1.12     rmind 	} else {
    578       1.12     rmind 		/* Broadcast: all, including local. */
    579       1.12     rmind 		xc_send_ipi(NULL);
    580       1.12     rmind 		xc_ipi_handler();
    581       1.12     rmind 	}
    582       1.12     rmind 	kpreempt_enable();
    583       1.12     rmind #else
    584       1.15     rmind 	KASSERT(ci == NULL || curcpu() == ci);
    585       1.12     rmind 	xc_ipi_handler();
    586       1.12     rmind #endif
    587       1.14    martin #endif
    588       1.12     rmind 
    589       1.12     rmind 	/* Indicate a high priority ticket. */
    590       1.12     rmind 	return (where | XC_PRI_BIT);
    591       1.12     rmind }
    592