1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <sys/auxv.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/cpuid_drv.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/elf.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*7c478bd9Sstevel@tonic-gate #include <strings.h> 41*7c478bd9Sstevel@tonic-gate #include <unistd.h> 42*7c478bd9Sstevel@tonic-gate #include <errno.h> 43*7c478bd9Sstevel@tonic-gate #include <libintl.h> 44*7c478bd9Sstevel@tonic-gate #include <locale.h> 45*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include <elfcap.h> 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate static const char dev_cpu_self_cpuid[] = "/dev/" CPUID_SELF_NAME; 50*7c478bd9Sstevel@tonic-gate static char *pgmname; 51*7c478bd9Sstevel@tonic-gate static int mode = 0; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate #define BITS_MODE 0x1 54*7c478bd9Sstevel@tonic-gate #define NATIVE_MODE 0x2 55*7c478bd9Sstevel@tonic-gate #define KERN_MODE 0x4 56*7c478bd9Sstevel@tonic-gate #define VERBOSE_MODE 0x8 57*7c478bd9Sstevel@tonic-gate #define EXTN_MODE 0x10 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static char * 60*7c478bd9Sstevel@tonic-gate getsysinfo(int cmd) 61*7c478bd9Sstevel@tonic-gate { 62*7c478bd9Sstevel@tonic-gate char *buf; 63*7c478bd9Sstevel@tonic-gate size_t bufsize = 20; /* wild guess */ 64*7c478bd9Sstevel@tonic-gate long ret; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) 67*7c478bd9Sstevel@tonic-gate return (NULL); 68*7c478bd9Sstevel@tonic-gate do { 69*7c478bd9Sstevel@tonic-gate ret = sysinfo(cmd, buf, bufsize); 70*7c478bd9Sstevel@tonic-gate if (ret == -1) 71*7c478bd9Sstevel@tonic-gate return (NULL); 72*7c478bd9Sstevel@tonic-gate if (ret > bufsize) { 73*7c478bd9Sstevel@tonic-gate bufsize = ret; 74*7c478bd9Sstevel@tonic-gate buf = realloc(buf, bufsize); 75*7c478bd9Sstevel@tonic-gate } else 76*7c478bd9Sstevel@tonic-gate break; 77*7c478bd9Sstevel@tonic-gate } while (buf != NULL); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate return (buf); 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* 83*7c478bd9Sstevel@tonic-gate * Classify isa's as to bitness of the corresponding ABIs. 84*7c478bd9Sstevel@tonic-gate * isa's which have no "official" Solaris ABI are returned 85*7c478bd9Sstevel@tonic-gate * unrecognised i.e. "zero bit". 86*7c478bd9Sstevel@tonic-gate */ 87*7c478bd9Sstevel@tonic-gate static uint_t 88*7c478bd9Sstevel@tonic-gate bitness(const char *isaname) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0 || 91*7c478bd9Sstevel@tonic-gate strcmp(isaname, "i386") == 0) 92*7c478bd9Sstevel@tonic-gate return (32); 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0 || 95*7c478bd9Sstevel@tonic-gate strcmp(isaname, "amd64") == 0) 96*7c478bd9Sstevel@tonic-gate return (64); 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate return (0); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate static char * 102*7c478bd9Sstevel@tonic-gate report_abi(int cmd, const char *vfmt) 103*7c478bd9Sstevel@tonic-gate { 104*7c478bd9Sstevel@tonic-gate uint_t bits; 105*7c478bd9Sstevel@tonic-gate char *isa; 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate if ((isa = getsysinfo(cmd)) == NULL) 108*7c478bd9Sstevel@tonic-gate return (0); 109*7c478bd9Sstevel@tonic-gate if ((bits = bitness(isa)) == 0) { 110*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 111*7c478bd9Sstevel@tonic-gate gettext("%s: unable to identify isa '%s'!\n"), 112*7c478bd9Sstevel@tonic-gate pgmname, isa); 113*7c478bd9Sstevel@tonic-gate exit(3); 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (mode & VERBOSE_MODE) 117*7c478bd9Sstevel@tonic-gate (void) printf(vfmt, bits, isa); 118*7c478bd9Sstevel@tonic-gate else if (mode & BITS_MODE) 119*7c478bd9Sstevel@tonic-gate (void) printf("%d\n", bits); 120*7c478bd9Sstevel@tonic-gate else if (mode & (NATIVE_MODE|KERN_MODE)) 121*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", isa); 122*7c478bd9Sstevel@tonic-gate else 123*7c478bd9Sstevel@tonic-gate (void) printf("%s", isa); 124*7c478bd9Sstevel@tonic-gate return (isa); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate /* 128*7c478bd9Sstevel@tonic-gate * Classify isas as their machine type. 129*7c478bd9Sstevel@tonic-gate */ 130*7c478bd9Sstevel@tonic-gate static ushort_t 131*7c478bd9Sstevel@tonic-gate machtype(const char *isaname) 132*7c478bd9Sstevel@tonic-gate { 133*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparc") == 0) 134*7c478bd9Sstevel@tonic-gate return (EM_SPARC); 135*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "sparcv9") == 0) 136*7c478bd9Sstevel@tonic-gate return (EM_SPARCV9); 137*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "i386") == 0) 138*7c478bd9Sstevel@tonic-gate return (EM_386); 139*7c478bd9Sstevel@tonic-gate if (strcmp(isaname, "amd64") == 0) 140*7c478bd9Sstevel@tonic-gate return (EM_AMD64); 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate return (0); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate static void 146*7c478bd9Sstevel@tonic-gate report_hwcap(int d, const char *isa) 147*7c478bd9Sstevel@tonic-gate { 148*7c478bd9Sstevel@tonic-gate struct cpuid_get_hwcap __cgh, *cgh = &__cgh; 149*7c478bd9Sstevel@tonic-gate char buffer[1024]; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate cgh->cgh_archname = (char *)isa; 152*7c478bd9Sstevel@tonic-gate if (ioctl(d, CPUID_GET_HWCAP, cgh) != 0) 153*7c478bd9Sstevel@tonic-gate return; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate (void) hwcap_1_val2str(cgh->cgh_hwcap, buffer, sizeof (buffer), 156*7c478bd9Sstevel@tonic-gate CAP_FMT_SNGSPACE, machtype(isa)); 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE) { 159*7c478bd9Sstevel@tonic-gate (void) printf(": %s\n", buffer); 160*7c478bd9Sstevel@tonic-gate } else { 161*7c478bd9Sstevel@tonic-gate char *p; 162*7c478bd9Sstevel@tonic-gate int linecnt = 0; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate for (p = strtok(buffer, " "); p; p = strtok(NULL, " ")) { 165*7c478bd9Sstevel@tonic-gate if (linecnt == 0) 166*7c478bd9Sstevel@tonic-gate linecnt = printf("\t"); 167*7c478bd9Sstevel@tonic-gate linecnt += printf("%s ", p); 168*7c478bd9Sstevel@tonic-gate if (linecnt > 68) { 169*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 170*7c478bd9Sstevel@tonic-gate linecnt = 0; 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate if (linecnt != 0) 174*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 175*7c478bd9Sstevel@tonic-gate } 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 179*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 180*7c478bd9Sstevel@tonic-gate #endif 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate int 183*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 184*7c478bd9Sstevel@tonic-gate { 185*7c478bd9Sstevel@tonic-gate int errflg = 0; 186*7c478bd9Sstevel@tonic-gate int c; 187*7c478bd9Sstevel@tonic-gate char *vfmt; 188*7c478bd9Sstevel@tonic-gate char *isa, *isa32; 189*7c478bd9Sstevel@tonic-gate int d = -1; 190*7c478bd9Sstevel@tonic-gate const int excl_modes = /* exclusive mode settings */ 191*7c478bd9Sstevel@tonic-gate NATIVE_MODE | BITS_MODE | KERN_MODE | EXTN_MODE; 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 194*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate if ((pgmname = strrchr(*argv, '/')) == 0) 197*7c478bd9Sstevel@tonic-gate pgmname = argv[0]; 198*7c478bd9Sstevel@tonic-gate else 199*7c478bd9Sstevel@tonic-gate pgmname++; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "nbkvx")) != EOF) 202*7c478bd9Sstevel@tonic-gate switch (c) { 203*7c478bd9Sstevel@tonic-gate case 'n': 204*7c478bd9Sstevel@tonic-gate if (mode & excl_modes) 205*7c478bd9Sstevel@tonic-gate errflg++; 206*7c478bd9Sstevel@tonic-gate mode |= NATIVE_MODE; 207*7c478bd9Sstevel@tonic-gate break; 208*7c478bd9Sstevel@tonic-gate case 'b': 209*7c478bd9Sstevel@tonic-gate if (mode & excl_modes) 210*7c478bd9Sstevel@tonic-gate errflg++; 211*7c478bd9Sstevel@tonic-gate mode |= BITS_MODE; 212*7c478bd9Sstevel@tonic-gate break; 213*7c478bd9Sstevel@tonic-gate case 'k': 214*7c478bd9Sstevel@tonic-gate if (mode & excl_modes) 215*7c478bd9Sstevel@tonic-gate errflg++; 216*7c478bd9Sstevel@tonic-gate mode |= KERN_MODE; 217*7c478bd9Sstevel@tonic-gate break; 218*7c478bd9Sstevel@tonic-gate case 'x': 219*7c478bd9Sstevel@tonic-gate if (mode & excl_modes || mode & VERBOSE_MODE) 220*7c478bd9Sstevel@tonic-gate errflg++; 221*7c478bd9Sstevel@tonic-gate mode |= EXTN_MODE; 222*7c478bd9Sstevel@tonic-gate break; 223*7c478bd9Sstevel@tonic-gate case 'v': 224*7c478bd9Sstevel@tonic-gate if (mode & EXTN_MODE) 225*7c478bd9Sstevel@tonic-gate errflg++; 226*7c478bd9Sstevel@tonic-gate mode |= VERBOSE_MODE; 227*7c478bd9Sstevel@tonic-gate break; 228*7c478bd9Sstevel@tonic-gate case '?': 229*7c478bd9Sstevel@tonic-gate default: 230*7c478bd9Sstevel@tonic-gate errflg++; 231*7c478bd9Sstevel@tonic-gate break; 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate if (errflg || optind != argc) { 235*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 236*7c478bd9Sstevel@tonic-gate gettext("usage: %s [ [-v] [-b | -n | -k] | [-x] ]\n"), 237*7c478bd9Sstevel@tonic-gate pgmname); 238*7c478bd9Sstevel@tonic-gate return (1); 239*7c478bd9Sstevel@tonic-gate } 240*7c478bd9Sstevel@tonic-gate 241*7c478bd9Sstevel@tonic-gate /* 242*7c478bd9Sstevel@tonic-gate * We use dev_cpu_self_cpuid for discovering hardware capabilities; 243*7c478bd9Sstevel@tonic-gate * but we only complain if we can't open it if we've been 244*7c478bd9Sstevel@tonic-gate * asked to report on those capabilities. 245*7c478bd9Sstevel@tonic-gate */ 246*7c478bd9Sstevel@tonic-gate if ((mode & (VERBOSE_MODE|EXTN_MODE)) != 0 && 247*7c478bd9Sstevel@tonic-gate (d = open(dev_cpu_self_cpuid, O_RDONLY)) == -1) 248*7c478bd9Sstevel@tonic-gate perror(dev_cpu_self_cpuid), exit(1); 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate if (mode & KERN_MODE) { 251*7c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s kernel modules\n"); 252*7c478bd9Sstevel@tonic-gate (void) report_abi(SI_ARCHITECTURE_K, vfmt); 253*7c478bd9Sstevel@tonic-gate return (0); 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate vfmt = gettext("%d-bit %s applications\n"); 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate if (mode & (BITS_MODE | NATIVE_MODE)) { 259*7c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) == NULL) 260*7c478bd9Sstevel@tonic-gate isa = report_abi(SI_ARCHITECTURE_32, vfmt); 261*7c478bd9Sstevel@tonic-gate if (isa != NULL && (mode & VERBOSE_MODE) != 0) 262*7c478bd9Sstevel@tonic-gate report_hwcap(d, isa); 263*7c478bd9Sstevel@tonic-gate } else { 264*7c478bd9Sstevel@tonic-gate if ((isa = report_abi(SI_ARCHITECTURE_64, vfmt)) != NULL) { 265*7c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE)) 266*7c478bd9Sstevel@tonic-gate report_hwcap(d, isa); 267*7c478bd9Sstevel@tonic-gate else 268*7c478bd9Sstevel@tonic-gate (void) putchar(' '); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate 271*7c478bd9Sstevel@tonic-gate if ((isa32 = report_abi(SI_ARCHITECTURE_32, vfmt)) != NULL) { 272*7c478bd9Sstevel@tonic-gate if (mode & (EXTN_MODE|VERBOSE_MODE)) 273*7c478bd9Sstevel@tonic-gate report_hwcap(d, isa32); 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate if ((isa32 != NULL || isa != NULL) && 277*7c478bd9Sstevel@tonic-gate (mode & (EXTN_MODE|VERBOSE_MODE)) == 0) 278*7c478bd9Sstevel@tonic-gate (void) putchar('\n'); 279*7c478bd9Sstevel@tonic-gate } 280*7c478bd9Sstevel@tonic-gate 281*7c478bd9Sstevel@tonic-gate return (0); 282*7c478bd9Sstevel@tonic-gate } 283