1 /*
   2  * Copyright (c) 2006, 2014, 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 "memory/allocation.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "runtime/os.hpp"
  29 #include "vm_version_sparc.hpp"
  30 
  31 #include <sys/auxv.h>
  32 #include <sys/auxv_SPARC.h>
  33 #include <sys/systeminfo.h>
  34 #include <kstat.h>
  35 #include <picl.h>
  36 
  37 extern "C" static int PICL_get_l1_data_cache_line_size_helper(picl_nodehdl_t nodeh, void *result);
  38 extern "C" static int PICL_get_l2_cache_line_size_helper(picl_nodehdl_t nodeh, void *result);
  39 
  40 class PICL {
  41   // Get a value of the integer property. The value in the tree can be either 32 or 64 bit
  42   // depending on the platform. The result is converted to int.
  43   static int get_int_property(picl_nodehdl_t nodeh, const char* name, int* result) {
  44     picl_propinfo_t pinfo;
  45     picl_prophdl_t proph;
  46     if (picl_get_prop_by_name(nodeh, name, &proph) != PICL_SUCCESS ||
  47         picl_get_propinfo(proph, &pinfo) != PICL_SUCCESS) {
  48       return PICL_FAILURE;
  49     }
  50 
  51     if (pinfo.type != PICL_PTYPE_INT && pinfo.type != PICL_PTYPE_UNSIGNED_INT) {
  52       assert(false, "Invalid property type");
  53       return PICL_FAILURE;
  54     }
  55     if (pinfo.size == sizeof(int64_t)) {
  56       int64_t val;
  57       if (picl_get_propval(proph, &val, sizeof(int64_t)) != PICL_SUCCESS) {
  58         return PICL_FAILURE;
  59       }
  60       *result = static_cast<int>(val);
  61     } else if (pinfo.size == sizeof(int32_t)) {
  62       int32_t val;
  63       if (picl_get_propval(proph, &val, sizeof(int32_t)) != PICL_SUCCESS) {
  64         return PICL_FAILURE;
  65       }
  66       *result = static_cast<int>(val);
  67     } else {
  68       assert(false, "Unexpected integer property size");
  69       return PICL_FAILURE;
  70     }
  71     return PICL_SUCCESS;
  72   }
  73 
  74   // Visitor and a state machine that visits integer properties and verifies that the
  75   // values are the same. Stores the unique value observed.
  76   class UniqueValueVisitor {
  77     enum {
  78       INITIAL,        // Start state, no assignments happened
  79       ASSIGNED,       // Assigned a value
  80       INCONSISTENT    // Inconsistent value seen
  81     } _state;
  82     int _value;
  83   public:
  84     UniqueValueVisitor() : _state(INITIAL) { }
  85     int value() {
  86       assert(_state == ASSIGNED, "Precondition");
  87       return _value;
  88     }
  89     void set_value(int value) {
  90       assert(_state == INITIAL, "Precondition");
  91       _value = value;
  92       _state = ASSIGNED;
  93     }
  94     bool is_initial()       { return _state == INITIAL;      }
  95     bool is_assigned()      { return _state == ASSIGNED;     }
  96     bool is_inconsistent()  { return _state == INCONSISTENT; }
  97     void set_inconsistent() { _state = INCONSISTENT;         }
  98 
  99     static int visit(picl_nodehdl_t nodeh, const char* name, void *arg) {
 100       UniqueValueVisitor *state = static_cast<UniqueValueVisitor*>(arg);
 101       assert(!state->is_inconsistent(), "Precondition");
 102       int curr;
 103       if (PICL::get_int_property(nodeh, name, &curr) == PICL_SUCCESS) {
 104         if (!state->is_assigned()) { // first iteration
 105           state->set_value(curr);
 106         } else if (curr != state->value()) { // following iterations
 107           state->set_inconsistent();
 108         }
 109       }
 110       if (state->is_inconsistent()) {
 111         return PICL_WALK_TERMINATE;
 112       }
 113       return PICL_WALK_CONTINUE;
 114     }
 115   };
 116 
 117   int _L1_data_cache_line_size;
 118   int _L2_cache_line_size;
 119 public:
 120   static int get_l1_data_cache_line_size(picl_nodehdl_t nodeh, void *state) {
 121     return UniqueValueVisitor::visit(nodeh, "l1-dcache-line-size", state);
 122   }
 123   static int get_l2_cache_line_size(picl_nodehdl_t nodeh, void *state) {
 124     return UniqueValueVisitor::visit(nodeh, "l2-cache-line-size", state);
 125   }
 126 
 127   PICL() : _L1_data_cache_line_size(0), _L2_cache_line_size(0) {
 128     if (picl_initialize() == PICL_SUCCESS) {
 129       picl_nodehdl_t rooth;
 130       if (picl_get_root(&rooth) == PICL_SUCCESS) {
 131         UniqueValueVisitor L1_state;
 132         // Visit all "cpu" class instances
 133         picl_walk_tree_by_class(rooth, "cpu", &L1_state, PICL_get_l1_data_cache_line_size_helper);
 134         if (L1_state.is_initial()) { // Still initial, iteration found no values
 135           // Try walk all "core" class instances, it might be a Fujitsu machine
 136           picl_walk_tree_by_class(rooth, "core", &L1_state, PICL_get_l1_data_cache_line_size_helper);
 137         }
 138         if (L1_state.is_assigned()) { // Is there a value?
 139           _L1_data_cache_line_size = L1_state.value();
 140         }
 141 
 142         UniqueValueVisitor L2_state;
 143         picl_walk_tree_by_class(rooth, "cpu", &L2_state, PICL_get_l2_cache_line_size_helper);
 144         if (L2_state.is_initial()) {
 145           picl_walk_tree_by_class(rooth, "core", &L2_state, PICL_get_l2_cache_line_size_helper);
 146         }
 147         if (L2_state.is_assigned()) {
 148           _L2_cache_line_size = L2_state.value();
 149         }
 150       }
 151       picl_shutdown();
 152     }
 153   }
 154 
 155   unsigned int L1_data_cache_line_size() const { return _L1_data_cache_line_size; }
 156   unsigned int L2_cache_line_size() const      { return _L2_cache_line_size;      }
 157 };
 158 
 159 extern "C" static int PICL_get_l1_data_cache_line_size_helper(picl_nodehdl_t nodeh, void *result) {
 160   return PICL::get_l1_data_cache_line_size(nodeh, result);
 161 }
 162 extern "C" static int PICL_get_l2_cache_line_size_helper(picl_nodehdl_t nodeh, void *result) {
 163   return PICL::get_l2_cache_line_size(nodeh, result);
 164 }
 165 
 166 // We need to keep these here as long as we have to build on Solaris
 167 // versions before 10.
 168 #ifndef SI_ARCHITECTURE_32
 169 #define SI_ARCHITECTURE_32      516     /* basic 32-bit SI_ARCHITECTURE */
 170 #endif
 171 
 172 #ifndef SI_ARCHITECTURE_64
 173 #define SI_ARCHITECTURE_64      517     /* basic 64-bit SI_ARCHITECTURE */
 174 #endif
 175 
 176 static void do_sysinfo(int si, const char* string, int* features, int mask) {
 177   char   tmp;
 178   size_t bufsize = sysinfo(si, &tmp, 1);
 179 
 180   // All SI defines used below must be supported.
 181   guarantee(bufsize != -1, "must be supported");
 182 
 183   char* buf = (char*) os::malloc(bufsize, mtInternal);
 184 
 185   if (buf == NULL)
 186     return;
 187 
 188   if (sysinfo(si, buf, bufsize) == bufsize) {
 189     // Compare the string.
 190     if (strcmp(buf, string) == 0) {
 191       *features |= mask;
 192     }
 193   }
 194 
 195   os::free(buf);
 196 }
 197 
 198 int VM_Version::platform_features(int features) {
 199   assert(os::Solaris::supports_getisax(), "getisax() must be available");
 200 
 201   // Check 32-bit architecture.
 202   do_sysinfo(SI_ARCHITECTURE_32, "sparc", &features, v8_instructions_m);
 203 
 204   // Check 64-bit architecture.
 205   do_sysinfo(SI_ARCHITECTURE_64, "sparcv9", &features, generic_v9_m);
 206 
 207   // Extract valid instruction set extensions.
 208   uint_t avs[2];
 209   uint_t avn = os::Solaris::getisax(avs, 2);
 210   assert(avn <= 2, "should return two or less av's");
 211   uint_t av = avs[0];
 212 
 213 #ifndef PRODUCT
 214   if (PrintMiscellaneous && Verbose) {
 215     tty->print("getisax(2) returned: " PTR32_FORMAT, av);
 216     if (avn > 1) {
 217       tty->print(", " PTR32_FORMAT, avs[1]);
 218     }
 219     tty->cr();
 220   }
 221 #endif
 222 
 223   if (av & AV_SPARC_MUL32)  features |= hardware_mul32_m;
 224   if (av & AV_SPARC_DIV32)  features |= hardware_div32_m;
 225   if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m;
 226   if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m;
 227   if (av & AV_SPARC_POPC)   features |= hardware_popc_m;
 228   if (av & AV_SPARC_VIS)    features |= vis1_instructions_m;
 229   if (av & AV_SPARC_VIS2)   features |= vis2_instructions_m;
 230   if (avn > 1) {
 231     uint_t av2 = avs[1];
 232 #ifndef AV2_SPARC_SPARC5
 233 #define AV2_SPARC_SPARC5 0x00000008 /* The 29 new fp and sub instructions */
 234 #endif
 235     if (av2 & AV2_SPARC_SPARC5)       features |= sparc5_instructions_m;
 236   }
 237 
 238   // We only build on Solaris 10 and up, but some of the values below
 239   // are not defined on all versions of Solaris 10, so we define them,
 240   // if necessary.
 241 #ifndef AV_SPARC_ASI_BLK_INIT
 242 #define AV_SPARC_ASI_BLK_INIT 0x0080  /* ASI_BLK_INIT_xxx ASI */
 243 #endif
 244   if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m;
 245 
 246 #ifndef AV_SPARC_FMAF
 247 #define AV_SPARC_FMAF 0x0100        /* Fused Multiply-Add */
 248 #endif
 249   if (av & AV_SPARC_FMAF)         features |= fmaf_instructions_m;
 250 
 251 #ifndef AV_SPARC_FMAU
 252 #define AV_SPARC_FMAU    0x0200  /* Unfused Multiply-Add */
 253 #endif
 254   if (av & AV_SPARC_FMAU)         features |= fmau_instructions_m;
 255 
 256 #ifndef AV_SPARC_VIS3
 257 #define AV_SPARC_VIS3    0x0400  /* VIS3 instruction set extensions */
 258 #endif
 259   if (av & AV_SPARC_VIS3)         features |= vis3_instructions_m;
 260 
 261 #ifndef AV_SPARC_CBCOND
 262 #define AV_SPARC_CBCOND 0x10000000  /* compare and branch instrs supported */
 263 #endif
 264   if (av & AV_SPARC_CBCOND)       features |= cbcond_instructions_m;
 265 
 266 #ifndef AV_SPARC_AES
 267 #define AV_SPARC_AES 0x00020000  /* aes instrs supported */
 268 #endif
 269   if (av & AV_SPARC_AES)       features |= aes_instructions_m;
 270 
 271 #ifndef AV_SPARC_SHA1
 272 #define AV_SPARC_SHA1   0x00400000  /* sha1 instruction supported */
 273 #endif
 274   if (av & AV_SPARC_SHA1)         features |= sha1_instruction_m;
 275 
 276 #ifndef AV_SPARC_SHA256
 277 #define AV_SPARC_SHA256 0x00800000  /* sha256 instruction supported */
 278 #endif
 279   if (av & AV_SPARC_SHA256)       features |= sha256_instruction_m;
 280 
 281 #ifndef AV_SPARC_SHA512
 282 #define AV_SPARC_SHA512 0x01000000  /* sha512 instruction supported */
 283 #endif
 284   if (av & AV_SPARC_SHA512)       features |= sha512_instruction_m;
 285 
 286   // Determine the machine type.
 287   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 288 
 289   {
 290     // Using kstat to determine the machine type.
 291     kstat_ctl_t* kc = kstat_open();
 292     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 293     const char* implementation = "UNKNOWN";
 294     if (ksp != NULL) {
 295       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 296         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 297         for (int i = 0; i < ksp->ks_ndata; i++) {
 298           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 299             implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 300 #ifndef PRODUCT
 301             if (PrintMiscellaneous && Verbose) {
 302               tty->print_cr("cpu_info.implementation: %s", implementation);
 303             }
 304 #endif
 305             // Convert to UPPER case before compare.
 306             char* impl = os::strdup_check_oom(implementation);
 307 
 308             for (int i = 0; impl[i] != 0; i++)
 309               impl[i] = (char)toupper((uint)impl[i]);
 310 
 311             if (strstr(impl, "SPARC64") != NULL) {
 312               features |= sparc64_family_m;
 313             } else if (strstr(impl, "SPARC-M") != NULL) {
 314               // M-series SPARC is based on T-series.
 315               features |= (M_family_m | T_family_m);
 316             } else if (strstr(impl, "SPARC-T") != NULL) {
 317               features |= T_family_m;
 318               if (strstr(impl, "SPARC-T1") != NULL) {
 319                 features |= T1_model_m;
 320               }
 321             } else {
 322               if (strstr(impl, "SPARC") == NULL) {
 323 #ifndef PRODUCT
 324                 // kstat on Solaris 8 virtual machines (branded zones)
 325                 // returns "(unsupported)" implementation. Solaris 8 is not
 326                 // supported anymore, but include this check to be on the
 327                 // safe side.
 328                 warning("kstat cpu_info implementation = '%s', assume generic SPARC", impl);
 329 #endif
 330                 implementation = "SPARC";
 331               }
 332             }
 333             os::free((void*)impl);
 334             break;
 335           }
 336         } // for(
 337       }
 338     }
 339     assert(strcmp(implementation, "UNKNOWN") != 0,
 340            "unknown cpu info (changed kstat interface?)");
 341     kstat_close(kc);
 342   }
 343 
 344   // Figure out cache line sizes using PICL
 345   PICL picl;
 346   _L1_data_cache_line_size = picl.L1_data_cache_line_size();
 347   _L2_cache_line_size      = picl.L2_cache_line_size();
 348 
 349   return features;
 350 }