Home | History | Annotate | Line # | Download | only in kern
subr_pcu.c revision 1.10.2.4
      1  1.10.2.4   yamt /*	$NetBSD: subr_pcu.c,v 1.10.2.4 2014/05/22 11:41:03 yamt Exp $	*/
      2       1.1  rmind 
      3       1.1  rmind /*-
      4       1.1  rmind  * Copyright (c) 2011 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.1  rmind  *	Request for a PCU release can be from owner LWP (whether PCU state
     42       1.1  rmind  *	is on current CPU or remote CPU) or any other LWP running on that
     43       1.1  rmind  *	CPU (in such case, owner LWP is on a remote CPU or sleeping).
     44       1.1  rmind  *
     45       1.1  rmind  *	In any case, PCU state can only be changed from the running CPU.
     46       1.1  rmind  *	If said PCU state is on the remote CPU, a cross-call will be sent
     47       1.1  rmind  *	by the owner LWP.  Therefore struct cpu_info::ci_pcu_curlwp[id]
     48       1.1  rmind  *	may only be changed by current CPU, and lwp_t::l_pcu_cpu[id] may
     49       1.1  rmind  *	only be unset by the CPU which has PCU state loaded.
     50       1.1  rmind  *
     51       1.1  rmind  *	There is a race condition: LWP may have a PCU state on a remote CPU,
     52       1.1  rmind  *	which it requests to be released via cross-call.  At the same time,
     53       1.1  rmind  *	other LWP on remote CPU might release existing PCU state and load
     54       1.1  rmind  *	its own one.  Cross-call may arrive after this and release different
     55       1.1  rmind  *	PCU state than intended.  In such case, such LWP would re-load its
     56       1.1  rmind  *	PCU state again.
     57       1.1  rmind  */
     58       1.1  rmind 
     59       1.1  rmind #include <sys/cdefs.h>
     60  1.10.2.4   yamt __KERNEL_RCSID(0, "$NetBSD: subr_pcu.c,v 1.10.2.4 2014/05/22 11:41:03 yamt Exp $");
     61       1.1  rmind 
     62       1.1  rmind #include <sys/param.h>
     63       1.1  rmind #include <sys/cpu.h>
     64       1.1  rmind #include <sys/lwp.h>
     65       1.1  rmind #include <sys/pcu.h>
     66       1.1  rmind #include <sys/xcall.h>
     67       1.1  rmind 
     68       1.3   matt #if PCU_UNIT_COUNT > 0
     69       1.3   matt 
     70  1.10.2.3   yamt static inline void pcu_do_op(const pcu_ops_t *, lwp_t * const, const int);
     71  1.10.2.3   yamt static void pcu_cpu_op(const pcu_ops_t *, const int);
     72  1.10.2.3   yamt static void pcu_lwp_op(const pcu_ops_t *, lwp_t *, const int);
     73  1.10.2.3   yamt 
     74  1.10.2.3   yamt __CTASSERT(PCU_KERNEL == 1);
     75  1.10.2.3   yamt 
     76  1.10.2.3   yamt #define	PCU_SAVE	(PCU_LOADED << 1) /* Save PCU state to the LWP. */
     77  1.10.2.3   yamt #define	PCU_RELEASE	(PCU_SAVE << 1)	/* Release PCU state on the CPU. */
     78  1.10.2.3   yamt #define	PCU_CLAIM	(PCU_RELEASE << 1)	/* CLAIM a PCU for a LWP. */
     79       1.1  rmind 
     80       1.4  rmind /* XXX */
     81       1.4  rmind extern const pcu_ops_t * const	pcu_ops_md_defs[];
     82       1.4  rmind 
     83  1.10.2.1   yamt /*
     84  1.10.2.1   yamt  * pcu_switchpoint: release PCU state if the LWP is being run on another CPU.
     85  1.10.2.1   yamt  *
     86  1.10.2.1   yamt  * On each context switches, called by mi_switch() with IPL_SCHED.
     87  1.10.2.1   yamt  * 'l' is an LWP which is just we switched to.  (the new curlwp)
     88  1.10.2.1   yamt  */
     89  1.10.2.1   yamt 
     90       1.1  rmind void
     91       1.4  rmind pcu_switchpoint(lwp_t *l)
     92       1.1  rmind {
     93  1.10.2.3   yamt 	const uint32_t pcu_kernel_inuse = l->l_pcu_used[PCU_KERNEL];
     94  1.10.2.3   yamt 	uint32_t pcu_user_inuse = l->l_pcu_used[PCU_USER];
     95       1.4  rmind 	/* int s; */
     96       1.1  rmind 
     97  1.10.2.2   yamt 	KASSERTMSG(l == curlwp, "l %p != curlwp %p", l, curlwp);
     98       1.4  rmind 
     99  1.10.2.3   yamt 	if (__predict_false(pcu_kernel_inuse != 0)) {
    100  1.10.2.3   yamt 		for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    101  1.10.2.3   yamt 			if ((pcu_kernel_inuse & (1 << id)) == 0) {
    102  1.10.2.3   yamt 				continue;
    103  1.10.2.3   yamt 			}
    104  1.10.2.3   yamt 			struct cpu_info * const pcu_ci = l->l_pcu_cpu[id];
    105  1.10.2.3   yamt 			if (pcu_ci == NULL || pcu_ci == l->l_cpu) {
    106  1.10.2.3   yamt 				continue;
    107  1.10.2.3   yamt 			}
    108  1.10.2.3   yamt 			const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    109  1.10.2.3   yamt 			/*
    110  1.10.2.3   yamt 			 * Steal the PCU away from the current owner and
    111  1.10.2.3   yamt 			 * take ownership of it.
    112  1.10.2.3   yamt 			 */
    113  1.10.2.3   yamt 			pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
    114  1.10.2.3   yamt 			pcu_do_op(pcu, l, PCU_KERNEL | PCU_CLAIM | PCU_RELOAD);
    115  1.10.2.3   yamt 			pcu_user_inuse &= ~(1 << id);
    116  1.10.2.3   yamt 		}
    117  1.10.2.3   yamt 	}
    118  1.10.2.3   yamt 
    119  1.10.2.3   yamt 	if (__predict_true(pcu_user_inuse == 0)) {
    120       1.4  rmind 		/* PCUs are not in use. */
    121       1.4  rmind 		return;
    122       1.4  rmind 	}
    123  1.10.2.1   yamt 	/* commented out as we know we are already at IPL_SCHED */
    124  1.10.2.4   yamt 	/* s = splsoftserial(); */
    125  1.10.2.3   yamt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    126  1.10.2.3   yamt 		if ((pcu_user_inuse & (1 << id)) == 0) {
    127       1.4  rmind 			continue;
    128       1.4  rmind 		}
    129       1.5   matt 		struct cpu_info * const pcu_ci = l->l_pcu_cpu[id];
    130       1.4  rmind 		if (pcu_ci == NULL || pcu_ci == l->l_cpu) {
    131       1.4  rmind 			continue;
    132       1.4  rmind 		}
    133       1.4  rmind 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    134  1.10.2.3   yamt 		pcu->pcu_state_release(l, 0);
    135       1.4  rmind 	}
    136       1.4  rmind 	/* splx(s); */
    137       1.1  rmind }
    138       1.1  rmind 
    139  1.10.2.1   yamt /*
    140  1.10.2.1   yamt  * pcu_discard_all: discard PCU state of the given LWP.
    141  1.10.2.1   yamt  *
    142  1.10.2.1   yamt  * Used by exec and LWP exit.
    143  1.10.2.1   yamt  */
    144  1.10.2.1   yamt 
    145       1.7   matt void
    146       1.7   matt pcu_discard_all(lwp_t *l)
    147       1.7   matt {
    148  1.10.2.3   yamt 	const uint32_t pcu_inuse = l->l_pcu_used[PCU_USER];
    149       1.7   matt 
    150       1.8   matt 	KASSERT(l == curlwp || ((l->l_flag & LW_SYSTEM) && pcu_inuse == 0));
    151  1.10.2.3   yamt 	KASSERT(l->l_pcu_used[PCU_KERNEL] == 0);
    152       1.7   matt 
    153       1.7   matt 	if (__predict_true(pcu_inuse == 0)) {
    154       1.7   matt 		/* PCUs are not in use. */
    155       1.7   matt 		return;
    156       1.7   matt 	}
    157  1.10.2.4   yamt 	const int s = splsoftserial();
    158       1.7   matt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    159       1.7   matt 		if ((pcu_inuse & (1 << id)) == 0) {
    160       1.7   matt 			continue;
    161       1.7   matt 		}
    162       1.7   matt 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    163       1.7   matt 			continue;
    164       1.7   matt 		}
    165       1.7   matt 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    166       1.7   matt 		/*
    167       1.7   matt 		 * We aren't releasing since this LWP isn't giving up PCU,
    168       1.7   matt 		 * just saving it.
    169       1.7   matt 		 */
    170       1.7   matt 		pcu_lwp_op(pcu, l, PCU_RELEASE);
    171       1.7   matt 	}
    172  1.10.2.3   yamt 	l->l_pcu_used[PCU_USER] = 0;
    173       1.7   matt 	splx(s);
    174       1.7   matt }
    175       1.7   matt 
    176  1.10.2.1   yamt /*
    177  1.10.2.1   yamt  * pcu_save_all: save PCU state of the given LWP so that eg. coredump can
    178  1.10.2.1   yamt  * examine it.
    179  1.10.2.1   yamt  */
    180  1.10.2.1   yamt 
    181       1.7   matt void
    182       1.7   matt pcu_save_all(lwp_t *l)
    183       1.7   matt {
    184  1.10.2.3   yamt 	const uint32_t pcu_inuse = l->l_pcu_used[PCU_USER];
    185  1.10.2.1   yamt 	/*
    186  1.10.2.1   yamt 	 * Unless LW_WCORE, we aren't releasing since this LWP isn't giving
    187  1.10.2.1   yamt 	 * up PCU, just saving it.
    188  1.10.2.1   yamt 	 */
    189       1.9   matt 	const int flags = PCU_SAVE | (l->l_flag & LW_WCORE ? PCU_RELEASE : 0);
    190       1.7   matt 
    191       1.9   matt 	/*
    192       1.9   matt 	 * Normally we save for the current LWP, but sometimes we get called
    193       1.9   matt 	 * with a different LWP (forking a system LWP or doing a coredump of
    194       1.9   matt 	 * a process with multiple threads) and we need to deal with that.
    195       1.9   matt 	 */
    196       1.9   matt 	KASSERT(l == curlwp
    197       1.9   matt 	    || (((l->l_flag & LW_SYSTEM)
    198       1.9   matt 		 || (curlwp->l_proc == l->l_proc && l->l_stat == LSSUSPENDED))
    199       1.9   matt 	        && pcu_inuse == 0));
    200  1.10.2.3   yamt 	KASSERT(l->l_pcu_used[PCU_KERNEL] == 0);
    201       1.7   matt 
    202       1.7   matt 	if (__predict_true(pcu_inuse == 0)) {
    203       1.7   matt 		/* PCUs are not in use. */
    204       1.7   matt 		return;
    205       1.7   matt 	}
    206  1.10.2.4   yamt 	const int s = splsoftserial();
    207       1.7   matt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    208       1.7   matt 		if ((pcu_inuse & (1 << id)) == 0) {
    209       1.7   matt 			continue;
    210       1.7   matt 		}
    211       1.7   matt 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    212       1.7   matt 			continue;
    213       1.7   matt 		}
    214       1.7   matt 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    215       1.9   matt 		pcu_lwp_op(pcu, l, flags);
    216       1.7   matt 	}
    217       1.7   matt 	splx(s);
    218       1.7   matt }
    219       1.7   matt 
    220       1.1  rmind /*
    221       1.4  rmind  * pcu_do_op: save/release PCU state on the current CPU.
    222       1.1  rmind  *
    223  1.10.2.4   yamt  * => Must be called at IPL_SOFTSERIAL or from the soft-interrupt.
    224       1.1  rmind  */
    225       1.4  rmind static inline void
    226       1.4  rmind pcu_do_op(const pcu_ops_t *pcu, lwp_t * const l, const int flags)
    227       1.4  rmind {
    228       1.4  rmind 	struct cpu_info * const ci = curcpu();
    229       1.4  rmind 	const u_int id = pcu->pcu_id;
    230  1.10.2.3   yamt 	u_int state_flags = flags & (PCU_KERNEL|PCU_RELOAD|PCU_ENABLE);
    231  1.10.2.3   yamt 	uint32_t id_mask = 1 << id;
    232  1.10.2.3   yamt 	const bool kernel_p = (l->l_pcu_used[PCU_KERNEL] & id_mask) != 0;
    233       1.4  rmind 
    234  1.10.2.3   yamt 	KASSERT(l->l_pcu_cpu[id] == (flags & PCU_CLAIM ? NULL : ci));
    235       1.4  rmind 
    236       1.4  rmind 	if (flags & PCU_SAVE) {
    237  1.10.2.3   yamt 		pcu->pcu_state_save(l, (kernel_p ? PCU_KERNEL : 0));
    238       1.4  rmind 	}
    239       1.4  rmind 	if (flags & PCU_RELEASE) {
    240  1.10.2.3   yamt 		pcu->pcu_state_release(l, state_flags);
    241  1.10.2.3   yamt 		if (flags & PCU_KERNEL) {
    242  1.10.2.3   yamt 			l->l_pcu_used[PCU_KERNEL] &= ~id_mask;
    243  1.10.2.3   yamt 		}
    244       1.4  rmind 		ci->ci_pcu_curlwp[id] = NULL;
    245       1.4  rmind 		l->l_pcu_cpu[id] = NULL;
    246       1.4  rmind 	}
    247  1.10.2.3   yamt 	if (flags & PCU_CLAIM) {
    248  1.10.2.3   yamt 		if (l->l_pcu_used[(flags & PCU_KERNEL)] & id_mask)
    249  1.10.2.3   yamt 			state_flags |= PCU_LOADED;
    250  1.10.2.3   yamt 		pcu->pcu_state_load(l, state_flags);
    251  1.10.2.3   yamt 		l->l_pcu_cpu[id] = ci;
    252  1.10.2.3   yamt 		ci->ci_pcu_curlwp[id] = l;
    253  1.10.2.3   yamt 		l->l_pcu_used[flags & PCU_KERNEL] |= id_mask;
    254  1.10.2.3   yamt 	}
    255  1.10.2.3   yamt 	if (flags == PCU_KERNEL) {
    256  1.10.2.3   yamt 		KASSERT(ci->ci_pcu_curlwp[id] == l);
    257  1.10.2.3   yamt 		pcu->pcu_state_save(l, 0);
    258  1.10.2.3   yamt 		l->l_pcu_used[PCU_KERNEL] |= id_mask;
    259  1.10.2.3   yamt 	}
    260       1.4  rmind }
    261       1.4  rmind 
    262       1.4  rmind /*
    263       1.6   matt  * pcu_cpu_op: helper routine to call pcu_do_op() via xcall(9) or
    264       1.6   matt  * by pcu_load.
    265       1.4  rmind  */
    266       1.1  rmind static void
    267       1.1  rmind pcu_cpu_op(const pcu_ops_t *pcu, const int flags)
    268       1.1  rmind {
    269       1.1  rmind 	const u_int id = pcu->pcu_id;
    270       1.4  rmind 	lwp_t * const l = curcpu()->ci_pcu_curlwp[id];
    271       1.4  rmind 
    272       1.6   matt 	//KASSERT(cpu_softintr_p());
    273       1.1  rmind 
    274       1.1  rmind 	/* If no state - nothing to do. */
    275       1.1  rmind 	if (l == NULL) {
    276       1.1  rmind 		return;
    277       1.1  rmind 	}
    278       1.4  rmind 	pcu_do_op(pcu, l, flags);
    279       1.1  rmind }
    280       1.1  rmind 
    281       1.1  rmind /*
    282       1.1  rmind  * pcu_lwp_op: perform PCU state save, release or both operations on LWP.
    283       1.1  rmind  */
    284       1.1  rmind static void
    285  1.10.2.3   yamt pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, const int flags)
    286       1.1  rmind {
    287       1.1  rmind 	const u_int id = pcu->pcu_id;
    288       1.1  rmind 	struct cpu_info *ci;
    289       1.1  rmind 	uint64_t where;
    290       1.1  rmind 	int s;
    291       1.1  rmind 
    292       1.1  rmind 	/*
    293       1.1  rmind 	 * Caller should have re-checked if there is any state to manage.
    294       1.1  rmind 	 * Block the interrupts and inspect again, since cross-call sent
    295       1.1  rmind 	 * by remote CPU could have changed the state.
    296       1.1  rmind 	 */
    297  1.10.2.4   yamt 	s = splsoftserial();
    298       1.1  rmind 	ci = l->l_pcu_cpu[id];
    299       1.1  rmind 	if (ci == curcpu()) {
    300       1.1  rmind 		/*
    301       1.1  rmind 		 * State is on the current CPU - just perform the operations.
    302       1.1  rmind 		 */
    303  1.10.2.3   yamt 		KASSERT((flags & PCU_CLAIM) == 0);
    304       1.6   matt 		KASSERTMSG(ci->ci_pcu_curlwp[id] == l,
    305      1.10    jym 		    "%s: cpu%u: pcu_curlwp[%u] (%p) != l (%p)",
    306      1.10    jym 		     __func__, cpu_index(ci), id, ci->ci_pcu_curlwp[id], l);
    307       1.4  rmind 		pcu_do_op(pcu, l, flags);
    308       1.1  rmind 		splx(s);
    309       1.1  rmind 		return;
    310       1.1  rmind 	}
    311       1.1  rmind 
    312       1.1  rmind 	if (__predict_false(ci == NULL)) {
    313  1.10.2.3   yamt 		if (flags & PCU_CLAIM) {
    314  1.10.2.3   yamt 			pcu_do_op(pcu, l, flags);
    315  1.10.2.3   yamt 		}
    316       1.1  rmind 		/* Cross-call has won the race - no state to manage. */
    317  1.10.2.3   yamt 		splx(s);
    318       1.1  rmind 		return;
    319       1.1  rmind 	}
    320       1.1  rmind 
    321  1.10.2.3   yamt 	splx(s);
    322  1.10.2.3   yamt 
    323       1.1  rmind 	/*
    324       1.1  rmind 	 * State is on the remote CPU - perform the operations there.
    325       1.1  rmind 	 * Note: there is a race condition; see description in the top.
    326       1.1  rmind 	 */
    327       1.1  rmind 	where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
    328       1.1  rmind 	    __UNCONST(pcu), (void *)(uintptr_t)flags, ci);
    329       1.1  rmind 	xc_wait(where);
    330       1.1  rmind 
    331       1.1  rmind 	KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL);
    332       1.1  rmind }
    333       1.1  rmind 
    334       1.1  rmind /*
    335       1.1  rmind  * pcu_load: load/initialize the PCU state of current LWP on current CPU.
    336       1.1  rmind  */
    337       1.1  rmind void
    338       1.1  rmind pcu_load(const pcu_ops_t *pcu)
    339       1.1  rmind {
    340       1.1  rmind 	const u_int id = pcu->pcu_id;
    341       1.1  rmind 	struct cpu_info *ci, *curci;
    342       1.5   matt 	lwp_t * const l = curlwp;
    343       1.1  rmind 	uint64_t where;
    344       1.1  rmind 	int s;
    345       1.1  rmind 
    346       1.1  rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    347       1.1  rmind 
    348  1.10.2.4   yamt 	s = splsoftserial();
    349       1.1  rmind 	curci = curcpu();
    350       1.1  rmind 	ci = l->l_pcu_cpu[id];
    351       1.1  rmind 
    352       1.1  rmind 	/* Does this CPU already have our PCU state loaded? */
    353       1.1  rmind 	if (ci == curci) {
    354       1.1  rmind 		KASSERT(curci->ci_pcu_curlwp[id] == l);
    355  1.10.2.4   yamt 		KASSERT(pcu_used_p(pcu));
    356  1.10.2.4   yamt 
    357  1.10.2.4   yamt 		/* Re-enable */
    358  1.10.2.4   yamt 		pcu->pcu_state_load(l, PCU_LOADED | PCU_ENABLE);
    359       1.1  rmind 		splx(s);
    360       1.1  rmind 		return;
    361       1.1  rmind 	}
    362       1.1  rmind 
    363       1.1  rmind 	/* If PCU state of this LWP is on the remote CPU - save it there. */
    364       1.1  rmind 	if (ci) {
    365       1.1  rmind 		splx(s);
    366       1.1  rmind 		/* Note: there is a race; see description in the top. */
    367       1.1  rmind 		where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
    368       1.1  rmind 		    __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci);
    369       1.1  rmind 		xc_wait(where);
    370       1.1  rmind 
    371  1.10.2.4   yamt 		/* Enter IPL_SOFTSERIAL and re-fetch the current CPU. */
    372  1.10.2.4   yamt 		s = splsoftserial();
    373       1.1  rmind 		curci = curcpu();
    374       1.1  rmind 	}
    375       1.1  rmind 	KASSERT(l->l_pcu_cpu[id] == NULL);
    376       1.1  rmind 
    377       1.1  rmind 	/* Save the PCU state on the current CPU, if there is any. */
    378       1.6   matt 	pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
    379       1.1  rmind 	KASSERT(curci->ci_pcu_curlwp[id] == NULL);
    380       1.1  rmind 
    381       1.1  rmind 	/*
    382       1.1  rmind 	 * Finally, load the state for this LWP on this CPU.  Indicate to
    383       1.1  rmind 	 * load function whether PCU was used before.  Note the usage.
    384       1.1  rmind 	 */
    385  1.10.2.3   yamt 	pcu_do_op(pcu, l, PCU_CLAIM | PCU_ENABLE | PCU_RELOAD);
    386       1.1  rmind 	splx(s);
    387       1.1  rmind }
    388       1.1  rmind 
    389       1.1  rmind /*
    390       1.1  rmind  * pcu_discard: discard the PCU state of current LWP.
    391  1.10.2.4   yamt  * If the "usesw" flag is set, pcu_used_p() will return "true".
    392       1.1  rmind  */
    393       1.1  rmind void
    394  1.10.2.4   yamt pcu_discard(const pcu_ops_t *pcu, bool usesw)
    395       1.1  rmind {
    396       1.1  rmind 	const u_int id = pcu->pcu_id;
    397       1.5   matt 	lwp_t * const l = curlwp;
    398       1.1  rmind 
    399       1.1  rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    400       1.1  rmind 
    401  1.10.2.4   yamt 	if (usesw)
    402  1.10.2.4   yamt 		l->l_pcu_used[PCU_USER] |= (1 << id);
    403  1.10.2.4   yamt 	else
    404  1.10.2.4   yamt 		l->l_pcu_used[PCU_USER] &= ~(1 << id);
    405  1.10.2.4   yamt 
    406       1.1  rmind 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    407       1.1  rmind 		return;
    408       1.1  rmind 	}
    409       1.1  rmind 	pcu_lwp_op(pcu, l, PCU_RELEASE);
    410       1.1  rmind }
    411       1.1  rmind 
    412       1.1  rmind /*
    413       1.1  rmind  * pcu_save_lwp: save PCU state to the given LWP.
    414       1.1  rmind  */
    415       1.1  rmind void
    416       1.4  rmind pcu_save(const pcu_ops_t *pcu)
    417       1.1  rmind {
    418       1.1  rmind 	const u_int id = pcu->pcu_id;
    419       1.4  rmind 	lwp_t * const l = curlwp;
    420       1.1  rmind 
    421       1.1  rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    422       1.1  rmind 
    423       1.1  rmind 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    424       1.1  rmind 		return;
    425       1.1  rmind 	}
    426       1.1  rmind 	pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE);
    427       1.1  rmind }
    428       1.1  rmind 
    429       1.1  rmind /*
    430  1.10.2.4   yamt  * pcu_save_all_on_cpu: save all PCU state on current CPU
    431  1.10.2.4   yamt  */
    432  1.10.2.4   yamt void
    433  1.10.2.4   yamt pcu_save_all_on_cpu(void)
    434  1.10.2.4   yamt {
    435  1.10.2.4   yamt 
    436  1.10.2.4   yamt 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    437  1.10.2.4   yamt 		pcu_cpu_op(pcu_ops_md_defs[id], PCU_SAVE | PCU_RELEASE);
    438  1.10.2.4   yamt 	}
    439  1.10.2.4   yamt }
    440  1.10.2.4   yamt 
    441  1.10.2.4   yamt /*
    442       1.1  rmind  * pcu_used: return true if PCU was used (pcu_load() case) by the LWP.
    443       1.1  rmind  */
    444       1.1  rmind bool
    445       1.4  rmind pcu_used_p(const pcu_ops_t *pcu)
    446       1.1  rmind {
    447       1.1  rmind 	const u_int id = pcu->pcu_id;
    448       1.4  rmind 	lwp_t * const l = curlwp;
    449       1.1  rmind 
    450  1.10.2.4   yamt 	return l->l_pcu_used[PCU_USER] & (1 << id);
    451  1.10.2.3   yamt }
    452  1.10.2.3   yamt 
    453  1.10.2.3   yamt void
    454  1.10.2.3   yamt pcu_kernel_acquire(const pcu_ops_t *pcu)
    455  1.10.2.3   yamt {
    456  1.10.2.3   yamt 	struct cpu_info * const ci = curcpu();
    457  1.10.2.3   yamt 	lwp_t * const l = curlwp;
    458  1.10.2.3   yamt 	const u_int id = pcu->pcu_id;
    459  1.10.2.3   yamt 
    460  1.10.2.3   yamt 	/*
    461  1.10.2.3   yamt 	 * If we own the PCU, save our user state.
    462  1.10.2.3   yamt 	 */
    463  1.10.2.3   yamt 	if (ci == l->l_pcu_cpu[id]) {
    464  1.10.2.3   yamt 		pcu_lwp_op(pcu, l, PCU_KERNEL);
    465  1.10.2.3   yamt 		return;
    466  1.10.2.3   yamt 	}
    467  1.10.2.3   yamt 	if (ci->ci_data.cpu_pcu_curlwp[id] != NULL) {
    468  1.10.2.3   yamt 		/*
    469  1.10.2.3   yamt 		 * The PCU is owned by another LWP so save its state.
    470  1.10.2.3   yamt 		 */
    471  1.10.2.3   yamt 		pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
    472  1.10.2.3   yamt 	}
    473  1.10.2.3   yamt 	/*
    474  1.10.2.3   yamt 	 * Mark the PCU as hijacked and take ownership of it.
    475  1.10.2.3   yamt 	 */
    476  1.10.2.3   yamt 	pcu_lwp_op(pcu, l, PCU_KERNEL | PCU_CLAIM | PCU_ENABLE | PCU_RELOAD);
    477  1.10.2.3   yamt }
    478  1.10.2.3   yamt 
    479  1.10.2.3   yamt void
    480  1.10.2.3   yamt pcu_kernel_release(const pcu_ops_t *pcu)
    481  1.10.2.3   yamt {
    482  1.10.2.3   yamt 	lwp_t * const l = curlwp;
    483  1.10.2.3   yamt 
    484  1.10.2.3   yamt 	KASSERT(l->l_pcu_used[PCU_KERNEL] & (1 << pcu->pcu_id));
    485  1.10.2.3   yamt 
    486  1.10.2.3   yamt 	/*
    487  1.10.2.3   yamt 	 * Release the PCU, if the curlwp wants to use it, it will have incur
    488  1.10.2.3   yamt 	 * a trap to reenable it.
    489  1.10.2.3   yamt 	 */
    490  1.10.2.3   yamt 	pcu_lwp_op(pcu, l, PCU_KERNEL | PCU_RELEASE);
    491       1.1  rmind }
    492       1.3   matt 
    493       1.3   matt #endif /* PCU_UNIT_COUNT > 0 */
    494