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
599f63845Sab196087 * Common Development and Distribution License (the "License").
699f63845Sab196087 * 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 /*
2299f63845Sab196087 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26eb23829fSBryan Cantrill /*
27*ebb8ac07SRobert Mustacchi * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28eb23829fSBryan Cantrill */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
327c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
367c478bd9Sstevel@tonic-gate #include <sys/cpuid_drv.h>
377c478bd9Sstevel@tonic-gate #include <sys/elf.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <libintl.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include <elfcap.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static const char dev_cpu_self_cpuid[] = "/dev/" CPUID_SELF_NAME;
517c478bd9Sstevel@tonic-gate static char *pgmname;
527c478bd9Sstevel@tonic-gate static int mode = 0;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #define BITS_MODE 0x1
557c478bd9Sstevel@tonic-gate #define NATIVE_MODE 0x2
567c478bd9Sstevel@tonic-gate #define KERN_MODE 0x4
577c478bd9Sstevel@tonic-gate #define VERBOSE_MODE 0x8
587c478bd9Sstevel@tonic-gate #define EXTN_MODE 0x10
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static char *
getsysinfo(int cmd)617c478bd9Sstevel@tonic-gate getsysinfo(int cmd)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate char *buf;
647c478bd9Sstevel@tonic-gate size_t bufsize = 20; /* wild guess */
657c478bd9Sstevel@tonic-gate long ret;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL)
687c478bd9Sstevel@tonic-gate return (NULL);
697c478bd9Sstevel@tonic-gate do {
707c478bd9Sstevel@tonic-gate ret = sysinfo(cmd, buf, bufsize);
717c478bd9Sstevel@tonic-gate if (ret == -1)
727c478bd9Sstevel@tonic-gate return (NULL);
737c478bd9Sstevel@tonic-gate if (ret > bufsize) {
747c478bd9Sstevel@tonic-gate bufsize = ret;
757c478bd9Sstevel@tonic-gate buf = realloc(buf, bufsize);
767c478bd9Sstevel@tonic-gate } else
777c478bd9Sstevel@tonic-gate break;
787c478bd9Sstevel@tonic-gate } while (buf != NULL);
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate return (buf);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * Classify isa's as to bitness of the corresponding ABIs.
857c478bd9Sstevel@tonic-gate * isa's which have no "official" Solaris ABI are returned
867c478bd9Sstevel@tonic-gate * unrecognised i.e. "zero bit".
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate static uint_t
bitness(const char * isaname)897c478bd9Sstevel@tonic-gate bitness(const char *isaname)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0 ||
927c478bd9Sstevel@tonic-gate strcmp(isaname, "i386") == 0)
937c478bd9Sstevel@tonic-gate return (32);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0 ||
967c478bd9Sstevel@tonic-gate strcmp(isaname, "amd64") == 0)
977c478bd9Sstevel@tonic-gate return (64);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate return (0);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate static char *
report_abi(int cmd,const char * vfmt)1037c478bd9Sstevel@tonic-gate report_abi(int cmd, const char *vfmt)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate uint_t bits;
1067c478bd9Sstevel@tonic-gate char *isa;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate if ((isa = getsysinfo(cmd)) == NULL)
1097c478bd9Sstevel@tonic-gate return (0);
1107c478bd9Sstevel@tonic-gate if ((bits = bitness(isa)) == 0) {
1117c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1127c478bd9Sstevel@tonic-gate gettext("%s: unable to identify isa '%s'!\n"),
1137c478bd9Sstevel@tonic-gate pgmname, isa);
1147c478bd9Sstevel@tonic-gate exit(3);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate if (mode & VERBOSE_MODE)
1187c478bd9Sstevel@tonic-gate (void) printf(vfmt, bits, isa);
1197c478bd9Sstevel@tonic-gate else if (mode & BITS_MODE)
1207c478bd9Sstevel@tonic-gate (void) printf("%d\n", bits);
1217c478bd9Sstevel@tonic-gate else if (mode & (NATIVE_MODE|KERN_MODE))
1227c478bd9Sstevel@tonic-gate (void) printf("%s\n", isa);
1237c478bd9Sstevel@tonic-gate else
1247c478bd9Sstevel@tonic-gate (void) printf("%s", isa);
1257c478bd9Sstevel@tonic-gate return (isa);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * Classify isas as their machine type.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate static ushort_t
machtype(const char * isaname)1327c478bd9Sstevel@tonic-gate machtype(const char *isaname)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0)
1357c478bd9Sstevel@tonic-gate return (EM_SPARC);
1367c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0)
1377c478bd9Sstevel@tonic-gate return (EM_SPARCV9);
1387c478bd9Sstevel@tonic-gate if (strcmp(isaname, "i386") == 0)
1397c478bd9Sstevel@tonic-gate return (EM_386);
1407c478bd9Sstevel@tonic-gate if (strcmp(isaname, "amd64") == 0)
1417c478bd9Sstevel@tonic-gate return (EM_AMD64);
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate return (0);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate static void
report_hwcap(int d,const char * isa)1477c478bd9Sstevel@tonic-gate report_hwcap(int d, const char *isa)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate struct cpuid_get_hwcap __cgh, *cgh = &__cgh;
150*ebb8ac07SRobert Mustacchi char buffer[1024], cap2[1024];
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate cgh->cgh_archname = (char *)isa;
1537c478bd9Sstevel@tonic-gate if (ioctl(d, CPUID_GET_HWCAP, cgh) != 0)
1547c478bd9Sstevel@tonic-gate return;
1557c478bd9Sstevel@tonic-gate
156*ebb8ac07SRobert Mustacchi (void) elfcap_hw1_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap[0],
15799f63845Sab196087 buffer, sizeof (buffer), ELFCAP_FMT_SNGSPACE, machtype(isa));
1587c478bd9Sstevel@tonic-gate
159*ebb8ac07SRobert Mustacchi if (cgh->cgh_hwcap[1] != 0)
160*ebb8ac07SRobert Mustacchi (void) elfcap_hw2_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap[1],
161*ebb8ac07SRobert Mustacchi cap2, sizeof (cap2), ELFCAP_FMT_SNGSPACE, machtype(isa));
162*ebb8ac07SRobert Mustacchi else
163*ebb8ac07SRobert Mustacchi cap2[0] = '\0';
164*ebb8ac07SRobert Mustacchi
1657c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE) {
166*ebb8ac07SRobert Mustacchi (void) printf(":");
167*ebb8ac07SRobert Mustacchi if (cgh->cgh_hwcap[1] != NULL)
168*ebb8ac07SRobert Mustacchi (void) printf(" %s", cap2);
169*ebb8ac07SRobert Mustacchi (void) printf(" %s", buffer);
170*ebb8ac07SRobert Mustacchi (void) printf("\n");
1717c478bd9Sstevel@tonic-gate } else {
1727c478bd9Sstevel@tonic-gate char *p;
1737c478bd9Sstevel@tonic-gate int linecnt = 0;
1747c478bd9Sstevel@tonic-gate
175*ebb8ac07SRobert Mustacchi for (p = strtok(cap2, " "); p; p = strtok(NULL, " ")) {
176*ebb8ac07SRobert Mustacchi if (linecnt + strlen(p) > 68) {
177*ebb8ac07SRobert Mustacchi (void) printf("\n");
178*ebb8ac07SRobert Mustacchi linecnt = 0;
179*ebb8ac07SRobert Mustacchi }
180*ebb8ac07SRobert Mustacchi if (linecnt == 0)
181*ebb8ac07SRobert Mustacchi linecnt = printf("\t");
182*ebb8ac07SRobert Mustacchi linecnt += printf("%s ", p);
183*ebb8ac07SRobert Mustacchi }
184*ebb8ac07SRobert Mustacchi
1857c478bd9Sstevel@tonic-gate for (p = strtok(buffer, " "); p; p = strtok(NULL, " ")) {
186eb23829fSBryan Cantrill if (linecnt + strlen(p) > 68) {
1877c478bd9Sstevel@tonic-gate (void) printf("\n");
1887c478bd9Sstevel@tonic-gate linecnt = 0;
1897c478bd9Sstevel@tonic-gate }
190eb23829fSBryan Cantrill if (linecnt == 0)
191eb23829fSBryan Cantrill linecnt = printf("\t");
192eb23829fSBryan Cantrill linecnt += printf("%s ", p);
1937c478bd9Sstevel@tonic-gate }
194*ebb8ac07SRobert Mustacchi
1957c478bd9Sstevel@tonic-gate if (linecnt != 0)
1967c478bd9Sstevel@tonic-gate (void) printf("\n");
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2017c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
2027c478bd9Sstevel@tonic-gate #endif
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2057c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate int errflg = 0;
2087c478bd9Sstevel@tonic-gate int c;
2097c478bd9Sstevel@tonic-gate char *vfmt;
2107c478bd9Sstevel@tonic-gate char *isa, *isa32;
2117c478bd9Sstevel@tonic-gate int d = -1;
2127c478bd9Sstevel@tonic-gate const int excl_modes = /* exclusive mode settings */
2137c478bd9Sstevel@tonic-gate NATIVE_MODE | BITS_MODE | KERN_MODE | EXTN_MODE;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2167c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate if ((pgmname = strrchr(*argv, '/')) == 0)
2197c478bd9Sstevel@tonic-gate pgmname = argv[0];
2207c478bd9Sstevel@tonic-gate else
2217c478bd9Sstevel@tonic-gate pgmname++;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "nbkvx")) != EOF)
2247c478bd9Sstevel@tonic-gate switch (c) {
2257c478bd9Sstevel@tonic-gate case 'n':
2267c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2277c478bd9Sstevel@tonic-gate errflg++;
2287c478bd9Sstevel@tonic-gate mode |= NATIVE_MODE;
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate case 'b':
2317c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2327c478bd9Sstevel@tonic-gate errflg++;
2337c478bd9Sstevel@tonic-gate mode |= BITS_MODE;
2347c478bd9Sstevel@tonic-gate break;
2357c478bd9Sstevel@tonic-gate case 'k':
2367c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2377c478bd9Sstevel@tonic-gate errflg++;
2387c478bd9Sstevel@tonic-gate mode |= KERN_MODE;
2397c478bd9Sstevel@tonic-gate break;
2407c478bd9Sstevel@tonic-gate case 'x':
2417c478bd9Sstevel@tonic-gate if (mode & excl_modes || mode & VERBOSE_MODE)
2427c478bd9Sstevel@tonic-gate errflg++;
2437c478bd9Sstevel@tonic-gate mode |= EXTN_MODE;
2447c478bd9Sstevel@tonic-gate break;
2457c478bd9Sstevel@tonic-gate case 'v':
2467c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE)
2477c478bd9Sstevel@tonic-gate errflg++;
2487c478bd9Sstevel@tonic-gate mode |= VERBOSE_MODE;
2497c478bd9Sstevel@tonic-gate break;
2507c478bd9Sstevel@tonic-gate case '?':
2517c478bd9Sstevel@tonic-gate default:
2527c478bd9Sstevel@tonic-gate errflg++;
2537c478bd9Sstevel@tonic-gate break;
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate if (errflg || optind != argc) {
2577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2587c478bd9Sstevel@tonic-gate gettext("usage: %s [ [-v] [-b | -n | -k] | [-x] ]\n"),
2597c478bd9Sstevel@tonic-gate pgmname);
2607c478bd9Sstevel@tonic-gate return (1);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate * We use dev_cpu_self_cpuid for discovering hardware capabilities;
2657c478bd9Sstevel@tonic-gate * but we only complain if we can't open it if we've been
2667c478bd9Sstevel@tonic-gate * asked to report on those capabilities.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate if ((mode & (VERBOSE_MODE|EXTN_MODE)) != 0 &&
2697c478bd9Sstevel@tonic-gate (d = open(dev_cpu_self_cpuid, O_RDONLY)) == -1)
2707c478bd9Sstevel@tonic-gate perror(dev_cpu_self_cpuid), exit(1);
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate if (mode & KERN_MODE) {
2737c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s kernel modules\n");
2747c478bd9Sstevel@tonic-gate (void) report_abi(SI_ARCHITECTURE_K, vfmt);
2757c478bd9Sstevel@tonic-gate return (0);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s applications\n");
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate if (mode & (BITS_MODE | NATIVE_MODE)) {
2817c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) == NULL)
2827c478bd9Sstevel@tonic-gate isa = report_abi(SI_ARCHITECTURE_32, vfmt);
2837c478bd9Sstevel@tonic-gate if (isa != NULL && (mode & VERBOSE_MODE) != 0)
2847c478bd9Sstevel@tonic-gate report_hwcap(d, isa);
2857c478bd9Sstevel@tonic-gate } else {
2867c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) != NULL) {
2877c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE))
2887c478bd9Sstevel@tonic-gate report_hwcap(d, isa);
2897c478bd9Sstevel@tonic-gate else
2907c478bd9Sstevel@tonic-gate (void) putchar(' ');
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate if ((isa32 = report_abi(SI_ARCHITECTURE_32, vfmt)) != NULL) {
2947c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE))
2957c478bd9Sstevel@tonic-gate report_hwcap(d, isa32);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if ((isa32 != NULL || isa != NULL) &&
2997c478bd9Sstevel@tonic-gate (mode & (EXTN_MODE|VERBOSE_MODE)) == 0)
3007c478bd9Sstevel@tonic-gate (void) putchar('\n');
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate return (0);
3047c478bd9Sstevel@tonic-gate }
305