1 /*
   2  * Copyright (c) 2006, 2011, 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 av;
  79     uint_t avn = os::Solaris::getisax(&av, 1);
  80     assert(avn == 1, "should only return one av");
  81 
  82 #ifndef PRODUCT
  83     if (PrintMiscellaneous && Verbose)
  84       tty->print_cr("getisax(2) returned: " PTR32_FORMAT, av);
  85 #endif
  86 
  87     if (av & AV_SPARC_MUL32)  features |= hardware_mul32_m;
  88     if (av & AV_SPARC_DIV32)  features |= hardware_div32_m;
  89     if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m;
  90     if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m;
  91     if (av & AV_SPARC_POPC)   features |= hardware_popc_m;
  92     if (av & AV_SPARC_VIS)    features |= vis1_instructions_m;
  93     if (av & AV_SPARC_VIS2)   features |= vis2_instructions_m;
  94 
  95     // Next values are not defined before Solaris 10
  96     // but Solaris 8 is used for jdk6 update builds.
  97 #ifndef AV_SPARC_ASI_BLK_INIT
  98 #define AV_SPARC_ASI_BLK_INIT 0x0080  /* ASI_BLK_INIT_xxx ASI */
  99 #endif
 100     if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m;
 101 
 102 #ifndef AV_SPARC_FMAF
 103 #define AV_SPARC_FMAF 0x0100        /* Fused Multiply-Add */
 104 #endif
 105     if (av & AV_SPARC_FMAF)         features |= fmaf_instructions_m;
 106 
 107 #ifndef AV_SPARC_FMAU
 108 #define    AV_SPARC_FMAU    0x0200  /* Unfused Multiply-Add */
 109 #endif
 110     if (av & AV_SPARC_FMAU)         features |= fmau_instructions_m;
 111 
 112 #ifndef AV_SPARC_VIS3
 113 #define    AV_SPARC_VIS3    0x0400  /* VIS3 instruction set extensions */
 114 #endif
 115     if (av & AV_SPARC_VIS3)         features |= vis3_instructions_m;
 116 
 117   } else {
 118     // getisax(2) failed, use the old legacy code.
 119 #ifndef PRODUCT
 120     if (PrintMiscellaneous && Verbose)
 121       tty->print_cr("getisax(2) is not supported.");
 122 #endif
 123 
 124     char   tmp;
 125     size_t bufsize = sysinfo(SI_ISALIST, &tmp, 1);
 126     char*  buf     = (char*) malloc(bufsize);
 127 
 128     if (buf != NULL) {
 129       if (sysinfo(SI_ISALIST, buf, bufsize) == bufsize) {
 130         // Figure out what kind of sparc we have
 131         char *sparc_string = strstr(buf, "sparc");
 132         if (sparc_string != NULL) {              features |= v8_instructions_m;
 133           if (sparc_string[5] == 'v') {
 134             if (sparc_string[6] == '8') {
 135               if (sparc_string[7] == '-') {      features |= hardware_mul32_m;
 136                                                  features |= hardware_div32_m;
 137               } else if (sparc_string[7] == 'p') features |= generic_v9_m;
 138               else                               features |= generic_v8_m;
 139             } else if (sparc_string[6] == '9')   features |= generic_v9_m;
 140           }
 141         }
 142 
 143         // Check for visualization instructions
 144         char *vis = strstr(buf, "vis");
 145         if (vis != NULL) {                       features |= vis1_instructions_m;
 146           if (vis[3] == '2')                     features |= vis2_instructions_m;
 147         }
 148       }
 149       free(buf);
 150     }
 151   }
 152 
 153   // Determine the machine type.
 154   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 155 
 156   {
 157     // Using kstat to determine the machine type.
 158     kstat_ctl_t* kc = kstat_open();
 159     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 160     const char* implementation = "UNKNOWN";
 161     if (ksp != NULL) {
 162       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 163         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 164         for (int i = 0; i < ksp->ks_ndata; i++) {
 165           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 166 #ifndef KSTAT_DATA_STRING
 167 #define KSTAT_DATA_STRING   9
 168 #endif
 169             if (knm[i].data_type == KSTAT_DATA_CHAR) {
 170               // VM is running on Solaris 8 which does not have value.str.
 171               implementation = &(knm[i].value.c[0]);
 172             } else if (knm[i].data_type == KSTAT_DATA_STRING) {
 173               // VM is running on Solaris 10.
 174 #ifndef KSTAT_NAMED_STR_PTR
 175               // Solaris 8 was used to build VM, define the structure it misses.
 176               struct str_t {
 177                 union {
 178                   char *ptr;     /* NULL-term string */
 179                   char __pad[8]; /* 64-bit padding */
 180                 } addr;
 181                 uint32_t len;    /* # bytes for strlen + '\0' */
 182               };
 183 #define KSTAT_NAMED_STR_PTR(knptr) (( (str_t*)&((knptr)->value) )->addr.ptr)
 184 #endif
 185               implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 186             }
 187 #ifndef PRODUCT
 188             if (PrintMiscellaneous && Verbose) {
 189               tty->print_cr("cpu_info.implementation: %s", implementation);
 190             }
 191 #endif
 192             // Convert to UPPER case before compare.
 193             char* impl = strdup(implementation);
 194 
 195             for (int i = 0; impl[i] != 0; i++)
 196               impl[i] = (char)toupper((uint)impl[i]);
 197             if (strstr(impl, "SPARC64") != NULL) {
 198               features |= sparc64_family_m;
 199             } else if (strstr(impl, "SPARC-T") != NULL) {
 200               features |= T_family_m;
 201               if (strstr(impl, "SPARC-T1") != NULL) {
 202                 features |= T1_model_m;
 203               }
 204             } else {
 205               assert(strstr(impl, "SPARC") != NULL, "should be sparc");
 206             }
 207             free((void*)impl);
 208             break;
 209           }
 210         } // for(
 211       }
 212     }
 213     assert(strcmp(implementation, "UNKNOWN") != 0,
 214            "unknown cpu info (changed kstat interface?)");
 215     kstat_close(kc);
 216   }
 217 
 218   return features;
 219 }