Home | History | Annotate | Line # | Download | only in kern
subr_pcu.c revision 1.17.2.1
      1  1.17.2.1       tls /*	$NetBSD: subr_pcu.c,v 1.17.2.1 2014/08/10 06:55:58 tls Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4  1.17.2.1       tls  * Copyright (c) 2011, 2014 The NetBSD Foundation, Inc.
      5       1.1     rmind  * All rights reserved.
      6       1.1     rmind  *
      7       1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     rmind  * by Mindaugas Rasiukevicius.
      9       1.1     rmind  *
     10       1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11       1.1     rmind  * modification, are permitted provided that the following conditions
     12       1.1     rmind  * are met:
     13       1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14       1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15       1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18       1.1     rmind  *
     19       1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     rmind  */
     31       1.1     rmind 
     32       1.1     rmind /*
     33       1.1     rmind  * Per CPU Unit (PCU) - is an interface to manage synchronization of any
     34       1.1     rmind  * per CPU context (unit) tied with LWP context.  Typical use: FPU state.
     35       1.1     rmind  *
     36       1.1     rmind  * Concurrency notes:
     37       1.1     rmind  *
     38       1.1     rmind  *	PCU state may be loaded only by the current LWP, that is, curlwp.
     39       1.1     rmind  *	Therefore, only LWP itself can set a CPU for lwp_t::l_pcu_cpu[id].
     40       1.1     rmind  *
     41  1.17.2.1       tls  *	There are some important rules about operation calls.  The request
     42  1.17.2.1       tls  *	for a PCU release can be from a) the owner LWP (regardless whether
     43  1.17.2.1       tls  *	the PCU state is on the current CPU or remote CPU) b) any other LWP
     44  1.17.2.1       tls  *	running on that CPU (in such case, the owner LWP is on a remote CPU
     45  1.17.2.1       tls  *	or sleeping).
     46  1.17.2.1       tls  *
     47  1.17.2.1       tls  *	In any case, the PCU state can *only* be changed from the current
     48  1.17.2.1       tls  *	CPU.  If said PCU state is on the remote CPU, a cross-call will be
     49  1.17.2.1       tls  *	sent by the owner LWP.  Therefore struct cpu_info::ci_pcu_curlwp[id]
     50  1.17.2.1       tls  *	may only be changed by the current CPU and lwp_t::l_pcu_cpu[id] may
     51  1.17.2.1       tls  *	only be cleared by the CPU which has the PCU state loaded.
     52       1.1     rmind  */
     53       1.1     rmind 
     54       1.1     rmind #include <sys/cdefs.h>
     55  1.17.2.1       tls __KERNEL_RCSID(0, "$NetBSD: subr_pcu.c,v 1.17.2.1 2014/08/10 06:55:58 tls Exp $");
     56       1.1     rmind 
     57       1.1     rmind #include <sys/param.h>
     58       1.1     rmind #include <sys/cpu.h>
     59       1.1     rmind #include <sys/lwp.h>
     60       1.1     rmind #include <sys/pcu.h>
     61  1.17.2.1       tls #include <sys/ipi.h>
     62       1.1     rmind 
     63       1.3      matt #if PCU_UNIT_COUNT > 0
     64       1.3      matt 
     65      1.13      matt static inline void pcu_do_op(const pcu_ops_t *, lwp_t * const, const int);
     66      1.13      matt static void pcu_lwp_op(const pcu_ops_t *, lwp_t *, const int);
     67      1.13      matt 
     68  1.17.2.1       tls /*
     69  1.17.2.1       tls  * Internal PCU commands for the pcu_do_op() function.
     70  1.17.2.1       tls  */
     71  1.17.2.1       tls #define	PCU_CMD_SAVE		0x01	/* save PCU state to the LWP */
     72  1.17.2.1       tls #define	PCU_CMD_RELEASE		0x02	/* release PCU state on the CPU */
     73  1.17.2.1       tls 
     74  1.17.2.1       tls /*
     75  1.17.2.1       tls  * Message structure for another CPU passed via ipi(9).
     76  1.17.2.1       tls  */
     77  1.17.2.1       tls typedef struct {
     78  1.17.2.1       tls 	const pcu_ops_t *pcu;
     79  1.17.2.1       tls 	lwp_t *		owner;
     80  1.17.2.1       tls 	const int	flags;
     81  1.17.2.1       tls } pcu_ipi_msg_t;
     82      1.13      matt 
     83  1.17.2.1       tls /*
     84  1.17.2.1       tls  * PCU IPIs run at IPL_HIGH (aka IPL_PCU in this code).
     85  1.17.2.1       tls  */
     86  1.17.2.1       tls #define	splpcu		splhigh
     87       1.1     rmind 
     88  1.17.2.1       tls /* PCU operations structure provided by the MD code. */
     89  1.17.2.1       tls extern const pcu_ops_t * const pcu_ops_md_defs[];
     90       1.4     rmind 
     91      1.11      yamt /*
     92      1.11      yamt  * pcu_switchpoint: release PCU state if the LWP is being run on another CPU.
     93  1.17.2.1       tls  * This routine is called on each context switch by by mi_switch().
     94      1.11      yamt  */
     95       1.1     rmind void
     96       1.4     rmind pcu_switchpoint(lwp_t *l)
     97       1.1     rmind {
     98  1.17.2.1       tls 	const uint32_t pcu_valid = l->l_pcu_valid;
     99  1.17.2.1       tls 	int s;
    100       1.1     rmind 
    101      1.12      matt 	KASSERTMSG(l == curlwp, "l %p != curlwp %p", l, curlwp);
    102       1.4     rmind 
    103  1.17.2.1       tls 	if (__predict_true(pcu_valid == 0)) {
    104       1.4     rmind 		/* PCUs are not in use. */
    105       1.4     rmind 		return;
    106       1.4     rmind 	}
    107  1.17.2.1       tls 	s = splpcu();
    108      1.13      matt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    109  1.17.2.1       tls 		if ((pcu_valid & (1U << id)) == 0) {
    110       1.4     rmind 			continue;
    111       1.4     rmind 		}
    112       1.5      matt 		struct cpu_info * const pcu_ci = l->l_pcu_cpu[id];
    113       1.4     rmind 		if (pcu_ci == NULL || pcu_ci == l->l_cpu) {
    114       1.4     rmind 			continue;
    115       1.4     rmind 		}
    116       1.4     rmind 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    117  1.17.2.1       tls 		pcu->pcu_state_release(l);
    118       1.4     rmind 	}
    119  1.17.2.1       tls 	splx(s);
    120       1.1     rmind }
    121       1.1     rmind 
    122      1.11      yamt /*
    123      1.11      yamt  * pcu_discard_all: discard PCU state of the given LWP.
    124      1.11      yamt  *
    125      1.11      yamt  * Used by exec and LWP exit.
    126      1.11      yamt  */
    127       1.7      matt void
    128       1.7      matt pcu_discard_all(lwp_t *l)
    129       1.7      matt {
    130  1.17.2.1       tls 	const uint32_t pcu_valid = l->l_pcu_valid;
    131       1.7      matt 
    132  1.17.2.1       tls 	KASSERT(l == curlwp || ((l->l_flag & LW_SYSTEM) && pcu_valid == 0));
    133       1.7      matt 
    134  1.17.2.1       tls 	if (__predict_true(pcu_valid == 0)) {
    135       1.7      matt 		/* PCUs are not in use. */
    136       1.7      matt 		return;
    137       1.7      matt 	}
    138       1.7      matt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    139  1.17.2.1       tls 		if ((pcu_valid & (1U << id)) == 0) {
    140       1.7      matt 			continue;
    141       1.7      matt 		}
    142       1.7      matt 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    143       1.7      matt 			continue;
    144       1.7      matt 		}
    145       1.7      matt 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    146  1.17.2.1       tls 		pcu_lwp_op(pcu, l, PCU_CMD_RELEASE);
    147       1.7      matt 	}
    148  1.17.2.1       tls 	l->l_pcu_valid = 0;
    149       1.7      matt }
    150       1.7      matt 
    151      1.11      yamt /*
    152      1.11      yamt  * pcu_save_all: save PCU state of the given LWP so that eg. coredump can
    153      1.11      yamt  * examine it.
    154      1.11      yamt  */
    155       1.7      matt void
    156       1.7      matt pcu_save_all(lwp_t *l)
    157       1.7      matt {
    158  1.17.2.1       tls 	const uint32_t pcu_valid = l->l_pcu_valid;
    159  1.17.2.1       tls 	int flags = PCU_CMD_SAVE;
    160  1.17.2.1       tls 
    161  1.17.2.1       tls 	/* If LW_WCORE, we are also releasing the state. */
    162  1.17.2.1       tls 	if (__predict_false(l->l_flag & LW_WCORE)) {
    163  1.17.2.1       tls 		flags |= PCU_CMD_RELEASE;
    164  1.17.2.1       tls 	}
    165       1.7      matt 
    166       1.9      matt 	/*
    167       1.9      matt 	 * Normally we save for the current LWP, but sometimes we get called
    168       1.9      matt 	 * with a different LWP (forking a system LWP or doing a coredump of
    169       1.9      matt 	 * a process with multiple threads) and we need to deal with that.
    170       1.9      matt 	 */
    171  1.17.2.1       tls 	KASSERT(l == curlwp || (((l->l_flag & LW_SYSTEM) ||
    172  1.17.2.1       tls 	    (curlwp->l_proc == l->l_proc && l->l_stat == LSSUSPENDED)) &&
    173  1.17.2.1       tls 	    pcu_valid == 0));
    174       1.7      matt 
    175  1.17.2.1       tls 	if (__predict_true(pcu_valid == 0)) {
    176       1.7      matt 		/* PCUs are not in use. */
    177       1.7      matt 		return;
    178       1.7      matt 	}
    179       1.7      matt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    180  1.17.2.1       tls 		if ((pcu_valid & (1U << id)) == 0) {
    181       1.7      matt 			continue;
    182       1.7      matt 		}
    183       1.7      matt 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    184       1.7      matt 			continue;
    185       1.7      matt 		}
    186       1.7      matt 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    187       1.9      matt 		pcu_lwp_op(pcu, l, flags);
    188       1.7      matt 	}
    189       1.7      matt }
    190       1.7      matt 
    191       1.1     rmind /*
    192       1.4     rmind  * pcu_do_op: save/release PCU state on the current CPU.
    193       1.1     rmind  *
    194  1.17.2.1       tls  * => Must be called at IPL_PCU or from the interrupt.
    195       1.1     rmind  */
    196       1.4     rmind static inline void
    197       1.4     rmind pcu_do_op(const pcu_ops_t *pcu, lwp_t * const l, const int flags)
    198       1.4     rmind {
    199       1.4     rmind 	struct cpu_info * const ci = curcpu();
    200       1.4     rmind 	const u_int id = pcu->pcu_id;
    201  1.17.2.1       tls 
    202  1.17.2.1       tls 	KASSERT(l->l_pcu_cpu[id] == ci);
    203  1.17.2.1       tls 
    204  1.17.2.1       tls 	if (flags & PCU_CMD_SAVE) {
    205  1.17.2.1       tls 		pcu->pcu_state_save(l);
    206  1.17.2.1       tls 	}
    207  1.17.2.1       tls 	if (flags & PCU_CMD_RELEASE) {
    208  1.17.2.1       tls 		pcu->pcu_state_release(l);
    209       1.4     rmind 		ci->ci_pcu_curlwp[id] = NULL;
    210       1.4     rmind 		l->l_pcu_cpu[id] = NULL;
    211       1.4     rmind 	}
    212       1.4     rmind }
    213       1.4     rmind 
    214       1.4     rmind /*
    215  1.17.2.1       tls  * pcu_cpu_ipi: helper routine to call pcu_do_op() via ipi(9).
    216       1.4     rmind  */
    217       1.1     rmind static void
    218  1.17.2.1       tls pcu_cpu_ipi(void *arg)
    219       1.1     rmind {
    220  1.17.2.1       tls 	const pcu_ipi_msg_t *pcu_msg = arg;
    221  1.17.2.1       tls 	const pcu_ops_t *pcu = pcu_msg->pcu;
    222       1.1     rmind 	const u_int id = pcu->pcu_id;
    223  1.17.2.1       tls 	lwp_t *l = pcu_msg->owner;
    224       1.4     rmind 
    225  1.17.2.1       tls 	KASSERT(pcu_msg->owner != NULL);
    226       1.1     rmind 
    227  1.17.2.1       tls 	if (curcpu()->ci_pcu_curlwp[id] != l) {
    228  1.17.2.1       tls 		/*
    229  1.17.2.1       tls 		 * Different ownership: another LWP raced with us and
    230  1.17.2.1       tls 		 * perform save and release.  There is nothing to do.
    231  1.17.2.1       tls 		 */
    232  1.17.2.1       tls 		KASSERT(l->l_pcu_cpu[id] == NULL);
    233       1.1     rmind 		return;
    234       1.1     rmind 	}
    235  1.17.2.1       tls 	pcu_do_op(pcu, l, pcu_msg->flags);
    236       1.1     rmind }
    237       1.1     rmind 
    238       1.1     rmind /*
    239       1.1     rmind  * pcu_lwp_op: perform PCU state save, release or both operations on LWP.
    240       1.1     rmind  */
    241       1.1     rmind static void
    242      1.13      matt pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, const int flags)
    243       1.1     rmind {
    244       1.1     rmind 	const u_int id = pcu->pcu_id;
    245       1.1     rmind 	struct cpu_info *ci;
    246       1.1     rmind 	int s;
    247       1.1     rmind 
    248       1.1     rmind 	/*
    249       1.1     rmind 	 * Caller should have re-checked if there is any state to manage.
    250       1.1     rmind 	 * Block the interrupts and inspect again, since cross-call sent
    251       1.1     rmind 	 * by remote CPU could have changed the state.
    252       1.1     rmind 	 */
    253  1.17.2.1       tls 	s = splpcu();
    254       1.1     rmind 	ci = l->l_pcu_cpu[id];
    255       1.1     rmind 	if (ci == curcpu()) {
    256       1.1     rmind 		/*
    257       1.1     rmind 		 * State is on the current CPU - just perform the operations.
    258       1.1     rmind 		 */
    259       1.6      matt 		KASSERTMSG(ci->ci_pcu_curlwp[id] == l,
    260      1.10       jym 		    "%s: cpu%u: pcu_curlwp[%u] (%p) != l (%p)",
    261      1.10       jym 		     __func__, cpu_index(ci), id, ci->ci_pcu_curlwp[id], l);
    262       1.4     rmind 		pcu_do_op(pcu, l, flags);
    263       1.1     rmind 		splx(s);
    264       1.1     rmind 		return;
    265       1.1     rmind 	}
    266       1.1     rmind 	if (__predict_false(ci == NULL)) {
    267       1.1     rmind 		/* Cross-call has won the race - no state to manage. */
    268      1.13      matt 		splx(s);
    269       1.1     rmind 		return;
    270       1.1     rmind 	}
    271       1.1     rmind 
    272       1.1     rmind 	/*
    273  1.17.2.1       tls 	 * The state is on the remote CPU: perform the operation(s) there.
    274       1.1     rmind 	 */
    275  1.17.2.1       tls 	pcu_ipi_msg_t pcu_msg = { .pcu = pcu, .owner = l, .flags = flags };
    276  1.17.2.1       tls 	ipi_msg_t ipi_msg = { .func = pcu_cpu_ipi, .arg = &pcu_msg };
    277  1.17.2.1       tls 	ipi_unicast(&ipi_msg, ci);
    278  1.17.2.1       tls 	splx(s);
    279  1.17.2.1       tls 
    280  1.17.2.1       tls 	/* Wait for completion. */
    281  1.17.2.1       tls 	ipi_wait(&ipi_msg);
    282       1.1     rmind 
    283  1.17.2.1       tls 	KASSERT((flags & PCU_CMD_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL);
    284       1.1     rmind }
    285       1.1     rmind 
    286       1.1     rmind /*
    287       1.1     rmind  * pcu_load: load/initialize the PCU state of current LWP on current CPU.
    288       1.1     rmind  */
    289       1.1     rmind void
    290       1.1     rmind pcu_load(const pcu_ops_t *pcu)
    291       1.1     rmind {
    292  1.17.2.1       tls 	lwp_t *oncpu_lwp, * const l = curlwp;
    293       1.1     rmind 	const u_int id = pcu->pcu_id;
    294       1.1     rmind 	struct cpu_info *ci, *curci;
    295       1.1     rmind 	int s;
    296       1.1     rmind 
    297       1.1     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    298       1.1     rmind 
    299  1.17.2.1       tls 	s = splpcu();
    300       1.1     rmind 	curci = curcpu();
    301       1.1     rmind 	ci = l->l_pcu_cpu[id];
    302       1.1     rmind 
    303       1.1     rmind 	/* Does this CPU already have our PCU state loaded? */
    304       1.1     rmind 	if (ci == curci) {
    305  1.17.2.1       tls 		/*
    306  1.17.2.1       tls 		 * Fault reoccurred while the PCU state is loaded and
    307  1.17.2.1       tls 		 * therefore PCU should be reenabled.  This happens
    308  1.17.2.1       tls 		 * if LWP is context switched to another CPU and then
    309  1.17.2.1       tls 		 * switched back to the original CPU while the state
    310  1.17.2.1       tls 		 * on that CPU has not been changed by other LWPs.
    311  1.17.2.1       tls 		 *
    312  1.17.2.1       tls 		 * It may also happen due to instruction "bouncing" on
    313  1.17.2.1       tls 		 * some architectures.
    314  1.17.2.1       tls 		 */
    315       1.1     rmind 		KASSERT(curci->ci_pcu_curlwp[id] == l);
    316  1.17.2.1       tls 		KASSERT(pcu_valid_p(pcu));
    317  1.17.2.1       tls 		pcu->pcu_state_load(l, PCU_VALID | PCU_REENABLE);
    318       1.1     rmind 		splx(s);
    319       1.1     rmind 		return;
    320       1.1     rmind 	}
    321       1.1     rmind 
    322       1.1     rmind 	/* If PCU state of this LWP is on the remote CPU - save it there. */
    323       1.1     rmind 	if (ci) {
    324  1.17.2.1       tls 		pcu_ipi_msg_t pcu_msg = { .pcu = pcu, .owner = l,
    325  1.17.2.1       tls 		    .flags = PCU_CMD_SAVE | PCU_CMD_RELEASE };
    326  1.17.2.1       tls 		ipi_msg_t ipi_msg = { .func = pcu_cpu_ipi, .arg = &pcu_msg };
    327  1.17.2.1       tls 		ipi_unicast(&ipi_msg, ci);
    328       1.1     rmind 		splx(s);
    329       1.1     rmind 
    330  1.17.2.1       tls 		/*
    331  1.17.2.1       tls 		 * Wait for completion, re-enter IPL_PCU and re-fetch
    332  1.17.2.1       tls 		 * the current CPU.
    333  1.17.2.1       tls 		 */
    334  1.17.2.1       tls 		ipi_wait(&ipi_msg);
    335  1.17.2.1       tls 		s = splpcu();
    336       1.1     rmind 		curci = curcpu();
    337       1.1     rmind 	}
    338       1.1     rmind 	KASSERT(l->l_pcu_cpu[id] == NULL);
    339       1.1     rmind 
    340       1.1     rmind 	/* Save the PCU state on the current CPU, if there is any. */
    341  1.17.2.1       tls 	if ((oncpu_lwp = curci->ci_pcu_curlwp[id]) != NULL) {
    342  1.17.2.1       tls 		pcu_do_op(pcu, oncpu_lwp, PCU_CMD_SAVE | PCU_CMD_RELEASE);
    343  1.17.2.1       tls 		KASSERT(curci->ci_pcu_curlwp[id] == NULL);
    344  1.17.2.1       tls 	}
    345       1.1     rmind 
    346       1.1     rmind 	/*
    347       1.1     rmind 	 * Finally, load the state for this LWP on this CPU.  Indicate to
    348  1.17.2.1       tls 	 * the load function whether PCU state was valid before this call.
    349       1.1     rmind 	 */
    350  1.17.2.1       tls 	const bool valid = ((1U << id) & l->l_pcu_valid) != 0;
    351  1.17.2.1       tls 	pcu->pcu_state_load(l, valid ? PCU_VALID : 0);
    352  1.17.2.1       tls 	curci->ci_pcu_curlwp[id] = l;
    353  1.17.2.1       tls 	l->l_pcu_cpu[id] = curci;
    354  1.17.2.1       tls 	l->l_pcu_valid |= (1U << id);
    355       1.1     rmind 	splx(s);
    356       1.1     rmind }
    357       1.1     rmind 
    358       1.1     rmind /*
    359  1.17.2.1       tls  * pcu_discard: discard the PCU state of current LWP.  If "valid"
    360  1.17.2.1       tls  * parameter is true, then keep considering the PCU state as valid.
    361       1.1     rmind  */
    362       1.1     rmind void
    363  1.17.2.1       tls pcu_discard(const pcu_ops_t *pcu, bool valid)
    364       1.1     rmind {
    365       1.1     rmind 	const u_int id = pcu->pcu_id;
    366       1.5      matt 	lwp_t * const l = curlwp;
    367       1.1     rmind 
    368       1.1     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    369       1.1     rmind 
    370  1.17.2.1       tls 	if (__predict_false(valid)) {
    371  1.17.2.1       tls 		l->l_pcu_valid |= (1U << id);
    372  1.17.2.1       tls 	} else {
    373  1.17.2.1       tls 		l->l_pcu_valid &= ~(1U << id);
    374  1.17.2.1       tls 	}
    375       1.1     rmind 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    376       1.1     rmind 		return;
    377       1.1     rmind 	}
    378  1.17.2.1       tls 	pcu_lwp_op(pcu, l, PCU_CMD_RELEASE);
    379       1.1     rmind }
    380       1.1     rmind 
    381       1.1     rmind /*
    382       1.1     rmind  * pcu_save_lwp: save PCU state to the given LWP.
    383       1.1     rmind  */
    384       1.1     rmind void
    385       1.4     rmind pcu_save(const pcu_ops_t *pcu)
    386       1.1     rmind {
    387       1.1     rmind 	const u_int id = pcu->pcu_id;
    388       1.4     rmind 	lwp_t * const l = curlwp;
    389       1.1     rmind 
    390       1.1     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    391       1.1     rmind 
    392       1.1     rmind 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    393       1.1     rmind 		return;
    394       1.1     rmind 	}
    395  1.17.2.1       tls 	pcu_lwp_op(pcu, l, PCU_CMD_SAVE | PCU_CMD_RELEASE);
    396       1.1     rmind }
    397       1.1     rmind 
    398       1.1     rmind /*
    399  1.17.2.1       tls  * pcu_save_all_on_cpu: save all PCU states on the current CPU.
    400      1.15  drochner  */
    401      1.15  drochner void
    402      1.15  drochner pcu_save_all_on_cpu(void)
    403      1.15  drochner {
    404  1.17.2.1       tls 	int s;
    405      1.15  drochner 
    406  1.17.2.1       tls 	s = splpcu();
    407      1.15  drochner 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    408  1.17.2.1       tls 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    409  1.17.2.1       tls 		lwp_t *l;
    410  1.17.2.1       tls 
    411  1.17.2.1       tls 		if ((l = curcpu()->ci_pcu_curlwp[id]) != NULL) {
    412  1.17.2.1       tls 			pcu_do_op(pcu, l, PCU_CMD_SAVE | PCU_CMD_RELEASE);
    413  1.17.2.1       tls 		}
    414      1.15  drochner 	}
    415  1.17.2.1       tls 	splx(s);
    416      1.15  drochner }
    417      1.15  drochner 
    418      1.15  drochner /*
    419  1.17.2.1       tls  * pcu_valid_p: return true if PCU state is considered valid.  Generally,
    420  1.17.2.1       tls  * it always becomes "valid" when pcu_load() is called.
    421       1.1     rmind  */
    422       1.1     rmind bool
    423  1.17.2.1       tls pcu_valid_p(const pcu_ops_t *pcu)
    424      1.13      matt {
    425      1.13      matt 	const u_int id = pcu->pcu_id;
    426      1.13      matt 	lwp_t * const l = curlwp;
    427      1.13      matt 
    428  1.17.2.1       tls 	return (l->l_pcu_valid & (1U << id)) != 0;
    429       1.1     rmind }
    430       1.3      matt 
    431       1.3      matt #endif /* PCU_UNIT_COUNT > 0 */
    432