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
5*23a1cceaSRoger A. Faulkner * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
22*23a1cceaSRoger A. Faulkner /*
23*23a1cceaSRoger A. Faulkner * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24*23a1cceaSRoger A. Faulkner */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Routines to capture processor-dependencies in event specification.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #include "libcpc.h"
397c478bd9Sstevel@tonic-gate #include "libcpc_impl.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate * Event specifications for UltraSPARC performance counters are based
437c478bd9Sstevel@tonic-gate * on the content of a getsubopt-like string.
447c478bd9Sstevel@tonic-gate * The string should contain something that looks like this:
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * pic0=<eventspec>,pic1=<eventspec>
477c478bd9Sstevel@tonic-gate * [,nouser][,sys]
487c478bd9Sstevel@tonic-gate *
497c478bd9Sstevel@tonic-gate * For example:
507c478bd9Sstevel@tonic-gate * pic1=0x4,pic0=Instr_cnt
517c478bd9Sstevel@tonic-gate * or
527c478bd9Sstevel@tonic-gate * pic0=Instr_cnt,pic1=Cycle_cnt,nouser,sys
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * The two events must be named. The names can be ascii or
557c478bd9Sstevel@tonic-gate * a decimal, octal or hexadecimal number as parsed by strtol(3C).
567c478bd9Sstevel@tonic-gate *
577c478bd9Sstevel@tonic-gate * By default, user event counting is enabled, system event counting
587c478bd9Sstevel@tonic-gate * is disabled.
597c478bd9Sstevel@tonic-gate *
607c478bd9Sstevel@tonic-gate * The routine counts the number of errors encountered while parsing
617c478bd9Sstevel@tonic-gate * the string, if no errors are encountered, the event handle is
627c478bd9Sstevel@tonic-gate * returned.
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate const char *
cpc_getusage(int cpuver)667c478bd9Sstevel@tonic-gate cpc_getusage(int cpuver)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate switch (cpuver) {
697c478bd9Sstevel@tonic-gate case CPC_ULTRA1:
707c478bd9Sstevel@tonic-gate case CPC_ULTRA2:
717c478bd9Sstevel@tonic-gate case CPC_ULTRA3:
727c478bd9Sstevel@tonic-gate case CPC_ULTRA3_PLUS:
737c478bd9Sstevel@tonic-gate case CPC_ULTRA3_I:
7423961e2bSvb70745 case CPC_ULTRA4_PLUS:
757c478bd9Sstevel@tonic-gate return ("pic0=<event0>,pic1=<event1> "
767c478bd9Sstevel@tonic-gate "[,sys] "
777c478bd9Sstevel@tonic-gate "[,nouser]");
787c478bd9Sstevel@tonic-gate default:
797c478bd9Sstevel@tonic-gate return (NULL);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * This private structure is used to build tables that correspond
857c478bd9Sstevel@tonic-gate * to the bit patterns in the control registers of the processor.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate struct keyval {
887c478bd9Sstevel@tonic-gate char *kv_token;
897c478bd9Sstevel@tonic-gate int (*kv_action)(const char *,
907c478bd9Sstevel@tonic-gate const struct keyval *, int, char *, uint64_t *);
917c478bd9Sstevel@tonic-gate uint64_t kv_mask;
927c478bd9Sstevel@tonic-gate int kv_shift;
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate static int
picbits(const char * fn,const struct keyval * kv,int cpuver,char * value,uint64_t * bits)967c478bd9Sstevel@tonic-gate picbits(const char *fn,
977c478bd9Sstevel@tonic-gate const struct keyval *kv, int cpuver, char *value, uint64_t *bits)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate uint8_t val8;
1007c478bd9Sstevel@tonic-gate uint_t regno;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate regno = strcmp(kv->kv_token, "pic0") == 0 ? 0 : 1;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate if (value == NULL) {
1057c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("missing '%s' value\n"),
1067c478bd9Sstevel@tonic-gate kv->kv_token);
1077c478bd9Sstevel@tonic-gate return (-1);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate if (__cpc_name_to_reg(cpuver, regno, value, &val8) != 0) {
1107c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("%%pic%d cannot measure "
1117c478bd9Sstevel@tonic-gate "event '%s' on this cpu\n"), regno, value);
1127c478bd9Sstevel@tonic-gate return (-1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate *bits |= (((uint64_t)val8 & kv->kv_mask) << kv->kv_shift);
1157c478bd9Sstevel@tonic-gate return (0);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1197c478bd9Sstevel@tonic-gate static int
bitclr(const char * fn,const struct keyval * kv,int cpuver,char * value,uint64_t * bits)1207c478bd9Sstevel@tonic-gate bitclr(const char *fn,
1217c478bd9Sstevel@tonic-gate const struct keyval *kv, int cpuver, char *value, uint64_t *bits)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate if (value != NULL) {
1247c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("bad arg to '%s'\n"), kv->kv_token);
1257c478bd9Sstevel@tonic-gate return (-1);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate *bits &= ~(kv->kv_mask << kv->kv_shift);
1287c478bd9Sstevel@tonic-gate return (0);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1327c478bd9Sstevel@tonic-gate static int
bitset(const char * fn,const struct keyval * kv,int cpuver,char * value,uint64_t * bits)1337c478bd9Sstevel@tonic-gate bitset(const char *fn,
1347c478bd9Sstevel@tonic-gate const struct keyval *kv, int cpuver, char *value, uint64_t *bits)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate if (value != NULL) {
1377c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("bad arg to '%s'\n"), kv->kv_token);
1387c478bd9Sstevel@tonic-gate return (-1);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate *bits |= (kv->kv_mask << kv->kv_shift);
1417c478bd9Sstevel@tonic-gate return (0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate * This token table must match the keyval tables below.
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate static char * const tokens[] = {
1497c478bd9Sstevel@tonic-gate #define D_pic0 0
1507c478bd9Sstevel@tonic-gate "pic0", /* takes a valid event name */
1517c478bd9Sstevel@tonic-gate #define D_pic1 1
1527c478bd9Sstevel@tonic-gate "pic1", /* takes a valid event name */
1537c478bd9Sstevel@tonic-gate #define D_nouser 2
1547c478bd9Sstevel@tonic-gate "nouser", /* disables user counts */
1557c478bd9Sstevel@tonic-gate #define D_sys 3
1567c478bd9Sstevel@tonic-gate "sys", /* enables system counts */
1577c478bd9Sstevel@tonic-gate NULL
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate static const struct keyval us2_keyvals[] = {
1617c478bd9Sstevel@tonic-gate { "pic0", picbits,
1627c478bd9Sstevel@tonic-gate CPC_ULTRA2_PCR_PIC0_MASK, CPC_ULTRA_PCR_PIC0_SHIFT },
1637c478bd9Sstevel@tonic-gate { "pic1", picbits,
1647c478bd9Sstevel@tonic-gate CPC_ULTRA2_PCR_PIC1_MASK, CPC_ULTRA_PCR_PIC1_SHIFT },
1657c478bd9Sstevel@tonic-gate { "nouser", bitclr,
1667c478bd9Sstevel@tonic-gate UINT64_C(1), CPC_ULTRA_PCR_USR },
1677c478bd9Sstevel@tonic-gate { "sys", bitset,
1687c478bd9Sstevel@tonic-gate UINT64_C(1), CPC_ULTRA_PCR_SYS },
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate static const struct keyval us3_keyvals[] = {
1727c478bd9Sstevel@tonic-gate { "pic0", picbits,
1737c478bd9Sstevel@tonic-gate CPC_ULTRA3_PCR_PIC0_MASK, CPC_ULTRA_PCR_PIC0_SHIFT },
1747c478bd9Sstevel@tonic-gate { "pic1", picbits,
1757c478bd9Sstevel@tonic-gate CPC_ULTRA3_PCR_PIC1_MASK, CPC_ULTRA_PCR_PIC1_SHIFT },
1767c478bd9Sstevel@tonic-gate { "nouser", bitclr,
1777c478bd9Sstevel@tonic-gate UINT64_C(1), CPC_ULTRA_PCR_USR },
1787c478bd9Sstevel@tonic-gate { "sys", bitset,
1797c478bd9Sstevel@tonic-gate UINT64_C(1), CPC_ULTRA_PCR_SYS },
1807c478bd9Sstevel@tonic-gate };
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate #if !defined(NDEBUG)
1837c478bd9Sstevel@tonic-gate #pragma init(__tablecheck)
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate static void
__tablecheck(void)1867c478bd9Sstevel@tonic-gate __tablecheck(void)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate uint_t ntokens = sizeof (tokens) / sizeof (tokens[0]) - 1;
1897c478bd9Sstevel@tonic-gate uint_t us3_nkeys = sizeof (us3_keyvals) / sizeof (us3_keyvals[0]);
1907c478bd9Sstevel@tonic-gate uint_t us2_nkeys = sizeof (us2_keyvals) / sizeof (us2_keyvals[0]);
1917c478bd9Sstevel@tonic-gate uint_t n;
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate assert(ntokens == us3_nkeys);
1947c478bd9Sstevel@tonic-gate for (n = 0; n < ntokens; n++)
1957c478bd9Sstevel@tonic-gate assert(strcmp(tokens[n], us3_keyvals[n].kv_token) == 0);
1967c478bd9Sstevel@tonic-gate assert(us3_nkeys >= us2_nkeys);
1977c478bd9Sstevel@tonic-gate for (n = 0; n < us2_nkeys; n++)
1987c478bd9Sstevel@tonic-gate assert(strcmp(tokens[n], us2_keyvals[n].kv_token) == 0);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate #endif /* !NDEBUG */
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate int
cpc_strtoevent(int cpuver,const char * spec,cpc_event_t * event)2047c478bd9Sstevel@tonic-gate cpc_strtoevent(int cpuver, const char *spec, cpc_event_t *event)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate static const char fn[] = "strtoevent";
2077c478bd9Sstevel@tonic-gate char *value;
2087c478bd9Sstevel@tonic-gate char *pic[2];
2097c478bd9Sstevel@tonic-gate char *opts;
2107c478bd9Sstevel@tonic-gate int errcnt = 0;
2117c478bd9Sstevel@tonic-gate uint_t ntokens;
2127c478bd9Sstevel@tonic-gate const struct keyval *keyvals;
2137c478bd9Sstevel@tonic-gate uint64_t *bits;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate if (spec == NULL)
2167c478bd9Sstevel@tonic-gate return (errcnt = 1);
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate bzero(event, sizeof (*event));
2197c478bd9Sstevel@tonic-gate switch (event->ce_cpuver = cpuver) {
2207c478bd9Sstevel@tonic-gate case CPC_ULTRA1:
2217c478bd9Sstevel@tonic-gate case CPC_ULTRA2:
2227c478bd9Sstevel@tonic-gate keyvals = us2_keyvals;
2237c478bd9Sstevel@tonic-gate ntokens = sizeof (us2_keyvals) / sizeof (us2_keyvals[0]);
2247c478bd9Sstevel@tonic-gate bits = &event->ce_pcr;
2257c478bd9Sstevel@tonic-gate *bits = UINT64_C(1) << CPC_ULTRA_PCR_USR;
2267c478bd9Sstevel@tonic-gate break;
2277c478bd9Sstevel@tonic-gate case CPC_ULTRA3:
2287c478bd9Sstevel@tonic-gate case CPC_ULTRA3_PLUS:
2297c478bd9Sstevel@tonic-gate case CPC_ULTRA3_I:
23023961e2bSvb70745 case CPC_ULTRA4_PLUS:
2317c478bd9Sstevel@tonic-gate keyvals = us3_keyvals;
2327c478bd9Sstevel@tonic-gate ntokens = sizeof (us3_keyvals) / sizeof (us3_keyvals[0]);
2337c478bd9Sstevel@tonic-gate bits = &event->ce_pcr;
2347c478bd9Sstevel@tonic-gate *bits = UINT64_C(1) << CPC_ULTRA_PCR_USR;
2357c478bd9Sstevel@tonic-gate break;
2367c478bd9Sstevel@tonic-gate default:
2377c478bd9Sstevel@tonic-gate return (errcnt = 1);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate pic[0] = pic[1] = NULL;
2417c478bd9Sstevel@tonic-gate
242*23a1cceaSRoger A. Faulkner opts = strdupa(spec);
2437c478bd9Sstevel@tonic-gate while (*opts != '\0') {
2447c478bd9Sstevel@tonic-gate const struct keyval *kv;
2457c478bd9Sstevel@tonic-gate int idx = getsubopt(&opts, tokens, &value);
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if (idx >= 0 && idx < ntokens) {
2487c478bd9Sstevel@tonic-gate kv = &keyvals[idx];
2497c478bd9Sstevel@tonic-gate if (kv->kv_action(fn, kv, cpuver, value, bits) != 0) {
2507c478bd9Sstevel@tonic-gate errcnt++;
2517c478bd9Sstevel@tonic-gate break;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate if (idx == D_pic0) {
2557c478bd9Sstevel@tonic-gate if (pic[0] != NULL) {
2567c478bd9Sstevel@tonic-gate __cpc_error(fn,
2577c478bd9Sstevel@tonic-gate "repeated '%s' token\n",
2587c478bd9Sstevel@tonic-gate tokens[idx]);
2597c478bd9Sstevel@tonic-gate errcnt++;
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate pic[0] = value;
2637c478bd9Sstevel@tonic-gate } else if (idx == D_pic1) {
2647c478bd9Sstevel@tonic-gate if (pic[1] != NULL) {
2657c478bd9Sstevel@tonic-gate __cpc_error(fn,
2667c478bd9Sstevel@tonic-gate "repeated '%s' token\n",
2677c478bd9Sstevel@tonic-gate tokens[idx]);
2687c478bd9Sstevel@tonic-gate errcnt++;
2697c478bd9Sstevel@tonic-gate break;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate pic[1] = value;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate } else if (idx == -1) {
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * The token given wasn't recognized.
2767c478bd9Sstevel@tonic-gate * See if it was an implicit pic specification..
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate if (pic[0] == NULL) {
2797c478bd9Sstevel@tonic-gate kv = &keyvals[D_pic0];
2807c478bd9Sstevel@tonic-gate if (kv->kv_action(fn,
2817c478bd9Sstevel@tonic-gate kv, cpuver, value, bits) != 0) {
2827c478bd9Sstevel@tonic-gate errcnt++;
2837c478bd9Sstevel@tonic-gate break;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate pic[0] = value;
2867c478bd9Sstevel@tonic-gate } else if (pic[1] == NULL) {
2877c478bd9Sstevel@tonic-gate kv = &keyvals[D_pic1];
2887c478bd9Sstevel@tonic-gate if (kv->kv_action(fn,
2897c478bd9Sstevel@tonic-gate kv, cpuver, value, bits) != 0) {
2907c478bd9Sstevel@tonic-gate errcnt++;
2917c478bd9Sstevel@tonic-gate break;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate pic[1] = value;
2947c478bd9Sstevel@tonic-gate } else {
2957c478bd9Sstevel@tonic-gate __cpc_error(fn,
2967c478bd9Sstevel@tonic-gate gettext("bad token '%s'\n"), value);
2977c478bd9Sstevel@tonic-gate errcnt++;
2987c478bd9Sstevel@tonic-gate break;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate } else {
3017c478bd9Sstevel@tonic-gate if (idx >= 0 &&
3027c478bd9Sstevel@tonic-gate idx < sizeof (tokens) / sizeof (tokens[0]))
3037c478bd9Sstevel@tonic-gate __cpc_error(fn,
3047c478bd9Sstevel@tonic-gate gettext("bad token '%s'\n"), tokens[idx]);
3057c478bd9Sstevel@tonic-gate else
3067c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("bad token\n"));
3077c478bd9Sstevel@tonic-gate errcnt++;
3087c478bd9Sstevel@tonic-gate break;
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate if (pic[0] == NULL || pic[1] == NULL) {
3137c478bd9Sstevel@tonic-gate __cpc_error(fn, gettext("two events must be specified\n"));
3147c478bd9Sstevel@tonic-gate errcnt++;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate return (errcnt);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * Return a printable description of the control registers.
3227c478bd9Sstevel@tonic-gate *
3237c478bd9Sstevel@tonic-gate * This routine should always succeed (notwithstanding heap problems),
3247c478bd9Sstevel@tonic-gate * but may not be able to correctly decode the registers, if, for
3257c478bd9Sstevel@tonic-gate * example, a new processor is under test.
3267c478bd9Sstevel@tonic-gate *
3277c478bd9Sstevel@tonic-gate * The caller is responsible for free(3c)ing the string returned.
3287c478bd9Sstevel@tonic-gate */
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate static char *
val8tostr(uint8_t bits)3317c478bd9Sstevel@tonic-gate val8tostr(uint8_t bits)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate char buf[2 + 2 + 1]; /* 0x %2x \0 */
3347c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "0x%x", bits);
3357c478bd9Sstevel@tonic-gate return (strdup(buf));
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate static char *
regtostr(int cpuver,int regno,uint8_t bits)3397c478bd9Sstevel@tonic-gate regtostr(int cpuver, int regno, uint8_t bits)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate const char *sname;
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate if ((sname = __cpc_reg_to_name(cpuver, regno, bits)) != NULL)
3447c478bd9Sstevel@tonic-gate return (strdup(sname));
3457c478bd9Sstevel@tonic-gate return (val8tostr(bits));
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate struct xpcr {
3497c478bd9Sstevel@tonic-gate uint8_t pic[2];
3507c478bd9Sstevel@tonic-gate int usr, sys;
3517c478bd9Sstevel@tonic-gate };
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate static void
unmake_pcr(uint64_t pcr,int cpuver,struct xpcr * xpcr)3547c478bd9Sstevel@tonic-gate unmake_pcr(uint64_t pcr, int cpuver, struct xpcr *xpcr)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate const struct keyval *kv;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate switch (cpuver) {
3597c478bd9Sstevel@tonic-gate case CPC_ULTRA1:
3607c478bd9Sstevel@tonic-gate case CPC_ULTRA2:
3617c478bd9Sstevel@tonic-gate default:
3627c478bd9Sstevel@tonic-gate kv = us2_keyvals;
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate case CPC_ULTRA3:
3657c478bd9Sstevel@tonic-gate case CPC_ULTRA3_PLUS:
3667c478bd9Sstevel@tonic-gate case CPC_ULTRA3_I:
36723961e2bSvb70745 case CPC_ULTRA4_PLUS:
3687c478bd9Sstevel@tonic-gate kv = us3_keyvals;
3697c478bd9Sstevel@tonic-gate break;
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate xpcr->pic[0] = (uint8_t)((pcr >> kv[D_pic0].kv_shift) &
3727c478bd9Sstevel@tonic-gate kv[D_pic0].kv_mask);
3737c478bd9Sstevel@tonic-gate xpcr->pic[1] = (uint8_t)((pcr >> kv[D_pic1].kv_shift) &
3747c478bd9Sstevel@tonic-gate kv[D_pic1].kv_mask);
3757c478bd9Sstevel@tonic-gate xpcr->usr = (pcr >> kv[D_nouser].kv_shift) &
3767c478bd9Sstevel@tonic-gate kv[D_nouser].kv_mask;
3777c478bd9Sstevel@tonic-gate xpcr->sys = (pcr >> kv[D_sys].kv_shift) &
3787c478bd9Sstevel@tonic-gate kv[D_sys].kv_mask;
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate char *
cpc_eventtostr(cpc_event_t * event)3827c478bd9Sstevel@tonic-gate cpc_eventtostr(cpc_event_t *event)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate struct xpcr xpcr;
3857c478bd9Sstevel@tonic-gate char *pic[2];
3867c478bd9Sstevel@tonic-gate char buffer[1024];
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate switch (event->ce_cpuver) {
3897c478bd9Sstevel@tonic-gate case CPC_ULTRA1:
3907c478bd9Sstevel@tonic-gate case CPC_ULTRA2:
3917c478bd9Sstevel@tonic-gate case CPC_ULTRA3:
3927c478bd9Sstevel@tonic-gate case CPC_ULTRA3_PLUS:
3937c478bd9Sstevel@tonic-gate case CPC_ULTRA3_I:
39423961e2bSvb70745 case CPC_ULTRA4_PLUS:
3957c478bd9Sstevel@tonic-gate break;
3967c478bd9Sstevel@tonic-gate default:
3977c478bd9Sstevel@tonic-gate return (NULL);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate unmake_pcr(event->ce_pcr, event->ce_cpuver, &xpcr);
4017c478bd9Sstevel@tonic-gate if ((pic[0] = regtostr(event->ce_cpuver, 0, xpcr.pic[0])) == NULL)
4027c478bd9Sstevel@tonic-gate return (NULL);
4037c478bd9Sstevel@tonic-gate if ((pic[1] = regtostr(event->ce_cpuver, 1, xpcr.pic[1])) == NULL) {
4047c478bd9Sstevel@tonic-gate free(pic[0]);
4057c478bd9Sstevel@tonic-gate return (NULL);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate (void) snprintf(buffer, sizeof (buffer), "%s=%s,%s=%s",
4097c478bd9Sstevel@tonic-gate tokens[D_pic0], pic[0], tokens[D_pic1], pic[1]);
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate free(pic[1]);
4127c478bd9Sstevel@tonic-gate free(pic[0]);
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate if (!xpcr.usr)
4157c478bd9Sstevel@tonic-gate (void) strcat(strcat(buffer, ","), tokens[D_nouser]);
4167c478bd9Sstevel@tonic-gate if (xpcr.sys)
4177c478bd9Sstevel@tonic-gate (void) strcat(strcat(buffer, ","), tokens[D_sys]);
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate return (strdup(buffer));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Utility operations on events
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate void
cpc_event_accum(cpc_event_t * accum,cpc_event_t * event)4267c478bd9Sstevel@tonic-gate cpc_event_accum(cpc_event_t *accum, cpc_event_t *event)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate if (accum->ce_hrt < event->ce_hrt)
4297c478bd9Sstevel@tonic-gate accum->ce_hrt = event->ce_hrt;
4307c478bd9Sstevel@tonic-gate accum->ce_tick += event->ce_tick;
4317c478bd9Sstevel@tonic-gate accum->ce_pic[0] += event->ce_pic[0];
4327c478bd9Sstevel@tonic-gate accum->ce_pic[1] += event->ce_pic[1];
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate void
cpc_event_diff(cpc_event_t * diff,cpc_event_t * left,cpc_event_t * right)4367c478bd9Sstevel@tonic-gate cpc_event_diff(cpc_event_t *diff, cpc_event_t *left, cpc_event_t *right)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate diff->ce_hrt = left->ce_hrt;
4397c478bd9Sstevel@tonic-gate diff->ce_tick = left->ce_tick - right->ce_tick;
4407c478bd9Sstevel@tonic-gate diff->ce_pic[0] = left->ce_pic[0] - right->ce_pic[0];
4417c478bd9Sstevel@tonic-gate diff->ce_pic[1] = left->ce_pic[1] - right->ce_pic[1];
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate * Given a cpc_event_t and cpc_bind_event() flags, translate the event into the
4467c478bd9Sstevel@tonic-gate * cpc_set_t format.
4477c478bd9Sstevel@tonic-gate *
4487c478bd9Sstevel@tonic-gate * Returns NULL on failure.
4497c478bd9Sstevel@tonic-gate */
4507c478bd9Sstevel@tonic-gate cpc_set_t *
__cpc_eventtoset(cpc_t * cpc,cpc_event_t * event,int iflags)4517c478bd9Sstevel@tonic-gate __cpc_eventtoset(cpc_t *cpc, cpc_event_t *event, int iflags)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate cpc_set_t *set = NULL;
4547c478bd9Sstevel@tonic-gate struct xpcr xpcr;
4557c478bd9Sstevel@tonic-gate char *pic[2];
4567c478bd9Sstevel@tonic-gate uint32_t flag = 0;
4577c478bd9Sstevel@tonic-gate cpc_attr_t attr = { "picnum", 0 };
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate switch (event->ce_cpuver) {
4607c478bd9Sstevel@tonic-gate case CPC_ULTRA1:
4617c478bd9Sstevel@tonic-gate case CPC_ULTRA2:
4627c478bd9Sstevel@tonic-gate case CPC_ULTRA3:
4637c478bd9Sstevel@tonic-gate case CPC_ULTRA3_PLUS:
4647c478bd9Sstevel@tonic-gate case CPC_ULTRA3_I:
46523961e2bSvb70745 case CPC_ULTRA4_PLUS:
4667c478bd9Sstevel@tonic-gate break;
4677c478bd9Sstevel@tonic-gate default:
4687c478bd9Sstevel@tonic-gate return (NULL);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate unmake_pcr(event->ce_pcr, event->ce_cpuver, &xpcr);
4727c478bd9Sstevel@tonic-gate if ((pic[0] = regtostr(event->ce_cpuver, 0, xpcr.pic[0])) == NULL)
4737c478bd9Sstevel@tonic-gate return (NULL);
4747c478bd9Sstevel@tonic-gate if ((pic[1] = regtostr(event->ce_cpuver, 1, xpcr.pic[1])) == NULL) {
4757c478bd9Sstevel@tonic-gate free(pic[0]);
4767c478bd9Sstevel@tonic-gate return (NULL);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate if (xpcr.usr)
4807c478bd9Sstevel@tonic-gate flag |= CPC_COUNT_USER;
4817c478bd9Sstevel@tonic-gate if (xpcr.sys)
4827c478bd9Sstevel@tonic-gate flag |= CPC_COUNT_SYSTEM;
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate if (iflags & CPC_BIND_EMT_OVF)
4857c478bd9Sstevel@tonic-gate flag |= CPC_OVF_NOTIFY_EMT;
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate if ((set = cpc_set_create(cpc)) == NULL)
4887c478bd9Sstevel@tonic-gate goto bad;
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate if (cpc_set_add_request(cpc, set, pic[0], event->ce_pic[0], flag,
4917c478bd9Sstevel@tonic-gate 1, &attr) != 0)
4927c478bd9Sstevel@tonic-gate goto bad;
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate attr.ca_val = 1;
4957c478bd9Sstevel@tonic-gate if (cpc_set_add_request(cpc, set, pic[1], event->ce_pic[1], flag,
4967c478bd9Sstevel@tonic-gate 1, &attr) != 1)
4977c478bd9Sstevel@tonic-gate goto bad;
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate free(pic[0]);
5007c478bd9Sstevel@tonic-gate free(pic[1]);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate return (set);
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate bad:
5057c478bd9Sstevel@tonic-gate if (set != NULL)
5067c478bd9Sstevel@tonic-gate (void) cpc_set_destroy(cpc, set);
5077c478bd9Sstevel@tonic-gate free(pic[0]);
5087c478bd9Sstevel@tonic-gate free(pic[1]);
5097c478bd9Sstevel@tonic-gate return (NULL);
5107c478bd9Sstevel@tonic-gate }
511