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