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