xref: /illumos-gate/usr/src/cmd/isainfo/isainfo.c (revision 99f63845ee65f89a523460ce4b6b0603a06eceb7)
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*99f63845Sab196087  * Common Development and Distribution License (the "License").
6*99f63845Sab196087  * 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*99f63845Sab196087  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
307c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuid_drv.h>
357c478bd9Sstevel@tonic-gate #include <sys/elf.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <elfcap.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static const char dev_cpu_self_cpuid[] = "/dev/" CPUID_SELF_NAME;
497c478bd9Sstevel@tonic-gate static char *pgmname;
507c478bd9Sstevel@tonic-gate static int mode = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	BITS_MODE	0x1
537c478bd9Sstevel@tonic-gate #define	NATIVE_MODE	0x2
547c478bd9Sstevel@tonic-gate #define	KERN_MODE	0x4
557c478bd9Sstevel@tonic-gate #define	VERBOSE_MODE	0x8
567c478bd9Sstevel@tonic-gate #define	EXTN_MODE	0x10
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static char *
597c478bd9Sstevel@tonic-gate getsysinfo(int cmd)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	char *buf;
627c478bd9Sstevel@tonic-gate 	size_t bufsize = 20;	/* wild guess */
637c478bd9Sstevel@tonic-gate 	long ret;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL)
667c478bd9Sstevel@tonic-gate 		return (NULL);
677c478bd9Sstevel@tonic-gate 	do {
687c478bd9Sstevel@tonic-gate 		ret = sysinfo(cmd, buf, bufsize);
697c478bd9Sstevel@tonic-gate 		if (ret == -1)
707c478bd9Sstevel@tonic-gate 			return (NULL);
717c478bd9Sstevel@tonic-gate 		if (ret > bufsize) {
727c478bd9Sstevel@tonic-gate 			bufsize = ret;
737c478bd9Sstevel@tonic-gate 			buf = realloc(buf, bufsize);
747c478bd9Sstevel@tonic-gate 		} else
757c478bd9Sstevel@tonic-gate 			break;
767c478bd9Sstevel@tonic-gate 	} while (buf != NULL);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (buf);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Classify isa's as to bitness of the corresponding ABIs.
837c478bd9Sstevel@tonic-gate  * isa's which have no "official" Solaris ABI are returned
847c478bd9Sstevel@tonic-gate  * unrecognised i.e. "zero bit".
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate static uint_t
877c478bd9Sstevel@tonic-gate bitness(const char *isaname)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "sparc") == 0 ||
907c478bd9Sstevel@tonic-gate 	    strcmp(isaname, "i386") == 0)
917c478bd9Sstevel@tonic-gate 		return (32);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "sparcv9") == 0 ||
947c478bd9Sstevel@tonic-gate 	    strcmp(isaname, "amd64") == 0)
957c478bd9Sstevel@tonic-gate 		return (64);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (0);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static char *
1017c478bd9Sstevel@tonic-gate report_abi(int cmd, const char *vfmt)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	uint_t bits;
1047c478bd9Sstevel@tonic-gate 	char *isa;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if ((isa = getsysinfo(cmd)) == NULL)
1077c478bd9Sstevel@tonic-gate 		return (0);
1087c478bd9Sstevel@tonic-gate 	if ((bits = bitness(isa)) == 0) {
1097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1107c478bd9Sstevel@tonic-gate 		    gettext("%s: unable to identify isa '%s'!\n"),
1117c478bd9Sstevel@tonic-gate 		    pgmname, isa);
1127c478bd9Sstevel@tonic-gate 		exit(3);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (mode & VERBOSE_MODE)
1167c478bd9Sstevel@tonic-gate 		(void) printf(vfmt, bits, isa);
1177c478bd9Sstevel@tonic-gate 	else if (mode & BITS_MODE)
1187c478bd9Sstevel@tonic-gate 		(void) printf("%d\n", bits);
1197c478bd9Sstevel@tonic-gate 	else if (mode & (NATIVE_MODE|KERN_MODE))
1207c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", isa);
1217c478bd9Sstevel@tonic-gate 	else
1227c478bd9Sstevel@tonic-gate 		(void) printf("%s", isa);
1237c478bd9Sstevel@tonic-gate 	return (isa);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Classify isas as their machine type.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static ushort_t
1307c478bd9Sstevel@tonic-gate machtype(const char *isaname)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "sparc") == 0)
1337c478bd9Sstevel@tonic-gate 		return (EM_SPARC);
1347c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "sparcv9") == 0)
1357c478bd9Sstevel@tonic-gate 		return (EM_SPARCV9);
1367c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "i386") == 0)
1377c478bd9Sstevel@tonic-gate 		return (EM_386);
1387c478bd9Sstevel@tonic-gate 	if (strcmp(isaname, "amd64") == 0)
1397c478bd9Sstevel@tonic-gate 		return (EM_AMD64);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void
1457c478bd9Sstevel@tonic-gate report_hwcap(int d, const char *isa)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	struct cpuid_get_hwcap __cgh, *cgh = &__cgh;
1487c478bd9Sstevel@tonic-gate 	char buffer[1024];
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	cgh->cgh_archname = (char *)isa;
1517c478bd9Sstevel@tonic-gate 	if (ioctl(d, CPUID_GET_HWCAP, cgh) != 0)
1527c478bd9Sstevel@tonic-gate 		return;
1537c478bd9Sstevel@tonic-gate 
154*99f63845Sab196087 	(void) elfcap_hw1_to_str(ELFCAP_STYLE_LC, cgh->cgh_hwcap,
155*99f63845Sab196087 	    buffer, sizeof (buffer), ELFCAP_FMT_SNGSPACE, machtype(isa));
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (mode & EXTN_MODE) {
1587c478bd9Sstevel@tonic-gate 		(void) printf(": %s\n", buffer);
1597c478bd9Sstevel@tonic-gate 	} else {
1607c478bd9Sstevel@tonic-gate 		char *p;
1617c478bd9Sstevel@tonic-gate 		int linecnt = 0;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		for (p = strtok(buffer, " "); p; p = strtok(NULL, " ")) {
1647c478bd9Sstevel@tonic-gate 			if (linecnt == 0)
1657c478bd9Sstevel@tonic-gate 				linecnt = printf("\t");
1667c478bd9Sstevel@tonic-gate 			linecnt += printf("%s ", p);
1677c478bd9Sstevel@tonic-gate 			if (linecnt > 68) {
1687c478bd9Sstevel@tonic-gate 				(void) printf("\n");
1697c478bd9Sstevel@tonic-gate 				linecnt = 0;
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		if (linecnt != 0)
1737c478bd9Sstevel@tonic-gate 			(void) printf("\n");
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1787c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate int
1827c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	int errflg = 0;
1857c478bd9Sstevel@tonic-gate 	int c;
1867c478bd9Sstevel@tonic-gate 	char *vfmt;
1877c478bd9Sstevel@tonic-gate 	char *isa, *isa32;
1887c478bd9Sstevel@tonic-gate 	int d = -1;
1897c478bd9Sstevel@tonic-gate 	const int excl_modes =	/* exclusive mode settings */
1907c478bd9Sstevel@tonic-gate 	    NATIVE_MODE | BITS_MODE | KERN_MODE | EXTN_MODE;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1937c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if ((pgmname = strrchr(*argv, '/')) == 0)
1967c478bd9Sstevel@tonic-gate 		pgmname = argv[0];
1977c478bd9Sstevel@tonic-gate 	else
1987c478bd9Sstevel@tonic-gate 		pgmname++;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "nbkvx")) != EOF)
2017c478bd9Sstevel@tonic-gate 		switch (c) {
2027c478bd9Sstevel@tonic-gate 		case 'n':
2037c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2047c478bd9Sstevel@tonic-gate 				errflg++;
2057c478bd9Sstevel@tonic-gate 			mode |= NATIVE_MODE;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		case 'b':
2087c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2097c478bd9Sstevel@tonic-gate 				errflg++;
2107c478bd9Sstevel@tonic-gate 			mode |= BITS_MODE;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		case 'k':
2137c478bd9Sstevel@tonic-gate 			if (mode & excl_modes)
2147c478bd9Sstevel@tonic-gate 				errflg++;
2157c478bd9Sstevel@tonic-gate 			mode |= KERN_MODE;
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 		case 'x':
2187c478bd9Sstevel@tonic-gate 			if (mode & excl_modes || mode & VERBOSE_MODE)
2197c478bd9Sstevel@tonic-gate 				errflg++;
2207c478bd9Sstevel@tonic-gate 			mode |= EXTN_MODE;
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case 'v':
2237c478bd9Sstevel@tonic-gate 			if (mode & EXTN_MODE)
2247c478bd9Sstevel@tonic-gate 				errflg++;
2257c478bd9Sstevel@tonic-gate 			mode |= VERBOSE_MODE;
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 		case '?':
2287c478bd9Sstevel@tonic-gate 		default:
2297c478bd9Sstevel@tonic-gate 			errflg++;
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (errflg || optind != argc) {
2347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2357c478bd9Sstevel@tonic-gate 		    gettext("usage: %s [ [-v] [-b | -n | -k] | [-x] ]\n"),
2367c478bd9Sstevel@tonic-gate 		    pgmname);
2377c478bd9Sstevel@tonic-gate 		return (1);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * We use dev_cpu_self_cpuid for discovering hardware capabilities;
2427c478bd9Sstevel@tonic-gate 	 * but we only complain if we can't open it if we've been
2437c478bd9Sstevel@tonic-gate 	 * asked to report on those capabilities.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	if ((mode & (VERBOSE_MODE|EXTN_MODE)) != 0 &&
2467c478bd9Sstevel@tonic-gate 	    (d = open(dev_cpu_self_cpuid, O_RDONLY)) == -1)
2477c478bd9Sstevel@tonic-gate 		perror(dev_cpu_self_cpuid), exit(1);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (mode & KERN_MODE) {
2507c478bd9Sstevel@tonic-gate 		vfmt = gettext("%d-bit %s kernel modules\n");
2517c478bd9Sstevel@tonic-gate 		(void) report_abi(SI_ARCHITECTURE_K, vfmt);
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	vfmt = gettext("%d-bit %s applications\n");
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (mode & (BITS_MODE | NATIVE_MODE)) {
2587c478bd9Sstevel@tonic-gate 		if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) == NULL)
2597c478bd9Sstevel@tonic-gate 			isa = report_abi(SI_ARCHITECTURE_32, vfmt);
2607c478bd9Sstevel@tonic-gate 		if (isa != NULL && (mode & VERBOSE_MODE) != 0)
2617c478bd9Sstevel@tonic-gate 			report_hwcap(d, isa);
2627c478bd9Sstevel@tonic-gate 	} else {
2637c478bd9Sstevel@tonic-gate 		if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) != NULL) {
2647c478bd9Sstevel@tonic-gate 			if (mode & (EXTN_MODE|VERBOSE_MODE))
2657c478bd9Sstevel@tonic-gate 				report_hwcap(d, isa);
2667c478bd9Sstevel@tonic-gate 			else
2677c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if ((isa32 = report_abi(SI_ARCHITECTURE_32, vfmt)) != NULL) {
2717c478bd9Sstevel@tonic-gate 			if (mode & (EXTN_MODE|VERBOSE_MODE))
2727c478bd9Sstevel@tonic-gate 				report_hwcap(d, isa32);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		if ((isa32 != NULL || isa != NULL) &&
2767c478bd9Sstevel@tonic-gate 		    (mode & (EXTN_MODE|VERBOSE_MODE)) == 0)
2777c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (0);
2817c478bd9Sstevel@tonic-gate }
282