1 /*
   2  * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "runtime/os.hpp"
  27 #include "vm_version_sparc.hpp"
  28 
  29 # include <sys/auxv.h>
  30 # include <sys/auxv_SPARC.h>
  31 # include <sys/systeminfo.h>
  32 # include <kstat.h>
  33 
  34 // We need to keep these here as long as we have to build on Solaris
  35 // versions before 10.
  36 #ifndef SI_ARCHITECTURE_32
  37 #define SI_ARCHITECTURE_32      516     /* basic 32-bit SI_ARCHITECTURE */
  38 #endif
  39 
  40 #ifndef SI_ARCHITECTURE_64
  41 #define SI_ARCHITECTURE_64      517     /* basic 64-bit SI_ARCHITECTURE */
  42 #endif
  43 
  44 static void do_sysinfo(int si, const char* string, int* features, int mask) {
  45   char   tmp;
  46   size_t bufsize = sysinfo(si, &tmp, 1);
  47 
  48   // All SI defines used below must be supported.
  49   guarantee(bufsize != -1, "must be supported");
  50 
  51   char* buf = (char*) malloc(bufsize);
  52 
  53   if (buf == NULL)
  54     return;
  55 
  56   if (sysinfo(si, buf, bufsize) == bufsize) {
  57     // Compare the string.
  58     if (strcmp(buf, string) == 0) {
  59       *features |= mask;
  60     }
  61   }
  62 
  63   free(buf);
  64 }
  65 
  66 int VM_Version::platform_features(int features) {
  67   // getisax(2), SI_ARCHITECTURE_32, and SI_ARCHITECTURE_64 are
  68   // supported on Solaris 10 and later.
  69   if (os::Solaris::supports_getisax()) {
  70 
  71     // Check 32-bit architecture.
  72     do_sysinfo(SI_ARCHITECTURE_32, "sparc", &features, v8_instructions_m);
  73 
  74     // Check 64-bit architecture.
  75     do_sysinfo(SI_ARCHITECTURE_64, "sparcv9", &features, generic_v9_m);
  76 
  77     // Extract valid instruction set extensions.
  78     uint_t avs[2];
  79     uint_t avn = os::Solaris::getisax(avs, 2);
  80     assert(avn <= 2, "should return two or less av's");
  81     uint_t av = avs[0];
  82 
  83 #ifndef PRODUCT
  84     if (PrintMiscellaneous && Verbose) {
  85       tty->print("getisax(2) returned: " PTR32_FORMAT, av);
  86       if (avn > 1) {
  87         tty->print(", " PTR32_FORMAT, avs[1]);
  88       }
  89       tty->cr();
  90     }
  91 #endif
  92 
  93     if (av & AV_SPARC_MUL32)  features |= hardware_mul32_m;
  94     if (av & AV_SPARC_DIV32)  features |= hardware_div32_m;
  95     if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m;
  96     if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m;
  97     if (av & AV_SPARC_POPC)   features |= hardware_popc_m;
  98     if (av & AV_SPARC_VIS)    features |= vis1_instructions_m;
  99     if (av & AV_SPARC_VIS2)   features |= vis2_instructions_m;
 100     if (avn > 1) {
 101       uint_t av2 = avs[1];
 102 #ifndef AV2_SPARC_SPARC5
 103 #define AV2_SPARC_SPARC5 0x00000008 /* The 29 new fp and sub instructions */
 104 #endif
 105       if (av2 & AV2_SPARC_SPARC5)       features |= sparc5_instructions_m;
 106     }
 107 
 108     // Next values are not defined before Solaris 10
 109     // but Solaris 8 is used for jdk6 update builds.
 110 #ifndef AV_SPARC_ASI_BLK_INIT
 111 #define AV_SPARC_ASI_BLK_INIT 0x0080  /* ASI_BLK_INIT_xxx ASI */
 112 #endif
 113     if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m;
 114 
 115 #ifndef AV_SPARC_FMAF
 116 #define AV_SPARC_FMAF 0x0100        /* Fused Multiply-Add */
 117 #endif
 118     if (av & AV_SPARC_FMAF)         features |= fmaf_instructions_m;
 119 
 120 #ifndef AV_SPARC_FMAU
 121 #define    AV_SPARC_FMAU    0x0200  /* Unfused Multiply-Add */
 122 #endif
 123     if (av & AV_SPARC_FMAU)         features |= fmau_instructions_m;
 124 
 125 #ifndef AV_SPARC_VIS3
 126 #define    AV_SPARC_VIS3    0x0400  /* VIS3 instruction set extensions */
 127 #endif
 128     if (av & AV_SPARC_VIS3)         features |= vis3_instructions_m;
 129 
 130 #ifndef AV_SPARC_CBCOND
 131 #define AV_SPARC_CBCOND 0x10000000  /* compare and branch instrs supported */
 132 #endif
 133     if (av & AV_SPARC_CBCOND)       features |= cbcond_instructions_m;
 134 
 135 #ifndef AV_SPARC_AES
 136 #define AV_SPARC_AES 0x00020000  /* aes instrs supported */
 137 #endif
 138     if (av & AV_SPARC_AES)       features |= aes_instructions_m;
 139 
 140   } else {
 141     // getisax(2) failed, use the old legacy code.
 142 #ifndef PRODUCT
 143     if (PrintMiscellaneous && Verbose)
 144       tty->print_cr("getisax(2) is not supported.");
 145 #endif
 146 
 147     char   tmp;
 148     size_t bufsize = sysinfo(SI_ISALIST, &tmp, 1);
 149     char*  buf     = (char*) malloc(bufsize);
 150 
 151     if (buf != NULL) {
 152       if (sysinfo(SI_ISALIST, buf, bufsize) == bufsize) {
 153         // Figure out what kind of sparc we have
 154         char *sparc_string = strstr(buf, "sparc");
 155         if (sparc_string != NULL) {              features |= v8_instructions_m;
 156           if (sparc_string[5] == 'v') {
 157             if (sparc_string[6] == '8') {
 158               if (sparc_string[7] == '-') {      features |= hardware_mul32_m;
 159                                                  features |= hardware_div32_m;
 160               } else if (sparc_string[7] == 'p') features |= generic_v9_m;
 161               else                               features |= generic_v8_m;
 162             } else if (sparc_string[6] == '9')   features |= generic_v9_m;
 163           }
 164         }
 165 
 166         // Check for visualization instructions
 167         char *vis = strstr(buf, "vis");
 168         if (vis != NULL) {                       features |= vis1_instructions_m;
 169           if (vis[3] == '2')                     features |= vis2_instructions_m;
 170         }
 171       }
 172       free(buf);
 173     }
 174   }
 175 
 176   // Determine the machine type.
 177   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 178 
 179   {
 180     // Using kstat to determine the machine type.
 181     kstat_ctl_t* kc = kstat_open();
 182     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 183     const char* implementation = "UNKNOWN";
 184     if (ksp != NULL) {
 185       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 186         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 187         for (int i = 0; i < ksp->ks_ndata; i++) {
 188           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 189 #ifndef KSTAT_DATA_STRING
 190 #define KSTAT_DATA_STRING   9
 191 #endif
 192             if (knm[i].data_type == KSTAT_DATA_CHAR) {
 193               // VM is running on Solaris 8 which does not have value.str.
 194               implementation = &(knm[i].value.c[0]);
 195             } else if (knm[i].data_type == KSTAT_DATA_STRING) {
 196               // VM is running on Solaris 10.
 197 #ifndef KSTAT_NAMED_STR_PTR
 198               // Solaris 8 was used to build VM, define the structure it misses.
 199               struct str_t {
 200                 union {
 201                   char *ptr;     /* NULL-term string */
 202                   char __pad[8]; /* 64-bit padding */
 203                 } addr;
 204                 uint32_t len;    /* # bytes for strlen + '\0' */
 205               };
 206 #define KSTAT_NAMED_STR_PTR(knptr) (( (str_t*)&((knptr)->value) )->addr.ptr)
 207 #endif
 208               implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 209             }
 210 #ifndef PRODUCT
 211             if (PrintMiscellaneous && Verbose) {
 212               tty->print_cr("cpu_info.implementation: %s", implementation);
 213             }
 214 #endif
 215             // Convert to UPPER case before compare.
 216             char* impl = strdup(implementation);
 217 
 218             for (int i = 0; impl[i] != 0; i++)
 219               impl[i] = (char)toupper((uint)impl[i]);
 220             if (strstr(impl, "SPARC64") != NULL) {
 221               features |= sparc64_family_m;
 222             } else if (strstr(impl, "SPARC-M") != NULL) {
 223               // M-series SPARC is based on T-series.
 224               features |= (M_family_m | T_family_m);
 225             } else if (strstr(impl, "SPARC-T") != NULL) {
 226               features |= T_family_m;
 227               if (strstr(impl, "SPARC-T1") != NULL) {
 228                 features |= T1_model_m;
 229               }
 230             } else {
 231               if (strstr(impl, "SPARC") == NULL) {
 232 #ifndef PRODUCT
 233                 // kstat on Solaris 8 virtual machines (branded zones)
 234                 // returns "(unsupported)" implementation.
 235                 warning("kstat cpu_info implementation = '%s', should contain SPARC", impl);
 236 #endif
 237                 implementation = "SPARC";
 238               }
 239             }
 240             free((void*)impl);
 241             break;
 242           }
 243         } // for(
 244       }
 245     }
 246     assert(strcmp(implementation, "UNKNOWN") != 0,
 247            "unknown cpu info (changed kstat interface?)");
 248     kstat_close(kc);
 249   }
 250 
 251   return features;
 252 }