10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*657Svb70745 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #ifndef _LIBCPC_IMPL_H 280Sstevel@tonic-gate #define _LIBCPC_IMPL_H 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <libcpc.h> 330Sstevel@tonic-gate #include <inttypes.h> 340Sstevel@tonic-gate #include <thread.h> 350Sstevel@tonic-gate #include <synch.h> 360Sstevel@tonic-gate #include <sys/types.h> 370Sstevel@tonic-gate #include <sys/cpc_impl.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef __cplusplus 400Sstevel@tonic-gate extern "C" { 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate #define CPC_VER_1 1 440Sstevel@tonic-gate #define CPC1_BUFSIZE (2 * sizeof (uint64_t)) 450Sstevel@tonic-gate 460Sstevel@tonic-gate struct _cpc_attr { 470Sstevel@tonic-gate char ca_name[CPC_MAX_ATTR_LEN]; 480Sstevel@tonic-gate uint64_t ca_val; 490Sstevel@tonic-gate }; 500Sstevel@tonic-gate 510Sstevel@tonic-gate typedef struct __cpc_request cpc_request_t; 520Sstevel@tonic-gate 530Sstevel@tonic-gate struct __cpc_request { 540Sstevel@tonic-gate char cr_event[CPC_MAX_EVENT_LEN]; 550Sstevel@tonic-gate uint64_t cr_preset; /* Initial value */ 560Sstevel@tonic-gate uint16_t cr_index; /* Index of request in data */ 570Sstevel@tonic-gate uint_t cr_flags; 580Sstevel@tonic-gate uint_t cr_nattrs; /* # CPU-specific attrs */ 590Sstevel@tonic-gate kcpc_attr_t *cr_attr; 600Sstevel@tonic-gate cpc_request_t *cr_next; /* next request in set */ 610Sstevel@tonic-gate }; 620Sstevel@tonic-gate 630Sstevel@tonic-gate struct __cpc_buf { 640Sstevel@tonic-gate uint64_t *cb_data; /* Pointer to data store */ 650Sstevel@tonic-gate hrtime_t cb_hrtime; /* hrtime at last sample */ 660Sstevel@tonic-gate uint64_t cb_tick; /* virtualized tsc/tick */ 670Sstevel@tonic-gate size_t cb_size; /* Size of data store, bytes */ 680Sstevel@tonic-gate cpc_buf_t *cb_next; /* List of all bufs */ 690Sstevel@tonic-gate }; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * Possible cpc_set_t states: 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate typedef enum { 750Sstevel@tonic-gate CS_UNBOUND, /* Set is not currently bound */ 760Sstevel@tonic-gate CS_BOUND_CURLWP, /* Set has been bound to curlwp */ 770Sstevel@tonic-gate CS_BOUND_PCTX, /* Set has been bound via libpctx */ 780Sstevel@tonic-gate CS_BOUND_CPU /* Set has been bound to a CPU */ 790Sstevel@tonic-gate } __cpc_state_t; 800Sstevel@tonic-gate 810Sstevel@tonic-gate struct __cpc_set { 820Sstevel@tonic-gate cpc_request_t *cs_request; /* linked list of requests */ 830Sstevel@tonic-gate __cpc_state_t cs_state; /* State of this set */ 840Sstevel@tonic-gate int cs_nreqs; /* Number of requests in set */ 850Sstevel@tonic-gate int cs_fd; /* file descriptor of cpc dev */ 860Sstevel@tonic-gate processorid_t cs_obind; /* previous proc binding */ 870Sstevel@tonic-gate pctx_t *cs_pctx; /* pctx of process bound to */ 880Sstevel@tonic-gate id_t cs_id; /* lwp ID of pctx binding */ 890Sstevel@tonic-gate thread_t cs_thr; /* thread ID which bound set */ 900Sstevel@tonic-gate cpc_set_t *cs_next; /* Linked list of all sets */ 910Sstevel@tonic-gate }; 920Sstevel@tonic-gate 930Sstevel@tonic-gate struct __cpc { 940Sstevel@tonic-gate cpc_set_t *cpc_sets; /* List of existing sets */ 950Sstevel@tonic-gate cpc_buf_t *cpc_bufs; /* List of existing bufs */ 960Sstevel@tonic-gate cpc_errhndlr_t *cpc_errfn; /* Handles library errors */ 970Sstevel@tonic-gate mutex_t cpc_lock; /* Protect various ops */ 980Sstevel@tonic-gate char *cpc_attrlist; /* List of supported attrs */ 990Sstevel@tonic-gate char **cpc_evlist; /* List of events per pic */ 1000Sstevel@tonic-gate char cpc_cpuref[CPC_MAX_CPUREF]; 1010Sstevel@tonic-gate char cpc_cciname[CPC_MAX_IMPL_NAME]; 1020Sstevel@tonic-gate uint_t cpc_caps; 1030Sstevel@tonic-gate uint_t cpc_npic; 1040Sstevel@tonic-gate }; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * cpc_t handle for CPCv1 clients. 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate extern cpc_t *__cpc; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /*PRINTFLIKE2*/ 1120Sstevel@tonic-gate extern void __cpc_error(const char *fn, const char *fmt, ...); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate extern const char *__cpc_reg_to_name(int cpuver, int regno, uint8_t bits); 1150Sstevel@tonic-gate extern int __cpc_name_to_reg(int cpuver, int regno, 1160Sstevel@tonic-gate const char *name, uint8_t *bits); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate extern uint_t __cpc_workver; 1190Sstevel@tonic-gate extern int __cpc_v1_cpuver; 1200Sstevel@tonic-gate #ifdef __sparc 1210Sstevel@tonic-gate extern uint64_t __cpc_v1_pcr; 1220Sstevel@tonic-gate #else 1230Sstevel@tonic-gate extern uint32_t __cpc_v1_pes[2]; 1240Sstevel@tonic-gate #endif /* __sparc */ 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate extern char *__cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate typedef struct __cpc_strhash cpc_strhash_t; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate struct __cpc_strhash { 1310Sstevel@tonic-gate char *str; 1320Sstevel@tonic-gate struct __cpc_strhash *cur; 1330Sstevel@tonic-gate struct __cpc_strhash *next; 1340Sstevel@tonic-gate }; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate extern cpc_strhash_t *__cpc_strhash_alloc(void); 1370Sstevel@tonic-gate extern void __cpc_strhash_free(cpc_strhash_t *hash); 1380Sstevel@tonic-gate extern int __cpc_strhash_add(cpc_strhash_t *hash, char *key); 1390Sstevel@tonic-gate extern char *__cpc_strhash_next(cpc_strhash_t *hash); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Implementation-private system call used by libcpc 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate struct __cpc; 1450Sstevel@tonic-gate extern int __pctx_cpc(pctx_t *pctx, struct __cpc *cpc, int cmd, id_t lwpid, 1460Sstevel@tonic-gate void *data1, void *data2, void *data3, int bufsize); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate #define CPUDRV "/devices/pseudo/cpc@0" 1490Sstevel@tonic-gate #define CPUDRV_SHARED CPUDRV":shared" 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate #if defined(__sparc) || defined(__i386) 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * These two are only used for backwards compatibility to the Obsolete CPCv1. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate extern int __cpc_init(void); 1560Sstevel@tonic-gate extern cpc_set_t *__cpc_eventtoset(cpc_t *cpc, cpc_event_t *event, int flags); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* 1590Sstevel@tonic-gate * ce_cpuver values 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate #define CPC_ULTRA1 1000 1620Sstevel@tonic-gate #define CPC_ULTRA2 1001 /* same as ultra1 for these purposes */ 1630Sstevel@tonic-gate #define CPC_ULTRA3 1002 1640Sstevel@tonic-gate #define CPC_ULTRA3_PLUS 1003 1650Sstevel@tonic-gate #define CPC_ULTRA3_I 1004 166*657Svb70745 #define CPC_ULTRA4_PLUS 1005 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate #define CPC_PENTIUM 2000 1690Sstevel@tonic-gate #define CPC_PENTIUM_MMX 2001 1700Sstevel@tonic-gate #define CPC_PENTIUM_PRO 2002 1710Sstevel@tonic-gate #define CPC_PENTIUM_PRO_MMX 2003 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate #define CPC_SPARC64_III 3000 1740Sstevel@tonic-gate #define CPC_SPARC64_V 3002 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate #endif /* __sparc || __i386 */ 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * This is common between i386 and amd64, because amd64 implements %tick. 1810Sstevel@tonic-gate * Currently only used by the cpc tools to print the label atop the CPU ticks 1820Sstevel@tonic-gate * column on amd64. 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate #define CPC_TICKREG_NAME "tsc" 1850Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate #if defined(__sparc) 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate /* 190*657Svb70745 * UltraSPARC I, II, III and IV processors 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * The performance counters on these processors allow up to two 32-bit 1930Sstevel@tonic-gate * performance events to be captured simultaneously from a selection 1940Sstevel@tonic-gate * of metrics. The metrics are selected by writing to the performance 1950Sstevel@tonic-gate * control register, and subsequent values collected by reading from the 1960Sstevel@tonic-gate * performance instrumentation counter registers. Both registers are 1970Sstevel@tonic-gate * priviliged by default, and implemented as ASRs. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate struct _cpc_event { 2010Sstevel@tonic-gate int ce_cpuver; 2020Sstevel@tonic-gate hrtime_t ce_hrt; /* gethrtime() */ 2030Sstevel@tonic-gate uint64_t ce_tick; /* virtualized %tick */ 2040Sstevel@tonic-gate uint64_t ce_pic[2]; /* virtualized %pic */ 2050Sstevel@tonic-gate uint64_t ce_pcr; /* %pcr */ 2060Sstevel@tonic-gate }; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate #define CPC_TICKREG(ev) ((ev)->ce_tick) 2090Sstevel@tonic-gate #define CPC_TICKREG_NAME "%tick" 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * "Well known" bitfields in the UltraSPARC %pcr register 2130Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2140Sstevel@tonic-gate */ 2150Sstevel@tonic-gate #define CPC_ULTRA_PCR_USR 2 2160Sstevel@tonic-gate #define CPC_ULTRA_PCR_SYS 1 2170Sstevel@tonic-gate #define CPC_ULTRA_PCR_PRIVPIC 0 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate #define CPC_ULTRA_PCR_PIC0_SHIFT 4 2200Sstevel@tonic-gate #define CPC_ULTRA2_PCR_PIC0_MASK UINT64_C(0xf) 2210Sstevel@tonic-gate #define CPC_ULTRA3_PCR_PIC0_MASK UINT64_C(0x3f) 2220Sstevel@tonic-gate #define CPC_ULTRA_PCR_PIC1_SHIFT 11 2230Sstevel@tonic-gate #define CPC_ULTRA2_PCR_PIC1_MASK UINT64_C(0xf) 2240Sstevel@tonic-gate #define CPC_ULTRA3_PCR_PIC1_MASK UINT64_C(0x3f) 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate #elif defined(__i386) 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * Pentium I, II and III processors 2300Sstevel@tonic-gate * 2310Sstevel@tonic-gate * These CPUs allow pairs of events to captured. 2320Sstevel@tonic-gate * The hardware counters count up to 40-bits of significance, but 2330Sstevel@tonic-gate * only allow 32 (signed) bits to be programmed into them. 2340Sstevel@tonic-gate * Pentium I and Pentium II processors are programmed differently, but 2350Sstevel@tonic-gate * the resulting counters and timestamps can be handled portably. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate struct _cpc_event { 2390Sstevel@tonic-gate int ce_cpuver; 2400Sstevel@tonic-gate hrtime_t ce_hrt; /* gethrtime() */ 2410Sstevel@tonic-gate uint64_t ce_tsc; /* virtualized rdtsc value */ 2420Sstevel@tonic-gate uint64_t ce_pic[2]; /* virtualized PerfCtr[01] */ 2430Sstevel@tonic-gate uint32_t ce_pes[2]; /* Pentium II */ 2440Sstevel@tonic-gate #define ce_cesr ce_pes[0] /* Pentium I */ 2450Sstevel@tonic-gate }; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate #define CPC_TICKREG(ev) ((ev)->ce_tsc) 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2500Sstevel@tonic-gate * "Well known" bit fields in the Pentium CES register 2510Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2520Sstevel@tonic-gate */ 2530Sstevel@tonic-gate #define CPC_P5_CESR_ES0_SHIFT 0 2540Sstevel@tonic-gate #define CPC_P5_CESR_ES0_MASK 0x3f 2550Sstevel@tonic-gate #define CPC_P5_CESR_ES1_SHIFT 16 2560Sstevel@tonic-gate #define CPC_P5_CESR_ES1_MASK 0x3f 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate #define CPC_P5_CESR_OS0 6 2590Sstevel@tonic-gate #define CPC_P5_CESR_USR0 7 2600Sstevel@tonic-gate #define CPC_P5_CESR_CLK0 8 2610Sstevel@tonic-gate #define CPC_P5_CESR_PC0 9 2620Sstevel@tonic-gate #define CPC_P5_CESR_OS1 (CPC_P5_CESR_OS0 + 16) 2630Sstevel@tonic-gate #define CPC_P5_CESR_USR1 (CPC_P5_CESR_USR0 + 16) 2640Sstevel@tonic-gate #define CPC_P5_CESR_CLK1 (CPC_P5_CESR_CLK0 + 16) 2650Sstevel@tonic-gate #define CPC_P5_CESR_PC1 (CPC_P5_CESR_PC0 + 16) 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* 2680Sstevel@tonic-gate * "Well known" bit fields in the Pentium Pro PerfEvtSel registers 2690Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate #define CPC_P6_PES_INV 23 2720Sstevel@tonic-gate #define CPC_P6_PES_EN 22 2730Sstevel@tonic-gate #define CPC_P6_PES_INT 20 2740Sstevel@tonic-gate #define CPC_P6_PES_PC 19 2750Sstevel@tonic-gate #define CPC_P6_PES_E 18 2760Sstevel@tonic-gate #define CPC_P6_PES_OS 17 2770Sstevel@tonic-gate #define CPC_P6_PES_USR 16 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate #define CPC_P6_PES_UMASK_SHIFT 8 2800Sstevel@tonic-gate #define CPC_P6_PES_UMASK_MASK (0xffu) 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate #define CPC_P6_PES_CMASK_SHIFT 24 2830Sstevel@tonic-gate #define CPC_P6_PES_CMASK_MASK (0xffu) 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate #define CPC_P6_PES_PIC0_MASK (0xffu) 2860Sstevel@tonic-gate #define CPC_P6_PES_PIC1_MASK (0xffu) 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate #endif /* __i386 */ 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate #ifdef __cplusplus 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate #endif 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate #endif /* _LIBCPC_IMPL_H */ 295