xref: /onnv-gate/usr/src/lib/libcpc/common/libcpc.c (revision 11389)
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 /*
229460SKuriakose.Kuruvilla@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <libcpc.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <stropts.h>
330Sstevel@tonic-gate #include <libintl.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate #include <sys/syscall.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/processor.h>
380Sstevel@tonic-gate #include <sys/procset.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "libcpc_impl.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	MASK32 0xFFFFFFFF
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * The library uses the cpc_lock field of the cpc_t struct to protect access to
460Sstevel@tonic-gate  * the linked lists inside the cpc_t, and only the linked lists. It is NOT used
470Sstevel@tonic-gate  * to protect against a user shooting his/herself in the foot (such as, for
480Sstevel@tonic-gate  * instance, destroying the same set at the same time from different threads.).
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * SIGEMT needs to be blocked while holding the lock, to prevent deadlock among
510Sstevel@tonic-gate  * an app holding the lock and a signal handler attempting to sample or bind.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static char *cpc_get_list(int which, int arg);
550Sstevel@tonic-gate static void cpc_err(cpc_t *cpc, const char *fn, int subcode, ...);
560Sstevel@tonic-gate static int cpc_set_valid(cpc_t *cpc, cpc_set_t *set);
570Sstevel@tonic-gate static int cpc_lock(cpc_t *cpc);
580Sstevel@tonic-gate static void cpc_unlock(cpc_t *cpc, int blocked);
590Sstevel@tonic-gate static int cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev);
600Sstevel@tonic-gate static int cpc_valid_attr(cpc_t *cpc, char *attr);
610Sstevel@tonic-gate static void cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate cpc_t *
640Sstevel@tonic-gate cpc_open(int ver)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	cpc_t	*cpc;
670Sstevel@tonic-gate 	void	(*sigsaved)();
680Sstevel@tonic-gate 	int	error = 0;
690Sstevel@tonic-gate 	int	i;
700Sstevel@tonic-gate 	int	j;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (ver != CPC_VER_CURRENT) {
730Sstevel@tonic-gate 		/*
740Sstevel@tonic-gate 		 * v1 clients must stick to the v1 interface: cpc_version()
750Sstevel@tonic-gate 		 */
760Sstevel@tonic-gate 		errno = EINVAL;
770Sstevel@tonic-gate 		return (NULL);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * Call the syscall with invalid parameters.  If we get ENOSYS this CPU
820Sstevel@tonic-gate 	 * has no CPC support.  We need to block SIGSYS because the syscall code
830Sstevel@tonic-gate 	 * will send the signal if the system call fails to load.
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	sigsaved = signal(SIGSYS, SIG_IGN);
860Sstevel@tonic-gate 	if (syscall(SYS_cpc, -1, -1, -1, -1, -1) != -1) {
870Sstevel@tonic-gate 		(void) signal(SIGSYS, sigsaved);
880Sstevel@tonic-gate 		errno = EINVAL;
890Sstevel@tonic-gate 		return (NULL);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	error = errno;
920Sstevel@tonic-gate 	(void) signal(SIGSYS, sigsaved);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (error != EINVAL) {
950Sstevel@tonic-gate 		errno = error;
960Sstevel@tonic-gate 		return (NULL);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if ((cpc = malloc(sizeof (cpc_t))) == NULL) {
1000Sstevel@tonic-gate 		errno = ENOMEM;
1010Sstevel@tonic-gate 		return (NULL);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	cpc->cpc_npic = syscall(SYS_cpc, CPC_NPIC, -1, 0, 0, 0);
1050Sstevel@tonic-gate 	cpc->cpc_caps = syscall(SYS_cpc, CPC_CAPS, -1, 0, 0, 0);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_IMPL_NAME, -1, &cpc->cpc_cciname, 0, 0) != 0)
1080Sstevel@tonic-gate 		return (NULL);
1090Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_CPUREF, -1, &cpc->cpc_cpuref, 0, 0) != 0)
1100Sstevel@tonic-gate 		return (NULL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if ((cpc->cpc_attrlist = cpc_get_list(CPC_LIST_ATTRS, 0)) == NULL) {
1140Sstevel@tonic-gate 		free(cpc);
1150Sstevel@tonic-gate 		return (NULL);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if ((cpc->cpc_evlist = malloc(cpc->cpc_npic * sizeof (char *))) ==
1190Sstevel@tonic-gate 	    NULL) {
1200Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
1210Sstevel@tonic-gate 		free(cpc);
1220Sstevel@tonic-gate 		return (NULL);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	for (i = 0; i < cpc->cpc_npic; i++) {
1260Sstevel@tonic-gate 		if ((cpc->cpc_evlist[i] = cpc_get_list(CPC_LIST_EVENTS, i)) ==
1270Sstevel@tonic-gate 		    NULL)
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	if (i != cpc->cpc_npic) {
1310Sstevel@tonic-gate 		for (j = 0; j < i; j++)
1320Sstevel@tonic-gate 			free(cpc->cpc_evlist[j]);
1330Sstevel@tonic-gate 		free(cpc->cpc_evlist);
1340Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
1350Sstevel@tonic-gate 		free(cpc);
1360Sstevel@tonic-gate 		return (NULL);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	cpc->cpc_sets = NULL;
1400Sstevel@tonic-gate 	cpc->cpc_bufs = NULL;
1410Sstevel@tonic-gate 	cpc->cpc_errfn = NULL;
1420Sstevel@tonic-gate 	(void) mutex_init(&cpc->cpc_lock, USYNC_THREAD, NULL);
1430Sstevel@tonic-gate 	__pctx_cpc_register_callback(cpc_invalidate_pctx);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	return (cpc);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * Ensure state is cleaned up:
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * - Hardware is unbound
1520Sstevel@tonic-gate  * - Sets are all destroyed
1530Sstevel@tonic-gate  * - Bufs are all freed
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate int
1560Sstevel@tonic-gate cpc_close(cpc_t *cpc)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	while (cpc->cpc_sets != NULL) {
1590Sstevel@tonic-gate 		if (cpc->cpc_sets->cs_state != CS_UNBOUND)
1600Sstevel@tonic-gate 			(void) cpc_unbind(cpc, cpc->cpc_sets);
1610Sstevel@tonic-gate 		(void) cpc_set_destroy(cpc, cpc->cpc_sets);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	while (cpc->cpc_bufs != NULL)
1650Sstevel@tonic-gate 		(void) cpc_buf_destroy(cpc, cpc->cpc_bufs);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	free(cpc);
1680Sstevel@tonic-gate 	return (0);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
171*11389SAlexander.Kolbasov@Sun.COM /*
172*11389SAlexander.Kolbasov@Sun.COM  * Terminate everything that runs in pctx_run
173*11389SAlexander.Kolbasov@Sun.COM  */
174*11389SAlexander.Kolbasov@Sun.COM void
175*11389SAlexander.Kolbasov@Sun.COM cpc_terminate(cpc_t *cpc)
176*11389SAlexander.Kolbasov@Sun.COM {
177*11389SAlexander.Kolbasov@Sun.COM 	cpc_set_t	*csp;
178*11389SAlexander.Kolbasov@Sun.COM 	int		sigblocked;
179*11389SAlexander.Kolbasov@Sun.COM 
180*11389SAlexander.Kolbasov@Sun.COM 	sigblocked = cpc_lock(cpc);
181*11389SAlexander.Kolbasov@Sun.COM 	for (csp = cpc->cpc_sets; csp != NULL; csp = csp->cs_next) {
182*11389SAlexander.Kolbasov@Sun.COM 		if (csp->cs_pctx != NULL)
183*11389SAlexander.Kolbasov@Sun.COM 			pctx_terminate(csp->cs_pctx);
184*11389SAlexander.Kolbasov@Sun.COM 	}
185*11389SAlexander.Kolbasov@Sun.COM 	cpc_unlock(cpc, sigblocked);
186*11389SAlexander.Kolbasov@Sun.COM }
187*11389SAlexander.Kolbasov@Sun.COM 
1880Sstevel@tonic-gate cpc_set_t *
1890Sstevel@tonic-gate cpc_set_create(cpc_t *cpc)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	cpc_set_t	*set;
1920Sstevel@tonic-gate 	int		sigblocked;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if ((set = malloc(sizeof (*set))) == NULL) {
1950Sstevel@tonic-gate 		errno = ENOMEM;
1960Sstevel@tonic-gate 		return (NULL);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	set->cs_request = NULL;
2000Sstevel@tonic-gate 	set->cs_nreqs	= 0;
2010Sstevel@tonic-gate 	set->cs_state	= CS_UNBOUND;
2020Sstevel@tonic-gate 	set->cs_fd	= -1;
2030Sstevel@tonic-gate 	set->cs_pctx	= NULL;
2040Sstevel@tonic-gate 	set->cs_id	= -1;
2050Sstevel@tonic-gate 	set->cs_thr	= NULL;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
2080Sstevel@tonic-gate 	set->cs_next = cpc->cpc_sets;
2090Sstevel@tonic-gate 	cpc->cpc_sets = set;
2100Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	return (set);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate int
2160Sstevel@tonic-gate cpc_set_destroy(cpc_t *cpc, cpc_set_t *set)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	cpc_set_t	*csp, *prev;
2190Sstevel@tonic-gate 	cpc_request_t	*req, *next;
2200Sstevel@tonic-gate 	int		sigblocked;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * Remove this set from the cpc handle's list of sets.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
2260Sstevel@tonic-gate 	for (csp = prev = cpc->cpc_sets; csp != NULL; csp = csp->cs_next) {
2270Sstevel@tonic-gate 		if (csp == set)
2280Sstevel@tonic-gate 			break;
2290Sstevel@tonic-gate 		prev = csp;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	if (csp == NULL) {
2320Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
2330Sstevel@tonic-gate 		errno = EINVAL;
2340Sstevel@tonic-gate 		return (-1);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	if (csp == cpc->cpc_sets)
2370Sstevel@tonic-gate 		cpc->cpc_sets = csp->cs_next;
2380Sstevel@tonic-gate 	prev->cs_next = csp->cs_next;
2390Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (csp->cs_state != CS_UNBOUND)
2420Sstevel@tonic-gate 		(void) cpc_unbind(cpc, csp);
2430Sstevel@tonic-gate 
244*11389SAlexander.Kolbasov@Sun.COM 	/*
245*11389SAlexander.Kolbasov@Sun.COM 	 * Detach from the process
246*11389SAlexander.Kolbasov@Sun.COM 	 */
247*11389SAlexander.Kolbasov@Sun.COM 	if (csp->cs_pctx != NULL) {
248*11389SAlexander.Kolbasov@Sun.COM 		pctx_release(csp->cs_pctx);
249*11389SAlexander.Kolbasov@Sun.COM 		csp->cs_pctx = NULL;
250*11389SAlexander.Kolbasov@Sun.COM 	}
251*11389SAlexander.Kolbasov@Sun.COM 
2520Sstevel@tonic-gate 	for (req = csp->cs_request; req != NULL; req = next) {
2530Sstevel@tonic-gate 		next = req->cr_next;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		if (req->cr_nattrs != 0)
2560Sstevel@tonic-gate 			free(req->cr_attr);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		free(req);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	free(set);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	return (0);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*ARGSUSED*/
2680Sstevel@tonic-gate int
2690Sstevel@tonic-gate cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
2700Sstevel@tonic-gate     uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	cpc_request_t	*req;
2730Sstevel@tonic-gate 	const char	*fn = "cpc_set_add_request";
2740Sstevel@tonic-gate 	int		i;
2750Sstevel@tonic-gate 	int		npics = cpc_npic(cpc);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0 || set->cs_state != CS_UNBOUND) {
2780Sstevel@tonic-gate 		errno = EINVAL;
2790Sstevel@tonic-gate 		return (-1);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	for (i = 0; i < npics; i++)
2830Sstevel@tonic-gate 		if (cpc_valid_event(cpc, i, event))
2840Sstevel@tonic-gate 			break;
2850Sstevel@tonic-gate 	if (i == npics) {
2860Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_INVALID_EVENT);
2870Sstevel@tonic-gate 		errno = EINVAL;
2880Sstevel@tonic-gate 		return (-1);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if ((req = malloc(sizeof (*req))) == NULL) {
2920Sstevel@tonic-gate 		errno = ENOMEM;
2930Sstevel@tonic-gate 		return (-1);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	(void) strncpy(req->cr_event, event, CPC_MAX_EVENT_LEN);
2970Sstevel@tonic-gate 	req->cr_preset = preset;
2980Sstevel@tonic-gate 	req->cr_flags = flags;
2990Sstevel@tonic-gate 	req->cr_nattrs = nattrs;
3000Sstevel@tonic-gate 	req->cr_index = set->cs_nreqs;
3010Sstevel@tonic-gate 	req->cr_attr = NULL;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (nattrs != 0) {
3040Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * Verify that each attribute name is legal and valid.
3070Sstevel@tonic-gate 			 */
3080Sstevel@tonic-gate 			if (attrs[i].ca_name[0] == '\0' ||
3090Sstevel@tonic-gate 			    cpc_valid_attr(cpc, attrs[i].ca_name) == 0) {
3100Sstevel@tonic-gate 				cpc_err(cpc, fn, CPC_INVALID_ATTRIBUTE);
3110Sstevel@tonic-gate 				goto inval;
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 			/*
3150Sstevel@tonic-gate 			 * If the user requested a specific picnum, ensure that
3160Sstevel@tonic-gate 			 * the pic can count the requested event.
3170Sstevel@tonic-gate 			 */
3180Sstevel@tonic-gate 			if (strncmp("picnum", attrs[i].ca_name, 8) == 0) {
3190Sstevel@tonic-gate 				if (attrs[i].ca_val >= npics) {
3200Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_INVALID_PICNUM);
3210Sstevel@tonic-gate 					goto inval;
3220Sstevel@tonic-gate 				}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 				if (cpc_valid_event(cpc, attrs[i].ca_val,
3250Sstevel@tonic-gate 				    req->cr_event) == 0) {
3260Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_PIC_NOT_CAPABLE);
3270Sstevel@tonic-gate 					goto inval;
3280Sstevel@tonic-gate 				}
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		if ((req->cr_attr = malloc(nattrs * sizeof (kcpc_attr_t)))
3330Sstevel@tonic-gate 		    == NULL) {
3340Sstevel@tonic-gate 			free(req);
3350Sstevel@tonic-gate 			return (-1);
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
3390Sstevel@tonic-gate 			req->cr_attr[i].ka_val = attrs[i].ca_val;
3400Sstevel@tonic-gate 			(void) strncpy(req->cr_attr[i].ka_name,
3410Sstevel@tonic-gate 			    attrs[i].ca_name, CPC_MAX_ATTR_LEN);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	} else
3440Sstevel@tonic-gate 		req->cr_attr = NULL;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	req->cr_next = set->cs_request;
3470Sstevel@tonic-gate 	set->cs_request = req;
3480Sstevel@tonic-gate 	set->cs_nreqs++;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	return (req->cr_index);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate inval:
3530Sstevel@tonic-gate 	free(req);
3540Sstevel@tonic-gate 	errno = EINVAL;
3550Sstevel@tonic-gate 	return (-1);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate cpc_buf_t *
3590Sstevel@tonic-gate cpc_buf_create(cpc_t *cpc, cpc_set_t *set)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	cpc_buf_t	*buf;
3620Sstevel@tonic-gate 	int		sigblocked;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
3650Sstevel@tonic-gate 		errno = EINVAL;
3660Sstevel@tonic-gate 		return (NULL);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if ((buf = malloc(sizeof (*buf))) == NULL)
3700Sstevel@tonic-gate 		return (NULL);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	buf->cb_size = set->cs_nreqs * sizeof (uint64_t);
3730Sstevel@tonic-gate 	if ((buf->cb_data = malloc(buf->cb_size)) == NULL) {
3740Sstevel@tonic-gate 		free(buf);
3750Sstevel@tonic-gate 		return (NULL);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	buf->cb_hrtime = 0;
3810Sstevel@tonic-gate 	buf->cb_tick = 0;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
3840Sstevel@tonic-gate 	buf->cb_next = cpc->cpc_bufs;
3850Sstevel@tonic-gate 	cpc->cpc_bufs = buf;
3860Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (buf);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate int
3920Sstevel@tonic-gate cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	cpc_buf_t	*cbp, *prev;
3950Sstevel@tonic-gate 	int		sigblocked;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Remove this buf from the cpc handle's list of bufs.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
4010Sstevel@tonic-gate 	for (cbp = prev = cpc->cpc_bufs; cbp != NULL; cbp = cbp->cb_next) {
4020Sstevel@tonic-gate 		if (cbp == buf)
4030Sstevel@tonic-gate 			break;
4040Sstevel@tonic-gate 		prev = cbp;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	if (cbp == NULL) {
4070Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
4080Sstevel@tonic-gate 		errno = EINVAL;
4090Sstevel@tonic-gate 		return (-1);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	if (cbp == cpc->cpc_bufs)
4120Sstevel@tonic-gate 		cpc->cpc_bufs = cbp->cb_next;
4130Sstevel@tonic-gate 	prev->cb_next = cbp->cb_next;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
4160Sstevel@tonic-gate 	free(cbp->cb_data);
4170Sstevel@tonic-gate 	free(cbp);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	return (0);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*ARGSUSED*/
4230Sstevel@tonic-gate int
4240Sstevel@tonic-gate cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	char		*packed_set;
4270Sstevel@tonic-gate 	size_t		packsize;
4280Sstevel@tonic-gate 	int		ret;
4290Sstevel@tonic-gate 	int		subcode = -1;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * We don't bother checking cpc_set_valid() here, because this is in the
4330Sstevel@tonic-gate 	 * fast path of an app doing SIGEMT-based profiling as they restart the
4340Sstevel@tonic-gate 	 * counters from their signal handler.
4350Sstevel@tonic-gate 	 */
4360Sstevel@tonic-gate 	if (CPC_SET_VALID_FLAGS(flags) == 0 || set->cs_nreqs <= 0) {
4370Sstevel@tonic-gate 		errno = EINVAL;
4380Sstevel@tonic-gate 		return (-1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
4420Sstevel@tonic-gate 		errno = ENOMEM;
4430Sstevel@tonic-gate 		return (-1);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	ret = syscall(SYS_cpc, CPC_BIND, -1, packed_set, packsize, &subcode);
4470Sstevel@tonic-gate 	free(packed_set);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (ret != 0) {
4500Sstevel@tonic-gate 		if (subcode != -1)
4510Sstevel@tonic-gate 			cpc_err(cpc, "cpc_bind_curlwp", subcode);
4520Sstevel@tonic-gate 		return (-1);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	set->cs_thr = thr_self();
4560Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CURLWP;
4570Sstevel@tonic-gate 	return (ret);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*ARGSUSED*/
4610Sstevel@tonic-gate int
4620Sstevel@tonic-gate cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set, uint_t flags)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	char		*packed_set;
4650Sstevel@tonic-gate 	size_t		packsize;
4660Sstevel@tonic-gate 	int		ret;
4670Sstevel@tonic-gate 	int		subcode = -1;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * cpc_bind_pctx() currently has no valid flags.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
4730Sstevel@tonic-gate 		errno = EINVAL;
4740Sstevel@tonic-gate 		return (-1);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
4780Sstevel@tonic-gate 		errno = ENOMEM;
4790Sstevel@tonic-gate 		return (-1);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	ret = __pctx_cpc(pctx, cpc, CPC_BIND, id, packed_set, (void *)packsize,
4830Sstevel@tonic-gate 	    (void *)&subcode, -1);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	free(packed_set);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (ret == 0) {
4880Sstevel@tonic-gate 		set->cs_pctx = pctx;
4890Sstevel@tonic-gate 		set->cs_id = id;
4900Sstevel@tonic-gate 		set->cs_state = CS_BOUND_PCTX;
4910Sstevel@tonic-gate 	} else if (subcode != -1)
4920Sstevel@tonic-gate 		cpc_err(cpc, "cpc_bind_pctx", subcode);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	return (ret);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*ARGSUSED*/
4980Sstevel@tonic-gate int
4990Sstevel@tonic-gate cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set, uint_t flags)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	int		fd;
5020Sstevel@tonic-gate 	char		*packed_set;
5030Sstevel@tonic-gate 	size_t		packsize;
5040Sstevel@tonic-gate 	__cpc_args_t	cpc_args;
5050Sstevel@tonic-gate 	int		error;
5060Sstevel@tonic-gate 	const char	*fn = "cpc_bind_cpu";
5070Sstevel@tonic-gate 	int		subcode = -1;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * cpc_bind_cpu() currently has no valid flags.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
5130Sstevel@tonic-gate 		errno = EINVAL;
5140Sstevel@tonic-gate 		return (-1);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (processor_bind(P_LWPID, P_MYID, id, &set->cs_obind) == -1) {
5180Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_PBIND_FAILED);
5190Sstevel@tonic-gate 		return (-1);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if ((fd = open(CPUDRV_SHARED, O_RDWR)) < 0) {
5230Sstevel@tonic-gate 		error = errno;
5240Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5250Sstevel@tonic-gate 		errno = error;
5260Sstevel@tonic-gate 		return (-1);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * To avoid leaking file descriptors, if we find an existing fd here we
5310Sstevel@tonic-gate 	 * just close it. This is only a problem if a user attempts to bind the
5320Sstevel@tonic-gate 	 * same set to different CPUs without first unbinding it.
5330Sstevel@tonic-gate 	 */
5340Sstevel@tonic-gate 	if (set->cs_fd != -1)
5350Sstevel@tonic-gate 		(void) close(set->cs_fd);
5360Sstevel@tonic-gate 	set->cs_fd = fd;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
5390Sstevel@tonic-gate 		(void) close(fd);
5400Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5410Sstevel@tonic-gate 		errno = ENOMEM;
5420Sstevel@tonic-gate 		return (-1);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	cpc_args.udata1 = packed_set;
5460Sstevel@tonic-gate 	cpc_args.udata2 = (void *)packsize;
5470Sstevel@tonic-gate 	cpc_args.udata3 = (void *)&subcode;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (ioctl(fd, CPCIO_BIND, &cpc_args) != 0) {
5500Sstevel@tonic-gate 		error = errno;
5510Sstevel@tonic-gate 		free(packed_set);
5520Sstevel@tonic-gate 		(void) close(fd);
5530Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
5540Sstevel@tonic-gate 		if (subcode != -1)
5550Sstevel@tonic-gate 			cpc_err(cpc, fn, subcode);
5560Sstevel@tonic-gate 		errno = error;
5570Sstevel@tonic-gate 		return (-1);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	free(packed_set);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	set->cs_thr = thr_self();
5630Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CPU;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	return (0);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate /*ARGSUSED*/
5690Sstevel@tonic-gate int
5700Sstevel@tonic-gate cpc_request_preset(cpc_t *cpc, int index, uint64_t preset)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_PRESET, -1, index,
5730Sstevel@tonic-gate 	    (uint32_t)(preset >> 32), (uint32_t)(preset & MASK32)));
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate /*ARGSUSED*/
5770Sstevel@tonic-gate int
5780Sstevel@tonic-gate cpc_set_restart(cpc_t *cpc, cpc_set_t *set)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_RESTART, -1, 0, 0, 0));
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /*ARGSUSED*/
5840Sstevel@tonic-gate int
5850Sstevel@tonic-gate cpc_unbind(cpc_t *cpc, cpc_set_t *set)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	int		ret = 0;
5880Sstevel@tonic-gate 	int		error;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
5910Sstevel@tonic-gate 		errno = EINVAL;
5920Sstevel@tonic-gate 		return (-1);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	switch (set->cs_state) {
5960Sstevel@tonic-gate 	case CS_UNBOUND:
5970Sstevel@tonic-gate 		errno = EINVAL;
5980Sstevel@tonic-gate 		return (-1);
5990Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
6000Sstevel@tonic-gate 		ret = syscall(SYS_cpc, CPC_RELE, -1, 0, 0, 0);
6010Sstevel@tonic-gate 		error = errno;
6020Sstevel@tonic-gate 		break;
6030Sstevel@tonic-gate 	case CS_BOUND_CPU:
6040Sstevel@tonic-gate 		ret = ioctl(set->cs_fd, CPCIO_RELE, NULL);
6050Sstevel@tonic-gate 		error = errno;
6060Sstevel@tonic-gate 		(void) close(set->cs_fd);
6070Sstevel@tonic-gate 		set->cs_fd = -1;
6080Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
6090Sstevel@tonic-gate 		break;
6100Sstevel@tonic-gate 	case CS_BOUND_PCTX:
6110Sstevel@tonic-gate 		if (set->cs_pctx != NULL) {
6120Sstevel@tonic-gate 			ret = __pctx_cpc(set->cs_pctx, cpc, CPC_RELE,
6130Sstevel@tonic-gate 			    set->cs_id, 0, 0, 0, 0);
6140Sstevel@tonic-gate 			error = errno;
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 		break;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	set->cs_thr = NULL;
6200Sstevel@tonic-gate 	set->cs_id = -1;
6210Sstevel@tonic-gate 	set->cs_state = CS_UNBOUND;
6220Sstevel@tonic-gate 	if (ret != 0)
6230Sstevel@tonic-gate 		errno = error;
6240Sstevel@tonic-gate 	return (ret);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate /*ARGSUSED*/
6280Sstevel@tonic-gate int
6290Sstevel@tonic-gate cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	__cpc_args_t args;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/*
6340Sstevel@tonic-gate 	 * The following check ensures that only the most recently bound set
6350Sstevel@tonic-gate 	 * can be sampled, as binding a set invalidates all other sets in the
6360Sstevel@tonic-gate 	 * cpc_t.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	if (set->cs_state == CS_UNBOUND ||
6390Sstevel@tonic-gate 	    buf->cb_size != set->cs_nreqs * sizeof (uint64_t)) {
6400Sstevel@tonic-gate 		errno = EINVAL;
6410Sstevel@tonic-gate 		return (-1);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	switch (set->cs_state) {
6450Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
6460Sstevel@tonic-gate 		return (syscall(SYS_cpc, CPC_SAMPLE, -1, buf->cb_data,
6470Sstevel@tonic-gate 		    &buf->cb_hrtime, &buf->cb_tick));
6480Sstevel@tonic-gate 	case CS_BOUND_CPU:
6490Sstevel@tonic-gate 		args.udata1 = buf->cb_data;
6500Sstevel@tonic-gate 		args.udata2 = &buf->cb_hrtime;
6510Sstevel@tonic-gate 		args.udata3 = &buf->cb_tick;
6520Sstevel@tonic-gate 		return (ioctl(set->cs_fd, CPCIO_SAMPLE, &args));
6530Sstevel@tonic-gate 	case CS_BOUND_PCTX:
6540Sstevel@tonic-gate 		return (__pctx_cpc(set->cs_pctx, cpc, CPC_SAMPLE, set->cs_id,
6550Sstevel@tonic-gate 		    buf->cb_data, &buf->cb_hrtime, &buf->cb_tick,
6560Sstevel@tonic-gate 		    buf->cb_size));
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	errno = EINVAL;
6600Sstevel@tonic-gate 	return (-1);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*ARGSUSED*/
6640Sstevel@tonic-gate void
6650Sstevel@tonic-gate cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	int i;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
6700Sstevel@tonic-gate 		return;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
6730Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
6740Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick - b->cb_tick;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
6770Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] - b->cb_data[i];
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*ARGSUSED*/
6810Sstevel@tonic-gate void
6820Sstevel@tonic-gate cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	int i;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
6870Sstevel@tonic-gate 		return;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
6900Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
6910Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick + b->cb_tick;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
6940Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] + b->cb_data[i];
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate /*ARGSUSED*/
6980Sstevel@tonic-gate void
6990Sstevel@tonic-gate cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	if (ds->cb_size != src->cb_size)
7020Sstevel@tonic-gate 		return;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	bcopy(src->cb_data, ds->cb_data, ds->cb_size);
7050Sstevel@tonic-gate 	ds->cb_hrtime = src->cb_hrtime;
7060Sstevel@tonic-gate 	ds->cb_tick = src->cb_tick;
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*ARGSUSED*/
7100Sstevel@tonic-gate void
7110Sstevel@tonic-gate cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
7140Sstevel@tonic-gate 	buf->cb_hrtime = 0;
7150Sstevel@tonic-gate 	buf->cb_tick = 0;
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate  * Gets or sets the value of the request specified by index.
7200Sstevel@tonic-gate  */
7210Sstevel@tonic-gate /*ARGSUSED*/
7220Sstevel@tonic-gate int
7230Sstevel@tonic-gate cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val)
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate 	*val = buf->cb_data[index];
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	return (0);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate /*ARGSUSED*/
7310Sstevel@tonic-gate int
7320Sstevel@tonic-gate cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	buf->cb_data[index] = val;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	return (0);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*ARGSUSED*/
7400Sstevel@tonic-gate hrtime_t
7410Sstevel@tonic-gate cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	return (buf->cb_hrtime);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate /*ARGSUSED*/
7470Sstevel@tonic-gate uint64_t
7480Sstevel@tonic-gate cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	return (buf->cb_tick);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate static char *
7540Sstevel@tonic-gate cpc_get_list(int which, int arg)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	int	szcmd;
7570Sstevel@tonic-gate 	int	size;
7580Sstevel@tonic-gate 	char	*list;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (which == CPC_LIST_ATTRS)
7610Sstevel@tonic-gate 		szcmd = CPC_ATTRLIST_SIZE;
7620Sstevel@tonic-gate 	else
7630Sstevel@tonic-gate 		szcmd = CPC_EVLIST_SIZE;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (syscall(SYS_cpc, szcmd, -1, &size, arg, 0) != 0)
7660Sstevel@tonic-gate 		return (NULL);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if ((list = malloc(size)) == NULL)
7690Sstevel@tonic-gate 		return (NULL);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (syscall(SYS_cpc, which, -1, list, arg, 0) != 0) {
7720Sstevel@tonic-gate 		free(list);
7730Sstevel@tonic-gate 		return (NULL);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	return (list);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*ARGSUSED*/
7800Sstevel@tonic-gate void
7810Sstevel@tonic-gate cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
7820Sstevel@tonic-gate     void (*action)(void *arg, int index, const char *event, uint64_t preset,
7830Sstevel@tonic-gate 	uint_t flags, int nattrs, const cpc_attr_t *attrs))
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	cpc_request_t	*rp;
7860Sstevel@tonic-gate 	cpc_attr_t	*attrs = NULL;
7870Sstevel@tonic-gate 	int		i;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	for (rp = set->cs_request; rp != NULL; rp = rp->cr_next) {
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Need to reconstruct a temporary cpc_attr_t array for req.
7920Sstevel@tonic-gate 		 */
7930Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
7940Sstevel@tonic-gate 			if ((attrs = malloc(rp->cr_nattrs *
7950Sstevel@tonic-gate 			    sizeof (cpc_attr_t))) == NULL)
7960Sstevel@tonic-gate 				return;
7970Sstevel@tonic-gate 		for (i = 0; i < rp->cr_nattrs; i++) {
7980Sstevel@tonic-gate 			attrs[i].ca_name = rp->cr_attr[i].ka_name;
7990Sstevel@tonic-gate 			attrs[i].ca_val = rp->cr_attr[i].ka_val;
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		action(arg, rp->cr_index, rp->cr_event, rp->cr_preset,
8030Sstevel@tonic-gate 		    rp->cr_flags, rp->cr_nattrs, attrs);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
8060Sstevel@tonic-gate 			free(attrs);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*ARGSUSED*/
8119460SKuriakose.Kuruvilla@Sun.COM static void
8129460SKuriakose.Kuruvilla@Sun.COM cpc_walk_events_impl(cpc_t *cpc, void *arg,
8139460SKuriakose.Kuruvilla@Sun.COM     void (*action)(void *arg, const char *event), int is_generic)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	char		**list;
8160Sstevel@tonic-gate 	char		*p, *e;
8170Sstevel@tonic-gate 	int		i;
8189460SKuriakose.Kuruvilla@Sun.COM 	int		is_papi;
8197640SJonathan.Haslam@Sun.COM 	int		ncounters = cpc_npic(cpc);
8207640SJonathan.Haslam@Sun.COM 	cpc_strhash_t	*hash;
8217640SJonathan.Haslam@Sun.COM 
8227640SJonathan.Haslam@Sun.COM 	if ((list = malloc(ncounters * sizeof (char *))) == NULL)
8237640SJonathan.Haslam@Sun.COM 		return;
8247640SJonathan.Haslam@Sun.COM 
8257640SJonathan.Haslam@Sun.COM 	if ((hash = __cpc_strhash_alloc()) == NULL) {
8267640SJonathan.Haslam@Sun.COM 		free(list);
8277640SJonathan.Haslam@Sun.COM 		return;
8287640SJonathan.Haslam@Sun.COM 	}
8297640SJonathan.Haslam@Sun.COM 
8307640SJonathan.Haslam@Sun.COM 	for (i = 0; i < ncounters; i++) {
8317640SJonathan.Haslam@Sun.COM 		if ((list[i] = strdup(cpc->cpc_evlist[i])) == NULL)
8320Sstevel@tonic-gate 			goto err;
8337640SJonathan.Haslam@Sun.COM 		p = list[i];
8347640SJonathan.Haslam@Sun.COM 		while ((e = strchr(p, ',')) != NULL) {
8357640SJonathan.Haslam@Sun.COM 			*e = '\0';
8367640SJonathan.Haslam@Sun.COM 
8379460SKuriakose.Kuruvilla@Sun.COM 			/*
8389460SKuriakose.Kuruvilla@Sun.COM 			 * Based on is_generic flag, skip appropriate
8399460SKuriakose.Kuruvilla@Sun.COM 			 * event names.
8409460SKuriakose.Kuruvilla@Sun.COM 			 */
8419460SKuriakose.Kuruvilla@Sun.COM 			is_papi = (strncmp(p, "PAPI", 4) == 0);
8429460SKuriakose.Kuruvilla@Sun.COM 			if (is_generic != is_papi) {
8437640SJonathan.Haslam@Sun.COM 				p = e + 1;
8447640SJonathan.Haslam@Sun.COM 				continue;
8457640SJonathan.Haslam@Sun.COM 			}
8467640SJonathan.Haslam@Sun.COM 
8477640SJonathan.Haslam@Sun.COM 			if (__cpc_strhash_add(hash, p) == -1)
8487640SJonathan.Haslam@Sun.COM 				goto err;
8499460SKuriakose.Kuruvilla@Sun.COM 
8507640SJonathan.Haslam@Sun.COM 			p = e + 1;
8517640SJonathan.Haslam@Sun.COM 		}
8529460SKuriakose.Kuruvilla@Sun.COM 
8539460SKuriakose.Kuruvilla@Sun.COM 		is_papi = (strncmp(p, "PAPI", 4) == 0);
8549460SKuriakose.Kuruvilla@Sun.COM 		if (is_generic == is_papi) {
8557640SJonathan.Haslam@Sun.COM 			if (__cpc_strhash_add(hash, p) == -1)
8567640SJonathan.Haslam@Sun.COM 				goto err;
8577640SJonathan.Haslam@Sun.COM 		}
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	while ((p = __cpc_strhash_next(hash)) != NULL)
8610Sstevel@tonic-gate 		action(arg, p);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate err:
8640Sstevel@tonic-gate 	__cpc_strhash_free(hash);
8650Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++)
8660Sstevel@tonic-gate 		free(list[i]);
8670Sstevel@tonic-gate 	free(list);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*ARGSUSED*/
8710Sstevel@tonic-gate void
8729460SKuriakose.Kuruvilla@Sun.COM cpc_walk_events_all(cpc_t *cpc, void *arg,
8739460SKuriakose.Kuruvilla@Sun.COM 		    void (*action)(void *arg, const char *event))
8749460SKuriakose.Kuruvilla@Sun.COM {
8759460SKuriakose.Kuruvilla@Sun.COM 	cpc_walk_events_impl(cpc, arg, action, 0);
8769460SKuriakose.Kuruvilla@Sun.COM }
8779460SKuriakose.Kuruvilla@Sun.COM 
8789460SKuriakose.Kuruvilla@Sun.COM 
8799460SKuriakose.Kuruvilla@Sun.COM /*ARGSUSED*/
8809460SKuriakose.Kuruvilla@Sun.COM void
8819460SKuriakose.Kuruvilla@Sun.COM cpc_walk_generic_events_all(cpc_t *cpc, void *arg,
8829460SKuriakose.Kuruvilla@Sun.COM 			    void (*action)(void *arg, const char *event))
8839460SKuriakose.Kuruvilla@Sun.COM {
8849460SKuriakose.Kuruvilla@Sun.COM 	cpc_walk_events_impl(cpc, arg, action, 1);
8859460SKuriakose.Kuruvilla@Sun.COM }
8869460SKuriakose.Kuruvilla@Sun.COM 
8879460SKuriakose.Kuruvilla@Sun.COM /*ARGSUSED*/
8889460SKuriakose.Kuruvilla@Sun.COM static void
8899460SKuriakose.Kuruvilla@Sun.COM cpc_walk_events_pic_impl(cpc_t *cpc, uint_t picno, void *arg,
8909460SKuriakose.Kuruvilla@Sun.COM     void (*action)(void *arg, uint_t picno, const char *event), int is_generic)
8910Sstevel@tonic-gate {
8920Sstevel@tonic-gate 	char	*p;
8930Sstevel@tonic-gate 	char	*e;
8940Sstevel@tonic-gate 	char	*list;
8959460SKuriakose.Kuruvilla@Sun.COM 	int	is_papi;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	if (picno >= cpc->cpc_npic) {
8980Sstevel@tonic-gate 		errno = EINVAL;
8990Sstevel@tonic-gate 		return;
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_evlist[picno])) == NULL)
9030Sstevel@tonic-gate 		return;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/*
9060Sstevel@tonic-gate 	 * List now points to a comma-separated list of events supported by
9070Sstevel@tonic-gate 	 * the designated pic.
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 	p = list;
9100Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
9110Sstevel@tonic-gate 		*e = '\0';
9127640SJonathan.Haslam@Sun.COM 
9139460SKuriakose.Kuruvilla@Sun.COM 		/*
9149460SKuriakose.Kuruvilla@Sun.COM 		 * Based on is_generic flag, skip appropriate
9159460SKuriakose.Kuruvilla@Sun.COM 		 * event names.
9169460SKuriakose.Kuruvilla@Sun.COM 		 */
9179460SKuriakose.Kuruvilla@Sun.COM 		is_papi = (strncmp(p, "PAPI", 4) == 0);
9189460SKuriakose.Kuruvilla@Sun.COM 		if (is_generic != is_papi) {
9197640SJonathan.Haslam@Sun.COM 			p = e + 1;
9207640SJonathan.Haslam@Sun.COM 			continue;
9217640SJonathan.Haslam@Sun.COM 		}
9227640SJonathan.Haslam@Sun.COM 
9230Sstevel@tonic-gate 		action(arg, picno, p);
9240Sstevel@tonic-gate 		p = e + 1;
9250Sstevel@tonic-gate 	}
9267640SJonathan.Haslam@Sun.COM 
9279460SKuriakose.Kuruvilla@Sun.COM 	is_papi = (strncmp(p, "PAPI", 4) == 0);
9289460SKuriakose.Kuruvilla@Sun.COM 	if (is_generic == is_papi)
9299460SKuriakose.Kuruvilla@Sun.COM 		action(arg, picno, p);
9309460SKuriakose.Kuruvilla@Sun.COM 
9319460SKuriakose.Kuruvilla@Sun.COM 	free(list);
9329460SKuriakose.Kuruvilla@Sun.COM }
9337640SJonathan.Haslam@Sun.COM 
9349460SKuriakose.Kuruvilla@Sun.COM /*ARGSUSED*/
9359460SKuriakose.Kuruvilla@Sun.COM void
9369460SKuriakose.Kuruvilla@Sun.COM cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
9379460SKuriakose.Kuruvilla@Sun.COM     void (*action)(void *arg, uint_t picno, const char *event))
9389460SKuriakose.Kuruvilla@Sun.COM {
9399460SKuriakose.Kuruvilla@Sun.COM 	cpc_walk_events_pic_impl(cpc, picno, arg, action, 0);
9407640SJonathan.Haslam@Sun.COM }
9417640SJonathan.Haslam@Sun.COM 
9427640SJonathan.Haslam@Sun.COM /*ARGSUSED*/
9437640SJonathan.Haslam@Sun.COM void
9447640SJonathan.Haslam@Sun.COM cpc_walk_generic_events_pic(cpc_t *cpc, uint_t picno, void *arg,
9457640SJonathan.Haslam@Sun.COM     void (*action)(void *arg, uint_t picno, const char *event))
9467640SJonathan.Haslam@Sun.COM {
9479460SKuriakose.Kuruvilla@Sun.COM 	cpc_walk_events_pic_impl(cpc, picno, arg, action, 1);
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*ARGSUSED*/
9510Sstevel@tonic-gate void
9520Sstevel@tonic-gate cpc_walk_attrs(cpc_t *cpc, void *arg,
9530Sstevel@tonic-gate     void (*action)(void *arg, const char *attr))
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	char	*p;
9560Sstevel@tonic-gate 	char	*e;
9570Sstevel@tonic-gate 	char	*list;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_attrlist)) == NULL)
9600Sstevel@tonic-gate 		return;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Platforms with no attributes will return an empty string.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	if (*list == '\0')
9660Sstevel@tonic-gate 		return;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/*
9690Sstevel@tonic-gate 	 * List now points to a comma-separated list of attributes supported by
9700Sstevel@tonic-gate 	 * the underlying platform.
9710Sstevel@tonic-gate 	 */
9720Sstevel@tonic-gate 	p = list;
9730Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
9740Sstevel@tonic-gate 		*e = '\0';
9750Sstevel@tonic-gate 		action(arg, p);
9760Sstevel@tonic-gate 		p = e + 1;
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	action(arg, p);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	free(list);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /*ARGSUSED*/
9840Sstevel@tonic-gate int
9850Sstevel@tonic-gate cpc_enable(cpc_t *cpc)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_ENABLE, -1, 0, 0, 0));
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*ARGSUSED*/
9910Sstevel@tonic-gate int
9920Sstevel@tonic-gate cpc_disable(cpc_t *cpc)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_DISABLE, -1, 0, 0, 0));
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*ARGSUSED*/
9980Sstevel@tonic-gate uint_t
9990Sstevel@tonic-gate cpc_npic(cpc_t *cpc)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	return (cpc->cpc_npic);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate /*ARGSUSED*/
10050Sstevel@tonic-gate uint_t
10060Sstevel@tonic-gate cpc_caps(cpc_t *cpc)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate 	return (cpc->cpc_caps);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate const char *
10120Sstevel@tonic-gate cpc_cciname(cpc_t *cpc)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	return (cpc->cpc_cciname);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate const char *
10180Sstevel@tonic-gate cpc_cpuref(cpc_t *cpc)
10190Sstevel@tonic-gate {
10200Sstevel@tonic-gate 	return (cpc->cpc_cpuref);
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate int
10240Sstevel@tonic-gate cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	cpc->cpc_errfn = fn;
10270Sstevel@tonic-gate 	return (0);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate /*
10310Sstevel@tonic-gate  * These strings may contain printf() conversion specifiers.
10320Sstevel@tonic-gate  */
10330Sstevel@tonic-gate static const char *errstr[] = {
10340Sstevel@tonic-gate "",						/* zero slot filler */
10350Sstevel@tonic-gate "Unknown event\n",				/* CPC_INVALID_EVENT */
10360Sstevel@tonic-gate "Invalid counter number\n",			/* CPC_INVALID_PICNUM */
10370Sstevel@tonic-gate "Unknown attribute\n",				/* CPC_INVALID_ATTRIBUTE */
10380Sstevel@tonic-gate "Attribute out of range\n",			/* CPC_ATTRIBUTE_OUT_OF_RANGE */
10390Sstevel@tonic-gate "Hardware resource unavailable\n",		/* CPC_RESOURCE_UNAVAIL */
10400Sstevel@tonic-gate "Counter cannot count requested event\n",	/* CPC_PIC_NOT_CAPABLE */
10410Sstevel@tonic-gate "Invalid flags in a request\n",			/* CPC_REQ_INVALID_FLAGS */
10420Sstevel@tonic-gate "Requests conflict with each other\n",		/* CPC_CONFLICTING_REQS */
10430Sstevel@tonic-gate "Attribute requires the cpc_cpu privilege\n",  /* CPC_ATTR_REQUIRES_PRIVILEGE */
10443732Sae112802 "Couldn't bind LWP to requested processor\n",	/* CPC_PBIND_FAILED */
10453732Sae112802 "Hypervisor event access denied\n"		/* CPC_HV_NO_ACCESS */
10460Sstevel@tonic-gate };
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate /*VARARGS3*/
10490Sstevel@tonic-gate static void
10500Sstevel@tonic-gate cpc_err(cpc_t *cpc, const char *fn, int subcode, ...)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	va_list		ap;
10530Sstevel@tonic-gate 	const char	*str;
10540Sstevel@tonic-gate 	int		error;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * If subcode is -1, there is no specific description for this error.
10580Sstevel@tonic-gate 	 */
10590Sstevel@tonic-gate 	if (subcode == -1)
10600Sstevel@tonic-gate 		return;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	/*
10630Sstevel@tonic-gate 	 * We need to preserve errno across calls to this function to prevent it
10640Sstevel@tonic-gate 	 * from being clobbered while here, or in the user's error handler.
10650Sstevel@tonic-gate 	 */
10660Sstevel@tonic-gate 	error = errno;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	str = dgettext(TEXT_DOMAIN, errstr[subcode]);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	va_start(ap, subcode);
10710Sstevel@tonic-gate 	if (cpc->cpc_errfn != NULL)
10720Sstevel@tonic-gate 		cpc->cpc_errfn(fn, subcode, str, ap);
10730Sstevel@tonic-gate 	else {
10740Sstevel@tonic-gate 		/*
10750Sstevel@tonic-gate 		 * If printf() conversion specifiers are added to the errstr[]
10760Sstevel@tonic-gate 		 * table, this call needs to be changed to vfprintf().
10770Sstevel@tonic-gate 		 */
10780Sstevel@tonic-gate 		(void) fprintf(stderr, "libcpc: %s: %s", fn, str);
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 	va_end(ap);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	errno = error;
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * Hook used by libpctx to alert libcpc when a pctx handle is going away.
10870Sstevel@tonic-gate  * This is necessary to prevent libcpc from attempting a libpctx operation on a
10880Sstevel@tonic-gate  * stale and invalid pctx_t handle. Since pctx_t's are cached by libcpc, we need
10890Sstevel@tonic-gate  * to be notified when they go away.
10900Sstevel@tonic-gate  */
10910Sstevel@tonic-gate static void
10920Sstevel@tonic-gate cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx)
10930Sstevel@tonic-gate {
10940Sstevel@tonic-gate 	cpc_set_t	*set;
10950Sstevel@tonic-gate 	int		sigblocked;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
10980Sstevel@tonic-gate 	for (set = cpc->cpc_sets; set != NULL; set = set->cs_next)
10990Sstevel@tonic-gate 		if (set->cs_pctx == pctx)
11000Sstevel@tonic-gate 			set->cs_pctx = NULL;
11010Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate  * Check that the set is valid; if so it will be in the cpc handle's
11060Sstevel@tonic-gate  * list of sets. The lock protects the list of sets, but not the set
11070Sstevel@tonic-gate  * itself.
11080Sstevel@tonic-gate  */
11090Sstevel@tonic-gate static int
11100Sstevel@tonic-gate cpc_set_valid(cpc_t *cpc, cpc_set_t *set)
11110Sstevel@tonic-gate {
11120Sstevel@tonic-gate 	cpc_set_t	*csp;
11130Sstevel@tonic-gate 	int		sigblocked;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
11160Sstevel@tonic-gate 	for (csp = cpc->cpc_sets; csp != NULL; csp = csp->cs_next)
11170Sstevel@tonic-gate 		if (csp == set)
11180Sstevel@tonic-gate 			break;
11190Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
11200Sstevel@tonic-gate 	if (csp == NULL)
11210Sstevel@tonic-gate 		return (-1);
11220Sstevel@tonic-gate 	return (0);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate static int
11260Sstevel@tonic-gate cpc_lock(cpc_t *cpc)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate 	int ret = (sigset(SIGEMT, SIG_HOLD) == SIG_HOLD);
11290Sstevel@tonic-gate 	(void) mutex_lock(&cpc->cpc_lock);
11300Sstevel@tonic-gate 	return (ret);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate static void
11340Sstevel@tonic-gate cpc_unlock(cpc_t *cpc, int sigblocked)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 	(void) mutex_unlock(&cpc->cpc_lock);
11370Sstevel@tonic-gate 	if (sigblocked == 0)
11380Sstevel@tonic-gate 		(void) sigrelse(SIGEMT);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate struct priv {
11420Sstevel@tonic-gate 	const char *name;
11430Sstevel@tonic-gate 	int found;
11440Sstevel@tonic-gate };
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate /*ARGSUSED*/
11470Sstevel@tonic-gate static void
11480Sstevel@tonic-gate ev_walker(void *arg, uint_t picno, const char *ev)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, ev) == 0)
11510Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate static void
11550Sstevel@tonic-gate at_walker(void *arg, const char *at)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, at) == 0)
11580Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate static int
11620Sstevel@tonic-gate cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
11657120Svk226950 	char *end_ev;
11669460SKuriakose.Kuruvilla@Sun.COM 	int err;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	pr.name = ev;
11690Sstevel@tonic-gate 	cpc_walk_events_pic(cpc, pic, &pr, ev_walker);
11702791Srab 	if (pr.found)
11712791Srab 		return (1);
11722791Srab 
11737640SJonathan.Haslam@Sun.COM 	cpc_walk_generic_events_pic(cpc, pic, &pr, ev_walker);
11747640SJonathan.Haslam@Sun.COM 	if (pr.found)
11757640SJonathan.Haslam@Sun.COM 		return (1);
11767640SJonathan.Haslam@Sun.COM 
11772791Srab 	/*
11782791Srab 	 * Before assuming this is an invalid event, see if we have been given
11799460SKuriakose.Kuruvilla@Sun.COM 	 * a raw event code.
11807120Svk226950 	 * Check the second argument of strtol() to ensure invalid events
11817120Svk226950 	 * beginning with number do not go through.
11822791Srab 	 */
11839460SKuriakose.Kuruvilla@Sun.COM 	err = errno;
11849460SKuriakose.Kuruvilla@Sun.COM 	errno = 0;
11859460SKuriakose.Kuruvilla@Sun.COM 	(void) strtol(ev, &end_ev, 0);
11869460SKuriakose.Kuruvilla@Sun.COM 	if ((errno == 0) && (*end_ev == '\0')) {
11872791Srab 		/*
11882791Srab 		 * Success - this is a valid raw code in hex, decimal, or octal.
11892791Srab 		 */
11909460SKuriakose.Kuruvilla@Sun.COM 		errno = err;
11912791Srab 		return (1);
11929460SKuriakose.Kuruvilla@Sun.COM 	}
11932791Srab 
11949460SKuriakose.Kuruvilla@Sun.COM 	errno = err;
11952791Srab 	return (0);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate static int
11990Sstevel@tonic-gate cpc_valid_attr(cpc_t *cpc, char *attr)
12000Sstevel@tonic-gate {
12010Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	pr.name = attr;
12040Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &pr, at_walker);
12050Sstevel@tonic-gate 	return (pr.found);
12060Sstevel@tonic-gate }
1207