xref: /illumos-gate/usr/src/cmd/isainfo/isainfo.c (revision eb23829ff08a873c612ac45d191d559394b4b408)
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 
26*eb23829fSBryan Cantrill /*
27*eb23829fSBryan Cantrill  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
28*eb23829fSBryan 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 *
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
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 *
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
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
1477c478bd9Sstevel@tonic-gate report_hwcap(int d, const char *isa)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	struct cpuid_get_hwcap __cgh, *cgh = &__cgh;
1507c478bd9Sstevel@tonic-gate 	char buffer[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 
15699f63845Sab196087 	(void) elfcap_hw1_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap,
15799f63845Sab196087 	    buffer, sizeof (buffer), ELFCAP_FMT_SNGSPACE, machtype(isa));
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (mode & EXTN_MODE) {
1607c478bd9Sstevel@tonic-gate 		(void) printf(": %s\n", buffer);
1617c478bd9Sstevel@tonic-gate 	} else {
1627c478bd9Sstevel@tonic-gate 		char *p;
1637c478bd9Sstevel@tonic-gate 		int linecnt = 0;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		for (p = strtok(buffer, " "); p; p = strtok(NULL, " ")) {
166*eb23829fSBryan Cantrill 			if (linecnt + strlen(p) > 68) {
1677c478bd9Sstevel@tonic-gate 				(void) printf("\n");
1687c478bd9Sstevel@tonic-gate 				linecnt = 0;
1697c478bd9Sstevel@tonic-gate 			}
170*eb23829fSBryan Cantrill 			if (linecnt == 0)
171*eb23829fSBryan Cantrill 				linecnt = printf("\t");
172*eb23829fSBryan Cantrill 			linecnt += printf("%s ", p);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		if (linecnt != 0)
1757c478bd9Sstevel@tonic-gate 			(void) printf("\n");
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1807c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate int
1847c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int errflg = 0;
1877c478bd9Sstevel@tonic-gate 	int c;
1887c478bd9Sstevel@tonic-gate 	char *vfmt;
1897c478bd9Sstevel@tonic-gate 	char *isa, *isa32;
1907c478bd9Sstevel@tonic-gate 	int d = -1;
1917c478bd9Sstevel@tonic-gate 	const int excl_modes =	/* exclusive mode settings */
1927c478bd9Sstevel@tonic-gate 	    NATIVE_MODE | BITS_MODE | KERN_MODE | EXTN_MODE;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1957c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if ((pgmname = strrchr(*argv, '/')) == 0)
1987c478bd9Sstevel@tonic-gate 		pgmname = argv[0];
1997c478bd9Sstevel@tonic-gate 	else
2007c478bd9Sstevel@tonic-gate 		pgmname++;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "nbkvx")) != EOF)
2037c478bd9Sstevel@tonic-gate 		switch (c) {
2047c478bd9Sstevel@tonic-gate 		case 'n':
2057c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2067c478bd9Sstevel@tonic-gate 				errflg++;
2077c478bd9Sstevel@tonic-gate 			mode |= NATIVE_MODE;
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		case 'b':
2107c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2117c478bd9Sstevel@tonic-gate 				errflg++;
2127c478bd9Sstevel@tonic-gate 			mode |= BITS_MODE;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 		case 'k':
2157c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2167c478bd9Sstevel@tonic-gate 				errflg++;
2177c478bd9Sstevel@tonic-gate 			mode |= KERN_MODE;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case 'x':
2207c478bd9Sstevel@tonic-gate 			if (mode & excl_modes || mode & VERBOSE_MODE)
2217c478bd9Sstevel@tonic-gate 				errflg++;
2227c478bd9Sstevel@tonic-gate 			mode |= EXTN_MODE;
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		case 'v':
2257c478bd9Sstevel@tonic-gate 			if (mode & EXTN_MODE)
2267c478bd9Sstevel@tonic-gate 				errflg++;
2277c478bd9Sstevel@tonic-gate 			mode |= VERBOSE_MODE;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		case '?':
2307c478bd9Sstevel@tonic-gate 		default:
2317c478bd9Sstevel@tonic-gate 			errflg++;
2327c478bd9Sstevel@tonic-gate 			break;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (errflg || optind != argc) {
2367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2377c478bd9Sstevel@tonic-gate 		    gettext("usage: %s [ [-v] [-b | -n | -k] | [-x] ]\n"),
2387c478bd9Sstevel@tonic-gate 		    pgmname);
2397c478bd9Sstevel@tonic-gate 		return (1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * We use dev_cpu_self_cpuid for discovering hardware capabilities;
2447c478bd9Sstevel@tonic-gate 	 * but we only complain if we can't open it if we've been
2457c478bd9Sstevel@tonic-gate 	 * asked to report on those capabilities.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if ((mode & (VERBOSE_MODE|EXTN_MODE)) != 0 &&
2487c478bd9Sstevel@tonic-gate 	    (d = open(dev_cpu_self_cpuid, O_RDONLY)) == -1)
2497c478bd9Sstevel@tonic-gate 		perror(dev_cpu_self_cpuid), exit(1);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (mode & KERN_MODE) {
2527c478bd9Sstevel@tonic-gate 		vfmt = gettext("%d-bit %s kernel modules\n");
2537c478bd9Sstevel@tonic-gate 		(void) report_abi(SI_ARCHITECTURE_K, vfmt);
2547c478bd9Sstevel@tonic-gate 		return (0);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	vfmt = gettext("%d-bit %s applications\n");
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (mode & (BITS_MODE | NATIVE_MODE)) {
2607c478bd9Sstevel@tonic-gate 		if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) == NULL)
2617c478bd9Sstevel@tonic-gate 			isa = report_abi(SI_ARCHITECTURE_32, vfmt);
2627c478bd9Sstevel@tonic-gate 		if (isa != NULL && (mode & VERBOSE_MODE) != 0)
2637c478bd9Sstevel@tonic-gate 			report_hwcap(d, isa);
2647c478bd9Sstevel@tonic-gate 	} else {
2657c478bd9Sstevel@tonic-gate 		if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) != NULL) {
2667c478bd9Sstevel@tonic-gate 			if (mode & (EXTN_MODE|VERBOSE_MODE))
2677c478bd9Sstevel@tonic-gate 				report_hwcap(d, isa);
2687c478bd9Sstevel@tonic-gate 			else
2697c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		if ((isa32 = report_abi(SI_ARCHITECTURE_32, vfmt)) != NULL) {
2737c478bd9Sstevel@tonic-gate 			if (mode & (EXTN_MODE|VERBOSE_MODE))
2747c478bd9Sstevel@tonic-gate 				report_hwcap(d, isa32);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if ((isa32 != NULL || isa != NULL) &&
2787c478bd9Sstevel@tonic-gate 		    (mode & (EXTN_MODE|VERBOSE_MODE)) == 0)
2797c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (0);
2837c478bd9Sstevel@tonic-gate }
284