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
599f63845Sab * Common Development and Distribution License (the "License").
699f63845Sab * 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 /*
2299f63845Sab * 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 /*
27ebb8ac07SRobert Mustacchi * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28*56726c7eSRobert Mustacchi * Copyright 2022 Oxide Computer Company
29eb23829fSBryan Cantrill */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
337c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuid_drv.h>
387c478bd9Sstevel@tonic-gate #include <sys/elf.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <strings.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <libintl.h>
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include <elfcap.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static const char dev_cpu_self_cpuid[] = "/dev/" CPUID_SELF_NAME;
527c478bd9Sstevel@tonic-gate static char *pgmname;
537c478bd9Sstevel@tonic-gate static int mode = 0;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define BITS_MODE 0x1
567c478bd9Sstevel@tonic-gate #define NATIVE_MODE 0x2
577c478bd9Sstevel@tonic-gate #define KERN_MODE 0x4
587c478bd9Sstevel@tonic-gate #define VERBOSE_MODE 0x8
597c478bd9Sstevel@tonic-gate #define EXTN_MODE 0x10
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static char *
getsysinfo(int cmd)627c478bd9Sstevel@tonic-gate getsysinfo(int cmd)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate char *buf;
657c478bd9Sstevel@tonic-gate size_t bufsize = 20; /* wild guess */
667c478bd9Sstevel@tonic-gate long ret;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL)
697c478bd9Sstevel@tonic-gate return (NULL);
707c478bd9Sstevel@tonic-gate do {
717c478bd9Sstevel@tonic-gate ret = sysinfo(cmd, buf, bufsize);
727c478bd9Sstevel@tonic-gate if (ret == -1)
737c478bd9Sstevel@tonic-gate return (NULL);
747c478bd9Sstevel@tonic-gate if (ret > bufsize) {
757c478bd9Sstevel@tonic-gate bufsize = ret;
767c478bd9Sstevel@tonic-gate buf = realloc(buf, bufsize);
777c478bd9Sstevel@tonic-gate } else
787c478bd9Sstevel@tonic-gate break;
797c478bd9Sstevel@tonic-gate } while (buf != NULL);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate return (buf);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Classify isa's as to bitness of the corresponding ABIs.
867c478bd9Sstevel@tonic-gate * isa's which have no "official" Solaris ABI are returned
877c478bd9Sstevel@tonic-gate * unrecognised i.e. "zero bit".
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate static uint_t
bitness(const char * isaname)907c478bd9Sstevel@tonic-gate bitness(const char *isaname)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0 ||
937c478bd9Sstevel@tonic-gate strcmp(isaname, "i386") == 0)
947c478bd9Sstevel@tonic-gate return (32);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0 ||
977c478bd9Sstevel@tonic-gate strcmp(isaname, "amd64") == 0)
987c478bd9Sstevel@tonic-gate return (64);
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate return (0);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate static char *
report_abi(int cmd,const char * vfmt)1047c478bd9Sstevel@tonic-gate report_abi(int cmd, const char *vfmt)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate uint_t bits;
1077c478bd9Sstevel@tonic-gate char *isa;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if ((isa = getsysinfo(cmd)) == NULL)
1107c478bd9Sstevel@tonic-gate return (0);
1117c478bd9Sstevel@tonic-gate if ((bits = bitness(isa)) == 0) {
1127c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1137c478bd9Sstevel@tonic-gate gettext("%s: unable to identify isa '%s'!\n"),
1147c478bd9Sstevel@tonic-gate pgmname, isa);
1157c478bd9Sstevel@tonic-gate exit(3);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if (mode & VERBOSE_MODE)
1197c478bd9Sstevel@tonic-gate (void) printf(vfmt, bits, isa);
1207c478bd9Sstevel@tonic-gate else if (mode & BITS_MODE)
1217c478bd9Sstevel@tonic-gate (void) printf("%d\n", bits);
1227c478bd9Sstevel@tonic-gate else if (mode & (NATIVE_MODE|KERN_MODE))
1237c478bd9Sstevel@tonic-gate (void) printf("%s\n", isa);
1247c478bd9Sstevel@tonic-gate else
1257c478bd9Sstevel@tonic-gate (void) printf("%s", isa);
1267c478bd9Sstevel@tonic-gate return (isa);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * Classify isas as their machine type.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate static ushort_t
machtype(const char * isaname)1337c478bd9Sstevel@tonic-gate machtype(const char *isaname)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0)
1367c478bd9Sstevel@tonic-gate return (EM_SPARC);
1377c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0)
1387c478bd9Sstevel@tonic-gate return (EM_SPARCV9);
1397c478bd9Sstevel@tonic-gate if (strcmp(isaname, "i386") == 0)
1407c478bd9Sstevel@tonic-gate return (EM_386);
1417c478bd9Sstevel@tonic-gate if (strcmp(isaname, "amd64") == 0)
1427c478bd9Sstevel@tonic-gate return (EM_AMD64);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate static void
report_hwcap(int d,const char * isa)1487c478bd9Sstevel@tonic-gate report_hwcap(int d, const char *isa)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate struct cpuid_get_hwcap __cgh, *cgh = &__cgh;
151*56726c7eSRobert Mustacchi char cap1[ELFCAP_HW1_BUFSIZE];
152*56726c7eSRobert Mustacchi char cap2[ELFCAP_HW2_BUFSIZE];
153*56726c7eSRobert Mustacchi char cap3[ELFCAP_HW3_BUFSIZE];
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate cgh->cgh_archname = (char *)isa;
1567c478bd9Sstevel@tonic-gate if (ioctl(d, CPUID_GET_HWCAP, cgh) != 0)
1577c478bd9Sstevel@tonic-gate return;
1587c478bd9Sstevel@tonic-gate
159ebb8ac07SRobert Mustacchi (void) elfcap_hw1_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap[0],
160*56726c7eSRobert Mustacchi cap1, sizeof (cap1), ELFCAP_FMT_SNGSPACE, machtype(isa));
1617c478bd9Sstevel@tonic-gate
162*56726c7eSRobert Mustacchi if (cgh->cgh_hwcap[1] != 0) {
163ebb8ac07SRobert Mustacchi (void) elfcap_hw2_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap[1],
164ebb8ac07SRobert Mustacchi cap2, sizeof (cap2), ELFCAP_FMT_SNGSPACE, machtype(isa));
165*56726c7eSRobert Mustacchi } else {
166ebb8ac07SRobert Mustacchi cap2[0] = '\0';
167*56726c7eSRobert Mustacchi }
168*56726c7eSRobert Mustacchi
169*56726c7eSRobert Mustacchi if (cgh->cgh_hwcap[2] != 0) {
170*56726c7eSRobert Mustacchi (void) elfcap_hw3_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap[2],
171*56726c7eSRobert Mustacchi cap3, sizeof (cap3), ELFCAP_FMT_SNGSPACE, machtype(isa));
172*56726c7eSRobert Mustacchi } else {
173*56726c7eSRobert Mustacchi cap3[0] = '\0';
174*56726c7eSRobert Mustacchi }
175ebb8ac07SRobert Mustacchi
1767c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE) {
177ebb8ac07SRobert Mustacchi (void) printf(":");
178*56726c7eSRobert Mustacchi if (cgh->cgh_hwcap[2] != 0)
179*56726c7eSRobert Mustacchi (void) printf(" %s", cap3);
18008cab4beSToomas Soome if (cgh->cgh_hwcap[1] != 0)
181ebb8ac07SRobert Mustacchi (void) printf(" %s", cap2);
182*56726c7eSRobert Mustacchi (void) printf(" %s", cap1);
183ebb8ac07SRobert Mustacchi (void) printf("\n");
1847c478bd9Sstevel@tonic-gate } else {
1857c478bd9Sstevel@tonic-gate char *p;
1867c478bd9Sstevel@tonic-gate int linecnt = 0;
1877c478bd9Sstevel@tonic-gate
188*56726c7eSRobert Mustacchi for (p = strtok(cap3, " "); p; p = strtok(NULL, " ")) {
189*56726c7eSRobert Mustacchi if (linecnt + strlen(p) > 68) {
190*56726c7eSRobert Mustacchi (void) printf("\n");
191*56726c7eSRobert Mustacchi linecnt = 0;
192*56726c7eSRobert Mustacchi }
193*56726c7eSRobert Mustacchi if (linecnt == 0)
194*56726c7eSRobert Mustacchi linecnt = printf("\t");
195*56726c7eSRobert Mustacchi linecnt += printf("%s ", p);
196*56726c7eSRobert Mustacchi }
197*56726c7eSRobert Mustacchi
198ebb8ac07SRobert Mustacchi for (p = strtok(cap2, " "); p; p = strtok(NULL, " ")) {
199ebb8ac07SRobert Mustacchi if (linecnt + strlen(p) > 68) {
200ebb8ac07SRobert Mustacchi (void) printf("\n");
201ebb8ac07SRobert Mustacchi linecnt = 0;
202ebb8ac07SRobert Mustacchi }
203ebb8ac07SRobert Mustacchi if (linecnt == 0)
204ebb8ac07SRobert Mustacchi linecnt = printf("\t");
205ebb8ac07SRobert Mustacchi linecnt += printf("%s ", p);
206ebb8ac07SRobert Mustacchi }
207ebb8ac07SRobert Mustacchi
208*56726c7eSRobert Mustacchi for (p = strtok(cap1, " "); p; p = strtok(NULL, " ")) {
209eb23829fSBryan Cantrill if (linecnt + strlen(p) > 68) {
2107c478bd9Sstevel@tonic-gate (void) printf("\n");
2117c478bd9Sstevel@tonic-gate linecnt = 0;
2127c478bd9Sstevel@tonic-gate }
213eb23829fSBryan Cantrill if (linecnt == 0)
214eb23829fSBryan Cantrill linecnt = printf("\t");
215eb23829fSBryan Cantrill linecnt += printf("%s ", p);
2167c478bd9Sstevel@tonic-gate }
217ebb8ac07SRobert Mustacchi
2187c478bd9Sstevel@tonic-gate if (linecnt != 0)
2197c478bd9Sstevel@tonic-gate (void) printf("\n");
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2247c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
2257c478bd9Sstevel@tonic-gate #endif
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2287c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate int errflg = 0;
2317c478bd9Sstevel@tonic-gate int c;
2327c478bd9Sstevel@tonic-gate char *vfmt;
2337c478bd9Sstevel@tonic-gate char *isa, *isa32;
2347c478bd9Sstevel@tonic-gate int d = -1;
2357c478bd9Sstevel@tonic-gate const int excl_modes = /* exclusive mode settings */
2367c478bd9Sstevel@tonic-gate NATIVE_MODE | BITS_MODE | KERN_MODE | EXTN_MODE;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2397c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate if ((pgmname = strrchr(*argv, '/')) == 0)
2427c478bd9Sstevel@tonic-gate pgmname = argv[0];
2437c478bd9Sstevel@tonic-gate else
2447c478bd9Sstevel@tonic-gate pgmname++;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "nbkvx")) != EOF)
2477c478bd9Sstevel@tonic-gate switch (c) {
2487c478bd9Sstevel@tonic-gate case 'n':
2497c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2507c478bd9Sstevel@tonic-gate errflg++;
2517c478bd9Sstevel@tonic-gate mode |= NATIVE_MODE;
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate case 'b':
2547c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2557c478bd9Sstevel@tonic-gate errflg++;
2567c478bd9Sstevel@tonic-gate mode |= BITS_MODE;
2577c478bd9Sstevel@tonic-gate break;
2587c478bd9Sstevel@tonic-gate case 'k':
2597c478bd9Sstevel@tonic-gate if (mode & excl_modes)
2607c478bd9Sstevel@tonic-gate errflg++;
2617c478bd9Sstevel@tonic-gate mode |= KERN_MODE;
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate case 'x':
2647c478bd9Sstevel@tonic-gate if (mode & excl_modes || mode & VERBOSE_MODE)
2657c478bd9Sstevel@tonic-gate errflg++;
2667c478bd9Sstevel@tonic-gate mode |= EXTN_MODE;
2677c478bd9Sstevel@tonic-gate break;
2687c478bd9Sstevel@tonic-gate case 'v':
2697c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE)
2707c478bd9Sstevel@tonic-gate errflg++;
2717c478bd9Sstevel@tonic-gate mode |= VERBOSE_MODE;
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate case '?':
2747c478bd9Sstevel@tonic-gate default:
2757c478bd9Sstevel@tonic-gate errflg++;
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate if (errflg || optind != argc) {
2807c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2817c478bd9Sstevel@tonic-gate gettext("usage: %s [ [-v] [-b | -n | -k] | [-x] ]\n"),
2827c478bd9Sstevel@tonic-gate pgmname);
2837c478bd9Sstevel@tonic-gate return (1);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate * We use dev_cpu_self_cpuid for discovering hardware capabilities;
2887c478bd9Sstevel@tonic-gate * but we only complain if we can't open it if we've been
2897c478bd9Sstevel@tonic-gate * asked to report on those capabilities.
2907c478bd9Sstevel@tonic-gate */
2917c478bd9Sstevel@tonic-gate if ((mode & (VERBOSE_MODE|EXTN_MODE)) != 0 &&
2927c478bd9Sstevel@tonic-gate (d = open(dev_cpu_self_cpuid, O_RDONLY)) == -1)
2937c478bd9Sstevel@tonic-gate perror(dev_cpu_self_cpuid), exit(1);
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (mode & KERN_MODE) {
2967c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s kernel modules\n");
2977c478bd9Sstevel@tonic-gate (void) report_abi(SI_ARCHITECTURE_K, vfmt);
2987c478bd9Sstevel@tonic-gate return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s applications\n");
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate if (mode & (BITS_MODE | NATIVE_MODE)) {
3047c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) == NULL)
3057c478bd9Sstevel@tonic-gate isa = report_abi(SI_ARCHITECTURE_32, vfmt);
3067c478bd9Sstevel@tonic-gate if (isa != NULL && (mode & VERBOSE_MODE) != 0)
3077c478bd9Sstevel@tonic-gate report_hwcap(d, isa);
3087c478bd9Sstevel@tonic-gate } else {
3097c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) != NULL) {
3107c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE))
3117c478bd9Sstevel@tonic-gate report_hwcap(d, isa);
3127c478bd9Sstevel@tonic-gate else
3137c478bd9Sstevel@tonic-gate (void) putchar(' ');
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate if ((isa32 = report_abi(SI_ARCHITECTURE_32, vfmt)) != NULL) {
3177c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE))
3187c478bd9Sstevel@tonic-gate report_hwcap(d, isa32);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate if ((isa32 != NULL || isa != NULL) &&
3227c478bd9Sstevel@tonic-gate (mode & (EXTN_MODE|VERBOSE_MODE)) == 0)
3237c478bd9Sstevel@tonic-gate (void) putchar('\n');
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate return (0);
3277c478bd9Sstevel@tonic-gate }
328