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
52791Srab  * Common Development and Distribution License (the "License").
62791Srab  * 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  */
210Sstevel@tonic-gate /*
22*3732Sae112802  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <libcpc.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <stropts.h>
350Sstevel@tonic-gate #include <libintl.h>
360Sstevel@tonic-gate #include <signal.h>
370Sstevel@tonic-gate #include <sys/syscall.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/processor.h>
400Sstevel@tonic-gate #include <sys/procset.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "libcpc_impl.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	MASK32 0xFFFFFFFF
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * The library uses the cpc_lock field of the cpc_t struct to protect access to
480Sstevel@tonic-gate  * the linked lists inside the cpc_t, and only the linked lists. It is NOT used
490Sstevel@tonic-gate  * to protect against a user shooting his/herself in the foot (such as, for
500Sstevel@tonic-gate  * instance, destroying the same set at the same time from different threads.).
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * SIGEMT needs to be blocked while holding the lock, to prevent deadlock among
530Sstevel@tonic-gate  * an app holding the lock and a signal handler attempting to sample or bind.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static char *cpc_get_list(int which, int arg);
570Sstevel@tonic-gate static void cpc_err(cpc_t *cpc, const char *fn, int subcode, ...);
580Sstevel@tonic-gate static int cpc_set_valid(cpc_t *cpc, cpc_set_t *set);
590Sstevel@tonic-gate static int cpc_lock(cpc_t *cpc);
600Sstevel@tonic-gate static void cpc_unlock(cpc_t *cpc, int blocked);
610Sstevel@tonic-gate static int cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev);
620Sstevel@tonic-gate static int cpc_valid_attr(cpc_t *cpc, char *attr);
630Sstevel@tonic-gate static void cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate cpc_t *
660Sstevel@tonic-gate cpc_open(int ver)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	cpc_t	*cpc;
690Sstevel@tonic-gate 	void	(*sigsaved)();
700Sstevel@tonic-gate 	int	error = 0;
710Sstevel@tonic-gate 	int	i;
720Sstevel@tonic-gate 	int	j;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (ver != CPC_VER_CURRENT) {
750Sstevel@tonic-gate 		/*
760Sstevel@tonic-gate 		 * v1 clients must stick to the v1 interface: cpc_version()
770Sstevel@tonic-gate 		 */
780Sstevel@tonic-gate 		errno = EINVAL;
790Sstevel@tonic-gate 		return (NULL);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * Call the syscall with invalid parameters.  If we get ENOSYS this CPU
840Sstevel@tonic-gate 	 * has no CPC support.  We need to block SIGSYS because the syscall code
850Sstevel@tonic-gate 	 * will send the signal if the system call fails to load.
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	sigsaved = signal(SIGSYS, SIG_IGN);
880Sstevel@tonic-gate 	if (syscall(SYS_cpc, -1, -1, -1, -1, -1) != -1) {
890Sstevel@tonic-gate 		(void) signal(SIGSYS, sigsaved);
900Sstevel@tonic-gate 		errno = EINVAL;
910Sstevel@tonic-gate 		return (NULL);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 	error = errno;
940Sstevel@tonic-gate 	(void) signal(SIGSYS, sigsaved);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (error != EINVAL) {
970Sstevel@tonic-gate 		errno = error;
980Sstevel@tonic-gate 		return (NULL);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if ((cpc = malloc(sizeof (cpc_t))) == NULL) {
1020Sstevel@tonic-gate 		errno = ENOMEM;
1030Sstevel@tonic-gate 		return (NULL);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	cpc->cpc_npic = syscall(SYS_cpc, CPC_NPIC, -1, 0, 0, 0);
1070Sstevel@tonic-gate 	cpc->cpc_caps = syscall(SYS_cpc, CPC_CAPS, -1, 0, 0, 0);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_IMPL_NAME, -1, &cpc->cpc_cciname, 0, 0) != 0)
1100Sstevel@tonic-gate 		return (NULL);
1110Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_CPUREF, -1, &cpc->cpc_cpuref, 0, 0) != 0)
1120Sstevel@tonic-gate 		return (NULL);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if ((cpc->cpc_attrlist = cpc_get_list(CPC_LIST_ATTRS, 0)) == NULL) {
1160Sstevel@tonic-gate 		free(cpc);
1170Sstevel@tonic-gate 		return (NULL);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if ((cpc->cpc_evlist = malloc(cpc->cpc_npic * sizeof (char *))) ==
1210Sstevel@tonic-gate 	    NULL) {
1220Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
1230Sstevel@tonic-gate 		free(cpc);
1240Sstevel@tonic-gate 		return (NULL);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	for (i = 0; i < cpc->cpc_npic; i++) {
1280Sstevel@tonic-gate 		if ((cpc->cpc_evlist[i] = cpc_get_list(CPC_LIST_EVENTS, i)) ==
1290Sstevel@tonic-gate 		    NULL)
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	if (i != cpc->cpc_npic) {
1330Sstevel@tonic-gate 		for (j = 0; j < i; j++)
1340Sstevel@tonic-gate 			free(cpc->cpc_evlist[j]);
1350Sstevel@tonic-gate 		free(cpc->cpc_evlist);
1360Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
1370Sstevel@tonic-gate 		free(cpc);
1380Sstevel@tonic-gate 		return (NULL);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	cpc->cpc_sets = NULL;
1420Sstevel@tonic-gate 	cpc->cpc_bufs = NULL;
1430Sstevel@tonic-gate 	cpc->cpc_errfn = NULL;
1440Sstevel@tonic-gate 	(void) mutex_init(&cpc->cpc_lock, USYNC_THREAD, NULL);
1450Sstevel@tonic-gate 	__pctx_cpc_register_callback(cpc_invalidate_pctx);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (cpc);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Ensure state is cleaned up:
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  * - Hardware is unbound
1540Sstevel@tonic-gate  * - Sets are all destroyed
1550Sstevel@tonic-gate  * - Bufs are all freed
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate int
1580Sstevel@tonic-gate cpc_close(cpc_t *cpc)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	while (cpc->cpc_sets != NULL) {
1610Sstevel@tonic-gate 		if (cpc->cpc_sets->cs_state != CS_UNBOUND)
1620Sstevel@tonic-gate 			(void) cpc_unbind(cpc, cpc->cpc_sets);
1630Sstevel@tonic-gate 		(void) cpc_set_destroy(cpc, cpc->cpc_sets);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	while (cpc->cpc_bufs != NULL)
1670Sstevel@tonic-gate 		(void) cpc_buf_destroy(cpc, cpc->cpc_bufs);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	free(cpc);
1700Sstevel@tonic-gate 	return (0);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate cpc_set_t *
1740Sstevel@tonic-gate cpc_set_create(cpc_t *cpc)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	cpc_set_t	*set;
1770Sstevel@tonic-gate 	int		sigblocked;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if ((set = malloc(sizeof (*set))) == NULL) {
1800Sstevel@tonic-gate 		errno = ENOMEM;
1810Sstevel@tonic-gate 		return (NULL);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	set->cs_request = NULL;
1850Sstevel@tonic-gate 	set->cs_nreqs	= 0;
1860Sstevel@tonic-gate 	set->cs_state	= CS_UNBOUND;
1870Sstevel@tonic-gate 	set->cs_fd	= -1;
1880Sstevel@tonic-gate 	set->cs_pctx	= NULL;
1890Sstevel@tonic-gate 	set->cs_id	= -1;
1900Sstevel@tonic-gate 	set->cs_thr	= NULL;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
1930Sstevel@tonic-gate 	set->cs_next = cpc->cpc_sets;
1940Sstevel@tonic-gate 	cpc->cpc_sets = set;
1950Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	return (set);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
2010Sstevel@tonic-gate cpc_set_destroy(cpc_t *cpc, cpc_set_t *set)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	cpc_set_t	*csp, *prev;
2040Sstevel@tonic-gate 	cpc_request_t	*req, *next;
2050Sstevel@tonic-gate 	int		sigblocked;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * Remove this set from the cpc handle's list of sets.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
2110Sstevel@tonic-gate 	for (csp = prev = cpc->cpc_sets; csp != NULL; csp = csp->cs_next) {
2120Sstevel@tonic-gate 		if (csp == set)
2130Sstevel@tonic-gate 			break;
2140Sstevel@tonic-gate 		prev = csp;
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	if (csp == NULL) {
2170Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
2180Sstevel@tonic-gate 		errno = EINVAL;
2190Sstevel@tonic-gate 		return (-1);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	if (csp == cpc->cpc_sets)
2220Sstevel@tonic-gate 		cpc->cpc_sets = csp->cs_next;
2230Sstevel@tonic-gate 	prev->cs_next = csp->cs_next;
2240Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (csp->cs_state != CS_UNBOUND)
2270Sstevel@tonic-gate 		(void) cpc_unbind(cpc, csp);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (req = csp->cs_request; req != NULL; req = next) {
2300Sstevel@tonic-gate 		next = req->cr_next;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if (req->cr_nattrs != 0)
2330Sstevel@tonic-gate 			free(req->cr_attr);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		free(req);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	free(set);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	return (0);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*ARGSUSED*/
2450Sstevel@tonic-gate int
2460Sstevel@tonic-gate cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
2470Sstevel@tonic-gate     uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	cpc_request_t	*req;
2500Sstevel@tonic-gate 	const char	*fn = "cpc_set_add_request";
2510Sstevel@tonic-gate 	int		i;
2520Sstevel@tonic-gate 	int		npics = cpc_npic(cpc);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0 || set->cs_state != CS_UNBOUND) {
2550Sstevel@tonic-gate 		errno = EINVAL;
2560Sstevel@tonic-gate 		return (-1);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	for (i = 0; i < npics; i++)
2600Sstevel@tonic-gate 		if (cpc_valid_event(cpc, i, event))
2610Sstevel@tonic-gate 			break;
2620Sstevel@tonic-gate 	if (i == npics) {
2630Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_INVALID_EVENT);
2640Sstevel@tonic-gate 		errno = EINVAL;
2650Sstevel@tonic-gate 		return (-1);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if ((req = malloc(sizeof (*req))) == NULL) {
2690Sstevel@tonic-gate 		errno = ENOMEM;
2700Sstevel@tonic-gate 		return (-1);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	(void) strncpy(req->cr_event, event, CPC_MAX_EVENT_LEN);
2740Sstevel@tonic-gate 	req->cr_preset = preset;
2750Sstevel@tonic-gate 	req->cr_flags = flags;
2760Sstevel@tonic-gate 	req->cr_nattrs = nattrs;
2770Sstevel@tonic-gate 	req->cr_index = set->cs_nreqs;
2780Sstevel@tonic-gate 	req->cr_attr = NULL;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (nattrs != 0) {
2810Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
2820Sstevel@tonic-gate 			/*
2830Sstevel@tonic-gate 			 * Verify that each attribute name is legal and valid.
2840Sstevel@tonic-gate 			 */
2850Sstevel@tonic-gate 			if (attrs[i].ca_name[0] == '\0' ||
2860Sstevel@tonic-gate 			    cpc_valid_attr(cpc, attrs[i].ca_name) == 0) {
2870Sstevel@tonic-gate 				cpc_err(cpc, fn, CPC_INVALID_ATTRIBUTE);
2880Sstevel@tonic-gate 				goto inval;
2890Sstevel@tonic-gate 			}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 			/*
2920Sstevel@tonic-gate 			 * If the user requested a specific picnum, ensure that
2930Sstevel@tonic-gate 			 * the pic can count the requested event.
2940Sstevel@tonic-gate 			 */
2950Sstevel@tonic-gate 			if (strncmp("picnum", attrs[i].ca_name, 8) == 0) {
2960Sstevel@tonic-gate 				if (attrs[i].ca_val >= npics) {
2970Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_INVALID_PICNUM);
2980Sstevel@tonic-gate 					goto inval;
2990Sstevel@tonic-gate 				}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 				if (cpc_valid_event(cpc, attrs[i].ca_val,
3020Sstevel@tonic-gate 				    req->cr_event) == 0) {
3030Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_PIC_NOT_CAPABLE);
3040Sstevel@tonic-gate 					goto inval;
3050Sstevel@tonic-gate 				}
3060Sstevel@tonic-gate 			}
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		if ((req->cr_attr = malloc(nattrs * sizeof (kcpc_attr_t)))
3100Sstevel@tonic-gate 		    == NULL) {
3110Sstevel@tonic-gate 			free(req);
3120Sstevel@tonic-gate 			return (-1);
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
3160Sstevel@tonic-gate 			req->cr_attr[i].ka_val = attrs[i].ca_val;
3170Sstevel@tonic-gate 			(void) strncpy(req->cr_attr[i].ka_name,
3180Sstevel@tonic-gate 			    attrs[i].ca_name, CPC_MAX_ATTR_LEN);
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 	} else
3210Sstevel@tonic-gate 		req->cr_attr = NULL;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	req->cr_next = set->cs_request;
3240Sstevel@tonic-gate 	set->cs_request = req;
3250Sstevel@tonic-gate 	set->cs_nreqs++;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	return (req->cr_index);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate inval:
3300Sstevel@tonic-gate 	free(req);
3310Sstevel@tonic-gate 	errno = EINVAL;
3320Sstevel@tonic-gate 	return (-1);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate cpc_buf_t *
3360Sstevel@tonic-gate cpc_buf_create(cpc_t *cpc, cpc_set_t *set)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	cpc_buf_t	*buf;
3390Sstevel@tonic-gate 	int		sigblocked;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
3420Sstevel@tonic-gate 		errno = EINVAL;
3430Sstevel@tonic-gate 		return (NULL);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if ((buf = malloc(sizeof (*buf))) == NULL)
3470Sstevel@tonic-gate 		return (NULL);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	buf->cb_size = set->cs_nreqs * sizeof (uint64_t);
3500Sstevel@tonic-gate 	if ((buf->cb_data = malloc(buf->cb_size)) == NULL) {
3510Sstevel@tonic-gate 		free(buf);
3520Sstevel@tonic-gate 		return (NULL);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	buf->cb_hrtime = 0;
3580Sstevel@tonic-gate 	buf->cb_tick = 0;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
3610Sstevel@tonic-gate 	buf->cb_next = cpc->cpc_bufs;
3620Sstevel@tonic-gate 	cpc->cpc_bufs = buf;
3630Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	return (buf);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate int
3690Sstevel@tonic-gate cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	cpc_buf_t	*cbp, *prev;
3720Sstevel@tonic-gate 	int		sigblocked;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * Remove this buf from the cpc handle's list of bufs.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
3780Sstevel@tonic-gate 	for (cbp = prev = cpc->cpc_bufs; cbp != NULL; cbp = cbp->cb_next) {
3790Sstevel@tonic-gate 		if (cbp == buf)
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 		prev = cbp;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	if (cbp == NULL) {
3840Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
3850Sstevel@tonic-gate 		errno = EINVAL;
3860Sstevel@tonic-gate 		return (-1);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	if (cbp == cpc->cpc_bufs)
3890Sstevel@tonic-gate 		cpc->cpc_bufs = cbp->cb_next;
3900Sstevel@tonic-gate 	prev->cb_next = cbp->cb_next;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
3930Sstevel@tonic-gate 	free(cbp->cb_data);
3940Sstevel@tonic-gate 	free(cbp);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	return (0);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*ARGSUSED*/
4000Sstevel@tonic-gate int
4010Sstevel@tonic-gate cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	char		*packed_set;
4040Sstevel@tonic-gate 	size_t		packsize;
4050Sstevel@tonic-gate 	int		ret;
4060Sstevel@tonic-gate 	int		subcode = -1;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * We don't bother checking cpc_set_valid() here, because this is in the
4100Sstevel@tonic-gate 	 * fast path of an app doing SIGEMT-based profiling as they restart the
4110Sstevel@tonic-gate 	 * counters from their signal handler.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	if (CPC_SET_VALID_FLAGS(flags) == 0 || set->cs_nreqs <= 0) {
4140Sstevel@tonic-gate 		errno = EINVAL;
4150Sstevel@tonic-gate 		return (-1);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
4190Sstevel@tonic-gate 		errno = ENOMEM;
4200Sstevel@tonic-gate 		return (-1);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	ret = syscall(SYS_cpc, CPC_BIND, -1, packed_set, packsize, &subcode);
4240Sstevel@tonic-gate 	free(packed_set);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (ret != 0) {
4270Sstevel@tonic-gate 		if (subcode != -1)
4280Sstevel@tonic-gate 			cpc_err(cpc, "cpc_bind_curlwp", subcode);
4290Sstevel@tonic-gate 		return (-1);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	set->cs_thr = thr_self();
4330Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CURLWP;
4340Sstevel@tonic-gate 	return (ret);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*ARGSUSED*/
4380Sstevel@tonic-gate int
4390Sstevel@tonic-gate cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set, uint_t flags)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	char		*packed_set;
4420Sstevel@tonic-gate 	size_t		packsize;
4430Sstevel@tonic-gate 	int		ret;
4440Sstevel@tonic-gate 	int		subcode = -1;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * cpc_bind_pctx() currently has no valid flags.
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
4500Sstevel@tonic-gate 		errno = EINVAL;
4510Sstevel@tonic-gate 		return (-1);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
4550Sstevel@tonic-gate 		errno = ENOMEM;
4560Sstevel@tonic-gate 		return (-1);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	ret = __pctx_cpc(pctx, cpc, CPC_BIND, id, packed_set, (void *)packsize,
4600Sstevel@tonic-gate 	    (void *)&subcode, -1);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	free(packed_set);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (ret == 0) {
4650Sstevel@tonic-gate 		set->cs_pctx = pctx;
4660Sstevel@tonic-gate 		set->cs_id = id;
4670Sstevel@tonic-gate 		set->cs_state = CS_BOUND_PCTX;
4680Sstevel@tonic-gate 	} else if (subcode != -1)
4690Sstevel@tonic-gate 		cpc_err(cpc, "cpc_bind_pctx", subcode);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	return (ret);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*ARGSUSED*/
4750Sstevel@tonic-gate int
4760Sstevel@tonic-gate cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set, uint_t flags)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	int		fd;
4790Sstevel@tonic-gate 	char		*packed_set;
4800Sstevel@tonic-gate 	size_t		packsize;
4810Sstevel@tonic-gate 	__cpc_args_t	cpc_args;
4820Sstevel@tonic-gate 	int		error;
4830Sstevel@tonic-gate 	const char	*fn = "cpc_bind_cpu";
4840Sstevel@tonic-gate 	int		subcode = -1;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * cpc_bind_cpu() currently has no valid flags.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
4900Sstevel@tonic-gate 		errno = EINVAL;
4910Sstevel@tonic-gate 		return (-1);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (processor_bind(P_LWPID, P_MYID, id, &set->cs_obind) == -1) {
4950Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_PBIND_FAILED);
4960Sstevel@tonic-gate 		return (-1);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if ((fd = open(CPUDRV_SHARED, O_RDWR)) < 0) {
5000Sstevel@tonic-gate 		error = errno;
5010Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5020Sstevel@tonic-gate 		errno = error;
5030Sstevel@tonic-gate 		return (-1);
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	/*
5070Sstevel@tonic-gate 	 * To avoid leaking file descriptors, if we find an existing fd here we
5080Sstevel@tonic-gate 	 * just close it. This is only a problem if a user attempts to bind the
5090Sstevel@tonic-gate 	 * same set to different CPUs without first unbinding it.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	if (set->cs_fd != -1)
5120Sstevel@tonic-gate 		(void) close(set->cs_fd);
5130Sstevel@tonic-gate 	set->cs_fd = fd;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
5160Sstevel@tonic-gate 		(void) close(fd);
5170Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5180Sstevel@tonic-gate 		errno = ENOMEM;
5190Sstevel@tonic-gate 		return (-1);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	cpc_args.udata1 = packed_set;
5230Sstevel@tonic-gate 	cpc_args.udata2 = (void *)packsize;
5240Sstevel@tonic-gate 	cpc_args.udata3 = (void *)&subcode;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (ioctl(fd, CPCIO_BIND, &cpc_args) != 0) {
5270Sstevel@tonic-gate 		error = errno;
5280Sstevel@tonic-gate 		free(packed_set);
5290Sstevel@tonic-gate 		(void) close(fd);
5300Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5310Sstevel@tonic-gate 		if (subcode != -1)
5320Sstevel@tonic-gate 			cpc_err(cpc, fn, subcode);
5330Sstevel@tonic-gate 		errno = error;
5340Sstevel@tonic-gate 		return (-1);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	free(packed_set);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	set->cs_thr = thr_self();
5400Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CPU;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	return (0);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*ARGSUSED*/
5460Sstevel@tonic-gate int
5470Sstevel@tonic-gate cpc_request_preset(cpc_t *cpc, int index, uint64_t preset)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_PRESET, -1, index,
5500Sstevel@tonic-gate 	    (uint32_t)(preset >> 32), (uint32_t)(preset & MASK32)));
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*ARGSUSED*/
5540Sstevel@tonic-gate int
5550Sstevel@tonic-gate cpc_set_restart(cpc_t *cpc, cpc_set_t *set)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_RESTART, -1, 0, 0, 0));
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate /*ARGSUSED*/
5610Sstevel@tonic-gate int
5620Sstevel@tonic-gate cpc_unbind(cpc_t *cpc, cpc_set_t *set)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	int		ret = 0;
5650Sstevel@tonic-gate 	int		error;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
5680Sstevel@tonic-gate 		errno = EINVAL;
5690Sstevel@tonic-gate 		return (-1);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	switch (set->cs_state) {
5730Sstevel@tonic-gate 	case CS_UNBOUND:
5740Sstevel@tonic-gate 		errno = EINVAL;
5750Sstevel@tonic-gate 		return (-1);
5760Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
5770Sstevel@tonic-gate 		ret = syscall(SYS_cpc, CPC_RELE, -1, 0, 0, 0);
5780Sstevel@tonic-gate 		error = errno;
5790Sstevel@tonic-gate 		break;
5800Sstevel@tonic-gate 	case CS_BOUND_CPU:
5810Sstevel@tonic-gate 		ret = ioctl(set->cs_fd, CPCIO_RELE, NULL);
5820Sstevel@tonic-gate 		error = errno;
5830Sstevel@tonic-gate 		(void) close(set->cs_fd);
5840Sstevel@tonic-gate 		set->cs_fd = -1;
5850Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5860Sstevel@tonic-gate 		break;
5870Sstevel@tonic-gate 	case CS_BOUND_PCTX:
5880Sstevel@tonic-gate 		if (set->cs_pctx != NULL) {
5890Sstevel@tonic-gate 			ret = __pctx_cpc(set->cs_pctx, cpc, CPC_RELE,
5900Sstevel@tonic-gate 			    set->cs_id, 0, 0, 0, 0);
5910Sstevel@tonic-gate 			error = errno;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 		break;
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	set->cs_thr = NULL;
5970Sstevel@tonic-gate 	set->cs_id = -1;
5980Sstevel@tonic-gate 	set->cs_state = CS_UNBOUND;
5990Sstevel@tonic-gate 	if (ret != 0)
6000Sstevel@tonic-gate 		errno = error;
6010Sstevel@tonic-gate 	return (ret);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate /*ARGSUSED*/
6050Sstevel@tonic-gate int
6060Sstevel@tonic-gate cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	__cpc_args_t args;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * The following check ensures that only the most recently bound set
6120Sstevel@tonic-gate 	 * can be sampled, as binding a set invalidates all other sets in the
6130Sstevel@tonic-gate 	 * cpc_t.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if (set->cs_state == CS_UNBOUND ||
6160Sstevel@tonic-gate 	    buf->cb_size != set->cs_nreqs * sizeof (uint64_t)) {
6170Sstevel@tonic-gate 		errno = EINVAL;
6180Sstevel@tonic-gate 		return (-1);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	switch (set->cs_state) {
6220Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
6230Sstevel@tonic-gate 		return (syscall(SYS_cpc, CPC_SAMPLE, -1, buf->cb_data,
6240Sstevel@tonic-gate 		    &buf->cb_hrtime, &buf->cb_tick));
6250Sstevel@tonic-gate 	case CS_BOUND_CPU:
6260Sstevel@tonic-gate 		args.udata1 = buf->cb_data;
6270Sstevel@tonic-gate 		args.udata2 = &buf->cb_hrtime;
6280Sstevel@tonic-gate 		args.udata3 = &buf->cb_tick;
6290Sstevel@tonic-gate 		return (ioctl(set->cs_fd, CPCIO_SAMPLE, &args));
6300Sstevel@tonic-gate 	case CS_BOUND_PCTX:
6310Sstevel@tonic-gate 		return (__pctx_cpc(set->cs_pctx, cpc, CPC_SAMPLE, set->cs_id,
6320Sstevel@tonic-gate 		    buf->cb_data, &buf->cb_hrtime, &buf->cb_tick,
6330Sstevel@tonic-gate 		    buf->cb_size));
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	errno = EINVAL;
6370Sstevel@tonic-gate 	return (-1);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*ARGSUSED*/
6410Sstevel@tonic-gate void
6420Sstevel@tonic-gate cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate 	int i;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
6470Sstevel@tonic-gate 		return;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
6500Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
6510Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick - b->cb_tick;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
6540Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] - b->cb_data[i];
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /*ARGSUSED*/
6580Sstevel@tonic-gate void
6590Sstevel@tonic-gate cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	int i;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
6640Sstevel@tonic-gate 		return;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
6670Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
6680Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick + b->cb_tick;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
6710Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] + b->cb_data[i];
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*ARGSUSED*/
6750Sstevel@tonic-gate void
6760Sstevel@tonic-gate cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	if (ds->cb_size != src->cb_size)
6790Sstevel@tonic-gate 		return;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	bcopy(src->cb_data, ds->cb_data, ds->cb_size);
6820Sstevel@tonic-gate 	ds->cb_hrtime = src->cb_hrtime;
6830Sstevel@tonic-gate 	ds->cb_tick = src->cb_tick;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate /*ARGSUSED*/
6870Sstevel@tonic-gate void
6880Sstevel@tonic-gate cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
6910Sstevel@tonic-gate 	buf->cb_hrtime = 0;
6920Sstevel@tonic-gate 	buf->cb_tick = 0;
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate  * Gets or sets the value of the request specified by index.
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate /*ARGSUSED*/
6990Sstevel@tonic-gate int
7000Sstevel@tonic-gate cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	*val = buf->cb_data[index];
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	return (0);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /*ARGSUSED*/
7080Sstevel@tonic-gate int
7090Sstevel@tonic-gate cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	buf->cb_data[index] = val;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	return (0);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /*ARGSUSED*/
7170Sstevel@tonic-gate hrtime_t
7180Sstevel@tonic-gate cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	return (buf->cb_hrtime);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*ARGSUSED*/
7240Sstevel@tonic-gate uint64_t
7250Sstevel@tonic-gate cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate 	return (buf->cb_tick);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static char *
7310Sstevel@tonic-gate cpc_get_list(int which, int arg)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	int	szcmd;
7340Sstevel@tonic-gate 	int	size;
7350Sstevel@tonic-gate 	char	*list;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (which == CPC_LIST_ATTRS)
7380Sstevel@tonic-gate 		szcmd = CPC_ATTRLIST_SIZE;
7390Sstevel@tonic-gate 	else
7400Sstevel@tonic-gate 		szcmd = CPC_EVLIST_SIZE;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (syscall(SYS_cpc, szcmd, -1, &size, arg, 0) != 0)
7430Sstevel@tonic-gate 		return (NULL);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if ((list = malloc(size)) == NULL)
7460Sstevel@tonic-gate 		return (NULL);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (syscall(SYS_cpc, which, -1, list, arg, 0) != 0) {
7490Sstevel@tonic-gate 		free(list);
7500Sstevel@tonic-gate 		return (NULL);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	return (list);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*ARGSUSED*/
7570Sstevel@tonic-gate void
7580Sstevel@tonic-gate cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
7590Sstevel@tonic-gate     void (*action)(void *arg, int index, const char *event, uint64_t preset,
7600Sstevel@tonic-gate 	uint_t flags, int nattrs, const cpc_attr_t *attrs))
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	cpc_request_t	*rp;
7630Sstevel@tonic-gate 	cpc_attr_t	*attrs = NULL;
7640Sstevel@tonic-gate 	int		i;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	for (rp = set->cs_request; rp != NULL; rp = rp->cr_next) {
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * Need to reconstruct a temporary cpc_attr_t array for req.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
7710Sstevel@tonic-gate 			if ((attrs = malloc(rp->cr_nattrs *
7720Sstevel@tonic-gate 			    sizeof (cpc_attr_t))) == NULL)
7730Sstevel@tonic-gate 				return;
7740Sstevel@tonic-gate 		for (i = 0; i < rp->cr_nattrs; i++) {
7750Sstevel@tonic-gate 			attrs[i].ca_name = rp->cr_attr[i].ka_name;
7760Sstevel@tonic-gate 			attrs[i].ca_val = rp->cr_attr[i].ka_val;
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		action(arg, rp->cr_index, rp->cr_event, rp->cr_preset,
7800Sstevel@tonic-gate 		    rp->cr_flags, rp->cr_nattrs, attrs);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
7830Sstevel@tonic-gate 			free(attrs);
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate /*ARGSUSED*/
7880Sstevel@tonic-gate void
7890Sstevel@tonic-gate cpc_walk_events_all(cpc_t *cpc, void *arg,
7900Sstevel@tonic-gate     void (*action)(void *arg, const char *event))
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	char		**list;
7930Sstevel@tonic-gate 	char		*p, *e;
7940Sstevel@tonic-gate 	int		i;
7950Sstevel@tonic-gate 	int		ncounters = cpc_npic(cpc);
7960Sstevel@tonic-gate 	cpc_strhash_t	*hash;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if ((list = malloc(ncounters * sizeof (char *))) == NULL)
7990Sstevel@tonic-gate 		return;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if ((hash = __cpc_strhash_alloc()) == NULL) {
8020Sstevel@tonic-gate 		free(list);
8030Sstevel@tonic-gate 		return;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
8070Sstevel@tonic-gate 		if ((list[i] = strdup(cpc->cpc_evlist[i])) == NULL)
8080Sstevel@tonic-gate 			goto err;
8090Sstevel@tonic-gate 		p = list[i];
8100Sstevel@tonic-gate 		while ((e = strchr(p, ',')) != NULL) {
8110Sstevel@tonic-gate 			*e = '\0';
8120Sstevel@tonic-gate 			if (__cpc_strhash_add(hash, p) == -1)
8130Sstevel@tonic-gate 				goto err;
8140Sstevel@tonic-gate 			p = e + 1;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		if (__cpc_strhash_add(hash, p) == -1)
8170Sstevel@tonic-gate 			goto err;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	while ((p = __cpc_strhash_next(hash)) != NULL)
8210Sstevel@tonic-gate 		action(arg, p);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate err:
8240Sstevel@tonic-gate 	__cpc_strhash_free(hash);
8250Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++)
8260Sstevel@tonic-gate 		free(list[i]);
8270Sstevel@tonic-gate 	free(list);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*ARGSUSED*/
8310Sstevel@tonic-gate void
8320Sstevel@tonic-gate cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
8330Sstevel@tonic-gate     void (*action)(void *arg, uint_t picno, const char *event))
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	char	*p;
8360Sstevel@tonic-gate 	char	*e;
8370Sstevel@tonic-gate 	char	*list;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (picno >= cpc->cpc_npic) {
8400Sstevel@tonic-gate 		errno = EINVAL;
8410Sstevel@tonic-gate 		return;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_evlist[picno])) == NULL)
8450Sstevel@tonic-gate 		return;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * List now points to a comma-separated list of events supported by
8490Sstevel@tonic-gate 	 * the designated pic.
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	p = list;
8520Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
8530Sstevel@tonic-gate 		*e = '\0';
8540Sstevel@tonic-gate 		action(arg, picno, p);
8550Sstevel@tonic-gate 		p = e + 1;
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 	action(arg, picno, p);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	free(list);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate /*ARGSUSED*/
8630Sstevel@tonic-gate void
8640Sstevel@tonic-gate cpc_walk_attrs(cpc_t *cpc, void *arg,
8650Sstevel@tonic-gate     void (*action)(void *arg, const char *attr))
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 	char	*p;
8680Sstevel@tonic-gate 	char	*e;
8690Sstevel@tonic-gate 	char	*list;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_attrlist)) == NULL)
8720Sstevel@tonic-gate 		return;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * Platforms with no attributes will return an empty string.
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	if (*list == '\0')
8780Sstevel@tonic-gate 		return;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	/*
8810Sstevel@tonic-gate 	 * List now points to a comma-separated list of attributes supported by
8820Sstevel@tonic-gate 	 * the underlying platform.
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	p = list;
8850Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
8860Sstevel@tonic-gate 		*e = '\0';
8870Sstevel@tonic-gate 		action(arg, p);
8880Sstevel@tonic-gate 		p = e + 1;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	action(arg, p);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	free(list);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*ARGSUSED*/
8960Sstevel@tonic-gate int
8970Sstevel@tonic-gate cpc_enable(cpc_t *cpc)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_ENABLE, -1, 0, 0, 0));
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate /*ARGSUSED*/
9030Sstevel@tonic-gate int
9040Sstevel@tonic-gate cpc_disable(cpc_t *cpc)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_DISABLE, -1, 0, 0, 0));
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*ARGSUSED*/
9100Sstevel@tonic-gate uint_t
9110Sstevel@tonic-gate cpc_npic(cpc_t *cpc)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	return (cpc->cpc_npic);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate /*ARGSUSED*/
9170Sstevel@tonic-gate uint_t
9180Sstevel@tonic-gate cpc_caps(cpc_t *cpc)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	return (cpc->cpc_caps);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate const char *
9240Sstevel@tonic-gate cpc_cciname(cpc_t *cpc)
9250Sstevel@tonic-gate {
9260Sstevel@tonic-gate 	return (cpc->cpc_cciname);
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate const char *
9300Sstevel@tonic-gate cpc_cpuref(cpc_t *cpc)
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate 	return (cpc->cpc_cpuref);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate int
9360Sstevel@tonic-gate cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	cpc->cpc_errfn = fn;
9390Sstevel@tonic-gate 	return (0);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * These strings may contain printf() conversion specifiers.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate static const char *errstr[] = {
9460Sstevel@tonic-gate "",						/* zero slot filler */
9470Sstevel@tonic-gate "Unknown event\n",				/* CPC_INVALID_EVENT */
9480Sstevel@tonic-gate "Invalid counter number\n",			/* CPC_INVALID_PICNUM */
9490Sstevel@tonic-gate "Unknown attribute\n",				/* CPC_INVALID_ATTRIBUTE */
9500Sstevel@tonic-gate "Attribute out of range\n",			/* CPC_ATTRIBUTE_OUT_OF_RANGE */
9510Sstevel@tonic-gate "Hardware resource unavailable\n",		/* CPC_RESOURCE_UNAVAIL */
9520Sstevel@tonic-gate "Counter cannot count requested event\n",	/* CPC_PIC_NOT_CAPABLE */
9530Sstevel@tonic-gate "Invalid flags in a request\n",			/* CPC_REQ_INVALID_FLAGS */
9540Sstevel@tonic-gate "Requests conflict with each other\n",		/* CPC_CONFLICTING_REQS */
9550Sstevel@tonic-gate "Attribute requires the cpc_cpu privilege\n",  /* CPC_ATTR_REQUIRES_PRIVILEGE */
956*3732Sae112802 "Couldn't bind LWP to requested processor\n",	/* CPC_PBIND_FAILED */
957*3732Sae112802 "Hypervisor event access denied\n"		/* CPC_HV_NO_ACCESS */
9580Sstevel@tonic-gate };
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /*VARARGS3*/
9610Sstevel@tonic-gate static void
9620Sstevel@tonic-gate cpc_err(cpc_t *cpc, const char *fn, int subcode, ...)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	va_list		ap;
9650Sstevel@tonic-gate 	const char	*str;
9660Sstevel@tonic-gate 	int		error;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/*
9690Sstevel@tonic-gate 	 * If subcode is -1, there is no specific description for this error.
9700Sstevel@tonic-gate 	 */
9710Sstevel@tonic-gate 	if (subcode == -1)
9720Sstevel@tonic-gate 		return;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * We need to preserve errno across calls to this function to prevent it
9760Sstevel@tonic-gate 	 * from being clobbered while here, or in the user's error handler.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	error = errno;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	str = dgettext(TEXT_DOMAIN, errstr[subcode]);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	va_start(ap, subcode);
9830Sstevel@tonic-gate 	if (cpc->cpc_errfn != NULL)
9840Sstevel@tonic-gate 		cpc->cpc_errfn(fn, subcode, str, ap);
9850Sstevel@tonic-gate 	else {
9860Sstevel@tonic-gate 		/*
9870Sstevel@tonic-gate 		 * If printf() conversion specifiers are added to the errstr[]
9880Sstevel@tonic-gate 		 * table, this call needs to be changed to vfprintf().
9890Sstevel@tonic-gate 		 */
9900Sstevel@tonic-gate 		(void) fprintf(stderr, "libcpc: %s: %s", fn, str);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	va_end(ap);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	errno = error;
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate  * Hook used by libpctx to alert libcpc when a pctx handle is going away.
9990Sstevel@tonic-gate  * This is necessary to prevent libcpc from attempting a libpctx operation on a
10000Sstevel@tonic-gate  * stale and invalid pctx_t handle. Since pctx_t's are cached by libcpc, we need
10010Sstevel@tonic-gate  * to be notified when they go away.
10020Sstevel@tonic-gate  */
10030Sstevel@tonic-gate static void
10040Sstevel@tonic-gate cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	cpc_set_t	*set;
10070Sstevel@tonic-gate 	int		sigblocked;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
10100Sstevel@tonic-gate 	for (set = cpc->cpc_sets; set != NULL; set = set->cs_next)
10110Sstevel@tonic-gate 		if (set->cs_pctx == pctx)
10120Sstevel@tonic-gate 			set->cs_pctx = NULL;
10130Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate  * Check that the set is valid; if so it will be in the cpc handle's
10180Sstevel@tonic-gate  * list of sets. The lock protects the list of sets, but not the set
10190Sstevel@tonic-gate  * itself.
10200Sstevel@tonic-gate  */
10210Sstevel@tonic-gate static int
10220Sstevel@tonic-gate cpc_set_valid(cpc_t *cpc, cpc_set_t *set)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	cpc_set_t	*csp;
10250Sstevel@tonic-gate 	int		sigblocked;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
10280Sstevel@tonic-gate 	for (csp = cpc->cpc_sets; csp != NULL; csp = csp->cs_next)
10290Sstevel@tonic-gate 		if (csp == set)
10300Sstevel@tonic-gate 			break;
10310Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
10320Sstevel@tonic-gate 	if (csp == NULL)
10330Sstevel@tonic-gate 		return (-1);
10340Sstevel@tonic-gate 	return (0);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate static int
10380Sstevel@tonic-gate cpc_lock(cpc_t *cpc)
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	int ret = (sigset(SIGEMT, SIG_HOLD) == SIG_HOLD);
10410Sstevel@tonic-gate 	(void) mutex_lock(&cpc->cpc_lock);
10420Sstevel@tonic-gate 	return (ret);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate static void
10460Sstevel@tonic-gate cpc_unlock(cpc_t *cpc, int sigblocked)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	(void) mutex_unlock(&cpc->cpc_lock);
10490Sstevel@tonic-gate 	if (sigblocked == 0)
10500Sstevel@tonic-gate 		(void) sigrelse(SIGEMT);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate struct priv {
10540Sstevel@tonic-gate 	const char *name;
10550Sstevel@tonic-gate 	int found;
10560Sstevel@tonic-gate };
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*ARGSUSED*/
10590Sstevel@tonic-gate static void
10600Sstevel@tonic-gate ev_walker(void *arg, uint_t picno, const char *ev)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, ev) == 0)
10630Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate static void
10670Sstevel@tonic-gate at_walker(void *arg, const char *at)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, at) == 0)
10700Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate static int
10740Sstevel@tonic-gate cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	pr.name = ev;
10790Sstevel@tonic-gate 	cpc_walk_events_pic(cpc, pic, &pr, ev_walker);
10802791Srab 	if (pr.found)
10812791Srab 		return (1);
10822791Srab 
10832791Srab 	/*
10842791Srab 	 * Before assuming this is an invalid event, see if we have been given
10852791Srab 	 * a raw event code. An event code of '0' is not recognized, as it
10862791Srab 	 * already has a corresponding event name in existing backends and it
10872791Srab 	 * is the only reasonable way to know if strtol() succeeded.
10882791Srab 	 */
10892791Srab 	if (strtol(ev, NULL, 0) != 0)
10902791Srab 		/*
10912791Srab 		 * Success - this is a valid raw code in hex, decimal, or octal.
10922791Srab 		 */
10932791Srab 		return (1);
10942791Srab 
10952791Srab 	return (0);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate static int
10990Sstevel@tonic-gate cpc_valid_attr(cpc_t *cpc, char *attr)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	pr.name = attr;
11040Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &pr, at_walker);
11050Sstevel@tonic-gate 	return (pr.found);
11060Sstevel@tonic-gate }
1107