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