17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*23961e2bSvb  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_LIBCPC_IMPL_H
287c478bd9Sstevel@tonic-gate #define	_LIBCPC_IMPL_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <libcpc.h>
317c478bd9Sstevel@tonic-gate #include <inttypes.h>
327c478bd9Sstevel@tonic-gate #include <thread.h>
337c478bd9Sstevel@tonic-gate #include <synch.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	CPC_VER_1 1
427c478bd9Sstevel@tonic-gate #define	CPC1_BUFSIZE (2 * sizeof (uint64_t))
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct _cpc_attr {
457c478bd9Sstevel@tonic-gate 	char			ca_name[CPC_MAX_ATTR_LEN];
467c478bd9Sstevel@tonic-gate 	uint64_t		ca_val;
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct __cpc_request cpc_request_t;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate struct __cpc_request {
527c478bd9Sstevel@tonic-gate 	char			cr_event[CPC_MAX_EVENT_LEN];
537c478bd9Sstevel@tonic-gate 	uint64_t		cr_preset;	/* Initial value */
547c478bd9Sstevel@tonic-gate 	uint16_t		cr_index;	/* Index of request in data */
557c478bd9Sstevel@tonic-gate 	uint_t			cr_flags;
567c478bd9Sstevel@tonic-gate 	uint_t			cr_nattrs;	/* # CPU-specific attrs */
577c478bd9Sstevel@tonic-gate 	kcpc_attr_t		*cr_attr;
587c478bd9Sstevel@tonic-gate 	cpc_request_t		*cr_next;	/* next request in set */
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct __cpc_buf {
627c478bd9Sstevel@tonic-gate 	uint64_t		*cb_data;	/* Pointer to data store */
637c478bd9Sstevel@tonic-gate 	hrtime_t		cb_hrtime;	/* hrtime at last sample */
647c478bd9Sstevel@tonic-gate 	uint64_t		cb_tick;	/* virtualized tsc/tick */
657c478bd9Sstevel@tonic-gate 	size_t			cb_size;	/* Size of data store, bytes */
667c478bd9Sstevel@tonic-gate 	cpc_buf_t		*cb_next;	/* List of all bufs */
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Possible cpc_set_t states:
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate typedef enum {
737c478bd9Sstevel@tonic-gate 	CS_UNBOUND,		/* Set is not currently bound */
747c478bd9Sstevel@tonic-gate 	CS_BOUND_CURLWP,	/* Set has been bound to curlwp */
757c478bd9Sstevel@tonic-gate 	CS_BOUND_PCTX,		/* Set has been bound via libpctx */
767c478bd9Sstevel@tonic-gate 	CS_BOUND_CPU		/* Set has been bound to a CPU */
777c478bd9Sstevel@tonic-gate } __cpc_state_t;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct __cpc_set {
807c478bd9Sstevel@tonic-gate 	cpc_request_t		*cs_request;	/* linked list of requests */
817c478bd9Sstevel@tonic-gate 	__cpc_state_t		cs_state;	/* State of this set */
827c478bd9Sstevel@tonic-gate 	int			cs_nreqs;	/* Number of requests in set */
837c478bd9Sstevel@tonic-gate 	int			cs_fd;		/* file descriptor of cpc dev */
847c478bd9Sstevel@tonic-gate 	processorid_t		cs_obind;	/* previous proc binding */
857c478bd9Sstevel@tonic-gate 	pctx_t			*cs_pctx;	/* pctx of process bound to */
867c478bd9Sstevel@tonic-gate 	id_t			cs_id;		/* lwp ID of pctx binding */
877c478bd9Sstevel@tonic-gate 	thread_t		cs_thr;		/* thread ID which bound set */
887c478bd9Sstevel@tonic-gate 	cpc_set_t		*cs_next;	/* Linked list of all sets */
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate struct __cpc {
927c478bd9Sstevel@tonic-gate 	cpc_set_t		*cpc_sets;	/* List of existing sets */
937c478bd9Sstevel@tonic-gate 	cpc_buf_t		*cpc_bufs;	/* List of existing bufs */
947c478bd9Sstevel@tonic-gate 	cpc_errhndlr_t		*cpc_errfn;	/* Handles library errors */
957c478bd9Sstevel@tonic-gate 	mutex_t			cpc_lock;	/* Protect various ops */
967c478bd9Sstevel@tonic-gate 	char			*cpc_attrlist;	/* List of supported attrs */
977c478bd9Sstevel@tonic-gate 	char			**cpc_evlist;	/* List of events per pic */
987c478bd9Sstevel@tonic-gate 	char			cpc_cpuref[CPC_MAX_CPUREF];
997c478bd9Sstevel@tonic-gate 	char			cpc_cciname[CPC_MAX_IMPL_NAME];
1007c478bd9Sstevel@tonic-gate 	uint_t			cpc_caps;
1017c478bd9Sstevel@tonic-gate 	uint_t			cpc_npic;
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * cpc_t handle for CPCv1 clients.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate extern cpc_t *__cpc;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1107c478bd9Sstevel@tonic-gate extern void __cpc_error(const char *fn, const char *fmt, ...);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate extern const char *__cpc_reg_to_name(int cpuver, int regno, uint8_t bits);
1137c478bd9Sstevel@tonic-gate extern int __cpc_name_to_reg(int cpuver, int regno,
1147c478bd9Sstevel@tonic-gate     const char *name, uint8_t *bits);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate extern uint_t __cpc_workver;
1177c478bd9Sstevel@tonic-gate extern int __cpc_v1_cpuver;
1187c478bd9Sstevel@tonic-gate #ifdef __sparc
1197c478bd9Sstevel@tonic-gate extern uint64_t __cpc_v1_pcr;
1207c478bd9Sstevel@tonic-gate #else
1217c478bd9Sstevel@tonic-gate extern uint32_t __cpc_v1_pes[2];
1227c478bd9Sstevel@tonic-gate #endif /* __sparc */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate extern char *__cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate typedef struct __cpc_strhash cpc_strhash_t;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate struct __cpc_strhash {
1297c478bd9Sstevel@tonic-gate 	char *str;
1307c478bd9Sstevel@tonic-gate 	struct __cpc_strhash *cur;
1317c478bd9Sstevel@tonic-gate 	struct __cpc_strhash *next;
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate extern cpc_strhash_t *__cpc_strhash_alloc(void);
1357c478bd9Sstevel@tonic-gate extern void __cpc_strhash_free(cpc_strhash_t *hash);
1367c478bd9Sstevel@tonic-gate extern int __cpc_strhash_add(cpc_strhash_t *hash, char *key);
1377c478bd9Sstevel@tonic-gate extern char *__cpc_strhash_next(cpc_strhash_t *hash);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Implementation-private system call used by libcpc
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate struct __cpc;
1437c478bd9Sstevel@tonic-gate extern int __pctx_cpc(pctx_t *pctx, struct __cpc *cpc, int cmd, id_t lwpid,
1447c478bd9Sstevel@tonic-gate     void *data1, void *data2, void *data3, int bufsize);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #define	CPUDRV				"/devices/pseudo/cpc@0"
1477c478bd9Sstevel@tonic-gate #define	CPUDRV_SHARED			CPUDRV":shared"
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386)
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * These two are only used for backwards compatibility to the Obsolete CPCv1.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate extern int __cpc_init(void);
1547c478bd9Sstevel@tonic-gate extern cpc_set_t *__cpc_eventtoset(cpc_t *cpc, cpc_event_t *event, int flags);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * ce_cpuver values
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate #define	CPC_ULTRA1		1000
1607c478bd9Sstevel@tonic-gate #define	CPC_ULTRA2		1001	/* same as ultra1 for these purposes */
1617c478bd9Sstevel@tonic-gate #define	CPC_ULTRA3		1002
1627c478bd9Sstevel@tonic-gate #define	CPC_ULTRA3_PLUS		1003
1637c478bd9Sstevel@tonic-gate #define	CPC_ULTRA3_I		1004
164*23961e2bSvb #define	CPC_ULTRA4_PLUS		1005
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate #define	CPC_PENTIUM		2000
1677c478bd9Sstevel@tonic-gate #define	CPC_PENTIUM_MMX		2001
1687c478bd9Sstevel@tonic-gate #define	CPC_PENTIUM_PRO		2002
1697c478bd9Sstevel@tonic-gate #define	CPC_PENTIUM_PRO_MMX	2003
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	CPC_SPARC64_III		3000
1727c478bd9Sstevel@tonic-gate #define	CPC_SPARC64_V		3002
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #endif /* __sparc || __i386 */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * This is common between i386 and amd64, because amd64 implements %tick.
1797c478bd9Sstevel@tonic-gate  * Currently only used by the cpc tools to print the label atop the CPU ticks
1807c478bd9Sstevel@tonic-gate  * column on amd64.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate #define	CPC_TICKREG_NAME	"tsc"
1837c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #if defined(__sparc)
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
188*23961e2bSvb  * UltraSPARC I, II, III and IV processors
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * The performance counters on these processors allow up to two 32-bit
1917c478bd9Sstevel@tonic-gate  * performance events to be captured simultaneously from a selection
1927c478bd9Sstevel@tonic-gate  * of metrics.   The metrics are selected by writing to the performance
1937c478bd9Sstevel@tonic-gate  * control register, and subsequent values collected by reading from the
1947c478bd9Sstevel@tonic-gate  * performance instrumentation counter registers.  Both registers are
1957c478bd9Sstevel@tonic-gate  * priviliged by default, and implemented as ASRs.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate struct _cpc_event {
1997c478bd9Sstevel@tonic-gate 	int ce_cpuver;
2007c478bd9Sstevel@tonic-gate 	hrtime_t ce_hrt;	/* gethrtime() */
2017c478bd9Sstevel@tonic-gate 	uint64_t ce_tick;	/* virtualized %tick */
2027c478bd9Sstevel@tonic-gate 	uint64_t ce_pic[2];	/* virtualized %pic */
2037c478bd9Sstevel@tonic-gate 	uint64_t ce_pcr;	/* %pcr */
2047c478bd9Sstevel@tonic-gate };
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate #define	CPC_TICKREG(ev)		((ev)->ce_tick)
2077c478bd9Sstevel@tonic-gate #define	CPC_TICKREG_NAME	"%tick"
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * "Well known" bitfields in the UltraSPARC %pcr register
2117c478bd9Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate #define	CPC_ULTRA_PCR_USR		2
2147c478bd9Sstevel@tonic-gate #define	CPC_ULTRA_PCR_SYS		1
2157c478bd9Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PRIVPIC		0
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PIC0_SHIFT	4
2187c478bd9Sstevel@tonic-gate #define	CPC_ULTRA2_PCR_PIC0_MASK	UINT64_C(0xf)
2197c478bd9Sstevel@tonic-gate #define	CPC_ULTRA3_PCR_PIC0_MASK	UINT64_C(0x3f)
2207c478bd9Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PIC1_SHIFT	11
2217c478bd9Sstevel@tonic-gate #define	CPC_ULTRA2_PCR_PIC1_MASK	UINT64_C(0xf)
2227c478bd9Sstevel@tonic-gate #define	CPC_ULTRA3_PCR_PIC1_MASK	UINT64_C(0x3f)
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #elif defined(__i386)
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Pentium I, II and III processors
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  * These CPUs allow pairs of events to captured.
2307c478bd9Sstevel@tonic-gate  * The hardware counters count up to 40-bits of significance, but
2317c478bd9Sstevel@tonic-gate  * only allow 32 (signed) bits to be programmed into them.
2327c478bd9Sstevel@tonic-gate  * Pentium I and Pentium II processors are programmed differently, but
2337c478bd9Sstevel@tonic-gate  * the resulting counters and timestamps can be handled portably.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate struct _cpc_event {
2377c478bd9Sstevel@tonic-gate 	int ce_cpuver;
2387c478bd9Sstevel@tonic-gate 	hrtime_t ce_hrt;	/* gethrtime() */
2397c478bd9Sstevel@tonic-gate 	uint64_t ce_tsc;	/* virtualized rdtsc value */
2407c478bd9Sstevel@tonic-gate 	uint64_t ce_pic[2];	/* virtualized PerfCtr[01] */
2417c478bd9Sstevel@tonic-gate 	uint32_t ce_pes[2];	/* Pentium II */
2427c478bd9Sstevel@tonic-gate #define	ce_cesr	ce_pes[0]	/* Pentium I */
2437c478bd9Sstevel@tonic-gate };
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate #define	CPC_TICKREG(ev)		((ev)->ce_tsc)
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * "Well known" bit fields in the Pentium CES register
2497c478bd9Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_ES0_SHIFT	0
2527c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_ES0_MASK	0x3f
2537c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_ES1_SHIFT	16
2547c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_ES1_MASK	0x3f
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_OS0		6
2577c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_USR0	7
2587c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_CLK0	8
2597c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_PC0		9
2607c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_OS1		(CPC_P5_CESR_OS0 + 16)
2617c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_USR1	(CPC_P5_CESR_USR0 + 16)
2627c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_CLK1	(CPC_P5_CESR_CLK0 + 16)
2637c478bd9Sstevel@tonic-gate #define	CPC_P5_CESR_PC1		(CPC_P5_CESR_PC0 + 16)
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * "Well known" bit fields in the Pentium Pro PerfEvtSel registers
2677c478bd9Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_INV		23
2707c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_EN		22
2717c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_INT		20
2727c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_PC		19
2737c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_E		18
2747c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_OS		17
2757c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_USR		16
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_UMASK_SHIFT	8
2787c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_UMASK_MASK	(0xffu)
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_CMASK_SHIFT	24
2817c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_CMASK_MASK	(0xffu)
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_PIC0_MASK	(0xffu)
2847c478bd9Sstevel@tonic-gate #define	CPC_P6_PES_PIC1_MASK	(0xffu)
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #endif /* __i386 */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate #endif
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate #endif	/* _LIBCPC_IMPL_H */
293