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     void 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       }
 142     }
 143   };
 144 
 145   class CPUVisitor {
 146     UniqueValueVisitor _l1_visitor;
 147     UniqueValueVisitor _l2_visitor;
 148     int _limit; // number of times visit() can be run
 149   public:
 150     CPUVisitor(PICL *picl, int limit) : _l1_visitor(picl), _l2_visitor(picl), _limit(limit) {}
 151     static int visit(picl_nodehdl_t nodeh, void *arg) {
 152       CPUVisitor *cpu_visitor = static_cast<CPUVisitor*>(arg);
 153       UniqueValueVisitor* l1_visitor = cpu_visitor->l1_visitor();
 154       UniqueValueVisitor* l2_visitor = cpu_visitor->l2_visitor();
 155       if (!l1_visitor->is_inconsistent()) {
 156         l1_visitor->visit(nodeh, "l1-dcache-line-size");
 157       }
 158       if (!l2_visitor->is_inconsistent()) {
 159         l2_visitor->visit(nodeh, "l2-cache-line-size");
 160       }
 161 
 162       if (l1_visitor->is_inconsistent() && l2_visitor->is_inconsistent()) {
 163         return PICL_WALK_TERMINATE;
 164       }
 165       cpu_visitor->_limit--;
 166       if (cpu_visitor->_limit <= 0) {
 167         return PICL_WALK_TERMINATE;
 168       }
 169       return PICL_WALK_CONTINUE;
 170     }
 171     UniqueValueVisitor* l1_visitor() { return &_l1_visitor; }
 172     UniqueValueVisitor* l2_visitor() { return &_l2_visitor; }
 173   };
 174   int _L1_data_cache_line_size;
 175   int _L2_cache_line_size;
 176 public:
 177   static int visit_cpu(picl_nodehdl_t nodeh, void *state) {
 178     return CPUVisitor::visit(nodeh, state);
 179   }
 180 
 181   PICL(bool is_fujitsu) : _L1_data_cache_line_size(0), _L2_cache_line_size(0), _dl_handle(NULL) {
 182     if (!open_library()) {
 183       return;
 184     }
 185     if (_picl_initialize() == PICL_SUCCESS) {
 186       picl_nodehdl_t rooth;
 187       if (_picl_get_root(&rooth) == PICL_SUCCESS) {
 188         const char* cpu_class = "cpu";
 189         // If it's a Fujitsu machine, it's a "core"
 190         if (is_fujitsu) {
 191           cpu_class = "core";
 192         }
 193         CPUVisitor cpu_visitor(this, os::processor_count());
 194         _picl_walk_tree_by_class(rooth, cpu_class, &cpu_visitor, PICL_visit_cpu_helper);
 195         if (cpu_visitor.l1_visitor()->is_assigned()) { // Is there a value?
 196           _L1_data_cache_line_size = cpu_visitor.l1_visitor()->value();
 197         }
 198         if (cpu_visitor.l2_visitor()->is_assigned()) {
 199           _L2_cache_line_size = cpu_visitor.l2_visitor()->value();
 200         }
 201       }
 202       _picl_shutdown();
 203     }
 204     close_library();
 205   }
 206 
 207   unsigned int L1_data_cache_line_size() const { return _L1_data_cache_line_size; }
 208   unsigned int L2_cache_line_size() const      { return _L2_cache_line_size;      }
 209 };
 210 
 211 
 212 extern "C" static int PICL_visit_cpu_helper(picl_nodehdl_t nodeh, void *result) {
 213   return PICL::visit_cpu(nodeh, result);
 214 }
 215 
 216 template<typename FuncType>
 217 bool PICL::bind(FuncType& func, const char* name) {
 218   func = reinterpret_cast<FuncType>(dlsym(_dl_handle, name));
 219   return func != NULL;
 220 }
 221 
 222 bool PICL::bind_library_functions() {
 223   assert(_dl_handle != NULL, "library should be open");
 224   return bind(_picl_initialize,         "picl_initialize"        ) &&
 225          bind(_picl_shutdown,           "picl_shutdown"          ) &&
 226          bind(_picl_get_root,           "picl_get_root"          ) &&
 227          bind(_picl_walk_tree_by_class, "picl_walk_tree_by_class") &&
 228          bind(_picl_get_prop_by_name,   "picl_get_prop_by_name"  ) &&
 229          bind(_picl_get_propval,        "picl_get_propval"       ) &&
 230          bind(_picl_get_propinfo,       "picl_get_propinfo"      );
 231 }
 232 
 233 bool PICL::open_library() {
 234   _dl_handle = dlopen("libpicl.so.1", RTLD_LAZY);
 235   if (_dl_handle == NULL) {
 236     warning("PICL (libpicl.so.1) is missing. Performance will not be optimal.");
 237     return false;
 238   }
 239   if (!bind_library_functions()) {
 240     assert(false, "unexpected PICL API change");
 241     close_library();
 242     return false;
 243   }
 244   return true;
 245 }
 246 
 247 void PICL::close_library() {
 248   assert(_dl_handle != NULL, "library should be open");
 249   dlclose(_dl_handle);
 250   _dl_handle = NULL;
 251 }
 252 
 253 // We need to keep these here as long as we have to build on Solaris
 254 // versions before 10.
 255 #ifndef SI_ARCHITECTURE_32
 256 #define SI_ARCHITECTURE_32      516     /* basic 32-bit SI_ARCHITECTURE */
 257 #endif
 258 
 259 #ifndef SI_ARCHITECTURE_64
 260 #define SI_ARCHITECTURE_64      517     /* basic 64-bit SI_ARCHITECTURE */
 261 #endif
 262 
 263 static void do_sysinfo(int si, const char* string, int* features, int mask) {
 264   char   tmp;
 265   size_t bufsize = sysinfo(si, &tmp, 1);
 266 
 267   // All SI defines used below must be supported.
 268   guarantee(bufsize != -1, "must be supported");
 269 
 270   char* buf = (char*) os::malloc(bufsize, mtInternal);
 271 
 272   if (buf == NULL)
 273     return;
 274 
 275   if (sysinfo(si, buf, bufsize) == bufsize) {
 276     // Compare the string.
 277     if (strcmp(buf, string) == 0) {
 278       *features |= mask;
 279     }
 280   }
 281 
 282   os::free(buf);
 283 }
 284 
 285 int VM_Version::platform_features(int features) {
 286   assert(os::Solaris::supports_getisax(), "getisax() must be available");
 287 
 288   // Check 32-bit architecture.
 289   do_sysinfo(SI_ARCHITECTURE_32, "sparc", &features, v8_instructions_m);
 290 
 291   // Check 64-bit architecture.
 292   do_sysinfo(SI_ARCHITECTURE_64, "sparcv9", &features, generic_v9_m);
 293 
 294   // Extract valid instruction set extensions.
 295   uint_t avs[2];
 296   uint_t avn = os::Solaris::getisax(avs, 2);
 297   assert(avn <= 2, "should return two or less av's");
 298   uint_t av = avs[0];
 299 
 300 #ifndef PRODUCT
 301   if (PrintMiscellaneous && Verbose) {
 302     tty->print("getisax(2) returned: " PTR32_FORMAT, av);
 303     if (avn > 1) {
 304       tty->print(", " PTR32_FORMAT, avs[1]);
 305     }
 306     tty->cr();
 307   }
 308 #endif
 309 
 310   if (av & AV_SPARC_MUL32)  features |= hardware_mul32_m;
 311   if (av & AV_SPARC_DIV32)  features |= hardware_div32_m;
 312   if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m;
 313   if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m;
 314   if (av & AV_SPARC_POPC)   features |= hardware_popc_m;
 315   if (av & AV_SPARC_VIS)    features |= vis1_instructions_m;
 316   if (av & AV_SPARC_VIS2)   features |= vis2_instructions_m;
 317   if (avn > 1) {
 318     uint_t av2 = avs[1];
 319 #ifndef AV2_SPARC_SPARC5
 320 #define AV2_SPARC_SPARC5 0x00000008 /* The 29 new fp and sub instructions */
 321 #endif
 322     if (av2 & AV2_SPARC_SPARC5)       features |= sparc5_instructions_m;
 323   }
 324 
 325   // We only build on Solaris 10 and up, but some of the values below
 326   // are not defined on all versions of Solaris 10, so we define them,
 327   // if necessary.
 328 #ifndef AV_SPARC_ASI_BLK_INIT
 329 #define AV_SPARC_ASI_BLK_INIT 0x0080  /* ASI_BLK_INIT_xxx ASI */
 330 #endif
 331   if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m;
 332 
 333 #ifndef AV_SPARC_FMAF
 334 #define AV_SPARC_FMAF 0x0100        /* Fused Multiply-Add */
 335 #endif
 336   if (av & AV_SPARC_FMAF)         features |= fmaf_instructions_m;
 337 
 338 #ifndef AV_SPARC_FMAU
 339 #define AV_SPARC_FMAU    0x0200  /* Unfused Multiply-Add */
 340 #endif
 341   if (av & AV_SPARC_FMAU)         features |= fmau_instructions_m;
 342 
 343 #ifndef AV_SPARC_VIS3
 344 #define AV_SPARC_VIS3    0x0400  /* VIS3 instruction set extensions */
 345 #endif
 346   if (av & AV_SPARC_VIS3)         features |= vis3_instructions_m;
 347 
 348 #ifndef AV_SPARC_CBCOND
 349 #define AV_SPARC_CBCOND 0x10000000  /* compare and branch instrs supported */
 350 #endif
 351   if (av & AV_SPARC_CBCOND)       features |= cbcond_instructions_m;
 352 
 353 #ifndef AV_SPARC_AES
 354 #define AV_SPARC_AES 0x00020000  /* aes instrs supported */
 355 #endif
 356   if (av & AV_SPARC_AES)       features |= aes_instructions_m;
 357 
 358 #ifndef AV_SPARC_SHA1
 359 #define AV_SPARC_SHA1   0x00400000  /* sha1 instruction supported */
 360 #endif
 361   if (av & AV_SPARC_SHA1)         features |= sha1_instruction_m;
 362 
 363 #ifndef AV_SPARC_SHA256
 364 #define AV_SPARC_SHA256 0x00800000  /* sha256 instruction supported */
 365 #endif
 366   if (av & AV_SPARC_SHA256)       features |= sha256_instruction_m;
 367 
 368 #ifndef AV_SPARC_SHA512
 369 #define AV_SPARC_SHA512 0x01000000  /* sha512 instruction supported */
 370 #endif
 371   if (av & AV_SPARC_SHA512)       features |= sha512_instruction_m;
 372 
 373   // Determine the machine type.
 374   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 375 
 376   {
 377     // Using kstat to determine the machine type.
 378     kstat_ctl_t* kc = kstat_open();
 379     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 380     const char* implementation = "UNKNOWN";
 381     if (ksp != NULL) {
 382       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 383         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 384         for (int i = 0; i < ksp->ks_ndata; i++) {
 385           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 386             implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 387 #ifndef PRODUCT
 388             if (PrintMiscellaneous && Verbose) {
 389               tty->print_cr("cpu_info.implementation: %s", implementation);
 390             }
 391 #endif
 392             // Convert to UPPER case before compare.
 393             char* impl = os::strdup_check_oom(implementation);
 394 
 395             for (int i = 0; impl[i] != 0; i++)
 396               impl[i] = (char)toupper((uint)impl[i]);
 397 
 398             if (strstr(impl, "SPARC64") != NULL) {
 399               features |= sparc64_family_m;
 400             } else if (strstr(impl, "SPARC-M") != NULL) {
 401               // M-series SPARC is based on T-series.
 402               features |= (M_family_m | T_family_m);
 403             } else if (strstr(impl, "SPARC-T") != NULL) {
 404               features |= T_family_m;
 405               if (strstr(impl, "SPARC-T1") != NULL) {
 406                 features |= T1_model_m;
 407               }
 408             } else {
 409               if (strstr(impl, "SPARC") == NULL) {
 410 #ifndef PRODUCT
 411                 // kstat on Solaris 8 virtual machines (branded zones)
 412                 // returns "(unsupported)" implementation. Solaris 8 is not
 413                 // supported anymore, but include this check to be on the
 414                 // safe side.
 415                 warning("kstat cpu_info implementation = '%s', assume generic SPARC", impl);
 416 #endif
 417                 implementation = "SPARC";
 418               }
 419             }
 420             os::free((void*)impl);
 421             break;
 422           }
 423         } // for(
 424       }
 425     }
 426     assert(strcmp(implementation, "UNKNOWN") != 0,
 427            "unknown cpu info (changed kstat interface?)");
 428     kstat_close(kc);
 429   }
 430 
 431   // Figure out cache line sizes using PICL
 432   PICL picl((features & sparc64_family_m) != 0);
 433   _L1_data_cache_line_size = picl.L1_data_cache_line_size();
 434   _L2_cache_line_size      = picl.L2_cache_line_size();
 435 
 436   return features;
 437 }