xref: /onnv-gate/usr/src/uts/common/os/kcpc.c (revision 6275:c0f4f775d1fa)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53156Sgirish  * Common Development and Distribution License (the "License").
63156Sgirish  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211414Scindi 
220Sstevel@tonic-gate /*
23*6275Strevtom  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/thread.h>
310Sstevel@tonic-gate #include <sys/cpuvar.h>
320Sstevel@tonic-gate #include <sys/inttypes.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/time.h>
35*6275Strevtom #include <sys/ksynch.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/kcpc.h>
380Sstevel@tonic-gate #include <sys/cpc_impl.h>
390Sstevel@tonic-gate #include <sys/cpc_pcbe.h>
400Sstevel@tonic-gate #include <sys/atomic.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/modctl.h>
430Sstevel@tonic-gate #include <sys/sdt.h>
440Sstevel@tonic-gate #if defined(__x86)
450Sstevel@tonic-gate #include <asm/clock.h>
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate 
480Sstevel@tonic-gate kmutex_t	kcpc_ctx_llock[CPC_HASH_BUCKETS];	/* protects ctx_list */
490Sstevel@tonic-gate kcpc_ctx_t	*kcpc_ctx_list[CPC_HASH_BUCKETS];	/* head of list */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate krwlock_t	kcpc_cpuctx_lock;	/* lock for 'kcpc_cpuctx' below */
530Sstevel@tonic-gate int		kcpc_cpuctx;		/* number of cpu-specific contexts */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate int kcpc_counts_include_idle = 1; /* Project Private /etc/system variable */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * These are set when a PCBE module is loaded.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate uint_t		cpc_ncounters = 0;
610Sstevel@tonic-gate pcbe_ops_t	*pcbe_ops = NULL;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Statistics on (mis)behavior
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static uint32_t kcpc_intrctx_count;    /* # overflows in an interrupt handler */
670Sstevel@tonic-gate static uint32_t kcpc_nullctx_count;    /* # overflows in a thread with no ctx */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Is misbehaviour (overflow in a thread with no context) fatal?
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate #ifdef DEBUG
730Sstevel@tonic-gate static int kcpc_nullctx_panic = 1;
740Sstevel@tonic-gate #else
750Sstevel@tonic-gate static int kcpc_nullctx_panic = 0;
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static void kcpc_lwp_create(kthread_t *t, kthread_t *ct);
790Sstevel@tonic-gate static void kcpc_restore(kcpc_ctx_t *ctx);
800Sstevel@tonic-gate static void kcpc_save(kcpc_ctx_t *ctx);
810Sstevel@tonic-gate static void kcpc_free(kcpc_ctx_t *ctx, int isexec);
820Sstevel@tonic-gate static int kcpc_configure_reqs(kcpc_ctx_t *ctx, kcpc_set_t *set, int *subcode);
830Sstevel@tonic-gate static void kcpc_free_configs(kcpc_set_t *set);
840Sstevel@tonic-gate static kcpc_ctx_t *kcpc_ctx_alloc(void);
850Sstevel@tonic-gate static void kcpc_ctx_clone(kcpc_ctx_t *ctx, kcpc_ctx_t *cctx);
860Sstevel@tonic-gate static void kcpc_ctx_free(kcpc_ctx_t *ctx);
870Sstevel@tonic-gate static int kcpc_assign_reqs(kcpc_set_t *set, kcpc_ctx_t *ctx);
880Sstevel@tonic-gate static int kcpc_tryassign(kcpc_set_t *set, int starting_req, int *scratch);
890Sstevel@tonic-gate static kcpc_set_t *kcpc_dup_set(kcpc_set_t *set);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate void
920Sstevel@tonic-gate kcpc_register_pcbe(pcbe_ops_t *ops)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	pcbe_ops = ops;
950Sstevel@tonic-gate 	cpc_ncounters = pcbe_ops->pcbe_ncounters();
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate int
990Sstevel@tonic-gate kcpc_bind_cpu(kcpc_set_t *set, processorid_t cpuid, int *subcode)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	cpu_t		*cp;
1020Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
1030Sstevel@tonic-gate 	int		error;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	ctx = kcpc_ctx_alloc();
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (kcpc_assign_reqs(set, ctx) != 0) {
1080Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
1090Sstevel@tonic-gate 		*subcode = CPC_RESOURCE_UNAVAIL;
1100Sstevel@tonic-gate 		return (EINVAL);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	ctx->kc_cpuid = cpuid;
1140Sstevel@tonic-gate 	ctx->kc_thread = curthread;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	set->ks_data = kmem_zalloc(set->ks_nreqs * sizeof (uint64_t), KM_SLEEP);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((error = kcpc_configure_reqs(ctx, set, subcode)) != 0) {
1190Sstevel@tonic-gate 		kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
1200Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
1210Sstevel@tonic-gate 		return (error);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	set->ks_ctx = ctx;
1250Sstevel@tonic-gate 	ctx->kc_set = set;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * We must hold cpu_lock to prevent DR, offlining, or unbinding while
1290Sstevel@tonic-gate 	 * we are manipulating the cpu_t and programming the hardware, else the
1300Sstevel@tonic-gate 	 * the cpu_t could go away while we're looking at it.
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1330Sstevel@tonic-gate 	cp = cpu_get(cpuid);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (cp == NULL)
1360Sstevel@tonic-gate 		/*
1370Sstevel@tonic-gate 		 * The CPU could have been DRd out while we were getting set up.
1380Sstevel@tonic-gate 		 */
1390Sstevel@tonic-gate 		goto unbound;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	mutex_enter(&cp->cpu_cpc_ctxlock);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (cp->cpu_cpc_ctx != NULL) {
1440Sstevel@tonic-gate 		/*
1450Sstevel@tonic-gate 		 * If this CPU already has a bound set, return an error.
1460Sstevel@tonic-gate 		 */
1470Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
1480Sstevel@tonic-gate 		goto unbound;
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (curthread->t_bind_cpu != cpuid) {
1520Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
1530Sstevel@tonic-gate 		goto unbound;
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	cp->cpu_cpc_ctx = ctx;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * Kernel preemption must be disabled while fiddling with the hardware
1590Sstevel@tonic-gate 	 * registers to prevent partial updates.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	kpreempt_disable();
1620Sstevel@tonic-gate 	ctx->kc_rawtick = KCPC_GET_TICK();
1630Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
1640Sstevel@tonic-gate 	kpreempt_enable();
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	mutex_exit(&cp->cpu_cpc_ctxlock);
1670Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1680Sstevel@tonic-gate 
169*6275Strevtom 	mutex_enter(&set->ks_lock);
170*6275Strevtom 	set->ks_state |= KCPC_SET_BOUND;
171*6275Strevtom 	cv_signal(&set->ks_condv);
172*6275Strevtom 	mutex_exit(&set->ks_lock);
173*6275Strevtom 
1740Sstevel@tonic-gate 	return (0);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate unbound:
1770Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1780Sstevel@tonic-gate 	set->ks_ctx = NULL;
1790Sstevel@tonic-gate 	kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
1800Sstevel@tonic-gate 	kcpc_ctx_free(ctx);
1810Sstevel@tonic-gate 	return (EAGAIN);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate int
1850Sstevel@tonic-gate kcpc_bind_thread(kcpc_set_t *set, kthread_t *t, int *subcode)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
1880Sstevel@tonic-gate 	int		error;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Only one set is allowed per context, so ensure there is no
1920Sstevel@tonic-gate 	 * existing context.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (t->t_cpc_ctx != NULL)
1960Sstevel@tonic-gate 		return (EEXIST);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	ctx = kcpc_ctx_alloc();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * The context must begin life frozen until it has been properly
2020Sstevel@tonic-gate 	 * programmed onto the hardware. This prevents the context ops from
2030Sstevel@tonic-gate 	 * worrying about it until we're ready.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	ctx->kc_flags |= KCPC_CTX_FREEZE;
2060Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime();
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (kcpc_assign_reqs(set, ctx) != 0) {
2090Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
2100Sstevel@tonic-gate 		*subcode = CPC_RESOURCE_UNAVAIL;
2110Sstevel@tonic-gate 		return (EINVAL);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	ctx->kc_cpuid = -1;
2150Sstevel@tonic-gate 	if (set->ks_flags & CPC_BIND_LWP_INHERIT)
2160Sstevel@tonic-gate 		ctx->kc_flags |= KCPC_CTX_LWPINHERIT;
2170Sstevel@tonic-gate 	ctx->kc_thread = t;
2180Sstevel@tonic-gate 	t->t_cpc_ctx = ctx;
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * Permit threads to look at their own hardware counters from userland.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	ctx->kc_flags |= KCPC_CTX_NONPRIV;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * Create the data store for this set.
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 	set->ks_data = kmem_alloc(set->ks_nreqs * sizeof (uint64_t), KM_SLEEP);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if ((error = kcpc_configure_reqs(ctx, set, subcode)) != 0) {
2300Sstevel@tonic-gate 		kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
2310Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
2320Sstevel@tonic-gate 		t->t_cpc_ctx = NULL;
2330Sstevel@tonic-gate 		return (error);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	set->ks_ctx = ctx;
2370Sstevel@tonic-gate 	ctx->kc_set = set;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Add a device context to the subject thread.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	installctx(t, ctx, kcpc_save, kcpc_restore, NULL,
2430Sstevel@tonic-gate 	    kcpc_lwp_create, NULL, kcpc_free);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * Ask the backend to program the hardware.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	if (t == curthread) {
2490Sstevel@tonic-gate 		kpreempt_disable();
2500Sstevel@tonic-gate 		ctx->kc_rawtick = KCPC_GET_TICK();
2510Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
2520Sstevel@tonic-gate 		pcbe_ops->pcbe_program(ctx);
2530Sstevel@tonic-gate 		kpreempt_enable();
2540Sstevel@tonic-gate 	} else
2550Sstevel@tonic-gate 		/*
2560Sstevel@tonic-gate 		 * Since we are the agent LWP, we know the victim LWP is stopped
2570Sstevel@tonic-gate 		 * until we're done here; no need to worry about preemption or
2580Sstevel@tonic-gate 		 * migration here. We still use an atomic op to clear the flag
2590Sstevel@tonic-gate 		 * to ensure the flags are always self-consistent; they can
2600Sstevel@tonic-gate 		 * still be accessed from, for instance, another CPU doing a
2610Sstevel@tonic-gate 		 * kcpc_invalidate_all().
2620Sstevel@tonic-gate 		 */
2630Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
2640Sstevel@tonic-gate 
265*6275Strevtom 	mutex_enter(&set->ks_lock);
266*6275Strevtom 	set->ks_state |= KCPC_SET_BOUND;
267*6275Strevtom 	cv_signal(&set->ks_condv);
268*6275Strevtom 	mutex_exit(&set->ks_lock);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Walk through each request in the set and ask the PCBE to configure a
2750Sstevel@tonic-gate  * corresponding counter.
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate static int
2780Sstevel@tonic-gate kcpc_configure_reqs(kcpc_ctx_t *ctx, kcpc_set_t *set, int *subcode)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	int		i;
2810Sstevel@tonic-gate 	int		ret;
2820Sstevel@tonic-gate 	kcpc_request_t	*rp;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
2850Sstevel@tonic-gate 		int n;
2860Sstevel@tonic-gate 		rp = &set->ks_req[i];
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		n = rp->kr_picnum;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		ASSERT(n >= 0 && n < cpc_ncounters);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		ASSERT(ctx->kc_pics[n].kp_req == NULL);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		if (rp->kr_flags & CPC_OVF_NOTIFY_EMT) {
2950Sstevel@tonic-gate 			if ((pcbe_ops->pcbe_caps & CPC_CAP_OVERFLOW_INTERRUPT)
2960Sstevel@tonic-gate 			    == 0) {
2970Sstevel@tonic-gate 				*subcode = -1;
2980Sstevel@tonic-gate 				return (ENOTSUP);
2990Sstevel@tonic-gate 			}
3000Sstevel@tonic-gate 			/*
3010Sstevel@tonic-gate 			 * If any of the counters have requested overflow
3020Sstevel@tonic-gate 			 * notification, we flag the context as being one that
3030Sstevel@tonic-gate 			 * cares about overflow.
3040Sstevel@tonic-gate 			 */
3050Sstevel@tonic-gate 			ctx->kc_flags |= KCPC_CTX_SIGOVF;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		rp->kr_config = NULL;
3090Sstevel@tonic-gate 		if ((ret = pcbe_ops->pcbe_configure(n, rp->kr_event,
3100Sstevel@tonic-gate 		    rp->kr_preset, rp->kr_flags, rp->kr_nattrs, rp->kr_attr,
3110Sstevel@tonic-gate 		    &(rp->kr_config), (void *)ctx)) != 0) {
3120Sstevel@tonic-gate 			kcpc_free_configs(set);
3130Sstevel@tonic-gate 			*subcode = ret;
3143732Sae112802 			switch (ret) {
3153732Sae112802 			case CPC_ATTR_REQUIRES_PRIVILEGE:
3163732Sae112802 			case CPC_HV_NO_ACCESS:
3170Sstevel@tonic-gate 				return (EACCES);
3183732Sae112802 			default:
3193732Sae112802 				return (EINVAL);
3203732Sae112802 			}
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		ctx->kc_pics[n].kp_req = rp;
3240Sstevel@tonic-gate 		rp->kr_picp = &ctx->kc_pics[n];
3250Sstevel@tonic-gate 		rp->kr_data = set->ks_data + rp->kr_index;
3260Sstevel@tonic-gate 		*rp->kr_data = rp->kr_preset;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	return (0);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate static void
3330Sstevel@tonic-gate kcpc_free_configs(kcpc_set_t *set)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	int i;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
3380Sstevel@tonic-gate 		if (set->ks_req[i].kr_config != NULL)
3390Sstevel@tonic-gate 			pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * buf points to a user address and the data should be copied out to that
3440Sstevel@tonic-gate  * address in the current process.
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate int
3470Sstevel@tonic-gate kcpc_sample(kcpc_set_t *set, uint64_t *buf, hrtime_t *hrtime, uint64_t *tick)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = set->ks_ctx;
3500Sstevel@tonic-gate 	uint64_t	curtick = KCPC_GET_TICK();
3510Sstevel@tonic-gate 
352*6275Strevtom 	mutex_enter(&set->ks_lock);
353*6275Strevtom 	if ((set->ks_state & KCPC_SET_BOUND) == 0) {
354*6275Strevtom 		mutex_exit(&set->ks_lock);
3550Sstevel@tonic-gate 		return (EINVAL);
356*6275Strevtom 	}
357*6275Strevtom 	mutex_exit(&set->ks_lock);
358*6275Strevtom 
359*6275Strevtom 	if (ctx->kc_flags & KCPC_CTX_INVALID)
3600Sstevel@tonic-gate 		return (EAGAIN);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0) {
3630Sstevel@tonic-gate 		/*
3640Sstevel@tonic-gate 		 * Kernel preemption must be disabled while reading the
3650Sstevel@tonic-gate 		 * hardware regs, and if this is a CPU-bound context, while
3660Sstevel@tonic-gate 		 * checking the CPU binding of the current thread.
3670Sstevel@tonic-gate 		 */
3680Sstevel@tonic-gate 		kpreempt_disable();
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		if (ctx->kc_cpuid != -1) {
3710Sstevel@tonic-gate 			if (curthread->t_bind_cpu != ctx->kc_cpuid) {
3720Sstevel@tonic-gate 				kpreempt_enable();
3730Sstevel@tonic-gate 				return (EAGAIN);
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		if (ctx->kc_thread == curthread) {
3780Sstevel@tonic-gate 			ctx->kc_hrtime = gethrtime();
3790Sstevel@tonic-gate 			pcbe_ops->pcbe_sample(ctx);
3800Sstevel@tonic-gate 			ctx->kc_vtick += curtick - ctx->kc_rawtick;
3810Sstevel@tonic-gate 			ctx->kc_rawtick = curtick;
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		kpreempt_enable();
3853732Sae112802 
3863732Sae112802 		/*
3873732Sae112802 		 * The config may have been invalidated by
3883732Sae112802 		 * the pcbe_sample op.
3893732Sae112802 		 */
3903732Sae112802 		if (ctx->kc_flags & KCPC_CTX_INVALID)
3913732Sae112802 			return (EAGAIN);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	if (copyout(set->ks_data, buf,
3950Sstevel@tonic-gate 	    set->ks_nreqs * sizeof (uint64_t)) == -1)
3960Sstevel@tonic-gate 		return (EFAULT);
3970Sstevel@tonic-gate 	if (copyout(&ctx->kc_hrtime, hrtime, sizeof (uint64_t)) == -1)
3980Sstevel@tonic-gate 		return (EFAULT);
3990Sstevel@tonic-gate 	if (copyout(&ctx->kc_vtick, tick, sizeof (uint64_t)) == -1)
4000Sstevel@tonic-gate 		return (EFAULT);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	return (0);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Stop the counters on the CPU this context is bound to.
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate static void
4090Sstevel@tonic-gate kcpc_stop_hw(kcpc_ctx_t *ctx)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	cpu_t *cp;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	ASSERT((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED))
4140Sstevel@tonic-gate 	    == KCPC_CTX_INVALID);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	kpreempt_disable();
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	cp = cpu_get(ctx->kc_cpuid);
4190Sstevel@tonic-gate 	ASSERT(cp != NULL);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (cp == CPU) {
4220Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
4230Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
4240Sstevel@tonic-gate 		    KCPC_CTX_INVALID_STOPPED);
4250Sstevel@tonic-gate 	} else
4260Sstevel@tonic-gate 		kcpc_remote_stop(cp);
4270Sstevel@tonic-gate 	kpreempt_enable();
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate int
4310Sstevel@tonic-gate kcpc_unbind(kcpc_set_t *set)
4320Sstevel@tonic-gate {
433*6275Strevtom 	kcpc_ctx_t	*ctx;
4340Sstevel@tonic-gate 	kthread_t	*t;
4350Sstevel@tonic-gate 
436*6275Strevtom 	/*
437*6275Strevtom 	 * We could be racing with the process's agent thread as it
438*6275Strevtom 	 * binds the set; we must wait for the set to finish binding
439*6275Strevtom 	 * before attempting to tear it down.
440*6275Strevtom 	 */
441*6275Strevtom 	mutex_enter(&set->ks_lock);
442*6275Strevtom 	while ((set->ks_state & KCPC_SET_BOUND) == 0)
443*6275Strevtom 		cv_wait(&set->ks_condv, &set->ks_lock);
444*6275Strevtom 	mutex_exit(&set->ks_lock);
4450Sstevel@tonic-gate 
446*6275Strevtom 	ctx = set->ks_ctx;
447*6275Strevtom 
448*6275Strevtom 	/*
449*6275Strevtom 	 * Use kc_lock to synchronize with kcpc_restore().
450*6275Strevtom 	 */
451*6275Strevtom 	mutex_enter(&ctx->kc_lock);
452*6275Strevtom 	ctx->kc_flags |= KCPC_CTX_INVALID;
453*6275Strevtom 	mutex_exit(&ctx->kc_lock);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (ctx->kc_cpuid == -1) {
4560Sstevel@tonic-gate 		t = ctx->kc_thread;
4570Sstevel@tonic-gate 		/*
4580Sstevel@tonic-gate 		 * The context is thread-bound and therefore has a device
4590Sstevel@tonic-gate 		 * context.  It will be freed via removectx() calling
4600Sstevel@tonic-gate 		 * freectx() calling kcpc_free().
4610Sstevel@tonic-gate 		 */
4620Sstevel@tonic-gate 		if (t == curthread &&
4635254Sgavinm 		    (ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
4640Sstevel@tonic-gate 			kpreempt_disable();
4650Sstevel@tonic-gate 			pcbe_ops->pcbe_allstop();
4660Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags,
4670Sstevel@tonic-gate 			    KCPC_CTX_INVALID_STOPPED);
4680Sstevel@tonic-gate 			kpreempt_enable();
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate #ifdef DEBUG
4710Sstevel@tonic-gate 		if (removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
4720Sstevel@tonic-gate 		    kcpc_lwp_create, NULL, kcpc_free) == 0)
4730Sstevel@tonic-gate 			panic("kcpc_unbind: context %p not preset on thread %p",
4740Sstevel@tonic-gate 			    ctx, t);
4750Sstevel@tonic-gate #else
4760Sstevel@tonic-gate 		(void) removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
4770Sstevel@tonic-gate 		    kcpc_lwp_create, NULL, kcpc_free);
4780Sstevel@tonic-gate #endif /* DEBUG */
4790Sstevel@tonic-gate 		t->t_cpc_set = NULL;
4800Sstevel@tonic-gate 		t->t_cpc_ctx = NULL;
4810Sstevel@tonic-gate 	} else {
4820Sstevel@tonic-gate 		/*
4830Sstevel@tonic-gate 		 * If we are unbinding a CPU-bound set from a remote CPU, the
4840Sstevel@tonic-gate 		 * native CPU's idle thread could be in the midst of programming
4850Sstevel@tonic-gate 		 * this context onto the CPU. We grab the context's lock here to
4860Sstevel@tonic-gate 		 * ensure that the idle thread is done with it. When we release
4870Sstevel@tonic-gate 		 * the lock, the CPU no longer has a context and the idle thread
4880Sstevel@tonic-gate 		 * will move on.
4890Sstevel@tonic-gate 		 *
4900Sstevel@tonic-gate 		 * cpu_lock must be held to prevent the CPU from being DR'd out
4910Sstevel@tonic-gate 		 * while we disassociate the context from the cpu_t.
4920Sstevel@tonic-gate 		 */
4930Sstevel@tonic-gate 		cpu_t *cp;
4940Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4950Sstevel@tonic-gate 		cp = cpu_get(ctx->kc_cpuid);
4960Sstevel@tonic-gate 		if (cp != NULL) {
4970Sstevel@tonic-gate 			/*
4980Sstevel@tonic-gate 			 * The CPU may have been DR'd out of the system.
4990Sstevel@tonic-gate 			 */
5000Sstevel@tonic-gate 			mutex_enter(&cp->cpu_cpc_ctxlock);
5010Sstevel@tonic-gate 			if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0)
5020Sstevel@tonic-gate 				kcpc_stop_hw(ctx);
5030Sstevel@tonic-gate 			ASSERT(ctx->kc_flags & KCPC_CTX_INVALID_STOPPED);
5040Sstevel@tonic-gate 			cp->cpu_cpc_ctx = NULL;
5050Sstevel@tonic-gate 			mutex_exit(&cp->cpu_cpc_ctxlock);
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
5080Sstevel@tonic-gate 		if (ctx->kc_thread == curthread) {
5090Sstevel@tonic-gate 			kcpc_free(ctx, 0);
5100Sstevel@tonic-gate 			curthread->t_cpc_set = NULL;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	return (0);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate int
5180Sstevel@tonic-gate kcpc_preset(kcpc_set_t *set, int index, uint64_t preset)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 	int i;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	ASSERT(set != NULL);
523*6275Strevtom 	ASSERT(set->ks_state & KCPC_SET_BOUND);
5240Sstevel@tonic-gate 	ASSERT(set->ks_ctx->kc_thread == curthread);
5250Sstevel@tonic-gate 	ASSERT(set->ks_ctx->kc_cpuid == -1);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (index < 0 || index >= set->ks_nreqs)
5280Sstevel@tonic-gate 		return (EINVAL);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
5310Sstevel@tonic-gate 		if (set->ks_req[i].kr_index == index)
5320Sstevel@tonic-gate 			break;
5330Sstevel@tonic-gate 	ASSERT(i != set->ks_nreqs);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	set->ks_req[i].kr_preset = preset;
5360Sstevel@tonic-gate 	return (0);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate int
5400Sstevel@tonic-gate kcpc_restart(kcpc_set_t *set)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = set->ks_ctx;
5430Sstevel@tonic-gate 	int		i;
5440Sstevel@tonic-gate 
545*6275Strevtom 	ASSERT(set->ks_state & KCPC_SET_BOUND);
5460Sstevel@tonic-gate 	ASSERT(ctx->kc_thread == curthread);
5470Sstevel@tonic-gate 	ASSERT(ctx->kc_cpuid == -1);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	kpreempt_disable();
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * If the user is doing this on a running set, make sure the counters
5530Sstevel@tonic-gate 	 * are stopped first.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
5560Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
5590Sstevel@tonic-gate 		*(set->ks_req[i].kr_data) = set->ks_req[i].kr_preset;
5600Sstevel@tonic-gate 		pcbe_ops->pcbe_configure(0, NULL, set->ks_req[i].kr_preset,
5610Sstevel@tonic-gate 		    0, 0, NULL, &set->ks_req[i].kr_config, NULL);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * Ask the backend to program the hardware.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	ctx->kc_rawtick = KCPC_GET_TICK();
5680Sstevel@tonic-gate 	atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
5690Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
5700Sstevel@tonic-gate 	kpreempt_enable();
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	return (0);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate  * Caller must hold kcpc_cpuctx_lock.
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate int
5790Sstevel@tonic-gate kcpc_enable(kthread_t *t, int cmd, int enable)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = t->t_cpc_ctx;
5820Sstevel@tonic-gate 	kcpc_set_t	*set = t->t_cpc_set;
5830Sstevel@tonic-gate 	kcpc_set_t	*newset;
5840Sstevel@tonic-gate 	int		i;
5850Sstevel@tonic-gate 	int		flag;
5860Sstevel@tonic-gate 	int		err;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&kcpc_cpuctx_lock));
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (ctx == NULL) {
5910Sstevel@tonic-gate 		/*
5920Sstevel@tonic-gate 		 * This thread has a set but no context; it must be a
5930Sstevel@tonic-gate 		 * CPU-bound set.
5940Sstevel@tonic-gate 		 */
5950Sstevel@tonic-gate 		ASSERT(t->t_cpc_set != NULL);
5960Sstevel@tonic-gate 		ASSERT(t->t_cpc_set->ks_ctx->kc_cpuid != -1);
5970Sstevel@tonic-gate 		return (EINVAL);
5980Sstevel@tonic-gate 	} else if (ctx->kc_flags & KCPC_CTX_INVALID)
5990Sstevel@tonic-gate 		return (EAGAIN);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (cmd == CPC_ENABLE) {
6020Sstevel@tonic-gate 		if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
6030Sstevel@tonic-gate 			return (EINVAL);
6040Sstevel@tonic-gate 		kpreempt_disable();
6050Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
6060Sstevel@tonic-gate 		kcpc_restore(ctx);
6070Sstevel@tonic-gate 		kpreempt_enable();
6080Sstevel@tonic-gate 	} else if (cmd == CPC_DISABLE) {
6090Sstevel@tonic-gate 		if (ctx->kc_flags & KCPC_CTX_FREEZE)
6100Sstevel@tonic-gate 			return (EINVAL);
6110Sstevel@tonic-gate 		kpreempt_disable();
6120Sstevel@tonic-gate 		kcpc_save(ctx);
6130Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
6140Sstevel@tonic-gate 		kpreempt_enable();
6150Sstevel@tonic-gate 	} else if (cmd == CPC_USR_EVENTS || cmd == CPC_SYS_EVENTS) {
6160Sstevel@tonic-gate 		/*
6170Sstevel@tonic-gate 		 * Strategy for usr/sys: stop counters and update set's presets
6180Sstevel@tonic-gate 		 * with current counter values, unbind, update requests with
6190Sstevel@tonic-gate 		 * new config, then re-bind.
6200Sstevel@tonic-gate 		 */
6210Sstevel@tonic-gate 		flag = (cmd == CPC_USR_EVENTS) ?
6220Sstevel@tonic-gate 		    CPC_COUNT_USER: CPC_COUNT_SYSTEM;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		kpreempt_disable();
6250Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
6260Sstevel@tonic-gate 		    KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
6270Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
6280Sstevel@tonic-gate 		kpreempt_enable();
6290Sstevel@tonic-gate 		for (i = 0; i < set->ks_nreqs; i++) {
6300Sstevel@tonic-gate 			set->ks_req[i].kr_preset = *(set->ks_req[i].kr_data);
6310Sstevel@tonic-gate 			if (enable)
6320Sstevel@tonic-gate 				set->ks_req[i].kr_flags |= flag;
6330Sstevel@tonic-gate 			else
6340Sstevel@tonic-gate 				set->ks_req[i].kr_flags &= ~flag;
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 		newset = kcpc_dup_set(set);
6370Sstevel@tonic-gate 		if (kcpc_unbind(set) != 0)
6380Sstevel@tonic-gate 			return (EINVAL);
6390Sstevel@tonic-gate 		t->t_cpc_set = newset;
6400Sstevel@tonic-gate 		if (kcpc_bind_thread(newset, t, &err) != 0) {
6410Sstevel@tonic-gate 			t->t_cpc_set = NULL;
6420Sstevel@tonic-gate 			kcpc_free_set(newset);
6430Sstevel@tonic-gate 			return (EINVAL);
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 	} else
6460Sstevel@tonic-gate 		return (EINVAL);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	return (0);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Provide PCBEs with a way of obtaining the configs of every counter which will
6530Sstevel@tonic-gate  * be programmed together.
6540Sstevel@tonic-gate  *
6550Sstevel@tonic-gate  * If current is NULL, provide the first config.
6560Sstevel@tonic-gate  *
6570Sstevel@tonic-gate  * If data != NULL, caller wants to know where the data store associated with
6580Sstevel@tonic-gate  * the config we return is located.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate void *
6610Sstevel@tonic-gate kcpc_next_config(void *token, void *current, uint64_t **data)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate 	int		i;
6640Sstevel@tonic-gate 	kcpc_pic_t	*pic;
6650Sstevel@tonic-gate 	kcpc_ctx_t *ctx = (kcpc_ctx_t *)token;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if (current == NULL) {
6680Sstevel@tonic-gate 		/*
6690Sstevel@tonic-gate 		 * Client would like the first config, which may not be in
6700Sstevel@tonic-gate 		 * counter 0; we need to search through the counters for the
6710Sstevel@tonic-gate 		 * first config.
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++)
6740Sstevel@tonic-gate 			if (ctx->kc_pics[i].kp_req != NULL)
6750Sstevel@tonic-gate 				break;
6760Sstevel@tonic-gate 		/*
6770Sstevel@tonic-gate 		 * There are no counters configured for the given context.
6780Sstevel@tonic-gate 		 */
6790Sstevel@tonic-gate 		if (i == cpc_ncounters)
6800Sstevel@tonic-gate 			return (NULL);
6810Sstevel@tonic-gate 	} else {
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * There surely is a faster way to do this.
6840Sstevel@tonic-gate 		 */
6850Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++) {
6860Sstevel@tonic-gate 			pic = &ctx->kc_pics[i];
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 			if (pic->kp_req != NULL &&
6890Sstevel@tonic-gate 			    current == pic->kp_req->kr_config)
6900Sstevel@tonic-gate 				break;
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		/*
6940Sstevel@tonic-gate 		 * We found the current config at picnum i. Now search for the
6950Sstevel@tonic-gate 		 * next configured PIC.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		for (i++; i < cpc_ncounters; i++) {
6980Sstevel@tonic-gate 			pic = &ctx->kc_pics[i];
6990Sstevel@tonic-gate 			if (pic->kp_req != NULL)
7000Sstevel@tonic-gate 				break;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		if (i == cpc_ncounters)
7040Sstevel@tonic-gate 			return (NULL);
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (data != NULL) {
7080Sstevel@tonic-gate 		*data = ctx->kc_pics[i].kp_req->kr_data;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	return (ctx->kc_pics[i].kp_req->kr_config);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate static kcpc_ctx_t *
7160Sstevel@tonic-gate kcpc_ctx_alloc(void)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
7190Sstevel@tonic-gate 	long		hash;
7200Sstevel@tonic-gate 
721*6275Strevtom 	ctx = (kcpc_ctx_t *)kmem_zalloc(sizeof (kcpc_ctx_t), KM_SLEEP);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	hash = CPC_HASH_CTX(ctx);
7240Sstevel@tonic-gate 	mutex_enter(&kcpc_ctx_llock[hash]);
7250Sstevel@tonic-gate 	ctx->kc_next = kcpc_ctx_list[hash];
7260Sstevel@tonic-gate 	kcpc_ctx_list[hash] = ctx;
7270Sstevel@tonic-gate 	mutex_exit(&kcpc_ctx_llock[hash]);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	ctx->kc_pics = (kcpc_pic_t *)kmem_zalloc(sizeof (kcpc_pic_t) *
7300Sstevel@tonic-gate 	    cpc_ncounters, KM_SLEEP);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	ctx->kc_cpuid = -1;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	return (ctx);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate  * Copy set from ctx to the child context, cctx, if it has CPC_BIND_LWP_INHERIT
7390Sstevel@tonic-gate  * in the flags.
7400Sstevel@tonic-gate  */
7410Sstevel@tonic-gate static void
7420Sstevel@tonic-gate kcpc_ctx_clone(kcpc_ctx_t *ctx, kcpc_ctx_t *cctx)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	kcpc_set_t	*ks = ctx->kc_set, *cks;
7450Sstevel@tonic-gate 	int		i, j;
7460Sstevel@tonic-gate 	int		code;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	ASSERT(ks != NULL);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if ((ks->ks_flags & CPC_BIND_LWP_INHERIT) == 0)
7510Sstevel@tonic-gate 		return;
7520Sstevel@tonic-gate 
753*6275Strevtom 	cks = kmem_zalloc(sizeof (*cks), KM_SLEEP);
754*6275Strevtom 	cks->ks_state &= ~KCPC_SET_BOUND;
7550Sstevel@tonic-gate 	cctx->kc_set = cks;
7560Sstevel@tonic-gate 	cks->ks_flags = ks->ks_flags;
7570Sstevel@tonic-gate 	cks->ks_nreqs = ks->ks_nreqs;
7580Sstevel@tonic-gate 	cks->ks_req = kmem_alloc(cks->ks_nreqs *
7590Sstevel@tonic-gate 	    sizeof (kcpc_request_t), KM_SLEEP);
7600Sstevel@tonic-gate 	cks->ks_data = kmem_alloc(cks->ks_nreqs * sizeof (uint64_t),
7610Sstevel@tonic-gate 	    KM_SLEEP);
7620Sstevel@tonic-gate 	cks->ks_ctx = cctx;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	for (i = 0; i < cks->ks_nreqs; i++) {
7650Sstevel@tonic-gate 		cks->ks_req[i].kr_index = ks->ks_req[i].kr_index;
7660Sstevel@tonic-gate 		cks->ks_req[i].kr_picnum = ks->ks_req[i].kr_picnum;
7670Sstevel@tonic-gate 		(void) strncpy(cks->ks_req[i].kr_event,
7680Sstevel@tonic-gate 		    ks->ks_req[i].kr_event, CPC_MAX_EVENT_LEN);
7690Sstevel@tonic-gate 		cks->ks_req[i].kr_preset = ks->ks_req[i].kr_preset;
7700Sstevel@tonic-gate 		cks->ks_req[i].kr_flags = ks->ks_req[i].kr_flags;
7710Sstevel@tonic-gate 		cks->ks_req[i].kr_nattrs = ks->ks_req[i].kr_nattrs;
7720Sstevel@tonic-gate 		if (ks->ks_req[i].kr_nattrs > 0) {
7730Sstevel@tonic-gate 			cks->ks_req[i].kr_attr =
7740Sstevel@tonic-gate 			    kmem_alloc(ks->ks_req[i].kr_nattrs *
7755254Sgavinm 			    sizeof (kcpc_attr_t), KM_SLEEP);
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		for (j = 0; j < ks->ks_req[i].kr_nattrs; j++) {
7780Sstevel@tonic-gate 			(void) strncpy(cks->ks_req[i].kr_attr[j].ka_name,
7790Sstevel@tonic-gate 			    ks->ks_req[i].kr_attr[j].ka_name,
7800Sstevel@tonic-gate 			    CPC_MAX_ATTR_LEN);
7810Sstevel@tonic-gate 			cks->ks_req[i].kr_attr[j].ka_val =
7820Sstevel@tonic-gate 			    ks->ks_req[i].kr_attr[j].ka_val;
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	if (kcpc_configure_reqs(cctx, cks, &code) != 0)
7863732Sae112802 		kcpc_invalidate_config(cctx);
787*6275Strevtom 
788*6275Strevtom 	mutex_enter(&cks->ks_lock);
789*6275Strevtom 	cks->ks_state |= KCPC_SET_BOUND;
790*6275Strevtom 	cv_signal(&cks->ks_condv);
791*6275Strevtom 	mutex_exit(&cks->ks_lock);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate static void
7960Sstevel@tonic-gate kcpc_ctx_free(kcpc_ctx_t *ctx)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	kcpc_ctx_t	**loc;
7990Sstevel@tonic-gate 	long		hash = CPC_HASH_CTX(ctx);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	mutex_enter(&kcpc_ctx_llock[hash]);
8020Sstevel@tonic-gate 	loc = &kcpc_ctx_list[hash];
8030Sstevel@tonic-gate 	ASSERT(*loc != NULL);
8040Sstevel@tonic-gate 	while (*loc != ctx)
8050Sstevel@tonic-gate 		loc = &(*loc)->kc_next;
8060Sstevel@tonic-gate 	*loc = ctx->kc_next;
8070Sstevel@tonic-gate 	mutex_exit(&kcpc_ctx_llock[hash]);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	kmem_free(ctx->kc_pics, cpc_ncounters * sizeof (kcpc_pic_t));
810*6275Strevtom 	cv_destroy(&ctx->kc_condv);
811*6275Strevtom 	mutex_destroy(&ctx->kc_lock);
8120Sstevel@tonic-gate 	kmem_free(ctx, sizeof (*ctx));
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * Generic interrupt handler used on hardware that generates
8170Sstevel@tonic-gate  * overflow interrupts.
8180Sstevel@tonic-gate  *
8190Sstevel@tonic-gate  * Note: executed at high-level interrupt context!
8200Sstevel@tonic-gate  */
8210Sstevel@tonic-gate /*ARGSUSED*/
8220Sstevel@tonic-gate kcpc_ctx_t *
8230Sstevel@tonic-gate kcpc_overflow_intr(caddr_t arg, uint64_t bitmap)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
8260Sstevel@tonic-gate 	kthread_t	*t = curthread;
8270Sstevel@tonic-gate 	int		i;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * On both x86 and UltraSPARC, we may deliver the high-level
8310Sstevel@tonic-gate 	 * interrupt in kernel mode, just after we've started to run an
8320Sstevel@tonic-gate 	 * interrupt thread.  (That's because the hardware helpfully
8330Sstevel@tonic-gate 	 * delivers the overflow interrupt some random number of cycles
8340Sstevel@tonic-gate 	 * after the instruction that caused the overflow by which time
8350Sstevel@tonic-gate 	 * we're in some part of the kernel, not necessarily running on
8360Sstevel@tonic-gate 	 * the right thread).
8370Sstevel@tonic-gate 	 *
8380Sstevel@tonic-gate 	 * Check for this case here -- find the pinned thread
8390Sstevel@tonic-gate 	 * that was running when the interrupt went off.
8400Sstevel@tonic-gate 	 */
8410Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD) {
8420Sstevel@tonic-gate 		klwp_t *lwp;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		atomic_add_32(&kcpc_intrctx_count, 1);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * Note that t_lwp is always set to point at the underlying
8480Sstevel@tonic-gate 		 * thread, thus this will work in the presence of nested
8490Sstevel@tonic-gate 		 * interrupts.
8500Sstevel@tonic-gate 		 */
8510Sstevel@tonic-gate 		ctx = NULL;
8520Sstevel@tonic-gate 		if ((lwp = t->t_lwp) != NULL) {
8530Sstevel@tonic-gate 			t = lwptot(lwp);
8540Sstevel@tonic-gate 			ctx = t->t_cpc_ctx;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 	} else
8570Sstevel@tonic-gate 		ctx = t->t_cpc_ctx;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (ctx == NULL) {
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * This can easily happen if we're using the counters in
8620Sstevel@tonic-gate 		 * "shared" mode, for example, and an overflow interrupt
8630Sstevel@tonic-gate 		 * occurs while we are running cpustat.  In that case, the
8640Sstevel@tonic-gate 		 * bound thread that has the context that belongs to this
8650Sstevel@tonic-gate 		 * CPU is almost certainly sleeping (if it was running on
8660Sstevel@tonic-gate 		 * the CPU we'd have found it above), and the actual
8670Sstevel@tonic-gate 		 * interrupted thread has no knowledge of performance counters!
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		ctx = curthread->t_cpu->cpu_cpc_ctx;
8700Sstevel@tonic-gate 		if (ctx != NULL) {
8710Sstevel@tonic-gate 			/*
8720Sstevel@tonic-gate 			 * Return the bound context for this CPU to
8730Sstevel@tonic-gate 			 * the interrupt handler so that it can synchronously
8740Sstevel@tonic-gate 			 * sample the hardware counters and restart them.
8750Sstevel@tonic-gate 			 */
8760Sstevel@tonic-gate 			return (ctx);
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		/*
8800Sstevel@tonic-gate 		 * As long as the overflow interrupt really is delivered early
8810Sstevel@tonic-gate 		 * enough after trapping into the kernel to avoid switching
8820Sstevel@tonic-gate 		 * threads, we must always be able to find the cpc context,
8830Sstevel@tonic-gate 		 * or something went terribly wrong i.e. we ended up
8840Sstevel@tonic-gate 		 * running a passivated interrupt thread, a kernel
8850Sstevel@tonic-gate 		 * thread or we interrupted idle, all of which are Very Bad.
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		if (kcpc_nullctx_panic)
8880Sstevel@tonic-gate 			panic("null cpc context, thread %p", (void *)t);
8890Sstevel@tonic-gate 		atomic_add_32(&kcpc_nullctx_count, 1);
8900Sstevel@tonic-gate 	} else if ((ctx->kc_flags & KCPC_CTX_INVALID) == 0) {
8910Sstevel@tonic-gate 		/*
8920Sstevel@tonic-gate 		 * Schedule an ast to sample the counters, which will
8930Sstevel@tonic-gate 		 * propagate any overflow into the virtualized performance
8940Sstevel@tonic-gate 		 * counter(s), and may deliver a signal.
8950Sstevel@tonic-gate 		 */
8960Sstevel@tonic-gate 		ttolwp(t)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
8970Sstevel@tonic-gate 		/*
8980Sstevel@tonic-gate 		 * If a counter has overflowed which was counting on behalf of
8990Sstevel@tonic-gate 		 * a request which specified CPC_OVF_NOTIFY_EMT, send the
9000Sstevel@tonic-gate 		 * process a signal.
9010Sstevel@tonic-gate 		 */
9020Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++) {
9030Sstevel@tonic-gate 			if (ctx->kc_pics[i].kp_req != NULL &&
9040Sstevel@tonic-gate 			    bitmap & (1 << i) &&
9050Sstevel@tonic-gate 			    ctx->kc_pics[i].kp_req->kr_flags &
9060Sstevel@tonic-gate 			    CPC_OVF_NOTIFY_EMT) {
9070Sstevel@tonic-gate 				/*
9080Sstevel@tonic-gate 				 * A signal has been requested for this PIC, so
9090Sstevel@tonic-gate 				 * so freeze the context. The interrupt handler
9100Sstevel@tonic-gate 				 * has already stopped the counter hardware.
9110Sstevel@tonic-gate 				 */
9120Sstevel@tonic-gate 				atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
9130Sstevel@tonic-gate 				atomic_or_uint(&ctx->kc_pics[i].kp_flags,
9140Sstevel@tonic-gate 				    KCPC_PIC_OVERFLOWED);
9150Sstevel@tonic-gate 			}
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 		aston(t);
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 	return (NULL);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate  * The current thread context had an overflow interrupt; we're
9240Sstevel@tonic-gate  * executing here in high-level interrupt context.
9250Sstevel@tonic-gate  */
9260Sstevel@tonic-gate /*ARGSUSED*/
9270Sstevel@tonic-gate uint_t
9280Sstevel@tonic-gate kcpc_hw_overflow_intr(caddr_t arg1, caddr_t arg2)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
9310Sstevel@tonic-gate 	uint64_t	bitmap;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if (pcbe_ops == NULL ||
9340Sstevel@tonic-gate 	    (bitmap = pcbe_ops->pcbe_overflow_bitmap()) == 0)
9350Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
9363884Sha137994 
9370Sstevel@tonic-gate 	/*
9380Sstevel@tonic-gate 	 * Prevent any further interrupts.
9390Sstevel@tonic-gate 	 */
9400Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	/*
9430Sstevel@tonic-gate 	 * Invoke the "generic" handler.
9440Sstevel@tonic-gate 	 *
9450Sstevel@tonic-gate 	 * If the interrupt has occurred in the context of an lwp owning
9460Sstevel@tonic-gate 	 * the counters, then the handler posts an AST to the lwp to
9470Sstevel@tonic-gate 	 * trigger the actual sampling, and optionally deliver a signal or
9480Sstevel@tonic-gate 	 * restart the counters, on the way out of the kernel using
9490Sstevel@tonic-gate 	 * kcpc_hw_overflow_ast() (see below).
9500Sstevel@tonic-gate 	 *
9510Sstevel@tonic-gate 	 * On the other hand, if the handler returns the context to us
9520Sstevel@tonic-gate 	 * directly, then it means that there are no other threads in
9530Sstevel@tonic-gate 	 * the middle of updating it, no AST has been posted, and so we
9540Sstevel@tonic-gate 	 * should sample the counters here, and restart them with no
9550Sstevel@tonic-gate 	 * further fuss.
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	if ((ctx = kcpc_overflow_intr(arg1, bitmap)) != NULL) {
9580Sstevel@tonic-gate 		uint64_t curtick = KCPC_GET_TICK();
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		ctx->kc_hrtime = gethrtime_waitfree();
9610Sstevel@tonic-gate 		ctx->kc_vtick += curtick - ctx->kc_rawtick;
9620Sstevel@tonic-gate 		ctx->kc_rawtick = curtick;
9630Sstevel@tonic-gate 		pcbe_ops->pcbe_sample(ctx);
9640Sstevel@tonic-gate 		pcbe_ops->pcbe_program(ctx);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Called from trap() when processing the ast posted by the high-level
9720Sstevel@tonic-gate  * interrupt handler.
9730Sstevel@tonic-gate  */
9740Sstevel@tonic-gate int
9750Sstevel@tonic-gate kcpc_overflow_ast()
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = curthread->t_cpc_ctx;
9780Sstevel@tonic-gate 	int		i;
9790Sstevel@tonic-gate 	int		found = 0;
9800Sstevel@tonic-gate 	uint64_t	curtick = KCPC_GET_TICK();
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	ASSERT(ctx != NULL);	/* Beware of interrupt skid. */
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/*
9850Sstevel@tonic-gate 	 * An overflow happened: sample the context to ensure that
9860Sstevel@tonic-gate 	 * the overflow is propagated into the upper bits of the
9870Sstevel@tonic-gate 	 * virtualized 64-bit counter(s).
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	kpreempt_disable();
9900Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime_waitfree();
9910Sstevel@tonic-gate 	pcbe_ops->pcbe_sample(ctx);
9920Sstevel@tonic-gate 	kpreempt_enable();
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	ctx->kc_vtick += curtick - ctx->kc_rawtick;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * The interrupt handler has marked any pics with KCPC_PIC_OVERFLOWED
9980Sstevel@tonic-gate 	 * if that pic generated an overflow and if the request it was counting
9990Sstevel@tonic-gate 	 * on behalf of had CPC_OVERFLOW_REQUEST specified. We go through all
10000Sstevel@tonic-gate 	 * pics in the context and clear the KCPC_PIC_OVERFLOWED flags. If we
10010Sstevel@tonic-gate 	 * found any overflowed pics, keep the context frozen and return true
10020Sstevel@tonic-gate 	 * (thus causing a signal to be sent).
10030Sstevel@tonic-gate 	 */
10040Sstevel@tonic-gate 	for (i = 0; i < cpc_ncounters; i++) {
10050Sstevel@tonic-gate 		if (ctx->kc_pics[i].kp_flags & KCPC_PIC_OVERFLOWED) {
10060Sstevel@tonic-gate 			atomic_and_uint(&ctx->kc_pics[i].kp_flags,
10070Sstevel@tonic-gate 			    ~KCPC_PIC_OVERFLOWED);
10080Sstevel@tonic-gate 			found = 1;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	if (found)
10120Sstevel@tonic-gate 		return (1);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * Otherwise, re-enable the counters and continue life as before.
10160Sstevel@tonic-gate 	 */
10170Sstevel@tonic-gate 	kpreempt_disable();
10180Sstevel@tonic-gate 	atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
10190Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
10200Sstevel@tonic-gate 	kpreempt_enable();
10210Sstevel@tonic-gate 	return (0);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate  * Called when switching away from current thread.
10260Sstevel@tonic-gate  */
10270Sstevel@tonic-gate static void
10280Sstevel@tonic-gate kcpc_save(kcpc_ctx_t *ctx)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_INVALID) {
10310Sstevel@tonic-gate 		if (ctx->kc_flags & KCPC_CTX_INVALID_STOPPED)
10320Sstevel@tonic-gate 			return;
10330Sstevel@tonic-gate 		/*
10340Sstevel@tonic-gate 		 * This context has been invalidated but the counters have not
10350Sstevel@tonic-gate 		 * been stopped. Stop them here and mark the context stopped.
10360Sstevel@tonic-gate 		 */
10370Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
10380Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID_STOPPED);
10390Sstevel@tonic-gate 		return;
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
10430Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_FREEZE)
10440Sstevel@tonic-gate 		return;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	/*
10470Sstevel@tonic-gate 	 * Need to sample for all reqs into each req's current mpic.
10480Sstevel@tonic-gate 	 */
10490Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime();
10500Sstevel@tonic-gate 	ctx->kc_vtick += KCPC_GET_TICK() - ctx->kc_rawtick;
10510Sstevel@tonic-gate 	pcbe_ops->pcbe_sample(ctx);
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate static void
10550Sstevel@tonic-gate kcpc_restore(kcpc_ctx_t *ctx)
10560Sstevel@tonic-gate {
1057*6275Strevtom 	mutex_enter(&ctx->kc_lock);
10580Sstevel@tonic-gate 	if ((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED)) ==
10590Sstevel@tonic-gate 	    KCPC_CTX_INVALID)
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * The context is invalidated but has not been marked stopped.
10620Sstevel@tonic-gate 		 * We mark it as such here because we will not start the
10630Sstevel@tonic-gate 		 * counters during this context switch.
10640Sstevel@tonic-gate 		 */
1065*6275Strevtom 		ctx->kc_flags |= KCPC_CTX_INVALID_STOPPED;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 
1068*6275Strevtom 	if (ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_FREEZE)) {
1069*6275Strevtom 		mutex_exit(&ctx->kc_lock);
10700Sstevel@tonic-gate 		return;
1071*6275Strevtom 	}
1072*6275Strevtom 
1073*6275Strevtom 	/*
1074*6275Strevtom 	 * Set kc_flags to show that a kcpc_restore() is in progress to avoid
1075*6275Strevtom 	 * ctx & set related memory objects being freed without us knowing.
1076*6275Strevtom 	 * This can happen if an agent thread is executing a kcpc_unbind(),
1077*6275Strevtom 	 * with this thread as the target, whilst we're concurrently doing a
1078*6275Strevtom 	 * restorectx() during, for example, a proc_exit().  Effectively, by
1079*6275Strevtom 	 * doing this, we're asking kcpc_free() to cv_wait() until
1080*6275Strevtom 	 * kcpc_restore() has completed.
1081*6275Strevtom 	 */
1082*6275Strevtom 	ctx->kc_flags |= KCPC_CTX_RESTORE;
1083*6275Strevtom 	mutex_exit(&ctx->kc_lock);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * While programming the hardware, the counters should be stopped. We
10870Sstevel@tonic-gate 	 * don't do an explicit pcbe_allstop() here because they should have
10880Sstevel@tonic-gate 	 * been stopped already by the last consumer.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 	ctx->kc_rawtick = KCPC_GET_TICK();
10910Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
1092*6275Strevtom 
1093*6275Strevtom 	/*
1094*6275Strevtom 	 * Wake the agent thread if it's waiting in kcpc_free().
1095*6275Strevtom 	 */
1096*6275Strevtom 	mutex_enter(&ctx->kc_lock);
1097*6275Strevtom 	ctx->kc_flags &= ~KCPC_CTX_RESTORE;
1098*6275Strevtom 	cv_signal(&ctx->kc_condv);
1099*6275Strevtom 	mutex_exit(&ctx->kc_lock);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * If kcpc_counts_include_idle is set to 0 by the sys admin, we add the the
11040Sstevel@tonic-gate  * following context operators to the idle thread on each CPU. They stop the
11050Sstevel@tonic-gate  * counters when the idle thread is switched on, and they start them again when
11060Sstevel@tonic-gate  * it is switched off.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*ARGSUSED*/
11100Sstevel@tonic-gate void
11110Sstevel@tonic-gate kcpc_idle_save(struct cpu *cp)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	/*
11140Sstevel@tonic-gate 	 * The idle thread shouldn't be run anywhere else.
11150Sstevel@tonic-gate 	 */
11160Sstevel@tonic-gate 	ASSERT(CPU == cp);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	/*
11190Sstevel@tonic-gate 	 * We must hold the CPU's context lock to ensure the context isn't freed
11200Sstevel@tonic-gate 	 * while we're looking at it.
11210Sstevel@tonic-gate 	 */
11220Sstevel@tonic-gate 	mutex_enter(&cp->cpu_cpc_ctxlock);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	if ((cp->cpu_cpc_ctx == NULL) ||
11250Sstevel@tonic-gate 	    (cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
11260Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
11270Sstevel@tonic-gate 		return;
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	pcbe_ops->pcbe_program(cp->cpu_cpc_ctx);
11310Sstevel@tonic-gate 	mutex_exit(&cp->cpu_cpc_ctxlock);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate void
11350Sstevel@tonic-gate kcpc_idle_restore(struct cpu *cp)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	/*
11380Sstevel@tonic-gate 	 * The idle thread shouldn't be run anywhere else.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate 	ASSERT(CPU == cp);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/*
11430Sstevel@tonic-gate 	 * We must hold the CPU's context lock to ensure the context isn't freed
11440Sstevel@tonic-gate 	 * while we're looking at it.
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate 	mutex_enter(&cp->cpu_cpc_ctxlock);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	if ((cp->cpu_cpc_ctx == NULL) ||
11490Sstevel@tonic-gate 	    (cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
11500Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
11510Sstevel@tonic-gate 		return;
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
11550Sstevel@tonic-gate 	mutex_exit(&cp->cpu_cpc_ctxlock);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate /*ARGSUSED*/
11590Sstevel@tonic-gate static void
11600Sstevel@tonic-gate kcpc_lwp_create(kthread_t *t, kthread_t *ct)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = t->t_cpc_ctx, *cctx;
11630Sstevel@tonic-gate 	int		i;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (ctx == NULL || (ctx->kc_flags & KCPC_CTX_LWPINHERIT) == 0)
11660Sstevel@tonic-gate 		return;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	rw_enter(&kcpc_cpuctx_lock, RW_READER);
11690Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_INVALID) {
11700Sstevel@tonic-gate 		rw_exit(&kcpc_cpuctx_lock);
11710Sstevel@tonic-gate 		return;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 	cctx = kcpc_ctx_alloc();
11740Sstevel@tonic-gate 	kcpc_ctx_clone(ctx, cctx);
11750Sstevel@tonic-gate 	rw_exit(&kcpc_cpuctx_lock);
11760Sstevel@tonic-gate 
11773732Sae112802 	/*
11783732Sae112802 	 * Copy the parent context's kc_flags field, but don't overwrite
11793732Sae112802 	 * the child's in case it was modified during kcpc_ctx_clone.
11803732Sae112802 	 */
11813732Sae112802 	cctx->kc_flags |= ctx->kc_flags;
11820Sstevel@tonic-gate 	cctx->kc_thread = ct;
11830Sstevel@tonic-gate 	cctx->kc_cpuid = -1;
11840Sstevel@tonic-gate 	ct->t_cpc_set = cctx->kc_set;
11850Sstevel@tonic-gate 	ct->t_cpc_ctx = cctx;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (cctx->kc_flags & KCPC_CTX_SIGOVF) {
11880Sstevel@tonic-gate 		kcpc_set_t *ks = cctx->kc_set;
11890Sstevel@tonic-gate 		/*
11900Sstevel@tonic-gate 		 * Our contract with the user requires us to immediately send an
11910Sstevel@tonic-gate 		 * overflow signal to all children if we have the LWPINHERIT
11920Sstevel@tonic-gate 		 * and SIGOVF flags set. In addition, all counters should be
11930Sstevel@tonic-gate 		 * set to UINT64_MAX, and their pic's overflow flag turned on
11940Sstevel@tonic-gate 		 * so that our trap() processing knows to send a signal.
11950Sstevel@tonic-gate 		 */
11960Sstevel@tonic-gate 		atomic_or_uint(&cctx->kc_flags, KCPC_CTX_FREEZE);
11970Sstevel@tonic-gate 		for (i = 0; i < ks->ks_nreqs; i++) {
11980Sstevel@tonic-gate 			kcpc_request_t *kr = &ks->ks_req[i];
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 			if (kr->kr_flags & CPC_OVF_NOTIFY_EMT) {
12010Sstevel@tonic-gate 				*(kr->kr_data) = UINT64_MAX;
12020Sstevel@tonic-gate 				kr->kr_picp->kp_flags |= KCPC_PIC_OVERFLOWED;
12030Sstevel@tonic-gate 			}
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 		ttolwp(ct)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
12060Sstevel@tonic-gate 		aston(ct);
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	installctx(ct, cctx, kcpc_save, kcpc_restore,
12100Sstevel@tonic-gate 	    NULL, kcpc_lwp_create, NULL, kcpc_free);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate /*
12140Sstevel@tonic-gate  * Counter Stoppage Theory
12150Sstevel@tonic-gate  *
12160Sstevel@tonic-gate  * The counters may need to be stopped properly at the following occasions:
12170Sstevel@tonic-gate  *
12180Sstevel@tonic-gate  * 1) An LWP exits.
12190Sstevel@tonic-gate  * 2) A thread exits.
12200Sstevel@tonic-gate  * 3) An LWP performs an exec().
12210Sstevel@tonic-gate  * 4) A bound set is unbound.
12220Sstevel@tonic-gate  *
12230Sstevel@tonic-gate  * In addition to stopping the counters, the CPC context (a kcpc_ctx_t) may need
12240Sstevel@tonic-gate  * to be freed as well.
12250Sstevel@tonic-gate  *
12260Sstevel@tonic-gate  * Case 1: kcpc_passivate(), called via lwp_exit(), stops the counters. Later on
12270Sstevel@tonic-gate  * when the thread is freed, kcpc_free(), called by freectx(), frees the
12280Sstevel@tonic-gate  * context.
12290Sstevel@tonic-gate  *
12300Sstevel@tonic-gate  * Case 2: same as case 1 except kcpc_passivate is called from thread_exit().
12310Sstevel@tonic-gate  *
12320Sstevel@tonic-gate  * Case 3: kcpc_free(), called via freectx() via exec(), recognizes that it has
12330Sstevel@tonic-gate  * been called from exec. It stops the counters _and_ frees the context.
12340Sstevel@tonic-gate  *
12350Sstevel@tonic-gate  * Case 4: kcpc_unbind() stops the hardware _and_ frees the context.
12360Sstevel@tonic-gate  *
12370Sstevel@tonic-gate  * CPU-bound counters are always stopped via kcpc_unbind().
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * We're being called to delete the context; we ensure that all associated data
12420Sstevel@tonic-gate  * structures are freed, and that the hardware is passivated if this is an exec.
12430Sstevel@tonic-gate  */
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*ARGSUSED*/
12460Sstevel@tonic-gate static void
12470Sstevel@tonic-gate kcpc_free(kcpc_ctx_t *ctx, int isexec)
12480Sstevel@tonic-gate {
12490Sstevel@tonic-gate 	int		i;
12500Sstevel@tonic-gate 	kcpc_set_t	*set = ctx->kc_set;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	ASSERT(set != NULL);
12530Sstevel@tonic-gate 
1254*6275Strevtom 	/*
1255*6275Strevtom 	 * Wait for kcpc_restore() to finish before we tear things down.
1256*6275Strevtom 	 */
1257*6275Strevtom 	mutex_enter(&ctx->kc_lock);
1258*6275Strevtom 	while (ctx->kc_flags & KCPC_CTX_RESTORE)
1259*6275Strevtom 		cv_wait(&ctx->kc_condv, &ctx->kc_lock);
1260*6275Strevtom 	ctx->kc_flags |= KCPC_CTX_INVALID;
1261*6275Strevtom 	mutex_exit(&ctx->kc_lock);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if (isexec) {
12640Sstevel@tonic-gate 		/*
12650Sstevel@tonic-gate 		 * This thread is execing, and after the exec it should not have
12660Sstevel@tonic-gate 		 * any performance counter context. Stop the counters properly
12670Sstevel@tonic-gate 		 * here so the system isn't surprised by an overflow interrupt
12680Sstevel@tonic-gate 		 * later.
12690Sstevel@tonic-gate 		 */
12700Sstevel@tonic-gate 		if (ctx->kc_cpuid != -1) {
12710Sstevel@tonic-gate 			cpu_t *cp;
12720Sstevel@tonic-gate 			/*
12730Sstevel@tonic-gate 			 * CPU-bound context; stop the appropriate CPU's ctrs.
12740Sstevel@tonic-gate 			 * Hold cpu_lock while examining the CPU to ensure it
12750Sstevel@tonic-gate 			 * doesn't go away.
12760Sstevel@tonic-gate 			 */
12770Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
12780Sstevel@tonic-gate 			cp = cpu_get(ctx->kc_cpuid);
12790Sstevel@tonic-gate 			/*
12800Sstevel@tonic-gate 			 * The CPU could have been DR'd out, so only stop the
12810Sstevel@tonic-gate 			 * CPU and clear its context pointer if the CPU still
12820Sstevel@tonic-gate 			 * exists.
12830Sstevel@tonic-gate 			 */
12840Sstevel@tonic-gate 			if (cp != NULL) {
12850Sstevel@tonic-gate 				mutex_enter(&cp->cpu_cpc_ctxlock);
12860Sstevel@tonic-gate 				kcpc_stop_hw(ctx);
12870Sstevel@tonic-gate 				cp->cpu_cpc_ctx = NULL;
12880Sstevel@tonic-gate 				mutex_exit(&cp->cpu_cpc_ctxlock);
12890Sstevel@tonic-gate 			}
12900Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
12910Sstevel@tonic-gate 			ASSERT(curthread->t_cpc_ctx == NULL);
12920Sstevel@tonic-gate 		} else {
12930Sstevel@tonic-gate 			/*
12940Sstevel@tonic-gate 			 * Thread-bound context; stop _this_ CPU's counters.
12950Sstevel@tonic-gate 			 */
12960Sstevel@tonic-gate 			kpreempt_disable();
12970Sstevel@tonic-gate 			pcbe_ops->pcbe_allstop();
12980Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags,
12990Sstevel@tonic-gate 			    KCPC_CTX_INVALID_STOPPED);
13000Sstevel@tonic-gate 			kpreempt_enable();
13010Sstevel@tonic-gate 			curthread->t_cpc_ctx = NULL;
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		/*
13050Sstevel@tonic-gate 		 * Since we are being called from an exec and we know that
13060Sstevel@tonic-gate 		 * exec is not permitted via the agent thread, we should clean
13070Sstevel@tonic-gate 		 * up this thread's CPC state completely, and not leave dangling
13080Sstevel@tonic-gate 		 * CPC pointers behind.
13090Sstevel@tonic-gate 		 */
13100Sstevel@tonic-gate 		ASSERT(ctx->kc_thread == curthread);
13110Sstevel@tonic-gate 		curthread->t_cpc_set = NULL;
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	/*
13150Sstevel@tonic-gate 	 * Walk through each request in this context's set and free the PCBE's
13160Sstevel@tonic-gate 	 * configuration if it exists.
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
13190Sstevel@tonic-gate 		if (set->ks_req[i].kr_config != NULL)
13200Sstevel@tonic-gate 			pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
13240Sstevel@tonic-gate 	kcpc_ctx_free(ctx);
13250Sstevel@tonic-gate 	kcpc_free_set(set);
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate /*
13290Sstevel@tonic-gate  * Free the memory associated with a request set.
13300Sstevel@tonic-gate  */
13310Sstevel@tonic-gate void
13320Sstevel@tonic-gate kcpc_free_set(kcpc_set_t *set)
13330Sstevel@tonic-gate {
13340Sstevel@tonic-gate 	int		i;
13350Sstevel@tonic-gate 	kcpc_request_t	*req;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	ASSERT(set->ks_req != NULL);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
13400Sstevel@tonic-gate 		req = &set->ks_req[i];
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 		if (req->kr_nattrs != 0) {
13430Sstevel@tonic-gate 			kmem_free(req->kr_attr,
13440Sstevel@tonic-gate 			    req->kr_nattrs * sizeof (kcpc_attr_t));
13450Sstevel@tonic-gate 		}
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	kmem_free(set->ks_req, sizeof (kcpc_request_t) * set->ks_nreqs);
1349*6275Strevtom 	cv_destroy(&set->ks_condv);
1350*6275Strevtom 	mutex_destroy(&set->ks_lock);
13510Sstevel@tonic-gate 	kmem_free(set, sizeof (kcpc_set_t));
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate /*
13550Sstevel@tonic-gate  * Grab every existing context and mark it as invalid.
13560Sstevel@tonic-gate  */
13570Sstevel@tonic-gate void
13580Sstevel@tonic-gate kcpc_invalidate_all(void)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	kcpc_ctx_t *ctx;
13610Sstevel@tonic-gate 	long hash;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	for (hash = 0; hash < CPC_HASH_BUCKETS; hash++) {
13640Sstevel@tonic-gate 		mutex_enter(&kcpc_ctx_llock[hash]);
13650Sstevel@tonic-gate 		for (ctx = kcpc_ctx_list[hash]; ctx; ctx = ctx->kc_next)
13660Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
13670Sstevel@tonic-gate 		mutex_exit(&kcpc_ctx_llock[hash]);
13680Sstevel@tonic-gate 	}
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate /*
13723732Sae112802  * Interface for PCBEs to signal that an existing configuration has suddenly
13733732Sae112802  * become invalid.
13743732Sae112802  */
13753732Sae112802 void
13763732Sae112802 kcpc_invalidate_config(void *token)
13773732Sae112802 {
13783732Sae112802 	kcpc_ctx_t *ctx = token;
13793732Sae112802 
13803732Sae112802 	ASSERT(ctx != NULL);
13813732Sae112802 
13823732Sae112802 	atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
13833732Sae112802 }
13843732Sae112802 
13853732Sae112802 /*
13860Sstevel@tonic-gate  * Called from lwp_exit() and thread_exit()
13870Sstevel@tonic-gate  */
13880Sstevel@tonic-gate void
13890Sstevel@tonic-gate kcpc_passivate(void)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 	kcpc_ctx_t *ctx = curthread->t_cpc_ctx;
13920Sstevel@tonic-gate 	kcpc_set_t *set = curthread->t_cpc_set;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (set == NULL)
13950Sstevel@tonic-gate 		return;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/*
13980Sstevel@tonic-gate 	 * We're cleaning up after this thread; ensure there are no dangling
13990Sstevel@tonic-gate 	 * CPC pointers left behind. The context and set will be freed by
14000Sstevel@tonic-gate 	 * freectx() in the case of an LWP-bound set, and by kcpc_unbind() in
14010Sstevel@tonic-gate 	 * the case of a CPU-bound set.
14020Sstevel@tonic-gate 	 */
14030Sstevel@tonic-gate 	curthread->t_cpc_ctx = NULL;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	if (ctx == NULL) {
14060Sstevel@tonic-gate 		/*
14070Sstevel@tonic-gate 		 * This thread has a set but no context; it must be a CPU-bound
14080Sstevel@tonic-gate 		 * set. The hardware will be stopped via kcpc_unbind() when the
14090Sstevel@tonic-gate 		 * process exits and closes its file descriptors with
14100Sstevel@tonic-gate 		 * kcpc_close(). Our only job here is to clean up this thread's
14110Sstevel@tonic-gate 		 * state; the set will be freed with the unbind().
14120Sstevel@tonic-gate 		 */
14130Sstevel@tonic-gate 		(void) kcpc_unbind(set);
14140Sstevel@tonic-gate 		/*
14150Sstevel@tonic-gate 		 * Unbinding a set belonging to the current thread should clear
14160Sstevel@tonic-gate 		 * its set pointer.
14170Sstevel@tonic-gate 		 */
14180Sstevel@tonic-gate 		ASSERT(curthread->t_cpc_set == NULL);
14190Sstevel@tonic-gate 		return;
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	curthread->t_cpc_set = NULL;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	/*
14250Sstevel@tonic-gate 	 * This thread/LWP is exiting but context switches will continue to
14260Sstevel@tonic-gate 	 * happen for a bit as the exit proceeds.  Kernel preemption must be
14270Sstevel@tonic-gate 	 * disabled here to prevent a race between checking or setting the
14280Sstevel@tonic-gate 	 * INVALID_STOPPED flag here and kcpc_restore() setting the flag during
14290Sstevel@tonic-gate 	 * a context switch.
14300Sstevel@tonic-gate 	 */
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	kpreempt_disable();
14330Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
14340Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
14350Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
14360Sstevel@tonic-gate 		    KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 	kpreempt_enable();
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate /*
14420Sstevel@tonic-gate  * Assign the requests in the given set to the PICs in the context.
14430Sstevel@tonic-gate  * Returns 0 if successful, -1 on failure.
14440Sstevel@tonic-gate  */
14450Sstevel@tonic-gate /*ARGSUSED*/
14460Sstevel@tonic-gate static int
14470Sstevel@tonic-gate kcpc_assign_reqs(kcpc_set_t *set, kcpc_ctx_t *ctx)
14480Sstevel@tonic-gate {
14490Sstevel@tonic-gate 	int i;
14500Sstevel@tonic-gate 	int *picnum_save;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	ASSERT(set->ks_nreqs <= cpc_ncounters);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * Provide kcpc_tryassign() with scratch space to avoid doing an
14560Sstevel@tonic-gate 	 * alloc/free with every invocation.
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	picnum_save = kmem_alloc(set->ks_nreqs * sizeof (int), KM_SLEEP);
14590Sstevel@tonic-gate 	/*
14600Sstevel@tonic-gate 	 * kcpc_tryassign() blindly walks through each request in the set,
14610Sstevel@tonic-gate 	 * seeing if a counter can count its event. If yes, it assigns that
14620Sstevel@tonic-gate 	 * counter. However, that counter may have been the only capable counter
14630Sstevel@tonic-gate 	 * for _another_ request's event. The solution is to try every possible
14640Sstevel@tonic-gate 	 * request first. Note that this does not cover all solutions, as
14650Sstevel@tonic-gate 	 * that would require all unique orderings of requests, an n^n operation
14660Sstevel@tonic-gate 	 * which would be unacceptable for architectures with many counters.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
14690Sstevel@tonic-gate 		if (kcpc_tryassign(set, i, picnum_save) == 0)
14700Sstevel@tonic-gate 			break;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	kmem_free(picnum_save, set->ks_nreqs * sizeof (int));
14730Sstevel@tonic-gate 	if (i == set->ks_nreqs)
14740Sstevel@tonic-gate 		return (-1);
14750Sstevel@tonic-gate 	return (0);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate static int
14790Sstevel@tonic-gate kcpc_tryassign(kcpc_set_t *set, int starting_req, int *scratch)
14800Sstevel@tonic-gate {
14810Sstevel@tonic-gate 	int		i;
14820Sstevel@tonic-gate 	int		j;
14830Sstevel@tonic-gate 	uint64_t	bitmap = 0, resmap = 0;
14840Sstevel@tonic-gate 	uint64_t	ctrmap;
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	/*
14870Sstevel@tonic-gate 	 * We are attempting to assign the reqs to pics, but we may fail. If we
14880Sstevel@tonic-gate 	 * fail, we need to restore the state of the requests to what it was
14890Sstevel@tonic-gate 	 * when we found it, as some reqs may have been explicitly assigned to
14900Sstevel@tonic-gate 	 * a specific PIC beforehand. We do this by snapshotting the assignments
14910Sstevel@tonic-gate 	 * now and restoring from it later if we fail.
14920Sstevel@tonic-gate 	 *
14930Sstevel@tonic-gate 	 * Also we note here which counters have already been claimed by
14940Sstevel@tonic-gate 	 * requests with explicit counter assignments.
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
14970Sstevel@tonic-gate 		scratch[i] = set->ks_req[i].kr_picnum;
14980Sstevel@tonic-gate 		if (set->ks_req[i].kr_picnum != -1)
14990Sstevel@tonic-gate 			resmap |= (1 << set->ks_req[i].kr_picnum);
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * Walk through requests assigning them to the first PIC that is
15040Sstevel@tonic-gate 	 * capable.
15050Sstevel@tonic-gate 	 */
15060Sstevel@tonic-gate 	i = starting_req;
15070Sstevel@tonic-gate 	do {
15080Sstevel@tonic-gate 		if (set->ks_req[i].kr_picnum != -1) {
15090Sstevel@tonic-gate 			ASSERT((bitmap & (1 << set->ks_req[i].kr_picnum)) == 0);
15100Sstevel@tonic-gate 			bitmap |= (1 << set->ks_req[i].kr_picnum);
15110Sstevel@tonic-gate 			if (++i == set->ks_nreqs)
15120Sstevel@tonic-gate 				i = 0;
15130Sstevel@tonic-gate 			continue;
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 		ctrmap = pcbe_ops->pcbe_event_coverage(set->ks_req[i].kr_event);
15170Sstevel@tonic-gate 		for (j = 0; j < cpc_ncounters; j++) {
15180Sstevel@tonic-gate 			if (ctrmap & (1 << j) && (bitmap & (1 << j)) == 0 &&
15190Sstevel@tonic-gate 			    (resmap & (1 << j)) == 0) {
15200Sstevel@tonic-gate 				/*
15210Sstevel@tonic-gate 				 * We can assign this counter because:
15220Sstevel@tonic-gate 				 *
15230Sstevel@tonic-gate 				 * 1. It can count the event (ctrmap)
15240Sstevel@tonic-gate 				 * 2. It hasn't been assigned yet (bitmap)
15250Sstevel@tonic-gate 				 * 3. It wasn't reserved by a request (resmap)
15260Sstevel@tonic-gate 				 */
15270Sstevel@tonic-gate 				bitmap |= (1 << j);
15280Sstevel@tonic-gate 				break;
15290Sstevel@tonic-gate 			}
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 		if (j == cpc_ncounters) {
15320Sstevel@tonic-gate 			for (i = 0; i < set->ks_nreqs; i++)
15330Sstevel@tonic-gate 				set->ks_req[i].kr_picnum = scratch[i];
15340Sstevel@tonic-gate 			return (-1);
15350Sstevel@tonic-gate 		}
15360Sstevel@tonic-gate 		set->ks_req[i].kr_picnum = j;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		if (++i == set->ks_nreqs)
15390Sstevel@tonic-gate 			i = 0;
15400Sstevel@tonic-gate 	} while (i != starting_req);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	return (0);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate kcpc_set_t *
15460Sstevel@tonic-gate kcpc_dup_set(kcpc_set_t *set)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	kcpc_set_t	*new;
15490Sstevel@tonic-gate 	int		i;
15500Sstevel@tonic-gate 	int		j;
15510Sstevel@tonic-gate 
1552*6275Strevtom 	new = kmem_zalloc(sizeof (*new), KM_SLEEP);
1553*6275Strevtom 	new->ks_state &= ~KCPC_SET_BOUND;
15540Sstevel@tonic-gate 	new->ks_flags = set->ks_flags;
15550Sstevel@tonic-gate 	new->ks_nreqs = set->ks_nreqs;
15560Sstevel@tonic-gate 	new->ks_req = kmem_alloc(set->ks_nreqs * sizeof (kcpc_request_t),
15570Sstevel@tonic-gate 	    KM_SLEEP);
15580Sstevel@tonic-gate 	new->ks_data = NULL;
15590Sstevel@tonic-gate 	new->ks_ctx = NULL;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	for (i = 0; i < new->ks_nreqs; i++) {
15620Sstevel@tonic-gate 		new->ks_req[i].kr_config = NULL;
15630Sstevel@tonic-gate 		new->ks_req[i].kr_index = set->ks_req[i].kr_index;
15640Sstevel@tonic-gate 		new->ks_req[i].kr_picnum = set->ks_req[i].kr_picnum;
15650Sstevel@tonic-gate 		new->ks_req[i].kr_picp = NULL;
15660Sstevel@tonic-gate 		new->ks_req[i].kr_data = NULL;
15670Sstevel@tonic-gate 		(void) strncpy(new->ks_req[i].kr_event, set->ks_req[i].kr_event,
15680Sstevel@tonic-gate 		    CPC_MAX_EVENT_LEN);
15690Sstevel@tonic-gate 		new->ks_req[i].kr_preset = set->ks_req[i].kr_preset;
15700Sstevel@tonic-gate 		new->ks_req[i].kr_flags = set->ks_req[i].kr_flags;
15710Sstevel@tonic-gate 		new->ks_req[i].kr_nattrs = set->ks_req[i].kr_nattrs;
15720Sstevel@tonic-gate 		new->ks_req[i].kr_attr = kmem_alloc(new->ks_req[i].kr_nattrs *
15730Sstevel@tonic-gate 		    sizeof (kcpc_attr_t), KM_SLEEP);
15740Sstevel@tonic-gate 		for (j = 0; j < new->ks_req[i].kr_nattrs; j++) {
15750Sstevel@tonic-gate 			new->ks_req[i].kr_attr[j].ka_val =
15760Sstevel@tonic-gate 			    set->ks_req[i].kr_attr[j].ka_val;
15770Sstevel@tonic-gate 			(void) strncpy(new->ks_req[i].kr_attr[j].ka_name,
15780Sstevel@tonic-gate 			    set->ks_req[i].kr_attr[j].ka_name,
15790Sstevel@tonic-gate 			    CPC_MAX_ATTR_LEN);
15800Sstevel@tonic-gate 		}
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	return (new);
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate int
15870Sstevel@tonic-gate kcpc_allow_nonpriv(void *token)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	return (((kcpc_ctx_t *)token)->kc_flags & KCPC_CTX_NONPRIV);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate void
15930Sstevel@tonic-gate kcpc_invalidate(kthread_t *t)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate 	kcpc_ctx_t *ctx = t->t_cpc_ctx;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	if (ctx != NULL)
15980Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate /*
16020Sstevel@tonic-gate  * Given a PCBE ID, attempt to load a matching PCBE module. The strings given
16030Sstevel@tonic-gate  * are used to construct PCBE names, starting with the most specific,
16040Sstevel@tonic-gate  * "pcbe.first.second.third.fourth" and ending with the least specific,
16050Sstevel@tonic-gate  * "pcbe.first".
16060Sstevel@tonic-gate  *
16070Sstevel@tonic-gate  * Returns 0 if a PCBE was successfully loaded and -1 upon error.
16080Sstevel@tonic-gate  */
16090Sstevel@tonic-gate int
16100Sstevel@tonic-gate kcpc_pcbe_tryload(const char *prefix, uint_t first, uint_t second, uint_t third)
16110Sstevel@tonic-gate {
16121414Scindi 	uint_t s[3];
16130Sstevel@tonic-gate 
16141414Scindi 	s[0] = first;
16151414Scindi 	s[1] = second;
16161414Scindi 	s[2] = third;
16170Sstevel@tonic-gate 
16181414Scindi 	return (modload_qualified("pcbe",
16195254Sgavinm 	    "pcbe", prefix, ".", s, 3, NULL) < 0 ? -1 : 0);
16200Sstevel@tonic-gate }
1621