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