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 #ifndef AV_SPARC_CBCOND
 118 #define AV_SPARC_CBCOND 0x10000000  /* compare and branch instrs supported */
 119 #endif
 120     if (av & AV_SPARC_CBCOND)       features |= cbcond_instructions_m;
 121 
 122   } else {
 123     // getisax(2) failed, use the old legacy code.
 124 #ifndef PRODUCT
 125     if (PrintMiscellaneous && Verbose)
 126       tty->print_cr("getisax(2) is not supported.");
 127 #endif
 128 
 129     char   tmp;
 130     size_t bufsize = sysinfo(SI_ISALIST, &tmp, 1);
 131     char*  buf     = (char*) malloc(bufsize);
 132 
 133     if (buf != NULL) {
 134       if (sysinfo(SI_ISALIST, buf, bufsize) == bufsize) {
 135         // Figure out what kind of sparc we have
 136         char *sparc_string = strstr(buf, "sparc");
 137         if (sparc_string != NULL) {              features |= v8_instructions_m;
 138           if (sparc_string[5] == 'v') {
 139             if (sparc_string[6] == '8') {
 140               if (sparc_string[7] == '-') {      features |= hardware_mul32_m;
 141                                                  features |= hardware_div32_m;
 142               } else if (sparc_string[7] == 'p') features |= generic_v9_m;
 143               else                               features |= generic_v8_m;
 144             } else if (sparc_string[6] == '9')   features |= generic_v9_m;
 145           }
 146         }
 147 
 148         // Check for visualization instructions
 149         char *vis = strstr(buf, "vis");
 150         if (vis != NULL) {                       features |= vis1_instructions_m;
 151           if (vis[3] == '2')                     features |= vis2_instructions_m;
 152         }
 153       }
 154       free(buf);
 155     }
 156   }
 157 
 158   // Determine the machine type.
 159   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 160 
 161   {
 162     // Using kstat to determine the machine type.
 163     kstat_ctl_t* kc = kstat_open();
 164     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 165     const char* implementation = "UNKNOWN";
 166     if (ksp != NULL) {
 167       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 168         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 169         for (int i = 0; i < ksp->ks_ndata; i++) {
 170           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 171 #ifndef KSTAT_DATA_STRING
 172 #define KSTAT_DATA_STRING   9
 173 #endif
 174             if (knm[i].data_type == KSTAT_DATA_CHAR) {
 175               // VM is running on Solaris 8 which does not have value.str.
 176               implementation = &(knm[i].value.c[0]);
 177             } else if (knm[i].data_type == KSTAT_DATA_STRING) {
 178               // VM is running on Solaris 10.
 179 #ifndef KSTAT_NAMED_STR_PTR
 180               // Solaris 8 was used to build VM, define the structure it misses.
 181               struct str_t {
 182                 union {
 183                   char *ptr;     /* NULL-term string */
 184                   char __pad[8]; /* 64-bit padding */
 185                 } addr;
 186                 uint32_t len;    /* # bytes for strlen + '\0' */
 187               };
 188 #define KSTAT_NAMED_STR_PTR(knptr) (( (str_t*)&((knptr)->value) )->addr.ptr)
 189 #endif
 190               implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 191             }
 192 #ifndef PRODUCT
 193             if (PrintMiscellaneous && Verbose) {
 194               tty->print_cr("cpu_info.implementation: %s", implementation);
 195             }
 196 #endif
 197             // Convert to UPPER case before compare.
 198             char* impl = strdup(implementation);
 199 
 200             for (int i = 0; impl[i] != 0; i++)
 201               impl[i] = (char)toupper((uint)impl[i]);
 202             if (strstr(impl, "SPARC64") != NULL) {
 203               features |= sparc64_family_m;
 204             } else if (strstr(impl, "SPARC-T") != NULL) {
 205               features |= T_family_m;
 206               if (strstr(impl, "SPARC-T1") != NULL) {
 207                 features |= T1_model_m;
 208               }
 209             } else {
 210               assert(strstr(impl, "SPARC") != NULL, "should be sparc");
 211             }
 212             free((void*)impl);
 213             break;
 214           }
 215         } // for(
 216       }
 217     }
 218     assert(strcmp(implementation, "UNKNOWN") != 0,
 219            "unknown cpu info (changed kstat interface?)");
 220     kstat_close(kc);
 221   }
 222 
 223   return features;
 224 }