xref: /onnv-gate/usr/src/uts/common/os/kcpc.c (revision 5254:38162db71c7d)
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 /*
233732Sae112802  * Copyright 2007 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>
350Sstevel@tonic-gate #include <sys/mutex.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 
1690Sstevel@tonic-gate 	return (0);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate unbound:
1720Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1730Sstevel@tonic-gate 	set->ks_ctx = NULL;
1740Sstevel@tonic-gate 	kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
1750Sstevel@tonic-gate 	kcpc_ctx_free(ctx);
1760Sstevel@tonic-gate 	return (EAGAIN);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate int
1800Sstevel@tonic-gate kcpc_bind_thread(kcpc_set_t *set, kthread_t *t, int *subcode)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
1830Sstevel@tonic-gate 	int		error;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Only one set is allowed per context, so ensure there is no
1870Sstevel@tonic-gate 	 * existing context.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	if (t->t_cpc_ctx != NULL)
1910Sstevel@tonic-gate 		return (EEXIST);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	ctx = kcpc_ctx_alloc();
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * The context must begin life frozen until it has been properly
1970Sstevel@tonic-gate 	 * programmed onto the hardware. This prevents the context ops from
1980Sstevel@tonic-gate 	 * worrying about it until we're ready.
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	ctx->kc_flags |= KCPC_CTX_FREEZE;
2010Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime();
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (kcpc_assign_reqs(set, ctx) != 0) {
2040Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
2050Sstevel@tonic-gate 		*subcode = CPC_RESOURCE_UNAVAIL;
2060Sstevel@tonic-gate 		return (EINVAL);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	ctx->kc_cpuid = -1;
2100Sstevel@tonic-gate 	if (set->ks_flags & CPC_BIND_LWP_INHERIT)
2110Sstevel@tonic-gate 		ctx->kc_flags |= KCPC_CTX_LWPINHERIT;
2120Sstevel@tonic-gate 	ctx->kc_thread = t;
2130Sstevel@tonic-gate 	t->t_cpc_ctx = ctx;
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * Permit threads to look at their own hardware counters from userland.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	ctx->kc_flags |= KCPC_CTX_NONPRIV;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * Create the data store for this set.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	set->ks_data = kmem_alloc(set->ks_nreqs * sizeof (uint64_t), KM_SLEEP);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if ((error = kcpc_configure_reqs(ctx, set, subcode)) != 0) {
2250Sstevel@tonic-gate 		kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
2260Sstevel@tonic-gate 		kcpc_ctx_free(ctx);
2270Sstevel@tonic-gate 		t->t_cpc_ctx = NULL;
2280Sstevel@tonic-gate 		return (error);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	set->ks_ctx = ctx;
2320Sstevel@tonic-gate 	ctx->kc_set = set;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Add a device context to the subject thread.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	installctx(t, ctx, kcpc_save, kcpc_restore, NULL,
2380Sstevel@tonic-gate 	    kcpc_lwp_create, NULL, kcpc_free);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * Ask the backend to program the hardware.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if (t == curthread) {
2440Sstevel@tonic-gate 		kpreempt_disable();
2450Sstevel@tonic-gate 		ctx->kc_rawtick = KCPC_GET_TICK();
2460Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
2470Sstevel@tonic-gate 		pcbe_ops->pcbe_program(ctx);
2480Sstevel@tonic-gate 		kpreempt_enable();
2490Sstevel@tonic-gate 	} else
2500Sstevel@tonic-gate 		/*
2510Sstevel@tonic-gate 		 * Since we are the agent LWP, we know the victim LWP is stopped
2520Sstevel@tonic-gate 		 * until we're done here; no need to worry about preemption or
2530Sstevel@tonic-gate 		 * migration here. We still use an atomic op to clear the flag
2540Sstevel@tonic-gate 		 * to ensure the flags are always self-consistent; they can
2550Sstevel@tonic-gate 		 * still be accessed from, for instance, another CPU doing a
2560Sstevel@tonic-gate 		 * kcpc_invalidate_all().
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * Walk through each request in the set and ask the PCBE to configure a
2660Sstevel@tonic-gate  * corresponding counter.
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate static int
2690Sstevel@tonic-gate kcpc_configure_reqs(kcpc_ctx_t *ctx, kcpc_set_t *set, int *subcode)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	int		i;
2720Sstevel@tonic-gate 	int		ret;
2730Sstevel@tonic-gate 	kcpc_request_t	*rp;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
2760Sstevel@tonic-gate 		int n;
2770Sstevel@tonic-gate 		rp = &set->ks_req[i];
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		n = rp->kr_picnum;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		ASSERT(n >= 0 && n < cpc_ncounters);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		ASSERT(ctx->kc_pics[n].kp_req == NULL);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if (rp->kr_flags & CPC_OVF_NOTIFY_EMT) {
2860Sstevel@tonic-gate 			if ((pcbe_ops->pcbe_caps & CPC_CAP_OVERFLOW_INTERRUPT)
2870Sstevel@tonic-gate 			    == 0) {
2880Sstevel@tonic-gate 				*subcode = -1;
2890Sstevel@tonic-gate 				return (ENOTSUP);
2900Sstevel@tonic-gate 			}
2910Sstevel@tonic-gate 			/*
2920Sstevel@tonic-gate 			 * If any of the counters have requested overflow
2930Sstevel@tonic-gate 			 * notification, we flag the context as being one that
2940Sstevel@tonic-gate 			 * cares about overflow.
2950Sstevel@tonic-gate 			 */
2960Sstevel@tonic-gate 			ctx->kc_flags |= KCPC_CTX_SIGOVF;
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		rp->kr_config = NULL;
3000Sstevel@tonic-gate 		if ((ret = pcbe_ops->pcbe_configure(n, rp->kr_event,
3010Sstevel@tonic-gate 		    rp->kr_preset, rp->kr_flags, rp->kr_nattrs, rp->kr_attr,
3020Sstevel@tonic-gate 		    &(rp->kr_config), (void *)ctx)) != 0) {
3030Sstevel@tonic-gate 			kcpc_free_configs(set);
3040Sstevel@tonic-gate 			*subcode = ret;
3053732Sae112802 			switch (ret) {
3063732Sae112802 			case CPC_ATTR_REQUIRES_PRIVILEGE:
3073732Sae112802 			case CPC_HV_NO_ACCESS:
3080Sstevel@tonic-gate 				return (EACCES);
3093732Sae112802 			default:
3103732Sae112802 				return (EINVAL);
3113732Sae112802 			}
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		ctx->kc_pics[n].kp_req = rp;
3150Sstevel@tonic-gate 		rp->kr_picp = &ctx->kc_pics[n];
3160Sstevel@tonic-gate 		rp->kr_data = set->ks_data + rp->kr_index;
3170Sstevel@tonic-gate 		*rp->kr_data = rp->kr_preset;
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	return (0);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static void
3240Sstevel@tonic-gate kcpc_free_configs(kcpc_set_t *set)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	int i;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
3290Sstevel@tonic-gate 		if (set->ks_req[i].kr_config != NULL)
3300Sstevel@tonic-gate 			pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * buf points to a user address and the data should be copied out to that
3350Sstevel@tonic-gate  * address in the current process.
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate int
3380Sstevel@tonic-gate kcpc_sample(kcpc_set_t *set, uint64_t *buf, hrtime_t *hrtime, uint64_t *tick)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = set->ks_ctx;
3410Sstevel@tonic-gate 	uint64_t	curtick = KCPC_GET_TICK();
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (ctx == NULL)
3440Sstevel@tonic-gate 		return (EINVAL);
3450Sstevel@tonic-gate 	else if (ctx->kc_flags & KCPC_CTX_INVALID)
3460Sstevel@tonic-gate 		return (EAGAIN);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0) {
3490Sstevel@tonic-gate 		/*
3500Sstevel@tonic-gate 		 * Kernel preemption must be disabled while reading the
3510Sstevel@tonic-gate 		 * hardware regs, and if this is a CPU-bound context, while
3520Sstevel@tonic-gate 		 * checking the CPU binding of the current thread.
3530Sstevel@tonic-gate 		 */
3540Sstevel@tonic-gate 		kpreempt_disable();
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		if (ctx->kc_cpuid != -1) {
3570Sstevel@tonic-gate 			if (curthread->t_bind_cpu != ctx->kc_cpuid) {
3580Sstevel@tonic-gate 				kpreempt_enable();
3590Sstevel@tonic-gate 				return (EAGAIN);
3600Sstevel@tonic-gate 			}
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		if (ctx->kc_thread == curthread) {
3640Sstevel@tonic-gate 			ctx->kc_hrtime = gethrtime();
3650Sstevel@tonic-gate 			pcbe_ops->pcbe_sample(ctx);
3660Sstevel@tonic-gate 			ctx->kc_vtick += curtick - ctx->kc_rawtick;
3670Sstevel@tonic-gate 			ctx->kc_rawtick = curtick;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		kpreempt_enable();
3713732Sae112802 
3723732Sae112802 		/*
3733732Sae112802 		 * The config may have been invalidated by
3743732Sae112802 		 * the pcbe_sample op.
3753732Sae112802 		 */
3763732Sae112802 		if (ctx->kc_flags & KCPC_CTX_INVALID)
3773732Sae112802 			return (EAGAIN);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if (copyout(set->ks_data, buf,
3810Sstevel@tonic-gate 	    set->ks_nreqs * sizeof (uint64_t)) == -1)
3820Sstevel@tonic-gate 		return (EFAULT);
3830Sstevel@tonic-gate 	if (copyout(&ctx->kc_hrtime, hrtime, sizeof (uint64_t)) == -1)
3840Sstevel@tonic-gate 		return (EFAULT);
3850Sstevel@tonic-gate 	if (copyout(&ctx->kc_vtick, tick, sizeof (uint64_t)) == -1)
3860Sstevel@tonic-gate 		return (EFAULT);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Stop the counters on the CPU this context is bound to.
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate static void
3950Sstevel@tonic-gate kcpc_stop_hw(kcpc_ctx_t *ctx)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	cpu_t *cp;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	ASSERT((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED))
4000Sstevel@tonic-gate 	    == KCPC_CTX_INVALID);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	kpreempt_disable();
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	cp = cpu_get(ctx->kc_cpuid);
4050Sstevel@tonic-gate 	ASSERT(cp != NULL);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (cp == CPU) {
4080Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
4090Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
4100Sstevel@tonic-gate 		    KCPC_CTX_INVALID_STOPPED);
4110Sstevel@tonic-gate 	} else
4120Sstevel@tonic-gate 		kcpc_remote_stop(cp);
4130Sstevel@tonic-gate 	kpreempt_enable();
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate int
4170Sstevel@tonic-gate kcpc_unbind(kcpc_set_t *set)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = set->ks_ctx;
4200Sstevel@tonic-gate 	kthread_t	*t;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (ctx == NULL)
4230Sstevel@tonic-gate 		return (EINVAL);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (ctx->kc_cpuid == -1) {
4280Sstevel@tonic-gate 		t = ctx->kc_thread;
4290Sstevel@tonic-gate 		/*
4300Sstevel@tonic-gate 		 * The context is thread-bound and therefore has a device
4310Sstevel@tonic-gate 		 * context.  It will be freed via removectx() calling
4320Sstevel@tonic-gate 		 * freectx() calling kcpc_free().
4330Sstevel@tonic-gate 		 */
4340Sstevel@tonic-gate 		if (t == curthread &&
435*5254Sgavinm 		    (ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
4360Sstevel@tonic-gate 			kpreempt_disable();
4370Sstevel@tonic-gate 			pcbe_ops->pcbe_allstop();
4380Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags,
4390Sstevel@tonic-gate 			    KCPC_CTX_INVALID_STOPPED);
4400Sstevel@tonic-gate 			kpreempt_enable();
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate #ifdef DEBUG
4430Sstevel@tonic-gate 		if (removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
4440Sstevel@tonic-gate 		    kcpc_lwp_create, NULL, kcpc_free) == 0)
4450Sstevel@tonic-gate 			panic("kcpc_unbind: context %p not preset on thread %p",
4460Sstevel@tonic-gate 			    ctx, t);
4470Sstevel@tonic-gate #else
4480Sstevel@tonic-gate 		(void) removectx(t, ctx, kcpc_save, kcpc_restore, NULL,
4490Sstevel@tonic-gate 		    kcpc_lwp_create, NULL, kcpc_free);
4500Sstevel@tonic-gate #endif /* DEBUG */
4510Sstevel@tonic-gate 		t->t_cpc_set = NULL;
4520Sstevel@tonic-gate 		t->t_cpc_ctx = NULL;
4530Sstevel@tonic-gate 	} else {
4540Sstevel@tonic-gate 		/*
4550Sstevel@tonic-gate 		 * If we are unbinding a CPU-bound set from a remote CPU, the
4560Sstevel@tonic-gate 		 * native CPU's idle thread could be in the midst of programming
4570Sstevel@tonic-gate 		 * this context onto the CPU. We grab the context's lock here to
4580Sstevel@tonic-gate 		 * ensure that the idle thread is done with it. When we release
4590Sstevel@tonic-gate 		 * the lock, the CPU no longer has a context and the idle thread
4600Sstevel@tonic-gate 		 * will move on.
4610Sstevel@tonic-gate 		 *
4620Sstevel@tonic-gate 		 * cpu_lock must be held to prevent the CPU from being DR'd out
4630Sstevel@tonic-gate 		 * while we disassociate the context from the cpu_t.
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		cpu_t *cp;
4660Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
4670Sstevel@tonic-gate 		cp = cpu_get(ctx->kc_cpuid);
4680Sstevel@tonic-gate 		if (cp != NULL) {
4690Sstevel@tonic-gate 			/*
4700Sstevel@tonic-gate 			 * The CPU may have been DR'd out of the system.
4710Sstevel@tonic-gate 			 */
4720Sstevel@tonic-gate 			mutex_enter(&cp->cpu_cpc_ctxlock);
4730Sstevel@tonic-gate 			if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0)
4740Sstevel@tonic-gate 				kcpc_stop_hw(ctx);
4750Sstevel@tonic-gate 			ASSERT(ctx->kc_flags & KCPC_CTX_INVALID_STOPPED);
4760Sstevel@tonic-gate 			cp->cpu_cpc_ctx = NULL;
4770Sstevel@tonic-gate 			mutex_exit(&cp->cpu_cpc_ctxlock);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
4800Sstevel@tonic-gate 		if (ctx->kc_thread == curthread) {
4810Sstevel@tonic-gate 			kcpc_free(ctx, 0);
4820Sstevel@tonic-gate 			curthread->t_cpc_set = NULL;
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	return (0);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate int
4900Sstevel@tonic-gate kcpc_preset(kcpc_set_t *set, int index, uint64_t preset)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	int i;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	ASSERT(set != NULL);
4950Sstevel@tonic-gate 	ASSERT(set->ks_ctx != NULL);
4960Sstevel@tonic-gate 	ASSERT(set->ks_ctx->kc_thread == curthread);
4970Sstevel@tonic-gate 	ASSERT(set->ks_ctx->kc_cpuid == -1);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (index < 0 || index >= set->ks_nreqs)
5000Sstevel@tonic-gate 		return (EINVAL);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
5030Sstevel@tonic-gate 		if (set->ks_req[i].kr_index == index)
5040Sstevel@tonic-gate 			break;
5050Sstevel@tonic-gate 	ASSERT(i != set->ks_nreqs);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	set->ks_req[i].kr_preset = preset;
5080Sstevel@tonic-gate 	return (0);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate int
5120Sstevel@tonic-gate kcpc_restart(kcpc_set_t *set)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = set->ks_ctx;
5150Sstevel@tonic-gate 	int		i;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	ASSERT(ctx != NULL);
5180Sstevel@tonic-gate 	ASSERT(ctx->kc_thread == curthread);
5190Sstevel@tonic-gate 	ASSERT(ctx->kc_cpuid == -1);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	kpreempt_disable();
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * If the user is doing this on a running set, make sure the counters
5250Sstevel@tonic-gate 	 * are stopped first.
5260Sstevel@tonic-gate 	 */
5270Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
5280Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
5310Sstevel@tonic-gate 		*(set->ks_req[i].kr_data) = set->ks_req[i].kr_preset;
5320Sstevel@tonic-gate 		pcbe_ops->pcbe_configure(0, NULL, set->ks_req[i].kr_preset,
5330Sstevel@tonic-gate 		    0, 0, NULL, &set->ks_req[i].kr_config, NULL);
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * Ask the backend to program the hardware.
5380Sstevel@tonic-gate 	 */
5390Sstevel@tonic-gate 	ctx->kc_rawtick = KCPC_GET_TICK();
5400Sstevel@tonic-gate 	atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
5410Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
5420Sstevel@tonic-gate 	kpreempt_enable();
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	return (0);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * Caller must hold kcpc_cpuctx_lock.
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate int
5510Sstevel@tonic-gate kcpc_enable(kthread_t *t, int cmd, int enable)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = t->t_cpc_ctx;
5540Sstevel@tonic-gate 	kcpc_set_t	*set = t->t_cpc_set;
5550Sstevel@tonic-gate 	kcpc_set_t	*newset;
5560Sstevel@tonic-gate 	int		i;
5570Sstevel@tonic-gate 	int		flag;
5580Sstevel@tonic-gate 	int		err;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&kcpc_cpuctx_lock));
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (ctx == NULL) {
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * This thread has a set but no context; it must be a
5650Sstevel@tonic-gate 		 * CPU-bound set.
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 		ASSERT(t->t_cpc_set != NULL);
5680Sstevel@tonic-gate 		ASSERT(t->t_cpc_set->ks_ctx->kc_cpuid != -1);
5690Sstevel@tonic-gate 		return (EINVAL);
5700Sstevel@tonic-gate 	} else if (ctx->kc_flags & KCPC_CTX_INVALID)
5710Sstevel@tonic-gate 		return (EAGAIN);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (cmd == CPC_ENABLE) {
5740Sstevel@tonic-gate 		if ((ctx->kc_flags & KCPC_CTX_FREEZE) == 0)
5750Sstevel@tonic-gate 			return (EINVAL);
5760Sstevel@tonic-gate 		kpreempt_disable();
5770Sstevel@tonic-gate 		atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
5780Sstevel@tonic-gate 		kcpc_restore(ctx);
5790Sstevel@tonic-gate 		kpreempt_enable();
5800Sstevel@tonic-gate 	} else if (cmd == CPC_DISABLE) {
5810Sstevel@tonic-gate 		if (ctx->kc_flags & KCPC_CTX_FREEZE)
5820Sstevel@tonic-gate 			return (EINVAL);
5830Sstevel@tonic-gate 		kpreempt_disable();
5840Sstevel@tonic-gate 		kcpc_save(ctx);
5850Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
5860Sstevel@tonic-gate 		kpreempt_enable();
5870Sstevel@tonic-gate 	} else if (cmd == CPC_USR_EVENTS || cmd == CPC_SYS_EVENTS) {
5880Sstevel@tonic-gate 		/*
5890Sstevel@tonic-gate 		 * Strategy for usr/sys: stop counters and update set's presets
5900Sstevel@tonic-gate 		 * with current counter values, unbind, update requests with
5910Sstevel@tonic-gate 		 * new config, then re-bind.
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate 		flag = (cmd == CPC_USR_EVENTS) ?
5940Sstevel@tonic-gate 		    CPC_COUNT_USER: CPC_COUNT_SYSTEM;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		kpreempt_disable();
5970Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
5980Sstevel@tonic-gate 		    KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
5990Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
6000Sstevel@tonic-gate 		kpreempt_enable();
6010Sstevel@tonic-gate 		for (i = 0; i < set->ks_nreqs; i++) {
6020Sstevel@tonic-gate 			set->ks_req[i].kr_preset = *(set->ks_req[i].kr_data);
6030Sstevel@tonic-gate 			if (enable)
6040Sstevel@tonic-gate 				set->ks_req[i].kr_flags |= flag;
6050Sstevel@tonic-gate 			else
6060Sstevel@tonic-gate 				set->ks_req[i].kr_flags &= ~flag;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		newset = kcpc_dup_set(set);
6090Sstevel@tonic-gate 		if (kcpc_unbind(set) != 0)
6100Sstevel@tonic-gate 			return (EINVAL);
6110Sstevel@tonic-gate 		t->t_cpc_set = newset;
6120Sstevel@tonic-gate 		if (kcpc_bind_thread(newset, t, &err) != 0) {
6130Sstevel@tonic-gate 			t->t_cpc_set = NULL;
6140Sstevel@tonic-gate 			kcpc_free_set(newset);
6150Sstevel@tonic-gate 			return (EINVAL);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 	} else
6180Sstevel@tonic-gate 		return (EINVAL);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Provide PCBEs with a way of obtaining the configs of every counter which will
6250Sstevel@tonic-gate  * be programmed together.
6260Sstevel@tonic-gate  *
6270Sstevel@tonic-gate  * If current is NULL, provide the first config.
6280Sstevel@tonic-gate  *
6290Sstevel@tonic-gate  * If data != NULL, caller wants to know where the data store associated with
6300Sstevel@tonic-gate  * the config we return is located.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate void *
6330Sstevel@tonic-gate kcpc_next_config(void *token, void *current, uint64_t **data)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	int		i;
6360Sstevel@tonic-gate 	kcpc_pic_t	*pic;
6370Sstevel@tonic-gate 	kcpc_ctx_t *ctx = (kcpc_ctx_t *)token;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (current == NULL) {
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * Client would like the first config, which may not be in
6420Sstevel@tonic-gate 		 * counter 0; we need to search through the counters for the
6430Sstevel@tonic-gate 		 * first config.
6440Sstevel@tonic-gate 		 */
6450Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++)
6460Sstevel@tonic-gate 			if (ctx->kc_pics[i].kp_req != NULL)
6470Sstevel@tonic-gate 				break;
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * There are no counters configured for the given context.
6500Sstevel@tonic-gate 		 */
6510Sstevel@tonic-gate 		if (i == cpc_ncounters)
6520Sstevel@tonic-gate 			return (NULL);
6530Sstevel@tonic-gate 	} else {
6540Sstevel@tonic-gate 		/*
6550Sstevel@tonic-gate 		 * There surely is a faster way to do this.
6560Sstevel@tonic-gate 		 */
6570Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++) {
6580Sstevel@tonic-gate 			pic = &ctx->kc_pics[i];
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 			if (pic->kp_req != NULL &&
6610Sstevel@tonic-gate 			    current == pic->kp_req->kr_config)
6620Sstevel@tonic-gate 				break;
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		/*
6660Sstevel@tonic-gate 		 * We found the current config at picnum i. Now search for the
6670Sstevel@tonic-gate 		 * next configured PIC.
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate 		for (i++; i < cpc_ncounters; i++) {
6700Sstevel@tonic-gate 			pic = &ctx->kc_pics[i];
6710Sstevel@tonic-gate 			if (pic->kp_req != NULL)
6720Sstevel@tonic-gate 				break;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		if (i == cpc_ncounters)
6760Sstevel@tonic-gate 			return (NULL);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	if (data != NULL) {
6800Sstevel@tonic-gate 		*data = ctx->kc_pics[i].kp_req->kr_data;
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	return (ctx->kc_pics[i].kp_req->kr_config);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate static kcpc_ctx_t *
6880Sstevel@tonic-gate kcpc_ctx_alloc(void)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
6910Sstevel@tonic-gate 	long		hash;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	ctx = (kcpc_ctx_t *)kmem_alloc(sizeof (kcpc_ctx_t), KM_SLEEP);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	hash = CPC_HASH_CTX(ctx);
6960Sstevel@tonic-gate 	mutex_enter(&kcpc_ctx_llock[hash]);
6970Sstevel@tonic-gate 	ctx->kc_next = kcpc_ctx_list[hash];
6980Sstevel@tonic-gate 	kcpc_ctx_list[hash] = ctx;
6990Sstevel@tonic-gate 	mutex_exit(&kcpc_ctx_llock[hash]);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	ctx->kc_pics = (kcpc_pic_t *)kmem_zalloc(sizeof (kcpc_pic_t) *
7020Sstevel@tonic-gate 	    cpc_ncounters, KM_SLEEP);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	ctx->kc_flags = 0;
7050Sstevel@tonic-gate 	ctx->kc_vtick = 0;
7060Sstevel@tonic-gate 	ctx->kc_rawtick = 0;
7070Sstevel@tonic-gate 	ctx->kc_cpuid = -1;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	return (ctx);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate /*
7130Sstevel@tonic-gate  * Copy set from ctx to the child context, cctx, if it has CPC_BIND_LWP_INHERIT
7140Sstevel@tonic-gate  * in the flags.
7150Sstevel@tonic-gate  */
7160Sstevel@tonic-gate static void
7170Sstevel@tonic-gate kcpc_ctx_clone(kcpc_ctx_t *ctx, kcpc_ctx_t *cctx)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	kcpc_set_t	*ks = ctx->kc_set, *cks;
7200Sstevel@tonic-gate 	int		i, j;
7210Sstevel@tonic-gate 	int		code;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	ASSERT(ks != NULL);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	if ((ks->ks_flags & CPC_BIND_LWP_INHERIT) == 0)
7260Sstevel@tonic-gate 		return;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	cks = kmem_alloc(sizeof (*cks), KM_SLEEP);
7290Sstevel@tonic-gate 	cctx->kc_set = cks;
7300Sstevel@tonic-gate 	cks->ks_flags = ks->ks_flags;
7310Sstevel@tonic-gate 	cks->ks_nreqs = ks->ks_nreqs;
7320Sstevel@tonic-gate 	cks->ks_req = kmem_alloc(cks->ks_nreqs *
7330Sstevel@tonic-gate 	    sizeof (kcpc_request_t), KM_SLEEP);
7340Sstevel@tonic-gate 	cks->ks_data = kmem_alloc(cks->ks_nreqs * sizeof (uint64_t),
7350Sstevel@tonic-gate 	    KM_SLEEP);
7360Sstevel@tonic-gate 	cks->ks_ctx = cctx;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	for (i = 0; i < cks->ks_nreqs; i++) {
7390Sstevel@tonic-gate 		cks->ks_req[i].kr_index = ks->ks_req[i].kr_index;
7400Sstevel@tonic-gate 		cks->ks_req[i].kr_picnum = ks->ks_req[i].kr_picnum;
7410Sstevel@tonic-gate 		(void) strncpy(cks->ks_req[i].kr_event,
7420Sstevel@tonic-gate 		    ks->ks_req[i].kr_event, CPC_MAX_EVENT_LEN);
7430Sstevel@tonic-gate 		cks->ks_req[i].kr_preset = ks->ks_req[i].kr_preset;
7440Sstevel@tonic-gate 		cks->ks_req[i].kr_flags = ks->ks_req[i].kr_flags;
7450Sstevel@tonic-gate 		cks->ks_req[i].kr_nattrs = ks->ks_req[i].kr_nattrs;
7460Sstevel@tonic-gate 		if (ks->ks_req[i].kr_nattrs > 0) {
7470Sstevel@tonic-gate 			cks->ks_req[i].kr_attr =
7480Sstevel@tonic-gate 			    kmem_alloc(ks->ks_req[i].kr_nattrs *
749*5254Sgavinm 			    sizeof (kcpc_attr_t), KM_SLEEP);
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		for (j = 0; j < ks->ks_req[i].kr_nattrs; j++) {
7520Sstevel@tonic-gate 			(void) strncpy(cks->ks_req[i].kr_attr[j].ka_name,
7530Sstevel@tonic-gate 			    ks->ks_req[i].kr_attr[j].ka_name,
7540Sstevel@tonic-gate 			    CPC_MAX_ATTR_LEN);
7550Sstevel@tonic-gate 			cks->ks_req[i].kr_attr[j].ka_val =
7560Sstevel@tonic-gate 			    ks->ks_req[i].kr_attr[j].ka_val;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	if (kcpc_configure_reqs(cctx, cks, &code) != 0)
7603732Sae112802 		kcpc_invalidate_config(cctx);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate static void
7650Sstevel@tonic-gate kcpc_ctx_free(kcpc_ctx_t *ctx)
7660Sstevel@tonic-gate {
7670Sstevel@tonic-gate 	kcpc_ctx_t	**loc;
7680Sstevel@tonic-gate 	long		hash = CPC_HASH_CTX(ctx);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	mutex_enter(&kcpc_ctx_llock[hash]);
7710Sstevel@tonic-gate 	loc = &kcpc_ctx_list[hash];
7720Sstevel@tonic-gate 	ASSERT(*loc != NULL);
7730Sstevel@tonic-gate 	while (*loc != ctx)
7740Sstevel@tonic-gate 		loc = &(*loc)->kc_next;
7750Sstevel@tonic-gate 	*loc = ctx->kc_next;
7760Sstevel@tonic-gate 	mutex_exit(&kcpc_ctx_llock[hash]);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	kmem_free(ctx->kc_pics, cpc_ncounters * sizeof (kcpc_pic_t));
7790Sstevel@tonic-gate 	kmem_free(ctx, sizeof (*ctx));
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * Generic interrupt handler used on hardware that generates
7840Sstevel@tonic-gate  * overflow interrupts.
7850Sstevel@tonic-gate  *
7860Sstevel@tonic-gate  * Note: executed at high-level interrupt context!
7870Sstevel@tonic-gate  */
7880Sstevel@tonic-gate /*ARGSUSED*/
7890Sstevel@tonic-gate kcpc_ctx_t *
7900Sstevel@tonic-gate kcpc_overflow_intr(caddr_t arg, uint64_t bitmap)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
7930Sstevel@tonic-gate 	kthread_t	*t = curthread;
7940Sstevel@tonic-gate 	int		i;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * On both x86 and UltraSPARC, we may deliver the high-level
7980Sstevel@tonic-gate 	 * interrupt in kernel mode, just after we've started to run an
7990Sstevel@tonic-gate 	 * interrupt thread.  (That's because the hardware helpfully
8000Sstevel@tonic-gate 	 * delivers the overflow interrupt some random number of cycles
8010Sstevel@tonic-gate 	 * after the instruction that caused the overflow by which time
8020Sstevel@tonic-gate 	 * we're in some part of the kernel, not necessarily running on
8030Sstevel@tonic-gate 	 * the right thread).
8040Sstevel@tonic-gate 	 *
8050Sstevel@tonic-gate 	 * Check for this case here -- find the pinned thread
8060Sstevel@tonic-gate 	 * that was running when the interrupt went off.
8070Sstevel@tonic-gate 	 */
8080Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD) {
8090Sstevel@tonic-gate 		klwp_t *lwp;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 		atomic_add_32(&kcpc_intrctx_count, 1);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * Note that t_lwp is always set to point at the underlying
8150Sstevel@tonic-gate 		 * thread, thus this will work in the presence of nested
8160Sstevel@tonic-gate 		 * interrupts.
8170Sstevel@tonic-gate 		 */
8180Sstevel@tonic-gate 		ctx = NULL;
8190Sstevel@tonic-gate 		if ((lwp = t->t_lwp) != NULL) {
8200Sstevel@tonic-gate 			t = lwptot(lwp);
8210Sstevel@tonic-gate 			ctx = t->t_cpc_ctx;
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 	} else
8240Sstevel@tonic-gate 		ctx = t->t_cpc_ctx;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (ctx == NULL) {
8270Sstevel@tonic-gate 		/*
8280Sstevel@tonic-gate 		 * This can easily happen if we're using the counters in
8290Sstevel@tonic-gate 		 * "shared" mode, for example, and an overflow interrupt
8300Sstevel@tonic-gate 		 * occurs while we are running cpustat.  In that case, the
8310Sstevel@tonic-gate 		 * bound thread that has the context that belongs to this
8320Sstevel@tonic-gate 		 * CPU is almost certainly sleeping (if it was running on
8330Sstevel@tonic-gate 		 * the CPU we'd have found it above), and the actual
8340Sstevel@tonic-gate 		 * interrupted thread has no knowledge of performance counters!
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate 		ctx = curthread->t_cpu->cpu_cpc_ctx;
8370Sstevel@tonic-gate 		if (ctx != NULL) {
8380Sstevel@tonic-gate 			/*
8390Sstevel@tonic-gate 			 * Return the bound context for this CPU to
8400Sstevel@tonic-gate 			 * the interrupt handler so that it can synchronously
8410Sstevel@tonic-gate 			 * sample the hardware counters and restart them.
8420Sstevel@tonic-gate 			 */
8430Sstevel@tonic-gate 			return (ctx);
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * As long as the overflow interrupt really is delivered early
8480Sstevel@tonic-gate 		 * enough after trapping into the kernel to avoid switching
8490Sstevel@tonic-gate 		 * threads, we must always be able to find the cpc context,
8500Sstevel@tonic-gate 		 * or something went terribly wrong i.e. we ended up
8510Sstevel@tonic-gate 		 * running a passivated interrupt thread, a kernel
8520Sstevel@tonic-gate 		 * thread or we interrupted idle, all of which are Very Bad.
8530Sstevel@tonic-gate 		 */
8540Sstevel@tonic-gate 		if (kcpc_nullctx_panic)
8550Sstevel@tonic-gate 			panic("null cpc context, thread %p", (void *)t);
8560Sstevel@tonic-gate 		atomic_add_32(&kcpc_nullctx_count, 1);
8570Sstevel@tonic-gate 	} else if ((ctx->kc_flags & KCPC_CTX_INVALID) == 0) {
8580Sstevel@tonic-gate 		/*
8590Sstevel@tonic-gate 		 * Schedule an ast to sample the counters, which will
8600Sstevel@tonic-gate 		 * propagate any overflow into the virtualized performance
8610Sstevel@tonic-gate 		 * counter(s), and may deliver a signal.
8620Sstevel@tonic-gate 		 */
8630Sstevel@tonic-gate 		ttolwp(t)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
8640Sstevel@tonic-gate 		/*
8650Sstevel@tonic-gate 		 * If a counter has overflowed which was counting on behalf of
8660Sstevel@tonic-gate 		 * a request which specified CPC_OVF_NOTIFY_EMT, send the
8670Sstevel@tonic-gate 		 * process a signal.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		for (i = 0; i < cpc_ncounters; i++) {
8700Sstevel@tonic-gate 			if (ctx->kc_pics[i].kp_req != NULL &&
8710Sstevel@tonic-gate 			    bitmap & (1 << i) &&
8720Sstevel@tonic-gate 			    ctx->kc_pics[i].kp_req->kr_flags &
8730Sstevel@tonic-gate 			    CPC_OVF_NOTIFY_EMT) {
8740Sstevel@tonic-gate 				/*
8750Sstevel@tonic-gate 				 * A signal has been requested for this PIC, so
8760Sstevel@tonic-gate 				 * so freeze the context. The interrupt handler
8770Sstevel@tonic-gate 				 * has already stopped the counter hardware.
8780Sstevel@tonic-gate 				 */
8790Sstevel@tonic-gate 				atomic_or_uint(&ctx->kc_flags, KCPC_CTX_FREEZE);
8800Sstevel@tonic-gate 				atomic_or_uint(&ctx->kc_pics[i].kp_flags,
8810Sstevel@tonic-gate 				    KCPC_PIC_OVERFLOWED);
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 		aston(t);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	return (NULL);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate  * The current thread context had an overflow interrupt; we're
8910Sstevel@tonic-gate  * executing here in high-level interrupt context.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate /*ARGSUSED*/
8940Sstevel@tonic-gate uint_t
8950Sstevel@tonic-gate kcpc_hw_overflow_intr(caddr_t arg1, caddr_t arg2)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	kcpc_ctx_t	*ctx;
8980Sstevel@tonic-gate 	uint64_t	bitmap;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if (pcbe_ops == NULL ||
9010Sstevel@tonic-gate 	    (bitmap = pcbe_ops->pcbe_overflow_bitmap()) == 0)
9020Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
9033884Sha137994 
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Prevent any further interrupts.
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/*
9100Sstevel@tonic-gate 	 * Invoke the "generic" handler.
9110Sstevel@tonic-gate 	 *
9120Sstevel@tonic-gate 	 * If the interrupt has occurred in the context of an lwp owning
9130Sstevel@tonic-gate 	 * the counters, then the handler posts an AST to the lwp to
9140Sstevel@tonic-gate 	 * trigger the actual sampling, and optionally deliver a signal or
9150Sstevel@tonic-gate 	 * restart the counters, on the way out of the kernel using
9160Sstevel@tonic-gate 	 * kcpc_hw_overflow_ast() (see below).
9170Sstevel@tonic-gate 	 *
9180Sstevel@tonic-gate 	 * On the other hand, if the handler returns the context to us
9190Sstevel@tonic-gate 	 * directly, then it means that there are no other threads in
9200Sstevel@tonic-gate 	 * the middle of updating it, no AST has been posted, and so we
9210Sstevel@tonic-gate 	 * should sample the counters here, and restart them with no
9220Sstevel@tonic-gate 	 * further fuss.
9230Sstevel@tonic-gate 	 */
9240Sstevel@tonic-gate 	if ((ctx = kcpc_overflow_intr(arg1, bitmap)) != NULL) {
9250Sstevel@tonic-gate 		uint64_t curtick = KCPC_GET_TICK();
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		ctx->kc_hrtime = gethrtime_waitfree();
9280Sstevel@tonic-gate 		ctx->kc_vtick += curtick - ctx->kc_rawtick;
9290Sstevel@tonic-gate 		ctx->kc_rawtick = curtick;
9300Sstevel@tonic-gate 		pcbe_ops->pcbe_sample(ctx);
9310Sstevel@tonic-gate 		pcbe_ops->pcbe_program(ctx);
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * Called from trap() when processing the ast posted by the high-level
9390Sstevel@tonic-gate  * interrupt handler.
9400Sstevel@tonic-gate  */
9410Sstevel@tonic-gate int
9420Sstevel@tonic-gate kcpc_overflow_ast()
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = curthread->t_cpc_ctx;
9450Sstevel@tonic-gate 	int		i;
9460Sstevel@tonic-gate 	int		found = 0;
9470Sstevel@tonic-gate 	uint64_t	curtick = KCPC_GET_TICK();
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	ASSERT(ctx != NULL);	/* Beware of interrupt skid. */
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/*
9520Sstevel@tonic-gate 	 * An overflow happened: sample the context to ensure that
9530Sstevel@tonic-gate 	 * the overflow is propagated into the upper bits of the
9540Sstevel@tonic-gate 	 * virtualized 64-bit counter(s).
9550Sstevel@tonic-gate 	 */
9560Sstevel@tonic-gate 	kpreempt_disable();
9570Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime_waitfree();
9580Sstevel@tonic-gate 	pcbe_ops->pcbe_sample(ctx);
9590Sstevel@tonic-gate 	kpreempt_enable();
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	ctx->kc_vtick += curtick - ctx->kc_rawtick;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * The interrupt handler has marked any pics with KCPC_PIC_OVERFLOWED
9650Sstevel@tonic-gate 	 * if that pic generated an overflow and if the request it was counting
9660Sstevel@tonic-gate 	 * on behalf of had CPC_OVERFLOW_REQUEST specified. We go through all
9670Sstevel@tonic-gate 	 * pics in the context and clear the KCPC_PIC_OVERFLOWED flags. If we
9680Sstevel@tonic-gate 	 * found any overflowed pics, keep the context frozen and return true
9690Sstevel@tonic-gate 	 * (thus causing a signal to be sent).
9700Sstevel@tonic-gate 	 */
9710Sstevel@tonic-gate 	for (i = 0; i < cpc_ncounters; i++) {
9720Sstevel@tonic-gate 		if (ctx->kc_pics[i].kp_flags & KCPC_PIC_OVERFLOWED) {
9730Sstevel@tonic-gate 			atomic_and_uint(&ctx->kc_pics[i].kp_flags,
9740Sstevel@tonic-gate 			    ~KCPC_PIC_OVERFLOWED);
9750Sstevel@tonic-gate 			found = 1;
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	if (found)
9790Sstevel@tonic-gate 		return (1);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/*
9820Sstevel@tonic-gate 	 * Otherwise, re-enable the counters and continue life as before.
9830Sstevel@tonic-gate 	 */
9840Sstevel@tonic-gate 	kpreempt_disable();
9850Sstevel@tonic-gate 	atomic_and_uint(&ctx->kc_flags, ~KCPC_CTX_FREEZE);
9860Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
9870Sstevel@tonic-gate 	kpreempt_enable();
9880Sstevel@tonic-gate 	return (0);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * Called when switching away from current thread.
9930Sstevel@tonic-gate  */
9940Sstevel@tonic-gate static void
9950Sstevel@tonic-gate kcpc_save(kcpc_ctx_t *ctx)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_INVALID) {
9980Sstevel@tonic-gate 		if (ctx->kc_flags & KCPC_CTX_INVALID_STOPPED)
9990Sstevel@tonic-gate 			return;
10000Sstevel@tonic-gate 		/*
10010Sstevel@tonic-gate 		 * This context has been invalidated but the counters have not
10020Sstevel@tonic-gate 		 * been stopped. Stop them here and mark the context stopped.
10030Sstevel@tonic-gate 		 */
10040Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
10050Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID_STOPPED);
10060Sstevel@tonic-gate 		return;
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
10100Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_FREEZE)
10110Sstevel@tonic-gate 		return;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/*
10140Sstevel@tonic-gate 	 * Need to sample for all reqs into each req's current mpic.
10150Sstevel@tonic-gate 	 */
10160Sstevel@tonic-gate 	ctx->kc_hrtime = gethrtime();
10170Sstevel@tonic-gate 	ctx->kc_vtick += KCPC_GET_TICK() - ctx->kc_rawtick;
10180Sstevel@tonic-gate 	pcbe_ops->pcbe_sample(ctx);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate static void
10220Sstevel@tonic-gate kcpc_restore(kcpc_ctx_t *ctx)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	if ((ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED)) ==
10250Sstevel@tonic-gate 	    KCPC_CTX_INVALID)
10260Sstevel@tonic-gate 		/*
10270Sstevel@tonic-gate 		 * The context is invalidated but has not been marked stopped.
10280Sstevel@tonic-gate 		 * We mark it as such here because we will not start the
10290Sstevel@tonic-gate 		 * counters during this context switch.
10300Sstevel@tonic-gate 		 */
10310Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID_STOPPED);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if (ctx->kc_flags & (KCPC_CTX_INVALID | KCPC_CTX_FREEZE))
10350Sstevel@tonic-gate 		return;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/*
10380Sstevel@tonic-gate 	 * While programming the hardware, the counters should be stopped. We
10390Sstevel@tonic-gate 	 * don't do an explicit pcbe_allstop() here because they should have
10400Sstevel@tonic-gate 	 * been stopped already by the last consumer.
10410Sstevel@tonic-gate 	 */
10420Sstevel@tonic-gate 	ctx->kc_rawtick = KCPC_GET_TICK();
10430Sstevel@tonic-gate 	pcbe_ops->pcbe_program(ctx);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate /*
10470Sstevel@tonic-gate  * If kcpc_counts_include_idle is set to 0 by the sys admin, we add the the
10480Sstevel@tonic-gate  * following context operators to the idle thread on each CPU. They stop the
10490Sstevel@tonic-gate  * counters when the idle thread is switched on, and they start them again when
10500Sstevel@tonic-gate  * it is switched off.
10510Sstevel@tonic-gate  */
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate /*ARGSUSED*/
10540Sstevel@tonic-gate void
10550Sstevel@tonic-gate kcpc_idle_save(struct cpu *cp)
10560Sstevel@tonic-gate {
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * The idle thread shouldn't be run anywhere else.
10590Sstevel@tonic-gate 	 */
10600Sstevel@tonic-gate 	ASSERT(CPU == cp);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/*
10630Sstevel@tonic-gate 	 * We must hold the CPU's context lock to ensure the context isn't freed
10640Sstevel@tonic-gate 	 * while we're looking at it.
10650Sstevel@tonic-gate 	 */
10660Sstevel@tonic-gate 	mutex_enter(&cp->cpu_cpc_ctxlock);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if ((cp->cpu_cpc_ctx == NULL) ||
10690Sstevel@tonic-gate 	    (cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
10700Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
10710Sstevel@tonic-gate 		return;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	pcbe_ops->pcbe_program(cp->cpu_cpc_ctx);
10750Sstevel@tonic-gate 	mutex_exit(&cp->cpu_cpc_ctxlock);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate void
10790Sstevel@tonic-gate kcpc_idle_restore(struct cpu *cp)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * The idle thread shouldn't be run anywhere else.
10830Sstevel@tonic-gate 	 */
10840Sstevel@tonic-gate 	ASSERT(CPU == cp);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * We must hold the CPU's context lock to ensure the context isn't freed
10880Sstevel@tonic-gate 	 * while we're looking at it.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 	mutex_enter(&cp->cpu_cpc_ctxlock);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	if ((cp->cpu_cpc_ctx == NULL) ||
10930Sstevel@tonic-gate 	    (cp->cpu_cpc_ctx->kc_flags & KCPC_CTX_INVALID)) {
10940Sstevel@tonic-gate 		mutex_exit(&cp->cpu_cpc_ctxlock);
10950Sstevel@tonic-gate 		return;
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	pcbe_ops->pcbe_allstop();
10990Sstevel@tonic-gate 	mutex_exit(&cp->cpu_cpc_ctxlock);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*ARGSUSED*/
11030Sstevel@tonic-gate static void
11040Sstevel@tonic-gate kcpc_lwp_create(kthread_t *t, kthread_t *ct)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	kcpc_ctx_t	*ctx = t->t_cpc_ctx, *cctx;
11070Sstevel@tonic-gate 	int		i;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	if (ctx == NULL || (ctx->kc_flags & KCPC_CTX_LWPINHERIT) == 0)
11100Sstevel@tonic-gate 		return;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	rw_enter(&kcpc_cpuctx_lock, RW_READER);
11130Sstevel@tonic-gate 	if (ctx->kc_flags & KCPC_CTX_INVALID) {
11140Sstevel@tonic-gate 		rw_exit(&kcpc_cpuctx_lock);
11150Sstevel@tonic-gate 		return;
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	cctx = kcpc_ctx_alloc();
11180Sstevel@tonic-gate 	kcpc_ctx_clone(ctx, cctx);
11190Sstevel@tonic-gate 	rw_exit(&kcpc_cpuctx_lock);
11200Sstevel@tonic-gate 
11213732Sae112802 	/*
11223732Sae112802 	 * Copy the parent context's kc_flags field, but don't overwrite
11233732Sae112802 	 * the child's in case it was modified during kcpc_ctx_clone.
11243732Sae112802 	 */
11253732Sae112802 	cctx->kc_flags |= ctx->kc_flags;
11260Sstevel@tonic-gate 	cctx->kc_thread = ct;
11270Sstevel@tonic-gate 	cctx->kc_cpuid = -1;
11280Sstevel@tonic-gate 	ct->t_cpc_set = cctx->kc_set;
11290Sstevel@tonic-gate 	ct->t_cpc_ctx = cctx;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (cctx->kc_flags & KCPC_CTX_SIGOVF) {
11320Sstevel@tonic-gate 		kcpc_set_t *ks = cctx->kc_set;
11330Sstevel@tonic-gate 		/*
11340Sstevel@tonic-gate 		 * Our contract with the user requires us to immediately send an
11350Sstevel@tonic-gate 		 * overflow signal to all children if we have the LWPINHERIT
11360Sstevel@tonic-gate 		 * and SIGOVF flags set. In addition, all counters should be
11370Sstevel@tonic-gate 		 * set to UINT64_MAX, and their pic's overflow flag turned on
11380Sstevel@tonic-gate 		 * so that our trap() processing knows to send a signal.
11390Sstevel@tonic-gate 		 */
11400Sstevel@tonic-gate 		atomic_or_uint(&cctx->kc_flags, KCPC_CTX_FREEZE);
11410Sstevel@tonic-gate 		for (i = 0; i < ks->ks_nreqs; i++) {
11420Sstevel@tonic-gate 			kcpc_request_t *kr = &ks->ks_req[i];
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 			if (kr->kr_flags & CPC_OVF_NOTIFY_EMT) {
11450Sstevel@tonic-gate 				*(kr->kr_data) = UINT64_MAX;
11460Sstevel@tonic-gate 				kr->kr_picp->kp_flags |= KCPC_PIC_OVERFLOWED;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 		ttolwp(ct)->lwp_pcb.pcb_flags |= CPC_OVERFLOW;
11500Sstevel@tonic-gate 		aston(ct);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	installctx(ct, cctx, kcpc_save, kcpc_restore,
11540Sstevel@tonic-gate 	    NULL, kcpc_lwp_create, NULL, kcpc_free);
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate /*
11580Sstevel@tonic-gate  * Counter Stoppage Theory
11590Sstevel@tonic-gate  *
11600Sstevel@tonic-gate  * The counters may need to be stopped properly at the following occasions:
11610Sstevel@tonic-gate  *
11620Sstevel@tonic-gate  * 1) An LWP exits.
11630Sstevel@tonic-gate  * 2) A thread exits.
11640Sstevel@tonic-gate  * 3) An LWP performs an exec().
11650Sstevel@tonic-gate  * 4) A bound set is unbound.
11660Sstevel@tonic-gate  *
11670Sstevel@tonic-gate  * In addition to stopping the counters, the CPC context (a kcpc_ctx_t) may need
11680Sstevel@tonic-gate  * to be freed as well.
11690Sstevel@tonic-gate  *
11700Sstevel@tonic-gate  * Case 1: kcpc_passivate(), called via lwp_exit(), stops the counters. Later on
11710Sstevel@tonic-gate  * when the thread is freed, kcpc_free(), called by freectx(), frees the
11720Sstevel@tonic-gate  * context.
11730Sstevel@tonic-gate  *
11740Sstevel@tonic-gate  * Case 2: same as case 1 except kcpc_passivate is called from thread_exit().
11750Sstevel@tonic-gate  *
11760Sstevel@tonic-gate  * Case 3: kcpc_free(), called via freectx() via exec(), recognizes that it has
11770Sstevel@tonic-gate  * been called from exec. It stops the counters _and_ frees the context.
11780Sstevel@tonic-gate  *
11790Sstevel@tonic-gate  * Case 4: kcpc_unbind() stops the hardware _and_ frees the context.
11800Sstevel@tonic-gate  *
11810Sstevel@tonic-gate  * CPU-bound counters are always stopped via kcpc_unbind().
11820Sstevel@tonic-gate  */
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * We're being called to delete the context; we ensure that all associated data
11860Sstevel@tonic-gate  * structures are freed, and that the hardware is passivated if this is an exec.
11870Sstevel@tonic-gate  */
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate /*ARGSUSED*/
11900Sstevel@tonic-gate static void
11910Sstevel@tonic-gate kcpc_free(kcpc_ctx_t *ctx, int isexec)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	int		i;
11940Sstevel@tonic-gate 	kcpc_set_t	*set = ctx->kc_set;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	ASSERT(set != NULL);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (isexec) {
12010Sstevel@tonic-gate 		/*
12020Sstevel@tonic-gate 		 * This thread is execing, and after the exec it should not have
12030Sstevel@tonic-gate 		 * any performance counter context. Stop the counters properly
12040Sstevel@tonic-gate 		 * here so the system isn't surprised by an overflow interrupt
12050Sstevel@tonic-gate 		 * later.
12060Sstevel@tonic-gate 		 */
12070Sstevel@tonic-gate 		if (ctx->kc_cpuid != -1) {
12080Sstevel@tonic-gate 			cpu_t *cp;
12090Sstevel@tonic-gate 			/*
12100Sstevel@tonic-gate 			 * CPU-bound context; stop the appropriate CPU's ctrs.
12110Sstevel@tonic-gate 			 * Hold cpu_lock while examining the CPU to ensure it
12120Sstevel@tonic-gate 			 * doesn't go away.
12130Sstevel@tonic-gate 			 */
12140Sstevel@tonic-gate 			mutex_enter(&cpu_lock);
12150Sstevel@tonic-gate 			cp = cpu_get(ctx->kc_cpuid);
12160Sstevel@tonic-gate 			/*
12170Sstevel@tonic-gate 			 * The CPU could have been DR'd out, so only stop the
12180Sstevel@tonic-gate 			 * CPU and clear its context pointer if the CPU still
12190Sstevel@tonic-gate 			 * exists.
12200Sstevel@tonic-gate 			 */
12210Sstevel@tonic-gate 			if (cp != NULL) {
12220Sstevel@tonic-gate 				mutex_enter(&cp->cpu_cpc_ctxlock);
12230Sstevel@tonic-gate 				kcpc_stop_hw(ctx);
12240Sstevel@tonic-gate 				cp->cpu_cpc_ctx = NULL;
12250Sstevel@tonic-gate 				mutex_exit(&cp->cpu_cpc_ctxlock);
12260Sstevel@tonic-gate 			}
12270Sstevel@tonic-gate 			mutex_exit(&cpu_lock);
12280Sstevel@tonic-gate 			ASSERT(curthread->t_cpc_ctx == NULL);
12290Sstevel@tonic-gate 		} else {
12300Sstevel@tonic-gate 			/*
12310Sstevel@tonic-gate 			 * Thread-bound context; stop _this_ CPU's counters.
12320Sstevel@tonic-gate 			 */
12330Sstevel@tonic-gate 			kpreempt_disable();
12340Sstevel@tonic-gate 			pcbe_ops->pcbe_allstop();
12350Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags,
12360Sstevel@tonic-gate 			    KCPC_CTX_INVALID_STOPPED);
12370Sstevel@tonic-gate 			kpreempt_enable();
12380Sstevel@tonic-gate 			curthread->t_cpc_ctx = NULL;
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 		/*
12420Sstevel@tonic-gate 		 * Since we are being called from an exec and we know that
12430Sstevel@tonic-gate 		 * exec is not permitted via the agent thread, we should clean
12440Sstevel@tonic-gate 		 * up this thread's CPC state completely, and not leave dangling
12450Sstevel@tonic-gate 		 * CPC pointers behind.
12460Sstevel@tonic-gate 		 */
12470Sstevel@tonic-gate 		ASSERT(ctx->kc_thread == curthread);
12480Sstevel@tonic-gate 		curthread->t_cpc_set = NULL;
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * Walk through each request in this context's set and free the PCBE's
12530Sstevel@tonic-gate 	 * configuration if it exists.
12540Sstevel@tonic-gate 	 */
12550Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
12560Sstevel@tonic-gate 		if (set->ks_req[i].kr_config != NULL)
12570Sstevel@tonic-gate 			pcbe_ops->pcbe_free(set->ks_req[i].kr_config);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	kmem_free(set->ks_data, set->ks_nreqs * sizeof (uint64_t));
12610Sstevel@tonic-gate 	kcpc_ctx_free(ctx);
12620Sstevel@tonic-gate 	kcpc_free_set(set);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate  * Free the memory associated with a request set.
12670Sstevel@tonic-gate  */
12680Sstevel@tonic-gate void
12690Sstevel@tonic-gate kcpc_free_set(kcpc_set_t *set)
12700Sstevel@tonic-gate {
12710Sstevel@tonic-gate 	int		i;
12720Sstevel@tonic-gate 	kcpc_request_t	*req;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	ASSERT(set->ks_req != NULL);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
12770Sstevel@tonic-gate 		req = &set->ks_req[i];
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		if (req->kr_nattrs != 0) {
12800Sstevel@tonic-gate 			kmem_free(req->kr_attr,
12810Sstevel@tonic-gate 			    req->kr_nattrs * sizeof (kcpc_attr_t));
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	kmem_free(set->ks_req, sizeof (kcpc_request_t) * set->ks_nreqs);
12860Sstevel@tonic-gate 	kmem_free(set, sizeof (kcpc_set_t));
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate /*
12900Sstevel@tonic-gate  * Grab every existing context and mark it as invalid.
12910Sstevel@tonic-gate  */
12920Sstevel@tonic-gate void
12930Sstevel@tonic-gate kcpc_invalidate_all(void)
12940Sstevel@tonic-gate {
12950Sstevel@tonic-gate 	kcpc_ctx_t *ctx;
12960Sstevel@tonic-gate 	long hash;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	for (hash = 0; hash < CPC_HASH_BUCKETS; hash++) {
12990Sstevel@tonic-gate 		mutex_enter(&kcpc_ctx_llock[hash]);
13000Sstevel@tonic-gate 		for (ctx = kcpc_ctx_list[hash]; ctx; ctx = ctx->kc_next)
13010Sstevel@tonic-gate 			atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
13020Sstevel@tonic-gate 		mutex_exit(&kcpc_ctx_llock[hash]);
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate /*
13073732Sae112802  * Interface for PCBEs to signal that an existing configuration has suddenly
13083732Sae112802  * become invalid.
13093732Sae112802  */
13103732Sae112802 void
13113732Sae112802 kcpc_invalidate_config(void *token)
13123732Sae112802 {
13133732Sae112802 	kcpc_ctx_t *ctx = token;
13143732Sae112802 
13153732Sae112802 	ASSERT(ctx != NULL);
13163732Sae112802 
13173732Sae112802 	atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
13183732Sae112802 }
13193732Sae112802 
13203732Sae112802 /*
13210Sstevel@tonic-gate  * Called from lwp_exit() and thread_exit()
13220Sstevel@tonic-gate  */
13230Sstevel@tonic-gate void
13240Sstevel@tonic-gate kcpc_passivate(void)
13250Sstevel@tonic-gate {
13260Sstevel@tonic-gate 	kcpc_ctx_t *ctx = curthread->t_cpc_ctx;
13270Sstevel@tonic-gate 	kcpc_set_t *set = curthread->t_cpc_set;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if (set == NULL)
13300Sstevel@tonic-gate 		return;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/*
13330Sstevel@tonic-gate 	 * We're cleaning up after this thread; ensure there are no dangling
13340Sstevel@tonic-gate 	 * CPC pointers left behind. The context and set will be freed by
13350Sstevel@tonic-gate 	 * freectx() in the case of an LWP-bound set, and by kcpc_unbind() in
13360Sstevel@tonic-gate 	 * the case of a CPU-bound set.
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 	curthread->t_cpc_ctx = NULL;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (ctx == NULL) {
13410Sstevel@tonic-gate 		/*
13420Sstevel@tonic-gate 		 * This thread has a set but no context; it must be a CPU-bound
13430Sstevel@tonic-gate 		 * set. The hardware will be stopped via kcpc_unbind() when the
13440Sstevel@tonic-gate 		 * process exits and closes its file descriptors with
13450Sstevel@tonic-gate 		 * kcpc_close(). Our only job here is to clean up this thread's
13460Sstevel@tonic-gate 		 * state; the set will be freed with the unbind().
13470Sstevel@tonic-gate 		 */
13480Sstevel@tonic-gate 		(void) kcpc_unbind(set);
13490Sstevel@tonic-gate 		/*
13500Sstevel@tonic-gate 		 * Unbinding a set belonging to the current thread should clear
13510Sstevel@tonic-gate 		 * its set pointer.
13520Sstevel@tonic-gate 		 */
13530Sstevel@tonic-gate 		ASSERT(curthread->t_cpc_set == NULL);
13540Sstevel@tonic-gate 		return;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	curthread->t_cpc_set = NULL;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/*
13600Sstevel@tonic-gate 	 * This thread/LWP is exiting but context switches will continue to
13610Sstevel@tonic-gate 	 * happen for a bit as the exit proceeds.  Kernel preemption must be
13620Sstevel@tonic-gate 	 * disabled here to prevent a race between checking or setting the
13630Sstevel@tonic-gate 	 * INVALID_STOPPED flag here and kcpc_restore() setting the flag during
13640Sstevel@tonic-gate 	 * a context switch.
13650Sstevel@tonic-gate 	 */
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	kpreempt_disable();
13680Sstevel@tonic-gate 	if ((ctx->kc_flags & KCPC_CTX_INVALID_STOPPED) == 0) {
13690Sstevel@tonic-gate 		pcbe_ops->pcbe_allstop();
13700Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags,
13710Sstevel@tonic-gate 		    KCPC_CTX_INVALID | KCPC_CTX_INVALID_STOPPED);
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 	kpreempt_enable();
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate  * Assign the requests in the given set to the PICs in the context.
13780Sstevel@tonic-gate  * Returns 0 if successful, -1 on failure.
13790Sstevel@tonic-gate  */
13800Sstevel@tonic-gate /*ARGSUSED*/
13810Sstevel@tonic-gate static int
13820Sstevel@tonic-gate kcpc_assign_reqs(kcpc_set_t *set, kcpc_ctx_t *ctx)
13830Sstevel@tonic-gate {
13840Sstevel@tonic-gate 	int i;
13850Sstevel@tonic-gate 	int *picnum_save;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	ASSERT(set->ks_nreqs <= cpc_ncounters);
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	/*
13900Sstevel@tonic-gate 	 * Provide kcpc_tryassign() with scratch space to avoid doing an
13910Sstevel@tonic-gate 	 * alloc/free with every invocation.
13920Sstevel@tonic-gate 	 */
13930Sstevel@tonic-gate 	picnum_save = kmem_alloc(set->ks_nreqs * sizeof (int), KM_SLEEP);
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * kcpc_tryassign() blindly walks through each request in the set,
13960Sstevel@tonic-gate 	 * seeing if a counter can count its event. If yes, it assigns that
13970Sstevel@tonic-gate 	 * counter. However, that counter may have been the only capable counter
13980Sstevel@tonic-gate 	 * for _another_ request's event. The solution is to try every possible
13990Sstevel@tonic-gate 	 * request first. Note that this does not cover all solutions, as
14000Sstevel@tonic-gate 	 * that would require all unique orderings of requests, an n^n operation
14010Sstevel@tonic-gate 	 * which would be unacceptable for architectures with many counters.
14020Sstevel@tonic-gate 	 */
14030Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++)
14040Sstevel@tonic-gate 		if (kcpc_tryassign(set, i, picnum_save) == 0)
14050Sstevel@tonic-gate 			break;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	kmem_free(picnum_save, set->ks_nreqs * sizeof (int));
14080Sstevel@tonic-gate 	if (i == set->ks_nreqs)
14090Sstevel@tonic-gate 		return (-1);
14100Sstevel@tonic-gate 	return (0);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate static int
14140Sstevel@tonic-gate kcpc_tryassign(kcpc_set_t *set, int starting_req, int *scratch)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	int		i;
14170Sstevel@tonic-gate 	int		j;
14180Sstevel@tonic-gate 	uint64_t	bitmap = 0, resmap = 0;
14190Sstevel@tonic-gate 	uint64_t	ctrmap;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/*
14220Sstevel@tonic-gate 	 * We are attempting to assign the reqs to pics, but we may fail. If we
14230Sstevel@tonic-gate 	 * fail, we need to restore the state of the requests to what it was
14240Sstevel@tonic-gate 	 * when we found it, as some reqs may have been explicitly assigned to
14250Sstevel@tonic-gate 	 * a specific PIC beforehand. We do this by snapshotting the assignments
14260Sstevel@tonic-gate 	 * now and restoring from it later if we fail.
14270Sstevel@tonic-gate 	 *
14280Sstevel@tonic-gate 	 * Also we note here which counters have already been claimed by
14290Sstevel@tonic-gate 	 * requests with explicit counter assignments.
14300Sstevel@tonic-gate 	 */
14310Sstevel@tonic-gate 	for (i = 0; i < set->ks_nreqs; i++) {
14320Sstevel@tonic-gate 		scratch[i] = set->ks_req[i].kr_picnum;
14330Sstevel@tonic-gate 		if (set->ks_req[i].kr_picnum != -1)
14340Sstevel@tonic-gate 			resmap |= (1 << set->ks_req[i].kr_picnum);
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * Walk through requests assigning them to the first PIC that is
14390Sstevel@tonic-gate 	 * capable.
14400Sstevel@tonic-gate 	 */
14410Sstevel@tonic-gate 	i = starting_req;
14420Sstevel@tonic-gate 	do {
14430Sstevel@tonic-gate 		if (set->ks_req[i].kr_picnum != -1) {
14440Sstevel@tonic-gate 			ASSERT((bitmap & (1 << set->ks_req[i].kr_picnum)) == 0);
14450Sstevel@tonic-gate 			bitmap |= (1 << set->ks_req[i].kr_picnum);
14460Sstevel@tonic-gate 			if (++i == set->ks_nreqs)
14470Sstevel@tonic-gate 				i = 0;
14480Sstevel@tonic-gate 			continue;
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 		ctrmap = pcbe_ops->pcbe_event_coverage(set->ks_req[i].kr_event);
14520Sstevel@tonic-gate 		for (j = 0; j < cpc_ncounters; j++) {
14530Sstevel@tonic-gate 			if (ctrmap & (1 << j) && (bitmap & (1 << j)) == 0 &&
14540Sstevel@tonic-gate 			    (resmap & (1 << j)) == 0) {
14550Sstevel@tonic-gate 				/*
14560Sstevel@tonic-gate 				 * We can assign this counter because:
14570Sstevel@tonic-gate 				 *
14580Sstevel@tonic-gate 				 * 1. It can count the event (ctrmap)
14590Sstevel@tonic-gate 				 * 2. It hasn't been assigned yet (bitmap)
14600Sstevel@tonic-gate 				 * 3. It wasn't reserved by a request (resmap)
14610Sstevel@tonic-gate 				 */
14620Sstevel@tonic-gate 				bitmap |= (1 << j);
14630Sstevel@tonic-gate 				break;
14640Sstevel@tonic-gate 			}
14650Sstevel@tonic-gate 		}
14660Sstevel@tonic-gate 		if (j == cpc_ncounters) {
14670Sstevel@tonic-gate 			for (i = 0; i < set->ks_nreqs; i++)
14680Sstevel@tonic-gate 				set->ks_req[i].kr_picnum = scratch[i];
14690Sstevel@tonic-gate 			return (-1);
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 		set->ks_req[i].kr_picnum = j;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 		if (++i == set->ks_nreqs)
14740Sstevel@tonic-gate 			i = 0;
14750Sstevel@tonic-gate 	} while (i != starting_req);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	return (0);
14780Sstevel@tonic-gate }
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate kcpc_set_t *
14810Sstevel@tonic-gate kcpc_dup_set(kcpc_set_t *set)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	kcpc_set_t	*new;
14840Sstevel@tonic-gate 	int		i;
14850Sstevel@tonic-gate 	int		j;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	new = kmem_alloc(sizeof (*new), KM_SLEEP);
14880Sstevel@tonic-gate 	new->ks_flags = set->ks_flags;
14890Sstevel@tonic-gate 	new->ks_nreqs = set->ks_nreqs;
14900Sstevel@tonic-gate 	new->ks_req = kmem_alloc(set->ks_nreqs * sizeof (kcpc_request_t),
14910Sstevel@tonic-gate 	    KM_SLEEP);
14920Sstevel@tonic-gate 	new->ks_data = NULL;
14930Sstevel@tonic-gate 	new->ks_ctx = NULL;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	for (i = 0; i < new->ks_nreqs; i++) {
14960Sstevel@tonic-gate 		new->ks_req[i].kr_config = NULL;
14970Sstevel@tonic-gate 		new->ks_req[i].kr_index = set->ks_req[i].kr_index;
14980Sstevel@tonic-gate 		new->ks_req[i].kr_picnum = set->ks_req[i].kr_picnum;
14990Sstevel@tonic-gate 		new->ks_req[i].kr_picp = NULL;
15000Sstevel@tonic-gate 		new->ks_req[i].kr_data = NULL;
15010Sstevel@tonic-gate 		(void) strncpy(new->ks_req[i].kr_event, set->ks_req[i].kr_event,
15020Sstevel@tonic-gate 		    CPC_MAX_EVENT_LEN);
15030Sstevel@tonic-gate 		new->ks_req[i].kr_preset = set->ks_req[i].kr_preset;
15040Sstevel@tonic-gate 		new->ks_req[i].kr_flags = set->ks_req[i].kr_flags;
15050Sstevel@tonic-gate 		new->ks_req[i].kr_nattrs = set->ks_req[i].kr_nattrs;
15060Sstevel@tonic-gate 		new->ks_req[i].kr_attr = kmem_alloc(new->ks_req[i].kr_nattrs *
15070Sstevel@tonic-gate 		    sizeof (kcpc_attr_t), KM_SLEEP);
15080Sstevel@tonic-gate 		for (j = 0; j < new->ks_req[i].kr_nattrs; j++) {
15090Sstevel@tonic-gate 			new->ks_req[i].kr_attr[j].ka_val =
15100Sstevel@tonic-gate 			    set->ks_req[i].kr_attr[j].ka_val;
15110Sstevel@tonic-gate 			(void) strncpy(new->ks_req[i].kr_attr[j].ka_name,
15120Sstevel@tonic-gate 			    set->ks_req[i].kr_attr[j].ka_name,
15130Sstevel@tonic-gate 			    CPC_MAX_ATTR_LEN);
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	return (new);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate int
15210Sstevel@tonic-gate kcpc_allow_nonpriv(void *token)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	return (((kcpc_ctx_t *)token)->kc_flags & KCPC_CTX_NONPRIV);
15240Sstevel@tonic-gate }
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate void
15270Sstevel@tonic-gate kcpc_invalidate(kthread_t *t)
15280Sstevel@tonic-gate {
15290Sstevel@tonic-gate 	kcpc_ctx_t *ctx = t->t_cpc_ctx;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	if (ctx != NULL)
15320Sstevel@tonic-gate 		atomic_or_uint(&ctx->kc_flags, KCPC_CTX_INVALID);
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate /*
15360Sstevel@tonic-gate  * Given a PCBE ID, attempt to load a matching PCBE module. The strings given
15370Sstevel@tonic-gate  * are used to construct PCBE names, starting with the most specific,
15380Sstevel@tonic-gate  * "pcbe.first.second.third.fourth" and ending with the least specific,
15390Sstevel@tonic-gate  * "pcbe.first".
15400Sstevel@tonic-gate  *
15410Sstevel@tonic-gate  * Returns 0 if a PCBE was successfully loaded and -1 upon error.
15420Sstevel@tonic-gate  */
15430Sstevel@tonic-gate int
15440Sstevel@tonic-gate kcpc_pcbe_tryload(const char *prefix, uint_t first, uint_t second, uint_t third)
15450Sstevel@tonic-gate {
15461414Scindi 	uint_t s[3];
15470Sstevel@tonic-gate 
15481414Scindi 	s[0] = first;
15491414Scindi 	s[1] = second;
15501414Scindi 	s[2] = third;
15510Sstevel@tonic-gate 
15521414Scindi 	return (modload_qualified("pcbe",
1553*5254Sgavinm 	    "pcbe", prefix, ".", s, 3, NULL) < 0 ? -1 : 0);
15540Sstevel@tonic-gate }
1555