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