xref: /titanic_54/usr/src/lib/libcpc/common/libcpc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <libcpc.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <errno.h>
33*7c478bd9Sstevel@tonic-gate #include <strings.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <stropts.h>
36*7c478bd9Sstevel@tonic-gate #include <libintl.h>
37*7c478bd9Sstevel@tonic-gate #include <signal.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/processor.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "libcpc_impl.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	MASK32 0xFFFFFFFF
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * The library uses the cpc_lock field of the cpc_t struct to protect access to
49*7c478bd9Sstevel@tonic-gate  * the linked lists inside the cpc_t, and only the linked lists. It is NOT used
50*7c478bd9Sstevel@tonic-gate  * to protect against a user shooting his/herself in the foot (such as, for
51*7c478bd9Sstevel@tonic-gate  * instance, destroying the same set at the same time from different threads.).
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  * SIGEMT needs to be blocked while holding the lock, to prevent deadlock among
54*7c478bd9Sstevel@tonic-gate  * an app holding the lock and a signal handler attempting to sample or bind.
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static char *cpc_get_list(int which, int arg);
58*7c478bd9Sstevel@tonic-gate static void cpc_err(cpc_t *cpc, const char *fn, int subcode, ...);
59*7c478bd9Sstevel@tonic-gate static int cpc_set_valid(cpc_t *cpc, cpc_set_t *set);
60*7c478bd9Sstevel@tonic-gate static int cpc_lock(cpc_t *cpc);
61*7c478bd9Sstevel@tonic-gate static void cpc_unlock(cpc_t *cpc, int blocked);
62*7c478bd9Sstevel@tonic-gate static int cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev);
63*7c478bd9Sstevel@tonic-gate static int cpc_valid_attr(cpc_t *cpc, char *attr);
64*7c478bd9Sstevel@tonic-gate static void cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx);
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate cpc_t *
67*7c478bd9Sstevel@tonic-gate cpc_open(int ver)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	cpc_t	*cpc;
70*7c478bd9Sstevel@tonic-gate 	void	(*sigsaved)();
71*7c478bd9Sstevel@tonic-gate 	int	error = 0;
72*7c478bd9Sstevel@tonic-gate 	int	i;
73*7c478bd9Sstevel@tonic-gate 	int	j;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (ver != CPC_VER_CURRENT) {
76*7c478bd9Sstevel@tonic-gate 		/*
77*7c478bd9Sstevel@tonic-gate 		 * v1 clients must stick to the v1 interface: cpc_version()
78*7c478bd9Sstevel@tonic-gate 		 */
79*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
80*7c478bd9Sstevel@tonic-gate 		return (NULL);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	/*
84*7c478bd9Sstevel@tonic-gate 	 * Call the syscall with invalid parameters.  If we get ENOSYS this CPU
85*7c478bd9Sstevel@tonic-gate 	 * has no CPC support.  We need to block SIGSYS because the syscall code
86*7c478bd9Sstevel@tonic-gate 	 * will send the signal if the system call fails to load.
87*7c478bd9Sstevel@tonic-gate 	 */
88*7c478bd9Sstevel@tonic-gate 	sigsaved = signal(SIGSYS, SIG_IGN);
89*7c478bd9Sstevel@tonic-gate 	if (syscall(SYS_cpc, -1, -1, -1, -1, -1) != -1) {
90*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGSYS, sigsaved);
91*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
92*7c478bd9Sstevel@tonic-gate 		return (NULL);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 	error = errno;
95*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGSYS, sigsaved);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	if (error != EINVAL) {
98*7c478bd9Sstevel@tonic-gate 		errno = error;
99*7c478bd9Sstevel@tonic-gate 		return (NULL);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if ((cpc = malloc(sizeof (cpc_t))) == NULL) {
103*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
104*7c478bd9Sstevel@tonic-gate 		return (NULL);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	cpc->cpc_npic = syscall(SYS_cpc, CPC_NPIC, -1, 0, 0, 0);
108*7c478bd9Sstevel@tonic-gate 	cpc->cpc_caps = syscall(SYS_cpc, CPC_CAPS, -1, 0, 0, 0);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_IMPL_NAME, -1, &cpc->cpc_cciname, 0, 0) != 0)
111*7c478bd9Sstevel@tonic-gate 		return (NULL);
112*7c478bd9Sstevel@tonic-gate 	if (syscall(SYS_cpc, CPC_CPUREF, -1, &cpc->cpc_cpuref, 0, 0) != 0)
113*7c478bd9Sstevel@tonic-gate 		return (NULL);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if ((cpc->cpc_attrlist = cpc_get_list(CPC_LIST_ATTRS, 0)) == NULL) {
117*7c478bd9Sstevel@tonic-gate 		free(cpc);
118*7c478bd9Sstevel@tonic-gate 		return (NULL);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if ((cpc->cpc_evlist = malloc(cpc->cpc_npic * sizeof (char *))) ==
122*7c478bd9Sstevel@tonic-gate 	    NULL) {
123*7c478bd9Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
124*7c478bd9Sstevel@tonic-gate 		free(cpc);
125*7c478bd9Sstevel@tonic-gate 		return (NULL);
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < cpc->cpc_npic; i++) {
129*7c478bd9Sstevel@tonic-gate 		if ((cpc->cpc_evlist[i] = cpc_get_list(CPC_LIST_EVENTS, i)) ==
130*7c478bd9Sstevel@tonic-gate 		    NULL)
131*7c478bd9Sstevel@tonic-gate 			break;
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	if (i != cpc->cpc_npic) {
134*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < i; j++)
135*7c478bd9Sstevel@tonic-gate 			free(cpc->cpc_evlist[j]);
136*7c478bd9Sstevel@tonic-gate 		free(cpc->cpc_evlist);
137*7c478bd9Sstevel@tonic-gate 		free(cpc->cpc_attrlist);
138*7c478bd9Sstevel@tonic-gate 		free(cpc);
139*7c478bd9Sstevel@tonic-gate 		return (NULL);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	cpc->cpc_sets = NULL;
143*7c478bd9Sstevel@tonic-gate 	cpc->cpc_bufs = NULL;
144*7c478bd9Sstevel@tonic-gate 	cpc->cpc_errfn = NULL;
145*7c478bd9Sstevel@tonic-gate 	(void) mutex_init(&cpc->cpc_lock, USYNC_THREAD, NULL);
146*7c478bd9Sstevel@tonic-gate 	__pctx_cpc_register_callback(cpc_invalidate_pctx);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	return (cpc);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate  * Ensure state is cleaned up:
153*7c478bd9Sstevel@tonic-gate  *
154*7c478bd9Sstevel@tonic-gate  * - Hardware is unbound
155*7c478bd9Sstevel@tonic-gate  * - Sets are all destroyed
156*7c478bd9Sstevel@tonic-gate  * - Bufs are all freed
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate cpc_close(cpc_t *cpc)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	while (cpc->cpc_sets != NULL) {
162*7c478bd9Sstevel@tonic-gate 		if (cpc->cpc_sets->cs_state != CS_UNBOUND)
163*7c478bd9Sstevel@tonic-gate 			(void) cpc_unbind(cpc, cpc->cpc_sets);
164*7c478bd9Sstevel@tonic-gate 		(void) cpc_set_destroy(cpc, cpc->cpc_sets);
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	while (cpc->cpc_bufs != NULL)
168*7c478bd9Sstevel@tonic-gate 		(void) cpc_buf_destroy(cpc, cpc->cpc_bufs);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	free(cpc);
171*7c478bd9Sstevel@tonic-gate 	return (0);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate cpc_set_t *
175*7c478bd9Sstevel@tonic-gate cpc_set_create(cpc_t *cpc)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*set;
178*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if ((set = malloc(sizeof (*set))) == NULL) {
181*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
182*7c478bd9Sstevel@tonic-gate 		return (NULL);
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	set->cs_request = NULL;
186*7c478bd9Sstevel@tonic-gate 	set->cs_nreqs	= 0;
187*7c478bd9Sstevel@tonic-gate 	set->cs_state	= CS_UNBOUND;
188*7c478bd9Sstevel@tonic-gate 	set->cs_fd	= -1;
189*7c478bd9Sstevel@tonic-gate 	set->cs_pctx	= NULL;
190*7c478bd9Sstevel@tonic-gate 	set->cs_id	= -1;
191*7c478bd9Sstevel@tonic-gate 	set->cs_thr	= NULL;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
194*7c478bd9Sstevel@tonic-gate 	set->cs_next = cpc->cpc_sets;
195*7c478bd9Sstevel@tonic-gate 	cpc->cpc_sets = set;
196*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	return (set);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate int
202*7c478bd9Sstevel@tonic-gate cpc_set_destroy(cpc_t *cpc, cpc_set_t *set)
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*csp, *prev;
205*7c478bd9Sstevel@tonic-gate 	cpc_request_t	*req, *next;
206*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	/*
209*7c478bd9Sstevel@tonic-gate 	 * Remove this set from the cpc handle's list of sets.
210*7c478bd9Sstevel@tonic-gate 	 */
211*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
212*7c478bd9Sstevel@tonic-gate 	for (csp = prev = cpc->cpc_sets; csp != NULL; csp = csp->cs_next) {
213*7c478bd9Sstevel@tonic-gate 		if (csp == set)
214*7c478bd9Sstevel@tonic-gate 			break;
215*7c478bd9Sstevel@tonic-gate 		prev = csp;
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 	if (csp == NULL) {
218*7c478bd9Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
219*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
220*7c478bd9Sstevel@tonic-gate 		return (-1);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 	if (csp == cpc->cpc_sets)
223*7c478bd9Sstevel@tonic-gate 		cpc->cpc_sets = csp->cs_next;
224*7c478bd9Sstevel@tonic-gate 	prev->cs_next = csp->cs_next;
225*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if (csp->cs_state != CS_UNBOUND)
228*7c478bd9Sstevel@tonic-gate 		(void) cpc_unbind(cpc, csp);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	for (req = csp->cs_request; req != NULL; req = next) {
231*7c478bd9Sstevel@tonic-gate 		next = req->cr_next;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		if (req->cr_nattrs != 0)
234*7c478bd9Sstevel@tonic-gate 			free(req->cr_attr);
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		free(req);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	free(set);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	return (0);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
246*7c478bd9Sstevel@tonic-gate int
247*7c478bd9Sstevel@tonic-gate cpc_set_add_request(cpc_t *cpc, cpc_set_t *set, const char *event,
248*7c478bd9Sstevel@tonic-gate     uint64_t preset, uint_t flags, uint_t nattrs, const cpc_attr_t *attrs)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	cpc_request_t	*req;
251*7c478bd9Sstevel@tonic-gate 	const char	*fn = "cpc_set_add_request";
252*7c478bd9Sstevel@tonic-gate 	int		i;
253*7c478bd9Sstevel@tonic-gate 	int		npics = cpc_npic(cpc);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0 || set->cs_state != CS_UNBOUND) {
256*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
257*7c478bd9Sstevel@tonic-gate 		return (-1);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < npics; i++)
261*7c478bd9Sstevel@tonic-gate 		if (cpc_valid_event(cpc, i, event))
262*7c478bd9Sstevel@tonic-gate 			break;
263*7c478bd9Sstevel@tonic-gate 	if (i == npics) {
264*7c478bd9Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_INVALID_EVENT);
265*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
266*7c478bd9Sstevel@tonic-gate 		return (-1);
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if ((req = malloc(sizeof (*req))) == NULL) {
270*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
271*7c478bd9Sstevel@tonic-gate 		return (-1);
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	(void) strncpy(req->cr_event, event, CPC_MAX_EVENT_LEN);
275*7c478bd9Sstevel@tonic-gate 	req->cr_preset = preset;
276*7c478bd9Sstevel@tonic-gate 	req->cr_flags = flags;
277*7c478bd9Sstevel@tonic-gate 	req->cr_nattrs = nattrs;
278*7c478bd9Sstevel@tonic-gate 	req->cr_index = set->cs_nreqs;
279*7c478bd9Sstevel@tonic-gate 	req->cr_attr = NULL;
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	if (nattrs != 0) {
282*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
283*7c478bd9Sstevel@tonic-gate 			/*
284*7c478bd9Sstevel@tonic-gate 			 * Verify that each attribute name is legal and valid.
285*7c478bd9Sstevel@tonic-gate 			 */
286*7c478bd9Sstevel@tonic-gate 			if (attrs[i].ca_name[0] == '\0' ||
287*7c478bd9Sstevel@tonic-gate 			    cpc_valid_attr(cpc, attrs[i].ca_name) == 0) {
288*7c478bd9Sstevel@tonic-gate 				cpc_err(cpc, fn, CPC_INVALID_ATTRIBUTE);
289*7c478bd9Sstevel@tonic-gate 				goto inval;
290*7c478bd9Sstevel@tonic-gate 			}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 			/*
293*7c478bd9Sstevel@tonic-gate 			 * If the user requested a specific picnum, ensure that
294*7c478bd9Sstevel@tonic-gate 			 * the pic can count the requested event.
295*7c478bd9Sstevel@tonic-gate 			 */
296*7c478bd9Sstevel@tonic-gate 			if (strncmp("picnum", attrs[i].ca_name, 8) == 0) {
297*7c478bd9Sstevel@tonic-gate 				if (attrs[i].ca_val >= npics) {
298*7c478bd9Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_INVALID_PICNUM);
299*7c478bd9Sstevel@tonic-gate 					goto inval;
300*7c478bd9Sstevel@tonic-gate 				}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 				if (cpc_valid_event(cpc, attrs[i].ca_val,
303*7c478bd9Sstevel@tonic-gate 				    req->cr_event) == 0) {
304*7c478bd9Sstevel@tonic-gate 					cpc_err(cpc, fn, CPC_PIC_NOT_CAPABLE);
305*7c478bd9Sstevel@tonic-gate 					goto inval;
306*7c478bd9Sstevel@tonic-gate 				}
307*7c478bd9Sstevel@tonic-gate 			}
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 		if ((req->cr_attr = malloc(nattrs * sizeof (kcpc_attr_t)))
311*7c478bd9Sstevel@tonic-gate 		    == NULL) {
312*7c478bd9Sstevel@tonic-gate 			free(req);
313*7c478bd9Sstevel@tonic-gate 			return (-1);
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nattrs; i++) {
317*7c478bd9Sstevel@tonic-gate 			req->cr_attr[i].ka_val = attrs[i].ca_val;
318*7c478bd9Sstevel@tonic-gate 			(void) strncpy(req->cr_attr[i].ka_name,
319*7c478bd9Sstevel@tonic-gate 			    attrs[i].ca_name, CPC_MAX_ATTR_LEN);
320*7c478bd9Sstevel@tonic-gate 		}
321*7c478bd9Sstevel@tonic-gate 	} else
322*7c478bd9Sstevel@tonic-gate 		req->cr_attr = NULL;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	req->cr_next = set->cs_request;
325*7c478bd9Sstevel@tonic-gate 	set->cs_request = req;
326*7c478bd9Sstevel@tonic-gate 	set->cs_nreqs++;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	return (req->cr_index);
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate inval:
331*7c478bd9Sstevel@tonic-gate 	free(req);
332*7c478bd9Sstevel@tonic-gate 	errno = EINVAL;
333*7c478bd9Sstevel@tonic-gate 	return (-1);
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate cpc_buf_t *
337*7c478bd9Sstevel@tonic-gate cpc_buf_create(cpc_t *cpc, cpc_set_t *set)
338*7c478bd9Sstevel@tonic-gate {
339*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	*buf;
340*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
343*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
344*7c478bd9Sstevel@tonic-gate 		return (NULL);
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if ((buf = malloc(sizeof (*buf))) == NULL)
348*7c478bd9Sstevel@tonic-gate 		return (NULL);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	buf->cb_size = set->cs_nreqs * sizeof (uint64_t);
351*7c478bd9Sstevel@tonic-gate 	if ((buf->cb_data = malloc(buf->cb_size)) == NULL) {
352*7c478bd9Sstevel@tonic-gate 		free(buf);
353*7c478bd9Sstevel@tonic-gate 		return (NULL);
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	buf->cb_hrtime = 0;
359*7c478bd9Sstevel@tonic-gate 	buf->cb_tick = 0;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
362*7c478bd9Sstevel@tonic-gate 	buf->cb_next = cpc->cpc_bufs;
363*7c478bd9Sstevel@tonic-gate 	cpc->cpc_bufs = buf;
364*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	return (buf);
367*7c478bd9Sstevel@tonic-gate }
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate int
370*7c478bd9Sstevel@tonic-gate cpc_buf_destroy(cpc_t *cpc, cpc_buf_t *buf)
371*7c478bd9Sstevel@tonic-gate {
372*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	*cbp, *prev;
373*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	/*
376*7c478bd9Sstevel@tonic-gate 	 * Remove this buf from the cpc handle's list of bufs.
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
379*7c478bd9Sstevel@tonic-gate 	for (cbp = prev = cpc->cpc_bufs; cbp != NULL; cbp = cbp->cb_next) {
380*7c478bd9Sstevel@tonic-gate 		if (cbp == buf)
381*7c478bd9Sstevel@tonic-gate 			break;
382*7c478bd9Sstevel@tonic-gate 		prev = cbp;
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 	if (cbp == NULL) {
385*7c478bd9Sstevel@tonic-gate 		cpc_unlock(cpc, sigblocked);
386*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
387*7c478bd9Sstevel@tonic-gate 		return (-1);
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate 	if (cbp == cpc->cpc_bufs)
390*7c478bd9Sstevel@tonic-gate 		cpc->cpc_bufs = cbp->cb_next;
391*7c478bd9Sstevel@tonic-gate 	prev->cb_next = cbp->cb_next;
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
394*7c478bd9Sstevel@tonic-gate 	free(cbp->cb_data);
395*7c478bd9Sstevel@tonic-gate 	free(cbp);
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	return (0);
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
401*7c478bd9Sstevel@tonic-gate int
402*7c478bd9Sstevel@tonic-gate cpc_bind_curlwp(cpc_t *cpc, cpc_set_t *set, uint_t flags)
403*7c478bd9Sstevel@tonic-gate {
404*7c478bd9Sstevel@tonic-gate 	char		*packed_set;
405*7c478bd9Sstevel@tonic-gate 	size_t		packsize;
406*7c478bd9Sstevel@tonic-gate 	int		ret;
407*7c478bd9Sstevel@tonic-gate 	int		subcode = -1;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	/*
410*7c478bd9Sstevel@tonic-gate 	 * We don't bother checking cpc_set_valid() here, because this is in the
411*7c478bd9Sstevel@tonic-gate 	 * fast path of an app doing SIGEMT-based profiling as they restart the
412*7c478bd9Sstevel@tonic-gate 	 * counters from their signal handler.
413*7c478bd9Sstevel@tonic-gate 	 */
414*7c478bd9Sstevel@tonic-gate 	if (CPC_SET_VALID_FLAGS(flags) == 0 || set->cs_nreqs <= 0) {
415*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
416*7c478bd9Sstevel@tonic-gate 		return (-1);
417*7c478bd9Sstevel@tonic-gate 	}
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
420*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
421*7c478bd9Sstevel@tonic-gate 		return (-1);
422*7c478bd9Sstevel@tonic-gate 	}
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	ret = syscall(SYS_cpc, CPC_BIND, -1, packed_set, packsize, &subcode);
425*7c478bd9Sstevel@tonic-gate 	free(packed_set);
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	if (ret != 0) {
428*7c478bd9Sstevel@tonic-gate 		if (subcode != -1)
429*7c478bd9Sstevel@tonic-gate 			cpc_err(cpc, "cpc_bind_curlwp", subcode);
430*7c478bd9Sstevel@tonic-gate 		return (-1);
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	set->cs_thr = thr_self();
434*7c478bd9Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CURLWP;
435*7c478bd9Sstevel@tonic-gate 	return (ret);
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
439*7c478bd9Sstevel@tonic-gate int
440*7c478bd9Sstevel@tonic-gate cpc_bind_pctx(cpc_t *cpc, pctx_t *pctx, id_t id, cpc_set_t *set, uint_t flags)
441*7c478bd9Sstevel@tonic-gate {
442*7c478bd9Sstevel@tonic-gate 	char		*packed_set;
443*7c478bd9Sstevel@tonic-gate 	size_t		packsize;
444*7c478bd9Sstevel@tonic-gate 	int		ret;
445*7c478bd9Sstevel@tonic-gate 	int		subcode = -1;
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	/*
448*7c478bd9Sstevel@tonic-gate 	 * cpc_bind_pctx() currently has no valid flags.
449*7c478bd9Sstevel@tonic-gate 	 */
450*7c478bd9Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
451*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
452*7c478bd9Sstevel@tonic-gate 		return (-1);
453*7c478bd9Sstevel@tonic-gate 	}
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
456*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
457*7c478bd9Sstevel@tonic-gate 		return (-1);
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	ret = __pctx_cpc(pctx, cpc, CPC_BIND, id, packed_set, (void *)packsize,
461*7c478bd9Sstevel@tonic-gate 	    (void *)&subcode, -1);
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	free(packed_set);
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (ret == 0) {
466*7c478bd9Sstevel@tonic-gate 		set->cs_pctx = pctx;
467*7c478bd9Sstevel@tonic-gate 		set->cs_id = id;
468*7c478bd9Sstevel@tonic-gate 		set->cs_state = CS_BOUND_PCTX;
469*7c478bd9Sstevel@tonic-gate 	} else if (subcode != -1)
470*7c478bd9Sstevel@tonic-gate 		cpc_err(cpc, "cpc_bind_pctx", subcode);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	return (ret);
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
476*7c478bd9Sstevel@tonic-gate int
477*7c478bd9Sstevel@tonic-gate cpc_bind_cpu(cpc_t *cpc, processorid_t id, cpc_set_t *set, uint_t flags)
478*7c478bd9Sstevel@tonic-gate {
479*7c478bd9Sstevel@tonic-gate 	int		fd;
480*7c478bd9Sstevel@tonic-gate 	char		*packed_set;
481*7c478bd9Sstevel@tonic-gate 	size_t		packsize;
482*7c478bd9Sstevel@tonic-gate 	__cpc_args_t	cpc_args;
483*7c478bd9Sstevel@tonic-gate 	int		error;
484*7c478bd9Sstevel@tonic-gate 	const char	*fn = "cpc_bind_cpu";
485*7c478bd9Sstevel@tonic-gate 	int		subcode = -1;
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 	/*
488*7c478bd9Sstevel@tonic-gate 	 * cpc_bind_cpu() currently has no valid flags.
489*7c478bd9Sstevel@tonic-gate 	 */
490*7c478bd9Sstevel@tonic-gate 	if (flags != 0 || cpc_set_valid(cpc, set) != 0 || set->cs_nreqs <= 0) {
491*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
492*7c478bd9Sstevel@tonic-gate 		return (-1);
493*7c478bd9Sstevel@tonic-gate 	}
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	if (processor_bind(P_LWPID, P_MYID, id, &set->cs_obind) == -1) {
496*7c478bd9Sstevel@tonic-gate 		cpc_err(cpc, fn, CPC_PBIND_FAILED);
497*7c478bd9Sstevel@tonic-gate 		return (-1);
498*7c478bd9Sstevel@tonic-gate 	}
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	if ((fd = open(CPUDRV_SHARED, O_RDWR)) < 0) {
501*7c478bd9Sstevel@tonic-gate 		error = errno;
502*7c478bd9Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
503*7c478bd9Sstevel@tonic-gate 		errno = error;
504*7c478bd9Sstevel@tonic-gate 		return (-1);
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	/*
508*7c478bd9Sstevel@tonic-gate 	 * To avoid leaking file descriptors, if we find an existing fd here we
509*7c478bd9Sstevel@tonic-gate 	 * just close it. This is only a problem if a user attempts to bind the
510*7c478bd9Sstevel@tonic-gate 	 * same set to different CPUs without first unbinding it.
511*7c478bd9Sstevel@tonic-gate 	 */
512*7c478bd9Sstevel@tonic-gate 	if (set->cs_fd != -1)
513*7c478bd9Sstevel@tonic-gate 		(void) close(set->cs_fd);
514*7c478bd9Sstevel@tonic-gate 	set->cs_fd = fd;
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	if ((packed_set = __cpc_pack_set(set, flags, &packsize)) == NULL) {
517*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
518*7c478bd9Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
519*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
520*7c478bd9Sstevel@tonic-gate 		return (-1);
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	cpc_args.udata1 = packed_set;
524*7c478bd9Sstevel@tonic-gate 	cpc_args.udata2 = (void *)packsize;
525*7c478bd9Sstevel@tonic-gate 	cpc_args.udata3 = (void *)&subcode;
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, CPCIO_BIND, &cpc_args) != 0) {
528*7c478bd9Sstevel@tonic-gate 		error = errno;
529*7c478bd9Sstevel@tonic-gate 		free(packed_set);
530*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
531*7c478bd9Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
532*7c478bd9Sstevel@tonic-gate 		if (subcode != -1)
533*7c478bd9Sstevel@tonic-gate 			cpc_err(cpc, fn, subcode);
534*7c478bd9Sstevel@tonic-gate 		errno = error;
535*7c478bd9Sstevel@tonic-gate 		return (-1);
536*7c478bd9Sstevel@tonic-gate 	}
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	free(packed_set);
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	set->cs_thr = thr_self();
541*7c478bd9Sstevel@tonic-gate 	set->cs_state = CS_BOUND_CPU;
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	return (0);
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
547*7c478bd9Sstevel@tonic-gate int
548*7c478bd9Sstevel@tonic-gate cpc_request_preset(cpc_t *cpc, int index, uint64_t preset)
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_PRESET, -1, index,
551*7c478bd9Sstevel@tonic-gate 	    (uint32_t)(preset >> 32), (uint32_t)(preset & MASK32)));
552*7c478bd9Sstevel@tonic-gate }
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
555*7c478bd9Sstevel@tonic-gate int
556*7c478bd9Sstevel@tonic-gate cpc_set_restart(cpc_t *cpc, cpc_set_t *set)
557*7c478bd9Sstevel@tonic-gate {
558*7c478bd9Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_RESTART, -1, 0, 0, 0));
559*7c478bd9Sstevel@tonic-gate }
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
562*7c478bd9Sstevel@tonic-gate int
563*7c478bd9Sstevel@tonic-gate cpc_unbind(cpc_t *cpc, cpc_set_t *set)
564*7c478bd9Sstevel@tonic-gate {
565*7c478bd9Sstevel@tonic-gate 	int		ret = 0;
566*7c478bd9Sstevel@tonic-gate 	int		error;
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	if (cpc_set_valid(cpc, set) != 0) {
569*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
570*7c478bd9Sstevel@tonic-gate 		return (-1);
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	switch (set->cs_state) {
574*7c478bd9Sstevel@tonic-gate 	case CS_UNBOUND:
575*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
576*7c478bd9Sstevel@tonic-gate 		return (-1);
577*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
578*7c478bd9Sstevel@tonic-gate 		ret = syscall(SYS_cpc, CPC_RELE, -1, 0, 0, 0);
579*7c478bd9Sstevel@tonic-gate 		error = errno;
580*7c478bd9Sstevel@tonic-gate 		break;
581*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_CPU:
582*7c478bd9Sstevel@tonic-gate 		ret = ioctl(set->cs_fd, CPCIO_RELE, NULL);
583*7c478bd9Sstevel@tonic-gate 		error = errno;
584*7c478bd9Sstevel@tonic-gate 		(void) close(set->cs_fd);
585*7c478bd9Sstevel@tonic-gate 		set->cs_fd = -1;
586*7c478bd9Sstevel@tonic-gate 		(void) processor_bind(P_LWPID, P_MYID, set->cs_obind, NULL);
587*7c478bd9Sstevel@tonic-gate 		break;
588*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_PCTX:
589*7c478bd9Sstevel@tonic-gate 		if (set->cs_pctx != NULL) {
590*7c478bd9Sstevel@tonic-gate 			ret = __pctx_cpc(set->cs_pctx, cpc, CPC_RELE,
591*7c478bd9Sstevel@tonic-gate 			    set->cs_id, 0, 0, 0, 0);
592*7c478bd9Sstevel@tonic-gate 			error = errno;
593*7c478bd9Sstevel@tonic-gate 		}
594*7c478bd9Sstevel@tonic-gate 		break;
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 	set->cs_thr = NULL;
598*7c478bd9Sstevel@tonic-gate 	set->cs_id = -1;
599*7c478bd9Sstevel@tonic-gate 	set->cs_state = CS_UNBOUND;
600*7c478bd9Sstevel@tonic-gate 	if (ret != 0)
601*7c478bd9Sstevel@tonic-gate 		errno = error;
602*7c478bd9Sstevel@tonic-gate 	return (ret);
603*7c478bd9Sstevel@tonic-gate }
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
606*7c478bd9Sstevel@tonic-gate int
607*7c478bd9Sstevel@tonic-gate cpc_set_sample(cpc_t *cpc, cpc_set_t *set, cpc_buf_t *buf)
608*7c478bd9Sstevel@tonic-gate {
609*7c478bd9Sstevel@tonic-gate 	__cpc_args_t args;
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	/*
612*7c478bd9Sstevel@tonic-gate 	 * The following check ensures that only the most recently bound set
613*7c478bd9Sstevel@tonic-gate 	 * can be sampled, as binding a set invalidates all other sets in the
614*7c478bd9Sstevel@tonic-gate 	 * cpc_t.
615*7c478bd9Sstevel@tonic-gate 	 */
616*7c478bd9Sstevel@tonic-gate 	if (set->cs_state == CS_UNBOUND ||
617*7c478bd9Sstevel@tonic-gate 	    buf->cb_size != set->cs_nreqs * sizeof (uint64_t)) {
618*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
619*7c478bd9Sstevel@tonic-gate 		return (-1);
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	switch (set->cs_state) {
623*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_CURLWP:
624*7c478bd9Sstevel@tonic-gate 		return (syscall(SYS_cpc, CPC_SAMPLE, -1, buf->cb_data,
625*7c478bd9Sstevel@tonic-gate 		    &buf->cb_hrtime, &buf->cb_tick));
626*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_CPU:
627*7c478bd9Sstevel@tonic-gate 		args.udata1 = buf->cb_data;
628*7c478bd9Sstevel@tonic-gate 		args.udata2 = &buf->cb_hrtime;
629*7c478bd9Sstevel@tonic-gate 		args.udata3 = &buf->cb_tick;
630*7c478bd9Sstevel@tonic-gate 		return (ioctl(set->cs_fd, CPCIO_SAMPLE, &args));
631*7c478bd9Sstevel@tonic-gate 	case CS_BOUND_PCTX:
632*7c478bd9Sstevel@tonic-gate 		return (__pctx_cpc(set->cs_pctx, cpc, CPC_SAMPLE, set->cs_id,
633*7c478bd9Sstevel@tonic-gate 		    buf->cb_data, &buf->cb_hrtime, &buf->cb_tick,
634*7c478bd9Sstevel@tonic-gate 		    buf->cb_size));
635*7c478bd9Sstevel@tonic-gate 	}
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 	errno = EINVAL;
638*7c478bd9Sstevel@tonic-gate 	return (-1);
639*7c478bd9Sstevel@tonic-gate }
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
642*7c478bd9Sstevel@tonic-gate void
643*7c478bd9Sstevel@tonic-gate cpc_buf_sub(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
644*7c478bd9Sstevel@tonic-gate {
645*7c478bd9Sstevel@tonic-gate 	int i;
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
648*7c478bd9Sstevel@tonic-gate 		return;
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
651*7c478bd9Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
652*7c478bd9Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick - b->cb_tick;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
655*7c478bd9Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] - b->cb_data[i];
656*7c478bd9Sstevel@tonic-gate }
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
659*7c478bd9Sstevel@tonic-gate void
660*7c478bd9Sstevel@tonic-gate cpc_buf_add(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *a, cpc_buf_t *b)
661*7c478bd9Sstevel@tonic-gate {
662*7c478bd9Sstevel@tonic-gate 	int i;
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	if (a->cb_size != ds->cb_size || b->cb_size != ds->cb_size)
665*7c478bd9Sstevel@tonic-gate 		return;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	ds->cb_hrtime = (a->cb_hrtime > b->cb_hrtime) ?
668*7c478bd9Sstevel@tonic-gate 	    a->cb_hrtime : b->cb_hrtime;
669*7c478bd9Sstevel@tonic-gate 	ds->cb_tick = a->cb_tick + b->cb_tick;
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ds->cb_size / sizeof (uint64_t); i++)
672*7c478bd9Sstevel@tonic-gate 		ds->cb_data[i] = a->cb_data[i] + b->cb_data[i];
673*7c478bd9Sstevel@tonic-gate }
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
676*7c478bd9Sstevel@tonic-gate void
677*7c478bd9Sstevel@tonic-gate cpc_buf_copy(cpc_t *cpc, cpc_buf_t *ds, cpc_buf_t *src)
678*7c478bd9Sstevel@tonic-gate {
679*7c478bd9Sstevel@tonic-gate 	if (ds->cb_size != src->cb_size)
680*7c478bd9Sstevel@tonic-gate 		return;
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	bcopy(src->cb_data, ds->cb_data, ds->cb_size);
683*7c478bd9Sstevel@tonic-gate 	ds->cb_hrtime = src->cb_hrtime;
684*7c478bd9Sstevel@tonic-gate 	ds->cb_tick = src->cb_tick;
685*7c478bd9Sstevel@tonic-gate }
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
688*7c478bd9Sstevel@tonic-gate void
689*7c478bd9Sstevel@tonic-gate cpc_buf_zero(cpc_t *cpc, cpc_buf_t *buf)
690*7c478bd9Sstevel@tonic-gate {
691*7c478bd9Sstevel@tonic-gate 	bzero(buf->cb_data, buf->cb_size);
692*7c478bd9Sstevel@tonic-gate 	buf->cb_hrtime = 0;
693*7c478bd9Sstevel@tonic-gate 	buf->cb_tick = 0;
694*7c478bd9Sstevel@tonic-gate }
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate /*
697*7c478bd9Sstevel@tonic-gate  * Gets or sets the value of the request specified by index.
698*7c478bd9Sstevel@tonic-gate  */
699*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
700*7c478bd9Sstevel@tonic-gate int
701*7c478bd9Sstevel@tonic-gate cpc_buf_get(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t *val)
702*7c478bd9Sstevel@tonic-gate {
703*7c478bd9Sstevel@tonic-gate 	*val = buf->cb_data[index];
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	return (0);
706*7c478bd9Sstevel@tonic-gate }
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
709*7c478bd9Sstevel@tonic-gate int
710*7c478bd9Sstevel@tonic-gate cpc_buf_set(cpc_t *cpc, cpc_buf_t *buf, int index, uint64_t val)
711*7c478bd9Sstevel@tonic-gate {
712*7c478bd9Sstevel@tonic-gate 	buf->cb_data[index] = val;
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	return (0);
715*7c478bd9Sstevel@tonic-gate }
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
718*7c478bd9Sstevel@tonic-gate hrtime_t
719*7c478bd9Sstevel@tonic-gate cpc_buf_hrtime(cpc_t *cpc, cpc_buf_t *buf)
720*7c478bd9Sstevel@tonic-gate {
721*7c478bd9Sstevel@tonic-gate 	return (buf->cb_hrtime);
722*7c478bd9Sstevel@tonic-gate }
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
725*7c478bd9Sstevel@tonic-gate uint64_t
726*7c478bd9Sstevel@tonic-gate cpc_buf_tick(cpc_t *cpc, cpc_buf_t *buf)
727*7c478bd9Sstevel@tonic-gate {
728*7c478bd9Sstevel@tonic-gate 	return (buf->cb_tick);
729*7c478bd9Sstevel@tonic-gate }
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate static char *
732*7c478bd9Sstevel@tonic-gate cpc_get_list(int which, int arg)
733*7c478bd9Sstevel@tonic-gate {
734*7c478bd9Sstevel@tonic-gate 	int	szcmd;
735*7c478bd9Sstevel@tonic-gate 	int	size;
736*7c478bd9Sstevel@tonic-gate 	char	*list;
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	if (which == CPC_LIST_ATTRS)
739*7c478bd9Sstevel@tonic-gate 		szcmd = CPC_ATTRLIST_SIZE;
740*7c478bd9Sstevel@tonic-gate 	else
741*7c478bd9Sstevel@tonic-gate 		szcmd = CPC_EVLIST_SIZE;
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 	if (syscall(SYS_cpc, szcmd, -1, &size, arg, 0) != 0)
744*7c478bd9Sstevel@tonic-gate 		return (NULL);
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate 	if ((list = malloc(size)) == NULL)
747*7c478bd9Sstevel@tonic-gate 		return (NULL);
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	if (syscall(SYS_cpc, which, -1, list, arg, 0) != 0) {
750*7c478bd9Sstevel@tonic-gate 		free(list);
751*7c478bd9Sstevel@tonic-gate 		return (NULL);
752*7c478bd9Sstevel@tonic-gate 	}
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	return (list);
755*7c478bd9Sstevel@tonic-gate }
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
758*7c478bd9Sstevel@tonic-gate void
759*7c478bd9Sstevel@tonic-gate cpc_walk_requests(cpc_t *cpc, cpc_set_t *set, void *arg,
760*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, int index, const char *event, uint64_t preset,
761*7c478bd9Sstevel@tonic-gate 	uint_t flags, int nattrs, const cpc_attr_t *attrs))
762*7c478bd9Sstevel@tonic-gate {
763*7c478bd9Sstevel@tonic-gate 	cpc_request_t	*rp;
764*7c478bd9Sstevel@tonic-gate 	cpc_attr_t	*attrs = NULL;
765*7c478bd9Sstevel@tonic-gate 	int		i;
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	for (rp = set->cs_request; rp != NULL; rp = rp->cr_next) {
768*7c478bd9Sstevel@tonic-gate 		/*
769*7c478bd9Sstevel@tonic-gate 		 * Need to reconstruct a temporary cpc_attr_t array for req.
770*7c478bd9Sstevel@tonic-gate 		 */
771*7c478bd9Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
772*7c478bd9Sstevel@tonic-gate 			if ((attrs = malloc(rp->cr_nattrs *
773*7c478bd9Sstevel@tonic-gate 			    sizeof (cpc_attr_t))) == NULL)
774*7c478bd9Sstevel@tonic-gate 				return;
775*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < rp->cr_nattrs; i++) {
776*7c478bd9Sstevel@tonic-gate 			attrs[i].ca_name = rp->cr_attr[i].ka_name;
777*7c478bd9Sstevel@tonic-gate 			attrs[i].ca_val = rp->cr_attr[i].ka_val;
778*7c478bd9Sstevel@tonic-gate 		}
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 		action(arg, rp->cr_index, rp->cr_event, rp->cr_preset,
781*7c478bd9Sstevel@tonic-gate 		    rp->cr_flags, rp->cr_nattrs, attrs);
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 		if (rp->cr_nattrs != 0)
784*7c478bd9Sstevel@tonic-gate 			free(attrs);
785*7c478bd9Sstevel@tonic-gate 	}
786*7c478bd9Sstevel@tonic-gate }
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
789*7c478bd9Sstevel@tonic-gate void
790*7c478bd9Sstevel@tonic-gate cpc_walk_events_all(cpc_t *cpc, void *arg,
791*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *event))
792*7c478bd9Sstevel@tonic-gate {
793*7c478bd9Sstevel@tonic-gate 	char		**list;
794*7c478bd9Sstevel@tonic-gate 	char		*p, *e;
795*7c478bd9Sstevel@tonic-gate 	int		i;
796*7c478bd9Sstevel@tonic-gate 	int		ncounters = cpc_npic(cpc);
797*7c478bd9Sstevel@tonic-gate 	cpc_strhash_t	*hash;
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 	if ((list = malloc(ncounters * sizeof (char *))) == NULL)
800*7c478bd9Sstevel@tonic-gate 		return;
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	if ((hash = __cpc_strhash_alloc()) == NULL) {
803*7c478bd9Sstevel@tonic-gate 		free(list);
804*7c478bd9Sstevel@tonic-gate 		return;
805*7c478bd9Sstevel@tonic-gate 	}
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
808*7c478bd9Sstevel@tonic-gate 		if ((list[i] = strdup(cpc->cpc_evlist[i])) == NULL)
809*7c478bd9Sstevel@tonic-gate 			goto err;
810*7c478bd9Sstevel@tonic-gate 		p = list[i];
811*7c478bd9Sstevel@tonic-gate 		while ((e = strchr(p, ',')) != NULL) {
812*7c478bd9Sstevel@tonic-gate 			*e = '\0';
813*7c478bd9Sstevel@tonic-gate 			if (__cpc_strhash_add(hash, p) == -1)
814*7c478bd9Sstevel@tonic-gate 				goto err;
815*7c478bd9Sstevel@tonic-gate 			p = e + 1;
816*7c478bd9Sstevel@tonic-gate 		}
817*7c478bd9Sstevel@tonic-gate 		if (__cpc_strhash_add(hash, p) == -1)
818*7c478bd9Sstevel@tonic-gate 			goto err;
819*7c478bd9Sstevel@tonic-gate 	}
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 	while ((p = __cpc_strhash_next(hash)) != NULL)
822*7c478bd9Sstevel@tonic-gate 		action(arg, p);
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate err:
825*7c478bd9Sstevel@tonic-gate 	__cpc_strhash_free(hash);
826*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++)
827*7c478bd9Sstevel@tonic-gate 		free(list[i]);
828*7c478bd9Sstevel@tonic-gate 	free(list);
829*7c478bd9Sstevel@tonic-gate }
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
832*7c478bd9Sstevel@tonic-gate void
833*7c478bd9Sstevel@tonic-gate cpc_walk_events_pic(cpc_t *cpc, uint_t picno, void *arg,
834*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, uint_t picno, const char *event))
835*7c478bd9Sstevel@tonic-gate {
836*7c478bd9Sstevel@tonic-gate 	char	*p;
837*7c478bd9Sstevel@tonic-gate 	char	*e;
838*7c478bd9Sstevel@tonic-gate 	char	*list;
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	if (picno >= cpc->cpc_npic) {
841*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
842*7c478bd9Sstevel@tonic-gate 		return;
843*7c478bd9Sstevel@tonic-gate 	}
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_evlist[picno])) == NULL)
846*7c478bd9Sstevel@tonic-gate 		return;
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	/*
849*7c478bd9Sstevel@tonic-gate 	 * List now points to a comma-separated list of events supported by
850*7c478bd9Sstevel@tonic-gate 	 * the designated pic.
851*7c478bd9Sstevel@tonic-gate 	 */
852*7c478bd9Sstevel@tonic-gate 	p = list;
853*7c478bd9Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
854*7c478bd9Sstevel@tonic-gate 		*e = '\0';
855*7c478bd9Sstevel@tonic-gate 		action(arg, picno, p);
856*7c478bd9Sstevel@tonic-gate 		p = e + 1;
857*7c478bd9Sstevel@tonic-gate 	}
858*7c478bd9Sstevel@tonic-gate 	action(arg, picno, p);
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 	free(list);
861*7c478bd9Sstevel@tonic-gate }
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
864*7c478bd9Sstevel@tonic-gate void
865*7c478bd9Sstevel@tonic-gate cpc_walk_attrs(cpc_t *cpc, void *arg,
866*7c478bd9Sstevel@tonic-gate     void (*action)(void *arg, const char *attr))
867*7c478bd9Sstevel@tonic-gate {
868*7c478bd9Sstevel@tonic-gate 	char	*p;
869*7c478bd9Sstevel@tonic-gate 	char	*e;
870*7c478bd9Sstevel@tonic-gate 	char	*list;
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate 	if ((list = strdup(cpc->cpc_attrlist)) == NULL)
873*7c478bd9Sstevel@tonic-gate 		return;
874*7c478bd9Sstevel@tonic-gate 
875*7c478bd9Sstevel@tonic-gate 	/*
876*7c478bd9Sstevel@tonic-gate 	 * Platforms with no attributes will return an empty string.
877*7c478bd9Sstevel@tonic-gate 	 */
878*7c478bd9Sstevel@tonic-gate 	if (*list == '\0')
879*7c478bd9Sstevel@tonic-gate 		return;
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	/*
882*7c478bd9Sstevel@tonic-gate 	 * List now points to a comma-separated list of attributes supported by
883*7c478bd9Sstevel@tonic-gate 	 * the underlying platform.
884*7c478bd9Sstevel@tonic-gate 	 */
885*7c478bd9Sstevel@tonic-gate 	p = list;
886*7c478bd9Sstevel@tonic-gate 	while ((e = strchr(p, ',')) != NULL) {
887*7c478bd9Sstevel@tonic-gate 		*e = '\0';
888*7c478bd9Sstevel@tonic-gate 		action(arg, p);
889*7c478bd9Sstevel@tonic-gate 		p = e + 1;
890*7c478bd9Sstevel@tonic-gate 	}
891*7c478bd9Sstevel@tonic-gate 	action(arg, p);
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	free(list);
894*7c478bd9Sstevel@tonic-gate }
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
897*7c478bd9Sstevel@tonic-gate int
898*7c478bd9Sstevel@tonic-gate cpc_enable(cpc_t *cpc)
899*7c478bd9Sstevel@tonic-gate {
900*7c478bd9Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_ENABLE, -1, 0, 0, 0));
901*7c478bd9Sstevel@tonic-gate }
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
904*7c478bd9Sstevel@tonic-gate int
905*7c478bd9Sstevel@tonic-gate cpc_disable(cpc_t *cpc)
906*7c478bd9Sstevel@tonic-gate {
907*7c478bd9Sstevel@tonic-gate 	return (syscall(SYS_cpc, CPC_DISABLE, -1, 0, 0, 0));
908*7c478bd9Sstevel@tonic-gate }
909*7c478bd9Sstevel@tonic-gate 
910*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
911*7c478bd9Sstevel@tonic-gate uint_t
912*7c478bd9Sstevel@tonic-gate cpc_npic(cpc_t *cpc)
913*7c478bd9Sstevel@tonic-gate {
914*7c478bd9Sstevel@tonic-gate 	return (cpc->cpc_npic);
915*7c478bd9Sstevel@tonic-gate }
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
918*7c478bd9Sstevel@tonic-gate uint_t
919*7c478bd9Sstevel@tonic-gate cpc_caps(cpc_t *cpc)
920*7c478bd9Sstevel@tonic-gate {
921*7c478bd9Sstevel@tonic-gate 	return (cpc->cpc_caps);
922*7c478bd9Sstevel@tonic-gate }
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate const char *
925*7c478bd9Sstevel@tonic-gate cpc_cciname(cpc_t *cpc)
926*7c478bd9Sstevel@tonic-gate {
927*7c478bd9Sstevel@tonic-gate 	return (cpc->cpc_cciname);
928*7c478bd9Sstevel@tonic-gate }
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate const char *
931*7c478bd9Sstevel@tonic-gate cpc_cpuref(cpc_t *cpc)
932*7c478bd9Sstevel@tonic-gate {
933*7c478bd9Sstevel@tonic-gate 	return (cpc->cpc_cpuref);
934*7c478bd9Sstevel@tonic-gate }
935*7c478bd9Sstevel@tonic-gate 
936*7c478bd9Sstevel@tonic-gate int
937*7c478bd9Sstevel@tonic-gate cpc_seterrhndlr(cpc_t *cpc, cpc_errhndlr_t *fn)
938*7c478bd9Sstevel@tonic-gate {
939*7c478bd9Sstevel@tonic-gate 	cpc->cpc_errfn = fn;
940*7c478bd9Sstevel@tonic-gate 	return (0);
941*7c478bd9Sstevel@tonic-gate }
942*7c478bd9Sstevel@tonic-gate 
943*7c478bd9Sstevel@tonic-gate /*
944*7c478bd9Sstevel@tonic-gate  * These strings may contain printf() conversion specifiers.
945*7c478bd9Sstevel@tonic-gate  */
946*7c478bd9Sstevel@tonic-gate static const char *errstr[] = {
947*7c478bd9Sstevel@tonic-gate "",						/* zero slot filler */
948*7c478bd9Sstevel@tonic-gate "Unknown event\n",				/* CPC_INVALID_EVENT */
949*7c478bd9Sstevel@tonic-gate "Invalid counter number\n",			/* CPC_INVALID_PICNUM */
950*7c478bd9Sstevel@tonic-gate "Unknown attribute\n",				/* CPC_INVALID_ATTRIBUTE */
951*7c478bd9Sstevel@tonic-gate "Attribute out of range\n",			/* CPC_ATTRIBUTE_OUT_OF_RANGE */
952*7c478bd9Sstevel@tonic-gate "Hardware resource unavailable\n",		/* CPC_RESOURCE_UNAVAIL */
953*7c478bd9Sstevel@tonic-gate "Counter cannot count requested event\n",	/* CPC_PIC_NOT_CAPABLE */
954*7c478bd9Sstevel@tonic-gate "Invalid flags in a request\n",			/* CPC_REQ_INVALID_FLAGS */
955*7c478bd9Sstevel@tonic-gate "Requests conflict with each other\n",		/* CPC_CONFLICTING_REQS */
956*7c478bd9Sstevel@tonic-gate "Attribute requires the cpc_cpu privilege\n",  /* CPC_ATTR_REQUIRES_PRIVILEGE */
957*7c478bd9Sstevel@tonic-gate "Couldn't bind LWP to requested processor\n"	/* CPC_PBIND_FAILED */
958*7c478bd9Sstevel@tonic-gate };
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate /*VARARGS3*/
961*7c478bd9Sstevel@tonic-gate static void
962*7c478bd9Sstevel@tonic-gate cpc_err(cpc_t *cpc, const char *fn, int subcode, ...)
963*7c478bd9Sstevel@tonic-gate {
964*7c478bd9Sstevel@tonic-gate 	va_list		ap;
965*7c478bd9Sstevel@tonic-gate 	const char	*str;
966*7c478bd9Sstevel@tonic-gate 	int		error;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	/*
969*7c478bd9Sstevel@tonic-gate 	 * If subcode is -1, there is no specific description for this error.
970*7c478bd9Sstevel@tonic-gate 	 */
971*7c478bd9Sstevel@tonic-gate 	if (subcode == -1)
972*7c478bd9Sstevel@tonic-gate 		return;
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	/*
975*7c478bd9Sstevel@tonic-gate 	 * We need to preserve errno across calls to this function to prevent it
976*7c478bd9Sstevel@tonic-gate 	 * from being clobbered while here, or in the user's error handler.
977*7c478bd9Sstevel@tonic-gate 	 */
978*7c478bd9Sstevel@tonic-gate 	error = errno;
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 	str = dgettext(TEXT_DOMAIN, errstr[subcode]);
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate 	va_start(ap, subcode);
983*7c478bd9Sstevel@tonic-gate 	if (cpc->cpc_errfn != NULL)
984*7c478bd9Sstevel@tonic-gate 		cpc->cpc_errfn(fn, subcode, str, ap);
985*7c478bd9Sstevel@tonic-gate 	else {
986*7c478bd9Sstevel@tonic-gate 		/*
987*7c478bd9Sstevel@tonic-gate 		 * If printf() conversion specifiers are added to the errstr[]
988*7c478bd9Sstevel@tonic-gate 		 * table, this call needs to be changed to vfprintf().
989*7c478bd9Sstevel@tonic-gate 		 */
990*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "libcpc: %s: %s", fn, str);
991*7c478bd9Sstevel@tonic-gate 	}
992*7c478bd9Sstevel@tonic-gate 	va_end(ap);
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 	errno = error;
995*7c478bd9Sstevel@tonic-gate }
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate /*
998*7c478bd9Sstevel@tonic-gate  * Hook used by libpctx to alert libcpc when a pctx handle is going away.
999*7c478bd9Sstevel@tonic-gate  * This is necessary to prevent libcpc from attempting a libpctx operation on a
1000*7c478bd9Sstevel@tonic-gate  * stale and invalid pctx_t handle. Since pctx_t's are cached by libcpc, we need
1001*7c478bd9Sstevel@tonic-gate  * to be notified when they go away.
1002*7c478bd9Sstevel@tonic-gate  */
1003*7c478bd9Sstevel@tonic-gate static void
1004*7c478bd9Sstevel@tonic-gate cpc_invalidate_pctx(cpc_t *cpc, pctx_t *pctx)
1005*7c478bd9Sstevel@tonic-gate {
1006*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*set;
1007*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
1010*7c478bd9Sstevel@tonic-gate 	for (set = cpc->cpc_sets; set != NULL; set = set->cs_next)
1011*7c478bd9Sstevel@tonic-gate 		if (set->cs_pctx == pctx)
1012*7c478bd9Sstevel@tonic-gate 			set->cs_pctx = NULL;
1013*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
1014*7c478bd9Sstevel@tonic-gate }
1015*7c478bd9Sstevel@tonic-gate 
1016*7c478bd9Sstevel@tonic-gate /*
1017*7c478bd9Sstevel@tonic-gate  * Check that the set is valid; if so it will be in the cpc handle's
1018*7c478bd9Sstevel@tonic-gate  * list of sets. The lock protects the list of sets, but not the set
1019*7c478bd9Sstevel@tonic-gate  * itself.
1020*7c478bd9Sstevel@tonic-gate  */
1021*7c478bd9Sstevel@tonic-gate static int
1022*7c478bd9Sstevel@tonic-gate cpc_set_valid(cpc_t *cpc, cpc_set_t *set)
1023*7c478bd9Sstevel@tonic-gate {
1024*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*csp;
1025*7c478bd9Sstevel@tonic-gate 	int		sigblocked;
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate 	sigblocked = cpc_lock(cpc);
1028*7c478bd9Sstevel@tonic-gate 	for (csp = cpc->cpc_sets; csp != NULL; csp = csp->cs_next)
1029*7c478bd9Sstevel@tonic-gate 		if (csp == set)
1030*7c478bd9Sstevel@tonic-gate 			break;
1031*7c478bd9Sstevel@tonic-gate 	cpc_unlock(cpc, sigblocked);
1032*7c478bd9Sstevel@tonic-gate 	if (csp == NULL)
1033*7c478bd9Sstevel@tonic-gate 		return (-1);
1034*7c478bd9Sstevel@tonic-gate 	return (0);
1035*7c478bd9Sstevel@tonic-gate }
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate static int
1038*7c478bd9Sstevel@tonic-gate cpc_lock(cpc_t *cpc)
1039*7c478bd9Sstevel@tonic-gate {
1040*7c478bd9Sstevel@tonic-gate 	int ret = (sigset(SIGEMT, SIG_HOLD) == SIG_HOLD);
1041*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cpc->cpc_lock);
1042*7c478bd9Sstevel@tonic-gate 	return (ret);
1043*7c478bd9Sstevel@tonic-gate }
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate static void
1046*7c478bd9Sstevel@tonic-gate cpc_unlock(cpc_t *cpc, int sigblocked)
1047*7c478bd9Sstevel@tonic-gate {
1048*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cpc->cpc_lock);
1049*7c478bd9Sstevel@tonic-gate 	if (sigblocked == 0)
1050*7c478bd9Sstevel@tonic-gate 		(void) sigrelse(SIGEMT);
1051*7c478bd9Sstevel@tonic-gate }
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate struct priv {
1054*7c478bd9Sstevel@tonic-gate 	const char *name;
1055*7c478bd9Sstevel@tonic-gate 	int found;
1056*7c478bd9Sstevel@tonic-gate };
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1059*7c478bd9Sstevel@tonic-gate static void
1060*7c478bd9Sstevel@tonic-gate ev_walker(void *arg, uint_t picno, const char *ev)
1061*7c478bd9Sstevel@tonic-gate {
1062*7c478bd9Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, ev) == 0)
1063*7c478bd9Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
1064*7c478bd9Sstevel@tonic-gate }
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate static void
1067*7c478bd9Sstevel@tonic-gate at_walker(void *arg, const char *at)
1068*7c478bd9Sstevel@tonic-gate {
1069*7c478bd9Sstevel@tonic-gate 	if (strcmp(((struct priv *)arg)->name, at) == 0)
1070*7c478bd9Sstevel@tonic-gate 		((struct priv *)arg)->found = 1;
1071*7c478bd9Sstevel@tonic-gate }
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate static int
1074*7c478bd9Sstevel@tonic-gate cpc_valid_event(cpc_t *cpc, uint_t pic, const char *ev)
1075*7c478bd9Sstevel@tonic-gate {
1076*7c478bd9Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
1077*7c478bd9Sstevel@tonic-gate 
1078*7c478bd9Sstevel@tonic-gate 	pr.name = ev;
1079*7c478bd9Sstevel@tonic-gate 	cpc_walk_events_pic(cpc, pic, &pr, ev_walker);
1080*7c478bd9Sstevel@tonic-gate 	return (pr.found);
1081*7c478bd9Sstevel@tonic-gate }
1082*7c478bd9Sstevel@tonic-gate 
1083*7c478bd9Sstevel@tonic-gate static int
1084*7c478bd9Sstevel@tonic-gate cpc_valid_attr(cpc_t *cpc, char *attr)
1085*7c478bd9Sstevel@tonic-gate {
1086*7c478bd9Sstevel@tonic-gate 	struct priv pr = { NULL, 0 };
1087*7c478bd9Sstevel@tonic-gate 
1088*7c478bd9Sstevel@tonic-gate 	pr.name = attr;
1089*7c478bd9Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &pr, at_walker);
1090*7c478bd9Sstevel@tonic-gate 	return (pr.found);
1091*7c478bd9Sstevel@tonic-gate }
1092